connection2.c 66 KB

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