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