ssh1connection.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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. struct ssh1_channel;
  13. struct outstanding_succfail;
  14. struct ssh1_connection_state {
  15. int crState;
  16. Ssh *ssh;
  17. Conf *conf;
  18. int local_protoflags;
  19. tree234 *channels; /* indexed by local id */
  20. int got_pty;
  21. int echoedit;
  22. int ospeed, ispeed;
  23. int stdout_throttling;
  24. int session_ready;
  25. int session_eof_pending, session_eof_sent, session_terminated;
  26. int term_width, term_height, term_width_orig, term_height_orig;
  27. int X11_fwd_enabled;
  28. struct X11Display *x11disp;
  29. struct X11FakeAuth *x11auth;
  30. tree234 *x11authtree;
  31. int agent_fwd_enabled;
  32. tree234 *rportfwds;
  33. PortFwdManager *portfwdmgr;
  34. int portfwdmgr_configured;
  35. int finished_setup;
  36. /*
  37. * These store the list of requests that we're waiting for
  38. * SSH_SMSG_{SUCCESS,FAILURE} replies to. (Those messages don't
  39. * come with any indication of what they're in response to, so we
  40. * have to keep track of the queue ourselves.)
  41. */
  42. struct outstanding_succfail *succfail_head, *succfail_tail;
  43. ConnectionLayer cl;
  44. PacketProtocolLayer ppl;
  45. };
  46. static int ssh1_rportfwd_cmp(void *av, void *bv)
  47. {
  48. struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
  49. struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
  50. int i;
  51. if ( (i = strcmp(a->dhost, b->dhost)) != 0)
  52. return i < 0 ? -1 : +1;
  53. if (a->dport > b->dport)
  54. return +1;
  55. if (a->dport < b->dport)
  56. return -1;
  57. return 0;
  58. }
  59. static void ssh1_connection_free(PacketProtocolLayer *);
  60. static void ssh1_connection_process_queue(PacketProtocolLayer *);
  61. static void ssh1_connection_special_cmd(PacketProtocolLayer *ppl,
  62. SessionSpecialCode code, int arg);
  63. static int ssh1_connection_want_user_input(PacketProtocolLayer *ppl);
  64. static void ssh1_connection_got_user_input(PacketProtocolLayer *ppl);
  65. static void ssh1_connection_reconfigure(PacketProtocolLayer *ppl, Conf *conf);
  66. static const struct PacketProtocolLayerVtable ssh1_connection_vtable = {
  67. ssh1_connection_free,
  68. ssh1_connection_process_queue,
  69. ssh1_common_get_specials,
  70. ssh1_connection_special_cmd,
  71. ssh1_connection_want_user_input,
  72. ssh1_connection_got_user_input,
  73. ssh1_connection_reconfigure,
  74. NULL /* no layer names in SSH-1 */,
  75. };
  76. static struct ssh_rportfwd *ssh1_rportfwd_alloc(
  77. ConnectionLayer *cl,
  78. const char *shost, int sport, const char *dhost, int dport,
  79. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  80. ssh_sharing_connstate *share_ctx);
  81. static void ssh1_rportfwd_remove(
  82. ConnectionLayer *cl, struct ssh_rportfwd *rpf);
  83. static SshChannel *ssh1_lportfwd_open(
  84. ConnectionLayer *cl, const char *hostname, int port,
  85. const char *org, Channel *chan);
  86. static int ssh1_agent_forwarding_permitted(ConnectionLayer *cl);
  87. static void ssh1_terminal_size(ConnectionLayer *cl, int width, int height);
  88. static void ssh1_stdout_unthrottle(ConnectionLayer *cl, int bufsize);
  89. static int ssh1_stdin_backlog(ConnectionLayer *cl);
  90. static void ssh1_throttle_all_channels(ConnectionLayer *cl, int throttled);
  91. static int ssh1_ldisc_option(ConnectionLayer *cl, int option);
  92. static const struct ConnectionLayerVtable ssh1_connlayer_vtable = {
  93. ssh1_rportfwd_alloc,
  94. ssh1_rportfwd_remove,
  95. ssh1_lportfwd_open,
  96. NULL /* add_sharing_x11_display */,
  97. NULL /* remove_sharing_x11_display */,
  98. NULL /* send_packet_from_downstream */,
  99. NULL /* alloc_sharing_channel */,
  100. NULL /* delete_sharing_channel */,
  101. NULL /* sharing_queue_global_request */,
  102. NULL /* sharing_no_more_downstreams */,
  103. ssh1_agent_forwarding_permitted,
  104. ssh1_terminal_size,
  105. ssh1_stdout_unthrottle,
  106. ssh1_stdin_backlog,
  107. ssh1_throttle_all_channels,
  108. ssh1_ldisc_option,
  109. };
  110. struct ssh1_channel {
  111. struct ssh1_connection_state *connlayer;
  112. unsigned remoteid, localid;
  113. int type;
  114. /* True if we opened this channel but server hasn't confirmed. */
  115. int halfopen;
  116. /* Bitmap of whether we've sent/received CHANNEL_CLOSE and
  117. * CHANNEL_CLOSE_CONFIRMATION. */
  118. #define CLOSES_SENT_CLOSE 1
  119. #define CLOSES_SENT_CLOSECONF 2
  120. #define CLOSES_RCVD_CLOSE 4
  121. #define CLOSES_RCVD_CLOSECONF 8
  122. int closes;
  123. /*
  124. * This flag indicates that an EOF is pending on the outgoing side
  125. * of the channel: that is, wherever we're getting the data for
  126. * this channel has sent us some data followed by EOF. We can't
  127. * actually send the EOF until we've finished sending the data, so
  128. * we set this flag instead to remind us to do so once our buffer
  129. * is clear.
  130. */
  131. int pending_eof;
  132. /*
  133. * True if this channel is causing the underlying connection to be
  134. * throttled.
  135. */
  136. int throttling_conn;
  137. /*
  138. * True if we currently have backed-up data on the direction of
  139. * this channel pointing out of the SSH connection, and therefore
  140. * would prefer the 'Channel' implementation not to read further
  141. * local input if possible.
  142. */
  143. int throttled_by_backlog;
  144. Channel *chan; /* handle the client side of this channel, if not */
  145. SshChannel sc; /* entry point for chan to talk back to */
  146. };
  147. static int ssh1channel_write(SshChannel *c, const void *buf, int len);
  148. static void ssh1channel_write_eof(SshChannel *c);
  149. static void ssh1channel_unclean_close(SshChannel *c, const char *err);
  150. static void ssh1channel_unthrottle(SshChannel *c, int bufsize);
  151. static Conf *ssh1channel_get_conf(SshChannel *c);
  152. static const struct SshChannelVtable ssh1channel_vtable = {
  153. ssh1channel_write,
  154. ssh1channel_write_eof,
  155. ssh1channel_unclean_close,
  156. ssh1channel_unthrottle,
  157. ssh1channel_get_conf,
  158. NULL /* window_override_removed is only used by SSH-2 sharing */,
  159. NULL /* x11_sharing_handover, likewise */,
  160. };
  161. static void ssh1_channel_init(struct ssh1_channel *c);
  162. static void ssh1_channel_try_eof(struct ssh1_channel *c);
  163. static void ssh1_channel_close_local(struct ssh1_channel *c,
  164. const char *reason);
  165. static void ssh1_channel_destroy(struct ssh1_channel *c);
  166. static void ssh1_channel_check_close(struct ssh1_channel *c);
  167. static int ssh1_check_termination(struct ssh1_connection_state *s);
  168. typedef void (*sf_handler_fn_t)(struct ssh1_connection_state *s,
  169. PktIn *pktin, void *ctx);
  170. struct outstanding_succfail {
  171. sf_handler_fn_t handler;
  172. void *ctx;
  173. struct outstanding_succfail *next;
  174. };
  175. static void ssh1_queue_succfail_handler(
  176. struct ssh1_connection_state *s, sf_handler_fn_t handler, void *ctx)
  177. {
  178. struct outstanding_succfail *osf =
  179. snew(struct outstanding_succfail);
  180. osf->handler = handler;
  181. osf->ctx = ctx;
  182. if (s->succfail_tail)
  183. s->succfail_tail->next = osf;
  184. else
  185. s->succfail_head = osf;
  186. s->succfail_tail = osf;
  187. }
  188. static int ssh1_channelcmp(void *av, void *bv)
  189. {
  190. const struct ssh1_channel *a = (const struct ssh1_channel *) av;
  191. const struct ssh1_channel *b = (const struct ssh1_channel *) bv;
  192. if (a->localid < b->localid)
  193. return -1;
  194. if (a->localid > b->localid)
  195. return +1;
  196. return 0;
  197. }
  198. static int ssh1_channelfind(void *av, void *bv)
  199. {
  200. const unsigned *a = (const unsigned *) av;
  201. const struct ssh1_channel *b = (const struct ssh1_channel *) bv;
  202. if (*a < b->localid)
  203. return -1;
  204. if (*a > b->localid)
  205. return +1;
  206. return 0;
  207. }
  208. static void ssh1_channel_free(struct ssh1_channel *c)
  209. {
  210. if (c->chan)
  211. chan_free(c->chan);
  212. sfree(c);
  213. }
  214. PacketProtocolLayer *ssh1_connection_new(
  215. Ssh *ssh, Conf *conf, ConnectionLayer **cl_out)
  216. {
  217. struct ssh1_connection_state *s = snew(struct ssh1_connection_state);
  218. memset(s, 0, sizeof(*s));
  219. s->ppl.vt = &ssh1_connection_vtable;
  220. s->conf = conf_copy(conf);
  221. s->channels = newtree234(ssh1_channelcmp);
  222. s->x11authtree = newtree234(x11_authcmp);
  223. /* Need to get the frontend for s->cl now, because we won't be
  224. * helpfully notified when a copy is written into s->ppl by our
  225. * owner. */
  226. s->cl.vt = &ssh1_connlayer_vtable;
  227. s->cl.frontend = ssh_get_frontend(ssh);
  228. s->portfwdmgr = portfwdmgr_new(&s->cl);
  229. s->rportfwds = newtree234(ssh1_rportfwd_cmp);
  230. *cl_out = &s->cl;
  231. return &s->ppl;
  232. }
  233. static void ssh1_connection_free(PacketProtocolLayer *ppl)
  234. {
  235. struct ssh1_connection_state *s =
  236. container_of(ppl, struct ssh1_connection_state, ppl);
  237. struct X11FakeAuth *auth;
  238. struct ssh1_channel *c;
  239. struct ssh_rportfwd *rpf;
  240. conf_free(s->conf);
  241. while ((c = delpos234(s->channels, 0)) != NULL)
  242. ssh1_channel_free(c);
  243. freetree234(s->channels);
  244. if (s->x11disp)
  245. x11_free_display(s->x11disp);
  246. while ((auth = delpos234(s->x11authtree, 0)) != NULL)
  247. x11_free_fake_auth(auth);
  248. freetree234(s->x11authtree);
  249. while ((rpf = delpos234(s->rportfwds, 0)) != NULL)
  250. free_rportfwd(rpf);
  251. freetree234(s->rportfwds);
  252. portfwdmgr_free(s->portfwdmgr);
  253. sfree(s);
  254. }
  255. void ssh1_connection_set_local_protoflags(PacketProtocolLayer *ppl, int flags)
  256. {
  257. pinitassert(ppl->vt == &ssh1_connection_vtable);
  258. struct ssh1_connection_state *s =
  259. container_of(ppl, struct ssh1_connection_state, ppl);
  260. s->local_protoflags = flags;
  261. }
  262. static int ssh1_connection_filter_queue(struct ssh1_connection_state *s)
  263. {
  264. PktIn *pktin;
  265. PktOut *pktout;
  266. ptrlen data, host;
  267. struct ssh1_channel *c;
  268. unsigned localid, remid;
  269. int port, expect_halfopen;
  270. struct ssh_rportfwd pf, *pfp;
  271. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  272. /* Cross-reference to ssh1login.c to handle the common packets
  273. * between login and connection: DISCONNECT, DEBUG and IGNORE. */
  274. extern int ssh1_common_filter_queue(PacketProtocolLayer *ppl);
  275. while (1) {
  276. if (ssh1_common_filter_queue(&s->ppl))
  277. return TRUE;
  278. if ((pktin = pq_peek(s->ppl.in_pq)) == NULL)
  279. return FALSE;
  280. switch (pktin->type) {
  281. case SSH1_SMSG_SUCCESS:
  282. case SSH1_SMSG_FAILURE:
  283. if (!s->finished_setup) {
  284. /* During initial setup, these messages are not
  285. * filtered out, but go back to the main coroutine. */
  286. return FALSE;
  287. }
  288. if (!s->succfail_head) {
  289. ssh_remote_error(s->ppl.ssh,
  290. "Received %s with no outstanding request",
  291. ssh1_pkt_type(pktin->type));
  292. return TRUE;
  293. }
  294. s->succfail_head->handler(s, pktin, s->succfail_head->ctx);
  295. {
  296. struct outstanding_succfail *tmp = s->succfail_head;
  297. s->succfail_head = s->succfail_head->next;
  298. sfree(tmp);
  299. }
  300. pq_pop(s->ppl.in_pq);
  301. break;
  302. case SSH1_SMSG_X11_OPEN:
  303. remid = get_uint32(pktin);
  304. /* Refuse if X11 forwarding is disabled. */
  305. if (!s->X11_fwd_enabled) {
  306. pktout = ssh_bpp_new_pktout(
  307. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_FAILURE);
  308. put_uint32(pktout, remid);
  309. pq_push(s->ppl.out_pq, pktout);
  310. ppl_logevent(("Rejected X11 connect request"));
  311. } else {
  312. c = snew(struct ssh1_channel);
  313. c->connlayer = s;
  314. ssh1_channel_init(c);
  315. c->remoteid = remid;
  316. c->chan = x11_new_channel(s->x11authtree, &c->sc,
  317. NULL, -1, FALSE);
  318. c->remoteid = remid;
  319. c->halfopen = FALSE;
  320. pktout = ssh_bpp_new_pktout(
  321. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION);
  322. put_uint32(pktout, c->remoteid);
  323. put_uint32(pktout, c->localid);
  324. pq_push(s->ppl.out_pq, pktout);
  325. ppl_logevent(("Opened X11 forward channel"));
  326. }
  327. pq_pop(s->ppl.in_pq);
  328. break;
  329. case SSH1_SMSG_AGENT_OPEN:
  330. remid = get_uint32(pktin);
  331. /* Refuse if agent forwarding is disabled. */
  332. if (!s->agent_fwd_enabled) {
  333. pktout = ssh_bpp_new_pktout(
  334. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_FAILURE);
  335. put_uint32(pktout, remid);
  336. pq_push(s->ppl.out_pq, pktout);
  337. } else {
  338. c = snew(struct ssh1_channel);
  339. c->connlayer = s;
  340. ssh1_channel_init(c);
  341. c->remoteid = remid;
  342. c->chan = agentf_new(&c->sc);
  343. c->halfopen = FALSE;
  344. pktout = ssh_bpp_new_pktout(
  345. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION);
  346. put_uint32(pktout, c->remoteid);
  347. put_uint32(pktout, c->localid);
  348. pq_push(s->ppl.out_pq, pktout);
  349. }
  350. pq_pop(s->ppl.in_pq);
  351. break;
  352. case SSH1_MSG_PORT_OPEN:
  353. remid = get_uint32(pktin);
  354. host = get_string(pktin);
  355. port = toint(get_uint32(pktin));
  356. pf.dhost = mkstr(host);
  357. pf.dport = port;
  358. pfp = find234(s->rportfwds, &pf, NULL);
  359. if (!pfp) {
  360. ppl_logevent(("Rejected remote port open request for %s:%d",
  361. pf.dhost, port));
  362. pktout = ssh_bpp_new_pktout(
  363. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_FAILURE);
  364. put_uint32(pktout, remid);
  365. pq_push(s->ppl.out_pq, pktout);
  366. } else {
  367. char *err;
  368. c = snew(struct ssh1_channel);
  369. c->connlayer = s;
  370. ppl_logevent(("Received remote port open request for %s:%d",
  371. pf.dhost, port));
  372. err = portfwdmgr_connect(
  373. s->portfwdmgr, &c->chan, pf.dhost, port,
  374. &c->sc, pfp->addressfamily);
  375. if (err) {
  376. ppl_logevent(("Port open failed: %s", err));
  377. sfree(err);
  378. ssh1_channel_free(c);
  379. pktout = ssh_bpp_new_pktout(
  380. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_FAILURE);
  381. put_uint32(pktout, remid);
  382. pq_push(s->ppl.out_pq, pktout);
  383. } else {
  384. ssh1_channel_init(c);
  385. c->remoteid = remid;
  386. c->halfopen = FALSE;
  387. pktout = ssh_bpp_new_pktout(
  388. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION);
  389. put_uint32(pktout, c->remoteid);
  390. put_uint32(pktout, c->localid);
  391. pq_push(s->ppl.out_pq, pktout);
  392. ppl_logevent(("Forwarded port opened successfully"));
  393. }
  394. }
  395. sfree(pf.dhost);
  396. pq_pop(s->ppl.in_pq);
  397. break;
  398. case SSH1_MSG_CHANNEL_DATA:
  399. case SSH1_MSG_CHANNEL_OPEN_CONFIRMATION:
  400. case SSH1_MSG_CHANNEL_OPEN_FAILURE:
  401. case SSH1_MSG_CHANNEL_CLOSE:
  402. case SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION:
  403. /*
  404. * Common preliminary code for all the messages from the
  405. * server that cite one of our channel ids: look up that
  406. * channel id, check it exists, and if it's for a sharing
  407. * downstream, pass it on.
  408. */
  409. localid = get_uint32(pktin);
  410. c = find234(s->channels, &localid, ssh1_channelfind);
  411. expect_halfopen = (
  412. pktin->type == SSH1_MSG_CHANNEL_OPEN_CONFIRMATION ||
  413. pktin->type == SSH1_MSG_CHANNEL_OPEN_FAILURE);
  414. if (!c || c->halfopen != expect_halfopen) {
  415. ssh_remote_error(
  416. s->ppl.ssh, "Received %s for %s channel %u",
  417. ssh1_pkt_type(pktin->type),
  418. !c ? "nonexistent" : c->halfopen ? "half-open" : "open",
  419. localid);
  420. return TRUE;
  421. }
  422. switch (pktin->type) {
  423. case SSH1_MSG_CHANNEL_OPEN_CONFIRMATION:
  424. assert(c->halfopen);
  425. c->remoteid = get_uint32(pktin);
  426. c->halfopen = FALSE;
  427. c->throttling_conn = FALSE;
  428. chan_open_confirmation(c->chan);
  429. /*
  430. * Now that the channel is fully open, it's possible
  431. * in principle to immediately close it. Check whether
  432. * it wants us to!
  433. *
  434. * This can occur if a local socket error occurred
  435. * between us sending out CHANNEL_OPEN and receiving
  436. * OPEN_CONFIRMATION. If that happens, all we can do
  437. * is immediately initiate close proceedings now that
  438. * we know the server's id to put in the close
  439. * message. We'll have handled that in this code by
  440. * having already turned c->chan into a zombie, so its
  441. * want_close method (which ssh1_channel_check_close
  442. * will consult) will already be returning TRUE.
  443. */
  444. ssh1_channel_check_close(c);
  445. if (c->pending_eof)
  446. ssh1_channel_try_eof(c); /* in case we had a pending EOF */
  447. break;
  448. case SSH1_MSG_CHANNEL_OPEN_FAILURE:
  449. assert(c->halfopen);
  450. chan_open_failed(c->chan, NULL);
  451. chan_free(c->chan);
  452. del234(s->channels, c);
  453. ssh1_channel_free(c);
  454. break;
  455. case SSH1_MSG_CHANNEL_DATA:
  456. data = get_string(pktin);
  457. if (!get_err(pktin)) {
  458. int bufsize = chan_send(
  459. c->chan, FALSE, data.ptr, data.len);
  460. if (!c->throttling_conn && bufsize > SSH1_BUFFER_LIMIT) {
  461. c->throttling_conn = TRUE;
  462. ssh_throttle_conn(s->ppl.ssh, +1);
  463. }
  464. }
  465. break;
  466. case SSH1_MSG_CHANNEL_CLOSE:
  467. if (!(c->closes & CLOSES_RCVD_CLOSE)) {
  468. c->closes |= CLOSES_RCVD_CLOSE;
  469. chan_send_eof(c->chan);
  470. ssh1_channel_check_close(c);
  471. }
  472. break;
  473. case SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION:
  474. if (!(c->closes & CLOSES_RCVD_CLOSECONF)) {
  475. if (!(c->closes & CLOSES_SENT_CLOSE)) {
  476. ssh_remote_error(
  477. s->ppl.ssh,
  478. "Received CHANNEL_CLOSE_CONFIRMATION for channel"
  479. " %u for which we never sent CHANNEL_CLOSE\n",
  480. c->localid);
  481. return TRUE;
  482. }
  483. c->closes |= CLOSES_RCVD_CLOSECONF;
  484. ssh1_channel_check_close(c);
  485. }
  486. break;
  487. }
  488. pq_pop(s->ppl.in_pq);
  489. break;
  490. case SSH1_SMSG_STDOUT_DATA:
  491. case SSH1_SMSG_STDERR_DATA:
  492. data = get_string(pktin);
  493. if (!get_err(pktin)) {
  494. int bufsize = from_backend(
  495. s->ppl.frontend, pktin->type == SSH1_SMSG_STDERR_DATA,
  496. data.ptr, data.len);
  497. if (!s->stdout_throttling && bufsize > SSH1_BUFFER_LIMIT) {
  498. s->stdout_throttling = 1;
  499. ssh_throttle_conn(s->ppl.ssh, +1);
  500. }
  501. }
  502. pq_pop(s->ppl.in_pq);
  503. break;
  504. case SSH1_SMSG_EXIT_STATUS:
  505. {
  506. int exitcode = get_uint32(pktin);
  507. ppl_logevent(("Server sent command exit status %d", exitcode));
  508. ssh_got_exitcode(s->ppl.ssh, exitcode);
  509. s->session_terminated = TRUE;
  510. if (ssh1_check_termination(s))
  511. return TRUE;
  512. }
  513. pq_pop(s->ppl.in_pq);
  514. break;
  515. default:
  516. return FALSE;
  517. }
  518. }
  519. }
  520. static PktIn *ssh1_connection_pop(struct ssh1_connection_state *s)
  521. {
  522. ssh1_connection_filter_queue(s);
  523. return pq_pop(s->ppl.in_pq);
  524. }
  525. static void ssh1_connection_process_queue(PacketProtocolLayer *ppl)
  526. {
  527. struct ssh1_connection_state *s =
  528. container_of(ppl, struct ssh1_connection_state, ppl);
  529. PktIn *pktin;
  530. PktOut *pktout;
  531. if (ssh1_connection_filter_queue(s)) /* no matter why we were called */
  532. return;
  533. crBegin(s->crState);
  534. if (ssh_agent_forwarding_permitted(&s->cl)) {
  535. ppl_logevent(("Requesting agent forwarding"));
  536. pktout = ssh_bpp_new_pktout(s->ppl.bpp,
  537. SSH1_CMSG_AGENT_REQUEST_FORWARDING);
  538. pq_push(s->ppl.out_pq, pktout);
  539. crMaybeWaitUntilV((pktin = ssh1_connection_pop(s)) != NULL);
  540. if (pktin->type == SSH1_SMSG_SUCCESS) {
  541. ppl_logevent(("Agent forwarding enabled"));
  542. s->agent_fwd_enabled = TRUE;
  543. } else if (pktin->type == SSH1_SMSG_FAILURE) {
  544. ppl_logevent(("Agent forwarding refused"));
  545. } else {
  546. ssh_proto_error(s->ppl.ssh, "Unexpected packet received"
  547. " in response to agent forwarding request, "
  548. "type %d (%s)", pktin->type,
  549. ssh1_pkt_type(pktin->type));
  550. return;
  551. }
  552. }
  553. if (conf_get_int(s->conf, CONF_x11_forward)) {
  554. char *x11_setup_err;
  555. s->x11disp =
  556. x11_setup_display(conf_get_str(s->conf, CONF_x11_display),
  557. s->conf, &x11_setup_err);
  558. if (!s->x11disp) {
  559. ppl_logevent(("X11 forwarding not enabled: unable to"
  560. " initialise X display: %s", x11_setup_err));
  561. sfree(x11_setup_err);
  562. } else {
  563. s->x11auth = x11_invent_fake_auth
  564. (s->x11authtree, conf_get_int(s->conf, CONF_x11_auth));
  565. s->x11auth->disp = s->x11disp;
  566. ppl_logevent(("Requesting X11 forwarding"));
  567. pktout = ssh_bpp_new_pktout(
  568. s->ppl.bpp, SSH1_CMSG_X11_REQUEST_FORWARDING);
  569. put_stringz(pktout, s->x11auth->protoname);
  570. put_stringz(pktout, s->x11auth->datastring);
  571. if (s->local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER)
  572. put_uint32(pktout, s->x11disp->screennum);
  573. pq_push(s->ppl.out_pq, pktout);
  574. crMaybeWaitUntilV((pktin = ssh1_connection_pop(s)) != NULL);
  575. if (pktin->type == SSH1_SMSG_SUCCESS) {
  576. ppl_logevent(("X11 forwarding enabled"));
  577. s->X11_fwd_enabled = TRUE;
  578. } else if (pktin->type == SSH1_SMSG_FAILURE) {
  579. ppl_logevent(("X11 forwarding refused"));
  580. } else {
  581. ssh_proto_error(s->ppl.ssh, "Unexpected packet received"
  582. " in response to X11 forwarding request, "
  583. "type %d (%s)", pktin->type,
  584. ssh1_pkt_type(pktin->type));
  585. return;
  586. }
  587. }
  588. }
  589. portfwdmgr_config(s->portfwdmgr, s->conf);
  590. s->portfwdmgr_configured = TRUE;
  591. if (!conf_get_int(s->conf, CONF_nopty)) {
  592. /* Unpick the terminal-speed string. */
  593. /* XXX perhaps we should allow no speeds to be sent. */
  594. s->ospeed = 38400; s->ispeed = 38400; /* last-resort defaults */
  595. sscanf(conf_get_str(s->conf, CONF_termspeed), "%d,%d",
  596. &s->ospeed, &s->ispeed);
  597. /* Send the pty request. */
  598. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_REQUEST_PTY);
  599. put_stringz(pktout, conf_get_str(s->conf, CONF_termtype));
  600. put_uint32(pktout, s->term_height);
  601. put_uint32(pktout, s->term_width);
  602. s->term_width_orig = s->term_width;
  603. s->term_height_orig = s->term_height;
  604. put_uint32(pktout, 0); /* width in pixels */
  605. put_uint32(pktout, 0); /* height in pixels */
  606. write_ttymodes_to_packet_from_conf(
  607. BinarySink_UPCAST(pktout), s->ppl.frontend, s->conf,
  608. 1, s->ospeed, s->ispeed);
  609. pq_push(s->ppl.out_pq, pktout);
  610. crMaybeWaitUntilV((pktin = ssh1_connection_pop(s)) != NULL);
  611. if (pktin->type == SSH1_SMSG_SUCCESS) {
  612. ppl_logevent(("Allocated pty (ospeed %dbps, ispeed %dbps)",
  613. s->ospeed, s->ispeed));
  614. s->got_pty = TRUE;
  615. } else if (pktin->type == SSH1_SMSG_FAILURE) {
  616. ppl_printf(("Server refused to allocate pty\r\n"));
  617. s->echoedit = TRUE;
  618. } else {
  619. ssh_proto_error(s->ppl.ssh, "Unexpected packet received"
  620. " in response to pty request, "
  621. "type %d (%s)", pktin->type,
  622. ssh1_pkt_type(pktin->type));
  623. crStopV;
  624. }
  625. } else {
  626. s->echoedit = TRUE;
  627. }
  628. /*
  629. * Start the shell or command.
  630. *
  631. * Special case: if the first-choice command is an SSH-2
  632. * subsystem (hence not usable here) and the second choice
  633. * exists, we fall straight back to that.
  634. */
  635. {
  636. char *cmd = conf_get_str(s->conf, CONF_remote_cmd);
  637. if (conf_get_int(s->conf, CONF_ssh_subsys) &&
  638. conf_get_str(s->conf, CONF_remote_cmd2)) {
  639. cmd = conf_get_str(s->conf, CONF_remote_cmd2);
  640. ssh_got_fallback_cmd(s->ppl.ssh);
  641. }
  642. if (*cmd) {
  643. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_EXEC_CMD);
  644. put_stringz(pktout, cmd);
  645. } else {
  646. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_EXEC_SHELL);
  647. }
  648. pq_push(s->ppl.out_pq, pktout);
  649. ppl_logevent(("Started session"));
  650. }
  651. s->session_ready = TRUE;
  652. ssh_ppl_got_user_input(&s->ppl); /* in case any input is already queued */
  653. /* If an EOF or a window-size change arrived before we were ready
  654. * to handle either one, handle them now. */
  655. if (s->session_eof_pending) {
  656. ssh_ppl_special_cmd(&s->ppl, SS_EOF, 0);
  657. s->session_eof_pending = FALSE;
  658. }
  659. if (s->term_width_orig != s->term_width ||
  660. s->term_height_orig != s->term_height)
  661. ssh_terminal_size(&s->cl, s->term_width, s->term_height);
  662. ssh_ldisc_update(s->ppl.ssh);
  663. s->finished_setup = TRUE;
  664. while (1) {
  665. /*
  666. * By this point, most incoming packets are already being
  667. * handled by filter_queue, and we need only pay attention to
  668. * the unusual ones.
  669. */
  670. if ((pktin = ssh1_connection_pop(s)) != NULL) {
  671. ssh_proto_error(s->ppl.ssh, "Unexpected packet received, "
  672. "type %d (%s)", pktin->type,
  673. ssh1_pkt_type(pktin->type));
  674. return;
  675. }
  676. while (bufchain_size(s->ppl.user_input) > 0) {
  677. void *data;
  678. int len;
  679. bufchain_prefix(s->ppl.user_input, &data, &len);
  680. if (len > 512)
  681. len = 512;
  682. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_STDIN_DATA);
  683. put_string(pktout, data, len);
  684. pq_push(s->ppl.out_pq, pktout);
  685. bufchain_consume(s->ppl.user_input, len);
  686. }
  687. crReturnV;
  688. }
  689. crFinishV;
  690. }
  691. static void ssh1_channel_check_close(struct ssh1_channel *c)
  692. {
  693. struct ssh1_connection_state *s = c->connlayer;
  694. PktOut *pktout;
  695. if (c->halfopen) {
  696. /*
  697. * If we've sent out our own CHANNEL_OPEN but not yet seen
  698. * either OPEN_CONFIRMATION or OPEN_FAILURE in response, then
  699. * it's too early to be sending close messages of any kind.
  700. */
  701. return;
  702. }
  703. if ((!((CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE) & ~c->closes) ||
  704. chan_want_close(c->chan, (c->closes & CLOSES_SENT_CLOSE),
  705. (c->closes & CLOSES_RCVD_CLOSE))) &&
  706. !(c->closes & CLOSES_SENT_CLOSECONF)) {
  707. /*
  708. * We have both sent and received CLOSE (or the channel type
  709. * doesn't need us to), which means the channel is in final
  710. * wind-up. Send CLOSE and/or CLOSE_CONFIRMATION, whichever we
  711. * haven't sent yet.
  712. */
  713. if (!(c->closes & CLOSES_SENT_CLOSE)) {
  714. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_CHANNEL_CLOSE);
  715. put_uint32(pktout, c->remoteid);
  716. pq_push(s->ppl.out_pq, pktout);
  717. c->closes |= CLOSES_SENT_CLOSE;
  718. }
  719. if (c->closes & CLOSES_RCVD_CLOSE) {
  720. pktout = ssh_bpp_new_pktout(
  721. s->ppl.bpp, SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION);
  722. put_uint32(pktout, c->remoteid);
  723. pq_push(s->ppl.out_pq, pktout);
  724. c->closes |= CLOSES_SENT_CLOSECONF;
  725. }
  726. }
  727. if (!((CLOSES_SENT_CLOSECONF | CLOSES_RCVD_CLOSECONF) & ~c->closes)) {
  728. /*
  729. * We have both sent and received CLOSE_CONFIRMATION, which
  730. * means we're completely done with the channel.
  731. */
  732. ssh1_channel_destroy(c);
  733. }
  734. }
  735. static void ssh1_channel_try_eof(struct ssh1_channel *c)
  736. {
  737. struct ssh1_connection_state *s = c->connlayer;
  738. PktOut *pktout;
  739. assert(c->pending_eof); /* precondition for calling us */
  740. if (c->halfopen)
  741. return; /* can't close: not even opened yet */
  742. c->pending_eof = FALSE; /* we're about to send it */
  743. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_CHANNEL_CLOSE);
  744. put_uint32(pktout, c->remoteid);
  745. pq_push(s->ppl.out_pq, pktout);
  746. c->closes |= CLOSES_SENT_CLOSE;
  747. ssh1_channel_check_close(c);
  748. }
  749. /*
  750. * Close any local socket and free any local resources associated with
  751. * a channel. This converts the channel into a zombie.
  752. */
  753. static void ssh1_channel_close_local(struct ssh1_channel *c,
  754. const char *reason)
  755. {
  756. struct ssh1_connection_state *s = c->connlayer;
  757. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  758. const char *msg = chan_log_close_msg(c->chan);
  759. if (msg != NULL)
  760. ppl_logevent(("%s%s%s", msg, reason ? " " : "", reason ? reason : ""));
  761. chan_free(c->chan);
  762. c->chan = zombiechan_new();
  763. }
  764. static void ssh1_check_termination_callback(void *vctx)
  765. {
  766. struct ssh1_connection_state *s = (struct ssh1_connection_state *)vctx;
  767. ssh1_check_termination(s);
  768. }
  769. static void ssh1_channel_destroy(struct ssh1_channel *c)
  770. {
  771. struct ssh1_connection_state *s = c->connlayer;
  772. ssh1_channel_close_local(c, NULL);
  773. del234(s->channels, c);
  774. ssh1_channel_free(c);
  775. /*
  776. * If that was the last channel left open, we might need to
  777. * terminate. But we'll be a bit cautious, by doing that in a
  778. * toplevel callback, just in case anything on the current call
  779. * stack objects to this entire PPL being freed.
  780. */
  781. queue_toplevel_callback(s->cl.frontend, ssh1_check_termination_callback, s); // WINSCP
  782. }
  783. static int ssh1_check_termination(struct ssh1_connection_state *s)
  784. {
  785. /*
  786. * Decide whether we should terminate the SSH connection now.
  787. * Called after a channel goes away, or when the main session
  788. * returns SSH1_SMSG_EXIT_STATUS; we terminate when none of either
  789. * is left.
  790. */
  791. if (s->session_terminated && count234(s->channels) == 0) {
  792. PktOut *pktout = ssh_bpp_new_pktout(
  793. s->ppl.bpp, SSH1_CMSG_EXIT_CONFIRMATION);
  794. pq_push(s->ppl.out_pq, pktout);
  795. ssh_user_close(s->ppl.ssh, "Session finished");
  796. return TRUE;
  797. }
  798. return FALSE;
  799. }
  800. /*
  801. * Set up most of a new ssh1_channel. Leaves chan untouched (since it
  802. * will sometimes have been filled in before calling this).
  803. */
  804. static void ssh1_channel_init(struct ssh1_channel *c)
  805. {
  806. struct ssh1_connection_state *s = c->connlayer;
  807. c->closes = 0;
  808. c->pending_eof = FALSE;
  809. c->throttling_conn = FALSE;
  810. c->sc.vt = &ssh1channel_vtable;
  811. c->localid = alloc_channel_id(s->channels, struct ssh1_channel);
  812. add234(s->channels, c);
  813. }
  814. static Conf *ssh1channel_get_conf(SshChannel *sc)
  815. {
  816. struct ssh1_channel *c = container_of(sc, struct ssh1_channel, sc);
  817. struct ssh1_connection_state *s = c->connlayer;
  818. return s->conf;
  819. }
  820. static void ssh1channel_write_eof(SshChannel *sc)
  821. {
  822. struct ssh1_channel *c = container_of(sc, struct ssh1_channel, sc);
  823. if (c->closes & CLOSES_SENT_CLOSE)
  824. return;
  825. c->pending_eof = TRUE;
  826. ssh1_channel_try_eof(c);
  827. }
  828. static void ssh1channel_unclean_close(SshChannel *sc, const char *err)
  829. {
  830. struct ssh1_channel *c = container_of(sc, struct ssh1_channel, sc);
  831. char *reason;
  832. reason = dupprintf("due to local error: %s", err);
  833. ssh1_channel_close_local(c, reason);
  834. sfree(reason);
  835. c->pending_eof = FALSE; /* this will confuse a zombie channel */
  836. ssh1_channel_check_close(c);
  837. }
  838. static void ssh1channel_unthrottle(SshChannel *sc, int bufsize)
  839. {
  840. struct ssh1_channel *c = container_of(sc, struct ssh1_channel, sc);
  841. struct ssh1_connection_state *s = c->connlayer;
  842. if (c->throttling_conn && bufsize <= SSH1_BUFFER_LIMIT) {
  843. c->throttling_conn = 0;
  844. ssh_throttle_conn(s->ppl.ssh, -1);
  845. }
  846. }
  847. static int ssh1channel_write(SshChannel *sc, const void *buf, int len)
  848. {
  849. struct ssh1_channel *c = container_of(sc, struct ssh1_channel, sc);
  850. struct ssh1_connection_state *s = c->connlayer;
  851. pinitassert(!(c->closes & CLOSES_SENT_CLOSE));
  852. PktOut *pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_CHANNEL_DATA);
  853. put_uint32(pktout, c->remoteid);
  854. put_string(pktout, buf, len);
  855. pq_push(s->ppl.out_pq, pktout);
  856. /*
  857. * In SSH-1 we can return 0 here - implying that channels are
  858. * never individually throttled - because the only circumstance
  859. * that can cause throttling will be the whole SSH connection
  860. * backing up, in which case _everything_ will be throttled as a
  861. * whole.
  862. */
  863. return 0;
  864. }
  865. static SshChannel *ssh1_lportfwd_open(
  866. ConnectionLayer *cl, const char *hostname, int port,
  867. const char *org, Channel *chan)
  868. {
  869. struct ssh1_connection_state *s =
  870. container_of(cl, struct ssh1_connection_state, cl);
  871. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  872. struct ssh1_channel *c = snew(struct ssh1_channel);
  873. PktOut *pktout;
  874. c->connlayer = s;
  875. ssh1_channel_init(c);
  876. c->halfopen = TRUE;
  877. c->chan = chan;
  878. ppl_logevent(("Opening connection to %s:%d for %s", hostname, port, org));
  879. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_PORT_OPEN);
  880. put_uint32(pktout, c->localid);
  881. put_stringz(pktout, hostname);
  882. put_uint32(pktout, port);
  883. /* originator string would go here, but we didn't specify
  884. * SSH_PROTOFLAG_HOST_IN_FWD_OPEN */
  885. pq_push(s->ppl.out_pq, pktout);
  886. return &c->sc;
  887. }
  888. static void ssh1_rportfwd_response(struct ssh1_connection_state *s,
  889. PktIn *pktin, void *ctx)
  890. {
  891. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  892. struct ssh_rportfwd *rpf = (struct ssh_rportfwd *)ctx;
  893. if (pktin->type == SSH1_SMSG_SUCCESS) {
  894. ppl_logevent(("Remote port forwarding from %s enabled",
  895. rpf->log_description));
  896. } else {
  897. ppl_logevent(("Remote port forwarding from %s refused",
  898. rpf->log_description));
  899. { // WINSCP
  900. struct ssh_rportfwd *realpf = del234(s->rportfwds, rpf);
  901. assert(realpf == rpf);
  902. portfwdmgr_close(s->portfwdmgr, rpf->pfr);
  903. free_rportfwd(rpf);
  904. } // WINSCP
  905. }
  906. }
  907. static struct ssh_rportfwd *ssh1_rportfwd_alloc(
  908. ConnectionLayer *cl,
  909. const char *shost, int sport, const char *dhost, int dport,
  910. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  911. ssh_sharing_connstate *share_ctx)
  912. {
  913. struct ssh1_connection_state *s =
  914. container_of(cl, struct ssh1_connection_state, cl);
  915. struct ssh_rportfwd *rpf = snew(struct ssh_rportfwd);
  916. rpf->shost = dupstr(shost);
  917. rpf->sport = sport;
  918. rpf->dhost = dupstr(dhost);
  919. rpf->dport = dport;
  920. rpf->addressfamily = addressfamily;
  921. rpf->log_description = dupstr(log_description);
  922. rpf->pfr = pfr;
  923. if (add234(s->rportfwds, rpf) != rpf) {
  924. free_rportfwd(rpf);
  925. return NULL;
  926. }
  927. { // WINSCP
  928. PktOut *pktout = ssh_bpp_new_pktout(
  929. s->ppl.bpp, SSH1_CMSG_PORT_FORWARD_REQUEST);
  930. put_uint32(pktout, rpf->sport);
  931. put_stringz(pktout, rpf->dhost);
  932. put_uint32(pktout, rpf->dport);
  933. pq_push(s->ppl.out_pq, pktout);
  934. } // WINSCP
  935. ssh1_queue_succfail_handler(s, ssh1_rportfwd_response, rpf);
  936. return rpf;
  937. }
  938. static void ssh1_rportfwd_remove(ConnectionLayer *cl, struct ssh_rportfwd *rpf)
  939. {
  940. /*
  941. * We cannot cancel listening ports on the server side in SSH-1!
  942. * There's no message to support it.
  943. */
  944. }
  945. static int ssh1_agent_forwarding_permitted(ConnectionLayer *cl)
  946. {
  947. struct ssh1_connection_state *s =
  948. container_of(cl, struct ssh1_connection_state, cl);
  949. return conf_get_int(s->conf, CONF_agentfwd) && agent_exists();
  950. }
  951. static void ssh1_connection_special_cmd(PacketProtocolLayer *ppl,
  952. SessionSpecialCode code, int arg)
  953. {
  954. struct ssh1_connection_state *s =
  955. container_of(ppl, struct ssh1_connection_state, ppl);
  956. PktOut *pktout;
  957. if (code == SS_PING || code == SS_NOP) {
  958. if (!(s->ppl.remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) {
  959. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_IGNORE);
  960. put_stringz(pktout, "");
  961. pq_push(s->ppl.out_pq, pktout);
  962. }
  963. } else if (code == SS_EOF) {
  964. if (!s->session_ready) {
  965. /*
  966. * Buffer the EOF to send as soon as the main session is
  967. * fully set up.
  968. */
  969. s->session_eof_pending = TRUE;
  970. } else if (!s->session_eof_sent) {
  971. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_EOF);
  972. pq_push(s->ppl.out_pq, pktout);
  973. ppl_logevent(("Sent EOF message"));
  974. s->session_eof_sent = TRUE;
  975. }
  976. }
  977. }
  978. static void ssh1_terminal_size(ConnectionLayer *cl, int width, int height)
  979. {
  980. struct ssh1_connection_state *s =
  981. container_of(cl, struct ssh1_connection_state, cl);
  982. s->term_width = width;
  983. s->term_height = height;
  984. if (s->session_ready) {
  985. PktOut *pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_WINDOW_SIZE);
  986. put_uint32(pktout, s->term_height);
  987. put_uint32(pktout, s->term_width);
  988. put_uint32(pktout, 0);
  989. put_uint32(pktout, 0);
  990. pq_push(s->ppl.out_pq, pktout);
  991. }
  992. }
  993. static void ssh1_stdout_unthrottle(ConnectionLayer *cl, int bufsize)
  994. {
  995. struct ssh1_connection_state *s =
  996. container_of(cl, struct ssh1_connection_state, cl);
  997. if (s->stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) {
  998. s->stdout_throttling = 0;
  999. ssh_throttle_conn(s->ppl.ssh, -1);
  1000. }
  1001. }
  1002. static int ssh1_stdin_backlog(ConnectionLayer *cl)
  1003. {
  1004. return 0;
  1005. }
  1006. static void ssh1_throttle_all_channels(ConnectionLayer *cl, int throttled)
  1007. {
  1008. struct ssh1_connection_state *s =
  1009. container_of(cl, struct ssh1_connection_state, cl);
  1010. struct ssh1_channel *c;
  1011. int i;
  1012. for (i = 0; NULL != (c = index234(s->channels, i)); i++)
  1013. chan_set_input_wanted(c->chan, !throttled);
  1014. }
  1015. static int ssh1_ldisc_option(ConnectionLayer *cl, int option)
  1016. {
  1017. struct ssh1_connection_state *s =
  1018. container_of(cl, struct ssh1_connection_state, cl);
  1019. /* We always return the same value for LD_ECHO and LD_EDIT */
  1020. return s->echoedit;
  1021. }
  1022. static int ssh1_connection_want_user_input(PacketProtocolLayer *ppl)
  1023. {
  1024. struct ssh1_connection_state *s =
  1025. container_of(ppl, struct ssh1_connection_state, ppl);
  1026. return s->session_ready && !s->session_eof_sent;
  1027. }
  1028. static void ssh1_connection_got_user_input(PacketProtocolLayer *ppl)
  1029. {
  1030. struct ssh1_connection_state *s =
  1031. container_of(ppl, struct ssh1_connection_state, ppl);
  1032. if (s->session_ready && !s->session_eof_sent)
  1033. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1034. }
  1035. static void ssh1_connection_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  1036. {
  1037. struct ssh1_connection_state *s =
  1038. container_of(ppl, struct ssh1_connection_state, ppl);
  1039. conf_free(s->conf);
  1040. s->conf = conf_copy(conf);
  1041. if (s->portfwdmgr_configured)
  1042. portfwdmgr_config(s->portfwdmgr, s->conf);
  1043. }