1 /* $OpenBSD: mux.c,v 1.77 2018/09/26 07:32:44 djm Exp $ */
3 * Copyright (c) 2002-2008 Damien Miller <djm@openbsd.org>
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.
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.
18 /* ssh session multiplexing support */
23 #include <sys/types.h>
25 #include <sys/socket.h>
44 # ifdef HAVE_SYS_POLL_H
45 # include <sys/poll.h>
53 #include "openbsd-compat/sys-queue.h"
58 #include "pathnames.h"
65 #include "monitor_fdpass.h"
69 #include "clientloop.h"
74 extern Options options;
75 extern int stdin_null_flag;
77 extern int subsystem_flag;
78 extern struct sshbuf *command;
79 extern volatile sig_atomic_t quit_pending;
81 /* Context for session open confirmation callback */
82 struct mux_session_confirm_ctx {
94 /* Context for stdio fwd open confirmation callback */
95 struct mux_stdio_confirm_ctx {
99 /* Context for global channel callback */
100 struct mux_channel_confirm_ctx {
101 u_int cid; /* channel id */
102 u_int rid; /* request id */
103 int fid; /* forward id */
106 /* fd to control socket */
107 int muxserver_sock = -1;
109 /* client request id */
110 u_int muxclient_request_id = 0;
112 /* Multiplexing control command */
113 u_int muxclient_command = 0;
115 /* Set when signalled. */
116 static volatile sig_atomic_t muxclient_terminate = 0;
118 /* PID of multiplex server */
119 static u_int muxserver_pid = 0;
121 static Channel *mux_listener_channel = NULL;
123 struct mux_master_state {
127 /* mux protocol messages */
128 #define MUX_MSG_HELLO 0x00000001
129 #define MUX_C_NEW_SESSION 0x10000002
130 #define MUX_C_ALIVE_CHECK 0x10000004
131 #define MUX_C_TERMINATE 0x10000005
132 #define MUX_C_OPEN_FWD 0x10000006
133 #define MUX_C_CLOSE_FWD 0x10000007
134 #define MUX_C_NEW_STDIO_FWD 0x10000008
135 #define MUX_C_STOP_LISTENING 0x10000009
136 #define MUX_C_PROXY 0x1000000f
137 #define MUX_S_OK 0x80000001
138 #define MUX_S_PERMISSION_DENIED 0x80000002
139 #define MUX_S_FAILURE 0x80000003
140 #define MUX_S_EXIT_MESSAGE 0x80000004
141 #define MUX_S_ALIVE 0x80000005
142 #define MUX_S_SESSION_OPENED 0x80000006
143 #define MUX_S_REMOTE_PORT 0x80000007
144 #define MUX_S_TTY_ALLOC_FAIL 0x80000008
145 #define MUX_S_PROXY 0x8000000f
147 /* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
148 #define MUX_FWD_LOCAL 1
149 #define MUX_FWD_REMOTE 2
150 #define MUX_FWD_DYNAMIC 3
152 static void mux_session_confirm(struct ssh *, int, int, void *);
153 static void mux_stdio_confirm(struct ssh *, int, int, void *);
155 static int mux_master_process_hello(struct ssh *, u_int,
156 Channel *, struct sshbuf *, struct sshbuf *);
157 static int mux_master_process_new_session(struct ssh *, u_int,
158 Channel *, struct sshbuf *, struct sshbuf *);
159 static int mux_master_process_alive_check(struct ssh *, u_int,
160 Channel *, struct sshbuf *, struct sshbuf *);
161 static int mux_master_process_terminate(struct ssh *, u_int,
162 Channel *, struct sshbuf *, struct sshbuf *);
163 static int mux_master_process_open_fwd(struct ssh *, u_int,
164 Channel *, struct sshbuf *, struct sshbuf *);
165 static int mux_master_process_close_fwd(struct ssh *, u_int,
166 Channel *, struct sshbuf *, struct sshbuf *);
167 static int mux_master_process_stdio_fwd(struct ssh *, u_int,
168 Channel *, struct sshbuf *, struct sshbuf *);
169 static int mux_master_process_stop_listening(struct ssh *, u_int,
170 Channel *, struct sshbuf *, struct sshbuf *);
171 static int mux_master_process_proxy(struct ssh *, u_int,
172 Channel *, struct sshbuf *, struct sshbuf *);
174 static const struct {
176 int (*handler)(struct ssh *, u_int, Channel *,
177 struct sshbuf *, struct sshbuf *);
178 } mux_master_handlers[] = {
179 { MUX_MSG_HELLO, mux_master_process_hello },
180 { MUX_C_NEW_SESSION, mux_master_process_new_session },
181 { MUX_C_ALIVE_CHECK, mux_master_process_alive_check },
182 { MUX_C_TERMINATE, mux_master_process_terminate },
183 { MUX_C_OPEN_FWD, mux_master_process_open_fwd },
184 { MUX_C_CLOSE_FWD, mux_master_process_close_fwd },
185 { MUX_C_NEW_STDIO_FWD, mux_master_process_stdio_fwd },
186 { MUX_C_STOP_LISTENING, mux_master_process_stop_listening },
187 { MUX_C_PROXY, mux_master_process_proxy },
191 /* Cleanup callback fired on closure of mux slave _session_ channel */
194 mux_master_session_cleanup_cb(struct ssh *ssh, int cid, void *unused)
196 Channel *cc, *c = channel_by_id(ssh, cid);
198 debug3("%s: entering for channel %d", __func__, cid);
200 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
201 if (c->ctl_chan != -1) {
202 if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
203 fatal("%s: channel %d missing control channel %d",
204 __func__, c->self, c->ctl_chan);
207 cc->have_remote_id = 0;
208 chan_rcvd_oclose(ssh, cc);
210 channel_cancel_cleanup(ssh, c->self);
213 /* Cleanup callback fired on closure of mux slave _control_ channel */
216 mux_master_control_cleanup_cb(struct ssh *ssh, int cid, void *unused)
218 Channel *sc, *c = channel_by_id(ssh, cid);
220 debug3("%s: entering for channel %d", __func__, cid);
222 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
223 if (c->have_remote_id) {
224 if ((sc = channel_by_id(ssh, c->remote_id)) == NULL)
225 fatal("%s: channel %d missing session channel %u",
226 __func__, c->self, c->remote_id);
228 c->have_remote_id = 0;
230 if (sc->type != SSH_CHANNEL_OPEN &&
231 sc->type != SSH_CHANNEL_OPENING) {
232 debug2("%s: channel %d: not open", __func__, sc->self);
233 chan_mark_dead(ssh, sc);
235 if (sc->istate == CHAN_INPUT_OPEN)
236 chan_read_failed(ssh, sc);
237 if (sc->ostate == CHAN_OUTPUT_OPEN)
238 chan_write_failed(ssh, sc);
241 channel_cancel_cleanup(ssh, c->self);
244 /* Check mux client environment variables before passing them to mux master. */
246 env_permitted(char *env)
249 char name[1024], *cp;
251 if ((cp = strchr(env, '=')) == NULL || cp == env)
253 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
254 if (ret <= 0 || (size_t)ret >= sizeof(name)) {
255 error("%s: name '%.100s...' too long", __func__, env);
259 for (i = 0; i < options.num_send_env; i++)
260 if (match_pattern(name, options.send_env[i]))
266 /* Mux master protocol message handlers */
269 mux_master_process_hello(struct ssh *ssh, u_int rid,
270 Channel *c, struct sshbuf *m, struct sshbuf *reply)
273 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
277 fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self);
278 if (state->hello_rcvd) {
279 error("%s: HELLO received twice", __func__);
282 if ((r = sshbuf_get_u32(m, &ver)) != 0) {
283 error("%s: malformed message: %s", __func__, ssh_err(r));
286 if (ver != SSHMUX_VER) {
287 error("%s: unsupported multiplexing protocol version %u "
288 "(expected %u)", __func__, ver, SSHMUX_VER);
291 debug2("%s: channel %d slave version %u", __func__, c->self, ver);
293 /* No extensions are presently defined */
294 while (sshbuf_len(m) > 0) {
296 size_t value_len = 0;
298 if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
299 (r = sshbuf_get_string_direct(m, NULL, &value_len)) != 0) {
300 error("%s: malformed extension: %s",
301 __func__, ssh_err(r));
304 debug2("%s: Unrecognised extension \"%s\" length %zu",
305 __func__, name, value_len);
308 state->hello_rcvd = 1;
312 /* Enqueue a "ok" response to the reply buffer */
314 reply_ok(struct sshbuf *reply, u_int rid)
318 if ((r = sshbuf_put_u32(reply, MUX_S_OK)) != 0 ||
319 (r = sshbuf_put_u32(reply, rid)) != 0)
320 fatal("%s: reply: %s", __func__, ssh_err(r));
323 /* Enqueue an error response to the reply buffer */
325 reply_error(struct sshbuf *reply, u_int type, u_int rid, const char *msg)
329 if ((r = sshbuf_put_u32(reply, type)) != 0 ||
330 (r = sshbuf_put_u32(reply, rid)) != 0 ||
331 (r = sshbuf_put_cstring(reply, msg)) != 0)
332 fatal("%s: reply: %s", __func__, ssh_err(r));
336 mux_master_process_new_session(struct ssh *ssh, u_int rid,
337 Channel *c, struct sshbuf *m, struct sshbuf *reply)
340 struct mux_session_confirm_ctx *cctx;
342 u_int i, j, env_len, escape_char, window, packetmax;
345 /* Reply for SSHMUX_COMMAND_OPEN */
346 cctx = xcalloc(1, sizeof(*cctx));
352 if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
353 (r = sshbuf_get_u32(m, &cctx->want_tty)) != 0 ||
354 (r = sshbuf_get_u32(m, &cctx->want_x_fwd)) != 0 ||
355 (r = sshbuf_get_u32(m, &cctx->want_agent_fwd)) != 0 ||
356 (r = sshbuf_get_u32(m, &cctx->want_subsys)) != 0 ||
357 (r = sshbuf_get_u32(m, &escape_char)) != 0 ||
358 (r = sshbuf_get_cstring(m, &cctx->term, NULL)) != 0 ||
359 (r = sshbuf_get_cstring(m, &cmd, NULL)) != 0) {
362 for (j = 0; j < env_len; j++)
367 error("%s: malformed message", __func__);
371 #define MUX_MAX_ENV_VARS 4096
372 while (sshbuf_len(m) > 0) {
373 if ((r = sshbuf_get_cstring(m, &cp, NULL)) != 0)
375 if (!env_permitted(cp)) {
379 cctx->env = xreallocarray(cctx->env, env_len + 2,
381 cctx->env[env_len++] = cp;
382 cctx->env[env_len] = NULL;
383 if (env_len > MUX_MAX_ENV_VARS) {
384 error("%s: >%d environment variables received, "
385 "ignoring additional", __func__, MUX_MAX_ENV_VARS);
390 debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
391 "term \"%s\", cmd \"%s\", env %u", __func__, c->self,
392 cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
393 cctx->want_subsys, cctx->term, cmd, env_len);
395 if ((cctx->cmd = sshbuf_new()) == NULL)
396 fatal("%s: sshbuf_new", __func__);
397 if ((r = sshbuf_put(cctx->cmd, cmd, strlen(cmd))) != 0)
398 fatal("%s: sshbuf_put: %s", __func__, ssh_err(r));
402 /* Gather fds from client */
403 for(i = 0; i < 3; i++) {
404 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
405 error("%s: failed to receive fd %d from slave",
407 for (j = 0; j < i; j++)
409 for (j = 0; j < env_len; j++)
413 sshbuf_free(cctx->cmd);
415 reply_error(reply, MUX_S_FAILURE, rid,
416 "did not receive file descriptors");
421 debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
422 new_fd[0], new_fd[1], new_fd[2]);
424 /* XXX support multiple child sessions in future */
425 if (c->have_remote_id) {
426 debug2("%s: session already open", __func__);
427 reply_error(reply, MUX_S_FAILURE, rid,
428 "Multiple sessions not supported");
435 for (i = 0; i < env_len; i++)
439 sshbuf_free(cctx->cmd);
444 if (options.control_master == SSHCTL_MASTER_ASK ||
445 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
446 if (!ask_permission("Allow shared connection to %s? ", host)) {
447 debug2("%s: session refused by user", __func__);
448 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
449 "Permission denied");
454 /* Try to pick up ttymodes from client before it goes raw */
455 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
456 error("%s: tcgetattr: %s", __func__, strerror(errno));
458 /* enable nonblocking unless tty */
459 if (!isatty(new_fd[0]))
460 set_nonblock(new_fd[0]);
461 if (!isatty(new_fd[1]))
462 set_nonblock(new_fd[1]);
463 if (!isatty(new_fd[2]))
464 set_nonblock(new_fd[2]);
466 window = CHAN_SES_WINDOW_DEFAULT;
467 packetmax = CHAN_SES_PACKET_DEFAULT;
468 if (cctx->want_tty) {
473 nc = channel_new(ssh, "session", SSH_CHANNEL_OPENING,
474 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
475 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
477 nc->ctl_chan = c->self; /* link session -> control channel */
478 c->remote_id = nc->self; /* link control -> session channel */
479 c->have_remote_id = 1;
481 if (cctx->want_tty && escape_char != 0xffffffff) {
482 channel_register_filter(ssh, nc->self,
483 client_simple_escape_filter, NULL,
484 client_filter_cleanup,
485 client_new_escape_filter_ctx((int)escape_char));
488 debug2("%s: channel_new: %d linked to control channel %d",
489 __func__, nc->self, nc->ctl_chan);
491 channel_send_open(ssh, nc->self);
492 channel_register_open_confirm(ssh, nc->self, mux_session_confirm, cctx);
493 c->mux_pause = 1; /* stop handling messages until open_confirm done */
494 channel_register_cleanup(ssh, nc->self,
495 mux_master_session_cleanup_cb, 1);
497 /* reply is deferred, sent by mux_session_confirm */
502 mux_master_process_alive_check(struct ssh *ssh, u_int rid,
503 Channel *c, struct sshbuf *m, struct sshbuf *reply)
507 debug2("%s: channel %d: alive check", __func__, c->self);
510 if ((r = sshbuf_put_u32(reply, MUX_S_ALIVE)) != 0 ||
511 (r = sshbuf_put_u32(reply, rid)) != 0 ||
512 (r = sshbuf_put_u32(reply, (u_int)getpid())) != 0)
513 fatal("%s: reply: %s", __func__, ssh_err(r));
519 mux_master_process_terminate(struct ssh *ssh, u_int rid,
520 Channel *c, struct sshbuf *m, struct sshbuf *reply)
522 debug2("%s: channel %d: terminate request", __func__, c->self);
524 if (options.control_master == SSHCTL_MASTER_ASK ||
525 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
526 if (!ask_permission("Terminate shared connection to %s? ",
528 debug2("%s: termination refused by user", __func__);
529 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
530 "Permission denied");
536 reply_ok(reply, rid);
537 /* XXX exit happens too soon - message never makes it to client */
542 format_forward(u_int ftype, struct Forward *fwd)
548 xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
549 (fwd->listen_path != NULL) ? fwd->listen_path :
550 (fwd->listen_host == NULL) ?
551 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
552 fwd->listen_host, fwd->listen_port,
553 (fwd->connect_path != NULL) ? fwd->connect_path :
554 fwd->connect_host, fwd->connect_port);
556 case MUX_FWD_DYNAMIC:
557 xasprintf(&ret, "dynamic forward %.200s:%d -> *",
558 (fwd->listen_host == NULL) ?
559 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
560 fwd->listen_host, fwd->listen_port);
563 xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
564 (fwd->listen_path != NULL) ? fwd->listen_path :
565 (fwd->listen_host == NULL) ?
566 "LOCALHOST" : fwd->listen_host,
568 (fwd->connect_path != NULL) ? fwd->connect_path :
569 fwd->connect_host, fwd->connect_port);
572 fatal("%s: unknown forward type %u", __func__, ftype);
578 compare_host(const char *a, const char *b)
580 if (a == NULL && b == NULL)
582 if (a == NULL || b == NULL)
584 return strcmp(a, b) == 0;
588 compare_forward(struct Forward *a, struct Forward *b)
590 if (!compare_host(a->listen_host, b->listen_host))
592 if (!compare_host(a->listen_path, b->listen_path))
594 if (a->listen_port != b->listen_port)
596 if (!compare_host(a->connect_host, b->connect_host))
598 if (!compare_host(a->connect_path, b->connect_path))
600 if (a->connect_port != b->connect_port)
607 mux_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
609 struct mux_channel_confirm_ctx *fctx = ctxt;
610 char *failmsg = NULL;
611 struct Forward *rfwd;
616 if ((c = channel_by_id(ssh, fctx->cid)) == NULL) {
617 /* no channel for reply */
618 error("%s: unknown channel", __func__);
621 if ((out = sshbuf_new()) == NULL)
622 fatal("%s: sshbuf_new", __func__);
623 if (fctx->fid >= options.num_remote_forwards ||
624 (options.remote_forwards[fctx->fid].connect_path == NULL &&
625 options.remote_forwards[fctx->fid].connect_host == NULL)) {
626 xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
629 rfwd = &options.remote_forwards[fctx->fid];
630 debug("%s: %s for: listen %d, connect %s:%d", __func__,
631 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
632 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
633 rfwd->connect_host, rfwd->connect_port);
634 if (type == SSH2_MSG_REQUEST_SUCCESS) {
635 if (rfwd->listen_port == 0) {
636 rfwd->allocated_port = packet_get_int();
637 debug("Allocated port %u for mux remote forward"
638 " to %s:%d", rfwd->allocated_port,
639 rfwd->connect_host, rfwd->connect_port);
640 if ((r = sshbuf_put_u32(out,
641 MUX_S_REMOTE_PORT)) != 0 ||
642 (r = sshbuf_put_u32(out, fctx->rid)) != 0 ||
643 (r = sshbuf_put_u32(out,
644 rfwd->allocated_port)) != 0)
645 fatal("%s: reply: %s", __func__, ssh_err(r));
646 channel_update_permission(ssh, rfwd->handle,
647 rfwd->allocated_port);
649 reply_ok(out, fctx->rid);
653 if (rfwd->listen_port == 0)
654 channel_update_permission(ssh, rfwd->handle, -1);
655 if (rfwd->listen_path != NULL)
656 xasprintf(&failmsg, "remote port forwarding failed for "
657 "listen path %s", rfwd->listen_path);
659 xasprintf(&failmsg, "remote port forwarding failed for "
660 "listen port %d", rfwd->listen_port);
662 debug2("%s: clearing registered forwarding for listen %d, "
663 "connect %s:%d", __func__, rfwd->listen_port,
664 rfwd->connect_path ? rfwd->connect_path :
665 rfwd->connect_host, rfwd->connect_port);
667 free(rfwd->listen_host);
668 free(rfwd->listen_path);
669 free(rfwd->connect_host);
670 free(rfwd->connect_path);
671 memset(rfwd, 0, sizeof(*rfwd));
674 error("%s: %s", __func__, failmsg);
675 reply_error(out, MUX_S_FAILURE, fctx->rid, failmsg);
678 if ((r = sshbuf_put_stringb(c->output, out)) != 0)
679 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
681 if (c->mux_pause <= 0)
682 fatal("%s: mux_pause %d", __func__, c->mux_pause);
683 c->mux_pause = 0; /* start processing messages again */
687 mux_master_process_open_fwd(struct ssh *ssh, u_int rid,
688 Channel *c, struct sshbuf *m, struct sshbuf *reply)
691 char *fwd_desc = NULL;
692 char *listen_addr, *connect_addr;
695 int r, i, ret = 0, freefwd = 1;
697 memset(&fwd, 0, sizeof(fwd));
699 /* XXX - lport/cport check redundant */
700 if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
701 (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
702 (r = sshbuf_get_u32(m, &lport)) != 0 ||
703 (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
704 (r = sshbuf_get_u32(m, &cport)) != 0 ||
705 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
706 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
707 error("%s: malformed message", __func__);
711 if (*listen_addr == '\0') {
715 if (*connect_addr == '\0') {
720 memset(&fwd, 0, sizeof(fwd));
721 fwd.listen_port = lport;
722 if (fwd.listen_port == PORT_STREAMLOCAL)
723 fwd.listen_path = listen_addr;
725 fwd.listen_host = listen_addr;
726 fwd.connect_port = cport;
727 if (fwd.connect_port == PORT_STREAMLOCAL)
728 fwd.connect_path = connect_addr;
730 fwd.connect_host = connect_addr;
732 debug2("%s: channel %d: request %s", __func__, c->self,
733 (fwd_desc = format_forward(ftype, &fwd)));
735 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
736 ftype != MUX_FWD_DYNAMIC) {
737 logit("%s: invalid forwarding type %u", __func__, ftype);
741 reply_error(reply, MUX_S_FAILURE, rid,
742 "Invalid forwarding request");
745 if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) {
746 logit("%s: streamlocal and dynamic forwards "
747 "are mutually exclusive", __func__);
750 if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) {
751 logit("%s: invalid listen port %u", __func__,
755 if ((fwd.connect_port != PORT_STREAMLOCAL &&
756 fwd.connect_port >= 65536) ||
757 (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE &&
758 fwd.connect_port == 0)) {
759 logit("%s: invalid connect port %u", __func__,
763 if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL &&
764 fwd.connect_path == NULL) {
765 logit("%s: missing connect host", __func__);
769 /* Skip forwards that have already been requested */
772 case MUX_FWD_DYNAMIC:
773 for (i = 0; i < options.num_local_forwards; i++) {
774 if (compare_forward(&fwd,
775 options.local_forwards + i)) {
777 debug2("%s: found existing forwarding",
779 reply_ok(reply, rid);
785 for (i = 0; i < options.num_remote_forwards; i++) {
786 if (!compare_forward(&fwd, options.remote_forwards + i))
788 if (fwd.listen_port != 0)
790 debug2("%s: found allocated port", __func__);
791 if ((r = sshbuf_put_u32(reply,
792 MUX_S_REMOTE_PORT)) != 0 ||
793 (r = sshbuf_put_u32(reply, rid)) != 0 ||
794 (r = sshbuf_put_u32(reply,
795 options.remote_forwards[i].allocated_port)) != 0)
796 fatal("%s: reply: %s", __func__, ssh_err(r));
802 if (options.control_master == SSHCTL_MASTER_ASK ||
803 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
804 if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
805 debug2("%s: forwarding refused by user", __func__);
806 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
807 "Permission denied");
812 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
813 if (!channel_setup_local_fwd_listener(ssh, &fwd,
814 &options.fwd_opts)) {
816 logit("%s: requested %s failed", __func__, fwd_desc);
817 reply_error(reply, MUX_S_FAILURE, rid,
818 "Port forwarding failed");
821 add_local_forward(&options, &fwd);
824 struct mux_channel_confirm_ctx *fctx;
826 fwd.handle = channel_request_remote_forwarding(ssh, &fwd);
829 add_remote_forward(&options, &fwd);
830 fctx = xcalloc(1, sizeof(*fctx));
833 fctx->fid = options.num_remote_forwards - 1;
834 client_register_global_confirm(mux_confirm_remote_forward,
837 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
838 /* delayed reply in mux_confirm_remote_forward */
841 reply_ok(reply, rid);
845 free(fwd.listen_host);
846 free(fwd.listen_path);
847 free(fwd.connect_host);
848 free(fwd.connect_path);
854 mux_master_process_close_fwd(struct ssh *ssh, u_int rid,
855 Channel *c, struct sshbuf *m, struct sshbuf *reply)
857 struct Forward fwd, *found_fwd;
858 char *fwd_desc = NULL;
859 const char *error_reason = NULL;
860 char *listen_addr = NULL, *connect_addr = NULL;
865 memset(&fwd, 0, sizeof(fwd));
867 if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
868 (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
869 (r = sshbuf_get_u32(m, &lport)) != 0 ||
870 (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
871 (r = sshbuf_get_u32(m, &cport)) != 0 ||
872 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
873 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
874 error("%s: malformed message", __func__);
879 if (*listen_addr == '\0') {
883 if (*connect_addr == '\0') {
888 memset(&fwd, 0, sizeof(fwd));
889 fwd.listen_port = lport;
890 if (fwd.listen_port == PORT_STREAMLOCAL)
891 fwd.listen_path = listen_addr;
893 fwd.listen_host = listen_addr;
894 fwd.connect_port = cport;
895 if (fwd.connect_port == PORT_STREAMLOCAL)
896 fwd.connect_path = connect_addr;
898 fwd.connect_host = connect_addr;
900 debug2("%s: channel %d: request cancel %s", __func__, c->self,
901 (fwd_desc = format_forward(ftype, &fwd)));
903 /* make sure this has been requested */
907 case MUX_FWD_DYNAMIC:
908 for (i = 0; i < options.num_local_forwards; i++) {
909 if (compare_forward(&fwd,
910 options.local_forwards + i)) {
911 found_fwd = options.local_forwards + i;
917 for (i = 0; i < options.num_remote_forwards; i++) {
918 if (compare_forward(&fwd,
919 options.remote_forwards + i)) {
920 found_fwd = options.remote_forwards + i;
927 if (found_fwd == NULL)
928 error_reason = "port not forwarded";
929 else if (ftype == MUX_FWD_REMOTE) {
931 * This shouldn't fail unless we confused the host/port
932 * between options.remote_forwards and permitted_opens.
933 * However, for dynamic allocated listen ports we need
934 * to use the actual listen port.
936 if (channel_request_rforward_cancel(ssh, found_fwd) == -1)
937 error_reason = "port not in permitted opens";
938 } else { /* local and dynamic forwards */
940 if (channel_cancel_lport_listener(ssh, &fwd, fwd.connect_port,
941 &options.fwd_opts) == -1)
942 error_reason = "port not found";
945 if (error_reason != NULL)
946 reply_error(reply, MUX_S_FAILURE, rid, error_reason);
948 reply_ok(reply, rid);
949 free(found_fwd->listen_host);
950 free(found_fwd->listen_path);
951 free(found_fwd->connect_host);
952 free(found_fwd->connect_path);
953 found_fwd->listen_host = found_fwd->connect_host = NULL;
954 found_fwd->listen_path = found_fwd->connect_path = NULL;
955 found_fwd->listen_port = found_fwd->connect_port = 0;
966 mux_master_process_stdio_fwd(struct ssh *ssh, u_int rid,
967 Channel *c, struct sshbuf *m, struct sshbuf *reply)
973 struct mux_stdio_confirm_ctx *cctx;
975 if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
976 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
977 (r = sshbuf_get_u32(m, &cport)) != 0) {
979 error("%s: malformed message", __func__);
983 debug2("%s: channel %d: request stdio fwd to %s:%u",
984 __func__, c->self, chost, cport);
986 /* Gather fds from client */
987 for(i = 0; i < 2; i++) {
988 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
989 error("%s: failed to receive fd %d from slave",
991 for (j = 0; j < i; j++)
996 reply_error(reply, MUX_S_FAILURE, rid,
997 "did not receive file descriptors");
1002 debug3("%s: got fds stdin %d, stdout %d", __func__,
1003 new_fd[0], new_fd[1]);
1005 /* XXX support multiple child sessions in future */
1006 if (c->have_remote_id) {
1007 debug2("%s: session already open", __func__);
1008 reply_error(reply, MUX_S_FAILURE, rid,
1009 "Multiple sessions not supported");
1017 if (options.control_master == SSHCTL_MASTER_ASK ||
1018 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
1019 if (!ask_permission("Allow forward to %s:%u? ",
1021 debug2("%s: stdio fwd refused by user", __func__);
1022 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
1023 "Permission denied");
1028 /* enable nonblocking unless tty */
1029 if (!isatty(new_fd[0]))
1030 set_nonblock(new_fd[0]);
1031 if (!isatty(new_fd[1]))
1032 set_nonblock(new_fd[1]);
1034 nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1]);
1037 nc->ctl_chan = c->self; /* link session -> control channel */
1038 c->remote_id = nc->self; /* link control -> session channel */
1039 c->have_remote_id = 1;
1041 debug2("%s: channel_new: %d linked to control channel %d",
1042 __func__, nc->self, nc->ctl_chan);
1044 channel_register_cleanup(ssh, nc->self,
1045 mux_master_session_cleanup_cb, 1);
1047 cctx = xcalloc(1, sizeof(*cctx));
1049 channel_register_open_confirm(ssh, nc->self, mux_stdio_confirm, cctx);
1050 c->mux_pause = 1; /* stop handling messages until open_confirm done */
1052 /* reply is deferred, sent by mux_session_confirm */
1056 /* Callback on open confirmation in mux master for a mux stdio fwd session. */
1058 mux_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
1060 struct mux_stdio_confirm_ctx *cctx = arg;
1062 struct sshbuf *reply;
1066 fatal("%s: cctx == NULL", __func__);
1067 if ((c = channel_by_id(ssh, id)) == NULL)
1068 fatal("%s: no channel for id %d", __func__, id);
1069 if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
1070 fatal("%s: channel %d lacks control channel %d", __func__,
1072 if ((reply = sshbuf_new()) == NULL)
1073 fatal("%s: sshbuf_new", __func__);
1076 debug3("%s: sending failure reply", __func__);
1077 reply_error(reply, MUX_S_FAILURE, cctx->rid,
1078 "Session open refused by peer");
1083 debug3("%s: sending success reply", __func__);
1085 if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
1086 (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
1087 (r = sshbuf_put_u32(reply, c->self)) != 0)
1088 fatal("%s: reply: %s", __func__, ssh_err(r));
1092 if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
1093 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
1096 if (cc->mux_pause <= 0)
1097 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1098 cc->mux_pause = 0; /* start processing messages again */
1099 c->open_confirm_ctx = NULL;
1104 mux_master_process_stop_listening(struct ssh *ssh, u_int rid,
1105 Channel *c, struct sshbuf *m, struct sshbuf *reply)
1107 debug("%s: channel %d: stop listening", __func__, c->self);
1109 if (options.control_master == SSHCTL_MASTER_ASK ||
1110 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
1111 if (!ask_permission("Disable further multiplexing on shared "
1112 "connection to %s? ", host)) {
1113 debug2("%s: stop listen refused by user", __func__);
1114 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
1115 "Permission denied");
1120 if (mux_listener_channel != NULL) {
1121 channel_free(ssh, mux_listener_channel);
1123 free(options.control_path);
1124 options.control_path = NULL;
1125 mux_listener_channel = NULL;
1126 muxserver_sock = -1;
1129 reply_ok(reply, rid);
1134 mux_master_process_proxy(struct ssh *ssh, u_int rid,
1135 Channel *c, struct sshbuf *m, struct sshbuf *reply)
1139 debug("%s: channel %d: proxy request", __func__, c->self);
1141 c->mux_rcb = channel_proxy_downstream;
1142 if ((r = sshbuf_put_u32(reply, MUX_S_PROXY)) != 0 ||
1143 (r = sshbuf_put_u32(reply, rid)) != 0)
1144 fatal("%s: reply: %s", __func__, ssh_err(r));
1149 /* Channel callbacks fired on read/write from mux slave fd */
1151 mux_master_read_cb(struct ssh *ssh, Channel *c)
1153 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
1154 struct sshbuf *in = NULL, *out = NULL;
1158 if ((out = sshbuf_new()) == NULL)
1159 fatal("%s: sshbuf_new", __func__);
1162 if (c->mux_ctx == NULL) {
1163 state = xcalloc(1, sizeof(*state));
1165 channel_register_cleanup(ssh, c->self,
1166 mux_master_control_cleanup_cb, 0);
1169 if ((r = sshbuf_put_u32(out, MUX_MSG_HELLO)) != 0 ||
1170 (r = sshbuf_put_u32(out, SSHMUX_VER)) != 0)
1171 fatal("%s: reply: %s", __func__, ssh_err(r));
1173 if ((r = sshbuf_put_stringb(c->output, out)) != 0)
1174 fatal("%s: sshbuf_put_stringb: %s",
1175 __func__, ssh_err(r));
1176 debug3("%s: channel %d: hello sent", __func__, c->self);
1181 /* Channel code ensures that we receive whole packets */
1182 if ((r = sshbuf_froms(c->input, &in)) != 0) {
1184 error("%s: malformed message", __func__);
1188 if ((r = sshbuf_get_u32(in, &type)) != 0)
1190 debug3("%s: channel %d packet type 0x%08x len %zu",
1191 __func__, c->self, type, sshbuf_len(in));
1193 if (type == MUX_MSG_HELLO)
1196 if (!state->hello_rcvd) {
1197 error("%s: expected MUX_MSG_HELLO(0x%08x), "
1198 "received 0x%08x", __func__, MUX_MSG_HELLO, type);
1201 if ((r = sshbuf_get_u32(in, &rid)) != 0)
1205 for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
1206 if (type == mux_master_handlers[i].type) {
1207 ret = mux_master_handlers[i].handler(ssh, rid,
1212 if (mux_master_handlers[i].handler == NULL) {
1213 error("%s: unsupported mux message 0x%08x", __func__, type);
1214 reply_error(out, MUX_S_FAILURE, rid, "unsupported request");
1217 /* Enqueue reply packet */
1218 if (sshbuf_len(out) != 0) {
1219 if ((r = sshbuf_put_stringb(c->output, out)) != 0)
1220 fatal("%s: sshbuf_put_stringb: %s",
1221 __func__, ssh_err(r));
1230 mux_exit_message(struct ssh *ssh, Channel *c, int exitval)
1236 debug3("%s: channel %d: exit message, exitval %d", __func__, c->self,
1239 if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
1240 fatal("%s: channel %d missing mux channel %d",
1241 __func__, c->self, c->ctl_chan);
1243 /* Append exit message packet to control socket output queue */
1244 if ((m = sshbuf_new()) == NULL)
1245 fatal("%s: sshbuf_new", __func__);
1246 if ((r = sshbuf_put_u32(m, MUX_S_EXIT_MESSAGE)) != 0 ||
1247 (r = sshbuf_put_u32(m, c->self)) != 0 ||
1248 (r = sshbuf_put_u32(m, exitval)) != 0 ||
1249 (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
1250 fatal("%s: reply: %s", __func__, ssh_err(r));
1255 mux_tty_alloc_failed(struct ssh *ssh, Channel *c)
1261 debug3("%s: channel %d: TTY alloc failed", __func__, c->self);
1263 if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
1264 fatal("%s: channel %d missing mux channel %d",
1265 __func__, c->self, c->ctl_chan);
1267 /* Append exit message packet to control socket output queue */
1268 if ((m = sshbuf_new()) == NULL)
1269 fatal("%s: sshbuf_new", __func__);
1270 if ((r = sshbuf_put_u32(m, MUX_S_TTY_ALLOC_FAIL)) != 0 ||
1271 (r = sshbuf_put_u32(m, c->self)) != 0 ||
1272 (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
1273 fatal("%s: reply: %s", __func__, ssh_err(r));
1277 /* Prepare a mux master to listen on a Unix domain socket. */
1279 muxserver_listen(struct ssh *ssh)
1282 char *orig_control_path = options.control_path;
1287 if (options.control_path == NULL ||
1288 options.control_master == SSHCTL_MASTER_NO)
1291 debug("setting up multiplex master socket");
1294 * Use a temporary path before listen so we can pseudo-atomically
1295 * establish the listening socket in its final location to avoid
1296 * other processes racing in between bind() and listen() and hitting
1297 * an unready socket.
1299 for (i = 0; i < sizeof(rbuf) - 1; i++) {
1300 r = arc4random_uniform(26+26+10);
1301 rbuf[i] = (r < 26) ? 'a' + r :
1302 (r < 26*2) ? 'A' + r - 26 :
1305 rbuf[sizeof(rbuf) - 1] = '\0';
1306 options.control_path = NULL;
1307 xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
1308 debug3("%s: temporary control path %s", __func__, options.control_path);
1310 old_umask = umask(0177);
1311 muxserver_sock = unix_listener(options.control_path, 64, 0);
1314 if (muxserver_sock < 0) {
1315 if (oerrno == EINVAL || oerrno == EADDRINUSE) {
1316 error("ControlSocket %s already exists, "
1317 "disabling multiplexing", options.control_path);
1319 if (muxserver_sock != -1) {
1320 close(muxserver_sock);
1321 muxserver_sock = -1;
1323 free(orig_control_path);
1324 free(options.control_path);
1325 options.control_path = NULL;
1326 options.control_master = SSHCTL_MASTER_NO;
1329 /* unix_listener() logs the error */
1334 /* Now atomically "move" the mux socket into position */
1335 if (link(options.control_path, orig_control_path) != 0) {
1336 if (errno != EEXIST) {
1337 fatal("%s: link mux listener %s => %s: %s", __func__,
1338 options.control_path, orig_control_path,
1341 error("ControlSocket %s already exists, disabling multiplexing",
1343 unlink(options.control_path);
1344 goto disable_mux_master;
1346 unlink(options.control_path);
1347 free(options.control_path);
1348 options.control_path = orig_control_path;
1350 set_nonblock(muxserver_sock);
1352 mux_listener_channel = channel_new(ssh, "mux listener",
1353 SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
1354 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1355 0, options.control_path, 1);
1356 mux_listener_channel->mux_rcb = mux_master_read_cb;
1357 debug3("%s: mux listener channel %d fd %d", __func__,
1358 mux_listener_channel->self, mux_listener_channel->sock);
1361 /* Callback on open confirmation in mux master for a mux client session. */
1363 mux_session_confirm(struct ssh *ssh, int id, int success, void *arg)
1365 struct mux_session_confirm_ctx *cctx = arg;
1366 const char *display;
1369 struct sshbuf *reply;
1372 fatal("%s: cctx == NULL", __func__);
1373 if ((c = channel_by_id(ssh, id)) == NULL)
1374 fatal("%s: no channel for id %d", __func__, id);
1375 if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
1376 fatal("%s: channel %d lacks control channel %d", __func__,
1378 if ((reply = sshbuf_new()) == NULL)
1379 fatal("%s: sshbuf_new", __func__);
1382 debug3("%s: sending failure reply", __func__);
1383 reply_error(reply, MUX_S_FAILURE, cctx->rid,
1384 "Session open refused by peer");
1388 display = getenv("DISPLAY");
1389 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1392 /* Get reasonable local authentication information. */
1393 if (client_x11_get_proto(ssh, display, options.xauth_location,
1394 options.forward_x11_trusted, options.forward_x11_timeout,
1395 &proto, &data) == 0) {
1396 /* Request forwarding with authentication spoofing. */
1397 debug("Requesting X11 forwarding with authentication "
1399 x11_request_forwarding_with_spoofing(ssh, id,
1400 display, proto, data, 1);
1401 /* XXX exit_on_forward_failure */
1402 client_expect_confirm(ssh, id, "X11 forwarding",
1407 if (cctx->want_agent_fwd && options.forward_agent) {
1408 debug("Requesting authentication agent forwarding.");
1409 channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
1413 client_session2_setup(ssh, id, cctx->want_tty, cctx->want_subsys,
1414 cctx->term, &cctx->tio, c->rfd, cctx->cmd, cctx->env);
1416 debug3("%s: sending success reply", __func__);
1418 if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
1419 (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
1420 (r = sshbuf_put_u32(reply, c->self)) != 0)
1421 fatal("%s: reply: %s", __func__, ssh_err(r));
1425 if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
1426 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
1429 if (cc->mux_pause <= 0)
1430 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1431 cc->mux_pause = 0; /* start processing messages again */
1432 c->open_confirm_ctx = NULL;
1433 sshbuf_free(cctx->cmd);
1435 if (cctx->env != NULL) {
1436 for (i = 0; cctx->env[i] != NULL; i++)
1443 /* ** Multiplexing client support */
1445 /* Exit signal handler */
1447 control_client_sighandler(int signo)
1449 muxclient_terminate = signo;
1453 * Relay signal handler - used to pass some signals from mux client to
1457 control_client_sigrelay(int signo)
1459 int save_errno = errno;
1461 if (muxserver_pid > 1)
1462 kill(muxserver_pid, signo);
1468 mux_client_read(int fd, struct sshbuf *b, size_t need)
1477 pfd.events = POLLIN;
1478 if ((r = sshbuf_reserve(b, need, &p)) != 0)
1479 fatal("%s: reserve: %s", __func__, ssh_err(r));
1480 for (have = 0; have < need; ) {
1481 if (muxclient_terminate) {
1485 len = read(fd, p + have, need - have);
1488 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1492 (void)poll(&pfd, 1, -1);
1504 have += (size_t)len;
1510 mux_client_write_packet(int fd, struct sshbuf *m)
1512 struct sshbuf *queue;
1519 pfd.events = POLLOUT;
1520 if ((queue = sshbuf_new()) == NULL)
1521 fatal("%s: sshbuf_new", __func__);
1522 if ((r = sshbuf_put_stringb(queue, m)) != 0)
1523 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
1525 need = sshbuf_len(queue);
1526 ptr = sshbuf_ptr(queue);
1528 for (have = 0; have < need; ) {
1529 if (muxclient_terminate) {
1534 len = write(fd, ptr + have, need - have);
1537 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1541 (void)poll(&pfd, 1, -1);
1564 mux_client_read_packet(int fd, struct sshbuf *m)
1566 struct sshbuf *queue;
1571 if ((queue = sshbuf_new()) == NULL)
1572 fatal("%s: sshbuf_new", __func__);
1573 if (mux_client_read(fd, queue, 4) != 0) {
1574 if ((oerrno = errno) == EPIPE)
1575 debug3("%s: read header failed: %s", __func__,
1581 need = PEEK_U32(sshbuf_ptr(queue));
1582 if (mux_client_read(fd, queue, need) != 0) {
1584 debug3("%s: read body failed: %s", __func__, strerror(errno));
1589 if ((r = sshbuf_get_string_direct(queue, &ptr, &have)) != 0 ||
1590 (r = sshbuf_put(m, ptr, have)) != 0)
1591 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1597 mux_client_hello_exchange(int fd)
1603 if ((m = sshbuf_new()) == NULL)
1604 fatal("%s: sshbuf_new", __func__);
1605 if ((r = sshbuf_put_u32(m, MUX_MSG_HELLO)) != 0 ||
1606 (r = sshbuf_put_u32(m, SSHMUX_VER)) != 0)
1607 fatal("%s: hello: %s", __func__, ssh_err(r));
1610 if (mux_client_write_packet(fd, m) != 0) {
1611 debug("%s: write packet: %s", __func__, strerror(errno));
1617 /* Read their HELLO */
1618 if (mux_client_read_packet(fd, m) != 0) {
1619 debug("%s: read packet failed", __func__);
1623 if ((r = sshbuf_get_u32(m, &type)) != 0)
1624 fatal("%s: decode type: %s", __func__, ssh_err(r));
1625 if (type != MUX_MSG_HELLO) {
1626 error("%s: expected HELLO (%u) received %u",
1627 __func__, MUX_MSG_HELLO, type);
1630 if ((r = sshbuf_get_u32(m, &ver)) != 0)
1631 fatal("%s: decode version: %s", __func__, ssh_err(r));
1632 if (ver != SSHMUX_VER) {
1633 error("Unsupported multiplexing protocol version %d "
1634 "(expected %d)", ver, SSHMUX_VER);
1637 debug2("%s: master version %u", __func__, ver);
1638 /* No extensions are presently defined */
1639 while (sshbuf_len(m) > 0) {
1642 if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
1643 (r = sshbuf_skip_string(m)) != 0) { /* value */
1644 error("%s: malformed extension: %s",
1645 __func__, ssh_err(r));
1648 debug2("Unrecognised master extension \"%s\"", name);
1659 mux_client_request_alive(int fd)
1663 u_int pid, type, rid;
1666 debug3("%s: entering", __func__);
1668 if ((m = sshbuf_new()) == NULL)
1669 fatal("%s: sshbuf_new", __func__);
1670 if ((r = sshbuf_put_u32(m, MUX_C_ALIVE_CHECK)) != 0 ||
1671 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
1672 fatal("%s: request: %s", __func__, ssh_err(r));
1674 if (mux_client_write_packet(fd, m) != 0)
1675 fatal("%s: write packet: %s", __func__, strerror(errno));
1679 /* Read their reply */
1680 if (mux_client_read_packet(fd, m) != 0) {
1685 if ((r = sshbuf_get_u32(m, &type)) != 0)
1686 fatal("%s: decode type: %s", __func__, ssh_err(r));
1687 if (type != MUX_S_ALIVE) {
1688 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1689 fatal("%s: decode error: %s", __func__, ssh_err(r));
1690 fatal("%s: master returned error: %s", __func__, e);
1693 if ((r = sshbuf_get_u32(m, &rid)) != 0)
1694 fatal("%s: decode remote ID: %s", __func__, ssh_err(r));
1695 if (rid != muxclient_request_id)
1696 fatal("%s: out of sequence reply: my id %u theirs %u",
1697 __func__, muxclient_request_id, rid);
1698 if ((r = sshbuf_get_u32(m, &pid)) != 0)
1699 fatal("%s: decode PID: %s", __func__, ssh_err(r));
1702 debug3("%s: done pid = %u", __func__, pid);
1704 muxclient_request_id++;
1710 mux_client_request_terminate(int fd)
1717 debug3("%s: entering", __func__);
1719 if ((m = sshbuf_new()) == NULL)
1720 fatal("%s: sshbuf_new", __func__);
1721 if ((r = sshbuf_put_u32(m, MUX_C_TERMINATE)) != 0 ||
1722 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
1723 fatal("%s: request: %s", __func__, ssh_err(r));
1725 if (mux_client_write_packet(fd, m) != 0)
1726 fatal("%s: write packet: %s", __func__, strerror(errno));
1730 /* Read their reply */
1731 if (mux_client_read_packet(fd, m) != 0) {
1732 /* Remote end exited already */
1733 if (errno == EPIPE) {
1737 fatal("%s: read from master failed: %s",
1738 __func__, strerror(errno));
1741 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1742 (r = sshbuf_get_u32(m, &rid)) != 0)
1743 fatal("%s: decode: %s", __func__, ssh_err(r));
1744 if (rid != muxclient_request_id)
1745 fatal("%s: out of sequence reply: my id %u theirs %u",
1746 __func__, muxclient_request_id, rid);
1750 case MUX_S_PERMISSION_DENIED:
1751 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1752 fatal("%s: decode error: %s", __func__, ssh_err(r));
1753 fatal("Master refused termination request: %s", e);
1755 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1756 fatal("%s: decode error: %s", __func__, ssh_err(r));
1757 fatal("%s: termination request failed: %s", __func__, e);
1759 fatal("%s: unexpected response from master 0x%08x",
1763 muxclient_request_id++;
1767 mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd)
1771 const char *lhost, *chost;
1775 fwd_desc = format_forward(ftype, fwd);
1776 debug("Requesting %s %s",
1777 cancel_flag ? "cancellation of" : "forwarding of", fwd_desc);
1780 type = cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD;
1781 if (fwd->listen_path != NULL)
1782 lhost = fwd->listen_path;
1783 else if (fwd->listen_host == NULL)
1785 else if (*fwd->listen_host == '\0')
1788 lhost = fwd->listen_host;
1790 if (fwd->connect_path != NULL)
1791 chost = fwd->connect_path;
1792 else if (fwd->connect_host == NULL)
1795 chost = fwd->connect_host;
1797 if ((m = sshbuf_new()) == NULL)
1798 fatal("%s: sshbuf_new", __func__);
1799 if ((r = sshbuf_put_u32(m, type)) != 0 ||
1800 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
1801 (r = sshbuf_put_u32(m, ftype)) != 0 ||
1802 (r = sshbuf_put_cstring(m, lhost)) != 0 ||
1803 (r = sshbuf_put_u32(m, fwd->listen_port)) != 0 ||
1804 (r = sshbuf_put_cstring(m, chost)) != 0 ||
1805 (r = sshbuf_put_u32(m, fwd->connect_port)) != 0)
1806 fatal("%s: request: %s", __func__, ssh_err(r));
1808 if (mux_client_write_packet(fd, m) != 0)
1809 fatal("%s: write packet: %s", __func__, strerror(errno));
1813 /* Read their reply */
1814 if (mux_client_read_packet(fd, m) != 0) {
1819 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1820 (r = sshbuf_get_u32(m, &rid)) != 0)
1821 fatal("%s: decode: %s", __func__, ssh_err(r));
1822 if (rid != muxclient_request_id)
1823 fatal("%s: out of sequence reply: my id %u theirs %u",
1824 __func__, muxclient_request_id, rid);
1829 case MUX_S_REMOTE_PORT:
1831 fatal("%s: got MUX_S_REMOTE_PORT for cancel", __func__);
1832 if ((r = sshbuf_get_u32(m, &fwd->allocated_port)) != 0)
1833 fatal("%s: decode port: %s", __func__, ssh_err(r));
1834 verbose("Allocated port %u for remote forward to %s:%d",
1835 fwd->allocated_port,
1836 fwd->connect_host ? fwd->connect_host : "",
1838 if (muxclient_command == SSHMUX_COMMAND_FORWARD)
1839 fprintf(stdout, "%i\n", fwd->allocated_port);
1841 case MUX_S_PERMISSION_DENIED:
1842 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1843 fatal("%s: decode error: %s", __func__, ssh_err(r));
1845 error("Master refused forwarding request: %s", e);
1848 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1849 fatal("%s: decode error: %s", __func__, ssh_err(r));
1851 error("%s: forwarding request failed: %s", __func__, e);
1854 fatal("%s: unexpected response from master 0x%08x",
1859 muxclient_request_id++;
1864 mux_client_forwards(int fd, int cancel_flag)
1868 debug3("%s: %s forwardings: %d local, %d remote", __func__,
1869 cancel_flag ? "cancel" : "request",
1870 options.num_local_forwards, options.num_remote_forwards);
1872 /* XXX ExitOnForwardingFailure */
1873 for (i = 0; i < options.num_local_forwards; i++) {
1874 if (mux_client_forward(fd, cancel_flag,
1875 options.local_forwards[i].connect_port == 0 ?
1876 MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
1877 options.local_forwards + i) != 0)
1880 for (i = 0; i < options.num_remote_forwards; i++) {
1881 if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE,
1882 options.remote_forwards + i) != 0)
1889 mux_client_request_session(int fd)
1894 u_int echar, rid, sid, esid, exitval, type, exitval_seen;
1895 extern char **environ;
1896 int r, i, devnull, rawmode;
1898 debug3("%s: entering", __func__);
1900 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1901 error("%s: master alive request failed", __func__);
1905 signal(SIGPIPE, SIG_IGN);
1907 if (stdin_null_flag) {
1908 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1909 fatal("open(/dev/null): %s", strerror(errno));
1910 if (dup2(devnull, STDIN_FILENO) == -1)
1911 fatal("dup2: %s", strerror(errno));
1912 if (devnull > STDERR_FILENO)
1916 if ((term = getenv("TERM")) == NULL)
1919 if (options.escape_char != SSH_ESCAPECHAR_NONE)
1920 echar = (u_int)options.escape_char;
1922 if ((m = sshbuf_new()) == NULL)
1923 fatal("%s: sshbuf_new", __func__);
1924 if ((r = sshbuf_put_u32(m, MUX_C_NEW_SESSION)) != 0 ||
1925 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
1926 (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
1927 (r = sshbuf_put_u32(m, tty_flag)) != 0 ||
1928 (r = sshbuf_put_u32(m, options.forward_x11)) != 0 ||
1929 (r = sshbuf_put_u32(m, options.forward_agent)) != 0 ||
1930 (r = sshbuf_put_u32(m, subsystem_flag)) != 0 ||
1931 (r = sshbuf_put_u32(m, echar)) != 0 ||
1932 (r = sshbuf_put_cstring(m, term)) != 0 ||
1933 (r = sshbuf_put_stringb(m, command)) != 0)
1934 fatal("%s: request: %s", __func__, ssh_err(r));
1936 /* Pass environment */
1937 if (options.num_send_env > 0 && environ != NULL) {
1938 for (i = 0; environ[i] != NULL; i++) {
1939 if (!env_permitted(environ[i]))
1941 if ((r = sshbuf_put_cstring(m, environ[i])) != 0)
1942 fatal("%s: request: %s", __func__, ssh_err(r));
1945 for (i = 0; i < options.num_setenv; i++) {
1946 if ((r = sshbuf_put_cstring(m, options.setenv[i])) != 0)
1947 fatal("%s: request: %s", __func__, ssh_err(r));
1950 if (mux_client_write_packet(fd, m) != 0)
1951 fatal("%s: write packet: %s", __func__, strerror(errno));
1953 /* Send the stdio file descriptors */
1954 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1955 mm_send_fd(fd, STDOUT_FILENO) == -1 ||
1956 mm_send_fd(fd, STDERR_FILENO) == -1)
1957 fatal("%s: send fds failed", __func__);
1959 debug3("%s: session request sent", __func__);
1961 /* Read their reply */
1963 if (mux_client_read_packet(fd, m) != 0) {
1964 error("%s: read from master failed: %s",
1965 __func__, strerror(errno));
1970 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1971 (r = sshbuf_get_u32(m, &rid)) != 0)
1972 fatal("%s: decode: %s", __func__, ssh_err(r));
1973 if (rid != muxclient_request_id)
1974 fatal("%s: out of sequence reply: my id %u theirs %u",
1975 __func__, muxclient_request_id, rid);
1978 case MUX_S_SESSION_OPENED:
1979 if ((r = sshbuf_get_u32(m, &sid)) != 0)
1980 fatal("%s: decode ID: %s", __func__, ssh_err(r));
1982 case MUX_S_PERMISSION_DENIED:
1983 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1984 fatal("%s: decode error: %s", __func__, ssh_err(r));
1985 error("Master refused session request: %s", e);
1989 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1990 fatal("%s: decode error: %s", __func__, ssh_err(r));
1991 error("%s: session request failed: %s", __func__, e);
1996 error("%s: unexpected response from master 0x%08x",
2000 muxclient_request_id++;
2002 if (pledge("stdio proc tty", NULL) == -1)
2003 fatal("%s pledge(): %s", __func__, strerror(errno));
2004 platform_pledge_mux();
2006 signal(SIGHUP, control_client_sighandler);
2007 signal(SIGINT, control_client_sighandler);
2008 signal(SIGTERM, control_client_sighandler);
2009 signal(SIGWINCH, control_client_sigrelay);
2013 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
2016 * Stick around until the controlee closes the client_fd.
2017 * Before it does, it is expected to write an exit message.
2018 * This process must read the value and wait for the closure of
2019 * the client_fd; if this one closes early, the multiplex master will
2020 * terminate early too (possibly losing data).
2022 for (exitval = 255, exitval_seen = 0;;) {
2024 if (mux_client_read_packet(fd, m) != 0)
2026 if ((r = sshbuf_get_u32(m, &type)) != 0)
2027 fatal("%s: decode type: %s", __func__, ssh_err(r));
2029 case MUX_S_TTY_ALLOC_FAIL:
2030 if ((r = sshbuf_get_u32(m, &esid)) != 0)
2031 fatal("%s: decode ID: %s",
2032 __func__, ssh_err(r));
2034 fatal("%s: tty alloc fail on unknown session: "
2035 "my id %u theirs %u",
2036 __func__, sid, esid);
2037 leave_raw_mode(options.request_tty ==
2041 case MUX_S_EXIT_MESSAGE:
2042 if ((r = sshbuf_get_u32(m, &esid)) != 0)
2043 fatal("%s: decode ID: %s",
2044 __func__, ssh_err(r));
2046 fatal("%s: exit on unknown session: "
2047 "my id %u theirs %u",
2048 __func__, sid, esid);
2050 fatal("%s: exitval sent twice", __func__);
2051 if ((r = sshbuf_get_u32(m, &exitval)) != 0)
2052 fatal("%s: decode exit value: %s",
2053 __func__, ssh_err(r));
2057 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2058 fatal("%s: decode error: %s",
2059 __func__, ssh_err(r));
2060 fatal("%s: master returned error: %s", __func__, e);
2066 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
2068 if (muxclient_terminate) {
2069 debug2("Exiting on signal: %s", strsignal(muxclient_terminate));
2071 } else if (!exitval_seen) {
2072 debug2("Control master terminated unexpectedly");
2075 debug2("Received exit status from master %d", exitval);
2077 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
2078 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
2084 mux_client_proxy(int fd)
2091 if ((m = sshbuf_new()) == NULL)
2092 fatal("%s: sshbuf_new", __func__);
2093 if ((r = sshbuf_put_u32(m, MUX_C_PROXY)) != 0 ||
2094 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
2095 fatal("%s: request: %s", __func__, ssh_err(r));
2096 if (mux_client_write_packet(fd, m) != 0)
2097 fatal("%s: write packet: %s", __func__, strerror(errno));
2101 /* Read their reply */
2102 if (mux_client_read_packet(fd, m) != 0) {
2106 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
2107 (r = sshbuf_get_u32(m, &rid)) != 0)
2108 fatal("%s: decode: %s", __func__, ssh_err(r));
2109 if (rid != muxclient_request_id)
2110 fatal("%s: out of sequence reply: my id %u theirs %u",
2111 __func__, muxclient_request_id, rid);
2112 if (type != MUX_S_PROXY) {
2113 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2114 fatal("%s: decode error: %s", __func__, ssh_err(r));
2115 fatal("%s: master returned error: %s", __func__, e);
2119 debug3("%s: done", __func__);
2120 muxclient_request_id++;
2125 mux_client_request_stdio_fwd(int fd)
2129 u_int type, rid, sid;
2132 debug3("%s: entering", __func__);
2134 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
2135 error("%s: master alive request failed", __func__);
2139 signal(SIGPIPE, SIG_IGN);
2141 if (stdin_null_flag) {
2142 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
2143 fatal("open(/dev/null): %s", strerror(errno));
2144 if (dup2(devnull, STDIN_FILENO) == -1)
2145 fatal("dup2: %s", strerror(errno));
2146 if (devnull > STDERR_FILENO)
2150 if ((m = sshbuf_new()) == NULL)
2151 fatal("%s: sshbuf_new", __func__);
2152 if ((r = sshbuf_put_u32(m, MUX_C_NEW_STDIO_FWD)) != 0 ||
2153 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
2154 (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
2155 (r = sshbuf_put_cstring(m, options.stdio_forward_host)) != 0 ||
2156 (r = sshbuf_put_u32(m, options.stdio_forward_port)) != 0)
2157 fatal("%s: request: %s", __func__, ssh_err(r));
2159 if (mux_client_write_packet(fd, m) != 0)
2160 fatal("%s: write packet: %s", __func__, strerror(errno));
2162 /* Send the stdio file descriptors */
2163 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
2164 mm_send_fd(fd, STDOUT_FILENO) == -1)
2165 fatal("%s: send fds failed", __func__);
2167 if (pledge("stdio proc tty", NULL) == -1)
2168 fatal("%s pledge(): %s", __func__, strerror(errno));
2169 platform_pledge_mux();
2171 debug3("%s: stdio forward request sent", __func__);
2173 /* Read their reply */
2176 if (mux_client_read_packet(fd, m) != 0) {
2177 error("%s: read from master failed: %s",
2178 __func__, strerror(errno));
2183 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
2184 (r = sshbuf_get_u32(m, &rid)) != 0)
2185 fatal("%s: decode: %s", __func__, ssh_err(r));
2186 if (rid != muxclient_request_id)
2187 fatal("%s: out of sequence reply: my id %u theirs %u",
2188 __func__, muxclient_request_id, rid);
2190 case MUX_S_SESSION_OPENED:
2191 if ((r = sshbuf_get_u32(m, &sid)) != 0)
2192 fatal("%s: decode ID: %s", __func__, ssh_err(r));
2193 debug("%s: master session id: %u", __func__, sid);
2195 case MUX_S_PERMISSION_DENIED:
2196 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2197 fatal("%s: decode error: %s", __func__, ssh_err(r));
2199 fatal("Master refused stdio forwarding request: %s", e);
2201 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2202 fatal("%s: decode error: %s", __func__, ssh_err(r));
2204 fatal("Stdio forwarding request failed: %s", e);
2207 error("%s: unexpected response from master 0x%08x",
2211 muxclient_request_id++;
2213 signal(SIGHUP, control_client_sighandler);
2214 signal(SIGINT, control_client_sighandler);
2215 signal(SIGTERM, control_client_sighandler);
2216 signal(SIGWINCH, control_client_sigrelay);
2219 * Stick around until the controlee closes the client_fd.
2222 if (mux_client_read_packet(fd, m) != 0) {
2223 if (errno == EPIPE ||
2224 (errno == EINTR && muxclient_terminate != 0))
2226 fatal("%s: mux_client_read_packet: %s",
2227 __func__, strerror(errno));
2229 fatal("%s: master returned unexpected message %u", __func__, type);
2233 mux_client_request_stop_listening(int fd)
2240 debug3("%s: entering", __func__);
2242 if ((m = sshbuf_new()) == NULL)
2243 fatal("%s: sshbuf_new", __func__);
2244 if ((r = sshbuf_put_u32(m, MUX_C_STOP_LISTENING)) != 0 ||
2245 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
2246 fatal("%s: request: %s", __func__, ssh_err(r));
2248 if (mux_client_write_packet(fd, m) != 0)
2249 fatal("%s: write packet: %s", __func__, strerror(errno));
2253 /* Read their reply */
2254 if (mux_client_read_packet(fd, m) != 0)
2255 fatal("%s: read from master failed: %s",
2256 __func__, strerror(errno));
2258 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
2259 (r = sshbuf_get_u32(m, &rid)) != 0)
2260 fatal("%s: decode: %s", __func__, ssh_err(r));
2261 if (rid != muxclient_request_id)
2262 fatal("%s: out of sequence reply: my id %u theirs %u",
2263 __func__, muxclient_request_id, rid);
2268 case MUX_S_PERMISSION_DENIED:
2269 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2270 fatal("%s: decode error: %s", __func__, ssh_err(r));
2271 fatal("Master refused stop listening request: %s", e);
2273 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2274 fatal("%s: decode error: %s", __func__, ssh_err(r));
2275 fatal("%s: stop listening request failed: %s", __func__, e);
2277 fatal("%s: unexpected response from master 0x%08x",
2281 muxclient_request_id++;
2284 /* Multiplex client main loop. */
2286 muxclient(const char *path)
2288 struct sockaddr_un addr;
2292 if (muxclient_command == 0) {
2293 if (options.stdio_forward_host != NULL)
2294 muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
2296 muxclient_command = SSHMUX_COMMAND_OPEN;
2299 switch (options.control_master) {
2300 case SSHCTL_MASTER_AUTO:
2301 case SSHCTL_MASTER_AUTO_ASK:
2302 debug("auto-mux: Trying existing master");
2304 case SSHCTL_MASTER_NO:
2310 memset(&addr, '\0', sizeof(addr));
2311 addr.sun_family = AF_UNIX;
2313 if (strlcpy(addr.sun_path, path,
2314 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
2315 fatal("ControlPath too long ('%s' >= %u bytes)", path,
2316 (unsigned int)sizeof(addr.sun_path));
2318 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
2319 fatal("%s socket(): %s", __func__, strerror(errno));
2321 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
2322 switch (muxclient_command) {
2323 case SSHMUX_COMMAND_OPEN:
2324 case SSHMUX_COMMAND_STDIO_FWD:
2327 fatal("Control socket connect(%.100s): %s", path,
2330 if (errno == ECONNREFUSED &&
2331 options.control_master != SSHCTL_MASTER_NO) {
2332 debug("Stale control socket %.100s, unlinking", path);
2334 } else if (errno == ENOENT) {
2335 debug("Control socket \"%.100s\" does not exist", path);
2337 error("Control socket connect(%.100s): %s", path,
2345 if (mux_client_hello_exchange(sock) != 0) {
2346 error("%s: master hello exchange failed", __func__);
2351 switch (muxclient_command) {
2352 case SSHMUX_COMMAND_ALIVE_CHECK:
2353 if ((pid = mux_client_request_alive(sock)) == 0)
2354 fatal("%s: master alive check failed", __func__);
2355 fprintf(stderr, "Master running (pid=%u)\r\n", pid);
2357 case SSHMUX_COMMAND_TERMINATE:
2358 mux_client_request_terminate(sock);
2359 if (options.log_level != SYSLOG_LEVEL_QUIET)
2360 fprintf(stderr, "Exit request sent.\r\n");
2362 case SSHMUX_COMMAND_FORWARD:
2363 if (mux_client_forwards(sock, 0) != 0)
2364 fatal("%s: master forward request failed", __func__);
2366 case SSHMUX_COMMAND_OPEN:
2367 if (mux_client_forwards(sock, 0) != 0) {
2368 error("%s: master forward request failed", __func__);
2371 mux_client_request_session(sock);
2373 case SSHMUX_COMMAND_STDIO_FWD:
2374 mux_client_request_stdio_fwd(sock);
2376 case SSHMUX_COMMAND_STOP:
2377 mux_client_request_stop_listening(sock);
2378 if (options.log_level != SYSLOG_LEVEL_QUIET)
2379 fprintf(stderr, "Stop listening request sent.\r\n");
2381 case SSHMUX_COMMAND_CANCEL_FWD:
2382 if (mux_client_forwards(sock, 1) != 0)
2383 error("%s: master cancel forward request failed",
2386 case SSHMUX_COMMAND_PROXY:
2387 mux_client_proxy(sock);
2390 fatal("unrecognised muxclient_command %d", muxclient_command);