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