blob: 3dde4da40b89a4eb046ff8448c226f02dcbb6163 [file] [log] [blame]
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001/* $OpenBSD: mux.c,v 1.65 2017/06/09 06:47:13 djm 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
Darren Tucker2fb66ca2008-06-13 04:49:33 +100020/*
21 * TODO:
Damien Millere1537f92010-01-26 13:26:22 +110022 * - Better signalling from master to slave, especially passing of
Darren Tucker2fb66ca2008-06-13 04:49:33 +100023 * error messages
Damien Millere1537f92010-01-26 13:26:22 +110024 * - Better fall-back from mux slave error to new connection.
25 * - ExitOnForwardingFailure
26 * - Maybe extension mechanisms for multi-X11/multi-agent forwarding
27 * - Support ~^Z in mux slaves.
28 * - Inspect or control sessions in master.
29 * - If we ever support the "signal" channel request, send signals on
30 * sessions in master.
Darren Tucker2fb66ca2008-06-13 04:49:33 +100031 */
32
Damien Millere1537f92010-01-26 13:26:22 +110033#include "includes.h"
34
Damien Millerb1cbfa22008-05-19 16:00:08 +100035#include <sys/types.h>
Damien Millerb1cbfa22008-05-19 16:00:08 +100036#include <sys/stat.h>
37#include <sys/socket.h>
38#include <sys/un.h>
39
40#include <errno.h>
41#include <fcntl.h>
42#include <signal.h>
43#include <stdarg.h>
44#include <stddef.h>
45#include <stdlib.h>
46#include <stdio.h>
47#include <string.h>
48#include <unistd.h>
Darren Tuckerce38d822008-06-07 06:25:15 +100049#ifdef HAVE_PATHS_H
Damien Millerb1cbfa22008-05-19 16:00:08 +100050#include <paths.h>
Darren Tuckerce38d822008-06-07 06:25:15 +100051#endif
Damien Millerb1cbfa22008-05-19 16:00:08 +100052
Damien Millere1537f92010-01-26 13:26:22 +110053#ifdef HAVE_POLL_H
54#include <poll.h>
55#else
56# ifdef HAVE_SYS_POLL_H
57# include <sys/poll.h>
58# endif
59#endif
60
Damien Millera7058ec2008-05-20 08:57:06 +100061#ifdef HAVE_UTIL_H
62# include <util.h>
63#endif
64
Damien Millerb1cbfa22008-05-19 16:00:08 +100065#include "openbsd-compat/sys-queue.h"
66#include "xmalloc.h"
67#include "log.h"
68#include "ssh.h"
Damien Miller388f6fc2010-05-21 14:57:35 +100069#include "ssh2.h"
Damien Millerb1cbfa22008-05-19 16:00:08 +100070#include "pathnames.h"
71#include "misc.h"
72#include "match.h"
73#include "buffer.h"
74#include "channels.h"
75#include "msg.h"
76#include "packet.h"
77#include "monitor_fdpass.h"
78#include "sshpty.h"
79#include "key.h"
80#include "readconf.h"
81#include "clientloop.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000082#include "ssherr.h"
Damien Millerb1cbfa22008-05-19 16:00:08 +100083
84/* from ssh.c */
85extern int tty_flag;
86extern Options options;
87extern int stdin_null_flag;
88extern char *host;
Darren Tucker8ec4fd82009-10-07 08:39:57 +110089extern int subsystem_flag;
Damien Millerb1cbfa22008-05-19 16:00:08 +100090extern Buffer command;
Damien Millere1537f92010-01-26 13:26:22 +110091extern volatile sig_atomic_t quit_pending;
Damien Millerb1cbfa22008-05-19 16:00:08 +100092
Darren Tucker2fb66ca2008-06-13 04:49:33 +100093/* Context for session open confirmation callback */
94struct mux_session_confirm_ctx {
Damien Millere1537f92010-01-26 13:26:22 +110095 u_int want_tty;
96 u_int want_subsys;
97 u_int want_x_fwd;
98 u_int want_agent_fwd;
Darren Tucker2fb66ca2008-06-13 04:49:33 +100099 Buffer cmd;
100 char *term;
101 struct termios tio;
102 char **env;
Damien Millerd530f5f2010-05-21 14:57:10 +1000103 u_int rid;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000104};
105
Damien Miller357610d2014-07-18 15:04:10 +1000106/* Context for stdio fwd open confirmation callback */
107struct mux_stdio_confirm_ctx {
108 u_int rid;
109};
110
Damien Miller388f6fc2010-05-21 14:57:35 +1000111/* Context for global channel callback */
112struct mux_channel_confirm_ctx {
113 u_int cid; /* channel id */
114 u_int rid; /* request id */
115 int fid; /* forward id */
116};
117
Damien Millerb1cbfa22008-05-19 16:00:08 +1000118/* fd to control socket */
119int muxserver_sock = -1;
120
Damien Millere1537f92010-01-26 13:26:22 +1100121/* client request id */
122u_int muxclient_request_id = 0;
123
Damien Millerb1cbfa22008-05-19 16:00:08 +1000124/* Multiplexing control command */
125u_int muxclient_command = 0;
126
127/* Set when signalled. */
128static volatile sig_atomic_t muxclient_terminate = 0;
129
130/* PID of multiplex server */
131static u_int muxserver_pid = 0;
132
Damien Millere1537f92010-01-26 13:26:22 +1100133static Channel *mux_listener_channel = NULL;
Damien Millerb1cbfa22008-05-19 16:00:08 +1000134
Damien Millere1537f92010-01-26 13:26:22 +1100135struct mux_master_state {
136 int hello_rcvd;
137};
138
139/* mux protocol messages */
140#define MUX_MSG_HELLO 0x00000001
141#define MUX_C_NEW_SESSION 0x10000002
142#define MUX_C_ALIVE_CHECK 0x10000004
143#define MUX_C_TERMINATE 0x10000005
144#define MUX_C_OPEN_FWD 0x10000006
145#define MUX_C_CLOSE_FWD 0x10000007
146#define MUX_C_NEW_STDIO_FWD 0x10000008
Damien Miller6c3eec72011-05-05 14:16:22 +1000147#define MUX_C_STOP_LISTENING 0x10000009
markus@openbsd.org8d057842016-09-30 09:19:13 +0000148#define MUX_C_PROXY 0x1000000f
Damien Millere1537f92010-01-26 13:26:22 +1100149#define MUX_S_OK 0x80000001
150#define MUX_S_PERMISSION_DENIED 0x80000002
151#define MUX_S_FAILURE 0x80000003
152#define MUX_S_EXIT_MESSAGE 0x80000004
153#define MUX_S_ALIVE 0x80000005
154#define MUX_S_SESSION_OPENED 0x80000006
Damien Miller388f6fc2010-05-21 14:57:35 +1000155#define MUX_S_REMOTE_PORT 0x80000007
Damien Miller555f3b82011-05-15 08:48:05 +1000156#define MUX_S_TTY_ALLOC_FAIL 0x80000008
markus@openbsd.org8d057842016-09-30 09:19:13 +0000157#define MUX_S_PROXY 0x8000000f
Damien Millere1537f92010-01-26 13:26:22 +1100158
159/* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
160#define MUX_FWD_LOCAL 1
161#define MUX_FWD_REMOTE 2
162#define MUX_FWD_DYNAMIC 3
163
Damien Millerd530f5f2010-05-21 14:57:10 +1000164static void mux_session_confirm(int, int, void *);
Damien Miller357610d2014-07-18 15:04:10 +1000165static void mux_stdio_confirm(int, int, void *);
Damien Millere1537f92010-01-26 13:26:22 +1100166
167static int process_mux_master_hello(u_int, Channel *, Buffer *, Buffer *);
168static int process_mux_new_session(u_int, Channel *, Buffer *, Buffer *);
169static int process_mux_alive_check(u_int, Channel *, Buffer *, Buffer *);
170static int process_mux_terminate(u_int, Channel *, Buffer *, Buffer *);
171static int process_mux_open_fwd(u_int, Channel *, Buffer *, Buffer *);
172static int process_mux_close_fwd(u_int, Channel *, Buffer *, Buffer *);
173static int process_mux_stdio_fwd(u_int, Channel *, Buffer *, Buffer *);
Damien Miller6c3eec72011-05-05 14:16:22 +1000174static int process_mux_stop_listening(u_int, Channel *, Buffer *, Buffer *);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000175static int process_mux_proxy(u_int, Channel *, Buffer *, Buffer *);
Damien Millere1537f92010-01-26 13:26:22 +1100176
177static const struct {
178 u_int type;
179 int (*handler)(u_int, Channel *, Buffer *, Buffer *);
180} mux_master_handlers[] = {
181 { MUX_MSG_HELLO, process_mux_master_hello },
182 { MUX_C_NEW_SESSION, process_mux_new_session },
183 { MUX_C_ALIVE_CHECK, process_mux_alive_check },
184 { MUX_C_TERMINATE, process_mux_terminate },
185 { MUX_C_OPEN_FWD, process_mux_open_fwd },
186 { MUX_C_CLOSE_FWD, process_mux_close_fwd },
187 { MUX_C_NEW_STDIO_FWD, process_mux_stdio_fwd },
Damien Miller6c3eec72011-05-05 14:16:22 +1000188 { MUX_C_STOP_LISTENING, process_mux_stop_listening },
markus@openbsd.org8d057842016-09-30 09:19:13 +0000189 { MUX_C_PROXY, process_mux_proxy },
Damien Millere1537f92010-01-26 13:26:22 +1100190 { 0, NULL }
191};
192
193/* Cleanup callback fired on closure of mux slave _session_ channel */
194/* ARGSUSED */
Darren Tuckerea8342c2013-06-06 08:11:40 +1000195static void
Damien Millere1537f92010-01-26 13:26:22 +1100196mux_master_session_cleanup_cb(int cid, void *unused)
197{
198 Channel *cc, *c = channel_by_id(cid);
199
200 debug3("%s: entering for channel %d", __func__, cid);
201 if (c == NULL)
202 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
203 if (c->ctl_chan != -1) {
204 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
205 fatal("%s: channel %d missing control channel %d",
206 __func__, c->self, c->ctl_chan);
207 c->ctl_chan = -1;
208 cc->remote_id = -1;
209 chan_rcvd_oclose(cc);
210 }
211 channel_cancel_cleanup(c->self);
212}
213
214/* Cleanup callback fired on closure of mux slave _control_ channel */
215/* ARGSUSED */
216static void
217mux_master_control_cleanup_cb(int cid, void *unused)
218{
219 Channel *sc, *c = channel_by_id(cid);
220
221 debug3("%s: entering for channel %d", __func__, cid);
222 if (c == NULL)
223 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
224 if (c->remote_id != -1) {
225 if ((sc = channel_by_id(c->remote_id)) == NULL)
Damien Miller601a23c2010-04-16 15:54:01 +1000226 fatal("%s: channel %d missing session channel %d",
Damien Millere1537f92010-01-26 13:26:22 +1100227 __func__, c->self, c->remote_id);
228 c->remote_id = -1;
229 sc->ctl_chan = -1;
Damien Miller172859c2013-04-23 15:19:27 +1000230 if (sc->type != SSH_CHANNEL_OPEN &&
231 sc->type != SSH_CHANNEL_OPENING) {
Damien Millera21cdfa2010-01-28 06:26:59 +1100232 debug2("%s: channel %d: not open", __func__, sc->self);
Damien Miller133d9d32010-01-30 17:30:04 +1100233 chan_mark_dead(sc);
Damien Millera21cdfa2010-01-28 06:26:59 +1100234 } else {
Damien Miller0dac03f2010-01-30 17:36:33 +1100235 if (sc->istate == CHAN_INPUT_OPEN)
236 chan_read_failed(sc);
237 if (sc->ostate == CHAN_OUTPUT_OPEN)
238 chan_write_failed(sc);
Damien Millera21cdfa2010-01-28 06:26:59 +1100239 }
Damien Millere1537f92010-01-26 13:26:22 +1100240 }
241 channel_cancel_cleanup(c->self);
242}
243
244/* Check mux client environment variables before passing them to mux master. */
245static int
246env_permitted(char *env)
247{
248 int i, ret;
249 char name[1024], *cp;
250
251 if ((cp = strchr(env, '=')) == NULL || cp == env)
252 return 0;
253 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
254 if (ret <= 0 || (size_t)ret >= sizeof(name)) {
255 error("env_permitted: name '%.100s...' too long", env);
256 return 0;
257 }
258
259 for (i = 0; i < options.num_send_env; i++)
260 if (match_pattern(name, options.send_env[i]))
261 return 1;
262
263 return 0;
264}
265
266/* Mux master protocol message handlers */
267
268static int
269process_mux_master_hello(u_int rid, Channel *c, Buffer *m, Buffer *r)
270{
271 u_int ver;
272 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
273
274 if (state == NULL)
275 fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self);
276 if (state->hello_rcvd) {
277 error("%s: HELLO received twice", __func__);
278 return -1;
279 }
280 if (buffer_get_int_ret(&ver, m) != 0) {
281 malf:
282 error("%s: malformed message", __func__);
283 return -1;
284 }
285 if (ver != SSHMUX_VER) {
286 error("Unsupported multiplexing protocol version %d "
287 "(expected %d)", ver, SSHMUX_VER);
288 return -1;
289 }
290 debug2("%s: channel %d slave version %u", __func__, c->self, ver);
291
292 /* No extensions are presently defined */
293 while (buffer_len(m) > 0) {
294 char *name = buffer_get_string_ret(m, NULL);
295 char *value = buffer_get_string_ret(m, NULL);
296
297 if (name == NULL || value == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000298 free(name);
Darren Tucker746e9062013-06-06 08:20:13 +1000299 free(value);
Damien Millere1537f92010-01-26 13:26:22 +1100300 goto malf;
301 }
302 debug2("Unrecognised slave extension \"%s\"", name);
Darren Tuckera627d422013-06-02 07:31:17 +1000303 free(name);
304 free(value);
Damien Millere1537f92010-01-26 13:26:22 +1100305 }
306 state->hello_rcvd = 1;
307 return 0;
308}
309
310static int
311process_mux_new_session(u_int rid, Channel *c, Buffer *m, Buffer *r)
312{
313 Channel *nc;
314 struct mux_session_confirm_ctx *cctx;
315 char *reserved, *cmd, *cp;
316 u_int i, j, len, env_len, escape_char, window, packetmax;
317 int new_fd[3];
318
319 /* Reply for SSHMUX_COMMAND_OPEN */
320 cctx = xcalloc(1, sizeof(*cctx));
321 cctx->term = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000322 cctx->rid = rid;
Damien Millere1537f92010-01-26 13:26:22 +1100323 cmd = reserved = NULL;
Damien Millerab523b02012-07-06 13:44:43 +1000324 cctx->env = NULL;
325 env_len = 0;
Damien Millere1537f92010-01-26 13:26:22 +1100326 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
327 buffer_get_int_ret(&cctx->want_tty, m) != 0 ||
328 buffer_get_int_ret(&cctx->want_x_fwd, m) != 0 ||
329 buffer_get_int_ret(&cctx->want_agent_fwd, m) != 0 ||
330 buffer_get_int_ret(&cctx->want_subsys, m) != 0 ||
331 buffer_get_int_ret(&escape_char, m) != 0 ||
332 (cctx->term = buffer_get_string_ret(m, &len)) == NULL ||
333 (cmd = buffer_get_string_ret(m, &len)) == NULL) {
334 malf:
Darren Tuckera627d422013-06-02 07:31:17 +1000335 free(cmd);
336 free(reserved);
Damien Millerab523b02012-07-06 13:44:43 +1000337 for (j = 0; j < env_len; j++)
Darren Tuckera627d422013-06-02 07:31:17 +1000338 free(cctx->env[j]);
339 free(cctx->env);
340 free(cctx->term);
341 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100342 error("%s: malformed message", __func__);
343 return -1;
344 }
Darren Tuckera627d422013-06-02 07:31:17 +1000345 free(reserved);
Damien Millere1537f92010-01-26 13:26:22 +1100346 reserved = NULL;
347
Damien Millere1537f92010-01-26 13:26:22 +1100348 while (buffer_len(m) > 0) {
349#define MUX_MAX_ENV_VARS 4096
Damien Miller2ec03422012-02-11 08:16:28 +1100350 if ((cp = buffer_get_string_ret(m, &len)) == NULL)
Damien Millere1537f92010-01-26 13:26:22 +1100351 goto malf;
Damien Millere1537f92010-01-26 13:26:22 +1100352 if (!env_permitted(cp)) {
Darren Tuckera627d422013-06-02 07:31:17 +1000353 free(cp);
Damien Millere1537f92010-01-26 13:26:22 +1100354 continue;
355 }
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000356 cctx->env = xreallocarray(cctx->env, env_len + 2,
Damien Millere1537f92010-01-26 13:26:22 +1100357 sizeof(*cctx->env));
358 cctx->env[env_len++] = cp;
359 cctx->env[env_len] = NULL;
360 if (env_len > MUX_MAX_ENV_VARS) {
361 error(">%d environment variables received, ignoring "
362 "additional", MUX_MAX_ENV_VARS);
363 break;
364 }
365 }
366
367 debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
368 "term \"%s\", cmd \"%s\", env %u", __func__, c->self,
369 cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
370 cctx->want_subsys, cctx->term, cmd, env_len);
371
372 buffer_init(&cctx->cmd);
373 buffer_append(&cctx->cmd, cmd, strlen(cmd));
Darren Tuckera627d422013-06-02 07:31:17 +1000374 free(cmd);
Damien Millere1537f92010-01-26 13:26:22 +1100375 cmd = NULL;
376
377 /* Gather fds from client */
378 for(i = 0; i < 3; i++) {
379 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
380 error("%s: failed to receive fd %d from slave",
381 __func__, i);
382 for (j = 0; j < i; j++)
383 close(new_fd[j]);
384 for (j = 0; j < env_len; j++)
Darren Tuckera627d422013-06-02 07:31:17 +1000385 free(cctx->env[j]);
386 free(cctx->env);
387 free(cctx->term);
Damien Millere1537f92010-01-26 13:26:22 +1100388 buffer_free(&cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +1000389 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100390
391 /* prepare reply */
392 buffer_put_int(r, MUX_S_FAILURE);
393 buffer_put_int(r, rid);
394 buffer_put_cstring(r,
395 "did not receive file descriptors");
396 return -1;
397 }
398 }
399
400 debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
401 new_fd[0], new_fd[1], new_fd[2]);
402
403 /* XXX support multiple child sessions in future */
404 if (c->remote_id != -1) {
405 debug2("%s: session already open", __func__);
406 /* prepare reply */
407 buffer_put_int(r, MUX_S_FAILURE);
408 buffer_put_int(r, rid);
409 buffer_put_cstring(r, "Multiple sessions not supported");
410 cleanup:
411 close(new_fd[0]);
412 close(new_fd[1]);
413 close(new_fd[2]);
Darren Tuckera627d422013-06-02 07:31:17 +1000414 free(cctx->term);
Damien Millere1537f92010-01-26 13:26:22 +1100415 if (env_len != 0) {
416 for (i = 0; i < env_len; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000417 free(cctx->env[i]);
418 free(cctx->env);
Damien Millere1537f92010-01-26 13:26:22 +1100419 }
420 buffer_free(&cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +1000421 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100422 return 0;
423 }
424
425 if (options.control_master == SSHCTL_MASTER_ASK ||
426 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
427 if (!ask_permission("Allow shared connection to %s? ", host)) {
428 debug2("%s: session refused by user", __func__);
429 /* prepare reply */
430 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
431 buffer_put_int(r, rid);
432 buffer_put_cstring(r, "Permission denied");
433 goto cleanup;
434 }
435 }
436
437 /* Try to pick up ttymodes from client before it goes raw */
438 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
439 error("%s: tcgetattr: %s", __func__, strerror(errno));
440
441 /* enable nonblocking unless tty */
442 if (!isatty(new_fd[0]))
443 set_nonblock(new_fd[0]);
444 if (!isatty(new_fd[1]))
445 set_nonblock(new_fd[1]);
446 if (!isatty(new_fd[2]))
447 set_nonblock(new_fd[2]);
448
449 window = CHAN_SES_WINDOW_DEFAULT;
450 packetmax = CHAN_SES_PACKET_DEFAULT;
451 if (cctx->want_tty) {
452 window >>= 1;
453 packetmax >>= 1;
454 }
455
456 nc = channel_new("session", SSH_CHANNEL_OPENING,
457 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
458 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
459
460 nc->ctl_chan = c->self; /* link session -> control channel */
461 c->remote_id = nc->self; /* link control -> session channel */
462
463 if (cctx->want_tty && escape_char != 0xffffffff) {
464 channel_register_filter(nc->self,
465 client_simple_escape_filter, NULL,
466 client_filter_cleanup,
467 client_new_escape_filter_ctx((int)escape_char));
468 }
469
470 debug2("%s: channel_new: %d linked to control channel %d",
471 __func__, nc->self, nc->ctl_chan);
472
473 channel_send_open(nc->self);
474 channel_register_open_confirm(nc->self, mux_session_confirm, cctx);
Damien Millerd530f5f2010-05-21 14:57:10 +1000475 c->mux_pause = 1; /* stop handling messages until open_confirm done */
Damien Miller85c50d72010-05-10 11:53:02 +1000476 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +1100477
Damien Millerd530f5f2010-05-21 14:57:10 +1000478 /* reply is deferred, sent by mux_session_confirm */
Damien Millere1537f92010-01-26 13:26:22 +1100479 return 0;
480}
481
482static int
483process_mux_alive_check(u_int rid, Channel *c, Buffer *m, Buffer *r)
484{
485 debug2("%s: channel %d: alive check", __func__, c->self);
486
487 /* prepare reply */
488 buffer_put_int(r, MUX_S_ALIVE);
489 buffer_put_int(r, rid);
490 buffer_put_int(r, (u_int)getpid());
491
492 return 0;
493}
494
495static int
496process_mux_terminate(u_int rid, Channel *c, Buffer *m, Buffer *r)
497{
498 debug2("%s: channel %d: terminate request", __func__, c->self);
499
500 if (options.control_master == SSHCTL_MASTER_ASK ||
501 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
502 if (!ask_permission("Terminate shared connection to %s? ",
503 host)) {
504 debug2("%s: termination refused by user", __func__);
505 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
506 buffer_put_int(r, rid);
507 buffer_put_cstring(r, "Permission denied");
508 return 0;
509 }
510 }
511
512 quit_pending = 1;
513 buffer_put_int(r, MUX_S_OK);
514 buffer_put_int(r, rid);
515 /* XXX exit happens too soon - message never makes it to client */
516 return 0;
517}
518
519static char *
Damien Miller7acefbb2014-07-18 14:11:24 +1000520format_forward(u_int ftype, struct Forward *fwd)
Damien Millere1537f92010-01-26 13:26:22 +1100521{
522 char *ret;
523
524 switch (ftype) {
525 case MUX_FWD_LOCAL:
526 xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +1000527 (fwd->listen_path != NULL) ? fwd->listen_path :
Damien Millere1537f92010-01-26 13:26:22 +1100528 (fwd->listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +1000529 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millere1537f92010-01-26 13:26:22 +1100530 fwd->listen_host, fwd->listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +1000531 (fwd->connect_path != NULL) ? fwd->connect_path :
Damien Millere1537f92010-01-26 13:26:22 +1100532 fwd->connect_host, fwd->connect_port);
533 break;
534 case MUX_FWD_DYNAMIC:
535 xasprintf(&ret, "dynamic forward %.200s:%d -> *",
536 (fwd->listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +1000537 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millere1537f92010-01-26 13:26:22 +1100538 fwd->listen_host, fwd->listen_port);
539 break;
540 case MUX_FWD_REMOTE:
541 xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +1000542 (fwd->listen_path != NULL) ? fwd->listen_path :
Damien Millere1537f92010-01-26 13:26:22 +1100543 (fwd->listen_host == NULL) ?
544 "LOCALHOST" : fwd->listen_host,
545 fwd->listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +1000546 (fwd->connect_path != NULL) ? fwd->connect_path :
Damien Millere1537f92010-01-26 13:26:22 +1100547 fwd->connect_host, fwd->connect_port);
548 break;
549 default:
550 fatal("%s: unknown forward type %u", __func__, ftype);
551 }
552 return ret;
553}
554
555static int
556compare_host(const char *a, const char *b)
557{
558 if (a == NULL && b == NULL)
559 return 1;
560 if (a == NULL || b == NULL)
561 return 0;
562 return strcmp(a, b) == 0;
563}
564
565static int
Damien Miller7acefbb2014-07-18 14:11:24 +1000566compare_forward(struct Forward *a, struct Forward *b)
Damien Millere1537f92010-01-26 13:26:22 +1100567{
568 if (!compare_host(a->listen_host, b->listen_host))
569 return 0;
Damien Miller7acefbb2014-07-18 14:11:24 +1000570 if (!compare_host(a->listen_path, b->listen_path))
571 return 0;
Damien Millere1537f92010-01-26 13:26:22 +1100572 if (a->listen_port != b->listen_port)
573 return 0;
574 if (!compare_host(a->connect_host, b->connect_host))
575 return 0;
Damien Miller7acefbb2014-07-18 14:11:24 +1000576 if (!compare_host(a->connect_path, b->connect_path))
577 return 0;
Damien Millere1537f92010-01-26 13:26:22 +1100578 if (a->connect_port != b->connect_port)
579 return 0;
580
581 return 1;
582}
583
Damien Miller388f6fc2010-05-21 14:57:35 +1000584static void
585mux_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
586{
587 struct mux_channel_confirm_ctx *fctx = ctxt;
588 char *failmsg = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000589 struct Forward *rfwd;
Damien Miller388f6fc2010-05-21 14:57:35 +1000590 Channel *c;
591 Buffer out;
592
593 if ((c = channel_by_id(fctx->cid)) == NULL) {
594 /* no channel for reply */
595 error("%s: unknown channel", __func__);
596 return;
597 }
598 buffer_init(&out);
djm@openbsd.orgca430d42015-05-01 04:03:20 +0000599 if (fctx->fid >= options.num_remote_forwards ||
600 (options.remote_forwards[fctx->fid].connect_path == NULL &&
601 options.remote_forwards[fctx->fid].connect_host == NULL)) {
Damien Miller388f6fc2010-05-21 14:57:35 +1000602 xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
603 goto fail;
604 }
605 rfwd = &options.remote_forwards[fctx->fid];
606 debug("%s: %s for: listen %d, connect %s:%d", __func__,
607 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Miller7acefbb2014-07-18 14:11:24 +1000608 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
609 rfwd->connect_host, rfwd->connect_port);
Damien Miller388f6fc2010-05-21 14:57:35 +1000610 if (type == SSH2_MSG_REQUEST_SUCCESS) {
611 if (rfwd->listen_port == 0) {
612 rfwd->allocated_port = packet_get_int();
djm@openbsd.org8312cfb2015-05-01 04:01:58 +0000613 debug("Allocated port %u for mux remote forward"
Damien Miller388f6fc2010-05-21 14:57:35 +1000614 " to %s:%d", rfwd->allocated_port,
615 rfwd->connect_host, rfwd->connect_port);
616 buffer_put_int(&out, MUX_S_REMOTE_PORT);
617 buffer_put_int(&out, fctx->rid);
618 buffer_put_int(&out, rfwd->allocated_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +1100619 channel_update_permitted_opens(rfwd->handle,
620 rfwd->allocated_port);
Damien Miller388f6fc2010-05-21 14:57:35 +1000621 } else {
622 buffer_put_int(&out, MUX_S_OK);
623 buffer_put_int(&out, fctx->rid);
624 }
625 goto out;
626 } else {
Darren Tucker68afb8c2011-10-02 18:59:03 +1100627 if (rfwd->listen_port == 0)
628 channel_update_permitted_opens(rfwd->handle, -1);
Damien Miller7acefbb2014-07-18 14:11:24 +1000629 if (rfwd->listen_path != NULL)
630 xasprintf(&failmsg, "remote port forwarding failed for "
631 "listen path %s", rfwd->listen_path);
632 else
633 xasprintf(&failmsg, "remote port forwarding failed for "
634 "listen port %d", rfwd->listen_port);
djm@openbsd.orgca430d42015-05-01 04:03:20 +0000635
636 debug2("%s: clearing registered forwarding for listen %d, "
637 "connect %s:%d", __func__, rfwd->listen_port,
638 rfwd->connect_path ? rfwd->connect_path :
639 rfwd->connect_host, rfwd->connect_port);
640
641 free(rfwd->listen_host);
642 free(rfwd->listen_path);
643 free(rfwd->connect_host);
644 free(rfwd->connect_path);
645 memset(rfwd, 0, sizeof(*rfwd));
Damien Miller388f6fc2010-05-21 14:57:35 +1000646 }
647 fail:
648 error("%s: %s", __func__, failmsg);
649 buffer_put_int(&out, MUX_S_FAILURE);
650 buffer_put_int(&out, fctx->rid);
651 buffer_put_cstring(&out, failmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000652 free(failmsg);
Damien Miller388f6fc2010-05-21 14:57:35 +1000653 out:
654 buffer_put_string(&c->output, buffer_ptr(&out), buffer_len(&out));
655 buffer_free(&out);
656 if (c->mux_pause <= 0)
657 fatal("%s: mux_pause %d", __func__, c->mux_pause);
658 c->mux_pause = 0; /* start processing messages again */
659}
660
Damien Millere1537f92010-01-26 13:26:22 +1100661static int
662process_mux_open_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
663{
Damien Miller7acefbb2014-07-18 14:11:24 +1000664 struct Forward fwd;
Damien Millere1537f92010-01-26 13:26:22 +1100665 char *fwd_desc = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000666 char *listen_addr, *connect_addr;
Damien Millere1537f92010-01-26 13:26:22 +1100667 u_int ftype;
Damien Millerce986542013-07-18 16:12:44 +1000668 u_int lport, cport;
Damien Millere1537f92010-01-26 13:26:22 +1100669 int i, ret = 0, freefwd = 1;
670
djm@openbsd.org45b0eb72015-08-19 23:18:26 +0000671 memset(&fwd, 0, sizeof(fwd));
672
Damien Miller7acefbb2014-07-18 14:11:24 +1000673 /* XXX - lport/cport check redundant */
Damien Millere1537f92010-01-26 13:26:22 +1100674 if (buffer_get_int_ret(&ftype, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000675 (listen_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000676 buffer_get_int_ret(&lport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000677 (connect_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000678 buffer_get_int_ret(&cport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000679 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
680 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
Damien Millere1537f92010-01-26 13:26:22 +1100681 error("%s: malformed message", __func__);
682 ret = -1;
683 goto out;
684 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000685 if (*listen_addr == '\0') {
686 free(listen_addr);
687 listen_addr = NULL;
688 }
689 if (*connect_addr == '\0') {
690 free(connect_addr);
691 connect_addr = NULL;
692 }
693
694 memset(&fwd, 0, sizeof(fwd));
Damien Millerce986542013-07-18 16:12:44 +1000695 fwd.listen_port = lport;
Damien Miller7acefbb2014-07-18 14:11:24 +1000696 if (fwd.listen_port == PORT_STREAMLOCAL)
697 fwd.listen_path = listen_addr;
698 else
699 fwd.listen_host = listen_addr;
Damien Millerce986542013-07-18 16:12:44 +1000700 fwd.connect_port = cport;
Damien Miller7acefbb2014-07-18 14:11:24 +1000701 if (fwd.connect_port == PORT_STREAMLOCAL)
702 fwd.connect_path = connect_addr;
703 else
704 fwd.connect_host = connect_addr;
Damien Millere1537f92010-01-26 13:26:22 +1100705
706 debug2("%s: channel %d: request %s", __func__, c->self,
707 (fwd_desc = format_forward(ftype, &fwd)));
708
709 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
710 ftype != MUX_FWD_DYNAMIC) {
711 logit("%s: invalid forwarding type %u", __func__, ftype);
712 invalid:
Damien Miller7acefbb2014-07-18 14:11:24 +1000713 free(listen_addr);
714 free(connect_addr);
Damien Millere1537f92010-01-26 13:26:22 +1100715 buffer_put_int(r, MUX_S_FAILURE);
716 buffer_put_int(r, rid);
717 buffer_put_cstring(r, "Invalid forwarding request");
718 return 0;
719 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000720 if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) {
721 logit("%s: streamlocal and dynamic forwards "
722 "are mutually exclusive", __func__);
723 goto invalid;
724 }
725 if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) {
Damien Millere1537f92010-01-26 13:26:22 +1100726 logit("%s: invalid listen port %u", __func__,
727 fwd.listen_port);
728 goto invalid;
729 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000730 if ((fwd.connect_port != PORT_STREAMLOCAL && fwd.connect_port >= 65536)
731 || (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE && fwd.connect_port == 0)) {
Damien Millere1537f92010-01-26 13:26:22 +1100732 logit("%s: invalid connect port %u", __func__,
733 fwd.connect_port);
734 goto invalid;
735 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000736 if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL && fwd.connect_path == NULL) {
Damien Millere1537f92010-01-26 13:26:22 +1100737 logit("%s: missing connect host", __func__);
738 goto invalid;
739 }
740
741 /* Skip forwards that have already been requested */
742 switch (ftype) {
743 case MUX_FWD_LOCAL:
744 case MUX_FWD_DYNAMIC:
745 for (i = 0; i < options.num_local_forwards; i++) {
746 if (compare_forward(&fwd,
747 options.local_forwards + i)) {
748 exists:
749 debug2("%s: found existing forwarding",
750 __func__);
751 buffer_put_int(r, MUX_S_OK);
752 buffer_put_int(r, rid);
753 goto out;
754 }
755 }
756 break;
757 case MUX_FWD_REMOTE:
758 for (i = 0; i < options.num_remote_forwards; i++) {
759 if (compare_forward(&fwd,
Damien Miller388f6fc2010-05-21 14:57:35 +1000760 options.remote_forwards + i)) {
761 if (fwd.listen_port != 0)
762 goto exists;
763 debug2("%s: found allocated port",
764 __func__);
765 buffer_put_int(r, MUX_S_REMOTE_PORT);
766 buffer_put_int(r, rid);
767 buffer_put_int(r,
768 options.remote_forwards[i].allocated_port);
769 goto out;
770 }
Damien Millere1537f92010-01-26 13:26:22 +1100771 }
772 break;
773 }
774
775 if (options.control_master == SSHCTL_MASTER_ASK ||
776 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
777 if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
778 debug2("%s: forwarding refused by user", __func__);
779 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
780 buffer_put_int(r, rid);
781 buffer_put_cstring(r, "Permission denied");
782 goto out;
783 }
784 }
785
786 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000787 if (!channel_setup_local_fwd_listener(&fwd,
788 &options.fwd_opts)) {
Damien Millere1537f92010-01-26 13:26:22 +1100789 fail:
790 logit("slave-requested %s failed", fwd_desc);
791 buffer_put_int(r, MUX_S_FAILURE);
792 buffer_put_int(r, rid);
793 buffer_put_cstring(r, "Port forwarding failed");
794 goto out;
795 }
796 add_local_forward(&options, &fwd);
797 freefwd = 0;
798 } else {
Damien Miller388f6fc2010-05-21 14:57:35 +1000799 struct mux_channel_confirm_ctx *fctx;
800
Damien Miller7acefbb2014-07-18 14:11:24 +1000801 fwd.handle = channel_request_remote_forwarding(&fwd);
Darren Tucker68afb8c2011-10-02 18:59:03 +1100802 if (fwd.handle < 0)
Damien Millere1537f92010-01-26 13:26:22 +1100803 goto fail;
804 add_remote_forward(&options, &fwd);
Damien Miller388f6fc2010-05-21 14:57:35 +1000805 fctx = xcalloc(1, sizeof(*fctx));
806 fctx->cid = c->self;
807 fctx->rid = rid;
Damien Miller232cfb12010-06-26 09:50:30 +1000808 fctx->fid = options.num_remote_forwards - 1;
Damien Miller388f6fc2010-05-21 14:57:35 +1000809 client_register_global_confirm(mux_confirm_remote_forward,
810 fctx);
Damien Millere1537f92010-01-26 13:26:22 +1100811 freefwd = 0;
Damien Miller388f6fc2010-05-21 14:57:35 +1000812 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
813 /* delayed reply in mux_confirm_remote_forward */
814 goto out;
Damien Millere1537f92010-01-26 13:26:22 +1100815 }
816 buffer_put_int(r, MUX_S_OK);
817 buffer_put_int(r, rid);
818 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000819 free(fwd_desc);
Damien Millere1537f92010-01-26 13:26:22 +1100820 if (freefwd) {
Darren Tuckera627d422013-06-02 07:31:17 +1000821 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000822 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000823 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000824 free(fwd.connect_path);
Damien Millere1537f92010-01-26 13:26:22 +1100825 }
826 return ret;
827}
828
829static int
830process_mux_close_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
831{
Damien Miller7acefbb2014-07-18 14:11:24 +1000832 struct Forward fwd, *found_fwd;
Damien Millere1537f92010-01-26 13:26:22 +1100833 char *fwd_desc = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000834 const char *error_reason = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000835 char *listen_addr = NULL, *connect_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100836 u_int ftype;
Damien Miller7acefbb2014-07-18 14:11:24 +1000837 int i, ret = 0;
Damien Millerce986542013-07-18 16:12:44 +1000838 u_int lport, cport;
Damien Millere1537f92010-01-26 13:26:22 +1100839
djm@openbsd.org45b0eb72015-08-19 23:18:26 +0000840 memset(&fwd, 0, sizeof(fwd));
841
Damien Millere1537f92010-01-26 13:26:22 +1100842 if (buffer_get_int_ret(&ftype, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000843 (listen_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000844 buffer_get_int_ret(&lport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000845 (connect_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000846 buffer_get_int_ret(&cport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000847 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
848 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
Damien Millere1537f92010-01-26 13:26:22 +1100849 error("%s: malformed message", __func__);
850 ret = -1;
851 goto out;
852 }
853
Damien Miller7acefbb2014-07-18 14:11:24 +1000854 if (*listen_addr == '\0') {
855 free(listen_addr);
856 listen_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100857 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000858 if (*connect_addr == '\0') {
859 free(connect_addr);
860 connect_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100861 }
862
Damien Miller7acefbb2014-07-18 14:11:24 +1000863 memset(&fwd, 0, sizeof(fwd));
864 fwd.listen_port = lport;
865 if (fwd.listen_port == PORT_STREAMLOCAL)
866 fwd.listen_path = listen_addr;
867 else
868 fwd.listen_host = listen_addr;
869 fwd.connect_port = cport;
870 if (fwd.connect_port == PORT_STREAMLOCAL)
871 fwd.connect_path = connect_addr;
872 else
873 fwd.connect_host = connect_addr;
874
Damien Millerf6dff7c2011-09-22 21:38:52 +1000875 debug2("%s: channel %d: request cancel %s", __func__, c->self,
Damien Millere1537f92010-01-26 13:26:22 +1100876 (fwd_desc = format_forward(ftype, &fwd)));
877
Damien Millerf6dff7c2011-09-22 21:38:52 +1000878 /* make sure this has been requested */
879 found_fwd = NULL;
880 switch (ftype) {
881 case MUX_FWD_LOCAL:
882 case MUX_FWD_DYNAMIC:
883 for (i = 0; i < options.num_local_forwards; i++) {
884 if (compare_forward(&fwd,
885 options.local_forwards + i)) {
886 found_fwd = options.local_forwards + i;
887 break;
888 }
889 }
890 break;
891 case MUX_FWD_REMOTE:
892 for (i = 0; i < options.num_remote_forwards; i++) {
893 if (compare_forward(&fwd,
894 options.remote_forwards + i)) {
895 found_fwd = options.remote_forwards + i;
896 break;
897 }
898 }
899 break;
900 }
Damien Millere1537f92010-01-26 13:26:22 +1100901
Damien Millerf6dff7c2011-09-22 21:38:52 +1000902 if (found_fwd == NULL)
903 error_reason = "port not forwarded";
904 else if (ftype == MUX_FWD_REMOTE) {
905 /*
906 * This shouldn't fail unless we confused the host/port
907 * between options.remote_forwards and permitted_opens.
Darren Tucker68afb8c2011-10-02 18:59:03 +1100908 * However, for dynamic allocated listen ports we need
Damien Miller7acefbb2014-07-18 14:11:24 +1000909 * to use the actual listen port.
Damien Millerf6dff7c2011-09-22 21:38:52 +1000910 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000911 if (channel_request_rforward_cancel(found_fwd) == -1)
Damien Millerf6dff7c2011-09-22 21:38:52 +1000912 error_reason = "port not in permitted opens";
913 } else { /* local and dynamic forwards */
914 /* Ditto */
Damien Miller7acefbb2014-07-18 14:11:24 +1000915 if (channel_cancel_lport_listener(&fwd, fwd.connect_port,
916 &options.fwd_opts) == -1)
Damien Millerf6dff7c2011-09-22 21:38:52 +1000917 error_reason = "port not found";
918 }
919
920 if (error_reason == NULL) {
921 buffer_put_int(r, MUX_S_OK);
922 buffer_put_int(r, rid);
923
Darren Tuckera627d422013-06-02 07:31:17 +1000924 free(found_fwd->listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000925 free(found_fwd->listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000926 free(found_fwd->connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000927 free(found_fwd->connect_path);
Damien Millerf6dff7c2011-09-22 21:38:52 +1000928 found_fwd->listen_host = found_fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000929 found_fwd->listen_path = found_fwd->connect_path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000930 found_fwd->listen_port = found_fwd->connect_port = 0;
931 } else {
932 buffer_put_int(r, MUX_S_FAILURE);
933 buffer_put_int(r, rid);
934 buffer_put_cstring(r, error_reason);
935 }
Damien Millere1537f92010-01-26 13:26:22 +1100936 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000937 free(fwd_desc);
Damien Miller7acefbb2014-07-18 14:11:24 +1000938 free(listen_addr);
939 free(connect_addr);
Damien Millere1537f92010-01-26 13:26:22 +1100940
941 return ret;
942}
943
944static int
945process_mux_stdio_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
946{
947 Channel *nc;
948 char *reserved, *chost;
949 u_int cport, i, j;
950 int new_fd[2];
Damien Miller357610d2014-07-18 15:04:10 +1000951 struct mux_stdio_confirm_ctx *cctx;
Damien Millere1537f92010-01-26 13:26:22 +1100952
953 chost = reserved = NULL;
954 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
955 (chost = buffer_get_string_ret(m, NULL)) == NULL ||
956 buffer_get_int_ret(&cport, m) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000957 free(reserved);
958 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +1100959 error("%s: malformed message", __func__);
960 return -1;
961 }
Darren Tuckera627d422013-06-02 07:31:17 +1000962 free(reserved);
Damien Millere1537f92010-01-26 13:26:22 +1100963
964 debug2("%s: channel %d: request stdio fwd to %s:%u",
965 __func__, c->self, chost, cport);
966
967 /* Gather fds from client */
968 for(i = 0; i < 2; i++) {
969 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
970 error("%s: failed to receive fd %d from slave",
971 __func__, i);
972 for (j = 0; j < i; j++)
973 close(new_fd[j]);
Darren Tuckera627d422013-06-02 07:31:17 +1000974 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +1100975
976 /* prepare reply */
977 buffer_put_int(r, MUX_S_FAILURE);
978 buffer_put_int(r, rid);
979 buffer_put_cstring(r,
980 "did not receive file descriptors");
981 return -1;
982 }
983 }
984
985 debug3("%s: got fds stdin %d, stdout %d", __func__,
986 new_fd[0], new_fd[1]);
987
988 /* XXX support multiple child sessions in future */
989 if (c->remote_id != -1) {
990 debug2("%s: session already open", __func__);
991 /* prepare reply */
992 buffer_put_int(r, MUX_S_FAILURE);
993 buffer_put_int(r, rid);
994 buffer_put_cstring(r, "Multiple sessions not supported");
995 cleanup:
996 close(new_fd[0]);
997 close(new_fd[1]);
Darren Tuckera627d422013-06-02 07:31:17 +1000998 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +1100999 return 0;
1000 }
1001
1002 if (options.control_master == SSHCTL_MASTER_ASK ||
1003 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
Damien Miller68512c02010-10-21 15:21:11 +11001004 if (!ask_permission("Allow forward to %s:%u? ",
Damien Millere1537f92010-01-26 13:26:22 +11001005 chost, cport)) {
1006 debug2("%s: stdio fwd refused by user", __func__);
1007 /* prepare reply */
1008 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
1009 buffer_put_int(r, rid);
1010 buffer_put_cstring(r, "Permission denied");
1011 goto cleanup;
1012 }
1013 }
1014
1015 /* enable nonblocking unless tty */
1016 if (!isatty(new_fd[0]))
1017 set_nonblock(new_fd[0]);
1018 if (!isatty(new_fd[1]))
1019 set_nonblock(new_fd[1]);
1020
1021 nc = channel_connect_stdio_fwd(chost, cport, new_fd[0], new_fd[1]);
1022
1023 nc->ctl_chan = c->self; /* link session -> control channel */
1024 c->remote_id = nc->self; /* link control -> session channel */
1025
1026 debug2("%s: channel_new: %d linked to control channel %d",
1027 __func__, nc->self, nc->ctl_chan);
1028
Damien Miller85c50d72010-05-10 11:53:02 +10001029 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +11001030
Damien Miller357610d2014-07-18 15:04:10 +10001031 cctx = xcalloc(1, sizeof(*cctx));
1032 cctx->rid = rid;
1033 channel_register_open_confirm(nc->self, mux_stdio_confirm, cctx);
1034 c->mux_pause = 1; /* stop handling messages until open_confirm done */
Damien Millere1537f92010-01-26 13:26:22 +11001035
Damien Miller357610d2014-07-18 15:04:10 +10001036 /* reply is deferred, sent by mux_session_confirm */
Damien Millere1537f92010-01-26 13:26:22 +11001037 return 0;
1038}
1039
Damien Miller357610d2014-07-18 15:04:10 +10001040/* Callback on open confirmation in mux master for a mux stdio fwd session. */
1041static void
1042mux_stdio_confirm(int id, int success, void *arg)
1043{
1044 struct mux_stdio_confirm_ctx *cctx = arg;
1045 Channel *c, *cc;
1046 Buffer reply;
1047
1048 if (cctx == NULL)
1049 fatal("%s: cctx == NULL", __func__);
1050 if ((c = channel_by_id(id)) == NULL)
1051 fatal("%s: no channel for id %d", __func__, id);
1052 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
1053 fatal("%s: channel %d lacks control channel %d", __func__,
1054 id, c->ctl_chan);
1055
1056 if (!success) {
1057 debug3("%s: sending failure reply", __func__);
1058 /* prepare reply */
1059 buffer_init(&reply);
1060 buffer_put_int(&reply, MUX_S_FAILURE);
1061 buffer_put_int(&reply, cctx->rid);
1062 buffer_put_cstring(&reply, "Session open refused by peer");
1063 goto done;
1064 }
1065
1066 debug3("%s: sending success reply", __func__);
1067 /* prepare reply */
1068 buffer_init(&reply);
1069 buffer_put_int(&reply, MUX_S_SESSION_OPENED);
1070 buffer_put_int(&reply, cctx->rid);
1071 buffer_put_int(&reply, c->self);
1072
1073 done:
1074 /* Send reply */
1075 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply));
1076 buffer_free(&reply);
1077
1078 if (cc->mux_pause <= 0)
1079 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1080 cc->mux_pause = 0; /* start processing messages again */
1081 c->open_confirm_ctx = NULL;
1082 free(cctx);
1083}
1084
Damien Miller6c3eec72011-05-05 14:16:22 +10001085static int
1086process_mux_stop_listening(u_int rid, Channel *c, Buffer *m, Buffer *r)
1087{
1088 debug("%s: channel %d: stop listening", __func__, c->self);
1089
1090 if (options.control_master == SSHCTL_MASTER_ASK ||
1091 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
1092 if (!ask_permission("Disable further multiplexing on shared "
1093 "connection to %s? ", host)) {
1094 debug2("%s: stop listen refused by user", __func__);
1095 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
1096 buffer_put_int(r, rid);
1097 buffer_put_cstring(r, "Permission denied");
1098 return 0;
1099 }
1100 }
1101
1102 if (mux_listener_channel != NULL) {
1103 channel_free(mux_listener_channel);
1104 client_stop_mux();
Darren Tuckera627d422013-06-02 07:31:17 +10001105 free(options.control_path);
Damien Miller6c3eec72011-05-05 14:16:22 +10001106 options.control_path = NULL;
1107 mux_listener_channel = NULL;
1108 muxserver_sock = -1;
1109 }
1110
1111 /* prepare reply */
1112 buffer_put_int(r, MUX_S_OK);
1113 buffer_put_int(r, rid);
1114
1115 return 0;
1116}
1117
markus@openbsd.org8d057842016-09-30 09:19:13 +00001118static int
1119process_mux_proxy(u_int rid, Channel *c, Buffer *m, Buffer *r)
1120{
1121 debug("%s: channel %d: proxy request", __func__, c->self);
1122
1123 c->mux_rcb = channel_proxy_downstream;
1124 buffer_put_int(r, MUX_S_PROXY);
1125 buffer_put_int(r, rid);
1126
1127 return 0;
1128}
1129
Damien Millere1537f92010-01-26 13:26:22 +11001130/* Channel callbacks fired on read/write from mux slave fd */
1131static int
1132mux_master_read_cb(Channel *c)
1133{
1134 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
1135 Buffer in, out;
Damien Miller633de332014-05-15 13:48:26 +10001136 const u_char *ptr;
Damien Millere1537f92010-01-26 13:26:22 +11001137 u_int type, rid, have, i;
1138 int ret = -1;
1139
1140 /* Setup ctx and */
1141 if (c->mux_ctx == NULL) {
Damien Millerc094d1e2010-06-26 09:36:34 +10001142 state = xcalloc(1, sizeof(*state));
Damien Millere1537f92010-01-26 13:26:22 +11001143 c->mux_ctx = state;
1144 channel_register_cleanup(c->self,
1145 mux_master_control_cleanup_cb, 0);
1146
1147 /* Send hello */
1148 buffer_init(&out);
1149 buffer_put_int(&out, MUX_MSG_HELLO);
1150 buffer_put_int(&out, SSHMUX_VER);
1151 /* no extensions */
1152 buffer_put_string(&c->output, buffer_ptr(&out),
1153 buffer_len(&out));
1154 buffer_free(&out);
1155 debug3("%s: channel %d: hello sent", __func__, c->self);
1156 return 0;
1157 }
1158
1159 buffer_init(&in);
1160 buffer_init(&out);
1161
1162 /* Channel code ensures that we receive whole packets */
1163 if ((ptr = buffer_get_string_ptr_ret(&c->input, &have)) == NULL) {
1164 malf:
1165 error("%s: malformed message", __func__);
1166 goto out;
1167 }
1168 buffer_append(&in, ptr, have);
1169
1170 if (buffer_get_int_ret(&type, &in) != 0)
1171 goto malf;
1172 debug3("%s: channel %d packet type 0x%08x len %u",
1173 __func__, c->self, type, buffer_len(&in));
1174
1175 if (type == MUX_MSG_HELLO)
1176 rid = 0;
1177 else {
1178 if (!state->hello_rcvd) {
1179 error("%s: expected MUX_MSG_HELLO(0x%08x), "
1180 "received 0x%08x", __func__, MUX_MSG_HELLO, type);
1181 goto out;
1182 }
1183 if (buffer_get_int_ret(&rid, &in) != 0)
1184 goto malf;
1185 }
1186
1187 for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
1188 if (type == mux_master_handlers[i].type) {
1189 ret = mux_master_handlers[i].handler(rid, c, &in, &out);
1190 break;
1191 }
1192 }
1193 if (mux_master_handlers[i].handler == NULL) {
1194 error("%s: unsupported mux message 0x%08x", __func__, type);
1195 buffer_put_int(&out, MUX_S_FAILURE);
1196 buffer_put_int(&out, rid);
1197 buffer_put_cstring(&out, "unsupported request");
1198 ret = 0;
1199 }
1200 /* Enqueue reply packet */
1201 if (buffer_len(&out) != 0) {
1202 buffer_put_string(&c->output, buffer_ptr(&out),
1203 buffer_len(&out));
1204 }
1205 out:
1206 buffer_free(&in);
1207 buffer_free(&out);
1208 return ret;
1209}
1210
1211void
1212mux_exit_message(Channel *c, int exitval)
1213{
1214 Buffer m;
1215 Channel *mux_chan;
1216
Damien Millerbc02f162013-04-23 19:25:49 +10001217 debug3("%s: channel %d: exit message, exitval %d", __func__, c->self,
Damien Millere1537f92010-01-26 13:26:22 +11001218 exitval);
1219
1220 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL)
1221 fatal("%s: channel %d missing mux channel %d",
1222 __func__, c->self, c->ctl_chan);
1223
1224 /* Append exit message packet to control socket output queue */
1225 buffer_init(&m);
1226 buffer_put_int(&m, MUX_S_EXIT_MESSAGE);
1227 buffer_put_int(&m, c->self);
1228 buffer_put_int(&m, exitval);
1229
1230 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m));
1231 buffer_free(&m);
1232}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001233
Damien Miller555f3b82011-05-15 08:48:05 +10001234void
1235mux_tty_alloc_failed(Channel *c)
1236{
1237 Buffer m;
1238 Channel *mux_chan;
1239
1240 debug3("%s: channel %d: TTY alloc failed", __func__, c->self);
1241
1242 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL)
1243 fatal("%s: channel %d missing mux channel %d",
1244 __func__, c->self, c->ctl_chan);
1245
1246 /* Append exit message packet to control socket output queue */
1247 buffer_init(&m);
1248 buffer_put_int(&m, MUX_S_TTY_ALLOC_FAIL);
1249 buffer_put_int(&m, c->self);
1250
1251 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m));
1252 buffer_free(&m);
1253}
1254
Damien Millerb1cbfa22008-05-19 16:00:08 +10001255/* Prepare a mux master to listen on a Unix domain socket. */
1256void
1257muxserver_listen(void)
1258{
Damien Millerb1cbfa22008-05-19 16:00:08 +10001259 mode_t old_umask;
Damien Miller603134e2010-09-24 22:07:55 +10001260 char *orig_control_path = options.control_path;
1261 char rbuf[16+1];
1262 u_int i, r;
Damien Millerf42f7682014-07-18 15:03:27 +10001263 int oerrno;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001264
1265 if (options.control_path == NULL ||
1266 options.control_master == SSHCTL_MASTER_NO)
1267 return;
1268
1269 debug("setting up multiplex master socket");
1270
Damien Miller603134e2010-09-24 22:07:55 +10001271 /*
1272 * Use a temporary path before listen so we can pseudo-atomically
1273 * establish the listening socket in its final location to avoid
1274 * other processes racing in between bind() and listen() and hitting
1275 * an unready socket.
1276 */
1277 for (i = 0; i < sizeof(rbuf) - 1; i++) {
1278 r = arc4random_uniform(26+26+10);
1279 rbuf[i] = (r < 26) ? 'a' + r :
1280 (r < 26*2) ? 'A' + r - 26 :
1281 '0' + r - 26 - 26;
1282 }
1283 rbuf[sizeof(rbuf) - 1] = '\0';
1284 options.control_path = NULL;
1285 xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
1286 debug3("%s: temporary control path %s", __func__, options.control_path);
1287
Damien Millerb1cbfa22008-05-19 16:00:08 +10001288 old_umask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001289 muxserver_sock = unix_listener(options.control_path, 64, 0);
Damien Millerf42f7682014-07-18 15:03:27 +10001290 oerrno = errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10001291 umask(old_umask);
1292 if (muxserver_sock < 0) {
Damien Millerf42f7682014-07-18 15:03:27 +10001293 if (oerrno == EINVAL || oerrno == EADDRINUSE) {
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001294 error("ControlSocket %s already exists, "
1295 "disabling multiplexing", options.control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001296 disable_mux_master:
Damien Miller60432d82011-05-15 08:34:46 +10001297 if (muxserver_sock != -1) {
1298 close(muxserver_sock);
1299 muxserver_sock = -1;
1300 }
Darren Tuckera627d422013-06-02 07:31:17 +10001301 free(orig_control_path);
1302 free(options.control_path);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001303 options.control_path = NULL;
1304 options.control_master = SSHCTL_MASTER_NO;
1305 return;
Damien Miller7acefbb2014-07-18 14:11:24 +10001306 } else {
1307 /* unix_listener() logs the error */
1308 cleanup_exit(255);
1309 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001310 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001311
Damien Miller603134e2010-09-24 22:07:55 +10001312 /* Now atomically "move" the mux socket into position */
1313 if (link(options.control_path, orig_control_path) != 0) {
1314 if (errno != EEXIST) {
djm@openbsd.org95687f52016-04-01 02:34:10 +00001315 fatal("%s: link mux listener %s => %s: %s", __func__,
Damien Miller603134e2010-09-24 22:07:55 +10001316 options.control_path, orig_control_path,
1317 strerror(errno));
1318 }
1319 error("ControlSocket %s already exists, disabling multiplexing",
1320 orig_control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001321 unlink(options.control_path);
1322 goto disable_mux_master;
1323 }
1324 unlink(options.control_path);
Darren Tuckera627d422013-06-02 07:31:17 +10001325 free(options.control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001326 options.control_path = orig_control_path;
1327
Damien Millerb1cbfa22008-05-19 16:00:08 +10001328 set_nonblock(muxserver_sock);
Damien Millere1537f92010-01-26 13:26:22 +11001329
1330 mux_listener_channel = channel_new("mux listener",
1331 SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
1332 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller603134e2010-09-24 22:07:55 +10001333 0, options.control_path, 1);
Damien Millere1537f92010-01-26 13:26:22 +11001334 mux_listener_channel->mux_rcb = mux_master_read_cb;
1335 debug3("%s: mux listener channel %d fd %d", __func__,
1336 mux_listener_channel->self, mux_listener_channel->sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001337}
1338
1339/* Callback on open confirmation in mux master for a mux client session. */
1340static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001341mux_session_confirm(int id, int success, void *arg)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001342{
1343 struct mux_session_confirm_ctx *cctx = arg;
1344 const char *display;
Damien Millerd530f5f2010-05-21 14:57:10 +10001345 Channel *c, *cc;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001346 int i;
Damien Millerd530f5f2010-05-21 14:57:10 +10001347 Buffer reply;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001348
1349 if (cctx == NULL)
1350 fatal("%s: cctx == NULL", __func__);
Damien Millere1537f92010-01-26 13:26:22 +11001351 if ((c = channel_by_id(id)) == NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001352 fatal("%s: no channel for id %d", __func__, id);
Damien Millerd530f5f2010-05-21 14:57:10 +10001353 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
1354 fatal("%s: channel %d lacks control channel %d", __func__,
1355 id, c->ctl_chan);
1356
1357 if (!success) {
1358 debug3("%s: sending failure reply", __func__);
1359 /* prepare reply */
1360 buffer_init(&reply);
1361 buffer_put_int(&reply, MUX_S_FAILURE);
1362 buffer_put_int(&reply, cctx->rid);
1363 buffer_put_cstring(&reply, "Session open refused by peer");
1364 goto done;
1365 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001366
1367 display = getenv("DISPLAY");
1368 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1369 char *proto, *data;
Damien Miller1ab6a512010-06-26 10:02:24 +10001370
Damien Millerb1cbfa22008-05-19 16:00:08 +10001371 /* Get reasonable local authentication information. */
djm@openbsd.orged4ce822016-01-13 23:04:47 +00001372 if (client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001373 options.forward_x11_trusted, options.forward_x11_timeout,
djm@openbsd.orged4ce822016-01-13 23:04:47 +00001374 &proto, &data) == 0) {
1375 /* Request forwarding with authentication spoofing. */
1376 debug("Requesting X11 forwarding with authentication "
1377 "spoofing.");
1378 x11_request_forwarding_with_spoofing(id, display, proto,
1379 data, 1);
1380 /* XXX exit_on_forward_failure */
1381 client_expect_confirm(id, "X11 forwarding",
1382 CONFIRM_WARN);
1383 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001384 }
1385
1386 if (cctx->want_agent_fwd && options.forward_agent) {
1387 debug("Requesting authentication agent forwarding.");
1388 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1389 packet_send();
1390 }
1391
1392 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
1393 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
1394
Damien Millerd530f5f2010-05-21 14:57:10 +10001395 debug3("%s: sending success reply", __func__);
1396 /* prepare reply */
1397 buffer_init(&reply);
1398 buffer_put_int(&reply, MUX_S_SESSION_OPENED);
1399 buffer_put_int(&reply, cctx->rid);
1400 buffer_put_int(&reply, c->self);
1401
1402 done:
1403 /* Send reply */
1404 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply));
1405 buffer_free(&reply);
1406
1407 if (cc->mux_pause <= 0)
1408 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1409 cc->mux_pause = 0; /* start processing messages again */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001410 c->open_confirm_ctx = NULL;
1411 buffer_free(&cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +10001412 free(cctx->term);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001413 if (cctx->env != NULL) {
1414 for (i = 0; cctx->env[i] != NULL; i++)
Darren Tuckera627d422013-06-02 07:31:17 +10001415 free(cctx->env[i]);
1416 free(cctx->env);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001417 }
Darren Tuckera627d422013-06-02 07:31:17 +10001418 free(cctx);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001419}
1420
Damien Millerb1cbfa22008-05-19 16:00:08 +10001421/* ** Multiplexing client support */
1422
1423/* Exit signal handler */
1424static void
1425control_client_sighandler(int signo)
1426{
1427 muxclient_terminate = signo;
1428}
1429
1430/*
1431 * Relay signal handler - used to pass some signals from mux client to
1432 * mux master.
1433 */
1434static void
1435control_client_sigrelay(int signo)
1436{
1437 int save_errno = errno;
1438
1439 if (muxserver_pid > 1)
1440 kill(muxserver_pid, signo);
1441
1442 errno = save_errno;
1443}
1444
Damien Millerb1cbfa22008-05-19 16:00:08 +10001445static int
Damien Millere1537f92010-01-26 13:26:22 +11001446mux_client_read(int fd, Buffer *b, u_int need)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001447{
Damien Millere1537f92010-01-26 13:26:22 +11001448 u_int have;
1449 ssize_t len;
1450 u_char *p;
1451 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001452
Damien Millere1537f92010-01-26 13:26:22 +11001453 pfd.fd = fd;
1454 pfd.events = POLLIN;
1455 p = buffer_append_space(b, need);
1456 for (have = 0; have < need; ) {
1457 if (muxclient_terminate) {
1458 errno = EINTR;
1459 return -1;
1460 }
1461 len = read(fd, p + have, need - have);
1462 if (len < 0) {
1463 switch (errno) {
1464#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1465 case EWOULDBLOCK:
1466#endif
1467 case EAGAIN:
1468 (void)poll(&pfd, 1, -1);
1469 /* FALLTHROUGH */
1470 case EINTR:
1471 continue;
1472 default:
1473 return -1;
1474 }
1475 }
1476 if (len == 0) {
1477 errno = EPIPE;
1478 return -1;
1479 }
1480 have += (u_int)len;
1481 }
1482 return 0;
1483}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001484
Damien Millere1537f92010-01-26 13:26:22 +11001485static int
1486mux_client_write_packet(int fd, Buffer *m)
1487{
1488 Buffer queue;
1489 u_int have, need;
1490 int oerrno, len;
1491 u_char *ptr;
1492 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001493
Damien Millere1537f92010-01-26 13:26:22 +11001494 pfd.fd = fd;
1495 pfd.events = POLLOUT;
1496 buffer_init(&queue);
1497 buffer_put_string(&queue, buffer_ptr(m), buffer_len(m));
1498
1499 need = buffer_len(&queue);
1500 ptr = buffer_ptr(&queue);
1501
1502 for (have = 0; have < need; ) {
1503 if (muxclient_terminate) {
1504 buffer_free(&queue);
1505 errno = EINTR;
1506 return -1;
1507 }
1508 len = write(fd, ptr + have, need - have);
1509 if (len < 0) {
1510 switch (errno) {
1511#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1512 case EWOULDBLOCK:
1513#endif
1514 case EAGAIN:
1515 (void)poll(&pfd, 1, -1);
1516 /* FALLTHROUGH */
1517 case EINTR:
1518 continue;
1519 default:
1520 oerrno = errno;
1521 buffer_free(&queue);
1522 errno = oerrno;
1523 return -1;
1524 }
1525 }
1526 if (len == 0) {
1527 buffer_free(&queue);
1528 errno = EPIPE;
1529 return -1;
1530 }
1531 have += (u_int)len;
1532 }
1533 buffer_free(&queue);
1534 return 0;
1535}
1536
1537static int
1538mux_client_read_packet(int fd, Buffer *m)
1539{
1540 Buffer queue;
1541 u_int need, have;
Damien Miller633de332014-05-15 13:48:26 +10001542 const u_char *ptr;
Damien Millere1537f92010-01-26 13:26:22 +11001543 int oerrno;
1544
1545 buffer_init(&queue);
1546 if (mux_client_read(fd, &queue, 4) != 0) {
1547 if ((oerrno = errno) == EPIPE)
Darren Tucker746e9062013-06-06 08:20:13 +10001548 debug3("%s: read header failed: %s", __func__,
1549 strerror(errno));
1550 buffer_free(&queue);
Damien Millere1537f92010-01-26 13:26:22 +11001551 errno = oerrno;
1552 return -1;
1553 }
1554 need = get_u32(buffer_ptr(&queue));
1555 if (mux_client_read(fd, &queue, need) != 0) {
1556 oerrno = errno;
1557 debug3("%s: read body failed: %s", __func__, strerror(errno));
Darren Tucker746e9062013-06-06 08:20:13 +10001558 buffer_free(&queue);
Damien Millere1537f92010-01-26 13:26:22 +11001559 errno = oerrno;
1560 return -1;
1561 }
1562 ptr = buffer_get_string_ptr(&queue, &have);
1563 buffer_append(m, ptr, have);
1564 buffer_free(&queue);
1565 return 0;
1566}
1567
1568static int
1569mux_client_hello_exchange(int fd)
1570{
1571 Buffer m;
1572 u_int type, ver;
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001573 int ret = -1;
Damien Millere1537f92010-01-26 13:26:22 +11001574
1575 buffer_init(&m);
1576 buffer_put_int(&m, MUX_MSG_HELLO);
1577 buffer_put_int(&m, SSHMUX_VER);
1578 /* no extensions */
1579
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001580 if (mux_client_write_packet(fd, &m) != 0) {
1581 debug("%s: write packet: %s", __func__, strerror(errno));
1582 goto out;
1583 }
Damien Millere1537f92010-01-26 13:26:22 +11001584
1585 buffer_clear(&m);
1586
1587 /* Read their HELLO */
1588 if (mux_client_read_packet(fd, &m) != 0) {
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001589 debug("%s: read packet failed", __func__);
1590 goto out;
Damien Millere1537f92010-01-26 13:26:22 +11001591 }
1592
1593 type = buffer_get_int(&m);
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001594 if (type != MUX_MSG_HELLO) {
1595 error("%s: expected HELLO (%u) received %u",
Damien Millere1537f92010-01-26 13:26:22 +11001596 __func__, MUX_MSG_HELLO, type);
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001597 goto out;
1598 }
Damien Millere1537f92010-01-26 13:26:22 +11001599 ver = buffer_get_int(&m);
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001600 if (ver != SSHMUX_VER) {
1601 error("Unsupported multiplexing protocol version %d "
Damien Millere1537f92010-01-26 13:26:22 +11001602 "(expected %d)", ver, SSHMUX_VER);
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001603 goto out;
1604 }
Damien Millere1537f92010-01-26 13:26:22 +11001605 debug2("%s: master version %u", __func__, ver);
1606 /* No extensions are presently defined */
1607 while (buffer_len(&m) > 0) {
1608 char *name = buffer_get_string(&m, NULL);
1609 char *value = buffer_get_string(&m, NULL);
1610
1611 debug2("Unrecognised master extension \"%s\"", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001612 free(name);
1613 free(value);
Damien Millere1537f92010-01-26 13:26:22 +11001614 }
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001615 /* success */
1616 ret = 0;
1617 out:
Damien Millere1537f92010-01-26 13:26:22 +11001618 buffer_free(&m);
djm@openbsd.org5b2f34a2017-06-09 06:47:13 +00001619 return ret;
Damien Millere1537f92010-01-26 13:26:22 +11001620}
1621
1622static u_int
1623mux_client_request_alive(int fd)
1624{
1625 Buffer m;
1626 char *e;
1627 u_int pid, type, rid;
1628
1629 debug3("%s: entering", __func__);
1630
1631 buffer_init(&m);
1632 buffer_put_int(&m, MUX_C_ALIVE_CHECK);
1633 buffer_put_int(&m, muxclient_request_id);
1634
1635 if (mux_client_write_packet(fd, &m) != 0)
1636 fatal("%s: write packet: %s", __func__, strerror(errno));
1637
1638 buffer_clear(&m);
1639
1640 /* Read their reply */
1641 if (mux_client_read_packet(fd, &m) != 0) {
1642 buffer_free(&m);
1643 return 0;
1644 }
1645
1646 type = buffer_get_int(&m);
1647 if (type != MUX_S_ALIVE) {
1648 e = buffer_get_string(&m, NULL);
1649 fatal("%s: master returned error: %s", __func__, e);
1650 }
1651
1652 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1653 fatal("%s: out of sequence reply: my id %u theirs %u",
1654 __func__, muxclient_request_id, rid);
1655 pid = buffer_get_int(&m);
1656 buffer_free(&m);
1657
1658 debug3("%s: done pid = %u", __func__, pid);
1659
1660 muxclient_request_id++;
1661
1662 return pid;
1663}
1664
1665static void
1666mux_client_request_terminate(int fd)
1667{
1668 Buffer m;
1669 char *e;
1670 u_int type, rid;
1671
1672 debug3("%s: entering", __func__);
1673
1674 buffer_init(&m);
1675 buffer_put_int(&m, MUX_C_TERMINATE);
1676 buffer_put_int(&m, muxclient_request_id);
1677
1678 if (mux_client_write_packet(fd, &m) != 0)
1679 fatal("%s: write packet: %s", __func__, strerror(errno));
1680
1681 buffer_clear(&m);
1682
1683 /* Read their reply */
1684 if (mux_client_read_packet(fd, &m) != 0) {
1685 /* Remote end exited already */
1686 if (errno == EPIPE) {
1687 buffer_free(&m);
1688 return;
1689 }
1690 fatal("%s: read from master failed: %s",
1691 __func__, strerror(errno));
1692 }
1693
1694 type = buffer_get_int(&m);
1695 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1696 fatal("%s: out of sequence reply: my id %u theirs %u",
1697 __func__, muxclient_request_id, rid);
1698 switch (type) {
1699 case MUX_S_OK:
1700 break;
1701 case MUX_S_PERMISSION_DENIED:
1702 e = buffer_get_string(&m, NULL);
1703 fatal("Master refused termination request: %s", e);
1704 case MUX_S_FAILURE:
1705 e = buffer_get_string(&m, NULL);
1706 fatal("%s: termination request failed: %s", __func__, e);
1707 default:
1708 fatal("%s: unexpected response from master 0x%08x",
1709 __func__, type);
1710 }
1711 buffer_free(&m);
1712 muxclient_request_id++;
1713}
1714
1715static int
Damien Miller7acefbb2014-07-18 14:11:24 +10001716mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd)
Damien Millere1537f92010-01-26 13:26:22 +11001717{
1718 Buffer m;
1719 char *e, *fwd_desc;
1720 u_int type, rid;
1721
1722 fwd_desc = format_forward(ftype, fwd);
Damien Millerf6dff7c2011-09-22 21:38:52 +10001723 debug("Requesting %s %s",
1724 cancel_flag ? "cancellation of" : "forwarding of", fwd_desc);
Darren Tuckera627d422013-06-02 07:31:17 +10001725 free(fwd_desc);
Damien Millere1537f92010-01-26 13:26:22 +11001726
1727 buffer_init(&m);
Damien Millerf6dff7c2011-09-22 21:38:52 +10001728 buffer_put_int(&m, cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD);
Damien Millere1537f92010-01-26 13:26:22 +11001729 buffer_put_int(&m, muxclient_request_id);
1730 buffer_put_int(&m, ftype);
Damien Miller7acefbb2014-07-18 14:11:24 +10001731 if (fwd->listen_path != NULL) {
1732 buffer_put_cstring(&m, fwd->listen_path);
1733 } else {
1734 buffer_put_cstring(&m,
djm@openbsd.org46ac2ed2014-12-22 07:24:11 +00001735 fwd->listen_host == NULL ? "" :
1736 (*fwd->listen_host == '\0' ? "*" : fwd->listen_host));
Damien Miller7acefbb2014-07-18 14:11:24 +10001737 }
Damien Millere1537f92010-01-26 13:26:22 +11001738 buffer_put_int(&m, fwd->listen_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10001739 if (fwd->connect_path != NULL) {
1740 buffer_put_cstring(&m, fwd->connect_path);
1741 } else {
1742 buffer_put_cstring(&m,
1743 fwd->connect_host == NULL ? "" : fwd->connect_host);
1744 }
Damien Millere1537f92010-01-26 13:26:22 +11001745 buffer_put_int(&m, fwd->connect_port);
1746
1747 if (mux_client_write_packet(fd, &m) != 0)
1748 fatal("%s: write packet: %s", __func__, strerror(errno));
1749
1750 buffer_clear(&m);
1751
1752 /* Read their reply */
1753 if (mux_client_read_packet(fd, &m) != 0) {
1754 buffer_free(&m);
1755 return -1;
1756 }
1757
1758 type = buffer_get_int(&m);
1759 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1760 fatal("%s: out of sequence reply: my id %u theirs %u",
1761 __func__, muxclient_request_id, rid);
1762 switch (type) {
1763 case MUX_S_OK:
1764 break;
Damien Miller388f6fc2010-05-21 14:57:35 +10001765 case MUX_S_REMOTE_PORT:
Damien Millerf6dff7c2011-09-22 21:38:52 +10001766 if (cancel_flag)
1767 fatal("%s: got MUX_S_REMOTE_PORT for cancel", __func__);
Damien Miller388f6fc2010-05-21 14:57:35 +10001768 fwd->allocated_port = buffer_get_int(&m);
djm@openbsd.org8312cfb2015-05-01 04:01:58 +00001769 verbose("Allocated port %u for remote forward to %s:%d",
Damien Miller388f6fc2010-05-21 14:57:35 +10001770 fwd->allocated_port,
1771 fwd->connect_host ? fwd->connect_host : "",
1772 fwd->connect_port);
1773 if (muxclient_command == SSHMUX_COMMAND_FORWARD)
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +00001774 fprintf(stdout, "%i\n", fwd->allocated_port);
Damien Miller388f6fc2010-05-21 14:57:35 +10001775 break;
Damien Millere1537f92010-01-26 13:26:22 +11001776 case MUX_S_PERMISSION_DENIED:
1777 e = buffer_get_string(&m, NULL);
1778 buffer_free(&m);
1779 error("Master refused forwarding request: %s", e);
1780 return -1;
1781 case MUX_S_FAILURE:
1782 e = buffer_get_string(&m, NULL);
1783 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001784 error("%s: forwarding request failed: %s", __func__, e);
Damien Millere1537f92010-01-26 13:26:22 +11001785 return -1;
1786 default:
1787 fatal("%s: unexpected response from master 0x%08x",
1788 __func__, type);
1789 }
1790 buffer_free(&m);
1791
1792 muxclient_request_id++;
1793 return 0;
1794}
1795
1796static int
Damien Millerf6dff7c2011-09-22 21:38:52 +10001797mux_client_forwards(int fd, int cancel_flag)
Damien Millere1537f92010-01-26 13:26:22 +11001798{
Damien Millerf6dff7c2011-09-22 21:38:52 +10001799 int i, ret = 0;
Damien Millere1537f92010-01-26 13:26:22 +11001800
Damien Millerf6dff7c2011-09-22 21:38:52 +10001801 debug3("%s: %s forwardings: %d local, %d remote", __func__,
1802 cancel_flag ? "cancel" : "request",
Damien Millere1537f92010-01-26 13:26:22 +11001803 options.num_local_forwards, options.num_remote_forwards);
1804
1805 /* XXX ExitOnForwardingFailure */
1806 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10001807 if (mux_client_forward(fd, cancel_flag,
Damien Millere1537f92010-01-26 13:26:22 +11001808 options.local_forwards[i].connect_port == 0 ?
1809 MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
1810 options.local_forwards + i) != 0)
Damien Millerf6dff7c2011-09-22 21:38:52 +10001811 ret = -1;
Damien Millere1537f92010-01-26 13:26:22 +11001812 }
1813 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10001814 if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE,
Damien Millere1537f92010-01-26 13:26:22 +11001815 options.remote_forwards + i) != 0)
Damien Millerf6dff7c2011-09-22 21:38:52 +10001816 ret = -1;
Damien Millere1537f92010-01-26 13:26:22 +11001817 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10001818 return ret;
Damien Millere1537f92010-01-26 13:26:22 +11001819}
1820
1821static int
1822mux_client_request_session(int fd)
1823{
1824 Buffer m;
1825 char *e, *term;
1826 u_int i, rid, sid, esid, exitval, type, exitval_seen;
1827 extern char **environ;
Damien Miller555f3b82011-05-15 08:48:05 +10001828 int devnull, rawmode;
Damien Millere1537f92010-01-26 13:26:22 +11001829
1830 debug3("%s: entering", __func__);
1831
1832 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1833 error("%s: master alive request failed", __func__);
1834 return -1;
1835 }
1836
1837 signal(SIGPIPE, SIG_IGN);
1838
1839 if (stdin_null_flag) {
1840 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1841 fatal("open(/dev/null): %s", strerror(errno));
1842 if (dup2(devnull, STDIN_FILENO) == -1)
1843 fatal("dup2: %s", strerror(errno));
1844 if (devnull > STDERR_FILENO)
1845 close(devnull);
1846 }
1847
1848 term = getenv("TERM");
1849
1850 buffer_init(&m);
1851 buffer_put_int(&m, MUX_C_NEW_SESSION);
1852 buffer_put_int(&m, muxclient_request_id);
1853 buffer_put_cstring(&m, ""); /* reserved */
1854 buffer_put_int(&m, tty_flag);
1855 buffer_put_int(&m, options.forward_x11);
1856 buffer_put_int(&m, options.forward_agent);
1857 buffer_put_int(&m, subsystem_flag);
1858 buffer_put_int(&m, options.escape_char == SSH_ESCAPECHAR_NONE ?
1859 0xffffffff : (u_int)options.escape_char);
1860 buffer_put_cstring(&m, term == NULL ? "" : term);
1861 buffer_put_string(&m, buffer_ptr(&command), buffer_len(&command));
1862
1863 if (options.num_send_env > 0 && environ != NULL) {
1864 /* Pass environment */
1865 for (i = 0; environ[i] != NULL; i++) {
1866 if (env_permitted(environ[i])) {
1867 buffer_put_cstring(&m, environ[i]);
1868 }
1869 }
1870 }
1871
1872 if (mux_client_write_packet(fd, &m) != 0)
1873 fatal("%s: write packet: %s", __func__, strerror(errno));
1874
1875 /* Send the stdio file descriptors */
1876 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1877 mm_send_fd(fd, STDOUT_FILENO) == -1 ||
1878 mm_send_fd(fd, STDERR_FILENO) == -1)
1879 fatal("%s: send fds failed", __func__);
1880
1881 debug3("%s: session request sent", __func__);
1882
1883 /* Read their reply */
1884 buffer_clear(&m);
1885 if (mux_client_read_packet(fd, &m) != 0) {
1886 error("%s: read from master failed: %s",
1887 __func__, strerror(errno));
1888 buffer_free(&m);
1889 return -1;
1890 }
1891
1892 type = buffer_get_int(&m);
1893 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1894 fatal("%s: out of sequence reply: my id %u theirs %u",
1895 __func__, muxclient_request_id, rid);
1896 switch (type) {
1897 case MUX_S_SESSION_OPENED:
1898 sid = buffer_get_int(&m);
1899 debug("%s: master session id: %u", __func__, sid);
1900 break;
1901 case MUX_S_PERMISSION_DENIED:
1902 e = buffer_get_string(&m, NULL);
1903 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001904 error("Master refused session request: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11001905 return -1;
1906 case MUX_S_FAILURE:
1907 e = buffer_get_string(&m, NULL);
1908 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001909 error("%s: session request failed: %s", __func__, e);
Damien Millere1537f92010-01-26 13:26:22 +11001910 return -1;
1911 default:
1912 buffer_free(&m);
1913 error("%s: unexpected response from master 0x%08x",
1914 __func__, type);
1915 return -1;
1916 }
1917 muxclient_request_id++;
1918
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001919 if (pledge("stdio proc tty", NULL) == -1)
1920 fatal("%s pledge(): %s", __func__, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11001921 platform_pledge_mux();
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001922
Damien Millere1537f92010-01-26 13:26:22 +11001923 signal(SIGHUP, control_client_sighandler);
1924 signal(SIGINT, control_client_sighandler);
1925 signal(SIGTERM, control_client_sighandler);
1926 signal(SIGWINCH, control_client_sigrelay);
1927
Damien Miller555f3b82011-05-15 08:48:05 +10001928 rawmode = tty_flag;
Damien Millere1537f92010-01-26 13:26:22 +11001929 if (tty_flag)
Damien Miller21771e22011-05-15 08:45:50 +10001930 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millere1537f92010-01-26 13:26:22 +11001931
1932 /*
1933 * Stick around until the controlee closes the client_fd.
1934 * Before it does, it is expected to write an exit message.
1935 * This process must read the value and wait for the closure of
1936 * the client_fd; if this one closes early, the multiplex master will
1937 * terminate early too (possibly losing data).
1938 */
1939 for (exitval = 255, exitval_seen = 0;;) {
1940 buffer_clear(&m);
1941 if (mux_client_read_packet(fd, &m) != 0)
1942 break;
1943 type = buffer_get_int(&m);
Damien Miller555f3b82011-05-15 08:48:05 +10001944 switch (type) {
1945 case MUX_S_TTY_ALLOC_FAIL:
1946 if ((esid = buffer_get_int(&m)) != sid)
1947 fatal("%s: tty alloc fail on unknown session: "
1948 "my id %u theirs %u",
1949 __func__, sid, esid);
1950 leave_raw_mode(options.request_tty ==
1951 REQUEST_TTY_FORCE);
1952 rawmode = 0;
1953 continue;
1954 case MUX_S_EXIT_MESSAGE:
1955 if ((esid = buffer_get_int(&m)) != sid)
1956 fatal("%s: exit on unknown session: "
1957 "my id %u theirs %u",
1958 __func__, sid, esid);
1959 if (exitval_seen)
1960 fatal("%s: exitval sent twice", __func__);
1961 exitval = buffer_get_int(&m);
1962 exitval_seen = 1;
1963 continue;
1964 default:
Damien Millere1537f92010-01-26 13:26:22 +11001965 e = buffer_get_string(&m, NULL);
1966 fatal("%s: master returned error: %s", __func__, e);
1967 }
Damien Millere1537f92010-01-26 13:26:22 +11001968 }
1969
1970 close(fd);
Damien Miller555f3b82011-05-15 08:48:05 +10001971 if (rawmode)
1972 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millere1537f92010-01-26 13:26:22 +11001973
1974 if (muxclient_terminate) {
1975 debug2("Exiting on signal %d", muxclient_terminate);
1976 exitval = 255;
1977 } else if (!exitval_seen) {
1978 debug2("Control master terminated unexpectedly");
1979 exitval = 255;
1980 } else
1981 debug2("Received exit status from master %d", exitval);
1982
1983 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1984 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
1985
1986 exit(exitval);
1987}
1988
1989static int
markus@openbsd.org8d057842016-09-30 09:19:13 +00001990mux_client_proxy(int fd)
1991{
1992 Buffer m;
1993 char *e;
1994 u_int type, rid;
1995
1996 buffer_init(&m);
1997 buffer_put_int(&m, MUX_C_PROXY);
1998 buffer_put_int(&m, muxclient_request_id);
1999 if (mux_client_write_packet(fd, &m) != 0)
2000 fatal("%s: write packet: %s", __func__, strerror(errno));
2001
2002 buffer_clear(&m);
2003
2004 /* Read their reply */
2005 if (mux_client_read_packet(fd, &m) != 0) {
2006 buffer_free(&m);
2007 return 0;
2008 }
2009 type = buffer_get_int(&m);
2010 if (type != MUX_S_PROXY) {
2011 e = buffer_get_string(&m, NULL);
2012 fatal("%s: master returned error: %s", __func__, e);
2013 }
2014 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
2015 fatal("%s: out of sequence reply: my id %u theirs %u",
2016 __func__, muxclient_request_id, rid);
2017 buffer_free(&m);
2018
2019 debug3("%s: done", __func__);
2020 muxclient_request_id++;
2021 return 0;
2022}
2023
2024static int
Damien Millere1537f92010-01-26 13:26:22 +11002025mux_client_request_stdio_fwd(int fd)
2026{
2027 Buffer m;
2028 char *e;
2029 u_int type, rid, sid;
2030 int devnull;
2031
2032 debug3("%s: entering", __func__);
2033
2034 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
2035 error("%s: master alive request failed", __func__);
2036 return -1;
2037 }
2038
2039 signal(SIGPIPE, SIG_IGN);
2040
2041 if (stdin_null_flag) {
2042 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
2043 fatal("open(/dev/null): %s", strerror(errno));
2044 if (dup2(devnull, STDIN_FILENO) == -1)
2045 fatal("dup2: %s", strerror(errno));
2046 if (devnull > STDERR_FILENO)
2047 close(devnull);
2048 }
2049
2050 buffer_init(&m);
2051 buffer_put_int(&m, MUX_C_NEW_STDIO_FWD);
2052 buffer_put_int(&m, muxclient_request_id);
2053 buffer_put_cstring(&m, ""); /* reserved */
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00002054 buffer_put_cstring(&m, options.stdio_forward_host);
2055 buffer_put_int(&m, options.stdio_forward_port);
Damien Millere1537f92010-01-26 13:26:22 +11002056
2057 if (mux_client_write_packet(fd, &m) != 0)
2058 fatal("%s: write packet: %s", __func__, strerror(errno));
2059
2060 /* Send the stdio file descriptors */
2061 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
2062 mm_send_fd(fd, STDOUT_FILENO) == -1)
2063 fatal("%s: send fds failed", __func__);
2064
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00002065 if (pledge("stdio proc tty", NULL) == -1)
2066 fatal("%s pledge(): %s", __func__, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11002067 platform_pledge_mux();
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00002068
Damien Millere1537f92010-01-26 13:26:22 +11002069 debug3("%s: stdio forward request sent", __func__);
2070
2071 /* Read their reply */
2072 buffer_clear(&m);
2073
2074 if (mux_client_read_packet(fd, &m) != 0) {
2075 error("%s: read from master failed: %s",
2076 __func__, strerror(errno));
2077 buffer_free(&m);
2078 return -1;
2079 }
2080
2081 type = buffer_get_int(&m);
2082 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
2083 fatal("%s: out of sequence reply: my id %u theirs %u",
2084 __func__, muxclient_request_id, rid);
2085 switch (type) {
2086 case MUX_S_SESSION_OPENED:
2087 sid = buffer_get_int(&m);
2088 debug("%s: master session id: %u", __func__, sid);
2089 break;
2090 case MUX_S_PERMISSION_DENIED:
2091 e = buffer_get_string(&m, NULL);
2092 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11002093 fatal("Master refused stdio forwarding request: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11002094 case MUX_S_FAILURE:
2095 e = buffer_get_string(&m, NULL);
2096 buffer_free(&m);
Damien Miller357610d2014-07-18 15:04:10 +10002097 fatal("Stdio forwarding request failed: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11002098 default:
2099 buffer_free(&m);
2100 error("%s: unexpected response from master 0x%08x",
2101 __func__, type);
2102 return -1;
2103 }
2104 muxclient_request_id++;
2105
2106 signal(SIGHUP, control_client_sighandler);
2107 signal(SIGINT, control_client_sighandler);
2108 signal(SIGTERM, control_client_sighandler);
2109 signal(SIGWINCH, control_client_sigrelay);
2110
2111 /*
2112 * Stick around until the controlee closes the client_fd.
2113 */
2114 buffer_clear(&m);
2115 if (mux_client_read_packet(fd, &m) != 0) {
2116 if (errno == EPIPE ||
2117 (errno == EINTR && muxclient_terminate != 0))
2118 return 0;
2119 fatal("%s: mux_client_read_packet: %s",
2120 __func__, strerror(errno));
2121 }
2122 fatal("%s: master returned unexpected message %u", __func__, type);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002123}
2124
Damien Miller6c3eec72011-05-05 14:16:22 +10002125static void
2126mux_client_request_stop_listening(int fd)
2127{
2128 Buffer m;
2129 char *e;
2130 u_int type, rid;
2131
2132 debug3("%s: entering", __func__);
2133
2134 buffer_init(&m);
2135 buffer_put_int(&m, MUX_C_STOP_LISTENING);
2136 buffer_put_int(&m, muxclient_request_id);
2137
2138 if (mux_client_write_packet(fd, &m) != 0)
2139 fatal("%s: write packet: %s", __func__, strerror(errno));
2140
2141 buffer_clear(&m);
2142
2143 /* Read their reply */
2144 if (mux_client_read_packet(fd, &m) != 0)
2145 fatal("%s: read from master failed: %s",
2146 __func__, strerror(errno));
2147
2148 type = buffer_get_int(&m);
2149 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
2150 fatal("%s: out of sequence reply: my id %u theirs %u",
2151 __func__, muxclient_request_id, rid);
2152 switch (type) {
2153 case MUX_S_OK:
2154 break;
2155 case MUX_S_PERMISSION_DENIED:
2156 e = buffer_get_string(&m, NULL);
2157 fatal("Master refused stop listening request: %s", e);
2158 case MUX_S_FAILURE:
2159 e = buffer_get_string(&m, NULL);
2160 fatal("%s: stop listening request failed: %s", __func__, e);
2161 default:
2162 fatal("%s: unexpected response from master 0x%08x",
2163 __func__, type);
2164 }
2165 buffer_free(&m);
2166 muxclient_request_id++;
2167}
2168
Damien Millerb1cbfa22008-05-19 16:00:08 +10002169/* Multiplex client main loop. */
markus@openbsd.org8d057842016-09-30 09:19:13 +00002170int
Damien Millerb1cbfa22008-05-19 16:00:08 +10002171muxclient(const char *path)
2172{
2173 struct sockaddr_un addr;
Damien Millere1537f92010-01-26 13:26:22 +11002174 int sock;
2175 u_int pid;
Damien Millerb1cbfa22008-05-19 16:00:08 +10002176
Damien Millere1537f92010-01-26 13:26:22 +11002177 if (muxclient_command == 0) {
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00002178 if (options.stdio_forward_host != NULL)
Damien Millere1537f92010-01-26 13:26:22 +11002179 muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
2180 else
2181 muxclient_command = SSHMUX_COMMAND_OPEN;
2182 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002183
2184 switch (options.control_master) {
2185 case SSHCTL_MASTER_AUTO:
2186 case SSHCTL_MASTER_AUTO_ASK:
2187 debug("auto-mux: Trying existing master");
2188 /* FALLTHROUGH */
2189 case SSHCTL_MASTER_NO:
2190 break;
2191 default:
markus@openbsd.org8d057842016-09-30 09:19:13 +00002192 return -1;
Damien Millerb1cbfa22008-05-19 16:00:08 +10002193 }
2194
2195 memset(&addr, '\0', sizeof(addr));
2196 addr.sun_family = AF_UNIX;
Damien Millerb1cbfa22008-05-19 16:00:08 +10002197
2198 if (strlcpy(addr.sun_path, path,
2199 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
dtucker@openbsd.org67dca602016-08-08 22:40:57 +00002200 fatal("ControlPath too long ('%s' >= %u bytes)", path,
2201 (unsigned int)sizeof(addr.sun_path));
Damien Millerb1cbfa22008-05-19 16:00:08 +10002202
2203 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
2204 fatal("%s socket(): %s", __func__, strerror(errno));
2205
guenther@openbsd.org4ba15462017-01-21 11:32:04 +00002206 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
Damien Millere1537f92010-01-26 13:26:22 +11002207 switch (muxclient_command) {
2208 case SSHMUX_COMMAND_OPEN:
2209 case SSHMUX_COMMAND_STDIO_FWD:
2210 break;
2211 default:
Damien Millerb1cbfa22008-05-19 16:00:08 +10002212 fatal("Control socket connect(%.100s): %s", path,
2213 strerror(errno));
2214 }
Damien Miller603134e2010-09-24 22:07:55 +10002215 if (errno == ECONNREFUSED &&
2216 options.control_master != SSHCTL_MASTER_NO) {
2217 debug("Stale control socket %.100s, unlinking", path);
2218 unlink(path);
2219 } else if (errno == ENOENT) {
Damien Millerb1cbfa22008-05-19 16:00:08 +10002220 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller603134e2010-09-24 22:07:55 +10002221 } else {
Damien Millerb1cbfa22008-05-19 16:00:08 +10002222 error("Control socket connect(%.100s): %s", path,
2223 strerror(errno));
2224 }
2225 close(sock);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002226 return -1;
Damien Millerb1cbfa22008-05-19 16:00:08 +10002227 }
Damien Millere1537f92010-01-26 13:26:22 +11002228 set_nonblock(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002229
Damien Millere1537f92010-01-26 13:26:22 +11002230 if (mux_client_hello_exchange(sock) != 0) {
2231 error("%s: master hello exchange failed", __func__);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10002232 close(sock);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002233 return -1;
Darren Tuckerca19bfe2008-06-13 10:24:03 +10002234 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002235
2236 switch (muxclient_command) {
2237 case SSHMUX_COMMAND_ALIVE_CHECK:
Damien Millere1537f92010-01-26 13:26:22 +11002238 if ((pid = mux_client_request_alive(sock)) == 0)
2239 fatal("%s: master alive check failed", __func__);
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +00002240 fprintf(stderr, "Master running (pid=%u)\r\n", pid);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002241 exit(0);
2242 case SSHMUX_COMMAND_TERMINATE:
Damien Millere1537f92010-01-26 13:26:22 +11002243 mux_client_request_terminate(sock);
dtucker@openbsd.org0b9ee622016-10-19 23:21:56 +00002244 if (options.log_level != SYSLOG_LEVEL_QUIET)
2245 fprintf(stderr, "Exit request sent.\r\n");
Damien Millerb1cbfa22008-05-19 16:00:08 +10002246 exit(0);
Damien Miller388f6fc2010-05-21 14:57:35 +10002247 case SSHMUX_COMMAND_FORWARD:
Damien Millerf6dff7c2011-09-22 21:38:52 +10002248 if (mux_client_forwards(sock, 0) != 0)
Damien Miller388f6fc2010-05-21 14:57:35 +10002249 fatal("%s: master forward request failed", __func__);
2250 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002251 case SSHMUX_COMMAND_OPEN:
Damien Millerf6dff7c2011-09-22 21:38:52 +10002252 if (mux_client_forwards(sock, 0) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11002253 error("%s: master forward request failed", __func__);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002254 return -1;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10002255 }
Damien Millere1537f92010-01-26 13:26:22 +11002256 mux_client_request_session(sock);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002257 return -1;
Damien Millere1537f92010-01-26 13:26:22 +11002258 case SSHMUX_COMMAND_STDIO_FWD:
2259 mux_client_request_stdio_fwd(sock);
2260 exit(0);
Damien Miller6c3eec72011-05-05 14:16:22 +10002261 case SSHMUX_COMMAND_STOP:
2262 mux_client_request_stop_listening(sock);
dtucker@openbsd.org0b9ee622016-10-19 23:21:56 +00002263 if (options.log_level != SYSLOG_LEVEL_QUIET)
2264 fprintf(stderr, "Stop listening request sent.\r\n");
Damien Miller6c3eec72011-05-05 14:16:22 +10002265 exit(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10002266 case SSHMUX_COMMAND_CANCEL_FWD:
2267 if (mux_client_forwards(sock, 1) != 0)
2268 error("%s: master cancel forward request failed",
2269 __func__);
2270 exit(0);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002271 case SSHMUX_COMMAND_PROXY:
2272 mux_client_proxy(sock);
2273 return (sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002274 default:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10002275 fatal("unrecognised muxclient_command %d", muxclient_command);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002276 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002277}