ssh2connection.c 64 KB

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