ssh1connection.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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. 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(s->cl.frontend, ssh1_check_termination_callback, s); // WINSCP
  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 = container_of(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 = container_of(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 = container_of(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 = container_of(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 = container_of(sc, struct ssh1_channel, sc);
  849. struct ssh1_connection_state *s = c->connlayer;
  850. pinitassert(!(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. container_of(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. { // WINSCP
  899. struct ssh_rportfwd *realpf = del234(s->rportfwds, rpf);
  900. assert(realpf == rpf);
  901. portfwdmgr_close(s->portfwdmgr, rpf->pfr);
  902. free_rportfwd(rpf);
  903. } // WINSCP
  904. }
  905. }
  906. static struct ssh_rportfwd *ssh1_rportfwd_alloc(
  907. ConnectionLayer *cl,
  908. const char *shost, int sport, const char *dhost, int dport,
  909. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  910. ssh_sharing_connstate *share_ctx)
  911. {
  912. struct ssh1_connection_state *s =
  913. container_of(cl, struct ssh1_connection_state, cl);
  914. struct ssh_rportfwd *rpf = snew(struct ssh_rportfwd);
  915. rpf->shost = dupstr(shost);
  916. rpf->sport = sport;
  917. rpf->dhost = dupstr(dhost);
  918. rpf->dport = dport;
  919. rpf->addressfamily = addressfamily;
  920. rpf->log_description = dupstr(log_description);
  921. rpf->pfr = pfr;
  922. if (add234(s->rportfwds, rpf) != rpf) {
  923. free_rportfwd(rpf);
  924. return NULL;
  925. }
  926. { // WINSCP
  927. PktOut *pktout = ssh_bpp_new_pktout(
  928. s->ppl.bpp, SSH1_CMSG_PORT_FORWARD_REQUEST);
  929. put_uint32(pktout, rpf->sport);
  930. put_stringz(pktout, rpf->dhost);
  931. put_uint32(pktout, rpf->dport);
  932. pq_push(s->ppl.out_pq, pktout);
  933. } // WINSCP
  934. ssh1_queue_succfail_handler(s, ssh1_rportfwd_response, rpf);
  935. return rpf;
  936. }
  937. static void ssh1_rportfwd_remove(ConnectionLayer *cl, struct ssh_rportfwd *rpf)
  938. {
  939. /*
  940. * We cannot cancel listening ports on the server side in SSH-1!
  941. * There's no message to support it.
  942. */
  943. }
  944. static int ssh1_agent_forwarding_permitted(ConnectionLayer *cl)
  945. {
  946. struct ssh1_connection_state *s =
  947. container_of(cl, struct ssh1_connection_state, cl);
  948. return conf_get_int(s->conf, CONF_agentfwd) && agent_exists();
  949. }
  950. static void ssh1_connection_special_cmd(PacketProtocolLayer *ppl,
  951. SessionSpecialCode code, int arg)
  952. {
  953. struct ssh1_connection_state *s =
  954. container_of(ppl, struct ssh1_connection_state, ppl);
  955. PktOut *pktout;
  956. if (code == SS_PING || code == SS_NOP) {
  957. if (!(s->ppl.remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) {
  958. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_MSG_IGNORE);
  959. put_stringz(pktout, "");
  960. pq_push(s->ppl.out_pq, pktout);
  961. }
  962. } else if (code == SS_EOF) {
  963. if (!s->session_ready) {
  964. /*
  965. * Buffer the EOF to send as soon as the main session is
  966. * fully set up.
  967. */
  968. s->session_eof_pending = TRUE;
  969. } else if (!s->session_eof_sent) {
  970. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_EOF);
  971. pq_push(s->ppl.out_pq, pktout);
  972. ppl_logevent(("Sent EOF message"));
  973. s->session_eof_sent = TRUE;
  974. }
  975. }
  976. }
  977. static void ssh1_terminal_size(ConnectionLayer *cl, int width, int height)
  978. {
  979. struct ssh1_connection_state *s =
  980. container_of(cl, struct ssh1_connection_state, cl);
  981. s->term_width = width;
  982. s->term_height = height;
  983. if (s->session_ready) {
  984. PktOut *pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_WINDOW_SIZE);
  985. put_uint32(pktout, s->term_height);
  986. put_uint32(pktout, s->term_width);
  987. put_uint32(pktout, 0);
  988. put_uint32(pktout, 0);
  989. pq_push(s->ppl.out_pq, pktout);
  990. }
  991. }
  992. static void ssh1_stdout_unthrottle(ConnectionLayer *cl, int bufsize)
  993. {
  994. struct ssh1_connection_state *s =
  995. container_of(cl, struct ssh1_connection_state, cl);
  996. if (s->stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) {
  997. s->stdout_throttling = 0;
  998. ssh_throttle_conn(s->ppl.ssh, -1);
  999. }
  1000. }
  1001. static int ssh1_stdin_backlog(ConnectionLayer *cl)
  1002. {
  1003. return 0;
  1004. }
  1005. static void ssh1_throttle_all_channels(ConnectionLayer *cl, int throttled)
  1006. {
  1007. struct ssh1_connection_state *s =
  1008. container_of(cl, struct ssh1_connection_state, cl);
  1009. struct ssh1_channel *c;
  1010. int i;
  1011. for (i = 0; NULL != (c = index234(s->channels, i)); i++)
  1012. chan_set_input_wanted(c->chan, !throttled);
  1013. }
  1014. static int ssh1_ldisc_option(ConnectionLayer *cl, int option)
  1015. {
  1016. struct ssh1_connection_state *s =
  1017. container_of(cl, struct ssh1_connection_state, cl);
  1018. /* We always return the same value for LD_ECHO and LD_EDIT */
  1019. return s->echoedit;
  1020. }
  1021. static int ssh1_connection_want_user_input(PacketProtocolLayer *ppl)
  1022. {
  1023. struct ssh1_connection_state *s =
  1024. container_of(ppl, struct ssh1_connection_state, ppl);
  1025. return s->session_ready && !s->session_eof_sent;
  1026. }
  1027. static void ssh1_connection_got_user_input(PacketProtocolLayer *ppl)
  1028. {
  1029. struct ssh1_connection_state *s =
  1030. container_of(ppl, struct ssh1_connection_state, ppl);
  1031. if (s->session_ready && !s->session_eof_sent)
  1032. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1033. }
  1034. static void ssh1_connection_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  1035. {
  1036. struct ssh1_connection_state *s =
  1037. container_of(ppl, struct ssh1_connection_state, ppl);
  1038. conf_free(s->conf);
  1039. s->conf = conf_copy(conf);
  1040. if (s->portfwdmgr_configured)
  1041. portfwdmgr_config(s->portfwdmgr, s->conf);
  1042. }