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,
43 struct berval **attrs,
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->bv_len == 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 struct berval *matched_dn = NULL;
96 struct berval **refs = NULL;
98 if ( matched != NULL ) {
99 struct berval **erefs;
100 matched_dn = ber_bvdup( &matched->e_name );
102 erefs = is_entry_referral( matched )
103 ? get_entry_referrals( be, conn, op, matched )
106 bdb_entry_return( be, matched );
110 refs = referral_rewrite( erefs, matched_dn,
112 ber_bvecfree( erefs );
116 refs = referral_rewrite( default_referral,
120 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
121 matched_dn->bv_val, text, refs, NULL );
123 ber_bvecfree( refs );
124 ber_bvfree( matched_dn );
128 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
129 /* entry is a referral, don't allow add */
130 struct berval *matched_dn = ber_bvdup( &e->e_name );
131 struct berval **erefs = get_entry_referrals( be,
133 struct berval **refs = NULL;
135 bdb_entry_return( be, e );
139 refs = referral_rewrite( erefs, matched_dn,
141 ber_bvecfree( erefs );
144 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
147 send_ldap_result( conn, op, LDAP_REFERRAL,
149 refs ? NULL : "bad referral object",
152 ber_bvecfree( refs );
153 ber_bvfree( matched_dn );
157 /* if not root, get appropriate limits */
158 if ( be_isroot( be, &op->o_ndn ) ) {
161 ( void ) get_limits( be, &op->o_ndn, &limit );
164 /* The time/size limits come first because they require very little
165 * effort, so there's no chance the candidates are selected and then
166 * the request is not honored only because of time/size constraints */
168 /* if no time limit requested, use soft limit (unless root!) */
171 tlimit = -1; /* allow root to set no limit */
179 /* if no limit is required, use soft limit */
181 tlimit = limit->lms_t_soft;
183 /* if requested limit higher than hard limit, abort */
184 } else if ( tlimit > limit->lms_t_hard ) {
185 /* no hard limit means use soft instead */
186 if ( limit->lms_t_hard == 0 ) {
187 tlimit = limit->lms_t_soft;
189 /* positive hard limit means abort */
190 } else if ( limit->lms_t_hard > 0 ) {
191 send_search_result( conn, op,
192 LDAP_UNWILLING_TO_PERFORM,
193 NULL, NULL, NULL, NULL, 0 );
198 /* negative hard limit means no limit */
201 /* if no limit is required, use soft limit */
203 slimit = limit->lms_s_soft;
205 /* if requested limit higher than hard limit, abort */
206 } else if ( slimit > limit->lms_s_hard ) {
207 /* no hard limit means use soft instead */
208 if ( limit->lms_s_hard == 0 ) {
209 slimit = limit->lms_s_soft;
211 /* positive hard limit means abort */
212 } else if ( limit->lms_s_hard > 0 ) {
213 send_search_result( conn, op,
214 LDAP_UNWILLING_TO_PERFORM,
215 NULL, NULL, NULL, NULL, 0 );
220 /* negative hard limit means no limit */
224 /* compute it anyway; root does not use it */
225 stoptime = op->o_time + tlimit;
227 /* select candidates */
228 if ( scope == LDAP_SCOPE_BASE ) {
229 rc = base_candidate( be, e, candidates );
232 BDB_IDL_ALL( bdb, candidates );
233 rc = search_candidates( be, e, filter,
234 scope, deref, manageDSAit, candidates );
237 /* need normalized dn below */
238 realbase = ch_strdup( e->e_ndn );
240 /* start cursor at base entry's id
241 * FIXME: hack to make "" base work */
242 cursor = e->e_id == NOID ? 1 : e->e_id;
244 if ( e != &slap_entry_root ) {
245 bdb_entry_return( be, e );
249 if ( candidates[0] == 0 ) {
250 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
253 send_search_result( conn, op,
255 NULL, NULL, NULL, NULL, 0 );
261 /* if not root and candidates exceed to-be-checked entries, abort */
262 if ( !isroot && limit->lms_s_unchecked != -1 ) {
263 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
264 send_search_result( conn, op,
265 LDAP_UNWILLING_TO_PERFORM,
266 NULL, NULL, NULL, NULL, 0 );
272 for ( id = bdb_idl_first( candidates, &cursor );
274 id = bdb_idl_next( candidates, &cursor ) )
278 /* check for abandon */
279 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
280 abandon = op->o_abandon;
281 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
288 /* check time limit */
289 if ( tlimit != -1 && slap_get_time() > stoptime ) {
290 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
291 NULL, NULL, v2refs, NULL, nentries );
295 /* get the entry with reader lock */
296 rc = bdb_id2entry( be, NULL, id, &e );
299 if( !BDB_IDL_IS_RANGE(candidates) ) {
300 /* only complain for non-range IDLs */
301 Debug( LDAP_DEBUG_TRACE,
302 "bdb_search: candidate %ld not found\n",
310 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
315 e = deref_entry_r( be, e, &err, &matched, &text );
322 if( e->e_id == id ) {
327 /* need to skip alias which deref into scope */
328 if( scope & LDAP_SCOPE_ONELEVEL ) {
329 char *pdn = dn_parent( NULL, e->e_ndn );
331 if( strcmp( pdn, realbase ) ) {
336 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
337 /* alias is within scope */
338 Debug( LDAP_DEBUG_TRACE,
339 "bdb_search: \"%s\" in subtree\n",
349 * if it's a referral, add it to the list of referrals. only do
350 * this for non-base searches, and don't check the filter
351 * explicitly here since it's only a candidate anyway.
353 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
354 is_entry_referral( e ) )
356 struct berval **erefs = get_entry_referrals(
358 struct berval **refs = referral_rewrite( erefs,
360 scope == LDAP_SCOPE_SUBTREE
364 send_search_reference( be, conn, op,
365 e, refs, NULL, &v2refs );
367 ber_bvecfree( refs );
372 /* if it matches the filter and scope, send it */
373 rc = test_filter( be, conn, op, e, filter );
374 if ( rc == LDAP_COMPARE_TRUE ) {
378 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
379 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
380 scopeok = (dn == realbase)
382 : (strcmp( dn, realbase ) ? 0 : 1 );
384 scopeok = (realbase == NULL || *realbase == '\0');
387 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
388 dn = ch_strdup( e->e_ndn );
389 scopeok = dn_issuffix( dn, realbase );
397 /* check size limit */
398 if ( --slimit == -1 ) {
399 bdb_entry_return( be, e );
401 send_search_result( conn, op,
402 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
403 v2refs, NULL, nentries );
408 int result = send_search_entry( be, conn, op,
409 e, attrs, attrsonly, NULL);
412 case 0: /* entry sent ok */
415 case 1: /* entry not sent */
417 case -1: /* connection closed */
418 bdb_entry_return( be, e );
425 Debug( LDAP_DEBUG_TRACE,
426 "bdb_search: %ld scope not okay\n",
430 Debug( LDAP_DEBUG_TRACE,
431 "bdb_search: %ld does match filter\n",
437 /* free reader lock */
438 bdb_entry_return( be, e );
441 ldap_pvt_thread_yield();
443 send_search_result( conn, op,
444 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
445 NULL, NULL, v2refs, NULL, nentries );
450 ber_bvecfree( v2refs );
451 if( realbase ) ch_free( realbase );
457 static int base_candidate(
462 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
463 e->e_dn, (long) e->e_id, 0);
470 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
471 * doesn't count, we're looking for explicit values.
473 static int oc_filter(
479 switch(f->f_choice) {
480 case LDAP_FILTER_EQUALITY:
481 case LDAP_FILTER_APPROX:
482 if (f->f_av_desc == slap_schema.si_ad_objectClass)
486 case LDAP_FILTER_SUBSTRINGS:
487 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
491 case LDAP_FILTER_AND:
493 for (f=f->f_and; f; f=f->f_next)
494 if ((rc = oc_filter(f)))
503 static int search_candidates(
513 Filter f, fand, rf, xf;
514 ID tmp[BDB_IDL_UM_SIZE];
515 AttributeAssertion aa_ref;
516 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
519 AttributeAssertion aa_alias;
522 Debug(LDAP_DEBUG_TRACE,
523 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
524 e->e_dn, (long) e->e_id, scope );
527 xf.f_choice = LDAP_FILTER_OR;
530 /* If the user's filter doesn't mention objectClass, or if
531 * it just uses objectClass=*, these clauses are redundant.
533 if (oc_filter(filter)) {
534 if( !manageDSAit ) { /* match referrals */
535 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
536 rf.f_choice = LDAP_FILTER_EQUALITY;
538 rf.f_av_desc = slap_schema.si_ad_objectClass;
539 rf.f_av_value = &bv_ref;
545 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
546 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
547 af.f_choice = LDAP_FILTER_EQUALITY;
548 af.f_ava = &aa_alias;
549 af.f_av_desc = slap_schema.si_ad_objectClass;
550 af.f_av_value = &bv_alias;
558 f.f_choice = LDAP_FILTER_AND;
560 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
561 ? SLAPD_FILTER_DN_SUBTREE
562 : SLAPD_FILTER_DN_ONE;
563 fand.f_dn = &e->e_nname;
564 fand.f_next = xf.f_or == filter ? filter : &xf ;
567 #ifdef BDB_FILTER_INDICES
568 rc = bdb_filter_candidates( be, &f, ids, tmp );
570 /* FIXME: Original code:
571 BDB_IDL_ID( bdb, ids, e->e_id );
572 * this is a hack to make "" base work; when bdb_filter_candidates
573 * is used this should not be needed any more */
574 BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
578 Debug(LDAP_DEBUG_TRACE,
579 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
581 (long) BDB_IDL_FIRST(ids),
582 (long) BDB_IDL_LAST(ids) );