1 /* search.c - ldbm backend search function */
6 #include <sys/socket.h>
10 extern time_t currenttime;
11 extern pthread_mutex_t currenttime_mutex;
14 extern IDList *idl_alloc();
15 extern Entry *id2entry();
16 extern Entry *dn2entry();
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 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 );
143 if ( (e = id2entry( be, id )) == NULL ) {
144 Debug( LDAP_DEBUG_ARGS, "candidate %d not found\n", id,
150 * if it's a referral, add it to the list of referrals. only do
151 * this for subtree searches, and don't check the filter explicitly
152 * here since it's only a candidate anyway.
154 if ( e->e_dn != NULL && strncasecmp( e->e_dn, "ref=", 4 )
155 == 0 && (ref = attr_find( e->e_attrs, "ref" )) != NULL &&
156 scope == LDAP_SCOPE_SUBTREE )
160 if ( ref->a_vals == NULL ) {
161 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n", 0,
164 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
165 /* referral + newline + null */
166 MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
168 strncpy( rcur, ref->a_vals[i]->bv_val,
169 ref->a_vals[i]->bv_len );
170 rcur = rcur + ref->a_vals[i]->bv_len;
176 /* otherwise it's an entry - see if it matches the filter */
178 /* if it matches the filter and scope, send it */
179 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
185 if ( scope == LDAP_SCOPE_ONELEVEL ) {
186 if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
187 (void) dn_normalize( dn );
188 scopeok = (dn == base) ? 1 : (! strcasecmp( dn, base ));
190 scopeok = (base == NULL || *base == '\0');
193 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
194 dn = strdup( e->e_dn );
195 (void) dn_normalize( dn );
196 scopeok = dn_issuffix( dn, base );
201 /* check size limit */
202 if ( --slimit == -1 ) {
203 cache_return_entry( &li->li_cache, e );
204 send_ldap_search_result( conn, op,
205 LDAP_SIZELIMIT_EXCEEDED, NULL,
206 nrefs > 0 ? rbuf : NULL, nentries );
207 idl_free( candidates );
212 switch ( send_search_entry( be, conn, op, e,
213 attrs, attrsonly ) ) {
214 case 0: /* entry sent ok */
217 case 1: /* entry not sent */
219 case -1: /* connection closed */
220 cache_return_entry( &li->li_cache, e );
221 idl_free( candidates );
229 cache_return_entry( &li->li_cache, e );
233 idl_free( candidates );
235 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
238 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
259 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
266 if ( (e = dn2entry( be, base, matched )) == NULL ) {
267 *err = LDAP_NO_SUCH_OBJECT;
271 idl = idl_alloc( 1 );
272 idl_insert( &idl, e->e_id, 1 );
274 cache_return_entry( &li->li_cache, e );
292 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
300 /* get the base object */
301 if ( base != NULL && *base != '\0' && (e = dn2entry( be, base,
302 matched )) == NULL ) {
303 *err = LDAP_NO_SUCH_OBJECT;
308 * modify the filter to be something like this:
310 * parent=baseobject & originalfilter
313 f = (Filter *) ch_malloc( sizeof(Filter) );
315 f->f_choice = LDAP_FILTER_AND;
316 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
317 f->f_and->f_choice = LDAP_FILTER_EQUALITY;
318 f->f_and->f_ava.ava_type = strdup( "id2children" );
319 sprintf( buf, "%d", e != NULL ? e->e_id : 0 );
320 f->f_and->f_ava.ava_value.bv_val = strdup( buf );
321 f->f_and->f_ava.ava_value.bv_len = strlen( buf );
322 f->f_and->f_next = filter;
324 /* from here, it's just like subtree_candidates */
325 candidates = subtree_candidates( be, conn, op, base, f, attrs,
326 attrsonly, matched, e, err, 0 );
328 /* free up just the filter stuff we allocated above */
329 f->f_and->f_next = NULL;
332 return( candidates );
350 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
355 * get the base object - unless we already have it (from one-level).
356 * also, unless this is a one-level search or a subtree search
357 * starting at the very top of our subtree, we need to modify the
358 * filter to be something like this:
360 * dn=*baseobjectdn & (originalfilter | ref=*)
362 * the "objectclass=referral" part is used to select referrals to return
368 if ( base != NULL && *base != '\0' && (e = dn2entry( be, base,
369 matched )) == NULL ) {
370 *err = LDAP_NO_SUCH_OBJECT;
374 f = (Filter *) ch_malloc( sizeof(Filter) );
376 f->f_choice = LDAP_FILTER_OR;
377 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
378 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
379 f->f_or->f_avtype = strdup( "objectclass" );
380 /* Patch to use normalized uppercase */
381 f->f_or->f_avvalue.bv_val = strdup( "REFERRAL" );
382 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
383 f->f_or->f_next = filter;
386 if ( ! be_issuffix( be, base ) ) {
387 f = (Filter *) ch_malloc( sizeof(Filter) );
389 f->f_choice = LDAP_FILTER_AND;
390 f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
391 f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
392 f->f_and->f_sub_type = strdup( "dn" );
393 f->f_and->f_sub_initial = NULL;
394 f->f_and->f_sub_any = NULL;
395 f->f_and->f_sub_final = strdup( base );
396 value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
397 f->f_and->f_next = filter;
402 candidates = filter_candidates( be, filter );
404 /* free up just the parts we allocated above */
406 f->f_and->f_next = NULL;
411 cache_return_entry( &li->li_cache, e );
414 return( candidates );