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