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