blob: 5efc849c4b3c1f5fd2d955fdddff49105b08368a [file] [log] [blame]
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00001/* $OpenBSD: mux.c,v 1.81 2020/01/23 07:10:22 dtucker Exp $ */
Damien Millerb1cbfa22008-05-19 16:00:08 +10002/*
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
Damien Millere1537f92010-01-26 13:26:22 +110020#include "includes.h"
21
Damien Millerb1cbfa22008-05-19 16:00:08 +100022#include <sys/types.h>
Damien Millerb1cbfa22008-05-19 16:00:08 +100023#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 <signal.h>
30#include <stdarg.h>
31#include <stddef.h>
32#include <stdlib.h>
33#include <stdio.h>
34#include <string.h>
35#include <unistd.h>
Darren Tuckerce38d822008-06-07 06:25:15 +100036#ifdef HAVE_PATHS_H
Damien Millerb1cbfa22008-05-19 16:00:08 +100037#include <paths.h>
Darren Tuckerce38d822008-06-07 06:25:15 +100038#endif
Damien Millerb1cbfa22008-05-19 16:00:08 +100039
Damien Millere1537f92010-01-26 13:26:22 +110040#ifdef HAVE_POLL_H
41#include <poll.h>
42#else
43# ifdef HAVE_SYS_POLL_H
44# include <sys/poll.h>
45# endif
46#endif
47
Damien Millera7058ec2008-05-20 08:57:06 +100048#ifdef HAVE_UTIL_H
49# include <util.h>
50#endif
51
Damien Millerb1cbfa22008-05-19 16:00:08 +100052#include "openbsd-compat/sys-queue.h"
53#include "xmalloc.h"
54#include "log.h"
55#include "ssh.h"
Damien Miller388f6fc2010-05-21 14:57:35 +100056#include "ssh2.h"
Damien Millerb1cbfa22008-05-19 16:00:08 +100057#include "pathnames.h"
58#include "misc.h"
59#include "match.h"
markus@openbsd.orgf4608a72018-07-09 21:18:10 +000060#include "sshbuf.h"
Damien Millerb1cbfa22008-05-19 16:00:08 +100061#include "channels.h"
62#include "msg.h"
63#include "packet.h"
64#include "monitor_fdpass.h"
65#include "sshpty.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000066#include "sshkey.h"
Damien Millerb1cbfa22008-05-19 16:00:08 +100067#include "readconf.h"
68#include "clientloop.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000069#include "ssherr.h"
Damien Millerb1cbfa22008-05-19 16:00:08 +100070
71/* from ssh.c */
72extern int tty_flag;
73extern Options options;
74extern int stdin_null_flag;
75extern char *host;
Darren Tucker8ec4fd82009-10-07 08:39:57 +110076extern int subsystem_flag;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +000077extern struct sshbuf *command;
Damien Millere1537f92010-01-26 13:26:22 +110078extern volatile sig_atomic_t quit_pending;
Damien Millerb1cbfa22008-05-19 16:00:08 +100079
Darren Tucker2fb66ca2008-06-13 04:49:33 +100080/* Context for session open confirmation callback */
81struct mux_session_confirm_ctx {
Damien Millere1537f92010-01-26 13:26:22 +110082 u_int want_tty;
83 u_int want_subsys;
84 u_int want_x_fwd;
85 u_int want_agent_fwd;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +000086 struct sshbuf *cmd;
Darren Tucker2fb66ca2008-06-13 04:49:33 +100087 char *term;
88 struct termios tio;
89 char **env;
Damien Millerd530f5f2010-05-21 14:57:10 +100090 u_int rid;
Darren Tucker2fb66ca2008-06-13 04:49:33 +100091};
92
Damien Miller357610d2014-07-18 15:04:10 +100093/* Context for stdio fwd open confirmation callback */
94struct mux_stdio_confirm_ctx {
95 u_int rid;
96};
97
Damien Miller388f6fc2010-05-21 14:57:35 +100098/* Context for global channel callback */
99struct mux_channel_confirm_ctx {
100 u_int cid; /* channel id */
101 u_int rid; /* request id */
102 int fid; /* forward id */
103};
104
Damien Millerb1cbfa22008-05-19 16:00:08 +1000105/* fd to control socket */
106int muxserver_sock = -1;
107
Damien Millere1537f92010-01-26 13:26:22 +1100108/* client request id */
109u_int muxclient_request_id = 0;
110
Damien Millerb1cbfa22008-05-19 16:00:08 +1000111/* Multiplexing control command */
112u_int muxclient_command = 0;
113
114/* Set when signalled. */
115static volatile sig_atomic_t muxclient_terminate = 0;
116
117/* PID of multiplex server */
118static u_int muxserver_pid = 0;
119
Damien Millere1537f92010-01-26 13:26:22 +1100120static Channel *mux_listener_channel = NULL;
Damien Millerb1cbfa22008-05-19 16:00:08 +1000121
Damien Millere1537f92010-01-26 13:26:22 +1100122struct 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
Damien Miller6c3eec72011-05-05 14:16:22 +1000134#define MUX_C_STOP_LISTENING 0x10000009
markus@openbsd.org8d057842016-09-30 09:19:13 +0000135#define MUX_C_PROXY 0x1000000f
Damien Millere1537f92010-01-26 13:26:22 +1100136#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
Damien Miller388f6fc2010-05-21 14:57:35 +1000142#define MUX_S_REMOTE_PORT 0x80000007
Damien Miller555f3b82011-05-15 08:48:05 +1000143#define MUX_S_TTY_ALLOC_FAIL 0x80000008
markus@openbsd.org8d057842016-09-30 09:19:13 +0000144#define MUX_S_PROXY 0x8000000f
Damien Millere1537f92010-01-26 13:26:22 +1100145
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
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000151static void mux_session_confirm(struct ssh *, int, int, void *);
152static void mux_stdio_confirm(struct ssh *, int, int, void *);
Damien Millere1537f92010-01-26 13:26:22 +1100153
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000154static int mux_master_process_hello(struct ssh *, u_int,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000155 Channel *, struct sshbuf *, struct sshbuf *);
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000156static int mux_master_process_new_session(struct ssh *, u_int,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000157 Channel *, struct sshbuf *, struct sshbuf *);
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000158static int mux_master_process_alive_check(struct ssh *, u_int,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000159 Channel *, struct sshbuf *, struct sshbuf *);
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000160static int mux_master_process_terminate(struct ssh *, u_int,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000161 Channel *, struct sshbuf *, struct sshbuf *);
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000162static int mux_master_process_open_fwd(struct ssh *, u_int,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000163 Channel *, struct sshbuf *, struct sshbuf *);
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000164static int mux_master_process_close_fwd(struct ssh *, u_int,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000165 Channel *, struct sshbuf *, struct sshbuf *);
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000166static int mux_master_process_stdio_fwd(struct ssh *, u_int,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000167 Channel *, struct sshbuf *, struct sshbuf *);
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000168static int mux_master_process_stop_listening(struct ssh *, u_int,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000169 Channel *, struct sshbuf *, struct sshbuf *);
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000170static int mux_master_process_proxy(struct ssh *, u_int,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000171 Channel *, struct sshbuf *, struct sshbuf *);
Damien Millere1537f92010-01-26 13:26:22 +1100172
173static const struct {
174 u_int type;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000175 int (*handler)(struct ssh *, u_int, Channel *,
176 struct sshbuf *, struct sshbuf *);
Damien Millere1537f92010-01-26 13:26:22 +1100177} mux_master_handlers[] = {
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000178 { 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 },
Damien Millere1537f92010-01-26 13:26:22 +1100187 { 0, NULL }
188};
189
190/* Cleanup callback fired on closure of mux slave _session_ channel */
191/* ARGSUSED */
Darren Tuckerea8342c2013-06-06 08:11:40 +1000192static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000193mux_master_session_cleanup_cb(struct ssh *ssh, int cid, void *unused)
Damien Millere1537f92010-01-26 13:26:22 +1100194{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000195 Channel *cc, *c = channel_by_id(ssh, cid);
Damien Millere1537f92010-01-26 13:26:22 +1100196
197 debug3("%s: entering for channel %d", __func__, cid);
198 if (c == NULL)
199 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
200 if (c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000201 if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
Damien Millere1537f92010-01-26 13:26:22 +1100202 fatal("%s: channel %d missing control channel %d",
203 __func__, c->self, c->ctl_chan);
204 c->ctl_chan = -1;
djm@openbsd.org9f532292017-09-12 06:35:31 +0000205 cc->remote_id = 0;
206 cc->have_remote_id = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000207 chan_rcvd_oclose(ssh, cc);
Damien Millere1537f92010-01-26 13:26:22 +1100208 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000209 channel_cancel_cleanup(ssh, c->self);
Damien Millere1537f92010-01-26 13:26:22 +1100210}
211
212/* Cleanup callback fired on closure of mux slave _control_ channel */
213/* ARGSUSED */
214static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000215mux_master_control_cleanup_cb(struct ssh *ssh, int cid, void *unused)
Damien Millere1537f92010-01-26 13:26:22 +1100216{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000217 Channel *sc, *c = channel_by_id(ssh, cid);
Damien Millere1537f92010-01-26 13:26:22 +1100218
219 debug3("%s: entering for channel %d", __func__, cid);
220 if (c == NULL)
221 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
djm@openbsd.org9f532292017-09-12 06:35:31 +0000222 if (c->have_remote_id) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000223 if ((sc = channel_by_id(ssh, c->remote_id)) == NULL)
djm@openbsd.org9f532292017-09-12 06:35:31 +0000224 fatal("%s: channel %d missing session channel %u",
Damien Millere1537f92010-01-26 13:26:22 +1100225 __func__, c->self, c->remote_id);
djm@openbsd.org9f532292017-09-12 06:35:31 +0000226 c->remote_id = 0;
227 c->have_remote_id = 0;
Damien Millere1537f92010-01-26 13:26:22 +1100228 sc->ctl_chan = -1;
Damien Miller172859c2013-04-23 15:19:27 +1000229 if (sc->type != SSH_CHANNEL_OPEN &&
230 sc->type != SSH_CHANNEL_OPENING) {
Damien Millera21cdfa2010-01-28 06:26:59 +1100231 debug2("%s: channel %d: not open", __func__, sc->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000232 chan_mark_dead(ssh, sc);
Damien Millera21cdfa2010-01-28 06:26:59 +1100233 } else {
Damien Miller0dac03f2010-01-30 17:36:33 +1100234 if (sc->istate == CHAN_INPUT_OPEN)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000235 chan_read_failed(ssh, sc);
Damien Miller0dac03f2010-01-30 17:36:33 +1100236 if (sc->ostate == CHAN_OUTPUT_OPEN)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000237 chan_write_failed(ssh, sc);
Damien Millera21cdfa2010-01-28 06:26:59 +1100238 }
Damien Millere1537f92010-01-26 13:26:22 +1100239 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000240 channel_cancel_cleanup(ssh, c->self);
Damien Millere1537f92010-01-26 13:26:22 +1100241}
242
243/* Check mux client environment variables before passing them to mux master. */
244static int
245env_permitted(char *env)
246{
247 int i, ret;
248 char name[1024], *cp;
249
250 if ((cp = strchr(env, '=')) == NULL || cp == env)
251 return 0;
252 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
253 if (ret <= 0 || (size_t)ret >= sizeof(name)) {
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000254 error("%s: name '%.100s...' too long", __func__, env);
Damien Millere1537f92010-01-26 13:26:22 +1100255 return 0;
256 }
257
258 for (i = 0; i < options.num_send_env; i++)
259 if (match_pattern(name, options.send_env[i]))
260 return 1;
261
262 return 0;
263}
264
265/* Mux master protocol message handlers */
266
267static int
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000268mux_master_process_hello(struct ssh *ssh, u_int rid,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000269 Channel *c, struct sshbuf *m, struct sshbuf *reply)
Damien Millere1537f92010-01-26 13:26:22 +1100270{
271 u_int ver;
272 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000273 int r;
Damien Millere1537f92010-01-26 13:26:22 +1100274
275 if (state == NULL)
276 fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self);
277 if (state->hello_rcvd) {
278 error("%s: HELLO received twice", __func__);
279 return -1;
280 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000281 if ((r = sshbuf_get_u32(m, &ver)) != 0) {
282 error("%s: malformed message: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +1100283 return -1;
284 }
285 if (ver != SSHMUX_VER) {
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000286 error("%s: unsupported multiplexing protocol version %u "
287 "(expected %u)", __func__, ver, SSHMUX_VER);
Damien Millere1537f92010-01-26 13:26:22 +1100288 return -1;
289 }
290 debug2("%s: channel %d slave version %u", __func__, c->self, ver);
291
292 /* No extensions are presently defined */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000293 while (sshbuf_len(m) > 0) {
294 char *name = NULL;
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000295 size_t value_len = 0;
Damien Millere1537f92010-01-26 13:26:22 +1100296
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000297 if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000298 (r = sshbuf_get_string_direct(m, NULL, &value_len)) != 0) {
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000299 error("%s: malformed extension: %s",
300 __func__, ssh_err(r));
301 return -1;
Damien Millere1537f92010-01-26 13:26:22 +1100302 }
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000303 debug2("%s: Unrecognised extension \"%s\" length %zu",
304 __func__, name, value_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000305 free(name);
Damien Millere1537f92010-01-26 13:26:22 +1100306 }
307 state->hello_rcvd = 1;
308 return 0;
309}
310
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000311/* Enqueue a "ok" response to the reply buffer */
312static void
313reply_ok(struct sshbuf *reply, u_int rid)
314{
315 int r;
316
317 if ((r = sshbuf_put_u32(reply, MUX_S_OK)) != 0 ||
318 (r = sshbuf_put_u32(reply, rid)) != 0)
319 fatal("%s: reply: %s", __func__, ssh_err(r));
320}
321
322/* Enqueue an error response to the reply buffer */
323static void
324reply_error(struct sshbuf *reply, u_int type, u_int rid, const char *msg)
325{
326 int r;
327
328 if ((r = sshbuf_put_u32(reply, type)) != 0 ||
329 (r = sshbuf_put_u32(reply, rid)) != 0 ||
330 (r = sshbuf_put_cstring(reply, msg)) != 0)
331 fatal("%s: reply: %s", __func__, ssh_err(r));
332}
333
Damien Millere1537f92010-01-26 13:26:22 +1100334static int
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000335mux_master_process_new_session(struct ssh *ssh, u_int rid,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000336 Channel *c, struct sshbuf *m, struct sshbuf *reply)
Damien Millere1537f92010-01-26 13:26:22 +1100337{
338 Channel *nc;
339 struct mux_session_confirm_ctx *cctx;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000340 char *cmd, *cp;
341 u_int i, j, env_len, escape_char, window, packetmax;
342 int r, new_fd[3];
Damien Millere1537f92010-01-26 13:26:22 +1100343
344 /* Reply for SSHMUX_COMMAND_OPEN */
345 cctx = xcalloc(1, sizeof(*cctx));
346 cctx->term = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000347 cctx->rid = rid;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000348 cmd = NULL;
Damien Millerab523b02012-07-06 13:44:43 +1000349 cctx->env = NULL;
350 env_len = 0;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000351 if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
352 (r = sshbuf_get_u32(m, &cctx->want_tty)) != 0 ||
353 (r = sshbuf_get_u32(m, &cctx->want_x_fwd)) != 0 ||
354 (r = sshbuf_get_u32(m, &cctx->want_agent_fwd)) != 0 ||
355 (r = sshbuf_get_u32(m, &cctx->want_subsys)) != 0 ||
356 (r = sshbuf_get_u32(m, &escape_char)) != 0 ||
357 (r = sshbuf_get_cstring(m, &cctx->term, NULL)) != 0 ||
358 (r = sshbuf_get_cstring(m, &cmd, NULL)) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +1100359 malf:
Darren Tuckera627d422013-06-02 07:31:17 +1000360 free(cmd);
Damien Millerab523b02012-07-06 13:44:43 +1000361 for (j = 0; j < env_len; j++)
Darren Tuckera627d422013-06-02 07:31:17 +1000362 free(cctx->env[j]);
363 free(cctx->env);
364 free(cctx->term);
365 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100366 error("%s: malformed message", __func__);
367 return -1;
368 }
Damien Millere1537f92010-01-26 13:26:22 +1100369
Damien Millere1537f92010-01-26 13:26:22 +1100370#define MUX_MAX_ENV_VARS 4096
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000371 while (sshbuf_len(m) > 0) {
372 if ((r = sshbuf_get_cstring(m, &cp, NULL)) != 0)
Damien Millere1537f92010-01-26 13:26:22 +1100373 goto malf;
Damien Millere1537f92010-01-26 13:26:22 +1100374 if (!env_permitted(cp)) {
Darren Tuckera627d422013-06-02 07:31:17 +1000375 free(cp);
Damien Millere1537f92010-01-26 13:26:22 +1100376 continue;
377 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000378 cctx->env = xreallocarray(cctx->env, env_len + 2,
Damien Millere1537f92010-01-26 13:26:22 +1100379 sizeof(*cctx->env));
380 cctx->env[env_len++] = cp;
381 cctx->env[env_len] = NULL;
382 if (env_len > MUX_MAX_ENV_VARS) {
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000383 error("%s: >%d environment variables received, "
384 "ignoring additional", __func__, MUX_MAX_ENV_VARS);
Damien Millere1537f92010-01-26 13:26:22 +1100385 break;
386 }
387 }
388
389 debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
390 "term \"%s\", cmd \"%s\", env %u", __func__, c->self,
391 cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
392 cctx->want_subsys, cctx->term, cmd, env_len);
393
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000394 if ((cctx->cmd = sshbuf_new()) == NULL)
395 fatal("%s: sshbuf_new", __func__);
396 if ((r = sshbuf_put(cctx->cmd, cmd, strlen(cmd))) != 0)
397 fatal("%s: sshbuf_put: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000398 free(cmd);
Damien Millere1537f92010-01-26 13:26:22 +1100399 cmd = NULL;
400
401 /* Gather fds from client */
402 for(i = 0; i < 3; i++) {
403 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
404 error("%s: failed to receive fd %d from slave",
405 __func__, i);
406 for (j = 0; j < i; j++)
407 close(new_fd[j]);
408 for (j = 0; j < env_len; j++)
Darren Tuckera627d422013-06-02 07:31:17 +1000409 free(cctx->env[j]);
410 free(cctx->env);
411 free(cctx->term);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000412 sshbuf_free(cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +1000413 free(cctx);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000414 reply_error(reply, MUX_S_FAILURE, rid,
Damien Millere1537f92010-01-26 13:26:22 +1100415 "did not receive file descriptors");
416 return -1;
417 }
418 }
419
420 debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
421 new_fd[0], new_fd[1], new_fd[2]);
422
423 /* XXX support multiple child sessions in future */
djm@openbsd.org9f532292017-09-12 06:35:31 +0000424 if (c->have_remote_id) {
Damien Millere1537f92010-01-26 13:26:22 +1100425 debug2("%s: session already open", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000426 reply_error(reply, MUX_S_FAILURE, rid,
427 "Multiple sessions not supported");
Damien Millere1537f92010-01-26 13:26:22 +1100428 cleanup:
429 close(new_fd[0]);
430 close(new_fd[1]);
431 close(new_fd[2]);
Darren Tuckera627d422013-06-02 07:31:17 +1000432 free(cctx->term);
Damien Millere1537f92010-01-26 13:26:22 +1100433 if (env_len != 0) {
434 for (i = 0; i < env_len; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000435 free(cctx->env[i]);
436 free(cctx->env);
Damien Millere1537f92010-01-26 13:26:22 +1100437 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000438 sshbuf_free(cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +1000439 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100440 return 0;
441 }
442
443 if (options.control_master == SSHCTL_MASTER_ASK ||
444 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
445 if (!ask_permission("Allow shared connection to %s? ", host)) {
446 debug2("%s: session refused by user", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000447 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
448 "Permission denied");
Damien Millere1537f92010-01-26 13:26:22 +1100449 goto cleanup;
450 }
451 }
452
453 /* Try to pick up ttymodes from client before it goes raw */
454 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
455 error("%s: tcgetattr: %s", __func__, strerror(errno));
456
457 /* enable nonblocking unless tty */
458 if (!isatty(new_fd[0]))
459 set_nonblock(new_fd[0]);
460 if (!isatty(new_fd[1]))
461 set_nonblock(new_fd[1]);
462 if (!isatty(new_fd[2]))
463 set_nonblock(new_fd[2]);
464
465 window = CHAN_SES_WINDOW_DEFAULT;
466 packetmax = CHAN_SES_PACKET_DEFAULT;
467 if (cctx->want_tty) {
468 window >>= 1;
469 packetmax >>= 1;
470 }
471
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000472 nc = channel_new(ssh, "session", SSH_CHANNEL_OPENING,
Damien Millere1537f92010-01-26 13:26:22 +1100473 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
474 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
475
476 nc->ctl_chan = c->self; /* link session -> control channel */
477 c->remote_id = nc->self; /* link control -> session channel */
djm@openbsd.org9f532292017-09-12 06:35:31 +0000478 c->have_remote_id = 1;
Damien Millere1537f92010-01-26 13:26:22 +1100479
480 if (cctx->want_tty && escape_char != 0xffffffff) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000481 channel_register_filter(ssh, nc->self,
Damien Millere1537f92010-01-26 13:26:22 +1100482 client_simple_escape_filter, NULL,
483 client_filter_cleanup,
484 client_new_escape_filter_ctx((int)escape_char));
485 }
486
487 debug2("%s: channel_new: %d linked to control channel %d",
488 __func__, nc->self, nc->ctl_chan);
489
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000490 channel_send_open(ssh, nc->self);
491 channel_register_open_confirm(ssh, nc->self, mux_session_confirm, cctx);
Damien Millerd530f5f2010-05-21 14:57:10 +1000492 c->mux_pause = 1; /* stop handling messages until open_confirm done */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000493 channel_register_cleanup(ssh, nc->self,
494 mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +1100495
Damien Millerd530f5f2010-05-21 14:57:10 +1000496 /* reply is deferred, sent by mux_session_confirm */
Damien Millere1537f92010-01-26 13:26:22 +1100497 return 0;
498}
499
500static int
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000501mux_master_process_alive_check(struct ssh *ssh, u_int rid,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000502 Channel *c, struct sshbuf *m, struct sshbuf *reply)
Damien Millere1537f92010-01-26 13:26:22 +1100503{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000504 int r;
505
Damien Millere1537f92010-01-26 13:26:22 +1100506 debug2("%s: channel %d: alive check", __func__, c->self);
507
508 /* prepare reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000509 if ((r = sshbuf_put_u32(reply, MUX_S_ALIVE)) != 0 ||
510 (r = sshbuf_put_u32(reply, rid)) != 0 ||
511 (r = sshbuf_put_u32(reply, (u_int)getpid())) != 0)
512 fatal("%s: reply: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +1100513
514 return 0;
515}
516
517static int
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000518mux_master_process_terminate(struct ssh *ssh, u_int rid,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000519 Channel *c, struct sshbuf *m, struct sshbuf *reply)
Damien Millere1537f92010-01-26 13:26:22 +1100520{
521 debug2("%s: channel %d: terminate request", __func__, c->self);
522
523 if (options.control_master == SSHCTL_MASTER_ASK ||
524 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
525 if (!ask_permission("Terminate shared connection to %s? ",
526 host)) {
527 debug2("%s: termination refused by user", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000528 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
529 "Permission denied");
Damien Millere1537f92010-01-26 13:26:22 +1100530 return 0;
531 }
532 }
533
534 quit_pending = 1;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000535 reply_ok(reply, rid);
Damien Millere1537f92010-01-26 13:26:22 +1100536 /* XXX exit happens too soon - message never makes it to client */
537 return 0;
538}
539
540static char *
Damien Miller7acefbb2014-07-18 14:11:24 +1000541format_forward(u_int ftype, struct Forward *fwd)
Damien Millere1537f92010-01-26 13:26:22 +1100542{
543 char *ret;
544
545 switch (ftype) {
546 case MUX_FWD_LOCAL:
547 xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +1000548 (fwd->listen_path != NULL) ? fwd->listen_path :
Damien Millere1537f92010-01-26 13:26:22 +1100549 (fwd->listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +1000550 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millere1537f92010-01-26 13:26:22 +1100551 fwd->listen_host, fwd->listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +1000552 (fwd->connect_path != NULL) ? fwd->connect_path :
Damien Millere1537f92010-01-26 13:26:22 +1100553 fwd->connect_host, fwd->connect_port);
554 break;
555 case MUX_FWD_DYNAMIC:
556 xasprintf(&ret, "dynamic forward %.200s:%d -> *",
557 (fwd->listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +1000558 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millere1537f92010-01-26 13:26:22 +1100559 fwd->listen_host, fwd->listen_port);
560 break;
561 case MUX_FWD_REMOTE:
562 xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +1000563 (fwd->listen_path != NULL) ? fwd->listen_path :
Damien Millere1537f92010-01-26 13:26:22 +1100564 (fwd->listen_host == NULL) ?
565 "LOCALHOST" : fwd->listen_host,
566 fwd->listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +1000567 (fwd->connect_path != NULL) ? fwd->connect_path :
Damien Millere1537f92010-01-26 13:26:22 +1100568 fwd->connect_host, fwd->connect_port);
569 break;
570 default:
571 fatal("%s: unknown forward type %u", __func__, ftype);
572 }
573 return ret;
574}
575
576static int
577compare_host(const char *a, const char *b)
578{
579 if (a == NULL && b == NULL)
580 return 1;
581 if (a == NULL || b == NULL)
582 return 0;
583 return strcmp(a, b) == 0;
584}
585
586static int
Damien Miller7acefbb2014-07-18 14:11:24 +1000587compare_forward(struct Forward *a, struct Forward *b)
Damien Millere1537f92010-01-26 13:26:22 +1100588{
589 if (!compare_host(a->listen_host, b->listen_host))
590 return 0;
Damien Miller7acefbb2014-07-18 14:11:24 +1000591 if (!compare_host(a->listen_path, b->listen_path))
592 return 0;
Damien Millere1537f92010-01-26 13:26:22 +1100593 if (a->listen_port != b->listen_port)
594 return 0;
595 if (!compare_host(a->connect_host, b->connect_host))
596 return 0;
Damien Miller7acefbb2014-07-18 14:11:24 +1000597 if (!compare_host(a->connect_path, b->connect_path))
598 return 0;
Damien Millere1537f92010-01-26 13:26:22 +1100599 if (a->connect_port != b->connect_port)
600 return 0;
601
602 return 1;
603}
604
Damien Miller388f6fc2010-05-21 14:57:35 +1000605static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000606mux_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
Damien Miller388f6fc2010-05-21 14:57:35 +1000607{
608 struct mux_channel_confirm_ctx *fctx = ctxt;
609 char *failmsg = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000610 struct Forward *rfwd;
Damien Miller388f6fc2010-05-21 14:57:35 +1000611 Channel *c;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000612 struct sshbuf *out;
djm@openbsd.orge3128b32019-01-19 21:35:25 +0000613 u_int port;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000614 int r;
Damien Miller388f6fc2010-05-21 14:57:35 +1000615
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000616 if ((c = channel_by_id(ssh, fctx->cid)) == NULL) {
Damien Miller388f6fc2010-05-21 14:57:35 +1000617 /* no channel for reply */
618 error("%s: unknown channel", __func__);
619 return;
620 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000621 if ((out = sshbuf_new()) == NULL)
622 fatal("%s: sshbuf_new", __func__);
djm@openbsd.orgca430d42015-05-01 04:03:20 +0000623 if (fctx->fid >= options.num_remote_forwards ||
624 (options.remote_forwards[fctx->fid].connect_path == NULL &&
625 options.remote_forwards[fctx->fid].connect_host == NULL)) {
Damien Miller388f6fc2010-05-21 14:57:35 +1000626 xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
627 goto fail;
628 }
629 rfwd = &options.remote_forwards[fctx->fid];
630 debug("%s: %s for: listen %d, connect %s:%d", __func__,
631 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Miller7acefbb2014-07-18 14:11:24 +1000632 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
633 rfwd->connect_host, rfwd->connect_port);
Damien Miller388f6fc2010-05-21 14:57:35 +1000634 if (type == SSH2_MSG_REQUEST_SUCCESS) {
635 if (rfwd->listen_port == 0) {
djm@openbsd.orge3128b32019-01-19 21:35:25 +0000636 if ((r = sshpkt_get_u32(ssh, &port)) != 0)
637 fatal("%s: packet error: %s",
638 __func__, ssh_err(r));
639 if (port > 65535) {
640 fatal("Invalid allocated port %u for "
641 "mux remote forward to %s:%d", port,
642 rfwd->connect_host, rfwd->connect_port);
643 }
644 rfwd->allocated_port = (int)port;
djm@openbsd.org8312cfb2015-05-01 04:01:58 +0000645 debug("Allocated port %u for mux remote forward"
Damien Miller388f6fc2010-05-21 14:57:35 +1000646 " to %s:%d", rfwd->allocated_port,
647 rfwd->connect_host, rfwd->connect_port);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000648 if ((r = sshbuf_put_u32(out,
649 MUX_S_REMOTE_PORT)) != 0 ||
650 (r = sshbuf_put_u32(out, fctx->rid)) != 0 ||
651 (r = sshbuf_put_u32(out,
652 rfwd->allocated_port)) != 0)
653 fatal("%s: reply: %s", __func__, ssh_err(r));
djm@openbsd.org115063a2018-06-06 18:22:41 +0000654 channel_update_permission(ssh, rfwd->handle,
Darren Tucker68afb8c2011-10-02 18:59:03 +1100655 rfwd->allocated_port);
Damien Miller388f6fc2010-05-21 14:57:35 +1000656 } else {
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000657 reply_ok(out, fctx->rid);
Damien Miller388f6fc2010-05-21 14:57:35 +1000658 }
659 goto out;
660 } else {
Darren Tucker68afb8c2011-10-02 18:59:03 +1100661 if (rfwd->listen_port == 0)
djm@openbsd.org115063a2018-06-06 18:22:41 +0000662 channel_update_permission(ssh, rfwd->handle, -1);
Damien Miller7acefbb2014-07-18 14:11:24 +1000663 if (rfwd->listen_path != NULL)
664 xasprintf(&failmsg, "remote port forwarding failed for "
665 "listen path %s", rfwd->listen_path);
666 else
667 xasprintf(&failmsg, "remote port forwarding failed for "
668 "listen port %d", rfwd->listen_port);
djm@openbsd.orgca430d42015-05-01 04:03:20 +0000669
670 debug2("%s: clearing registered forwarding for listen %d, "
671 "connect %s:%d", __func__, rfwd->listen_port,
672 rfwd->connect_path ? rfwd->connect_path :
673 rfwd->connect_host, rfwd->connect_port);
674
675 free(rfwd->listen_host);
676 free(rfwd->listen_path);
677 free(rfwd->connect_host);
678 free(rfwd->connect_path);
679 memset(rfwd, 0, sizeof(*rfwd));
Damien Miller388f6fc2010-05-21 14:57:35 +1000680 }
681 fail:
682 error("%s: %s", __func__, failmsg);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000683 reply_error(out, MUX_S_FAILURE, fctx->rid, failmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000684 free(failmsg);
Damien Miller388f6fc2010-05-21 14:57:35 +1000685 out:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000686 if ((r = sshbuf_put_stringb(c->output, out)) != 0)
687 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
688 sshbuf_free(out);
Damien Miller388f6fc2010-05-21 14:57:35 +1000689 if (c->mux_pause <= 0)
690 fatal("%s: mux_pause %d", __func__, c->mux_pause);
691 c->mux_pause = 0; /* start processing messages again */
692}
693
Damien Millere1537f92010-01-26 13:26:22 +1100694static int
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000695mux_master_process_open_fwd(struct ssh *ssh, u_int rid,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000696 Channel *c, struct sshbuf *m, struct sshbuf *reply)
Damien Millere1537f92010-01-26 13:26:22 +1100697{
Damien Miller7acefbb2014-07-18 14:11:24 +1000698 struct Forward fwd;
Damien Millere1537f92010-01-26 13:26:22 +1100699 char *fwd_desc = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000700 char *listen_addr, *connect_addr;
Damien Millere1537f92010-01-26 13:26:22 +1100701 u_int ftype;
Damien Millerce986542013-07-18 16:12:44 +1000702 u_int lport, cport;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000703 int r, i, ret = 0, freefwd = 1;
Damien Millere1537f92010-01-26 13:26:22 +1100704
djm@openbsd.org45b0eb72015-08-19 23:18:26 +0000705 memset(&fwd, 0, sizeof(fwd));
706
Damien Miller7acefbb2014-07-18 14:11:24 +1000707 /* XXX - lport/cport check redundant */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000708 if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
709 (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
710 (r = sshbuf_get_u32(m, &lport)) != 0 ||
711 (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
712 (r = sshbuf_get_u32(m, &cport)) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000713 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
714 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
Damien Millere1537f92010-01-26 13:26:22 +1100715 error("%s: malformed message", __func__);
716 ret = -1;
717 goto out;
718 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000719 if (*listen_addr == '\0') {
720 free(listen_addr);
721 listen_addr = NULL;
722 }
723 if (*connect_addr == '\0') {
724 free(connect_addr);
725 connect_addr = NULL;
726 }
727
728 memset(&fwd, 0, sizeof(fwd));
Damien Millerce986542013-07-18 16:12:44 +1000729 fwd.listen_port = lport;
Damien Miller7acefbb2014-07-18 14:11:24 +1000730 if (fwd.listen_port == PORT_STREAMLOCAL)
731 fwd.listen_path = listen_addr;
732 else
733 fwd.listen_host = listen_addr;
Damien Millerce986542013-07-18 16:12:44 +1000734 fwd.connect_port = cport;
Damien Miller7acefbb2014-07-18 14:11:24 +1000735 if (fwd.connect_port == PORT_STREAMLOCAL)
736 fwd.connect_path = connect_addr;
737 else
738 fwd.connect_host = connect_addr;
Damien Millere1537f92010-01-26 13:26:22 +1100739
740 debug2("%s: channel %d: request %s", __func__, c->self,
741 (fwd_desc = format_forward(ftype, &fwd)));
742
743 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
744 ftype != MUX_FWD_DYNAMIC) {
745 logit("%s: invalid forwarding type %u", __func__, ftype);
746 invalid:
Damien Miller7acefbb2014-07-18 14:11:24 +1000747 free(listen_addr);
748 free(connect_addr);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000749 reply_error(reply, MUX_S_FAILURE, rid,
750 "Invalid forwarding request");
Damien Millere1537f92010-01-26 13:26:22 +1100751 return 0;
752 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000753 if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) {
754 logit("%s: streamlocal and dynamic forwards "
755 "are mutually exclusive", __func__);
756 goto invalid;
757 }
758 if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) {
Damien Millere1537f92010-01-26 13:26:22 +1100759 logit("%s: invalid listen port %u", __func__,
760 fwd.listen_port);
761 goto invalid;
762 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000763 if ((fwd.connect_port != PORT_STREAMLOCAL &&
764 fwd.connect_port >= 65536) ||
765 (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE &&
766 fwd.connect_port == 0)) {
Damien Millere1537f92010-01-26 13:26:22 +1100767 logit("%s: invalid connect port %u", __func__,
768 fwd.connect_port);
769 goto invalid;
770 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000771 if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL &&
772 fwd.connect_path == NULL) {
Damien Millere1537f92010-01-26 13:26:22 +1100773 logit("%s: missing connect host", __func__);
774 goto invalid;
775 }
776
777 /* Skip forwards that have already been requested */
778 switch (ftype) {
779 case MUX_FWD_LOCAL:
780 case MUX_FWD_DYNAMIC:
781 for (i = 0; i < options.num_local_forwards; i++) {
782 if (compare_forward(&fwd,
783 options.local_forwards + i)) {
784 exists:
785 debug2("%s: found existing forwarding",
786 __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000787 reply_ok(reply, rid);
Damien Millere1537f92010-01-26 13:26:22 +1100788 goto out;
789 }
790 }
791 break;
792 case MUX_FWD_REMOTE:
793 for (i = 0; i < options.num_remote_forwards; i++) {
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000794 if (!compare_forward(&fwd, options.remote_forwards + i))
795 continue;
796 if (fwd.listen_port != 0)
797 goto exists;
798 debug2("%s: found allocated port", __func__);
799 if ((r = sshbuf_put_u32(reply,
800 MUX_S_REMOTE_PORT)) != 0 ||
801 (r = sshbuf_put_u32(reply, rid)) != 0 ||
802 (r = sshbuf_put_u32(reply,
803 options.remote_forwards[i].allocated_port)) != 0)
804 fatal("%s: reply: %s", __func__, ssh_err(r));
805 goto out;
Damien Millere1537f92010-01-26 13:26:22 +1100806 }
807 break;
808 }
809
810 if (options.control_master == SSHCTL_MASTER_ASK ||
811 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
812 if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
813 debug2("%s: forwarding refused by user", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000814 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
815 "Permission denied");
Damien Millere1537f92010-01-26 13:26:22 +1100816 goto out;
817 }
818 }
819
820 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000821 if (!channel_setup_local_fwd_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000822 &options.fwd_opts)) {
Damien Millere1537f92010-01-26 13:26:22 +1100823 fail:
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000824 logit("%s: requested %s failed", __func__, fwd_desc);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000825 reply_error(reply, MUX_S_FAILURE, rid,
826 "Port forwarding failed");
Damien Millere1537f92010-01-26 13:26:22 +1100827 goto out;
828 }
829 add_local_forward(&options, &fwd);
830 freefwd = 0;
831 } else {
Damien Miller388f6fc2010-05-21 14:57:35 +1000832 struct mux_channel_confirm_ctx *fctx;
833
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000834 fwd.handle = channel_request_remote_forwarding(ssh, &fwd);
Darren Tucker68afb8c2011-10-02 18:59:03 +1100835 if (fwd.handle < 0)
Damien Millere1537f92010-01-26 13:26:22 +1100836 goto fail;
837 add_remote_forward(&options, &fwd);
Damien Miller388f6fc2010-05-21 14:57:35 +1000838 fctx = xcalloc(1, sizeof(*fctx));
839 fctx->cid = c->self;
840 fctx->rid = rid;
Damien Miller232cfb12010-06-26 09:50:30 +1000841 fctx->fid = options.num_remote_forwards - 1;
Damien Miller388f6fc2010-05-21 14:57:35 +1000842 client_register_global_confirm(mux_confirm_remote_forward,
843 fctx);
Damien Millere1537f92010-01-26 13:26:22 +1100844 freefwd = 0;
Damien Miller388f6fc2010-05-21 14:57:35 +1000845 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
846 /* delayed reply in mux_confirm_remote_forward */
847 goto out;
Damien Millere1537f92010-01-26 13:26:22 +1100848 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000849 reply_ok(reply, rid);
Damien Millere1537f92010-01-26 13:26:22 +1100850 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000851 free(fwd_desc);
Damien Millere1537f92010-01-26 13:26:22 +1100852 if (freefwd) {
Darren Tuckera627d422013-06-02 07:31:17 +1000853 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000854 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000855 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000856 free(fwd.connect_path);
Damien Millere1537f92010-01-26 13:26:22 +1100857 }
858 return ret;
859}
860
861static int
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000862mux_master_process_close_fwd(struct ssh *ssh, u_int rid,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000863 Channel *c, struct sshbuf *m, struct sshbuf *reply)
Damien Millere1537f92010-01-26 13:26:22 +1100864{
Damien Miller7acefbb2014-07-18 14:11:24 +1000865 struct Forward fwd, *found_fwd;
Damien Millere1537f92010-01-26 13:26:22 +1100866 char *fwd_desc = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000867 const char *error_reason = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000868 char *listen_addr = NULL, *connect_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100869 u_int ftype;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000870 int r, i, ret = 0;
Damien Millerce986542013-07-18 16:12:44 +1000871 u_int lport, cport;
Damien Millere1537f92010-01-26 13:26:22 +1100872
djm@openbsd.org45b0eb72015-08-19 23:18:26 +0000873 memset(&fwd, 0, sizeof(fwd));
874
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000875 if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
876 (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
877 (r = sshbuf_get_u32(m, &lport)) != 0 ||
878 (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
879 (r = sshbuf_get_u32(m, &cport)) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000880 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
881 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
Damien Millere1537f92010-01-26 13:26:22 +1100882 error("%s: malformed message", __func__);
883 ret = -1;
884 goto out;
885 }
886
Damien Miller7acefbb2014-07-18 14:11:24 +1000887 if (*listen_addr == '\0') {
888 free(listen_addr);
889 listen_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100890 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000891 if (*connect_addr == '\0') {
892 free(connect_addr);
893 connect_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100894 }
895
Damien Miller7acefbb2014-07-18 14:11:24 +1000896 memset(&fwd, 0, sizeof(fwd));
897 fwd.listen_port = lport;
898 if (fwd.listen_port == PORT_STREAMLOCAL)
899 fwd.listen_path = listen_addr;
900 else
901 fwd.listen_host = listen_addr;
902 fwd.connect_port = cport;
903 if (fwd.connect_port == PORT_STREAMLOCAL)
904 fwd.connect_path = connect_addr;
905 else
906 fwd.connect_host = connect_addr;
907
Damien Millerf6dff7c2011-09-22 21:38:52 +1000908 debug2("%s: channel %d: request cancel %s", __func__, c->self,
Damien Millere1537f92010-01-26 13:26:22 +1100909 (fwd_desc = format_forward(ftype, &fwd)));
910
Damien Millerf6dff7c2011-09-22 21:38:52 +1000911 /* make sure this has been requested */
912 found_fwd = NULL;
913 switch (ftype) {
914 case MUX_FWD_LOCAL:
915 case MUX_FWD_DYNAMIC:
916 for (i = 0; i < options.num_local_forwards; i++) {
917 if (compare_forward(&fwd,
918 options.local_forwards + i)) {
919 found_fwd = options.local_forwards + i;
920 break;
921 }
922 }
923 break;
924 case MUX_FWD_REMOTE:
925 for (i = 0; i < options.num_remote_forwards; i++) {
926 if (compare_forward(&fwd,
927 options.remote_forwards + i)) {
928 found_fwd = options.remote_forwards + i;
929 break;
930 }
931 }
932 break;
933 }
Damien Millere1537f92010-01-26 13:26:22 +1100934
Damien Millerf6dff7c2011-09-22 21:38:52 +1000935 if (found_fwd == NULL)
936 error_reason = "port not forwarded";
937 else if (ftype == MUX_FWD_REMOTE) {
938 /*
939 * This shouldn't fail unless we confused the host/port
940 * between options.remote_forwards and permitted_opens.
Darren Tucker68afb8c2011-10-02 18:59:03 +1100941 * However, for dynamic allocated listen ports we need
Damien Miller7acefbb2014-07-18 14:11:24 +1000942 * to use the actual listen port.
Damien Millerf6dff7c2011-09-22 21:38:52 +1000943 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000944 if (channel_request_rforward_cancel(ssh, found_fwd) == -1)
Damien Millerf6dff7c2011-09-22 21:38:52 +1000945 error_reason = "port not in permitted opens";
946 } else { /* local and dynamic forwards */
947 /* Ditto */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000948 if (channel_cancel_lport_listener(ssh, &fwd, fwd.connect_port,
Damien Miller7acefbb2014-07-18 14:11:24 +1000949 &options.fwd_opts) == -1)
Damien Millerf6dff7c2011-09-22 21:38:52 +1000950 error_reason = "port not found";
951 }
952
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000953 if (error_reason != NULL)
954 reply_error(reply, MUX_S_FAILURE, rid, error_reason);
955 else {
956 reply_ok(reply, rid);
Darren Tuckera627d422013-06-02 07:31:17 +1000957 free(found_fwd->listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000958 free(found_fwd->listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000959 free(found_fwd->connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000960 free(found_fwd->connect_path);
Damien Millerf6dff7c2011-09-22 21:38:52 +1000961 found_fwd->listen_host = found_fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000962 found_fwd->listen_path = found_fwd->connect_path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000963 found_fwd->listen_port = found_fwd->connect_port = 0;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000964 }
Damien Millere1537f92010-01-26 13:26:22 +1100965 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000966 free(fwd_desc);
Damien Miller7acefbb2014-07-18 14:11:24 +1000967 free(listen_addr);
968 free(connect_addr);
Damien Millere1537f92010-01-26 13:26:22 +1100969
970 return ret;
971}
972
973static int
djm@openbsd.org9d883a12018-09-26 01:48:57 +0000974mux_master_process_stdio_fwd(struct ssh *ssh, u_int rid,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000975 Channel *c, struct sshbuf *m, struct sshbuf *reply)
Damien Millere1537f92010-01-26 13:26:22 +1100976{
977 Channel *nc;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000978 char *chost = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100979 u_int cport, i, j;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000980 int r, new_fd[2];
Damien Miller357610d2014-07-18 15:04:10 +1000981 struct mux_stdio_confirm_ctx *cctx;
Damien Millere1537f92010-01-26 13:26:22 +1100982
markus@openbsd.orgf4608a72018-07-09 21:18:10 +0000983 if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
984 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
985 (r = sshbuf_get_u32(m, &cport)) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000986 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +1100987 error("%s: malformed message", __func__);
988 return -1;
989 }
Damien Millere1537f92010-01-26 13:26:22 +1100990
991 debug2("%s: channel %d: request stdio fwd to %s:%u",
992 __func__, c->self, chost, cport);
993
994 /* Gather fds from client */
995 for(i = 0; i < 2; i++) {
996 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
997 error("%s: failed to receive fd %d from slave",
998 __func__, i);
999 for (j = 0; j < i; j++)
1000 close(new_fd[j]);
Darren Tuckera627d422013-06-02 07:31:17 +10001001 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +11001002
1003 /* prepare reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001004 reply_error(reply, MUX_S_FAILURE, rid,
Damien Millere1537f92010-01-26 13:26:22 +11001005 "did not receive file descriptors");
1006 return -1;
1007 }
1008 }
1009
1010 debug3("%s: got fds stdin %d, stdout %d", __func__,
1011 new_fd[0], new_fd[1]);
1012
1013 /* XXX support multiple child sessions in future */
djm@openbsd.org9f532292017-09-12 06:35:31 +00001014 if (c->have_remote_id) {
Damien Millere1537f92010-01-26 13:26:22 +11001015 debug2("%s: session already open", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001016 reply_error(reply, MUX_S_FAILURE, rid,
1017 "Multiple sessions not supported");
Damien Millere1537f92010-01-26 13:26:22 +11001018 cleanup:
1019 close(new_fd[0]);
1020 close(new_fd[1]);
Darren Tuckera627d422013-06-02 07:31:17 +10001021 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +11001022 return 0;
1023 }
1024
1025 if (options.control_master == SSHCTL_MASTER_ASK ||
1026 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
Damien Miller68512c02010-10-21 15:21:11 +11001027 if (!ask_permission("Allow forward to %s:%u? ",
Damien Millere1537f92010-01-26 13:26:22 +11001028 chost, cport)) {
1029 debug2("%s: stdio fwd refused by user", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001030 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
1031 "Permission denied");
Damien Millere1537f92010-01-26 13:26:22 +11001032 goto cleanup;
1033 }
1034 }
1035
1036 /* enable nonblocking unless tty */
1037 if (!isatty(new_fd[0]))
1038 set_nonblock(new_fd[0]);
1039 if (!isatty(new_fd[1]))
1040 set_nonblock(new_fd[1]);
1041
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001042 nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1]);
djm@openbsd.org1a660792018-07-31 03:07:24 +00001043 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +11001044
1045 nc->ctl_chan = c->self; /* link session -> control channel */
1046 c->remote_id = nc->self; /* link control -> session channel */
djm@openbsd.org9f532292017-09-12 06:35:31 +00001047 c->have_remote_id = 1;
Damien Millere1537f92010-01-26 13:26:22 +11001048
1049 debug2("%s: channel_new: %d linked to control channel %d",
1050 __func__, nc->self, nc->ctl_chan);
1051
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001052 channel_register_cleanup(ssh, nc->self,
1053 mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +11001054
Damien Miller357610d2014-07-18 15:04:10 +10001055 cctx = xcalloc(1, sizeof(*cctx));
1056 cctx->rid = rid;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001057 channel_register_open_confirm(ssh, nc->self, mux_stdio_confirm, cctx);
Damien Miller357610d2014-07-18 15:04:10 +10001058 c->mux_pause = 1; /* stop handling messages until open_confirm done */
Damien Millere1537f92010-01-26 13:26:22 +11001059
Damien Miller357610d2014-07-18 15:04:10 +10001060 /* reply is deferred, sent by mux_session_confirm */
Damien Millere1537f92010-01-26 13:26:22 +11001061 return 0;
1062}
1063
Damien Miller357610d2014-07-18 15:04:10 +10001064/* Callback on open confirmation in mux master for a mux stdio fwd session. */
1065static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001066mux_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
Damien Miller357610d2014-07-18 15:04:10 +10001067{
1068 struct mux_stdio_confirm_ctx *cctx = arg;
1069 Channel *c, *cc;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001070 struct sshbuf *reply;
1071 int r;
Damien Miller357610d2014-07-18 15:04:10 +10001072
1073 if (cctx == NULL)
1074 fatal("%s: cctx == NULL", __func__);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001075 if ((c = channel_by_id(ssh, id)) == NULL)
Damien Miller357610d2014-07-18 15:04:10 +10001076 fatal("%s: no channel for id %d", __func__, id);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001077 if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
Damien Miller357610d2014-07-18 15:04:10 +10001078 fatal("%s: channel %d lacks control channel %d", __func__,
1079 id, c->ctl_chan);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001080 if ((reply = sshbuf_new()) == NULL)
1081 fatal("%s: sshbuf_new", __func__);
Damien Miller357610d2014-07-18 15:04:10 +10001082
1083 if (!success) {
1084 debug3("%s: sending failure reply", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001085 reply_error(reply, MUX_S_FAILURE, cctx->rid,
1086 "Session open refused by peer");
Damien Miller357610d2014-07-18 15:04:10 +10001087 /* prepare reply */
Damien Miller357610d2014-07-18 15:04:10 +10001088 goto done;
1089 }
1090
1091 debug3("%s: sending success reply", __func__);
1092 /* prepare reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001093 if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
1094 (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
1095 (r = sshbuf_put_u32(reply, c->self)) != 0)
1096 fatal("%s: reply: %s", __func__, ssh_err(r));
Damien Miller357610d2014-07-18 15:04:10 +10001097
1098 done:
1099 /* Send reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001100 if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
1101 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
1102 sshbuf_free(reply);
Damien Miller357610d2014-07-18 15:04:10 +10001103
1104 if (cc->mux_pause <= 0)
1105 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1106 cc->mux_pause = 0; /* start processing messages again */
1107 c->open_confirm_ctx = NULL;
1108 free(cctx);
1109}
1110
Damien Miller6c3eec72011-05-05 14:16:22 +10001111static int
djm@openbsd.org9d883a12018-09-26 01:48:57 +00001112mux_master_process_stop_listening(struct ssh *ssh, u_int rid,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001113 Channel *c, struct sshbuf *m, struct sshbuf *reply)
Damien Miller6c3eec72011-05-05 14:16:22 +10001114{
1115 debug("%s: channel %d: stop listening", __func__, c->self);
1116
1117 if (options.control_master == SSHCTL_MASTER_ASK ||
1118 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
1119 if (!ask_permission("Disable further multiplexing on shared "
1120 "connection to %s? ", host)) {
1121 debug2("%s: stop listen refused by user", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001122 reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
1123 "Permission denied");
Damien Miller6c3eec72011-05-05 14:16:22 +10001124 return 0;
1125 }
1126 }
1127
1128 if (mux_listener_channel != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001129 channel_free(ssh, mux_listener_channel);
Damien Miller6c3eec72011-05-05 14:16:22 +10001130 client_stop_mux();
Darren Tuckera627d422013-06-02 07:31:17 +10001131 free(options.control_path);
Damien Miller6c3eec72011-05-05 14:16:22 +10001132 options.control_path = NULL;
1133 mux_listener_channel = NULL;
1134 muxserver_sock = -1;
1135 }
1136
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001137 reply_ok(reply, rid);
Damien Miller6c3eec72011-05-05 14:16:22 +10001138 return 0;
1139}
1140
markus@openbsd.org8d057842016-09-30 09:19:13 +00001141static int
djm@openbsd.org9d883a12018-09-26 01:48:57 +00001142mux_master_process_proxy(struct ssh *ssh, u_int rid,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001143 Channel *c, struct sshbuf *m, struct sshbuf *reply)
markus@openbsd.org8d057842016-09-30 09:19:13 +00001144{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001145 int r;
1146
markus@openbsd.org8d057842016-09-30 09:19:13 +00001147 debug("%s: channel %d: proxy request", __func__, c->self);
1148
1149 c->mux_rcb = channel_proxy_downstream;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001150 if ((r = sshbuf_put_u32(reply, MUX_S_PROXY)) != 0 ||
1151 (r = sshbuf_put_u32(reply, rid)) != 0)
1152 fatal("%s: reply: %s", __func__, ssh_err(r));
markus@openbsd.org8d057842016-09-30 09:19:13 +00001153
1154 return 0;
1155}
1156
Damien Millere1537f92010-01-26 13:26:22 +11001157/* Channel callbacks fired on read/write from mux slave fd */
1158static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001159mux_master_read_cb(struct ssh *ssh, Channel *c)
Damien Millere1537f92010-01-26 13:26:22 +11001160{
1161 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001162 struct sshbuf *in = NULL, *out = NULL;
1163 u_int type, rid, i;
1164 int r, ret = -1;
1165
1166 if ((out = sshbuf_new()) == NULL)
1167 fatal("%s: sshbuf_new", __func__);
Damien Millere1537f92010-01-26 13:26:22 +11001168
1169 /* Setup ctx and */
1170 if (c->mux_ctx == NULL) {
Damien Millerc094d1e2010-06-26 09:36:34 +10001171 state = xcalloc(1, sizeof(*state));
Damien Millere1537f92010-01-26 13:26:22 +11001172 c->mux_ctx = state;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001173 channel_register_cleanup(ssh, c->self,
Damien Millere1537f92010-01-26 13:26:22 +11001174 mux_master_control_cleanup_cb, 0);
1175
1176 /* Send hello */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001177 if ((r = sshbuf_put_u32(out, MUX_MSG_HELLO)) != 0 ||
1178 (r = sshbuf_put_u32(out, SSHMUX_VER)) != 0)
1179 fatal("%s: reply: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001180 /* no extensions */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001181 if ((r = sshbuf_put_stringb(c->output, out)) != 0)
1182 fatal("%s: sshbuf_put_stringb: %s",
1183 __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001184 debug3("%s: channel %d: hello sent", __func__, c->self);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001185 ret = 0;
1186 goto out;
Damien Millere1537f92010-01-26 13:26:22 +11001187 }
1188
Damien Millere1537f92010-01-26 13:26:22 +11001189 /* Channel code ensures that we receive whole packets */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001190 if ((r = sshbuf_froms(c->input, &in)) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11001191 malf:
1192 error("%s: malformed message", __func__);
1193 goto out;
1194 }
Damien Millere1537f92010-01-26 13:26:22 +11001195
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001196 if ((r = sshbuf_get_u32(in, &type)) != 0)
Damien Millere1537f92010-01-26 13:26:22 +11001197 goto malf;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001198 debug3("%s: channel %d packet type 0x%08x len %zu",
1199 __func__, c->self, type, sshbuf_len(in));
Damien Millere1537f92010-01-26 13:26:22 +11001200
1201 if (type == MUX_MSG_HELLO)
1202 rid = 0;
1203 else {
1204 if (!state->hello_rcvd) {
1205 error("%s: expected MUX_MSG_HELLO(0x%08x), "
1206 "received 0x%08x", __func__, MUX_MSG_HELLO, type);
1207 goto out;
1208 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001209 if ((r = sshbuf_get_u32(in, &rid)) != 0)
Damien Millere1537f92010-01-26 13:26:22 +11001210 goto malf;
1211 }
1212
1213 for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
1214 if (type == mux_master_handlers[i].type) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001215 ret = mux_master_handlers[i].handler(ssh, rid,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001216 c, in, out);
Damien Millere1537f92010-01-26 13:26:22 +11001217 break;
1218 }
1219 }
1220 if (mux_master_handlers[i].handler == NULL) {
1221 error("%s: unsupported mux message 0x%08x", __func__, type);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001222 reply_error(out, MUX_S_FAILURE, rid, "unsupported request");
Damien Millere1537f92010-01-26 13:26:22 +11001223 ret = 0;
1224 }
1225 /* Enqueue reply packet */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001226 if (sshbuf_len(out) != 0) {
1227 if ((r = sshbuf_put_stringb(c->output, out)) != 0)
1228 fatal("%s: sshbuf_put_stringb: %s",
1229 __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001230 }
1231 out:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001232 sshbuf_free(in);
1233 sshbuf_free(out);
Damien Millere1537f92010-01-26 13:26:22 +11001234 return ret;
1235}
1236
1237void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001238mux_exit_message(struct ssh *ssh, Channel *c, int exitval)
Damien Millere1537f92010-01-26 13:26:22 +11001239{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001240 struct sshbuf *m;
Damien Millere1537f92010-01-26 13:26:22 +11001241 Channel *mux_chan;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001242 int r;
Damien Millere1537f92010-01-26 13:26:22 +11001243
Damien Millerbc02f162013-04-23 19:25:49 +10001244 debug3("%s: channel %d: exit message, exitval %d", __func__, c->self,
Damien Millere1537f92010-01-26 13:26:22 +11001245 exitval);
1246
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001247 if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
Damien Millere1537f92010-01-26 13:26:22 +11001248 fatal("%s: channel %d missing mux channel %d",
1249 __func__, c->self, c->ctl_chan);
1250
1251 /* Append exit message packet to control socket output queue */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001252 if ((m = sshbuf_new()) == NULL)
1253 fatal("%s: sshbuf_new", __func__);
1254 if ((r = sshbuf_put_u32(m, MUX_S_EXIT_MESSAGE)) != 0 ||
1255 (r = sshbuf_put_u32(m, c->self)) != 0 ||
1256 (r = sshbuf_put_u32(m, exitval)) != 0 ||
1257 (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
1258 fatal("%s: reply: %s", __func__, ssh_err(r));
1259 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11001260}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001261
Damien Miller555f3b82011-05-15 08:48:05 +10001262void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001263mux_tty_alloc_failed(struct ssh *ssh, Channel *c)
Damien Miller555f3b82011-05-15 08:48:05 +10001264{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001265 struct sshbuf *m;
Damien Miller555f3b82011-05-15 08:48:05 +10001266 Channel *mux_chan;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001267 int r;
Damien Miller555f3b82011-05-15 08:48:05 +10001268
1269 debug3("%s: channel %d: TTY alloc failed", __func__, c->self);
1270
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001271 if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
Damien Miller555f3b82011-05-15 08:48:05 +10001272 fatal("%s: channel %d missing mux channel %d",
1273 __func__, c->self, c->ctl_chan);
1274
1275 /* Append exit message packet to control socket output queue */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001276 if ((m = sshbuf_new()) == NULL)
1277 fatal("%s: sshbuf_new", __func__);
1278 if ((r = sshbuf_put_u32(m, MUX_S_TTY_ALLOC_FAIL)) != 0 ||
1279 (r = sshbuf_put_u32(m, c->self)) != 0 ||
1280 (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
1281 fatal("%s: reply: %s", __func__, ssh_err(r));
1282 sshbuf_free(m);
Damien Miller555f3b82011-05-15 08:48:05 +10001283}
1284
Damien Millerb1cbfa22008-05-19 16:00:08 +10001285/* Prepare a mux master to listen on a Unix domain socket. */
1286void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001287muxserver_listen(struct ssh *ssh)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001288{
Damien Millerb1cbfa22008-05-19 16:00:08 +10001289 mode_t old_umask;
Damien Miller603134e2010-09-24 22:07:55 +10001290 char *orig_control_path = options.control_path;
1291 char rbuf[16+1];
1292 u_int i, r;
Damien Millerf42f7682014-07-18 15:03:27 +10001293 int oerrno;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001294
1295 if (options.control_path == NULL ||
1296 options.control_master == SSHCTL_MASTER_NO)
1297 return;
1298
1299 debug("setting up multiplex master socket");
1300
Damien Miller603134e2010-09-24 22:07:55 +10001301 /*
1302 * Use a temporary path before listen so we can pseudo-atomically
1303 * establish the listening socket in its final location to avoid
1304 * other processes racing in between bind() and listen() and hitting
1305 * an unready socket.
1306 */
1307 for (i = 0; i < sizeof(rbuf) - 1; i++) {
1308 r = arc4random_uniform(26+26+10);
1309 rbuf[i] = (r < 26) ? 'a' + r :
1310 (r < 26*2) ? 'A' + r - 26 :
1311 '0' + r - 26 - 26;
1312 }
1313 rbuf[sizeof(rbuf) - 1] = '\0';
1314 options.control_path = NULL;
1315 xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
1316 debug3("%s: temporary control path %s", __func__, options.control_path);
1317
Damien Millerb1cbfa22008-05-19 16:00:08 +10001318 old_umask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001319 muxserver_sock = unix_listener(options.control_path, 64, 0);
Damien Millerf42f7682014-07-18 15:03:27 +10001320 oerrno = errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10001321 umask(old_umask);
1322 if (muxserver_sock < 0) {
Damien Millerf42f7682014-07-18 15:03:27 +10001323 if (oerrno == EINVAL || oerrno == EADDRINUSE) {
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001324 error("ControlSocket %s already exists, "
1325 "disabling multiplexing", options.control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001326 disable_mux_master:
Damien Miller60432d82011-05-15 08:34:46 +10001327 if (muxserver_sock != -1) {
1328 close(muxserver_sock);
1329 muxserver_sock = -1;
1330 }
Darren Tuckera627d422013-06-02 07:31:17 +10001331 free(orig_control_path);
1332 free(options.control_path);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001333 options.control_path = NULL;
1334 options.control_master = SSHCTL_MASTER_NO;
1335 return;
Damien Miller7acefbb2014-07-18 14:11:24 +10001336 } else {
1337 /* unix_listener() logs the error */
1338 cleanup_exit(255);
1339 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001340 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001341
Damien Miller603134e2010-09-24 22:07:55 +10001342 /* Now atomically "move" the mux socket into position */
1343 if (link(options.control_path, orig_control_path) != 0) {
1344 if (errno != EEXIST) {
djm@openbsd.org95687f52016-04-01 02:34:10 +00001345 fatal("%s: link mux listener %s => %s: %s", __func__,
Damien Miller603134e2010-09-24 22:07:55 +10001346 options.control_path, orig_control_path,
1347 strerror(errno));
1348 }
1349 error("ControlSocket %s already exists, disabling multiplexing",
1350 orig_control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001351 unlink(options.control_path);
1352 goto disable_mux_master;
1353 }
1354 unlink(options.control_path);
Darren Tuckera627d422013-06-02 07:31:17 +10001355 free(options.control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001356 options.control_path = orig_control_path;
1357
Damien Millerb1cbfa22008-05-19 16:00:08 +10001358 set_nonblock(muxserver_sock);
Damien Millere1537f92010-01-26 13:26:22 +11001359
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001360 mux_listener_channel = channel_new(ssh, "mux listener",
Damien Millere1537f92010-01-26 13:26:22 +11001361 SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
1362 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller603134e2010-09-24 22:07:55 +10001363 0, options.control_path, 1);
Damien Millere1537f92010-01-26 13:26:22 +11001364 mux_listener_channel->mux_rcb = mux_master_read_cb;
1365 debug3("%s: mux listener channel %d fd %d", __func__,
1366 mux_listener_channel->self, mux_listener_channel->sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001367}
1368
1369/* Callback on open confirmation in mux master for a mux client session. */
1370static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001371mux_session_confirm(struct ssh *ssh, int id, int success, void *arg)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001372{
1373 struct mux_session_confirm_ctx *cctx = arg;
1374 const char *display;
Damien Millerd530f5f2010-05-21 14:57:10 +10001375 Channel *c, *cc;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001376 int i, r;
1377 struct sshbuf *reply;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001378
1379 if (cctx == NULL)
1380 fatal("%s: cctx == NULL", __func__);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001381 if ((c = channel_by_id(ssh, id)) == NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001382 fatal("%s: no channel for id %d", __func__, id);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001383 if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
Damien Millerd530f5f2010-05-21 14:57:10 +10001384 fatal("%s: channel %d lacks control channel %d", __func__,
1385 id, c->ctl_chan);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001386 if ((reply = sshbuf_new()) == NULL)
1387 fatal("%s: sshbuf_new", __func__);
Damien Millerd530f5f2010-05-21 14:57:10 +10001388
1389 if (!success) {
1390 debug3("%s: sending failure reply", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001391 reply_error(reply, MUX_S_FAILURE, cctx->rid,
1392 "Session open refused by peer");
Damien Millerd530f5f2010-05-21 14:57:10 +10001393 goto done;
1394 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001395
1396 display = getenv("DISPLAY");
1397 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1398 char *proto, *data;
Damien Miller1ab6a512010-06-26 10:02:24 +10001399
Damien Millerb1cbfa22008-05-19 16:00:08 +10001400 /* Get reasonable local authentication information. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001401 if (client_x11_get_proto(ssh, display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001402 options.forward_x11_trusted, options.forward_x11_timeout,
djm@openbsd.orged4ce822016-01-13 23:04:47 +00001403 &proto, &data) == 0) {
1404 /* Request forwarding with authentication spoofing. */
1405 debug("Requesting X11 forwarding with authentication "
1406 "spoofing.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001407 x11_request_forwarding_with_spoofing(ssh, id,
1408 display, proto, data, 1);
djm@openbsd.orged4ce822016-01-13 23:04:47 +00001409 /* XXX exit_on_forward_failure */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001410 client_expect_confirm(ssh, id, "X11 forwarding",
djm@openbsd.orged4ce822016-01-13 23:04:47 +00001411 CONFIRM_WARN);
1412 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001413 }
1414
1415 if (cctx->want_agent_fwd && options.forward_agent) {
1416 debug("Requesting authentication agent forwarding.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001417 channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
djm@openbsd.orge3128b32019-01-19 21:35:25 +00001418 if ((r = sshpkt_send(ssh)) != 0)
1419 fatal("%s: packet error: %s", __func__, ssh_err(r));
Damien Millerb1cbfa22008-05-19 16:00:08 +10001420 }
1421
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001422 client_session2_setup(ssh, id, cctx->want_tty, cctx->want_subsys,
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001423 cctx->term, &cctx->tio, c->rfd, cctx->cmd, cctx->env);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001424
Damien Millerd530f5f2010-05-21 14:57:10 +10001425 debug3("%s: sending success reply", __func__);
1426 /* prepare reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001427 if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
1428 (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
1429 (r = sshbuf_put_u32(reply, c->self)) != 0)
1430 fatal("%s: reply: %s", __func__, ssh_err(r));
Damien Millerd530f5f2010-05-21 14:57:10 +10001431
1432 done:
1433 /* Send reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001434 if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
1435 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
1436 sshbuf_free(reply);
Damien Millerd530f5f2010-05-21 14:57:10 +10001437
1438 if (cc->mux_pause <= 0)
1439 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1440 cc->mux_pause = 0; /* start processing messages again */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001441 c->open_confirm_ctx = NULL;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001442 sshbuf_free(cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +10001443 free(cctx->term);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001444 if (cctx->env != NULL) {
1445 for (i = 0; cctx->env[i] != NULL; i++)
Darren Tuckera627d422013-06-02 07:31:17 +10001446 free(cctx->env[i]);
1447 free(cctx->env);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001448 }
Darren Tuckera627d422013-06-02 07:31:17 +10001449 free(cctx);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001450}
1451
Damien Millerb1cbfa22008-05-19 16:00:08 +10001452/* ** Multiplexing client support */
1453
1454/* Exit signal handler */
1455static void
1456control_client_sighandler(int signo)
1457{
1458 muxclient_terminate = signo;
1459}
1460
1461/*
1462 * Relay signal handler - used to pass some signals from mux client to
1463 * mux master.
1464 */
1465static void
1466control_client_sigrelay(int signo)
1467{
1468 int save_errno = errno;
1469
1470 if (muxserver_pid > 1)
1471 kill(muxserver_pid, signo);
1472
1473 errno = save_errno;
1474}
1475
Damien Millerb1cbfa22008-05-19 16:00:08 +10001476static int
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001477mux_client_read(int fd, struct sshbuf *b, size_t need)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001478{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001479 size_t have;
Damien Millere1537f92010-01-26 13:26:22 +11001480 ssize_t len;
1481 u_char *p;
1482 struct pollfd pfd;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001483 int r;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001484
Damien Millere1537f92010-01-26 13:26:22 +11001485 pfd.fd = fd;
1486 pfd.events = POLLIN;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001487 if ((r = sshbuf_reserve(b, need, &p)) != 0)
1488 fatal("%s: reserve: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001489 for (have = 0; have < need; ) {
1490 if (muxclient_terminate) {
1491 errno = EINTR;
1492 return -1;
1493 }
1494 len = read(fd, p + have, need - have);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001495 if (len == -1) {
Damien Millere1537f92010-01-26 13:26:22 +11001496 switch (errno) {
1497#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1498 case EWOULDBLOCK:
1499#endif
1500 case EAGAIN:
1501 (void)poll(&pfd, 1, -1);
1502 /* FALLTHROUGH */
1503 case EINTR:
1504 continue;
1505 default:
1506 return -1;
1507 }
1508 }
1509 if (len == 0) {
1510 errno = EPIPE;
1511 return -1;
1512 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001513 have += (size_t)len;
Damien Millere1537f92010-01-26 13:26:22 +11001514 }
1515 return 0;
1516}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001517
Damien Millere1537f92010-01-26 13:26:22 +11001518static int
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001519mux_client_write_packet(int fd, struct sshbuf *m)
Damien Millere1537f92010-01-26 13:26:22 +11001520{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001521 struct sshbuf *queue;
Damien Millere1537f92010-01-26 13:26:22 +11001522 u_int have, need;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001523 int r, oerrno, len;
1524 const u_char *ptr;
Damien Millere1537f92010-01-26 13:26:22 +11001525 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001526
Damien Millere1537f92010-01-26 13:26:22 +11001527 pfd.fd = fd;
1528 pfd.events = POLLOUT;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001529 if ((queue = sshbuf_new()) == NULL)
1530 fatal("%s: sshbuf_new", __func__);
1531 if ((r = sshbuf_put_stringb(queue, m)) != 0)
1532 fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001533
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001534 need = sshbuf_len(queue);
1535 ptr = sshbuf_ptr(queue);
Damien Millere1537f92010-01-26 13:26:22 +11001536
1537 for (have = 0; have < need; ) {
1538 if (muxclient_terminate) {
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001539 sshbuf_free(queue);
Damien Millere1537f92010-01-26 13:26:22 +11001540 errno = EINTR;
1541 return -1;
1542 }
1543 len = write(fd, ptr + have, need - have);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001544 if (len == -1) {
Damien Millere1537f92010-01-26 13:26:22 +11001545 switch (errno) {
1546#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1547 case EWOULDBLOCK:
1548#endif
1549 case EAGAIN:
1550 (void)poll(&pfd, 1, -1);
1551 /* FALLTHROUGH */
1552 case EINTR:
1553 continue;
1554 default:
1555 oerrno = errno;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001556 sshbuf_free(queue);
Damien Millere1537f92010-01-26 13:26:22 +11001557 errno = oerrno;
1558 return -1;
1559 }
1560 }
1561 if (len == 0) {
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001562 sshbuf_free(queue);
Damien Millere1537f92010-01-26 13:26:22 +11001563 errno = EPIPE;
1564 return -1;
1565 }
1566 have += (u_int)len;
1567 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001568 sshbuf_free(queue);
Damien Millere1537f92010-01-26 13:26:22 +11001569 return 0;
1570}
1571
1572static int
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001573mux_client_read_packet(int fd, struct sshbuf *m)
Damien Millere1537f92010-01-26 13:26:22 +11001574{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001575 struct sshbuf *queue;
1576 size_t need, have;
Damien Miller633de332014-05-15 13:48:26 +10001577 const u_char *ptr;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001578 int r, oerrno;
Damien Millere1537f92010-01-26 13:26:22 +11001579
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001580 if ((queue = sshbuf_new()) == NULL)
1581 fatal("%s: sshbuf_new", __func__);
1582 if (mux_client_read(fd, queue, 4) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11001583 if ((oerrno = errno) == EPIPE)
Darren Tucker746e9062013-06-06 08:20:13 +10001584 debug3("%s: read header failed: %s", __func__,
1585 strerror(errno));
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001586 sshbuf_free(queue);
Damien Millere1537f92010-01-26 13:26:22 +11001587 errno = oerrno;
1588 return -1;
1589 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001590 need = PEEK_U32(sshbuf_ptr(queue));
1591 if (mux_client_read(fd, queue, need) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11001592 oerrno = errno;
1593 debug3("%s: read body failed: %s", __func__, strerror(errno));
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001594 sshbuf_free(queue);
Damien Millere1537f92010-01-26 13:26:22 +11001595 errno = oerrno;
1596 return -1;
1597 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001598 if ((r = sshbuf_get_string_direct(queue, &ptr, &have)) != 0 ||
1599 (r = sshbuf_put(m, ptr, have)) != 0)
1600 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1601 sshbuf_free(queue);
Damien Millere1537f92010-01-26 13:26:22 +11001602 return 0;
1603}
1604
1605static int
1606mux_client_hello_exchange(int fd)
1607{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001608 struct sshbuf *m;
Damien Millere1537f92010-01-26 13:26:22 +11001609 u_int type, ver;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001610 int r, ret = -1;
Damien Millere1537f92010-01-26 13:26:22 +11001611
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001612 if ((m = sshbuf_new()) == NULL)
1613 fatal("%s: sshbuf_new", __func__);
1614 if ((r = sshbuf_put_u32(m, MUX_MSG_HELLO)) != 0 ||
1615 (r = sshbuf_put_u32(m, SSHMUX_VER)) != 0)
1616 fatal("%s: hello: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001617 /* no extensions */
1618
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001619 if (mux_client_write_packet(fd, m) != 0) {
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001620 debug("%s: write packet: %s", __func__, strerror(errno));
1621 goto out;
1622 }
Damien Millere1537f92010-01-26 13:26:22 +11001623
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001624 sshbuf_reset(m);
Damien Millere1537f92010-01-26 13:26:22 +11001625
1626 /* Read their HELLO */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001627 if (mux_client_read_packet(fd, m) != 0) {
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001628 debug("%s: read packet failed", __func__);
1629 goto out;
Damien Millere1537f92010-01-26 13:26:22 +11001630 }
1631
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001632 if ((r = sshbuf_get_u32(m, &type)) != 0)
1633 fatal("%s: decode type: %s", __func__, ssh_err(r));
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001634 if (type != MUX_MSG_HELLO) {
1635 error("%s: expected HELLO (%u) received %u",
Damien Millere1537f92010-01-26 13:26:22 +11001636 __func__, MUX_MSG_HELLO, type);
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001637 goto out;
1638 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001639 if ((r = sshbuf_get_u32(m, &ver)) != 0)
1640 fatal("%s: decode version: %s", __func__, ssh_err(r));
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001641 if (ver != SSHMUX_VER) {
1642 error("Unsupported multiplexing protocol version %d "
Damien Millere1537f92010-01-26 13:26:22 +11001643 "(expected %d)", ver, SSHMUX_VER);
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001644 goto out;
1645 }
Damien Millere1537f92010-01-26 13:26:22 +11001646 debug2("%s: master version %u", __func__, ver);
1647 /* No extensions are presently defined */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001648 while (sshbuf_len(m) > 0) {
1649 char *name = NULL;
Damien Millere1537f92010-01-26 13:26:22 +11001650
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001651 if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
1652 (r = sshbuf_skip_string(m)) != 0) { /* value */
1653 error("%s: malformed extension: %s",
1654 __func__, ssh_err(r));
1655 goto out;
1656 }
Damien Millere1537f92010-01-26 13:26:22 +11001657 debug2("Unrecognised master extension \"%s\"", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001658 free(name);
Damien Millere1537f92010-01-26 13:26:22 +11001659 }
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001660 /* success */
1661 ret = 0;
1662 out:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001663 sshbuf_free(m);
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001664 return ret;
Damien Millere1537f92010-01-26 13:26:22 +11001665}
1666
1667static u_int
1668mux_client_request_alive(int fd)
1669{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001670 struct sshbuf *m;
Damien Millere1537f92010-01-26 13:26:22 +11001671 char *e;
1672 u_int pid, type, rid;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001673 int r;
Damien Millere1537f92010-01-26 13:26:22 +11001674
1675 debug3("%s: entering", __func__);
1676
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001677 if ((m = sshbuf_new()) == NULL)
1678 fatal("%s: sshbuf_new", __func__);
1679 if ((r = sshbuf_put_u32(m, MUX_C_ALIVE_CHECK)) != 0 ||
1680 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
1681 fatal("%s: request: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001682
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001683 if (mux_client_write_packet(fd, m) != 0)
Damien Millere1537f92010-01-26 13:26:22 +11001684 fatal("%s: write packet: %s", __func__, strerror(errno));
1685
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001686 sshbuf_reset(m);
Damien Millere1537f92010-01-26 13:26:22 +11001687
1688 /* Read their reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001689 if (mux_client_read_packet(fd, m) != 0) {
1690 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11001691 return 0;
1692 }
1693
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001694 if ((r = sshbuf_get_u32(m, &type)) != 0)
1695 fatal("%s: decode type: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001696 if (type != MUX_S_ALIVE) {
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001697 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1698 fatal("%s: decode error: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001699 fatal("%s: master returned error: %s", __func__, e);
1700 }
1701
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001702 if ((r = sshbuf_get_u32(m, &rid)) != 0)
1703 fatal("%s: decode remote ID: %s", __func__, ssh_err(r));
1704 if (rid != muxclient_request_id)
Damien Millere1537f92010-01-26 13:26:22 +11001705 fatal("%s: out of sequence reply: my id %u theirs %u",
1706 __func__, muxclient_request_id, rid);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001707 if ((r = sshbuf_get_u32(m, &pid)) != 0)
1708 fatal("%s: decode PID: %s", __func__, ssh_err(r));
1709 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11001710
1711 debug3("%s: done pid = %u", __func__, pid);
1712
1713 muxclient_request_id++;
1714
1715 return pid;
1716}
1717
1718static void
1719mux_client_request_terminate(int fd)
1720{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001721 struct sshbuf *m;
Damien Millere1537f92010-01-26 13:26:22 +11001722 char *e;
1723 u_int type, rid;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001724 int r;
Damien Millere1537f92010-01-26 13:26:22 +11001725
1726 debug3("%s: entering", __func__);
1727
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001728 if ((m = sshbuf_new()) == NULL)
1729 fatal("%s: sshbuf_new", __func__);
1730 if ((r = sshbuf_put_u32(m, MUX_C_TERMINATE)) != 0 ||
1731 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
1732 fatal("%s: request: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001733
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001734 if (mux_client_write_packet(fd, m) != 0)
Damien Millere1537f92010-01-26 13:26:22 +11001735 fatal("%s: write packet: %s", __func__, strerror(errno));
1736
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001737 sshbuf_reset(m);
Damien Millere1537f92010-01-26 13:26:22 +11001738
1739 /* Read their reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001740 if (mux_client_read_packet(fd, m) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11001741 /* Remote end exited already */
1742 if (errno == EPIPE) {
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001743 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11001744 return;
1745 }
1746 fatal("%s: read from master failed: %s",
1747 __func__, strerror(errno));
1748 }
1749
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001750 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1751 (r = sshbuf_get_u32(m, &rid)) != 0)
1752 fatal("%s: decode: %s", __func__, ssh_err(r));
1753 if (rid != muxclient_request_id)
Damien Millere1537f92010-01-26 13:26:22 +11001754 fatal("%s: out of sequence reply: my id %u theirs %u",
1755 __func__, muxclient_request_id, rid);
1756 switch (type) {
1757 case MUX_S_OK:
1758 break;
1759 case MUX_S_PERMISSION_DENIED:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001760 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1761 fatal("%s: decode error: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001762 fatal("Master refused termination request: %s", e);
1763 case MUX_S_FAILURE:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001764 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1765 fatal("%s: decode error: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001766 fatal("%s: termination request failed: %s", __func__, e);
1767 default:
1768 fatal("%s: unexpected response from master 0x%08x",
1769 __func__, type);
1770 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001771 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11001772 muxclient_request_id++;
1773}
1774
1775static int
Damien Miller7acefbb2014-07-18 14:11:24 +10001776mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd)
Damien Millere1537f92010-01-26 13:26:22 +11001777{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001778 struct sshbuf *m;
Damien Millere1537f92010-01-26 13:26:22 +11001779 char *e, *fwd_desc;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001780 const char *lhost, *chost;
Damien Millere1537f92010-01-26 13:26:22 +11001781 u_int type, rid;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001782 int r;
Damien Millere1537f92010-01-26 13:26:22 +11001783
1784 fwd_desc = format_forward(ftype, fwd);
Damien Millerf6dff7c2011-09-22 21:38:52 +10001785 debug("Requesting %s %s",
1786 cancel_flag ? "cancellation of" : "forwarding of", fwd_desc);
Darren Tuckera627d422013-06-02 07:31:17 +10001787 free(fwd_desc);
Damien Millere1537f92010-01-26 13:26:22 +11001788
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001789 type = cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD;
1790 if (fwd->listen_path != NULL)
1791 lhost = fwd->listen_path;
1792 else if (fwd->listen_host == NULL)
1793 lhost = "";
1794 else if (*fwd->listen_host == '\0')
1795 lhost = "*";
1796 else
1797 lhost = fwd->listen_host;
Damien Millere1537f92010-01-26 13:26:22 +11001798
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001799 if (fwd->connect_path != NULL)
1800 chost = fwd->connect_path;
1801 else if (fwd->connect_host == NULL)
1802 chost = "";
1803 else
1804 chost = fwd->connect_host;
1805
1806 if ((m = sshbuf_new()) == NULL)
1807 fatal("%s: sshbuf_new", __func__);
1808 if ((r = sshbuf_put_u32(m, type)) != 0 ||
1809 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
1810 (r = sshbuf_put_u32(m, ftype)) != 0 ||
1811 (r = sshbuf_put_cstring(m, lhost)) != 0 ||
1812 (r = sshbuf_put_u32(m, fwd->listen_port)) != 0 ||
1813 (r = sshbuf_put_cstring(m, chost)) != 0 ||
1814 (r = sshbuf_put_u32(m, fwd->connect_port)) != 0)
1815 fatal("%s: request: %s", __func__, ssh_err(r));
1816
1817 if (mux_client_write_packet(fd, m) != 0)
Damien Millere1537f92010-01-26 13:26:22 +11001818 fatal("%s: write packet: %s", __func__, strerror(errno));
1819
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001820 sshbuf_reset(m);
Damien Millere1537f92010-01-26 13:26:22 +11001821
1822 /* Read their reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001823 if (mux_client_read_packet(fd, m) != 0) {
1824 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11001825 return -1;
1826 }
1827
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001828 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1829 (r = sshbuf_get_u32(m, &rid)) != 0)
1830 fatal("%s: decode: %s", __func__, ssh_err(r));
1831 if (rid != muxclient_request_id)
Damien Millere1537f92010-01-26 13:26:22 +11001832 fatal("%s: out of sequence reply: my id %u theirs %u",
1833 __func__, muxclient_request_id, rid);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001834
Damien Millere1537f92010-01-26 13:26:22 +11001835 switch (type) {
1836 case MUX_S_OK:
1837 break;
Damien Miller388f6fc2010-05-21 14:57:35 +10001838 case MUX_S_REMOTE_PORT:
Damien Millerf6dff7c2011-09-22 21:38:52 +10001839 if (cancel_flag)
1840 fatal("%s: got MUX_S_REMOTE_PORT for cancel", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001841 if ((r = sshbuf_get_u32(m, &fwd->allocated_port)) != 0)
1842 fatal("%s: decode port: %s", __func__, ssh_err(r));
djm@openbsd.org8312cfb2015-05-01 04:01:58 +00001843 verbose("Allocated port %u for remote forward to %s:%d",
Damien Miller388f6fc2010-05-21 14:57:35 +10001844 fwd->allocated_port,
1845 fwd->connect_host ? fwd->connect_host : "",
1846 fwd->connect_port);
1847 if (muxclient_command == SSHMUX_COMMAND_FORWARD)
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +00001848 fprintf(stdout, "%i\n", fwd->allocated_port);
Damien Miller388f6fc2010-05-21 14:57:35 +10001849 break;
Damien Millere1537f92010-01-26 13:26:22 +11001850 case MUX_S_PERMISSION_DENIED:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001851 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1852 fatal("%s: decode error: %s", __func__, ssh_err(r));
1853 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11001854 error("Master refused forwarding request: %s", e);
1855 return -1;
1856 case MUX_S_FAILURE:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001857 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1858 fatal("%s: decode error: %s", __func__, ssh_err(r));
1859 sshbuf_free(m);
Damien Miller445c9a52011-01-14 12:01:29 +11001860 error("%s: forwarding request failed: %s", __func__, e);
Damien Millere1537f92010-01-26 13:26:22 +11001861 return -1;
1862 default:
1863 fatal("%s: unexpected response from master 0x%08x",
1864 __func__, type);
1865 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001866 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11001867
1868 muxclient_request_id++;
1869 return 0;
1870}
1871
1872static int
Damien Millerf6dff7c2011-09-22 21:38:52 +10001873mux_client_forwards(int fd, int cancel_flag)
Damien Millere1537f92010-01-26 13:26:22 +11001874{
Damien Millerf6dff7c2011-09-22 21:38:52 +10001875 int i, ret = 0;
Damien Millere1537f92010-01-26 13:26:22 +11001876
Damien Millerf6dff7c2011-09-22 21:38:52 +10001877 debug3("%s: %s forwardings: %d local, %d remote", __func__,
1878 cancel_flag ? "cancel" : "request",
Damien Millere1537f92010-01-26 13:26:22 +11001879 options.num_local_forwards, options.num_remote_forwards);
1880
1881 /* XXX ExitOnForwardingFailure */
1882 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10001883 if (mux_client_forward(fd, cancel_flag,
Damien Millere1537f92010-01-26 13:26:22 +11001884 options.local_forwards[i].connect_port == 0 ?
1885 MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
1886 options.local_forwards + i) != 0)
Damien Millerf6dff7c2011-09-22 21:38:52 +10001887 ret = -1;
Damien Millere1537f92010-01-26 13:26:22 +11001888 }
1889 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10001890 if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE,
Damien Millere1537f92010-01-26 13:26:22 +11001891 options.remote_forwards + i) != 0)
Damien Millerf6dff7c2011-09-22 21:38:52 +10001892 ret = -1;
Damien Millere1537f92010-01-26 13:26:22 +11001893 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10001894 return ret;
Damien Millere1537f92010-01-26 13:26:22 +11001895}
1896
1897static int
1898mux_client_request_session(int fd)
1899{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001900 struct sshbuf *m;
1901 char *e;
1902 const char *term;
1903 u_int echar, rid, sid, esid, exitval, type, exitval_seen;
Damien Millere1537f92010-01-26 13:26:22 +11001904 extern char **environ;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001905 int r, i, devnull, rawmode;
Damien Millere1537f92010-01-26 13:26:22 +11001906
1907 debug3("%s: entering", __func__);
1908
1909 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1910 error("%s: master alive request failed", __func__);
1911 return -1;
1912 }
1913
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00001914 ssh_signal(SIGPIPE, SIG_IGN);
Damien Millere1537f92010-01-26 13:26:22 +11001915
1916 if (stdin_null_flag) {
1917 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1918 fatal("open(/dev/null): %s", strerror(errno));
1919 if (dup2(devnull, STDIN_FILENO) == -1)
1920 fatal("dup2: %s", strerror(errno));
1921 if (devnull > STDERR_FILENO)
1922 close(devnull);
1923 }
1924
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001925 if ((term = getenv("TERM")) == NULL)
1926 term = "";
1927 echar = 0xffffffff;
1928 if (options.escape_char != SSH_ESCAPECHAR_NONE)
1929 echar = (u_int)options.escape_char;
Damien Millere1537f92010-01-26 13:26:22 +11001930
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001931 if ((m = sshbuf_new()) == NULL)
1932 fatal("%s: sshbuf_new", __func__);
1933 if ((r = sshbuf_put_u32(m, MUX_C_NEW_SESSION)) != 0 ||
1934 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
1935 (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
1936 (r = sshbuf_put_u32(m, tty_flag)) != 0 ||
1937 (r = sshbuf_put_u32(m, options.forward_x11)) != 0 ||
1938 (r = sshbuf_put_u32(m, options.forward_agent)) != 0 ||
1939 (r = sshbuf_put_u32(m, subsystem_flag)) != 0 ||
1940 (r = sshbuf_put_u32(m, echar)) != 0 ||
1941 (r = sshbuf_put_cstring(m, term)) != 0 ||
1942 (r = sshbuf_put_stringb(m, command)) != 0)
1943 fatal("%s: request: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001944
djm@openbsd.org7082bb52018-06-09 03:01:12 +00001945 /* Pass environment */
Damien Millere1537f92010-01-26 13:26:22 +11001946 if (options.num_send_env > 0 && environ != NULL) {
Damien Millere1537f92010-01-26 13:26:22 +11001947 for (i = 0; environ[i] != NULL; i++) {
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001948 if (!env_permitted(environ[i]))
1949 continue;
1950 if ((r = sshbuf_put_cstring(m, environ[i])) != 0)
1951 fatal("%s: request: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001952 }
1953 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001954 for (i = 0; i < options.num_setenv; i++) {
1955 if ((r = sshbuf_put_cstring(m, options.setenv[i])) != 0)
1956 fatal("%s: request: %s", __func__, ssh_err(r));
1957 }
Damien Millere1537f92010-01-26 13:26:22 +11001958
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001959 if (mux_client_write_packet(fd, m) != 0)
Damien Millere1537f92010-01-26 13:26:22 +11001960 fatal("%s: write packet: %s", __func__, strerror(errno));
1961
1962 /* Send the stdio file descriptors */
1963 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1964 mm_send_fd(fd, STDOUT_FILENO) == -1 ||
1965 mm_send_fd(fd, STDERR_FILENO) == -1)
1966 fatal("%s: send fds failed", __func__);
1967
1968 debug3("%s: session request sent", __func__);
1969
1970 /* Read their reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001971 sshbuf_reset(m);
1972 if (mux_client_read_packet(fd, m) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11001973 error("%s: read from master failed: %s",
1974 __func__, strerror(errno));
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001975 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11001976 return -1;
1977 }
1978
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001979 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1980 (r = sshbuf_get_u32(m, &rid)) != 0)
1981 fatal("%s: decode: %s", __func__, ssh_err(r));
1982 if (rid != muxclient_request_id)
Damien Millere1537f92010-01-26 13:26:22 +11001983 fatal("%s: out of sequence reply: my id %u theirs %u",
1984 __func__, muxclient_request_id, rid);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001985
Damien Millere1537f92010-01-26 13:26:22 +11001986 switch (type) {
1987 case MUX_S_SESSION_OPENED:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001988 if ((r = sshbuf_get_u32(m, &sid)) != 0)
1989 fatal("%s: decode ID: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11001990 break;
1991 case MUX_S_PERMISSION_DENIED:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001992 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1993 fatal("%s: decode error: %s", __func__, ssh_err(r));
Damien Miller445c9a52011-01-14 12:01:29 +11001994 error("Master refused session request: %s", e);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001995 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11001996 return -1;
1997 case MUX_S_FAILURE:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00001998 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
1999 fatal("%s: decode error: %s", __func__, ssh_err(r));
Damien Miller445c9a52011-01-14 12:01:29 +11002000 error("%s: session request failed: %s", __func__, e);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002001 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11002002 return -1;
2003 default:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002004 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11002005 error("%s: unexpected response from master 0x%08x",
2006 __func__, type);
2007 return -1;
2008 }
2009 muxclient_request_id++;
2010
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00002011 if (pledge("stdio proc tty", NULL) == -1)
2012 fatal("%s pledge(): %s", __func__, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11002013 platform_pledge_mux();
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00002014
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00002015 ssh_signal(SIGHUP, control_client_sighandler);
2016 ssh_signal(SIGINT, control_client_sighandler);
2017 ssh_signal(SIGTERM, control_client_sighandler);
2018 ssh_signal(SIGWINCH, control_client_sigrelay);
Damien Millere1537f92010-01-26 13:26:22 +11002019
Damien Miller555f3b82011-05-15 08:48:05 +10002020 rawmode = tty_flag;
Damien Millere1537f92010-01-26 13:26:22 +11002021 if (tty_flag)
Damien Miller21771e22011-05-15 08:45:50 +10002022 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millere1537f92010-01-26 13:26:22 +11002023
2024 /*
2025 * Stick around until the controlee closes the client_fd.
2026 * Before it does, it is expected to write an exit message.
2027 * This process must read the value and wait for the closure of
2028 * the client_fd; if this one closes early, the multiplex master will
2029 * terminate early too (possibly losing data).
2030 */
2031 for (exitval = 255, exitval_seen = 0;;) {
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002032 sshbuf_reset(m);
2033 if (mux_client_read_packet(fd, m) != 0)
Damien Millere1537f92010-01-26 13:26:22 +11002034 break;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002035 if ((r = sshbuf_get_u32(m, &type)) != 0)
2036 fatal("%s: decode type: %s", __func__, ssh_err(r));
Damien Miller555f3b82011-05-15 08:48:05 +10002037 switch (type) {
2038 case MUX_S_TTY_ALLOC_FAIL:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002039 if ((r = sshbuf_get_u32(m, &esid)) != 0)
2040 fatal("%s: decode ID: %s",
2041 __func__, ssh_err(r));
2042 if (esid != sid)
Damien Miller555f3b82011-05-15 08:48:05 +10002043 fatal("%s: tty alloc fail on unknown session: "
2044 "my id %u theirs %u",
2045 __func__, sid, esid);
2046 leave_raw_mode(options.request_tty ==
2047 REQUEST_TTY_FORCE);
2048 rawmode = 0;
2049 continue;
2050 case MUX_S_EXIT_MESSAGE:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002051 if ((r = sshbuf_get_u32(m, &esid)) != 0)
2052 fatal("%s: decode ID: %s",
2053 __func__, ssh_err(r));
2054 if (esid != sid)
Damien Miller555f3b82011-05-15 08:48:05 +10002055 fatal("%s: exit on unknown session: "
2056 "my id %u theirs %u",
2057 __func__, sid, esid);
2058 if (exitval_seen)
2059 fatal("%s: exitval sent twice", __func__);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002060 if ((r = sshbuf_get_u32(m, &exitval)) != 0)
2061 fatal("%s: decode exit value: %s",
2062 __func__, ssh_err(r));
Damien Miller555f3b82011-05-15 08:48:05 +10002063 exitval_seen = 1;
2064 continue;
2065 default:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002066 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2067 fatal("%s: decode error: %s",
2068 __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11002069 fatal("%s: master returned error: %s", __func__, e);
2070 }
Damien Millere1537f92010-01-26 13:26:22 +11002071 }
2072
2073 close(fd);
Damien Miller555f3b82011-05-15 08:48:05 +10002074 if (rawmode)
2075 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millere1537f92010-01-26 13:26:22 +11002076
2077 if (muxclient_terminate) {
dtucker@openbsd.org36945fa2017-09-20 05:19:00 +00002078 debug2("Exiting on signal: %s", strsignal(muxclient_terminate));
Damien Millere1537f92010-01-26 13:26:22 +11002079 exitval = 255;
2080 } else if (!exitval_seen) {
2081 debug2("Control master terminated unexpectedly");
2082 exitval = 255;
2083 } else
2084 debug2("Received exit status from master %d", exitval);
2085
2086 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
2087 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
2088
2089 exit(exitval);
2090}
2091
2092static int
markus@openbsd.org8d057842016-09-30 09:19:13 +00002093mux_client_proxy(int fd)
2094{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002095 struct sshbuf *m;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002096 char *e;
2097 u_int type, rid;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002098 int r;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002099
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002100 if ((m = sshbuf_new()) == NULL)
2101 fatal("%s: sshbuf_new", __func__);
2102 if ((r = sshbuf_put_u32(m, MUX_C_PROXY)) != 0 ||
2103 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
2104 fatal("%s: request: %s", __func__, ssh_err(r));
2105 if (mux_client_write_packet(fd, m) != 0)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002106 fatal("%s: write packet: %s", __func__, strerror(errno));
2107
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002108 sshbuf_reset(m);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002109
2110 /* Read their reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002111 if (mux_client_read_packet(fd, m) != 0) {
2112 sshbuf_free(m);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002113 return 0;
2114 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002115 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
2116 (r = sshbuf_get_u32(m, &rid)) != 0)
2117 fatal("%s: decode: %s", __func__, ssh_err(r));
2118 if (rid != muxclient_request_id)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002119 fatal("%s: out of sequence reply: my id %u theirs %u",
2120 __func__, muxclient_request_id, rid);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002121 if (type != MUX_S_PROXY) {
2122 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2123 fatal("%s: decode error: %s", __func__, ssh_err(r));
2124 fatal("%s: master returned error: %s", __func__, e);
2125 }
2126 sshbuf_free(m);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002127
2128 debug3("%s: done", __func__);
2129 muxclient_request_id++;
2130 return 0;
2131}
2132
2133static int
Damien Millere1537f92010-01-26 13:26:22 +11002134mux_client_request_stdio_fwd(int fd)
2135{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002136 struct sshbuf *m;
Damien Millere1537f92010-01-26 13:26:22 +11002137 char *e;
2138 u_int type, rid, sid;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002139 int r, devnull;
Damien Millere1537f92010-01-26 13:26:22 +11002140
2141 debug3("%s: entering", __func__);
2142
2143 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
2144 error("%s: master alive request failed", __func__);
2145 return -1;
2146 }
2147
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00002148 ssh_signal(SIGPIPE, SIG_IGN);
Damien Millere1537f92010-01-26 13:26:22 +11002149
2150 if (stdin_null_flag) {
2151 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
2152 fatal("open(/dev/null): %s", strerror(errno));
2153 if (dup2(devnull, STDIN_FILENO) == -1)
2154 fatal("dup2: %s", strerror(errno));
2155 if (devnull > STDERR_FILENO)
2156 close(devnull);
2157 }
2158
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002159 if ((m = sshbuf_new()) == NULL)
2160 fatal("%s: sshbuf_new", __func__);
2161 if ((r = sshbuf_put_u32(m, MUX_C_NEW_STDIO_FWD)) != 0 ||
2162 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
2163 (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
2164 (r = sshbuf_put_cstring(m, options.stdio_forward_host)) != 0 ||
2165 (r = sshbuf_put_u32(m, options.stdio_forward_port)) != 0)
2166 fatal("%s: request: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11002167
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002168 if (mux_client_write_packet(fd, m) != 0)
Damien Millere1537f92010-01-26 13:26:22 +11002169 fatal("%s: write packet: %s", __func__, strerror(errno));
2170
2171 /* Send the stdio file descriptors */
2172 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
2173 mm_send_fd(fd, STDOUT_FILENO) == -1)
2174 fatal("%s: send fds failed", __func__);
2175
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00002176 if (pledge("stdio proc tty", NULL) == -1)
2177 fatal("%s pledge(): %s", __func__, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11002178 platform_pledge_mux();
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00002179
Damien Millere1537f92010-01-26 13:26:22 +11002180 debug3("%s: stdio forward request sent", __func__);
2181
2182 /* Read their reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002183 sshbuf_reset(m);
Damien Millere1537f92010-01-26 13:26:22 +11002184
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002185 if (mux_client_read_packet(fd, m) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11002186 error("%s: read from master failed: %s",
2187 __func__, strerror(errno));
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002188 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11002189 return -1;
2190 }
2191
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002192 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
2193 (r = sshbuf_get_u32(m, &rid)) != 0)
2194 fatal("%s: decode: %s", __func__, ssh_err(r));
2195 if (rid != muxclient_request_id)
Damien Millere1537f92010-01-26 13:26:22 +11002196 fatal("%s: out of sequence reply: my id %u theirs %u",
2197 __func__, muxclient_request_id, rid);
2198 switch (type) {
2199 case MUX_S_SESSION_OPENED:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002200 if ((r = sshbuf_get_u32(m, &sid)) != 0)
2201 fatal("%s: decode ID: %s", __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +11002202 debug("%s: master session id: %u", __func__, sid);
2203 break;
2204 case MUX_S_PERMISSION_DENIED:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002205 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2206 fatal("%s: decode error: %s", __func__, ssh_err(r));
2207 sshbuf_free(m);
Damien Miller445c9a52011-01-14 12:01:29 +11002208 fatal("Master refused stdio forwarding request: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11002209 case MUX_S_FAILURE:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002210 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2211 fatal("%s: decode error: %s", __func__, ssh_err(r));
2212 sshbuf_free(m);
Damien Miller357610d2014-07-18 15:04:10 +10002213 fatal("Stdio forwarding request failed: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11002214 default:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002215 sshbuf_free(m);
Damien Millere1537f92010-01-26 13:26:22 +11002216 error("%s: unexpected response from master 0x%08x",
2217 __func__, type);
2218 return -1;
2219 }
2220 muxclient_request_id++;
2221
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00002222 ssh_signal(SIGHUP, control_client_sighandler);
2223 ssh_signal(SIGINT, control_client_sighandler);
2224 ssh_signal(SIGTERM, control_client_sighandler);
2225 ssh_signal(SIGWINCH, control_client_sigrelay);
Damien Millere1537f92010-01-26 13:26:22 +11002226
2227 /*
2228 * Stick around until the controlee closes the client_fd.
2229 */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002230 sshbuf_reset(m);
2231 if (mux_client_read_packet(fd, m) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11002232 if (errno == EPIPE ||
2233 (errno == EINTR && muxclient_terminate != 0))
2234 return 0;
2235 fatal("%s: mux_client_read_packet: %s",
2236 __func__, strerror(errno));
2237 }
2238 fatal("%s: master returned unexpected message %u", __func__, type);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002239}
2240
Damien Miller6c3eec72011-05-05 14:16:22 +10002241static void
2242mux_client_request_stop_listening(int fd)
2243{
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002244 struct sshbuf *m;
Damien Miller6c3eec72011-05-05 14:16:22 +10002245 char *e;
2246 u_int type, rid;
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002247 int r;
Damien Miller6c3eec72011-05-05 14:16:22 +10002248
2249 debug3("%s: entering", __func__);
2250
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002251 if ((m = sshbuf_new()) == NULL)
2252 fatal("%s: sshbuf_new", __func__);
2253 if ((r = sshbuf_put_u32(m, MUX_C_STOP_LISTENING)) != 0 ||
2254 (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
2255 fatal("%s: request: %s", __func__, ssh_err(r));
Damien Miller6c3eec72011-05-05 14:16:22 +10002256
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002257 if (mux_client_write_packet(fd, m) != 0)
Damien Miller6c3eec72011-05-05 14:16:22 +10002258 fatal("%s: write packet: %s", __func__, strerror(errno));
2259
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002260 sshbuf_reset(m);
Damien Miller6c3eec72011-05-05 14:16:22 +10002261
2262 /* Read their reply */
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002263 if (mux_client_read_packet(fd, m) != 0)
Damien Miller6c3eec72011-05-05 14:16:22 +10002264 fatal("%s: read from master failed: %s",
2265 __func__, strerror(errno));
2266
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002267 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
2268 (r = sshbuf_get_u32(m, &rid)) != 0)
2269 fatal("%s: decode: %s", __func__, ssh_err(r));
2270 if (rid != muxclient_request_id)
Damien Miller6c3eec72011-05-05 14:16:22 +10002271 fatal("%s: out of sequence reply: my id %u theirs %u",
2272 __func__, muxclient_request_id, rid);
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002273
Damien Miller6c3eec72011-05-05 14:16:22 +10002274 switch (type) {
2275 case MUX_S_OK:
2276 break;
2277 case MUX_S_PERMISSION_DENIED:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002278 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2279 fatal("%s: decode error: %s", __func__, ssh_err(r));
Damien Miller6c3eec72011-05-05 14:16:22 +10002280 fatal("Master refused stop listening request: %s", e);
2281 case MUX_S_FAILURE:
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002282 if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2283 fatal("%s: decode error: %s", __func__, ssh_err(r));
Damien Miller6c3eec72011-05-05 14:16:22 +10002284 fatal("%s: stop listening request failed: %s", __func__, e);
2285 default:
2286 fatal("%s: unexpected response from master 0x%08x",
2287 __func__, type);
2288 }
markus@openbsd.orgf4608a72018-07-09 21:18:10 +00002289 sshbuf_free(m);
Damien Miller6c3eec72011-05-05 14:16:22 +10002290 muxclient_request_id++;
2291}
2292
Damien Millerb1cbfa22008-05-19 16:00:08 +10002293/* Multiplex client main loop. */
markus@openbsd.org8d057842016-09-30 09:19:13 +00002294int
Damien Millerb1cbfa22008-05-19 16:00:08 +10002295muxclient(const char *path)
2296{
2297 struct sockaddr_un addr;
Damien Millere1537f92010-01-26 13:26:22 +11002298 int sock;
2299 u_int pid;
Damien Millerb1cbfa22008-05-19 16:00:08 +10002300
Damien Millere1537f92010-01-26 13:26:22 +11002301 if (muxclient_command == 0) {
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00002302 if (options.stdio_forward_host != NULL)
Damien Millere1537f92010-01-26 13:26:22 +11002303 muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
2304 else
2305 muxclient_command = SSHMUX_COMMAND_OPEN;
2306 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002307
2308 switch (options.control_master) {
2309 case SSHCTL_MASTER_AUTO:
2310 case SSHCTL_MASTER_AUTO_ASK:
2311 debug("auto-mux: Trying existing master");
2312 /* FALLTHROUGH */
2313 case SSHCTL_MASTER_NO:
2314 break;
2315 default:
markus@openbsd.org8d057842016-09-30 09:19:13 +00002316 return -1;
Damien Millerb1cbfa22008-05-19 16:00:08 +10002317 }
2318
2319 memset(&addr, '\0', sizeof(addr));
2320 addr.sun_family = AF_UNIX;
Damien Millerb1cbfa22008-05-19 16:00:08 +10002321
2322 if (strlcpy(addr.sun_path, path,
2323 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
dtucker@openbsd.org67dca602016-08-08 22:40:57 +00002324 fatal("ControlPath too long ('%s' >= %u bytes)", path,
2325 (unsigned int)sizeof(addr.sun_path));
Damien Millerb1cbfa22008-05-19 16:00:08 +10002326
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002327 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
Damien Millerb1cbfa22008-05-19 16:00:08 +10002328 fatal("%s socket(): %s", __func__, strerror(errno));
2329
guenther@openbsd.org4ba15462017-01-21 11:32:04 +00002330 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
Damien Millere1537f92010-01-26 13:26:22 +11002331 switch (muxclient_command) {
2332 case SSHMUX_COMMAND_OPEN:
2333 case SSHMUX_COMMAND_STDIO_FWD:
2334 break;
2335 default:
Damien Millerb1cbfa22008-05-19 16:00:08 +10002336 fatal("Control socket connect(%.100s): %s", path,
2337 strerror(errno));
2338 }
Damien Miller603134e2010-09-24 22:07:55 +10002339 if (errno == ECONNREFUSED &&
2340 options.control_master != SSHCTL_MASTER_NO) {
2341 debug("Stale control socket %.100s, unlinking", path);
2342 unlink(path);
2343 } else if (errno == ENOENT) {
Damien Millerb1cbfa22008-05-19 16:00:08 +10002344 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller603134e2010-09-24 22:07:55 +10002345 } else {
Damien Millerb1cbfa22008-05-19 16:00:08 +10002346 error("Control socket connect(%.100s): %s", path,
2347 strerror(errno));
2348 }
2349 close(sock);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002350 return -1;
Damien Millerb1cbfa22008-05-19 16:00:08 +10002351 }
Damien Millere1537f92010-01-26 13:26:22 +11002352 set_nonblock(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002353
Damien Millere1537f92010-01-26 13:26:22 +11002354 if (mux_client_hello_exchange(sock) != 0) {
2355 error("%s: master hello exchange failed", __func__);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10002356 close(sock);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002357 return -1;
Darren Tuckerca19bfe2008-06-13 10:24:03 +10002358 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002359
2360 switch (muxclient_command) {
2361 case SSHMUX_COMMAND_ALIVE_CHECK:
Damien Millere1537f92010-01-26 13:26:22 +11002362 if ((pid = mux_client_request_alive(sock)) == 0)
2363 fatal("%s: master alive check failed", __func__);
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +00002364 fprintf(stderr, "Master running (pid=%u)\r\n", pid);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002365 exit(0);
2366 case SSHMUX_COMMAND_TERMINATE:
Damien Millere1537f92010-01-26 13:26:22 +11002367 mux_client_request_terminate(sock);
dtucker@openbsd.org0b9ee622016-10-19 23:21:56 +00002368 if (options.log_level != SYSLOG_LEVEL_QUIET)
2369 fprintf(stderr, "Exit request sent.\r\n");
Damien Millerb1cbfa22008-05-19 16:00:08 +10002370 exit(0);
Damien Miller388f6fc2010-05-21 14:57:35 +10002371 case SSHMUX_COMMAND_FORWARD:
Damien Millerf6dff7c2011-09-22 21:38:52 +10002372 if (mux_client_forwards(sock, 0) != 0)
Damien Miller388f6fc2010-05-21 14:57:35 +10002373 fatal("%s: master forward request failed", __func__);
2374 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002375 case SSHMUX_COMMAND_OPEN:
Damien Millerf6dff7c2011-09-22 21:38:52 +10002376 if (mux_client_forwards(sock, 0) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11002377 error("%s: master forward request failed", __func__);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002378 return -1;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10002379 }
Damien Millere1537f92010-01-26 13:26:22 +11002380 mux_client_request_session(sock);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002381 return -1;
Damien Millere1537f92010-01-26 13:26:22 +11002382 case SSHMUX_COMMAND_STDIO_FWD:
2383 mux_client_request_stdio_fwd(sock);
2384 exit(0);
Damien Miller6c3eec72011-05-05 14:16:22 +10002385 case SSHMUX_COMMAND_STOP:
2386 mux_client_request_stop_listening(sock);
dtucker@openbsd.org0b9ee622016-10-19 23:21:56 +00002387 if (options.log_level != SYSLOG_LEVEL_QUIET)
2388 fprintf(stderr, "Stop listening request sent.\r\n");
Damien Miller6c3eec72011-05-05 14:16:22 +10002389 exit(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10002390 case SSHMUX_COMMAND_CANCEL_FWD:
2391 if (mux_client_forwards(sock, 1) != 0)
2392 error("%s: master cancel forward request failed",
2393 __func__);
2394 exit(0);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002395 case SSHMUX_COMMAND_PROXY:
2396 mux_client_proxy(sock);
2397 return (sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002398 default:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10002399 fatal("unrecognised muxclient_command %d", muxclient_command);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002400 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002401}