ssh1connection.c 26 KB

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