]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/search.c
Use EntryInfo navigation for search scope checks
[openldap] / servers / slapd / back-bdb / search.c
index 38b087017eed7ff037218ecc107cc79982c860a0..046efa547130539124af21f77278c5736729e190 100644 (file)
@@ -24,7 +24,8 @@ static int search_candidates(
        SlapReply *rs,
        Entry *e,
        u_int32_t locker,
-       ID      *ids );
+       ID      *ids,
+       ID      *scopes );
 static void send_pagerequest_response( 
        Operation *op,
        SlapReply *rs,
@@ -35,7 +36,7 @@ static void send_pagerequest_response(
  * dereferenced entry on success, NULL on any failure.
  */
 static Entry * deref_base (
-       BackendDB *be,
+       Operation *op,
        SlapReply *rs,
        Entry *e,
        Entry **matched,
@@ -44,14 +45,15 @@ static Entry * deref_base (
        ID      *tmp,
        ID      *visited )
 {
-       struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+       struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
        struct berval ndn;
+       EntryInfo *ei;
        DB_LOCK lockr;
 
        rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
        rs->sr_text = "maximum deref depth exceeded";
 
-       while (BDB_IDL_N(tmp) < be->be_max_deref_depth) {
+       while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
 
                /* Remember the last entry we looked at, so we can
                 * report broken links
@@ -84,8 +86,12 @@ static Entry * deref_base (
                        break;
                }
 
-               rs->sr_err = bdb_dn2entry_r( be, NULL, &ndn, &e,
-                       NULL, 0, locker, &lockr );
+               rs->sr_err = bdb_dn2entry( op->o_bd, NULL, &ndn, &ei,
+                       0, locker, &lockr, op->o_tmpmemctx );
+
+               if ( ei ) e = ei->bei_e;
+               else    e = NULL;
+
                if (!e) {
                        rs->sr_err = LDAP_ALIAS_PROBLEM;
                        rs->sr_text = "aliasedObject not found";
@@ -113,7 +119,7 @@ static Entry * deref_base (
 
 /* Look for and dereference all aliases within the search scope. Adds
  * the dereferenced entries to the "ids" list. Requires "stack" to be
- * able to hold 8 levels of IDLs.
+ * able to hold 8 levels of DB_SIZE IDLs.
  */
 static int search_aliases(
        Operation *op,
@@ -122,6 +128,7 @@ static int search_aliases(
        u_int32_t locker,
        Filter *sf,
        ID *ids,
+       ID *scopes,
        ID *stack
 )
 {
@@ -129,6 +136,7 @@ static int search_aliases(
        ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
        ID cursora, ida, cursoro, ido, *subscop2;
        Entry *matched, *a;
+       EntryInfo *ei;
        struct berval bv_alias = { sizeof("alias")-1, "alias" };
        AttributeAssertion aa_alias;
        Filter  af;
@@ -137,17 +145,17 @@ static int search_aliases(
 
 
        aliases = stack;        /* IDL of all aliases in the database */
-       curscop = aliases + BDB_IDL_UM_SIZE;    /* Aliases in the current scope */
-       subscop = curscop + BDB_IDL_UM_SIZE;    /* The current scope */
-       visited = subscop + BDB_IDL_UM_SIZE;    /* IDs we've seen in this search */
-       newsubs = visited + BDB_IDL_UM_SIZE;    /* New subtrees we've added */
-       oldsubs = newsubs + BDB_IDL_UM_SIZE;    /* Subtrees added previously */
-       tmp = oldsubs + BDB_IDL_UM_SIZE;        /* Scratch space for deref_base() */
+       curscop = aliases + BDB_IDL_DB_SIZE;    /* Aliases in the current scope */
+       subscop = curscop + BDB_IDL_DB_SIZE;    /* The current scope */
+       visited = subscop + BDB_IDL_DB_SIZE;    /* IDs we've seen in this search */
+       newsubs = visited + BDB_IDL_DB_SIZE;    /* New subtrees we've added */
+       oldsubs = newsubs + BDB_IDL_DB_SIZE;    /* Subtrees added previously */
+       tmp = oldsubs + BDB_IDL_DB_SIZE;        /* Scratch space for deref_base() */
 
        /* A copy of subscop, because subscop gets clobbered by
         * the bdb_idl_union/intersection routines
         */
-       subscop2 = tmp + BDB_IDL_UM_SIZE;
+       subscop2 = tmp + BDB_IDL_DB_SIZE;
 
        af.f_choice = LDAP_FILTER_EQUALITY;
        af.f_ava = &aa_alias;
@@ -157,7 +165,7 @@ static int search_aliases(
 
        /* Find all aliases in database */
        BDB_IDL_ALL( bdb, aliases );
-       rs->sr_err = bdb_filter_candidates( op->o_bd, &af, aliases,
+       rs->sr_err = bdb_filter_candidates( op, &af, aliases,
                curscop, visited );
        if (rs->sr_err != LDAP_SUCCESS) {
                return rs->sr_err;
@@ -179,7 +187,7 @@ static int search_aliases(
                 * to the cumulative list of candidates.
                 */
                BDB_IDL_CPY( curscop, aliases );
-               rs->sr_err = bdb_filter_candidates( op->o_bd, sf, subscop, NULL, NULL );
+               rs->sr_err = bdb_filter_candidates( op, sf, subscop, NULL, NULL );
                if (first) {
                        first = 0;
                } else {
@@ -194,11 +202,13 @@ static int search_aliases(
                for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
                        ida = bdb_idl_next(curscop, &cursora))
                {
-                       rs->sr_err = bdb_id2entry_r(op->o_bd, NULL, ida, &a,
-                               locker, &lockr);
+                       ei = NULL;
+                       rs->sr_err = bdb_cache_find_entry_id(op->o_bd, NULL,
+                               ida, &ei, 0, locker, &lockr, op->o_tmpmemctx );
                        if (rs->sr_err != LDAP_SUCCESS) {
                                continue;
                        }
+                       a = ei->bei_e;
 
                        /* This should only happen if the curscop IDL has maxed out and
                         * turned into a range that spans IDs indiscriminately
@@ -211,16 +221,17 @@ static int search_aliases(
 
                        /* Actually dereference the alias */
                        BDB_IDL_ZERO(tmp);
-                       a = deref_base( op->o_bd, rs, a, &matched, locker, &lockr,
+                       a = deref_base( op, rs, a, &matched, locker, &lockr,
                                tmp, visited );
                        if (a) {
                                /* If the target was not already in our current candidates,
-                                * make note of it in the newsubs list.
-                                * FIXME: Somehow we have to propagate these new scopes back
-                                * up to bdb_search.
+                                * make note of it in the newsubs list. Also
+                                * set it in the scopes list so that bdb_search
+                                * can check it.
                                 */
                                if (bdb_idl_insert(ids, a->e_id) == 0) {
                                        bdb_idl_insert(newsubs, a->e_id);
+                                       bdb_idl_insert(scopes, a->e_id);
                                }
                                bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
                                        a, &lockr);
@@ -257,8 +268,11 @@ nextido:
                 * we should never see the ID of an entry that doesn't exist.
                 * Set the name so that the scope's IDL can be retrieved.
                 */
-               rs->sr_err = bdb_id2entry_r(op->o_bd, NULL, ido, &e, locker, &locka);
+               ei = NULL;
+               rs->sr_err = bdb_cache_find_entry_id(op->o_bd, NULL, ido, &ei,
+                       0, locker, &locka, op->o_tmpmemctx );
                if (rs->sr_err != LDAP_SUCCESS) goto nextido;
+               e = ei->bei_e;
                sf->f_dn = &e->e_nname;
        }
        return rs->sr_err;
@@ -338,8 +352,10 @@ int bdb_search( Operation *op, SlapReply *rs )
        time_t          stoptime;
        ID              id, cursor;
        ID              candidates[BDB_IDL_UM_SIZE];
-       Entry           *e = NULL;
+       ID              scopes[BDB_IDL_DB_SIZE];
+       Entry           *e = NULL, base;
        Entry   *matched = NULL;
+       EntryInfo       *ei;
        struct berval   realbase = { 0, NULL };
        int             manageDSAit;
        int             tentries = 0;
@@ -451,22 +467,16 @@ int bdb_search( Operation *op, SlapReply *rs )
        } else {
 dn2entry_retry:
                /* get entry with reader lock */
-               rs->sr_err = bdb_dn2entry_r( op->o_bd, NULL, &sop->o_req_ndn, &e,
-                       &matched, 0, locker, &lock );
+               rs->sr_err = bdb_dn2entry( op->o_bd, NULL, &sop->o_req_ndn, &ei,
+                       1, locker, &lock, op->o_tmpmemctx );
        }
 
        switch(rs->sr_err) {
        case DB_NOTFOUND:
+               matched = ei->bei_e; break;
        case 0:
-               break;
+               e = ei->bei_e; break;
        case LDAP_BUSY:
-               if (e != NULL) {
-                       bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
-               }
-               if (matched != NULL) {
-                       bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache,
-                               matched, &lock);
-               }
                send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
                LOCK_ID_FREE (bdb->bi_dbenv, locker );
                return LDAP_BUSY;
@@ -474,13 +484,6 @@ dn2entry_retry:
        case DB_LOCK_NOTGRANTED:
                goto dn2entry_retry;
        default:
-               if (e != NULL) {
-                       bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
-               }
-               if (matched != NULL) {
-                       bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache,
-                               matched, &lock);
-               }
                send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
                LOCK_ID_FREE (bdb->bi_dbenv, locker );
                return rs->sr_err;
@@ -488,7 +491,7 @@ dn2entry_retry:
 
        if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
                BDB_IDL_ZERO(candidates);
-               e = deref_base( op->o_bd, rs, e, &matched, locker, &lock,
+               e = deref_base( op, rs, e, &matched, locker, &lock,
                        candidates, NULL );
        }
 
@@ -648,13 +651,28 @@ dn2entry_retry:
        /* compute it anyway; root does not use it */
        stoptime = op->o_time + sop->oq_search.rs_tlimit;
 
+       /* need normalized dn below */
+       ber_dupbv( &realbase, &e->e_nname );
+
+       /* Copy info to base, must free entry before accessing the database
+        * in search_candidates, to avoid deadlocks.
+        */
+       base.e_nname = realbase;
+       base.e_id = e->e_id;
+
+       if ( e != &slap_entry_root ) {
+               bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
+       }
+       e = NULL;
+
        /* select candidates */
        if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
-               rs->sr_err = base_candidate( op->o_bd, e, candidates );
+               rs->sr_err = base_candidate( op->o_bd, &base, candidates );
 
        } else {
                BDB_IDL_ALL( bdb, candidates );
-               rs->sr_err = search_candidates( op, sop, rs, e, locker, candidates );
+               BDB_IDL_ZERO( scopes );
+               rs->sr_err = search_candidates( op, sop, rs, &base, locker, candidates, scopes );
        }
 
        /* start cursor at beginning of candidates.
@@ -680,14 +698,6 @@ dn2entry_retry:
        }
 #endif
 
-       /* need normalized dn below */
-       ber_dupbv( &realbase, &e->e_nname );
-
-       if ( e != &slap_entry_root ) {
-               bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
-       }
-       e = NULL;
-
        if ( candidates[0] == 0 ) {
 #ifdef NEW_LOGGING
                LDAP_LOG ( OPERATION, RESULTS,
@@ -849,8 +859,9 @@ loop_begin:
 #endif
 id2entry_retry:
                        /* get the entry with reader lock */
-                       rs->sr_err = bdb_id2entry_r( op->o_bd, NULL, id,
-                               &e, locker, &lock );
+                       ei = NULL;
+                       rs->sr_err = bdb_cache_find_entry_id( op->o_bd, NULL,
+                               id, &ei, 0, locker, &lock, op->o_tmpmemctx );
 
                        if (rs->sr_err == LDAP_BUSY) {
                                rs->sr_text = "ldap server busy";
@@ -863,6 +874,12 @@ id2entry_retry:
                                goto id2entry_retry;    
                        }
 
+                       if ( ei && rs->sr_err == 0 ) {
+                               e = ei->bei_e;
+                       } else {
+                               e = NULL;
+                       }
+
                        if ( e == NULL ) {
                                if( !BDB_IDL_IS_RANGE(candidates) ) {
                                        /* only complain for non-range IDLs */
@@ -907,72 +924,109 @@ id2entry_retry:
                }
 #endif
 
+               /* Does this candidate actually satisfy the search scope?
+                *
+                * Note that we don't lock access to the bei_parent pointer.
+                * Since only leaf nodes can be deleted, the parent of any
+                * node will always be a valid node. Also since we have
+                * a Read lock on the data, it cannot be renamed out of the
+                * scope while we are looking at it, and unless we're using
+                * BDB_HIER, its parents cannot be moved either.
+                */
+               switch( op->ors_scope ) {
+               case LDAP_SCOPE_BASE:
+                       /* This is always true, yes? */
+                       if ( id == base.e_id )
+                               scopeok = 1;
+                       break;
+               case LDAP_SCOPE_ONELEVEL:
+                       if ( ei->bei_parent->bei_id == base.e_id )
+                               scopeok = 1;
+                       break;
+               case LDAP_SCOPE_SUBTREE:
+                       { EntryInfo *tmp;
+                       for ( tmp = BEI(e); tmp->bei_parent;
+                               tmp = tmp->bei_parent ) {
+                               if ( tmp->bei_id == base.e_id ) {
+                                       scopeok = 1;
+                                       break;
+                               }
+                       } }
+                       break;
+               }
+
 #ifdef BDB_ALIASES
                /* aliases were already dereferenced in candidate list */
-               if ( (sop->ors_deref & LDAP_DEREF_SEARCHING) && is_entry_alias(e)) {
+               if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
                        /* but if the search base is an alias, and we didn't
                         * deref it when finding, return it.
                         */
-                       if ( (sop->ors_deref & LDAP_DEREF_FINDING)
-                               || !bvmatch(&e->e_nname, &op->o_req_ndn))
+                       if ( is_entry_alias(e) &&
+                               ((sop->ors_deref & LDAP_DEREF_FINDING)
+                                 || !bvmatch(&e->e_nname, &op->o_req_ndn)))
                        {
                                goto loop_continue;
                        }
+
+                       /* scopes is only non-empty for onelevel or subtree */
+                       if ( !scopeok && BDB_IDL_N(scopes) ) {
+                               unsigned x;
+                               if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
+                                       x = bdb_idl_search( scopes,
+                                               e->e_id );
+                                       if ( scopes[x] == e->e_id )
+                                               scopeok = 1;
+                               } else {
+                               /* subtree, walk up the tree */
+                                       EntryInfo *tmp = BEI(e);
+                                       for (;tmp->bei_parent;
+                                               tmp=tmp->bei_parent) {
+                                               x = bdb_idl_search(
+                                                       scopes, tmp->bei_id );
+                                               if ( scopes[x] == tmp->bei_id ) {
+                                                       scopeok = 1;
+                                                       break;
+                                               }
+                                       }
+                               }
+                       }
                }
 #endif
+
+               /* Not in scope, ignore it */
+               if ( !scopeok ) {
+#ifdef NEW_LOGGING
+                       LDAP_LOG ( OPERATION, RESULTS,
+                               "bdb_search: %ld scope not okay\n",
+                               (long) id, 0, 0);
+#else
+                       Debug( LDAP_DEBUG_TRACE,
+                               "bdb_search: %ld scope not okay\n",
+                               (long) id, 0, 0 );
+#endif
+                       goto loop_continue;
+               }
+
                /*
                 * if it's a referral, add it to the list of referrals. only do
                 * this for non-base searches, and don't check the filter
                 * explicitly here since it's only a candidate anyway.
                 */
-               if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE &&
-                       is_entry_referral( e ) )
+               if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
+                       && is_entry_referral( e ) )
                {
-                       struct berval   dn;
-
-                       /* check scope */
-                       if ( !scopeok && sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
-                               if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
-                                       dnParent( &e->e_nname, &dn );
-                                       scopeok = dn_match( &dn, &realbase );
-                               } else {
-                                       scopeok = (realbase.bv_len == 0);
-                               }
-
-                       } else if ( !scopeok
-                               && sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE )
-                       {
-                               scopeok = dnIsSuffix( &e->e_nname, &realbase );
-
-                       } else {
-                               scopeok = 1;
-                       }
-
-                       if( scopeok ) {
-                               BerVarray erefs = get_entry_referrals( sop, e );
-                               rs->sr_ref = referral_rewrite( erefs,
-                                       &e->e_name, NULL,
-                                       sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
-                                               ? LDAP_SCOPE_SUBTREE
-                                               : LDAP_SCOPE_BASE );
+                       BerVarray erefs = get_entry_referrals( sop, e );
+                       rs->sr_ref = referral_rewrite( erefs,
+                               &e->e_name, NULL,
+                               sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
+                                       ? LDAP_SCOPE_SUBTREE
+                                       : LDAP_SCOPE_BASE );
 
-                               send_search_reference( sop, rs );
+                       send_search_reference( sop, rs );
 
-                               ber_bvarray_free( rs->sr_ref );
-                               ber_bvarray_free( erefs );
-                               rs->sr_ref = NULL;
-
-                       } else {
-#ifdef NEW_LOGGING
-                               LDAP_LOG(OPERATION, DETAIL2, 
-                                       "bdb_search: candidate referral %ld scope not okay\n",
-                                       id, 0, 0 );
-#else
-                               Debug( LDAP_DEBUG_TRACE,
-                                       "bdb_search: candidate referral %ld scope not okay\n",
-                                       id, 0, 0 );
-#endif
-                       }
+                       ber_bvarray_free( rs->sr_ref );
+                       ber_bvarray_free( erefs );
+                       rs->sr_ref = NULL;
 
                        goto loop_continue;
                }
@@ -1015,106 +1069,85 @@ id2entry_retry:
 #endif
 
                if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
-                       struct berval   dn;
-
-                       /* check scope */
-                       if ( !scopeok && sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
-                               if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
-                                       scopeok = (realbase.bv_len == 0);
-                               } else {
-                                       dnParent( &e->e_nname, &dn );
-                                       scopeok = dn_match( &dn, &realbase );
-                               }
-
-                       } else if ( !scopeok &&
-                               sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE )
-                       {
-                               scopeok = dnIsSuffix( &e->e_nname, &realbase );
-
-                       } else {
-                               scopeok = 1;
-                       }
-
-                       if ( scopeok ) {
-                               /* check size limit */
-                               if ( --sop->oq_search.rs_slimit == -1 ) {
+                       /* check size limit */
+                       if ( --sop->oq_search.rs_slimit == -1 ) {
 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
-                                       if (!IS_PSEARCH)
+                               if (!IS_PSEARCH)
 #endif
-                                       bdb_cache_return_entry_r( bdb->bi_dbenv,
-                                               &bdb->bi_cache, e, &lock );
-                                       e = NULL;
-                                       rs->sr_entry = NULL;
-                                       rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
-                                       rs->sr_ref = rs->sr_v2ref;
-                                       send_ldap_result( sop, rs );
-                                       goto done;
-                               }
+                               bdb_cache_return_entry_r( bdb->bi_dbenv,
+                                       &bdb->bi_cache, e, &lock );
+                               e = NULL;
+                               rs->sr_entry = NULL;
+                               rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
+                               rs->sr_ref = rs->sr_v2ref;
+                               send_ldap_result( sop, rs );
+                               goto done;
+                       }
 
 #ifdef LDAP_CONTROL_PAGEDRESULTS
-                               if ( get_pagedresults(sop) ) {
-                                       if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
-                                               send_pagerequest_response( sop, rs,
-                                                       lastid, tentries );
-                                               goto done;
-                                       }
-                                       lastid = id;
+                       if ( get_pagedresults(sop) ) {
+                               if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
+                                       send_pagerequest_response( sop, rs,
+                                               lastid, tentries );
+                                       goto done;
                                }
+                               lastid = id;
+                       }
 #endif
 
-                               if (e) {
-                                       /* safe default */
-                                       int result = -1;
-                                       
+                       if (e) {
+                               /* safe default */
+                               int result = -1;
+                               
 #if 0  /* noop is masked SLAP_CTRL_UPDATE */
-                                       if( op->o_noop ) {
-                                               result = 0;
-                                       } else
+                               if( op->o_noop ) {
+                                       result = 0;
+                               } else
 #endif
 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
-                                       if (IS_PSEARCH) {
+                               if (IS_PSEARCH) {
 #ifdef LDAP_SYNC
-                                               int premodify_found = 0;
-                                               int entry_sync_state;
+                                       int premodify_found = 0;
+                                       int entry_sync_state;
 #endif
 
-                                               if ( ps_type == LDAP_PSEARCH_BY_ADD ||
-                                                        ps_type == LDAP_PSEARCH_BY_DELETE ||
-                                                        ps_type == LDAP_PSEARCH_BY_MODIFY ||
-                                                        ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
-                                               {
-                                                       if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
-                                                               struct psid_entry* psid_e;
-                                                               LDAP_LIST_FOREACH( psid_e,
-                                                                       &op->o_pm_list, ps_link)
-                                                               {
-                                                                       if( psid_e->ps_op == sop ) {
+                                       if ( ps_type == LDAP_PSEARCH_BY_ADD ||
+                                                ps_type == LDAP_PSEARCH_BY_DELETE ||
+                                                ps_type == LDAP_PSEARCH_BY_MODIFY ||
+                                                ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
+                                       {
+                                               if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
+                                                       struct psid_entry* psid_e;
+                                                       LDAP_LIST_FOREACH( psid_e,
+                                                               &op->o_pm_list, ps_link)
+                                                       {
+                                                               if( psid_e->ps_op == sop ) {
 #ifdef LDAP_SYNC
-                                                                               premodify_found = 1;
+                                                                       premodify_found = 1;
 #endif
-                                                                               LDAP_LIST_REMOVE(psid_e, ps_link);
-                                                                               break;
-                                                                       }
+                                                                       LDAP_LIST_REMOVE(psid_e, ps_link);
+                                                                       break;
                                                                }
-                                                               if (psid_e != NULL) free (psid_e);
                                                        }
+                                                       if (psid_e != NULL) free (psid_e);
+                                               }
 #ifdef LDAP_SYNC
-                                                       if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
+                                               if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
+                                                       entry_sync_state = LDAP_SYNC_ADD;
+                                               } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
+                                                       entry_sync_state = LDAP_SYNC_DELETE;
+                                               } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
+                                                       if ( premodify_found ) {
+                                                               entry_sync_state = LDAP_SYNC_MODIFY;
+                                                       } else {
                                                                entry_sync_state = LDAP_SYNC_ADD;
-                                                       } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
-                                                               entry_sync_state = LDAP_SYNC_DELETE;
-                                                       } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
-                                                               if ( premodify_found ) {
-                                                                       entry_sync_state = LDAP_SYNC_MODIFY;
-                                                               } else {
-                                                                       entry_sync_state = LDAP_SYNC_ADD;
-                                                               }
-                                                       } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
-                                                               entry_sync_state = LDAP_SYNC_DELETE;
-                                                       else {
-                                                               rs->sr_err = 1;
-                                                               goto done;
                                                        }
+                                               } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
+                                                       entry_sync_state = LDAP_SYNC_DELETE;
+                                               else {
+                                                       rs->sr_err = 1;
+                                                       goto done;
+                                               }
 #endif
 
 #ifdef LDAP_CLIENT_UPDATE
@@ -1174,73 +1207,63 @@ id2entry_retry:
                                        }
                                } else {
 #ifdef LDAP_CLIENT_UPDATE
-                                               if ( sop->o_clientupdate_type & SLAP_LCUP_SYNC ) {
-                                                       rs->sr_err = bdb_build_lcup_update_ctrl( sop,
-                                                               rs, e, ++entry_count, ctrls,
-                                                               num_ctrls++, &latest_entrycsn_bv,
-                                                               SLAP_LCUP_ENTRY_DELETED_FALSE );
-                                                       if ( rs->sr_err != LDAP_SUCCESS ) goto done;
-                                                       rs->sr_ctrls = ctrls;
-                                                       rs->sr_attrs = sop->oq_search.rs_attrs;
-                                                       result = send_search_entry( sop, rs );
-                                                       ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
-                                                       ch_free( ctrls[--num_ctrls] );
-                                                       ctrls[num_ctrls] = NULL;
-                                                       rs->sr_ctrls = NULL;
-                                               } else
+                                       if ( sop->o_clientupdate_type & SLAP_LCUP_SYNC ) {
+                                               rs->sr_err = bdb_build_lcup_update_ctrl( sop,
+                                                       rs, e, ++entry_count, ctrls,
+                                                       num_ctrls++, &latest_entrycsn_bv,
+                                                       SLAP_LCUP_ENTRY_DELETED_FALSE );
+                                               if ( rs->sr_err != LDAP_SUCCESS ) goto done;
+                                               rs->sr_ctrls = ctrls;
+                                               rs->sr_attrs = sop->oq_search.rs_attrs;
+                                               result = send_search_entry( sop, rs );
+                                               ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
+                                               ch_free( ctrls[--num_ctrls] );
+                                               ctrls[num_ctrls] = NULL;
+                                               rs->sr_ctrls = NULL;
+                                       } else
 #endif
 #ifdef LDAP_SYNC
-                                               if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
-                                                       rs->sr_err = bdb_build_sync_state_ctrl( sop,
-                                                               rs, e, entry_sync_state, ctrls,
-                                                               num_ctrls++, 0, &latest_entrycsn_bv );
-                                                       if ( rs->sr_err != LDAP_SUCCESS ) goto done;
-
-                                                       rs->sr_ctrls = ctrls;
-                                                       if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
-                                                               rs->sr_attrs = sop->oq_search.rs_attrs;
-                                                       } else { /* PRESENT */
-                                                               rs->sr_attrs = &null_attr;
-                                                       }
-                                                       result = send_search_entry( sop, rs );
-                                                       ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
-                                                       ch_free( ctrls[--num_ctrls] );
-                                                       ctrls[num_ctrls] = NULL;
-                                                       rs->sr_ctrls = NULL;
-                                               } else
-#endif
-#endif
-                                               {
+                                       if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
+                                               rs->sr_err = bdb_build_sync_state_ctrl( sop,
+                                                       rs, e, entry_sync_state, ctrls,
+                                                       num_ctrls++, 0, &latest_entrycsn_bv );
+                                               if ( rs->sr_err != LDAP_SUCCESS ) goto done;
+
+                                               rs->sr_ctrls = ctrls;
+                                               if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
                                                        rs->sr_attrs = sop->oq_search.rs_attrs;
-                                                       rs->sr_ctrls = NULL;
-                                                       result = send_search_entry( sop, rs );
+                                               } else { /* PRESENT */
+                                                       rs->sr_attrs = &null_attr;
                                                }
+                                               result = send_search_entry( sop, rs );
+                                               ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
+                                               ch_free( ctrls[--num_ctrls] );
+                                               ctrls[num_ctrls] = NULL;
+                                               rs->sr_ctrls = NULL;
+                                       } else
+#endif
+#endif
+                                       {
+                                               rs->sr_attrs = sop->oq_search.rs_attrs;
+                                               rs->sr_ctrls = NULL;
+                                               result = send_search_entry( sop, rs );
                                        }
+                               }
 
-                                       switch (result) {
-                                       case 0:         /* entry sent ok */
-                                               break;
-                                       case 1:         /* entry not sent */
-                                               break;
-                                       case -1:        /* connection closed */
-                                               if (!IS_PSEARCH)
-                                               bdb_cache_return_entry_r(bdb->bi_dbenv,
-                                                       &bdb->bi_cache, e, &lock);
-                                               e = NULL;
-                                               rs->sr_entry = NULL;
-                                               rs->sr_err = LDAP_OTHER;
-                                               goto done;
-                                       }
+                               switch (result) {
+                               case 0:         /* entry sent ok */
+                                       break;
+                               case 1:         /* entry not sent */
+                                       break;
+                               case -1:        /* connection closed */
+                                       if (!IS_PSEARCH)
+                                       bdb_cache_return_entry_r(bdb->bi_dbenv,
+                                               &bdb->bi_cache, e, &lock);
+                                       e = NULL;
+                                       rs->sr_entry = NULL;
+                                       rs->sr_err = LDAP_OTHER;
+                                       goto done;
                                }
-                       } else {
-#ifdef NEW_LOGGING
-                               LDAP_LOG ( OPERATION, RESULTS,
-                                       "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
-#else
-                               Debug( LDAP_DEBUG_TRACE,
-                                       "bdb_search: %ld scope not okay\n",
-                                       (long) id, 0, 0 );
-#endif
                        }
                } else {
 #ifdef NEW_LOGGING
@@ -1352,10 +1375,10 @@ static int base_candidate(
 #ifdef NEW_LOGGING
        LDAP_LOG ( OPERATION, ENTRY,
                "base_candidate: base: \"%s\" (0x%08lx)\n",
-               e->e_dn, (long) e->e_id, 0);
+               e->e_nname.bv_val, (long) e->e_id, 0);
 #else
        Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
-               e->e_dn, (long) e->e_id, 0);
+               e->e_nname.bv_val, (long) e->e_id, 0);
 #endif
 
        ids[0] = 1;
@@ -1435,7 +1458,8 @@ static int search_candidates(
        SlapReply *rs,
        Entry *e,
        u_int32_t locker,
-       ID      *ids )
+       ID      *ids,
+       ID      *scopes )
 {
        struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
        int rc, depth = 1;
@@ -1457,11 +1481,11 @@ static int search_candidates(
 #ifdef NEW_LOGGING
        LDAP_LOG ( OPERATION, ENTRY,
                "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n", 
-               e->e_dn, (long) e->e_id, op->oq_search.rs_scope);
+               e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope);
 #else
        Debug(LDAP_DEBUG_TRACE,
                "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
-               e->e_dn, (long) e->e_id, op->oq_search.rs_scope );
+               e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
 #endif
 
        xf.f_or = op->oq_search.rs_filter;
@@ -1523,14 +1547,14 @@ static int search_candidates(
        }
 
        if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
-               rc = search_aliases( op, rs, e, locker, &scopef, ids, stack );
+               rc = search_aliases( op, rs, e, locker, &scopef, ids, scopes, stack );
        } else {
-               rc = bdb_filter_candidates( op->o_bd, &scopef, ids,
+               rc = bdb_filter_candidates( op, &scopef, ids,
                        stack, stack+BDB_IDL_UM_SIZE );
        }
 
        if ( rc == LDAP_SUCCESS ) {
-               rc = bdb_filter_candidates( op->o_bd, &f, ids,
+               rc = bdb_filter_candidates( op, &f, ids,
                        stack, stack+BDB_IDL_UM_SIZE );
        }