]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-meta/conn.c
make additional checking optional (more on ITS#5860)
[openldap] / servers / slapd / back-meta / conn.c
index 587bd15d98bc38ed5f388d5ceb6d07eea0c5865f..3b086cfc0484c6b84a551d0a7ad6a50d38b2833e 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1999-2005 The OpenLDAP Foundation.
+ * Copyright 1999-2008 The OpenLDAP Foundation.
  * Portions Copyright 2001-2003 Pierangelo Masarati.
  * Portions Copyright 1999-2003 Howard Chu.
  * All rights reserved.
@@ -24,6 +24,7 @@
 
 #include <stdio.h>
 
+#include <ac/errno.h>
 #include <ac/socket.h>
 #include <ac/string.h>
 
 #include "back-meta.h"
 
 /*
- * Set PRINT_CONNTREE larger than 0 to dump the connection tree (debug only)
+ * meta_back_conndn_cmp
+ *
+ * compares two struct metaconn based on the value of the conn pointer
+ * and of the local DN; used by avl stuff
+ */
+int
+meta_back_conndn_cmp(
+       const void *c1,
+       const void *c2 )
+{
+       metaconn_t      *mc1 = ( metaconn_t * )c1;
+        metaconn_t     *mc2 = ( metaconn_t * )c2;
+       int             rc;
+       
+       /* If local DNs don't match, it is definitely not a match */
+       /* For shared sessions, conn is NULL. Only explicitly
+        * bound sessions will have non-NULL conn.
+        */
+       rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
+       if ( rc == 0 ) {
+               rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
+       }
+
+       return rc;
+}
+
+/*
+ * meta_back_conndnmc_cmp
+ *
+ * compares two struct metaconn based on the value of the conn pointer,
+ * the local DN and the struct pointer; used by avl stuff
  */
-#define PRINT_CONNTREE 0
+static int
+meta_back_conndnmc_cmp(
+       const void *c1,
+       const void *c2 )
+{
+       metaconn_t      *mc1 = ( metaconn_t * )c1;
+        metaconn_t     *mc2 = ( metaconn_t * )c2;
+       int             rc;
+       
+       /* If local DNs don't match, it is definitely not a match */
+       /* For shared sessions, conn is NULL. Only explicitly
+        * bound sessions will have non-NULL conn.
+        */
+       rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
+       if ( rc == 0 ) {
+               rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
+               if ( rc == 0 ) {
+                       rc = SLAP_PTRCMP( mc1, mc2 );
+               }
+       }
+
+       return rc;
+}
 
 /*
  * meta_back_conn_cmp
@@ -52,64 +105,124 @@ meta_back_conn_cmp(
        metaconn_t      *mc1 = ( metaconn_t * )c1;
         metaconn_t     *mc2 = ( metaconn_t * )c2;
        
+       /* For shared sessions, conn is NULL. Only explicitly
+        * bound sessions will have non-NULL conn.
+        */
        return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
 }
 
 /*
- * meta_back_conn_dup
+ * meta_back_conndn_dup
  *
  * returns -1 in case a duplicate struct metaconn has been inserted;
  * used by avl stuff
  */
 int
-meta_back_conn_dup(
+meta_back_conndn_dup(
        void *c1,
        void *c2 )
 {
        metaconn_t      *mc1 = ( metaconn_t * )c1;
        metaconn_t      *mc2 = ( metaconn_t * )c2;
 
-       return( ( mc1->mc_conn == mc2->mc_conn ) ? -1 : 0 );
+       /* Cannot have more than one shared session with same DN */
+       if ( mc1->mc_conn == mc2->mc_conn &&
+               dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) )
+       {
+               return -1;
+       }
+               
+       return 0;
 }
 
 /*
  * Debug stuff (got it from libavl)
  */
-#if PRINT_CONNTREE > 0
+#if META_BACK_PRINT_CONNTREE > 0
 static void
-ravl_print( Avlnode *root, int depth )
+meta_back_print( metaconn_t *mc, char *avlstr )
 {
-       int     i;
-       
+       int     i;
+
+       fputs( "targets=[", stderr );
+       for ( i = 0; i < mc->mc_info->mi_ntargets; i++ ) {
+               fputc( mc->mc_conns[ i ].msc_ld ? '*' : 'o', stderr);
+       }
+       fputc( ']', stderr );
+
+       fprintf( stderr, " mc=%p local=\"%s\" conn=%p refcnt=%d%s %s\n",
+               (void *)mc,
+               mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
+               (void *)mc->mc_conn,
+               mc->mc_refcnt,
+               LDAP_BACK_CONN_TAINTED( mc ) ? " tainted" : "",
+               avlstr );
+}
+
+static void
+meta_back_ravl_print( Avlnode *root, int depth )
+{
+       int             i;
+
        if ( root == 0 ) {
                return;
        }
        
-       ravl_print( root->avl_right, depth + 1 );
+       meta_back_ravl_print( root->avl_right, depth + 1 );
        
        for ( i = 0; i < depth; i++ ) {
-               printf( "    " );
+               fprintf( stderr, "-" );
        }
+       fputc( ' ', stderr );
 
-       printf( "c(%d) %d\n", ( ( metaconn_t * )root->avl_data )->mc_conn->c_connid, root->avl_bf );
-       
-       ravl_print( root->avl_left, depth + 1 );
+       meta_back_print( (metaconn_t *)root->avl_data,
+               avl_bf2str( root->avl_bf ) );
+
+       meta_back_ravl_print( root->avl_left, depth + 1 );
 }
 
-static void
-myprint( Avlnode *root )
+/* NOTE: duplicate from back-ldap/bind.c */
+static char* priv2str[] = {
+       "privileged",
+       "privileged/TLS",
+       "anonymous",
+       "anonymous/TLS",
+       "bind",
+       "bind/TLS",
+       NULL
+};
+
+void
+meta_back_print_conntree( metainfo_t *mi, char *msg )
 {
-       printf( "********\n" );
+       int     c;
+
+       fprintf( stderr, "========> %s\n", msg );
        
-       if ( root == 0 ) {
-               printf( "\tNULL\n" );
+       for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
+               int             i = 0;
+               metaconn_t      *mc;
+
+               fprintf( stderr, "  %s[%d]\n", priv2str[ c ], mi->mi_conn_priv[ c ].mic_num );
+
+               LDAP_TAILQ_FOREACH( mc, &mi->mi_conn_priv[ c ].mic_priv, mc_q )
+               {
+                       fprintf( stderr, "    [%d] ", i );
+                       meta_back_print( mc, "" );
+                       i++;
+               }
+       }
+       
+       if ( mi->mi_conninfo.lai_tree == NULL ) {
+               fprintf( stderr, "\t(empty)\n" );
+
        } else {
-               ravl_print( root, 0 );
+               meta_back_ravl_print( mi->mi_conninfo.lai_tree, 0 );
        }
        
-       printf( "********\n" );
+       fprintf( stderr, "<======== %s\n", msg );
 }
-#endif /* PRINT_CONNTREE */
+#endif /* META_BACK_PRINT_CONNTREE */
 /*
  * End of debug stuff
  */
@@ -125,51 +238,25 @@ metaconn_alloc(
 {
        metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
        metaconn_t      *mc;
-       int             i, ntargets = mi->mi_ntargets;
+       int             ntargets = mi->mi_ntargets;
 
        assert( ntargets > 0 );
 
-       /* malloc once only; leave an extra one for one-past-end */
-       mc = ( metaconn_t * )ch_malloc( sizeof( metaconn_t )
-                       + sizeof( metasingleconn_t ) * ntargets );
+       /* malloc all in one */
+       mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t )
+               + sizeof( metasingleconn_t ) * ( ntargets - 1 ) );
        if ( mc == NULL ) {
                return NULL;
        }
 
