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