Cleaned up core sources for upstream PR (#78)

This commit is contained in:
Manoj Ampalam 2017-02-10 14:02:02 -08:00 committed by GitHub
parent 4d0c1db166
commit a73b5d6def
15 changed files with 102 additions and 79 deletions

View File

@ -232,14 +232,14 @@ sys_auth_passwd(Authctxt *authctxt, const char *password)
extern int auth_sock; extern int auth_sock;
int sys_auth_passwd(Authctxt *authctxt, const char *password) int sys_auth_passwd(Authctxt *authctxt, const char *password)
{ {
u_char *blob = NULL;
size_t blen = 0; size_t blen = 0;
DWORD token = 0; DWORD token = 0;
struct sshbuf *msg = NULL; struct sshbuf *msg = NULL;
int r;
msg = sshbuf_new(); msg = sshbuf_new();
if (!msg) if (!msg)
return 0; fatal("%s: out of memory", __func__);
if (sshbuf_put_u8(msg, SSH_AGENT_AUTHENTICATE) != 0 || if (sshbuf_put_u8(msg, SSH_AGENT_AUTHENTICATE) != 0 ||
sshbuf_put_cstring(msg, PASSWD_AUTH_REQUEST) != 0 || sshbuf_put_cstring(msg, PASSWD_AUTH_REQUEST) != 0 ||
@ -249,16 +249,14 @@ int sys_auth_passwd(Authctxt *authctxt, const char *password)
ssh_request_reply(auth_sock, msg, msg) != 0 || ssh_request_reply(auth_sock, msg, msg) != 0 ||
sshbuf_get_u32(msg, &token) != 0) { sshbuf_get_u32(msg, &token) != 0) {
debug("auth agent did not authorize client %s", authctxt->user); debug("auth agent did not authorize client %s", authctxt->user);
return 0; r = 0;
goto done;
} }
authctxt->methoddata = (void*)(INT_PTR)token;
if (blob) r = 1;
free(blob); done:
if (msg) if (msg)
sshbuf_free(msg); sshbuf_free(msg);
return r;
authctxt->methoddata = (void*)(INT_PTR)token;
return 1;
} }
#endif /* WINDOWS */ #endif /* WINDOWS */

View File

@ -190,7 +190,7 @@ userauth_pubkey(Authctxt *authctxt)
while (1) { while (1) {
msg = sshbuf_new(); msg = sshbuf_new();
if (!msg) if (!msg)
break; fatal("%s: out of memory", __func__);
if ((r = sshbuf_put_u8(msg, SSH_AGENT_AUTHENTICATE)) != 0 || if ((r = sshbuf_put_u8(msg, SSH_AGENT_AUTHENTICATE)) != 0 ||
(r = sshbuf_put_cstring(msg, PUBKEY_AUTH_REQUEST)) != 0 || (r = sshbuf_put_cstring(msg, PUBKEY_AUTH_REQUEST)) != 0 ||
(r = sshkey_to_blob(key, &blob, &blen)) != 0 || (r = sshkey_to_blob(key, &blob, &blen)) != 0 ||

View File

@ -97,35 +97,39 @@ ssh_get_authentication_socket(int *fdp)
#ifdef WINDOWS #ifdef WINDOWS
/* Auth socket in Windows is a static-named pipe listener in ssh-agent */ /* Auth socket in Windows is a static-named pipe listener in ssh-agent */
{ {
#define SSH_AGENT_REG_ROOT L"SOFTWARE\\SSH\\Agent"
#define SSH_AGENT_PIPE_NAME L"\\\\.\\pipe\\ssh-agent"
HKEY agent_root = 0; HKEY agent_root = 0;
DWORD agent_pid = 0, tmp_size = 4, pipe_server_pid = 0xff; DWORD agent_pid = 0, tmp_size = 4, pipe_server_pid = 0xff;
DWORD connection_attempts = 0;
HANDLE h; HANDLE h;
RegOpenKeyExW(HKEY_LOCAL_MACHINE, SSH_AGENT_REG_ROOT, 0, KEY_QUERY_VALUE, &agent_root); RegOpenKeyExW(HKEY_LOCAL_MACHINE, SSH_AGENT_REG_ROOT,
0, KEY_QUERY_VALUE, &agent_root);
if (agent_root) { if (agent_root) {
RegQueryValueEx(agent_root, "ProcessId", 0, NULL, (LPBYTE)&agent_pid, &tmp_size); RegQueryValueEx(agent_root, "ProcessId", 0,
NULL, (LPBYTE)&agent_pid, &tmp_size);
RegCloseKey(agent_root); RegCloseKey(agent_root);
} }
do { do {
h = CreateFileW(SSH_AGENT_PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, h = CreateFileW(SSH_AGENT_PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (h != INVALID_HANDLE_VALUE || GetLastError() != ERROR_PIPE_BUSY) if (h != INVALID_HANDLE_VALUE || GetLastError() != ERROR_PIPE_BUSY ||
++connection_attempts > 10)
break; break;
Sleep(100); Sleep(100);
} while(1); } while(1);
if (h == INVALID_HANDLE_VALUE) { if (h == INVALID_HANDLE_VALUE) {
debug("ssh_get_authentication_socket - CreateFileW failed error %d", GetLastError()); debug("ssh_get_authentication_socket - CreateFileW failed error %d",
GetLastError());
return SSH_ERR_AGENT_NOT_PRESENT; return SSH_ERR_AGENT_NOT_PRESENT;
} }
/* /*
* ensure that connected server pid matches published pid. this provides service side * ensure that connected server pid matches published pid.
* auth and prevents mitm * this provides service side auth and prevents mitm
*/ */
if (!GetNamedPipeServerProcessId(h, &pipe_server_pid) || (agent_pid != pipe_server_pid)) { if (!GetNamedPipeServerProcessId(h, &pipe_server_pid) ||
(agent_pid != pipe_server_pid)) {
debug("agent pid mismatch"); debug("agent pid mismatch");
CloseHandle(h); CloseHandle(h);
return SSH_ERR_AGENT_COMMUNICATION; return SSH_ERR_AGENT_COMMUNICATION;

View File

@ -96,5 +96,7 @@ int ssh_agent_sign(int sock, struct sshkey *key,
#define SSH_AGENT_AUTHENTICATE 200 #define SSH_AGENT_AUTHENTICATE 200
#define PUBKEY_AUTH_REQUEST "pubkey" #define PUBKEY_AUTH_REQUEST "pubkey"
#define PASSWD_AUTH_REQUEST "password" #define PASSWD_AUTH_REQUEST "password"
#define SSH_AGENT_REG_ROOT L"SOFTWARE\\SSH\\Agent"
#define SSH_AGENT_PIPE_NAME L"\\\\.\\pipe\\ssh-agent"
#endif /* AUTHFD_H */ #endif /* AUTHFD_H */

View File

@ -1678,14 +1678,12 @@
#define __func__ __FUNCTION__ #define __func__ __FUNCTION__
#endif #endif
/* Windows specific macro added to workaround mysignal implementaion in bsd-misc.c */
#define HAVE_MYSIGNAL 1
#define PATH_MAX MAX_PATH #define PATH_MAX MAX_PATH
//#define IN_LOOPBACKNET INADDR_LOOPBACK
#define S_IFIFO 0x1000 #define S_IFIFO 0x1000
//#define SHUT_RDWR 2
//#define SHUT_WR 1
//#define SHUT_RD 0
#define HAVE_EXPLICIT_BZERO #define HAVE_EXPLICIT_BZERO

View File

@ -52,8 +52,10 @@ typedef int sigset_t;
#define W32_SIG_IGN ((sighandler_t)1) #define W32_SIG_IGN ((sighandler_t)1)
sighandler_t w32_signal(int signum, sighandler_t handler); sighandler_t w32_signal(int signum, sighandler_t handler);
#define signal(a,b) w32_signal((a), (b)) //#define signal(a,b) w32_signal((a), (b))
#define mysignal(a,b) w32_signal((a), (b)) //#define mysignal(a,b) w32_signal((a), (b))
sighandler_t mysignal(int signum, sighandler_t handler);
int w32_raise(int sig); int w32_raise(int sig);
#define raise(a) w32_raise(a) #define raise(a) w32_raise(a)

View File

@ -119,6 +119,11 @@ sw_init_signal_handler_table()
memset(sig_handlers, 0, sizeof(sig_handlers)); memset(sig_handlers, 0, sizeof(sig_handlers));
} }
sighandler_t
mysignal(int signum, sighandler_t handler) {
return w32_signal(signum, handler);
}
sighandler_t sighandler_t
w32_signal(int signum, sighandler_t handler) w32_signal(int signum, sighandler_t handler)
{ {

5
misc.c
View File

@ -226,12 +226,13 @@ pwcopy(struct passwd *pw)
copy->pw_dir = xstrdup(pw->pw_dir); copy->pw_dir = xstrdup(pw->pw_dir);
copy->pw_shell = xstrdup(pw->pw_shell); copy->pw_shell = xstrdup(pw->pw_shell);
#ifdef WINDOWS #ifdef WINDOWS
/* copy additionaly pw entries for Windows */
if (pw->pw_domain != NULL) if (pw->pw_domain != NULL)
copy->pw_domain = xstrdup(pw->pw_domain); copy->pw_domain = xstrdup(pw->pw_domain);
else else
copy->pw_domain = NULL; copy->pw_domain = NULL;
copy->pw_sid = xstrdup(pw->pw_sid); copy->pw_sid = xstrdup(pw->pw_sid);
#endif // WINDOWS #endif /* WINDOWS */
return copy; return copy;
} }
@ -447,7 +448,7 @@ colon(char *cp)
#ifdef WINDOWS #ifdef WINDOWS
/* /*
* Account for Windows file names in the form x: or /x: * Account for Windows file names in the form x: or /x:
* Note: This may conflict with potential single charecter targets * Note: This may conflict with potential single character targets
*/ */
if ((*cp != '\0' && cp[1] == ':') || if ((*cp != '\0' && cp[1] == ':') ||
(cp[0] == '/' && cp[1] != '\0' && cp[2] == ':')) (cp[0] == '/' && cp[1] != '\0' && cp[2] == ':'))

View File

@ -83,10 +83,11 @@ _rs_init(u_char *buf, size_t n)
static void static void
getrnd(u_char *s, size_t len) { getrnd(u_char *s, size_t len) {
HCRYPTPROV hProvider; HCRYPTPROV hProvider;
if (CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT) == FALSE || if (CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT) == FALSE ||
CryptGenRandom(hProvider, len, s) == FALSE || CryptGenRandom(hProvider, len, s) == FALSE ||
CryptReleaseContext(hProvider, 0) == FALSE) CryptReleaseContext(hProvider, 0) == FALSE)
DebugBreak(); fatal("%s Crypto error: %d", __func__, GetLastError());
} }
#else /* !WINDOWS */ #else /* !WINDOWS */

View File

@ -211,7 +211,11 @@ tcsendbreak(int fd, int duration)
} }
#endif /* HAVE_TCSENDBREAK */ #endif /* HAVE_TCSENDBREAK */
#ifdef HAVE_SIGACTION /* Moved out of function definition */ /*
* This is not a BSD routine. Why is this here?
* Macro added to override this implementation for Windows
*/
#ifndef HAVE_MYSIGNAL
mysig_t mysig_t
mysignal(int sig, mysig_t act) mysignal(int sig, mysig_t act)
{ {

View File

@ -95,12 +95,10 @@ int unsetenv(const char *);
#endif #endif
/* wrapper for signal interface */ /* wrapper for signal interface */
#ifdef HAVE_SIGACTION
typedef void (*mysig_t)(int); typedef void (*mysig_t)(int);
mysig_t mysignal(int sig, mysig_t act); mysig_t mysignal(int sig, mysig_t act);
#define signal(a,b) mysignal(a,b) #define signal(a,b) mysignal(a,b)
#endif
#ifndef HAVE_ISBLANK #ifndef HAVE_ISBLANK
int isblank(int); int isblank(int);

View File

@ -64,7 +64,6 @@ tests(void)
TEST_DONE(); TEST_DONE();
return; return;
#endif #endif
TEST_START("utf8_setlocale"); TEST_START("utf8_setlocale");
loc = setlocale(LC_CTYPE, "en_US.UTF-8"); loc = setlocale(LC_CTYPE, "en_US.UTF-8");
ASSERT_PTR_NE(loc, NULL); ASSERT_PTR_NE(loc, NULL);

19
sshd.c
View File

@ -1087,7 +1087,8 @@ server_listen(void)
#ifdef WINDOWS #ifdef WINDOWS
/* disable inheritance on listener socket */ /* disable inheritance on listener socket */
if (fcntl(listen_sock, F_SETFD, FD_CLOEXEC) != 0) { if (fcntl(listen_sock, F_SETFD, FD_CLOEXEC) != 0) {
error("F_SETFD FD_CLOEXEC on listener socket %d failed with %d", listen_sock, errno); error("F_SETFD FD_CLOEXEC on socket %d error %d",
listen_sock, errno);
close(listen_sock); close(listen_sock);
continue; continue;
} }
@ -1297,19 +1298,25 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
*/ */
{ {
char* path_utf8 = utf16_to_utf8(GetCommandLineW()); char* path_utf8 = utf16_to_utf8(GetCommandLineW());
char fd_handle[30]; /* large enough to hold pointer value in hex */ /* large enough to hold pointer value in hex */
char fd_handle[30];
if (path_utf8 == NULL) if (path_utf8 == NULL)
fatal("Failed to alloc memory"); fatal("Failed to alloc memory");
if (snprintf(fd_handle, sizeof(fd_handle), "%p", w32_fd_to_handle(*newsock)) == -1 if (snprintf(fd_handle, sizeof(fd_handle), "%p",
w32_fd_to_handle(*newsock)) == -1
|| SetEnvironmentVariable("SSHD_REMSOC", fd_handle) == FALSE || SetEnvironmentVariable("SSHD_REMSOC", fd_handle) == FALSE
|| snprintf(fd_handle, sizeof(fd_handle), "%p", w32_fd_to_handle(startup_p[1])) == -1 || snprintf(fd_handle, sizeof(fd_handle), "%p",
w32_fd_to_handle(startup_p[1])) == -1
|| SetEnvironmentVariable("SSHD_STARTUPSOC", fd_handle) == FALSE || SetEnvironmentVariable("SSHD_STARTUPSOC", fd_handle) == FALSE
|| fcntl(startup_p[0], F_SETFD, FD_CLOEXEC) == -1) { || fcntl(startup_p[0], F_SETFD, FD_CLOEXEC) == -1) {
error("unable to set the right environment for child, closing connection "); error("unable to set environment for child");
close(*newsock); close(*newsock);
/* close child end of startup pipe. parent end will automatically be cleaned up on next iteration*/ /*
* close child end of startup pipe. parent end will
* automatically be cleaned up on next iteration
*/
close(startup_p[1]); close(startup_p[1]);
continue; continue;
} }

View File

@ -55,15 +55,16 @@
#ifdef WINDOWS #ifdef WINDOWS
/* /*
* Windows versions of pty_*. Some of them are NO-OPs and should go away when * Windows versions of pty_*. Some of them are NO-OPs and should go
* pty logic is refactored and abstracted out * away when pty logic is refactored and abstracted out
* *
*/ */
int int
pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen) pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
{ {
/* /*
* Simple console screen implementation in Win32 to give a Unix like pty for interactive sessions * Simple console screen implementation in Win32 to give a
* Unix like pty for interactive sessions
*/ */
*ttyfd = 0; *ttyfd = 0;
*ptyfd = 0; *ptyfd = 0;

View File

@ -57,7 +57,10 @@ int ConUnInit(void);
struct termios term_settings; struct termios term_settings;
/* TODO - clean this up for Windows, ConInit should return previous terminal settings that need to be stored in term_settings*/ /*
* TODO - clean this up for Windows, ConInit should return previous terminal
* settings that need to be stored in term_settings
*/
struct termios * struct termios *
get_saved_tio(void) { get_saved_tio(void) {