mux.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356
  1. /* $OpenBSD: mux.c,v 1.86 2020/10/29 02:52:43 djm Exp $ */
  2. /*
  3. * Copyright (c) 2002-2008 Damien Miller <djm@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* ssh session multiplexing support */
  18. #include "includes.h"
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <sys/socket.h>
  22. #include <sys/un.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <signal.h>
  26. #include <stdarg.h>
  27. #include <stddef.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #ifdef HAVE_PATHS_H
  33. #include <paths.h>
  34. #endif
  35. #ifdef HAVE_POLL_H
  36. #include <poll.h>
  37. #else
  38. # ifdef HAVE_SYS_POLL_H
  39. # include <sys/poll.h>
  40. # endif
  41. #endif
  42. #ifdef HAVE_UTIL_H
  43. # include <util.h>
  44. #endif
  45. #include "openbsd-compat/sys-queue.h"
  46. #include "xmalloc.h"
  47. #include "log.h"
  48. #include "ssh.h"
  49. #include "ssh2.h"
  50. #include "pathnames.h"
  51. #include "misc.h"
  52. #include "match.h"
  53. #include "sshbuf.h"
  54. #include "channels.h"
  55. #include "msg.h"
  56. #include "packet.h"
  57. #include "monitor_fdpass.h"
  58. #include "sshpty.h"
  59. #include "sshkey.h"
  60. #include "readconf.h"
  61. #include "clientloop.h"
  62. #include "ssherr.h"
  63. /* from ssh.c */
  64. extern int tty_flag;
  65. extern Options options;
  66. extern char *host;
  67. extern int subsystem_flag;
  68. extern struct sshbuf *command;
  69. extern volatile sig_atomic_t quit_pending;
  70. /* Context for session open confirmation callback */
  71. struct mux_session_confirm_ctx {
  72. u_int want_tty;
  73. u_int want_subsys;
  74. u_int want_x_fwd;
  75. u_int want_agent_fwd;
  76. struct sshbuf *cmd;
  77. char *term;
  78. struct termios tio;
  79. char **env;
  80. u_int rid;
  81. };
  82. /* Context for stdio fwd open confirmation callback */
  83. struct mux_stdio_confirm_ctx {
  84. u_int rid;
  85. };
  86. /* Context for global channel callback */
  87. struct mux_channel_confirm_ctx {
  88. u_int cid; /* channel id */
  89. u_int rid; /* request id */
  90. int fid; /* forward id */
  91. };
  92. /* fd to control socket */
  93. int muxserver_sock = -1;
  94. /* client request id */
  95. u_int muxclient_request_id = 0;
  96. /* Multiplexing control command */
  97. u_int muxclient_command = 0;
  98. /* Set when signalled. */
  99. static volatile sig_atomic_t muxclient_terminate = 0;
  100. /* PID of multiplex server */
  101. static u_int muxserver_pid = 0;
  102. static Channel *mux_listener_channel = NULL;
  103. struct mux_master_state {
  104. int hello_rcvd;
  105. };
  106. /* mux protocol messages */
  107. #define MUX_MSG_HELLO 0x00000001
  108. #define MUX_C_NEW_SESSION 0x10000002
  109. #define MUX_C_ALIVE_CHECK 0x10000004
  110. #define MUX_C_TERMINATE 0x10000005
  111. #define MUX_C_OPEN_FWD 0x10000006
  112. #define MUX_C_CLOSE_FWD 0x10000007
  113. #define MUX_C_NEW_STDIO_FWD 0x10000008
  114. #define MUX_C_STOP_LISTENING 0x10000009
  115. #define MUX_C_PROXY 0x1000000f
  116. #define MUX_S_OK 0x80000001
  117. #define MUX_S_PERMISSION_DENIED 0x80000002
  118. #define MUX_S_FAILURE 0x80000003
  119. #define MUX_S_EXIT_MESSAGE 0x80000004
  120. #define MUX_S_ALIVE 0x80000005
  121. #define MUX_S_SESSION_OPENED 0x80000006
  122. #define MUX_S_REMOTE_PORT 0x80000007
  123. #define MUX_S_TTY_ALLOC_FAIL 0x80000008
  124. #define MUX_S_PROXY 0x8000000f
  125. /* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
  126. #define MUX_FWD_LOCAL 1
  127. #define MUX_FWD_REMOTE 2
  128. #define MUX_FWD_DYNAMIC 3
  129. static void mux_session_confirm(struct ssh *, int, int, void *);
  130. static void mux_stdio_confirm(struct ssh *, int, int, void *);
  131. static int mux_master_process_hello(struct ssh *, u_int,
  132. Channel *, struct sshbuf *, struct sshbuf *);
  133. static int mux_master_process_new_session(struct ssh *, u_int,
  134. Channel *, struct sshbuf *, struct sshbuf *);
  135. static int mux_master_process_alive_check(struct ssh *, u_int,
  136. Channel *, struct sshbuf *, struct sshbuf *);
  137. static int mux_master_process_terminate(struct ssh *, u_int,
  138. Channel *, struct sshbuf *, struct sshbuf *);
  139. static int mux_master_process_open_fwd(struct ssh *, u_int,
  140. Channel *, struct sshbuf *, struct sshbuf *);
  141. static int mux_master_process_close_fwd(struct ssh *, u_int,
  142. Channel *, struct sshbuf *, struct sshbuf *);
  143. static int mux_master_process_stdio_fwd(struct ssh *, u_int,
  144. Channel *, struct sshbuf *, struct sshbuf *);
  145. static int mux_master_process_stop_listening(struct ssh *, u_int,
  146. Channel *, struct sshbuf *, struct sshbuf *);
  147. static int mux_master_process_proxy(struct ssh *, u_int,
  148. Channel *, struct sshbuf *, struct sshbuf *);
  149. static const struct {
  150. u_int type;
  151. int (*handler)(struct ssh *, u_int, Channel *,
  152. struct sshbuf *, struct sshbuf *);
  153. } mux_master_handlers[] = {
  154. { MUX_MSG_HELLO, mux_master_process_hello },
  155. { MUX_C_NEW_SESSION, mux_master_process_new_session },
  156. { MUX_C_ALIVE_CHECK, mux_master_process_alive_check },
  157. { MUX_C_TERMINATE, mux_master_process_terminate },
  158. { MUX_C_OPEN_FWD, mux_master_process_open_fwd },
  159. { MUX_C_CLOSE_FWD, mux_master_process_close_fwd },
  160. { MUX_C_NEW_STDIO_FWD, mux_master_process_stdio_fwd },
  161. { MUX_C_STOP_LISTENING, mux_master_process_stop_listening },
  162. { MUX_C_PROXY, mux_master_process_proxy },
  163. { 0, NULL }
  164. };
  165. /* Cleanup callback fired on closure of mux client _session_ channel */
  166. /* ARGSUSED */
  167. static void
  168. mux_master_session_cleanup_cb(struct ssh *ssh, int cid, void *unused)
  169. {
  170. Channel *cc, *c = channel_by_id(ssh, cid);
  171. debug3("entering for channel %d", cid);
  172. if (c == NULL)
  173. fatal("channel_by_id(%i) == NULL", cid);
  174. if (c->ctl_chan != -1) {
  175. if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
  176. fatal("channel %d missing control channel %d",
  177. c->self, c->ctl_chan);
  178. c->ctl_chan = -1;
  179. cc->remote_id = 0;
  180. cc->have_remote_id = 0;
  181. chan_rcvd_oclose(ssh, cc);
  182. }
  183. channel_cancel_cleanup(ssh, c->self);
  184. }
  185. /* Cleanup callback fired on closure of mux client _control_ channel */
  186. /* ARGSUSED */
  187. static void
  188. mux_master_control_cleanup_cb(struct ssh *ssh, int cid, void *unused)
  189. {
  190. Channel *sc, *c = channel_by_id(ssh, cid);
  191. debug3("entering for channel %d", cid);
  192. if (c == NULL)
  193. fatal("channel_by_id(%i) == NULL", cid);
  194. if (c->have_remote_id) {
  195. if ((sc = channel_by_id(ssh, c->remote_id)) == NULL)
  196. fatal("channel %d missing session channel %u",
  197. c->self, c->remote_id);
  198. c->remote_id = 0;
  199. c->have_remote_id = 0;
  200. sc->ctl_chan = -1;
  201. if (sc->type != SSH_CHANNEL_OPEN &&
  202. sc->type != SSH_CHANNEL_OPENING) {
  203. debug2("channel %d: not open", sc->self);
  204. chan_mark_dead(ssh, sc);
  205. } else {
  206. if (sc->istate == CHAN_INPUT_OPEN)
  207. chan_read_failed(ssh, sc);
  208. if (sc->ostate == CHAN_OUTPUT_OPEN)
  209. chan_write_failed(ssh, sc);
  210. }
  211. }
  212. channel_cancel_cleanup(ssh, c->self);
  213. }
  214. /* Check mux client environment variables before passing them to mux master. */
  215. static int
  216. env_permitted(char *env)
  217. {
  218. int i, ret;
  219. char name[1024], *cp;
  220. if ((cp = strchr(env, '=')) == NULL || cp == env)
  221. return 0;
  222. ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
  223. if (ret <= 0 || (size_t)ret >= sizeof(name)) {
  224. error("name '%.100s...' too long", env);
  225. return 0;
  226. }
  227. for (i = 0; i < options.num_send_env; i++)
  228. if (match_pattern(name, options.send_env[i]))
  229. return 1;
  230. return 0;
  231. }
  232. /* Mux master protocol message handlers */
  233. static int
  234. mux_master_process_hello(struct ssh *ssh, u_int rid,
  235. Channel *c, struct sshbuf *m, struct sshbuf *reply)
  236. {
  237. u_int ver;
  238. struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
  239. int r;
  240. if (state == NULL)
  241. fatal("channel %d: c->mux_ctx == NULL", c->self);
  242. if (state->hello_rcvd) {
  243. error("HELLO received twice");
  244. return -1;
  245. }
  246. if ((r = sshbuf_get_u32(m, &ver)) != 0) {
  247. error(r, "parse");
  248. return -1;
  249. }
  250. if (ver != SSHMUX_VER) {
  251. error("unsupported multiplexing protocol version %u "
  252. "(expected %u)", ver, SSHMUX_VER);
  253. return -1;
  254. }
  255. debug2("channel %d client version %u", c->self, ver);
  256. /* No extensions are presently defined */
  257. while (sshbuf_len(m) > 0) {
  258. char *name = NULL;
  259. size_t value_len = 0;
  260. if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
  261. (r = sshbuf_get_string_direct(m, NULL, &value_len)) != 0) {
  262. error(r, "parse extension");
  263. return -1;
  264. }
  265. debug2("Unrecognised extension \"%s\" length %zu",
  266. name, value_len);
  267. free(name);
  268. }
  269. state->hello_rcvd = 1;
  270. return 0;
  271. }
  272. /* Enqueue a "ok" response to the reply buffer */
  273. static void
  274. reply_ok(struct sshbuf *reply, u_int rid)
  275. {
  276. int r;
  277. if ((r = sshbuf_put_u32(reply, MUX_S_OK)) != 0 ||
  278. (r = sshbuf_put_u32(reply, rid)) != 0)
  279. fatal(r, "reply");
  280. }
  281. /* Enqueue an error response to the reply buffer */
  282. static void
  283. reply_error(struct sshbuf *reply, u_int type, u_int rid, const char *msg)
  284. {
  285. int r;
  286. if ((r = sshbuf_put_u32(reply, type)) != 0 ||
  287. (r = sshbuf_put_u32(reply, rid)) != 0 ||
  288. (r = sshbuf_put_cstring(reply, msg)) != 0)
  289. fatal(r, "reply");
  290. }
  291. static int
  292. mux_master_process_new_session(struct ssh *ssh, u_int rid,
  293. Channel *c, struct sshbuf *m, struct sshbuf *reply)
  294. {
  295. Channel *nc;
  296. struct mux_session_confirm_ctx *cctx;
  297. char *cmd, *cp;
  298. u_int i, j, env_len, escape_char, window, packetmax;
  299. int r, new_fd[3];
  300. /* Reply for SSHMUX_COMMAND_OPEN */
  301. cctx = xcalloc(1, sizeof(*cctx));
  302. cctx->term = NULL;
  303. cctx->rid = rid;
  304. cmd = NULL;
  305. cctx->env = NULL;
  306. env_len = 0;
  307. if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
  308. (r = sshbuf_get_u32(m, &cctx->want_tty)) != 0 ||
  309. (r = sshbuf_get_u32(m, &cctx->want_x_fwd)) != 0 ||
  310. (r = sshbuf_get_u32(m, &cctx->want_agent_fwd)) != 0 ||
  311. (r = sshbuf_get_u32(m, &cctx->want_subsys)) != 0 ||
  312. (r = sshbuf_get_u32(m, &escape_char)) != 0 ||
  313. (r = sshbuf_get_cstring(m, &cctx->term, NULL)) != 0 ||
  314. (r = sshbuf_get_cstring(m, &cmd, NULL)) != 0) {
  315. malf:
  316. free(cmd);
  317. for (j = 0; j < env_len; j++)
  318. free(cctx->env[j]);
  319. free(cctx->env);
  320. free(cctx->term);
  321. free(cctx);
  322. error("malformed message");
  323. return -1;
  324. }
  325. #define MUX_MAX_ENV_VARS 4096
  326. while (sshbuf_len(m) > 0) {
  327. if ((r = sshbuf_get_cstring(m, &cp, NULL)) != 0)
  328. goto malf;
  329. if (!env_permitted(cp)) {
  330. free(cp);
  331. continue;
  332. }
  333. cctx->env = xreallocarray(cctx->env, env_len + 2,
  334. sizeof(*cctx->env));
  335. cctx->env[env_len++] = cp;
  336. cctx->env[env_len] = NULL;
  337. if (env_len > MUX_MAX_ENV_VARS) {
  338. error(">%d environment variables received, "
  339. "ignoring additional", MUX_MAX_ENV_VARS);
  340. break;
  341. }
  342. }
  343. debug2("channel %d: request tty %d, X %d, agent %d, subsys %d, "
  344. "term \"%s\", cmd \"%s\", env %u", c->self,
  345. cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
  346. cctx->want_subsys, cctx->term, cmd, env_len);
  347. if ((cctx->cmd = sshbuf_new()) == NULL)
  348. fatal("sshbuf_new");
  349. if ((r = sshbuf_put(cctx->cmd, cmd, strlen(cmd))) != 0)
  350. fatal(r, "sshbuf_put");
  351. free(cmd);
  352. cmd = NULL;
  353. /* Gather fds from client */
  354. for(i = 0; i < 3; i++) {
  355. if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
  356. error("failed to receive fd %d from client", i);
  357. for (j = 0; j < i; j++)
  358. close(new_fd[j]);
  359. for (j = 0; j < env_len; j++)
  360. free(cctx->env[j]);
  361. free(cctx->env);
  362. free(cctx->term);
  363. sshbuf_free(cctx->cmd);
  364. free(cctx);
  365. reply_error(reply, MUX_S_FAILURE, rid,
  366. "did not receive file descriptors");
  367. return -1;
  368. }
  369. }
  370. debug3("got fds stdin %d, stdout %d, stderr %d",
  371. new_fd[0], new_fd[1], new_fd[2]);
  372. /* XXX support multiple child sessions in future */
  373. if (c->have_remote_id) {
  374. debug2("session already open");
  375. reply_error(reply, MUX_S_FAILURE, rid,
  376. "Multiple sessions not supported");
  377. cleanup:
  378. close(new_fd[0]);
  379. close(new_fd[1]);
  380. close(new_fd[2]);
  381. free(cctx->term);
  382. if (env_len != 0) {
  383. for (i = 0; i < env_len; i++)
  384. free(cctx->env[i]);
  385. free(cctx->env);
  386. }
  387. sshbuf_free(cctx->cmd);
  388. free(cctx);
  389. return 0;
  390. }
  391. if (options.control_master == SSHCTL_MASTER_ASK ||
  392. options.control_master == SSHCTL_MASTER_AUTO_ASK) {
  393. if (!ask_permission("Allow shared connection to %s? ", host)) {
  394. debug2("session refused by user");
  395. reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
  396. "Permission denied");
  397. goto cleanup;
  398. }
  399. }
  400. /* Try to pick up ttymodes from client before it goes raw */
  401. if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
  402. error("tcgetattr: %s", strerror(errno));
  403. /* enable nonblocking unless tty */
  404. if (!isatty(new_fd[0]))
  405. set_nonblock(new_fd[0]);
  406. if (!isatty(new_fd[1]))
  407. set_nonblock(new_fd[1]);
  408. if (!isatty(new_fd[2]))
  409. set_nonblock(new_fd[2]);
  410. window = CHAN_SES_WINDOW_DEFAULT;
  411. packetmax = CHAN_SES_PACKET_DEFAULT;
  412. if (cctx->want_tty) {
  413. window >>= 1;
  414. packetmax >>= 1;
  415. }
  416. nc = channel_new(ssh, "session", SSH_CHANNEL_OPENING,
  417. new_fd[0], new_fd[1], new_fd[2], window, packetmax,
  418. CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
  419. nc->ctl_chan = c->self; /* link session -> control channel */
  420. c->remote_id = nc->self; /* link control -> session channel */
  421. c->have_remote_id = 1;
  422. if (cctx->want_tty && escape_char != 0xffffffff) {
  423. channel_register_filter(ssh, nc->self,
  424. client_simple_escape_filter, NULL,
  425. client_filter_cleanup,
  426. client_new_escape_filter_ctx((int)escape_char));
  427. }
  428. debug2("channel_new: %d linked to control channel %d",
  429. nc->self, nc->ctl_chan);
  430. channel_send_open(ssh, nc->self);
  431. channel_register_open_confirm(ssh, nc->self, mux_session_confirm, cctx);
  432. c->mux_pause = 1; /* stop handling messages until open_confirm done */
  433. channel_register_cleanup(ssh, nc->self,
  434. mux_master_session_cleanup_cb, 1);
  435. /* reply is deferred, sent by mux_session_confirm */
  436. return 0;
  437. }
  438. static int
  439. mux_master_process_alive_check(struct ssh *ssh, u_int rid,
  440. Channel *c, struct sshbuf *m, struct sshbuf *reply)
  441. {
  442. int r;
  443. debug2("channel %d: alive check", c->self);
  444. /* prepare reply */
  445. if ((r = sshbuf_put_u32(reply, MUX_S_ALIVE)) != 0 ||
  446. (r = sshbuf_put_u32(reply, rid)) != 0 ||
  447. (r = sshbuf_put_u32(reply, (u_int)getpid())) != 0)
  448. fatal(r, "reply");
  449. return 0;
  450. }
  451. static int
  452. mux_master_process_terminate(struct ssh *ssh, u_int rid,
  453. Channel *c, struct sshbuf *m, struct sshbuf *reply)
  454. {
  455. debug2("channel %d: terminate request", c->self);
  456. if (options.control_master == SSHCTL_MASTER_ASK ||
  457. options.control_master == SSHCTL_MASTER_AUTO_ASK) {
  458. if (!ask_permission("Terminate shared connection to %s? ",
  459. host)) {
  460. debug2("termination refused by user");
  461. reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
  462. "Permission denied");
  463. return 0;
  464. }
  465. }
  466. quit_pending = 1;
  467. reply_ok(reply, rid);
  468. /* XXX exit happens too soon - message never makes it to client */
  469. return 0;
  470. }
  471. static char *
  472. format_forward(u_int ftype, struct Forward *fwd)
  473. {
  474. char *ret;
  475. switch (ftype) {
  476. case MUX_FWD_LOCAL:
  477. xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
  478. (fwd->listen_path != NULL) ? fwd->listen_path :
  479. (fwd->listen_host == NULL) ?
  480. (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
  481. fwd->listen_host, fwd->listen_port,
  482. (fwd->connect_path != NULL) ? fwd->connect_path :
  483. fwd->connect_host, fwd->connect_port);
  484. break;
  485. case MUX_FWD_DYNAMIC:
  486. xasprintf(&ret, "dynamic forward %.200s:%d -> *",
  487. (fwd->listen_host == NULL) ?
  488. (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
  489. fwd->listen_host, fwd->listen_port);
  490. break;
  491. case MUX_FWD_REMOTE:
  492. xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
  493. (fwd->listen_path != NULL) ? fwd->listen_path :
  494. (fwd->listen_host == NULL) ?
  495. "LOCALHOST" : fwd->listen_host,
  496. fwd->listen_port,
  497. (fwd->connect_path != NULL) ? fwd->connect_path :
  498. fwd->connect_host, fwd->connect_port);
  499. break;
  500. default:
  501. fatal("unknown forward type %u", ftype);
  502. }
  503. return ret;
  504. }
  505. static int
  506. compare_host(const char *a, const char *b)
  507. {
  508. if (a == NULL && b == NULL)
  509. return 1;
  510. if (a == NULL || b == NULL)
  511. return 0;
  512. return strcmp(a, b) == 0;
  513. }
  514. static int
  515. compare_forward(struct Forward *a, struct Forward *b)
  516. {
  517. if (!compare_host(a->listen_host, b->listen_host))
  518. return 0;
  519. if (!compare_host(a->listen_path, b->listen_path))
  520. return 0;
  521. if (a->listen_port != b->listen_port)
  522. return 0;
  523. if (!compare_host(a->connect_host, b->connect_host))
  524. return 0;
  525. if (!compare_host(a->connect_path, b->connect_path))
  526. return 0;
  527. if (a->connect_port != b->connect_port)
  528. return 0;
  529. return 1;
  530. }
  531. static void
  532. mux_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
  533. {
  534. struct mux_channel_confirm_ctx *fctx = ctxt;
  535. char *failmsg = NULL;
  536. struct Forward *rfwd;
  537. Channel *c;
  538. struct sshbuf *out;
  539. u_int port;
  540. int r;
  541. if ((c = channel_by_id(ssh, fctx->cid)) == NULL) {
  542. /* no channel for reply */
  543. error("unknown channel");
  544. return;
  545. }
  546. if ((out = sshbuf_new()) == NULL)
  547. fatal("sshbuf_new");
  548. if (fctx->fid >= options.num_remote_forwards ||
  549. (options.remote_forwards[fctx->fid].connect_path == NULL &&
  550. options.remote_forwards[fctx->fid].connect_host == NULL)) {
  551. xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
  552. goto fail;
  553. }
  554. rfwd = &options.remote_forwards[fctx->fid];
  555. debug("%s for: listen %d, connect %s:%d",
  556. type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
  557. rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
  558. rfwd->connect_host, rfwd->connect_port);
  559. if (type == SSH2_MSG_REQUEST_SUCCESS) {
  560. if (rfwd->listen_port == 0) {
  561. if ((r = sshpkt_get_u32(ssh, &port)) != 0)
  562. fatal(r, "parse port");
  563. if (port > 65535) {
  564. fatal("Invalid allocated port %u for "
  565. "mux remote forward to %s:%d", port,
  566. rfwd->connect_host, rfwd->connect_port);
  567. }
  568. rfwd->allocated_port = (int)port;
  569. debug("Allocated port %u for mux remote forward"
  570. " to %s:%d", rfwd->allocated_port,
  571. rfwd->connect_host, rfwd->connect_port);
  572. if ((r = sshbuf_put_u32(out,
  573. MUX_S_REMOTE_PORT)) != 0 ||
  574. (r = sshbuf_put_u32(out, fctx->rid)) != 0 ||
  575. (r = sshbuf_put_u32(out,
  576. rfwd->allocated_port)) != 0)
  577. fatal(r, "reply");
  578. channel_update_permission(ssh, rfwd->handle,
  579. rfwd->allocated_port);
  580. } else {
  581. reply_ok(out, fctx->rid);
  582. }
  583. goto out;
  584. } else {
  585. if (rfwd->listen_port == 0)
  586. channel_update_permission(ssh, rfwd->handle, -1);
  587. if (rfwd->listen_path != NULL)
  588. xasprintf(&failmsg, "remote port forwarding failed for "
  589. "listen path %s", rfwd->listen_path);
  590. else
  591. xasprintf(&failmsg, "remote port forwarding failed for "
  592. "listen port %d", rfwd->listen_port);
  593. debug2("clearing registered forwarding for listen %d, "
  594. "connect %s:%d", rfwd->listen_port,
  595. rfwd->connect_path ? rfwd->connect_path :
  596. rfwd->connect_host, rfwd->connect_port);
  597. free(rfwd->listen_host);
  598. free(rfwd->listen_path);
  599. free(rfwd->connect_host);
  600. free(rfwd->connect_path);
  601. memset(rfwd, 0, sizeof(*rfwd));
  602. }
  603. fail:
  604. error("%s", failmsg);
  605. reply_error(out, MUX_S_FAILURE, fctx->rid, failmsg);
  606. free(failmsg);
  607. out:
  608. if ((r = sshbuf_put_stringb(c->output, out)) != 0)
  609. fatal(r, "enqueue");
  610. sshbuf_free(out);
  611. if (c->mux_pause <= 0)
  612. fatal("mux_pause %d", c->mux_pause);
  613. c->mux_pause = 0; /* start processing messages again */
  614. }
  615. static int
  616. mux_master_process_open_fwd(struct ssh *ssh, u_int rid,
  617. Channel *c, struct sshbuf *m, struct sshbuf *reply)
  618. {
  619. struct Forward fwd;
  620. char *fwd_desc = NULL;
  621. char *listen_addr, *connect_addr;
  622. u_int ftype;
  623. u_int lport, cport;
  624. int r, i, ret = 0, freefwd = 1;
  625. memset(&fwd, 0, sizeof(fwd));
  626. /* XXX - lport/cport check redundant */
  627. if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
  628. (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
  629. (r = sshbuf_get_u32(m, &lport)) != 0 ||
  630. (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
  631. (r = sshbuf_get_u32(m, &cport)) != 0 ||
  632. (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
  633. (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
  634. error("malformed message");
  635. ret = -1;
  636. goto out;
  637. }
  638. if (*listen_addr == '\0') {
  639. free(listen_addr);
  640. listen_addr = NULL;
  641. }
  642. if (*connect_addr == '\0') {
  643. free(connect_addr);
  644. connect_addr = NULL;
  645. }
  646. memset(&fwd, 0, sizeof(fwd));
  647. fwd.listen_port = lport;
  648. if (fwd.listen_port == PORT_STREAMLOCAL)
  649. fwd.listen_path = listen_addr;
  650. else
  651. fwd.listen_host = listen_addr;
  652. fwd.connect_port = cport;
  653. if (fwd.connect_port == PORT_STREAMLOCAL)
  654. fwd.connect_path = connect_addr;
  655. else
  656. fwd.connect_host = connect_addr;
  657. debug2("channel %d: request %s", c->self,
  658. (fwd_desc = format_forward(ftype, &fwd)));
  659. if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
  660. ftype != MUX_FWD_DYNAMIC) {
  661. logit("invalid forwarding type %u", ftype);
  662. invalid:
  663. free(listen_addr);
  664. free(connect_addr);
  665. reply_error(reply, MUX_S_FAILURE, rid,
  666. "Invalid forwarding request");
  667. return 0;
  668. }
  669. if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) {
  670. logit("streamlocal and dynamic forwards "
  671. "are mutually exclusive");
  672. goto invalid;
  673. }
  674. if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) {
  675. logit("invalid listen port %u", fwd.listen_port);
  676. goto invalid;
  677. }
  678. if ((fwd.connect_port != PORT_STREAMLOCAL &&
  679. fwd.connect_port >= 65536) ||
  680. (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE &&
  681. fwd.connect_port == 0)) {
  682. logit("invalid connect port %u",
  683. fwd.connect_port);
  684. goto invalid;
  685. }
  686. if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL &&
  687. fwd.connect_path == NULL) {
  688. logit("missing connect host");
  689. goto invalid;
  690. }
  691. /* Skip forwards that have already been requested */
  692. switch (ftype) {
  693. case MUX_FWD_LOCAL:
  694. case MUX_FWD_DYNAMIC:
  695. for (i = 0; i < options.num_local_forwards; i++) {
  696. if (compare_forward(&fwd,
  697. options.local_forwards + i)) {
  698. exists:
  699. debug2("found existing forwarding");
  700. reply_ok(reply, rid);
  701. goto out;
  702. }
  703. }
  704. break;
  705. case MUX_FWD_REMOTE:
  706. for (i = 0; i < options.num_remote_forwards; i++) {
  707. if (!compare_forward(&fwd, options.remote_forwards + i))
  708. continue;
  709. if (fwd.listen_port != 0)
  710. goto exists;
  711. debug2("found allocated port");
  712. if ((r = sshbuf_put_u32(reply,
  713. MUX_S_REMOTE_PORT)) != 0 ||
  714. (r = sshbuf_put_u32(reply, rid)) != 0 ||
  715. (r = sshbuf_put_u32(reply,
  716. options.remote_forwards[i].allocated_port)) != 0)
  717. fatal(r, "reply FWD_REMOTE");
  718. goto out;
  719. }
  720. break;
  721. }
  722. if (options.control_master == SSHCTL_MASTER_ASK ||
  723. options.control_master == SSHCTL_MASTER_AUTO_ASK) {
  724. if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
  725. debug2("forwarding refused by user");
  726. reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
  727. "Permission denied");
  728. goto out;
  729. }
  730. }
  731. if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
  732. if (!channel_setup_local_fwd_listener(ssh, &fwd,
  733. &options.fwd_opts)) {
  734. fail:
  735. logit("requested %s failed", fwd_desc);
  736. reply_error(reply, MUX_S_FAILURE, rid,
  737. "Port forwarding failed");
  738. goto out;
  739. }
  740. add_local_forward(&options, &fwd);
  741. freefwd = 0;
  742. } else {
  743. struct mux_channel_confirm_ctx *fctx;
  744. fwd.handle = channel_request_remote_forwarding(ssh, &fwd);
  745. if (fwd.handle < 0)
  746. goto fail;
  747. add_remote_forward(&options, &fwd);
  748. fctx = xcalloc(1, sizeof(*fctx));
  749. fctx->cid = c->self;
  750. fctx->rid = rid;
  751. fctx->fid = options.num_remote_forwards - 1;
  752. client_register_global_confirm(mux_confirm_remote_forward,
  753. fctx);
  754. freefwd = 0;
  755. c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
  756. /* delayed reply in mux_confirm_remote_forward */
  757. goto out;
  758. }
  759. reply_ok(reply, rid);
  760. out:
  761. free(fwd_desc);
  762. if (freefwd) {
  763. free(fwd.listen_host);
  764. free(fwd.listen_path);
  765. free(fwd.connect_host);
  766. free(fwd.connect_path);
  767. }
  768. return ret;
  769. }
  770. static int
  771. mux_master_process_close_fwd(struct ssh *ssh, u_int rid,
  772. Channel *c, struct sshbuf *m, struct sshbuf *reply)
  773. {
  774. struct Forward fwd, *found_fwd;
  775. char *fwd_desc = NULL;
  776. const char *error_reason = NULL;
  777. char *listen_addr = NULL, *connect_addr = NULL;
  778. u_int ftype;
  779. int r, i, ret = 0;
  780. u_int lport, cport;
  781. memset(&fwd, 0, sizeof(fwd));
  782. if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
  783. (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
  784. (r = sshbuf_get_u32(m, &lport)) != 0 ||
  785. (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
  786. (r = sshbuf_get_u32(m, &cport)) != 0 ||
  787. (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
  788. (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
  789. error("malformed message");
  790. ret = -1;
  791. goto out;
  792. }
  793. if (*listen_addr == '\0') {
  794. free(listen_addr);
  795. listen_addr = NULL;
  796. }
  797. if (*connect_addr == '\0') {
  798. free(connect_addr);
  799. connect_addr = NULL;
  800. }
  801. memset(&fwd, 0, sizeof(fwd));
  802. fwd.listen_port = lport;
  803. if (fwd.listen_port == PORT_STREAMLOCAL)
  804. fwd.listen_path = listen_addr;
  805. else
  806. fwd.listen_host = listen_addr;
  807. fwd.connect_port = cport;
  808. if (fwd.connect_port == PORT_STREAMLOCAL)
  809. fwd.connect_path = connect_addr;
  810. else
  811. fwd.connect_host = connect_addr;
  812. debug2("channel %d: request cancel %s", c->self,
  813. (fwd_desc = format_forward(ftype, &fwd)));
  814. /* make sure this has been requested */
  815. found_fwd = NULL;
  816. switch (ftype) {
  817. case MUX_FWD_LOCAL:
  818. case MUX_FWD_DYNAMIC:
  819. for (i = 0; i < options.num_local_forwards; i++) {
  820. if (compare_forward(&fwd,
  821. options.local_forwards + i)) {
  822. found_fwd = options.local_forwards + i;
  823. break;
  824. }
  825. }
  826. break;
  827. case MUX_FWD_REMOTE:
  828. for (i = 0; i < options.num_remote_forwards; i++) {
  829. if (compare_forward(&fwd,
  830. options.remote_forwards + i)) {
  831. found_fwd = options.remote_forwards + i;
  832. break;
  833. }
  834. }
  835. break;
  836. }
  837. if (found_fwd == NULL)
  838. error_reason = "port not forwarded";
  839. else if (ftype == MUX_FWD_REMOTE) {
  840. /*
  841. * This shouldn't fail unless we confused the host/port
  842. * between options.remote_forwards and permitted_opens.
  843. * However, for dynamic allocated listen ports we need
  844. * to use the actual listen port.
  845. */
  846. if (channel_request_rforward_cancel(ssh, found_fwd) == -1)
  847. error_reason = "port not in permitted opens";
  848. } else { /* local and dynamic forwards */
  849. /* Ditto */
  850. if (channel_cancel_lport_listener(ssh, &fwd, fwd.connect_port,
  851. &options.fwd_opts) == -1)
  852. error_reason = "port not found";
  853. }
  854. if (error_reason != NULL)
  855. reply_error(reply, MUX_S_FAILURE, rid, error_reason);
  856. else {
  857. reply_ok(reply, rid);
  858. free(found_fwd->listen_host);
  859. free(found_fwd->listen_path);
  860. free(found_fwd->connect_host);
  861. free(found_fwd->connect_path);
  862. found_fwd->listen_host = found_fwd->connect_host = NULL;
  863. found_fwd->listen_path = found_fwd->connect_path = NULL;
  864. found_fwd->listen_port = found_fwd->connect_port = 0;
  865. }
  866. out:
  867. free(fwd_desc);
  868. free(listen_addr);
  869. free(connect_addr);
  870. return ret;
  871. }
  872. static int
  873. mux_master_process_stdio_fwd(struct ssh *ssh, u_int rid,
  874. Channel *c, struct sshbuf *m, struct sshbuf *reply)
  875. {
  876. Channel *nc;
  877. char *chost = NULL;
  878. u_int cport, i, j;
  879. int r, new_fd[2];
  880. struct mux_stdio_confirm_ctx *cctx;
  881. if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
  882. (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
  883. (r = sshbuf_get_u32(m, &cport)) != 0) {
  884. free(chost);
  885. error("malformed message");
  886. return -1;
  887. }
  888. debug2("channel %d: stdio fwd to %s:%u", c->self, chost, cport);
  889. /* Gather fds from client */
  890. for(i = 0; i < 2; i++) {
  891. if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
  892. error("failed to receive fd %d from client", i);
  893. for (j = 0; j < i; j++)
  894. close(new_fd[j]);
  895. free(chost);
  896. /* prepare reply */
  897. reply_error(reply, MUX_S_FAILURE, rid,
  898. "did not receive file descriptors");
  899. return -1;
  900. }
  901. }
  902. debug3("got fds stdin %d, stdout %d", new_fd[0], new_fd[1]);
  903. /* XXX support multiple child sessions in future */
  904. if (c->have_remote_id) {
  905. debug2("session already open");
  906. reply_error(reply, MUX_S_FAILURE, rid,
  907. "Multiple sessions not supported");
  908. cleanup:
  909. close(new_fd[0]);
  910. close(new_fd[1]);
  911. free(chost);
  912. return 0;
  913. }
  914. if (options.control_master == SSHCTL_MASTER_ASK ||
  915. options.control_master == SSHCTL_MASTER_AUTO_ASK) {
  916. if (!ask_permission("Allow forward to %s:%u? ",
  917. chost, cport)) {
  918. debug2("stdio fwd refused by user");
  919. reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
  920. "Permission denied");
  921. goto cleanup;
  922. }
  923. }
  924. /* enable nonblocking unless tty */
  925. if (!isatty(new_fd[0]))
  926. set_nonblock(new_fd[0]);
  927. if (!isatty(new_fd[1]))
  928. set_nonblock(new_fd[1]);
  929. nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1]);
  930. free(chost);
  931. nc->ctl_chan = c->self; /* link session -> control channel */
  932. c->remote_id = nc->self; /* link control -> session channel */
  933. c->have_remote_id = 1;
  934. debug2("channel_new: %d control %d", nc->self, nc->ctl_chan);
  935. channel_register_cleanup(ssh, nc->self,
  936. mux_master_session_cleanup_cb, 1);
  937. cctx = xcalloc(1, sizeof(*cctx));
  938. cctx->rid = rid;
  939. channel_register_open_confirm(ssh, nc->self, mux_stdio_confirm, cctx);
  940. c->mux_pause = 1; /* stop handling messages until open_confirm done */
  941. /* reply is deferred, sent by mux_session_confirm */
  942. return 0;
  943. }
  944. /* Callback on open confirmation in mux master for a mux stdio fwd session. */
  945. static void
  946. mux_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
  947. {
  948. struct mux_stdio_confirm_ctx *cctx = arg;
  949. Channel *c, *cc;
  950. struct sshbuf *reply;
  951. int r;
  952. if (cctx == NULL)
  953. fatal("cctx == NULL");
  954. if ((c = channel_by_id(ssh, id)) == NULL)
  955. fatal("no channel for id %d", id);
  956. if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
  957. fatal("channel %d lacks control channel %d",
  958. id, c->ctl_chan);
  959. if ((reply = sshbuf_new()) == NULL)
  960. fatal("sshbuf_new");
  961. if (!success) {
  962. debug3("sending failure reply");
  963. reply_error(reply, MUX_S_FAILURE, cctx->rid,
  964. "Session open refused by peer");
  965. /* prepare reply */
  966. goto done;
  967. }
  968. debug3("sending success reply");
  969. /* prepare reply */
  970. if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
  971. (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
  972. (r = sshbuf_put_u32(reply, c->self)) != 0)
  973. fatal(r, "reply");
  974. done:
  975. /* Send reply */
  976. if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
  977. fatal(r, "enqueue");
  978. sshbuf_free(reply);
  979. if (cc->mux_pause <= 0)
  980. fatal("mux_pause %d", cc->mux_pause);
  981. cc->mux_pause = 0; /* start processing messages again */
  982. c->open_confirm_ctx = NULL;
  983. free(cctx);
  984. }
  985. static int
  986. mux_master_process_stop_listening(struct ssh *ssh, u_int rid,
  987. Channel *c, struct sshbuf *m, struct sshbuf *reply)
  988. {
  989. debug("channel %d: stop listening", c->self);
  990. if (options.control_master == SSHCTL_MASTER_ASK ||
  991. options.control_master == SSHCTL_MASTER_AUTO_ASK) {
  992. if (!ask_permission("Disable further multiplexing on shared "
  993. "connection to %s? ", host)) {
  994. debug2("stop listen refused by user");
  995. reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
  996. "Permission denied");
  997. return 0;
  998. }
  999. }
  1000. if (mux_listener_channel != NULL) {
  1001. channel_free(ssh, mux_listener_channel);
  1002. client_stop_mux();
  1003. free(options.control_path);
  1004. options.control_path = NULL;
  1005. mux_listener_channel = NULL;
  1006. muxserver_sock = -1;
  1007. }
  1008. reply_ok(reply, rid);
  1009. return 0;
  1010. }
  1011. static int
  1012. mux_master_process_proxy(struct ssh *ssh, u_int rid,
  1013. Channel *c, struct sshbuf *m, struct sshbuf *reply)
  1014. {
  1015. int r;
  1016. debug("channel %d: proxy request", c->self);
  1017. c->mux_rcb = channel_proxy_downstream;
  1018. if ((r = sshbuf_put_u32(reply, MUX_S_PROXY)) != 0 ||
  1019. (r = sshbuf_put_u32(reply, rid)) != 0)
  1020. fatal(r, "reply");
  1021. return 0;
  1022. }
  1023. /* Channel callbacks fired on read/write from mux client fd */
  1024. static int
  1025. mux_master_read_cb(struct ssh *ssh, Channel *c)
  1026. {
  1027. struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
  1028. struct sshbuf *in = NULL, *out = NULL;
  1029. u_int type, rid, i;
  1030. int r, ret = -1;
  1031. if ((out = sshbuf_new()) == NULL)
  1032. fatal("sshbuf_new");
  1033. /* Setup ctx and */
  1034. if (c->mux_ctx == NULL) {
  1035. state = xcalloc(1, sizeof(*state));
  1036. c->mux_ctx = state;
  1037. channel_register_cleanup(ssh, c->self,
  1038. mux_master_control_cleanup_cb, 0);
  1039. /* Send hello */
  1040. if ((r = sshbuf_put_u32(out, MUX_MSG_HELLO)) != 0 ||
  1041. (r = sshbuf_put_u32(out, SSHMUX_VER)) != 0)
  1042. fatal(r, "reply");
  1043. /* no extensions */
  1044. if ((r = sshbuf_put_stringb(c->output, out)) != 0)
  1045. fatal(r, "enqueue");
  1046. debug3("channel %d: hello sent", c->self);
  1047. ret = 0;
  1048. goto out;
  1049. }
  1050. /* Channel code ensures that we receive whole packets */
  1051. if ((r = sshbuf_froms(c->input, &in)) != 0) {
  1052. malf:
  1053. error("malformed message");
  1054. goto out;
  1055. }
  1056. if ((r = sshbuf_get_u32(in, &type)) != 0)
  1057. goto malf;
  1058. debug3("channel %d packet type 0x%08x len %zu", c->self,
  1059. type, sshbuf_len(in));
  1060. if (type == MUX_MSG_HELLO)
  1061. rid = 0;
  1062. else {
  1063. if (!state->hello_rcvd) {
  1064. error("expected MUX_MSG_HELLO(0x%08x), "
  1065. "received 0x%08x", MUX_MSG_HELLO, type);
  1066. goto out;
  1067. }
  1068. if ((r = sshbuf_get_u32(in, &rid)) != 0)
  1069. goto malf;
  1070. }
  1071. for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
  1072. if (type == mux_master_handlers[i].type) {
  1073. ret = mux_master_handlers[i].handler(ssh, rid,
  1074. c, in, out);
  1075. break;
  1076. }
  1077. }
  1078. if (mux_master_handlers[i].handler == NULL) {
  1079. error("unsupported mux message 0x%08x", type);
  1080. reply_error(out, MUX_S_FAILURE, rid, "unsupported request");
  1081. ret = 0;
  1082. }
  1083. /* Enqueue reply packet */
  1084. if (sshbuf_len(out) != 0 &&
  1085. (r = sshbuf_put_stringb(c->output, out)) != 0)
  1086. fatal(r, "enqueue");
  1087. out:
  1088. sshbuf_free(in);
  1089. sshbuf_free(out);
  1090. return ret;
  1091. }
  1092. void
  1093. mux_exit_message(struct ssh *ssh, Channel *c, int exitval)
  1094. {
  1095. struct sshbuf *m;
  1096. Channel *mux_chan;
  1097. int r;
  1098. debug3("channel %d: exit message, exitval %d", c->self, exitval);
  1099. if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
  1100. fatal("channel %d missing mux %d", c->self, c->ctl_chan);
  1101. /* Append exit message packet to control socket output queue */
  1102. if ((m = sshbuf_new()) == NULL)
  1103. fatal("sshbuf_new");
  1104. if ((r = sshbuf_put_u32(m, MUX_S_EXIT_MESSAGE)) != 0 ||
  1105. (r = sshbuf_put_u32(m, c->self)) != 0 ||
  1106. (r = sshbuf_put_u32(m, exitval)) != 0 ||
  1107. (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
  1108. fatal(r, "reply");
  1109. sshbuf_free(m);
  1110. }
  1111. void
  1112. mux_tty_alloc_failed(struct ssh *ssh, Channel *c)
  1113. {
  1114. struct sshbuf *m;
  1115. Channel *mux_chan;
  1116. int r;
  1117. debug3("channel %d: TTY alloc failed", c->self);
  1118. if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
  1119. fatal("channel %d missing mux %d", c->self, c->ctl_chan);
  1120. /* Append exit message packet to control socket output queue */
  1121. if ((m = sshbuf_new()) == NULL)
  1122. fatal("sshbuf_new");
  1123. if ((r = sshbuf_put_u32(m, MUX_S_TTY_ALLOC_FAIL)) != 0 ||
  1124. (r = sshbuf_put_u32(m, c->self)) != 0 ||
  1125. (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
  1126. fatal(r, "reply");
  1127. sshbuf_free(m);
  1128. }
  1129. /* Prepare a mux master to listen on a Unix domain socket. */
  1130. void
  1131. muxserver_listen(struct ssh *ssh)
  1132. {
  1133. mode_t old_umask;
  1134. char *orig_control_path = options.control_path;
  1135. char rbuf[16+1];
  1136. u_int i, r;
  1137. int oerrno;
  1138. if (options.control_path == NULL ||
  1139. options.control_master == SSHCTL_MASTER_NO)
  1140. return;
  1141. debug("setting up multiplex master socket");
  1142. /*
  1143. * Use a temporary path before listen so we can pseudo-atomically
  1144. * establish the listening socket in its final location to avoid
  1145. * other processes racing in between bind() and listen() and hitting
  1146. * an unready socket.
  1147. */
  1148. for (i = 0; i < sizeof(rbuf) - 1; i++) {
  1149. r = arc4random_uniform(26+26+10);
  1150. rbuf[i] = (r < 26) ? 'a' + r :
  1151. (r < 26*2) ? 'A' + r - 26 :
  1152. '0' + r - 26 - 26;
  1153. }
  1154. rbuf[sizeof(rbuf) - 1] = '\0';
  1155. options.control_path = NULL;
  1156. xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
  1157. debug3("temporary control path %s", options.control_path);
  1158. old_umask = umask(0177);
  1159. muxserver_sock = unix_listener(options.control_path, 64, 0);
  1160. oerrno = errno;
  1161. umask(old_umask);
  1162. if (muxserver_sock < 0) {
  1163. if (oerrno == EINVAL || oerrno == EADDRINUSE) {
  1164. error("ControlSocket %s already exists, "
  1165. "disabling multiplexing", options.control_path);
  1166. disable_mux_master:
  1167. if (muxserver_sock != -1) {
  1168. close(muxserver_sock);
  1169. muxserver_sock = -1;
  1170. }
  1171. free(orig_control_path);
  1172. free(options.control_path);
  1173. options.control_path = NULL;
  1174. options.control_master = SSHCTL_MASTER_NO;
  1175. return;
  1176. } else {
  1177. /* unix_listener() logs the error */
  1178. cleanup_exit(255);
  1179. }
  1180. }
  1181. /* Now atomically "move" the mux socket into position */
  1182. if (link(options.control_path, orig_control_path) != 0) {
  1183. if (errno != EEXIST) {
  1184. fatal("link mux listener %s => %s: %s",
  1185. options.control_path, orig_control_path,
  1186. strerror(errno));
  1187. }
  1188. error("ControlSocket %s already exists, disabling multiplexing",
  1189. orig_control_path);
  1190. unlink(options.control_path);
  1191. goto disable_mux_master;
  1192. }
  1193. unlink(options.control_path);
  1194. free(options.control_path);
  1195. options.control_path = orig_control_path;
  1196. set_nonblock(muxserver_sock);
  1197. mux_listener_channel = channel_new(ssh, "mux listener",
  1198. SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
  1199. CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
  1200. 0, options.control_path, 1);
  1201. mux_listener_channel->mux_rcb = mux_master_read_cb;
  1202. debug3("mux listener channel %d fd %d",
  1203. mux_listener_channel->self, mux_listener_channel->sock);
  1204. }
  1205. /* Callback on open confirmation in mux master for a mux client session. */
  1206. static void
  1207. mux_session_confirm(struct ssh *ssh, int id, int success, void *arg)
  1208. {
  1209. struct mux_session_confirm_ctx *cctx = arg;
  1210. const char *display;
  1211. Channel *c, *cc;
  1212. int i, r;
  1213. struct sshbuf *reply;
  1214. if (cctx == NULL)
  1215. fatal("cctx == NULL");
  1216. if ((c = channel_by_id(ssh, id)) == NULL)
  1217. fatal("no channel for id %d", id);
  1218. if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
  1219. fatal("channel %d lacks control channel %d",
  1220. id, c->ctl_chan);
  1221. if ((reply = sshbuf_new()) == NULL)
  1222. fatal("sshbuf_new");
  1223. if (!success) {
  1224. debug3("sending failure reply");
  1225. reply_error(reply, MUX_S_FAILURE, cctx->rid,
  1226. "Session open refused by peer");
  1227. goto done;
  1228. }
  1229. display = getenv("DISPLAY");
  1230. if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
  1231. char *proto, *data;
  1232. /* Get reasonable local authentication information. */
  1233. if (client_x11_get_proto(ssh, display, options.xauth_location,
  1234. options.forward_x11_trusted, options.forward_x11_timeout,
  1235. &proto, &data) == 0) {
  1236. /* Request forwarding with authentication spoofing. */
  1237. debug("Requesting X11 forwarding with authentication "
  1238. "spoofing.");
  1239. x11_request_forwarding_with_spoofing(ssh, id,
  1240. display, proto, data, 1);
  1241. /* XXX exit_on_forward_failure */
  1242. client_expect_confirm(ssh, id, "X11 forwarding",
  1243. CONFIRM_WARN);
  1244. }
  1245. }
  1246. if (cctx->want_agent_fwd && options.forward_agent) {
  1247. debug("Requesting authentication agent forwarding.");
  1248. channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
  1249. if ((r = sshpkt_send(ssh)) != 0)
  1250. fatal(r, "send");
  1251. }
  1252. client_session2_setup(ssh, id, cctx->want_tty, cctx->want_subsys,
  1253. cctx->term, &cctx->tio, c->rfd, cctx->cmd, cctx->env);
  1254. debug3("sending success reply");
  1255. /* prepare reply */
  1256. if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
  1257. (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
  1258. (r = sshbuf_put_u32(reply, c->self)) != 0)
  1259. fatal(r, "reply");
  1260. done:
  1261. /* Send reply */
  1262. if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
  1263. fatal(r, "enqueue");
  1264. sshbuf_free(reply);
  1265. if (cc->mux_pause <= 0)
  1266. fatal("mux_pause %d", cc->mux_pause);
  1267. cc->mux_pause = 0; /* start processing messages again */
  1268. c->open_confirm_ctx = NULL;
  1269. sshbuf_free(cctx->cmd);
  1270. free(cctx->term);
  1271. if (cctx->env != NULL) {
  1272. for (i = 0; cctx->env[i] != NULL; i++)
  1273. free(cctx->env[i]);
  1274. free(cctx->env);
  1275. }
  1276. free(cctx);
  1277. }
  1278. /* ** Multiplexing client support */
  1279. /* Exit signal handler */
  1280. static void
  1281. control_client_sighandler(int signo)
  1282. {
  1283. muxclient_terminate = signo;
  1284. }
  1285. /*
  1286. * Relay signal handler - used to pass some signals from mux client to
  1287. * mux master.
  1288. */
  1289. static void
  1290. control_client_sigrelay(int signo)
  1291. {
  1292. int save_errno = errno;
  1293. if (muxserver_pid > 1)
  1294. kill(muxserver_pid, signo);
  1295. errno = save_errno;
  1296. }
  1297. static int
  1298. mux_client_read(int fd, struct sshbuf *b, size_t need)
  1299. {
  1300. size_t have;
  1301. ssize_t len;
  1302. u_char *p;
  1303. struct pollfd pfd;
  1304. int r;
  1305. pfd.fd = fd;
  1306. pfd.events = POLLIN;
  1307. if ((r = sshbuf_reserve(b, need, &p)) != 0)
  1308. fatal(r, "reserve");
  1309. for (have = 0; have < need; ) {
  1310. if (muxclient_terminate) {
  1311. errno = EINTR;
  1312. return -1;
  1313. }
  1314. len = read(fd, p + have, need - have);
  1315. if (len == -1) {
  1316. switch (errno) {
  1317. #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
  1318. case EWOULDBLOCK:
  1319. #endif
  1320. case EAGAIN:
  1321. (void)poll(&pfd, 1, -1);
  1322. /* FALLTHROUGH */
  1323. case EINTR:
  1324. continue;
  1325. default:
  1326. return -1;
  1327. }
  1328. }
  1329. if (len == 0) {
  1330. errno = EPIPE;
  1331. return -1;
  1332. }
  1333. have += (size_t)len;
  1334. }
  1335. return 0;
  1336. }
  1337. static int
  1338. mux_client_write_packet(int fd, struct sshbuf *m)
  1339. {
  1340. struct sshbuf *queue;
  1341. u_int have, need;
  1342. int r, oerrno, len;
  1343. const u_char *ptr;
  1344. struct pollfd pfd;
  1345. pfd.fd = fd;
  1346. pfd.events = POLLOUT;
  1347. if ((queue = sshbuf_new()) == NULL)
  1348. fatal("sshbuf_new");
  1349. if ((r = sshbuf_put_stringb(queue, m)) != 0)
  1350. fatal(r, "enqueue");
  1351. need = sshbuf_len(queue);
  1352. ptr = sshbuf_ptr(queue);
  1353. for (have = 0; have < need; ) {
  1354. if (muxclient_terminate) {
  1355. sshbuf_free(queue);
  1356. errno = EINTR;
  1357. return -1;
  1358. }
  1359. len = write(fd, ptr + have, need - have);
  1360. if (len == -1) {
  1361. switch (errno) {
  1362. #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
  1363. case EWOULDBLOCK:
  1364. #endif
  1365. case EAGAIN:
  1366. (void)poll(&pfd, 1, -1);
  1367. /* FALLTHROUGH */
  1368. case EINTR:
  1369. continue;
  1370. default:
  1371. oerrno = errno;
  1372. sshbuf_free(queue);
  1373. errno = oerrno;
  1374. return -1;
  1375. }
  1376. }
  1377. if (len == 0) {
  1378. sshbuf_free(queue);
  1379. errno = EPIPE;
  1380. return -1;
  1381. }
  1382. have += (u_int)len;
  1383. }
  1384. sshbuf_free(queue);
  1385. return 0;
  1386. }
  1387. static int
  1388. mux_client_read_packet(int fd, struct sshbuf *m)
  1389. {
  1390. struct sshbuf *queue;
  1391. size_t need, have;
  1392. const u_char *ptr;
  1393. int r, oerrno;
  1394. if ((queue = sshbuf_new()) == NULL)
  1395. fatal("sshbuf_new");
  1396. if (mux_client_read(fd, queue, 4) != 0) {
  1397. if ((oerrno = errno) == EPIPE)
  1398. debug3("read header failed: %s",
  1399. strerror(errno));
  1400. sshbuf_free(queue);
  1401. errno = oerrno;
  1402. return -1;
  1403. }
  1404. need = PEEK_U32(sshbuf_ptr(queue));
  1405. if (mux_client_read(fd, queue, need) != 0) {
  1406. oerrno = errno;
  1407. debug3("read body failed: %s", strerror(errno));
  1408. sshbuf_free(queue);
  1409. errno = oerrno;
  1410. return -1;
  1411. }
  1412. if ((r = sshbuf_get_string_direct(queue, &ptr, &have)) != 0 ||
  1413. (r = sshbuf_put(m, ptr, have)) != 0)
  1414. fatal(r, "dequeue");
  1415. sshbuf_free(queue);
  1416. return 0;
  1417. }
  1418. static int
  1419. mux_client_hello_exchange(int fd)
  1420. {
  1421. struct sshbuf *m;
  1422. u_int type, ver;
  1423. int r, ret = -1;
  1424. if ((m = sshbuf_new()) == NULL)
  1425. fatal("sshbuf_new");
  1426. if ((r = sshbuf_put_u32(m, MUX_MSG_HELLO)) != 0 ||
  1427. (r = sshbuf_put_u32(m, SSHMUX_VER)) != 0)
  1428. fatal(r, "assemble hello");
  1429. /* no extensions */
  1430. if (mux_client_write_packet(fd, m) != 0) {
  1431. debug("write packet: %s", strerror(errno));
  1432. goto out;
  1433. }
  1434. sshbuf_reset(m);
  1435. /* Read their HELLO */
  1436. if (mux_client_read_packet(fd, m) != 0) {
  1437. debug("read packet failed");
  1438. goto out;
  1439. }
  1440. if ((r = sshbuf_get_u32(m, &type)) != 0)
  1441. fatal(r, "parse type");
  1442. if (type != MUX_MSG_HELLO) {
  1443. error("expected HELLO (%u) got %u", MUX_MSG_HELLO, type);
  1444. goto out;
  1445. }
  1446. if ((r = sshbuf_get_u32(m, &ver)) != 0)
  1447. fatal(r, "parse version");
  1448. if (ver != SSHMUX_VER) {
  1449. error("Unsupported multiplexing protocol version %d "
  1450. "(expected %d)", ver, SSHMUX_VER);
  1451. goto out;
  1452. }
  1453. debug2("master version %u", ver);
  1454. /* No extensions are presently defined */
  1455. while (sshbuf_len(m) > 0) {
  1456. char *name = NULL;
  1457. if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
  1458. (r = sshbuf_skip_string(m)) != 0) { /* value */
  1459. error(r, "parse extension");
  1460. goto out;
  1461. }
  1462. debug2("Unrecognised master extension \"%s\"", name);
  1463. free(name);
  1464. }
  1465. /* success */
  1466. ret = 0;
  1467. out:
  1468. sshbuf_free(m);
  1469. return ret;
  1470. }
  1471. static u_int
  1472. mux_client_request_alive(int fd)
  1473. {
  1474. struct sshbuf *m;
  1475. char *e;
  1476. u_int pid, type, rid;
  1477. int r;
  1478. debug3("entering");
  1479. if ((m = sshbuf_new()) == NULL)
  1480. fatal("sshbuf_new");
  1481. if ((r = sshbuf_put_u32(m, MUX_C_ALIVE_CHECK)) != 0 ||
  1482. (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
  1483. fatal(r, "assemble");
  1484. if (mux_client_write_packet(fd, m) != 0)
  1485. fatal("write packet: %s", strerror(errno));
  1486. sshbuf_reset(m);
  1487. /* Read their reply */
  1488. if (mux_client_read_packet(fd, m) != 0) {
  1489. sshbuf_free(m);
  1490. return 0;
  1491. }
  1492. if ((r = sshbuf_get_u32(m, &type)) != 0)
  1493. fatal(r, "parse type");
  1494. if (type != MUX_S_ALIVE) {
  1495. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1496. fatal(r, "parse error message");
  1497. fatal("master returned error: %s", e);
  1498. }
  1499. if ((r = sshbuf_get_u32(m, &rid)) != 0)
  1500. fatal(r, "parse remote ID");
  1501. if (rid != muxclient_request_id)
  1502. fatal("out of sequence reply: my id %u theirs %u",
  1503. muxclient_request_id, rid);
  1504. if ((r = sshbuf_get_u32(m, &pid)) != 0)
  1505. fatal(r, "parse PID");
  1506. sshbuf_free(m);
  1507. debug3("done pid = %u", pid);
  1508. muxclient_request_id++;
  1509. return pid;
  1510. }
  1511. static void
  1512. mux_client_request_terminate(int fd)
  1513. {
  1514. struct sshbuf *m;
  1515. char *e;
  1516. u_int type, rid;
  1517. int r;
  1518. debug3("entering");
  1519. if ((m = sshbuf_new()) == NULL)
  1520. fatal("sshbuf_new");
  1521. if ((r = sshbuf_put_u32(m, MUX_C_TERMINATE)) != 0 ||
  1522. (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
  1523. fatal(r, "request");
  1524. if (mux_client_write_packet(fd, m) != 0)
  1525. fatal("write packet: %s", strerror(errno));
  1526. sshbuf_reset(m);
  1527. /* Read their reply */
  1528. if (mux_client_read_packet(fd, m) != 0) {
  1529. /* Remote end exited already */
  1530. if (errno == EPIPE) {
  1531. sshbuf_free(m);
  1532. return;
  1533. }
  1534. fatal("read from master failed: %s", strerror(errno));
  1535. }
  1536. if ((r = sshbuf_get_u32(m, &type)) != 0 ||
  1537. (r = sshbuf_get_u32(m, &rid)) != 0)
  1538. fatal(r, "parse");
  1539. if (rid != muxclient_request_id)
  1540. fatal("out of sequence reply: my id %u theirs %u",
  1541. muxclient_request_id, rid);
  1542. switch (type) {
  1543. case MUX_S_OK:
  1544. break;
  1545. case MUX_S_PERMISSION_DENIED:
  1546. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1547. fatal(r, "parse error message");
  1548. fatal("Master refused termination request: %s", e);
  1549. case MUX_S_FAILURE:
  1550. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1551. fatal(r, "parse error message");
  1552. fatal("termination request failed: %s", e);
  1553. default:
  1554. fatal("unexpected response from master 0x%08x", type);
  1555. }
  1556. sshbuf_free(m);
  1557. muxclient_request_id++;
  1558. }
  1559. static int
  1560. mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd)
  1561. {
  1562. struct sshbuf *m;
  1563. char *e, *fwd_desc;
  1564. const char *lhost, *chost;
  1565. u_int type, rid;
  1566. int r;
  1567. fwd_desc = format_forward(ftype, fwd);
  1568. debug("Requesting %s %s",
  1569. cancel_flag ? "cancellation of" : "forwarding of", fwd_desc);
  1570. free(fwd_desc);
  1571. type = cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD;
  1572. if (fwd->listen_path != NULL)
  1573. lhost = fwd->listen_path;
  1574. else if (fwd->listen_host == NULL)
  1575. lhost = "";
  1576. else if (*fwd->listen_host == '\0')
  1577. lhost = "*";
  1578. else
  1579. lhost = fwd->listen_host;
  1580. if (fwd->connect_path != NULL)
  1581. chost = fwd->connect_path;
  1582. else if (fwd->connect_host == NULL)
  1583. chost = "";
  1584. else
  1585. chost = fwd->connect_host;
  1586. if ((m = sshbuf_new()) == NULL)
  1587. fatal("sshbuf_new");
  1588. if ((r = sshbuf_put_u32(m, type)) != 0 ||
  1589. (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
  1590. (r = sshbuf_put_u32(m, ftype)) != 0 ||
  1591. (r = sshbuf_put_cstring(m, lhost)) != 0 ||
  1592. (r = sshbuf_put_u32(m, fwd->listen_port)) != 0 ||
  1593. (r = sshbuf_put_cstring(m, chost)) != 0 ||
  1594. (r = sshbuf_put_u32(m, fwd->connect_port)) != 0)
  1595. fatal(r, "request");
  1596. if (mux_client_write_packet(fd, m) != 0)
  1597. fatal("write packet: %s", strerror(errno));
  1598. sshbuf_reset(m);
  1599. /* Read their reply */
  1600. if (mux_client_read_packet(fd, m) != 0) {
  1601. sshbuf_free(m);
  1602. return -1;
  1603. }
  1604. if ((r = sshbuf_get_u32(m, &type)) != 0 ||
  1605. (r = sshbuf_get_u32(m, &rid)) != 0)
  1606. fatal(r, "parse");
  1607. if (rid != muxclient_request_id)
  1608. fatal("out of sequence reply: my id %u theirs %u",
  1609. muxclient_request_id, rid);
  1610. switch (type) {
  1611. case MUX_S_OK:
  1612. break;
  1613. case MUX_S_REMOTE_PORT:
  1614. if (cancel_flag)
  1615. fatal("got MUX_S_REMOTE_PORT for cancel");
  1616. if ((r = sshbuf_get_u32(m, &fwd->allocated_port)) != 0)
  1617. fatal(r, "parse port");
  1618. verbose("Allocated port %u for remote forward to %s:%d",
  1619. fwd->allocated_port,
  1620. fwd->connect_host ? fwd->connect_host : "",
  1621. fwd->connect_port);
  1622. if (muxclient_command == SSHMUX_COMMAND_FORWARD)
  1623. fprintf(stdout, "%i\n", fwd->allocated_port);
  1624. break;
  1625. case MUX_S_PERMISSION_DENIED:
  1626. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1627. fatal(r, "parse error message");
  1628. sshbuf_free(m);
  1629. error("Master refused forwarding request: %s", e);
  1630. return -1;
  1631. case MUX_S_FAILURE:
  1632. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1633. fatal(r, "parse error message");
  1634. sshbuf_free(m);
  1635. error("forwarding request failed: %s", e);
  1636. return -1;
  1637. default:
  1638. fatal("unexpected response from master 0x%08x", type);
  1639. }
  1640. sshbuf_free(m);
  1641. muxclient_request_id++;
  1642. return 0;
  1643. }
  1644. static int
  1645. mux_client_forwards(int fd, int cancel_flag)
  1646. {
  1647. int i, ret = 0;
  1648. debug3("%s forwardings: %d local, %d remote",
  1649. cancel_flag ? "cancel" : "request",
  1650. options.num_local_forwards, options.num_remote_forwards);
  1651. /* XXX ExitOnForwardingFailure */
  1652. for (i = 0; i < options.num_local_forwards; i++) {
  1653. if (mux_client_forward(fd, cancel_flag,
  1654. options.local_forwards[i].connect_port == 0 ?
  1655. MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
  1656. options.local_forwards + i) != 0)
  1657. ret = -1;
  1658. }
  1659. for (i = 0; i < options.num_remote_forwards; i++) {
  1660. if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE,
  1661. options.remote_forwards + i) != 0)
  1662. ret = -1;
  1663. }
  1664. return ret;
  1665. }
  1666. static int
  1667. mux_client_request_session(int fd)
  1668. {
  1669. struct sshbuf *m;
  1670. char *e;
  1671. const char *term;
  1672. u_int echar, rid, sid, esid, exitval, type, exitval_seen;
  1673. extern char **environ;
  1674. int r, i, rawmode;
  1675. debug3("entering");
  1676. if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
  1677. error("master alive request failed");
  1678. return -1;
  1679. }
  1680. ssh_signal(SIGPIPE, SIG_IGN);
  1681. if (options.stdin_null && stdfd_devnull(1, 0, 0) == -1)
  1682. fatal("stdfd_devnull failed");
  1683. if ((term = getenv("TERM")) == NULL)
  1684. term = "";
  1685. echar = 0xffffffff;
  1686. if (options.escape_char != SSH_ESCAPECHAR_NONE)
  1687. echar = (u_int)options.escape_char;
  1688. if ((m = sshbuf_new()) == NULL)
  1689. fatal("sshbuf_new");
  1690. if ((r = sshbuf_put_u32(m, MUX_C_NEW_SESSION)) != 0 ||
  1691. (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
  1692. (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
  1693. (r = sshbuf_put_u32(m, tty_flag)) != 0 ||
  1694. (r = sshbuf_put_u32(m, options.forward_x11)) != 0 ||
  1695. (r = sshbuf_put_u32(m, options.forward_agent)) != 0 ||
  1696. (r = sshbuf_put_u32(m, subsystem_flag)) != 0 ||
  1697. (r = sshbuf_put_u32(m, echar)) != 0 ||
  1698. (r = sshbuf_put_cstring(m, term)) != 0 ||
  1699. (r = sshbuf_put_stringb(m, command)) != 0)
  1700. fatal(r, "request");
  1701. /* Pass environment */
  1702. if (options.num_send_env > 0 && environ != NULL) {
  1703. for (i = 0; environ[i] != NULL; i++) {
  1704. if (!env_permitted(environ[i]))
  1705. continue;
  1706. if ((r = sshbuf_put_cstring(m, environ[i])) != 0)
  1707. fatal(r, "request sendenv");
  1708. }
  1709. }
  1710. for (i = 0; i < options.num_setenv; i++) {
  1711. if ((r = sshbuf_put_cstring(m, options.setenv[i])) != 0)
  1712. fatal(r, "request setenv");
  1713. }
  1714. if (mux_client_write_packet(fd, m) != 0)
  1715. fatal("write packet: %s", strerror(errno));
  1716. /* Send the stdio file descriptors */
  1717. if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
  1718. mm_send_fd(fd, STDOUT_FILENO) == -1 ||
  1719. mm_send_fd(fd, STDERR_FILENO) == -1)
  1720. fatal("send fds failed");
  1721. debug3("session request sent");
  1722. /* Read their reply */
  1723. sshbuf_reset(m);
  1724. if (mux_client_read_packet(fd, m) != 0) {
  1725. error("read from master failed: %s", strerror(errno));
  1726. sshbuf_free(m);
  1727. return -1;
  1728. }
  1729. if ((r = sshbuf_get_u32(m, &type)) != 0 ||
  1730. (r = sshbuf_get_u32(m, &rid)) != 0)
  1731. fatal(r, "parse");
  1732. if (rid != muxclient_request_id)
  1733. fatal("out of sequence reply: my id %u theirs %u",
  1734. muxclient_request_id, rid);
  1735. switch (type) {
  1736. case MUX_S_SESSION_OPENED:
  1737. if ((r = sshbuf_get_u32(m, &sid)) != 0)
  1738. fatal(r, "parse session ID");
  1739. debug("master session id: %u", sid);
  1740. break;
  1741. case MUX_S_PERMISSION_DENIED:
  1742. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1743. fatal(r, "parse error message");
  1744. error("Master refused session request: %s", e);
  1745. sshbuf_free(m);
  1746. return -1;
  1747. case MUX_S_FAILURE:
  1748. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1749. fatal(r, "parse error message");
  1750. error("session request failed: %s", e);
  1751. sshbuf_free(m);
  1752. return -1;
  1753. default:
  1754. sshbuf_free(m);
  1755. error("unexpected response from master 0x%08x", type);
  1756. return -1;
  1757. }
  1758. muxclient_request_id++;
  1759. if (pledge("stdio proc tty", NULL) == -1)
  1760. fatal("pledge(): %s", strerror(errno));
  1761. platform_pledge_mux();
  1762. ssh_signal(SIGHUP, control_client_sighandler);
  1763. ssh_signal(SIGINT, control_client_sighandler);
  1764. ssh_signal(SIGTERM, control_client_sighandler);
  1765. ssh_signal(SIGWINCH, control_client_sigrelay);
  1766. rawmode = tty_flag;
  1767. if (tty_flag)
  1768. enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
  1769. /*
  1770. * Stick around until the controlee closes the client_fd.
  1771. * Before it does, it is expected to write an exit message.
  1772. * This process must read the value and wait for the closure of
  1773. * the client_fd; if this one closes early, the multiplex master will
  1774. * terminate early too (possibly losing data).
  1775. */
  1776. for (exitval = 255, exitval_seen = 0;;) {
  1777. sshbuf_reset(m);
  1778. if (mux_client_read_packet(fd, m) != 0)
  1779. break;
  1780. if ((r = sshbuf_get_u32(m, &type)) != 0)
  1781. fatal(r, "parse type");
  1782. switch (type) {
  1783. case MUX_S_TTY_ALLOC_FAIL:
  1784. if ((r = sshbuf_get_u32(m, &esid)) != 0)
  1785. fatal(r, "parse session ID");
  1786. if (esid != sid)
  1787. fatal("tty alloc fail on unknown session: "
  1788. "my id %u theirs %u", sid, esid);
  1789. leave_raw_mode(options.request_tty ==
  1790. REQUEST_TTY_FORCE);
  1791. rawmode = 0;
  1792. continue;
  1793. case MUX_S_EXIT_MESSAGE:
  1794. if ((r = sshbuf_get_u32(m, &esid)) != 0)
  1795. fatal(r, "parse session ID");
  1796. if (esid != sid)
  1797. fatal("exit on unknown session: "
  1798. "my id %u theirs %u", sid, esid);
  1799. if (exitval_seen)
  1800. fatal("exitval sent twice");
  1801. if ((r = sshbuf_get_u32(m, &exitval)) != 0)
  1802. fatal(r, "parse exitval");
  1803. exitval_seen = 1;
  1804. continue;
  1805. default:
  1806. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1807. fatal(r, "parse error message");
  1808. fatal("master returned error: %s", e);
  1809. }
  1810. }
  1811. close(fd);
  1812. if (rawmode)
  1813. leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
  1814. if (muxclient_terminate) {
  1815. debug2("Exiting on signal: %s", strsignal(muxclient_terminate));
  1816. exitval = 255;
  1817. } else if (!exitval_seen) {
  1818. debug2("Control master terminated unexpectedly");
  1819. exitval = 255;
  1820. } else
  1821. debug2("Received exit status from master %d", exitval);
  1822. if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
  1823. fprintf(stderr, "Shared connection to %s closed.\r\n", host);
  1824. exit(exitval);
  1825. }
  1826. static int
  1827. mux_client_proxy(int fd)
  1828. {
  1829. struct sshbuf *m;
  1830. char *e;
  1831. u_int type, rid;
  1832. int r;
  1833. if ((m = sshbuf_new()) == NULL)
  1834. fatal("sshbuf_new");
  1835. if ((r = sshbuf_put_u32(m, MUX_C_PROXY)) != 0 ||
  1836. (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
  1837. fatal(r, "request");
  1838. if (mux_client_write_packet(fd, m) != 0)
  1839. fatal("write packet: %s", strerror(errno));
  1840. sshbuf_reset(m);
  1841. /* Read their reply */
  1842. if (mux_client_read_packet(fd, m) != 0) {
  1843. sshbuf_free(m);
  1844. return 0;
  1845. }
  1846. if ((r = sshbuf_get_u32(m, &type)) != 0 ||
  1847. (r = sshbuf_get_u32(m, &rid)) != 0)
  1848. fatal(r, "parse");
  1849. if (rid != muxclient_request_id)
  1850. fatal("out of sequence reply: my id %u theirs %u",
  1851. muxclient_request_id, rid);
  1852. if (type != MUX_S_PROXY) {
  1853. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1854. fatal(r, "parse error message");
  1855. fatal("master returned error: %s", e);
  1856. }
  1857. sshbuf_free(m);
  1858. debug3("done");
  1859. muxclient_request_id++;
  1860. return 0;
  1861. }
  1862. static int
  1863. mux_client_request_stdio_fwd(int fd)
  1864. {
  1865. struct sshbuf *m;
  1866. char *e;
  1867. u_int type, rid, sid;
  1868. int r;
  1869. debug3("entering");
  1870. if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
  1871. error("master alive request failed");
  1872. return -1;
  1873. }
  1874. ssh_signal(SIGPIPE, SIG_IGN);
  1875. if (options.stdin_null && stdfd_devnull(1, 0, 0) == -1)
  1876. fatal("stdfd_devnull failed");
  1877. if ((m = sshbuf_new()) == NULL)
  1878. fatal("sshbuf_new");
  1879. if ((r = sshbuf_put_u32(m, MUX_C_NEW_STDIO_FWD)) != 0 ||
  1880. (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
  1881. (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
  1882. (r = sshbuf_put_cstring(m, options.stdio_forward_host)) != 0 ||
  1883. (r = sshbuf_put_u32(m, options.stdio_forward_port)) != 0)
  1884. fatal(r, "request");
  1885. if (mux_client_write_packet(fd, m) != 0)
  1886. fatal("write packet: %s", strerror(errno));
  1887. /* Send the stdio file descriptors */
  1888. if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
  1889. mm_send_fd(fd, STDOUT_FILENO) == -1)
  1890. fatal("send fds failed");
  1891. if (pledge("stdio proc tty", NULL) == -1)
  1892. fatal("pledge(): %s", strerror(errno));
  1893. platform_pledge_mux();
  1894. debug3("stdio forward request sent");
  1895. /* Read their reply */
  1896. sshbuf_reset(m);
  1897. if (mux_client_read_packet(fd, m) != 0) {
  1898. error("read from master failed: %s", strerror(errno));
  1899. sshbuf_free(m);
  1900. return -1;
  1901. }
  1902. if ((r = sshbuf_get_u32(m, &type)) != 0 ||
  1903. (r = sshbuf_get_u32(m, &rid)) != 0)
  1904. fatal(r, "parse");
  1905. if (rid != muxclient_request_id)
  1906. fatal("out of sequence reply: my id %u theirs %u",
  1907. muxclient_request_id, rid);
  1908. switch (type) {
  1909. case MUX_S_SESSION_OPENED:
  1910. if ((r = sshbuf_get_u32(m, &sid)) != 0)
  1911. fatal(r, "parse session ID");
  1912. debug("master session id: %u", sid);
  1913. break;
  1914. case MUX_S_PERMISSION_DENIED:
  1915. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1916. fatal(r, "parse error message");
  1917. sshbuf_free(m);
  1918. fatal("Master refused stdio forwarding request: %s", e);
  1919. case MUX_S_FAILURE:
  1920. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1921. fatal(r, "parse error message");
  1922. sshbuf_free(m);
  1923. fatal("Stdio forwarding request failed: %s", e);
  1924. default:
  1925. sshbuf_free(m);
  1926. error("unexpected response from master 0x%08x", type);
  1927. return -1;
  1928. }
  1929. muxclient_request_id++;
  1930. ssh_signal(SIGHUP, control_client_sighandler);
  1931. ssh_signal(SIGINT, control_client_sighandler);
  1932. ssh_signal(SIGTERM, control_client_sighandler);
  1933. ssh_signal(SIGWINCH, control_client_sigrelay);
  1934. /*
  1935. * Stick around until the controlee closes the client_fd.
  1936. */
  1937. sshbuf_reset(m);
  1938. if (mux_client_read_packet(fd, m) != 0) {
  1939. if (errno == EPIPE ||
  1940. (errno == EINTR && muxclient_terminate != 0))
  1941. return 0;
  1942. fatal("mux_client_read_packet: %s", strerror(errno));
  1943. }
  1944. fatal("master returned unexpected message %u", type);
  1945. }
  1946. static void
  1947. mux_client_request_stop_listening(int fd)
  1948. {
  1949. struct sshbuf *m;
  1950. char *e;
  1951. u_int type, rid;
  1952. int r;
  1953. debug3("entering");
  1954. if ((m = sshbuf_new()) == NULL)
  1955. fatal("sshbuf_new");
  1956. if ((r = sshbuf_put_u32(m, MUX_C_STOP_LISTENING)) != 0 ||
  1957. (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
  1958. fatal(r, "request");
  1959. if (mux_client_write_packet(fd, m) != 0)
  1960. fatal("write packet: %s", strerror(errno));
  1961. sshbuf_reset(m);
  1962. /* Read their reply */
  1963. if (mux_client_read_packet(fd, m) != 0)
  1964. fatal("read from master failed: %s", strerror(errno));
  1965. if ((r = sshbuf_get_u32(m, &type)) != 0 ||
  1966. (r = sshbuf_get_u32(m, &rid)) != 0)
  1967. fatal(r, "parse");
  1968. if (rid != muxclient_request_id)
  1969. fatal("out of sequence reply: my id %u theirs %u",
  1970. muxclient_request_id, rid);
  1971. switch (type) {
  1972. case MUX_S_OK:
  1973. break;
  1974. case MUX_S_PERMISSION_DENIED:
  1975. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1976. fatal(r, "parse error message");
  1977. fatal("Master refused stop listening request: %s", e);
  1978. case MUX_S_FAILURE:
  1979. if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
  1980. fatal(r, "parse error message");
  1981. fatal("stop listening request failed: %s", e);
  1982. default:
  1983. fatal("unexpected response from master 0x%08x", type);
  1984. }
  1985. sshbuf_free(m);
  1986. muxclient_request_id++;
  1987. }
  1988. /* Multiplex client main loop. */
  1989. int
  1990. muxclient(const char *path)
  1991. {
  1992. struct sockaddr_un addr;
  1993. int sock;
  1994. u_int pid;
  1995. if (muxclient_command == 0) {
  1996. if (options.stdio_forward_host != NULL)
  1997. muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
  1998. else
  1999. muxclient_command = SSHMUX_COMMAND_OPEN;
  2000. }
  2001. switch (options.control_master) {
  2002. case SSHCTL_MASTER_AUTO:
  2003. case SSHCTL_MASTER_AUTO_ASK:
  2004. debug("auto-mux: Trying existing master");
  2005. /* FALLTHROUGH */
  2006. case SSHCTL_MASTER_NO:
  2007. break;
  2008. default:
  2009. return -1;
  2010. }
  2011. memset(&addr, '\0', sizeof(addr));
  2012. addr.sun_family = AF_UNIX;
  2013. if (strlcpy(addr.sun_path, path,
  2014. sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
  2015. fatal("ControlPath too long ('%s' >= %u bytes)", path,
  2016. (unsigned int)sizeof(addr.sun_path));
  2017. if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
  2018. fatal("socket(): %s", strerror(errno));
  2019. if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
  2020. switch (muxclient_command) {
  2021. case SSHMUX_COMMAND_OPEN:
  2022. case SSHMUX_COMMAND_STDIO_FWD:
  2023. break;
  2024. default:
  2025. fatal("Control socket connect(%.100s): %s", path,
  2026. strerror(errno));
  2027. }
  2028. if (errno == ECONNREFUSED &&
  2029. options.control_master != SSHCTL_MASTER_NO) {
  2030. debug("Stale control socket %.100s, unlinking", path);
  2031. unlink(path);
  2032. } else if (errno == ENOENT) {
  2033. debug("Control socket \"%.100s\" does not exist", path);
  2034. } else {
  2035. error("Control socket connect(%.100s): %s", path,
  2036. strerror(errno));
  2037. }
  2038. close(sock);
  2039. return -1;
  2040. }
  2041. set_nonblock(sock);
  2042. if (mux_client_hello_exchange(sock) != 0) {
  2043. error("master hello exchange failed");
  2044. close(sock);
  2045. return -1;
  2046. }
  2047. switch (muxclient_command) {
  2048. case SSHMUX_COMMAND_ALIVE_CHECK:
  2049. if ((pid = mux_client_request_alive(sock)) == 0)
  2050. fatal("master alive check failed");
  2051. fprintf(stderr, "Master running (pid=%u)\r\n", pid);
  2052. exit(0);
  2053. case SSHMUX_COMMAND_TERMINATE:
  2054. mux_client_request_terminate(sock);
  2055. if (options.log_level != SYSLOG_LEVEL_QUIET)
  2056. fprintf(stderr, "Exit request sent.\r\n");
  2057. exit(0);
  2058. case SSHMUX_COMMAND_FORWARD:
  2059. if (mux_client_forwards(sock, 0) != 0)
  2060. fatal("master forward request failed");
  2061. exit(0);
  2062. case SSHMUX_COMMAND_OPEN:
  2063. if (mux_client_forwards(sock, 0) != 0) {
  2064. error("master forward request failed");
  2065. return -1;
  2066. }
  2067. mux_client_request_session(sock);
  2068. return -1;
  2069. case SSHMUX_COMMAND_STDIO_FWD:
  2070. mux_client_request_stdio_fwd(sock);
  2071. exit(0);
  2072. case SSHMUX_COMMAND_STOP:
  2073. mux_client_request_stop_listening(sock);
  2074. if (options.log_level != SYSLOG_LEVEL_QUIET)
  2075. fprintf(stderr, "Stop listening request sent.\r\n");
  2076. exit(0);
  2077. case SSHMUX_COMMAND_CANCEL_FWD:
  2078. if (mux_client_forwards(sock, 1) != 0)
  2079. error("master cancel forward request failed");
  2080. exit(0);
  2081. case SSHMUX_COMMAND_PROXY:
  2082. mux_client_proxy(sock);
  2083. return (sock);
  2084. default:
  2085. fatal("unrecognised muxclient_command %d", muxclient_command);
  2086. }
  2087. }