ssh1connection.c 28 KB

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