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