1 /* search.c - search operation */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
16 static int base_candidate(
20 static int search_candidates(
29 static ID idl_first( ID *ids, ID *cursor );
30 static ID idl_next( ID *ids, ID *cursor );
44 const char *filterstr,
48 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
51 const char *text = NULL;
54 ID candidates[BDB_IDL_SIZE];
56 struct berval **v2refs = NULL;
57 Entry *matched = NULL;
58 char *realbase = NULL;
62 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
65 manageDSAit = get_manageDSAit( op );
68 /* get entry with reader lock */
69 if ( deref & LDAP_DEREF_FINDING ) {
70 e = deref_dn_r( be, nbase, &err, &matched, &text );
76 rc = dn2entry_r( be, NULL, nbase, &e, &matched );
84 send_ldap_result( conn, op, rc=LDAP_OTHER,
85 NULL, "internal error", NULL, NULL );
90 char *matched_dn = NULL;
91 struct berval **refs = NULL;
93 if ( matched != NULL ) {
94 matched_dn = ch_strdup( matched->e_dn );
96 refs = is_entry_referral( matched )
97 ? get_entry_referrals( be, conn, op, matched )
101 refs = default_referral;
104 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
105 matched_dn, text, refs, NULL );
107 if( matched != NULL ) {
108 ber_bvecfree( refs );
110 bdb_entry_return( be, matched );
116 if (!manageDSAit && is_entry_referral( e ) ) {
117 /* entry is a referral, don't allow add */
118 char *matched_dn = ch_strdup( e->e_dn );
119 struct berval **refs = get_entry_referrals( be,
122 bdb_entry_return( be, e );
124 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
127 send_ldap_result( conn, op, LDAP_REFERRAL,
128 matched_dn, NULL, refs, NULL );
130 ber_bvecfree( refs );
136 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
137 tlimit = -1; /* allow root to set no limit */
139 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
140 be->be_timelimit : tlimit;
141 stoptime = op->o_time + tlimit;
144 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
145 slimit = -1; /* allow root to set no limit */
147 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
148 be->be_sizelimit : slimit;
151 if ( scope == LDAP_SCOPE_BASE ) {
152 rc = base_candidate( be, e, candidates );
155 rc = search_candidates( be, e, filter,
156 scope, deref, manageDSAit, candidates );
159 /* need normalized dn below */
160 realbase = ch_strdup( e->e_ndn );
162 /* start cursor at base entry's id */
165 bdb_entry_return( be, e );
167 if ( candidates[0] == 0 ) {
168 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
171 send_search_result( conn, op,
173 NULL, NULL, NULL, NULL, 0 );
179 for ( id = idl_first( candidates, &cursor );
181 id = idl_next( candidates, &cursor ) )
185 /* check for abandon */
186 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
187 abandon = op->o_abandon;
188 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
195 /* check time limit */
196 if ( tlimit != -1 && slap_get_time() > stoptime ) {
197 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
198 NULL, NULL, v2refs, NULL, nentries );
202 /* get the entry with reader lock */
203 rc = bdb_id2entry( be, NULL, id, &e );
206 Debug( LDAP_DEBUG_TRACE,
207 "bdb_search: candidate %ld not found\n",
214 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
219 e = deref_entry_r( be, e, &err, &matched, &text );
226 if( e->e_id == id ) {
231 /* need to skip alias which deref into scope */
232 if( scope & LDAP_SCOPE_ONELEVEL ) {
233 char *pdn = dn_parent( NULL, e->e_ndn );
235 if( strcmp( pdn, realbase ) ) {
242 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
243 /* alias is within scope */
244 Debug( LDAP_DEBUG_TRACE,
245 "bdb_search: \"%s\" in subtree\n",
255 * if it's a referral, add it to the list of referrals. only do
256 * this for non-base searches, and don't check the filter
257 * explicitly here since it's only a candidate anyway.
259 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
260 is_entry_referral( e ) )
262 struct berval **refs = get_entry_referrals(
265 send_search_reference( be, conn, op,
266 e, refs, scope, NULL, &v2refs );
268 ber_bvecfree( refs );
273 /* if it matches the filter and scope, send it */
274 rc = test_filter( be, conn, op, e, filter );
275 if ( rc == LDAP_COMPARE_TRUE ) {
279 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
280 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
281 (void) dn_normalize( dn );
282 scopeok = (dn == realbase)
284 : (strcmp( dn, realbase ) ? 0 : 1 );
288 scopeok = (realbase == NULL || *realbase == '\0');
291 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
292 dn = ch_strdup( e->e_ndn );
293 scopeok = dn_issuffix( dn, realbase );
301 /* check size limit */
302 if ( --slimit == -1 ) {
303 bdb_entry_return( be, e );
304 send_search_result( conn, op,
305 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
306 v2refs, NULL, nentries );
311 int result = send_search_entry( be, conn, op,
312 e, attrs, attrsonly, NULL);
315 case 0: /* entry sent ok */
318 case 1: /* entry not sent */
320 case -1: /* connection closed */
321 bdb_entry_return( be, e );
327 Debug( LDAP_DEBUG_TRACE,
328 "bdb_search: %ld scope not okay\n",
332 Debug( LDAP_DEBUG_TRACE,
333 "bdb_search: %ld does match filter\n",
339 /* free reader lock */
340 bdb_entry_return( be, e );
343 ldap_pvt_thread_yield();
345 send_search_result( conn, op,
346 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
347 NULL, NULL, v2refs, NULL, nentries );
352 ber_bvecfree( v2refs );
353 if( realbase ) ch_free( realbase );
359 static int base_candidate(
364 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
365 e->e_dn, (long) e->e_id, 0);
372 static int search_candidates(
381 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" (0x%08lx)\n",
382 e->e_dn, (long) e->e_id, 0);
384 /* return a RANGE IDL for now */
387 ids[2] = e->e_id+128;
392 static ID idl_first( ID *ids, ID *cursor )
401 if ( BDB_IDL_IS_RANGE( ids ) ) {
402 if( *cursor < ids[1] ) {
408 pos = bdb_idl_search( ids, *cursor );
418 static ID idl_next( ID *ids, ID *cursor )
420 if ( BDB_IDL_IS_RANGE( ids ) ) {
421 if( ids[2] < ++(*cursor) ) {
427 if ( *cursor < ids[0] ) {
428 return ids[(*cursor)++];