-       mc->mc_conns = ( metasingleconn_t * )&mc[ 1 ];
+       mc->mc_info = mi;
 
-       for ( i = 0; i < ntargets; i++ ) {
-               mc->mc_conns[ i ].msc_ld = NULL;
-               BER_BVZERO( &mc->mc_conns[ i ].msc_bound_ndn );
-               BER_BVZERO( &mc->mc_conns[ i ].msc_cred );
-               mc->mc_conns[ i ].msc_bound = META_UNBOUND;
-               mc->mc_conns[ i ].msc_info = mi;
-       }
-
-       mc->mc_auth_target = META_BOUND_NONE;
-       ldap_pvt_thread_mutex_init( &mc->mc_mutex );
+       mc->mc_authz_target = META_BOUND_NONE;
+       mc->mc_refcnt = 1;
 
        return mc;
 }
 
-/*
- * meta_back_conn_free
- *
- * clears a metaconn
- */
-void
-meta_back_conn_free(
-       metaconn_t      *mc )
-{
-       if ( mc == NULL ) {
-               return;
-       }
-
-       ldap_pvt_thread_mutex_destroy( &mc->mc_mutex );
-       
-       free( mc );
-}
-
 /*
  * meta_back_init_one_conn
  * 
@@ -179,26 +266,136 @@ int
 meta_back_init_one_conn(
        Operation               *op,
        SlapReply               *rs,
-       metatarget_t            *mt, 
-       metasingleconn_t        *msc,
-       ldap_back_send_t        sendok )
+       metaconn_t              *mc,
+       int                     candidate,
+       int                     ispriv,
+       ldap_back_send_t        sendok,
+       int                     dolock )
 {
-       metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
-       int             vers;
-       dncookie        dc;
+       metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
+       metatarget_t            *mt = mi->mi_targets[ candidate ];
+       metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
+       int                     version;
+       dncookie                dc;
+       int                     isauthz = ( candidate == mc->mc_authz_target );
+       int                     do_return = 0;
+#ifdef HAVE_TLS
+       int                     is_ldaps = 0;
+#endif /* HAVE_TLS */
+
+       /* if the server is quarantined, and
+        * - the current interval did not expire yet, or
+        * - no more retries should occur,
+        * don't return the connection */
+       if ( mt->mt_isquarantined ) {
+               slap_retry_info_t       *ri = &mt->mt_quarantine;
+               int                     dont_retry = 0;
+
+               if ( mt->mt_quarantine.ri_interval ) {
+                       ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
+                       dont_retry = ( mt->mt_isquarantined > LDAP_BACK_FQ_NO );
+                       if ( dont_retry ) {
+                               dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
+                                       || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
+                               if ( !dont_retry ) {
+                                       if ( LogTest( LDAP_DEBUG_ANY ) ) {
+                                               char    buf[ SLAP_TEXT_BUFLEN ];
+
+                                               snprintf( buf, sizeof( buf ),
+                                                       "meta_back_init_one_conn[%d]: quarantine "
+                                                       "retry block #%d try #%d",
+                                                       candidate, ri->ri_idx, ri->ri_count );
+                                               Debug( LDAP_DEBUG_ANY, "%s %s.\n",
+                                                       op->o_log_prefix, buf, 0 );
+                                       }
+
+                                       mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
+                               }
+
+                       }
+                       ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
+               }
+
+               if ( dont_retry ) {
+                       rs->sr_err = LDAP_UNAVAILABLE;
+                       if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
+                               rs->sr_text = "Target is quarantined";
+                               send_ldap_result( op, rs );
+                       }
+                       return rs->sr_err;
+               }
+       }
+
+retry_lock:;
+       if ( dolock ) {
+               ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
+       }
 
        /*
         * Already init'ed
         */
-       if ( msc->msc_ld != NULL ) {
+       if ( LDAP_BACK_CONN_ISBOUND( msc )
+               || LDAP_BACK_CONN_ISANON( msc ) )
+       {
+               assert( msc->msc_ld != NULL );
                rs->sr_err = LDAP_SUCCESS;
-               goto error_return;
+               do_return = 1;
+
+       } else if ( META_BACK_CONN_CREATING( msc )
+               || LDAP_BACK_CONN_BINDING( msc ) )
+       {
+               if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
+                       if ( dolock ) {
+                               ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+                       }
+
+                       ldap_pvt_thread_yield();
+                       goto retry_lock;
+               }
+
+               /* sounds more appropriate */
+               rs->sr_err = LDAP_BUSY;
+               rs->sr_text = "No connections to target are available";
+               do_return = 1;
+
+       } else if ( META_BACK_CONN_INITED( msc ) ) {
+               assert( msc->msc_ld != NULL );
+               rs->sr_err = LDAP_SUCCESS;
+               do_return = 1;
+
+       } else {
+               /*
+                * creating...
+                */
+               META_BACK_CONN_CREATING_SET( msc );
+       }
+
+       if ( dolock ) {
+               ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
        }
+
+       if ( do_return ) {
+               if ( rs->sr_err != LDAP_SUCCESS
+                       && op->o_conn
+                       && ( sendok & LDAP_BACK_SENDERR ) )
+               {
+                       send_ldap_result( op, rs );
+               }
+
+               return rs->sr_err;
+       }
+
+       assert( msc->msc_ld == NULL );
        
        /*
         * Attempts to initialize the connection to the target ds
         */
+       ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
        rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
+#ifdef HAVE_TLS
+       is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
+#endif /* HAVE_TLS */
+       ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
        if ( rs->sr_err != LDAP_SUCCESS ) {
                goto error_return;
        }
