]> git.sur5r.net Git - openldap/blob - servers/slapd/filterentry.c
Import Hallvards idl memory plugs from devel.
[openldap] / servers / slapd / filterentry.c
1 /* filterentry.c - apply a filter to an entry */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8 #include <ac/string.h>
9 #include <ac/regex.h>
10
11 #include "slap.h"
12
13 static int      test_filter_list(Backend *be, Connection *conn, Operation *op, Entry *e, Filter *flist, int ftype);
14 static int      test_substring_filter(Backend *be, Connection *conn, Operation *op, Entry *e, Filter *f);
15 static int      test_ava_filter(Backend *be, Connection *conn, Operation *op, Entry *e, Ava *ava, int type);
16 static int      test_approx_filter(Backend *be, Connection *conn, Operation *op, Entry *e, Ava *ava);
17 static int      test_presence_filter(Backend *be, Connection *conn, Operation *op, Entry *e, char *type);
18
19 /*
20  * test_filter - test a filter against a single entry.
21  * returns      0       filter matched
22  *              -1      filter did not match
23  *              >0      an ldap error code
24  */
25
26 int
27 test_filter(
28     Backend     *be,
29     Connection  *conn,
30     Operation   *op,
31     Entry       *e,
32     Filter      *f
33 )
34 {
35         int     rc;
36
37         Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
38
39         switch ( f->f_choice ) {
40         case LDAP_FILTER_EQUALITY:
41                 Debug( LDAP_DEBUG_FILTER, "    EQUALITY\n", 0, 0, 0 );
42                 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
43                     LDAP_FILTER_EQUALITY );
44                 break;
45
46         case LDAP_FILTER_SUBSTRINGS:
47                 Debug( LDAP_DEBUG_FILTER, "    SUBSTRINGS\n", 0, 0, 0 );
48                 rc = test_substring_filter( be, conn, op, e, f );
49                 break;
50
51         case LDAP_FILTER_GE:
52                 Debug( LDAP_DEBUG_FILTER, "    GE\n", 0, 0, 0 );
53                 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
54                     LDAP_FILTER_GE );
55                 break;
56
57         case LDAP_FILTER_LE:
58                 Debug( LDAP_DEBUG_FILTER, "    LE\n", 0, 0, 0 );
59                 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
60                     LDAP_FILTER_LE );
61                 break;
62
63         case LDAP_FILTER_PRESENT:
64                 Debug( LDAP_DEBUG_FILTER, "    PRESENT\n", 0, 0, 0 );
65                 rc = test_presence_filter( be, conn, op, e, f->f_type );
66                 break;
67
68         case LDAP_FILTER_APPROX:
69                 Debug( LDAP_DEBUG_FILTER, "    APPROX\n", 0, 0, 0 );
70                 rc = test_approx_filter( be, conn, op, e, &f->f_ava );
71                 break;
72
73         case LDAP_FILTER_AND:
74                 Debug( LDAP_DEBUG_FILTER, "    AND\n", 0, 0, 0 );
75                 rc = test_filter_list( be, conn, op, e, f->f_and,
76                     LDAP_FILTER_AND );
77                 break;
78
79         case LDAP_FILTER_OR:
80                 Debug( LDAP_DEBUG_FILTER, "    OR\n", 0, 0, 0 );
81                 rc = test_filter_list( be, conn, op, e, f->f_or,
82                     LDAP_FILTER_OR );
83                 break;
84
85         case LDAP_FILTER_NOT:
86                 Debug( LDAP_DEBUG_FILTER, "    NOT\n", 0, 0, 0 );
87                 rc = (! test_filter( be, conn, op, e, f->f_not ) );
88                 break;
89
90         default:
91                 Debug( LDAP_DEBUG_ANY, "    unknown filter type %lu\n",
92                     f->f_choice, 0, 0 );
93                 rc = -1;
94         }
95
96         Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
97         return( rc );
98 }
99
100 static int
101 test_ava_filter(
102     Backend     *be,
103     Connection  *conn,
104     Operation   *op,
105     Entry       *e,
106     Ava         *ava,
107     int         type
108 )
109 {
110         int             i, rc;
111         Attribute       *a;
112
113         if ( be != NULL && ! access_allowed( be, conn, op, e,
114                 ava->ava_type, &ava->ava_value, ACL_SEARCH ) )
115         {
116                 return( -2 );
117         }
118
119         if ( (a = attr_find( e->e_attrs, ava->ava_type )) == NULL ) {
120                 return( -1 );
121         }
122
123         if ( a->a_syntax == 0 ) {
124                 a->a_syntax = attr_syntax( ava->ava_type );
125         }
126         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
127                 rc = value_cmp( a->a_vals[i], &ava->ava_value, a->a_syntax,
128                     3 );
129
130                 switch ( type ) {
131                 case LDAP_FILTER_EQUALITY:
132                         if ( rc == 0 ) {
133                                 return( 0 );
134                         }
135                         break;
136
137                 case LDAP_FILTER_GE:
138                         if ( rc >= 0 ) {
139                                 return( 0 );
140                         }
141                         break;
142
143                 case LDAP_FILTER_LE:
144                         if ( rc <= 0 ) {
145                                 return( 0 );
146                         }
147                         break;
148                 }
149         }
150
151         return( 1 );
152 }
153
154 static int
155 test_presence_filter(
156     Backend     *be,
157     Connection  *conn,
158     Operation   *op,
159     Entry       *e,
160     char        *type
161 )
162 {
163         if ( be != NULL && ! access_allowed( be, conn, op, e,
164                 type, NULL, ACL_SEARCH ) )
165         {
166                 return( -2 );
167         }
168
169         return( attr_find( e->e_attrs, type ) != NULL ? 0 : -1 );
170 }
171
172 static int
173 test_approx_filter(
174     Backend     *be,
175     Connection  *conn,
176     Operation   *op,
177     Entry       *e,
178     Ava         *ava
179 )
180 {
181         char            *w1, *w2, *c1, *c2;
182         int             i, rc, match;
183         Attribute       *a;
184
185         if ( be != NULL && ! access_allowed( be, conn, op, e,
186                 ava->ava_type, NULL, ACL_SEARCH ) )
187         {
188                 return( -2 );
189         }
190
191         if ( (a = attr_find( e->e_attrs, ava->ava_type )) == NULL ) {
192                 return( -1 );
193         }
194
195         /* for each value in the attribute */
196         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
197                 /*
198                  * try to match words in the filter value in order
199                  * in the attribute value.
200                  */
201
202                 w2 = a->a_vals[i]->bv_val;
203                 /* for each word in the filter value */
204                 for ( w1 = first_word( ava->ava_value.bv_val ); w1 != NULL;
205                     w1 = next_word( w1 ) ) {
206                         if ( (c1 = phonetic( w1 )) == NULL ) {
207                                 break;
208                         }
209
210                         /*
211                          * for each word in the attribute value from
212                          * where we left off...
213                          */
214                         for ( w2 = first_word( w2 ); w2 != NULL;
215                             w2 = next_word( w2 ) ) {
216                                 c2 = phonetic( w2 );
217                                 if ( strcmp( c1, c2 ) == 0 ) {
218                                         free( c2 );
219                                         break;
220                                 }
221                                 free( c2 );
222                         }
223                         free( c1 );
224
225                         /*
226                          * if we stopped because we ran out of words
227                          * before making a match, go on to the next
228                          * value.  otherwise try to keep matching
229                          * words in this value from where we left off.
230                          */
231                         if ( w2 == NULL ) {
232                                 break;
233                         } else {
234                                 w2 = next_word( w2 );
235                         }
236                 }
237                 /*
238                  * if we stopped because we ran out of words we
239                  * have a match.
240                  */
241                 if ( w1 == NULL ) {
242                         return( 0 );
243                 }
244         }
245
246         return( 1 );
247 }
248
249 static int
250 test_filter_list(
251     Backend     *be,
252     Connection  *conn,
253     Operation   *op,
254     Entry       *e,
255     Filter      *flist,
256     int         ftype
257 )
258 {
259         int     rc, nomatch;
260         Filter  *f;
261
262         Debug( LDAP_DEBUG_FILTER, "=> test_filter_list\n", 0, 0, 0 );
263
264         nomatch = 1;
265         for ( f = flist; f != NULL; f = f->f_next ) {
266                 if ( test_filter( be, conn, op, e, f ) != 0 ) {
267                         if ( ftype == LDAP_FILTER_AND ) {
268                                 Debug( LDAP_DEBUG_FILTER,
269                                     "<= test_filter_list 1\n", 0, 0, 0 );
270                                 return( 1 );
271                         }
272                 } else {
273                         nomatch = 0;
274                 }
275         }
276
277         Debug( LDAP_DEBUG_FILTER, "<= test_filter_list %d\n", nomatch, 0, 0 );
278         return( nomatch );
279 }
280
281 static void
282 strcpy_special( char *d, char *s )
283 {
284         for ( ; *s; s++ ) {
285                 switch ( *s ) {
286                 case '.':
287                 case '\\':
288                 case '[':
289                 case ']':
290                 case '*':
291                 case '+':
292                 case '^':
293                 case '$':
294                         *d++ = '\\';
295                         /* FALL */
296                 default:
297                         *d++ = *s;
298                 }
299         }
300         *d = '\0';
301 }
302
303 static int
304 test_substring_filter(
305     Backend     *be,
306     Connection  *conn,
307     Operation   *op,
308     Entry       *e,
309     Filter      *f
310 )
311 {
312         Attribute       *a;
313         int             i, rc;
314         char            *p, *end, *realval, *tmp;
315         char            pat[BUFSIZ];
316         char            buf[BUFSIZ];
317         struct berval   *val;
318         regex_t         re;
319
320         Debug( LDAP_DEBUG_FILTER, "begin test_substring_filter\n", 0, 0, 0 );
321
322         if ( be != NULL && ! access_allowed( be, conn, op, e,
323                 f->f_sub_type, NULL, ACL_SEARCH ) )
324         {
325                 return( -2 );
326         }
327
328         if ( (a = attr_find( e->e_attrs, f->f_sub_type )) == NULL ) {
329                 return( -1 );
330         }
331
332         if ( a->a_syntax & SYNTAX_BIN ) {
333                 Debug( LDAP_DEBUG_FILTER, "test_substring_filter bin attr\n",
334                     0, 0, 0 );
335                 return( -1 );
336         }
337
338         /*
339          * construct a regular expression corresponding to the
340          * filter and let regex do the work
341          */
342
343         pat[0] = '\0';
344         p = pat;
345         end = pat + sizeof(pat) - 2;    /* leave room for null */
346         if ( f->f_sub_initial != NULL ) {
347                 strcpy( p, "^" );
348                 p = strchr( p, '\0' );
349                 /* 2 * in case every char is special */
350                 if ( p + 2 * strlen( f->f_sub_initial ) > end ) {
351                         Debug( LDAP_DEBUG_ANY, "not enough pattern space\n",
352                             0, 0, 0 );
353                         return( -1 );
354                 }
355                 strcpy_special( p, f->f_sub_initial );
356                 p = strchr( p, '\0' );
357         }
358         if ( f->f_sub_any != NULL ) {
359                 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
360                         /* ".*" + value */
361                         if ( p + 2 * strlen( f->f_sub_any[i] ) + 2 > end ) {
362                                 Debug( LDAP_DEBUG_ANY,
363                                     "not enough pattern space\n", 0, 0, 0 );
364                                 return( -1 );
365                         }
366                         strcpy( p, ".*" );
367                         p = strchr( p, '\0' );
368                         strcpy_special( p, f->f_sub_any[i] );
369                         p = strchr( p, '\0' );
370                 }
371         }
372         if ( f->f_sub_final != NULL ) {
373                 /* ".*" + value */
374                 if ( p + 2 * strlen( f->f_sub_final ) + 2 > end ) {
375                         Debug( LDAP_DEBUG_ANY, "not enough pattern space\n",
376                             0, 0, 0 );
377                         return( -1 );
378                 }
379                 strcpy( p, ".*" );
380                 p = strchr( p, '\0' );
381                 strcpy_special( p, f->f_sub_final );
382                 p = strchr( p, '\0' );
383                 strcpy( p, "$" );
384         }
385
386         /* compile the regex */
387         Debug( LDAP_DEBUG_FILTER, "test_substring_filter: regcomp pat: %s\n",
388                 pat, 0, 0 );
389         if ((rc = regcomp(&re, pat, 0))) {
390                 char error[512];
391
392                 regerror(rc, &re, error, sizeof(error));
393                 Debug( LDAP_DEBUG_ANY, "regcomp failed (%s) %s\n",
394                         p, error, 0 );
395                 return( -1 );
396         }
397
398         /* for each value in the attribute see if regex matches */
399         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
400                 val = a->a_vals[i];
401                 tmp = NULL;
402                 if ( val->bv_len < sizeof(buf) ) {
403                         strcpy( buf, val->bv_val );
404                         realval = buf;
405                 } else {
406                         tmp = (char *) ch_malloc( val->bv_len + 1 );
407                         strcpy( tmp, val->bv_val );
408                         realval = tmp;
409                 }
410                 value_normalize( realval, a->a_syntax );
411
412                 rc = !regexec(&re, realval, 0, NULL, 0);
413
414                 if ( tmp != NULL ) {
415                         free( tmp );
416                 }
417                 if ( rc == 1 ) {
418                         regfree(&re);
419                         return( 0 );
420                 }
421         }
422
423         regfree(&re);
424
425         Debug( LDAP_DEBUG_FILTER, "end test_substring_filter 1\n", 0, 0, 0 );
426         return( 1 );
427 }