From 2447272622cc65313d4ed48f4274a9e67dd068d2 Mon Sep 17 00:00:00 2001 From: Ray Hayes Date: Tue, 18 Oct 2016 11:21:34 -0700 Subject: [PATCH] Initial WIN32_FIXME changes. --- contrib/win32/openssh/config.h.vs | 4 + contrib/win32/openssh/paths.targets | 12 +- contrib/win32/win32compat/inc/unistd.h | 1 + contrib/win32/win32compat/inc/w32posix.h | 2 +- contrib/win32/win32compat/shell-host.c | 67 +++- contrib/win32/win32compat/w32fd.c | 17 + misc.c | 28 +- progressmeter.c | 17 +- scp.c | 429 ++++++++++------------- sftp-client.c | 40 +-- sftp-common.c | 35 +- sftp-glob.c | 4 +- sftp.c | 83 +---- 13 files changed, 345 insertions(+), 394 deletions(-) diff --git a/contrib/win32/openssh/config.h.vs b/contrib/win32/openssh/config.h.vs index 1236341..b6cb81c 100644 --- a/contrib/win32/openssh/config.h.vs +++ b/contrib/win32/openssh/config.h.vs @@ -1581,6 +1581,7 @@ #define _CRT_SECURE_NO_DEPRECATE 1 #define _CRT_NONSTDC_NO_DEPRECATE 1 #define WIN32_FIXME 1 +#define WINDOWS 1 /* Define if you must implement a startup_needs function for your platform */ #define HAVE_STARTUP_NEEDS 1 @@ -1631,6 +1632,9 @@ #define HAVE_STRNCASECMP 1 #endif +/* Define to 1 if you have the locale.h header. */ +#define HAVE_LOCALE_H 1 + #define HAVE_STRUCT_IN6_ADDR 1 #define HAVE_STRUCT_SOCKADDR_IN6 1 #define HAVE_STRUCT_TIMEVAL 1 diff --git a/contrib/win32/openssh/paths.targets b/contrib/win32/openssh/paths.targets index 21322fe..0d37f85 100644 --- a/contrib/win32/openssh/paths.targets +++ b/contrib/win32/openssh/paths.targets @@ -4,10 +4,12 @@ $(SolutionDir)..\..\..\ $(SolutionDir)..\..\..\bin\ $(SolutionDir)lib\ - $(SolutionDir)..\..\..\..\OpenSSL\1.0.2d\VS2015\ - $(SolutionDir)..\..\..\..\OpenSSL\1.0.2d\VS2015\Win32\Release\ - $(SolutionDir)..\..\..\..\OpenSSL\1.0.2d\VS2015\Win32\Debug\ - $(SolutionDir)..\..\..\..\OpenSSL\1.0.2d\VS2015\x64\Release\ - $(SolutionDir)..\..\..\..\OpenSSL\1.0.2d\VS2015\x64\Debug\ + g:\openssl-1.0.2h-x64\OpenSSLInstallx64_vs2015-debug\ + g:\openssl-1.0.2h-x86\OpenSSLInstallx86_vs2015\ + g:\openssl-1.0.2h-x86\OpenSSLInstallx86_vs2015-debug\ + g:\openssl-1.0.2h-x64\OpenSSLInstallx64_vs2015\ + g:\openssl-1.0.2h-x64\OpenSSLInstallx64_vs2015-debug\ + g:\openssl-1.0.2h-arm-x86\OpenSSLInstallx86_vs2015\ + g:\openssl-1.0.2h-arm-x86\OpenSSLInstallx86_vs2015\ diff --git a/contrib/win32/win32compat/inc/unistd.h b/contrib/win32/win32compat/inc/unistd.h index 3e1a055..b4d3558 100644 --- a/contrib/win32/win32compat/inc/unistd.h +++ b/contrib/win32/win32compat/inc/unistd.h @@ -24,6 +24,7 @@ #define open w32_open #define read w32_read #define write w32_write +#define writev w32_writev //#define isatty w32_isatty #define close w32_close #define dup w32_dup diff --git a/contrib/win32/win32compat/inc/w32posix.h b/contrib/win32/win32compat/inc/w32posix.h index 4fc91df..5e94cff 100644 --- a/contrib/win32/win32compat/inc/w32posix.h +++ b/contrib/win32/win32compat/inc/w32posix.h @@ -44,6 +44,7 @@ int w32_pipe(int *pfds); int w32_open(const char *pathname, int flags, ...); int w32_read(int fd, void *dst, size_t max); int w32_write(int fd, const void *buf, unsigned int max); +int w32_writev(int fd, const struct iovec *iov, int iovcnt); int w32_fstat(int fd, struct w32_stat *buf); int w32_stat(const char *path, struct w32_stat *buf); long w32_lseek( int fd, long offset, int origin); @@ -117,7 +118,6 @@ int sw_add_child(HANDLE child, DWORD pid); #define allocate_sfd(a, b) w32_allocate_fd_for_handle((a, b)) //#define WSHELPwopen(a, b) w32_open((a, b)) - /* TODO - These defs need to revisited and positioned appropriately */ #define environ _environ diff --git a/contrib/win32/win32compat/shell-host.c b/contrib/win32/win32compat/shell-host.c index 86129dd..760bf42 100644 --- a/contrib/win32/win32compat/shell-host.c +++ b/contrib/win32/win32compat/shell-host.c @@ -50,6 +50,33 @@ #define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200 #endif +HINSTANCE hUserLibrary; +HINSTANCE hKernelLibrary; + +// kernel32.dll +typedef BOOL(WINAPI * fSetCurrentConsoleFontEx)( + _In_ HANDLE hConsoleOutput, + _In_ BOOL bMaximumWindow, + _In_ PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); +fSetCurrentConsoleFontEx fnSetCurrentConsoleFontEx = NULL; + +// user32.dll +typedef HWINEVENTHOOK (WINAPI * fSetWinEventHook)( + _In_ UINT eventMin, + _In_ UINT eventMax, + _In_ HMODULE hmodWinEventProc, + _In_ WINEVENTPROC lpfnWinEventProc, + _In_ DWORD idProcess, + _In_ DWORD idThread, + _In_ UINT dwflags +); +fSetWinEventHook fnSetWinEventHook = NULL; + +typedef BOOL (WINAPI * fUnhookWinEvent)( + _In_ HWINEVENTHOOK hWinEventHook +); +fUnhookWinEvent fnUnhookWinEvent = NULL; + typedef struct consoleEvent { DWORD event; HWND hwnd; @@ -66,6 +93,7 @@ BOOL bRet = FALSE; BOOL bNoScrollRegion = FALSE; BOOL bStartup = TRUE; BOOL bAnsi = FALSE; +BOOL bHookEvents = FALSE; HANDLE child_out = INVALID_HANDLE_VALUE; HANDLE child_in = INVALID_HANDLE_VALUE; @@ -389,7 +417,8 @@ void SizeWindow(HANDLE hInput) { matchingFont.FontWeight = FW_NORMAL; wcscpy(matchingFont.FaceName, L"Consolas"); - bSuccess = SetCurrentConsoleFontEx(child_out, FALSE, &matchingFont); + if(fnSetCurrentConsoleFontEx != NULL) + bSuccess = fnSetCurrentConsoleFontEx(child_out, FALSE, &matchingFont); // This information is the live screen ZeroMemory(&consoleInfo, sizeof(consoleInfo)); @@ -1058,6 +1087,22 @@ int wmain(int ac, wchar_t **av) { DWORD dwStatus; HANDLE hEventHook = NULL; + HINSTANCE hUserLibrary; + HINSTANCE hKernelLibrary; + + hKernelLibrary = LoadLibrary(L"kernel32.dll"); + if (hKernelLibrary != NULL) + { + fnSetCurrentConsoleFontEx = (fSetCurrentConsoleFontEx)GetProcAddress(hKernelLibrary, "SetCurrentConsoleFontEx"); + } + + hUserLibrary = LoadLibrary(L"user32.dll"); + if (hUserLibrary != NULL) + { + fnSetWinEventHook = (fSetWinEventHook)GetProcAddress(hUserLibrary, "SetWinEventHook"); + fnUnhookWinEvent = (fUnhookWinEvent)GetProcAddress(hUserLibrary, "UnhookWinEvent"); + } + pipe_in = GetStdHandle(STD_INPUT_HANDLE); pipe_out = GetStdHandle(STD_OUTPUT_HANDLE); pipe_err = GetStdHandle(STD_ERROR_HANDLE); @@ -1094,8 +1139,9 @@ int wmain(int ac, wchar_t **av) { InitializeCriticalSection(&criticalSection); - hEventHook = SetWinEventHook(EVENT_CONSOLE_CARET, EVENT_CONSOLE_LAYOUT, NULL, - ConsoleEventProc, 0, 0, WINEVENT_OUTOFCONTEXT); + if(fnSetWinEventHook != NULL) + hEventHook = SetWinEventHook(EVENT_CONSOLE_CARET, EVENT_CONSOLE_LAYOUT, NULL, + ConsoleEventProc, 0, 0, WINEVENT_OUTOFCONTEXT); memset(&si, 0, sizeof(STARTUPINFO)); memset(&pi, 0, sizeof(PROCESS_INFORMATION)); @@ -1103,6 +1149,12 @@ int wmain(int ac, wchar_t **av) { // Copy our parent buffer sizes si.cb = sizeof(STARTUPINFO); si.dwFlags = 0; + if (fnSetWinEventHook == NULL) { + si.hStdInput = INVALID_HANDLE_VALUE; + si.hStdOutput = pipe_out; + si.hStdError = pipe_err; + si.dwFlags = STARTF_USESTDHANDLES; + } /* disable inheritance on pipe_in*/ GOTO_CLEANUP_ON_FALSE(SetHandleInformation(pipe_in, HANDLE_FLAG_INHERIT, 0)); @@ -1163,8 +1215,13 @@ cleanup: WaitForSingleObject(monitor_thread, INFINITE); if (ux_thread != INVALID_HANDLE_VALUE) TerminateThread(ux_thread, S_OK); - if (hEventHook) - UnhookWinEvent(hEventHook); + if (hEventHook && fnUnhookWinEvent != NULL) + fnUnhookWinEvent(hEventHook); + + if (hKernelLibrary != NULL) + FreeLibrary(hKernelLibrary); + if (hUserLibrary != NULL) + FreeLibrary(hUserLibrary); FreeConsole(); diff --git a/contrib/win32/win32compat/w32fd.c b/contrib/win32/win32compat/w32fd.c index 4fa0d67..40446d2 100644 --- a/contrib/win32/win32compat/w32fd.c +++ b/contrib/win32/win32compat/w32fd.c @@ -382,6 +382,23 @@ w32_write(int fd, const void *buf, unsigned int max) { return fileio_write(fd_table.w32_ios[fd], buf, max); } +int w32_writev(int fd, const struct iovec *iov, int iovcnt) { + int written = 0; + int i = 0; + + CHECK_FD(fd); + + for (i = 0; i < iovcnt; i++) { + int ret = write(fd, iov[i].iov_base, iov[i].iov_len); + + if (ret > 0) { + written += ret; + } + } + + return written; +} + int w32_fstat(int fd, struct w32_stat *buf) { CHECK_FD(fd); diff --git a/misc.c b/misc.c index 0d2b2ea..e42c23f 100644 --- a/misc.c +++ b/misc.c @@ -436,9 +436,34 @@ char * colon(char *cp) { int flag = 0; + int len = 0; if (*cp == ':') /* Leading colon is part of file name. */ return NULL; + +#ifdef WINDOWS + for (; *cp; ++cp) { + len++; + + if (*cp == '[') + flag = 1; + + if (flag && *cp != ']') + continue; + + if (*cp == ']') + flag = 0; + + if (*cp == ':') { + if (len != 2) { // avoid x: format for drive letter in Windows + return (cp); + } + } + // if ( (*cp == '/') || (*cp == '\\') ) + // return (0); + } + return NULL; +#else if (*cp == '[') flag = 1; @@ -452,7 +477,8 @@ colon(char *cp) if (*cp == '/') return NULL; } - return NULL; + return NULL; +#endif } /* function to assist building execv() arguments */ diff --git a/progressmeter.c b/progressmeter.c index 233283d..b632d5f 100644 --- a/progressmeter.c +++ b/progressmeter.c @@ -81,10 +81,16 @@ static const char unit[] = " KMGT"; static int can_output(void) { -#ifndef WIN32_FIXME//R - return (getpgrp() == tcgetpgrp(STDOUT_FILENO)); +#ifndef WINDOWS + return (getpgrp() == tcgetpgrp(STDOUT_FILENO)); #else - return 1; + DWORD dwProcessId = -1; + if (GetWindowThreadProcessId(GetStdHandle(STD_OUTPUT_HANDLE), &dwProcessId)) { + return(GetCurrentProcess() == dwProcessId); + } + else { + return -1; + } #endif } @@ -298,7 +304,7 @@ sig_winch(int sig) static void setscreensize(void) { - #ifndef WIN32_FIXME//N +#ifndef WINDOWS struct winsize winsize; if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 && @@ -310,6 +316,7 @@ setscreensize(void) } else win_size = DEFAULT_WINSIZE; win_size += 1; /* trailing \0 */ +#else + win_size = ConScreenSizeX() + 1; #endif - win_size = DEFAULT_WINSIZE + 1; } diff --git a/scp.c b/scp.c index 67997f2..dcf2a3d 100644 --- a/scp.c +++ b/scp.c @@ -93,7 +93,7 @@ #include -#ifndef WIN32_FIXME +#ifndef WINDOWS #include #endif @@ -159,14 +159,11 @@ char *ssh_program = _PATH_SSH_PROGRAM; pid_t do_cmd_pid = -1; -#ifdef WIN32_FIXME +#ifdef WINDOWS typedef BOOL bool; #define false FALSE #define true TRUE -char *win32colon(char *); -#define colon win32colon - #ifndef _SH_DENYNO #define _SH_DENYNO 0x40 #endif @@ -293,21 +290,27 @@ int start_process_io(char *exename, char **argv, char **envv, unsigned long CreateFlags, PROCESS_INFORMATION *pi, char *homedir, char *lpDesktop); +#ifdef WINDOWS + // InitForMicrosoftWindows() will initialize Unix like settings in Windows operating system. struct passwd pw; char username[128]; int InitForMicrosoftWindows() { - int rc; - struct passwd *pwd; + int rc; + struct passwd *pwd; - /* Get user\'s passwd structure. We need this for the home directory. */ - pwd = &pw ; - rc = sizeof(username); - GetUserName(username,(LPDWORD)&rc); - pwd->pw_name = username; + /* Get user\'s passwd structure. We need this for the home directory. */ + pwd = &pw ; + rc = sizeof(username); + if (GetUserName(username, (LPDWORD)&rc)) { + pwd->pw_name = username; + } + else { + return GetLastError(); + } - return 0; + return 0; } // start of direntry functions in Windows NT like UNIX @@ -324,8 +327,8 @@ struct scp_dirent { }; typedef struct { - long hFile; - struct _finddata_t c_file; + long hFile; + struct _finddata_t c_file; } SCPDIR; @@ -343,21 +346,7 @@ char * fixslashes(char * str) return str; } -char * unfixslashes(char * str) -{ - int i; - if (str == NULL) - return str; - - int len = (int)strlen(str); - - for (i = 0; i < len; i++) - if (str[i] == '//') - str[i] = '/'; - return str; -} - -// force path separator to +// force path separator to sep char * forcepathsep(char * str, char sep) { int i; @@ -374,7 +363,6 @@ char * forcepathsep(char * str, char sep) if (sep == '/') antisep = '\\'; - int len = (int)strlen(str); for (i = 0; i < len; i++) @@ -406,13 +394,10 @@ bool getRootFrompath(char * path, char * root) return (lastslash != NULL); } - - /* * get option letter from argument vector */ - char * getfilenamefrompath(char * path) { char * lastslash; @@ -447,7 +432,7 @@ char * getfilenamefrompath(char * path) } return NULL; } - +#endif #define EMSG "" #define BADCH (int)'~' @@ -669,7 +654,7 @@ do_local_cmd(arglist *a) fprintf(stderr, " %s", a->list[i]); fprintf(stderr, "\n"); } - #ifdef WIN32_FIXME +#ifdef WINDOWS // flatten the cmd into a long space separated string and execute using system(cmd) api char cmdstr[2048] ; cmdstr[0] = '\0' ; @@ -680,7 +665,7 @@ do_local_cmd(arglist *a) if (system(cmdstr)) return (-1); // failure executing return (0); // success - #else +#else if ((pid = fork()) == -1) fatal("do_local_cmd: fork: %s", strerror(errno)); @@ -705,7 +690,7 @@ do_local_cmd(arglist *a) return (-1); return (0); - #endif +#endif } static int pipe_counter = 1; @@ -770,179 +755,168 @@ error: int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout) { - #ifdef WIN32_FIXME - size_t i, j; +#ifdef WINDOWS + size_t i, j; - HANDLE hSaveStdout, hSaveStdin ; - HANDLE hstdout[2], hstdin[2] ; - PROCESS_INFORMATION pi; - SECURITY_ATTRIBUTES sa ; /* simple */ - int rc; - HANDLE rfdfromssh, wfdtossh ; - char *args[256]; + HANDLE hSaveStdout, hSaveStdin ; + HANDLE hstdout[2], hstdin[2] ; + PROCESS_INFORMATION pi; + SECURITY_ATTRIBUTES sa ; /* simple */ + int rc; + HANDLE rfdfromssh, wfdtossh ; + char *args[256]; - if (verbose_mode) - fprintf(stderr, "Executing: host %s, user %s, command %s\n", - host, remuser ? remuser : "(unspecified)", cmd); + if (verbose_mode) + fprintf(stderr, "Executing: host %s, user %s, command %s\n", + host, remuser ? remuser : "(unspecified)", cmd); // Child code in Windows OS will be a new created process of ssh.exe. // Child to execute the command on the remote host using ssh. - if (1) { // No fork in Windows OS, so we code it such that we use CreateProcess() + i = 0; + args[i++] = ssh_program; + size_t len; + for(j = 0; j < ssh_options_cnt; j++) { + args[i++] = "-o"; - i = 0; - args[i++] = ssh_program; - size_t len; - for(j = 0; j < ssh_options_cnt; j++) { - args[i++] = "-o"; + //args[i++] = ssh_options[j]; + len = strlen(ssh_options[j])+3; - //args[i++] = ssh_options[j]; - len = strlen(ssh_options[j])+3; + args[i] = (char *) malloc(len); // add quotes + strcpy_s(args[i],len, "\""); + strcat_s(args[i],len, ssh_options[j]); + strcat_s(args[i],len, "\""); + i++ ; - args[i] = (char *) malloc(len); // add quotes - strcpy_s(args[i],len, "\""); - strcat_s(args[i],len, ssh_options[j]); - strcat_s(args[i],len, "\""); - i++ ; + if (i > 250) + fatal("Too many -o options (total number of arguments is more than 256)"); + } + args[i++] = "-x"; + args[i++] = "-a"; + args[i++] = "\"-oFallBackToRsh no\""; // extra double quote needed for + // Windows platforms + //7/2/2001 args[i++] = "\"-oClearAllForwardings yes\""; + if (verbose_mode) + args[i++] = "-v"; + if (compress) + args[i++] = "-C"; + if (!use_privileged_port) + args[i++] = "-P"; + if (batchmode) + args[i++] = "\"-oBatchMode yes\""; + if (password != NULL) { + args[i++] = "-A"; + args[i++] = password; + } + if (cipher != NULL) { + args[i++] = "-c"; + args[i++] = cipher; + } + if (identity != NULL) { + args[i++] = "-i"; + args[i++] = identity; + } + if (port != NULL) { + args[i++] = "-p"; + args[i++] = port; + } + if (remuser != NULL) { + args[i++] = "-l"; + args[i++] = remuser; + } - if (i > 250) - fatal("Too many -o options (total number of arguments is more than 256)"); - } - args[i++] = "-x"; - args[i++] = "-a"; - args[i++] = "\"-oFallBackToRsh no\""; // extra double quote needed for - // Windows platforms - //7/2/2001 args[i++] = "\"-oClearAllForwardings yes\""; - if (verbose_mode) - args[i++] = "-v"; - if (compress) - args[i++] = "-C"; - if (!use_privileged_port) - args[i++] = "-P"; - if (batchmode) - args[i++] = "\"-oBatchMode yes\""; - if (password != NULL) - { - args[i++] = "-A"; - args[i++] = password; - } - if (cipher != NULL) - { - args[i++] = "-c"; - args[i++] = cipher; - } - if (identity != NULL) - { - args[i++] = "-i"; - args[i++] = identity; - } - if (port != NULL) - { - args[i++] = "-p"; - args[i++] = port; - } - if (remuser != NULL) - { - args[i++] = "-l"; - args[i++] = remuser; - } + if (ipv_restrict == ONLY_IPV4) + args[i++] = "-4"; + if (ipv_restrict == ONLY_IPV6) + args[i++] = "-6"; - if (ipv_restrict == ONLY_IPV4) - args[i++] = "-4"; - if (ipv_restrict == ONLY_IPV6) - args[i++] = "-6"; + args[i++] = host; + args[i++] = cmd; + args[i++] = NULL; - args[i++] = host; - args[i++] = cmd; - args[i++] = NULL; + // Create a pair of pipes for communicating with ssh + // which we will spawn + // Do the plunmbing so that child ssh process to be spawned has its + // standard input from the pout[0] and its standard output going to + // pin[1] - // Create a pair of pipes for communicating with ssh - // which we will spawn - // Do the plunmbing so that child ssh process to be spawned has its - // standard input from the pout[0] and its standard output going to - // pin[1] + sa.nLength = sizeof(SECURITY_ATTRIBUTES); + sa.bInheritHandle = TRUE ; /* pipe handles to be inherited */ + sa.lpSecurityDescriptor = NULL; + /* command processor output redirected to a nameless pipe */ - sa.nLength = sizeof(SECURITY_ATTRIBUTES); - sa.bInheritHandle = TRUE ; /* pipe handles to be inherited */ - sa.lpSecurityDescriptor = NULL; - /* command processor output redirected to a nameless pipe */ + rc = CreateOverlappedPipe( &hstdout[0], &hstdout[1], &sa, 0 ) ; + /* read from this fd to get data from ssh.exe*/ - rc = CreateOverlappedPipe( &hstdout[0], &hstdout[1], &sa, 0 ) ; - /* read from this fd to get data from ssh.exe*/ + // make scp's pipe read handle not inheritable by ssh + rc = DuplicateHandle(GetCurrentProcess(), hstdout[0], + GetCurrentProcess(), (PHANDLE) &rfdfromssh, + 0, // this parm ignored if DUPLICATE_SAME_ACCESS below + FALSE, // not inherited + DUPLICATE_SAME_ACCESS); + CloseHandle(hstdout[0]); // this CloseHandle() is a crucial must do + hstdout[0] = rfdfromssh ; - // make scp's pipe read handle not inheritable by ssh - rc = DuplicateHandle(GetCurrentProcess(), hstdout[0], - GetCurrentProcess(), (PHANDLE) &rfdfromssh, - 0, // this parm ignored if DUPLICATE_SAME_ACCESS below - FALSE, // not inherited - DUPLICATE_SAME_ACCESS); - CloseHandle(hstdout[0]); // this CloseHandle() is a crucial must do - hstdout[0] = rfdfromssh ; + *fdin = _open_osfhandle((intptr_t)hstdout[0],0); + _setmode (*fdin, O_BINARY); // set this file handle for binary I/O + rc = CreateOverlappedPipe( &hstdin[0], &hstdin[1], &sa, 0 ) ; + /* write to this fd to get data into ssh.exe*/ - *fdin = _open_osfhandle((intptr_t)hstdout[0],0); - _setmode (*fdin, O_BINARY); // set this file handle for binary I/O + // make scp's pipe write handle not inheritable by ssh + rc = DuplicateHandle(GetCurrentProcess(), hstdin[1], + GetCurrentProcess(), (PHANDLE) &wfdtossh, + 0, // this parm ignored if DUPLICATE_SAME_ACCESS below + FALSE, // not inherited + DUPLICATE_SAME_ACCESS); + CloseHandle(hstdin[1]); // this CloseHandle() is a crucial must do + hstdin[1] = (HANDLE) wfdtossh ; - rc = CreateOverlappedPipe( &hstdin[0], &hstdin[1], &sa, 0 ) ; - /* write to this fd to get data into ssh.exe*/ + *fdout = _open_osfhandle((intptr_t)hstdin[1],0); + _setmode (*fdout, O_BINARY); // set this file handle for binary I/O - // make scp's pipe write handle not inheritable by ssh - rc = DuplicateHandle(GetCurrentProcess(), hstdin[1], - GetCurrentProcess(), (PHANDLE) &wfdtossh, - 0, // this parm ignored if DUPLICATE_SAME_ACCESS below - FALSE, // not inherited - DUPLICATE_SAME_ACCESS); - CloseHandle(hstdin[1]); // this CloseHandle() is a crucial must do - hstdin[1] = (HANDLE) wfdtossh ; + hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); + //hSaveStderr = GetStdHandle(STD_ERROR_HANDLE); + hSaveStdin = GetStdHandle(STD_INPUT_HANDLE); + // Set a write handle to the pipe to be STDOUT. + SetStdHandle(STD_OUTPUT_HANDLE, hstdout[1]); + // Set a write handle to the pipe to be STDERR. + //SetStdHandle(STD_ERROR_HANDLE, hstdout[1]); + // Set a input handle to the pipe to be STDIN. + SetStdHandle(STD_INPUT_HANDLE, hstdin[0]); - *fdout = _open_osfhandle((intptr_t)hstdin[1],0); - _setmode (*fdout, O_BINARY); // set this file handle for binary I/O + // start the child process(ssh) + rc = start_process_io( + NULL, /* executable name with .ext found in argv[0] */ + &args[0], /* argv */ + NULL , + hstdin[0], /* std input for cmd.exe */ + hstdout[1], /* std output for cmd.exe */ + GetStdHandle(STD_ERROR_HANDLE), //hstdout[1], /* std error for cmd.exe */ + 0, // dwStartupFlags, + &pi, + NULL, /* current directory is default directory we set before */ + NULL + ); - hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); - //hSaveStderr = GetStdHandle(STD_ERROR_HANDLE); - hSaveStdin = GetStdHandle(STD_INPUT_HANDLE); + if (!rc) { + printf("%s could not be started\n", ssh_program); + exit(1); + } + else { + hprocess = pi.hProcess ; + } - // Set a write handle to the pipe to be STDOUT. - SetStdHandle(STD_OUTPUT_HANDLE, hstdout[1]); - // Set a write handle to the pipe to be STDERR. - //SetStdHandle(STD_ERROR_HANDLE, hstdout[1]); - // Set a input handle to the pipe to be STDIN. - SetStdHandle(STD_INPUT_HANDLE, hstdin[0]); + // After process creation, restore the saved STDOUT and STDERR. + SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout); + //SetStdHandle(STD_ERROR_HANDLE, hSaveStderr); + SetStdHandle(STD_INPUT_HANDLE, hSaveStdin); - - // start the child process(ssh) - rc = start_process_io( - NULL, /* executable name with .ext found in argv[0] */ - &args[0], /* argv */ - NULL , - hstdin[0], /* std input for cmd.exe */ - hstdout[1], /* std output for cmd.exe */ - GetStdHandle(STD_ERROR_HANDLE), //hstdout[1], /* std error for cmd.exe */ - 0, // dwStartupFlags, - &pi, - NULL, /* current directory is default directory we set before */ - NULL - ); - - if (!rc) { - printf("%s could not be started\n", ssh_program); - exit(1); - } - else { - hprocess = pi.hProcess ; - } - - // After process creation, restore the saved STDOUT and STDERR. - SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout); - //SetStdHandle(STD_ERROR_HANDLE, hSaveStderr); - SetStdHandle(STD_INPUT_HANDLE, hSaveStdin); - - /* now close the pipe's side that the ssh.exe will use as write handle */ - CloseHandle(hstdout[1]) ; - /* now close the pipe's side that the ssh.exe will use as read handle */ - CloseHandle(hstdin[0]) ; - } + /* now close the pipe's side that the ssh.exe will use as write handle */ + CloseHandle(hstdout[1]) ; + /* now close the pipe's side that the ssh.exe will use as read handle */ + CloseHandle(hstdin[0]) ; // update passed variables with where other funstions should read and write // from to get I/O from above child process over pipe. @@ -950,8 +924,8 @@ do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout) //*fdout = remout; //*fdin = remin; - return 0; - #else + return 0; +#else int pin[2], pout[2], reserved[2]; if (verbose_mode) @@ -1016,7 +990,7 @@ do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout) signal(SIGINT, killchild); signal(SIGHUP, killchild); return 0; - #endif +#endif } /* @@ -1027,8 +1001,7 @@ do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout) int do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout) { - #ifndef WIN32_FIXME - +#ifndef WIN32_FIXME pid_t pid; int status; @@ -1062,7 +1035,7 @@ do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout) while (waitpid(pid, &status, 0) == -1) if (errno != EINTR) fatal("do_cmd2: waitpid: %s", strerror(errno)); - #endif +#endif return 0; } @@ -1104,9 +1077,7 @@ main(int argc, char **argv) extern int optind; /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ - #ifndef WIN32_FIXME sanitise_stdfd(); - #endif /* Copy argv, because we modify it */ newargv = xcalloc(MAX(argc + 1, 1), sizeof(*newargv)); @@ -1208,22 +1179,18 @@ main(int argc, char **argv) argc -= optind; argv += optind; - #ifndef WIN32_FIXME +#ifndef WINDOWS if ((pwd = getpwuid(userid = getuid())) == NULL) fatal("unknown user %u", (u_int) userid); - #else +#else InitForMicrosoftWindows(); // picks the username, user home dir - #endif +#endif if (!isatty(STDOUT_FILENO)) showprogress = 0; remin = STDIN_FILENO; remout = STDOUT_FILENO; - #ifdef WIN32_FIXME - _setmode(remin,O_BINARY); // needed for Windows OS to avoid CrLf translations of text mode - _setmode(remout,O_BINARY); - #endif if (fflag) { /* Follow "protocol", send data. */ @@ -1249,7 +1216,7 @@ main(int argc, char **argv) iamrecursive ? " -r" : "", pflag ? " -p" : "", targetshouldbedirectory ? " -d" : ""); - #ifndef WIN32_FIXME + #ifndef WINDOWS (void) signal(SIGPIPE, lostconn); #endif @@ -1264,7 +1231,7 @@ main(int argc, char **argv) * Finally check the exit status of the ssh process, if one was forked * and no error has occurred yet */ - #ifndef WIN32_FIXME +#ifndef WINDOWS if (do_cmd_pid != -1 && errs == 0) { if (remin != -1) (void) close(remin); @@ -1277,7 +1244,7 @@ main(int argc, char **argv) errs = 1; } } - #endif +#endif exit(errs != 0); } @@ -1443,7 +1410,6 @@ tolocal(int argc, char **argv) for (i = 0; i < argc - 1; i++) { if (!(src = colon(argv[i]))) { /* Local to local. */ - #ifndef WIN32_FIXME freeargs(&alist); addargs(&alist, "%s", _PATH_CP); if (iamrecursive) @@ -1455,7 +1421,6 @@ tolocal(int argc, char **argv) addargs(&alist, "%s", argv[argc-1]); if (do_local_cmd(&alist)) ++errs; - #endif continue; } *src++ = 0; @@ -2823,36 +2788,6 @@ lostconn(int signo) exit(1); } #else -char *win32colon(char *cp) -{ - int len=0; - bool bSkip = false; - - if (*cp == ':') /* Leading colon is part of file name. */ - return (0); - - for (; *cp; ++cp) { - len++; - - if (*cp == '[') - bSkip = true; - - if (bSkip && *cp!= ']') - continue; - - if (*cp == ']') - bSkip = false; - - if (*cp == ':') { - if ( len != 2 ) { // avoid x: format for drive letter in Windows - return (cp); - } - } - // if ( (*cp == '/') || (*cp == '\\') ) - // return (0); - } - return (0); -} void verifydir(char *cp) { @@ -2910,8 +2845,6 @@ void lostconn(int signo) } #endif -#ifdef WIN32_FIXME - #ifdef WITH_SCP_STATS void stats_fixlen(int bwritten) { @@ -2927,17 +2860,11 @@ void stats_fixlen(int bwritten) fflush(SOME_STATS_FILE); } - char *stat_eta_new(int msecs) { static char stat_result[32]; int hours = 0, mins = 0, secs = 0; - // hours = msecs / 3600000; - // msecs %= 3600000; - // mins = msecs / 60000; - // msecs %= 60000; - hours = msecs / 3600000; msecs %= 3600000; mins = msecs / 60000; @@ -2954,7 +2881,7 @@ char *stat_eta_new(int msecs) return(stat_result); } -char *stat_eta_old(int secs) +char *stat_eta(int secs) { static char stat_result[20]; int hours, mins; @@ -2964,19 +2891,19 @@ char *stat_eta_old(int secs) mins = secs / 60; secs %= 60; - sprintf_s(stat_result, sizeof(stat_result), "%02d:%02d:%02d", hours, mins, secs); + sprintf(stat_result, "%02d:%02d:%02d", hours, mins, secs); return(stat_result); } #endif /* WITH_SCP_STATS */ - +#ifdef WIN32_FIXME char *TranslatePath(char *path, bool *bDirSpec) { - char temp[MAX_PATH*2]; + char temp[MAX_PATH * 2]; char resolved[MAX_PATH]; - char * rootpath; + char* rootpath; - if ( iamremote == 0) + if (iamremote == 0) return path; // if we are scp client, nothing special to do, return path we got. char *s = NULL; @@ -3007,7 +2934,6 @@ char *TranslatePath(char *path, bool *bDirSpec) fixslashes(temp); PathCanonicalizeA(resolved,temp); - *bDirSpec = (resolved[strlen(temp)-1] == '\\'); // Remove trailing slash unless it's a root spec (c:\ etc) if (strcmp(&(resolved[1]),":\\") && resolved[strlen(temp)-1] == '\\') @@ -3033,7 +2959,6 @@ char *TranslatePath(char *path, bool *bDirSpec) if (path[0] != '/' && path[0] != '\\') return path; - s = (char *)LocalAlloc(LPTR,strlen(resolved)+1); strcpy_s(s,strlen(resolved)+1,resolved); isRootedPath = 1; diff --git a/sftp-client.c b/sftp-client.c index 141234f..83cdfb0 100644 --- a/sftp-client.c +++ b/sftp-client.c @@ -109,30 +109,8 @@ } } - /** TODO - Move this to POSIX wrapper**/ - /* ??? What if fd is nonblocking ???*/ - int writev(int fd, struct iovec *iov, int iovcnt) - { - int written = 0; - - int i = 0; - - for (i = 0; i < iovcnt; i++) - { - int ret = write(fd, iov[i].iov_base, iov[i].iov_len); - - if (ret > 0) - { - written += ret; - } - } - - return written; - } - #endif - extern volatile sig_atomic_t interrupted; extern int showprogress; @@ -1502,15 +1480,19 @@ do_download(struct sftp_conn *conn, const char *remote_path, error("Can't set times on \"%s\": %s", local_path, strerror(errno)); } -#ifndef WIN32_FIXME -// PRAGMA:TODO + if (fsync_flag) { debug("syncing \"%s\"", local_path); +#ifdef WINDOWS + if(FlushFileBuffers(local_fd)) + error("Couldn't sync file \"%s\": %s", + local_path, strerror(GetLastError())); +#else if (fsync(local_fd) == -1) error("Couldn't sync file \"%s\": %s", local_path, strerror(errno)); - } #endif + } } close(local_fd); sshbuf_free(msg); @@ -1657,15 +1639,7 @@ do_upload(struct sftp_conn *conn, const char *local_path, TAILQ_INIT(&acks); - #if(0)//def WIN32_FIXME - - if ((local_fd = _open(local_path, O_RDONLY | O_BINARY, 0)) == -1) { - - #else - if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) { - - #endif error("Couldn't open local file \"%s\" for reading: %s", local_path, strerror(errno)); return(-1); diff --git a/sftp-common.c b/sftp-common.c index 277c14a..9a16f04 100644 --- a/sftp-common.c +++ b/sftp-common.c @@ -26,7 +26,7 @@ #include "includes.h" -#ifdef WIN32_FIXME +#ifdef WINDOWS void strmode(mode_t mode, char *p); void strmode_from_attrib(unsigned attrib, char *p); #endif @@ -225,36 +225,28 @@ ls_file(const char *name, const struct stat *st, int remote, int si_units) char sbuf[FMT_SCALED_STRSIZE]; time_t now; -#ifndef WIN32_FIXME - strmode(st->st_mode, mode); -#else - strmode(st->st_mode, mode); + strmode(st->st_mode, mode); +#ifdef WINDOWS strmode_from_attrib(remote, mode); #endif if (!remote) { user = user_from_uid(st->st_uid, 0); - #ifdef WIN32_FIXME snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); group = gbuf; - #endif } else { snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); user = ubuf; -#ifdef WIN32_FIXME - - snprintf(gbuf, sizeof gbuf, "%u", (u_int) st -> st_gid); - - group = gbuf; - +#ifdef WINDOWS + snprintf(gbuf, sizeof gbuf, "%u", (u_int) st -> st_gid); + group = gbuf; #else - if (!remote) { - group = group_from_gid(st->st_gid, 0); - } else { - snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); - group = gbuf; - } + if (!remote) { + group = group_from_gid(st->st_gid, 0); + } else { + snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); + group = gbuf; + } #endif - } if (ltime != NULL) { now = time(NULL); @@ -281,7 +273,7 @@ ls_file(const char *name, const struct stat *st, int remote, int si_units) return xstrdup(buf); } -#ifdef WIN32_FIXME +#ifdef WINDOWS #include #include @@ -297,7 +289,6 @@ strmode_from_attrib(unsigned attrib, char *p) *p = '-'; } - void strmode(mode_t mode, char *p) { diff --git a/sftp-glob.c b/sftp-glob.c index 5ba93f2..3863e87 100644 --- a/sftp-glob.c +++ b/sftp-glob.c @@ -22,7 +22,7 @@ # include #endif -#ifdef WIN32_VS +#ifdef WINDOWS #include "win32_dirent.h" #else #include @@ -37,7 +37,7 @@ #include "sftp-common.h" #include "sftp-client.h" -#ifdef WIN32_VS +#ifdef WINDOWS #include "win32_dirent.c" #endif diff --git a/sftp.c b/sftp.c index 806faf9..b2a02ae 100644 --- a/sftp.c +++ b/sftp.c @@ -73,10 +73,6 @@ typedef void EditLine; #define DEFAULT_COPY_BUFLEN 32768 /* Size of buffer for up/download */ #define DEFAULT_NUM_REQUESTS 64 /* # concurrent outstanding requests */ -#ifdef WIN32_FIXME - #define FAIL(X) if (X) goto fail -#endif - #ifdef WIN32_VS #include "win32_dirent.h" #endif @@ -290,17 +286,12 @@ help(void) static void local_do_shell(const char *args) { - #ifdef WIN32_FIXME - + #ifdef WINDOWS if (!*args) { args = (char *) getenv("ComSpec"); // get name of Windows cmd shell } system(args); // execute the shell or cmd given #else - - /* - * Original OpenSSH code. - */ int status; char *shell; pid_t pid; @@ -823,13 +814,12 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) tmp = path_strip(path, strip_path); m += strlen(tmp); free(tmp); -#ifndef WIN32_FIXME +#ifdef WINDOWS + width = ConSetScreenX(); +#else if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) width = ws.ws_col; #endif - - - columns = width / (m + 2); columns = MAX(columns, 1); colspace = width / columns; @@ -926,14 +916,13 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, return err; } - -#ifndef WIN32_FIXME +#ifdef WINDOWS + width = ConSetScreenX(); +#else if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) width = ws.ws_col; #endif - - if (!(lflag & LS_SHORT_VIEW)) { /* Count entries for sort and find longest filename */ for (i = 0; g.gl_pathv[i]; i++) @@ -2187,9 +2176,6 @@ interactive_loop(struct sftp_conn *conn, char *file1, char *file2) static void connect_to_server(char *path, char **args, int *in, int *out) { - /* - * Original OpenSSH code. - */ int c_in, c_out; #ifdef USE_PIPES @@ -2210,9 +2196,9 @@ connect_to_server(char *path, char **args, int *in, int *out) c_in = c_out = inout[1]; #endif /* USE_PIPES */ -#ifdef WIN32_FIXME +#ifdef WINDOWS { - int i; + int i = 0; char fullCmd[MAX_PATH] = { 0 }; char ioArg[1024] = { 0 }; PROCESS_INFORMATION pi = { 0 }; @@ -2220,8 +2206,7 @@ connect_to_server(char *path, char **args, int *in, int *out) debug3("Generating ssh-client command..."); strncat(fullCmd, path, MAX_PATH); - for (i = 1; args[i]; i++) - { + for (i = 1; args[i]; i++) { strncat(fullCmd, " ", MAX_PATH); strncat(fullCmd, args[i], MAX_PATH); } @@ -2332,40 +2317,29 @@ main(int argc, char **argv) size_t num_requests = DEFAULT_NUM_REQUESTS; long long limit_kbps = 0; -#ifdef WIN32_FIXME - +#ifdef WINDOWS /* * Initialize I/O wrappers. */ - w32posix_initialize(); - + w32posix_initialize(); setvbuf(stdout, NULL, _IONBF, 0); - #endif + #endif /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ sanitise_stdfd(); -#ifndef WIN32_FIXME setlocale(LC_CTYPE, ""); -#endif __progname = ssh_get_progname(argv[0]); memset(&args, '\0', sizeof(args)); args.list = NULL; addargs(&args, "%s", ssh_program); - #ifdef WIN32_FIXME - addargs(&args, "-oForwardX11=no"); - addargs(&args, "-oForwardAgent=no"); - addargs(&args, "-oPermitLocalCommand=no"); - addargs(&args, "-oClearAllForwardings=yes"); - - #else addargs(&args, "-oForwardX11 no"); addargs(&args, "-oForwardAgent no"); addargs(&args, "-oPermitLocalCommand no"); addargs(&args, "-oClearAllForwardings yes"); -#endif + ll = SYSLOG_LEVEL_INFO; infile = stdin; @@ -2393,16 +2367,7 @@ main(int argc, char **argv) addargs(&args, "-%c", ch); break; case 'P': - #ifdef WIN32_FIXME - - addargs(&args, "-oPort=%s", optarg); - - #else - - addargs(&args, "-oPort %s", optarg); - - #endif - + addargs(&args, "-oPort %s", optarg); break; case 'v': if (debug_level < 3) { @@ -2437,16 +2402,7 @@ main(int argc, char **argv) fatal("%s (%s).", strerror(errno), optarg); showprogress = 0; quiet = batchmode = 1; - #ifdef WIN32_FIXME - - addargs(&args, "-obatchmode=yes"); - - #else - addargs(&args, "-obatchmode yes"); - - #endif - break; case 'f': global_fflag = 1; @@ -2520,17 +2476,8 @@ main(int argc, char **argv) fprintf(stderr, "Missing hostname\n"); usage(); } - #ifdef WIN32_FIXME - - addargs(&args, "-oProtocol=%d", sshver); - - #else - addargs(&args, "-oProtocol %d", sshver); - #endif - - /* no subsystem if the server-spec contains a '/' */ if (sftp_server == NULL || strchr(sftp_server, '/') == NULL) addargs(&args, "-s");