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