1 /* search.c - bdb2 backend search function */
12 #include "back-bdb2.h"
13 #include "proto-back-bdb2.h"
15 static ID_BLOCK *base_candidates(BackendDB *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
16 static ID_BLOCK *onelevel_candidates(BackendDB *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
17 static ID_BLOCK *subtree_candidates(BackendDB *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, Entry *e, int *err, int lookupbase);
19 #define GRABSIZE BUFSIZ
21 #define MAKE_SPACE( n ) { \
22 if ( rcur + (n) > rbuf + rmaxsize ) { \
23 int offset = rcur - rbuf; \
24 rbuf = ch_realloc( rbuf, rmaxsize + GRABSIZE ); \
25 rmaxsize += GRABSIZE; \
26 rcur = rbuf + offset; \
31 bdb2i_back_search_internal(
46 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
58 Debug(LDAP_DEBUG_ARGS, "=> bdb2i_back_search\n", 0, 0, 0);
60 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
61 tlimit = -1; /* allow root to set no limit */
63 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
64 be->be_timelimit : tlimit;
65 stoptime = op->o_time + tlimit;
67 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
68 slimit = -1; /* allow root to set no limit */
70 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
71 be->be_sizelimit : slimit;
75 * check and apply aliasing where the dereferencing applies to
76 * the subordinates of the base
80 case LDAP_DEREF_FINDING:
81 case LDAP_DEREF_ALWAYS:
82 realBase = bdb2i_derefDN ( be, conn, op, base );
85 realBase = ch_strdup(base);
88 (void) dn_normalize_case( realBase );
90 Debug( LDAP_DEBUG_TRACE, "using base \"%s\"\n",
95 candidates = base_candidates( be, conn, op, realBase, filter,
96 attrs, attrsonly, &matched, &err );
99 case LDAP_SCOPE_ONELEVEL:
100 candidates = onelevel_candidates( be, conn, op, realBase, filter,
101 attrs, attrsonly, &matched, &err );
104 case LDAP_SCOPE_SUBTREE:
105 candidates = subtree_candidates( be, conn, op, realBase, filter,
106 attrs, attrsonly, &matched, NULL, &err, 1 );
110 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
111 NULL, "Bad scope", NULL );
112 if( realBase != NULL) {
118 /* null candidates means we could not find the base object */
119 if ( candidates == NULL ) {
120 send_ldap_result( conn, op, err, matched, NULL, NULL );
121 if ( matched != NULL ) {
124 if( realBase != NULL) {
130 if ( matched != NULL ) {
136 for ( id = bdb2i_idl_firstid( candidates ); id != NOID;
137 id = bdb2i_idl_nextid( candidates, id ) ) {
138 /* check for abandon */
139 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
140 if ( op->o_abandon ) {
141 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
142 bdb2i_idl_free( candidates );
143 ber_bvecfree( refs );
144 if( realBase != NULL) {
149 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
151 /* check time limit */
152 if ( tlimit != -1 && slap_get_time() > stoptime ) {
153 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
154 NULL, NULL, refs, nentries );
155 bdb2i_idl_free( candidates );
156 ber_bvecfree( refs );
157 if( realBase != NULL) {
163 /* get the entry with reader lock */
164 if ( (e = bdb2i_id2entry_r( be, id )) == NULL ) {
165 Debug( LDAP_DEBUG_ARGS, "candidate %ld not found\n",
171 * if it's a referral, add it to the list of referrals. only do
172 * this for subtree searches, and don't check the filter explicitly
173 * here since it's only a candidate anyway.
175 if ( scope == LDAP_SCOPE_SUBTREE &&
177 strncmp( e->e_ndn, "REF=", 4 ) == 0 &&
178 (ref = attr_find( e->e_attrs, "ref" )) != NULL )
180 send_search_reference( be, conn, op,
181 e, ref->a_vals, &refs );
183 /* otherwise it's an entry - see if it matches the filter */
185 /* if it matches the filter and scope, send it */
186 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
192 if ( scope == LDAP_SCOPE_ONELEVEL ) {
193 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
194 (void) dn_normalize_case( dn );
195 scopeok = (dn == realBase)
197 : (strcmp( dn, realBase ) ? 0 : 1 );
200 scopeok = (realBase == NULL || *realBase == '\0');
202 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
203 dn = ch_strdup( e->e_ndn );
204 scopeok = dn_issuffix( dn, realBase );
209 /* check size limit */
210 if ( --slimit == -1 ) {
211 bdb2i_cache_return_entry_r( &li->li_cache, e );
212 send_search_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
213 NULL, NULL, refs, nentries );
214 bdb2i_idl_free( candidates );
215 ber_bvecfree( refs );
217 if( realBase != NULL) {
224 * check and apply aliasing where the dereferencing applies to
225 * the subordinates of the base
228 case LDAP_DEREF_SEARCHING:
229 case LDAP_DEREF_ALWAYS:
231 Entry *newe = bdb2i_derefAlias_r( be, conn, op, e );
232 if ( newe == NULL ) { /* problem with the alias */
233 bdb2i_cache_return_entry_r( &li->li_cache, e );
236 else if ( newe != e ) { /* reassign e */
237 bdb2i_cache_return_entry_r( &li->li_cache, e );
245 switch ( send_search_entry( be, conn, op, e,
246 attrs, attrsonly, 0 ) ) {
247 case 0: /* entry sent ok */
250 case 1: /* entry not sent */
252 case -1: /* connection closed */
253 bdb2i_cache_return_entry_r( &li->li_cache, e );
254 bdb2i_idl_free( candidates );
255 ber_bvecfree( refs );
257 if( realBase != NULL) {
268 /* free reader lock */
269 bdb2i_cache_return_entry_r( &li->li_cache, e );
272 ldap_pvt_thread_yield();
274 bdb2i_idl_free( candidates );
276 send_search_result( conn, op,
277 refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
278 NULL, NULL, refs, nentries );
280 ber_bvecfree( refs );
282 if( realBase != NULL) {
307 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
308 struct timeval time1;
311 bdb2i_start_timing( be->bd_info, &time1 );
313 if ( bdb2i_enter_backend_r( &lock ) != 0 ) {
314 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
320 ret = bdb2i_back_search_internal( be, conn, op, base, scope, deref,
321 slimit, tlimit, filter, filterstr, attrs, attrsonly );
323 (void) bdb2i_leave_backend_r( lock );
324 bdb2i_stop_timing( be->bd_info, time1, "SRCH", conn, op );
343 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
347 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n", base, 0, 0);
351 /* get entry with reader lock */
352 if ( (e = bdb2i_dn2entry_r( be, base, matched )) == NULL ) {
353 *err = LDAP_NO_SUCH_OBJECT;
357 /* check for deleted */
359 idl = bdb2i_idl_alloc( 1 );
360 bdb2i_idl_insert( &idl, e->e_id, 1 );
363 /* free reader lock */
364 bdb2i_cache_return_entry_r( &li->li_cache, e );
382 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
386 ID_BLOCK *candidates;
388 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: \"%s\"\n", base, 0, 0);
392 /* get the base object with reader lock */
393 if ( base != NULL && *base != '\0' &&
394 (e = bdb2i_dn2entry_r( be, base, matched )) == NULL )
396 *err = LDAP_NO_SUCH_OBJECT;
401 * modify the filter to be something like this:
403 * parent=baseobject & originalfilter
406 f = (Filter *) ch_malloc( sizeof(Filter) );
408 f->f_choice = LDAP_FILTER_AND;
409 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
410 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
411 f->f_and->f_ava.ava_type = ch_strdup( "id2children" );
412 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
413 f->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
414 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
415 f->f_and->f_next = filter;
417 /* from here, it's just like subtree_candidates */
418 candidates = subtree_candidates( be, conn, op, base, f, attrs,
419 attrsonly, matched, e, err, 0 );
421 /* free up just the filter stuff we allocated above */
422 f->f_and->f_next = NULL;
425 /* free entry and reader lock */
427 bdb2i_cache_return_entry_r( &li->li_cache, e );
429 return( candidates );
447 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
448 Filter *f, **filterarg_ptr;
449 ID_BLOCK *candidates;
451 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" %s\n",
452 base ? base : "NULL", lookupbase ? "lookupbase" : "", 0);
455 * get the base object - unless we already have it (from one-level).
456 * also, unless this is a one-level search or a subtree search
457 * starting at the very top of our subtree, we need to modify the
458 * filter to be something like this:
460 * dn=*baseobjectdn & (originalfilter | ref=*)
462 * the "objectclass=referral" part is used to select referrals to return
470 if ( base != NULL && *base != '\0' &&
471 (e = bdb2i_dn2entry_r( be, base, matched )) == NULL )
473 *err = LDAP_NO_SUCH_OBJECT;
478 bdb2i_cache_return_entry_r( &li->li_cache, e );
481 f = (Filter *) ch_malloc( sizeof(Filter) );
483 f->f_choice = LDAP_FILTER_OR;
484 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
485 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
486 f->f_or->f_avtype = ch_strdup( "objectclass" );
487 /* Patch to use normalized uppercase */
488 f->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
489 f->f_or->f_avvalue.bv_len = sizeof( "REFERRAL" )-1;
490 filterarg_ptr = &f->f_or->f_next;
491 *filterarg_ptr = filter;
494 if ( ! be_issuffix( be, base ) ) {
495 f = (Filter *) ch_malloc( sizeof(Filter) );
497 f->f_choice = LDAP_FILTER_AND;
498 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
499 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
500 f->f_and->f_sub_type = ch_strdup( "dn" );
501 f->f_and->f_sub_initial = NULL;
502 f->f_and->f_sub_any = NULL;
503 f->f_and->f_sub_final = ch_strdup( base );
504 value_normalize( f->f_and->f_sub_final, SYNTAX_DN|SYNTAX_CIS );
505 f->f_and->f_next = filter;
510 candidates = bdb2i_filter_candidates( be, filter );
512 /* free up just the parts we allocated above */
514 *filterarg_ptr = NULL;
518 return( candidates );