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-2004 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 )
545 op.o_tmpmemctx = slap_sl_context( f );
546 op.o_tmpmfuncs = &slap_sl_mfuncs;
547 filter_free_x( &op, f );
551 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
557 ber_bvfalse = BER_BVC( "(?=false)" ),
558 ber_bvtrue = BER_BVC( "(?=true)" ),
559 ber_bvundefined = BER_BVC( "(?=undefined)" ),
560 ber_bverror = BER_BVC( "(?=error)" ),
561 ber_bvunknown = BER_BVC( "(?=unknown)" ),
562 ber_bvnone = BER_BVC( "(?=none)" );
566 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
570 switch ( f->f_choice ) {
571 case LDAP_FILTER_EQUALITY:
572 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
574 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
575 tmp.bv_len + ( sizeof("(=)") - 1 );
576 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
578 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
579 f->f_av_desc->ad_cname.bv_val,
582 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
586 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
588 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
589 tmp.bv_len + ( sizeof("(>=)") - 1 );
590 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
592 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
593 f->f_av_desc->ad_cname.bv_val,
596 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
600 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
602 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
603 tmp.bv_len + ( sizeof("(<=)") - 1 );
604 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
606 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
607 f->f_av_desc->ad_cname.bv_val,
610 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
613 case LDAP_FILTER_APPROX:
614 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
616 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
617 tmp.bv_len + ( sizeof("(~=)") - 1 );
618 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
620 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
621 f->f_av_desc->ad_cname.bv_val,
623 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
626 case LDAP_FILTER_SUBSTRINGS:
627 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
628 ( sizeof("(=*)") - 1 );
629 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
631 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
632 f->f_sub_desc->ad_cname.bv_val );
634 if ( f->f_sub_initial.bv_val != NULL ) {
637 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
639 fstr->bv_len += tmp.bv_len;
640 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
643 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
644 /* "(attr=" */ "%s*)",
647 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
650 if ( f->f_sub_any != NULL ) {
651 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
653 filter_escape_value_x( &f->f_sub_any[i],
654 &tmp, op->o_tmpmemctx );
656 fstr->bv_len += tmp.bv_len + 1;
657 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
660 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
661 /* "(attr=[init]*[any*]" */ "%s*)",
663 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
667 if ( f->f_sub_final.bv_val != NULL ) {
670 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
672 fstr->bv_len += tmp.bv_len;
673 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
676 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
677 /* "(attr=[init*][any*]" */ "%s)",
680 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
685 case LDAP_FILTER_PRESENT:
686 fstr->bv_len = f->f_desc->ad_cname.bv_len +
687 ( sizeof("(=*)") - 1 );
688 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
690 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
691 f->f_desc->ad_cname.bv_val );
694 case LDAP_FILTER_AND:
696 case LDAP_FILTER_NOT:
697 fstr->bv_len = sizeof("(%)") - 1;
698 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
700 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
701 f->f_choice == LDAP_FILTER_AND ? '&' :
702 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
704 for ( p = f->f_list; p != NULL; p = p->f_next ) {
707 filter2bv_x( op, p, &tmp );
709 fstr->bv_len += tmp.bv_len;
710 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
713 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
714 /*"("*/ "%s)", tmp.bv_val );
716 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
721 case LDAP_FILTER_EXT: {
723 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
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 ? sizeof(":dn")-1 : 0 ) +
734 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
735 tmp.bv_len + ( sizeof("(:=)") - 1 );
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)",
740 f->f_mr_dnattrs ? ":dn" : "",
741 f->f_mr_rule_text.bv_len ? ":" : "",
742 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
744 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
747 case SLAPD_FILTER_COMPUTED:
748 switch ( f->f_result ) {
749 case LDAP_COMPARE_FALSE:
753 case LDAP_COMPARE_TRUE:
757 case SLAPD_COMPARE_UNDEFINED:
758 tmp = ber_bvundefined;
766 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
770 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
776 filter2bv( Filter *f, struct berval *fstr )
779 op.o_tmpmemctx = NULL;
780 op.o_tmpmfuncs = &ch_mfuncs;
782 filter2bv_x( &op, f, fstr );
786 filter_escape_value_x(
795 i = in->bv_len * 3 + 1;
796 out->bv_val = ctx ? slap_sl_malloc( i, ctx ) : ch_malloc( i );
799 for( i=0; i < in->bv_len ; i++ ) {
800 if( FILTER_ESCAPE(in->bv_val[i]) ) {
801 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
802 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
803 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
805 out->bv_val[out->bv_len++] = in->bv_val[i];
809 out->bv_val[out->bv_len] = '\0';
818 return filter_escape_value_x( in, out, NULL );
825 ValuesReturnFilter **filt,
831 ValuesReturnFilter vrf;
833 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
835 tag = ber_peek_tag( ber, &len );
837 if( tag == LBER_ERROR ) {
838 *text = "error decoding filter";
839 return SLAPD_DISCONNECT;
845 vrf.vrf_choice = tag;
847 switch ( vrf.vrf_choice ) {
848 case LDAP_FILTER_EQUALITY:
849 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
850 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
851 if ( err != LDAP_SUCCESS ) {
855 assert( vrf.vrf_ava != NULL );
858 case LDAP_FILTER_SUBSTRINGS:
859 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
860 err = get_ssa( op, ber, &vrf.vrf_sub, text );
864 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
865 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
866 if ( err != LDAP_SUCCESS ) {
872 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
873 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
874 if ( err != LDAP_SUCCESS ) {
879 case LDAP_FILTER_PRESENT: {
882 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
883 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
884 err = SLAPD_DISCONNECT;
885 *text = "error decoding filter";
890 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
892 if( err != LDAP_SUCCESS ) {
893 /* unrecognized attribute description or other error */
894 Debug( LDAP_DEBUG_ANY,
895 "get_simple_vrFilter: conn %d unknown "
896 "attribute type=%s (%d)\n",
897 op->o_connid, type.bv_val, err );
899 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
900 vrf.vrf_result = LDAP_COMPARE_FALSE;
906 case LDAP_FILTER_APPROX:
907 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
908 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
909 if ( err != LDAP_SUCCESS ) {
914 case LDAP_FILTER_EXT:
915 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
917 err = get_mra( op, ber, &vrf.vrf_mra, text );
918 if ( err != LDAP_SUCCESS ) {
922 assert( vrf.vrf_mra != NULL );
926 (void) ber_scanf( ber, "x" ); /* skip the element */
927 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
928 vrf.vrf_choice, 0, 0 );
929 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
930 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
934 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
936 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
937 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
941 if ( err == LDAP_SUCCESS ) {
942 *filt = ch_malloc( sizeof vrf );
946 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
952 get_vrFilter( Operation *op, BerElement *ber,
953 ValuesReturnFilter **vrf,
957 * A ValuesReturnFilter looks like this:
959 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
960 * SimpleFilterItem ::= CHOICE {
961 * equalityMatch [3] AttributeValueAssertion,
962 * substrings [4] SubstringFilter,
963 * greaterOrEqual [5] AttributeValueAssertion,
964 * lessOrEqual [6] AttributeValueAssertion,
965 * present [7] AttributeType,
966 * approxMatch [8] AttributeValueAssertion,
967 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
970 * SubstringFilter ::= SEQUENCE {
971 * type AttributeType,
972 * SEQUENCE OF CHOICE {
973 * initial [0] IA5String,
975 * final [2] IA5String
979 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
980 * matchingRule [1] MatchingRuleId OPTIONAL,
981 * type [2] AttributeDescription OPTIONAL,
982 * matchValue [3] AssertionValue }
985 ValuesReturnFilter **n;
990 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
992 tag = ber_peek_tag( ber, &len );
994 if( tag == LBER_ERROR ) {
995 *text = "error decoding vrFilter";
996 return SLAPD_DISCONNECT;
999 if( tag != LBER_SEQUENCE ) {
1000 *text = "error decoding vrFilter, expect SEQUENCE tag";
1001 return SLAPD_DISCONNECT;
1005 for ( tag = ber_first_element( ber, &len, &last );
1006 tag != LBER_DEFAULT;
1007 tag = ber_next_element( ber, &len, last ) )
1009 int err = get_simple_vrFilter( op, ber, n, text );
1011 if ( err != LDAP_SUCCESS ) return( err );
1013 n = &(*n)->vrf_next;
1017 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1018 return( LDAP_SUCCESS );
1022 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1024 ValuesReturnFilter *p, *next;
1026 if ( vrf == NULL ) {
1030 for ( p = vrf; p != NULL; p = next ) {
1033 switch ( vrf->vrf_choice ) {
1034 case LDAP_FILTER_PRESENT:
1037 case LDAP_FILTER_EQUALITY:
1038 case LDAP_FILTER_GE:
1039 case LDAP_FILTER_LE:
1040 case LDAP_FILTER_APPROX:
1041 ava_free( op, vrf->vrf_ava, 1 );
1044 case LDAP_FILTER_SUBSTRINGS:
1045 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1046 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1048 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1049 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1050 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1052 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1055 case LDAP_FILTER_EXT:
1056 mra_free( op, vrf->vrf_mra, 1 );
1059 case SLAPD_FILTER_COMPUTED:
1063 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1064 vrf->vrf_choice, 0, 0 );
1068 op->o_tmpfree( vrf, op->o_tmpmemctx );
1073 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1075 ValuesReturnFilter *p;
1079 if ( vrf == NULL ) {
1080 ber_str2bv_x( "No filter!", sizeof("No filter!")-1,
1081 1, fstr, op->o_tmpmemctx );
1085 fstr->bv_len = sizeof("()") - 1;
1086 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1088 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1090 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1093 simple_vrFilter2bv( op, p, &tmp );
1095 fstr->bv_len += tmp.bv_len;
1096 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1099 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1100 /*"("*/ "%s)", tmp.bv_val );
1102 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1107 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1112 if ( vrf == NULL ) {
1113 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr,
1118 switch ( vrf->vrf_choice ) {
1119 case LDAP_FILTER_EQUALITY:
1120 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1122 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1123 tmp.bv_len + ( sizeof("(=)") - 1 );
1124 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1126 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1127 vrf->vrf_av_desc->ad_cname.bv_val,
1130 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1133 case LDAP_FILTER_GE:
1134 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1136 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1137 tmp.bv_len + ( sizeof("(>=)") - 1 );
1138 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1140 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1141 vrf->vrf_av_desc->ad_cname.bv_val,
1144 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1147 case LDAP_FILTER_LE:
1148 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1150 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1151 tmp.bv_len + ( sizeof("(<=)") - 1 );
1152 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1154 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1155 vrf->vrf_av_desc->ad_cname.bv_val,
1158 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1161 case LDAP_FILTER_APPROX:
1162 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1164 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1165 tmp.bv_len + ( sizeof("(~=)") - 1 );
1166 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1168 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1169 vrf->vrf_av_desc->ad_cname.bv_val,
1171 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1174 case LDAP_FILTER_SUBSTRINGS:
1175 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1176 ( sizeof("(=*)") - 1 );
1177 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1179 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1180 vrf->vrf_sub_desc->ad_cname.bv_val );
1182 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1185 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1187 fstr->bv_len += tmp.bv_len;
1188 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1191 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1192 /* "(attr=" */ "%s*)",
1195 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1198 if ( vrf->vrf_sub_any != NULL ) {
1200 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1202 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1205 fstr->bv_len += tmp.bv_len + 1;
1206 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1207 fstr->bv_len + 1, op->o_tmpmemctx );
1209 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1210 /* "(attr=[init]*[any*]" */ "%s*)",
1212 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1216 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1219 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1221 fstr->bv_len += tmp.bv_len;
1222 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1225 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1226 /* "(attr=[init*][any*]" */ "%s)",
1229 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1234 case LDAP_FILTER_PRESENT:
1235 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1236 ( sizeof("(=*)") - 1 );
1237 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1239 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1240 vrf->vrf_desc->ad_cname.bv_val );
1243 case LDAP_FILTER_EXT: {
1245 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1247 if ( vrf->vrf_mr_desc ) {
1248 ad = vrf->vrf_mr_desc->ad_cname;
1254 fstr->bv_len = ad.bv_len +
1255 ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1256 ( vrf->vrf_mr_rule_text.bv_len
1257 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1258 tmp.bv_len + ( sizeof("(:=)") - 1 );
1259 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1261 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1263 vrf->vrf_mr_dnattrs ? ":dn" : "",
1264 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1265 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1268 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1271 case SLAPD_FILTER_COMPUTED:
1273 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1274 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1275 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1276 ? "(?=undefined)" : "(?=error)",
1277 vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1278 vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1279 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1280 ? sizeof("(?=undefined)")-1 : sizeof("(?=error)")-1,
1281 1, fstr, op->o_tmpmemctx );
1285 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1,
1286 1, fstr, op->o_tmpmemctx );