ssh2connection.c 63 KB

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