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 && strncasecmp( e->e_dn, "ref=", 4 )
176 == 0 && (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, "%d", 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 );