]> git.sur5r.net Git - openldap/blob - servers/slapd/filterentry.c
ITS#3226: Clear attribute flags after schema_check failed
[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;
207         BER_MEMFREE_FN  *memfree;
208
209         if ( op == NULL ) {
210                 memctx = NULL;
211                 memfree = slap_sl_free;
212         } else {
213                 memctx = op->o_tmpmemctx;
214                 memfree = op->o_tmpfree;
215         }
216
217         if ( mra->ma_desc ) {
218                 /*
219                  * if ma_desc is available, then we're filtering for
220                  * one attribute, and SEARCH permissions can be checked
221                  * directly.
222                  */
223                 if( !access_allowed( op, e,
224                         mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
225                 {
226                         return LDAP_INSUFFICIENT_ACCESS;
227                 }
228
229                 for(a = attrs_find( e->e_attrs, mra->ma_desc );
230                         a != NULL;
231                         a = attrs_find( a->a_next, mra->ma_desc ) )
232                 {
233                         struct berval *bv;
234                         /* If ma_rule is not the same as the attribute's
235                          * normal rule, then we can't use the a_nvals.
236                          */
237                         if (mra->ma_rule == a->a_desc->ad_type->sat_equality) {
238                                 bv = a->a_nvals;
239                         } else {
240                                 bv = a->a_vals;
241                         }
242
243                         for ( ; 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, 0,
249                                         bv, &mra->ma_value, &text );
250         
251                                 if( rc != LDAP_SUCCESS ) return rc;
252                                 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
253                         }
254                 }
255
256         } else {
257                 /*
258                  * No attribute description: test all
259                  */
260                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
261                         struct berval   *bv, value;
262                         const char      *text = NULL;
263                         int             rc;
264
265                         /* check if matching is appropriate */
266                         if ( !mr_usable_with_at( mra->ma_rule, a->a_desc->ad_type )) {
267                                 continue;
268                         }
269
270                         /* normalize for equality */
271                         rc = asserted_value_validate_normalize( a->a_desc, mra->ma_rule,
272                                 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
273                                 &mra->ma_value, &value, &text, memctx );
274                         if ( rc != LDAP_SUCCESS ) continue;
275
276                         /* check search access */
277                         if ( !access_allowed( op, e,
278                                 a->a_desc, &value, ACL_SEARCH, NULL ) ) {
279                                 memfree( value.bv_val, memctx );
280                                 continue;
281                         }
282
283                         /* check match */
284                         if (mra->ma_rule == a->a_desc->ad_type->sat_equality) {
285                                 bv = a->a_nvals;
286                         } else {
287                                 bv = a->a_vals;
288                         }
289
290                         for ( ; bv->bv_val != NULL; bv++ ) {
291                                 int ret;
292         
293                                 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
294                                         bv, &value, &text );
295         
296                                 if( rc != LDAP_SUCCESS ) break;
297         
298                                 if ( ret == 0 ) {
299                                         rc = LDAP_COMPARE_TRUE;
300                                         break;
301                                 }
302                         }
303                         memfree( value.bv_val, memctx );
304                         if ( rc != LDAP_SUCCESS ) return rc;
305                 }
306         }
307
308         /* check attrs in DN AVAs if required */
309         if ( mra->ma_dnattrs ) {
310                 LDAPDN          dn = NULL;
311                 int             iRDN, iAVA;
312                 int             rc;
313
314                 /* parse and pretty the dn */
315                 rc = dnPrettyDN( NULL, &e->e_name, &dn, memctx );
316                 if ( rc != LDAP_SUCCESS ) {
317                         return LDAP_INVALID_SYNTAX;
318                 }
319
320                 /* for each AVA of each RDN ... */
321                 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
322                         LDAPRDN         rdn = dn[ iRDN ];
323
324                         for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
325                                 LDAPAVA         *ava = rdn[ iAVA ];
326                                 struct berval   *bv = &ava->la_value, value;
327                                 AttributeDescription *ad =
328                                         (AttributeDescription *)ava->la_private;
329                                 int ret;
330                                 const char *text;
331
332                                 assert( ad );
333
334                                 if ( mra->ma_desc ) {
335                                         /* have a mra type? check for subtype */
336                                         if ( !is_ad_subtype( ad, mra->ma_desc ) ) {
337                                                 continue;
338                                         }
339                                         value = mra->ma_value;
340
341                                 } else {
342                                         const char      *text = NULL;
343
344                                         /* check if matching is appropriate */
345                                         if ( !mr_usable_with_at( mra->ma_rule, ad->ad_type )) {
346                                                 continue;
347                                         }
348
349                                         /* normalize for equality */
350                                         rc = asserted_value_validate_normalize( ad,
351                                                 mra->ma_rule,
352                                                 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
353                                                 &mra->ma_value, &value, &text, memctx );
354                                         if ( rc != LDAP_SUCCESS ) continue;
355
356                                         /* check search access */
357                                         if ( !access_allowed( op, e,
358                                                 ad, &value, ACL_SEARCH, NULL ) )
359                                         {
360                                                 memfree( value.bv_val, memctx );
361                                                 continue;
362                                         }
363                                 }
364
365                                 /* check match */
366                                 rc = value_match( &ret, ad, mra->ma_rule, 0,
367                                         bv, &value, &text );
368
369                                 if ( value.bv_val != mra->ma_value.bv_val ) {
370                                         memfree( value.bv_val, memctx );
371                                 }
372
373                                 if ( rc == LDAP_SUCCESS && ret == 0 ) rc = LDAP_COMPARE_TRUE;
374
375                                 if( rc != LDAP_SUCCESS ) {
376                                         ldap_dnfree_x( dn, memctx );
377                                         return rc;
378                                 }
379                         }
380                 }
381                 ldap_dnfree_x( dn, memctx );
382         }
383
384         return LDAP_COMPARE_FALSE;
385 }
386
387 static int
388 test_ava_filter(
389         Operation       *op,
390         Entry           *e,
391         AttributeAssertion *ava,
392         int             type )
393 {
394         Attribute       *a;
395
396         if ( !access_allowed( op, e,
397                 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
398         {
399                 return LDAP_INSUFFICIENT_ACCESS;
400         }
401
402         for(a = attrs_find( e->e_attrs, ava->aa_desc );
403                 a != NULL;
404                 a = attrs_find( a->a_next, ava->aa_desc ) )
405         {
406                 MatchingRule *mr;
407                 struct berval *bv;
408
409                 switch ( type ) {
410                 case LDAP_FILTER_APPROX:
411                         mr = a->a_desc->ad_type->sat_approx;
412                         if( mr != NULL ) break;
413
414                         /* use EQUALITY matching rule if no APPROX rule */
415
416                 case LDAP_FILTER_EQUALITY:
417                         mr = a->a_desc->ad_type->sat_equality;
418                         break;
419
420                 case LDAP_FILTER_GE:
421                 case LDAP_FILTER_LE:
422                         mr = a->a_desc->ad_type->sat_ordering;
423                         break;
424
425                 default:
426                         mr = NULL;
427                 }
428
429                 if( mr == NULL ) {
430                         continue;
431                 }
432
433                 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
434                         int ret;
435                         int rc;
436                         const char *text;
437
438                         rc = value_match( &ret, a->a_desc, mr, 0,
439                                 bv, &ava->aa_value, &text );
440
441                         if( rc != LDAP_SUCCESS ) return rc;
442
443                         switch ( type ) {
444                         case LDAP_FILTER_EQUALITY:
445                         case LDAP_FILTER_APPROX:
446                                 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
447                                 break;
448
449                         case LDAP_FILTER_GE:
450                                 if ( ret >= 0 ) return LDAP_COMPARE_TRUE;
451                                 break;
452
453                         case LDAP_FILTER_LE:
454                                 if ( ret <= 0 ) return LDAP_COMPARE_TRUE;
455                                 break;
456                         }
457                 }
458         }
459
460         if ( ava->aa_desc == slap_schema.si_ad_hasSubordinates 
461                 && op && op->o_bd && op->o_bd->be_has_subordinates )
462         {
463                 int             hasSubordinates;
464                 struct berval   hs;
465
466                 /*
467                  * No other match should be allowed ...
468                  */
469                 assert( type == LDAP_FILTER_EQUALITY );
470                 
471                 if ( op->o_bd->be_has_subordinates( op, e, &hasSubordinates ) !=
472                         LDAP_SUCCESS )
473                 {
474                         return LDAP_OTHER;
475                 }
476
477                 if ( hasSubordinates == LDAP_COMPARE_TRUE ) {
478                         hs = slap_true_bv;
479
480                 } else if ( hasSubordinates == LDAP_COMPARE_FALSE ) {
481                         hs = slap_false_bv;
482
483                 } else {
484                         return LDAP_OTHER;
485                 }
486
487                 if ( bvmatch( &ava->aa_value, &hs ) ) return LDAP_COMPARE_TRUE;
488                 return LDAP_COMPARE_FALSE;
489         }
490
491         return( LDAP_COMPARE_FALSE );
492 }
493
494
495 static int
496 test_presence_filter(
497         Operation       *op,
498         Entry           *e,
499         AttributeDescription *desc )
500 {
501         Attribute       *a;
502
503         if ( !access_allowed( op, e, desc, NULL, ACL_SEARCH, NULL ) ) {
504                 return LDAP_INSUFFICIENT_ACCESS;
505         }
506
507         a = attrs_find( e->e_attrs, desc );
508         if ( a == NULL && desc == slap_schema.si_ad_hasSubordinates ) {
509
510                 /*
511                  * XXX: fairly optimistic: if the function is defined,
512                  * then PRESENCE must succeed, because hasSubordinate
513                  * is boolean-valued; I think we may live with this 
514                  * simplification by now
515                  */
516                 if ( op && op->o_bd && op->o_bd->be_has_subordinates ) {
517                         return LDAP_COMPARE_TRUE;
518                 }
519
520                 return LDAP_COMPARE_FALSE;
521         }
522
523         return a != NULL ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
524 }
525
526
527 static int
528 test_filter_and(
529         Operation       *op,
530         Entry   *e,
531         Filter  *flist )
532 {
533         Filter  *f;
534         int rtn = LDAP_COMPARE_TRUE; /* True if empty */
535
536 #ifdef NEW_LOGGING
537         LDAP_LOG( FILTER, ENTRY, "test_filter_and: begin\n", 0, 0, 0 );
538 #else
539         Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
540 #endif
541
542
543         for ( f = flist; f != NULL; f = f->f_next ) {
544                 int rc = test_filter( op, e, f );
545
546                 if ( rc == LDAP_COMPARE_FALSE ) {
547                         /* filter is False */
548                         rtn = rc;
549                         break;
550                 }
551
552                 if ( rc != LDAP_COMPARE_TRUE ) {
553                         /* filter is Undefined unless later elements are False */
554                         rtn = rc;
555                 }
556         }
557
558 #ifdef NEW_LOGGING
559         LDAP_LOG( FILTER, RESULTS, "test_filter_and:  rc=%d\n", rtn, 0, 0 );
560 #else
561         Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
562 #endif
563
564         return rtn;
565 }
566
567 static int
568 test_filter_or(
569         Operation       *op,
570         Entry   *e,
571         Filter  *flist
572 )
573 {
574         Filter  *f;
575         int rtn = LDAP_COMPARE_FALSE; /* False if empty */
576
577 #ifdef NEW_LOGGING
578         LDAP_LOG( FILTER, ENTRY, "test_filter_or: begin\n", 0, 0, 0 );
579 #else
580         Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
581 #endif
582
583         for ( f = flist; f != NULL; f = f->f_next ) {
584                 int rc = test_filter( op, e, f );
585
586                 if ( rc == LDAP_COMPARE_TRUE ) {
587                         /* filter is True */
588                         rtn = rc;
589                         break;
590                 }
591
592                 if ( rc != LDAP_COMPARE_FALSE ) {
593                         /* filter is Undefined unless later elements are True */
594                         rtn = rc;
595                 }
596         }
597
598 #ifdef NEW_LOGGING
599         LDAP_LOG( FILTER, ENTRY, "test_filter_or: result=%d\n", rtn, 0, 0 );
600 #else
601         Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
602 #endif
603         return rtn;
604 }
605
606
607 static int
608 test_substrings_filter(
609         Operation       *op,
610         Entry   *e,
611         Filter  *f )
612 {
613         Attribute       *a;
614
615 #ifdef NEW_LOGGING
616         LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: begin\n", 0, 0, 0 );
617 #else
618         Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
619 #endif
620
621         if ( !access_allowed( op, e,
622                 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
623         {
624                 return LDAP_INSUFFICIENT_ACCESS;
625         }
626
627         for(a = attrs_find( e->e_attrs, f->f_sub_desc );
628                 a != NULL;
629                 a = attrs_find( a->a_next, f->f_sub_desc ) )
630         {
631                 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
632                 struct berval *bv;
633
634                 if( mr == NULL ) continue;
635
636                 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
637                         int ret;
638                         int rc;
639                         const char *text;
640
641                         rc = value_match( &ret, a->a_desc, mr, 0,
642                                 bv, f->f_sub, &text );
643
644                         if( rc != LDAP_SUCCESS ) return rc;
645                         if ( ret == 0 ) return LDAP_COMPARE_TRUE;
646                 }
647         }
648
649 #ifdef NEW_LOGGING
650         LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: return FALSE\n",
651                 0, 0, 0 );
652 #else
653         Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n",
654                 0, 0, 0 );
655 #endif
656         return LDAP_COMPARE_FALSE;
657 }