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(
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, "m", &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);
226 fstr->bv_len = sizeof("(=*)") - 1
227 + f->f_desc->ad_cname.bv_len;
228 fstr->bv_val = ch_malloc( fstr->bv_len + 1);
229 sprintf( fstr->bv_val, "(%s=*)",
230 f->f_desc->ad_cname.bv_val );
234 case LDAP_FILTER_APPROX:
236 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
237 "get_filter: conn %d APPROX\n", conn->c_connid ));
239 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
241 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY_APPROX, text );
242 if ( err != LDAP_SUCCESS ) {
246 filter_escape_value( &f->f_av_value, &escaped );
248 fstr->bv_len = sizeof("(~=)") - 1
249 + f->f_av_desc->ad_cname.bv_len
251 fstr->bv_val = ch_malloc( fstr->bv_len + 1);
253 sprintf( fstr->bv_val, "(%s~=%s)",
254 f->f_av_desc->ad_cname.bv_val,
257 ber_memfree( escaped.bv_val );
260 case LDAP_FILTER_AND:
262 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
263 "get_filter: conn %d AND\n", conn->c_connid ));
265 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
267 err = get_filter_list( conn, ber, &f->f_and, &ftmp, text );
268 if ( err != LDAP_SUCCESS ) {
271 fstr->bv_len = sizeof("(&)") - 1 + ftmp.bv_len;
272 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
273 sprintf( fstr->bv_val, "(&%s)",
274 ftmp.bv_len ? ftmp.bv_val : "" );
279 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
280 "get_filter: conn %d OR\n", conn->c_connid ));
282 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
284 err = get_filter_list( conn, ber, &f->f_and, &ftmp, text );
285 if ( err != LDAP_SUCCESS ) {
288 fstr->bv_len = sizeof("(|)") - 1 + ftmp.bv_len;
289 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
290 sprintf( fstr->bv_val, "(|%s)",
291 ftmp.bv_len ? ftmp.bv_val : "" );
294 case LDAP_FILTER_NOT:
296 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
297 "get_filter: conn %d NOT\n", conn->c_connid ));
299 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
301 (void) ber_skip_tag( ber, &len );
302 err = get_filter( conn, ber, &f->f_not, &ftmp, text );
303 if ( err != LDAP_SUCCESS ) {
306 fstr->bv_len = sizeof("(!)") - 1 + ftmp.bv_len;
307 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
308 sprintf( fstr->bv_val, "(!%s)",
309 ftmp.bv_len ? ftmp.bv_val : "" );
312 case LDAP_FILTER_EXT:
314 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
315 "get_filter: conn %d EXTENSIBLE\n", conn->c_connid ));
317 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
320 err = get_mra( ber, &f->f_mra, text );
321 if ( err != LDAP_SUCCESS ) {
325 assert( f->f_mra != NULL );
327 filter_escape_value( &f->f_mr_value, &escaped );
329 fstr->bv_len = sizeof("(:dn::=)") - 1
330 + (f->f_mr_desc ? f->f_mr_desc->ad_cname.bv_len : 0)
331 + f->f_mr_rule_text.bv_len
334 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
335 sprintf( fstr->bv_val, "(%s%s%s%s:=%s)",
336 (f->f_mr_desc ? f->f_mr_desc->ad_cname.bv_val : ""),
337 (f->f_mr_dnattrs ? ":dn" : ""),
338 (f->f_mr_rule_text.bv_len ? ":" : ""),
339 (f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : ""),
342 ber_memfree( escaped.bv_val );
346 (void) ber_scanf( ber, "x" ); /* skip the element */
348 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
349 "get_filter: conn %d unknown filter type=%lu\n",
350 conn->c_connid, f->f_choice ));
352 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
355 f->f_choice = SLAPD_FILTER_COMPUTED;
356 f->f_result = SLAPD_COMPARE_UNDEFINED;
357 ber_str2bv( "(undefined)", sizeof("(undefined)") - 1,
362 if ( ftmp.bv_val ) free( ftmp.bv_val );
364 if ( err != LDAP_SUCCESS ) {
365 if ( fstr->bv_val != NULL ) {
366 free( fstr->bv_val );
369 if( err != SLAPD_DISCONNECT ) {
371 f->f_choice = SLAPD_FILTER_COMPUTED;
372 f->f_result = SLAPD_COMPARE_UNDEFINED;
373 ber_str2bv( "(badfilter)", sizeof("(badfilter)") - 1,
386 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL2,
387 "get_filter: conn %d exit\n", conn->c_connid ));
389 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
395 get_filter_list( Connection *conn, BerElement *ber,
396 Filter **f, struct berval *fstr,
407 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
408 "get_filter_list: conn %d start\n", conn->c_connid ));
410 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
413 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
414 tag = ber_next_element( ber, &len, last ) )
416 err = get_filter( conn, ber, new, &ftmp, text );
417 if ( err != LDAP_SUCCESS )
420 if ( !fstr->bv_len ) {
423 int i = fstr->bv_len;
424 fstr->bv_len += ftmp.bv_len;
425 fstr->bv_val = ch_realloc( fstr->bv_val,
427 strcpy( fstr->bv_val+i, ftmp.bv_val );
430 new = &(*new)->f_next;
435 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
436 "get_filter_list: conn %d exit\n", conn->c_connid ));
438 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
440 return( LDAP_SUCCESS );
444 get_substring_filter(
456 struct berval escaped;
459 *text = "error decoding filter";
462 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
463 "get_substring_filter: conn %d begin\n", conn->c_connid ));
465 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
467 if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
468 return SLAPD_DISCONNECT;
471 f->f_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
472 f->f_sub_desc = NULL;
473 rc = slap_bv2ad( &bv, &f->f_sub_desc, text );
475 if( rc != LDAP_SUCCESS ) {
478 f->f_choice = SLAPD_FILTER_COMPUTED;
479 f->f_result = SLAPD_COMPARE_UNDEFINED;
480 ber_str2bv( "(undefined)", sizeof("(undefined)")-1, 1, fstr );
484 f->f_sub_initial.bv_val = NULL;
486 f->f_sub_final.bv_val = NULL;
488 fstr->bv_len = sizeof("(=" /*)*/) - 1 +
489 f->f_sub_desc->ad_cname.bv_len;
490 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
491 sprintf( fstr->bv_val, "(%s=" /*)*/, f->f_sub_desc->ad_cname.bv_val );
493 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
494 tag = ber_next_element( ber, &len, last ) )
498 rc = ber_scanf( ber, "m", &value );
499 if ( rc == LBER_ERROR ) {
500 rc = SLAPD_DISCONNECT;
504 if ( value.bv_val == NULL || value.bv_len == 0 ) {
505 rc = LDAP_INVALID_SYNTAX;
510 case LDAP_SUBSTRING_INITIAL:
511 usage = SLAP_MR_SUBSTR_INITIAL;
514 case LDAP_SUBSTRING_ANY:
515 usage = SLAP_MR_SUBSTR_ANY;
518 case LDAP_SUBSTRING_FINAL:
519 usage = SLAP_MR_SUBSTR_FINAL;
523 rc = LDAP_PROTOCOL_ERROR;
526 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
527 "get_filter_substring: conn %d unknown substring choice=%ld\n",
528 conn->c_connid, (long)tag ));
530 Debug( LDAP_DEBUG_FILTER,
531 " unknown substring choice=%ld\n",
537 rc = value_normalize( f->f_sub_desc, usage, &value, &bv, text );
539 if( rc != LDAP_SUCCESS ) {
545 rc = LDAP_PROTOCOL_ERROR;
548 case LDAP_SUBSTRING_INITIAL:
550 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
551 "get_substring_filter: conn %d INITIAL\n",
554 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
557 if ( f->f_sub_initial.bv_val != NULL
558 || f->f_sub_any != NULL
559 || f->f_sub_final.bv_val != NULL )
561 free( value.bv_val );
565 f->f_sub_initial = value;
568 int i = fstr->bv_len;
569 filter_escape_value( &value, &escaped );
570 fstr->bv_len += escaped.bv_len;
571 fstr->bv_val = ch_realloc( fstr->bv_val,
573 strcpy( fstr->bv_val+i, escaped.bv_val );
574 ber_memfree( escaped.bv_val );
578 case LDAP_SUBSTRING_ANY:
580 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
581 "get_substring_filter: conn %d ANY\n",
584 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
587 if ( f->f_sub_final.bv_val != NULL ) {
588 free( value.bv_val );
592 bvarray_add( &f->f_sub_any, &value );
595 int i = fstr->bv_len;
596 filter_escape_value( &value, &escaped );
597 fstr->bv_len += escaped.bv_len + 2;
598 fstr->bv_val = ch_realloc( fstr->bv_val,
600 strcpy( fstr->bv_val+i, "*" );
601 strcpy( fstr->bv_val+i+1, escaped.bv_val );
602 ber_memfree( escaped.bv_val );
606 case LDAP_SUBSTRING_FINAL:
608 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
609 "get_substring_filter: conn %d FINAL\n",
612 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
615 if ( f->f_sub_final.bv_val != NULL ) {
616 free( value.bv_val );
620 f->f_sub_final = value;
623 int i = fstr->bv_len;
624 filter_escape_value( &value, &escaped );
625 fstr->bv_len += escaped.bv_len + 2;
626 fstr->bv_val = ch_realloc( fstr->bv_val,
628 strcpy( fstr->bv_val+i, "*" );
629 strcpy( fstr->bv_val+i+1, escaped.bv_val );
630 ber_memfree( escaped.bv_val );
636 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
637 "get_substring_filter: conn %d unknown substring type %ld\n",
638 conn->c_connid, (long)tag ));
640 Debug( LDAP_DEBUG_FILTER,
641 " unknown substring type=%ld\n",
645 free( value.bv_val );
649 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
650 "get_substring_filter: conn %d error %ld\n",
651 conn->c_connid, (long)rc ));
653 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
657 free( fstr->bv_val );
662 free( f->f_sub_initial.bv_val );
663 bvarray_free( f->f_sub_any );
664 free( f->f_sub_final.bv_val );
671 int i = fstr->bv_len;
673 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 3 );
674 if ( f->f_sub_final.bv_val == NULL ) {
675 strcpy( fstr->bv_val+i, "*" );
678 strcpy( fstr->bv_val+i, /*(*/ ")" );
682 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
683 "get_substring_filter: conn %d exit\n", conn->c_connid ));
685 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
687 return( LDAP_SUCCESS );
691 filter_free( Filter *f )
699 switch ( f->f_choice ) {
700 case LDAP_FILTER_PRESENT:
703 case LDAP_FILTER_EQUALITY:
706 case LDAP_FILTER_APPROX:
707 ava_free( f->f_ava, 1 );
710 case LDAP_FILTER_SUBSTRINGS:
711 if ( f->f_sub_initial.bv_val != NULL ) {
712 free( f->f_sub_initial.bv_val );
714 bvarray_free( f->f_sub_any );
715 if ( f->f_sub_final.bv_val != NULL ) {
716 free( f->f_sub_final.bv_val );
721 case LDAP_FILTER_AND:
723 case LDAP_FILTER_NOT:
724 for ( p = f->f_list; p != NULL; p = next ) {
730 case LDAP_FILTER_EXT:
731 mra_free( f->f_mra, 1 );
734 case SLAPD_FILTER_COMPUTED:
739 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
740 "filter_free: unknown filter type %lu\n", f->f_choice ));
742 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
753 filter_print( Filter *f )
757 struct berval escaped;
760 fprintf( stderr, "No filter!" );
763 switch ( f->f_choice ) {
764 case LDAP_FILTER_EQUALITY:
765 filter_escape_value( &f->f_av_value, &escaped );
766 fprintf( stderr, "(%s=%s)",
767 f->f_av_desc->ad_cname.bv_val,
769 ber_memfree( escaped.bv_val );
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_ava->aa_desc->ad_cname.bv_val,
785 ber_memfree( escaped.bv_val );
788 case LDAP_FILTER_APPROX:
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_SUBSTRINGS:
797 fprintf( stderr, "(%s=" /*)*/,
798 f->f_sub_desc->ad_cname.bv_val );
799 if ( f->f_sub_initial.bv_val != NULL ) {
800 filter_escape_value( &f->f_sub_initial, &escaped );
801 fprintf( stderr, "%s",
803 ber_memfree( escaped.bv_val );
805 if ( f->f_sub_any != NULL ) {
806 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
807 filter_escape_value( &f->f_sub_any[i], &escaped );
808 fprintf( stderr, "*%s",
810 ber_memfree( escaped.bv_val );
813 if ( f->f_sub_final.bv_val != NULL ) {
814 filter_escape_value( &f->f_sub_final, &escaped );
816 "*%s", escaped.bv_val );
817 ber_memfree( escaped.bv_val );
819 fprintf( stderr, /*(*/ ")" );
822 case LDAP_FILTER_PRESENT:
823 fprintf( stderr, "(%s=*)",
824 f->f_desc->ad_cname.bv_val );
827 case LDAP_FILTER_AND:
829 case LDAP_FILTER_NOT:
830 fprintf( stderr, "(%c" /*)*/,
831 f->f_choice == LDAP_FILTER_AND ? '&' :
832 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
833 for ( p = f->f_list; p != NULL; p = p->f_next ) {
836 fprintf( stderr, /*(*/ ")" );
839 case SLAPD_FILTER_COMPUTED:
840 fprintf( stderr, "(?=%s)",
841 f->f_result == LDAP_COMPARE_FALSE ? "false" :
842 f->f_result == LDAP_COMPARE_TRUE ? "true" :
843 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
848 fprintf( stderr, "(unknown-filter=%lu)", f->f_choice );
853 #endif /* ldap_debug */
855 static int filter_escape_value(
863 out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 );
866 for( i=0; i < in->bv_len ; i++ ) {
867 if( FILTER_ESCAPE(in->bv_val[i]) ) {
868 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
869 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
870 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
872 out->bv_val[out->bv_len++] = in->bv_val[i];
876 out->bv_val[out->bv_len] = '\0';