]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldap/bind.c
cannot happen...
[openldap] / servers / slapd / back-ldap / bind.c
index abd8912f7c341232b46ebc4a75c351c9d911c6a6..5c0ed2f69eba98f32089dfee653fa125ec5219a1 100644 (file)
@@ -37,7 +37,9 @@
 
 #define PRINT_CONNTREE 0
 
-static LDAP_REBIND_PROC        ldap_back_rebind;
+static LDAP_REBIND_PROC        ldap_back_default_rebind;
+
+LDAP_REBIND_PROC       *ldap_back_rebind_f = ldap_back_default_rebind;
 
 static int
 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs );
@@ -63,13 +65,13 @@ ldap_back_bind( Operation *op, SlapReply *rs )
                ch_free( lc->lc_bound_ndn.bv_val );
                BER_BVZERO( &lc->lc_bound_ndn );
        }
-       lc->lc_bound = 0;
+       LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
 
        /* method is always LDAP_AUTH_SIMPLE if we got here */
        rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
                        LDAP_SASL_SIMPLE,
                        &op->orb_cred, op->o_ctrls, NULL, &msgid );
-       rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
+       rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
 
        if ( rc == LDAP_SUCCESS ) {
                /* If defined, proxyAuthz will be used also when
@@ -79,13 +81,13 @@ ldap_back_bind( Operation *op, SlapReply *rs )
                /* NOTE: use with care */
                if ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
                        ldap_back_proxy_authz_bind( lc, op, rs );
-                       if ( lc->lc_bound == 0 ) {
+                       if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
                                rc = 1;
                                goto done;
                        }
                }
 
-               lc->lc_bound = 1;
+               LDAP_BACK_CONN_ISBOUND_SET( lc );
                ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
 
                if ( LDAP_BACK_SAVECRED( li ) ) {
@@ -94,30 +96,24 @@ ldap_back_bind( Operation *op, SlapReply *rs )
                                                lc->lc_cred.bv_len );
                        }
                        ber_bvreplace( &lc->lc_cred, &op->orb_cred );
-                       ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind, lc );
+                       ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind_f, lc );
                }
        }
 done:;
 
        /* must re-insert if local DN changed as result of bind */
