Skip to content

Commit e7ad7dd

Browse files
authored
Merge pull request #722 from JacobBarthelmeh/sshd-windows
Fixes for SSHD Windows virtual terminal sequences
2 parents 5a06817 + 89a2456 commit e7ad7dd

1 file changed

Lines changed: 25 additions & 24 deletions

File tree

apps/wolfsshd/wolfsshd.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,7 +2545,7 @@ static int StartSSHD(int argc, char** argv)
25452545
#ifdef _WIN32
25462546
/* Used to setup a console and run command as a user.
25472547
* returns the process exit value */
2548-
static int SetupConsole(char* sysCmd)
2548+
static int SetupConsole(char* inCmd)
25492549
{
25502550
HANDLE sOut;
25512551
HANDLE sIn;
@@ -2558,8 +2558,9 @@ static int SetupConsole(char* sysCmd)
25582558
PROCESS_INFORMATION processInfo;
25592559
size_t sz = 0;
25602560
DWORD processState = 0;
2561+
PCSTR shellCmd = "c:\\windows\\system32\\cmd.exe";
25612562

2562-
if (sysCmd == NULL) {
2563+
if (inCmd == NULL) {
25632564
return -1;
25642565
}
25652566

@@ -2568,15 +2569,27 @@ static int SetupConsole(char* sysCmd)
25682569
cord.Y = 24;
25692570

25702571
sIn = GetStdHandle(STD_INPUT_HANDLE);
2571-
sOut = GetStdHandle(STD_OUTPUT_HANDLE);
2572-
if (CreatePseudoConsole(cord, sIn, sOut, 0, &pCon) != S_OK) {
2573-
wolfSSH_Log(WS_LOG_ERROR,
2574-
"[SSHD] Issue creating pseudo console");
2575-
ret = WS_FATAL_ERROR;
2572+
2573+
if (WSTRCMP(shellCmd, inCmd) != 0) {
2574+
/* if not opening a shell, pipe virtual terminal sequences to 'nul' */
2575+
if (CreatePseudoConsole(cord, sIn, INVALID_HANDLE_VALUE, 0, &pCon) != S_OK) {
2576+
ret = WS_FATAL_ERROR;
2577+
}
2578+
else {
2579+
CloseHandle(sIn);
2580+
}
25762581
}
2577-
else {
2578-
CloseHandle(sIn);
2579-
CloseHandle(sOut);
2582+
else
2583+
{
2584+
/* if opening a shell, pipe virtual terminal sequences back to calling process */
2585+
sOut = GetStdHandle(STD_OUTPUT_HANDLE);
2586+
if (CreatePseudoConsole(cord, sIn, sOut, 0, &pCon) != S_OK) {
2587+
ret = WS_FATAL_ERROR;
2588+
}
2589+
else {
2590+
CloseHandle(sIn);
2591+
CloseHandle(sOut);
2592+
}
25802593
}
25812594

25822595
/* setup startup extended info for pseudo terminal */
@@ -2594,17 +2607,13 @@ static int SetupConsole(char* sysCmd)
25942607
ext.lpAttributeList =
25952608
(PPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), 0, sz);
25962609
if (ext.lpAttributeList == NULL) {
2597-
wolfSSH_Log(WS_LOG_ERROR,
2598-
"[SSHD] Issue getting memory for attribute list");
25992610
ret = WS_FATAL_ERROR;
26002611
}
26012612
}
26022613

26032614
if (ret == WS_SUCCESS) {
26042615
if (InitializeProcThreadAttributeList(ext.lpAttributeList, 1, 0,
26052616
&sz) != TRUE) {
2606-
wolfSSH_Log(WS_LOG_ERROR,
2607-
"[SSHD] Issue initializing proc thread attribute");
26082617
ret = WS_FATAL_ERROR;
26092618
}
26102619
}
@@ -2613,15 +2622,13 @@ static int SetupConsole(char* sysCmd)
26132622
if (UpdateProcThreadAttribute(ext.lpAttributeList, 0,
26142623
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
26152624
pCon, sizeof(HPCON), NULL, NULL) != TRUE) {
2616-
wolfSSH_Log(WS_LOG_ERROR,
2617-
"[SSHD] Issue updating proc thread attribute");
26182625
ret = WS_FATAL_ERROR;
26192626
}
26202627
}
26212628
}
26222629

26232630
if (ret == WS_SUCCESS) {
2624-
cmdSz = WSTRLEN(sysCmd) + 1; /* +1 for terminator */
2631+
cmdSz = WSTRLEN(inCmd) + 1; /* +1 for terminator */
26252632
cmd = (PWSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(wchar_t) * cmdSz);
26262633
if (cmd == NULL) {
26272634
ret = WS_MEMORY_E;
@@ -2630,18 +2637,15 @@ static int SetupConsole(char* sysCmd)
26302637
size_t numConv = 0;
26312638

26322639
WMEMSET(cmd, 0, sizeof(wchar_t) * cmdSz);
2633-
mbstowcs_s(&numConv, cmd, cmdSz, sysCmd, strlen(sysCmd));
2640+
mbstowcs_s(&numConv, cmd, cmdSz, inCmd, strlen(inCmd));
26342641
}
26352642
}
26362643

2637-
26382644
ZeroMemory(&processInfo, sizeof(processInfo));
26392645
if (ret == WS_SUCCESS) {
26402646
if (CreateProcessW(NULL, cmd,
26412647
NULL, NULL, FALSE, EXTENDED_STARTUPINFO_PRESENT, NULL, NULL,
26422648
&ext.StartupInfo, &processInfo) != TRUE) {
2643-
wolfSSH_Log(WS_LOG_ERROR,
2644-
"[SSHD] Issue creating process, Windows error %d", GetLastError());
26452649
return WS_FATAL_ERROR;
26462650
}
26472651
else {
@@ -2661,9 +2665,6 @@ static int SetupConsole(char* sysCmd)
26612665
if (GetExitCodeProcess(processInfo.hProcess, &processState)
26622666
== TRUE) {
26632667
if (processState != STILL_ACTIVE) {
2664-
wolfSSH_Log(WS_LOG_INFO,
2665-
"[SSHD] Process has exited, exit state = %d, "
2666-
"close down SSH connection", processState);
26672668
Sleep(100); /* give the stdout/stderr of process a
26682669
* little time to write to pipe */
26692670
if (PeekNamedPipe(GetStdHandle(STD_OUTPUT_HANDLE), NULL, 0, NULL, &ava, NULL)

0 commit comments

Comments
 (0)