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-2005 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(
45 SubstringsAssertion **s,
48 static int filter_escape_value_x(
53 static void simple_vrFilter2bv(
55 ValuesReturnFilter *f,
56 struct berval *fstr );
58 static int get_simple_vrFilter(
61 ValuesReturnFilter **f,
76 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
78 * A filter looks like this coming in:
80 * and [0] SET OF Filter,
81 * or [1] SET OF Filter,
83 * equalityMatch [3] AttributeValueAssertion,
84 * substrings [4] SubstringFilter,
85 * greaterOrEqual [5] AttributeValueAssertion,
86 * lessOrEqual [6] AttributeValueAssertion,
87 * present [7] AttributeType,,
88 * approxMatch [8] AttributeValueAssertion
89 * extensibleMatch [9] MatchingRuleAssertion
92 * SubstringFilter ::= SEQUENCE {
94 * SEQUENCE OF CHOICE {
95 * initial [0] IA5String,
101 * MatchingRuleAssertion ::= SEQUENCE {
102 * matchingRule [1] MatchingRuleId OPTIONAL,
103 * type [2] AttributeDescription OPTIONAL,
104 * matchValue [3] AssertionValue,
105 * dnAttributes [4] BOOLEAN DEFAULT FALSE
110 tag = ber_peek_tag( ber, &len );
112 if( tag == LBER_ERROR ) {
113 *text = "error decoding filter";
114 return SLAPD_DISCONNECT;
122 switch ( f.f_choice ) {
123 case LDAP_FILTER_EQUALITY:
124 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
125 err = get_ava( op, ber, &f.f_ava, SLAP_MR_EQUALITY, text );
126 if ( err != LDAP_SUCCESS ) {
130 assert( f.f_ava != NULL );
133 case LDAP_FILTER_SUBSTRINGS:
134 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
135 err = get_ssa( op, ber, &f.f_sub, text );
136 if( err != LDAP_SUCCESS ) {
139 assert( f.f_sub != NULL );
143 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
144 err = get_ava( op, ber, &f.f_ava, SLAP_MR_ORDERING, text );
145 if ( err != LDAP_SUCCESS ) {
148 assert( f.f_ava != NULL );
152 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
153 err = get_ava( op, ber, &f.f_ava, SLAP_MR_ORDERING, text );
154 if ( err != LDAP_SUCCESS ) {
157 assert( f.f_ava != NULL );
160 case LDAP_FILTER_PRESENT: {
163 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
164 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
165 err = SLAPD_DISCONNECT;
166 *text = "error decoding filter";
171 err = slap_bv2ad( &type, &f.f_desc, text );
173 if( err != LDAP_SUCCESS ) {
174 /* unrecognized attribute description or other error */
175 Debug( LDAP_DEBUG_ANY,
176 "get_filter: conn %d unknown attribute "
178 op->o_connid, type.bv_val, err );
180 f.f_choice = SLAPD_FILTER_COMPUTED;
181 f.f_result = LDAP_COMPARE_FALSE;
187 assert( f.f_desc != NULL );
190 case LDAP_FILTER_APPROX:
191 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
192 err = get_ava( op, ber, &f.f_ava, SLAP_MR_EQUALITY_APPROX, text );
193 if ( err != LDAP_SUCCESS ) {
196 assert( f.f_ava != NULL );
199 case LDAP_FILTER_AND:
200 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
201 err = get_filter_list( op, ber, &f.f_and, text );
202 if ( err != LDAP_SUCCESS ) {
205 if ( f.f_and == NULL ) {
206 f.f_choice = SLAPD_FILTER_COMPUTED;
207 f.f_result = LDAP_COMPARE_TRUE;
209 /* no assert - list could be empty */
213 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
214 err = get_filter_list( op, ber, &f.f_or, text );
215 if ( err != LDAP_SUCCESS ) {
218 if ( f.f_or == NULL ) {
219 f.f_choice = SLAPD_FILTER_COMPUTED;
220 f.f_result = LDAP_COMPARE_FALSE;
222 /* no assert - list could be empty */
225 case LDAP_FILTER_NOT:
226 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
227 (void) ber_skip_tag( ber, &len );
228 err = get_filter( op, ber, &f.f_not, text );
229 if ( err != LDAP_SUCCESS ) {
233 assert( f.f_not != NULL );
234 if ( f.f_not->f_choice == SLAPD_FILTER_COMPUTED ) {
235 int fresult = f.f_not->f_result;
236 f.f_choice = SLAPD_FILTER_COMPUTED;
237 op->o_tmpfree( f.f_not, op->o_tmpmemctx );
241 case LDAP_COMPARE_TRUE:
242 f.f_result = LDAP_COMPARE_FALSE;
244 case LDAP_COMPARE_FALSE:
245 f.f_result = LDAP_COMPARE_TRUE;
248 /* (!Undefined) is Undefined */
253 case LDAP_FILTER_EXT:
254 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
256 err = get_mra( op, ber, &f.f_mra, text );
257 if ( err != LDAP_SUCCESS ) {
261 assert( f.f_mra != NULL );
265 (void) ber_scanf( ber, "x" ); /* skip the element */
266 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
268 f.f_choice = SLAPD_FILTER_COMPUTED;
269 f.f_result = SLAPD_COMPARE_UNDEFINED;
273 if( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
276 f.f_choice = SLAPD_FILTER_COMPUTED;
277 f.f_result = SLAPD_COMPARE_UNDEFINED;
281 if ( err == LDAP_SUCCESS ) {
282 *filt = op->o_tmpalloc( sizeof(f), op->o_tmpmemctx );
286 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
292 get_filter_list( Operation *op, BerElement *ber,
302 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
304 for ( tag = ber_first_element( ber, &len, &last );
306 tag = ber_next_element( ber, &len, last ) )
308 err = get_filter( op, ber, new, text );
309 if ( err != LDAP_SUCCESS )
311 new = &(*new)->f_next;
315 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
316 return( LDAP_SUCCESS );
323 SubstringsAssertion **out,
329 struct berval desc, value, nvalue;
331 SubstringsAssertion ssa;
333 *text = "error decoding filter";
336 Debug( LDAP_DEBUG_FILTER, "begin get_ssa\n", 0, 0, 0 );
337 if ( ber_scanf( ber, "{m" /*}*/, &desc ) == LBER_ERROR ) {
338 return SLAPD_DISCONNECT;
344 ssa.sa_initial.bv_val = NULL;
346 ssa.sa_final.bv_val = NULL;
348 rc = slap_bv2ad( &desc, &ssa.sa_desc, text );
350 if( rc != LDAP_SUCCESS ) {
351 Debug( LDAP_DEBUG_ANY,
352 "get_ssa: conn %d unknown attribute type=%s (%d)\n",
353 op->o_connid, desc.bv_val, rc );
355 /* skip over the rest of this filter */
356 for ( tag = ber_first_element( ber, &len, &last );
358 tag = ber_next_element( ber, &len, last ) ) {
359 ber_scanf( ber, "x" );
364 rc = LDAP_PROTOCOL_ERROR;
366 for ( tag = ber_first_element( ber, &len, &last );
368 tag = ber_next_element( ber, &len, last ) )
372 rc = ber_scanf( ber, "m", &value );
373 if ( rc == LBER_ERROR ) {
374 rc = SLAPD_DISCONNECT;
378 if ( value.bv_val == NULL || value.bv_len == 0 ) {
379 rc = LDAP_INVALID_SYNTAX;
384 case LDAP_SUBSTRING_INITIAL:
385 usage = SLAP_MR_SUBSTR_INITIAL;
388 case LDAP_SUBSTRING_ANY:
389 usage = SLAP_MR_SUBSTR_ANY;
392 case LDAP_SUBSTRING_FINAL:
393 usage = SLAP_MR_SUBSTR_FINAL;
397 rc = LDAP_PROTOCOL_ERROR;
399 Debug( LDAP_DEBUG_FILTER,
400 " unknown substring choice=%ld\n",
406 /* validate/normalize using equality matching rule validator! */
407 rc = asserted_value_validate_normalize(
408 ssa.sa_desc, ssa.sa_desc->ad_type->sat_equality,
409 usage, &value, &nvalue, text, op->o_tmpmemctx );
411 if( rc != LDAP_SUCCESS ) {
415 rc = LDAP_PROTOCOL_ERROR;
418 case LDAP_SUBSTRING_INITIAL:
419 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
421 if ( ssa.sa_initial.bv_val != NULL
422 || ssa.sa_any != NULL
423 || ssa.sa_final.bv_val != NULL )
425 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
429 ssa.sa_initial = nvalue;
432 case LDAP_SUBSTRING_ANY:
433 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
435 if ( ssa.sa_final.bv_val != NULL ) {
436 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
440 ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx );
443 case LDAP_SUBSTRING_FINAL:
444 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
446 if ( ssa.sa_final.bv_val != NULL ) {
447 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
451 ssa.sa_final = nvalue;
455 Debug( LDAP_DEBUG_FILTER,
456 " unknown substring type=%ld\n",
460 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
463 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
465 slap_sl_free( ssa.sa_initial.bv_val, op->o_tmpmemctx );
466 ber_bvarray_free_x( ssa.sa_any, op->o_tmpmemctx );
467 slap_sl_free( ssa.sa_final.bv_val, op->o_tmpmemctx );
474 if( rc == LDAP_SUCCESS ) {
475 *out = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
479 Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
481 return rc /* LDAP_SUCCESS */ ;
485 filter_free_x( Operation *op, Filter *f )
493 switch ( f->f_choice ) {
494 case LDAP_FILTER_PRESENT:
497 case LDAP_FILTER_EQUALITY:
500 case LDAP_FILTER_APPROX:
501 ava_free( op, f->f_ava, 1 );
504 case LDAP_FILTER_SUBSTRINGS:
505 if ( f->f_sub_initial.bv_val != NULL ) {
506 op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx );
508 ber_bvarray_free_x( f->f_sub_any, op->o_tmpmemctx );
509 if ( f->f_sub_final.bv_val != NULL ) {
510 op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx );
512 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
515 case LDAP_FILTER_AND:
517 case LDAP_FILTER_NOT:
518 for ( p = f->f_list; p != NULL; p = next ) {
520 filter_free_x( op, p );
524 case LDAP_FILTER_EXT:
525 mra_free( op, f->f_mra, 1 );
528 case SLAPD_FILTER_COMPUTED:
532 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
537 op->o_tmpfree( f, op->o_tmpmemctx );
541 filter_free( Filter *f )
547 op.o_tmpmemctx = slap_sl_context( f );
548 op.o_tmpmfuncs = &slap_sl_mfuncs;
549 filter_free_x( &op, f );
553 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
559 ber_bvfalse = BER_BVC( "(?=false)" ),
560 ber_bvtrue = BER_BVC( "(?=true)" ),
561 ber_bvundefined = BER_BVC( "(?=undefined)" ),
562 ber_bverror = BER_BVC( "(?=error)" ),
563 ber_bvunknown = BER_BVC( "(?=unknown)" ),
564 ber_bvnone = BER_BVC( "(?=none)" );
568 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
572 switch ( f->f_choice ) {
573 case LDAP_FILTER_EQUALITY:
574 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
576 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
577 tmp.bv_len + ( sizeof("(=)") - 1 );
578 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
580 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
581 f->f_av_desc->ad_cname.bv_val,
584 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
588 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
590 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
591 tmp.bv_len + ( sizeof("(>=)") - 1 );
592 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
594 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
595 f->f_av_desc->ad_cname.bv_val,
598 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
602 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
604 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
605 tmp.bv_len + ( sizeof("(<=)") - 1 );
606 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
608 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
609 f->f_av_desc->ad_cname.bv_val,
612 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
615 case LDAP_FILTER_APPROX:
616 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
618 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
619 tmp.bv_len + ( sizeof("(~=)") - 1 );
620 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
622 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
623 f->f_av_desc->ad_cname.bv_val,
625 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
628 case LDAP_FILTER_SUBSTRINGS:
629 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
630 ( sizeof("(=*)") - 1 );
631 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
633 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
634 f->f_sub_desc->ad_cname.bv_val );
636 if ( f->f_sub_initial.bv_val != NULL ) {
639 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
641 fstr->bv_len += tmp.bv_len;
642 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
645 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
646 /* "(attr=" */ "%s*)",
649 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
652 if ( f->f_sub_any != NULL ) {
653 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
655 filter_escape_value_x( &f->f_sub_any[i],
656 &tmp, op->o_tmpmemctx );
658 fstr->bv_len += tmp.bv_len + 1;
659 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
662 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
663 /* "(attr=[init]*[any*]" */ "%s*)",
665 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
669 if ( f->f_sub_final.bv_val != NULL ) {
672 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
674 fstr->bv_len += tmp.bv_len;
675 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
678 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
679 /* "(attr=[init*][any*]" */ "%s)",
682 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
687 case LDAP_FILTER_PRESENT:
688 fstr->bv_len = f->f_desc->ad_cname.bv_len +
689 ( sizeof("(=*)") - 1 );
690 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
692 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
693 f->f_desc->ad_cname.bv_val );
696 case LDAP_FILTER_AND:
698 case LDAP_FILTER_NOT:
699 fstr->bv_len = sizeof("(%)") - 1;
700 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
702 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
703 f->f_choice == LDAP_FILTER_AND ? '&' :
704 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
706 for ( p = f->f_list; p != NULL; p = p->f_next ) {
709 filter2bv_x( op, p, &tmp );
711 fstr->bv_len += tmp.bv_len;
712 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
715 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
716 /*"("*/ "%s)", tmp.bv_val );
718 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
723 case LDAP_FILTER_EXT: {
725 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
727 if ( f->f_mr_desc ) {
728 ad = f->f_mr_desc->ad_cname;
734 fstr->bv_len = ad.bv_len +
735 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
736 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
737 tmp.bv_len + ( sizeof("(:=)") - 1 );
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%s%s:=%s)",
742 f->f_mr_dnattrs ? ":dn" : "",
743 f->f_mr_rule_text.bv_len ? ":" : "",
744 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
746 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
749 case SLAPD_FILTER_COMPUTED:
750 switch ( f->f_result ) {
751 case LDAP_COMPARE_FALSE:
755 case LDAP_COMPARE_TRUE:
759 case SLAPD_COMPARE_UNDEFINED:
760 tmp = ber_bvundefined;
768 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
772 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
778 filter2bv( Filter *f, struct berval *fstr )
784 op.o_tmpmemctx = NULL;
785 op.o_tmpmfuncs = &ch_mfuncs;
787 filter2bv_x( &op, f, fstr );
791 filter_escape_value_x(
800 i = in->bv_len * 3 + 1;
801 out->bv_val = ctx ? slap_sl_malloc( i, ctx ) : ch_malloc( i );
804 for( i=0; i < in->bv_len ; i++ ) {
805 if( FILTER_ESCAPE(in->bv_val[i]) ) {
806 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
807 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
808 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
810 out->bv_val[out->bv_len++] = in->bv_val[i];
814 out->bv_val[out->bv_len] = '\0';
823 return filter_escape_value_x( in, out, NULL );
830 ValuesReturnFilter **filt,
836 ValuesReturnFilter vrf;
838 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
840 tag = ber_peek_tag( ber, &len );
842 if( tag == LBER_ERROR ) {
843 *text = "error decoding filter";
844 return SLAPD_DISCONNECT;
850 vrf.vrf_choice = tag;
852 switch ( vrf.vrf_choice ) {
853 case LDAP_FILTER_EQUALITY:
854 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
855 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
856 if ( err != LDAP_SUCCESS ) {
860 assert( vrf.vrf_ava != NULL );
863 case LDAP_FILTER_SUBSTRINGS:
864 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
865 err = get_ssa( op, ber, &vrf.vrf_sub, text );
869 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
870 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
871 if ( err != LDAP_SUCCESS ) {
877 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
878 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
879 if ( err != LDAP_SUCCESS ) {
884 case LDAP_FILTER_PRESENT: {
887 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
888 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
889 err = SLAPD_DISCONNECT;
890 *text = "error decoding filter";
895 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
897 if( err != LDAP_SUCCESS ) {
898 /* unrecognized attribute description or other error */
899 Debug( LDAP_DEBUG_ANY,
900 "get_simple_vrFilter: conn %d unknown "
901 "attribute type=%s (%d)\n",
902 op->o_connid, type.bv_val, err );
904 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
905 vrf.vrf_result = LDAP_COMPARE_FALSE;
911 case LDAP_FILTER_APPROX:
912 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
913 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
914 if ( err != LDAP_SUCCESS ) {
919 case LDAP_FILTER_EXT:
920 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
922 err = get_mra( op, ber, &vrf.vrf_mra, text );
923 if ( err != LDAP_SUCCESS ) {
927 assert( vrf.vrf_mra != NULL );
931 (void) ber_scanf( ber, "x" ); /* skip the element */
932 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
933 vrf.vrf_choice, 0, 0 );
934 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
935 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
939 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
941 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
942 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
946 if ( err == LDAP_SUCCESS ) {
947 *filt = ch_malloc( sizeof vrf );
951 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
957 get_vrFilter( Operation *op, BerElement *ber,
958 ValuesReturnFilter **vrf,
962 * A ValuesReturnFilter looks like this:
964 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
965 * SimpleFilterItem ::= CHOICE {
966 * equalityMatch [3] AttributeValueAssertion,
967 * substrings [4] SubstringFilter,
968 * greaterOrEqual [5] AttributeValueAssertion,
969 * lessOrEqual [6] AttributeValueAssertion,
970 * present [7] AttributeType,
971 * approxMatch [8] AttributeValueAssertion,
972 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
975 * SubstringFilter ::= SEQUENCE {
976 * type AttributeType,
977 * SEQUENCE OF CHOICE {
978 * initial [0] IA5String,
980 * final [2] IA5String
984 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
985 * matchingRule [1] MatchingRuleId OPTIONAL,
986 * type [2] AttributeDescription OPTIONAL,
987 * matchValue [3] AssertionValue }
990 ValuesReturnFilter **n;
995 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
997 tag = ber_peek_tag( ber, &len );
999 if( tag == LBER_ERROR ) {
1000 *text = "error decoding vrFilter";
1001 return SLAPD_DISCONNECT;
1004 if( tag != LBER_SEQUENCE ) {
1005 *text = "error decoding vrFilter, expect SEQUENCE tag";
1006 return SLAPD_DISCONNECT;
1010 for ( tag = ber_first_element( ber, &len, &last );
1011 tag != LBER_DEFAULT;
1012 tag = ber_next_element( ber, &len, last ) )
1014 int err = get_simple_vrFilter( op, ber, n, text );
1016 if ( err != LDAP_SUCCESS ) return( err );
1018 n = &(*n)->vrf_next;
1022 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1023 return( LDAP_SUCCESS );
1027 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1029 ValuesReturnFilter *p, *next;
1031 if ( vrf == NULL ) {
1035 for ( p = vrf; p != NULL; p = next ) {
1038 switch ( vrf->vrf_choice ) {
1039 case LDAP_FILTER_PRESENT:
1042 case LDAP_FILTER_EQUALITY:
1043 case LDAP_FILTER_GE:
1044 case LDAP_FILTER_LE:
1045 case LDAP_FILTER_APPROX:
1046 ava_free( op, vrf->vrf_ava, 1 );
1049 case LDAP_FILTER_SUBSTRINGS:
1050 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1051 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1053 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1054 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1055 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1057 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1060 case LDAP_FILTER_EXT:
1061 mra_free( op, vrf->vrf_mra, 1 );
1064 case SLAPD_FILTER_COMPUTED:
1068 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1069 vrf->vrf_choice, 0, 0 );
1073 op->o_tmpfree( vrf, op->o_tmpmemctx );
1078 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1080 ValuesReturnFilter *p;
1084 if ( vrf == NULL ) {
1085 ber_str2bv_x( "No filter!", sizeof("No filter!")-1,
1086 1, fstr, op->o_tmpmemctx );
1090 fstr->bv_len = sizeof("()") - 1;
1091 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1093 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1095 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1098 simple_vrFilter2bv( op, p, &tmp );
1100 fstr->bv_len += tmp.bv_len;
1101 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1104 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1105 /*"("*/ "%s)", tmp.bv_val );
1107 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1112 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1117 if ( vrf == NULL ) {
1118 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr,
1123 switch ( vrf->vrf_choice ) {
1124 case LDAP_FILTER_EQUALITY:
1125 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1127 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1128 tmp.bv_len + ( sizeof("(=)") - 1 );
1129 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1131 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1132 vrf->vrf_av_desc->ad_cname.bv_val,
1135 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1138 case LDAP_FILTER_GE:
1139 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1141 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1142 tmp.bv_len + ( sizeof("(>=)") - 1 );
1143 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1145 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1146 vrf->vrf_av_desc->ad_cname.bv_val,
1149 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1152 case LDAP_FILTER_LE:
1153 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1155 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1156 tmp.bv_len + ( sizeof("(<=)") - 1 );
1157 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1159 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1160 vrf->vrf_av_desc->ad_cname.bv_val,
1163 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1166 case LDAP_FILTER_APPROX:
1167 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1169 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1170 tmp.bv_len + ( sizeof("(~=)") - 1 );
1171 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1173 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1174 vrf->vrf_av_desc->ad_cname.bv_val,
1176 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1179 case LDAP_FILTER_SUBSTRINGS:
1180 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1181 ( sizeof("(=*)") - 1 );
1182 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1184 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1185 vrf->vrf_sub_desc->ad_cname.bv_val );
1187 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1190 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1192 fstr->bv_len += tmp.bv_len;
1193 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1196 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1197 /* "(attr=" */ "%s*)",
1200 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1203 if ( vrf->vrf_sub_any != NULL ) {
1205 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1207 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1210 fstr->bv_len += tmp.bv_len + 1;
1211 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1212 fstr->bv_len + 1, op->o_tmpmemctx );
1214 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1215 /* "(attr=[init]*[any*]" */ "%s*)",
1217 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1221 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1224 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1226 fstr->bv_len += tmp.bv_len;
1227 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1230 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1231 /* "(attr=[init*][any*]" */ "%s)",
1234 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1239 case LDAP_FILTER_PRESENT:
1240 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1241 ( sizeof("(=*)") - 1 );
1242 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1244 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1245 vrf->vrf_desc->ad_cname.bv_val );
1248 case LDAP_FILTER_EXT: {
1250 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1252 if ( vrf->vrf_mr_desc ) {
1253 ad = vrf->vrf_mr_desc->ad_cname;
1259 fstr->bv_len = ad.bv_len +
1260 ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1261 ( vrf->vrf_mr_rule_text.bv_len
1262 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1263 tmp.bv_len + ( sizeof("(:=)") - 1 );
1264 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1266 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1268 vrf->vrf_mr_dnattrs ? ":dn" : "",
1269 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1270 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1273 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1276 case SLAPD_FILTER_COMPUTED:
1278 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1279 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1280 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1281 ? "(?=undefined)" : "(?=error)",
1282 vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1283 vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1284 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1285 ? sizeof("(?=undefined)")-1 : sizeof("(?=error)")-1,
1286 1, fstr, op->o_tmpmemctx );
1290 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1,
1291 1, fstr, op->o_tmpmemctx );