1 /* search.c - ldbm backend search function */
8 #include <sys/socket.h>
10 #include "back-ldbm.h"
11 #include "proto-back-ldbm.h"
13 extern time_t currenttime;
14 extern pthread_mutex_t currenttime_mutex;
16 extern IDList *idl_alloc();
17 extern Attribute *attr_find();
18 extern IDList *filter_candidates();
19 extern char *ch_realloc();
20 extern char *dn_parent();
22 static IDList *base_candidates();
23 static IDList *onelevel_candidates();
24 static IDList *subtree_candidates();
26 #define GRABSIZE BUFSIZ
28 #define MAKE_SPACE( n ) { \
29 if ( rcur + n > rbuf + rmaxsize ) { \
30 int offset = rcur - rbuf; \
31 rbuf = ch_realloc( rbuf, rmaxsize + GRABSIZE ); \
32 rmaxsize += GRABSIZE; \
33 rcur = rbuf + offset; \
53 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
62 char *rbuf, *rcur, *r;
65 Debug(LDAP_DEBUG_ARGS, "=> ldbm_back_search\n", 0, 0, 0);
67 if ( tlimit == 0 && be_isroot( be, op->o_dn ) ) {
68 tlimit = -1; /* allow root to set no limit */
70 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
71 be->be_timelimit : tlimit;
72 stoptime = op->o_time + tlimit;
74 if ( slimit == 0 && be_isroot( be, op->o_dn ) ) {
75 slimit = -1; /* allow root to set no limit */
77 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
78 be->be_sizelimit : slimit;
83 candidates = base_candidates( be, conn, op, base, filter,
84 attrs, attrsonly, &matched, &err );
87 case LDAP_SCOPE_ONELEVEL:
88 candidates = onelevel_candidates( be, conn, op, base, filter,
89 attrs, attrsonly, &matched, &err );
92 case LDAP_SCOPE_SUBTREE:
93 candidates = subtree_candidates( be, conn, op, base, filter,
94 attrs, attrsonly, &matched, NULL, &err, 1 );
98 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, "",
103 /* null candidates means we could not find the base object */
104 if ( candidates == NULL ) {
105 send_ldap_result( conn, op, err, matched, "" );
106 if ( matched != NULL ) {
115 MAKE_SPACE( sizeof("Referral:") + 1 );
116 strcpy( rbuf, "Referral:" );
117 rcur = strchr( rbuf, '\0' );
118 for ( id = idl_firstid( candidates ); id != NOID;
119 id = idl_nextid( candidates, id ) ) {
120 /* check for abandon */
121 pthread_mutex_lock( &op->o_abandonmutex );
122 if ( op->o_abandon ) {
123 pthread_mutex_unlock( &op->o_abandonmutex );
124 idl_free( candidates );
128 pthread_mutex_unlock( &op->o_abandonmutex );
130 /* check time limit */
131 pthread_mutex_lock( ¤ttime_mutex );
132 time( ¤ttime );
133 if ( tlimit != -1 && currenttime > stoptime ) {
134 pthread_mutex_unlock( ¤ttime_mutex );
135 send_ldap_search_result( conn, op,
136 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
138 idl_free( candidates );
142 pthread_mutex_unlock( ¤ttime_mutex );
144 /* get the entry with reader lock */
145 if ( (e = id2entry_r( be, id )) == NULL ) {
146 Debug( LDAP_DEBUG_ARGS, "candidate %d not found\n", id, 0, 0 );
151 * if it's a referral, add it to the list of referrals. only do
152 * this for subtree searches, and don't check the filter explicitly
153 * here since it's only a candidate anyway.
155 if ( e->e_dn != NULL && strncasecmp( e->e_dn, "ref=", 4 )
156 == 0 && (ref = attr_find( e->e_attrs, "ref" )) != NULL &&
157 scope == LDAP_SCOPE_SUBTREE )
161 if ( ref->a_vals == NULL ) {
162 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n", 0,
165 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
166 /* referral + newline + null */
167 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
169 strncpy( rcur, ref->a_vals[i]->bv_val,
170 ref->a_vals[i]->bv_len );
171 rcur = rcur + ref->a_vals[i]->bv_len;
177 /* otherwise it's an entry - see if it matches the filter */
179 /* if it matches the filter and scope, send it */
180 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
186 if ( scope == LDAP_SCOPE_ONELEVEL ) {
187 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
188 (void) dn_normalize( dn );
189 scopeok = (dn == base) ? 1 : (! strcasecmp( dn, base ));
191 scopeok = (base == NULL || *base == '\0');
194 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
195 dn = strdup( e->e_dn );
196 (void) dn_normalize( dn );
197 scopeok = dn_issuffix( dn, base );
202 /* check size limit */
203 if ( --slimit == -1 ) {
204 cache_return_entry_r( &li->li_cache, e );
205 send_ldap_search_result( conn, op,
206 LDAP_SIZELIMIT_EXCEEDED, NULL,
207 nrefs > 0 ? rbuf : NULL, nentries );
208 idl_free( candidates );
213 switch ( send_search_entry( be, conn, op, e,
214 attrs, attrsonly ) ) {
215 case 0: /* entry sent ok */
218 case 1: /* entry not sent */
220 case -1: /* connection closed */
221 cache_return_entry_r( &li->li_cache, e );
222 idl_free( candidates );
231 /* free reader lock */
232 cache_return_entry_r( &li->li_cache, e );
237 idl_free( candidates );
239 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
242 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
263 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
269 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: %s\n", base, 0, 0);
273 /* get entry with reader lock */
274 if ( (e = dn2entry_r( be, base, matched )) == NULL ) {
275 *err = LDAP_NO_SUCH_OBJECT;
279 /* check for deleted */
281 idl = idl_alloc( 1 );
282 idl_insert( &idl, e->e_id, 1 );
285 /* free reader lock */
286 cache_return_entry_r( &li->li_cache, e );
304 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
310 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: %s\n", base, 0, 0);
314 /* get the base object with reader lock */
315 if ( base != NULL && *base != '\0' &&
316 (e = dn2entry_r( be, base, matched )) == NULL )
318 *err = LDAP_NO_SUCH_OBJECT;
323 * modify the filter to be something like this:
325 * parent=baseobject & originalfilter
328 f = (Filter *) ch_malloc( sizeof(Filter) );
330 f->f_choice = LDAP_FILTER_AND;
331 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
332 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
333 f->f_and->f_ava.ava_type = strdup( "id2children" );
334 sprintf( buf, "%d", e != NULL ? e->e_id : 0 );
335 f->f_and->f_ava.ava_value.bv_val = strdup( buf );
336 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
337 f->f_and->f_next = filter;
339 /* from here, it's just like subtree_candidates */
340 candidates = subtree_candidates( be, conn, op, base, f, attrs,
341 attrsonly, matched, e, err, 0 );
343 /* free up just the filter stuff we allocated above */
344 f->f_and->f_next = NULL;
347 /* free entry and reader lock */
348 cache_return_entry_r( &li->li_cache, e );
349 return( candidates );
367 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
371 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: %s\n",
372 base ? base : "NULL", 0, 0);
375 * get the base object - unless we already have it (from one-level).
376 * also, unless this is a one-level search or a subtree search
377 * starting at the very top of our subtree, we need to modify the
378 * filter to be something like this:
380 * dn=*baseobjectdn & (originalfilter | ref=*)
382 * the "objectclass=referral" part is used to select referrals to return
388 if ( base != NULL && *base != '\0' &&
389 (e = dn2entry_r( be, base, matched )) == NULL )
391 *err = LDAP_NO_SUCH_OBJECT;
396 cache_return_entry_r( &li->li_cache, e );
399 f = (Filter *) ch_malloc( sizeof(Filter) );
401 f->f_choice = LDAP_FILTER_OR;
402 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
403 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
404 f->f_or->f_avtype = strdup( "objectclass" );
405 /* Patch to use normalized uppercase */
406 f->f_or->f_avvalue.bv_val = strdup( "REFERRAL" );
407 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
408 f->f_or->f_next = filter;
411 if ( ! be_issuffix( be, base ) ) {
412 f = (Filter *) ch_malloc( sizeof(Filter) );
414 f->f_choice = LDAP_FILTER_AND;
415 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
416 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
417 f->f_and->f_sub_type = strdup( "dn" );
418 f->f_and->f_sub_initial = NULL;
419 f->f_and->f_sub_any = NULL;
420 f->f_and->f_sub_final = strdup( base );
421 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
422 f->f_and->f_next = filter;
427 candidates = filter_candidates( be, filter );
429 /* free up just the parts we allocated above */
431 f->f_and->f_next = NULL;
435 return( candidates );