connection2.c 66 KB

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