Skip to content

Commit a3d0255

Browse files
committed
Fix Console Insertion issue of multiple sshd interactive sessions not working
We create console for each sshd session from CONIN$ and CONOUT$ before starting shell cmd.exe
1 parent 339912c commit a3d0255

File tree

1 file changed

+67
-17
lines changed

1 file changed

+67
-17
lines changed

session.c

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,46 @@ do_authenticated1(Authctxt *authctxt)
494494
#ifndef WIN32_FIXME
495495
#define USE_PIPES 1
496496
#endif
497+
498+
#ifdef WIN32_FIXME
499+
HANDLE hConIn = NULL;
500+
HANDLE hConOut = NULL;
501+
HANDLE hConErr = NULL;
502+
503+
BOOL MakeNewConsole(void)
504+
{
505+
BOOL bRet = TRUE;
506+
507+
if (!(bRet = FreeConsole())) return bRet;
508+
if (!(bRet = AllocConsole())) return bRet;
509+
HANDLE hTemp;
510+
511+
hTemp = CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
512+
if (INVALID_HANDLE_VALUE != hTemp)
513+
{
514+
DuplicateHandle(GetCurrentProcess(),hTemp,GetCurrentProcess(),&hConIn, 0,TRUE,DUPLICATE_SAME_ACCESS);
515+
CloseHandle(hTemp);
516+
} else
517+
return FALSE;
518+
519+
hTemp = CreateFile("CONOUT$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
520+
if (INVALID_HANDLE_VALUE != hTemp)
521+
{
522+
DuplicateHandle(GetCurrentProcess(),hTemp,GetCurrentProcess(),&hConOut, 0,TRUE,DUPLICATE_SAME_ACCESS);
523+
DuplicateHandle(GetCurrentProcess(),hTemp,GetCurrentProcess(),&hConErr, 0,TRUE,DUPLICATE_SAME_ACCESS);
524+
CloseHandle(hTemp);
525+
526+
} else
527+
return FALSE;
528+
529+
SetStdHandle(STD_INPUT_HANDLE,hConIn);
530+
SetStdHandle(STD_OUTPUT_HANDLE,hConOut);
531+
SetStdHandle(STD_ERROR_HANDLE,hConErr);
532+
533+
return TRUE;
534+
535+
}
536+
#endif
497537
/*
498538
* This is called to fork and execute a command when we have no tty. This
499539
* will call do_child from the child, and server_loop from the parent after
@@ -545,6 +585,8 @@ do_exec_no_pty(Session *s, const char *command)
545585
char *exec_command;
546586
char *laddr;
547587
char buf[256];
588+
int prot_scr_width = 80;
589+
int prot_scr_height = 25;
548590

549591
if (!command)
550592
{
@@ -565,10 +607,14 @@ do_exec_no_pty(Session *s, const char *command)
565607
HANDLE wfdtocmd = -1;
566608
if ( (!s -> is_subsystem) && (s ->ttyfd != -1))
567609
{
610+
//FreeConsole();
611+
//AllocConsole();
612+
MakeNewConsole();
613+
568614
wfdtocmd = GetStdHandle (STD_INPUT_HANDLE) ;
569615
sockin[1] = allocate_sfd((int)wfdtocmd); // put the std input handle in our global general handle table
570-
if (sockin[1] >= 0)
571-
sfd_set_to_console(sockin[1]); // mark it as Console type
616+
//if (sockin[1] >= 0)
617+
// sfd_set_to_console(sockin[1]); // mark it as Console type
572618

573619
//allocate_standard_descriptor(STDIN_FILENO);
574620
//allocate_standard_descriptor(wfdtocmd); // put the std input handle in our global general handle table
@@ -605,10 +651,10 @@ do_exec_no_pty(Session *s, const char *command)
605651
si.lpTitle = NULL; /* NULL means use exe name as title */
606652
si.dwX = 0;
607653
si.dwY = 0;
608-
si.dwXSize = 80;
609-
si.dwYSize = 25;
610-
si.dwXCountChars = 80;
611-
si.dwYCountChars = 25;
654+
si.dwXSize = prot_scr_width;
655+
si.dwYSize = prot_scr_height;
656+
si.dwXCountChars = prot_scr_width;
657+
si.dwYCountChars = prot_scr_height;
612658
si.dwFillAttribute = 0;
613659
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESIZE | STARTF_USECOUNTCHARS; // | STARTF_USESHOWWINDOW ;
614660
si.wShowWindow = 0; // FALSE ;
@@ -619,15 +665,16 @@ do_exec_no_pty(Session *s, const char *command)
619665
si.hStdInput = GetStdHandle (STD_INPUT_HANDLE) ; // shell tty interactive session gets a console input for Win32
620666
si.hStdOutput = (HANDLE) sfd_to_handle(sockout[0]);
621667
si.hStdError = (HANDLE) sfd_to_handle(sockerr[0]);
668+
si.lpDesktop = NULL ; //winstadtname_w ;
622669
}
623670
else {
624671
si.hStdInput = (HANDLE) sfd_to_handle(sockin[0]);
625672
si.hStdOutput = (HANDLE) sfd_to_handle(sockout[0]);
626673
si.hStdError = (HANDLE) sfd_to_handle(sockerr[0]);
674+
si.lpDesktop = NULL; //L"winsta0\\default";
627675
}
628676
//si.wShowWindow = SW_HIDE;
629677
//si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
630-
si.lpDesktop = L"winsta0\\default";
631678