@@ -207,23 +404,33 @@ meta_back_init_one_conn(
         * Set LDAP version. This will always succeed: If the client
         * bound with a particular version, then so can we.
         */
-       vers = op->o_conn->c_protocol;
-       ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
+       if ( mt->mt_version != 0 ) {
+               version = mt->mt_version;
+
+       } else if ( op->o_conn->c_protocol != 0 ) {
+               version = op->o_conn->c_protocol;
 
-       /* automatically chase referrals ("chase-referrals"/"dont-chase-referrals" statement) */
-       if ( LDAP_BACK_CHASE_REFERRALS( mi ) ) {
-               ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
+       } else {
+               version = LDAP_VERSION3;
        }
+       ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
+       ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
+
+       /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
+       ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
+               LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
 
 #ifdef HAVE_TLS
-       /* start TLS ("start-tls"/"try-start-tls" statements) */
-       if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
-                       && !ldap_is_ldaps_url( mt->mt_uri ) )
+       /* start TLS ("tls [try-]{start|propagate}" statement) */
+       if ( ( LDAP_BACK_USE_TLS( mi )
+               || ( op->o_conn->c_is_tls
+                       && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
+               && !is_ldaps )
        {
 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
                /*
-                * use asynchronous StartTLS
-                * in case, chase referral (not implemented yet)
+                * use asynchronous StartTLS; in case, chase referral
+                * FIXME: OpenLDAP does not return referral on StartTLS yet
                 */
                int             msgid;
 
@@ -231,32 +438,53 @@ meta_back_init_one_conn(
                if ( rs->sr_err == LDAP_SUCCESS ) {
                        LDAPMessage     *res = NULL;
                        int             rc, nretries = mt->mt_nretries;
-                       struct timeval  tv = { 0, 0 };
+                       struct timeval  tv;
+
+                       LDAP_BACK_TV_SET( &tv );
 
 retry:;
                        rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
-                       if ( rc < 0 ) {
+                       switch ( rc ) {
+                       case -1:
                                rs->sr_err = LDAP_OTHER;
+                               break;
 
-                       } else if ( rc == 0 ) {
+                       case 0:
                                if ( nretries != 0 ) {
                                        if ( nretries > 0 ) {
                                                nretries--;
                                        }
-                                       tv.tv_sec = 0;
-                                       tv.tv_usec = 100000;
+                                       LDAP_BACK_TV_SET( &tv );
                                        goto retry;
                                }
                                rs->sr_err = LDAP_OTHER;
+                               break;
 
-                       } else if ( rc == LDAP_RES_EXTENDED ) {
+                       default:
+                               /* only touch when activity actually took place... */
+                               if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
+                                       msc->msc_time = op->o_time;
+                               }
+                               break;
+                       }
+
+                       if ( rc == LDAP_RES_EXTENDED ) {
                                struct berval   *data = NULL;
 
-                               rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
-                                               NULL, &data, 0 );
+                               /* NOTE: right now, data is unused, so don't get it */
+                               rs->sr_err = ldap_parse_extended_result( msc->msc_ld,
+                                       res, NULL, NULL /* &data */ , 0 );
                                if ( rs->sr_err == LDAP_SUCCESS ) {
-                                       rs->sr_err = ldap_result2error( msc->msc_ld, res, 1 );
+                                       int             err;
+
+                                       /* FIXME: matched? referrals? response controls? */
+                                       rs->sr_err = ldap_parse_result( msc->msc_ld,
+                                               res, &err, NULL, NULL, NULL, NULL, 1 );
                                        res = NULL;
+
+                                       if ( rs->sr_err == LDAP_SUCCESS ) {
+                                               rs->sr_err = err;
+                                       }
                                        
                                        /* FIXME: in case a referral 
                                         * is returned, should we try
@@ -266,15 +494,14 @@ retry:;
                                                ldap_install_tls( msc->msc_ld );
 
                                        } else if ( rs->sr_err == LDAP_REFERRAL ) {
+                                               /* FIXME: LDAP_OPERATIONS_ERROR? */
                                                rs->sr_err = LDAP_OTHER;
-                                               rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
+                                               rs->sr_text = "Unwilling to chase referral "
+                                                       "returned by Start TLS exop";
                                        }
 
                                        if ( data ) {
-                                               if ( data->bv_val ) {
-                                                       ber_memfree( data->bv_val );
-                                               }
-                                               ber_memfree( data );
+                                               ber_bvfree( data );
                                        }
                                }
 
@@ -298,9 +525,20 @@ retry:;
                 * of misconfiguration, but also when used in the chain 
                 * overlay, where the "uri" can be parsed out of a referral */
                if ( rs->sr_err == LDAP_SERVER_DOWN
-                               || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
+                       || ( rs->sr_err != LDAP_SUCCESS
+                               && LDAP_BACK_TLS_CRITICAL( mi ) ) )
                {
-                       ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
+
+#ifdef DEBUG_205
+                       Debug( LDAP_DEBUG_ANY,
+                               "### %s meta_back_init_one_conn(TLS) "
+                               "ldap_unbind_ext[%d] ld=%p\n",
+                               op->o_log_prefix, candidate,
+                               (void *)msc->msc_ld );
+#endif /* DEBUG_205 */
+
+                       /* need to trash a failed Start TLS */
+                       meta_clear_one_candidate( op, mc, candidate );
                        goto error_return;
                }
        }
@@ -309,58 +547,108 @@ retry:;
        /*
         * Set the network timeout if set
         */
-       if ( mi->mi_network_timeout != 0 ) {
+       if ( mt->mt_network_timeout != 0 ) {
                struct timeval  network_timeout;
 
                network_timeout.tv_usec = 0;
-               network_timeout.tv_sec = mi->mi_network_timeout;
+               network_timeout.tv_sec = mt->mt_network_timeout;
 
                ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
                                (void *)&network_timeout );
        }
 
-       /*
-        * Sets a cookie for the rewrite session
-        */
-       ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
-
        /*
         * If the connection DN is not null, an attempt to rewrite it is made
         */
-       if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
-               dc.target = mt;
-               dc.conn = op->o_conn;
-               dc.rs = rs;
-               dc.ctx = "bindDN";
-               
-               /*
-                * Rewrite the bind dn if needed
-                */
-               if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
-                                       &msc->msc_bound_ndn ) )
-               {
-                       goto error_return;
+
+       if ( ispriv ) {
+               if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
+                       ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
+                       if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
+                               if ( !BER_BVISNULL( &msc->msc_cred ) ) {
+                                       memset( msc->msc_cred.bv_val, 0,
+                                               msc->msc_cred.bv_len );
+                               }
+                               ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
+                       }
+
+               } else {
+                       ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
                }
 
-               /* copy the DN idf needed */
-               if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
-                       ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
+       } else {
+               if ( !BER_BVISNULL( &msc->msc_cred ) ) {
+                       memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
+                       ber_memfree_x( msc->msc_cred.bv_val, NULL );
+                       BER_BVZERO( &msc->msc_cred );
                }
+               if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
+                       ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
+                       BER_BVZERO( &msc->msc_bound_ndn );
+               }
+               if ( !BER_BVISEMPTY( &op->o_ndn )
+                       && SLAP_IS_AUTHZ_BACKEND( op )
+                       && isauthz )
+               {
+                       dc.target = mt;
+                       dc.conn = op->o_conn;
+                       dc.rs = rs;
+                       dc.ctx = "bindDN";
+               
+                       /*
+                        * Rewrite the bind dn if needed
+                        */
+                       if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
+                                               &msc->msc_bound_ndn ) )
+                       {
 
-               assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
+#ifdef DEBUG_205
+                               Debug( LDAP_DEBUG_ANY,
+                                       "### %s meta_back_init_one_conn(rewrite) "
+                                       "ldap_unbind_ext[%d] ld=%p\n",
+                                       op->o_log_prefix, candidate,
+                                       (void *)msc->msc_ld );
+#endif /* DEBUG_205 */
+
+                               /* need to trash a connection not fully established */
+                               meta_clear_one_candidate( op, mc, candidate );
+                               goto error_return;
+                       }
+                       
+                       /* copy the DN if needed */
+                       if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
+                               ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
+                       }
 
-       } else {
-               ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
+                       assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
+
+               } else {
+                       ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
+               }
        }
 
-       msc->msc_bound = META_UNBOUND;
+       assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
 
 error_return:;
+       if ( dolock ) {
+               ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
+       }
+       META_BACK_CONN_CREATING_CLEAR( msc );
+       if ( rs->sr_err == LDAP_SUCCESS ) {
+               /*
+                * Sets a cookie for the rewrite session
+                */
+               ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
+               META_BACK_CONN_INITED_SET( msc );
+       }
+       if ( dolock ) {
+               ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+       }
+
        if ( rs->sr_err != LDAP_SUCCESS ) {
                rs->sr_err = slap_map_api2result( rs );
                if ( sendok & LDAP_BACK_SENDERR ) {
                        send_ldap_result( op, rs );
-                       rs->sr_text = NULL;
                }
        }
 
