]> git.sur5r.net Git - openldap/blob - servers/slapd/filterentry.c
01a89c4f45e8945868c3e9fd4f45b2dbcbe59edf
[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, ENTRY, "test_filter: begin\n", 0, 0, 0 );
60 #else
61         Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
62 #endif
63
64
65         switch ( f->f_choice ) {
66         case SLAPD_FILTER_COMPUTED:
67 #ifdef NEW_LOGGING
68                 LDAP_LOG( FILTER, DETAIL1,
69                         "test_filter:   COMPUTED %s (%d)\n",
70                         f->f_result == LDAP_COMPARE_FALSE ? "false" :
71                         f->f_result == LDAP_COMPARE_TRUE         ? "true"  :
72                         f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
73                         "error", f->f_result, 0 );
74 #else
75                 Debug( LDAP_DEBUG_FILTER, "    COMPUTED %s (%d)\n",
76                         f->f_result == LDAP_COMPARE_FALSE ? "false" :
77                         f->f_result == LDAP_COMPARE_TRUE ? "true" :
78                         f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : "error",
79                         f->f_result, 0 );
80 #endif
81
82                 rc = f->f_result;
83                 break;
84
85         case LDAP_FILTER_EQUALITY:
86 #ifdef NEW_LOGGING
87                 LDAP_LOG( FILTER, DETAIL1, "test_filter:   EQUALITY\n", 0, 0, 0 );
88 #else
89                 Debug( LDAP_DEBUG_FILTER, "    EQUALITY\n", 0, 0, 0 );
90 #endif
91
92                 rc = test_ava_filter( be, conn, op, e, f->f_ava,
93                     LDAP_FILTER_EQUALITY );
94                 break;
95
96         case LDAP_FILTER_SUBSTRINGS:
97 #ifdef NEW_LOGGING
98                 LDAP_LOG( FILTER, DETAIL1, "test_filter  SUBSTRINGS\n", 0, 0, 0 );
99 #else
100                 Debug( LDAP_DEBUG_FILTER, "    SUBSTRINGS\n", 0, 0, 0 );
101 #endif
102
103                 rc = test_substrings_filter( be, conn, op, e, f );
104                 break;
105
106         case LDAP_FILTER_GE:
107                 rc = test_ava_filter( be, conn, op, e, f->f_ava,
108                     LDAP_FILTER_GE );
109                 break;
110
111         case LDAP_FILTER_LE:
112                 rc = test_ava_filter( be, conn, op, e, f->f_ava,
113                     LDAP_FILTER_LE );
114                 break;
115
116         case LDAP_FILTER_PRESENT:
117 #ifdef NEW_LOGGING
118                 LDAP_LOG( FILTER, DETAIL1, "test_filter:        PRESENT\n", 0, 0, 0 );
119 #else
120                 Debug( LDAP_DEBUG_FILTER, "    PRESENT\n", 0, 0, 0 );
121 #endif
122
123                 rc = test_presence_filter( be, conn, op, e, f->f_desc );
124                 break;
125
126         case LDAP_FILTER_APPROX:
127 #ifdef NEW_LOGGING
128                 LDAP_LOG( FILTER, DETAIL1, "test_filter: APPROX\n", 0, 0, 0 );
129 #else
130                 Debug( LDAP_DEBUG_FILTER, "    APPROX\n", 0, 0, 0 );
131 #endif
132                 rc = test_ava_filter( be, conn, op, e, f->f_ava,
133                     LDAP_FILTER_APPROX );
134                 break;
135
136         case LDAP_FILTER_AND:
137 #ifdef NEW_LOGGING
138                 LDAP_LOG( FILTER, DETAIL1, "test_filter:  AND\n", 0, 0, 0 );
139 #else
140                 Debug( LDAP_DEBUG_FILTER, "    AND\n", 0, 0, 0 );
141 #endif
142
143                 rc = test_filter_and( be, conn, op, e, f->f_and );
144                 break;
145
146         case LDAP_FILTER_OR:
147 #ifdef NEW_LOGGING
148                 LDAP_LOG( FILTER, DETAIL1, "test_filter:        OR\n", 0, 0, 0 );
149 #else
150                 Debug( LDAP_DEBUG_FILTER, "    OR\n", 0, 0, 0 );
151 #endif
152
153                 rc = test_filter_or( be, conn, op, e, f->f_or );
154                 break;
155
156         case LDAP_FILTER_NOT:
157 #ifdef NEW_LOGGING
158                 LDAP_LOG( FILTER, DETAIL1, "test_filter:        NOT\n", 0, 0, 0 );
159 #else
160                 Debug( LDAP_DEBUG_FILTER, "    NOT\n", 0, 0, 0 );
161 #endif
162
163                 rc = test_filter( be, conn, op, e, f->f_not );
164
165                 /* Flip true to false and false to true
166                  * but leave Undefined alone.
167                  */
168                 switch( rc ) {
169                 case LDAP_COMPARE_TRUE:
170                         rc = LDAP_COMPARE_FALSE;
171                         break;
172                 case LDAP_COMPARE_FALSE:
173                         rc = LDAP_COMPARE_TRUE;
174                         break;
175                 }
176                 break;
177
178         case LDAP_FILTER_EXT:
179 #ifdef NEW_LOGGING
180                 LDAP_LOG( FILTER, DETAIL1, "test_filter:        EXT\n", 0, 0, 0 );
181 #else
182                 Debug( LDAP_DEBUG_FILTER, "    EXT\n", 0, 0, 0 );
183 #endif
184
185                 rc = test_mra_filter( be, conn, op, e, f->f_mra );
186                 break;
187
188         default:
189 #ifdef NEW_LOGGING
190                 LDAP_LOG( FILTER, INFO, 
191                         "test_filter:  unknown filter type %lu\n", f->f_choice, 0, 0 );
192 #else
193                 Debug( LDAP_DEBUG_ANY, "    unknown filter type %lu\n",
194                     f->f_choice, 0, 0 );
195 #endif
196
197                 rc = LDAP_PROTOCOL_ERROR;
198         }
199
200 #ifdef NEW_LOGGING
201         LDAP_LOG( FILTER, RESULTS, "test_filter:  return=%d\n", rc, 0, 0 );
202 #else
203         Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
204 #endif
205
206         return( rc );
207 }
208
209 static int test_mra_filter(
210         Backend *be,
211         Connection *conn,
212         Operation *op,
213         Entry *e,
214         MatchingRuleAssertion *mra )
215 {
216         Attribute       *a;
217
218 #ifndef SLAP_X_MRA_MATCH_DNATTRS
219         if( !access_allowed( be, conn, op, e,
220                 mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
221         {
222                 return LDAP_INSUFFICIENT_ACCESS;
223         }
224 #else /* SLAP_X_MRA_MATCH_DNATTRS */
225         if ( mra->ma_desc ) {
226                 /*
227                  * if ma_desc is available, then we're filtering for
228                  * one attribute, and SEARCH permissions can be checked
229                  * directly.
230                  */
231                 if( !access_allowed( be, conn, op, e,
232                         mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
233                 {
234                         return LDAP_INSUFFICIENT_ACCESS;
235                 }
236 #endif /* SLAP_X_MRA_MATCH_DNATTRS */
237
238                 for(a = attrs_find( e->e_attrs, mra->ma_desc );
239                         a != NULL;
240                         a = attrs_find( a->a_next, mra->ma_desc ) )
241                 {
242                         struct berval *bv;
243                         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
244                                 int ret;
245                                 int rc;
246                                 const char *text;
247         
248                                 rc = value_match( &ret, a->a_desc, mra->ma_rule,
249                                         SLAP_MR_ASSERTION_SYNTAX_MATCH,
250                                         bv, &mra->ma_value, &text );
251         
252                                 if( rc != LDAP_SUCCESS ) {
253                                         return rc;
254                                 }
255         
256                                 if ( ret == 0 ) {
257                                         return LDAP_COMPARE_TRUE;
258                                 }
259                         }
260                 }
261 #ifdef SLAP_X_MRA_MATCH_DNATTRS
262         } else {
263
264                 /*
265                  * No attribute description: test all
266                  */
267                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
268                         struct berval   *bv, value;
269                         const char      *text = NULL;
270                         int             rc;
271
272                         /* check if matching is appropriate */
273                         if ( !mr_usable_with_at( mra->ma_rule, a->a_desc->ad_type )) {
274                                 continue;
275                         }
276
277                         /* normalize for equality */
278                         rc = value_validate_normalize( a->a_desc, 
279                                 SLAP_MR_EQUALITY,
280                                 &mra->ma_value, &value, &text );
281                         if ( rc != LDAP_SUCCESS ) {
282                                 continue;
283                         }
284
285                         /* check search access */
286                         if ( !access_allowed( be, conn, op, e,
287                                 a->a_desc, &value, ACL_SEARCH, NULL ) ) {
288                                 continue;
289                         }
290
291                         /* check match */
292                         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
293                                 int ret;
294                                 int rc;
295         
296                                 rc = value_match( &ret, a->a_desc, mra->ma_rule,
297                                         SLAP_MR_ASSERTION_SYNTAX_MATCH,
298                                         bv, &value, &text );
299         
300                                 if( rc != LDAP_SUCCESS ) {
301                                         return rc;
302                                 }
303         
304                                 if ( ret == 0 ) {
305                                         return LDAP_COMPARE_TRUE;
306                                 }
307                         }
308                 }
309         }
310
311         /* check attrs in DN AVAs if required */
312         if ( mra->ma_dnattrs ) {
313                 LDAPDN          *dn = NULL;
314                 int             iRDN, iAVA;
315                 int             rc;
316
317                 /* parse and pretty the dn */
318                 rc = dnPrettyDN( NULL, &e->e_name, &dn );
319                 if ( rc != LDAP_SUCCESS ) {
320                         return LDAP_INVALID_SYNTAX;
321                 }
322
323                 /* for each AVA of each RDN ... */
324                 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
325                         LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
326
327                         for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
328                                 LDAPAVA         *ava = rdn[ 0 ][ iAVA ];
329                                 struct berval   *bv = &ava->la_value, value;
330                                 AttributeDescription *ad = (AttributeDescription *)ava->la_private;
331                                 int ret;
332                                 int rc;
333                                 const char *text;
334
335                                 assert( ad );
336
337                                 if ( mra->ma_desc ) {
338                                         /* have a mra type? check for subtype */
339                                         if ( !is_ad_subtype( ad, mra->ma_desc ) ) {
340                                                 continue;
341                                         }
342                                         value = mra->ma_value;
343
344                                 } else {
345                                         const char      *text = NULL;
346
347                                         /* check if matching is appropriate */
348                                         if ( !mr_usable_with_at( mra->ma_rule, ad->ad_type )) {
349                                                 continue;
350                                         }
351
352                                         /* normalize for equality */
353                                         rc = value_validate_normalize( ad, SLAP_MR_EQUALITY,
354                                                 &mra->ma_value, &value, &text );
355                                         if ( rc != LDAP_SUCCESS ) {
356                                                 continue;
357                                         }
358
359                                         /* check search access */
360                                         if ( !access_allowed( be, conn, op, e,
361                                                 ad, &value, ACL_SEARCH, NULL ) ) {
362                                                 continue;
363                                         }
364                                 }
365
366                                 /* check match */
367                                 rc = value_match( &ret, ad, mra->ma_rule,
368                                         SLAP_MR_ASSERTION_SYNTAX_MATCH,
369                                         bv, &value, &text );
370
371                                 if( rc != LDAP_SUCCESS ) {
372                                         ldap_dnfree( dn );
373                                         return rc;
374                                 }
375
376                                 if ( ret == 0 ) {
377                                         ldap_dnfree( dn );
378                                         return LDAP_COMPARE_TRUE;
379                                 }
380                         }
381                 }
382         }
383 #endif /* SLAP_X_MRA_MATCH_DNATTRS */
384
385         return LDAP_COMPARE_FALSE;
386 }
387
388 static int
389 test_ava_filter(
390     Backend     *be,
391     Connection  *conn,
392     Operation   *op,
393     Entry       *e,
394         AttributeAssertion *ava,
395     int         type
396 )
397 {
398         Attribute       *a;
399
400         if ( !access_allowed( be, conn, op, e,
401                 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
402         {
403                 return LDAP_INSUFFICIENT_ACCESS;
404         }
405
406         for(a = attrs_find( e->e_attrs, ava->aa_desc );
407                 a != NULL;
408                 a = attrs_find( a->a_next, ava->aa_desc ) )
409         {
410                 MatchingRule *mr;
411                 struct berval *bv;
412
413                 switch ( type ) {
414                 case LDAP_FILTER_APPROX:
415                         mr = a->a_desc->ad_type->sat_approx;
416                         if( mr != NULL ) break;
417
418                         /* use EQUALITY matching rule if no APPROX rule */
419
420                 case LDAP_FILTER_EQUALITY:
421                         mr = a->a_desc->ad_type->sat_equality;
422                         break;
423
424                 case LDAP_FILTER_GE:
425                 case LDAP_FILTER_LE:
426                         mr = a->a_desc->ad_type->sat_ordering;
427                         break;
428
429                 default:
430                         mr = NULL;
431                 }
432
433                 if( mr == NULL ) {
434                         continue;
435                 }
436
437                 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
438                         int ret;
439                         int rc;
440                         const char *text;
441
442                         rc = value_match( &ret, a->a_desc, mr,
443                                 SLAP_MR_ASSERTION_SYNTAX_MATCH,
444                                 bv, &ava->aa_value, &text );
445
446                         if( rc != LDAP_SUCCESS ) {
447                                 return rc;
448                         }
449
450                         switch ( type ) {
451                         case LDAP_FILTER_EQUALITY:
452                         case LDAP_FILTER_APPROX:
453                                 if ( ret == 0 ) {
454                                         return LDAP_COMPARE_TRUE;
455                                 }
456                                 break;
457
458                         case LDAP_FILTER_GE:
459                                 if ( ret >= 0 ) {
460                                         return LDAP_COMPARE_TRUE;
461                                 }
462                                 break;
463
464                         case LDAP_FILTER_LE:
465                                 if ( ret <= 0 ) {
466                                         return LDAP_COMPARE_TRUE;
467                                 }
468                                 break;
469                         }
470                 }
471         }
472
473         return( LDAP_COMPARE_FALSE );
474 }
475
476
477 static int
478 test_presence_filter(
479     Backend     *be,
480     Connection  *conn,
481     Operation   *op,
482     Entry       *e,
483         AttributeDescription *desc
484 )
485 {
486         if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_SEARCH, NULL ) )
487         {
488                 return LDAP_INSUFFICIENT_ACCESS;
489         }
490
491         return attrs_find( e->e_attrs, desc ) != NULL
492                 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
493 }
494
495
496 static int
497 test_filter_and(
498     Backend     *be,
499     Connection  *conn,
500     Operation   *op,
501     Entry       *e,
502     Filter      *flist
503 )
504 {
505         Filter  *f;
506         int rtn = LDAP_COMPARE_TRUE; /* True if empty */
507
508 #ifdef NEW_LOGGING
509         LDAP_LOG( FILTER, ENTRY, "test_filter_and: begin\n", 0, 0, 0 );
510 #else
511         Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
512 #endif
513
514
515         for ( f = flist; f != NULL; f = f->f_next ) {
516                 int rc = test_filter( be, conn, op, e, f );
517
518                 if ( rc == LDAP_COMPARE_FALSE ) {
519                         /* filter is False */
520                         rtn = rc;
521                         break;
522                 }
523
524                 if ( rc != LDAP_COMPARE_TRUE ) {
525                         /* filter is Undefined unless later elements are False */
526                         rtn = rc;
527                 }
528         }
529
530 #ifdef NEW_LOGGING
531         LDAP_LOG( FILTER, RESULTS, "test_filter_and:  rc=%d\n", rtn, 0, 0 );
532 #else
533         Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
534 #endif
535
536         return rtn;
537 }
538
539 static int
540 test_filter_or(
541     Backend     *be,
542     Connection  *conn,
543     Operation   *op,
544     Entry       *e,
545     Filter      *flist
546 )
547 {
548         Filter  *f;
549         int rtn = LDAP_COMPARE_FALSE; /* False if empty */
550
551 #ifdef NEW_LOGGING
552         LDAP_LOG( FILTER, ENTRY, "test_filter_or: begin\n", 0, 0, 0 );
553 #else
554         Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
555 #endif
556
557
558         for ( f = flist; f != NULL; f = f->f_next ) {
559                 int rc = test_filter( be, conn, op, e, f );
560
561                 if ( rc == LDAP_COMPARE_TRUE ) {
562                         /* filter is True */
563                         rtn = rc;
564                         break;
565                 }
566
567                 if ( rc != LDAP_COMPARE_FALSE ) {
568                         /* filter is Undefined unless later elements are True */
569                         rtn = rc;
570                 }
571         }
572
573 #ifdef NEW_LOGGING
574         LDAP_LOG( FILTER, ENTRY, "test_filter_or: result=%d\n", rtn, 0, 0 );
575 #else
576         Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
577 #endif
578
579         return rtn;
580 }
581
582
583 static int
584 test_substrings_filter(
585     Backend     *be,
586     Connection  *conn,
587     Operation   *op,
588     Entry       *e,
589     Filter      *f
590 )
591 {
592         Attribute       *a;
593
594 #ifdef NEW_LOGGING
595         LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: begin\n", 0, 0, 0 );
596 #else
597         Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
598 #endif
599
600
601         if ( !access_allowed( be, conn, op, e,
602                 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
603         {
604                 return LDAP_INSUFFICIENT_ACCESS;
605         }
606
607         for(a = attrs_find( e->e_attrs, f->f_sub_desc );
608                 a != NULL;
609                 a = attrs_find( a->a_next, f->f_sub_desc ) )
610         {
611                 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
612                 struct berval *bv;
613
614                 if( mr == NULL ) {
615                         continue;
616                 }
617
618                 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
619                         int ret;
620                         int rc;
621                         const char *text;
622
623                         rc = value_match( &ret, a->a_desc, mr,
624                                 SLAP_MR_ASSERTION_SYNTAX_MATCH,
625                                 bv, f->f_sub, &text );
626
627                         if( rc != LDAP_SUCCESS ) {
628                                 return rc;
629                         }
630
631                         if ( ret == 0 ) {
632                                 return LDAP_COMPARE_TRUE;
633                         }
634                 }
635         }
636
637 #ifdef NEW_LOGGING
638         LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: return FALSE\n", 0, 0, 0 );
639 #else
640         Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n", 0, 0, 0 );
641 #endif
642
643         return LDAP_COMPARE_FALSE;
644 }