1 /* search.c - ldbm backend search function */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
19 static ID_BLOCK *base_candidate(
20 Backend *be, Entry *e );
22 static ID_BLOCK *search_candidates(
23 Backend *be, Entry *e, Filter *filter,
24 int scope, int deref, int manageDSAit );
44 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
51 struct berval **v2refs = NULL;
52 Entry *matched = NULL;
53 char *realbase = NULL;
55 int manageDSAit = get_manageDSAit( op );
57 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
59 /* get entry with reader lock */
60 if ( deref & LDAP_DEREF_FINDING ) {
61 e = deref_dn_r( be, nbase, &err, &matched, &text );
64 e = dn2entry_r( be, nbase, &matched );
65 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
70 char *matched_dn = NULL;
71 struct berval **refs = NULL;
73 if ( matched != NULL ) {
74 matched_dn = ch_strdup( matched->e_dn );
76 refs = is_entry_referral( matched )
77 ? get_entry_referrals( be, conn, op, matched )
80 cache_return_entry_r( &li->li_cache, matched );
82 refs = default_referral;
85 send_ldap_result( conn, op, err,
86 matched_dn, text, refs, NULL );
88 if( matched != NULL ) {
96 if (!manageDSAit && is_entry_referral( e ) ) {
97 /* entry is a referral, don't allow add */
98 char *matched_dn = ch_strdup( e->e_dn );
99 struct berval **refs = get_entry_referrals( be,
102 cache_return_entry_r( &li->li_cache, e );
104 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
107 send_ldap_result( conn, op, LDAP_REFERRAL,
108 matched_dn, NULL, refs, NULL );
110 ber_bvecfree( refs );
116 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
117 tlimit = -1; /* allow root to set no limit */
119 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
120 be->be_timelimit : tlimit;
121 stoptime = op->o_time + tlimit;
124 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
125 slimit = -1; /* allow root to set no limit */
127 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
128 be->be_sizelimit : slimit;
131 if ( scope == LDAP_SCOPE_BASE ) {
132 candidates = base_candidate( be, e );
135 candidates = search_candidates( be, e, filter,
136 scope, deref, manageDSAit );
139 /* need normalized dn below */
140 realbase = ch_strdup( e->e_ndn );
141 cache_return_entry_r( &li->li_cache, e );
143 if ( candidates == NULL ) {
145 Debug( LDAP_DEBUG_TRACE, "no candidates\n", 0,
148 send_search_result( conn, op,
150 NULL, NULL, NULL, NULL, 0 );
156 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
157 id = idl_nextid( candidates, &cursor ) )
161 /* check for abandon */
162 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
164 if ( op->o_abandon ) {
165 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
170 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
172 /* check time limit */
173 if ( tlimit != -1 && slap_get_time() > stoptime ) {
174 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
175 NULL, NULL, v2refs, NULL, nentries );
180 /* get the entry with reader lock */
181 e = id2entry_r( be, id );
184 Debug( LDAP_DEBUG_ARGS, "search: candidate %ld not found\n",
190 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
195 e = deref_entry_r( be, e, &err, &matched, &text );
202 if( e->e_id == id ) {
207 /* need to skip alias which deref into scope */
208 if( scope & LDAP_SCOPE_ONELEVEL ) {
209 char *pdn = dn_parent( NULL, e->e_ndn );
211 if( strcmp( pdn, realbase ) ) {
218 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
219 /* alias is within scope */
220 Debug( LDAP_DEBUG_ARGS, "search: \"%s\" in subtree\n",
229 * if it's a referral, add it to the list of referrals. only do
230 * this for non-base searches, and don't check the filter
231 * explicitly here since it's only a candidate anyway.
233 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
234 is_entry_referral( e ) )
236 struct berval **refs = get_entry_referrals(
239 send_search_reference( be, conn, op,
240 e, refs, scope, NULL, &v2refs );
242 ber_bvecfree( refs );
247 /* if it matches the filter and scope, send it */
248 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
252 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
253 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
254 (void) dn_normalize( dn );
255 scopeok = (dn == realbase)
257 : (strcmp( dn, realbase ) ? 0 : 1 );
261 scopeok = (realbase == NULL || *realbase == '\0');
264 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
265 dn = ch_strdup( e->e_ndn );
266 scopeok = dn_issuffix( dn, realbase );
274 /* check size limit */
275 if ( --slimit == -1 ) {
276 cache_return_entry_r( &li->li_cache, e );
277 send_search_result( conn, op,
278 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
279 v2refs, NULL, nentries );
285 switch ( send_search_entry( be, conn, op, e,
286 attrs, attrsonly, NULL ) ) {
287 case 0: /* entry sent ok */
290 case 1: /* entry not sent */
292 case -1: /* connection closed */
293 cache_return_entry_r( &li->li_cache, e );
299 Debug( LDAP_DEBUG_TRACE, "candidate %ld scope not okay\n",
303 Debug( LDAP_DEBUG_TRACE, "candidate %ld does match filter\n",
309 /* free reader lock */
310 cache_return_entry_r( &li->li_cache, e );
313 ldap_pvt_thread_yield();
315 send_search_result( conn, op,
316 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
317 NULL, NULL, v2refs, NULL, nentries );
322 if( candidates != NULL )
323 idl_free( candidates );
325 ber_bvecfree( v2refs );
326 if( realbase ) free( realbase );
339 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
342 idl = idl_alloc( 1 );
343 idl_insert( &idl, e->e_id, 1 );
358 ID_BLOCK *candidates;
359 Filter *f, *rf, *af, *lf;
361 Debug(LDAP_DEBUG_TRACE, "search_candidates: base=\"%s\" s=%d d=%d\n",
362 e->e_ndn, scope, deref );
367 /* match referrals */
368 rf = (Filter *) ch_malloc( sizeof(Filter) );
370 rf->f_choice = LDAP_FILTER_OR;
371 rf->f_or = (Filter *) ch_malloc( sizeof(Filter) );
372 rf->f_or->f_choice = LDAP_FILTER_EQUALITY;
373 rf->f_or->f_avtype = ch_strdup( "objectclass" );
374 rf->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
375 rf->f_or->f_avvalue.bv_len = sizeof("REFERRAL")-1;
376 rf->f_or->f_next = filter;
383 if( deref & LDAP_DEREF_SEARCHING ) {
385 af = (Filter *) ch_malloc( sizeof(Filter) );
387 af->f_choice = LDAP_FILTER_OR;
388 af->f_or = (Filter *) ch_malloc( sizeof(Filter) );
389 af->f_or->f_choice = LDAP_FILTER_EQUALITY;
390 af->f_or->f_avtype = ch_strdup( "objectclass" );
391 af->f_or->f_avvalue.bv_val = ch_strdup( "ALIAS" );
392 af->f_or->f_avvalue.bv_len = sizeof("ALIAS")-1;
393 af->f_or->f_next = f;
399 if ( scope == LDAP_SCOPE_SUBTREE ) {
400 lf = (Filter *) ch_malloc( sizeof(Filter) );
402 lf->f_choice = LDAP_FILTER_AND;
403 lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
405 lf->f_and->f_choice = SLAPD_FILTER_DN_SUBTREE;
406 lf->f_and->f_dn = e->e_ndn;
408 lf->f_and->f_next = f;
411 } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
412 lf = (Filter *) ch_malloc( sizeof(Filter) );
414 lf->f_choice = LDAP_FILTER_AND;
415 lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
417 lf->f_and->f_choice = SLAPD_FILTER_DN_ONE;
418 lf->f_and->f_dn = e->e_ndn;
420 lf->f_and->f_next = f;
427 candidates = filter_candidates( be, f );
429 /* free up filter additions we allocated above */
436 af->f_or->f_next = NULL;
441 rf->f_or->f_next = NULL;
445 return( candidates );