ssh2connection.c 64 KB

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