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