blob: 09468359fcbc9a2a1bb547a0ca8c0d1a2bbe1230 [file] [log] [blame]
Damien Miller6c3eec72011-05-05 14:16:22 +10001/* $OpenBSD: mux.c,v 1.25 2011/04/17 22:42:41 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
66#ifdef HAVE_LIBUTIL_H
67# include <libutil.h>
68#endif
69
Damien Millerb1cbfa22008-05-19 16:00:08 +100070#include "openbsd-compat/sys-queue.h"
71#include "xmalloc.h"
72#include "log.h"
73#include "ssh.h"
Damien Miller388f6fc2010-05-21 14:57:35 +100074#include "ssh2.h"
Damien Millerb1cbfa22008-05-19 16:00:08 +100075#include "pathnames.h"
76#include "misc.h"
77#include "match.h"
78#include "buffer.h"
79#include "channels.h"
80#include "msg.h"
81#include "packet.h"
82#include "monitor_fdpass.h"
83#include "sshpty.h"
84#include "key.h"
85#include "readconf.h"
86#include "clientloop.h"
87
88/* from ssh.c */
89extern int tty_flag;
Darren Tucker37c1b3d2010-01-09 22:26:23 +110090extern int force_tty_flag;
Damien Millerb1cbfa22008-05-19 16:00:08 +100091extern Options options;
92extern int stdin_null_flag;
93extern char *host;
Darren Tucker8ec4fd82009-10-07 08:39:57 +110094extern int subsystem_flag;
Damien Millerb1cbfa22008-05-19 16:00:08 +100095extern Buffer command;
Damien Millere1537f92010-01-26 13:26:22 +110096extern volatile sig_atomic_t quit_pending;
97extern char *stdio_forward_host;
98extern int stdio_forward_port;
Damien Millerb1cbfa22008-05-19 16:00:08 +100099
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000100/* Context for session open confirmation callback */
101struct mux_session_confirm_ctx {
Damien Millere1537f92010-01-26 13:26:22 +1100102 u_int want_tty;
103 u_int want_subsys;
104 u_int want_x_fwd;
105 u_int want_agent_fwd;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000106 Buffer cmd;
107 char *term;
108 struct termios tio;
109 char **env;
Damien Millerd530f5f2010-05-21 14:57:10 +1000110 u_int rid;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000111};
112
Damien Miller388f6fc2010-05-21 14:57:35 +1000113/* Context for global channel callback */
114struct mux_channel_confirm_ctx {
115 u_int cid; /* channel id */
116 u_int rid; /* request id */
117 int fid; /* forward id */
118};
119
Damien Millerb1cbfa22008-05-19 16:00:08 +1000120/* fd to control socket */
121int muxserver_sock = -1;
122
Damien Millere1537f92010-01-26 13:26:22 +1100123/* client request id */
124u_int muxclient_request_id = 0;
125
Damien Millerb1cbfa22008-05-19 16:00:08 +1000126/* Multiplexing control command */
127u_int muxclient_command = 0;
128
129/* Set when signalled. */
130static volatile sig_atomic_t muxclient_terminate = 0;
131
132/* PID of multiplex server */
133static u_int muxserver_pid = 0;
134
Damien Millere1537f92010-01-26 13:26:22 +1100135static Channel *mux_listener_channel = NULL;
Damien Millerb1cbfa22008-05-19 16:00:08 +1000136
Damien Millere1537f92010-01-26 13:26:22 +1100137struct mux_master_state {
138 int hello_rcvd;
139};
140
141/* mux protocol messages */
142#define MUX_MSG_HELLO 0x00000001
143#define MUX_C_NEW_SESSION 0x10000002
144#define MUX_C_ALIVE_CHECK 0x10000004
145#define MUX_C_TERMINATE 0x10000005
146#define MUX_C_OPEN_FWD 0x10000006
147#define MUX_C_CLOSE_FWD 0x10000007
148#define MUX_C_NEW_STDIO_FWD 0x10000008
Damien Miller6c3eec72011-05-05 14:16:22 +1000149#define MUX_C_STOP_LISTENING 0x10000009
Damien Millere1537f92010-01-26 13:26:22 +1100150#define MUX_S_OK 0x80000001
151#define MUX_S_PERMISSION_DENIED 0x80000002
152#define MUX_S_FAILURE 0x80000003
153#define MUX_S_EXIT_MESSAGE 0x80000004
154#define MUX_S_ALIVE 0x80000005
155#define MUX_S_SESSION_OPENED 0x80000006
Damien Miller388f6fc2010-05-21 14:57:35 +1000156#define MUX_S_REMOTE_PORT 0x80000007
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 Millere1537f92010-01-26 13:26:22 +1100164
165static int process_mux_master_hello(u_int, Channel *, Buffer *, Buffer *);
166static int process_mux_new_session(u_int, Channel *, Buffer *, Buffer *);
167static int process_mux_alive_check(u_int, Channel *, Buffer *, Buffer *);
168static int process_mux_terminate(u_int, Channel *, Buffer *, Buffer *);
169static int process_mux_open_fwd(u_int, Channel *, Buffer *, Buffer *);
170static int process_mux_close_fwd(u_int, Channel *, Buffer *, Buffer *);
171static int process_mux_stdio_fwd(u_int, Channel *, Buffer *, Buffer *);
Damien Miller6c3eec72011-05-05 14:16:22 +1000172static int process_mux_stop_listening(u_int, Channel *, Buffer *, Buffer *);
Damien Millere1537f92010-01-26 13:26:22 +1100173
174static const struct {
175 u_int type;
176 int (*handler)(u_int, Channel *, Buffer *, Buffer *);
177} mux_master_handlers[] = {
178 { MUX_MSG_HELLO, process_mux_master_hello },
179 { MUX_C_NEW_SESSION, process_mux_new_session },
180 { MUX_C_ALIVE_CHECK, process_mux_alive_check },
181 { MUX_C_TERMINATE, process_mux_terminate },
182 { MUX_C_OPEN_FWD, process_mux_open_fwd },
183 { MUX_C_CLOSE_FWD, process_mux_close_fwd },
184 { MUX_C_NEW_STDIO_FWD, process_mux_stdio_fwd },
Damien Miller6c3eec72011-05-05 14:16:22 +1000185 { MUX_C_STOP_LISTENING, process_mux_stop_listening },
Damien Millere1537f92010-01-26 13:26:22 +1100186 { 0, NULL }
187};
188
189/* Cleanup callback fired on closure of mux slave _session_ channel */
190/* ARGSUSED */
191static void
192mux_master_session_cleanup_cb(int cid, void *unused)
193{
194 Channel *cc, *c = channel_by_id(cid);
195
196 debug3("%s: entering for channel %d", __func__, cid);
197 if (c == NULL)
198 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
199 if (c->ctl_chan != -1) {
200 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
201 fatal("%s: channel %d missing control channel %d",
202 __func__, c->self, c->ctl_chan);
203 c->ctl_chan = -1;
204 cc->remote_id = -1;
205 chan_rcvd_oclose(cc);
206 }
207 channel_cancel_cleanup(c->self);
208}
209
210/* Cleanup callback fired on closure of mux slave _control_ channel */
211/* ARGSUSED */
212static void
213mux_master_control_cleanup_cb(int cid, void *unused)
214{
215 Channel *sc, *c = channel_by_id(cid);
216
217 debug3("%s: entering for channel %d", __func__, cid);
218 if (c == NULL)
219 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
220 if (c->remote_id != -1) {
221 if ((sc = channel_by_id(c->remote_id)) == NULL)
Damien Miller601a23c2010-04-16 15:54:01 +1000222 fatal("%s: channel %d missing session channel %d",
Damien Millere1537f92010-01-26 13:26:22 +1100223 __func__, c->self, c->remote_id);
224 c->remote_id = -1;
225 sc->ctl_chan = -1;
Damien Millera21cdfa2010-01-28 06:26:59 +1100226 if (sc->type != SSH_CHANNEL_OPEN) {
227 debug2("%s: channel %d: not open", __func__, sc->self);
Damien Miller133d9d32010-01-30 17:30:04 +1100228 chan_mark_dead(sc);
Damien Millera21cdfa2010-01-28 06:26:59 +1100229 } else {
Damien Miller0dac03f2010-01-30 17:36:33 +1100230 if (sc->istate == CHAN_INPUT_OPEN)
231 chan_read_failed(sc);
232 if (sc->ostate == CHAN_OUTPUT_OPEN)
233 chan_write_failed(sc);
Damien Millera21cdfa2010-01-28 06:26:59 +1100234 }
Damien Millere1537f92010-01-26 13:26:22 +1100235 }
236 channel_cancel_cleanup(c->self);
237}
238
239/* Check mux client environment variables before passing them to mux master. */
240static int
241env_permitted(char *env)
242{
243 int i, ret;
244 char name[1024], *cp;
245
246 if ((cp = strchr(env, '=')) == NULL || cp == env)
247 return 0;
248 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
249 if (ret <= 0 || (size_t)ret >= sizeof(name)) {
250 error("env_permitted: name '%.100s...' too long", env);
251 return 0;
252 }
253
254 for (i = 0; i < options.num_send_env; i++)
255 if (match_pattern(name, options.send_env[i]))
256 return 1;
257
258 return 0;
259}
260
261/* Mux master protocol message handlers */
262
263static int
264process_mux_master_hello(u_int rid, Channel *c, Buffer *m, Buffer *r)
265{
266 u_int ver;
267 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
268
269 if (state == NULL)
270 fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self);
271 if (state->hello_rcvd) {
272 error("%s: HELLO received twice", __func__);
273 return -1;
274 }
275 if (buffer_get_int_ret(&ver, m) != 0) {
276 malf:
277 error("%s: malformed message", __func__);
278 return -1;
279 }
280 if (ver != SSHMUX_VER) {
281 error("Unsupported multiplexing protocol version %d "
282 "(expected %d)", ver, SSHMUX_VER);
283 return -1;
284 }
285 debug2("%s: channel %d slave version %u", __func__, c->self, ver);
286
287 /* No extensions are presently defined */
288 while (buffer_len(m) > 0) {
289 char *name = buffer_get_string_ret(m, NULL);
290 char *value = buffer_get_string_ret(m, NULL);
291
292 if (name == NULL || value == NULL) {
293 if (name != NULL)
294 xfree(name);
295 goto malf;
296 }
297 debug2("Unrecognised slave extension \"%s\"", name);
298 xfree(name);
299 xfree(value);
300 }
301 state->hello_rcvd = 1;
302 return 0;
303}
304
305static int
306process_mux_new_session(u_int rid, Channel *c, Buffer *m, Buffer *r)
307{
308 Channel *nc;
309 struct mux_session_confirm_ctx *cctx;
310 char *reserved, *cmd, *cp;
311 u_int i, j, len, env_len, escape_char, window, packetmax;
312 int new_fd[3];
313
314 /* Reply for SSHMUX_COMMAND_OPEN */
315 cctx = xcalloc(1, sizeof(*cctx));
316 cctx->term = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000317 cctx->rid = rid;
Damien Millere1537f92010-01-26 13:26:22 +1100318 cmd = reserved = NULL;
319 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
320 buffer_get_int_ret(&cctx->want_tty, m) != 0 ||
321 buffer_get_int_ret(&cctx->want_x_fwd, m) != 0 ||
322 buffer_get_int_ret(&cctx->want_agent_fwd, m) != 0 ||
323 buffer_get_int_ret(&cctx->want_subsys, m) != 0 ||
324 buffer_get_int_ret(&escape_char, m) != 0 ||
325 (cctx->term = buffer_get_string_ret(m, &len)) == NULL ||
326 (cmd = buffer_get_string_ret(m, &len)) == NULL) {
327 malf:
328 if (cmd != NULL)
329 xfree(cmd);
330 if (reserved != NULL)
331 xfree(reserved);
332 if (cctx->term != NULL)
333 xfree(cctx->term);
334 error("%s: malformed message", __func__);
335 return -1;
336 }
337 xfree(reserved);
338 reserved = NULL;
339
340 cctx->env = NULL;
341 env_len = 0;
342 while (buffer_len(m) > 0) {
343#define MUX_MAX_ENV_VARS 4096
344 if ((cp = buffer_get_string_ret(m, &len)) == NULL) {
345 xfree(cmd);
346 goto malf;
347 }
348 if (!env_permitted(cp)) {
349 xfree(cp);
350 continue;
351 }
352 cctx->env = xrealloc(cctx->env, env_len + 2,
353 sizeof(*cctx->env));
354 cctx->env[env_len++] = cp;
355 cctx->env[env_len] = NULL;
356 if (env_len > MUX_MAX_ENV_VARS) {
357 error(">%d environment variables received, ignoring "
358 "additional", MUX_MAX_ENV_VARS);
359 break;
360 }
361 }
362
363 debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
364 "term \"%s\", cmd \"%s\", env %u", __func__, c->self,
365 cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
366 cctx->want_subsys, cctx->term, cmd, env_len);
367
368 buffer_init(&cctx->cmd);
369 buffer_append(&cctx->cmd, cmd, strlen(cmd));
370 xfree(cmd);
371 cmd = NULL;
372
373 /* Gather fds from client */
374 for(i = 0; i < 3; i++) {
375 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
376 error("%s: failed to receive fd %d from slave",
377 __func__, i);
378 for (j = 0; j < i; j++)
379 close(new_fd[j]);
380 for (j = 0; j < env_len; j++)
381 xfree(cctx->env[j]);
382 if (env_len > 0)
383 xfree(cctx->env);
384 xfree(cctx->term);
385 buffer_free(&cctx->cmd);
386 xfree(cctx);
387
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]);
411 xfree(cctx->term);
412 if (env_len != 0) {
413 for (i = 0; i < env_len; i++)
414 xfree(cctx->env[i]);
415 xfree(cctx->env);
416 }
417 buffer_free(&cctx->cmd);
418 return 0;
419 }
420
421 if (options.control_master == SSHCTL_MASTER_ASK ||
422 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
423 if (!ask_permission("Allow shared connection to %s? ", host)) {
424 debug2("%s: session refused by user", __func__);
425 /* prepare reply */
426 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
427 buffer_put_int(r, rid);
428 buffer_put_cstring(r, "Permission denied");
429 goto cleanup;
430 }
431 }
432
433 /* Try to pick up ttymodes from client before it goes raw */
434 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
435 error("%s: tcgetattr: %s", __func__, strerror(errno));
436
437 /* enable nonblocking unless tty */
438 if (!isatty(new_fd[0]))
439 set_nonblock(new_fd[0]);
440 if (!isatty(new_fd[1]))
441 set_nonblock(new_fd[1]);
442 if (!isatty(new_fd[2]))
443 set_nonblock(new_fd[2]);
444
445 window = CHAN_SES_WINDOW_DEFAULT;
446 packetmax = CHAN_SES_PACKET_DEFAULT;
447 if (cctx->want_tty) {
448 window >>= 1;
449 packetmax >>= 1;
450 }
451
452 nc = channel_new("session", SSH_CHANNEL_OPENING,
453 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
454 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
455
456 nc->ctl_chan = c->self; /* link session -> control channel */
457 c->remote_id = nc->self; /* link control -> session channel */
458
459 if (cctx->want_tty && escape_char != 0xffffffff) {
460 channel_register_filter(nc->self,
461 client_simple_escape_filter, NULL,
462 client_filter_cleanup,
463 client_new_escape_filter_ctx((int)escape_char));
464 }
465
466 debug2("%s: channel_new: %d linked to control channel %d",
467 __func__, nc->self, nc->ctl_chan);
468
469 channel_send_open(nc->self);
470 channel_register_open_confirm(nc->self, mux_session_confirm, cctx);
Damien Millerd530f5f2010-05-21 14:57:10 +1000471 c->mux_pause = 1; /* stop handling messages until open_confirm done */
Damien Miller85c50d72010-05-10 11:53:02 +1000472 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +1100473
Damien Millerd530f5f2010-05-21 14:57:10 +1000474 /* reply is deferred, sent by mux_session_confirm */
Damien Millere1537f92010-01-26 13:26:22 +1100475 return 0;
476}
477
478static int
479process_mux_alive_check(u_int rid, Channel *c, Buffer *m, Buffer *r)
480{
481 debug2("%s: channel %d: alive check", __func__, c->self);
482
483 /* prepare reply */
484 buffer_put_int(r, MUX_S_ALIVE);
485 buffer_put_int(r, rid);
486 buffer_put_int(r, (u_int)getpid());
487
488 return 0;
489}
490
491static int
492process_mux_terminate(u_int rid, Channel *c, Buffer *m, Buffer *r)
493{
494 debug2("%s: channel %d: terminate request", __func__, c->self);
495
496 if (options.control_master == SSHCTL_MASTER_ASK ||
497 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
498 if (!ask_permission("Terminate shared connection to %s? ",
499 host)) {
500 debug2("%s: termination refused by user", __func__);
501 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
502 buffer_put_int(r, rid);
503 buffer_put_cstring(r, "Permission denied");
504 return 0;
505 }
506 }
507
508 quit_pending = 1;
509 buffer_put_int(r, MUX_S_OK);
510 buffer_put_int(r, rid);
511 /* XXX exit happens too soon - message never makes it to client */
512 return 0;
513}
514
515static char *
516format_forward(u_int ftype, Forward *fwd)
517{
518 char *ret;
519
520 switch (ftype) {
521 case MUX_FWD_LOCAL:
522 xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
523 (fwd->listen_host == NULL) ?
524 (options.gateway_ports ? "*" : "LOCALHOST") :
525 fwd->listen_host, fwd->listen_port,
526 fwd->connect_host, fwd->connect_port);
527 break;
528 case MUX_FWD_DYNAMIC:
529 xasprintf(&ret, "dynamic forward %.200s:%d -> *",
530 (fwd->listen_host == NULL) ?
531 (options.gateway_ports ? "*" : "LOCALHOST") :
532 fwd->listen_host, fwd->listen_port);
533 break;
534 case MUX_FWD_REMOTE:
535 xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
536 (fwd->listen_host == NULL) ?
537 "LOCALHOST" : fwd->listen_host,
538 fwd->listen_port,
539 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
558compare_forward(Forward *a, Forward *b)
559{
560 if (!compare_host(a->listen_host, b->listen_host))
561 return 0;
562 if (a->listen_port != b->listen_port)
563 return 0;
564 if (!compare_host(a->connect_host, b->connect_host))
565 return 0;
566 if (a->connect_port != b->connect_port)
567 return 0;
568
569 return 1;
570}
571
Damien Miller388f6fc2010-05-21 14:57:35 +1000572static void
573mux_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
574{
575 struct mux_channel_confirm_ctx *fctx = ctxt;
576 char *failmsg = NULL;
577 Forward *rfwd;
578 Channel *c;
579 Buffer out;
580
581 if ((c = channel_by_id(fctx->cid)) == NULL) {
582 /* no channel for reply */
583 error("%s: unknown channel", __func__);
584 return;
585 }
586 buffer_init(&out);
587 if (fctx->fid >= options.num_remote_forwards) {
588 xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
589 goto fail;
590 }
591 rfwd = &options.remote_forwards[fctx->fid];
592 debug("%s: %s for: listen %d, connect %s:%d", __func__,
593 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
594 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
595 if (type == SSH2_MSG_REQUEST_SUCCESS) {
596 if (rfwd->listen_port == 0) {
597 rfwd->allocated_port = packet_get_int();
598 logit("Allocated port %u for mux remote forward"
599 " to %s:%d", rfwd->allocated_port,
600 rfwd->connect_host, rfwd->connect_port);
601 buffer_put_int(&out, MUX_S_REMOTE_PORT);
602 buffer_put_int(&out, fctx->rid);
603 buffer_put_int(&out, rfwd->allocated_port);
604 } else {
605 buffer_put_int(&out, MUX_S_OK);
606 buffer_put_int(&out, fctx->rid);
607 }
608 goto out;
609 } else {
610 xasprintf(&failmsg, "remote port forwarding failed for "
611 "listen port %d", rfwd->listen_port);
612 }
613 fail:
614 error("%s: %s", __func__, failmsg);
615 buffer_put_int(&out, MUX_S_FAILURE);
616 buffer_put_int(&out, fctx->rid);
617 buffer_put_cstring(&out, failmsg);
618 xfree(failmsg);
619 out:
620 buffer_put_string(&c->output, buffer_ptr(&out), buffer_len(&out));
621 buffer_free(&out);
622 if (c->mux_pause <= 0)
623 fatal("%s: mux_pause %d", __func__, c->mux_pause);
624 c->mux_pause = 0; /* start processing messages again */
625}
626
Damien Millere1537f92010-01-26 13:26:22 +1100627static int
628process_mux_open_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
629{
630 Forward fwd;
631 char *fwd_desc = NULL;
632 u_int ftype;
633 int i, ret = 0, freefwd = 1;
634
635 fwd.listen_host = fwd.connect_host = NULL;
636 if (buffer_get_int_ret(&ftype, m) != 0 ||
637 (fwd.listen_host = buffer_get_string_ret(m, NULL)) == NULL ||
638 buffer_get_int_ret(&fwd.listen_port, m) != 0 ||
639 (fwd.connect_host = buffer_get_string_ret(m, NULL)) == NULL ||
640 buffer_get_int_ret(&fwd.connect_port, m) != 0) {
641 error("%s: malformed message", __func__);
642 ret = -1;
643 goto out;
644 }
645
646 if (*fwd.listen_host == '\0') {
647 xfree(fwd.listen_host);
648 fwd.listen_host = NULL;
649 }
650 if (*fwd.connect_host == '\0') {
651 xfree(fwd.connect_host);
652 fwd.connect_host = NULL;
653 }
654
655 debug2("%s: channel %d: request %s", __func__, c->self,
656 (fwd_desc = format_forward(ftype, &fwd)));
657
658 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
659 ftype != MUX_FWD_DYNAMIC) {
660 logit("%s: invalid forwarding type %u", __func__, ftype);
661 invalid:
Damien Miller388f6fc2010-05-21 14:57:35 +1000662 if (fwd.listen_host)
663 xfree(fwd.listen_host);
664 if (fwd.connect_host)
665 xfree(fwd.connect_host);
Damien Millere1537f92010-01-26 13:26:22 +1100666 buffer_put_int(r, MUX_S_FAILURE);
667 buffer_put_int(r, rid);
668 buffer_put_cstring(r, "Invalid forwarding request");
669 return 0;
670 }
Damien Miller388f6fc2010-05-21 14:57:35 +1000671 if (fwd.listen_port >= 65536) {
Damien Millere1537f92010-01-26 13:26:22 +1100672 logit("%s: invalid listen port %u", __func__,
673 fwd.listen_port);
674 goto invalid;
675 }
676 if (fwd.connect_port >= 65536 || (ftype != MUX_FWD_DYNAMIC &&
677 ftype != MUX_FWD_REMOTE && fwd.connect_port == 0)) {
678 logit("%s: invalid connect port %u", __func__,
679 fwd.connect_port);
680 goto invalid;
681 }
682 if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL) {
683 logit("%s: missing connect host", __func__);
684 goto invalid;
685 }
686
687 /* Skip forwards that have already been requested */
688 switch (ftype) {
689 case MUX_FWD_LOCAL:
690 case MUX_FWD_DYNAMIC:
691 for (i = 0; i < options.num_local_forwards; i++) {
692 if (compare_forward(&fwd,
693 options.local_forwards + i)) {
694 exists:
695 debug2("%s: found existing forwarding",
696 __func__);
697 buffer_put_int(r, MUX_S_OK);
698 buffer_put_int(r, rid);
699 goto out;
700 }
701 }
702 break;
703 case MUX_FWD_REMOTE:
704 for (i = 0; i < options.num_remote_forwards; i++) {
705 if (compare_forward(&fwd,
Damien Miller388f6fc2010-05-21 14:57:35 +1000706 options.remote_forwards + i)) {
707 if (fwd.listen_port != 0)
708 goto exists;
709 debug2("%s: found allocated port",
710 __func__);
711 buffer_put_int(r, MUX_S_REMOTE_PORT);
712 buffer_put_int(r, rid);
713 buffer_put_int(r,
714 options.remote_forwards[i].allocated_port);
715 goto out;
716 }
Damien Millere1537f92010-01-26 13:26:22 +1100717 }
718 break;
719 }
720
721 if (options.control_master == SSHCTL_MASTER_ASK ||
722 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
723 if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
724 debug2("%s: forwarding refused by user", __func__);
725 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
726 buffer_put_int(r, rid);
727 buffer_put_cstring(r, "Permission denied");
728 goto out;
729 }
730 }
731
732 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
Damien Miller232cfb12010-06-26 09:50:30 +1000733 if (channel_setup_local_fwd_listener(fwd.listen_host,
Damien Millere1537f92010-01-26 13:26:22 +1100734 fwd.listen_port, fwd.connect_host, fwd.connect_port,
735 options.gateway_ports) < 0) {
736 fail:
737 logit("slave-requested %s failed", fwd_desc);
738 buffer_put_int(r, MUX_S_FAILURE);
739 buffer_put_int(r, rid);
740 buffer_put_cstring(r, "Port forwarding failed");
741 goto out;
742 }
743 add_local_forward(&options, &fwd);
744 freefwd = 0;
745 } else {
Damien Miller388f6fc2010-05-21 14:57:35 +1000746 struct mux_channel_confirm_ctx *fctx;
747
Damien Miller232cfb12010-06-26 09:50:30 +1000748 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millere1537f92010-01-26 13:26:22 +1100749 fwd.listen_port, fwd.connect_host, fwd.connect_port) < 0)
750 goto fail;
751 add_remote_forward(&options, &fwd);
Damien Miller388f6fc2010-05-21 14:57:35 +1000752 fctx = xcalloc(1, sizeof(*fctx));
753 fctx->cid = c->self;
754 fctx->rid = rid;
Damien Miller232cfb12010-06-26 09:50:30 +1000755 fctx->fid = options.num_remote_forwards - 1;
Damien Miller388f6fc2010-05-21 14:57:35 +1000756 client_register_global_confirm(mux_confirm_remote_forward,
757 fctx);
Damien Millere1537f92010-01-26 13:26:22 +1100758 freefwd = 0;
Damien Miller388f6fc2010-05-21 14:57:35 +1000759 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
760 /* delayed reply in mux_confirm_remote_forward */
761 goto out;
Damien Millere1537f92010-01-26 13:26:22 +1100762 }
763 buffer_put_int(r, MUX_S_OK);
764 buffer_put_int(r, rid);
765 out:
766 if (fwd_desc != NULL)
767 xfree(fwd_desc);
768 if (freefwd) {
769 if (fwd.listen_host != NULL)
770 xfree(fwd.listen_host);
771 if (fwd.connect_host != NULL)
772 xfree(fwd.connect_host);
773 }
774 return ret;
775}
776
777static int
778process_mux_close_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
779{
780 Forward fwd;
781 char *fwd_desc = NULL;
782 u_int ftype;
783 int ret = 0;
784
785 fwd.listen_host = fwd.connect_host = NULL;
786 if (buffer_get_int_ret(&ftype, m) != 0 ||
787 (fwd.listen_host = buffer_get_string_ret(m, NULL)) == NULL ||
788 buffer_get_int_ret(&fwd.listen_port, m) != 0 ||
789 (fwd.connect_host = buffer_get_string_ret(m, NULL)) == NULL ||
790 buffer_get_int_ret(&fwd.connect_port, m) != 0) {
791 error("%s: malformed message", __func__);
792 ret = -1;
793 goto out;
794 }
795
796 if (*fwd.listen_host == '\0') {
797 xfree(fwd.listen_host);
798 fwd.listen_host = NULL;
799 }
800 if (*fwd.connect_host == '\0') {
801 xfree(fwd.connect_host);
802 fwd.connect_host = NULL;
803 }
804
805 debug2("%s: channel %d: request %s", __func__, c->self,
806 (fwd_desc = format_forward(ftype, &fwd)));
807
808 /* XXX implement this */
809 buffer_put_int(r, MUX_S_FAILURE);
810 buffer_put_int(r, rid);
811 buffer_put_cstring(r, "unimplemented");
812
813 out:
814 if (fwd_desc != NULL)
815 xfree(fwd_desc);
816 if (fwd.listen_host != NULL)
817 xfree(fwd.listen_host);
818 if (fwd.connect_host != NULL)
819 xfree(fwd.connect_host);
820
821 return ret;
822}
823
824static int
825process_mux_stdio_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
826{
827 Channel *nc;
828 char *reserved, *chost;
829 u_int cport, i, j;
830 int new_fd[2];
831
832 chost = reserved = NULL;
833 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
834 (chost = buffer_get_string_ret(m, NULL)) == NULL ||
835 buffer_get_int_ret(&cport, m) != 0) {
836 if (reserved != NULL)
837 xfree(reserved);
838 if (chost != NULL)
839 xfree(chost);
840 error("%s: malformed message", __func__);
841 return -1;
842 }
843 xfree(reserved);
844
845 debug2("%s: channel %d: request stdio fwd to %s:%u",
846 __func__, c->self, chost, cport);
847
848 /* Gather fds from client */
849 for(i = 0; i < 2; i++) {
850 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
851 error("%s: failed to receive fd %d from slave",
852 __func__, i);
853 for (j = 0; j < i; j++)
854 close(new_fd[j]);
855 xfree(chost);
856
857 /* prepare reply */
858 buffer_put_int(r, MUX_S_FAILURE);
859 buffer_put_int(r, rid);
860 buffer_put_cstring(r,
861 "did not receive file descriptors");
862 return -1;
863 }
864 }
865
866 debug3("%s: got fds stdin %d, stdout %d", __func__,
867 new_fd[0], new_fd[1]);
868
869 /* XXX support multiple child sessions in future */
870 if (c->remote_id != -1) {
871 debug2("%s: session already open", __func__);
872 /* prepare reply */
873 buffer_put_int(r, MUX_S_FAILURE);
874 buffer_put_int(r, rid);
875 buffer_put_cstring(r, "Multiple sessions not supported");
876 cleanup:
877 close(new_fd[0]);
878 close(new_fd[1]);
879 xfree(chost);
880 return 0;
881 }
882
883 if (options.control_master == SSHCTL_MASTER_ASK ||
884 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
Damien Miller68512c02010-10-21 15:21:11 +1100885 if (!ask_permission("Allow forward to %s:%u? ",
Damien Millere1537f92010-01-26 13:26:22 +1100886 chost, cport)) {
887 debug2("%s: stdio fwd refused by user", __func__);
888 /* prepare reply */
889 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
890 buffer_put_int(r, rid);
891 buffer_put_cstring(r, "Permission denied");
892 goto cleanup;
893 }
894 }
895
896 /* enable nonblocking unless tty */
897 if (!isatty(new_fd[0]))
898 set_nonblock(new_fd[0]);
899 if (!isatty(new_fd[1]))
900 set_nonblock(new_fd[1]);
901
902 nc = channel_connect_stdio_fwd(chost, cport, new_fd[0], new_fd[1]);
903
904 nc->ctl_chan = c->self; /* link session -> control channel */
905 c->remote_id = nc->self; /* link control -> session channel */
906
907 debug2("%s: channel_new: %d linked to control channel %d",
908 __func__, nc->self, nc->ctl_chan);
909
Damien Miller85c50d72010-05-10 11:53:02 +1000910 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +1100911
912 /* prepare reply */
913 /* XXX defer until channel confirmed */
914 buffer_put_int(r, MUX_S_SESSION_OPENED);
915 buffer_put_int(r, rid);
916 buffer_put_int(r, nc->self);
917
918 return 0;
919}
920
Damien Miller6c3eec72011-05-05 14:16:22 +1000921static int
922process_mux_stop_listening(u_int rid, Channel *c, Buffer *m, Buffer *r)
923{
924 debug("%s: channel %d: stop listening", __func__, c->self);
925
926 if (options.control_master == SSHCTL_MASTER_ASK ||
927 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
928 if (!ask_permission("Disable further multiplexing on shared "
929 "connection to %s? ", host)) {
930 debug2("%s: stop listen refused by user", __func__);
931 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
932 buffer_put_int(r, rid);
933 buffer_put_cstring(r, "Permission denied");
934 return 0;
935 }
936 }
937
938 if (mux_listener_channel != NULL) {
939 channel_free(mux_listener_channel);
940 client_stop_mux();
941 xfree(options.control_path);
942 options.control_path = NULL;
943 mux_listener_channel = NULL;
944 muxserver_sock = -1;
945 }
946
947 /* prepare reply */
948 buffer_put_int(r, MUX_S_OK);
949 buffer_put_int(r, rid);
950
951 return 0;
952}
953
Damien Millere1537f92010-01-26 13:26:22 +1100954/* Channel callbacks fired on read/write from mux slave fd */
955static int
956mux_master_read_cb(Channel *c)
957{
958 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
959 Buffer in, out;
960 void *ptr;
961 u_int type, rid, have, i;
962 int ret = -1;
963
964 /* Setup ctx and */
965 if (c->mux_ctx == NULL) {
Damien Millerc094d1e2010-06-26 09:36:34 +1000966 state = xcalloc(1, sizeof(*state));
Damien Millere1537f92010-01-26 13:26:22 +1100967 c->mux_ctx = state;
968 channel_register_cleanup(c->self,
969 mux_master_control_cleanup_cb, 0);
970
971 /* Send hello */
972 buffer_init(&out);
973 buffer_put_int(&out, MUX_MSG_HELLO);
974 buffer_put_int(&out, SSHMUX_VER);
975 /* no extensions */
976 buffer_put_string(&c->output, buffer_ptr(&out),
977 buffer_len(&out));
978 buffer_free(&out);
979 debug3("%s: channel %d: hello sent", __func__, c->self);
980 return 0;
981 }
982
983 buffer_init(&in);
984 buffer_init(&out);
985
986 /* Channel code ensures that we receive whole packets */
987 if ((ptr = buffer_get_string_ptr_ret(&c->input, &have)) == NULL) {
988 malf:
989 error("%s: malformed message", __func__);
990 goto out;
991 }
992 buffer_append(&in, ptr, have);
993
994 if (buffer_get_int_ret(&type, &in) != 0)
995 goto malf;
996 debug3("%s: channel %d packet type 0x%08x len %u",
997 __func__, c->self, type, buffer_len(&in));
998
999 if (type == MUX_MSG_HELLO)
1000 rid = 0;
1001 else {
1002 if (!state->hello_rcvd) {
1003 error("%s: expected MUX_MSG_HELLO(0x%08x), "
1004 "received 0x%08x", __func__, MUX_MSG_HELLO, type);
1005 goto out;
1006 }
1007 if (buffer_get_int_ret(&rid, &in) != 0)
1008 goto malf;
1009 }
1010
1011 for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
1012 if (type == mux_master_handlers[i].type) {
1013 ret = mux_master_handlers[i].handler(rid, c, &in, &out);
1014 break;
1015 }
1016 }
1017 if (mux_master_handlers[i].handler == NULL) {
1018 error("%s: unsupported mux message 0x%08x", __func__, type);
1019 buffer_put_int(&out, MUX_S_FAILURE);
1020 buffer_put_int(&out, rid);
1021 buffer_put_cstring(&out, "unsupported request");
1022 ret = 0;
1023 }
1024 /* Enqueue reply packet */
1025 if (buffer_len(&out) != 0) {
1026 buffer_put_string(&c->output, buffer_ptr(&out),
1027 buffer_len(&out));
1028 }
1029 out:
1030 buffer_free(&in);
1031 buffer_free(&out);
1032 return ret;
1033}
1034
1035void
1036mux_exit_message(Channel *c, int exitval)
1037{
1038 Buffer m;
1039 Channel *mux_chan;
1040
1041 debug3("%s: channel %d: exit message, evitval %d", __func__, c->self,
1042 exitval);
1043
1044 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL)
1045 fatal("%s: channel %d missing mux channel %d",
1046 __func__, c->self, c->ctl_chan);
1047
1048 /* Append exit message packet to control socket output queue */
1049 buffer_init(&m);
1050 buffer_put_int(&m, MUX_S_EXIT_MESSAGE);
1051 buffer_put_int(&m, c->self);
1052 buffer_put_int(&m, exitval);
1053
1054 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m));
1055 buffer_free(&m);
1056}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001057
1058/* Prepare a mux master to listen on a Unix domain socket. */
1059void
1060muxserver_listen(void)
1061{
1062 struct sockaddr_un addr;
Damien Millere1537f92010-01-26 13:26:22 +11001063 socklen_t sun_len;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001064 mode_t old_umask;
Damien Miller603134e2010-09-24 22:07:55 +10001065 char *orig_control_path = options.control_path;
1066 char rbuf[16+1];
1067 u_int i, r;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001068
1069 if (options.control_path == NULL ||
1070 options.control_master == SSHCTL_MASTER_NO)
1071 return;
1072
1073 debug("setting up multiplex master socket");
1074
Damien Miller603134e2010-09-24 22:07:55 +10001075 /*
1076 * Use a temporary path before listen so we can pseudo-atomically
1077 * establish the listening socket in its final location to avoid
1078 * other processes racing in between bind() and listen() and hitting
1079 * an unready socket.
1080 */
1081 for (i = 0; i < sizeof(rbuf) - 1; i++) {
1082 r = arc4random_uniform(26+26+10);
1083 rbuf[i] = (r < 26) ? 'a' + r :
1084 (r < 26*2) ? 'A' + r - 26 :
1085 '0' + r - 26 - 26;
1086 }
1087 rbuf[sizeof(rbuf) - 1] = '\0';
1088 options.control_path = NULL;
1089 xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
1090 debug3("%s: temporary control path %s", __func__, options.control_path);
1091
Damien Millerb1cbfa22008-05-19 16:00:08 +10001092 memset(&addr, '\0', sizeof(addr));
1093 addr.sun_family = AF_UNIX;
Damien Millere1537f92010-01-26 13:26:22 +11001094 sun_len = offsetof(struct sockaddr_un, sun_path) +
Damien Millerb1cbfa22008-05-19 16:00:08 +10001095 strlen(options.control_path) + 1;
1096
1097 if (strlcpy(addr.sun_path, options.control_path,
1098 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1099 fatal("ControlPath too long");
1100
1101 if ((muxserver_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1102 fatal("%s socket(): %s", __func__, strerror(errno));
1103
1104 old_umask = umask(0177);
Damien Millere1537f92010-01-26 13:26:22 +11001105 if (bind(muxserver_sock, (struct sockaddr *)&addr, sun_len) == -1) {
Damien Millerb1cbfa22008-05-19 16:00:08 +10001106 muxserver_sock = -1;
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001107 if (errno == EINVAL || errno == EADDRINUSE) {
1108 error("ControlSocket %s already exists, "
1109 "disabling multiplexing", options.control_path);
Damien Miller603134e2010-09-24 22:07:55 +10001110 disable_mux_master:
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001111 close(muxserver_sock);
1112 muxserver_sock = -1;
1113 xfree(options.control_path);
1114 options.control_path = NULL;
1115 options.control_master = SSHCTL_MASTER_NO;
1116 return;
1117 } else
Damien Millerb1cbfa22008-05-19 16:00:08 +10001118 fatal("%s bind(): %s", __func__, strerror(errno));
1119 }
1120 umask(old_umask);
1121
1122 if (listen(muxserver_sock, 64) == -1)
1123 fatal("%s listen(): %s", __func__, strerror(errno));
1124
Damien Miller603134e2010-09-24 22:07:55 +10001125 /* Now atomically "move" the mux socket into position */
1126 if (link(options.control_path, orig_control_path) != 0) {
1127 if (errno != EEXIST) {
1128 fatal("%s: link mux listener %s => %s: %s", __func__,
1129 options.control_path, orig_control_path,
1130 strerror(errno));
1131 }
1132 error("ControlSocket %s already exists, disabling multiplexing",
1133 orig_control_path);
1134 xfree(orig_control_path);
1135 unlink(options.control_path);
1136 goto disable_mux_master;
1137 }
1138 unlink(options.control_path);
1139 xfree(options.control_path);
1140 options.control_path = orig_control_path;
1141
Damien Millerb1cbfa22008-05-19 16:00:08 +10001142 set_nonblock(muxserver_sock);
Damien Millere1537f92010-01-26 13:26:22 +11001143
1144 mux_listener_channel = channel_new("mux listener",
1145 SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
1146 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller603134e2010-09-24 22:07:55 +10001147 0, options.control_path, 1);
Damien Millere1537f92010-01-26 13:26:22 +11001148 mux_listener_channel->mux_rcb = mux_master_read_cb;
1149 debug3("%s: mux listener channel %d fd %d", __func__,
1150 mux_listener_channel->self, mux_listener_channel->sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001151}
1152
1153/* Callback on open confirmation in mux master for a mux client session. */
1154static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001155mux_session_confirm(int id, int success, void *arg)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001156{
1157 struct mux_session_confirm_ctx *cctx = arg;
1158 const char *display;
Damien Millerd530f5f2010-05-21 14:57:10 +10001159 Channel *c, *cc;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001160 int i;
Damien Millerd530f5f2010-05-21 14:57:10 +10001161 Buffer reply;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001162
1163 if (cctx == NULL)
1164 fatal("%s: cctx == NULL", __func__);
Damien Millere1537f92010-01-26 13:26:22 +11001165 if ((c = channel_by_id(id)) == NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001166 fatal("%s: no channel for id %d", __func__, id);
Damien Millerd530f5f2010-05-21 14:57:10 +10001167 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
1168 fatal("%s: channel %d lacks control channel %d", __func__,
1169 id, c->ctl_chan);
1170
1171 if (!success) {
1172 debug3("%s: sending failure reply", __func__);
1173 /* prepare reply */
1174 buffer_init(&reply);
1175 buffer_put_int(&reply, MUX_S_FAILURE);
1176 buffer_put_int(&reply, cctx->rid);
1177 buffer_put_cstring(&reply, "Session open refused by peer");
1178 goto done;
1179 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001180
1181 display = getenv("DISPLAY");
1182 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1183 char *proto, *data;
Damien Miller1ab6a512010-06-26 10:02:24 +10001184
Damien Millerb1cbfa22008-05-19 16:00:08 +10001185 /* Get reasonable local authentication information. */
1186 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001187 options.forward_x11_trusted, options.forward_x11_timeout,
1188 &proto, &data);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001189 /* Request forwarding with authentication spoofing. */
Damien Miller1ab6a512010-06-26 10:02:24 +10001190 debug("Requesting X11 forwarding with authentication "
1191 "spoofing.");
Damien Millerb1cbfa22008-05-19 16:00:08 +10001192 x11_request_forwarding_with_spoofing(id, display, proto, data);
1193 /* XXX wait for reply */
1194 }
1195
1196 if (cctx->want_agent_fwd && options.forward_agent) {
1197 debug("Requesting authentication agent forwarding.");
1198 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1199 packet_send();
1200 }
1201
1202 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
1203 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
1204
Damien Millerd530f5f2010-05-21 14:57:10 +10001205 debug3("%s: sending success reply", __func__);
1206 /* prepare reply */
1207 buffer_init(&reply);
1208 buffer_put_int(&reply, MUX_S_SESSION_OPENED);
1209 buffer_put_int(&reply, cctx->rid);
1210 buffer_put_int(&reply, c->self);
1211
1212 done:
1213 /* Send reply */
1214 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply));
1215 buffer_free(&reply);
1216
1217 if (cc->mux_pause <= 0)
1218 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1219 cc->mux_pause = 0; /* start processing messages again */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001220 c->open_confirm_ctx = NULL;
1221 buffer_free(&cctx->cmd);
1222 xfree(cctx->term);
1223 if (cctx->env != NULL) {
1224 for (i = 0; cctx->env[i] != NULL; i++)
1225 xfree(cctx->env[i]);
1226 xfree(cctx->env);
1227 }
1228 xfree(cctx);
1229}
1230
Damien Millerb1cbfa22008-05-19 16:00:08 +10001231/* ** Multiplexing client support */
1232
1233/* Exit signal handler */
1234static void
1235control_client_sighandler(int signo)
1236{
1237 muxclient_terminate = signo;
1238}
1239
1240/*
1241 * Relay signal handler - used to pass some signals from mux client to
1242 * mux master.
1243 */
1244static void
1245control_client_sigrelay(int signo)
1246{
1247 int save_errno = errno;
1248
1249 if (muxserver_pid > 1)
1250 kill(muxserver_pid, signo);
1251
1252 errno = save_errno;
1253}
1254
Damien Millerb1cbfa22008-05-19 16:00:08 +10001255static int
Damien Millere1537f92010-01-26 13:26:22 +11001256mux_client_read(int fd, Buffer *b, u_int need)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001257{
Damien Millere1537f92010-01-26 13:26:22 +11001258 u_int have;
1259 ssize_t len;
1260 u_char *p;
1261 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001262
Damien Millere1537f92010-01-26 13:26:22 +11001263 pfd.fd = fd;
1264 pfd.events = POLLIN;
1265 p = buffer_append_space(b, need);
1266 for (have = 0; have < need; ) {
1267 if (muxclient_terminate) {
1268 errno = EINTR;
1269 return -1;
1270 }
1271 len = read(fd, p + have, need - have);
1272 if (len < 0) {
1273 switch (errno) {
1274#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1275 case EWOULDBLOCK:
1276#endif
1277 case EAGAIN:
1278 (void)poll(&pfd, 1, -1);
1279 /* FALLTHROUGH */
1280 case EINTR:
1281 continue;
1282 default:
1283 return -1;
1284 }
1285 }
1286 if (len == 0) {
1287 errno = EPIPE;
1288 return -1;
1289 }
1290 have += (u_int)len;
1291 }
1292 return 0;
1293}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001294
Damien Millere1537f92010-01-26 13:26:22 +11001295static int
1296mux_client_write_packet(int fd, Buffer *m)
1297{
1298 Buffer queue;
1299 u_int have, need;
1300 int oerrno, len;
1301 u_char *ptr;
1302 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001303
Damien Millere1537f92010-01-26 13:26:22 +11001304 pfd.fd = fd;
1305 pfd.events = POLLOUT;
1306 buffer_init(&queue);
1307 buffer_put_string(&queue, buffer_ptr(m), buffer_len(m));
1308
1309 need = buffer_len(&queue);
1310 ptr = buffer_ptr(&queue);
1311
1312 for (have = 0; have < need; ) {
1313 if (muxclient_terminate) {
1314 buffer_free(&queue);
1315 errno = EINTR;
1316 return -1;
1317 }
1318 len = write(fd, ptr + have, need - have);
1319 if (len < 0) {
1320 switch (errno) {
1321#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1322 case EWOULDBLOCK:
1323#endif
1324 case EAGAIN:
1325 (void)poll(&pfd, 1, -1);
1326 /* FALLTHROUGH */
1327 case EINTR:
1328 continue;
1329 default:
1330 oerrno = errno;
1331 buffer_free(&queue);
1332 errno = oerrno;
1333 return -1;
1334 }
1335 }
1336 if (len == 0) {
1337 buffer_free(&queue);
1338 errno = EPIPE;
1339 return -1;
1340 }
1341 have += (u_int)len;
1342 }
1343 buffer_free(&queue);
1344 return 0;
1345}
1346
1347static int
1348mux_client_read_packet(int fd, Buffer *m)
1349{
1350 Buffer queue;
1351 u_int need, have;
1352 void *ptr;
1353 int oerrno;
1354
1355 buffer_init(&queue);
1356 if (mux_client_read(fd, &queue, 4) != 0) {
1357 if ((oerrno = errno) == EPIPE)
1358 debug3("%s: read header failed: %s", __func__, strerror(errno));
1359 errno = oerrno;
1360 return -1;
1361 }
1362 need = get_u32(buffer_ptr(&queue));
1363 if (mux_client_read(fd, &queue, need) != 0) {
1364 oerrno = errno;
1365 debug3("%s: read body failed: %s", __func__, strerror(errno));
1366 errno = oerrno;
1367 return -1;
1368 }
1369 ptr = buffer_get_string_ptr(&queue, &have);
1370 buffer_append(m, ptr, have);
1371 buffer_free(&queue);
1372 return 0;
1373}
1374
1375static int
1376mux_client_hello_exchange(int fd)
1377{
1378 Buffer m;
1379 u_int type, ver;
1380
1381 buffer_init(&m);
1382 buffer_put_int(&m, MUX_MSG_HELLO);
1383 buffer_put_int(&m, SSHMUX_VER);
1384 /* no extensions */
1385
1386 if (mux_client_write_packet(fd, &m) != 0)
1387 fatal("%s: write packet: %s", __func__, strerror(errno));
1388
1389 buffer_clear(&m);
1390
1391 /* Read their HELLO */
1392 if (mux_client_read_packet(fd, &m) != 0) {
1393 buffer_free(&m);
1394 return -1;
1395 }
1396
1397 type = buffer_get_int(&m);
1398 if (type != MUX_MSG_HELLO)
1399 fatal("%s: expected HELLO (%u) received %u",
1400 __func__, MUX_MSG_HELLO, type);
1401 ver = buffer_get_int(&m);
1402 if (ver != SSHMUX_VER)
1403 fatal("Unsupported multiplexing protocol version %d "
1404 "(expected %d)", ver, SSHMUX_VER);
1405 debug2("%s: master version %u", __func__, ver);
1406 /* No extensions are presently defined */
1407 while (buffer_len(&m) > 0) {
1408 char *name = buffer_get_string(&m, NULL);
1409 char *value = buffer_get_string(&m, NULL);
1410
1411 debug2("Unrecognised master extension \"%s\"", name);
1412 xfree(name);
1413 xfree(value);
1414 }
1415 buffer_free(&m);
1416 return 0;
1417}
1418
1419static u_int
1420mux_client_request_alive(int fd)
1421{
1422 Buffer m;
1423 char *e;
1424 u_int pid, type, rid;
1425
1426 debug3("%s: entering", __func__);
1427
1428 buffer_init(&m);
1429 buffer_put_int(&m, MUX_C_ALIVE_CHECK);
1430 buffer_put_int(&m, muxclient_request_id);
1431
1432 if (mux_client_write_packet(fd, &m) != 0)
1433 fatal("%s: write packet: %s", __func__, strerror(errno));
1434
1435 buffer_clear(&m);
1436
1437 /* Read their reply */
1438 if (mux_client_read_packet(fd, &m) != 0) {
1439 buffer_free(&m);
1440 return 0;
1441 }
1442
1443 type = buffer_get_int(&m);
1444 if (type != MUX_S_ALIVE) {
1445 e = buffer_get_string(&m, NULL);
1446 fatal("%s: master returned error: %s", __func__, e);
1447 }
1448
1449 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1450 fatal("%s: out of sequence reply: my id %u theirs %u",
1451 __func__, muxclient_request_id, rid);
1452 pid = buffer_get_int(&m);
1453 buffer_free(&m);
1454
1455 debug3("%s: done pid = %u", __func__, pid);
1456
1457 muxclient_request_id++;
1458
1459 return pid;
1460}
1461
1462static void
1463mux_client_request_terminate(int fd)
1464{
1465 Buffer m;
1466 char *e;
1467 u_int type, rid;
1468
1469 debug3("%s: entering", __func__);
1470
1471 buffer_init(&m);
1472 buffer_put_int(&m, MUX_C_TERMINATE);
1473 buffer_put_int(&m, muxclient_request_id);
1474
1475 if (mux_client_write_packet(fd, &m) != 0)
1476 fatal("%s: write packet: %s", __func__, strerror(errno));
1477
1478 buffer_clear(&m);
1479
1480 /* Read their reply */
1481 if (mux_client_read_packet(fd, &m) != 0) {
1482 /* Remote end exited already */
1483 if (errno == EPIPE) {
1484 buffer_free(&m);
1485 return;
1486 }
1487 fatal("%s: read from master failed: %s",
1488 __func__, strerror(errno));
1489 }
1490
1491 type = buffer_get_int(&m);
1492 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1493 fatal("%s: out of sequence reply: my id %u theirs %u",
1494 __func__, muxclient_request_id, rid);
1495 switch (type) {
1496 case MUX_S_OK:
1497 break;
1498 case MUX_S_PERMISSION_DENIED:
1499 e = buffer_get_string(&m, NULL);
1500 fatal("Master refused termination request: %s", e);
1501 case MUX_S_FAILURE:
1502 e = buffer_get_string(&m, NULL);
1503 fatal("%s: termination request failed: %s", __func__, e);
1504 default:
1505 fatal("%s: unexpected response from master 0x%08x",
1506 __func__, type);
1507 }
1508 buffer_free(&m);
1509 muxclient_request_id++;
1510}
1511
1512static int
1513mux_client_request_forward(int fd, u_int ftype, Forward *fwd)
1514{
1515 Buffer m;
1516 char *e, *fwd_desc;
1517 u_int type, rid;
1518
1519 fwd_desc = format_forward(ftype, fwd);
1520 debug("Requesting %s", fwd_desc);
1521 xfree(fwd_desc);
1522
1523 buffer_init(&m);
1524 buffer_put_int(&m, MUX_C_OPEN_FWD);
1525 buffer_put_int(&m, muxclient_request_id);
1526 buffer_put_int(&m, ftype);
1527 buffer_put_cstring(&m,
1528 fwd->listen_host == NULL ? "" : fwd->listen_host);
1529 buffer_put_int(&m, fwd->listen_port);
1530 buffer_put_cstring(&m,
1531 fwd->connect_host == NULL ? "" : fwd->connect_host);
1532 buffer_put_int(&m, fwd->connect_port);
1533
1534 if (mux_client_write_packet(fd, &m) != 0)
1535 fatal("%s: write packet: %s", __func__, strerror(errno));
1536
1537 buffer_clear(&m);
1538
1539 /* Read their reply */
1540 if (mux_client_read_packet(fd, &m) != 0) {
1541 buffer_free(&m);
1542 return -1;
1543 }
1544
1545 type = buffer_get_int(&m);
1546 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1547 fatal("%s: out of sequence reply: my id %u theirs %u",
1548 __func__, muxclient_request_id, rid);
1549 switch (type) {
1550 case MUX_S_OK:
1551 break;
Damien Miller388f6fc2010-05-21 14:57:35 +10001552 case MUX_S_REMOTE_PORT:
1553 fwd->allocated_port = buffer_get_int(&m);
1554 logit("Allocated port %u for remote forward to %s:%d",
1555 fwd->allocated_port,
1556 fwd->connect_host ? fwd->connect_host : "",
1557 fwd->connect_port);
1558 if (muxclient_command == SSHMUX_COMMAND_FORWARD)
1559 fprintf(stdout, "%u\n", fwd->allocated_port);
1560 break;
Damien Millere1537f92010-01-26 13:26:22 +11001561 case MUX_S_PERMISSION_DENIED:
1562 e = buffer_get_string(&m, NULL);
1563 buffer_free(&m);
1564 error("Master refused forwarding request: %s", e);
1565 return -1;
1566 case MUX_S_FAILURE:
1567 e = buffer_get_string(&m, NULL);
1568 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001569 error("%s: forwarding request failed: %s", __func__, e);
Damien Millere1537f92010-01-26 13:26:22 +11001570 return -1;
1571 default:
1572 fatal("%s: unexpected response from master 0x%08x",
1573 __func__, type);
1574 }
1575 buffer_free(&m);
1576
1577 muxclient_request_id++;
1578 return 0;
1579}
1580
1581static int
1582mux_client_request_forwards(int fd)
1583{
1584 int i;
1585
1586 debug3("%s: requesting forwardings: %d local, %d remote", __func__,
1587 options.num_local_forwards, options.num_remote_forwards);
1588
1589 /* XXX ExitOnForwardingFailure */
1590 for (i = 0; i < options.num_local_forwards; i++) {
1591 if (mux_client_request_forward(fd,
1592 options.local_forwards[i].connect_port == 0 ?
1593 MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
1594 options.local_forwards + i) != 0)
1595 return -1;
1596 }
1597 for (i = 0; i < options.num_remote_forwards; i++) {
1598 if (mux_client_request_forward(fd, MUX_FWD_REMOTE,
1599 options.remote_forwards + i) != 0)
1600 return -1;
1601 }
1602 return 0;
1603}
1604
1605static int
1606mux_client_request_session(int fd)
1607{
1608 Buffer m;
1609 char *e, *term;
1610 u_int i, rid, sid, esid, exitval, type, exitval_seen;
1611 extern char **environ;
1612 int devnull;
1613
1614 debug3("%s: entering", __func__);
1615
1616 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1617 error("%s: master alive request failed", __func__);
1618 return -1;
1619 }
1620
1621 signal(SIGPIPE, SIG_IGN);
1622
1623 if (stdin_null_flag) {
1624 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1625 fatal("open(/dev/null): %s", strerror(errno));
1626 if (dup2(devnull, STDIN_FILENO) == -1)
1627 fatal("dup2: %s", strerror(errno));
1628 if (devnull > STDERR_FILENO)
1629 close(devnull);
1630 }
1631
1632 term = getenv("TERM");
1633
1634 buffer_init(&m);
1635 buffer_put_int(&m, MUX_C_NEW_SESSION);
1636 buffer_put_int(&m, muxclient_request_id);
1637 buffer_put_cstring(&m, ""); /* reserved */
1638 buffer_put_int(&m, tty_flag);
1639 buffer_put_int(&m, options.forward_x11);
1640 buffer_put_int(&m, options.forward_agent);
1641 buffer_put_int(&m, subsystem_flag);
1642 buffer_put_int(&m, options.escape_char == SSH_ESCAPECHAR_NONE ?
1643 0xffffffff : (u_int)options.escape_char);
1644 buffer_put_cstring(&m, term == NULL ? "" : term);
1645 buffer_put_string(&m, buffer_ptr(&command), buffer_len(&command));
1646
1647 if (options.num_send_env > 0 && environ != NULL) {
1648 /* Pass environment */
1649 for (i = 0; environ[i] != NULL; i++) {
1650 if (env_permitted(environ[i])) {
1651 buffer_put_cstring(&m, environ[i]);
1652 }
1653 }
1654 }
1655
1656 if (mux_client_write_packet(fd, &m) != 0)
1657 fatal("%s: write packet: %s", __func__, strerror(errno));
1658
1659 /* Send the stdio file descriptors */
1660 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1661 mm_send_fd(fd, STDOUT_FILENO) == -1 ||
1662 mm_send_fd(fd, STDERR_FILENO) == -1)
1663 fatal("%s: send fds failed", __func__);
1664
1665 debug3("%s: session request sent", __func__);
1666
1667 /* Read their reply */
1668 buffer_clear(&m);
1669 if (mux_client_read_packet(fd, &m) != 0) {
1670 error("%s: read from master failed: %s",
1671 __func__, strerror(errno));
1672 buffer_free(&m);
1673 return -1;
1674 }
1675
1676 type = buffer_get_int(&m);
1677 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1678 fatal("%s: out of sequence reply: my id %u theirs %u",
1679 __func__, muxclient_request_id, rid);
1680 switch (type) {
1681 case MUX_S_SESSION_OPENED:
1682 sid = buffer_get_int(&m);
1683 debug("%s: master session id: %u", __func__, sid);
1684 break;
1685 case MUX_S_PERMISSION_DENIED:
1686 e = buffer_get_string(&m, NULL);
1687 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001688 error("Master refused session request: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11001689 return -1;
1690 case MUX_S_FAILURE:
1691 e = buffer_get_string(&m, NULL);
1692 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001693 error("%s: session request failed: %s", __func__, e);
Damien Millere1537f92010-01-26 13:26:22 +11001694 return -1;
1695 default:
1696 buffer_free(&m);
1697 error("%s: unexpected response from master 0x%08x",
1698 __func__, type);
1699 return -1;
1700 }
1701 muxclient_request_id++;
1702
1703 signal(SIGHUP, control_client_sighandler);
1704 signal(SIGINT, control_client_sighandler);
1705 signal(SIGTERM, control_client_sighandler);
1706 signal(SIGWINCH, control_client_sigrelay);
1707
1708 if (tty_flag)
1709 enter_raw_mode(force_tty_flag);
1710
1711 /*
1712 * Stick around until the controlee closes the client_fd.
1713 * Before it does, it is expected to write an exit message.
1714 * This process must read the value and wait for the closure of
1715 * the client_fd; if this one closes early, the multiplex master will
1716 * terminate early too (possibly losing data).
1717 */
1718 for (exitval = 255, exitval_seen = 0;;) {
1719 buffer_clear(&m);
1720 if (mux_client_read_packet(fd, &m) != 0)
1721 break;
1722 type = buffer_get_int(&m);
1723 if (type != MUX_S_EXIT_MESSAGE) {
1724 e = buffer_get_string(&m, NULL);
1725 fatal("%s: master returned error: %s", __func__, e);
1726 }
1727 if ((esid = buffer_get_int(&m)) != sid)
1728 fatal("%s: exit on unknown session: my id %u theirs %u",
1729 __func__, sid, esid);
1730 debug("%s: master session id: %u", __func__, sid);
1731 if (exitval_seen)
1732 fatal("%s: exitval sent twice", __func__);
1733 exitval = buffer_get_int(&m);
1734 exitval_seen = 1;
1735 }
1736
1737 close(fd);
1738 leave_raw_mode(force_tty_flag);
1739
1740 if (muxclient_terminate) {
1741 debug2("Exiting on signal %d", muxclient_terminate);
1742 exitval = 255;
1743 } else if (!exitval_seen) {
1744 debug2("Control master terminated unexpectedly");
1745 exitval = 255;
1746 } else
1747 debug2("Received exit status from master %d", exitval);
1748
1749 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1750 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
1751
1752 exit(exitval);
1753}
1754
1755static int
1756mux_client_request_stdio_fwd(int fd)
1757{
1758 Buffer m;
1759 char *e;
1760 u_int type, rid, sid;
1761 int devnull;
1762
1763 debug3("%s: entering", __func__);
1764
1765 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1766 error("%s: master alive request failed", __func__);
1767 return -1;
1768 }
1769
1770 signal(SIGPIPE, SIG_IGN);
1771
1772 if (stdin_null_flag) {
1773 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1774 fatal("open(/dev/null): %s", strerror(errno));
1775 if (dup2(devnull, STDIN_FILENO) == -1)
1776 fatal("dup2: %s", strerror(errno));
1777 if (devnull > STDERR_FILENO)
1778 close(devnull);
1779 }
1780
1781 buffer_init(&m);
1782 buffer_put_int(&m, MUX_C_NEW_STDIO_FWD);
1783 buffer_put_int(&m, muxclient_request_id);
1784 buffer_put_cstring(&m, ""); /* reserved */
1785 buffer_put_cstring(&m, stdio_forward_host);
1786 buffer_put_int(&m, stdio_forward_port);
1787
1788 if (mux_client_write_packet(fd, &m) != 0)
1789 fatal("%s: write packet: %s", __func__, strerror(errno));
1790
1791 /* Send the stdio file descriptors */
1792 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1793 mm_send_fd(fd, STDOUT_FILENO) == -1)
1794 fatal("%s: send fds failed", __func__);
1795
1796 debug3("%s: stdio forward request sent", __func__);
1797
1798 /* Read their reply */
1799 buffer_clear(&m);
1800
1801 if (mux_client_read_packet(fd, &m) != 0) {
1802 error("%s: read from master failed: %s",
1803 __func__, strerror(errno));
1804 buffer_free(&m);
1805 return -1;
1806 }
1807
1808 type = buffer_get_int(&m);
1809 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1810 fatal("%s: out of sequence reply: my id %u theirs %u",
1811 __func__, muxclient_request_id, rid);
1812 switch (type) {
1813 case MUX_S_SESSION_OPENED:
1814 sid = buffer_get_int(&m);
1815 debug("%s: master session id: %u", __func__, sid);
1816 break;
1817 case MUX_S_PERMISSION_DENIED:
1818 e = buffer_get_string(&m, NULL);
1819 buffer_free(&m);
Damien Miller445c9a52011-01-14 12:01:29 +11001820 fatal("Master refused stdio forwarding request: %s", e);
Damien Millere1537f92010-01-26 13:26:22 +11001821 case MUX_S_FAILURE:
1822 e = buffer_get_string(&m, NULL);
1823 buffer_free(&m);
1824 fatal("%s: stdio forwarding request failed: %s", __func__, e);
1825 default:
1826 buffer_free(&m);
1827 error("%s: unexpected response from master 0x%08x",
1828 __func__, type);
1829 return -1;
1830 }
1831 muxclient_request_id++;
1832
1833 signal(SIGHUP, control_client_sighandler);
1834 signal(SIGINT, control_client_sighandler);
1835 signal(SIGTERM, control_client_sighandler);
1836 signal(SIGWINCH, control_client_sigrelay);
1837
1838 /*
1839 * Stick around until the controlee closes the client_fd.
1840 */
1841 buffer_clear(&m);
1842 if (mux_client_read_packet(fd, &m) != 0) {
1843 if (errno == EPIPE ||
1844 (errno == EINTR && muxclient_terminate != 0))
1845 return 0;
1846 fatal("%s: mux_client_read_packet: %s",
1847 __func__, strerror(errno));
1848 }
1849 fatal("%s: master returned unexpected message %u", __func__, type);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001850}
1851
Damien Miller6c3eec72011-05-05 14:16:22 +10001852static void
1853mux_client_request_stop_listening(int fd)
1854{
1855 Buffer m;
1856 char *e;
1857 u_int type, rid;
1858
1859 debug3("%s: entering", __func__);
1860
1861 buffer_init(&m);
1862 buffer_put_int(&m, MUX_C_STOP_LISTENING);
1863 buffer_put_int(&m, muxclient_request_id);
1864
1865 if (mux_client_write_packet(fd, &m) != 0)
1866 fatal("%s: write packet: %s", __func__, strerror(errno));
1867
1868 buffer_clear(&m);
1869
1870 /* Read their reply */
1871 if (mux_client_read_packet(fd, &m) != 0)
1872 fatal("%s: read from master failed: %s",
1873 __func__, strerror(errno));
1874
1875 type = buffer_get_int(&m);
1876 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1877 fatal("%s: out of sequence reply: my id %u theirs %u",
1878 __func__, muxclient_request_id, rid);
1879 switch (type) {
1880 case MUX_S_OK:
1881 break;
1882 case MUX_S_PERMISSION_DENIED:
1883 e = buffer_get_string(&m, NULL);
1884 fatal("Master refused stop listening request: %s", e);
1885 case MUX_S_FAILURE:
1886 e = buffer_get_string(&m, NULL);
1887 fatal("%s: stop listening request failed: %s", __func__, e);
1888 default:
1889 fatal("%s: unexpected response from master 0x%08x",
1890 __func__, type);
1891 }
1892 buffer_free(&m);
1893 muxclient_request_id++;
1894}
1895
Damien Millerb1cbfa22008-05-19 16:00:08 +10001896/* Multiplex client main loop. */
1897void
1898muxclient(const char *path)
1899{
1900 struct sockaddr_un addr;
Damien Millere1537f92010-01-26 13:26:22 +11001901 socklen_t sun_len;
1902 int sock;
1903 u_int pid;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001904
Damien Millere1537f92010-01-26 13:26:22 +11001905 if (muxclient_command == 0) {
1906 if (stdio_forward_host != NULL)
1907 muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
1908 else
1909 muxclient_command = SSHMUX_COMMAND_OPEN;
1910 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001911
1912 switch (options.control_master) {
1913 case SSHCTL_MASTER_AUTO:
1914 case SSHCTL_MASTER_AUTO_ASK:
1915 debug("auto-mux: Trying existing master");
1916 /* FALLTHROUGH */
1917 case SSHCTL_MASTER_NO:
1918 break;
1919 default:
1920 return;
1921 }
1922
1923 memset(&addr, '\0', sizeof(addr));
1924 addr.sun_family = AF_UNIX;
Damien Millere1537f92010-01-26 13:26:22 +11001925 sun_len = offsetof(struct sockaddr_un, sun_path) +
Damien Millerb1cbfa22008-05-19 16:00:08 +10001926 strlen(path) + 1;
1927
1928 if (strlcpy(addr.sun_path, path,
1929 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1930 fatal("ControlPath too long");
1931
1932 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1933 fatal("%s socket(): %s", __func__, strerror(errno));
1934
Damien Millere1537f92010-01-26 13:26:22 +11001935 if (connect(sock, (struct sockaddr *)&addr, sun_len) == -1) {
1936 switch (muxclient_command) {
1937 case SSHMUX_COMMAND_OPEN:
1938 case SSHMUX_COMMAND_STDIO_FWD:
1939 break;
1940 default:
Damien Millerb1cbfa22008-05-19 16:00:08 +10001941 fatal("Control socket connect(%.100s): %s", path,
1942 strerror(errno));
1943 }
Damien Miller603134e2010-09-24 22:07:55 +10001944 if (errno == ECONNREFUSED &&
1945 options.control_master != SSHCTL_MASTER_NO) {
1946 debug("Stale control socket %.100s, unlinking", path);
1947 unlink(path);
1948 } else if (errno == ENOENT) {
Damien Millerb1cbfa22008-05-19 16:00:08 +10001949 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller603134e2010-09-24 22:07:55 +10001950 } else {
Damien Millerb1cbfa22008-05-19 16:00:08 +10001951 error("Control socket connect(%.100s): %s", path,
1952 strerror(errno));
1953 }
1954 close(sock);
1955 return;
1956 }
Damien Millere1537f92010-01-26 13:26:22 +11001957 set_nonblock(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001958
Damien Millere1537f92010-01-26 13:26:22 +11001959 if (mux_client_hello_exchange(sock) != 0) {
1960 error("%s: master hello exchange failed", __func__);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001961 close(sock);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001962 return;
1963 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001964
1965 switch (muxclient_command) {
1966 case SSHMUX_COMMAND_ALIVE_CHECK:
Damien Millere1537f92010-01-26 13:26:22 +11001967 if ((pid = mux_client_request_alive(sock)) == 0)
1968 fatal("%s: master alive check failed", __func__);
1969 fprintf(stderr, "Master running (pid=%d)\r\n", pid);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001970 exit(0);
1971 case SSHMUX_COMMAND_TERMINATE:
Damien Millere1537f92010-01-26 13:26:22 +11001972 mux_client_request_terminate(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001973 fprintf(stderr, "Exit request sent.\r\n");
1974 exit(0);
Damien Miller388f6fc2010-05-21 14:57:35 +10001975 case SSHMUX_COMMAND_FORWARD:
1976 if (mux_client_request_forwards(sock) != 0)
1977 fatal("%s: master forward request failed", __func__);
1978 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001979 case SSHMUX_COMMAND_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +11001980 if (mux_client_request_forwards(sock) != 0) {
1981 error("%s: master forward request failed", __func__);
1982 return;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001983 }
Damien Millere1537f92010-01-26 13:26:22 +11001984 mux_client_request_session(sock);
1985 return;
1986 case SSHMUX_COMMAND_STDIO_FWD:
1987 mux_client_request_stdio_fwd(sock);
1988 exit(0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001989 case SSHMUX_COMMAND_STOP:
1990 mux_client_request_stop_listening(sock);
1991 fprintf(stderr, "Stop listening request sent.\r\n");
1992 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001993 default:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001994 fatal("unrecognised muxclient_command %d", muxclient_command);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001995 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001996}