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: {
747 filter_escape_value( &f->f_mr_value, &tmp );
749 if ( f->f_mr_desc ) {
750 ad = f->f_mr_desc->ad_cname;
756 fstr->bv_len = ad.bv_len +
757 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
758 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
759 tmp.bv_len + ( sizeof("(:=)") - 1 );
760 fstr->bv_val = malloc( fstr->bv_len + 1 );
762 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
764 f->f_mr_dnattrs ? ":dn" : "",
765 f->f_mr_rule_text.bv_len ? ":" : "",
766 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
768 ber_memfree( tmp.bv_val );
771 case SLAPD_FILTER_COMPUTED:
773 f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
774 f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
775 f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
777 f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
778 f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
779 f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
780 sizeof("(?=error)")-1,
785 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
790 static int filter_escape_value(
798 out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 );
801 for( i=0; i < in->bv_len ; i++ ) {
802 if( FILTER_ESCAPE(in->bv_val[i]) ) {
803 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
804 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
805 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
807 out->bv_val[out->bv_len++] = in->bv_val[i];
811 out->bv_val[out->bv_len] = '\0';
819 ValuesReturnFilter **filt,
825 ValuesReturnFilter *f;
828 LDAP_LOG( FILTER, ENTRY,
829 "get_simple_vrFilter: conn %d\n", conn->c_connid, 0, 0 );
831 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
834 tag = ber_peek_tag( ber, &len );
836 if( tag == LBER_ERROR ) {
837 *text = "error decoding filter";
838 return SLAPD_DISCONNECT;
841 f = (ValuesReturnFilter *) ch_malloc( sizeof(ValuesReturnFilter) );
847 switch ( f->f_choice ) {
848 case LDAP_FILTER_EQUALITY:
850 LDAP_LOG( FILTER, DETAIL2,
851 "get_simple_vrFilter: conn %d EQUALITY\n", conn->c_connid, 0, 0 );
853 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
855 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY, text );
856 if ( err != LDAP_SUCCESS ) {
860 assert( f->f_ava != NULL );
863 case LDAP_FILTER_SUBSTRINGS:
865 LDAP_LOG( FILTER, DETAIL1,
866 "get_simple_vrFilter: conn %d SUBSTRINGS\n", conn->c_connid, 0, 0 );
868 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
870 err = get_substring_filter( conn, ber, (Filter *)f, text );
875 LDAP_LOG( FILTER, DETAIL1,
876 "get_simple_vrFilter: conn %d GE\n", conn->c_connid, 0, 0 );
878 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
880 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
881 if ( err != LDAP_SUCCESS ) {
888 LDAP_LOG( FILTER, DETAIL1,
889 "get_simple_vrFilter: conn %d LE\n", conn->c_connid, 0, 0 );
891 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
893 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
894 if ( err != LDAP_SUCCESS ) {
899 case LDAP_FILTER_PRESENT: {
903 LDAP_LOG( FILTER, DETAIL1,
904 "get_simple_vrFilter: conn %d PRESENT\n", conn->c_connid, 0, 0 );
906 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
908 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
909 err = SLAPD_DISCONNECT;
910 *text = "error decoding filter";
915 err = slap_bv2ad( &type, &f->f_desc, text );
917 if( err != LDAP_SUCCESS ) {
918 /* unrecognized attribute description or other error */
919 f->f_choice = SLAPD_FILTER_COMPUTED;
920 f->f_result = LDAP_COMPARE_FALSE;
926 case LDAP_FILTER_APPROX:
928 LDAP_LOG( FILTER, DETAIL1,
929 "get_simple_vrFilter: conn %d APPROX\n", conn->c_connid, 0, 0 );
931 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
933 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY_APPROX, text );
934 if ( err != LDAP_SUCCESS ) {
939 case LDAP_FILTER_EXT:
941 LDAP_LOG( FILTER, DETAIL1,
942 "get_simple_vrFilter: conn %d EXTENSIBLE\n", conn->c_connid, 0, 0 );
944 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
947 err = get_mra( ber, &f->f_mra, text );
948 if ( err != LDAP_SUCCESS ) {
952 assert( f->f_mra != NULL );
956 (void) ber_scanf( ber, "x" ); /* skip the element */
958 LDAP_LOG( FILTER, ERR,
959 "get_simple_vrFilter: conn %d unknown filter type=%lu\n",
960 conn->c_connid, f->f_choice, 0 );
962 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
965 f->f_choice = SLAPD_FILTER_COMPUTED;
966 f->f_result = SLAPD_COMPARE_UNDEFINED;
970 if ( err != LDAP_SUCCESS ) {
971 if( err != SLAPD_DISCONNECT ) {
973 f->f_choice = SLAPD_FILTER_COMPUTED;
974 f->f_result = SLAPD_COMPARE_UNDEFINED;
987 LDAP_LOG( FILTER, DETAIL2,
988 "get_simple_vrFilter: conn %d exit\n", conn->c_connid, 0, 0 );
990 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
996 get_vrFilter( Connection *conn, BerElement *ber,
997 ValuesReturnFilter **f,
1001 * A ValuesReturnFilter looks like this:
1003 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1004 * SimpleFilterItem ::= CHOICE {
1005 * equalityMatch [3] AttributeValueAssertion,
1006 * substrings [4] SubstringFilter,
1007 * greaterOrEqual [5] AttributeValueAssertion,
1008 * lessOrEqual [6] AttributeValueAssertion,
1009 * present [7] AttributeType,
1010 * approxMatch [8] AttributeValueAssertion,
1011 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1014 * SubstringFilter ::= SEQUENCE {
1015 * type AttributeType,
1016 * SEQUENCE OF CHOICE {
1017 * initial [0] IA5String,
1018 * any [1] IA5String,
1019 * final [2] IA5String
1023 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1024 * matchingRule [1] MatchingRuleId OPTIONAL,
1025 * type [2] AttributeDescription OPTIONAL,
1026 * matchValue [3] AssertionValue }
1029 ValuesReturnFilter **new;
1035 LDAP_LOG( FILTER, ENTRY,
1036 "get_vrFilter: conn %d start\n", conn->c_connid, 0, 0 );
1038 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1041 tag = ber_peek_tag( ber, &len );
1043 if( tag == LBER_ERROR ) {
1044 *text = "error decoding vrFilter";
1045 return SLAPD_DISCONNECT;
1048 if( tag != LBER_SEQUENCE ) {
1049 *text = "error decoding vrFilter, expect SEQUENCE tag";
1050 return SLAPD_DISCONNECT;
1054 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
1055 tag = ber_next_element( ber, &len, last ) )
1057 int err = get_simple_vrFilter( conn, ber, new, text );
1058 if ( err != LDAP_SUCCESS )
1060 new = &(*new)->f_next;
1065 LDAP_LOG( FILTER, ENTRY,
1066 "get_vrFilter: conn %d exit\n", conn->c_connid, 0, 0 );
1068 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1070 return( LDAP_SUCCESS );
1074 vrFilter_free( ValuesReturnFilter *f )
1076 ValuesReturnFilter *p, *next;
1082 for ( p = f; p != NULL; p = next ) {
1085 switch ( f->f_choice ) {
1086 case LDAP_FILTER_PRESENT:
1089 case LDAP_FILTER_EQUALITY:
1090 case LDAP_FILTER_GE:
1091 case LDAP_FILTER_LE:
1092 case LDAP_FILTER_APPROX:
1093 ava_free( f->f_ava, 1 );
1096 case LDAP_FILTER_SUBSTRINGS:
1097 if ( f->f_sub_initial.bv_val != NULL ) {
1098 free( f->f_sub_initial.bv_val );
1100 ber_bvarray_free( f->f_sub_any );
1101 if ( f->f_sub_final.bv_val != NULL ) {
1102 free( f->f_sub_final.bv_val );
1104 ch_free( f->f_sub );
1107 case LDAP_FILTER_EXT:
1108 mra_free( f->f_mra, 1 );
1111 case SLAPD_FILTER_COMPUTED:
1116 LDAP_LOG( FILTER, ERR,
1117 "filter_free: unknown filter type %lu\n", f->f_choice, 0, 0 );
1119 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1120 f->f_choice, 0, 0 );
1131 vrFilter2bv( ValuesReturnFilter *f, struct berval *fstr )
1133 ValuesReturnFilter *p;
1138 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
1142 fstr->bv_len = sizeof("()") - 1;
1143 fstr->bv_val = malloc( fstr->bv_len + 128 );
1145 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1147 for ( p = f; p != NULL; p = p->f_next ) {
1150 simple_vrFilter2bv( p, &tmp );
1152 fstr->bv_len += tmp.bv_len;
1153 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1155 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1156 /*"("*/ "%s)", tmp.bv_val );
1158 ch_free( tmp.bv_val );
1163 simple_vrFilter2bv( ValuesReturnFilter *f, struct berval *fstr )
1169 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
1173 switch ( f->f_choice ) {
1174 case LDAP_FILTER_EQUALITY:
1175 filter_escape_value( &f->f_av_value, &tmp );
1177 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
1178 tmp.bv_len + ( sizeof("(=)") - 1 );
1179 fstr->bv_val = malloc( fstr->bv_len + 1 );
1181 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1182 f->f_av_desc->ad_cname.bv_val,
1185 ber_memfree( tmp.bv_val );
1188 case LDAP_FILTER_GE:
1189 filter_escape_value( &f->f_av_value, &tmp );
1191 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
1192 tmp.bv_len + ( sizeof("(>=)") - 1 );
1193 fstr->bv_val = malloc( fstr->bv_len + 1 );
1195 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1196 f->f_av_desc->ad_cname.bv_val,
1199 ber_memfree( tmp.bv_val );
1202 case LDAP_FILTER_LE:
1203 filter_escape_value( &f->f_av_value, &tmp );
1205 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
1206 tmp.bv_len + ( sizeof("(<=)") - 1 );
1207 fstr->bv_val = malloc( fstr->bv_len + 1 );
1209 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1210 f->f_av_desc->ad_cname.bv_val,
1213 ber_memfree( tmp.bv_val );
1216 case LDAP_FILTER_APPROX:
1217 filter_escape_value( &f->f_av_value, &tmp );
1219 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
1220 tmp.bv_len + ( sizeof("(~=)") - 1 );
1221 fstr->bv_val = malloc( fstr->bv_len + 1 );
1223 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1224 f->f_av_desc->ad_cname.bv_val,
1226 ber_memfree( tmp.bv_val );
1229 case LDAP_FILTER_SUBSTRINGS:
1230 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
1231 ( sizeof("(=*)") - 1 );
1232 fstr->bv_val = malloc( fstr->bv_len + 128 );
1234 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1235 f->f_sub_desc->ad_cname.bv_val );
1237 if ( f->f_sub_initial.bv_val != NULL ) {
1240 filter_escape_value( &f->f_sub_initial, &tmp );
1242 fstr->bv_len += tmp.bv_len;
1243 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1245 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1246 /* "(attr=" */ "%s*)",
1249 ber_memfree( tmp.bv_val );
1252 if ( f->f_sub_any != NULL ) {
1254 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1256 filter_escape_value( &f->f_sub_any[i], &tmp );
1258 fstr->bv_len += tmp.bv_len + 1;
1259 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1261 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1262 /* "(attr=[init]*[any*]" */ "%s*)",
1264 ber_memfree( tmp.bv_val );
1268 if ( f->f_sub_final.bv_val != NULL ) {
1271 filter_escape_value( &f->f_sub_final, &tmp );
1273 fstr->bv_len += tmp.bv_len;
1274 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
1276 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1277 /* "(attr=[init*][any*]" */ "%s)",
1280 ber_memfree( tmp.bv_val );
1285 case LDAP_FILTER_PRESENT:
1286 fstr->bv_len = f->f_desc->ad_cname.bv_len +
1287 ( sizeof("(=*)") - 1 );
1288 fstr->bv_val = malloc( fstr->bv_len + 1 );
1290 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1291 f->f_desc->ad_cname.bv_val );
1294 case LDAP_FILTER_EXT: {
1296 filter_escape_value( &f->f_mr_value, &tmp );
1298 if ( f->f_mr_desc ) {
1299 ad = f->f_mr_desc->ad_cname;
1305 fstr->bv_len = ad.bv_len +
1306 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1307 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
1308 tmp.bv_len + ( sizeof("(:=)") - 1 );
1309 fstr->bv_val = malloc( fstr->bv_len + 1 );
1311 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1313 f->f_mr_dnattrs ? ":dn" : "",
1314 f->f_mr_rule_text.bv_len ? ":" : "",
1315 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
1318 ber_memfree( tmp.bv_val );
1321 case SLAPD_FILTER_COMPUTED:
1323 f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1324 f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1325 f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
1327 f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1328 f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1329 f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
1330 sizeof("(?=error)")-1,
1335 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
1341 get_substring_vrFilter(
1344 ValuesReturnFilter *f,
1350 struct berval value;
1353 *text = "error decoding filter";
1356 LDAP_LOG( FILTER, ENTRY,
1357 "get_substring_filter: conn %d begin\n", conn->c_connid, 0, 0 );
1359 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
1361 if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
1362 return SLAPD_DISCONNECT;
1365 f->f_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
1366 f->f_sub_desc = NULL;
1367 rc = slap_bv2ad( &bv, &f->f_sub_desc, text );
1369 if( rc != LDAP_SUCCESS ) {
1371 ch_free( f->f_sub );
1372 f->f_choice = SLAPD_FILTER_COMPUTED;
1373 f->f_result = SLAPD_COMPARE_UNDEFINED;
1374 return LDAP_SUCCESS;
1377 f->f_sub_initial.bv_val = NULL;
1378 f->f_sub_any = NULL;
1379 f->f_sub_final.bv_val = NULL;
1381 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
1382 tag = ber_next_element( ber, &len, last ) )
1386 rc = ber_scanf( ber, "m", &value );
1387 if ( rc == LBER_ERROR ) {
1388 rc = SLAPD_DISCONNECT;
1392 if ( value.bv_val == NULL || value.bv_len == 0 ) {
1393 rc = LDAP_INVALID_SYNTAX;
1398 case LDAP_SUBSTRING_INITIAL:
1399 usage = SLAP_MR_SUBSTR_INITIAL;
1402 case LDAP_SUBSTRING_ANY:
1403 usage = SLAP_MR_SUBSTR_ANY;
1406 case LDAP_SUBSTRING_FINAL:
1407 usage = SLAP_MR_SUBSTR_FINAL;
1411 rc = LDAP_PROTOCOL_ERROR;
1414 LDAP_LOG( FILTER, ERR,
1415 "get_filter_substring: conn %d unknown substring choice=%ld\n",
1416 conn->c_connid, (long)tag, 0 );
1418 Debug( LDAP_DEBUG_FILTER,
1419 " unknown substring choice=%ld\n",
1425 /* valiate using equality matching rule validator! */
1426 rc = value_validate( f->f_sub_desc->ad_type->sat_equality,
1428 if( rc != LDAP_SUCCESS ) {
1432 rc = value_normalize( f->f_sub_desc, usage,
1433 &value, &bv, text );
1434 if( rc != LDAP_SUCCESS ) {
1440 rc = LDAP_PROTOCOL_ERROR;
1443 case LDAP_SUBSTRING_INITIAL:
1445 LDAP_LOG( FILTER, DETAIL1,
1446 "get_substring_filter: conn %d INITIAL\n",
1447 conn->c_connid, 0, 0 );
1449 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
1452 if ( f->f_sub_initial.bv_val != NULL
1453 || f->f_sub_any != NULL
1454 || f->f_sub_final.bv_val != NULL )
1456 free( value.bv_val );
1460 f->f_sub_initial = value;
1463 case LDAP_SUBSTRING_ANY:
1465 LDAP_LOG( FILTER, DETAIL1,
1466 "get_substring_filter: conn %d ANY\n", conn->c_connid, 0, 0 );
1468 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
1471 if ( f->f_sub_final.bv_val != NULL ) {
1472 free( value.bv_val );
1476 ber_bvarray_add( &f->f_sub_any, &value );
1479 case LDAP_SUBSTRING_FINAL:
1481 LDAP_LOG( FILTER, DETAIL1,
1482 "get_substring_filter: conn %d FINAL\n", conn->c_connid, 0, 0 );
1484 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
1487 if ( f->f_sub_final.bv_val != NULL ) {
1488 free( value.bv_val );
1492 f->f_sub_final = value;
1497 LDAP_LOG( FILTER, INFO,
1498 "get_substring_filter: conn %d unknown substring type %ld\n",
1499 conn->c_connid, (long)tag, 0 );
1501 Debug( LDAP_DEBUG_FILTER,
1502 " unknown substring type=%ld\n",
1506 free( value.bv_val );
1510 LDAP_LOG( FILTER, INFO,
1511 "get_substring_filter: conn %d error %ld\n",
1512 conn->c_connid, (long)rc, 0 );
1514 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
1517 free( f->f_sub_initial.bv_val );
1518 ber_bvarray_free( f->f_sub_any );
1519 free( f->f_sub_final.bv_val );
1520 ch_free( f->f_sub );
1526 LDAP_LOG( FILTER, ENTRY,
1527 "get_substring_filter: conn %d exit\n", conn->c_connid, 0, 0 );
1529 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
1531 return( LDAP_SUCCESS );
1534 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1535 static int filter_has_subordinates_list(
1539 * FIXME: we could detect the need to filter
1540 * for hasSubordinates when parsing the filter ...
1544 filter_has_subordinates_list(
1549 for ( f = fl; f != NULL; f = f->f_next ) {
1552 rc = filter_has_subordinates( f );
1563 filter_has_subordinates(
1566 AttributeDescription *ad = NULL;
1568 switch ( f->f_choice ) {
1569 case LDAP_FILTER_PRESENT:
1573 case LDAP_FILTER_EQUALITY:
1574 case LDAP_FILTER_APPROX:
1575 case LDAP_FILTER_GE:
1576 case LDAP_FILTER_LE:
1577 ad = f->f_ava->aa_desc;
1580 case LDAP_FILTER_SUBSTRINGS:
1584 case LDAP_FILTER_EXT:
1585 /* could be null; however here it is harmless */
1586 ad = f->f_mra->ma_desc;
1589 case LDAP_FILTER_NOT:
1590 return filter_has_subordinates( f->f_not );
1592 case LDAP_FILTER_AND:
1593 return filter_has_subordinates_list( f->f_and );
1595 case LDAP_FILTER_OR:
1596 return filter_has_subordinates_list( f->f_or );
1598 case SLAPD_FILTER_COMPUTED:
1606 * this means a new type of filter has been implemented,
1607 * which is not handled yet in this function; we should
1608 * issue a developer's warning, e.g. an assertion
1614 if ( ad == slap_schema.si_ad_hasSubordinates ) {
1621 #endif /* SLAP_X_FILTER_HASSUBORDINATES */