connection2.c 62 KB

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