1 /* search.c - ldbm backend search function */
4 * Copyright 1998-2000 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 );
39 const char *filterstr,
43 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
45 const char *text = NULL;
50 struct berval **v2refs = NULL;
51 Entry *matched = NULL;
52 char *realbase = NULL;
54 int manageDSAit = get_manageDSAit( op );
56 int timelimit = -1, sizelimit = -1;
60 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
61 "ldbm_back_search: enter\n" ));
63 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
67 if ( *nbase == '\0' ) {
68 /* DIT root special case */
69 static const Entry root = { NOID, "", "", NULL, NULL };
72 /* need normalized dn below */
73 realbase = ch_strdup( e->e_ndn );
75 candidates = search_candidates( be, e, filter,
76 scope, deref, manageDSAit );
80 } else if ( deref & LDAP_DEREF_FINDING ) {
81 /* deref dn and get entry with reader lock */
82 e = deref_dn_r( be, nbase, &err, &matched, &text );
84 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
87 /* get entry with reader lock */
88 e = dn2entry_r( be, nbase, &matched );
89 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
94 char *matched_dn = NULL;
95 struct berval **refs = NULL;
97 if ( matched != NULL ) {
98 matched_dn = ch_strdup( matched->e_dn );
100 refs = is_entry_referral( matched )
101 ? get_entry_referrals( be, conn, op, matched )
104 cache_return_entry_r( &li->li_cache, matched );
107 refs = default_referral;
110 send_ldap_result( conn, op, err,
111 matched_dn, text, refs, NULL );
113 if( matched != NULL ) {
114 ber_bvecfree( refs );
121 if (!manageDSAit && is_entry_referral( e ) ) {
122 /* entry is a referral, don't allow add */
123 char *matched_dn = ch_strdup( e->e_dn );
124 struct berval **refs = get_entry_referrals( be,
127 cache_return_entry_r( &li->li_cache, e );
130 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
131 "ldbm_search: entry (%s) is a referral.\n",
134 Debug( LDAP_DEBUG_TRACE,
135 "ldbm_search: entry is referral\n",
140 send_ldap_result( conn, op, LDAP_REFERRAL,
141 matched_dn, NULL, refs, NULL );
143 ber_bvecfree( refs );
149 if ( is_entry_alias( e ) ) {
151 deref = LDAP_DEREF_NEVER;
154 if ( scope == LDAP_SCOPE_BASE ) {
155 candidates = base_candidate( be, e );
158 candidates = search_candidates( be, e, filter,
159 scope, deref, manageDSAit );
162 /* need normalized dn below */
163 realbase = ch_strdup( e->e_ndn );
165 cache_return_entry_r( &li->li_cache, e );
168 if ( candidates == NULL ) {
171 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
172 "ldbm_search: no candidates\n" ));
174 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
179 send_search_result( conn, op,
181 NULL, NULL, NULL, NULL, 0 );
187 if ( be_isroot( be, op->o_ndn ) ) {
190 if ( get_limits( be, op->o_ndn, &timelimit, &sizelimit) ) {
191 timelimit = be->be_timelimit;
192 sizelimit = be->be_sizelimit;
196 if ( tlimit == 0 && isroot ) {
197 tlimit = -1; /* allow root to set no limit */
199 tlimit = (tlimit > timelimit || tlimit < 1) ?
201 stoptime = op->o_time + tlimit;
204 if ( slimit == 0 && isroot ) {
205 slimit = -1; /* allow root to set no limit */
207 slimit = (slimit > sizelimit || slimit < 1) ?
211 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
212 id = idl_nextid( candidates, &cursor ) )
216 /* check for abandon */
217 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
219 if ( op->o_abandon ) {
220 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
225 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
227 /* check time limit */
228 if ( tlimit != -1 && slap_get_time() > stoptime ) {
229 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
230 NULL, NULL, v2refs, NULL, nentries );
235 /* get the entry with reader lock */
236 e = id2entry_r( be, id );
240 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
241 "ldbm_search: candidate %ld not found.\n", id ));
243 Debug( LDAP_DEBUG_TRACE,
244 "ldbm_search: candidate %ld not found\n",
252 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
257 e = deref_entry_r( be, e, &err, &matched, &text );
264 if( e->e_id == id ) {
269 /* need to skip alias which deref into scope */
270 if( scope & LDAP_SCOPE_ONELEVEL ) {
271 char *pdn = dn_parent( NULL, e->e_ndn );
273 if( strcmp( pdn, realbase ) ) {
280 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
281 /* alias is within scope */
283 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
284 "ldbm_search: \"%s\" in subtree\n", e->e_dn ));
286 Debug( LDAP_DEBUG_TRACE,
287 "ldbm_search: \"%s\" in subtree\n",
298 * if it's a referral, add it to the list of referrals. only do
299 * this for non-base searches, and don't check the filter
300 * explicitly here since it's only a candidate anyway.
302 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
303 is_entry_referral( e ) )
305 struct berval **refs = get_entry_referrals(
308 send_search_reference( be, conn, op,
309 e, refs, scope, NULL, &v2refs );
311 ber_bvecfree( refs );
316 /* if it matches the filter and scope, send it */
317 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
321 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
322 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
323 (void) dn_normalize( dn );
324 scopeok = (dn == realbase)
326 : (strcmp( dn, realbase ) ? 0 : 1 );
330 scopeok = (realbase == NULL || *realbase == '\0');
333 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
334 dn = ch_strdup( e->e_ndn );
335 scopeok = dn_issuffix( dn, realbase );
343 /* check size limit */
344 if ( --slimit == -1 ) {
345 cache_return_entry_r( &li->li_cache, e );
346 send_search_result( conn, op,
347 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
348 v2refs, NULL, nentries );
354 int result = send_search_entry(be, conn, op,
355 e, attrs, attrsonly, NULL);
358 case 0: /* entry sent ok */
361 case 1: /* entry not sent */
363 case -1: /* connection closed */
364 cache_return_entry_r( &li->li_cache, e );
371 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
372 "ldbm_search: candidate %ld scope not okay\n", id ));
374 Debug( LDAP_DEBUG_TRACE,
375 "ldbm_search: candidate %ld scope not okay\n",
382 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
383 "ldbm_search: candidate %ld does not match filter\n", id ));
385 Debug( LDAP_DEBUG_TRACE,
386 "ldbm_search: candidate %ld does not match filter\n",
394 /* free reader lock */
395 cache_return_entry_r( &li->li_cache, e );
398 ldap_pvt_thread_yield();
400 send_search_result( conn, op,
401 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
402 NULL, NULL, v2refs, NULL, nentries );
407 if( candidates != NULL )
408 idl_free( candidates );
410 ber_bvecfree( v2refs );
411 if( realbase ) free( realbase );
424 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
425 "base_candidate: base (%s)\n", e->e_dn ));
427 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
432 idl = idl_alloc( 1 );
433 idl_insert( &idl, e->e_id, 1 );
447 ID_BLOCK *candidates;
448 Filter f, fand, rf, af, xf;
449 AttributeAssertion aa_ref, aa_alias;
450 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
451 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
454 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
455 "search_candidates: base (%s) scope %d deref %d\n",
456 e->e_ndn, scope, deref ));
458 Debug(LDAP_DEBUG_TRACE,
459 "search_candidates: base=\"%s\" s=%d d=%d\n",
460 e->e_ndn, scope, deref );
465 xf.f_choice = LDAP_FILTER_OR;
469 /* match referrals */
470 rf.f_choice = LDAP_FILTER_EQUALITY;
472 rf.f_av_desc = slap_schema.si_ad_objectClass;
473 rf.f_av_value = &bv_ref;
478 if( deref & LDAP_DEREF_SEARCHING ) {
480 af.f_choice = LDAP_FILTER_EQUALITY;
481 af.f_ava = &aa_alias;
482 af.f_av_desc = slap_schema.si_ad_objectClass;
483 af.f_av_value = &bv_alias;
489 f.f_choice = LDAP_FILTER_AND;
491 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
492 ? SLAPD_FILTER_DN_SUBTREE
493 : SLAPD_FILTER_DN_ONE;
494 fand.f_dn = e->e_ndn;
495 fand.f_next = xf.f_or == filter ? filter : &xf ;
497 candidates = filter_candidates( be, &f );
499 return( candidates );