@@ -376,30 +664,153 @@ int
 meta_back_retry(
        Operation               *op,
        SlapReply               *rs,
-       metaconn_t              *mc,
+       metaconn_t              **mcp,
        int                     candidate,
        ldap_back_send_t        sendok )
 {
        metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
-       metatarget_t            *mt = &mi->mi_targets[ candidate ];
-       int                     rc;
+       metatarget_t            *mt = mi->mi_targets[ candidate ];
+       metaconn_t              *mc = *mcp;
        metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
+       int                     rc = LDAP_UNAVAILABLE,
+                               binding,
+                               quarantine = 1;
+
+       ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
+
+       assert( !META_BACK_CONN_CREATING( msc ) );
+       binding = LDAP_BACK_CONN_BINDING( msc );
+       LDAP_BACK_CONN_BINDING_CLEAR( msc );
+
+       assert( mc->mc_refcnt > 0 );
+       if ( mc->mc_refcnt == 1 ) {
+               if ( LogTest( LDAP_DEBUG_ANY ) ) {
+                       char    buf[ SLAP_TEXT_BUFLEN ];
+
+                       /* this lock is required; however,
+                        * it's invoked only when logging is on */
+                       ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
+                       snprintf( buf, sizeof( buf ),
+                               "retrying URI=\"%s\" DN=\"%s\"",
+                               mt->mt_uri,
+                               BER_BVISNULL( &msc->msc_bound_ndn ) ?
+                                       "" : msc->msc_bound_ndn.bv_val );
+                       ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
+
+                       Debug( LDAP_DEBUG_ANY,
+                               "%s meta_back_retry[%d]: %s.\n",
+                               op->o_log_prefix, candidate, buf );
+               }
+
+               meta_clear_one_candidate( op, mc, candidate );
+               LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
+
+               ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
+
+               /* mc here must be the regular mc, reset and ready for init */
+               rc = meta_back_init_one_conn( op, rs, mc, candidate,
+                       LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
+
+               /* restore the "binding" flag, in case */
+               if ( binding ) {
+                       LDAP_BACK_CONN_BINDING_SET( msc );
+               }
+
+               if ( rc == LDAP_SUCCESS ) {
+                       quarantine = 0;
+                       rc = meta_back_single_dobind( op, rs, mcp, candidate,
+                               sendok, mt->mt_nretries, 0 );
+
+                       Debug( LDAP_DEBUG_ANY,
+                               "%s meta_back_retry[%d]: "
+                               "meta_back_single_dobind=%d\n",
+                               op->o_log_prefix, candidate, rc );
+                       if ( rc == LDAP_SUCCESS ) {
+                               if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
+                                       !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
+                               {
+                                       LDAP_BACK_CONN_ISBOUND_SET( msc );
+
+                               } else {
+                                       LDAP_BACK_CONN_ISANON_SET( msc );
+                               }
+
+                               /* when bound, dispose of the "binding" flag */
+                               if ( binding ) {
+                                       LDAP_BACK_CONN_BINDING_CLEAR( msc );
+                               }
+                       }
+               }
+
+               /* don't send twice */
+               sendok &= ~LDAP_BACK_SENDERR;
+       }
+
+       if ( rc != LDAP_SUCCESS ) {
+               SlapReply               *candidates = meta_back_candidates_get( op );
+
+               candidates[ candidate ].sr_err = rc;
+
+               if ( *mcp != NULL ) {
+                       if ( mc->mc_refcnt == 1 ) {
+                               if ( binding ) {
+                                       LDAP_BACK_CONN_BINDING_CLEAR( msc );
+                               }
+                               (void)meta_clear_one_candidate( op, mc, candidate );
+                       }
+
+                       LDAP_BACK_CONN_TAINTED_SET( mc );
+                       /* only release if mandatory; otherwise
+                        * let the caller do what's best before
+                        * releasing */
+                       if ( META_BACK_ONERR_STOP( mi ) ) {
+                               meta_back_release_conn_lock( mi, mc, 0 );
+                               *mcp = NULL;
+
+                       } else {
+#if META_BACK_PRINT_CONNTREE > 0
+                               meta_back_print_conntree( mi, ">>> meta_back_retry" );
+#endif /* META_BACK_PRINT_CONNTREE */
+
+                               /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
+                               if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
+                                       if ( mc->mc_q.tqe_prev != NULL ) {
+                                               assert( LDAP_BACK_CONN_CACHED( mc ) );
+                                               assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
+                                               LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
+                                                       mc, mc_q );
+                                               mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
+                                               LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
+
+                                       } else {
+                                               assert( !LDAP_BACK_CONN_CACHED( mc ) );
+                                       }
 
-       ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
+                               } else {
+                                       /* FIXME: check if in tree, for consistency? */
+                                       (void)avl_delete( &mi->mi_conninfo.lai_tree,
+                                               ( caddr_t )mc, meta_back_conndnmc_cmp );
+                               }
+                               LDAP_BACK_CONN_CACHED_CLEAR( mc );
 
-       ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
-        msc->msc_ld = NULL;
-        msc->msc_bound = 0;
+#if META_BACK_PRINT_CONNTREE > 0
+                               meta_back_print_conntree( mi, "<<< meta_back_retry" );
+#endif /* META_BACK_PRINT_CONNTREE */
+                       }
+               }
 
-        /* mc here must be the regular mc, reset and ready for init */
-        rc = meta_back_init_one_conn( op, rs, mt, msc, sendok );
+               if ( sendok & LDAP_BACK_SENDERR ) {
+                       rs->sr_err = rc;
+                       rs->sr_text = "Unable to retry";
+                       send_ldap_result( op, rs );
+               }
+       }
 
-       if ( rc == LDAP_SUCCESS ) {
-               rc = meta_back_single_dobind( op, rs, mc, candidate,
-                               sendok, mt->mt_nretries );
-        }
+       if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
+               meta_back_quarantine( op, rs, candidate );
+       }
 
-       ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
+       ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
 
        return rc == LDAP_SUCCESS ? 1 : 0;
 }
