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_UM_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 );
68 if ( *nbase == '\0' ) {
69 /* DIT root special case */
70 e = (Entry *) &slap_entry_root;
74 /* get entry with reader lock */
75 if ( deref & LDAP_DEREF_FINDING ) {
76 e = deref_dn_r( be, nbase, &err, &matched, &text );
81 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
89 send_ldap_result( conn, op, rc=LDAP_OTHER,
90 NULL, "internal error", NULL, NULL );
95 char *matched_dn = NULL;
96 struct berval **refs = NULL;
98 if ( matched != NULL ) {
99 struct berval **erefs;
100 matched_dn = ch_strdup( matched->e_dn );
102 erefs = is_entry_referral( matched )
103 ? get_entry_referrals( be, conn, op, matched,
107 bdb_entry_return( be, matched );
111 refs = referral_rewrite( erefs, matched_dn,
113 ber_bvecfree( erefs );
117 refs = referral_rewrite( default_referral,
121 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
122 matched_dn, text, refs, NULL );
124 ber_bvecfree( refs );
130 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
131 /* entry is a referral, don't allow add */
132 char *matched_dn = ch_strdup( e->e_dn );
133 struct berval **erefs = get_entry_referrals( be,
134 conn, op, e, base, scope );
135 struct berval **refs = NULL;
137 bdb_entry_return( be, e );
141 refs = referral_rewrite( erefs, matched_dn,
143 ber_bvecfree( erefs );
146 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
149 send_ldap_result( conn, op, LDAP_REFERRAL,
150 matched_dn, refs ? NULL : "bad referral object",
153 ber_bvecfree( refs );
159 /* if not root, get appropriate limits */
160 if ( be_isroot( be, op->o_ndn ) ) {
163 ( void ) get_limits( be, op->o_ndn, &limit );
166 /* The time/size limits come first because they require very little
167 * effort, so there's no chance the candidates are selected and then
168 * the request is not honored only because of time/size constraints */
170 /* if no time limit requested, use soft limit (unless root!) */
173 tlimit = -1; /* allow root to set no limit */
181 /* if no limit is required, use soft limit */
183 tlimit = limit->lms_t_soft;
185 /* if requested limit higher than hard limit, abort */
186 } else if ( tlimit > limit->lms_t_hard ) {
187 /* no hard limit means use soft instead */
188 if ( limit->lms_t_hard == 0 ) {
189 tlimit = limit->lms_t_soft;
191 /* positive hard limit means abort */
192 } else if ( limit->lms_t_hard > 0 ) {
193 send_search_result( conn, op,
194 LDAP_UNWILLING_TO_PERFORM,
195 NULL, NULL, NULL, NULL, 0 );
200 /* negative hard limit means no limit */
203 /* if no limit is required, use soft limit */
205 slimit = limit->lms_s_soft;
207 /* if requested limit higher than hard limit, abort */
208 } else if ( slimit > limit->lms_s_hard ) {
209 /* no hard limit means use soft instead */
210 if ( limit->lms_s_hard == 0 ) {
211 slimit = limit->lms_s_soft;
213 /* positive hard limit means abort */
214 } else if ( limit->lms_s_hard > 0 ) {
215 send_search_result( conn, op,
216 LDAP_UNWILLING_TO_PERFORM,
217 NULL, NULL, NULL, NULL, 0 );
222 /* negative hard limit means no limit */
226 /* compute it anyway; root does not use it */
227 stoptime = op->o_time + tlimit;
229 /* select candidates */
230 if ( scope == LDAP_SCOPE_BASE ) {
231 rc = base_candidate( be, e, candidates );
234 rc = search_candidates( be, e, filter,
235 scope, deref, manageDSAit, candidates );
238 /* need normalized dn below */
239 realbase = ch_strdup( e->e_ndn );
241 /* start cursor at base entry's id
242 * FIXME: hack to make "" base work */
243 cursor = e->e_id == NOID ? 1 : e->e_id;
245 if ( e != &slap_entry_root ) {
246 bdb_entry_return( be, e );
250 if ( candidates[0] == 0 ) {
251 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
254 send_search_result( conn, op,
256 NULL, NULL, NULL, NULL, 0 );
262 /* if not root and candidates exceed to-be-checked entries, abort */
263 if ( !isroot && limit->lms_s_unchecked != -1 ) {
264 if ( BDB_IDL_N(candidates) > limit->lms_s_unchecked ) {
265 send_search_result( conn, op,
266 LDAP_UNWILLING_TO_PERFORM,
267 NULL, NULL, NULL, NULL, 0 );
273 for ( id = bdb_idl_first( candidates, &cursor );
275 id = bdb_idl_next( candidates, &cursor ) )
279 /* check for abandon */
280 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
281 abandon = op->o_abandon;
282 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
289 /* check time limit */
290 if ( tlimit != -1 && slap_get_time() > stoptime ) {
291 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
292 NULL, NULL, v2refs, NULL, nentries );
296 /* get the entry with reader lock */
297 rc = bdb_id2entry( be, NULL, id, &e );
300 if( !BDB_IDL_IS_RANGE(candidates) ) {
301 /* only complain for non-range IDLs */
302 Debug( LDAP_DEBUG_TRACE,
303 "bdb_search: candidate %ld not found\n",
311 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
316 e = deref_entry_r( be, e, &err, &matched, &text );
323 if( e->e_id == id ) {
328 /* need to skip alias which deref into scope */
329 if( scope & LDAP_SCOPE_ONELEVEL ) {
330 char *pdn = dn_parent( NULL, e->e_ndn );
332 if( strcmp( pdn, realbase ) ) {
339 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
340 /* alias is within scope */
341 Debug( LDAP_DEBUG_TRACE,
342 "bdb_search: \"%s\" in subtree\n",
352 * if it's a referral, add it to the list of referrals. only do
353 * this for non-base searches, and don't check the filter
354 * explicitly here since it's only a candidate anyway.
356 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
357 is_entry_referral( e ) )
359 struct berval **refs = get_entry_referrals(
360 be, conn, op, e, NULL, scope );
362 send_search_reference( be, conn, op,
363 e, refs, NULL, &v2refs );
365 ber_bvecfree( refs );
370 /* if it matches the filter and scope, send it */
371 rc = test_filter( be, conn, op, e, filter );
372 if ( rc == LDAP_COMPARE_TRUE ) {
376 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
377 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
378 (void) dn_normalize( dn );
379 scopeok = (dn == realbase)
381 : (strcmp( dn, realbase ) ? 0 : 1 );
385 scopeok = (realbase == NULL || *realbase == '\0');
388 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
389 dn = ch_strdup( e->e_ndn );
390 scopeok = dn_issuffix( dn, realbase );
398 /* check size limit */
399 if ( --slimit == -1 ) {
400 bdb_entry_return( be, e );
402 send_search_result( conn, op,
403 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
404 v2refs, NULL, nentries );
409 int result = send_search_entry( be, conn, op,
410 e, attrs, attrsonly, NULL);
413 case 0: /* entry sent ok */
416 case 1: /* entry not sent */
418 case -1: /* connection closed */
419 bdb_entry_return( be, e );
426 Debug( LDAP_DEBUG_TRACE,
427 "bdb_search: %ld scope not okay\n",
431 Debug( LDAP_DEBUG_TRACE,
432 "bdb_search: %ld does match filter\n",
438 /* free reader lock */
439 bdb_entry_return( be, e );
442 ldap_pvt_thread_yield();
444 send_search_result( conn, op,
445 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
446 NULL, NULL, v2refs, NULL, nentries );
451 ber_bvecfree( v2refs );
452 if( realbase ) ch_free( realbase );
458 static int base_candidate(
463 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
464 e->e_dn, (long) e->e_id, 0);
471 static int search_candidates(
481 Filter f, fand, rf, xf;
482 AttributeAssertion aa_ref;
483 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
486 AttributeAssertion aa_alias;
489 Debug(LDAP_DEBUG_TRACE,
490 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
491 e->e_dn, (long) e->e_id, scope );
494 xf.f_choice = LDAP_FILTER_OR;
498 /* match referrals */
499 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
500 rf.f_choice = LDAP_FILTER_EQUALITY;
502 rf.f_av_desc = slap_schema.si_ad_objectClass;
503 rf.f_av_value = &bv_ref;
509 if( deref & LDAP_DEREF_SEARCHING ) {
511 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
512 af.f_choice = LDAP_FILTER_EQUALITY;
513 af.f_ava = &aa_alias;
514 af.f_av_desc = slap_schema.si_ad_objectClass;
515 af.f_av_value = &bv_alias;
522 f.f_choice = LDAP_FILTER_AND;
524 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
525 ? SLAPD_FILTER_DN_SUBTREE
526 : SLAPD_FILTER_DN_ONE;
527 fand.f_dn = e->e_ndn;
528 fand.f_next = xf.f_or == filter ? filter : &xf ;
531 #ifdef BDB_FILTER_INDICES
532 rc = bdb_filter_candidates( be, &f, ids );
534 /* FIXME: Original code:
535 BDB_IDL_ID( bdb, ids, e->e_id );
536 * this is a hack to make "" base work; when bdb_filter_candidates
537 * is used this should not be needed any more */
538 BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
542 Debug(LDAP_DEBUG_TRACE,
543 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
545 (long) BDB_IDL_FIRST(ids),
546 (long) BDB_IDL_LAST(ids) );