1 /* $OpenBSD: mux.c,v 1.79 2019/01/19 21:35:25 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 */
22 #include <sys/types.h>
24 #include <sys/socket.h>
43 # ifdef HAVE_SYS_POLL_H
44 # include <sys/poll.h>
52 #include "openbsd-compat/sys-queue.h"
57 #include "pathnames.h"
64 #include "monitor_fdpass.h"
68 #include "clientloop.h"
73 extern Options options;
74 extern int stdin_null_flag;
76 extern int subsystem_flag;
77 extern struct sshbuf *command;
78 extern volatile sig_atomic_t quit_pending;
80 /* Context for session open confirmation callback */
81 struct mux_session_confirm_ctx {
93 /* Context for stdio fwd open confirmation callback */
94 struct mux_stdio_confirm_ctx {
98 /* Context for global channel callback */
99 struct mux_channel_confirm_ctx {
100 u_int cid; /* channel id */
101 u_int rid; /* request id */
102 int fid; /* forward id */
105 /* fd to control socket */
106 int muxserver_sock = -1;
108 /* client request id */
109 u_int muxclient_request_id = 0;
111 /* Multiplexing control command */
112 u_int muxclient_command = 0;
114 /* Set when signalled. */
115 static volatile sig_atomic_t muxclient_terminate = 0;
117 /* PID of multiplex server */
118 static u_int muxserver_pid = 0;
120 static Channel *mux_listener_channel = NULL;
122 struct mux_master_state {
126 /* mux protocol messages */
127 #define MUX_MSG_HELLO 0x00000001
128 #define MUX_C_NEW_SESSION 0x10000002
129 #define MUX_C_ALIVE_CHECK 0x10000004
130 #define MUX_C_TERMINATE 0x10000005
131 #define MUX_C_OPEN_FWD 0x10000006
132 #define MUX_C_CLOSE_FWD 0x10000007
133 #define MUX_C_NEW_STDIO_FWD 0x10000008
134 #define MUX_C_STOP_LISTENING 0x10000009
135 #define MUX_C_PROXY 0x1000000f
136 #define MUX_S_OK 0x80000001
137 #define MUX_S_PERMISSION_DENIED 0x80000002
138 #define MUX_S_FAILURE 0x80000003
139 #define MUX_S_EXIT_MESSAGE 0x80000004
140 #define MUX_S_ALIVE 0x80000005
141 #define MUX_S_SESSION_OPENED 0x80000006
142 #define MUX_S_REMOTE_PORT 0x80000007
143 #define MUX_S_TTY_ALLOC_FAIL 0x80000008
144 #define MUX_S_PROXY 0x8000000f
146 /* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
147 #define MUX_FWD_LOCAL 1
148 #define MUX_FWD_REMOTE 2
149 #define MUX_FWD_DYNAMIC 3
151 static void mux_session_confirm(struct ssh *, int, int, void *);
152 static void mux_stdio_confirm(struct ssh *, int, int, void *);
154 static int mux_master_process_hello(struct ssh *, u_int,
155 Channel *, struct sshbuf *, struct sshbuf *);
156 static int mux_master_process_new_session(struct ssh *, u_int,
157 Channel *, struct sshbuf *, struct sshbuf *);
158 static int mux_master_process_alive_check(struct ssh *, u_int,
159 Channel *, struct sshbuf *, struct sshbuf *);
160 static int mux_master_process_terminate(struct ssh *, u_int,
161 Channel *, struct sshbuf *, struct sshbuf *);
162 static int mux_master_process_open_fwd(struct ssh *, u_int,
163 Channel *, struct sshbuf *, struct sshbuf *);
164 static int mux_master_process_close_fwd(struct ssh *, u_int,
165 Channel *, struct sshbuf *, struct sshbuf *);
166 static int mux_master_process_stdio_fwd(struct ssh *, u_int,
167 Channel *, struct sshbuf *, struct sshbuf *);
168 static int mux_master_process_stop_listening(struct ssh *, u_int,
169 Channel *, struct sshbuf *, struct sshbuf *);
170 static int mux_master_process_proxy(struct ssh *, u_int,
171 Channel *, struct sshbuf *, struct sshbuf *);
173 static const struct {
175 int (*handler)(struct ssh *, u_int, Channel *,
176 struct sshbuf *, struct sshbuf *);
177 } mux_master_handlers[] = {
178 { MUX_MSG_HELLO, mux_master_process_hello },
179 { MUX_C_NEW_SESSION, mux_master_process_new_session },
180 { MUX_C_ALIVE_CHECK, mux_master_process_alive_check },
181 { MUX_C_TERMINATE, mux_master_process_terminate },
182 { MUX_C_OPEN_FWD, mux_master_process_open_fwd },
183 { MUX_C_CLOSE_FWD, mux_master_process_close_fwd },
184 { MUX_C_NEW_STDIO_FWD, mux_master_process_stdio_fwd },
185 { MUX_C_STOP_LISTENING, mux_master_process_stop_listening },
186 { MUX_C_PROXY, mux_master_process_proxy },
190 /* Cleanup callback fired on closure of mux slave _session_ channel */
193 mux_master_session_cleanup_cb(struct ssh *ssh, int cid, void *unused)
195 Channel *cc, *c = channel_by_id(ssh, cid);
197 debug3("%s: entering for channel %d", __func__, cid);
199 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
200 if (c->ctl_chan != -1) {
201 if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
202 fatal("%s: channel %d missing control channel %d",
203 __func__, c->self, c->ctl_chan);
206 cc->have_remote_id = 0;
207 chan_rcvd_oclose(ssh, cc);
209 channel_cancel_cleanup(ssh, c->self);
212 /* Cleanup callback fired on closure of mux slave _control_ channel */
215 mux_master_control_cleanup_cb(struct ssh *ssh, int cid, void *unused)
217 Channel *sc, *c = channel_by_id(ssh, cid);
219 debug3("%s: entering for channel %d", __func__, cid);
221 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
222 if (c->have_remote_id) {
223 if ((sc = channel_by_id(ssh, c->remote_id)) == NULL)
224 fatal("%s: channel %d missing session channel %u",
225 __func__, c->self, c->remote_id);
227 c->have_remote_id = 0;
229 if (sc->type != SSH_CHANNEL_OPEN &&
230 sc->type != SSH_CHANNEL_OPENING) {
231 debug2("%s: channel %d: not open", __func__, sc->self);
232 chan_mark_dead(ssh, sc);
234 if (sc->istate == CHAN_INPUT_OPEN)
235 chan_read_failed(ssh, sc);
236 if (sc->ostate == CHAN_OUTPUT_OPEN)
237 chan_write_failed(ssh, sc);
240 channel_cancel_cleanup(ssh, c->self);
243 /* Check mux client environment variables before passing them to mux master. */
245 env_permitted(char *env)
248 char name[1024], *cp;
250 if ((cp = strchr(env, '=')) == NULL || cp == env)
252 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
253 if (ret <= 0 || (size_t)ret >= sizeof(name)) {
254 error("%s: name '%.100s...' too long", __func__, env);
258 for (i = 0; i < options.num_send_env; i++)
259 if (match_pattern(name, options.send_env[i]))
265 /* Mux master protocol message handlers */
268 mux_master_process_hello(struct ssh *ssh, u_int rid,
269 Channel *c, struct sshbuf *m, struct sshbuf *reply)
272 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
276 fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self);
277 if (state->hello_rcvd) {
278 error("%s: HELLO received twice", __func__);
281 if ((r = sshbuf_get_u32(m, &ver)) != 0) {
282 error("%s: malformed message: %s", __func__, ssh_err(r));
285 if (ver != SSHMUX_VER) {
286 error("%s: unsupported multiplexing protocol version %u "
287 "(expected %u)", __func__, ver, SSHMUX_VER);
290 debug2("%s: channel %d slave version %u", __func__, c->self, ver);
292 /* No extensions are presently defined */
293 while (sshbuf_len(m) > 0) {
295 size_t value_len = 0;
297 if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
298 (r = sshbuf_get_string_direct(m, NULL, &value_len)) != 0) {
299 error("%s: malformed extension: %s",
300 __func__, ssh_err(r));
303 debug2("%s: Unrecognised extension \"%s\" length %zu",
304 __func__, name, value_len);
307 state->hello_rcvd = 1;
311 /* Enqueue a "ok" response to the reply buffer */
313 reply_ok(struct sshbuf *reply, u_int rid)
317 if ((r = sshbuf_put_u32(reply, MUX_S_OK)) != 0 ||
318 (r = sshbuf_put_u32(reply, rid)) != 0)
319 fatal("%s: reply: %s", __func__, ssh_err(r));
322 /* Enqueue an error response to the reply buffer */
324 reply_error(struct sshbuf *reply, u_int type, u_int rid, const char *msg)
328 if ((r = sshbuf_put_u32(reply, type)) != 0 ||
329 (r = sshbuf_put_u32(reply, rid)) != 0 ||
330 (r = sshbuf_put_cstring(reply, msg)) != 0)
331 fatal("%s: reply: %s", __func__, ssh_err(r));
335 mux_master_process_new_session(struct ssh *ssh, u_int rid,
336 Channel *c, struct sshbuf *m, struct sshbuf *reply)
339 struct mux_session_confirm_ctx *cctx;
341 u_int i, j, env_len, escape_char, window, packetmax;
344 /* Reply for SSHMUX_COMMAND_OPEN */
345 cctx = xcalloc(1, sizeof(*cctx));
351 if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
352 (r = sshbuf_get_u32(m, &cctx->want_tty)) != 0 ||
353 (r = sshbuf_get_u32(m, &cctx->want_x_fwd)) != 0 ||
354 (r = sshbuf_get_u32(m, &cctx->want_agent_fwd)) != 0 ||
355 (r = sshbuf_get_u32(m, &cctx->want_subsys)) != 0 ||
356 (r = sshbuf_get_u32(m, &escape_char)) != 0 ||
357 (r = sshbuf_get_cstring(m, &cctx->term, NULL)) != 0 ||
358 (r = sshbuf_get_cstring(m, &cmd, NULL)) != 0) {
361 for (j = 0; j < env_len; j++)
366 error("%s: malformed message", __func__);
370 #define MUX_MAX_ENV_VARS 4096
371 while (sshbuf_len(m) > 0) {
372 if ((r = sshbuf_get_cstring(m, &cp, NULL)) != 0)
374 if (!env_permitted(cp)) {
378 cctx->env = xreallocarray(cctx->env, env_len + 2,
380 cctx->env[env_len++] = cp;
381 cctx->env[env_len] = NULL;
382 if (env_len > MUX_MAX_ENV_VARS) {
383 error("%s: >%d environment variables received, "
384 "ignoring additional", __func__, MUX_MAX_ENV_VARS);
389 debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
390 "term \"%s\", cmd \"%s\", env %u", __func__, c->self,
391 cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
392 cctx->want_subsys, cctx->term, cmd, env_len);
394 if ((cctx->cmd = sshbuf_new()) == NULL)
395 fatal("%s: sshbuf_new", __func__);
396 if ((r = sshbuf_put(cctx->cmd, cmd, strlen(cmd))) != 0)
397 fatal("%s: sshbuf_put: %s", __func__, ssh_err(r));
401 /* Gather fds from client */
402 for(i = 0; i < 3; i++) {
403 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
404 error("%s: failed to receive fd %d from slave",
406 for (j = 0; j < i; j++)
408 for (j = 0; j < env_len; j++)
412 sshbuf_free(cctx->cmd);
414 reply_error(reply, MUX_S_FAILURE, rid,
415 "did not receive file descriptors");
420 debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
421 new_fd[0], new_fd[1], new_fd[2]);
423 /* XXX support multiple child sessions in future */
424 if (c->have_remote_id) {
425 debug2("%s: session already open", __func__);
426 reply_error(reply, MUX_S_FAILURE, rid,
427 "Multiple sessions not supported");
434 for (i = 0; i < env_len; i++)
438 sshbuf_free(cctx->cmd);
443 if (options.control_master == SSHCTL_MASTER_ASK ||
444 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
445 if (!ask_permission("Allow shared connection to %s? ", host)) {
446 debug2("%s: session refused by user", __func__);
447 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
448 "Permission denied");
453 /* Try to pick up ttymodes from client before it goes raw */
454 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
455 error("%s: tcgetattr: %s", __func__, strerror(errno));
457 /* enable nonblocking unless tty */
458 if (!isatty(new_fd[0]))
459 set_nonblock(new_fd[0]);
460 if (!isatty(new_fd[1]))
461 set_nonblock(new_fd[1]);
462 if (!isatty(new_fd[2]))
463 set_nonblock(new_fd[2]);
465 window = CHAN_SES_WINDOW_DEFAULT;
466 packetmax = CHAN_SES_PACKET_DEFAULT;
467 if (cctx->want_tty) {
472 nc = channel_new(ssh, "session", SSH_CHANNEL_OPENING,
473 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
474 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
476 nc->ctl_chan = c->self; /* link session -> control channel */
477 c->remote_id = nc->self; /* link control -> session channel */
478 c->have_remote_id = 1;
480 if (cctx->want_tty && escape_char != 0xffffffff) {
481 channel_register_filter(ssh, nc->self,
482 client_simple_escape_filter, NULL,
483 client_filter_cleanup,
484 client_new_escape_filter_ctx((int)escape_char));
487 debug2("%s: channel_new: %d linked to control channel %d",
488 __func__, nc->self, nc->ctl_chan);
490 channel_send_open(ssh, nc->self);
491 channel_register_open_confirm(ssh, nc->self, mux_session_confirm, cctx);
492 c->mux_pause = 1; /* stop handling messages until open_confirm done */
493 channel_register_cleanup(ssh, nc->self,
494 mux_master_session_cleanup_cb, 1);
496 /* reply is deferred, sent by mux_session_confirm */
501 mux_master_process_alive_check(struct ssh *ssh, u_int rid,
502 Channel *c, struct sshbuf *m, struct sshbuf *reply)
506 debug2("%s: channel %d: alive check", __func__, c->self);
509 if ((r = sshbuf_put_u32(reply, MUX_S_ALIVE)) != 0 ||
510 (r = sshbuf_put_u32(reply, rid)) != 0 ||
511 (r = sshbuf_put_u32(reply, (u_int)getpid())) != 0)
512 fatal("%s: reply: %s", __func__, ssh_err(r));
518 mux_master_process_terminate(struct ssh *ssh, u_int rid,
519 Channel *c, struct sshbuf *m, struct sshbuf *reply)
521 debug2("%s: channel %d: terminate request", __func__, c->self);
523 if (options.control_master == SSHCTL_MASTER_ASK ||
524 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
525 if (!ask_permission("Terminate shared connection to %s? ",
527 debug2("%s: termination refused by user", __func__);
528 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
529 "Permission denied");
535 reply_ok(reply, rid);
536 /* XXX exit happens too soon - message never makes it to client */
541 format_forward(u_int ftype, struct Forward *fwd)
547 xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
548 (fwd->listen_path != NULL) ? fwd->listen_path :
549 (fwd->listen_host == NULL) ?
550 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
551 fwd->listen_host, fwd->listen_port,
552 (fwd->connect_path != NULL) ? fwd->connect_path :
553 fwd->connect_host, fwd->connect_port);
555 case MUX_FWD_DYNAMIC:
556 xasprintf(&ret, "dynamic forward %.200s:%d -> *",
557 (fwd->listen_host == NULL) ?
558 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
559 fwd->listen_host, fwd->listen_port);
562 xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
563 (fwd->listen_path != NULL) ? fwd->listen_path :
564 (fwd->listen_host == NULL) ?
565 "LOCALHOST" : fwd->listen_host,
567 (fwd->connect_path != NULL) ? fwd->connect_path :
568 fwd->connect_host, fwd->connect_port);
571 fatal("%s: unknown forward type %u", __func__, ftype);
577 compare_host(const char *a, const char *b)
579 if (a == NULL && b == NULL)
581 if (a == NULL || b == NULL)
583 return strcmp(a, b) == 0;
587 compare_forward(struct Forward *a, struct Forward *b)
589 if (!compare_host(a->listen_host, b->listen_host))
591 if (!compare_host(a->listen_path, b->listen_path))
593 if (a->listen_port != b->listen_port)
595 if (!compare_host(a->connect_host, b->connect_host))
597 if (!compare_host(a->connect_path, b->connect_path))
599 if (a->connect_port != b->connect_port)
606 mux_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
608 struct mux_channel_confirm_ctx *fctx = ctxt;
609 char *failmsg = NULL;
610 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 if ((r = sshpkt_get_u32(ssh, &port)) != 0)
637 fatal("%s: packet error: %s",
638 __func__, ssh_err(r));
640 fatal("Invalid allocated port %u for "
641 "mux remote forward to %s:%d", port,
642 rfwd->connect_host, rfwd->connect_port);
644 rfwd->allocated_port = (int)port;
645 debug("Allocated port %u for mux remote forward"
646 " to %s:%d", rfwd->allocated_port,
647 rfwd->connect_host, rfwd->connect_port);
648 if ((r = sshbuf_put_u32(out,
649 MUX_S_REMOTE_PORT)) != 0 ||
650 (r = sshbuf_put_u32(out, fctx->rid)) != 0 ||
651 (r = sshbuf_put_u32(out,
652 rfwd->allocated_port)) != 0)
653 fatal("%s: reply: %s", __func__, ssh_err(r));
654 channel_update_permission(ssh, rfwd->handle,
655 rfwd->allocated_port);
657 reply_ok(out, fctx->rid);
661 if (rfwd->listen_port == 0)
662 channel_update_permission(ssh, rfwd->handle, -1);
663 if (rfwd->listen_path != NULL)
664 xasprintf(&failmsg, "remote port forwarding failed for "
665 "listen path %s", rfwd->listen_path);
667 xasprintf(&failmsg, "remote port forwarding failed for "
668 "listen port %d", rfwd->listen_port);
670 debug2("%s: clearing registered forwarding for listen %d, "
671 "connect %s:%d", __func__, rfwd->listen_port,
672 rfwd->connect_path ? rfwd->connect_path :
673 rfwd->connect_host, rfwd->connect_port);
675 free(rfwd->listen_host);
676 free(rfwd->listen_path);
677 free(rfwd->connect_host);
678 free(rfwd->connect_path);
679 memset(rfwd, 0, sizeof(*rfwd));
682 error("%s: %s", __func__, failmsg);
683 reply_error(out, MUX_S_FAILURE, fctx->rid, failmsg);
686 if ((r = sshbuf_put_stringb(c->output, out)) != 0)
687 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
689 if (c->mux_pause <= 0)
690 fatal("%s: mux_pause %d", __func__, c->mux_pause);
691 c->mux_pause = 0; /* start processing messages again */
695 mux_master_process_open_fwd(struct ssh *ssh, u_int rid,
696 Channel *c, struct sshbuf *m, struct sshbuf *reply)
699 char *fwd_desc = NULL;
700 char *listen_addr, *connect_addr;
703 int r, i, ret = 0, freefwd = 1;
705 memset(&fwd, 0, sizeof(fwd));
707 /* XXX - lport/cport check redundant */
708 if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
709 (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
710 (r = sshbuf_get_u32(m, &lport)) != 0 ||
711 (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
712 (r = sshbuf_get_u32(m, &cport)) != 0 ||
713 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
714 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
715 error("%s: malformed message", __func__);
719 if (*listen_addr == '\0') {
723 if (*connect_addr == '\0') {
728 memset(&fwd, 0, sizeof(fwd));
729 fwd.listen_port = lport;
730 if (fwd.listen_port == PORT_STREAMLOCAL)
731 fwd.listen_path = listen_addr;
733 fwd.listen_host = listen_addr;
734 fwd.connect_port = cport;
735 if (fwd.connect_port == PORT_STREAMLOCAL)
736 fwd.connect_path = connect_addr;
738 fwd.connect_host = connect_addr;
740 debug2("%s: channel %d: request %s", __func__, c->self,
741 (fwd_desc = format_forward(ftype, &fwd)));
743 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
744 ftype != MUX_FWD_DYNAMIC) {
745 logit("%s: invalid forwarding type %u", __func__, ftype);
749 reply_error(reply, MUX_S_FAILURE, rid,
750 "Invalid forwarding request");
753 if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) {
754 logit("%s: streamlocal and dynamic forwards "
755 "are mutually exclusive", __func__);
758 if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) {
759 logit("%s: invalid listen port %u", __func__,
763 if ((fwd.connect_port != PORT_STREAMLOCAL &&
764 fwd.connect_port >= 65536) ||
765 (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE &&
766 fwd.connect_port == 0)) {
767 logit("%s: invalid connect port %u", __func__,
771 if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL &&
772 fwd.connect_path == NULL) {
773 logit("%s: missing connect host", __func__);
777 /* Skip forwards that have already been requested */
780 case MUX_FWD_DYNAMIC:
781 for (i = 0; i < options.num_local_forwards; i++) {
782 if (compare_forward(&fwd,
783 options.local_forwards + i)) {
785 debug2("%s: found existing forwarding",
787 reply_ok(reply, rid);
793 for (i = 0; i < options.num_remote_forwards; i++) {
794 if (!compare_forward(&fwd, options.remote_forwards + i))
796 if (fwd.listen_port != 0)
798 debug2("%s: found allocated port", __func__);
799 if ((r = sshbuf_put_u32(reply,
800 MUX_S_REMOTE_PORT)) != 0 ||
801 (r = sshbuf_put_u32(reply, rid)) != 0 ||
802 (r = sshbuf_put_u32(reply,
803 options.remote_forwards[i].allocated_port)) != 0)
804 fatal("%s: reply: %s", __func__, ssh_err(r));
810 if (options.control_master == SSHCTL_MASTER_ASK ||
811 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
812 if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
813 debug2("%s: forwarding refused by user", __func__);
814 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
815 "Permission denied");
820 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
821 if (!channel_setup_local_fwd_listener(ssh, &fwd,
822 &options.fwd_opts)) {
824 logit("%s: requested %s failed", __func__, fwd_desc);
825 reply_error(reply, MUX_S_FAILURE, rid,
826 "Port forwarding failed");
829 add_local_forward(&options, &fwd);
832 struct mux_channel_confirm_ctx *fctx;
834 fwd.handle = channel_request_remote_forwarding(ssh, &fwd);
837 add_remote_forward(&options, &fwd);
838 fctx = xcalloc(1, sizeof(*fctx));
841 fctx->fid = options.num_remote_forwards - 1;
842 client_register_global_confirm(mux_confirm_remote_forward,
845 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
846 /* delayed reply in mux_confirm_remote_forward */
849 reply_ok(reply, rid);
853 free(fwd.listen_host);
854 free(fwd.listen_path);
855 free(fwd.connect_host);
856 free(fwd.connect_path);
862 mux_master_process_close_fwd(struct ssh *ssh, u_int rid,
863 Channel *c, struct sshbuf *m, struct sshbuf *reply)
865 struct Forward fwd, *found_fwd;
866 char *fwd_desc = NULL;
867 const char *error_reason = NULL;
868 char *listen_addr = NULL, *connect_addr = NULL;
873 memset(&fwd, 0, sizeof(fwd));
875 if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
876 (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
877 (r = sshbuf_get_u32(m, &lport)) != 0 ||
878 (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
879 (r = sshbuf_get_u32(m, &cport)) != 0 ||
880 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
881 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
882 error("%s: malformed message", __func__);
887 if (*listen_addr == '\0') {
891 if (*connect_addr == '\0') {
896 memset(&fwd, 0, sizeof(fwd));
897 fwd.listen_port = lport;
898 if (fwd.listen_port == PORT_STREAMLOCAL)
899 fwd.listen_path = listen_addr;
901 fwd.listen_host = listen_addr;
902 fwd.connect_port = cport;
903 if (fwd.connect_port == PORT_STREAMLOCAL)
904 fwd.connect_path = connect_addr;
906 fwd.connect_host = connect_addr;
908 debug2("%s: channel %d: request cancel %s", __func__, c->self,
909 (fwd_desc = format_forward(ftype, &fwd)));
911 /* make sure this has been requested */
915 case MUX_FWD_DYNAMIC:
916 for (i = 0; i < options.num_local_forwards; i++) {
917 if (compare_forward(&fwd,
918 options.local_forwards + i)) {
919 found_fwd = options.local_forwards + i;
925 for (i = 0; i < options.num_remote_forwards; i++) {
926 if (compare_forward(&fwd,
927 options.remote_forwards + i)) {
928 found_fwd = options.remote_forwards + i;
935 if (found_fwd == NULL)
936 error_reason = "port not forwarded";
937 else if (ftype == MUX_FWD_REMOTE) {
939 * This shouldn't fail unless we confused the host/port
940 * between options.remote_forwards and permitted_opens.
941 * However, for dynamic allocated listen ports we need
942 * to use the actual listen port.
944 if (channel_request_rforward_cancel(ssh, found_fwd) == -1)
945 error_reason = "port not in permitted opens";
946 } else { /* local and dynamic forwards */
948 if (channel_cancel_lport_listener(ssh, &fwd, fwd.connect_port,
949 &options.fwd_opts) == -1)
950 error_reason = "port not found";
953 if (error_reason != NULL)
954 reply_error(reply, MUX_S_FAILURE, rid, error_reason);
956 reply_ok(reply, rid);
957 free(found_fwd->listen_host);
958 free(found_fwd->listen_path);
959 free(found_fwd->connect_host);
960 free(found_fwd->connect_path);
961 found_fwd->listen_host = found_fwd->connect_host = NULL;
962 found_fwd->listen_path = found_fwd->connect_path = NULL;
963 found_fwd->listen_port = found_fwd->connect_port = 0;
974 mux_master_process_stdio_fwd(struct ssh *ssh, u_int rid,
975 Channel *c, struct sshbuf *m, struct sshbuf *reply)
981 struct mux_stdio_confirm_ctx *cctx;
983 if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
984 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
985 (r = sshbuf_get_u32(m, &cport)) != 0) {
987 error("%s: malformed message", __func__);
991 debug2("%s: channel %d: request stdio fwd to %s:%u",
992 __func__, c->self, chost, cport);
994 /* Gather fds from client */
995 for(i = 0; i < 2; i++) {
996 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
997 error("%s: failed to receive fd %d from slave",
999 for (j = 0; j < i; j++)
1004 reply_error(reply, MUX_S_FAILURE, rid,
1005 "did not receive file descriptors");
1010 debug3("%s: got fds stdin %d, stdout %d", __func__,
1011 new_fd[0], new_fd[1]);
1013 /* XXX support multiple child sessions in future */
1014 if (c->have_remote_id) {
1015 debug2("%s: session already open", __func__);
1016 reply_error(reply, MUX_S_FAILURE, rid,
1017 "Multiple sessions not supported");
1025 if (options.control_master == SSHCTL_MASTER_ASK ||
1026 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
1027 if (!ask_permission("Allow forward to %s:%u? ",
1029 debug2("%s: stdio fwd refused by user", __func__);
1030 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
1031 "Permission denied");
1036 /* enable nonblocking unless tty */
1037 if (!isatty(new_fd[0]))
1038 set_nonblock(new_fd[0]);
1039 if (!isatty(new_fd[1]))
1040 set_nonblock(new_fd[1]);
1042 nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1]);
1045 nc->ctl_chan = c->self; /* link session -> control channel */
1046 c->remote_id = nc->self; /* link control -> session channel */
1047 c->have_remote_id = 1;
1049 debug2("%s: channel_new: %d linked to control channel %d",
1050 __func__, nc->self, nc->ctl_chan);
1052 channel_register_cleanup(ssh, nc->self,
1053 mux_master_session_cleanup_cb, 1);
1055 cctx = xcalloc(1, sizeof(*cctx));
1057 channel_register_open_confirm(ssh, nc->self, mux_stdio_confirm, cctx);
1058 c->mux_pause = 1; /* stop handling messages until open_confirm done */
1060 /* reply is deferred, sent by mux_session_confirm */
1064 /* Callback on open confirmation in mux master for a mux stdio fwd session. */
1066 mux_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
1068 struct mux_stdio_confirm_ctx *cctx = arg;
1070 struct sshbuf *reply;
1074 fatal("%s: cctx == NULL", __func__);
1075 if ((c = channel_by_id(ssh, id)) == NULL)
1076 fatal("%s: no channel for id %d", __func__, id);
1077 if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
1078 fatal("%s: channel %d lacks control channel %d", __func__,
1080 if ((reply = sshbuf_new()) == NULL)
1081 fatal("%s: sshbuf_new", __func__);
1084 debug3("%s: sending failure reply", __func__);
1085 reply_error(reply, MUX_S_FAILURE, cctx->rid,
1086 "Session open refused by peer");
1091 debug3("%s: sending success reply", __func__);
1093 if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
1094 (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
1095 (r = sshbuf_put_u32(reply, c->self)) != 0)
1096 fatal("%s: reply: %s", __func__, ssh_err(r));
1100 if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
1101 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
1104 if (cc->mux_pause <= 0)
1105 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1106 cc->mux_pause = 0; /* start processing messages again */
1107 c->open_confirm_ctx = NULL;
1112 mux_master_process_stop_listening(struct ssh *ssh, u_int rid,
1113 Channel *c, struct sshbuf *m, struct sshbuf *reply)
1115 debug("%s: channel %d: stop listening", __func__, c->self);
1117 if (options.control_master == SSHCTL_MASTER_ASK ||
1118 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
1119 if (!ask_permission("Disable further multiplexing on shared "
1120 "connection to %s? ", host)) {
1121 debug2("%s: stop listen refused by user", __func__);
1122 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
1123 "Permission denied");
1128 if (mux_listener_channel != NULL) {
1129 channel_free(ssh, mux_listener_channel);
1131 free(options.control_path);
1132 options.control_path = NULL;
1133 mux_listener_channel = NULL;
1134 muxserver_sock = -1;
1137 reply_ok(reply, rid);
1142 mux_master_process_proxy(struct ssh *ssh, u_int rid,
1143 Channel *c, struct sshbuf *m, struct sshbuf *reply)
1147 debug("%s: channel %d: proxy request", __func__, c->self);
1149 c->mux_rcb = channel_proxy_downstream;
1150 if ((r = sshbuf_put_u32(reply, MUX_S_PROXY)) != 0 ||
1151 (r = sshbuf_put_u32(reply, rid)) != 0)
1152 fatal("%s: reply: %s", __func__, ssh_err(r));
1157 /* Channel callbacks fired on read/write from mux slave fd */
1159 mux_master_read_cb(struct ssh *ssh, Channel *c)
1161 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
1162 struct sshbuf *in = NULL, *out = NULL;
1166 if ((out = sshbuf_new()) == NULL)
1167 fatal("%s: sshbuf_new", __func__);
1170 if (c->mux_ctx == NULL) {
1171 state = xcalloc(1, sizeof(*state));
1173 channel_register_cleanup(ssh, c->self,
1174 mux_master_control_cleanup_cb, 0);
1177 if ((r = sshbuf_put_u32(out, MUX_MSG_HELLO)) != 0 ||
1178 (r = sshbuf_put_u32(out, SSHMUX_VER)) != 0)
1179 fatal("%s: reply: %s", __func__, ssh_err(r));
1181 if ((r = sshbuf_put_stringb(c->output, out)) != 0)
1182 fatal("%s: sshbuf_put_stringb: %s",
1183 __func__, ssh_err(r));
1184 debug3("%s: channel %d: hello sent", __func__, c->self);
1189 /* Channel code ensures that we receive whole packets */
1190 if ((r = sshbuf_froms(c->input, &in)) != 0) {
1192 error("%s: malformed message", __func__);
1196 if ((r = sshbuf_get_u32(in, &type)) != 0)
1198 debug3("%s: channel %d packet type 0x%08x len %zu",
1199 __func__, c->self, type, sshbuf_len(in));
1201 if (type == MUX_MSG_HELLO)
1204 if (!state->hello_rcvd) {
1205 error("%s: expected MUX_MSG_HELLO(0x%08x), "
1206 "received 0x%08x", __func__, MUX_MSG_HELLO, type);
1209 if ((r = sshbuf_get_u32(in, &rid)) != 0)
1213 for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
1214 if (type == mux_master_handlers[i].type) {
1215 ret = mux_master_handlers[i].handler(ssh, rid,
1220 if (mux_master_handlers[i].handler == NULL) {
1221 error("%s: unsupported mux message 0x%08x", __func__, type);
1222 reply_error(out, MUX_S_FAILURE, rid, "unsupported request");
1225 /* Enqueue reply packet */
1226 if (sshbuf_len(out) != 0) {
1227 if ((r = sshbuf_put_stringb(c->output, out)) != 0)
1228 fatal("%s: sshbuf_put_stringb: %s",
1229 __func__, ssh_err(r));
1238 mux_exit_message(struct ssh *ssh, Channel *c, int exitval)
1244 debug3("%s: channel %d: exit message, exitval %d", __func__, c->self,
1247 if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
1248 fatal("%s: channel %d missing mux channel %d",
1249 __func__, c->self, c->ctl_chan);
1251 /* Append exit message packet to control socket output queue */
1252 if ((m = sshbuf_new()) == NULL)
1253 fatal("%s: sshbuf_new", __func__);
1254 if ((r = sshbuf_put_u32(m, MUX_S_EXIT_MESSAGE)) != 0 ||
1255 (r = sshbuf_put_u32(m, c->self)) != 0 ||
1256 (r = sshbuf_put_u32(m, exitval)) != 0 ||
1257 (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
1258 fatal("%s: reply: %s", __func__, ssh_err(r));
1263 mux_tty_alloc_failed(struct ssh *ssh, Channel *c)
1269 debug3("%s: channel %d: TTY alloc failed", __func__, c->self);
1271 if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
1272 fatal("%s: channel %d missing mux channel %d",
1273 __func__, c->self, c->ctl_chan);
1275 /* Append exit message packet to control socket output queue */
1276 if ((m = sshbuf_new()) == NULL)
1277 fatal("%s: sshbuf_new", __func__);
1278 if ((r = sshbuf_put_u32(m, MUX_S_TTY_ALLOC_FAIL)) != 0 ||
1279 (r = sshbuf_put_u32(m, c->self)) != 0 ||
1280 (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
1281 fatal("%s: reply: %s", __func__, ssh_err(r));
1285 /* Prepare a mux master to listen on a Unix domain socket. */
1287 muxserver_listen(struct ssh *ssh)
1290 char *orig_control_path = options.control_path;
1295 if (options.control_path == NULL ||
1296 options.control_master == SSHCTL_MASTER_NO)
1299 debug("setting up multiplex master socket");
1302 * Use a temporary path before listen so we can pseudo-atomically
1303 * establish the listening socket in its final location to avoid
1304 * other processes racing in between bind() and listen() and hitting
1305 * an unready socket.
1307 for (i = 0; i < sizeof(rbuf) - 1; i++) {
1308 r = arc4random_uniform(26+26+10);
1309 rbuf[i] = (r < 26) ? 'a' + r :
1310 (r < 26*2) ? 'A' + r - 26 :
1313 rbuf[sizeof(rbuf) - 1] = '\0';
1314 options.control_path = NULL;
1315 xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
1316 debug3("%s: temporary control path %s", __func__, options.control_path);
1318 old_umask = umask(0177);
1319 muxserver_sock = unix_listener(options.control_path, 64, 0);
1322 if (muxserver_sock < 0) {
1323 if (oerrno == EINVAL || oerrno == EADDRINUSE) {
1324 error("ControlSocket %s already exists, "
1325 "disabling multiplexing", options.control_path);
1327 if (muxserver_sock != -1) {
1328 close(muxserver_sock);
1329 muxserver_sock = -1;
1331 free(orig_control_path);
1332 free(options.control_path);
1333 options.control_path = NULL;
1334 options.control_master = SSHCTL_MASTER_NO;
1337 /* unix_listener() logs the error */
1342 /* Now atomically "move" the mux socket into position */
1343 if (link(options.control_path, orig_control_path) != 0) {
1344 if (errno != EEXIST) {
1345 fatal("%s: link mux listener %s => %s: %s", __func__,
1346 options.control_path, orig_control_path,
1349 error("ControlSocket %s already exists, disabling multiplexing",
1351 unlink(options.control_path);
1352 goto disable_mux_master;
1354 unlink(options.control_path);
1355 free(options.control_path);
1356 options.control_path = orig_control_path;
1358 set_nonblock(muxserver_sock);
1360 mux_listener_channel = channel_new(ssh, "mux listener",
1361 SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
1362 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1363 0, options.control_path, 1);
1364 mux_listener_channel->mux_rcb = mux_master_read_cb;
1365 debug3("%s: mux listener channel %d fd %d", __func__,
1366 mux_listener_channel->self, mux_listener_channel->sock);
1369 /* Callback on open confirmation in mux master for a mux client session. */
1371 mux_session_confirm(struct ssh *ssh, int id, int success, void *arg)
1373 struct mux_session_confirm_ctx *cctx = arg;
1374 const char *display;
1377 struct sshbuf *reply;
1380 fatal("%s: cctx == NULL", __func__);
1381 if ((c = channel_by_id(ssh, id)) == NULL)
1382 fatal("%s: no channel for id %d", __func__, id);
1383 if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
1384 fatal("%s: channel %d lacks control channel %d", __func__,
1386 if ((reply = sshbuf_new()) == NULL)
1387 fatal("%s: sshbuf_new", __func__);
1390 debug3("%s: sending failure reply", __func__);
1391 reply_error(reply, MUX_S_FAILURE, cctx->rid,
1392 "Session open refused by peer");
1396 display = getenv("DISPLAY");
1397 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1400 /* Get reasonable local authentication information. */
1401 if (client_x11_get_proto(ssh, display, options.xauth_location,
1402 options.forward_x11_trusted, options.forward_x11_timeout,
1403 &proto, &data) == 0) {
1404 /* Request forwarding with authentication spoofing. */
1405 debug("Requesting X11 forwarding with authentication "
1407 x11_request_forwarding_with_spoofing(ssh, id,
1408 display, proto, data, 1);
1409 /* XXX exit_on_forward_failure */
1410 client_expect_confirm(ssh, id, "X11 forwarding",
1415 if (cctx->want_agent_fwd && options.forward_agent) {
1416 debug("Requesting authentication agent forwarding.");
1417 channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
1418 if ((r = sshpkt_send(ssh)) != 0)
1419 fatal("%s: packet error: %s", __func__, ssh_err(r));
1422 client_session2_setup(ssh, id, cctx->want_tty, cctx->want_subsys,
1423 cctx->term, &cctx->tio, c->rfd, cctx->cmd, cctx->env);
1425 debug3("%s: sending success reply", __func__);
1427 if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
1428 (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
1429 (r = sshbuf_put_u32(reply, c->self)) != 0)
1430 fatal("%s: reply: %s", __func__, ssh_err(r));
1434 if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
1435 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
1438 if (cc->mux_pause <= 0)
1439 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1440 cc->mux_pause = 0; /* start processing messages again */
1441 c->open_confirm_ctx = NULL;
1442 sshbuf_free(cctx->cmd);
1444 if (cctx->env != NULL) {
1445 for (i = 0; cctx->env[i] != NULL; i++)
1452 /* ** Multiplexing client support */
1454 /* Exit signal handler */
1456 control_client_sighandler(int signo)
1458 muxclient_terminate = signo;
1462 * Relay signal handler - used to pass some signals from mux client to
1466 control_client_sigrelay(int signo)
1468 int save_errno = errno;
1470 if (muxserver_pid > 1)
1471 kill(muxserver_pid, signo);
1477 mux_client_read(int fd, struct sshbuf *b, size_t need)
1486 pfd.events = POLLIN;
1487 if ((r = sshbuf_reserve(b, need, &p)) != 0)
1488 fatal("%s: reserve: %s", __func__, ssh_err(r));
1489 for (have = 0; have < need; ) {
1490 if (muxclient_terminate) {
1494 len = read(fd, p + have, need - have);
1497 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1501 (void)poll(&pfd, 1, -1);
1513 have += (size_t)len;
1519 mux_client_write_packet(int fd, struct sshbuf *m)
1521 struct sshbuf *queue;
1528 pfd.events = POLLOUT;
1529 if ((queue = sshbuf_new()) == NULL)
1530 fatal("%s: sshbuf_new", __func__);
1531 if ((r = sshbuf_put_stringb(queue, m)) != 0)
1532 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
1534 need = sshbuf_len(queue);
1535 ptr = sshbuf_ptr(queue);
1537 for (have = 0; have < need; ) {
1538 if (muxclient_terminate) {
1543 len = write(fd, ptr + have, need - have);
1546 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1550 (void)poll(&pfd, 1, -1);
1573 mux_client_read_packet(int fd, struct sshbuf *m)
1575 struct sshbuf *queue;
1580 if ((queue = sshbuf_new()) == NULL)
1581 fatal("%s: sshbuf_new", __func__);
1582 if (mux_client_read(fd, queue, 4) != 0) {
1583 if ((oerrno = errno) == EPIPE)
1584 debug3("%s: read header failed: %s", __func__,
1590 need = PEEK_U32(sshbuf_ptr(queue));
1591 if (mux_client_read(fd, queue, need) != 0) {
1593 debug3("%s: read body failed: %s", __func__, strerror(errno));
1598 if ((r = sshbuf_get_string_direct(queue, &ptr, &have)) != 0 ||
1599 (r = sshbuf_put(m, ptr, have)) != 0)
1600 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1606 mux_client_hello_exchange(int fd)
1612 if ((m = sshbuf_new()) == NULL)
1613 fatal("%s: sshbuf_new", __func__);
1614 if ((r = sshbuf_put_u32(m, MUX_MSG_HELLO)) != 0 ||
1615 (r = sshbuf_put_u32(m, SSHMUX_VER)) != 0)
1616 fatal("%s: hello: %s", __func__, ssh_err(r));
1619 if (mux_client_write_packet(fd, m) != 0) {
1620 debug("%s: write packet: %s", __func__, strerror(errno));
1626 /* Read their HELLO */
1627 if (mux_client_read_packet(fd, m) != 0) {
1628 debug("%s: read packet failed", __func__);
1632 if ((r = sshbuf_get_u32(m, &type)) != 0)
1633 fatal("%s: decode type: %s", __func__, ssh_err(r));
1634 if (type != MUX_MSG_HELLO) {
1635 error("%s: expected HELLO (%u) received %u",
1636 __func__, MUX_MSG_HELLO, type);
1639 if ((r = sshbuf_get_u32(m, &ver)) != 0)
1640 fatal("%s: decode version: %s", __func__, ssh_err(r));
1641 if (ver != SSHMUX_VER) {
1642 error("Unsupported multiplexing protocol version %d "
1643 "(expected %d)", ver, SSHMUX_VER);
1646 debug2("%s: master version %u", __func__, ver);
1647 /* No extensions are presently defined */
1648 while (sshbuf_len(m) > 0) {
1651 if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
1652 (r = sshbuf_skip_string(m)) != 0) { /* value */
1653 error("%s: malformed extension: %s",
1654 __func__, ssh_err(r));
1657 debug2("Unrecognised master extension \"%s\"", name);
1668 mux_client_request_alive(int fd)
1672 u_int pid, type, rid;
1675 debug3("%s: entering", __func__);
1677 if ((m = sshbuf_new()) == NULL)
1678 fatal("%s: sshbuf_new", __func__);
1679 if ((r = sshbuf_put_u32(m, MUX_C_ALIVE_CHECK)) != 0 ||
1680 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
1681 fatal("%s: request: %s", __func__, ssh_err(r));
1683 if (mux_client_write_packet(fd, m) != 0)
1684 fatal("%s: write packet: %s", __func__, strerror(errno));
1688 /* Read their reply */
1689 if (mux_client_read_packet(fd, m) != 0) {
1694 if ((r = sshbuf_get_u32(m, &type)) != 0)
1695 fatal("%s: decode type: %s", __func__, ssh_err(r));
1696 if (type != MUX_S_ALIVE) {
1697 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1698 fatal("%s: decode error: %s", __func__, ssh_err(r));
1699 fatal("%s: master returned error: %s", __func__, e);
1702 if ((r = sshbuf_get_u32(m, &rid)) != 0)
1703 fatal("%s: decode remote ID: %s", __func__, ssh_err(r));
1704 if (rid != muxclient_request_id)
1705 fatal("%s: out of sequence reply: my id %u theirs %u",
1706 __func__, muxclient_request_id, rid);
1707 if ((r = sshbuf_get_u32(m, &pid)) != 0)
1708 fatal("%s: decode PID: %s", __func__, ssh_err(r));
1711 debug3("%s: done pid = %u", __func__, pid);
1713 muxclient_request_id++;
1719 mux_client_request_terminate(int fd)
1726 debug3("%s: entering", __func__);
1728 if ((m = sshbuf_new()) == NULL)
1729 fatal("%s: sshbuf_new", __func__);
1730 if ((r = sshbuf_put_u32(m, MUX_C_TERMINATE)) != 0 ||
1731 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
1732 fatal("%s: request: %s", __func__, ssh_err(r));
1734 if (mux_client_write_packet(fd, m) != 0)
1735 fatal("%s: write packet: %s", __func__, strerror(errno));
1739 /* Read their reply */
1740 if (mux_client_read_packet(fd, m) != 0) {
1741 /* Remote end exited already */
1742 if (errno == EPIPE) {
1746 fatal("%s: read from master failed: %s",
1747 __func__, strerror(errno));
1750 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1751 (r = sshbuf_get_u32(m, &rid)) != 0)
1752 fatal("%s: decode: %s", __func__, ssh_err(r));
1753 if (rid != muxclient_request_id)
1754 fatal("%s: out of sequence reply: my id %u theirs %u",
1755 __func__, muxclient_request_id, rid);
1759 case MUX_S_PERMISSION_DENIED:
1760 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1761 fatal("%s: decode error: %s", __func__, ssh_err(r));
1762 fatal("Master refused termination request: %s", e);
1764 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1765 fatal("%s: decode error: %s", __func__, ssh_err(r));
1766 fatal("%s: termination request failed: %s", __func__, e);
1768 fatal("%s: unexpected response from master 0x%08x",
1772 muxclient_request_id++;
1776 mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd)
1780 const char *lhost, *chost;
1784 fwd_desc = format_forward(ftype, fwd);
1785 debug("Requesting %s %s",
1786 cancel_flag ? "cancellation of" : "forwarding of", fwd_desc);
1789 type = cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD;
1790 if (fwd->listen_path != NULL)
1791 lhost = fwd->listen_path;
1792 else if (fwd->listen_host == NULL)
1794 else if (*fwd->listen_host == '\0')
1797 lhost = fwd->listen_host;
1799 if (fwd->connect_path != NULL)
1800 chost = fwd->connect_path;
1801 else if (fwd->connect_host == NULL)
1804 chost = fwd->connect_host;
1806 if ((m = sshbuf_new()) == NULL)
1807 fatal("%s: sshbuf_new", __func__);
1808 if ((r = sshbuf_put_u32(m, type)) != 0 ||
1809 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
1810 (r = sshbuf_put_u32(m, ftype)) != 0 ||
1811 (r = sshbuf_put_cstring(m, lhost)) != 0 ||
1812 (r = sshbuf_put_u32(m, fwd->listen_port)) != 0 ||
1813 (r = sshbuf_put_cstring(m, chost)) != 0 ||
1814 (r = sshbuf_put_u32(m, fwd->connect_port)) != 0)
1815 fatal("%s: request: %s", __func__, ssh_err(r));
1817 if (mux_client_write_packet(fd, m) != 0)
1818 fatal("%s: write packet: %s", __func__, strerror(errno));
1822 /* Read their reply */
1823 if (mux_client_read_packet(fd, m) != 0) {
1828 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1829 (r = sshbuf_get_u32(m, &rid)) != 0)
1830 fatal("%s: decode: %s", __func__, ssh_err(r));
1831 if (rid != muxclient_request_id)
1832 fatal("%s: out of sequence reply: my id %u theirs %u",
1833 __func__, muxclient_request_id, rid);
1838 case MUX_S_REMOTE_PORT:
1840 fatal("%s: got MUX_S_REMOTE_PORT for cancel", __func__);
1841 if ((r = sshbuf_get_u32(m, &fwd->allocated_port)) != 0)
1842 fatal("%s: decode port: %s", __func__, ssh_err(r));
1843 verbose("Allocated port %u for remote forward to %s:%d",
1844 fwd->allocated_port,
1845 fwd->connect_host ? fwd->connect_host : "",
1847 if (muxclient_command == SSHMUX_COMMAND_FORWARD)
1848 fprintf(stdout, "%i\n", fwd->allocated_port);
1850 case MUX_S_PERMISSION_DENIED:
1851 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1852 fatal("%s: decode error: %s", __func__, ssh_err(r));
1854 error("Master refused forwarding request: %s", e);
1857 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1858 fatal("%s: decode error: %s", __func__, ssh_err(r));
1860 error("%s: forwarding request failed: %s", __func__, e);
1863 fatal("%s: unexpected response from master 0x%08x",
1868 muxclient_request_id++;
1873 mux_client_forwards(int fd, int cancel_flag)
1877 debug3("%s: %s forwardings: %d local, %d remote", __func__,
1878 cancel_flag ? "cancel" : "request",
1879 options.num_local_forwards, options.num_remote_forwards);
1881 /* XXX ExitOnForwardingFailure */
1882 for (i = 0; i < options.num_local_forwards; i++) {
1883 if (mux_client_forward(fd, cancel_flag,
1884 options.local_forwards[i].connect_port == 0 ?
1885 MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
1886 options.local_forwards + i) != 0)
1889 for (i = 0; i < options.num_remote_forwards; i++) {
1890 if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE,
1891 options.remote_forwards + i) != 0)
1898 mux_client_request_session(int fd)
1903 u_int echar, rid, sid, esid, exitval, type, exitval_seen;
1904 extern char **environ;
1905 int r, i, devnull, rawmode;
1907 debug3("%s: entering", __func__);
1909 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1910 error("%s: master alive request failed", __func__);
1914 signal(SIGPIPE, SIG_IGN);
1916 if (stdin_null_flag) {
1917 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1918 fatal("open(/dev/null): %s", strerror(errno));
1919 if (dup2(devnull, STDIN_FILENO) == -1)
1920 fatal("dup2: %s", strerror(errno));
1921 if (devnull > STDERR_FILENO)
1925 if ((term = getenv("TERM")) == NULL)
1928 if (options.escape_char != SSH_ESCAPECHAR_NONE)
1929 echar = (u_int)options.escape_char;
1931 if ((m = sshbuf_new()) == NULL)
1932 fatal("%s: sshbuf_new", __func__);
1933 if ((r = sshbuf_put_u32(m, MUX_C_NEW_SESSION)) != 0 ||
1934 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
1935 (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
1936 (r = sshbuf_put_u32(m, tty_flag)) != 0 ||
1937 (r = sshbuf_put_u32(m, options.forward_x11)) != 0 ||
1938 (r = sshbuf_put_u32(m, options.forward_agent)) != 0 ||
1939 (r = sshbuf_put_u32(m, subsystem_flag)) != 0 ||
1940 (r = sshbuf_put_u32(m, echar)) != 0 ||
1941 (r = sshbuf_put_cstring(m, term)) != 0 ||
1942 (r = sshbuf_put_stringb(m, command)) != 0)
1943 fatal("%s: request: %s", __func__, ssh_err(r));
1945 /* Pass environment */
1946 if (options.num_send_env > 0 && environ != NULL) {
1947 for (i = 0; environ[i] != NULL; i++) {
1948 if (!env_permitted(environ[i]))
1950 if ((r = sshbuf_put_cstring(m, environ[i])) != 0)
1951 fatal("%s: request: %s", __func__, ssh_err(r));
1954 for (i = 0; i < options.num_setenv; i++) {
1955 if ((r = sshbuf_put_cstring(m, options.setenv[i])) != 0)
1956 fatal("%s: request: %s", __func__, ssh_err(r));
1959 if (mux_client_write_packet(fd, m) != 0)
1960 fatal("%s: write packet: %s", __func__, strerror(errno));
1962 /* Send the stdio file descriptors */
1963 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1964 mm_send_fd(fd, STDOUT_FILENO) == -1 ||
1965 mm_send_fd(fd, STDERR_FILENO) == -1)
1966 fatal("%s: send fds failed", __func__);
1968 debug3("%s: session request sent", __func__);
1970 /* Read their reply */
1972 if (mux_client_read_packet(fd, m) != 0) {
1973 error("%s: read from master failed: %s",
1974 __func__, strerror(errno));
1979 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1980 (r = sshbuf_get_u32(m, &rid)) != 0)
1981 fatal("%s: decode: %s", __func__, ssh_err(r));
1982 if (rid != muxclient_request_id)
1983 fatal("%s: out of sequence reply: my id %u theirs %u",
1984 __func__, muxclient_request_id, rid);
1987 case MUX_S_SESSION_OPENED:
1988 if ((r = sshbuf_get_u32(m, &sid)) != 0)
1989 fatal("%s: decode ID: %s", __func__, ssh_err(r));
1991 case MUX_S_PERMISSION_DENIED:
1992 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1993 fatal("%s: decode error: %s", __func__, ssh_err(r));
1994 error("Master refused session request: %s", e);
1998 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1999 fatal("%s: decode error: %s", __func__, ssh_err(r));
2000 error("%s: session request failed: %s", __func__, e);
2005 error("%s: unexpected response from master 0x%08x",
2009 muxclient_request_id++;
2011 if (pledge("stdio proc tty", NULL) == -1)
2012 fatal("%s pledge(): %s", __func__, strerror(errno));
2013 platform_pledge_mux();
2015 signal(SIGHUP, control_client_sighandler);
2016 signal(SIGINT, control_client_sighandler);
2017 signal(SIGTERM, control_client_sighandler);
2018 signal(SIGWINCH, control_client_sigrelay);
2022 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
2025 * Stick around until the controlee closes the client_fd.
2026 * Before it does, it is expected to write an exit message.
2027 * This process must read the value and wait for the closure of
2028 * the client_fd; if this one closes early, the multiplex master will
2029 * terminate early too (possibly losing data).
2031 for (exitval = 255, exitval_seen = 0;;) {
2033 if (mux_client_read_packet(fd, m) != 0)
2035 if ((r = sshbuf_get_u32(m, &type)) != 0)
2036 fatal("%s: decode type: %s", __func__, ssh_err(r));
2038 case MUX_S_TTY_ALLOC_FAIL:
2039 if ((r = sshbuf_get_u32(m, &esid)) != 0)
2040 fatal("%s: decode ID: %s",
2041 __func__, ssh_err(r));
2043 fatal("%s: tty alloc fail on unknown session: "
2044 "my id %u theirs %u",
2045 __func__, sid, esid);
2046 leave_raw_mode(options.request_tty ==
2050 case MUX_S_EXIT_MESSAGE:
2051 if ((r = sshbuf_get_u32(m, &esid)) != 0)
2052 fatal("%s: decode ID: %s",
2053 __func__, ssh_err(r));
2055 fatal("%s: exit on unknown session: "
2056 "my id %u theirs %u",
2057 __func__, sid, esid);
2059 fatal("%s: exitval sent twice", __func__);
2060 if ((r = sshbuf_get_u32(m, &exitval)) != 0)
2061 fatal("%s: decode exit value: %s",
2062 __func__, ssh_err(r));
2066 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2067 fatal("%s: decode error: %s",
2068 __func__, ssh_err(r));
2069 fatal("%s: master returned error: %s", __func__, e);
2075 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
2077 if (muxclient_terminate) {
2078 debug2("Exiting on signal: %s", strsignal(muxclient_terminate));
2080 } else if (!exitval_seen) {
2081 debug2("Control master terminated unexpectedly");
2084 debug2("Received exit status from master %d", exitval);
2086 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
2087 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
2093 mux_client_proxy(int fd)
2100 if ((m = sshbuf_new()) == NULL)
2101 fatal("%s: sshbuf_new", __func__);
2102 if ((r = sshbuf_put_u32(m, MUX_C_PROXY)) != 0 ||
2103 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
2104 fatal("%s: request: %s", __func__, ssh_err(r));
2105 if (mux_client_write_packet(fd, m) != 0)
2106 fatal("%s: write packet: %s", __func__, strerror(errno));
2110 /* Read their reply */
2111 if (mux_client_read_packet(fd, m) != 0) {
2115 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
2116 (r = sshbuf_get_u32(m, &rid)) != 0)
2117 fatal("%s: decode: %s", __func__, ssh_err(r));
2118 if (rid != muxclient_request_id)
2119 fatal("%s: out of sequence reply: my id %u theirs %u",
2120 __func__, muxclient_request_id, rid);
2121 if (type != MUX_S_PROXY) {
2122 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2123 fatal("%s: decode error: %s", __func__, ssh_err(r));
2124 fatal("%s: master returned error: %s", __func__, e);
2128 debug3("%s: done", __func__);
2129 muxclient_request_id++;
2134 mux_client_request_stdio_fwd(int fd)
2138 u_int type, rid, sid;
2141 debug3("%s: entering", __func__);
2143 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
2144 error("%s: master alive request failed", __func__);
2148 signal(SIGPIPE, SIG_IGN);
2150 if (stdin_null_flag) {
2151 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
2152 fatal("open(/dev/null): %s", strerror(errno));
2153 if (dup2(devnull, STDIN_FILENO) == -1)
2154 fatal("dup2: %s", strerror(errno));
2155 if (devnull > STDERR_FILENO)
2159 if ((m = sshbuf_new()) == NULL)
2160 fatal("%s: sshbuf_new", __func__);
2161 if ((r = sshbuf_put_u32(m, MUX_C_NEW_STDIO_FWD)) != 0 ||
2162 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
2163 (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
2164 (r = sshbuf_put_cstring(m, options.stdio_forward_host)) != 0 ||
2165 (r = sshbuf_put_u32(m, options.stdio_forward_port)) != 0)
2166 fatal("%s: request: %s", __func__, ssh_err(r));
2168 if (mux_client_write_packet(fd, m) != 0)
2169 fatal("%s: write packet: %s", __func__, strerror(errno));
2171 /* Send the stdio file descriptors */
2172 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
2173 mm_send_fd(fd, STDOUT_FILENO) == -1)
2174 fatal("%s: send fds failed", __func__);
2176 if (pledge("stdio proc tty", NULL) == -1)
2177 fatal("%s pledge(): %s", __func__, strerror(errno));
2178 platform_pledge_mux();
2180 debug3("%s: stdio forward request sent", __func__);
2182 /* Read their reply */
2185 if (mux_client_read_packet(fd, m) != 0) {
2186 error("%s: read from master failed: %s",
2187 __func__, strerror(errno));
2192 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
2193 (r = sshbuf_get_u32(m, &rid)) != 0)
2194 fatal("%s: decode: %s", __func__, ssh_err(r));
2195 if (rid != muxclient_request_id)
2196 fatal("%s: out of sequence reply: my id %u theirs %u",
2197 __func__, muxclient_request_id, rid);
2199 case MUX_S_SESSION_OPENED:
2200 if ((r = sshbuf_get_u32(m, &sid)) != 0)
2201 fatal("%s: decode ID: %s", __func__, ssh_err(r));
2202 debug("%s: master session id: %u", __func__, sid);
2204 case MUX_S_PERMISSION_DENIED:
2205 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2206 fatal("%s: decode error: %s", __func__, ssh_err(r));
2208 fatal("Master refused stdio forwarding request: %s", e);
2210 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2211 fatal("%s: decode error: %s", __func__, ssh_err(r));
2213 fatal("Stdio forwarding request failed: %s", e);
2216 error("%s: unexpected response from master 0x%08x",
2220 muxclient_request_id++;
2222 signal(SIGHUP, control_client_sighandler);
2223 signal(SIGINT, control_client_sighandler);
2224 signal(SIGTERM, control_client_sighandler);
2225 signal(SIGWINCH, control_client_sigrelay);
2228 * Stick around until the controlee closes the client_fd.
2231 if (mux_client_read_packet(fd, m) != 0) {
2232 if (errno == EPIPE ||
2233 (errno == EINTR && muxclient_terminate != 0))
2235 fatal("%s: mux_client_read_packet: %s",
2236 __func__, strerror(errno));
2238 fatal("%s: master returned unexpected message %u", __func__, type);
2242 mux_client_request_stop_listening(int fd)
2249 debug3("%s: entering", __func__);
2251 if ((m = sshbuf_new()) == NULL)
2252 fatal("%s: sshbuf_new", __func__);
2253 if ((r = sshbuf_put_u32(m, MUX_C_STOP_LISTENING)) != 0 ||
2254 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
2255 fatal("%s: request: %s", __func__, ssh_err(r));
2257 if (mux_client_write_packet(fd, m) != 0)
2258 fatal("%s: write packet: %s", __func__, strerror(errno));
2262 /* Read their reply */
2263 if (mux_client_read_packet(fd, m) != 0)
2264 fatal("%s: read from master failed: %s",
2265 __func__, strerror(errno));
2267 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
2268 (r = sshbuf_get_u32(m, &rid)) != 0)
2269 fatal("%s: decode: %s", __func__, ssh_err(r));
2270 if (rid != muxclient_request_id)
2271 fatal("%s: out of sequence reply: my id %u theirs %u",
2272 __func__, muxclient_request_id, rid);
2277 case MUX_S_PERMISSION_DENIED:
2278 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2279 fatal("%s: decode error: %s", __func__, ssh_err(r));
2280 fatal("Master refused stop listening request: %s", e);
2282 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2283 fatal("%s: decode error: %s", __func__, ssh_err(r));
2284 fatal("%s: stop listening request failed: %s", __func__, e);
2286 fatal("%s: unexpected response from master 0x%08x",
2290 muxclient_request_id++;
2293 /* Multiplex client main loop. */
2295 muxclient(const char *path)
2297 struct sockaddr_un addr;
2301 if (muxclient_command == 0) {
2302 if (options.stdio_forward_host != NULL)
2303 muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
2305 muxclient_command = SSHMUX_COMMAND_OPEN;
2308 switch (options.control_master) {
2309 case SSHCTL_MASTER_AUTO:
2310 case SSHCTL_MASTER_AUTO_ASK:
2311 debug("auto-mux: Trying existing master");
2313 case SSHCTL_MASTER_NO:
2319 memset(&addr, '\0', sizeof(addr));
2320 addr.sun_family = AF_UNIX;
2322 if (strlcpy(addr.sun_path, path,
2323 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
2324 fatal("ControlPath too long ('%s' >= %u bytes)", path,
2325 (unsigned int)sizeof(addr.sun_path));
2327 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
2328 fatal("%s socket(): %s", __func__, strerror(errno));
2330 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
2331 switch (muxclient_command) {
2332 case SSHMUX_COMMAND_OPEN:
2333 case SSHMUX_COMMAND_STDIO_FWD:
2336 fatal("Control socket connect(%.100s): %s", path,
2339 if (errno == ECONNREFUSED &&
2340 options.control_master != SSHCTL_MASTER_NO) {
2341 debug("Stale control socket %.100s, unlinking", path);
2343 } else if (errno == ENOENT) {
2344 debug("Control socket \"%.100s\" does not exist", path);
2346 error("Control socket connect(%.100s): %s", path,
2354 if (mux_client_hello_exchange(sock) != 0) {
2355 error("%s: master hello exchange failed", __func__);
2360 switch (muxclient_command) {
2361 case SSHMUX_COMMAND_ALIVE_CHECK:
2362 if ((pid = mux_client_request_alive(sock)) == 0)
2363 fatal("%s: master alive check failed", __func__);
2364 fprintf(stderr, "Master running (pid=%u)\r\n", pid);
2366 case SSHMUX_COMMAND_TERMINATE:
2367 mux_client_request_terminate(sock);
2368 if (options.log_level != SYSLOG_LEVEL_QUIET)
2369 fprintf(stderr, "Exit request sent.\r\n");
2371 case SSHMUX_COMMAND_FORWARD:
2372 if (mux_client_forwards(sock, 0) != 0)
2373 fatal("%s: master forward request failed", __func__);
2375 case SSHMUX_COMMAND_OPEN:
2376 if (mux_client_forwards(sock, 0) != 0) {
2377 error("%s: master forward request failed", __func__);
2380 mux_client_request_session(sock);
2382 case SSHMUX_COMMAND_STDIO_FWD:
2383 mux_client_request_stdio_fwd(sock);
2385 case SSHMUX_COMMAND_STOP:
2386 mux_client_request_stop_listening(sock);
2387 if (options.log_level != SYSLOG_LEVEL_QUIET)
2388 fprintf(stderr, "Stop listening request sent.\r\n");
2390 case SSHMUX_COMMAND_CANCEL_FWD:
2391 if (mux_client_forwards(sock, 1) != 0)
2392 error("%s: master cancel forward request failed",
2395 case SSHMUX_COMMAND_PROXY:
2396 mux_client_proxy(sock);
2399 fatal("unrecognised muxclient_command %d", muxclient_command);