1
0

connection2.c 66 KB

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