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-2010 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 there is no substring matching rule, there's nothing
389 * we can do with this filter. But we continue to parse it
390 * for logging purposes.
392 if ( ssa.sa_desc->ad_type->sat_substr == NULL ) {
393 f->f_choice |= SLAPD_FILTER_UNDEFINED;
394 Debug( LDAP_DEBUG_FILTER,
395 "get_ssa: no substring matching rule for attributeType %s\n",
399 for ( tag = ber_first_element( ber, &len, &last );
401 tag = ber_next_element( ber, &len, last ) )
405 rc = ber_scanf( ber, "m", &value );
406 if ( rc == LBER_ERROR ) {
407 rc = SLAPD_DISCONNECT;
411 if ( value.bv_val == NULL || value.bv_len == 0 ) {
412 rc = LDAP_INVALID_SYNTAX;
417 case LDAP_SUBSTRING_INITIAL:
418 if ( ssa.sa_initial.bv_val != NULL
419 || ssa.sa_any != NULL
420 || ssa.sa_final.bv_val != NULL )
422 rc = LDAP_PROTOCOL_ERROR;
425 usage = SLAP_MR_SUBSTR_INITIAL;
428 case LDAP_SUBSTRING_ANY:
429 if ( ssa.sa_final.bv_val != NULL ) {
430 rc = LDAP_PROTOCOL_ERROR;
433 usage = SLAP_MR_SUBSTR_ANY;
436 case LDAP_SUBSTRING_FINAL:
437 if ( ssa.sa_final.bv_val != NULL ) {
438 rc = LDAP_PROTOCOL_ERROR;
442 usage = SLAP_MR_SUBSTR_FINAL;
446 Debug( LDAP_DEBUG_FILTER,
447 " unknown substring choice=%ld\n",
450 rc = LDAP_PROTOCOL_ERROR;
454 /* validate/normalize using equality matching rule validator! */
455 rc = asserted_value_validate_normalize(
456 ssa.sa_desc, ssa.sa_desc->ad_type->sat_equality,
457 usage, &value, &nvalue, text, op->o_tmpmemctx );
458 if( rc != LDAP_SUCCESS ) {
459 f->f_choice |= SLAPD_FILTER_UNDEFINED;
460 Debug( LDAP_DEBUG_FILTER,
461 "get_ssa: illegal value for attributeType %s (%d) %s\n",
462 desc.bv_val, rc, *text );
463 ber_dupbv_x( &nvalue, &value, op->o_tmpmemctx );
467 case LDAP_SUBSTRING_INITIAL:
468 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
469 ssa.sa_initial = nvalue;
472 case LDAP_SUBSTRING_ANY:
473 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
474 ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx );
477 case LDAP_SUBSTRING_FINAL:
478 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
479 ssa.sa_final = nvalue;
484 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
485 rc = LDAP_PROTOCOL_ERROR;
488 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
490 slap_sl_free( ssa.sa_initial.bv_val, op->o_tmpmemctx );
491 ber_bvarray_free_x( ssa.sa_any, op->o_tmpmemctx );
492 if ( ssa.sa_desc->ad_flags & SLAP_DESC_TEMPORARY )
493 op->o_tmpfree( ssa.sa_desc, op->o_tmpmemctx );
494 slap_sl_free( ssa.sa_final.bv_val, op->o_tmpmemctx );
502 if( rc == LDAP_SUCCESS ) {
503 f->f_sub = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
507 Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
508 return rc /* LDAP_SUCCESS */ ;
512 filter_free_x( Operation *op, Filter *f, int freeme )
520 f->f_choice &= SLAPD_FILTER_MASK;
522 switch ( f->f_choice ) {
523 case LDAP_FILTER_PRESENT:
526 case LDAP_FILTER_EQUALITY:
529 case LDAP_FILTER_APPROX:
530 ava_free( op, f->f_ava, 1 );
533 case LDAP_FILTER_SUBSTRINGS:
534 if ( f->f_sub_initial.bv_val != NULL ) {
535 op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx );
537 ber_bvarray_free_x( f->f_sub_any, op->o_tmpmemctx );
538 if ( f->f_sub_final.bv_val != NULL ) {
539 op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx );
541 if ( f->f_sub->sa_desc->ad_flags & SLAP_DESC_TEMPORARY )
542 op->o_tmpfree( f->f_sub->sa_desc, op->o_tmpmemctx );
543 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
546 case LDAP_FILTER_AND:
548 case LDAP_FILTER_NOT:
549 for ( p = f->f_list; p != NULL; p = next ) {
551 filter_free_x( op, p, 1 );
555 case LDAP_FILTER_EXT:
556 mra_free( op, f->f_mra, 1 );
559 case SLAPD_FILTER_COMPUTED:
563 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
569 op->o_tmpfree( f, op->o_tmpmemctx );
574 filter_free( Filter *f )
580 op.o_tmpmemctx = slap_sl_context( f );
581 op.o_tmpmfuncs = &slap_sl_mfuncs;
582 filter_free_x( &op, f, 1 );
586 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
590 struct berval tmp, value;
592 ber_bvfalse = BER_BVC( "(?=false)" ),
593 ber_bvtrue = BER_BVC( "(?=true)" ),
594 ber_bvundefined = BER_BVC( "(?=undefined)" ),
595 ber_bverror = BER_BVC( "(?=error)" ),
596 ber_bvunknown = BER_BVC( "(?=unknown)" ),
597 ber_bvnone = BER_BVC( "(?=none)" );
604 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
608 undef = f->f_choice & SLAPD_FILTER_UNDEFINED;
609 choice = f->f_choice & SLAPD_FILTER_MASK;
612 case LDAP_FILTER_EQUALITY:
613 fstr->bv_len = STRLENOF("(=)");
617 fstr->bv_len = STRLENOF("(>=)");
621 fstr->bv_len = STRLENOF("(<=)");
624 case LDAP_FILTER_APPROX:
625 fstr->bv_len = STRLENOF("(~=)");
629 value = f->f_av_value;
630 if ( f->f_av_desc->ad_type->sat_equality &&
632 ( f->f_av_desc->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))
634 f->f_av_desc->ad_type->sat_equality->smr_normalize(
635 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
636 NULL, NULL, &f->f_av_value, &value, op->o_tmpmemctx );
639 filter_escape_value_x( &value, &tmp, op->o_tmpmemctx );
640 /* NOTE: tmp can legitimately be NULL (meaning empty)
641 * since in a Filter values in AVAs are supposed
642 * to have been normalized, meaning that an empty value
643 * is legal for that attribute's syntax */
645 fstr->bv_len += f->f_av_desc->ad_cname.bv_len + tmp.bv_len;
648 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
650 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s)",
652 f->f_av_desc->ad_cname.bv_val, sign,
653 tmp.bv_len ? tmp.bv_val : "" );
655 if ( value.bv_val != f->f_av_value.bv_val ) {
656 ber_memfree_x( value.bv_val, op->o_tmpmemctx );
659 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
662 case LDAP_FILTER_SUBSTRINGS:
663 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
667 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
669 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s=*)",
671 f->f_sub_desc->ad_cname.bv_val );
673 if ( f->f_sub_initial.bv_val != NULL ) {
678 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
681 fstr->bv_len += tmplen;
682 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
683 fstr->bv_len + 1, op->o_tmpmemctx );
685 snprintf( &fstr->bv_val[len - 2],
686 tmplen + STRLENOF( /*(*/ "*)" ) + 1,
687 /* "(attr=" */ "%s*)",
688 tmp.bv_len ? tmp.bv_val : "");
690 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
693 if ( f->f_sub_any != NULL ) {
694 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
698 filter_escape_value_x( &f->f_sub_any[i],
699 &tmp, op->o_tmpmemctx );
702 fstr->bv_len += tmplen + STRLENOF( /*(*/ ")" );
703 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
704 fstr->bv_len + 1, op->o_tmpmemctx );
706 snprintf( &fstr->bv_val[len - 1],
707 tmplen + STRLENOF( /*(*/ "*)" ) + 1,
708 /* "(attr=[init]*[any*]" */ "%s*)",
709 tmp.bv_len ? tmp.bv_val : "");
710 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
714 if ( f->f_sub_final.bv_val != NULL ) {
719 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
722 fstr->bv_len += tmplen;
723 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
724 fstr->bv_len + 1, op->o_tmpmemctx );
726 snprintf( &fstr->bv_val[len - 1],
727 tmplen + STRLENOF( /*(*/ ")" ) + 1,
728 /* "(attr=[init*][any*]" */ "%s)",
729 tmp.bv_len ? tmp.bv_val : "");
731 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
736 case LDAP_FILTER_PRESENT:
737 fstr->bv_len = f->f_desc->ad_cname.bv_len +
742 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
744 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s=*)",
746 f->f_desc->ad_cname.bv_val );
749 case LDAP_FILTER_AND:
751 case LDAP_FILTER_NOT:
752 fstr->bv_len = STRLENOF("(%)");
753 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
755 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
756 f->f_choice == LDAP_FILTER_AND ? '&' :
757 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
759 for ( p = f->f_list; p != NULL; p = p->f_next ) {
762 filter2bv_x( op, p, &tmp );
764 fstr->bv_len += tmp.bv_len;
765 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
768 snprintf( &fstr->bv_val[len-1],
769 tmp.bv_len + STRLENOF( /*(*/ ")" ) + 1,
770 /*"("*/ "%s)", tmp.bv_val );
772 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
777 case LDAP_FILTER_EXT: {
780 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
781 /* NOTE: tmp can legitimately be NULL (meaning empty)
782 * since in a Filter values in MRAs are supposed
783 * to have been normalized, meaning that an empty value
784 * is legal for that attribute's syntax */
786 if ( f->f_mr_desc ) {
787 ad = f->f_mr_desc->ad_cname;
793 fstr->bv_len = ad.bv_len +
794 ( f->f_mr_dnattrs ? STRLENOF(":dn") : 0 ) +
795 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
796 tmp.bv_len + STRLENOF("(:=)");
797 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
799 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s%s:=%s)",
802 f->f_mr_dnattrs ? ":dn" : "",
803 f->f_mr_rule_text.bv_len ? ":" : "",
804 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
805 tmp.bv_len ? tmp.bv_val : "" );
806 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
809 case SLAPD_FILTER_COMPUTED:
810 switch ( f->f_result ) {
811 case LDAP_COMPARE_FALSE:
815 case LDAP_COMPARE_TRUE:
819 case SLAPD_COMPARE_UNDEFINED:
820 tmp = ber_bvundefined;
828 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
832 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
838 filter2bv( Filter *f, struct berval *fstr )
844 op.o_tmpmemctx = NULL;
845 op.o_tmpmfuncs = &ch_mfuncs;
847 filter2bv_x( &op, f, fstr );
851 filter_dup( Filter *f, void *memctx )
853 BerMemoryFunctions *mf = &slap_sl_mfuncs;
859 n = mf->bmf_malloc( sizeof(Filter), memctx );
860 n->f_choice = f->f_choice;
863 switch( f->f_choice & SLAPD_FILTER_MASK ) {
864 case SLAPD_FILTER_COMPUTED:
865 n->f_result = f->f_result;
867 case LDAP_FILTER_PRESENT:
868 if ( f->f_desc->ad_flags & SLAP_DESC_TEMPORARY )
869 n->f_desc = slap_bv2tmp_ad( &f->f_desc->ad_cname, memctx );
871 n->f_desc = f->f_desc;
873 case LDAP_FILTER_EQUALITY:
876 case LDAP_FILTER_APPROX:
877 /* Should this be ava_dup() ? */
878 n->f_ava = mf->bmf_calloc( 1, sizeof(AttributeAssertion), memctx );
879 *n->f_ava = *f->f_ava;
880 if ( f->f_av_desc->ad_flags & SLAP_DESC_TEMPORARY )
881 n->f_av_desc = slap_bv2tmp_ad( &f->f_av_desc->ad_cname, memctx );
882 ber_dupbv_x( &n->f_av_value, &f->f_av_value, memctx );
884 case LDAP_FILTER_SUBSTRINGS:
885 n->f_sub = mf->bmf_calloc( 1, sizeof(SubstringsAssertion), memctx );
886 if ( f->f_sub_desc->ad_flags & SLAP_DESC_TEMPORARY )
887 n->f_sub_desc = slap_bv2tmp_ad( &f->f_sub_desc->ad_cname, memctx );
889 n->f_sub_desc = f->f_sub_desc;
890 if ( !BER_BVISNULL( &f->f_sub_initial ))
891 ber_dupbv_x( &n->f_sub_initial, &f->f_sub_initial, memctx );
892 if ( f->f_sub_any ) {
894 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ );
895 n->f_sub_any = mf->bmf_malloc(( i+1 )*sizeof( struct berval ),
897 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
898 ber_dupbv_x( &n->f_sub_any[i], &f->f_sub_any[i], memctx );
900 BER_BVZERO( &n->f_sub_any[i] );
902 if ( !BER_BVISNULL( &f->f_sub_final ))
903 ber_dupbv_x( &n->f_sub_final, &f->f_sub_final, memctx );
905 case LDAP_FILTER_EXT: {
906 /* Should this be mra_dup() ? */
908 length = sizeof(MatchingRuleAssertion);
909 if ( !BER_BVISNULL( &f->f_mr_rule_text ))
910 length += f->f_mr_rule_text.bv_len + 1;
911 n->f_mra = mf->bmf_calloc( 1, length, memctx );
912 *n->f_mra = *f->f_mra;
913 if ( f->f_mr_desc && ( f->f_sub_desc->ad_flags & SLAP_DESC_TEMPORARY ))
914 n->f_mr_desc = slap_bv2tmp_ad( &f->f_mr_desc->ad_cname, memctx );
915 ber_dupbv_x( &n->f_mr_value, &f->f_mr_value, memctx );
916 if ( !BER_BVISNULL( &f->f_mr_rule_text )) {
917 n->f_mr_rule_text.bv_val = (char *)(n->f_mra+1);
918 AC_MEMCPY(n->f_mr_rule_text.bv_val,
919 f->f_mr_rule_text.bv_val, f->f_mr_rule_text.bv_len );
922 case LDAP_FILTER_AND:
924 case LDAP_FILTER_NOT: {
926 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
927 *p = filter_dup( f, memctx );
939 ValuesReturnFilter **filt,
945 ValuesReturnFilter vrf;
947 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
949 tag = ber_peek_tag( ber, &len );
951 if( tag == LBER_ERROR ) {
952 *text = "error decoding filter";
953 return SLAPD_DISCONNECT;
959 vrf.vrf_choice = tag;
961 switch ( vrf.vrf_choice ) {
962 case LDAP_FILTER_EQUALITY:
963 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
964 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_EQUALITY, text );
965 if ( err != LDAP_SUCCESS ) {
969 assert( vrf.vrf_ava != NULL );
972 case LDAP_FILTER_SUBSTRINGS:
973 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
974 err = get_ssa( op, ber, (Filter *)&vrf, text );
978 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
979 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_ORDERING, text );
980 if ( err != LDAP_SUCCESS ) {
986 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
987 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_ORDERING, text );
988 if ( err != LDAP_SUCCESS ) {
993 case LDAP_FILTER_PRESENT: {
996 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
997 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
998 err = SLAPD_DISCONNECT;
999 *text = "error decoding filter";
1003 vrf.vrf_desc = NULL;
1004 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
1006 if( err != LDAP_SUCCESS ) {
1007 vrf.vrf_choice |= SLAPD_FILTER_UNDEFINED;
1008 err = slap_bv2undef_ad( &type, &vrf.vrf_desc, text,
1011 if( err != LDAP_SUCCESS ) {
1012 /* unrecognized attribute description or other error */
1013 Debug( LDAP_DEBUG_ANY,
1014 "get_simple_vrFilter: conn %lu unknown "
1015 "attribute type=%s (%d)\n",
1016 op->o_connid, type.bv_val, err );
1018 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1019 vrf.vrf_result = LDAP_COMPARE_FALSE;
1026 case LDAP_FILTER_APPROX:
1027 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
1028 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_EQUALITY_APPROX, text );
1029 if ( err != LDAP_SUCCESS ) {
1034 case LDAP_FILTER_EXT:
1035 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1037 err = get_mra( op, ber, (Filter *)&vrf, text );
1038 if ( err != LDAP_SUCCESS ) {
1042 assert( vrf.vrf_mra != NULL );
1046 (void) ber_scanf( ber, "x" ); /* skip the element */
1047 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1048 vrf.vrf_choice, 0, 0 );
1049 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1050 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1054 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1056 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1057 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1061 if ( err == LDAP_SUCCESS ) {
1062 *filt = op->o_tmpalloc( sizeof vrf, op->o_tmpmemctx );
1066 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1072 get_vrFilter( Operation *op, BerElement *ber,
1073 ValuesReturnFilter **vrf,
1077 * A ValuesReturnFilter looks like this:
1079 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1080 * SimpleFilterItem ::= CHOICE {
1081 * equalityMatch [3] AttributeValueAssertion,
1082 * substrings [4] SubstringFilter,
1083 * greaterOrEqual [5] AttributeValueAssertion,
1084 * lessOrEqual [6] AttributeValueAssertion,
1085 * present [7] AttributeType,
1086 * approxMatch [8] AttributeValueAssertion,
1087 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1090 * SubstringFilter ::= SEQUENCE {
1091 * type AttributeType,
1092 * SEQUENCE OF CHOICE {
1093 * initial [0] IA5String,
1094 * any [1] IA5String,
1095 * final [2] IA5String
1099 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1100 * matchingRule [1] MatchingRuleId OPTIONAL,
1101 * type [2] AttributeDescription OPTIONAL,
1102 * matchValue [3] AssertionValue }
1105 ValuesReturnFilter **n;
1110 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1112 tag = ber_peek_tag( ber, &len );
1114 if( tag == LBER_ERROR ) {
1115 *text = "error decoding vrFilter";
1116 return SLAPD_DISCONNECT;
1119 if( tag != LBER_SEQUENCE ) {
1120 *text = "error decoding vrFilter, expect SEQUENCE tag";
1121 return SLAPD_DISCONNECT;
1125 for ( tag = ber_first_element( ber, &len, &last );
1126 tag != LBER_DEFAULT;
1127 tag = ber_next_element( ber, &len, last ) )
1129 int err = get_simple_vrFilter( op, ber, n, text );
1131 if ( err != LDAP_SUCCESS ) return( err );
1133 n = &(*n)->vrf_next;
1137 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1138 return( LDAP_SUCCESS );
1142 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1144 ValuesReturnFilter *p, *next;
1146 if ( vrf == NULL ) {
1150 for ( p = vrf; p != NULL; p = next ) {
1153 switch ( vrf->vrf_choice & SLAPD_FILTER_MASK ) {
1154 case LDAP_FILTER_PRESENT:
1157 case LDAP_FILTER_EQUALITY:
1158 case LDAP_FILTER_GE:
1159 case LDAP_FILTER_LE:
1160 case LDAP_FILTER_APPROX:
1161 ava_free( op, vrf->vrf_ava, 1 );
1164 case LDAP_FILTER_SUBSTRINGS:
1165 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1166 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1168 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1169 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1170 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1172 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1175 case LDAP_FILTER_EXT:
1176 mra_free( op, vrf->vrf_mra, 1 );
1179 case SLAPD_FILTER_COMPUTED:
1183 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1184 vrf->vrf_choice, 0, 0 );
1188 op->o_tmpfree( vrf, op->o_tmpmemctx );
1193 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1195 ValuesReturnFilter *p;
1199 if ( vrf == NULL ) {
1200 ber_str2bv_x( "No filter!", STRLENOF("No filter!"),
1201 1, fstr, op->o_tmpmemctx );
1205 fstr->bv_len = STRLENOF("()");
1206 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1208 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1210 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1213 simple_vrFilter2bv( op, p, &tmp );
1215 fstr->bv_len += tmp.bv_len;
1216 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1219 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1220 /*"("*/ "%s)", tmp.bv_val );
1222 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1227 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1233 if ( vrf == NULL ) {
1234 ber_str2bv_x( "No filter!", STRLENOF("No filter!"), 1, fstr,
1238 undef = vrf->vrf_choice & SLAPD_FILTER_UNDEFINED;
1240 switch ( vrf->vrf_choice & SLAPD_FILTER_MASK ) {
1241 case LDAP_FILTER_EQUALITY:
1242 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1244 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1245 tmp.bv_len + STRLENOF("(=)");
1246 if ( undef ) fstr->bv_len++;
1247 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1249 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1250 vrf->vrf_av_desc->ad_cname.bv_val,
1253 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1256 case LDAP_FILTER_GE:
1257 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1259 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1260 tmp.bv_len + STRLENOF("(>=)");
1261 if ( undef ) fstr->bv_len++;
1262 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1264 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1265 vrf->vrf_av_desc->ad_cname.bv_val,
1268 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1271 case LDAP_FILTER_LE:
1272 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1274 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1275 tmp.bv_len + STRLENOF("(<=)");
1276 if ( undef ) fstr->bv_len++;
1277 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1279 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1280 vrf->vrf_av_desc->ad_cname.bv_val,
1283 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1286 case LDAP_FILTER_APPROX:
1287 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1289 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1290 tmp.bv_len + STRLENOF("(~=)");
1291 if ( undef ) fstr->bv_len++;
1292 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1294 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1295 vrf->vrf_av_desc->ad_cname.bv_val,
1297 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1300 case LDAP_FILTER_SUBSTRINGS:
1301 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1303 if ( undef ) fstr->bv_len++;
1304 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1306 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1307 vrf->vrf_sub_desc->ad_cname.bv_val );
1309 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1312 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1314 fstr->bv_len += tmp.bv_len;
1315 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1318 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1319 /* "(attr=" */ "%s*)",
1322 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1325 if ( vrf->vrf_sub_any != NULL ) {
1327 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1329 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1332 fstr->bv_len += tmp.bv_len + 1;
1333 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1334 fstr->bv_len + 1, op->o_tmpmemctx );
1336 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1337 /* "(attr=[init]*[any*]" */ "%s*)",
1339 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1343 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1346 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1348 fstr->bv_len += tmp.bv_len;
1349 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1352 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1353 /* "(attr=[init*][any*]" */ "%s)",
1356 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1361 case LDAP_FILTER_PRESENT:
1362 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1364 if ( undef ) fstr->bv_len++;
1365 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1367 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1368 vrf->vrf_desc->ad_cname.bv_val );
1371 case LDAP_FILTER_EXT: {
1373 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1375 if ( vrf->vrf_mr_desc ) {
1376 ad = vrf->vrf_mr_desc->ad_cname;
1382 fstr->bv_len = ad.bv_len +
1383 ( vrf->vrf_mr_dnattrs ? STRLENOF(":dn") : 0 ) +
1384 ( vrf->vrf_mr_rule_text.bv_len
1385 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1386 tmp.bv_len + STRLENOF("(:=)");
1387 if ( undef ) fstr->bv_len++;
1388 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1390 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1392 vrf->vrf_mr_dnattrs ? ":dn" : "",
1393 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1394 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1397 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1400 case SLAPD_FILTER_COMPUTED:
1402 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1403 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1404 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1405 ? "(?=undefined)" : "(?=error)",
1406 vrf->vrf_result == LDAP_COMPARE_FALSE ? STRLENOF("(?=false)") :
1407 vrf->vrf_result == LDAP_COMPARE_TRUE ? STRLENOF("(?=true)") :
1408 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1409 ? STRLENOF("(?=undefined)") : STRLENOF("(?=error)"),
1410 1, fstr, op->o_tmpmemctx );
1414 ber_str2bv_x( "(?=unknown)", STRLENOF("(?=unknown)"),
1415 1, fstr, op->o_tmpmemctx );