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 );
246 (void) ber_scanf( ber, "x" ); /* skip the element */
248 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
249 "get_filter: conn %d unknown filter type=%lu\n",
250 conn->c_connid, f->f_choice ));
252 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
255 f->f_choice = SLAPD_FILTER_COMPUTED;
256 f->f_result = SLAPD_COMPARE_UNDEFINED;
260 if ( err != LDAP_SUCCESS ) {
261 if( err != SLAPD_DISCONNECT ) {
263 f->f_choice = SLAPD_FILTER_COMPUTED;
264 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(
335 *text = "error decoding filter";
338 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
339 "get_substring_filter: conn %d begin\n", conn->c_connid ));
341 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
343 if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
344 return SLAPD_DISCONNECT;
347 f->f_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
348 f->f_sub_desc = NULL;
349 rc = slap_bv2ad( &bv, &f->f_sub_desc, text );
351 if( rc != LDAP_SUCCESS ) {
354 f->f_choice = SLAPD_FILTER_COMPUTED;
355 f->f_result = SLAPD_COMPARE_UNDEFINED;
359 f->f_sub_initial.bv_val = NULL;
361 f->f_sub_final.bv_val = NULL;
363 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
364 tag = ber_next_element( ber, &len, last ) )
368 rc = ber_scanf( ber, "m", &value );
369 if ( rc == LBER_ERROR ) {
370 rc = SLAPD_DISCONNECT;
374 if ( value.bv_val == NULL || value.bv_len == 0 ) {
375 rc = LDAP_INVALID_SYNTAX;
380 case LDAP_SUBSTRING_INITIAL:
381 usage = SLAP_MR_SUBSTR_INITIAL;
384 case LDAP_SUBSTRING_ANY:
385 usage = SLAP_MR_SUBSTR_ANY;
388 case LDAP_SUBSTRING_FINAL:
389 usage = SLAP_MR_SUBSTR_FINAL;
393 rc = LDAP_PROTOCOL_ERROR;
396 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
397 "get_filter_substring: conn %d unknown substring choice=%ld\n",
398 conn->c_connid, (long)tag ));
400 Debug( LDAP_DEBUG_FILTER,
401 " unknown substring choice=%ld\n",
407 /* valiate using equality matching rule validator! */
408 rc = value_validate( f->f_sub_desc->ad_type->sat_equality,
410 if( rc != LDAP_SUCCESS ) {
414 rc = value_normalize( f->f_sub_desc, usage,
416 if( rc != LDAP_SUCCESS ) {
422 rc = LDAP_PROTOCOL_ERROR;
425 case LDAP_SUBSTRING_INITIAL:
427 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
428 "get_substring_filter: conn %d INITIAL\n",
431 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
434 if ( f->f_sub_initial.bv_val != NULL
435 || f->f_sub_any != NULL
436 || f->f_sub_final.bv_val != NULL )
438 free( value.bv_val );
442 f->f_sub_initial = value;
445 case LDAP_SUBSTRING_ANY:
447 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
448 "get_substring_filter: conn %d ANY\n",
451 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
454 if ( f->f_sub_final.bv_val != NULL ) {
455 free( value.bv_val );
459 ber_bvarray_add( &f->f_sub_any, &value );
462 case LDAP_SUBSTRING_FINAL:
464 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
465 "get_substring_filter: conn %d FINAL\n",
468 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
471 if ( f->f_sub_final.bv_val != NULL ) {
472 free( value.bv_val );
476 f->f_sub_final = value;
481 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
482 "get_substring_filter: conn %d unknown substring type %ld\n",
483 conn->c_connid, (long)tag ));
485 Debug( LDAP_DEBUG_FILTER,
486 " unknown substring type=%ld\n",
490 free( value.bv_val );
494 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
495 "get_substring_filter: conn %d error %ld\n",
496 conn->c_connid, (long)rc ));
498 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
501 free( f->f_sub_initial.bv_val );
502 ber_bvarray_free( f->f_sub_any );
503 free( f->f_sub_final.bv_val );
510 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
511 "get_substring_filter: conn %d exit\n", conn->c_connid ));
513 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
515 return( LDAP_SUCCESS );
519 filter_free( Filter *f )
527 switch ( f->f_choice ) {
528 case LDAP_FILTER_PRESENT:
531 case LDAP_FILTER_EQUALITY:
534 case LDAP_FILTER_APPROX:
535 ava_free( f->f_ava, 1 );
538 case LDAP_FILTER_SUBSTRINGS:
539 if ( f->f_sub_initial.bv_val != NULL ) {
540 free( f->f_sub_initial.bv_val );
542 ber_bvarray_free( f->f_sub_any );
543 if ( f->f_sub_final.bv_val != NULL ) {
544 free( f->f_sub_final.bv_val );
549 case LDAP_FILTER_AND:
551 case LDAP_FILTER_NOT:
552 for ( p = f->f_list; p != NULL; p = next ) {
558 case LDAP_FILTER_EXT:
559 mra_free( f->f_mra, 1 );
562 case SLAPD_FILTER_COMPUTED:
567 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
568 "filter_free: unknown filter type %lu\n", f->f_choice ));
570 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
580 filter2bv( Filter *f, struct berval *fstr )
588 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
592 switch ( f->f_choice ) {
593 case LDAP_FILTER_EQUALITY:
594 filter_escape_value( &f->f_av_value, &tmp );
596 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
597 tmp.bv_len + ( sizeof("(=)") - 1 );
598 fstr->bv_val = malloc( fstr->bv_len + 1 );
600 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
601 f->f_av_desc->ad_cname.bv_val,
604 ber_memfree( tmp.bv_val );
608 filter_escape_value( &f->f_av_value, &tmp );
610 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
611 tmp.bv_len + ( sizeof("(>=)") - 1 );
612 fstr->bv_val = malloc( fstr->bv_len + 1 );
614 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
615 f->f_av_desc->ad_cname.bv_val,
618 ber_memfree( tmp.bv_val );
622 filter_escape_value( &f->f_av_value, &tmp );
624 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
625 tmp.bv_len + ( sizeof("(<=)") - 1 );
626 fstr->bv_val = malloc( fstr->bv_len + 1 );
628 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
629 f->f_av_desc->ad_cname.bv_val,
632 ber_memfree( tmp.bv_val );
635 case LDAP_FILTER_APPROX:
636 filter_escape_value( &f->f_av_value, &tmp );
638 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
639 tmp.bv_len + ( sizeof("(~=)") - 1 );
640 fstr->bv_val = malloc( fstr->bv_len + 1 );
642 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
643 f->f_av_desc->ad_cname.bv_val,
645 ber_memfree( tmp.bv_val );
648 case LDAP_FILTER_SUBSTRINGS:
649 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
650 ( sizeof("(=*)") - 1 );
651 fstr->bv_val = malloc( fstr->bv_len + 128 );
653 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
654 f->f_sub_desc->ad_cname.bv_val );
656 if ( f->f_sub_initial.bv_val != NULL ) {
659 filter_escape_value( &f->f_sub_initial, &tmp );
661 fstr->bv_len += tmp.bv_len;
662 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
664 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
665 /* "(attr=" */ "%s*)",
668 ber_memfree( tmp.bv_val );
671 if ( f->f_sub_any != NULL ) {
672 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
674 filter_escape_value( &f->f_sub_any[i], &tmp );
676 fstr->bv_len += tmp.bv_len + 1;
677 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
679 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
680 /* "(attr=[init]*[any*]" */ "%s*)",
682 ber_memfree( tmp.bv_val );
686 if ( f->f_sub_final.bv_val != NULL ) {
689 filter_escape_value( &f->f_sub_final, &tmp );
691 fstr->bv_len += tmp.bv_len;
692 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
694 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
695 /* "(attr=[init*][any*]" */ "%s)",
698 ber_memfree( tmp.bv_val );
703 case LDAP_FILTER_PRESENT:
704 fstr->bv_len = f->f_desc->ad_cname.bv_len +
705 ( sizeof("(=*)") - 1 );
706 fstr->bv_val = malloc( fstr->bv_len + 1 );
708 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
709 f->f_desc->ad_cname.bv_val );
712 case LDAP_FILTER_AND:
714 case LDAP_FILTER_NOT:
715 fstr->bv_len = sizeof("(%)") - 1;
716 fstr->bv_val = malloc( fstr->bv_len + 128 );
718 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
719 f->f_choice == LDAP_FILTER_AND ? '&' :
720 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
722 for ( p = f->f_list; p != NULL; p = p->f_next ) {
725 filter2bv( p, &tmp );
727 fstr->bv_len += tmp.bv_len;
728 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
730 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
731 /*"("*/ "%s)", tmp.bv_val );
733 ch_free( tmp.bv_val );
738 case LDAP_FILTER_EXT:
739 filter_escape_value( &f->f_mr_value, &tmp );
741 fstr->bv_len = f->f_mr_desc->ad_cname.bv_len +
742 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
743 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
744 tmp.bv_len + ( sizeof("(:=)") - 1 );
745 fstr->bv_val = malloc( fstr->bv_len + 1 );
747 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
748 f->f_mr_desc->ad_cname.bv_val,
749 f->f_mr_dnattrs ? ":dn" : "",
750 f->f_mr_rule_text.bv_len ? ":" : "",
751 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
753 ber_memfree( tmp.bv_val );
756 case SLAPD_FILTER_COMPUTED:
758 f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
759 f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
760 f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
762 f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
763 f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
764 f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
765 sizeof("(?=error)")-1,
770 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
775 static int filter_escape_value(
783 out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 );
786 for( i=0; i < in->bv_len ; i++ ) {
787 if( FILTER_ESCAPE(in->bv_val[i]) ) {
788 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
789 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
790 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
792 out->bv_val[out->bv_len++] = in->bv_val[i];
796 out->bv_val[out->bv_len] = '\0';