connection1.c 26 KB

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