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>
17 static int base_candidate(
21 static int search_candidates(
42 const char *filterstr,
46 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
49 const char *text = NULL;
52 ID candidates[BDB_IDL_SIZE];
54 struct berval **v2refs = NULL;
55 Entry *matched = NULL;
56 char *realbase = NULL;
60 struct slap_limits_set *limit = NULL;
63 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
66 manageDSAit = get_manageDSAit( op );
69 /* get entry with reader lock */
70 if ( deref & LDAP_DEREF_FINDING ) {
71 e = deref_dn_r( be, nbase, &err, &matched, &text );
76 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
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 );
117 if (!manageDSAit && is_entry_referral( e ) ) {
118 /* entry is a referral, don't allow add */
119 char *matched_dn = ch_strdup( e->e_dn );
120 struct berval **refs = get_entry_referrals( be,
123 bdb_entry_return( be, e );
126 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
129 send_ldap_result( conn, op, LDAP_REFERRAL,
130 matched_dn, NULL, refs, NULL );
132 ber_bvecfree( refs );
138 /* if not root, get appropriate limits */
139 if ( be_isroot( be, op->o_ndn ) ) {
142 ( void ) get_limits( be, op->o_ndn, &limit );
145 /* The time/size limits come first because they require very little
146 * effort, so there's no chance the candidates are selected and then
147 * the request is not honored only because of time/size constraints */
149 /* if no time limit requested, use soft limit (unless root!) */
152 tlimit = -1; /* allow root to set no limit */
154 tlimit = limit->lms_t_soft;
157 /* if requested limit higher than hard limit, abort */
158 } else if ( tlimit > limit->lms_t_hard ) {
159 /* no hard limit means use soft instead */
160 if ( limit->lms_t_hard == 0 ) {
161 tlimit = limit->lms_t_soft;
163 /* positive hard limit means abort */
164 } else if ( limit->lms_t_hard > 0 ) {
165 send_search_result( conn, op,
166 LDAP_UNWILLING_TO_PERFORM,
167 NULL, NULL, NULL, NULL, 0 );
172 /* negative hard limit means no limit */
175 /* compute it anyway; root does not use it */
176 stoptime = op->o_time + tlimit;
178 /* if no size limit requested, use soft limit (unless root!) */
181 slimit = -1; /* allow root to set no limit */
183 slimit = limit->lms_s_soft;
186 /* if requested limit higher than hard limit, abort */
187 } else if ( slimit > limit->lms_s_hard ) {
188 /* no hard limit means use soft instead */
189 if ( limit->lms_s_hard == 0 ) {
190 slimit = limit->lms_s_soft;
192 /* positive hard limit means abort */
193 } else if ( limit->lms_s_hard > 0 ) {
194 send_search_result( conn, op,
195 LDAP_UNWILLING_TO_PERFORM,
196 NULL, NULL, NULL, NULL, 0 );
201 /* negative hard limit means no limit */
204 /* select candidates */
205 if ( scope == LDAP_SCOPE_BASE ) {
206 rc = base_candidate( be, e, candidates );
209 rc = search_candidates( be, e, filter,
210 scope, deref, manageDSAit, candidates );
213 /* need normalized dn below */
214 realbase = ch_strdup( e->e_ndn );
216 /* start cursor at base entry's id */
219 bdb_entry_return( be, e );
222 if ( candidates[0] == 0 ) {
223 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
226 send_search_result( conn, op,
228 NULL, NULL, NULL, NULL, 0 );
234 /* if not root and candidates exceed to-be-checked entries, abort */
235 if ( !isroot && limit->lms_s_unchecked != -1 ) {
236 if ( BDB_IDL_N(candidates) > limit->lms_s_unchecked ) {
237 send_search_result( conn, op,
238 LDAP_UNWILLING_TO_PERFORM,
239 NULL, NULL, NULL, NULL, 0 );
245 for ( id = bdb_idl_first( candidates, &cursor );
247 id = bdb_idl_next( candidates, &cursor ) )
251 /* check for abandon */
252 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
253 abandon = op->o_abandon;
254 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
261 /* check time limit */
262 if ( tlimit != -1 && slap_get_time() > stoptime ) {
263 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
264 NULL, NULL, v2refs, NULL, nentries );
268 /* get the entry with reader lock */
269 rc = bdb_id2entry( be, NULL, id, &e );
272 if( !BDB_IDL_IS_RANGE(candidates) ) {
273 /* only complain for non-range IDLs */
274 Debug( LDAP_DEBUG_TRACE,
275 "bdb_search: candidate %ld not found\n",
283 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
288 e = deref_entry_r( be, e, &err, &matched, &text );
295 if( e->e_id == id ) {
300 /* need to skip alias which deref into scope */
301 if( scope & LDAP_SCOPE_ONELEVEL ) {
302 char *pdn = dn_parent( NULL, e->e_ndn );
304 if( strcmp( pdn, realbase ) ) {
311 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
312 /* alias is within scope */
313 Debug( LDAP_DEBUG_TRACE,
314 "bdb_search: \"%s\" in subtree\n",
324 * if it's a referral, add it to the list of referrals. only do
325 * this for non-base searches, and don't check the filter
326 * explicitly here since it's only a candidate anyway.
328 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
329 is_entry_referral( e ) )
331 struct berval **refs = get_entry_referrals(
334 send_search_reference( be, conn, op,
335 e, refs, scope, NULL, &v2refs );
337 ber_bvecfree( refs );
342 /* if it matches the filter and scope, send it */
343 rc = test_filter( be, conn, op, e, filter );
344 if ( rc == LDAP_COMPARE_TRUE ) {
348 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
349 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
350 (void) dn_normalize( dn );
351 scopeok = (dn == realbase)
353 : (strcmp( dn, realbase ) ? 0 : 1 );
357 scopeok = (realbase == NULL || *realbase == '\0');
360 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
361 dn = ch_strdup( e->e_ndn );
362 scopeok = dn_issuffix( dn, realbase );
370 /* check size limit */
371 if ( --slimit == -1 ) {
372 bdb_entry_return( be, e );
374 send_search_result( conn, op,
375 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
376 v2refs, NULL, nentries );
381 int result = send_search_entry( be, conn, op,
382 e, attrs, attrsonly, NULL);
385 case 0: /* entry sent ok */
388 case 1: /* entry not sent */
390 case -1: /* connection closed */
391 bdb_entry_return( be, e );
398 Debug( LDAP_DEBUG_TRACE,
399 "bdb_search: %ld scope not okay\n",
403 Debug( LDAP_DEBUG_TRACE,
404 "bdb_search: %ld does match filter\n",
410 /* free reader lock */
411 bdb_entry_return( be, e );
414 ldap_pvt_thread_yield();
416 send_search_result( conn, op,
417 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
418 NULL, NULL, v2refs, NULL, nentries );
423 ber_bvecfree( v2refs );
424 if( realbase ) ch_free( realbase );
430 static int base_candidate(
435 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
436 e->e_dn, (long) e->e_id, 0);
443 static int search_candidates(
453 Filter f, fand, rf, xf;
454 AttributeAssertion aa_ref;
455 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
458 AttributeAssertion aa_alias;
461 Debug(LDAP_DEBUG_TRACE,
462 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
463 e->e_dn, (long) e->e_id, scope );
466 xf.f_choice = LDAP_FILTER_OR;
470 /* match referrals */
471 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
472 rf.f_choice = LDAP_FILTER_EQUALITY;
474 rf.f_av_desc = slap_schema.si_ad_objectClass;
475 rf.f_av_value = &bv_ref;
481 if( deref & LDAP_DEREF_SEARCHING ) {
483 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
484 af.f_choice = LDAP_FILTER_EQUALITY;
485 af.f_ava = &aa_alias;
486 af.f_av_desc = slap_schema.si_ad_objectClass;
487 af.f_av_value = &bv_alias;
494 f.f_choice = LDAP_FILTER_AND;
496 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
497 ? SLAPD_FILTER_DN_SUBTREE
498 : SLAPD_FILTER_DN_ONE;
499 fand.f_dn = e->e_ndn;
500 fand.f_next = xf.f_or == filter ? filter : &xf ;
503 #ifdef BDB_FILTER_INDICES
506 BDB_IDL_ID( bdb, range, e->e_id );
507 rc = bdb_filter_candidates( be, range, &f, ids );
510 BDB_IDL_ID( bdb, ids, e->e_id );
514 Debug(LDAP_DEBUG_TRACE,
515 "search_candidates: id=%ld first=%ld last=%ld\n",
517 BDB_IDL_IS_RANGE( ids ) ? ids[2] : ids[ids[0]] );