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(
23 static int get_substring_filter(
29 static int filter_escape_value(
33 static void simple_vrFilter2bv(
34 ValuesReturnFilter *f,
35 struct berval *fstr );
37 static int get_simple_vrFilter(
40 ValuesReturnFilter **f,
57 LDAP_LOG( FILTER, ENTRY, "get_filter: conn %d\n", conn->c_connid, 0, 0 );
59 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
62 * A filter looks like this coming in:
64 * and [0] SET OF Filter,
65 * or [1] SET OF Filter,
67 * equalityMatch [3] AttributeValueAssertion,
68 * substrings [4] SubstringFilter,
69 * greaterOrEqual [5] AttributeValueAssertion,
70 * lessOrEqual [6] AttributeValueAssertion,
71 * present [7] AttributeType,,
72 * approxMatch [8] AttributeValueAssertion
73 * extensibleMatch [9] MatchingRuleAssertion
76 * SubstringFilter ::= SEQUENCE {
78 * SEQUENCE OF CHOICE {
79 * initial [0] IA5String,
85 * MatchingRuleAssertion ::= SEQUENCE {
86 * matchingRule [1] MatchingRuleId OPTIONAL,
87 * type [2] AttributeDescription OPTIONAL,
88 * matchValue [3] AssertionValue,
89 * dnAttributes [4] BOOLEAN DEFAULT FALSE
94 tag = ber_peek_tag( ber, &len );
96 if( tag == LBER_ERROR ) {
97 *text = "error decoding filter";
98 return SLAPD_DISCONNECT;
101 f = (Filter *) ch_malloc( sizeof(Filter) );
107 switch ( f->f_choice ) {
108 case LDAP_FILTER_EQUALITY:
110 LDAP_LOG( FILTER, DETAIL2,
111 "get_filter: conn %d EQUALITY\n", conn->c_connid, 0, 0 );
113 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
115 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY, text );
116 if ( err != LDAP_SUCCESS ) {
120 assert( f->f_ava != NULL );
123 case LDAP_FILTER_SUBSTRINGS:
125 LDAP_LOG( FILTER, DETAIL1,
126 "get_filter: conn %d SUBSTRINGS\n", conn->c_connid, 0, 0 );
128 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
130 err = get_substring_filter( conn, ber, f, text );
135 LDAP_LOG( FILTER, DETAIL1,
136 "get_filter: conn %d GE\n", conn->c_connid, 0, 0 );
138 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
140 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
141 if ( err != LDAP_SUCCESS ) {
148 LDAP_LOG( FILTER, DETAIL1,
149 "get_filter: conn %d LE\n", conn->c_connid, 0, 0 );
151 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
153 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
154 if ( err != LDAP_SUCCESS ) {
159 case LDAP_FILTER_PRESENT: {
163 LDAP_LOG( FILTER, DETAIL1,
164 "get_filter: conn %d PRESENT\n", conn->c_connid, 0, 0 );
166 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
168 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
169 err = SLAPD_DISCONNECT;
170 *text = "error decoding filter";
175 err = slap_bv2ad( &type, &f->f_desc, text );
177 if( err != LDAP_SUCCESS ) {
178 /* unrecognized attribute description or other error */
179 f->f_choice = SLAPD_FILTER_COMPUTED;
180 f->f_result = LDAP_COMPARE_FALSE;
186 case LDAP_FILTER_APPROX:
188 LDAP_LOG( FILTER, DETAIL1,
189 "get_filter: conn %d APPROX\n", conn->c_connid, 0, 0 );
191 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
193 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY_APPROX, text );
194 if ( err != LDAP_SUCCESS ) {
199 case LDAP_FILTER_AND:
201 LDAP_LOG( FILTER, DETAIL1,
202 "get_filter: conn %d AND\n", conn->c_connid, 0, 0 );
204 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
206 err = get_filter_list( conn, ber, &f->f_and, text );
207 if ( err != LDAP_SUCCESS ) {
214 LDAP_LOG( FILTER, DETAIL1,
215 "get_filter: conn %d OR\n", conn->c_connid, 0, 0 );
217 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
219 err = get_filter_list( conn, ber, &f->f_or, text );
220 if ( err != LDAP_SUCCESS ) {
225 case LDAP_FILTER_NOT:
227 LDAP_LOG( FILTER, DETAIL1,
228 "get_filter: conn %d NOT\n", conn->c_connid, 0, 0 );
230 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
232 (void) ber_skip_tag( ber, &len );
233 err = get_filter( conn, ber, &f->f_not, text );
234 if ( err != LDAP_SUCCESS ) {
239 case LDAP_FILTER_EXT:
241 LDAP_LOG( FILTER, DETAIL1,
242 "get_filter: conn %d EXTENSIBLE\n", conn->c_connid, 0, 0 );
244 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
247 err = get_mra( ber, &f->f_mra, text );
248 if ( err != LDAP_SUCCESS ) {
252 assert( f->f_mra != NULL );
256 (void) ber_scanf( ber, "x" ); /* skip the element */
258 LDAP_LOG( FILTER, ERR,
259 "get_filter: conn %d unknown filter type=%lu\n",
260 conn->c_connid, f->f_choice, 0 );
262 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
265 f->f_choice = SLAPD_FILTER_COMPUTED;
266 f->f_result = SLAPD_COMPARE_UNDEFINED;
270 if ( err != LDAP_SUCCESS ) {
271 if( err != SLAPD_DISCONNECT ) {
273 f->f_choice = SLAPD_FILTER_COMPUTED;
274 f->f_result = SLAPD_COMPARE_UNDEFINED;
287 LDAP_LOG( FILTER, DETAIL2,
288 "get_filter: conn %d exit\n", conn->c_connid, 0, 0 );
290 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
296 get_filter_list( Connection *conn, BerElement *ber,
307 LDAP_LOG( FILTER, ENTRY,
308 "get_filter_list: conn %d start\n", conn->c_connid, 0, 0 );
310 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
313 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
314 tag = ber_next_element( ber, &len, last ) )
316 err = get_filter( conn, ber, new, text );
317 if ( err != LDAP_SUCCESS )
319 new = &(*new)->f_next;
324 LDAP_LOG( FILTER, ENTRY,
325 "get_filter_list: conn %d exit\n", conn->c_connid, 0, 0 );
327 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
329 return( LDAP_SUCCESS );
333 get_substring_filter(
345 *text = "error decoding filter";
348 LDAP_LOG( FILTER, ENTRY,
349 "get_substring_filter: conn %d begin\n", conn->c_connid, 0, 0 );
351 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
353 if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
354 return SLAPD_DISCONNECT;
357 f->f_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
358 f->f_sub_desc = NULL;
359 rc = slap_bv2ad( &bv, &f->f_sub_desc, text );
361 if( rc != LDAP_SUCCESS ) {
364 f->f_choice = SLAPD_FILTER_COMPUTED;
365 f->f_result = SLAPD_COMPARE_UNDEFINED;
369 f->f_sub_initial.bv_val = NULL;
371 f->f_sub_final.bv_val = NULL;
373 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
374 tag = ber_next_element( ber, &len, last ) )
378 rc = ber_scanf( ber, "m", &value );
379 if ( rc == LBER_ERROR ) {
380 rc = SLAPD_DISCONNECT;
384 if ( value.bv_val == NULL || value.bv_len == 0 ) {
385 rc = LDAP_INVALID_SYNTAX;
390 case LDAP_SUBSTRING_INITIAL:
391 usage = SLAP_MR_SUBSTR_INITIAL;
394 case LDAP_SUBSTRING_ANY:
395 usage = SLAP_MR_SUBSTR_ANY;
398 case LDAP_SUBSTRING_FINAL:
399 usage = SLAP_MR_SUBSTR_FINAL;
403 rc = LDAP_PROTOCOL_ERROR;
406 LDAP_LOG( FILTER, ERR,
407 "get_filter_substring: conn %d unknown substring choice=%ld\n",
408 conn->c_connid, (long)tag, 0 );
410 Debug( LDAP_DEBUG_FILTER,
411 " unknown substring choice=%ld\n",
418 /* validate/normalize using equality matching rule validator! */
419 rc = asserted_value_validate_normalize(
420 f->f_sub_desc, f->f_sub_desc->ad_type->sat_equality,
421 usage, &value, &bv, text );
422 if( rc != LDAP_SUCCESS ) {
426 /* validate using equality matching rule validator! */
427 rc = value_validate( f->f_sub_desc->ad_type->sat_equality,
429 if( rc != LDAP_SUCCESS ) {
433 rc = value_normalize( f->f_sub_desc, usage,
435 if( rc != LDAP_SUCCESS ) {
442 rc = LDAP_PROTOCOL_ERROR;
445 case LDAP_SUBSTRING_INITIAL:
447 LDAP_LOG( FILTER, DETAIL1,
448 "get_substring_filter: conn %d INITIAL\n", conn->c_connid, 0, 0 );
450 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
453 if ( f->f_sub_initial.bv_val != NULL
454 || f->f_sub_any != NULL
455 || f->f_sub_final.bv_val != NULL )
457 free( value.bv_val );
461 f->f_sub_initial = value;
464 case LDAP_SUBSTRING_ANY:
466 LDAP_LOG( FILTER, DETAIL1,
467 "get_substring_filter: conn %d ANY\n", conn->c_connid, 0, 0 );
469 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
472 if ( f->f_sub_final.bv_val != NULL ) {
473 free( value.bv_val );
477 ber_bvarray_add( &f->f_sub_any, &value );
480 case LDAP_SUBSTRING_FINAL:
482 LDAP_LOG( FILTER, DETAIL1,
483 "get_substring_filter: conn %d FINAL\n", conn->c_connid, 0, 0 );
485 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
488 if ( f->f_sub_final.bv_val != NULL ) {
489 free( value.bv_val );
493 f->f_sub_final = value;
498 LDAP_LOG( FILTER, INFO,
499 "get_substring_filter: conn %d unknown substring type %ld\n",
500 conn->c_connid, (long)tag, 0 );
502 Debug( LDAP_DEBUG_FILTER,
503 " unknown substring type=%ld\n",
507 free( value.bv_val );
511 LDAP_LOG( FILTER, INFO,
512 "get_substring_filter: conn %d error %ld\n",
513 conn->c_connid, (long)rc, 0 );
515 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
518 free( f->f_sub_initial.bv_val );
519 ber_bvarray_free( f->f_sub_any );
520 free( f->f_sub_final.bv_val );
527 LDAP_LOG( FILTER, ENTRY,
528 "get_substring_filter: conn %d exit\n", conn->c_connid, 0, 0 );
530 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
532 return( LDAP_SUCCESS );
536 filter_free( Filter *f )
544 switch ( f->f_choice ) {
545 case LDAP_FILTER_PRESENT:
548 case LDAP_FILTER_EQUALITY:
551 case LDAP_FILTER_APPROX:
552 ava_free( f->f_ava, 1 );
555 case LDAP_FILTER_SUBSTRINGS:
556 if ( f->f_sub_initial.bv_val != NULL ) {
557 free( f->f_sub_initial.bv_val );
559 ber_bvarray_free( f->f_sub_any );
560 if ( f->f_sub_final.bv_val != NULL ) {
561 free( f->f_sub_final.bv_val );
566 case LDAP_FILTER_AND:
568 case LDAP_FILTER_NOT:
569 for ( p = f->f_list; p != NULL; p = next ) {
575 case LDAP_FILTER_EXT:
576 mra_free( f->f_mra, 1 );
579 case SLAPD_FILTER_COMPUTED:
584 LDAP_LOG( FILTER, ERR,
585 "filter_free: unknown filter type %lu\n", f->f_choice, 0, 0 );
587 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
597 filter2bv( Filter *f, struct berval *fstr )
605 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
609 switch ( f->f_choice ) {
610 case LDAP_FILTER_EQUALITY:
611 filter_escape_value( &f->f_av_value, &tmp );
613 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
614 tmp.bv_len + ( sizeof("(=)") - 1 );
615 fstr->bv_val = malloc( fstr->bv_len + 1 );
617 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
618 f->f_av_desc->ad_cname.bv_val,
621 ber_memfree( tmp.bv_val );
625 filter_escape_value( &f->f_av_value, &tmp );
627 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
628 tmp.bv_len + ( sizeof("(>=)") - 1 );
629 fstr->bv_val = malloc( fstr->bv_len + 1 );
631 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
632 f->f_av_desc->ad_cname.bv_val,
635 ber_memfree( tmp.bv_val );
639 filter_escape_value( &f->f_av_value, &tmp );
641 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
642 tmp.bv_len + ( sizeof("(<=)") - 1 );
643 fstr->bv_val = malloc( fstr->bv_len + 1 );
645 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
646 f->f_av_desc->ad_cname.bv_val,
649 ber_memfree( tmp.bv_val );
652 case LDAP_FILTER_APPROX:
653 filter_escape_value( &f->f_av_value, &tmp );
655 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
656 tmp.bv_len + ( sizeof("(~=)") - 1 );
657 fstr->bv_val = malloc( fstr->bv_len + 1 );
659 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
660 f->f_av_desc->ad_cname.bv_val,
662 ber_memfree( tmp.bv_val );
665 case LDAP_FILTER_SUBSTRINGS:
666 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
667 ( sizeof("(=*)") - 1 );
668 fstr->bv_val = malloc( fstr->bv_len + 128 );
670 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
671 f->f_sub_desc->ad_cname.bv_val );
673 if ( f->f_sub_initial.bv_val != NULL ) {
676 filter_escape_value( &f->f_sub_initial, &tmp );
678 fstr->bv_len += tmp.bv_len;
679 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
681 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
682 /* "(attr=" */ "%s*)",
685 ber_memfree( tmp.bv_val );
688 if ( f->f_sub_any != NULL ) {
689 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
691 filter_escape_value( &f->f_sub_any[i], &tmp );
693 fstr->bv_len += tmp.bv_len + 1;
694 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
696 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
697 /* "(attr=[init]*[any*]" */ "%s*)",
699 ber_memfree( tmp.bv_val );
703 if ( f->f_sub_final.bv_val != NULL ) {
706 filter_escape_value( &f->f_sub_final, &tmp );
708 fstr->bv_len += tmp.bv_len;
709 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
711 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
712 /* "(attr=[init*][any*]" */ "%s)",
715 ber_memfree( tmp.bv_val );
720 case LDAP_FILTER_PRESENT:
721 fstr->bv_len = f->f_desc->ad_cname.bv_len +
722 ( sizeof("(=*)") - 1 );
723 fstr->bv_val = malloc( fstr->bv_len + 1 );
725 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
726 f->f_desc->ad_cname.bv_val );
729 case LDAP_FILTER_AND:
731 case LDAP_FILTER_NOT:
732 fstr->bv_len = sizeof("(%)") - 1;
733 fstr->bv_val = malloc( fstr->bv_len + 128 );
735 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
736 f->f_choice == LDAP_FILTER_AND ? '&' :
737 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
739 for ( p = f->f_list; p != NULL; p = p->f_next ) {
742 filter2bv( p, &tmp );
744 fstr->bv_len += tmp.bv_len;
745 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
747 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
748 /*"("*/ "%s)", tmp.bv_val );
750 ch_free( tmp.bv_val );
755 case LDAP_FILTER_EXT: {
757 filter_escape_value( &f->f_mr_value, &tmp );
759 if ( f->f_mr_desc ) {
760 ad = f->f_mr_desc->ad_cname;
766 fstr->bv_len = ad.bv_len +
767 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
768 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
769 tmp.bv_len + ( sizeof("(:=)") - 1 );
770 fstr->bv_val = malloc( fstr->bv_len + 1 );
772 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
774 f->f_mr_dnattrs ? ":dn" : "",
775 f->f_mr_rule_text.bv_len ? ":" : "",
776 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
778 ber_memfree( tmp.bv_val );
781 case SLAPD_FILTER_COMPUTED:
783 f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
784 f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
785 f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
787 f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
788 f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
789 f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
790 sizeof("(?=error)")-1,
795 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
800 static int filter_escape_value(
808 out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 );
811 for( i=0; i < in->bv_len ; i++ ) {
812 if( FILTER_ESCAPE(in->bv_val[i]) ) {
813 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
814 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
815 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
817 out->bv_val[out->bv_len++] = in->bv_val[i];
821 out->bv_val[out->bv_len] = '\0';
829 ValuesReturnFilter **filt,
835 ValuesReturnFilter *vrf;
838 LDAP_LOG( FILTER, ENTRY,
839 "get_simple_vrFilter: conn %d\n", conn->c_connid, 0, 0 );
841 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
844 tag = ber_peek_tag( ber, &len );
846 if( tag == LBER_ERROR ) {
847 *text = "error decoding filter";
848 return SLAPD_DISCONNECT;
851 vrf = (ValuesReturnFilter *) ch_malloc( sizeof(ValuesReturnFilter) );
852 vrf->vrf_next = NULL;
855 vrf->vrf_choice = tag;
857 switch ( vrf->vrf_choice ) {
858 case LDAP_FILTER_EQUALITY:
860 LDAP_LOG( FILTER, DETAIL2,
861 "get_simple_vrFilter: conn %d EQUALITY\n", conn->c_connid, 0, 0 );
863 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
865 err = get_ava( ber, &vrf->vrf_ava, SLAP_MR_EQUALITY, text );
866 if ( err != LDAP_SUCCESS ) {
870 assert( vrf->vrf_ava != NULL );
873 case LDAP_FILTER_SUBSTRINGS:
875 LDAP_LOG( FILTER, DETAIL1,
876 "get_simple_vrFilter: conn %d SUBSTRINGS\n", conn->c_connid, 0, 0 );
878 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
880 err = get_substring_filter( conn, ber, (Filter *)vrf, text );
885 LDAP_LOG( FILTER, DETAIL1,
886 "get_simple_vrFilter: conn %d GE\n", conn->c_connid, 0, 0 );
888 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
890 err = get_ava( ber, &vrf->vrf_ava, SLAP_MR_ORDERING, text );
891 if ( err != LDAP_SUCCESS ) {
898 LDAP_LOG( FILTER, DETAIL1,
899 "get_simple_vrFilter: conn %d LE\n", conn->c_connid, 0, 0 );
901 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
903 err = get_ava( ber, &vrf->vrf_ava, SLAP_MR_ORDERING, text );
904 if ( err != LDAP_SUCCESS ) {
909 case LDAP_FILTER_PRESENT: {
913 LDAP_LOG( FILTER, DETAIL1,
914 "get_simple_vrFilter: conn %d PRESENT\n", conn->c_connid, 0, 0 );
916 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
918 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
919 err = SLAPD_DISCONNECT;
920 *text = "error decoding filter";
924 vrf->vrf_desc = NULL;
925 err = slap_bv2ad( &type, &vrf->vrf_desc, text );
927 if( err != LDAP_SUCCESS ) {
928 /* unrecognized attribute description or other error */
929 vrf->vrf_choice = SLAPD_FILTER_COMPUTED;
930 vrf->vrf_result = LDAP_COMPARE_FALSE;
936 case LDAP_FILTER_APPROX:
938 LDAP_LOG( FILTER, DETAIL1,
939 "get_simple_vrFilter: conn %d APPROX\n", conn->c_connid, 0, 0 );
941 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
943 err = get_ava( ber, &vrf->vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
944 if ( err != LDAP_SUCCESS ) {
949 case LDAP_FILTER_EXT:
951 LDAP_LOG( FILTER, DETAIL1,
952 "get_simple_vrFilter: conn %d EXTENSIBLE\n", conn->c_connid, 0, 0 );
954 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
957 err = get_mra( ber, &vrf->vrf_mra, text );
958 if ( err != LDAP_SUCCESS ) {
962 assert( vrf->vrf_mra != NULL );
966 (void) ber_scanf( ber, "x" ); /* skip the element */
968 LDAP_LOG( FILTER, ERR,
969 "get_simple_vrFilter: conn %d unknown filter type=%lu\n",
970 conn->c_connid, vrf->vrf_choice, 0 );
972 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
973 vrf->vrf_choice, 0, 0 );
975 vrf->vrf_choice = SLAPD_FILTER_COMPUTED;
976 vrf->vrf_result = SLAPD_COMPARE_UNDEFINED;
980 if ( err != LDAP_SUCCESS ) {
981 if( err != SLAPD_DISCONNECT ) {
983 vrf->vrf_choice = SLAPD_FILTER_COMPUTED;
984 vrf->vrf_result = SLAPD_COMPARE_UNDEFINED;
997 LDAP_LOG( FILTER, DETAIL2,
998 "get_simple_vrFilter: conn %d exit\n", conn->c_connid, 0, 0 );
1000 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1006 get_vrFilter( Connection *conn, BerElement *ber,
1007 ValuesReturnFilter **vrf,
1011 * A ValuesReturnFilter looks like this:
1013 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1014 * SimpleFilterItem ::= CHOICE {
1015 * equalityMatch [3] AttributeValueAssertion,
1016 * substrings [4] SubstringFilter,
1017 * greaterOrEqual [5] AttributeValueAssertion,
1018 * lessOrEqual [6] AttributeValueAssertion,
1019 * present [7] AttributeType,
1020 * approxMatch [8] AttributeValueAssertion,
1021 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1024 * SubstringFilter ::= SEQUENCE {
1025 * type AttributeType,
1026 * SEQUENCE OF CHOICE {
1027 * initial [0] IA5String,
1028 * any [1] IA5String,
1029 * final [2] IA5String
1033 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1034 * matchingRule [1] MatchingRuleId OPTIONAL,
1035 * type [2] AttributeDescription OPTIONAL,
1036 * matchValue [3] AssertionValue }
1039 ValuesReturnFilter **n;
1045 LDAP_LOG( FILTER, ENTRY,
1046 "get_vrFilter: conn %d start\n", conn->c_connid, 0, 0 );
1048 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1051 tag = ber_peek_tag( ber, &len );
1053 if( tag == LBER_ERROR ) {
1054 *text = "error decoding vrFilter";
1055 return SLAPD_DISCONNECT;
1058 if( tag != LBER_SEQUENCE ) {
1059 *text = "error decoding vrFilter, expect SEQUENCE tag";
1060 return SLAPD_DISCONNECT;
1064 for ( tag = ber_first_element( ber, &len, &last );
1065 tag != LBER_DEFAULT;
1066 tag = ber_next_element( ber, &len, last ) )
1068 int err = get_simple_vrFilter( conn, ber, n, text );
1069 if ( err != LDAP_SUCCESS )
1071 n = &(*n)->vrf_next;
1076 LDAP_LOG( FILTER, ENTRY,
1077 "get_vrFilter: conn %d exit\n", conn->c_connid, 0, 0 );
1079 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1081 return( LDAP_SUCCESS );
1085 vrFilter_free( ValuesReturnFilter *vrf )
1087 ValuesReturnFilter *p, *next;
1089 if ( vrf == NULL ) {
1093 for ( p = vrf; p != NULL; p = next ) {
1096 switch ( vrf->vrf_choice ) {
1097 case LDAP_FILTER_PRESENT:
1100 case LDAP_FILTER_EQUALITY:
1101 case LDAP_FILTER_GE:
1102 case LDAP_FILTER_LE:
1103 case LDAP_FILTER_APPROX:
1104 ava_free( vrf->vrf_ava, 1 );
1107 case LDAP_FILTER_SUBSTRINGS:
1108 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1109 free( vrf->vrf_sub_initial.bv_val );
1111 ber_bvarray_free( vrf->vrf_sub_any );
1112 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1113 free( vrf->vrf_sub_final.bv_val );
1115 ch_free( vrf->vrf_sub );
1118 case LDAP_FILTER_EXT:
1119 mra_free( vrf->vrf_mra, 1 );
1122 case SLAPD_FILTER_COMPUTED:
1127 LDAP_LOG( FILTER, ERR,
1128 "filter_free: unknown filter type %lu\n", vrf->vrf_choice, 0, 0 );
1130 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1131 vrf->vrf_choice, 0, 0 );
1142 vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr )
1144 ValuesReturnFilter *p;
1148 if ( vrf == NULL ) {
1149 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
1153 fstr->bv_len = sizeof("()") - 1;
1154 fstr->bv_val = malloc( fstr->bv_len + 128 );
1156 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1158 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1161 simple_vrFilter2bv( p, &tmp );
1163 fstr->bv_len += tmp.bv_len;
1164 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1166 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1167 /*"("*/ "%s)", tmp.bv_val );
1169 ch_free( tmp.bv_val );
1174 simple_vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr )
1179 if ( vrf == NULL ) {
1180 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
1184 switch ( vrf->vrf_choice ) {
1185 case LDAP_FILTER_EQUALITY:
1186 filter_escape_value( &vrf->vrf_av_value, &tmp );
1188 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1189 tmp.bv_len + ( sizeof("(=)") - 1 );
1190 fstr->bv_val = malloc( fstr->bv_len + 1 );
1192 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1193 vrf->vrf_av_desc->ad_cname.bv_val,
1196 ber_memfree( tmp.bv_val );
1199 case LDAP_FILTER_GE:
1200 filter_escape_value( &vrf->vrf_av_value, &tmp );
1202 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1203 tmp.bv_len + ( sizeof("(>=)") - 1 );
1204 fstr->bv_val = malloc( fstr->bv_len + 1 );
1206 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1207 vrf->vrf_av_desc->ad_cname.bv_val,
1210 ber_memfree( tmp.bv_val );
1213 case LDAP_FILTER_LE:
1214 filter_escape_value( &vrf->vrf_av_value, &tmp );
1216 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1217 tmp.bv_len + ( sizeof("(<=)") - 1 );
1218 fstr->bv_val = malloc( fstr->bv_len + 1 );
1220 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1221 vrf->vrf_av_desc->ad_cname.bv_val,
1224 ber_memfree( tmp.bv_val );
1227 case LDAP_FILTER_APPROX:
1228 filter_escape_value( &vrf->vrf_av_value, &tmp );
1230 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1231 tmp.bv_len + ( sizeof("(~=)") - 1 );
1232 fstr->bv_val = malloc( fstr->bv_len + 1 );
1234 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1235 vrf->vrf_av_desc->ad_cname.bv_val,
1237 ber_memfree( tmp.bv_val );
1240 case LDAP_FILTER_SUBSTRINGS:
1241 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1242 ( sizeof("(=*)") - 1 );
1243 fstr->bv_val = malloc( fstr->bv_len + 128 );
1245 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1246 vrf->vrf_sub_desc->ad_cname.bv_val );
1248 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1251 filter_escape_value( &vrf->vrf_sub_initial, &tmp );
1253 fstr->bv_len += tmp.bv_len;
1254 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1256 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1257 /* "(attr=" */ "%s*)",
1260 ber_memfree( tmp.bv_val );
1263 if ( vrf->vrf_sub_any != NULL ) {
1265 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1267 filter_escape_value( &vrf->vrf_sub_any[i], &tmp );
1269 fstr->bv_len += tmp.bv_len + 1;
1270 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1272 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1273 /* "(attr=[init]*[any*]" */ "%s*)",
1275 ber_memfree( tmp.bv_val );
1279 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1282 filter_escape_value( &vrf->vrf_sub_final, &tmp );
1284 fstr->bv_len += tmp.bv_len;
1285 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1287 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1288 /* "(attr=[init*][any*]" */ "%s)",
1291 ber_memfree( tmp.bv_val );
1296 case LDAP_FILTER_PRESENT:
1297 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1298 ( sizeof("(=*)") - 1 );
1299 fstr->bv_val = malloc( fstr->bv_len + 1 );
1301 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1302 vrf->vrf_desc->ad_cname.bv_val );
1305 case LDAP_FILTER_EXT: {
1307 filter_escape_value( &vrf->vrf_mr_value, &tmp );
1309 if ( vrf->vrf_mr_desc ) {
1310 ad = vrf->vrf_mr_desc->ad_cname;
1316 fstr->bv_len = ad.bv_len +
1317 ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1318 ( vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1319 tmp.bv_len + ( sizeof("(:=)") - 1 );
1320 fstr->bv_val = malloc( fstr->bv_len + 1 );
1322 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1324 vrf->vrf_mr_dnattrs ? ":dn" : "",
1325 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1326 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1329 ber_memfree( tmp.bv_val );
1332 case SLAPD_FILTER_COMPUTED:
1334 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1335 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1336 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
1338 vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1339 vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1340 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
1341 sizeof("(?=error)")-1,
1346 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
1352 get_substring_vrFilter(
1355 ValuesReturnFilter *vrf,
1361 struct berval value;
1364 *text = "error decoding filter";
1367 LDAP_LOG( FILTER, ENTRY,
1368 "get_substring_filter: conn %d begin\n", conn->c_connid, 0, 0 );
1370 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
1372 if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
1373 return SLAPD_DISCONNECT;
1376 vrf->vrf_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
1377 vrf->vrf_sub_desc = NULL;
1378 rc = slap_bv2ad( &bv, &vrf->vrf_sub_desc, text );
1380 if( rc != LDAP_SUCCESS ) {
1382 ch_free( vrf->vrf_sub );
1383 vrf->vrf_choice = SLAPD_FILTER_COMPUTED;
1384 vrf->vrf_result = SLAPD_COMPARE_UNDEFINED;
1385 return LDAP_SUCCESS;
1388 vrf->vrf_sub_initial.bv_val = NULL;
1389 vrf->vrf_sub_any = NULL;
1390 vrf->vrf_sub_final.bv_val = NULL;
1392 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
1393 tag = ber_next_element( ber, &len, last ) )
1397 rc = ber_scanf( ber, "m", &value );
1398 if ( rc == LBER_ERROR ) {
1399 rc = SLAPD_DISCONNECT;
1403 if ( value.bv_val == NULL || value.bv_len == 0 ) {
1404 rc = LDAP_INVALID_SYNTAX;
1409 case LDAP_SUBSTRING_INITIAL:
1410 usage = SLAP_MR_SUBSTR_INITIAL;
1413 case LDAP_SUBSTRING_ANY:
1414 usage = SLAP_MR_SUBSTR_ANY;
1417 case LDAP_SUBSTRING_FINAL:
1418 usage = SLAP_MR_SUBSTR_FINAL;
1422 rc = LDAP_PROTOCOL_ERROR;
1425 LDAP_LOG( FILTER, ERR,
1426 "get_filter_substring: conn %d unknown substring choice=%ld\n",
1427 conn->c_connid, (long)tag, 0 );
1429 Debug( LDAP_DEBUG_FILTER,
1430 " unknown substring choice=%ld\n",
1437 /* validate/normalize using equality matching rule validator! */
1438 rc = asserted_value_validate_normalize(
1439 vrf->vrf_sub_desc, vrf->vrf_sub_desc->ad_type->sat_equality,
1440 usage, &value, &bv, text );
1441 if( rc != LDAP_SUCCESS ) {
1445 /* valiate using equality matching rule validator! */
1446 rc = value_validate( vrf->vrf_sub_desc->ad_type->sat_equality,
1448 if( rc != LDAP_SUCCESS ) {
1452 rc = value_normalize( vrf->vrf_sub_desc, usage,
1453 &value, &bv, text );
1454 if( rc != LDAP_SUCCESS ) {
1461 rc = LDAP_PROTOCOL_ERROR;
1464 case LDAP_SUBSTRING_INITIAL:
1466 LDAP_LOG( FILTER, DETAIL1,
1467 "get_substring_filter: conn %d INITIAL\n",
1468 conn->c_connid, 0, 0 );
1470 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
1473 if ( vrf->vrf_sub_initial.bv_val != NULL
1474 || vrf->vrf_sub_any != NULL
1475 || vrf->vrf_sub_final.bv_val != NULL )
1477 free( value.bv_val );
1481 vrf->vrf_sub_initial = value;
1484 case LDAP_SUBSTRING_ANY:
1486 LDAP_LOG( FILTER, DETAIL1,
1487 "get_substring_filter: conn %d ANY\n", conn->c_connid, 0, 0 );
1489 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
1492 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1493 free( value.bv_val );
1497 ber_bvarray_add( &vrf->vrf_sub_any, &value );
1500 case LDAP_SUBSTRING_FINAL:
1502 LDAP_LOG( FILTER, DETAIL1,
1503 "get_substring_filter: conn %d FINAL\n", conn->c_connid, 0, 0 );
1505 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
1508 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1509 free( value.bv_val );
1513 vrf->vrf_sub_final = value;
1518 LDAP_LOG( FILTER, INFO,
1519 "get_substring_filter: conn %d unknown substring type %ld\n",
1520 conn->c_connid, (long)tag, 0 );
1522 Debug( LDAP_DEBUG_FILTER,
1523 " unknown substring type=%ld\n",
1527 free( value.bv_val );
1531 LDAP_LOG( FILTER, INFO,
1532 "get_substring_filter: conn %d error %ld\n",
1533 conn->c_connid, (long)rc, 0 );
1535 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
1538 free( vrf->vrf_sub_initial.bv_val );
1539 ber_bvarray_free( vrf->vrf_sub_any );
1540 free( vrf->vrf_sub_final.bv_val );
1541 ch_free( vrf->vrf_sub );
1547 LDAP_LOG( FILTER, ENTRY,
1548 "get_substring_filter: conn %d exit\n", conn->c_connid, 0, 0 );
1550 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
1552 return( LDAP_SUCCESS );