1 /* $OpenBSD: mux.c,v 1.23 2010/10/12 02:22:24 dtucker Exp $ */
3 * Copyright (c) 2002-2008 Damien Miller <djm@openbsd.org>
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.
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.
18 /* ssh session multiplexing support */
22 * - Better signalling from master to slave, especially passing of
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
35 #include <sys/types.h>
36 #include <sys/param.h>
38 #include <sys/socket.h>
57 # ifdef HAVE_SYS_POLL_H
58 # include <sys/poll.h>
70 #include "openbsd-compat/sys-queue.h"
75 #include "pathnames.h"
82 #include "monitor_fdpass.h"
86 #include "clientloop.h"
90 extern int force_tty_flag
;
91 extern Options options
;
92 extern int stdin_null_flag
;
94 extern int subsystem_flag
;
95 extern Buffer command
;
96 extern volatile sig_atomic_t quit_pending
;
97 extern char *stdio_forward_host
;
98 extern int stdio_forward_port
;
100 /* Context for session open confirmation callback */
101 struct mux_session_confirm_ctx
{
105 u_int want_agent_fwd
;
113 /* Context for global channel callback */
114 struct mux_channel_confirm_ctx
{
115 u_int cid
; /* channel id */
116 u_int rid
; /* request id */
117 int fid
; /* forward id */
120 /* fd to control socket */
121 int muxserver_sock
= -1;
123 /* client request id */
124 u_int muxclient_request_id
= 0;
126 /* Multiplexing control command */
127 u_int muxclient_command
= 0;
129 /* Set when signalled. */
130 static volatile sig_atomic_t muxclient_terminate
= 0;
132 /* PID of multiplex server */
133 static u_int muxserver_pid
= 0;
135 static Channel
*mux_listener_channel
= NULL
;
137 struct mux_master_state
{
141 /* mux protocol messages */
142 #define MUX_MSG_HELLO 0x00000001
143 #define MUX_C_NEW_SESSION 0x10000002
144 #define MUX_C_ALIVE_CHECK 0x10000004
145 #define MUX_C_TERMINATE 0x10000005
146 #define MUX_C_OPEN_FWD 0x10000006
147 #define MUX_C_CLOSE_FWD 0x10000007
148 #define MUX_C_NEW_STDIO_FWD 0x10000008
149 #define MUX_S_OK 0x80000001
150 #define MUX_S_PERMISSION_DENIED 0x80000002
151 #define MUX_S_FAILURE 0x80000003
152 #define MUX_S_EXIT_MESSAGE 0x80000004
153 #define MUX_S_ALIVE 0x80000005
154 #define MUX_S_SESSION_OPENED 0x80000006
155 #define MUX_S_REMOTE_PORT 0x80000007
157 /* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
158 #define MUX_FWD_LOCAL 1
159 #define MUX_FWD_REMOTE 2
160 #define MUX_FWD_DYNAMIC 3
162 static void mux_session_confirm(int, int, void *);
164 static int process_mux_master_hello(u_int
, Channel
*, Buffer
*, Buffer
*);
165 static int process_mux_new_session(u_int
, Channel
*, Buffer
*, Buffer
*);
166 static int process_mux_alive_check(u_int
, Channel
*, Buffer
*, Buffer
*);
167 static int process_mux_terminate(u_int
, Channel
*, Buffer
*, Buffer
*);
168 static int process_mux_open_fwd(u_int
, Channel
*, Buffer
*, Buffer
*);
169 static int process_mux_close_fwd(u_int
, Channel
*, Buffer
*, Buffer
*);
170 static int process_mux_stdio_fwd(u_int
, Channel
*, Buffer
*, Buffer
*);
172 static const struct {
174 int (*handler
)(u_int
, Channel
*, Buffer
*, Buffer
*);
175 } mux_master_handlers
[] = {
176 { MUX_MSG_HELLO
, process_mux_master_hello
},
177 { MUX_C_NEW_SESSION
, process_mux_new_session
},
178 { MUX_C_ALIVE_CHECK
, process_mux_alive_check
},
179 { MUX_C_TERMINATE
, process_mux_terminate
},
180 { MUX_C_OPEN_FWD
, process_mux_open_fwd
},
181 { MUX_C_CLOSE_FWD
, process_mux_close_fwd
},
182 { MUX_C_NEW_STDIO_FWD
, process_mux_stdio_fwd
},
186 /* Cleanup callback fired on closure of mux slave _session_ channel */
189 mux_master_session_cleanup_cb(int cid
, void *unused
)
191 Channel
*cc
, *c
= channel_by_id(cid
);
193 debug3("%s: entering for channel %d", __func__
, cid
);
195 fatal("%s: channel_by_id(%i) == NULL", __func__
, cid
);
196 if (c
->ctl_chan
!= -1) {
197 if ((cc
= channel_by_id(c
->ctl_chan
)) == NULL
)
198 fatal("%s: channel %d missing control channel %d",
199 __func__
, c
->self
, c
->ctl_chan
);
202 chan_rcvd_oclose(cc
);
204 channel_cancel_cleanup(c
->self
);
207 /* Cleanup callback fired on closure of mux slave _control_ channel */
210 mux_master_control_cleanup_cb(int cid
, void *unused
)
212 Channel
*sc
, *c
= channel_by_id(cid
);
214 debug3("%s: entering for channel %d", __func__
, cid
);
216 fatal("%s: channel_by_id(%i) == NULL", __func__
, cid
);
217 if (c
->remote_id
!= -1) {
218 if ((sc
= channel_by_id(c
->remote_id
)) == NULL
)
219 fatal("%s: channel %d missing session channel %d",
220 __func__
, c
->self
, c
->remote_id
);
223 if (sc
->type
!= SSH_CHANNEL_OPEN
) {
224 debug2("%s: channel %d: not open", __func__
, sc
->self
);
227 if (sc
->istate
== CHAN_INPUT_OPEN
)
228 chan_read_failed(sc
);
229 if (sc
->ostate
== CHAN_OUTPUT_OPEN
)
230 chan_write_failed(sc
);
233 channel_cancel_cleanup(c
->self
);
236 /* Check mux client environment variables before passing them to mux master. */
238 env_permitted(char *env
)
241 char name
[1024], *cp
;
243 if ((cp
= strchr(env
, '=')) == NULL
|| cp
== env
)
245 ret
= snprintf(name
, sizeof(name
), "%.*s", (int)(cp
- env
), env
);
246 if (ret
<= 0 || (size_t)ret
>= sizeof(name
)) {
247 error("env_permitted: name '%.100s...' too long", env
);
251 for (i
= 0; i
< options
.num_send_env
; i
++)
252 if (match_pattern(name
, options
.send_env
[i
]))
258 /* Mux master protocol message handlers */
261 process_mux_master_hello(u_int rid
, Channel
*c
, Buffer
*m
, Buffer
*r
)
264 struct mux_master_state
*state
= (struct mux_master_state
*)c
->mux_ctx
;
267 fatal("%s: channel %d: c->mux_ctx == NULL", __func__
, c
->self
);
268 if (state
->hello_rcvd
) {
269 error("%s: HELLO received twice", __func__
);
272 if (buffer_get_int_ret(&ver
, m
) != 0) {
274 error("%s: malformed message", __func__
);
277 if (ver
!= SSHMUX_VER
) {
278 error("Unsupported multiplexing protocol version %d "
279 "(expected %d)", ver
, SSHMUX_VER
);
282 debug2("%s: channel %d slave version %u", __func__
, c
->self
, ver
);
284 /* No extensions are presently defined */
285 while (buffer_len(m
) > 0) {
286 char *name
= buffer_get_string_ret(m
, NULL
);
287 char *value
= buffer_get_string_ret(m
, NULL
);
289 if (name
== NULL
|| value
== NULL
) {
294 debug2("Unrecognised slave extension \"%s\"", name
);
298 state
->hello_rcvd
= 1;
303 process_mux_new_session(u_int rid
, Channel
*c
, Buffer
*m
, Buffer
*r
)
306 struct mux_session_confirm_ctx
*cctx
;
307 char *reserved
, *cmd
, *cp
;
308 u_int i
, j
, len
, env_len
, escape_char
, window
, packetmax
;
311 /* Reply for SSHMUX_COMMAND_OPEN */
312 cctx
= xcalloc(1, sizeof(*cctx
));
315 cmd
= reserved
= NULL
;
316 if ((reserved
= buffer_get_string_ret(m
, NULL
)) == NULL
||
317 buffer_get_int_ret(&cctx
->want_tty
, m
) != 0 ||
318 buffer_get_int_ret(&cctx
->want_x_fwd
, m
) != 0 ||
319 buffer_get_int_ret(&cctx
->want_agent_fwd
, m
) != 0 ||
320 buffer_get_int_ret(&cctx
->want_subsys
, m
) != 0 ||
321 buffer_get_int_ret(&escape_char
, m
) != 0 ||
322 (cctx
->term
= buffer_get_string_ret(m
, &len
)) == NULL
||
323 (cmd
= buffer_get_string_ret(m
, &len
)) == NULL
) {
327 if (reserved
!= NULL
)
329 if (cctx
->term
!= NULL
)
331 error("%s: malformed message", __func__
);
339 while (buffer_len(m
) > 0) {
340 #define MUX_MAX_ENV_VARS 4096
341 if ((cp
= buffer_get_string_ret(m
, &len
)) == NULL
) {
345 if (!env_permitted(cp
)) {
349 cctx
->env
= xrealloc(cctx
->env
, env_len
+ 2,
351 cctx
->env
[env_len
++] = cp
;
352 cctx
->env
[env_len
] = NULL
;
353 if (env_len
> MUX_MAX_ENV_VARS
) {
354 error(">%d environment variables received, ignoring "
355 "additional", MUX_MAX_ENV_VARS
);
360 debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
361 "term \"%s\", cmd \"%s\", env %u", __func__
, c
->self
,
362 cctx
->want_tty
, cctx
->want_x_fwd
, cctx
->want_agent_fwd
,
363 cctx
->want_subsys
, cctx
->term
, cmd
, env_len
);
365 buffer_init(&cctx
->cmd
);
366 buffer_append(&cctx
->cmd
, cmd
, strlen(cmd
));
370 /* Gather fds from client */
371 for(i
= 0; i
< 3; i
++) {
372 if ((new_fd
[i
] = mm_receive_fd(c
->sock
)) == -1) {
373 error("%s: failed to receive fd %d from slave",
375 for (j
= 0; j
< i
; j
++)
377 for (j
= 0; j
< env_len
; j
++)
382 buffer_free(&cctx
->cmd
);
386 buffer_put_int(r
, MUX_S_FAILURE
);
387 buffer_put_int(r
, rid
);
388 buffer_put_cstring(r
,
389 "did not receive file descriptors");
394 debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__
,
395 new_fd
[0], new_fd
[1], new_fd
[2]);
397 /* XXX support multiple child sessions in future */
398 if (c
->remote_id
!= -1) {
399 debug2("%s: session already open", __func__
);
401 buffer_put_int(r
, MUX_S_FAILURE
);
402 buffer_put_int(r
, rid
);
403 buffer_put_cstring(r
, "Multiple sessions not supported");
410 for (i
= 0; i
< env_len
; i
++)
414 buffer_free(&cctx
->cmd
);
418 if (options
.control_master
== SSHCTL_MASTER_ASK
||
419 options
.control_master
== SSHCTL_MASTER_AUTO_ASK
) {
420 if (!ask_permission("Allow shared connection to %s? ", host
)) {
421 debug2("%s: session refused by user", __func__
);
423 buffer_put_int(r
, MUX_S_PERMISSION_DENIED
);
424 buffer_put_int(r
, rid
);
425 buffer_put_cstring(r
, "Permission denied");
430 /* Try to pick up ttymodes from client before it goes raw */
431 if (cctx
->want_tty
&& tcgetattr(new_fd
[0], &cctx
->tio
) == -1)
432 error("%s: tcgetattr: %s", __func__
, strerror(errno
));
434 /* enable nonblocking unless tty */
435 if (!isatty(new_fd
[0]))
436 set_nonblock(new_fd
[0]);
437 if (!isatty(new_fd
[1]))
438 set_nonblock(new_fd
[1]);
439 if (!isatty(new_fd
[2]))
440 set_nonblock(new_fd
[2]);
442 window
= CHAN_SES_WINDOW_DEFAULT
;
443 packetmax
= CHAN_SES_PACKET_DEFAULT
;
444 if (cctx
->want_tty
) {
449 nc
= channel_new("session", SSH_CHANNEL_OPENING
,
450 new_fd
[0], new_fd
[1], new_fd
[2], window
, packetmax
,
451 CHAN_EXTENDED_WRITE
, "client-session", /*nonblock*/0);
453 nc
->ctl_chan
= c
->self
; /* link session -> control channel */
454 c
->remote_id
= nc
->self
; /* link control -> session channel */
456 if (cctx
->want_tty
&& escape_char
!= 0xffffffff) {
457 channel_register_filter(nc
->self
,
458 client_simple_escape_filter
, NULL
,
459 client_filter_cleanup
,
460 client_new_escape_filter_ctx((int)escape_char
));
463 debug2("%s: channel_new: %d linked to control channel %d",
464 __func__
, nc
->self
, nc
->ctl_chan
);
466 channel_send_open(nc
->self
);
467 channel_register_open_confirm(nc
->self
, mux_session_confirm
, cctx
);
468 c
->mux_pause
= 1; /* stop handling messages until open_confirm done */
469 channel_register_cleanup(nc
->self
, mux_master_session_cleanup_cb
, 1);
471 /* reply is deferred, sent by mux_session_confirm */
476 process_mux_alive_check(u_int rid
, Channel
*c
, Buffer
*m
, Buffer
*r
)
478 debug2("%s: channel %d: alive check", __func__
, c
->self
);
481 buffer_put_int(r
, MUX_S_ALIVE
);
482 buffer_put_int(r
, rid
);
483 buffer_put_int(r
, (u_int
)getpid());
489 process_mux_terminate(u_int rid
, Channel
*c
, Buffer
*m
, Buffer
*r
)
491 debug2("%s: channel %d: terminate request", __func__
, c
->self
);
493 if (options
.control_master
== SSHCTL_MASTER_ASK
||
494 options
.control_master
== SSHCTL_MASTER_AUTO_ASK
) {
495 if (!ask_permission("Terminate shared connection to %s? ",
497 debug2("%s: termination refused by user", __func__
);
498 buffer_put_int(r
, MUX_S_PERMISSION_DENIED
);
499 buffer_put_int(r
, rid
);
500 buffer_put_cstring(r
, "Permission denied");
506 buffer_put_int(r
, MUX_S_OK
);
507 buffer_put_int(r
, rid
);
508 /* XXX exit happens too soon - message never makes it to client */
513 format_forward(u_int ftype
, Forward
*fwd
)
519 xasprintf(&ret
, "local forward %.200s:%d -> %.200s:%d",
520 (fwd
->listen_host
== NULL
) ?
521 (options
.gateway_ports
? "*" : "LOCALHOST") :
522 fwd
->listen_host
, fwd
->listen_port
,
523 fwd
->connect_host
, fwd
->connect_port
);
525 case MUX_FWD_DYNAMIC
:
526 xasprintf(&ret
, "dynamic forward %.200s:%d -> *",
527 (fwd
->listen_host
== NULL
) ?
528 (options
.gateway_ports
? "*" : "LOCALHOST") :
529 fwd
->listen_host
, fwd
->listen_port
);
532 xasprintf(&ret
, "remote forward %.200s:%d -> %.200s:%d",
533 (fwd
->listen_host
== NULL
) ?
534 "LOCALHOST" : fwd
->listen_host
,
536 fwd
->connect_host
, fwd
->connect_port
);
539 fatal("%s: unknown forward type %u", __func__
, ftype
);
545 compare_host(const char *a
, const char *b
)
547 if (a
== NULL
&& b
== NULL
)
549 if (a
== NULL
|| b
== NULL
)
551 return strcmp(a
, b
) == 0;
555 compare_forward(Forward
*a
, Forward
*b
)
557 if (!compare_host(a
->listen_host
, b
->listen_host
))
559 if (a
->listen_port
!= b
->listen_port
)
561 if (!compare_host(a
->connect_host
, b
->connect_host
))
563 if (a
->connect_port
!= b
->connect_port
)
570 mux_confirm_remote_forward(int type
, u_int32_t seq
, void *ctxt
)
572 struct mux_channel_confirm_ctx
*fctx
= ctxt
;
573 char *failmsg
= NULL
;
578 if ((c
= channel_by_id(fctx
->cid
)) == NULL
) {
579 /* no channel for reply */
580 error("%s: unknown channel", __func__
);
584 if (fctx
->fid
>= options
.num_remote_forwards
) {
585 xasprintf(&failmsg
, "unknown forwarding id %d", fctx
->fid
);
588 rfwd
= &options
.remote_forwards
[fctx
->fid
];
589 debug("%s: %s for: listen %d, connect %s:%d", __func__
,
590 type
== SSH2_MSG_REQUEST_SUCCESS
? "success" : "failure",
591 rfwd
->listen_port
, rfwd
->connect_host
, rfwd
->connect_port
);
592 if (type
== SSH2_MSG_REQUEST_SUCCESS
) {
593 if (rfwd
->listen_port
== 0) {
594 rfwd
->allocated_port
= packet_get_int();
595 logit("Allocated port %u for mux remote forward"
596 " to %s:%d", rfwd
->allocated_port
,
597 rfwd
->connect_host
, rfwd
->connect_port
);
598 buffer_put_int(&out
, MUX_S_REMOTE_PORT
);
599 buffer_put_int(&out
, fctx
->rid
);
600 buffer_put_int(&out
, rfwd
->allocated_port
);
602 buffer_put_int(&out
, MUX_S_OK
);
603 buffer_put_int(&out
, fctx
->rid
);
607 xasprintf(&failmsg
, "remote port forwarding failed for "
608 "listen port %d", rfwd
->listen_port
);
611 error("%s: %s", __func__
, failmsg
);
612 buffer_put_int(&out
, MUX_S_FAILURE
);
613 buffer_put_int(&out
, fctx
->rid
);
614 buffer_put_cstring(&out
, failmsg
);
617 buffer_put_string(&c
->output
, buffer_ptr(&out
), buffer_len(&out
));
619 if (c
->mux_pause
<= 0)
620 fatal("%s: mux_pause %d", __func__
, c
->mux_pause
);
621 c
->mux_pause
= 0; /* start processing messages again */
625 process_mux_open_fwd(u_int rid
, Channel
*c
, Buffer
*m
, Buffer
*r
)
628 char *fwd_desc
= NULL
;
630 int i
, ret
= 0, freefwd
= 1;
632 fwd
.listen_host
= fwd
.connect_host
= NULL
;
633 if (buffer_get_int_ret(&ftype
, m
) != 0 ||
634 (fwd
.listen_host
= buffer_get_string_ret(m
, NULL
)) == NULL
||
635 buffer_get_int_ret(&fwd
.listen_port
, m
) != 0 ||
636 (fwd
.connect_host
= buffer_get_string_ret(m
, NULL
)) == NULL
||
637 buffer_get_int_ret(&fwd
.connect_port
, m
) != 0) {
638 error("%s: malformed message", __func__
);
643 if (*fwd
.listen_host
== '\0') {
644 xfree(fwd
.listen_host
);
645 fwd
.listen_host
= NULL
;
647 if (*fwd
.connect_host
== '\0') {
648 xfree(fwd
.connect_host
);
649 fwd
.connect_host
= NULL
;
652 debug2("%s: channel %d: request %s", __func__
, c
->self
,
653 (fwd_desc
= format_forward(ftype
, &fwd
)));
655 if (ftype
!= MUX_FWD_LOCAL
&& ftype
!= MUX_FWD_REMOTE
&&
656 ftype
!= MUX_FWD_DYNAMIC
) {
657 logit("%s: invalid forwarding type %u", __func__
, ftype
);
660 xfree(fwd
.listen_host
);
661 if (fwd
.connect_host
)
662 xfree(fwd
.connect_host
);
663 buffer_put_int(r
, MUX_S_FAILURE
);
664 buffer_put_int(r
, rid
);
665 buffer_put_cstring(r
, "Invalid forwarding request");
668 if (fwd
.listen_port
>= 65536) {
669 logit("%s: invalid listen port %u", __func__
,
673 if (fwd
.connect_port
>= 65536 || (ftype
!= MUX_FWD_DYNAMIC
&&
674 ftype
!= MUX_FWD_REMOTE
&& fwd
.connect_port
== 0)) {
675 logit("%s: invalid connect port %u", __func__
,
679 if (ftype
!= MUX_FWD_DYNAMIC
&& fwd
.connect_host
== NULL
) {
680 logit("%s: missing connect host", __func__
);
684 /* Skip forwards that have already been requested */
687 case MUX_FWD_DYNAMIC
:
688 for (i
= 0; i
< options
.num_local_forwards
; i
++) {
689 if (compare_forward(&fwd
,
690 options
.local_forwards
+ i
)) {
692 debug2("%s: found existing forwarding",
694 buffer_put_int(r
, MUX_S_OK
);
695 buffer_put_int(r
, rid
);
701 for (i
= 0; i
< options
.num_remote_forwards
; i
++) {
702 if (compare_forward(&fwd
,
703 options
.remote_forwards
+ i
)) {
704 if (fwd
.listen_port
!= 0)
706 debug2("%s: found allocated port",
708 buffer_put_int(r
, MUX_S_REMOTE_PORT
);
709 buffer_put_int(r
, rid
);
711 options
.remote_forwards
[i
].allocated_port
);
718 if (options
.control_master
== SSHCTL_MASTER_ASK
||
719 options
.control_master
== SSHCTL_MASTER_AUTO_ASK
) {
720 if (!ask_permission("Open %s on %s?", fwd_desc
, host
)) {
721 debug2("%s: forwarding refused by user", __func__
);
722 buffer_put_int(r
, MUX_S_PERMISSION_DENIED
);
723 buffer_put_int(r
, rid
);
724 buffer_put_cstring(r
, "Permission denied");
729 if (ftype
== MUX_FWD_LOCAL
|| ftype
== MUX_FWD_DYNAMIC
) {
730 if (channel_setup_local_fwd_listener(fwd
.listen_host
,
731 fwd
.listen_port
, fwd
.connect_host
, fwd
.connect_port
,
732 options
.gateway_ports
) < 0) {
734 logit("slave-requested %s failed", fwd_desc
);
735 buffer_put_int(r
, MUX_S_FAILURE
);
736 buffer_put_int(r
, rid
);
737 buffer_put_cstring(r
, "Port forwarding failed");
740 add_local_forward(&options
, &fwd
);
743 struct mux_channel_confirm_ctx
*fctx
;
745 if (channel_request_remote_forwarding(fwd
.listen_host
,
746 fwd
.listen_port
, fwd
.connect_host
, fwd
.connect_port
) < 0)
748 add_remote_forward(&options
, &fwd
);
749 fctx
= xcalloc(1, sizeof(*fctx
));
752 fctx
->fid
= options
.num_remote_forwards
- 1;
753 client_register_global_confirm(mux_confirm_remote_forward
,
756 c
->mux_pause
= 1; /* wait for mux_confirm_remote_forward */
757 /* delayed reply in mux_confirm_remote_forward */
760 buffer_put_int(r
, MUX_S_OK
);
761 buffer_put_int(r
, rid
);
763 if (fwd_desc
!= NULL
)
766 if (fwd
.listen_host
!= NULL
)
767 xfree(fwd
.listen_host
);
768 if (fwd
.connect_host
!= NULL
)
769 xfree(fwd
.connect_host
);
775 process_mux_close_fwd(u_int rid
, Channel
*c
, Buffer
*m
, Buffer
*r
)
778 char *fwd_desc
= NULL
;
782 fwd
.listen_host
= fwd
.connect_host
= NULL
;
783 if (buffer_get_int_ret(&ftype
, m
) != 0 ||
784 (fwd
.listen_host
= buffer_get_string_ret(m
, NULL
)) == NULL
||
785 buffer_get_int_ret(&fwd
.listen_port
, m
) != 0 ||
786 (fwd
.connect_host
= buffer_get_string_ret(m
, NULL
)) == NULL
||
787 buffer_get_int_ret(&fwd
.connect_port
, m
) != 0) {
788 error("%s: malformed message", __func__
);
793 if (*fwd
.listen_host
== '\0') {
794 xfree(fwd
.listen_host
);
795 fwd
.listen_host
= NULL
;
797 if (*fwd
.connect_host
== '\0') {
798 xfree(fwd
.connect_host
);
799 fwd
.connect_host
= NULL
;
802 debug2("%s: channel %d: request %s", __func__
, c
->self
,
803 (fwd_desc
= format_forward(ftype
, &fwd
)));
805 /* XXX implement this */
806 buffer_put_int(r
, MUX_S_FAILURE
);
807 buffer_put_int(r
, rid
);
808 buffer_put_cstring(r
, "unimplemented");
811 if (fwd_desc
!= NULL
)
813 if (fwd
.listen_host
!= NULL
)
814 xfree(fwd
.listen_host
);
815 if (fwd
.connect_host
!= NULL
)
816 xfree(fwd
.connect_host
);
822 process_mux_stdio_fwd(u_int rid
, Channel
*c
, Buffer
*m
, Buffer
*r
)
825 char *reserved
, *chost
;
829 chost
= reserved
= NULL
;
830 if ((reserved
= buffer_get_string_ret(m
, NULL
)) == NULL
||
831 (chost
= buffer_get_string_ret(m
, NULL
)) == NULL
||
832 buffer_get_int_ret(&cport
, m
) != 0) {
833 if (reserved
!= NULL
)
837 error("%s: malformed message", __func__
);
842 debug2("%s: channel %d: request stdio fwd to %s:%u",
843 __func__
, c
->self
, chost
, cport
);
845 /* Gather fds from client */
846 for(i
= 0; i
< 2; i
++) {
847 if ((new_fd
[i
] = mm_receive_fd(c
->sock
)) == -1) {
848 error("%s: failed to receive fd %d from slave",
850 for (j
= 0; j
< i
; j
++)
855 buffer_put_int(r
, MUX_S_FAILURE
);
856 buffer_put_int(r
, rid
);
857 buffer_put_cstring(r
,
858 "did not receive file descriptors");
863 debug3("%s: got fds stdin %d, stdout %d", __func__
,
864 new_fd
[0], new_fd
[1]);
866 /* XXX support multiple child sessions in future */
867 if (c
->remote_id
!= -1) {
868 debug2("%s: session already open", __func__
);
870 buffer_put_int(r
, MUX_S_FAILURE
);
871 buffer_put_int(r
, rid
);
872 buffer_put_cstring(r
, "Multiple sessions not supported");
880 if (options
.control_master
== SSHCTL_MASTER_ASK
||
881 options
.control_master
== SSHCTL_MASTER_AUTO_ASK
) {
882 if (!ask_permission("Allow forward to %s:%u? ",
884 debug2("%s: stdio fwd refused by user", __func__
);
886 buffer_put_int(r
, MUX_S_PERMISSION_DENIED
);
887 buffer_put_int(r
, rid
);
888 buffer_put_cstring(r
, "Permission denied");
893 /* enable nonblocking unless tty */
894 if (!isatty(new_fd
[0]))
895 set_nonblock(new_fd
[0]);
896 if (!isatty(new_fd
[1]))
897 set_nonblock(new_fd
[1]);
899 nc
= channel_connect_stdio_fwd(chost
, cport
, new_fd
[0], new_fd
[1]);
901 nc
->ctl_chan
= c
->self
; /* link session -> control channel */
902 c
->remote_id
= nc
->self
; /* link control -> session channel */
904 debug2("%s: channel_new: %d linked to control channel %d",
905 __func__
, nc
->self
, nc
->ctl_chan
);
907 channel_register_cleanup(nc
->self
, mux_master_session_cleanup_cb
, 1);
910 /* XXX defer until channel confirmed */
911 buffer_put_int(r
, MUX_S_SESSION_OPENED
);
912 buffer_put_int(r
, rid
);
913 buffer_put_int(r
, nc
->self
);
918 /* Channel callbacks fired on read/write from mux slave fd */
920 mux_master_read_cb(Channel
*c
)
922 struct mux_master_state
*state
= (struct mux_master_state
*)c
->mux_ctx
;
925 u_int type
, rid
, have
, i
;
929 if (c
->mux_ctx
== NULL
) {
930 state
= xcalloc(1, sizeof(*state
));
932 channel_register_cleanup(c
->self
,
933 mux_master_control_cleanup_cb
, 0);
937 buffer_put_int(&out
, MUX_MSG_HELLO
);
938 buffer_put_int(&out
, SSHMUX_VER
);
940 buffer_put_string(&c
->output
, buffer_ptr(&out
),
943 debug3("%s: channel %d: hello sent", __func__
, c
->self
);
950 /* Channel code ensures that we receive whole packets */
951 if ((ptr
= buffer_get_string_ptr_ret(&c
->input
, &have
)) == NULL
) {
953 error("%s: malformed message", __func__
);
956 buffer_append(&in
, ptr
, have
);
958 if (buffer_get_int_ret(&type
, &in
) != 0)
960 debug3("%s: channel %d packet type 0x%08x len %u",
961 __func__
, c
->self
, type
, buffer_len(&in
));
963 if (type
== MUX_MSG_HELLO
)
966 if (!state
->hello_rcvd
) {
967 error("%s: expected MUX_MSG_HELLO(0x%08x), "
968 "received 0x%08x", __func__
, MUX_MSG_HELLO
, type
);
971 if (buffer_get_int_ret(&rid
, &in
) != 0)
975 for (i
= 0; mux_master_handlers
[i
].handler
!= NULL
; i
++) {
976 if (type
== mux_master_handlers
[i
].type
) {
977 ret
= mux_master_handlers
[i
].handler(rid
, c
, &in
, &out
);
981 if (mux_master_handlers
[i
].handler
== NULL
) {
982 error("%s: unsupported mux message 0x%08x", __func__
, type
);
983 buffer_put_int(&out
, MUX_S_FAILURE
);
984 buffer_put_int(&out
, rid
);
985 buffer_put_cstring(&out
, "unsupported request");
988 /* Enqueue reply packet */
989 if (buffer_len(&out
) != 0) {
990 buffer_put_string(&c
->output
, buffer_ptr(&out
),
1000 mux_exit_message(Channel
*c
, int exitval
)
1005 debug3("%s: channel %d: exit message, evitval %d", __func__
, c
->self
,
1008 if ((mux_chan
= channel_by_id(c
->ctl_chan
)) == NULL
)
1009 fatal("%s: channel %d missing mux channel %d",
1010 __func__
, c
->self
, c
->ctl_chan
);
1012 /* Append exit message packet to control socket output queue */
1014 buffer_put_int(&m
, MUX_S_EXIT_MESSAGE
);
1015 buffer_put_int(&m
, c
->self
);
1016 buffer_put_int(&m
, exitval
);
1018 buffer_put_string(&mux_chan
->output
, buffer_ptr(&m
), buffer_len(&m
));
1022 /* Prepare a mux master to listen on a Unix domain socket. */
1024 muxserver_listen(void)
1026 struct sockaddr_un addr
;
1029 char *orig_control_path
= options
.control_path
;
1033 if (options
.control_path
== NULL
||
1034 options
.control_master
== SSHCTL_MASTER_NO
)
1037 debug("setting up multiplex master socket");
1040 * Use a temporary path before listen so we can pseudo-atomically
1041 * establish the listening socket in its final location to avoid
1042 * other processes racing in between bind() and listen() and hitting
1043 * an unready socket.
1045 for (i
= 0; i
< sizeof(rbuf
) - 1; i
++) {
1046 r
= arc4random_uniform(26+26+10);
1047 rbuf
[i
] = (r
< 26) ? 'a' + r
:
1048 (r
< 26*2) ? 'A' + r
- 26 :
1051 rbuf
[sizeof(rbuf
) - 1] = '\0';
1052 options
.control_path
= NULL
;
1053 xasprintf(&options
.control_path
, "%s.%s", orig_control_path
, rbuf
);
1054 debug3("%s: temporary control path %s", __func__
, options
.control_path
);
1056 memset(&addr
, '\0', sizeof(addr
));
1057 addr
.sun_family
= AF_UNIX
;
1058 sun_len
= offsetof(struct sockaddr_un
, sun_path
) +
1059 strlen(options
.control_path
) + 1;
1061 if (strlcpy(addr
.sun_path
, options
.control_path
,
1062 sizeof(addr
.sun_path
)) >= sizeof(addr
.sun_path
))
1063 fatal("ControlPath too long");
1065 if ((muxserver_sock
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
1066 fatal("%s socket(): %s", __func__
, strerror(errno
));
1068 old_umask
= umask(0177);
1069 if (bind(muxserver_sock
, (struct sockaddr
*)&addr
, sun_len
) == -1) {
1070 muxserver_sock
= -1;
1071 if (errno
== EINVAL
|| errno
== EADDRINUSE
) {
1072 error("ControlSocket %s already exists, "
1073 "disabling multiplexing", options
.control_path
);
1075 close(muxserver_sock
);
1076 muxserver_sock
= -1;
1077 xfree(options
.control_path
);
1078 options
.control_path
= NULL
;
1079 options
.control_master
= SSHCTL_MASTER_NO
;
1082 fatal("%s bind(): %s", __func__
, strerror(errno
));
1086 if (listen(muxserver_sock
, 64) == -1)
1087 fatal("%s listen(): %s", __func__
, strerror(errno
));
1089 /* Now atomically "move" the mux socket into position */
1090 if (link(options
.control_path
, orig_control_path
) != 0) {
1091 if (errno
!= EEXIST
) {
1092 fatal("%s: link mux listener %s => %s: %s", __func__
,
1093 options
.control_path
, orig_control_path
,
1096 error("ControlSocket %s already exists, disabling multiplexing",
1098 xfree(orig_control_path
);
1099 unlink(options
.control_path
);
1100 goto disable_mux_master
;
1102 unlink(options
.control_path
);
1103 xfree(options
.control_path
);
1104 options
.control_path
= orig_control_path
;
1106 set_nonblock(muxserver_sock
);
1108 mux_listener_channel
= channel_new("mux listener",
1109 SSH_CHANNEL_MUX_LISTENER
, muxserver_sock
, muxserver_sock
, -1,
1110 CHAN_TCP_WINDOW_DEFAULT
, CHAN_TCP_PACKET_DEFAULT
,
1111 0, options
.control_path
, 1);
1112 mux_listener_channel
->mux_rcb
= mux_master_read_cb
;
1113 debug3("%s: mux listener channel %d fd %d", __func__
,
1114 mux_listener_channel
->self
, mux_listener_channel
->sock
);
1117 /* Callback on open confirmation in mux master for a mux client session. */
1119 mux_session_confirm(int id
, int success
, void *arg
)
1121 struct mux_session_confirm_ctx
*cctx
= arg
;
1122 const char *display
;
1128 fatal("%s: cctx == NULL", __func__
);
1129 if ((c
= channel_by_id(id
)) == NULL
)
1130 fatal("%s: no channel for id %d", __func__
, id
);
1131 if ((cc
= channel_by_id(c
->ctl_chan
)) == NULL
)
1132 fatal("%s: channel %d lacks control channel %d", __func__
,
1136 debug3("%s: sending failure reply", __func__
);
1138 buffer_init(&reply
);
1139 buffer_put_int(&reply
, MUX_S_FAILURE
);
1140 buffer_put_int(&reply
, cctx
->rid
);
1141 buffer_put_cstring(&reply
, "Session open refused by peer");
1145 display
= getenv("DISPLAY");
1146 if (cctx
->want_x_fwd
&& options
.forward_x11
&& display
!= NULL
) {
1149 /* Get reasonable local authentication information. */
1150 client_x11_get_proto(display
, options
.xauth_location
,
1151 options
.forward_x11_trusted
, options
.forward_x11_timeout
,
1153 /* Request forwarding with authentication spoofing. */
1154 debug("Requesting X11 forwarding with authentication "
1156 x11_request_forwarding_with_spoofing(id
, display
, proto
, data
);
1157 /* XXX wait for reply */
1160 if (cctx
->want_agent_fwd
&& options
.forward_agent
) {
1161 debug("Requesting authentication agent forwarding.");
1162 channel_request_start(id
, "auth-agent-req@openssh.com", 0);
1166 client_session2_setup(id
, cctx
->want_tty
, cctx
->want_subsys
,
1167 cctx
->term
, &cctx
->tio
, c
->rfd
, &cctx
->cmd
, cctx
->env
);
1169 debug3("%s: sending success reply", __func__
);
1171 buffer_init(&reply
);
1172 buffer_put_int(&reply
, MUX_S_SESSION_OPENED
);
1173 buffer_put_int(&reply
, cctx
->rid
);
1174 buffer_put_int(&reply
, c
->self
);
1178 buffer_put_string(&cc
->output
, buffer_ptr(&reply
), buffer_len(&reply
));
1179 buffer_free(&reply
);
1181 if (cc
->mux_pause
<= 0)
1182 fatal("%s: mux_pause %d", __func__
, cc
->mux_pause
);
1183 cc
->mux_pause
= 0; /* start processing messages again */
1184 c
->open_confirm_ctx
= NULL
;
1185 buffer_free(&cctx
->cmd
);
1187 if (cctx
->env
!= NULL
) {
1188 for (i
= 0; cctx
->env
[i
] != NULL
; i
++)
1189 xfree(cctx
->env
[i
]);
1195 /* ** Multiplexing client support */
1197 /* Exit signal handler */
1199 control_client_sighandler(int signo
)
1201 muxclient_terminate
= signo
;
1205 * Relay signal handler - used to pass some signals from mux client to
1209 control_client_sigrelay(int signo
)
1211 int save_errno
= errno
;
1213 if (muxserver_pid
> 1)
1214 kill(muxserver_pid
, signo
);
1220 mux_client_read(int fd
, Buffer
*b
, u_int need
)
1228 pfd
.events
= POLLIN
;
1229 p
= buffer_append_space(b
, need
);
1230 for (have
= 0; have
< need
; ) {
1231 if (muxclient_terminate
) {
1235 len
= read(fd
, p
+ have
, need
- have
);
1238 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1242 (void)poll(&pfd
, 1, -1);
1260 mux_client_write_packet(int fd
, Buffer
*m
)
1269 pfd
.events
= POLLOUT
;
1270 buffer_init(&queue
);
1271 buffer_put_string(&queue
, buffer_ptr(m
), buffer_len(m
));
1273 need
= buffer_len(&queue
);
1274 ptr
= buffer_ptr(&queue
);
1276 for (have
= 0; have
< need
; ) {
1277 if (muxclient_terminate
) {
1278 buffer_free(&queue
);
1282 len
= write(fd
, ptr
+ have
, need
- have
);
1285 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1289 (void)poll(&pfd
, 1, -1);
1295 buffer_free(&queue
);
1301 buffer_free(&queue
);
1307 buffer_free(&queue
);
1312 mux_client_read_packet(int fd
, Buffer
*m
)
1319 buffer_init(&queue
);
1320 if (mux_client_read(fd
, &queue
, 4) != 0) {
1321 if ((oerrno
= errno
) == EPIPE
)
1322 debug3("%s: read header failed: %s", __func__
, strerror(errno
));
1326 need
= get_u32(buffer_ptr(&queue
));
1327 if (mux_client_read(fd
, &queue
, need
) != 0) {
1329 debug3("%s: read body failed: %s", __func__
, strerror(errno
));
1333 ptr
= buffer_get_string_ptr(&queue
, &have
);
1334 buffer_append(m
, ptr
, have
);
1335 buffer_free(&queue
);
1340 mux_client_hello_exchange(int fd
)
1346 buffer_put_int(&m
, MUX_MSG_HELLO
);
1347 buffer_put_int(&m
, SSHMUX_VER
);
1350 if (mux_client_write_packet(fd
, &m
) != 0)
1351 fatal("%s: write packet: %s", __func__
, strerror(errno
));
1355 /* Read their HELLO */
1356 if (mux_client_read_packet(fd
, &m
) != 0) {
1361 type
= buffer_get_int(&m
);
1362 if (type
!= MUX_MSG_HELLO
)
1363 fatal("%s: expected HELLO (%u) received %u",
1364 __func__
, MUX_MSG_HELLO
, type
);
1365 ver
= buffer_get_int(&m
);
1366 if (ver
!= SSHMUX_VER
)
1367 fatal("Unsupported multiplexing protocol version %d "
1368 "(expected %d)", ver
, SSHMUX_VER
);
1369 debug2("%s: master version %u", __func__
, ver
);
1370 /* No extensions are presently defined */
1371 while (buffer_len(&m
) > 0) {
1372 char *name
= buffer_get_string(&m
, NULL
);
1373 char *value
= buffer_get_string(&m
, NULL
);
1375 debug2("Unrecognised master extension \"%s\"", name
);
1384 mux_client_request_alive(int fd
)
1388 u_int pid
, type
, rid
;
1390 debug3("%s: entering", __func__
);
1393 buffer_put_int(&m
, MUX_C_ALIVE_CHECK
);
1394 buffer_put_int(&m
, muxclient_request_id
);
1396 if (mux_client_write_packet(fd
, &m
) != 0)
1397 fatal("%s: write packet: %s", __func__
, strerror(errno
));
1401 /* Read their reply */
1402 if (mux_client_read_packet(fd
, &m
) != 0) {
1407 type
= buffer_get_int(&m
);
1408 if (type
!= MUX_S_ALIVE
) {
1409 e
= buffer_get_string(&m
, NULL
);
1410 fatal("%s: master returned error: %s", __func__
, e
);
1413 if ((rid
= buffer_get_int(&m
)) != muxclient_request_id
)
1414 fatal("%s: out of sequence reply: my id %u theirs %u",
1415 __func__
, muxclient_request_id
, rid
);
1416 pid
= buffer_get_int(&m
);
1419 debug3("%s: done pid = %u", __func__
, pid
);
1421 muxclient_request_id
++;
1427 mux_client_request_terminate(int fd
)
1433 debug3("%s: entering", __func__
);
1436 buffer_put_int(&m
, MUX_C_TERMINATE
);
1437 buffer_put_int(&m
, muxclient_request_id
);
1439 if (mux_client_write_packet(fd
, &m
) != 0)
1440 fatal("%s: write packet: %s", __func__
, strerror(errno
));
1444 /* Read their reply */
1445 if (mux_client_read_packet(fd
, &m
) != 0) {
1446 /* Remote end exited already */
1447 if (errno
== EPIPE
) {
1451 fatal("%s: read from master failed: %s",
1452 __func__
, strerror(errno
));
1455 type
= buffer_get_int(&m
);
1456 if ((rid
= buffer_get_int(&m
)) != muxclient_request_id
)
1457 fatal("%s: out of sequence reply: my id %u theirs %u",
1458 __func__
, muxclient_request_id
, rid
);
1462 case MUX_S_PERMISSION_DENIED
:
1463 e
= buffer_get_string(&m
, NULL
);
1464 fatal("Master refused termination request: %s", e
);
1466 e
= buffer_get_string(&m
, NULL
);
1467 fatal("%s: termination request failed: %s", __func__
, e
);
1469 fatal("%s: unexpected response from master 0x%08x",
1473 muxclient_request_id
++;
1477 mux_client_request_forward(int fd
, u_int ftype
, Forward
*fwd
)
1483 fwd_desc
= format_forward(ftype
, fwd
);
1484 debug("Requesting %s", fwd_desc
);
1488 buffer_put_int(&m
, MUX_C_OPEN_FWD
);
1489 buffer_put_int(&m
, muxclient_request_id
);
1490 buffer_put_int(&m
, ftype
);
1491 buffer_put_cstring(&m
,
1492 fwd
->listen_host
== NULL
? "" : fwd
->listen_host
);
1493 buffer_put_int(&m
, fwd
->listen_port
);
1494 buffer_put_cstring(&m
,
1495 fwd
->connect_host
== NULL
? "" : fwd
->connect_host
);
1496 buffer_put_int(&m
, fwd
->connect_port
);
1498 if (mux_client_write_packet(fd
, &m
) != 0)
1499 fatal("%s: write packet: %s", __func__
, strerror(errno
));
1503 /* Read their reply */
1504 if (mux_client_read_packet(fd
, &m
) != 0) {
1509 type
= buffer_get_int(&m
);
1510 if ((rid
= buffer_get_int(&m
)) != muxclient_request_id
)
1511 fatal("%s: out of sequence reply: my id %u theirs %u",
1512 __func__
, muxclient_request_id
, rid
);
1516 case MUX_S_REMOTE_PORT
:
1517 fwd
->allocated_port
= buffer_get_int(&m
);
1518 logit("Allocated port %u for remote forward to %s:%d",
1519 fwd
->allocated_port
,
1520 fwd
->connect_host
? fwd
->connect_host
: "",
1522 if (muxclient_command
== SSHMUX_COMMAND_FORWARD
)
1523 fprintf(stdout
, "%u\n", fwd
->allocated_port
);
1525 case MUX_S_PERMISSION_DENIED
:
1526 e
= buffer_get_string(&m
, NULL
);
1528 error("Master refused forwarding request: %s", e
);
1531 e
= buffer_get_string(&m
, NULL
);
1533 error("%s: session request failed: %s", __func__
, e
);
1536 fatal("%s: unexpected response from master 0x%08x",
1541 muxclient_request_id
++;
1546 mux_client_request_forwards(int fd
)
1550 debug3("%s: requesting forwardings: %d local, %d remote", __func__
,
1551 options
.num_local_forwards
, options
.num_remote_forwards
);
1553 /* XXX ExitOnForwardingFailure */
1554 for (i
= 0; i
< options
.num_local_forwards
; i
++) {
1555 if (mux_client_request_forward(fd
,
1556 options
.local_forwards
[i
].connect_port
== 0 ?
1557 MUX_FWD_DYNAMIC
: MUX_FWD_LOCAL
,
1558 options
.local_forwards
+ i
) != 0)
1561 for (i
= 0; i
< options
.num_remote_forwards
; i
++) {
1562 if (mux_client_request_forward(fd
, MUX_FWD_REMOTE
,
1563 options
.remote_forwards
+ i
) != 0)
1570 mux_client_request_session(int fd
)
1574 u_int i
, rid
, sid
, esid
, exitval
, type
, exitval_seen
;
1575 extern char **environ
;
1578 debug3("%s: entering", __func__
);
1580 if ((muxserver_pid
= mux_client_request_alive(fd
)) == 0) {
1581 error("%s: master alive request failed", __func__
);
1585 signal(SIGPIPE
, SIG_IGN
);
1587 if (stdin_null_flag
) {
1588 if ((devnull
= open(_PATH_DEVNULL
, O_RDONLY
)) == -1)
1589 fatal("open(/dev/null): %s", strerror(errno
));
1590 if (dup2(devnull
, STDIN_FILENO
) == -1)
1591 fatal("dup2: %s", strerror(errno
));
1592 if (devnull
> STDERR_FILENO
)
1596 term
= getenv("TERM");
1599 buffer_put_int(&m
, MUX_C_NEW_SESSION
);
1600 buffer_put_int(&m
, muxclient_request_id
);
1601 buffer_put_cstring(&m
, ""); /* reserved */
1602 buffer_put_int(&m
, tty_flag
);
1603 buffer_put_int(&m
, options
.forward_x11
);
1604 buffer_put_int(&m
, options
.forward_agent
);
1605 buffer_put_int(&m
, subsystem_flag
);
1606 buffer_put_int(&m
, options
.escape_char
== SSH_ESCAPECHAR_NONE
?
1607 0xffffffff : (u_int
)options
.escape_char
);
1608 buffer_put_cstring(&m
, term
== NULL
? "" : term
);
1609 buffer_put_string(&m
, buffer_ptr(&command
), buffer_len(&command
));
1611 if (options
.num_send_env
> 0 && environ
!= NULL
) {
1612 /* Pass environment */
1613 for (i
= 0; environ
[i
] != NULL
; i
++) {
1614 if (env_permitted(environ
[i
])) {
1615 buffer_put_cstring(&m
, environ
[i
]);
1620 if (mux_client_write_packet(fd
, &m
) != 0)
1621 fatal("%s: write packet: %s", __func__
, strerror(errno
));
1623 /* Send the stdio file descriptors */
1624 if (mm_send_fd(fd
, STDIN_FILENO
) == -1 ||
1625 mm_send_fd(fd
, STDOUT_FILENO
) == -1 ||
1626 mm_send_fd(fd
, STDERR_FILENO
) == -1)
1627 fatal("%s: send fds failed", __func__
);
1629 debug3("%s: session request sent", __func__
);
1631 /* Read their reply */
1633 if (mux_client_read_packet(fd
, &m
) != 0) {
1634 error("%s: read from master failed: %s",
1635 __func__
, strerror(errno
));
1640 type
= buffer_get_int(&m
);
1641 if ((rid
= buffer_get_int(&m
)) != muxclient_request_id
)
1642 fatal("%s: out of sequence reply: my id %u theirs %u",
1643 __func__
, muxclient_request_id
, rid
);
1645 case MUX_S_SESSION_OPENED
:
1646 sid
= buffer_get_int(&m
);
1647 debug("%s: master session id: %u", __func__
, sid
);
1649 case MUX_S_PERMISSION_DENIED
:
1650 e
= buffer_get_string(&m
, NULL
);
1652 error("Master refused forwarding request: %s", e
);
1655 e
= buffer_get_string(&m
, NULL
);
1657 error("%s: forwarding request failed: %s", __func__
, e
);
1661 error("%s: unexpected response from master 0x%08x",
1665 muxclient_request_id
++;
1667 signal(SIGHUP
, control_client_sighandler
);
1668 signal(SIGINT
, control_client_sighandler
);
1669 signal(SIGTERM
, control_client_sighandler
);
1670 signal(SIGWINCH
, control_client_sigrelay
);
1673 enter_raw_mode(force_tty_flag
);
1676 * Stick around until the controlee closes the client_fd.
1677 * Before it does, it is expected to write an exit message.
1678 * This process must read the value and wait for the closure of
1679 * the client_fd; if this one closes early, the multiplex master will
1680 * terminate early too (possibly losing data).
1682 for (exitval
= 255, exitval_seen
= 0;;) {
1684 if (mux_client_read_packet(fd
, &m
) != 0)
1686 type
= buffer_get_int(&m
);
1687 if (type
!= MUX_S_EXIT_MESSAGE
) {
1688 e
= buffer_get_string(&m
, NULL
);
1689 fatal("%s: master returned error: %s", __func__
, e
);
1691 if ((esid
= buffer_get_int(&m
)) != sid
)
1692 fatal("%s: exit on unknown session: my id %u theirs %u",
1693 __func__
, sid
, esid
);
1694 debug("%s: master session id: %u", __func__
, sid
);
1696 fatal("%s: exitval sent twice", __func__
);
1697 exitval
= buffer_get_int(&m
);
1702 leave_raw_mode(force_tty_flag
);
1704 if (muxclient_terminate
) {
1705 debug2("Exiting on signal %d", muxclient_terminate
);
1707 } else if (!exitval_seen
) {
1708 debug2("Control master terminated unexpectedly");
1711 debug2("Received exit status from master %d", exitval
);
1713 if (tty_flag
&& options
.log_level
!= SYSLOG_LEVEL_QUIET
)
1714 fprintf(stderr
, "Shared connection to %s closed.\r\n", host
);
1720 mux_client_request_stdio_fwd(int fd
)
1724 u_int type
, rid
, sid
;
1727 debug3("%s: entering", __func__
);
1729 if ((muxserver_pid
= mux_client_request_alive(fd
)) == 0) {
1730 error("%s: master alive request failed", __func__
);
1734 signal(SIGPIPE
, SIG_IGN
);
1736 if (stdin_null_flag
) {
1737 if ((devnull
= open(_PATH_DEVNULL
, O_RDONLY
)) == -1)
1738 fatal("open(/dev/null): %s", strerror(errno
));
1739 if (dup2(devnull
, STDIN_FILENO
) == -1)
1740 fatal("dup2: %s", strerror(errno
));
1741 if (devnull
> STDERR_FILENO
)
1746 buffer_put_int(&m
, MUX_C_NEW_STDIO_FWD
);
1747 buffer_put_int(&m
, muxclient_request_id
);
1748 buffer_put_cstring(&m
, ""); /* reserved */
1749 buffer_put_cstring(&m
, stdio_forward_host
);
1750 buffer_put_int(&m
, stdio_forward_port
);
1752 if (mux_client_write_packet(fd
, &m
) != 0)
1753 fatal("%s: write packet: %s", __func__
, strerror(errno
));
1755 /* Send the stdio file descriptors */
1756 if (mm_send_fd(fd
, STDIN_FILENO
) == -1 ||
1757 mm_send_fd(fd
, STDOUT_FILENO
) == -1)
1758 fatal("%s: send fds failed", __func__
);
1760 debug3("%s: stdio forward request sent", __func__
);
1762 /* Read their reply */
1765 if (mux_client_read_packet(fd
, &m
) != 0) {
1766 error("%s: read from master failed: %s",
1767 __func__
, strerror(errno
));
1772 type
= buffer_get_int(&m
);
1773 if ((rid
= buffer_get_int(&m
)) != muxclient_request_id
)
1774 fatal("%s: out of sequence reply: my id %u theirs %u",
1775 __func__
, muxclient_request_id
, rid
);
1777 case MUX_S_SESSION_OPENED
:
1778 sid
= buffer_get_int(&m
);
1779 debug("%s: master session id: %u", __func__
, sid
);
1781 case MUX_S_PERMISSION_DENIED
:
1782 e
= buffer_get_string(&m
, NULL
);
1784 fatal("Master refused forwarding request: %s", e
);
1786 e
= buffer_get_string(&m
, NULL
);
1788 fatal("%s: stdio forwarding request failed: %s", __func__
, e
);
1791 error("%s: unexpected response from master 0x%08x",
1795 muxclient_request_id
++;
1797 signal(SIGHUP
, control_client_sighandler
);
1798 signal(SIGINT
, control_client_sighandler
);
1799 signal(SIGTERM
, control_client_sighandler
);
1800 signal(SIGWINCH
, control_client_sigrelay
);
1803 * Stick around until the controlee closes the client_fd.
1806 if (mux_client_read_packet(fd
, &m
) != 0) {
1807 if (errno
== EPIPE
||
1808 (errno
== EINTR
&& muxclient_terminate
!= 0))
1810 fatal("%s: mux_client_read_packet: %s",
1811 __func__
, strerror(errno
));
1813 fatal("%s: master returned unexpected message %u", __func__
, type
);
1816 /* Multiplex client main loop. */
1818 muxclient(const char *path
)
1820 struct sockaddr_un addr
;
1825 if (muxclient_command
== 0) {
1826 if (stdio_forward_host
!= NULL
)
1827 muxclient_command
= SSHMUX_COMMAND_STDIO_FWD
;
1829 muxclient_command
= SSHMUX_COMMAND_OPEN
;
1832 switch (options
.control_master
) {
1833 case SSHCTL_MASTER_AUTO
:
1834 case SSHCTL_MASTER_AUTO_ASK
:
1835 debug("auto-mux: Trying existing master");
1837 case SSHCTL_MASTER_NO
:
1843 memset(&addr
, '\0', sizeof(addr
));
1844 addr
.sun_family
= AF_UNIX
;
1845 sun_len
= offsetof(struct sockaddr_un
, sun_path
) +
1848 if (strlcpy(addr
.sun_path
, path
,
1849 sizeof(addr
.sun_path
)) >= sizeof(addr
.sun_path
))
1850 fatal("ControlPath too long");
1852 if ((sock
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
1853 fatal("%s socket(): %s", __func__
, strerror(errno
));
1855 if (connect(sock
, (struct sockaddr
*)&addr
, sun_len
) == -1) {
1856 switch (muxclient_command
) {
1857 case SSHMUX_COMMAND_OPEN
:
1858 case SSHMUX_COMMAND_STDIO_FWD
:
1861 fatal("Control socket connect(%.100s): %s", path
,
1864 if (errno
== ECONNREFUSED
&&
1865 options
.control_master
!= SSHCTL_MASTER_NO
) {
1866 debug("Stale control socket %.100s, unlinking", path
);
1868 } else if (errno
== ENOENT
) {
1869 debug("Control socket \"%.100s\" does not exist", path
);
1871 error("Control socket connect(%.100s): %s", path
,
1879 if (mux_client_hello_exchange(sock
) != 0) {
1880 error("%s: master hello exchange failed", __func__
);
1885 switch (muxclient_command
) {
1886 case SSHMUX_COMMAND_ALIVE_CHECK
:
1887 if ((pid
= mux_client_request_alive(sock
)) == 0)
1888 fatal("%s: master alive check failed", __func__
);
1889 fprintf(stderr
, "Master running (pid=%d)\r\n", pid
);
1891 case SSHMUX_COMMAND_TERMINATE
:
1892 mux_client_request_terminate(sock
);
1893 fprintf(stderr
, "Exit request sent.\r\n");
1895 case SSHMUX_COMMAND_FORWARD
:
1896 if (mux_client_request_forwards(sock
) != 0)
1897 fatal("%s: master forward request failed", __func__
);
1899 case SSHMUX_COMMAND_OPEN
:
1900 if (mux_client_request_forwards(sock
) != 0) {
1901 error("%s: master forward request failed", __func__
);
1904 mux_client_request_session(sock
);
1906 case SSHMUX_COMMAND_STDIO_FWD
:
1907 mux_client_request_stdio_fwd(sock
);
1910 fatal("unrecognised muxclient_command %d", muxclient_command
);