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 err = slap_bv2undef_ad( &type, &f.f_desc, text,
175 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
177 if ( err != LDAP_SUCCESS ) {
178 /* unrecognized attribute description or other error */
179 Debug( LDAP_DEBUG_ANY,
180 "get_filter: conn %lu unknown attribute "
182 op->o_connid, type.bv_val, err );
184 f.f_choice = SLAPD_FILTER_COMPUTED;
185 f.f_result = LDAP_COMPARE_FALSE;
192 assert( f.f_desc != NULL );
195 case LDAP_FILTER_APPROX:
196 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
197 err = get_ava( op, ber, &f.f_ava, SLAP_MR_EQUALITY_APPROX, text );
198 if ( err != LDAP_SUCCESS ) {
201 assert( f.f_ava != NULL );
204 case LDAP_FILTER_AND:
205 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
206 err = get_filter_list( op, ber, &f.f_and, text );
207 if ( err != LDAP_SUCCESS ) {
210 if ( f.f_and == NULL ) {
211 f.f_choice = SLAPD_FILTER_COMPUTED;
212 f.f_result = LDAP_COMPARE_TRUE;
214 /* no assert - list could be empty */
218 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
219 err = get_filter_list( op, ber, &f.f_or, text );
220 if ( err != LDAP_SUCCESS ) {
223 if ( f.f_or == NULL ) {
224 f.f_choice = SLAPD_FILTER_COMPUTED;
225 f.f_result = LDAP_COMPARE_FALSE;
227 /* no assert - list could be empty */
230 case LDAP_FILTER_NOT:
231 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
232 (void) ber_skip_tag( ber, &len );
233 err = get_filter( op, ber, &f.f_not, text );
234 if ( err != LDAP_SUCCESS ) {
238 assert( f.f_not != NULL );
239 if ( f.f_not->f_choice == SLAPD_FILTER_COMPUTED ) {
240 int fresult = f.f_not->f_result;
241 f.f_choice = SLAPD_FILTER_COMPUTED;
242 op->o_tmpfree( f.f_not, op->o_tmpmemctx );
246 case LDAP_COMPARE_TRUE:
247 f.f_result = LDAP_COMPARE_FALSE;
249 case LDAP_COMPARE_FALSE:
250 f.f_result = LDAP_COMPARE_TRUE;
253 /* (!Undefined) is Undefined */
258 case LDAP_FILTER_EXT:
259 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
261 err = get_mra( op, ber, &f.f_mra, text );
262 if ( err != LDAP_SUCCESS ) {
266 assert( f.f_mra != NULL );
270 (void) ber_scanf( ber, "x" ); /* skip the element */
271 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
273 f.f_choice = SLAPD_FILTER_COMPUTED;
274 f.f_result = SLAPD_COMPARE_UNDEFINED;
278 if( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
281 f.f_choice = SLAPD_FILTER_COMPUTED;
282 f.f_result = SLAPD_COMPARE_UNDEFINED;
286 if ( err == LDAP_SUCCESS ) {
287 *filt = op->o_tmpalloc( sizeof(f), op->o_tmpmemctx );
291 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
297 get_filter_list( Operation *op, BerElement *ber,
307 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
309 for ( tag = ber_first_element( ber, &len, &last );
311 tag = ber_next_element( ber, &len, last ) )
313 err = get_filter( op, ber, new, text );
314 if ( err != LDAP_SUCCESS )
316 new = &(*new)->f_next;
320 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
321 return( LDAP_SUCCESS );
328 SubstringsAssertion **out,
334 struct berval desc, value, nvalue;
336 SubstringsAssertion ssa;
338 *text = "error decoding filter";
341 Debug( LDAP_DEBUG_FILTER, "begin get_ssa\n", 0, 0, 0 );
342 if ( ber_scanf( ber, "{m" /*}*/, &desc ) == LBER_ERROR ) {
343 return SLAPD_DISCONNECT;
349 ssa.sa_initial.bv_val = NULL;
351 ssa.sa_final.bv_val = NULL;
353 rc = slap_bv2ad( &desc, &ssa.sa_desc, text );
355 if( rc != LDAP_SUCCESS ) {
356 rc = slap_bv2undef_ad( &desc, &ssa.sa_desc, text,
357 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
359 if( rc != LDAP_SUCCESS ) {
360 Debug( LDAP_DEBUG_ANY,
361 "get_ssa: conn %lu unknown attribute type=%s (%ld)\n",
362 op->o_connid, desc.bv_val, (long) rc );
364 /* skip over the rest of this filter */
365 for ( tag = ber_first_element( ber, &len, &last );
367 tag = ber_next_element( ber, &len, last ) ) {
368 ber_scanf( ber, "x" );
374 rc = LDAP_PROTOCOL_ERROR;
376 for ( tag = ber_first_element( ber, &len, &last );
378 tag = ber_next_element( ber, &len, last ) )
382 rc = ber_scanf( ber, "m", &value );
383 if ( rc == LBER_ERROR ) {
384 rc = SLAPD_DISCONNECT;
388 if ( value.bv_val == NULL || value.bv_len == 0 ) {
389 rc = LDAP_INVALID_SYNTAX;
394 case LDAP_SUBSTRING_INITIAL:
395 if ( ssa.sa_initial.bv_val != NULL
396 || ssa.sa_any != NULL
397 || ssa.sa_final.bv_val != NULL )
399 rc = LDAP_PROTOCOL_ERROR;
402 usage = SLAP_MR_SUBSTR_INITIAL;
405 case LDAP_SUBSTRING_ANY:
406 if ( ssa.sa_final.bv_val != NULL ) {
407 rc = LDAP_PROTOCOL_ERROR;
410 usage = SLAP_MR_SUBSTR_ANY;
413 case LDAP_SUBSTRING_FINAL:
414 if ( ssa.sa_final.bv_val != NULL ) {
415 rc = LDAP_PROTOCOL_ERROR;
419 usage = SLAP_MR_SUBSTR_FINAL;
423 Debug( LDAP_DEBUG_FILTER,
424 " unknown substring choice=%ld\n",
427 rc = LDAP_PROTOCOL_ERROR;
431 /* validate/normalize using equality matching rule validator! */
432 rc = asserted_value_validate_normalize(
433 ssa.sa_desc, ssa.sa_desc->ad_type->sat_equality,
434 usage, &value, &nvalue, text, op->o_tmpmemctx );
435 if( rc != LDAP_SUCCESS ) goto return_error;
438 case LDAP_SUBSTRING_INITIAL:
439 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
440 ssa.sa_initial = nvalue;
443 case LDAP_SUBSTRING_ANY:
444 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
445 ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx );
448 case LDAP_SUBSTRING_FINAL:
449 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
450 ssa.sa_final = nvalue;
455 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
456 rc = LDAP_PROTOCOL_ERROR;
459 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
461 slap_sl_free( ssa.sa_initial.bv_val, op->o_tmpmemctx );
462 ber_bvarray_free_x( ssa.sa_any, op->o_tmpmemctx );
463 slap_sl_free( ssa.sa_final.bv_val, op->o_tmpmemctx );
470 if( rc == LDAP_SUCCESS ) {
471 *out = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
475 Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
476 return rc /* LDAP_SUCCESS */ ;
480 filter_free_x( Operation *op, Filter *f )
488 switch ( f->f_choice ) {
489 case LDAP_FILTER_PRESENT:
492 case LDAP_FILTER_EQUALITY:
495 case LDAP_FILTER_APPROX:
496 ava_free( op, f->f_ava, 1 );
499 case LDAP_FILTER_SUBSTRINGS:
500 if ( f->f_sub_initial.bv_val != NULL ) {
501 op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx );
503 ber_bvarray_free_x( f->f_sub_any, op->o_tmpmemctx );
504 if ( f->f_sub_final.bv_val != NULL ) {
505 op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx );
507 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
510 case LDAP_FILTER_AND:
512 case LDAP_FILTER_NOT:
513 for ( p = f->f_list; p != NULL; p = next ) {
515 filter_free_x( op, p );
519 case LDAP_FILTER_EXT:
520 mra_free( op, f->f_mra, 1 );
523 case SLAPD_FILTER_COMPUTED:
527 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
532 op->o_tmpfree( f, op->o_tmpmemctx );
536 filter_free( Filter *f )
542 op.o_tmpmemctx = slap_sl_context( f );
543 op.o_tmpmfuncs = &slap_sl_mfuncs;
544 filter_free_x( &op, f );
548 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
554 ber_bvfalse = BER_BVC( "(?=false)" ),
555 ber_bvtrue = BER_BVC( "(?=true)" ),
556 ber_bvundefined = BER_BVC( "(?=undefined)" ),
557 ber_bverror = BER_BVC( "(?=error)" ),
558 ber_bvunknown = BER_BVC( "(?=unknown)" ),
559 ber_bvnone = BER_BVC( "(?=none)" );
563 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
567 switch ( f->f_choice ) {
568 case LDAP_FILTER_EQUALITY:
569 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
571 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
572 tmp.bv_len + ( sizeof("(=)") - 1 );
573 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
575 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
576 f->f_av_desc->ad_cname.bv_val,
579 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
583 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
585 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
586 tmp.bv_len + ( sizeof("(>=)") - 1 );
587 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
589 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
590 f->f_av_desc->ad_cname.bv_val,
593 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
597 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
599 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
600 tmp.bv_len + ( sizeof("(<=)") - 1 );
601 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
603 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
604 f->f_av_desc->ad_cname.bv_val,
607 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
610 case LDAP_FILTER_APPROX:
611 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
613 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
614 tmp.bv_len + ( sizeof("(~=)") - 1 );
615 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
617 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
618 f->f_av_desc->ad_cname.bv_val,
620 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
623 case LDAP_FILTER_SUBSTRINGS:
624 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
625 ( sizeof("(=*)") - 1 );
626 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
628 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
629 f->f_sub_desc->ad_cname.bv_val );
631 if ( f->f_sub_initial.bv_val != NULL ) {
634 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
636 fstr->bv_len += tmp.bv_len;
637 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
640 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
641 /* "(attr=" */ "%s*)",
644 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
647 if ( f->f_sub_any != NULL ) {
648 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
650 filter_escape_value_x( &f->f_sub_any[i],
651 &tmp, op->o_tmpmemctx );
653 fstr->bv_len += tmp.bv_len + 1;
654 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
657 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
658 /* "(attr=[init]*[any*]" */ "%s*)",
660 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
664 if ( f->f_sub_final.bv_val != NULL ) {
667 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
669 fstr->bv_len += tmp.bv_len;
670 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
673 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
674 /* "(attr=[init*][any*]" */ "%s)",
677 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
682 case LDAP_FILTER_PRESENT:
683 fstr->bv_len = f->f_desc->ad_cname.bv_len +
684 ( sizeof("(=*)") - 1 );
685 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
687 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
688 f->f_desc->ad_cname.bv_val );
691 case LDAP_FILTER_AND:
693 case LDAP_FILTER_NOT:
694 fstr->bv_len = sizeof("(%)") - 1;
695 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
697 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
698 f->f_choice == LDAP_FILTER_AND ? '&' :
699 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
701 for ( p = f->f_list; p != NULL; p = p->f_next ) {
704 filter2bv_x( op, p, &tmp );
706 fstr->bv_len += tmp.bv_len;
707 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
710 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
711 /*"("*/ "%s)", tmp.bv_val );
713 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
718 case LDAP_FILTER_EXT: {
720 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
722 if ( f->f_mr_desc ) {
723 ad = f->f_mr_desc->ad_cname;
729 fstr->bv_len = ad.bv_len +
730 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
731 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
732 tmp.bv_len + ( sizeof("(:=)") - 1 );
733 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
735 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
737 f->f_mr_dnattrs ? ":dn" : "",
738 f->f_mr_rule_text.bv_len ? ":" : "",
739 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
741 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
744 case SLAPD_FILTER_COMPUTED:
745 switch ( f->f_result ) {
746 case LDAP_COMPARE_FALSE:
750 case LDAP_COMPARE_TRUE:
754 case SLAPD_COMPARE_UNDEFINED:
755 tmp = ber_bvundefined;
763 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
767 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
773 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(
792 assert( in != NULL );
793 assert( out != NULL );
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 err = slap_bv2undef_ad( &type, &vrf.vrf_desc, text,
894 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
896 if( err != LDAP_SUCCESS ) {
897 /* unrecognized attribute description or other error */
898 Debug( LDAP_DEBUG_ANY,
899 "get_simple_vrFilter: conn %lu unknown "
900 "attribute type=%s (%d)\n",
901 op->o_connid, type.bv_val, err );
903 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
904 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 );