]> git.sur5r.net Git - openldap/blob - servers/slapd/str2filter.c
add new ber dump routine (behind NEW_LOGGING)
[openldap] / servers / slapd / str2filter.c
1 /* str2filter.c - parse an rfc 1588 string filter */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/ctype.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17 #include <ldap_pvt.h>
18
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);
23
24 Filter *
25 str2filter( const char *str )
26 {
27         Filter  *f = NULL;
28         char    *end, *freeme;
29
30         Debug( LDAP_DEBUG_FILTER, "str2filter \"%s\"\n", str, 0, 0 );
31
32         if ( str == NULL || *str == '\0' ) {
33                 return( NULL );
34         }
35
36         str = freeme = ch_strdup( str );
37
38         switch ( *str ) {
39         case '(':
40                 if ( (end = find_matching_paren( str )) == NULL ) {
41                         filter_free( f );
42                         free( freeme );
43                         return( NULL );
44                 }
45                 *end = '\0';
46
47                 str++;
48                 switch ( *str ) {
49                 case '&':
50                         Debug( LDAP_DEBUG_FILTER, "str2filter: AND\n",
51                             0, 0, 0 );
52
53                         str++;
54                         f = str2list( str, LDAP_FILTER_AND );
55                         break;
56
57                 case '|':
58                         Debug( LDAP_DEBUG_FILTER, "put_filter: OR\n",
59                             0, 0, 0 );
60
61                         str++;
62                         f = str2list( str, LDAP_FILTER_OR );
63                         break;
64
65                 case '!':
66                         Debug( LDAP_DEBUG_FILTER, "put_filter: NOT\n",
67                             0, 0, 0 );
68
69                         str++;
70                         f = str2list( str, LDAP_FILTER_NOT );
71                         break;
72
73                 default:
74                         Debug( LDAP_DEBUG_FILTER, "str2filter: simple\n",
75                             0, 0, 0 );
76
77                         f = str2simple( str );
78                         break;
79                 }
80                 *end = ')';
81                 break;
82
83         default:        /* assume it's a simple type=value filter */
84                 Debug( LDAP_DEBUG_FILTER, "str2filter: default\n", 0, 0,
85                     0 );
86
87                 f = str2simple( str );
88                 break;
89         }
90
91         free( freeme );
92         return( f );
93 }
94
95 /*
96  * Put a list of filters like this "(filter1)(filter2)..."
97  */
98
99 static Filter *
100 str2list( const char *str, unsigned long ftype )
101 {
102         Filter  *f;
103         Filter  **fp;
104         char    *next;
105         char    save;
106
107         Debug( LDAP_DEBUG_FILTER, "str2list \"%s\"\n", str, 0, 0 );
108
109         f = (Filter *) ch_calloc( 1, sizeof(Filter) );
110         f->f_choice = ftype;
111         fp = &f->f_list;
112
113         while ( *str ) {
114                 while ( *str && isspace( (unsigned char) *str ) )
115                         str++;
116                 if ( *str == '\0' )
117                         break;
118
119                 if ( (next = find_matching_paren( str )) == NULL ) {
120                         filter_free( f );
121                         return( NULL );
122                 }
123                 save = *++next;
124                 *next = '\0';
125
126                 /* now we have "(filter)" with str pointing to it */
127                 if ( (*fp = str2filter( str )) == NULL ) {
128                         filter_free( f );
129                         *next = save;
130                         return( NULL );
131                 }
132                 *next = save;
133
134                 str = next;
135                 fp = &(*fp)->f_next;
136         }
137         *fp = NULL;
138
139         return( f );
140 }
141
142 static Filter *
143 str2simple( const char *str )
144 {
145         Filter          *f;
146         char            *s;
147         char            *value, savechar;
148         int                     rc;
149         const char              *text;
150
151         Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
152
153         if ( (s = strchr( str, '=' )) == NULL ) {
154                 return( NULL );
155         }
156         value = &s[1];
157
158         *s-- = '\0';    /* we shouldn't be mucking with str */
159         savechar = *s;
160
161         f = (Filter *) ch_calloc( 1, sizeof(Filter) );
162
163         switch ( *s ) {
164         case '<':
165                 f->f_choice = LDAP_FILTER_LE;
166                 *s = '\0';
167                 break;
168         case '>':
169                 f->f_choice = LDAP_FILTER_GE;
170                 *s = '\0';
171                 break;
172         case '~':
173                 f->f_choice = LDAP_FILTER_APPROX;
174                 *s = '\0';
175                 break;
176         case ':':
177                 f->f_choice = LDAP_FILTER_EXT;
178                 *s = '\0';
179                 return NULL;
180                 break;
181
182         default:
183                 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
184                         f->f_choice = LDAP_FILTER_EQUALITY;
185                 } else if ( strcmp( value, "*" ) == 0 ) {
186                         f->f_choice = LDAP_FILTER_PRESENT;
187                 } else {
188                         f->f_choice = LDAP_FILTER_SUBSTRINGS;
189                         f->f_sub = ch_calloc( 1, sizeof( SubstringsAssertion ) );
190                         rc = slap_str2ad( str, &f->f_sub_desc, &text );
191                         if( rc != LDAP_SUCCESS ) {
192                                 filter_free( f );
193                                 *(value-1) = '=';
194                                 return NULL;
195                         }
196                         if ( str2subvals( value, f ) != 0 ) {
197                                 filter_free( f );
198                                 *(value-1) = '=';
199                                 return( NULL );
200                         }
201                         *(value-1) = '=';
202                         return( f );
203                 }
204                 break;
205         }
206
207         if ( f->f_choice == LDAP_FILTER_PRESENT ) {
208                 rc = slap_str2ad( str, &f->f_desc, &text );
209                 if( rc != LDAP_SUCCESS ) {
210                         filter_free( f );
211                         *(value-1) = '=';
212                         return NULL;
213                 }
214         } else {
215                 char *tmp;
216
217                 f->f_ava = ch_calloc( 1, sizeof( AttributeAssertion ) );
218                 f->f_av_desc = NULL;
219                 rc = slap_str2ad( str, &f->f_av_desc, &text );
220                 if( rc != LDAP_SUCCESS ) {
221                         filter_free( f );
222                         *(value-1) = '=';
223                         return NULL;
224                 }
225
226                 tmp = ch_strdup( value );
227                 ldap_pvt_filter_value_unescape( tmp );
228                 f->f_av_value = ber_bvstr( tmp );
229         }
230
231         *s = savechar;
232         *(value-1) = '=';
233
234         return( f );
235 }
236
237 static int
238 str2subvals( const char *in, Filter *f )
239 {
240         char    *nextstar, *val, *freeme;
241         int     gotstar;
242
243         Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", in, 0, 0 );
244
245         if( in == NULL ) return 0;
246
247         val = freeme = ch_strdup( in );
248         gotstar = 0;
249
250         while ( *val ) {
251                 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
252                         *nextstar++ = '\0';
253
254                 ldap_pvt_filter_value_unescape( val );
255
256                 if ( gotstar == 0 ) {
257                         f->f_sub_initial = ber_bvstrdup( val );
258
259                 } else if ( nextstar == NULL ) {
260                         f->f_sub_final = ber_bvstrdup( val );
261
262                 } else {
263                         charray_add( (char ***) &f->f_sub_any, (char *) ber_bvstrdup( val ) );
264                 }
265
266                 gotstar = 1;
267                 val = nextstar;
268         }
269
270         free( freeme );
271         return( 0 );
272 }
273
274 /*
275  * find_matching_paren - return a pointer to the right paren in s matching
276  * the left paren to which *s currently points
277  */
278
279 static char *
280 find_matching_paren( const char *s )
281 {
282         int     balance, escape;
283
284         balance = 0;
285         escape = 0;
286         for ( ; *s; s++ ) {
287                 if ( escape == 0 ) {
288                         if ( *s == '(' )
289                                 balance++;
290                         else if ( *s == ')' )
291                                 balance--;
292                 }
293                 if ( balance == 0 ) {
294                         return (char *) s;
295                 }
296                 if ( *s == '\\' && ! escape )
297                         escape = 1;
298                 else
299                         escape = 0;
300         }
301
302         return NULL;
303 }