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