]> git.sur5r.net Git - openldap/blob - servers/slapd/filterentry.c
Suck in HEAD changes since 2.1alpha
[openldap] / servers / slapd / filterentry.c
1 /* filterentry.c - apply a filter to an entry */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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         case LDAP_FILTER_EXT:
188 #ifdef NEW_LOGGING
189                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
190                            "test_filter:        EXT\n" ));
191 #else
192                 Debug( LDAP_DEBUG_FILTER, "    EXT\n", 0, 0, 0 );
193 #endif
194
195                 rc = test_mra_filter( be, conn, op, e, f->f_mra );
196                 break;
197
198         default:
199 #ifdef NEW_LOGGING
200                 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
201                            "test_filter:  unknown filter type %lu\n", 
202                        f->f_choice ));
203 #else
204                 Debug( LDAP_DEBUG_ANY, "    unknown filter type %lu\n",
205                     f->f_choice, 0, 0 );
206 #endif
207
208                 rc = LDAP_PROTOCOL_ERROR;
209         }
210
211 #ifdef NEW_LOGGING
212         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
213                    "test_filter:  return=%d\n", rc ));
214 #else
215         Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
216 #endif
217
218         return( rc );
219 }
220
221 static int test_mra_filter(
222         Backend *be,
223         Connection *conn,
224         Operation *op,
225         Entry *e,
226         MatchingRuleAssertion *mra )
227 {
228         Attribute       *a;
229
230         if( !access_allowed( be, conn, op, e,
231                 mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
232         {
233                 return LDAP_INSUFFICIENT_ACCESS;
234         }
235
236         for(a = attrs_find( e->e_attrs, mra->ma_desc );
237                 a != NULL;
238                 a = attrs_find( a->a_next, mra->ma_desc ) )
239         {
240                 struct berval *bv;
241                 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
242                         int ret;
243                         int rc;
244                         const char *text;
245
246                         rc = value_match( &ret, a->a_desc, mra->ma_rule,
247                                 SLAP_MR_ASSERTION_SYNTAX_MATCH,
248                                 bv, &mra->ma_value,
249                                 &text );
250
251                         if( rc != LDAP_SUCCESS ) {
252                                 return rc;
253                         }
254
255                         if ( ret == 0 ) {
256                                 return LDAP_COMPARE_TRUE;
257                         }
258                 }
259         }
260
261         return LDAP_COMPARE_FALSE;
262 }
263
264 static int
265 test_ava_filter(
266     Backend     *be,
267     Connection  *conn,
268     Operation   *op,
269     Entry       *e,
270         AttributeAssertion *ava,
271     int         type
272 )
273 {
274         Attribute       *a;
275
276         if ( !access_allowed( be, conn, op, e,
277                 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
278         {
279                 return LDAP_INSUFFICIENT_ACCESS;
280         }
281
282         for(a = attrs_find( e->e_attrs, ava->aa_desc );
283                 a != NULL;
284                 a = attrs_find( a->a_next, ava->aa_desc ) )
285         {
286                 MatchingRule *mr;
287                 struct berval *bv;
288
289                 switch ( type ) {
290                 case LDAP_FILTER_APPROX:
291                         mr = a->a_desc->ad_type->sat_approx;
292                         if( mr != NULL ) break;
293
294                         /* use EQUALITY matching rule if no APPROX rule */
295
296                 case LDAP_FILTER_EQUALITY:
297                         mr = a->a_desc->ad_type->sat_equality;
298                         break;
299
300                 case LDAP_FILTER_GE:
301                 case LDAP_FILTER_LE:
302                         mr = a->a_desc->ad_type->sat_ordering;
303                         break;
304
305                 default:
306                         mr = NULL;
307                 }
308
309                 if( mr == NULL ) {
310                         continue;
311                 }
312
313                 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
314                         int ret;
315                         int rc;
316                         const char *text;
317
318                         rc = value_match( &ret, a->a_desc, mr,
319                                 SLAP_MR_ASSERTION_SYNTAX_MATCH,
320                                 bv, &ava->aa_value, &text );
321
322                         if( rc != LDAP_SUCCESS ) {
323                                 return rc;
324                         }
325
326                         switch ( type ) {
327                         case LDAP_FILTER_EQUALITY:
328                         case LDAP_FILTER_APPROX:
329                                 if ( ret == 0 ) {
330                                         return LDAP_COMPARE_TRUE;
331                                 }
332                                 break;
333
334                         case LDAP_FILTER_GE:
335                                 if ( ret >= 0 ) {
336                                         return LDAP_COMPARE_TRUE;
337                                 }
338                                 break;
339
340                         case LDAP_FILTER_LE:
341                                 if ( ret <= 0 ) {
342                                         return LDAP_COMPARE_TRUE;
343                                 }
344                                 break;
345                         }
346                 }
347         }
348
349         return( LDAP_COMPARE_FALSE );
350 }
351
352
353 static int
354 test_presence_filter(
355     Backend     *be,
356     Connection  *conn,
357     Operation   *op,
358     Entry       *e,
359         AttributeDescription *desc
360 )
361 {
362         if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_SEARCH, NULL ) )
363         {
364                 return LDAP_INSUFFICIENT_ACCESS;
365         }
366
367         return attrs_find( e->e_attrs, desc ) != NULL
368                 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
369 }
370
371
372 static int
373 test_filter_and(
374     Backend     *be,
375     Connection  *conn,
376     Operation   *op,
377     Entry       *e,
378     Filter      *flist
379 )
380 {
381         Filter  *f;
382         int rtn = LDAP_COMPARE_TRUE; /* True if empty */
383
384 #ifdef NEW_LOGGING
385         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
386                    "test_filter_and: begin\n" ));
387 #else
388         Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
389 #endif
390
391
392         for ( f = flist; f != NULL; f = f->f_next ) {
393                 int rc = test_filter( be, conn, op, e, f );
394
395                 if ( rc == LDAP_COMPARE_FALSE ) {
396                         /* filter is False */
397                         rtn = rc;
398                         break;
399                 }
400
401                 if ( rc != LDAP_COMPARE_TRUE ) {
402                         /* filter is Undefined unless later elements are False */
403                         rtn = rc;
404                 }
405         }
406
407 #ifdef NEW_LOGGING
408         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
409                    "test_filter_and:  rc=%d\n", rtn ));
410 #else
411         Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
412 #endif
413
414         return rtn;
415 }
416
417 static int
418 test_filter_or(
419     Backend     *be,
420     Connection  *conn,
421     Operation   *op,
422     Entry       *e,
423     Filter      *flist
424 )
425 {
426         Filter  *f;
427         int rtn = LDAP_COMPARE_FALSE; /* False if empty */
428
429 #ifdef NEW_LOGGING
430         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
431                    "test_filter_or: begin\n" ));
432 #else
433         Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
434 #endif
435
436
437         for ( f = flist; f != NULL; f = f->f_next ) {
438                 int rc = test_filter( be, conn, op, e, f );
439
440                 if ( rc == LDAP_COMPARE_TRUE ) {
441                         /* filter is True */
442                         rtn = rc;
443                         break;
444                 }
445
446                 if ( rc != LDAP_COMPARE_FALSE ) {
447                         /* filter is Undefined unless later elements are True */
448                         rtn = rc;
449                 }
450         }
451
452 #ifdef NEW_LOGGING
453         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
454                    "test_filter_or: result=%d\n", rtn ));
455 #else
456         Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
457 #endif
458
459         return rtn;
460 }
461
462
463 static int
464 test_substrings_filter(
465     Backend     *be,
466     Connection  *conn,
467     Operation   *op,
468     Entry       *e,
469     Filter      *f
470 )
471 {
472         Attribute       *a;
473
474 #ifdef NEW_LOGGING
475         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
476                    "test_substrings_filter: begin\n" ));
477 #else
478         Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
479 #endif
480
481
482         if ( !access_allowed( be, conn, op, e,
483                 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
484         {
485                 return LDAP_INSUFFICIENT_ACCESS;
486         }
487
488         for(a = attrs_find( e->e_attrs, f->f_sub_desc );
489                 a != NULL;
490                 a = attrs_find( a->a_next, f->f_sub_desc ) )
491         {
492                 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
493                 struct berval *bv;
494
495                 if( mr == NULL ) {
496                         continue;
497                 }
498
499                 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
500                         int ret;
501                         int rc;
502                         const char *text;
503
504                         rc = value_match( &ret, a->a_desc, mr,
505                                 SLAP_MR_ASSERTION_SYNTAX_MATCH,
506                                 bv, f->f_sub, &text );
507
508                         if( rc != LDAP_SUCCESS ) {
509                                 return rc;
510                         }
511
512                         if ( ret == 0 ) {
513                                 return LDAP_COMPARE_TRUE;
514                         }
515                 }
516         }
517
518 #ifdef NEW_LOGGING
519         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
520                    "test_substrings_filter: return FALSE\n" ));
521 #else
522         Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n", 0, 0, 0 );
523 #endif
524
525         return LDAP_COMPARE_FALSE;
526 }