@@ -414,7 +825,7 @@ meta_back_conn_cb( Operation *op, SlapReply *rs )
 
        switch ( rs->sr_type ) {
        case REP_SEARCH:
-               ((int *)op->o_callback->sc_private)[0] = (int)op->o_private;
+               ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
                break;
 
        case REP_SEARCHREF:
@@ -436,7 +847,7 @@ meta_back_get_candidate(
        struct berval   *ndn )
 {
        metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
-       int             candidate;
+       long            candidate;
 
        /*
         * tries to get a unique candidate
@@ -449,10 +860,9 @@ meta_back_get_candidate(
         */
        if ( candidate == META_TARGET_NONE ) {
                rs->sr_err = LDAP_NO_SUCH_OBJECT;
-               rs->sr_text = "no suitable candidate target found";
+               rs->sr_text = "No suitable candidate target found";
 
        } else if ( candidate == META_TARGET_MULTIPLE ) {
-               Filter          f = { 0 };
                Operation       op2 = *op;
                SlapReply       rs2 = { 0 };
                slap_callback   cb2 = { 0 };
@@ -471,10 +881,8 @@ meta_back_get_candidate(
                op2.ors_slimit = 1;
                op2.ors_tlimit = SLAP_NO_LIMIT;
 
-               f.f_choice = LDAP_FILTER_PRESENT;
-               f.f_desc = slap_schema.si_ad_objectClass;
-               op2.ors_filter = &f;
-               BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
+               op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
+               op2.ors_filterstr = *slap_filterstr_objectClass_pres;
 
                op2.o_callback = &cb2;
                cb2.sc_response = meta_back_conn_cb;
@@ -493,7 +901,7 @@ meta_back_get_candidate(
                         * and a default target is defined, and it is
                         * a candidate, try using it (FIXME: YMMV) */
                        if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
-                               && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ].mt_nsuffix,
+                               && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
                                                ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
                        {
                                candidate = mi->mi_defaulttarget;
@@ -502,49 +910,76 @@ meta_back_get_candidate(
 
                        } else {
                                rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
-                               rs->sr_text = "cannot select unique candidate target";
+                               rs->sr_text = "Unable to select unique candidate target";
                        }
                        break;
                }
+
+       } else {
+               rs->sr_err = LDAP_SUCCESS;
        }
 
        return candidate;
 }
 
+static void    *meta_back_candidates_dummy;
+
 static void
-meta_back_candidate_keyfree(
+meta_back_candidates_keyfree(
        void            *key,
        void            *data )
 {
+       metacandidates_t        *mc = (metacandidates_t *)data;
+
+       ber_memfree_x( mc->mc_candidates, NULL );
        ber_memfree_x( data, NULL );
 }
 
 SlapReply *
 meta_back_candidates_get( Operation *op )
 {
-       metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
-       void            *data = NULL;
+       metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
+       metacandidates_t        *mc;
 
        if ( op->o_threadctx ) {
+               void            *data = NULL;
+
                ldap_pvt_thread_pool_getkey( op->o_threadctx,
-                               meta_back_candidate_keyfree, &data, NULL );
+                               &meta_back_candidates_dummy, &data, NULL );
+               mc = (metacandidates_t *)data;
+
        } else {
-               data = (void *)mi->mi_candidates;
+               mc = mi->mi_candidates;
        }
 
-       if ( data == NULL ) {
-               data = ber_memalloc( sizeof( SlapReply ) * mi->mi_ntargets );
+       if ( mc == NULL ) {
+               mc = ch_calloc( sizeof( metacandidates_t ), 1 );
+               mc->mc_ntargets = mi->mi_ntargets;
+               mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
                if ( op->o_threadctx ) {
+                       void            *data = NULL;
+
+                       data = (void *)mc;
                        ldap_pvt_thread_pool_setkey( op->o_threadctx,
-                                       meta_back_candidate_keyfree, data,
-                                       meta_back_candidate_keyfree );
+                                       &meta_back_candidates_dummy, data,
+                                       meta_back_candidates_keyfree,
+                                       NULL, NULL );
 
                } else {
-                       mi->mi_candidates = (SlapReply *)data;
+                       mi->mi_candidates = mc;
                }
+
+       } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
+               /* NOTE: in the future, may want to allow back-config
+                * to add/remove targets from back-meta... */
+               mc->mc_candidates = ch_realloc( mc->mc_candidates,
+                               sizeof( SlapReply ) * mi->mi_ntargets );
+               memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
+                       sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
+               mc->mc_ntargets = mi->mi_ntargets;
        }
 
-       return (SlapReply *)data;
+       return mc->mc_candidates;
 }
 
 /*
@@ -588,7 +1023,8 @@ meta_back_getconn(
        ldap_back_send_t        sendok )
 {
        metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
-       metaconn_t      *mc, mc_curr;
+       metaconn_t      *mc = NULL,
+                       mc_curr = { 0 };
        int             cached = META_TARGET_NONE,
                        i = META_TARGET_NONE,
                        err = LDAP_SUCCESS,
@@ -597,25 +1033,156 @@ meta_back_getconn(
 
 
        meta_op_type    op_type = META_OP_REQUIRE_SINGLE;
-       int             parent = 0,
-                       newparent = 0;
+       enum            {
+               META_DNTYPE_ENTRY,
+               META_DNTYPE_PARENT,
+               META_DNTYPE_NEWPARENT
+       }               dn_type = META_DNTYPE_ENTRY;
        struct berval   ndn = op->o_req_ndn,
                        pndn;
 
        SlapReply       *candidates = meta_back_candidates_get( op );
 
-       /* Searches for a metaconn in the avl tree */
-       mc_curr.mc_conn = op->o_conn;
-       ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
-       mc = (metaconn_t *)avl_find( mi->mi_conntree, 
-               (caddr_t)&mc_curr, meta_back_conn_cmp );
-       ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
+       /* Internal searches are privileged and shared. So is root. */
+       if ( ( !BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ALWAYS( mi ) )
+               || ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ANON( mi ) )
+               || op->o_do_not_cache || be_isroot( op ) )
+       {
+               LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
+               mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
+               LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op );
+
+       } else if ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_NOANON( mi ) )
+       {
+               LDAP_BACK_CONN_ISANON_SET( &mc_curr );
+               BER_BVSTR( &mc_curr.mc_local_ndn, "" );
+               LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
+
+       } else {
+               mc_curr.mc_local_ndn = op->o_ndn;
+
+               /* Explicit binds must not be shared */
+               if ( !BER_BVISEMPTY( &op->o_ndn )
+                       || op->o_tag == LDAP_REQ_BIND
+                       || SLAP_IS_AUTHZ_BACKEND( op ) )
+               {
+                       mc_curr.mc_conn = op->o_conn;
+       
+               } else {
+                       LDAP_BACK_CONN_ISANON_SET( &mc_curr );
+                       LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
+               }
+       }
+
+       /* Explicit Bind requests always get their own conn */
+       if ( sendok & LDAP_BACK_BINDING ) {
+               mc_curr.mc_conn = op->o_conn;
+
+       } else {
+               /* Searches for a metaconn in the avl tree */
+retry_lock:;
+               ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
+               if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr ) ) {
+                       /* lookup a conn that's not binding */
+                       LDAP_TAILQ_FOREACH( mc,
+                               &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv,
+                               mc_q )
+                       {
+                               if ( !LDAP_BACK_CONN_BINDING( mc ) && mc->mc_refcnt == 0 ) {
+                                       break;
+                               }
+                       }
+
+                       if ( mc != NULL ) {
+                               if ( mc != LDAP_TAILQ_LAST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
+                                       metaconn_t, mc_q ) )
+                               {
+                                       LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
+                                               mc, mc_q );
+                                       LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
+                                       LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
+                                               mc, mc_q );
+                               }
+
+                       } else if ( !LDAP_BACK_USE_TEMPORARIES( mi )
+                               && mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_num == mi->mi_conn_priv_max )
+                       {
+                               mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv );
+                       }
+                       
+
+               } else {
+                       mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree, 
+                               (caddr_t)&mc_curr, meta_back_conndn_cmp );
+               }
+
+               if ( mc ) {
+                       /* catch taint errors */
+                       assert( !LDAP_BACK_CONN_TAINTED( mc ) );
+
+                       /* Don't reuse connections while they're still binding
+                        * NOTE: only makes sense for binds */
+                       if ( LDAP_BACK_CONN_BINDING( mc ) ) {
+                               if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
+                                       ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+
+                                       ldap_pvt_thread_yield();
+                                       goto retry_lock;
+                               }
+
+                               /* release conn, and create a temporary */
+                               mc = NULL;
+
+                       } else {
+                               if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
+                                       || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
+                               {
+#if META_BACK_PRINT_CONNTREE > 0
+                                       meta_back_print_conntree( mi,
+                                               ">>> meta_back_getconn(expired)" );
+#endif /* META_BACK_PRINT_CONNTREE */
+
+                                       /* don't let anyone else use this expired connection */
+                                       if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
+                                               if ( mc->mc_q.tqe_prev != NULL ) {
+                                                       assert( LDAP_BACK_CONN_CACHED( mc ) );
+                                                       assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
+                                                       LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
+                                                               mc, mc_q );
+                                                       mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
+                                                       LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
+
+                                               } else {
+                                                       assert( !LDAP_BACK_CONN_CACHED( mc ) );
+                                               }
+
+                                       } else {
+                                               (void)avl_delete( &mi->mi_conninfo.lai_tree,
+                                                       (caddr_t)mc, meta_back_conndnmc_cmp );
+                                       }
+
+#if META_BACK_PRINT_CONNTREE > 0
+                                       meta_back_print_conntree( mi,
+                                               "<<< meta_back_getconn(expired)" );
+#endif /* META_BACK_PRINT_CONNTREE */
+                                       LDAP_BACK_CONN_TAINTED_SET( mc );
+                                       LDAP_BACK_CONN_CACHED_CLEAR( mc );
+
+                                       Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired (tainted).\n",
+                                               op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc ) );
+                               }
+
+                               mc->mc_refcnt++;
+                       }
+               }
+               ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+       }
 
        switch ( op->o_tag ) {
        case LDAP_REQ_ADD:
                /* if we go to selection, the entry must not exist,
                 * and we must be able to resolve the parent */
-               parent = 1;
+               dn_type = META_DNTYPE_PARENT;
                dnParent( &ndn, &pndn );
                break;
 
@@ -623,24 +1190,25 @@ meta_back_getconn(
                /* if nnewSuperior is not NULL, it must resolve
                 * to the same candidate as the req_ndn */
                if ( op->orr_nnewSup ) {
-                       newparent = 1;
+                       dn_type = META_DNTYPE_NEWPARENT;
                }
                break;
 
        case LDAP_REQ_BIND:
                /* if bound as rootdn, the backend must bind to all targets
-                * with the administrative identity */
+                * with the administrative identity
+                * (unless pseoudoroot-bind-defer is TRUE) */
                if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
                        op_type = META_OP_REQUIRE_ALL;
                }
                break;
 
+       case LDAP_REQ_COMPARE:
        case LDAP_REQ_DELETE:
        case LDAP_REQ_MODIFY:
                /* just a unique candidate */
                break;
 
-       case LDAP_REQ_COMPARE:
        case LDAP_REQ_SEARCH:
                /* allow multiple candidates for the searchBase */
                op_type = META_OP_ALLOW_MULTIPLE;
@@ -657,24 +1225,42 @@ meta_back_getconn(
        if ( op_type == META_OP_REQUIRE_ALL ) {
 
                /* Looks like we didn't get a bind. Open a new session... */
-               if ( !mc ) {
+               if ( mc == NULL ) {
+                       assert( new_conn == 0 );
                        mc = metaconn_alloc( op );
-                       mc->mc_conn = op->o_conn;
+                       mc->mc_conn = mc_curr.mc_conn;
+                       ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
                        new_conn = 1;
+                       if ( sendok & LDAP_BACK_BINDING ) {
+                               LDAP_BACK_CONN_BINDING_SET( mc );
+                       }
+                       if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
+                               LDAP_BACK_CONN_ISPRIV_SET( mc );
+
+                       } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
+                               LDAP_BACK_CONN_ISANON_SET( mc );
+                       }
+
+               } else if ( 0 ) {
+                       /* TODO: if any of the connections is binding,
+                        * release mc and create a new one */
                }
 
                for ( i = 0; i < mi->mi_ntargets; i++ ) {
-
                        /*
                         * The target is activated; if needed, it is
                         * also init'd
                         */
-                       int lerr = meta_back_init_one_conn( op, rs, &mi->mi_targets[ i ],
-                                       &mc->mc_conns[ i ], sendok );
-                       if ( lerr == LDAP_SUCCESS ) {
-                               candidates[ i ].sr_tag = META_CANDIDATE;
+                       candidates[ i ].sr_err = meta_back_init_one_conn( op,
+                               rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
+                               LDAP_BACK_DONTSEND, !new_conn );
+                       if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
+                               if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
+                                       LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
+                               }
+                               META_CANDIDATE_SET( &candidates[ i ] );
                                ncandidates++;
-                               
+       
                        } else {
                                
                                /*
@@ -682,11 +1268,35 @@ meta_back_getconn(
                                 * be init'd, should the other ones
                                 * be tried?
                                 */
-                               candidates[ i ].sr_tag = META_NOT_CANDIDATE;
-                               err = lerr;
+                               META_CANDIDATE_RESET( &candidates[ i ] );
+                               err = candidates[ i ].sr_err;
                                continue;
                        }
                }
+
+               if ( ncandidates == 0 ) {
+                       if ( new_conn ) {
+                               mc->mc_refcnt = 0;
+                               meta_back_conn_free( mc );
+
+                       } else {
+                               meta_back_release_conn( mi, mc );
+                       }
+
+                       rs->sr_err = LDAP_NO_SUCH_OBJECT;
+                       rs->sr_text = "Unable to select valid candidates";
+
+                       if ( sendok & LDAP_BACK_SENDERR ) {
+                               if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
+                                       rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
+                               }
+                               send_ldap_result( op, rs );
+                               rs->sr_matched = NULL;
+                       }
+
+                       return NULL;
+               }
+
                goto done;
        }
        
