1 /* search.c - ldbm backend search function */
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
14 extern time_t currenttime;
15 extern pthread_mutex_t currenttime_mutex;
17 extern IDList *idl_alloc();
18 extern Attribute *attr_find();
19 extern IDList *filter_candidates();
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;
66 Debug(LDAP_DEBUG_ARGS, "=> ldbm_back_search\n", 0, 0, 0);
68 if ( tlimit == 0 && be_isroot( be, op->o_dn ) ) {
69 tlimit = -1; /* allow root to set no limit */
71 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
72 be->be_timelimit : tlimit;
73 stoptime = op->o_time + tlimit;
75 if ( slimit == 0 && be_isroot( be, op->o_dn ) ) {
76 slimit = -1; /* allow root to set no limit */
78 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
79 be->be_sizelimit : slimit;
83 * check and apply aliasing where the dereferencing applies to
84 * the subordinates of the base
86 realBase = strdup (base);
88 case LDAP_DEREF_FINDING:
89 case LDAP_DEREF_ALWAYS:
91 realBase = derefDN ( be, conn, op, base );
95 (void) dn_normalize (realBase);
97 Debug( LDAP_DEBUG_TRACE, "using base %s\n",
101 case LDAP_SCOPE_BASE:
102 candidates = base_candidates( be, conn, op, realBase, filter,
103 attrs, attrsonly, &matched, &err );
106 case LDAP_SCOPE_ONELEVEL:
107 candidates = onelevel_candidates( be, conn, op, realBase, filter,
108 attrs, attrsonly, &matched, &err );
111 case LDAP_SCOPE_SUBTREE:
112 candidates = subtree_candidates( be, conn, op, realBase, filter,
113 attrs, attrsonly, &matched, NULL, &err, 1 );
117 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, "",
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 ) {
134 MAKE_SPACE( sizeof("Referral:") + 1 );
135 strcpy( rbuf, "Referral:" );
136 rcur = strchr( rbuf, '\0' );
137 for ( id = idl_firstid( candidates ); id != NOID;
138 id = idl_nextid( candidates, id ) ) {
139 /* check for abandon */
140 pthread_mutex_lock( &op->o_abandonmutex );
141 if ( op->o_abandon ) {
142 pthread_mutex_unlock( &op->o_abandonmutex );
143 idl_free( candidates );
147 pthread_mutex_unlock( &op->o_abandonmutex );
149 /* check time limit */
150 pthread_mutex_lock( ¤ttime_mutex );
151 time( ¤ttime );
152 if ( tlimit != -1 && currenttime > stoptime ) {
153 pthread_mutex_unlock( ¤ttime_mutex );
154 send_ldap_search_result( conn, op,
155 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
157 idl_free( candidates );
161 pthread_mutex_unlock( ¤ttime_mutex );
163 /* get the entry with reader lock */
164 if ( (e = id2entry_r( be, id )) == NULL ) {
165 Debug( LDAP_DEBUG_ARGS, "candidate %d not found\n", id, 0, 0 );
170 * if it's a referral, add it to the list of referrals. only do
171 * this for subtree searches, and don't check the filter explicitly
172 * here since it's only a candidate anyway.
174 if ( e->e_dn != NULL &&
175 strncasecmp( e->e_dn, "ref=", 4 ) == 0 &&
176 (ref = attr_find( e->e_attrs, "ref" )) != NULL &&
177 scope == LDAP_SCOPE_SUBTREE )
181 if ( ref->a_vals == NULL ) {
182 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n",
185 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
186 /* referral + newline + null */
187 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
189 strncpy( rcur, ref->a_vals[i]->bv_val,
190 ref->a_vals[i]->bv_len );
191 rcur = rcur + ref->a_vals[i]->bv_len;
197 /* otherwise it's an entry - see if it matches the filter */
199 /* if it matches the filter and scope, send it */
200 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
206 if ( scope == LDAP_SCOPE_ONELEVEL ) {
207 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
208 (void) dn_normalize( dn );
209 scopeok = (dn == realBase) ? 1 : (! strcasecmp( dn, realBase ));
211 scopeok = (realBase == NULL || *realBase == '\0');
214 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
215 dn = strdup( e->e_dn );
216 (void) dn_normalize( dn );
217 scopeok = dn_issuffix( dn, realBase );
222 /* check size limit */
223 if ( --slimit == -1 ) {
224 cache_return_entry_r( &li->li_cache, e );
225 send_ldap_search_result( conn, op,
226 LDAP_SIZELIMIT_EXCEEDED, NULL,
227 nrefs > 0 ? rbuf : NULL, nentries );
228 idl_free( candidates );
234 * check and apply aliasing where the dereferencing applies to
235 * the subordinates of the base
238 case LDAP_DEREF_SEARCHING:
239 case LDAP_DEREF_ALWAYS:
241 Entry *newe = derefAlias_r( be, conn, op, e );
242 cache_return_entry_r( &li->li_cache, e );
248 switch ( send_search_entry( be, conn, op, e,
249 attrs, attrsonly ) ) {
250 case 0: /* entry sent ok */
253 case 1: /* entry not sent */
255 case -1: /* connection closed */
256 cache_return_entry_r( &li->li_cache, e );
257 idl_free( candidates );
266 /* free reader lock */
267 cache_return_entry_r( &li->li_cache, e );
272 idl_free( candidates );
274 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
277 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
298 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
304 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: %s\n", base, 0, 0);
308 /* get entry with reader lock */
309 if ( (e = dn2entry_r( be, base, matched )) == NULL ) {
310 *err = LDAP_NO_SUCH_OBJECT;
314 /* check for deleted */
316 idl = idl_alloc( 1 );
317 idl_insert( &idl, e->e_id, 1 );
320 /* free reader lock */
321 cache_return_entry_r( &li->li_cache, e );
339 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
345 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: %s\n", base, 0, 0);
349 /* get the base object with reader lock */
350 if ( base != NULL && *base != '\0' &&
351 (e = dn2entry_r( be, base, matched )) == NULL )
353 *err = LDAP_NO_SUCH_OBJECT;
358 * modify the filter to be something like this:
360 * parent=baseobject & originalfilter
363 f = (Filter *) ch_malloc( sizeof(Filter) );
365 f->f_choice = LDAP_FILTER_AND;
366 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
367 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
368 f->f_and->f_ava.ava_type = strdup( "id2children" );
369 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
370 f->f_and->f_ava.ava_value.bv_val = strdup( buf );
371 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
372 f->f_and->f_next = filter;
374 /* from here, it's just like subtree_candidates */
375 candidates = subtree_candidates( be, conn, op, base, f, attrs,
376 attrsonly, matched, e, err, 0 );
378 /* free up just the filter stuff we allocated above */
379 f->f_and->f_next = NULL;
382 /* free entry and reader lock */
383 cache_return_entry_r( &li->li_cache, e );
384 return( candidates );
402 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
406 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: %s\n",
407 base ? base : "NULL", 0, 0);
410 * get the base object - unless we already have it (from one-level).
411 * also, unless this is a one-level search or a subtree search
412 * starting at the very top of our subtree, we need to modify the
413 * filter to be something like this:
415 * dn=*baseobjectdn & (originalfilter | ref=*)
417 * the "objectclass=referral" part is used to select referrals to return
423 if ( base != NULL && *base != '\0' &&
424 (e = dn2entry_r( be, base, matched )) == NULL )
426 *err = LDAP_NO_SUCH_OBJECT;
431 cache_return_entry_r( &li->li_cache, e );
434 f = (Filter *) ch_malloc( sizeof(Filter) );
436 f->f_choice = LDAP_FILTER_OR;
437 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
438 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
439 f->f_or->f_avtype = strdup( "objectclass" );
440 /* Patch to use normalized uppercase */
441 f->f_or->f_avvalue.bv_val = strdup( "REFERRAL" );
442 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
443 f->f_or->f_next = filter;
446 if ( ! be_issuffix( be, base ) ) {
447 f = (Filter *) ch_malloc( sizeof(Filter) );
449 f->f_choice = LDAP_FILTER_AND;
450 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
451 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
452 f->f_and->f_sub_type = strdup( "dn" );
453 f->f_and->f_sub_initial = NULL;
454 f->f_and->f_sub_any = NULL;
455 f->f_and->f_sub_final = strdup( base );
456 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
457 f->f_and->f_next = filter;
462 candidates = filter_candidates( be, filter );
464 /* free up just the parts we allocated above */
466 f->f_and->f_next = NULL;
470 return( candidates );