ssh2connection.c 88 KB

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