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
78 realBase = strdup (base);
80 case LDAP_DEREF_FINDING:
81 case LDAP_DEREF_ALWAYS:
83 realBase = derefDN ( be, conn, op, base );
87 (void) dn_normalize (realBase);
89 Debug( LDAP_DEBUG_TRACE, "using base %s\n",
94 candidates = base_candidates( be, conn, op, realBase, filter,
95 attrs, attrsonly, &matched, &err );
98 case LDAP_SCOPE_ONELEVEL:
99 candidates = onelevel_candidates( be, conn, op, realBase, filter,
100 attrs, attrsonly, &matched, &err );
103 case LDAP_SCOPE_SUBTREE:
104 candidates = subtree_candidates( be, conn, op, realBase, filter,
105 attrs, attrsonly, &matched, NULL, &err, 1 );
109 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, "",
114 /* null candidates means we could not find the base object */
115 if ( candidates == NULL ) {
116 send_ldap_result( conn, op, err, matched, "" );
117 if ( matched != NULL ) {
126 MAKE_SPACE( sizeof("Referral:") + 1 );
127 strcpy( rbuf, "Referral:" );
128 rcur = strchr( rbuf, '\0' );
129 for ( id = idl_firstid( candidates ); id != NOID;
130 id = idl_nextid( candidates, id ) ) {
131 /* check for abandon */
132 pthread_mutex_lock( &op->o_abandonmutex );
133 if ( op->o_abandon ) {
134 pthread_mutex_unlock( &op->o_abandonmutex );
135 idl_free( candidates );
139 pthread_mutex_unlock( &op->o_abandonmutex );
141 /* check time limit */
142 pthread_mutex_lock( ¤ttime_mutex );
143 time( ¤ttime );
144 if ( tlimit != -1 && currenttime > stoptime ) {
145 pthread_mutex_unlock( ¤ttime_mutex );
146 send_ldap_search_result( conn, op,
147 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
149 idl_free( candidates );
153 pthread_mutex_unlock( ¤ttime_mutex );
155 /* get the entry with reader lock */
156 if ( (e = id2entry_r( be, id )) == NULL ) {
157 Debug( LDAP_DEBUG_ARGS, "candidate %lu not found\n",
163 * if it's a referral, add it to the list of referrals. only do
164 * this for subtree searches, and don't check the filter explicitly
165 * here since it's only a candidate anyway.
167 if ( e->e_dn != NULL &&
168 strncasecmp( e->e_dn, "ref=", 4 ) == 0 &&
169 (ref = attr_find( e->e_attrs, "ref" )) != NULL &&
170 scope == LDAP_SCOPE_SUBTREE )
174 if ( ref->a_vals == NULL ) {
175 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n",
178 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
179 /* referral + newline + null */
180 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
182 strncpy( rcur, ref->a_vals[i]->bv_val,
183 ref->a_vals[i]->bv_len );
184 rcur = rcur + ref->a_vals[i]->bv_len;
190 /* otherwise it's an entry - see if it matches the filter */
192 /* if it matches the filter and scope, send it */
193 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
199 if ( scope == LDAP_SCOPE_ONELEVEL ) {
200 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
201 (void) dn_normalize( dn );
202 scopeok = (dn == realBase) ? 1 : (! strcasecmp( dn, realBase ));
204 scopeok = (realBase == NULL || *realBase == '\0');
207 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
208 dn = strdup( e->e_dn );
209 (void) dn_normalize( dn );
210 scopeok = dn_issuffix( dn, realBase );
215 /* check size limit */
216 if ( --slimit == -1 ) {
217 cache_return_entry_r( &li->li_cache, e );
218 send_ldap_search_result( conn, op,
219 LDAP_SIZELIMIT_EXCEEDED, NULL,
220 nrefs > 0 ? rbuf : NULL, nentries );
221 idl_free( candidates );
227 * check and apply aliasing where the dereferencing applies to
228 * the subordinates of the base
231 case LDAP_DEREF_SEARCHING:
232 case LDAP_DEREF_ALWAYS:
234 Entry *newe = derefAlias_r( be, conn, op, e );
235 cache_return_entry_r( &li->li_cache, e );
241 switch ( send_search_entry( be, conn, op, e,
242 attrs, attrsonly ) ) {
243 case 0: /* entry sent ok */
246 case 1: /* entry not sent */
248 case -1: /* connection closed */
249 cache_return_entry_r( &li->li_cache, e );
250 idl_free( candidates );
259 /* free reader lock */
260 cache_return_entry_r( &li->li_cache, e );
265 idl_free( candidates );
267 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
270 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
291 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
297 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: %s\n", base, 0, 0);
301 /* get entry with reader lock */
302 if ( (e = dn2entry_r( be, base, matched )) == NULL ) {
303 *err = LDAP_NO_SUCH_OBJECT;
307 /* check for deleted */
309 idl = idl_alloc( 1 );
310 idl_insert( &idl, e->e_id, 1 );
313 /* free reader lock */
314 cache_return_entry_r( &li->li_cache, e );
332 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
338 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: %s\n", base, 0, 0);
342 /* get the base object with reader lock */
343 if ( base != NULL && *base != '\0' &&
344 (e = dn2entry_r( be, base, matched )) == NULL )
346 *err = LDAP_NO_SUCH_OBJECT;
351 * modify the filter to be something like this:
353 * parent=baseobject & originalfilter
356 f = (Filter *) ch_malloc( sizeof(Filter) );
358 f->f_choice = LDAP_FILTER_AND;
359 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
360 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
361 f->f_and->f_ava.ava_type = strdup( "id2children" );
362 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
363 f->f_and->f_ava.ava_value.bv_val = strdup( buf );
364 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
365 f->f_and->f_next = filter;
367 /* from here, it's just like subtree_candidates */
368 candidates = subtree_candidates( be, conn, op, base, f, attrs,
369 attrsonly, matched, e, err, 0 );
371 /* free up just the filter stuff we allocated above */
372 f->f_and->f_next = NULL;
375 /* free entry and reader lock */
376 cache_return_entry_r( &li->li_cache, e );
377 return( candidates );
395 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
399 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: %s\n",
400 base ? base : "NULL", 0, 0);
403 * get the base object - unless we already have it (from one-level).
404 * also, unless this is a one-level search or a subtree search
405 * starting at the very top of our subtree, we need to modify the
406 * filter to be something like this:
408 * dn=*baseobjectdn & (originalfilter | ref=*)
410 * the "objectclass=referral" part is used to select referrals to return
416 if ( base != NULL && *base != '\0' &&
417 (e = dn2entry_r( be, base, matched )) == NULL )
419 *err = LDAP_NO_SUCH_OBJECT;
424 cache_return_entry_r( &li->li_cache, e );
427 f = (Filter *) ch_malloc( sizeof(Filter) );
429 f->f_choice = LDAP_FILTER_OR;
430 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
431 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
432 f->f_or->f_avtype = strdup( "objectclass" );
433 /* Patch to use normalized uppercase */
434 f->f_or->f_avvalue.bv_val = strdup( "REFERRAL" );
435 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
436 f->f_or->f_next = filter;
439 if ( ! be_issuffix( be, base ) ) {
440 f = (Filter *) ch_malloc( sizeof(Filter) );
442 f->f_choice = LDAP_FILTER_AND;
443 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
444 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
445 f->f_and->f_sub_type = strdup( "dn" );
446 f->f_and->f_sub_initial = NULL;
447 f->f_and->f_sub_any = NULL;
448 f->f_and->f_sub_final = strdup( base );
449 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
450 f->f_and->f_next = filter;
455 candidates = filter_candidates( be, filter );
457 /* free up just the parts we allocated above */
459 f->f_and->f_next = NULL;
463 return( candidates );