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