blob: f3faaeec90eeca193ae99e264b13eb65a04fbea2 [file] [log] [blame]
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001/* $OpenBSD: mux.c,v 1.50 2015/01/20 23:14:00 deraadt 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"
82
83/* from ssh.c */
84extern int tty_flag;
85extern Options options;
86extern int stdin_null_flag;
87extern char *host;
Darren Tucker8ec4fd82009-10-07 08:39:57 +110088extern int subsystem_flag;
Damien Millerb1cbfa22008-05-19 16:00:08 +100089extern Buffer command;
Damien Millere1537f92010-01-26 13:26:22 +110090extern volatile sig_atomic_t quit_pending;
91extern char *stdio_forward_host;
92extern int stdio_forward_port;
Damien Millerb1cbfa22008-05-19 16:00:08 +100093
Darren Tucker2fb66ca2008-06-13 04:49:33 +100094/* Context for session open confirmation callback */
95struct mux_session_confirm_ctx {
Damien Millere1537f92010-01-26 13:26:22 +110096 u_int want_tty;
97 u_int want_subsys;
98 u_int want_x_fwd;
99 u_int want_agent_fwd;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000100 Buffer cmd;
101 char *term;
102 struct termios tio;
103 char **env;
Damien Millerd530f5f2010-05-21 14:57:10 +1000104 u_int rid;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000105};
106
Damien Miller357610d2014-07-18 15:04:10 +1000107/* Context for stdio fwd open confirmation callback */
108struct mux_stdio_confirm_ctx {
109 u_int rid;
110};
111
Damien Miller388f6fc2010-05-21 14:57:35 +1000112/* Context for global channel callback */
113struct mux_channel_confirm_ctx {
114 u_int cid; /* channel id */
115 u_int rid; /* request id */
116 int fid; /* forward id */
117};
118
Damien Millerb1cbfa22008-05-19 16:00:08 +1000119/* fd to control socket */
120int muxserver_sock = -1;
121
Damien Millere1537f92010-01-26 13:26:22 +1100122/* client request id */
123u_int muxclient_request_id = 0;
124
Damien Millerb1cbfa22008-05-19 16:00:08 +1000125/* Multiplexing control command */
126u_int muxclient_command = 0;
127
128/* Set when signalled. */
129static volatile sig_atomic_t muxclient_terminate = 0;
130
131/* PID of multiplex server */
132static u_int muxserver_pid = 0;
133
Damien Millere1537f92010-01-26 13:26:22 +1100134static Channel *mux_listener_channel = NULL;
Damien Millerb1cbfa22008-05-19 16:00:08 +1000135
Damien Millere1537f92010-01-26 13:26:22 +1100136struct mux_master_state {
137 int hello_rcvd;
138};
139
140/* mux protocol messages */
141#define MUX_MSG_HELLO 0x00000001
142#define MUX_C_NEW_SESSION 0x10000002
143#define MUX_C_ALIVE_CHECK 0x10000004
144#define MUX_C_TERMINATE 0x10000005
145#define MUX_C_OPEN_FWD 0x10000006
146#define MUX_C_CLOSE_FWD 0x10000007
147#define MUX_C_NEW_STDIO_FWD 0x10000008
Damien Miller6c3eec72011-05-05 14:16:22 +1000148#define MUX_C_STOP_LISTENING 0x10000009
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
Damien Millere1537f92010-01-26 13:26:22 +1100157
158/* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
159#define MUX_FWD_LOCAL 1
160#define MUX_FWD_REMOTE 2
161#define MUX_FWD_DYNAMIC 3
162
Damien Millerd530f5f2010-05-21 14:57:10 +1000163static void mux_session_confirm(int, int, void *);
Damien Miller357610d2014-07-18 15:04:10 +1000164static void mux_stdio_confirm(int, int, void *);
Damien Millere1537f92010-01-26 13:26:22 +1100165
166static int process_mux_master_hello(u_int, Channel *, Buffer *, Buffer *);
167static int process_mux_new_session(u_int, Channel *, Buffer *, Buffer *);
168static int process_mux_alive_check(u_int, Channel *, Buffer *, Buffer *);
169static int process_mux_terminate(u_int, Channel *, Buffer *, Buffer *);
170static int process_mux_open_fwd(u_int, Channel *, Buffer *, Buffer *);
171static int process_mux_close_fwd(u_int, Channel *, Buffer *, Buffer *);
172static int process_mux_stdio_fwd(u_int, Channel *, Buffer *, Buffer *);
Damien Miller6c3eec72011-05-05 14:16:22 +1000173static int process_mux_stop_listening(u_int, Channel *, Buffer *, Buffer *);
Damien Millere1537f92010-01-26 13:26:22 +1100174
175static const struct {
176 u_int type;
177 int (*handler)(u_int, Channel *, Buffer *, Buffer *);
178} mux_master_handlers[] = {
179 { MUX_MSG_HELLO, process_mux_master_hello },
180 { MUX_C_NEW_SESSION, process_mux_new_session },
181 { MUX_C_ALIVE_CHECK, process_mux_alive_check },
182 { MUX_C_TERMINATE, process_mux_terminate },
183 { MUX_C_OPEN_FWD, process_mux_open_fwd },
184 { MUX_C_CLOSE_FWD, process_mux_close_fwd },
185 { MUX_C_NEW_STDIO_FWD, process_mux_stdio_fwd },
Damien Miller6c3eec72011-05-05 14:16:22 +1000186 { MUX_C_STOP_LISTENING, process_mux_stop_listening },
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
Damien Millere1537f92010-01-26 13:26:22 +1100193mux_master_session_cleanup_cb(int cid, void *unused)
194{
195 Channel *cc, *c = channel_by_id(cid);
196
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) {
201 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
202 fatal("%s: channel %d missing control channel %d",
203 __func__, c->self, c->ctl_chan);
204 c->ctl_chan = -1;
205 cc->remote_id = -1;
206 chan_rcvd_oclose(cc);
207 }
208 channel_cancel_cleanup(c->self);
209}
210
211/* Cleanup callback fired on closure of mux slave _control_ channel */
212/* ARGSUSED */
213static void
214mux_master_control_cleanup_cb(int cid, void *unused)
215{
216 Channel *sc, *c = channel_by_id(cid);
217
218 debug3("%s: entering for channel %d", __func__, cid);
219 if (c == NULL)
220 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
221 if (c->remote_id != -1) {
222 if ((sc = channel_by_id(c->remote_id)) == NULL)
Damien Miller601a23c2010-04-16 15:54:01 +1000223 fatal("%s: channel %d missing session channel %d",
Damien Millere1537f92010-01-26 13:26:22 +1100224 __func__, c->self, c->remote_id);
225 c->remote_id = -1;
226 sc->ctl_chan = -1;
Damien Miller172859c2013-04-23 15:19:27 +1000227 if (sc->type != SSH_CHANNEL_OPEN &&
228 sc->type != SSH_CHANNEL_OPENING) {
Damien Millera21cdfa2010-01-28 06:26:59 +1100229 debug2("%s: channel %d: not open", __func__, sc->self);
Damien Miller133d9d32010-01-30 17:30:04 +1100230 chan_mark_dead(sc);
Damien Millera21cdfa2010-01-28 06:26:59 +1100231 } else {
Damien Miller0dac03f2010-01-30 17:36:33 +1100232 if (sc->istate == CHAN_INPUT_OPEN)
233 chan_read_failed(sc);
234 if (sc->ostate == CHAN_OUTPUT_OPEN)
235 chan_write_failed(sc);
Damien Millera21cdfa2010-01-28 06:26:59 +1100236 }
Damien Millere1537f92010-01-26 13:26:22 +1100237 }
238 channel_cancel_cleanup(c->self);
239}
240
241/* Check mux client environment variables before passing them to mux master. */
242static int
243env_permitted(char *env)
244{
245 int i, ret;
246 char name[1024], *cp;
247
248 if ((cp = strchr(env, '=')) == NULL || cp == env)
249 return 0;
250 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
251 if (ret <= 0 || (size_t)ret >= sizeof(name)) {
252 error("env_permitted: name '%.100s...' too long", env);
253 return 0;
254 }
255
256 for (i = 0; i < options.num_send_env; i++)
257 if (match_pattern(name, options.send_env[i]))
258 return 1;
259
260 return 0;
261}
262
263/* Mux master protocol message handlers */
264
265static int
266process_mux_master_hello(u_int rid, Channel *c, Buffer *m, Buffer *r)
267{
268 u_int ver;
269 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
270
271 if (state == NULL)
272 fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self);
273 if (state->hello_rcvd) {
274 error("%s: HELLO received twice", __func__);
275 return -1;
276 }
277 if (buffer_get_int_ret(&ver, m) != 0) {
278 malf:
279 error("%s: malformed message", __func__);
280 return -1;
281 }
282 if (ver != SSHMUX_VER) {
283 error("Unsupported multiplexing protocol version %d "
284 "(expected %d)", ver, SSHMUX_VER);
285 return -1;
286 }
287 debug2("%s: channel %d slave version %u", __func__, c->self, ver);
288
289 /* No extensions are presently defined */
290 while (buffer_len(m) > 0) {
291 char *name = buffer_get_string_ret(m, NULL);
292 char *value = buffer_get_string_ret(m, NULL);
293
294 if (name == NULL || value == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000295 free(name);
Darren Tucker746e9062013-06-06 08:20:13 +1000296 free(value);
Damien Millere1537f92010-01-26 13:26:22 +1100297 goto malf;
298 }
299 debug2("Unrecognised slave extension \"%s\"", name);
Darren Tuckera627d422013-06-02 07:31:17 +1000300 free(name);
301 free(value);
Damien Millere1537f92010-01-26 13:26:22 +1100302 }
303 state->hello_rcvd = 1;
304 return 0;
305}
306
307static int
308process_mux_new_session(u_int rid, Channel *c, Buffer *m, Buffer *r)
309{
310 Channel *nc;
311 struct mux_session_confirm_ctx *cctx;
312 char *reserved, *cmd, *cp;
313 u_int i, j, len, env_len, escape_char, window, packetmax;
314 int new_fd[3];
315
316 /* Reply for SSHMUX_COMMAND_OPEN */
317 cctx = xcalloc(1, sizeof(*cctx));
318 cctx->term = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000319 cctx->rid = rid;
Damien Millere1537f92010-01-26 13:26:22 +1100320 cmd = reserved = NULL;
Damien Millerab523b02012-07-06 13:44:43 +1000321 cctx->env = NULL;
322 env_len = 0;
Damien Millere1537f92010-01-26 13:26:22 +1100323 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
324 buffer_get_int_ret(&cctx->want_tty, m) != 0 ||
325 buffer_get_int_ret(&cctx->want_x_fwd, m) != 0 ||
326 buffer_get_int_ret(&cctx->want_agent_fwd, m) != 0 ||
327 buffer_get_int_ret(&cctx->want_subsys, m) != 0 ||
328 buffer_get_int_ret(&escape_char, m) != 0 ||
329 (cctx->term = buffer_get_string_ret(m, &len)) == NULL ||
330 (cmd = buffer_get_string_ret(m, &len)) == NULL) {
331 malf:
Darren Tuckera627d422013-06-02 07:31:17 +1000332 free(cmd);
333 free(reserved);
Damien Millerab523b02012-07-06 13:44:43 +1000334 for (j = 0; j < env_len; j++)
Darren Tuckera627d422013-06-02 07:31:17 +1000335 free(cctx->env[j]);
336 free(cctx->env);
337 free(cctx->term);
338 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100339 error("%s: malformed message", __func__);
340 return -1;
341 }
Darren Tuckera627d422013-06-02 07:31:17 +1000342 free(reserved);
Damien Millere1537f92010-01-26 13:26:22 +1100343 reserved = NULL;
344
Damien Millere1537f92010-01-26 13:26:22 +1100345 while (buffer_len(m) > 0) {
346#define MUX_MAX_ENV_VARS 4096
Damien Miller2ec03422012-02-11 08:16:28 +1100347 if ((cp = buffer_get_string_ret(m, &len)) == NULL)
Damien Millere1537f92010-01-26 13:26:22 +1100348 goto malf;
Damien Millere1537f92010-01-26 13:26:22 +1100349 if (!env_permitted(cp)) {
Darren Tuckera627d422013-06-02 07:31:17 +1000350 free(cp);
Damien Millere1537f92010-01-26 13:26:22 +1100351 continue;
352 }
353 cctx->env = xrealloc(cctx->env, env_len + 2,
354 sizeof(*cctx->env));
355 cctx->env[env_len++] = cp;
356 cctx->env[env_len] = NULL;
357 if (env_len > MUX_MAX_ENV_VARS) {
358 error(">%d environment variables received, ignoring "
359 "additional", MUX_MAX_ENV_VARS);
360 break;
361 }
362 }
363
364 debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
365 "term \"%s\", cmd \"%s\", env %u", __func__, c->self,
366 cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
367 cctx->want_subsys, cctx->term, cmd, env_len);
368
369 buffer_init(&cctx->cmd);
370 buffer_append(&cctx->cmd, cmd, strlen(cmd));
Darren Tuckera627d422013-06-02 07:31:17 +1000371 free(cmd);
Damien Millere1537f92010-01-26 13:26:22 +1100372 cmd = NULL;
373
374 /* Gather fds from client */
375 for(i = 0; i < 3; i++) {
376 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
377 error("%s: failed to receive fd %d from slave",
378 __func__, i);
379 for (j = 0; j < i; j++)
380 close(new_fd[j]);
381 for (j = 0; j < env_len; j++)
Darren Tuckera627d422013-06-02 07:31:17 +1000382 free(cctx->env[j]);
383 free(cctx->env);
384 free(cctx->term);
Damien Millere1537f92010-01-26 13:26:22 +1100385 buffer_free(&cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +1000386 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100387
388 /* prepare reply */
389 buffer_put_int(r, MUX_S_FAILURE);
390 buffer_put_int(r, rid);
391 buffer_put_cstring(r,
392 "did not receive file descriptors");
393 return -1;
394 }
395 }
396
397 debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
398 new_fd[0], new_fd[1], new_fd[2]);
399
400 /* XXX support multiple child sessions in future */
401 if (c->remote_id != -1) {
402 debug2("%s: session already open", __func__);
403 /* prepare reply */
404 buffer_put_int(r, MUX_S_FAILURE);
405 buffer_put_int(r, rid);
406 buffer_put_cstring(r, "Multiple sessions not supported");
407 cleanup:
408 close(new_fd[0]);
409 close(new_fd[1]);
410 close(new_fd[2]);
Darren Tuckera627d422013-06-02 07:31:17 +1000411 free(cctx->term);
Damien Millere1537f92010-01-26 13:26:22 +1100412 if (env_len != 0) {
413 for (i = 0; i < env_len; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000414 free(cctx->env[i]);
415 free(cctx->env);
Damien Millere1537f92010-01-26 13:26:22 +1100416 }
417 buffer_free(&cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +1000418 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100419 return 0;
420 }
421
422 if (options.control_master == SSHCTL_MASTER_ASK ||
423 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
424 if (!ask_permission("Allow shared connection to %s? ", host)) {
425 debug2("%s: session refused by user", __func__);
426 /* prepare reply */
427 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
428 buffer_put_int(r, rid);
429 buffer_put_cstring(r, "Permission denied");
430 goto cleanup;
431 }
432 }
433
434 /* Try to pick up ttymodes from client before it goes raw */
435 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
436 error("%s: tcgetattr: %s", __func__, strerror(errno));
437
438 /* enable nonblocking unless tty */
439 if (!isatty(new_fd[0]))
440 set_nonblock(new_fd[0]);
441 if (!isatty(new_fd[1]))
442 set_nonblock(new_fd[1]);
443 if (!isatty(new_fd[2]))
444 set_nonblock(new_fd[2]);
445
446 window = CHAN_SES_WINDOW_DEFAULT;
447 packetmax = CHAN_SES_PACKET_DEFAULT;
448 if (cctx->want_tty) {
449 window >>= 1;
450 packetmax >>= 1;
451 }
452
453 nc = channel_new("session", SSH_CHANNEL_OPENING,
454 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
455 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
456
457 nc->ctl_chan = c->self; /* link session -> control channel */
458 c->remote_id = nc->self; /* link control -> session channel */
459
460 if (cctx->want_tty && escape_char != 0xffffffff) {
461 channel_register_filter(nc->self,
462 client_simple_escape_filter, NULL,
463 client_filter_cleanup,
464 client_new_escape_filter_ctx((int)escape_char));
465 }
466
467 debug2("%s: channel_new: %d linked to control channel %d",
468 __func__, nc->self, nc->ctl_chan);
469
470 channel_send_open(nc->self);
471 channel_register_open_confirm(nc->self, mux_session_confirm, cctx);
Damien Millerd530f5f2010-05-21 14:57:10 +1000472 c->mux_pause = 1; /* stop handling messages until open_confirm done */
Damien Miller85c50d72010-05-10 11:53:02 +1000473 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +1100474
Damien Millerd530f5f2010-05-21 14:57:10 +1000475 /* reply is deferred, sent by mux_session_confirm */
Damien Millere1537f92010-01-26 13:26:22 +1100476 return 0;
477}
478
479static int
480process_mux_alive_check(u_int rid, Channel *c, Buffer *m, Buffer *r)
481{
482 debug2("%s: channel %d: alive check", __func__, c->self);
483
484 /* prepare reply */
485 buffer_put_int(r, MUX_S_ALIVE);
486 buffer_put_int(r, rid);
487 buffer_put_int(r, (u_int)getpid());
488
489 return 0;
490}
491
492static int
493process_mux_terminate(u_int rid, Channel *c, Buffer *m, Buffer *r)
494{
495 debug2("%s: channel %d: terminate request", __func__, c->self);
496
497 if (options.control_master == SSHCTL_MASTER_ASK ||
498 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
499 if (!ask_permission("Terminate shared connection to %s? ",
500 host)) {
501 debug2("%s: termination refused by user", __func__);
502 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
503 buffer_put_int(r, rid);
504 buffer_put_cstring(r, "Permission denied");
505 return 0;
506 }
507 }
508
509 quit_pending = 1;
510 buffer_put_int(r, MUX_S_OK);
511 buffer_put_int(r, rid);
512 /* XXX exit happens too soon - message never makes it to client */
513 return 0;
514}
515
516static char *
Damien Miller7acefbb2014-07-18 14:11:24 +1000517format_forward(u_int ftype, struct Forward *fwd)
Damien Millere1537f92010-01-26 13:26:22 +1100518{
519 char *ret;
520
521 switch (ftype) {
522 case MUX_FWD_LOCAL:
523 xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +1000524 (fwd->listen_path != NULL) ? fwd->listen_path :
Damien Millere1537f92010-01-26 13:26:22 +1100525 (fwd->listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +1000526 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millere1537f92010-01-26 13:26:22 +1100527 fwd->listen_host, fwd->listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +1000528 (fwd->connect_path != NULL) ? fwd->connect_path :
Damien Millere1537f92010-01-26 13:26:22 +1100529 fwd->connect_host, fwd->connect_port);
530 break;
531 case MUX_FWD_DYNAMIC:
532 xasprintf(&ret, "dynamic forward %.200s:%d -> *",
533 (fwd->listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +1000534 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millere1537f92010-01-26 13:26:22 +1100535 fwd->listen_host, fwd->listen_port);
536 break;
537 case MUX_FWD_REMOTE:
538 xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +1000539 (fwd->listen_path != NULL) ? fwd->listen_path :
Damien Millere1537f92010-01-26 13:26:22 +1100540 (fwd->listen_host == NULL) ?
541 "LOCALHOST" : fwd->listen_host,
542 fwd->listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +1000543 (fwd->connect_path != NULL) ? fwd->connect_path :
Damien Millere1537f92010-01-26 13:26:22 +1100544 fwd->connect_host, fwd->connect_port);
545 break;
546 default:
547 fatal("%s: unknown forward type %u", __func__, ftype);
548 }
549 return ret;
550}
551
552static int
553compare_host(const char *a, const char *b)
554{
555 if (a == NULL && b == NULL)
556 return 1;
557 if (a == NULL || b == NULL)
558 return 0;
559 return strcmp(a, b) == 0;
560}
561
562static int
Damien Miller7acefbb2014-07-18 14:11:24 +1000563compare_forward(struct Forward *a, struct Forward *b)
Damien Millere1537f92010-01-26 13:26:22 +1100564{
565 if (!compare_host(a->listen_host, b->listen_host))
566 return 0;
Damien Miller7acefbb2014-07-18 14:11:24 +1000567 if (!compare_host(a->listen_path, b->listen_path))
568 return 0;
Damien Millere1537f92010-01-26 13:26:22 +1100569 if (a->listen_port != b->listen_port)
570 return 0;
571 if (!compare_host(a->connect_host, b->connect_host))
572 return 0;
Damien Miller7acefbb2014-07-18 14:11:24 +1000573 if (!compare_host(a->connect_path, b->connect_path))
574 return 0;
Damien Millere1537f92010-01-26 13:26:22 +1100575 if (a->connect_port != b->connect_port)
576 return 0;
577
578 return 1;
579}
580
Damien Miller388f6fc2010-05-21 14:57:35 +1000581static void
582mux_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
583{
584 struct mux_channel_confirm_ctx *fctx = ctxt;
585 char *failmsg = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000586 struct Forward *rfwd;
Damien Miller388f6fc2010-05-21 14:57:35 +1000587 Channel *c;
588 Buffer out;
589
590 if ((c = channel_by_id(fctx->cid)) == NULL) {
591 /* no channel for reply */
592 error("%s: unknown channel", __func__);
593 return;
594 }
595 buffer_init(&out);
596 if (fctx->fid >= options.num_remote_forwards) {
597 xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
598 goto fail;
599 }
600 rfwd = &options.remote_forwards[fctx->fid];
601 debug("%s: %s for: listen %d, connect %s:%d", __func__,
602 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Miller7acefbb2014-07-18 14:11:24 +1000603 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
604 rfwd->connect_host, rfwd->connect_port);
Damien Miller388f6fc2010-05-21 14:57:35 +1000605 if (type == SSH2_MSG_REQUEST_SUCCESS) {
606 if (rfwd->listen_port == 0) {
607 rfwd->allocated_port = packet_get_int();
608 logit("Allocated port %u for mux remote forward"
609 " to %s:%d", rfwd->allocated_port,
610 rfwd->connect_host, rfwd->connect_port);
611 buffer_put_int(&out, MUX_S_REMOTE_PORT);
612 buffer_put_int(&out, fctx->rid);
613 buffer_put_int(&out, rfwd->allocated_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +1100614 channel_update_permitted_opens(rfwd->handle,
615 rfwd->allocated_port);
Damien Miller388f6fc2010-05-21 14:57:35 +1000616 } else {
617 buffer_put_int(&out, MUX_S_OK);
618 buffer_put_int(&out, fctx->rid);
619 }
620 goto out;
621 } else {
Darren Tucker68afb8c2011-10-02 18:59:03 +1100622 if (rfwd->listen_port == 0)
623 channel_update_permitted_opens(rfwd->handle, -1);
Damien Miller7acefbb2014-07-18 14:11:24 +1000624 if (rfwd->listen_path != NULL)
625 xasprintf(&failmsg, "remote port forwarding failed for "
626 "listen path %s", rfwd->listen_path);
627 else
628 xasprintf(&failmsg, "remote port forwarding failed for "
629 "listen port %d", rfwd->listen_port);
Damien Miller388f6fc2010-05-21 14:57:35 +1000630 }
631 fail:
632 error("%s: %s", __func__, failmsg);
633 buffer_put_int(&out, MUX_S_FAILURE);
634 buffer_put_int(&out, fctx->rid);
635 buffer_put_cstring(&out, failmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000636 free(failmsg);
Damien Miller388f6fc2010-05-21 14:57:35 +1000637 out:
638 buffer_put_string(&c->output, buffer_ptr(&out), buffer_len(&out));
639 buffer_free(&out);
640 if (c->mux_pause <= 0)
641 fatal("%s: mux_pause %d", __func__, c->mux_pause);
642 c->mux_pause = 0; /* start processing messages again */
643}
644
Damien Millere1537f92010-01-26 13:26:22 +1100645static int
646process_mux_open_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
647{
Damien Miller7acefbb2014-07-18 14:11:24 +1000648 struct Forward fwd;
Damien Millere1537f92010-01-26 13:26:22 +1100649 char *fwd_desc = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000650 char *listen_addr, *connect_addr;
Damien Millere1537f92010-01-26 13:26:22 +1100651 u_int ftype;
Damien Millerce986542013-07-18 16:12:44 +1000652 u_int lport, cport;
Damien Millere1537f92010-01-26 13:26:22 +1100653 int i, ret = 0, freefwd = 1;
654
Damien Miller7acefbb2014-07-18 14:11:24 +1000655 /* XXX - lport/cport check redundant */
Damien Millere1537f92010-01-26 13:26:22 +1100656 if (buffer_get_int_ret(&ftype, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000657 (listen_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000658 buffer_get_int_ret(&lport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000659 (connect_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000660 buffer_get_int_ret(&cport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000661 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
662 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
Damien Millere1537f92010-01-26 13:26:22 +1100663 error("%s: malformed message", __func__);
664 ret = -1;
665 goto out;
666 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000667 if (*listen_addr == '\0') {
668 free(listen_addr);
669 listen_addr = NULL;
670 }
671 if (*connect_addr == '\0') {
672 free(connect_addr);
673 connect_addr = NULL;
674 }
675
676 memset(&fwd, 0, sizeof(fwd));
Damien Millerce986542013-07-18 16:12:44 +1000677 fwd.listen_port = lport;
Damien Miller7acefbb2014-07-18 14:11:24 +1000678 if (fwd.listen_port == PORT_STREAMLOCAL)
679 fwd.listen_path = listen_addr;
680 else
681 fwd.listen_host = listen_addr;
Damien Millerce986542013-07-18 16:12:44 +1000682 fwd.connect_port = cport;
Damien Miller7acefbb2014-07-18 14:11:24 +1000683 if (fwd.connect_port == PORT_STREAMLOCAL)
684 fwd.connect_path = connect_addr;
685 else
686 fwd.connect_host = connect_addr;
Damien Millere1537f92010-01-26 13:26:22 +1100687
688 debug2("%s: channel %d: request %s", __func__, c->self,
689 (fwd_desc = format_forward(ftype, &fwd)));
690
691 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
692 ftype != MUX_FWD_DYNAMIC) {
693 logit("%s: invalid forwarding type %u", __func__, ftype);
694 invalid:
Damien Miller7acefbb2014-07-18 14:11:24 +1000695 free(listen_addr);
696 free(connect_addr);
Damien Millere1537f92010-01-26 13:26:22 +1100697 buffer_put_int(r, MUX_S_FAILURE);
698 buffer_put_int(r, rid);
699 buffer_put_cstring(r, "Invalid forwarding request");
700 return 0;
701 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000702 if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) {
703 logit("%s: streamlocal and dynamic forwards "
704 "are mutually exclusive", __func__);
705 goto invalid;
706 }
707 if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) {
Damien Millere1537f92010-01-26 13:26:22 +1100708 logit("%s: invalid listen port %u", __func__,
709 fwd.listen_port);
710 goto invalid;
711 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000712 if ((fwd.connect_port != PORT_STREAMLOCAL && fwd.connect_port >= 65536)
713 || (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE && fwd.connect_port == 0)) {
Damien Millere1537f92010-01-26 13:26:22 +1100714 logit("%s: invalid connect port %u", __func__,
715 fwd.connect_port);
716 goto invalid;
717 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000718 if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL && fwd.connect_path == NULL) {
Damien Millere1537f92010-01-26 13:26:22 +1100719 logit("%s: missing connect host", __func__);
720 goto invalid;
721 }
722
723 /* Skip forwards that have already been requested */
724 switch (ftype) {
725 case MUX_FWD_LOCAL:
726 case MUX_FWD_DYNAMIC:
727 for (i = 0; i < options.num_local_forwards; i++) {
728 if (compare_forward(&fwd,
729 options.local_forwards + i)) {
730 exists:
731 debug2("%s: found existing forwarding",
732 __func__);
733 buffer_put_int(r, MUX_S_OK);
734 buffer_put_int(r, rid);
735 goto out;
736 }
737 }
738 break;
739 case MUX_FWD_REMOTE:
740 for (i = 0; i < options.num_remote_forwards; i++) {
741 if (compare_forward(&fwd,
Damien Miller388f6fc2010-05-21 14:57:35 +1000742 options.remote_forwards + i)) {
743 if (fwd.listen_port != 0)
744 goto exists;
745 debug2("%s: found allocated port",
746 __func__);
747 buffer_put_int(r, MUX_S_REMOTE_PORT);
748 buffer_put_int(r, rid);
749 buffer_put_int(r,
750 options.remote_forwards[i].allocated_port);
751 goto out;
752 }
Damien Millere1537f92010-01-26 13:26:22 +1100753 }
754 break;
755 }
756
757 if (options.control_master == SSHCTL_MASTER_ASK ||
758 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
759 if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
760 debug2("%s: forwarding refused by user", __func__);
761 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
762 buffer_put_int(r, rid);
763 buffer_put_cstring(r, "Permission denied");
764 goto out;
765 }
766 }
767
768 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000769 if (!channel_setup_local_fwd_listener(&fwd,
770 &options.fwd_opts)) {
Damien Millere1537f92010-01-26 13:26:22 +1100771 fail:
772 logit("slave-requested %s failed", fwd_desc);
773 buffer_put_int(r, MUX_S_FAILURE);
774 buffer_put_int(r, rid);
775 buffer_put_cstring(r, "Port forwarding failed");
776 goto out;
777 }
778 add_local_forward(&options, &fwd);
779 freefwd = 0;
780 } else {
Damien Miller388f6fc2010-05-21 14:57:35 +1000781 struct mux_channel_confirm_ctx *fctx;
782
Damien Miller7acefbb2014-07-18 14:11:24 +1000783 fwd.handle = channel_request_remote_forwarding(&fwd);
Darren Tucker68afb8c2011-10-02 18:59:03 +1100784 if (fwd.handle < 0)
Damien Millere1537f92010-01-26 13:26:22 +1100785 goto fail;
786 add_remote_forward(&options, &fwd);
Damien Miller388f6fc2010-05-21 14:57:35 +1000787 fctx = xcalloc(1, sizeof(*fctx));
788 fctx->cid = c->self;
789 fctx->rid = rid;
Damien Miller232cfb12010-06-26 09:50:30 +1000790 fctx->fid = options.num_remote_forwards - 1;
Damien Miller388f6fc2010-05-21 14:57:35 +1000791 client_register_global_confirm(mux_confirm_remote_forward,
792 fctx);
Damien Millere1537f92010-01-26 13:26:22 +1100793 freefwd = 0;
Damien Miller388f6fc2010-05-21 14:57:35 +1000794 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
795 /* delayed reply in mux_confirm_remote_forward */
796 goto out;
Damien Millere1537f92010-01-26 13:26:22 +1100797 }
798 buffer_put_int(r, MUX_S_OK);
799 buffer_put_int(r, rid);
800 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000801 free(fwd_desc);
Damien Millere1537f92010-01-26 13:26:22 +1100802 if (freefwd) {
Darren Tuckera627d422013-06-02 07:31:17 +1000803 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000804 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000805 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000806 free(fwd.connect_path);
Damien Millere1537f92010-01-26 13:26:22 +1100807 }
808 return ret;
809}
810
811static int
812process_mux_close_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
813{
Damien Miller7acefbb2014-07-18 14:11:24 +1000814 struct Forward fwd, *found_fwd;
Damien Millere1537f92010-01-26 13:26:22 +1100815 char *fwd_desc = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000816 const char *error_reason = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000817 char *listen_addr = NULL, *connect_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100818 u_int ftype;
Damien Miller7acefbb2014-07-18 14:11:24 +1000819 int i, ret = 0;
Damien Millerce986542013-07-18 16:12:44 +1000820 u_int lport, cport;
Damien Millere1537f92010-01-26 13:26:22 +1100821
Damien Millere1537f92010-01-26 13:26:22 +1100822 if (buffer_get_int_ret(&ftype, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000823 (listen_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000824 buffer_get_int_ret(&lport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000825 (connect_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000826 buffer_get_int_ret(&cport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000827 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
828 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
Damien Millere1537f92010-01-26 13:26:22 +1100829 error("%s: malformed message", __func__);
830 ret = -1;
831 goto out;
832 }
833
Damien Miller7acefbb2014-07-18 14:11:24 +1000834 if (*listen_addr == '\0') {
835 free(listen_addr);
836 listen_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100837 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000838 if (*connect_addr == '\0') {
839 free(connect_addr);
840 connect_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100841 }
842
Damien Miller7acefbb2014-07-18 14:11:24 +1000843 memset(&fwd, 0, sizeof(fwd));
844 fwd.listen_port = lport;
845 if (fwd.listen_port == PORT_STREAMLOCAL)
846 fwd.listen_path = listen_addr;
847 else
848 fwd.listen_host = listen_addr;
849 fwd.connect_port = cport;
850 if (fwd.connect_port == PORT_STREAMLOCAL)
851 fwd.connect_path = connect_addr;
852 else
853 fwd.connect_host = connect_addr;
854
Damien Millerf6dff7c2011-09-22 21:38:52 +1000855 debug2("%s: channel %d: request cancel %s", __func__, c->self,
Damien Millere1537f92010-01-26 13:26:22 +1100856 (fwd_desc = format_forward(ftype, &fwd)));
857
Damien Millerf6dff7c2011-09-22 21:38:52 +1000858 /* make sure this has been requested */
859 found_fwd = NULL;
860 switch (ftype) {
861 case MUX_FWD_LOCAL:
862 case MUX_FWD_DYNAMIC:
863 for (i = 0; i < options.num_local_forwards; i++) {
864 if (compare_forward(&fwd,
865 options.local_forwards + i)) {
866 found_fwd = options.local_forwards + i;
867 break;
868 }
869 }
870 break;
871 case MUX_FWD_REMOTE:
872 for (i = 0; i < options.num_remote_forwards; i++) {
873 if (compare_forward(&fwd,
874 options.remote_forwards + i)) {
875 found_fwd = options.remote_forwards + i;
876 break;
877 }
878 }
879 break;
880 }
Damien Millere1537f92010-01-26 13:26:22 +1100881
Damien Millerf6dff7c2011-09-22 21:38:52 +1000882 if (found_fwd == NULL)
883 error_reason = "port not forwarded";
884 else if (ftype == MUX_FWD_REMOTE) {
885 /*
886 * This shouldn't fail unless we confused the host/port
887 * between options.remote_forwards and permitted_opens.
Darren Tucker68afb8c2011-10-02 18:59:03 +1100888 * However, for dynamic allocated listen ports we need
Damien Miller7acefbb2014-07-18 14:11:24 +1000889 * to use the actual listen port.
Damien Millerf6dff7c2011-09-22 21:38:52 +1000890 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000891 if (channel_request_rforward_cancel(found_fwd) == -1)
Damien Millerf6dff7c2011-09-22 21:38:52 +1000892 error_reason = "port not in permitted opens";
893 } else { /* local and dynamic forwards */
894 /* Ditto */
Damien Miller7acefbb2014-07-18 14:11:24 +1000895 if (channel_cancel_lport_listener(&fwd, fwd.connect_port,
896 &options.fwd_opts) == -1)
Damien Millerf6dff7c2011-09-22 21:38:52 +1000897 error_reason = "port not found";
898 }
899
900 if (error_reason == NULL) {
901 buffer_put_int(r, MUX_S_OK);
902 buffer_put_int(r, rid);
903
Darren Tuckera627d422013-06-02 07:31:17 +1000904 free(found_fwd->listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000905 free(found_fwd->listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000906 free(found_fwd->connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000907 free(found_fwd->connect_path);
Damien Millerf6dff7c2011-09-22 21:38:52 +1000908 found_fwd->listen_host = found_fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000909 found_fwd->listen_path = found_fwd->connect_path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000910 found_fwd->listen_port = found_fwd->connect_port = 0;
911 } else {
912 buffer_put_int(r, MUX_S_FAILURE);
913 buffer_put_int(r, rid);
914 buffer_put_cstring(r, error_reason);
915 }
Damien Millere1537f92010-01-26 13:26:22 +1100916 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000917 free(fwd_desc);
Damien Miller7acefbb2014-07-18 14:11:24 +1000918 free(listen_addr);
919 free(connect_addr);
Damien Millere1537f92010-01-26 13:26:22 +1100920
921 return ret;
922}
923
924static int
925process_mux_stdio_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
926{
927 Channel *nc;
928 char *reserved, *chost;
929 u_int cport, i, j;
930 int new_fd[2];
Damien Miller357610d2014-07-18 15:04:10 +1000931 struct mux_stdio_confirm_ctx *cctx;
Damien Millere1537f92010-01-26 13:26:22 +1100932
933 chost = reserved = NULL;
934 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
935 (chost = buffer_get_string_ret(m, NULL)) == NULL ||
936 buffer_get_int_ret(&cport, m) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000937 free(reserved);
938 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +1100939 error("%s: malformed message", __func__);
940 return -1;
941 }
Darren Tuckera627d422013-06-02 07:31:17 +1000942 free(reserved);
Damien Millere1537f92010-01-26 13:26:22 +1100943
944 debug2("%s: channel %d: request stdio fwd to %s:%u",
945 __func__, c->self, chost, cport);
946
947 /* Gather fds from client */
948 for(i = 0; i < 2; i++) {
949 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
950 error("%s: failed to receive fd %d from slave",
951 __func__, i);
952 for (j = 0; j < i; j++)
953 close(new_fd[j]);
Darren Tuckera627d422013-06-02 07:31:17 +1000954 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +1100955
956 /* prepare reply */
957 buffer_put_int(r, MUX_S_FAILURE);
958 buffer_put_int(r, rid);
959 buffer_put_cstring(r,
960 "did not receive file descriptors");
961 return -1;
962 }
963 }
964
965 debug3("%s: got fds stdin %d, stdout %d", __func__,
966 new_fd[0], new_fd[1]);
967
968 /* XXX support multiple child sessions in future */
969 if (c->remote_id != -1) {
970 debug2("%s: session already open", __func__);
971 /* prepare reply */
972 buffer_put_int(r, MUX_S_FAILURE);
973 buffer_put_int(r, rid);
974 buffer_put_cstring(r, "Multiple sessions not supported");
975 cleanup:
976 close(new_fd[0]);
977 close(new_fd[1]);
Darren Tuckera627d422013-06-02 07:31:17 +1000978 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +1100979 return 0;
980 }
981
982 if (options.control_master == SSHCTL_MASTER_ASK ||
983 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
Damien Miller68512c02010-10-21 15:21:11 +1100984 if (!ask_permission("Allow forward to %s:%u? ",
Damien Millere1537f92010-01-26 13:26:22 +1100985 chost, cport)) {
986 debug2("%s: stdio fwd refused by user", __func__);
987 /* prepare reply */
988 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
989 buffer_put_int(r, rid);
990 buffer_put_cstring(r, "Permission denied");
991 goto cleanup;
992 }
993 }
994
995 /* enable nonblocking unless tty */
996 if (!isatty(new_fd[0]))
997 set_nonblock(new_fd[0]);
998 if (!isatty(new_fd[1]))
999 set_nonblock(new_fd[1]);
1000
1001 nc = channel_connect_stdio_fwd(chost, cport, new_fd[0], new_fd[1]);
1002
1003 nc->ctl_chan = c->self; /* link session -> control channel */
1004 c->remote_id = nc->self; /* link control -> session channel */
1005
1006 debug2("%s: channel_new: %d linked to control channel %d",
1007 __func__, nc->self, nc->ctl_chan);
1008
Damien Miller85c50d72010-05-10 11:53:02 +10001009 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +11001010
Damien Miller357610d2014-07-18 15:04:10 +10001011 cctx = xcalloc(1, sizeof(*cctx));
1012 cctx->rid = rid;
1013 channel_register_open_confirm(nc->self, mux_stdio_confirm, cctx);
1014 c->mux_pause = 1; /* stop handling messages until open_confirm done */
Damien Millere1537f92010-01-26 13:26:22 +11001015
Damien Miller357610d2014-07-18 15:04:10 +10001016 /* reply is deferred, sent by mux_session_confirm */
Damien Millere1537f92010-01-26 13:26:22 +11001017 return 0;
1018}
1019
Damien Miller357610d2014-07-18 15:04:10 +10001020/* Callback on open confirmation in mux master for a mux stdio fwd session. */
1021static void
1022mux_stdio_confirm(int id, int success, void *arg)
1023{
1024 struct mux_stdio_confirm_ctx *cctx = arg;
1025 Channel *c, *cc;
1026 Buffer reply;
1027
1028 if (cctx == NULL)
1029 fatal("%s: cctx == NULL", __func__);
1030 if ((c = channel_by_id(id)) == NULL)
1031 fatal("%s: no channel for id %d", __func__, id);
1032 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
1033 fatal("%s: channel %d lacks control channel %d", __func__,
1034 id, c->ctl_chan);
1035
1036 if (!success) {
1037 debug3("%s: sending failure reply", __func__);
1038 /* prepare reply */
1039 buffer_init(&reply);
1040 buffer_put_int(&reply, MUX_S_FAILURE);
1041 buffer_put_int(&reply, cctx->rid);
1042 buffer_put_cstring(&reply, "Session open refused by peer");
1043 goto done;
1044 }
1045
1046 debug3("%s: sending success reply", __func__);
1047 /* prepare reply */
1048 buffer_init(&reply);
1049 buffer_put_int(&reply, MUX_S_SESSION_OPENED);
1050 buffer_put_int(&reply, cctx->rid);
1051 buffer_put_int(&reply, c->self);
1052
1053 done:
1054 /* Send reply */
1055 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply));
1056 buffer_free(&reply);
1057
1058 if (cc->mux_pause <= 0)
1059 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1060 cc->mux_pause = 0; /* start processing messages again */
1061 c->open_confirm_ctx = NULL;
1062 free(cctx);
1063}
1064
Damien Miller6c3eec72011-05-05 14:16:22 +10001065static int
1066process_mux_stop_listening(u_int rid, Channel *c, Buffer *m, Buffer *r)
1067{
1068 debug("%s: channel %d: stop listening", __func__, c->self);
1069
1070 if (options.control_master == SSHCTL_MASTER_ASK ||
1071 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
1072 if (!ask_permission("Disable further multiplexing on shared "
1073 "connection to %s? ", host)) {
1074 debug2("%s: stop listen refused by user", __func__);
1075 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
1076 buffer_put_int(r, rid);
1077 buffer_put_cstring(r, "Permission denied");
1078 return 0;
1079 }
1080 }
1081
1082 if (mux_listener_channel != NULL) {
1083 channel_free(mux_listener_channel);
1084 client_stop_mux();
Darren Tuckera627d422013-06-02 07:31:17 +10001085 free(options.control_path);
Damien Miller6c3eec72011-05-05 14:16:22 +10001086 options.control_path = NULL;
1087 mux_listener_channel = NULL;
1088 muxserver_sock = -1;
1089 }
1090
1091 /* prepare reply */
1092 buffer_put_int(r, MUX_S_OK);
1093 buffer_put_int(r, rid);
1094
1095 return 0;
1096}
1097
Damien Millere1537f92010-01-26 13:26:22 +11001098/* Channel callbacks fired on read/write from mux slave fd */
1099static int
1100mux_master_read_cb(Channel *c)
1101{
1102 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
1103 Buffer in, out;
Damien Miller633de332014-05-15 13:48:26 +10001104 const u_char *ptr;
Damien Millere1537f92010-01-26 13:26:22 +11001105 u_int type, rid, have, i;
1106 int ret = -1;
1107
1108 /* Setup ctx and */
1109 if (c->mux_ctx == NULL) {
Damien Millerc094d1e2010-06-26 09:36:34 +10001110 state = xcalloc(1, sizeof(*state));
Damien Millere1537f92010-01-26 13:26:22 +11001111 c->mux_ctx = state;
1112 channel_register_cleanup(c->self,
1113 mux_master_control_cleanup_cb, 0);
1114
1115 /* Send hello */
1116 buffer_init(&out);
1117 buffer_put_int(&out, MUX_MSG_HELLO);
1118 buffer_put_int(&out, SSHMUX_VER);
1119 /* no extensions */
1120 buffer_put_string(&c->output, buffer_ptr(&out),
1121 buffer_len(&out));
1122 buffer_free(&out);
1123 debug3("%s: channel %d: hello sent", __func__, c->self);
1124 return 0;
1125 }
1126
1127 buffer_init(&in);
1128 buffer_init(&out);
1129
1130 /* Channel code ensures that we receive whole packets */
1131 if ((ptr = buffer_get_string_ptr_ret(&c->input, &have)) == NULL) {
1132 malf:
1133 error("%s: malformed message", __func__);
1134 goto out;
1135 }
1136 buffer_append(&in, ptr, have);
1137
1138 if (buffer_get_int_ret(&type, &in) != 0)
1139 goto malf;
1140 debug3("%s: channel %d packet type 0x%08x len %u",
1141 __func__, c->self, type, buffer_len(&in));
1142
1143 if (type == MUX_MSG_HELLO)
1144 rid = 0;
1145 else {
1146 if (!state->hello_rcvd) {
1147 error("%s: expected MUX_MSG_HELLO(0x%08x), "
1148 "received 0x%08x", __func__, MUX_MSG_HELLO, type);
1149 goto out;
1150 }
1151 if (buffer_get_int_ret(&rid, &in) != 0)
1152 goto malf;
1153 }
1154
1155 for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
1156 if (type == mux_master_handlers[i].type) {
1157 ret = mux_master_handlers[i].handler(rid, c, &in, &out);
1158 break;
1159 }
1160 }
1161 if (mux_master_handlers[i].handler == NULL) {
1162 error("%s: unsupported mux message 0x%08x", __func__, type);
1163 buffer_put_int(&out, MUX_S_FAILURE);
1164 buffer_put_int(&out, rid);
1165 buffer_put_cstring(&out, "unsupported request");
1166 ret = 0;
1167 }
1168 /* Enqueue reply packet */
1169 if (buffer_len(&out) != 0) {
1170 buffer_put_string(&c->output, buffer_ptr(&out),
1171 buffer_len(&out));
1172 }
1173 out:
1174 buffer_free(&in);
1175 buffer_free(&out);
1176 return ret;
1177}
1178
1179void
1180mux_exit_message(Channel *c, int exitval)
1181{
1182 Buffer m;
1183 Channel *mux_chan;
1184
Damien Millerbc02f162013-04-23 19:25:49 +10001185 debug3("%s: channel %d: exit message, exitval %d", __func__, c->self,
Damien Millere1537f92010-01-26 13:26:22 +11001186 exitval);
1187
1188 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL)
1189 fatal("%s: channel %d missing mux channel %d",
1190 __func__, c->self, c->ctl_chan);
1191
1192 /* Append exit message packet to control socket output queue */
1193 buffer_init(&m);
1194 buffer_put_int(&m, MUX_S_EXIT_MESSAGE);
1195 buffer_put_int(&m, c->self);
1196 buffer_put_int(&m, exitval);
1197
1198 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m));
1199 buffer_free(&m);
1200}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001201
Damien Miller555f3b82011-05-15 08:48:05 +10001202void
1203mux_tty_alloc_failed(Channel *c)
1204{
1205 Buffer m;
1206 Channel *mux_chan;
1207
1208 debug3("%s: channel %d: TTY alloc failed", __func__, c->self);
1209
1210 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL)
1211 fatal("%s: channel %d missing mux channel %d",
1212 __func__, c->self, c->ctl_chan);
1213
1214 /* Append exit message packet to control socket output queue */
1215 buffer_init(&m);
1216 buffer_put_int(&m, MUX_S_TTY_ALLOC_FAIL);
1217 buffer_put_int(&m, c->self);
1218
1219 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m));
1220 buffer_free(&m);
1221}
1222
Damien Millerb1cbfa22008-05-19 16:00:08 +10001223/* Prepare a mux master to listen on a Unix domain socket. */
1224void
1225muxserver_listen(void)
1226{
Damien Millerb1cbfa22008-05-19 16:00:08 +10001227 mode_t old_umask;
Damien Miller603134e2010-09-24 22:07:55 +10001228 char *orig_control_path = options.control_path;
1229 char rbuf[16+1];
1230 u_int i, r;
Damien Millerf42f7682014-07-18 15:03:27 +10001231 int oerrno;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001232
1233 if (options.control_path == NULL ||
1234 options.control_master == SSHCTL_MASTER_NO)
1235 return;
1236
1237 debug("setting up multiplex master socket");
1238
Damien Miller603134e2010-09-24 22:07:55 +10001239 /*
1240 * Use a temporary path before listen so we can pseudo-atomically
1241 * establish the listening socket in its final location to avoid
1242 * other processes racing in between bind() and listen() and hitting
1243 * an unready socket.
1244 */
1245 for (i = 0; i < sizeof(rbuf) - 1; i++) {
1246 r = arc4random_uniform(26+26+10);
1247 rbuf[i] = (r < 26) ? 'a' + r :
1248 (r < 26*2) ? 'A' + r - 26 :
1249 '0' + r - 26 - 26;
1250 }
1251 rbuf[sizeof(rbuf) - 1] = '\0';
1252 options.control_path = NULL;
1253 xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
1254 debug3("%s: temporary control path %s", __func__, options.control_path);
1255
Damien Millerb1cbfa22008-05-19 16:00:08 +10001256 old_umask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001257 muxserver_sock = unix_listener(options.control_path, 64, 0);
Damien Millerf42f7682014-07-18 15:03:27 +10001258 oerrno = errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10001259 umask(old_umask);
1260 if (muxserver_sock < 0) {
Damien Millerf42f7682014-07-18 15:03:27 +10001261 if (oerrno == EINVAL || oerrno == EADDRINUSE) {
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001262 error("ControlSocket %s already exists, "
1263 "disabling multiplexing", options.control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001264 disable_mux_master:
Damien Miller60432d82011-05-15 08:34:46 +10001265 if (muxserver_sock != -1) {
1266 close(muxserver_sock);
1267 muxserver_sock = -1;
1268 }
Darren Tuckera627d422013-06-02 07:31:17 +10001269 free(orig_control_path);
1270 free(options.control_path);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001271 options.control_path = NULL;
1272 options.control_master = SSHCTL_MASTER_NO;
1273 return;
Damien Miller7acefbb2014-07-18 14:11:24 +10001274 } else {
1275 /* unix_listener() logs the error */
1276 cleanup_exit(255);
1277 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001278 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001279
Damien Miller603134e2010-09-24 22:07:55 +10001280 /* Now atomically "move" the mux socket into position */
1281 if (link(options.control_path, orig_control_path) != 0) {
1282 if (errno != EEXIST) {
1283 fatal("%s: link mux listener %s => %s: %s", __func__,
1284 options.control_path, orig_control_path,
1285 strerror(errno));
1286 }
1287 error("ControlSocket %s already exists, disabling multiplexing",
1288 orig_control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001289 unlink(options.control_path);
1290 goto disable_mux_master;
1291 }
1292 unlink(options.control_path);
Darren Tuckera627d422013-06-02 07:31:17 +10001293 free(options.control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001294 options.control_path = orig_control_path;
1295
Damien Millerb1cbfa22008-05-19 16:00:08 +10001296 set_nonblock(muxserver_sock);
Damien Millere1537f92010-01-26 13:26:22 +11001297
1298 mux_listener_channel = channel_new("mux listener",
1299 SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
1300 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller603134e2010-09-24 22:07:55 +10001301 0, options.control_path, 1);
Damien Millere1537f92010-01-26 13:26:22 +11001302 mux_listener_channel->mux_rcb = mux_master_read_cb;
1303 debug3("%s: mux listener channel %d fd %d", __func__,
1304 mux_listener_channel->self, mux_listener_channel->sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001305}
1306
1307/* Callback on open confirmation in mux master for a mux client session. */
1308static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001309mux_session_confirm(int id, int success, void *arg)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001310{
1311 struct mux_session_confirm_ctx *cctx = arg;
1312 const char *display;
Damien Millerd530f5f2010-05-21 14:57:10 +10001313 Channel *c, *cc;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001314 int i;
Damien Millerd530f5f2010-05-21 14:57:10 +10001315 Buffer reply;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001316
1317 if (cctx == NULL)
1318 fatal("%s: cctx == NULL", __func__);
Damien Millere1537f92010-01-26 13:26:22 +11001319 if ((c = channel_by_id(id)) == NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001320 fatal("%s: no channel for id %d", __func__, id);
Damien Millerd530f5f2010-05-21 14:57:10 +10001321 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
1322 fatal("%s: channel %d lacks control channel %d", __func__,
1323 id, c->ctl_chan);
1324
1325 if (!success) {
1326 debug3("%s: sending failure reply", __func__);
1327 /* prepare reply */
1328 buffer_init(&reply);
1329 buffer_put_int(&reply, MUX_S_FAILURE);
1330 buffer_put_int(&reply, cctx->rid);
1331 buffer_put_cstring(&reply, "Session open refused by peer");
1332 goto done;
1333 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001334
1335 display = getenv("DISPLAY");
1336 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1337 char *proto, *data;
Damien Miller1ab6a512010-06-26 10:02:24 +10001338
Damien Millerb1cbfa22008-05-19 16:00:08 +10001339 /* Get reasonable local authentication information. */
1340 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001341 options.forward_x11_trusted, options.forward_x11_timeout,
1342 &proto, &data);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001343 /* Request forwarding with authentication spoofing. */
Damien Miller1ab6a512010-06-26 10:02:24 +10001344 debug("Requesting X11 forwarding with authentication "
1345 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001346 x11_request_forwarding_with_spoofing(id, display, proto,
1347 data, 1);
1348 client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
1349 /* XXX exit_on_forward_failure */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001350 }
1351
1352 if (cctx->want_agent_fwd && options.forward_agent) {
1353 debug("Requesting authentication agent forwarding.");
1354 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1355 packet_send();
1356 }
1357
1358 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
1359 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
1360
Damien Millerd530f5f2010-05-21 14:57:10 +10001361 debug3("%s: sending success reply", __func__);
1362 /* prepare reply */
1363 buffer_init(&reply);
1364 buffer_put_int(&reply, MUX_S_SESSION_OPENED);
1365 buffer_put_int(&reply, cctx->rid);
1366 buffer_put_int(&reply, c->self);
1367
1368 done:
1369 /* Send reply */
1370 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply));
1371 buffer_free(&reply);
1372
1373 if (cc->mux_pause <= 0)
1374 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1375 cc->mux_pause = 0; /* start processing messages again */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001376 c->open_confirm_ctx = NULL;
1377 buffer_free(&cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +10001378 free(cctx->term);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001379 if (cctx->env != NULL) {
1380 for (i = 0; cctx->env[i] != NULL; i++)
Darren Tuckera627d422013-06-02 07:31:17 +10001381 free(cctx->env[i]);
1382 free(cctx->env);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001383 }
Darren Tuckera627d422013-06-02 07:31:17 +10001384 free(cctx);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001385}
1386
Damien Millerb1cbfa22008-05-19 16:00:08 +10001387/* ** Multiplexing client support */
1388
1389/* Exit signal handler */
1390static void
1391control_client_sighandler(int signo)
1392{
1393 muxclient_terminate = signo;
1394}
1395
1396/*
1397 * Relay signal handler - used to pass some signals from mux client to
1398 * mux master.
1399 */
1400static void
1401control_client_sigrelay(int signo)
1402{
1403 int save_errno = errno;
1404
1405 if (muxserver_pid > 1)
1406 kill(muxserver_pid, signo);
1407
1408 errno = save_errno;
1409}
1410
Damien Millerb1cbfa22008-05-19 16:00:08 +10001411static int
Damien Millere1537f92010-01-26 13:26:22 +11001412mux_client_read(int fd, Buffer *b, u_int need)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001413{
Damien Millere1537f92010-01-26 13:26:22 +11001414 u_int have;
1415 ssize_t len;
1416 u_char *p;
1417 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001418
Damien Millere1537f92010-01-26 13:26:22 +11001419 pfd.fd = fd;
1420 pfd.events = POLLIN;
1421 p = buffer_append_space(b, need);
1422 for (have = 0; have < need; ) {
1423 if (muxclient_terminate) {
1424 errno = EINTR;
1425 return -1;
1426 }
1427 len = read(fd, p + have, need - have);
1428 if (len < 0) {
1429 switch (errno) {
1430#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1431 case EWOULDBLOCK:
1432#endif
1433 case EAGAIN:
1434 (void)poll(&pfd, 1, -1);
1435 /* FALLTHROUGH */
1436 case EINTR:
1437 continue;
1438 default:
1439 return -1;
1440 }
1441 }
1442 if (len == 0) {
1443 errno = EPIPE;
1444 return -1;
1445 }
1446 have += (u_int)len;
1447 }
1448 return 0;
1449}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001450
Damien Millere1537f92010-01-26 13:26:22 +11001451static int
1452mux_client_write_packet(int fd, Buffer *m)
1453{
1454 Buffer queue;
1455 u_int have, need;
1456 int oerrno, len;
1457 u_char *ptr;
1458 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001459
Damien Millere1537f92010-01-26 13:26:22 +11001460 pfd.fd = fd;
1461 pfd.events = POLLOUT;
1462 buffer_init(&queue);
1463 buffer_put_string(&queue, buffer_ptr(m), buffer_len(m));
1464
1465 need = buffer_len(&queue);
1466 ptr = buffer_ptr(&queue);
1467
1468 for (have = 0; have < need; ) {
1469 if (muxclient_terminate) {
1470 buffer_free(&queue);
1471 errno = EINTR;
1472 return -1;
1473 }
1474 len = write(fd, ptr + have, need - have);
1475 if (len < 0) {
1476 switch (errno) {
1477#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1478 case EWOULDBLOCK:
1479#endif
1480 case EAGAIN:
1481 (void)poll(&pfd, 1, -1);
1482 /* FALLTHROUGH */
1483 case EINTR:
1484 continue;
1485 default:
1486 oerrno = errno;
1487 buffer_free(&queue);
1488 errno = oerrno;
1489 return -1;
1490 }
1491 }
1492 if (len == 0) {
1493 buffer_free(&queue);
1494 errno = EPIPE;
1495 return -1;
1496 }
1497 have += (u_int)len;
1498 }
1499 buffer_free(&queue);
1500 return 0;
1501}
1502
1503static int
1504mux_client_read_packet(int fd, Buffer *m)
1505{
1506 Buffer queue;
1507 u_int need, have;
Damien Miller633de332014-05-15 13:48:26 +10001508 const u_char *ptr;
Damien Millere1537f92010-01-26 13:26:22 +11001509 int oerrno;
1510
1511 buffer_init(&queue);
1512 if (mux_client_read(fd, &queue, 4) != 0) {
1513 if ((oerrno = errno) == EPIPE)
Darren Tucker746e9062013-06-06 08:20:13 +10001514 debug3("%s: read header failed: %s", __func__,
1515 strerror(errno));
1516 buffer_free(&queue);
Damien Millere1537f92010-01-26 13:26:22 +11001517 errno = oerrno;
1518 return -1;
1519 }
1520 need = get_u32(buffer_ptr(&queue));
1521 if (mux_client_read(fd, &queue, need) != 0) {
1522 oerrno = errno;
1523 debug3("%s: read body failed: %s", __func__, strerror(errno));
Darren Tucker746e9062013-06-06 08:20:13 +10001524 buffer_free(&queue);
Damien Millere1537f92010-01-26 13:26:22 +11001525 errno = oerrno;
1526 return -1;
1527 }
1528 ptr = buffer_get_string_ptr(&queue, &have);
1529 buffer_append(m, ptr, have);
1530 buffer_free(&queue);
1531 return 0;
1532}
1533
1534static int
1535mux_client_hello_exchange(int fd)
1536{
1537 Buffer m;
1538 u_int type, ver;
1539
1540 buffer_init(&m);
1541 buffer_put_int(&m, MUX_MSG_HELLO);
1542 buffer_put_int(&m, SSHMUX_VER);
1543 /* no extensions */
1544
1545 if (mux_client_write_packet(fd, &m) != 0)
1546 fatal("%s: write packet: %s", __func__, strerror(errno));
1547
1548 buffer_clear(&m);
1549
1550 /* Read their HELLO */
1551 if (mux_client_read_packet(fd, &m) != 0) {
1552 buffer_free(&m);
1553 return -1;
1554 }
1555
1556 type = buffer_get_int(&m);
1557 if (type != MUX_MSG_HELLO)
1558 fatal("%s: expected HELLO (%u) received %u",
1559 __func__, MUX_MSG_HELLO, type);
1560 ver = buffer_get_int(&m);
1561 if (ver != SSHMUX_VER)
1562 fatal("Unsupported multiplexing protocol version %d "
1563 "(expected %d)", ver, SSHMUX_VER);
1564 debug2("%s: master version %u", __func__, ver);
1565 /* No extensions are presently defined */
1566 while (buffer_len(&m) > 0) {
1567 char *name = buffer_get_string(&m, NULL);
1568 char *value = buffer_get_string(&m, NULL);
1569
1570 debug2("Unrecognised master extension \"%s\"", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001571 free(name);
1572 free(value);
Damien Millere1537f92010-01-26 13:26:22 +11001573 }
1574 buffer_free(&m);
1575 return 0;
1576}
1577
1578static u_int
1579mux_client_request_alive(int fd)
1580{
1581 Buffer m;
1582 char *e;
1583 u_int pid, type, rid;
1584
1585 debug3("%s: entering", __func__);
1586
1587 buffer_init(&m);
1588 buffer_put_int(&m, MUX_C_ALIVE_CHECK);
1589 buffer_put_int(&m, muxclient_request_id);
1590
1591 if (mux_client_write_packet(fd, &m) != 0)
1592 fatal("%s: write packet: %s", __func__, strerror(errno));
1593
1594 buffer_clear(&m);
1595
1596 /* Read their reply */
1597 if (mux_client_read_packet(fd, &m) != 0) {
1598 buffer_free(&m);
1599 return 0;
1600 }
1601
1602 type = buffer_get_int(&m);
1603 if (type != MUX_S_ALIVE) {
1604 e = buffer_get_string(&m, NULL);
1605 fatal("%s: master returned error: %s", __func__, e);
1606 }
1607
1608 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1609 fatal("%s: out of sequence reply: my id %u theirs %u",
1610 __func__, muxclient_request_id, rid);
1611 pid = buffer_get_int(&m);
1612 buffer_free(&m);
1613
1614 debug3("%s: done pid = %u", __func__, pid);
1615
1616 muxclient_request_id++;
1617
1618 return pid;
1619}
1620
1621static void
1622mux_client_request_terminate(int fd)
1623{
1624 Buffer m;
1625 char *e;
1626 u_int type, rid;
1627
1628 debug3("%s: entering", __func__);
1629
1630 buffer_init(&m);
1631 buffer_put_int(&m, MUX_C_TERMINATE);
1632 buffer_put_int(&m, muxclient_request_id);
1633
1634 if (mux_client_write_packet(fd, &m) != 0)
1635 fatal("%s: write packet: %s", __func__, strerror(errno));
1636
1637 buffer_clear(&m);
1638
1639 /* Read their reply */
1640 if (mux_client_read_packet(fd, &m) != 0) {
1641 /* Remote end exited already */
1642 if (errno == EPIPE) {
1643 buffer_free(&m);
1644 return;
1645 }
1646 fatal("%s: read from master failed: %s",
1647 __func__, strerror(errno));
1648 }
1649
1650 type = buffer_get_int(&m);
1651 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1652 fatal("%s: out of sequence reply: my id %u theirs %u",
1653 __func__, muxclient_request_id, rid);
1654 switch (type) {
1655 case MUX_S_OK:
1656 break;
1657 case MUX_S_PERMISSION_DENIED:
1658 e = buffer_get_string(&m, NULL);
1659 fatal("Master refused termination request: %s", e);
1660 case MUX_S_FAILURE:
1661 e = buffer_get_string(&m, NULL);
1662 fatal("%s: termination request failed: %s", __func__, e);
1663 default:
1664 fatal("%s: unexpected response from master 0x%08x",
1665 __func__, type);
1666 }
1667 buffer_free(&m);
1668 muxclient_request_id++;
1669}
1670
1671static int
Damien Miller7acefbb2014-07-18 14:11:24 +10001672mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd)
Damien Millere1537f92010-01-26 13:26:22 +11001673{
1674 Buffer m;
1675 char *e, *fwd_desc;
1676 u_int type, rid;
1677
1678 fwd_desc = format_forward(ftype, fwd);
Damien Millerf6dff7c2011-09-22 21:38:52 +10001679 debug("Requesting %s %s",
1680 cancel_flag ? "cancellation of" : "forwarding of", fwd_desc);
Darren Tuckera627d422013-06-02 07:31:17 +10001681 free(fwd_desc);
Damien Millere1537f92010-01-26 13:26:22 +11001682
1683 buffer_init(&m);
Damien Millerf6dff7c2011-09-22 21:38:52 +10001684 buffer_put_int(&m, cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD);
Damien Millere1537f92010-01-26 13:26:22 +11001685 buffer_put_int(&m, muxclient_request_id);
1686 buffer_put_int(&m, ftype);
Damien Miller7acefbb2014-07-18 14:11:24 +10001687 if (fwd->listen_path != NULL) {
1688 buffer_put_cstring(&m, fwd->listen_path);
1689 } else {
1690 buffer_put_cstring(&m,
djm@openbsd.org46ac2ed2014-12-22 07:24:11 +00001691 fwd->listen_host == NULL ? "" :
1692 (*fwd->listen_host == '\0' ? "*" : fwd->listen_host));
Damien Miller7acefbb2014-07-18 14:11:24 +10001693 }
Damien Millere1537f92010-01-26 13:26:22 +11001694 buffer_put_int(&m, fwd->listen_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10001695 if (fwd->connect_path != NULL) {
1696 buffer_put_cstring(&m, fwd->connect_path);
1697 } else {
1698 buffer_put_cstring(&m,
1699 fwd->connect_host == NULL ? "" : fwd->connect_host);
1700 }
Damien Millere1537f92010-01-26 13:26:22 +11001701 buffer_put_int(&m, fwd->connect_port);
1702
1703 if (mux_client_write_packet(fd, &m) != 0)
1704 fatal("%s: write packet: %s", __func__, strerror(errno));
1705
1706 buffer_clear(&m);
1707
1708 /* Read their reply */
1709 if (mux_client_read_packet(fd, &m) != 0) {
1710 buffer_free(&m);
1711 return -1;
1712 }
1713
1714 type = buffer_get_int(&m);
1715 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1716 fatal("%s: out of sequence reply: my id %u theirs %u",
1717 __func__, muxclient_request_id, rid);
1718 switch (type) {
1719 case MUX_S_OK:
1720 break;
Damien Miller388f6fc2010-05-21 14:57:35 +10001721 case MUX_S_REMOTE_PORT:
Damien Millerf6dff7c2011-09-22 21:38:52 +10001722 if (cancel_flag)
1723 fatal("%s: got MUX_S_REMOTE_PORT for cancel", __func__);
Damien Miller388f6fc2010-05-21 14:57:35 +10001724 fwd->allocated_port = buffer_get_int(&m);
1725 logit("Allocated port %u for remote forward to %s:%d",
1726 fwd->allocated_port,
1727 fwd->connect_host ? fwd->connect_host : "",
1728 fwd->connect_port);
1729 if (muxclient_command == SSHMUX_COMMAND_FORWARD)
1730 fprintf(stdout, "%u\n", fwd->allocated_port);
1731 break;
Damien Millere1537f92010-01-26 13:26:22 +11001732 case MUX_S_PERMISSION_DENIED:
1733 e = buffer_get_string(&m, NULL);
1734 buffer_free(&m);
1735 error("Master refused forwarding request: %s", e);
1736 return -1;
1737 case MUX_S_FAILURE:
1738 e = buffer_get_string(&m, NULL);
1739 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001740 error("%s: forwarding request failed: %s", __func__, e);
Damien Millere1537f92010-01-26 13:26:22 +11001741 return -1;
1742 default:
1743 fatal("%s: unexpected response from master 0x%08x",
1744 __func__, type);
1745 }
1746 buffer_free(&m);
1747
1748 muxclient_request_id++;
1749 return 0;
1750}
1751
1752static int
Damien Millerf6dff7c2011-09-22 21:38:52 +10001753mux_client_forwards(int fd, int cancel_flag)
Damien Millere1537f92010-01-26 13:26:22 +11001754{
Damien Millerf6dff7c2011-09-22 21:38:52 +10001755 int i, ret = 0;
Damien Millere1537f92010-01-26 13:26:22 +11001756
Damien Millerf6dff7c2011-09-22 21:38:52 +10001757 debug3("%s: %s forwardings: %d local, %d remote", __func__,
1758 cancel_flag ? "cancel" : "request",
Damien Millere1537f92010-01-26 13:26:22 +11001759 options.num_local_forwards, options.num_remote_forwards);
1760
1761 /* XXX ExitOnForwardingFailure */
1762 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10001763 if (mux_client_forward(fd, cancel_flag,
Damien Millere1537f92010-01-26 13:26:22 +11001764 options.local_forwards[i].connect_port == 0 ?
1765 MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
1766 options.local_forwards + i) != 0)
Damien Millerf6dff7c2011-09-22 21:38:52 +10001767 ret = -1;
Damien Millere1537f92010-01-26 13:26:22 +11001768 }
1769 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10001770 if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE,
Damien Millere1537f92010-01-26 13:26:22 +11001771 options.remote_forwards + i) != 0)
Damien Millerf6dff7c2011-09-22 21:38:52 +10001772 ret = -1;
Damien Millere1537f92010-01-26 13:26:22 +11001773 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10001774 return ret;
Damien Millere1537f92010-01-26 13:26:22 +11001775}
1776
1777static int
1778mux_client_request_session(int fd)
1779{
1780 Buffer m;
1781 char *e, *term;
1782 u_int i, rid, sid, esid, exitval, type, exitval_seen;
1783 extern char **environ;
Damien Miller555f3b82011-05-15 08:48:05 +10001784 int devnull, rawmode;
Damien Millere1537f92010-01-26 13:26:22 +11001785
1786 debug3("%s: entering", __func__);
1787
1788 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1789 error("%s: master alive request failed", __func__);
1790 return -1;
1791 }
1792
1793 signal(SIGPIPE, SIG_IGN);
1794
1795 if (stdin_null_flag) {
1796 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1797 fatal("open(/dev/null): %s", strerror(errno));
1798 if (dup2(devnull, STDIN_FILENO) == -1)
1799 fatal("dup2: %s", strerror(errno));
1800 if (devnull > STDERR_FILENO)
1801 close(devnull);
1802 }
1803
1804 term = getenv("TERM");
1805
1806 buffer_init(&m);
1807 buffer_put_int(&m, MUX_C_NEW_SESSION);
1808 buffer_put_int(&m, muxclient_request_id);
1809 buffer_put_cstring(&m, ""); /* reserved */
1810 buffer_put_int(&m, tty_flag);
1811 buffer_put_int(&m, options.forward_x11);
1812 buffer_put_int(&m, options.forward_agent);
1813 buffer_put_int(&m, subsystem_flag);
1814 buffer_put_int(&m, options.escape_char == SSH_ESCAPECHAR_NONE ?
1815 0xffffffff : (u_int)options.escape_char);
1816 buffer_put_cstring(&m, term == NULL ? "" : term);
1817 buffer_put_string(&m, buffer_ptr(&command), buffer_len(&command));
1818
1819 if (options.num_send_env > 0 && environ != NULL) {
1820 /* Pass environment */
1821 for (i = 0; environ[i] != NULL; i++) {
1822 if (env_permitted(environ[i])) {
1823 buffer_put_cstring(&m, environ[i]);
1824 }
1825 }
1826 }
1827
1828 if (mux_client_write_packet(fd, &m) != 0)
1829 fatal("%s: write packet: %s", __func__, strerror(errno));
1830
1831 /* Send the stdio file descriptors */
1832 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1833 mm_send_fd(fd, STDOUT_FILENO) == -1 ||
1834 mm_send_fd(fd, STDERR_FILENO) == -1)
1835 fatal("%s: send fds failed", __func__);
1836
1837 debug3("%s: session request sent", __func__);
1838
1839 /* Read their reply */
1840 buffer_clear(&m);
1841 if (mux_client_read_packet(fd, &m) != 0) {
1842 error("%s: read from master failed: %s",
1843 __func__, strerror(errno));
1844 buffer_free(&m);
1845 return -1;
1846 }
1847
1848 type = buffer_get_int(&m);
1849 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1850 fatal("%s: out of sequence reply: my id %u theirs %u",
1851 __func__, muxclient_request_id, rid);
1852 switch (type) {
1853 case MUX_S_SESSION_OPENED:
1854 sid = buffer_get_int(&m);
1855 debug("%s: master session id: %u", __func__, sid);
1856 break;
1857 case MUX_S_PERMISSION_DENIED:
1858 e = buffer_get_string(&m, NULL);
1859 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001860 error("Master refused session request: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11001861 return -1;
1862 case MUX_S_FAILURE:
1863 e = buffer_get_string(&m, NULL);
1864 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001865 error("%s: session request failed: %s", __func__, e);
Damien Millere1537f92010-01-26 13:26:22 +11001866 return -1;
1867 default:
1868 buffer_free(&m);
1869 error("%s: unexpected response from master 0x%08x",
1870 __func__, type);
1871 return -1;
1872 }
1873 muxclient_request_id++;
1874
1875 signal(SIGHUP, control_client_sighandler);
1876 signal(SIGINT, control_client_sighandler);
1877 signal(SIGTERM, control_client_sighandler);
1878 signal(SIGWINCH, control_client_sigrelay);
1879
Damien Miller555f3b82011-05-15 08:48:05 +10001880 rawmode = tty_flag;
Damien Millere1537f92010-01-26 13:26:22 +11001881 if (tty_flag)
Damien Miller21771e22011-05-15 08:45:50 +10001882 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millere1537f92010-01-26 13:26:22 +11001883
1884 /*
1885 * Stick around until the controlee closes the client_fd.
1886 * Before it does, it is expected to write an exit message.
1887 * This process must read the value and wait for the closure of
1888 * the client_fd; if this one closes early, the multiplex master will
1889 * terminate early too (possibly losing data).
1890 */
1891 for (exitval = 255, exitval_seen = 0;;) {
1892 buffer_clear(&m);
1893 if (mux_client_read_packet(fd, &m) != 0)
1894 break;
1895 type = buffer_get_int(&m);
Damien Miller555f3b82011-05-15 08:48:05 +10001896 switch (type) {
1897 case MUX_S_TTY_ALLOC_FAIL:
1898 if ((esid = buffer_get_int(&m)) != sid)
1899 fatal("%s: tty alloc fail on unknown session: "
1900 "my id %u theirs %u",
1901 __func__, sid, esid);
1902 leave_raw_mode(options.request_tty ==
1903 REQUEST_TTY_FORCE);
1904 rawmode = 0;
1905 continue;
1906 case MUX_S_EXIT_MESSAGE:
1907 if ((esid = buffer_get_int(&m)) != sid)
1908 fatal("%s: exit on unknown session: "
1909 "my id %u theirs %u",
1910 __func__, sid, esid);
1911 if (exitval_seen)
1912 fatal("%s: exitval sent twice", __func__);
1913 exitval = buffer_get_int(&m);
1914 exitval_seen = 1;
1915 continue;
1916 default:
Damien Millere1537f92010-01-26 13:26:22 +11001917 e = buffer_get_string(&m, NULL);
1918 fatal("%s: master returned error: %s", __func__, e);
1919 }
Damien Millere1537f92010-01-26 13:26:22 +11001920 }
1921
1922 close(fd);
Damien Miller555f3b82011-05-15 08:48:05 +10001923 if (rawmode)
1924 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millere1537f92010-01-26 13:26:22 +11001925
1926 if (muxclient_terminate) {
1927 debug2("Exiting on signal %d", muxclient_terminate);
1928 exitval = 255;
1929 } else if (!exitval_seen) {
1930 debug2("Control master terminated unexpectedly");
1931 exitval = 255;
1932 } else
1933 debug2("Received exit status from master %d", exitval);
1934
1935 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1936 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
1937
1938 exit(exitval);
1939}
1940
1941static int
1942mux_client_request_stdio_fwd(int fd)
1943{
1944 Buffer m;
1945 char *e;
1946 u_int type, rid, sid;
1947 int devnull;
1948
1949 debug3("%s: entering", __func__);
1950
1951 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1952 error("%s: master alive request failed", __func__);
1953 return -1;
1954 }
1955
1956 signal(SIGPIPE, SIG_IGN);
1957
1958 if (stdin_null_flag) {
1959 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1960 fatal("open(/dev/null): %s", strerror(errno));
1961 if (dup2(devnull, STDIN_FILENO) == -1)
1962 fatal("dup2: %s", strerror(errno));
1963 if (devnull > STDERR_FILENO)
1964 close(devnull);
1965 }
1966
1967 buffer_init(&m);
1968 buffer_put_int(&m, MUX_C_NEW_STDIO_FWD);
1969 buffer_put_int(&m, muxclient_request_id);
1970 buffer_put_cstring(&m, ""); /* reserved */
1971 buffer_put_cstring(&m, stdio_forward_host);
1972 buffer_put_int(&m, stdio_forward_port);
1973
1974 if (mux_client_write_packet(fd, &m) != 0)
1975 fatal("%s: write packet: %s", __func__, strerror(errno));
1976
1977 /* Send the stdio file descriptors */
1978 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1979 mm_send_fd(fd, STDOUT_FILENO) == -1)
1980 fatal("%s: send fds failed", __func__);
1981
1982 debug3("%s: stdio forward request sent", __func__);
1983
1984 /* Read their reply */
1985 buffer_clear(&m);
1986
1987 if (mux_client_read_packet(fd, &m) != 0) {
1988 error("%s: read from master failed: %s",
1989 __func__, strerror(errno));
1990 buffer_free(&m);
1991 return -1;
1992 }
1993
1994 type = buffer_get_int(&m);
1995 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1996 fatal("%s: out of sequence reply: my id %u theirs %u",
1997 __func__, muxclient_request_id, rid);
1998 switch (type) {
1999 case MUX_S_SESSION_OPENED:
2000 sid = buffer_get_int(&m);
2001 debug("%s: master session id: %u", __func__, sid);
2002 break;
2003 case MUX_S_PERMISSION_DENIED:
2004 e = buffer_get_string(&m, NULL);
2005 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11002006 fatal("Master refused stdio forwarding request: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11002007 case MUX_S_FAILURE:
2008 e = buffer_get_string(&m, NULL);
2009 buffer_free(&m);
Damien Miller357610d2014-07-18 15:04:10 +10002010 fatal("Stdio forwarding request failed: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11002011 default:
2012 buffer_free(&m);
2013 error("%s: unexpected response from master 0x%08x",
2014 __func__, type);
2015 return -1;
2016 }
2017 muxclient_request_id++;
2018
2019 signal(SIGHUP, control_client_sighandler);
2020 signal(SIGINT, control_client_sighandler);
2021 signal(SIGTERM, control_client_sighandler);
2022 signal(SIGWINCH, control_client_sigrelay);
2023
2024 /*
2025 * Stick around until the controlee closes the client_fd.
2026 */
2027 buffer_clear(&m);
2028 if (mux_client_read_packet(fd, &m) != 0) {
2029 if (errno == EPIPE ||
2030 (errno == EINTR && muxclient_terminate != 0))
2031 return 0;
2032 fatal("%s: mux_client_read_packet: %s",
2033 __func__, strerror(errno));
2034 }
2035 fatal("%s: master returned unexpected message %u", __func__, type);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002036}
2037
Damien Miller6c3eec72011-05-05 14:16:22 +10002038static void
2039mux_client_request_stop_listening(int fd)
2040{
2041 Buffer m;
2042 char *e;
2043 u_int type, rid;
2044
2045 debug3("%s: entering", __func__);
2046
2047 buffer_init(&m);
2048 buffer_put_int(&m, MUX_C_STOP_LISTENING);
2049 buffer_put_int(&m, muxclient_request_id);
2050
2051 if (mux_client_write_packet(fd, &m) != 0)
2052 fatal("%s: write packet: %s", __func__, strerror(errno));
2053
2054 buffer_clear(&m);
2055
2056 /* Read their reply */
2057 if (mux_client_read_packet(fd, &m) != 0)
2058 fatal("%s: read from master failed: %s",
2059 __func__, strerror(errno));
2060
2061 type = buffer_get_int(&m);
2062 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
2063 fatal("%s: out of sequence reply: my id %u theirs %u",
2064 __func__, muxclient_request_id, rid);
2065 switch (type) {
2066 case MUX_S_OK:
2067 break;
2068 case MUX_S_PERMISSION_DENIED:
2069 e = buffer_get_string(&m, NULL);
2070 fatal("Master refused stop listening request: %s", e);
2071 case MUX_S_FAILURE:
2072 e = buffer_get_string(&m, NULL);
2073 fatal("%s: stop listening request failed: %s", __func__, e);
2074 default:
2075 fatal("%s: unexpected response from master 0x%08x",
2076 __func__, type);
2077 }
2078 buffer_free(&m);
2079 muxclient_request_id++;
2080}
2081
Damien Millerb1cbfa22008-05-19 16:00:08 +10002082/* Multiplex client main loop. */
2083void
2084muxclient(const char *path)
2085{
2086 struct sockaddr_un addr;
Damien Millere1537f92010-01-26 13:26:22 +11002087 socklen_t sun_len;
2088 int sock;
2089 u_int pid;
Damien Millerb1cbfa22008-05-19 16:00:08 +10002090
Damien Millere1537f92010-01-26 13:26:22 +11002091 if (muxclient_command == 0) {
2092 if (stdio_forward_host != NULL)
2093 muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
2094 else
2095 muxclient_command = SSHMUX_COMMAND_OPEN;
2096 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002097
2098 switch (options.control_master) {
2099 case SSHCTL_MASTER_AUTO:
2100 case SSHCTL_MASTER_AUTO_ASK:
2101 debug("auto-mux: Trying existing master");
2102 /* FALLTHROUGH */
2103 case SSHCTL_MASTER_NO:
2104 break;
2105 default:
2106 return;
2107 }
2108
2109 memset(&addr, '\0', sizeof(addr));
2110 addr.sun_family = AF_UNIX;
Damien Millere1537f92010-01-26 13:26:22 +11002111 sun_len = offsetof(struct sockaddr_un, sun_path) +
Damien Millerb1cbfa22008-05-19 16:00:08 +10002112 strlen(path) + 1;
2113
2114 if (strlcpy(addr.sun_path, path,
2115 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
2116 fatal("ControlPath too long");
2117
2118 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
2119 fatal("%s socket(): %s", __func__, strerror(errno));
2120
Damien Millere1537f92010-01-26 13:26:22 +11002121 if (connect(sock, (struct sockaddr *)&addr, sun_len) == -1) {
2122 switch (muxclient_command) {
2123 case SSHMUX_COMMAND_OPEN:
2124 case SSHMUX_COMMAND_STDIO_FWD:
2125 break;
2126 default:
Damien Millerb1cbfa22008-05-19 16:00:08 +10002127 fatal("Control socket connect(%.100s): %s", path,
2128 strerror(errno));
2129 }
Damien Miller603134e2010-09-24 22:07:55 +10002130 if (errno == ECONNREFUSED &&
2131 options.control_master != SSHCTL_MASTER_NO) {
2132 debug("Stale control socket %.100s, unlinking", path);
2133 unlink(path);
2134 } else if (errno == ENOENT) {
Damien Millerb1cbfa22008-05-19 16:00:08 +10002135 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller603134e2010-09-24 22:07:55 +10002136 } else {
Damien Millerb1cbfa22008-05-19 16:00:08 +10002137 error("Control socket connect(%.100s): %s", path,
2138 strerror(errno));
2139 }
2140 close(sock);
2141 return;
2142 }
Damien Millere1537f92010-01-26 13:26:22 +11002143 set_nonblock(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002144
Damien Millere1537f92010-01-26 13:26:22 +11002145 if (mux_client_hello_exchange(sock) != 0) {
2146 error("%s: master hello exchange failed", __func__);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10002147 close(sock);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10002148 return;
2149 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002150
2151 switch (muxclient_command) {
2152 case SSHMUX_COMMAND_ALIVE_CHECK:
Damien Millere1537f92010-01-26 13:26:22 +11002153 if ((pid = mux_client_request_alive(sock)) == 0)
2154 fatal("%s: master alive check failed", __func__);
2155 fprintf(stderr, "Master running (pid=%d)\r\n", pid);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002156 exit(0);
2157 case SSHMUX_COMMAND_TERMINATE:
Damien Millere1537f92010-01-26 13:26:22 +11002158 mux_client_request_terminate(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002159 fprintf(stderr, "Exit request sent.\r\n");
2160 exit(0);
Damien Miller388f6fc2010-05-21 14:57:35 +10002161 case SSHMUX_COMMAND_FORWARD:
Damien Millerf6dff7c2011-09-22 21:38:52 +10002162 if (mux_client_forwards(sock, 0) != 0)
Damien Miller388f6fc2010-05-21 14:57:35 +10002163 fatal("%s: master forward request failed", __func__);
2164 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002165 case SSHMUX_COMMAND_OPEN:
Damien Millerf6dff7c2011-09-22 21:38:52 +10002166 if (mux_client_forwards(sock, 0) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11002167 error("%s: master forward request failed", __func__);
2168 return;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10002169 }
Damien Millere1537f92010-01-26 13:26:22 +11002170 mux_client_request_session(sock);
2171 return;
2172 case SSHMUX_COMMAND_STDIO_FWD:
2173 mux_client_request_stdio_fwd(sock);
2174 exit(0);
Damien Miller6c3eec72011-05-05 14:16:22 +10002175 case SSHMUX_COMMAND_STOP:
2176 mux_client_request_stop_listening(sock);
2177 fprintf(stderr, "Stop listening request sent.\r\n");
2178 exit(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10002179 case SSHMUX_COMMAND_CANCEL_FWD:
2180 if (mux_client_forwards(sock, 1) != 0)
2181 error("%s: master cancel forward request failed",
2182 __func__);
2183 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002184 default:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10002185 fatal("unrecognised muxclient_command %d", muxclient_command);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002186 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002187}