blob: fdf0385e09cabe0f3fe71c687e042057c688de70 [file] [log] [blame]
Damien Miller232cfb12010-06-26 09:50:30 +10001/* $OpenBSD: mux.c,v 1.20 2010/06/25 07:14:46 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) {
Damien Miller232cfb12010-06-26 09:50:30 +1000730 if (channel_setup_local_fwd_listener(fwd.listen_host,
Damien Millere1537f92010-01-26 13:26:22 +1100731 fwd.listen_port, fwd.connect_host, fwd.connect_port,
732 options.gateway_ports) < 0) {
733 fail:
734 logit("slave-requested %s failed", fwd_desc);
735 buffer_put_int(r, MUX_S_FAILURE);
736 buffer_put_int(r, rid);
737 buffer_put_cstring(r, "Port forwarding failed");
738 goto out;
739 }
740 add_local_forward(&options, &fwd);
741 freefwd = 0;
742 } else {
Damien Miller388f6fc2010-05-21 14:57:35 +1000743 struct mux_channel_confirm_ctx *fctx;
744
Damien Miller232cfb12010-06-26 09:50:30 +1000745 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millere1537f92010-01-26 13:26:22 +1100746 fwd.listen_port, fwd.connect_host, fwd.connect_port) < 0)
747 goto fail;
748 add_remote_forward(&options, &fwd);
Damien Miller388f6fc2010-05-21 14:57:35 +1000749 fctx = xcalloc(1, sizeof(*fctx));
750 fctx->cid = c->self;
751 fctx->rid = rid;
Damien Miller232cfb12010-06-26 09:50:30 +1000752 fctx->fid = options.num_remote_forwards - 1;
Damien Miller388f6fc2010-05-21 14:57:35 +1000753 client_register_global_confirm(mux_confirm_remote_forward,
754 fctx);
Damien Millere1537f92010-01-26 13:26:22 +1100755 freefwd = 0;
Damien Miller388f6fc2010-05-21 14:57:35 +1000756 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
757 /* delayed reply in mux_confirm_remote_forward */
758 goto out;
Damien Millere1537f92010-01-26 13:26:22 +1100759 }
760 buffer_put_int(r, MUX_S_OK);
761 buffer_put_int(r, rid);
762 out:
763 if (fwd_desc != NULL)
764 xfree(fwd_desc);
765 if (freefwd) {
766 if (fwd.listen_host != NULL)
767 xfree(fwd.listen_host);
768 if (fwd.connect_host != NULL)
769 xfree(fwd.connect_host);
770 }
771 return ret;
772}
773
774static int
775process_mux_close_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
776{
777 Forward fwd;
778 char *fwd_desc = NULL;
779 u_int ftype;
780 int ret = 0;
781
782 fwd.listen_host = fwd.connect_host = NULL;
783 if (buffer_get_int_ret(&ftype, m) != 0 ||
784 (fwd.listen_host = buffer_get_string_ret(m, NULL)) == NULL ||
785 buffer_get_int_ret(&fwd.listen_port, m) != 0 ||
786 (fwd.connect_host = buffer_get_string_ret(m, NULL)) == NULL ||
787 buffer_get_int_ret(&fwd.connect_port, m) != 0) {
788 error("%s: malformed message", __func__);
789 ret = -1;
790 goto out;
791 }
792
793 if (*fwd.listen_host == '\0') {
794 xfree(fwd.listen_host);
795 fwd.listen_host = NULL;
796 }
797 if (*fwd.connect_host == '\0') {
798 xfree(fwd.connect_host);
799 fwd.connect_host = NULL;
800 }
801
802 debug2("%s: channel %d: request %s", __func__, c->self,
803 (fwd_desc = format_forward(ftype, &fwd)));
804
805 /* XXX implement this */
806 buffer_put_int(r, MUX_S_FAILURE);
807 buffer_put_int(r, rid);
808 buffer_put_cstring(r, "unimplemented");
809
810 out:
811 if (fwd_desc != NULL)
812 xfree(fwd_desc);
813 if (fwd.listen_host != NULL)
814 xfree(fwd.listen_host);
815 if (fwd.connect_host != NULL)
816 xfree(fwd.connect_host);
817
818 return ret;
819}
820
821static int
822process_mux_stdio_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
823{
824 Channel *nc;
825 char *reserved, *chost;
826 u_int cport, i, j;
827 int new_fd[2];
828
829 chost = reserved = NULL;
830 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
831 (chost = buffer_get_string_ret(m, NULL)) == NULL ||
832 buffer_get_int_ret(&cport, m) != 0) {
833 if (reserved != NULL)
834 xfree(reserved);
835 if (chost != NULL)
836 xfree(chost);
837 error("%s: malformed message", __func__);
838 return -1;
839 }
840 xfree(reserved);
841
842 debug2("%s: channel %d: request stdio fwd to %s:%u",
843 __func__, c->self, chost, cport);
844
845 /* Gather fds from client */
846 for(i = 0; i < 2; i++) {
847 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
848 error("%s: failed to receive fd %d from slave",
849 __func__, i);
850 for (j = 0; j < i; j++)
851 close(new_fd[j]);
852 xfree(chost);
853
854 /* prepare reply */
855 buffer_put_int(r, MUX_S_FAILURE);
856 buffer_put_int(r, rid);
857 buffer_put_cstring(r,
858 "did not receive file descriptors");
859 return -1;
860 }
861 }
862
863 debug3("%s: got fds stdin %d, stdout %d", __func__,
864 new_fd[0], new_fd[1]);
865
866 /* XXX support multiple child sessions in future */
867 if (c->remote_id != -1) {
868 debug2("%s: session already open", __func__);
869 /* prepare reply */
870 buffer_put_int(r, MUX_S_FAILURE);
871 buffer_put_int(r, rid);
872 buffer_put_cstring(r, "Multiple sessions not supported");
873 cleanup:
874 close(new_fd[0]);
875 close(new_fd[1]);
876 xfree(chost);
877 return 0;
878 }
879
880 if (options.control_master == SSHCTL_MASTER_ASK ||
881 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
882 if (!ask_permission("Allow forward to to %s:%u? ",
883 chost, cport)) {
884 debug2("%s: stdio fwd refused by user", __func__);
885 /* prepare reply */
886 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
887 buffer_put_int(r, rid);
888 buffer_put_cstring(r, "Permission denied");
889 goto cleanup;
890 }
891 }
892
893 /* enable nonblocking unless tty */
894 if (!isatty(new_fd[0]))
895 set_nonblock(new_fd[0]);
896 if (!isatty(new_fd[1]))
897 set_nonblock(new_fd[1]);
898
899 nc = channel_connect_stdio_fwd(chost, cport, new_fd[0], new_fd[1]);
900
901 nc->ctl_chan = c->self; /* link session -> control channel */
902 c->remote_id = nc->self; /* link control -> session channel */
903
904 debug2("%s: channel_new: %d linked to control channel %d",
905 __func__, nc->self, nc->ctl_chan);
906
Damien Miller85c50d72010-05-10 11:53:02 +1000907 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
Damien Millere1537f92010-01-26 13:26:22 +1100908
909 /* prepare reply */
910 /* XXX defer until channel confirmed */
911 buffer_put_int(r, MUX_S_SESSION_OPENED);
912 buffer_put_int(r, rid);
913 buffer_put_int(r, nc->self);
914
915 return 0;
916}
917
918/* Channel callbacks fired on read/write from mux slave fd */
919static int
920mux_master_read_cb(Channel *c)
921{
922 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
923 Buffer in, out;
924 void *ptr;
925 u_int type, rid, have, i;
926 int ret = -1;
927
928 /* Setup ctx and */
929 if (c->mux_ctx == NULL) {
Damien Millerc094d1e2010-06-26 09:36:34 +1000930 state = xcalloc(1, sizeof(*state));
Damien Millere1537f92010-01-26 13:26:22 +1100931 c->mux_ctx = state;
932 channel_register_cleanup(c->self,
933 mux_master_control_cleanup_cb, 0);
934
935 /* Send hello */
936 buffer_init(&out);
937 buffer_put_int(&out, MUX_MSG_HELLO);
938 buffer_put_int(&out, SSHMUX_VER);
939 /* no extensions */
940 buffer_put_string(&c->output, buffer_ptr(&out),
941 buffer_len(&out));
942 buffer_free(&out);
943 debug3("%s: channel %d: hello sent", __func__, c->self);
944 return 0;
945 }
946
947 buffer_init(&in);
948 buffer_init(&out);
949
950 /* Channel code ensures that we receive whole packets */
951 if ((ptr = buffer_get_string_ptr_ret(&c->input, &have)) == NULL) {
952 malf:
953 error("%s: malformed message", __func__);
954 goto out;
955 }
956 buffer_append(&in, ptr, have);
957
958 if (buffer_get_int_ret(&type, &in) != 0)
959 goto malf;
960 debug3("%s: channel %d packet type 0x%08x len %u",
961 __func__, c->self, type, buffer_len(&in));
962
963 if (type == MUX_MSG_HELLO)
964 rid = 0;
965 else {
966 if (!state->hello_rcvd) {
967 error("%s: expected MUX_MSG_HELLO(0x%08x), "
968 "received 0x%08x", __func__, MUX_MSG_HELLO, type);
969 goto out;
970 }
971 if (buffer_get_int_ret(&rid, &in) != 0)
972 goto malf;
973 }
974
975 for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
976 if (type == mux_master_handlers[i].type) {
977 ret = mux_master_handlers[i].handler(rid, c, &in, &out);
978 break;
979 }
980 }
981 if (mux_master_handlers[i].handler == NULL) {
982 error("%s: unsupported mux message 0x%08x", __func__, type);
983 buffer_put_int(&out, MUX_S_FAILURE);
984 buffer_put_int(&out, rid);
985 buffer_put_cstring(&out, "unsupported request");
986 ret = 0;
987 }
988 /* Enqueue reply packet */
989 if (buffer_len(&out) != 0) {
990 buffer_put_string(&c->output, buffer_ptr(&out),
991 buffer_len(&out));
992 }
993 out:
994 buffer_free(&in);
995 buffer_free(&out);
996 return ret;
997}
998
999void
1000mux_exit_message(Channel *c, int exitval)
1001{
1002 Buffer m;
1003 Channel *mux_chan;
1004
1005 debug3("%s: channel %d: exit message, evitval %d", __func__, c->self,
1006 exitval);
1007
1008 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL)
1009 fatal("%s: channel %d missing mux channel %d",
1010 __func__, c->self, c->ctl_chan);
1011
1012 /* Append exit message packet to control socket output queue */
1013 buffer_init(&m);
1014 buffer_put_int(&m, MUX_S_EXIT_MESSAGE);
1015 buffer_put_int(&m, c->self);
1016 buffer_put_int(&m, exitval);
1017
1018 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m));
1019 buffer_free(&m);
1020}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001021
1022/* Prepare a mux master to listen on a Unix domain socket. */
1023void
1024muxserver_listen(void)
1025{
1026 struct sockaddr_un addr;
Damien Millere1537f92010-01-26 13:26:22 +11001027 socklen_t sun_len;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001028 mode_t old_umask;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001029
1030 if (options.control_path == NULL ||
1031 options.control_master == SSHCTL_MASTER_NO)
1032 return;
1033
1034 debug("setting up multiplex master socket");
1035
1036 memset(&addr, '\0', sizeof(addr));
1037 addr.sun_family = AF_UNIX;
Damien Millere1537f92010-01-26 13:26:22 +11001038 sun_len = offsetof(struct sockaddr_un, sun_path) +
Damien Millerb1cbfa22008-05-19 16:00:08 +10001039 strlen(options.control_path) + 1;
1040
1041 if (strlcpy(addr.sun_path, options.control_path,
1042 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1043 fatal("ControlPath too long");
1044
1045 if ((muxserver_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1046 fatal("%s socket(): %s", __func__, strerror(errno));
1047
1048 old_umask = umask(0177);
Damien Millere1537f92010-01-26 13:26:22 +11001049 if (bind(muxserver_sock, (struct sockaddr *)&addr, sun_len) == -1) {
Damien Millerb1cbfa22008-05-19 16:00:08 +10001050 muxserver_sock = -1;
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001051 if (errno == EINVAL || errno == EADDRINUSE) {
1052 error("ControlSocket %s already exists, "
1053 "disabling multiplexing", options.control_path);
1054 close(muxserver_sock);
1055 muxserver_sock = -1;
1056 xfree(options.control_path);
1057 options.control_path = NULL;
1058 options.control_master = SSHCTL_MASTER_NO;
1059 return;
1060 } else
Damien Millerb1cbfa22008-05-19 16:00:08 +10001061 fatal("%s bind(): %s", __func__, strerror(errno));
1062 }
1063 umask(old_umask);
1064
1065 if (listen(muxserver_sock, 64) == -1)
1066 fatal("%s listen(): %s", __func__, strerror(errno));
1067
1068 set_nonblock(muxserver_sock);
Damien Millere1537f92010-01-26 13:26:22 +11001069
1070 mux_listener_channel = channel_new("mux listener",
1071 SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
1072 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1073 0, addr.sun_path, 1);
1074 mux_listener_channel->mux_rcb = mux_master_read_cb;
1075 debug3("%s: mux listener channel %d fd %d", __func__,
1076 mux_listener_channel->self, mux_listener_channel->sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001077}
1078
1079/* Callback on open confirmation in mux master for a mux client session. */
1080static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001081mux_session_confirm(int id, int success, void *arg)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001082{
1083 struct mux_session_confirm_ctx *cctx = arg;
1084 const char *display;
Damien Millerd530f5f2010-05-21 14:57:10 +10001085 Channel *c, *cc;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001086 int i;
Damien Millerd530f5f2010-05-21 14:57:10 +10001087 Buffer reply;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001088
1089 if (cctx == NULL)
1090 fatal("%s: cctx == NULL", __func__);
Damien Millere1537f92010-01-26 13:26:22 +11001091 if ((c = channel_by_id(id)) == NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001092 fatal("%s: no channel for id %d", __func__, id);
Damien Millerd530f5f2010-05-21 14:57:10 +10001093 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
1094 fatal("%s: channel %d lacks control channel %d", __func__,
1095 id, c->ctl_chan);
1096
1097 if (!success) {
1098 debug3("%s: sending failure reply", __func__);
1099 /* prepare reply */
1100 buffer_init(&reply);
1101 buffer_put_int(&reply, MUX_S_FAILURE);
1102 buffer_put_int(&reply, cctx->rid);
1103 buffer_put_cstring(&reply, "Session open refused by peer");
1104 goto done;
1105 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001106
1107 display = getenv("DISPLAY");
1108 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1109 char *proto, *data;
1110 /* Get reasonable local authentication information. */
1111 client_x11_get_proto(display, options.xauth_location,
1112 options.forward_x11_trusted, &proto, &data);
1113 /* Request forwarding with authentication spoofing. */
1114 debug("Requesting X11 forwarding with authentication spoofing.");
1115 x11_request_forwarding_with_spoofing(id, display, proto, data);
1116 /* XXX wait for reply */
1117 }
1118
1119 if (cctx->want_agent_fwd && options.forward_agent) {
1120 debug("Requesting authentication agent forwarding.");
1121 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1122 packet_send();
1123 }
1124
1125 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
1126 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
1127
Damien Millerd530f5f2010-05-21 14:57:10 +10001128 debug3("%s: sending success reply", __func__);
1129 /* prepare reply */
1130 buffer_init(&reply);
1131 buffer_put_int(&reply, MUX_S_SESSION_OPENED);
1132 buffer_put_int(&reply, cctx->rid);
1133 buffer_put_int(&reply, c->self);
1134
1135 done:
1136 /* Send reply */
1137 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply));
1138 buffer_free(&reply);
1139
1140 if (cc->mux_pause <= 0)
1141 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1142 cc->mux_pause = 0; /* start processing messages again */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001143 c->open_confirm_ctx = NULL;
1144 buffer_free(&cctx->cmd);
1145 xfree(cctx->term);
1146 if (cctx->env != NULL) {
1147 for (i = 0; cctx->env[i] != NULL; i++)
1148 xfree(cctx->env[i]);
1149 xfree(cctx->env);
1150 }
1151 xfree(cctx);
1152}
1153
Damien Millerb1cbfa22008-05-19 16:00:08 +10001154/* ** Multiplexing client support */
1155
1156/* Exit signal handler */
1157static void
1158control_client_sighandler(int signo)
1159{
1160 muxclient_terminate = signo;
1161}
1162
1163/*
1164 * Relay signal handler - used to pass some signals from mux client to
1165 * mux master.
1166 */
1167static void
1168control_client_sigrelay(int signo)
1169{
1170 int save_errno = errno;
1171
1172 if (muxserver_pid > 1)
1173 kill(muxserver_pid, signo);
1174
1175 errno = save_errno;
1176}
1177
Damien Millerb1cbfa22008-05-19 16:00:08 +10001178static int
Damien Millere1537f92010-01-26 13:26:22 +11001179mux_client_read(int fd, Buffer *b, u_int need)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001180{
Damien Millere1537f92010-01-26 13:26:22 +11001181 u_int have;
1182 ssize_t len;
1183 u_char *p;
1184 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001185
Damien Millere1537f92010-01-26 13:26:22 +11001186 pfd.fd = fd;
1187 pfd.events = POLLIN;
1188 p = buffer_append_space(b, need);
1189 for (have = 0; have < need; ) {
1190 if (muxclient_terminate) {
1191 errno = EINTR;
1192 return -1;
1193 }
1194 len = read(fd, p + have, need - have);
1195 if (len < 0) {
1196 switch (errno) {
1197#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1198 case EWOULDBLOCK:
1199#endif
1200 case EAGAIN:
1201 (void)poll(&pfd, 1, -1);
1202 /* FALLTHROUGH */
1203 case EINTR:
1204 continue;
1205 default:
1206 return -1;
1207 }
1208 }
1209 if (len == 0) {
1210 errno = EPIPE;
1211 return -1;
1212 }
1213 have += (u_int)len;
1214 }
1215 return 0;
1216}
Damien Millerb1cbfa22008-05-19 16:00:08 +10001217
Damien Millere1537f92010-01-26 13:26:22 +11001218static int
1219mux_client_write_packet(int fd, Buffer *m)
1220{
1221 Buffer queue;
1222 u_int have, need;
1223 int oerrno, len;
1224 u_char *ptr;
1225 struct pollfd pfd;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001226
Damien Millere1537f92010-01-26 13:26:22 +11001227 pfd.fd = fd;
1228 pfd.events = POLLOUT;
1229 buffer_init(&queue);
1230 buffer_put_string(&queue, buffer_ptr(m), buffer_len(m));
1231
1232 need = buffer_len(&queue);
1233 ptr = buffer_ptr(&queue);
1234
1235 for (have = 0; have < need; ) {
1236 if (muxclient_terminate) {
1237 buffer_free(&queue);
1238 errno = EINTR;
1239 return -1;
1240 }
1241 len = write(fd, ptr + have, need - have);
1242 if (len < 0) {
1243 switch (errno) {
1244#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1245 case EWOULDBLOCK:
1246#endif
1247 case EAGAIN:
1248 (void)poll(&pfd, 1, -1);
1249 /* FALLTHROUGH */
1250 case EINTR:
1251 continue;
1252 default:
1253 oerrno = errno;
1254 buffer_free(&queue);
1255 errno = oerrno;
1256 return -1;
1257 }
1258 }
1259 if (len == 0) {
1260 buffer_free(&queue);
1261 errno = EPIPE;
1262 return -1;
1263 }
1264 have += (u_int)len;
1265 }
1266 buffer_free(&queue);
1267 return 0;
1268}
1269
1270static int
1271mux_client_read_packet(int fd, Buffer *m)
1272{
1273 Buffer queue;
1274 u_int need, have;
1275 void *ptr;
1276 int oerrno;
1277
1278 buffer_init(&queue);
1279 if (mux_client_read(fd, &queue, 4) != 0) {
1280 if ((oerrno = errno) == EPIPE)
1281 debug3("%s: read header failed: %s", __func__, strerror(errno));
1282 errno = oerrno;
1283 return -1;
1284 }
1285 need = get_u32(buffer_ptr(&queue));
1286 if (mux_client_read(fd, &queue, need) != 0) {
1287 oerrno = errno;
1288 debug3("%s: read body failed: %s", __func__, strerror(errno));
1289 errno = oerrno;
1290 return -1;
1291 }
1292 ptr = buffer_get_string_ptr(&queue, &have);
1293 buffer_append(m, ptr, have);
1294 buffer_free(&queue);
1295 return 0;
1296}
1297
1298static int
1299mux_client_hello_exchange(int fd)
1300{
1301 Buffer m;
1302 u_int type, ver;
1303
1304 buffer_init(&m);
1305 buffer_put_int(&m, MUX_MSG_HELLO);
1306 buffer_put_int(&m, SSHMUX_VER);
1307 /* no extensions */
1308
1309 if (mux_client_write_packet(fd, &m) != 0)
1310 fatal("%s: write packet: %s", __func__, strerror(errno));
1311
1312 buffer_clear(&m);
1313
1314 /* Read their HELLO */
1315 if (mux_client_read_packet(fd, &m) != 0) {
1316 buffer_free(&m);
1317 return -1;
1318 }
1319
1320 type = buffer_get_int(&m);
1321 if (type != MUX_MSG_HELLO)
1322 fatal("%s: expected HELLO (%u) received %u",
1323 __func__, MUX_MSG_HELLO, type);
1324 ver = buffer_get_int(&m);
1325 if (ver != SSHMUX_VER)
1326 fatal("Unsupported multiplexing protocol version %d "
1327 "(expected %d)", ver, SSHMUX_VER);
1328 debug2("%s: master version %u", __func__, ver);
1329 /* No extensions are presently defined */
1330 while (buffer_len(&m) > 0) {
1331 char *name = buffer_get_string(&m, NULL);
1332 char *value = buffer_get_string(&m, NULL);
1333
1334 debug2("Unrecognised master extension \"%s\"", name);
1335 xfree(name);
1336 xfree(value);
1337 }
1338 buffer_free(&m);
1339 return 0;
1340}
1341
1342static u_int
1343mux_client_request_alive(int fd)
1344{
1345 Buffer m;
1346 char *e;
1347 u_int pid, type, rid;
1348
1349 debug3("%s: entering", __func__);
1350
1351 buffer_init(&m);
1352 buffer_put_int(&m, MUX_C_ALIVE_CHECK);
1353 buffer_put_int(&m, muxclient_request_id);
1354
1355 if (mux_client_write_packet(fd, &m) != 0)
1356 fatal("%s: write packet: %s", __func__, strerror(errno));
1357
1358 buffer_clear(&m);
1359
1360 /* Read their reply */
1361 if (mux_client_read_packet(fd, &m) != 0) {
1362 buffer_free(&m);
1363 return 0;
1364 }
1365
1366 type = buffer_get_int(&m);
1367 if (type != MUX_S_ALIVE) {
1368 e = buffer_get_string(&m, NULL);
1369 fatal("%s: master returned error: %s", __func__, e);
1370 }
1371
1372 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1373 fatal("%s: out of sequence reply: my id %u theirs %u",
1374 __func__, muxclient_request_id, rid);
1375 pid = buffer_get_int(&m);
1376 buffer_free(&m);
1377
1378 debug3("%s: done pid = %u", __func__, pid);
1379
1380 muxclient_request_id++;
1381
1382 return pid;
1383}
1384
1385static void
1386mux_client_request_terminate(int fd)
1387{
1388 Buffer m;
1389 char *e;
1390 u_int type, rid;
1391
1392 debug3("%s: entering", __func__);
1393
1394 buffer_init(&m);
1395 buffer_put_int(&m, MUX_C_TERMINATE);
1396 buffer_put_int(&m, muxclient_request_id);
1397
1398 if (mux_client_write_packet(fd, &m) != 0)
1399 fatal("%s: write packet: %s", __func__, strerror(errno));
1400
1401 buffer_clear(&m);
1402
1403 /* Read their reply */
1404 if (mux_client_read_packet(fd, &m) != 0) {
1405 /* Remote end exited already */
1406 if (errno == EPIPE) {
1407 buffer_free(&m);
1408 return;
1409 }
1410 fatal("%s: read from master failed: %s",
1411 __func__, strerror(errno));
1412 }
1413
1414 type = buffer_get_int(&m);
1415 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1416 fatal("%s: out of sequence reply: my id %u theirs %u",
1417 __func__, muxclient_request_id, rid);
1418 switch (type) {
1419 case MUX_S_OK:
1420 break;
1421 case MUX_S_PERMISSION_DENIED:
1422 e = buffer_get_string(&m, NULL);
1423 fatal("Master refused termination request: %s", e);
1424 case MUX_S_FAILURE:
1425 e = buffer_get_string(&m, NULL);
1426 fatal("%s: termination request failed: %s", __func__, e);
1427 default:
1428 fatal("%s: unexpected response from master 0x%08x",
1429 __func__, type);
1430 }
1431 buffer_free(&m);
1432 muxclient_request_id++;
1433}
1434
1435static int
1436mux_client_request_forward(int fd, u_int ftype, Forward *fwd)
1437{
1438 Buffer m;
1439 char *e, *fwd_desc;
1440 u_int type, rid;
1441
1442 fwd_desc = format_forward(ftype, fwd);
1443 debug("Requesting %s", fwd_desc);
1444 xfree(fwd_desc);
1445
1446 buffer_init(&m);
1447 buffer_put_int(&m, MUX_C_OPEN_FWD);
1448 buffer_put_int(&m, muxclient_request_id);
1449 buffer_put_int(&m, ftype);
1450 buffer_put_cstring(&m,
1451 fwd->listen_host == NULL ? "" : fwd->listen_host);
1452 buffer_put_int(&m, fwd->listen_port);
1453 buffer_put_cstring(&m,
1454 fwd->connect_host == NULL ? "" : fwd->connect_host);
1455 buffer_put_int(&m, fwd->connect_port);
1456
1457 if (mux_client_write_packet(fd, &m) != 0)
1458 fatal("%s: write packet: %s", __func__, strerror(errno));
1459
1460 buffer_clear(&m);
1461
1462 /* Read their reply */
1463 if (mux_client_read_packet(fd, &m) != 0) {
1464 buffer_free(&m);
1465 return -1;
1466 }
1467
1468 type = buffer_get_int(&m);
1469 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1470 fatal("%s: out of sequence reply: my id %u theirs %u",
1471 __func__, muxclient_request_id, rid);
1472 switch (type) {
1473 case MUX_S_OK:
1474 break;
Damien Miller388f6fc2010-05-21 14:57:35 +10001475 case MUX_S_REMOTE_PORT:
1476 fwd->allocated_port = buffer_get_int(&m);
1477 logit("Allocated port %u for remote forward to %s:%d",
1478 fwd->allocated_port,
1479 fwd->connect_host ? fwd->connect_host : "",
1480 fwd->connect_port);
1481 if (muxclient_command == SSHMUX_COMMAND_FORWARD)
1482 fprintf(stdout, "%u\n", fwd->allocated_port);
1483 break;
Damien Millere1537f92010-01-26 13:26:22 +11001484 case MUX_S_PERMISSION_DENIED:
1485 e = buffer_get_string(&m, NULL);
1486 buffer_free(&m);
1487 error("Master refused forwarding request: %s", e);
1488 return -1;
1489 case MUX_S_FAILURE:
1490 e = buffer_get_string(&m, NULL);
1491 buffer_free(&m);
1492 error("%s: session request failed: %s", __func__, e);
1493 return -1;
1494 default:
1495 fatal("%s: unexpected response from master 0x%08x",
1496 __func__, type);
1497 }
1498 buffer_free(&m);
1499
1500 muxclient_request_id++;
1501 return 0;
1502}
1503
1504static int
1505mux_client_request_forwards(int fd)
1506{
1507 int i;
1508
1509 debug3("%s: requesting forwardings: %d local, %d remote", __func__,
1510 options.num_local_forwards, options.num_remote_forwards);
1511
1512 /* XXX ExitOnForwardingFailure */
1513 for (i = 0; i < options.num_local_forwards; i++) {
1514 if (mux_client_request_forward(fd,
1515 options.local_forwards[i].connect_port == 0 ?
1516 MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
1517 options.local_forwards + i) != 0)
1518 return -1;
1519 }
1520 for (i = 0; i < options.num_remote_forwards; i++) {
1521 if (mux_client_request_forward(fd, MUX_FWD_REMOTE,
1522 options.remote_forwards + i) != 0)
1523 return -1;
1524 }
1525 return 0;
1526}
1527
1528static int
1529mux_client_request_session(int fd)
1530{
1531 Buffer m;
1532 char *e, *term;
1533 u_int i, rid, sid, esid, exitval, type, exitval_seen;
1534 extern char **environ;
1535 int devnull;
1536
1537 debug3("%s: entering", __func__);
1538
1539 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1540 error("%s: master alive request failed", __func__);
1541 return -1;
1542 }
1543
1544 signal(SIGPIPE, SIG_IGN);
1545
1546 if (stdin_null_flag) {
1547 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1548 fatal("open(/dev/null): %s", strerror(errno));
1549 if (dup2(devnull, STDIN_FILENO) == -1)
1550 fatal("dup2: %s", strerror(errno));
1551 if (devnull > STDERR_FILENO)
1552 close(devnull);
1553 }
1554
1555 term = getenv("TERM");
1556
1557 buffer_init(&m);
1558 buffer_put_int(&m, MUX_C_NEW_SESSION);
1559 buffer_put_int(&m, muxclient_request_id);
1560 buffer_put_cstring(&m, ""); /* reserved */
1561 buffer_put_int(&m, tty_flag);
1562 buffer_put_int(&m, options.forward_x11);
1563 buffer_put_int(&m, options.forward_agent);
1564 buffer_put_int(&m, subsystem_flag);
1565 buffer_put_int(&m, options.escape_char == SSH_ESCAPECHAR_NONE ?
1566 0xffffffff : (u_int)options.escape_char);
1567 buffer_put_cstring(&m, term == NULL ? "" : term);
1568 buffer_put_string(&m, buffer_ptr(&command), buffer_len(&command));
1569
1570 if (options.num_send_env > 0 && environ != NULL) {
1571 /* Pass environment */
1572 for (i = 0; environ[i] != NULL; i++) {
1573 if (env_permitted(environ[i])) {
1574 buffer_put_cstring(&m, environ[i]);
1575 }
1576 }
1577 }
1578
1579 if (mux_client_write_packet(fd, &m) != 0)
1580 fatal("%s: write packet: %s", __func__, strerror(errno));
1581
1582 /* Send the stdio file descriptors */
1583 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1584 mm_send_fd(fd, STDOUT_FILENO) == -1 ||
1585 mm_send_fd(fd, STDERR_FILENO) == -1)
1586 fatal("%s: send fds failed", __func__);
1587
1588 debug3("%s: session request sent", __func__);
1589
1590 /* Read their reply */
1591 buffer_clear(&m);
1592 if (mux_client_read_packet(fd, &m) != 0) {
1593 error("%s: read from master failed: %s",
1594 __func__, strerror(errno));
1595 buffer_free(&m);
1596 return -1;
1597 }
1598
1599 type = buffer_get_int(&m);
1600 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1601 fatal("%s: out of sequence reply: my id %u theirs %u",
1602 __func__, muxclient_request_id, rid);
1603 switch (type) {
1604 case MUX_S_SESSION_OPENED:
1605 sid = buffer_get_int(&m);
1606 debug("%s: master session id: %u", __func__, sid);
1607 break;
1608 case MUX_S_PERMISSION_DENIED:
1609 e = buffer_get_string(&m, NULL);
1610 buffer_free(&m);
1611 error("Master refused forwarding request: %s", e);
1612 return -1;
1613 case MUX_S_FAILURE:
1614 e = buffer_get_string(&m, NULL);
1615 buffer_free(&m);
1616 error("%s: forwarding request failed: %s", __func__, e);
1617 return -1;
1618 default:
1619 buffer_free(&m);
1620 error("%s: unexpected response from master 0x%08x",
1621 __func__, type);
1622 return -1;
1623 }
1624 muxclient_request_id++;
1625
1626 signal(SIGHUP, control_client_sighandler);
1627 signal(SIGINT, control_client_sighandler);
1628 signal(SIGTERM, control_client_sighandler);
1629 signal(SIGWINCH, control_client_sigrelay);
1630
1631 if (tty_flag)
1632 enter_raw_mode(force_tty_flag);
1633
1634 /*
1635 * Stick around until the controlee closes the client_fd.
1636 * Before it does, it is expected to write an exit message.
1637 * This process must read the value and wait for the closure of
1638 * the client_fd; if this one closes early, the multiplex master will
1639 * terminate early too (possibly losing data).
1640 */
1641 for (exitval = 255, exitval_seen = 0;;) {
1642 buffer_clear(&m);
1643 if (mux_client_read_packet(fd, &m) != 0)
1644 break;
1645 type = buffer_get_int(&m);
1646 if (type != MUX_S_EXIT_MESSAGE) {
1647 e = buffer_get_string(&m, NULL);
1648 fatal("%s: master returned error: %s", __func__, e);
1649 }
1650 if ((esid = buffer_get_int(&m)) != sid)
1651 fatal("%s: exit on unknown session: my id %u theirs %u",
1652 __func__, sid, esid);
1653 debug("%s: master session id: %u", __func__, sid);
1654 if (exitval_seen)
1655 fatal("%s: exitval sent twice", __func__);
1656 exitval = buffer_get_int(&m);
1657 exitval_seen = 1;
1658 }
1659
1660 close(fd);
1661 leave_raw_mode(force_tty_flag);
1662
1663 if (muxclient_terminate) {
1664 debug2("Exiting on signal %d", muxclient_terminate);
1665 exitval = 255;
1666 } else if (!exitval_seen) {
1667 debug2("Control master terminated unexpectedly");
1668 exitval = 255;
1669 } else
1670 debug2("Received exit status from master %d", exitval);
1671
1672 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1673 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
1674
1675 exit(exitval);
1676}
1677
1678static int
1679mux_client_request_stdio_fwd(int fd)
1680{
1681 Buffer m;
1682 char *e;
1683 u_int type, rid, sid;
1684 int devnull;
1685
1686 debug3("%s: entering", __func__);
1687
1688 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
1689 error("%s: master alive request failed", __func__);
1690 return -1;
1691 }
1692
1693 signal(SIGPIPE, SIG_IGN);
1694
1695 if (stdin_null_flag) {
1696 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1697 fatal("open(/dev/null): %s", strerror(errno));
1698 if (dup2(devnull, STDIN_FILENO) == -1)
1699 fatal("dup2: %s", strerror(errno));
1700 if (devnull > STDERR_FILENO)
1701 close(devnull);
1702 }
1703
1704 buffer_init(&m);
1705 buffer_put_int(&m, MUX_C_NEW_STDIO_FWD);
1706 buffer_put_int(&m, muxclient_request_id);
1707 buffer_put_cstring(&m, ""); /* reserved */
1708 buffer_put_cstring(&m, stdio_forward_host);
1709 buffer_put_int(&m, stdio_forward_port);
1710
1711 if (mux_client_write_packet(fd, &m) != 0)
1712 fatal("%s: write packet: %s", __func__, strerror(errno));
1713
1714 /* Send the stdio file descriptors */
1715 if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
1716 mm_send_fd(fd, STDOUT_FILENO) == -1)
1717 fatal("%s: send fds failed", __func__);
1718
1719 debug3("%s: stdio forward request sent", __func__);
1720
1721 /* Read their reply */
1722 buffer_clear(&m);
1723
1724 if (mux_client_read_packet(fd, &m) != 0) {
1725 error("%s: read from master failed: %s",
1726 __func__, strerror(errno));
1727 buffer_free(&m);
1728 return -1;
1729 }
1730
1731 type = buffer_get_int(&m);
1732 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1733 fatal("%s: out of sequence reply: my id %u theirs %u",
1734 __func__, muxclient_request_id, rid);
1735 switch (type) {
1736 case MUX_S_SESSION_OPENED:
1737 sid = buffer_get_int(&m);
1738 debug("%s: master session id: %u", __func__, sid);
1739 break;
1740 case MUX_S_PERMISSION_DENIED:
1741 e = buffer_get_string(&m, NULL);
1742 buffer_free(&m);
1743 fatal("Master refused forwarding request: %s", e);
1744 case MUX_S_FAILURE:
1745 e = buffer_get_string(&m, NULL);
1746 buffer_free(&m);
1747 fatal("%s: stdio forwarding request failed: %s", __func__, e);
1748 default:
1749 buffer_free(&m);
1750 error("%s: unexpected response from master 0x%08x",
1751 __func__, type);
1752 return -1;
1753 }
1754 muxclient_request_id++;
1755
1756 signal(SIGHUP, control_client_sighandler);
1757 signal(SIGINT, control_client_sighandler);
1758 signal(SIGTERM, control_client_sighandler);
1759 signal(SIGWINCH, control_client_sigrelay);
1760
1761 /*
1762 * Stick around until the controlee closes the client_fd.
1763 */
1764 buffer_clear(&m);
1765 if (mux_client_read_packet(fd, &m) != 0) {
1766 if (errno == EPIPE ||
1767 (errno == EINTR && muxclient_terminate != 0))
1768 return 0;
1769 fatal("%s: mux_client_read_packet: %s",
1770 __func__, strerror(errno));
1771 }
1772 fatal("%s: master returned unexpected message %u", __func__, type);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001773}
1774
1775/* Multiplex client main loop. */
1776void
1777muxclient(const char *path)
1778{
1779 struct sockaddr_un addr;
Damien Millere1537f92010-01-26 13:26:22 +11001780 socklen_t sun_len;
1781 int sock;
1782 u_int pid;
Damien Millerb1cbfa22008-05-19 16:00:08 +10001783
Damien Millere1537f92010-01-26 13:26:22 +11001784 if (muxclient_command == 0) {
1785 if (stdio_forward_host != NULL)
1786 muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
1787 else
1788 muxclient_command = SSHMUX_COMMAND_OPEN;
1789 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001790
1791 switch (options.control_master) {
1792 case SSHCTL_MASTER_AUTO:
1793 case SSHCTL_MASTER_AUTO_ASK:
1794 debug("auto-mux: Trying existing master");
1795 /* FALLTHROUGH */
1796 case SSHCTL_MASTER_NO:
1797 break;
1798 default:
1799 return;
1800 }
1801
1802 memset(&addr, '\0', sizeof(addr));
1803 addr.sun_family = AF_UNIX;
Damien Millere1537f92010-01-26 13:26:22 +11001804 sun_len = offsetof(struct sockaddr_un, sun_path) +
Damien Millerb1cbfa22008-05-19 16:00:08 +10001805 strlen(path) + 1;
1806
1807 if (strlcpy(addr.sun_path, path,
1808 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1809 fatal("ControlPath too long");
1810
1811 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1812 fatal("%s socket(): %s", __func__, strerror(errno));
1813
Damien Millere1537f92010-01-26 13:26:22 +11001814 if (connect(sock, (struct sockaddr *)&addr, sun_len) == -1) {
1815 switch (muxclient_command) {
1816 case SSHMUX_COMMAND_OPEN:
1817 case SSHMUX_COMMAND_STDIO_FWD:
1818 break;
1819 default:
Damien Millerb1cbfa22008-05-19 16:00:08 +10001820 fatal("Control socket connect(%.100s): %s", path,
1821 strerror(errno));
1822 }
1823 if (errno == ENOENT)
1824 debug("Control socket \"%.100s\" does not exist", path);
1825 else {
1826 error("Control socket connect(%.100s): %s", path,
1827 strerror(errno));
1828 }
1829 close(sock);
1830 return;
1831 }
Damien Millere1537f92010-01-26 13:26:22 +11001832 set_nonblock(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001833
Damien Millere1537f92010-01-26 13:26:22 +11001834 if (mux_client_hello_exchange(sock) != 0) {
1835 error("%s: master hello exchange failed", __func__);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001836 close(sock);
Darren Tuckerca19bfe2008-06-13 10:24:03 +10001837 return;
1838 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001839
1840 switch (muxclient_command) {
1841 case SSHMUX_COMMAND_ALIVE_CHECK:
Damien Millere1537f92010-01-26 13:26:22 +11001842 if ((pid = mux_client_request_alive(sock)) == 0)
1843 fatal("%s: master alive check failed", __func__);
1844 fprintf(stderr, "Master running (pid=%d)\r\n", pid);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001845 exit(0);
1846 case SSHMUX_COMMAND_TERMINATE:
Damien Millere1537f92010-01-26 13:26:22 +11001847 mux_client_request_terminate(sock);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001848 fprintf(stderr, "Exit request sent.\r\n");
1849 exit(0);
Damien Miller388f6fc2010-05-21 14:57:35 +10001850 case SSHMUX_COMMAND_FORWARD:
1851 if (mux_client_request_forwards(sock) != 0)
1852 fatal("%s: master forward request failed", __func__);
1853 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001854 case SSHMUX_COMMAND_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +11001855 if (mux_client_request_forwards(sock) != 0) {
1856 error("%s: master forward request failed", __func__);
1857 return;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001858 }
Damien Millere1537f92010-01-26 13:26:22 +11001859 mux_client_request_session(sock);
1860 return;
1861 case SSHMUX_COMMAND_STDIO_FWD:
1862 mux_client_request_stdio_fwd(sock);
1863 exit(0);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001864 default:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001865 fatal("unrecognised muxclient_command %d", muxclient_command);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001866 }
Damien Millerb1cbfa22008-05-19 16:00:08 +10001867}