1 /* filterindex.c - generate the list of candidate entries from a filter */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
21 #include <ac/socket.h>
22 #include <ac/string.h>
25 #include "back-ldbm.h"
27 static ID_BLOCK *presence_candidates(
29 AttributeDescription *desc );
30 static ID_BLOCK *equality_candidates(
31 Operation *op, AttributeAssertion *ava );
32 static ID_BLOCK *approx_candidates(
33 Operation *op, AttributeAssertion *ava );
34 static ID_BLOCK *substring_candidates(
36 SubstringsAssertion *sub );
37 static ID_BLOCK *list_candidates(
48 char *sub = "SUBTREE";
52 LDAP_LOG( FILTER, ENTRY, "filter_candidates: enter\n", 0, 0, 0 );
54 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
59 switch ( f->f_choice ) {
60 case SLAPD_FILTER_COMPUTED:
61 switch( f->f_result ) {
62 case SLAPD_COMPARE_UNDEFINED:
63 /* This technically is not the same as FALSE, but it
64 * certainly will produce no matches.
67 case LDAP_COMPARE_FALSE:
70 case LDAP_COMPARE_TRUE:
71 result = idl_allids( op->o_bd );
76 case SLAPD_FILTER_DN_ONE:
78 LDAP_LOG( FILTER, DETAIL1,
79 "filter_candidates: DN ONE (%s)\n", f->f_dn, 0, 0 );
81 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
84 /* an error is treated as an empty list */
85 if ( dn2idl( op->o_bd, f->f_dn, DN_ONE_PREFIX, &result ) != 0
92 case SLAPD_FILTER_DN_CHILDREN:
94 case SLAPD_FILTER_DN_SUBTREE:
96 LDAP_LOG( FILTER, DETAIL1,
97 "filter_candidates: DN %s (%s)\n", sub, f->f_dn, 0 );
99 Debug( LDAP_DEBUG_FILTER,
100 "\tDN %s\n", sub, 0, 0 );
103 /* an error is treated as an empty list */
104 if ( dn2idl( op->o_bd, f->f_dn, DN_SUBTREE_PREFIX, &result ) != 0
105 && result != NULL ) {
111 case LDAP_FILTER_PRESENT:
113 LDAP_LOG( FILTER, DETAIL1,
114 "filter_candidates: Present (%s)\n",
115 f->f_desc->ad_cname.bv_val, 0, 0 );
117 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
120 result = presence_candidates( op, f->f_desc );
123 case LDAP_FILTER_EQUALITY:
125 LDAP_LOG( FILTER, DETAIL1,
126 "filter_candidates: EQUALITY (%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, "\tEQUALITY\n", 0, 0, 0 );
133 result = equality_candidates( op, f->f_ava );
136 case LDAP_FILTER_APPROX:
138 LDAP_LOG( FILTER, DETAIL1,
139 "filter_candidates: APPROX (%s), (%s)\n",
140 f->f_ava->aa_desc->ad_cname.bv_val,
141 f->f_ava->aa_value.bv_val, 0 );
143 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
146 result = approx_candidates( op, f->f_ava );
149 case LDAP_FILTER_SUBSTRINGS:
151 LDAP_LOG( FILTER, DETAIL1,
152 "filter_candidates: SUBSTRINGS\n", 0, 0, 0 );
154 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
157 result = substring_candidates( op, f->f_sub );
162 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: GE\n", 0, 0, 0 );
164 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
167 result = presence_candidates( op, f->f_ava->aa_desc );
172 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: LE\n", 0, 0, 0 );
174 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
177 result = presence_candidates( op, f->f_ava->aa_desc );
180 case LDAP_FILTER_AND:
182 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: AND\n", 0, 0, 0 );
184 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
187 result = list_candidates( op, f->f_and, LDAP_FILTER_AND );
192 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: OR\n", 0, 0, 0 );
194 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
197 result = list_candidates( op, f->f_or, LDAP_FILTER_OR );
200 case LDAP_FILTER_NOT:
202 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: NOT\n", 0, 0, 0 );
204 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
208 * As candidates lists may contain entries which do
209 * not match the assertion, negation of the inner candidate
210 * list could result in matching entries be excluded from
211 * the returned candidate list.
213 result = idl_allids( op->o_bd );
217 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: UNKNOWN\n", 0, 0, 0 );
219 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN\n", 0, 0, 0 );
221 /* unknown filters must not return NULL, to allow
222 * extended filter processing to be done later.
224 result = idl_allids( op->o_bd );
229 LDAP_LOG( FILTER, ENTRY,
230 "filter_candidates: return %ld\n",
231 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
233 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
234 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
243 AttributeDescription *desc
251 struct berval prefix = {0, NULL};
254 LDAP_LOG( FILTER, ENTRY, "presence_candidates: enter\n", 0, 0, 0 );
256 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
259 idl = idl_allids( op->o_bd );
261 if( desc == slap_schema.si_ad_objectClass ) {
265 rc = index_param( op->o_bd, desc, LDAP_FILTER_PRESENT,
266 &dbname, &mask, &prefix );
268 if( rc != LDAP_SUCCESS ) {
270 LDAP_LOG( FILTER, INFO,
271 "presence_candidates: index_param returned %d\n", rc, 0, 0 );
273 Debug( LDAP_DEBUG_TRACE,
274 "<= presence_candidates: index_param returned=%d\n",
281 if( dbname == NULL ) {
284 LDAP_LOG( FILTER, INFO, "presence_candidates: not indexed\n", 0, 0, 0 );
286 Debug( LDAP_DEBUG_TRACE,
287 "<= presense_candidates: not indexed\n",
294 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
298 LDAP_LOG( FILTER, INFO,
299 "presence_candidates: db open failed (%s%s)\n",
300 dbname, LDBM_SUFFIX, 0 );
302 Debug( LDAP_DEBUG_ANY,
303 "<= presense_candidates db open failed (%s%s)\n",
304 dbname, LDBM_SUFFIX, 0 );
310 if( prefix.bv_val != NULL ) {
314 rc = key_read( op->o_bd, db, &prefix, &idl );
316 if( rc != LDAP_SUCCESS ) {
318 LDAP_LOG( FILTER, ERR,
319 "presence_candidates: key read failed (%d)\n", rc, 0, 0 );
321 Debug( LDAP_DEBUG_TRACE,
322 "<= presense_candidates key read failed (%d)\n",
327 } else if( idl == NULL ) {
329 LDAP_LOG( FILTER, DETAIL1, "presence_candidates: NULL\n", 0, 0, 0 );
331 Debug( LDAP_DEBUG_TRACE,
332 "<= presense_candidates NULL\n",
339 ldbm_cache_close( op->o_bd, db );
342 LDAP_LOG( FILTER, ENTRY,
343 "presence_candidates: return %ld\n",
344 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
346 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
347 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
356 AttributeAssertion *ava
365 struct berval prefix = {0, NULL};
366 struct berval *keys = NULL;
370 LDAP_LOG( FILTER, ENTRY, "equality_candidates: enter\n", 0, 0, 0 );
372 Debug( LDAP_DEBUG_TRACE, "=> equality_candidates\n", 0, 0, 0 );
376 idl = idl_allids( op->o_bd );
378 rc = index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_EQUALITY,
379 &dbname, &mask, &prefix );
381 if( rc != LDAP_SUCCESS ) {
383 LDAP_LOG( FILTER, ERR,
384 "equality_candidates: index_param returned %d\n", rc, 0, 0 );
386 Debug( LDAP_DEBUG_TRACE,
387 "<= equality_candidates: index_param returned=%d\n",
394 if( dbname == NULL ) {
397 LDAP_LOG( FILTER, ERR, "equality_candidates: not indexed\n", 0, 0, 0 );
399 Debug( LDAP_DEBUG_TRACE,
400 "<= equality_candidates: not indexed\n",
407 mr = ava->aa_desc->ad_type->sat_equality;
412 if( !mr->smr_filter ) {
416 rc = (mr->smr_filter)(
417 LDAP_FILTER_EQUALITY,
419 ava->aa_desc->ad_type->sat_syntax,
423 &keys, op->o_tmpmemctx );
425 if( rc != LDAP_SUCCESS ) {
427 LDAP_LOG( FILTER, ERR,
428 "equality_candidates: (%s%s) MR filter failed (%d\n",
429 dbname, LDBM_SUFFIX, rc );
431 Debug( LDAP_DEBUG_TRACE,
432 "<= equality_candidates: (%s%s) MR filter failed (%d)\n",
433 dbname, LDBM_SUFFIX, rc );
441 LDAP_LOG( FILTER, ERR,
442 "equality_candidates: no keys (%s%s)\n", dbname, LDBM_SUFFIX, 0 );
444 Debug( LDAP_DEBUG_TRACE,
445 "<= equality_candidates: no keys (%s%s)\n",
446 dbname, LDBM_SUFFIX, 0 );
452 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
456 LDAP_LOG( FILTER, ERR, "equality_candidates: db open failed (%s%s)\n",
457 dbname, LDBM_SUFFIX, 0 );
459 Debug( LDAP_DEBUG_ANY,
460 "<= equality_candidates db open failed (%s%s)\n",
461 dbname, LDBM_SUFFIX, 0 );
467 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
471 rc = key_read( op->o_bd, db, &keys[i], &tmp );
473 if( rc != LDAP_SUCCESS ) {
477 LDAP_LOG( FILTER, ERR,
478 "equality_candidates: key read failed (%d)\n", rc, 0, 0 );
480 Debug( LDAP_DEBUG_TRACE,
481 "<= equality_candidates key read failed (%d)\n",
492 LDAP_LOG( FILTER, INFO, "equality_candidates NULL\n", 0, 0, 0 );
494 Debug( LDAP_DEBUG_TRACE,
495 "<= equality_candidates NULL\n",
503 idl = idl_intersection( op->o_bd, idl, tmp );
507 if( idl == NULL ) break;
510 ber_bvarray_free_x( keys, op->o_tmpmemctx );
512 ldbm_cache_close( op->o_bd, db );
516 LDAP_LOG( FILTER, ENTRY,
517 "equality_candidates: return %ld\n",
518 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
520 Debug( LDAP_DEBUG_TRACE, "<= equality_candidates %ld\n",
521 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
530 AttributeAssertion *ava
539 struct berval prefix = {0, NULL};
540 struct berval *keys = NULL;
544 LDAP_LOG( FILTER, ENTRY, "approx_candidates: enter\n", 0, 0, 0 );
546 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
550 idl = idl_allids( op->o_bd );
552 rc = index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_APPROX,
553 &dbname, &mask, &prefix );
555 if( rc != LDAP_SUCCESS ) {
557 LDAP_LOG( FILTER, ERR,
558 "approx_candidates: index_param returned %d\n", rc, 0, 0 );
560 Debug( LDAP_DEBUG_TRACE,
561 "<= approx_candidates: index_param returned=%d\n",
568 if( dbname == NULL ) {
571 LDAP_LOG( FILTER, ERR, "approx_candidates: not indexed\n", 0, 0, 0 );
573 Debug( LDAP_DEBUG_ANY,
574 "<= approx_candidates: not indexed\n",
581 mr = ava->aa_desc->ad_type->sat_approx;
583 /* no approx matching rule, try equality matching rule */
584 mr = ava->aa_desc->ad_type->sat_equality;
591 if( !mr->smr_filter ) {
595 rc = (mr->smr_filter)(
598 ava->aa_desc->ad_type->sat_syntax,
602 &keys, op->o_tmpmemctx );
604 if( rc != LDAP_SUCCESS ) {
606 LDAP_LOG( FILTER, ERR,
607 "approx_candidates: (%s%s) MR filter failed (%d)\n",
608 dbname, LDBM_SUFFIX, rc );
610 Debug( LDAP_DEBUG_TRACE,
611 "<= approx_candidates: (%s%s) MR filter failed (%d)\n",
612 dbname, LDBM_SUFFIX, rc );
620 LDAP_LOG( FILTER, INFO,
621 "approx_candidates: no keys (%s%s)\n",
622 dbname, LDBM_SUFFIX, 0 );
624 Debug( LDAP_DEBUG_TRACE,
625 "<= approx_candidates: no keys (%s%s)\n",
626 dbname, LDBM_SUFFIX, 0 );
632 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
636 LDAP_LOG( FILTER, ERR,
637 "approx_candidates db open failed (%s%s)\n",
638 dbname, LDBM_SUFFIX, 0 );
640 Debug( LDAP_DEBUG_ANY,
641 "<= approx_candidates db open failed (%s%s)\n",
642 dbname, LDBM_SUFFIX, 0 );
648 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
652 rc = key_read( op->o_bd, db, &keys[i], &tmp );
654 if( rc != LDAP_SUCCESS ) {
658 LDAP_LOG( FILTER, ERR,
659 "approx_candidates: key read failed (%d)\n", rc, 0, 0 );
661 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates key read failed (%d)\n",
672 LDAP_LOG( FILTER, INFO, "approx_candidates: NULL\n", 0, 0, 0 );
674 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
682 idl = idl_intersection( op->o_bd, idl, tmp );
686 if( idl == NULL ) break;
689 ber_bvarray_free_x( keys, op->o_tmpmemctx );
691 ldbm_cache_close( op->o_bd, db );
694 LDAP_LOG( FILTER, ENTRY,
695 "approx_candidates: return %ld\n",
696 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
698 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
699 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
712 ID_BLOCK *idl, *tmp, *tmp2;
716 LDAP_LOG( FILTER, ENTRY, "list_candidates: 0x%x\n", ftype, 0, 0 );
718 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
723 for ( f = flist; f != NULL; f = f->f_next ) {
724 if ( (tmp = filter_candidates( op, f )) == NULL &&
725 ftype == LDAP_FILTER_AND ) {
727 LDAP_LOG( FILTER, INFO, "list_candidates: NULL\n", 0, 0, 0 );
729 Debug( LDAP_DEBUG_TRACE,
730 "<= list_candidates NULL\n", 0, 0, 0 );
740 } else if ( ftype == LDAP_FILTER_AND ) {
741 idl = idl_intersection( op->o_bd, idl, tmp );
745 idl = idl_union( op->o_bd, idl, tmp );
752 LDAP_LOG( FILTER, ENTRY, "list_candidates: return %ld\n",
753 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
755 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
756 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
763 substring_candidates(
765 SubstringsAssertion *sub
774 struct berval prefix = {0, NULL};
775 struct berval *keys = NULL;
779 LDAP_LOG( FILTER, ENTRY, "substrings_candidates: enter\n", 0, 0, 0 );
781 Debug( LDAP_DEBUG_TRACE, "=> substrings_candidates\n", 0, 0, 0 );
785 idl = idl_allids( op->o_bd );
787 rc = index_param( op->o_bd, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
788 &dbname, &mask, &prefix );
790 if( rc != LDAP_SUCCESS ) {
792 LDAP_LOG( FILTER, ERR,
793 "substrings_candidates: index_param returned %d\n", rc, 0, 0 );
795 Debug( LDAP_DEBUG_TRACE,
796 "<= substrings_candidates: index_param returned=%d\n",
803 if( dbname == NULL ) {
806 LDAP_LOG( FILTER, ERR, "substrings_candidates: not indexed\n", 0, 0, 0);
808 Debug( LDAP_DEBUG_ANY,
809 "<= substrings_candidates: not indexed\n",
816 mr = sub->sa_desc->ad_type->sat_substr;
822 if( !mr->smr_filter ) {
826 rc = (mr->smr_filter)(
827 LDAP_FILTER_SUBSTRINGS,
829 sub->sa_desc->ad_type->sat_syntax,
833 &keys, op->o_tmpmemctx );
835 if( rc != LDAP_SUCCESS ) {
837 LDAP_LOG( FILTER, ERR,
838 "substrings_candidates: (%s%s) MR filter failed (%d)\n",
839 dbname, LDBM_SUFFIX, rc );
841 Debug( LDAP_DEBUG_TRACE,
842 "<= substrings_candidates: (%s%s) MR filter failed (%d)\n",
843 dbname, LDBM_SUFFIX, rc );
851 LDAP_LOG( FILTER, ERR,
852 "substrings_candidates: (0x%04lx) no keys (%s%s)\n",
853 mask, dbname, LDBM_SUFFIX );
855 Debug( LDAP_DEBUG_TRACE,
856 "<= substrings_candidates: (0x%04lx) no keys (%s%s)\n",
857 mask, dbname, LDBM_SUFFIX );
863 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
867 LDAP_LOG( FILTER, ERR,
868 "substrings_candidates: db open failed (%s%s)\n",
869 dbname, LDBM_SUFFIX, 0 );
871 Debug( LDAP_DEBUG_ANY,
872 "<= substrings_candidates db open failed (%s%s)\n",
873 dbname, LDBM_SUFFIX, 0 );
879 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
883 rc = key_read( op->o_bd, db, &keys[i], &tmp );
885 if( rc != LDAP_SUCCESS ) {
889 LDAP_LOG( FILTER, ERR,
890 "substrings_candidates: key read failed (%d)\n", rc, 0, 0 );
892 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates key read failed (%d)\n",
903 LDAP_LOG( FILTER, INFO, "substrings_candidates: NULL\n", 0, 0, 0 );
905 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates NULL\n",
913 idl = idl_intersection( op->o_bd, idl, tmp );
917 if( idl == NULL ) break;
920 ber_bvarray_free_x( keys, op->o_tmpmemctx );
922 ldbm_cache_close( op->o_bd, db );
925 LDAP_LOG( FILTER, ENTRY,
926 "substrings_candidates: return %ld\n",
927 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
929 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates %ld\n",
930 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );