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 *ch_realloc();
21 extern char *dn_parent();
23 static IDList *base_candidates();
24 static IDList *onelevel_candidates();
25 static IDList *subtree_candidates();
27 #define GRABSIZE BUFSIZ
29 #define MAKE_SPACE( n ) { \
30 if ( rcur + n > rbuf + rmaxsize ) { \
31 int offset = rcur - rbuf; \
32 rbuf = ch_realloc( rbuf, rmaxsize + GRABSIZE ); \
33 rmaxsize += GRABSIZE; \
34 rcur = rbuf + offset; \
54 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
63 char *rbuf, *rcur, *r;
67 Debug(LDAP_DEBUG_ARGS, "=> ldbm_back_search\n", 0, 0, 0);
69 if ( tlimit == 0 && be_isroot( be, op->o_dn ) ) {
70 tlimit = -1; /* allow root to set no limit */
72 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
73 be->be_timelimit : tlimit;
74 stoptime = op->o_time + tlimit;
76 if ( slimit == 0 && be_isroot( be, op->o_dn ) ) {
77 slimit = -1; /* allow root to set no limit */
79 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
80 be->be_sizelimit : slimit;
84 * check and apply aliasing where the dereferencing applies to
85 * the subordinates of the base
87 realBase = strdup (base);
89 case LDAP_DEREF_FINDING:
90 case LDAP_DEREF_ALWAYS:
92 realBase = derefDN ( be, conn, op, base );
96 (void) dn_normalize (realBase);
98 Debug( LDAP_DEBUG_TRACE, "using base %s\n",
102 case LDAP_SCOPE_BASE:
103 candidates = base_candidates( be, conn, op, realBase, filter,
104 attrs, attrsonly, &matched, &err );
107 case LDAP_SCOPE_ONELEVEL:
108 candidates = onelevel_candidates( be, conn, op, realBase, filter,
109 attrs, attrsonly, &matched, &err );
112 case LDAP_SCOPE_SUBTREE:
113 candidates = subtree_candidates( be, conn, op, realBase, filter,
114 attrs, attrsonly, &matched, NULL, &err, 1 );
118 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, "",
123 /* null candidates means we could not find the base object */
124 if ( candidates == NULL ) {
125 send_ldap_result( conn, op, err, matched, "" );
126 if ( matched != NULL ) {
135 MAKE_SPACE( sizeof("Referral:") + 1 );
136 strcpy( rbuf, "Referral:" );
137 rcur = strchr( rbuf, '\0' );
138 for ( id = idl_firstid( candidates ); id != NOID;
139 id = idl_nextid( candidates, id ) ) {
140 /* check for abandon */
141 pthread_mutex_lock( &op->o_abandonmutex );
142 if ( op->o_abandon ) {
143 pthread_mutex_unlock( &op->o_abandonmutex );
144 idl_free( candidates );
148 pthread_mutex_unlock( &op->o_abandonmutex );
150 /* check time limit */
151 pthread_mutex_lock( ¤ttime_mutex );
152 time( ¤ttime );
153 if ( tlimit != -1 && currenttime > stoptime ) {
154 pthread_mutex_unlock( ¤ttime_mutex );
155 send_ldap_search_result( conn, op,
156 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
158 idl_free( candidates );
162 pthread_mutex_unlock( ¤ttime_mutex );
164 /* get the entry with reader lock */
165 if ( (e = id2entry_r( be, id )) == NULL ) {
166 Debug( LDAP_DEBUG_ARGS, "candidate %d not found\n", id, 0, 0 );
171 * if it's a referral, add it to the list of referrals. only do
172 * this for subtree searches, and don't check the filter explicitly
173 * here since it's only a candidate anyway.
175 if ( e->e_dn != NULL &&
176 strncasecmp( e->e_dn, "ref=", 4 ) == 0 &&
177 (ref = attr_find( e->e_attrs, "ref" )) != NULL &&
178 scope == LDAP_SCOPE_SUBTREE )
182 if ( ref->a_vals == NULL ) {
183 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n",
186 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
187 /* referral + newline + null */
188 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
190 strncpy( rcur, ref->a_vals[i]->bv_val,
191 ref->a_vals[i]->bv_len );
192 rcur = rcur + ref->a_vals[i]->bv_len;
198 /* otherwise it's an entry - see if it matches the filter */
200 /* if it matches the filter and scope, send it */
201 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
207 if ( scope == LDAP_SCOPE_ONELEVEL ) {
208 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
209 (void) dn_normalize( dn );
210 scopeok = (dn == realBase) ? 1 : (! strcasecmp( dn, realBase ));
212 scopeok = (realBase == NULL || *realBase == '\0');
215 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
216 dn = strdup( e->e_dn );
217 (void) dn_normalize( dn );
218 scopeok = dn_issuffix( dn, realBase );
223 /* check size limit */
224 if ( --slimit == -1 ) {
225 cache_return_entry_r( &li->li_cache, e );
226 send_ldap_search_result( conn, op,
227 LDAP_SIZELIMIT_EXCEEDED, NULL,
228 nrefs > 0 ? rbuf : NULL, nentries );
229 idl_free( candidates );
235 * check and apply aliasing where the dereferencing applies to
236 * the subordinates of the base
239 case LDAP_DEREF_SEARCHING:
240 case LDAP_DEREF_ALWAYS:
242 Entry *newe = derefAlias_r( be, conn, op, e );
243 cache_return_entry_r( &li->li_cache, e );
249 switch ( send_search_entry( be, conn, op, e,
250 attrs, attrsonly ) ) {
251 case 0: /* entry sent ok */
254 case 1: /* entry not sent */
256 case -1: /* connection closed */
257 cache_return_entry_r( &li->li_cache, e );
258 idl_free( candidates );
267 /* free reader lock */
268 cache_return_entry_r( &li->li_cache, e );
273 idl_free( candidates );
275 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
278 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
299 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
305 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: %s\n", base, 0, 0);
309 /* get entry with reader lock */
310 if ( (e = dn2entry_r( be, base, matched )) == NULL ) {
311 *err = LDAP_NO_SUCH_OBJECT;
315 /* check for deleted */
317 idl = idl_alloc( 1 );
318 idl_insert( &idl, e->e_id, 1 );
321 /* free reader lock */
322 cache_return_entry_r( &li->li_cache, e );
340 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
346 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: %s\n", base, 0, 0);
350 /* get the base object with reader lock */
351 if ( base != NULL && *base != '\0' &&
352 (e = dn2entry_r( be, base, matched )) == NULL )
354 *err = LDAP_NO_SUCH_OBJECT;
359 * modify the filter to be something like this:
361 * parent=baseobject & originalfilter
364 f = (Filter *) ch_malloc( sizeof(Filter) );
366 f->f_choice = LDAP_FILTER_AND;
367 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
368 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
369 f->f_and->f_ava.ava_type = strdup( "id2children" );
370 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
371 f->f_and->f_ava.ava_value.bv_val = strdup( buf );
372 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
373 f->f_and->f_next = filter;
375 /* from here, it's just like subtree_candidates */
376 candidates = subtree_candidates( be, conn, op, base, f, attrs,
377 attrsonly, matched, e, err, 0 );
379 /* free up just the filter stuff we allocated above */
380 f->f_and->f_next = NULL;
383 /* free entry and reader lock */
384 cache_return_entry_r( &li->li_cache, e );
385 return( candidates );
403 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
407 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: %s\n",
408 base ? base : "NULL", 0, 0);
411 * get the base object - unless we already have it (from one-level).
412 * also, unless this is a one-level search or a subtree search
413 * starting at the very top of our subtree, we need to modify the
414 * filter to be something like this:
416 * dn=*baseobjectdn & (originalfilter | ref=*)
418 * the "objectclass=referral" part is used to select referrals to return
424 if ( base != NULL && *base != '\0' &&
425 (e = dn2entry_r( be, base, matched )) == NULL )
427 *err = LDAP_NO_SUCH_OBJECT;
432 cache_return_entry_r( &li->li_cache, e );
435 f = (Filter *) ch_malloc( sizeof(Filter) );
437 f->f_choice = LDAP_FILTER_OR;
438 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
439 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
440 f->f_or->f_avtype = strdup( "objectclass" );
441 /* Patch to use normalized uppercase */
442 f->f_or->f_avvalue.bv_val = strdup( "REFERRAL" );
443 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
444 f->f_or->f_next = filter;
447 if ( ! be_issuffix( be, base ) ) {
448 f = (Filter *) ch_malloc( sizeof(Filter) );
450 f->f_choice = LDAP_FILTER_AND;
451 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
452 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
453 f->f_and->f_sub_type = strdup( "dn" );
454 f->f_and->f_sub_initial = NULL;
455 f->f_and->f_sub_any = NULL;
456 f->f_and->f_sub_final = strdup( base );
457 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
458 f->f_and->f_next = filter;
463 candidates = filter_candidates( be, filter );
465 /* free up just the parts we allocated above */
467 f->f_and->f_next = NULL;
471 return( candidates );