-       if ( lc->lc_bound && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) {
+       if ( LDAP_BACK_CONN_ISBOUND( lc )
+               && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) )
+       {
                int             lerr;
 
                /* wait for all other ops to release the connection */
 retry_lock:;
-               switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
-               case LDAP_PVT_THREAD_EBUSY:
-               default:
+               ldap_pvt_thread_mutex_lock( &li->conn_mutex );
+               if ( lc->lc_refcnt > 1 ) {
+                       ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
                        ldap_pvt_thread_yield();
                        goto retry_lock;
-
-               case 0:
-                       if ( lc->lc_refcnt > 1 ) {
-                               ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
-                               ldap_pvt_thread_yield();
-                               goto retry_lock;
-                       }
-                       break;
                }
 
                assert( lc->lc_refcnt == 1 );
@@ -235,16 +231,7 @@ ldap_back_freeconn( Operation *op, struct ldapconn *lc )
 {
        struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
 
-retry_lock:;
-       switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
-       case LDAP_PVT_THREAD_EBUSY:
-       default:
-               ldap_pvt_thread_yield();
-               goto retry_lock;
-
-       case 0:
-               break;
-       }
+       ldap_pvt_thread_mutex_lock( &li->conn_mutex );
 
        assert( lc->lc_refcnt > 0 );
        if ( --lc->lc_refcnt == 0 ) {
@@ -260,34 +247,26 @@ retry_lock:;
        return 0;
 }
 
+#ifdef HAVE_TLS
 static int
-ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
+ldap_back_start_tls(
+       LDAP            *ld,
+       int             protocol,
+       int             *is_tls,
+       const char      *url,
+       unsigned        flags,
+       int             retries,
+       const char      **text )
 {
-       struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
-       int             vers = op->o_protocol;
-       LDAP            *ld = NULL;
-
-       assert( lcp != NULL );
-
-       rs->sr_err = ldap_initialize( &ld, li->url );
-       if ( rs->sr_err != LDAP_SUCCESS ) {
-               goto error_return;
-       }
+       int             rc = LDAP_SUCCESS;
+       struct ldapinfo dummy;
 
-       /* Set LDAP version. This will always succeed: If the client
-        * bound with a particular version, then so can we.
-        */
-       ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
+       /* this is ridiculous... */
+       dummy.flags = flags;
 
-       /* automatically chase referrals ("[dont-]chase-referrals" statement) */
-       if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
-               ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
-       }
-
-#ifdef HAVE_TLS
        /* start TLS ("tls-[try-]{start,propagate}" statements) */
-       if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
-                               && !ldap_is_ldaps_url( li->url ) )
+       if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
+                               && !ldap_is_ldaps_url( url ) )
        {
 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
                /*
@@ -296,45 +275,60 @@ ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, lda
                 */
                int             msgid;
 
-               rs->sr_err = ldap_start_tls( ld, NULL, NULL, &msgid );
-               if ( rs->sr_err == LDAP_SUCCESS ) {
+               if ( protocol == 0 ) {
+                       ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
+                                       (void *)&protocol );
+               }
+
+               if ( protocol < LDAP_VERSION3 ) {
+                       protocol = LDAP_VERSION3;
+                       /* Set LDAP version */
+                       ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
+                                       (const void *)&protocol );
+               }
+
+               rc = ldap_start_tls( ld, NULL, NULL, &msgid );
+               if ( rc == LDAP_SUCCESS ) {
                        LDAPMessage     *res = NULL;
-                       int             rc, retries = 1;
-                       struct timeval  tv = { 0, 0 };
+                       struct timeval  tv;
+
+                       LDAP_BACK_TV_SET( &tv );
 
 retry:;
                        rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
                        if ( rc < 0 ) {
-                               rs->sr_err = LDAP_OTHER;
+                               rc = LDAP_OTHER;
 
                        } else if ( rc == 0 ) {
-                               if ( retries ) {
-                                       retries--;
-                                       tv.tv_sec = 0;
-                                       tv.tv_usec = 100000;
+                               if ( retries != LDAP_BACK_RETRY_NEVER ) {
+                                       ldap_pvt_thread_yield();
+                                       if ( retries > 0 ) {
+                                               retries--;
+                                       }
+                                       LDAP_BACK_TV_SET( &tv );
                                        goto retry;
                                }
-                               rs->sr_err = LDAP_OTHER;
+                               rc = LDAP_OTHER;
 
                        } else if ( rc == LDAP_RES_EXTENDED ) {
                                struct berval   *data = NULL;
 
-                               rs->sr_err = ldap_parse_extended_result( ld, res,
+                               rc = ldap_parse_extended_result( ld, res,
                                                NULL, &data, 0 );
-                               if ( rs->sr_err == LDAP_SUCCESS ) {
-                                       rs->sr_err = ldap_result2error( ld, res, 1 );
+                               if ( rc == LDAP_SUCCESS ) {
+                                       rc = ldap_result2error( ld, res, 1 );
                                        res = NULL;
                                        
                                        /* FIXME: in case a referral 
                                         * is returned, should we try
                                         * using it instead of the 
                                         * configured URI? */
-                                       if ( rs->sr_err == LDAP_SUCCESS ) {
-                                               ldap_install_tls( ld );
+                                       if ( rc == LDAP_SUCCESS ) {
+                                               rc = ldap_install_tls( ld );
 
-                                       } else if ( rs->sr_err == LDAP_REFERRAL ) {
-                                               rs->sr_err = LDAP_OTHER;
-                                               rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
+                                       } else if ( rc == LDAP_REFERRAL ) {
+                                               rc = LDAP_OTHER;
+                                               *text = "unwilling to chase referral returned by Start TLS exop";
                                        }
 
                                        if ( data ) {
@@ -346,7 +340,7 @@ retry:;
                                }
 
                        } else {
-                               rs->sr_err = LDAP_OTHER;
+                               rc = LDAP_OTHER;
                        }
 
                        if ( res != NULL ) {
@@ -357,30 +351,90 @@ retry:;
                /*
                 * use synchronous StartTLS
                 */
-               rs->sr_err = ldap_start_tls_s( ld, NULL, NULL );
+               rc = ldap_start_tls_s( ld, NULL, NULL );
 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
 
                /* if StartTLS is requested, only attempt it if the URL
                 * is not "ldaps://"; this may occur not only in case
                 * 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( li ) ) )
-               {
-                       ldap_unbind_ext_s( ld, NULL, NULL );
-                       goto error_return;
+               switch ( rc ) {
+               case LDAP_SUCCESS:
+                       *is_tls = 1;
+                       break;
+
+               case LDAP_SERVER_DOWN:
+                       break;
+
+               default:
+                       if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
+                               *text = "could not start TLS";
+                               break;
+                       }
+
+                       /* in case Start TLS is not critical */
+                       *is_tls = 0;
+                       rc = LDAP_SUCCESS;
+                       break;
                }
 
-               /* in case Start TLS is not critical */
-               rs->sr_err = LDAP_SUCCESS;
+       } else {
+               *is_tls = 0;
+       }
+
+       return rc;
+}
+#endif /* HAVE_TLS */
+
+static int
+ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
+{
+       struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
+       int             vers = op->o_protocol;
+       LDAP            *ld = NULL;
+#ifdef HAVE_TLS
+       int             is_tls = op->o_conn->c_is_tls;
+#endif /* HAVE_TLS */
+
+       assert( lcp != NULL );
+
+       rs->sr_err = ldap_initialize( &ld, li->url );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
+               goto error_return;
+       }
+
+       /* Set LDAP version. This will always succeed: If the client
+        * bound with a particular version, then so can we.
+        */
+       ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
+
+       /* automatically chase referrals ("[dont-]chase-referrals" statement) */
+       if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
+               ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
+       }
+
+#ifdef HAVE_TLS
+       rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
+                       li->url, li->flags, li->nretries, &rs->sr_text );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
+               ldap_unbind_ext( ld, NULL, NULL );
+               goto error_return;
        }
 #endif /* HAVE_TLS */
 
        if ( *lcp == NULL ) {
                *lcp = (struct ldapconn *)ch_calloc( 1, sizeof( struct ldapconn ) );
+               (*lcp)->lc_flags= li->flags;
        }
        (*lcp)->lc_ld = ld;
        (*lcp)->lc_refcnt = 1;
+#ifdef HAVE_TLS
+       if ( is_tls ) {
+               LDAP_BACK_CONN_ISTLS_SET( *lcp );
+       } else {
+               LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
+       }
+#endif /* HAVE_TLS */
 
 error_return:;
        if ( rs->sr_err != LDAP_SUCCESS ) {
@@ -405,41 +459,25 @@ ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
                        lc_curr = { 0 };
        int             refcnt = 1;
 
-       /* Searches for a ldapconn in the avl tree */
-
-       /* Explicit binds must not be shared */
-       if ( op->o_tag == LDAP_REQ_BIND
-               || ( op->o_conn
-                       && op->o_conn->c_authz_backend
-                       && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
-       {
-               lc_curr.lc_conn = op->o_conn;
-
-       } else {
-               lc_curr.lc_conn = NULL;
-       }
-       
        /* Internal searches are privileged and shared. So is root. */
-       /* FIXME: there seem to be concurrency issues */
        if ( op->o_do_not_cache || be_isroot( op ) ) {
+               LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
                lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
-               lc_curr.lc_conn = NULL;
-               lc_curr.lc_ispriv = 1;
+               lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
 
        } else {
                lc_curr.lc_local_ndn = op->o_ndn;
+               /* Explicit binds must not be shared */
+               if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
+                       lc_curr.lc_conn = op->o_conn;
+       
+               } else {
+                       lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
+               }
        }
 
-retry_lock:;
-       switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
-       case LDAP_PVT_THREAD_EBUSY:
-       default:
-               ldap_pvt_thread_yield();
-               goto retry_lock;
-
-       case 0:
-               break;
-       }
+       /* Searches for a ldapconn in the avl tree */
+       ldap_pvt_thread_mutex_lock( &li->conn_mutex );
 
        lc = (struct ldapconn *)avl_find( li->conntree, 
                        (caddr_t)&lc_curr, ldap_back_conn_cmp );
@@ -457,36 +495,56 @@ retry_lock:;
                lc->lc_conn = lc_curr.lc_conn;
                ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
 
-               ldap_pvt_thread_mutex_init( &lc->lc_mutex );
-
-               if ( lc_curr.lc_ispriv ) {
+               if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
                        ber_dupbv( &lc->lc_cred, &li->acl_passwd );
                        ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
-                       lc->lc_ispriv = lc_curr.lc_ispriv;
+                       LDAP_BACK_CONN_ISPRIV_SET( lc );
 
                } else {
                        BER_BVZERO( &lc->lc_cred );
                        BER_BVZERO( &lc->lc_bound_ndn );
-                       if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
-                               && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private )
+#if 0
+                       /* FIXME: if we set lc_bound_ndn = o_ndn
+                        * we end up with a bind with DN but no password! */
+                       if ( !BER_BVISEMPTY( &op->o_ndn )
+                               && SLAP_IS_AUTHZ_BACKEND( op ) )
                        {
                                ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
                        }
+#endif
                }
 
-               lc->lc_bound = 0;
-
-               /* Inserts the newly created ldapconn in the avl tree */
-retry_lock2:;
-               switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
-               case LDAP_PVT_THREAD_EBUSY:
-               default:
-                       ldap_pvt_thread_yield();
-                       goto retry_lock2;
+#ifdef HAVE_TLS
+               /* if start TLS failed but it was not mandatory,
+                * check if the non-TLS connection was already
+                * in cache; in case, destroy the newly created
+                * connection and use the existing one */
+               if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
+                               && !ldap_tls_inplace( lc->lc_ld ) )
+               {
+                       struct ldapconn *tmplc;
+                       
+                       lc_curr.lc_conn = LDAP_BACK_PCONN;
+                       ldap_pvt_thread_mutex_lock( &li->conn_mutex );
+                       tmplc = (struct ldapconn *)avl_find( li->conntree, 
+                                       (caddr_t)&lc_curr, ldap_back_conn_cmp );
+                       if ( tmplc != NULL ) {
+                               refcnt = ++tmplc->lc_refcnt;
+                               ldap_back_conn_free( lc );
+                               lc = tmplc;
+                       }
+                       ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
 
-               case 0:
-                       break;
+                       if ( tmplc != NULL ) {
+                               goto done;
+                       }
                }
