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-2006 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>
36 static int get_filter_list(
48 static void simple_vrFilter2bv(
50 ValuesReturnFilter *f,
51 struct berval *fstr );
53 static int get_simple_vrFilter(
56 ValuesReturnFilter **f,
71 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
73 * A filter looks like this coming in:
75 * and [0] SET OF Filter,
76 * or [1] SET OF Filter,
78 * equalityMatch [3] AttributeValueAssertion,
79 * substrings [4] SubstringFilter,
80 * greaterOrEqual [5] AttributeValueAssertion,
81 * lessOrEqual [6] AttributeValueAssertion,
82 * present [7] AttributeType,
83 * approxMatch [8] AttributeValueAssertion,
84 * extensibleMatch [9] MatchingRuleAssertion
87 * SubstringFilter ::= SEQUENCE {
89 * SEQUENCE OF CHOICE {
90 * initial [0] IA5String,
96 * MatchingRuleAssertion ::= SEQUENCE {
97 * matchingRule [1] MatchingRuleId OPTIONAL,
98 * type [2] AttributeDescription OPTIONAL,
99 * matchValue [3] AssertionValue,
100 * dnAttributes [4] BOOLEAN DEFAULT FALSE
105 tag = ber_peek_tag( ber, &len );
107 if( tag == LBER_ERROR ) {
108 *text = "error decoding filter";
109 return SLAPD_DISCONNECT;
117 switch ( f.f_choice ) {
118 case LDAP_FILTER_EQUALITY:
119 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
120 err = get_ava( op, ber, &f, SLAP_MR_EQUALITY, text );
121 if ( err != LDAP_SUCCESS ) {
125 assert( f.f_ava != NULL );
128 case LDAP_FILTER_SUBSTRINGS:
129 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
130 err = get_ssa( op, ber, &f, text );
131 if( err != LDAP_SUCCESS ) {
134 assert( f.f_sub != NULL );
138 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
139 err = get_ava( op, ber, &f, SLAP_MR_ORDERING, text );
140 if ( err != LDAP_SUCCESS ) {
143 assert( f.f_ava != NULL );
147 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
148 err = get_ava( op, ber, &f, SLAP_MR_ORDERING, text );
149 if ( err != LDAP_SUCCESS ) {
152 assert( f.f_ava != NULL );
155 case LDAP_FILTER_PRESENT: {
158 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
159 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
160 err = SLAPD_DISCONNECT;
161 *text = "error decoding filter";
166 err = slap_bv2ad( &type, &f.f_desc, text );
168 if( err != LDAP_SUCCESS ) {
169 f.f_choice |= SLAPD_FILTER_UNDEFINED;
170 err = slap_bv2undef_ad( &type, &f.f_desc, text, SLAP_AD_PROXIED);
171 if ( err != LDAP_SUCCESS ) {
172 /* unrecognized attribute description or other error */
173 Debug( LDAP_DEBUG_ANY,
174 "get_filter: conn %lu unknown attribute "
176 op->o_connid, type.bv_val, err );
183 assert( f.f_desc != NULL );
186 case LDAP_FILTER_APPROX:
187 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
188 err = get_ava( op, ber, &f, SLAP_MR_EQUALITY_APPROX, text );
189 if ( err != LDAP_SUCCESS ) {
192 assert( f.f_ava != NULL );
195 case LDAP_FILTER_AND:
196 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
197 err = get_filter_list( op, ber, &f.f_and, text );
198 if ( err != LDAP_SUCCESS ) {
201 if ( f.f_and == NULL ) {
202 f.f_choice = SLAPD_FILTER_COMPUTED;
203 f.f_result = LDAP_COMPARE_TRUE;
205 /* no assert - list could be empty */
209 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
210 err = get_filter_list( op, ber, &f.f_or, text );
211 if ( err != LDAP_SUCCESS ) {
214 if ( f.f_or == NULL ) {
215 f.f_choice = SLAPD_FILTER_COMPUTED;
216 f.f_result = LDAP_COMPARE_FALSE;
218 /* no assert - list could be empty */
221 case LDAP_FILTER_NOT:
222 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
223 (void) ber_skip_tag( ber, &len );
224 err = get_filter( op, ber, &f.f_not, text );
225 if ( err != LDAP_SUCCESS ) {
229 assert( f.f_not != NULL );
230 if ( f.f_not->f_choice == SLAPD_FILTER_COMPUTED ) {
231 int fresult = f.f_not->f_result;
232 f.f_choice = SLAPD_FILTER_COMPUTED;
233 op->o_tmpfree( f.f_not, op->o_tmpmemctx );
237 case LDAP_COMPARE_TRUE:
238 f.f_result = LDAP_COMPARE_FALSE;
240 case LDAP_COMPARE_FALSE:
241 f.f_result = LDAP_COMPARE_TRUE;
244 /* (!Undefined) is Undefined */
249 case LDAP_FILTER_EXT:
250 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
252 err = get_mra( op, ber, &f, text );
253 if ( err != LDAP_SUCCESS ) {
257 assert( f.f_mra != NULL );
261 (void) ber_scanf( ber, "x" ); /* skip the element */
262 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
264 f.f_choice = SLAPD_FILTER_COMPUTED;
265 f.f_result = SLAPD_COMPARE_UNDEFINED;
269 if( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
272 f.f_choice = SLAPD_FILTER_COMPUTED;
273 f.f_result = SLAPD_COMPARE_UNDEFINED;
277 if ( err == LDAP_SUCCESS ) {
278 *filt = op->o_tmpalloc( sizeof(f), op->o_tmpmemctx );
282 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
288 get_filter_list( Operation *op, BerElement *ber,
298 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
300 for ( tag = ber_first_element( ber, &len, &last );
302 tag = ber_next_element( ber, &len, last ) )
304 err = get_filter( op, ber, new, text );
305 if ( err != LDAP_SUCCESS )
307 new = &(*new)->f_next;
311 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
312 return( LDAP_SUCCESS );
325 struct berval desc, value, nvalue;
327 SubstringsAssertion ssa;
329 *text = "error decoding filter";
331 Debug( LDAP_DEBUG_FILTER, "begin get_ssa\n", 0, 0, 0 );
332 if ( ber_scanf( ber, "{m" /*}*/, &desc ) == LBER_ERROR ) {
333 return SLAPD_DISCONNECT;
339 ssa.sa_initial.bv_val = NULL;
341 ssa.sa_final.bv_val = NULL;
343 rc = slap_bv2ad( &desc, &ssa.sa_desc, text );
345 if( rc != LDAP_SUCCESS ) {
346 f->f_choice |= SLAPD_FILTER_UNDEFINED;
347 rc = slap_bv2undef_ad( &desc, &ssa.sa_desc, text, SLAP_AD_PROXIED);
348 if( rc != LDAP_SUCCESS ) {
349 Debug( LDAP_DEBUG_ANY,
350 "get_ssa: conn %lu unknown attribute type=%s (%ld)\n",
351 op->o_connid, desc.bv_val, (long) rc );
353 /* skip over the rest of this filter */
354 for ( tag = ber_first_element( ber, &len, &last );
356 tag = ber_next_element( ber, &len, last ) ) {
357 ber_scanf( ber, "x" );
363 rc = LDAP_PROTOCOL_ERROR;
365 for ( tag = ber_first_element( ber, &len, &last );
367 tag = ber_next_element( ber, &len, last ) )
371 rc = ber_scanf( ber, "m", &value );
372 if ( rc == LBER_ERROR ) {
373 rc = SLAPD_DISCONNECT;
377 if ( value.bv_val == NULL || value.bv_len == 0 ) {
378 rc = LDAP_INVALID_SYNTAX;
383 case LDAP_SUBSTRING_INITIAL:
384 if ( ssa.sa_initial.bv_val != NULL
385 || ssa.sa_any != NULL
386 || ssa.sa_final.bv_val != NULL )
388 rc = LDAP_PROTOCOL_ERROR;
391 usage = SLAP_MR_SUBSTR_INITIAL;
394 case LDAP_SUBSTRING_ANY:
395 if ( ssa.sa_final.bv_val != NULL ) {
396 rc = LDAP_PROTOCOL_ERROR;
399 usage = SLAP_MR_SUBSTR_ANY;
402 case LDAP_SUBSTRING_FINAL:
403 if ( ssa.sa_final.bv_val != NULL ) {
404 rc = LDAP_PROTOCOL_ERROR;
408 usage = SLAP_MR_SUBSTR_FINAL;
412 Debug( LDAP_DEBUG_FILTER,
413 " unknown substring choice=%ld\n",
416 rc = LDAP_PROTOCOL_ERROR;
420 /* validate/normalize using equality matching rule validator! */
421 rc = asserted_value_validate_normalize(
422 ssa.sa_desc, ssa.sa_desc->ad_type->sat_equality,
423 usage, &value, &nvalue, text, op->o_tmpmemctx );
424 if( rc != LDAP_SUCCESS ) goto return_error;
427 case LDAP_SUBSTRING_INITIAL:
428 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
429 ssa.sa_initial = nvalue;
432 case LDAP_SUBSTRING_ANY:
433 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
434 ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx );
437 case LDAP_SUBSTRING_FINAL:
438 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
439 ssa.sa_final = nvalue;
444 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
445 rc = LDAP_PROTOCOL_ERROR;
448 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
450 slap_sl_free( ssa.sa_initial.bv_val, op->o_tmpmemctx );
451 ber_bvarray_free_x( ssa.sa_any, op->o_tmpmemctx );
452 slap_sl_free( ssa.sa_final.bv_val, op->o_tmpmemctx );
459 if( rc == LDAP_SUCCESS ) {
460 f->f_sub = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
464 Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
465 return rc /* LDAP_SUCCESS */ ;
469 filter_free_x( Operation *op, Filter *f )
477 f->f_choice &= SLAPD_FILTER_MASK;
479 switch ( f->f_choice ) {
480 case LDAP_FILTER_PRESENT:
483 case LDAP_FILTER_EQUALITY:
486 case LDAP_FILTER_APPROX:
487 ava_free( op, f->f_ava, 1 );
490 case LDAP_FILTER_SUBSTRINGS:
491 if ( f->f_sub_initial.bv_val != NULL ) {
492 op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx );
494 ber_bvarray_free_x( f->f_sub_any, op->o_tmpmemctx );
495 if ( f->f_sub_final.bv_val != NULL ) {
496 op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx );
498 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
501 case LDAP_FILTER_AND:
503 case LDAP_FILTER_NOT:
504 for ( p = f->f_list; p != NULL; p = next ) {
506 filter_free_x( op, p );
510 case LDAP_FILTER_EXT:
511 mra_free( op, f->f_mra, 1 );
514 case SLAPD_FILTER_COMPUTED:
518 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
523 op->o_tmpfree( f, op->o_tmpmemctx );
527 filter_free( Filter *f )
533 op.o_tmpmemctx = slap_sl_context( f );
534 op.o_tmpmfuncs = &slap_sl_mfuncs;
535 filter_free_x( &op, f );
539 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
545 ber_bvfalse = BER_BVC( "(?=false)" ),
546 ber_bvtrue = BER_BVC( "(?=true)" ),
547 ber_bvundefined = BER_BVC( "(?=undefined)" ),
548 ber_bverror = BER_BVC( "(?=error)" ),
549 ber_bvunknown = BER_BVC( "(?=unknown)" ),
550 ber_bvnone = BER_BVC( "(?=none)" );
557 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
561 undef = f->f_choice & SLAPD_FILTER_UNDEFINED;
562 choice = f->f_choice & SLAPD_FILTER_MASK;
565 case LDAP_FILTER_EQUALITY:
566 fstr->bv_len = STRLENOF("(=)");
570 fstr->bv_len = STRLENOF("(>=)");
574 fstr->bv_len = STRLENOF("(<=)");
577 case LDAP_FILTER_APPROX:
578 fstr->bv_len = STRLENOF("(~=)");
582 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
583 /* NOTE: tmp can legitimately be NULL (meaning empty)
584 * since in a Filter values in AVAs are supposed
585 * to have been normalized, meaning that an empty value
586 * is legal for that attribute's syntax */
588 fstr->bv_len += f->f_av_desc->ad_cname.bv_len + tmp.bv_len;
591 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
593 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s)",
595 f->f_av_desc->ad_cname.bv_val, sign,
596 tmp.bv_len ? tmp.bv_val : "" );
598 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
601 case LDAP_FILTER_SUBSTRINGS:
602 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
606 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
608 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s=*)",
610 f->f_sub_desc->ad_cname.bv_val );
612 if ( f->f_sub_initial.bv_val != NULL ) {
617 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
618 tmplen = tmp.bv_len ? tmp.bv_len : STRLENOF( "(null)" );
620 fstr->bv_len += tmplen;
621 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
622 fstr->bv_len + 1, op->o_tmpmemctx );
624 snprintf( &fstr->bv_val[len-2],
625 tmplen + STRLENOF( /*(*/ "*)" ) + 1,
626 /* "(attr=" */ "%s*)",
627 tmp.bv_len ? tmp.bv_val : "(null)");
629 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
632 if ( f->f_sub_any != NULL ) {
633 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
637 filter_escape_value_x( &f->f_sub_any[i],
638 &tmp, op->o_tmpmemctx );
639 tmplen = tmp.bv_len ? tmp.bv_len : STRLENOF( "(null)" );
641 fstr->bv_len += tmplen + STRLENOF( /*(*/ ")" );
642 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
643 fstr->bv_len + 1, op->o_tmpmemctx );
645 snprintf( &fstr->bv_val[len-1],
646 tmplen + STRLENOF( /*(*/ "*)" ) + 1,
647 /* "(attr=[init]*[any*]" */ "%s*)",
648 tmp.bv_len ? tmp.bv_val : "(null)");
649 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
653 if ( f->f_sub_final.bv_val != NULL ) {
658 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
659 tmplen = tmp.bv_len ? tmp.bv_len : STRLENOF( "(null)" );
661 fstr->bv_len += tmplen;
662 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
663 fstr->bv_len + 1, op->o_tmpmemctx );
665 snprintf( &fstr->bv_val[len-1],
666 tmplen + STRLENOF( /*(*/ ")" ) + 1,
667 /* "(attr=[init*][any*]" */ "%s)",
668 tmp.bv_len ? tmp.bv_val : "(null)");
670 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
675 case LDAP_FILTER_PRESENT:
676 fstr->bv_len = f->f_desc->ad_cname.bv_len +
681 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
683 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s=*)",
685 f->f_desc->ad_cname.bv_val );
688 case LDAP_FILTER_AND:
690 case LDAP_FILTER_NOT:
691 fstr->bv_len = STRLENOF("(%)");
692 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
694 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
695 f->f_choice == LDAP_FILTER_AND ? '&' :
696 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
698 for ( p = f->f_list; p != NULL; p = p->f_next ) {
701 filter2bv_x( op, p, &tmp );
703 fstr->bv_len += tmp.bv_len;
704 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
707 snprintf( &fstr->bv_val[len-1],
708 tmp.bv_len + STRLENOF( /*(*/ ")" ) + 1,
709 /*"("*/ "%s)", tmp.bv_val );
711 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
716 case LDAP_FILTER_EXT: {
719 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
720 /* NOTE: tmp can legitimately be NULL (meaning empty)
721 * since in a Filter values in MRAs are supposed
722 * to have been normalized, meaning that an empty value
723 * is legal for that attribute's syntax */
725 if ( f->f_mr_desc ) {
726 ad = f->f_mr_desc->ad_cname;
732 fstr->bv_len = ad.bv_len +
733 ( f->f_mr_dnattrs ? STRLENOF(":dn") : 0 ) +
734 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
735 tmp.bv_len + STRLENOF("(:=)");
736 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
738 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s%s:=%s)",
741 f->f_mr_dnattrs ? ":dn" : "",
742 f->f_mr_rule_text.bv_len ? ":" : "",
743 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
744 tmp.bv_len ? tmp.bv_val : "" );
745 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
748 case SLAPD_FILTER_COMPUTED:
749 switch ( f->f_result ) {
750 case LDAP_COMPARE_FALSE:
754 case LDAP_COMPARE_TRUE:
758 case SLAPD_COMPARE_UNDEFINED:
759 tmp = ber_bvundefined;
767 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
771 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
777 filter2bv( Filter *f, struct berval *fstr )
783 op.o_tmpmemctx = NULL;
784 op.o_tmpmfuncs = &ch_mfuncs;
786 filter2bv_x( &op, f, fstr );
790 filter_dup( Filter *f, void *memctx )
792 BerMemoryFunctions *mf = &slap_sl_mfuncs;
798 n = mf->bmf_malloc( sizeof(Filter), memctx );
799 n->f_choice = f->f_choice;
802 switch( f->f_choice & SLAPD_FILTER_MASK ) {
803 case SLAPD_FILTER_COMPUTED:
804 n->f_result = f->f_result;
806 case LDAP_FILTER_PRESENT:
807 n->f_desc = f->f_desc;
809 case LDAP_FILTER_EQUALITY:
812 case LDAP_FILTER_APPROX:
813 /* Should this be ava_dup() ? */
814 n->f_ava = mf->bmf_calloc( 1, sizeof(AttributeAssertion), memctx );
815 *n->f_ava = *f->f_ava;
816 ber_dupbv_x( &n->f_av_value, &f->f_av_value, memctx );
818 case LDAP_FILTER_SUBSTRINGS:
819 n->f_sub = mf->bmf_calloc( 1, sizeof(SubstringsAssertion), memctx );
820 n->f_sub_desc = f->f_sub_desc;
821 if ( !BER_BVISNULL( &f->f_sub_initial ))
822 ber_dupbv_x( &n->f_sub_initial, &f->f_sub_initial, memctx );
823 if ( f->f_sub_any ) {
825 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ );
826 n->f_sub_any = mf->bmf_malloc(( i+1 )*sizeof( struct berval ),
828 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
829 ber_dupbv_x( &n->f_sub_any[i], &f->f_sub_any[i], memctx );
831 BER_BVZERO( &n->f_sub_any[i] );
833 if ( !BER_BVISNULL( &f->f_sub_final ))
834 ber_dupbv_x( &n->f_sub_final, &f->f_sub_final, memctx );
836 case LDAP_FILTER_EXT: {
837 /* Should this be mra_dup() ? */
839 length = sizeof(MatchingRuleAssertion);
840 if ( !BER_BVISNULL( &f->f_mr_rule_text ))
841 length += f->f_mr_rule_text.bv_len + 1;
842 n->f_mra = mf->bmf_calloc( 1, length, memctx );
843 *n->f_mra = *f->f_mra;
844 ber_dupbv_x( &n->f_mr_value, &f->f_mr_value, memctx );
845 if ( !BER_BVISNULL( &f->f_mr_rule_text )) {
846 n->f_mr_rule_text.bv_val = (char *)(n->f_mra+1);
847 AC_MEMCPY(n->f_mr_rule_text.bv_val,
848 f->f_mr_rule_text.bv_val, f->f_mr_rule_text.bv_len );
851 case LDAP_FILTER_AND:
853 case LDAP_FILTER_NOT: {
855 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
856 *p = filter_dup( f, memctx );
868 ValuesReturnFilter **filt,
874 ValuesReturnFilter vrf;
876 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
878 tag = ber_peek_tag( ber, &len );
880 if( tag == LBER_ERROR ) {
881 *text = "error decoding filter";
882 return SLAPD_DISCONNECT;
888 vrf.vrf_choice = tag;
890 switch ( vrf.vrf_choice ) {
891 case LDAP_FILTER_EQUALITY:
892 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
893 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_EQUALITY, text );
894 if ( err != LDAP_SUCCESS ) {
898 assert( vrf.vrf_ava != NULL );
901 case LDAP_FILTER_SUBSTRINGS:
902 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
903 err = get_ssa( op, ber, (Filter *)&vrf, text );
907 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
908 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_ORDERING, text );
909 if ( err != LDAP_SUCCESS ) {
915 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
916 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_ORDERING, text );
917 if ( err != LDAP_SUCCESS ) {
922 case LDAP_FILTER_PRESENT: {
925 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
926 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
927 err = SLAPD_DISCONNECT;
928 *text = "error decoding filter";
933 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
935 if( err != LDAP_SUCCESS ) {
936 vrf.vrf_choice |= SLAPD_FILTER_UNDEFINED;
937 err = slap_bv2undef_ad( &type, &vrf.vrf_desc, text,
940 if( err != LDAP_SUCCESS ) {
941 /* unrecognized attribute description or other error */
942 Debug( LDAP_DEBUG_ANY,
943 "get_simple_vrFilter: conn %lu unknown "
944 "attribute type=%s (%d)\n",
945 op->o_connid, type.bv_val, err );
947 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
948 vrf.vrf_result = LDAP_COMPARE_FALSE;
955 case LDAP_FILTER_APPROX:
956 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
957 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_EQUALITY_APPROX, text );
958 if ( err != LDAP_SUCCESS ) {
963 case LDAP_FILTER_EXT:
964 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
966 err = get_mra( op, ber, (Filter *)&vrf, text );
967 if ( err != LDAP_SUCCESS ) {
971 assert( vrf.vrf_mra != NULL );
975 (void) ber_scanf( ber, "x" ); /* skip the element */
976 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
977 vrf.vrf_choice, 0, 0 );
978 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
979 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
983 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
985 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
986 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
990 if ( err == LDAP_SUCCESS ) {
991 *filt = ch_malloc( sizeof vrf );
995 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1001 get_vrFilter( Operation *op, BerElement *ber,
1002 ValuesReturnFilter **vrf,
1006 * A ValuesReturnFilter looks like this:
1008 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1009 * SimpleFilterItem ::= CHOICE {
1010 * equalityMatch [3] AttributeValueAssertion,
1011 * substrings [4] SubstringFilter,
1012 * greaterOrEqual [5] AttributeValueAssertion,
1013 * lessOrEqual [6] AttributeValueAssertion,
1014 * present [7] AttributeType,
1015 * approxMatch [8] AttributeValueAssertion,
1016 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1019 * SubstringFilter ::= SEQUENCE {
1020 * type AttributeType,
1021 * SEQUENCE OF CHOICE {
1022 * initial [0] IA5String,
1023 * any [1] IA5String,
1024 * final [2] IA5String
1028 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1029 * matchingRule [1] MatchingRuleId OPTIONAL,
1030 * type [2] AttributeDescription OPTIONAL,
1031 * matchValue [3] AssertionValue }
1034 ValuesReturnFilter **n;
1039 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1041 tag = ber_peek_tag( ber, &len );
1043 if( tag == LBER_ERROR ) {
1044 *text = "error decoding vrFilter";
1045 return SLAPD_DISCONNECT;
1048 if( tag != LBER_SEQUENCE ) {
1049 *text = "error decoding vrFilter, expect SEQUENCE tag";
1050 return SLAPD_DISCONNECT;
1054 for ( tag = ber_first_element( ber, &len, &last );
1055 tag != LBER_DEFAULT;
1056 tag = ber_next_element( ber, &len, last ) )
1058 int err = get_simple_vrFilter( op, ber, n, text );
1060 if ( err != LDAP_SUCCESS ) return( err );
1062 n = &(*n)->vrf_next;
1066 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1067 return( LDAP_SUCCESS );
1071 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1073 ValuesReturnFilter *p, *next;
1075 if ( vrf == NULL ) {
1079 for ( p = vrf; p != NULL; p = next ) {
1082 switch ( vrf->vrf_choice & SLAPD_FILTER_MASK ) {
1083 case LDAP_FILTER_PRESENT:
1086 case LDAP_FILTER_EQUALITY:
1087 case LDAP_FILTER_GE:
1088 case LDAP_FILTER_LE:
1089 case LDAP_FILTER_APPROX:
1090 ava_free( op, vrf->vrf_ava, 1 );
1093 case LDAP_FILTER_SUBSTRINGS:
1094 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1095 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1097 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1098 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1099 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1101 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1104 case LDAP_FILTER_EXT:
1105 mra_free( op, vrf->vrf_mra, 1 );
1108 case SLAPD_FILTER_COMPUTED:
1112 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1113 vrf->vrf_choice, 0, 0 );
1117 op->o_tmpfree( vrf, op->o_tmpmemctx );
1122 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1124 ValuesReturnFilter *p;
1128 if ( vrf == NULL ) {
1129 ber_str2bv_x( "No filter!", STRLENOF("No filter!"),
1130 1, fstr, op->o_tmpmemctx );
1134 fstr->bv_len = STRLENOF("()");
1135 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1137 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1139 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1142 simple_vrFilter2bv( op, p, &tmp );
1144 fstr->bv_len += tmp.bv_len;
1145 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1148 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1149 /*"("*/ "%s)", tmp.bv_val );
1151 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1156 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1161 if ( vrf == NULL ) {
1162 ber_str2bv_x( "No filter!", STRLENOF("No filter!"), 1, fstr,
1166 int undef = vrf->vrf_choice & SLAPD_FILTER_UNDEFINED;
1168 switch ( vrf->vrf_choice & SLAPD_FILTER_MASK ) {
1169 case LDAP_FILTER_EQUALITY:
1170 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1172 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1173 tmp.bv_len + STRLENOF("(=)");
1174 if ( undef ) fstr->bv_len++;
1175 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1177 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1178 vrf->vrf_av_desc->ad_cname.bv_val,
1181 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1184 case LDAP_FILTER_GE:
1185 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1187 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1188 tmp.bv_len + STRLENOF("(>=)");
1189 if ( undef ) fstr->bv_len++;
1190 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1192 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1193 vrf->vrf_av_desc->ad_cname.bv_val,
1196 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1199 case LDAP_FILTER_LE:
1200 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1202 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1203 tmp.bv_len + STRLENOF("(<=)");
1204 if ( undef ) fstr->bv_len++;
1205 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1207 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1208 vrf->vrf_av_desc->ad_cname.bv_val,
1211 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1214 case LDAP_FILTER_APPROX:
1215 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1217 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1218 tmp.bv_len + STRLENOF("(~=)");
1219 if ( undef ) fstr->bv_len++;
1220 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1222 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1223 vrf->vrf_av_desc->ad_cname.bv_val,
1225 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1228 case LDAP_FILTER_SUBSTRINGS:
1229 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1231 if ( undef ) fstr->bv_len++;
1232 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1234 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1235 vrf->vrf_sub_desc->ad_cname.bv_val );
1237 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1240 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1242 fstr->bv_len += tmp.bv_len;
1243 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1246 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1247 /* "(attr=" */ "%s*)",
1250 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1253 if ( vrf->vrf_sub_any != NULL ) {
1255 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1257 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1260 fstr->bv_len += tmp.bv_len + 1;
1261 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1262 fstr->bv_len + 1, op->o_tmpmemctx );
1264 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1265 /* "(attr=[init]*[any*]" */ "%s*)",
1267 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1271 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1274 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1276 fstr->bv_len += tmp.bv_len;
1277 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1280 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1281 /* "(attr=[init*][any*]" */ "%s)",
1284 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1289 case LDAP_FILTER_PRESENT:
1290 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1292 if ( undef ) fstr->bv_len++;
1293 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1295 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1296 vrf->vrf_desc->ad_cname.bv_val );
1299 case LDAP_FILTER_EXT: {
1301 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1303 if ( vrf->vrf_mr_desc ) {
1304 ad = vrf->vrf_mr_desc->ad_cname;
1310 fstr->bv_len = ad.bv_len +
1311 ( vrf->vrf_mr_dnattrs ? STRLENOF(":dn") : 0 ) +
1312 ( vrf->vrf_mr_rule_text.bv_len
1313 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1314 tmp.bv_len + STRLENOF("(:=)");
1315 if ( undef ) fstr->bv_len++;
1316 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1318 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1320 vrf->vrf_mr_dnattrs ? ":dn" : "",
1321 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1322 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1325 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1328 case SLAPD_FILTER_COMPUTED:
1330 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1331 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1332 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1333 ? "(?=undefined)" : "(?=error)",
1334 vrf->vrf_result == LDAP_COMPARE_FALSE ? STRLENOF("(?=false)") :
1335 vrf->vrf_result == LDAP_COMPARE_TRUE ? STRLENOF("(?=true)") :
1336 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1337 ? STRLENOF("(?=undefined)") : STRLENOF("(?=error)"),
1338 1, fstr, op->o_tmpmemctx );
1342 ber_str2bv_x( "(?=unknown)", STRLENOF("(?=unknown)"),
1343 1, fstr, op->o_tmpmemctx );