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 static int search_candidates(
28 static ID idl_first( ID *ids, ID *cursor );
29 static ID idl_next( ID *ids, ID *cursor );
43 const char *filterstr,
48 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
50 const char *text = NULL;
52 ID candidates[BDB_IDL_SIZE];
55 struct berval **v2refs = NULL;
56 Entry *matched = NULL;
57 char *realbase = NULL;
59 int manageDSAit = get_manageDSAit( op );
61 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
64 /* get entry with reader lock */
65 if ( deref & LDAP_DEREF_FINDING ) {
66 e = deref_dn_r( be, nbase, &err, &matched, &text );
72 rc = dn2entry_r( be, NULL, nbase, &e, &matched );
80 send_ldap_result( conn, op, rc=LDAP_OTHER,
81 NULL, "internal error", NULL, NULL );
86 char *matched_dn = NULL;
87 struct berval **refs = NULL;
89 if ( matched != NULL ) {
90 matched_dn = ch_strdup( matched->e_dn );
92 refs = is_entry_referral( matched )
93 ? get_entry_referrals( be, conn, op, matched )
97 refs = default_referral;
100 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
101 matched_dn, text, refs, NULL );
103 if( matched != NULL ) {
104 ber_bvecfree( refs );
106 bdb_entry_return( be, matched );
112 if (!manageDSAit && is_entry_referral( e ) ) {
113 /* entry is a referral, don't allow add */
114 char *matched_dn = ch_strdup( e->e_dn );
115 struct berval **refs = get_entry_referrals( be,
118 bdb_entry_return( be, e );
120 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
123 send_ldap_result( conn, op, LDAP_REFERRAL,
124 matched_dn, NULL, refs, NULL );
126 ber_bvecfree( refs );
132 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
133 tlimit = -1; /* allow root to set no limit */
135 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
136 be->be_timelimit : tlimit;
137 stoptime = op->o_time + tlimit;
140 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
141 slimit = -1; /* allow root to set no limit */
143 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
144 be->be_sizelimit : slimit;
147 if ( scope == LDAP_SCOPE_BASE ) {
148 rc = base_candidate( be, e, candidates );
151 rc = search_candidates( be, e, filter,
152 scope, deref, manageDSAit, candidates );
155 /* need normalized dn below */
156 realbase = ch_strdup( e->e_ndn );
158 /* start cursor at base entry's id */
161 bdb_entry_return( be, e );
163 if ( candidates[0] == 0 ) {
164 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
167 send_search_result( conn, op,
169 NULL, NULL, NULL, NULL, 0 );
175 for ( id = idl_first( candidates, &cursor );
177 id = idl_next( candidates, &cursor ) )
181 /* check for abandon */
182 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
183 abandon = op->o_abandon;
184 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
191 /* check time limit */
192 if ( tlimit != -1 && slap_get_time() > stoptime ) {
193 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
194 NULL, NULL, v2refs, NULL, nentries );
198 /* get the entry with reader lock */
199 rc = bdb_id2entry( be, NULL, id, &e );
202 Debug( LDAP_DEBUG_TRACE,
203 "bdb_search: candidate %ld not found\n",
210 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
215 e = deref_entry_r( be, e, &err, &matched, &text );
222 if( e->e_id == id ) {
227 /* need to skip alias which deref into scope */
228 if( scope & LDAP_SCOPE_ONELEVEL ) {
229 char *pdn = dn_parent( NULL, e->e_ndn );
231 if( strcmp( pdn, realbase ) ) {
238 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
239 /* alias is within scope */
240 Debug( LDAP_DEBUG_TRACE,
241 "bdb_search: \"%s\" in subtree\n",
251 * if it's a referral, add it to the list of referrals. only do
252 * this for non-base searches, and don't check the filter
253 * explicitly here since it's only a candidate anyway.
255 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
256 is_entry_referral( e ) )
258 struct berval **refs = get_entry_referrals(
261 send_search_reference( be, conn, op,
262 e, refs, scope, NULL, &v2refs );
264 ber_bvecfree( refs );
269 /* if it matches the filter and scope, send it */
270 rc = test_filter( be, conn, op, e, filter );
271 if ( rc == LDAP_COMPARE_TRUE ) {
275 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
276 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
277 (void) dn_normalize( dn );
278 scopeok = (dn == realbase)
280 : (strcmp( dn, realbase ) ? 0 : 1 );
284 scopeok = (realbase == NULL || *realbase == '\0');
287 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
288 dn = ch_strdup( e->e_ndn );
289 scopeok = dn_issuffix( dn, realbase );
297 /* check size limit */
298 if ( --slimit == -1 ) {
299 bdb_entry_return( be, e );
300 send_search_result( conn, op,
301 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
302 v2refs, NULL, nentries );
307 int result = send_search_entry( be, conn, op,
308 e, attrs, attrsonly, NULL);
311 case 0: /* entry sent ok */
314 case 1: /* entry not sent */
316 case -1: /* connection closed */
317 bdb_entry_return( be, e );
323 Debug( LDAP_DEBUG_TRACE,
324 "bdb_search: %ld scope not okay\n",
328 Debug( LDAP_DEBUG_TRACE,
329 "bdb_search: %ld does match filter\n",
335 /* free reader lock */
336 bdb_entry_return( be, e );
339 ldap_pvt_thread_yield();
341 send_search_result( conn, op,
342 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
343 NULL, NULL, v2refs, NULL, nentries );
348 ber_bvecfree( v2refs );
349 if( realbase ) ch_free( realbase );
355 static int base_candidate(
360 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\" (0x08lx)\n",
361 e->e_dn, (long) e->e_id, 0);
368 static int search_candidates(
377 Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" (0x08lx)\n",
378 e->e_dn, (long) e->e_id, 0);
384 static ID idl_first( ID *ids, ID *cursor )
393 if ( BDB_IS_ALLIDS( ids ) ) {
397 pos = bdb_idl_search( ids, *cursor );
407 static ID idl_next( ID *ids, ID *cursor )
409 if ( BDB_IS_ALLIDS( ids ) ) {
410 if( ++(*cursor) < ids[1] ) {
416 if ( *cursor < ids[0] ) {
417 return ids[(*cursor)++];