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