Skip to content

Commit 1c258ec

Browse files
committed
Fix control-c working in sshd server
control-c is handled correctly by sshd server and passed to shell for it to process which usually means terminating the current program or programs.
1 parent 1178689 commit 1c258ec

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

channels.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,11 +2463,21 @@ channel_input_data(int type, u_int32_t seq, void *ctxt)
24632463
if ( c->client_tty )
24642464
telProcessNetwork ( data, data_len ); // run it by ANSI engine if it is the ssh client
24652465
else {
2466-
buffer_append(&c->output, data, data_len); // it is the sshd server, so pass it on
2467-
if ( c->isatty ) { // we echo the data if it is sshd server and pty interactive mode
2466+
if ( ( c->isatty) && (data_len ==1) && (data[0] == '\003') ) {
2467+
/* send control-c to the shell process */
2468+
if ( GenerateConsoleCtrlEvent ( CTRL_C_EVENT, 0 ) ) {
2469+
}
2470+
else {
2471+
debug3("GenerateConsoleCtrlEvent failed with %d\n",GetLastError());
2472+
}
2473+
}
2474+
else {
2475+
buffer_append(&c->output, data, data_len); // it is the sshd server, so pass it on
2476+
if ( c->isatty ) { // we echo the data if it is sshd server and pty interactive mode
24682477
buffer_append(&c->input, data, data_len);
24692478
if ( (data_len ==1) && (data[0] == '\b') )
24702479
buffer_append(&c->input, " \b", 2); // for backspace, we need to send space and another backspace for visual erase
2480+
}
24712481
}
24722482
}
24732483

session.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ do_exec_no_pty(Session *s, const char *command)
605605
*/
606606

607607
HANDLE wfdtocmd = -1;
608+
int retcode = -1;
608609
if ( (!s -> is_subsystem) && (s ->ttyfd != -1))
609610
{
610611
//FreeConsole();
@@ -622,8 +623,6 @@ do_exec_no_pty(Session *s, const char *command)
622623
//if (sockin[1] >= 0)
623624
// sfd_set_to_console(sockin[1]); // mark it as Console type
624625

625-
//allocate_standard_descriptor(STDIN_FILENO);
626-
//allocate_standard_descriptor(wfdtocmd); // put the std input handle in our global general handle table
627626
}
628627
else
629628
socketpair(sockin);
@@ -880,8 +879,9 @@ do_exec_no_pty(Session *s, const char *command)
880879

881880
if (!(s -> is_subsystem)) {
882881
// Send to the remote client ANSI/VT Sequence so that they send us CRLF in place of LF
882+
char *inittermseq = "\033[20h\033[?7h\0" ; // LFtoCRLF AUTOWRAPON
883883
Channel *c=channel_by_id ( s->chanid );
884-
buffer_append(&c->input, "\033[20h", 5);
884+
buffer_append(&c->input, inittermseq, strlen(inittermseq));
885885
channel_output_poll();
886886
}
887887

@@ -908,6 +908,7 @@ do_exec_no_pty(Session *s, const char *command)
908908
MultiByteToWideChar(CP_UTF8, 0, exec_command, -1, exec_command_w, MAX_PATH);
909909
DWORD dwStartupFlags = CREATE_SUSPENDED ; // 0
910910

911+
SetConsoleCtrlHandler(NULL, FALSE);
911912
b = CreateProcessAsUserW(hToken, NULL, exec_command_w, NULL, NULL, TRUE,
912913
/*CREATE_NEW_PROCESS_GROUP*/ dwStartupFlags, NULL, s -> pw -> pw_dir,
913914
&si, &pi);
@@ -969,6 +970,7 @@ do_exec_no_pty(Session *s, const char *command)
969970
close(sockerr[0]);
970971

971972
ResumeThread ( pi.hThread ); /* now let cmd shell main thread be active s we have closed all i/o file handle that cmd will use */
973+
SetConsoleCtrlHandler(NULL, TRUE);
972974

973975
/*
974976
* 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
@@ -2726,7 +2728,9 @@ session_pty_req(Session *s)
27262728
pty_setowner(s->pw, s->tty);
27272729

27282730
/* Set window size from the packet. */
2731+
#ifndef WIN32_FIXME
27292732
pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2733+
#endif
27302734

27312735
packet_check_eom();
27322736
session_proctitle(s);

0 commit comments

Comments
 (0)