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