Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for SSHD Windows virtual terminal sequences #722

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions apps/wolfsshd/wolfsshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -2500,15 +2501,27 @@ 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) {
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) {
ret = WS_FATAL_ERROR;
}
else {
CloseHandle(sIn);
CloseHandle(sOut);
}
}

/* setup startup extended info for pseudo terminal */
Expand All @@ -2526,17 +2539,13 @@ static int SetupConsole(char* sysCmd)
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;
}
}

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;
}
}
Expand All @@ -2545,15 +2554,13 @@ static int SetupConsole(char* sysCmd)
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;
}
}
}

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;
Expand All @@ -2562,18 +2569,15 @@ 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,
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 {
Expand All @@ -2593,9 +2597,6 @@ static int SetupConsole(char* sysCmd)
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)
Expand Down
Loading