1 /* filter.c - routines for parsing and dealing with filters */
6 #include <sys/socket.h>
9 static int get_filter_list();
10 static int get_substring_filter();
13 extern char *ch_malloc();
14 extern char *ch_realloc();
17 get_filter( Connection *conn, BerElement *ber, Filter **filt, char **fstr )
19 unsigned long tag, len;
24 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
27 * A filter looks like this coming in:
29 * and [0] SET OF Filter,
30 * or [1] SET OF Filter,
32 * equalityMatch [3] AttributeValueAssertion,
33 * substrings [4] SubstringFilter,
34 * greaterOrEqual [5] AttributeValueAssertion,
35 * lessOrEqual [6] AttributeValueAssertion,
36 * present [7] AttributeType,,
37 * approxMatch [8] AttributeValueAssertion
40 * SubstringFilter ::= SEQUENCE {
42 * SEQUENCE OF CHOICE {
43 * initial [0] IA5String,
50 f = (Filter *) ch_malloc( sizeof(Filter) );
56 f->f_choice = ber_peek_tag( ber, &len );
58 if ( conn->c_version == 30 ) {
59 switch ( f->f_choice ) {
60 case LDAP_FILTER_EQUALITY:
63 case LDAP_FILTER_PRESENT:
64 case LDAP_FILTER_PRESENT_30:
65 case LDAP_FILTER_APPROX:
66 (void) ber_skip_tag( ber, &len );
67 if ( f->f_choice == LDAP_FILTER_PRESENT_30 ) {
68 f->f_choice = LDAP_FILTER_PRESENT;
76 switch ( f->f_choice ) {
77 case LDAP_FILTER_EQUALITY:
78 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
79 if ( (err = get_ava( ber, &f->f_ava )) == 0 ) {
80 *fstr = ch_malloc(4 + strlen( f->f_avtype ) +
82 sprintf( *fstr, "(%s=%s)", f->f_avtype,
83 f->f_avvalue.bv_val );
87 case LDAP_FILTER_SUBSTRINGS:
88 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
89 err = get_substring_filter( conn, ber, f, fstr );
93 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
94 if ( (err = get_ava( ber, &f->f_ava )) == 0 ) {
95 *fstr = ch_malloc(5 + strlen( f->f_avtype ) +
97 sprintf( *fstr, "(%s>=%s)", f->f_avtype,
98 f->f_avvalue.bv_val );
103 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
104 if ( (err = get_ava( ber, &f->f_ava )) == 0 ) {
105 *fstr = ch_malloc(5 + strlen( f->f_avtype ) +
106 f->f_avvalue.bv_len);
107 sprintf( *fstr, "(%s<=%s)", f->f_avtype,
108 f->f_avvalue.bv_val );
112 case LDAP_FILTER_PRESENT:
113 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
114 if ( ber_scanf( ber, "a", &f->f_type ) == LBER_ERROR ) {
115 err = LDAP_PROTOCOL_ERROR;
118 attr_normalize( f->f_type );
119 *fstr = ch_malloc( 5 + strlen( f->f_type ) );
120 sprintf( *fstr, "(%s=*)", f->f_type );
124 case LDAP_FILTER_APPROX:
125 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
126 if ( (err = get_ava( ber, &f->f_ava )) == 0 ) {
127 *fstr = ch_malloc(5 + strlen( f->f_avtype ) +
128 f->f_avvalue.bv_len);
129 sprintf( *fstr, "(%s~=%s)", f->f_avtype,
130 f->f_avvalue.bv_val );
134 case LDAP_FILTER_AND:
135 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
136 if ( (err = get_filter_list( conn, ber, &f->f_and, &ftmp ))
138 if (ftmp == NULL) ftmp = strdup("");
139 *fstr = ch_malloc( 4 + strlen( ftmp ) );
140 sprintf( *fstr, "(&%s)", ftmp );
146 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
147 if ( (err = get_filter_list( conn, ber, &f->f_or, &ftmp ))
149 if (ftmp == NULL) ftmp = strdup("");
150 *fstr = ch_malloc( 4 + strlen( ftmp ) );
151 sprintf( *fstr, "(|%s)", ftmp );
156 case LDAP_FILTER_NOT:
157 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
158 (void) ber_skip_tag( ber, &len );
159 if ( (err = get_filter( conn, ber, &f->f_not, &ftmp )) == 0 ) {
160 if (ftmp == NULL) ftmp = strdup("");
161 *fstr = ch_malloc( 4 + strlen( ftmp ) );
162 sprintf( *fstr, "(!%s)", ftmp );
168 Debug( LDAP_DEBUG_ANY, "unknown filter type %d\n", f->f_choice,
170 err = LDAP_PROTOCOL_ERROR;
176 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 %d\n", f->f_choice,
385 filter_print( Filter *f )
394 switch ( f->f_choice ) {
395 case LDAP_FILTER_EQUALITY:
396 printf( "(%s=%s)", f->f_ava.ava_type,
397 f->f_ava.ava_value.bv_val );
401 printf( "(%s>=%s)", f->f_ava.ava_type,
402 f->f_ava.ava_value.bv_val );
406 printf( "(%s<=%s)", f->f_ava.ava_type,
407 f->f_ava.ava_value.bv_val );
410 case LDAP_FILTER_APPROX:
411 printf( "(%s~=%s)", f->f_ava.ava_type,
412 f->f_ava.ava_value.bv_val );
415 case LDAP_FILTER_SUBSTRINGS:
416 printf( "(%s=", f->f_sub_type );
417 if ( f->f_sub_initial != NULL ) {
418 printf( "%s", f->f_sub_initial );
420 if ( f->f_sub_any != NULL ) {
421 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
422 printf( "*%s", f->f_sub_any[i] );
425 charray_free( f->f_sub_any );
426 if ( f->f_sub_final != NULL ) {
427 printf( "*%s", f->f_sub_final );
431 case LDAP_FILTER_PRESENT:
432 printf( "%s=*", f->f_type );
435 case LDAP_FILTER_AND:
437 case LDAP_FILTER_NOT:
438 printf( "(%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 ) {
447 printf( "unknown type %d", f->f_choice );
452 #endif /* ldap_debug */