]> git.sur5r.net Git - openldap/blob - servers/slapd/str2filter.c
Y2k copyright update
[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
149         Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
150
151         if ( (s = strchr( str, '=' )) == NULL ) {
152                 return( NULL );
153         }
154         value = &s[1];
155         *s-- = '\0';
156         savechar = *s;
157
158         f = (Filter *) ch_calloc( 1, sizeof(Filter) );
159
160         switch ( *s ) {
161         case '<':
162                 f->f_choice = LDAP_FILTER_LE;
163                 *s = '\0';
164                 break;
165         case '>':
166                 f->f_choice = LDAP_FILTER_GE;
167                 *s = '\0';
168                 break;
169         case '~':
170                 f->f_choice = LDAP_FILTER_APPROX;
171                 *s = '\0';
172                 break;
173         case ':':
174                 f->f_choice = LDAP_FILTER_EXT;
175                 *s = '\0';
176                 break;
177
178         default:
179                 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
180                         f->f_choice = LDAP_FILTER_EQUALITY;
181                 } else if ( strcmp( value, "*" ) == 0 ) {
182                         f->f_choice = LDAP_FILTER_PRESENT;
183                 } else {
184                         f->f_choice = LDAP_FILTER_SUBSTRINGS;
185 #ifndef SLAPD_SCHEMA_NOT_COMPAT
186                         f->f_sub_type = ch_strdup( str );
187                         if ( str2subvals( value, f ) != 0 ) {
188                                 filter_free( f );
189                                 *(value-1) = '=';
190                                 return( NULL );
191                         }
192                         *(value-1) = '=';
193                         return( f );
194 #endif
195                 }
196                 break;
197         }
198
199 #ifndef SLAPD_SCHEMA_NOT_COMPAT
200         if ( f->f_choice == LDAP_FILTER_PRESENT ) {
201                 f->f_type = ch_strdup( str );
202         } else {
203                 f->f_avtype = ch_strdup( str );
204                 f->f_avvalue.bv_val = ch_strdup( value );
205                 ldap_pvt_filter_value_unescape( f->f_avvalue.bv_val );
206                 f->f_avvalue.bv_len = strlen( value );
207         }
208
209         *s = savechar;
210         *(value-1) = '=';
211 #endif
212         return( f );
213 }
214
215 static int
216 str2subvals( const char *in, Filter *f )
217 {
218         char    *nextstar, *val, *freeme;
219         int     gotstar;
220
221         Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", in, 0, 0 );
222
223         if( in == NULL ) return 0;
224
225         val = freeme = ch_strdup( in );
226         gotstar = 0;
227
228         while ( *val ) {
229                 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
230                         *nextstar++ = '\0';
231
232                 ldap_pvt_filter_value_unescape( val );
233
234                 if ( gotstar == 0 ) {
235                         f->f_sub_initial = ber_bvstrdup( val );
236
237                 } else if ( nextstar == NULL ) {
238                         f->f_sub_final = ber_bvstrdup( val );
239
240                 } else {
241                         charray_add( (char ***) &f->f_sub_any, (char *) ber_bvstrdup( val ) );
242                 }
243
244                 gotstar = 1;
245                 val = nextstar;
246         }
247
248         free( freeme );
249         return( 0 );
250 }
251
252 /*
253  * find_matching_paren - return a pointer to the right paren in s matching
254  * the left paren to which *s currently points
255  */
256
257 static char *
258 find_matching_paren( const char *s )
259 {
260         int     balance, escape;
261
262         balance = 0;
263         escape = 0;
264         for ( ; *s; s++ ) {
265                 if ( escape == 0 ) {
266                         if ( *s == '(' )
267                                 balance++;
268                         else if ( *s == ')' )
269                                 balance--;
270                 }
271                 if ( balance == 0 ) {
272                         return (char *) s;
273                 }
274                 if ( *s == '\\' && ! escape )
275                         escape = 1;
276                 else
277                         escape = 0;
278         }
279
280         return NULL;
281 }