mainchan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * SSH main session channel handling.
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "putty.h"
  8. #include "ssh.h"
  9. #include "sshppl.h"
  10. #include "sshchan.h"
  11. static void mainchan_free(Channel *chan);
  12. static void mainchan_open_confirmation(Channel *chan);
  13. static void mainchan_open_failure(Channel *chan, const char *errtext);
  14. static size_t mainchan_send(
  15. Channel *chan, bool is_stderr, const void *, size_t);
  16. static void mainchan_send_eof(Channel *chan);
  17. static void mainchan_set_input_wanted(Channel *chan, bool wanted);
  18. static char *mainchan_log_close_msg(Channel *chan);
  19. static bool mainchan_rcvd_exit_status(Channel *chan, int status);
  20. static bool mainchan_rcvd_exit_signal(
  21. Channel *chan, ptrlen signame, bool core_dumped, ptrlen msg);
  22. static bool mainchan_rcvd_exit_signal_numeric(
  23. Channel *chan, int signum, bool core_dumped, ptrlen msg);
  24. static void mainchan_request_response(Channel *chan, bool success);
  25. static const struct ChannelVtable mainchan_channelvt = {
  26. mainchan_free,
  27. mainchan_open_confirmation,
  28. mainchan_open_failure,
  29. mainchan_send,
  30. mainchan_send_eof,
  31. mainchan_set_input_wanted,
  32. mainchan_log_close_msg,
  33. chan_default_want_close,
  34. mainchan_rcvd_exit_status,
  35. mainchan_rcvd_exit_signal,
  36. mainchan_rcvd_exit_signal_numeric,
  37. chan_no_run_shell,
  38. chan_no_run_command,
  39. chan_no_run_subsystem,
  40. chan_no_enable_x11_forwarding,
  41. chan_no_enable_agent_forwarding,
  42. chan_no_allocate_pty,
  43. chan_no_set_env,
  44. chan_no_send_break,
  45. chan_no_send_signal,
  46. chan_no_change_window_size,
  47. mainchan_request_response,
  48. };
  49. typedef enum MainChanType {
  50. MAINCHAN_SESSION, MAINCHAN_DIRECT_TCPIP
  51. } MainChanType;
  52. struct mainchan {
  53. SshChannel *sc;
  54. Conf *conf;
  55. PacketProtocolLayer *ppl;
  56. ConnectionLayer *cl;
  57. MainChanType type;
  58. bool is_simple;
  59. bool req_x11, req_agent, req_pty, req_cmd_primary, req_cmd_fallback;
  60. int n_req_env, n_env_replies, n_env_fails;
  61. bool eof_pending, eof_sent, got_pty, ready;
  62. int term_width, term_height;
  63. Channel chan;
  64. };
  65. mainchan *mainchan_new(
  66. PacketProtocolLayer *ppl, ConnectionLayer *cl, Conf *conf,
  67. int term_width, int term_height, bool is_simple, SshChannel **sc_out)
  68. {
  69. mainchan *mc;
  70. if (conf_get_bool(conf, CONF_ssh_no_shell))
  71. return NULL; /* no main channel at all */
  72. mc = snew(mainchan);
  73. memset(mc, 0, sizeof(mainchan));
  74. mc->ppl = ppl;
  75. mc->cl = cl;
  76. mc->conf = conf_copy(conf);
  77. mc->term_width = term_width;
  78. mc->term_height = term_height;
  79. mc->is_simple = is_simple;
  80. mc->sc = NULL;
  81. mc->chan.vt = &mainchan_channelvt;
  82. mc->chan.initial_fixed_window_size = 0;
  83. if (*conf_get_str(mc->conf, CONF_ssh_nc_host)) {
  84. const char *host = conf_get_str(mc->conf, CONF_ssh_nc_host);
  85. int port = conf_get_int(mc->conf, CONF_ssh_nc_port);
  86. mc->sc = ssh_lportfwd_open(cl, host, port, "main channel",
  87. NULL, &mc->chan);
  88. mc->type = MAINCHAN_DIRECT_TCPIP;
  89. } else {
  90. mc->sc = ssh_session_open(cl, &mc->chan);
  91. mc->type = MAINCHAN_SESSION;
  92. }
  93. if (sc_out) *sc_out = mc->sc;
  94. return mc;
  95. }
  96. static void mainchan_free(Channel *chan)
  97. {
  98. pinitassert(chan->vt == &mainchan_channelvt);
  99. mainchan *mc = container_of(chan, mainchan, chan);
  100. conf_free(mc->conf);
  101. sfree(mc);
  102. }
  103. static void mainchan_try_fallback_command(mainchan *mc);
  104. static void mainchan_ready(mainchan *mc);
  105. static void mainchan_open_confirmation(Channel *chan)
  106. {
  107. mainchan *mc = container_of(chan, mainchan, chan);
  108. PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
  109. seat_update_specials_menu(mc->ppl->seat);
  110. ppl_logevent("Opened main channel");
  111. if (mc->is_simple)
  112. sshfwd_hint_channel_is_simple(mc->sc);
  113. if (mc->type == MAINCHAN_SESSION) {
  114. /*
  115. * Send the CHANNEL_REQUESTS for the main session channel.
  116. */
  117. char *key, *val, *cmd;
  118. struct X11Display *x11disp;
  119. struct X11FakeAuth *x11auth;
  120. bool retry_cmd_now = false;
  121. if (conf_get_bool(mc->conf, CONF_x11_forward)) {
  122. char *x11_setup_err;
  123. if ((x11disp = x11_setup_display(
  124. conf_get_str(mc->conf, CONF_x11_display),
  125. mc->conf, &x11_setup_err)) == NULL) {
  126. ppl_logevent("X11 forwarding not enabled: unable to"
  127. " initialise X display: %s", x11_setup_err);
  128. sfree(x11_setup_err);
  129. } else {
  130. x11auth = ssh_add_x11_display(
  131. mc->cl, conf_get_int(mc->conf, CONF_x11_auth), x11disp);
  132. sshfwd_request_x11_forwarding(
  133. mc->sc, true, x11auth->protoname, x11auth->datastring,
  134. x11disp->screennum, false);
  135. mc->req_x11 = true;
  136. }
  137. }
  138. if (ssh_agent_forwarding_permitted(mc->cl)) {
  139. sshfwd_request_agent_forwarding(mc->sc, true);
  140. mc->req_agent = true;
  141. }
  142. if (!conf_get_bool(mc->conf, CONF_nopty)) {
  143. sshfwd_request_pty(
  144. mc->sc, true, mc->conf, mc->term_width, mc->term_height);
  145. mc->req_pty = true;
  146. }
  147. for (val = conf_get_str_strs(mc->conf, CONF_environmt, NULL, &key);
  148. val != NULL;
  149. val = conf_get_str_strs(mc->conf, CONF_environmt, key, &key)) {
  150. sshfwd_send_env_var(mc->sc, true, key, val);
  151. mc->n_req_env++;
  152. }
  153. if (mc->n_req_env)
  154. ppl_logevent("Sent %d environment variables", mc->n_req_env);
  155. cmd = conf_get_str(mc->conf, CONF_remote_cmd);
  156. if (conf_get_bool(mc->conf, CONF_ssh_subsys)) {
  157. retry_cmd_now = !sshfwd_start_subsystem(mc->sc, true, cmd);
  158. } else if (*cmd) {
  159. sshfwd_start_command(mc->sc, true, cmd);
  160. } else {
  161. sshfwd_start_shell(mc->sc, true);
  162. }
  163. if (retry_cmd_now)
  164. mainchan_try_fallback_command(mc);
  165. else
  166. mc->req_cmd_primary = true;
  167. } else {
  168. ssh_set_ldisc_option(mc->cl, LD_ECHO, true);
  169. ssh_set_ldisc_option(mc->cl, LD_EDIT, true);
  170. mainchan_ready(mc);
  171. }
  172. }
  173. static void mainchan_try_fallback_command(mainchan *mc)
  174. {
  175. const char *cmd = conf_get_str(mc->conf, CONF_remote_cmd2);
  176. if (conf_get_bool(mc->conf, CONF_ssh_subsys2)) {
  177. sshfwd_start_subsystem(mc->sc, true, cmd);
  178. } else {
  179. sshfwd_start_command(mc->sc, true, cmd);
  180. }
  181. mc->req_cmd_fallback = true;
  182. }
  183. static void mainchan_request_response(Channel *chan, bool success)
  184. {
  185. pinitassert(chan->vt == &mainchan_channelvt);
  186. mainchan *mc = container_of(chan, mainchan, chan);
  187. PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
  188. if (mc->req_x11) {
  189. mc->req_x11 = false;
  190. if (success) {
  191. ppl_logevent("X11 forwarding enabled");
  192. ssh_enable_x_fwd(mc->cl);
  193. } else {
  194. ppl_logevent("X11 forwarding refused");
  195. }
  196. return;
  197. }
  198. if (mc->req_agent) {
  199. mc->req_agent = false;
  200. if (success) {
  201. ppl_logevent("Agent forwarding enabled");
  202. ssh_enable_agent_fwd(mc->cl);
  203. } else {
  204. ppl_logevent("Agent forwarding refused");
  205. }
  206. return;
  207. }
  208. if (mc->req_pty) {
  209. mc->req_pty = false;
  210. if (success) {
  211. ppl_logevent("Allocated pty");
  212. mc->got_pty = true;
  213. } else {
  214. ppl_logevent("Server refused to allocate pty");
  215. ppl_printf("Server refused to allocate pty\r\n");
  216. ssh_set_ldisc_option(mc->cl, LD_ECHO, true);
  217. ssh_set_ldisc_option(mc->cl, LD_EDIT, true);
  218. }
  219. return;
  220. }
  221. if (mc->n_env_replies < mc->n_req_env) {
  222. int j = mc->n_env_replies++;
  223. if (!success) {
  224. ppl_logevent("Server refused to set environment variable %s",
  225. conf_get_str_nthstrkey(mc->conf,
  226. CONF_environmt, j));
  227. mc->n_env_fails++;
  228. }
  229. if (mc->n_env_replies == mc->n_req_env) {
  230. if (mc->n_env_fails == 0) {
  231. ppl_logevent("All environment variables successfully set");
  232. } else if (mc->n_env_fails == mc->n_req_env) {
  233. ppl_logevent("All environment variables refused");
  234. ppl_printf("Server refused to set environment "
  235. "variables\r\n");
  236. } else {
  237. ppl_printf("Server refused to set all environment "
  238. "variables\r\n");
  239. }
  240. }
  241. return;
  242. }
  243. if (mc->req_cmd_primary) {
  244. mc->req_cmd_primary = false;
  245. if (success) {
  246. ppl_logevent("Started a shell/command");
  247. mainchan_ready(mc);
  248. } else if (*conf_get_str(mc->conf, CONF_remote_cmd2) || conf_get_bool(mc->conf, CONF_force_remote_cmd2)) { // WINSCP
  249. ppl_logevent("Primary command failed; attempting fallback");
  250. mainchan_try_fallback_command(mc);
  251. } else {
  252. /*
  253. * If there's no remote_cmd2 configured, then we have no
  254. * fallback command, so we've run out of options.
  255. */
  256. ssh_sw_abort(mc->ppl->ssh,
  257. "Server refused to start a shell/command");
  258. }
  259. return;
  260. }
  261. if (mc->req_cmd_fallback) {
  262. mc->req_cmd_fallback = false;
  263. if (success) {
  264. ppl_logevent("Started a shell/command");
  265. ssh_got_fallback_cmd(mc->ppl->ssh);
  266. mainchan_ready(mc);
  267. } else {
  268. ssh_sw_abort(mc->ppl->ssh,
  269. "Server refused to start a shell/command");
  270. }
  271. return;
  272. }
  273. }
  274. static void mainchan_ready(mainchan *mc)
  275. {
  276. mc->ready = true;
  277. ssh_set_wants_user_input(mc->cl, true);
  278. ssh_ppl_got_user_input(mc->ppl); /* in case any is already queued */
  279. /* If an EOF arrived before we were ready, handle it now. */
  280. if (mc->eof_pending) {
  281. mc->eof_pending = false;
  282. mainchan_special_cmd(mc, SS_EOF, 0);
  283. }
  284. ssh_ldisc_update(mc->ppl->ssh);
  285. queue_idempotent_callback(&mc->ppl->ic_process_queue);
  286. }
  287. struct mainchan_open_failure_abort_ctx {
  288. Ssh *ssh;
  289. char *abort_message;
  290. };
  291. static void mainchan_open_failure_abort(void *vctx)
  292. {
  293. struct mainchan_open_failure_abort_ctx *ctx =
  294. (struct mainchan_open_failure_abort_ctx *)vctx;
  295. ssh_sw_abort(
  296. ctx->ssh, "Server refused to open main channel: %s",
  297. ctx->abort_message);
  298. sfree(ctx->abort_message);
  299. sfree(ctx);
  300. }
  301. static void mainchan_open_failure(Channel *chan, const char *errtext)
  302. {
  303. pinitassert(chan->vt == &mainchan_channelvt);
  304. mainchan *mc = container_of(chan, mainchan, chan);
  305. struct mainchan_open_failure_abort_ctx *ctx =
  306. snew(struct mainchan_open_failure_abort_ctx);
  307. ctx->ssh = mc->ppl->ssh;
  308. ctx->abort_message = dupstr(errtext);
  309. queue_toplevel_callback(get_log_callback_set(mc->cl->logctx), mainchan_open_failure_abort, ctx);
  310. }
  311. static size_t mainchan_send(Channel *chan, bool is_stderr,
  312. const void *data, size_t length)
  313. {
  314. pinitassert(chan->vt == &mainchan_channelvt);
  315. mainchan *mc = container_of(chan, mainchan, chan);
  316. return seat_output(mc->ppl->seat, is_stderr, data, length);
  317. }
  318. static void mainchan_send_eof(Channel *chan)
  319. {
  320. pinitassert(chan->vt == &mainchan_channelvt);
  321. mainchan *mc = container_of(chan, mainchan, chan);
  322. PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
  323. if (!mc->eof_sent && (seat_eof(mc->ppl->seat) || mc->got_pty)) {
  324. /*
  325. * Either seat_eof told us that the front end wants us to
  326. * close the outgoing side of the connection as soon as we see
  327. * EOF from the far end, or else we've unilaterally decided to
  328. * do that because we've allocated a remote pty and hence EOF
  329. * isn't a particularly meaningful concept.
  330. */
  331. sshfwd_write_eof(mc->sc);
  332. ppl_logevent("Sent EOF message");
  333. mc->eof_sent = true;
  334. ssh_set_wants_user_input(mc->cl, false); /* stop reading from stdin */
  335. }
  336. }
  337. static void mainchan_set_input_wanted(Channel *chan, bool wanted)
  338. {
  339. pinitassert(chan->vt == &mainchan_channelvt);
  340. mainchan *mc = container_of(chan, mainchan, chan);
  341. /*
  342. * This is the main channel of the SSH session, i.e. the one tied
  343. * to the standard input (or GUI) of the primary SSH client user
  344. * interface. So ssh->send_ok is how we control whether we're
  345. * reading from that input.
  346. */
  347. ssh_set_wants_user_input(mc->cl, wanted);
  348. }
  349. static char *mainchan_log_close_msg(Channel *chan)
  350. {
  351. return dupstr("Main session channel closed");
  352. }
  353. static bool mainchan_rcvd_exit_status(Channel *chan, int status)
  354. {
  355. pinitassert(chan->vt == &mainchan_channelvt);
  356. mainchan *mc = container_of(chan, mainchan, chan);
  357. PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
  358. ssh_got_exitcode(mc->ppl->ssh, status);
  359. ppl_logevent("Session sent command exit status %d", status);
  360. return true;
  361. }
  362. static void mainchan_log_exit_signal_common(
  363. mainchan *mc, const char *sigdesc,
  364. bool core_dumped, ptrlen msg)
  365. {
  366. PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
  367. const char *core_msg = core_dumped ? " (core dumped)" : "";
  368. const char *msg_pre = (msg.len ? " (" : "");
  369. const char *msg_post = (msg.len ? ")" : "");
  370. ppl_logevent("Session exited on %s%s%s%.*s%s",
  371. sigdesc, core_msg, msg_pre, PTRLEN_PRINTF(msg), msg_post);
  372. }
  373. static bool mainchan_rcvd_exit_signal(
  374. Channel *chan, ptrlen signame, bool core_dumped, ptrlen msg)
  375. {
  376. pinitassert(chan->vt == &mainchan_channelvt);
  377. mainchan *mc = container_of(chan, mainchan, chan);
  378. int exitcode;
  379. char *signame_str;
  380. /*
  381. * Translate the signal description back into a locally meaningful
  382. * number, or 128 if the string didn't match any we recognise.
  383. */
  384. exitcode = 128;
  385. #define SIGNAL_SUB(s) \
  386. if (ptrlen_eq_string(signame, #s)) \
  387. exitcode = 128 + SIG ## s;
  388. #define SIGNAL_MAIN(s, text) SIGNAL_SUB(s)
  389. #define SIGNALS_LOCAL_ONLY
  390. #include "sshsignals.h"
  391. #undef SIGNAL_SUB
  392. #undef SIGNAL_MAIN
  393. #undef SIGNALS_LOCAL_ONLY
  394. ssh_got_exitcode(mc->ppl->ssh, exitcode);
  395. if (exitcode == 128)
  396. signame_str = dupprintf("unrecognised signal \"%.*s\"",
  397. PTRLEN_PRINTF(signame));
  398. else
  399. signame_str = dupprintf("signal SIG%.*s", PTRLEN_PRINTF(signame));
  400. mainchan_log_exit_signal_common(mc, signame_str, core_dumped, msg);
  401. sfree(signame_str);
  402. return true;
  403. }
  404. static bool mainchan_rcvd_exit_signal_numeric(
  405. Channel *chan, int signum, bool core_dumped, ptrlen msg)
  406. {
  407. pinitassert(chan->vt == &mainchan_channelvt);
  408. mainchan *mc = container_of(chan, mainchan, chan);
  409. char *signum_str;
  410. ssh_got_exitcode(mc->ppl->ssh, 128 + signum);
  411. signum_str = dupprintf("signal %d", signum);
  412. mainchan_log_exit_signal_common(mc, signum_str, core_dumped, msg);
  413. sfree(signum_str);
  414. return true;
  415. }
  416. void mainchan_get_specials(
  417. mainchan *mc, add_special_fn_t add_special, void *ctx)
  418. {
  419. /* FIXME: this _does_ depend on whether these services are supported */
  420. /* WINSCP (causes linker warning and we do not use these)
  421. add_special(ctx, "Break", SS_BRK, 0);
  422. #define SIGNAL_MAIN(name, desc) \
  423. add_special(ctx, "SIG" #name " (" desc ")", SS_SIG ## name, 0);
  424. #define SIGNAL_SUB(name)
  425. #include "sshsignals.h"
  426. #undef SIGNAL_MAIN
  427. #undef SIGNAL_SUB
  428. add_special(ctx, "More signals", SS_SUBMENU, 0);
  429. #define SIGNAL_MAIN(name, desc)
  430. #define SIGNAL_SUB(name) \
  431. add_special(ctx, "SIG" #name, SS_SIG ## name, 0);
  432. #include "sshsignals.h"
  433. #undef SIGNAL_MAIN
  434. #undef SIGNAL_SUB
  435. add_special(ctx, NULL, SS_EXITMENU, 0);
  436. */
  437. }
  438. static const char *ssh_signal_lookup(SessionSpecialCode code)
  439. {
  440. #define SIGNAL_SUB(name) \
  441. if (code == SS_SIG ## name) return #name;
  442. #define SIGNAL_MAIN(name, desc) SIGNAL_SUB(name)
  443. #include "sshsignals.h"
  444. #undef SIGNAL_MAIN
  445. #undef SIGNAL_SUB
  446. /* If none of those clauses matched, fail lookup. */
  447. return NULL;
  448. }
  449. void mainchan_special_cmd(mainchan *mc, SessionSpecialCode code, int arg)
  450. {
  451. PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
  452. const char *signame;
  453. if (code == SS_EOF) {
  454. if (!mc->ready) {
  455. /*
  456. * Buffer the EOF to send as soon as the main channel is
  457. * fully set up.
  458. */
  459. mc->eof_pending = true;
  460. } else if (!mc->eof_sent) {
  461. sshfwd_write_eof(mc->sc);
  462. mc->eof_sent = true;
  463. }
  464. } else if (code == SS_BRK) {
  465. sshfwd_send_serial_break(
  466. mc->sc, false, 0 /* default break length */);
  467. } else if ((signame = ssh_signal_lookup(code)) != NULL) {
  468. /* It's a signal. */
  469. sshfwd_send_signal(mc->sc, false, signame);
  470. ppl_logevent("Sent signal SIG%s", signame);
  471. }
  472. }
  473. void mainchan_terminal_size(mainchan *mc, int width, int height)
  474. {
  475. mc->term_width = width;
  476. mc->term_height = height;
  477. if (mc->req_pty || mc->got_pty)
  478. sshfwd_send_terminal_size_change(mc->sc, width, height);
  479. }