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