ssh1connection.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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. FROMFIELD(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. assert(ppl->vt == &ssh1_connection_vtable);
  258. struct ssh1_connection_state *s =
  259. FROMFIELD(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. FROMFIELD(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. s->x11disp =
  555. x11_setup_display(conf_get_str(s->conf, CONF_x11_display),
  556. s->conf);
  557. if (!s->x11disp) {
  558. /* FIXME: return an error message from x11_setup_display */
  559. ppl_logevent(("X11 forwarding not enabled: unable to"
  560. " initialise X display"));
  561. } else {
  562. s->x11auth = x11_invent_fake_auth
  563. (s->x11authtree, conf_get_int(s->conf, CONF_x11_auth));
  564. s->x11auth->disp = s->x11disp;
  565. ppl_logevent(("Requesting X11 forwarding"));
  566. pktout = ssh_bpp_new_pktout(
  567. s->ppl.bpp, SSH1_CMSG_X11_REQUEST_FORWARDING);
  568. put_stringz(pktout, s->x11auth->protoname);
  569. put_stringz(pktout, s->x11auth->datastring);
  570. if (s->local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER)
  571. put_uint32(pktout, s->x11disp->screennum);
  572. pq_push(s->ppl.out_pq, pktout);
  573. crMaybeWaitUntilV((pktin = ssh1_connection_pop(s)) != NULL);
  574. if (pktin->type == SSH1_SMSG_SUCCESS) {
  575. ppl_logevent(("X11 forwarding enabled"));
  576. s->X11_fwd_enabled = TRUE;
  577. } else if (pktin->type == SSH1_SMSG_FAILURE) {
  578. ppl_logevent(("X11 forwarding refused"));
  579. } else {
  580. ssh_proto_error(s->ppl.ssh, "Unexpected packet received"
  581. " in response to X11 forwarding request, "
  582. "type %d (%s)", pktin->type,
  583. ssh1_pkt_type(pktin->type));
  584. return;
  585. }
  586. }
  587. }
  588. portfwdmgr_config(s->portfwdmgr, s->conf);
  589. s->portfwdmgr_configured = TRUE;
  590. if (!conf_get_int(s->conf, CONF_nopty)) {
  591. /* Unpick the terminal-speed string. */
  592. /* XXX perhaps we should allow no speeds to be sent. */
  593. s->ospeed = 38400; s->ispeed = 38400; /* last-resort defaults */
  594. sscanf(conf_get_str(s->conf, CONF_termspeed), "%d,%d",
  595. &s->ospeed, &s->ispeed);
  596. /* Send the pty request. */
  597. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_REQUEST_PTY);
  598. put_stringz(pktout, conf_get_str(s->conf, CONF_termtype));
  599. put_uint32(pktout, s->term_height);
  600. put_uint32(pktout, s->term_width);
  601. s->term_width_orig = s->term_width;
  602. s->term_height_orig = s->term_height;
  603. put_uint32(pktout, 0); /* width in pixels */
  604. put_uint32(pktout, 0); /* height in pixels */
  605. write_ttymodes_to_packet_from_conf(
  606. BinarySink_UPCAST(pktout), s->ppl.frontend, s->conf,
  607. 1, s->ospeed, s->ispeed);
  608. pq_push(s->ppl.out_pq, pktout);
  609. crMaybeWaitUntilV((pktin = ssh1_connection_pop(s)) != NULL);
  610. if (pktin->type == SSH1_SMSG_SUCCESS) {
  611. ppl_logevent(("Allocated pty (ospeed %dbps, ispeed %dbps)",
  612. s->ospeed, s->ispeed));
  613. s->got_pty = TRUE;
  614. } else if (pktin->type == SSH1_SMSG_FAILURE) {
  615. ppl_printf(("Server refused to allocate pty\r\n"));
  616. s->echoedit = TRUE;
  617. } else {
  618. ssh_proto_error(s->ppl.ssh, "Unexpected packet received"
  619. " in response to pty request, "
  620. "type %d (%s)", pktin->type,
  621. ssh1_pkt_type(pktin->type));
  622. crStopV;
  623. }
  624. } else {
  625. s->echoedit = TRUE;
  626. }
  627. /*
  628. * Start the shell or command.
  629. *
  630. * Special case: if the first-choice command is an SSH-2
  631. * subsystem (hence not usable here) and the second choice
  632. * exists, we fall straight back to that.
  633. */
  634. {
  635. char *cmd = conf_get_str(s->conf, CONF_remote_cmd);
  636. if (conf_get_int(s->conf, CONF_ssh_subsys) &&
  637. conf_get_str(s->conf, CONF_remote_cmd2)) {
  638. cmd = conf_get_str(s->conf, CONF_remote_cmd2);
  639. ssh_got_fallback_cmd(s->ppl.ssh);
  640. }
  641. if (*cmd) {
  642. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_EXEC_CMD);
  643. put_stringz(pktout, cmd);
  644. } else {
  645. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_EXEC_SHELL);
  646. }
  647. pq_push(s->ppl.out_pq, pktout);
  648. ppl_logevent(("Started session"));
  649. }
  650. s->session_ready = TRUE;
  651. ssh_ppl_got_user_input(&s->ppl); /* in case any input is already queued */
  652. /* If an EOF or a window-size change arrived before we were ready
  653. * to handle either one, handle them now. */
  654. if (s->session_eof_pending) {
  655. ssh_ppl_special_cmd(&s->ppl, SS_EOF, 0);
  656. s->session_eof_pending = FALSE;
  657. }
  658. if (s->term_width_orig != s->term_width ||
  659. s->term_height_orig != s->term_height)
  660. ssh_terminal_size(&s->cl, s->term_width, s->term_height);
  661. ssh_ldisc_update(s->ppl.ssh);
  662. s->finished_setup = TRUE;
  663. while (1) {
  664. /*
  665. * By this point, most incoming packets are already being
  666. * handled by filter_queue, and we need only pay attention to
  667. * the unusual ones.
  668. */
  669. if ((pktin = ssh1_connection_pop(s)) != NULL) {
  670. ssh_proto_error(s->ppl.ssh, "Unexpected packet received, "
  671. "type %d (%s)", pktin->type,
  672. ssh1_pkt_type(pktin->type));
  673. return;
  674. }
  675. while (bufchain_size(s->ppl.user_input) > 0) {
  676. void *data;
  677. int len;
  678. bufchain_prefix(s->ppl.user_input, &data, &len);
  679. if (len > 512)
  680. len = 512;
  681. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_STDIN_DATA);
  682. put_string(pktout, data, len);
  683. pq_push(s->ppl.out_pq, pktout);
  684. bufchain_consume(s->ppl.user_input, len);
  685. }
  686. crReturnV;
  687. }
  688. crFinishV;
  689. }
  690. static void ssh1_channel_check_close(struct ssh1_channel *c)
  691. {
  692. struct ssh1_connection_state *s = c->connlayer;
  693. PktOut *pktout;
  694. if (c->halfopen) {
  695. /*
  696. * If we've sent out our own CHANNEL_OPEN but not yet seen
  697. * either OPEN_CONFIRMATION or OPEN_FAILURE in response, then
  698. * it's too early to be sending close messages of any kind.
  699. */
  700. return;
  701. }
  702. if ((!((CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE) & ~c->closes) ||
  703. chan_want_close(c->chan, (c->closes & CLOSES_SENT_CLOSE),
  704. (c->closes & CLOSES_RCVD_CLOSE))) &&
  705. !(c->closes & CLOSES_SENT_CLOSECONF)) {
  706. /*
  707. * We have both sent and received CLOSE (or the channel type
  708. * doesn't need us to), which means the channel is in final
  709. * wind-up. Send CLOSE and/or CLOSE_CONFIRMATION, whichever we
  710. * haven't sent yet.
  711. */
  712. if (!(c->closes & CLOSES_SENT_CLOSE)) {
  713. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_CHANNEL_CLOSE);
  714. put_uint32(pktout, c->remoteid);
  715. pq_push(s->ppl.out_pq, pktout);
  716. c->closes |= CLOSES_SENT_CLOSE;
  717. }
  718. if (c->closes & CLOSES_RCVD_CLOSE) {
  719. pktout = ssh_bpp_new_pktout(
  720. s->ppl.bpp, SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION);
  721. put_uint32(pktout, c->remoteid);
  722. pq_push(s->ppl.out_pq, pktout);
  723. c->closes |= CLOSES_SENT_CLOSECONF;
  724. }
  725. }
  726. if (!((CLOSES_SENT_CLOSECONF | CLOSES_RCVD_CLOSECONF) & ~c->closes)) {
  727. /*
  728. * We have both sent and received CLOSE_CONFIRMATION, which
  729. * means we're completely done with the channel.
  730. */
  731. ssh1_channel_destroy(c);
  732. }
  733. }
  734. static void ssh1_channel_try_eof(struct ssh1_channel *c)
  735. {
  736. struct ssh1_connection_state *s = c->connlayer;
  737. PktOut *pktout;
  738. assert(c->pending_eof); /* precondition for calling us */
  739. if (c->halfopen)
  740. return; /* can't close: not even opened yet */
  741. c->pending_eof = FALSE; /* we're about to send it */
  742. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_CHANNEL_CLOSE);
  743. put_uint32(pktout, c->remoteid);
  744. pq_push(s->ppl.out_pq, pktout);
  745. c->closes |= CLOSES_SENT_CLOSE;
  746. ssh1_channel_check_close(c);
  747. }
  748. /*
  749. * Close any local socket and free any local resources associated with
  750. * a channel. This converts the channel into a zombie.
  751. */
  752. static void ssh1_channel_close_local(struct ssh1_channel *c,
  753. const char *reason)
  754. {
  755. struct ssh1_connection_state *s = c->connlayer;
  756. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  757. const char *msg = chan_log_close_msg(c->chan);
  758. if (msg != NULL)
  759. ppl_logevent(("%s%s%s", msg, reason ? " " : "", reason ? reason : ""));
  760. chan_free(c->chan);
  761. c->chan = zombiechan_new();
  762. }
  763. static void ssh1_check_termination_callback(void *vctx)
  764. {
  765. struct ssh1_connection_state *s = (struct ssh1_connection_state *)vctx;
  766. ssh1_check_termination(s);
  767. }
  768. static void ssh1_channel_destroy(struct ssh1_channel *c)
  769. {
  770. struct ssh1_connection_state *s = c->connlayer;
  771. ssh1_channel_close_local(c, NULL);
  772. del234(s->channels, c);
  773. ssh1_channel_free(c);
  774. /*
  775. * If that was the last channel left open, we might need to
  776. * terminate. But we'll be a bit cautious, by doing that in a
  777. * toplevel callback, just in case anything on the current call
  778. * stack objects to this entire PPL being freed.
  779. */
  780. queue_toplevel_callback(ssh1_check_termination_callback, s);
  781. }
  782. static int ssh1_check_termination(struct ssh1_connection_state *s)
  783. {
  784. /*
  785. * Decide whether we should terminate the SSH connection now.
  786. * Called after a channel goes away, or when the main session
  787. * returns SSH1_SMSG_EXIT_STATUS; we terminate when none of either
  788. * is left.
  789. */
  790. if (s->session_terminated && count234(s->channels) == 0) {
  791. PktOut *pktout = ssh_bpp_new_pktout(
  792. s->ppl.bpp, SSH1_CMSG_EXIT_CONFIRMATION);
  793. pq_push(s->ppl.out_pq, pktout);
  794. ssh_user_close(s->ppl.ssh, "Session finished");
  795. return TRUE;
  796. }
  797. return FALSE;
  798. }
  799. /*
  800. * Set up most of a new ssh1_channel. Leaves chan untouched (since it
  801. * will sometimes have been filled in before calling this).
  802. */
  803. static void ssh1_channel_init(struct ssh1_channel *c)
  804. {
  805. struct ssh1_connection_state *s = c->connlayer;
  806. c->closes = 0;
  807. c->pending_eof = FALSE;
  808. c->throttling_conn = FALSE;
  809. c->sc.vt = &ssh1channel_vtable;
  810. c->localid = alloc_channel_id(s->channels, struct ssh1_channel);
  811. add234(s->channels, c);
  812. }
  813. static Conf *ssh1channel_get_conf(SshChannel *sc)
  814. {
  815. struct ssh1_channel *c = FROMFIELD(sc, struct ssh1_channel, sc);
  816. struct ssh1_connection_state *s = c->connlayer;
  817. return s->conf;
  818. }
  819. static void ssh1channel_write_eof(SshChannel *sc)
  820. {
  821. struct ssh1_channel *c = FROMFIELD(sc, struct ssh1_channel, sc);
  822. if (c->closes & CLOSES_SENT_CLOSE)
  823. return;
  824. c->pending_eof = TRUE;
  825. ssh1_channel_try_eof(c);
  826. }
  827. static void ssh1channel_unclean_close(SshChannel *sc, const char *err)
  828. {
  829. struct ssh1_channel *c = FROMFIELD(sc, struct ssh1_channel, sc);
  830. char *reason;
  831. reason = dupprintf("due to local error: %s", err);
  832. ssh1_channel_close_local(c, reason);
  833. sfree(reason);
  834. c->pending_eof = FALSE; /* this will confuse a zombie channel */
  835. ssh1_channel_check_close(c);
  836. }
  837. static void ssh1channel_unthrottle(SshChannel *sc, int bufsize)
  838. {
  839. struct ssh1_channel *c = FROMFIELD(sc, struct ssh1_channel, sc);
  840. struct ssh1_connection_state *s = c->connlayer;
  841. if (c->throttling_conn && bufsize <= SSH1_BUFFER_LIMIT) {
  842. c->throttling_conn = 0;
  843. ssh_throttle_conn(s->ppl.ssh, -1);
  844. }
  845. }
  846. static int ssh1channel_write(SshChannel *sc, const void *buf, int len)
  847. {
  848. struct ssh1_channel *c = FROMFIELD(sc, struct ssh1_channel, sc);
  849. struct ssh1_connection_state *s = c->connlayer;
  850. assert(!(c->closes & CLOSES_SENT_CLOSE));
  851. PktOut *pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_CHANNEL_DATA);
  852. put_uint32(pktout, c->remoteid);
  853. put_string(pktout, buf, len);
  854. pq_push(s->ppl.out_pq, pktout);
  855. /*
  856. * In SSH-1 we can return 0 here - implying that channels are
  857. * never individually throttled - because the only circumstance
  858. * that can cause throttling will be the whole SSH connection
  859. * backing up, in which case _everything_ will be throttled as a
  860. * whole.
  861. */
  862. return 0;
  863. }
  864. static SshChannel *ssh1_lportfwd_open(
  865. ConnectionLayer *cl, const char *hostname, int port,
  866. const char *org, Channel *chan)
  867. {
  868. struct ssh1_connection_state *s =
  869. FROMFIELD(cl, struct ssh1_connection_state, cl);
  870. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  871. struct ssh1_channel *c = snew(struct ssh1_channel);
  872. PktOut *pktout;
  873. c->connlayer = s;
  874. ssh1_channel_init(c);
  875. c->halfopen = TRUE;
  876. c->chan = chan;
  877. ppl_logevent(("Opening connection to %s:%d for %s", hostname, port, org));
  878. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_PORT_OPEN);
  879. put_uint32(pktout, c->localid);
  880. put_stringz(pktout, hostname);
  881. put_uint32(pktout, port);
  882. /* originator string would go here, but we didn't specify
  883. * SSH_PROTOFLAG_HOST_IN_FWD_OPEN */
  884. pq_push(s->ppl.out_pq, pktout);
  885. return &c->sc;
  886. }
  887. static void ssh1_rportfwd_response(struct ssh1_connection_state *s,
  888. PktIn *pktin, void *ctx)
  889. {
  890. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  891. struct ssh_rportfwd *rpf = (struct ssh_rportfwd *)ctx;
  892. if (pktin->type == SSH1_SMSG_SUCCESS) {
  893. ppl_logevent(("Remote port forwarding from %s enabled",
  894. rpf->log_description));
  895. } else {
  896. ppl_logevent(("Remote port forwarding from %s refused",
  897. rpf->log_description));
  898. struct ssh_rportfwd *realpf = del234(s->rportfwds, rpf);
  899. assert(realpf == rpf);
  900. portfwdmgr_close(s->portfwdmgr, rpf->pfr);
  901. free_rportfwd(rpf);
  902. }
  903. }
  904. static struct ssh_rportfwd *ssh1_rportfwd_alloc(
  905. ConnectionLayer *cl,
  906. const char *shost, int sport, const char *dhost, int dport,
  907. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  908. ssh_sharing_connstate *share_ctx)
  909. {
  910. struct ssh1_connection_state *s =
  911. FROMFIELD(cl, struct ssh1_connection_state, cl);
  912. struct ssh_rportfwd *rpf = snew(struct ssh_rportfwd);
  913. rpf->shost = dupstr(shost);
  914. rpf->sport = sport;
  915. rpf->dhost = dupstr(dhost);
  916. rpf->dport = dport;
  917. rpf->addressfamily = addressfamily;
  918. rpf->log_description = dupstr(log_description);
  919. rpf->pfr = pfr;
  920. if (add234(s->rportfwds, rpf) != rpf) {
  921. free_rportfwd(rpf);
  922. return NULL;
  923. }
  924. PktOut *pktout = ssh_bpp_new_pktout(
  925. s->ppl.bpp, SSH1_CMSG_PORT_FORWARD_REQUEST);
  926. put_uint32(pktout, rpf->sport);
  927. put_stringz(pktout, rpf->dhost);
  928. put_uint32(pktout, rpf->dport);
  929. pq_push(s->ppl.out_pq, pktout);
  930. ssh1_queue_succfail_handler(s, ssh1_rportfwd_response, rpf);
  931. return rpf;
  932. }
  933. static void ssh1_rportfwd_remove(ConnectionLayer *cl, struct ssh_rportfwd *rpf)
  934. {
  935. /*
  936. * We cannot cancel listening ports on the server side in SSH-1!
  937. * There's no message to support it.
  938. */
  939. }
  940. static int ssh1_agent_forwarding_permitted(ConnectionLayer *cl)
  941. {
  942. struct ssh1_connection_state *s =
  943. FROMFIELD(cl, struct ssh1_connection_state, cl);
  944. return conf_get_int(s->conf, CONF_agentfwd) && agent_exists();
  945. }
  946. static void ssh1_connection_special_cmd(PacketProtocolLayer *ppl,
  947. SessionSpecialCode code, int arg)
  948. {
  949. struct ssh1_connection_state *s =
  950. FROMFIELD(ppl, struct ssh1_connection_state, ppl);
  951. PktOut *pktout;
  952. if (code == SS_PING || code == SS_NOP) {
  953. if (!(s->ppl.remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) {
  954. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_IGNORE);
  955. put_stringz(pktout, "");
  956. pq_push(s->ppl.out_pq, pktout);
  957. }
  958. } else if (code == SS_EOF) {
  959. if (!s->session_ready) {
  960. /*
  961. * Buffer the EOF to send as soon as the main session is
  962. * fully set up.
  963. */
  964. s->session_eof_pending = TRUE;
  965. } else if (!s->session_eof_sent) {
  966. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_EOF);
  967. pq_push(s->ppl.out_pq, pktout);
  968. ppl_logevent(("Sent EOF message"));
  969. s->session_eof_sent = TRUE;
  970. }
  971. }
  972. }
  973. static void ssh1_terminal_size(ConnectionLayer *cl, int width, int height)
  974. {
  975. struct ssh1_connection_state *s =
  976. FROMFIELD(cl, struct ssh1_connection_state, cl);
  977. s->term_width = width;
  978. s->term_height = height;
  979. if (s->session_ready) {
  980. PktOut *pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_WINDOW_SIZE);
  981. put_uint32(pktout, s->term_height);
  982. put_uint32(pktout, s->term_width);
  983. put_uint32(pktout, 0);
  984. put_uint32(pktout, 0);
  985. pq_push(s->ppl.out_pq, pktout);
  986. }
  987. }
  988. static void ssh1_stdout_unthrottle(ConnectionLayer *cl, int bufsize)
  989. {
  990. struct ssh1_connection_state *s =
  991. FROMFIELD(cl, struct ssh1_connection_state, cl);
  992. if (s->stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) {
  993. s->stdout_throttling = 0;
  994. ssh_throttle_conn(s->ppl.ssh, -1);
  995. }
  996. }
  997. static int ssh1_stdin_backlog(ConnectionLayer *cl)
  998. {
  999. return 0;
  1000. }
  1001. static void ssh1_throttle_all_channels(ConnectionLayer *cl, int throttled)
  1002. {
  1003. struct ssh1_connection_state *s =
  1004. FROMFIELD(cl, struct ssh1_connection_state, cl);
  1005. struct ssh1_channel *c;
  1006. int i;
  1007. for (i = 0; NULL != (c = index234(s->channels, i)); i++)
  1008. chan_set_input_wanted(c->chan, !throttled);
  1009. }
  1010. static int ssh1_ldisc_option(ConnectionLayer *cl, int option)
  1011. {
  1012. struct ssh1_connection_state *s =
  1013. FROMFIELD(cl, struct ssh1_connection_state, cl);
  1014. /* We always return the same value for LD_ECHO and LD_EDIT */
  1015. return s->echoedit;
  1016. }
  1017. static int ssh1_connection_want_user_input(PacketProtocolLayer *ppl)
  1018. {
  1019. struct ssh1_connection_state *s =
  1020. FROMFIELD(ppl, struct ssh1_connection_state, ppl);
  1021. return s->session_ready && !s->session_eof_sent;
  1022. }
  1023. static void ssh1_connection_got_user_input(PacketProtocolLayer *ppl)
  1024. {
  1025. struct ssh1_connection_state *s =
  1026. FROMFIELD(ppl, struct ssh1_connection_state, ppl);
  1027. if (s->session_ready && !s->session_eof_sent)
  1028. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1029. }
  1030. static void ssh1_connection_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  1031. {
  1032. struct ssh1_connection_state *s =
  1033. FROMFIELD(ppl, struct ssh1_connection_state, ppl);
  1034. conf_free(s->conf);
  1035. s->conf = conf_copy(conf);
  1036. if (s->portfwdmgr_configured)
  1037. portfwdmgr_config(s->portfwdmgr, s->conf);
  1038. }