2018-09-30 06:16:38 +00:00
|
|
|
/*
|
|
|
|
* SSH main session channel handling.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
#include "ssh.h"
|
|
|
|
#include "sshppl.h"
|
|
|
|
#include "sshchan.h"
|
|
|
|
|
|
|
|
static void mainchan_free(Channel *chan);
|
|
|
|
static void mainchan_open_confirmation(Channel *chan);
|
|
|
|
static void mainchan_open_failure(Channel *chan, const char *errtext);
|
|
|
|
static int mainchan_send(Channel *chan, int is_stderr, const void *, int);
|
|
|
|
static void mainchan_send_eof(Channel *chan);
|
|
|
|
static void mainchan_set_input_wanted(Channel *chan, int wanted);
|
|
|
|
static char *mainchan_log_close_msg(Channel *chan);
|
|
|
|
static int mainchan_rcvd_exit_status(Channel *chan, int status);
|
|
|
|
static int mainchan_rcvd_exit_signal(
|
|
|
|
Channel *chan, ptrlen signame, int core_dumped, ptrlen msg);
|
|
|
|
static int mainchan_rcvd_exit_signal_numeric(
|
|
|
|
Channel *chan, int signum, int core_dumped, ptrlen msg);
|
|
|
|
static void mainchan_request_response(Channel *chan, int success);
|
|
|
|
|
|
|
|
static const struct ChannelVtable mainchan_channelvt = {
|
|
|
|
mainchan_free,
|
|
|
|
mainchan_open_confirmation,
|
|
|
|
mainchan_open_failure,
|
|
|
|
mainchan_send,
|
|
|
|
mainchan_send_eof,
|
|
|
|
mainchan_set_input_wanted,
|
|
|
|
mainchan_log_close_msg,
|
Allow channels not to close immediately after two EOFs.
Some kinds of channel, even after they've sent EOF in both directions,
still have something to do before they initiate the CLOSE mechanism
and wind up the channel completely. For example, a session channel
with a subprocess running inside it will want to be sure to send the
"exit-status" or "exit-signal" notification, even if that happens
after bidirectional EOF of the data channels.
Previously, the SSH-2 connection layer had the standard policy that
once EOF had been both sent and received, it would start the final
close procedure. There's a method chan_want_close() by which a Channel
could vary this policy in one direction, by indicating that it wanted
the close procedure to commence after EOF was sent in only one
direction. Its parameters are a pair of booleans saying whether EOF
has been sent, and whether it's been received.
Now chan_want_close can vary the policy in the other direction as
well: if it returns FALSE even when _both_ parameters are true, the
connection layer will honour that, and not send CHANNEL_CLOSE. If it
does that, the Channel is responsible for indicating when it _does_
want close later, by calling sshfwd_initiate_close.
2018-10-18 17:02:59 +00:00
|
|
|
chan_default_want_close,
|
2018-09-30 06:16:38 +00:00
|
|
|
mainchan_rcvd_exit_status,
|
|
|
|
mainchan_rcvd_exit_signal,
|
|
|
|
mainchan_rcvd_exit_signal_numeric,
|
2018-10-20 20:48:49 +00:00
|
|
|
chan_no_run_shell,
|
|
|
|
chan_no_run_command,
|
|
|
|
chan_no_run_subsystem,
|
|
|
|
chan_no_enable_x11_forwarding,
|
|
|
|
chan_no_enable_agent_forwarding,
|
|
|
|
chan_no_allocate_pty,
|
|
|
|
chan_no_set_env,
|
|
|
|
chan_no_send_break,
|
|
|
|
chan_no_send_signal,
|
|
|
|
chan_no_change_window_size,
|
2018-09-30 06:16:38 +00:00
|
|
|
mainchan_request_response,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum MainChanType {
|
|
|
|
MAINCHAN_SESSION, MAINCHAN_DIRECT_TCPIP
|
|
|
|
} MainChanType;
|
|
|
|
|
2018-11-01 18:17:41 +00:00
|
|
|
struct mainchan {
|
2018-09-30 06:16:38 +00:00
|
|
|
SshChannel *sc;
|
|
|
|
Conf *conf;
|
|
|
|
PacketProtocolLayer *ppl;
|
|
|
|
ConnectionLayer *cl;
|
|
|
|
|
|
|
|
MainChanType type;
|
|
|
|
int is_simple;
|
|
|
|
|
|
|
|
int req_x11, req_agent, req_pty, req_cmd_primary, req_cmd_fallback;
|
|
|
|
int n_req_env, n_env_replies, n_env_fails;
|
|
|
|
int eof_pending, eof_sent, got_pty, ready;
|
|
|
|
|
|
|
|
int term_width, term_height;
|
|
|
|
|
|
|
|
Channel chan;
|
2018-11-01 18:17:41 +00:00
|
|
|
};
|
2018-09-30 06:16:38 +00:00
|
|
|
|
|
|
|
mainchan *mainchan_new(
|
|
|
|
PacketProtocolLayer *ppl, ConnectionLayer *cl, Conf *conf,
|
|
|
|
int term_width, int term_height, int is_simple, SshChannel **sc_out)
|
|
|
|
{
|
|
|
|
mainchan *mc;
|
|
|
|
|
|
|
|
if (conf_get_int(conf, CONF_ssh_no_shell))
|
|
|
|
return NULL; /* no main channel at all */
|
|
|
|
|
|
|
|
mc = snew(mainchan);
|
|
|
|
memset(mc, 0, sizeof(mainchan));
|
|
|
|
mc->ppl = ppl;
|
|
|
|
mc->cl = cl;
|
|
|
|
mc->conf = conf_copy(conf);
|
|
|
|
mc->term_width = term_width;
|
|
|
|
mc->term_height = term_height;
|
|
|
|
mc->is_simple = is_simple;
|
|
|
|
|
|
|
|
mc->sc = NULL;
|
|
|
|
mc->chan.vt = &mainchan_channelvt;
|
|
|
|
mc->chan.initial_fixed_window_size = 0;
|
|
|
|
|
|
|
|
if (*conf_get_str(mc->conf, CONF_ssh_nc_host)) {
|
|
|
|
const char *host = conf_get_str(mc->conf, CONF_ssh_nc_host);
|
|
|
|
int port = conf_get_int(mc->conf, CONF_ssh_nc_port);
|
|
|
|
|
2018-10-18 19:06:42 +00:00
|
|
|
mc->sc = ssh_lportfwd_open(cl, host, port, "main channel",
|
|
|
|
NULL, &mc->chan);
|
2018-09-30 06:16:38 +00:00
|
|
|
mc->type = MAINCHAN_DIRECT_TCPIP;
|
|
|
|
} else {
|
|
|
|
mc->sc = ssh_session_open(cl, &mc->chan);
|
|
|
|
mc->type = MAINCHAN_SESSION;
|
|
|
|
}
|
|
|
|
|
2018-09-30 10:22:01 +00:00
|
|
|
if (sc_out) *sc_out = mc->sc;
|
2018-09-30 06:16:38 +00:00
|
|
|
return mc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_free(Channel *chan)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
conf_free(mc->conf);
|
|
|
|
sfree(mc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_try_fallback_command(mainchan *mc);
|
|
|
|
static void mainchan_ready(mainchan *mc);
|
|
|
|
|
|
|
|
static void mainchan_open_confirmation(Channel *chan)
|
|
|
|
{
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
|
|
|
|
seat_update_specials_menu(mc->ppl->seat);
|
|
|
|
ppl_logevent(("Opened main channel"));
|
|
|
|
|
|
|
|
if (mc->is_simple)
|
|
|
|
sshfwd_hint_channel_is_simple(mc->sc);
|
|
|
|
|
|
|
|
if (mc->type == MAINCHAN_SESSION) {
|
|
|
|
/*
|
|
|
|
* Send the CHANNEL_REQUESTS for the main session channel.
|
|
|
|
*/
|
|
|
|
char *key, *val, *cmd;
|
|
|
|
struct X11Display *x11disp;
|
|
|
|
struct X11FakeAuth *x11auth;
|
2018-10-29 19:50:29 +00:00
|
|
|
int retry_cmd_now = false;
|
2018-09-30 06:16:38 +00:00
|
|
|
|
|
|
|
if (conf_get_int(mc->conf, CONF_x11_forward)) {;
|
|
|
|
char *x11_setup_err;
|
|
|
|
if ((x11disp = x11_setup_display(
|
|
|
|
conf_get_str(mc->conf, CONF_x11_display),
|
|
|
|
mc->conf, &x11_setup_err)) == NULL) {
|
|
|
|
ppl_logevent(("X11 forwarding not enabled: unable to"
|
|
|
|
" initialise X display: %s", x11_setup_err));
|
|
|
|
sfree(x11_setup_err);
|
|
|
|
} else {
|
|
|
|
x11auth = ssh_add_x11_display(
|
|
|
|
mc->cl, conf_get_int(mc->conf, CONF_x11_auth), x11disp);
|
|
|
|
|
|
|
|
sshfwd_request_x11_forwarding(
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->sc, true, x11auth->protoname, x11auth->datastring,
|
|
|
|
x11disp->screennum, false);
|
|
|
|
mc->req_x11 = true;
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssh_agent_forwarding_permitted(mc->cl)) {
|
2018-10-29 19:50:29 +00:00
|
|
|
sshfwd_request_agent_forwarding(mc->sc, true);
|
|
|
|
mc->req_agent = true;
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!conf_get_int(mc->conf, CONF_nopty)) {
|
|
|
|
sshfwd_request_pty(
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->sc, true, mc->conf, mc->term_width, mc->term_height);
|
|
|
|
mc->req_pty = true;
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (val = conf_get_str_strs(mc->conf, CONF_environmt, NULL, &key);
|
|
|
|
val != NULL;
|
|
|
|
val = conf_get_str_strs(mc->conf, CONF_environmt, key, &key)) {
|
2018-10-29 19:50:29 +00:00
|
|
|
sshfwd_send_env_var(mc->sc, true, key, val);
|
2018-09-30 06:16:38 +00:00
|
|
|
mc->n_req_env++;
|
|
|
|
}
|
|
|
|
if (mc->n_req_env)
|
|
|
|
ppl_logevent(("Sent %d environment variables", mc->n_req_env));
|
|
|
|
|
|
|
|
cmd = conf_get_str(mc->conf, CONF_remote_cmd);
|
|
|
|
if (conf_get_int(mc->conf, CONF_ssh_subsys)) {
|
2018-10-29 19:50:29 +00:00
|
|
|
retry_cmd_now = !sshfwd_start_subsystem(mc->sc, true, cmd);
|
2018-09-30 06:16:38 +00:00
|
|
|
} else if (*cmd) {
|
2018-10-29 19:50:29 +00:00
|
|
|
sshfwd_start_command(mc->sc, true, cmd);
|
2018-09-30 06:16:38 +00:00
|
|
|
} else {
|
2018-10-29 19:50:29 +00:00
|
|
|
sshfwd_start_shell(mc->sc, true);
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (retry_cmd_now)
|
|
|
|
mainchan_try_fallback_command(mc);
|
|
|
|
else
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->req_cmd_primary = true;
|
2018-09-30 06:16:38 +00:00
|
|
|
|
|
|
|
} else {
|
2018-10-29 19:50:29 +00:00
|
|
|
ssh_set_ldisc_option(mc->cl, LD_ECHO, true);
|
|
|
|
ssh_set_ldisc_option(mc->cl, LD_EDIT, true);
|
2018-09-30 06:16:38 +00:00
|
|
|
mainchan_ready(mc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_try_fallback_command(mainchan *mc)
|
|
|
|
{
|
|
|
|
const char *cmd = conf_get_str(mc->conf, CONF_remote_cmd2);
|
|
|
|
if (conf_get_int(mc->conf, CONF_ssh_subsys2)) {
|
2018-10-29 19:50:29 +00:00
|
|
|
sshfwd_start_subsystem(mc->sc, true, cmd);
|
2018-09-30 06:16:38 +00:00
|
|
|
} else {
|
2018-10-29 19:50:29 +00:00
|
|
|
sshfwd_start_command(mc->sc, true, cmd);
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->req_cmd_fallback = true;
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_request_response(Channel *chan, int success)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
|
|
|
|
if (mc->req_x11) {
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->req_x11 = false;
|
2018-09-30 06:16:38 +00:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
ppl_logevent(("X11 forwarding enabled"));
|
|
|
|
ssh_enable_x_fwd(mc->cl);
|
|
|
|
} else {
|
|
|
|
ppl_logevent(("X11 forwarding refused"));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->req_agent) {
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->req_agent = false;
|
2018-09-30 06:16:38 +00:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
ppl_logevent(("Agent forwarding enabled"));
|
|
|
|
ssh_enable_agent_fwd(mc->cl);
|
|
|
|
} else {
|
|
|
|
ppl_logevent(("Agent forwarding refused"));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->req_pty) {
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->req_pty = false;
|
2018-09-30 06:16:38 +00:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
ppl_logevent(("Allocated pty"));
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->got_pty = true;
|
2018-09-30 06:16:38 +00:00
|
|
|
} else {
|
|
|
|
ppl_logevent(("Server refused to allocate pty"));
|
|
|
|
ppl_printf(("Server refused to allocate pty\r\n"));
|
2018-10-29 19:50:29 +00:00
|
|
|
ssh_set_ldisc_option(mc->cl, LD_ECHO, true);
|
|
|
|
ssh_set_ldisc_option(mc->cl, LD_EDIT, true);
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->n_env_replies < mc->n_req_env) {
|
|
|
|
int j = mc->n_env_replies++;
|
|
|
|
if (!success) {
|
|
|
|
ppl_logevent(("Server refused to set environment variable %s",
|
|
|
|
conf_get_str_nthstrkey(mc->conf,
|
|
|
|
CONF_environmt, j)));
|
|
|
|
mc->n_env_fails++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->n_env_replies == mc->n_req_env) {
|
|
|
|
if (mc->n_env_fails == 0) {
|
|
|
|
ppl_logevent(("All environment variables successfully set"));
|
|
|
|
} else if (mc->n_env_fails == mc->n_req_env) {
|
|
|
|
ppl_logevent(("All environment variables refused"));
|
|
|
|
ppl_printf(("Server refused to set environment "
|
|
|
|
"variables\r\n"));
|
|
|
|
} else {
|
|
|
|
ppl_printf(("Server refused to set all environment "
|
|
|
|
"variables\r\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->req_cmd_primary) {
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->req_cmd_primary = false;
|
2018-09-30 06:16:38 +00:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
ppl_logevent(("Started a shell/command"));
|
|
|
|
mainchan_ready(mc);
|
|
|
|
} else if (*conf_get_str(mc->conf, CONF_remote_cmd2)) {
|
|
|
|
ppl_logevent(("Primary command failed; attempting fallback"));
|
|
|
|
mainchan_try_fallback_command(mc);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If there's no remote_cmd2 configured, then we have no
|
|
|
|
* fallback command, so we've run out of options.
|
|
|
|
*/
|
|
|
|
ssh_sw_abort(mc->ppl->ssh,
|
|
|
|
"Server refused to start a shell/command");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->req_cmd_fallback) {
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->req_cmd_fallback = false;
|
2018-09-30 06:16:38 +00:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
ppl_logevent(("Started a shell/command"));
|
|
|
|
ssh_got_fallback_cmd(mc->ppl->ssh);
|
|
|
|
mainchan_ready(mc);
|
|
|
|
} else {
|
|
|
|
ssh_sw_abort(mc->ppl->ssh,
|
|
|
|
"Server refused to start a shell/command");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_ready(mainchan *mc)
|
|
|
|
{
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->ready = true;
|
2018-09-30 06:16:38 +00:00
|
|
|
|
2018-10-29 19:50:29 +00:00
|
|
|
ssh_set_wants_user_input(mc->cl, true);
|
2018-09-30 06:16:38 +00:00
|
|
|
ssh_ppl_got_user_input(mc->ppl); /* in case any is already queued */
|
|
|
|
|
|
|
|
/* If an EOF arrived before we were ready, handle it now. */
|
|
|
|
if (mc->eof_pending) {
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->eof_pending = false;
|
2018-09-30 06:16:38 +00:00
|
|
|
mainchan_special_cmd(mc, SS_EOF, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssh_ldisc_update(mc->ppl->ssh);
|
|
|
|
queue_idempotent_callback(&mc->ppl->ic_process_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mainchan_open_failure_abort_ctx {
|
|
|
|
Ssh *ssh;
|
|
|
|
char *abort_message;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void mainchan_open_failure_abort(void *vctx)
|
|
|
|
{
|
|
|
|
struct mainchan_open_failure_abort_ctx *ctx =
|
|
|
|
(struct mainchan_open_failure_abort_ctx *)vctx;
|
|
|
|
ssh_sw_abort(
|
|
|
|
ctx->ssh, "Server refused to open main channel: %s",
|
|
|
|
ctx->abort_message);
|
|
|
|
sfree(ctx->abort_message);
|
|
|
|
sfree(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_open_failure(Channel *chan, const char *errtext)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
|
|
|
|
struct mainchan_open_failure_abort_ctx *ctx =
|
|
|
|
snew(struct mainchan_open_failure_abort_ctx);
|
|
|
|
|
|
|
|
ctx->ssh = mc->ppl->ssh;
|
|
|
|
ctx->abort_message = dupstr(errtext);
|
|
|
|
queue_toplevel_callback(mainchan_open_failure_abort, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mainchan_send(Channel *chan, int is_stderr,
|
|
|
|
const void *data, int length)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
return seat_output(mc->ppl->seat, is_stderr, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_send_eof(Channel *chan)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
|
2018-10-30 18:09:12 +00:00
|
|
|
if (!mc->eof_sent && (seat_eof(mc->ppl->seat) || mc->got_pty)) {
|
2018-09-30 06:16:38 +00:00
|
|
|
/*
|
|
|
|
* Either seat_eof told us that the front end wants us to
|
|
|
|
* close the outgoing side of the connection as soon as we see
|
|
|
|
* EOF from the far end, or else we've unilaterally decided to
|
|
|
|
* do that because we've allocated a remote pty and hence EOF
|
|
|
|
* isn't a particularly meaningful concept.
|
|
|
|
*/
|
|
|
|
sshfwd_write_eof(mc->sc);
|
|
|
|
ppl_logevent(("Sent EOF message"));
|
|
|
|
}
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->eof_sent = true;
|
|
|
|
ssh_set_wants_user_input(mc->cl, false); /* now stop reading from stdin */
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_set_input_wanted(Channel *chan, int wanted)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the main channel of the SSH session, i.e. the one tied
|
|
|
|
* to the standard input (or GUI) of the primary SSH client user
|
|
|
|
* interface. So ssh->send_ok is how we control whether we're
|
|
|
|
* reading from that input.
|
|
|
|
*/
|
|
|
|
ssh_set_wants_user_input(mc->cl, wanted);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *mainchan_log_close_msg(Channel *chan)
|
|
|
|
{
|
|
|
|
return dupstr("Main session channel closed");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mainchan_rcvd_exit_status(Channel *chan, int status)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
|
|
|
|
ssh_got_exitcode(mc->ppl->ssh, status);
|
|
|
|
ppl_logevent(("Session sent command exit status %d", status));
|
2018-10-29 19:50:29 +00:00
|
|
|
return true;
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_log_exit_signal_common(
|
|
|
|
mainchan *mc, const char *sigdesc,
|
|
|
|
int core_dumped, ptrlen msg)
|
|
|
|
{
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
|
|
|
|
const char *core_msg = core_dumped ? " (core dumped)" : "";
|
|
|
|
const char *msg_pre = (msg.len ? " (" : "");
|
|
|
|
const char *msg_post = (msg.len ? ")" : "");
|
|
|
|
ppl_logevent(("Session exited on %s%s%s%.*s%s",
|
|
|
|
sigdesc, core_msg, msg_pre, PTRLEN_PRINTF(msg), msg_post));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mainchan_rcvd_exit_signal(
|
|
|
|
Channel *chan, ptrlen signame, int core_dumped, ptrlen msg)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
int exitcode;
|
|
|
|
char *signame_str;
|
|
|
|
|
|
|
|
/*
|
2018-10-14 09:05:23 +00:00
|
|
|
* Translate the signal description back into a locally meaningful
|
|
|
|
* number, or 128 if the string didn't match any we recognise.
|
2018-09-30 06:16:38 +00:00
|
|
|
*/
|
2018-10-14 09:05:23 +00:00
|
|
|
exitcode = 128;
|
|
|
|
|
|
|
|
#define SIGNAL_SUB(s) \
|
|
|
|
if (ptrlen_eq_string(signame, #s)) \
|
|
|
|
exitcode = 128 + SIG ## s;
|
|
|
|
#define SIGNAL_MAIN(s, text) SIGNAL_SUB(s)
|
|
|
|
#define SIGNALS_LOCAL_ONLY
|
|
|
|
#include "sshsignals.h"
|
|
|
|
#undef SIGNAL_SUB
|
|
|
|
#undef SIGNAL_MAIN
|
|
|
|
#undef SIGNALS_LOCAL_ONLY
|
2018-09-30 06:16:38 +00:00
|
|
|
|
|
|
|
ssh_got_exitcode(mc->ppl->ssh, exitcode);
|
|
|
|
if (exitcode == 128)
|
|
|
|
signame_str = dupprintf("unrecognised signal \"%.*s\"",
|
|
|
|
PTRLEN_PRINTF(signame));
|
|
|
|
else
|
|
|
|
signame_str = dupprintf("signal SIG%.*s", PTRLEN_PRINTF(signame));
|
|
|
|
mainchan_log_exit_signal_common(mc, signame_str, core_dumped, msg);
|
|
|
|
sfree(signame_str);
|
2018-10-29 19:50:29 +00:00
|
|
|
return true;
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mainchan_rcvd_exit_signal_numeric(
|
|
|
|
Channel *chan, int signum, int core_dumped, ptrlen msg)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
char *signum_str;
|
|
|
|
|
|
|
|
ssh_got_exitcode(mc->ppl->ssh, 128 + signum);
|
|
|
|
signum_str = dupprintf("signal %d", signum);
|
|
|
|
mainchan_log_exit_signal_common(mc, signum_str, core_dumped, msg);
|
|
|
|
sfree(signum_str);
|
2018-10-29 19:50:29 +00:00
|
|
|
return true;
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mainchan_get_specials(
|
|
|
|
mainchan *mc, add_special_fn_t add_special, void *ctx)
|
|
|
|
{
|
|
|
|
/* FIXME: this _does_ depend on whether these services are supported */
|
|
|
|
|
|
|
|
add_special(ctx, "Break", SS_BRK, 0);
|
|
|
|
|
2018-10-14 09:05:23 +00:00
|
|
|
#define SIGNAL_MAIN(name, desc) \
|
2018-09-30 06:16:38 +00:00
|
|
|
add_special(ctx, "SIG" #name " (" desc ")", SS_SIG ## name, 0);
|
2018-10-14 09:05:23 +00:00
|
|
|
#define SIGNAL_SUB(name)
|
|
|
|
#include "sshsignals.h"
|
|
|
|
#undef SIGNAL_MAIN
|
|
|
|
#undef SIGNAL_SUB
|
2018-09-30 06:16:38 +00:00
|
|
|
|
|
|
|
add_special(ctx, "More signals", SS_SUBMENU, 0);
|
|
|
|
|
2018-10-14 09:05:23 +00:00
|
|
|
#define SIGNAL_MAIN(name, desc)
|
|
|
|
#define SIGNAL_SUB(name) \
|
|
|
|
add_special(ctx, "SIG" #name, SS_SIG ## name, 0);
|
|
|
|
#include "sshsignals.h"
|
|
|
|
#undef SIGNAL_MAIN
|
|
|
|
#undef SIGNAL_SUB
|
|
|
|
|
|
|
|
add_special(ctx, NULL, SS_EXITMENU, 0);
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *ssh_signal_lookup(SessionSpecialCode code)
|
|
|
|
{
|
2018-10-14 09:05:23 +00:00
|
|
|
#define SIGNAL_SUB(name) \
|
2018-09-30 06:16:38 +00:00
|
|
|
if (code == SS_SIG ## name) return #name;
|
2018-10-14 09:05:23 +00:00
|
|
|
#define SIGNAL_MAIN(name, desc) SIGNAL_SUB(name)
|
|
|
|
#include "sshsignals.h"
|
|
|
|
#undef SIGNAL_MAIN
|
|
|
|
#undef SIGNAL_SUB
|
2018-09-30 06:16:38 +00:00
|
|
|
|
2018-10-14 09:05:23 +00:00
|
|
|
/* If none of those clauses matched, fail lookup. */
|
2018-09-30 06:16:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mainchan_special_cmd(mainchan *mc, SessionSpecialCode code, int arg)
|
|
|
|
{
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
const char *signame;
|
|
|
|
|
|
|
|
if (code == SS_EOF) {
|
|
|
|
if (!mc->ready) {
|
|
|
|
/*
|
|
|
|
* Buffer the EOF to send as soon as the main channel is
|
|
|
|
* fully set up.
|
|
|
|
*/
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->eof_pending = true;
|
2018-09-30 06:16:38 +00:00
|
|
|
} else if (!mc->eof_sent) {
|
|
|
|
sshfwd_write_eof(mc->sc);
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->eof_sent = true;
|
2018-09-30 06:16:38 +00:00
|
|
|
}
|
|
|
|
} else if (code == SS_BRK) {
|
|
|
|
sshfwd_send_serial_break(
|
2018-10-29 19:50:29 +00:00
|
|
|
mc->sc, false, 0 /* default break length */);
|
2018-09-30 06:16:38 +00:00
|
|
|
} else if ((signame = ssh_signal_lookup(code)) != NULL) {
|
|
|
|
/* It's a signal. */
|
2018-10-29 19:50:29 +00:00
|
|
|
sshfwd_send_signal(mc->sc, false, signame);
|
2018-09-30 06:16:38 +00:00
|
|
|
ppl_logevent(("Sent signal SIG%s", signame));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mainchan_terminal_size(mainchan *mc, int width, int height)
|
|
|
|
{
|
|
|
|
mc->term_width = width;
|
|
|
|
mc->term_height = height;
|
|
|
|
|
|
|
|
if (mc->req_pty || mc->got_pty)
|
|
|
|
sshfwd_send_terminal_size_change(mc->sc, width, height);
|
|
|
|
}
|