From 25a0414378de9edecdc973eca21e5bc72535997e Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Fri, 12 Jul 2024 00:43:13 -0600 Subject: [PATCH 1/2] handling virtual terminal sequences with exec command --- apps/wolfsshd/wolfsshd.c | 42 +++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/apps/wolfsshd/wolfsshd.c b/apps/wolfsshd/wolfsshd.c index 3fbcbf736..fbebdf759 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -2477,7 +2477,7 @@ static int StartSSHD(int argc, char** argv) #ifdef _WIN32 /* Used to setup a console and run command as a user. * returns the process exit value */ -static int SetupConsole(char* sysCmd) +static int SetupConsole(char* inCmd) { HANDLE sOut; HANDLE sIn; @@ -2490,8 +2490,9 @@ static int SetupConsole(char* sysCmd) PROCESS_INFORMATION processInfo; size_t sz = 0; DWORD processState = 0; + PCSTR shellCmd = "c:\\windows\\system32\\cmd.exe"; - if (sysCmd == NULL) { + if (inCmd == NULL) { return -1; } @@ -2500,15 +2501,31 @@ static int SetupConsole(char* sysCmd) cord.Y = 24; sIn = GetStdHandle(STD_INPUT_HANDLE); - sOut = GetStdHandle(STD_OUTPUT_HANDLE); - if (CreatePseudoConsole(cord, sIn, sOut, 0, &pCon) != S_OK) { - wolfSSH_Log(WS_LOG_ERROR, - "[SSHD] Issue creating pseudo console"); - ret = WS_FATAL_ERROR; + + if (WSTRCMP(shellCmd, inCmd) != 0) { + /* if not opening a shell, pipe virtual terminal sequences to 'nul' */ + if (CreatePseudoConsole(cord, sIn, INVALID_HANDLE_VALUE, 0, &pCon) != S_OK) { + wolfSSH_Log(WS_LOG_ERROR, + "[SSHD] Issue creating pseudo console"); + ret = WS_FATAL_ERROR; + } + else { + CloseHandle(sIn); + } } - else { - CloseHandle(sIn); - CloseHandle(sOut); + else + { + /* if opening a shell, pipe virtual terminal sequences back to calling process */ + sOut = GetStdHandle(STD_OUTPUT_HANDLE); + if (CreatePseudoConsole(cord, sIn, sOut, 0, &pCon) != S_OK) { + wolfSSH_Log(WS_LOG_ERROR, + "[SSHD] Issue creating pseudo console"); + ret = WS_FATAL_ERROR; + } + else { + CloseHandle(sIn); + CloseHandle(sOut); + } } /* setup startup extended info for pseudo terminal */ @@ -2553,7 +2570,7 @@ static int SetupConsole(char* sysCmd) } if (ret == WS_SUCCESS) { - cmdSz = WSTRLEN(sysCmd) + 1; /* +1 for terminator */ + cmdSz = WSTRLEN(inCmd) + 1; /* +1 for terminator */ cmd = (PWSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(wchar_t) * cmdSz); if (cmd == NULL) { ret = WS_MEMORY_E; @@ -2562,11 +2579,10 @@ static int SetupConsole(char* sysCmd) size_t numConv = 0; WMEMSET(cmd, 0, sizeof(wchar_t) * cmdSz); - mbstowcs_s(&numConv, cmd, cmdSz, sysCmd, strlen(sysCmd)); + mbstowcs_s(&numConv, cmd, cmdSz, inCmd, strlen(inCmd)); } } - ZeroMemory(&processInfo, sizeof(processInfo)); if (ret == WS_SUCCESS) { if (CreateProcessW(NULL, cmd, From 89a24568a699740f24539673574d3563d7b5c2b5 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Fri, 12 Jul 2024 01:02:33 -0600 Subject: [PATCH 2/2] remove extra debug print outs --- apps/wolfsshd/wolfsshd.c | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/apps/wolfsshd/wolfsshd.c b/apps/wolfsshd/wolfsshd.c index fbebdf759..16c940e77 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -2505,8 +2505,6 @@ static int SetupConsole(char* inCmd) if (WSTRCMP(shellCmd, inCmd) != 0) { /* if not opening a shell, pipe virtual terminal sequences to 'nul' */ if (CreatePseudoConsole(cord, sIn, INVALID_HANDLE_VALUE, 0, &pCon) != S_OK) { - wolfSSH_Log(WS_LOG_ERROR, - "[SSHD] Issue creating pseudo console"); ret = WS_FATAL_ERROR; } else { @@ -2518,8 +2516,6 @@ static int SetupConsole(char* inCmd) /* if opening a shell, pipe virtual terminal sequences back to calling process */ sOut = GetStdHandle(STD_OUTPUT_HANDLE); if (CreatePseudoConsole(cord, sIn, sOut, 0, &pCon) != S_OK) { - wolfSSH_Log(WS_LOG_ERROR, - "[SSHD] Issue creating pseudo console"); ret = WS_FATAL_ERROR; } else { @@ -2543,8 +2539,6 @@ static int SetupConsole(char* inCmd) ext.lpAttributeList = (PPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), 0, sz); if (ext.lpAttributeList == NULL) { - wolfSSH_Log(WS_LOG_ERROR, - "[SSHD] Issue getting memory for attribute list"); ret = WS_FATAL_ERROR; } } @@ -2552,8 +2546,6 @@ static int SetupConsole(char* inCmd) if (ret == WS_SUCCESS) { if (InitializeProcThreadAttributeList(ext.lpAttributeList, 1, 0, &sz) != TRUE) { - wolfSSH_Log(WS_LOG_ERROR, - "[SSHD] Issue initializing proc thread attribute"); ret = WS_FATAL_ERROR; } } @@ -2562,8 +2554,6 @@ static int SetupConsole(char* inCmd) if (UpdateProcThreadAttribute(ext.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, pCon, sizeof(HPCON), NULL, NULL) != TRUE) { - wolfSSH_Log(WS_LOG_ERROR, - "[SSHD] Issue updating proc thread attribute"); ret = WS_FATAL_ERROR; } } @@ -2588,8 +2578,6 @@ static int SetupConsole(char* inCmd) if (CreateProcessW(NULL, cmd, NULL, NULL, FALSE, EXTENDED_STARTUPINFO_PRESENT, NULL, NULL, &ext.StartupInfo, &processInfo) != TRUE) { - wolfSSH_Log(WS_LOG_ERROR, - "[SSHD] Issue creating process, Windows error %d", GetLastError()); return WS_FATAL_ERROR; } else { @@ -2609,9 +2597,6 @@ static int SetupConsole(char* inCmd) if (GetExitCodeProcess(processInfo.hProcess, &processState) == TRUE) { if (processState != STILL_ACTIVE) { - wolfSSH_Log(WS_LOG_INFO, - "[SSHD] Process has exited, exit state = %d, " - "close down SSH connection", processState); Sleep(100); /* give the stdout/stderr of process a * little time to write to pipe */ if (PeekNamedPipe(GetStdHandle(STD_OUTPUT_HANDLE), NULL, 0, NULL, &ava, NULL)