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 struct berval *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 BVarray v2refs = NULL;
55 Entry *matched = NULL;
56 struct berval realbase = { 0, 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;
98 if ( matched != NULL ) {
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 bvarray_free( erefs );
116 refs = referral_rewrite( default_referral,
120 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
121 matched_dn ? matched_dn->bv_val : NULL, text, refs, NULL );
123 if ( refs ) bvarray_free( refs );
124 if ( matched_dn ) 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 BVarray erefs = get_entry_referrals( be, conn, op, e );
134 bdb_entry_return( be, e );
138 refs = referral_rewrite( erefs, matched_dn,
140 bvarray_free( erefs );
143 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
146 send_ldap_result( conn, op, LDAP_REFERRAL,
148 refs ? NULL : "bad referral object",
151 bvarray_free( refs );
152 ber_bvfree( matched_dn );
156 /* if not root, get appropriate limits */
157 if ( be_isroot( be, &op->o_ndn ) ) {
160 ( void ) get_limits( be, &op->o_ndn, &limit );
163 /* The time/size limits come first because they require very little
164 * effort, so there's no chance the candidates are selected and then
165 * the request is not honored only because of time/size constraints */
167 /* if no time limit requested, use soft limit (unless root!) */
170 tlimit = -1; /* allow root to set no limit */
178 /* if no limit is required, use soft limit */
180 tlimit = limit->lms_t_soft;
182 /* if requested limit higher than hard limit, abort */
183 } else if ( tlimit > limit->lms_t_hard ) {
184 /* no hard limit means use soft instead */
185 if ( limit->lms_t_hard == 0 ) {
186 tlimit = limit->lms_t_soft;
188 /* positive hard limit means abort */
189 } else if ( limit->lms_t_hard > 0 ) {
190 send_search_result( conn, op,
191 LDAP_UNWILLING_TO_PERFORM,
192 NULL, NULL, NULL, NULL, 0 );
197 /* negative hard limit means no limit */
200 /* if no limit is required, use soft limit */
202 slimit = limit->lms_s_soft;
204 /* if requested limit higher than hard limit, abort */
205 } else if ( slimit > limit->lms_s_hard ) {
206 /* no hard limit means use soft instead */
207 if ( limit->lms_s_hard == 0 ) {
208 slimit = limit->lms_s_soft;
210 /* positive hard limit means abort */
211 } else if ( limit->lms_s_hard > 0 ) {
212 send_search_result( conn, op,
213 LDAP_UNWILLING_TO_PERFORM,
214 NULL, NULL, NULL, NULL, 0 );
219 /* negative hard limit means no limit */
223 /* compute it anyway; root does not use it */
224 stoptime = op->o_time + tlimit;
226 /* select candidates */
227 if ( scope == LDAP_SCOPE_BASE ) {
228 rc = base_candidate( be, e, candidates );
231 BDB_IDL_ALL( bdb, candidates );
232 rc = search_candidates( be, e, filter,
233 scope, deref, manageDSAit, candidates );
236 /* need normalized dn below */
237 ber_dupbv( &realbase, &e->e_nname );
239 /* start cursor at base entry's id
240 * FIXME: hack to make "" base work */
241 cursor = e->e_id == NOID ? 1 : e->e_id;
243 if ( e != &slap_entry_root ) {
244 bdb_entry_return( be, e );
248 if ( candidates[0] == 0 ) {
249 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
252 send_search_result( conn, op,
254 NULL, NULL, NULL, NULL, 0 );
260 /* if not root and candidates exceed to-be-checked entries, abort */
261 if ( !isroot && limit->lms_s_unchecked != -1 ) {
262 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
263 send_search_result( conn, op,
264 LDAP_UNWILLING_TO_PERFORM,
265 NULL, NULL, NULL, NULL, 0 );
271 for ( id = bdb_idl_first( candidates, &cursor );
273 id = bdb_idl_next( candidates, &cursor ) )
277 /* check for abandon */
278 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
279 abandon = op->o_abandon;
280 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
287 /* check time limit */
288 if ( tlimit != -1 && slap_get_time() > stoptime ) {
289 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
290 NULL, NULL, v2refs, NULL, nentries );
294 /* get the entry with reader lock */
295 rc = bdb_id2entry( be, NULL, id, &e );
298 if( !BDB_IDL_IS_RANGE(candidates) ) {
299 /* only complain for non-range IDLs */
300 Debug( LDAP_DEBUG_TRACE,
301 "bdb_search: candidate %ld not found\n",
309 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
314 e = deref_entry_r( be, e, &err, &matched, &text );
321 if( e->e_id == id ) {
326 /* need to skip alias which deref into scope */
327 if( scope & LDAP_SCOPE_ONELEVEL ) {
328 char *pdn = dn_parent( NULL, e->e_ndn );
330 if( strcmp( pdn, realbase.bv_val ) ) {
335 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
336 /* alias is within scope */
337 Debug( LDAP_DEBUG_TRACE,
338 "bdb_search: \"%s\" in subtree\n",
348 * if it's a referral, add it to the list of referrals. only do
349 * this for non-base searches, and don't check the filter
350 * explicitly here since it's only a candidate anyway.
352 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
353 is_entry_referral( e ) )
355 BVarray erefs = get_entry_referrals(
357 BVarray refs = referral_rewrite( erefs,
359 scope == LDAP_SCOPE_SUBTREE
363 send_search_reference( be, conn, op,
364 e, refs, NULL, &v2refs );
366 bvarray_free( refs );
371 /* if it matches the filter and scope, send it */
372 rc = test_filter( be, conn, op, e, filter );
373 if ( rc == LDAP_COMPARE_TRUE ) {
377 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
378 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
379 scopeok = (dn == realbase.bv_val)
381 : (strcmp( dn, realbase.bv_val ) ? 0 : 1 );
383 scopeok = (realbase.bv_len == 0);
386 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
387 scopeok = dnIsSuffix( &e->e_nname, &realbase );
394 /* check size limit */
395 if ( --slimit == -1 ) {
396 bdb_entry_return( be, e );
398 send_search_result( conn, op,
399 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
400 v2refs, NULL, nentries );
405 int result = send_search_entry( be, conn, op,
406 e, attrs, attrsonly, NULL);
409 case 0: /* entry sent ok */
412 case 1: /* entry not sent */
414 case -1: /* connection closed */
415 bdb_entry_return( be, e );
422 Debug( LDAP_DEBUG_TRACE,
423 "bdb_search: %ld scope not okay\n",
427 Debug( LDAP_DEBUG_TRACE,
428 "bdb_search: %ld does match filter\n",
434 /* free reader lock */
435 bdb_entry_return( be, e );
438 ldap_pvt_thread_yield();
440 send_search_result( conn, op,
441 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
442 NULL, NULL, v2refs, NULL, nentries );
447 if( v2refs ) bvarray_free( v2refs );
448 if( realbase.bv_val ) ch_free( realbase.bv_val );
454 static int base_candidate(
459 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
460 e->e_dn, (long) e->e_id, 0);
467 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
468 * doesn't count, we're looking for explicit values.
470 static int oc_filter(
476 switch(f->f_choice) {
477 case LDAP_FILTER_EQUALITY:
478 case LDAP_FILTER_APPROX:
479 if (f->f_av_desc == slap_schema.si_ad_objectClass)
483 case LDAP_FILTER_SUBSTRINGS:
484 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
488 case LDAP_FILTER_AND:
490 for (f=f->f_and; f; f=f->f_next)
491 if ((rc = oc_filter(f)))
500 static int search_candidates(
510 Filter f, fand, rf, xf;
511 ID tmp[BDB_IDL_UM_SIZE];
512 AttributeAssertion aa_ref;
513 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
516 AttributeAssertion aa_alias;
519 Debug(LDAP_DEBUG_TRACE,
520 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
521 e->e_dn, (long) e->e_id, scope );
524 xf.f_choice = LDAP_FILTER_OR;
527 /* If the user's filter doesn't mention objectClass, or if
528 * it just uses objectClass=*, these clauses are redundant.
530 if (oc_filter(filter)) {
531 if( !manageDSAit ) { /* match referrals */
532 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
533 rf.f_choice = LDAP_FILTER_EQUALITY;
535 rf.f_av_desc = slap_schema.si_ad_objectClass;
536 rf.f_av_value = bv_ref;
542 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
543 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
544 af.f_choice = LDAP_FILTER_EQUALITY;
545 af.f_ava = &aa_alias;
546 af.f_av_desc = slap_schema.si_ad_objectClass;
547 af.f_av_value = bv_alias;
555 f.f_choice = LDAP_FILTER_AND;
557 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
558 ? SLAPD_FILTER_DN_SUBTREE
559 : SLAPD_FILTER_DN_ONE;
560 fand.f_dn = &e->e_nname;
561 fand.f_next = xf.f_or == filter ? filter : &xf ;
564 #ifdef BDB_FILTER_INDICES
565 rc = bdb_filter_candidates( be, &f, ids, tmp );
567 /* FIXME: Original code:
568 BDB_IDL_ID( bdb, ids, e->e_id );
569 * this is a hack to make "" base work; when bdb_filter_candidates
570 * is used this should not be needed any more */
571 BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
575 Debug(LDAP_DEBUG_TRACE,
576 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
578 (long) BDB_IDL_FIRST(ids),
579 (long) BDB_IDL_LAST(ids) );