1 /* search.c - bdb2 backend search function */
11 #include "back-bdb2.h"
12 #include "proto-back-bdb2.h"
14 static ID_BLOCK *base_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
15 static ID_BLOCK *onelevel_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
16 static ID_BLOCK *subtree_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, Entry *e, int *err, int lookupbase);
18 #define GRABSIZE BUFSIZ
20 #define MAKE_SPACE( n ) { \
21 if ( rcur + n > rbuf + rmaxsize ) { \
22 int offset = rcur - rbuf; \
23 rbuf = ch_realloc( rbuf, rmaxsize + GRABSIZE ); \
24 rmaxsize += GRABSIZE; \
25 rcur = rbuf + offset; \
30 bdb2i_back_search_internal(
45 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
54 char *rbuf, *rcur, *r;
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, "",
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, "" );
121 if ( matched != NULL ) {
124 if( realBase != NULL) {
130 if ( matched != NULL ) {
137 MAKE_SPACE( sizeof("Referral:") + 1 );
138 strcpy( rbuf, "Referral:" );
139 rcur = strchr( rbuf, '\0' );
140 for ( id = bdb2i_idl_firstid( candidates ); id != NOID;
141 id = bdb2i_idl_nextid( candidates, id ) ) {
142 /* check for abandon */
143 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
144 if ( op->o_abandon ) {
145 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
146 bdb2i_idl_free( candidates );
148 if( realBase != NULL) {
153 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
155 /* check time limit */
156 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
157 time( ¤ttime );
158 if ( tlimit != -1 && currenttime > stoptime ) {
159 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
160 send_ldap_search_result( conn, op,
161 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
163 bdb2i_idl_free( candidates );
165 if( realBase != NULL) {
170 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
172 /* get the entry with reader lock */
173 if ( (e = bdb2i_id2entry_r( be, id )) == NULL ) {
174 Debug( LDAP_DEBUG_ARGS, "candidate %lu not found\n",
180 * if it's a referral, add it to the list of referrals. only do
181 * this for subtree searches, and don't check the filter explicitly
182 * here since it's only a candidate anyway.
184 if ( scope == LDAP_SCOPE_SUBTREE &&
186 strncmp( e->e_ndn, "REF=", 4 ) == 0 &&
187 (ref = attr_find( e->e_attrs, "ref" )) != NULL )
191 if ( ref->a_vals == NULL ) {
192 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n",
195 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
196 /* referral + newline + null */
197 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
199 strncpy( rcur, ref->a_vals[i]->bv_val,
200 ref->a_vals[i]->bv_len );
201 rcur = rcur + ref->a_vals[i]->bv_len;
207 /* otherwise it's an entry - see if it matches the filter */
209 /* if it matches the filter and scope, send it */
210 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
216 if ( scope == LDAP_SCOPE_ONELEVEL ) {
217 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
218 (void) dn_normalize_case( dn );
219 scopeok = (dn == realBase)
221 : (strcmp( dn, realBase ) ? 0 : 1 );
224 scopeok = (realBase == NULL || *realBase == '\0');
226 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
227 dn = ch_strdup( e->e_ndn );
228 scopeok = dn_issuffix( dn, realBase );
233 /* check size limit */
234 if ( --slimit == -1 ) {
235 bdb2i_cache_return_entry_r( &li->li_cache, e );
236 send_ldap_search_result( conn, op,
237 LDAP_SIZELIMIT_EXCEEDED, NULL,
238 nrefs > 0 ? rbuf : NULL, nentries );
239 bdb2i_idl_free( candidates );
242 if( realBase != NULL) {
249 * check and apply aliasing where the dereferencing applies to
250 * the subordinates of the base
253 case LDAP_DEREF_SEARCHING:
254 case LDAP_DEREF_ALWAYS:
256 Entry *newe = bdb2i_derefAlias_r( be, conn, op, e );
257 if ( newe == NULL ) { /* problem with the alias */
258 bdb2i_cache_return_entry_r( &li->li_cache, e );
261 else if ( newe != e ) { /* reassign e */
262 bdb2i_cache_return_entry_r( &li->li_cache, e );
270 switch ( send_search_entry( be, conn, op, e,
271 attrs, attrsonly ) ) {
272 case 0: /* entry sent ok */
275 case 1: /* entry not sent */
277 case -1: /* connection closed */
278 bdb2i_cache_return_entry_r( &li->li_cache, e );
279 bdb2i_idl_free( candidates );
282 if( realBase != NULL) {
293 /* free reader lock */
294 bdb2i_cache_return_entry_r( &li->li_cache, e );
297 ldap_pvt_thread_yield();
299 bdb2i_idl_free( candidates );
301 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
304 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
309 if( realBase != NULL) {
334 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
336 struct timeval time1, time2;
340 gettimeofday( &time1, NULL );
342 if ( bdb2i_enter_backend_r( &li->li_db_env, &lock ) != 0 ) {
344 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
349 ret = bdb2i_back_search_internal( be, conn, op, base, scope, deref,
350 slimit, tlimit, filter, filterstr, attrs, attrsonly );
352 (void) bdb2i_leave_backend( &li->li_db_env, lock );
354 if ( bdb2i_do_timing ) {
356 gettimeofday( &time2, NULL);
357 elapsed_time = bdb2i_elapsed( time1, time2 );
358 Debug( LDAP_DEBUG_ANY, "conn=%d op=%d SRCH elapsed=%s\n",
359 conn->c_connid, op->o_opid, elapsed_time );
360 free( elapsed_time );
381 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
387 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n", base, 0, 0);
391 /* get entry with reader lock */
392 if ( (e = bdb2i_dn2entry_r( be, base, matched )) == NULL ) {
393 *err = LDAP_NO_SUCH_OBJECT;
397 /* check for deleted */
399 idl = bdb2i_idl_alloc( 1 );
400 bdb2i_idl_insert( &idl, e->e_id, 1 );
403 /* free reader lock */
404 bdb2i_cache_return_entry_r( &li->li_cache, e );
422 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
426 ID_BLOCK *candidates;
428 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: \"%s\"\n", base, 0, 0);
432 /* get the base object with reader lock */
433 if ( base != NULL && *base != '\0' &&
434 (e = bdb2i_dn2entry_r( be, base, matched )) == NULL )
436 *err = LDAP_NO_SUCH_OBJECT;
441 * modify the filter to be something like this:
443 * parent=baseobject & originalfilter
446 f = (Filter *) ch_malloc( sizeof(Filter) );
448 f->f_choice = LDAP_FILTER_AND;
449 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
450 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
451 f->f_and->f_ava.ava_type = ch_strdup( "id2children" );
452 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
453 f->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
454 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
455 f->f_and->f_next = filter;
457 /* from here, it's just like subtree_candidates */
458 candidates = subtree_candidates( be, conn, op, base, f, attrs,
459 attrsonly, matched, e, err, 0 );
461 /* free up just the filter stuff we allocated above */
462 f->f_and->f_next = NULL;
465 /* free entry and reader lock */
467 bdb2i_cache_return_entry_r( &li->li_cache, e );
469 return( candidates );
487 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
488 Filter *f, **filterarg_ptr;
489 ID_BLOCK *candidates;
491 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" %s\n",
492 base ? base : "NULL", lookupbase ? "lookupbase" : "", 0);
495 * get the base object - unless we already have it (from one-level).
496 * also, unless this is a one-level search or a subtree search
497 * starting at the very top of our subtree, we need to modify the
498 * filter to be something like this:
500 * dn=*baseobjectdn & (originalfilter | ref=*)
502 * the "objectclass=referral" part is used to select referrals to return
510 if ( base != NULL && *base != '\0' &&
511 (e = bdb2i_dn2entry_r( be, base, matched )) == NULL )
513 *err = LDAP_NO_SUCH_OBJECT;
518 bdb2i_cache_return_entry_r( &li->li_cache, e );
521 f = (Filter *) ch_malloc( sizeof(Filter) );
523 f->f_choice = LDAP_FILTER_OR;
524 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
525 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
526 f->f_or->f_avtype = ch_strdup( "objectclass" );
527 /* Patch to use normalized uppercase */
528 f->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
529 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
530 filterarg_ptr = &f->f_or->f_next;
531 *filterarg_ptr = filter;
534 if ( ! be_issuffix( be, base ) ) {
535 f = (Filter *) ch_malloc( sizeof(Filter) );
537 f->f_choice = LDAP_FILTER_AND;
538 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
539 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
540 f->f_and->f_sub_type = ch_strdup( "dn" );
541 f->f_and->f_sub_initial = NULL;
542 f->f_and->f_sub_any = NULL;
543 f->f_and->f_sub_final = ch_strdup( base );
544 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
545 f->f_and->f_next = filter;
550 candidates = bdb2i_filter_candidates( be, filter );
552 /* free up just the parts we allocated above */
554 *filterarg_ptr = NULL;
558 return( candidates );