]> git.sur5r.net Git - openldap/blob - servers/slapd/filterentry.c
ITS#3151 always reschedule consistency_check task
[openldap] / servers / slapd / filterentry.c
1 /* filterentry.c - apply a filter to an entry */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/socket.h>
32 #include <ac/string.h>
33
34
35 #include "slap.h"
36
37 static int      test_filter_and( Operation *op, Entry *e, Filter *flist );
38 static int      test_filter_or( Operation *op, Entry *e, Filter *flist );
39 static int      test_substrings_filter( Operation *op, Entry *e, Filter *f);
40 static int      test_ava_filter( Operation *op, Entry *e, AttributeAssertion *ava, int type );
41 static int      test_mra_filter( Operation *op, Entry *e, MatchingRuleAssertion *mra );
42 static int      test_presence_filter( Operation *op, Entry *e, AttributeDescription *desc );
43
44
45 /*
46  * test_filter - test a filter against a single entry.
47  * returns:
48  *              LDAP_COMPARE_TRUE               filter matched
49  *              LDAP_COMPARE_FALSE              filter did not match
50  *              SLAPD_COMPARE_UNDEFINED filter is undefined
51  *      or an ldap result code indicating error
52  */
53
54 int
55 test_filter(
56     Operation   *op,
57     Entry       *e,
58     Filter      *f )
59 {
60         int     rc;
61 #ifdef NEW_LOGGING
62         LDAP_LOG( FILTER, ENTRY, "test_filter: begin\n", 0, 0, 0 );
63 #else
64         Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
65 #endif
66
67         switch ( f->f_choice ) {
68         case SLAPD_FILTER_COMPUTED:
69 #ifdef NEW_LOGGING
70                 LDAP_LOG( FILTER, DETAIL1,
71                         "test_filter:   COMPUTED %s (%d)\n",
72                         f->f_result == LDAP_COMPARE_FALSE ? "false" :
73                         f->f_result == LDAP_COMPARE_TRUE         ? "true"  :
74                         f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
75                         "error", f->f_result, 0 );
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, DETAIL1, "test_filter:   EQUALITY\n", 0, 0, 0 );
90 #else
91                 Debug( LDAP_DEBUG_FILTER, "    EQUALITY\n", 0, 0, 0 );
92 #endif
93
94                 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_EQUALITY );
95                 break;
96
97         case LDAP_FILTER_SUBSTRINGS:
98 #ifdef NEW_LOGGING
99                 LDAP_LOG( FILTER, DETAIL1, "test_filter  SUBSTRINGS\n", 0, 0, 0 );
100 #else
101                 Debug( LDAP_DEBUG_FILTER, "    SUBSTRINGS\n", 0, 0, 0 );
102 #endif
103
104                 rc = test_substrings_filter( op, e, f );
105                 break;
106
107         case LDAP_FILTER_GE:
108                 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_GE );
109                 break;
110
111         case LDAP_FILTER_LE:
112                 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_LE );
113                 break;
114
115         case LDAP_FILTER_PRESENT:
116 #ifdef NEW_LOGGING
117                 LDAP_LOG( FILTER, DETAIL1, "test_filter:        PRESENT\n", 0, 0, 0 );
118 #else
119                 Debug( LDAP_DEBUG_FILTER, "    PRESENT\n", 0, 0, 0 );
120 #endif
121                 rc = test_presence_filter( op, e, f->f_desc );
122                 break;
123
124         case LDAP_FILTER_APPROX:
125 #ifdef NEW_LOGGING
126                 LDAP_LOG( FILTER, DETAIL1, "test_filter: APPROX\n", 0, 0, 0 );
127 #else
128                 Debug( LDAP_DEBUG_FILTER, "    APPROX\n", 0, 0, 0 );
129 #endif
130                 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_APPROX );
131                 break;
132
133         case LDAP_FILTER_AND:
134 #ifdef NEW_LOGGING
135                 LDAP_LOG( FILTER, DETAIL1, "test_filter:  AND\n", 0, 0, 0 );
136 #else
137                 Debug( LDAP_DEBUG_FILTER, "    AND\n", 0, 0, 0 );
138 #endif
139                 rc = test_filter_and( op, e, f->f_and );
140                 break;
141
142         case LDAP_FILTER_OR:
143 #ifdef NEW_LOGGING
144                 LDAP_LOG( FILTER, DETAIL1, "test_filter:        OR\n", 0, 0, 0 );
145 #else
146                 Debug( LDAP_DEBUG_FILTER, "    OR\n", 0, 0, 0 );
147 #endif
148                 rc = test_filter_or( op, e, f->f_or );
149                 break;
150
151         case LDAP_FILTER_NOT:
152 #ifdef NEW_LOGGING
153                 LDAP_LOG( FILTER, DETAIL1, "test_filter:        NOT\n", 0, 0, 0 );
154 #else
155                 Debug( LDAP_DEBUG_FILTER, "    NOT\n", 0, 0, 0 );
156 #endif
157                 rc = test_filter( op, e, f->f_not );
158
159                 /* Flip true to false and false to true
160                  * but leave Undefined alone.
161                  */
162                 switch( rc ) {
163                 case LDAP_COMPARE_TRUE:
164                         rc = LDAP_COMPARE_FALSE;
165                         break;
166                 case LDAP_COMPARE_FALSE:
167                         rc = LDAP_COMPARE_TRUE;
168                         break;
169                 }
170                 break;
171
172         case LDAP_FILTER_EXT:
173 #ifdef NEW_LOGGING
174                 LDAP_LOG( FILTER, DETAIL1, "test_filter:        EXT\n", 0, 0, 0 );
175 #else
176                 Debug( LDAP_DEBUG_FILTER, "    EXT\n", 0, 0, 0 );
177 #endif
178                 rc = test_mra_filter( op, e, f->f_mra );
179                 break;
180
181         default:
182 #ifdef NEW_LOGGING
183                 LDAP_LOG( FILTER, INFO, 
184                         "test_filter:  unknown filter type %lu\n", f->f_choice, 0, 0 );
185 #else
186                 Debug( LDAP_DEBUG_ANY, "    unknown filter type %lu\n",
187                     f->f_choice, 0, 0 );
188 #endif
189                 rc = LDAP_PROTOCOL_ERROR;
190         }
191
192 #ifdef NEW_LOGGING
193         LDAP_LOG( FILTER, RESULTS, "test_filter:  return=%d\n", rc, 0, 0 );
194 #else
195         Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
196 #endif
197         return( rc );
198 }
199
200 static int test_mra_filter(
201         Operation *op,
202         Entry *e,
203         MatchingRuleAssertion *mra )
204 {
205         Attribute       *a;
206         void *memctx = op ? op->o_tmpmemctx : NULL;
207
208         if ( mra->ma_desc ) {
209                 /*
210                  * if ma_desc is available, then we're filtering for
211                  * one attribute, and SEARCH permissions can be checked
212                  * directly.
213                  */
214                 if( !access_allowed( op, e,
215                         mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
216                 {
217                         return LDAP_INSUFFICIENT_ACCESS;
218                 }
219
220                 for(a = attrs_find( e->e_attrs, mra->ma_desc );
221                         a != NULL;
222                         a = attrs_find( a->a_next, mra->ma_desc ) )
223                 {
224                         struct berval *bv;
225                         /* If ma_rule is not the same as the attribute's
226                          * normal rule, then we can't use the a_nvals.
227                          */
228                         if (mra->ma_rule == a->a_desc->ad_type->sat_equality) {
229                                 bv = a->a_nvals;
230                         } else {
231                                 bv = a->a_vals;
232                         }
233
234                         for ( ; bv->bv_val != NULL; bv++ ) {
235                                 int ret;
236                                 int rc;
237                                 const char *text;
238         
239                                 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
240                                         bv, &mra->ma_value, &text );
241         
242                                 if( rc != LDAP_SUCCESS ) return rc;
243                                 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
244                         }
245                 }
246
247         } else {
248                 /*
249                  * No attribute description: test all
250                  */
251                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
252                         struct berval   *bv, value;
253                         const char      *text = NULL;
254                         int             rc;
255
256                         /* check if matching is appropriate */
257                         if ( !mr_usable_with_at( mra->ma_rule, a->a_desc->ad_type )) {
258                                 continue;
259                         }
260
261                         /* normalize for equality */
262                         rc = asserted_value_validate_normalize( a->a_desc, mra->ma_rule,
263                                 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
264                                 &mra->ma_value, &value, &text, memctx );
265                         if ( rc != LDAP_SUCCESS ) continue;
266
267                         /* check search access */
268                         if ( !access_allowed( op, e,
269                                 a->a_desc, &value, ACL_SEARCH, NULL ) ) {
270                                 op->o_tmpfree( value.bv_val, memctx );
271                                 continue;
272                         }
273
274                         /* check match */
275                         if (mra->ma_rule == a->a_desc->ad_type->sat_equality) {
276                                 bv = a->a_nvals;
277                         } else {
278                                 bv = a->a_vals;
279                         }
280
281                         for ( ; bv->bv_val != NULL; bv++ ) {
282                                 int ret;
283         
284                                 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
285                                         bv, &value, &text );
286         
287                                 if( rc != LDAP_SUCCESS ) break;
288         
289                                 if ( ret == 0 ) {
290                                         rc = LDAP_COMPARE_TRUE;
291                                         break;
292                                 }
293                         }
294                         op->o_tmpfree( value.bv_val, memctx );
295                         if ( rc != LDAP_SUCCESS ) return rc;
296                 }
297         }
298
299         /* check attrs in DN AVAs if required */
300         if ( mra->ma_dnattrs ) {
301                 LDAPDN          dn = NULL;
302                 int             iRDN, iAVA;
303                 int             rc;
304
305                 /* parse and pretty the dn */
306                 rc = dnPrettyDN( NULL, &e->e_name, &dn, memctx );
307                 if ( rc != LDAP_SUCCESS ) {
308                         return LDAP_INVALID_SYNTAX;
309                 }
310
311                 /* for each AVA of each RDN ... */
312                 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
313                         LDAPRDN         rdn = dn[ iRDN ];
314
315                         for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
316                                 LDAPAVA         *ava = rdn[ iAVA ];
317                                 struct berval   *bv = &ava->la_value, value;
318                                 AttributeDescription *ad =
319                                         (AttributeDescription *)ava->la_private;
320                                 int ret;
321                                 const char *text;
322
323                                 assert( ad );
324
325                                 if ( mra->ma_desc ) {
326                                         /* have a mra type? check for subtype */
327                                         if ( !is_ad_subtype( ad, mra->ma_desc ) ) {
328                                                 continue;
329                                         }
330                                         value = mra->ma_value;
331
332                                 } else {
333                                         const char      *text = NULL;
334
335                                         /* check if matching is appropriate */
336                                         if ( !mr_usable_with_at( mra->ma_rule, ad->ad_type )) {
337                                                 continue;
338                                         }
339
340                                         /* normalize for equality */
341                                         rc = asserted_value_validate_normalize( ad,
342                                                 mra->ma_rule,
343                                                 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
344                                                 &mra->ma_value, &value, &text, memctx );
345                                         if ( rc != LDAP_SUCCESS ) continue;
346
347                                         /* check search access */
348                                         if ( !access_allowed( op, e,
349                                                 ad, &value, ACL_SEARCH, NULL ) )
350                                         {
351                                                 op->o_tmpfree( value.bv_val, memctx );
352                                                 continue;
353                                         }
354                                 }
355
356                                 /* check match */
357                                 rc = value_match( &ret, ad, mra->ma_rule, 0,
358                                         bv, &value, &text );
359
360                                 if ( value.bv_val != mra->ma_value.bv_val ) {
361                                         op->o_tmpfree( value.bv_val, memctx );
362                                 }
363
364                                 if ( rc == LDAP_SUCCESS && ret == 0 ) rc = LDAP_COMPARE_TRUE;
365
366                                 if( rc != LDAP_SUCCESS ) {
367                                         ldap_dnfree_x( dn, memctx );
368                                         return rc;
369                                 }
370                         }
371                 }
372                 ldap_dnfree_x( dn, memctx );
373         }
374
375         return LDAP_COMPARE_FALSE;
376 }
377
378 static int
379 test_ava_filter(
380         Operation       *op,
381         Entry           *e,
382         AttributeAssertion *ava,
383         int             type )
384 {
385         Attribute       *a;
386
387         if ( !access_allowed( op, e,
388                 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
389         {
390                 return LDAP_INSUFFICIENT_ACCESS;
391         }
392
393         for(a = attrs_find( e->e_attrs, ava->aa_desc );
394                 a != NULL;
395                 a = attrs_find( a->a_next, ava->aa_desc ) )
396         {
397                 MatchingRule *mr;
398                 struct berval *bv;
399
400                 switch ( type ) {
401                 case LDAP_FILTER_APPROX:
402                         mr = a->a_desc->ad_type->sat_approx;
403                         if( mr != NULL ) break;
404
405                         /* use EQUALITY matching rule if no APPROX rule */
406
407                 case LDAP_FILTER_EQUALITY:
408                         mr = a->a_desc->ad_type->sat_equality;
409                         break;
410
411                 case LDAP_FILTER_GE:
412                 case LDAP_FILTER_LE:
413                         mr = a->a_desc->ad_type->sat_ordering;
414                         break;
415
416                 default:
417                         mr = NULL;
418                 }
419
420                 if( mr == NULL ) {
421                         continue;
422                 }
423
424                 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
425                         int ret;
426                         int rc;
427                         const char *text;
428
429                         rc = value_match( &ret, a->a_desc, mr, 0,
430                                 bv, &ava->aa_value, &text );
431
432                         if( rc != LDAP_SUCCESS ) return rc;
433
434                         switch ( type ) {
435                         case LDAP_FILTER_EQUALITY:
436                         case LDAP_FILTER_APPROX:
437                                 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
438                                 break;
439
440                         case LDAP_FILTER_GE:
441                                 if ( ret >= 0 ) return LDAP_COMPARE_TRUE;
442                                 break;
443
444                         case LDAP_FILTER_LE:
445                                 if ( ret <= 0 ) return LDAP_COMPARE_TRUE;
446                                 break;
447                         }
448                 }
449         }
450
451         if ( ava->aa_desc == slap_schema.si_ad_hasSubordinates 
452                 && op && op->o_bd && op->o_bd->be_has_subordinates )
453         {
454                 int             hasSubordinates;
455                 struct berval   hs;
456
457                 /*
458                  * No other match should be allowed ...
459                  */
460                 assert( type == LDAP_FILTER_EQUALITY );
461                 
462                 if ( op->o_bd->be_has_subordinates( op, e, &hasSubordinates ) !=
463                         LDAP_SUCCESS )
464                 {
465                         return LDAP_OTHER;
466                 }
467
468                 if ( hasSubordinates == LDAP_COMPARE_TRUE ) {
469                         hs = slap_true_bv;
470
471                 } else if ( hasSubordinates == LDAP_COMPARE_FALSE ) {
472                         hs = slap_false_bv;
473
474                 } else {
475                         return LDAP_OTHER;
476                 }
477
478                 if ( bvmatch( &ava->aa_value, &hs ) ) return LDAP_COMPARE_TRUE;
479                 return LDAP_COMPARE_FALSE;
480         }
481
482         return( LDAP_COMPARE_FALSE );
483 }
484
485
486 static int
487 test_presence_filter(
488         Operation       *op,
489         Entry           *e,
490         AttributeDescription *desc )
491 {
492         Attribute       *a;
493
494         if ( !access_allowed( op, e, desc, NULL, ACL_SEARCH, NULL ) ) {
495                 return LDAP_INSUFFICIENT_ACCESS;
496         }
497
498         a = attrs_find( e->e_attrs, desc );
499         if ( a == NULL && desc == slap_schema.si_ad_hasSubordinates ) {
500
501                 /*
502                  * XXX: fairly optimistic: if the function is defined,
503                  * then PRESENCE must succeed, because hasSubordinate
504                  * is boolean-valued; I think we may live with this 
505                  * simplification by now
506                  */
507                 if ( op && op->o_bd && op->o_bd->be_has_subordinates ) {
508                         return LDAP_COMPARE_TRUE;
509                 }
510
511                 return LDAP_COMPARE_FALSE;
512         }
513
514         return a != NULL ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
515 }
516
517
518 static int
519 test_filter_and(
520         Operation       *op,
521         Entry   *e,
522         Filter  *flist )
523 {
524         Filter  *f;
525         int rtn = LDAP_COMPARE_TRUE; /* True if empty */
526
527 #ifdef NEW_LOGGING
528         LDAP_LOG( FILTER, ENTRY, "test_filter_and: begin\n", 0, 0, 0 );
529 #else
530         Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
531 #endif
532
533
534         for ( f = flist; f != NULL; f = f->f_next ) {
535                 int rc = test_filter( op, e, f );
536
537                 if ( rc == LDAP_COMPARE_FALSE ) {
538                         /* filter is False */
539                         rtn = rc;
540                         break;
541                 }
542
543                 if ( rc != LDAP_COMPARE_TRUE ) {
544                         /* filter is Undefined unless later elements are False */
545                         rtn = rc;
546                 }
547         }
548
549 #ifdef NEW_LOGGING
550         LDAP_LOG( FILTER, RESULTS, "test_filter_and:  rc=%d\n", rtn, 0, 0 );
551 #else
552         Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
553 #endif
554
555         return rtn;
556 }
557
558 static int
559 test_filter_or(
560         Operation       *op,
561         Entry   *e,
562         Filter  *flist
563 )
564 {
565         Filter  *f;
566         int rtn = LDAP_COMPARE_FALSE; /* False if empty */
567
568 #ifdef NEW_LOGGING
569         LDAP_LOG( FILTER, ENTRY, "test_filter_or: begin\n", 0, 0, 0 );
570 #else
571         Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
572 #endif
573
574         for ( f = flist; f != NULL; f = f->f_next ) {
575                 int rc = test_filter( op, e, f );
576
577                 if ( rc == LDAP_COMPARE_TRUE ) {
578                         /* filter is True */
579                         rtn = rc;
580                         break;
581                 }
582
583                 if ( rc != LDAP_COMPARE_FALSE ) {
584                         /* filter is Undefined unless later elements are True */
585                         rtn = rc;
586                 }
587         }
588
589 #ifdef NEW_LOGGING
590         LDAP_LOG( FILTER, ENTRY, "test_filter_or: result=%d\n", rtn, 0, 0 );
591 #else
592         Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
593 #endif
594         return rtn;
595 }
596
597
598 static int
599 test_substrings_filter(
600         Operation       *op,
601         Entry   *e,
602         Filter  *f )
603 {
604         Attribute       *a;
605
606 #ifdef NEW_LOGGING
607         LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: begin\n", 0, 0, 0 );
608 #else
609         Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
610 #endif
611
612         if ( !access_allowed( op, e,
613                 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
614         {
615                 return LDAP_INSUFFICIENT_ACCESS;
616         }
617
618         for(a = attrs_find( e->e_attrs, f->f_sub_desc );
619                 a != NULL;
620                 a = attrs_find( a->a_next, f->f_sub_desc ) )
621         {
622                 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
623                 struct berval *bv;
624
625                 if( mr == NULL ) continue;
626
627                 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
628                         int ret;
629                         int rc;
630                         const char *text;
631
632                         rc = value_match( &ret, a->a_desc, mr, 0,
633                                 bv, f->f_sub, &text );
634
635                         if( rc != LDAP_SUCCESS ) return rc;
636                         if ( ret == 0 ) return LDAP_COMPARE_TRUE;
637                 }
638         }
639
640 #ifdef NEW_LOGGING
641         LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: return FALSE\n",
642                 0, 0, 0 );
643 #else
644         Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n",
645                 0, 0, 0 );
646 #endif
647         return LDAP_COMPARE_FALSE;
648 }