ssh1connection.c 26 KB

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