]> git.sur5r.net Git - openldap/blob - servers/slapd/filterentry.c
d960ac3209887c846c45cbbf501458876ad6020e
[openldap] / servers / slapd / filterentry.c
1 /* filterentry.c - apply a filter to an entry */
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/socket.h>
13 #include <ac/string.h>
14
15
16 #include "slap.h"
17
18 static int      test_filter_and( Backend *be,
19         Connection *conn, Operation *op,
20         Entry *e, Filter *flist );
21 static int      test_filter_or( Backend *be,
22         Connection *conn, Operation *op,
23         Entry *e, Filter *flist );
24 static int      test_substrings_filter( Backend *be,
25         Connection *conn, Operation *op,
26         Entry *e, Filter *f);
27 static int      test_ava_filter( Backend *be,
28         Connection *conn, Operation *op,
29         Entry *e, AttributeAssertion *ava, int type );
30 static int      test_mra_filter( Backend *be,
31         Connection *conn, Operation *op,
32         Entry *e, MatchingRuleAssertion *mra );
33 static int      test_presence_filter( Backend *be,
34         Connection *conn, Operation *op,
35         Entry *e, AttributeDescription *desc );
36
37
38 /*
39  * test_filter - test a filter against a single entry.
40  * returns:
41  *              LDAP_COMPARE_TRUE               filter matched
42  *              LDAP_COMPARE_FALSE              filter did not match
43  *              SLAPD_COMPARE_UNDEFINED filter is undefined
44  *      or an ldap result code indicating error
45  */
46
47 int
48 test_filter(
49     Backend     *be,
50     Connection  *conn,
51     Operation   *op,
52     Entry       *e,
53     Filter      *f
54 )
55 {
56         int     rc;
57
58 #ifdef NEW_LOGGING
59         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
60                    "test_filter: begin\n" ));
61 #else
62         Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
63 #endif
64
65
66         switch ( f->f_choice ) {
67         case SLAPD_FILTER_COMPUTED:
68 #ifdef NEW_LOGGING
69                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
70                            "test_filter:   COMPUTED %s (%d)\n",
71                            f->f_result == LDAP_COMPARE_FALSE ? "false" :
72                            f->f_result == LDAP_COMPARE_TRUE      ? "true"  :
73                            f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
74                            "error",
75                            f->f_result ));
76 #else
77                 Debug( LDAP_DEBUG_FILTER, "    COMPUTED %s (%d)\n",
78                         f->f_result == LDAP_COMPARE_FALSE ? "false" :
79                         f->f_result == LDAP_COMPARE_TRUE ? "true" :
80                         f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : "error",
81                         f->f_result, 0 );
82 #endif
83
84                 rc = f->f_result;
85                 break;
86
87         case LDAP_FILTER_EQUALITY:
88 #ifdef NEW_LOGGING
89                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
90                            "test_filter:   EQUALITY\n" ));
91 #else
92                 Debug( LDAP_DEBUG_FILTER, "    EQUALITY\n", 0, 0, 0 );
93 #endif
94
95                 rc = test_ava_filter( be, conn, op, e, f->f_ava,
96                     LDAP_FILTER_EQUALITY );
97                 break;
98
99         case LDAP_FILTER_SUBSTRINGS:
100 #ifdef NEW_LOGGING
101                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
102                            "test_filter  SUBSTRINGS\n" ));
103 #else
104                 Debug( LDAP_DEBUG_FILTER, "    SUBSTRINGS\n", 0, 0, 0 );
105 #endif
106
107                 rc = test_substrings_filter( be, conn, op, e, f );
108                 break;
109
110         case LDAP_FILTER_GE:
111                 rc = test_ava_filter( be, conn, op, e, f->f_ava,
112                     LDAP_FILTER_GE );
113                 break;
114
115         case LDAP_FILTER_LE:
116                 rc = test_ava_filter( be, conn, op, e, f->f_ava,
117                     LDAP_FILTER_LE );
118                 break;
119
120         case LDAP_FILTER_PRESENT:
121 #ifdef NEW_LOGGING
122                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
123                            "test_filter:        PRESENT\n" ));
124 #else
125                 Debug( LDAP_DEBUG_FILTER, "    PRESENT\n", 0, 0, 0 );
126 #endif
127
128                 rc = test_presence_filter( be, conn, op, e, f->f_desc );
129                 break;
130
131         case LDAP_FILTER_APPROX:
132 #ifdef NEW_LOGGING
133                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
134                            "test_filter: APPROX\n" ));
135 #else
136                 Debug( LDAP_DEBUG_FILTER, "    APPROX\n", 0, 0, 0 );
137 #endif
138                 rc = test_ava_filter( be, conn, op, e, f->f_ava,
139                     LDAP_FILTER_APPROX );
140                 break;
141
142         case LDAP_FILTER_AND:
143 #ifdef NEW_LOGGING
144                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
145                            "test_filter:  AND\n" ));
146 #else
147                 Debug( LDAP_DEBUG_FILTER, "    AND\n", 0, 0, 0 );
148 #endif
149
150                 rc = test_filter_and( be, conn, op, e, f->f_and );
151                 break;
152
153         case LDAP_FILTER_OR:
154 #ifdef NEW_LOGGING
155                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
156                            "test_filter:        OR\n" ));
157 #else
158                 Debug( LDAP_DEBUG_FILTER, "    OR\n", 0, 0, 0 );
159 #endif
160
161                 rc = test_filter_or( be, conn, op, e, f->f_or );
162                 break;
163
164         case LDAP_FILTER_NOT:
165 #ifdef NEW_LOGGING
166                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
167                            "test_filter:        NOT\n" ));
168 #else
169                 Debug( LDAP_DEBUG_FILTER, "    NOT\n", 0, 0, 0 );
170 #endif
171
172                 rc = test_filter( be, conn, op, e, f->f_not );
173
174                 /* Flip true to false and false to true
175                  * but leave Undefined alone.
176                  */
177                 switch( rc ) {
178                 case LDAP_COMPARE_TRUE:
179                         rc = LDAP_COMPARE_FALSE;
180                         break;
181                 case LDAP_COMPARE_FALSE:
182                         rc = LDAP_COMPARE_TRUE;
183                         break;
184                 }
185                 break;
186
187 #ifdef SLAPD_EXT_FILTERS
188         case LDAP_FILTER_EXT:
189 #ifdef NEW_LOGGING
190                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
191                            "test_filter:        EXT\n" ));
192 #else
193                 Debug( LDAP_DEBUG_FILTER, "    EXT\n", 0, 0, 0 );
194 #endif
195
196                 rc = test_mra_filter( be, conn, op, e, f->f_mra );
197                 break;
198 #endif
199
200         default:
201 #ifdef NEW_LOGGING
202                 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
203                            "test_filter:  unknown filter type %lu\n", 
204                        f->f_choice ));
205 #else
206                 Debug( LDAP_DEBUG_ANY, "    unknown filter type %lu\n",
207                     f->f_choice, 0, 0 );
208 #endif
209
210                 rc = LDAP_PROTOCOL_ERROR;
211         }
212
213 #ifdef NEW_LOGGING
214         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
215                    "test_filter:  return=%d\n", rc ));
216 #else
217         Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
218 #endif
219
220         return( rc );
221 }
222
223
224 static int
225 test_ava_filter(
226     Backend     *be,
227     Connection  *conn,
228     Operation   *op,
229     Entry       *e,
230         AttributeAssertion *ava,
231     int         type
232 )
233 {
234         int             i;
235         Attribute       *a;
236
237         if ( !access_allowed( be, conn, op, e,
238                 ava->aa_desc, ava->aa_value, ACL_SEARCH ) )
239         {
240                 return LDAP_INSUFFICIENT_ACCESS;
241         }
242
243         for(a = attrs_find( e->e_attrs, ava->aa_desc );
244                 a != NULL;
245                 a = attrs_find( a->a_next, ava->aa_desc ) )
246         {
247                 MatchingRule *mr;
248
249                 switch ( type ) {
250                 case LDAP_FILTER_APPROX:
251                         mr = a->a_desc->ad_type->sat_approx;
252                         if( mr != NULL ) break;
253
254                         /* use EQUALITY matching rule if no APPROX rule */
255
256                 case LDAP_FILTER_EQUALITY:
257                         mr = a->a_desc->ad_type->sat_equality;
258                         break;
259
260                 case LDAP_FILTER_GE:
261                 case LDAP_FILTER_LE:
262                         mr = a->a_desc->ad_type->sat_ordering;
263                         break;
264
265                 default:
266                         mr = NULL;
267                 }
268
269                 if( mr == NULL ) {
270                         continue;
271                 }
272
273                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
274                         int ret;
275                         int rc;
276                         const char *text;
277
278                         rc = value_match( &ret, a->a_desc, mr, 0,
279                                 a->a_vals[i], ava->aa_value,
280                                 &text );
281
282                         if( rc != LDAP_SUCCESS ) {
283                                 return rc;
284                         }
285
286                         switch ( type ) {
287                         case LDAP_FILTER_EQUALITY:
288                         case LDAP_FILTER_APPROX:
289                                 if ( ret == 0 ) {
290                                         return LDAP_COMPARE_TRUE;
291                                 }
292                                 break;
293
294                         case LDAP_FILTER_GE:
295                                 if ( ret >= 0 ) {
296                                         return LDAP_COMPARE_TRUE;
297                                 }
298                                 break;
299
300                         case LDAP_FILTER_LE:
301                                 if ( ret <= 0 ) {
302                                         return LDAP_COMPARE_TRUE;
303                                 }
304                                 break;
305                         }
306                 }
307         }
308
309         return( LDAP_COMPARE_FALSE );
310 }
311
312
313 static int
314 test_presence_filter(
315     Backend     *be,
316     Connection  *conn,
317     Operation   *op,
318     Entry       *e,
319         AttributeDescription *desc
320 )
321 {
322         if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_SEARCH ) )
323         {
324                 return LDAP_INSUFFICIENT_ACCESS;
325         }
326
327         return attrs_find( e->e_attrs, desc ) != NULL
328                 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
329 }
330
331
332 static int
333 test_filter_and(
334     Backend     *be,
335     Connection  *conn,
336     Operation   *op,
337     Entry       *e,
338     Filter      *flist
339 )
340 {
341         Filter  *f;
342         int rtn = LDAP_COMPARE_TRUE; /* True if empty */
343
344 #ifdef NEW_LOGGING
345         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
346                    "test_filter_and: begin\n" ));
347 #else
348         Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
349 #endif
350
351
352         for ( f = flist; f != NULL; f = f->f_next ) {
353                 int rc = test_filter( be, conn, op, e, f );
354
355                 if ( rc == LDAP_COMPARE_FALSE ) {
356                         /* filter is False */
357                         rtn = rc;
358                         break;
359                 }
360
361                 if ( rc != LDAP_COMPARE_TRUE ) {
362                         /* filter is Undefined unless later elements are False */
363                         rtn = rc;
364                 }
365         }
366
367 #ifdef NEW_LOGGING
368         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
369                    "test_filter_and:  rc=%d\n", rtn ));
370 #else
371         Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
372 #endif
373
374         return rtn;
375 }
376
377 static int
378 test_filter_or(
379     Backend     *be,
380     Connection  *conn,
381     Operation   *op,
382     Entry       *e,
383     Filter      *flist
384 )
385 {
386         Filter  *f;
387         int rtn = LDAP_COMPARE_FALSE; /* False if empty */
388
389 #ifdef NEW_LOGGING
390         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
391                    "test_filter_or: begin\n" ));
392 #else
393         Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
394 #endif
395
396
397         for ( f = flist; f != NULL; f = f->f_next ) {
398                 int rc = test_filter( be, conn, op, e, f );
399
400                 if ( rc == LDAP_COMPARE_TRUE ) {
401                         /* filter is True */
402                         rtn = rc;
403                         break;
404                 }
405
406                 if ( rc != LDAP_COMPARE_FALSE ) {
407                         /* filter is Undefined unless later elements are True */
408                         rtn = rc;
409                 }
410         }
411
412 #ifdef NEW_LOGGING
413         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
414                    "test_filter_or: result=%d\n", rtn ));
415 #else
416         Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
417 #endif
418
419         return rtn;
420 }
421
422
423 static int
424 test_substrings_filter(
425     Backend     *be,
426     Connection  *conn,
427     Operation   *op,
428     Entry       *e,
429     Filter      *f
430 )
431 {
432         Attribute       *a;
433
434 #ifdef NEW_LOGGING
435         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
436                    "test_substrings_filter: begin\n" ));
437 #else
438         Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
439 #endif
440
441
442         if ( !access_allowed( be, conn, op, e,
443                 f->f_sub_desc, NULL, ACL_SEARCH ) )
444         {
445                 return LDAP_INSUFFICIENT_ACCESS;
446         }
447
448         for(a = attrs_find( e->e_attrs, f->f_sub_desc );
449                 a != NULL;
450                 a = attrs_find( a->a_next, f->f_sub_desc ) )
451         {
452                 int i;
453                 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
454
455                 if( mr == NULL ) {
456                         continue;
457                 }
458
459                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
460                         int ret;
461                         int rc;
462                         const char *text;
463
464                         rc = value_match( &ret, a->a_desc, mr, 0,
465                                 a->a_vals[i], f->f_sub,
466                                 &text );
467
468                         if( rc != LDAP_SUCCESS ) {
469                                 return rc;
470                         }
471
472                         if ( ret == 0 ) {
473                                 return LDAP_COMPARE_TRUE;
474                         }
475                 }
476         }
477
478 #ifdef NEW_LOGGING
479         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
480                    "test_substrings_filter: return FALSE\n" ));
481 #else
482         Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n", 0, 0, 0 );
483 #endif
484
485         return LDAP_COMPARE_FALSE;
486 }