1 /* search.c - ldbm backend search function */
6 #include <sys/socket.h>
9 #include "proto-back-ldbm.h"
11 extern time_t currenttime;
12 extern pthread_mutex_t currenttime_mutex;
14 extern IDList *idl_alloc();
15 extern Attribute *attr_find();
16 extern IDList *filter_candidates();
17 extern char *ch_realloc();
18 extern char *dn_parent();
20 static IDList *base_candidates();
21 static IDList *onelevel_candidates();
22 static IDList *subtree_candidates();
24 #define GRABSIZE BUFSIZ
26 #define MAKE_SPACE( n ) { \
27 if ( rcur + n > rbuf + rmaxsize ) { \
28 int offset = rcur - rbuf; \
29 rbuf = ch_realloc( rbuf, rmaxsize + GRABSIZE ); \
30 rmaxsize += GRABSIZE; \
31 rcur = rbuf + offset; \
51 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
60 char *rbuf, *rcur, *r;
63 Debug(LDAP_DEBUG_ARGS, "=> ldbm_back_search\n", 0, 0, 0);
65 if ( tlimit == 0 && be_isroot( be, op->o_dn ) ) {
66 tlimit = -1; /* allow root to set no limit */
68 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
69 be->be_timelimit : tlimit;
70 stoptime = op->o_time + tlimit;
72 if ( slimit == 0 && be_isroot( be, op->o_dn ) ) {
73 slimit = -1; /* allow root to set no limit */
75 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
76 be->be_sizelimit : slimit;
81 candidates = base_candidates( be, conn, op, base, filter,
82 attrs, attrsonly, &matched, &err );
85 case LDAP_SCOPE_ONELEVEL:
86 candidates = onelevel_candidates( be, conn, op, base, filter,
87 attrs, attrsonly, &matched, &err );
90 case LDAP_SCOPE_SUBTREE:
91 candidates = subtree_candidates( be, conn, op, base, filter,
92 attrs, attrsonly, &matched, NULL, &err, 1 );
96 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, "",
101 /* null candidates means we could not find the base object */
102 if ( candidates == NULL ) {
103 send_ldap_result( conn, op, err, matched, "" );
104 if ( matched != NULL ) {
113 MAKE_SPACE( sizeof("Referral:") + 1 );
114 strcpy( rbuf, "Referral:" );
115 rcur = strchr( rbuf, '\0' );
116 for ( id = idl_firstid( candidates ); id != NOID;
117 id = idl_nextid( candidates, id ) ) {
118 /* check for abandon */
119 pthread_mutex_lock( &op->o_abandonmutex );
120 if ( op->o_abandon ) {
121 pthread_mutex_unlock( &op->o_abandonmutex );
122 idl_free( candidates );
126 pthread_mutex_unlock( &op->o_abandonmutex );
128 /* check time limit */
129 pthread_mutex_lock( ¤ttime_mutex );
130 time( ¤ttime );
131 if ( tlimit != -1 && currenttime > stoptime ) {
132 pthread_mutex_unlock( ¤ttime_mutex );
133 send_ldap_search_result( conn, op,
134 LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
136 idl_free( candidates );
140 pthread_mutex_unlock( ¤ttime_mutex );
142 /* get the entry with reader lock */
143 if ( (e = id2entry_r( be, id )) == NULL ) {
144 Debug( LDAP_DEBUG_ARGS, "candidate %d not found\n", id, 0, 0 );
149 * if it's a referral, add it to the list of referrals. only do
150 * this for subtree searches, and don't check the filter explicitly
151 * here since it's only a candidate anyway.
153 if ( e->e_dn != NULL && strncasecmp( e->e_dn, "ref=", 4 )
154 == 0 && (ref = attr_find( e->e_attrs, "ref" )) != NULL &&
155 scope == LDAP_SCOPE_SUBTREE )
159 if ( ref->a_vals == NULL ) {
160 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n", 0,
163 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
164 /* referral + newline + null */
165 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
167 strncpy( rcur, ref->a_vals[i]->bv_val,
168 ref->a_vals[i]->bv_len );
169 rcur = rcur + ref->a_vals[i]->bv_len;
175 /* otherwise it's an entry - see if it matches the filter */
177 /* if it matches the filter and scope, send it */
178 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
184 if ( scope == LDAP_SCOPE_ONELEVEL ) {
185 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
186 (void) dn_normalize( dn );
187 scopeok = (dn == base) ? 1 : (! strcasecmp( dn, base ));
189 scopeok = (base == NULL || *base == '\0');
192 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
193 dn = strdup( e->e_dn );
194 (void) dn_normalize( dn );
195 scopeok = dn_issuffix( dn, base );
200 /* check size limit */
201 if ( --slimit == -1 ) {
202 cache_return_entry_r( &li->li_cache, e );
203 send_ldap_search_result( conn, op,
204 LDAP_SIZELIMIT_EXCEEDED, NULL,
205 nrefs > 0 ? rbuf : NULL, nentries );
206 idl_free( candidates );
211 switch ( send_search_entry( be, conn, op, e,
212 attrs, attrsonly ) ) {
213 case 0: /* entry sent ok */
216 case 1: /* entry not sent */
218 case -1: /* connection closed */
219 cache_return_entry_r( &li->li_cache, e );
220 idl_free( candidates );
229 /* free reader lock */
230 cache_return_entry_r( &li->li_cache, e );
235 idl_free( candidates );
237 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
240 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
261 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
267 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: %s\n", base, 0, 0);
271 /* get entry with reader lock */
272 if ( (e = dn2entry_r( be, base, matched )) == NULL ) {
273 *err = LDAP_NO_SUCH_OBJECT;
277 /* check for deleted */
279 idl = idl_alloc( 1 );
280 idl_insert( &idl, e->e_id, 1 );
283 /* free reader lock */
284 cache_return_entry_r( &li->li_cache, e );
302 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
308 Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: %s\n", base, 0, 0);
312 /* get the base object with reader lock */
313 if ( base != NULL && *base != '\0' &&
314 (e = dn2entry_r( be, base, matched )) == NULL )
316 *err = LDAP_NO_SUCH_OBJECT;
321 * modify the filter to be something like this:
323 * parent=baseobject & originalfilter
326 f = (Filter *) ch_malloc( sizeof(Filter) );
328 f->f_choice = LDAP_FILTER_AND;
329 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
330 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
331 f->f_and->f_ava.ava_type = strdup( "id2children" );
332 sprintf( buf, "%d", e != NULL ? e->e_id : 0 );
333 f->f_and->f_ava.ava_value.bv_val = strdup( buf );
334 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
335 f->f_and->f_next = filter;
337 /* from here, it's just like subtree_candidates */
338 candidates = subtree_candidates( be, conn, op, base, f, attrs,
339 attrsonly, matched, e, err, 0 );
341 /* free up just the filter stuff we allocated above */
342 f->f_and->f_next = NULL;
345 /* free entry and reader lock */
346 cache_return_entry_r( &li->li_cache, e );
347 return( candidates );
365 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
369 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: %s\n",
370 base ? base : "NULL", 0, 0);
373 * get the base object - unless we already have it (from one-level).
374 * also, unless this is a one-level search or a subtree search
375 * starting at the very top of our subtree, we need to modify the
376 * filter to be something like this:
378 * dn=*baseobjectdn & (originalfilter | ref=*)
380 * the "objectclass=referral" part is used to select referrals to return
386 if ( base != NULL && *base != '\0' &&
387 (e = dn2entry_r( be, base, matched )) == NULL )
389 *err = LDAP_NO_SUCH_OBJECT;
394 cache_return_entry_r( &li->li_cache, e );
397 f = (Filter *) ch_malloc( sizeof(Filter) );
399 f->f_choice = LDAP_FILTER_OR;
400 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
401 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
402 f->f_or->f_avtype = strdup( "objectclass" );
403 /* Patch to use normalized uppercase */
404 f->f_or->f_avvalue.bv_val = strdup( "REFERRAL" );
405 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
406 f->f_or->f_next = filter;
409 if ( ! be_issuffix( be, base ) ) {
410 f = (Filter *) ch_malloc( sizeof(Filter) );
412 f->f_choice = LDAP_FILTER_AND;
413 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
414 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
415 f->f_and->f_sub_type = strdup( "dn" );
416 f->f_and->f_sub_initial = NULL;
417 f->f_and->f_sub_any = NULL;
418 f->f_and->f_sub_final = strdup( base );
419 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
420 f->f_and->f_next = filter;
425 candidates = filter_candidates( be, filter );
427 /* free up just the parts we allocated above */
429 f->f_and->f_next = NULL;
433 return( candidates );