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
79 #ifdef SLAPD_ALIAS_DEREF
81 case LDAP_DEREF_FINDING:
82 case LDAP_DEREF_ALWAYS:
83 realBase = derefDN ( be, conn, op, base );
86 realBase = ch_strdup(base);
89 realBase = ch_strdup(base);
92 (void) dn_normalize (realBase);
94 Debug( LDAP_DEBUG_TRACE, "using base \"%s\"\n",
99 candidates = base_candidates( be, conn, op, realBase, filter,
100 attrs, attrsonly, &matched, &err );
103 case LDAP_SCOPE_ONELEVEL:
104 candidates = onelevel_candidates( be, conn, op, realBase, filter,
105 attrs, attrsonly, &matched, &err );
108 case LDAP_SCOPE_SUBTREE:
109 candidates = subtree_candidates( be, conn, op, realBase, filter,
110 attrs, attrsonly, &matched, NULL, &err, 1 );
114 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, "",
116 if( realBase != NULL) {
122 /* null candidates means we could not find the base object */
123 if ( candidates == NULL ) {
124 send_ldap_result( conn, op, err, matched, "" );
125 if ( matched != NULL ) {
128 if( realBase != NULL) {
134 if ( matched != NULL ) {
141 MAKE_SPACE( sizeof("Referral:") + 1 );
142 strcpy( rbuf, "Referral:" );
143 rcur = strchr( rbuf, '\0' );
144 for ( id = idl_firstid( candidates ); id != NOID;
145 id = idl_nextid( candidates, id ) ) {
146 /* check for abandon */
147 pthread_mutex_lock( &op->o_abandonmutex );
148 if ( op->o_abandon ) {
149 pthread_mutex_unlock( &op->o_abandonmutex );
150 idl_free( candidates );
152 if( realBase != NULL) {
157 pthread_mutex_unlock( &op->o_abandonmutex );
159 /* check time limit */
160 pthread_mutex_lock( ¤ttime_mutex );
161 time( ¤ttime );
162 if ( tlimit != -1 && currenttime > stoptime ) {
163 pthread_mutex_unlock( ¤ttime_mutex );
164 send_ldap_search_result( conn, op,
165 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
167 idl_free( candidates );
169 if( realBase != NULL) {
174 pthread_mutex_unlock( ¤ttime_mutex );
176 /* get the entry with reader lock */
177 if ( (e = id2entry_r( be, id )) == NULL ) {
178 Debug( LDAP_DEBUG_ARGS, "candidate %lu not found\n",
184 * if it's a referral, add it to the list of referrals. only do
185 * this for subtree searches, and don't check the filter explicitly
186 * here since it's only a candidate anyway.
188 if ( e->e_dn != NULL &&
189 strncasecmp( e->e_dn, "ref=", 4 ) == 0 &&
190 (ref = attr_find( e->e_attrs, "ref" )) != NULL &&
191 scope == LDAP_SCOPE_SUBTREE )
195 if ( ref->a_vals == NULL ) {
196 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n",
199 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
200 /* referral + newline + null */
201 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
203 strncpy( rcur, ref->a_vals[i]->bv_val,
204 ref->a_vals[i]->bv_len );
205 rcur = rcur + ref->a_vals[i]->bv_len;
211 /* otherwise it's an entry - see if it matches the filter */
213 /* if it matches the filter and scope, send it */
214 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
220 if ( scope == LDAP_SCOPE_ONELEVEL ) {
221 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
222 (void) dn_normalize( dn );
223 scopeok = (dn == realBase) ? 1 : (! strcasecmp( dn, realBase ));
225 scopeok = (realBase == NULL || *realBase == '\0');
228 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
229 dn = ch_strdup( e->e_dn );
230 (void) dn_normalize( dn );
231 scopeok = dn_issuffix( dn, realBase );
236 /* check size limit */
237 if ( --slimit == -1 ) {
238 cache_return_entry_r( &li->li_cache, e );
239 send_ldap_search_result( conn, op,
240 LDAP_SIZELIMIT_EXCEEDED, NULL,
241 nrefs > 0 ? rbuf : NULL, nentries );
242 idl_free( candidates );
245 if( realBase != NULL) {
251 #ifdef SLAPD_ALIAS_DEREF
253 * check and apply aliasing where the dereferencing applies to
254 * the subordinates of the base
257 case LDAP_DEREF_SEARCHING:
258 case LDAP_DEREF_ALWAYS:
260 Entry *newe = derefAlias_r( be, conn, op, e );
261 cache_return_entry_r( &li->li_cache, e );
268 switch ( send_search_entry( be, conn, op, e,
269 attrs, attrsonly ) ) {
270 case 0: /* entry sent ok */
273 case 1: /* entry not sent */
275 case -1: /* connection closed */
276 cache_return_entry_r( &li->li_cache, e );
277 idl_free( candidates );
280 if( realBase != NULL) {
290 /* free reader lock */
291 cache_return_entry_r( &li->li_cache, e );
296 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) {
326 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
332 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n", base, 0, 0);
336 /* get entry with reader lock */
337 if ( (e = dn2entry_r( be, base, matched )) == NULL ) {
338 *err = LDAP_NO_SUCH_OBJECT;
342 /* check for deleted */
344 idl = idl_alloc( 1 );
345 idl_insert( &idl, e->e_id, 1 );
348 /* free reader lock */
349 cache_return_entry_r( &li->li_cache, e );
367 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
373 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: \"%s\"\n", base, 0, 0);
377 /* get the base object with reader lock */
378 if ( base != NULL && *base != '\0' &&
379 (e = dn2entry_r( be, base, matched )) == NULL )
381 *err = LDAP_NO_SUCH_OBJECT;
386 * modify the filter to be something like this:
388 * parent=baseobject & originalfilter
391 f = (Filter *) ch_malloc( sizeof(Filter) );
393 f->f_choice = LDAP_FILTER_AND;
394 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
395 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
396 f->f_and->f_ava.ava_type = ch_strdup( "id2children" );
397 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
398 f->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
399 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
400 f->f_and->f_next = filter;
402 /* from here, it's just like subtree_candidates */
403 candidates = subtree_candidates( be, conn, op, base, f, attrs,
404 attrsonly, matched, e, err, 0 );
406 /* free up just the filter stuff we allocated above */
407 f->f_and->f_next = NULL;
410 /* free entry and reader lock */
412 cache_return_entry_r( &li->li_cache, e );
414 return( candidates );
432 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
433 Filter *f, **filterarg_ptr;
436 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" %s\n",
437 base ? base : "NULL", lookupbase ? "lookupbase" : "", 0);
440 * get the base object - unless we already have it (from one-level).
441 * also, unless this is a one-level search or a subtree search
442 * starting at the very top of our subtree, we need to modify the
443 * filter to be something like this:
445 * dn=*baseobjectdn & (originalfilter | ref=*)
447 * the "objectclass=referral" part is used to select referrals to return
455 if ( base != NULL && *base != '\0' &&
456 (e = dn2entry_r( be, base, matched )) == NULL )
458 *err = LDAP_NO_SUCH_OBJECT;
463 cache_return_entry_r( &li->li_cache, e );
466 f = (Filter *) ch_malloc( sizeof(Filter) );
468 f->f_choice = LDAP_FILTER_OR;
469 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
470 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
471 f->f_or->f_avtype = ch_strdup( "objectclass" );
472 /* Patch to use normalized uppercase */
473 f->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
474 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
475 filterarg_ptr = &f->f_or->f_next;
476 *filterarg_ptr = filter;
479 if ( ! be_issuffix( be, base ) ) {
480 f = (Filter *) ch_malloc( sizeof(Filter) );
482 f->f_choice = LDAP_FILTER_AND;
483 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
484 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
485 f->f_and->f_sub_type = ch_strdup( "dn" );
486 f->f_and->f_sub_initial = NULL;
487 f->f_and->f_sub_any = NULL;
488 f->f_and->f_sub_final = ch_strdup( base );
489 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
490 f->f_and->f_next = filter;
495 candidates = filter_candidates( be, filter );
497 /* free up just the parts we allocated above */
499 *filterarg_ptr = NULL;
503 return( candidates );