ssh1connection.c 25 KB

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