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 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
158 time( ¤ttime );
159 if ( tlimit != -1 && currenttime > stoptime ) {
160 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
161 send_ldap_search_result( conn, op,
162 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
164 bdb2i_idl_free( candidates );
166 if( realBase != NULL) {
171 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
173 /* get the entry with reader lock */
174 if ( (e = bdb2i_id2entry_r( be, id )) == NULL ) {
175 Debug( LDAP_DEBUG_ARGS, "candidate %ld not found\n",
181 * if it's a referral, add it to the list of referrals. only do
182 * this for subtree searches, and don't check the filter explicitly
183 * here since it's only a candidate anyway.
185 if ( scope == LDAP_SCOPE_SUBTREE &&
187 strncmp( e->e_ndn, "REF=", 4 ) == 0 &&
188 (ref = attr_find( e->e_attrs, "ref" )) != NULL )
192 if ( ref->a_vals == NULL ) {
193 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n",
196 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
197 /* referral + newline + null */
198 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
200 strncpy( rcur, ref->a_vals[i]->bv_val,
201 ref->a_vals[i]->bv_len );
202 rcur = rcur + ref->a_vals[i]->bv_len;
208 /* otherwise it's an entry - see if it matches the filter */
210 /* if it matches the filter and scope, send it */
211 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
217 if ( scope == LDAP_SCOPE_ONELEVEL ) {
218 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
219 (void) dn_normalize_case( dn );
220 scopeok = (dn == realBase)
222 : (strcmp( dn, realBase ) ? 0 : 1 );
225 scopeok = (realBase == NULL || *realBase == '\0');
227 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
228 dn = ch_strdup( e->e_ndn );
229 scopeok = dn_issuffix( dn, realBase );
234 /* check size limit */
235 if ( --slimit == -1 ) {
236 bdb2i_cache_return_entry_r( &li->li_cache, e );
237 send_ldap_search_result( conn, op,
238 LDAP_SIZELIMIT_EXCEEDED, NULL,
239 nrefs > 0 ? rbuf : NULL, nentries );
240 bdb2i_idl_free( candidates );
243 if( realBase != NULL) {
250 * check and apply aliasing where the dereferencing applies to
251 * the subordinates of the base
254 case LDAP_DEREF_SEARCHING:
255 case LDAP_DEREF_ALWAYS:
257 Entry *newe = bdb2i_derefAlias_r( be, conn, op, e );
258 if ( newe == NULL ) { /* problem with the alias */
259 bdb2i_cache_return_entry_r( &li->li_cache, e );
262 else if ( newe != e ) { /* reassign e */
263 bdb2i_cache_return_entry_r( &li->li_cache, e );
271 switch ( send_search_entry( be, conn, op, e,
272 attrs, attrsonly ) ) {
273 case 0: /* entry sent ok */
276 case 1: /* entry not sent */
278 case -1: /* connection closed */
279 bdb2i_cache_return_entry_r( &li->li_cache, e );
280 bdb2i_idl_free( candidates );
283 if( realBase != NULL) {
294 /* free reader lock */
295 bdb2i_cache_return_entry_r( &li->li_cache, e );
298 ldap_pvt_thread_yield();
300 bdb2i_idl_free( candidates );
302 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
305 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
310 if( realBase != NULL) {
335 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
336 struct timeval time1;
339 bdb2i_start_timing( be->bd_info, &time1 );
341 if ( bdb2i_enter_backend_r( &lock ) != 0 ) {
343 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
348 ret = bdb2i_back_search_internal( be, conn, op, base, scope, deref,
349 slimit, tlimit, filter, filterstr, attrs, attrsonly );
351 (void) bdb2i_leave_backend_r( lock );
352 bdb2i_stop_timing( be->bd_info, time1, "SRCH", conn, op );
371 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
375 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n", base, 0, 0);
379 /* get entry with reader lock */
380 if ( (e = bdb2i_dn2entry_r( be, base, matched )) == NULL ) {
381 *err = LDAP_NO_SUCH_OBJECT;
385 /* check for deleted */
387 idl = bdb2i_idl_alloc( 1 );
388 bdb2i_idl_insert( &idl, e->e_id, 1 );
391 /* free reader lock */
392 bdb2i_cache_return_entry_r( &li->li_cache, e );
410 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
414 ID_BLOCK *candidates;
416 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: \"%s\"\n", base, 0, 0);
420 /* get the base object with reader lock */
421 if ( base != NULL && *base != '\0' &&
422 (e = bdb2i_dn2entry_r( be, base, matched )) == NULL )
424 *err = LDAP_NO_SUCH_OBJECT;
429 * modify the filter to be something like this:
431 * parent=baseobject & originalfilter
434 f = (Filter *) ch_malloc( sizeof(Filter) );
436 f->f_choice = LDAP_FILTER_AND;
437 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
438 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
439 f->f_and->f_ava.ava_type = ch_strdup( "id2children" );
440 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
441 f->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
442 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
443 f->f_and->f_next = filter;
445 /* from here, it's just like subtree_candidates */
446 candidates = subtree_candidates( be, conn, op, base, f, attrs,
447 attrsonly, matched, e, err, 0 );
449 /* free up just the filter stuff we allocated above */
450 f->f_and->f_next = NULL;
453 /* free entry and reader lock */
455 bdb2i_cache_return_entry_r( &li->li_cache, e );
457 return( candidates );
475 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
476 Filter *f, **filterarg_ptr;
477 ID_BLOCK *candidates;
479 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" %s\n",
480 base ? base : "NULL", lookupbase ? "lookupbase" : "", 0);
483 * get the base object - unless we already have it (from one-level).
484 * also, unless this is a one-level search or a subtree search
485 * starting at the very top of our subtree, we need to modify the
486 * filter to be something like this:
488 * dn=*baseobjectdn & (originalfilter | ref=*)
490 * the "objectclass=referral" part is used to select referrals to return
498 if ( base != NULL && *base != '\0' &&
499 (e = bdb2i_dn2entry_r( be, base, matched )) == NULL )
501 *err = LDAP_NO_SUCH_OBJECT;
506 bdb2i_cache_return_entry_r( &li->li_cache, e );
509 f = (Filter *) ch_malloc( sizeof(Filter) );
511 f->f_choice = LDAP_FILTER_OR;
512 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
513 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
514 f->f_or->f_avtype = ch_strdup( "objectclass" );
515 /* Patch to use normalized uppercase */
516 f->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
517 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
518 filterarg_ptr = &f->f_or->f_next;
519 *filterarg_ptr = filter;
522 if ( ! be_issuffix( be, base ) ) {
523 f = (Filter *) ch_malloc( sizeof(Filter) );
525 f->f_choice = LDAP_FILTER_AND;
526 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
527 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
528 f->f_and->f_sub_type = ch_strdup( "dn" );
529 f->f_and->f_sub_initial = NULL;
530 f->f_and->f_sub_any = NULL;
531 f->f_and->f_sub_final = ch_strdup( base );
532 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
533 f->f_and->f_next = filter;
538 candidates = bdb2i_filter_candidates( be, filter );
540 /* free up just the parts we allocated above */
542 *filterarg_ptr = NULL;
546 return( candidates );