1 /* filter.c - routines for parsing and dealing with filters */
4 * Copyright 1998-2002 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",
417 /* valiate using equality matching rule validator! */
418 rc = value_validate( f->f_sub_desc->ad_type->sat_equality,
420 if( rc != LDAP_SUCCESS ) {
424 rc = value_normalize( f->f_sub_desc, usage,
426 if( rc != LDAP_SUCCESS ) {
432 rc = LDAP_PROTOCOL_ERROR;
435 case LDAP_SUBSTRING_INITIAL:
437 LDAP_LOG( FILTER, DETAIL1,
438 "get_substring_filter: conn %d INITIAL\n", conn->c_connid, 0, 0 );
440 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
443 if ( f->f_sub_initial.bv_val != NULL
444 || f->f_sub_any != NULL
445 || f->f_sub_final.bv_val != NULL )
447 free( value.bv_val );
451 f->f_sub_initial = value;
454 case LDAP_SUBSTRING_ANY:
456 LDAP_LOG( FILTER, DETAIL1,
457 "get_substring_filter: conn %d ANY\n", conn->c_connid, 0, 0 );
459 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
462 if ( f->f_sub_final.bv_val != NULL ) {
463 free( value.bv_val );
467 ber_bvarray_add( &f->f_sub_any, &value );
470 case LDAP_SUBSTRING_FINAL:
472 LDAP_LOG( FILTER, DETAIL1,
473 "get_substring_filter: conn %d FINAL\n", conn->c_connid, 0, 0 );
475 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
478 if ( f->f_sub_final.bv_val != NULL ) {
479 free( value.bv_val );
483 f->f_sub_final = value;
488 LDAP_LOG( FILTER, INFO,
489 "get_substring_filter: conn %d unknown substring type %ld\n",
490 conn->c_connid, (long)tag, 0 );
492 Debug( LDAP_DEBUG_FILTER,
493 " unknown substring type=%ld\n",
497 free( value.bv_val );
501 LDAP_LOG( FILTER, INFO,
502 "get_substring_filter: conn %d error %ld\n",
503 conn->c_connid, (long)rc, 0 );
505 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
508 free( f->f_sub_initial.bv_val );
509 ber_bvarray_free( f->f_sub_any );
510 free( f->f_sub_final.bv_val );
517 LDAP_LOG( FILTER, ENTRY,
518 "get_substring_filter: conn %d exit\n", conn->c_connid, 0, 0 );
520 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
522 return( LDAP_SUCCESS );
526 filter_free( Filter *f )
534 switch ( f->f_choice ) {
535 case LDAP_FILTER_PRESENT:
538 case LDAP_FILTER_EQUALITY:
541 case LDAP_FILTER_APPROX:
542 ava_free( f->f_ava, 1 );
545 case LDAP_FILTER_SUBSTRINGS:
546 if ( f->f_sub_initial.bv_val != NULL ) {
547 free( f->f_sub_initial.bv_val );
549 ber_bvarray_free( f->f_sub_any );
550 if ( f->f_sub_final.bv_val != NULL ) {
551 free( f->f_sub_final.bv_val );
556 case LDAP_FILTER_AND:
558 case LDAP_FILTER_NOT:
559 for ( p = f->f_list; p != NULL; p = next ) {
565 case LDAP_FILTER_EXT:
566 mra_free( f->f_mra, 1 );
569 case SLAPD_FILTER_COMPUTED:
574 LDAP_LOG( FILTER, ERR,
575 "filter_free: unknown filter type %lu\n", f->f_choice, 0, 0 );
577 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
587 filter2bv( Filter *f, struct berval *fstr )
595 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
599 switch ( f->f_choice ) {
600 case LDAP_FILTER_EQUALITY:
601 filter_escape_value( &f->f_av_value, &tmp );
603 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
604 tmp.bv_len + ( sizeof("(=)") - 1 );
605 fstr->bv_val = malloc( fstr->bv_len + 1 );
607 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
608 f->f_av_desc->ad_cname.bv_val,
611 ber_memfree( tmp.bv_val );
615 filter_escape_value( &f->f_av_value, &tmp );
617 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
618 tmp.bv_len + ( sizeof("(>=)") - 1 );
619 fstr->bv_val = malloc( fstr->bv_len + 1 );
621 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
622 f->f_av_desc->ad_cname.bv_val,
625 ber_memfree( tmp.bv_val );
629 filter_escape_value( &f->f_av_value, &tmp );
631 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
632 tmp.bv_len + ( sizeof("(<=)") - 1 );
633 fstr->bv_val = malloc( fstr->bv_len + 1 );
635 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
636 f->f_av_desc->ad_cname.bv_val,
639 ber_memfree( tmp.bv_val );
642 case LDAP_FILTER_APPROX:
643 filter_escape_value( &f->f_av_value, &tmp );
645 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
646 tmp.bv_len + ( sizeof("(~=)") - 1 );
647 fstr->bv_val = malloc( fstr->bv_len + 1 );
649 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
650 f->f_av_desc->ad_cname.bv_val,
652 ber_memfree( tmp.bv_val );
655 case LDAP_FILTER_SUBSTRINGS:
656 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
657 ( sizeof("(=*)") - 1 );
658 fstr->bv_val = malloc( fstr->bv_len + 128 );
660 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
661 f->f_sub_desc->ad_cname.bv_val );
663 if ( f->f_sub_initial.bv_val != NULL ) {
666 filter_escape_value( &f->f_sub_initial, &tmp );
668 fstr->bv_len += tmp.bv_len;
669 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
671 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
672 /* "(attr=" */ "%s*)",
675 ber_memfree( tmp.bv_val );
678 if ( f->f_sub_any != NULL ) {
679 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
681 filter_escape_value( &f->f_sub_any[i], &tmp );
683 fstr->bv_len += tmp.bv_len + 1;
684 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
686 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
687 /* "(attr=[init]*[any*]" */ "%s*)",
689 ber_memfree( tmp.bv_val );
693 if ( f->f_sub_final.bv_val != NULL ) {
696 filter_escape_value( &f->f_sub_final, &tmp );
698 fstr->bv_len += tmp.bv_len;
699 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
701 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
702 /* "(attr=[init*][any*]" */ "%s)",
705 ber_memfree( tmp.bv_val );
710 case LDAP_FILTER_PRESENT:
711 fstr->bv_len = f->f_desc->ad_cname.bv_len +
712 ( sizeof("(=*)") - 1 );
713 fstr->bv_val = malloc( fstr->bv_len + 1 );
715 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
716 f->f_desc->ad_cname.bv_val );
719 case LDAP_FILTER_AND:
721 case LDAP_FILTER_NOT:
722 fstr->bv_len = sizeof("(%)") - 1;
723 fstr->bv_val = malloc( fstr->bv_len + 128 );
725 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
726 f->f_choice == LDAP_FILTER_AND ? '&' :
727 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
729 for ( p = f->f_list; p != NULL; p = p->f_next ) {
732 filter2bv( p, &tmp );
734 fstr->bv_len += tmp.bv_len;
735 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
737 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
738 /*"("*/ "%s)", tmp.bv_val );
740 ch_free( tmp.bv_val );
745 case LDAP_FILTER_EXT:
746 filter_escape_value( &f->f_mr_value, &tmp );
748 fstr->bv_len = f->f_mr_desc->ad_cname.bv_len +
749 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
750 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
751 tmp.bv_len + ( sizeof("(:=)") - 1 );
752 fstr->bv_val = malloc( fstr->bv_len + 1 );
754 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
755 f->f_mr_desc->ad_cname.bv_val,
756 f->f_mr_dnattrs ? ":dn" : "",
757 f->f_mr_rule_text.bv_len ? ":" : "",
758 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
760 ber_memfree( tmp.bv_val );
763 case SLAPD_FILTER_COMPUTED:
765 f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
766 f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
767 f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
769 f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
770 f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
771 f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
772 sizeof("(?=error)")-1,
777 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
782 static int filter_escape_value(
790 out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 );
793 for( i=0; i < in->bv_len ; i++ ) {
794 if( FILTER_ESCAPE(in->bv_val[i]) ) {
795 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
796 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
797 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
799 out->bv_val[out->bv_len++] = in->bv_val[i];
803 out->bv_val[out->bv_len] = '\0';
811 ValuesReturnFilter **filt,
817 ValuesReturnFilter *f;
820 LDAP_LOG( FILTER, ENTRY,
821 "get_simple_vrFilter: conn %d\n", conn->c_connid, 0, 0 );
823 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
826 tag = ber_peek_tag( ber, &len );
828 if( tag == LBER_ERROR ) {
829 *text = "error decoding filter";
830 return SLAPD_DISCONNECT;
833 f = (ValuesReturnFilter *) ch_malloc( sizeof(ValuesReturnFilter) );
839 switch ( f->f_choice ) {
840 case LDAP_FILTER_EQUALITY:
842 LDAP_LOG( FILTER, DETAIL2,
843 "get_simple_vrFilter: conn %d EQUALITY\n", conn->c_connid, 0, 0 );
845 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
847 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY, text );
848 if ( err != LDAP_SUCCESS ) {
852 assert( f->f_ava != NULL );
855 case LDAP_FILTER_SUBSTRINGS:
857 LDAP_LOG( FILTER, DETAIL1,
858 "get_simple_vrFilter: conn %d SUBSTRINGS\n", conn->c_connid, 0, 0 );
860 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
862 err = get_substring_filter( conn, ber, (Filter *)f, text );
867 LDAP_LOG( FILTER, DETAIL1,
868 "get_simple_vrFilter: conn %d GE\n", conn->c_connid, 0, 0 );
870 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
872 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
873 if ( err != LDAP_SUCCESS ) {
880 LDAP_LOG( FILTER, DETAIL1,
881 "get_simple_vrFilter: conn %d LE\n", conn->c_connid, 0, 0 );
883 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
885 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
886 if ( err != LDAP_SUCCESS ) {
891 case LDAP_FILTER_PRESENT: {
895 LDAP_LOG( FILTER, DETAIL1,
896 "get_simple_vrFilter: conn %d PRESENT\n", conn->c_connid, 0, 0 );
898 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
900 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
901 err = SLAPD_DISCONNECT;
902 *text = "error decoding filter";
907 err = slap_bv2ad( &type, &f->f_desc, text );
909 if( err != LDAP_SUCCESS ) {
910 /* unrecognized attribute description or other error */
911 f->f_choice = SLAPD_FILTER_COMPUTED;
912 f->f_result = LDAP_COMPARE_FALSE;
918 case LDAP_FILTER_APPROX:
920 LDAP_LOG( FILTER, DETAIL1,
921 "get_simple_vrFilter: conn %d APPROX\n", conn->c_connid, 0, 0 );
923 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
925 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY_APPROX, text );
926 if ( err != LDAP_SUCCESS ) {
931 case LDAP_FILTER_EXT:
933 LDAP_LOG( FILTER, DETAIL1,
934 "get_simple_vrFilter: conn %d EXTENSIBLE\n", conn->c_connid, 0, 0 );
936 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
939 err = get_mra( ber, &f->f_mra, text );
940 if ( err != LDAP_SUCCESS ) {
944 assert( f->f_mra != NULL );
948 (void) ber_scanf( ber, "x" ); /* skip the element */
950 LDAP_LOG( FILTER, ERR,
951 "get_simple_vrFilter: conn %d unknown filter type=%lu\n",
952 conn->c_connid, f->f_choice, 0 );
954 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
957 f->f_choice = SLAPD_FILTER_COMPUTED;
958 f->f_result = SLAPD_COMPARE_UNDEFINED;
962 if ( err != LDAP_SUCCESS ) {
963 if( err != SLAPD_DISCONNECT ) {
965 f->f_choice = SLAPD_FILTER_COMPUTED;
966 f->f_result = SLAPD_COMPARE_UNDEFINED;
979 LDAP_LOG( FILTER, DETAIL2,
980 "get_simple_vrFilter: conn %d exit\n", conn->c_connid, 0, 0 );
982 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
988 get_vrFilter( Connection *conn, BerElement *ber,
989 ValuesReturnFilter **f,
993 * A ValuesReturnFilter looks like this:
995 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
996 * SimpleFilterItem ::= CHOICE {
997 * equalityMatch [3] AttributeValueAssertion,
998 * substrings [4] SubstringFilter,
999 * greaterOrEqual [5] AttributeValueAssertion,
1000 * lessOrEqual [6] AttributeValueAssertion,
1001 * present [7] AttributeType,
1002 * approxMatch [8] AttributeValueAssertion,
1003 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1006 * SubstringFilter ::= SEQUENCE {
1007 * type AttributeType,
1008 * SEQUENCE OF CHOICE {
1009 * initial [0] IA5String,
1010 * any [1] IA5String,
1011 * final [2] IA5String
1015 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1016 * matchingRule [1] MatchingRuleId OPTIONAL,
1017 * type [2] AttributeDescription OPTIONAL,
1018 * matchValue [3] AssertionValue }
1021 ValuesReturnFilter **new;
1027 LDAP_LOG( FILTER, ENTRY,
1028 "get_vrFilter: conn %d start\n", conn->c_connid, 0, 0 );
1030 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1033 tag = ber_peek_tag( ber, &len );
1035 if( tag == LBER_ERROR ) {
1036 *text = "error decoding vrFilter";
1037 return SLAPD_DISCONNECT;
1040 if( tag != LBER_SEQUENCE ) {
1041 *text = "error decoding vrFilter, expect SEQUENCE tag";
1042 return SLAPD_DISCONNECT;
1046 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
1047 tag = ber_next_element( ber, &len, last ) )
1049 int err = get_simple_vrFilter( conn, ber, new, text );
1050 if ( err != LDAP_SUCCESS )
1052 new = &(*new)->f_next;
1057 LDAP_LOG( FILTER, ENTRY,
1058 "get_vrFilter: conn %d exit\n", conn->c_connid, 0, 0 );
1060 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1062 return( LDAP_SUCCESS );
1066 vrFilter_free( ValuesReturnFilter *f )
1068 ValuesReturnFilter *p, *next;
1074 for ( p = f; p != NULL; p = next ) {
1077 switch ( f->f_choice ) {
1078 case LDAP_FILTER_PRESENT:
1081 case LDAP_FILTER_EQUALITY:
1082 case LDAP_FILTER_GE:
1083 case LDAP_FILTER_LE:
1084 case LDAP_FILTER_APPROX:
1085 ava_free( f->f_ava, 1 );
1088 case LDAP_FILTER_SUBSTRINGS:
1089 if ( f->f_sub_initial.bv_val != NULL ) {
1090 free( f->f_sub_initial.bv_val );
1092 ber_bvarray_free( f->f_sub_any );
1093 if ( f->f_sub_final.bv_val != NULL ) {
1094 free( f->f_sub_final.bv_val );
1096 ch_free( f->f_sub );
1099 case LDAP_FILTER_EXT:
1100 mra_free( f->f_mra, 1 );
1103 case SLAPD_FILTER_COMPUTED:
1108 LDAP_LOG( FILTER, ERR,
1109 "filter_free: unknown filter type %lu\n", f->f_choice, 0, 0 );
1111 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1112 f->f_choice, 0, 0 );
1123 vrFilter2bv( ValuesReturnFilter *f, struct berval *fstr )
1125 ValuesReturnFilter *p;
1130 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
1134 fstr->bv_len = sizeof("()") - 1;
1135 fstr->bv_val = malloc( fstr->bv_len + 128 );
1137 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1139 for ( p = f; p != NULL; p = p->f_next ) {
1142 simple_vrFilter2bv( p, &tmp );
1144 fstr->bv_len += tmp.bv_len;
1145 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1147 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1148 /*"("*/ "%s)", tmp.bv_val );
1150 ch_free( tmp.bv_val );
1155 simple_vrFilter2bv( ValuesReturnFilter *f, struct berval *fstr )
1161 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
1165 switch ( f->f_choice ) {
1166 case LDAP_FILTER_EQUALITY:
1167 filter_escape_value( &f->f_av_value, &tmp );
1169 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
1170 tmp.bv_len + ( sizeof("(=)") - 1 );
1171 fstr->bv_val = malloc( fstr->bv_len + 1 );
1173 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1174 f->f_av_desc->ad_cname.bv_val,
1177 ber_memfree( tmp.bv_val );
1180 case LDAP_FILTER_GE:
1181 filter_escape_value( &f->f_av_value, &tmp );
1183 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
1184 tmp.bv_len + ( sizeof("(>=)") - 1 );
1185 fstr->bv_val = malloc( fstr->bv_len + 1 );
1187 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1188 f->f_av_desc->ad_cname.bv_val,
1191 ber_memfree( tmp.bv_val );
1194 case LDAP_FILTER_LE:
1195 filter_escape_value( &f->f_av_value, &tmp );
1197 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
1198 tmp.bv_len + ( sizeof("(<=)") - 1 );
1199 fstr->bv_val = malloc( fstr->bv_len + 1 );
1201 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1202 f->f_av_desc->ad_cname.bv_val,
1205 ber_memfree( tmp.bv_val );
1208 case LDAP_FILTER_APPROX:
1209 filter_escape_value( &f->f_av_value, &tmp );
1211 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
1212 tmp.bv_len + ( sizeof("(~=)") - 1 );
1213 fstr->bv_val = malloc( fstr->bv_len + 1 );
1215 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1216 f->f_av_desc->ad_cname.bv_val,
1218 ber_memfree( tmp.bv_val );
1221 case LDAP_FILTER_SUBSTRINGS:
1222 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
1223 ( sizeof("(=*)") - 1 );
1224 fstr->bv_val = malloc( fstr->bv_len + 128 );
1226 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1227 f->f_sub_desc->ad_cname.bv_val );
1229 if ( f->f_sub_initial.bv_val != NULL ) {
1232 filter_escape_value( &f->f_sub_initial, &tmp );
1234 fstr->bv_len += tmp.bv_len;
1235 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1237 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1238 /* "(attr=" */ "%s*)",
1241 ber_memfree( tmp.bv_val );
1244 if ( f->f_sub_any != NULL ) {
1246 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1248 filter_escape_value( &f->f_sub_any[i], &tmp );
1250 fstr->bv_len += tmp.bv_len + 1;
1251 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1253 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1254 /* "(attr=[init]*[any*]" */ "%s*)",
1256 ber_memfree( tmp.bv_val );
1260 if ( f->f_sub_final.bv_val != NULL ) {
1263 filter_escape_value( &f->f_sub_final, &tmp );
1265 fstr->bv_len += tmp.bv_len;
1266 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1268 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1269 /* "(attr=[init*][any*]" */ "%s)",
1272 ber_memfree( tmp.bv_val );
1277 case LDAP_FILTER_PRESENT:
1278 fstr->bv_len = f->f_desc->ad_cname.bv_len +
1279 ( sizeof("(=*)") - 1 );
1280 fstr->bv_val = malloc( fstr->bv_len + 1 );
1282 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1283 f->f_desc->ad_cname.bv_val );
1286 case LDAP_FILTER_EXT:
1287 filter_escape_value( &f->f_mr_value, &tmp );
1289 fstr->bv_len = f->f_mr_desc->ad_cname.bv_len +
1290 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1291 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
1292 tmp.bv_len + ( sizeof("(:=)") - 1 );
1293 fstr->bv_val = malloc( fstr->bv_len + 1 );
1295 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1296 f->f_mr_desc->ad_cname.bv_val,
1297 f->f_mr_dnattrs ? ":dn" : "",
1298 f->f_mr_rule_text.bv_len ? ":" : "",
1299 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
1301 ber_memfree( tmp.bv_val );
1304 case SLAPD_FILTER_COMPUTED:
1306 f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1307 f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1308 f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
1310 f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1311 f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1312 f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
1313 sizeof("(?=error)")-1,
1318 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
1324 get_substring_vrFilter(
1327 ValuesReturnFilter *f,
1333 struct berval value;
1336 *text = "error decoding filter";
1339 LDAP_LOG( FILTER, ENTRY,
1340 "get_substring_filter: conn %d begin\n", conn->c_connid, 0, 0 );
1342 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
1344 if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
1345 return SLAPD_DISCONNECT;
1348 f->f_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
1349 f->f_sub_desc = NULL;
1350 rc = slap_bv2ad( &bv, &f->f_sub_desc, text );
1352 if( rc != LDAP_SUCCESS ) {
1354 ch_free( f->f_sub );
1355 f->f_choice = SLAPD_FILTER_COMPUTED;
1356 f->f_result = SLAPD_COMPARE_UNDEFINED;
1357 return LDAP_SUCCESS;
1360 f->f_sub_initial.bv_val = NULL;
1361 f->f_sub_any = NULL;
1362 f->f_sub_final.bv_val = NULL;
1364 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
1365 tag = ber_next_element( ber, &len, last ) )
1369 rc = ber_scanf( ber, "m", &value );
1370 if ( rc == LBER_ERROR ) {
1371 rc = SLAPD_DISCONNECT;
1375 if ( value.bv_val == NULL || value.bv_len == 0 ) {
1376 rc = LDAP_INVALID_SYNTAX;
1381 case LDAP_SUBSTRING_INITIAL:
1382 usage = SLAP_MR_SUBSTR_INITIAL;
1385 case LDAP_SUBSTRING_ANY:
1386 usage = SLAP_MR_SUBSTR_ANY;
1389 case LDAP_SUBSTRING_FINAL:
1390 usage = SLAP_MR_SUBSTR_FINAL;
1394 rc = LDAP_PROTOCOL_ERROR;
1397 LDAP_LOG( FILTER, ERR,
1398 "get_filter_substring: conn %d unknown substring choice=%ld\n",
1399 conn->c_connid, (long)tag, 0 );
1401 Debug( LDAP_DEBUG_FILTER,
1402 " unknown substring choice=%ld\n",
1408 /* valiate using equality matching rule validator! */
1409 rc = value_validate( f->f_sub_desc->ad_type->sat_equality,
1411 if( rc != LDAP_SUCCESS ) {
1415 rc = value_normalize( f->f_sub_desc, usage,
1416 &value, &bv, text );
1417 if( rc != LDAP_SUCCESS ) {
1423 rc = LDAP_PROTOCOL_ERROR;
1426 case LDAP_SUBSTRING_INITIAL:
1428 LDAP_LOG( FILTER, DETAIL1,
1429 "get_substring_filter: conn %d INITIAL\n",
1430 conn->c_connid, 0, 0 );
1432 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
1435 if ( f->f_sub_initial.bv_val != NULL
1436 || f->f_sub_any != NULL
1437 || f->f_sub_final.bv_val != NULL )
1439 free( value.bv_val );
1443 f->f_sub_initial = value;
1446 case LDAP_SUBSTRING_ANY:
1448 LDAP_LOG( FILTER, DETAIL1,
1449 "get_substring_filter: conn %d ANY\n", conn->c_connid, 0, 0 );
1451 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
1454 if ( f->f_sub_final.bv_val != NULL ) {
1455 free( value.bv_val );
1459 ber_bvarray_add( &f->f_sub_any, &value );
1462 case LDAP_SUBSTRING_FINAL:
1464 LDAP_LOG( FILTER, DETAIL1,
1465 "get_substring_filter: conn %d FINAL\n", conn->c_connid, 0, 0 );
1467 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
1470 if ( f->f_sub_final.bv_val != NULL ) {
1471 free( value.bv_val );
1475 f->f_sub_final = value;
1480 LDAP_LOG( FILTER, INFO,
1481 "get_substring_filter: conn %d unknown substring type %ld\n",
1482 conn->c_connid, (long)tag, 0 );
1484 Debug( LDAP_DEBUG_FILTER,
1485 " unknown substring type=%ld\n",
1489 free( value.bv_val );
1493 LDAP_LOG( FILTER, INFO,
1494 "get_substring_filter: conn %d error %ld\n",
1495 conn->c_connid, (long)rc, 0 );
1497 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
1500 free( f->f_sub_initial.bv_val );
1501 ber_bvarray_free( f->f_sub_any );
1502 free( f->f_sub_final.bv_val );
1503 ch_free( f->f_sub );
1509 LDAP_LOG( FILTER, ENTRY,
1510 "get_substring_filter: conn %d exit\n", conn->c_connid, 0, 0 );
1512 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
1514 return( LDAP_SUCCESS );