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-2007 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 for ( tag = ber_first_element( ber, &len, &last );
390 tag = ber_next_element( ber, &len, last ) )
394 rc = ber_scanf( ber, "m", &value );
395 if ( rc == LBER_ERROR ) {
396 rc = SLAPD_DISCONNECT;
400 if ( value.bv_val == NULL || value.bv_len == 0 ) {
401 rc = LDAP_INVALID_SYNTAX;
406 case LDAP_SUBSTRING_INITIAL:
407 if ( ssa.sa_initial.bv_val != NULL
408 || ssa.sa_any != NULL
409 || ssa.sa_final.bv_val != NULL )
411 rc = LDAP_PROTOCOL_ERROR;
414 usage = SLAP_MR_SUBSTR_INITIAL;
417 case LDAP_SUBSTRING_ANY:
418 if ( ssa.sa_final.bv_val != NULL ) {
419 rc = LDAP_PROTOCOL_ERROR;
422 usage = SLAP_MR_SUBSTR_ANY;
425 case LDAP_SUBSTRING_FINAL:
426 if ( ssa.sa_final.bv_val != NULL ) {
427 rc = LDAP_PROTOCOL_ERROR;
431 usage = SLAP_MR_SUBSTR_FINAL;
435 Debug( LDAP_DEBUG_FILTER,
436 " unknown substring choice=%ld\n",
439 rc = LDAP_PROTOCOL_ERROR;
443 /* validate/normalize using equality matching rule validator! */
444 rc = asserted_value_validate_normalize(
445 ssa.sa_desc, ssa.sa_desc->ad_type->sat_equality,
446 usage, &value, &nvalue, text, op->o_tmpmemctx );
447 if( rc != LDAP_SUCCESS ) goto return_error;
450 case LDAP_SUBSTRING_INITIAL:
451 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
452 ssa.sa_initial = nvalue;
455 case LDAP_SUBSTRING_ANY:
456 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
457 ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx );
460 case LDAP_SUBSTRING_FINAL:
461 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
462 ssa.sa_final = nvalue;
467 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
468 rc = LDAP_PROTOCOL_ERROR;
471 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
473 slap_sl_free( ssa.sa_initial.bv_val, op->o_tmpmemctx );
474 ber_bvarray_free_x( ssa.sa_any, op->o_tmpmemctx );
475 if ( ssa.sa_desc->ad_flags & SLAP_DESC_TEMPORARY )
476 op->o_tmpfree( ssa.sa_desc, op->o_tmpmemctx );
477 slap_sl_free( ssa.sa_final.bv_val, op->o_tmpmemctx );
484 if( rc == LDAP_SUCCESS ) {
485 f->f_sub = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
489 Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
490 return rc /* LDAP_SUCCESS */ ;
494 filter_free_x( Operation *op, Filter *f )
502 f->f_choice &= SLAPD_FILTER_MASK;
504 switch ( f->f_choice ) {
505 case LDAP_FILTER_PRESENT:
508 case LDAP_FILTER_EQUALITY:
511 case LDAP_FILTER_APPROX:
512 ava_free( op, f->f_ava, 1 );
515 case LDAP_FILTER_SUBSTRINGS:
516 if ( f->f_sub_initial.bv_val != NULL ) {
517 op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx );
519 ber_bvarray_free_x( f->f_sub_any, op->o_tmpmemctx );
520 if ( f->f_sub_final.bv_val != NULL ) {
521 op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx );
523 if ( f->f_sub->sa_desc->ad_flags & SLAP_DESC_TEMPORARY )
524 op->o_tmpfree( f->f_sub->sa_desc, op->o_tmpmemctx );
525 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
528 case LDAP_FILTER_AND:
530 case LDAP_FILTER_NOT:
531 for ( p = f->f_list; p != NULL; p = next ) {
533 filter_free_x( op, p );
537 case LDAP_FILTER_EXT:
538 mra_free( op, f->f_mra, 1 );
541 case SLAPD_FILTER_COMPUTED:
545 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
550 op->o_tmpfree( f, op->o_tmpmemctx );
554 filter_free( Filter *f )
560 op.o_tmpmemctx = slap_sl_context( f );
561 op.o_tmpmfuncs = &slap_sl_mfuncs;
562 filter_free_x( &op, f );
566 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
572 ber_bvfalse = BER_BVC( "(?=false)" ),
573 ber_bvtrue = BER_BVC( "(?=true)" ),
574 ber_bvundefined = BER_BVC( "(?=undefined)" ),
575 ber_bverror = BER_BVC( "(?=error)" ),
576 ber_bvunknown = BER_BVC( "(?=unknown)" ),
577 ber_bvnone = BER_BVC( "(?=none)" );
584 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
588 undef = f->f_choice & SLAPD_FILTER_UNDEFINED;
589 choice = f->f_choice & SLAPD_FILTER_MASK;
592 case LDAP_FILTER_EQUALITY:
593 fstr->bv_len = STRLENOF("(=)");
595 if ( f->f_av_desc->ad_type->sat_syntax == slap_schema.si_ad_entryUUID->ad_type->sat_syntax ) {
596 tmp.bv_val = op->o_tmpalloc( LDAP_LUTIL_UUIDSTR_BUFSIZE, op->o_tmpmemctx );
597 tmp.bv_len = lutil_uuidstr_from_normalized( f->f_av_value.bv_val,
598 f->f_av_value.bv_len, tmp.bv_val, LDAP_LUTIL_UUIDSTR_BUFSIZE );
599 assert( tmp.bv_len > 0 );
604 fstr->bv_len = STRLENOF("(>=)");
608 fstr->bv_len = STRLENOF("(<=)");
611 case LDAP_FILTER_APPROX:
612 fstr->bv_len = STRLENOF("(~=)");
616 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
617 /* NOTE: tmp can legitimately be NULL (meaning empty)
618 * since in a Filter values in AVAs are supposed
619 * to have been normalized, meaning that an empty value
620 * is legal for that attribute's syntax */
623 fstr->bv_len += f->f_av_desc->ad_cname.bv_len + tmp.bv_len;
626 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
628 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s)",
630 f->f_av_desc->ad_cname.bv_val, sign,
631 tmp.bv_len ? tmp.bv_val : "" );
633 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
636 case LDAP_FILTER_SUBSTRINGS:
637 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
641 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
643 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s=*)",
645 f->f_sub_desc->ad_cname.bv_val );
647 if ( f->f_sub_initial.bv_val != NULL ) {
652 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
655 fstr->bv_len += tmplen;
656 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
657 fstr->bv_len + 1, op->o_tmpmemctx );
659 snprintf( &fstr->bv_val[len - 2],
660 tmplen + STRLENOF( /*(*/ "*)" ) + 1,
661 /* "(attr=" */ "%s*)",
662 tmp.bv_len ? tmp.bv_val : "");
664 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
667 if ( f->f_sub_any != NULL ) {
668 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
672 filter_escape_value_x( &f->f_sub_any[i],
673 &tmp, op->o_tmpmemctx );
676 fstr->bv_len += tmplen + STRLENOF( /*(*/ ")" );
677 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
678 fstr->bv_len + 1, op->o_tmpmemctx );
680 snprintf( &fstr->bv_val[len - 1],
681 tmplen + STRLENOF( /*(*/ "*)" ) + 1,
682 /* "(attr=[init]*[any*]" */ "%s*)",
683 tmp.bv_len ? tmp.bv_val : "");
684 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
688 if ( f->f_sub_final.bv_val != NULL ) {
693 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
696 fstr->bv_len += tmplen;
697 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
698 fstr->bv_len + 1, op->o_tmpmemctx );
700 snprintf( &fstr->bv_val[len - 1],
701 tmplen + STRLENOF( /*(*/ ")" ) + 1,
702 /* "(attr=[init*][any*]" */ "%s)",
703 tmp.bv_len ? tmp.bv_val : "");
705 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
710 case LDAP_FILTER_PRESENT:
711 fstr->bv_len = f->f_desc->ad_cname.bv_len +
716 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
718 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s=*)",
720 f->f_desc->ad_cname.bv_val );
723 case LDAP_FILTER_AND:
725 case LDAP_FILTER_NOT:
726 fstr->bv_len = STRLENOF("(%)");
727 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
729 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
730 f->f_choice == LDAP_FILTER_AND ? '&' :
731 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
733 for ( p = f->f_list; p != NULL; p = p->f_next ) {
736 filter2bv_x( op, p, &tmp );
738 fstr->bv_len += tmp.bv_len;
739 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
742 snprintf( &fstr->bv_val[len-1],
743 tmp.bv_len + STRLENOF( /*(*/ ")" ) + 1,
744 /*"("*/ "%s)", tmp.bv_val );
746 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
751 case LDAP_FILTER_EXT: {
754 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
755 /* NOTE: tmp can legitimately be NULL (meaning empty)
756 * since in a Filter values in MRAs are supposed
757 * to have been normalized, meaning that an empty value
758 * is legal for that attribute's syntax */
760 if ( f->f_mr_desc ) {
761 ad = f->f_mr_desc->ad_cname;
767 fstr->bv_len = ad.bv_len +
768 ( f->f_mr_dnattrs ? STRLENOF(":dn") : 0 ) +
769 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
770 tmp.bv_len + STRLENOF("(:=)");
771 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
773 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s%s:=%s)",
776 f->f_mr_dnattrs ? ":dn" : "",
777 f->f_mr_rule_text.bv_len ? ":" : "",
778 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
779 tmp.bv_len ? tmp.bv_val : "" );
780 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
783 case SLAPD_FILTER_COMPUTED:
784 switch ( f->f_result ) {
785 case LDAP_COMPARE_FALSE:
789 case LDAP_COMPARE_TRUE:
793 case SLAPD_COMPARE_UNDEFINED:
794 tmp = ber_bvundefined;
802 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
806 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
812 filter2bv( Filter *f, struct berval *fstr )
818 op.o_tmpmemctx = NULL;
819 op.o_tmpmfuncs = &ch_mfuncs;
821 filter2bv_x( &op, f, fstr );
825 filter_dup( Filter *f, void *memctx )
827 BerMemoryFunctions *mf = &slap_sl_mfuncs;
833 n = mf->bmf_malloc( sizeof(Filter), memctx );
834 n->f_choice = f->f_choice;
837 switch( f->f_choice & SLAPD_FILTER_MASK ) {
838 case SLAPD_FILTER_COMPUTED:
839 n->f_result = f->f_result;
841 case LDAP_FILTER_PRESENT:
842 if ( f->f_desc->ad_flags & SLAP_DESC_TEMPORARY )
843 n->f_desc = slap_bv2tmp_ad( &f->f_desc->ad_cname, memctx );
845 n->f_desc = f->f_desc;
847 case LDAP_FILTER_EQUALITY:
850 case LDAP_FILTER_APPROX:
851 /* Should this be ava_dup() ? */
852 n->f_ava = mf->bmf_calloc( 1, sizeof(AttributeAssertion), memctx );
853 *n->f_ava = *f->f_ava;
854 if ( f->f_av_desc->ad_flags & SLAP_DESC_TEMPORARY )
855 n->f_av_desc = slap_bv2tmp_ad( &f->f_av_desc->ad_cname, memctx );
856 ber_dupbv_x( &n->f_av_value, &f->f_av_value, memctx );
858 case LDAP_FILTER_SUBSTRINGS:
859 n->f_sub = mf->bmf_calloc( 1, sizeof(SubstringsAssertion), memctx );
860 if ( f->f_sub_desc->ad_flags & SLAP_DESC_TEMPORARY )
861 n->f_sub_desc = slap_bv2tmp_ad( &f->f_sub_desc->ad_cname, memctx );
863 n->f_sub_desc = f->f_sub_desc;
864 if ( !BER_BVISNULL( &f->f_sub_initial ))
865 ber_dupbv_x( &n->f_sub_initial, &f->f_sub_initial, memctx );
866 if ( f->f_sub_any ) {
868 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ );
869 n->f_sub_any = mf->bmf_malloc(( i+1 )*sizeof( struct berval ),
871 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
872 ber_dupbv_x( &n->f_sub_any[i], &f->f_sub_any[i], memctx );
874 BER_BVZERO( &n->f_sub_any[i] );
876 if ( !BER_BVISNULL( &f->f_sub_final ))
877 ber_dupbv_x( &n->f_sub_final, &f->f_sub_final, memctx );
879 case LDAP_FILTER_EXT: {
880 /* Should this be mra_dup() ? */
882 length = sizeof(MatchingRuleAssertion);
883 if ( !BER_BVISNULL( &f->f_mr_rule_text ))
884 length += f->f_mr_rule_text.bv_len + 1;
885 n->f_mra = mf->bmf_calloc( 1, length, memctx );
886 *n->f_mra = *f->f_mra;
887 if ( f->f_mr_desc && ( f->f_sub_desc->ad_flags & SLAP_DESC_TEMPORARY ))
888 n->f_mr_desc = slap_bv2tmp_ad( &f->f_mr_desc->ad_cname, memctx );
889 ber_dupbv_x( &n->f_mr_value, &f->f_mr_value, memctx );
890 if ( !BER_BVISNULL( &f->f_mr_rule_text )) {
891 n->f_mr_rule_text.bv_val = (char *)(n->f_mra+1);
892 AC_MEMCPY(n->f_mr_rule_text.bv_val,
893 f->f_mr_rule_text.bv_val, f->f_mr_rule_text.bv_len );
896 case LDAP_FILTER_AND:
898 case LDAP_FILTER_NOT: {
900 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
901 *p = filter_dup( f, memctx );
913 ValuesReturnFilter **filt,
919 ValuesReturnFilter vrf;
921 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
923 tag = ber_peek_tag( ber, &len );
925 if( tag == LBER_ERROR ) {
926 *text = "error decoding filter";
927 return SLAPD_DISCONNECT;
933 vrf.vrf_choice = tag;
935 switch ( vrf.vrf_choice ) {
936 case LDAP_FILTER_EQUALITY:
937 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
938 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_EQUALITY, text );
939 if ( err != LDAP_SUCCESS ) {
943 assert( vrf.vrf_ava != NULL );
946 case LDAP_FILTER_SUBSTRINGS:
947 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
948 err = get_ssa( op, ber, (Filter *)&vrf, text );
952 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
953 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_ORDERING, text );
954 if ( err != LDAP_SUCCESS ) {
960 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
961 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_ORDERING, text );
962 if ( err != LDAP_SUCCESS ) {
967 case LDAP_FILTER_PRESENT: {
970 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
971 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
972 err = SLAPD_DISCONNECT;
973 *text = "error decoding filter";
978 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
980 if( err != LDAP_SUCCESS ) {
981 vrf.vrf_choice |= SLAPD_FILTER_UNDEFINED;
982 err = slap_bv2undef_ad( &type, &vrf.vrf_desc, text,
985 if( err != LDAP_SUCCESS ) {
986 /* unrecognized attribute description or other error */
987 Debug( LDAP_DEBUG_ANY,
988 "get_simple_vrFilter: conn %lu unknown "
989 "attribute type=%s (%d)\n",
990 op->o_connid, type.bv_val, err );
992 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
993 vrf.vrf_result = LDAP_COMPARE_FALSE;
1000 case LDAP_FILTER_APPROX:
1001 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
1002 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_EQUALITY_APPROX, text );
1003 if ( err != LDAP_SUCCESS ) {
1008 case LDAP_FILTER_EXT:
1009 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1011 err = get_mra( op, ber, (Filter *)&vrf, text );
1012 if ( err != LDAP_SUCCESS ) {
1016 assert( vrf.vrf_mra != NULL );
1020 (void) ber_scanf( ber, "x" ); /* skip the element */
1021 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1022 vrf.vrf_choice, 0, 0 );
1023 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1024 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1028 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1030 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1031 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1035 if ( err == LDAP_SUCCESS ) {
1036 *filt = op->o_tmpalloc( sizeof vrf, op->o_tmpmemctx );
1040 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1046 get_vrFilter( Operation *op, BerElement *ber,
1047 ValuesReturnFilter **vrf,
1051 * A ValuesReturnFilter looks like this:
1053 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1054 * SimpleFilterItem ::= CHOICE {
1055 * equalityMatch [3] AttributeValueAssertion,
1056 * substrings [4] SubstringFilter,
1057 * greaterOrEqual [5] AttributeValueAssertion,
1058 * lessOrEqual [6] AttributeValueAssertion,
1059 * present [7] AttributeType,
1060 * approxMatch [8] AttributeValueAssertion,
1061 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1064 * SubstringFilter ::= SEQUENCE {
1065 * type AttributeType,
1066 * SEQUENCE OF CHOICE {
1067 * initial [0] IA5String,
1068 * any [1] IA5String,
1069 * final [2] IA5String
1073 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1074 * matchingRule [1] MatchingRuleId OPTIONAL,
1075 * type [2] AttributeDescription OPTIONAL,
1076 * matchValue [3] AssertionValue }
1079 ValuesReturnFilter **n;
1084 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1086 tag = ber_peek_tag( ber, &len );
1088 if( tag == LBER_ERROR ) {
1089 *text = "error decoding vrFilter";
1090 return SLAPD_DISCONNECT;
1093 if( tag != LBER_SEQUENCE ) {
1094 *text = "error decoding vrFilter, expect SEQUENCE tag";
1095 return SLAPD_DISCONNECT;
1099 for ( tag = ber_first_element( ber, &len, &last );
1100 tag != LBER_DEFAULT;
1101 tag = ber_next_element( ber, &len, last ) )
1103 int err = get_simple_vrFilter( op, ber, n, text );
1105 if ( err != LDAP_SUCCESS ) return( err );
1107 n = &(*n)->vrf_next;
1111 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1112 return( LDAP_SUCCESS );
1116 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1118 ValuesReturnFilter *p, *next;
1120 if ( vrf == NULL ) {
1124 for ( p = vrf; p != NULL; p = next ) {
1127 switch ( vrf->vrf_choice & SLAPD_FILTER_MASK ) {
1128 case LDAP_FILTER_PRESENT:
1131 case LDAP_FILTER_EQUALITY:
1132 case LDAP_FILTER_GE:
1133 case LDAP_FILTER_LE:
1134 case LDAP_FILTER_APPROX:
1135 ava_free( op, vrf->vrf_ava, 1 );
1138 case LDAP_FILTER_SUBSTRINGS:
1139 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1140 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1142 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1143 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1144 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1146 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1149 case LDAP_FILTER_EXT:
1150 mra_free( op, vrf->vrf_mra, 1 );
1153 case SLAPD_FILTER_COMPUTED:
1157 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1158 vrf->vrf_choice, 0, 0 );
1162 op->o_tmpfree( vrf, op->o_tmpmemctx );
1167 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1169 ValuesReturnFilter *p;
1173 if ( vrf == NULL ) {
1174 ber_str2bv_x( "No filter!", STRLENOF("No filter!"),
1175 1, fstr, op->o_tmpmemctx );
1179 fstr->bv_len = STRLENOF("()");
1180 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1182 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1184 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1187 simple_vrFilter2bv( op, p, &tmp );
1189 fstr->bv_len += tmp.bv_len;
1190 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1193 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1194 /*"("*/ "%s)", tmp.bv_val );
1196 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1201 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1207 if ( vrf == NULL ) {
1208 ber_str2bv_x( "No filter!", STRLENOF("No filter!"), 1, fstr,
1212 undef = vrf->vrf_choice & SLAPD_FILTER_UNDEFINED;
1214 switch ( vrf->vrf_choice & SLAPD_FILTER_MASK ) {
1215 case LDAP_FILTER_EQUALITY:
1216 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1218 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1219 tmp.bv_len + STRLENOF("(=)");
1220 if ( undef ) fstr->bv_len++;
1221 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1223 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1224 vrf->vrf_av_desc->ad_cname.bv_val,
1227 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1230 case LDAP_FILTER_GE:
1231 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1233 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1234 tmp.bv_len + STRLENOF("(>=)");
1235 if ( undef ) fstr->bv_len++;
1236 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1238 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1239 vrf->vrf_av_desc->ad_cname.bv_val,
1242 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1245 case LDAP_FILTER_LE:
1246 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1248 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1249 tmp.bv_len + STRLENOF("(<=)");
1250 if ( undef ) fstr->bv_len++;
1251 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1253 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1254 vrf->vrf_av_desc->ad_cname.bv_val,
1257 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1260 case LDAP_FILTER_APPROX:
1261 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1263 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1264 tmp.bv_len + STRLENOF("(~=)");
1265 if ( undef ) fstr->bv_len++;
1266 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1268 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1269 vrf->vrf_av_desc->ad_cname.bv_val,
1271 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1274 case LDAP_FILTER_SUBSTRINGS:
1275 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1277 if ( undef ) fstr->bv_len++;
1278 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1280 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1281 vrf->vrf_sub_desc->ad_cname.bv_val );
1283 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1286 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1288 fstr->bv_len += tmp.bv_len;
1289 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1292 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1293 /* "(attr=" */ "%s*)",
1296 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1299 if ( vrf->vrf_sub_any != NULL ) {
1301 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1303 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1306 fstr->bv_len += tmp.bv_len + 1;
1307 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1308 fstr->bv_len + 1, op->o_tmpmemctx );
1310 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1311 /* "(attr=[init]*[any*]" */ "%s*)",
1313 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1317 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1320 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1322 fstr->bv_len += tmp.bv_len;
1323 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1326 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1327 /* "(attr=[init*][any*]" */ "%s)",
1330 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1335 case LDAP_FILTER_PRESENT:
1336 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1338 if ( undef ) fstr->bv_len++;
1339 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1341 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1342 vrf->vrf_desc->ad_cname.bv_val );
1345 case LDAP_FILTER_EXT: {
1347 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1349 if ( vrf->vrf_mr_desc ) {
1350 ad = vrf->vrf_mr_desc->ad_cname;
1356 fstr->bv_len = ad.bv_len +
1357 ( vrf->vrf_mr_dnattrs ? STRLENOF(":dn") : 0 ) +
1358 ( vrf->vrf_mr_rule_text.bv_len
1359 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1360 tmp.bv_len + STRLENOF("(:=)");
1361 if ( undef ) fstr->bv_len++;
1362 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1364 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1366 vrf->vrf_mr_dnattrs ? ":dn" : "",
1367 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1368 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1371 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1374 case SLAPD_FILTER_COMPUTED:
1376 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1377 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1378 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1379 ? "(?=undefined)" : "(?=error)",
1380 vrf->vrf_result == LDAP_COMPARE_FALSE ? STRLENOF("(?=false)") :
1381 vrf->vrf_result == LDAP_COMPARE_TRUE ? STRLENOF("(?=true)") :
1382 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1383 ? STRLENOF("(?=undefined)") : STRLENOF("(?=error)"),
1384 1, fstr, op->o_tmpmemctx );
1388 ber_str2bv_x( "(?=unknown)", STRLENOF("(?=unknown)"),
1389 1, fstr, op->o_tmpmemctx );