@@ -494,6 +494,46 @@ do_authenticated1(Authctxt *authctxt)
494
494
#ifndef WIN32_FIXME
495
495
#define USE_PIPES 1
496
496
#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
497
537
/*
498
538
* This is called to fork and execute a command when we have no tty. This
499
539
* 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)
545
585
char * exec_command ;
546
586
char * laddr ;
547
587
char buf [256 ];
588
+ int prot_scr_width = 80 ;
589
+ int prot_scr_height = 25 ;
548
590
549
591
if (!command )
550
592
{
@@ -565,10 +607,14 @@ do_exec_no_pty(Session *s, const char *command)
565
607
HANDLE wfdtocmd = -1 ;
566
608
if ( (!s -> is_subsystem ) && (s -> ttyfd != -1 ))
567
609
{
610
+ //FreeConsole();
611
+ //AllocConsole();
612
+ MakeNewConsole ();
613
+
568
614
wfdtocmd = GetStdHandle (STD_INPUT_HANDLE ) ;
569
615
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
572
618
573
619
//allocate_standard_descriptor(STDIN_FILENO);
574
620
//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)
605
651
si .lpTitle = NULL ; /* NULL means use exe name as title */
606
652
si .dwX = 0 ;
607
653
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 ;
612
658
si .dwFillAttribute = 0 ;
613
659
si .dwFlags = STARTF_USESTDHANDLES | STARTF_USESIZE | STARTF_USECOUNTCHARS ; // | STARTF_USESHOWWINDOW ;
614
660
si .wShowWindow = 0 ; // FALSE ;
@@ -619,15 +665,16 @@ do_exec_no_pty(Session *s, const char *command)
619
665
si .hStdInput = GetStdHandle (STD_INPUT_HANDLE ) ; // shell tty interactive session gets a console input for Win32
620
666
si .hStdOutput = (HANDLE ) sfd_to_handle (sockout [0 ]);
621
667
si .hStdError = (HANDLE ) sfd_to_handle (sockerr [0 ]);
668
+ si .lpDesktop = NULL ; //winstadtname_w ;
622
669
}
623
670
else {
624
671
si .hStdInput = (HANDLE ) sfd_to_handle (sockin [0 ]);
625
672
si .hStdOutput = (HANDLE ) sfd_to_handle (sockout [0 ]);
626
673
si .hStdError = (HANDLE ) sfd_to_handle (sockerr [0 ]);
674
+ si .lpDesktop = NULL ; //L"winsta0\\default";
627
675
}
628
676
//si.wShowWindow = SW_HIDE;
629
677
//si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
630
- si .lpDesktop = L"winsta0\\default" ;
631
678
632
679
633
680
SetEnvironmentVariable ("USER" , s -> pw -> pw_name );
@@ -853,9 +900,10 @@ do_exec_no_pty(Session *s, const char *command)
853
900
wchar_t exec_command_w [MAX_PATH ];
854
901
855
902
MultiByteToWideChar (CP_UTF8 , 0 , exec_command , -1 , exec_command_w , MAX_PATH );
903
+ DWORD dwStartupFlags = CREATE_SUSPENDED ; // 0
856
904
857
905
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 ,
859
907
& si , & pi );
860
908
/*
861
909
* If CreateProcessAsUser() fails we will try CreateProcess()
@@ -865,7 +913,7 @@ do_exec_no_pty(Session *s, const char *command)
865
913
if ((!b ) && (strcmp (name , s -> pw -> pw_name ) == 0 ))
866
914
{
867
915
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 ,
869
917
& si , & pi );
870
918
}
871
919
@@ -887,14 +935,6 @@ do_exec_no_pty(Session *s, const char *command)
887
935
888
936
s -> authctxt -> currentToken_ = hToken ;
889
937
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
-
898
938
/*
899
939
* Log the process handle (fake it as the pid) for termination lookups
900
940
*/
@@ -922,6 +962,16 @@ do_exec_no_pty(Session *s, const char *command)
922
962
close (sockout [0 ]);
923
963
close (sockerr [0 ]);
924
964
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
+
925
975
/*
926
976
* Clear loginmsg, since it's the child's responsibility to display
927
977
* it to the user, otherwise multiple sessions may accumulate
0 commit comments