ssh2connection.c 62 KB

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