ssh1connection.c 41 KB

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