1 /* $OpenBSD: mux.c,v 1.1 2008/05/09 14:18:44 djm Exp $ */
3 * Copyright (c) 2002-2008 Damien Miller <djm@openbsd.org>
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.
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.
18 /* ssh session multiplexing support */
20 #include <sys/types.h>
21 #include <sys/param.h>
23 #include <sys/socket.h>
38 #include "openbsd-compat/sys-queue.h"
42 #include "pathnames.h"
49 #include "monitor_fdpass.h"
53 #include "clientloop.h"
57 extern Options options;
58 extern int stdin_null_flag;
61 extern Buffer command;
63 /* fd to control socket */
64 int muxserver_sock = -1;
66 /* Multiplexing control command */
67 u_int muxclient_command = 0;
69 /* Set when signalled. */
70 static volatile sig_atomic_t muxclient_terminate = 0;
72 /* PID of multiplex server */
73 static u_int muxserver_pid = 0;
76 /* ** Multiplexing master support */
78 /* Prepare a mux master to listen on a Unix domain socket. */
80 muxserver_listen(void)
82 struct sockaddr_un addr;
86 if (options.control_path == NULL ||
87 options.control_master == SSHCTL_MASTER_NO)
90 debug("setting up multiplex master socket");
92 memset(&addr, '\0', sizeof(addr));
93 addr.sun_family = AF_UNIX;
94 addr_len = offsetof(struct sockaddr_un, sun_path) +
95 strlen(options.control_path) + 1;
97 if (strlcpy(addr.sun_path, options.control_path,
98 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
99 fatal("ControlPath too long");
101 if ((muxserver_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
102 fatal("%s socket(): %s", __func__, strerror(errno));
104 old_umask = umask(0177);
105 if (bind(muxserver_sock, (struct sockaddr *)&addr, addr_len) == -1) {
107 if (errno == EINVAL || errno == EADDRINUSE)
108 fatal("ControlSocket %s already exists",
109 options.control_path);
111 fatal("%s bind(): %s", __func__, strerror(errno));
115 if (listen(muxserver_sock, 64) == -1)
116 fatal("%s listen(): %s", __func__, strerror(errno));
118 set_nonblock(muxserver_sock);
121 /* Callback on open confirmation in mux master for a mux client session. */
123 client_extra_session2_setup(int id, void *arg)
125 struct mux_session_confirm_ctx *cctx = arg;
131 fatal("%s: cctx == NULL", __func__);
132 if ((c = channel_lookup(id)) == NULL)
133 fatal("%s: no channel for id %d", __func__, id);
135 display = getenv("DISPLAY");
136 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
138 /* Get reasonable local authentication information. */
139 client_x11_get_proto(display, options.xauth_location,
140 options.forward_x11_trusted, &proto, &data);
141 /* Request forwarding with authentication spoofing. */
142 debug("Requesting X11 forwarding with authentication spoofing.");
143 x11_request_forwarding_with_spoofing(id, display, proto, data);
144 /* XXX wait for reply */
147 if (cctx->want_agent_fwd && options.forward_agent) {
148 debug("Requesting authentication agent forwarding.");
149 channel_request_start(id, "auth-agent-req@openssh.com", 0);
153 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
154 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
156 c->open_confirm_ctx = NULL;
157 buffer_free(&cctx->cmd);
159 if (cctx->env != NULL) {
160 for (i = 0; cctx->env[i] != NULL; i++)
168 * Accept a connection on the mux master socket and process the
169 * client's request. Returns flag indicating whether mux master should
170 * begin graceful close.
173 muxserver_accept_control(void)
177 int client_fd, new_fd[3], ver, allowed, window, packetmax;
179 struct sockaddr_storage addr;
180 struct mux_session_confirm_ctx *cctx;
182 u_int i, j, len, env_len, mux_command, flags;
188 * Accept connection on control socket
190 memset(&addr, 0, sizeof(addr));
191 addrlen = sizeof(addr);
192 if ((client_fd = accept(muxserver_sock,
193 (struct sockaddr*)&addr, &addrlen)) == -1) {
194 error("%s accept: %s", __func__, strerror(errno));
198 if (getpeereid(client_fd, &euid, &egid) < 0) {
199 error("%s getpeereid failed: %s", __func__, strerror(errno));
203 if ((euid != 0) && (getuid() != euid)) {
204 error("control mode uid mismatch: peer euid %u != uid %u",
205 (u_int) euid, (u_int) getuid());
210 /* XXX handle asynchronously */
211 unset_nonblock(client_fd);
215 if (ssh_msg_recv(client_fd, &m) == -1) {
216 error("%s: client msg_recv failed", __func__);
221 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
222 error("%s: wrong client version %d", __func__, ver);
229 mux_command = buffer_get_int(&m);
230 flags = buffer_get_int(&m);
234 switch (mux_command) {
235 case SSHMUX_COMMAND_OPEN:
236 if (options.control_master == SSHCTL_MASTER_ASK ||
237 options.control_master == SSHCTL_MASTER_AUTO_ASK)
238 allowed = ask_permission("Allow shared connection "
242 case SSHMUX_COMMAND_TERMINATE:
243 if (options.control_master == SSHCTL_MASTER_ASK ||
244 options.control_master == SSHCTL_MASTER_AUTO_ASK)
245 allowed = ask_permission("Terminate shared connection "
250 case SSHMUX_COMMAND_ALIVE_CHECK:
251 /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */
253 buffer_put_int(&m, allowed);
254 buffer_put_int(&m, getpid());
255 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
256 error("%s: client msg_send failed", __func__);
265 error("Unsupported command %d", mux_command);
271 /* Reply for SSHMUX_COMMAND_OPEN */
273 buffer_put_int(&m, allowed);
274 buffer_put_int(&m, getpid());
275 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
276 error("%s: client msg_send failed", __func__);
283 error("Refused control connection");
290 if (ssh_msg_recv(client_fd, &m) == -1) {
291 error("%s: client msg_recv failed", __func__);
296 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
297 error("%s: wrong client version %d", __func__, ver);
303 cctx = xcalloc(1, sizeof(*cctx));
304 cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
305 cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
306 cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
307 cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0;
308 cctx->term = buffer_get_string(&m, &len);
310 cmd = buffer_get_string(&m, &len);
311 buffer_init(&cctx->cmd);
312 buffer_append(&cctx->cmd, cmd, strlen(cmd));
314 env_len = buffer_get_int(&m);
315 env_len = MIN(env_len, 4096);
316 debug3("%s: receiving %d env vars", __func__, env_len);
318 cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env));
319 for (i = 0; i < env_len; i++)
320 cctx->env[i] = buffer_get_string(&m, &len);
324 debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
325 cctx->want_tty, cctx->want_subsys, cmd);
328 /* Gather fds from client */
329 for(i = 0; i < 3; i++) {
330 if ((new_fd[i] = mm_receive_fd(client_fd)) == -1) {
331 error("%s: failed to receive fd %d from slave",
333 for (j = 0; j < i; j++)
335 for (j = 0; j < env_len; j++)
340 buffer_free(&cctx->cmd);
347 debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
348 new_fd[0], new_fd[1], new_fd[2]);
350 /* Try to pick up ttymodes from client before it goes raw */
351 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
352 error("%s: tcgetattr: %s", __func__, strerror(errno));
354 /* This roundtrip is just for synchronisation of ttymodes */
356 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
357 error("%s: client msg_send failed", __func__);
365 for (i = 0; i < env_len; i++)
373 /* enable nonblocking unless tty */
374 if (!isatty(new_fd[0]))
375 set_nonblock(new_fd[0]);
376 if (!isatty(new_fd[1]))
377 set_nonblock(new_fd[1]);
378 if (!isatty(new_fd[2]))
379 set_nonblock(new_fd[2]);
381 set_nonblock(client_fd);
383 window = CHAN_SES_WINDOW_DEFAULT;
384 packetmax = CHAN_SES_PACKET_DEFAULT;
385 if (cctx->want_tty) {
390 c = channel_new("session", SSH_CHANNEL_OPENING,
391 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
392 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
395 c->ctl_fd = client_fd;
397 debug3("%s: channel_new: %d", __func__, c->self);
399 channel_send_open(c->self);
400 channel_register_open_confirm(c->self,
401 client_extra_session2_setup, cctx);
405 /* ** Multiplexing client support */
407 /* Exit signal handler */
409 control_client_sighandler(int signo)
411 muxclient_terminate = signo;
415 * Relay signal handler - used to pass some signals from mux client to
419 control_client_sigrelay(int signo)
421 int save_errno = errno;
423 if (muxserver_pid > 1)
424 kill(muxserver_pid, signo);
429 /* Check mux client environment variables before passing them to mux master. */
431 env_permitted(char *env)
434 char name[1024], *cp;
436 if ((cp = strchr(env, '=')) == NULL || cp == env)
438 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
439 if (ret <= 0 || (size_t)ret >= sizeof(name))
440 fatal("env_permitted: name '%.100s...' too long", env);
442 for (i = 0; i < options.num_send_env; i++)
443 if (match_pattern(name, options.send_env[i]))
449 /* Multiplex client main loop. */
451 muxclient(const char *path)
453 struct sockaddr_un addr;
454 int i, r, fd, sock, exitval[2], num_env, addr_len;
457 extern char **environ;
460 if (muxclient_command == 0)
461 muxclient_command = SSHMUX_COMMAND_OPEN;
463 switch (options.control_master) {
464 case SSHCTL_MASTER_AUTO:
465 case SSHCTL_MASTER_AUTO_ASK:
466 debug("auto-mux: Trying existing master");
468 case SSHCTL_MASTER_NO:
474 memset(&addr, '\0', sizeof(addr));
475 addr.sun_family = AF_UNIX;
476 addr_len = offsetof(struct sockaddr_un, sun_path) +
479 if (strlcpy(addr.sun_path, path,
480 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
481 fatal("ControlPath too long");
483 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
484 fatal("%s socket(): %s", __func__, strerror(errno));
486 if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
487 if (muxclient_command != SSHMUX_COMMAND_OPEN) {
488 fatal("Control socket connect(%.100s): %s", path,
492 debug("Control socket \"%.100s\" does not exist", path);
494 error("Control socket connect(%.100s): %s", path,
501 if (stdin_null_flag) {
502 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
503 fatal("open(/dev/null): %s", strerror(errno));
504 if (dup2(fd, STDIN_FILENO) == -1)
505 fatal("dup2: %s", strerror(errno));
506 if (fd > STDERR_FILENO)
510 term = getenv("TERM");
514 flags |= SSHMUX_FLAG_TTY;
516 flags |= SSHMUX_FLAG_SUBSYS;
517 if (options.forward_x11)
518 flags |= SSHMUX_FLAG_X11_FWD;
519 if (options.forward_agent)
520 flags |= SSHMUX_FLAG_AGENT_FWD;
522 signal(SIGPIPE, SIG_IGN);
526 /* Send our command to server */
527 buffer_put_int(&m, muxclient_command);
528 buffer_put_int(&m, flags);
529 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
530 fatal("%s: msg_send", __func__);
533 /* Get authorisation status and PID of controlee */
534 if (ssh_msg_recv(sock, &m) == -1)
535 fatal("%s: msg_recv", __func__);
536 if (buffer_get_char(&m) != SSHMUX_VER)
537 fatal("%s: wrong version", __func__);
538 if (buffer_get_int(&m) != 1)
539 fatal("Connection to master denied");
540 muxserver_pid = buffer_get_int(&m);
544 switch (muxclient_command) {
545 case SSHMUX_COMMAND_ALIVE_CHECK:
546 fprintf(stderr, "Master running (pid=%d)\r\n",
549 case SSHMUX_COMMAND_TERMINATE:
550 fprintf(stderr, "Exit request sent.\r\n");
552 case SSHMUX_COMMAND_OPEN:
556 fatal("silly muxclient_command %d", muxclient_command);
559 /* SSHMUX_COMMAND_OPEN */
560 buffer_put_cstring(&m, term ? term : "");
561 buffer_append(&command, "\0", 1);
562 buffer_put_cstring(&m, buffer_ptr(&command));
564 if (options.num_send_env == 0 || environ == NULL) {
565 buffer_put_int(&m, 0);
567 /* Pass environment */
569 for (i = 0; environ[i] != NULL; i++)
570 if (env_permitted(environ[i]))
571 num_env++; /* Count */
573 buffer_put_int(&m, num_env);
575 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
576 if (env_permitted(environ[i])) {
578 buffer_put_cstring(&m, environ[i]);
582 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
583 fatal("%s: msg_send", __func__);
585 if (mm_send_fd(sock, STDIN_FILENO) == -1 ||
586 mm_send_fd(sock, STDOUT_FILENO) == -1 ||
587 mm_send_fd(sock, STDERR_FILENO) == -1)
588 fatal("%s: send fds failed", __func__);
590 /* Wait for reply, so master has a chance to gather ttymodes */
592 if (ssh_msg_recv(sock, &m) == -1)
593 fatal("%s: msg_recv", __func__);
594 if (buffer_get_char(&m) != SSHMUX_VER)
595 fatal("%s: wrong version", __func__);
598 signal(SIGHUP, control_client_sighandler);
599 signal(SIGINT, control_client_sighandler);
600 signal(SIGTERM, control_client_sighandler);
601 signal(SIGWINCH, control_client_sigrelay);
607 * Stick around until the controlee closes the client_fd.
608 * Before it does, it is expected to write this process' exit
609 * value (one int). This process must read the value and wait for
610 * the closure of the client_fd; if this one closes early, the
611 * multiplex master will terminate early too (possibly losing data).
614 for (i = 0; !muxclient_terminate && i < (int)sizeof(exitval);) {
615 r = read(sock, (char *)exitval + i, sizeof(exitval) - i);
617 debug2("Received EOF from master");
623 fatal("%s: read %s", __func__, strerror(errno));
630 if (i > (int)sizeof(int))
631 fatal("%s: master returned too much data (%d > %lu)",
632 __func__, i, sizeof(int));
633 if (muxclient_terminate) {
634 debug2("Exiting on signal %d", muxclient_terminate);
636 } else if (i < (int)sizeof(int)) {
637 debug2("Control master terminated unexpectedly");
640 debug2("Received exit status from master %d", exitval[0]);
642 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
643 fprintf(stderr, "Shared connection to %s closed.\r\n", host);