1 /* filterindex.c - generate the list of candidate entries from a filter */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
16 #include "back-ldbm.h"
18 static ID_BLOCK *presence_candidates(
20 AttributeDescription *desc );
21 static ID_BLOCK *equality_candidates(
22 Backend *be, AttributeAssertion *ava );
23 static ID_BLOCK *approx_candidates(
24 Backend *be, AttributeAssertion *ava );
25 static ID_BLOCK *substring_candidates(
27 SubstringsAssertion *sub );
28 static ID_BLOCK *list_candidates(
42 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY, "filter_candidates: enter\n"));
44 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
49 switch ( f->f_choice ) {
50 case SLAPD_FILTER_DN_ONE:
52 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
53 "filter_candidates: DN ONE (%s)\n", f->f_dn ));
55 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
58 /* an error is treated as an empty list */
59 if ( dn2idl( be, f->f_dn, DN_ONE_PREFIX, &result ) != 0
66 case SLAPD_FILTER_DN_SUBTREE:
68 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
69 "filter_candidates: DN SUBTREE (%s)\n", f->f_dn ));
71 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
74 /* an error is treated as an empty list */
75 if ( dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX, &result ) != 0
82 case LDAP_FILTER_PRESENT:
84 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
85 "filter_candidates: Present (%s)\n", f->f_desc->ad_cname.bv_val ));
87 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
90 result = presence_candidates( be, f->f_desc );
93 case LDAP_FILTER_EQUALITY:
95 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
96 "filter_candidates: EQUALITY (%s),(%s)\n",
97 f->f_ava->aa_desc->ad_cname.bv_val,
98 f->f_ava->aa_value.bv_val ));
100 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
103 result = equality_candidates( be, f->f_ava );
106 case LDAP_FILTER_APPROX:
108 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
109 "filter_candidates: APPROX (%s), (%s)\n",
110 f->f_ava->aa_desc->ad_cname.bv_val,
111 f->f_ava->aa_value.bv_val ));
113 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
116 result = approx_candidates( be, f->f_ava );
119 case LDAP_FILTER_SUBSTRINGS:
121 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
122 "filter_candidates: SUBSTRINGS\n"));
124 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
127 result = substring_candidates( be, f->f_sub );
132 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
133 "filter_candidates: GE\n"));
135 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
138 result = presence_candidates( be, f->f_ava->aa_desc );
143 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
144 "filter_candidates: LE\n" ));
146 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
149 result = presence_candidates( be, f->f_ava->aa_desc );
152 case LDAP_FILTER_AND:
154 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
155 "filter_candidates: AND\n" ));
157 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
160 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
165 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
166 "filter_candidates: OR\n" ));
168 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
171 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
174 case LDAP_FILTER_NOT:
176 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
177 "filter_candidates: NOT\n" ));
179 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
183 * As candidates lists may contain entries which do
184 * not match the assertion, negation of the inner candidate
185 * list could result in matching entries be excluded from
186 * the returned candidate list.
188 result = idl_allids( be );
192 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
193 "filter_candidates: UNKNOWN\n" ));
195 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN\n", 0, 0, 0 );
197 /* unknown filters must not return NULL, to allow
198 * extended filter processing to be done later.
200 result = idl_allids( be );
205 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
206 "filter_candidates: return %ld\n",
207 result ? ID_BLOCK_NIDS(result) : 0 ));
209 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
210 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
219 AttributeDescription *desc
227 struct berval prefix = {0};
230 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
231 "presence_candidates: enter\n" ));
233 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
236 idl = idl_allids( be );
238 if( desc == slap_schema.si_ad_objectClass ) {
242 rc = index_param( be, desc, LDAP_FILTER_PRESENT,
243 &dbname, &mask, &prefix );
245 if( rc != LDAP_SUCCESS ) {
247 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
248 "presence_candidates: index_param returned %d\n",
251 Debug( LDAP_DEBUG_TRACE,
252 "<= presence_candidates: index_param returned=%d\n",
259 if( dbname == NULL ) {
262 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
263 "presence_candidates: not indexed\n" ));
265 Debug( LDAP_DEBUG_TRACE,
266 "<= presense_candidates: not indexed\n",
273 db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
277 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
278 "presence_candidates: db open failed (%s%s)\n",
279 dbname, LDBM_SUFFIX ));
281 Debug( LDAP_DEBUG_ANY,
282 "<= presense_candidates db open failed (%s%s)\n",
283 dbname, LDBM_SUFFIX, 0 );
289 if( prefix.bv_val != NULL ) {
293 rc = key_read( be, db, &prefix, &idl );
295 if( rc != LDAP_SUCCESS ) {
297 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
298 "presence_candidates: key read failed (%d)\n", rc ));
300 Debug( LDAP_DEBUG_TRACE,
301 "<= presense_candidates key read failed (%d)\n",
306 } else if( idl == NULL ) {
308 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
309 "presence_candidates: NULL\n" ));
311 Debug( LDAP_DEBUG_TRACE,
312 "<= presense_candidates NULL\n",
319 ldbm_cache_close( be, db );
322 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
323 "presence_candidates: return %ld\n",
324 idl ? ID_BLOCK_NIDS(idl) : 0 ));
326 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
327 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
336 AttributeAssertion *ava
345 struct berval prefix = {0};
346 struct berval *keys = NULL;
350 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
351 "equality_candidates: enter\n" ));
353 Debug( LDAP_DEBUG_TRACE, "=> equality_candidates\n", 0, 0, 0 );
357 idl = idl_allids( be );
359 rc = index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
360 &dbname, &mask, &prefix );
362 if( rc != LDAP_SUCCESS ) {
364 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
365 "equality_candidates: index_param returned %d\n", rc ));
367 Debug( LDAP_DEBUG_TRACE,
368 "<= equality_candidates: index_param returned=%d\n",
375 if( dbname == NULL ) {
378 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
379 "equality_candidates: not indexed\n" ));
381 Debug( LDAP_DEBUG_TRACE,
382 "<= equality_candidates: not indexed\n",
389 mr = ava->aa_desc->ad_type->sat_equality;
394 if( !mr->smr_filter ) {
398 rc = (mr->smr_filter)(
399 LDAP_FILTER_EQUALITY,
401 ava->aa_desc->ad_type->sat_syntax,
407 if( rc != LDAP_SUCCESS ) {
409 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
410 "equality_candidates: (%s%s) MR filter failed (%d\n",
411 dbname, LDBM_SUFFIX, rc ));
413 Debug( LDAP_DEBUG_TRACE,
414 "<= equality_candidates: (%s%s) MR filter failed (%d)\n",
415 dbname, LDBM_SUFFIX, rc );
423 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
424 "equality_candidates: no keys (%s%s)\n",
425 dbname, LDBM_SUFFIX ));
427 Debug( LDAP_DEBUG_TRACE,
428 "<= equality_candidates: no keys (%s%s)\n",
429 dbname, LDBM_SUFFIX, 0 );
435 db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
439 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
440 "equality_candidates: db open failed (%s%s)\n",
441 dbname, LDBM_SUFFIX ));
443 Debug( LDAP_DEBUG_ANY,
444 "<= equality_candidates db open failed (%s%s)\n",
445 dbname, LDBM_SUFFIX, 0 );
451 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
455 rc = key_read( be, db, &keys[i], &tmp );
457 if( rc != LDAP_SUCCESS ) {
461 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
462 "equality_candidates: key read failed (%d)\n", rc ));
464 Debug( LDAP_DEBUG_TRACE,
465 "<= equality_candidates key read failed (%d)\n",
476 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
477 "equality_candidates NULL\n" ));
479 Debug( LDAP_DEBUG_TRACE,
480 "<= equality_candidates NULL\n",
488 idl = idl_intersection( be, idl, tmp );
492 if( idl == NULL ) break;
495 ber_bvarray_free( keys );
497 ldbm_cache_close( be, db );
501 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
502 "equality_candidates: return %ld\n",
503 idl ? ID_BLOCK_NIDS(idl) : 0 ));
505 Debug( LDAP_DEBUG_TRACE, "<= equality_candidates %ld\n",
506 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
515 AttributeAssertion *ava
524 struct berval prefix = {0};
525 struct berval *keys = NULL;
529 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
530 "approx_candidates: enter\n" ));
532 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
536 idl = idl_allids( be );
538 rc = index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
539 &dbname, &mask, &prefix );
541 if( rc != LDAP_SUCCESS ) {
543 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
544 "approx_candidates: index_param returned %d\n", rc ));
546 Debug( LDAP_DEBUG_TRACE,
547 "<= approx_candidates: index_param returned=%d\n",
554 if( dbname == NULL ) {
557 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
558 "approx_candidates: not indexed\n" ));
560 Debug( LDAP_DEBUG_ANY,
561 "<= approx_candidates: not indexed\n",
568 mr = ava->aa_desc->ad_type->sat_approx;
570 /* no approx matching rule, try equality matching rule */
571 mr = ava->aa_desc->ad_type->sat_equality;
578 if( !mr->smr_filter ) {
582 rc = (mr->smr_filter)(
585 ava->aa_desc->ad_type->sat_syntax,
591 if( rc != LDAP_SUCCESS ) {
593 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
594 "approx_candidates: (%s%s) MR filter failed (%d)\n",
595 dbname, LDBM_SUFFIX, rc ));
597 Debug( LDAP_DEBUG_TRACE,
598 "<= approx_candidates: (%s%s) MR filter failed (%d)\n",
599 dbname, LDBM_SUFFIX, rc );
607 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
608 "approx_candidates: no keys (%s%s)\n",
609 dbname, LDBM_SUFFIX ));
611 Debug( LDAP_DEBUG_TRACE,
612 "<= approx_candidates: no keys (%s%s)\n",
613 dbname, LDBM_SUFFIX, 0 );
619 db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
623 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
624 "approx_candidates db open failed (%s%s)\n",
625 dbname, LDBM_SUFFIX ));
627 Debug( LDAP_DEBUG_ANY,
628 "<= approx_candidates db open failed (%s%s)\n",
629 dbname, LDBM_SUFFIX, 0 );
635 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
639 rc = key_read( be, db, &keys[i], &tmp );
641 if( rc != LDAP_SUCCESS ) {
645 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
646 "approx_candidates: key read failed (%d)\n", rc ));
648 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates key read failed (%d)\n",
659 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
660 "approx_candidates: NULL\n" ));
662 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
670 idl = idl_intersection( be, idl, tmp );
674 if( idl == NULL ) break;
677 ber_bvarray_free( keys );
679 ldbm_cache_close( be, db );
682 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
683 "approx_candidates: return %ld\n",
684 idl ? ID_BLOCK_NIDS(idl) : 0 ));
686 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
687 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
700 ID_BLOCK *idl, *tmp, *tmp2;
704 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
705 "list_candidates: 0x%x\n", ftype ));
707 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
712 for ( f = flist; f != NULL; f = f->f_next ) {
713 if ( (tmp = filter_candidates( be, f )) == NULL &&
714 ftype == LDAP_FILTER_AND ) {
716 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
717 "list_candidates: NULL\n" ));
719 Debug( LDAP_DEBUG_TRACE,
720 "<= list_candidates NULL\n", 0, 0, 0 );
730 } else if ( ftype == LDAP_FILTER_AND ) {
731 idl = idl_intersection( be, idl, tmp );
735 idl = idl_union( be, idl, tmp );
742 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
743 "list_candidates: return %ld\n",
744 idl ? ID_BLOCK_NIDS(idl) : 0 ));
746 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
747 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
754 substring_candidates(
756 SubstringsAssertion *sub
765 struct berval prefix = {0};
766 struct berval *keys = NULL;
770 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
771 "substrings_candidates: enter\n" ));
773 Debug( LDAP_DEBUG_TRACE, "=> substrings_candidates\n", 0, 0, 0 );
777 idl = idl_allids( be );
779 rc = index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
780 &dbname, &mask, &prefix );
782 if( rc != LDAP_SUCCESS ) {
784 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
785 "substrings_candidates: index_param returned %d\n", rc ));
787 Debug( LDAP_DEBUG_TRACE,
788 "<= substrings_candidates: index_param returned=%d\n",
795 if( dbname == NULL ) {
798 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
799 "substrings_candidates: not indexed\n" ));
801 Debug( LDAP_DEBUG_ANY,
802 "<= substrings_candidates: not indexed\n",
809 mr = sub->sa_desc->ad_type->sat_substr;
815 if( !mr->smr_filter ) {
819 rc = (mr->smr_filter)(
820 LDAP_FILTER_SUBSTRINGS,
822 sub->sa_desc->ad_type->sat_syntax,
828 if( rc != LDAP_SUCCESS ) {
830 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
831 "substrings_candidates: (%s%s) MR filter failed (%d)\n",
832 dbname, LDBM_SUFFIX, rc ));
834 Debug( LDAP_DEBUG_TRACE,
835 "<= substrings_candidates: (%s%s) MR filter failed (%d)\n",
836 dbname, LDBM_SUFFIX, rc );
844 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
845 "substrings_candidates: (0x%04lx) no keys (%s%s)\n",
846 mask, dbname, LDBM_SUFFIX ));
848 Debug( LDAP_DEBUG_TRACE,
849 "<= substrings_candidates: (0x%04lx) no keys (%s%s)\n",
850 mask, dbname, LDBM_SUFFIX );
856 db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
860 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
861 "substrings_candidates: db open failed (%s%s)\n",
862 dbname, LDBM_SUFFIX ));
864 Debug( LDAP_DEBUG_ANY,
865 "<= substrings_candidates db open failed (%s%s)\n",
866 dbname, LDBM_SUFFIX, 0 );
872 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
876 rc = key_read( be, db, &keys[i], &tmp );
878 if( rc != LDAP_SUCCESS ) {
882 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
883 "substrings_candidates: key read failed (%d)\n",
886 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates key read failed (%d)\n",
897 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
898 "substrings_candidates: NULL\n" ));
900 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates NULL\n",
908 idl = idl_intersection( be, idl, tmp );
912 if( idl == NULL ) break;
915 ber_bvarray_free( keys );
917 ldbm_cache_close( be, db );
920 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
921 "substrings_candidates: return %ld\n",
922 idl ? ID_BLOCK_NIDS(idl) : 0 ));
924 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates %ld\n",
925 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );