1 /* filter.c - routines for parsing and dealing with filters */
12 static int get_filter_list(Connection *conn, BerElement *ber, Filter **f, char **fstr);
13 static int get_substring_filter(Connection *conn, BerElement *ber, Filter *f, char **fstr);
16 get_filter( Connection *conn, BerElement *ber, Filter **filt, char **fstr )
18 unsigned long tag, len;
23 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
26 * A filter looks like this coming in:
28 * and [0] SET OF Filter,
29 * or [1] SET OF Filter,
31 * equalityMatch [3] AttributeValueAssertion,
32 * substrings [4] SubstringFilter,
33 * greaterOrEqual [5] AttributeValueAssertion,
34 * lessOrEqual [6] AttributeValueAssertion,
35 * present [7] AttributeType,,
36 * approxMatch [8] AttributeValueAssertion
39 * SubstringFilter ::= SEQUENCE {
41 * SEQUENCE OF CHOICE {
42 * initial [0] IA5String,
49 f = (Filter *) ch_malloc( sizeof(Filter) );
54 f->f_choice = ber_peek_tag( ber, &len );
56 if ( conn->c_version == 30 ) {
57 switch ( f->f_choice ) {
58 case LDAP_FILTER_EQUALITY:
61 case LDAP_FILTER_PRESENT:
62 case LDAP_FILTER_PRESENT_30:
63 case LDAP_FILTER_APPROX:
64 (void) ber_skip_tag( ber, &len );
65 if ( f->f_choice == LDAP_FILTER_PRESENT_30 ) {
66 f->f_choice = LDAP_FILTER_PRESENT;
74 switch ( f->f_choice ) {
75 case LDAP_FILTER_EQUALITY:
76 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
77 if ( (err = get_ava( ber, &f->f_ava )) == 0 ) {
78 *fstr = ch_malloc(4 + strlen( f->f_avtype ) +
80 sprintf( *fstr, "(%s=%s)", f->f_avtype,
81 f->f_avvalue.bv_val );
85 case LDAP_FILTER_SUBSTRINGS:
86 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
87 err = get_substring_filter( conn, ber, f, fstr );
91 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
92 if ( (err = get_ava( ber, &f->f_ava )) == 0 ) {
93 *fstr = ch_malloc(5 + strlen( f->f_avtype ) +
95 sprintf( *fstr, "(%s>=%s)", f->f_avtype,
96 f->f_avvalue.bv_val );
101 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
102 if ( (err = get_ava( ber, &f->f_ava )) == 0 ) {
103 *fstr = ch_malloc(5 + strlen( f->f_avtype ) +
104 f->f_avvalue.bv_len);
105 sprintf( *fstr, "(%s<=%s)", f->f_avtype,
106 f->f_avvalue.bv_val );
110 case LDAP_FILTER_PRESENT:
111 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
112 if ( ber_scanf( ber, "a", &f->f_type ) == LBER_ERROR ) {
113 err = LDAP_PROTOCOL_ERROR;
116 attr_normalize( f->f_type );
117 *fstr = ch_malloc( 5 + strlen( f->f_type ) );
118 sprintf( *fstr, "(%s=*)", f->f_type );
122 case LDAP_FILTER_APPROX:
123 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
124 if ( (err = get_ava( ber, &f->f_ava )) == 0 ) {
125 *fstr = ch_malloc(5 + strlen( f->f_avtype ) +
126 f->f_avvalue.bv_len);
127 sprintf( *fstr, "(%s~=%s)", f->f_avtype,
128 f->f_avvalue.bv_val );
132 case LDAP_FILTER_AND:
133 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
134 if ( (err = get_filter_list( conn, ber, &f->f_and, &ftmp ))
136 if (ftmp == NULL) ftmp = ch_strdup("");
137 *fstr = ch_malloc( 4 + strlen( ftmp ) );
138 sprintf( *fstr, "(&%s)", ftmp );
144 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
145 if ( (err = get_filter_list( conn, ber, &f->f_or, &ftmp ))
147 if (ftmp == NULL) ftmp = ch_strdup("");
148 *fstr = ch_malloc( 4 + strlen( ftmp ) );
149 sprintf( *fstr, "(|%s)", ftmp );
154 case LDAP_FILTER_NOT:
155 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
156 (void) ber_skip_tag( ber, &len );
157 if ( (err = get_filter( conn, ber, &f->f_not, &ftmp )) == 0 ) {
158 if (ftmp == NULL) ftmp = ch_strdup("");
159 *fstr = ch_malloc( 4 + strlen( ftmp ) );
160 sprintf( *fstr, "(!%s)", ftmp );
166 Debug( LDAP_DEBUG_ANY, "unknown filter type %lu\n",
168 err = LDAP_PROTOCOL_ERROR;
174 if ( *fstr != NULL ) {
181 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
186 get_filter_list( Connection *conn, BerElement *ber, Filter **f, char **fstr )
190 unsigned long tag, len;
193 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
196 if ( conn->c_version == 30 ) {
197 (void) ber_skip_tag( ber, &len );
202 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
203 tag = ber_next_element( ber, &len, last ) ) {
204 if ( (err = get_filter( conn, ber, new, &ftmp )) != 0 )
206 if ( *fstr == NULL ) {
209 *fstr = ch_realloc( *fstr, strlen( *fstr ) +
210 strlen( ftmp ) + 1 );
211 strcat( *fstr, ftmp );
214 new = &(*new)->f_next;
218 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
223 get_substring_filter(
230 unsigned long tag, len, rc;
234 Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
237 if ( conn->c_version == 30 ) {
238 (void) ber_skip_tag( ber, &len );
241 if ( ber_scanf( ber, "{a", &f->f_sub_type ) == LBER_ERROR ) {
242 return( LDAP_PROTOCOL_ERROR );
244 attr_normalize( f->f_sub_type );
245 syntax = attr_syntax( f->f_sub_type );
246 f->f_sub_initial = NULL;
248 f->f_sub_final = NULL;
250 *fstr = ch_malloc( strlen( f->f_sub_type ) + 3 );
251 sprintf( *fstr, "(%s=", f->f_sub_type );
252 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
253 tag = ber_next_element( ber, &len, last ) ) {
255 if ( conn->c_version == 30 ) {
256 rc = ber_scanf( ber, "{a}", &val );
259 rc = ber_scanf( ber, "a", &val );
260 if ( rc == LBER_ERROR ) {
261 return( LDAP_PROTOCOL_ERROR );
263 if ( val == NULL || *val == '\0' ) {
267 return( LDAP_INVALID_SYNTAX );
269 value_normalize( val, syntax );
273 case LDAP_SUBSTRING_INITIAL_30:
275 case LDAP_SUBSTRING_INITIAL:
276 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
277 if ( f->f_sub_initial != NULL ) {
278 return( LDAP_PROTOCOL_ERROR );
280 f->f_sub_initial = val;
281 *fstr = ch_realloc( *fstr, strlen( *fstr ) +
283 strcat( *fstr, val );
287 case LDAP_SUBSTRING_ANY_30:
289 case LDAP_SUBSTRING_ANY:
290 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
291 charray_add( &f->f_sub_any, val );
292 *fstr = ch_realloc( *fstr, strlen( *fstr ) +
294 strcat( *fstr, "*" );
295 strcat( *fstr, val );
299 case LDAP_SUBSTRING_FINAL_30:
301 case LDAP_SUBSTRING_FINAL:
302 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
303 if ( f->f_sub_final != NULL ) {
304 return( LDAP_PROTOCOL_ERROR );
306 f->f_sub_final = val;
307 *fstr = ch_realloc( *fstr, strlen( *fstr ) +
309 strcat( *fstr, "*" );
310 strcat( *fstr, val );
314 Debug( LDAP_DEBUG_FILTER, " unknown type\n", tag, 0,
316 return( LDAP_PROTOCOL_ERROR );
319 *fstr = ch_realloc( *fstr, strlen( *fstr ) + 3 );
320 if ( f->f_sub_final == NULL ) {
321 strcat( *fstr, "*" );
323 strcat( *fstr, ")" );
325 Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
330 filter_free( Filter *f )
338 switch ( f->f_choice ) {
339 case LDAP_FILTER_EQUALITY:
342 case LDAP_FILTER_APPROX:
343 ava_free( &f->f_ava, 0 );
346 case LDAP_FILTER_SUBSTRINGS:
347 if ( f->f_sub_type != NULL ) {
348 free( f->f_sub_type );
350 if ( f->f_sub_initial != NULL ) {
351 free( f->f_sub_initial );
353 charray_free( f->f_sub_any );
354 if ( f->f_sub_final != NULL ) {
355 free( f->f_sub_final );
359 case LDAP_FILTER_PRESENT:
360 if ( f->f_type != NULL ) {
365 case LDAP_FILTER_AND:
367 case LDAP_FILTER_NOT:
368 for ( p = f->f_list; p != NULL; p = next ) {
375 Debug( LDAP_DEBUG_ANY, "unknown filter type %lu\n",
385 filter_print( Filter *f )
391 fprintf( stderr, "NULL" );
394 switch ( f->f_choice ) {
395 case LDAP_FILTER_EQUALITY:
396 fprintf( stderr, "(%s=%s)", f->f_ava.ava_type,
397 f->f_ava.ava_value.bv_val );
401 fprintf( stderr, "(%s>=%s)", f->f_ava.ava_type,
402 f->f_ava.ava_value.bv_val );
406 fprintf( stderr, "(%s<=%s)", f->f_ava.ava_type,
407 f->f_ava.ava_value.bv_val );
410 case LDAP_FILTER_APPROX:
411 fprintf( stderr, "(%s~=%s)", f->f_ava.ava_type,
412 f->f_ava.ava_value.bv_val );
415 case LDAP_FILTER_SUBSTRINGS:
416 fprintf( stderr, "(%s=", f->f_sub_type );
417 if ( f->f_sub_initial != NULL ) {
418 fprintf( stderr, "%s", f->f_sub_initial );
420 if ( f->f_sub_any != NULL ) {
421 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
422 fprintf( stderr, "*%s", f->f_sub_any[i] );
425 charray_free( f->f_sub_any );
426 if ( f->f_sub_final != NULL ) {
427 fprintf( stderr, "*%s", f->f_sub_final );
431 case LDAP_FILTER_PRESENT:
432 fprintf( stderr, "%s=*", f->f_type );
435 case LDAP_FILTER_AND:
437 case LDAP_FILTER_NOT:
438 fprintf( stderr, "(%c", f->f_choice == LDAP_FILTER_AND ? '&' :
439 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
440 for ( p = f->f_list; p != NULL; p = p->f_next ) {
443 fprintf( stderr, ")" );
447 fprintf( stderr, "unknown type %lu", f->f_choice );
452 #endif /* ldap_debug */