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;
59 Debug(LDAP_DEBUG_ARGS, "=> bdb2i_back_search\n", 0, 0, 0);
61 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
62 tlimit = -1; /* allow root to set no limit */
64 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
65 be->be_timelimit : tlimit;
66 stoptime = op->o_time + tlimit;
68 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
69 slimit = -1; /* allow root to set no limit */
71 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
72 be->be_sizelimit : slimit;
76 * check and apply aliasing where the dereferencing applies to
77 * the subordinates of the base
81 case LDAP_DEREF_FINDING:
82 case LDAP_DEREF_ALWAYS:
83 realBase = bdb2i_derefDN ( be, conn, op, base );
86 realBase = ch_strdup(base);
89 (void) dn_normalize_case( realBase );
91 Debug( LDAP_DEBUG_TRACE, "using base \"%s\"\n",
96 candidates = base_candidates( be, conn, op, realBase, filter,
97 attrs, attrsonly, &matched, &err );
100 case LDAP_SCOPE_ONELEVEL:
101 candidates = onelevel_candidates( be, conn, op, realBase, filter,
102 attrs, attrsonly, &matched, &err );
105 case LDAP_SCOPE_SUBTREE:
106 candidates = subtree_candidates( be, conn, op, realBase, filter,
107 attrs, attrsonly, &matched, NULL, &err, 1 );
111 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, "",
113 if( realBase != NULL) {
119 /* null candidates means we could not find the base object */
120 if ( candidates == NULL ) {
121 send_ldap_result( conn, op, err, matched, "" );
122 if ( matched != NULL ) {
125 if( realBase != NULL) {
131 if ( matched != NULL ) {
138 MAKE_SPACE( sizeof("Referral:") + 1 );
139 strcpy( rbuf, "Referral:" );
140 rcur = strchr( rbuf, '\0' );
141 for ( id = bdb2i_idl_firstid( candidates ); id != NOID;
142 id = bdb2i_idl_nextid( candidates, id ) ) {
143 /* check for abandon */
144 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
145 if ( op->o_abandon ) {
146 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
147 bdb2i_idl_free( candidates );
149 if( realBase != NULL) {
154 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
156 /* check time limit */
157 if ( tlimit != -1 && slap_get_time() > stoptime ) {
158 send_ldap_search_result( conn, op,
159 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
161 bdb2i_idl_free( candidates );
163 if( realBase != NULL) {
169 /* get the entry with reader lock */
170 if ( (e = bdb2i_id2entry_r( be, id )) == NULL ) {
171 Debug( LDAP_DEBUG_ARGS, "candidate %ld not found\n",
177 * if it's a referral, add it to the list of referrals. only do
178 * this for subtree searches, and don't check the filter explicitly
179 * here since it's only a candidate anyway.
181 if ( scope == LDAP_SCOPE_SUBTREE &&
183 strncmp( e->e_ndn, "REF=", 4 ) == 0 &&
184 (ref = attr_find( e->e_attrs, "ref" )) != NULL )
188 if ( ref->a_vals == NULL ) {
189 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n",
192 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
193 /* referral + newline + null */
194 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
196 strncpy( rcur, ref->a_vals[i]->bv_val,
197 ref->a_vals[i]->bv_len );
198 rcur = rcur + ref->a_vals[i]->bv_len;
204 /* otherwise it's an entry - see if it matches the filter */
206 /* if it matches the filter and scope, send it */
207 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
213 if ( scope == LDAP_SCOPE_ONELEVEL ) {
214 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
215 (void) dn_normalize_case( dn );
216 scopeok = (dn == realBase)
218 : (strcmp( dn, realBase ) ? 0 : 1 );
221 scopeok = (realBase == NULL || *realBase == '\0');
223 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
224 dn = ch_strdup( e->e_ndn );
225 scopeok = dn_issuffix( dn, realBase );
230 /* check size limit */
231 if ( --slimit == -1 ) {
232 bdb2i_cache_return_entry_r( &li->li_cache, e );
233 send_ldap_search_result( conn, op,
234 LDAP_SIZELIMIT_EXCEEDED, NULL,
235 nrefs > 0 ? rbuf : NULL, nentries );
236 bdb2i_idl_free( candidates );
239 if( realBase != NULL) {
246 * check and apply aliasing where the dereferencing applies to
247 * the subordinates of the base
250 case LDAP_DEREF_SEARCHING:
251 case LDAP_DEREF_ALWAYS:
253 Entry *newe = bdb2i_derefAlias_r( be, conn, op, e );
254 if ( newe == NULL ) { /* problem with the alias */
255 bdb2i_cache_return_entry_r( &li->li_cache, e );
258 else if ( newe != e ) { /* reassign e */
259 bdb2i_cache_return_entry_r( &li->li_cache, e );
267 switch ( send_search_entry( be, conn, op, e,
268 attrs, attrsonly ) ) {
269 case 0: /* entry sent ok */
272 case 1: /* entry not sent */
274 case -1: /* connection closed */
275 bdb2i_cache_return_entry_r( &li->li_cache, e );
276 bdb2i_idl_free( candidates );
279 if( realBase != NULL) {
290 /* free reader lock */
291 bdb2i_cache_return_entry_r( &li->li_cache, e );
294 ldap_pvt_thread_yield();
296 bdb2i_idl_free( candidates );
298 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
301 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
306 if( realBase != NULL) {
331 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
332 struct timeval time1;
335 bdb2i_start_timing( be->bd_info, &time1 );
337 if ( bdb2i_enter_backend_r( &lock ) != 0 ) {
339 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
344 ret = bdb2i_back_search_internal( be, conn, op, base, scope, deref,
345 slimit, tlimit, filter, filterstr, attrs, attrsonly );
347 (void) bdb2i_leave_backend_r( lock );
348 bdb2i_stop_timing( be->bd_info, time1, "SRCH", conn, op );
367 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
371 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n", base, 0, 0);
375 /* get entry with reader lock */
376 if ( (e = bdb2i_dn2entry_r( be, base, matched )) == NULL ) {
377 *err = LDAP_NO_SUCH_OBJECT;
381 /* check for deleted */
383 idl = bdb2i_idl_alloc( 1 );
384 bdb2i_idl_insert( &idl, e->e_id, 1 );
387 /* free reader lock */
388 bdb2i_cache_return_entry_r( &li->li_cache, e );
406 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
410 ID_BLOCK *candidates;
412 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: \"%s\"\n", base, 0, 0);
416 /* get the base object with reader lock */
417 if ( base != NULL && *base != '\0' &&
418 (e = bdb2i_dn2entry_r( be, base, matched )) == NULL )
420 *err = LDAP_NO_SUCH_OBJECT;
425 * modify the filter to be something like this:
427 * parent=baseobject & originalfilter
430 f = (Filter *) ch_malloc( sizeof(Filter) );
432 f->f_choice = LDAP_FILTER_AND;
433 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
434 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
435 f->f_and->f_ava.ava_type = ch_strdup( "id2children" );
436 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
437 f->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
438 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
439 f->f_and->f_next = filter;
441 /* from here, it's just like subtree_candidates */
442 candidates = subtree_candidates( be, conn, op, base, f, attrs,
443 attrsonly, matched, e, err, 0 );
445 /* free up just the filter stuff we allocated above */
446 f->f_and->f_next = NULL;
449 /* free entry and reader lock */
451 bdb2i_cache_return_entry_r( &li->li_cache, e );
453 return( candidates );
471 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
472 Filter *f, **filterarg_ptr;
473 ID_BLOCK *candidates;
475 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" %s\n",
476 base ? base : "NULL", lookupbase ? "lookupbase" : "", 0);
479 * get the base object - unless we already have it (from one-level).
480 * also, unless this is a one-level search or a subtree search
481 * starting at the very top of our subtree, we need to modify the
482 * filter to be something like this:
484 * dn=*baseobjectdn & (originalfilter | ref=*)
486 * the "objectclass=referral" part is used to select referrals to return
494 if ( base != NULL && *base != '\0' &&
495 (e = bdb2i_dn2entry_r( be, base, matched )) == NULL )
497 *err = LDAP_NO_SUCH_OBJECT;
502 bdb2i_cache_return_entry_r( &li->li_cache, e );
505 f = (Filter *) ch_malloc( sizeof(Filter) );
507 f->f_choice = LDAP_FILTER_OR;
508 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
509 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
510 f->f_or->f_avtype = ch_strdup( "objectclass" );
511 /* Patch to use normalized uppercase */
512 f->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
513 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
514 filterarg_ptr = &f->f_or->f_next;
515 *filterarg_ptr = filter;
518 if ( ! be_issuffix( be, base ) ) {
519 f = (Filter *) ch_malloc( sizeof(Filter) );
521 f->f_choice = LDAP_FILTER_AND;
522 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
523 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
524 f->f_and->f_sub_type = ch_strdup( "dn" );
525 f->f_and->f_sub_initial = NULL;
526 f->f_and->f_sub_any = NULL;
527 f->f_and->f_sub_final = ch_strdup( base );
528 value_normalize( f->f_and->f_sub_final, SYNTAX_DN );
529 f->f_and->f_next = filter;
534 candidates = bdb2i_filter_candidates( be, filter );
536 /* free up just the parts we allocated above */
538 *filterarg_ptr = NULL;
542 return( candidates );