From aaed635e3a401cfcc4cc97f33788179c458901c3 Mon Sep 17 00:00:00 2001
From: "djm@openbsd.org" <djm@openbsd.org>
Date: Fri, 16 Nov 2018 02:46:20 +0000
Subject: [PATCH 111/294] upstream: fix bug in client that was keeping a
 redundant ssh-agent

socket around for the life of the connection; bz#2912; reported by Simon
Tatham; ok dtucker@

OpenBSD-Commit-ID: 4ded588301183d343dce3e8c5fc1398e35058478
---
 sshconnect2.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/sshconnect2.c b/sshconnect2.c
index 1e894e8..ff450e5 100644
--- a/sshconnect2.c
+++ b/sshconnect2.c
@@ -583,7 +583,6 @@ ssh_userauth2(const char *local_user, const char *server_user, char *host,
 
 	/* setup authentication context */
 	memset(&authctxt, 0, sizeof(authctxt));
-	pubkey_prepare(&authctxt);
 	authctxt.server_user = server_user;
 	authctxt.local_user = local_user;
 	authctxt.host = host;
@@ -596,6 +595,7 @@ ssh_userauth2(const char *local_user, const char *server_user, char *host,
 	authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
 	authctxt.info_req_seen = 0;
 	authctxt.agent_fd = -1;
+        pubkey_prepare(&authctxt);
 	if (authctxt.method == NULL)
 		fatal("ssh_userauth2: internal error: cannot send userauth none request");
 
@@ -1849,8 +1849,10 @@ pubkey_cleanup(Authctxt *authctxt)
 {
 	Identity *id;
 
-	if (authctxt->agent_fd != -1)
+	if (authctxt->agent_fd != -1) {
 		ssh_close_authentication_socket(authctxt->agent_fd);
+		authctxt->agent_fd = -1;
+	}
 	for (id = TAILQ_FIRST(&authctxt->keys); id;
 	    id = TAILQ_FIRST(&authctxt->keys)) {
 		TAILQ_REMOVE(&authctxt->keys, id, next);
-- 
1.8.3.1

