ssh1connection.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*
  2. * Packet protocol layer for the SSH-1 'connection protocol', i.e.
  3. * everything after authentication finishes.
  4. */
  5. #include <assert.h>
  6. #include "putty.h"
  7. #include "ssh.h"
  8. #include "sshbpp.h"
  9. #include "sshppl.h"
  10. #include "sshchan.h"
  11. #include "sshcr.h"
  12. #include "ssh1connection.h"
  13. // WINSCP
  14. #define queue_toplevel_callback(FN, CTX) queue_toplevel_callback(get_log_callback_set(CTX->cl.logctx), FN, CTX)
  15. static int ssh1_rportfwd_cmp(void *av, void *bv)
  16. {
  17. struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
  18. struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
  19. int i;
  20. if ( (i = strcmp(a->dhost, b->dhost)) != 0)
  21. return i < 0 ? -1 : +1;
  22. if (a->dport > b->dport)
  23. return +1;
  24. if (a->dport < b->dport)
  25. return -1;
  26. return 0;
  27. }
  28. static void ssh1_connection_free(PacketProtocolLayer *);
  29. static void ssh1_connection_process_queue(PacketProtocolLayer *);
  30. static void ssh1_connection_special_cmd(PacketProtocolLayer *ppl,
  31. SessionSpecialCode code, int arg);
  32. static bool ssh1_connection_want_user_input(PacketProtocolLayer *ppl);
  33. static void ssh1_connection_got_user_input(PacketProtocolLayer *ppl);
  34. static void ssh1_connection_reconfigure(PacketProtocolLayer *ppl, Conf *conf);
  35. static unsigned int ssh1_connection_winscp_query(PacketProtocolLayer *ppl, int query);
  36. static const PacketProtocolLayerVtable ssh1_connection_vtable = {
  37. // WINSCP
  38. /*.free =*/ ssh1_connection_free,
  39. /*.process_queue =*/ ssh1_connection_process_queue,
  40. /*.get_specials =*/ ssh1_common_get_specials,
  41. /*.special_cmd =*/ ssh1_connection_special_cmd,
  42. /*.want_user_input =*/ ssh1_connection_want_user_input,
  43. /*.got_user_input =*/ ssh1_connection_got_user_input,
  44. /*.reconfigure =*/ ssh1_connection_reconfigure,
  45. /*.queued_data_size =*/ ssh_ppl_default_queued_data_size,
  46. /*.name =*/ NULL, /* no layer names in SSH-1 */
  47. ssh1_connection_winscp_query,
  48. };
  49. static void ssh1_rportfwd_remove(
  50. ConnectionLayer *cl, struct ssh_rportfwd *rpf);
  51. static SshChannel *ssh1_lportfwd_open(
  52. ConnectionLayer *cl, const char *hostname, int port,
  53. const char *description, const SocketPeerInfo *pi, Channel *chan);
  54. static struct X11FakeAuth *ssh1_add_x11_display(
  55. ConnectionLayer *cl, int authtype, struct X11Display *disp);
  56. static bool ssh1_agent_forwarding_permitted(ConnectionLayer *cl);
  57. static void ssh1_terminal_size(ConnectionLayer *cl, int width, int height);
  58. static void ssh1_stdout_unthrottle(ConnectionLayer *cl, size_t bufsize);
  59. static size_t ssh1_stdin_backlog(ConnectionLayer *cl);
  60. static void ssh1_throttle_all_channels(ConnectionLayer *cl, bool throttled);
  61. static bool ssh1_ldisc_option(ConnectionLayer *cl, int option);
  62. static void ssh1_set_ldisc_option(ConnectionLayer *cl, int option, bool value);
  63. static void ssh1_enable_x_fwd(ConnectionLayer *cl);
  64. static void ssh1_set_wants_user_input(ConnectionLayer *cl, bool wanted);
  65. static const ConnectionLayerVtable ssh1_connlayer_vtable = {
  66. // WINSCP
  67. /*.rportfwd_alloc =*/ ssh1_rportfwd_alloc,
  68. /*.rportfwd_remove =*/ ssh1_rportfwd_remove,
  69. /*.lportfwd_open =*/ ssh1_lportfwd_open,
  70. /*.session_open =*/ ssh1_session_open,
  71. /*.serverside_x11_open =*/ ssh1_serverside_x11_open,
  72. /*.serverside_agent_open =*/ ssh1_serverside_agent_open,
  73. /*.add_x11_display =*/ ssh1_add_x11_display,
  74. NULL, NULL, NULL, NULL, NULL, NULL, NULL, // WINSCP
  75. /*.agent_forwarding_permitted =*/ ssh1_agent_forwarding_permitted,
  76. /*.terminal_size =*/ ssh1_terminal_size,
  77. /*.stdout_unthrottle =*/ ssh1_stdout_unthrottle,
  78. /*.stdin_backlog =*/ ssh1_stdin_backlog,
  79. /*.throttle_all_channels =*/ ssh1_throttle_all_channels,
  80. /*.ldisc_option =*/ ssh1_ldisc_option,
  81. /*.set_ldisc_option =*/ ssh1_set_ldisc_option,
  82. /*.enable_x_fwd =*/ ssh1_enable_x_fwd,
  83. /*.set_wants_user_input =*/ ssh1_set_wants_user_input,
  84. /* other methods are NULL */
  85. };
  86. static size_t ssh1channel_write(
  87. SshChannel *c, bool is_stderr, const void *buf, size_t len);
  88. static void ssh1channel_write_eof(SshChannel *c);
  89. static void ssh1channel_initiate_close(SshChannel *c, const char *err);
  90. static void ssh1channel_unthrottle(SshChannel *c, size_t bufsize);
  91. static Conf *ssh1channel_get_conf(SshChannel *c);
  92. static void ssh1channel_window_override_removed(SshChannel *c) { /* ignore */ }
  93. static const SshChannelVtable ssh1channel_vtable = {
  94. // WINSCP
  95. /*.write =*/ ssh1channel_write,
  96. /*.write_eof =*/ ssh1channel_write_eof,
  97. /*.initiate_close =*/ ssh1channel_initiate_close,
  98. /*.unthrottle =*/ ssh1channel_unthrottle,
  99. /*.get_conf =*/ ssh1channel_get_conf,
  100. /*.window_override_removed =*/ ssh1channel_window_override_removed,
  101. /* everything else is NULL */
  102. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // WINSCP
  103. };
  104. static void ssh1_channel_try_eof(struct ssh1_channel *c);
  105. static void ssh1_channel_close_local(struct ssh1_channel *c,
  106. const char *reason);
  107. static void ssh1_channel_destroy(struct ssh1_channel *c);
  108. static void ssh1_channel_check_close(struct ssh1_channel *c);
  109. static int ssh1_channelcmp(void *av, void *bv)
  110. {
  111. const struct ssh1_channel *a = (const struct ssh1_channel *) av;
  112. const struct ssh1_channel *b = (const struct ssh1_channel *) bv;
  113. if (a->localid < b->localid)
  114. return -1;
  115. if (a->localid > b->localid)
  116. return +1;
  117. return 0;
  118. }
  119. static int ssh1_channelfind(void *av, void *bv)
  120. {
  121. const unsigned *a = (const unsigned *) av;
  122. const struct ssh1_channel *b = (const struct ssh1_channel *) bv;
  123. if (*a < b->localid)
  124. return -1;
  125. if (*a > b->localid)
  126. return +1;
  127. return 0;
  128. }
  129. void ssh1_channel_free(struct ssh1_channel *c)
  130. {
  131. if (c->chan)
  132. chan_free(c->chan);
  133. sfree(c);
  134. }
  135. PacketProtocolLayer *ssh1_connection_new(
  136. Ssh *ssh, Conf *conf, ConnectionLayer **cl_out)
  137. {
  138. struct ssh1_connection_state *s = snew(struct ssh1_connection_state);
  139. memset(s, 0, sizeof(*s));
  140. s->ppl.vt = &ssh1_connection_vtable;
  141. s->conf = conf_copy(conf);
  142. s->channels = newtree234(ssh1_channelcmp);
  143. s->x11authtree = newtree234(x11_authcmp);
  144. /* Need to get the log context for s->cl now, because we won't be
  145. * helpfully notified when a copy is written into s->ppl by our
  146. * owner. */
  147. s->cl.vt = &ssh1_connlayer_vtable;
  148. s->cl.logctx = ssh_get_logctx(ssh);
  149. s->portfwdmgr = portfwdmgr_new(&s->cl);
  150. s->rportfwds = newtree234(ssh1_rportfwd_cmp);
  151. *cl_out = &s->cl;
  152. return &s->ppl;
  153. }
  154. static void ssh1_connection_free(PacketProtocolLayer *ppl)
  155. {
  156. struct ssh1_connection_state *s =
  157. container_of(ppl, struct ssh1_connection_state, ppl);
  158. struct X11FakeAuth *auth;
  159. struct ssh1_channel *c;
  160. struct ssh_rportfwd *rpf;
  161. conf_free(s->conf);
  162. while ((c = delpos234(s->channels, 0)) != NULL)
  163. ssh1_channel_free(c);
  164. freetree234(s->channels);
  165. if (s->mainchan_chan)
  166. chan_free(s->mainchan_chan);
  167. if (s->x11disp)
  168. x11_free_display(s->x11disp);
  169. while ((auth = delpos234(s->x11authtree, 0)) != NULL)
  170. x11_free_fake_auth(auth);
  171. freetree234(s->x11authtree);
  172. while ((rpf = delpos234(s->rportfwds, 0)) != NULL)
  173. free_rportfwd(rpf);
  174. freetree234(s->rportfwds);
  175. portfwdmgr_free(s->portfwdmgr);
  176. if (s->antispoof_prompt)
  177. free_prompts(s->antispoof_prompt);
  178. delete_callbacks_for_context(get_seat_callback_set(ppl->seat), s);
  179. sfree(s);
  180. }
  181. void ssh1_connection_set_protoflags(PacketProtocolLayer *ppl,
  182. int local, int remote)
  183. {
  184. pinitassert(ppl->vt == &ssh1_connection_vtable);
  185. struct ssh1_connection_state *s =
  186. container_of(ppl, struct ssh1_connection_state, ppl);
  187. s->local_protoflags = local;
  188. s->remote_protoflags = remote;
  189. }
  190. static bool ssh1_connection_filter_queue(struct ssh1_connection_state *s)
  191. {
  192. PktIn *pktin;
  193. ptrlen data;
  194. struct ssh1_channel *c;
  195. unsigned localid;
  196. bool expect_halfopen;
  197. while (1) {
  198. if (ssh1_common_filter_queue(&s->ppl))
  199. return true;
  200. if ((pktin = pq_peek(s->ppl.in_pq)) == NULL)
  201. return false;
  202. switch (pktin->type) {
  203. case SSH1_MSG_CHANNEL_DATA:
  204. case SSH1_MSG_CHANNEL_OPEN_CONFIRMATION:
  205. case SSH1_MSG_CHANNEL_OPEN_FAILURE:
  206. case SSH1_MSG_CHANNEL_CLOSE:
  207. case SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION:
  208. /*
  209. * Common preliminary code for all the messages from the
  210. * server that cite one of our channel ids: look up that
  211. * channel id, check it exists, and if it's for a sharing
  212. * downstream, pass it on.
  213. */
  214. localid = get_uint32(pktin);
  215. c = find234(s->channels, &localid, ssh1_channelfind);
  216. expect_halfopen = (
  217. pktin->type == SSH1_MSG_CHANNEL_OPEN_CONFIRMATION ||
  218. pktin->type == SSH1_MSG_CHANNEL_OPEN_FAILURE);
  219. if (!c || c->halfopen != expect_halfopen) {
  220. ssh_remote_error(
  221. s->ppl.ssh, "Received %s for %s channel %u",
  222. ssh1_pkt_type(pktin->type),
  223. !c ? "nonexistent" : c->halfopen ? "half-open" : "open",
  224. localid);
  225. return true;
  226. }
  227. switch (pktin->type) {
  228. case SSH1_MSG_CHANNEL_OPEN_CONFIRMATION:
  229. assert(c->halfopen);
  230. c->remoteid = get_uint32(pktin);
  231. c->halfopen = false;
  232. c->throttling_conn = false;
  233. chan_open_confirmation(c->chan);
  234. /*
  235. * Now that the channel is fully open, it's possible
  236. * in principle to immediately close it. Check whether
  237. * it wants us to!
  238. *
  239. * This can occur if a local socket error occurred
  240. * between us sending out CHANNEL_OPEN and receiving
  241. * OPEN_CONFIRMATION. If that happens, all we can do
  242. * is immediately initiate close proceedings now that
  243. * we know the server's id to put in the close
  244. * message. We'll have handled that in this code by
  245. * having already turned c->chan into a zombie, so its
  246. * want_close method (which ssh1_channel_check_close
  247. * will consult) will already be returning true.
  248. */
  249. ssh1_channel_check_close(c);
  250. if (c->pending_eof)
  251. ssh1_channel_try_eof(c); /* in case we had a pending EOF */
  252. break;
  253. case SSH1_MSG_CHANNEL_OPEN_FAILURE:
  254. assert(c->halfopen);
  255. chan_open_failed(c->chan, NULL);
  256. chan_free(c->chan);
  257. del234(s->channels, c);
  258. ssh1_channel_free(c);
  259. break;
  260. case SSH1_MSG_CHANNEL_DATA:
  261. data = get_string(pktin);
  262. if (!get_err(pktin)) {
  263. int bufsize = chan_send(
  264. c->chan, false, data.ptr, data.len);
  265. if (!c->throttling_conn && bufsize > SSH1_BUFFER_LIMIT) {
  266. c->throttling_conn = true;
  267. ssh_throttle_conn(s->ppl.ssh, +1);
  268. }
  269. }
  270. break;
  271. case SSH1_MSG_CHANNEL_CLOSE:
  272. if (!(c->closes & CLOSES_RCVD_CLOSE)) {
  273. c->closes |= CLOSES_RCVD_CLOSE;
  274. chan_send_eof(c->chan);
  275. ssh1_channel_check_close(c);
  276. }
  277. break;
  278. case SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION:
  279. if (!(c->closes & CLOSES_RCVD_CLOSECONF)) {
  280. if (!(c->closes & CLOSES_SENT_CLOSE)) {
  281. ssh_remote_error(
  282. s->ppl.ssh,
  283. "Received CHANNEL_CLOSE_CONFIRMATION for channel"
  284. " %u for which we never sent CHANNEL_CLOSE\n",
  285. c->localid);
  286. return true;
  287. }
  288. c->closes |= CLOSES_RCVD_CLOSECONF;
  289. ssh1_channel_check_close(c);
  290. }
  291. break;
  292. }
  293. pq_pop(s->ppl.in_pq);
  294. break;
  295. default:
  296. if (ssh1_handle_direction_specific_packet(s, pktin)) {
  297. pq_pop(s->ppl.in_pq);
  298. if (ssh1_check_termination(s))
  299. return true;
  300. } else {
  301. return false;
  302. }
  303. }
  304. }
  305. }
  306. static PktIn *ssh1_connection_pop(struct ssh1_connection_state *s)
  307. {
  308. ssh1_connection_filter_queue(s);
  309. return pq_pop(s->ppl.in_pq);
  310. }
  311. static void ssh1_connection_process_queue(PacketProtocolLayer *ppl)
  312. {
  313. struct ssh1_connection_state *s =
  314. container_of(ppl, struct ssh1_connection_state, ppl);
  315. PktIn *pktin;
  316. if (ssh1_connection_filter_queue(s)) /* no matter why we were called */
  317. return;
  318. crBegin(s->crState);
  319. /*
  320. * Signal the seat that authentication is done, so that it can
  321. * deploy spoofing defences. If it doesn't have any, deploy our
  322. * own fallback one.
  323. *
  324. * We do this here rather than at the end of userauth, because we
  325. * might not have gone through userauth at all (if we're a
  326. * connection-sharing downstream).
  327. */
  328. if (ssh1_connection_need_antispoof_prompt(s)) {
  329. s->antispoof_prompt = new_prompts();
  330. s->antispoof_prompt->to_server = true;
  331. s->antispoof_prompt->from_server = false;
  332. s->antispoof_prompt->name = dupstr("Authentication successful");
  333. add_prompt(
  334. s->antispoof_prompt,
  335. dupstr("Access granted. Press Return to begin session. "), false);
  336. s->antispoof_ret = seat_get_userpass_input(
  337. s->ppl.seat, s->antispoof_prompt, NULL);
  338. while (1) {
  339. while (s->antispoof_ret < 0 &&
  340. bufchain_size(s->ppl.user_input) > 0)
  341. s->antispoof_ret = seat_get_userpass_input(
  342. s->ppl.seat, s->antispoof_prompt, s->ppl.user_input);
  343. if (s->antispoof_ret >= 0)
  344. break;
  345. s->want_user_input = true;
  346. crReturnV;
  347. s->want_user_input = false;
  348. }
  349. free_prompts(s->antispoof_prompt);
  350. s->antispoof_prompt = NULL;
  351. }
  352. portfwdmgr_config(s->portfwdmgr, s->conf);
  353. s->portfwdmgr_configured = true;
  354. while (!s->finished_setup) {
  355. ssh1_connection_direction_specific_setup(s);
  356. crReturnV;
  357. }
  358. while (1) {
  359. /*
  360. * By this point, most incoming packets are already being
  361. * handled by filter_queue, and we need only pay attention to
  362. * the unusual ones.
  363. */
  364. if ((pktin = ssh1_connection_pop(s)) != NULL) {
  365. ssh_proto_error(s->ppl.ssh, "Unexpected packet received, "
  366. "type %d (%s)", pktin->type,
  367. ssh1_pkt_type(pktin->type));
  368. return;
  369. }
  370. crReturnV;
  371. }
  372. crFinishV;
  373. }
  374. static void ssh1_channel_check_close(struct ssh1_channel *c)
  375. {
  376. struct ssh1_connection_state *s = c->connlayer;
  377. PktOut *pktout;
  378. if (c->halfopen) {
  379. /*
  380. * If we've sent out our own CHANNEL_OPEN but not yet seen
  381. * either OPEN_CONFIRMATION or OPEN_FAILURE in response, then
  382. * it's too early to be sending close messages of any kind.
  383. */
  384. return;
  385. }
  386. if ((!((CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE) & ~c->closes) ||
  387. chan_want_close(c->chan, (c->closes & CLOSES_SENT_CLOSE),
  388. (c->closes & CLOSES_RCVD_CLOSE))) &&
  389. !(c->closes & CLOSES_SENT_CLOSECONF)) {
  390. /*
  391. * We have both sent and received CLOSE (or the channel type
  392. * doesn't need us to), which means the channel is in final
  393. * wind-up. Send CLOSE and/or CLOSE_CONFIRMATION, whichever we
  394. * haven't sent yet.
  395. */
  396. if (!(c->closes & CLOSES_SENT_CLOSE)) {
  397. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_CHANNEL_CLOSE);
  398. put_uint32(pktout, c->remoteid);
  399. pq_push(s->ppl.out_pq, pktout);
  400. c->closes |= CLOSES_SENT_CLOSE;
  401. }
  402. if (c->closes & CLOSES_RCVD_CLOSE) {
  403. pktout = ssh_bpp_new_pktout(
  404. s->ppl.bpp, SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION);
  405. put_uint32(pktout, c->remoteid);
  406. pq_push(s->ppl.out_pq, pktout);
  407. c->closes |= CLOSES_SENT_CLOSECONF;
  408. }
  409. }
  410. if (!((CLOSES_SENT_CLOSECONF | CLOSES_RCVD_CLOSECONF) & ~c->closes)) {
  411. /*
  412. * We have both sent and received CLOSE_CONFIRMATION, which
  413. * means we're completely done with the channel.
  414. */
  415. ssh1_channel_destroy(c);
  416. }
  417. }
  418. static void ssh1_channel_try_eof(struct ssh1_channel *c)
  419. {
  420. struct ssh1_connection_state *s = c->connlayer;
  421. PktOut *pktout;
  422. assert(c->pending_eof); /* precondition for calling us */
  423. if (c->halfopen)
  424. return; /* can't close: not even opened yet */
  425. c->pending_eof = false; /* we're about to send it */
  426. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_CHANNEL_CLOSE);
  427. put_uint32(pktout, c->remoteid);
  428. pq_push(s->ppl.out_pq, pktout);
  429. c->closes |= CLOSES_SENT_CLOSE;
  430. ssh1_channel_check_close(c);
  431. }
  432. /*
  433. * Close any local socket and free any local resources associated with
  434. * a channel. This converts the channel into a zombie.
  435. */
  436. static void ssh1_channel_close_local(struct ssh1_channel *c,
  437. const char *reason)
  438. {
  439. struct ssh1_connection_state *s = c->connlayer;
  440. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  441. char *msg = chan_log_close_msg(c->chan);
  442. if (msg != NULL) {
  443. ppl_logevent("%s%s%s", msg, reason ? " " : "", reason ? reason : "");
  444. sfree(msg);
  445. }
  446. chan_free(c->chan);
  447. c->chan = zombiechan_new();
  448. }
  449. static void ssh1_check_termination_callback(void *vctx)
  450. {
  451. struct ssh1_connection_state *s = (struct ssh1_connection_state *)vctx;
  452. ssh1_check_termination(s);
  453. }
  454. static void ssh1_channel_destroy(struct ssh1_channel *c)
  455. {
  456. struct ssh1_connection_state *s = c->connlayer;
  457. ssh1_channel_close_local(c, NULL);
  458. del234(s->channels, c);
  459. ssh1_channel_free(c);
  460. /*
  461. * If that was the last channel left open, we might need to
  462. * terminate. But we'll be a bit cautious, by doing that in a
  463. * toplevel callback, just in case anything on the current call
  464. * stack objects to this entire PPL being freed.
  465. */
  466. queue_toplevel_callback(ssh1_check_termination_callback, s);
  467. }
  468. bool ssh1_check_termination(struct ssh1_connection_state *s)
  469. {
  470. /*
  471. * Decide whether we should terminate the SSH connection now.
  472. * Called after a channel goes away, or when the main session
  473. * returns SSH1_SMSG_EXIT_STATUS; we terminate when none of either
  474. * is left.
  475. */
  476. if (s->session_terminated && count234(s->channels) == 0) {
  477. PktOut *pktout = ssh_bpp_new_pktout(
  478. s->ppl.bpp, SSH1_CMSG_EXIT_CONFIRMATION);
  479. pq_push(s->ppl.out_pq, pktout);
  480. ssh_user_close(s->ppl.ssh, "Session finished");
  481. return true;
  482. }
  483. return false;
  484. }
  485. /*
  486. * Set up most of a new ssh1_channel. Leaves chan untouched (since it
  487. * will sometimes have been filled in before calling this).
  488. */
  489. void ssh1_channel_init(struct ssh1_channel *c)
  490. {
  491. struct ssh1_connection_state *s = c->connlayer;
  492. c->closes = 0;
  493. c->pending_eof = false;
  494. c->throttling_conn = false;
  495. c->sc.vt = &ssh1channel_vtable;
  496. c->sc.cl = &s->cl;
  497. c->localid = alloc_channel_id(s->channels, struct ssh1_channel);
  498. add234(s->channels, c);
  499. }
  500. static Conf *ssh1channel_get_conf(SshChannel *sc)
  501. {
  502. struct ssh1_channel *c = container_of(sc, struct ssh1_channel, sc);
  503. struct ssh1_connection_state *s = c->connlayer;
  504. return s->conf;
  505. }
  506. static void ssh1channel_write_eof(SshChannel *sc)
  507. {
  508. struct ssh1_channel *c = container_of(sc, struct ssh1_channel, sc);
  509. if (c->closes & CLOSES_SENT_CLOSE)
  510. return;
  511. c->pending_eof = true;
  512. ssh1_channel_try_eof(c);
  513. }
  514. static void ssh1channel_initiate_close(SshChannel *sc, const char *err)
  515. {
  516. struct ssh1_channel *c = container_of(sc, struct ssh1_channel, sc);
  517. char *reason;
  518. reason = err ? dupprintf("due to local error: %s", err) : NULL;
  519. ssh1_channel_close_local(c, reason);
  520. sfree(reason);
  521. c->pending_eof = false; /* this will confuse a zombie channel */
  522. ssh1_channel_check_close(c);
  523. }
  524. static void ssh1channel_unthrottle(SshChannel *sc, size_t bufsize)
  525. {
  526. struct ssh1_channel *c = container_of(sc, struct ssh1_channel, sc);
  527. struct ssh1_connection_state *s = c->connlayer;
  528. if (c->throttling_conn && bufsize <= SSH1_BUFFER_LIMIT) {
  529. c->throttling_conn = false;
  530. ssh_throttle_conn(s->ppl.ssh, -1);
  531. }
  532. }
  533. static size_t ssh1channel_write(
  534. SshChannel *sc, bool is_stderr, const void *buf, size_t len)
  535. {
  536. struct ssh1_channel *c = container_of(sc, struct ssh1_channel, sc);
  537. struct ssh1_connection_state *s = c->connlayer;
  538. pinitassert(!(c->closes & CLOSES_SENT_CLOSE));
  539. PktOut *pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_CHANNEL_DATA);
  540. put_uint32(pktout, c->remoteid);
  541. put_string(pktout, buf, len);
  542. pq_push(s->ppl.out_pq, pktout);
  543. /*
  544. * In SSH-1 we can return 0 here - implying that channels are
  545. * never individually throttled - because the only circumstance
  546. * that can cause throttling will be the whole SSH connection
  547. * backing up, in which case _everything_ will be throttled as a
  548. * whole.
  549. */
  550. return 0;
  551. }
  552. static struct X11FakeAuth *ssh1_add_x11_display(
  553. ConnectionLayer *cl, int authtype, struct X11Display *disp)
  554. {
  555. struct ssh1_connection_state *s =
  556. container_of(cl, struct ssh1_connection_state, cl);
  557. struct X11FakeAuth *auth = x11_invent_fake_auth(s->x11authtree, authtype);
  558. auth->disp = disp;
  559. return auth;
  560. }
  561. static SshChannel *ssh1_lportfwd_open(
  562. ConnectionLayer *cl, const char *hostname, int port,
  563. const char *description, const SocketPeerInfo *pi, Channel *chan)
  564. {
  565. struct ssh1_connection_state *s =
  566. container_of(cl, struct ssh1_connection_state, cl);
  567. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  568. struct ssh1_channel *c = snew(struct ssh1_channel);
  569. PktOut *pktout;
  570. c->connlayer = s;
  571. ssh1_channel_init(c);
  572. c->halfopen = true;
  573. c->chan = chan;
  574. ppl_logevent("Opening connection to %s:%d for %s",
  575. hostname, port, description);
  576. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_PORT_OPEN);
  577. put_uint32(pktout, c->localid);
  578. put_stringz(pktout, hostname);
  579. put_uint32(pktout, port);
  580. /* originator string would go here, but we didn't specify
  581. * SSH_PROTOFLAG_HOST_IN_FWD_OPEN */
  582. pq_push(s->ppl.out_pq, pktout);
  583. return &c->sc;
  584. }
  585. static void ssh1_rportfwd_remove(ConnectionLayer *cl, struct ssh_rportfwd *rpf)
  586. {
  587. /*
  588. * We cannot cancel listening ports on the server side in SSH-1!
  589. * There's no message to support it.
  590. */
  591. }
  592. static bool ssh1_agent_forwarding_permitted(ConnectionLayer *cl)
  593. {
  594. struct ssh1_connection_state *s =
  595. container_of(cl, struct ssh1_connection_state, cl);
  596. return conf_get_bool(s->conf, CONF_agentfwd) && agent_exists();
  597. }
  598. static void ssh1_connection_special_cmd(PacketProtocolLayer *ppl,
  599. SessionSpecialCode code, int arg)
  600. {
  601. struct ssh1_connection_state *s =
  602. container_of(ppl, struct ssh1_connection_state, ppl);
  603. PktOut *pktout;
  604. if (code == SS_PING || code == SS_NOP) {
  605. if (!(s->ppl.remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) {
  606. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_IGNORE);
  607. put_stringz(pktout, "");
  608. pq_push(s->ppl.out_pq, pktout);
  609. }
  610. } else if (s->mainchan) {
  611. mainchan_special_cmd(s->mainchan, code, arg);
  612. }
  613. }
  614. static void ssh1_terminal_size(ConnectionLayer *cl, int width, int height)
  615. {
  616. struct ssh1_connection_state *s =
  617. container_of(cl, struct ssh1_connection_state, cl);
  618. s->term_width = width;
  619. s->term_height = height;
  620. if (s->mainchan)
  621. mainchan_terminal_size(s->mainchan, width, height);
  622. }
  623. static void ssh1_stdout_unthrottle(ConnectionLayer *cl, size_t bufsize)
  624. {
  625. struct ssh1_connection_state *s =
  626. container_of(cl, struct ssh1_connection_state, cl);
  627. if (s->stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) {
  628. s->stdout_throttling = false;
  629. ssh_throttle_conn(s->ppl.ssh, -1);
  630. }
  631. }
  632. static size_t ssh1_stdin_backlog(ConnectionLayer *cl)
  633. {
  634. return 0;
  635. }
  636. static void ssh1_throttle_all_channels(ConnectionLayer *cl, bool throttled)
  637. {
  638. struct ssh1_connection_state *s =
  639. container_of(cl, struct ssh1_connection_state, cl);
  640. struct ssh1_channel *c;
  641. int i;
  642. for (i = 0; NULL != (c = index234(s->channels, i)); i++)
  643. chan_set_input_wanted(c->chan, !throttled);
  644. }
  645. static bool ssh1_ldisc_option(ConnectionLayer *cl, int option)
  646. {
  647. struct ssh1_connection_state *s =
  648. container_of(cl, struct ssh1_connection_state, cl);
  649. return s->ldisc_opts[option];
  650. }
  651. static void ssh1_set_ldisc_option(ConnectionLayer *cl, int option, bool value)
  652. {
  653. struct ssh1_connection_state *s =
  654. container_of(cl, struct ssh1_connection_state, cl);
  655. s->ldisc_opts[option] = value;
  656. }
  657. static void ssh1_enable_x_fwd(ConnectionLayer *cl)
  658. {
  659. struct ssh1_connection_state *s =
  660. container_of(cl, struct ssh1_connection_state, cl);
  661. s->X11_fwd_enabled = true;
  662. }
  663. static void ssh1_set_wants_user_input(ConnectionLayer *cl, bool wanted)
  664. {
  665. struct ssh1_connection_state *s =
  666. container_of(cl, struct ssh1_connection_state, cl);
  667. s->want_user_input = wanted;
  668. s->finished_setup = true;
  669. }
  670. static bool ssh1_connection_want_user_input(PacketProtocolLayer *ppl)
  671. {
  672. struct ssh1_connection_state *s =
  673. container_of(ppl, struct ssh1_connection_state, ppl);
  674. return s->want_user_input;
  675. }
  676. static void ssh1_connection_got_user_input(PacketProtocolLayer *ppl)
  677. {
  678. struct ssh1_connection_state *s =
  679. container_of(ppl, struct ssh1_connection_state, ppl);
  680. while (s->mainchan && bufchain_size(s->ppl.user_input) > 0) {
  681. /*
  682. * Add user input to the main channel's buffer.
  683. */
  684. ptrlen data = bufchain_prefix(s->ppl.user_input);
  685. if (data.len > 512)
  686. data.len = 512;
  687. sshfwd_write(&s->mainchan_sc, data.ptr, data.len);
  688. bufchain_consume(s->ppl.user_input, data.len);
  689. }
  690. }
  691. static void ssh1_connection_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  692. {
  693. struct ssh1_connection_state *s =
  694. container_of(ppl, struct ssh1_connection_state, ppl);
  695. conf_free(s->conf);
  696. s->conf = conf_copy(conf);
  697. if (s->portfwdmgr_configured)
  698. portfwdmgr_config(s->portfwdmgr, s->conf);
  699. }
  700. #include <puttyexp.h>
  701. static unsigned int ssh1_connection_winscp_query(PacketProtocolLayer *ppl, int query)
  702. {
  703. struct ssh1_connection_state *s =
  704. container_of(ppl, struct ssh1_connection_state, ppl);
  705. if (query == WINSCP_QUERY_TIMER)
  706. {
  707. return 0; // dummy
  708. }
  709. else if (query == WINSCP_QUERY_REMMAXPKT)
  710. {
  711. return 0; // dummy
  712. }
  713. else if (query == WINSCP_QUERY_MAIN_CHANNEL)
  714. {
  715. return s->finished_setup;
  716. }
  717. else
  718. {
  719. assert(0);
  720. return 0;
  721. }
  722. }