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