ssh2connection.c 56 KB

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