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