ssh1connection.c 28 KB

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