ssh2connection.c 63 KB

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