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