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(
23 static int get_substring_filter(
44 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
47 * A filter looks like this coming in:
49 * and [0] SET OF Filter,
50 * or [1] SET OF Filter,
52 * equalityMatch [3] AttributeValueAssertion,
53 * substrings [4] SubstringFilter,
54 * greaterOrEqual [5] AttributeValueAssertion,
55 * lessOrEqual [6] AttributeValueAssertion,
56 * present [7] AttributeType,,
57 * approxMatch [8] AttributeValueAssertion
58 * extensibleMatch [9] MatchingRuleAssertion
61 * SubstringFilter ::= SEQUENCE {
63 * SEQUENCE OF CHOICE {
64 * initial [0] IA5String,
70 * MatchingRuleAssertion ::= SEQUENCE {
71 * matchingRule [1] MatchingRuleId OPTIONAL,
72 * type [2] AttributeDescription OPTIONAL,
73 * matchValue [3] AssertionValue,
74 * dnAttributes [4] BOOLEAN DEFAULT FALSE
79 tag = ber_peek_tag( ber, &len );
81 if( tag == LBER_ERROR ) {
82 *text = "error decoding filter";
83 return SLAPD_DISCONNECT;
86 f = (Filter *) ch_malloc( sizeof(Filter) );
93 switch ( f->f_choice ) {
94 case LDAP_FILTER_EQUALITY:
95 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
97 if ( (err = get_ava( ber, &f->f_ava )) != LDAP_SUCCESS ) {
98 *text = "error decoding filter";
102 #ifdef SLAPD_SCHEMA_NOT_COMPAT
103 *fstr = ch_malloc( sizeof("(=)")
104 + f->f_av_desc->ad_cname->bv_len
105 + f->f_av_value->bv_len );
107 sprintf( *fstr, "(%s=%s)",
108 f->f_av_desc->ad_cname->bv_val,
109 f->f_av_value->bv_val );
112 *fstr = ch_malloc( sizeof("(=)")
113 + strlen( f->f_avtype )
114 + f->f_avvalue.bv_len);
115 sprintf( *fstr, "(%s=%s)", f->f_avtype,
116 f->f_avvalue.bv_val );
120 case LDAP_FILTER_SUBSTRINGS:
121 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
122 err = get_substring_filter( conn, ber, f, fstr, text );
126 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
128 if ( (err = get_ava( ber, &f->f_ava )) != LDAP_SUCCESS ) {
129 *text = "decoding filter error";
133 #ifdef SLAPD_SCHEMA_NOT_COMPAT
134 *fstr = ch_malloc( sizeof("(>=)")
135 + f->f_av_desc->ad_cname->bv_len
136 + f->f_av_value->bv_len );
138 sprintf( *fstr, "(%s>=%s)",
139 f->f_av_desc->ad_cname->bv_val,
140 f->f_av_value->bv_val );
143 *fstr = ch_malloc( sizeof("(>=)")
144 + strlen( f->f_avtype )
145 + f->f_avvalue.bv_len);
146 sprintf( *fstr, "(%s>=%s)", f->f_avtype,
147 f->f_avvalue.bv_val );
152 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
154 if ( (err = get_ava( ber, &f->f_ava )) != LDAP_SUCCESS ) {
155 *text = "decoding filter error";
159 #ifdef SLAPD_SCHEMA_NOT_COMPAT
161 *fstr = ch_malloc( sizeof("(<=)")
162 + f->f_av_desc->ad_cname->bv_len
163 + f->f_av_value->bv_len );
165 sprintf( *fstr, "(%s<=%s)",
166 f->f_av_desc->ad_cname->bv_val,
167 f->f_av_value->bv_val );
170 *fstr = ch_malloc( sizeof("(<=)")
171 + strlen( f->f_avtype )
172 + f->f_avvalue.bv_len);
173 sprintf( *fstr, "(%s<=%s)", f->f_avtype,
174 f->f_avvalue.bv_val );
178 case LDAP_FILTER_PRESENT: {
181 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
183 if ( ber_scanf( ber, "o", &type ) == LBER_ERROR ) {
184 err = SLAPD_DISCONNECT;
185 *text = "error decoding filter";
189 #ifdef SLAPD_SCHEMA_NOT_COMPAT
194 err = slap_bv2ad( &type, &f->f_desc, &text );
196 if( err != LDAP_SUCCESS ) {
197 ch_free( type.bv_val );
201 ch_free( type.bv_val );
204 *fstr = ch_malloc( sizeof("(=*)")
205 + f->f_desc->ad_cname->bv_len );
206 sprintf( *fstr, "(%s=*)",
207 f->f_desc->ad_cname->bv_val );
209 f->f_type = type.bv_val;
211 attr_normalize( f->f_type );
212 *fstr = ch_malloc( sizeof("(=*)")
213 + strlen( f->f_type ) );
214 sprintf( *fstr, "(%s=*)", f->f_type );
218 case LDAP_FILTER_APPROX:
219 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
221 if ( (err = get_ava( ber, &f->f_ava )) != LDAP_SUCCESS ) {
222 *text = "decoding filter error";
226 #ifdef SLAPD_SCHEMA_NOT_COMPAT
227 *fstr = ch_malloc( sizeof("(~=)")
228 + f->f_av_desc->ad_cname->bv_len
229 + f->f_av_value->bv_len );
231 sprintf( *fstr, "(%s~=%s)",
232 f->f_av_desc->ad_cname->bv_val,
233 f->f_av_value->bv_val );
236 *fstr = ch_malloc( sizeof("(~=)")
237 + strlen( f->f_avtype )
238 + f->f_avvalue.bv_len);
239 sprintf( *fstr, "(%s~=%s)", f->f_avtype,
240 f->f_avvalue.bv_val );
244 case LDAP_FILTER_AND:
245 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
246 err = get_filter_list( conn, ber, &f->f_and, &ftmp, text );
247 if ( err != LDAP_SUCCESS ) {
250 if (ftmp == NULL) ftmp = ch_strdup("");
251 *fstr = ch_malloc( 4 + strlen( ftmp ) );
252 sprintf( *fstr, "(&%s)", ftmp );
257 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
258 err = get_filter_list( conn, ber, &f->f_and, &ftmp, text );
259 if ( err != LDAP_SUCCESS ) {
262 if (ftmp == NULL) ftmp = ch_strdup("");
263 *fstr = ch_malloc( 4 + strlen( ftmp ) );
264 sprintf( *fstr, "(|%s)", ftmp );
268 case LDAP_FILTER_NOT:
269 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
270 (void) ber_skip_tag( ber, &len );
271 err = get_filter( conn, ber, &f->f_not, &ftmp, text );
272 if ( err != LDAP_SUCCESS ) {
275 if (ftmp == NULL) ftmp = ch_strdup("");
276 *fstr = ch_malloc( 4 + strlen( ftmp ) );
277 sprintf( *fstr, "(!%s)", ftmp );
281 case LDAP_FILTER_EXT:
282 /* not yet implemented */
283 Debug( LDAP_DEBUG_ANY, "extensible match not yet implemented.\n",
285 err = LDAP_PROTOCOL_ERROR;
286 *text = "extensible match not yet implemented";
290 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
292 err = LDAP_PROTOCOL_ERROR;
293 *text = "unknown filter type";
297 if ( err != LDAP_SUCCESS ) {
299 if ( *fstr != NULL ) {
306 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
311 get_filter_list( Connection *conn, BerElement *ber, Filter **f, char **fstr, char **text )
319 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
323 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
324 tag = ber_next_element( ber, &len, last ) )
326 err = get_filter( conn, ber, new, &ftmp, text );
327 if ( err != LDAP_SUCCESS )
330 if ( *fstr == NULL ) {
333 *fstr = ch_realloc( *fstr, strlen( *fstr ) +
334 strlen( ftmp ) + 1 );
335 strcat( *fstr, ftmp );
338 new = &(*new)->f_next;
342 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
343 return( LDAP_SUCCESS );
347 get_substring_filter(
363 *text = "error decoding filter";
365 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
367 if ( ber_scanf( ber, "{a" /*}*/, &type ) == LBER_ERROR ) {
368 return SLAPD_DISCONNECT;
371 #ifdef SLAPD_SCHEMA_NOT_COMPAT
372 /* not yet implemented */
374 f->f_sub_type = type;
375 attr_normalize( f->f_sub_type );
377 /* should get real syntax and see if we have a substring matching rule */
378 syntax = attr_syntax( f->f_sub_type );
381 f->f_sub_initial = NULL;
383 f->f_sub_final = NULL;
385 #ifdef SLAPD_SCHEMA_NOT_COMPAT
386 /* not yet implemented */
389 *fstr = ch_malloc( strlen( f->f_sub_type ) + 3 );
390 sprintf( *fstr, "(%s=" /*)*/, f->f_sub_type );
394 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
395 tag = ber_next_element( ber, &len, last ) )
397 rc = ber_scanf( ber, "O", &val );
398 if ( rc == LBER_ERROR ) {
399 rc = SLAPD_DISCONNECT;
403 if ( val == NULL || val->bv_len == 0 ) {
405 rc = LDAP_INVALID_SYNTAX;
409 rc = LDAP_PROTOCOL_ERROR;
411 #ifdef SLAPD_SCHEMA_NOT_COMPAT
412 /* not yet implemented */
414 /* we should call a substring syntax normalization routine */
415 value_normalize( val->bv_val, syntax );
416 /* this is bogus, value_normalize should take a berval */
417 val->bv_len = strlen( val->bv_val );
421 case LDAP_SUBSTRING_INITIAL:
422 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
423 if ( f->f_sub_initial != NULL ) {
427 f->f_sub_initial = val;
430 *fstr = ch_realloc( *fstr,
431 strlen( *fstr ) + val->bv_len + 1 );
432 strcat( *fstr, val->bv_val );
436 case LDAP_SUBSTRING_ANY:
437 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
438 if( ber_bvecadd( &f->f_sub_any, val ) < 0 ) {
444 *fstr = ch_realloc( *fstr,
445 strlen( *fstr ) + val->bv_len + 2 );
446 strcat( *fstr, "*" );
447 strcat( *fstr, val->bv_val );
451 case LDAP_SUBSTRING_FINAL:
452 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
453 if ( f->f_sub_final != NULL ) {
457 f->f_sub_final = val;
460 *fstr = ch_realloc( *fstr,
461 strlen( *fstr ) + val->bv_len + 2 );
462 strcat( *fstr, "*" );
463 strcat( *fstr, val->bv_val );
468 Debug( LDAP_DEBUG_FILTER,
469 " unknown substring type=%ld\n",
475 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
483 #ifdef SLAPD_SCHEMA_NOT_COMPAT
484 /* not yet implemented */
486 ch_free( f->f_sub_type );
488 ber_bvfree( f->f_sub_initial );
489 ber_bvecfree( f->f_sub_any );
490 ber_bvfree( f->f_sub_final );
496 *fstr = ch_realloc( *fstr, strlen( *fstr ) + 3 );
497 if ( f->f_sub_final == NULL ) {
498 strcat( *fstr, "*" );
500 strcat( *fstr, /*(*/ ")" );
503 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
504 return( LDAP_SUCCESS );
508 filter_free( Filter *f )
516 switch ( f->f_choice ) {
517 case LDAP_FILTER_PRESENT:
518 #ifdef SLAPD_SCHEMA_NOT_COMPAT
519 ad_free( f->f_desc, 1 );
521 if ( f->f_type != NULL ) {
527 case LDAP_FILTER_EQUALITY:
530 case LDAP_FILTER_APPROX:
531 #ifdef SLAPD_SCHEMA_NOT_COMPAT
532 ava_free( f->f_ava, 1 );
534 ava_free( &f->f_ava, 0 );
538 case LDAP_FILTER_SUBSTRINGS:
539 #ifdef SLAPD_SCHEMA_NOT_COMPAT
540 ad_free( f->f_sub_desc, 1 );
541 if ( f->f_sub_initial != NULL ) {
542 ber_bvfree( f->f_sub_initial );
544 ber_bvecfree( f->f_sub_any );
545 if ( f->f_sub_final != NULL ) {
546 ber_bvfree( f->f_sub_final );
549 if ( f->f_sub_type != NULL ) {
550 free( f->f_sub_type );
552 if ( f->f_sub_initial != NULL ) {
553 ber_bvfree( f->f_sub_initial );
555 ber_bvecfree( f->f_sub_any );
556 if ( f->f_sub_final != NULL ) {
557 ber_bvfree( f->f_sub_final );
562 case LDAP_FILTER_AND:
564 case LDAP_FILTER_NOT:
565 for ( p = f->f_list; p != NULL; p = next ) {
571 case SLAPD_FILTER_COMPUTED:
575 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
586 filter_print( Filter *f )
592 fprintf( stderr, "No filter!" );
595 switch ( f->f_choice ) {
596 case LDAP_FILTER_EQUALITY:
597 #ifdef SLAPD_SCHEMA_NOT_COMPAT
598 fprintf( stderr, "(%s=%s)",
599 f->f_av_desc->ad_cname->bv_val,
600 f->f_av_value->bv_val );
602 fprintf( stderr, "(%s=%s)", f->f_ava.ava_type,
603 f->f_ava.ava_value.bv_val );
608 #ifdef SLAPD_SCHEMA_NOT_COMPAT
609 fprintf( stderr, "(%s>=%s)",
610 f->f_av_desc->ad_cname->bv_val,
611 f->f_av_value->bv_val );
613 fprintf( stderr, "(%s>=%s)", f->f_ava.ava_type,
614 f->f_ava.ava_value.bv_val );
619 #ifdef SLAPD_SCHEMA_NOT_COMPAT
620 fprintf( stderr, "(%s<=%s)",
621 f->f_ava->aa_desc->ad_cname->bv_val,
622 f->f_ava->aa_value->bv_val );
624 fprintf( stderr, "(%s<=%s)", f->f_ava.ava_type,
625 f->f_ava.ava_value.bv_val );
629 case LDAP_FILTER_APPROX:
630 #ifdef SLAPD_SCHEMA_NOT_COMPAT
631 fprintf( stderr, "(%s~=%s)",
632 f->f_ava->aa_desc->ad_cname->bv_val,
633 f->f_ava->aa_value->bv_val );
635 fprintf( stderr, "(%s~=%s)", f->f_ava.ava_type,
636 f->f_ava.ava_value.bv_val );
640 case LDAP_FILTER_SUBSTRINGS:
641 #ifdef SLAPD_SCHEMA_NOT_COMPAT
642 fprintf( stderr, "(%s=" /*)*/,
643 f->f_sub_desc->ad_cname->bv_val );
645 fprintf( stderr, "(%s=" /*)*/, f->f_sub_type );
647 if ( f->f_sub_initial != NULL ) {
648 fprintf( stderr, "%s", f->f_sub_initial->bv_val );
650 if ( f->f_sub_any != NULL ) {
651 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
652 fprintf( stderr, "*%s", f->f_sub_any[i]->bv_val );
655 if ( f->f_sub_final != NULL ) {
656 fprintf( stderr, "*%s", f->f_sub_final->bv_val );
658 fprintf( stderr, /*(*/ ")" );
661 case LDAP_FILTER_PRESENT:
662 #ifdef SLAPD_SCHEMA_NOT_COMPAT
663 fprintf( stderr, "(%s=*)",
664 f->f_desc->ad_cname->bv_val );
666 fprintf( stderr, "(%s=*)", f->f_type );
670 case LDAP_FILTER_AND:
672 case LDAP_FILTER_NOT:
673 fprintf( stderr, "(%c" /*)*/,
674 f->f_choice == LDAP_FILTER_AND ? '&' :
675 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
676 for ( p = f->f_list; p != NULL; p = p->f_next ) {
679 fprintf( stderr, /*(*/ ")" );
682 case SLAPD_FILTER_COMPUTED:
683 fprintf( stderr, "(%s)",
684 f->f_result == LDAP_COMPARE_FALSE ? "false" :
685 f->f_result == LDAP_COMPARE_TRUE ? "true" :
686 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
691 fprintf( stderr, "(unknown filter %lu)", f->f_choice );
696 #endif /* ldap_debug */