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>
15 static int base_candidate(
19 int search_candidates();
21 static ID idl_first( ID *ids, ID *cursor );
22 static ID idl_next( ID *ids, ID *cursor );
36 const char *filterstr,
41 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
43 const char *text = NULL;
45 ID candidates[BDB_IDL_SIZE];
48 struct berval **v2refs = NULL;
49 Entry *matched = NULL;
50 char *realbase = NULL;
52 int manageDSAit = get_manageDSAit( op );
54 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
57 rc = dn2entry_r( be, NULL, nbase, &e, &matched );
64 send_ldap_result( conn, op, rc=LDAP_OTHER,
65 NULL, "internal error", NULL, NULL );
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 )
81 refs = default_referral;
84 send_ldap_result( conn, op, err,
85 matched_dn, text, refs, NULL );
87 if( matched != NULL ) {
90 bdb_entry_return( be, matched );
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 bdb_entry_return( be, e );
104 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
107 send_ldap_result( conn, op, LDAP_REFERRAL,
108 matched_dn, NULL, refs, NULL );
110 ber_bvecfree( refs );
116 if ( is_entry_alias( e ) ) {
118 deref = LDAP_DEREF_NEVER;
121 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
122 tlimit = -1; /* allow root to set no limit */
124 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
125 be->be_timelimit : tlimit;
126 stoptime = op->o_time + tlimit;
129 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
130 slimit = -1; /* allow root to set no limit */
132 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
133 be->be_sizelimit : slimit;
136 if ( scope == LDAP_SCOPE_BASE ) {
137 rc = base_candidate( be, e, candidates );
140 rc = search_candidates( be, e, filter,
141 scope, deref, manageDSAit, candidates );
144 /* need normalized dn below */
145 realbase = ch_strdup( e->e_ndn );
147 /* start cursor at base entry's id */
150 bdb_entry_return( be, e );
152 if ( candidates[0] == 0 ) {
153 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
156 send_search_result( conn, op,
158 NULL, NULL, NULL, NULL, 0 );
164 for ( id = idl_first( candidates, &cursor );
166 id = idl_next( candidates, &cursor ) )
170 /* check for abandon */
171 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
172 abandon = op->o_abandon;
173 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
180 /* check time limit */
181 if ( tlimit != -1 && slap_get_time() > stoptime ) {
182 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
183 NULL, NULL, v2refs, NULL, nentries );
188 /* get the entry with reader lock */
189 rc = bdb_id2entry( be, NULL, id, &e );
192 Debug( LDAP_DEBUG_TRACE,
193 "bdb_search: candidate %ld not found\n",
199 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
204 e = deref_entry_r( be, e, &err, &matched, &text );
211 if( e->e_id == id ) {
216 /* need to skip alias which deref into scope */
217 if( scope & LDAP_SCOPE_ONELEVEL ) {
218 char *pdn = dn_parent( NULL, e->e_ndn );
220 if( strcmp( pdn, realbase ) ) {
227 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
228 /* alias is within scope */
229 Debug( LDAP_DEBUG_TRACE,
230 "bdb_search: \"%s\" in subtree\n",
239 * if it's a referral, add it to the list of referrals. only do
240 * this for non-base searches, and don't check the filter
241 * explicitly here since it's only a candidate anyway.
243 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
244 is_entry_referral( e ) )
246 struct berval **refs = get_entry_referrals(
249 send_search_reference( be, conn, op,
250 e, refs, scope, NULL, &v2refs );
252 ber_bvecfree( refs );
257 /* if it matches the filter and scope, send it */
258 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
262 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
263 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
264 (void) dn_normalize( dn );
265 scopeok = (dn == realbase)
267 : (strcmp( dn, realbase ) ? 0 : 1 );
271 scopeok = (realbase == NULL || *realbase == '\0');
274 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
275 dn = ch_strdup( e->e_ndn );
276 scopeok = dn_issuffix( dn, realbase );
284 /* check size limit */
285 if ( --slimit == -1 ) {
286 bdb_entry_return( be, e );
287 send_search_result( conn, op,
288 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
289 v2refs, NULL, nentries );
295 int result = send_search_entry( be, conn, op,
296 e, attrs, attrsonly, NULL);
299 case 0: /* entry sent ok */
302 case 1: /* entry not sent */
304 case -1: /* connection closed */
305 bdb_entry_return( be, e );
311 Debug( LDAP_DEBUG_TRACE,
312 "bdb_search: %ld scope not okay\n",
316 Debug( LDAP_DEBUG_TRACE,
317 "bdb_search: %ld does match filter\n",
323 /* free reader lock */
324 bdb_entry_return( be, e );
327 ldap_pvt_thread_yield();
329 send_search_result( conn, op,
330 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
331 NULL, NULL, v2refs, NULL, nentries );
336 ber_bvecfree( v2refs );
337 if( realbase ) free( realbase );
343 static int base_candidate(
348 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\" (0x08lx)\n",
349 e->e_dn, (long) e->e_id, 0);
356 static ID idl_first( ID *ids, ID *cursor )
365 if ( BDB_IS_ALLIDS( ids ) ) {
369 pos = bdb_idl_search( ids, *cursor );
379 static ID idl_next( ID *ids, ID *cursor )
381 if ( BDB_IS_ALLIDS( ids ) ) {
382 if( ++(*cursor) < ids[1] ) {
388 if ( *cursor < ids[0] ) {
389 return ids[(*cursor)++];