1 /* search.c - search operation */
4 * Copyright 1998-2002 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;
48 const char *text = NULL;
51 ID candidates[BDB_IDL_UM_SIZE];
53 BerVarray v2refs = NULL;
54 Entry *matched = NULL;
55 struct berval realbase = { 0, NULL };
59 struct slap_limits_set *limit = NULL;
66 LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
68 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
72 manageDSAit = get_manageDSAit( op );
74 LOCK_ID (bdb->bi_dbenv, &locker );
76 if ( nbase->bv_len == 0 ) {
77 /* DIT root special case */
78 e = (Entry *) &slap_entry_root;
82 /* get entry with reader lock */
83 if ( deref & LDAP_DEREF_FINDING ) {
84 e = deref_dn_r( be, nbase-, &err, &matched, &text );
90 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
99 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
101 if (matched != NULL) {
102 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
104 send_ldap_result( conn, op, LDAP_BUSY,
105 NULL, "ldap server busy", NULL, NULL );
106 LOCK_ID_FREE (bdb->bi_dbenv, locker );
108 case DB_LOCK_DEADLOCK:
109 case DB_LOCK_NOTGRANTED:
113 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
115 if (matched != NULL) {
116 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
118 send_ldap_result( conn, op, rc=LDAP_OTHER,
119 NULL, "internal error", NULL, NULL );
120 LOCK_ID_FREE (bdb->bi_dbenv, locker );
125 struct berval matched_dn = { 0, NULL };
126 BerVarray refs = NULL;
128 if ( matched != NULL ) {
130 ber_dupbv( &matched_dn, &matched->e_name );
132 erefs = is_entry_referral( matched )
133 ? get_entry_referrals( be, conn, op, matched )
136 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
140 refs = referral_rewrite( erefs, &matched_dn,
142 ber_bvarray_free( erefs );
146 refs = referral_rewrite( default_referral,
150 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
151 matched_dn.bv_val, text, refs, NULL );
153 LOCK_ID_FREE (bdb->bi_dbenv, locker );
154 if ( refs ) ber_bvarray_free( refs );
155 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
159 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
160 /* entry is a referral, don't allow add */
161 struct berval matched_dn;
162 BerVarray erefs, refs;
164 ber_dupbv( &matched_dn, &e->e_name );
165 erefs = get_entry_referrals( be, conn, op, e );
168 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
172 refs = referral_rewrite( erefs, &matched_dn,
174 ber_bvarray_free( erefs );
178 LDAP_LOG ( OPERATION, RESULTS,
179 "bdb_search: entry is referral\n", 0, 0, 0 );
181 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
185 send_ldap_result( conn, op, LDAP_REFERRAL,
187 refs ? NULL : "bad referral object",
190 LOCK_ID_FREE (bdb->bi_dbenv, locker );
191 ber_bvarray_free( refs );
192 ber_memfree( matched_dn.bv_val );
196 /* if not root, get appropriate limits */
197 if ( be_isroot( be, &op->o_ndn ) ) {
200 ( void ) get_limits( be, &op->o_ndn, &limit );
203 /* The time/size limits come first because they require very little
204 * effort, so there's no chance the candidates are selected and then
205 * the request is not honored only because of time/size constraints */
207 /* if no time limit requested, use soft limit (unless root!) */
210 tlimit = -1; /* allow root to set no limit */
218 /* if no limit is required, use soft limit */
220 tlimit = limit->lms_t_soft;
222 /* if requested limit higher than hard limit, abort */
223 } else if ( tlimit > limit->lms_t_hard ) {
224 /* no hard limit means use soft instead */
225 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
226 tlimit = limit->lms_t_soft;
228 /* positive hard limit means abort */
229 } else if ( limit->lms_t_hard > 0 ) {
230 send_search_result( conn, op,
231 LDAP_UNWILLING_TO_PERFORM,
232 NULL, NULL, NULL, NULL, 0 );
237 /* negative hard limit means no limit */
240 /* if no limit is required, use soft limit */
242 slimit = limit->lms_s_soft;
244 /* if requested limit higher than hard limit, abort */
245 } else if ( slimit > limit->lms_s_hard ) {
246 /* no hard limit means use soft instead */
247 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
248 slimit = limit->lms_s_soft;
250 /* positive hard limit means abort */
251 } else if ( limit->lms_s_hard > 0 ) {
252 send_search_result( conn, op,
253 LDAP_UNWILLING_TO_PERFORM,
254 NULL, NULL, NULL, NULL, 0 );
259 /* negative hard limit means no limit */
263 /* compute it anyway; root does not use it */
264 stoptime = op->o_time + tlimit;
266 /* select candidates */
267 if ( scope == LDAP_SCOPE_BASE ) {
268 rc = base_candidate( be, e, candidates );
271 BDB_IDL_ALL( bdb, candidates );
272 rc = search_candidates( be, op, e, filter,
273 scope, deref, candidates );
276 /* need normalized dn below */
277 ber_dupbv( &realbase, &e->e_nname );
279 /* start cursor at base entry's id
280 * FIXME: hack to make "" base work
281 * FIXME: moddn needs to assign new ID for this to work
283 cursor = e->e_id == NOID ? 1 : e->e_id;
285 if ( e != &slap_entry_root ) {
286 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
290 if ( candidates[0] == 0 ) {
292 LDAP_LOG ( OPERATION, RESULTS, "bdb_search: no candidates\n", 0, 0, 0 );
294 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
298 send_search_result( conn, op,
300 NULL, NULL, NULL, NULL, 0 );
306 /* if not root and candidates exceed to-be-checked entries, abort */
307 if ( !isroot && limit->lms_s_unchecked != -1 ) {
308 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
309 send_search_result( conn, op,
310 LDAP_ADMINLIMIT_EXCEEDED,
311 NULL, NULL, NULL, NULL, 0 );
317 for ( id = bdb_idl_first( candidates, &cursor );
319 id = bdb_idl_next( candidates, &cursor ) )
323 /* check for abandon */
324 if ( op->o_abandon ) {
329 /* check time limit */
330 if ( tlimit != -1 && slap_get_time() > stoptime ) {
331 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
332 NULL, NULL, v2refs, NULL, nentries );
337 /* get the entry with reader lock */
338 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
340 if (rc == LDAP_BUSY) {
341 send_ldap_result( conn, op, rc=LDAP_BUSY,
342 NULL, "ldap server busy", NULL, NULL );
345 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
350 if( !BDB_IDL_IS_RANGE(candidates) ) {
351 /* only complain for non-range IDLs */
353 LDAP_LOG ( OPERATION, RESULTS,
354 "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
356 Debug( LDAP_DEBUG_TRACE,
357 "bdb_search: candidate %ld not found\n",
365 #ifdef BDB_SUBENTRIES
366 if ( is_entry_subentry( e ) ) {
367 if( scope != LDAP_SCOPE_BASE ) {
368 if(!get_subentries_visibility( op )) {
369 /* only subentries are visible */
373 } else if ( get_subentries( op ) &&
374 !get_subentries_visibility( op ))
376 /* only subentries are visible */
380 } else if ( get_subentries_visibility( op )) {
381 /* only subentries are visible */
387 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
392 e = deref_entry_r( be, e, &err, &matched, &text );
399 if( e->e_id == id ) {
404 /* need to skip alias which deref into scope */
405 if( scope & LDAP_SCOPE_ONELEVEL ) {
408 dnParent( &e->e_nname, &pdn ):
409 if ( ber_bvcmp( pdn, &realbase ) ) {
413 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
414 /* alias is within scope */
416 LDAP_LOG ( OPERATION, RESULTS,
417 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
419 Debug( LDAP_DEBUG_TRACE,
420 "bdb_search: \"%s\" in subtree\n",
431 * if it's a referral, add it to the list of referrals. only do
432 * this for non-base searches, and don't check the filter
433 * explicitly here since it's only a candidate anyway.
435 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
436 is_entry_referral( e ) )
441 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
442 if ( !be_issuffix( be, &e->e_nname ) ) {
443 dnParent( &e->e_nname, &dn );
444 scopeok = dn_match( &dn, &realbase );
446 scopeok = (realbase.bv_len == 0);
449 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
450 scopeok = dnIsSuffix( &e->e_nname, &realbase );
457 BerVarray erefs = get_entry_referrals(
459 BerVarray refs = referral_rewrite( erefs,
461 scope == LDAP_SCOPE_SUBTREE
465 send_search_reference( be, conn, op,
466 e, refs, NULL, &v2refs );
468 ber_bvarray_free( refs );
472 LDAP_LOG(OPERATION, DETAIL2,
473 "bdb_search: candidate referral %ld scope not okay\n",
476 Debug( LDAP_DEBUG_TRACE,
477 "bdb_search: candidate referral %ld scope not okay\n",
485 /* if it matches the filter and scope, send it */
486 rc = test_filter( be, conn, op, e, filter );
487 if ( rc == LDAP_COMPARE_TRUE ) {
491 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
492 if ( be_issuffix( be, &e->e_nname ) ) {
493 scopeok = (realbase.bv_len == 0);
495 dnParent( &e->e_nname, &dn );
496 scopeok = dn_match( &dn, &realbase );
499 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
500 scopeok = dnIsSuffix( &e->e_nname, &realbase );
507 /* check size limit */
508 if ( --slimit == -1 ) {
509 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
511 send_search_result( conn, op,
512 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
513 v2refs, NULL, nentries );
523 result = send_search_entry( be, conn, op,
524 e, attrs, attrsonly, NULL);
528 case 0: /* entry sent ok */
531 case 1: /* entry not sent */
533 case -1: /* connection closed */
534 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
542 LDAP_LOG ( OPERATION, RESULTS,
543 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
545 Debug( LDAP_DEBUG_TRACE,
546 "bdb_search: %ld scope not okay\n",
552 LDAP_LOG ( OPERATION, RESULTS,
553 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
555 Debug( LDAP_DEBUG_TRACE,
556 "bdb_search: %ld does match filter\n",
563 /* free reader lock */
564 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e , &lock);
568 ldap_pvt_thread_yield();
570 send_search_result( conn, op,
571 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
572 NULL, NULL, v2refs, NULL, nentries );
578 /* free reader lock */
579 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
582 LOCK_ID_FREE (bdb->bi_dbenv, locker );
584 if( v2refs ) ber_bvarray_free( v2refs );
585 if( realbase.bv_val ) ch_free( realbase.bv_val );
591 static int base_candidate(
597 LDAP_LOG ( OPERATION, ENTRY,
598 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
600 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
601 e->e_dn, (long) e->e_id, 0);
609 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
610 * doesn't count, we're looking for explicit values. Also count depth
611 * of filter tree while we're at it.
613 static int oc_filter(
621 if( cur > *max ) *max = cur;
623 switch(f->f_choice) {
624 case LDAP_FILTER_EQUALITY:
625 case LDAP_FILTER_APPROX:
626 if (f->f_av_desc == slap_schema.si_ad_objectClass)
630 case LDAP_FILTER_SUBSTRINGS:
631 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
635 case LDAP_FILTER_AND:
638 for (f=f->f_and; f; f=f->f_next)
639 if ((rc = oc_filter(f, cur, max)))
648 static int search_candidates(
658 Filter f, scopef, rf, xf;
660 AttributeAssertion aa_ref;
661 #ifdef BDB_SUBENTRIES
663 AttributeAssertion aa_subentry;
667 AttributeAssertion aa_alias;
671 * This routine takes as input a filter (user-filter)
672 * and rewrites it as follows:
673 * (&(scope=DN)[(objectClass=subentry)]
674 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
678 LDAP_LOG ( OPERATION, ENTRY,
679 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
680 e->e_dn, (long) e->e_id, scope);
682 Debug(LDAP_DEBUG_TRACE,
683 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
684 e->e_dn, (long) e->e_id, scope );
688 xf.f_choice = LDAP_FILTER_OR;
691 /* If the user's filter doesn't mention objectClass, or if
692 * it just uses objectClass=*, these clauses are redundant.
694 if (oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
695 if( !get_manageDSAit(op) ) { /* match referrals */
696 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
697 rf.f_choice = LDAP_FILTER_EQUALITY;
699 rf.f_av_desc = slap_schema.si_ad_objectClass;
700 rf.f_av_value = bv_ref;
706 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
707 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
708 af.f_choice = LDAP_FILTER_EQUALITY;
709 af.f_ava = &aa_alias;
710 af.f_av_desc = slap_schema.si_ad_objectClass;
711 af.f_av_value = bv_alias;
716 /* We added one of these clauses, filter depth increased */
717 if( xf.f_or != filter ) depth++;
721 f.f_choice = LDAP_FILTER_AND;
723 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
724 ? SLAPD_FILTER_DN_SUBTREE
725 : SLAPD_FILTER_DN_ONE;
726 scopef.f_dn = &e->e_nname;
727 scopef.f_next = xf.f_or == filter ? filter : &xf ;
728 /* Filter depth increased again, adding scope clause */
731 #ifdef BDB_SUBENTRIES
732 if( get_subentries_visibility( op ) ) {
733 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
734 sf.f_choice = LDAP_FILTER_EQUALITY;
735 sf.f_ava = &aa_subentry;
736 sf.f_av_desc = slap_schema.si_ad_objectClass;
737 sf.f_av_value = bv_subentry;
738 sf.f_next = scopef.f_next;
743 /* Allocate IDL stack, plus 1 more for former tmp */
744 stack = malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
746 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
752 LDAP_LOG ( OPERATION, DETAIL1,
753 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
755 Debug(LDAP_DEBUG_TRACE,
756 "bdb_search_candidates: failed (rc=%d)\n",
762 LDAP_LOG ( OPERATION, DETAIL1,
763 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
764 (long) ids[0], (long) BDB_IDL_FIRST(ids),
765 (long) BDB_IDL_LAST(ids));
767 Debug(LDAP_DEBUG_TRACE,
768 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
770 (long) BDB_IDL_FIRST(ids),
771 (long) BDB_IDL_LAST(ids) );