1 /* filter.c - routines for parsing and dealing with filters */
4 * Copyright 1998-2000 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(
24 static int get_substring_filter(
31 static int filter_escape_value(
47 struct berval ftmp = { 0, NULL };
48 struct berval escaped;
51 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY, "get_filter: conn %d\n",
54 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
57 * A filter looks like this coming in:
59 * and [0] SET OF Filter,
60 * or [1] SET OF Filter,
62 * equalityMatch [3] AttributeValueAssertion,
63 * substrings [4] SubstringFilter,
64 * greaterOrEqual [5] AttributeValueAssertion,
65 * lessOrEqual [6] AttributeValueAssertion,
66 * present [7] AttributeType,,
67 * approxMatch [8] AttributeValueAssertion
68 * extensibleMatch [9] MatchingRuleAssertion
71 * SubstringFilter ::= SEQUENCE {
73 * SEQUENCE OF CHOICE {
74 * initial [0] IA5String,
80 * MatchingRuleAssertion ::= SEQUENCE {
81 * matchingRule [1] MatchingRuleId OPTIONAL,
82 * type [2] AttributeDescription OPTIONAL,
83 * matchValue [3] AssertionValue,
84 * dnAttributes [4] BOOLEAN DEFAULT FALSE
89 tag = ber_peek_tag( ber, &len );
91 if( tag == LBER_ERROR ) {
92 *text = "error decoding filter";
93 return SLAPD_DISCONNECT;
96 f = (Filter *) ch_malloc( sizeof(Filter) );
103 switch ( f->f_choice ) {
104 case LDAP_FILTER_EQUALITY:
106 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL2,
107 "get_filter: conn %d EQUALITY\n", conn->c_connid ));
109 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
111 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY, text );
112 if ( err != LDAP_SUCCESS ) {
116 assert( f->f_ava != NULL );
118 filter_escape_value( f->f_av_value, &escaped );
120 fstr->bv_len = sizeof("(=)")-1
121 + f->f_av_desc->ad_cname.bv_len
124 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
126 sprintf( fstr->bv_val, "(%s=%s)",
127 f->f_av_desc->ad_cname.bv_val,
130 ber_memfree( escaped.bv_val );
133 case LDAP_FILTER_SUBSTRINGS:
135 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
136 "get_filter: conn %d SUBSTRINGS\n", conn->c_connid ));
138 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
140 err = get_substring_filter( conn, ber, f, fstr, text );
145 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
146 "get_filter: conn %d GE\n", conn->c_connid ));
148 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
150 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
151 if ( err != LDAP_SUCCESS ) {
155 filter_escape_value( f->f_av_value, &escaped );
157 fstr->bv_len = sizeof("(>=)")-1
158 + f->f_av_desc->ad_cname.bv_len
161 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
163 sprintf( fstr->bv_val, "(%s>=%s)",
164 f->f_av_desc->ad_cname.bv_val,
167 ber_memfree( escaped.bv_val );
172 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
173 "get_filter: conn %d LE\n", conn->c_connid ));
175 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
177 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
178 if ( err != LDAP_SUCCESS ) {
183 filter_escape_value( f->f_av_value, &escaped );
185 fstr->bv_len = sizeof("(<=)")-1
186 + f->f_av_desc->ad_cname.bv_len
189 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
191 sprintf( fstr->bv_val, "(%s<=%s)",
192 f->f_av_desc->ad_cname.bv_val,
195 ber_memfree( escaped.bv_val );
198 case LDAP_FILTER_PRESENT: {
202 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
203 "get_filter: conn %d PRESENT\n", conn->c_connid ));
205 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
207 if ( ber_scanf( ber, "o", &type ) == LBER_ERROR ) {
208 err = SLAPD_DISCONNECT;
209 *text = "error decoding filter";
214 err = slap_bv2ad( &type, &f->f_desc, text );
216 if( err != LDAP_SUCCESS ) {
217 /* unrecognized attribute description or other error */
218 f->f_choice = SLAPD_FILTER_COMPUTED;
219 f->f_result = LDAP_COMPARE_FALSE;
220 ber_str2bv("(unrecognized=*)",
221 sizeof("(unrecognized=*)")-1, 1, fstr);
222 ch_free( type.bv_val );
227 ch_free( type.bv_val );
229 fstr->bv_len = sizeof("(=*)") - 1
230 + f->f_desc->ad_cname.bv_len;
231 fstr->bv_val = ch_malloc( fstr->bv_len + 1);
232 sprintf( fstr->bv_val, "(%s=*)",
233 f->f_desc->ad_cname.bv_val );
237 case LDAP_FILTER_APPROX:
239 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
240 "get_filter: conn %d APPROX\n", conn->c_connid ));
242 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
244 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY_APPROX, text );
245 if ( err != LDAP_SUCCESS ) {
249 filter_escape_value( f->f_av_value, &escaped );
251 fstr->bv_len = sizeof("(~=)") - 1
252 + f->f_av_desc->ad_cname.bv_len
254 fstr->bv_val = ch_malloc( fstr->bv_len + 1);
256 sprintf( fstr->bv_val, "(%s~=%s)",
257 f->f_av_desc->ad_cname.bv_val,
260 ber_memfree( escaped.bv_val );
263 case LDAP_FILTER_AND:
265 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
266 "get_filter: conn %d AND\n", conn->c_connid ));
268 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
270 err = get_filter_list( conn, ber, &f->f_and, &ftmp, text );
271 if ( err != LDAP_SUCCESS ) {
274 fstr->bv_len = sizeof("(&)") - 1 + ftmp.bv_len;
275 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
276 sprintf( fstr->bv_val, "(&%s)",
277 ftmp.bv_len ? ftmp.bv_val : "" );
282 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
283 "get_filter: conn %d OR\n", conn->c_connid ));
285 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
287 err = get_filter_list( conn, ber, &f->f_and, &ftmp, text );
288 if ( err != LDAP_SUCCESS ) {
291 fstr->bv_len = sizeof("(|)") - 1 + ftmp.bv_len;
292 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
293 sprintf( fstr->bv_val, "(|%s)",
294 ftmp.bv_len ? ftmp.bv_val : "" );
297 case LDAP_FILTER_NOT:
299 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
300 "get_filter: conn %d NOT\n", conn->c_connid ));
302 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
304 (void) ber_skip_tag( ber, &len );
305 err = get_filter( conn, ber, &f->f_not, &ftmp, text );
306 if ( err != LDAP_SUCCESS ) {
309 fstr->bv_len = sizeof("(!)") - 1 + ftmp.bv_len;
310 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
311 sprintf( fstr->bv_val, "(!%s)",
312 ftmp.bv_len ? ftmp.bv_val : "" );
315 case LDAP_FILTER_EXT:
317 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
318 "get_filter: conn %d EXTENSIBLE\n", conn->c_connid ));
320 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
323 err = get_mra( ber, &f->f_mra, text );
324 if ( err != LDAP_SUCCESS ) {
328 assert( f->f_mra != NULL );
330 filter_escape_value( f->f_mr_value, &escaped );
332 fstr->bv_len = sizeof("(:dn::=)") - 1
333 + (f->f_mr_desc ? f->f_mr_desc->ad_cname.bv_len : 0)
334 + f->f_mr_rule_text.bv_len
337 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
338 sprintf( fstr->bv_val, "(%s%s%s%s:=%s)",
339 (f->f_mr_desc ? f->f_mr_desc->ad_cname.bv_val : ""),
340 (f->f_mr_dnattrs ? ":dn" : ""),
341 (f->f_mr_rule_text.bv_len ? ":" : ""),
342 (f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : ""),
345 ber_memfree( escaped.bv_val );
349 (void) ber_scanf( ber, "x" ); /* skip the element */
351 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
352 "get_filter: conn %d unknown filter type=%lu\n",
353 conn->c_connid, f->f_choice ));
355 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
358 f->f_choice = SLAPD_FILTER_COMPUTED;
359 f->f_result = SLAPD_COMPARE_UNDEFINED;
360 ber_str2bv( "(undefined)", sizeof("(undefined)") - 1,
367 if ( err != LDAP_SUCCESS ) {
368 if ( fstr->bv_val != NULL ) {
369 free( fstr->bv_val );
372 if( err != SLAPD_DISCONNECT ) {
374 f->f_choice = SLAPD_FILTER_COMPUTED;
375 f->f_result = SLAPD_COMPARE_UNDEFINED;
376 ber_str2bv( "(badfilter)", sizeof("(badfilter)") - 1,
389 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL2,
390 "get_filter: conn %d exit\n", conn->c_connid ));
392 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
398 get_filter_list( Connection *conn, BerElement *ber,
399 Filter **f, struct berval *fstr,
410 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
411 "get_filter_list: conn %d start\n", conn->c_connid ));
413 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
416 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
417 tag = ber_next_element( ber, &len, last ) )
419 err = get_filter( conn, ber, new, &ftmp, text );
420 if ( err != LDAP_SUCCESS )
423 if ( !fstr->bv_len ) {
426 int i = fstr->bv_len;
427 fstr->bv_len += ftmp.bv_len;
428 fstr->bv_val = ch_realloc( fstr->bv_val,
430 strcpy( fstr->bv_val+i, ftmp.bv_val );
433 new = &(*new)->f_next;
438 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
439 "get_filter_list: conn %d exit\n", conn->c_connid ));
441 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
443 return( LDAP_SUCCESS );
447 get_substring_filter(
458 struct berval *value;
459 struct berval escaped;
462 struct berval *nvalue;
463 *text = "error decoding filter";
466 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
467 "get_substring_filter: conn %d begin\n", conn->c_connid ));
469 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
471 if ( ber_scanf( ber, "{o" /*}*/, &type ) == LBER_ERROR ) {
472 return SLAPD_DISCONNECT;
475 f->f_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
476 f->f_sub_desc = NULL;
477 rc = slap_bv2ad( &type, &f->f_sub_desc, text );
479 ch_free( type.bv_val );
481 if( rc != LDAP_SUCCESS ) {
484 f->f_choice = SLAPD_FILTER_COMPUTED;
485 f->f_result = SLAPD_COMPARE_UNDEFINED;
486 ber_str2bv( "(undefined)", sizeof("(undefined)")-1, 1, fstr );
490 f->f_sub_initial = NULL;
492 f->f_sub_final = NULL;
494 fstr->bv_len = sizeof("(=" /*)*/) - 1 +
495 f->f_sub_desc->ad_cname.bv_len;
496 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
497 sprintf( fstr->bv_val, "(%s=" /*)*/, f->f_sub_desc->ad_cname.bv_val );
499 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
500 tag = ber_next_element( ber, &len, last ) )
504 rc = ber_scanf( ber, "O", &value );
505 if ( rc == LBER_ERROR ) {
506 rc = SLAPD_DISCONNECT;
510 if ( value == NULL || value->bv_len == 0 ) {
512 rc = LDAP_INVALID_SYNTAX;
517 case LDAP_SUBSTRING_INITIAL:
518 usage = SLAP_MR_SUBSTR_INITIAL;
521 case LDAP_SUBSTRING_ANY:
522 usage = SLAP_MR_SUBSTR_ANY;
525 case LDAP_SUBSTRING_FINAL:
526 usage = SLAP_MR_SUBSTR_FINAL;
530 rc = LDAP_PROTOCOL_ERROR;
533 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
534 "get_filter_substring: conn %d unknown substring choice=%ld\n",
535 conn->c_connid, (long)tag ));
537 Debug( LDAP_DEBUG_FILTER,
538 " unknown substring choice=%ld\n",
545 rc = value_normalize( f->f_sub_desc, usage, value, &nvalue, text );
548 if( rc != LDAP_SUCCESS ) {
554 rc = LDAP_PROTOCOL_ERROR;
557 case LDAP_SUBSTRING_INITIAL:
559 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
560 "get_substring_filter: conn %d INITIAL\n",
563 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
566 if ( f->f_sub_initial != NULL
567 || f->f_sub_any != NULL
568 || f->f_sub_final != NULL )
574 f->f_sub_initial = value;
577 int i = fstr->bv_len;
578 filter_escape_value( value, &escaped );
579 fstr->bv_len += escaped.bv_len;
580 fstr->bv_val = ch_realloc( fstr->bv_val,
582 strcpy( fstr->bv_val+i, escaped.bv_val );
583 ber_memfree( escaped.bv_val );
587 case LDAP_SUBSTRING_ANY:
589 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
590 "get_substring_filter: conn %d ANY\n",
593 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
596 if ( f->f_sub_final != NULL ) {
601 if( ber_bvecadd( &f->f_sub_any, value ) < 0 ) {
607 int i = fstr->bv_len;
608 filter_escape_value( value, &escaped );
609 fstr->bv_len += escaped.bv_len + 2;
610 fstr->bv_val = ch_realloc( fstr->bv_val,
612 strcpy( fstr->bv_val+i, "*" );
613 strcpy( fstr->bv_val+i+1, escaped.bv_val );
614 ber_memfree( escaped.bv_val );
618 case LDAP_SUBSTRING_FINAL:
620 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
621 "get_substring_filter: conn %d FINAL\n",
624 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
627 if ( f->f_sub_final != NULL ) {
632 f->f_sub_final = value;
635 int i = fstr->bv_len;
636 filter_escape_value( value, &escaped );
637 fstr->bv_len += escaped.bv_len + 2;
638 fstr->bv_val = ch_realloc( fstr->bv_val,
640 strcpy( fstr->bv_val+i, "*" );
641 strcpy( fstr->bv_val+i+1, escaped.bv_val );
642 ber_memfree( escaped.bv_val );
648 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
649 "get_substring_filter: conn %d unknown substring type %ld\n",
650 conn->c_connid, (long)tag ));
652 Debug( LDAP_DEBUG_FILTER,
653 " unknown substring type=%ld\n",
661 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
662 "get_substring_filter: conn %d error %ld\n",
663 conn->c_connid, (long)rc ));
665 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
669 free( fstr->bv_val );
674 ber_bvfree( f->f_sub_initial );
675 ber_bvecfree( f->f_sub_any );
676 ber_bvfree( f->f_sub_final );
683 int i = fstr->bv_len;
685 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 3 );
686 if ( f->f_sub_final == NULL ) {
687 strcpy( fstr->bv_val+i, "*" );
690 strcpy( fstr->bv_val+i, /*(*/ ")" );
694 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
695 "get_substring_filter: conn %d exit\n", conn->c_connid ));
697 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
699 return( LDAP_SUCCESS );
703 filter_free( Filter *f )
711 switch ( f->f_choice ) {
712 case LDAP_FILTER_PRESENT:
715 case LDAP_FILTER_EQUALITY:
718 case LDAP_FILTER_APPROX:
719 ava_free( f->f_ava, 1 );
722 case LDAP_FILTER_SUBSTRINGS:
723 if ( f->f_sub_initial != NULL ) {
724 ber_bvfree( f->f_sub_initial );
726 ber_bvecfree( f->f_sub_any );
727 if ( f->f_sub_final != NULL ) {
728 ber_bvfree( f->f_sub_final );
733 case LDAP_FILTER_AND:
735 case LDAP_FILTER_NOT:
736 for ( p = f->f_list; p != NULL; p = next ) {
742 case SLAPD_FILTER_COMPUTED:
747 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
748 "filter_free: unknown filter type %lu\n", f->f_choice ));
750 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
761 filter_print( Filter *f )
765 struct berval escaped;
768 fprintf( stderr, "No filter!" );
771 switch ( f->f_choice ) {
772 case LDAP_FILTER_EQUALITY:
773 filter_escape_value( f->f_av_value, &escaped );
774 fprintf( stderr, "(%s=%s)",
775 f->f_av_desc->ad_cname.bv_val,
777 ber_memfree( escaped.bv_val );
781 filter_escape_value( f->f_av_value, &escaped );
782 fprintf( stderr, "(%s>=%s)",
783 f->f_av_desc->ad_cname.bv_val,
785 ber_memfree( escaped.bv_val );
789 filter_escape_value( f->f_av_value, &escaped );
790 fprintf( stderr, "(%s<=%s)",
791 f->f_ava->aa_desc->ad_cname.bv_val,
793 ber_memfree( escaped.bv_val );
796 case LDAP_FILTER_APPROX:
797 filter_escape_value( f->f_av_value, &escaped );
798 fprintf( stderr, "(%s~=%s)",
799 f->f_ava->aa_desc->ad_cname.bv_val,
801 ber_memfree( escaped.bv_val );
804 case LDAP_FILTER_SUBSTRINGS:
805 fprintf( stderr, "(%s=" /*)*/,
806 f->f_sub_desc->ad_cname.bv_val );
807 if ( f->f_sub_initial != NULL ) {
808 filter_escape_value( f->f_sub_initial, &escaped );
809 fprintf( stderr, "%s",
811 ber_memfree( escaped.bv_val );
813 if ( f->f_sub_any != NULL ) {
814 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
815 filter_escape_value( f->f_sub_any[i], &escaped );
816 fprintf( stderr, "*%s",
818 ber_memfree( escaped.bv_val );
821 if ( f->f_sub_final != NULL ) {
822 filter_escape_value( f->f_sub_final, &escaped );
824 "*%s", escaped.bv_val );
825 ber_memfree( escaped.bv_val );
827 fprintf( stderr, /*(*/ ")" );
830 case LDAP_FILTER_PRESENT:
831 fprintf( stderr, "(%s=*)",
832 f->f_desc->ad_cname.bv_val );
835 case LDAP_FILTER_AND:
837 case LDAP_FILTER_NOT:
838 fprintf( stderr, "(%c" /*)*/,
839 f->f_choice == LDAP_FILTER_AND ? '&' :
840 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
841 for ( p = f->f_list; p != NULL; p = p->f_next ) {
844 fprintf( stderr, /*(*/ ")" );
847 case SLAPD_FILTER_COMPUTED:
848 fprintf( stderr, "(?=%s)",
849 f->f_result == LDAP_COMPARE_FALSE ? "false" :
850 f->f_result == LDAP_COMPARE_TRUE ? "true" :
851 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
856 fprintf( stderr, "(unknown-filter=%lu)", f->f_choice );
861 #endif /* ldap_debug */
863 static int filter_escape_value(
871 out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 );
874 for( i=0; i < in->bv_len ; i++ ) {
875 if( FILTER_ESCAPE(in->bv_val[i]) ) {
876 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
877 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
878 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
880 out->bv_val[out->bv_len++] = in->bv_val[i];
884 out->bv_val[out->bv_len] = '\0';