1 /* search.c - ldbm backend search function */
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
14 static IDList *base_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
15 static IDList *onelevel_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
16 static IDList *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; \
45 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
54 char *rbuf, *rcur, *r;
58 Debug(LDAP_DEBUG_ARGS, "=> ldbm_back_search\n", 0, 0, 0);
60 if ( tlimit == 0 && be_isroot( be, op->o_dn ) ) {
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_dn ) ) {
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 = derefDN ( be, conn, op, base );
85 realBase = ch_strdup(base);
88 (void) dn_normalize (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 = idl_firstid( candidates ); id != NOID;
141 id = idl_nextid( candidates, id ) ) {
142 /* check for abandon */
143 pthread_mutex_lock( &op->o_abandonmutex );
144 if ( op->o_abandon ) {
145 pthread_mutex_unlock( &op->o_abandonmutex );
146 idl_free( candidates );
148 if( realBase != NULL) {
153 pthread_mutex_unlock( &op->o_abandonmutex );
155 /* check time limit */
156 pthread_mutex_lock( ¤ttime_mutex );
157 time( ¤ttime );
158 if ( tlimit != -1 && currenttime > stoptime ) {
159 pthread_mutex_unlock( ¤ttime_mutex );
160 send_ldap_search_result( conn, op,
161 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
163 idl_free( candidates );
165 if( realBase != NULL) {
170 pthread_mutex_unlock( ¤ttime_mutex );
172 /* get the entry with reader lock */
173 if ( (e = 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 ( e->e_dn != NULL &&
185 strncasecmp( e->e_dn, "ref=", 4 ) == 0 &&
186 (ref = attr_find( e->e_attrs, "ref" )) != NULL &&
187 scope == LDAP_SCOPE_SUBTREE )
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( dn );
219 scopeok = (dn == realBase) ? 1 : (! strcasecmp( dn, realBase ));
222 scopeok = (realBase == NULL || *realBase == '\0');
224 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
225 dn = ch_strdup( e->e_ndn );
226 scopeok = dn_issuffix( dn, realBase );
231 /* check size limit */
232 if ( --slimit == -1 ) {
233 cache_return_entry_r( &li->li_cache, e );
234 send_ldap_search_result( conn, op,
235 LDAP_SIZELIMIT_EXCEEDED, NULL,
236 nrefs > 0 ? rbuf : NULL, nentries );
237 idl_free( candidates );
240 if( realBase != NULL) {
247 * check and apply aliasing where the dereferencing applies to
248 * the subordinates of the base
251 case LDAP_DEREF_SEARCHING:
252 case LDAP_DEREF_ALWAYS:
254 Entry *newe = derefAlias_r( be, conn, op, e );
255 cache_return_entry_r( &li->li_cache, e );
261 switch ( send_search_entry( be, conn, op, e,
262 attrs, attrsonly ) ) {
263 case 0: /* entry sent ok */
266 case 1: /* entry not sent */
268 case -1: /* connection closed */
269 cache_return_entry_r( &li->li_cache, e );
270 idl_free( candidates );
273 if( realBase != NULL) {
283 /* free reader lock */
284 cache_return_entry_r( &li->li_cache, e );
289 idl_free( candidates );
291 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
294 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
299 if( realBase != NULL) {
319 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
325 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n", base, 0, 0);
329 /* get entry with reader lock */
330 if ( (e = dn2entry_r( be, base, matched )) == NULL ) {
331 *err = LDAP_NO_SUCH_OBJECT;
335 /* check for deleted */
337 idl = idl_alloc( 1 );
338 idl_insert( &idl, e->e_id, 1 );
341 /* free reader lock */
342 cache_return_entry_r( &li->li_cache, e );
360 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
366 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: \"%s\"\n", base, 0, 0);
370 /* get the base object with reader lock */
371 if ( base != NULL && *base != '\0' &&
372 (e = dn2entry_r( be, base, matched )) == NULL )
374 *err = LDAP_NO_SUCH_OBJECT;
379 * modify the filter to be something like this:
381 * parent=baseobject & originalfilter
384 f = (Filter *) ch_malloc( sizeof(Filter) );
386 f->f_choice = LDAP_FILTER_AND;
387 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
388 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
389 f->f_and->f_ava.ava_type = ch_strdup( "id2children" );
390 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
391 f->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
392 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
393 f->f_and->f_next = filter;
395 /* from here, it's just like subtree_candidates */
396 candidates = subtree_candidates( be, conn, op, base, f, attrs,
397 attrsonly, matched, e, err, 0 );
399 /* free up just the filter stuff we allocated above */
400 f->f_and->f_next = NULL;
403 /* free entry and reader lock */
405 cache_return_entry_r( &li->li_cache, e );
407 return( candidates );
425 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
426 Filter *f, **filterarg_ptr;
429 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" %s\n",
430 base ? base : "NULL", lookupbase ? "lookupbase" : "", 0);
433 * get the base object - unless we already have it (from one-level).
434 * also, unless this is a one-level search or a subtree search
435 * starting at the very top of our subtree, we need to modify the
436 * filter to be something like this:
438 * dn=*baseobjectdn & (originalfilter | ref=*)
440 * the "objectclass=referral" part is used to select referrals to return
448 if ( base != NULL && *base != '\0' &&
449 (e = dn2entry_r( be, base, matched )) == NULL )
451 *err = LDAP_NO_SUCH_OBJECT;
456 cache_return_entry_r( &li->li_cache, e );
459 f = (Filter *) ch_malloc( sizeof(Filter) );
461 f->f_choice = LDAP_FILTER_OR;
462 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
463 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
464 f->f_or->f_avtype = ch_strdup( "objectclass" );
465 /* Patch to use normalized uppercase */
466 f->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
467 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
468 filterarg_ptr = &f->f_or->f_next;
469 *filterarg_ptr = filter;
472 if ( ! be_issuffix( be, base ) ) {
473 f = (Filter *) ch_malloc( sizeof(Filter) );
475 f->f_choice = LDAP_FILTER_AND;
476 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
477 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
478 f->f_and->f_sub_type = ch_strdup( "dn" );
479 f->f_and->f_sub_initial = NULL;
480 f->f_and->f_sub_any = NULL;
481 f->f_and->f_sub_final = ch_strdup( base );
482 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
483 f->f_and->f_next = filter;
488 candidates = filter_candidates( be, filter );
490 /* free up just the parts we allocated above */
492 *filterarg_ptr = NULL;
496 return( candidates );