- ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
- if ( !lc->lc_bound ) {
- /*
- * 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 ideintity and explicitly
- * add the proxyAuthz control to every operation with the
- * dn bound to the connection as control value.
- */
- if ( op->o_conn != NULL && BER_BVISNULL( &lc->lc_bound_ndn ) ) {
- (void)ldap_back_proxy_authz_bind( lc, op, rs );
- goto done;
+retry_lock:;
+ if ( dolock ) {
+ ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
+ }
+
+ if ( binding == 0 ) {
+ /* check if already bound */
+ rc = LDAP_BACK_CONN_ISBOUND( lc );
+ if ( rc ) {
+ lc->lc_binding--;
+ if ( dolock ) {
+ ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
+ }
+ return rc;
+ }
+
+ if ( LDAP_BACK_CONN_BINDING( lc ) ) {
+ /* if someone else is about to bind it, give up and retry */
+ if ( dolock ) {
+ ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
+ }
+ ldap_pvt_thread_yield();
+ goto retry_lock;
+
+ } else {
+ /* otherwise this thread will bind it */
+ LDAP_BACK_CONN_BINDING_SET( lc );
+ binding = 1;
+ }
+ }
+
+ /* wait for pending operations to finish */
+ /* FIXME: may become a bottleneck! */
+ if ( lc->lc_refcnt != lc->lc_binding ) {
+ if ( dolock ) {
+ ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
+ }
+ ldap_pvt_thread_yield();
+ goto retry_lock;
+ }
+
+ if ( dolock ) {
+ ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
+ }
+
+#if 0
+ while ( lc->lc_refcnt > 1 ) {
+ ldap_pvt_thread_yield();
+ rc = LDAP_BACK_CONN_ISBOUND( lc );
+ if ( rc ) {
+ return rc;
+ }
+ }
+
+ if ( dolock ) {
+ ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
+ }
+ LDAP_BACK_CONN_BINDING_SET( lc );
+ if ( dolock ) {
+ ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
+ }
+#endif
+
+ /*
+ * 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->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
+ {
+ (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
+ goto done;
+ }
+
+#ifdef HAVE_CYRUS_SASL
+ if ( LDAP_BACK_CONN_ISPRIV( lc )
+ && li->li_acl_authmethod == LDAP_AUTH_SASL )
+ {
+ void *defaults = NULL;
+
+ if ( li->li_acl_secprops != NULL ) {
+ rc = ldap_set_option( lc->lc_ld,
+ LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
+
+ if ( rc != LDAP_OPT_SUCCESS ) {
+ Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
+ "(%s,SECPROPS,\"%s\") failed!\n",
+ li->li_uri, li->li_acl_secprops, 0 );
+ goto done;
+ }
+ }
+
+ defaults = lutil_sasl_defaults( lc->lc_ld,
+ li->li_acl_sasl_mech.bv_val,
+ li->li_acl_sasl_realm.bv_val,
+ li->li_acl_authcID.bv_val,
+ li->li_acl_passwd.bv_val,
+ NULL );
+
+ rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
+ li->li_acl_authcDN.bv_val,
+ li->li_acl_sasl_mech.bv_val, NULL, NULL,
+ LDAP_SASL_QUIET, lutil_sasl_interact,
+ defaults );
+
+ lutil_sasl_freedefs( defaults );
+
+ 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 {
+ LDAP_BACK_CONN_ISBOUND_SET( lc );