1 /* filter.c - routines for parsing and dealing with filters */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
17 static int get_filter_list(
26 SubstringsAssertion **s,
29 static void simple_vrFilter2bv(
31 ValuesReturnFilter *f,
32 struct berval *fstr );
34 static int get_simple_vrFilter(
37 ValuesReturnFilter **f,
53 LDAP_LOG( FILTER, ENTRY, "get_filter: conn %d\n", op->o_connid, 0, 0 );
55 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
58 * A filter looks like this coming in:
60 * and [0] SET OF Filter,
61 * or [1] SET OF Filter,
63 * equalityMatch [3] AttributeValueAssertion,
64 * substrings [4] SubstringFilter,
65 * greaterOrEqual [5] AttributeValueAssertion,
66 * lessOrEqual [6] AttributeValueAssertion,
67 * present [7] AttributeType,,
68 * approxMatch [8] AttributeValueAssertion
69 * extensibleMatch [9] MatchingRuleAssertion
72 * SubstringFilter ::= SEQUENCE {
74 * SEQUENCE OF CHOICE {
75 * initial [0] IA5String,
81 * MatchingRuleAssertion ::= SEQUENCE {
82 * matchingRule [1] MatchingRuleId OPTIONAL,
83 * type [2] AttributeDescription OPTIONAL,
84 * matchValue [3] AssertionValue,
85 * dnAttributes [4] BOOLEAN DEFAULT FALSE
90 tag = ber_peek_tag( ber, &len );
92 if( tag == LBER_ERROR ) {
93 *text = "error decoding filter";
94 return SLAPD_DISCONNECT;
102 switch ( f.f_choice ) {
103 case LDAP_FILTER_EQUALITY:
105 LDAP_LOG( FILTER, DETAIL2,
106 "get_filter: conn %d EQUALITY\n", op->o_connid, 0, 0 );
108 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
110 err = get_ava( op, ber, &f.f_ava, SLAP_MR_EQUALITY, text );
111 if ( err != LDAP_SUCCESS ) {
115 assert( f.f_ava != NULL );
118 case LDAP_FILTER_SUBSTRINGS:
120 LDAP_LOG( FILTER, DETAIL1,
121 "get_filter: conn %d SUBSTRINGS\n", op->o_connid, 0, 0 );
123 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
125 err = get_ssa( op, ber, &f.f_sub, text );
126 if( err != LDAP_SUCCESS ) {
129 assert( f.f_sub != NULL );
134 LDAP_LOG( FILTER, DETAIL1,
135 "get_filter: conn %d GE\n", op->o_connid, 0, 0 );
137 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 );
148 LDAP_LOG( FILTER, DETAIL1,
149 "get_filter: conn %d LE\n", op->o_connid, 0, 0 );
151 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: {
164 LDAP_LOG( FILTER, DETAIL1,
165 "get_filter: conn %d PRESENT\n", op->o_connid, 0, 0 );
167 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
169 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
170 err = SLAPD_DISCONNECT;
171 *text = "error decoding filter";
176 err = slap_bv2ad( &type, &f.f_desc, text );
178 if( err != LDAP_SUCCESS ) {
179 /* unrecognized attribute description or other error */
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:
192 LDAP_LOG( FILTER, DETAIL1,
193 "get_filter: conn %d APPROX\n", op->o_connid, 0, 0 );
195 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:
206 LDAP_LOG( FILTER, DETAIL1,
207 "get_filter: conn %d AND\n", op->o_connid, 0, 0 );
209 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
211 err = get_filter_list( op, ber, &f.f_and, text );
212 if ( err != LDAP_SUCCESS ) {
215 /* no assert - list could be empty */
220 LDAP_LOG( FILTER, DETAIL1,
221 "get_filter: conn %d OR\n", op->o_connid, 0, 0 );
223 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
225 err = get_filter_list( op, ber, &f.f_or, text );
226 if ( err != LDAP_SUCCESS ) {
229 /* no assert - list could be empty */
232 case LDAP_FILTER_NOT:
234 LDAP_LOG( FILTER, DETAIL1,
235 "get_filter: conn %d NOT\n", op->o_connid, 0, 0 );
237 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
239 (void) ber_skip_tag( ber, &len );
240 err = get_filter( op, ber, &f.f_not, text );
241 if ( err != LDAP_SUCCESS ) {
245 assert( f.f_not != NULL );
248 case LDAP_FILTER_EXT:
250 LDAP_LOG( FILTER, DETAIL1,
251 "get_filter: conn %d EXTENSIBLE\n", op->o_connid, 0, 0 );
253 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 */
267 LDAP_LOG( FILTER, ERR,
268 "get_filter: conn %d unknown filter type=%lu\n",
269 op->o_connid, f.f_choice, 0 );
271 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
274 f.f_choice = SLAPD_FILTER_COMPUTED;
275 f.f_result = SLAPD_COMPARE_UNDEFINED;
279 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 );
292 LDAP_LOG( FILTER, DETAIL2,
293 "get_filter: conn %d exit\n", op->o_connid, 0, 0 );
295 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
302 get_filter_list( Operation *op, BerElement *ber,
313 LDAP_LOG( FILTER, ENTRY,
314 "get_filter_list: conn %d start\n", op->o_connid, 0, 0 );
316 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
319 for ( tag = ber_first_element( ber, &len, &last );
321 tag = ber_next_element( ber, &len, last ) )
323 err = get_filter( op, ber, new, text );
324 if ( err != LDAP_SUCCESS )
326 new = &(*new)->f_next;
331 LDAP_LOG( FILTER, ENTRY,
332 "get_filter_list: conn %d exit\n", op->o_connid, 0, 0 );
334 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
336 return( LDAP_SUCCESS );
343 SubstringsAssertion **out,
349 struct berval desc, value, nvalue;
351 SubstringsAssertion ssa;
353 *text = "error decoding filter";
356 LDAP_LOG( FILTER, ENTRY,
357 "get_ssa: conn %d begin\n", op->o_connid, 0, 0 );
359 Debug( LDAP_DEBUG_FILTER, "begin get_ssa\n", 0, 0, 0 );
361 if ( ber_scanf( ber, "{m" /*}*/, &desc ) == LBER_ERROR ) {
362 return SLAPD_DISCONNECT;
368 ssa.sa_initial.bv_val = NULL;
370 ssa.sa_final.bv_val = NULL;
372 rc = slap_bv2ad( &desc, &ssa.sa_desc, text );
374 if( rc != LDAP_SUCCESS ) {
378 rc = LDAP_PROTOCOL_ERROR;
380 for ( tag = ber_first_element( ber, &len, &last );
382 tag = ber_next_element( ber, &len, last ) )
386 rc = ber_scanf( ber, "m", &value );
387 if ( rc == LBER_ERROR ) {
388 rc = SLAPD_DISCONNECT;
392 if ( value.bv_val == NULL || value.bv_len == 0 ) {
393 rc = LDAP_INVALID_SYNTAX;
398 case LDAP_SUBSTRING_INITIAL:
399 usage = SLAP_MR_SUBSTR_INITIAL;
402 case LDAP_SUBSTRING_ANY:
403 usage = SLAP_MR_SUBSTR_ANY;
406 case LDAP_SUBSTRING_FINAL:
407 usage = SLAP_MR_SUBSTR_FINAL;
411 rc = LDAP_PROTOCOL_ERROR;
414 LDAP_LOG( FILTER, ERR,
415 "get_filter_substring: conn %d unknown substring choice=%ld\n",
416 op->o_connid, (long)tag, 0 );
418 Debug( LDAP_DEBUG_FILTER,
419 " unknown substring choice=%ld\n",
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 );
431 if( rc != LDAP_SUCCESS ) {
435 rc = LDAP_PROTOCOL_ERROR;
438 case LDAP_SUBSTRING_INITIAL:
440 LDAP_LOG( FILTER, DETAIL1,
441 "get_ssa: conn %d INITIAL\n",
442 op->o_connid, 0, 0 );
444 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
447 if ( ssa.sa_initial.bv_val != NULL
448 || ssa.sa_any != NULL
449 || ssa.sa_final.bv_val != NULL )
451 free( nvalue.bv_val );
455 ssa.sa_initial = nvalue;
458 case LDAP_SUBSTRING_ANY:
460 LDAP_LOG( FILTER, DETAIL1,
461 "get_ssa: conn %d ANY\n",
462 op->o_connid, 0, 0 );
464 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
467 if ( ssa.sa_final.bv_val != NULL ) {
468 free( nvalue.bv_val );
473 ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx );
475 ber_bvarray_add( &ssa.sa_any, &nvalue );
479 case LDAP_SUBSTRING_FINAL:
481 LDAP_LOG( FILTER, DETAIL1,
482 "get_ssa: conn %d FINAL\n",
483 op->o_connid, 0, 0 );
485 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
488 if ( ssa.sa_final.bv_val != NULL ) {
489 free( nvalue.bv_val );
493 ssa.sa_final = nvalue;
498 LDAP_LOG( FILTER, INFO,
499 "get_ssa: conn %d unknown substring type %ld\n",
500 op->o_connid, (long)tag, 0 );
502 Debug( LDAP_DEBUG_FILTER,
503 " unknown substring type=%ld\n",
508 free( nvalue.bv_val );
512 LDAP_LOG( FILTER, INFO,
513 "get_ssa: conn %d error %ld\n",
514 op->o_connid, (long)rc, 0 );
516 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
519 free( ssa.sa_initial.bv_val );
520 ber_bvarray_free( ssa.sa_any );
521 free( ssa.sa_final.bv_val );
528 if( rc == LDAP_SUCCESS ) {
529 *out = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
534 LDAP_LOG( FILTER, ENTRY,
535 "get_ssa: conn %d exit\n", op->o_connid, 0, 0 );
537 Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
544 filter_free_x( Operation *op, Filter *f )
552 switch ( f->f_choice ) {
553 case LDAP_FILTER_PRESENT:
556 case LDAP_FILTER_EQUALITY:
559 case LDAP_FILTER_APPROX:
560 ava_free( op, f->f_ava, 1 );
563 case LDAP_FILTER_SUBSTRINGS:
564 if ( f->f_sub_initial.bv_val != NULL ) {
566 op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx );
568 ch_free( f->f_sub_initial.bv_val );
571 ber_bvarray_free( f->f_sub_any );
572 if ( f->f_sub_final.bv_val != NULL ) {
574 op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx );
576 ch_free( f->f_sub_final.bv_val );
579 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
582 case LDAP_FILTER_AND:
584 case LDAP_FILTER_NOT:
585 for ( p = f->f_list; p != NULL; p = next ) {
587 filter_free_x( op, p );
591 case LDAP_FILTER_EXT:
592 mra_free( op, f->f_mra, 1 );
595 case SLAPD_FILTER_COMPUTED:
600 LDAP_LOG( FILTER, ERR,
601 "filter_free: unknown filter type %lu\n", f->f_choice, 0, 0 );
603 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
609 op->o_tmpfree( f, op->o_tmpmemctx );
613 filter_free( Filter *f )
617 op.o_tmpmemctx = NULL;
618 op.o_tmpfree = (BER_MEMFREE_FN *)ch_free;
619 filter_free_x( &op, f );
623 filter2bv_x( Filter *f, struct berval *fstr, void *ctx )
629 BER_MEMALLOC_FN *alloc = ctx ? sl_malloc : (BER_MEMALLOC_FN *)ch_malloc;
630 BER_MEMREALLOC_FN *reallo = ctx ? sl_realloc : (BER_MEMREALLOC_FN *)ch_realloc;
633 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, ctx );
637 switch ( f->f_choice ) {
638 case LDAP_FILTER_EQUALITY:
639 filter_escape_value_x( &f->f_av_value, &tmp, ctx );
641 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
642 tmp.bv_len + ( sizeof("(=)") - 1 );
643 fstr->bv_val = alloc( fstr->bv_len + 1, ctx );
645 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
646 f->f_av_desc->ad_cname.bv_val,
649 ber_memfree_x( tmp.bv_val, ctx );
653 filter_escape_value_x( &f->f_av_value, &tmp, ctx );
655 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
656 tmp.bv_len + ( sizeof("(>=)") - 1 );
657 fstr->bv_val = alloc( fstr->bv_len + 1, ctx );
659 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
660 f->f_av_desc->ad_cname.bv_val,
663 ber_memfree_x( tmp.bv_val, ctx );
667 filter_escape_value_x( &f->f_av_value, &tmp, ctx );
669 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
670 tmp.bv_len + ( sizeof("(<=)") - 1 );
671 fstr->bv_val = alloc( fstr->bv_len + 1, ctx );
673 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
674 f->f_av_desc->ad_cname.bv_val,
677 ber_memfree_x( tmp.bv_val, ctx );
680 case LDAP_FILTER_APPROX:
681 filter_escape_value_x( &f->f_av_value, &tmp, ctx );
683 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
684 tmp.bv_len + ( sizeof("(~=)") - 1 );
685 fstr->bv_val = alloc( fstr->bv_len + 1, ctx );
687 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
688 f->f_av_desc->ad_cname.bv_val,
690 ber_memfree_x( tmp.bv_val, ctx );
693 case LDAP_FILTER_SUBSTRINGS:
694 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
695 ( sizeof("(=*)") - 1 );
696 fstr->bv_val = alloc( fstr->bv_len + 128, ctx );
698 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
699 f->f_sub_desc->ad_cname.bv_val );
701 if ( f->f_sub_initial.bv_val != NULL ) {
704 filter_escape_value_x( &f->f_sub_initial, &tmp, ctx );
706 fstr->bv_len += tmp.bv_len;
707 fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, ctx );
709 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
710 /* "(attr=" */ "%s*)",
713 ber_memfree_x( tmp.bv_val, ctx );
716 if ( f->f_sub_any != NULL ) {
717 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
719 filter_escape_value_x( &f->f_sub_any[i], &tmp, ctx );
721 fstr->bv_len += tmp.bv_len + 1;
722 fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, ctx );
724 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
725 /* "(attr=[init]*[any*]" */ "%s*)",
727 ber_memfree_x( tmp.bv_val, ctx );
731 if ( f->f_sub_final.bv_val != NULL ) {
734 filter_escape_value_x( &f->f_sub_final, &tmp, ctx );
736 fstr->bv_len += tmp.bv_len;
737 fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, ctx );
739 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
740 /* "(attr=[init*][any*]" */ "%s)",
743 ber_memfree_x( tmp.bv_val, ctx );
748 case LDAP_FILTER_PRESENT:
749 fstr->bv_len = f->f_desc->ad_cname.bv_len +
750 ( sizeof("(=*)") - 1 );
751 fstr->bv_val = alloc( fstr->bv_len + 1, ctx );
753 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
754 f->f_desc->ad_cname.bv_val );
757 case LDAP_FILTER_AND:
759 case LDAP_FILTER_NOT:
760 fstr->bv_len = sizeof("(%)") - 1;
761 fstr->bv_val = alloc( fstr->bv_len + 128, ctx );
763 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
764 f->f_choice == LDAP_FILTER_AND ? '&' :
765 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
767 for ( p = f->f_list; p != NULL; p = p->f_next ) {
770 filter2bv( p, &tmp );
772 fstr->bv_len += tmp.bv_len;
773 fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, ctx );
775 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
776 /*"("*/ "%s)", tmp.bv_val );
778 ch_free( tmp.bv_val );
783 case LDAP_FILTER_EXT: {
785 filter_escape_value_x( &f->f_mr_value, &tmp, ctx );
787 if ( f->f_mr_desc ) {
788 ad = f->f_mr_desc->ad_cname;
794 fstr->bv_len = ad.bv_len +
795 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
796 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
797 tmp.bv_len + ( sizeof("(:=)") - 1 );
798 fstr->bv_val = alloc( fstr->bv_len + 1, ctx );
800 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
802 f->f_mr_dnattrs ? ":dn" : "",
803 f->f_mr_rule_text.bv_len ? ":" : "",
804 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
806 ber_memfree_x( tmp.bv_val, ctx );
809 case SLAPD_FILTER_COMPUTED:
811 f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
812 f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
813 f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
815 f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
816 f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
817 f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
818 sizeof("(?=error)")-1,
823 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr, ctx );
829 filter2bv( Filter *f, struct berval *fstr )
831 filter2bv_x( f, fstr, NULL );
835 filter_escape_value_x(
844 i = in->bv_len * 3 + 1;
845 out->bv_val = ctx ? sl_malloc( i, ctx ) : ch_malloc( i );
848 for( i=0; i < in->bv_len ; i++ ) {
849 if( FILTER_ESCAPE(in->bv_val[i]) ) {
850 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
851 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
852 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
854 out->bv_val[out->bv_len++] = in->bv_val[i];
858 out->bv_val[out->bv_len] = '\0';
867 return filter_escape_value_x( in, out, NULL );
874 ValuesReturnFilter **filt,
880 ValuesReturnFilter vrf;
883 LDAP_LOG( FILTER, ENTRY,
884 "get_simple_vrFilter: conn %d\n", op->o_connid, 0, 0 );
886 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
889 tag = ber_peek_tag( ber, &len );
891 if( tag == LBER_ERROR ) {
892 *text = "error decoding filter";
893 return SLAPD_DISCONNECT;
899 vrf.vrf_choice = tag;
901 switch ( vrf.vrf_choice ) {
902 case LDAP_FILTER_EQUALITY:
904 LDAP_LOG( FILTER, DETAIL2,
905 "get_simple_vrFilter: conn %d EQUALITY\n", op->o_connid, 0, 0 );
907 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
909 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
910 if ( err != LDAP_SUCCESS ) {
914 assert( vrf.vrf_ava != NULL );
917 case LDAP_FILTER_SUBSTRINGS:
919 LDAP_LOG( FILTER, DETAIL1,
920 "get_simple_vrFilter: conn %d SUBSTRINGS\n", op->o_connid, 0, 0 );
922 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
924 err = get_ssa( op, ber, &vrf.vrf_sub, text );
929 LDAP_LOG( FILTER, DETAIL1,
930 "get_simple_vrFilter: conn %d GE\n", op->o_connid, 0, 0 );
932 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
934 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
935 if ( err != LDAP_SUCCESS ) {
942 LDAP_LOG( FILTER, DETAIL1,
943 "get_simple_vrFilter: conn %d LE\n", op->o_connid, 0, 0 );
945 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
947 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
948 if ( err != LDAP_SUCCESS ) {
953 case LDAP_FILTER_PRESENT: {
957 LDAP_LOG( FILTER, DETAIL1,
958 "get_simple_vrFilter: conn %d PRESENT\n", op->o_connid, 0, 0 );
960 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
962 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
963 err = SLAPD_DISCONNECT;
964 *text = "error decoding filter";
969 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
971 if( err != LDAP_SUCCESS ) {
972 /* unrecognized attribute description or other error */
973 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
974 vrf.vrf_result = LDAP_COMPARE_FALSE;
980 case LDAP_FILTER_APPROX:
982 LDAP_LOG( FILTER, DETAIL1,
983 "get_simple_vrFilter: conn %d APPROX\n", op->o_connid, 0, 0 );
985 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
987 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
988 if ( err != LDAP_SUCCESS ) {
993 case LDAP_FILTER_EXT:
995 LDAP_LOG( FILTER, DETAIL1,
996 "get_simple_vrFilter: conn %d EXTENSIBLE\n", op->o_connid, 0, 0 );
998 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1001 err = get_mra( op, ber, &vrf.vrf_mra, text );
1002 if ( err != LDAP_SUCCESS ) {
1006 assert( vrf.vrf_mra != NULL );
1010 (void) ber_scanf( ber, "x" ); /* skip the element */
1012 LDAP_LOG( FILTER, ERR,
1013 "get_simple_vrFilter: conn %d unknown filter type=%lu\n",
1014 op->o_connid, vrf.vrf_choice, 0 );
1016 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1017 vrf.vrf_choice, 0, 0 );
1019 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1020 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1024 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1026 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1027 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1031 if ( err == LDAP_SUCCESS ) {
1032 *filt = ch_malloc( sizeof vrf );
1037 LDAP_LOG( FILTER, DETAIL2,
1038 "get_simple_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1040 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1047 get_vrFilter( Operation *op, BerElement *ber,
1048 ValuesReturnFilter **vrf,
1052 * A ValuesReturnFilter looks like this:
1054 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1055 * SimpleFilterItem ::= CHOICE {
1056 * equalityMatch [3] AttributeValueAssertion,
1057 * substrings [4] SubstringFilter,
1058 * greaterOrEqual [5] AttributeValueAssertion,
1059 * lessOrEqual [6] AttributeValueAssertion,
1060 * present [7] AttributeType,
1061 * approxMatch [8] AttributeValueAssertion,
1062 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1065 * SubstringFilter ::= SEQUENCE {
1066 * type AttributeType,
1067 * SEQUENCE OF CHOICE {
1068 * initial [0] IA5String,
1069 * any [1] IA5String,
1070 * final [2] IA5String
1074 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1075 * matchingRule [1] MatchingRuleId OPTIONAL,
1076 * type [2] AttributeDescription OPTIONAL,
1077 * matchValue [3] AssertionValue }
1080 ValuesReturnFilter **n;
1086 LDAP_LOG( FILTER, ENTRY,
1087 "get_vrFilter: conn %d start\n", op->o_connid, 0, 0 );
1089 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1092 tag = ber_peek_tag( ber, &len );
1094 if( tag == LBER_ERROR ) {
1095 *text = "error decoding vrFilter";
1096 return SLAPD_DISCONNECT;
1099 if( tag != LBER_SEQUENCE ) {
1100 *text = "error decoding vrFilter, expect SEQUENCE tag";
1101 return SLAPD_DISCONNECT;
1105 for ( tag = ber_first_element( ber, &len, &last );
1106 tag != LBER_DEFAULT;
1107 tag = ber_next_element( ber, &len, last ) )
1109 int err = get_simple_vrFilter( op, ber, n, text );
1111 if ( err != LDAP_SUCCESS ) return( err );
1113 n = &(*n)->vrf_next;
1118 LDAP_LOG( FILTER, ENTRY,
1119 "get_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1121 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1123 return( LDAP_SUCCESS );
1127 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1129 ValuesReturnFilter *p, *next;
1131 if ( vrf == NULL ) {
1135 for ( p = vrf; p != NULL; p = next ) {
1138 switch ( vrf->vrf_choice ) {
1139 case LDAP_FILTER_PRESENT:
1142 case LDAP_FILTER_EQUALITY:
1143 case LDAP_FILTER_GE:
1144 case LDAP_FILTER_LE:
1145 case LDAP_FILTER_APPROX:
1146 ava_free( op, vrf->vrf_ava, 1 );
1149 case LDAP_FILTER_SUBSTRINGS:
1150 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1152 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1154 ch_free( vrf->vrf_sub_initial.bv_val );
1157 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1158 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1160 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1162 ch_free( vrf->vrf_sub_final.bv_val );
1165 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1168 case LDAP_FILTER_EXT:
1169 mra_free( op, vrf->vrf_mra, 1 );
1172 case SLAPD_FILTER_COMPUTED:
1177 LDAP_LOG( FILTER, ERR,
1178 "filter_free: unknown filter type %lu\n", vrf->vrf_choice, 0, 0 );
1180 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1181 vrf->vrf_choice, 0, 0 );
1186 op->o_tmpfree( vrf, op->o_tmpmemctx );
1191 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1193 ValuesReturnFilter *p;
1196 BER_MEMREALLOC_FN *reallo = op->o_tmpmemctx ? sl_realloc :
1197 (BER_MEMREALLOC_FN *)ch_realloc;
1199 if ( vrf == NULL ) {
1200 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx );
1204 fstr->bv_len = sizeof("()") - 1;
1205 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1207 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1209 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1212 simple_vrFilter2bv( op, p, &tmp );
1214 fstr->bv_len += tmp.bv_len;
1215 fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1217 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1218 /*"("*/ "%s)", tmp.bv_val );
1220 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1225 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1229 BER_MEMREALLOC_FN *reallo = op->o_tmpmemctx ? sl_realloc :
1230 (BER_MEMREALLOC_FN *) ch_realloc;
1232 if ( vrf == NULL ) {
1233 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx );
1237 switch ( vrf->vrf_choice ) {
1238 case LDAP_FILTER_EQUALITY:
1239 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1241 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1242 tmp.bv_len + ( sizeof("(=)") - 1 );
1243 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1245 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1246 vrf->vrf_av_desc->ad_cname.bv_val,
1249 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1252 case LDAP_FILTER_GE:
1253 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1255 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1256 tmp.bv_len + ( sizeof("(>=)") - 1 );
1257 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1259 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1260 vrf->vrf_av_desc->ad_cname.bv_val,
1263 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1266 case LDAP_FILTER_LE:
1267 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1269 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1270 tmp.bv_len + ( sizeof("(<=)") - 1 );
1271 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1273 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1274 vrf->vrf_av_desc->ad_cname.bv_val,
1277 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1280 case LDAP_FILTER_APPROX:
1281 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1283 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1284 tmp.bv_len + ( sizeof("(~=)") - 1 );
1285 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1287 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1288 vrf->vrf_av_desc->ad_cname.bv_val,
1290 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1293 case LDAP_FILTER_SUBSTRINGS:
1294 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1295 ( sizeof("(=*)") - 1 );
1296 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1298 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1299 vrf->vrf_sub_desc->ad_cname.bv_val );
1301 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1304 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1306 fstr->bv_len += tmp.bv_len;
1307 fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1309 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1310 /* "(attr=" */ "%s*)",
1313 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1316 if ( vrf->vrf_sub_any != NULL ) {
1318 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1320 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp, op->o_tmpmemctx );
1322 fstr->bv_len += tmp.bv_len + 1;
1323 fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1325 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1326 /* "(attr=[init]*[any*]" */ "%s*)",
1328 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1332 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1335 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1337 fstr->bv_len += tmp.bv_len;
1338 fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1340 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1341 /* "(attr=[init*][any*]" */ "%s)",
1344 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1349 case LDAP_FILTER_PRESENT:
1350 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1351 ( sizeof("(=*)") - 1 );
1352 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1354 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1355 vrf->vrf_desc->ad_cname.bv_val );
1358 case LDAP_FILTER_EXT: {
1360 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1362 if ( vrf->vrf_mr_desc ) {
1363 ad = vrf->vrf_mr_desc->ad_cname;
1369 fstr->bv_len = ad.bv_len +
1370 ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1371 ( vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1372 tmp.bv_len + ( sizeof("(:=)") - 1 );
1373 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1375 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1377 vrf->vrf_mr_dnattrs ? ":dn" : "",
1378 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1379 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1382 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1385 case SLAPD_FILTER_COMPUTED:
1387 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1388 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1389 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
1391 vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1392 vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1393 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
1394 sizeof("(?=error)")-1,
1395 1, fstr, op->o_tmpmemctx );
1399 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr, op->o_tmpmemctx );
1406 get_substring_vrFilter(
1409 ValuesReturnFilter *vrf,
1415 struct berval value;
1418 *text = "error decoding filter";
1421 LDAP_LOG( FILTER, ENTRY,
1422 "get_substring_filter: conn %d begin\n", op->o_connid, 0, 0 );
1424 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
1426 if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
1427 return SLAPD_DISCONNECT;
1430 vrf->vrf_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
1431 vrf->vrf_sub_desc = NULL;
1432 rc = slap_bv2ad( &bv, &vrf->vrf_sub_desc, text );
1434 if( rc != LDAP_SUCCESS ) {
1436 ch_free( vrf->vrf_sub );
1437 vrf->vrf_choice = SLAPD_FILTER_COMPUTED;
1438 vrf->vrf_result = SLAPD_COMPARE_UNDEFINED;
1439 return LDAP_SUCCESS;
1442 vrf->vrf_sub_initial.bv_val = NULL;
1443 vrf->vrf_sub_any = NULL;
1444 vrf->vrf_sub_final.bv_val = NULL;
1446 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
1447 tag = ber_next_element( ber, &len, last ) )
1451 rc = ber_scanf( ber, "m", &value );
1452 if ( rc == LBER_ERROR ) {
1453 rc = SLAPD_DISCONNECT;
1457 if ( value.bv_val == NULL || value.bv_len == 0 ) {
1458 rc = LDAP_INVALID_SYNTAX;
1463 case LDAP_SUBSTRING_INITIAL:
1464 usage = SLAP_MR_SUBSTR_INITIAL;
1467 case LDAP_SUBSTRING_ANY:
1468 usage = SLAP_MR_SUBSTR_ANY;
1471 case LDAP_SUBSTRING_FINAL:
1472 usage = SLAP_MR_SUBSTR_FINAL;
1476 rc = LDAP_PROTOCOL_ERROR;
1479 LDAP_LOG( FILTER, ERR,
1480 "get_filter_substring: conn %d unknown substring choice=%ld\n",
1481 op->o_connid, (long)tag, 0 );
1483 Debug( LDAP_DEBUG_FILTER,
1484 " unknown substring choice=%ld\n",
1490 /* validate/normalize using equality matching rule validator! */
1491 rc = asserted_value_validate_normalize(
1492 vrf->vrf_sub_desc, vrf->vrf_sub_desc->ad_type->sat_equality,
1493 usage, &value, &bv, text );
1494 if( rc != LDAP_SUCCESS ) {
1500 rc = LDAP_PROTOCOL_ERROR;
1503 case LDAP_SUBSTRING_INITIAL:
1505 LDAP_LOG( FILTER, DETAIL1,
1506 "get_substring_filter: conn %d INITIAL\n",
1507 op->o_connid, 0, 0 );
1509 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
1512 if ( vrf->vrf_sub_initial.bv_val != NULL
1513 || vrf->vrf_sub_any != NULL
1514 || vrf->vrf_sub_final.bv_val != NULL )
1516 free( value.bv_val );
1520 vrf->vrf_sub_initial = value;
1523 case LDAP_SUBSTRING_ANY:
1525 LDAP_LOG( FILTER, DETAIL1,
1526 "get_substring_filter: conn %d ANY\n", op->o_connid, 0, 0 );
1528 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
1531 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1532 free( value.bv_val );
1536 ber_bvarray_add( &vrf->vrf_sub_any, &value );
1539 case LDAP_SUBSTRING_FINAL:
1541 LDAP_LOG( FILTER, DETAIL1,
1542 "get_substring_filter: conn %d FINAL\n", op->o_connid, 0, 0 );
1544 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
1547 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1548 free( value.bv_val );
1552 vrf->vrf_sub_final = value;
1557 LDAP_LOG( FILTER, INFO,
1558 "get_substring_filter: conn %d unknown substring type %ld\n",
1559 op->o_connid, (long)tag, 0 );
1561 Debug( LDAP_DEBUG_FILTER,
1562 " unknown substring type=%ld\n",
1566 free( value.bv_val );
1570 LDAP_LOG( FILTER, INFO,
1571 "get_substring_filter: conn %d error %ld\n",
1572 op->o_connid, (long)rc, 0 );
1574 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
1577 free( vrf->vrf_sub_initial.bv_val );
1578 ber_bvarray_free( vrf->vrf_sub_any );
1579 free( vrf->vrf_sub_final.bv_val );
1580 ch_free( vrf->vrf_sub );
1586 LDAP_LOG( FILTER, ENTRY,
1587 "get_substring_filter: conn %d exit\n", op->o_connid, 0, 0 );
1589 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
1591 return( LDAP_SUCCESS );