ssh2connection.c 87 KB

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