+#endif /* HAVE_TLS */
+
+               LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
+
+               /* Inserts the newly created ldapconn in the avl tree */
+               ldap_pvt_thread_mutex_lock( &li->conn_mutex );
 
                assert( lc->lc_refcnt == 1 );
                rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
@@ -518,7 +576,8 @@ retry_lock2:;
                        "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
                        (void *)lc, refcnt, 0 );
        }
-       
+
+done:;
        return lc;
 }
 
@@ -530,17 +589,7 @@ ldap_back_release_conn(
 {
        struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
 
-retry_lock:;
-       switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
-       case LDAP_PVT_THREAD_EBUSY:
-       default:
-               ldap_pvt_thread_yield();
-               goto retry_lock;
-
-       case 0:
-               break;
-       }
-
+       ldap_pvt_thread_mutex_lock( &li->conn_mutex );
        assert( lc->lc_refcnt > 0 );
        lc->lc_refcnt--;
        ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
@@ -553,8 +602,7 @@ retry_lock:;
  * it from all the callers, and I made the function return the flag, so
  * it can be used to simplify the check.
  *
- * Note: lc_mutex is locked; dolock indicates whether li->conn_mutex
- * must be locked or not
+ * Note: dolock indicates whether li->conn_mutex must be locked or not
  */
 static int
 ldap_back_dobind_int(
@@ -565,169 +613,181 @@ ldap_back_dobind_int(
        int                     retries,
        int                     dolock )
 {      
-       int             rc;
+       struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
+
+       int             rc = LDAP_BACK_CONN_ISBOUND( lc );
        ber_int_t       msgid;
 
        assert( retries >= 0 );
 
-       if ( !lc->lc_bound ) {
-               struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
+       if ( rc ) {
+               return rc;
+       }
 
-               /*
-                * FIXME: we need to let clients use proxyAuthz
-                * otherwise we cannot do symmetric pools of servers;
-                * we have to live with the fact that a user can
-                * authorize itself as any ID that is allowed
-                * by the authzTo directive of the "proxyauthzdn".
-                */
-               /*
-                * NOTE: current Proxy Authorization specification
-                * and implementation do not allow proxy authorization
-                * control to be provided with Bind requests
-                */
-               /*
-                * if no bind took place yet, but the connection is bound
-                * and the "idassert-authcDN" (or other ID) is set, 
-                * then bind as the asserting identity and explicitly 
-                * add the proxyAuthz control to every operation with the
-                * dn bound to the connection as control value.
-                * This is done also if this is the authrizing backend,
-                * but the "override" flag is given to idassert.
-                * It allows to use SASL bind and yet proxyAuthz users
-                */
-               if ( op->o_conn != NULL &&
-                               !op->o_do_not_cache &&
-                               ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
-                                 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
-               {
-                       (void)ldap_back_proxy_authz_bind( lc, op, rs );
-                       goto done;
-               }
+       /*
+        * FIXME: we need to let clients use proxyAuthz
+        * otherwise we cannot do symmetric pools of servers;
+        * we have to live with the fact that a user can
+        * authorize itself as any ID that is allowed
+        * by the authzTo directive of the "proxyauthzdn".
+        */
+       /*
+        * NOTE: current Proxy Authorization specification
+        * and implementation do not allow proxy authorization
+        * control to be provided with Bind requests
+        */
+       /*
+        * if no bind took place yet, but the connection is bound
+        * and the "idassert-authcDN" (or other ID) is set, 
+        * then bind as the asserting identity and explicitly 
+        * add the proxyAuthz control to every operation with the
+        * dn bound to the connection as control value.
+        * This is done also if this is the authrizing backend,
+        * but the "override" flag is given to idassert.
+        * It allows to use SASL bind and yet proxyAuthz users
+        */
+       if ( op->o_conn != NULL &&
+                       !op->o_do_not_cache &&
+                       ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
+                         ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
+       {
+               (void)ldap_back_proxy_authz_bind( lc, op, rs );
+               goto done;
+       }
 
 #ifdef HAVE_CYRUS_SASL
-               if ( lc->lc_ispriv && li->acl_authmethod == LDAP_AUTH_SASL ) {
-                       void            *defaults = NULL;
-
-#if 1  /* will deal with this later... */
-                       if ( li->acl_secprops != NULL ) {
-                               rc = ldap_set_option( lc->lc_ld,
-                                       LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
-
-                               if( rc != LDAP_OPT_SUCCESS ) {
-                                       Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
-                                               "(%s,SECPROPS,\"%s\") failed!\n",
-                                               li->url, li->acl_secprops, 0 );
-                                       goto done;
-                               }
-                       }
-#endif
+       if ( LDAP_BACK_CONN_ISPRIV( lc )
+               && li->acl_authmethod == LDAP_AUTH_SASL )
+       {
+               void            *defaults = NULL;
 
-                       defaults = lutil_sasl_defaults( lc->lc_ld,
-                                       li->acl_sasl_mech.bv_val,
-                                       li->acl_sasl_realm.bv_val,
-                                       li->acl_authcID.bv_val,
-                                       li->acl_passwd.bv_val,
-                                       NULL );
+               if ( li->acl_secprops != NULL ) {
+                       rc = ldap_set_option( lc->lc_ld,
+                               LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
+
+                       if ( rc != LDAP_OPT_SUCCESS ) {
+                               Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
+                                       "(%s,SECPROPS,\"%s\") failed!\n",
+                                       li->url, li->acl_secprops, 0 );
+                               goto done;
+                       }
+               }
 
-                       rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
-                                       li->acl_authcDN.bv_val,
-                                       li->acl_sasl_mech.bv_val, NULL, NULL,
-                                       LDAP_SASL_QUIET, lutil_sasl_interact,
-                                       defaults );
+               defaults = lutil_sasl_defaults( lc->lc_ld,
+                               li->acl_sasl_mech.bv_val,
+                               li->acl_sasl_realm.bv_val,
+                               li->acl_authcID.bv_val,
+                               li->acl_passwd.bv_val,
+                               NULL );
+
+               rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
+                               li->acl_authcDN.bv_val,
+                               li->acl_sasl_mech.bv_val, NULL, NULL,
+                               LDAP_SASL_QUIET, lutil_sasl_interact,
+                               defaults );
 
-                       lutil_sasl_freedefs( defaults );
+               lutil_sasl_freedefs( defaults );
 
-                       rs->sr_err = slap_map_api2result( rs );
-                       if ( rs->sr_err != LDAP_SUCCESS ) {
-                               lc->lc_bound = 0;
-                               send_ldap_result( op, rs );
+               rs->sr_err = slap_map_api2result( rs );
+               if ( rs->sr_err != LDAP_SUCCESS ) {
+                       LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
+                       send_ldap_result( op, rs );
 
-                       } else {
-                               lc->lc_bound = 1;
-                       }
-                       goto done;
+               } else {
+                       LDAP_BACK_CONN_ISBOUND_SET( lc );
                }
+               goto done;
+       }
 #endif /* HAVE_CYRUS_SASL */
 
 retry:;
-               rs->sr_err = ldap_sasl_bind( lc->lc_ld,
-                               lc->lc_bound_ndn.bv_val,
-                               LDAP_SASL_SIMPLE, &lc->lc_cred,
-                               NULL, NULL, &msgid );
-
-               if ( rs->sr_err == LDAP_SERVER_DOWN ) {
-                       if ( retries > 0 ) {
-                               if ( dolock ) {
-retry_lock:;
-                                       switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
-                                       case LDAP_PVT_THREAD_EBUSY:
-                                       default:
-                                               ldap_pvt_thread_yield();
-                                               goto retry_lock;
-
-                                       case 0:
-                                               break;
-                                       }
-                               }
+       rs->sr_err = ldap_sasl_bind( lc->lc_ld,
+                       lc->lc_bound_ndn.bv_val,
+                       LDAP_SASL_SIMPLE, &lc->lc_cred,
+                       NULL, NULL, &msgid );
+
+       if ( rs->sr_err == LDAP_SERVER_DOWN ) {
+               if ( retries != LDAP_BACK_RETRY_NEVER ) {
+                       if ( dolock ) {
+                               ldap_pvt_thread_mutex_lock( &li->conn_mutex );
+                       }
 
-                               assert( lc->lc_refcnt > 0 );
-                               if ( lc->lc_refcnt == 1 ) {
-                                       ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
-                                       lc->lc_ld = NULL;
+                       assert( lc->lc_refcnt > 0 );
+                       if ( lc->lc_refcnt == 1 ) {
+                               ldap_unbind_ext( lc->lc_ld, NULL, NULL );
+                               lc->lc_ld = NULL;
 
-                                       /* lc here must be the regular lc, reset and ready for init */
-                                       rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
-                               }
-                               if ( dolock ) {
-                                       ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
-                               }
-                               if ( rs->sr_err == LDAP_SUCCESS ) {
+                               /* lc here must be the regular lc, reset and ready for init */
+                               rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
+                       }
+                       if ( dolock ) {
+                               ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
+                       }
+                       if ( rs->sr_err == LDAP_SUCCESS ) {
+                               if ( retries > 0 ) {
                                        retries--;
-                                       goto retry;
                                }
+                               goto retry;
                        }
+               }
 
-                       ldap_back_freeconn( op, lc );
-                       rs->sr_err = slap_map_api2result( rs );
+               ldap_back_freeconn( op, lc );
+               rs->sr_err = slap_map_api2result( rs );
 
-                       return 0;
-               }
+               return 0;
+       }
 
-               rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
-               if ( rc == LDAP_SUCCESS ) {
-                       lc->lc_bound = 1;
-               }
+       rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
+       if ( rc == LDAP_SUCCESS ) {
+               LDAP_BACK_CONN_ISBOUND_SET( lc );
+
+       } else {
+               ldap_back_release_conn( op, rs, lc );
        }
 
 done:;
-       rc = lc->lc_bound;
+       rc = LDAP_BACK_CONN_ISBOUND( lc );
+
        return rc;
 }
 
 int
 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
 {
-       int     rc;
-
-       ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
-       rc = ldap_back_dobind_int( lc, op, rs, sendok, 1, 1 );
-       ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
+       struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
 
-       return rc;
+       return ldap_back_dobind_int( lc, op, rs, sendok, li->nretries, 1 );
 }
 
 /*
- * ldap_back_rebind
+ * ldap_back_default_rebind
  *
  * This is a callback used for chasing referrals using the same
  * credentials as the original user on this session.
  */
 static int 
-ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
+ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
        ber_int_t msgid, void *params )
 {
        struct ldapconn *lc = (struct ldapconn *)params;
 
+#ifdef HAVE_TLS
+       /* ... otherwise we couldn't get here */
+       assert( lc != NULL );
+
+       if ( !ldap_tls_inplace( ld ) ) {
+               int             is_tls = LDAP_BACK_CONN_ISTLS( lc ),
+                               rc;
+               const char      *text = NULL;
+
+               rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
+                       LDAP_BACK_RETRY_DEFAULT, &text );
+               if ( rc != LDAP_SUCCESS ) {
+                       return rc;
+               }
+       }
+#endif /* HAVE_TLS */
+
        /* FIXME: add checks on the URL/identity? */
 
        return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
@@ -740,6 +800,7 @@ ldap_back_op_result(
                Operation               *op,
                SlapReply               *rs,
                ber_int_t               msgid,
+               time_t                  timeout,
                ldap_back_send_t        sendok )
 {
        char            *match = NULL;
@@ -756,14 +817,29 @@ ldap_back_op_result(
         * remote server response */
        if ( ERR_OK( rs->sr_err ) ) {
                int             rc;
-               struct timeval  tv = { 0, 0 };
+               struct timeval  tv;
+
+               if ( timeout ) {
+                       tv.tv_sec = timeout;
+                       tv.tv_usec = 0;
+
+               } else {
+                       LDAP_BACK_TV_SET( &tv );
+               }
 
 retry:;
                /* if result parsing fails, note the failure reason */
-               switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
+               rc = ldap_result( lc->lc_ld, msgid, 1, &tv, &res );
+               switch ( rc ) {
                case 0:
-                       tv.tv_sec = 0;
-                       tv.tv_usec = 100000;    /* 0.1 s */
+                       if ( timeout ) {
+                               (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
+                               rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
+                               rs->sr_text = "Operation timed out";
+                               break;
+                       }
+
+                       LDAP_BACK_TV_SET( &tv );
                        ldap_pvt_thread_yield();
                        goto retry;
 
@@ -819,7 +895,7 @@ retry:;
                ldap_memfree( text );
        }
        rs->sr_text = NULL;
-       return( ERR_OK( rs->sr_err ) ? 0 : -1 );
+       return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
 }
 
 /* return true if bound, false if failed */
@@ -829,21 +905,18 @@ ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_se
        int             rc = 0;
        struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
        
-retry_lock:;
-       switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
-       case LDAP_PVT_THREAD_EBUSY:
-       default:
-               ldap_pvt_thread_yield();
-               goto retry_lock;
-
-       case 0:
-               break;
-       }
+       ldap_pvt_thread_mutex_lock( &li->conn_mutex );
 
        if ( lc->lc_refcnt == 1 ) {
-               ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
+               Debug( LDAP_DEBUG_ANY,
+                       "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
+                       op->o_log_prefix, li->url,
+                       BER_BVISNULL( &lc->lc_bound_ndn ) ?
+                               "" : lc->lc_bound_ndn.bv_val );
+
+               ldap_unbind_ext( lc->lc_ld, NULL, NULL );
                lc->lc_ld = NULL;
-               lc->lc_bound = 0;
+               LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
 
                /* lc here must be the regular lc, reset and ready for init */
                rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
@@ -918,7 +991,7 @@ ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
                        if ( rs->sr_err != LDAP_SUCCESS ) {
                                if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
                                        send_ldap_result( op, rs );
-                                       lc->lc_bound = 0;
+                                       LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
 
                                } else {
                                        rs->sr_err = LDAP_SUCCESS;
@@ -976,18 +1049,17 @@ ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
                        }
                }
 
-#if 0  /* will deal with this later... */
-               if ( sasl_secprops != NULL ) {
-                       rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
-                               (void *) sasl_secprops );
+               if ( li->idassert_secprops != NULL ) {
+                       rs->sr_err = ldap_set_option( lc->lc_ld,
+                               LDAP_OPT_X_SASL_SECPROPS,
+                               (void *)li->idassert_secprops );
 
                        if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
                                send_ldap_result( op, rs );
-                               lc->lc_bound = 0;
+                               LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
                                goto done;
                        }
                }
-#endif
 
                defaults = lutil_sasl_defaults( lc->lc_ld,
                                li->idassert_sasl_mech.bv_val,
@@ -1001,48 +1073,49 @@ ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
                                LDAP_SASL_QUIET, lutil_sasl_interact,
                                defaults );
 
-               lutil_sasl_freedefs( defaults );
-               if ( freeauthz ) {
-                       slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
-               }
-
                rs->sr_err = slap_map_api2result( rs );
                if ( rs->sr_err != LDAP_SUCCESS ) {
-                       lc->lc_bound = 0;
+                       LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
                        send_ldap_result( op, rs );
 
                } else {
-                       lc->lc_bound = 1;
+                       LDAP_BACK_CONN_ISBOUND_SET( lc );
+               }
+
+               lutil_sasl_freedefs( defaults );
+               if ( freeauthz ) {
+                       slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
                }
+
                goto done;
 #endif /* HAVE_CYRUS_SASL */
        }
 
        switch ( li->idassert_authmethod ) {
+       case LDAP_AUTH_NONE:
+               LDAP_BACK_CONN_ISBOUND_SET( lc );
+               goto done;
+
        case LDAP_AUTH_SIMPLE:
                rs->sr_err = ldap_sasl_bind( lc->lc_ld,
                                binddn.bv_val, LDAP_SASL_SIMPLE,
                                &bindcred, NULL, NULL, &msgid );
                break;
 
-       case LDAP_AUTH_NONE:
-               lc->lc_bound = 1;
-               goto done;
-
        default:
                /* unsupported! */
-               lc->lc_bound = 0;
+               LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
                rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
                send_ldap_result( op, rs );
                goto done;
        }
 
-       rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
+       rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
        if ( rc == LDAP_SUCCESS ) {
-               lc->lc_bound = 1;
+               LDAP_BACK_CONN_ISBOUND_SET( lc );
        }
 done:;
-       return lc->lc_bound;
+       return LDAP_BACK_CONN_ISBOUND( lc );
 }
 
 /*
@@ -1134,7 +1207,8 @@ ldap_back_proxy_authz_ctrl(
 
        } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
                if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
-                               /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
+                               /* && ( !BER_BVISNULL( &op->o_conn->c_ndn )
+                                       || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
                {
                        /* already asserted in SASL via native authz */
                        /* NOTE: the test on lc->lc_bound is used to trap
@@ -1187,7 +1261,7 @@ ldap_back_proxy_authz_ctrl(
                rs->sr_text = "proxyAuthz not allowed within namingContext";
        }
 
-       if ( op->o_do_not_cache && op->o_is_auth_check ) {
+       if ( op->o_is_auth_check ) {
                mode = LDAP_BACK_IDASSERT_NOASSERT;
 
        } else {