From fe136bc35276eeb8566264ccc0e6787343cb96cf Mon Sep 17 00:00:00 2001 From: quamrulmina Date: Sat, 16 Jan 2016 00:05:33 -0600 Subject: [PATCH] Code fixed for ssh-agent.exe and ssh-add.exe to work in Windows ssh-agent.exe and ssh-add.exe code updated and fixed to work in Windows. For convenience of users, ssh-agent.exe starts a cmd shell with the "SSH_AUTH_SOCK" and "SSH_AGENT_PID" environment variables set. ssh-add.exe can be run immediately from the cmd shell. 'ssh-add -L" and "ssh-add id_rsa" and "ssh-add -d id_rsa" are 3 useful commands to list, add and delete keys from ssh-agent cache. --- authfd.c | 8 ++++++++ contrib/win32/win32compat/socket.c | 7 ++++--- ssh-add.c | 5 +++++ ssh-agent.c | 7 +++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/authfd.c b/authfd.c index bc52248..0d54e0a 100644 --- a/authfd.c +++ b/authfd.c @@ -114,6 +114,14 @@ ssh_get_authentication_socket(int *fdp) errno = oerrno; return SSH_ERR_SYSTEM_ERROR; } +#else + if ( + connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) { + oerrno = errno; + close(sock); + errno = oerrno; + return SSH_ERR_SYSTEM_ERROR; + } #endif /* #ifndef WIN32_FIXME */ if (fdp != NULL) diff --git a/contrib/win32/win32compat/socket.c b/contrib/win32/win32compat/socket.c index 3f7bf5e..3138763 100644 --- a/contrib/win32/win32compat/socket.c +++ b/contrib/win32/win32compat/socket.c @@ -2013,9 +2013,10 @@ int cleanSelectThread(int thread_no) if(thread_data_set[thread_no] != NULL) { - HeapFree(GetProcessHeap(), 0, thread_data_set[thread_no]); - - thread_data_set[thread_no] = NULL; + thread_data_p ap = thread_data_set[thread_no]; + thread_data_set[thread_no] = NULL; + HeapFree(GetProcessHeap(), 0, ap); + } DBG_MSG("<- cleanSelectThread()...\n"); diff --git a/ssh-add.c b/ssh-add.c index b650084..722746f 100644 --- a/ssh-add.c +++ b/ssh-add.c @@ -520,6 +520,11 @@ main(int argc, char **argv) fprintf(stderr, "Could not open a connection to your " "authentication agent.\n"); exit(2); + #ifdef WIN32_FIXME + case SSH_ERR_SYSTEM_ERROR: + fprintf(stderr, "Error connecting to agent: ssh-agent.exe may not be running\n"); + exit(2); + #endif default: fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r)); exit(2); diff --git a/ssh-agent.c b/ssh-agent.c index 2e4079f..3b3c9b9 100644 --- a/ssh-agent.c +++ b/ssh-agent.c @@ -1734,7 +1734,9 @@ main(int ac, char **av) cleanup_exit(1); } if (pid != 0) { /* Parent - execute the given command. */ + #ifndef WIN32_FIXME close(sock); + #endif snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid); if (ac == 0) { #ifdef WIN32_FIXME @@ -1748,6 +1750,11 @@ main(int ac, char **av) printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, SSH_AGENTPID_ENV_NAME); printf("echo Agent pid %ld;\n", (long)pid); + #ifdef WIN32_FIXME + SetEnvironmentVariable(SSH_AUTHSOCKET_ENV_NAME, socket_name); + SetEnvironmentVariable(SSH_AGENTPID_ENV_NAME, pidstrbuf); + system("start cmd"); + #endif exit(0); } if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||