ssh2connection.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  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. // WINSCP
  12. #define queue_toplevel_callback(FN, CTX) queue_toplevel_callback(get_log_callback_set(CTX->cl.logctx), FN, CTX)
  13. struct ssh2_channel;
  14. typedef enum MainChanType {
  15. MAINCHAN_DIRECT_TCPIP, MAINCHAN_SESSION, MAINCHAN_NONE
  16. } MainChanType;
  17. struct outstanding_global_request;
  18. struct ssh2_connection_state {
  19. int crState;
  20. Ssh *ssh;
  21. ssh_sharing_state *connshare;
  22. char *peer_verstring;
  23. struct ssh2_channel *mainchan; /* primary session channel */
  24. MainChanType mctype;
  25. char *mainchan_open_error;
  26. int mainchan_ready;
  27. int echoedit;
  28. int mainchan_eof_pending, mainchan_eof_sent;
  29. int session_attempt, session_status;
  30. int term_width, term_height, term_width_orig, term_height_orig;
  31. int want_user_input;
  32. int ssh_is_simple;
  33. Conf *conf;
  34. tree234 *channels; /* indexed by local id */
  35. int all_channels_throttled;
  36. int X11_fwd_enabled;
  37. struct X11Display *x11disp;
  38. struct X11FakeAuth *x11auth;
  39. tree234 *x11authtree;
  40. int got_pty;
  41. int agent_fwd_enabled;
  42. tree234 *rportfwds;
  43. PortFwdManager *portfwdmgr;
  44. int portfwdmgr_configured;
  45. /*
  46. * These store the list of global requests that we're waiting for
  47. * replies to. (REQUEST_FAILURE doesn't come with any indication
  48. * of what message caused it, so we have to keep track of the
  49. * queue ourselves.)
  50. */
  51. struct outstanding_global_request *globreq_head, *globreq_tail;
  52. ConnectionLayer cl;
  53. PacketProtocolLayer ppl;
  54. };
  55. static int ssh2_rportfwd_cmp(void *av, void *bv)
  56. {
  57. struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
  58. struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
  59. int i;
  60. if ( (i = strcmp(a->shost, b->shost)) != 0)
  61. return i < 0 ? -1 : +1;
  62. if (a->sport > b->sport)
  63. return +1;
  64. if (a->sport < b->sport)
  65. return -1;
  66. return 0;
  67. }
  68. static void ssh2_connection_free(PacketProtocolLayer *);
  69. static void ssh2_connection_process_queue(PacketProtocolLayer *);
  70. static int ssh2_connection_get_specials(
  71. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx);
  72. static void ssh2_connection_special_cmd(PacketProtocolLayer *ppl,
  73. SessionSpecialCode code, int arg);
  74. static int ssh2_connection_want_user_input(PacketProtocolLayer *ppl);
  75. static void ssh2_connection_got_user_input(PacketProtocolLayer *ppl);
  76. static void ssh2_connection_reconfigure(PacketProtocolLayer *ppl, Conf *conf);
  77. static unsigned int ssh2_connection_winscp_query(PacketProtocolLayer *ppl, int query);
  78. static const struct PacketProtocolLayerVtable ssh2_connection_vtable = {
  79. ssh2_connection_free,
  80. ssh2_connection_process_queue,
  81. ssh2_connection_get_specials,
  82. ssh2_connection_special_cmd,
  83. ssh2_connection_want_user_input,
  84. ssh2_connection_got_user_input,
  85. ssh2_connection_reconfigure,
  86. "ssh-connection",
  87. ssh2_connection_winscp_query,
  88. };
  89. static struct ssh_rportfwd *ssh2_rportfwd_alloc(
  90. ConnectionLayer *cl,
  91. const char *shost, int sport, const char *dhost, int dport,
  92. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  93. ssh_sharing_connstate *share_ctx);
  94. static void ssh2_rportfwd_remove(
  95. ConnectionLayer *cl, struct ssh_rportfwd *rpf);
  96. static SshChannel *ssh2_lportfwd_open(
  97. ConnectionLayer *cl, const char *hostname, int port,
  98. const char *org, Channel *chan);
  99. static struct X11FakeAuth *ssh2_add_sharing_x11_display(
  100. ConnectionLayer *cl, int authtype, ssh_sharing_connstate *share_cs,
  101. share_channel *share_chan);
  102. static void ssh2_remove_sharing_x11_display(ConnectionLayer *cl,
  103. struct X11FakeAuth *auth);
  104. static void ssh2_send_packet_from_downstream(
  105. ConnectionLayer *cl, unsigned id, int type,
  106. const void *pkt, int pktlen, const char *additional_log_text);
  107. static unsigned ssh2_alloc_sharing_channel(
  108. ConnectionLayer *cl, ssh_sharing_connstate *connstate);
  109. static void ssh2_delete_sharing_channel(
  110. ConnectionLayer *cl, unsigned localid);
  111. static void ssh2_sharing_queue_global_request(
  112. ConnectionLayer *cl, ssh_sharing_connstate *share_ctx);
  113. static void ssh2_sharing_no_more_downstreams(ConnectionLayer *cl);
  114. static int ssh2_agent_forwarding_permitted(ConnectionLayer *cl);
  115. static void ssh2_terminal_size(ConnectionLayer *cl, int width, int height);
  116. static void ssh2_stdout_unthrottle(ConnectionLayer *cl, int bufsize);
  117. static int ssh2_stdin_backlog(ConnectionLayer *cl);
  118. static void ssh2_throttle_all_channels(ConnectionLayer *cl, int throttled);
  119. static int ssh2_ldisc_option(ConnectionLayer *cl, int option);
  120. static const struct ConnectionLayerVtable ssh2_connlayer_vtable = {
  121. ssh2_rportfwd_alloc,
  122. ssh2_rportfwd_remove,
  123. ssh2_lportfwd_open,
  124. ssh2_add_sharing_x11_display,
  125. ssh2_remove_sharing_x11_display,
  126. ssh2_send_packet_from_downstream,
  127. ssh2_alloc_sharing_channel,
  128. ssh2_delete_sharing_channel,
  129. ssh2_sharing_queue_global_request,
  130. ssh2_sharing_no_more_downstreams,
  131. ssh2_agent_forwarding_permitted,
  132. ssh2_terminal_size,
  133. ssh2_stdout_unthrottle,
  134. ssh2_stdin_backlog,
  135. ssh2_throttle_all_channels,
  136. ssh2_ldisc_option,
  137. };
  138. static char *ssh2_channel_open_failure_error_text(PktIn *pktin)
  139. {
  140. static const char *const reasons[] = {
  141. NULL,
  142. "Administratively prohibited",
  143. "Connect failed",
  144. "Unknown channel type",
  145. "Resource shortage",
  146. };
  147. unsigned reason_code;
  148. const char *reason_code_string;
  149. char reason_code_buf[256];
  150. ptrlen reason;
  151. reason_code = get_uint32(pktin);
  152. if (reason_code < lenof(reasons) && reasons[reason_code]) {
  153. reason_code_string = reasons[reason_code];
  154. } else {
  155. reason_code_string = reason_code_buf;
  156. sprintf(reason_code_buf, "unknown reason code %#x", reason_code);
  157. }
  158. reason = get_string(pktin);
  159. return dupprintf("%s [%.*s]", reason_code_string, PTRLEN_PRINTF(reason));
  160. }
  161. struct outstanding_channel_request;
  162. struct outstanding_global_request;
  163. struct ssh2_channel {
  164. struct ssh2_connection_state *connlayer;
  165. unsigned remoteid, localid;
  166. int type;
  167. /* True if we opened this channel but server hasn't confirmed. */
  168. int halfopen;
  169. /* Bitmap of whether we've sent/received CHANNEL_EOF and
  170. * CHANNEL_CLOSE. */
  171. #define CLOSES_SENT_EOF 1
  172. #define CLOSES_SENT_CLOSE 2
  173. #define CLOSES_RCVD_EOF 4
  174. #define CLOSES_RCVD_CLOSE 8
  175. int closes;
  176. /*
  177. * This flag indicates that an EOF is pending on the outgoing side
  178. * of the channel: that is, wherever we're getting the data for
  179. * this channel has sent us some data followed by EOF. We can't
  180. * actually send the EOF until we've finished sending the data, so
  181. * we set this flag instead to remind us to do so once our buffer
  182. * is clear.
  183. */
  184. int pending_eof;
  185. /*
  186. * True if this channel is causing the underlying connection to be
  187. * throttled.
  188. */
  189. int throttling_conn;
  190. /*
  191. * True if we currently have backed-up data on the direction of
  192. * this channel pointing out of the SSH connection, and therefore
  193. * would prefer the 'Channel' implementation not to read further
  194. * local input if possible.
  195. */
  196. int throttled_by_backlog;
  197. bufchain outbuffer;
  198. unsigned remwindow, remmaxpkt;
  199. /* locwindow is signed so we can cope with excess data. */
  200. int locwindow, locmaxwin;
  201. /*
  202. * remlocwin is the amount of local window that we think
  203. * the remote end had available to it after it sent the
  204. * last data packet or window adjust ack.
  205. */
  206. int remlocwin;
  207. /*
  208. * These store the list of channel requests that we're waiting for
  209. * replies to. (CHANNEL_FAILURE doesn't come with any indication
  210. * of what message caused it, so we have to keep track of the
  211. * queue ourselves.)
  212. */
  213. struct outstanding_channel_request *chanreq_head, *chanreq_tail;
  214. enum { THROTTLED, UNTHROTTLING, UNTHROTTLED } throttle_state;
  215. ssh_sharing_connstate *sharectx; /* sharing context, if this is a
  216. * downstream channel */
  217. Channel *chan; /* handle the client side of this channel, if not */
  218. SshChannel sc; /* entry point for chan to talk back to */
  219. };
  220. static int ssh2channel_write(SshChannel *c, const void *buf, int len);
  221. static void ssh2channel_write_eof(SshChannel *c);
  222. static void ssh2channel_unclean_close(SshChannel *c, const char *err);
  223. static void ssh2channel_unthrottle(SshChannel *c, int bufsize);
  224. static Conf *ssh2channel_get_conf(SshChannel *c);
  225. static void ssh2channel_window_override_removed(SshChannel *c);
  226. static void ssh2channel_x11_sharing_handover(
  227. SshChannel *c, ssh_sharing_connstate *share_cs, share_channel *share_chan,
  228. const char *peer_addr, int peer_port, int endian,
  229. int protomajor, int protominor, const void *initial_data, int initial_len);
  230. static const struct SshChannelVtable ssh2channel_vtable = {
  231. ssh2channel_write,
  232. ssh2channel_write_eof,
  233. ssh2channel_unclean_close,
  234. ssh2channel_unthrottle,
  235. ssh2channel_get_conf,
  236. ssh2channel_window_override_removed,
  237. ssh2channel_x11_sharing_handover,
  238. };
  239. typedef void (*cr_handler_fn_t)(struct ssh2_channel *, PktIn *, void *);
  240. static void ssh2_channel_init(struct ssh2_channel *c);
  241. static PktOut *ssh2_chanopen_init(struct ssh2_channel *c, const char *type);
  242. static PktOut *ssh2_chanreq_init(struct ssh2_channel *c, const char *type,
  243. cr_handler_fn_t handler, void *ctx);
  244. static void ssh2_channel_check_close(struct ssh2_channel *c);
  245. static void ssh2_channel_try_eof(struct ssh2_channel *c);
  246. static void ssh2_set_window(struct ssh2_channel *c, int newwin);
  247. static int ssh2_try_send(struct ssh2_channel *c);
  248. static void ssh2_try_send_and_unthrottle(struct ssh2_channel *c);
  249. static void ssh2_channel_check_throttle(struct ssh2_channel *c);
  250. static void ssh2_channel_close_local(struct ssh2_channel *c,
  251. const char *reason);
  252. static void ssh2_channel_destroy(struct ssh2_channel *c);
  253. static void ssh2_check_termination(struct ssh2_connection_state *s);
  254. typedef void (*gr_handler_fn_t)(struct ssh2_connection_state *s,
  255. PktIn *pktin, void *ctx);
  256. struct outstanding_global_request {
  257. gr_handler_fn_t handler;
  258. void *ctx;
  259. struct outstanding_global_request *next;
  260. };
  261. static void ssh2_queue_global_request_handler(
  262. struct ssh2_connection_state *s, gr_handler_fn_t handler, void *ctx)
  263. {
  264. struct outstanding_global_request *ogr =
  265. snew(struct outstanding_global_request);
  266. ogr->handler = handler;
  267. ogr->ctx = ctx;
  268. if (s->globreq_tail)
  269. s->globreq_tail->next = ogr;
  270. else
  271. s->globreq_head = ogr;
  272. s->globreq_tail = ogr;
  273. }
  274. typedef struct mainchan {
  275. struct ssh2_connection_state *connlayer;
  276. SshChannel *sc;
  277. Channel chan;
  278. } mainchan;
  279. static mainchan *mainchan_new(struct ssh2_connection_state *s);
  280. static void ssh2_setup_x11(struct ssh2_channel *c, PktIn *pktin, void *ctx);
  281. static void ssh2_setup_agent(struct ssh2_channel *c, PktIn *pktin, void *ctx);
  282. static void ssh2_setup_pty(struct ssh2_channel *c, PktIn *pktin, void *ctx);
  283. static void ssh2_setup_env(struct ssh2_channel *c, PktIn *pktin, void *ctx);
  284. static void ssh2_response_session(struct ssh2_channel *c, PktIn *, void *);
  285. static int ssh2_channelcmp(void *av, void *bv)
  286. {
  287. const struct ssh2_channel *a = (const struct ssh2_channel *) av;
  288. const struct ssh2_channel *b = (const struct ssh2_channel *) bv;
  289. if (a->localid < b->localid)
  290. return -1;
  291. if (a->localid > b->localid)
  292. return +1;
  293. return 0;
  294. }
  295. static int ssh2_channelfind(void *av, void *bv)
  296. {
  297. const unsigned *a = (const unsigned *) av;
  298. const struct ssh2_channel *b = (const struct ssh2_channel *) bv;
  299. if (*a < b->localid)
  300. return -1;
  301. if (*a > b->localid)
  302. return +1;
  303. return 0;
  304. }
  305. /*
  306. * Each channel has a queue of outstanding CHANNEL_REQUESTS and their
  307. * handlers.
  308. */
  309. struct outstanding_channel_request {
  310. cr_handler_fn_t handler;
  311. void *ctx;
  312. struct outstanding_channel_request *next;
  313. };
  314. static void ssh2_channel_free(struct ssh2_channel *c)
  315. {
  316. bufchain_clear(&c->outbuffer);
  317. while (c->chanreq_head) {
  318. struct outstanding_channel_request *chanreq = c->chanreq_head;
  319. c->chanreq_head = c->chanreq_head->next;
  320. sfree(chanreq);
  321. }
  322. if (c->chan)
  323. chan_free(c->chan);
  324. sfree(c);
  325. }
  326. PacketProtocolLayer *ssh2_connection_new(
  327. Ssh *ssh, ssh_sharing_state *connshare, int is_simple,
  328. Conf *conf, const char *peer_verstring, ConnectionLayer **cl_out)
  329. {
  330. struct ssh2_connection_state *s = snew(struct ssh2_connection_state);
  331. memset(s, 0, sizeof(*s));
  332. s->ppl.vt = &ssh2_connection_vtable;
  333. s->conf = conf_copy(conf);
  334. s->ssh_is_simple = is_simple;
  335. s->connshare = connshare;
  336. s->peer_verstring = dupstr(peer_verstring);
  337. s->channels = newtree234(ssh2_channelcmp);
  338. s->x11authtree = newtree234(x11_authcmp);
  339. /* Need to get the log context for s->cl now, because we won't be
  340. * helpfully notified when a copy is written into s->ppl by our
  341. * owner. */
  342. s->cl.vt = &ssh2_connlayer_vtable;
  343. s->cl.logctx = ssh_get_logctx(ssh);
  344. s->portfwdmgr = portfwdmgr_new(&s->cl);
  345. s->rportfwds = newtree234(ssh2_rportfwd_cmp);
  346. *cl_out = &s->cl;
  347. if (s->connshare)
  348. ssh_connshare_provide_connlayer(s->connshare, &s->cl);
  349. return &s->ppl;
  350. }
  351. static void ssh2_connection_free(PacketProtocolLayer *ppl)
  352. {
  353. struct ssh2_connection_state *s =
  354. container_of(ppl, struct ssh2_connection_state, ppl);
  355. struct X11FakeAuth *auth;
  356. struct ssh2_channel *c;
  357. struct ssh_rportfwd *rpf;
  358. sfree(s->peer_verstring);
  359. conf_free(s->conf);
  360. sfree(s->mainchan_open_error);
  361. while ((c = delpos234(s->channels, 0)) != NULL)
  362. ssh2_channel_free(c);
  363. freetree234(s->channels);
  364. if (s->x11disp)
  365. x11_free_display(s->x11disp);
  366. while ((auth = delpos234(s->x11authtree, 0)) != NULL)
  367. x11_free_fake_auth(auth);
  368. freetree234(s->x11authtree);
  369. while ((rpf = delpos234(s->rportfwds, 0)) != NULL)
  370. free_rportfwd(rpf);
  371. freetree234(s->rportfwds);
  372. portfwdmgr_free(s->portfwdmgr);
  373. sfree(s);
  374. }
  375. static int ssh2_connection_filter_queue(struct ssh2_connection_state *s)
  376. {
  377. PktIn *pktin;
  378. PktOut *pktout;
  379. ptrlen type, data;
  380. struct ssh2_channel *c;
  381. struct outstanding_channel_request *ocr;
  382. unsigned localid, remid, winsize, pktsize, ext_type;
  383. int want_reply, reply_type, expect_halfopen;
  384. const char *error;
  385. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  386. /* Cross-reference to ssh2transport.c to handle the common packets
  387. * between login and connection: DISCONNECT, DEBUG and IGNORE. If
  388. * we have an instance of ssh2transport below us, then those
  389. * messages won't come here anyway, but they could if we're
  390. * running in bare ssh2-connection mode. */
  391. extern int ssh2_common_filter_queue(PacketProtocolLayer *ppl);
  392. while (1) {
  393. if (ssh2_common_filter_queue(&s->ppl))
  394. return TRUE;
  395. if ((pktin = pq_peek(s->ppl.in_pq)) == NULL)
  396. return FALSE;
  397. switch (pktin->type) {
  398. case SSH2_MSG_GLOBAL_REQUEST:
  399. /* type = */ get_string(pktin);
  400. want_reply = get_bool(pktin);
  401. /*
  402. * 'reply_type' is the message type we'll send in
  403. * response, if want_reply is set. Initialise it to the
  404. * default value of REQUEST_FAILURE, for any request we
  405. * don't recognise and handle below.
  406. */
  407. reply_type = SSH2_MSG_REQUEST_FAILURE;
  408. /*
  409. * We currently don't support any incoming global requests
  410. * at all. Here's where to insert some code to handle
  411. * them, if and when we do.
  412. */
  413. if (want_reply) {
  414. pktout = ssh_bpp_new_pktout(s->ppl.bpp, reply_type);
  415. pq_push(s->ppl.out_pq, pktout);
  416. }
  417. pq_pop(s->ppl.in_pq);
  418. break;
  419. case SSH2_MSG_REQUEST_SUCCESS:
  420. case SSH2_MSG_REQUEST_FAILURE:
  421. if (!s->globreq_head) {
  422. ssh_proto_error(
  423. s->ppl.ssh,
  424. "Received %s with no outstanding global request",
  425. ssh2_pkt_type(s->ppl.bpp->pls->kctx, s->ppl.bpp->pls->actx,
  426. pktin->type));
  427. return TRUE;
  428. }
  429. s->globreq_head->handler(s, pktin, s->globreq_head->ctx);
  430. {
  431. struct outstanding_global_request *tmp = s->globreq_head;
  432. s->globreq_head = s->globreq_head->next;
  433. sfree(tmp);
  434. }
  435. pq_pop(s->ppl.in_pq);
  436. break;
  437. case SSH2_MSG_CHANNEL_OPEN:
  438. error = NULL;
  439. type = get_string(pktin);
  440. c = snew(struct ssh2_channel);
  441. c->connlayer = s;
  442. remid = get_uint32(pktin);
  443. winsize = get_uint32(pktin);
  444. pktsize = get_uint32(pktin);
  445. if (ptrlen_eq_string(type, "x11")) {
  446. char *addrstr = mkstr(get_string(pktin));
  447. int peerport = get_uint32(pktin);
  448. ppl_logevent(("Received X11 connect request from %s:%d",
  449. addrstr, peerport));
  450. if (!s->X11_fwd_enabled && !s->connshare) {
  451. error = "X11 forwarding is not enabled";
  452. } else {
  453. c->chan = x11_new_channel(
  454. s->x11authtree, &c->sc, addrstr, peerport,
  455. s->connshare != NULL);
  456. ppl_logevent(("Opened X11 forward channel"));
  457. }
  458. sfree(addrstr);
  459. } else if (ptrlen_eq_string(type, "forwarded-tcpip")) {
  460. struct ssh_rportfwd pf, *realpf;
  461. ptrlen peeraddr;
  462. int peerport;
  463. pf.shost = mkstr(get_string(pktin));
  464. pf.sport = get_uint32(pktin);
  465. peeraddr = get_string(pktin);
  466. peerport = get_uint32(pktin);
  467. realpf = find234(s->rportfwds, &pf, NULL);
  468. ppl_logevent(("Received remote port %s:%d open request "
  469. "from %.*s:%d", pf.shost, pf.sport,
  470. PTRLEN_PRINTF(peeraddr), peerport));
  471. sfree(pf.shost);
  472. if (realpf == NULL) {
  473. error = "Remote port is not recognised";
  474. } else {
  475. char *err;
  476. if (realpf->share_ctx) {
  477. /*
  478. * This port forwarding is on behalf of a
  479. * connection-sharing downstream, so abandon our own
  480. * channel-open procedure and just pass the message on
  481. * to sshshare.c.
  482. */
  483. share_got_pkt_from_server(
  484. realpf->share_ctx, pktin->type,
  485. BinarySource_UPCAST(pktin)->data,
  486. BinarySource_UPCAST(pktin)->len);
  487. sfree(c);
  488. break;
  489. }
  490. err = portfwdmgr_connect(
  491. s->portfwdmgr, &c->chan, realpf->dhost, realpf->dport,
  492. &c->sc, realpf->addressfamily);
  493. ppl_logevent(("Attempting to forward remote port to "
  494. "%s:%d", realpf->dhost, realpf->dport));
  495. if (err != NULL) {
  496. ppl_logevent(("Port open failed: %s", err));
  497. sfree(err);
  498. error = "Port open failed";
  499. } else {
  500. ppl_logevent(("Forwarded port opened successfully"));
  501. }
  502. }
  503. } else if (ptrlen_eq_string(type, "[email protected]")) {
  504. if (!s->agent_fwd_enabled)
  505. error = "Agent forwarding is not enabled";
  506. else
  507. c->chan = agentf_new(&c->sc);
  508. } else {
  509. error = "Unsupported channel type requested";
  510. }
  511. c->remoteid = remid;
  512. c->halfopen = FALSE;
  513. if (error) {
  514. pktout = ssh_bpp_new_pktout(
  515. s->ppl.bpp, SSH2_MSG_CHANNEL_OPEN_FAILURE);
  516. put_uint32(pktout, c->remoteid);
  517. put_uint32(pktout, SSH2_OPEN_CONNECT_FAILED);
  518. put_stringz(pktout, error);
  519. put_stringz(pktout, "en"); /* language tag */
  520. pq_push(s->ppl.out_pq, pktout);
  521. ppl_logevent(("Rejected channel open: %s", error));
  522. sfree(c);
  523. } else {
  524. ssh2_channel_init(c);
  525. c->remwindow = winsize;
  526. c->remmaxpkt = pktsize;
  527. if (c->chan->initial_fixed_window_size) {
  528. c->locwindow = c->locmaxwin = c->remlocwin =
  529. c->chan->initial_fixed_window_size;
  530. }
  531. pktout = ssh_bpp_new_pktout(
  532. s->ppl.bpp, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
  533. put_uint32(pktout, c->remoteid);
  534. put_uint32(pktout, c->localid);
  535. put_uint32(pktout, c->locwindow);
  536. put_uint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */
  537. pq_push(s->ppl.out_pq, pktout);
  538. }
  539. pq_pop(s->ppl.in_pq);
  540. break;
  541. case SSH2_MSG_CHANNEL_DATA:
  542. case SSH2_MSG_CHANNEL_EXTENDED_DATA:
  543. case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
  544. case SSH2_MSG_CHANNEL_REQUEST:
  545. case SSH2_MSG_CHANNEL_EOF:
  546. case SSH2_MSG_CHANNEL_CLOSE:
  547. case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
  548. case SSH2_MSG_CHANNEL_OPEN_FAILURE:
  549. case SSH2_MSG_CHANNEL_SUCCESS:
  550. case SSH2_MSG_CHANNEL_FAILURE:
  551. /*
  552. * Common preliminary code for all the messages from the
  553. * server that cite one of our channel ids: look up that
  554. * channel id, check it exists, and if it's for a sharing
  555. * downstream, pass it on.
  556. */
  557. localid = get_uint32(pktin);
  558. c = find234(s->channels, &localid, ssh2_channelfind);
  559. if (c && c->sharectx) {
  560. share_got_pkt_from_server(c->sharectx, pktin->type,
  561. BinarySource_UPCAST(pktin)->data,
  562. BinarySource_UPCAST(pktin)->len);
  563. pq_pop(s->ppl.in_pq);
  564. break;
  565. }
  566. expect_halfopen = (
  567. pktin->type == SSH2_MSG_CHANNEL_OPEN_CONFIRMATION ||
  568. pktin->type == SSH2_MSG_CHANNEL_OPEN_FAILURE);
  569. if (!c || c->halfopen != expect_halfopen) {
  570. ssh_proto_error(s->ppl.ssh,
  571. "Received %s for %s channel %u",
  572. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  573. s->ppl.bpp->pls->actx,
  574. pktin->type),
  575. (!c ? "nonexistent" :
  576. c->halfopen ? "half-open" : "open"),
  577. localid);
  578. return TRUE;
  579. }
  580. switch (pktin->type) {
  581. case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
  582. assert(c->halfopen);
  583. c->remoteid = get_uint32(pktin);
  584. c->halfopen = FALSE;
  585. c->remwindow = get_uint32(pktin);
  586. c->remmaxpkt = get_uint32(pktin);
  587. chan_open_confirmation(c->chan);
  588. /*
  589. * Now that the channel is fully open, it's possible
  590. * in principle to immediately close it. Check whether
  591. * it wants us to!
  592. *
  593. * This can occur if a local socket error occurred
  594. * between us sending out CHANNEL_OPEN and receiving
  595. * OPEN_CONFIRMATION. If that happens, all we can do
  596. * is immediately initiate close proceedings now that
  597. * we know the server's id to put in the close
  598. * message. We'll have handled that in this code by
  599. * having already turned c->chan into a zombie, so its
  600. * want_close method (which ssh2_channel_check_close
  601. * will consult) will already be returning TRUE.
  602. */
  603. ssh2_channel_check_close(c);
  604. if (c->pending_eof)
  605. ssh2_channel_try_eof(c); /* in case we had a pending EOF */
  606. break;
  607. case SSH2_MSG_CHANNEL_OPEN_FAILURE:
  608. assert(c->halfopen);
  609. {
  610. char *err = ssh2_channel_open_failure_error_text(pktin);
  611. chan_open_failed(c->chan, err);
  612. sfree(err);
  613. }
  614. chan_free(c->chan);
  615. del234(s->channels, c);
  616. ssh2_channel_free(c);
  617. break;
  618. case SSH2_MSG_CHANNEL_DATA:
  619. case SSH2_MSG_CHANNEL_EXTENDED_DATA:
  620. ext_type = (pktin->type == SSH2_MSG_CHANNEL_DATA ? 0 :
  621. get_uint32(pktin));
  622. data = get_string(pktin);
  623. if (!get_err(pktin)) {
  624. int bufsize;
  625. c->locwindow -= data.len;
  626. c->remlocwin -= data.len;
  627. if (ext_type != 0 && ext_type != SSH2_EXTENDED_DATA_STDERR)
  628. data.len = 0; /* ignore unknown extended data */
  629. bufsize = chan_send(
  630. c->chan, ext_type == SSH2_EXTENDED_DATA_STDERR,
  631. data.ptr, data.len);
  632. /*
  633. * If it looks like the remote end hit the end of
  634. * its window, and we didn't want it to do that,
  635. * think about using a larger window.
  636. */
  637. if (c->remlocwin <= 0 &&
  638. c->throttle_state == UNTHROTTLED &&
  639. c->locmaxwin < 0x40000000)
  640. c->locmaxwin += OUR_V2_WINSIZE;
  641. /*
  642. * If we are not buffering too much data, enlarge
  643. * the window again at the remote side. If we are
  644. * buffering too much, we may still need to adjust
  645. * the window if the server's sent excess data.
  646. */
  647. if (bufsize < c->locmaxwin)
  648. ssh2_set_window(c, c->locmaxwin - bufsize);
  649. /*
  650. * If we're either buffering way too much data, or
  651. * if we're buffering anything at all and we're in
  652. * "simple" mode, throttle the whole channel.
  653. */
  654. if ((bufsize > c->locmaxwin ||
  655. (s->ssh_is_simple && bufsize>0)) &&
  656. !c->throttling_conn) {
  657. c->throttling_conn = TRUE;
  658. ssh_throttle_conn(s->ppl.ssh, +1);
  659. }
  660. }
  661. break;
  662. case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
  663. if (!(c->closes & CLOSES_SENT_EOF)) {
  664. c->remwindow += get_uint32(pktin);
  665. ssh2_try_send_and_unthrottle(c);
  666. }
  667. break;
  668. case SSH2_MSG_CHANNEL_REQUEST:
  669. type = get_string(pktin);
  670. want_reply = get_bool(pktin);
  671. /*
  672. * 'reply_type' is the message type we'll send in
  673. * response, if want_reply is set. Initialise it to
  674. * the default value of CHANNEL_FAILURE, for any
  675. * request we don't recognise and handle below.
  676. */
  677. reply_type = SSH2_MSG_CHANNEL_FAILURE;
  678. if (c->closes & CLOSES_SENT_CLOSE) {
  679. /*
  680. * We don't reply to channel requests after we've
  681. * sent CHANNEL_CLOSE for the channel, because our
  682. * reply might cross in the network with the other
  683. * side's CHANNEL_CLOSE and arrive after they have
  684. * wound the channel up completely.
  685. */
  686. want_reply = FALSE;
  687. }
  688. /*
  689. * Having got the channel number, we now look at the
  690. * request type string to see if it's something we
  691. * recognise.
  692. */
  693. if (c == s->mainchan) {
  694. int exitcode;
  695. /*
  696. * We recognise "exit-status" and "exit-signal" on
  697. * the primary channel.
  698. */
  699. if (ptrlen_eq_string(type, "exit-status")) {
  700. exitcode = toint(get_uint32(pktin));
  701. ssh_got_exitcode(s->ppl.ssh, exitcode);
  702. ppl_logevent(("Server sent command exit status %d",
  703. exitcode));
  704. reply_type = SSH2_MSG_CHANNEL_SUCCESS;
  705. } else if (ptrlen_eq_string(type, "exit-signal")) {
  706. char *fmt_sig = NULL, *fmt_msg = NULL;
  707. ptrlen errmsg;
  708. int core = FALSE;
  709. int format;
  710. /*
  711. * ICK: older versions of OpenSSH (e.g. 3.4p1)
  712. * provide an `int' for the signal, despite
  713. * its having been a `string' in the drafts of
  714. * RFC 4254 since at least 2001. (Fixed in
  715. * session.c 1.147.) Try to infer which we can
  716. * safely parse it as.
  717. */
  718. size_t startpos = BinarySource_UPCAST(pktin)->pos;
  719. for (format = 0; format < 2; format++) {
  720. BinarySource_UPCAST(pktin)->pos = startpos;
  721. BinarySource_UPCAST(pktin)->err = BSE_NO_ERROR;
  722. if (format == 0) {
  723. /* standard string-based format */
  724. ptrlen signame = get_string(pktin);
  725. fmt_sig = dupprintf(" \"%.*s\"",
  726. PTRLEN_PRINTF(signame));
  727. /*
  728. * Really hideous method of translating the
  729. * signal description back into a locally
  730. * meaningful number.
  731. */
  732. if (0)
  733. ;
  734. #define TRANSLATE_SIGNAL(s) \
  735. else if (ptrlen_eq_string(signame, #s)) \
  736. exitcode = 128 + SIG ## s
  737. #ifdef SIGABRT
  738. TRANSLATE_SIGNAL(ABRT);
  739. #endif
  740. #ifdef SIGALRM
  741. TRANSLATE_SIGNAL(ALRM);
  742. #endif
  743. #ifdef SIGFPE
  744. TRANSLATE_SIGNAL(FPE);
  745. #endif
  746. #ifdef SIGHUP
  747. TRANSLATE_SIGNAL(HUP);
  748. #endif
  749. #ifdef SIGILL
  750. TRANSLATE_SIGNAL(ILL);
  751. #endif
  752. #ifdef SIGINT
  753. TRANSLATE_SIGNAL(INT);
  754. #endif
  755. #ifdef SIGKILL
  756. TRANSLATE_SIGNAL(KILL);
  757. #endif
  758. #ifdef SIGPIPE
  759. TRANSLATE_SIGNAL(PIPE);
  760. #endif
  761. #ifdef SIGQUIT
  762. TRANSLATE_SIGNAL(QUIT);
  763. #endif
  764. #ifdef SIGSEGV
  765. TRANSLATE_SIGNAL(SEGV);
  766. #endif
  767. #ifdef SIGTERM
  768. TRANSLATE_SIGNAL(TERM);
  769. #endif
  770. #ifdef SIGUSR1
  771. TRANSLATE_SIGNAL(USR1);
  772. #endif
  773. #ifdef SIGUSR2
  774. TRANSLATE_SIGNAL(USR2);
  775. #endif
  776. #undef TRANSLATE_SIGNAL
  777. else
  778. exitcode = 128;
  779. } else {
  780. /* nonstandard integer format */
  781. unsigned signum = get_uint32(pktin);
  782. fmt_sig = dupprintf(" %u", signum);
  783. exitcode = 128 + signum;
  784. }
  785. core = get_bool(pktin);
  786. errmsg = get_string(pktin); /* error message */
  787. get_string(pktin); /* language tag */
  788. if (!get_err(pktin) && get_avail(pktin) == 0)
  789. break; /* successful parse */
  790. sfree(fmt_sig);
  791. }
  792. if (format == 2) {
  793. fmt_sig = NULL;
  794. exitcode = 128;
  795. }
  796. if (errmsg.len) {
  797. fmt_msg = dupprintf(" (\"%.*s\")",
  798. PTRLEN_PRINTF(errmsg));
  799. }
  800. ssh_got_exitcode(s->ppl.ssh, exitcode);
  801. ppl_logevent(("Server exited on signal%s%s%s",
  802. fmt_sig ? fmt_sig : "",
  803. core ? " (core dumped)" : "",
  804. fmt_msg ? fmt_msg : ""));
  805. sfree(fmt_sig);
  806. sfree(fmt_msg);
  807. reply_type = SSH2_MSG_CHANNEL_SUCCESS;
  808. }
  809. }
  810. if (want_reply) {
  811. pktout = ssh_bpp_new_pktout(s->ppl.bpp, reply_type);
  812. put_uint32(pktout, c->remoteid);
  813. pq_push(s->ppl.out_pq, pktout);
  814. }
  815. break;
  816. case SSH2_MSG_CHANNEL_SUCCESS:
  817. case SSH2_MSG_CHANNEL_FAILURE:
  818. ocr = c->chanreq_head;
  819. if (!ocr) {
  820. ssh_proto_error(
  821. s->ppl.ssh,
  822. "Received %s for channel %d with no outstanding "
  823. "channel request",
  824. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  825. s->ppl.bpp->pls->actx, pktin->type));
  826. return TRUE;
  827. }
  828. ocr->handler(c, pktin, ocr->ctx);
  829. c->chanreq_head = ocr->next;
  830. sfree(ocr);
  831. /*
  832. * We may now initiate channel-closing procedures, if
  833. * that CHANNEL_REQUEST was the last thing outstanding
  834. * before we send CHANNEL_CLOSE.
  835. */
  836. ssh2_channel_check_close(c);
  837. break;
  838. case SSH2_MSG_CHANNEL_EOF:
  839. if (!(c->closes & CLOSES_RCVD_EOF)) {
  840. c->closes |= CLOSES_RCVD_EOF;
  841. chan_send_eof(c->chan);
  842. ssh2_channel_check_close(c);
  843. }
  844. break;
  845. case SSH2_MSG_CHANNEL_CLOSE:
  846. /*
  847. * When we receive CLOSE on a channel, we assume it
  848. * comes with an implied EOF if we haven't seen EOF
  849. * yet.
  850. */
  851. if (!(c->closes & CLOSES_RCVD_EOF)) {
  852. c->closes |= CLOSES_RCVD_EOF;
  853. chan_send_eof(c->chan);
  854. }
  855. if (!(s->ppl.remote_bugs & BUG_SENDS_LATE_REQUEST_REPLY)) {
  856. /*
  857. * It also means we stop expecting to see replies
  858. * to any outstanding channel requests, so clean
  859. * those up too. (ssh_chanreq_init will enforce by
  860. * assertion that we don't subsequently put
  861. * anything back on this list.)
  862. */
  863. while (c->chanreq_head) {
  864. struct outstanding_channel_request *ocr =
  865. c->chanreq_head;
  866. ocr->handler(c, NULL, ocr->ctx);
  867. c->chanreq_head = ocr->next;
  868. sfree(ocr);
  869. }
  870. }
  871. /*
  872. * And we also send an outgoing EOF, if we haven't
  873. * already, on the assumption that CLOSE is a pretty
  874. * forceful announcement that the remote side is doing
  875. * away with the entire channel. (If it had wanted to
  876. * send us EOF and continue receiving data from us, it
  877. * would have just sent CHANNEL_EOF.)
  878. */
  879. if (!(c->closes & CLOSES_SENT_EOF)) {
  880. /*
  881. * Abandon any buffered data we still wanted to
  882. * send to this channel. Receiving a CHANNEL_CLOSE
  883. * is an indication that the server really wants
  884. * to get on and _destroy_ this channel, and it
  885. * isn't going to send us any further
  886. * WINDOW_ADJUSTs to permit us to send pending
  887. * stuff.
  888. */
  889. bufchain_clear(&c->outbuffer);
  890. /*
  891. * Send outgoing EOF.
  892. */
  893. sshfwd_write_eof(&c->sc);
  894. /*
  895. * Make sure we don't read any more from whatever
  896. * our local data source is for this channel.
  897. * (This will pick up on the changes made by
  898. * sshfwd_write_eof.)
  899. */
  900. ssh2_channel_check_throttle(c);
  901. }
  902. /*
  903. * Now process the actual close.
  904. */
  905. if (!(c->closes & CLOSES_RCVD_CLOSE)) {
  906. c->closes |= CLOSES_RCVD_CLOSE;
  907. ssh2_channel_check_close(c);
  908. }
  909. break;
  910. }
  911. pq_pop(s->ppl.in_pq);
  912. break;
  913. default:
  914. return FALSE;
  915. }
  916. }
  917. }
  918. static void ssh2_handle_winadj_response(struct ssh2_channel *c,
  919. PktIn *pktin, void *ctx)
  920. {
  921. unsigned *sizep = ctx;
  922. /*
  923. * Winadj responses should always be failures. However, at least
  924. * one server ("boks_sshd") is known to return SUCCESS for channel
  925. * requests it's never heard of, such as "winadj@putty". Raised
  926. * with foxt.com as bug 090916-090424, but for the sake of a quiet
  927. * life, we don't worry about what kind of response we got.
  928. */
  929. c->remlocwin += *sizep;
  930. sfree(sizep);
  931. /*
  932. * winadj messages are only sent when the window is fully open, so
  933. * if we get an ack of one, we know any pending unthrottle is
  934. * complete.
  935. */
  936. if (c->throttle_state == UNTHROTTLING)
  937. c->throttle_state = UNTHROTTLED;
  938. }
  939. static void ssh2_set_window(struct ssh2_channel *c, int newwin)
  940. {
  941. struct ssh2_connection_state *s = c->connlayer;
  942. /*
  943. * Never send WINDOW_ADJUST for a channel that the remote side has
  944. * already sent EOF on; there's no point, since it won't be
  945. * sending any more data anyway. Ditto if _we've_ already sent
  946. * CLOSE.
  947. */
  948. if (c->closes & (CLOSES_RCVD_EOF | CLOSES_SENT_CLOSE))
  949. return;
  950. /*
  951. * If the client-side Channel is in an initial setup phase with a
  952. * fixed window size, e.g. for an X11 channel when we're still
  953. * waiting to see its initial auth and may yet hand it off to a
  954. * downstream, don't send any WINDOW_ADJUST either.
  955. */
  956. if (c->chan->initial_fixed_window_size)
  957. return;
  958. /*
  959. * If the remote end has a habit of ignoring maxpkt, limit the
  960. * window so that it has no choice (assuming it doesn't ignore the
  961. * window as well).
  962. */
  963. if ((s->ppl.remote_bugs & BUG_SSH2_MAXPKT) && newwin > OUR_V2_MAXPKT)
  964. newwin = OUR_V2_MAXPKT;
  965. /*
  966. * Only send a WINDOW_ADJUST if there's significantly more window
  967. * available than the other end thinks there is. This saves us
  968. * sending a WINDOW_ADJUST for every character in a shell session.
  969. *
  970. * "Significant" is arbitrarily defined as half the window size.
  971. */
  972. if (newwin / 2 >= c->locwindow) {
  973. PktOut *pktout;
  974. unsigned *up;
  975. /*
  976. * In order to keep track of how much window the client
  977. * actually has available, we'd like it to acknowledge each
  978. * WINDOW_ADJUST. We can't do that directly, so we accompany
  979. * it with a CHANNEL_REQUEST that has to be acknowledged.
  980. *
  981. * This is only necessary if we're opening the window wide.
  982. * If we're not, then throughput is being constrained by
  983. * something other than the maximum window size anyway.
  984. */
  985. if (newwin == c->locmaxwin &&
  986. !(s->ppl.remote_bugs & BUG_CHOKES_ON_WINADJ)) {
  987. up = snew(unsigned);
  988. *up = newwin - c->locwindow;
  989. pktout = ssh2_chanreq_init(c, "[email protected]",
  990. ssh2_handle_winadj_response, up);
  991. pq_push(s->ppl.out_pq, pktout);
  992. if (c->throttle_state != UNTHROTTLED)
  993. c->throttle_state = UNTHROTTLING;
  994. } else {
  995. /* Pretend the WINDOW_ADJUST was acked immediately. */
  996. c->remlocwin = newwin;
  997. c->throttle_state = THROTTLED;
  998. }
  999. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_WINDOW_ADJUST);
  1000. put_uint32(pktout, c->remoteid);
  1001. put_uint32(pktout, newwin - c->locwindow);
  1002. pq_push(s->ppl.out_pq, pktout);
  1003. c->locwindow = newwin;
  1004. }
  1005. }
  1006. static PktIn *ssh2_connection_pop(struct ssh2_connection_state *s)
  1007. {
  1008. ssh2_connection_filter_queue(s);
  1009. return pq_pop(s->ppl.in_pq);
  1010. }
  1011. static void ssh2_connection_process_queue(PacketProtocolLayer *ppl)
  1012. {
  1013. struct ssh2_connection_state *s =
  1014. container_of(ppl, struct ssh2_connection_state, ppl);
  1015. PktIn *pktin;
  1016. PktOut *pktout;
  1017. if (ssh2_connection_filter_queue(s)) /* no matter why we were called */
  1018. return;
  1019. crBegin(s->crState);
  1020. /*
  1021. * Create the main session channel, if any.
  1022. */
  1023. if (conf_get_int(s->conf, CONF_ssh_no_shell)) {
  1024. s->mctype = MAINCHAN_NONE;
  1025. } else if (*conf_get_str(s->conf, CONF_ssh_nc_host)) {
  1026. s->mctype = MAINCHAN_DIRECT_TCPIP;
  1027. } else {
  1028. s->mctype = MAINCHAN_SESSION;
  1029. }
  1030. if (s->mctype != MAINCHAN_NONE) {
  1031. mainchan *mc = mainchan_new(s);
  1032. switch (s->mctype) {
  1033. case MAINCHAN_NONE:
  1034. assert(0 && "Unreachable");
  1035. break;
  1036. case MAINCHAN_SESSION:
  1037. s->mainchan = snew(struct ssh2_channel);
  1038. mc->sc = &s->mainchan->sc;
  1039. s->mainchan->connlayer = s;
  1040. ssh2_channel_init(s->mainchan);
  1041. s->mainchan->chan = &mc->chan;
  1042. s->mainchan->halfopen = TRUE;
  1043. pktout = ssh2_chanopen_init(s->mainchan, "session");
  1044. ppl_logevent(("Opening session as main channel"));
  1045. pq_push(s->ppl.out_pq, pktout);
  1046. break;
  1047. case MAINCHAN_DIRECT_TCPIP:
  1048. mc->sc = ssh_lportfwd_open(
  1049. &s->cl, conf_get_str(s->conf, CONF_ssh_nc_host),
  1050. conf_get_int(s->conf, CONF_ssh_nc_port),
  1051. "main channel", &mc->chan);
  1052. s->mainchan = container_of(mc->sc, struct ssh2_channel, sc);
  1053. break;
  1054. }
  1055. /*
  1056. * Wait until that channel has been successfully opened (or
  1057. * not).
  1058. */
  1059. crMaybeWaitUntilV(!s->mainchan || !s->mainchan->halfopen);
  1060. if (!s->mainchan) {
  1061. ssh_sw_abort(s->ppl.ssh, "Server refused to open main channel: %s",
  1062. s->mainchan_open_error);
  1063. return;
  1064. }
  1065. }
  1066. /*
  1067. * Now the connection protocol is properly up and running, with
  1068. * all those dispatch table entries, so it's safe to let
  1069. * downstreams start trying to open extra channels through us.
  1070. */
  1071. if (s->connshare)
  1072. share_activate(s->connshare, s->peer_verstring);
  1073. if (s->mainchan && s->ssh_is_simple) {
  1074. /*
  1075. * This message indicates to the server that we promise
  1076. * not to try to run any other channel in parallel with
  1077. * this one, so it's safe for it to advertise a very large
  1078. * window and leave the flow control to TCP.
  1079. */
  1080. pktout = ssh2_chanreq_init(
  1081. s->mainchan, "[email protected]", NULL, NULL);
  1082. pq_push(s->ppl.out_pq, pktout);
  1083. }
  1084. /*
  1085. * Enable port forwardings.
  1086. */
  1087. portfwdmgr_config(s->portfwdmgr, s->conf);
  1088. s->portfwdmgr_configured = TRUE;
  1089. if (s->mainchan && s->mctype == MAINCHAN_SESSION) {
  1090. /*
  1091. * Send the CHANNEL_REQUESTS for the main session channel.
  1092. * Each one is handled by its own little asynchronous
  1093. * co-routine.
  1094. */
  1095. /* Potentially enable X11 forwarding. */
  1096. if (conf_get_int(s->conf, CONF_x11_forward)) {
  1097. char *x11_setup_err;
  1098. s->x11disp = x11_setup_display(
  1099. conf_get_str(s->conf, CONF_x11_display),
  1100. s->conf, &x11_setup_err);
  1101. if (!s->x11disp) {
  1102. ppl_logevent(("X11 forwarding not enabled: unable to"
  1103. " initialise X display: %s", x11_setup_err));
  1104. sfree(x11_setup_err);
  1105. } else {
  1106. s->x11auth = x11_invent_fake_auth(
  1107. s->x11authtree, conf_get_int(s->conf, CONF_x11_auth));
  1108. s->x11auth->disp = s->x11disp;
  1109. ssh2_setup_x11(s->mainchan, NULL, NULL);
  1110. }
  1111. }
  1112. /* Potentially enable agent forwarding. */
  1113. if (ssh_agent_forwarding_permitted(&s->cl))
  1114. ssh2_setup_agent(s->mainchan, NULL, NULL);
  1115. /* Now allocate a pty for the session. */
  1116. if (!conf_get_int(s->conf, CONF_nopty))
  1117. ssh2_setup_pty(s->mainchan, NULL, NULL);
  1118. /* Send environment variables. */
  1119. ssh2_setup_env(s->mainchan, NULL, NULL);
  1120. /*
  1121. * Start a shell or a remote command. We may have to attempt
  1122. * this twice if the config data has provided a second choice
  1123. * of command.
  1124. */
  1125. for (s->session_attempt = 0; s->session_attempt < 2;
  1126. s->session_attempt++) {
  1127. int subsys;
  1128. char *cmd;
  1129. if (s->session_attempt == 0) {
  1130. subsys = conf_get_int(s->conf, CONF_ssh_subsys);
  1131. cmd = conf_get_str(s->conf, CONF_remote_cmd);
  1132. } else {
  1133. subsys = conf_get_int(s->conf, CONF_ssh_subsys2);
  1134. cmd = conf_get_str(s->conf, CONF_remote_cmd2);
  1135. if (!*cmd && !conf_get_int(s->conf, CONF_force_remote_cmd2)) // WINSCP
  1136. break;
  1137. ppl_logevent(("Primary command failed; attempting fallback"));
  1138. }
  1139. if (subsys) {
  1140. pktout = ssh2_chanreq_init(s->mainchan, "subsystem",
  1141. ssh2_response_session, s);
  1142. put_stringz(pktout, cmd);
  1143. } else if (*cmd) {
  1144. pktout = ssh2_chanreq_init(s->mainchan, "exec",
  1145. ssh2_response_session, s);
  1146. put_stringz(pktout, cmd);
  1147. } else {
  1148. pktout = ssh2_chanreq_init(s->mainchan, "shell",
  1149. ssh2_response_session, s);
  1150. }
  1151. pq_push(s->ppl.out_pq, pktout);
  1152. s->session_status = 0;
  1153. /* Wait for success or failure message to be passed to
  1154. * ssh2_response_session, which will set session_status to
  1155. * +1 for success or -1 for failure */
  1156. crMaybeWaitUntilV(s->session_status != 0);
  1157. if (s->session_status > 0) {
  1158. if (s->session_attempt == 1)
  1159. ssh_got_fallback_cmd(s->ppl.ssh);
  1160. ppl_logevent(("Started a shell/command"));
  1161. break;
  1162. }
  1163. }
  1164. if (s->session_status < 0) {
  1165. /*
  1166. * We failed to start either the primary or the fallback
  1167. * command.
  1168. */
  1169. ssh_sw_abort(s->ppl.ssh,
  1170. "Server refused to start a shell/command");
  1171. return;
  1172. }
  1173. } else {
  1174. s->echoedit = TRUE;
  1175. }
  1176. s->mainchan_ready = TRUE;
  1177. if (s->mainchan) {
  1178. s->want_user_input = TRUE;
  1179. ssh_ppl_got_user_input(&s->ppl); /* in case any is already queued */
  1180. }
  1181. /* If an EOF or a window-size change arrived before we were ready
  1182. * to handle either one, handle them now. */
  1183. if (s->mainchan_eof_pending)
  1184. ssh_ppl_special_cmd(&s->ppl, SS_EOF, 0);
  1185. if (s->term_width_orig != s->term_width ||
  1186. s->term_height_orig != s->term_height)
  1187. ssh_terminal_size(&s->cl, s->term_width, s->term_height);
  1188. ssh_ldisc_update(s->ppl.ssh);
  1189. /*
  1190. * Transfer data!
  1191. */
  1192. while (1) {
  1193. if ((pktin = ssh2_connection_pop(s)) != NULL) {
  1194. /*
  1195. * _All_ the connection-layer packets we expect to
  1196. * receive are now handled by the dispatch table.
  1197. * Anything that reaches here must be bogus.
  1198. */
  1199. ssh_proto_error(s->ppl.ssh, "Received unexpected connection-layer "
  1200. "packet, type %d (%s)", pktin->type,
  1201. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  1202. s->ppl.bpp->pls->actx,
  1203. pktin->type));
  1204. return;
  1205. }
  1206. crReturnV;
  1207. }
  1208. crFinishV;
  1209. }
  1210. static void ssh2_channel_check_close(struct ssh2_channel *c)
  1211. {
  1212. struct ssh2_connection_state *s = c->connlayer;
  1213. PktOut *pktout;
  1214. if (c->halfopen) {
  1215. /*
  1216. * If we've sent out our own CHANNEL_OPEN but not yet seen
  1217. * either OPEN_CONFIRMATION or OPEN_FAILURE in response, then
  1218. * it's too early to be sending close messages of any kind.
  1219. */
  1220. return;
  1221. }
  1222. if ((!((CLOSES_SENT_EOF | CLOSES_RCVD_EOF) & ~c->closes) ||
  1223. chan_want_close(c->chan, (c->closes & CLOSES_SENT_EOF),
  1224. (c->closes & CLOSES_RCVD_EOF))) &&
  1225. !c->chanreq_head &&
  1226. !(c->closes & CLOSES_SENT_CLOSE)) {
  1227. /*
  1228. * We have both sent and received EOF (or the channel is a
  1229. * zombie), and we have no outstanding channel requests, which
  1230. * means the channel is in final wind-up. But we haven't sent
  1231. * CLOSE, so let's do so now.
  1232. */
  1233. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_CLOSE);
  1234. put_uint32(pktout, c->remoteid);
  1235. pq_push(s->ppl.out_pq, pktout);
  1236. c->closes |= CLOSES_SENT_EOF | CLOSES_SENT_CLOSE;
  1237. }
  1238. if (!((CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE) & ~c->closes)) {
  1239. assert(c->chanreq_head == NULL);
  1240. /*
  1241. * We have both sent and received CLOSE, which means we're
  1242. * completely done with the channel.
  1243. */
  1244. ssh2_channel_destroy(c);
  1245. }
  1246. }
  1247. static void ssh2_channel_try_eof(struct ssh2_channel *c)
  1248. {
  1249. struct ssh2_connection_state *s = c->connlayer;
  1250. PktOut *pktout;
  1251. assert(c->pending_eof); /* precondition for calling us */
  1252. if (c->halfopen)
  1253. return; /* can't close: not even opened yet */
  1254. if (bufchain_size(&c->outbuffer) > 0)
  1255. return; /* can't send EOF: pending outgoing data */
  1256. c->pending_eof = FALSE; /* we're about to send it */
  1257. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_EOF);
  1258. put_uint32(pktout, c->remoteid);
  1259. pq_push(s->ppl.out_pq, pktout);
  1260. c->closes |= CLOSES_SENT_EOF;
  1261. ssh2_channel_check_close(c);
  1262. }
  1263. /*
  1264. * Attempt to send data on an SSH-2 channel.
  1265. */
  1266. static int ssh2_try_send(struct ssh2_channel *c)
  1267. {
  1268. struct ssh2_connection_state *s = c->connlayer;
  1269. PktOut *pktout;
  1270. int bufsize;
  1271. while (c->remwindow > 0 && bufchain_size(&c->outbuffer) > 0) {
  1272. int len;
  1273. void *data;
  1274. bufchain_prefix(&c->outbuffer, &data, &len);
  1275. if ((unsigned)len > c->remwindow)
  1276. len = c->remwindow;
  1277. if ((unsigned)len > c->remmaxpkt)
  1278. len = c->remmaxpkt;
  1279. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_DATA);
  1280. put_uint32(pktout, c->remoteid);
  1281. put_string(pktout, data, len);
  1282. pq_push(s->ppl.out_pq, pktout);
  1283. bufchain_consume(&c->outbuffer, len);
  1284. c->remwindow -= len;
  1285. }
  1286. /*
  1287. * After having sent as much data as we can, return the amount
  1288. * still buffered.
  1289. */
  1290. bufsize = bufchain_size(&c->outbuffer);
  1291. /*
  1292. * And if there's no data pending but we need to send an EOF, send
  1293. * it.
  1294. */
  1295. if (!bufsize && c->pending_eof)
  1296. ssh2_channel_try_eof(c);
  1297. return bufsize;
  1298. }
  1299. static void ssh2_try_send_and_unthrottle(struct ssh2_channel *c)
  1300. {
  1301. int bufsize;
  1302. if (c->closes & CLOSES_SENT_EOF)
  1303. return; /* don't send on channels we've EOFed */
  1304. bufsize = ssh2_try_send(c);
  1305. if (bufsize == 0) {
  1306. c->throttled_by_backlog = FALSE;
  1307. ssh2_channel_check_throttle(c);
  1308. }
  1309. }
  1310. static void ssh2_channel_check_throttle(struct ssh2_channel *c)
  1311. {
  1312. /*
  1313. * We don't want this channel to read further input if this
  1314. * particular channel has a backed-up SSH window, or if the
  1315. * outgoing side of the whole SSH connection is currently
  1316. * throttled, or if this channel already has an outgoing EOF
  1317. * either sent or pending.
  1318. */
  1319. chan_set_input_wanted(c->chan,
  1320. !c->throttled_by_backlog &&
  1321. !c->connlayer->all_channels_throttled &&
  1322. !c->pending_eof &&
  1323. !(c->closes & CLOSES_SENT_EOF));
  1324. }
  1325. /*
  1326. * Close any local socket and free any local resources associated with
  1327. * a channel. This converts the channel into a zombie.
  1328. */
  1329. static void ssh2_channel_close_local(struct ssh2_channel *c,
  1330. const char *reason)
  1331. {
  1332. struct ssh2_connection_state *s = c->connlayer;
  1333. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  1334. char *msg = NULL;
  1335. if (c->sharectx)
  1336. return;
  1337. msg = chan_log_close_msg(c->chan);
  1338. if (msg)
  1339. ppl_logevent(("%s%s%s", msg, reason ? " " : "", reason ? reason : ""));
  1340. sfree(msg);
  1341. chan_free(c->chan);
  1342. c->chan = zombiechan_new();
  1343. }
  1344. static void ssh2_check_termination_callback(void *vctx)
  1345. {
  1346. struct ssh2_connection_state *s = (struct ssh2_connection_state *)vctx;
  1347. ssh2_check_termination(s);
  1348. }
  1349. static void ssh2_channel_destroy(struct ssh2_channel *c)
  1350. {
  1351. struct ssh2_connection_state *s = c->connlayer;
  1352. assert(c->chanreq_head == NULL);
  1353. ssh2_channel_close_local(c, NULL);
  1354. del234(s->channels, c);
  1355. ssh2_channel_free(c);
  1356. /*
  1357. * If that was the last channel left open, we might need to
  1358. * terminate. But we'll be a bit cautious, by doing that in a
  1359. * toplevel callback, just in case anything on the current call
  1360. * stack objects to this entire PPL being freed.
  1361. */
  1362. queue_toplevel_callback(ssh2_check_termination_callback, s);
  1363. }
  1364. static void ssh2_check_termination(struct ssh2_connection_state *s)
  1365. {
  1366. /*
  1367. * Decide whether we should terminate the SSH connection now.
  1368. * Called after a channel or a downstream goes away. The general
  1369. * policy is that we terminate when none of either is left.
  1370. */
  1371. if (s->mctype == MAINCHAN_NONE) {
  1372. /*
  1373. * Exception: in ssh_no_shell mode we persist even in the
  1374. * absence of any channels (because our purpose is probably to
  1375. * be a background port forwarder).
  1376. */
  1377. return;
  1378. }
  1379. if (count234(s->channels) == 0 &&
  1380. !(s->connshare && share_ndownstreams(s->connshare) > 0)) {
  1381. /*
  1382. * We used to send SSH_MSG_DISCONNECT here, because I'd
  1383. * believed that _every_ conforming SSH-2 connection had to
  1384. * end with a disconnect being sent by at least one side;
  1385. * apparently I was wrong and it's perfectly OK to
  1386. * unceremoniously slam the connection shut when you're done,
  1387. * and indeed OpenSSH feels this is more polite than sending a
  1388. * DISCONNECT. So now we don't.
  1389. */
  1390. ssh_user_close(s->ppl.ssh, "All channels closed");
  1391. return;
  1392. }
  1393. }
  1394. static void ssh2_setup_x11(struct ssh2_channel *c, PktIn *pktin, void *ctx)
  1395. {
  1396. struct ssh2_setup_x11_state {
  1397. int crLine;
  1398. };
  1399. struct ssh2_connection_state *cs = c->connlayer;
  1400. PacketProtocolLayer *ppl = &cs->ppl; /* for ppl_logevent */
  1401. PktOut *pktout;
  1402. crStateP(ssh2_setup_x11_state, ctx);
  1403. crBeginState;
  1404. ppl_logevent(("Requesting X11 forwarding"));
  1405. pktout = ssh2_chanreq_init(cs->mainchan, "x11-req", ssh2_setup_x11, s);
  1406. put_bool(pktout, 0); /* many connections */
  1407. put_stringz(pktout, cs->x11auth->protoname);
  1408. put_stringz(pktout, cs->x11auth->datastring);
  1409. put_uint32(pktout, cs->x11disp->screennum);
  1410. pq_push(cs->ppl.out_pq, pktout);
  1411. /* Wait to be called back with either a response packet, or NULL
  1412. * meaning clean up and free our data */
  1413. crReturnV;
  1414. if (pktin) {
  1415. if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS) {
  1416. ppl_logevent(("X11 forwarding enabled"));
  1417. cs->X11_fwd_enabled = TRUE;
  1418. } else
  1419. ppl_logevent(("X11 forwarding refused"));
  1420. }
  1421. crFinishFreeV;
  1422. }
  1423. static void ssh2_setup_agent(struct ssh2_channel *c, PktIn *pktin, void *ctx)
  1424. {
  1425. struct ssh2_setup_agent_state {
  1426. int crLine;
  1427. };
  1428. struct ssh2_connection_state *cs = c->connlayer;
  1429. PacketProtocolLayer *ppl = &cs->ppl; /* for ppl_logevent */
  1430. PktOut *pktout;
  1431. crStateP(ssh2_setup_agent_state, ctx);
  1432. crBeginState;
  1433. ppl_logevent(("Requesting OpenSSH-style agent forwarding"));
  1434. pktout = ssh2_chanreq_init(cs->mainchan, "[email protected]",
  1435. ssh2_setup_agent, s);
  1436. pq_push(cs->ppl.out_pq, pktout);
  1437. /* Wait to be called back with either a response packet, or NULL
  1438. * meaning clean up and free our data */
  1439. crReturnV;
  1440. if (pktin) {
  1441. if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS) {
  1442. ppl_logevent(("Agent forwarding enabled"));
  1443. cs->agent_fwd_enabled = TRUE;
  1444. } else
  1445. ppl_logevent(("Agent forwarding refused"));
  1446. }
  1447. crFinishFreeV;
  1448. }
  1449. static void ssh2_setup_pty(struct ssh2_channel *c, PktIn *pktin, void *ctx)
  1450. {
  1451. struct ssh2_setup_pty_state {
  1452. int crLine;
  1453. int ospeed, ispeed;
  1454. };
  1455. struct ssh2_connection_state *cs = c->connlayer;
  1456. PacketProtocolLayer *ppl = &cs->ppl; /* for ppl_logevent, ppl_printf */
  1457. PktOut *pktout;
  1458. crStateP(ssh2_setup_pty_state, ctx);
  1459. crBeginState;
  1460. /* Unpick the terminal-speed string. */
  1461. s->ospeed = 38400; s->ispeed = 38400; /* last-resort defaults */
  1462. sscanf(conf_get_str(cs->conf, CONF_termspeed), "%d,%d",
  1463. &s->ospeed, &s->ispeed);
  1464. /* Build the pty request. */
  1465. pktout = ssh2_chanreq_init(cs->mainchan, "pty-req", ssh2_setup_pty, s);
  1466. put_stringz(pktout, conf_get_str(cs->conf, CONF_termtype));
  1467. put_uint32(pktout, cs->term_width);
  1468. put_uint32(pktout, cs->term_height);
  1469. cs->term_width_orig = cs->term_width;
  1470. cs->term_height_orig = cs->term_height;
  1471. put_uint32(pktout, 0); /* pixel width */
  1472. put_uint32(pktout, 0); /* pixel height */
  1473. {
  1474. strbuf *modebuf = strbuf_new();
  1475. write_ttymodes_to_packet_from_conf(
  1476. BinarySink_UPCAST(modebuf), cs->ppl.seat, cs->conf,
  1477. 2, s->ospeed, s->ispeed);
  1478. put_stringsb(pktout, modebuf);
  1479. }
  1480. pq_push(cs->ppl.out_pq, pktout);
  1481. /* Wait to be called back with either a response packet, or NULL
  1482. * meaning clean up and free our data */
  1483. crReturnV;
  1484. if (pktin) {
  1485. if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS) {
  1486. ppl_logevent(("Allocated pty (ospeed %dbps, ispeed %dbps)",
  1487. s->ospeed, s->ispeed));
  1488. cs->got_pty = TRUE;
  1489. } else {
  1490. ppl_printf(("Server refused to allocate pty\r\n"));
  1491. cs->echoedit = TRUE;
  1492. }
  1493. }
  1494. crFinishFreeV;
  1495. }
  1496. static void ssh2_setup_env(struct ssh2_channel *c, PktIn *pktin, void *ctx)
  1497. {
  1498. struct ssh2_setup_env_state {
  1499. int crLine;
  1500. int num_env, env_left, env_ok;
  1501. };
  1502. struct ssh2_connection_state *cs = c->connlayer;
  1503. PacketProtocolLayer *ppl = &cs->ppl; /* for ppl_logevent, ppl_printf */
  1504. PktOut *pktout;
  1505. crStateP(ssh2_setup_env_state, ctx);
  1506. crBeginState;
  1507. /*
  1508. * Send environment variables.
  1509. *
  1510. * Simplest thing here is to send all the requests at once, and
  1511. * then wait for a whole bunch of successes or failures.
  1512. */
  1513. s->num_env = 0;
  1514. {
  1515. char *key, *val;
  1516. for (val = conf_get_str_strs(cs->conf, CONF_environmt, NULL, &key);
  1517. val != NULL;
  1518. val = conf_get_str_strs(cs->conf, CONF_environmt, key, &key)) {
  1519. pktout = ssh2_chanreq_init(cs->mainchan, "env", ssh2_setup_env, s);
  1520. put_stringz(pktout, key);
  1521. put_stringz(pktout, val);
  1522. pq_push(cs->ppl.out_pq, pktout);
  1523. s->num_env++;
  1524. }
  1525. if (s->num_env)
  1526. ppl_logevent(("Sent %d environment variables", s->num_env));
  1527. }
  1528. if (s->num_env) {
  1529. s->env_ok = 0;
  1530. s->env_left = s->num_env;
  1531. while (s->env_left > 0) {
  1532. /* Wait to be called back with either a response packet,
  1533. * or NULL meaning clean up and free our data */
  1534. crReturnV;
  1535. if (!pktin) goto out;
  1536. if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS)
  1537. s->env_ok++;
  1538. s->env_left--;
  1539. }
  1540. if (s->env_ok == s->num_env) {
  1541. ppl_logevent(("All environment variables successfully set"));
  1542. } else if (s->env_ok == 0) {
  1543. ppl_logevent(("All environment variables refused"));
  1544. ppl_printf(("Server refused to set environment variables\r\n"));
  1545. } else {
  1546. ppl_logevent(("%d environment variables refused",
  1547. s->num_env - s->env_ok));
  1548. ppl_printf(("Server refused to set all environment "
  1549. "variables\r\n"));
  1550. }
  1551. }
  1552. out:;
  1553. crFinishFreeV;
  1554. }
  1555. static void ssh2_response_session(struct ssh2_channel *c, PktIn *pktin,
  1556. void *ctx)
  1557. {
  1558. struct ssh2_connection_state *s = c->connlayer;
  1559. s->session_status = (pktin->type == SSH2_MSG_CHANNEL_SUCCESS ? +1 : -1);
  1560. }
  1561. /*
  1562. * Set up most of a new ssh2_channel. Nulls out sharectx, but leaves
  1563. * chan untouched (since it will sometimes have been filled in before
  1564. * calling this).
  1565. */
  1566. static void ssh2_channel_init(struct ssh2_channel *c)
  1567. {
  1568. struct ssh2_connection_state *s = c->connlayer;
  1569. c->closes = 0;
  1570. c->pending_eof = FALSE;
  1571. c->throttling_conn = FALSE;
  1572. c->sharectx = NULL;
  1573. c->locwindow = c->locmaxwin = c->remlocwin =
  1574. s->ssh_is_simple ? OUR_V2_BIGWIN : OUR_V2_WINSIZE;
  1575. c->chanreq_head = NULL;
  1576. c->throttle_state = UNTHROTTLED;
  1577. bufchain_init(&c->outbuffer);
  1578. c->sc.vt = &ssh2channel_vtable;
  1579. c->localid = alloc_channel_id(s->channels, struct ssh2_channel);
  1580. add234(s->channels, c);
  1581. }
  1582. /*
  1583. * Construct the common parts of a CHANNEL_OPEN.
  1584. */
  1585. static PktOut *ssh2_chanopen_init(struct ssh2_channel *c, const char *type)
  1586. {
  1587. struct ssh2_connection_state *s = c->connlayer;
  1588. PktOut *pktout;
  1589. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_OPEN);
  1590. put_stringz(pktout, type);
  1591. put_uint32(pktout, c->localid);
  1592. put_uint32(pktout, c->locwindow); /* our window size */
  1593. put_uint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */
  1594. return pktout;
  1595. }
  1596. /*
  1597. * Construct the common parts of a CHANNEL_REQUEST. If handler is not
  1598. * NULL then a reply will be requested and the handler will be called
  1599. * when it arrives. The returned packet is ready to have any
  1600. * request-specific data added and be sent. Note that if a handler is
  1601. * provided, it's essential that the request actually be sent.
  1602. *
  1603. * The handler will usually be passed the response packet in pktin. If
  1604. * pktin is NULL, this means that no reply will ever be forthcoming
  1605. * (e.g. because the entire connection is being destroyed, or because
  1606. * the server initiated channel closure before we saw the response)
  1607. * and the handler should free any storage it's holding.
  1608. */
  1609. static PktOut *ssh2_chanreq_init(struct ssh2_channel *c, const char *type,
  1610. cr_handler_fn_t handler, void *ctx)
  1611. {
  1612. struct ssh2_connection_state *s = c->connlayer;
  1613. PktOut *pktout;
  1614. assert(!(c->closes & (CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE)));
  1615. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_REQUEST);
  1616. put_uint32(pktout, c->remoteid);
  1617. put_stringz(pktout, type);
  1618. put_bool(pktout, handler != NULL);
  1619. if (handler != NULL) {
  1620. struct outstanding_channel_request *ocr =
  1621. snew(struct outstanding_channel_request);
  1622. ocr->handler = handler;
  1623. ocr->ctx = ctx;
  1624. ocr->next = NULL;
  1625. if (!c->chanreq_head)
  1626. c->chanreq_head = ocr;
  1627. else
  1628. c->chanreq_tail->next = ocr;
  1629. c->chanreq_tail = ocr;
  1630. }
  1631. return pktout;
  1632. }
  1633. static Conf *ssh2channel_get_conf(SshChannel *sc)
  1634. {
  1635. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1636. struct ssh2_connection_state *s = c->connlayer;
  1637. return s->conf;
  1638. }
  1639. static void ssh2channel_write_eof(SshChannel *sc)
  1640. {
  1641. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1642. if (c->closes & CLOSES_SENT_EOF)
  1643. return;
  1644. c->pending_eof = TRUE;
  1645. ssh2_channel_try_eof(c);
  1646. }
  1647. static void ssh2channel_unclean_close(SshChannel *sc, const char *err)
  1648. {
  1649. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1650. char *reason;
  1651. reason = dupprintf("due to local error: %s", err);
  1652. ssh2_channel_close_local(c, reason);
  1653. sfree(reason);
  1654. c->pending_eof = FALSE; /* this will confuse a zombie channel */
  1655. ssh2_channel_check_close(c);
  1656. }
  1657. static void ssh2channel_unthrottle(SshChannel *sc, int bufsize)
  1658. {
  1659. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1660. struct ssh2_connection_state *s = c->connlayer;
  1661. int buflimit;
  1662. buflimit = s->ssh_is_simple ? 0 : c->locmaxwin;
  1663. if (bufsize < buflimit)
  1664. ssh2_set_window(c, buflimit - bufsize);
  1665. if (c->throttling_conn && bufsize <= buflimit) {
  1666. c->throttling_conn = 0;
  1667. ssh_throttle_conn(s->ppl.ssh, -1);
  1668. }
  1669. }
  1670. static int ssh2channel_write(SshChannel *sc, const void *buf, int len)
  1671. {
  1672. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1673. assert(!(c->closes & CLOSES_SENT_EOF));
  1674. bufchain_add(&c->outbuffer, buf, len);
  1675. return ssh2_try_send(c);
  1676. }
  1677. static void ssh2channel_x11_sharing_handover(
  1678. SshChannel *sc, ssh_sharing_connstate *share_cs, share_channel *share_chan,
  1679. const char *peer_addr, int peer_port, int endian,
  1680. int protomajor, int protominor, const void *initial_data, int initial_len)
  1681. {
  1682. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1683. /*
  1684. * This function is called when we've just discovered that an X
  1685. * forwarding channel on which we'd been handling the initial auth
  1686. * ourselves turns out to be destined for a connection-sharing
  1687. * downstream. So we turn the channel into a sharing one, meaning
  1688. * that we completely stop tracking windows and buffering data and
  1689. * just pass more or less unmodified SSH messages back and forth.
  1690. */
  1691. c->sharectx = share_cs;
  1692. share_setup_x11_channel(share_cs, share_chan,
  1693. c->localid, c->remoteid, c->remwindow,
  1694. c->remmaxpkt, c->locwindow,
  1695. peer_addr, peer_port, endian,
  1696. protomajor, protominor,
  1697. initial_data, initial_len);
  1698. chan_free(c->chan);
  1699. c->chan = NULL;
  1700. }
  1701. static void ssh2channel_window_override_removed(SshChannel *sc)
  1702. {
  1703. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  1704. struct ssh2_connection_state *s = c->connlayer;
  1705. /*
  1706. * This function is called when a client-side Channel has just
  1707. * stopped requiring an initial fixed-size window.
  1708. */
  1709. assert(!c->chan->initial_fixed_window_size);
  1710. ssh2_set_window(c, s->ssh_is_simple ? OUR_V2_BIGWIN : OUR_V2_WINSIZE);
  1711. }
  1712. static SshChannel *ssh2_lportfwd_open(
  1713. ConnectionLayer *cl, const char *hostname, int port,
  1714. const char *org, Channel *chan)
  1715. {
  1716. struct ssh2_connection_state *s =
  1717. container_of(cl, struct ssh2_connection_state, cl);
  1718. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  1719. struct ssh2_channel *c = snew(struct ssh2_channel);
  1720. PktOut *pktout;
  1721. c->connlayer = s;
  1722. ssh2_channel_init(c);
  1723. c->halfopen = TRUE;
  1724. c->chan = chan;
  1725. ppl_logevent(("Opening connection to %s:%d for %s", hostname, port, org));
  1726. pktout = ssh2_chanopen_init(c, "direct-tcpip");
  1727. {
  1728. char *trimmed_host = host_strduptrim(hostname);
  1729. put_stringz(pktout, trimmed_host);
  1730. sfree(trimmed_host);
  1731. }
  1732. put_uint32(pktout, port);
  1733. /*
  1734. * We make up values for the originator data; partly it's too much
  1735. * hassle to keep track, and partly I'm not convinced the server
  1736. * should be told details like that about my local network
  1737. * configuration. The "originator IP address" is syntactically a
  1738. * numeric IP address, and some servers (e.g., Tectia) get upset
  1739. * if it doesn't match this syntax.
  1740. */
  1741. put_stringz(pktout, "0.0.0.0");
  1742. put_uint32(pktout, 0);
  1743. pq_push(s->ppl.out_pq, pktout);
  1744. return &c->sc;
  1745. }
  1746. static void ssh2_rportfwd_globreq_response(struct ssh2_connection_state *s,
  1747. PktIn *pktin, void *ctx)
  1748. {
  1749. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  1750. struct ssh_rportfwd *rpf = (struct ssh_rportfwd *)ctx;
  1751. if (pktin->type == SSH2_MSG_REQUEST_SUCCESS) {
  1752. ppl_logevent(("Remote port forwarding from %s enabled",
  1753. rpf->log_description));
  1754. } else {
  1755. ppl_logevent(("Remote port forwarding from %s refused",
  1756. rpf->log_description));
  1757. { // WINSCP
  1758. struct ssh_rportfwd *realpf = del234(s->rportfwds, rpf);
  1759. assert(realpf == rpf);
  1760. } // WINSCP
  1761. portfwdmgr_close(s->portfwdmgr, rpf->pfr);
  1762. free_rportfwd(rpf);
  1763. }
  1764. }
  1765. static struct ssh_rportfwd *ssh2_rportfwd_alloc(
  1766. ConnectionLayer *cl,
  1767. const char *shost, int sport, const char *dhost, int dport,
  1768. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  1769. ssh_sharing_connstate *share_ctx)
  1770. {
  1771. struct ssh2_connection_state *s =
  1772. container_of(cl, struct ssh2_connection_state, cl);
  1773. struct ssh_rportfwd *rpf = snew(struct ssh_rportfwd);
  1774. rpf->shost = dupstr(shost);
  1775. rpf->sport = sport;
  1776. rpf->dhost = dupstr(dhost);
  1777. rpf->dport = dport;
  1778. rpf->addressfamily = addressfamily;
  1779. rpf->log_description = dupstr(log_description);
  1780. rpf->pfr = pfr;
  1781. rpf->share_ctx = share_ctx;
  1782. if (add234(s->rportfwds, rpf) != rpf) {
  1783. free_rportfwd(rpf);
  1784. return NULL;
  1785. }
  1786. if (!rpf->share_ctx) {
  1787. PktOut *pktout = ssh_bpp_new_pktout(
  1788. s->ppl.bpp, SSH2_MSG_GLOBAL_REQUEST);
  1789. put_stringz(pktout, "tcpip-forward");
  1790. put_bool(pktout, 1); /* want reply */
  1791. put_stringz(pktout, rpf->shost);
  1792. put_uint32(pktout, rpf->sport);
  1793. pq_push(s->ppl.out_pq, pktout);
  1794. ssh2_queue_global_request_handler(
  1795. s, ssh2_rportfwd_globreq_response, rpf);
  1796. }
  1797. return rpf;
  1798. }
  1799. static void ssh2_rportfwd_remove(ConnectionLayer *cl, struct ssh_rportfwd *rpf)
  1800. {
  1801. struct ssh2_connection_state *s =
  1802. container_of(cl, struct ssh2_connection_state, cl);
  1803. if (rpf->share_ctx) {
  1804. /*
  1805. * We don't manufacture a cancel-tcpip-forward message for
  1806. * remote port forwardings being removed on behalf of a
  1807. * downstream; we just pass through the one the downstream
  1808. * sent to us.
  1809. */
  1810. } else {
  1811. PktOut *pktout = ssh_bpp_new_pktout(
  1812. s->ppl.bpp, SSH2_MSG_GLOBAL_REQUEST);
  1813. put_stringz(pktout, "cancel-tcpip-forward");
  1814. put_bool(pktout, 0); /* _don't_ want reply */
  1815. put_stringz(pktout, rpf->shost);
  1816. put_uint32(pktout, rpf->sport);
  1817. pq_push(s->ppl.out_pq, pktout);
  1818. }
  1819. { // WINSCP
  1820. struct ssh_rportfwd *realpf = del234(s->rportfwds, rpf);
  1821. assert(realpf == rpf);
  1822. } // WINSCP
  1823. free_rportfwd(rpf);
  1824. }
  1825. static void ssh2_sharing_globreq_response(
  1826. struct ssh2_connection_state *s, PktIn *pktin, void *ctx)
  1827. {
  1828. ssh_sharing_connstate *cs = (ssh_sharing_connstate *)ctx;
  1829. share_got_pkt_from_server(cs, pktin->type,
  1830. BinarySource_UPCAST(pktin)->data,
  1831. BinarySource_UPCAST(pktin)->len);
  1832. }
  1833. static void ssh2_sharing_queue_global_request(
  1834. ConnectionLayer *cl, ssh_sharing_connstate *cs)
  1835. {
  1836. struct ssh2_connection_state *s =
  1837. container_of(cl, struct ssh2_connection_state, cl);
  1838. ssh2_queue_global_request_handler(s, ssh2_sharing_globreq_response, cs);
  1839. }
  1840. static void ssh2_sharing_no_more_downstreams(ConnectionLayer *cl)
  1841. {
  1842. struct ssh2_connection_state *s =
  1843. container_of(cl, struct ssh2_connection_state, cl);
  1844. queue_toplevel_callback(ssh2_check_termination_callback, s);
  1845. }
  1846. static struct X11FakeAuth *ssh2_add_sharing_x11_display(
  1847. ConnectionLayer *cl, int authtype, ssh_sharing_connstate *share_cs,
  1848. share_channel *share_chan)
  1849. {
  1850. struct ssh2_connection_state *s =
  1851. container_of(cl, struct ssh2_connection_state, cl);
  1852. struct X11FakeAuth *auth;
  1853. /*
  1854. * Make up a new set of fake X11 auth data, and add it to the tree
  1855. * of currently valid ones with an indication of the sharing
  1856. * context that it's relevant to.
  1857. */
  1858. auth = x11_invent_fake_auth(s->x11authtree, authtype);
  1859. auth->share_cs = share_cs;
  1860. auth->share_chan = share_chan;
  1861. return auth;
  1862. }
  1863. static void ssh2_remove_sharing_x11_display(
  1864. ConnectionLayer *cl, struct X11FakeAuth *auth)
  1865. {
  1866. struct ssh2_connection_state *s =
  1867. container_of(cl, struct ssh2_connection_state, cl);
  1868. del234(s->x11authtree, auth);
  1869. x11_free_fake_auth(auth);
  1870. }
  1871. static unsigned ssh2_alloc_sharing_channel(
  1872. ConnectionLayer *cl, ssh_sharing_connstate *connstate)
  1873. {
  1874. struct ssh2_connection_state *s =
  1875. container_of(cl, struct ssh2_connection_state, cl);
  1876. struct ssh2_channel *c = snew(struct ssh2_channel);
  1877. c->connlayer = s;
  1878. ssh2_channel_init(c);
  1879. c->chan = NULL;
  1880. c->sharectx = connstate;
  1881. return c->localid;
  1882. }
  1883. static void ssh2_delete_sharing_channel(ConnectionLayer *cl, unsigned localid)
  1884. {
  1885. struct ssh2_connection_state *s =
  1886. container_of(cl, struct ssh2_connection_state, cl);
  1887. struct ssh2_channel *c = find234(s->channels, &localid, ssh2_channelfind);
  1888. if (c)
  1889. ssh2_channel_destroy(c);
  1890. }
  1891. static void ssh2_send_packet_from_downstream(
  1892. ConnectionLayer *cl, unsigned id, int type,
  1893. const void *data, int datalen, const char *additional_log_text)
  1894. {
  1895. struct ssh2_connection_state *s =
  1896. container_of(cl, struct ssh2_connection_state, cl);
  1897. PktOut *pkt = ssh_bpp_new_pktout(s->ppl.bpp, type);
  1898. pkt->downstream_id = id;
  1899. pkt->additional_log_text = additional_log_text;
  1900. put_data(pkt, data, datalen);
  1901. pq_push(s->ppl.out_pq, pkt);
  1902. }
  1903. static int ssh2_agent_forwarding_permitted(ConnectionLayer *cl)
  1904. {
  1905. struct ssh2_connection_state *s =
  1906. container_of(cl, struct ssh2_connection_state, cl);
  1907. return conf_get_int(s->conf, CONF_agentfwd) && agent_exists();
  1908. }
  1909. static void mainchan_free(Channel *chan);
  1910. static void mainchan_open_confirmation(Channel *chan);
  1911. static void mainchan_open_failure(Channel *chan, const char *errtext);
  1912. static int mainchan_send(Channel *chan, int is_stderr, const void *, int);
  1913. static void mainchan_send_eof(Channel *chan);
  1914. static void mainchan_set_input_wanted(Channel *chan, int wanted);
  1915. static char *mainchan_log_close_msg(Channel *chan);
  1916. static const struct ChannelVtable mainchan_channelvt = {
  1917. mainchan_free,
  1918. mainchan_open_confirmation,
  1919. mainchan_open_failure,
  1920. mainchan_send,
  1921. mainchan_send_eof,
  1922. mainchan_set_input_wanted,
  1923. mainchan_log_close_msg,
  1924. chan_no_eager_close,
  1925. };
  1926. static mainchan *mainchan_new(struct ssh2_connection_state *s)
  1927. {
  1928. mainchan *mc = snew(mainchan);
  1929. mc->connlayer = s;
  1930. mc->sc = NULL;
  1931. mc->chan.vt = &mainchan_channelvt;
  1932. mc->chan.initial_fixed_window_size = 0;
  1933. return mc;
  1934. }
  1935. static void mainchan_free(Channel *chan)
  1936. {
  1937. pinitassert(chan->vt == &mainchan_channelvt);
  1938. mainchan *mc = container_of(chan, mainchan, chan);
  1939. struct ssh2_connection_state *s = mc->connlayer;
  1940. s->mainchan = NULL;
  1941. sfree(mc);
  1942. }
  1943. static void mainchan_open_confirmation(Channel *chan)
  1944. {
  1945. mainchan *mc = container_of(chan, mainchan, chan);
  1946. struct ssh2_connection_state *s = mc->connlayer;
  1947. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  1948. seat_update_specials_menu(s->ppl.seat);
  1949. ppl_logevent(("Opened main channel"));
  1950. }
  1951. static void mainchan_open_failure(Channel *chan, const char *errtext)
  1952. {
  1953. pinitassert(chan->vt == &mainchan_channelvt);
  1954. mainchan *mc = container_of(chan, mainchan, chan);
  1955. struct ssh2_connection_state *s = mc->connlayer;
  1956. /*
  1957. * Record the failure reason we're given, and let the main
  1958. * coroutine handle closing the SSH session.
  1959. */
  1960. s->mainchan_open_error = dupstr(errtext);
  1961. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1962. }
  1963. static int mainchan_send(Channel *chan, int is_stderr,
  1964. const void *data, int length)
  1965. {
  1966. pinitassert(chan->vt == &mainchan_channelvt);
  1967. mainchan *mc = container_of(chan, mainchan, chan);
  1968. struct ssh2_connection_state *s = mc->connlayer;
  1969. return seat_output(s->ppl.seat, is_stderr, data, length);
  1970. }
  1971. static void mainchan_send_eof(Channel *chan)
  1972. {
  1973. pinitassert(chan->vt == &mainchan_channelvt);
  1974. mainchan *mc = container_of(chan, mainchan, chan);
  1975. struct ssh2_connection_state *s = mc->connlayer;
  1976. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  1977. if (!s->mainchan_eof_sent && (seat_eof(s->ppl.seat) || s->got_pty)) {
  1978. /*
  1979. * Either seat_eof told us that the front end wants us to
  1980. * close the outgoing side of the connection as soon as we see
  1981. * EOF from the far end, or else we've unilaterally decided to
  1982. * do that because we've allocated a remote pty and hence EOF
  1983. * isn't a particularly meaningful concept.
  1984. */
  1985. sshfwd_write_eof(mc->sc);
  1986. ppl_logevent(("Sent EOF message"));
  1987. s->mainchan_eof_sent = TRUE;
  1988. s->want_user_input = FALSE; /* now stop reading from stdin */
  1989. }
  1990. }
  1991. static void mainchan_set_input_wanted(Channel *chan, int wanted)
  1992. {
  1993. pinitassert(chan->vt == &mainchan_channelvt);
  1994. mainchan *mc = container_of(chan, mainchan, chan);
  1995. struct ssh2_connection_state *s = mc->connlayer;
  1996. /*
  1997. * This is the main channel of the SSH session, i.e. the one tied
  1998. * to the standard input (or GUI) of the primary SSH client user
  1999. * interface. So ssh->send_ok is how we control whether we're
  2000. * reading from that input.
  2001. */
  2002. s->want_user_input = wanted;
  2003. }
  2004. static char *mainchan_log_close_msg(Channel *chan)
  2005. {
  2006. return dupstr("Main session channel closed");
  2007. }
  2008. /*
  2009. * List of signal names defined by RFC 4254. These include all the ISO
  2010. * C signals, but are a subset of the POSIX required signals.
  2011. *
  2012. * The list macro takes parameters MAIN and SUB, which is an arbitrary
  2013. * UI decision to expose the signals we think users are most likely to
  2014. * want, with extra descriptive text, and relegate the less probable
  2015. * ones to a submenu for people who know what they're doing.
  2016. */
  2017. #define SIGNAL_LIST(MAIN, SUB) \
  2018. MAIN(INT, "Interrupt") \
  2019. MAIN(TERM, "Terminate") \
  2020. MAIN(KILL, "Kill") \
  2021. MAIN(QUIT, "Quit") \
  2022. MAIN(HUP, "Hangup") \
  2023. SUB(ABRT) \
  2024. SUB(ALRM) \
  2025. SUB(FPE) \
  2026. SUB(ILL) \
  2027. SUB(PIPE) \
  2028. SUB(SEGV) \
  2029. SUB(USR1) \
  2030. SUB(USR2) \
  2031. /* end of list */
  2032. static int ssh2_connection_get_specials(
  2033. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  2034. {
  2035. struct ssh2_connection_state *s =
  2036. container_of(ppl, struct ssh2_connection_state, ppl);
  2037. int toret = FALSE;
  2038. if (s->mainchan) {
  2039. add_special(ctx, "Break", SS_BRK, 0);
  2040. #define ADD_MAIN(name, desc) \
  2041. add_special(ctx, "SIG" #name " (" desc ")", SS_SIG ## name, 0);
  2042. #define ADD_SUB(name) \
  2043. add_special(ctx, "SIG" #name, SS_SIG ## name, 0);
  2044. #define NO_ADD_SUB(name)
  2045. #define NO_ADD_MAIN(name, desc)
  2046. SIGNAL_LIST(ADD_MAIN, NO_ADD_SUB);
  2047. add_special(ctx, "More signals", SS_SUBMENU, 0);
  2048. SIGNAL_LIST(NO_ADD_MAIN, ADD_SUB);
  2049. add_special(ctx, NULL, SS_EXITMENU, 0);
  2050. #undef ADD_MAIN
  2051. #undef ADD_SUB
  2052. #undef NO_ADD_MAIN
  2053. #undef NO_ADD_SUB
  2054. toret = TRUE;
  2055. }
  2056. /*
  2057. * Don't bother offering IGNORE if we've decided the remote
  2058. * won't cope with it, since we wouldn't bother sending it if
  2059. * asked anyway.
  2060. */
  2061. if (!(s->ppl.remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
  2062. if (toret)
  2063. add_special(ctx, NULL, SS_SEP, 0);
  2064. add_special(ctx, "IGNORE message", SS_NOP, 0);
  2065. toret = TRUE;
  2066. }
  2067. return toret;
  2068. }
  2069. static const char *ssh_signal_lookup(SessionSpecialCode code)
  2070. {
  2071. #define CHECK_SUB(name) \
  2072. if (code == SS_SIG ## name) return #name;
  2073. #define CHECK_MAIN(name, desc) CHECK_SUB(name)
  2074. SIGNAL_LIST(CHECK_MAIN, CHECK_SUB);
  2075. return NULL;
  2076. #undef CHECK_MAIN
  2077. #undef CHECK_SUB
  2078. }
  2079. static void ssh2_connection_special_cmd(PacketProtocolLayer *ppl,
  2080. SessionSpecialCode code, int arg)
  2081. {
  2082. struct ssh2_connection_state *s =
  2083. container_of(ppl, struct ssh2_connection_state, ppl);
  2084. PktOut *pktout;
  2085. const char *signame;
  2086. if (code == SS_PING || code == SS_NOP) {
  2087. if (!(s->ppl.remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
  2088. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_IGNORE);
  2089. put_stringz(pktout, "");
  2090. pq_push(s->ppl.out_pq, pktout);
  2091. }
  2092. } else if (code == SS_EOF) {
  2093. if (!s->mainchan_ready) {
  2094. /*
  2095. * Buffer the EOF to send as soon as the main channel is
  2096. * fully set up.
  2097. */
  2098. s->mainchan_eof_pending = TRUE;
  2099. } else if (s->mainchan && !s->mainchan_eof_sent) {
  2100. sshfwd_write_eof(&s->mainchan->sc);
  2101. }
  2102. } else if (code == SS_BRK) {
  2103. if (s->mainchan) {
  2104. pktout = ssh2_chanreq_init(s->mainchan, "break", NULL, NULL);
  2105. put_uint32(pktout, 0); /* default break length */
  2106. pq_push(s->ppl.out_pq, pktout);
  2107. }
  2108. } else if ((signame = ssh_signal_lookup(code)) != NULL) {
  2109. /* It's a signal. */
  2110. if (s->mainchan) {
  2111. pktout = ssh2_chanreq_init(s->mainchan, "signal", NULL, NULL);
  2112. put_stringz(pktout, signame);
  2113. pq_push(s->ppl.out_pq, pktout);
  2114. ppl_logevent(("Sent signal SIG%s", signame));
  2115. }
  2116. }
  2117. }
  2118. static void ssh2_terminal_size(ConnectionLayer *cl, int width, int height)
  2119. {
  2120. struct ssh2_connection_state *s =
  2121. container_of(cl, struct ssh2_connection_state, cl);
  2122. s->term_width = width;
  2123. s->term_height = height;
  2124. if (s->mainchan_ready) {
  2125. PktOut *pktout = ssh2_chanreq_init(
  2126. s->mainchan, "window-change", NULL, NULL);
  2127. put_uint32(pktout, s->term_width);
  2128. put_uint32(pktout, s->term_height);
  2129. put_uint32(pktout, 0);
  2130. put_uint32(pktout, 0);
  2131. pq_push(s->ppl.out_pq, pktout);
  2132. }
  2133. }
  2134. static void ssh2_stdout_unthrottle(ConnectionLayer *cl, int bufsize)
  2135. {
  2136. struct ssh2_connection_state *s =
  2137. container_of(cl, struct ssh2_connection_state, cl);
  2138. if (s->mainchan)
  2139. ssh2channel_unthrottle(&s->mainchan->sc, bufsize);
  2140. }
  2141. static int ssh2_stdin_backlog(ConnectionLayer *cl)
  2142. {
  2143. struct ssh2_connection_state *s =
  2144. container_of(cl, struct ssh2_connection_state, cl);
  2145. return s->mainchan ? bufchain_size(&s->mainchan->outbuffer) : 0;
  2146. }
  2147. static void ssh2_throttle_all_channels(ConnectionLayer *cl, int throttled)
  2148. {
  2149. struct ssh2_connection_state *s =
  2150. container_of(cl, struct ssh2_connection_state, cl);
  2151. struct ssh2_channel *c;
  2152. int i;
  2153. s->all_channels_throttled = throttled;
  2154. for (i = 0; NULL != (c = index234(s->channels, i)); i++)
  2155. ssh2_channel_check_throttle(c);
  2156. }
  2157. static int ssh2_ldisc_option(ConnectionLayer *cl, int option)
  2158. {
  2159. struct ssh2_connection_state *s =
  2160. container_of(cl, struct ssh2_connection_state, cl);
  2161. /* We always return the same value for LD_ECHO and LD_EDIT */
  2162. return s->echoedit;
  2163. }
  2164. static int ssh2_connection_want_user_input(PacketProtocolLayer *ppl)
  2165. {
  2166. struct ssh2_connection_state *s =
  2167. container_of(ppl, struct ssh2_connection_state, ppl);
  2168. return s->mainchan_ready && s->want_user_input;
  2169. }
  2170. static void ssh2_connection_got_user_input(PacketProtocolLayer *ppl)
  2171. {
  2172. struct ssh2_connection_state *s =
  2173. container_of(ppl, struct ssh2_connection_state, ppl);
  2174. while (s->mainchan && bufchain_size(s->ppl.user_input) > 0) {
  2175. /*
  2176. * Add user input to the main channel's buffer.
  2177. */
  2178. void *data;
  2179. int len;
  2180. bufchain_prefix(s->ppl.user_input, &data, &len);
  2181. sshfwd_write(&s->mainchan->sc, data, len);
  2182. bufchain_consume(s->ppl.user_input, len);
  2183. }
  2184. }
  2185. static void ssh2_connection_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  2186. {
  2187. struct ssh2_connection_state *s =
  2188. container_of(ppl, struct ssh2_connection_state, ppl);
  2189. conf_free(s->conf);
  2190. s->conf = conf_copy(conf);
  2191. if (s->portfwdmgr_configured)
  2192. portfwdmgr_config(s->portfwdmgr, s->conf);
  2193. }
  2194. #include <puttyexp.h>
  2195. static unsigned int ssh2_connection_winscp_query(PacketProtocolLayer *ppl, int query)
  2196. {
  2197. struct ssh2_connection_state *s =
  2198. container_of(ppl, struct ssh2_connection_state, ppl);
  2199. if (query == WINSCP_QUERY_REMMAXPKT)
  2200. {
  2201. return s->mainchan != NULL ? s->mainchan->remmaxpkt : 0;
  2202. }
  2203. else if (query == WINSCP_QUERY_MAIN_CHANNEL)
  2204. {
  2205. return s->mainchan_ready;
  2206. }
  2207. else
  2208. {
  2209. assert(0);
  2210. return 0;
  2211. }
  2212. }