1 /* filter.c - routines for parsing and dealing with filters */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
12 #include <ac/string.h>
16 static int get_filter_list(Connection *conn, BerElement *ber, Filter **f, char **fstr);
17 static int get_substring_filter(Connection *conn, BerElement *ber, Filter *f, char **fstr);
20 get_filter( Connection *conn, BerElement *ber, Filter **filt, char **fstr )
27 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
30 * A filter looks like this coming in:
32 * and [0] SET OF Filter,
33 * or [1] SET OF Filter,
35 * equalityMatch [3] AttributeValueAssertion,
36 * substrings [4] SubstringFilter,
37 * greaterOrEqual [5] AttributeValueAssertion,
38 * lessOrEqual [6] AttributeValueAssertion,
39 * present [7] AttributeType,,
40 * approxMatch [8] AttributeValueAssertion
41 * extensibleMatch [9] MatchingRuleAssertion
44 * SubstringFilter ::= SEQUENCE {
46 * SEQUENCE OF CHOICE {
47 * initial [0] IA5String,
53 * MatchingRuleAssertion ::= SEQUENCE {
54 * matchingRule [1] MatchingRuleId OPTIONAL,
55 * type [2] AttributeDescription OPTIONAL,
56 * matchValue [3] AssertionValue,
57 * dnAttributes [4] BOOLEAN DEFAULT FALSE
62 f = (Filter *) ch_malloc( sizeof(Filter) );
67 f->f_choice = ber_peek_tag( ber, &len );
69 switch ( f->f_choice ) {
70 case LDAP_FILTER_EQUALITY:
71 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
72 if ( (err = get_ava( ber, &f->f_ava )) == LDAP_SUCCESS ) {
73 *fstr = ch_malloc(4 + strlen( f->f_avtype ) +
75 sprintf( *fstr, "(%s=%s)", f->f_avtype,
76 f->f_avvalue.bv_val );
80 case LDAP_FILTER_SUBSTRINGS:
81 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
82 err = get_substring_filter( conn, ber, f, fstr );
86 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
87 if ( (err = get_ava( ber, &f->f_ava )) == LDAP_SUCCESS ) {
88 *fstr = ch_malloc(5 + strlen( f->f_avtype ) +
90 sprintf( *fstr, "(%s>=%s)", f->f_avtype,
91 f->f_avvalue.bv_val );
96 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
97 if ( (err = get_ava( ber, &f->f_ava )) == LDAP_SUCCESS ) {
98 *fstr = ch_malloc(5 + strlen( f->f_avtype ) +
100 sprintf( *fstr, "(%s<=%s)", f->f_avtype,
101 f->f_avvalue.bv_val );
105 case LDAP_FILTER_PRESENT:
106 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
107 if ( ber_scanf( ber, "a", &f->f_type ) == LBER_ERROR ) {
111 attr_normalize( f->f_type );
112 *fstr = ch_malloc( 5 + strlen( f->f_type ) );
113 sprintf( *fstr, "(%s=*)", f->f_type );
117 case LDAP_FILTER_APPROX:
118 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
119 if ( (err = get_ava( ber, &f->f_ava )) == LDAP_SUCCESS ) {
120 *fstr = ch_malloc(5 + strlen( f->f_avtype ) +
121 f->f_avvalue.bv_len);
122 sprintf( *fstr, "(%s~=%s)", f->f_avtype,
123 f->f_avvalue.bv_val );
127 case LDAP_FILTER_AND:
128 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
129 if ( (err = get_filter_list( conn, ber, &f->f_and, &ftmp ))
131 if (ftmp == NULL) ftmp = ch_strdup("");
132 *fstr = ch_malloc( 4 + strlen( ftmp ) );
133 sprintf( *fstr, "(&%s)", ftmp );
139 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
140 if ( (err = get_filter_list( conn, ber, &f->f_or, &ftmp ))
142 if (ftmp == NULL) ftmp = ch_strdup("");
143 *fstr = ch_malloc( 4 + strlen( ftmp ) );
144 sprintf( *fstr, "(|%s)", ftmp );
149 case LDAP_FILTER_NOT:
150 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
151 (void) ber_skip_tag( ber, &len );
152 if ( (err = get_filter( conn, ber, &f->f_not, &ftmp )) == LDAP_SUCCESS ) {
153 if (ftmp == NULL) ftmp = ch_strdup("");
154 *fstr = ch_malloc( 4 + strlen( ftmp ) );
155 sprintf( *fstr, "(!%s)", ftmp );
161 Debug( LDAP_DEBUG_ANY, "decoding filter error\n",
167 Debug( LDAP_DEBUG_ANY, "unknown filter type %lu\n",
169 err = LDAP_PROTOCOL_ERROR;
173 if ( err != LDAP_SUCCESS ) {
175 if ( *fstr != NULL ) {
182 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
187 get_filter_list( Connection *conn, BerElement *ber, Filter **f, char **fstr )
195 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
199 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
200 tag = ber_next_element( ber, &len, last ) )
202 if ( (err = get_filter( conn, ber, new, &ftmp )) != LDAP_SUCCESS )
204 if ( *fstr == NULL ) {
207 *fstr = ch_realloc( *fstr, strlen( *fstr ) +
208 strlen( ftmp ) + 1 );
209 strcat( *fstr, ftmp );
212 new = &(*new)->f_next;
216 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
217 return( LDAP_SUCCESS );
221 get_substring_filter(
234 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
236 if ( ber_scanf( ber, "{a" /*}*/, &f->f_sub_type ) == LBER_ERROR ) {
239 attr_normalize( f->f_sub_type );
240 syntax = attr_syntax( f->f_sub_type );
241 f->f_sub_initial = NULL;
243 f->f_sub_final = NULL;
245 *fstr = ch_malloc( strlen( f->f_sub_type ) + 3 );
246 sprintf( *fstr, "(%s=", f->f_sub_type );
247 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
248 tag = ber_next_element( ber, &len, last ) )
250 rc = ber_scanf( ber, "a", &val );
251 if ( rc == LBER_ERROR ) {
254 if ( val == NULL || *val == '\0' ) {
258 return( LDAP_INVALID_SYNTAX );
260 value_normalize( val, syntax );
263 case LDAP_SUBSTRING_INITIAL:
264 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
265 if ( f->f_sub_initial != NULL ) {
266 return( LDAP_PROTOCOL_ERROR );
268 f->f_sub_initial = val;
269 *fstr = ch_realloc( *fstr, strlen( *fstr ) +
271 strcat( *fstr, val );
274 case LDAP_SUBSTRING_ANY:
275 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
276 charray_add( &f->f_sub_any, val );
277 *fstr = ch_realloc( *fstr, strlen( *fstr ) +
279 strcat( *fstr, "*" );
280 strcat( *fstr, val );
283 case LDAP_SUBSTRING_FINAL:
284 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
285 if ( f->f_sub_final != NULL ) {
286 return( LDAP_PROTOCOL_ERROR );
288 f->f_sub_final = val;
289 *fstr = ch_realloc( *fstr, strlen( *fstr ) +
291 strcat( *fstr, "*" );
292 strcat( *fstr, val );
296 Debug( LDAP_DEBUG_FILTER, " unknown type\n", tag, 0,
298 return( LDAP_PROTOCOL_ERROR );
301 *fstr = ch_realloc( *fstr, strlen( *fstr ) + 3 );
302 if ( f->f_sub_final == NULL ) {
303 strcat( *fstr, "*" );
305 strcat( *fstr, ")" );
307 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
308 return( LDAP_SUCCESS );
312 filter_free( Filter *f )
320 switch ( f->f_choice ) {
321 case LDAP_FILTER_EQUALITY:
324 case LDAP_FILTER_APPROX:
325 ava_free( &f->f_ava, 0 );
328 case LDAP_FILTER_SUBSTRINGS:
329 if ( f->f_sub_type != NULL ) {
330 free( f->f_sub_type );
332 if ( f->f_sub_initial != NULL ) {
333 free( f->f_sub_initial );
335 charray_free( f->f_sub_any );
336 if ( f->f_sub_final != NULL ) {
337 free( f->f_sub_final );
341 case LDAP_FILTER_PRESENT:
342 if ( f->f_type != NULL ) {
347 case LDAP_FILTER_AND:
349 case LDAP_FILTER_NOT:
350 for ( p = f->f_list; p != NULL; p = next ) {
357 Debug( LDAP_DEBUG_ANY, "unknown filter type %lu\n",
367 filter_print( Filter *f )
373 fprintf( stderr, "NULL" );
376 switch ( f->f_choice ) {
377 case LDAP_FILTER_EQUALITY:
378 fprintf( stderr, "(%s=%s)", f->f_ava.ava_type,
379 f->f_ava.ava_value.bv_val );
383 fprintf( stderr, "(%s>=%s)", f->f_ava.ava_type,
384 f->f_ava.ava_value.bv_val );
388 fprintf( stderr, "(%s<=%s)", f->f_ava.ava_type,
389 f->f_ava.ava_value.bv_val );
392 case LDAP_FILTER_APPROX:
393 fprintf( stderr, "(%s~=%s)", f->f_ava.ava_type,
394 f->f_ava.ava_value.bv_val );
397 case LDAP_FILTER_SUBSTRINGS:
398 fprintf( stderr, "(%s=", f->f_sub_type );
399 if ( f->f_sub_initial != NULL ) {
400 fprintf( stderr, "%s", f->f_sub_initial );
402 if ( f->f_sub_any != NULL ) {
403 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
404 fprintf( stderr, "*%s", f->f_sub_any[i] );
407 charray_free( f->f_sub_any );
408 if ( f->f_sub_final != NULL ) {
409 fprintf( stderr, "*%s", f->f_sub_final );
413 case LDAP_FILTER_PRESENT:
414 fprintf( stderr, "%s=*", f->f_type );
417 case LDAP_FILTER_AND:
419 case LDAP_FILTER_NOT:
420 fprintf( stderr, "(%c", f->f_choice == LDAP_FILTER_AND ? '&' :
421 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
422 for ( p = f->f_list; p != NULL; p = p->f_next ) {
425 fprintf( stderr, ")" );
429 fprintf( stderr, "unknown type %lu", f->f_choice );
434 #endif /* ldap_debug */