djm@openbsd.org | 46ac2ed | 2014-12-22 07:24:11 +0000 | [diff] [blame] | 1 | /* $OpenBSD: mux.c,v 1.49 2014/12/22 07:24:11 djm Exp $ */ |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 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 | |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 20 | /* |
| 21 | * TODO: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 22 | * - Better signalling from master to slave, especially passing of |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 23 | * error messages |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 24 | * - Better fall-back from mux slave error to new connection. |
| 25 | * - ExitOnForwardingFailure |
| 26 | * - Maybe extension mechanisms for multi-X11/multi-agent forwarding |
| 27 | * - Support ~^Z in mux slaves. |
| 28 | * - Inspect or control sessions in master. |
| 29 | * - If we ever support the "signal" channel request, send signals on |
| 30 | * sessions in master. |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 31 | */ |
| 32 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 33 | #include "includes.h" |
| 34 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 35 | #include <sys/types.h> |
| 36 | #include <sys/param.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/socket.h> |
| 39 | #include <sys/un.h> |
| 40 | |
| 41 | #include <errno.h> |
| 42 | #include <fcntl.h> |
| 43 | #include <signal.h> |
| 44 | #include <stdarg.h> |
| 45 | #include <stddef.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <stdio.h> |
| 48 | #include <string.h> |
| 49 | #include <unistd.h> |
Darren Tucker | ce38d82 | 2008-06-07 06:25:15 +1000 | [diff] [blame] | 50 | #ifdef HAVE_PATHS_H |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 51 | #include <paths.h> |
Darren Tucker | ce38d82 | 2008-06-07 06:25:15 +1000 | [diff] [blame] | 52 | #endif |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 53 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 54 | #ifdef HAVE_POLL_H |
| 55 | #include <poll.h> |
| 56 | #else |
| 57 | # ifdef HAVE_SYS_POLL_H |
| 58 | # include <sys/poll.h> |
| 59 | # endif |
| 60 | #endif |
| 61 | |
Damien Miller | a7058ec | 2008-05-20 08:57:06 +1000 | [diff] [blame] | 62 | #ifdef HAVE_UTIL_H |
| 63 | # include <util.h> |
| 64 | #endif |
| 65 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 66 | #include "openbsd-compat/sys-queue.h" |
| 67 | #include "xmalloc.h" |
| 68 | #include "log.h" |
| 69 | #include "ssh.h" |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 70 | #include "ssh2.h" |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 71 | #include "pathnames.h" |
| 72 | #include "misc.h" |
| 73 | #include "match.h" |
| 74 | #include "buffer.h" |
| 75 | #include "channels.h" |
| 76 | #include "msg.h" |
| 77 | #include "packet.h" |
| 78 | #include "monitor_fdpass.h" |
| 79 | #include "sshpty.h" |
| 80 | #include "key.h" |
| 81 | #include "readconf.h" |
| 82 | #include "clientloop.h" |
| 83 | |
| 84 | /* from ssh.c */ |
| 85 | extern int tty_flag; |
| 86 | extern Options options; |
| 87 | extern int stdin_null_flag; |
| 88 | extern char *host; |
Darren Tucker | 8ec4fd8 | 2009-10-07 08:39:57 +1100 | [diff] [blame] | 89 | extern int subsystem_flag; |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 90 | extern Buffer command; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 91 | extern volatile sig_atomic_t quit_pending; |
| 92 | extern char *stdio_forward_host; |
| 93 | extern int stdio_forward_port; |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 94 | |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 95 | /* Context for session open confirmation callback */ |
| 96 | struct mux_session_confirm_ctx { |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 97 | u_int want_tty; |
| 98 | u_int want_subsys; |
| 99 | u_int want_x_fwd; |
| 100 | u_int want_agent_fwd; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 101 | Buffer cmd; |
| 102 | char *term; |
| 103 | struct termios tio; |
| 104 | char **env; |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 105 | u_int rid; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 106 | }; |
| 107 | |
Damien Miller | 357610d | 2014-07-18 15:04:10 +1000 | [diff] [blame] | 108 | /* Context for stdio fwd open confirmation callback */ |
| 109 | struct mux_stdio_confirm_ctx { |
| 110 | u_int rid; |
| 111 | }; |
| 112 | |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 113 | /* Context for global channel callback */ |
| 114 | struct mux_channel_confirm_ctx { |
| 115 | u_int cid; /* channel id */ |
| 116 | u_int rid; /* request id */ |
| 117 | int fid; /* forward id */ |
| 118 | }; |
| 119 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 120 | /* fd to control socket */ |
| 121 | int muxserver_sock = -1; |
| 122 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 123 | /* client request id */ |
| 124 | u_int muxclient_request_id = 0; |
| 125 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 126 | /* Multiplexing control command */ |
| 127 | u_int muxclient_command = 0; |
| 128 | |
| 129 | /* Set when signalled. */ |
| 130 | static volatile sig_atomic_t muxclient_terminate = 0; |
| 131 | |
| 132 | /* PID of multiplex server */ |
| 133 | static u_int muxserver_pid = 0; |
| 134 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 135 | static Channel *mux_listener_channel = NULL; |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 136 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 137 | struct mux_master_state { |
| 138 | int hello_rcvd; |
| 139 | }; |
| 140 | |
| 141 | /* mux protocol messages */ |
| 142 | #define MUX_MSG_HELLO 0x00000001 |
| 143 | #define MUX_C_NEW_SESSION 0x10000002 |
| 144 | #define MUX_C_ALIVE_CHECK 0x10000004 |
| 145 | #define MUX_C_TERMINATE 0x10000005 |
| 146 | #define MUX_C_OPEN_FWD 0x10000006 |
| 147 | #define MUX_C_CLOSE_FWD 0x10000007 |
| 148 | #define MUX_C_NEW_STDIO_FWD 0x10000008 |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 149 | #define MUX_C_STOP_LISTENING 0x10000009 |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 150 | #define MUX_S_OK 0x80000001 |
| 151 | #define MUX_S_PERMISSION_DENIED 0x80000002 |
| 152 | #define MUX_S_FAILURE 0x80000003 |
| 153 | #define MUX_S_EXIT_MESSAGE 0x80000004 |
| 154 | #define MUX_S_ALIVE 0x80000005 |
| 155 | #define MUX_S_SESSION_OPENED 0x80000006 |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 156 | #define MUX_S_REMOTE_PORT 0x80000007 |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 157 | #define MUX_S_TTY_ALLOC_FAIL 0x80000008 |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 158 | |
| 159 | /* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */ |
| 160 | #define MUX_FWD_LOCAL 1 |
| 161 | #define MUX_FWD_REMOTE 2 |
| 162 | #define MUX_FWD_DYNAMIC 3 |
| 163 | |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 164 | static void mux_session_confirm(int, int, void *); |
Damien Miller | 357610d | 2014-07-18 15:04:10 +1000 | [diff] [blame] | 165 | static void mux_stdio_confirm(int, int, void *); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 166 | |
| 167 | static int process_mux_master_hello(u_int, Channel *, Buffer *, Buffer *); |
| 168 | static int process_mux_new_session(u_int, Channel *, Buffer *, Buffer *); |
| 169 | static int process_mux_alive_check(u_int, Channel *, Buffer *, Buffer *); |
| 170 | static int process_mux_terminate(u_int, Channel *, Buffer *, Buffer *); |
| 171 | static int process_mux_open_fwd(u_int, Channel *, Buffer *, Buffer *); |
| 172 | static int process_mux_close_fwd(u_int, Channel *, Buffer *, Buffer *); |
| 173 | static int process_mux_stdio_fwd(u_int, Channel *, Buffer *, Buffer *); |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 174 | static int process_mux_stop_listening(u_int, Channel *, Buffer *, Buffer *); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 175 | |
| 176 | static const struct { |
| 177 | u_int type; |
| 178 | int (*handler)(u_int, Channel *, Buffer *, Buffer *); |
| 179 | } mux_master_handlers[] = { |
| 180 | { MUX_MSG_HELLO, process_mux_master_hello }, |
| 181 | { MUX_C_NEW_SESSION, process_mux_new_session }, |
| 182 | { MUX_C_ALIVE_CHECK, process_mux_alive_check }, |
| 183 | { MUX_C_TERMINATE, process_mux_terminate }, |
| 184 | { MUX_C_OPEN_FWD, process_mux_open_fwd }, |
| 185 | { MUX_C_CLOSE_FWD, process_mux_close_fwd }, |
| 186 | { MUX_C_NEW_STDIO_FWD, process_mux_stdio_fwd }, |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 187 | { MUX_C_STOP_LISTENING, process_mux_stop_listening }, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 188 | { 0, NULL } |
| 189 | }; |
| 190 | |
| 191 | /* Cleanup callback fired on closure of mux slave _session_ channel */ |
| 192 | /* ARGSUSED */ |
Darren Tucker | ea8342c | 2013-06-06 08:11:40 +1000 | [diff] [blame] | 193 | static void |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 194 | mux_master_session_cleanup_cb(int cid, void *unused) |
| 195 | { |
| 196 | Channel *cc, *c = channel_by_id(cid); |
| 197 | |
| 198 | debug3("%s: entering for channel %d", __func__, cid); |
| 199 | if (c == NULL) |
| 200 | fatal("%s: channel_by_id(%i) == NULL", __func__, cid); |
| 201 | if (c->ctl_chan != -1) { |
| 202 | if ((cc = channel_by_id(c->ctl_chan)) == NULL) |
| 203 | fatal("%s: channel %d missing control channel %d", |
| 204 | __func__, c->self, c->ctl_chan); |
| 205 | c->ctl_chan = -1; |
| 206 | cc->remote_id = -1; |
| 207 | chan_rcvd_oclose(cc); |
| 208 | } |
| 209 | channel_cancel_cleanup(c->self); |
| 210 | } |
| 211 | |
| 212 | /* Cleanup callback fired on closure of mux slave _control_ channel */ |
| 213 | /* ARGSUSED */ |
| 214 | static void |
| 215 | mux_master_control_cleanup_cb(int cid, void *unused) |
| 216 | { |
| 217 | Channel *sc, *c = channel_by_id(cid); |
| 218 | |
| 219 | debug3("%s: entering for channel %d", __func__, cid); |
| 220 | if (c == NULL) |
| 221 | fatal("%s: channel_by_id(%i) == NULL", __func__, cid); |
| 222 | if (c->remote_id != -1) { |
| 223 | if ((sc = channel_by_id(c->remote_id)) == NULL) |
Damien Miller | 601a23c | 2010-04-16 15:54:01 +1000 | [diff] [blame] | 224 | fatal("%s: channel %d missing session channel %d", |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 225 | __func__, c->self, c->remote_id); |
| 226 | c->remote_id = -1; |
| 227 | sc->ctl_chan = -1; |
Damien Miller | 172859c | 2013-04-23 15:19:27 +1000 | [diff] [blame] | 228 | if (sc->type != SSH_CHANNEL_OPEN && |
| 229 | sc->type != SSH_CHANNEL_OPENING) { |
Damien Miller | a21cdfa | 2010-01-28 06:26:59 +1100 | [diff] [blame] | 230 | debug2("%s: channel %d: not open", __func__, sc->self); |
Damien Miller | 133d9d3 | 2010-01-30 17:30:04 +1100 | [diff] [blame] | 231 | chan_mark_dead(sc); |
Damien Miller | a21cdfa | 2010-01-28 06:26:59 +1100 | [diff] [blame] | 232 | } else { |
Damien Miller | 0dac03f | 2010-01-30 17:36:33 +1100 | [diff] [blame] | 233 | if (sc->istate == CHAN_INPUT_OPEN) |
| 234 | chan_read_failed(sc); |
| 235 | if (sc->ostate == CHAN_OUTPUT_OPEN) |
| 236 | chan_write_failed(sc); |
Damien Miller | a21cdfa | 2010-01-28 06:26:59 +1100 | [diff] [blame] | 237 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 238 | } |
| 239 | channel_cancel_cleanup(c->self); |
| 240 | } |
| 241 | |
| 242 | /* Check mux client environment variables before passing them to mux master. */ |
| 243 | static int |
| 244 | env_permitted(char *env) |
| 245 | { |
| 246 | int i, ret; |
| 247 | char name[1024], *cp; |
| 248 | |
| 249 | if ((cp = strchr(env, '=')) == NULL || cp == env) |
| 250 | return 0; |
| 251 | ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env); |
| 252 | if (ret <= 0 || (size_t)ret >= sizeof(name)) { |
| 253 | error("env_permitted: name '%.100s...' too long", env); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | for (i = 0; i < options.num_send_env; i++) |
| 258 | if (match_pattern(name, options.send_env[i])) |
| 259 | return 1; |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | /* Mux master protocol message handlers */ |
| 265 | |
| 266 | static int |
| 267 | process_mux_master_hello(u_int rid, Channel *c, Buffer *m, Buffer *r) |
| 268 | { |
| 269 | u_int ver; |
| 270 | struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx; |
| 271 | |
| 272 | if (state == NULL) |
| 273 | fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self); |
| 274 | if (state->hello_rcvd) { |
| 275 | error("%s: HELLO received twice", __func__); |
| 276 | return -1; |
| 277 | } |
| 278 | if (buffer_get_int_ret(&ver, m) != 0) { |
| 279 | malf: |
| 280 | error("%s: malformed message", __func__); |
| 281 | return -1; |
| 282 | } |
| 283 | if (ver != SSHMUX_VER) { |
| 284 | error("Unsupported multiplexing protocol version %d " |
| 285 | "(expected %d)", ver, SSHMUX_VER); |
| 286 | return -1; |
| 287 | } |
| 288 | debug2("%s: channel %d slave version %u", __func__, c->self, ver); |
| 289 | |
| 290 | /* No extensions are presently defined */ |
| 291 | while (buffer_len(m) > 0) { |
| 292 | char *name = buffer_get_string_ret(m, NULL); |
| 293 | char *value = buffer_get_string_ret(m, NULL); |
| 294 | |
| 295 | if (name == NULL || value == NULL) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 296 | free(name); |
Darren Tucker | 746e906 | 2013-06-06 08:20:13 +1000 | [diff] [blame] | 297 | free(value); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 298 | goto malf; |
| 299 | } |
| 300 | debug2("Unrecognised slave extension \"%s\"", name); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 301 | free(name); |
| 302 | free(value); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 303 | } |
| 304 | state->hello_rcvd = 1; |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int |
| 309 | process_mux_new_session(u_int rid, Channel *c, Buffer *m, Buffer *r) |
| 310 | { |
| 311 | Channel *nc; |
| 312 | struct mux_session_confirm_ctx *cctx; |
| 313 | char *reserved, *cmd, *cp; |
| 314 | u_int i, j, len, env_len, escape_char, window, packetmax; |
| 315 | int new_fd[3]; |
| 316 | |
| 317 | /* Reply for SSHMUX_COMMAND_OPEN */ |
| 318 | cctx = xcalloc(1, sizeof(*cctx)); |
| 319 | cctx->term = NULL; |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 320 | cctx->rid = rid; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 321 | cmd = reserved = NULL; |
Damien Miller | ab523b0 | 2012-07-06 13:44:43 +1000 | [diff] [blame] | 322 | cctx->env = NULL; |
| 323 | env_len = 0; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 324 | if ((reserved = buffer_get_string_ret(m, NULL)) == NULL || |
| 325 | buffer_get_int_ret(&cctx->want_tty, m) != 0 || |
| 326 | buffer_get_int_ret(&cctx->want_x_fwd, m) != 0 || |
| 327 | buffer_get_int_ret(&cctx->want_agent_fwd, m) != 0 || |
| 328 | buffer_get_int_ret(&cctx->want_subsys, m) != 0 || |
| 329 | buffer_get_int_ret(&escape_char, m) != 0 || |
| 330 | (cctx->term = buffer_get_string_ret(m, &len)) == NULL || |
| 331 | (cmd = buffer_get_string_ret(m, &len)) == NULL) { |
| 332 | malf: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 333 | free(cmd); |
| 334 | free(reserved); |
Damien Miller | ab523b0 | 2012-07-06 13:44:43 +1000 | [diff] [blame] | 335 | for (j = 0; j < env_len; j++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 336 | free(cctx->env[j]); |
| 337 | free(cctx->env); |
| 338 | free(cctx->term); |
| 339 | free(cctx); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 340 | error("%s: malformed message", __func__); |
| 341 | return -1; |
| 342 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 343 | free(reserved); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 344 | reserved = NULL; |
| 345 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 346 | while (buffer_len(m) > 0) { |
| 347 | #define MUX_MAX_ENV_VARS 4096 |
Damien Miller | 2ec0342 | 2012-02-11 08:16:28 +1100 | [diff] [blame] | 348 | if ((cp = buffer_get_string_ret(m, &len)) == NULL) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 349 | goto malf; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 350 | if (!env_permitted(cp)) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 351 | free(cp); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 352 | continue; |
| 353 | } |
| 354 | cctx->env = xrealloc(cctx->env, env_len + 2, |
| 355 | sizeof(*cctx->env)); |
| 356 | cctx->env[env_len++] = cp; |
| 357 | cctx->env[env_len] = NULL; |
| 358 | if (env_len > MUX_MAX_ENV_VARS) { |
| 359 | error(">%d environment variables received, ignoring " |
| 360 | "additional", MUX_MAX_ENV_VARS); |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, " |
| 366 | "term \"%s\", cmd \"%s\", env %u", __func__, c->self, |
| 367 | cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd, |
| 368 | cctx->want_subsys, cctx->term, cmd, env_len); |
| 369 | |
| 370 | buffer_init(&cctx->cmd); |
| 371 | buffer_append(&cctx->cmd, cmd, strlen(cmd)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 372 | free(cmd); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 373 | cmd = NULL; |
| 374 | |
| 375 | /* Gather fds from client */ |
| 376 | for(i = 0; i < 3; i++) { |
| 377 | if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) { |
| 378 | error("%s: failed to receive fd %d from slave", |
| 379 | __func__, i); |
| 380 | for (j = 0; j < i; j++) |
| 381 | close(new_fd[j]); |
| 382 | for (j = 0; j < env_len; j++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 383 | free(cctx->env[j]); |
| 384 | free(cctx->env); |
| 385 | free(cctx->term); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 386 | buffer_free(&cctx->cmd); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 387 | free(cctx); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 388 | |
| 389 | /* prepare reply */ |
| 390 | buffer_put_int(r, MUX_S_FAILURE); |
| 391 | buffer_put_int(r, rid); |
| 392 | buffer_put_cstring(r, |
| 393 | "did not receive file descriptors"); |
| 394 | return -1; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__, |
| 399 | new_fd[0], new_fd[1], new_fd[2]); |
| 400 | |
| 401 | /* XXX support multiple child sessions in future */ |
| 402 | if (c->remote_id != -1) { |
| 403 | debug2("%s: session already open", __func__); |
| 404 | /* prepare reply */ |
| 405 | buffer_put_int(r, MUX_S_FAILURE); |
| 406 | buffer_put_int(r, rid); |
| 407 | buffer_put_cstring(r, "Multiple sessions not supported"); |
| 408 | cleanup: |
| 409 | close(new_fd[0]); |
| 410 | close(new_fd[1]); |
| 411 | close(new_fd[2]); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 412 | free(cctx->term); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 413 | if (env_len != 0) { |
| 414 | for (i = 0; i < env_len; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 415 | free(cctx->env[i]); |
| 416 | free(cctx->env); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 417 | } |
| 418 | buffer_free(&cctx->cmd); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 419 | free(cctx); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | if (options.control_master == SSHCTL_MASTER_ASK || |
| 424 | options.control_master == SSHCTL_MASTER_AUTO_ASK) { |
| 425 | if (!ask_permission("Allow shared connection to %s? ", host)) { |
| 426 | debug2("%s: session refused by user", __func__); |
| 427 | /* prepare reply */ |
| 428 | buffer_put_int(r, MUX_S_PERMISSION_DENIED); |
| 429 | buffer_put_int(r, rid); |
| 430 | buffer_put_cstring(r, "Permission denied"); |
| 431 | goto cleanup; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* Try to pick up ttymodes from client before it goes raw */ |
| 436 | if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1) |
| 437 | error("%s: tcgetattr: %s", __func__, strerror(errno)); |
| 438 | |
| 439 | /* enable nonblocking unless tty */ |
| 440 | if (!isatty(new_fd[0])) |
| 441 | set_nonblock(new_fd[0]); |
| 442 | if (!isatty(new_fd[1])) |
| 443 | set_nonblock(new_fd[1]); |
| 444 | if (!isatty(new_fd[2])) |
| 445 | set_nonblock(new_fd[2]); |
| 446 | |
| 447 | window = CHAN_SES_WINDOW_DEFAULT; |
| 448 | packetmax = CHAN_SES_PACKET_DEFAULT; |
| 449 | if (cctx->want_tty) { |
| 450 | window >>= 1; |
| 451 | packetmax >>= 1; |
| 452 | } |
| 453 | |
| 454 | nc = channel_new("session", SSH_CHANNEL_OPENING, |
| 455 | new_fd[0], new_fd[1], new_fd[2], window, packetmax, |
| 456 | CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0); |
| 457 | |
| 458 | nc->ctl_chan = c->self; /* link session -> control channel */ |
| 459 | c->remote_id = nc->self; /* link control -> session channel */ |
| 460 | |
| 461 | if (cctx->want_tty && escape_char != 0xffffffff) { |
| 462 | channel_register_filter(nc->self, |
| 463 | client_simple_escape_filter, NULL, |
| 464 | client_filter_cleanup, |
| 465 | client_new_escape_filter_ctx((int)escape_char)); |
| 466 | } |
| 467 | |
| 468 | debug2("%s: channel_new: %d linked to control channel %d", |
| 469 | __func__, nc->self, nc->ctl_chan); |
| 470 | |
| 471 | channel_send_open(nc->self); |
| 472 | channel_register_open_confirm(nc->self, mux_session_confirm, cctx); |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 473 | c->mux_pause = 1; /* stop handling messages until open_confirm done */ |
Damien Miller | 85c50d7 | 2010-05-10 11:53:02 +1000 | [diff] [blame] | 474 | channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 475 | |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 476 | /* reply is deferred, sent by mux_session_confirm */ |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int |
| 481 | process_mux_alive_check(u_int rid, Channel *c, Buffer *m, Buffer *r) |
| 482 | { |
| 483 | debug2("%s: channel %d: alive check", __func__, c->self); |
| 484 | |
| 485 | /* prepare reply */ |
| 486 | buffer_put_int(r, MUX_S_ALIVE); |
| 487 | buffer_put_int(r, rid); |
| 488 | buffer_put_int(r, (u_int)getpid()); |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static int |
| 494 | process_mux_terminate(u_int rid, Channel *c, Buffer *m, Buffer *r) |
| 495 | { |
| 496 | debug2("%s: channel %d: terminate request", __func__, c->self); |
| 497 | |
| 498 | if (options.control_master == SSHCTL_MASTER_ASK || |
| 499 | options.control_master == SSHCTL_MASTER_AUTO_ASK) { |
| 500 | if (!ask_permission("Terminate shared connection to %s? ", |
| 501 | host)) { |
| 502 | debug2("%s: termination refused by user", __func__); |
| 503 | buffer_put_int(r, MUX_S_PERMISSION_DENIED); |
| 504 | buffer_put_int(r, rid); |
| 505 | buffer_put_cstring(r, "Permission denied"); |
| 506 | return 0; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | quit_pending = 1; |
| 511 | buffer_put_int(r, MUX_S_OK); |
| 512 | buffer_put_int(r, rid); |
| 513 | /* XXX exit happens too soon - message never makes it to client */ |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static char * |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 518 | format_forward(u_int ftype, struct Forward *fwd) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 519 | { |
| 520 | char *ret; |
| 521 | |
| 522 | switch (ftype) { |
| 523 | case MUX_FWD_LOCAL: |
| 524 | xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d", |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 525 | (fwd->listen_path != NULL) ? fwd->listen_path : |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 526 | (fwd->listen_host == NULL) ? |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 527 | (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") : |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 528 | fwd->listen_host, fwd->listen_port, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 529 | (fwd->connect_path != NULL) ? fwd->connect_path : |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 530 | fwd->connect_host, fwd->connect_port); |
| 531 | break; |
| 532 | case MUX_FWD_DYNAMIC: |
| 533 | xasprintf(&ret, "dynamic forward %.200s:%d -> *", |
| 534 | (fwd->listen_host == NULL) ? |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 535 | (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") : |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 536 | fwd->listen_host, fwd->listen_port); |
| 537 | break; |
| 538 | case MUX_FWD_REMOTE: |
| 539 | xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d", |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 540 | (fwd->listen_path != NULL) ? fwd->listen_path : |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 541 | (fwd->listen_host == NULL) ? |
| 542 | "LOCALHOST" : fwd->listen_host, |
| 543 | fwd->listen_port, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 544 | (fwd->connect_path != NULL) ? fwd->connect_path : |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 545 | fwd->connect_host, fwd->connect_port); |
| 546 | break; |
| 547 | default: |
| 548 | fatal("%s: unknown forward type %u", __func__, ftype); |
| 549 | } |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | static int |
| 554 | compare_host(const char *a, const char *b) |
| 555 | { |
| 556 | if (a == NULL && b == NULL) |
| 557 | return 1; |
| 558 | if (a == NULL || b == NULL) |
| 559 | return 0; |
| 560 | return strcmp(a, b) == 0; |
| 561 | } |
| 562 | |
| 563 | static int |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 564 | compare_forward(struct Forward *a, struct Forward *b) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 565 | { |
| 566 | if (!compare_host(a->listen_host, b->listen_host)) |
| 567 | return 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 568 | if (!compare_host(a->listen_path, b->listen_path)) |
| 569 | return 0; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 570 | if (a->listen_port != b->listen_port) |
| 571 | return 0; |
| 572 | if (!compare_host(a->connect_host, b->connect_host)) |
| 573 | return 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 574 | if (!compare_host(a->connect_path, b->connect_path)) |
| 575 | return 0; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 576 | if (a->connect_port != b->connect_port) |
| 577 | return 0; |
| 578 | |
| 579 | return 1; |
| 580 | } |
| 581 | |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 582 | static void |
| 583 | mux_confirm_remote_forward(int type, u_int32_t seq, void *ctxt) |
| 584 | { |
| 585 | struct mux_channel_confirm_ctx *fctx = ctxt; |
| 586 | char *failmsg = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 587 | struct Forward *rfwd; |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 588 | Channel *c; |
| 589 | Buffer out; |
| 590 | |
| 591 | if ((c = channel_by_id(fctx->cid)) == NULL) { |
| 592 | /* no channel for reply */ |
| 593 | error("%s: unknown channel", __func__); |
| 594 | return; |
| 595 | } |
| 596 | buffer_init(&out); |
| 597 | if (fctx->fid >= options.num_remote_forwards) { |
| 598 | xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid); |
| 599 | goto fail; |
| 600 | } |
| 601 | rfwd = &options.remote_forwards[fctx->fid]; |
| 602 | debug("%s: %s for: listen %d, connect %s:%d", __func__, |
| 603 | type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 604 | rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path : |
| 605 | rfwd->connect_host, rfwd->connect_port); |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 606 | if (type == SSH2_MSG_REQUEST_SUCCESS) { |
| 607 | if (rfwd->listen_port == 0) { |
| 608 | rfwd->allocated_port = packet_get_int(); |
| 609 | logit("Allocated port %u for mux remote forward" |
| 610 | " to %s:%d", rfwd->allocated_port, |
| 611 | rfwd->connect_host, rfwd->connect_port); |
| 612 | buffer_put_int(&out, MUX_S_REMOTE_PORT); |
| 613 | buffer_put_int(&out, fctx->rid); |
| 614 | buffer_put_int(&out, rfwd->allocated_port); |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 615 | channel_update_permitted_opens(rfwd->handle, |
| 616 | rfwd->allocated_port); |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 617 | } else { |
| 618 | buffer_put_int(&out, MUX_S_OK); |
| 619 | buffer_put_int(&out, fctx->rid); |
| 620 | } |
| 621 | goto out; |
| 622 | } else { |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 623 | if (rfwd->listen_port == 0) |
| 624 | channel_update_permitted_opens(rfwd->handle, -1); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 625 | if (rfwd->listen_path != NULL) |
| 626 | xasprintf(&failmsg, "remote port forwarding failed for " |
| 627 | "listen path %s", rfwd->listen_path); |
| 628 | else |
| 629 | xasprintf(&failmsg, "remote port forwarding failed for " |
| 630 | "listen port %d", rfwd->listen_port); |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 631 | } |
| 632 | fail: |
| 633 | error("%s: %s", __func__, failmsg); |
| 634 | buffer_put_int(&out, MUX_S_FAILURE); |
| 635 | buffer_put_int(&out, fctx->rid); |
| 636 | buffer_put_cstring(&out, failmsg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 637 | free(failmsg); |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 638 | out: |
| 639 | buffer_put_string(&c->output, buffer_ptr(&out), buffer_len(&out)); |
| 640 | buffer_free(&out); |
| 641 | if (c->mux_pause <= 0) |
| 642 | fatal("%s: mux_pause %d", __func__, c->mux_pause); |
| 643 | c->mux_pause = 0; /* start processing messages again */ |
| 644 | } |
| 645 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 646 | static int |
| 647 | process_mux_open_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r) |
| 648 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 649 | struct Forward fwd; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 650 | char *fwd_desc = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 651 | char *listen_addr, *connect_addr; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 652 | u_int ftype; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 653 | u_int lport, cport; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 654 | int i, ret = 0, freefwd = 1; |
| 655 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 656 | /* XXX - lport/cport check redundant */ |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 657 | if (buffer_get_int_ret(&ftype, m) != 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 658 | (listen_addr = buffer_get_string_ret(m, NULL)) == NULL || |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 659 | buffer_get_int_ret(&lport, m) != 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 660 | (connect_addr = buffer_get_string_ret(m, NULL)) == NULL || |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 661 | buffer_get_int_ret(&cport, m) != 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 662 | (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) || |
| 663 | (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) { |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 664 | error("%s: malformed message", __func__); |
| 665 | ret = -1; |
| 666 | goto out; |
| 667 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 668 | if (*listen_addr == '\0') { |
| 669 | free(listen_addr); |
| 670 | listen_addr = NULL; |
| 671 | } |
| 672 | if (*connect_addr == '\0') { |
| 673 | free(connect_addr); |
| 674 | connect_addr = NULL; |
| 675 | } |
| 676 | |
| 677 | memset(&fwd, 0, sizeof(fwd)); |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 678 | fwd.listen_port = lport; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 679 | if (fwd.listen_port == PORT_STREAMLOCAL) |
| 680 | fwd.listen_path = listen_addr; |
| 681 | else |
| 682 | fwd.listen_host = listen_addr; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 683 | fwd.connect_port = cport; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 684 | if (fwd.connect_port == PORT_STREAMLOCAL) |
| 685 | fwd.connect_path = connect_addr; |
| 686 | else |
| 687 | fwd.connect_host = connect_addr; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 688 | |
| 689 | debug2("%s: channel %d: request %s", __func__, c->self, |
| 690 | (fwd_desc = format_forward(ftype, &fwd))); |
| 691 | |
| 692 | if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE && |
| 693 | ftype != MUX_FWD_DYNAMIC) { |
| 694 | logit("%s: invalid forwarding type %u", __func__, ftype); |
| 695 | invalid: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 696 | free(listen_addr); |
| 697 | free(connect_addr); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 698 | buffer_put_int(r, MUX_S_FAILURE); |
| 699 | buffer_put_int(r, rid); |
| 700 | buffer_put_cstring(r, "Invalid forwarding request"); |
| 701 | return 0; |
| 702 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 703 | if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) { |
| 704 | logit("%s: streamlocal and dynamic forwards " |
| 705 | "are mutually exclusive", __func__); |
| 706 | goto invalid; |
| 707 | } |
| 708 | if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) { |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 709 | logit("%s: invalid listen port %u", __func__, |
| 710 | fwd.listen_port); |
| 711 | goto invalid; |
| 712 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 713 | if ((fwd.connect_port != PORT_STREAMLOCAL && fwd.connect_port >= 65536) |
| 714 | || (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE && fwd.connect_port == 0)) { |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 715 | logit("%s: invalid connect port %u", __func__, |
| 716 | fwd.connect_port); |
| 717 | goto invalid; |
| 718 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 719 | if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL && fwd.connect_path == NULL) { |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 720 | logit("%s: missing connect host", __func__); |
| 721 | goto invalid; |
| 722 | } |
| 723 | |
| 724 | /* Skip forwards that have already been requested */ |
| 725 | switch (ftype) { |
| 726 | case MUX_FWD_LOCAL: |
| 727 | case MUX_FWD_DYNAMIC: |
| 728 | for (i = 0; i < options.num_local_forwards; i++) { |
| 729 | if (compare_forward(&fwd, |
| 730 | options.local_forwards + i)) { |
| 731 | exists: |
| 732 | debug2("%s: found existing forwarding", |
| 733 | __func__); |
| 734 | buffer_put_int(r, MUX_S_OK); |
| 735 | buffer_put_int(r, rid); |
| 736 | goto out; |
| 737 | } |
| 738 | } |
| 739 | break; |
| 740 | case MUX_FWD_REMOTE: |
| 741 | for (i = 0; i < options.num_remote_forwards; i++) { |
| 742 | if (compare_forward(&fwd, |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 743 | options.remote_forwards + i)) { |
| 744 | if (fwd.listen_port != 0) |
| 745 | goto exists; |
| 746 | debug2("%s: found allocated port", |
| 747 | __func__); |
| 748 | buffer_put_int(r, MUX_S_REMOTE_PORT); |
| 749 | buffer_put_int(r, rid); |
| 750 | buffer_put_int(r, |
| 751 | options.remote_forwards[i].allocated_port); |
| 752 | goto out; |
| 753 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 754 | } |
| 755 | break; |
| 756 | } |
| 757 | |
| 758 | if (options.control_master == SSHCTL_MASTER_ASK || |
| 759 | options.control_master == SSHCTL_MASTER_AUTO_ASK) { |
| 760 | if (!ask_permission("Open %s on %s?", fwd_desc, host)) { |
| 761 | debug2("%s: forwarding refused by user", __func__); |
| 762 | buffer_put_int(r, MUX_S_PERMISSION_DENIED); |
| 763 | buffer_put_int(r, rid); |
| 764 | buffer_put_cstring(r, "Permission denied"); |
| 765 | goto out; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 770 | if (!channel_setup_local_fwd_listener(&fwd, |
| 771 | &options.fwd_opts)) { |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 772 | fail: |
| 773 | logit("slave-requested %s failed", fwd_desc); |
| 774 | buffer_put_int(r, MUX_S_FAILURE); |
| 775 | buffer_put_int(r, rid); |
| 776 | buffer_put_cstring(r, "Port forwarding failed"); |
| 777 | goto out; |
| 778 | } |
| 779 | add_local_forward(&options, &fwd); |
| 780 | freefwd = 0; |
| 781 | } else { |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 782 | struct mux_channel_confirm_ctx *fctx; |
| 783 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 784 | fwd.handle = channel_request_remote_forwarding(&fwd); |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 785 | if (fwd.handle < 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 786 | goto fail; |
| 787 | add_remote_forward(&options, &fwd); |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 788 | fctx = xcalloc(1, sizeof(*fctx)); |
| 789 | fctx->cid = c->self; |
| 790 | fctx->rid = rid; |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 791 | fctx->fid = options.num_remote_forwards - 1; |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 792 | client_register_global_confirm(mux_confirm_remote_forward, |
| 793 | fctx); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 794 | freefwd = 0; |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 795 | c->mux_pause = 1; /* wait for mux_confirm_remote_forward */ |
| 796 | /* delayed reply in mux_confirm_remote_forward */ |
| 797 | goto out; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 798 | } |
| 799 | buffer_put_int(r, MUX_S_OK); |
| 800 | buffer_put_int(r, rid); |
| 801 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 802 | free(fwd_desc); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 803 | if (freefwd) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 804 | free(fwd.listen_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 805 | free(fwd.listen_path); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 806 | free(fwd.connect_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 807 | free(fwd.connect_path); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 808 | } |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | static int |
| 813 | process_mux_close_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r) |
| 814 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 815 | struct Forward fwd, *found_fwd; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 816 | char *fwd_desc = NULL; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 817 | const char *error_reason = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 818 | char *listen_addr = NULL, *connect_addr = NULL; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 819 | u_int ftype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 820 | int i, ret = 0; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 821 | u_int lport, cport; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 822 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 823 | if (buffer_get_int_ret(&ftype, m) != 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 824 | (listen_addr = buffer_get_string_ret(m, NULL)) == NULL || |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 825 | buffer_get_int_ret(&lport, m) != 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 826 | (connect_addr = buffer_get_string_ret(m, NULL)) == NULL || |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 827 | buffer_get_int_ret(&cport, m) != 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 828 | (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) || |
| 829 | (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) { |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 830 | error("%s: malformed message", __func__); |
| 831 | ret = -1; |
| 832 | goto out; |
| 833 | } |
| 834 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 835 | if (*listen_addr == '\0') { |
| 836 | free(listen_addr); |
| 837 | listen_addr = NULL; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 838 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 839 | if (*connect_addr == '\0') { |
| 840 | free(connect_addr); |
| 841 | connect_addr = NULL; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 842 | } |
| 843 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 844 | memset(&fwd, 0, sizeof(fwd)); |
| 845 | fwd.listen_port = lport; |
| 846 | if (fwd.listen_port == PORT_STREAMLOCAL) |
| 847 | fwd.listen_path = listen_addr; |
| 848 | else |
| 849 | fwd.listen_host = listen_addr; |
| 850 | fwd.connect_port = cport; |
| 851 | if (fwd.connect_port == PORT_STREAMLOCAL) |
| 852 | fwd.connect_path = connect_addr; |
| 853 | else |
| 854 | fwd.connect_host = connect_addr; |
| 855 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 856 | debug2("%s: channel %d: request cancel %s", __func__, c->self, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 857 | (fwd_desc = format_forward(ftype, &fwd))); |
| 858 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 859 | /* make sure this has been requested */ |
| 860 | found_fwd = NULL; |
| 861 | switch (ftype) { |
| 862 | case MUX_FWD_LOCAL: |
| 863 | case MUX_FWD_DYNAMIC: |
| 864 | for (i = 0; i < options.num_local_forwards; i++) { |
| 865 | if (compare_forward(&fwd, |
| 866 | options.local_forwards + i)) { |
| 867 | found_fwd = options.local_forwards + i; |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | break; |
| 872 | case MUX_FWD_REMOTE: |
| 873 | for (i = 0; i < options.num_remote_forwards; i++) { |
| 874 | if (compare_forward(&fwd, |
| 875 | options.remote_forwards + i)) { |
| 876 | found_fwd = options.remote_forwards + i; |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | break; |
| 881 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 882 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 883 | if (found_fwd == NULL) |
| 884 | error_reason = "port not forwarded"; |
| 885 | else if (ftype == MUX_FWD_REMOTE) { |
| 886 | /* |
| 887 | * This shouldn't fail unless we confused the host/port |
| 888 | * between options.remote_forwards and permitted_opens. |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 889 | * However, for dynamic allocated listen ports we need |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 890 | * to use the actual listen port. |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 891 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 892 | if (channel_request_rforward_cancel(found_fwd) == -1) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 893 | error_reason = "port not in permitted opens"; |
| 894 | } else { /* local and dynamic forwards */ |
| 895 | /* Ditto */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 896 | if (channel_cancel_lport_listener(&fwd, fwd.connect_port, |
| 897 | &options.fwd_opts) == -1) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 898 | error_reason = "port not found"; |
| 899 | } |
| 900 | |
| 901 | if (error_reason == NULL) { |
| 902 | buffer_put_int(r, MUX_S_OK); |
| 903 | buffer_put_int(r, rid); |
| 904 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 905 | free(found_fwd->listen_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 906 | free(found_fwd->listen_path); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 907 | free(found_fwd->connect_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 908 | free(found_fwd->connect_path); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 909 | found_fwd->listen_host = found_fwd->connect_host = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 910 | found_fwd->listen_path = found_fwd->connect_path = NULL; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 911 | found_fwd->listen_port = found_fwd->connect_port = 0; |
| 912 | } else { |
| 913 | buffer_put_int(r, MUX_S_FAILURE); |
| 914 | buffer_put_int(r, rid); |
| 915 | buffer_put_cstring(r, error_reason); |
| 916 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 917 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 918 | free(fwd_desc); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 919 | free(listen_addr); |
| 920 | free(connect_addr); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 921 | |
| 922 | return ret; |
| 923 | } |
| 924 | |
| 925 | static int |
| 926 | process_mux_stdio_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r) |
| 927 | { |
| 928 | Channel *nc; |
| 929 | char *reserved, *chost; |
| 930 | u_int cport, i, j; |
| 931 | int new_fd[2]; |
Damien Miller | 357610d | 2014-07-18 15:04:10 +1000 | [diff] [blame] | 932 | struct mux_stdio_confirm_ctx *cctx; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 933 | |
| 934 | chost = reserved = NULL; |
| 935 | if ((reserved = buffer_get_string_ret(m, NULL)) == NULL || |
| 936 | (chost = buffer_get_string_ret(m, NULL)) == NULL || |
| 937 | buffer_get_int_ret(&cport, m) != 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 938 | free(reserved); |
| 939 | free(chost); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 940 | error("%s: malformed message", __func__); |
| 941 | return -1; |
| 942 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 943 | free(reserved); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 944 | |
| 945 | debug2("%s: channel %d: request stdio fwd to %s:%u", |
| 946 | __func__, c->self, chost, cport); |
| 947 | |
| 948 | /* Gather fds from client */ |
| 949 | for(i = 0; i < 2; i++) { |
| 950 | if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) { |
| 951 | error("%s: failed to receive fd %d from slave", |
| 952 | __func__, i); |
| 953 | for (j = 0; j < i; j++) |
| 954 | close(new_fd[j]); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 955 | free(chost); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 956 | |
| 957 | /* prepare reply */ |
| 958 | buffer_put_int(r, MUX_S_FAILURE); |
| 959 | buffer_put_int(r, rid); |
| 960 | buffer_put_cstring(r, |
| 961 | "did not receive file descriptors"); |
| 962 | return -1; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | debug3("%s: got fds stdin %d, stdout %d", __func__, |
| 967 | new_fd[0], new_fd[1]); |
| 968 | |
| 969 | /* XXX support multiple child sessions in future */ |
| 970 | if (c->remote_id != -1) { |
| 971 | debug2("%s: session already open", __func__); |
| 972 | /* prepare reply */ |
| 973 | buffer_put_int(r, MUX_S_FAILURE); |
| 974 | buffer_put_int(r, rid); |
| 975 | buffer_put_cstring(r, "Multiple sessions not supported"); |
| 976 | cleanup: |
| 977 | close(new_fd[0]); |
| 978 | close(new_fd[1]); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 979 | free(chost); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | if (options.control_master == SSHCTL_MASTER_ASK || |
| 984 | options.control_master == SSHCTL_MASTER_AUTO_ASK) { |
Damien Miller | 68512c0 | 2010-10-21 15:21:11 +1100 | [diff] [blame] | 985 | if (!ask_permission("Allow forward to %s:%u? ", |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 986 | chost, cport)) { |
| 987 | debug2("%s: stdio fwd refused by user", __func__); |
| 988 | /* prepare reply */ |
| 989 | buffer_put_int(r, MUX_S_PERMISSION_DENIED); |
| 990 | buffer_put_int(r, rid); |
| 991 | buffer_put_cstring(r, "Permission denied"); |
| 992 | goto cleanup; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /* enable nonblocking unless tty */ |
| 997 | if (!isatty(new_fd[0])) |
| 998 | set_nonblock(new_fd[0]); |
| 999 | if (!isatty(new_fd[1])) |
| 1000 | set_nonblock(new_fd[1]); |
| 1001 | |
| 1002 | nc = channel_connect_stdio_fwd(chost, cport, new_fd[0], new_fd[1]); |
| 1003 | |
| 1004 | nc->ctl_chan = c->self; /* link session -> control channel */ |
| 1005 | c->remote_id = nc->self; /* link control -> session channel */ |
| 1006 | |
| 1007 | debug2("%s: channel_new: %d linked to control channel %d", |
| 1008 | __func__, nc->self, nc->ctl_chan); |
| 1009 | |
Damien Miller | 85c50d7 | 2010-05-10 11:53:02 +1000 | [diff] [blame] | 1010 | channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1011 | |
Damien Miller | 357610d | 2014-07-18 15:04:10 +1000 | [diff] [blame] | 1012 | cctx = xcalloc(1, sizeof(*cctx)); |
| 1013 | cctx->rid = rid; |
| 1014 | channel_register_open_confirm(nc->self, mux_stdio_confirm, cctx); |
| 1015 | c->mux_pause = 1; /* stop handling messages until open_confirm done */ |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1016 | |
Damien Miller | 357610d | 2014-07-18 15:04:10 +1000 | [diff] [blame] | 1017 | /* reply is deferred, sent by mux_session_confirm */ |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
| 1020 | |
Damien Miller | 357610d | 2014-07-18 15:04:10 +1000 | [diff] [blame] | 1021 | /* Callback on open confirmation in mux master for a mux stdio fwd session. */ |
| 1022 | static void |
| 1023 | mux_stdio_confirm(int id, int success, void *arg) |
| 1024 | { |
| 1025 | struct mux_stdio_confirm_ctx *cctx = arg; |
| 1026 | Channel *c, *cc; |
| 1027 | Buffer reply; |
| 1028 | |
| 1029 | if (cctx == NULL) |
| 1030 | fatal("%s: cctx == NULL", __func__); |
| 1031 | if ((c = channel_by_id(id)) == NULL) |
| 1032 | fatal("%s: no channel for id %d", __func__, id); |
| 1033 | if ((cc = channel_by_id(c->ctl_chan)) == NULL) |
| 1034 | fatal("%s: channel %d lacks control channel %d", __func__, |
| 1035 | id, c->ctl_chan); |
| 1036 | |
| 1037 | if (!success) { |
| 1038 | debug3("%s: sending failure reply", __func__); |
| 1039 | /* prepare reply */ |
| 1040 | buffer_init(&reply); |
| 1041 | buffer_put_int(&reply, MUX_S_FAILURE); |
| 1042 | buffer_put_int(&reply, cctx->rid); |
| 1043 | buffer_put_cstring(&reply, "Session open refused by peer"); |
| 1044 | goto done; |
| 1045 | } |
| 1046 | |
| 1047 | debug3("%s: sending success reply", __func__); |
| 1048 | /* prepare reply */ |
| 1049 | buffer_init(&reply); |
| 1050 | buffer_put_int(&reply, MUX_S_SESSION_OPENED); |
| 1051 | buffer_put_int(&reply, cctx->rid); |
| 1052 | buffer_put_int(&reply, c->self); |
| 1053 | |
| 1054 | done: |
| 1055 | /* Send reply */ |
| 1056 | buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply)); |
| 1057 | buffer_free(&reply); |
| 1058 | |
| 1059 | if (cc->mux_pause <= 0) |
| 1060 | fatal("%s: mux_pause %d", __func__, cc->mux_pause); |
| 1061 | cc->mux_pause = 0; /* start processing messages again */ |
| 1062 | c->open_confirm_ctx = NULL; |
| 1063 | free(cctx); |
| 1064 | } |
| 1065 | |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 1066 | static int |
| 1067 | process_mux_stop_listening(u_int rid, Channel *c, Buffer *m, Buffer *r) |
| 1068 | { |
| 1069 | debug("%s: channel %d: stop listening", __func__, c->self); |
| 1070 | |
| 1071 | if (options.control_master == SSHCTL_MASTER_ASK || |
| 1072 | options.control_master == SSHCTL_MASTER_AUTO_ASK) { |
| 1073 | if (!ask_permission("Disable further multiplexing on shared " |
| 1074 | "connection to %s? ", host)) { |
| 1075 | debug2("%s: stop listen refused by user", __func__); |
| 1076 | buffer_put_int(r, MUX_S_PERMISSION_DENIED); |
| 1077 | buffer_put_int(r, rid); |
| 1078 | buffer_put_cstring(r, "Permission denied"); |
| 1079 | return 0; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | if (mux_listener_channel != NULL) { |
| 1084 | channel_free(mux_listener_channel); |
| 1085 | client_stop_mux(); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1086 | free(options.control_path); |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 1087 | options.control_path = NULL; |
| 1088 | mux_listener_channel = NULL; |
| 1089 | muxserver_sock = -1; |
| 1090 | } |
| 1091 | |
| 1092 | /* prepare reply */ |
| 1093 | buffer_put_int(r, MUX_S_OK); |
| 1094 | buffer_put_int(r, rid); |
| 1095 | |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1099 | /* Channel callbacks fired on read/write from mux slave fd */ |
| 1100 | static int |
| 1101 | mux_master_read_cb(Channel *c) |
| 1102 | { |
| 1103 | struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx; |
| 1104 | Buffer in, out; |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 1105 | const u_char *ptr; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1106 | u_int type, rid, have, i; |
| 1107 | int ret = -1; |
| 1108 | |
| 1109 | /* Setup ctx and */ |
| 1110 | if (c->mux_ctx == NULL) { |
Damien Miller | c094d1e | 2010-06-26 09:36:34 +1000 | [diff] [blame] | 1111 | state = xcalloc(1, sizeof(*state)); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1112 | c->mux_ctx = state; |
| 1113 | channel_register_cleanup(c->self, |
| 1114 | mux_master_control_cleanup_cb, 0); |
| 1115 | |
| 1116 | /* Send hello */ |
| 1117 | buffer_init(&out); |
| 1118 | buffer_put_int(&out, MUX_MSG_HELLO); |
| 1119 | buffer_put_int(&out, SSHMUX_VER); |
| 1120 | /* no extensions */ |
| 1121 | buffer_put_string(&c->output, buffer_ptr(&out), |
| 1122 | buffer_len(&out)); |
| 1123 | buffer_free(&out); |
| 1124 | debug3("%s: channel %d: hello sent", __func__, c->self); |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | buffer_init(&in); |
| 1129 | buffer_init(&out); |
| 1130 | |
| 1131 | /* Channel code ensures that we receive whole packets */ |
| 1132 | if ((ptr = buffer_get_string_ptr_ret(&c->input, &have)) == NULL) { |
| 1133 | malf: |
| 1134 | error("%s: malformed message", __func__); |
| 1135 | goto out; |
| 1136 | } |
| 1137 | buffer_append(&in, ptr, have); |
| 1138 | |
| 1139 | if (buffer_get_int_ret(&type, &in) != 0) |
| 1140 | goto malf; |
| 1141 | debug3("%s: channel %d packet type 0x%08x len %u", |
| 1142 | __func__, c->self, type, buffer_len(&in)); |
| 1143 | |
| 1144 | if (type == MUX_MSG_HELLO) |
| 1145 | rid = 0; |
| 1146 | else { |
| 1147 | if (!state->hello_rcvd) { |
| 1148 | error("%s: expected MUX_MSG_HELLO(0x%08x), " |
| 1149 | "received 0x%08x", __func__, MUX_MSG_HELLO, type); |
| 1150 | goto out; |
| 1151 | } |
| 1152 | if (buffer_get_int_ret(&rid, &in) != 0) |
| 1153 | goto malf; |
| 1154 | } |
| 1155 | |
| 1156 | for (i = 0; mux_master_handlers[i].handler != NULL; i++) { |
| 1157 | if (type == mux_master_handlers[i].type) { |
| 1158 | ret = mux_master_handlers[i].handler(rid, c, &in, &out); |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | if (mux_master_handlers[i].handler == NULL) { |
| 1163 | error("%s: unsupported mux message 0x%08x", __func__, type); |
| 1164 | buffer_put_int(&out, MUX_S_FAILURE); |
| 1165 | buffer_put_int(&out, rid); |
| 1166 | buffer_put_cstring(&out, "unsupported request"); |
| 1167 | ret = 0; |
| 1168 | } |
| 1169 | /* Enqueue reply packet */ |
| 1170 | if (buffer_len(&out) != 0) { |
| 1171 | buffer_put_string(&c->output, buffer_ptr(&out), |
| 1172 | buffer_len(&out)); |
| 1173 | } |
| 1174 | out: |
| 1175 | buffer_free(&in); |
| 1176 | buffer_free(&out); |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
| 1180 | void |
| 1181 | mux_exit_message(Channel *c, int exitval) |
| 1182 | { |
| 1183 | Buffer m; |
| 1184 | Channel *mux_chan; |
| 1185 | |
Damien Miller | bc02f16 | 2013-04-23 19:25:49 +1000 | [diff] [blame] | 1186 | debug3("%s: channel %d: exit message, exitval %d", __func__, c->self, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1187 | exitval); |
| 1188 | |
| 1189 | if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL) |
| 1190 | fatal("%s: channel %d missing mux channel %d", |
| 1191 | __func__, c->self, c->ctl_chan); |
| 1192 | |
| 1193 | /* Append exit message packet to control socket output queue */ |
| 1194 | buffer_init(&m); |
| 1195 | buffer_put_int(&m, MUX_S_EXIT_MESSAGE); |
| 1196 | buffer_put_int(&m, c->self); |
| 1197 | buffer_put_int(&m, exitval); |
| 1198 | |
| 1199 | buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m)); |
| 1200 | buffer_free(&m); |
| 1201 | } |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1202 | |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 1203 | void |
| 1204 | mux_tty_alloc_failed(Channel *c) |
| 1205 | { |
| 1206 | Buffer m; |
| 1207 | Channel *mux_chan; |
| 1208 | |
| 1209 | debug3("%s: channel %d: TTY alloc failed", __func__, c->self); |
| 1210 | |
| 1211 | if ((mux_chan = channel_by_id(c->ctl_chan)) == NULL) |
| 1212 | fatal("%s: channel %d missing mux channel %d", |
| 1213 | __func__, c->self, c->ctl_chan); |
| 1214 | |
| 1215 | /* Append exit message packet to control socket output queue */ |
| 1216 | buffer_init(&m); |
| 1217 | buffer_put_int(&m, MUX_S_TTY_ALLOC_FAIL); |
| 1218 | buffer_put_int(&m, c->self); |
| 1219 | |
| 1220 | buffer_put_string(&mux_chan->output, buffer_ptr(&m), buffer_len(&m)); |
| 1221 | buffer_free(&m); |
| 1222 | } |
| 1223 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1224 | /* Prepare a mux master to listen on a Unix domain socket. */ |
| 1225 | void |
| 1226 | muxserver_listen(void) |
| 1227 | { |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1228 | mode_t old_umask; |
Damien Miller | 603134e | 2010-09-24 22:07:55 +1000 | [diff] [blame] | 1229 | char *orig_control_path = options.control_path; |
| 1230 | char rbuf[16+1]; |
| 1231 | u_int i, r; |
Damien Miller | f42f768 | 2014-07-18 15:03:27 +1000 | [diff] [blame] | 1232 | int oerrno; |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1233 | |
| 1234 | if (options.control_path == NULL || |
| 1235 | options.control_master == SSHCTL_MASTER_NO) |
| 1236 | return; |
| 1237 | |
| 1238 | debug("setting up multiplex master socket"); |
| 1239 | |
Damien Miller | 603134e | 2010-09-24 22:07:55 +1000 | [diff] [blame] | 1240 | /* |
| 1241 | * Use a temporary path before listen so we can pseudo-atomically |
| 1242 | * establish the listening socket in its final location to avoid |
| 1243 | * other processes racing in between bind() and listen() and hitting |
| 1244 | * an unready socket. |
| 1245 | */ |
| 1246 | for (i = 0; i < sizeof(rbuf) - 1; i++) { |
| 1247 | r = arc4random_uniform(26+26+10); |
| 1248 | rbuf[i] = (r < 26) ? 'a' + r : |
| 1249 | (r < 26*2) ? 'A' + r - 26 : |
| 1250 | '0' + r - 26 - 26; |
| 1251 | } |
| 1252 | rbuf[sizeof(rbuf) - 1] = '\0'; |
| 1253 | options.control_path = NULL; |
| 1254 | xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf); |
| 1255 | debug3("%s: temporary control path %s", __func__, options.control_path); |
| 1256 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1257 | old_umask = umask(0177); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1258 | muxserver_sock = unix_listener(options.control_path, 64, 0); |
Damien Miller | f42f768 | 2014-07-18 15:03:27 +1000 | [diff] [blame] | 1259 | oerrno = errno; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1260 | umask(old_umask); |
| 1261 | if (muxserver_sock < 0) { |
Damien Miller | f42f768 | 2014-07-18 15:03:27 +1000 | [diff] [blame] | 1262 | if (oerrno == EINVAL || oerrno == EADDRINUSE) { |
Darren Tucker | ca19bfe | 2008-06-13 10:24:03 +1000 | [diff] [blame] | 1263 | error("ControlSocket %s already exists, " |
| 1264 | "disabling multiplexing", options.control_path); |
Damien Miller | 603134e | 2010-09-24 22:07:55 +1000 | [diff] [blame] | 1265 | disable_mux_master: |
Damien Miller | 60432d8 | 2011-05-15 08:34:46 +1000 | [diff] [blame] | 1266 | if (muxserver_sock != -1) { |
| 1267 | close(muxserver_sock); |
| 1268 | muxserver_sock = -1; |
| 1269 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1270 | free(orig_control_path); |
| 1271 | free(options.control_path); |
Darren Tucker | ca19bfe | 2008-06-13 10:24:03 +1000 | [diff] [blame] | 1272 | options.control_path = NULL; |
| 1273 | options.control_master = SSHCTL_MASTER_NO; |
| 1274 | return; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1275 | } else { |
| 1276 | /* unix_listener() logs the error */ |
| 1277 | cleanup_exit(255); |
| 1278 | } |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1279 | } |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1280 | |
Damien Miller | 603134e | 2010-09-24 22:07:55 +1000 | [diff] [blame] | 1281 | /* Now atomically "move" the mux socket into position */ |
| 1282 | if (link(options.control_path, orig_control_path) != 0) { |
| 1283 | if (errno != EEXIST) { |
| 1284 | fatal("%s: link mux listener %s => %s: %s", __func__, |
| 1285 | options.control_path, orig_control_path, |
| 1286 | strerror(errno)); |
| 1287 | } |
| 1288 | error("ControlSocket %s already exists, disabling multiplexing", |
| 1289 | orig_control_path); |
Damien Miller | 603134e | 2010-09-24 22:07:55 +1000 | [diff] [blame] | 1290 | unlink(options.control_path); |
| 1291 | goto disable_mux_master; |
| 1292 | } |
| 1293 | unlink(options.control_path); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1294 | free(options.control_path); |
Damien Miller | 603134e | 2010-09-24 22:07:55 +1000 | [diff] [blame] | 1295 | options.control_path = orig_control_path; |
| 1296 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1297 | set_nonblock(muxserver_sock); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1298 | |
| 1299 | mux_listener_channel = channel_new("mux listener", |
| 1300 | SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1, |
| 1301 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | 603134e | 2010-09-24 22:07:55 +1000 | [diff] [blame] | 1302 | 0, options.control_path, 1); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1303 | mux_listener_channel->mux_rcb = mux_master_read_cb; |
| 1304 | debug3("%s: mux listener channel %d fd %d", __func__, |
| 1305 | mux_listener_channel->self, mux_listener_channel->sock); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /* Callback on open confirmation in mux master for a mux client session. */ |
| 1309 | static void |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1310 | mux_session_confirm(int id, int success, void *arg) |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1311 | { |
| 1312 | struct mux_session_confirm_ctx *cctx = arg; |
| 1313 | const char *display; |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1314 | Channel *c, *cc; |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1315 | int i; |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1316 | Buffer reply; |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1317 | |
| 1318 | if (cctx == NULL) |
| 1319 | fatal("%s: cctx == NULL", __func__); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1320 | if ((c = channel_by_id(id)) == NULL) |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1321 | fatal("%s: no channel for id %d", __func__, id); |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1322 | if ((cc = channel_by_id(c->ctl_chan)) == NULL) |
| 1323 | fatal("%s: channel %d lacks control channel %d", __func__, |
| 1324 | id, c->ctl_chan); |
| 1325 | |
| 1326 | if (!success) { |
| 1327 | debug3("%s: sending failure reply", __func__); |
| 1328 | /* prepare reply */ |
| 1329 | buffer_init(&reply); |
| 1330 | buffer_put_int(&reply, MUX_S_FAILURE); |
| 1331 | buffer_put_int(&reply, cctx->rid); |
| 1332 | buffer_put_cstring(&reply, "Session open refused by peer"); |
| 1333 | goto done; |
| 1334 | } |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1335 | |
| 1336 | display = getenv("DISPLAY"); |
| 1337 | if (cctx->want_x_fwd && options.forward_x11 && display != NULL) { |
| 1338 | char *proto, *data; |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 1339 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1340 | /* Get reasonable local authentication information. */ |
| 1341 | client_x11_get_proto(display, options.xauth_location, |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 1342 | options.forward_x11_trusted, options.forward_x11_timeout, |
| 1343 | &proto, &data); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1344 | /* Request forwarding with authentication spoofing. */ |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 1345 | debug("Requesting X11 forwarding with authentication " |
| 1346 | "spoofing."); |
Damien Miller | 6d7b437 | 2011-06-23 08:31:57 +1000 | [diff] [blame] | 1347 | x11_request_forwarding_with_spoofing(id, display, proto, |
| 1348 | data, 1); |
| 1349 | client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN); |
| 1350 | /* XXX exit_on_forward_failure */ |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | if (cctx->want_agent_fwd && options.forward_agent) { |
| 1354 | debug("Requesting authentication agent forwarding."); |
| 1355 | channel_request_start(id, "auth-agent-req@openssh.com", 0); |
| 1356 | packet_send(); |
| 1357 | } |
| 1358 | |
| 1359 | client_session2_setup(id, cctx->want_tty, cctx->want_subsys, |
| 1360 | cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env); |
| 1361 | |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1362 | debug3("%s: sending success reply", __func__); |
| 1363 | /* prepare reply */ |
| 1364 | buffer_init(&reply); |
| 1365 | buffer_put_int(&reply, MUX_S_SESSION_OPENED); |
| 1366 | buffer_put_int(&reply, cctx->rid); |
| 1367 | buffer_put_int(&reply, c->self); |
| 1368 | |
| 1369 | done: |
| 1370 | /* Send reply */ |
| 1371 | buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply)); |
| 1372 | buffer_free(&reply); |
| 1373 | |
| 1374 | if (cc->mux_pause <= 0) |
| 1375 | fatal("%s: mux_pause %d", __func__, cc->mux_pause); |
| 1376 | cc->mux_pause = 0; /* start processing messages again */ |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1377 | c->open_confirm_ctx = NULL; |
| 1378 | buffer_free(&cctx->cmd); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1379 | free(cctx->term); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1380 | if (cctx->env != NULL) { |
| 1381 | for (i = 0; cctx->env[i] != NULL; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1382 | free(cctx->env[i]); |
| 1383 | free(cctx->env); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1384 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1385 | free(cctx); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1386 | } |
| 1387 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1388 | /* ** Multiplexing client support */ |
| 1389 | |
| 1390 | /* Exit signal handler */ |
| 1391 | static void |
| 1392 | control_client_sighandler(int signo) |
| 1393 | { |
| 1394 | muxclient_terminate = signo; |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * Relay signal handler - used to pass some signals from mux client to |
| 1399 | * mux master. |
| 1400 | */ |
| 1401 | static void |
| 1402 | control_client_sigrelay(int signo) |
| 1403 | { |
| 1404 | int save_errno = errno; |
| 1405 | |
| 1406 | if (muxserver_pid > 1) |
| 1407 | kill(muxserver_pid, signo); |
| 1408 | |
| 1409 | errno = save_errno; |
| 1410 | } |
| 1411 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1412 | static int |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1413 | mux_client_read(int fd, Buffer *b, u_int need) |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1414 | { |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1415 | u_int have; |
| 1416 | ssize_t len; |
| 1417 | u_char *p; |
| 1418 | struct pollfd pfd; |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1419 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1420 | pfd.fd = fd; |
| 1421 | pfd.events = POLLIN; |
| 1422 | p = buffer_append_space(b, need); |
| 1423 | for (have = 0; have < need; ) { |
| 1424 | if (muxclient_terminate) { |
| 1425 | errno = EINTR; |
| 1426 | return -1; |
| 1427 | } |
| 1428 | len = read(fd, p + have, need - have); |
| 1429 | if (len < 0) { |
| 1430 | switch (errno) { |
| 1431 | #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN) |
| 1432 | case EWOULDBLOCK: |
| 1433 | #endif |
| 1434 | case EAGAIN: |
| 1435 | (void)poll(&pfd, 1, -1); |
| 1436 | /* FALLTHROUGH */ |
| 1437 | case EINTR: |
| 1438 | continue; |
| 1439 | default: |
| 1440 | return -1; |
| 1441 | } |
| 1442 | } |
| 1443 | if (len == 0) { |
| 1444 | errno = EPIPE; |
| 1445 | return -1; |
| 1446 | } |
| 1447 | have += (u_int)len; |
| 1448 | } |
| 1449 | return 0; |
| 1450 | } |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1451 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1452 | static int |
| 1453 | mux_client_write_packet(int fd, Buffer *m) |
| 1454 | { |
| 1455 | Buffer queue; |
| 1456 | u_int have, need; |
| 1457 | int oerrno, len; |
| 1458 | u_char *ptr; |
| 1459 | struct pollfd pfd; |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 1460 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1461 | pfd.fd = fd; |
| 1462 | pfd.events = POLLOUT; |
| 1463 | buffer_init(&queue); |
| 1464 | buffer_put_string(&queue, buffer_ptr(m), buffer_len(m)); |
| 1465 | |
| 1466 | need = buffer_len(&queue); |
| 1467 | ptr = buffer_ptr(&queue); |
| 1468 | |
| 1469 | for (have = 0; have < need; ) { |
| 1470 | if (muxclient_terminate) { |
| 1471 | buffer_free(&queue); |
| 1472 | errno = EINTR; |
| 1473 | return -1; |
| 1474 | } |
| 1475 | len = write(fd, ptr + have, need - have); |
| 1476 | if (len < 0) { |
| 1477 | switch (errno) { |
| 1478 | #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN) |
| 1479 | case EWOULDBLOCK: |
| 1480 | #endif |
| 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; |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 1509 | const u_char *ptr; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1510 | int oerrno; |
| 1511 | |
| 1512 | buffer_init(&queue); |
| 1513 | if (mux_client_read(fd, &queue, 4) != 0) { |
| 1514 | if ((oerrno = errno) == EPIPE) |
Darren Tucker | 746e906 | 2013-06-06 08:20:13 +1000 | [diff] [blame] | 1515 | debug3("%s: read header failed: %s", __func__, |
| 1516 | strerror(errno)); |
| 1517 | buffer_free(&queue); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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)); |
Darren Tucker | 746e906 | 2013-06-06 08:20:13 +1000 | [diff] [blame] | 1525 | buffer_free(&queue); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1572 | free(name); |
| 1573 | free(value); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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 |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1673 | mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1674 | { |
| 1675 | Buffer m; |
| 1676 | char *e, *fwd_desc; |
| 1677 | u_int type, rid; |
| 1678 | |
| 1679 | fwd_desc = format_forward(ftype, fwd); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1680 | debug("Requesting %s %s", |
| 1681 | cancel_flag ? "cancellation of" : "forwarding of", fwd_desc); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1682 | free(fwd_desc); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1683 | |
| 1684 | buffer_init(&m); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1685 | buffer_put_int(&m, cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1686 | buffer_put_int(&m, muxclient_request_id); |
| 1687 | buffer_put_int(&m, ftype); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1688 | if (fwd->listen_path != NULL) { |
| 1689 | buffer_put_cstring(&m, fwd->listen_path); |
| 1690 | } else { |
| 1691 | buffer_put_cstring(&m, |
djm@openbsd.org | 46ac2ed | 2014-12-22 07:24:11 +0000 | [diff] [blame] | 1692 | fwd->listen_host == NULL ? "" : |
| 1693 | (*fwd->listen_host == '\0' ? "*" : fwd->listen_host)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1694 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1695 | buffer_put_int(&m, fwd->listen_port); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 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 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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; |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 1722 | case MUX_S_REMOTE_PORT: |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1723 | if (cancel_flag) |
| 1724 | fatal("%s: got MUX_S_REMOTE_PORT for cancel", __func__); |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 1725 | fwd->allocated_port = buffer_get_int(&m); |
| 1726 | logit("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; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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); |
Damien Miller | 445c9a5 | 2011-01-14 12:01:29 +1100 | [diff] [blame] | 1741 | error("%s: forwarding request failed: %s", __func__, e); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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 |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1754 | mux_client_forwards(int fd, int cancel_flag) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1755 | { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1756 | int i, ret = 0; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1757 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1758 | debug3("%s: %s forwardings: %d local, %d remote", __func__, |
| 1759 | cancel_flag ? "cancel" : "request", |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1760 | options.num_local_forwards, options.num_remote_forwards); |
| 1761 | |
| 1762 | /* XXX ExitOnForwardingFailure */ |
| 1763 | for (i = 0; i < options.num_local_forwards; i++) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1764 | if (mux_client_forward(fd, cancel_flag, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1765 | options.local_forwards[i].connect_port == 0 ? |
| 1766 | MUX_FWD_DYNAMIC : MUX_FWD_LOCAL, |
| 1767 | options.local_forwards + i) != 0) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1768 | ret = -1; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1769 | } |
| 1770 | for (i = 0; i < options.num_remote_forwards; i++) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1771 | if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1772 | options.remote_forwards + i) != 0) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1773 | ret = -1; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1774 | } |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 1775 | return ret; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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; |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 1785 | int devnull, rawmode; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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); |
Damien Miller | 445c9a5 | 2011-01-14 12:01:29 +1100 | [diff] [blame] | 1861 | error("Master refused session request: %s", e); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1862 | return -1; |
| 1863 | case MUX_S_FAILURE: |
| 1864 | e = buffer_get_string(&m, NULL); |
| 1865 | buffer_free(&m); |
Damien Miller | 445c9a5 | 2011-01-14 12:01:29 +1100 | [diff] [blame] | 1866 | error("%s: session request failed: %s", __func__, e); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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 | |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 1881 | rawmode = tty_flag; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1882 | if (tty_flag) |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1883 | enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 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: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1918 | e = buffer_get_string(&m, NULL); |
| 1919 | fatal("%s: master returned error: %s", __func__, e); |
| 1920 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | close(fd); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 1924 | if (rawmode) |
| 1925 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1926 | |
| 1927 | if (muxclient_terminate) { |
| 1928 | debug2("Exiting on signal %d", 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); |
Damien Miller | 445c9a5 | 2011-01-14 12:01:29 +1100 | [diff] [blame] | 2007 | fatal("Master refused stdio forwarding request: %s", e); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2008 | case MUX_S_FAILURE: |
| 2009 | e = buffer_get_string(&m, NULL); |
| 2010 | buffer_free(&m); |
Damien Miller | 357610d | 2014-07-18 15:04:10 +1000 | [diff] [blame] | 2011 | fatal("Stdio forwarding request failed: %s", e); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 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); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2037 | } |
| 2038 | |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 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 | |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2083 | /* Multiplex client main loop. */ |
| 2084 | void |
| 2085 | muxclient(const char *path) |
| 2086 | { |
| 2087 | struct sockaddr_un addr; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2088 | socklen_t sun_len; |
| 2089 | int sock; |
| 2090 | u_int pid; |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2091 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2092 | if (muxclient_command == 0) { |
| 2093 | if (stdio_forward_host != NULL) |
| 2094 | muxclient_command = SSHMUX_COMMAND_STDIO_FWD; |
| 2095 | else |
| 2096 | muxclient_command = SSHMUX_COMMAND_OPEN; |
| 2097 | } |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2098 | |
| 2099 | switch (options.control_master) { |
| 2100 | case SSHCTL_MASTER_AUTO: |
| 2101 | case SSHCTL_MASTER_AUTO_ASK: |
| 2102 | debug("auto-mux: Trying existing master"); |
| 2103 | /* FALLTHROUGH */ |
| 2104 | case SSHCTL_MASTER_NO: |
| 2105 | break; |
| 2106 | default: |
| 2107 | return; |
| 2108 | } |
| 2109 | |
| 2110 | memset(&addr, '\0', sizeof(addr)); |
| 2111 | addr.sun_family = AF_UNIX; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2112 | sun_len = offsetof(struct sockaddr_un, sun_path) + |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2113 | strlen(path) + 1; |
| 2114 | |
| 2115 | if (strlcpy(addr.sun_path, path, |
| 2116 | sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) |
| 2117 | fatal("ControlPath too long"); |
| 2118 | |
| 2119 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
| 2120 | fatal("%s socket(): %s", __func__, strerror(errno)); |
| 2121 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2122 | if (connect(sock, (struct sockaddr *)&addr, sun_len) == -1) { |
| 2123 | switch (muxclient_command) { |
| 2124 | case SSHMUX_COMMAND_OPEN: |
| 2125 | case SSHMUX_COMMAND_STDIO_FWD: |
| 2126 | break; |
| 2127 | default: |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2128 | fatal("Control socket connect(%.100s): %s", path, |
| 2129 | strerror(errno)); |
| 2130 | } |
Damien Miller | 603134e | 2010-09-24 22:07:55 +1000 | [diff] [blame] | 2131 | if (errno == ECONNREFUSED && |
| 2132 | options.control_master != SSHCTL_MASTER_NO) { |
| 2133 | debug("Stale control socket %.100s, unlinking", path); |
| 2134 | unlink(path); |
| 2135 | } else if (errno == ENOENT) { |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2136 | debug("Control socket \"%.100s\" does not exist", path); |
Damien Miller | 603134e | 2010-09-24 22:07:55 +1000 | [diff] [blame] | 2137 | } else { |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2138 | error("Control socket connect(%.100s): %s", path, |
| 2139 | strerror(errno)); |
| 2140 | } |
| 2141 | close(sock); |
| 2142 | return; |
| 2143 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2144 | set_nonblock(sock); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2145 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2146 | if (mux_client_hello_exchange(sock) != 0) { |
| 2147 | error("%s: master hello exchange failed", __func__); |
Darren Tucker | ca19bfe | 2008-06-13 10:24:03 +1000 | [diff] [blame] | 2148 | close(sock); |
Darren Tucker | ca19bfe | 2008-06-13 10:24:03 +1000 | [diff] [blame] | 2149 | return; |
| 2150 | } |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2151 | |
| 2152 | switch (muxclient_command) { |
| 2153 | case SSHMUX_COMMAND_ALIVE_CHECK: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2154 | if ((pid = mux_client_request_alive(sock)) == 0) |
| 2155 | fatal("%s: master alive check failed", __func__); |
| 2156 | fprintf(stderr, "Master running (pid=%d)\r\n", pid); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2157 | exit(0); |
| 2158 | case SSHMUX_COMMAND_TERMINATE: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2159 | mux_client_request_terminate(sock); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2160 | fprintf(stderr, "Exit request sent.\r\n"); |
| 2161 | exit(0); |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 2162 | case SSHMUX_COMMAND_FORWARD: |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2163 | if (mux_client_forwards(sock, 0) != 0) |
Damien Miller | 388f6fc | 2010-05-21 14:57:35 +1000 | [diff] [blame] | 2164 | fatal("%s: master forward request failed", __func__); |
| 2165 | exit(0); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2166 | case SSHMUX_COMMAND_OPEN: |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2167 | if (mux_client_forwards(sock, 0) != 0) { |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2168 | error("%s: master forward request failed", __func__); |
| 2169 | return; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 2170 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2171 | mux_client_request_session(sock); |
| 2172 | return; |
| 2173 | case SSHMUX_COMMAND_STDIO_FWD: |
| 2174 | mux_client_request_stdio_fwd(sock); |
| 2175 | exit(0); |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 2176 | case SSHMUX_COMMAND_STOP: |
| 2177 | mux_client_request_stop_listening(sock); |
| 2178 | fprintf(stderr, "Stop listening request sent.\r\n"); |
| 2179 | exit(0); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2180 | case SSHMUX_COMMAND_CANCEL_FWD: |
| 2181 | if (mux_client_forwards(sock, 1) != 0) |
| 2182 | error("%s: master cancel forward request failed", |
| 2183 | __func__); |
| 2184 | exit(0); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2185 | default: |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 2186 | fatal("unrecognised muxclient_command %d", muxclient_command); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2187 | } |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2188 | } |