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-2003 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(
51 LDAP_LOG( FILTER, ENTRY, "filter_candidates: enter\n", 0, 0, 0 );
53 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
58 switch ( f->f_choice ) {
59 case SLAPD_FILTER_COMPUTED:
60 switch( f->f_result ) {
61 case SLAPD_COMPARE_UNDEFINED:
62 /* This technically is not the same as FALSE, but it
63 * certainly will produce no matches.
66 case LDAP_COMPARE_FALSE:
69 case LDAP_COMPARE_TRUE:
70 result = idl_allids( op->o_bd );
75 case SLAPD_FILTER_DN_ONE:
77 LDAP_LOG( FILTER, DETAIL1,
78 "filter_candidates: DN ONE (%s)\n", f->f_dn, 0, 0 );
80 Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
83 /* an error is treated as an empty list */
84 if ( dn2idl( op->o_bd, f->f_dn, DN_ONE_PREFIX, &result ) != 0
91 case SLAPD_FILTER_DN_SUBTREE:
93 LDAP_LOG( FILTER, DETAIL1,
94 "filter_candidates: DN SUBTREE (%s)\n", f->f_dn, 0, 0 );
96 Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
99 /* an error is treated as an empty list */
100 if ( dn2idl( op->o_bd, f->f_dn, DN_SUBTREE_PREFIX, &result ) != 0
101 && result != NULL ) {
107 case LDAP_FILTER_PRESENT:
109 LDAP_LOG( FILTER, DETAIL1,
110 "filter_candidates: Present (%s)\n",
111 f->f_desc->ad_cname.bv_val, 0, 0 );
113 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
116 result = presence_candidates( op, f->f_desc );
119 case LDAP_FILTER_EQUALITY:
121 LDAP_LOG( FILTER, DETAIL1,
122 "filter_candidates: EQUALITY (%s),(%s)\n",
123 f->f_ava->aa_desc->ad_cname.bv_val,
124 f->f_ava->aa_value.bv_val, 0 );
126 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
129 result = equality_candidates( op, f->f_ava );
132 case LDAP_FILTER_APPROX:
134 LDAP_LOG( FILTER, DETAIL1,
135 "filter_candidates: APPROX (%s), (%s)\n",
136 f->f_ava->aa_desc->ad_cname.bv_val,
137 f->f_ava->aa_value.bv_val, 0 );
139 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
142 result = approx_candidates( op, f->f_ava );
145 case LDAP_FILTER_SUBSTRINGS:
147 LDAP_LOG( FILTER, DETAIL1,
148 "filter_candidates: SUBSTRINGS\n", 0, 0, 0 );
150 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
153 result = substring_candidates( op, f->f_sub );
158 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: GE\n", 0, 0, 0 );
160 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
163 result = presence_candidates( op, f->f_ava->aa_desc );
168 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: LE\n", 0, 0, 0 );
170 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
173 result = presence_candidates( op, f->f_ava->aa_desc );
176 case LDAP_FILTER_AND:
178 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: AND\n", 0, 0, 0 );
180 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
183 result = list_candidates( op, f->f_and, LDAP_FILTER_AND );
188 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: OR\n", 0, 0, 0 );
190 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
193 result = list_candidates( op, f->f_or, LDAP_FILTER_OR );
196 case LDAP_FILTER_NOT:
198 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: NOT\n", 0, 0, 0 );
200 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
204 * As candidates lists may contain entries which do
205 * not match the assertion, negation of the inner candidate
206 * list could result in matching entries be excluded from
207 * the returned candidate list.
209 result = idl_allids( op->o_bd );
213 LDAP_LOG( FILTER, DETAIL1, "filter_candidates: UNKNOWN\n", 0, 0, 0 );
215 Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN\n", 0, 0, 0 );
217 /* unknown filters must not return NULL, to allow
218 * extended filter processing to be done later.
220 result = idl_allids( op->o_bd );
225 LDAP_LOG( FILTER, ENTRY,
226 "filter_candidates: return %ld\n",
227 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
229 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %ld\n",
230 result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
239 AttributeDescription *desc
247 struct berval prefix = {0, NULL};
250 LDAP_LOG( FILTER, ENTRY, "presence_candidates: enter\n", 0, 0, 0 );
252 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
255 idl = idl_allids( op->o_bd );
257 if( desc == slap_schema.si_ad_objectClass ) {
261 rc = index_param( op->o_bd, desc, LDAP_FILTER_PRESENT,
262 &dbname, &mask, &prefix );
264 if( rc != LDAP_SUCCESS ) {
266 LDAP_LOG( FILTER, INFO,
267 "presence_candidates: index_param returned %d\n", rc, 0, 0 );
269 Debug( LDAP_DEBUG_TRACE,
270 "<= presence_candidates: index_param returned=%d\n",
277 if( dbname == NULL ) {
280 LDAP_LOG( FILTER, INFO, "presence_candidates: not indexed\n", 0, 0, 0 );
282 Debug( LDAP_DEBUG_TRACE,
283 "<= presense_candidates: not indexed\n",
290 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
294 LDAP_LOG( FILTER, INFO,
295 "presence_candidates: db open failed (%s%s)\n",
296 dbname, LDBM_SUFFIX, 0 );
298 Debug( LDAP_DEBUG_ANY,
299 "<= presense_candidates db open failed (%s%s)\n",
300 dbname, LDBM_SUFFIX, 0 );
306 if( prefix.bv_val != NULL ) {
310 rc = key_read( op->o_bd, db, &prefix, &idl );
312 if( rc != LDAP_SUCCESS ) {
314 LDAP_LOG( FILTER, ERR,
315 "presence_candidates: key read failed (%d)\n", rc, 0, 0 );
317 Debug( LDAP_DEBUG_TRACE,
318 "<= presense_candidates key read failed (%d)\n",
323 } else if( idl == NULL ) {
325 LDAP_LOG( FILTER, DETAIL1, "presence_candidates: NULL\n", 0, 0, 0 );
327 Debug( LDAP_DEBUG_TRACE,
328 "<= presense_candidates NULL\n",
335 ldbm_cache_close( op->o_bd, db );
338 LDAP_LOG( FILTER, ENTRY,
339 "presence_candidates: return %ld\n",
340 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
342 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %ld\n",
343 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
352 AttributeAssertion *ava
361 struct berval prefix = {0, NULL};
362 struct berval *keys = NULL;
366 LDAP_LOG( FILTER, ENTRY, "equality_candidates: enter\n", 0, 0, 0 );
368 Debug( LDAP_DEBUG_TRACE, "=> equality_candidates\n", 0, 0, 0 );
372 idl = idl_allids( op->o_bd );
374 rc = index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_EQUALITY,
375 &dbname, &mask, &prefix );
377 if( rc != LDAP_SUCCESS ) {
379 LDAP_LOG( FILTER, ERR,
380 "equality_candidates: index_param returned %d\n", rc, 0, 0 );
382 Debug( LDAP_DEBUG_TRACE,
383 "<= equality_candidates: index_param returned=%d\n",
390 if( dbname == NULL ) {
393 LDAP_LOG( FILTER, ERR, "equality_candidates: not indexed\n", 0, 0, 0 );
395 Debug( LDAP_DEBUG_TRACE,
396 "<= equality_candidates: not indexed\n",
403 mr = ava->aa_desc->ad_type->sat_equality;
408 if( !mr->smr_filter ) {
412 rc = (mr->smr_filter)(
413 LDAP_FILTER_EQUALITY,
415 ava->aa_desc->ad_type->sat_syntax,
419 &keys, op->o_tmpmemctx );
421 if( rc != LDAP_SUCCESS ) {
423 LDAP_LOG( FILTER, ERR,
424 "equality_candidates: (%s%s) MR filter failed (%d\n",
425 dbname, LDBM_SUFFIX, rc );
427 Debug( LDAP_DEBUG_TRACE,
428 "<= equality_candidates: (%s%s) MR filter failed (%d)\n",
429 dbname, LDBM_SUFFIX, rc );
437 LDAP_LOG( FILTER, ERR,
438 "equality_candidates: no keys (%s%s)\n", dbname, LDBM_SUFFIX, 0 );
440 Debug( LDAP_DEBUG_TRACE,
441 "<= equality_candidates: no keys (%s%s)\n",
442 dbname, LDBM_SUFFIX, 0 );
448 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
452 LDAP_LOG( FILTER, ERR, "equality_candidates: db open failed (%s%s)\n",
453 dbname, LDBM_SUFFIX, 0 );
455 Debug( LDAP_DEBUG_ANY,
456 "<= equality_candidates db open failed (%s%s)\n",
457 dbname, LDBM_SUFFIX, 0 );
463 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
467 rc = key_read( op->o_bd, db, &keys[i], &tmp );
469 if( rc != LDAP_SUCCESS ) {
473 LDAP_LOG( FILTER, ERR,
474 "equality_candidates: key read failed (%d)\n", rc, 0, 0 );
476 Debug( LDAP_DEBUG_TRACE,
477 "<= equality_candidates key read failed (%d)\n",
488 LDAP_LOG( FILTER, INFO, "equality_candidates NULL\n", 0, 0, 0 );
490 Debug( LDAP_DEBUG_TRACE,
491 "<= equality_candidates NULL\n",
499 idl = idl_intersection( op->o_bd, idl, tmp );
503 if( idl == NULL ) break;
506 ber_bvarray_free_x( keys, op->o_tmpmemctx );
508 ldbm_cache_close( op->o_bd, db );
512 LDAP_LOG( FILTER, ENTRY,
513 "equality_candidates: return %ld\n",
514 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
516 Debug( LDAP_DEBUG_TRACE, "<= equality_candidates %ld\n",
517 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
526 AttributeAssertion *ava
535 struct berval prefix = {0, NULL};
536 struct berval *keys = NULL;
540 LDAP_LOG( FILTER, ENTRY, "approx_candidates: enter\n", 0, 0, 0 );
542 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
546 idl = idl_allids( op->o_bd );
548 rc = index_param( op->o_bd, ava->aa_desc, LDAP_FILTER_APPROX,
549 &dbname, &mask, &prefix );
551 if( rc != LDAP_SUCCESS ) {
553 LDAP_LOG( FILTER, ERR,
554 "approx_candidates: index_param returned %d\n", rc, 0, 0 );
556 Debug( LDAP_DEBUG_TRACE,
557 "<= approx_candidates: index_param returned=%d\n",
564 if( dbname == NULL ) {
567 LDAP_LOG( FILTER, ERR, "approx_candidates: not indexed\n", 0, 0, 0 );
569 Debug( LDAP_DEBUG_ANY,
570 "<= approx_candidates: not indexed\n",
577 mr = ava->aa_desc->ad_type->sat_approx;
579 /* no approx matching rule, try equality matching rule */
580 mr = ava->aa_desc->ad_type->sat_equality;
587 if( !mr->smr_filter ) {
591 rc = (mr->smr_filter)(
594 ava->aa_desc->ad_type->sat_syntax,
598 &keys, op->o_tmpmemctx );
600 if( rc != LDAP_SUCCESS ) {
602 LDAP_LOG( FILTER, ERR,
603 "approx_candidates: (%s%s) MR filter failed (%d)\n",
604 dbname, LDBM_SUFFIX, rc );
606 Debug( LDAP_DEBUG_TRACE,
607 "<= approx_candidates: (%s%s) MR filter failed (%d)\n",
608 dbname, LDBM_SUFFIX, rc );
616 LDAP_LOG( FILTER, INFO,
617 "approx_candidates: no keys (%s%s)\n",
618 dbname, LDBM_SUFFIX, 0 );
620 Debug( LDAP_DEBUG_TRACE,
621 "<= approx_candidates: no keys (%s%s)\n",
622 dbname, LDBM_SUFFIX, 0 );
628 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
632 LDAP_LOG( FILTER, ERR,
633 "approx_candidates db open failed (%s%s)\n",
634 dbname, LDBM_SUFFIX, 0 );
636 Debug( LDAP_DEBUG_ANY,
637 "<= approx_candidates db open failed (%s%s)\n",
638 dbname, LDBM_SUFFIX, 0 );
644 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
648 rc = key_read( op->o_bd, db, &keys[i], &tmp );
650 if( rc != LDAP_SUCCESS ) {
654 LDAP_LOG( FILTER, ERR,
655 "approx_candidates: key read failed (%d)\n", rc, 0, 0 );
657 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates key read failed (%d)\n",
668 LDAP_LOG( FILTER, INFO, "approx_candidates: NULL\n", 0, 0, 0 );
670 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
678 idl = idl_intersection( op->o_bd, idl, tmp );
682 if( idl == NULL ) break;
685 ber_bvarray_free_x( keys, op->o_tmpmemctx );
687 ldbm_cache_close( op->o_bd, db );
690 LDAP_LOG( FILTER, ENTRY,
691 "approx_candidates: return %ld\n",
692 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
694 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %ld\n",
695 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
708 ID_BLOCK *idl, *tmp, *tmp2;
712 LDAP_LOG( FILTER, ENTRY, "list_candidates: 0x%x\n", ftype, 0, 0 );
714 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
719 for ( f = flist; f != NULL; f = f->f_next ) {
720 if ( (tmp = filter_candidates( op, f )) == NULL &&
721 ftype == LDAP_FILTER_AND ) {
723 LDAP_LOG( FILTER, INFO, "list_candidates: NULL\n", 0, 0, 0 );
725 Debug( LDAP_DEBUG_TRACE,
726 "<= list_candidates NULL\n", 0, 0, 0 );
736 } else if ( ftype == LDAP_FILTER_AND ) {
737 idl = idl_intersection( op->o_bd, idl, tmp );
741 idl = idl_union( op->o_bd, idl, tmp );
748 LDAP_LOG( FILTER, ENTRY, "list_candidates: return %ld\n",
749 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
751 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %ld\n",
752 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
759 substring_candidates(
761 SubstringsAssertion *sub
770 struct berval prefix = {0, NULL};
771 struct berval *keys = NULL;
775 LDAP_LOG( FILTER, ENTRY, "substrings_candidates: enter\n", 0, 0, 0 );
777 Debug( LDAP_DEBUG_TRACE, "=> substrings_candidates\n", 0, 0, 0 );
781 idl = idl_allids( op->o_bd );
783 rc = index_param( op->o_bd, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
784 &dbname, &mask, &prefix );
786 if( rc != LDAP_SUCCESS ) {
788 LDAP_LOG( FILTER, ERR,
789 "substrings_candidates: index_param returned %d\n", rc, 0, 0 );
791 Debug( LDAP_DEBUG_TRACE,
792 "<= substrings_candidates: index_param returned=%d\n",
799 if( dbname == NULL ) {
802 LDAP_LOG( FILTER, ERR, "substrings_candidates: not indexed\n", 0, 0, 0);
804 Debug( LDAP_DEBUG_ANY,
805 "<= substrings_candidates: not indexed\n",
812 mr = sub->sa_desc->ad_type->sat_substr;
818 if( !mr->smr_filter ) {
822 rc = (mr->smr_filter)(
823 LDAP_FILTER_SUBSTRINGS,
825 sub->sa_desc->ad_type->sat_syntax,
829 &keys, op->o_tmpmemctx );
831 if( rc != LDAP_SUCCESS ) {
833 LDAP_LOG( FILTER, ERR,
834 "substrings_candidates: (%s%s) MR filter failed (%d)\n",
835 dbname, LDBM_SUFFIX, rc );
837 Debug( LDAP_DEBUG_TRACE,
838 "<= substrings_candidates: (%s%s) MR filter failed (%d)\n",
839 dbname, LDBM_SUFFIX, rc );
847 LDAP_LOG( FILTER, ERR,
848 "substrings_candidates: (0x%04lx) no keys (%s%s)\n",
849 mask, dbname, LDBM_SUFFIX );
851 Debug( LDAP_DEBUG_TRACE,
852 "<= substrings_candidates: (0x%04lx) no keys (%s%s)\n",
853 mask, dbname, LDBM_SUFFIX );
859 db = ldbm_cache_open( op->o_bd, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
863 LDAP_LOG( FILTER, ERR,
864 "substrings_candidates: db open failed (%s%s)\n",
865 dbname, LDBM_SUFFIX, 0 );
867 Debug( LDAP_DEBUG_ANY,
868 "<= substrings_candidates db open failed (%s%s)\n",
869 dbname, LDBM_SUFFIX, 0 );
875 for ( i= 0; keys[i].bv_val != NULL; i++ ) {
879 rc = key_read( op->o_bd, db, &keys[i], &tmp );
881 if( rc != LDAP_SUCCESS ) {
885 LDAP_LOG( FILTER, ERR,
886 "substrings_candidates: key read failed (%d)\n", rc, 0, 0 );
888 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates key read failed (%d)\n",
899 LDAP_LOG( FILTER, INFO, "substrings_candidates: NULL\n", 0, 0, 0 );
901 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates NULL\n",
909 idl = idl_intersection( op->o_bd, idl, tmp );
913 if( idl == NULL ) break;
916 ber_bvarray_free_x( keys, op->o_tmpmemctx );
918 ldbm_cache_close( op->o_bd, db );
921 LDAP_LOG( FILTER, ENTRY,
922 "substrings_candidates: return %ld\n",
923 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
925 Debug( LDAP_DEBUG_TRACE, "<= substrings_candidates %ld\n",
926 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );