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