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