@@ -698,10 +1308,13 @@ meta_back_getconn(
        }
 
        if ( op_type == META_OP_REQUIRE_SINGLE ) {
-               int     j;
+               metatarget_t            *mt = NULL;
+               metasingleconn_t        *msc = NULL;
+
+               int                     j;
 
                for ( j = 0; j < mi->mi_ntargets; j++ ) {
-                       candidates[ j ].sr_tag = META_NOT_CANDIDATE;
+                       META_CANDIDATE_RESET( &candidates[ j ] );
                }
 
                /*
@@ -711,46 +1324,95 @@ meta_back_getconn(
                if ( i == META_TARGET_NONE ) {
                        i = meta_back_get_candidate( op, rs, &ndn );
 
-                       if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && parent ) {
+                       if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
                                i = meta_back_get_candidate( op, rs, &pndn );
                        }
        
-                       if ( rs->sr_err != LDAP_SUCCESS ) {
+                       if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
+                               if ( mc != NULL ) {
+                                       meta_back_release_conn( mi, mc );
+                               }
+
                                if ( sendok & LDAP_BACK_SENDERR ) {
+                                       if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
+                                               rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
+                                       }
                                        send_ldap_result( op, rs );
-                                       rs->sr_text = NULL;
+                                       rs->sr_matched = NULL;
                                }
+                       
                                return NULL;
                        }
                }
 
-               if ( newparent && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
+               if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
                {
+                       if ( mc != NULL ) {
+                               meta_back_release_conn( mi, mc );
+                       }
+
                        rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
-                       rs->sr_text = "cross-target rename not supported";
+                       rs->sr_text = "Cross-target rename not supported";
                        if ( sendok & LDAP_BACK_SENDERR ) {
                                send_ldap_result( op, rs );
-                               rs->sr_text = NULL;
                        }
+
                        return NULL;
                }
 
                Debug( LDAP_DEBUG_TRACE,
-       "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
+       "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
                                i, op->o_req_ndn.bv_val, 0 );
 
-               /* Retries searching for a metaconn in the avl tree */
-               mc_curr.mc_conn = op->o_conn;
-               ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
-               mc = (metaconn_t *)avl_find( mi->mi_conntree, 
-                       (caddr_t)&mc_curr, meta_back_conn_cmp );
-               ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
+               if ( mc == NULL ) {
+                       /* Retries searching for a metaconn in the avl tree
+                        * the reason is that the connection might have been
+                        * created by meta_back_get_candidate() */
+                       if ( !( sendok & LDAP_BACK_BINDING ) ) {
+retry_lock2:;
+                               ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
+                               mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree, 
+                                       (caddr_t)&mc_curr, meta_back_conndn_cmp );
+                               if ( mc != NULL ) {
+                                       /* catch taint errors */
+                                       assert( !LDAP_BACK_CONN_TAINTED( mc ) );
+
+                                       /* Don't reuse connections while they're still binding */
+                                       if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
+                                               || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
+                                       {
+                                               if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
+                                                       ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+                                                       ldap_pvt_thread_yield();
+                                                       goto retry_lock2;
+                                               }
 
-               /* Looks like we didn't get a bind. Open a new session... */
-               if ( !mc ) {
-                       mc = metaconn_alloc( op );
-                       mc->mc_conn = op->o_conn;
-                       new_conn = 1;
+                                               mc = NULL;
+
+                                       } else {
+                                               mc->mc_refcnt++;
+                                       }
+                               }
+                               ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+                       }
+
+                       /* Looks like we didn't get a bind. Open a new session... */
+                       if ( mc == NULL ) {
+                               assert( new_conn == 0 );
+                               mc = metaconn_alloc( op );
+                               mc->mc_conn = mc_curr.mc_conn;
+                               ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
+                               new_conn = 1;
+                               if ( sendok & LDAP_BACK_BINDING ) {
+                                       LDAP_BACK_CONN_BINDING_SET( mc );
+                               }
+                               if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
+                                       LDAP_BACK_CONN_ISPRIV_SET( mc );
+
+                               } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
+                                       LDAP_BACK_CONN_ISANON_SET( mc );
+                               }
+                       }
                }
 
                /*
@@ -758,32 +1420,41 @@ meta_back_getconn(
                 */
                ( void )meta_clear_unused_candidates( op, i );
 
