1 /* $NetBSD: mux.c,v 1.13 2015/08/21 08:20:59 christos Exp $ */ 2 /* $OpenBSD: mux.c,v 1.54 2015/08/19 23:18:26 djm Exp $ */ 3 /* 4 * Copyright (c) 2002-2008 Damien Miller <djm@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* ssh session multiplexing support */ 20 21 /* 22 * TODO: 23 * - Better signalling from master to slave, especially passing of 24 * error messages 25 * - Better fall-back from mux slave error to new connection. 26 * - ExitOnForwardingFailure 27 * - Maybe extension mechanisms for multi-X11/multi-agent forwarding 28 * - Support ~^Z in mux slaves. 29 * - Inspect or control sessions in master. 30 * - If we ever support the "signal" channel request, send signals on 31 * sessions in master. 32 */ 33 34 #include "includes.h" 35 __RCSID("$NetBSD: mux.c,v 1.13 2015/08/21 08:20:59 christos Exp $"); 36 #include <sys/types.h> 37 #include <sys/queue.h> 38 #include <sys/stat.h> 39 #include <sys/socket.h> 40 #include <sys/un.h> 41 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <poll.h> 45 #include <signal.h> 46 #include <stdarg.h> 47 #include <stddef.h> 48 #include <stdlib.h> 49 #include <stdio.h> 50 #include <string.h> 51 #include <unistd.h> 52 #include <util.h> 53 #include <paths.h> 54 55 #include "atomicio.h" 56 #include "xmalloc.h" 57 #include "log.h" 58 #include "ssh.h" 59 #include "ssh2.h" 60 #include "pathnames.h" 61 #include "misc.h" 62 #include "match.h" 63 #include "buffer.h" 64 #include "channels.h" 65 #include "msg.h" 66 #include "packet.h" 67 #include "monitor_fdpass.h" 68 #include "sshpty.h" 69 #include "key.h" 70 #include "readconf.h" 71 #include "clientloop.h" 72 73 /* from ssh.c */ 74 extern int tty_flag; 75 extern Options options; 76 extern int stdin_null_flag; 77 extern char *host; 78 extern int subsystem_flag; 79 extern Buffer command; 80 extern volatile sig_atomic_t quit_pending; 81 extern char *stdio_forward_host; 82 extern int stdio_forward_port; 83 84 /* Context for session open confirmation callback */ 85 struct mux_session_confirm_ctx { 86 u_int want_tty; 87 u_int want_subsys; 88 u_int want_x_fwd; 89 u_int want_agent_fwd; 90 Buffer cmd; 91 char *term; 92 struct termios tio; 93 char **env; 94 u_int rid; 95 }; 96 97 /* Context for stdio fwd open confirmation callback */ 98 struct mux_stdio_confirm_ctx { 99 u_int rid; 100 }; 101 102 /* Context for global channel callback */ 103 struct mux_channel_confirm_ctx { 104 u_int cid; /* channel id */ 105 u_int rid; /* request id */ 106 int fid; /* forward id */ 107 }; 108 109 /* fd to control socket */ 110 int muxserver_sock = -1; 111 112 /* client request id */ 113 u_int muxclient_request_id = 0; 114 115 /* Multiplexing control command */ 116 u_int muxclient_command = 0; 117 118 /* Set when signalled. */ 119 static volatile sig_atomic_t muxclient_terminate = 0; 120 121 /* PID of multiplex server */ 122 static u_int muxserver_pid = 0; 123 124 static Channel *mux_listener_channel = NULL; 125 126 struct mux_master_state { 127 int hello_rcvd; 128 }; 129 130 /* mux protocol messages */ 131 #define MUX_MSG_HELLO 0x00000001 132 #define MUX_C_NEW_SESSION 0x10000002 133 #define MUX_C_ALIVE_CHECK 0x10000004 134 #define MUX_C_TERMINATE 0x10000005 135 #define MUX_C_OPEN_FWD 0x10000006 136 #define MUX_C_CLOSE_FWD 0x10000007 137 #define MUX_C_NEW_STDIO_FWD 0x10000008 138 #define MUX_C_STOP_LISTENING 0x10000009 139 #define MUX_S_OK 0x80000001 140 #define MUX_S_PERMISSION_DENIED 0x80000002 141 #define MUX_S_FAILURE 0x80000003 142 #define MUX_S_EXIT_MESSAGE 0x80000004 143 #define MUX_S_ALIVE 0x80000005 144 #define MUX_S_SESSION_OPENED 0x80000006 145 #define MUX_S_REMOTE_PORT 0x80000007 146 #define MUX_S_TTY_ALLOC_FAIL 0x80000008 147 148 /* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */ 149 #define MUX_FWD_LOCAL 1 150 #define MUX_FWD_REMOTE 2 151 #define MUX_FWD_DYNAMIC 3 152 153 static void mux_session_confirm(int, int, void *); 154 static void mux_stdio_confirm(int, int, void *); 155 156 static int process_mux_master_hello(u_int, Channel *, Buffer *, Buffer *); 157 static int process_mux_new_session(u_int, Channel *, Buffer *, Buffer *); 158 static int process_mux_alive_check(u_int, Channel *, Buffer *, Buffer *); 159 static int process_mux_terminate(u_int, Channel *, Buffer *, Buffer *); 160 static int process_mux_open_fwd(u_int, Channel *, Buffer *, Buffer *); 161 static int process_mux_close_fwd(u_int, Channel *, Buffer *, Buffer *); 162 static int process_mux_stdio_fwd(u_int, Channel *, Buffer *, Buffer *); 163 static int process_mux_stop_listening(u_int, Channel *, Buffer *, Buffer *); 164 165 static const struct { 166 u_int type; 167 int (*handler)(u_int, Channel *, Buffer *, Buffer *); 168 } mux_master_handlers[] = { 169 { MUX_MSG_HELLO, process_mux_master_hello }, 170 { MUX_C_NEW_SESSION, process_mux_new_session }, 171 { MUX_C_ALIVE_CHECK, process_mux_alive_check }, 172 { MUX_C_TERMINATE, process_mux_terminate }, 173 { MUX_C_OPEN_FWD, process_mux_open_fwd }, 174 { MUX_C_CLOSE_FWD, process_mux_close_fwd }, 175 { MUX_C_NEW_STDIO_FWD, process_mux_stdio_fwd }, 176 { MUX_C_STOP_LISTENING, process_mux_stop_listening }, 177 { 0, NULL } 178 }; 179 180 /* Cleanup callback fired on closure of mux slave _session_ channel */ 181 /* ARGSUSED */ 182 static void 183 mux_master_session_cleanup_cb(int cid, void *unused) 184 { 185 Channel *cc, *c = channel_by_id(cid); 186 187 debug3("%s: entering for channel %d", __func__, cid); 188 if (c == NULL) 189 fatal("%s: channel_by_id(%i) == NULL", __func__, cid); 190 if (c->ctl_chan != -1) { 191 if ((cc = channel_by_id(c->ctl_chan)) == NULL) 192 fatal("%s: channel %d missing control channel %d", 193 __func__, c->self, c->ctl_chan); 194 c->ctl_chan = -1; 195 cc->remote_id = -1; 196 chan_rcvd_oclose(cc); 197 } 198 channel_cancel_cleanup(c->self); 199 } 200 201 /* Cleanup callback fired on closure of mux slave _control_ channel */ 202 /* ARGSUSED */ 203 static void 204 mux_master_control_cleanup_cb(int cid, void *unused) 205 { 206 Channel *sc, *c = channel_by_id(cid); 207 208 debug3("%s: entering for channel %d", __func__, cid); 209 if (c == NULL) 210 fatal("%s: channel_by_id(%i) == NULL", __func__, cid); 211 if (c->remote_id != -1) { 212 if ((sc = channel_by_id(c->remote_id)) == NULL) 213 fatal("%s: channel %d missing session channel %d", 214 __func__, c->self, c->remote_id); 215 c->remote_id = -1; 216 sc->ctl_chan = -1; 217 if (sc->type != SSH_CHANNEL_OPEN && 218 sc->type != SSH_CHANNEL_OPENING) { 219 debug2("%s: channel %d: not open", __func__, sc->self); 220 chan_mark_dead(sc); 221 } else { 222 if (sc->istate == CHAN_INPUT_OPEN) 223 chan_read_failed(sc); 224 if (sc->ostate == CHAN_OUTPUT_OPEN) 225 chan_write_failed(sc); 226 } 227 } 228 channel_cancel_cleanup(c->self); 229 } 230 231 /* Check mux client environment variables before passing them to mux master. */ 232 static int 233 env_permitted(char *env) 234 { 235 int i, ret; 236 char name[1024], *cp; 237 238 if ((cp = strchr(env, '=')) == NULL || cp == env) 239 return 0; 240 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env); 241 if (ret <= 0 || (size_t)ret >= sizeof(name)) { 242 error("env_permitted: name '%.100s...' too long", env); 243 return 0; 244 } 245 246 for (i = 0; i < options.num_send_env; i++) 247 if (match_pattern(name, options.send_env[i])) 248 return 1; 249 250 return 0; 251 } 252 253 /* Mux master protocol message handlers */ 254 255 static int 256 process_mux_master_hello(u_int rid, Channel *c, Buffer *m, Buffer *r) 257 { 258 u_int ver; 259 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx; 260 261 if (state == NULL) 262 fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self); 263 if (state->hello_rcvd) { 264 error("%s: HELLO received twice", __func__); 265 return -1; 266 } 267 if (buffer_get_int_ret(&ver, m) != 0) { 268 malf: 269 error("%s: malformed message", __func__); 270 return -1; 271 } 272 if (ver != SSHMUX_VER) { 273 error("Unsupported multiplexing protocol version %d " 274 "(expected %d)", ver, SSHMUX_VER); 275 return -1; 276 } 277 debug2("%s: channel %d slave version %u", __func__, c->self, ver); 278 279 /* No extensions are presently defined */ 280 while (buffer_len(m) > 0) { 281 char *name = buffer_get_string_ret(m, NULL); 282 char *value = buffer_get_string_ret(m, NULL); 283 284 if (name == NULL || value == NULL) { 285 free(name); 286 free(value); 287 goto malf; 288 } 289 debug2("Unrecognised slave extension \"%s\"", name); 290 free(name); 291 free(value); 292 } 293 state->hello_rcvd = 1; 294 return 0; 295 } 296 297 static int 298 process_mux_new_session(u_int rid, Channel *c, Buffer *m, Buffer *r) 299 { 300 Channel *nc; 301 struct mux_session_confirm_ctx *cctx; 302 char *reserved, *cmd, *cp; 303 u_int i, j, len, env_len, escape_char, window, packetmax; 304 int new_fd[3]; 305 306 /* Reply for SSHMUX_COMMAND_OPEN */ 307 cctx = xcalloc(1, sizeof(*cctx)); 308 cctx->term = NULL; 309 cctx->rid = rid; 310 cmd = reserved = NULL; 311 cctx->env = NULL; 312 env_len = 0; 313 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL || 314 buffer_get_int_ret(&cctx->want_tty, m) != 0 || 315 buffer_get_int_ret(&cctx->want_x_fwd, m) != 0 || 316 buffer_get_int_ret(&cctx->want_agent_fwd, m) != 0 || 317 buffer_get_int_ret(&cctx->want_subsys, m) != 0 || 318 buffer_get_int_ret(&escape_char, m) != 0 || 319 (cctx->term = buffer_get_string_ret(m, &len)) == NULL || 320 (cmd = buffer_get_string_ret(m, &len)) == NULL) { 321 malf: 322 free(cmd); 323 free(reserved); 324 for (j = 0; j < env_len; j++) 325 free(cctx->env[j]); 326 free(cctx->env); 327 free(cctx->term); 328 free(cctx); 329 error("%s: malformed message", __func__); 330 return -1; 331 } 332 free(reserved); 333 reserved = NULL; 334 335 while (buffer_len(m) > 0) { 336 #define MUX_MAX_ENV_VARS 4096 337 if ((cp = buffer_get_string_ret(m, &len)) == NULL) 338 goto malf; 339 if (!env_permitted(cp)) { 340 free(cp); 341 continue; 342 } 343 cctx->env = xreallocarray(cctx->env, env_len + 2, 344 sizeof(*cctx->env)); 345 cctx->env[env_len++] = cp; 346 cctx->env[env_len] = NULL; 347 if (env_len > MUX_MAX_ENV_VARS) { 348 error(">%d environment variables received, ignoring " 349 "additional", MUX_MAX_ENV_VARS); 350 break; 351 } 352 } 353 354 debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, " 355 "term \"%s\", cmd \"%s\", env %u", __func__, c->self, 356 cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd, 357 cctx->want_subsys, cctx->term, cmd, env_len); 358 359 buffer_init(&cctx->cmd); 360 buffer_append(&cctx->cmd, cmd, strlen(cmd)); 361 free(cmd); 362 cmd = NULL; 363 364 /* Gather fds from client */ 365 for(i = 0; i < 3; i++) { 366 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) { 367 error("%s: failed to receive fd %d from slave", 368 __func__, i); 369 for (j = 0; j < i; j++) 370 close(new_fd[j]); 371 for (j = 0; j < env_len; j++) 372 free(cctx->env[j]); 373 free(cctx->env); 374 free(cctx->term); 375 buffer_free(&cctx->cmd); 376 free(cctx); 377 378 /* prepare reply */ 379 buffer_put_int(r, MUX_S_FAILURE); 380 buffer_put_int(r, rid); 381 buffer_put_cstring(r, 382 "did not receive file descriptors"); 383 return -1; 384 } 385 } 386 387 debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__, 388 new_fd[0], new_fd[1], new_fd[2]); 389 390 /* XXX support multiple child sessions in future */ 391 if (c->remote_id != -1) { 392 debug2("%s: session already open", __func__); 393 /* prepare reply */ 394 buffer_put_int(r, MUX_S_FAILURE); 395 buffer_put_int(r, rid); 396 buffer_put_cstring(r, "Multiple sessions not supported"); 397 cleanup: 398 close(new_fd[0]); 399 close(new_fd[1]); 400 close(new_fd[2]); 401 free(cctx->term); 402 if (env_len != 0) { 403 for (i = 0; i < env_len; i++) 404 free(cctx->env[i]); 405 free(cctx->env); 406 } 407 buffer_free(&cctx->cmd); 408 free(cctx); 409 return 0; 410 } 411 412 if (options.control_master == SSHCTL_MASTER_ASK || 413 options.control_master == SSHCTL_MASTER_AUTO_ASK) { 414 if (!ask_permission("Allow shared connection to %s? ", host)) { 415 debug2("%s: session refused by user", __func__); 416 /* prepare reply */ 417 buffer_put_int(r, MUX_S_PERMISSION_DENIED); 418 buffer_put_int(r, rid); 419 buffer_put_cstring(r, "Permission denied"); 420 goto cleanup; 421 } 422 } 423 424 /* Try to pick up ttymodes from client before it goes raw */ 425 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1) 426 error("%s: tcgetattr: %s", __func__, strerror(errno)); 427 428 /* enable nonblocking unless tty */ 429 if (!isatty(new_fd[0])) 430 set_nonblock(new_fd[0]); 431 if (!isatty(new_fd[1])) 432 set_nonblock(new_fd[1]); 433 if (!isatty(new_fd[2])) 434 set_nonblock(new_fd[2]); 435 436 window = CHAN_SES_WINDOW_DEFAULT; 437 packetmax = CHAN_SES_PACKET_DEFAULT; 438 if (cctx->want_tty) { 439 window >>= 1; 440 packetmax >>= 1; 441 } 442 443 nc = channel_new("session", SSH_CHANNEL_OPENING, 444 new_fd[0], new_fd[1], new_fd[2], window, packetmax, 445 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0); 446 447 nc->ctl_chan = c->self; /* link session -> control channel */ 448 c->remote_id = nc->self; /* link control -> session channel */ 449 450 if (cctx->want_tty && escape_char != 0xffffffff) { 451 channel_register_filter(nc->self, 452 client_simple_escape_filter, NULL, 453 client_filter_cleanup, 454 client_new_escape_filter_ctx((int)escape_char)); 455 } 456 457 debug2("%s: channel_new: %d linked to control channel %d", 458 __func__, nc->self, nc->ctl_chan); 459 460 channel_send_open(nc->self); 461 channel_register_open_confirm(nc->self, mux_session_confirm, cctx); 462 c->mux_pause = 1; /* stop handling messages until open_confirm done */ 463 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1); 464 465 /* reply is deferred, sent by mux_session_confirm */ 466 return 0; 467 } 468 469 static int 470 process_mux_alive_check(u_int rid, Channel *c, Buffer *m, Buffer *r) 471 { 472 debug2("%s: channel %d: alive check", __func__, c->self); 473 474 /* prepare reply */ 475 buffer_put_int(r, MUX_S_ALIVE); 476 buffer_put_int(r, rid); 477 buffer_put_int(r, (u_int)getpid()); 478 479 return 0; 480 } 481 482 static int 483 process_mux_terminate(u_int rid, Channel *c, Buffer *m, Buffer *r) 484 { 485 debug2("%s: channel %d: terminate request", __func__, c->self); 486 487 if (options.control_master == SSHCTL_MASTER_ASK || 488 options.control_master == SSHCTL_MASTER_AUTO_ASK) { 489 if (!ask_permission("Terminate shared connection to %s? ", 490 host)) { 491 debug2("%s: termination refused by user", __func__); 492 buffer_put_int(r, MUX_S_PERMISSION_DENIED); 493 buffer_put_int(r, rid); 494 buffer_put_cstring(r, "Permission denied"); 495 return 0; 496 } 497 } 498 499 quit_pending = 1; 500 buffer_put_int(r, MUX_S_OK); 501 buffer_put_int(r, rid); 502 /* XXX exit happens too soon - message never makes it to client */ 503 return 0; 504 } 505 506 static char * 507 format_forward(u_int ftype, struct Forward *fwd) 508 { 509 char *ret; 510 511 switch (ftype) { 512 case MUX_FWD_LOCAL: 513 xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d", 514 (fwd->listen_path != NULL) ? fwd->listen_path : 515 (fwd->listen_host == NULL) ? 516 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") : 517 fwd->listen_host, fwd->listen_port, 518 (fwd->connect_path != NULL) ? fwd->connect_path : 519 fwd->connect_host, fwd->connect_port); 520 break; 521 case MUX_FWD_DYNAMIC: 522 xasprintf(&ret, "dynamic forward %.200s:%d -> *", 523 (fwd->listen_host == NULL) ? 524 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") : 525 fwd->listen_host, fwd->listen_port); 526 break; 527 case MUX_FWD_REMOTE: 528 xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d", 529 (fwd->listen_path != NULL) ? fwd->listen_path : 530 (fwd->listen_host == NULL) ? 531 "LOCALHOST" : fwd->listen_host, 532 fwd->listen_port, 533 (fwd->connect_path != NULL) ? fwd->connect_path : 534 fwd->connect_host, fwd->connect_port); 535 break; 536 default: 537 fatal("%s: unknown forward type %u", __func__, ftype); 538 } 539 return ret; 540 } 541 542 static int 543 compare_host(const char *a, const char *b) 544 { 545 if (a == NULL && b == NULL) 546 return 1; 547 if (a == NULL || b == NULL) 548 return 0; 549 return strcmp(a, b) == 0; 550 } 551 552 static int 553 compare_forward(struct Forward *a, struct Forward *b) 554 { 555 if (!compare_host(a->listen_host, b->listen_host)) 556 return 0; 557 if (!compare_host(a->listen_path, b->listen_path)) 558 return 0; 559 if (a->listen_port != b->listen_port) 560 return 0; 561 if (!compare_host(a->connect_host, b->connect_host)) 562 return 0; 563 if (!compare_host(a->connect_path, b->connect_path)) 564 return 0; 565 if (a->connect_port != b->connect_port) 566 return 0; 567 568 return 1; 569 } 570 571 static void 572 mux_confirm_remote_forward(int type, u_int32_t seq, void *ctxt) 573 { 574 struct mux_channel_confirm_ctx *fctx = ctxt; 575 char *failmsg = NULL; 576 struct Forward *rfwd; 577 Channel *c; 578 Buffer out; 579 580 if ((c = channel_by_id(fctx->cid)) == NULL) { 581 /* no channel for reply */ 582 error("%s: unknown channel", __func__); 583 return; 584 } 585 buffer_init(&out); 586 if (fctx->fid >= options.num_remote_forwards || 587 (options.remote_forwards[fctx->fid].connect_path == NULL && 588 options.remote_forwards[fctx->fid].connect_host == NULL)) { 589 xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid); 590 goto fail; 591 } 592 rfwd = &options.remote_forwards[fctx->fid]; 593 debug("%s: %s for: listen %d, connect %s:%d", __func__, 594 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", 595 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path : 596 rfwd->connect_host, rfwd->connect_port); 597 if (type == SSH2_MSG_REQUEST_SUCCESS) { 598 if (rfwd->listen_port == 0) { 599 rfwd->allocated_port = packet_get_int(); 600 debug("Allocated port %u for mux remote forward" 601 " to %s:%d", rfwd->allocated_port, 602 rfwd->connect_host, rfwd->connect_port); 603 buffer_put_int(&out, MUX_S_REMOTE_PORT); 604 buffer_put_int(&out, fctx->rid); 605 buffer_put_int(&out, rfwd->allocated_port); 606 channel_update_permitted_opens(rfwd->handle, 607 rfwd->allocated_port); 608 } else { 609 buffer_put_int(&out, MUX_S_OK); 610 buffer_put_int(&out, fctx->rid); 611 } 612 goto out; 613 } else { 614 if (rfwd->listen_port == 0) 615 channel_update_permitted_opens(rfwd->handle, -1); 616 if (rfwd->listen_path != NULL) 617 xasprintf(&failmsg, "remote port forwarding failed for " 618 "listen path %s", rfwd->listen_path); 619 else 620 xasprintf(&failmsg, "remote port forwarding failed for " 621 "listen port %d", rfwd->listen_port); 622 623 debug2("%s: clearing registered forwarding for listen %d, " 624 "connect %s:%d", __func__, rfwd->listen_port, 625 rfwd->connect_path ? rfwd->connect_path : 626 rfwd->connect_host, rfwd->connect_port); 627 628 free(rfwd->listen_host); 629 free(rfwd->listen_path); 630 free(rfwd->connect_host); 631 free(rfwd->connect_path); 632 memset(rfwd, 0, sizeof(*rfwd)); 633 } 634 fail: 635 error("%s: %s", __func__, failmsg); 636 buffer_put_int(&out, MUX_S_FAILURE); 637 buffer_put_int(&out, fctx->rid); 638 buffer_put_cstring(&out, failmsg); 639 free(failmsg); 640 out: 641 buffer_put_string(&c->output, buffer_ptr(&out), buffer_len(&out)); 642 buffer_free(&out); 643 if (c->mux_pause <= 0) 644 fatal("%s: mux_pause %d", __func__, c->mux_pause); 645 c->mux_pause = 0; /* start processing messages again */ 646 } 647 648 static int 649 process_mux_open_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r) 650 { 651 struct Forward fwd; 652 char *fwd_desc = NULL; 653 char *listen_addr, *connect_addr; 654 u_int ftype; 655 u_int lport, cport; 656 int i, ret = 0, freefwd = 1; 657 658 memset(&fwd, 0, sizeof(fwd)); 659 660 /* XXX - lport/cport check redundant */ 661 if (buffer_get_int_ret(&ftype, m) != 0 || 662 (listen_addr = buffer_get_string_ret(m, NULL)) == NULL || 663 buffer_get_int_ret(&lport, m) != 0 || 664 (connect_addr = buffer_get_string_ret(m, NULL)) == NULL || 665 buffer_get_int_ret(&cport, m) != 0 || 666 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) || 667 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) { 668 error("%s: malformed message", __func__); 669 ret = -1; 670 goto out; 671 } 672 if (*listen_addr == '\0') { 673 free(listen_addr); 674 listen_addr = NULL; 675 } 676 if (*connect_addr == '\0') { 677 free(connect_addr); 678 connect_addr = NULL; 679 } 680 681 memset(&fwd, 0, sizeof(fwd)); 682 fwd.listen_port = lport; 683 if (fwd.listen_port == PORT_STREAMLOCAL) 684 fwd.listen_path = listen_addr; 685 else 686 fwd.listen_host = listen_addr; 687 fwd.connect_port = cport; 688 if (fwd.connect_port == PORT_STREAMLOCAL) 689 fwd.connect_path = connect_addr; 690 else 691 fwd.connect_host = connect_addr; 692 693 debug2("%s: channel %d: request %s", __func__, c->self, 694 (fwd_desc = format_forward(ftype, &fwd))); 695 696 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE && 697 ftype != MUX_FWD_DYNAMIC) { 698 logit("%s: invalid forwarding type %u", __func__, ftype); 699 invalid: 700 free(listen_addr); 701 free(connect_addr); 702 buffer_put_int(r, MUX_S_FAILURE); 703 buffer_put_int(r, rid); 704 buffer_put_cstring(r, "Invalid forwarding request"); 705 return 0; 706 } 707 if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) { 708 logit("%s: streamlocal and dynamic forwards " 709 "are mutually exclusive", __func__); 710 goto invalid; 711 } 712 if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) { 713 logit("%s: invalid listen port %u", __func__, 714 fwd.listen_port); 715 goto invalid; 716 } 717 if ((fwd.connect_port != PORT_STREAMLOCAL && fwd.connect_port >= 65536) 718 || (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE && fwd.connect_port == 0)) { 719 logit("%s: invalid connect port %u", __func__, 720 fwd.connect_port); 721 goto invalid; 722 } 723 if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL && fwd.connect_path == NULL) { 724 logit("%s: missing connect host", __func__); 725 goto invalid; 726 } 727 728 /* Skip forwards that have already been requested */ 729 switch (ftype) { 730 case MUX_FWD_LOCAL: 731 case MUX_FWD_DYNAMIC: 732 for (i = 0; i < options.num_local_forwards; i++) { 733 if (compare_forward(&fwd, 734 options.local_forwards + i)) { 735 exists: 736 debug2("%s: found existing forwarding", 737 __func__); 738 buffer_put_int(r, MUX_S_OK); 739 buffer_put_int(r, rid); 740 goto out; 741 } 742 } 743 break; 744 case MUX_FWD_REMOTE: 745 for (i = 0; i < options.num_remote_forwards; i++) { 746 if (compare_forward(&fwd, 747 options.remote_forwards + i)) { 748 if (fwd.listen_port != 0) 749 goto exists; 750 debug2("%s: found allocated port", 751 __func__); 752 buffer_put_int(r, MUX_S_REMOTE_PORT); 753 buffer_put_int(r, rid); 754 buffer_put_int(r, 755 options.remote_forwards[i].allocated_port); 756 goto out; 757 } 758 } 759 break; 760 } 761 762 if (options.control_master == SSHCTL_MASTER_ASK || 763 options.control_master == SSHCTL_MASTER_AUTO_ASK) { 764 if (!ask_permission("Open %s on %s?", fwd_desc, host)) { 765 debug2("%s: forwarding refused by user", __func__); 766 buffer_put_int(r, MUX_S_PERMISSION_DENIED); 767 buffer_put_int(r, rid); 768 buffer_put_cstring(r, "Permission denied"); 769 goto out; 770 } 771 } 772 773 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) { 774 if (!channel_setup_local_fwd_listener(&fwd, 775 &options.fwd_opts)) { 776 fail: 777 logit("slave-requested %s failed", fwd_desc); 778 buffer_put_int(r, MUX_S_FAILURE); 779 buffer_put_int(r, rid); 780 buffer_put_cstring(r, "Port forwarding failed"); 781 goto out; 782 } 783 add_local_forward(&options, &fwd); 784 freefwd = 0; 785 } else { 786 struct mux_channel_confirm_ctx *fctx; 787 788 fwd.handle = channel_request_remote_forwarding(&fwd); 789 if (fwd.handle < 0) 790 goto fail; 791 add_remote_forward(&options, &fwd); 792 fctx = xcalloc(1, sizeof(*fctx)); 793 fctx->cid = c->self; 794 fctx->rid = rid; 795 fctx->fid = options.num_remote_forwards - 1; 796 client_register_global_confirm(mux_confirm_remote_forward, 797 fctx); 798 freefwd = 0; 799 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */ 800 /* delayed reply in mux_confirm_remote_forward */ 801 goto out; 802 } 803 buffer_put_int(r, MUX_S_OK); 804 buffer_put_int(r, rid); 805 out: 806 free(fwd_desc); 807 if (freefwd) { 808 free(fwd.listen_host); 809 free(fwd.listen_path); 810 free(fwd.connect_host); 811 free(fwd.connect_path); 812 } 813 return ret; 814 } 815 816 static int 817 process_mux_close_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r) 818 { 819 struct Forward fwd, *found_fwd; 820 char *fwd_desc = NULL; 821 const char *error_reason = NULL; 822 char *listen_addr = NULL, *connect_addr = NULL; 823 u_int ftype; 824 int i, ret = 0; 825 u_int lport, cport; 826 827 memset(&fwd, 0, sizeof(fwd)); 828 829 if (buffer_get_int_ret(&ftype, m) != 0 || 830 (listen_addr = buffer_get_string_ret(m, NULL)) == NULL || 831 buffer_get_int_ret(&lport, m) != 0 || 832 (connect_addr = buffer_get_string_ret(m, NULL)) == NULL || 833 buffer_get_int_ret(&cport, m) != 0 || 834 (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) || 835 (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) { 836 error("%s: malformed message", __func__); 837 ret = -1; 838 goto out; 839 } 840 841 if (*listen_addr == '\0') { 842 free(listen_addr); 843 listen_addr = NULL; 844 } 845 if (*connect_addr == '\0') { 846 free(connect_addr); 847 connect_addr = NULL; 848 } 849 850 memset(&fwd, 0, sizeof(fwd)); 851 fwd.listen_port = lport; 852 if (fwd.listen_port == PORT_STREAMLOCAL) 853 fwd.listen_path = listen_addr; 854 else 855 fwd.listen_host = listen_addr; 856 fwd.connect_port = cport; 857 if (fwd.connect_port == PORT_STREAMLOCAL) 858 fwd.connect_path = connect_addr; 859 else 860 fwd.connect_host = connect_addr; 861 862 debug2("%s: channel %d: request cancel %s", __func__, c->self, 863 (fwd_desc = format_forward(ftype, &fwd))); 864 865 /* make sure this has been requested */ 866 found_fwd = NULL; 867 switch (ftype) { 868 case MUX_FWD_LOCAL: 869 case MUX_FWD_DYNAMIC: 870 for (i = 0; i < options.num_local_forwards; i++) { 871 if (compare_forward(&fwd, 872 options.local_forwards + i)) { 873 found_fwd = options.local_forwards + i; 874 break; 875 } 876 } 877 break; 878 case MUX_FWD_REMOTE: 879 for (i = 0; i < options.num_remote_forwards; i++) { 880 if (compare_forward(&fwd, 881 options.remote_forwards + i)) { 882 found_fwd = options.remote_forwards + i; 883 break; 884 } 885 } 886 break; 887 } 888 889 if (found_fwd == NULL) 890 error_reason = "port not forwarded"; 891 else if (ftype == MUX_FWD_REMOTE) { 892 /* 893 * This shouldn't fail unless we confused the host/port 894 * between options.remote_forwards and permitted_opens. 895 * However, for dynamic allocated listen ports we need 896 * to use the actual listen port. 897 */ 898 if (channel_request_rforward_cancel(found_fwd) == -1) 899 error_reason = "port not in permitted opens"; 900 } else { /* local and dynamic forwards */ 901 /* Ditto */ 902 if (channel_cancel_lport_listener(&fwd, fwd.connect_port, 903 &options.fwd_opts) == -1) 904 error_reason = "port not found"; 905 } 906 907 if (error_reason == NULL) { 908 buffer_put_int(r, MUX_S_OK); 909 buffer_put_int(r, rid); 910 911 free(found_fwd->listen_host); 912 free(found_fwd->listen_path); 913 free(found_fwd->connect_host); 914 free(found_fwd->connect_path); 915 found_fwd->listen_host = found_fwd->connect_host = NULL; 916 found_fwd->listen_path = found_fwd->connect_path = NULL; 917 found_fwd->listen_port = found_fwd->connect_port = 0; 918 } else { 919 buffer_put_int(r, MUX_S_FAILURE); 920 buffer_put_int(r, rid); 921 buffer_put_cstring(r, error_reason); 922 } 923 out: 924 free(fwd_desc); 925 free(listen_addr); 926 free(connect_addr); 927 928 return ret; 929 } 930 931 static int 932 process_mux_stdio_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r) 933 { 934 Channel *nc; 935 char *reserved, *chost; 936 u_int cport, i, j; 937 int new_fd[2]; 938 struct mux_stdio_confirm_ctx *cctx; 939 940 chost = reserved = NULL; 941 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL || 942 (chost = buffer_get_string_ret(m, NULL)) == NULL || 943 buffer_get_int_ret(&cport, m) != 0) { 944 free(reserved); 945 free(chost); 946 error("%s: malformed message", __func__); 947 return -1; 948 } 949 free(reserved); 950 951 debug2("%s: channel %d: request stdio fwd to %s:%u", 952 __func__, c->self, chost, cport); 953 954 /* Gather fds from client */ 955 for(i = 0; i < 2; i++) { 956 if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) { 957 error("%s: failed to receive fd %d from slave", 958 __func__, i); 959 for (j = 0; j < i; j++) 960 close(new_fd[j]); 961 free(chost); 962 963 /* prepare reply */ 964 buffer_put_int(r, MUX_S_FAILURE); 965 buffer_put_int(r, rid); 966 buffer_put_cstring(r, 967 "did not receive file descriptors"); 968 return -1; 969 } 970 } 971 972 debug3("%s: got fds stdin %d, stdout %d", __func__, 973 new_fd[0], new_fd[1]); 974 975 /* XXX support multiple child sessions in future */ 976 if (c->remote_id != -1) { 977 debug2("%s: session already open", __func__); 978 /* prepare reply */ 979 buffer_put_int(r, MUX_S_FAILURE); 980 buffer_put_int(r, rid); 981 buffer_put_cstring(r, "Multiple sessions not supported"); 982 cleanup: 983 close(new_fd[0]); 984 close(new_fd[1]); 985 free(chost); 986 return 0; 987 } 988 989 if (options.control_master == SSHCTL_MASTER_ASK || 990 options.control_master == SSHCTL_MASTER_AUTO_ASK) { 991 if (!ask_permission("Allow forward to %s:%u? ", 992 chost, cport)) { 993 debug2("%s: stdio fwd refused by user", __func__); 994 /* prepare reply */ 995 buffer_put_int(r, MUX_S_PERMISSION_DENIED); 996 buffer_put_int(r, rid); 997 buffer_put_cstring(r, "Permission denied"); 998 goto cleanup; 999 } 1000 } 1001 1002 /* enable nonblocking unless tty */ 1003 if (!isatty(new_fd[0])) 1004 set_nonblock(new_fd[0]); 1005 if (!isatty(new_fd[1])) 1006 set_nonblock(new_fd[1]); 1007 1008 nc = channel_connect_stdio_fwd(chost, cport, new_fd[0], new_fd[1]); 1009 1010 nc->ctl_chan = c->self; /* link session -> control channel */ 1011 c->remote_id = nc->self; /* link control -> session channel */ 1012 1013 debug2("%s: channel_new: %d linked to control channel %d", 1014 __func__, nc->self, nc->ctl_chan); 1015 1016 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1); 1017 1018 cctx = xcalloc(1, sizeof(*cctx)); 1019 cctx->rid = rid; 1020 channel_register_open_confirm(nc->self, mux_stdio_confirm, cctx); 1021 c->mux_pause = 1; /* stop handling messages until open_confirm done */ 1022 1023 /* reply is deferred, sent by mux_session_confirm */ 1024 return 0; 1025 } 1026 1027 /* Callback on open confirmation in mux master for a mux stdio fwd session. */ 1028 static void 1029 mux_stdio_confirm(int id, int success, void *arg) 1030 { 1031 struct mux_stdio_confirm_ctx *cctx = arg; 1032 Channel *c, *cc; 1033 Buffer reply; 1034 1035 if (cctx == NULL) 1036 fatal("%s: cctx == NULL", __func__); 1037 if ((c = channel_by_id(id)) == NULL) 1038 fatal("%s: no channel for id %d", __func__, id); 1039 if ((cc = channel_by_id(c->ctl_chan)) == NULL) 1040 fatal("%s: channel %d lacks control channel %d", __func__, 1041 id, c->ctl_chan); 1042 1043 if (!success) { 1044 debug3("%s: sending failure reply", __func__); 1045 /* prepare reply */ 1046 buffer_init(&reply); 1047 buffer_put_int(&reply, MUX_S_FAILURE); 1048 buffer_put_int(&reply, cctx->rid); 1049 buffer_put_cstring(&reply, "Session open refused by peer"); 1050 goto done; 1051 } 1052 1053 debug3("%s: sending success reply", __func__); 1054 /* prepare reply */ 1055 buffer_init(&reply); 1056 buffer_put_int(&reply, MUX_S_SESSION_OPENED); 1057 buffer_put_int(&reply, cctx->rid); 1058 buffer_put_int(&reply, c->self); 1059 1060 done: 1061 /* Send reply */ 1062 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply)); 1063 buffer_free(&reply); 1064 1065 if (cc->mux_pause <= 0) 1066 fatal("%s: mux_pause %d", __func__, cc->mux_pause); 1067 cc->mux_pause = 0; /* start processing messages again */ 1068 c->open_confirm_ctx = NULL; 1069 free(cctx); 1070 } 1071 1072 static int 1073 process_mux_stop_listening(u_int rid, Channel *c, Buffer *m, Buffer *r) 1074 { 1075 debug("%s: channel %d: stop listening", __func__, c->self); 1076 1077 if (options.control_master == SSHCTL_MASTER_ASK || 1078 options.control_master == SSHCTL_MASTER_AUTO_ASK) { 1079 if (!ask_permission("Disable further multiplexing on shared " 1080 "connection to %s? ", host)) { 1081 debug2("%s: stop listen refused by user", __func__); 1082 buffer_put_int(r, MUX_S_PERMISSION_DENIED); 1083 buffer_put_int(r, rid); 1084 buffer_put_cstring(r, "Permission denied"); 1085 return 0; 1086 } 1087 } 1088 1089 if (mux_listener_channel != NULL) { 1090 channel_free(mux_listener_channel); 1091 client_stop_mux(); 1092 free(options.control_path); 1093 options.control_path = NULL; 1094 mux_listener_channel = NULL; 1095 muxserver_sock = -1; 1096 } 1097 1098 /* prepare reply */ 1099 buffer_put_int(r, MUX_S_OK); 1100 buffer_put_int(r, rid); 1101 1102 return 0; 1103 } 1104 1105 /* Channel callbacks fired on read/write from mux slave fd */ 1106 static int 1107 mux_master_read_cb(Channel *c) 1108 { 1109 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx; 1110 Buffer in, out; 1111 const u_char *ptr; 1112 u_int type, rid, have, i; 1113 int ret = -1; 1114 1115 /* Setup ctx and */ 1116 if (c->mux_ctx == NULL) { 1117 state = xcalloc(1, sizeof(*state)); 1118 c->mux_ctx = state; 1119 channel_register_cleanup(c->self, 1120 mux_master_control_cleanup_cb, 0); 1121 1122 /* Send hello */ 1123 buffer_init(&out); 1124 buffer_put_int(&out, MUX_MSG_HELLO); 1125 buffer_put_int(&out, SSHMUX_VER); 1126 /* no extensions */ 1127 buffer_put_string(&c->output, buffer_ptr(&out), 1128 buffer_len(&out)); 1129 buffer_free(&out); 1130 debug3("%s: channel %d: hello sent", __func__, c->self); 1131 return 0; 1132 } 1133 1134 buffer_init(&in); 1135 buffer_init(&out); 1136 1137 /* Channel code ensures that we receive whole packets */ 1138 if ((ptr = buffer_get_string_ptr_ret(&c->input, &have)) == NULL) { 1139 malf: 1140 error("%s: malformed message", __func__); 1141 goto out; 1142 } 1143 buffer_append(&in, ptr, have); 1144 1145 if (buffer_get_int_ret(&type, &in) != 0) 1146 goto malf; 1147 debug3("%s: channel %d packet type 0x%08x len %u", 1148 __func__, c->self, type, buffer_len(&in)); 1149 1150 if (type == MUX_MSG_HELLO) 1151 rid = 0; 1152 else { 1153 if (!state->hello_rcvd) { 1154 error("%s: expected MUX_MSG_HELLO(0x%08x), " 1155 "received 0x%08x", __func__, MUX_MSG_HELLO, type); 1156 goto out; 1157 } 1158 if (buffer_get_int_ret(&rid, &in) != 0) 1159 goto malf; 1160 } 1161 1162 for (i = 0; mux_master_handlers[i].handler != NULL; i++) { 1163 if (type == mux_master_handlers[i].type) { 1164 ret = mux_master_handlers[i].handler(rid, c, &in, &out); 1165 break; 1166 } 1167 } 1168 if (mux_master_handlers[i].handler == NULL) { 1169 error("%s: unsupported mux message 0x%08x", __func__, type); 1170 buffer_put_int(&out, MUX_S_FAILURE); 1171 buffer_put_int(&out, rid); 1172 buffer_put_cstring(&out, "unsupported request"); 1173 ret = 0; 1174 } 1175 /* Enqueue reply packet */ 1176 if (buffer_len(&out) != 0) { 1177 buffer_put_string(&c->output, buffer_ptr(&out), 1178 buffer_len(&out)); 1179 } 1180 out: 1181 buffer_free(&in); 1182 buffer_free(&out); 1183 return ret; 1184 } 1185 1186 void 1187 mux_exit_message(Channel *c, int exitval) 1188 { 1189 Buffer m; 1190 Channel *mux_chan; 1191 1192 debug3("%s: channel %d: exit message, exitval %d", __func__, c->self, 1193 exitval); 1194 1195 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL) 1196 fatal("%s: channel %d missing mux channel %d", 1197 __func__, c->self, c->ctl_chan); 1198 1199 /* Append exit message packet to control socket output queue */ 1200 buffer_init(&m); 1201 buffer_put_int(&m, MUX_S_EXIT_MESSAGE); 1202 buffer_put_int(&m, c->self); 1203 buffer_put_int(&m, exitval); 1204 1205 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m)); 1206 buffer_free(&m); 1207 } 1208 1209 void 1210 mux_tty_alloc_failed(Channel *c) 1211 { 1212 Buffer m; 1213 Channel *mux_chan; 1214 1215 debug3("%s: channel %d: TTY alloc failed", __func__, c->self); 1216 1217 if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL) 1218 fatal("%s: channel %d missing mux channel %d", 1219 __func__, c->self, c->ctl_chan); 1220 1221 /* Append exit message packet to control socket output queue */ 1222 buffer_init(&m); 1223 buffer_put_int(&m, MUX_S_TTY_ALLOC_FAIL); 1224 buffer_put_int(&m, c->self); 1225 1226 buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m)); 1227 buffer_free(&m); 1228 } 1229 1230 /* Prepare a mux master to listen on a Unix domain socket. */ 1231 void 1232 muxserver_listen(void) 1233 { 1234 mode_t old_umask; 1235 char *orig_control_path = options.control_path; 1236 char rbuf[16+1]; 1237 u_int i, r; 1238 int oerrno; 1239 1240 if (options.control_path == NULL || 1241 options.control_master == SSHCTL_MASTER_NO) 1242 return; 1243 1244 debug("setting up multiplex master socket"); 1245 1246 /* 1247 * Use a temporary path before listen so we can pseudo-atomically 1248 * establish the listening socket in its final location to avoid 1249 * other processes racing in between bind() and listen() and hitting 1250 * an unready socket. 1251 */ 1252 for (i = 0; i < sizeof(rbuf) - 1; i++) { 1253 r = arc4random_uniform(26+26+10); 1254 rbuf[i] = (r < 26) ? 'a' + r : 1255 (r < 26*2) ? 'A' + r - 26 : 1256 '0' + r - 26 - 26; 1257 } 1258 rbuf[sizeof(rbuf) - 1] = '\0'; 1259 options.control_path = NULL; 1260 xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf); 1261 debug3("%s: temporary control path %s", __func__, options.control_path); 1262 1263 old_umask = umask(0177); 1264 muxserver_sock = unix_listener(options.control_path, 64, 0); 1265 oerrno = errno; 1266 umask(old_umask); 1267 if (muxserver_sock < 0) { 1268 if (oerrno == EINVAL || oerrno == EADDRINUSE) { 1269 error("ControlSocket %s already exists, " 1270 "disabling multiplexing", options.control_path); 1271 disable_mux_master: 1272 if (muxserver_sock != -1) { 1273 close(muxserver_sock); 1274 muxserver_sock = -1; 1275 } 1276 free(orig_control_path); 1277 free(options.control_path); 1278 options.control_path = NULL; 1279 options.control_master = SSHCTL_MASTER_NO; 1280 return; 1281 } else { 1282 /* unix_listener() logs the error */ 1283 cleanup_exit(255); 1284 } 1285 } 1286 1287 /* Now atomically "move" the mux socket into position */ 1288 if (link(options.control_path, orig_control_path) != 0) { 1289 if (errno != EEXIST) { 1290 fatal("%s: link mux listener %s => %s: %s", __func__, 1291 options.control_path, orig_control_path, 1292 strerror(errno)); 1293 } 1294 error("ControlSocket %s already exists, disabling multiplexing", 1295 orig_control_path); 1296 unlink(options.control_path); 1297 goto disable_mux_master; 1298 } 1299 unlink(options.control_path); 1300 free(options.control_path); 1301 options.control_path = orig_control_path; 1302 1303 set_nonblock(muxserver_sock); 1304 1305 mux_listener_channel = channel_new("mux listener", 1306 SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1, 1307 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 1308 0, options.control_path, 1); 1309 mux_listener_channel->mux_rcb = mux_master_read_cb; 1310 debug3("%s: mux listener channel %d fd %d", __func__, 1311 mux_listener_channel->self, mux_listener_channel->sock); 1312 } 1313 1314 /* Callback on open confirmation in mux master for a mux client session. */ 1315 static void 1316 mux_session_confirm(int id, int success, void *arg) 1317 { 1318 struct mux_session_confirm_ctx *cctx = arg; 1319 const char *display; 1320 Channel *c, *cc; 1321 int i; 1322 Buffer reply; 1323 1324 if (cctx == NULL) 1325 fatal("%s: cctx == NULL", __func__); 1326 if ((c = channel_by_id(id)) == NULL) 1327 fatal("%s: no channel for id %d", __func__, id); 1328 if ((cc = channel_by_id(c->ctl_chan)) == NULL) 1329 fatal("%s: channel %d lacks control channel %d", __func__, 1330 id, c->ctl_chan); 1331 1332 if (!success) { 1333 debug3("%s: sending failure reply", __func__); 1334 /* prepare reply */ 1335 buffer_init(&reply); 1336 buffer_put_int(&reply, MUX_S_FAILURE); 1337 buffer_put_int(&reply, cctx->rid); 1338 buffer_put_cstring(&reply, "Session open refused by peer"); 1339 goto done; 1340 } 1341 1342 display = getenv("DISPLAY"); 1343 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) { 1344 char *proto, *data; 1345 1346 /* Get reasonable local authentication information. */ 1347 client_x11_get_proto(display, options.xauth_location, 1348 options.forward_x11_trusted, options.forward_x11_timeout, 1349 &proto, &data); 1350 /* Request forwarding with authentication spoofing. */ 1351 debug("Requesting X11 forwarding with authentication " 1352 "spoofing."); 1353 x11_request_forwarding_with_spoofing(id, display, proto, 1354 data, 1); 1355 client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN); 1356 /* XXX exit_on_forward_failure */ 1357 } 1358 1359 if (cctx->want_agent_fwd && options.forward_agent) { 1360 debug("Requesting authentication agent forwarding."); 1361 channel_request_start(id, "auth-agent-req@openssh.com", 0); 1362 packet_send(); 1363 } 1364 1365 client_session2_setup(id, cctx->want_tty, cctx->want_subsys, 1366 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env); 1367 1368 debug3("%s: sending success reply", __func__); 1369 /* prepare reply */ 1370 buffer_init(&reply); 1371 buffer_put_int(&reply, MUX_S_SESSION_OPENED); 1372 buffer_put_int(&reply, cctx->rid); 1373 buffer_put_int(&reply, c->self); 1374 1375 done: 1376 /* Send reply */ 1377 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply)); 1378 buffer_free(&reply); 1379 1380 if (cc->mux_pause <= 0) 1381 fatal("%s: mux_pause %d", __func__, cc->mux_pause); 1382 cc->mux_pause = 0; /* start processing messages again */ 1383 c->open_confirm_ctx = NULL; 1384 buffer_free(&cctx->cmd); 1385 free(cctx->term); 1386 if (cctx->env != NULL) { 1387 for (i = 0; cctx->env[i] != NULL; i++) 1388 free(cctx->env[i]); 1389 free(cctx->env); 1390 } 1391 free(cctx); 1392 } 1393 1394 /* ** Multiplexing client support */ 1395 1396 /* Exit signal handler */ 1397 static void 1398 control_client_sighandler(int signo) 1399 { 1400 muxclient_terminate = signo; 1401 } 1402 1403 /* 1404 * Relay signal handler - used to pass some signals from mux client to 1405 * mux master. 1406 */ 1407 static void 1408 control_client_sigrelay(int signo) 1409 { 1410 int save_errno = errno; 1411 1412 if (muxserver_pid > 1) 1413 kill(muxserver_pid, signo); 1414 1415 errno = save_errno; 1416 } 1417 1418 static int 1419 mux_client_read(int fd, Buffer *b, u_int need) 1420 { 1421 u_int have; 1422 ssize_t len; 1423 u_char *p; 1424 struct pollfd pfd; 1425 1426 pfd.fd = fd; 1427 pfd.events = POLLIN; 1428 p = buffer_append_space(b, need); 1429 for (have = 0; have < need; ) { 1430 if (muxclient_terminate) { 1431 errno = EINTR; 1432 return -1; 1433 } 1434 len = read(fd, p + have, need - have); 1435 if (len < 0) { 1436 switch (errno) { 1437 case EAGAIN: 1438 (void)poll(&pfd, 1, -1); 1439 /* FALLTHROUGH */ 1440 case EINTR: 1441 continue; 1442 default: 1443 return -1; 1444 } 1445 } 1446 if (len == 0) { 1447 errno = EPIPE; 1448 return -1; 1449 } 1450 have += (u_int)len; 1451 } 1452 return 0; 1453 } 1454 1455 static int 1456 mux_client_write_packet(int fd, Buffer *m) 1457 { 1458 Buffer queue; 1459 u_int have, need; 1460 int oerrno, len; 1461 u_char *ptr; 1462 struct pollfd pfd; 1463 1464 pfd.fd = fd; 1465 pfd.events = POLLOUT; 1466 buffer_init(&queue); 1467 buffer_put_string(&queue, buffer_ptr(m), buffer_len(m)); 1468 1469 need = buffer_len(&queue); 1470 ptr = buffer_ptr(&queue); 1471 1472 for (have = 0; have < need; ) { 1473 if (muxclient_terminate) { 1474 buffer_free(&queue); 1475 errno = EINTR; 1476 return -1; 1477 } 1478 len = write(fd, ptr + have, need - have); 1479 if (len < 0) { 1480 switch (errno) { 1481 case EAGAIN: 1482 (void)poll(&pfd, 1, -1); 1483 /* FALLTHROUGH */ 1484 case EINTR: 1485 continue; 1486 default: 1487 oerrno = errno; 1488 buffer_free(&queue); 1489 errno = oerrno; 1490 return -1; 1491 } 1492 } 1493 if (len == 0) { 1494 buffer_free(&queue); 1495 errno = EPIPE; 1496 return -1; 1497 } 1498 have += (u_int)len; 1499 } 1500 buffer_free(&queue); 1501 return 0; 1502 } 1503 1504 static int 1505 mux_client_read_packet(int fd, Buffer *m) 1506 { 1507 Buffer queue; 1508 u_int need, have; 1509 const u_char *ptr; 1510 int oerrno; 1511 1512 buffer_init(&queue); 1513 if (mux_client_read(fd, &queue, 4) != 0) { 1514 if ((oerrno = errno) == EPIPE) 1515 debug3("%s: read header failed: %s", __func__, 1516 strerror(errno)); 1517 buffer_free(&queue); 1518 errno = oerrno; 1519 return -1; 1520 } 1521 need = get_u32(buffer_ptr(&queue)); 1522 if (mux_client_read(fd, &queue, need) != 0) { 1523 oerrno = errno; 1524 debug3("%s: read body failed: %s", __func__, strerror(errno)); 1525 buffer_free(&queue); 1526 errno = oerrno; 1527 return -1; 1528 } 1529 ptr = buffer_get_string_ptr(&queue, &have); 1530 buffer_append(m, ptr, have); 1531 buffer_free(&queue); 1532 return 0; 1533 } 1534 1535 static int 1536 mux_client_hello_exchange(int fd) 1537 { 1538 Buffer m; 1539 u_int type, ver; 1540 1541 buffer_init(&m); 1542 buffer_put_int(&m, MUX_MSG_HELLO); 1543 buffer_put_int(&m, SSHMUX_VER); 1544 /* no extensions */ 1545 1546 if (mux_client_write_packet(fd, &m) != 0) 1547 fatal("%s: write packet: %s", __func__, strerror(errno)); 1548 1549 buffer_clear(&m); 1550 1551 /* Read their HELLO */ 1552 if (mux_client_read_packet(fd, &m) != 0) { 1553 buffer_free(&m); 1554 return -1; 1555 } 1556 1557 type = buffer_get_int(&m); 1558 if (type != MUX_MSG_HELLO) 1559 fatal("%s: expected HELLO (%u) received %u", 1560 __func__, MUX_MSG_HELLO, type); 1561 ver = buffer_get_int(&m); 1562 if (ver != SSHMUX_VER) 1563 fatal("Unsupported multiplexing protocol version %d " 1564 "(expected %d)", ver, SSHMUX_VER); 1565 debug2("%s: master version %u", __func__, ver); 1566 /* No extensions are presently defined */ 1567 while (buffer_len(&m) > 0) { 1568 char *name = buffer_get_string(&m, NULL); 1569 char *value = buffer_get_string(&m, NULL); 1570 1571 debug2("Unrecognised master extension \"%s\"", name); 1572 free(name); 1573 free(value); 1574 } 1575 buffer_free(&m); 1576 return 0; 1577 } 1578 1579 static u_int 1580 mux_client_request_alive(int fd) 1581 { 1582 Buffer m; 1583 char *e; 1584 u_int pid, type, rid; 1585 1586 debug3("%s: entering", __func__); 1587 1588 buffer_init(&m); 1589 buffer_put_int(&m, MUX_C_ALIVE_CHECK); 1590 buffer_put_int(&m, muxclient_request_id); 1591 1592 if (mux_client_write_packet(fd, &m) != 0) 1593 fatal("%s: write packet: %s", __func__, strerror(errno)); 1594 1595 buffer_clear(&m); 1596 1597 /* Read their reply */ 1598 if (mux_client_read_packet(fd, &m) != 0) { 1599 buffer_free(&m); 1600 return 0; 1601 } 1602 1603 type = buffer_get_int(&m); 1604 if (type != MUX_S_ALIVE) { 1605 e = buffer_get_string(&m, NULL); 1606 fatal("%s: master returned error: %s", __func__, e); 1607 } 1608 1609 if ((rid = buffer_get_int(&m)) != muxclient_request_id) 1610 fatal("%s: out of sequence reply: my id %u theirs %u", 1611 __func__, muxclient_request_id, rid); 1612 pid = buffer_get_int(&m); 1613 buffer_free(&m); 1614 1615 debug3("%s: done pid = %u", __func__, pid); 1616 1617 muxclient_request_id++; 1618 1619 return pid; 1620 } 1621 1622 static void 1623 mux_client_request_terminate(int fd) 1624 { 1625 Buffer m; 1626 char *e; 1627 u_int type, rid; 1628 1629 debug3("%s: entering", __func__); 1630 1631 buffer_init(&m); 1632 buffer_put_int(&m, MUX_C_TERMINATE); 1633 buffer_put_int(&m, muxclient_request_id); 1634 1635 if (mux_client_write_packet(fd, &m) != 0) 1636 fatal("%s: write packet: %s", __func__, strerror(errno)); 1637 1638 buffer_clear(&m); 1639 1640 /* Read their reply */ 1641 if (mux_client_read_packet(fd, &m) != 0) { 1642 /* Remote end exited already */ 1643 if (errno == EPIPE) { 1644 buffer_free(&m); 1645 return; 1646 } 1647 fatal("%s: read from master failed: %s", 1648 __func__, strerror(errno)); 1649 } 1650 1651 type = buffer_get_int(&m); 1652 if ((rid = buffer_get_int(&m)) != muxclient_request_id) 1653 fatal("%s: out of sequence reply: my id %u theirs %u", 1654 __func__, muxclient_request_id, rid); 1655 switch (type) { 1656 case MUX_S_OK: 1657 break; 1658 case MUX_S_PERMISSION_DENIED: 1659 e = buffer_get_string(&m, NULL); 1660 fatal("Master refused termination request: %s", e); 1661 case MUX_S_FAILURE: 1662 e = buffer_get_string(&m, NULL); 1663 fatal("%s: termination request failed: %s", __func__, e); 1664 default: 1665 fatal("%s: unexpected response from master 0x%08x", 1666 __func__, type); 1667 } 1668 buffer_free(&m); 1669 muxclient_request_id++; 1670 } 1671 1672 static int 1673 mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd) 1674 { 1675 Buffer m; 1676 char *e, *fwd_desc; 1677 u_int type, rid; 1678 1679 fwd_desc = format_forward(ftype, fwd); 1680 debug("Requesting %s %s", 1681 cancel_flag ? "cancellation of" : "forwarding of", fwd_desc); 1682 free(fwd_desc); 1683 1684 buffer_init(&m); 1685 buffer_put_int(&m, cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD); 1686 buffer_put_int(&m, muxclient_request_id); 1687 buffer_put_int(&m, ftype); 1688 if (fwd->listen_path != NULL) { 1689 buffer_put_cstring(&m, fwd->listen_path); 1690 } else { 1691 buffer_put_cstring(&m, 1692 fwd->listen_host == NULL ? "" : 1693 (*fwd->listen_host == '\0' ? "*" : fwd->listen_host)); 1694 } 1695 buffer_put_int(&m, fwd->listen_port); 1696 if (fwd->connect_path != NULL) { 1697 buffer_put_cstring(&m, fwd->connect_path); 1698 } else { 1699 buffer_put_cstring(&m, 1700 fwd->connect_host == NULL ? "" : fwd->connect_host); 1701 } 1702 buffer_put_int(&m, fwd->connect_port); 1703 1704 if (mux_client_write_packet(fd, &m) != 0) 1705 fatal("%s: write packet: %s", __func__, strerror(errno)); 1706 1707 buffer_clear(&m); 1708 1709 /* Read their reply */ 1710 if (mux_client_read_packet(fd, &m) != 0) { 1711 buffer_free(&m); 1712 return -1; 1713 } 1714 1715 type = buffer_get_int(&m); 1716 if ((rid = buffer_get_int(&m)) != muxclient_request_id) 1717 fatal("%s: out of sequence reply: my id %u theirs %u", 1718 __func__, muxclient_request_id, rid); 1719 switch (type) { 1720 case MUX_S_OK: 1721 break; 1722 case MUX_S_REMOTE_PORT: 1723 if (cancel_flag) 1724 fatal("%s: got MUX_S_REMOTE_PORT for cancel", __func__); 1725 fwd->allocated_port = buffer_get_int(&m); 1726 verbose("Allocated port %u for remote forward to %s:%d", 1727 fwd->allocated_port, 1728 fwd->connect_host ? fwd->connect_host : "", 1729 fwd->connect_port); 1730 if (muxclient_command == SSHMUX_COMMAND_FORWARD) 1731 fprintf(stdout, "%u\n", fwd->allocated_port); 1732 break; 1733 case MUX_S_PERMISSION_DENIED: 1734 e = buffer_get_string(&m, NULL); 1735 buffer_free(&m); 1736 error("Master refused forwarding request: %s", e); 1737 return -1; 1738 case MUX_S_FAILURE: 1739 e = buffer_get_string(&m, NULL); 1740 buffer_free(&m); 1741 error("%s: forwarding request failed: %s", __func__, e); 1742 return -1; 1743 default: 1744 fatal("%s: unexpected response from master 0x%08x", 1745 __func__, type); 1746 } 1747 buffer_free(&m); 1748 1749 muxclient_request_id++; 1750 return 0; 1751 } 1752 1753 static int 1754 mux_client_forwards(int fd, int cancel_flag) 1755 { 1756 int i, ret = 0; 1757 1758 debug3("%s: %s forwardings: %d local, %d remote", __func__, 1759 cancel_flag ? "cancel" : "request", 1760 options.num_local_forwards, options.num_remote_forwards); 1761 1762 /* XXX ExitOnForwardingFailure */ 1763 for (i = 0; i < options.num_local_forwards; i++) { 1764 if (mux_client_forward(fd, cancel_flag, 1765 options.local_forwards[i].connect_port == 0 ? 1766 MUX_FWD_DYNAMIC : MUX_FWD_LOCAL, 1767 options.local_forwards + i) != 0) 1768 ret = -1; 1769 } 1770 for (i = 0; i < options.num_remote_forwards; i++) { 1771 if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE, 1772 options.remote_forwards + i) != 0) 1773 ret = -1; 1774 } 1775 return ret; 1776 } 1777 1778 static int 1779 mux_client_request_session(int fd) 1780 { 1781 Buffer m; 1782 char *e, *term; 1783 u_int i, rid, sid, esid, exitval, type, exitval_seen; 1784 extern char **environ; 1785 int devnull, rawmode; 1786 1787 debug3("%s: entering", __func__); 1788 1789 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) { 1790 error("%s: master alive request failed", __func__); 1791 return -1; 1792 } 1793 1794 signal(SIGPIPE, SIG_IGN); 1795 1796 if (stdin_null_flag) { 1797 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1) 1798 fatal("open(/dev/null): %s", strerror(errno)); 1799 if (dup2(devnull, STDIN_FILENO) == -1) 1800 fatal("dup2: %s", strerror(errno)); 1801 if (devnull > STDERR_FILENO) 1802 close(devnull); 1803 } 1804 1805 term = getenv("TERM"); 1806 1807 buffer_init(&m); 1808 buffer_put_int(&m, MUX_C_NEW_SESSION); 1809 buffer_put_int(&m, muxclient_request_id); 1810 buffer_put_cstring(&m, ""); /* reserved */ 1811 buffer_put_int(&m, tty_flag); 1812 buffer_put_int(&m, options.forward_x11); 1813 buffer_put_int(&m, options.forward_agent); 1814 buffer_put_int(&m, subsystem_flag); 1815 buffer_put_int(&m, options.escape_char == SSH_ESCAPECHAR_NONE ? 1816 0xffffffff : (u_int)options.escape_char); 1817 buffer_put_cstring(&m, term == NULL ? "" : term); 1818 buffer_put_string(&m, buffer_ptr(&command), buffer_len(&command)); 1819 1820 if (options.num_send_env > 0 && environ != NULL) { 1821 /* Pass environment */ 1822 for (i = 0; environ[i] != NULL; i++) { 1823 if (env_permitted(environ[i])) { 1824 buffer_put_cstring(&m, environ[i]); 1825 } 1826 } 1827 } 1828 1829 if (mux_client_write_packet(fd, &m) != 0) 1830 fatal("%s: write packet: %s", __func__, strerror(errno)); 1831 1832 /* Send the stdio file descriptors */ 1833 if (mm_send_fd(fd, STDIN_FILENO) == -1 || 1834 mm_send_fd(fd, STDOUT_FILENO) == -1 || 1835 mm_send_fd(fd, STDERR_FILENO) == -1) 1836 fatal("%s: send fds failed", __func__); 1837 1838 debug3("%s: session request sent", __func__); 1839 1840 /* Read their reply */ 1841 buffer_clear(&m); 1842 if (mux_client_read_packet(fd, &m) != 0) { 1843 error("%s: read from master failed: %s", 1844 __func__, strerror(errno)); 1845 buffer_free(&m); 1846 return -1; 1847 } 1848 1849 type = buffer_get_int(&m); 1850 if ((rid = buffer_get_int(&m)) != muxclient_request_id) 1851 fatal("%s: out of sequence reply: my id %u theirs %u", 1852 __func__, muxclient_request_id, rid); 1853 switch (type) { 1854 case MUX_S_SESSION_OPENED: 1855 sid = buffer_get_int(&m); 1856 debug("%s: master session id: %u", __func__, sid); 1857 break; 1858 case MUX_S_PERMISSION_DENIED: 1859 e = buffer_get_string(&m, NULL); 1860 buffer_free(&m); 1861 error("Master refused session request: %s", e); 1862 return -1; 1863 case MUX_S_FAILURE: 1864 e = buffer_get_string(&m, NULL); 1865 buffer_free(&m); 1866 error("%s: session request failed: %s", __func__, e); 1867 return -1; 1868 default: 1869 buffer_free(&m); 1870 error("%s: unexpected response from master 0x%08x", 1871 __func__, type); 1872 return -1; 1873 } 1874 muxclient_request_id++; 1875 1876 signal(SIGHUP, control_client_sighandler); 1877 signal(SIGINT, control_client_sighandler); 1878 signal(SIGTERM, control_client_sighandler); 1879 signal(SIGWINCH, control_client_sigrelay); 1880 1881 rawmode = tty_flag; 1882 if (tty_flag) 1883 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 1884 1885 /* 1886 * Stick around until the controlee closes the client_fd. 1887 * Before it does, it is expected to write an exit message. 1888 * This process must read the value and wait for the closure of 1889 * the client_fd; if this one closes early, the multiplex master will 1890 * terminate early too (possibly losing data). 1891 */ 1892 for (exitval = 255, exitval_seen = 0;;) { 1893 buffer_clear(&m); 1894 if (mux_client_read_packet(fd, &m) != 0) 1895 break; 1896 type = buffer_get_int(&m); 1897 switch (type) { 1898 case MUX_S_TTY_ALLOC_FAIL: 1899 if ((esid = buffer_get_int(&m)) != sid) 1900 fatal("%s: tty alloc fail on unknown session: " 1901 "my id %u theirs %u", 1902 __func__, sid, esid); 1903 leave_raw_mode(options.request_tty == 1904 REQUEST_TTY_FORCE); 1905 rawmode = 0; 1906 continue; 1907 case MUX_S_EXIT_MESSAGE: 1908 if ((esid = buffer_get_int(&m)) != sid) 1909 fatal("%s: exit on unknown session: " 1910 "my id %u theirs %u", 1911 __func__, sid, esid); 1912 if (exitval_seen) 1913 fatal("%s: exitval sent twice", __func__); 1914 exitval = buffer_get_int(&m); 1915 exitval_seen = 1; 1916 continue; 1917 default: 1918 e = buffer_get_string(&m, NULL); 1919 fatal("%s: master returned error: %s", __func__, e); 1920 } 1921 } 1922 1923 close(fd); 1924 if (rawmode) 1925 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 1926 1927 if (muxclient_terminate) { 1928 debug2("Exiting on signal %ld", (long)muxclient_terminate); 1929 exitval = 255; 1930 } else if (!exitval_seen) { 1931 debug2("Control master terminated unexpectedly"); 1932 exitval = 255; 1933 } else 1934 debug2("Received exit status from master %d", exitval); 1935 1936 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET) 1937 fprintf(stderr, "Shared connection to %s closed.\r\n", host); 1938 1939 exit(exitval); 1940 } 1941 1942 static int 1943 mux_client_request_stdio_fwd(int fd) 1944 { 1945 Buffer m; 1946 char *e; 1947 u_int type, rid, sid; 1948 int devnull; 1949 1950 debug3("%s: entering", __func__); 1951 1952 if ((muxserver_pid = mux_client_request_alive(fd)) == 0) { 1953 error("%s: master alive request failed", __func__); 1954 return -1; 1955 } 1956 1957 signal(SIGPIPE, SIG_IGN); 1958 1959 if (stdin_null_flag) { 1960 if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1) 1961 fatal("open(/dev/null): %s", strerror(errno)); 1962 if (dup2(devnull, STDIN_FILENO) == -1) 1963 fatal("dup2: %s", strerror(errno)); 1964 if (devnull > STDERR_FILENO) 1965 close(devnull); 1966 } 1967 1968 buffer_init(&m); 1969 buffer_put_int(&m, MUX_C_NEW_STDIO_FWD); 1970 buffer_put_int(&m, muxclient_request_id); 1971 buffer_put_cstring(&m, ""); /* reserved */ 1972 buffer_put_cstring(&m, stdio_forward_host); 1973 buffer_put_int(&m, stdio_forward_port); 1974 1975 if (mux_client_write_packet(fd, &m) != 0) 1976 fatal("%s: write packet: %s", __func__, strerror(errno)); 1977 1978 /* Send the stdio file descriptors */ 1979 if (mm_send_fd(fd, STDIN_FILENO) == -1 || 1980 mm_send_fd(fd, STDOUT_FILENO) == -1) 1981 fatal("%s: send fds failed", __func__); 1982 1983 debug3("%s: stdio forward request sent", __func__); 1984 1985 /* Read their reply */ 1986 buffer_clear(&m); 1987 1988 if (mux_client_read_packet(fd, &m) != 0) { 1989 error("%s: read from master failed: %s", 1990 __func__, strerror(errno)); 1991 buffer_free(&m); 1992 return -1; 1993 } 1994 1995 type = buffer_get_int(&m); 1996 if ((rid = buffer_get_int(&m)) != muxclient_request_id) 1997 fatal("%s: out of sequence reply: my id %u theirs %u", 1998 __func__, muxclient_request_id, rid); 1999 switch (type) { 2000 case MUX_S_SESSION_OPENED: 2001 sid = buffer_get_int(&m); 2002 debug("%s: master session id: %u", __func__, sid); 2003 break; 2004 case MUX_S_PERMISSION_DENIED: 2005 e = buffer_get_string(&m, NULL); 2006 buffer_free(&m); 2007 fatal("Master refused stdio forwarding request: %s", e); 2008 case MUX_S_FAILURE: 2009 e = buffer_get_string(&m, NULL); 2010 buffer_free(&m); 2011 fatal("Stdio forwarding request failed: %s", e); 2012 default: 2013 buffer_free(&m); 2014 error("%s: unexpected response from master 0x%08x", 2015 __func__, type); 2016 return -1; 2017 } 2018 muxclient_request_id++; 2019 2020 signal(SIGHUP, control_client_sighandler); 2021 signal(SIGINT, control_client_sighandler); 2022 signal(SIGTERM, control_client_sighandler); 2023 signal(SIGWINCH, control_client_sigrelay); 2024 2025 /* 2026 * Stick around until the controlee closes the client_fd. 2027 */ 2028 buffer_clear(&m); 2029 if (mux_client_read_packet(fd, &m) != 0) { 2030 if (errno == EPIPE || 2031 (errno == EINTR && muxclient_terminate != 0)) 2032 return 0; 2033 fatal("%s: mux_client_read_packet: %s", 2034 __func__, strerror(errno)); 2035 } 2036 fatal("%s: master returned unexpected message %u", __func__, type); 2037 } 2038 2039 static void 2040 mux_client_request_stop_listening(int fd) 2041 { 2042 Buffer m; 2043 char *e; 2044 u_int type, rid; 2045 2046 debug3("%s: entering", __func__); 2047 2048 buffer_init(&m); 2049 buffer_put_int(&m, MUX_C_STOP_LISTENING); 2050 buffer_put_int(&m, muxclient_request_id); 2051 2052 if (mux_client_write_packet(fd, &m) != 0) 2053 fatal("%s: write packet: %s", __func__, strerror(errno)); 2054 2055 buffer_clear(&m); 2056 2057 /* Read their reply */ 2058 if (mux_client_read_packet(fd, &m) != 0) 2059 fatal("%s: read from master failed: %s", 2060 __func__, strerror(errno)); 2061 2062 type = buffer_get_int(&m); 2063 if ((rid = buffer_get_int(&m)) != muxclient_request_id) 2064 fatal("%s: out of sequence reply: my id %u theirs %u", 2065 __func__, muxclient_request_id, rid); 2066 switch (type) { 2067 case MUX_S_OK: 2068 break; 2069 case MUX_S_PERMISSION_DENIED: 2070 e = buffer_get_string(&m, NULL); 2071 fatal("Master refused stop listening request: %s", e); 2072 case MUX_S_FAILURE: 2073 e = buffer_get_string(&m, NULL); 2074 fatal("%s: stop listening request failed: %s", __func__, e); 2075 default: 2076 fatal("%s: unexpected response from master 0x%08x", 2077 __func__, type); 2078 } 2079 buffer_free(&m); 2080 muxclient_request_id++; 2081 } 2082 2083 /* Multiplex client main loop. */ 2084 void 2085 muxclient(const char *path) 2086 { 2087 struct sockaddr_un addr; 2088 int sock; 2089 u_int pid; 2090 2091 if (muxclient_command == 0) { 2092 if (stdio_forward_host != NULL) 2093 muxclient_command = SSHMUX_COMMAND_STDIO_FWD; 2094 else 2095 muxclient_command = SSHMUX_COMMAND_OPEN; 2096 } 2097 2098 switch (options.control_master) { 2099 case SSHCTL_MASTER_AUTO: 2100 case SSHCTL_MASTER_AUTO_ASK: 2101 debug("auto-mux: Trying existing master"); 2102 /* FALLTHROUGH */ 2103 case SSHCTL_MASTER_NO: 2104 break; 2105 default: 2106 return; 2107 } 2108 2109 memset(&addr, '\0', sizeof(addr)); 2110 addr.sun_family = AF_UNIX; 2111 addr.sun_len = offsetof(struct sockaddr_un, sun_path) + 2112 strlen(path) + 1; 2113 2114 if (strlcpy(addr.sun_path, path, 2115 sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) 2116 fatal("ControlPath too long"); 2117 2118 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) 2119 fatal("%s socket(): %s", __func__, strerror(errno)); 2120 2121 if (connect(sock, (struct sockaddr *)&addr, addr.sun_len) == -1) { 2122 switch (muxclient_command) { 2123 case SSHMUX_COMMAND_OPEN: 2124 case SSHMUX_COMMAND_STDIO_FWD: 2125 break; 2126 default: 2127 fatal("Control socket connect(%.100s): %s", path, 2128 strerror(errno)); 2129 } 2130 if (errno == ECONNREFUSED && 2131 options.control_master != SSHCTL_MASTER_NO) { 2132 debug("Stale control socket %.100s, unlinking", path); 2133 unlink(path); 2134 } else if (errno == ENOENT) { 2135 debug("Control socket \"%.100s\" does not exist", path); 2136 } else { 2137 error("Control socket connect(%.100s): %s", path, 2138 strerror(errno)); 2139 } 2140 close(sock); 2141 return; 2142 } 2143 set_nonblock(sock); 2144 2145 if (mux_client_hello_exchange(sock) != 0) { 2146 error("%s: master hello exchange failed", __func__); 2147 close(sock); 2148 return; 2149 } 2150 2151 switch (muxclient_command) { 2152 case SSHMUX_COMMAND_ALIVE_CHECK: 2153 if ((pid = mux_client_request_alive(sock)) == 0) 2154 fatal("%s: master alive check failed", __func__); 2155 fprintf(stderr, "Master running (pid=%d)\r\n", pid); 2156 exit(0); 2157 case SSHMUX_COMMAND_TERMINATE: 2158 mux_client_request_terminate(sock); 2159 fprintf(stderr, "Exit request sent.\r\n"); 2160 exit(0); 2161 case SSHMUX_COMMAND_FORWARD: 2162 if (mux_client_forwards(sock, 0) != 0) 2163 fatal("%s: master forward request failed", __func__); 2164 exit(0); 2165 case SSHMUX_COMMAND_OPEN: 2166 if (mux_client_forwards(sock, 0) != 0) { 2167 error("%s: master forward request failed", __func__); 2168 return; 2169 } 2170 mux_client_request_session(sock); 2171 return; 2172 case SSHMUX_COMMAND_STDIO_FWD: 2173 mux_client_request_stdio_fwd(sock); 2174 exit(0); 2175 case SSHMUX_COMMAND_STOP: 2176 mux_client_request_stop_listening(sock); 2177 fprintf(stderr, "Stop listening request sent.\r\n"); 2178 exit(0); 2179 case SSHMUX_COMMAND_CANCEL_FWD: 2180 if (mux_client_forwards(sock, 1) != 0) 2181 error("%s: master cancel forward request failed", 2182 __func__); 2183 exit(0); 2184 default: 2185 fatal("unrecognised muxclient_command %d", muxclient_command); 2186 } 2187 } 2188