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(
46 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY, "get_filter: conn %d\n",
49 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
52 * A filter looks like this coming in:
54 * and [0] SET OF Filter,
55 * or [1] SET OF Filter,
57 * equalityMatch [3] AttributeValueAssertion,
58 * substrings [4] SubstringFilter,
59 * greaterOrEqual [5] AttributeValueAssertion,
60 * lessOrEqual [6] AttributeValueAssertion,
61 * present [7] AttributeType,,
62 * approxMatch [8] AttributeValueAssertion
63 * extensibleMatch [9] MatchingRuleAssertion
66 * SubstringFilter ::= SEQUENCE {
68 * SEQUENCE OF CHOICE {
69 * initial [0] IA5String,
75 * MatchingRuleAssertion ::= SEQUENCE {
76 * matchingRule [1] MatchingRuleId OPTIONAL,
77 * type [2] AttributeDescription OPTIONAL,
78 * matchValue [3] AssertionValue,
79 * dnAttributes [4] BOOLEAN DEFAULT FALSE
84 tag = ber_peek_tag( ber, &len );
86 if( tag == LBER_ERROR ) {
87 *text = "error decoding filter";
88 return SLAPD_DISCONNECT;
91 f = (Filter *) ch_malloc( sizeof(Filter) );
97 switch ( f->f_choice ) {
98 case LDAP_FILTER_EQUALITY:
100 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL2,
101 "get_filter: conn %d EQUALITY\n", conn->c_connid ));
103 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
105 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY, text );
106 if ( err != LDAP_SUCCESS ) {
110 assert( f->f_ava != NULL );
113 case LDAP_FILTER_SUBSTRINGS:
115 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
116 "get_filter: conn %d SUBSTRINGS\n", conn->c_connid ));
118 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
120 err = get_substring_filter( conn, ber, f, text );
125 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
126 "get_filter: conn %d GE\n", conn->c_connid ));
128 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
130 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
131 if ( err != LDAP_SUCCESS ) {
138 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
139 "get_filter: conn %d LE\n", conn->c_connid ));
141 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
143 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
144 if ( err != LDAP_SUCCESS ) {
149 case LDAP_FILTER_PRESENT: {
153 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
154 "get_filter: conn %d PRESENT\n", conn->c_connid ));
156 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
158 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
159 err = SLAPD_DISCONNECT;
160 *text = "error decoding filter";
165 err = slap_bv2ad( &type, &f->f_desc, text );
167 if( err != LDAP_SUCCESS ) {
168 /* unrecognized attribute description or other error */
169 f->f_choice = SLAPD_FILTER_COMPUTED;
170 f->f_result = LDAP_COMPARE_FALSE;
176 case LDAP_FILTER_APPROX:
178 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
179 "get_filter: conn %d APPROX\n", conn->c_connid ));
181 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
183 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY_APPROX, text );
184 if ( err != LDAP_SUCCESS ) {
189 case LDAP_FILTER_AND:
191 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
192 "get_filter: conn %d AND\n", conn->c_connid ));
194 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
196 err = get_filter_list( conn, ber, &f->f_and, text );
197 if ( err != LDAP_SUCCESS ) {
204 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
205 "get_filter: conn %d OR\n", conn->c_connid ));
207 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
209 err = get_filter_list( conn, ber, &f->f_or, text );
210 if ( err != LDAP_SUCCESS ) {
215 case LDAP_FILTER_NOT:
217 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
218 "get_filter: conn %d NOT\n", conn->c_connid ));
220 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
222 (void) ber_skip_tag( ber, &len );
223 err = get_filter( conn, ber, &f->f_not, text );
224 if ( err != LDAP_SUCCESS ) {
229 case LDAP_FILTER_EXT:
231 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
232 "get_filter: conn %d EXTENSIBLE\n", conn->c_connid ));
234 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
237 err = get_mra( ber, &f->f_mra, text );
238 if ( err != LDAP_SUCCESS ) {
242 assert( f->f_mra != NULL );
247 (void) ber_scanf( ber, "x" ); /* skip the element */
249 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
250 "get_filter: conn %d unknown filter type=%lu\n",
251 conn->c_connid, f->f_choice ));
253 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
256 f->f_choice = SLAPD_FILTER_COMPUTED;
257 f->f_result = SLAPD_COMPARE_UNDEFINED;
261 if ( err != LDAP_SUCCESS ) {
262 if( err != SLAPD_DISCONNECT ) {
264 f->f_choice = SLAPD_FILTER_COMPUTED;
265 f->f_result = SLAPD_COMPARE_UNDEFINED;
277 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL2,
278 "get_filter: conn %d exit\n", conn->c_connid ));
280 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
286 get_filter_list( Connection *conn, BerElement *ber,
297 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
298 "get_filter_list: conn %d start\n", conn->c_connid ));
300 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
303 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
304 tag = ber_next_element( ber, &len, last ) )
306 err = get_filter( conn, ber, new, text );
307 if ( err != LDAP_SUCCESS )
309 new = &(*new)->f_next;
314 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
315 "get_filter_list: conn %d exit\n", conn->c_connid ));
317 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
319 return( LDAP_SUCCESS );
323 get_substring_filter(
333 struct berval escaped;
336 *text = "error decoding filter";
339 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
340 "get_substring_filter: conn %d begin\n", conn->c_connid ));
342 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
344 if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
345 return SLAPD_DISCONNECT;
348 f->f_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
349 f->f_sub_desc = NULL;
350 rc = slap_bv2ad( &bv, &f->f_sub_desc, text );
352 if( rc != LDAP_SUCCESS ) {
355 f->f_choice = SLAPD_FILTER_COMPUTED;
356 f->f_result = SLAPD_COMPARE_UNDEFINED;
360 f->f_sub_initial.bv_val = NULL;
362 f->f_sub_final.bv_val = NULL;
364 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
365 tag = ber_next_element( ber, &len, last ) )
369 rc = ber_scanf( ber, "m", &value );
370 if ( rc == LBER_ERROR ) {
371 rc = SLAPD_DISCONNECT;
375 if ( value.bv_val == NULL || value.bv_len == 0 ) {
376 rc = LDAP_INVALID_SYNTAX;
381 case LDAP_SUBSTRING_INITIAL:
382 usage = SLAP_MR_SUBSTR_INITIAL;
385 case LDAP_SUBSTRING_ANY:
386 usage = SLAP_MR_SUBSTR_ANY;
389 case LDAP_SUBSTRING_FINAL:
390 usage = SLAP_MR_SUBSTR_FINAL;
394 rc = LDAP_PROTOCOL_ERROR;
397 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
398 "get_filter_substring: conn %d unknown substring choice=%ld\n",
399 conn->c_connid, (long)tag ));
401 Debug( LDAP_DEBUG_FILTER,
402 " unknown substring choice=%ld\n",
408 /* valiate using equality matching rule validator! */
409 rc = value_validate( f->f_sub_desc->ad_type->sat_equality,
411 if( rc != LDAP_SUCCESS ) {
415 rc = value_normalize( f->f_sub_desc, usage,
417 if( rc != LDAP_SUCCESS ) {
423 rc = LDAP_PROTOCOL_ERROR;
426 case LDAP_SUBSTRING_INITIAL:
428 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
429 "get_substring_filter: conn %d INITIAL\n",
432 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
435 if ( f->f_sub_initial.bv_val != NULL
436 || f->f_sub_any != NULL
437 || f->f_sub_final.bv_val != NULL )
439 free( value.bv_val );
443 f->f_sub_initial = value;
446 case LDAP_SUBSTRING_ANY:
448 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
449 "get_substring_filter: conn %d ANY\n",
452 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
455 if ( f->f_sub_final.bv_val != NULL ) {
456 free( value.bv_val );
460 ber_bvarray_add( &f->f_sub_any, &value );
463 case LDAP_SUBSTRING_FINAL:
465 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
466 "get_substring_filter: conn %d FINAL\n",
469 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
472 if ( f->f_sub_final.bv_val != NULL ) {
473 free( value.bv_val );
477 f->f_sub_final = value;
482 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
483 "get_substring_filter: conn %d unknown substring type %ld\n",
484 conn->c_connid, (long)tag ));
486 Debug( LDAP_DEBUG_FILTER,
487 " unknown substring type=%ld\n",
491 free( value.bv_val );
495 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
496 "get_substring_filter: conn %d error %ld\n",
497 conn->c_connid, (long)rc ));
499 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
502 free( f->f_sub_initial.bv_val );
503 ber_bvarray_free( f->f_sub_any );
504 free( f->f_sub_final.bv_val );
511 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
512 "get_substring_filter: conn %d exit\n", conn->c_connid ));
514 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
516 return( LDAP_SUCCESS );
520 filter_free( Filter *f )
528 switch ( f->f_choice ) {
529 case LDAP_FILTER_PRESENT:
532 case LDAP_FILTER_EQUALITY:
535 case LDAP_FILTER_APPROX:
536 ava_free( f->f_ava, 1 );
539 case LDAP_FILTER_SUBSTRINGS:
540 if ( f->f_sub_initial.bv_val != NULL ) {
541 free( f->f_sub_initial.bv_val );
543 ber_bvarray_free( f->f_sub_any );
544 if ( f->f_sub_final.bv_val != NULL ) {
545 free( f->f_sub_final.bv_val );
550 case LDAP_FILTER_AND:
552 case LDAP_FILTER_NOT:
553 for ( p = f->f_list; p != NULL; p = next ) {
559 case LDAP_FILTER_EXT:
560 mra_free( f->f_mra, 1 );
563 case SLAPD_FILTER_COMPUTED:
568 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
569 "filter_free: unknown filter type %lu\n", f->f_choice ));
571 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
582 filter_print( Filter *f )
586 struct berval escaped;
589 fprintf( stderr, "No filter!" );
592 switch ( f->f_choice ) {
593 case LDAP_FILTER_EQUALITY:
594 filter_escape_value( &f->f_av_value, &escaped );
595 fprintf( stderr, "(%s=%s)",
596 f->f_av_desc->ad_cname.bv_val,
598 ber_memfree( escaped.bv_val );
602 filter_escape_value( &f->f_av_value, &escaped );
603 fprintf( stderr, "(%s>=%s)",
604 f->f_av_desc->ad_cname.bv_val,
606 ber_memfree( escaped.bv_val );
610 filter_escape_value( &f->f_av_value, &escaped );
611 fprintf( stderr, "(%s<=%s)",
612 f->f_ava->aa_desc->ad_cname.bv_val,
614 ber_memfree( escaped.bv_val );
617 case LDAP_FILTER_APPROX:
618 filter_escape_value( &f->f_av_value, &escaped );
619 fprintf( stderr, "(%s~=%s)",
620 f->f_ava->aa_desc->ad_cname.bv_val,
622 ber_memfree( escaped.bv_val );
625 case LDAP_FILTER_SUBSTRINGS:
626 fprintf( stderr, "(%s=" /*)*/,
627 f->f_sub_desc->ad_cname.bv_val );
628 if ( f->f_sub_initial.bv_val != NULL ) {
629 filter_escape_value( &f->f_sub_initial, &escaped );
630 fprintf( stderr, "%s",
632 ber_memfree( escaped.bv_val );
634 if ( f->f_sub_any != NULL ) {
635 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
636 filter_escape_value( &f->f_sub_any[i], &escaped );
637 fprintf( stderr, "*%s",
639 ber_memfree( escaped.bv_val );
642 if ( f->f_sub_final.bv_val != NULL ) {
643 filter_escape_value( &f->f_sub_final, &escaped );
645 "*%s", escaped.bv_val );
646 ber_memfree( escaped.bv_val );
648 fprintf( stderr, /*(*/ ")" );
651 case LDAP_FILTER_PRESENT:
652 fprintf( stderr, "(%s=*)",
653 f->f_desc->ad_cname.bv_val );
656 case LDAP_FILTER_AND:
658 case LDAP_FILTER_NOT:
659 fprintf( stderr, "(%c" /*)*/,
660 f->f_choice == LDAP_FILTER_AND ? '&' :
661 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
662 for ( p = f->f_list; p != NULL; p = p->f_next ) {
665 fprintf( stderr, /*(*/ ")" );
668 case SLAPD_FILTER_COMPUTED:
669 fprintf( stderr, "(?=%s)",
670 f->f_result == LDAP_COMPARE_FALSE ? "false" :
671 f->f_result == LDAP_COMPARE_TRUE ? "true" :
672 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
677 fprintf( stderr, "(unknown-filter=%lu)", f->f_choice );
681 #endif /* ldap_debug */
684 filter2bv( Filter *f, struct berval *fstr )
692 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
696 switch ( f->f_choice ) {
697 case LDAP_FILTER_EQUALITY:
698 filter_escape_value( &f->f_av_value, &tmp );
700 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
701 tmp.bv_len + ( sizeof("(=)") - 1 );
702 fstr->bv_val = malloc( fstr->bv_len + 1 );
704 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
705 f->f_av_desc->ad_cname.bv_val,
708 ber_memfree( tmp.bv_val );
712 filter_escape_value( &f->f_av_value, &tmp );
714 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
715 tmp.bv_len + ( sizeof("(>=)") - 1 );
716 fstr->bv_val = malloc( fstr->bv_len + 1 );
718 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
719 f->f_av_desc->ad_cname.bv_val,
722 ber_memfree( tmp.bv_val );
726 filter_escape_value( &f->f_av_value, &tmp );
728 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
729 tmp.bv_len + ( sizeof("(<=)") - 1 );
730 fstr->bv_val = malloc( fstr->bv_len + 1 );
732 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
733 f->f_av_desc->ad_cname.bv_val,
736 ber_memfree( tmp.bv_val );
739 case LDAP_FILTER_APPROX:
740 filter_escape_value( &f->f_av_value, &tmp );
742 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
743 tmp.bv_len + ( sizeof("(~=)") - 1 );
744 fstr->bv_val = malloc( fstr->bv_len + 1 );
746 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
747 f->f_av_desc->ad_cname.bv_val,
749 ber_memfree( tmp.bv_val );
752 case LDAP_FILTER_SUBSTRINGS:
753 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
754 ( sizeof("(=*)") - 1 );
755 fstr->bv_val = malloc( fstr->bv_len + 128 );
757 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
758 f->f_sub_desc->ad_cname.bv_val );
760 if ( f->f_sub_initial.bv_val != NULL ) {
763 filter_escape_value( &f->f_sub_initial, &tmp );
765 fstr->bv_len += tmp.bv_len;
766 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
768 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
769 /* "(attr=" */ "%s*)",
772 ber_memfree( tmp.bv_val );
775 if ( f->f_sub_any != NULL ) {
776 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
778 filter_escape_value( &f->f_sub_any[i], &tmp );
780 fstr->bv_len += tmp.bv_len + 1;
781 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
783 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
784 /* "(attr=[init]*[any*]" */ "%s*)",
786 ber_memfree( tmp.bv_val );
790 if ( f->f_sub_final.bv_val != NULL ) {
793 filter_escape_value( &f->f_sub_final, &tmp );
795 fstr->bv_len += tmp.bv_len;
796 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
798 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
799 /* "(attr=[init*][any*]" */ "%s)",
802 ber_memfree( tmp.bv_val );
807 case LDAP_FILTER_PRESENT:
808 fstr->bv_len = f->f_desc->ad_cname.bv_len +
809 ( sizeof("(=*)") - 1 );
810 fstr->bv_val = malloc( fstr->bv_len + 1 );
812 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
813 f->f_desc->ad_cname.bv_val );
816 case LDAP_FILTER_AND:
818 case LDAP_FILTER_NOT:
819 fstr->bv_len = sizeof("(%)") - 1;
820 fstr->bv_val = malloc( fstr->bv_len + 128 );
822 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
823 f->f_choice == LDAP_FILTER_AND ? '&' :
824 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
826 for ( p = f->f_list; p != NULL; p = p->f_next ) {
829 filter2bv( p, &tmp );
831 fstr->bv_len += tmp.bv_len;
832 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
834 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
835 /*"("*/ "%s)", tmp.bv_val );
837 ch_free( tmp.bv_val );
842 case SLAPD_FILTER_COMPUTED:
844 f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
845 f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
846 f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
848 f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
849 f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
850 f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
851 sizeof("(?=error)")-1,
856 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
861 static int filter_escape_value(
869 out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 );
872 for( i=0; i < in->bv_len ; i++ ) {
873 if( FILTER_ESCAPE(in->bv_val[i]) ) {
874 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
875 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
876 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
878 out->bv_val[out->bv_len++] = in->bv_val[i];
882 out->bv_val[out->bv_len] = '\0';