+               mt = mi->mi_targets[ i ];
+               msc = &mc->mc_conns[ i ];
+
                /*
                 * The target is activated; if needed, it is
                 * also init'd. In case of error, meta_back_init_one_conn
                 * sends the appropriate result.
                 */
-               err = meta_back_init_one_conn( op, rs, &mi->mi_targets[ i ],
-                               &mc->mc_conns[ i ], sendok );
-               if ( err == LDAP_SUCCESS ) {
-                       candidates[ i ].sr_tag = META_CANDIDATE;
-                       ncandidates++;
-
-               } else {
-               
+               err = meta_back_init_one_conn( op, rs, mc, i,
+                       LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
+               if ( err != LDAP_SUCCESS ) {
                        /*
                         * FIXME: in case one target cannot
                         * be init'd, should the other ones
                         * be tried?
                         */
-                       candidates[ i ].sr_tag = META_NOT_CANDIDATE;
+                       META_CANDIDATE_RESET( &candidates[ i ] );
                        if ( new_conn ) {
-                               ( void )meta_clear_one_candidate( &mc->mc_conns[ i ] );
+                               mc->mc_refcnt = 0;
                                meta_back_conn_free( mc );
+
+                       } else {
+                               meta_back_release_conn( mi, mc );
                        }
                        return NULL;
                }
 
+               if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
+                       LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
+               }
+
+               candidates[ i ].sr_err = LDAP_SUCCESS;
+               META_CANDIDATE_SET( &candidates[ i ] );
+               ncandidates++;
+
                if ( candidate ) {
                        *candidate = i;
                }
@@ -794,32 +1465,52 @@ meta_back_getconn(
        } else {
 
                /* Looks like we didn't get a bind. Open a new session... */
-               if ( !mc ) {
+               if ( mc == NULL ) {
+                       assert( new_conn == 0 );
                        mc = metaconn_alloc( op );
-                       mc->mc_conn = op->o_conn;
+                       mc->mc_conn = mc_curr.mc_conn;
+                       ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
                        new_conn = 1;
+                       if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
+                               LDAP_BACK_CONN_ISPRIV_SET( mc );
+
+                       } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
+                               LDAP_BACK_CONN_ISANON_SET( mc );
+                       }
                }
 
                for ( i = 0; i < mi->mi_ntargets; i++ ) {
+                       metatarget_t            *mt = mi->mi_targets[ i ];
+
+                       META_CANDIDATE_RESET( &candidates[ i ] );
+
                        if ( i == cached 
-                               || meta_back_is_candidate( &mi->mi_targets[ i ].mt_nsuffix,
-                                               &op->o_req_ndn, LDAP_SCOPE_SUBTREE ) )
+                               || meta_back_is_candidate( mt, &op->o_req_ndn,
+                                       LDAP_SCOPE_SUBTREE ) )
                        {
 
                                /*
                                 * The target is activated; if needed, it is
                                 * also init'd
                                 */
-                               int lerr = meta_back_init_one_conn( op, rs,
-                                               &mi->mi_targets[ i ],
-                                               &mc->mc_conns[ i ], sendok );
+                               int lerr = meta_back_init_one_conn( op, rs, mc, i,
+                                       LDAP_BACK_CONN_ISPRIV( &mc_curr ),
+                                       LDAP_BACK_DONTSEND, !new_conn );
+                               candidates[ i ].sr_err = lerr;
                                if ( lerr == LDAP_SUCCESS ) {
-                                       candidates[ i ].sr_tag = META_CANDIDATE;
+                                       META_CANDIDATE_SET( &candidates[ i ] );
                                        ncandidates++;
 
-                                       Debug( LDAP_DEBUG_TRACE, "%s: meta_back_init_one_conn(%d)\n",
+                                       Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
                                                op->o_log_prefix, i, 0 );
 
+                               } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
+                                       META_CANDIDATE_SET( &candidates[ i ] );
+
+                                       Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
+                                               op->o_log_prefix, i,
+                                               mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
+
                                } else {
                                
                                        /*
@@ -828,36 +1519,65 @@ meta_back_getconn(
                                         * be tried?
                                         */
                                        if ( new_conn ) {
-                                               ( void )meta_clear_one_candidate( &mc->mc_conns[ i ] );
+                                               ( void )meta_clear_one_candidate( op, mc, i );
                                        }
-                                       candidates[ i ].sr_tag = META_NOT_CANDIDATE;
+                                       /* leave the target candidate, but record the error for later use */
                                        err = lerr;
 
-                                       Debug( LDAP_DEBUG_ANY, "%s: meta_back_init_one_conn(%d) failed: %d\n",
-                                               op->o_log_prefix, i, lerr );
+                                       if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
+                                               Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
+                                                       op->o_log_prefix, i, lerr );
+
+                                       } else {
+                                               Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
+                                                       op->o_log_prefix, i, lerr );
+                                       }
+
+                                       if ( META_BACK_ONERR_STOP( mi ) ) {
+                                               if ( sendok & LDAP_BACK_SENDERR ) {
+                                                       send_ldap_result( op, rs );
+                                               }
+                                               if ( new_conn ) {
+                                                       mc->mc_refcnt = 0;
+                                                       meta_back_conn_free( mc );
+
+                                               } else {
+                                                       meta_back_release_conn( mi, mc );
+                                               }
+
+                                               return NULL;
+                                       }
 
                                        continue;
                                }
 
                        } else {
                                if ( new_conn ) {
-                                       ( void )meta_clear_one_candidate( &mc->mc_conns[ i ] );
+                                       ( void )meta_clear_one_candidate( op, mc, i );
                                }
-                               candidates[ i ].sr_tag = META_NOT_CANDIDATE;
                        }
                }
 
                if ( ncandidates == 0 ) {
                        if ( new_conn ) {
+                               mc->mc_refcnt = 0;
                                meta_back_conn_free( mc );
+
+                       } else {
+                               meta_back_release_conn( mi, mc );
                        }
 
-                       rs->sr_err = LDAP_NO_SUCH_OBJECT;
-                       rs->sr_text = "Unable to select valid candidates";
+                       if ( rs->sr_err == LDAP_SUCCESS ) {
+                               rs->sr_err = LDAP_NO_SUCH_OBJECT;
+                               rs->sr_text = "Unable to select valid candidates";
+                       }
 
                        if ( sendok & LDAP_BACK_SENDERR ) {
+                               if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
+                                       rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
+                               }
                                send_ldap_result( op, rs );
-                               rs->sr_text = NULL;
+                               rs->sr_matched = NULL;
                        }
 
                        return NULL;
@@ -869,50 +1589,243 @@ done:;
        rs->sr_err = LDAP_SUCCESS;
        rs->sr_text = NULL;
 
+       /* touch the timestamp */
+       if ( mi->mi_idle_timeout != 0 ) {
+               mc->mc_time = op->o_time;
+       }
+
        if ( new_conn ) {
-               
+               if ( mi->mi_conn_ttl ) {
+                       mc->mc_create_time = op->o_time;
+               }
+
                /*
                 * Inserts the newly created metaconn in the avl tree
                 */
-               ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
-               err = avl_insert( &mi->mi_conntree, ( caddr_t )mc,
-                               meta_back_conn_cmp, meta_back_conn_dup );
+               ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
+#if META_BACK_PRINT_CONNTREE > 0
+               meta_back_print_conntree( mi, ">>> meta_back_getconn" );
+#endif /* META_BACK_PRINT_CONNTREE */
+
+               err = 0;
+               if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
+                       if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) {
+                               LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
+                               mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++;
+                               LDAP_BACK_CONN_CACHED_SET( mc );
 
-#if PRINT_CONNTREE > 0
-               myprint( mi->mi_conntree );
-#endif /* PRINT_CONNTREE */
-               
-               ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
+                       } else {
+                               LDAP_BACK_CONN_TAINTED_SET( mc );
+                       }
+                       rs->sr_err = 0;
 
