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(
45 SubstringsAssertion **s,
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.f_ava, 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.f_sub, 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.f_ava, 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.f_ava, 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 err = slap_bv2undef_ad( &type, &f.f_desc, text,
170 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
172 if ( err != LDAP_SUCCESS ) {
173 /* unrecognized attribute description or other error */
174 Debug( LDAP_DEBUG_ANY,
175 "get_filter: conn %lu unknown attribute "
177 op->o_connid, type.bv_val, err );
179 f.f_choice = SLAPD_FILTER_COMPUTED;
180 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 rc = slap_bv2undef_ad( &desc, &ssa.sa_desc, text,
352 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
354 if( rc != LDAP_SUCCESS ) {
355 Debug( LDAP_DEBUG_ANY,
356 "get_ssa: conn %lu unknown attribute type=%s (%ld)\n",
357 op->o_connid, desc.bv_val, (long) rc );
359 /* skip over the rest of this filter */
360 for ( tag = ber_first_element( ber, &len, &last );
362 tag = ber_next_element( ber, &len, last ) ) {
363 ber_scanf( ber, "x" );
369 rc = LDAP_PROTOCOL_ERROR;
371 for ( tag = ber_first_element( ber, &len, &last );
373 tag = ber_next_element( ber, &len, last ) )
377 rc = ber_scanf( ber, "m", &value );
378 if ( rc == LBER_ERROR ) {
379 rc = SLAPD_DISCONNECT;
383 if ( value.bv_val == NULL || value.bv_len == 0 ) {
384 rc = LDAP_INVALID_SYNTAX;
389 case LDAP_SUBSTRING_INITIAL:
390 if ( ssa.sa_initial.bv_val != NULL
391 || ssa.sa_any != NULL
392 || ssa.sa_final.bv_val != NULL )
394 rc = LDAP_PROTOCOL_ERROR;
397 usage = SLAP_MR_SUBSTR_INITIAL;
400 case LDAP_SUBSTRING_ANY:
401 if ( ssa.sa_final.bv_val != NULL ) {
402 rc = LDAP_PROTOCOL_ERROR;
405 usage = SLAP_MR_SUBSTR_ANY;
408 case LDAP_SUBSTRING_FINAL:
409 if ( ssa.sa_final.bv_val != NULL ) {
410 rc = LDAP_PROTOCOL_ERROR;
414 usage = SLAP_MR_SUBSTR_FINAL;
418 Debug( LDAP_DEBUG_FILTER,
419 " unknown substring choice=%ld\n",
422 rc = LDAP_PROTOCOL_ERROR;
426 /* validate/normalize using equality matching rule validator! */
427 rc = asserted_value_validate_normalize(
428 ssa.sa_desc, ssa.sa_desc->ad_type->sat_equality,
429 usage, &value, &nvalue, text, op->o_tmpmemctx );
430 if( rc != LDAP_SUCCESS ) goto return_error;
433 case LDAP_SUBSTRING_INITIAL:
434 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
435 ssa.sa_initial = nvalue;
438 case LDAP_SUBSTRING_ANY:
439 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
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 );
445 ssa.sa_final = nvalue;
450 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
451 rc = LDAP_PROTOCOL_ERROR;
454 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
456 slap_sl_free( ssa.sa_initial.bv_val, op->o_tmpmemctx );
457 ber_bvarray_free_x( ssa.sa_any, op->o_tmpmemctx );
458 slap_sl_free( ssa.sa_final.bv_val, op->o_tmpmemctx );
465 if( rc == LDAP_SUCCESS ) {
466 *out = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
470 Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
471 return rc /* LDAP_SUCCESS */ ;
475 filter_free_x( Operation *op, Filter *f )
483 switch ( f->f_choice ) {
484 case LDAP_FILTER_PRESENT:
487 case LDAP_FILTER_EQUALITY:
490 case LDAP_FILTER_APPROX:
491 ava_free( op, f->f_ava, 1 );
494 case LDAP_FILTER_SUBSTRINGS:
495 if ( f->f_sub_initial.bv_val != NULL ) {
496 op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx );
498 ber_bvarray_free_x( f->f_sub_any, op->o_tmpmemctx );
499 if ( f->f_sub_final.bv_val != NULL ) {
500 op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx );
502 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
505 case LDAP_FILTER_AND:
507 case LDAP_FILTER_NOT:
508 for ( p = f->f_list; p != NULL; p = next ) {
510 filter_free_x( op, p );
514 case LDAP_FILTER_EXT:
515 mra_free( op, f->f_mra, 1 );
518 case SLAPD_FILTER_COMPUTED:
522 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
527 op->o_tmpfree( f, op->o_tmpmemctx );
531 filter_free( Filter *f )
537 op.o_tmpmemctx = slap_sl_context( f );
538 op.o_tmpmfuncs = &slap_sl_mfuncs;
539 filter_free_x( &op, f );
543 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
549 ber_bvfalse = BER_BVC( "(?=false)" ),
550 ber_bvtrue = BER_BVC( "(?=true)" ),
551 ber_bvundefined = BER_BVC( "(?=undefined)" ),
552 ber_bverror = BER_BVC( "(?=error)" ),
553 ber_bvunknown = BER_BVC( "(?=unknown)" ),
554 ber_bvnone = BER_BVC( "(?=none)" );
558 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
562 switch ( f->f_choice ) {
563 case LDAP_FILTER_EQUALITY:
564 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
566 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
567 tmp.bv_len + ( sizeof("(=)") - 1 );
568 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
570 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
571 f->f_av_desc->ad_cname.bv_val,
572 tmp.bv_len ? tmp.bv_val : "(null)");
574 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
578 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
580 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
581 tmp.bv_len + ( sizeof("(>=)") - 1 );
582 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
584 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
585 f->f_av_desc->ad_cname.bv_val,
586 tmp.bv_len ? tmp.bv_val : "(null)");
588 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
592 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
594 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
595 tmp.bv_len + ( sizeof("(<=)") - 1 );
596 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
598 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
599 f->f_av_desc->ad_cname.bv_val,
600 tmp.bv_len ? tmp.bv_val : "(null)");
602 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
605 case LDAP_FILTER_APPROX:
606 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
608 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
609 tmp.bv_len + ( sizeof("(~=)") - 1 );
610 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
612 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
613 f->f_av_desc->ad_cname.bv_val,
614 tmp.bv_len ? tmp.bv_val : "(null)");
615 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
618 case LDAP_FILTER_SUBSTRINGS:
619 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
620 ( sizeof("(=*)") - 1 );
621 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
623 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
624 f->f_sub_desc->ad_cname.bv_val );
626 if ( f->f_sub_initial.bv_val != NULL ) {
629 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
631 fstr->bv_len += tmp.bv_len;
632 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
635 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
636 /* "(attr=" */ "%s*)",
637 tmp.bv_len ? tmp.bv_val : "(null)");
639 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
642 if ( f->f_sub_any != NULL ) {
643 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
645 filter_escape_value_x( &f->f_sub_any[i],
646 &tmp, op->o_tmpmemctx );
648 fstr->bv_len += tmp.bv_len + 1;
649 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
652 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
653 /* "(attr=[init]*[any*]" */ "%s*)",
654 tmp.bv_len ? tmp.bv_val : "(null)");
655 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
659 if ( f->f_sub_final.bv_val != NULL ) {
662 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
664 fstr->bv_len += tmp.bv_len;
665 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
668 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
669 /* "(attr=[init*][any*]" */ "%s)",
670 tmp.bv_len ? tmp.bv_val : "(null)");
672 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
677 case LDAP_FILTER_PRESENT:
678 fstr->bv_len = f->f_desc->ad_cname.bv_len +
679 ( sizeof("(=*)") - 1 );
680 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
682 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
683 f->f_desc->ad_cname.bv_val );
686 case LDAP_FILTER_AND:
688 case LDAP_FILTER_NOT:
689 fstr->bv_len = sizeof("(%)") - 1;
690 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
692 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
693 f->f_choice == LDAP_FILTER_AND ? '&' :
694 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
696 for ( p = f->f_list; p != NULL; p = p->f_next ) {
699 filter2bv_x( op, p, &tmp );
701 fstr->bv_len += tmp.bv_len;
702 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
705 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
706 /*"("*/ "%s)", tmp.bv_val );
708 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
713 case LDAP_FILTER_EXT: {
715 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
717 if ( f->f_mr_desc ) {
718 ad = f->f_mr_desc->ad_cname;
724 fstr->bv_len = ad.bv_len +
725 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
726 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
727 tmp.bv_len + ( sizeof("(:=)") - 1 );
728 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
730 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
732 f->f_mr_dnattrs ? ":dn" : "",
733 f->f_mr_rule_text.bv_len ? ":" : "",
734 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
735 tmp.bv_len ? tmp.bv_val : "(null)");
736 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
739 case SLAPD_FILTER_COMPUTED:
740 switch ( f->f_result ) {
741 case LDAP_COMPARE_FALSE:
745 case LDAP_COMPARE_TRUE:
749 case SLAPD_COMPARE_UNDEFINED:
750 tmp = ber_bvundefined;
758 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
762 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
768 filter2bv( Filter *f, struct berval *fstr )
774 op.o_tmpmemctx = NULL;
775 op.o_tmpmfuncs = &ch_mfuncs;
777 filter2bv_x( &op, f, fstr );
784 ValuesReturnFilter **filt,
790 ValuesReturnFilter vrf;
792 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
794 tag = ber_peek_tag( ber, &len );
796 if( tag == LBER_ERROR ) {
797 *text = "error decoding filter";
798 return SLAPD_DISCONNECT;
804 vrf.vrf_choice = tag;
806 switch ( vrf.vrf_choice ) {
807 case LDAP_FILTER_EQUALITY:
808 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
809 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
810 if ( err != LDAP_SUCCESS ) {
814 assert( vrf.vrf_ava != NULL );
817 case LDAP_FILTER_SUBSTRINGS:
818 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
819 err = get_ssa( op, ber, &vrf.vrf_sub, text );
823 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
824 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
825 if ( err != LDAP_SUCCESS ) {
831 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
832 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
833 if ( err != LDAP_SUCCESS ) {
838 case LDAP_FILTER_PRESENT: {
841 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
842 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
843 err = SLAPD_DISCONNECT;
844 *text = "error decoding filter";
849 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
851 if( err != LDAP_SUCCESS ) {
852 err = slap_bv2undef_ad( &type, &vrf.vrf_desc, text,
853 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
855 if( err != LDAP_SUCCESS ) {
856 /* unrecognized attribute description or other error */
857 Debug( LDAP_DEBUG_ANY,
858 "get_simple_vrFilter: conn %lu unknown "
859 "attribute type=%s (%d)\n",
860 op->o_connid, type.bv_val, err );
862 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
863 vrf.vrf_result = LDAP_COMPARE_FALSE;
870 case LDAP_FILTER_APPROX:
871 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
872 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
873 if ( err != LDAP_SUCCESS ) {
878 case LDAP_FILTER_EXT:
879 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
881 err = get_mra( op, ber, &vrf.vrf_mra, text );
882 if ( err != LDAP_SUCCESS ) {
886 assert( vrf.vrf_mra != NULL );
890 (void) ber_scanf( ber, "x" ); /* skip the element */
891 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
892 vrf.vrf_choice, 0, 0 );
893 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
894 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
898 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
900 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
901 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
905 if ( err == LDAP_SUCCESS ) {
906 *filt = ch_malloc( sizeof vrf );
910 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
916 get_vrFilter( Operation *op, BerElement *ber,
917 ValuesReturnFilter **vrf,
921 * A ValuesReturnFilter looks like this:
923 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
924 * SimpleFilterItem ::= CHOICE {
925 * equalityMatch [3] AttributeValueAssertion,
926 * substrings [4] SubstringFilter,
927 * greaterOrEqual [5] AttributeValueAssertion,
928 * lessOrEqual [6] AttributeValueAssertion,
929 * present [7] AttributeType,
930 * approxMatch [8] AttributeValueAssertion,
931 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
934 * SubstringFilter ::= SEQUENCE {
935 * type AttributeType,
936 * SEQUENCE OF CHOICE {
937 * initial [0] IA5String,
939 * final [2] IA5String
943 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
944 * matchingRule [1] MatchingRuleId OPTIONAL,
945 * type [2] AttributeDescription OPTIONAL,
946 * matchValue [3] AssertionValue }
949 ValuesReturnFilter **n;
954 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
956 tag = ber_peek_tag( ber, &len );
958 if( tag == LBER_ERROR ) {
959 *text = "error decoding vrFilter";
960 return SLAPD_DISCONNECT;
963 if( tag != LBER_SEQUENCE ) {
964 *text = "error decoding vrFilter, expect SEQUENCE tag";
965 return SLAPD_DISCONNECT;
969 for ( tag = ber_first_element( ber, &len, &last );
971 tag = ber_next_element( ber, &len, last ) )
973 int err = get_simple_vrFilter( op, ber, n, text );
975 if ( err != LDAP_SUCCESS ) return( err );
981 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
982 return( LDAP_SUCCESS );
986 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
988 ValuesReturnFilter *p, *next;
994 for ( p = vrf; p != NULL; p = next ) {
997 switch ( vrf->vrf_choice ) {
998 case LDAP_FILTER_PRESENT:
1001 case LDAP_FILTER_EQUALITY:
1002 case LDAP_FILTER_GE:
1003 case LDAP_FILTER_LE:
1004 case LDAP_FILTER_APPROX:
1005 ava_free( op, vrf->vrf_ava, 1 );
1008 case LDAP_FILTER_SUBSTRINGS:
1009 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1010 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1012 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1013 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1014 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1016 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1019 case LDAP_FILTER_EXT:
1020 mra_free( op, vrf->vrf_mra, 1 );
1023 case SLAPD_FILTER_COMPUTED:
1027 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1028 vrf->vrf_choice, 0, 0 );
1032 op->o_tmpfree( vrf, op->o_tmpmemctx );
1037 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1039 ValuesReturnFilter *p;
1043 if ( vrf == NULL ) {
1044 ber_str2bv_x( "No filter!", sizeof("No filter!")-1,
1045 1, fstr, op->o_tmpmemctx );
1049 fstr->bv_len = sizeof("()") - 1;
1050 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1052 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1054 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1057 simple_vrFilter2bv( op, p, &tmp );
1059 fstr->bv_len += tmp.bv_len;
1060 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1063 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1064 /*"("*/ "%s)", tmp.bv_val );
1066 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1071 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1076 if ( vrf == NULL ) {
1077 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr,
1082 switch ( vrf->vrf_choice ) {
1083 case LDAP_FILTER_EQUALITY:
1084 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1086 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1087 tmp.bv_len + ( sizeof("(=)") - 1 );
1088 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1090 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1091 vrf->vrf_av_desc->ad_cname.bv_val,
1094 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1097 case LDAP_FILTER_GE:
1098 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1100 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1101 tmp.bv_len + ( sizeof("(>=)") - 1 );
1102 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1104 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1105 vrf->vrf_av_desc->ad_cname.bv_val,
1108 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1111 case LDAP_FILTER_LE:
1112 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1114 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1115 tmp.bv_len + ( sizeof("(<=)") - 1 );
1116 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1118 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1119 vrf->vrf_av_desc->ad_cname.bv_val,
1122 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1125 case LDAP_FILTER_APPROX:
1126 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1128 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1129 tmp.bv_len + ( sizeof("(~=)") - 1 );
1130 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1132 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1133 vrf->vrf_av_desc->ad_cname.bv_val,
1135 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1138 case LDAP_FILTER_SUBSTRINGS:
1139 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1140 ( sizeof("(=*)") - 1 );
1141 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1143 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1144 vrf->vrf_sub_desc->ad_cname.bv_val );
1146 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1149 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1151 fstr->bv_len += tmp.bv_len;
1152 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1155 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1156 /* "(attr=" */ "%s*)",
1159 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1162 if ( vrf->vrf_sub_any != NULL ) {
1164 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1166 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1169 fstr->bv_len += tmp.bv_len + 1;
1170 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1171 fstr->bv_len + 1, op->o_tmpmemctx );
1173 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1174 /* "(attr=[init]*[any*]" */ "%s*)",
1176 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1180 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1183 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1185 fstr->bv_len += tmp.bv_len;
1186 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1189 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1190 /* "(attr=[init*][any*]" */ "%s)",
1193 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1198 case LDAP_FILTER_PRESENT:
1199 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1200 ( sizeof("(=*)") - 1 );
1201 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1203 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1204 vrf->vrf_desc->ad_cname.bv_val );
1207 case LDAP_FILTER_EXT: {
1209 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1211 if ( vrf->vrf_mr_desc ) {
1212 ad = vrf->vrf_mr_desc->ad_cname;
1218 fstr->bv_len = ad.bv_len +
1219 ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1220 ( vrf->vrf_mr_rule_text.bv_len
1221 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1222 tmp.bv_len + ( sizeof("(:=)") - 1 );
1223 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1225 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1227 vrf->vrf_mr_dnattrs ? ":dn" : "",
1228 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1229 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1232 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1235 case SLAPD_FILTER_COMPUTED:
1237 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1238 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1239 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1240 ? "(?=undefined)" : "(?=error)",
1241 vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1242 vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1243 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1244 ? sizeof("(?=undefined)")-1 : sizeof("(?=error)")-1,
1245 1, fstr, op->o_tmpmemctx );
1249 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1,
1250 1, fstr, op->o_tmpmemctx );