1 /* filterindex.c - generate the list of candidate entries from a filter */
4 * Copyright 1998-2003 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 Operation *op, AttributeAssertion *ava );
23 static ID_BLOCK *approx_candidates(
24 Operation *op, AttributeAssertion *ava );
25 static ID_BLOCK *substring_candidates(
27 SubstringsAssertion *sub );
28 static ID_BLOCK *list_candidates(
42 LDAP_LOG( FILTER, ENTRY, "filter_candidates: enter\n", 0, 0, 0 );
44 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
49 switch ( f->f_choice ) {
50 case SLAPD_FILTER_COMPUTED:
51 switch( f->f_result ) {
52 case SLAPD_COMPARE_UNDEFINED:
53 /* This technically is not the same as FALSE, but it
54 * certainly will produce no matches.
57 case LDAP_COMPARE_FALSE:
60 case LDAP_COMPARE_TRUE:
61 result = idl_allids( op->o_bd );
66 case SLAPD_FILTER_DN_ONE:
68 LDAP_LOG( FILTER, DETAIL1,
69 "filter_candidates: DN ONE (%s)\n", f->f_dn, 0, 0 );
71 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
74 /* an error is treated as an empty list */
75 if ( dn2idl( op->o_bd, f->f_dn, DN_ONE_PREFIX, &result ) != 0
82 case SLAPD_FILTER_DN_SUBTREE:
84 LDAP_LOG( FILTER, DETAIL1,
85 "filter_candidates: DN SUBTREE (%s)\n", f->f_dn, 0, 0 );
87 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
90 /* an error is treated as an empty list */
91 if ( dn2idl( op->o_bd, f->f_dn, DN_SUBTREE_PREFIX, &result ) != 0
98 case LDAP_FILTER_PRESENT:
100 LDAP_LOG( FILTER, DETAIL1,
101 "filter_candidates: Present (%s)\n",
102 f->f_desc->ad_cname.bv_val, 0, 0 );
104 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
107 result = presence_candidates( op, f->f_desc );
110 case LDAP_FILTER_EQUALITY:
112 LDAP_LOG( FILTER, DETAIL1,
113 "filter_candidates: EQUALITY (%s),(%s)\n",
114 f->f_ava->aa_desc->ad_cname.bv_val,
115 f->f_ava->aa_value.bv_val, 0 );
117 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
120 result = equality_candidates( op, f->f_ava );
123 case LDAP_FILTER_APPROX:
125 LDAP_LOG( FILTER, DETAIL1,
126 "filter_candidates: APPROX (%s), (%s)\n",
127 f->f_ava->aa_desc->ad_cname.bv_val,
128 f->f_ava->aa_value.bv_val, 0 );
130 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
133 result = approx_candidates( op, f->f_ava );
136 case LDAP_FILTER_SUBSTRINGS:
138 LDAP_LOG( FILTER, DETAIL1,
139 "filter_candidates: SUBSTRINGS\n", 0, 0, 0 );
141 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
144 result = substring_candidates( op, f->f_sub );
149 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: GE\n", 0, 0, 0 );
151 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
154 result = presence_candidates( op, f->f_ava->aa_desc );
159 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: LE\n", 0, 0, 0 );
161 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
164 result = presence_candidates( op, f->f_ava->aa_desc );
167 case LDAP_FILTER_AND:
169 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: AND\n", 0, 0, 0 );
171 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
174 result = list_candidates( op, f->f_and, LDAP_FILTER_AND );
179 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: OR\n", 0, 0, 0 );
181 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
184 result = list_candidates( op, f->f_or, LDAP_FILTER_OR );
187 case LDAP_FILTER_NOT:
189 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: NOT\n", 0, 0, 0 );
191 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
195 * As candidates lists may contain entries which do
196 * not match the assertion, negation of the inner candidate
197 * list could result in matching entries be excluded from
198 * the returned candidate list.
200 result = idl_allids( op->o_bd );
204 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: UNKNOWN\n", 0, 0, 0 );
206 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN\n", 0, 0, 0 );
208 /* unknown filters must not return NULL, to allow
209 * extended filter processing to be done later.
211 result = idl_allids( op->o_bd );
216 LDAP_LOG( FILTER, ENTRY,
217 "filter_candidates: return %ld\n",
218 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
220 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
221 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
230 AttributeDescription *desc
238 struct berval prefix = {0, NULL};
241 LDAP_LOG( FILTER, ENTRY, "presence_candidates: enter\n", 0, 0, 0 );
243 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
246 idl = idl_allids( op->o_bd );
248 if( desc == slap_schema.si_ad_objectClass ) {
252 rc = index_param( op->o_bd, desc, LDAP_FILTER_PRESENT,
253 &dbname, &mask, &prefix );
255 if( rc != LDAP_SUCCESS ) {
257 LDAP_LOG( FILTER, INFO,
258 "presence_candidates: index_param returned %d\n", rc, 0, 0 );
260 Debug( LDAP_DEBUG_TRACE,
261 "<= presence_candidates: index_param returned=%d\n",
268 if( dbname == NULL ) {
271 LDAP_LOG( FILTER, INFO, "presence_candidates: not indexed\n", 0, 0, 0 );
273 Debug( LDAP_DEBUG_TRACE,
274 "<= presense_candidates: not indexed\n",
281 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
285 LDAP_LOG( FILTER, INFO,
286 "presence_candidates: db open failed (%s%s)\n",
287 dbname, LDBM_SUFFIX, 0 );
289 Debug( LDAP_DEBUG_ANY,
290 "<= presense_candidates db open failed (%s%s)\n",
291 dbname, LDBM_SUFFIX, 0 );
297 if( prefix.bv_val != NULL ) {
301 rc = key_read( op->o_bd, db, &prefix, &idl );
303 if( rc != LDAP_SUCCESS ) {
305 LDAP_LOG( FILTER, ERR,
306 "presence_candidates: key read failed (%d)\n", rc, 0, 0 );
308 Debug( LDAP_DEBUG_TRACE,
309 "<= presense_candidates key read failed (%d)\n",
314 } else if( idl == NULL ) {
316 LDAP_LOG( FILTER, DETAIL1, "presence_candidates: NULL\n", 0, 0, 0 );
318 Debug( LDAP_DEBUG_TRACE,
319 "<= presense_candidates NULL\n",
326 ldbm_cache_close( op->o_bd, db );
329 LDAP_LOG( FILTER, ENTRY,
330 "presence_candidates: return %ld\n",
331 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
333 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
334 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
343 AttributeAssertion *ava
352 struct berval prefix = {0, NULL};
353 struct berval *keys = NULL;
357 LDAP_LOG( FILTER, ENTRY, "equality_candidates: enter\n", 0, 0, 0 );
359 Debug( LDAP_DEBUG_TRACE, "=> equality_candidates\n", 0, 0, 0 );
363 idl = idl_allids( op->o_bd );
365 rc = index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_EQUALITY,
366 &dbname, &mask, &prefix );
368 if( rc != LDAP_SUCCESS ) {
370 LDAP_LOG( FILTER, ERR,
371 "equality_candidates: index_param returned %d\n", rc, 0, 0 );
373 Debug( LDAP_DEBUG_TRACE,
374 "<= equality_candidates: index_param returned=%d\n",
381 if( dbname == NULL ) {
384 LDAP_LOG( FILTER, ERR, "equality_candidates: not indexed\n", 0, 0, 0 );
386 Debug( LDAP_DEBUG_TRACE,
387 "<= equality_candidates: not indexed\n",
394 mr = ava->aa_desc->ad_type->sat_equality;
399 if( !mr->smr_filter ) {
403 rc = (mr->smr_filter)(
404 LDAP_FILTER_EQUALITY,
406 ava->aa_desc->ad_type->sat_syntax,
410 &keys, op->o_tmpmemctx );
412 if( rc != LDAP_SUCCESS ) {
414 LDAP_LOG( FILTER, ERR,
415 "equality_candidates: (%s%s) MR filter failed (%d\n",
416 dbname, LDBM_SUFFIX, rc );
418 Debug( LDAP_DEBUG_TRACE,
419 "<= equality_candidates: (%s%s) MR filter failed (%d)\n",
420 dbname, LDBM_SUFFIX, rc );
428 LDAP_LOG( FILTER, ERR,
429 "equality_candidates: no keys (%s%s)\n", dbname, LDBM_SUFFIX, 0 );
431 Debug( LDAP_DEBUG_TRACE,
432 "<= equality_candidates: no keys (%s%s)\n",
433 dbname, LDBM_SUFFIX, 0 );
439 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
443 LDAP_LOG( FILTER, ERR, "equality_candidates: db open failed (%s%s)\n",
444 dbname, LDBM_SUFFIX, 0 );
446 Debug( LDAP_DEBUG_ANY,
447 "<= equality_candidates db open failed (%s%s)\n",
448 dbname, LDBM_SUFFIX, 0 );
454 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
458 rc = key_read( op->o_bd, db, &keys[i], &tmp );
460 if( rc != LDAP_SUCCESS ) {
464 LDAP_LOG( FILTER, ERR,
465 "equality_candidates: key read failed (%d)\n", rc, 0, 0 );
467 Debug( LDAP_DEBUG_TRACE,
468 "<= equality_candidates key read failed (%d)\n",
479 LDAP_LOG( FILTER, INFO, "equality_candidates NULL\n", 0, 0, 0 );
481 Debug( LDAP_DEBUG_TRACE,
482 "<= equality_candidates NULL\n",
490 idl = idl_intersection( op->o_bd, idl, tmp );
494 if( idl == NULL ) break;
497 ber_bvarray_free_x( keys, op->o_tmpmemctx );
499 ldbm_cache_close( op->o_bd, db );
503 LDAP_LOG( FILTER, ENTRY,
504 "equality_candidates: return %ld\n",
505 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
507 Debug( LDAP_DEBUG_TRACE, "<= equality_candidates %ld\n",
508 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
517 AttributeAssertion *ava
526 struct berval prefix = {0, NULL};
527 struct berval *keys = NULL;
531 LDAP_LOG( FILTER, ENTRY, "approx_candidates: enter\n", 0, 0, 0 );
533 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
537 idl = idl_allids( op->o_bd );
539 rc = index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_APPROX,
540 &dbname, &mask, &prefix );
542 if( rc != LDAP_SUCCESS ) {
544 LDAP_LOG( FILTER, ERR,
545 "approx_candidates: index_param returned %d\n", rc, 0, 0 );
547 Debug( LDAP_DEBUG_TRACE,
548 "<= approx_candidates: index_param returned=%d\n",
555 if( dbname == NULL ) {
558 LDAP_LOG( FILTER, ERR, "approx_candidates: not indexed\n", 0, 0, 0 );
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,
589 &keys, op->o_tmpmemctx );
591 if( rc != LDAP_SUCCESS ) {
593 LDAP_LOG( FILTER, 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, INFO,
608 "approx_candidates: no keys (%s%s)\n",
609 dbname, LDBM_SUFFIX, 0 );
611 Debug( LDAP_DEBUG_TRACE,
612 "<= approx_candidates: no keys (%s%s)\n",
613 dbname, LDBM_SUFFIX, 0 );
619 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
623 LDAP_LOG( FILTER, ERR,
624 "approx_candidates db open failed (%s%s)\n",
625 dbname, LDBM_SUFFIX, 0 );
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( op->o_bd, db, &keys[i], &tmp );
641 if( rc != LDAP_SUCCESS ) {
645 LDAP_LOG( FILTER, ERR,
646 "approx_candidates: key read failed (%d)\n", rc, 0, 0 );
648 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates key read failed (%d)\n",
659 LDAP_LOG( FILTER, INFO, "approx_candidates: NULL\n", 0, 0, 0 );
661 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
669 idl = idl_intersection( op->o_bd, idl, tmp );
673 if( idl == NULL ) break;
676 ber_bvarray_free_x( keys, op->o_tmpmemctx );
678 ldbm_cache_close( op->o_bd, db );
681 LDAP_LOG( FILTER, ENTRY,
682 "approx_candidates: return %ld\n",
683 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
685 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
686 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
699 ID_BLOCK *idl, *tmp, *tmp2;
703 LDAP_LOG( FILTER, ENTRY, "list_candidates: 0x%x\n", ftype, 0, 0 );
705 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
710 for ( f = flist; f != NULL; f = f->f_next ) {
711 if ( (tmp = filter_candidates( op, f )) == NULL &&
712 ftype == LDAP_FILTER_AND ) {
714 LDAP_LOG( FILTER, INFO, "list_candidates: NULL\n", 0, 0, 0 );
716 Debug( LDAP_DEBUG_TRACE,
717 "<= list_candidates NULL\n", 0, 0, 0 );
727 } else if ( ftype == LDAP_FILTER_AND ) {
728 idl = idl_intersection( op->o_bd, idl, tmp );
732 idl = idl_union( op->o_bd, idl, tmp );
739 LDAP_LOG( FILTER, ENTRY, "list_candidates: return %ld\n",
740 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
742 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
743 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
750 substring_candidates(
752 SubstringsAssertion *sub
761 struct berval prefix = {0, NULL};
762 struct berval *keys = NULL;
766 LDAP_LOG( FILTER, ENTRY, "substrings_candidates: enter\n", 0, 0, 0 );
768 Debug( LDAP_DEBUG_TRACE, "=> substrings_candidates\n", 0, 0, 0 );
772 idl = idl_allids( op->o_bd );
774 rc = index_param( op->o_bd, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
775 &dbname, &mask, &prefix );
777 if( rc != LDAP_SUCCESS ) {
779 LDAP_LOG( FILTER, ERR,
780 "substrings_candidates: index_param returned %d\n", rc, 0, 0 );
782 Debug( LDAP_DEBUG_TRACE,
783 "<= substrings_candidates: index_param returned=%d\n",
790 if( dbname == NULL ) {
793 LDAP_LOG( FILTER, ERR, "substrings_candidates: not indexed\n", 0, 0, 0);
795 Debug( LDAP_DEBUG_ANY,
796 "<= substrings_candidates: not indexed\n",
803 mr = sub->sa_desc->ad_type->sat_substr;
809 if( !mr->smr_filter ) {
813 rc = (mr->smr_filter)(
814 LDAP_FILTER_SUBSTRINGS,
816 sub->sa_desc->ad_type->sat_syntax,
820 &keys, op->o_tmpmemctx );
822 if( rc != LDAP_SUCCESS ) {
824 LDAP_LOG( FILTER, ERR,
825 "substrings_candidates: (%s%s) MR filter failed (%d)\n",
826 dbname, LDBM_SUFFIX, rc );
828 Debug( LDAP_DEBUG_TRACE,
829 "<= substrings_candidates: (%s%s) MR filter failed (%d)\n",
830 dbname, LDBM_SUFFIX, rc );
838 LDAP_LOG( FILTER, ERR,
839 "substrings_candidates: (0x%04lx) no keys (%s%s)\n",
840 mask, dbname, LDBM_SUFFIX );
842 Debug( LDAP_DEBUG_TRACE,
843 "<= substrings_candidates: (0x%04lx) no keys (%s%s)\n",
844 mask, dbname, LDBM_SUFFIX );
850 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
854 LDAP_LOG( FILTER, ERR,
855 "substrings_candidates: db open failed (%s%s)\n",
856 dbname, LDBM_SUFFIX, 0 );
858 Debug( LDAP_DEBUG_ANY,
859 "<= substrings_candidates db open failed (%s%s)\n",
860 dbname, LDBM_SUFFIX, 0 );
866 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
870 rc = key_read( op->o_bd, db, &keys[i], &tmp );
872 if( rc != LDAP_SUCCESS ) {
876 LDAP_LOG( FILTER, ERR,
877 "substrings_candidates: key read failed (%d)\n", rc, 0, 0 );
879 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates key read failed (%d)\n",
890 LDAP_LOG( FILTER, INFO, "substrings_candidates: NULL\n", 0, 0, 0 );
892 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates NULL\n",
900 idl = idl_intersection( op->o_bd, idl, tmp );
904 if( idl == NULL ) break;
907 ber_bvarray_free_x( keys, op->o_tmpmemctx );
909 ldbm_cache_close( op->o_bd, db );
912 LDAP_LOG( FILTER, ENTRY,
913 "substrings_candidates: return %ld\n",
914 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
916 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates %ld\n",
917 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );