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