ssh2connection.c 66 KB

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