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 );
781 filter_dup( Filter *f, void *memctx )
783 BerMemoryFunctions *mf = &slap_sl_mfuncs;
789 n = mf->bmf_malloc( sizeof(Filter), memctx );
790 n->f_choice = f->f_choice;
793 switch( f->f_choice ) {
794 case SLAPD_FILTER_COMPUTED:
795 n->f_result = f->f_result;
797 case LDAP_FILTER_PRESENT:
798 n->f_desc = f->f_desc;
800 case LDAP_FILTER_EQUALITY:
803 case LDAP_FILTER_APPROX:
804 /* Should this be ava_dup() ? */
805 n->f_ava = mf->bmf_calloc( 1, sizeof(AttributeAssertion), memctx );
806 *n->f_ava = *f->f_ava;
807 ber_dupbv_x( &n->f_av_value, &f->f_av_value, memctx );
809 case LDAP_FILTER_SUBSTRINGS:
810 n->f_sub = mf->bmf_calloc( 1, sizeof(SubstringsAssertion), memctx );
811 n->f_sub_desc = f->f_sub_desc;
812 if ( !BER_BVISNULL( &f->f_sub_initial ))
813 ber_dupbv_x( &n->f_sub_initial, &f->f_sub_initial, memctx );
814 if ( f->f_sub_any ) {
816 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ );
817 n->f_sub_any = mf->bmf_malloc(( i+1 )*sizeof( struct berval ),
819 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
820 ber_dupbv_x( &n->f_sub_any[i], &f->f_sub_any[i], memctx );
822 BER_BVZERO( &n->f_sub_any[i] );
824 if ( !BER_BVISNULL( &f->f_sub_final ))
825 ber_dupbv_x( &n->f_sub_final, &f->f_sub_final, memctx );
827 case LDAP_FILTER_EXT: {
828 /* Should this be mra_dup() ? */
830 length = sizeof(MatchingRuleAssertion);
831 if ( !BER_BVISNULL( &f->f_mr_rule_text ))
832 length += f->f_mr_rule_text.bv_len + 1;
833 n->f_mra = mf->bmf_calloc( 1, length, memctx );
834 *n->f_mra = *f->f_mra;
835 ber_dupbv_x( &n->f_mr_value, &f->f_mr_value, memctx );
836 if ( !BER_BVISNULL( &f->f_mr_rule_text )) {
837 n->f_mr_rule_text.bv_val = (char *)(n->f_mra+1);
838 AC_MEMCPY(n->f_mr_rule_text.bv_val,
839 f->f_mr_rule_text.bv_val, f->f_mr_rule_text.bv_len );
842 case LDAP_FILTER_AND:
844 case LDAP_FILTER_NOT: {
846 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
847 *p = filter_dup( f, memctx );
859 ValuesReturnFilter **filt,
865 ValuesReturnFilter vrf;
867 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
869 tag = ber_peek_tag( ber, &len );
871 if( tag == LBER_ERROR ) {
872 *text = "error decoding filter";
873 return SLAPD_DISCONNECT;
879 vrf.vrf_choice = tag;
881 switch ( vrf.vrf_choice ) {
882 case LDAP_FILTER_EQUALITY:
883 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
884 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
885 if ( err != LDAP_SUCCESS ) {
889 assert( vrf.vrf_ava != NULL );
892 case LDAP_FILTER_SUBSTRINGS:
893 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
894 err = get_ssa( op, ber, &vrf.vrf_sub, text );
898 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
899 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
900 if ( err != LDAP_SUCCESS ) {
906 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
907 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
908 if ( err != LDAP_SUCCESS ) {
913 case LDAP_FILTER_PRESENT: {
916 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
917 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
918 err = SLAPD_DISCONNECT;
919 *text = "error decoding filter";
924 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
926 if( err != LDAP_SUCCESS ) {
927 err = slap_bv2undef_ad( &type, &vrf.vrf_desc, text,
928 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
930 if( err != LDAP_SUCCESS ) {
931 /* unrecognized attribute description or other error */
932 Debug( LDAP_DEBUG_ANY,
933 "get_simple_vrFilter: conn %lu unknown "
934 "attribute type=%s (%d)\n",
935 op->o_connid, type.bv_val, err );
937 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
938 vrf.vrf_result = LDAP_COMPARE_FALSE;
945 case LDAP_FILTER_APPROX:
946 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
947 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
948 if ( err != LDAP_SUCCESS ) {
953 case LDAP_FILTER_EXT:
954 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
956 err = get_mra( op, ber, &vrf.vrf_mra, text );
957 if ( err != LDAP_SUCCESS ) {
961 assert( vrf.vrf_mra != NULL );
965 (void) ber_scanf( ber, "x" ); /* skip the element */
966 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
967 vrf.vrf_choice, 0, 0 );
968 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
969 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
973 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
975 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
976 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
980 if ( err == LDAP_SUCCESS ) {
981 *filt = ch_malloc( sizeof vrf );
985 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
991 get_vrFilter( Operation *op, BerElement *ber,
992 ValuesReturnFilter **vrf,
996 * A ValuesReturnFilter looks like this:
998 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
999 * SimpleFilterItem ::= CHOICE {
1000 * equalityMatch [3] AttributeValueAssertion,
1001 * substrings [4] SubstringFilter,
1002 * greaterOrEqual [5] AttributeValueAssertion,
1003 * lessOrEqual [6] AttributeValueAssertion,
1004 * present [7] AttributeType,
1005 * approxMatch [8] AttributeValueAssertion,
1006 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1009 * SubstringFilter ::= SEQUENCE {
1010 * type AttributeType,
1011 * SEQUENCE OF CHOICE {
1012 * initial [0] IA5String,
1013 * any [1] IA5String,
1014 * final [2] IA5String
1018 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1019 * matchingRule [1] MatchingRuleId OPTIONAL,
1020 * type [2] AttributeDescription OPTIONAL,
1021 * matchValue [3] AssertionValue }
1024 ValuesReturnFilter **n;
1029 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1031 tag = ber_peek_tag( ber, &len );
1033 if( tag == LBER_ERROR ) {
1034 *text = "error decoding vrFilter";
1035 return SLAPD_DISCONNECT;
1038 if( tag != LBER_SEQUENCE ) {
1039 *text = "error decoding vrFilter, expect SEQUENCE tag";
1040 return SLAPD_DISCONNECT;
1044 for ( tag = ber_first_element( ber, &len, &last );
1045 tag != LBER_DEFAULT;
1046 tag = ber_next_element( ber, &len, last ) )
1048 int err = get_simple_vrFilter( op, ber, n, text );
1050 if ( err != LDAP_SUCCESS ) return( err );
1052 n = &(*n)->vrf_next;
1056 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1057 return( LDAP_SUCCESS );
1061 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1063 ValuesReturnFilter *p, *next;
1065 if ( vrf == NULL ) {
1069 for ( p = vrf; p != NULL; p = next ) {
1072 switch ( vrf->vrf_choice ) {
1073 case LDAP_FILTER_PRESENT:
1076 case LDAP_FILTER_EQUALITY:
1077 case LDAP_FILTER_GE:
1078 case LDAP_FILTER_LE:
1079 case LDAP_FILTER_APPROX:
1080 ava_free( op, vrf->vrf_ava, 1 );
1083 case LDAP_FILTER_SUBSTRINGS:
1084 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1085 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1087 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1088 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1089 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1091 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1094 case LDAP_FILTER_EXT:
1095 mra_free( op, vrf->vrf_mra, 1 );
1098 case SLAPD_FILTER_COMPUTED:
1102 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1103 vrf->vrf_choice, 0, 0 );
1107 op->o_tmpfree( vrf, op->o_tmpmemctx );
1112 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1114 ValuesReturnFilter *p;
1118 if ( vrf == NULL ) {
1119 ber_str2bv_x( "No filter!", sizeof("No filter!")-1,
1120 1, fstr, op->o_tmpmemctx );
1124 fstr->bv_len = sizeof("()") - 1;
1125 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1127 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1129 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1132 simple_vrFilter2bv( op, p, &tmp );
1134 fstr->bv_len += tmp.bv_len;
1135 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1138 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1139 /*"("*/ "%s)", tmp.bv_val );
1141 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1146 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1151 if ( vrf == NULL ) {
1152 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr,
1157 switch ( vrf->vrf_choice ) {
1158 case LDAP_FILTER_EQUALITY:
1159 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1161 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1162 tmp.bv_len + ( sizeof("(=)") - 1 );
1163 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1165 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1166 vrf->vrf_av_desc->ad_cname.bv_val,
1169 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1172 case LDAP_FILTER_GE:
1173 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1175 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1176 tmp.bv_len + ( sizeof("(>=)") - 1 );
1177 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1179 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1180 vrf->vrf_av_desc->ad_cname.bv_val,
1183 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1186 case LDAP_FILTER_LE:
1187 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1189 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1190 tmp.bv_len + ( sizeof("(<=)") - 1 );
1191 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1193 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1194 vrf->vrf_av_desc->ad_cname.bv_val,
1197 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1200 case LDAP_FILTER_APPROX:
1201 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1203 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1204 tmp.bv_len + ( sizeof("(~=)") - 1 );
1205 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1207 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1208 vrf->vrf_av_desc->ad_cname.bv_val,
1210 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1213 case LDAP_FILTER_SUBSTRINGS:
1214 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1215 ( sizeof("(=*)") - 1 );
1216 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1218 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1219 vrf->vrf_sub_desc->ad_cname.bv_val );
1221 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1224 filter_escape_value_x( &vrf->vrf_sub_initial, &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-2], tmp.bv_len+3,
1231 /* "(attr=" */ "%s*)",
1234 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1237 if ( vrf->vrf_sub_any != NULL ) {
1239 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1241 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1244 fstr->bv_len += tmp.bv_len + 1;
1245 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1246 fstr->bv_len + 1, op->o_tmpmemctx );
1248 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1249 /* "(attr=[init]*[any*]" */ "%s*)",
1251 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1255 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1258 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1260 fstr->bv_len += tmp.bv_len;
1261 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1264 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1265 /* "(attr=[init*][any*]" */ "%s)",
1268 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1273 case LDAP_FILTER_PRESENT:
1274 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1275 ( sizeof("(=*)") - 1 );
1276 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1278 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1279 vrf->vrf_desc->ad_cname.bv_val );
1282 case LDAP_FILTER_EXT: {
1284 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1286 if ( vrf->vrf_mr_desc ) {
1287 ad = vrf->vrf_mr_desc->ad_cname;
1293 fstr->bv_len = ad.bv_len +
1294 ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1295 ( vrf->vrf_mr_rule_text.bv_len
1296 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1297 tmp.bv_len + ( sizeof("(:=)") - 1 );
1298 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1300 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1302 vrf->vrf_mr_dnattrs ? ":dn" : "",
1303 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1304 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1307 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1310 case SLAPD_FILTER_COMPUTED:
1312 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1313 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1314 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1315 ? "(?=undefined)" : "(?=error)",
1316 vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1317 vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1318 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1319 ? sizeof("(?=undefined)")-1 : sizeof("(?=error)")-1,
1320 1, fstr, op->o_tmpmemctx );
1324 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1,
1325 1, fstr, op->o_tmpmemctx );