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