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:
83 realBase = derefDN ( be, conn, op, base );
86 realBase = ch_strdup(base);
89 (void) dn_normalize (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, "",
116 /* null candidates means we could not find the base object */
117 if ( candidates == NULL ) {
118 send_ldap_result( conn, op, err, matched, "" );
119 if ( matched != NULL ) {
128 MAKE_SPACE( sizeof("Referral:") + 1 );
129 strcpy( rbuf, "Referral:" );
130 rcur = strchr( rbuf, '\0' );
131 for ( id = idl_firstid( candidates ); id != NOID;
132 id = idl_nextid( candidates, id ) ) {
133 /* check for abandon */
134 pthread_mutex_lock( &op->o_abandonmutex );
135 if ( op->o_abandon ) {
136 pthread_mutex_unlock( &op->o_abandonmutex );
137 idl_free( candidates );
141 pthread_mutex_unlock( &op->o_abandonmutex );
143 /* check time limit */
144 pthread_mutex_lock( ¤ttime_mutex );
145 time( ¤ttime );
146 if ( tlimit != -1 && currenttime > stoptime ) {
147 pthread_mutex_unlock( ¤ttime_mutex );
148 send_ldap_search_result( conn, op,
149 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
151 idl_free( candidates );
155 pthread_mutex_unlock( ¤ttime_mutex );
157 /* get the entry with reader lock */
158 if ( (e = id2entry_r( be, id )) == NULL ) {
159 Debug( LDAP_DEBUG_ARGS, "candidate %lu not found\n",
165 * if it's a referral, add it to the list of referrals. only do
166 * this for subtree searches, and don't check the filter explicitly
167 * here since it's only a candidate anyway.
169 if ( e->e_dn != NULL &&
170 strncasecmp( e->e_dn, "ref=", 4 ) == 0 &&
171 (ref = attr_find( e->e_attrs, "ref" )) != NULL &&
172 scope == LDAP_SCOPE_SUBTREE )
176 if ( ref->a_vals == NULL ) {
177 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n",
180 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
181 /* referral + newline + null */
182 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
184 strncpy( rcur, ref->a_vals[i]->bv_val,
185 ref->a_vals[i]->bv_len );
186 rcur = rcur + ref->a_vals[i]->bv_len;
192 /* otherwise it's an entry - see if it matches the filter */
194 /* if it matches the filter and scope, send it */
195 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
201 if ( scope == LDAP_SCOPE_ONELEVEL ) {
202 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
203 (void) dn_normalize( dn );
204 scopeok = (dn == realBase) ? 1 : (! strcasecmp( dn, realBase ));
206 scopeok = (realBase == NULL || *realBase == '\0');
209 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
210 dn = ch_strdup( e->e_dn );
211 (void) dn_normalize( dn );
212 scopeok = dn_issuffix( dn, realBase );
217 /* check size limit */
218 if ( --slimit == -1 ) {
219 cache_return_entry_r( &li->li_cache, e );
220 send_ldap_search_result( conn, op,
221 LDAP_SIZELIMIT_EXCEEDED, NULL,
222 nrefs > 0 ? rbuf : NULL, nentries );
223 idl_free( candidates );
229 * check and apply aliasing where the dereferencing applies to
230 * the subordinates of the base
233 case LDAP_DEREF_SEARCHING:
234 case LDAP_DEREF_ALWAYS:
236 Entry *newe = derefAlias_r( be, conn, op, e );
237 cache_return_entry_r( &li->li_cache, e );
243 switch ( send_search_entry( be, conn, op, e,
244 attrs, attrsonly ) ) {
245 case 0: /* entry sent ok */
248 case 1: /* entry not sent */
250 case -1: /* connection closed */
251 cache_return_entry_r( &li->li_cache, e );
252 idl_free( candidates );
261 /* free reader lock */
262 cache_return_entry_r( &li->li_cache, e );
267 idl_free( candidates );
269 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
272 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
293 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
299 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: %s\n", base, 0, 0);
303 /* get entry with reader lock */
304 if ( (e = dn2entry_r( be, base, matched )) == NULL ) {
305 *err = LDAP_NO_SUCH_OBJECT;
309 /* check for deleted */
311 idl = idl_alloc( 1 );
312 idl_insert( &idl, e->e_id, 1 );
315 /* free reader lock */
316 cache_return_entry_r( &li->li_cache, e );
334 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
340 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: %s\n", base, 0, 0);
344 /* get the base object with reader lock */
345 if ( base != NULL && *base != '\0' &&
346 (e = dn2entry_r( be, base, matched )) == NULL )
348 *err = LDAP_NO_SUCH_OBJECT;
353 * modify the filter to be something like this:
355 * parent=baseobject & originalfilter
358 f = (Filter *) ch_malloc( sizeof(Filter) );
360 f->f_choice = LDAP_FILTER_AND;
361 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
362 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
363 f->f_and->f_ava.ava_type = ch_strdup( "id2children" );
364 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
365 f->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
366 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
367 f->f_and->f_next = filter;
369 /* from here, it's just like subtree_candidates */
370 candidates = subtree_candidates( be, conn, op, base, f, attrs,
371 attrsonly, matched, e, err, 0 );
373 /* free up just the filter stuff we allocated above */
374 f->f_and->f_next = NULL;
377 /* free entry and reader lock */
378 cache_return_entry_r( &li->li_cache, e );
379 return( candidates );
397 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
401 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: %s\n",
402 base ? base : "NULL", 0, 0);
405 * get the base object - unless we already have it (from one-level).
406 * also, unless this is a one-level search or a subtree search
407 * starting at the very top of our subtree, we need to modify the
408 * filter to be something like this:
410 * dn=*baseobjectdn & (originalfilter | ref=*)
412 * the "objectclass=referral" part is used to select referrals to return
418 if ( base != NULL && *base != '\0' &&
419 (e = dn2entry_r( be, base, matched )) == NULL )
421 *err = LDAP_NO_SUCH_OBJECT;
426 cache_return_entry_r( &li->li_cache, e );
429 f = (Filter *) ch_malloc( sizeof(Filter) );
431 f->f_choice = LDAP_FILTER_OR;
432 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
433 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
434 f->f_or->f_avtype = ch_strdup( "objectclass" );
435 /* Patch to use normalized uppercase */
436 f->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
437 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
438 f->f_or->f_next = filter;
441 if ( ! be_issuffix( be, base ) ) {
442 f = (Filter *) ch_malloc( sizeof(Filter) );
444 f->f_choice = LDAP_FILTER_AND;
445 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
446 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
447 f->f_and->f_sub_type = ch_strdup( "dn" );
448 f->f_and->f_sub_initial = NULL;
449 f->f_and->f_sub_any = NULL;
450 f->f_and->f_sub_final = ch_strdup( base );
451 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
452 f->f_and->f_next = filter;
457 candidates = filter_candidates( be, filter );
459 /* free up just the parts we allocated above */
461 f->f_and->f_next = NULL;
465 return( candidates );