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_tmpmfuncs = &ch_mfuncs;
619 filter_free_x( &op, f );
623 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
631 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx );
635 switch ( f->f_choice ) {
636 case LDAP_FILTER_EQUALITY:
637 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
639 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
640 tmp.bv_len + ( sizeof("(=)") - 1 );
641 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
643 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
644 f->f_av_desc->ad_cname.bv_val,
647 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
651 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
653 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
654 tmp.bv_len + ( sizeof("(>=)") - 1 );
655 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
657 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
658 f->f_av_desc->ad_cname.bv_val,
661 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
665 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
667 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
668 tmp.bv_len + ( sizeof("(<=)") - 1 );
669 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
671 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
672 f->f_av_desc->ad_cname.bv_val,
675 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
678 case LDAP_FILTER_APPROX:
679 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
681 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
682 tmp.bv_len + ( sizeof("(~=)") - 1 );
683 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
685 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
686 f->f_av_desc->ad_cname.bv_val,
688 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
691 case LDAP_FILTER_SUBSTRINGS:
692 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
693 ( sizeof("(=*)") - 1 );
694 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
696 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
697 f->f_sub_desc->ad_cname.bv_val );
699 if ( f->f_sub_initial.bv_val != NULL ) {
702 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
704 fstr->bv_len += tmp.bv_len;
705 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
707 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
708 /* "(attr=" */ "%s*)",
711 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
714 if ( f->f_sub_any != NULL ) {
715 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
717 filter_escape_value_x( &f->f_sub_any[i], &tmp, op->o_tmpmemctx );
719 fstr->bv_len += tmp.bv_len + 1;
720 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
722 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
723 /* "(attr=[init]*[any*]" */ "%s*)",
725 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
729 if ( f->f_sub_final.bv_val != NULL ) {
732 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
734 fstr->bv_len += tmp.bv_len;
735 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
737 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
738 /* "(attr=[init*][any*]" */ "%s)",
741 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
746 case LDAP_FILTER_PRESENT:
747 fstr->bv_len = f->f_desc->ad_cname.bv_len +
748 ( sizeof("(=*)") - 1 );
749 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
751 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
752 f->f_desc->ad_cname.bv_val );
755 case LDAP_FILTER_AND:
757 case LDAP_FILTER_NOT:
758 fstr->bv_len = sizeof("(%)") - 1;
759 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
761 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
762 f->f_choice == LDAP_FILTER_AND ? '&' :
763 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
765 for ( p = f->f_list; p != NULL; p = p->f_next ) {
768 filter2bv_x( op, p, &tmp );
770 fstr->bv_len += tmp.bv_len;
771 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
773 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
774 /*"("*/ "%s)", tmp.bv_val );
776 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
781 case LDAP_FILTER_EXT: {
783 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
785 if ( f->f_mr_desc ) {
786 ad = f->f_mr_desc->ad_cname;
792 fstr->bv_len = ad.bv_len +
793 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
794 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
795 tmp.bv_len + ( sizeof("(:=)") - 1 );
796 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
798 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
800 f->f_mr_dnattrs ? ":dn" : "",
801 f->f_mr_rule_text.bv_len ? ":" : "",
802 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
804 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
807 case SLAPD_FILTER_COMPUTED:
809 f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
810 f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
811 f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
813 f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
814 f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
815 f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
816 sizeof("(?=error)")-1,
817 1, fstr, op->o_tmpmemctx );
821 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr, op->o_tmpmemctx );
827 filter2bv( Filter *f, struct berval *fstr )
830 op.o_tmpmemctx = NULL;
831 op.o_tmpmfuncs = &ch_mfuncs;
833 filter2bv_x( &op, f, fstr );
837 filter_escape_value_x(
846 i = in->bv_len * 3 + 1;
847 out->bv_val = ctx ? sl_malloc( i, ctx ) : ch_malloc( i );
850 for( i=0; i < in->bv_len ; i++ ) {
851 if( FILTER_ESCAPE(in->bv_val[i]) ) {
852 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
853 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
854 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
856 out->bv_val[out->bv_len++] = in->bv_val[i];
860 out->bv_val[out->bv_len] = '\0';
869 return filter_escape_value_x( in, out, NULL );
876 ValuesReturnFilter **filt,
882 ValuesReturnFilter vrf;
885 LDAP_LOG( FILTER, ENTRY,
886 "get_simple_vrFilter: conn %d\n", op->o_connid, 0, 0 );
888 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
891 tag = ber_peek_tag( ber, &len );
893 if( tag == LBER_ERROR ) {
894 *text = "error decoding filter";
895 return SLAPD_DISCONNECT;
901 vrf.vrf_choice = tag;
903 switch ( vrf.vrf_choice ) {
904 case LDAP_FILTER_EQUALITY:
906 LDAP_LOG( FILTER, DETAIL2,
907 "get_simple_vrFilter: conn %d EQUALITY\n", op->o_connid, 0, 0 );
909 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
911 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
912 if ( err != LDAP_SUCCESS ) {
916 assert( vrf.vrf_ava != NULL );
919 case LDAP_FILTER_SUBSTRINGS:
921 LDAP_LOG( FILTER, DETAIL1,
922 "get_simple_vrFilter: conn %d SUBSTRINGS\n", op->o_connid, 0, 0 );
924 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
926 err = get_ssa( op, ber, &vrf.vrf_sub, text );
931 LDAP_LOG( FILTER, DETAIL1,
932 "get_simple_vrFilter: conn %d GE\n", op->o_connid, 0, 0 );
934 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
936 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
937 if ( err != LDAP_SUCCESS ) {
944 LDAP_LOG( FILTER, DETAIL1,
945 "get_simple_vrFilter: conn %d LE\n", op->o_connid, 0, 0 );
947 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
949 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
950 if ( err != LDAP_SUCCESS ) {
955 case LDAP_FILTER_PRESENT: {
959 LDAP_LOG( FILTER, DETAIL1,
960 "get_simple_vrFilter: conn %d PRESENT\n", op->o_connid, 0, 0 );
962 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
964 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
965 err = SLAPD_DISCONNECT;
966 *text = "error decoding filter";
971 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
973 if( err != LDAP_SUCCESS ) {
974 /* unrecognized attribute description or other error */
975 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
976 vrf.vrf_result = LDAP_COMPARE_FALSE;
982 case LDAP_FILTER_APPROX:
984 LDAP_LOG( FILTER, DETAIL1,
985 "get_simple_vrFilter: conn %d APPROX\n", op->o_connid, 0, 0 );
987 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
989 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
990 if ( err != LDAP_SUCCESS ) {
995 case LDAP_FILTER_EXT:
997 LDAP_LOG( FILTER, DETAIL1,
998 "get_simple_vrFilter: conn %d EXTENSIBLE\n", op->o_connid, 0, 0 );
1000 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1003 err = get_mra( op, ber, &vrf.vrf_mra, text );
1004 if ( err != LDAP_SUCCESS ) {
1008 assert( vrf.vrf_mra != NULL );
1012 (void) ber_scanf( ber, "x" ); /* skip the element */
1014 LDAP_LOG( FILTER, ERR,
1015 "get_simple_vrFilter: conn %d unknown filter type=%lu\n",
1016 op->o_connid, vrf.vrf_choice, 0 );
1018 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1019 vrf.vrf_choice, 0, 0 );
1021 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1022 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1026 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1028 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1029 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1033 if ( err == LDAP_SUCCESS ) {
1034 *filt = ch_malloc( sizeof vrf );
1039 LDAP_LOG( FILTER, DETAIL2,
1040 "get_simple_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1042 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1049 get_vrFilter( Operation *op, BerElement *ber,
1050 ValuesReturnFilter **vrf,
1054 * A ValuesReturnFilter looks like this:
1056 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1057 * SimpleFilterItem ::= CHOICE {
1058 * equalityMatch [3] AttributeValueAssertion,
1059 * substrings [4] SubstringFilter,
1060 * greaterOrEqual [5] AttributeValueAssertion,
1061 * lessOrEqual [6] AttributeValueAssertion,
1062 * present [7] AttributeType,
1063 * approxMatch [8] AttributeValueAssertion,
1064 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1067 * SubstringFilter ::= SEQUENCE {
1068 * type AttributeType,
1069 * SEQUENCE OF CHOICE {
1070 * initial [0] IA5String,
1071 * any [1] IA5String,
1072 * final [2] IA5String
1076 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1077 * matchingRule [1] MatchingRuleId OPTIONAL,
1078 * type [2] AttributeDescription OPTIONAL,
1079 * matchValue [3] AssertionValue }
1082 ValuesReturnFilter **n;
1088 LDAP_LOG( FILTER, ENTRY,
1089 "get_vrFilter: conn %d start\n", op->o_connid, 0, 0 );
1091 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1094 tag = ber_peek_tag( ber, &len );
1096 if( tag == LBER_ERROR ) {
1097 *text = "error decoding vrFilter";
1098 return SLAPD_DISCONNECT;
1101 if( tag != LBER_SEQUENCE ) {
1102 *text = "error decoding vrFilter, expect SEQUENCE tag";
1103 return SLAPD_DISCONNECT;
1107 for ( tag = ber_first_element( ber, &len, &last );
1108 tag != LBER_DEFAULT;
1109 tag = ber_next_element( ber, &len, last ) )
1111 int err = get_simple_vrFilter( op, ber, n, text );
1113 if ( err != LDAP_SUCCESS ) return( err );
1115 n = &(*n)->vrf_next;
1120 LDAP_LOG( FILTER, ENTRY,
1121 "get_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1123 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1125 return( LDAP_SUCCESS );
1129 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1131 ValuesReturnFilter *p, *next;
1133 if ( vrf == NULL ) {
1137 for ( p = vrf; p != NULL; p = next ) {
1140 switch ( vrf->vrf_choice ) {
1141 case LDAP_FILTER_PRESENT:
1144 case LDAP_FILTER_EQUALITY:
1145 case LDAP_FILTER_GE:
1146 case LDAP_FILTER_LE:
1147 case LDAP_FILTER_APPROX:
1148 ava_free( op, vrf->vrf_ava, 1 );
1151 case LDAP_FILTER_SUBSTRINGS:
1152 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1154 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1156 ch_free( vrf->vrf_sub_initial.bv_val );
1159 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1160 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1162 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1164 ch_free( vrf->vrf_sub_final.bv_val );
1167 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1170 case LDAP_FILTER_EXT:
1171 mra_free( op, vrf->vrf_mra, 1 );
1174 case SLAPD_FILTER_COMPUTED:
1179 LDAP_LOG( FILTER, ERR,
1180 "filter_free: unknown filter type %lu\n", vrf->vrf_choice, 0, 0 );
1182 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1183 vrf->vrf_choice, 0, 0 );
1188 op->o_tmpfree( vrf, op->o_tmpmemctx );
1193 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1195 ValuesReturnFilter *p;
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 = op->o_tmprealloc( 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 )
1230 if ( vrf == NULL ) {
1231 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx );
1235 switch ( vrf->vrf_choice ) {
1236 case LDAP_FILTER_EQUALITY:
1237 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1239 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1240 tmp.bv_len + ( sizeof("(=)") - 1 );
1241 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1243 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1244 vrf->vrf_av_desc->ad_cname.bv_val,
1247 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1250 case LDAP_FILTER_GE:
1251 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1253 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1254 tmp.bv_len + ( sizeof("(>=)") - 1 );
1255 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1257 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1258 vrf->vrf_av_desc->ad_cname.bv_val,
1261 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1264 case LDAP_FILTER_LE:
1265 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1267 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1268 tmp.bv_len + ( sizeof("(<=)") - 1 );
1269 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1271 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1272 vrf->vrf_av_desc->ad_cname.bv_val,
1275 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1278 case LDAP_FILTER_APPROX:
1279 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1281 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1282 tmp.bv_len + ( sizeof("(~=)") - 1 );
1283 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1285 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1286 vrf->vrf_av_desc->ad_cname.bv_val,
1288 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1291 case LDAP_FILTER_SUBSTRINGS:
1292 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1293 ( sizeof("(=*)") - 1 );
1294 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1296 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1297 vrf->vrf_sub_desc->ad_cname.bv_val );
1299 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1302 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1304 fstr->bv_len += tmp.bv_len;
1305 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1307 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1308 /* "(attr=" */ "%s*)",
1311 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1314 if ( vrf->vrf_sub_any != NULL ) {
1316 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1318 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp, op->o_tmpmemctx );
1320 fstr->bv_len += tmp.bv_len + 1;
1321 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1323 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1324 /* "(attr=[init]*[any*]" */ "%s*)",
1326 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1330 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1333 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1335 fstr->bv_len += tmp.bv_len;
1336 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1338 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1339 /* "(attr=[init*][any*]" */ "%s)",
1342 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1347 case LDAP_FILTER_PRESENT:
1348 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1349 ( sizeof("(=*)") - 1 );
1350 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1352 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1353 vrf->vrf_desc->ad_cname.bv_val );
1356 case LDAP_FILTER_EXT: {
1358 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1360 if ( vrf->vrf_mr_desc ) {
1361 ad = vrf->vrf_mr_desc->ad_cname;
1367 fstr->bv_len = ad.bv_len +
1368 ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1369 ( vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1370 tmp.bv_len + ( sizeof("(:=)") - 1 );
1371 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1373 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1375 vrf->vrf_mr_dnattrs ? ":dn" : "",
1376 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1377 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1380 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1383 case SLAPD_FILTER_COMPUTED:
1385 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1386 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1387 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
1389 vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1390 vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1391 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
1392 sizeof("(?=error)")-1,
1393 1, fstr, op->o_tmpmemctx );
1397 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr, op->o_tmpmemctx );
1404 get_substring_vrFilter(
1407 ValuesReturnFilter *vrf,
1413 struct berval value;
1416 *text = "error decoding filter";
1419 LDAP_LOG( FILTER, ENTRY,
1420 "get_substring_filter: conn %d begin\n", op->o_connid, 0, 0 );
1422 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
1424 if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
1425 return SLAPD_DISCONNECT;
1428 vrf->vrf_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
1429 vrf->vrf_sub_desc = NULL;
1430 rc = slap_bv2ad( &bv, &vrf->vrf_sub_desc, text );
1432 if( rc != LDAP_SUCCESS ) {
1434 ch_free( vrf->vrf_sub );
1435 vrf->vrf_choice = SLAPD_FILTER_COMPUTED;
1436 vrf->vrf_result = SLAPD_COMPARE_UNDEFINED;
1437 return LDAP_SUCCESS;
1440 vrf->vrf_sub_initial.bv_val = NULL;
1441 vrf->vrf_sub_any = NULL;
1442 vrf->vrf_sub_final.bv_val = NULL;
1444 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
1445 tag = ber_next_element( ber, &len, last ) )
1449 rc = ber_scanf( ber, "m", &value );
1450 if ( rc == LBER_ERROR ) {
1451 rc = SLAPD_DISCONNECT;
1455 if ( value.bv_val == NULL || value.bv_len == 0 ) {
1456 rc = LDAP_INVALID_SYNTAX;
1461 case LDAP_SUBSTRING_INITIAL:
1462 usage = SLAP_MR_SUBSTR_INITIAL;
1465 case LDAP_SUBSTRING_ANY:
1466 usage = SLAP_MR_SUBSTR_ANY;
1469 case LDAP_SUBSTRING_FINAL:
1470 usage = SLAP_MR_SUBSTR_FINAL;
1474 rc = LDAP_PROTOCOL_ERROR;
1477 LDAP_LOG( FILTER, ERR,
1478 "get_filter_substring: conn %d unknown substring choice=%ld\n",
1479 op->o_connid, (long)tag, 0 );
1481 Debug( LDAP_DEBUG_FILTER,
1482 " unknown substring choice=%ld\n",
1488 /* validate/normalize using equality matching rule validator! */
1489 rc = asserted_value_validate_normalize(
1490 vrf->vrf_sub_desc, vrf->vrf_sub_desc->ad_type->sat_equality,
1491 usage, &value, &bv, text );
1492 if( rc != LDAP_SUCCESS ) {
1498 rc = LDAP_PROTOCOL_ERROR;
1501 case LDAP_SUBSTRING_INITIAL:
1503 LDAP_LOG( FILTER, DETAIL1,
1504 "get_substring_filter: conn %d INITIAL\n",
1505 op->o_connid, 0, 0 );
1507 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
1510 if ( vrf->vrf_sub_initial.bv_val != NULL
1511 || vrf->vrf_sub_any != NULL
1512 || vrf->vrf_sub_final.bv_val != NULL )
1514 free( value.bv_val );
1518 vrf->vrf_sub_initial = value;
1521 case LDAP_SUBSTRING_ANY:
1523 LDAP_LOG( FILTER, DETAIL1,
1524 "get_substring_filter: conn %d ANY\n", op->o_connid, 0, 0 );
1526 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
1529 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1530 free( value.bv_val );
1534 ber_bvarray_add( &vrf->vrf_sub_any, &value );
1537 case LDAP_SUBSTRING_FINAL:
1539 LDAP_LOG( FILTER, DETAIL1,
1540 "get_substring_filter: conn %d FINAL\n", op->o_connid, 0, 0 );
1542 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
1545 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1546 free( value.bv_val );
1550 vrf->vrf_sub_final = value;
1555 LDAP_LOG( FILTER, INFO,
1556 "get_substring_filter: conn %d unknown substring type %ld\n",
1557 op->o_connid, (long)tag, 0 );
1559 Debug( LDAP_DEBUG_FILTER,
1560 " unknown substring type=%ld\n",
1564 free( value.bv_val );
1568 LDAP_LOG( FILTER, INFO,
1569 "get_substring_filter: conn %d error %ld\n",
1570 op->o_connid, (long)rc, 0 );
1572 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
1575 free( vrf->vrf_sub_initial.bv_val );
1576 ber_bvarray_free( vrf->vrf_sub_any );
1577 free( vrf->vrf_sub_final.bv_val );
1578 ch_free( vrf->vrf_sub );
1584 LDAP_LOG( FILTER, ENTRY,
1585 "get_substring_filter: conn %d exit\n", op->o_connid, 0, 0 );
1587 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
1589 return( LDAP_SUCCESS );