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,
365 if ( ftmp.bv_val ) free( ftmp.bv_val );
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(
459 struct berval escaped;
462 *text = "error decoding filter";
465 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
466 "get_substring_filter: conn %d begin\n", conn->c_connid ));
468 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
470 if ( ber_scanf( ber, "{o" /*}*/, &bv ) == LBER_ERROR ) {
471 return SLAPD_DISCONNECT;
474 f->f_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
475 f->f_sub_desc = NULL;
476 rc = slap_bv2ad( &bv, &f->f_sub_desc, text );
478 ch_free( bv.bv_val );
480 if( rc != LDAP_SUCCESS ) {
483 f->f_choice = SLAPD_FILTER_COMPUTED;
484 f->f_result = SLAPD_COMPARE_UNDEFINED;
485 ber_str2bv( "(undefined)", sizeof("(undefined)")-1, 1, fstr );
489 f->f_sub_initial.bv_val = NULL;
491 f->f_sub_final.bv_val = NULL;
493 fstr->bv_len = sizeof("(=" /*)*/) - 1 +
494 f->f_sub_desc->ad_cname.bv_len;
495 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
496 sprintf( fstr->bv_val, "(%s=" /*)*/, f->f_sub_desc->ad_cname.bv_val );
498 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
499 tag = ber_next_element( ber, &len, last ) )
503 rc = ber_scanf( ber, "o", &value );
504 if ( rc == LBER_ERROR ) {
505 rc = SLAPD_DISCONNECT;
509 if ( value.bv_val == NULL || value.bv_len == 0 ) {
510 rc = LDAP_INVALID_SYNTAX;
515 case LDAP_SUBSTRING_INITIAL:
516 usage = SLAP_MR_SUBSTR_INITIAL;
519 case LDAP_SUBSTRING_ANY:
520 usage = SLAP_MR_SUBSTR_ANY;
523 case LDAP_SUBSTRING_FINAL:
524 usage = SLAP_MR_SUBSTR_FINAL;
528 rc = LDAP_PROTOCOL_ERROR;
531 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
532 "get_filter_substring: conn %d unknown substring choice=%ld\n",
533 conn->c_connid, (long)tag ));
535 Debug( LDAP_DEBUG_FILTER,
536 " unknown substring choice=%ld\n",
539 free( value.bv_val );
543 rc = value_normalize( f->f_sub_desc, usage, &value, &bv, text );
544 free( value.bv_val );
546 if( rc != LDAP_SUCCESS ) {
552 rc = LDAP_PROTOCOL_ERROR;
555 case LDAP_SUBSTRING_INITIAL:
557 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
558 "get_substring_filter: conn %d INITIAL\n",
561 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
564 if ( f->f_sub_initial.bv_val != NULL
565 || f->f_sub_any != NULL
566 || f->f_sub_final.bv_val != NULL )
568 free( value.bv_val );
572 f->f_sub_initial = value;
575 int i = fstr->bv_len;
576 filter_escape_value( &value, &escaped );
577 fstr->bv_len += escaped.bv_len;
578 fstr->bv_val = ch_realloc( fstr->bv_val,
580 strcpy( fstr->bv_val+i, escaped.bv_val );
581 ber_memfree( escaped.bv_val );
585 case LDAP_SUBSTRING_ANY:
587 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
588 "get_substring_filter: conn %d ANY\n",
591 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
594 if ( f->f_sub_final.bv_val != NULL ) {
595 free( value.bv_val );
599 bvarray_add( &f->f_sub_any, &value );
602 int i = fstr->bv_len;
603 filter_escape_value( &value, &escaped );
604 fstr->bv_len += escaped.bv_len + 2;
605 fstr->bv_val = ch_realloc( fstr->bv_val,
607 strcpy( fstr->bv_val+i, "*" );
608 strcpy( fstr->bv_val+i+1, escaped.bv_val );
609 ber_memfree( escaped.bv_val );
613 case LDAP_SUBSTRING_FINAL:
615 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
616 "get_substring_filter: conn %d FINAL\n",
619 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
622 if ( f->f_sub_final.bv_val != NULL ) {
623 free( value.bv_val );
627 f->f_sub_final = value;
630 int i = fstr->bv_len;
631 filter_escape_value( &value, &escaped );
632 fstr->bv_len += escaped.bv_len + 2;
633 fstr->bv_val = ch_realloc( fstr->bv_val,
635 strcpy( fstr->bv_val+i, "*" );
636 strcpy( fstr->bv_val+i+1, escaped.bv_val );
637 ber_memfree( escaped.bv_val );
643 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
644 "get_substring_filter: conn %d unknown substring type %ld\n",
645 conn->c_connid, (long)tag ));
647 Debug( LDAP_DEBUG_FILTER,
648 " unknown substring type=%ld\n",
652 free( value.bv_val );
656 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
657 "get_substring_filter: conn %d error %ld\n",
658 conn->c_connid, (long)rc ));
660 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
664 free( fstr->bv_val );
669 free( f->f_sub_initial.bv_val );
670 bvarray_free( f->f_sub_any );
671 free( f->f_sub_final.bv_val );
678 int i = fstr->bv_len;
680 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 3 );
681 if ( f->f_sub_final.bv_val == NULL ) {
682 strcpy( fstr->bv_val+i, "*" );
685 strcpy( fstr->bv_val+i, /*(*/ ")" );
689 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
690 "get_substring_filter: conn %d exit\n", conn->c_connid ));
692 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
694 return( LDAP_SUCCESS );
698 filter_free( Filter *f )
706 switch ( f->f_choice ) {
707 case LDAP_FILTER_PRESENT:
710 case LDAP_FILTER_EQUALITY:
713 case LDAP_FILTER_APPROX:
714 ava_free( f->f_ava, 1 );
717 case LDAP_FILTER_SUBSTRINGS:
718 if ( f->f_sub_initial.bv_val != NULL ) {
719 free( f->f_sub_initial.bv_val );
721 bvarray_free( f->f_sub_any );
722 if ( f->f_sub_final.bv_val != NULL ) {
723 free( f->f_sub_final.bv_val );
728 case LDAP_FILTER_AND:
730 case LDAP_FILTER_NOT:
731 for ( p = f->f_list; p != NULL; p = next ) {
737 case SLAPD_FILTER_COMPUTED:
742 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
743 "filter_free: unknown filter type %lu\n", f->f_choice ));
745 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
756 filter_print( Filter *f )
760 struct berval escaped;
763 fprintf( stderr, "No filter!" );
766 switch ( f->f_choice ) {
767 case LDAP_FILTER_EQUALITY:
768 filter_escape_value( &f->f_av_value, &escaped );
769 fprintf( stderr, "(%s=%s)",
770 f->f_av_desc->ad_cname.bv_val,
772 ber_memfree( escaped.bv_val );
776 filter_escape_value( &f->f_av_value, &escaped );
777 fprintf( stderr, "(%s>=%s)",
778 f->f_av_desc->ad_cname.bv_val,
780 ber_memfree( escaped.bv_val );
784 filter_escape_value( &f->f_av_value, &escaped );
785 fprintf( stderr, "(%s<=%s)",
786 f->f_ava->aa_desc->ad_cname.bv_val,
788 ber_memfree( escaped.bv_val );
791 case LDAP_FILTER_APPROX:
792 filter_escape_value( &f->f_av_value, &escaped );
793 fprintf( stderr, "(%s~=%s)",
794 f->f_ava->aa_desc->ad_cname.bv_val,
796 ber_memfree( escaped.bv_val );
799 case LDAP_FILTER_SUBSTRINGS:
800 fprintf( stderr, "(%s=" /*)*/,
801 f->f_sub_desc->ad_cname.bv_val );
802 if ( f->f_sub_initial.bv_val != NULL ) {
803 filter_escape_value( &f->f_sub_initial, &escaped );
804 fprintf( stderr, "%s",
806 ber_memfree( escaped.bv_val );
808 if ( f->f_sub_any != NULL ) {
809 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
810 filter_escape_value( &f->f_sub_any[i], &escaped );
811 fprintf( stderr, "*%s",
813 ber_memfree( escaped.bv_val );
816 if ( f->f_sub_final.bv_val != NULL ) {
817 filter_escape_value( &f->f_sub_final, &escaped );
819 "*%s", escaped.bv_val );
820 ber_memfree( escaped.bv_val );
822 fprintf( stderr, /*(*/ ")" );
825 case LDAP_FILTER_PRESENT:
826 fprintf( stderr, "(%s=*)",
827 f->f_desc->ad_cname.bv_val );
830 case LDAP_FILTER_AND:
832 case LDAP_FILTER_NOT:
833 fprintf( stderr, "(%c" /*)*/,
834 f->f_choice == LDAP_FILTER_AND ? '&' :
835 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
836 for ( p = f->f_list; p != NULL; p = p->f_next ) {
839 fprintf( stderr, /*(*/ ")" );
842 case SLAPD_FILTER_COMPUTED:
843 fprintf( stderr, "(?=%s)",
844 f->f_result == LDAP_COMPARE_FALSE ? "false" :
845 f->f_result == LDAP_COMPARE_TRUE ? "true" :
846 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
851 fprintf( stderr, "(unknown-filter=%lu)", f->f_choice );
856 #endif /* ldap_debug */
858 static int filter_escape_value(
866 out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 );
869 for( i=0; i < in->bv_len ; i++ ) {
870 if( FILTER_ESCAPE(in->bv_val[i]) ) {
871 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
872 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
873 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
875 out->bv_val[out->bv_len++] = in->bv_val[i];
879 out->bv_val[out->bv_len] = '\0';