blob: 8da2676b37af5ec405502fba31c880b43e606d67 [file] [log] [blame]
Damien Millerf42f7682014-07-18 15:03:27 +10001/* $OpenBSD: mux.c,v 1.47 2014/07/17 00:10:18 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>
36#include <sys/param.h>
37#include <sys/stat.h>
38#include <sys/socket.h>
39#include <sys/un.h>
40
41#include <errno.h>
42#include <fcntl.h>
43#include <signal.h>
44#include <stdarg.h>
45#include <stddef.h>
46#include <stdlib.h>
47#include <stdio.h>
48#include <string.h>
49#include <unistd.h>
Darren Tuckerce38d822008-06-07 06:25:15 +100050#ifdef HAVE_PATHS_H
Damien Millerb1cbfa22008-05-19 16:00:08 +100051#include <paths.h>
Darren Tuckerce38d822008-06-07 06:25:15 +100052#endif
Damien Millerb1cbfa22008-05-19 16:00:08 +100053
Damien Millere1537f92010-01-26 13:26:22 +110054#ifdef HAVE_POLL_H
55#include <poll.h>
56#else
57# ifdef HAVE_SYS_POLL_H
58# include <sys/poll.h>
59# endif
60#endif
61
Damien Millera7058ec2008-05-20 08:57:06 +100062#ifdef HAVE_UTIL_H
63# include <util.h>
64#endif
65
Damien Millerb1cbfa22008-05-19 16:00:08 +100066#include "openbsd-compat/sys-queue.h"
67#include "xmalloc.h"
68#include "log.h"
69#include "ssh.h"
Damien Miller388f6fc2010-05-21 14:57:35 +100070#include "ssh2.h"
Damien Millerb1cbfa22008-05-19 16:00:08 +100071#include "pathnames.h"
72#include "misc.h"
73#include "match.h"
74#include "buffer.h"
75#include "channels.h"
76#include "msg.h"
77#include "packet.h"
78#include "monitor_fdpass.h"
79#include "sshpty.h"
80#include "key.h"
81#include "readconf.h"
82#include "clientloop.h"
83
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;
92extern char *stdio_forward_host;
93extern int stdio_forward_port;
Damien Millerb1cbfa22008-05-19 16:00:08 +100094
Darren Tucker2fb66ca2008-06-13 04:49:33 +100095/* Context for session open confirmation callback */
96struct mux_session_confirm_ctx {
Damien Millere1537f92010-01-26 13:26:22 +110097 u_int want_tty;
98 u_int want_subsys;
99 u_int want_x_fwd;
100 u_int want_agent_fwd;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000101 Buffer cmd;
102 char *term;
103 struct termios tio;
104 char **env;
Damien Millerd530f5f2010-05-21 14:57:10 +1000105 u_int rid;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000106};
107
Damien Miller388f6fc2010-05-21 14:57:35 +1000108/* Context for global channel callback */
109struct mux_channel_confirm_ctx {
110 u_int cid; /* channel id */
111 u_int rid; /* request id */
112 int fid; /* forward id */
113};
114
Damien Millerb1cbfa22008-05-19 16:00:08 +1000115/* fd to control socket */
116int muxserver_sock = -1;
117
Damien Millere1537f92010-01-26 13:26:22 +1100118/* client request id */
119u_int muxclient_request_id = 0;
120
Damien Millerb1cbfa22008-05-19 16:00:08 +1000121/* Multiplexing control command */
122u_int muxclient_command = 0;
123
124/* Set when signalled. */
125static volatile sig_atomic_t muxclient_terminate = 0;
126
127/* PID of multiplex server */
128static u_int muxserver_pid = 0;
129
Damien Millere1537f92010-01-26 13:26:22 +1100130static Channel *mux_listener_channel = NULL;
Damien Millerb1cbfa22008-05-19 16:00:08 +1000131
Damien Millere1537f92010-01-26 13:26:22 +1100132struct mux_master_state {
133 int hello_rcvd;
134};
135
136/* mux protocol messages */
137#define MUX_MSG_HELLO 0x00000001
138#define MUX_C_NEW_SESSION 0x10000002
139#define MUX_C_ALIVE_CHECK 0x10000004
140#define MUX_C_TERMINATE 0x10000005
141#define MUX_C_OPEN_FWD 0x10000006
142#define MUX_C_CLOSE_FWD 0x10000007
143#define MUX_C_NEW_STDIO_FWD 0x10000008
Damien Miller6c3eec72011-05-05 14:16:22 +1000144#define MUX_C_STOP_LISTENING 0x10000009
Damien Millere1537f92010-01-26 13:26:22 +1100145#define MUX_S_OK 0x80000001
146#define MUX_S_PERMISSION_DENIED 0x80000002
147#define MUX_S_FAILURE 0x80000003
148#define MUX_S_EXIT_MESSAGE 0x80000004
149#define MUX_S_ALIVE 0x80000005
150#define MUX_S_SESSION_OPENED 0x80000006
Damien Miller388f6fc2010-05-21 14:57:35 +1000151#define MUX_S_REMOTE_PORT 0x80000007
Damien Miller555f3b82011-05-15 08:48:05 +1000152#define MUX_S_TTY_ALLOC_FAIL 0x80000008
Damien Millere1537f92010-01-26 13:26:22 +1100153
154/* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
155#define MUX_FWD_LOCAL 1
156#define MUX_FWD_REMOTE 2
157#define MUX_FWD_DYNAMIC 3
158
Damien Millerd530f5f2010-05-21 14:57:10 +1000159static void mux_session_confirm(int, int, void *);
Damien Millere1537f92010-01-26 13:26:22 +1100160
161static int process_mux_master_hello(u_int, Channel *, Buffer *, Buffer *);
162static int process_mux_new_session(u_int, Channel *, Buffer *, Buffer *);
163static int process_mux_alive_check(u_int, Channel *, Buffer *, Buffer *);
164static int process_mux_terminate(u_int, Channel *, Buffer *, Buffer *);
165static int process_mux_open_fwd(u_int, Channel *, Buffer *, Buffer *);
166static int process_mux_close_fwd(u_int, Channel *, Buffer *, Buffer *);
167static int process_mux_stdio_fwd(u_int, Channel *, Buffer *, Buffer *);
Damien Miller6c3eec72011-05-05 14:16:22 +1000168static int process_mux_stop_listening(u_int, Channel *, Buffer *, Buffer *);
Damien Millere1537f92010-01-26 13:26:22 +1100169
170static const struct {
171 u_int type;
172 int (*handler)(u_int, Channel *, Buffer *, Buffer *);
173} mux_master_handlers[] = {
174 { MUX_MSG_HELLO, process_mux_master_hello },
175 { MUX_C_NEW_SESSION, process_mux_new_session },
176 { MUX_C_ALIVE_CHECK, process_mux_alive_check },
177 { MUX_C_TERMINATE, process_mux_terminate },
178 { MUX_C_OPEN_FWD, process_mux_open_fwd },
179 { MUX_C_CLOSE_FWD, process_mux_close_fwd },
180 { MUX_C_NEW_STDIO_FWD, process_mux_stdio_fwd },
Damien Miller6c3eec72011-05-05 14:16:22 +1000181 { MUX_C_STOP_LISTENING, process_mux_stop_listening },
Damien Millere1537f92010-01-26 13:26:22 +1100182 { 0, NULL }
183};
184
185/* Cleanup callback fired on closure of mux slave _session_ channel */
186/* ARGSUSED */
Darren Tuckerea8342c2013-06-06 08:11:40 +1000187static void
Damien Millere1537f92010-01-26 13:26:22 +1100188mux_master_session_cleanup_cb(int cid, void *unused)
189{
190 Channel *cc, *c = channel_by_id(cid);
191
192 debug3("%s: entering for channel %d", __func__, cid);
193 if (c == NULL)
194 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
195 if (c->ctl_chan != -1) {
196 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
197 fatal("%s: channel %d missing control channel %d",
198 __func__, c->self, c->ctl_chan);
199 c->ctl_chan = -1;
200 cc->remote_id = -1;
201 chan_rcvd_oclose(cc);
202 }
203 channel_cancel_cleanup(c->self);
204}
205
206/* Cleanup callback fired on closure of mux slave _control_ channel */
207/* ARGSUSED */
208static void
209mux_master_control_cleanup_cb(int cid, void *unused)
210{
211 Channel *sc, *c = channel_by_id(cid);
212
213 debug3("%s: entering for channel %d", __func__, cid);
214 if (c == NULL)
215 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
216 if (c->remote_id != -1) {
217 if ((sc = channel_by_id(c->remote_id)) == NULL)
Damien Miller601a23c2010-04-16 15:54:01 +1000218 fatal("%s: channel %d missing session channel %d",
Damien Millere1537f92010-01-26 13:26:22 +1100219 __func__, c->self, c->remote_id);
220 c->remote_id = -1;
221 sc->ctl_chan = -1;
Damien Miller172859c2013-04-23 15:19:27 +1000222 if (sc->type != SSH_CHANNEL_OPEN &&
223 sc->type != SSH_CHANNEL_OPENING) {
Damien Millera21cdfa2010-01-28 06:26:59 +1100224 debug2("%s: channel %d: not open", __func__, sc->self);
Damien Miller133d9d32010-01-30 17:30:04 +1100225 chan_mark_dead(sc);
Damien Millera21cdfa2010-01-28 06:26:59 +1100226 } else {
Damien Miller0dac03f2010-01-30 17:36:33 +1100227 if (sc->istate == CHAN_INPUT_OPEN)
228 chan_read_failed(sc);
229 if (sc->ostate == CHAN_OUTPUT_OPEN)
230 chan_write_failed(sc);
Damien Millera21cdfa2010-01-28 06:26:59 +1100231 }
Damien Millere1537f92010-01-26 13:26:22 +1100232 }
233 channel_cancel_cleanup(c->self);
234}
235
236/* Check mux client environment variables before passing them to mux master. */
237static int
238env_permitted(char *env)
239{
240 int i, ret;
241 char name[1024], *cp;
242
243 if ((cp = strchr(env, '=')) == NULL || cp == env)
244 return 0;
245 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
246 if (ret <= 0 || (size_t)ret >= sizeof(name)) {
247 error("env_permitted: name '%.100s...' too long", env);
248 return 0;
249 }
250
251 for (i = 0; i < options.num_send_env; i++)
252 if (match_pattern(name, options.send_env[i]))
253 return 1;
254
255 return 0;
256}
257
258/* Mux master protocol message handlers */
259
260static int
261process_mux_master_hello(u_int rid, Channel *c, Buffer *m, Buffer *r)
262{
263 u_int ver;
264 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
265
266 if (state == NULL)
267 fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self);
268 if (state->hello_rcvd) {
269 error("%s: HELLO received twice", __func__);
270 return -1;
271 }
272 if (buffer_get_int_ret(&ver, m) != 0) {
273 malf:
274 error("%s: malformed message", __func__);
275 return -1;
276 }
277 if (ver != SSHMUX_VER) {
278 error("Unsupported multiplexing protocol version %d "
279 "(expected %d)", ver, SSHMUX_VER);
280 return -1;
281 }
282 debug2("%s: channel %d slave version %u", __func__, c->self, ver);
283
284 /* No extensions are presently defined */
285 while (buffer_len(m) > 0) {
286 char *name = buffer_get_string_ret(m, NULL);
287 char *value = buffer_get_string_ret(m, NULL);
288
289 if (name == NULL || value == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000290 free(name);
Darren Tucker746e9062013-06-06 08:20:13 +1000291 free(value);
Damien Millere1537f92010-01-26 13:26:22 +1100292 goto malf;
293 }
294 debug2("Unrecognised slave extension \"%s\"", name);
Darren Tuckera627d422013-06-02 07:31:17 +1000295 free(name);
296 free(value);
Damien Millere1537f92010-01-26 13:26:22 +1100297 }
298 state->hello_rcvd = 1;
299 return 0;
300}
301
302static int
303process_mux_new_session(u_int rid, Channel *c, Buffer *m, Buffer *r)
304{
305 Channel *nc;
306 struct mux_session_confirm_ctx *cctx;
307 char *reserved, *cmd, *cp;
308 u_int i, j, len, env_len, escape_char, window, packetmax;
309 int new_fd[3];
310
311 /* Reply for SSHMUX_COMMAND_OPEN */
312 cctx = xcalloc(1, sizeof(*cctx));
313 cctx->term = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000314 cctx->rid = rid;
Damien Millere1537f92010-01-26 13:26:22 +1100315 cmd = reserved = NULL;
Damien Millerab523b02012-07-06 13:44:43 +1000316 cctx->env = NULL;
317 env_len = 0;
Damien Millere1537f92010-01-26 13:26:22 +1100318 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
319 buffer_get_int_ret(&cctx->want_tty, m) != 0 ||
320 buffer_get_int_ret(&cctx->want_x_fwd, m) != 0 ||
321 buffer_get_int_ret(&cctx->want_agent_fwd, m) != 0 ||
322 buffer_get_int_ret(&cctx->want_subsys, m) != 0 ||
323 buffer_get_int_ret(&escape_char, m) != 0 ||
324 (cctx->term = buffer_get_string_ret(m, &len)) == NULL ||
325 (cmd = buffer_get_string_ret(m, &len)) == NULL) {
326 malf:
Darren Tuckera627d422013-06-02 07:31:17 +1000327 free(cmd);
328 free(reserved);
Damien Millerab523b02012-07-06 13:44:43 +1000329 for (j = 0; j < env_len; j++)
Darren Tuckera627d422013-06-02 07:31:17 +1000330 free(cctx->env[j]);
331 free(cctx->env);
332 free(cctx->term);
333 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100334 error("%s: malformed message", __func__);
335 return -1;
336 }
Darren Tuckera627d422013-06-02 07:31:17 +1000337 free(reserved);
Damien Millere1537f92010-01-26 13:26:22 +1100338 reserved = NULL;
339
Damien Millere1537f92010-01-26 13:26:22 +1100340 while (buffer_len(m) > 0) {
341#define MUX_MAX_ENV_VARS 4096
Damien Miller2ec03422012-02-11 08:16:28 +1100342 if ((cp = buffer_get_string_ret(m, &len)) == NULL)
Damien Millere1537f92010-01-26 13:26:22 +1100343 goto malf;
Damien Millere1537f92010-01-26 13:26:22 +1100344 if (!env_permitted(cp)) {
Darren Tuckera627d422013-06-02 07:31:17 +1000345 free(cp);
Damien Millere1537f92010-01-26 13:26:22 +1100346 continue;
347 }
348 cctx->env = xrealloc(cctx->env, env_len + 2,
349 sizeof(*cctx->env));
350 cctx->env[env_len++] = cp;
351 cctx->env[env_len] = NULL;
352 if (env_len > MUX_MAX_ENV_VARS) {
353 error(">%d environment variables received, ignoring "
354 "additional", MUX_MAX_ENV_VARS);
355 break;
356 }
357 }
358
359 debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
360 "term \"%s\", cmd \"%s\", env %u", __func__, c->self,
361 cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
362 cctx->want_subsys, cctx->term, cmd, env_len);
363
364 buffer_init(&cctx->cmd);
365 buffer_append(&cctx->cmd, cmd, strlen(cmd));
Darren Tuckera627d422013-06-02 07:31:17 +1000366 free(cmd);
Damien Millere1537f92010-01-26 13:26:22 +1100367 cmd = NULL;
368
369 /* Gather fds from client */
370 for(i = 0; i < 3; i++) {
371 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
372 error("%s: failed to receive fd %d from slave",
373 __func__, i);
374 for (j = 0; j < i; j++)
375 close(new_fd[j]);
376 for (j = 0; j < env_len; j++)
Darren Tuckera627d422013-06-02 07:31:17 +1000377 free(cctx->env[j]);
378 free(cctx->env);
379 free(cctx->term);
Damien Millere1537f92010-01-26 13:26:22 +1100380 buffer_free(&cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +1000381 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100382
383 /* prepare reply */
384 buffer_put_int(r, MUX_S_FAILURE);
385 buffer_put_int(r, rid);
386 buffer_put_cstring(r,
387 "did not receive file descriptors");
388 return -1;
389 }
390 }
391
392 debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
393 new_fd[0], new_fd[1], new_fd[2]);
394
395 /* XXX support multiple child sessions in future */
396 if (c->remote_id != -1) {
397 debug2("%s: session already open", __func__);
398 /* prepare reply */
399 buffer_put_int(r, MUX_S_FAILURE);
400 buffer_put_int(r, rid);
401 buffer_put_cstring(r, "Multiple sessions not supported");
402 cleanup:
403 close(new_fd[0]);
404 close(new_fd[1]);
405 close(new_fd[2]);
Darren Tuckera627d422013-06-02 07:31:17 +1000406 free(cctx->term);
Damien Millere1537f92010-01-26 13:26:22 +1100407 if (env_len != 0) {
408 for (i = 0; i < env_len; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000409 free(cctx->env[i]);
410 free(cctx->env);
Damien Millere1537f92010-01-26 13:26:22 +1100411 }
412 buffer_free(&cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +1000413 free(cctx);
Damien Millere1537f92010-01-26 13:26:22 +1100414 return 0;
415 }
416
417 if (options.control_master == SSHCTL_MASTER_ASK ||
418 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
419 if (!ask_permission("Allow shared connection to %s? ", host)) {
420 debug2("%s: session refused by user", __func__);
421 /* prepare reply */
422 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
423 buffer_put_int(r, rid);
424 buffer_put_cstring(r, "Permission denied");
425 goto cleanup;
426 }
427 }
428
429 /* Try to pick up ttymodes from client before it goes raw */
430 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
431 error("%s: tcgetattr: %s", __func__, strerror(errno));
432
433 /* enable nonblocking unless tty */
434 if (!isatty(new_fd[0]))
435 set_nonblock(new_fd[0]);
436 if (!isatty(new_fd[1]))
437 set_nonblock(new_fd[1]);
438 if (!isatty(new_fd[2]))
439 set_nonblock(new_fd[2]);
440
441 window = CHAN_SES_WINDOW_DEFAULT;
442 packetmax = CHAN_SES_PACKET_DEFAULT;
443 if (cctx->want_tty) {
444 window >>= 1;
445 packetmax >>= 1;
446 }
447
448 nc = channel_new("session", SSH_CHANNEL_OPENING,
449 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
450 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
451
452 nc->ctl_chan = c->self; /* link session -> control channel */
453 c->remote_id = nc->self; /* link control -> session channel */
454
455 if (cctx->want_tty && escape_char != 0xffffffff) {
456 channel_register_filter(nc->self,
457 client_simple_escape_filter, NULL,
458 client_filter_cleanup,
459 client_new_escape_filter_ctx((int)escape_char));
460 }
461
462 debug2("%s: channel_new: %d linked to control channel %d",
463 __func__, nc->self, nc->ctl_chan);
464
465 channel_send_open(nc->self);
466 channel_register_open_confirm(nc->self, mux_session_confirm, cctx);
Damien Millerd530f5f2010-05-21 14:57:10 +1000467 c->mux_pause = 1; /* stop handling messages until open_confirm done */
Damien Miller85c50d72010-05-10 11:53:02 +1000468 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +1100469
Damien Millerd530f5f2010-05-21 14:57:10 +1000470 /* reply is deferred, sent by mux_session_confirm */
Damien Millere1537f92010-01-26 13:26:22 +1100471 return 0;
472}
473
474static int
475process_mux_alive_check(u_int rid, Channel *c, Buffer *m, Buffer *r)
476{
477 debug2("%s: channel %d: alive check", __func__, c->self);
478
479 /* prepare reply */
480 buffer_put_int(r, MUX_S_ALIVE);
481 buffer_put_int(r, rid);
482 buffer_put_int(r, (u_int)getpid());
483
484 return 0;
485}
486
487static int
488process_mux_terminate(u_int rid, Channel *c, Buffer *m, Buffer *r)
489{
490 debug2("%s: channel %d: terminate request", __func__, c->self);
491
492 if (options.control_master == SSHCTL_MASTER_ASK ||
493 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
494 if (!ask_permission("Terminate shared connection to %s? ",
495 host)) {
496 debug2("%s: termination refused by user", __func__);
497 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
498 buffer_put_int(r, rid);
499 buffer_put_cstring(r, "Permission denied");
500 return 0;
501 }
502 }
503
504 quit_pending = 1;
505 buffer_put_int(r, MUX_S_OK);
506 buffer_put_int(r, rid);
507 /* XXX exit happens too soon - message never makes it to client */
508 return 0;
509}
510
511static char *
Damien Miller7acefbb2014-07-18 14:11:24 +1000512format_forward(u_int ftype, struct Forward *fwd)
Damien Millere1537f92010-01-26 13:26:22 +1100513{
514 char *ret;
515
516 switch (ftype) {
517 case MUX_FWD_LOCAL:
518 xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +1000519 (fwd->listen_path != NULL) ? fwd->listen_path :
Damien Millere1537f92010-01-26 13:26:22 +1100520 (fwd->listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +1000521 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millere1537f92010-01-26 13:26:22 +1100522 fwd->listen_host, fwd->listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +1000523 (fwd->connect_path != NULL) ? fwd->connect_path :
Damien Millere1537f92010-01-26 13:26:22 +1100524 fwd->connect_host, fwd->connect_port);
525 break;
526 case MUX_FWD_DYNAMIC:
527 xasprintf(&ret, "dynamic forward %.200s:%d -> *",
528 (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);
531 break;
532 case MUX_FWD_REMOTE:
533 xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +1000534 (fwd->listen_path != NULL) ? fwd->listen_path :
Damien Millere1537f92010-01-26 13:26:22 +1100535 (fwd->listen_host == NULL) ?
536 "LOCALHOST" : fwd->listen_host,
537 fwd->listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +1000538 (fwd->connect_path != NULL) ? fwd->connect_path :
Damien Millere1537f92010-01-26 13:26:22 +1100539 fwd->connect_host, fwd->connect_port);
540 break;
541 default:
542 fatal("%s: unknown forward type %u", __func__, ftype);
543 }
544 return ret;
545}
546
547static int
548compare_host(const char *a, const char *b)
549{
550 if (a == NULL && b == NULL)
551 return 1;
552 if (a == NULL || b == NULL)
553 return 0;
554 return strcmp(a, b) == 0;
555}
556
557static int
Damien Miller7acefbb2014-07-18 14:11:24 +1000558compare_forward(struct Forward *a, struct Forward *b)
Damien Millere1537f92010-01-26 13:26:22 +1100559{
560 if (!compare_host(a->listen_host, b->listen_host))
561 return 0;
Damien Miller7acefbb2014-07-18 14:11:24 +1000562 if (!compare_host(a->listen_path, b->listen_path))
563 return 0;
Damien Millere1537f92010-01-26 13:26:22 +1100564 if (a->listen_port != b->listen_port)
565 return 0;
566 if (!compare_host(a->connect_host, b->connect_host))
567 return 0;
Damien Miller7acefbb2014-07-18 14:11:24 +1000568 if (!compare_host(a->connect_path, b->connect_path))
569 return 0;
Damien Millere1537f92010-01-26 13:26:22 +1100570 if (a->connect_port != b->connect_port)
571 return 0;
572
573 return 1;
574}
575
Damien Miller388f6fc2010-05-21 14:57:35 +1000576static void
577mux_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
578{
579 struct mux_channel_confirm_ctx *fctx = ctxt;
580 char *failmsg = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000581 struct Forward *rfwd;
Damien Miller388f6fc2010-05-21 14:57:35 +1000582 Channel *c;
583 Buffer out;
584
585 if ((c = channel_by_id(fctx->cid)) == NULL) {
586 /* no channel for reply */
587 error("%s: unknown channel", __func__);
588 return;
589 }
590 buffer_init(&out);
591 if (fctx->fid >= options.num_remote_forwards) {
592 xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
593 goto fail;
594 }
595 rfwd = &options.remote_forwards[fctx->fid];
596 debug("%s: %s for: listen %d, connect %s:%d", __func__,
597 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Miller7acefbb2014-07-18 14:11:24 +1000598 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
599 rfwd->connect_host, rfwd->connect_port);
Damien Miller388f6fc2010-05-21 14:57:35 +1000600 if (type == SSH2_MSG_REQUEST_SUCCESS) {
601 if (rfwd->listen_port == 0) {
602 rfwd->allocated_port = packet_get_int();
603 logit("Allocated port %u for mux remote forward"
604 " to %s:%d", rfwd->allocated_port,
605 rfwd->connect_host, rfwd->connect_port);
606 buffer_put_int(&out, MUX_S_REMOTE_PORT);
607 buffer_put_int(&out, fctx->rid);
608 buffer_put_int(&out, rfwd->allocated_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +1100609 channel_update_permitted_opens(rfwd->handle,
610 rfwd->allocated_port);
Damien Miller388f6fc2010-05-21 14:57:35 +1000611 } else {
612 buffer_put_int(&out, MUX_S_OK);
613 buffer_put_int(&out, fctx->rid);
614 }
615 goto out;
616 } else {
Darren Tucker68afb8c2011-10-02 18:59:03 +1100617 if (rfwd->listen_port == 0)
618 channel_update_permitted_opens(rfwd->handle, -1);
Damien Miller7acefbb2014-07-18 14:11:24 +1000619 if (rfwd->listen_path != NULL)
620 xasprintf(&failmsg, "remote port forwarding failed for "
621 "listen path %s", rfwd->listen_path);
622 else
623 xasprintf(&failmsg, "remote port forwarding failed for "
624 "listen port %d", rfwd->listen_port);
Damien Miller388f6fc2010-05-21 14:57:35 +1000625 }
626 fail:
627 error("%s: %s", __func__, failmsg);
628 buffer_put_int(&out, MUX_S_FAILURE);
629 buffer_put_int(&out, fctx->rid);
630 buffer_put_cstring(&out, failmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000631 free(failmsg);
Damien Miller388f6fc2010-05-21 14:57:35 +1000632 out:
633 buffer_put_string(&c->output, buffer_ptr(&out), buffer_len(&out));
634 buffer_free(&out);
635 if (c->mux_pause <= 0)
636 fatal("%s: mux_pause %d", __func__, c->mux_pause);
637 c->mux_pause = 0; /* start processing messages again */
638}
639
Damien Millere1537f92010-01-26 13:26:22 +1100640static int
641process_mux_open_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
642{
Damien Miller7acefbb2014-07-18 14:11:24 +1000643 struct Forward fwd;
Damien Millere1537f92010-01-26 13:26:22 +1100644 char *fwd_desc = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000645 char *listen_addr, *connect_addr;
Damien Millere1537f92010-01-26 13:26:22 +1100646 u_int ftype;
Damien Millerce986542013-07-18 16:12:44 +1000647 u_int lport, cport;
Damien Millere1537f92010-01-26 13:26:22 +1100648 int i, ret = 0, freefwd = 1;
649
Damien Miller7acefbb2014-07-18 14:11:24 +1000650 /* XXX - lport/cport check redundant */
Damien Millere1537f92010-01-26 13:26:22 +1100651 if (buffer_get_int_ret(&ftype, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000652 (listen_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000653 buffer_get_int_ret(&lport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000654 (connect_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000655 buffer_get_int_ret(&cport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000656 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
657 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
Damien Millere1537f92010-01-26 13:26:22 +1100658 error("%s: malformed message", __func__);
659 ret = -1;
660 goto out;
661 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000662 if (*listen_addr == '\0') {
663 free(listen_addr);
664 listen_addr = NULL;
665 }
666 if (*connect_addr == '\0') {
667 free(connect_addr);
668 connect_addr = NULL;
669 }
670
671 memset(&fwd, 0, sizeof(fwd));
Damien Millerce986542013-07-18 16:12:44 +1000672 fwd.listen_port = lport;
Damien Miller7acefbb2014-07-18 14:11:24 +1000673 if (fwd.listen_port == PORT_STREAMLOCAL)
674 fwd.listen_path = listen_addr;
675 else
676 fwd.listen_host = listen_addr;
Damien Millerce986542013-07-18 16:12:44 +1000677 fwd.connect_port = cport;
Damien Miller7acefbb2014-07-18 14:11:24 +1000678 if (fwd.connect_port == PORT_STREAMLOCAL)
679 fwd.connect_path = connect_addr;
680 else
681 fwd.connect_host = connect_addr;
Damien Millere1537f92010-01-26 13:26:22 +1100682
683 debug2("%s: channel %d: request %s", __func__, c->self,
684 (fwd_desc = format_forward(ftype, &fwd)));
685
686 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
687 ftype != MUX_FWD_DYNAMIC) {
688 logit("%s: invalid forwarding type %u", __func__, ftype);
689 invalid:
Damien Miller7acefbb2014-07-18 14:11:24 +1000690 free(listen_addr);
691 free(connect_addr);
Damien Millere1537f92010-01-26 13:26:22 +1100692 buffer_put_int(r, MUX_S_FAILURE);
693 buffer_put_int(r, rid);
694 buffer_put_cstring(r, "Invalid forwarding request");
695 return 0;
696 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000697 if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) {
698 logit("%s: streamlocal and dynamic forwards "
699 "are mutually exclusive", __func__);
700 goto invalid;
701 }
702 if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) {
Damien Millere1537f92010-01-26 13:26:22 +1100703 logit("%s: invalid listen port %u", __func__,
704 fwd.listen_port);
705 goto invalid;
706 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000707 if ((fwd.connect_port != PORT_STREAMLOCAL && fwd.connect_port >= 65536)
708 || (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE && fwd.connect_port == 0)) {
Damien Millere1537f92010-01-26 13:26:22 +1100709 logit("%s: invalid connect port %u", __func__,
710 fwd.connect_port);
711 goto invalid;
712 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000713 if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL && fwd.connect_path == NULL) {
Damien Millere1537f92010-01-26 13:26:22 +1100714 logit("%s: missing connect host", __func__);
715 goto invalid;
716 }
717
718 /* Skip forwards that have already been requested */
719 switch (ftype) {
720 case MUX_FWD_LOCAL:
721 case MUX_FWD_DYNAMIC:
722 for (i = 0; i < options.num_local_forwards; i++) {
723 if (compare_forward(&fwd,
724 options.local_forwards + i)) {
725 exists:
726 debug2("%s: found existing forwarding",
727 __func__);
728 buffer_put_int(r, MUX_S_OK);
729 buffer_put_int(r, rid);
730 goto out;
731 }
732 }
733 break;
734 case MUX_FWD_REMOTE:
735 for (i = 0; i < options.num_remote_forwards; i++) {
736 if (compare_forward(&fwd,
Damien Miller388f6fc2010-05-21 14:57:35 +1000737 options.remote_forwards + i)) {
738 if (fwd.listen_port != 0)
739 goto exists;
740 debug2("%s: found allocated port",
741 __func__);
742 buffer_put_int(r, MUX_S_REMOTE_PORT);
743 buffer_put_int(r, rid);
744 buffer_put_int(r,
745 options.remote_forwards[i].allocated_port);
746 goto out;
747 }
Damien Millere1537f92010-01-26 13:26:22 +1100748 }
749 break;
750 }
751
752 if (options.control_master == SSHCTL_MASTER_ASK ||
753 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
754 if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
755 debug2("%s: forwarding refused by user", __func__);
756 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
757 buffer_put_int(r, rid);
758 buffer_put_cstring(r, "Permission denied");
759 goto out;
760 }
761 }
762
763 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000764 if (!channel_setup_local_fwd_listener(&fwd,
765 &options.fwd_opts)) {
Damien Millere1537f92010-01-26 13:26:22 +1100766 fail:
767 logit("slave-requested %s failed", fwd_desc);
768 buffer_put_int(r, MUX_S_FAILURE);
769 buffer_put_int(r, rid);
770 buffer_put_cstring(r, "Port forwarding failed");
771 goto out;
772 }
773 add_local_forward(&options, &fwd);
774 freefwd = 0;
775 } else {
Damien Miller388f6fc2010-05-21 14:57:35 +1000776 struct mux_channel_confirm_ctx *fctx;
777
Damien Miller7acefbb2014-07-18 14:11:24 +1000778 fwd.handle = channel_request_remote_forwarding(&fwd);
Darren Tucker68afb8c2011-10-02 18:59:03 +1100779 if (fwd.handle < 0)
Damien Millere1537f92010-01-26 13:26:22 +1100780 goto fail;
781 add_remote_forward(&options, &fwd);
Damien Miller388f6fc2010-05-21 14:57:35 +1000782 fctx = xcalloc(1, sizeof(*fctx));
783 fctx->cid = c->self;
784 fctx->rid = rid;
Damien Miller232cfb12010-06-26 09:50:30 +1000785 fctx->fid = options.num_remote_forwards - 1;
Damien Miller388f6fc2010-05-21 14:57:35 +1000786 client_register_global_confirm(mux_confirm_remote_forward,
787 fctx);
Damien Millere1537f92010-01-26 13:26:22 +1100788 freefwd = 0;
Damien Miller388f6fc2010-05-21 14:57:35 +1000789 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
790 /* delayed reply in mux_confirm_remote_forward */
791 goto out;
Damien Millere1537f92010-01-26 13:26:22 +1100792 }
793 buffer_put_int(r, MUX_S_OK);
794 buffer_put_int(r, rid);
795 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000796 free(fwd_desc);
Damien Millere1537f92010-01-26 13:26:22 +1100797 if (freefwd) {
Darren Tuckera627d422013-06-02 07:31:17 +1000798 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000799 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000800 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000801 free(fwd.connect_path);
Damien Millere1537f92010-01-26 13:26:22 +1100802 }
803 return ret;
804}
805
806static int
807process_mux_close_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
808{
Damien Miller7acefbb2014-07-18 14:11:24 +1000809 struct Forward fwd, *found_fwd;
Damien Millere1537f92010-01-26 13:26:22 +1100810 char *fwd_desc = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000811 const char *error_reason = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000812 char *listen_addr = NULL, *connect_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100813 u_int ftype;
Damien Miller7acefbb2014-07-18 14:11:24 +1000814 int i, ret = 0;
Damien Millerce986542013-07-18 16:12:44 +1000815 u_int lport, cport;
Damien Millere1537f92010-01-26 13:26:22 +1100816
Damien Millere1537f92010-01-26 13:26:22 +1100817 if (buffer_get_int_ret(&ftype, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000818 (listen_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000819 buffer_get_int_ret(&lport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000820 (connect_addr = buffer_get_string_ret(m, NULL)) == NULL ||
Damien Millerce986542013-07-18 16:12:44 +1000821 buffer_get_int_ret(&cport, m) != 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +1000822 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
823 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
Damien Millere1537f92010-01-26 13:26:22 +1100824 error("%s: malformed message", __func__);
825 ret = -1;
826 goto out;
827 }
828
Damien Miller7acefbb2014-07-18 14:11:24 +1000829 if (*listen_addr == '\0') {
830 free(listen_addr);
831 listen_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100832 }
Damien Miller7acefbb2014-07-18 14:11:24 +1000833 if (*connect_addr == '\0') {
834 free(connect_addr);
835 connect_addr = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100836 }
837
Damien Miller7acefbb2014-07-18 14:11:24 +1000838 memset(&fwd, 0, sizeof(fwd));
839 fwd.listen_port = lport;
840 if (fwd.listen_port == PORT_STREAMLOCAL)
841 fwd.listen_path = listen_addr;
842 else
843 fwd.listen_host = listen_addr;
844 fwd.connect_port = cport;
845 if (fwd.connect_port == PORT_STREAMLOCAL)
846 fwd.connect_path = connect_addr;
847 else
848 fwd.connect_host = connect_addr;
849
Damien Millerf6dff7c2011-09-22 21:38:52 +1000850 debug2("%s: channel %d: request cancel %s", __func__, c->self,
Damien Millere1537f92010-01-26 13:26:22 +1100851 (fwd_desc = format_forward(ftype, &fwd)));
852
Damien Millerf6dff7c2011-09-22 21:38:52 +1000853 /* make sure this has been requested */
854 found_fwd = NULL;
855 switch (ftype) {
856 case MUX_FWD_LOCAL:
857 case MUX_FWD_DYNAMIC:
858 for (i = 0; i < options.num_local_forwards; i++) {
859 if (compare_forward(&fwd,
860 options.local_forwards + i)) {
861 found_fwd = options.local_forwards + i;
862 break;
863 }
864 }
865 break;
866 case MUX_FWD_REMOTE:
867 for (i = 0; i < options.num_remote_forwards; i++) {
868 if (compare_forward(&fwd,
869 options.remote_forwards + i)) {
870 found_fwd = options.remote_forwards + i;
871 break;
872 }
873 }
874 break;
875 }
Damien Millere1537f92010-01-26 13:26:22 +1100876
Damien Millerf6dff7c2011-09-22 21:38:52 +1000877 if (found_fwd == NULL)
878 error_reason = "port not forwarded";
879 else if (ftype == MUX_FWD_REMOTE) {
880 /*
881 * This shouldn't fail unless we confused the host/port
882 * between options.remote_forwards and permitted_opens.
Darren Tucker68afb8c2011-10-02 18:59:03 +1100883 * However, for dynamic allocated listen ports we need
Damien Miller7acefbb2014-07-18 14:11:24 +1000884 * to use the actual listen port.
Damien Millerf6dff7c2011-09-22 21:38:52 +1000885 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000886 if (channel_request_rforward_cancel(found_fwd) == -1)
Damien Millerf6dff7c2011-09-22 21:38:52 +1000887 error_reason = "port not in permitted opens";
888 } else { /* local and dynamic forwards */
889 /* Ditto */
Damien Miller7acefbb2014-07-18 14:11:24 +1000890 if (channel_cancel_lport_listener(&fwd, fwd.connect_port,
891 &options.fwd_opts) == -1)
Damien Millerf6dff7c2011-09-22 21:38:52 +1000892 error_reason = "port not found";
893 }
894
895 if (error_reason == NULL) {
896 buffer_put_int(r, MUX_S_OK);
897 buffer_put_int(r, rid);
898
Darren Tuckera627d422013-06-02 07:31:17 +1000899 free(found_fwd->listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000900 free(found_fwd->listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000901 free(found_fwd->connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000902 free(found_fwd->connect_path);
Damien Millerf6dff7c2011-09-22 21:38:52 +1000903 found_fwd->listen_host = found_fwd->connect_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000904 found_fwd->listen_path = found_fwd->connect_path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000905 found_fwd->listen_port = found_fwd->connect_port = 0;
906 } else {
907 buffer_put_int(r, MUX_S_FAILURE);
908 buffer_put_int(r, rid);
909 buffer_put_cstring(r, error_reason);
910 }
Damien Millere1537f92010-01-26 13:26:22 +1100911 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000912 free(fwd_desc);
Damien Miller7acefbb2014-07-18 14:11:24 +1000913 free(listen_addr);
914 free(connect_addr);
Damien Millere1537f92010-01-26 13:26:22 +1100915
916 return ret;
917}
918
919static int
920process_mux_stdio_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
921{
922 Channel *nc;
923 char *reserved, *chost;
924 u_int cport, i, j;
925 int new_fd[2];
926
927 chost = reserved = NULL;
928 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
929 (chost = buffer_get_string_ret(m, NULL)) == NULL ||
930 buffer_get_int_ret(&cport, m) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000931 free(reserved);
932 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +1100933 error("%s: malformed message", __func__);
934 return -1;
935 }
Darren Tuckera627d422013-06-02 07:31:17 +1000936 free(reserved);
Damien Millere1537f92010-01-26 13:26:22 +1100937
938 debug2("%s: channel %d: request stdio fwd to %s:%u",
939 __func__, c->self, chost, cport);
940
941 /* Gather fds from client */
942 for(i = 0; i < 2; i++) {
943 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
944 error("%s: failed to receive fd %d from slave",
945 __func__, i);
946 for (j = 0; j < i; j++)
947 close(new_fd[j]);
Darren Tuckera627d422013-06-02 07:31:17 +1000948 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +1100949
950 /* prepare reply */
951 buffer_put_int(r, MUX_S_FAILURE);
952 buffer_put_int(r, rid);
953 buffer_put_cstring(r,
954 "did not receive file descriptors");
955 return -1;
956 }
957 }
958
959 debug3("%s: got fds stdin %d, stdout %d", __func__,
960 new_fd[0], new_fd[1]);
961
962 /* XXX support multiple child sessions in future */
963 if (c->remote_id != -1) {
964 debug2("%s: session already open", __func__);
965 /* prepare reply */
966 buffer_put_int(r, MUX_S_FAILURE);
967 buffer_put_int(r, rid);
968 buffer_put_cstring(r, "Multiple sessions not supported");
969 cleanup:
970 close(new_fd[0]);
971 close(new_fd[1]);
Darren Tuckera627d422013-06-02 07:31:17 +1000972 free(chost);
Damien Millere1537f92010-01-26 13:26:22 +1100973 return 0;
974 }
975
976 if (options.control_master == SSHCTL_MASTER_ASK ||
977 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
Damien Miller68512c02010-10-21 15:21:11 +1100978 if (!ask_permission("Allow forward to %s:%u? ",
Damien Millere1537f92010-01-26 13:26:22 +1100979 chost, cport)) {
980 debug2("%s: stdio fwd refused by user", __func__);
981 /* prepare reply */
982 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
983 buffer_put_int(r, rid);
984 buffer_put_cstring(r, "Permission denied");
985 goto cleanup;
986 }
987 }
988
989 /* enable nonblocking unless tty */
990 if (!isatty(new_fd[0]))
991 set_nonblock(new_fd[0]);
992 if (!isatty(new_fd[1]))
993 set_nonblock(new_fd[1]);
994
995 nc = channel_connect_stdio_fwd(chost, cport, new_fd[0], new_fd[1]);
996
997 nc->ctl_chan = c->self; /* link session -> control channel */
998 c->remote_id = nc->self; /* link control -> session channel */
999
1000 debug2("%s: channel_new: %d linked to control channel %d",
1001 __func__, nc->self, nc->ctl_chan);
1002
Damien Miller85c50d72010-05-10 11:53:02 +10001003 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +11001004
1005 /* prepare reply */
1006 /* XXX defer until channel confirmed */
1007 buffer_put_int(r, MUX_S_SESSION_OPENED);
1008 buffer_put_int(r, rid);
1009 buffer_put_int(r, nc->self);
1010
1011 return 0;
1012}
1013
Damien Miller6c3eec72011-05-05 14:16:22 +10001014static int
1015process_mux_stop_listening(u_int rid, Channel *c, Buffer *m, Buffer *r)
1016{
1017 debug("%s: channel %d: stop listening", __func__, c->self);
1018
1019 if (options.control_master == SSHCTL_MASTER_ASK ||
1020 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
1021 if (!ask_permission("Disable further multiplexing on shared "
1022 "connection to %s? ", host)) {
1023 debug2("%s: stop listen refused by user", __func__);
1024 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
1025 buffer_put_int(r, rid);
1026 buffer_put_cstring(r, "Permission denied");
1027 return 0;
1028 }
1029 }
1030
1031 if (mux_listener_channel != NULL) {
1032 channel_free(mux_listener_channel);
1033 client_stop_mux();
Darren Tuckera627d422013-06-02 07:31:17 +10001034 free(options.control_path);
Damien Miller6c3eec72011-05-05 14:16:22 +10001035 options.control_path = NULL;
1036 mux_listener_channel = NULL;
1037 muxserver_sock = -1;
1038 }
1039
1040 /* prepare reply */
1041 buffer_put_int(r, MUX_S_OK);
1042 buffer_put_int(r, rid);
1043
1044 return 0;
1045}
1046
Damien Millere1537f92010-01-26 13:26:22 +11001047/* Channel callbacks fired on read/write from mux slave fd */
1048static int
1049mux_master_read_cb(Channel *c)
1050{
1051 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
1052 Buffer in, out;
Damien Miller633de332014-05-15 13:48:26 +10001053 const u_char *ptr;
Damien Millere1537f92010-01-26 13:26:22 +11001054 u_int type, rid, have, i;
1055 int ret = -1;
1056
1057 /* Setup ctx and */
1058 if (c->mux_ctx == NULL) {
Damien Millerc094d1e2010-06-26 09:36:34 +10001059 state = xcalloc(1, sizeof(*state));
Damien Millere1537f92010-01-26 13:26:22 +11001060 c->mux_ctx = state;
1061 channel_register_cleanup(c->self,
1062 mux_master_control_cleanup_cb, 0);
1063
1064 /* Send hello */
1065 buffer_init(&out);
1066 buffer_put_int(&out, MUX_MSG_HELLO);
1067 buffer_put_int(&out, SSHMUX_VER);
1068 /* no extensions */
1069 buffer_put_string(&c->output, buffer_ptr(&out),
1070 buffer_len(&out));
1071 buffer_free(&out);
1072 debug3("%s: channel %d: hello sent", __func__, c->self);
1073 return 0;
1074 }
1075
1076 buffer_init(&in);
1077 buffer_init(&out);
1078
1079 /* Channel code ensures that we receive whole packets */
1080 if ((ptr = buffer_get_string_ptr_ret(&c->input, &have)) == NULL) {
1081 malf:
1082 error("%s: malformed message", __func__);
1083 goto out;
1084 }
1085 buffer_append(&in, ptr, have);
1086
1087 if (buffer_get_int_ret(&type, &in) != 0)
1088 goto malf;
1089 debug3("%s: channel %d packet type 0x%08x len %u",
1090 __func__, c->self, type, buffer_len(&in));
1091
1092 if (type == MUX_MSG_HELLO)
1093 rid = 0;
1094 else {
1095 if (!state->hello_rcvd) {
1096 error("%s: expected MUX_MSG_HELLO(0x%08x), "
1097 "received 0x%08x", __func__, MUX_MSG_HELLO, type);
1098 goto out;
1099 }
1100 if (buffer_get_int_ret(&rid, &in) != 0)
1101 goto malf;
1102 }
1103
1104 for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
1105 if (type == mux_master_handlers[i].type) {
1106 ret = mux_master_handlers[i].handler(rid, c, &in, &out);
1107 break;
1108 }
1109 }
1110 if (mux_master_handlers[i].handler == NULL) {
1111 error("%s: unsupported mux message 0x%08x", __func__, type);
1112 buffer_put_int(&out, MUX_S_FAILURE);
1113 buffer_put_int(&out, rid);
1114 buffer_put_cstring(&out, "unsupported request");
1115 ret = 0;
1116 }
1117 /* Enqueue reply packet */
1118 if (buffer_len(&out) != 0) {
1119 buffer_put_string(&c->output, buffer_ptr(&out),
1120 buffer_len(&out));
1121 }
1122 out:
1123 buffer_free(&in);
1124 buffer_free(&out);
1125 return ret;
1126}
1127
1128void
1129mux_exit_message(Channel *c, int exitval)
1130{
1131 Buffer m;
1132 Channel *mux_chan;
1133
Damien Millerbc02f162013-04-23 19:25:49 +10001134 debug3("%s: channel %d: exit message, exitval %d", __func__, c->self,
Damien Millere1537f92010-01-26 13:26:22 +11001135 exitval);
1136
1137 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL)
1138 fatal("%s: channel %d missing mux channel %d",
1139 __func__, c->self, c->ctl_chan);
1140
1141 /* Append exit message packet to control socket output queue */
1142 buffer_init(&m);
1143 buffer_put_int(&m, MUX_S_EXIT_MESSAGE);
1144 buffer_put_int(&m, c->self);
1145 buffer_put_int(&m, exitval);
1146
1147 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m));
1148 buffer_free(&m);
1149}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001150
Damien Miller555f3b82011-05-15 08:48:05 +10001151void
1152mux_tty_alloc_failed(Channel *c)
1153{
1154 Buffer m;
1155 Channel *mux_chan;
1156
1157 debug3("%s: channel %d: TTY alloc failed", __func__, c->self);
1158
1159 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL)
1160 fatal("%s: channel %d missing mux channel %d",
1161 __func__, c->self, c->ctl_chan);
1162
1163 /* Append exit message packet to control socket output queue */
1164 buffer_init(&m);
1165 buffer_put_int(&m, MUX_S_TTY_ALLOC_FAIL);
1166 buffer_put_int(&m, c->self);
1167
1168 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m));
1169 buffer_free(&m);
1170}
1171
Damien Millerb1cbfa22008-05-19 16:00:08 +10001172/* Prepare a mux master to listen on a Unix domain socket. */
1173void
1174muxserver_listen(void)
1175{
Damien Millerb1cbfa22008-05-19 16:00:08 +10001176 mode_t old_umask;
Damien Miller603134e2010-09-24 22:07:55 +10001177 char *orig_control_path = options.control_path;
1178 char rbuf[16+1];
1179 u_int i, r;
Damien Millerf42f7682014-07-18 15:03:27 +10001180 int oerrno;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001181
1182 if (options.control_path == NULL ||
1183 options.control_master == SSHCTL_MASTER_NO)
1184 return;
1185
1186 debug("setting up multiplex master socket");
1187
Damien Miller603134e2010-09-24 22:07:55 +10001188 /*
1189 * Use a temporary path before listen so we can pseudo-atomically
1190 * establish the listening socket in its final location to avoid
1191 * other processes racing in between bind() and listen() and hitting
1192 * an unready socket.
1193 */
1194 for (i = 0; i < sizeof(rbuf) - 1; i++) {
1195 r = arc4random_uniform(26+26+10);
1196 rbuf[i] = (r < 26) ? 'a' + r :
1197 (r < 26*2) ? 'A' + r - 26 :
1198 '0' + r - 26 - 26;
1199 }
1200 rbuf[sizeof(rbuf) - 1] = '\0';
1201 options.control_path = NULL;
1202 xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
1203 debug3("%s: temporary control path %s", __func__, options.control_path);
1204
Damien Millerb1cbfa22008-05-19 16:00:08 +10001205 old_umask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001206 muxserver_sock = unix_listener(options.control_path, 64, 0);
Damien Millerf42f7682014-07-18 15:03:27 +10001207 oerrno = errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10001208 umask(old_umask);
1209 if (muxserver_sock < 0) {
Damien Millerf42f7682014-07-18 15:03:27 +10001210 if (oerrno == EINVAL || oerrno == EADDRINUSE) {
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001211 error("ControlSocket %s already exists, "
1212 "disabling multiplexing", options.control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001213 disable_mux_master:
Damien Miller60432d82011-05-15 08:34:46 +10001214 if (muxserver_sock != -1) {
1215 close(muxserver_sock);
1216 muxserver_sock = -1;
1217 }
Darren Tuckera627d422013-06-02 07:31:17 +10001218 free(orig_control_path);
1219 free(options.control_path);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001220 options.control_path = NULL;
1221 options.control_master = SSHCTL_MASTER_NO;
1222 return;
Damien Miller7acefbb2014-07-18 14:11:24 +10001223 } else {
1224 /* unix_listener() logs the error */
1225 cleanup_exit(255);
1226 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001227 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001228
Damien Miller603134e2010-09-24 22:07:55 +10001229 /* Now atomically "move" the mux socket into position */
1230 if (link(options.control_path, orig_control_path) != 0) {
1231 if (errno != EEXIST) {
1232 fatal("%s: link mux listener %s => %s: %s", __func__,
1233 options.control_path, orig_control_path,
1234 strerror(errno));
1235 }
1236 error("ControlSocket %s already exists, disabling multiplexing",
1237 orig_control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001238 unlink(options.control_path);
1239 goto disable_mux_master;
1240 }
1241 unlink(options.control_path);
Darren Tuckera627d422013-06-02 07:31:17 +10001242 free(options.control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001243 options.control_path = orig_control_path;
1244
Damien Millerb1cbfa22008-05-19 16:00:08 +10001245 set_nonblock(muxserver_sock);
Damien Millere1537f92010-01-26 13:26:22 +11001246
1247 mux_listener_channel = channel_new("mux listener",
1248 SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
1249 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller603134e2010-09-24 22:07:55 +10001250 0, options.control_path, 1);
Damien Millere1537f92010-01-26 13:26:22 +11001251 mux_listener_channel->mux_rcb = mux_master_read_cb;
1252 debug3("%s: mux listener channel %d fd %d", __func__,
1253 mux_listener_channel->self, mux_listener_channel->sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001254}
1255
1256/* Callback on open confirmation in mux master for a mux client session. */
1257static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001258mux_session_confirm(int id, int success, void *arg)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001259{
1260 struct mux_session_confirm_ctx *cctx = arg;
1261 const char *display;
Damien Millerd530f5f2010-05-21 14:57:10 +10001262 Channel *c, *cc;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001263 int i;
Damien Millerd530f5f2010-05-21 14:57:10 +10001264 Buffer reply;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001265
1266 if (cctx == NULL)
1267 fatal("%s: cctx == NULL", __func__);
Damien Millere1537f92010-01-26 13:26:22 +11001268 if ((c = channel_by_id(id)) == NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001269 fatal("%s: no channel for id %d", __func__, id);
Damien Millerd530f5f2010-05-21 14:57:10 +10001270 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
1271 fatal("%s: channel %d lacks control channel %d", __func__,
1272 id, c->ctl_chan);
1273
1274 if (!success) {
1275 debug3("%s: sending failure reply", __func__);
1276 /* prepare reply */
1277 buffer_init(&reply);
1278 buffer_put_int(&reply, MUX_S_FAILURE);
1279 buffer_put_int(&reply, cctx->rid);
1280 buffer_put_cstring(&reply, "Session open refused by peer");
1281 goto done;
1282 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001283
1284 display = getenv("DISPLAY");
1285 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1286 char *proto, *data;
Damien Miller1ab6a512010-06-26 10:02:24 +10001287
Damien Millerb1cbfa22008-05-19 16:00:08 +10001288 /* Get reasonable local authentication information. */
1289 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001290 options.forward_x11_trusted, options.forward_x11_timeout,
1291 &proto, &data);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001292 /* Request forwarding with authentication spoofing. */
Damien Miller1ab6a512010-06-26 10:02:24 +10001293 debug("Requesting X11 forwarding with authentication "
1294 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001295 x11_request_forwarding_with_spoofing(id, display, proto,
1296 data, 1);
1297 client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
1298 /* XXX exit_on_forward_failure */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001299 }
1300
1301 if (cctx->want_agent_fwd && options.forward_agent) {
1302 debug("Requesting authentication agent forwarding.");
1303 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1304 packet_send();
1305 }
1306
1307 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
1308 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
1309
Damien Millerd530f5f2010-05-21 14:57:10 +10001310 debug3("%s: sending success reply", __func__);
1311 /* prepare reply */
1312 buffer_init(&reply);
1313 buffer_put_int(&reply, MUX_S_SESSION_OPENED);
1314 buffer_put_int(&reply, cctx->rid);
1315 buffer_put_int(&reply, c->self);
1316
1317 done:
1318 /* Send reply */
1319 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply));
1320 buffer_free(&reply);
1321
1322 if (cc->mux_pause <= 0)
1323 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1324 cc->mux_pause = 0; /* start processing messages again */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001325 c->open_confirm_ctx = NULL;
1326 buffer_free(&cctx->cmd);
Darren Tuckera627d422013-06-02 07:31:17 +10001327 free(cctx->term);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001328 if (cctx->env != NULL) {
1329 for (i = 0; cctx->env[i] != NULL; i++)
Darren Tuckera627d422013-06-02 07:31:17 +10001330 free(cctx->env[i]);
1331 free(cctx->env);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001332 }
Darren Tuckera627d422013-06-02 07:31:17 +10001333 free(cctx);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001334}
1335
Damien Millerb1cbfa22008-05-19 16:00:08 +10001336/* ** Multiplexing client support */
1337
1338/* Exit signal handler */
1339static void
1340control_client_sighandler(int signo)
1341{
1342 muxclient_terminate = signo;
1343}
1344
1345/*
1346 * Relay signal handler - used to pass some signals from mux client to
1347 * mux master.
1348 */
1349static void
1350control_client_sigrelay(int signo)
1351{
1352 int save_errno = errno;
1353
1354 if (muxserver_pid > 1)
1355 kill(muxserver_pid, signo);
1356
1357 errno = save_errno;
1358}
1359
Damien Millerb1cbfa22008-05-19 16:00:08 +10001360static int
Damien Millere1537f92010-01-26 13:26:22 +11001361mux_client_read(int fd, Buffer *b, u_int need)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001362{
Damien Millere1537f92010-01-26 13:26:22 +11001363 u_int have;
1364 ssize_t len;
1365 u_char *p;
1366 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001367
Damien Millere1537f92010-01-26 13:26:22 +11001368 pfd.fd = fd;
1369 pfd.events = POLLIN;
1370 p = buffer_append_space(b, need);
1371 for (have = 0; have < need; ) {
1372 if (muxclient_terminate) {
1373 errno = EINTR;
1374 return -1;
1375 }
1376 len = read(fd, p + have, need - have);
1377 if (len < 0) {
1378 switch (errno) {
1379#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1380 case EWOULDBLOCK:
1381#endif
1382 case EAGAIN:
1383 (void)poll(&pfd, 1, -1);
1384 /* FALLTHROUGH */
1385 case EINTR:
1386 continue;
1387 default:
1388 return -1;
1389 }
1390 }
1391 if (len == 0) {
1392 errno = EPIPE;
1393 return -1;
1394 }
1395 have += (u_int)len;
1396 }
1397 return 0;
1398}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001399
Damien Millere1537f92010-01-26 13:26:22 +11001400static int
1401mux_client_write_packet(int fd, Buffer *m)
1402{
1403 Buffer queue;
1404 u_int have, need;
1405 int oerrno, len;
1406 u_char *ptr;
1407 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001408
Damien Millere1537f92010-01-26 13:26:22 +11001409 pfd.fd = fd;
1410 pfd.events = POLLOUT;
1411 buffer_init(&queue);
1412 buffer_put_string(&queue, buffer_ptr(m), buffer_len(m));
1413
1414 need = buffer_len(&queue);
1415 ptr = buffer_ptr(&queue);
1416
1417 for (have = 0; have < need; ) {
1418 if (muxclient_terminate) {
1419 buffer_free(&queue);
1420 errno = EINTR;
1421 return -1;
1422 }
1423 len = write(fd, ptr + have, need - have);
1424 if (len < 0) {
1425 switch (errno) {
1426#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1427 case EWOULDBLOCK:
1428#endif
1429 case EAGAIN:
1430 (void)poll(&pfd, 1, -1);
1431 /* FALLTHROUGH */
1432 case EINTR:
1433 continue;
1434 default:
1435 oerrno = errno;
1436 buffer_free(&queue);
1437 errno = oerrno;
1438 return -1;
1439 }
1440 }
1441 if (len == 0) {
1442 buffer_free(&queue);
1443 errno = EPIPE;
1444 return -1;
1445 }
1446 have += (u_int)len;
1447 }
1448 buffer_free(&queue);
1449 return 0;
1450}
1451
1452static int
1453mux_client_read_packet(int fd, Buffer *m)
1454{
1455 Buffer queue;
1456 u_int need, have;
Damien Miller633de332014-05-15 13:48:26 +10001457 const u_char *ptr;
Damien Millere1537f92010-01-26 13:26:22 +11001458 int oerrno;
1459
1460 buffer_init(&queue);
1461 if (mux_client_read(fd, &queue, 4) != 0) {
1462 if ((oerrno = errno) == EPIPE)
Darren Tucker746e9062013-06-06 08:20:13 +10001463 debug3("%s: read header failed: %s", __func__,
1464 strerror(errno));
1465 buffer_free(&queue);
Damien Millere1537f92010-01-26 13:26:22 +11001466 errno = oerrno;
1467 return -1;
1468 }
1469 need = get_u32(buffer_ptr(&queue));
1470 if (mux_client_read(fd, &queue, need) != 0) {
1471 oerrno = errno;
1472 debug3("%s: read body failed: %s", __func__, strerror(errno));
Darren Tucker746e9062013-06-06 08:20:13 +10001473 buffer_free(&queue);
Damien Millere1537f92010-01-26 13:26:22 +11001474 errno = oerrno;
1475 return -1;
1476 }
1477 ptr = buffer_get_string_ptr(&queue, &have);
1478 buffer_append(m, ptr, have);
1479 buffer_free(&queue);
1480 return 0;
1481}
1482
1483static int
1484mux_client_hello_exchange(int fd)
1485{
1486 Buffer m;
1487 u_int type, ver;
1488
1489 buffer_init(&m);
1490 buffer_put_int(&m, MUX_MSG_HELLO);
1491 buffer_put_int(&m, SSHMUX_VER);
1492 /* no extensions */
1493
1494 if (mux_client_write_packet(fd, &m) != 0)
1495 fatal("%s: write packet: %s", __func__, strerror(errno));
1496
1497 buffer_clear(&m);
1498
1499 /* Read their HELLO */
1500 if (mux_client_read_packet(fd, &m) != 0) {
1501 buffer_free(&m);
1502 return -1;
1503 }
1504
1505 type = buffer_get_int(&m);
1506 if (type != MUX_MSG_HELLO)
1507 fatal("%s: expected HELLO (%u) received %u",
1508 __func__, MUX_MSG_HELLO, type);
1509 ver = buffer_get_int(&m);
1510 if (ver != SSHMUX_VER)
1511 fatal("Unsupported multiplexing protocol version %d "
1512 "(expected %d)", ver, SSHMUX_VER);
1513 debug2("%s: master version %u", __func__, ver);
1514 /* No extensions are presently defined */
1515 while (buffer_len(&m) > 0) {
1516 char *name = buffer_get_string(&m, NULL);
1517 char *value = buffer_get_string(&m, NULL);
1518
1519 debug2("Unrecognised master extension \"%s\"", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001520 free(name);
1521 free(value);
Damien Millere1537f92010-01-26 13:26:22 +11001522 }
1523 buffer_free(&m);
1524 return 0;
1525}
1526
1527static u_int
1528mux_client_request_alive(int fd)
1529{
1530 Buffer m;
1531 char *e;
1532 u_int pid, type, rid;
1533
1534 debug3("%s: entering", __func__);
1535
1536 buffer_init(&m);
1537 buffer_put_int(&m, MUX_C_ALIVE_CHECK);
1538 buffer_put_int(&m, muxclient_request_id);
1539
1540 if (mux_client_write_packet(fd, &m) != 0)
1541 fatal("%s: write packet: %s", __func__, strerror(errno));
1542
1543 buffer_clear(&m);
1544
1545 /* Read their reply */
1546 if (mux_client_read_packet(fd, &m) != 0) {
1547 buffer_free(&m);
1548 return 0;
1549 }
1550
1551 type = buffer_get_int(&m);
1552 if (type != MUX_S_ALIVE) {
1553 e = buffer_get_string(&m, NULL);
1554 fatal("%s: master returned error: %s", __func__, e);
1555 }
1556
1557 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1558 fatal("%s: out of sequence reply: my id %u theirs %u",
1559 __func__, muxclient_request_id, rid);
1560 pid = buffer_get_int(&m);
1561 buffer_free(&m);
1562
1563 debug3("%s: done pid = %u", __func__, pid);
1564
1565 muxclient_request_id++;
1566
1567 return pid;
1568}
1569
1570static void
1571mux_client_request_terminate(int fd)
1572{
1573 Buffer m;
1574 char *e;
1575 u_int type, rid;
1576
1577 debug3("%s: entering", __func__);
1578
1579 buffer_init(&m);
1580 buffer_put_int(&m, MUX_C_TERMINATE);
1581 buffer_put_int(&m, muxclient_request_id);
1582
1583 if (mux_client_write_packet(fd, &m) != 0)
1584 fatal("%s: write packet: %s", __func__, strerror(errno));
1585
1586 buffer_clear(&m);
1587
1588 /* Read their reply */
1589 if (mux_client_read_packet(fd, &m) != 0) {
1590 /* Remote end exited already */
1591 if (errno == EPIPE) {
1592 buffer_free(&m);
1593 return;
1594 }
1595 fatal("%s: read from master failed: %s",
1596 __func__, strerror(errno));
1597 }
1598
1599 type = buffer_get_int(&m);
1600 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1601 fatal("%s: out of sequence reply: my id %u theirs %u",
1602 __func__, muxclient_request_id, rid);
1603 switch (type) {
1604 case MUX_S_OK:
1605 break;
1606 case MUX_S_PERMISSION_DENIED:
1607 e = buffer_get_string(&m, NULL);
1608 fatal("Master refused termination request: %s", e);
1609 case MUX_S_FAILURE:
1610 e = buffer_get_string(&m, NULL);
1611 fatal("%s: termination request failed: %s", __func__, e);
1612 default:
1613 fatal("%s: unexpected response from master 0x%08x",
1614 __func__, type);
1615 }
1616 buffer_free(&m);
1617 muxclient_request_id++;
1618}
1619
1620static int
Damien Miller7acefbb2014-07-18 14:11:24 +10001621mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd)
Damien Millere1537f92010-01-26 13:26:22 +11001622{
1623 Buffer m;
1624 char *e, *fwd_desc;
1625 u_int type, rid;
1626
1627 fwd_desc = format_forward(ftype, fwd);
Damien Millerf6dff7c2011-09-22 21:38:52 +10001628 debug("Requesting %s %s",
1629 cancel_flag ? "cancellation of" : "forwarding of", fwd_desc);
Darren Tuckera627d422013-06-02 07:31:17 +10001630 free(fwd_desc);
Damien Millere1537f92010-01-26 13:26:22 +11001631
1632 buffer_init(&m);
Damien Millerf6dff7c2011-09-22 21:38:52 +10001633 buffer_put_int(&m, cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD);
Damien Millere1537f92010-01-26 13:26:22 +11001634 buffer_put_int(&m, muxclient_request_id);
1635 buffer_put_int(&m, ftype);
Damien Miller7acefbb2014-07-18 14:11:24 +10001636 if (fwd->listen_path != NULL) {
1637 buffer_put_cstring(&m, fwd->listen_path);
1638 } else {
1639 buffer_put_cstring(&m,
1640 fwd->listen_host == NULL ? "" : fwd->listen_host);
1641 }
Damien Millere1537f92010-01-26 13:26:22 +11001642 buffer_put_int(&m, fwd->listen_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10001643 if (fwd->connect_path != NULL) {
1644 buffer_put_cstring(&m, fwd->connect_path);
1645 } else {
1646 buffer_put_cstring(&m,
1647 fwd->connect_host == NULL ? "" : fwd->connect_host);
1648 }
Damien Millere1537f92010-01-26 13:26:22 +11001649 buffer_put_int(&m, fwd->connect_port);
1650
1651 if (mux_client_write_packet(fd, &m) != 0)
1652 fatal("%s: write packet: %s", __func__, strerror(errno));
1653
1654 buffer_clear(&m);
1655
1656 /* Read their reply */
1657 if (mux_client_read_packet(fd, &m) != 0) {
1658 buffer_free(&m);
1659 return -1;
1660 }
1661
1662 type = buffer_get_int(&m);
1663 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1664 fatal("%s: out of sequence reply: my id %u theirs %u",
1665 __func__, muxclient_request_id, rid);
1666 switch (type) {
1667 case MUX_S_OK:
1668 break;
Damien Miller388f6fc2010-05-21 14:57:35 +10001669 case MUX_S_REMOTE_PORT:
Damien Millerf6dff7c2011-09-22 21:38:52 +10001670 if (cancel_flag)
1671 fatal("%s: got MUX_S_REMOTE_PORT for cancel", __func__);
Damien Miller388f6fc2010-05-21 14:57:35 +10001672 fwd->allocated_port = buffer_get_int(&m);
1673 logit("Allocated port %u for remote forward to %s:%d",
1674 fwd->allocated_port,
1675 fwd->connect_host ? fwd->connect_host : "",
1676 fwd->connect_port);
1677 if (muxclient_command == SSHMUX_COMMAND_FORWARD)
1678 fprintf(stdout, "%u\n", fwd->allocated_port);
1679 break;
Damien Millere1537f92010-01-26 13:26:22 +11001680 case MUX_S_PERMISSION_DENIED:
1681 e = buffer_get_string(&m, NULL);
1682 buffer_free(&m);
1683 error("Master refused forwarding request: %s", e);
1684 return -1;
1685 case MUX_S_FAILURE:
1686 e = buffer_get_string(&m, NULL);
1687 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001688 error("%s: forwarding request failed: %s", __func__, e);
Damien Millere1537f92010-01-26 13:26:22 +11001689 return -1;
1690 default:
1691 fatal("%s: unexpected response from master 0x%08x",
1692 __func__, type);
1693 }
1694 buffer_free(&m);
1695
1696 muxclient_request_id++;
1697 return 0;
1698}
1699
1700static int
Damien Millerf6dff7c2011-09-22 21:38:52 +10001701mux_client_forwards(int fd, int cancel_flag)
Damien Millere1537f92010-01-26 13:26:22 +11001702{
Damien Millerf6dff7c2011-09-22 21:38:52 +10001703 int i, ret = 0;
Damien Millere1537f92010-01-26 13:26:22 +11001704
Damien Millerf6dff7c2011-09-22 21:38:52 +10001705 debug3("%s: %s forwardings: %d local, %d remote", __func__,
1706 cancel_flag ? "cancel" : "request",
Damien Millere1537f92010-01-26 13:26:22 +11001707 options.num_local_forwards, options.num_remote_forwards);
1708
1709 /* XXX ExitOnForwardingFailure */
1710 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10001711 if (mux_client_forward(fd, cancel_flag,
Damien Millere1537f92010-01-26 13:26:22 +11001712 options.local_forwards[i].connect_port == 0 ?
1713 MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
1714 options.local_forwards + i) != 0)
Damien Millerf6dff7c2011-09-22 21:38:52 +10001715 ret = -1;
Damien Millere1537f92010-01-26 13:26:22 +11001716 }
1717 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10001718 if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE,
Damien Millere1537f92010-01-26 13:26:22 +11001719 options.remote_forwards + i) != 0)
Damien Millerf6dff7c2011-09-22 21:38:52 +10001720 ret = -1;
Damien Millere1537f92010-01-26 13:26:22 +11001721 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10001722 return ret;
Damien Millere1537f92010-01-26 13:26:22 +11001723}
1724
1725static int
1726mux_client_request_session(int fd)
1727{
1728 Buffer m;
1729 char *e, *term;
1730 u_int i, rid, sid, esid, exitval, type, exitval_seen;
1731 extern char **environ;
Damien Miller555f3b82011-05-15 08:48:05 +10001732 int devnull, rawmode;
Damien Millere1537f92010-01-26 13:26:22 +11001733
1734 debug3("%s: entering", __func__);
1735
1736 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1737 error("%s: master alive request failed", __func__);
1738 return -1;
1739 }
1740
1741 signal(SIGPIPE, SIG_IGN);
1742
1743 if (stdin_null_flag) {
1744 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1745 fatal("open(/dev/null): %s", strerror(errno));
1746 if (dup2(devnull, STDIN_FILENO) == -1)
1747 fatal("dup2: %s", strerror(errno));
1748 if (devnull > STDERR_FILENO)
1749 close(devnull);
1750 }
1751
1752 term = getenv("TERM");
1753
1754 buffer_init(&m);
1755 buffer_put_int(&m, MUX_C_NEW_SESSION);
1756 buffer_put_int(&m, muxclient_request_id);
1757 buffer_put_cstring(&m, ""); /* reserved */
1758 buffer_put_int(&m, tty_flag);
1759 buffer_put_int(&m, options.forward_x11);
1760 buffer_put_int(&m, options.forward_agent);
1761 buffer_put_int(&m, subsystem_flag);
1762 buffer_put_int(&m, options.escape_char == SSH_ESCAPECHAR_NONE ?
1763 0xffffffff : (u_int)options.escape_char);
1764 buffer_put_cstring(&m, term == NULL ? "" : term);
1765 buffer_put_string(&m, buffer_ptr(&command), buffer_len(&command));
1766
1767 if (options.num_send_env > 0 && environ != NULL) {
1768 /* Pass environment */
1769 for (i = 0; environ[i] != NULL; i++) {
1770 if (env_permitted(environ[i])) {
1771 buffer_put_cstring(&m, environ[i]);
1772 }
1773 }
1774 }
1775
1776 if (mux_client_write_packet(fd, &m) != 0)
1777 fatal("%s: write packet: %s", __func__, strerror(errno));
1778
1779 /* Send the stdio file descriptors */
1780 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1781 mm_send_fd(fd, STDOUT_FILENO) == -1 ||
1782 mm_send_fd(fd, STDERR_FILENO) == -1)
1783 fatal("%s: send fds failed", __func__);
1784
1785 debug3("%s: session request sent", __func__);
1786
1787 /* Read their reply */
1788 buffer_clear(&m);
1789 if (mux_client_read_packet(fd, &m) != 0) {
1790 error("%s: read from master failed: %s",
1791 __func__, strerror(errno));
1792 buffer_free(&m);
1793 return -1;
1794 }
1795
1796 type = buffer_get_int(&m);
1797 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1798 fatal("%s: out of sequence reply: my id %u theirs %u",
1799 __func__, muxclient_request_id, rid);
1800 switch (type) {
1801 case MUX_S_SESSION_OPENED:
1802 sid = buffer_get_int(&m);
1803 debug("%s: master session id: %u", __func__, sid);
1804 break;
1805 case MUX_S_PERMISSION_DENIED:
1806 e = buffer_get_string(&m, NULL);
1807 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001808 error("Master refused session request: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11001809 return -1;
1810 case MUX_S_FAILURE:
1811 e = buffer_get_string(&m, NULL);
1812 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001813 error("%s: session request failed: %s", __func__, e);
Damien Millere1537f92010-01-26 13:26:22 +11001814 return -1;
1815 default:
1816 buffer_free(&m);
1817 error("%s: unexpected response from master 0x%08x",
1818 __func__, type);
1819 return -1;
1820 }
1821 muxclient_request_id++;
1822
1823 signal(SIGHUP, control_client_sighandler);
1824 signal(SIGINT, control_client_sighandler);
1825 signal(SIGTERM, control_client_sighandler);
1826 signal(SIGWINCH, control_client_sigrelay);
1827
Damien Miller555f3b82011-05-15 08:48:05 +10001828 rawmode = tty_flag;
Damien Millere1537f92010-01-26 13:26:22 +11001829 if (tty_flag)
Damien Miller21771e22011-05-15 08:45:50 +10001830 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millere1537f92010-01-26 13:26:22 +11001831
1832 /*
1833 * Stick around until the controlee closes the client_fd.
1834 * Before it does, it is expected to write an exit message.
1835 * This process must read the value and wait for the closure of
1836 * the client_fd; if this one closes early, the multiplex master will
1837 * terminate early too (possibly losing data).
1838 */
1839 for (exitval = 255, exitval_seen = 0;;) {
1840 buffer_clear(&m);
1841 if (mux_client_read_packet(fd, &m) != 0)
1842 break;
1843 type = buffer_get_int(&m);
Damien Miller555f3b82011-05-15 08:48:05 +10001844 switch (type) {
1845 case MUX_S_TTY_ALLOC_FAIL:
1846 if ((esid = buffer_get_int(&m)) != sid)
1847 fatal("%s: tty alloc fail on unknown session: "
1848 "my id %u theirs %u",
1849 __func__, sid, esid);
1850 leave_raw_mode(options.request_tty ==
1851 REQUEST_TTY_FORCE);
1852 rawmode = 0;
1853 continue;
1854 case MUX_S_EXIT_MESSAGE:
1855 if ((esid = buffer_get_int(&m)) != sid)
1856 fatal("%s: exit on unknown session: "
1857 "my id %u theirs %u",
1858 __func__, sid, esid);
1859 if (exitval_seen)
1860 fatal("%s: exitval sent twice", __func__);
1861 exitval = buffer_get_int(&m);
1862 exitval_seen = 1;
1863 continue;
1864 default:
Damien Millere1537f92010-01-26 13:26:22 +11001865 e = buffer_get_string(&m, NULL);
1866 fatal("%s: master returned error: %s", __func__, e);
1867 }
Damien Millere1537f92010-01-26 13:26:22 +11001868 }
1869
1870 close(fd);
Damien Miller555f3b82011-05-15 08:48:05 +10001871 if (rawmode)
1872 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millere1537f92010-01-26 13:26:22 +11001873
1874 if (muxclient_terminate) {
1875 debug2("Exiting on signal %d", muxclient_terminate);
1876 exitval = 255;
1877 } else if (!exitval_seen) {
1878 debug2("Control master terminated unexpectedly");
1879 exitval = 255;
1880 } else
1881 debug2("Received exit status from master %d", exitval);
1882
1883 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1884 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
1885
1886 exit(exitval);
1887}
1888
1889static int
1890mux_client_request_stdio_fwd(int fd)
1891{
1892 Buffer m;
1893 char *e;
1894 u_int type, rid, sid;
1895 int devnull;
1896
1897 debug3("%s: entering", __func__);
1898
1899 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1900 error("%s: master alive request failed", __func__);
1901 return -1;
1902 }
1903
1904 signal(SIGPIPE, SIG_IGN);
1905
1906 if (stdin_null_flag) {
1907 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1908 fatal("open(/dev/null): %s", strerror(errno));
1909 if (dup2(devnull, STDIN_FILENO) == -1)
1910 fatal("dup2: %s", strerror(errno));
1911 if (devnull > STDERR_FILENO)
1912 close(devnull);
1913 }
1914
1915 buffer_init(&m);
1916 buffer_put_int(&m, MUX_C_NEW_STDIO_FWD);
1917 buffer_put_int(&m, muxclient_request_id);
1918 buffer_put_cstring(&m, ""); /* reserved */
1919 buffer_put_cstring(&m, stdio_forward_host);
1920 buffer_put_int(&m, stdio_forward_port);
1921
1922 if (mux_client_write_packet(fd, &m) != 0)
1923 fatal("%s: write packet: %s", __func__, strerror(errno));
1924
1925 /* Send the stdio file descriptors */
1926 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1927 mm_send_fd(fd, STDOUT_FILENO) == -1)
1928 fatal("%s: send fds failed", __func__);
1929
1930 debug3("%s: stdio forward request sent", __func__);
1931
1932 /* Read their reply */
1933 buffer_clear(&m);
1934
1935 if (mux_client_read_packet(fd, &m) != 0) {
1936 error("%s: read from master failed: %s",
1937 __func__, strerror(errno));
1938 buffer_free(&m);
1939 return -1;
1940 }
1941
1942 type = buffer_get_int(&m);
1943 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1944 fatal("%s: out of sequence reply: my id %u theirs %u",
1945 __func__, muxclient_request_id, rid);
1946 switch (type) {
1947 case MUX_S_SESSION_OPENED:
1948 sid = buffer_get_int(&m);
1949 debug("%s: master session id: %u", __func__, sid);
1950 break;
1951 case MUX_S_PERMISSION_DENIED:
1952 e = buffer_get_string(&m, NULL);
1953 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001954 fatal("Master refused stdio forwarding request: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11001955 case MUX_S_FAILURE:
1956 e = buffer_get_string(&m, NULL);
1957 buffer_free(&m);
1958 fatal("%s: stdio forwarding request failed: %s", __func__, e);
1959 default:
1960 buffer_free(&m);
1961 error("%s: unexpected response from master 0x%08x",
1962 __func__, type);
1963 return -1;
1964 }
1965 muxclient_request_id++;
1966
1967 signal(SIGHUP, control_client_sighandler);
1968 signal(SIGINT, control_client_sighandler);
1969 signal(SIGTERM, control_client_sighandler);
1970 signal(SIGWINCH, control_client_sigrelay);
1971
1972 /*
1973 * Stick around until the controlee closes the client_fd.
1974 */
1975 buffer_clear(&m);
1976 if (mux_client_read_packet(fd, &m) != 0) {
1977 if (errno == EPIPE ||
1978 (errno == EINTR && muxclient_terminate != 0))
1979 return 0;
1980 fatal("%s: mux_client_read_packet: %s",
1981 __func__, strerror(errno));
1982 }
1983 fatal("%s: master returned unexpected message %u", __func__, type);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001984}
1985
Damien Miller6c3eec72011-05-05 14:16:22 +10001986static void
1987mux_client_request_stop_listening(int fd)
1988{
1989 Buffer m;
1990 char *e;
1991 u_int type, rid;
1992
1993 debug3("%s: entering", __func__);
1994
1995 buffer_init(&m);
1996 buffer_put_int(&m, MUX_C_STOP_LISTENING);
1997 buffer_put_int(&m, muxclient_request_id);
1998
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 fatal("%s: read from master failed: %s",
2007 __func__, strerror(errno));
2008
2009 type = buffer_get_int(&m);
2010 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
2011 fatal("%s: out of sequence reply: my id %u theirs %u",
2012 __func__, muxclient_request_id, rid);
2013 switch (type) {
2014 case MUX_S_OK:
2015 break;
2016 case MUX_S_PERMISSION_DENIED:
2017 e = buffer_get_string(&m, NULL);
2018 fatal("Master refused stop listening request: %s", e);
2019 case MUX_S_FAILURE:
2020 e = buffer_get_string(&m, NULL);
2021 fatal("%s: stop listening request failed: %s", __func__, e);
2022 default:
2023 fatal("%s: unexpected response from master 0x%08x",
2024 __func__, type);
2025 }
2026 buffer_free(&m);
2027 muxclient_request_id++;
2028}
2029
Damien Millerb1cbfa22008-05-19 16:00:08 +10002030/* Multiplex client main loop. */
2031void
2032muxclient(const char *path)
2033{
2034 struct sockaddr_un addr;
Damien Millere1537f92010-01-26 13:26:22 +11002035 socklen_t sun_len;
2036 int sock;
2037 u_int pid;
Damien Millerb1cbfa22008-05-19 16:00:08 +10002038
Damien Millere1537f92010-01-26 13:26:22 +11002039 if (muxclient_command == 0) {
2040 if (stdio_forward_host != NULL)
2041 muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
2042 else
2043 muxclient_command = SSHMUX_COMMAND_OPEN;
2044 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002045
2046 switch (options.control_master) {
2047 case SSHCTL_MASTER_AUTO:
2048 case SSHCTL_MASTER_AUTO_ASK:
2049 debug("auto-mux: Trying existing master");
2050 /* FALLTHROUGH */
2051 case SSHCTL_MASTER_NO:
2052 break;
2053 default:
2054 return;
2055 }
2056
2057 memset(&addr, '\0', sizeof(addr));
2058 addr.sun_family = AF_UNIX;
Damien Millere1537f92010-01-26 13:26:22 +11002059 sun_len = offsetof(struct sockaddr_un, sun_path) +
Damien Millerb1cbfa22008-05-19 16:00:08 +10002060 strlen(path) + 1;
2061
2062 if (strlcpy(addr.sun_path, path,
2063 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
2064 fatal("ControlPath too long");
2065
2066 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
2067 fatal("%s socket(): %s", __func__, strerror(errno));
2068
Damien Millere1537f92010-01-26 13:26:22 +11002069 if (connect(sock, (struct sockaddr *)&addr, sun_len) == -1) {
2070 switch (muxclient_command) {
2071 case SSHMUX_COMMAND_OPEN:
2072 case SSHMUX_COMMAND_STDIO_FWD:
2073 break;
2074 default:
Damien Millerb1cbfa22008-05-19 16:00:08 +10002075 fatal("Control socket connect(%.100s): %s", path,
2076 strerror(errno));
2077 }
Damien Miller603134e2010-09-24 22:07:55 +10002078 if (errno == ECONNREFUSED &&
2079 options.control_master != SSHCTL_MASTER_NO) {
2080 debug("Stale control socket %.100s, unlinking", path);
2081 unlink(path);
2082 } else if (errno == ENOENT) {
Damien Millerb1cbfa22008-05-19 16:00:08 +10002083 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller603134e2010-09-24 22:07:55 +10002084 } else {
Damien Millerb1cbfa22008-05-19 16:00:08 +10002085 error("Control socket connect(%.100s): %s", path,
2086 strerror(errno));
2087 }
2088 close(sock);
2089 return;
2090 }
Damien Millere1537f92010-01-26 13:26:22 +11002091 set_nonblock(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002092
Damien Millere1537f92010-01-26 13:26:22 +11002093 if (mux_client_hello_exchange(sock) != 0) {
2094 error("%s: master hello exchange failed", __func__);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10002095 close(sock);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10002096 return;
2097 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002098
2099 switch (muxclient_command) {
2100 case SSHMUX_COMMAND_ALIVE_CHECK:
Damien Millere1537f92010-01-26 13:26:22 +11002101 if ((pid = mux_client_request_alive(sock)) == 0)
2102 fatal("%s: master alive check failed", __func__);
2103 fprintf(stderr, "Master running (pid=%d)\r\n", pid);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002104 exit(0);
2105 case SSHMUX_COMMAND_TERMINATE:
Damien Millere1537f92010-01-26 13:26:22 +11002106 mux_client_request_terminate(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002107 fprintf(stderr, "Exit request sent.\r\n");
2108 exit(0);
Damien Miller388f6fc2010-05-21 14:57:35 +10002109 case SSHMUX_COMMAND_FORWARD:
Damien Millerf6dff7c2011-09-22 21:38:52 +10002110 if (mux_client_forwards(sock, 0) != 0)
Damien Miller388f6fc2010-05-21 14:57:35 +10002111 fatal("%s: master forward request failed", __func__);
2112 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002113 case SSHMUX_COMMAND_OPEN:
Damien Millerf6dff7c2011-09-22 21:38:52 +10002114 if (mux_client_forwards(sock, 0) != 0) {
Damien Millere1537f92010-01-26 13:26:22 +11002115 error("%s: master forward request failed", __func__);
2116 return;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10002117 }
Damien Millere1537f92010-01-26 13:26:22 +11002118 mux_client_request_session(sock);
2119 return;
2120 case SSHMUX_COMMAND_STDIO_FWD:
2121 mux_client_request_stdio_fwd(sock);
2122 exit(0);
Damien Miller6c3eec72011-05-05 14:16:22 +10002123 case SSHMUX_COMMAND_STOP:
2124 mux_client_request_stop_listening(sock);
2125 fprintf(stderr, "Stop listening request sent.\r\n");
2126 exit(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10002127 case SSHMUX_COMMAND_CANCEL_FWD:
2128 if (mux_client_forwards(sock, 1) != 0)
2129 error("%s: master cancel forward request failed",
2130 __func__);
2131 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002132 default:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10002133 fatal("unrecognised muxclient_command %d", muxclient_command);
Damien Millerb1cbfa22008-05-19 16:00:08 +10002134 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10002135}