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