]> git.sur5r.net Git - openldap/blob - servers/slapd/filterentry.c
0021f85996b0680128408fb95909700c592645bf
[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
392         for(a = attrs_find( e->e_attrs, ava->aa_desc );
393                 a != NULL;
394                 a = attrs_find( a->a_next, ava->aa_desc ) )
395         {
396                 MatchingRule *mr;
397                 struct berval *bv;
398
399                 switch ( type ) {
400                 case LDAP_FILTER_APPROX:
401                         mr = a->a_desc->ad_type->sat_approx;
402                         if( mr != NULL ) break;
403
404                         /* use EQUALITY matching rule if no APPROX rule */
405
406                 case LDAP_FILTER_EQUALITY:
407                         mr = a->a_desc->ad_type->sat_equality;
408                         break;
409
410                 case LDAP_FILTER_GE:
411                 case LDAP_FILTER_LE:
412                         mr = a->a_desc->ad_type->sat_ordering;
413                         break;
414
415                 default:
416                         mr = NULL;
417                 }
418
419                 if( mr == NULL ) continue;
420
421                 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
422                         int ret;
423                         int rc;
424                         const char *text;
425
426                         rc = value_match( &ret, a->a_desc, mr, 0,
427                                 bv, &ava->aa_value, &text );
428
429                         if( rc != LDAP_SUCCESS ) return rc;
430
431                         switch ( type ) {
432                         case LDAP_FILTER_EQUALITY:
433                         case LDAP_FILTER_APPROX:
434                                 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
435                                 break;
436
437                         case LDAP_FILTER_GE:
438                                 if ( ret >= 0 ) return LDAP_COMPARE_TRUE;
439                                 break;
440
441                         case LDAP_FILTER_LE:
442                                 if ( ret <= 0 ) return LDAP_COMPARE_TRUE;
443                                 break;
444                         }
445                 }
446         }
447
448         return LDAP_COMPARE_FALSE;
449 }
450
451
452 static int
453 test_presence_filter(
454         Operation       *op,
455         Entry           *e,
456         AttributeDescription *desc )
457 {
458         Attribute       *a;
459
460         if ( !access_allowed( op, e, desc, NULL, ACL_SEARCH, NULL ) ) {
461                 return LDAP_INSUFFICIENT_ACCESS;
462         }
463
464         a = attrs_find( e->e_attrs, desc );
465         if ( a == NULL && desc == slap_schema.si_ad_hasSubordinates ) {
466
467                 /*
468                  * XXX: fairly optimistic: if the function is defined,
469                  * then PRESENCE must succeed, because hasSubordinate
470                  * is boolean-valued; I think we may live with this 
471                  * simplification by now
472                  */
473                 if ( op && op->o_bd && op->o_bd->be_has_subordinates ) {
474                         return LDAP_COMPARE_TRUE;
475                 }
476
477                 return LDAP_COMPARE_FALSE;
478         }
479
480         return a != NULL ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
481 }
482
483
484 static int
485 test_filter_and(
486         Operation       *op,
487         Entry   *e,
488         Filter  *flist )
489 {
490         Filter  *f;
491         int rtn = LDAP_COMPARE_TRUE; /* True if empty */
492
493         Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
494
495
496         for ( f = flist; f != NULL; f = f->f_next ) {
497                 int rc = test_filter( op, e, f );
498
499                 if ( rc == LDAP_COMPARE_FALSE ) {
500                         /* filter is False */
501                         rtn = rc;
502                         break;
503                 }
504
505                 if ( rc != LDAP_COMPARE_TRUE ) {
506                         /* filter is Undefined unless later elements are False */
507                         rtn = rc;
508                 }
509         }
510
511         Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
512
513         return rtn;
514 }
515
516 static int
517 test_filter_or(
518         Operation       *op,
519         Entry   *e,
520         Filter  *flist )
521 {
522         Filter  *f;
523         int rtn = LDAP_COMPARE_FALSE; /* False if empty */
524
525         Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
526
527         for ( f = flist; f != NULL; f = f->f_next ) {
528                 int rc = test_filter( op, e, f );
529
530                 if ( rc == LDAP_COMPARE_TRUE ) {
531                         /* filter is True */
532                         rtn = rc;
533                         break;
534                 }
535
536                 if ( rc != LDAP_COMPARE_FALSE ) {
537                         /* filter is Undefined unless later elements are True */
538                         rtn = rc;
539                 }
540         }
541
542         Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
543         return rtn;
544 }
545
546
547 static int
548 test_substrings_filter(
549         Operation       *op,
550         Entry   *e,
551         Filter  *f )
552 {
553         Attribute       *a;
554
555         Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
556
557         if ( !access_allowed( op, e,
558                 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
559         {
560                 return LDAP_INSUFFICIENT_ACCESS;
561         }
562
563         for(a = attrs_find( e->e_attrs, f->f_sub_desc );
564                 a != NULL;
565                 a = attrs_find( a->a_next, f->f_sub_desc ) )
566         {
567                 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
568                 struct berval *bv;
569
570                 if( mr == NULL ) continue;
571
572                 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
573                         int ret;
574                         int rc;
575                         const char *text;
576
577                         rc = value_match( &ret, a->a_desc, mr, 0,
578                                 bv, f->f_sub, &text );
579
580                         if( rc != LDAP_SUCCESS ) return rc;
581                         if ( ret == 0 ) return LDAP_COMPARE_TRUE;
582                 }
583         }
584
585         Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n",
586                 0, 0, 0 );
587         return LDAP_COMPARE_FALSE;
588 }