+ ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
+ msc->msc_bound = META_BOUND;
+ mc->mc_auth_target = candidate;
+
+ if ( LDAP_BACK_SAVECRED( mi ) ) {
+ ber_bvreplace( &msc->msc_cred, &op->orb_cred );
+ ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind, msc );
+ }
+
+ if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
+ && op->o_req_ndn.bv_len != 0 )
+ {
+ ( void )meta_dncache_update_entry( &mi->mi_cache,
+ &op->o_req_ndn, candidate );
+ }
+
+return_results:;
+ if ( mdn.bv_val != op->o_req_dn.bv_val ) {
+ free( mdn.bv_val );
+ }
+
+ return rs->sr_err;
+}
+
+/*
+ * meta_back_single_dobind
+ */
+int
+meta_back_single_dobind(
+ Operation *op,
+ SlapReply *rs,
+ metaconn_t *mc,
+ int candidate,
+ ldap_back_send_t sendok,
+ int nretries )
+{
+ 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 rc;
+ struct berval cred = BER_BVC( "" );
+ int msgid,
+ rebinding = 0,
+ save_nretries = nretries;
+
+ /*
+ * Otherwise an anonymous bind is performed
+ * (note: if the target was already bound, the anonymous
+ * bind clears the previous bind).
+ */
+ if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
+ ber_memfree( msc->msc_bound_ndn.bv_val );
+ BER_BVZERO( &msc->msc_bound_ndn );
+ }
+
+ if ( !BER_BVISNULL( &msc->msc_cred ) ) {
+ /* destroy sensitive data */
+ memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
+ ber_memfree( msc->msc_cred.bv_val );
+ BER_BVZERO( &msc->msc_cred );
+ }
+
+ /* FIXME: should we check if at least some of the op->o_ctrls
+ * can/should be passed? */
+rebind:;
+ rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
+ NULL, NULL, &msgid );
+ if ( rc == LDAP_SUCCESS ) {
+ LDAPMessage *res;
+ struct timeval tv;
+
+ /*
+ * handle response!!!
+ */
+retry:;
+ tv.tv_sec = 0;
+ tv.tv_usec = META_BIND_TIMEOUT;
+ switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
+ case 0:
+ Debug( LDAP_DEBUG_ANY, "%s meta_back_single_dobind: ldap_result=%d nretries=%d\n",
+ op->o_log_prefix, 0, nretries );
+
+ if ( nretries != META_RETRY_NEVER ) {
+ ldap_pvt_thread_yield();
+ if ( nretries > 0 ) {
+ nretries--;
+ }
+ goto retry;
+ }
+
+ rc = LDAP_BUSY;
+ if ( rebinding ) {
+ ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
+ break;
+ }
+
+ /* FIXME: some times the request times out
+ * while the other party is not willing to
+ * send a response any more. Give it a second
+ * chance with a freshly bound connection */
+ rebinding = 1;
+ nretries = save_nretries;
+ /* fallthru */
+
+ case -1:
+ ldap_get_option( msc->msc_ld,
+ LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
+
+ if ( rebinding ) {
+ ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
+ }
+
+ Debug( LDAP_DEBUG_ANY, "### %s meta_back_single_dobind: err=%d nretries=%d\n",
+ op->o_log_prefix, rs->sr_err, nretries );
+
+ rc = slap_map_api2result( rs );
+ if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
+ ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
+ msc->msc_ld = NULL;
+ msc->msc_bound = 0;
+
+ /* mc here must be the regular mc, reset and ready for init */
+ rc = meta_back_init_one_conn( op, rs, mt, msc, LDAP_BACK_DONTSEND );
+
+ if ( rc == LDAP_SUCCESS ) {
+ ldap_pvt_thread_yield();
+ if ( nretries > 0 ) {
+ nretries--;
+ }
+ goto rebind;
+ }
+ }
+ break;
+
+ default:
+ rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
+ NULL, NULL, NULL, NULL, 1 );
+ if ( rc == LDAP_SUCCESS ) {
+ rc = slap_map_api2result( rs );
+ }
+ break;