ssh1connection.c 27 KB

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