ssh2connection.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. /*
  2. * Packet protocol layer for the SSH-2 connection protocol (RFC 4254).
  3. */
  4. #include <assert.h>
  5. #include "putty.h"
  6. #include "ssh.h"
  7. #include "sshbpp.h"
  8. #include "sshppl.h"
  9. #include "sshchan.h"
  10. #include "sshcr.h"
  11. #include "ssh2connection.h"
  12. static void ssh2_connection_free(PacketProtocolLayer *);
  13. static void ssh2_connection_process_queue(PacketProtocolLayer *);
  14. static bool ssh2_connection_get_specials(
  15. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx);
  16. static void ssh2_connection_special_cmd(PacketProtocolLayer *ppl,
  17. SessionSpecialCode code, int arg);
  18. static bool ssh2_connection_want_user_input(PacketProtocolLayer *ppl);
  19. static void ssh2_connection_got_user_input(PacketProtocolLayer *ppl);
  20. static void ssh2_connection_reconfigure(PacketProtocolLayer *ppl, Conf *conf);
  21. static const PacketProtocolLayerVtable ssh2_connection_vtable = {
  22. .free = ssh2_connection_free,
  23. .process_queue = ssh2_connection_process_queue,
  24. .get_specials = ssh2_connection_get_specials,
  25. .special_cmd = ssh2_connection_special_cmd,
  26. .want_user_input = ssh2_connection_want_user_input,
  27. .got_user_input = ssh2_connection_got_user_input,
  28. .reconfigure = ssh2_connection_reconfigure,
  29. .queued_data_size = ssh_ppl_default_queued_data_size,
  30. .name = "ssh-connection",
  31. };
  32. static SshChannel *ssh2_lportfwd_open(
  33. ConnectionLayer *cl, const char *hostname, int port,
  34. const char *description, const SocketPeerInfo *pi, Channel *chan);
  35. static struct X11FakeAuth *ssh2_add_x11_display(
  36. ConnectionLayer *cl, int authtype, struct X11Display *x11disp);
  37. static struct X11FakeAuth *ssh2_add_sharing_x11_display(
  38. ConnectionLayer *cl, int authtype, ssh_sharing_connstate *share_cs,
  39. share_channel *share_chan);
  40. static void ssh2_remove_sharing_x11_display(ConnectionLayer *cl,
  41. struct X11FakeAuth *auth);
  42. static void ssh2_send_packet_from_downstream(
  43. ConnectionLayer *cl, unsigned id, int type,
  44. const void *pkt, int pktlen, const char *additional_log_text);
  45. static unsigned ssh2_alloc_sharing_channel(
  46. ConnectionLayer *cl, ssh_sharing_connstate *connstate);
  47. static void ssh2_delete_sharing_channel(
  48. ConnectionLayer *cl, unsigned localid);
  49. static void ssh2_sharing_queue_global_request(
  50. ConnectionLayer *cl, ssh_sharing_connstate *share_ctx);
  51. static void ssh2_sharing_no_more_downstreams(ConnectionLayer *cl);
  52. static bool ssh2_agent_forwarding_permitted(ConnectionLayer *cl);
  53. static void ssh2_terminal_size(ConnectionLayer *cl, int width, int height);
  54. static void ssh2_stdout_unthrottle(ConnectionLayer *cl, size_t bufsize);
  55. static size_t ssh2_stdin_backlog(ConnectionLayer *cl);
  56. static void ssh2_throttle_all_channels(ConnectionLayer *cl, bool throttled);
  57. static bool ssh2_ldisc_option(ConnectionLayer *cl, int option);
  58. static void ssh2_set_ldisc_option(ConnectionLayer *cl, int option, bool value);
  59. static void ssh2_enable_x_fwd(ConnectionLayer *cl);
  60. static void ssh2_set_wants_user_input(ConnectionLayer *cl, bool wanted);
  61. static const ConnectionLayerVtable ssh2_connlayer_vtable = {
  62. .rportfwd_alloc = ssh2_rportfwd_alloc,
  63. .rportfwd_remove = ssh2_rportfwd_remove,
  64. .lportfwd_open = ssh2_lportfwd_open,
  65. .session_open = ssh2_session_open,
  66. .serverside_x11_open = ssh2_serverside_x11_open,
  67. .serverside_agent_open = ssh2_serverside_agent_open,
  68. .add_x11_display = ssh2_add_x11_display,
  69. .add_sharing_x11_display = ssh2_add_sharing_x11_display,
  70. .remove_sharing_x11_display = ssh2_remove_sharing_x11_display,
  71. .send_packet_from_downstream = ssh2_send_packet_from_downstream,
  72. .alloc_sharing_channel = ssh2_alloc_sharing_channel,
  73. .delete_sharing_channel = ssh2_delete_sharing_channel,
  74. .sharing_queue_global_request = ssh2_sharing_queue_global_request,
  75. .sharing_no_more_downstreams = ssh2_sharing_no_more_downstreams,
  76. .agent_forwarding_permitted = ssh2_agent_forwarding_permitted,
  77. .terminal_size = ssh2_terminal_size,
  78. .stdout_unthrottle = ssh2_stdout_unthrottle,
  79. .stdin_backlog = ssh2_stdin_backlog,
  80. .throttle_all_channels = ssh2_throttle_all_channels,
  81. .ldisc_option = ssh2_ldisc_option,
  82. .set_ldisc_option = ssh2_set_ldisc_option,
  83. .enable_x_fwd = ssh2_enable_x_fwd,
  84. .set_wants_user_input = ssh2_set_wants_user_input,
  85. };
  86. static char *ssh2_channel_open_failure_error_text(PktIn *pktin)
  87. {
  88. static const char *const reasons[] = {
  89. NULL,
  90. "Administratively prohibited",
  91. "Connect failed",
  92. "Unknown channel type",
  93. "Resource shortage",
  94. };
  95. unsigned reason_code;
  96. const char *reason_code_string;
  97. char reason_code_buf[256];
  98. ptrlen reason;
  99. reason_code = get_uint32(pktin);
  100. if (reason_code < lenof(reasons) && reasons[reason_code]) {
  101. reason_code_string = reasons[reason_code];
  102. } else {
  103. reason_code_string = reason_code_buf;
  104. sprintf(reason_code_buf, "unknown reason code %#x", reason_code);
  105. }
  106. reason = get_string(pktin);
  107. return dupprintf("%s [%.*s]", reason_code_string, PTRLEN_PRINTF(reason));
  108. }
  109. static size_t ssh2channel_write(
  110. SshChannel *c, bool is_stderr, const void *buf, size_t len);
  111. static void ssh2channel_write_eof(SshChannel *c);
  112. static void ssh2channel_initiate_close(SshChannel *c, const char *err);
  113. static void ssh2channel_unthrottle(SshChannel *c, size_t bufsize);
  114. static Conf *ssh2channel_get_conf(SshChannel *c);
  115. static void ssh2channel_window_override_removed(SshChannel *c);
  116. static void ssh2channel_x11_sharing_handover(
  117. SshChannel *c, ssh_sharing_connstate *share_cs, share_channel *share_chan,
  118. const char *peer_addr, int peer_port, int endian,
  119. int protomajor, int protominor, const void *initial_data, int initial_len);
  120. static void ssh2channel_hint_channel_is_simple(SshChannel *c);
  121. static const SshChannelVtable ssh2channel_vtable = {
  122. .write = ssh2channel_write,
  123. .write_eof = ssh2channel_write_eof,
  124. .initiate_close = ssh2channel_initiate_close,
  125. .unthrottle = ssh2channel_unthrottle,
  126. .get_conf = ssh2channel_get_conf,
  127. .window_override_removed = ssh2channel_window_override_removed,
  128. .x11_sharing_handover = ssh2channel_x11_sharing_handover,
  129. .send_exit_status = ssh2channel_send_exit_status,
  130. .send_exit_signal = ssh2channel_send_exit_signal,
  131. .send_exit_signal_numeric = ssh2channel_send_exit_signal_numeric,
  132. .request_x11_forwarding = ssh2channel_request_x11_forwarding,
  133. .request_agent_forwarding = ssh2channel_request_agent_forwarding,
  134. .request_pty = ssh2channel_request_pty,
  135. .send_env_var = ssh2channel_send_env_var,
  136. .start_shell = ssh2channel_start_shell,
  137. .start_command = ssh2channel_start_command,
  138. .start_subsystem = ssh2channel_start_subsystem,
  139. .send_serial_break = ssh2channel_send_serial_break,
  140. .send_signal = ssh2channel_send_signal,
  141. .send_terminal_size_change = ssh2channel_send_terminal_size_change,
  142. .hint_channel_is_simple = ssh2channel_hint_channel_is_simple,
  143. };
  144. static void ssh2_channel_check_close(struct ssh2_channel *c);
  145. static void ssh2_channel_try_eof(struct ssh2_channel *c);
  146. static void ssh2_set_window(struct ssh2_channel *c, int newwin);
  147. static size_t ssh2_try_send(struct ssh2_channel *c);
  148. static void ssh2_try_send_and_unthrottle(struct ssh2_channel *c);
  149. static void ssh2_channel_check_throttle(struct ssh2_channel *c);
  150. static void ssh2_channel_close_local(struct ssh2_channel *c,
  151. const char *reason);
  152. static void ssh2_channel_destroy(struct ssh2_channel *c);
  153. static void ssh2_check_termination(struct ssh2_connection_state *s);
  154. struct outstanding_global_request {
  155. gr_handler_fn_t handler;
  156. void *ctx;
  157. struct outstanding_global_request *next;
  158. };
  159. void ssh2_queue_global_request_handler(
  160. struct ssh2_connection_state *s, gr_handler_fn_t handler, void *ctx)
  161. {
  162. struct outstanding_global_request *ogr =
  163. snew(struct outstanding_global_request);
  164. ogr->handler = handler;
  165. ogr->ctx = ctx;
  166. if (s->globreq_tail)
  167. s->globreq_tail->next = ogr;
  168. else
  169. s->globreq_head = ogr;
  170. s->globreq_tail = ogr;
  171. }
  172. static int ssh2_channelcmp(void *av, void *bv)
  173. {
  174. const struct ssh2_channel *a = (const struct ssh2_channel *) av;
  175. const struct ssh2_channel *b = (const struct ssh2_channel *) bv;
  176. if (a->localid < b->localid)
  177. return -1;
  178. if (a->localid > b->localid)
  179. return +1;
  180. return 0;
  181. }
  182. static int ssh2_channelfind(void *av, void *bv)
  183. {
  184. const unsigned *a = (const unsigned *) av;
  185. const struct ssh2_channel *b = (const struct ssh2_channel *) bv;
  186. if (*a < b->localid)
  187. return -1;
  188. if (*a > b->localid)
  189. return +1;
  190. return 0;
  191. }
  192. /*
  193. * Each channel has a queue of outstanding CHANNEL_REQUESTS and their
  194. * handlers.
  195. */
  196. struct outstanding_channel_request {
  197. cr_handler_fn_t handler;
  198. void *ctx;
  199. struct outstanding_channel_request *next;
  200. };
  201. static void ssh2_channel_free(struct ssh2_channel *c)
  202. {
  203. bufchain_clear(&c->outbuffer);
  204. bufchain_clear(&c->errbuffer);
  205. while (c->chanreq_head) {
  206. struct outstanding_channel_request *chanreq = c->chanreq_head;
  207. c->chanreq_head = c->chanreq_head->next;
  208. sfree(chanreq);
  209. }
  210. if (c->chan) {
  211. struct ssh2_connection_state *s = c->connlayer;
  212. if (s->mainchan_sc == &c->sc) {
  213. s->mainchan = NULL;
  214. s->mainchan_sc = NULL;
  215. }
  216. chan_free(c->chan);
  217. }
  218. sfree(c);
  219. }
  220. PacketProtocolLayer *ssh2_connection_new(
  221. Ssh *ssh, ssh_sharing_state *connshare, bool is_simple,
  222. Conf *conf, const char *peer_verstring, ConnectionLayer **cl_out)
  223. {
  224. struct ssh2_connection_state *s = snew(struct ssh2_connection_state);
  225. memset(s, 0, sizeof(*s));
  226. s->ppl.vt = &ssh2_connection_vtable;
  227. s->conf = conf_copy(conf);
  228. s->ssh_is_simple = is_simple;
  229. /*
  230. * If the ssh_no_shell option is enabled, we disable the usual
  231. * termination check, so that we persist even in the absence of
  232. * any at all channels (because our purpose is probably to be a
  233. * background port forwarder).
  234. */
  235. s->persistent = conf_get_bool(s->conf, CONF_ssh_no_shell);
  236. s->connshare = connshare;
  237. s->peer_verstring = dupstr(peer_verstring);
  238. s->channels = newtree234(ssh2_channelcmp);
  239. s->x11authtree = newtree234(x11_authcmp);
  240. /* Need to get the log context for s->cl now, because we won't be
  241. * helpfully notified when a copy is written into s->ppl by our
  242. * owner. */
  243. s->cl.vt = &ssh2_connlayer_vtable;
  244. s->cl.logctx = ssh_get_logctx(ssh);
  245. s->portfwdmgr = portfwdmgr_new(&s->cl);
  246. *cl_out = &s->cl;
  247. if (s->connshare)
  248. ssh_connshare_provide_connlayer(s->connshare, &s->cl);
  249. return &s->ppl;
  250. }
  251. static void ssh2_connection_free(PacketProtocolLayer *ppl)
  252. {
  253. struct ssh2_connection_state *s =
  254. container_of(ppl, struct ssh2_connection_state, ppl);
  255. struct X11FakeAuth *auth;
  256. struct ssh2_channel *c;
  257. struct ssh_rportfwd *rpf;
  258. sfree(s->peer_verstring);
  259. conf_free(s->conf);
  260. while ((c = delpos234(s->channels, 0)) != NULL)
  261. ssh2_channel_free(c);
  262. freetree234(s->channels);
  263. while ((auth = delpos234(s->x11authtree, 0)) != NULL) {
  264. if (auth->disp)
  265. x11_free_display(auth->disp);
  266. x11_free_fake_auth(auth);
  267. }
  268. freetree234(s->x11authtree);
  269. if (s->rportfwds) {
  270. while ((rpf = delpos234(s->rportfwds, 0)) != NULL)
  271. free_rportfwd(rpf);
  272. freetree234(s->rportfwds);
  273. }
  274. portfwdmgr_free(s->portfwdmgr);
  275. if (s->antispoof_prompt)
  276. free_prompts(s->antispoof_prompt);
  277. delete_callbacks_for_context(s);
  278. sfree(s);
  279. }
  280. static bool ssh2_connection_filter_queue(struct ssh2_connection_state *s)
  281. {
  282. PktIn *pktin;
  283. PktOut *pktout;
  284. ptrlen type, data;
  285. struct ssh2_channel *c;
  286. struct outstanding_channel_request *ocr;
  287. unsigned localid, remid, winsize, pktsize, ext_type;
  288. bool want_reply, reply_success, expect_halfopen;
  289. ChanopenResult chanopen_result;
  290. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  291. while (1) {
  292. if (ssh2_common_filter_queue(&s->ppl))
  293. return true;
  294. if ((pktin = pq_peek(s->ppl.in_pq)) == NULL)
  295. return false;
  296. switch (pktin->type) {
  297. case SSH2_MSG_GLOBAL_REQUEST:
  298. type = get_string(pktin);
  299. want_reply = get_bool(pktin);
  300. reply_success = ssh2_connection_parse_global_request(
  301. s, type, pktin);
  302. if (want_reply) {
  303. int type = (reply_success ? SSH2_MSG_REQUEST_SUCCESS :
  304. SSH2_MSG_REQUEST_FAILURE);
  305. pktout = ssh_bpp_new_pktout(s->ppl.bpp, type);
  306. pq_push(s->ppl.out_pq, pktout);
  307. }
  308. pq_pop(s->ppl.in_pq);
  309. break;
  310. case SSH2_MSG_REQUEST_SUCCESS:
  311. case SSH2_MSG_REQUEST_FAILURE:
  312. if (!s->globreq_head) {
  313. ssh_proto_error(
  314. s->ppl.ssh,
  315. "Received %s with no outstanding global request",
  316. ssh2_pkt_type(s->ppl.bpp->pls->kctx, s->ppl.bpp->pls->actx,
  317. pktin->type));
  318. return true;
  319. }
  320. s->globreq_head->handler(s, pktin, s->globreq_head->ctx);
  321. {
  322. struct outstanding_global_request *tmp = s->globreq_head;
  323. s->globreq_head = s->globreq_head->next;
  324. sfree(tmp);
  325. }
  326. pq_pop(s->ppl.in_pq);
  327. break;
  328. case SSH2_MSG_CHANNEL_OPEN:
  329. type = get_string(pktin);
  330. c = snew(struct ssh2_channel);
  331. c->connlayer = s;
  332. c->chan = NULL;
  333. remid = get_uint32(pktin);
  334. winsize = get_uint32(pktin);
  335. pktsize = get_uint32(pktin);
  336. chanopen_result = ssh2_connection_parse_channel_open(
  337. s, type, pktin, &c->sc);
  338. if (chanopen_result.outcome == CHANOPEN_RESULT_DOWNSTREAM) {
  339. /*
  340. * This channel-open request needs to go to a
  341. * connection-sharing downstream, so abandon our own
  342. * channel-open procedure and just pass the message on
  343. * to sshshare.c.
  344. */
  345. share_got_pkt_from_server(
  346. chanopen_result.u.downstream.share_ctx, pktin->type,
  347. BinarySource_UPCAST(pktin)->data,
  348. BinarySource_UPCAST(pktin)->len);
  349. sfree(c);
  350. break;
  351. }
  352. c->remoteid = remid;
  353. c->halfopen = false;
  354. if (chanopen_result.outcome == CHANOPEN_RESULT_FAILURE) {
  355. pktout = ssh_bpp_new_pktout(
  356. s->ppl.bpp, SSH2_MSG_CHANNEL_OPEN_FAILURE);
  357. put_uint32(pktout, c->remoteid);
  358. put_uint32(pktout, chanopen_result.u.failure.reason_code);
  359. put_stringz(pktout, chanopen_result.u.failure.wire_message);
  360. put_stringz(pktout, "en"); /* language tag */
  361. pq_push(s->ppl.out_pq, pktout);
  362. ppl_logevent("Rejected channel open: %s",
  363. chanopen_result.u.failure.wire_message);
  364. sfree(chanopen_result.u.failure.wire_message);
  365. sfree(c);
  366. } else {
  367. c->chan = chanopen_result.u.success.channel;
  368. ssh2_channel_init(c);
  369. c->remwindow = winsize;
  370. c->remmaxpkt = pktsize;
  371. if (c->remmaxpkt > s->ppl.bpp->vt->packet_size_limit)
  372. c->remmaxpkt = s->ppl.bpp->vt->packet_size_limit;
  373. if (c->chan->initial_fixed_window_size) {
  374. c->locwindow = c->locmaxwin = c->remlocwin =
  375. c->chan->initial_fixed_window_size;
  376. }
  377. pktout = ssh_bpp_new_pktout(
  378. s->ppl.bpp, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
  379. put_uint32(pktout, c->remoteid);
  380. put_uint32(pktout, c->localid);
  381. put_uint32(pktout, c->locwindow);
  382. put_uint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */
  383. pq_push(s->ppl.out_pq, pktout);
  384. }
  385. pq_pop(s->ppl.in_pq);
  386. break;
  387. case SSH2_MSG_CHANNEL_DATA:
  388. case SSH2_MSG_CHANNEL_EXTENDED_DATA:
  389. case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
  390. case SSH2_MSG_CHANNEL_REQUEST:
  391. case SSH2_MSG_CHANNEL_EOF:
  392. case SSH2_MSG_CHANNEL_CLOSE:
  393. case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
  394. case SSH2_MSG_CHANNEL_OPEN_FAILURE:
  395. case SSH2_MSG_CHANNEL_SUCCESS:
  396. case SSH2_MSG_CHANNEL_FAILURE:
  397. /*
  398. * Common preliminary code for all the messages from the
  399. * server that cite one of our channel ids: look up that
  400. * channel id, check it exists, and if it's for a sharing
  401. * downstream, pass it on.
  402. */
  403. localid = get_uint32(pktin);
  404. c = find234(s->channels, &localid, ssh2_channelfind);
  405. if (c && c->sharectx) {
  406. share_got_pkt_from_server(c->sharectx, pktin->type,
  407. BinarySource_UPCAST(pktin)->data,
  408. BinarySource_UPCAST(pktin)->len);
  409. pq_pop(s->ppl.in_pq);
  410. break;
  411. }
  412. expect_halfopen = (
  413. pktin->type == SSH2_MSG_CHANNEL_OPEN_CONFIRMATION ||
  414. pktin->type == SSH2_MSG_CHANNEL_OPEN_FAILURE);
  415. if (!c || c->halfopen != expect_halfopen) {
  416. ssh_proto_error(s->ppl.ssh,
  417. "Received %s for %s channel %u",
  418. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  419. s->ppl.bpp->pls->actx,
  420. pktin->type),
  421. (!c ? "nonexistent" :
  422. c->halfopen ? "half-open" : "open"),
  423. localid);
  424. return true;
  425. }
  426. switch (pktin->type) {
  427. case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
  428. assert(c->halfopen);
  429. c->remoteid = get_uint32(pktin);
  430. c->halfopen = false;
  431. c->remwindow = get_uint32(pktin);
  432. c->remmaxpkt = get_uint32(pktin);
  433. if (c->remmaxpkt > s->ppl.bpp->vt->packet_size_limit)
  434. c->remmaxpkt = s->ppl.bpp->vt->packet_size_limit;
  435. chan_open_confirmation(c->chan);
  436. /*
  437. * Now that the channel is fully open, it's possible
  438. * in principle to immediately close it. Check whether
  439. * it wants us to!
  440. *
  441. * This can occur if a local socket error occurred
  442. * between us sending out CHANNEL_OPEN and receiving
  443. * OPEN_CONFIRMATION. If that happens, all we can do
  444. * is immediately initiate close proceedings now that
  445. * we know the server's id to put in the close
  446. * message. We'll have handled that in this code by
  447. * having already turned c->chan into a zombie, so its
  448. * want_close method (which ssh2_channel_check_close
  449. * will consult) will already be returning true.
  450. */
  451. ssh2_channel_check_close(c);
  452. if (c->pending_eof)
  453. ssh2_channel_try_eof(c); /* in case we had a pending EOF */
  454. break;
  455. case SSH2_MSG_CHANNEL_OPEN_FAILURE: {
  456. assert(c->halfopen);
  457. char *err = ssh2_channel_open_failure_error_text(pktin);
  458. chan_open_failed(c->chan, err);
  459. sfree(err);
  460. del234(s->channels, c);
  461. ssh2_channel_free(c);
  462. break;
  463. }
  464. case SSH2_MSG_CHANNEL_DATA:
  465. case SSH2_MSG_CHANNEL_EXTENDED_DATA:
  466. ext_type = (pktin->type == SSH2_MSG_CHANNEL_DATA ? 0 :
  467. get_uint32(pktin));
  468. data = get_string(pktin);
  469. if (!get_err(pktin)) {
  470. int bufsize;
  471. c->locwindow -= data.len;
  472. c->remlocwin -= data.len;
  473. if (ext_type != 0 && ext_type != SSH2_EXTENDED_DATA_STDERR)
  474. data.len = 0; /* ignore unknown extended data */
  475. bufsize = chan_send(
  476. c->chan, ext_type == SSH2_EXTENDED_DATA_STDERR,
  477. data.ptr, data.len);
  478. /*
  479. * The channel may have turned into a connection-
  480. * shared one as a result of that chan_send, e.g.
  481. * if the data we just provided completed the X11
  482. * auth phase and caused a callback to
  483. * x11_sharing_handover. If so, do nothing
  484. * further.
  485. */
  486. if (c->sharectx)
  487. break;
  488. /*
  489. * If it looks like the remote end hit the end of
  490. * its window, and we didn't want it to do that,
  491. * think about using a larger window.
  492. */
  493. if (c->remlocwin <= 0 &&
  494. c->throttle_state == UNTHROTTLED &&
  495. c->locmaxwin < 0x40000000)
  496. c->locmaxwin += OUR_V2_WINSIZE;
  497. /*
  498. * If we are not buffering too much data, enlarge
  499. * the window again at the remote side. If we are
  500. * buffering too much, we may still need to adjust
  501. * the window if the server's sent excess data.
  502. */
  503. if (bufsize < c->locmaxwin)
  504. ssh2_set_window(c, c->locmaxwin - bufsize);
  505. /*
  506. * If we're either buffering way too much data, or
  507. * if we're buffering anything at all and we're in
  508. * "simple" mode, throttle the whole channel.
  509. */
  510. if ((bufsize > c->locmaxwin ||
  511. (s->ssh_is_simple && bufsize>0)) &&
  512. !c->throttling_conn) {
  513. c->throttling_conn = true;
  514. ssh_throttle_conn(s->ppl.ssh, +1);
  515. }
  516. }
  517. break;
  518. case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
  519. if (!(c->closes & CLOSES_SENT_EOF)) {
  520. c->remwindow += get_uint32(pktin);
  521. ssh2_try_send_and_unthrottle(c);
  522. }
  523. break;
  524. case SSH2_MSG_CHANNEL_REQUEST:
  525. type = get_string(pktin);
  526. want_reply = get_bool(pktin);
  527. reply_success = false;
  528. if (c->closes & CLOSES_SENT_CLOSE) {
  529. /*
  530. * We don't reply to channel requests after we've
  531. * sent CHANNEL_CLOSE for the channel, because our
  532. * reply might cross in the network with the other
  533. * side's CHANNEL_CLOSE and arrive after they have
  534. * wound the channel up completely.
  535. */
  536. want_reply = false;
  537. }
  538. /*
  539. * Try every channel request name we recognise, no
  540. * matter what the channel, and see if the Channel
  541. * instance will accept it.
  542. */
  543. if (ptrlen_eq_string(type, "exit-status")) {
  544. int exitcode = toint(get_uint32(pktin));
  545. reply_success = chan_rcvd_exit_status(c->chan, exitcode);
  546. } else if (ptrlen_eq_string(type, "exit-signal")) {
  547. ptrlen signame;
  548. int signum;
  549. bool core = false;
  550. ptrlen errmsg;
  551. int format;
  552. /*
  553. * ICK: older versions of OpenSSH (e.g. 3.4p1)
  554. * provide an `int' for the signal, despite its
  555. * having been a `string' in the drafts of RFC
  556. * 4254 since at least 2001. (Fixed in session.c
  557. * 1.147.) Try to infer which we can safely parse
  558. * it as.
  559. */
  560. size_t startpos = BinarySource_UPCAST(pktin)->pos;
  561. for (format = 0; format < 2; format++) {
  562. BinarySource_UPCAST(pktin)->pos = startpos;
  563. BinarySource_UPCAST(pktin)->err = BSE_NO_ERROR;
  564. /* placate compiler warnings about unin */
  565. signame = make_ptrlen(NULL, 0);
  566. signum = 0;
  567. if (format == 0) /* standard string-based format */
  568. signame = get_string(pktin);
  569. else /* nonstandard integer format */
  570. signum = toint(get_uint32(pktin));
  571. core = get_bool(pktin);
  572. errmsg = get_string(pktin); /* error message */
  573. get_string(pktin); /* language tag */
  574. if (!get_err(pktin) && get_avail(pktin) == 0)
  575. break; /* successful parse */
  576. }
  577. switch (format) {
  578. case 0:
  579. reply_success = chan_rcvd_exit_signal(
  580. c->chan, signame, core, errmsg);
  581. break;
  582. case 1:
  583. reply_success = chan_rcvd_exit_signal_numeric(
  584. c->chan, signum, core, errmsg);
  585. break;
  586. default:
  587. /* Couldn't parse this message in either format */
  588. reply_success = false;
  589. break;
  590. }
  591. } else if (ptrlen_eq_string(type, "shell")) {
  592. reply_success = chan_run_shell(c->chan);
  593. } else if (ptrlen_eq_string(type, "exec")) {
  594. ptrlen command = get_string(pktin);
  595. reply_success = chan_run_command(c->chan, command);
  596. } else if (ptrlen_eq_string(type, "subsystem")) {
  597. ptrlen subsys = get_string(pktin);
  598. reply_success = chan_run_subsystem(c->chan, subsys);
  599. } else if (ptrlen_eq_string(type, "x11-req")) {
  600. bool oneshot = get_bool(pktin);
  601. ptrlen authproto = get_string(pktin);
  602. ptrlen authdata = get_string(pktin);
  603. unsigned screen_number = get_uint32(pktin);
  604. reply_success = chan_enable_x11_forwarding(
  605. c->chan, oneshot, authproto, authdata, screen_number);
  606. } else if (ptrlen_eq_string(type,
  607. "auth-agent-req@openssh.com")) {
  608. reply_success = chan_enable_agent_forwarding(c->chan);
  609. } else if (ptrlen_eq_string(type, "pty-req")) {
  610. ptrlen termtype = get_string(pktin);
  611. unsigned width = get_uint32(pktin);
  612. unsigned height = get_uint32(pktin);
  613. unsigned pixwidth = get_uint32(pktin);
  614. unsigned pixheight = get_uint32(pktin);
  615. ptrlen encoded_modes = get_string(pktin);
  616. BinarySource bs_modes[1];
  617. struct ssh_ttymodes modes;
  618. BinarySource_BARE_INIT_PL(bs_modes, encoded_modes);
  619. modes = read_ttymodes_from_packet(bs_modes, 2);
  620. if (get_err(bs_modes) || get_avail(bs_modes) > 0) {
  621. ppl_logevent("Unable to decode terminal mode string");
  622. reply_success = false;
  623. } else {
  624. reply_success = chan_allocate_pty(
  625. c->chan, termtype, width, height,
  626. pixwidth, pixheight, modes);
  627. }
  628. } else if (ptrlen_eq_string(type, "env")) {
  629. ptrlen var = get_string(pktin);
  630. ptrlen value = get_string(pktin);
  631. reply_success = chan_set_env(c->chan, var, value);
  632. } else if (ptrlen_eq_string(type, "break")) {
  633. unsigned length = get_uint32(pktin);
  634. reply_success = chan_send_break(c->chan, length);
  635. } else if (ptrlen_eq_string(type, "signal")) {
  636. ptrlen signame = get_string(pktin);
  637. reply_success = chan_send_signal(c->chan, signame);
  638. } else if (ptrlen_eq_string(type, "window-change")) {
  639. unsigned width = get_uint32(pktin);
  640. unsigned height = get_uint32(pktin);
  641. unsigned pixwidth = get_uint32(pktin);
  642. unsigned pixheight = get_uint32(pktin);
  643. reply_success = chan_change_window_size(
  644. c->chan, width, height, pixwidth, pixheight);
  645. }
  646. if (want_reply) {
  647. int type = (reply_success ? SSH2_MSG_CHANNEL_SUCCESS :
  648. SSH2_MSG_CHANNEL_FAILURE);
  649. pktout = ssh_bpp_new_pktout(s->ppl.bpp, type);
  650. put_uint32(pktout, c->remoteid);
  651. pq_push(s->ppl.out_pq, pktout);
  652. }
  653. break;
  654. case SSH2_MSG_CHANNEL_SUCCESS:
  655. case SSH2_MSG_CHANNEL_FAILURE:
  656. ocr = c->chanreq_head;
  657. if (!ocr) {
  658. ssh_proto_error(
  659. s->ppl.ssh,
  660. "Received %s for channel %d with no outstanding "
  661. "channel request",
  662. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  663. s->ppl.bpp->pls->actx, pktin->type),
  664. c->localid);
  665. return true;
  666. }
  667. ocr->handler(c, pktin, ocr->ctx);
  668. c->chanreq_head = ocr->next;
  669. sfree(ocr);
  670. /*
  671. * We may now initiate channel-closing procedures, if
  672. * that CHANNEL_REQUEST was the last thing outstanding
  673. * before we send CHANNEL_CLOSE.
  674. */
  675. ssh2_channel_check_close(c);
  676. break;
  677. case SSH2_MSG_CHANNEL_EOF:
  678. if (!(c->closes & CLOSES_RCVD_EOF)) {
  679. c->closes |= CLOSES_RCVD_EOF;
  680. chan_send_eof(c->chan);
  681. ssh2_channel_check_close(c);
  682. }
  683. break;
  684. case SSH2_MSG_CHANNEL_CLOSE:
  685. /*
  686. * When we receive CLOSE on a channel, we assume it
  687. * comes with an implied EOF if we haven't seen EOF
  688. * yet.
  689. */
  690. if (!(c->closes & CLOSES_RCVD_EOF)) {
  691. c->closes |= CLOSES_RCVD_EOF;
  692. chan_send_eof(c->chan);
  693. }
  694. if (!(s->ppl.remote_bugs & BUG_SENDS_LATE_REQUEST_REPLY)) {
  695. /*
  696. * It also means we stop expecting to see replies
  697. * to any outstanding channel requests, so clean
  698. * those up too. (ssh_chanreq_init will enforce by
  699. * assertion that we don't subsequently put
  700. * anything back on this list.)
  701. */
  702. while (c->chanreq_head) {
  703. struct outstanding_channel_request *ocr =
  704. c->chanreq_head;
  705. ocr->handler(c, NULL, ocr->ctx);
  706. c->chanreq_head = ocr->next;
  707. sfree(ocr);
  708. }
  709. }
  710. /*
  711. * And we also send an outgoing EOF, if we haven't
  712. * already, on the assumption that CLOSE is a pretty
  713. * forceful announcement that the remote side is doing
  714. * away with the entire channel. (If it had wanted to
  715. * send us EOF and continue receiving data from us, it
  716. * would have just sent CHANNEL_EOF.)
  717. */
  718. if (!(c->closes & CLOSES_SENT_EOF)) {
  719. /*
  720. * Abandon any buffered data we still wanted to
  721. * send to this channel. Receiving a CHANNEL_CLOSE
  722. * is an indication that the server really wants
  723. * to get on and _destroy_ this channel, and it
  724. * isn't going to send us any further
  725. * WINDOW_ADJUSTs to permit us to send pending
  726. * stuff.
  727. */
  728. bufchain_clear(&c->outbuffer);
  729. bufchain_clear(&c->errbuffer);
  730. /*
  731. * Send outgoing EOF.
  732. */
  733. sshfwd_write_eof(&c->sc);
  734. /*
  735. * Make sure we don't read any more from whatever
  736. * our local data source is for this channel.
  737. * (This will pick up on the changes made by
  738. * sshfwd_write_eof.)
  739. */
  740. ssh2_channel_check_throttle(c);
  741. }
  742. /*
  743. * Now process the actual close.
  744. */
  745. if (!(c->closes & CLOSES_RCVD_CLOSE)) {
  746. c->closes |= CLOSES_RCVD_CLOSE;
  747. ssh2_channel_check_close(c);
  748. }
  749. break;
  750. }
  751. pq_pop(s->ppl.in_pq);
  752. break;
  753. default:
  754. return false;
  755. }
  756. }
  757. }
  758. static void ssh2_handle_winadj_response(struct ssh2_channel *c,
  759. PktIn *pktin, void *ctx)
  760. {
  761. unsigned *sizep = ctx;
  762. /*
  763. * Winadj responses should always be failures. However, at least
  764. * one server ("boks_sshd") is known to return SUCCESS for channel
  765. * requests it's never heard of, such as "winadj@putty". Raised
  766. * with foxt.com as bug 090916-090424, but for the sake of a quiet
  767. * life, we don't worry about what kind of response we got.
  768. */
  769. c->remlocwin += *sizep;
  770. sfree(sizep);
  771. /*
  772. * winadj messages are only sent when the window is fully open, so
  773. * if we get an ack of one, we know any pending unthrottle is
  774. * complete.
  775. */
  776. if (c->throttle_state == UNTHROTTLING)
  777. c->throttle_state = UNTHROTTLED;
  778. }
  779. static void ssh2_set_window(struct ssh2_channel *c, int newwin)
  780. {
  781. struct ssh2_connection_state *s = c->connlayer;
  782. /*
  783. * Never send WINDOW_ADJUST for a channel that the remote side has
  784. * already sent EOF on; there's no point, since it won't be
  785. * sending any more data anyway. Ditto if _we've_ already sent
  786. * CLOSE.
  787. */
  788. if (c->closes & (CLOSES_RCVD_EOF | CLOSES_SENT_CLOSE))
  789. return;
  790. /*
  791. * If the client-side Channel is in an initial setup phase with a
  792. * fixed window size, e.g. for an X11 channel when we're still
  793. * waiting to see its initial auth and may yet hand it off to a
  794. * downstream, don't send any WINDOW_ADJUST either.
  795. */
  796. if (c->chan->initial_fixed_window_size)
  797. return;
  798. /*
  799. * If the remote end has a habit of ignoring maxpkt, limit the
  800. * window so that it has no choice (assuming it doesn't ignore the
  801. * window as well).
  802. */
  803. if ((s->ppl.remote_bugs & BUG_SSH2_MAXPKT) && newwin > OUR_V2_MAXPKT)
  804. newwin = OUR_V2_MAXPKT;
  805. /*
  806. * Only send a WINDOW_ADJUST if there's significantly more window
  807. * available than the other end thinks there is. This saves us
  808. * sending a WINDOW_ADJUST for every character in a shell session.
  809. *
  810. * "Significant" is arbitrarily defined as half the window size.
  811. */
  812. if (newwin / 2 >= c->locwindow) {
  813. PktOut *pktout;
  814. unsigned *up;
  815. /*
  816. * In order to keep track of how much window the client
  817. * actually has available, we'd like it to acknowledge each
  818. * WINDOW_ADJUST. We can't do that directly, so we accompany
  819. * it with a CHANNEL_REQUEST that has to be acknowledged.
  820. *
  821. * This is only necessary if we're opening the window wide.
  822. * If we're not, then throughput is being constrained by
  823. * something other than the maximum window size anyway.
  824. */
  825. if (newwin == c->locmaxwin &&
  826. !(s->ppl.remote_bugs & BUG_CHOKES_ON_WINADJ)) {
  827. up = snew(unsigned);
  828. *up = newwin - c->locwindow;
  829. pktout = ssh2_chanreq_init(c, "winadj@putty.projects.tartarus.org",
  830. ssh2_handle_winadj_response, up);
  831. pq_push(s->ppl.out_pq, pktout);
  832. if (c->throttle_state != UNTHROTTLED)
  833. c->throttle_state = UNTHROTTLING;
  834. } else {
  835. /* Pretend the WINDOW_ADJUST was acked immediately. */
  836. c->remlocwin = newwin;
  837. c->throttle_state = THROTTLED;
  838. }
  839. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_WINDOW_ADJUST);
  840. put_uint32(pktout, c->remoteid);
  841. put_uint32(pktout, newwin - c->locwindow);
  842. pq_push(s->ppl.out_pq, pktout);
  843. c->locwindow = newwin;
  844. }
  845. }
  846. static PktIn *ssh2_connection_pop(struct ssh2_connection_state *s)
  847. {
  848. ssh2_connection_filter_queue(s);
  849. return pq_pop(s->ppl.in_pq);
  850. }
  851. static void ssh2_connection_process_queue(PacketProtocolLayer *ppl)
  852. {
  853. struct ssh2_connection_state *s =
  854. container_of(ppl, struct ssh2_connection_state, ppl);
  855. PktIn *pktin;
  856. if (ssh2_connection_filter_queue(s)) /* no matter why we were called */
  857. return;
  858. crBegin(s->crState);
  859. if (s->connshare)
  860. share_activate(s->connshare, s->peer_verstring);
  861. /*
  862. * Signal the seat that authentication is done, so that it can
  863. * deploy spoofing defences. If it doesn't have any, deploy our
  864. * own fallback one.
  865. *
  866. * We do this here rather than at the end of userauth, because we
  867. * might not have gone through userauth at all (if we're a
  868. * connection-sharing downstream).
  869. */
  870. if (ssh2_connection_need_antispoof_prompt(s)) {
  871. s->antispoof_prompt = new_prompts();
  872. s->antispoof_prompt->to_server = true;
  873. s->antispoof_prompt->from_server = false;
  874. s->antispoof_prompt->name = dupstr("Authentication successful");
  875. add_prompt(
  876. s->antispoof_prompt,
  877. dupstr("Access granted. Press Return to begin session. "), false);
  878. s->antispoof_ret = seat_get_userpass_input(
  879. s->ppl.seat, s->antispoof_prompt, NULL);
  880. while (1) {
  881. while (s->antispoof_ret < 0 &&
  882. bufchain_size(s->ppl.user_input) > 0)
  883. s->antispoof_ret = seat_get_userpass_input(
  884. s->ppl.seat, s->antispoof_prompt, s->ppl.user_input);
  885. if (s->antispoof_ret >= 0)
  886. break;
  887. s->want_user_input = true;
  888. crReturnV;
  889. s->want_user_input = false;
  890. }
  891. free_prompts(s->antispoof_prompt);
  892. s->antispoof_prompt = NULL;
  893. }
  894. /*
  895. * Enable port forwardings.
  896. */
  897. portfwdmgr_config(s->portfwdmgr, s->conf);
  898. s->portfwdmgr_configured = true;
  899. /*
  900. * Create the main session channel, if any.
  901. */
  902. s->mainchan = mainchan_new(
  903. &s->ppl, &s->cl, s->conf, s->term_width, s->term_height,
  904. s->ssh_is_simple, &s->mainchan_sc);
  905. s->started = true;
  906. /*
  907. * Transfer data!
  908. */
  909. while (1) {
  910. if ((pktin = ssh2_connection_pop(s)) != NULL) {
  911. /*
  912. * _All_ the connection-layer packets we expect to
  913. * receive are now handled by the dispatch table.
  914. * Anything that reaches here must be bogus.
  915. */
  916. ssh_proto_error(s->ppl.ssh, "Received unexpected connection-layer "
  917. "packet, type %d (%s)", pktin->type,
  918. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  919. s->ppl.bpp->pls->actx,
  920. pktin->type));
  921. return;
  922. }
  923. crReturnV;
  924. }
  925. crFinishV;
  926. }
  927. static void ssh2_channel_check_close(struct ssh2_channel *c)
  928. {
  929. struct ssh2_connection_state *s = c->connlayer;
  930. PktOut *pktout;
  931. if (c->halfopen) {
  932. /*
  933. * If we've sent out our own CHANNEL_OPEN but not yet seen
  934. * either OPEN_CONFIRMATION or OPEN_FAILURE in response, then
  935. * it's too early to be sending close messages of any kind.
  936. */
  937. return;
  938. }
  939. if (chan_want_close(c->chan, (c->closes & CLOSES_SENT_EOF),
  940. (c->closes & CLOSES_RCVD_EOF)) &&
  941. !c->chanreq_head &&
  942. !(c->closes & CLOSES_SENT_CLOSE)) {
  943. /*
  944. * We have both sent and received EOF (or the channel is a
  945. * zombie), and we have no outstanding channel requests, which
  946. * means the channel is in final wind-up. But we haven't sent
  947. * CLOSE, so let's do so now.
  948. */
  949. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_CLOSE);
  950. put_uint32(pktout, c->remoteid);
  951. pq_push(s->ppl.out_pq, pktout);
  952. c->closes |= CLOSES_SENT_EOF | CLOSES_SENT_CLOSE;
  953. }
  954. if (!((CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE) & ~c->closes)) {
  955. assert(c->chanreq_head == NULL);
  956. /*
  957. * We have both sent and received CLOSE, which means we're
  958. * completely done with the channel.
  959. */
  960. ssh2_channel_destroy(c);
  961. }
  962. }
  963. static void ssh2_channel_try_eof(struct ssh2_channel *c)
  964. {
  965. struct ssh2_connection_state *s = c->connlayer;
  966. PktOut *pktout;
  967. assert(c->pending_eof); /* precondition for calling us */
  968. if (c->halfopen)
  969. return; /* can't close: not even opened yet */
  970. if (bufchain_size(&c->outbuffer) > 0 || bufchain_size(&c->errbuffer) > 0)
  971. return; /* can't send EOF: pending outgoing data */
  972. c->pending_eof = false; /* we're about to send it */
  973. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_EOF);
  974. put_uint32(pktout, c->remoteid);
  975. pq_push(s->ppl.out_pq, pktout);
  976. c->closes |= CLOSES_SENT_EOF;
  977. ssh2_channel_check_close(c);
  978. }
  979. /*
  980. * Attempt to send data on an SSH-2 channel.
  981. */
  982. static size_t ssh2_try_send(struct ssh2_channel *c)
  983. {
  984. struct ssh2_connection_state *s = c->connlayer;
  985. PktOut *pktout;
  986. size_t bufsize;
  987. if (!c->halfopen) {
  988. while (c->remwindow > 0 &&
  989. (bufchain_size(&c->outbuffer) > 0 ||
  990. bufchain_size(&c->errbuffer) > 0)) {
  991. bufchain *buf = (bufchain_size(&c->errbuffer) > 0 ?
  992. &c->errbuffer : &c->outbuffer);
  993. ptrlen data = bufchain_prefix(buf);
  994. if (data.len > c->remwindow)
  995. data.len = c->remwindow;
  996. if (data.len > c->remmaxpkt)
  997. data.len = c->remmaxpkt;
  998. if (buf == &c->errbuffer) {
  999. pktout = ssh_bpp_new_pktout(
  1000. s->ppl.bpp, SSH2_MSG_CHANNEL_EXTENDED_DATA);
  1001. put_uint32(pktout, c->remoteid);
  1002. put_uint32(pktout, SSH2_EXTENDED_DATA_STDERR);
  1003. } else {
  1004. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_DATA);
  1005. put_uint32(pktout, c->remoteid);
  1006. }
  1007. put_stringpl(pktout, data);
  1008. pq_push(s->ppl.out_pq, pktout);
  1009. bufchain_consume(buf, data.len);
  1010. c->remwindow -= data.len;
  1011. }
  1012. }
  1013. /*
  1014. * After having sent as much data as we can, return the amount
  1015. * still buffered.
  1016. */
  1017. bufsize = bufchain_size(&c->outbuffer) + bufchain_size(&c->errbuffer);
  1018. /*
  1019. * And if there's no data pending but we need to send an EOF, send
  1020. * it.
  1021. */
  1022. if (!bufsize && c->pending_eof)
  1023. ssh2_channel_try_eof(c);
  1024. return bufsize;
  1025. }
  1026. static void ssh2_try_send_and_unthrottle(struct ssh2_channel *c)
  1027. {
  1028. int bufsize;
  1029. if (c->closes & CLOSES_SENT_EOF)
  1030. return; /* don't send on channels we've EOFed */
  1031. bufsize = ssh2_try_send(c);
  1032. if (bufsize == 0) {
  1033. c->throttled_by_backlog = false;
  1034. ssh2_channel_check_throttle(c);
  1035. }
  1036. }
  1037. static void ssh2_channel_check_throttle(struct ssh2_channel *c)
  1038. {
  1039. /*
  1040. * We don't want this channel to read further input if this
  1041. * particular channel has a backed-up SSH window, or if the
  1042. * outgoing side of the whole SSH connection is currently
  1043. * throttled, or if this channel already has an outgoing EOF
  1044. * either sent or pending.
  1045. */
  1046. chan_set_input_wanted(c->chan,
  1047. !c->throttled_by_backlog &&
  1048. !c->connlayer->all_channels_throttled &&
  1049. !c->pending_eof &&
  1050. !(c->closes & CLOSES_SENT_EOF));
  1051. }
  1052. /*
  1053. * Close any local socket and free any local resources associated with
  1054. * a channel. This converts the channel into a zombie.
  1055. */
  1056. static void ssh2_channel_close_local(struct ssh2_channel *c,
  1057. const char *reason)
  1058. {
  1059. struct ssh2_connection_state *s = c->connlayer;
  1060. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  1061. char *msg = NULL;
  1062. if (c->sharectx)
  1063. return;
  1064. msg = chan_log_close_msg(c->chan);
  1065. if (msg)
  1066. ppl_logevent("%s%s%s", msg, reason ? " " : "", reason ? reason : "");
  1067. sfree(msg);
  1068. chan_free(c->chan);
  1069. c->chan = zombiechan_new();
  1070. }
  1071. static void ssh2_check_termination_callback(void *vctx)
  1072. {
  1073. struct ssh2_connection_state *s = (struct ssh2_connection_state *)vctx;
  1074. ssh2_check_termination(s);
  1075. }
  1076. static void ssh2_channel_destroy(struct ssh2_channel *c)
  1077. {
  1078. struct ssh2_connection_state *s = c->connlayer;
  1079. assert(c->chanreq_head == NULL);
  1080. ssh2_channel_close_local(c, NULL);
  1081. del234(s->channels, c);
  1082. ssh2_channel_free(c);
  1083. /*
  1084. * If that was the last channel left open, we might need to
  1085. * terminate. But we'll be a bit cautious, by doing that in a
  1086. * toplevel callback, just in case anything on the current call
  1087. * stack objects to this entire PPL being freed.
  1088. */
  1089. queue_toplevel_callback(ssh2_check_termination_callback, s);
  1090. }
  1091. static void ssh2_check_termination(struct ssh2_connection_state *s)
  1092. {
  1093. /*
  1094. * Decide whether we should terminate the SSH connection now.
  1095. * Called after a channel or a downstream goes away. The general
  1096. * policy is that we terminate when none of either is left.
  1097. */
  1098. if (s->persistent)
  1099. return; /* persistent mode: never proactively terminate */
  1100. if (!s->started) {
  1101. /* At startup, we don't have any channels open because we
  1102. * haven't got round to opening the main one yet. In that
  1103. * situation, we don't want to terminate, even if a sharing
  1104. * connection opens and closes and causes a call to this
  1105. * function. */
  1106. return;
  1107. }
  1108. if (count234(s->channels) == 0 &&
  1109. !(s->connshare && share_ndownstreams(s->connshare) > 0)) {
  1110. /*
  1111. * We used to send SSH_MSG_DISCONNECT here, because I'd
  1112. * believed that _every_ conforming SSH-2 connection had to
  1113. * end with a disconnect being sent by at least one side;
  1114. * apparently I was wrong and it's perfectly OK to
  1115. * unceremoniously slam the connection shut when you're done,
  1116. * and indeed OpenSSH feels this is more polite than sending a
  1117. * DISCONNECT. So now we don't.
  1118. */
  1119. ssh_user_close(s->ppl.ssh, "All channels closed");
  1120. return;
  1121. }
  1122. }
  1123. /*
  1124. * Set up most of a new ssh2_channel. Nulls out sharectx, but leaves
  1125. * chan untouched (since it will sometimes have been filled in before
  1126. * calling this).
  1127. */
  1128. void ssh2_channel_init(struct ssh2_channel *c)
  1129. {
  1130. struct ssh2_connection_state *s = c->connlayer;
  1131. c->closes = 0;
  1132. c->pending_eof = false;
  1133. c->throttling_conn = false;
  1134. c->throttled_by_backlog = false;
  1135. c->sharectx = NULL;
  1136. c->locwindow = c->locmaxwin = c->remlocwin =
  1137. s->ssh_is_simple ? OUR_V2_BIGWIN : OUR_V2_WINSIZE;
  1138. c->chanreq_head = NULL;
  1139. c->throttle_state = UNTHROTTLED;
  1140. bufchain_init(&c->outbuffer);
  1141. bufchain_init(&c->errbuffer);
  1142. c->sc.vt = &ssh2channel_vtable;
  1143. c->sc.cl = &s->cl;
  1144. c->localid = alloc_channel_id(s->channels, struct ssh2_channel);
  1145. add234(s->channels, c);
  1146. }
  1147. /*
  1148. * Construct the common parts of a CHANNEL_OPEN.
  1149. */
  1150. PktOut *ssh2_chanopen_init(struct ssh2_channel *c, const char *type)
  1151. {
  1152. struct ssh2_connection_state *s = c->connlayer;
  1153. PktOut *pktout;
  1154. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_OPEN);
  1155. put_stringz(pktout, type);
  1156. put_uint32(pktout, c->localid);
  1157. put_uint32(pktout, c->locwindow); /* our window size */
  1158. put_uint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */
  1159. return pktout;
  1160. }
  1161. /*
  1162. * Construct the common parts of a CHANNEL_REQUEST. If handler is not
  1163. * NULL then a reply will be requested and the handler will be called
  1164. * when it arrives. The returned packet is ready to have any
  1165. * request-specific data added and be sent. Note that if a handler is
  1166. * provided, it's essential that the request actually be sent.
  1167. *
  1168. * The handler will usually be passed the response packet in pktin. If
  1169. * pktin is NULL, this means that no reply will ever be forthcoming
  1170. * (e.g. because the entire connection is being destroyed, or because
  1171. * the server initiated channel closure before we saw the response)
  1172. * and the handler should free any storage it's holding.
  1173. */
  1174. PktOut *ssh2_chanreq_init(struct ssh2_channel *c, const char *type,
  1175. cr_handler_fn_t handler, void *ctx)
  1176. {
  1177. struct ssh2_connection_state *s = c->connlayer;
  1178. PktOut *pktout;
  1179. assert(!(c->closes & (CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE)));
  1180. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_REQUEST);
  1181. put_uint32(pktout, c->remoteid);
  1182. put_stringz(pktout, type);
  1183. put_bool(pktout, handler != NULL);
  1184. if (handler != NULL) {
  1185. struct outstanding_channel_request *ocr =
  1186. snew(struct outstanding_channel_request);
  1187. ocr->handler = handler;
  1188. ocr->ctx = ctx;
  1189. ocr->next = NULL;
  1190. if (!c->chanreq_head)
  1191. c->chanreq_head = ocr;
  1192. else
  1193. c->chanreq_tail->next = ocr;
  1194. c->chanreq_tail = ocr;
  1195. }
  1196. return pktout;
  1197. }
  1198. static Conf *ssh2channel_get_conf(SshChannel *sc)
  1199. {
  1200. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1201. struct ssh2_connection_state *s = c->connlayer;
  1202. return s->conf;
  1203. }
  1204. static void ssh2channel_write_eof(SshChannel *sc)
  1205. {
  1206. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1207. if (c->closes & CLOSES_SENT_EOF)
  1208. return;
  1209. c->pending_eof = true;
  1210. ssh2_channel_try_eof(c);
  1211. }
  1212. static void ssh2channel_initiate_close(SshChannel *sc, const char *err)
  1213. {
  1214. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1215. char *reason;
  1216. reason = err ? dupprintf("due to local error: %s", err) : NULL;
  1217. ssh2_channel_close_local(c, reason);
  1218. sfree(reason);
  1219. c->pending_eof = false; /* this will confuse a zombie channel */
  1220. ssh2_channel_check_close(c);
  1221. }
  1222. static void ssh2channel_unthrottle(SshChannel *sc, size_t bufsize)
  1223. {
  1224. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1225. struct ssh2_connection_state *s = c->connlayer;
  1226. size_t buflimit;
  1227. buflimit = s->ssh_is_simple ? 0 : c->locmaxwin;
  1228. if (bufsize < buflimit)
  1229. ssh2_set_window(c, buflimit - bufsize);
  1230. if (c->throttling_conn && bufsize <= buflimit) {
  1231. c->throttling_conn = false;
  1232. ssh_throttle_conn(s->ppl.ssh, -1);
  1233. }
  1234. }
  1235. static size_t ssh2channel_write(
  1236. SshChannel *sc, bool is_stderr, const void *buf, size_t len)
  1237. {
  1238. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1239. assert(!(c->closes & CLOSES_SENT_EOF));
  1240. bufchain_add(is_stderr ? &c->errbuffer : &c->outbuffer, buf, len);
  1241. return ssh2_try_send(c);
  1242. }
  1243. static void ssh2channel_x11_sharing_handover(
  1244. SshChannel *sc, ssh_sharing_connstate *share_cs, share_channel *share_chan,
  1245. const char *peer_addr, int peer_port, int endian,
  1246. int protomajor, int protominor, const void *initial_data, int initial_len)
  1247. {
  1248. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1249. /*
  1250. * This function is called when we've just discovered that an X
  1251. * forwarding channel on which we'd been handling the initial auth
  1252. * ourselves turns out to be destined for a connection-sharing
  1253. * downstream. So we turn the channel into a sharing one, meaning
  1254. * that we completely stop tracking windows and buffering data and
  1255. * just pass more or less unmodified SSH messages back and forth.
  1256. */
  1257. c->sharectx = share_cs;
  1258. share_setup_x11_channel(share_cs, share_chan,
  1259. c->localid, c->remoteid, c->remwindow,
  1260. c->remmaxpkt, c->locwindow,
  1261. peer_addr, peer_port, endian,
  1262. protomajor, protominor,
  1263. initial_data, initial_len);
  1264. chan_free(c->chan);
  1265. c->chan = NULL;
  1266. }
  1267. static void ssh2channel_window_override_removed(SshChannel *sc)
  1268. {
  1269. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1270. struct ssh2_connection_state *s = c->connlayer;
  1271. /*
  1272. * This function is called when a client-side Channel has just
  1273. * stopped requiring an initial fixed-size window.
  1274. */
  1275. assert(!c->chan->initial_fixed_window_size);
  1276. ssh2_set_window(c, s->ssh_is_simple ? OUR_V2_BIGWIN : OUR_V2_WINSIZE);
  1277. }
  1278. static void ssh2channel_hint_channel_is_simple(SshChannel *sc)
  1279. {
  1280. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1281. struct ssh2_connection_state *s = c->connlayer;
  1282. PktOut *pktout = ssh2_chanreq_init(
  1283. c, "simple@putty.projects.tartarus.org", NULL, NULL);
  1284. pq_push(s->ppl.out_pq, pktout);
  1285. }
  1286. static SshChannel *ssh2_lportfwd_open(
  1287. ConnectionLayer *cl, const char *hostname, int port,
  1288. const char *description, const SocketPeerInfo *pi, Channel *chan)
  1289. {
  1290. struct ssh2_connection_state *s =
  1291. container_of(cl, struct ssh2_connection_state, cl);
  1292. struct ssh2_channel *c = snew(struct ssh2_channel);
  1293. PktOut *pktout;
  1294. c->connlayer = s;
  1295. ssh2_channel_init(c);
  1296. c->halfopen = true;
  1297. c->chan = chan;
  1298. pktout = ssh2_portfwd_chanopen(s, c, hostname, port, description, pi);
  1299. pq_push(s->ppl.out_pq, pktout);
  1300. return &c->sc;
  1301. }
  1302. static void ssh2_sharing_globreq_response(
  1303. struct ssh2_connection_state *s, PktIn *pktin, void *ctx)
  1304. {
  1305. ssh_sharing_connstate *cs = (ssh_sharing_connstate *)ctx;
  1306. share_got_pkt_from_server(cs, pktin->type,
  1307. BinarySource_UPCAST(pktin)->data,
  1308. BinarySource_UPCAST(pktin)->len);
  1309. }
  1310. static void ssh2_sharing_queue_global_request(
  1311. ConnectionLayer *cl, ssh_sharing_connstate *cs)
  1312. {
  1313. struct ssh2_connection_state *s =
  1314. container_of(cl, struct ssh2_connection_state, cl);
  1315. ssh2_queue_global_request_handler(s, ssh2_sharing_globreq_response, cs);
  1316. }
  1317. static void ssh2_sharing_no_more_downstreams(ConnectionLayer *cl)
  1318. {
  1319. struct ssh2_connection_state *s =
  1320. container_of(cl, struct ssh2_connection_state, cl);
  1321. queue_toplevel_callback(ssh2_check_termination_callback, s);
  1322. }
  1323. static struct X11FakeAuth *ssh2_add_x11_display(
  1324. ConnectionLayer *cl, int authtype, struct X11Display *disp)
  1325. {
  1326. struct ssh2_connection_state *s =
  1327. container_of(cl, struct ssh2_connection_state, cl);
  1328. struct X11FakeAuth *auth = x11_invent_fake_auth(s->x11authtree, authtype);
  1329. auth->disp = disp;
  1330. return auth;
  1331. }
  1332. static struct X11FakeAuth *ssh2_add_sharing_x11_display(
  1333. ConnectionLayer *cl, int authtype, ssh_sharing_connstate *share_cs,
  1334. share_channel *share_chan)
  1335. {
  1336. struct ssh2_connection_state *s =
  1337. container_of(cl, struct ssh2_connection_state, cl);
  1338. struct X11FakeAuth *auth;
  1339. /*
  1340. * Make up a new set of fake X11 auth data, and add it to the tree
  1341. * of currently valid ones with an indication of the sharing
  1342. * context that it's relevant to.
  1343. */
  1344. auth = x11_invent_fake_auth(s->x11authtree, authtype);
  1345. auth->share_cs = share_cs;
  1346. auth->share_chan = share_chan;
  1347. return auth;
  1348. }
  1349. static void ssh2_remove_sharing_x11_display(
  1350. ConnectionLayer *cl, struct X11FakeAuth *auth)
  1351. {
  1352. struct ssh2_connection_state *s =
  1353. container_of(cl, struct ssh2_connection_state, cl);
  1354. del234(s->x11authtree, auth);
  1355. x11_free_fake_auth(auth);
  1356. }
  1357. static unsigned ssh2_alloc_sharing_channel(
  1358. ConnectionLayer *cl, ssh_sharing_connstate *connstate)
  1359. {
  1360. struct ssh2_connection_state *s =
  1361. container_of(cl, struct ssh2_connection_state, cl);
  1362. struct ssh2_channel *c = snew(struct ssh2_channel);
  1363. c->connlayer = s;
  1364. ssh2_channel_init(c);
  1365. c->chan = NULL;
  1366. c->sharectx = connstate;
  1367. return c->localid;
  1368. }
  1369. static void ssh2_delete_sharing_channel(ConnectionLayer *cl, unsigned localid)
  1370. {
  1371. struct ssh2_connection_state *s =
  1372. container_of(cl, struct ssh2_connection_state, cl);
  1373. struct ssh2_channel *c = find234(s->channels, &localid, ssh2_channelfind);
  1374. if (c)
  1375. ssh2_channel_destroy(c);
  1376. }
  1377. static void ssh2_send_packet_from_downstream(
  1378. ConnectionLayer *cl, unsigned id, int type,
  1379. const void *data, int datalen, const char *additional_log_text)
  1380. {
  1381. struct ssh2_connection_state *s =
  1382. container_of(cl, struct ssh2_connection_state, cl);
  1383. PktOut *pkt = ssh_bpp_new_pktout(s->ppl.bpp, type);
  1384. pkt->downstream_id = id;
  1385. pkt->additional_log_text = additional_log_text;
  1386. put_data(pkt, data, datalen);
  1387. pq_push(s->ppl.out_pq, pkt);
  1388. }
  1389. static bool ssh2_agent_forwarding_permitted(ConnectionLayer *cl)
  1390. {
  1391. struct ssh2_connection_state *s =
  1392. container_of(cl, struct ssh2_connection_state, cl);
  1393. return conf_get_bool(s->conf, CONF_agentfwd) && agent_exists();
  1394. }
  1395. static bool ssh2_connection_get_specials(
  1396. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  1397. {
  1398. struct ssh2_connection_state *s =
  1399. container_of(ppl, struct ssh2_connection_state, ppl);
  1400. bool toret = false;
  1401. if (s->mainchan) {
  1402. mainchan_get_specials(s->mainchan, add_special, ctx);
  1403. toret = true;
  1404. }
  1405. /*
  1406. * Don't bother offering IGNORE if we've decided the remote
  1407. * won't cope with it, since we wouldn't bother sending it if
  1408. * asked anyway.
  1409. */
  1410. if (!(s->ppl.remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
  1411. if (toret)
  1412. add_special(ctx, NULL, SS_SEP, 0);
  1413. add_special(ctx, "IGNORE message", SS_NOP, 0);
  1414. toret = true;
  1415. }
  1416. return toret;
  1417. }
  1418. static void ssh2_connection_special_cmd(PacketProtocolLayer *ppl,
  1419. SessionSpecialCode code, int arg)
  1420. {
  1421. struct ssh2_connection_state *s =
  1422. container_of(ppl, struct ssh2_connection_state, ppl);
  1423. PktOut *pktout;
  1424. if (code == SS_PING || code == SS_NOP) {
  1425. if (!(s->ppl.remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
  1426. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_IGNORE);
  1427. put_stringz(pktout, "");
  1428. pq_push(s->ppl.out_pq, pktout);
  1429. }
  1430. } else if (s->mainchan) {
  1431. mainchan_special_cmd(s->mainchan, code, arg);
  1432. }
  1433. }
  1434. static void ssh2_terminal_size(ConnectionLayer *cl, int width, int height)
  1435. {
  1436. struct ssh2_connection_state *s =
  1437. container_of(cl, struct ssh2_connection_state, cl);
  1438. s->term_width = width;
  1439. s->term_height = height;
  1440. if (s->mainchan)
  1441. mainchan_terminal_size(s->mainchan, width, height);
  1442. }
  1443. static void ssh2_stdout_unthrottle(ConnectionLayer *cl, size_t bufsize)
  1444. {
  1445. struct ssh2_connection_state *s =
  1446. container_of(cl, struct ssh2_connection_state, cl);
  1447. if (s->mainchan)
  1448. sshfwd_unthrottle(s->mainchan_sc, bufsize);
  1449. }
  1450. static size_t ssh2_stdin_backlog(ConnectionLayer *cl)
  1451. {
  1452. struct ssh2_connection_state *s =
  1453. container_of(cl, struct ssh2_connection_state, cl);
  1454. struct ssh2_channel *c;
  1455. if (!s->mainchan)
  1456. return 0;
  1457. c = container_of(s->mainchan_sc, struct ssh2_channel, sc);
  1458. return s->mainchan ?
  1459. bufchain_size(&c->outbuffer) + bufchain_size(&c->errbuffer) : 0;
  1460. }
  1461. static void ssh2_throttle_all_channels(ConnectionLayer *cl, bool throttled)
  1462. {
  1463. struct ssh2_connection_state *s =
  1464. container_of(cl, struct ssh2_connection_state, cl);
  1465. struct ssh2_channel *c;
  1466. int i;
  1467. s->all_channels_throttled = throttled;
  1468. for (i = 0; NULL != (c = index234(s->channels, i)); i++)
  1469. if (!c->sharectx)
  1470. ssh2_channel_check_throttle(c);
  1471. }
  1472. static bool ssh2_ldisc_option(ConnectionLayer *cl, int option)
  1473. {
  1474. struct ssh2_connection_state *s =
  1475. container_of(cl, struct ssh2_connection_state, cl);
  1476. return s->ldisc_opts[option];
  1477. }
  1478. static void ssh2_set_ldisc_option(ConnectionLayer *cl, int option, bool value)
  1479. {
  1480. struct ssh2_connection_state *s =
  1481. container_of(cl, struct ssh2_connection_state, cl);
  1482. s->ldisc_opts[option] = value;
  1483. }
  1484. static void ssh2_enable_x_fwd(ConnectionLayer *cl)
  1485. {
  1486. struct ssh2_connection_state *s =
  1487. container_of(cl, struct ssh2_connection_state, cl);
  1488. s->X11_fwd_enabled = true;
  1489. }
  1490. static void ssh2_set_wants_user_input(ConnectionLayer *cl, bool wanted)
  1491. {
  1492. struct ssh2_connection_state *s =
  1493. container_of(cl, struct ssh2_connection_state, cl);
  1494. s->want_user_input = wanted;
  1495. }
  1496. static bool ssh2_connection_want_user_input(PacketProtocolLayer *ppl)
  1497. {
  1498. struct ssh2_connection_state *s =
  1499. container_of(ppl, struct ssh2_connection_state, ppl);
  1500. return s->want_user_input;
  1501. }
  1502. static void ssh2_connection_got_user_input(PacketProtocolLayer *ppl)
  1503. {
  1504. struct ssh2_connection_state *s =
  1505. container_of(ppl, struct ssh2_connection_state, ppl);
  1506. while (s->mainchan && bufchain_size(s->ppl.user_input) > 0) {
  1507. /*
  1508. * Add user input to the main channel's buffer.
  1509. */
  1510. ptrlen data = bufchain_prefix(s->ppl.user_input);
  1511. sshfwd_write(s->mainchan_sc, data.ptr, data.len);
  1512. bufchain_consume(s->ppl.user_input, data.len);
  1513. }
  1514. }
  1515. static void ssh2_connection_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  1516. {
  1517. struct ssh2_connection_state *s =
  1518. container_of(ppl, struct ssh2_connection_state, ppl);
  1519. conf_free(s->conf);
  1520. s->conf = conf_copy(conf);
  1521. if (s->portfwdmgr_configured)
  1522. portfwdmgr_config(s->portfwdmgr, s->conf);
  1523. }