1 /* filter.c - routines for parsing and dealing with filters */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
32 #include <ac/string.h>
37 const Filter *slap_filter_objectClass_pres;
38 const struct berval *slap_filterstr_objectClass_pres;
40 static int get_filter_list(
52 static void simple_vrFilter2bv(
54 ValuesReturnFilter *f,
55 struct berval *fstr );
57 static int get_simple_vrFilter(
60 ValuesReturnFilter **f,
66 static Filter filter_objectClass_pres = { LDAP_FILTER_PRESENT };
67 static struct berval filterstr_objectClass_pres = BER_BVC("(objectClass=*)");
69 filter_objectClass_pres.f_desc = slap_schema.si_ad_objectClass;
71 slap_filter_objectClass_pres = &filter_objectClass_pres;
72 slap_filterstr_objectClass_pres = &filterstr_objectClass_pres;
78 filter_destroy( void )
95 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
97 * A filter looks like this coming in:
99 * and [0] SET OF Filter,
100 * or [1] SET OF Filter,
102 * equalityMatch [3] AttributeValueAssertion,
103 * substrings [4] SubstringFilter,
104 * greaterOrEqual [5] AttributeValueAssertion,
105 * lessOrEqual [6] AttributeValueAssertion,
106 * present [7] AttributeType,
107 * approxMatch [8] AttributeValueAssertion,
108 * extensibleMatch [9] MatchingRuleAssertion
111 * SubstringFilter ::= SEQUENCE {
112 * type AttributeType,
113 * SEQUENCE OF CHOICE {
114 * initial [0] IA5String,
116 * final [2] IA5String
120 * MatchingRuleAssertion ::= SEQUENCE {
121 * matchingRule [1] MatchingRuleId OPTIONAL,
122 * type [2] AttributeDescription OPTIONAL,
123 * matchValue [3] AssertionValue,
124 * dnAttributes [4] BOOLEAN DEFAULT FALSE
129 tag = ber_peek_tag( ber, &len );
131 if( tag == LBER_ERROR ) {
132 *text = "error decoding filter";
133 return SLAPD_DISCONNECT;
141 switch ( f.f_choice ) {
142 case LDAP_FILTER_EQUALITY:
143 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
144 err = get_ava( op, ber, &f, SLAP_MR_EQUALITY, text );
145 if ( err != LDAP_SUCCESS ) {
149 assert( f.f_ava != NULL );
152 case LDAP_FILTER_SUBSTRINGS:
153 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
154 err = get_ssa( op, ber, &f, text );
155 if( err != LDAP_SUCCESS ) {
158 assert( f.f_sub != NULL );
162 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
163 err = get_ava( op, ber, &f, SLAP_MR_ORDERING, text );
164 if ( err != LDAP_SUCCESS ) {
167 assert( f.f_ava != NULL );
171 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
172 err = get_ava( op, ber, &f, SLAP_MR_ORDERING, text );
173 if ( err != LDAP_SUCCESS ) {
176 assert( f.f_ava != NULL );
179 case LDAP_FILTER_PRESENT: {
182 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
183 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
184 err = SLAPD_DISCONNECT;
185 *text = "error decoding filter";
190 err = slap_bv2ad( &type, &f.f_desc, text );
192 if( err != LDAP_SUCCESS ) {
193 f.f_choice |= SLAPD_FILTER_UNDEFINED;
194 err = slap_bv2undef_ad( &type, &f.f_desc, text,
195 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
197 if ( err != LDAP_SUCCESS ) {
198 /* unrecognized attribute description or other error */
199 Debug( LDAP_DEBUG_ANY,
200 "get_filter: conn %lu unknown attribute "
202 op->o_connid, type.bv_val, err );
205 f.f_desc = slap_bv2tmp_ad( &type, op->o_tmpmemctx );
210 assert( f.f_desc != NULL );
213 case LDAP_FILTER_APPROX:
214 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
215 err = get_ava( op, ber, &f, SLAP_MR_EQUALITY_APPROX, text );
216 if ( err != LDAP_SUCCESS ) {
219 assert( f.f_ava != NULL );
222 case LDAP_FILTER_AND:
223 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
224 err = get_filter_list( op, ber, &f.f_and, text );
225 if ( err != LDAP_SUCCESS ) {
228 if ( f.f_and == NULL ) {
229 f.f_choice = SLAPD_FILTER_COMPUTED;
230 f.f_result = LDAP_COMPARE_TRUE;
232 /* no assert - list could be empty */
236 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
237 err = get_filter_list( op, ber, &f.f_or, text );
238 if ( err != LDAP_SUCCESS ) {
241 if ( f.f_or == NULL ) {
242 f.f_choice = SLAPD_FILTER_COMPUTED;
243 f.f_result = LDAP_COMPARE_FALSE;
245 /* no assert - list could be empty */
248 case LDAP_FILTER_NOT:
249 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
250 (void) ber_skip_tag( ber, &len );
251 err = get_filter( op, ber, &f.f_not, text );
252 if ( err != LDAP_SUCCESS ) {
256 assert( f.f_not != NULL );
257 if ( f.f_not->f_choice == SLAPD_FILTER_COMPUTED ) {
258 int fresult = f.f_not->f_result;
259 f.f_choice = SLAPD_FILTER_COMPUTED;
260 op->o_tmpfree( f.f_not, op->o_tmpmemctx );
264 case LDAP_COMPARE_TRUE:
265 f.f_result = LDAP_COMPARE_FALSE;
267 case LDAP_COMPARE_FALSE:
268 f.f_result = LDAP_COMPARE_TRUE;
271 /* (!Undefined) is Undefined */
276 case LDAP_FILTER_EXT:
277 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
279 err = get_mra( op, ber, &f, text );
280 if ( err != LDAP_SUCCESS ) {
284 assert( f.f_mra != NULL );
288 (void) ber_scanf( ber, "x" ); /* skip the element */
289 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
291 f.f_choice = SLAPD_FILTER_COMPUTED;
292 f.f_result = SLAPD_COMPARE_UNDEFINED;
296 if( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
299 f.f_choice = SLAPD_FILTER_COMPUTED;
300 f.f_result = SLAPD_COMPARE_UNDEFINED;
304 if ( err == LDAP_SUCCESS ) {
305 *filt = op->o_tmpalloc( sizeof(f), op->o_tmpmemctx );
309 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
315 get_filter_list( Operation *op, BerElement *ber,
325 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
327 for ( tag = ber_first_element( ber, &len, &last );
329 tag = ber_next_element( ber, &len, last ) )
331 err = get_filter( op, ber, new, text );
332 if ( err != LDAP_SUCCESS )
334 new = &(*new)->f_next;
338 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
339 return( LDAP_SUCCESS );
352 struct berval desc, value, nvalue;
354 SubstringsAssertion ssa;
356 *text = "error decoding filter";
358 Debug( LDAP_DEBUG_FILTER, "begin get_ssa\n", 0, 0, 0 );
359 if ( ber_scanf( ber, "{m" /*}*/, &desc ) == LBER_ERROR ) {
360 return SLAPD_DISCONNECT;
366 ssa.sa_initial.bv_val = NULL;
368 ssa.sa_final.bv_val = NULL;
370 rc = slap_bv2ad( &desc, &ssa.sa_desc, text );
372 if( rc != LDAP_SUCCESS ) {
373 f->f_choice |= SLAPD_FILTER_UNDEFINED;
374 rc = slap_bv2undef_ad( &desc, &ssa.sa_desc, text,
375 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
377 if( rc != LDAP_SUCCESS ) {
378 Debug( LDAP_DEBUG_ANY,
379 "get_ssa: conn %lu unknown attribute type=%s (%ld)\n",
380 op->o_connid, desc.bv_val, (long) rc );
382 ssa.sa_desc = slap_bv2tmp_ad( &desc, op->o_tmpmemctx );
386 rc = LDAP_PROTOCOL_ERROR;
388 if ( ssa.sa_desc->ad_type->sat_substr == NULL ) {
389 for ( tag = ber_first_element( ber, &len, &last );
391 tag = ber_next_element( ber, &len, last ) )
394 rc = ber_scanf( ber, "x" );
397 rc = LDAP_INVALID_SYNTAX;
401 for ( tag = ber_first_element( ber, &len, &last );
403 tag = ber_next_element( ber, &len, last ) )
407 rc = ber_scanf( ber, "m", &value );
408 if ( rc == LBER_ERROR ) {
409 rc = SLAPD_DISCONNECT;
413 if ( value.bv_val == NULL || value.bv_len == 0 ) {
414 rc = LDAP_INVALID_SYNTAX;
419 case LDAP_SUBSTRING_INITIAL:
420 if ( ssa.sa_initial.bv_val != NULL
421 || ssa.sa_any != NULL
422 || ssa.sa_final.bv_val != NULL )
424 rc = LDAP_PROTOCOL_ERROR;
427 usage = SLAP_MR_SUBSTR_INITIAL;
430 case LDAP_SUBSTRING_ANY:
431 if ( ssa.sa_final.bv_val != NULL ) {
432 rc = LDAP_PROTOCOL_ERROR;
435 usage = SLAP_MR_SUBSTR_ANY;
438 case LDAP_SUBSTRING_FINAL:
439 if ( ssa.sa_final.bv_val != NULL ) {
440 rc = LDAP_PROTOCOL_ERROR;
444 usage = SLAP_MR_SUBSTR_FINAL;
448 Debug( LDAP_DEBUG_FILTER,
449 " unknown substring choice=%ld\n",
452 rc = LDAP_PROTOCOL_ERROR;
456 /* validate/normalize using equality matching rule validator! */
457 rc = asserted_value_validate_normalize(
458 ssa.sa_desc, ssa.sa_desc->ad_type->sat_equality,
459 usage, &value, &nvalue, text, op->o_tmpmemctx );
460 if( rc != LDAP_SUCCESS ) goto return_error;
463 case LDAP_SUBSTRING_INITIAL:
464 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
465 ssa.sa_initial = nvalue;
468 case LDAP_SUBSTRING_ANY:
469 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
470 ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx );
473 case LDAP_SUBSTRING_FINAL:
474 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
475 ssa.sa_final = nvalue;
480 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
481 rc = LDAP_PROTOCOL_ERROR;
484 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
486 slap_sl_free( ssa.sa_initial.bv_val, op->o_tmpmemctx );
487 ber_bvarray_free_x( ssa.sa_any, op->o_tmpmemctx );
488 if ( ssa.sa_desc->ad_flags & SLAP_DESC_TEMPORARY )
489 op->o_tmpfree( ssa.sa_desc, op->o_tmpmemctx );
490 slap_sl_free( ssa.sa_final.bv_val, op->o_tmpmemctx );
498 if( rc == LDAP_SUCCESS ) {
499 f->f_sub = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
503 Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
504 return rc /* LDAP_SUCCESS */ ;
508 filter_free_x( Operation *op, Filter *f, int freeme )
516 f->f_choice &= SLAPD_FILTER_MASK;
518 switch ( f->f_choice ) {
519 case LDAP_FILTER_PRESENT:
522 case LDAP_FILTER_EQUALITY:
525 case LDAP_FILTER_APPROX:
526 ava_free( op, f->f_ava, 1 );
529 case LDAP_FILTER_SUBSTRINGS:
530 if ( f->f_sub_initial.bv_val != NULL ) {
531 op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx );
533 ber_bvarray_free_x( f->f_sub_any, op->o_tmpmemctx );
534 if ( f->f_sub_final.bv_val != NULL ) {
535 op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx );
537 if ( f->f_sub->sa_desc->ad_flags & SLAP_DESC_TEMPORARY )
538 op->o_tmpfree( f->f_sub->sa_desc, op->o_tmpmemctx );
539 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
542 case LDAP_FILTER_AND:
544 case LDAP_FILTER_NOT:
545 for ( p = f->f_list; p != NULL; p = next ) {
547 filter_free_x( op, p, 1 );
551 case LDAP_FILTER_EXT:
552 mra_free( op, f->f_mra, 1 );
555 case SLAPD_FILTER_COMPUTED:
559 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
565 op->o_tmpfree( f, op->o_tmpmemctx );
570 filter_free( Filter *f )
576 op.o_tmpmemctx = slap_sl_context( f );
577 op.o_tmpmfuncs = &slap_sl_mfuncs;
578 filter_free_x( &op, f, 1 );
582 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
586 struct berval tmp, value;
588 ber_bvfalse = BER_BVC( "(?=false)" ),
589 ber_bvtrue = BER_BVC( "(?=true)" ),
590 ber_bvundefined = BER_BVC( "(?=undefined)" ),
591 ber_bverror = BER_BVC( "(?=error)" ),
592 ber_bvunknown = BER_BVC( "(?=unknown)" ),
593 ber_bvnone = BER_BVC( "(?=none)" );
600 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
604 undef = f->f_choice & SLAPD_FILTER_UNDEFINED;
605 choice = f->f_choice & SLAPD_FILTER_MASK;
608 case LDAP_FILTER_EQUALITY:
609 fstr->bv_len = STRLENOF("(=)");
613 fstr->bv_len = STRLENOF("(>=)");
617 fstr->bv_len = STRLENOF("(<=)");
620 case LDAP_FILTER_APPROX:
621 fstr->bv_len = STRLENOF("(~=)");
625 value = f->f_av_value;
626 if ( f->f_av_desc->ad_type->sat_equality &&
628 ( f->f_av_desc->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))
630 f->f_av_desc->ad_type->sat_equality->smr_normalize(
631 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
632 NULL, NULL, &f->f_av_value, &value, op->o_tmpmemctx );
635 filter_escape_value_x( &value, &tmp, op->o_tmpmemctx );
636 /* NOTE: tmp can legitimately be NULL (meaning empty)
637 * since in a Filter values in AVAs are supposed
638 * to have been normalized, meaning that an empty value
639 * is legal for that attribute's syntax */
641 fstr->bv_len += f->f_av_desc->ad_cname.bv_len + tmp.bv_len;
644 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
646 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s)",
648 f->f_av_desc->ad_cname.bv_val, sign,
649 tmp.bv_len ? tmp.bv_val : "" );
651 if ( value.bv_val != f->f_av_value.bv_val ) {
652 ber_memfree_x( value.bv_val, op->o_tmpmemctx );
655 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
658 case LDAP_FILTER_SUBSTRINGS:
659 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
663 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
665 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s=*)",
667 f->f_sub_desc->ad_cname.bv_val );
669 if ( f->f_sub_initial.bv_val != NULL ) {
674 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
677 fstr->bv_len += tmplen;
678 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
679 fstr->bv_len + 1, op->o_tmpmemctx );
681 snprintf( &fstr->bv_val[len - 2],
682 tmplen + STRLENOF( /*(*/ "*)" ) + 1,
683 /* "(attr=" */ "%s*)",
684 tmp.bv_len ? tmp.bv_val : "");
686 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
689 if ( f->f_sub_any != NULL ) {
690 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
694 filter_escape_value_x( &f->f_sub_any[i],
695 &tmp, op->o_tmpmemctx );
698 fstr->bv_len += tmplen + STRLENOF( /*(*/ ")" );
699 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
700 fstr->bv_len + 1, op->o_tmpmemctx );
702 snprintf( &fstr->bv_val[len - 1],
703 tmplen + STRLENOF( /*(*/ "*)" ) + 1,
704 /* "(attr=[init]*[any*]" */ "%s*)",
705 tmp.bv_len ? tmp.bv_val : "");
706 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
710 if ( f->f_sub_final.bv_val != NULL ) {
715 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
718 fstr->bv_len += tmplen;
719 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
720 fstr->bv_len + 1, op->o_tmpmemctx );
722 snprintf( &fstr->bv_val[len - 1],
723 tmplen + STRLENOF( /*(*/ ")" ) + 1,
724 /* "(attr=[init*][any*]" */ "%s)",
725 tmp.bv_len ? tmp.bv_val : "");
727 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
732 case LDAP_FILTER_PRESENT:
733 fstr->bv_len = f->f_desc->ad_cname.bv_len +
738 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
740 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s=*)",
742 f->f_desc->ad_cname.bv_val );
745 case LDAP_FILTER_AND:
747 case LDAP_FILTER_NOT:
748 fstr->bv_len = STRLENOF("(%)");
749 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
751 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
752 f->f_choice == LDAP_FILTER_AND ? '&' :
753 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
755 for ( p = f->f_list; p != NULL; p = p->f_next ) {
758 filter2bv_x( op, p, &tmp );
760 fstr->bv_len += tmp.bv_len;
761 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
764 snprintf( &fstr->bv_val[len-1],
765 tmp.bv_len + STRLENOF( /*(*/ ")" ) + 1,
766 /*"("*/ "%s)", tmp.bv_val );
768 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
773 case LDAP_FILTER_EXT: {
776 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
777 /* NOTE: tmp can legitimately be NULL (meaning empty)
778 * since in a Filter values in MRAs are supposed
779 * to have been normalized, meaning that an empty value
780 * is legal for that attribute's syntax */
782 if ( f->f_mr_desc ) {
783 ad = f->f_mr_desc->ad_cname;
789 fstr->bv_len = ad.bv_len +
790 ( f->f_mr_dnattrs ? STRLENOF(":dn") : 0 ) +
791 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
792 tmp.bv_len + STRLENOF("(:=)");
793 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
795 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s%s:=%s)",
798 f->f_mr_dnattrs ? ":dn" : "",
799 f->f_mr_rule_text.bv_len ? ":" : "",
800 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
801 tmp.bv_len ? tmp.bv_val : "" );
802 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
805 case SLAPD_FILTER_COMPUTED:
806 switch ( f->f_result ) {
807 case LDAP_COMPARE_FALSE:
811 case LDAP_COMPARE_TRUE:
815 case SLAPD_COMPARE_UNDEFINED:
816 tmp = ber_bvundefined;
824 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
828 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
834 filter2bv( Filter *f, struct berval *fstr )
840 op.o_tmpmemctx = NULL;
841 op.o_tmpmfuncs = &ch_mfuncs;
843 filter2bv_x( &op, f, fstr );
847 filter_dup( Filter *f, void *memctx )
849 BerMemoryFunctions *mf = &slap_sl_mfuncs;
855 n = mf->bmf_malloc( sizeof(Filter), memctx );
856 n->f_choice = f->f_choice;
859 switch( f->f_choice & SLAPD_FILTER_MASK ) {
860 case SLAPD_FILTER_COMPUTED:
861 n->f_result = f->f_result;
863 case LDAP_FILTER_PRESENT:
864 if ( f->f_desc->ad_flags & SLAP_DESC_TEMPORARY )
865 n->f_desc = slap_bv2tmp_ad( &f->f_desc->ad_cname, memctx );
867 n->f_desc = f->f_desc;
869 case LDAP_FILTER_EQUALITY:
872 case LDAP_FILTER_APPROX:
873 /* Should this be ava_dup() ? */
874 n->f_ava = mf->bmf_calloc( 1, sizeof(AttributeAssertion), memctx );
875 *n->f_ava = *f->f_ava;
876 if ( f->f_av_desc->ad_flags & SLAP_DESC_TEMPORARY )
877 n->f_av_desc = slap_bv2tmp_ad( &f->f_av_desc->ad_cname, memctx );
878 ber_dupbv_x( &n->f_av_value, &f->f_av_value, memctx );
880 case LDAP_FILTER_SUBSTRINGS:
881 n->f_sub = mf->bmf_calloc( 1, sizeof(SubstringsAssertion), memctx );
882 if ( f->f_sub_desc->ad_flags & SLAP_DESC_TEMPORARY )
883 n->f_sub_desc = slap_bv2tmp_ad( &f->f_sub_desc->ad_cname, memctx );
885 n->f_sub_desc = f->f_sub_desc;
886 if ( !BER_BVISNULL( &f->f_sub_initial ))
887 ber_dupbv_x( &n->f_sub_initial, &f->f_sub_initial, memctx );
888 if ( f->f_sub_any ) {
890 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ );
891 n->f_sub_any = mf->bmf_malloc(( i+1 )*sizeof( struct berval ),
893 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
894 ber_dupbv_x( &n->f_sub_any[i], &f->f_sub_any[i], memctx );
896 BER_BVZERO( &n->f_sub_any[i] );
898 if ( !BER_BVISNULL( &f->f_sub_final ))
899 ber_dupbv_x( &n->f_sub_final, &f->f_sub_final, memctx );
901 case LDAP_FILTER_EXT: {
902 /* Should this be mra_dup() ? */
904 length = sizeof(MatchingRuleAssertion);
905 if ( !BER_BVISNULL( &f->f_mr_rule_text ))
906 length += f->f_mr_rule_text.bv_len + 1;
907 n->f_mra = mf->bmf_calloc( 1, length, memctx );
908 *n->f_mra = *f->f_mra;
909 if ( f->f_mr_desc && ( f->f_sub_desc->ad_flags & SLAP_DESC_TEMPORARY ))
910 n->f_mr_desc = slap_bv2tmp_ad( &f->f_mr_desc->ad_cname, memctx );
911 ber_dupbv_x( &n->f_mr_value, &f->f_mr_value, memctx );
912 if ( !BER_BVISNULL( &f->f_mr_rule_text )) {
913 n->f_mr_rule_text.bv_val = (char *)(n->f_mra+1);
914 AC_MEMCPY(n->f_mr_rule_text.bv_val,
915 f->f_mr_rule_text.bv_val, f->f_mr_rule_text.bv_len );
918 case LDAP_FILTER_AND:
920 case LDAP_FILTER_NOT: {
922 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
923 *p = filter_dup( f, memctx );
935 ValuesReturnFilter **filt,
941 ValuesReturnFilter vrf;
943 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
945 tag = ber_peek_tag( ber, &len );
947 if( tag == LBER_ERROR ) {
948 *text = "error decoding filter";
949 return SLAPD_DISCONNECT;
955 vrf.vrf_choice = tag;
957 switch ( vrf.vrf_choice ) {
958 case LDAP_FILTER_EQUALITY:
959 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
960 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_EQUALITY, text );
961 if ( err != LDAP_SUCCESS ) {
965 assert( vrf.vrf_ava != NULL );
968 case LDAP_FILTER_SUBSTRINGS:
969 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
970 err = get_ssa( op, ber, (Filter *)&vrf, text );
974 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
975 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_ORDERING, text );
976 if ( err != LDAP_SUCCESS ) {
982 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
983 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_ORDERING, text );
984 if ( err != LDAP_SUCCESS ) {
989 case LDAP_FILTER_PRESENT: {
992 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
993 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
994 err = SLAPD_DISCONNECT;
995 *text = "error decoding filter";
1000 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
1002 if( err != LDAP_SUCCESS ) {
1003 vrf.vrf_choice |= SLAPD_FILTER_UNDEFINED;
1004 err = slap_bv2undef_ad( &type, &vrf.vrf_desc, text,
1007 if( err != LDAP_SUCCESS ) {
1008 /* unrecognized attribute description or other error */
1009 Debug( LDAP_DEBUG_ANY,
1010 "get_simple_vrFilter: conn %lu unknown "
1011 "attribute type=%s (%d)\n",
1012 op->o_connid, type.bv_val, err );
1014 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1015 vrf.vrf_result = LDAP_COMPARE_FALSE;
1022 case LDAP_FILTER_APPROX:
1023 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
1024 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_EQUALITY_APPROX, text );
1025 if ( err != LDAP_SUCCESS ) {
1030 case LDAP_FILTER_EXT:
1031 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1033 err = get_mra( op, ber, (Filter *)&vrf, text );
1034 if ( err != LDAP_SUCCESS ) {
1038 assert( vrf.vrf_mra != NULL );
1042 (void) ber_scanf( ber, "x" ); /* skip the element */
1043 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1044 vrf.vrf_choice, 0, 0 );
1045 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1046 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1050 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1052 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1053 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1057 if ( err == LDAP_SUCCESS ) {
1058 *filt = op->o_tmpalloc( sizeof vrf, op->o_tmpmemctx );
1062 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1068 get_vrFilter( Operation *op, BerElement *ber,
1069 ValuesReturnFilter **vrf,
1073 * A ValuesReturnFilter looks like this:
1075 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1076 * SimpleFilterItem ::= CHOICE {
1077 * equalityMatch [3] AttributeValueAssertion,
1078 * substrings [4] SubstringFilter,
1079 * greaterOrEqual [5] AttributeValueAssertion,
1080 * lessOrEqual [6] AttributeValueAssertion,
1081 * present [7] AttributeType,
1082 * approxMatch [8] AttributeValueAssertion,
1083 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1086 * SubstringFilter ::= SEQUENCE {
1087 * type AttributeType,
1088 * SEQUENCE OF CHOICE {
1089 * initial [0] IA5String,
1090 * any [1] IA5String,
1091 * final [2] IA5String
1095 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1096 * matchingRule [1] MatchingRuleId OPTIONAL,
1097 * type [2] AttributeDescription OPTIONAL,
1098 * matchValue [3] AssertionValue }
1101 ValuesReturnFilter **n;
1106 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1108 tag = ber_peek_tag( ber, &len );
1110 if( tag == LBER_ERROR ) {
1111 *text = "error decoding vrFilter";
1112 return SLAPD_DISCONNECT;
1115 if( tag != LBER_SEQUENCE ) {
1116 *text = "error decoding vrFilter, expect SEQUENCE tag";
1117 return SLAPD_DISCONNECT;
1121 for ( tag = ber_first_element( ber, &len, &last );
1122 tag != LBER_DEFAULT;
1123 tag = ber_next_element( ber, &len, last ) )
1125 int err = get_simple_vrFilter( op, ber, n, text );
1127 if ( err != LDAP_SUCCESS ) return( err );
1129 n = &(*n)->vrf_next;
1133 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1134 return( LDAP_SUCCESS );
1138 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1140 ValuesReturnFilter *p, *next;
1142 if ( vrf == NULL ) {
1146 for ( p = vrf; p != NULL; p = next ) {
1149 switch ( vrf->vrf_choice & SLAPD_FILTER_MASK ) {
1150 case LDAP_FILTER_PRESENT:
1153 case LDAP_FILTER_EQUALITY:
1154 case LDAP_FILTER_GE:
1155 case LDAP_FILTER_LE:
1156 case LDAP_FILTER_APPROX:
1157 ava_free( op, vrf->vrf_ava, 1 );
1160 case LDAP_FILTER_SUBSTRINGS:
1161 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1162 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1164 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1165 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1166 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1168 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1171 case LDAP_FILTER_EXT:
1172 mra_free( op, vrf->vrf_mra, 1 );
1175 case SLAPD_FILTER_COMPUTED:
1179 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1180 vrf->vrf_choice, 0, 0 );
1184 op->o_tmpfree( vrf, op->o_tmpmemctx );
1189 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1191 ValuesReturnFilter *p;
1195 if ( vrf == NULL ) {
1196 ber_str2bv_x( "No filter!", STRLENOF("No filter!"),
1197 1, fstr, op->o_tmpmemctx );
1201 fstr->bv_len = STRLENOF("()");
1202 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1204 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1206 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1209 simple_vrFilter2bv( op, p, &tmp );
1211 fstr->bv_len += tmp.bv_len;
1212 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1215 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1216 /*"("*/ "%s)", tmp.bv_val );
1218 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1223 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1229 if ( vrf == NULL ) {
1230 ber_str2bv_x( "No filter!", STRLENOF("No filter!"), 1, fstr,
1234 undef = vrf->vrf_choice & SLAPD_FILTER_UNDEFINED;
1236 switch ( vrf->vrf_choice & SLAPD_FILTER_MASK ) {
1237 case LDAP_FILTER_EQUALITY:
1238 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1240 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1241 tmp.bv_len + STRLENOF("(=)");
1242 if ( undef ) fstr->bv_len++;
1243 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1245 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1246 vrf->vrf_av_desc->ad_cname.bv_val,
1249 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1252 case LDAP_FILTER_GE:
1253 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1255 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1256 tmp.bv_len + STRLENOF("(>=)");
1257 if ( undef ) fstr->bv_len++;
1258 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1260 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1261 vrf->vrf_av_desc->ad_cname.bv_val,
1264 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1267 case LDAP_FILTER_LE:
1268 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1270 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1271 tmp.bv_len + STRLENOF("(<=)");
1272 if ( undef ) fstr->bv_len++;
1273 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1275 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1276 vrf->vrf_av_desc->ad_cname.bv_val,
1279 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1282 case LDAP_FILTER_APPROX:
1283 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1285 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1286 tmp.bv_len + STRLENOF("(~=)");
1287 if ( undef ) fstr->bv_len++;
1288 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1290 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1291 vrf->vrf_av_desc->ad_cname.bv_val,
1293 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1296 case LDAP_FILTER_SUBSTRINGS:
1297 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1299 if ( undef ) fstr->bv_len++;
1300 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1302 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1303 vrf->vrf_sub_desc->ad_cname.bv_val );
1305 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1308 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1310 fstr->bv_len += tmp.bv_len;
1311 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1314 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1315 /* "(attr=" */ "%s*)",
1318 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1321 if ( vrf->vrf_sub_any != NULL ) {
1323 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1325 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1328 fstr->bv_len += tmp.bv_len + 1;
1329 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1330 fstr->bv_len + 1, op->o_tmpmemctx );
1332 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1333 /* "(attr=[init]*[any*]" */ "%s*)",
1335 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1339 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1342 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1344 fstr->bv_len += tmp.bv_len;
1345 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1348 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1349 /* "(attr=[init*][any*]" */ "%s)",
1352 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1357 case LDAP_FILTER_PRESENT:
1358 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1360 if ( undef ) fstr->bv_len++;
1361 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1363 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1364 vrf->vrf_desc->ad_cname.bv_val );
1367 case LDAP_FILTER_EXT: {
1369 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1371 if ( vrf->vrf_mr_desc ) {
1372 ad = vrf->vrf_mr_desc->ad_cname;
1378 fstr->bv_len = ad.bv_len +
1379 ( vrf->vrf_mr_dnattrs ? STRLENOF(":dn") : 0 ) +
1380 ( vrf->vrf_mr_rule_text.bv_len
1381 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1382 tmp.bv_len + STRLENOF("(:=)");
1383 if ( undef ) fstr->bv_len++;
1384 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1386 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1388 vrf->vrf_mr_dnattrs ? ":dn" : "",
1389 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1390 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1393 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1396 case SLAPD_FILTER_COMPUTED:
1398 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1399 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1400 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1401 ? "(?=undefined)" : "(?=error)",
1402 vrf->vrf_result == LDAP_COMPARE_FALSE ? STRLENOF("(?=false)") :
1403 vrf->vrf_result == LDAP_COMPARE_TRUE ? STRLENOF("(?=true)") :
1404 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1405 ? STRLENOF("(?=undefined)") : STRLENOF("(?=error)"),
1406 1, fstr, op->o_tmpmemctx );
1410 ber_str2bv_x( "(?=unknown)", STRLENOF("(?=unknown)"),
1411 1, fstr, op->o_tmpmemctx );