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