1 /* str2filter.c - parse an rfc 1588 string filter */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
14 #include <ac/socket.h>
19 static char *find_matching_paren( const char *s );
20 static Filter *str2list( const char *str, long unsigned int ftype);
21 static Filter *str2simple( const char *str);
22 static int str2subvals( const char *val, Filter *f);
25 str2filter( const char *str )
31 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
32 "str2filter: \"%s\"\n", str ));
34 Debug( LDAP_DEBUG_FILTER, "str2filter \"%s\"\n", str, 0, 0 );
38 if ( str == NULL || *str == '\0' ) {
42 str = freeme = ch_strdup( str );
46 if ( (end = find_matching_paren( str )) == NULL ) {
57 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
58 "str2filter: AND\n" ));
60 Debug( LDAP_DEBUG_FILTER, "str2filter: AND\n",
66 f = str2list( str, LDAP_FILTER_AND );
71 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
72 "str2filter: OR\n" ));
74 Debug( LDAP_DEBUG_FILTER, "put_filter: OR\n",
80 f = str2list( str, LDAP_FILTER_OR );
85 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
86 "str2filter: NOT\n" ));
88 Debug( LDAP_DEBUG_FILTER, "put_filter: NOT\n",
94 f = str2list( str, LDAP_FILTER_NOT );
99 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
100 "str2filter: simple\n" ));
102 Debug( LDAP_DEBUG_FILTER, "str2filter: simple\n",
107 f = str2simple( str );
113 default: /* assume it's a simple type=value filter */
115 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
116 "str2filter: default\n" ));
118 Debug( LDAP_DEBUG_FILTER, "str2filter: default\n", 0, 0,
123 f = str2simple( str );
132 * Put a list of filters like this "(filter1)(filter2)..."
136 str2list( const char *str, unsigned long ftype )
144 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
145 "str2list: \"%s\"\n", str ));
147 Debug( LDAP_DEBUG_FILTER, "str2list \"%s\"\n", str, 0, 0 );
151 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
156 while ( *str && isspace( (unsigned char) *str ) )
161 if ( (next = find_matching_paren( str )) == NULL ) {
168 /* now we have "(filter)" with str pointing to it */
169 if ( (*fp = str2filter( str )) == NULL ) {
185 str2simple( const char *str )
189 char *value, savechar;
194 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
195 "str2simple: \"%s\"\n", str ));
197 Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
201 if ( (s = strchr( str, '=' )) == NULL ) {
206 *s-- = '\0'; /* we shouldn't be mucking with str */
209 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
213 f->f_choice = LDAP_FILTER_LE;
217 f->f_choice = LDAP_FILTER_GE;
221 f->f_choice = LDAP_FILTER_APPROX;
225 f->f_choice = LDAP_FILTER_EXT;
231 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
232 f->f_choice = LDAP_FILTER_EQUALITY;
233 } else if ( strcmp( value, "*" ) == 0 ) {
234 f->f_choice = LDAP_FILTER_PRESENT;
236 f->f_choice = LDAP_FILTER_SUBSTRINGS;
237 f->f_sub = ch_calloc( 1, sizeof( SubstringsAssertion ) );
238 rc = slap_str2ad( str, &f->f_sub_desc, &text );
239 if( rc != LDAP_SUCCESS ) {
244 if ( str2subvals( value, f ) != 0 ) {
255 if ( f->f_choice == LDAP_FILTER_PRESENT ) {
256 rc = slap_str2ad( str, &f->f_desc, &text );
257 if( rc != LDAP_SUCCESS ) {
265 f->f_ava = ch_calloc( 1, sizeof( AttributeAssertion ) );
267 rc = slap_str2ad( str, &f->f_av_desc, &text );
268 if( rc != LDAP_SUCCESS ) {
274 tmp = ch_strdup( value );
275 ldap_pvt_filter_value_unescape( tmp );
276 f->f_av_value = ber_bvstr( tmp );
286 str2subvals( const char *in, Filter *f )
288 char *nextstar, *val, *freeme;
292 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
293 "str2subvals: \"%s\"\n", in ));
295 Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", in, 0, 0 );
299 if( in == NULL ) return 0;
301 val = freeme = ch_strdup( in );
305 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
308 ldap_pvt_filter_value_unescape( val );
310 if ( gotstar == 0 ) {
311 f->f_sub_initial = ber_bvstrdup( val );
313 } else if ( nextstar == NULL ) {
314 f->f_sub_final = ber_bvstrdup( val );
317 charray_add( (char ***) &f->f_sub_any, (char *) ber_bvstrdup( val ) );
329 * find_matching_paren - return a pointer to the right paren in s matching
330 * the left paren to which *s currently points
334 find_matching_paren( const char *s )
344 else if ( *s == ')' )
347 if ( balance == 0 ) {
350 if ( *s == '\\' && ! escape )