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