blob: 70c8d2adea0cf04c025832df4d7ea7486af42070 [file] [log] [blame]
Damien Millerc094d1e2010-06-26 09:36:34 +10001/* $OpenBSD: mux.c,v 1.19 2010/06/17 07:07:30 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
149#define MUX_S_OK 0x80000001
150#define MUX_S_PERMISSION_DENIED 0x80000002
151#define MUX_S_FAILURE 0x80000003
152#define MUX_S_EXIT_MESSAGE 0x80000004
153#define MUX_S_ALIVE 0x80000005
154#define MUX_S_SESSION_OPENED 0x80000006
Damien Miller388f6fc2010-05-21 14:57:35 +1000155#define MUX_S_REMOTE_PORT 0x80000007
Damien Millere1537f92010-01-26 13:26:22 +1100156
157/* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
158#define MUX_FWD_LOCAL 1
159#define MUX_FWD_REMOTE 2
160#define MUX_FWD_DYNAMIC 3
161
Damien Millerd530f5f2010-05-21 14:57:10 +1000162static void mux_session_confirm(int, int, void *);
Damien Millere1537f92010-01-26 13:26:22 +1100163
164static int process_mux_master_hello(u_int, Channel *, Buffer *, Buffer *);
165static int process_mux_new_session(u_int, Channel *, Buffer *, Buffer *);
166static int process_mux_alive_check(u_int, Channel *, Buffer *, Buffer *);
167static int process_mux_terminate(u_int, Channel *, Buffer *, Buffer *);
168static int process_mux_open_fwd(u_int, Channel *, Buffer *, Buffer *);
169static int process_mux_close_fwd(u_int, Channel *, Buffer *, Buffer *);
170static int process_mux_stdio_fwd(u_int, Channel *, Buffer *, Buffer *);
171
172static const struct {
173 u_int type;
174 int (*handler)(u_int, Channel *, Buffer *, Buffer *);
175} mux_master_handlers[] = {
176 { MUX_MSG_HELLO, process_mux_master_hello },
177 { MUX_C_NEW_SESSION, process_mux_new_session },
178 { MUX_C_ALIVE_CHECK, process_mux_alive_check },
179 { MUX_C_TERMINATE, process_mux_terminate },
180 { MUX_C_OPEN_FWD, process_mux_open_fwd },
181 { MUX_C_CLOSE_FWD, process_mux_close_fwd },
182 { MUX_C_NEW_STDIO_FWD, process_mux_stdio_fwd },
183 { 0, NULL }
184};
185
186/* Cleanup callback fired on closure of mux slave _session_ channel */
187/* ARGSUSED */
188static void
189mux_master_session_cleanup_cb(int cid, void *unused)
190{
191 Channel *cc, *c = channel_by_id(cid);
192
193 debug3("%s: entering for channel %d", __func__, cid);
194 if (c == NULL)
195 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
196 if (c->ctl_chan != -1) {
197 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
198 fatal("%s: channel %d missing control channel %d",
199 __func__, c->self, c->ctl_chan);
200 c->ctl_chan = -1;
201 cc->remote_id = -1;
202 chan_rcvd_oclose(cc);
203 }
204 channel_cancel_cleanup(c->self);
205}
206
207/* Cleanup callback fired on closure of mux slave _control_ channel */
208/* ARGSUSED */
209static void
210mux_master_control_cleanup_cb(int cid, void *unused)
211{
212 Channel *sc, *c = channel_by_id(cid);
213
214 debug3("%s: entering for channel %d", __func__, cid);
215 if (c == NULL)
216 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
217 if (c->remote_id != -1) {
218 if ((sc = channel_by_id(c->remote_id)) == NULL)
Damien Miller601a23c2010-04-16 15:54:01 +1000219 fatal("%s: channel %d missing session channel %d",
Damien Millere1537f92010-01-26 13:26:22 +1100220 __func__, c->self, c->remote_id);
221 c->remote_id = -1;
222 sc->ctl_chan = -1;
Damien Millera21cdfa2010-01-28 06:26:59 +1100223 if (sc->type != SSH_CHANNEL_OPEN) {
224 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) {
290 if (name != NULL)
291 xfree(name);
292 goto malf;
293 }
294 debug2("Unrecognised slave extension \"%s\"", name);
295 xfree(name);
296 xfree(value);
297 }
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;
316 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
317 buffer_get_int_ret(&cctx->want_tty, m) != 0 ||
318 buffer_get_int_ret(&cctx->want_x_fwd, m) != 0 ||
319 buffer_get_int_ret(&cctx->want_agent_fwd, m) != 0 ||
320 buffer_get_int_ret(&cctx->want_subsys, m) != 0 ||
321 buffer_get_int_ret(&escape_char, m) != 0 ||
322 (cctx->term = buffer_get_string_ret(m, &len)) == NULL ||
323 (cmd = buffer_get_string_ret(m, &len)) == NULL) {
324 malf:
325 if (cmd != NULL)
326 xfree(cmd);
327 if (reserved != NULL)
328 xfree(reserved);
329 if (cctx->term != NULL)
330 xfree(cctx->term);
331 error("%s: malformed message", __func__);
332 return -1;
333 }
334 xfree(reserved);
335 reserved = NULL;
336
337 cctx->env = NULL;
338 env_len = 0;
339 while (buffer_len(m) > 0) {
340#define MUX_MAX_ENV_VARS 4096
341 if ((cp = buffer_get_string_ret(m, &len)) == NULL) {
342 xfree(cmd);
343 goto malf;
344 }
345 if (!env_permitted(cp)) {
346 xfree(cp);
347 continue;
348 }
349 cctx->env = xrealloc(cctx->env, env_len + 2,
350 sizeof(*cctx->env));
351 cctx->env[env_len++] = cp;
352 cctx->env[env_len] = NULL;
353 if (env_len > MUX_MAX_ENV_VARS) {
354 error(">%d environment variables received, ignoring "
355 "additional", MUX_MAX_ENV_VARS);
356 break;
357 }
358 }
359
360 debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
361 "term \"%s\", cmd \"%s\", env %u", __func__, c->self,
362 cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
363 cctx->want_subsys, cctx->term, cmd, env_len);
364
365 buffer_init(&cctx->cmd);
366 buffer_append(&cctx->cmd, cmd, strlen(cmd));
367 xfree(cmd);
368 cmd = NULL;
369
370 /* Gather fds from client */
371 for(i = 0; i < 3; i++) {
372 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
373 error("%s: failed to receive fd %d from slave",
374 __func__, i);
375 for (j = 0; j < i; j++)
376 close(new_fd[j]);
377 for (j = 0; j < env_len; j++)
378 xfree(cctx->env[j]);
379 if (env_len > 0)
380 xfree(cctx->env);
381 xfree(cctx->term);
382 buffer_free(&cctx->cmd);
383 xfree(cctx);
384
385 /* prepare reply */
386 buffer_put_int(r, MUX_S_FAILURE);
387 buffer_put_int(r, rid);
388 buffer_put_cstring(r,
389 "did not receive file descriptors");
390 return -1;
391 }
392 }
393
394 debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
395 new_fd[0], new_fd[1], new_fd[2]);
396
397 /* XXX support multiple child sessions in future */
398 if (c->remote_id != -1) {
399 debug2("%s: session already open", __func__);
400 /* prepare reply */
401 buffer_put_int(r, MUX_S_FAILURE);
402 buffer_put_int(r, rid);
403 buffer_put_cstring(r, "Multiple sessions not supported");
404 cleanup:
405 close(new_fd[0]);
406 close(new_fd[1]);
407 close(new_fd[2]);
408 xfree(cctx->term);
409 if (env_len != 0) {
410 for (i = 0; i < env_len; i++)
411 xfree(cctx->env[i]);
412 xfree(cctx->env);
413 }
414 buffer_free(&cctx->cmd);
415 return 0;
416 }
417
418 if (options.control_master == SSHCTL_MASTER_ASK ||
419 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
420 if (!ask_permission("Allow shared connection to %s? ", host)) {
421 debug2("%s: session refused by user", __func__);
422 /* prepare reply */
423 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
424 buffer_put_int(r, rid);
425 buffer_put_cstring(r, "Permission denied");
426 goto cleanup;
427 }
428 }
429
430 /* Try to pick up ttymodes from client before it goes raw */
431 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
432 error("%s: tcgetattr: %s", __func__, strerror(errno));
433
434 /* enable nonblocking unless tty */
435 if (!isatty(new_fd[0]))
436 set_nonblock(new_fd[0]);
437 if (!isatty(new_fd[1]))
438 set_nonblock(new_fd[1]);
439 if (!isatty(new_fd[2]))
440 set_nonblock(new_fd[2]);
441
442 window = CHAN_SES_WINDOW_DEFAULT;
443 packetmax = CHAN_SES_PACKET_DEFAULT;
444 if (cctx->want_tty) {
445 window >>= 1;
446 packetmax >>= 1;
447 }
448
449 nc = channel_new("session", SSH_CHANNEL_OPENING,
450 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
451 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
452
453 nc->ctl_chan = c->self; /* link session -> control channel */
454 c->remote_id = nc->self; /* link control -> session channel */
455
456 if (cctx->want_tty && escape_char != 0xffffffff) {
457 channel_register_filter(nc->self,
458 client_simple_escape_filter, NULL,
459 client_filter_cleanup,
460 client_new_escape_filter_ctx((int)escape_char));
461 }
462
463 debug2("%s: channel_new: %d linked to control channel %d",
464 __func__, nc->self, nc->ctl_chan);
465
466 channel_send_open(nc->self);
467 channel_register_open_confirm(nc->self, mux_session_confirm, cctx);
Damien Millerd530f5f2010-05-21 14:57:10 +1000468 c->mux_pause = 1; /* stop handling messages until open_confirm done */
Damien Miller85c50d72010-05-10 11:53:02 +1000469 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +1100470
Damien Millerd530f5f2010-05-21 14:57:10 +1000471 /* reply is deferred, sent by mux_session_confirm */
Damien Millere1537f92010-01-26 13:26:22 +1100472 return 0;
473}
474
475static int
476process_mux_alive_check(u_int rid, Channel *c, Buffer *m, Buffer *r)
477{
478 debug2("%s: channel %d: alive check", __func__, c->self);
479
480 /* prepare reply */
481 buffer_put_int(r, MUX_S_ALIVE);
482 buffer_put_int(r, rid);
483 buffer_put_int(r, (u_int)getpid());
484
485 return 0;
486}
487
488static int
489process_mux_terminate(u_int rid, Channel *c, Buffer *m, Buffer *r)
490{
491 debug2("%s: channel %d: terminate request", __func__, c->self);
492
493 if (options.control_master == SSHCTL_MASTER_ASK ||
494 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
495 if (!ask_permission("Terminate shared connection to %s? ",
496 host)) {
497 debug2("%s: termination refused by user", __func__);
498 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
499 buffer_put_int(r, rid);
500 buffer_put_cstring(r, "Permission denied");
501 return 0;
502 }
503 }
504
505 quit_pending = 1;
506 buffer_put_int(r, MUX_S_OK);
507 buffer_put_int(r, rid);
508 /* XXX exit happens too soon - message never makes it to client */
509 return 0;
510}
511
512static char *
513format_forward(u_int ftype, Forward *fwd)
514{
515 char *ret;
516
517 switch (ftype) {
518 case MUX_FWD_LOCAL:
519 xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
520 (fwd->listen_host == NULL) ?
521 (options.gateway_ports ? "*" : "LOCALHOST") :
522 fwd->listen_host, fwd->listen_port,
523 fwd->connect_host, fwd->connect_port);
524 break;
525 case MUX_FWD_DYNAMIC:
526 xasprintf(&ret, "dynamic forward %.200s:%d -> *",
527 (fwd->listen_host == NULL) ?
528 (options.gateway_ports ? "*" : "LOCALHOST") :
529 fwd->listen_host, fwd->listen_port);
530 break;
531 case MUX_FWD_REMOTE:
532 xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
533 (fwd->listen_host == NULL) ?
534 "LOCALHOST" : fwd->listen_host,
535 fwd->listen_port,
536 fwd->connect_host, fwd->connect_port);
537 break;
538 default:
539 fatal("%s: unknown forward type %u", __func__, ftype);
540 }
541 return ret;
542}
543
544static int
545compare_host(const char *a, const char *b)
546{
547 if (a == NULL && b == NULL)
548 return 1;
549 if (a == NULL || b == NULL)
550 return 0;
551 return strcmp(a, b) == 0;
552}
553
554static int
555compare_forward(Forward *a, Forward *b)
556{
557 if (!compare_host(a->listen_host, b->listen_host))
558 return 0;
559 if (a->listen_port != b->listen_port)
560 return 0;
561 if (!compare_host(a->connect_host, b->connect_host))
562 return 0;
563 if (a->connect_port != b->connect_port)
564 return 0;
565
566 return 1;
567}
568
Damien Miller388f6fc2010-05-21 14:57:35 +1000569static void
570mux_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
571{
572 struct mux_channel_confirm_ctx *fctx = ctxt;
573 char *failmsg = NULL;
574 Forward *rfwd;
575 Channel *c;
576 Buffer out;
577
578 if ((c = channel_by_id(fctx->cid)) == NULL) {
579 /* no channel for reply */
580 error("%s: unknown channel", __func__);
581 return;
582 }
583 buffer_init(&out);
584 if (fctx->fid >= options.num_remote_forwards) {
585 xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
586 goto fail;
587 }
588 rfwd = &options.remote_forwards[fctx->fid];
589 debug("%s: %s for: listen %d, connect %s:%d", __func__,
590 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
591 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
592 if (type == SSH2_MSG_REQUEST_SUCCESS) {
593 if (rfwd->listen_port == 0) {
594 rfwd->allocated_port = packet_get_int();
595 logit("Allocated port %u for mux remote forward"
596 " to %s:%d", rfwd->allocated_port,
597 rfwd->connect_host, rfwd->connect_port);
598 buffer_put_int(&out, MUX_S_REMOTE_PORT);
599 buffer_put_int(&out, fctx->rid);
600 buffer_put_int(&out, rfwd->allocated_port);
601 } else {
602 buffer_put_int(&out, MUX_S_OK);
603 buffer_put_int(&out, fctx->rid);
604 }
605 goto out;
606 } else {
607 xasprintf(&failmsg, "remote port forwarding failed for "
608 "listen port %d", rfwd->listen_port);
609 }
610 fail:
611 error("%s: %s", __func__, failmsg);
612 buffer_put_int(&out, MUX_S_FAILURE);
613 buffer_put_int(&out, fctx->rid);
614 buffer_put_cstring(&out, failmsg);
615 xfree(failmsg);
616 out:
617 buffer_put_string(&c->output, buffer_ptr(&out), buffer_len(&out));
618 buffer_free(&out);
619 if (c->mux_pause <= 0)
620 fatal("%s: mux_pause %d", __func__, c->mux_pause);
621 c->mux_pause = 0; /* start processing messages again */
622}
623
Damien Millere1537f92010-01-26 13:26:22 +1100624static int
625process_mux_open_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
626{
627 Forward fwd;
628 char *fwd_desc = NULL;
629 u_int ftype;
630 int i, ret = 0, freefwd = 1;
631
632 fwd.listen_host = fwd.connect_host = NULL;
633 if (buffer_get_int_ret(&ftype, m) != 0 ||
634 (fwd.listen_host = buffer_get_string_ret(m, NULL)) == NULL ||
635 buffer_get_int_ret(&fwd.listen_port, m) != 0 ||
636 (fwd.connect_host = buffer_get_string_ret(m, NULL)) == NULL ||
637 buffer_get_int_ret(&fwd.connect_port, m) != 0) {
638 error("%s: malformed message", __func__);
639 ret = -1;
640 goto out;
641 }
642
643 if (*fwd.listen_host == '\0') {
644 xfree(fwd.listen_host);
645 fwd.listen_host = NULL;
646 }
647 if (*fwd.connect_host == '\0') {
648 xfree(fwd.connect_host);
649 fwd.connect_host = NULL;
650 }
651
652 debug2("%s: channel %d: request %s", __func__, c->self,
653 (fwd_desc = format_forward(ftype, &fwd)));
654
655 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
656 ftype != MUX_FWD_DYNAMIC) {
657 logit("%s: invalid forwarding type %u", __func__, ftype);
658 invalid:
Damien Miller388f6fc2010-05-21 14:57:35 +1000659 if (fwd.listen_host)
660 xfree(fwd.listen_host);
661 if (fwd.connect_host)
662 xfree(fwd.connect_host);
Damien Millere1537f92010-01-26 13:26:22 +1100663 buffer_put_int(r, MUX_S_FAILURE);
664 buffer_put_int(r, rid);
665 buffer_put_cstring(r, "Invalid forwarding request");
666 return 0;
667 }
Damien Miller388f6fc2010-05-21 14:57:35 +1000668 if (fwd.listen_port >= 65536) {
Damien Millere1537f92010-01-26 13:26:22 +1100669 logit("%s: invalid listen port %u", __func__,
670 fwd.listen_port);
671 goto invalid;
672 }
673 if (fwd.connect_port >= 65536 || (ftype != MUX_FWD_DYNAMIC &&
674 ftype != MUX_FWD_REMOTE && fwd.connect_port == 0)) {
675 logit("%s: invalid connect port %u", __func__,
676 fwd.connect_port);
677 goto invalid;
678 }
679 if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL) {
680 logit("%s: missing connect host", __func__);
681 goto invalid;
682 }
683
684 /* Skip forwards that have already been requested */
685 switch (ftype) {
686 case MUX_FWD_LOCAL:
687 case MUX_FWD_DYNAMIC:
688 for (i = 0; i < options.num_local_forwards; i++) {
689 if (compare_forward(&fwd,
690 options.local_forwards + i)) {
691 exists:
692 debug2("%s: found existing forwarding",
693 __func__);
694 buffer_put_int(r, MUX_S_OK);
695 buffer_put_int(r, rid);
696 goto out;
697 }
698 }
699 break;
700 case MUX_FWD_REMOTE:
701 for (i = 0; i < options.num_remote_forwards; i++) {
702 if (compare_forward(&fwd,
Damien Miller388f6fc2010-05-21 14:57:35 +1000703 options.remote_forwards + i)) {
704 if (fwd.listen_port != 0)
705 goto exists;
706 debug2("%s: found allocated port",
707 __func__);
708 buffer_put_int(r, MUX_S_REMOTE_PORT);
709 buffer_put_int(r, rid);
710 buffer_put_int(r,
711 options.remote_forwards[i].allocated_port);
712 goto out;
713 }
Damien Millere1537f92010-01-26 13:26:22 +1100714 }
715 break;
716 }
717
718 if (options.control_master == SSHCTL_MASTER_ASK ||
719 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
720 if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
721 debug2("%s: forwarding refused by user", __func__);
722 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
723 buffer_put_int(r, rid);
724 buffer_put_cstring(r, "Permission denied");
725 goto out;
726 }
727 }
728
729 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
730 if (options.num_local_forwards + 1 >=
731 SSH_MAX_FORWARDS_PER_DIRECTION ||
732 channel_setup_local_fwd_listener(fwd.listen_host,
733 fwd.listen_port, fwd.connect_host, fwd.connect_port,
734 options.gateway_ports) < 0) {
735 fail:
736 logit("slave-requested %s failed", fwd_desc);
737 buffer_put_int(r, MUX_S_FAILURE);
738 buffer_put_int(r, rid);
739 buffer_put_cstring(r, "Port forwarding failed");
740 goto out;
741 }
742 add_local_forward(&options, &fwd);
743 freefwd = 0;
744 } else {
Damien Miller388f6fc2010-05-21 14:57:35 +1000745 struct mux_channel_confirm_ctx *fctx;
746
Damien Millere1537f92010-01-26 13:26:22 +1100747 if (options.num_remote_forwards + 1 >=
748 SSH_MAX_FORWARDS_PER_DIRECTION ||
749 channel_request_remote_forwarding(fwd.listen_host,
750 fwd.listen_port, fwd.connect_host, fwd.connect_port) < 0)
751 goto fail;
752 add_remote_forward(&options, &fwd);
Damien Miller388f6fc2010-05-21 14:57:35 +1000753 fctx = xcalloc(1, sizeof(*fctx));
754 fctx->cid = c->self;
755 fctx->rid = rid;
756 fctx->fid = options.num_remote_forwards-1;
757 client_register_global_confirm(mux_confirm_remote_forward,
758 fctx);
Damien Millere1537f92010-01-26 13:26:22 +1100759 freefwd = 0;
Damien Miller388f6fc2010-05-21 14:57:35 +1000760 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
761 /* delayed reply in mux_confirm_remote_forward */
762 goto out;
Damien Millere1537f92010-01-26 13:26:22 +1100763 }
764 buffer_put_int(r, MUX_S_OK);
765 buffer_put_int(r, rid);
766 out:
767 if (fwd_desc != NULL)
768 xfree(fwd_desc);
769 if (freefwd) {
770 if (fwd.listen_host != NULL)
771 xfree(fwd.listen_host);
772 if (fwd.connect_host != NULL)
773 xfree(fwd.connect_host);
774 }
775 return ret;
776}
777
778static int
779process_mux_close_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
780{
781 Forward fwd;
782 char *fwd_desc = NULL;
783 u_int ftype;
784 int ret = 0;
785
786 fwd.listen_host = fwd.connect_host = NULL;
787 if (buffer_get_int_ret(&ftype, m) != 0 ||
788 (fwd.listen_host = buffer_get_string_ret(m, NULL)) == NULL ||
789 buffer_get_int_ret(&fwd.listen_port, m) != 0 ||
790 (fwd.connect_host = buffer_get_string_ret(m, NULL)) == NULL ||
791 buffer_get_int_ret(&fwd.connect_port, m) != 0) {
792 error("%s: malformed message", __func__);
793 ret = -1;
794 goto out;
795 }
796
797 if (*fwd.listen_host == '\0') {
798 xfree(fwd.listen_host);
799 fwd.listen_host = NULL;
800 }
801 if (*fwd.connect_host == '\0') {
802 xfree(fwd.connect_host);
803 fwd.connect_host = NULL;
804 }
805
806 debug2("%s: channel %d: request %s", __func__, c->self,
807 (fwd_desc = format_forward(ftype, &fwd)));
808
809 /* XXX implement this */
810 buffer_put_int(r, MUX_S_FAILURE);
811 buffer_put_int(r, rid);
812 buffer_put_cstring(r, "unimplemented");
813
814 out:
815 if (fwd_desc != NULL)
816 xfree(fwd_desc);
817 if (fwd.listen_host != NULL)
818 xfree(fwd.listen_host);
819 if (fwd.connect_host != NULL)
820 xfree(fwd.connect_host);
821
822 return ret;
823}
824
825static int
826process_mux_stdio_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
827{
828 Channel *nc;
829 char *reserved, *chost;
830 u_int cport, i, j;
831 int new_fd[2];
832
833 chost = reserved = NULL;
834 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
835 (chost = buffer_get_string_ret(m, NULL)) == NULL ||
836 buffer_get_int_ret(&cport, m) != 0) {
837 if (reserved != NULL)
838 xfree(reserved);
839 if (chost != NULL)
840 xfree(chost);
841 error("%s: malformed message", __func__);
842 return -1;
843 }
844 xfree(reserved);
845
846 debug2("%s: channel %d: request stdio fwd to %s:%u",
847 __func__, c->self, chost, cport);
848
849 /* Gather fds from client */
850 for(i = 0; i < 2; i++) {
851 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
852 error("%s: failed to receive fd %d from slave",
853 __func__, i);
854 for (j = 0; j < i; j++)
855 close(new_fd[j]);
856 xfree(chost);
857
858 /* prepare reply */
859 buffer_put_int(r, MUX_S_FAILURE);
860 buffer_put_int(r, rid);
861 buffer_put_cstring(r,
862 "did not receive file descriptors");
863 return -1;
864 }
865 }
866
867 debug3("%s: got fds stdin %d, stdout %d", __func__,
868 new_fd[0], new_fd[1]);
869
870 /* XXX support multiple child sessions in future */
871 if (c->remote_id != -1) {
872 debug2("%s: session already open", __func__);
873 /* prepare reply */
874 buffer_put_int(r, MUX_S_FAILURE);
875 buffer_put_int(r, rid);
876 buffer_put_cstring(r, "Multiple sessions not supported");
877 cleanup:
878 close(new_fd[0]);
879 close(new_fd[1]);
880 xfree(chost);
881 return 0;
882 }
883
884 if (options.control_master == SSHCTL_MASTER_ASK ||
885 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
886 if (!ask_permission("Allow forward to to %s:%u? ",
887 chost, cport)) {
888 debug2("%s: stdio fwd refused by user", __func__);
889 /* prepare reply */
890 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
891 buffer_put_int(r, rid);
892 buffer_put_cstring(r, "Permission denied");
893 goto cleanup;
894 }
895 }
896
897 /* enable nonblocking unless tty */
898 if (!isatty(new_fd[0]))
899 set_nonblock(new_fd[0]);
900 if (!isatty(new_fd[1]))
901 set_nonblock(new_fd[1]);
902
903 nc = channel_connect_stdio_fwd(chost, cport, new_fd[0], new_fd[1]);
904
905 nc->ctl_chan = c->self; /* link session -> control channel */
906 c->remote_id = nc->self; /* link control -> session channel */
907
908 debug2("%s: channel_new: %d linked to control channel %d",
909 __func__, nc->self, nc->ctl_chan);
910
Damien Miller85c50d72010-05-10 11:53:02 +1000911 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +1100912
913 /* prepare reply */
914 /* XXX defer until channel confirmed */
915 buffer_put_int(r, MUX_S_SESSION_OPENED);
916 buffer_put_int(r, rid);
917 buffer_put_int(r, nc->self);
918
919 return 0;
920}
921
922/* Channel callbacks fired on read/write from mux slave fd */
923static int
924mux_master_read_cb(Channel *c)
925{
926 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
927 Buffer in, out;
928 void *ptr;
929 u_int type, rid, have, i;
930 int ret = -1;
931
932 /* Setup ctx and */
933 if (c->mux_ctx == NULL) {
Damien Millerc094d1e2010-06-26 09:36:34 +1000934 state = xcalloc(1, sizeof(*state));
Damien Millere1537f92010-01-26 13:26:22 +1100935 c->mux_ctx = state;
936 channel_register_cleanup(c->self,
937 mux_master_control_cleanup_cb, 0);
938
939 /* Send hello */
940 buffer_init(&out);
941 buffer_put_int(&out, MUX_MSG_HELLO);
942 buffer_put_int(&out, SSHMUX_VER);
943 /* no extensions */
944 buffer_put_string(&c->output, buffer_ptr(&out),
945 buffer_len(&out));
946 buffer_free(&out);
947 debug3("%s: channel %d: hello sent", __func__, c->self);
948 return 0;
949 }
950
951 buffer_init(&in);
952 buffer_init(&out);
953
954 /* Channel code ensures that we receive whole packets */
955 if ((ptr = buffer_get_string_ptr_ret(&c->input, &have)) == NULL) {
956 malf:
957 error("%s: malformed message", __func__);
958 goto out;
959 }
960 buffer_append(&in, ptr, have);
961
962 if (buffer_get_int_ret(&type, &in) != 0)
963 goto malf;
964 debug3("%s: channel %d packet type 0x%08x len %u",
965 __func__, c->self, type, buffer_len(&in));
966
967 if (type == MUX_MSG_HELLO)
968 rid = 0;
969 else {
970 if (!state->hello_rcvd) {
971 error("%s: expected MUX_MSG_HELLO(0x%08x), "
972 "received 0x%08x", __func__, MUX_MSG_HELLO, type);
973 goto out;
974 }
975 if (buffer_get_int_ret(&rid, &in) != 0)
976 goto malf;
977 }
978
979 for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
980 if (type == mux_master_handlers[i].type) {
981 ret = mux_master_handlers[i].handler(rid, c, &in, &out);
982 break;
983 }
984 }
985 if (mux_master_handlers[i].handler == NULL) {
986 error("%s: unsupported mux message 0x%08x", __func__, type);
987 buffer_put_int(&out, MUX_S_FAILURE);
988 buffer_put_int(&out, rid);
989 buffer_put_cstring(&out, "unsupported request");
990 ret = 0;
991 }
992 /* Enqueue reply packet */
993 if (buffer_len(&out) != 0) {
994 buffer_put_string(&c->output, buffer_ptr(&out),
995 buffer_len(&out));
996 }
997 out:
998 buffer_free(&in);
999 buffer_free(&out);
1000 return ret;
1001}
1002
1003void
1004mux_exit_message(Channel *c, int exitval)
1005{
1006 Buffer m;
1007 Channel *mux_chan;
1008
1009 debug3("%s: channel %d: exit message, evitval %d", __func__, c->self,
1010 exitval);
1011
1012 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL)
1013 fatal("%s: channel %d missing mux channel %d",
1014 __func__, c->self, c->ctl_chan);
1015
1016 /* Append exit message packet to control socket output queue */
1017 buffer_init(&m);
1018 buffer_put_int(&m, MUX_S_EXIT_MESSAGE);
1019 buffer_put_int(&m, c->self);
1020 buffer_put_int(&m, exitval);
1021
1022 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m));
1023 buffer_free(&m);
1024}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001025
1026/* Prepare a mux master to listen on a Unix domain socket. */
1027void
1028muxserver_listen(void)
1029{
1030 struct sockaddr_un addr;
Damien Millere1537f92010-01-26 13:26:22 +11001031 socklen_t sun_len;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001032 mode_t old_umask;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001033
1034 if (options.control_path == NULL ||
1035 options.control_master == SSHCTL_MASTER_NO)
1036 return;
1037
1038 debug("setting up multiplex master socket");
1039
1040 memset(&addr, '\0', sizeof(addr));
1041 addr.sun_family = AF_UNIX;
Damien Millere1537f92010-01-26 13:26:22 +11001042 sun_len = offsetof(struct sockaddr_un, sun_path) +
Damien Millerb1cbfa22008-05-19 16:00:08 +10001043 strlen(options.control_path) + 1;
1044
1045 if (strlcpy(addr.sun_path, options.control_path,
1046 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1047 fatal("ControlPath too long");
1048
1049 if ((muxserver_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1050 fatal("%s socket(): %s", __func__, strerror(errno));
1051
1052 old_umask = umask(0177);
Damien Millere1537f92010-01-26 13:26:22 +11001053 if (bind(muxserver_sock, (struct sockaddr *)&addr, sun_len) == -1) {
Damien Millerb1cbfa22008-05-19 16:00:08 +10001054 muxserver_sock = -1;
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001055 if (errno == EINVAL || errno == EADDRINUSE) {
1056 error("ControlSocket %s already exists, "
1057 "disabling multiplexing", options.control_path);
1058 close(muxserver_sock);
1059 muxserver_sock = -1;
1060 xfree(options.control_path);
1061 options.control_path = NULL;
1062 options.control_master = SSHCTL_MASTER_NO;
1063 return;
1064 } else
Damien Millerb1cbfa22008-05-19 16:00:08 +10001065 fatal("%s bind(): %s", __func__, strerror(errno));
1066 }
1067 umask(old_umask);
1068
1069 if (listen(muxserver_sock, 64) == -1)
1070 fatal("%s listen(): %s", __func__, strerror(errno));
1071
1072 set_nonblock(muxserver_sock);
Damien Millere1537f92010-01-26 13:26:22 +11001073
1074 mux_listener_channel = channel_new("mux listener",
1075 SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
1076 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1077 0, addr.sun_path, 1);
1078 mux_listener_channel->mux_rcb = mux_master_read_cb;
1079 debug3("%s: mux listener channel %d fd %d", __func__,
1080 mux_listener_channel->self, mux_listener_channel->sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001081}
1082
1083/* Callback on open confirmation in mux master for a mux client session. */
1084static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001085mux_session_confirm(int id, int success, void *arg)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001086{
1087 struct mux_session_confirm_ctx *cctx = arg;
1088 const char *display;
Damien Millerd530f5f2010-05-21 14:57:10 +10001089 Channel *c, *cc;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001090 int i;
Damien Millerd530f5f2010-05-21 14:57:10 +10001091 Buffer reply;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001092
1093 if (cctx == NULL)
1094 fatal("%s: cctx == NULL", __func__);
Damien Millere1537f92010-01-26 13:26:22 +11001095 if ((c = channel_by_id(id)) == NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001096 fatal("%s: no channel for id %d", __func__, id);
Damien Millerd530f5f2010-05-21 14:57:10 +10001097 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
1098 fatal("%s: channel %d lacks control channel %d", __func__,
1099 id, c->ctl_chan);
1100
1101 if (!success) {
1102 debug3("%s: sending failure reply", __func__);
1103 /* prepare reply */
1104 buffer_init(&reply);
1105 buffer_put_int(&reply, MUX_S_FAILURE);
1106 buffer_put_int(&reply, cctx->rid);
1107 buffer_put_cstring(&reply, "Session open refused by peer");
1108 goto done;
1109 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001110
1111 display = getenv("DISPLAY");
1112 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1113 char *proto, *data;
1114 /* Get reasonable local authentication information. */
1115 client_x11_get_proto(display, options.xauth_location,
1116 options.forward_x11_trusted, &proto, &data);
1117 /* Request forwarding with authentication spoofing. */
1118 debug("Requesting X11 forwarding with authentication spoofing.");
1119 x11_request_forwarding_with_spoofing(id, display, proto, data);
1120 /* XXX wait for reply */
1121 }
1122
1123 if (cctx->want_agent_fwd && options.forward_agent) {
1124 debug("Requesting authentication agent forwarding.");
1125 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1126 packet_send();
1127 }
1128
1129 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
1130 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
1131
Damien Millerd530f5f2010-05-21 14:57:10 +10001132 debug3("%s: sending success reply", __func__);
1133 /* prepare reply */
1134 buffer_init(&reply);
1135 buffer_put_int(&reply, MUX_S_SESSION_OPENED);
1136 buffer_put_int(&reply, cctx->rid);
1137 buffer_put_int(&reply, c->self);
1138
1139 done:
1140 /* Send reply */
1141 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply));
1142 buffer_free(&reply);
1143
1144 if (cc->mux_pause <= 0)
1145 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1146 cc->mux_pause = 0; /* start processing messages again */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001147 c->open_confirm_ctx = NULL;
1148 buffer_free(&cctx->cmd);
1149 xfree(cctx->term);
1150 if (cctx->env != NULL) {
1151 for (i = 0; cctx->env[i] != NULL; i++)
1152 xfree(cctx->env[i]);
1153 xfree(cctx->env);
1154 }
1155 xfree(cctx);
1156}
1157
Damien Millerb1cbfa22008-05-19 16:00:08 +10001158/* ** Multiplexing client support */
1159
1160/* Exit signal handler */
1161static void
1162control_client_sighandler(int signo)
1163{
1164 muxclient_terminate = signo;
1165}
1166
1167/*
1168 * Relay signal handler - used to pass some signals from mux client to
1169 * mux master.
1170 */
1171static void
1172control_client_sigrelay(int signo)
1173{
1174 int save_errno = errno;
1175
1176 if (muxserver_pid > 1)
1177 kill(muxserver_pid, signo);
1178
1179 errno = save_errno;
1180}
1181
Damien Millerb1cbfa22008-05-19 16:00:08 +10001182static int
Damien Millere1537f92010-01-26 13:26:22 +11001183mux_client_read(int fd, Buffer *b, u_int need)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001184{
Damien Millere1537f92010-01-26 13:26:22 +11001185 u_int have;
1186 ssize_t len;
1187 u_char *p;
1188 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001189
Damien Millere1537f92010-01-26 13:26:22 +11001190 pfd.fd = fd;
1191 pfd.events = POLLIN;
1192 p = buffer_append_space(b, need);
1193 for (have = 0; have < need; ) {
1194 if (muxclient_terminate) {
1195 errno = EINTR;
1196 return -1;
1197 }
1198 len = read(fd, p + have, need - have);
1199 if (len < 0) {
1200 switch (errno) {
1201#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1202 case EWOULDBLOCK:
1203#endif
1204 case EAGAIN:
1205 (void)poll(&pfd, 1, -1);
1206 /* FALLTHROUGH */
1207 case EINTR:
1208 continue;
1209 default:
1210 return -1;
1211 }
1212 }
1213 if (len == 0) {
1214 errno = EPIPE;
1215 return -1;
1216 }
1217 have += (u_int)len;
1218 }
1219 return 0;
1220}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001221
Damien Millere1537f92010-01-26 13:26:22 +11001222static int
1223mux_client_write_packet(int fd, Buffer *m)
1224{
1225 Buffer queue;
1226 u_int have, need;
1227 int oerrno, len;
1228 u_char *ptr;
1229 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001230
Damien Millere1537f92010-01-26 13:26:22 +11001231 pfd.fd = fd;
1232 pfd.events = POLLOUT;
1233 buffer_init(&queue);
1234 buffer_put_string(&queue, buffer_ptr(m), buffer_len(m));
1235
1236 need = buffer_len(&queue);
1237 ptr = buffer_ptr(&queue);
1238
1239 for (have = 0; have < need; ) {
1240 if (muxclient_terminate) {
1241 buffer_free(&queue);
1242 errno = EINTR;
1243 return -1;
1244 }
1245 len = write(fd, ptr + have, need - have);
1246 if (len < 0) {
1247 switch (errno) {
1248#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1249 case EWOULDBLOCK:
1250#endif
1251 case EAGAIN:
1252 (void)poll(&pfd, 1, -1);
1253 /* FALLTHROUGH */
1254 case EINTR:
1255 continue;
1256 default:
1257 oerrno = errno;
1258 buffer_free(&queue);
1259 errno = oerrno;
1260 return -1;
1261 }
1262 }
1263 if (len == 0) {
1264 buffer_free(&queue);
1265 errno = EPIPE;
1266 return -1;
1267 }
1268 have += (u_int)len;
1269 }
1270 buffer_free(&queue);
1271 return 0;
1272}
1273
1274static int
1275mux_client_read_packet(int fd, Buffer *m)
1276{
1277 Buffer queue;
1278 u_int need, have;
1279 void *ptr;
1280 int oerrno;
1281
1282 buffer_init(&queue);
1283 if (mux_client_read(fd, &queue, 4) != 0) {
1284 if ((oerrno = errno) == EPIPE)
1285 debug3("%s: read header failed: %s", __func__, strerror(errno));
1286 errno = oerrno;
1287 return -1;
1288 }
1289 need = get_u32(buffer_ptr(&queue));
1290 if (mux_client_read(fd, &queue, need) != 0) {
1291 oerrno = errno;
1292 debug3("%s: read body failed: %s", __func__, strerror(errno));
1293 errno = oerrno;
1294 return -1;
1295 }
1296 ptr = buffer_get_string_ptr(&queue, &have);
1297 buffer_append(m, ptr, have);
1298 buffer_free(&queue);
1299 return 0;
1300}
1301
1302static int
1303mux_client_hello_exchange(int fd)
1304{
1305 Buffer m;
1306 u_int type, ver;
1307
1308 buffer_init(&m);
1309 buffer_put_int(&m, MUX_MSG_HELLO);
1310 buffer_put_int(&m, SSHMUX_VER);
1311 /* no extensions */
1312
1313 if (mux_client_write_packet(fd, &m) != 0)
1314 fatal("%s: write packet: %s", __func__, strerror(errno));
1315
1316 buffer_clear(&m);
1317
1318 /* Read their HELLO */
1319 if (mux_client_read_packet(fd, &m) != 0) {
1320 buffer_free(&m);
1321 return -1;
1322 }
1323
1324 type = buffer_get_int(&m);
1325 if (type != MUX_MSG_HELLO)
1326 fatal("%s: expected HELLO (%u) received %u",
1327 __func__, MUX_MSG_HELLO, type);
1328 ver = buffer_get_int(&m);
1329 if (ver != SSHMUX_VER)
1330 fatal("Unsupported multiplexing protocol version %d "
1331 "(expected %d)", ver, SSHMUX_VER);
1332 debug2("%s: master version %u", __func__, ver);
1333 /* No extensions are presently defined */
1334 while (buffer_len(&m) > 0) {
1335 char *name = buffer_get_string(&m, NULL);
1336 char *value = buffer_get_string(&m, NULL);
1337
1338 debug2("Unrecognised master extension \"%s\"", name);
1339 xfree(name);
1340 xfree(value);
1341 }
1342 buffer_free(&m);
1343 return 0;
1344}
1345
1346static u_int
1347mux_client_request_alive(int fd)
1348{
1349 Buffer m;
1350 char *e;
1351 u_int pid, type, rid;
1352
1353 debug3("%s: entering", __func__);
1354
1355 buffer_init(&m);
1356 buffer_put_int(&m, MUX_C_ALIVE_CHECK);
1357 buffer_put_int(&m, muxclient_request_id);
1358
1359 if (mux_client_write_packet(fd, &m) != 0)
1360 fatal("%s: write packet: %s", __func__, strerror(errno));
1361
1362 buffer_clear(&m);
1363
1364 /* Read their reply */
1365 if (mux_client_read_packet(fd, &m) != 0) {
1366 buffer_free(&m);
1367 return 0;
1368 }
1369
1370 type = buffer_get_int(&m);
1371 if (type != MUX_S_ALIVE) {
1372 e = buffer_get_string(&m, NULL);
1373 fatal("%s: master returned error: %s", __func__, e);
1374 }
1375
1376 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1377 fatal("%s: out of sequence reply: my id %u theirs %u",
1378 __func__, muxclient_request_id, rid);
1379 pid = buffer_get_int(&m);
1380 buffer_free(&m);
1381
1382 debug3("%s: done pid = %u", __func__, pid);
1383
1384 muxclient_request_id++;
1385
1386 return pid;
1387}
1388
1389static void
1390mux_client_request_terminate(int fd)
1391{
1392 Buffer m;
1393 char *e;
1394 u_int type, rid;
1395
1396 debug3("%s: entering", __func__);
1397
1398 buffer_init(&m);
1399 buffer_put_int(&m, MUX_C_TERMINATE);
1400 buffer_put_int(&m, muxclient_request_id);
1401
1402 if (mux_client_write_packet(fd, &m) != 0)
1403 fatal("%s: write packet: %s", __func__, strerror(errno));
1404
1405 buffer_clear(&m);
1406
1407 /* Read their reply */
1408 if (mux_client_read_packet(fd, &m) != 0) {
1409 /* Remote end exited already */
1410 if (errno == EPIPE) {
1411 buffer_free(&m);
1412 return;
1413 }
1414 fatal("%s: read from master failed: %s",
1415 __func__, strerror(errno));
1416 }
1417
1418 type = buffer_get_int(&m);
1419 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1420 fatal("%s: out of sequence reply: my id %u theirs %u",
1421 __func__, muxclient_request_id, rid);
1422 switch (type) {
1423 case MUX_S_OK:
1424 break;
1425 case MUX_S_PERMISSION_DENIED:
1426 e = buffer_get_string(&m, NULL);
1427 fatal("Master refused termination request: %s", e);
1428 case MUX_S_FAILURE:
1429 e = buffer_get_string(&m, NULL);
1430 fatal("%s: termination request failed: %s", __func__, e);
1431 default:
1432 fatal("%s: unexpected response from master 0x%08x",
1433 __func__, type);
1434 }
1435 buffer_free(&m);
1436 muxclient_request_id++;
1437}
1438
1439static int
1440mux_client_request_forward(int fd, u_int ftype, Forward *fwd)
1441{
1442 Buffer m;
1443 char *e, *fwd_desc;
1444 u_int type, rid;
1445
1446 fwd_desc = format_forward(ftype, fwd);
1447 debug("Requesting %s", fwd_desc);
1448 xfree(fwd_desc);
1449
1450 buffer_init(&m);
1451 buffer_put_int(&m, MUX_C_OPEN_FWD);
1452 buffer_put_int(&m, muxclient_request_id);
1453 buffer_put_int(&m, ftype);
1454 buffer_put_cstring(&m,
1455 fwd->listen_host == NULL ? "" : fwd->listen_host);
1456 buffer_put_int(&m, fwd->listen_port);
1457 buffer_put_cstring(&m,
1458 fwd->connect_host == NULL ? "" : fwd->connect_host);
1459 buffer_put_int(&m, fwd->connect_port);
1460
1461 if (mux_client_write_packet(fd, &m) != 0)
1462 fatal("%s: write packet: %s", __func__, strerror(errno));
1463
1464 buffer_clear(&m);
1465
1466 /* Read their reply */
1467 if (mux_client_read_packet(fd, &m) != 0) {
1468 buffer_free(&m);
1469 return -1;
1470 }
1471
1472 type = buffer_get_int(&m);
1473 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1474 fatal("%s: out of sequence reply: my id %u theirs %u",
1475 __func__, muxclient_request_id, rid);
1476 switch (type) {
1477 case MUX_S_OK:
1478 break;
Damien Miller388f6fc2010-05-21 14:57:35 +10001479 case MUX_S_REMOTE_PORT:
1480 fwd->allocated_port = buffer_get_int(&m);
1481 logit("Allocated port %u for remote forward to %s:%d",
1482 fwd->allocated_port,
1483 fwd->connect_host ? fwd->connect_host : "",
1484 fwd->connect_port);
1485 if (muxclient_command == SSHMUX_COMMAND_FORWARD)
1486 fprintf(stdout, "%u\n", fwd->allocated_port);
1487 break;
Damien Millere1537f92010-01-26 13:26:22 +11001488 case MUX_S_PERMISSION_DENIED:
1489 e = buffer_get_string(&m, NULL);
1490 buffer_free(&m);
1491 error("Master refused forwarding request: %s", e);
1492 return -1;
1493 case MUX_S_FAILURE:
1494 e = buffer_get_string(&m, NULL);
1495 buffer_free(&m);
1496 error("%s: session request failed: %s", __func__, e);
1497 return -1;
1498 default:
1499 fatal("%s: unexpected response from master 0x%08x",
1500 __func__, type);
1501 }
1502 buffer_free(&m);
1503
1504 muxclient_request_id++;
1505 return 0;
1506}
1507
1508static int
1509mux_client_request_forwards(int fd)
1510{
1511 int i;
1512
1513 debug3("%s: requesting forwardings: %d local, %d remote", __func__,
1514 options.num_local_forwards, options.num_remote_forwards);
1515
1516 /* XXX ExitOnForwardingFailure */
1517 for (i = 0; i < options.num_local_forwards; i++) {
1518 if (mux_client_request_forward(fd,
1519 options.local_forwards[i].connect_port == 0 ?
1520 MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
1521 options.local_forwards + i) != 0)
1522 return -1;
1523 }
1524 for (i = 0; i < options.num_remote_forwards; i++) {
1525 if (mux_client_request_forward(fd, MUX_FWD_REMOTE,
1526 options.remote_forwards + i) != 0)
1527 return -1;
1528 }
1529 return 0;
1530}
1531
1532static int
1533mux_client_request_session(int fd)
1534{
1535 Buffer m;
1536 char *e, *term;
1537 u_int i, rid, sid, esid, exitval, type, exitval_seen;
1538 extern char **environ;
1539 int devnull;
1540
1541 debug3("%s: entering", __func__);
1542
1543 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1544 error("%s: master alive request failed", __func__);
1545 return -1;
1546 }
1547
1548 signal(SIGPIPE, SIG_IGN);
1549
1550 if (stdin_null_flag) {
1551 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1552 fatal("open(/dev/null): %s", strerror(errno));
1553 if (dup2(devnull, STDIN_FILENO) == -1)
1554 fatal("dup2: %s", strerror(errno));
1555 if (devnull > STDERR_FILENO)
1556 close(devnull);
1557 }
1558
1559 term = getenv("TERM");
1560
1561 buffer_init(&m);
1562 buffer_put_int(&m, MUX_C_NEW_SESSION);
1563 buffer_put_int(&m, muxclient_request_id);
1564 buffer_put_cstring(&m, ""); /* reserved */
1565 buffer_put_int(&m, tty_flag);
1566 buffer_put_int(&m, options.forward_x11);
1567 buffer_put_int(&m, options.forward_agent);
1568 buffer_put_int(&m, subsystem_flag);
1569 buffer_put_int(&m, options.escape_char == SSH_ESCAPECHAR_NONE ?
1570 0xffffffff : (u_int)options.escape_char);
1571 buffer_put_cstring(&m, term == NULL ? "" : term);
1572 buffer_put_string(&m, buffer_ptr(&command), buffer_len(&command));
1573
1574 if (options.num_send_env > 0 && environ != NULL) {
1575 /* Pass environment */
1576 for (i = 0; environ[i] != NULL; i++) {
1577 if (env_permitted(environ[i])) {
1578 buffer_put_cstring(&m, environ[i]);
1579 }
1580 }
1581 }
1582
1583 if (mux_client_write_packet(fd, &m) != 0)
1584 fatal("%s: write packet: %s", __func__, strerror(errno));
1585
1586 /* Send the stdio file descriptors */
1587 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1588 mm_send_fd(fd, STDOUT_FILENO) == -1 ||
1589 mm_send_fd(fd, STDERR_FILENO) == -1)
1590 fatal("%s: send fds failed", __func__);
1591
1592 debug3("%s: session request sent", __func__);
1593
1594 /* Read their reply */
1595 buffer_clear(&m);
1596 if (mux_client_read_packet(fd, &m) != 0) {
1597 error("%s: read from master failed: %s",
1598 __func__, strerror(errno));
1599 buffer_free(&m);
1600 return -1;
1601 }
1602
1603 type = buffer_get_int(&m);
1604 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1605 fatal("%s: out of sequence reply: my id %u theirs %u",
1606 __func__, muxclient_request_id, rid);
1607 switch (type) {
1608 case MUX_S_SESSION_OPENED:
1609 sid = buffer_get_int(&m);
1610 debug("%s: master session id: %u", __func__, sid);
1611 break;
1612 case MUX_S_PERMISSION_DENIED:
1613 e = buffer_get_string(&m, NULL);
1614 buffer_free(&m);
1615 error("Master refused forwarding request: %s", e);
1616 return -1;
1617 case MUX_S_FAILURE:
1618 e = buffer_get_string(&m, NULL);
1619 buffer_free(&m);
1620 error("%s: forwarding request failed: %s", __func__, e);
1621 return -1;
1622 default:
1623 buffer_free(&m);
1624 error("%s: unexpected response from master 0x%08x",
1625 __func__, type);
1626 return -1;
1627 }
1628 muxclient_request_id++;
1629
1630 signal(SIGHUP, control_client_sighandler);
1631 signal(SIGINT, control_client_sighandler);
1632 signal(SIGTERM, control_client_sighandler);
1633 signal(SIGWINCH, control_client_sigrelay);
1634
1635 if (tty_flag)
1636 enter_raw_mode(force_tty_flag);
1637
1638 /*
1639 * Stick around until the controlee closes the client_fd.
1640 * Before it does, it is expected to write an exit message.
1641 * This process must read the value and wait for the closure of
1642 * the client_fd; if this one closes early, the multiplex master will
1643 * terminate early too (possibly losing data).
1644 */
1645 for (exitval = 255, exitval_seen = 0;;) {
1646 buffer_clear(&m);
1647 if (mux_client_read_packet(fd, &m) != 0)
1648 break;
1649 type = buffer_get_int(&m);
1650 if (type != MUX_S_EXIT_MESSAGE) {
1651 e = buffer_get_string(&m, NULL);
1652 fatal("%s: master returned error: %s", __func__, e);
1653 }
1654 if ((esid = buffer_get_int(&m)) != sid)
1655 fatal("%s: exit on unknown session: my id %u theirs %u",
1656 __func__, sid, esid);
1657 debug("%s: master session id: %u", __func__, sid);
1658 if (exitval_seen)
1659 fatal("%s: exitval sent twice", __func__);
1660 exitval = buffer_get_int(&m);
1661 exitval_seen = 1;
1662 }
1663
1664 close(fd);
1665 leave_raw_mode(force_tty_flag);
1666
1667 if (muxclient_terminate) {
1668 debug2("Exiting on signal %d", muxclient_terminate);
1669 exitval = 255;
1670 } else if (!exitval_seen) {
1671 debug2("Control master terminated unexpectedly");
1672 exitval = 255;
1673 } else
1674 debug2("Received exit status from master %d", exitval);
1675
1676 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1677 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
1678
1679 exit(exitval);
1680}
1681
1682static int
1683mux_client_request_stdio_fwd(int fd)
1684{
1685 Buffer m;
1686 char *e;
1687 u_int type, rid, sid;
1688 int devnull;
1689
1690 debug3("%s: entering", __func__);
1691
1692 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1693 error("%s: master alive request failed", __func__);
1694 return -1;
1695 }
1696
1697 signal(SIGPIPE, SIG_IGN);
1698
1699 if (stdin_null_flag) {
1700 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1701 fatal("open(/dev/null): %s", strerror(errno));
1702 if (dup2(devnull, STDIN_FILENO) == -1)
1703 fatal("dup2: %s", strerror(errno));
1704 if (devnull > STDERR_FILENO)
1705 close(devnull);
1706 }
1707
1708 buffer_init(&m);
1709 buffer_put_int(&m, MUX_C_NEW_STDIO_FWD);
1710 buffer_put_int(&m, muxclient_request_id);
1711 buffer_put_cstring(&m, ""); /* reserved */
1712 buffer_put_cstring(&m, stdio_forward_host);
1713 buffer_put_int(&m, stdio_forward_port);
1714
1715 if (mux_client_write_packet(fd, &m) != 0)
1716 fatal("%s: write packet: %s", __func__, strerror(errno));
1717
1718 /* Send the stdio file descriptors */
1719 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1720 mm_send_fd(fd, STDOUT_FILENO) == -1)
1721 fatal("%s: send fds failed", __func__);
1722
1723 debug3("%s: stdio forward request sent", __func__);
1724
1725 /* Read their reply */
1726 buffer_clear(&m);
1727
1728 if (mux_client_read_packet(fd, &m) != 0) {
1729 error("%s: read from master failed: %s",
1730 __func__, strerror(errno));
1731 buffer_free(&m);
1732 return -1;
1733 }
1734
1735 type = buffer_get_int(&m);
1736 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1737 fatal("%s: out of sequence reply: my id %u theirs %u",
1738 __func__, muxclient_request_id, rid);
1739 switch (type) {
1740 case MUX_S_SESSION_OPENED:
1741 sid = buffer_get_int(&m);
1742 debug("%s: master session id: %u", __func__, sid);
1743 break;
1744 case MUX_S_PERMISSION_DENIED:
1745 e = buffer_get_string(&m, NULL);
1746 buffer_free(&m);
1747 fatal("Master refused forwarding request: %s", e);
1748 case MUX_S_FAILURE:
1749 e = buffer_get_string(&m, NULL);
1750 buffer_free(&m);
1751 fatal("%s: stdio forwarding request failed: %s", __func__, e);
1752 default:
1753 buffer_free(&m);
1754 error("%s: unexpected response from master 0x%08x",
1755 __func__, type);
1756 return -1;
1757 }
1758 muxclient_request_id++;
1759
1760 signal(SIGHUP, control_client_sighandler);
1761 signal(SIGINT, control_client_sighandler);
1762 signal(SIGTERM, control_client_sighandler);
1763 signal(SIGWINCH, control_client_sigrelay);
1764
1765 /*
1766 * Stick around until the controlee closes the client_fd.
1767 */
1768 buffer_clear(&m);
1769 if (mux_client_read_packet(fd, &m) != 0) {
1770 if (errno == EPIPE ||
1771 (errno == EINTR && muxclient_terminate != 0))
1772 return 0;
1773 fatal("%s: mux_client_read_packet: %s",
1774 __func__, strerror(errno));
1775 }
1776 fatal("%s: master returned unexpected message %u", __func__, type);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001777}
1778
1779/* Multiplex client main loop. */
1780void
1781muxclient(const char *path)
1782{
1783 struct sockaddr_un addr;
Damien Millere1537f92010-01-26 13:26:22 +11001784 socklen_t sun_len;
1785 int sock;
1786 u_int pid;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001787
Damien Millere1537f92010-01-26 13:26:22 +11001788 if (muxclient_command == 0) {
1789 if (stdio_forward_host != NULL)
1790 muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
1791 else
1792 muxclient_command = SSHMUX_COMMAND_OPEN;
1793 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001794
1795 switch (options.control_master) {
1796 case SSHCTL_MASTER_AUTO:
1797 case SSHCTL_MASTER_AUTO_ASK:
1798 debug("auto-mux: Trying existing master");
1799 /* FALLTHROUGH */
1800 case SSHCTL_MASTER_NO:
1801 break;
1802 default:
1803 return;
1804 }
1805
1806 memset(&addr, '\0', sizeof(addr));
1807 addr.sun_family = AF_UNIX;
Damien Millere1537f92010-01-26 13:26:22 +11001808 sun_len = offsetof(struct sockaddr_un, sun_path) +
Damien Millerb1cbfa22008-05-19 16:00:08 +10001809 strlen(path) + 1;
1810
1811 if (strlcpy(addr.sun_path, path,
1812 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1813 fatal("ControlPath too long");
1814
1815 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1816 fatal("%s socket(): %s", __func__, strerror(errno));
1817
Damien Millere1537f92010-01-26 13:26:22 +11001818 if (connect(sock, (struct sockaddr *)&addr, sun_len) == -1) {
1819 switch (muxclient_command) {
1820 case SSHMUX_COMMAND_OPEN:
1821 case SSHMUX_COMMAND_STDIO_FWD:
1822 break;
1823 default:
Damien Millerb1cbfa22008-05-19 16:00:08 +10001824 fatal("Control socket connect(%.100s): %s", path,
1825 strerror(errno));
1826 }
1827 if (errno == ENOENT)
1828 debug("Control socket \"%.100s\" does not exist", path);
1829 else {
1830 error("Control socket connect(%.100s): %s", path,
1831 strerror(errno));
1832 }
1833 close(sock);
1834 return;
1835 }
Damien Millere1537f92010-01-26 13:26:22 +11001836 set_nonblock(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001837
Damien Millere1537f92010-01-26 13:26:22 +11001838 if (mux_client_hello_exchange(sock) != 0) {
1839 error("%s: master hello exchange failed", __func__);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001840 close(sock);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001841 return;
1842 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001843
1844 switch (muxclient_command) {
1845 case SSHMUX_COMMAND_ALIVE_CHECK:
Damien Millere1537f92010-01-26 13:26:22 +11001846 if ((pid = mux_client_request_alive(sock)) == 0)
1847 fatal("%s: master alive check failed", __func__);
1848 fprintf(stderr, "Master running (pid=%d)\r\n", pid);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001849 exit(0);
1850 case SSHMUX_COMMAND_TERMINATE:
Damien Millere1537f92010-01-26 13:26:22 +11001851 mux_client_request_terminate(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001852 fprintf(stderr, "Exit request sent.\r\n");
1853 exit(0);
Damien Miller388f6fc2010-05-21 14:57:35 +10001854 case SSHMUX_COMMAND_FORWARD:
1855 if (mux_client_request_forwards(sock) != 0)
1856 fatal("%s: master forward request failed", __func__);
1857 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001858 case SSHMUX_COMMAND_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +11001859 if (mux_client_request_forwards(sock) != 0) {
1860 error("%s: master forward request failed", __func__);
1861 return;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001862 }
Damien Millere1537f92010-01-26 13:26:22 +11001863 mux_client_request_session(sock);
1864 return;
1865 case SSHMUX_COMMAND_STDIO_FWD:
1866 mux_client_request_stdio_fwd(sock);
1867 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001868 default:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001869 fatal("unrecognised muxclient_command %d", muxclient_command);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001870 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001871}