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