Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1 | /* $OpenBSD: mux.c,v 1.1 2008/05/09 14:18:44 djm Exp $ */ |
| 2 | /* |
| 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 | |
Damien Miller | a7058ec | 2008-05-20 08:57:06 +1000 | [diff] [blame^] | 20 | #include "includes.h" |
| 21 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/param.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/un.h> |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <signal.h> |
| 31 | #include <stdarg.h> |
| 32 | #include <stddef.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 37 | #include <paths.h> |
| 38 | |
Damien Miller | a7058ec | 2008-05-20 08:57:06 +1000 | [diff] [blame^] | 39 | #ifdef HAVE_UTIL_H |
| 40 | # include <util.h> |
| 41 | #endif |
| 42 | |
| 43 | #ifdef HAVE_LIBUTIL_H |
| 44 | # include <libutil.h> |
| 45 | #endif |
| 46 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 47 | #include "openbsd-compat/sys-queue.h" |
| 48 | #include "xmalloc.h" |
| 49 | #include "log.h" |
| 50 | #include "ssh.h" |
| 51 | #include "pathnames.h" |
| 52 | #include "misc.h" |
| 53 | #include "match.h" |
| 54 | #include "buffer.h" |
| 55 | #include "channels.h" |
| 56 | #include "msg.h" |
| 57 | #include "packet.h" |
| 58 | #include "monitor_fdpass.h" |
| 59 | #include "sshpty.h" |
| 60 | #include "key.h" |
| 61 | #include "readconf.h" |
| 62 | #include "clientloop.h" |
| 63 | |
| 64 | /* from ssh.c */ |
| 65 | extern int tty_flag; |
| 66 | extern Options options; |
| 67 | extern int stdin_null_flag; |
| 68 | extern char *host; |
| 69 | int subsystem_flag; |
| 70 | extern Buffer command; |
| 71 | |
| 72 | /* fd to control socket */ |
| 73 | int muxserver_sock = -1; |
| 74 | |
| 75 | /* Multiplexing control command */ |
| 76 | u_int muxclient_command = 0; |
| 77 | |
| 78 | /* Set when signalled. */ |
| 79 | static volatile sig_atomic_t muxclient_terminate = 0; |
| 80 | |
| 81 | /* PID of multiplex server */ |
| 82 | static u_int muxserver_pid = 0; |
| 83 | |
| 84 | |
| 85 | /* ** Multiplexing master support */ |
| 86 | |
| 87 | /* Prepare a mux master to listen on a Unix domain socket. */ |
| 88 | void |
| 89 | muxserver_listen(void) |
| 90 | { |
| 91 | struct sockaddr_un addr; |
| 92 | mode_t old_umask; |
| 93 | int addr_len; |
| 94 | |
| 95 | if (options.control_path == NULL || |
| 96 | options.control_master == SSHCTL_MASTER_NO) |
| 97 | return; |
| 98 | |
| 99 | debug("setting up multiplex master socket"); |
| 100 | |
| 101 | memset(&addr, '\0', sizeof(addr)); |
| 102 | addr.sun_family = AF_UNIX; |
| 103 | addr_len = offsetof(struct sockaddr_un, sun_path) + |
| 104 | strlen(options.control_path) + 1; |
| 105 | |
| 106 | if (strlcpy(addr.sun_path, options.control_path, |
| 107 | sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) |
| 108 | fatal("ControlPath too long"); |
| 109 | |
| 110 | if ((muxserver_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
| 111 | fatal("%s socket(): %s", __func__, strerror(errno)); |
| 112 | |
| 113 | old_umask = umask(0177); |
| 114 | if (bind(muxserver_sock, (struct sockaddr *)&addr, addr_len) == -1) { |
| 115 | muxserver_sock = -1; |
| 116 | if (errno == EINVAL || errno == EADDRINUSE) |
| 117 | fatal("ControlSocket %s already exists", |
| 118 | options.control_path); |
| 119 | else |
| 120 | fatal("%s bind(): %s", __func__, strerror(errno)); |
| 121 | } |
| 122 | umask(old_umask); |
| 123 | |
| 124 | if (listen(muxserver_sock, 64) == -1) |
| 125 | fatal("%s listen(): %s", __func__, strerror(errno)); |
| 126 | |
| 127 | set_nonblock(muxserver_sock); |
| 128 | } |
| 129 | |
| 130 | /* Callback on open confirmation in mux master for a mux client session. */ |
| 131 | static void |
| 132 | client_extra_session2_setup(int id, void *arg) |
| 133 | { |
| 134 | struct mux_session_confirm_ctx *cctx = arg; |
| 135 | const char *display; |
| 136 | Channel *c; |
| 137 | int i; |
| 138 | |
| 139 | if (cctx == NULL) |
| 140 | fatal("%s: cctx == NULL", __func__); |
| 141 | if ((c = channel_lookup(id)) == NULL) |
| 142 | fatal("%s: no channel for id %d", __func__, id); |
| 143 | |
| 144 | display = getenv("DISPLAY"); |
| 145 | if (cctx->want_x_fwd && options.forward_x11 && display != NULL) { |
| 146 | char *proto, *data; |
| 147 | /* Get reasonable local authentication information. */ |
| 148 | client_x11_get_proto(display, options.xauth_location, |
| 149 | options.forward_x11_trusted, &proto, &data); |
| 150 | /* Request forwarding with authentication spoofing. */ |
| 151 | debug("Requesting X11 forwarding with authentication spoofing."); |
| 152 | x11_request_forwarding_with_spoofing(id, display, proto, data); |
| 153 | /* XXX wait for reply */ |
| 154 | } |
| 155 | |
| 156 | if (cctx->want_agent_fwd && options.forward_agent) { |
| 157 | debug("Requesting authentication agent forwarding."); |
| 158 | channel_request_start(id, "auth-agent-req@openssh.com", 0); |
| 159 | packet_send(); |
| 160 | } |
| 161 | |
| 162 | client_session2_setup(id, cctx->want_tty, cctx->want_subsys, |
| 163 | cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env); |
| 164 | |
| 165 | c->open_confirm_ctx = NULL; |
| 166 | buffer_free(&cctx->cmd); |
| 167 | xfree(cctx->term); |
| 168 | if (cctx->env != NULL) { |
| 169 | for (i = 0; cctx->env[i] != NULL; i++) |
| 170 | xfree(cctx->env[i]); |
| 171 | xfree(cctx->env); |
| 172 | } |
| 173 | xfree(cctx); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Accept a connection on the mux master socket and process the |
| 178 | * client's request. Returns flag indicating whether mux master should |
| 179 | * begin graceful close. |
| 180 | */ |
| 181 | int |
| 182 | muxserver_accept_control(void) |
| 183 | { |
| 184 | Buffer m; |
| 185 | Channel *c; |
| 186 | int client_fd, new_fd[3], ver, allowed, window, packetmax; |
| 187 | socklen_t addrlen; |
| 188 | struct sockaddr_storage addr; |
| 189 | struct mux_session_confirm_ctx *cctx; |
| 190 | char *cmd; |
| 191 | u_int i, j, len, env_len, mux_command, flags; |
| 192 | uid_t euid; |
| 193 | gid_t egid; |
| 194 | int start_close = 0; |
| 195 | |
| 196 | /* |
| 197 | * Accept connection on control socket |
| 198 | */ |
| 199 | memset(&addr, 0, sizeof(addr)); |
| 200 | addrlen = sizeof(addr); |
| 201 | if ((client_fd = accept(muxserver_sock, |
| 202 | (struct sockaddr*)&addr, &addrlen)) == -1) { |
| 203 | error("%s accept: %s", __func__, strerror(errno)); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | if (getpeereid(client_fd, &euid, &egid) < 0) { |
| 208 | error("%s getpeereid failed: %s", __func__, strerror(errno)); |
| 209 | close(client_fd); |
| 210 | return 0; |
| 211 | } |
| 212 | if ((euid != 0) && (getuid() != euid)) { |
| 213 | error("control mode uid mismatch: peer euid %u != uid %u", |
| 214 | (u_int) euid, (u_int) getuid()); |
| 215 | close(client_fd); |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* XXX handle asynchronously */ |
| 220 | unset_nonblock(client_fd); |
| 221 | |
| 222 | /* Read command */ |
| 223 | buffer_init(&m); |
| 224 | if (ssh_msg_recv(client_fd, &m) == -1) { |
| 225 | error("%s: client msg_recv failed", __func__); |
| 226 | close(client_fd); |
| 227 | buffer_free(&m); |
| 228 | return 0; |
| 229 | } |
| 230 | if ((ver = buffer_get_char(&m)) != SSHMUX_VER) { |
| 231 | error("%s: wrong client version %d", __func__, ver); |
| 232 | buffer_free(&m); |
| 233 | close(client_fd); |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | allowed = 1; |
| 238 | mux_command = buffer_get_int(&m); |
| 239 | flags = buffer_get_int(&m); |
| 240 | |
| 241 | buffer_clear(&m); |
| 242 | |
| 243 | switch (mux_command) { |
| 244 | case SSHMUX_COMMAND_OPEN: |
| 245 | if (options.control_master == SSHCTL_MASTER_ASK || |
| 246 | options.control_master == SSHCTL_MASTER_AUTO_ASK) |
| 247 | allowed = ask_permission("Allow shared connection " |
| 248 | "to %s? ", host); |
| 249 | /* continue below */ |
| 250 | break; |
| 251 | case SSHMUX_COMMAND_TERMINATE: |
| 252 | if (options.control_master == SSHCTL_MASTER_ASK || |
| 253 | options.control_master == SSHCTL_MASTER_AUTO_ASK) |
| 254 | allowed = ask_permission("Terminate shared connection " |
| 255 | "to %s? ", host); |
| 256 | if (allowed) |
| 257 | start_close = 1; |
| 258 | /* FALLTHROUGH */ |
| 259 | case SSHMUX_COMMAND_ALIVE_CHECK: |
| 260 | /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */ |
| 261 | buffer_clear(&m); |
| 262 | buffer_put_int(&m, allowed); |
| 263 | buffer_put_int(&m, getpid()); |
| 264 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { |
| 265 | error("%s: client msg_send failed", __func__); |
| 266 | close(client_fd); |
| 267 | buffer_free(&m); |
| 268 | return start_close; |
| 269 | } |
| 270 | buffer_free(&m); |
| 271 | close(client_fd); |
| 272 | return start_close; |
| 273 | default: |
| 274 | error("Unsupported command %d", mux_command); |
| 275 | buffer_free(&m); |
| 276 | close(client_fd); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /* Reply for SSHMUX_COMMAND_OPEN */ |
| 281 | buffer_clear(&m); |
| 282 | buffer_put_int(&m, allowed); |
| 283 | buffer_put_int(&m, getpid()); |
| 284 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { |
| 285 | error("%s: client msg_send failed", __func__); |
| 286 | close(client_fd); |
| 287 | buffer_free(&m); |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | if (!allowed) { |
| 292 | error("Refused control connection"); |
| 293 | close(client_fd); |
| 294 | buffer_free(&m); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | buffer_clear(&m); |
| 299 | if (ssh_msg_recv(client_fd, &m) == -1) { |
| 300 | error("%s: client msg_recv failed", __func__); |
| 301 | close(client_fd); |
| 302 | buffer_free(&m); |
| 303 | return 0; |
| 304 | } |
| 305 | if ((ver = buffer_get_char(&m)) != SSHMUX_VER) { |
| 306 | error("%s: wrong client version %d", __func__, ver); |
| 307 | buffer_free(&m); |
| 308 | close(client_fd); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | cctx = xcalloc(1, sizeof(*cctx)); |
| 313 | cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0; |
| 314 | cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0; |
| 315 | cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0; |
| 316 | cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0; |
| 317 | cctx->term = buffer_get_string(&m, &len); |
| 318 | |
| 319 | cmd = buffer_get_string(&m, &len); |
| 320 | buffer_init(&cctx->cmd); |
| 321 | buffer_append(&cctx->cmd, cmd, strlen(cmd)); |
| 322 | |
| 323 | env_len = buffer_get_int(&m); |
| 324 | env_len = MIN(env_len, 4096); |
| 325 | debug3("%s: receiving %d env vars", __func__, env_len); |
| 326 | if (env_len != 0) { |
| 327 | cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env)); |
| 328 | for (i = 0; i < env_len; i++) |
| 329 | cctx->env[i] = buffer_get_string(&m, &len); |
| 330 | cctx->env[i] = NULL; |
| 331 | } |
| 332 | |
| 333 | debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__, |
| 334 | cctx->want_tty, cctx->want_subsys, cmd); |
| 335 | xfree(cmd); |
| 336 | |
| 337 | /* Gather fds from client */ |
| 338 | for(i = 0; i < 3; i++) { |
| 339 | if ((new_fd[i] = mm_receive_fd(client_fd)) == -1) { |
| 340 | error("%s: failed to receive fd %d from slave", |
| 341 | __func__, i); |
| 342 | for (j = 0; j < i; j++) |
| 343 | close(new_fd[j]); |
| 344 | for (j = 0; j < env_len; j++) |
| 345 | xfree(cctx->env[j]); |
| 346 | if (env_len > 0) |
| 347 | xfree(cctx->env); |
| 348 | xfree(cctx->term); |
| 349 | buffer_free(&cctx->cmd); |
| 350 | close(client_fd); |
| 351 | xfree(cctx); |
| 352 | return 0; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__, |
| 357 | new_fd[0], new_fd[1], new_fd[2]); |
| 358 | |
| 359 | /* Try to pick up ttymodes from client before it goes raw */ |
| 360 | if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1) |
| 361 | error("%s: tcgetattr: %s", __func__, strerror(errno)); |
| 362 | |
| 363 | /* This roundtrip is just for synchronisation of ttymodes */ |
| 364 | buffer_clear(&m); |
| 365 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { |
| 366 | error("%s: client msg_send failed", __func__); |
| 367 | close(client_fd); |
| 368 | close(new_fd[0]); |
| 369 | close(new_fd[1]); |
| 370 | close(new_fd[2]); |
| 371 | buffer_free(&m); |
| 372 | xfree(cctx->term); |
| 373 | if (env_len != 0) { |
| 374 | for (i = 0; i < env_len; i++) |
| 375 | xfree(cctx->env[i]); |
| 376 | xfree(cctx->env); |
| 377 | } |
| 378 | return 0; |
| 379 | } |
| 380 | buffer_free(&m); |
| 381 | |
| 382 | /* enable nonblocking unless tty */ |
| 383 | if (!isatty(new_fd[0])) |
| 384 | set_nonblock(new_fd[0]); |
| 385 | if (!isatty(new_fd[1])) |
| 386 | set_nonblock(new_fd[1]); |
| 387 | if (!isatty(new_fd[2])) |
| 388 | set_nonblock(new_fd[2]); |
| 389 | |
| 390 | set_nonblock(client_fd); |
| 391 | |
| 392 | window = CHAN_SES_WINDOW_DEFAULT; |
| 393 | packetmax = CHAN_SES_PACKET_DEFAULT; |
| 394 | if (cctx->want_tty) { |
| 395 | window >>= 1; |
| 396 | packetmax >>= 1; |
| 397 | } |
| 398 | |
| 399 | c = channel_new("session", SSH_CHANNEL_OPENING, |
| 400 | new_fd[0], new_fd[1], new_fd[2], window, packetmax, |
| 401 | CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0); |
| 402 | |
| 403 | /* XXX */ |
| 404 | c->ctl_fd = client_fd; |
| 405 | |
| 406 | debug3("%s: channel_new: %d", __func__, c->self); |
| 407 | |
| 408 | channel_send_open(c->self); |
| 409 | channel_register_open_confirm(c->self, |
| 410 | client_extra_session2_setup, cctx); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* ** Multiplexing client support */ |
| 415 | |
| 416 | /* Exit signal handler */ |
| 417 | static void |
| 418 | control_client_sighandler(int signo) |
| 419 | { |
| 420 | muxclient_terminate = signo; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Relay signal handler - used to pass some signals from mux client to |
| 425 | * mux master. |
| 426 | */ |
| 427 | static void |
| 428 | control_client_sigrelay(int signo) |
| 429 | { |
| 430 | int save_errno = errno; |
| 431 | |
| 432 | if (muxserver_pid > 1) |
| 433 | kill(muxserver_pid, signo); |
| 434 | |
| 435 | errno = save_errno; |
| 436 | } |
| 437 | |
| 438 | /* Check mux client environment variables before passing them to mux master. */ |
| 439 | static int |
| 440 | env_permitted(char *env) |
| 441 | { |
| 442 | int i, ret; |
| 443 | char name[1024], *cp; |
| 444 | |
| 445 | if ((cp = strchr(env, '=')) == NULL || cp == env) |
| 446 | return (0); |
| 447 | ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env); |
| 448 | if (ret <= 0 || (size_t)ret >= sizeof(name)) |
| 449 | fatal("env_permitted: name '%.100s...' too long", env); |
| 450 | |
| 451 | for (i = 0; i < options.num_send_env; i++) |
| 452 | if (match_pattern(name, options.send_env[i])) |
| 453 | return (1); |
| 454 | |
| 455 | return (0); |
| 456 | } |
| 457 | |
| 458 | /* Multiplex client main loop. */ |
| 459 | void |
| 460 | muxclient(const char *path) |
| 461 | { |
| 462 | struct sockaddr_un addr; |
| 463 | int i, r, fd, sock, exitval[2], num_env, addr_len; |
| 464 | Buffer m; |
| 465 | char *term; |
| 466 | extern char **environ; |
| 467 | u_int flags; |
| 468 | |
| 469 | if (muxclient_command == 0) |
| 470 | muxclient_command = SSHMUX_COMMAND_OPEN; |
| 471 | |
| 472 | switch (options.control_master) { |
| 473 | case SSHCTL_MASTER_AUTO: |
| 474 | case SSHCTL_MASTER_AUTO_ASK: |
| 475 | debug("auto-mux: Trying existing master"); |
| 476 | /* FALLTHROUGH */ |
| 477 | case SSHCTL_MASTER_NO: |
| 478 | break; |
| 479 | default: |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | memset(&addr, '\0', sizeof(addr)); |
| 484 | addr.sun_family = AF_UNIX; |
| 485 | addr_len = offsetof(struct sockaddr_un, sun_path) + |
| 486 | strlen(path) + 1; |
| 487 | |
| 488 | if (strlcpy(addr.sun_path, path, |
| 489 | sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) |
| 490 | fatal("ControlPath too long"); |
| 491 | |
| 492 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
| 493 | fatal("%s socket(): %s", __func__, strerror(errno)); |
| 494 | |
| 495 | if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) { |
| 496 | if (muxclient_command != SSHMUX_COMMAND_OPEN) { |
| 497 | fatal("Control socket connect(%.100s): %s", path, |
| 498 | strerror(errno)); |
| 499 | } |
| 500 | if (errno == ENOENT) |
| 501 | debug("Control socket \"%.100s\" does not exist", path); |
| 502 | else { |
| 503 | error("Control socket connect(%.100s): %s", path, |
| 504 | strerror(errno)); |
| 505 | } |
| 506 | close(sock); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | if (stdin_null_flag) { |
| 511 | if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1) |
| 512 | fatal("open(/dev/null): %s", strerror(errno)); |
| 513 | if (dup2(fd, STDIN_FILENO) == -1) |
| 514 | fatal("dup2: %s", strerror(errno)); |
| 515 | if (fd > STDERR_FILENO) |
| 516 | close(fd); |
| 517 | } |
| 518 | |
| 519 | term = getenv("TERM"); |
| 520 | |
| 521 | flags = 0; |
| 522 | if (tty_flag) |
| 523 | flags |= SSHMUX_FLAG_TTY; |
| 524 | if (subsystem_flag) |
| 525 | flags |= SSHMUX_FLAG_SUBSYS; |
| 526 | if (options.forward_x11) |
| 527 | flags |= SSHMUX_FLAG_X11_FWD; |
| 528 | if (options.forward_agent) |
| 529 | flags |= SSHMUX_FLAG_AGENT_FWD; |
| 530 | |
| 531 | signal(SIGPIPE, SIG_IGN); |
| 532 | |
| 533 | buffer_init(&m); |
| 534 | |
| 535 | /* Send our command to server */ |
| 536 | buffer_put_int(&m, muxclient_command); |
| 537 | buffer_put_int(&m, flags); |
| 538 | if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1) |
| 539 | fatal("%s: msg_send", __func__); |
| 540 | buffer_clear(&m); |
| 541 | |
| 542 | /* Get authorisation status and PID of controlee */ |
| 543 | if (ssh_msg_recv(sock, &m) == -1) |
| 544 | fatal("%s: msg_recv", __func__); |
| 545 | if (buffer_get_char(&m) != SSHMUX_VER) |
| 546 | fatal("%s: wrong version", __func__); |
| 547 | if (buffer_get_int(&m) != 1) |
| 548 | fatal("Connection to master denied"); |
| 549 | muxserver_pid = buffer_get_int(&m); |
| 550 | |
| 551 | buffer_clear(&m); |
| 552 | |
| 553 | switch (muxclient_command) { |
| 554 | case SSHMUX_COMMAND_ALIVE_CHECK: |
| 555 | fprintf(stderr, "Master running (pid=%d)\r\n", |
| 556 | muxserver_pid); |
| 557 | exit(0); |
| 558 | case SSHMUX_COMMAND_TERMINATE: |
| 559 | fprintf(stderr, "Exit request sent.\r\n"); |
| 560 | exit(0); |
| 561 | case SSHMUX_COMMAND_OPEN: |
| 562 | /* continue below */ |
| 563 | break; |
| 564 | default: |
| 565 | fatal("silly muxclient_command %d", muxclient_command); |
| 566 | } |
| 567 | |
| 568 | /* SSHMUX_COMMAND_OPEN */ |
| 569 | buffer_put_cstring(&m, term ? term : ""); |
| 570 | buffer_append(&command, "\0", 1); |
| 571 | buffer_put_cstring(&m, buffer_ptr(&command)); |
| 572 | |
| 573 | if (options.num_send_env == 0 || environ == NULL) { |
| 574 | buffer_put_int(&m, 0); |
| 575 | } else { |
| 576 | /* Pass environment */ |
| 577 | num_env = 0; |
| 578 | for (i = 0; environ[i] != NULL; i++) |
| 579 | if (env_permitted(environ[i])) |
| 580 | num_env++; /* Count */ |
| 581 | |
| 582 | buffer_put_int(&m, num_env); |
| 583 | |
| 584 | for (i = 0; environ[i] != NULL && num_env >= 0; i++) |
| 585 | if (env_permitted(environ[i])) { |
| 586 | num_env--; |
| 587 | buffer_put_cstring(&m, environ[i]); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1) |
| 592 | fatal("%s: msg_send", __func__); |
| 593 | |
| 594 | if (mm_send_fd(sock, STDIN_FILENO) == -1 || |
| 595 | mm_send_fd(sock, STDOUT_FILENO) == -1 || |
| 596 | mm_send_fd(sock, STDERR_FILENO) == -1) |
| 597 | fatal("%s: send fds failed", __func__); |
| 598 | |
| 599 | /* Wait for reply, so master has a chance to gather ttymodes */ |
| 600 | buffer_clear(&m); |
| 601 | if (ssh_msg_recv(sock, &m) == -1) |
| 602 | fatal("%s: msg_recv", __func__); |
| 603 | if (buffer_get_char(&m) != SSHMUX_VER) |
| 604 | fatal("%s: wrong version", __func__); |
| 605 | buffer_free(&m); |
| 606 | |
| 607 | signal(SIGHUP, control_client_sighandler); |
| 608 | signal(SIGINT, control_client_sighandler); |
| 609 | signal(SIGTERM, control_client_sighandler); |
| 610 | signal(SIGWINCH, control_client_sigrelay); |
| 611 | |
| 612 | if (tty_flag) |
| 613 | enter_raw_mode(); |
| 614 | |
| 615 | /* |
| 616 | * Stick around until the controlee closes the client_fd. |
| 617 | * Before it does, it is expected to write this process' exit |
| 618 | * value (one int). This process must read the value and wait for |
| 619 | * the closure of the client_fd; if this one closes early, the |
| 620 | * multiplex master will terminate early too (possibly losing data). |
| 621 | */ |
| 622 | exitval[0] = 0; |
| 623 | for (i = 0; !muxclient_terminate && i < (int)sizeof(exitval);) { |
| 624 | r = read(sock, (char *)exitval + i, sizeof(exitval) - i); |
| 625 | if (r == 0) { |
| 626 | debug2("Received EOF from master"); |
| 627 | break; |
| 628 | } |
| 629 | if (r == -1) { |
| 630 | if (errno == EINTR) |
| 631 | continue; |
| 632 | fatal("%s: read %s", __func__, strerror(errno)); |
| 633 | } |
| 634 | i += r; |
| 635 | } |
| 636 | |
| 637 | close(sock); |
| 638 | leave_raw_mode(); |
| 639 | if (i > (int)sizeof(int)) |
| 640 | fatal("%s: master returned too much data (%d > %lu)", |
| 641 | __func__, i, sizeof(int)); |
| 642 | if (muxclient_terminate) { |
| 643 | debug2("Exiting on signal %d", muxclient_terminate); |
| 644 | exitval[0] = 255; |
| 645 | } else if (i < (int)sizeof(int)) { |
| 646 | debug2("Control master terminated unexpectedly"); |
| 647 | exitval[0] = 255; |
| 648 | } else |
| 649 | debug2("Received exit status from master %d", exitval[0]); |
| 650 | |
| 651 | if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET) |
| 652 | fprintf(stderr, "Shared connection to %s closed.\r\n", host); |
| 653 | |
| 654 | exit(exitval[0]); |
| 655 | } |