-               /*
-                * Err could be -1 in case a duplicate metaconn is inserted
-                */
-               if ( err == 0 ) {
-                       Debug( LDAP_DEBUG_TRACE,
-                               "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
-                               op->o_log_prefix, ncandidates, mc->mc_conn->c_connid );
+               } else if ( !( sendok & LDAP_BACK_BINDING ) ) {
+                       err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
+                               meta_back_conndn_cmp, meta_back_conndn_dup );
+                       LDAP_BACK_CONN_CACHED_SET( mc );
+               }
 
-               } else {
-                       Debug( LDAP_DEBUG_ANY,
-                               "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
-                               op->o_log_prefix, ncandidates, mc->mc_conn->c_connid );
-               
-                       rs->sr_err = LDAP_OTHER;
-                       rs->sr_text = "Internal server error";
-                       meta_back_conn_free( mc );
-                       if ( sendok & LDAP_BACK_SENDERR ) {
-                               send_ldap_result( op, rs );
-                               rs->sr_text = NULL;
+#if META_BACK_PRINT_CONNTREE > 0
+               meta_back_print_conntree( mi, "<<< meta_back_getconn" );
+#endif /* META_BACK_PRINT_CONNTREE */
+               ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+
+               if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
+                       /*
+                        * Err could be -1 in case a duplicate metaconn is inserted
+                        */
+                       switch ( err ) {
+                       case 0:
+                               break;
+
+                       case -1:
+                               LDAP_BACK_CONN_CACHED_CLEAR( mc );
+                               /* duplicate: free and try to get the newly created one */
+                               if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
+                                       mc->mc_refcnt = 0;      
+                                       meta_back_conn_free( mc );
+       
+                                       new_conn = 0;
+                                       goto retry_lock;
+                               }
+
+                               LDAP_BACK_CONN_TAINTED_SET( mc );
+                               break;
+
+                       default:
+                               LDAP_BACK_CONN_CACHED_CLEAR( mc );
+                               Debug( LDAP_DEBUG_ANY,
+                                       "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
+                                       op->o_log_prefix, ncandidates,
+                                       LDAP_BACK_PCONN_ID( mc ) );
+       
+                               mc->mc_refcnt = 0;      
+                               meta_back_conn_free( mc );
+
+                               rs->sr_err = LDAP_OTHER;
+                               rs->sr_text = "Proxy bind collision";
+                               if ( sendok & LDAP_BACK_SENDERR ) {
+                                       send_ldap_result( op, rs );
+                               }
+                               return NULL;
                        }
-                       return NULL;
                }
 
+               Debug( LDAP_DEBUG_TRACE,
+                       "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
+                       op->o_log_prefix, ncandidates,
+                       LDAP_BACK_PCONN_ID( mc ) );
+
        } else {
                Debug( LDAP_DEBUG_TRACE,
                        "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
-                       op->o_log_prefix, ncandidates, mc->mc_conn->c_connid );
+                       op->o_log_prefix, ncandidates,
+                       LDAP_BACK_PCONN_ID( mc ) );
        }
-       
+
        return mc;
 }
 
+void
+meta_back_release_conn_lock(
+               metainfo_t              *mi,
+       metaconn_t              *mc,
+       int                     dolock )
+{
+       assert( mc != NULL );
+
+       if ( dolock ) {
+               ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
+       }
+       assert( mc->mc_refcnt > 0 );
+       mc->mc_refcnt--;
+       /* NOTE: the connection is removed if either it is tainted
+        * or if it is shared and no one else is using it.  This needs
+        * to occur because for intrinsic reasons cached connections
+        * that are not privileged would live forever and pollute
+        * the connection space (and eat up resources).  Maybe this
+        * should be configurable... */
+       if ( LDAP_BACK_CONN_TAINTED( mc ) || !LDAP_BACK_CONN_CACHED( mc ) ) {
+#if META_BACK_PRINT_CONNTREE > 0
+               meta_back_print_conntree( mi, ">>> meta_back_release_conn" );
+#endif /* META_BACK_PRINT_CONNTREE */
+
+               if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
+                       if ( mc->mc_q.tqe_prev != NULL ) {
+                               assert( LDAP_BACK_CONN_CACHED( mc ) );
+                               assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
+                               LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
+                               mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
+                               LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
+
+                       } else {
+                               assert( !LDAP_BACK_CONN_CACHED( mc ) );
+                       }
+
+               } else if ( LDAP_BACK_CONN_CACHED( mc ) ) {
+                       metaconn_t      *tmpmc;
+
+                       tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
+                               ( caddr_t )mc, meta_back_conndnmc_cmp );
+
+                       /* Overparanoid, but useful... */
+                       assert( tmpmc == NULL || tmpmc == mc );
+               }
+
+               LDAP_BACK_CONN_CACHED_CLEAR( mc );
+
+#if META_BACK_PRINT_CONNTREE > 0
+               meta_back_print_conntree( mi, "<<< meta_back_release_conn" );
+#endif /* META_BACK_PRINT_CONNTREE */
+
+               if ( mc->mc_refcnt == 0 ) {
+                       meta_back_conn_free( mc );
+                       mc = NULL;
+               }
+       }
+
+       if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
+               LDAP_BACK_CONN_BINDING_CLEAR( mc );
+       }
+
+       if ( dolock ) {
+               ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+       }
+}
+
+void
+meta_back_quarantine(
+       Operation       *op,
+       SlapReply       *rs,
+       int             candidate )
+{
+       metainfo_t              *mi = (metainfo_t *)op->o_bd->be_private;
+       metatarget_t            *mt = mi->mi_targets[ candidate ];
+
+       slap_retry_info_t       *ri = &mt->mt_quarantine;
+
+       ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
+
+       if ( rs->sr_err == LDAP_UNAVAILABLE ) {
+               time_t  new_last = slap_get_time();
+
+               switch ( mt->mt_isquarantined ) {
+               case LDAP_BACK_FQ_NO:
+                       if ( ri->ri_last == new_last ) {
+                               goto done;
+                       }
+
+                       Debug( LDAP_DEBUG_ANY,
+                               "%s meta_back_quarantine[%d]: enter.\n",
+                               op->o_log_prefix, candidate, 0 );
+
+                       ri->ri_idx = 0;
+                       ri->ri_count = 0;
+                       break;
+
+               case LDAP_BACK_FQ_RETRYING:
+                       if ( LogTest( LDAP_DEBUG_ANY ) ) {
+                               char    buf[ SLAP_TEXT_BUFLEN ];
+
+                               snprintf( buf, sizeof( buf ),
+                                       "meta_back_quarantine[%d]: block #%d try #%d failed",
+                                       candidate, ri->ri_idx, ri->ri_count );
+                               Debug( LDAP_DEBUG_ANY, "%s %s.\n",
+                                       op->o_log_prefix, buf, 0 );
+                       }
+
+                       ++ri->ri_count;
+                       if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
+                               && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
+                       {
+                               ri->ri_count = 0;
+                               ++ri->ri_idx;
+                       }
+                       break;
+
+               default:
+                       break;
+               }
+
+               mt->mt_isquarantined = LDAP_BACK_FQ_YES;
+               ri->ri_last = new_last;
+
+       } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
+               Debug( LDAP_DEBUG_ANY,
+                       "%s meta_back_quarantine[%d]: exit.\n",
+                       op->o_log_prefix, candidate, 0 );
+
+               if ( mi->mi_quarantine_f ) {
+                       (void)mi->mi_quarantine_f( mi, candidate,
+                               mi->mi_quarantine_p );
+               }
+
+               ri->ri_count = 0;
+               ri->ri_idx = 0;
+               mt->mt_isquarantined = LDAP_BACK_FQ_NO;
+       }
+
+done:;
+       ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
+}
+