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_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
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_case( 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 ( scope == LDAP_SCOPE_SUBTREE &&
190 strncmp( e->e_ndn, "REF=", 4 ) == 0 &&
191 (ref = attr_find( e->e_attrs, "ref" )) != NULL )
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_case( dn );
223 scopeok = (dn == realBase)
225 : (strcmp( dn, realBase ) ? 0 : 1 );
228 scopeok = (realBase == NULL || *realBase == '\0');
230 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
231 dn = ch_strdup( e->e_ndn );
232 scopeok = dn_issuffix( dn, realBase );
237 /* check size limit */
238 if ( --slimit == -1 ) {
239 cache_return_entry_r( &li->li_cache, e );
240 send_ldap_search_result( conn, op,
241 LDAP_SIZELIMIT_EXCEEDED, NULL,
242 nrefs > 0 ? rbuf : NULL, nentries );
243 idl_free( candidates );
246 if( realBase != NULL) {
252 #ifdef SLAPD_ALIAS_DEREF
254 * check and apply aliasing where the dereferencing applies to
255 * the subordinates of the base
258 case LDAP_DEREF_SEARCHING:
259 case LDAP_DEREF_ALWAYS:
261 Entry *newe = derefAlias_r( be, conn, op, e );
262 cache_return_entry_r( &li->li_cache, e );
269 switch ( send_search_entry( be, conn, op, e,
270 attrs, attrsonly ) ) {
271 case 0: /* entry sent ok */
274 case 1: /* entry not sent */
276 case -1: /* connection closed */
277 cache_return_entry_r( &li->li_cache, e );
278 idl_free( candidates );
281 if( realBase != NULL) {
291 /* free reader lock */
292 cache_return_entry_r( &li->li_cache, e );
297 idl_free( candidates );
299 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
302 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
307 if( realBase != NULL) {
327 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
333 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n", base, 0, 0);
337 /* get entry with reader lock */
338 if ( (e = dn2entry_r( be, base, matched )) == NULL ) {
339 *err = LDAP_NO_SUCH_OBJECT;
343 /* check for deleted */
345 idl = idl_alloc( 1 );
346 idl_insert( &idl, e->e_id, 1 );
349 /* free reader lock */
350 cache_return_entry_r( &li->li_cache, e );
368 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
374 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: \"%s\"\n", base, 0, 0);
378 /* get the base object with reader lock */
379 if ( base != NULL && *base != '\0' &&
380 (e = dn2entry_r( be, base, matched )) == NULL )
382 *err = LDAP_NO_SUCH_OBJECT;
387 * modify the filter to be something like this:
389 * parent=baseobject & originalfilter
392 f = (Filter *) ch_malloc( sizeof(Filter) );
394 f->f_choice = LDAP_FILTER_AND;
395 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
396 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
397 f->f_and->f_ava.ava_type = ch_strdup( "id2children" );
398 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
399 f->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
400 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
401 f->f_and->f_next = filter;
403 /* from here, it's just like subtree_candidates */
404 candidates = subtree_candidates( be, conn, op, base, f, attrs,
405 attrsonly, matched, e, err, 0 );
407 /* free up just the filter stuff we allocated above */
408 f->f_and->f_next = NULL;
411 /* free entry and reader lock */
413 cache_return_entry_r( &li->li_cache, e );
415 return( candidates );
433 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
434 Filter *f, **filterarg_ptr;
437 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" %s\n",
438 base ? base : "NULL", lookupbase ? "lookupbase" : "", 0);
441 * get the base object - unless we already have it (from one-level).
442 * also, unless this is a one-level search or a subtree search
443 * starting at the very top of our subtree, we need to modify the
444 * filter to be something like this:
446 * dn=*baseobjectdn & (originalfilter | ref=*)
448 * the "objectclass=referral" part is used to select referrals to return
456 if ( base != NULL && *base != '\0' &&
457 (e = dn2entry_r( be, base, matched )) == NULL )
459 *err = LDAP_NO_SUCH_OBJECT;
464 cache_return_entry_r( &li->li_cache, e );
467 f = (Filter *) ch_malloc( sizeof(Filter) );
469 f->f_choice = LDAP_FILTER_OR;
470 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
471 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
472 f->f_or->f_avtype = ch_strdup( "objectclass" );
473 /* Patch to use normalized uppercase */
474 f->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
475 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
476 filterarg_ptr = &f->f_or->f_next;
477 *filterarg_ptr = filter;
480 if ( ! be_issuffix( be, base ) ) {
481 f = (Filter *) ch_malloc( sizeof(Filter) );
483 f->f_choice = LDAP_FILTER_AND;
484 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
485 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
486 f->f_and->f_sub_type = ch_strdup( "dn" );
487 f->f_and->f_sub_initial = NULL;
488 f->f_and->f_sub_any = NULL;
489 f->f_and->f_sub_final = ch_strdup( base );
490 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
491 f->f_and->f_next = filter;
496 candidates = filter_candidates( be, filter );
498 /* free up just the parts we allocated above */
500 *filterarg_ptr = NULL;
504 return( candidates );