632679

633680
SetEnvironmentVariable("USER", s->pw->pw_name);
@@ -853,9 +900,10 @@ do_exec_no_pty(Session *s, const char *command)
853900
wchar_t exec_command_w[MAX_PATH];
854901

855902
MultiByteToWideChar(CP_UTF8, 0, exec_command, -1, exec_command_w, MAX_PATH);
903+
DWORD dwStartupFlags = CREATE_SUSPENDED ; // 0
856904

857905
b = CreateProcessAsUserW(hToken, NULL, exec_command_w, NULL, NULL, TRUE,
858-
CREATE_NEW_PROCESS_GROUP, NULL, s -> pw -> pw_dir,
906+
/*CREATE_NEW_PROCESS_GROUP*/ dwStartupFlags, NULL, s -> pw -> pw_dir,
859907
&si, &pi);
860908
/*
861909
* If CreateProcessAsUser() fails we will try CreateProcess()
@@ -865,7 +913,7 @@ do_exec_no_pty(Session *s, const char *command)
865913
if ((!b) && (strcmp(name, s -> pw -> pw_name) == 0))
866914
{
867915
b = CreateProcessW(NULL, exec_command_w, NULL, NULL, TRUE,
868-
CREATE_NEW_PROCESS_GROUP, NULL, s -> pw -> pw_dir,
916+
/*CREATE_NEW_PROCESS_GROUP*/ dwStartupFlags, NULL, s -> pw -> pw_dir,
869917
&si, &pi);
870918
}
871919

@@ -887,14 +935,6 @@ do_exec_no_pty(Session *s, const char *command)
887935

888936
s -> authctxt -> currentToken_ = hToken;
889937

890-
/*
891-
* Close child thread and process handles so it can go away
892-
*/
893-
894-
CloseHandle(pi.hThread);
895-
896-
// CloseHandle(pi.hProcess);
897-
898938
/*
899939
* Log the process handle (fake it as the pid) for termination lookups
900940
*/
@@ -922,6 +962,16 @@ do_exec_no_pty(Session *s, const char *command)
922962
close(sockout[0]);
923963
close(sockerr[0]);
924964

965+
ResumeThread ( pi.hThread ); /* now let cmd shell main thread be active s we have closed all i/o file handle that cmd will use */
966+
967+
/*
968+
* Close child thread handles as we do not need it. Process handle we keep so that we can know if it has died o not
969+
*/
970+
971+
CloseHandle(pi.hThread);
972+
973+
// CloseHandle(pi.hProcess);
974+
925975
/*
926976
* Clear loginmsg, since it's the child's responsibility to display
927977
* it to the user, otherwise multiple sessions may accumulate

0 commit comments

Comments
 (0)