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