ssh1connection.c 26 KB

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