freebsd-src/crypto/openssh/mux.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

2375 lines
63 KiB
C
Raw Normal View History

2023-12-18 15:59:40 +00:00
/* $OpenBSD: mux.c,v 1.101 2023/11/23 03:37:05 dtucker Exp $ */
2008-07-23 09:33:08 +00:00
/*
* Copyright (c) 2002-2008 Damien Miller <djm@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* ssh session multiplexing support */
2010-03-08 11:19:52 +00:00
#include "includes.h"
2008-07-23 09:33:08 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <fcntl.h>
2023-08-10 16:16:53 +00:00
#include <limits.h>
2008-07-23 09:33:08 +00:00
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_PATHS_H
#include <paths.h>
#endif
2010-03-08 11:19:52 +00:00
#ifdef HAVE_POLL_H
#include <poll.h>
#else
# ifdef HAVE_SYS_POLL_H
# include <sys/poll.h>
# endif
#endif
2008-07-23 09:33:08 +00:00
#ifdef HAVE_UTIL_H
# include <util.h>
#endif
#include "openbsd-compat/sys-queue.h"
#include "xmalloc.h"
#include "log.h"
#include "ssh.h"
2010-11-08 10:45:44 +00:00
#include "ssh2.h"
2008-07-23 09:33:08 +00:00
#include "pathnames.h"
#include "misc.h"
#include "match.h"
2018-08-28 10:47:58 +00:00
#include "sshbuf.h"
2008-07-23 09:33:08 +00:00
#include "channels.h"
#include "msg.h"
#include "packet.h"
#include "monitor_fdpass.h"
#include "sshpty.h"
2018-08-28 10:47:58 +00:00
#include "sshkey.h"
2008-07-23 09:33:08 +00:00
#include "readconf.h"
#include "clientloop.h"
2017-01-31 12:33:47 +00:00
#include "ssherr.h"
2023-08-10 16:16:53 +00:00
#include "misc.h"
2008-07-23 09:33:08 +00:00
/* from ssh.c */
extern int tty_flag;
extern Options options;
extern char *host;
2018-08-28 10:47:58 +00:00
extern struct sshbuf *command;
2010-03-08 11:19:52 +00:00
extern volatile sig_atomic_t quit_pending;
2008-07-23 09:33:08 +00:00
/* Context for session open confirmation callback */
struct mux_session_confirm_ctx {
2010-03-08 11:19:52 +00:00
u_int want_tty;
u_int want_subsys;
u_int want_x_fwd;
u_int want_agent_fwd;
2018-08-28 10:47:58 +00:00
struct sshbuf *cmd;
2008-07-23 09:33:08 +00:00
char *term;
struct termios tio;
char **env;
2010-11-08 10:45:44 +00:00
u_int rid;
};
2015-01-05 16:09:55 +00:00
/* Context for stdio fwd open confirmation callback */
struct mux_stdio_confirm_ctx {
u_int rid;
};
2010-11-08 10:45:44 +00:00
/* Context for global channel callback */
struct mux_channel_confirm_ctx {
u_int cid; /* channel id */
u_int rid; /* request id */
int fid; /* forward id */
2008-07-23 09:33:08 +00:00
};
/* fd to control socket */
int muxserver_sock = -1;
2010-03-08 11:19:52 +00:00
/* client request id */
u_int muxclient_request_id = 0;
2008-07-23 09:33:08 +00:00
/* Multiplexing control command */
u_int muxclient_command = 0;
/* Set when signalled. */
static volatile sig_atomic_t muxclient_terminate = 0;
/* PID of multiplex server */
static u_int muxserver_pid = 0;
2010-03-08 11:19:52 +00:00
static Channel *mux_listener_channel = NULL;
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
struct mux_master_state {
int hello_rcvd;
};
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
/* mux protocol messages */
#define MUX_MSG_HELLO 0x00000001
#define MUX_C_NEW_SESSION 0x10000002
#define MUX_C_ALIVE_CHECK 0x10000004
#define MUX_C_TERMINATE 0x10000005
#define MUX_C_OPEN_FWD 0x10000006
#define MUX_C_CLOSE_FWD 0x10000007
#define MUX_C_NEW_STDIO_FWD 0x10000008
2011-09-28 08:14:41 +00:00
#define MUX_C_STOP_LISTENING 0x10000009
2017-01-31 12:33:47 +00:00
#define MUX_C_PROXY 0x1000000f
2010-03-08 11:19:52 +00:00
#define MUX_S_OK 0x80000001
#define MUX_S_PERMISSION_DENIED 0x80000002
#define MUX_S_FAILURE 0x80000003
#define MUX_S_EXIT_MESSAGE 0x80000004
#define MUX_S_ALIVE 0x80000005
#define MUX_S_SESSION_OPENED 0x80000006
2010-11-08 10:45:44 +00:00
#define MUX_S_REMOTE_PORT 0x80000007
2011-09-28 08:14:41 +00:00
#define MUX_S_TTY_ALLOC_FAIL 0x80000008
2017-01-31 12:33:47 +00:00
#define MUX_S_PROXY 0x8000000f
2010-03-08 11:19:52 +00:00
/* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
#define MUX_FWD_LOCAL 1
#define MUX_FWD_REMOTE 2
#define MUX_FWD_DYNAMIC 3
2018-05-06 12:24:45 +00:00
static void mux_session_confirm(struct ssh *, int, int, void *);
static void mux_stdio_confirm(struct ssh *, int, int, void *);
2019-02-05 15:03:53 +00:00
static int mux_master_process_hello(struct ssh *, u_int,
2018-05-06 12:24:45 +00:00
Channel *, struct sshbuf *, struct sshbuf *);
2019-02-05 15:03:53 +00:00
static int mux_master_process_new_session(struct ssh *, u_int,
2018-05-06 12:24:45 +00:00
Channel *, struct sshbuf *, struct sshbuf *);
2019-02-05 15:03:53 +00:00
static int mux_master_process_alive_check(struct ssh *, u_int,
2018-05-06 12:24:45 +00:00
Channel *, struct sshbuf *, struct sshbuf *);
2019-02-05 15:03:53 +00:00
static int mux_master_process_terminate(struct ssh *, u_int,
2018-05-06 12:24:45 +00:00
Channel *, struct sshbuf *, struct sshbuf *);
2019-02-05 15:03:53 +00:00
static int mux_master_process_open_fwd(struct ssh *, u_int,
2018-05-06 12:24:45 +00:00
Channel *, struct sshbuf *, struct sshbuf *);
2019-02-05 15:03:53 +00:00
static int mux_master_process_close_fwd(struct ssh *, u_int,
2018-05-06 12:24:45 +00:00
Channel *, struct sshbuf *, struct sshbuf *);
2019-02-05 15:03:53 +00:00
static int mux_master_process_stdio_fwd(struct ssh *, u_int,
2018-05-06 12:24:45 +00:00
Channel *, struct sshbuf *, struct sshbuf *);
2019-02-05 15:03:53 +00:00
static int mux_master_process_stop_listening(struct ssh *, u_int,
2018-05-06 12:24:45 +00:00
Channel *, struct sshbuf *, struct sshbuf *);
2019-02-05 15:03:53 +00:00
static int mux_master_process_proxy(struct ssh *, u_int,
2018-05-06 12:24:45 +00:00
Channel *, struct sshbuf *, struct sshbuf *);
2010-03-08 11:19:52 +00:00
static const struct {
u_int type;
2018-05-06 12:24:45 +00:00
int (*handler)(struct ssh *, u_int, Channel *,
struct sshbuf *, struct sshbuf *);
2010-03-08 11:19:52 +00:00
} mux_master_handlers[] = {
2019-02-05 15:03:53 +00:00
{ MUX_MSG_HELLO, mux_master_process_hello },
{ MUX_C_NEW_SESSION, mux_master_process_new_session },
{ MUX_C_ALIVE_CHECK, mux_master_process_alive_check },
{ MUX_C_TERMINATE, mux_master_process_terminate },
{ MUX_C_OPEN_FWD, mux_master_process_open_fwd },
{ MUX_C_CLOSE_FWD, mux_master_process_close_fwd },
{ MUX_C_NEW_STDIO_FWD, mux_master_process_stdio_fwd },
{ MUX_C_STOP_LISTENING, mux_master_process_stop_listening },
{ MUX_C_PROXY, mux_master_process_proxy },
2010-03-08 11:19:52 +00:00
{ 0, NULL }
};
2008-07-23 09:33:08 +00:00
2021-02-14 21:09:58 +00:00
/* Cleanup callback fired on closure of mux client _session_ channel */
2013-09-18 17:27:38 +00:00
static void
2023-02-05 18:04:12 +00:00
mux_master_session_cleanup_cb(struct ssh *ssh, int cid, int force, void *unused)
2010-03-08 11:19:52 +00:00
{
2018-05-06 12:24:45 +00:00
Channel *cc, *c = channel_by_id(ssh, cid);
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("entering for channel %d", cid);
2010-03-08 11:19:52 +00:00
if (c == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("channel_by_id(%i) == NULL", cid);
2010-03-08 11:19:52 +00:00
if (c->ctl_chan != -1) {
2018-05-06 12:24:45 +00:00
if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("channel %d missing control channel %d",
c->self, c->ctl_chan);
2010-03-08 11:19:52 +00:00
c->ctl_chan = -1;
2018-05-06 12:24:45 +00:00
cc->remote_id = 0;
cc->have_remote_id = 0;
chan_rcvd_oclose(ssh, cc);
2008-07-23 09:33:08 +00:00
}
2018-05-06 12:24:45 +00:00
channel_cancel_cleanup(ssh, c->self);
2008-07-23 09:33:08 +00:00
}
2021-02-14 21:09:58 +00:00
/* Cleanup callback fired on closure of mux client _control_ channel */
2008-07-23 09:33:08 +00:00
static void
2023-02-05 18:04:12 +00:00
mux_master_control_cleanup_cb(struct ssh *ssh, int cid, int force, void *unused)
2008-07-23 09:33:08 +00:00
{
2018-05-06 12:24:45 +00:00
Channel *sc, *c = channel_by_id(ssh, cid);
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("entering for channel %d", cid);
2010-03-08 11:19:52 +00:00
if (c == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("channel_by_id(%i) == NULL", cid);
2018-05-06 12:24:45 +00:00
if (c->have_remote_id) {
if ((sc = channel_by_id(ssh, c->remote_id)) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("channel %d missing session channel %u",
c->self, c->remote_id);
2018-05-06 12:24:45 +00:00
c->remote_id = 0;
c->have_remote_id = 0;
2010-03-08 11:19:52 +00:00
sc->ctl_chan = -1;
2013-09-18 17:27:38 +00:00
if (sc->type != SSH_CHANNEL_OPEN &&
sc->type != SSH_CHANNEL_OPENING) {
2021-04-23 19:10:38 +00:00
debug2_f("channel %d: not open", sc->self);
2018-05-06 12:24:45 +00:00
chan_mark_dead(ssh, sc);
2010-03-08 11:19:52 +00:00
} else {
if (sc->istate == CHAN_INPUT_OPEN)
2018-05-06 12:24:45 +00:00
chan_read_failed(ssh, sc);
2010-03-08 11:19:52 +00:00
if (sc->ostate == CHAN_OUTPUT_OPEN)
2018-05-06 12:24:45 +00:00
chan_write_failed(ssh, sc);
2010-03-08 11:19:52 +00:00
}
2008-07-23 09:33:08 +00:00
}
2018-05-06 12:24:45 +00:00
channel_cancel_cleanup(ssh, c->self);
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
/* Check mux client environment variables before passing them to mux master. */
static int
2022-10-04 15:10:40 +00:00
env_permitted(const char *env)
2008-07-23 09:33:08 +00:00
{
2022-10-04 15:10:40 +00:00
u_int i;
int ret;
2010-03-08 11:19:52 +00:00
char name[1024], *cp;
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
if ((cp = strchr(env, '=')) == NULL || cp == env)
2008-07-23 09:33:08 +00:00
return 0;
2010-03-08 11:19:52 +00:00
ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
if (ret <= 0 || (size_t)ret >= sizeof(name)) {
2021-04-23 19:10:38 +00:00
error_f("name '%.100s...' too long", env);
2008-07-23 09:33:08 +00:00
return 0;
}
2010-03-08 11:19:52 +00:00
for (i = 0; i < options.num_send_env; i++)
if (match_pattern(name, options.send_env[i]))
return 1;
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
return 0;
}
/* Mux master protocol message handlers */
static int
2019-02-05 15:03:53 +00:00
mux_master_process_hello(struct ssh *ssh, u_int rid,
2018-08-28 10:47:58 +00:00
Channel *c, struct sshbuf *m, struct sshbuf *reply)
2010-03-08 11:19:52 +00:00
{
u_int ver;
struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
2018-08-28 10:47:58 +00:00
int r;
2010-03-08 11:19:52 +00:00
if (state == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("channel %d: c->mux_ctx == NULL", c->self);
2010-03-08 11:19:52 +00:00
if (state->hello_rcvd) {
2021-04-23 19:10:38 +00:00
error_f("HELLO received twice");
2010-03-08 11:19:52 +00:00
return -1;
2008-07-23 09:33:08 +00:00
}
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &ver)) != 0) {
2021-04-23 19:10:38 +00:00
error_fr(r, "parse");
2010-03-08 11:19:52 +00:00
return -1;
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
if (ver != SSHMUX_VER) {
2021-04-23 19:10:38 +00:00
error_f("unsupported multiplexing protocol version %u "
"(expected %u)", ver, SSHMUX_VER);
2010-03-08 11:19:52 +00:00
return -1;
}
2021-04-23 19:10:38 +00:00
debug2_f("channel %d client version %u", c->self, ver);
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
/* No extensions are presently defined */
2018-08-28 10:47:58 +00:00
while (sshbuf_len(m) > 0) {
char *name = NULL;
2019-02-05 15:03:53 +00:00
size_t value_len = 0;
2008-07-23 09:33:08 +00:00
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
2019-02-05 15:03:53 +00:00
(r = sshbuf_get_string_direct(m, NULL, &value_len)) != 0) {
2021-04-23 19:10:38 +00:00
error_fr(r, "parse extension");
2018-08-28 10:47:58 +00:00
return -1;
2008-07-23 09:33:08 +00:00
}
2021-04-23 19:10:38 +00:00
debug2_f("Unrecognised extension \"%s\" length %zu",
name, value_len);
2013-09-18 17:27:38 +00:00
free(name);
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
state->hello_rcvd = 1;
return 0;
}
2018-08-28 10:47:58 +00:00
/* Enqueue a "ok" response to the reply buffer */
static void
reply_ok(struct sshbuf *reply, u_int rid)
{
int r;
if ((r = sshbuf_put_u32(reply, MUX_S_OK)) != 0 ||
(r = sshbuf_put_u32(reply, rid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply");
2018-08-28 10:47:58 +00:00
}
/* Enqueue an error response to the reply buffer */
static void
reply_error(struct sshbuf *reply, u_int type, u_int rid, const char *msg)
{
int r;
if ((r = sshbuf_put_u32(reply, type)) != 0 ||
(r = sshbuf_put_u32(reply, rid)) != 0 ||
(r = sshbuf_put_cstring(reply, msg)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply");
2018-08-28 10:47:58 +00:00
}
2010-03-08 11:19:52 +00:00
static int
2019-02-05 15:03:53 +00:00
mux_master_process_new_session(struct ssh *ssh, u_int rid,
2018-08-28 10:47:58 +00:00
Channel *c, struct sshbuf *m, struct sshbuf *reply)
2010-03-08 11:19:52 +00:00
{
Channel *nc;
struct mux_session_confirm_ctx *cctx;
2018-08-28 10:47:58 +00:00
char *cmd, *cp;
u_int i, j, env_len, escape_char, window, packetmax;
int r, new_fd[3];
2008-07-23 09:33:08 +00:00
/* Reply for SSHMUX_COMMAND_OPEN */
2010-03-08 11:19:52 +00:00
cctx = xcalloc(1, sizeof(*cctx));
cctx->term = NULL;
2010-11-08 10:45:44 +00:00
cctx->rid = rid;
2018-08-28 10:47:58 +00:00
cmd = NULL;
2012-08-29 15:55:54 +00:00
cctx->env = NULL;
env_len = 0;
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
(r = sshbuf_get_u32(m, &cctx->want_tty)) != 0 ||
(r = sshbuf_get_u32(m, &cctx->want_x_fwd)) != 0 ||
(r = sshbuf_get_u32(m, &cctx->want_agent_fwd)) != 0 ||
(r = sshbuf_get_u32(m, &cctx->want_subsys)) != 0 ||
(r = sshbuf_get_u32(m, &escape_char)) != 0 ||
(r = sshbuf_get_cstring(m, &cctx->term, NULL)) != 0 ||
(r = sshbuf_get_cstring(m, &cmd, NULL)) != 0) {
2010-03-08 11:19:52 +00:00
malf:
2013-09-18 17:27:38 +00:00
free(cmd);
2012-08-29 15:55:54 +00:00
for (j = 0; j < env_len; j++)
2013-09-18 17:27:38 +00:00
free(cctx->env[j]);
free(cctx->env);
free(cctx->term);
free(cctx);
2021-04-23 19:10:38 +00:00
error_f("malformed message");
2010-03-08 11:19:52 +00:00
return -1;
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
#define MUX_MAX_ENV_VARS 4096
2018-08-28 10:47:58 +00:00
while (sshbuf_len(m) > 0) {
if ((r = sshbuf_get_cstring(m, &cp, NULL)) != 0)
2010-03-08 11:19:52 +00:00
goto malf;
if (!env_permitted(cp)) {
2013-09-18 17:27:38 +00:00
free(cp);
2010-03-08 11:19:52 +00:00
continue;
}
2015-07-02 13:18:50 +00:00
cctx->env = xreallocarray(cctx->env, env_len + 2,
2010-03-08 11:19:52 +00:00
sizeof(*cctx->env));
cctx->env[env_len++] = cp;
cctx->env[env_len] = NULL;
if (env_len > MUX_MAX_ENV_VARS) {
2021-04-23 19:10:38 +00:00
error_f(">%d environment variables received, "
"ignoring additional", MUX_MAX_ENV_VARS);
2010-03-08 11:19:52 +00:00
break;
}
2008-07-23 09:33:08 +00:00
}
2021-04-23 19:10:38 +00:00
debug2_f("channel %d: request tty %d, X %d, agent %d, subsys %d, "
"term \"%s\", cmd \"%s\", env %u", c->self,
2010-03-08 11:19:52 +00:00
cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
cctx->want_subsys, cctx->term, cmd, env_len);
2008-07-23 09:33:08 +00:00
2018-08-28 10:47:58 +00:00
if ((cctx->cmd = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put(cctx->cmd, cmd, strlen(cmd))) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "sshbuf_put");
2013-09-18 17:27:38 +00:00
free(cmd);
2010-03-08 11:19:52 +00:00
cmd = NULL;
2008-07-23 09:33:08 +00:00
/* Gather fds from client */
for(i = 0; i < 3; i++) {
2010-03-08 11:19:52 +00:00
if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
2021-04-23 19:10:38 +00:00
error_f("failed to receive fd %d from client", i);
2008-07-23 09:33:08 +00:00
for (j = 0; j < i; j++)
close(new_fd[j]);
for (j = 0; j < env_len; j++)
2013-09-18 17:27:38 +00:00
free(cctx->env[j]);
free(cctx->env);
free(cctx->term);
2018-08-28 10:47:58 +00:00
sshbuf_free(cctx->cmd);
2013-09-18 17:27:38 +00:00
free(cctx);
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_FAILURE, rid,
2010-03-08 11:19:52 +00:00
"did not receive file descriptors");
return -1;
2008-07-23 09:33:08 +00:00
}
}
2021-04-23 19:10:38 +00:00
debug3_f("got fds stdin %d, stdout %d, stderr %d",
2008-07-23 09:33:08 +00:00
new_fd[0], new_fd[1], new_fd[2]);
2010-03-08 11:19:52 +00:00
/* XXX support multiple child sessions in future */
2018-05-06 12:24:45 +00:00
if (c->have_remote_id) {
2021-04-23 19:10:38 +00:00
debug2_f("session already open");
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_FAILURE, rid,
"Multiple sessions not supported");
2010-03-08 11:19:52 +00:00
cleanup:
2008-07-23 09:33:08 +00:00
close(new_fd[0]);
close(new_fd[1]);
close(new_fd[2]);
2013-09-18 17:27:38 +00:00
free(cctx->term);
2008-07-23 09:33:08 +00:00
if (env_len != 0) {
for (i = 0; i < env_len; i++)
2013-09-18 17:27:38 +00:00
free(cctx->env[i]);
free(cctx->env);
2008-07-23 09:33:08 +00:00
}
2018-08-28 10:47:58 +00:00
sshbuf_free(cctx->cmd);
2013-09-18 17:27:38 +00:00
free(cctx);
2008-07-23 09:33:08 +00:00
return 0;
}
2010-03-08 11:19:52 +00:00
if (options.control_master == SSHCTL_MASTER_ASK ||
options.control_master == SSHCTL_MASTER_AUTO_ASK) {
if (!ask_permission("Allow shared connection to %s? ", host)) {
2021-04-23 19:10:38 +00:00
debug2_f("session refused by user");
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
"Permission denied");
2010-03-08 11:19:52 +00:00
goto cleanup;
}
}
/* Try to pick up ttymodes from client before it goes raw */
if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
2021-04-23 19:10:38 +00:00
error_f("tcgetattr: %s", strerror(errno));
2008-07-23 09:33:08 +00:00
window = CHAN_SES_WINDOW_DEFAULT;
packetmax = CHAN_SES_PACKET_DEFAULT;
if (cctx->want_tty) {
window >>= 1;
packetmax >>= 1;
}
2010-03-08 11:19:52 +00:00
2018-05-06 12:24:45 +00:00
nc = channel_new(ssh, "session", SSH_CHANNEL_OPENING,
2008-07-23 09:33:08 +00:00
new_fd[0], new_fd[1], new_fd[2], window, packetmax,
2021-08-30 19:14:33 +00:00
CHAN_EXTENDED_WRITE, "client-session", CHANNEL_NONBLOCK_STDIO);
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
nc->ctl_chan = c->self; /* link session -> control channel */
2021-04-23 19:10:38 +00:00
c->remote_id = nc->self; /* link control -> session channel */
2018-05-06 12:24:45 +00:00
c->have_remote_id = 1;
2010-03-08 11:19:52 +00:00
2008-07-23 09:33:08 +00:00
if (cctx->want_tty && escape_char != 0xffffffff) {
2018-05-06 12:24:45 +00:00
channel_register_filter(ssh, nc->self,
2008-07-23 09:33:08 +00:00
client_simple_escape_filter, NULL,
client_filter_cleanup,
client_new_escape_filter_ctx((int)escape_char));
}
2021-04-23 19:10:38 +00:00
debug2_f("channel_new: %d linked to control channel %d",
nc->self, nc->ctl_chan);
2008-07-23 09:33:08 +00:00
2018-05-06 12:24:45 +00:00
channel_send_open(ssh, nc->self);
channel_register_open_confirm(ssh, nc->self, mux_session_confirm, cctx);
2010-11-08 10:45:44 +00:00
c->mux_pause = 1; /* stop handling messages until open_confirm done */
2018-05-06 12:24:45 +00:00
channel_register_cleanup(ssh, nc->self,
mux_master_session_cleanup_cb, 1);
2008-07-23 09:33:08 +00:00
2010-11-08 10:45:44 +00:00
/* reply is deferred, sent by mux_session_confirm */
2010-03-08 11:19:52 +00:00
return 0;
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
static int
2019-02-05 15:03:53 +00:00
mux_master_process_alive_check(struct ssh *ssh, u_int rid,
2018-08-28 10:47:58 +00:00
Channel *c, struct sshbuf *m, struct sshbuf *reply)
2008-07-23 09:33:08 +00:00
{
2018-08-28 10:47:58 +00:00
int r;
2021-04-23 19:10:38 +00:00
debug2_f("channel %d: alive check", c->self);
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
/* prepare reply */
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(reply, MUX_S_ALIVE)) != 0 ||
(r = sshbuf_put_u32(reply, rid)) != 0 ||
(r = sshbuf_put_u32(reply, (u_int)getpid())) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply");
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
return 0;
2008-07-23 09:33:08 +00:00
}
static int
2019-02-05 15:03:53 +00:00
mux_master_process_terminate(struct ssh *ssh, u_int rid,
2018-08-28 10:47:58 +00:00
Channel *c, struct sshbuf *m, struct sshbuf *reply)
2008-07-23 09:33:08 +00:00
{
2021-04-23 19:10:38 +00:00
debug2_f("channel %d: terminate request", c->self);
2010-03-08 11:19:52 +00:00
if (options.control_master == SSHCTL_MASTER_ASK ||
options.control_master == SSHCTL_MASTER_AUTO_ASK) {
if (!ask_permission("Terminate shared connection to %s? ",
host)) {
2021-04-23 19:10:38 +00:00
debug2_f("termination refused by user");
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
"Permission denied");
2010-03-08 11:19:52 +00:00
return 0;
}
}
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
quit_pending = 1;
2018-08-28 10:47:58 +00:00
reply_ok(reply, rid);
2010-03-08 11:19:52 +00:00
/* XXX exit happens too soon - message never makes it to client */
return 0;
}
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
static char *
2015-01-05 16:09:55 +00:00
format_forward(u_int ftype, struct Forward *fwd)
2010-03-08 11:19:52 +00:00
{
char *ret;
switch (ftype) {
case MUX_FWD_LOCAL:
xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
2015-01-05 16:09:55 +00:00
(fwd->listen_path != NULL) ? fwd->listen_path :
2010-03-08 11:19:52 +00:00
(fwd->listen_host == NULL) ?
2015-01-05 16:09:55 +00:00
(options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
2010-03-08 11:19:52 +00:00
fwd->listen_host, fwd->listen_port,
2015-01-05 16:09:55 +00:00
(fwd->connect_path != NULL) ? fwd->connect_path :
2010-03-08 11:19:52 +00:00
fwd->connect_host, fwd->connect_port);
break;
case MUX_FWD_DYNAMIC:
xasprintf(&ret, "dynamic forward %.200s:%d -> *",
(fwd->listen_host == NULL) ?
2015-01-05 16:09:55 +00:00
(options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
2021-04-23 19:13:32 +00:00
fwd->listen_host, fwd->listen_port);
2010-03-08 11:19:52 +00:00
break;
case MUX_FWD_REMOTE:
xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
2015-01-05 16:09:55 +00:00
(fwd->listen_path != NULL) ? fwd->listen_path :
2010-03-08 11:19:52 +00:00
(fwd->listen_host == NULL) ?
"LOCALHOST" : fwd->listen_host,
fwd->listen_port,
2015-01-05 16:09:55 +00:00
(fwd->connect_path != NULL) ? fwd->connect_path :
2010-03-08 11:19:52 +00:00
fwd->connect_host, fwd->connect_port);
break;
default:
2021-04-23 19:10:38 +00:00
fatal_f("unknown forward type %u", ftype);
2010-03-08 11:19:52 +00:00
}
return ret;
}
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
static int
compare_host(const char *a, const char *b)
{
if (a == NULL && b == NULL)
return 1;
if (a == NULL || b == NULL)
return 0;
return strcmp(a, b) == 0;
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
static int
2015-01-05 16:09:55 +00:00
compare_forward(struct Forward *a, struct Forward *b)
2008-07-23 09:33:08 +00:00
{
2010-03-08 11:19:52 +00:00
if (!compare_host(a->listen_host, b->listen_host))
return 0;
2015-01-05 16:09:55 +00:00
if (!compare_host(a->listen_path, b->listen_path))
return 0;
2010-03-08 11:19:52 +00:00
if (a->listen_port != b->listen_port)
return 0;
if (!compare_host(a->connect_host, b->connect_host))
return 0;
2015-01-05 16:09:55 +00:00
if (!compare_host(a->connect_path, b->connect_path))
return 0;
2010-03-08 11:19:52 +00:00
if (a->connect_port != b->connect_port)
return 0;
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
return 1;
}
2008-07-23 09:33:08 +00:00
2010-11-08 10:45:44 +00:00
static void
2018-05-06 12:24:45 +00:00
mux_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
2010-11-08 10:45:44 +00:00
{
struct mux_channel_confirm_ctx *fctx = ctxt;
char *failmsg = NULL;
2015-01-05 16:09:55 +00:00
struct Forward *rfwd;
2010-11-08 10:45:44 +00:00
Channel *c;
2018-08-28 10:47:58 +00:00
struct sshbuf *out;
2020-02-14 19:47:15 +00:00
u_int port;
2018-08-28 10:47:58 +00:00
int r;
2010-11-08 10:45:44 +00:00
2018-05-06 12:24:45 +00:00
if ((c = channel_by_id(ssh, fctx->cid)) == NULL) {
2010-11-08 10:45:44 +00:00
/* no channel for reply */
2021-04-23 19:10:38 +00:00
error_f("unknown channel");
2010-11-08 10:45:44 +00:00
return;
}
2018-08-28 10:47:58 +00:00
if ((out = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2015-07-02 13:18:50 +00:00
if (fctx->fid >= options.num_remote_forwards ||
(options.remote_forwards[fctx->fid].connect_path == NULL &&
options.remote_forwards[fctx->fid].connect_host == NULL)) {
2010-11-08 10:45:44 +00:00
xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
goto fail;
}
rfwd = &options.remote_forwards[fctx->fid];
2021-04-23 19:10:38 +00:00
debug_f("%s for: listen %d, connect %s:%d",
2010-11-08 10:45:44 +00:00
type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
2015-01-05 16:09:55 +00:00
rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
rfwd->connect_host, rfwd->connect_port);
2010-11-08 10:45:44 +00:00
if (type == SSH2_MSG_REQUEST_SUCCESS) {
if (rfwd->listen_port == 0) {
2020-02-14 19:47:15 +00:00
if ((r = sshpkt_get_u32(ssh, &port)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse port");
2020-02-14 19:47:15 +00:00
if (port > 65535) {
fatal("Invalid allocated port %u for "
"mux remote forward to %s:%d", port,
rfwd->connect_host, rfwd->connect_port);
}
rfwd->allocated_port = (int)port;
2015-07-02 13:18:50 +00:00
debug("Allocated port %u for mux remote forward"
2010-11-08 10:45:44 +00:00
" to %s:%d", rfwd->allocated_port,
rfwd->connect_host, rfwd->connect_port);
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(out,
MUX_S_REMOTE_PORT)) != 0 ||
(r = sshbuf_put_u32(out, fctx->rid)) != 0 ||
(r = sshbuf_put_u32(out,
rfwd->allocated_port)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply");
2018-08-28 10:47:58 +00:00
channel_update_permission(ssh, rfwd->handle,
2021-04-23 19:13:32 +00:00
rfwd->allocated_port);
2010-11-08 10:45:44 +00:00
} else {
2018-08-28 10:47:58 +00:00
reply_ok(out, fctx->rid);
2010-11-08 10:45:44 +00:00
}
goto out;
} else {
2012-08-29 15:46:01 +00:00
if (rfwd->listen_port == 0)
2018-08-28 10:47:58 +00:00
channel_update_permission(ssh, rfwd->handle, -1);
2015-01-05 16:09:55 +00:00
if (rfwd->listen_path != NULL)
xasprintf(&failmsg, "remote port forwarding failed for "
"listen path %s", rfwd->listen_path);
else
xasprintf(&failmsg, "remote port forwarding failed for "
"listen port %d", rfwd->listen_port);
2015-07-02 13:18:50 +00:00
2021-04-23 19:13:32 +00:00
debug2_f("clearing registered forwarding for listen %d, "
2021-04-23 19:10:38 +00:00
"connect %s:%d", rfwd->listen_port,
2015-07-02 13:18:50 +00:00
rfwd->connect_path ? rfwd->connect_path :
rfwd->connect_host, rfwd->connect_port);
free(rfwd->listen_host);
free(rfwd->listen_path);
free(rfwd->connect_host);
free(rfwd->connect_path);
memset(rfwd, 0, sizeof(*rfwd));
2010-11-08 10:45:44 +00:00
}
fail:
2021-04-23 19:10:38 +00:00
error_f("%s", failmsg);
2018-08-28 10:47:58 +00:00
reply_error(out, MUX_S_FAILURE, fctx->rid, failmsg);
2013-09-18 17:27:38 +00:00
free(failmsg);
2010-11-08 10:45:44 +00:00
out:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_stringb(c->output, out)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "enqueue");
2018-08-28 10:47:58 +00:00
sshbuf_free(out);
2010-11-08 10:45:44 +00:00
if (c->mux_pause <= 0)
2021-04-23 19:10:38 +00:00
fatal_f("mux_pause %d", c->mux_pause);
2010-11-08 10:45:44 +00:00
c->mux_pause = 0; /* start processing messages again */
}
2010-03-08 11:19:52 +00:00
static int
2019-02-05 15:03:53 +00:00
mux_master_process_open_fwd(struct ssh *ssh, u_int rid,
2018-08-28 10:47:58 +00:00
Channel *c, struct sshbuf *m, struct sshbuf *reply)
2010-03-08 11:19:52 +00:00
{
2015-01-05 16:09:55 +00:00
struct Forward fwd;
2010-03-08 11:19:52 +00:00
char *fwd_desc = NULL;
2015-01-05 16:09:55 +00:00
char *listen_addr, *connect_addr;
2010-03-08 11:19:52 +00:00
u_int ftype;
2013-09-18 17:27:38 +00:00
u_int lport, cport;
2018-08-28 10:47:58 +00:00
int r, i, ret = 0, freefwd = 1;
2010-03-08 11:19:52 +00:00
2015-08-26 09:27:05 +00:00
memset(&fwd, 0, sizeof(fwd));
2015-01-05 16:09:55 +00:00
/* XXX - lport/cport check redundant */
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
(r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
(r = sshbuf_get_u32(m, &lport)) != 0 ||
(r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
(r = sshbuf_get_u32(m, &cport)) != 0 ||
2015-01-05 16:09:55 +00:00
(lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
(cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
2021-04-23 19:10:38 +00:00
error_f("malformed message");
2010-03-08 11:19:52 +00:00
ret = -1;
goto out;
2008-07-23 09:33:08 +00:00
}
2015-01-05 16:09:55 +00:00
if (*listen_addr == '\0') {
free(listen_addr);
listen_addr = NULL;
2010-03-08 11:19:52 +00:00
}
2015-01-05 16:09:55 +00:00
if (*connect_addr == '\0') {
free(connect_addr);
connect_addr = NULL;
2010-03-08 11:19:52 +00:00
}
2015-01-05 16:09:55 +00:00
memset(&fwd, 0, sizeof(fwd));
fwd.listen_port = lport;
if (fwd.listen_port == PORT_STREAMLOCAL)
fwd.listen_path = listen_addr;
else
fwd.listen_host = listen_addr;
fwd.connect_port = cport;
if (fwd.connect_port == PORT_STREAMLOCAL)
fwd.connect_path = connect_addr;
else
fwd.connect_host = connect_addr;
2021-04-23 19:10:38 +00:00
debug2_f("channel %d: request %s", c->self,
2010-03-08 11:19:52 +00:00
(fwd_desc = format_forward(ftype, &fwd)));
if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
ftype != MUX_FWD_DYNAMIC) {
2021-04-23 19:10:38 +00:00
logit_f("invalid forwarding type %u", ftype);
2010-03-08 11:19:52 +00:00
invalid:
2015-01-05 16:09:55 +00:00
free(listen_addr);
free(connect_addr);
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_FAILURE, rid,
"Invalid forwarding request");
2010-03-08 11:19:52 +00:00
return 0;
}
2015-01-05 16:09:55 +00:00
if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) {
2021-04-23 19:10:38 +00:00
logit_f("streamlocal and dynamic forwards "
"are mutually exclusive");
2015-01-05 16:09:55 +00:00
goto invalid;
}
if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) {
2021-04-23 19:10:38 +00:00
logit_f("invalid listen port %u", fwd.listen_port);
2010-03-08 11:19:52 +00:00
goto invalid;
}
2018-05-06 12:24:45 +00:00
if ((fwd.connect_port != PORT_STREAMLOCAL &&
fwd.connect_port >= 65536) ||
(ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE &&
fwd.connect_port == 0)) {
2021-04-23 19:10:38 +00:00
logit_f("invalid connect port %u",
2010-03-08 11:19:52 +00:00
fwd.connect_port);
goto invalid;
}
2018-05-06 12:24:45 +00:00
if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL &&
fwd.connect_path == NULL) {
2021-04-23 19:10:38 +00:00
logit_f("missing connect host");
2010-03-08 11:19:52 +00:00
goto invalid;
}
/* Skip forwards that have already been requested */
switch (ftype) {
case MUX_FWD_LOCAL:
case MUX_FWD_DYNAMIC:
for (i = 0; i < options.num_local_forwards; i++) {
if (compare_forward(&fwd,
options.local_forwards + i)) {
exists:
2021-04-23 19:10:38 +00:00
debug2_f("found existing forwarding");
2018-08-28 10:47:58 +00:00
reply_ok(reply, rid);
2010-03-08 11:19:52 +00:00
goto out;
}
}
break;
case MUX_FWD_REMOTE:
for (i = 0; i < options.num_remote_forwards; i++) {
2018-08-28 10:47:58 +00:00
if (!compare_forward(&fwd, options.remote_forwards + i))
continue;
if (fwd.listen_port != 0)
goto exists;
2021-04-23 19:10:38 +00:00
debug2_f("found allocated port");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(reply,
MUX_S_REMOTE_PORT)) != 0 ||
(r = sshbuf_put_u32(reply, rid)) != 0 ||
(r = sshbuf_put_u32(reply,
options.remote_forwards[i].allocated_port)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply FWD_REMOTE");
2018-08-28 10:47:58 +00:00
goto out;
2010-03-08 11:19:52 +00:00
}
break;
}
if (options.control_master == SSHCTL_MASTER_ASK ||
options.control_master == SSHCTL_MASTER_AUTO_ASK) {
if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
2021-04-23 19:10:38 +00:00
debug2_f("forwarding refused by user");
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
"Permission denied");
2010-03-08 11:19:52 +00:00
goto out;
}
}
if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
2018-05-06 12:24:45 +00:00
if (!channel_setup_local_fwd_listener(ssh, &fwd,
2015-01-05 16:09:55 +00:00
&options.fwd_opts)) {
2010-03-08 11:19:52 +00:00
fail:
2021-04-23 19:10:38 +00:00
logit_f("requested %s failed", fwd_desc);
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_FAILURE, rid,
"Port forwarding failed");
2010-03-08 11:19:52 +00:00
goto out;
}
add_local_forward(&options, &fwd);
freefwd = 0;
} else {
2010-11-08 10:45:44 +00:00
struct mux_channel_confirm_ctx *fctx;
2018-05-06 12:24:45 +00:00
fwd.handle = channel_request_remote_forwarding(ssh, &fwd);
2012-08-29 15:46:01 +00:00
if (fwd.handle < 0)
2010-03-08 11:19:52 +00:00
goto fail;
add_remote_forward(&options, &fwd);
2010-11-08 10:45:44 +00:00
fctx = xcalloc(1, sizeof(*fctx));
fctx->cid = c->self;
fctx->rid = rid;
fctx->fid = options.num_remote_forwards - 1;
client_register_global_confirm(mux_confirm_remote_forward,
fctx);
2010-03-08 11:19:52 +00:00
freefwd = 0;
2010-11-08 10:45:44 +00:00
c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
/* delayed reply in mux_confirm_remote_forward */
goto out;
2010-03-08 11:19:52 +00:00
}
2018-08-28 10:47:58 +00:00
reply_ok(reply, rid);
2010-03-08 11:19:52 +00:00
out:
2013-09-18 17:27:38 +00:00
free(fwd_desc);
2010-03-08 11:19:52 +00:00
if (freefwd) {
2013-09-18 17:27:38 +00:00
free(fwd.listen_host);
2015-01-05 16:09:55 +00:00
free(fwd.listen_path);
2013-09-18 17:27:38 +00:00
free(fwd.connect_host);
2015-01-05 16:09:55 +00:00
free(fwd.connect_path);
2010-03-08 11:19:52 +00:00
}
return ret;
}
static int
2019-02-05 15:03:53 +00:00
mux_master_process_close_fwd(struct ssh *ssh, u_int rid,
2018-08-28 10:47:58 +00:00
Channel *c, struct sshbuf *m, struct sshbuf *reply)
2010-03-08 11:19:52 +00:00
{
2015-01-05 16:09:55 +00:00
struct Forward fwd, *found_fwd;
2010-03-08 11:19:52 +00:00
char *fwd_desc = NULL;
2012-08-29 15:46:01 +00:00
const char *error_reason = NULL;
2015-01-05 16:09:55 +00:00
char *listen_addr = NULL, *connect_addr = NULL;
2010-03-08 11:19:52 +00:00
u_int ftype;
2018-08-28 10:47:58 +00:00
int r, i, ret = 0;
2013-09-18 17:27:38 +00:00
u_int lport, cport;
2010-03-08 11:19:52 +00:00
2015-08-26 09:27:05 +00:00
memset(&fwd, 0, sizeof(fwd));
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
(r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
(r = sshbuf_get_u32(m, &lport)) != 0 ||
(r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
(r = sshbuf_get_u32(m, &cport)) != 0 ||
2015-01-05 16:09:55 +00:00
(lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
(cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
2021-04-23 19:10:38 +00:00
error_f("malformed message");
2010-03-08 11:19:52 +00:00
ret = -1;
goto out;
}
2015-01-05 16:09:55 +00:00
if (*listen_addr == '\0') {
free(listen_addr);
listen_addr = NULL;
2010-03-08 11:19:52 +00:00
}
2015-01-05 16:09:55 +00:00
if (*connect_addr == '\0') {
free(connect_addr);
connect_addr = NULL;
2010-03-08 11:19:52 +00:00
}
2015-01-05 16:09:55 +00:00
memset(&fwd, 0, sizeof(fwd));
fwd.listen_port = lport;
if (fwd.listen_port == PORT_STREAMLOCAL)
fwd.listen_path = listen_addr;
else
fwd.listen_host = listen_addr;
fwd.connect_port = cport;
if (fwd.connect_port == PORT_STREAMLOCAL)
fwd.connect_path = connect_addr;
else
fwd.connect_host = connect_addr;
2021-04-23 19:10:38 +00:00
debug2_f("channel %d: request cancel %s", c->self,
2010-03-08 11:19:52 +00:00
(fwd_desc = format_forward(ftype, &fwd)));
2012-08-29 15:46:01 +00:00
/* make sure this has been requested */
found_fwd = NULL;
switch (ftype) {
case MUX_FWD_LOCAL:
case MUX_FWD_DYNAMIC:
for (i = 0; i < options.num_local_forwards; i++) {
if (compare_forward(&fwd,
options.local_forwards + i)) {
found_fwd = options.local_forwards + i;
break;
}
}
break;
case MUX_FWD_REMOTE:
for (i = 0; i < options.num_remote_forwards; i++) {
if (compare_forward(&fwd,
options.remote_forwards + i)) {
found_fwd = options.remote_forwards + i;
break;
}
}
break;
}
2010-03-08 11:19:52 +00:00
2012-08-29 15:46:01 +00:00
if (found_fwd == NULL)
error_reason = "port not forwarded";
else if (ftype == MUX_FWD_REMOTE) {
/*
* This shouldn't fail unless we confused the host/port
* between options.remote_forwards and permitted_opens.
* However, for dynamic allocated listen ports we need
2015-01-05 16:09:55 +00:00
* to use the actual listen port.
2012-08-29 15:46:01 +00:00
*/
2018-05-06 12:24:45 +00:00
if (channel_request_rforward_cancel(ssh, found_fwd) == -1)
2012-08-29 15:46:01 +00:00
error_reason = "port not in permitted opens";
} else { /* local and dynamic forwards */
/* Ditto */
2018-05-06 12:24:45 +00:00
if (channel_cancel_lport_listener(ssh, &fwd, fwd.connect_port,
2015-01-05 16:09:55 +00:00
&options.fwd_opts) == -1)
2012-08-29 15:46:01 +00:00
error_reason = "port not found";
}
2018-08-28 10:47:58 +00:00
if (error_reason != NULL)
reply_error(reply, MUX_S_FAILURE, rid, error_reason);
else {
reply_ok(reply, rid);
2013-09-18 17:27:38 +00:00
free(found_fwd->listen_host);
2015-01-05 16:09:55 +00:00
free(found_fwd->listen_path);
2013-09-18 17:27:38 +00:00
free(found_fwd->connect_host);
2015-01-05 16:09:55 +00:00
free(found_fwd->connect_path);
2012-08-29 15:46:01 +00:00
found_fwd->listen_host = found_fwd->connect_host = NULL;
2015-01-05 16:09:55 +00:00
found_fwd->listen_path = found_fwd->connect_path = NULL;
2012-08-29 15:46:01 +00:00
found_fwd->listen_port = found_fwd->connect_port = 0;
}
2010-03-08 11:19:52 +00:00
out:
2013-09-18 17:27:38 +00:00
free(fwd_desc);
2015-01-05 16:09:55 +00:00
free(listen_addr);
free(connect_addr);
2010-03-08 11:19:52 +00:00
return ret;
}
static int
2019-02-05 15:03:53 +00:00
mux_master_process_stdio_fwd(struct ssh *ssh, u_int rid,
2018-08-28 10:47:58 +00:00
Channel *c, struct sshbuf *m, struct sshbuf *reply)
2010-03-08 11:19:52 +00:00
{
Channel *nc;
2018-08-28 10:47:58 +00:00
char *chost = NULL;
2023-08-10 16:16:53 +00:00
u_int _cport, i, j;
int ok = 0, cport, r, new_fd[2];
2015-01-05 16:09:55 +00:00
struct mux_stdio_confirm_ctx *cctx;
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
(r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
2023-08-10 16:16:53 +00:00
(r = sshbuf_get_u32(m, &_cport)) != 0) {
2013-09-18 17:27:38 +00:00
free(chost);
2021-04-23 19:10:38 +00:00
error_f("malformed message");
2010-03-08 11:19:52 +00:00
return -1;
}
2023-08-10 16:16:53 +00:00
if (_cport == (u_int)PORT_STREAMLOCAL)
cport = PORT_STREAMLOCAL;
else if (_cport <= INT_MAX)
cport = (int)_cport;
else {
free(chost);
error_f("invalid port 0x%x", _cport);
return -1;
}
2010-03-08 11:19:52 +00:00
2023-08-10 16:16:53 +00:00
debug2_f("channel %d: stdio fwd to %s:%d", c->self, chost, cport);
2010-03-08 11:19:52 +00:00
/* Gather fds from client */
for(i = 0; i < 2; i++) {
if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
2021-04-23 19:10:38 +00:00
error_f("failed to receive fd %d from client", i);
2010-03-08 11:19:52 +00:00
for (j = 0; j < i; j++)
close(new_fd[j]);
2013-09-18 17:27:38 +00:00
free(chost);
2010-03-08 11:19:52 +00:00
/* prepare reply */
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_FAILURE, rid,
2010-03-08 11:19:52 +00:00
"did not receive file descriptors");
return -1;
}
}
2021-04-23 19:10:38 +00:00
debug3_f("got fds stdin %d, stdout %d", new_fd[0], new_fd[1]);
2010-03-08 11:19:52 +00:00
/* XXX support multiple child sessions in future */
2018-05-06 12:24:45 +00:00
if (c->have_remote_id) {
2021-04-23 19:10:38 +00:00
debug2_f("session already open");
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_FAILURE, rid,
"Multiple sessions not supported");
2010-03-08 11:19:52 +00:00
cleanup:
close(new_fd[0]);
close(new_fd[1]);
2013-09-18 17:27:38 +00:00
free(chost);
2010-03-08 11:19:52 +00:00
return 0;
}
if (options.control_master == SSHCTL_MASTER_ASK ||
options.control_master == SSHCTL_MASTER_AUTO_ASK) {
2023-08-10 16:16:53 +00:00
if (cport == PORT_STREAMLOCAL) {
ok = ask_permission("Allow forward to path %s", chost);
} else {
ok = ask_permission("Allow forward to [%s]:%d? ",
chost, cport);
}
if (!ok) {
2021-04-23 19:10:38 +00:00
debug2_f("stdio fwd refused by user");
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
"Permission denied");
2010-03-08 11:19:52 +00:00
goto cleanup;
}
}
2021-08-30 19:14:33 +00:00
nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1],
CHANNEL_NONBLOCK_STDIO);
2018-08-28 10:47:58 +00:00
free(chost);
2010-03-08 11:19:52 +00:00
nc->ctl_chan = c->self; /* link session -> control channel */
2021-04-23 19:10:38 +00:00
c->remote_id = nc->self; /* link control -> session channel */
2018-05-06 12:24:45 +00:00
c->have_remote_id = 1;
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug2_f("channel_new: %d control %d", nc->self, nc->ctl_chan);
2010-03-08 11:19:52 +00:00
2018-05-06 12:24:45 +00:00
channel_register_cleanup(ssh, nc->self,
mux_master_session_cleanup_cb, 1);
2010-03-08 11:19:52 +00:00
2015-01-05 16:09:55 +00:00
cctx = xcalloc(1, sizeof(*cctx));
cctx->rid = rid;
2018-05-06 12:24:45 +00:00
channel_register_open_confirm(ssh, nc->self, mux_stdio_confirm, cctx);
2015-01-05 16:09:55 +00:00
c->mux_pause = 1; /* stop handling messages until open_confirm done */
2010-03-08 11:19:52 +00:00
2015-01-05 16:09:55 +00:00
/* reply is deferred, sent by mux_session_confirm */
2010-03-08 11:19:52 +00:00
return 0;
}
2015-01-05 16:09:55 +00:00
/* Callback on open confirmation in mux master for a mux stdio fwd session. */
static void
2018-05-06 12:24:45 +00:00
mux_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
2015-01-05 16:09:55 +00:00
{
struct mux_stdio_confirm_ctx *cctx = arg;
Channel *c, *cc;
2018-08-28 10:47:58 +00:00
struct sshbuf *reply;
int r;
2015-01-05 16:09:55 +00:00
if (cctx == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("cctx == NULL");
2018-05-06 12:24:45 +00:00
if ((c = channel_by_id(ssh, id)) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("no channel for id %d", id);
2018-05-06 12:24:45 +00:00
if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("channel %d lacks control channel %d",
2015-01-05 16:09:55 +00:00
id, c->ctl_chan);
2018-08-28 10:47:58 +00:00
if ((reply = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2015-01-05 16:09:55 +00:00
if (!success) {
2021-04-23 19:10:38 +00:00
debug3_f("sending failure reply");
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_FAILURE, cctx->rid,
"Session open refused by peer");
2015-01-05 16:09:55 +00:00
/* prepare reply */
goto done;
}
2021-04-23 19:10:38 +00:00
debug3_f("sending success reply");
2015-01-05 16:09:55 +00:00
/* prepare reply */
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
(r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
(r = sshbuf_put_u32(reply, c->self)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply");
2015-01-05 16:09:55 +00:00
done:
/* Send reply */
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "enqueue");
2018-08-28 10:47:58 +00:00
sshbuf_free(reply);
2015-01-05 16:09:55 +00:00
if (cc->mux_pause <= 0)
2021-04-23 19:10:38 +00:00
fatal_f("mux_pause %d", cc->mux_pause);
2015-01-05 16:09:55 +00:00
cc->mux_pause = 0; /* start processing messages again */
c->open_confirm_ctx = NULL;
free(cctx);
}
2011-09-28 08:14:41 +00:00
static int
2019-02-05 15:03:53 +00:00
mux_master_process_stop_listening(struct ssh *ssh, u_int rid,
2018-08-28 10:47:58 +00:00
Channel *c, struct sshbuf *m, struct sshbuf *reply)
2011-09-28 08:14:41 +00:00
{
2021-04-23 19:10:38 +00:00
debug_f("channel %d: stop listening", c->self);
2011-09-28 08:14:41 +00:00
if (options.control_master == SSHCTL_MASTER_ASK ||
options.control_master == SSHCTL_MASTER_AUTO_ASK) {
if (!ask_permission("Disable further multiplexing on shared "
"connection to %s? ", host)) {
2021-04-23 19:10:38 +00:00
debug2_f("stop listen refused by user");
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
"Permission denied");
2011-09-28 08:14:41 +00:00
return 0;
}
}
if (mux_listener_channel != NULL) {
2018-05-06 12:24:45 +00:00
channel_free(ssh, mux_listener_channel);
2011-09-28 08:14:41 +00:00
client_stop_mux();
2013-09-18 17:27:38 +00:00
free(options.control_path);
2011-09-28 08:14:41 +00:00
options.control_path = NULL;
mux_listener_channel = NULL;
muxserver_sock = -1;
}
2018-08-28 10:47:58 +00:00
reply_ok(reply, rid);
2011-09-28 08:14:41 +00:00
return 0;
}
2017-01-31 12:33:47 +00:00
static int
2019-02-05 15:03:53 +00:00
mux_master_process_proxy(struct ssh *ssh, u_int rid,
2018-08-28 10:47:58 +00:00
Channel *c, struct sshbuf *m, struct sshbuf *reply)
2017-01-31 12:33:47 +00:00
{
2018-08-28 10:47:58 +00:00
int r;
2021-04-23 19:10:38 +00:00
debug_f("channel %d: proxy request", c->self);
2017-01-31 12:33:47 +00:00
c->mux_rcb = channel_proxy_downstream;
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(reply, MUX_S_PROXY)) != 0 ||
(r = sshbuf_put_u32(reply, rid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply");
2017-01-31 12:33:47 +00:00
return 0;
}
2021-02-14 21:09:58 +00:00
/* Channel callbacks fired on read/write from mux client fd */
2010-03-08 11:19:52 +00:00
static int
2018-05-06 12:24:45 +00:00
mux_master_read_cb(struct ssh *ssh, Channel *c)
2010-03-08 11:19:52 +00:00
{
struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
2018-08-28 10:47:58 +00:00
struct sshbuf *in = NULL, *out = NULL;
u_int type, rid, i;
int r, ret = -1;
if ((out = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2010-03-08 11:19:52 +00:00
/* Setup ctx and */
if (c->mux_ctx == NULL) {
2010-11-08 10:45:44 +00:00
state = xcalloc(1, sizeof(*state));
2010-03-08 11:19:52 +00:00
c->mux_ctx = state;
2018-05-06 12:24:45 +00:00
channel_register_cleanup(ssh, c->self,
2010-03-08 11:19:52 +00:00
mux_master_control_cleanup_cb, 0);
/* Send hello */
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(out, MUX_MSG_HELLO)) != 0 ||
(r = sshbuf_put_u32(out, SSHMUX_VER)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply");
2010-03-08 11:19:52 +00:00
/* no extensions */
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_stringb(c->output, out)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "enqueue");
debug3_f("channel %d: hello sent", c->self);
2018-08-28 10:47:58 +00:00
ret = 0;
goto out;
2010-03-08 11:19:52 +00:00
}
/* Channel code ensures that we receive whole packets */
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_froms(c->input, &in)) != 0) {
2010-03-08 11:19:52 +00:00
malf:
2021-04-23 19:10:38 +00:00
error_f("malformed message");
2010-03-08 11:19:52 +00:00
goto out;
}
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(in, &type)) != 0)
2010-03-08 11:19:52 +00:00
goto malf;
2021-04-23 19:10:38 +00:00
debug3_f("channel %d packet type 0x%08x len %zu", c->self,
type, sshbuf_len(in));
2010-03-08 11:19:52 +00:00
if (type == MUX_MSG_HELLO)
rid = 0;
else {
if (!state->hello_rcvd) {
2021-04-23 19:10:38 +00:00
error_f("expected MUX_MSG_HELLO(0x%08x), "
"received 0x%08x", MUX_MSG_HELLO, type);
2010-03-08 11:19:52 +00:00
goto out;
}
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(in, &rid)) != 0)
2010-03-08 11:19:52 +00:00
goto malf;
}
for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
if (type == mux_master_handlers[i].type) {
2018-05-06 12:24:45 +00:00
ret = mux_master_handlers[i].handler(ssh, rid,
2018-08-28 10:47:58 +00:00
c, in, out);
2010-03-08 11:19:52 +00:00
break;
}
}
if (mux_master_handlers[i].handler == NULL) {
2021-04-23 19:10:38 +00:00
error_f("unsupported mux message 0x%08x", type);
2018-08-28 10:47:58 +00:00
reply_error(out, MUX_S_FAILURE, rid, "unsupported request");
2010-03-08 11:19:52 +00:00
ret = 0;
}
/* Enqueue reply packet */
2021-04-23 19:10:38 +00:00
if (sshbuf_len(out) != 0 &&
(r = sshbuf_put_stringb(c->output, out)) != 0)
fatal_fr(r, "enqueue");
2010-03-08 11:19:52 +00:00
out:
2018-08-28 10:47:58 +00:00
sshbuf_free(in);
sshbuf_free(out);
2010-03-08 11:19:52 +00:00
return ret;
}
void
2018-05-06 12:24:45 +00:00
mux_exit_message(struct ssh *ssh, Channel *c, int exitval)
2010-03-08 11:19:52 +00:00
{
2018-08-28 10:47:58 +00:00
struct sshbuf *m;
2010-03-08 11:19:52 +00:00
Channel *mux_chan;
2018-08-28 10:47:58 +00:00
int r;
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("channel %d: exit message, exitval %d", c->self, exitval);
2010-03-08 11:19:52 +00:00
2018-05-06 12:24:45 +00:00
if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("channel %d missing mux %d", c->self, c->ctl_chan);
2010-03-08 11:19:52 +00:00
/* Append exit message packet to control socket output queue */
2018-08-28 10:47:58 +00:00
if ((m = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(m, MUX_S_EXIT_MESSAGE)) != 0 ||
(r = sshbuf_put_u32(m, c->self)) != 0 ||
(r = sshbuf_put_u32(m, exitval)) != 0 ||
(r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply");
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
}
2011-09-28 08:14:41 +00:00
void
2018-05-06 12:24:45 +00:00
mux_tty_alloc_failed(struct ssh *ssh, Channel *c)
2011-09-28 08:14:41 +00:00
{
2018-08-28 10:47:58 +00:00
struct sshbuf *m;
2011-09-28 08:14:41 +00:00
Channel *mux_chan;
2018-08-28 10:47:58 +00:00
int r;
2011-09-28 08:14:41 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("channel %d: TTY alloc failed", c->self);
2011-09-28 08:14:41 +00:00
2018-05-06 12:24:45 +00:00
if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("channel %d missing mux %d", c->self, c->ctl_chan);
2011-09-28 08:14:41 +00:00
/* Append exit message packet to control socket output queue */
2018-08-28 10:47:58 +00:00
if ((m = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(m, MUX_S_TTY_ALLOC_FAIL)) != 0 ||
(r = sshbuf_put_u32(m, c->self)) != 0 ||
(r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply");
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2011-09-28 08:14:41 +00:00
}
2010-03-08 11:19:52 +00:00
/* Prepare a mux master to listen on a Unix domain socket. */
void
2018-05-06 12:24:45 +00:00
muxserver_listen(struct ssh *ssh)
2010-03-08 11:19:52 +00:00
{
mode_t old_umask;
2011-02-17 11:47:40 +00:00
char *orig_control_path = options.control_path;
char rbuf[16+1];
u_int i, r;
2015-01-05 16:09:55 +00:00
int oerrno;
2010-03-08 11:19:52 +00:00
if (options.control_path == NULL ||
options.control_master == SSHCTL_MASTER_NO)
return;
debug("setting up multiplex master socket");
2011-02-17 11:47:40 +00:00
/*
* Use a temporary path before listen so we can pseudo-atomically
* establish the listening socket in its final location to avoid
* other processes racing in between bind() and listen() and hitting
* an unready socket.
*/
for (i = 0; i < sizeof(rbuf) - 1; i++) {
r = arc4random_uniform(26+26+10);
rbuf[i] = (r < 26) ? 'a' + r :
(r < 26*2) ? 'A' + r - 26 :
'0' + r - 26 - 26;
}
rbuf[sizeof(rbuf) - 1] = '\0';
options.control_path = NULL;
xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
2021-04-23 19:10:38 +00:00
debug3_f("temporary control path %s", options.control_path);
2011-02-17 11:47:40 +00:00
2010-03-08 11:19:52 +00:00
old_umask = umask(0177);
2015-01-05 16:09:55 +00:00
muxserver_sock = unix_listener(options.control_path, 64, 0);
oerrno = errno;
umask(old_umask);
if (muxserver_sock < 0) {
if (oerrno == EINVAL || oerrno == EADDRINUSE) {
2010-03-08 11:19:52 +00:00
error("ControlSocket %s already exists, "
"disabling multiplexing", options.control_path);
2011-02-17 11:47:40 +00:00
disable_mux_master:
2011-09-28 08:14:41 +00:00
if (muxserver_sock != -1) {
close(muxserver_sock);
muxserver_sock = -1;
}
2013-09-18 17:27:38 +00:00
free(orig_control_path);
free(options.control_path);
2010-03-08 11:19:52 +00:00
options.control_path = NULL;
options.control_master = SSHCTL_MASTER_NO;
return;
2015-01-05 16:09:55 +00:00
} else {
/* unix_listener() logs the error */
cleanup_exit(255);
}
2010-03-08 11:19:52 +00:00
}
2011-02-17 11:47:40 +00:00
/* Now atomically "move" the mux socket into position */
if (link(options.control_path, orig_control_path) != 0) {
if (errno != EEXIST) {
2021-04-23 19:10:38 +00:00
fatal_f("link mux listener %s => %s: %s",
2011-02-17 11:47:40 +00:00
options.control_path, orig_control_path,
strerror(errno));
}
error("ControlSocket %s already exists, disabling multiplexing",
orig_control_path);
unlink(options.control_path);
goto disable_mux_master;
}
unlink(options.control_path);
2013-09-18 17:27:38 +00:00
free(options.control_path);
2011-02-17 11:47:40 +00:00
options.control_path = orig_control_path;
2010-03-08 11:19:52 +00:00
set_nonblock(muxserver_sock);
2018-05-06 12:24:45 +00:00
mux_listener_channel = channel_new(ssh, "mux listener",
2010-03-08 11:19:52 +00:00
SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
2011-02-17 11:47:40 +00:00
0, options.control_path, 1);
2010-03-08 11:19:52 +00:00
mux_listener_channel->mux_rcb = mux_master_read_cb;
2021-04-23 19:10:38 +00:00
debug3_f("mux listener channel %d fd %d",
2010-03-08 11:19:52 +00:00
mux_listener_channel->self, mux_listener_channel->sock);
}
/* Callback on open confirmation in mux master for a mux client session. */
static void
2018-05-06 12:24:45 +00:00
mux_session_confirm(struct ssh *ssh, int id, int success, void *arg)
2010-03-08 11:19:52 +00:00
{
struct mux_session_confirm_ctx *cctx = arg;
const char *display;
2010-11-08 10:45:44 +00:00
Channel *c, *cc;
2018-08-28 10:47:58 +00:00
int i, r;
struct sshbuf *reply;
2010-03-08 11:19:52 +00:00
if (cctx == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("cctx == NULL");
2018-05-06 12:24:45 +00:00
if ((c = channel_by_id(ssh, id)) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("no channel for id %d", id);
2018-05-06 12:24:45 +00:00
if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("channel %d lacks control channel %d",
2010-11-08 10:45:44 +00:00
id, c->ctl_chan);
2018-08-28 10:47:58 +00:00
if ((reply = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2010-11-08 10:45:44 +00:00
if (!success) {
2021-04-23 19:10:38 +00:00
debug3_f("sending failure reply");
2018-08-28 10:47:58 +00:00
reply_error(reply, MUX_S_FAILURE, cctx->rid,
"Session open refused by peer");
2010-11-08 10:45:44 +00:00
goto done;
}
2010-03-08 11:19:52 +00:00
display = getenv("DISPLAY");
if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
char *proto, *data;
2010-11-08 10:45:44 +00:00
2010-03-08 11:19:52 +00:00
/* Get reasonable local authentication information. */
2018-05-06 12:24:45 +00:00
if (client_x11_get_proto(ssh, display, options.xauth_location,
2010-11-08 10:45:44 +00:00
options.forward_x11_trusted, options.forward_x11_timeout,
2016-03-10 20:10:25 +00:00
&proto, &data) == 0) {
/* Request forwarding with authentication spoofing. */
debug("Requesting X11 forwarding with authentication "
"spoofing.");
2018-05-06 12:24:45 +00:00
x11_request_forwarding_with_spoofing(ssh, id,
display, proto, data, 1);
2016-03-10 20:10:25 +00:00
/* XXX exit_on_forward_failure */
2018-05-06 12:24:45 +00:00
client_expect_confirm(ssh, id, "X11 forwarding",
2016-03-10 20:10:25 +00:00
CONFIRM_WARN);
}
2010-03-08 11:19:52 +00:00
}
if (cctx->want_agent_fwd && options.forward_agent) {
debug("Requesting authentication agent forwarding.");
2018-05-06 12:24:45 +00:00
channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
2020-02-14 19:47:15 +00:00
if ((r = sshpkt_send(ssh)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "send");
2010-03-08 11:19:52 +00:00
}
2018-05-06 12:24:45 +00:00
client_session2_setup(ssh, id, cctx->want_tty, cctx->want_subsys,
2018-08-28 10:47:58 +00:00
cctx->term, &cctx->tio, c->rfd, cctx->cmd, cctx->env);
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("sending success reply");
2010-11-08 10:45:44 +00:00
/* prepare reply */
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
(r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
(r = sshbuf_put_u32(reply, c->self)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reply");
2010-11-08 10:45:44 +00:00
done:
/* Send reply */
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "enqueue");
2018-08-28 10:47:58 +00:00
sshbuf_free(reply);
2010-11-08 10:45:44 +00:00
if (cc->mux_pause <= 0)
2021-04-23 19:10:38 +00:00
fatal_f("mux_pause %d", cc->mux_pause);
2010-11-08 10:45:44 +00:00
cc->mux_pause = 0; /* start processing messages again */
2010-03-08 11:19:52 +00:00
c->open_confirm_ctx = NULL;
2018-08-28 10:47:58 +00:00
sshbuf_free(cctx->cmd);
2013-09-18 17:27:38 +00:00
free(cctx->term);
2010-03-08 11:19:52 +00:00
if (cctx->env != NULL) {
for (i = 0; cctx->env[i] != NULL; i++)
2013-09-18 17:27:38 +00:00
free(cctx->env[i]);
free(cctx->env);
2010-03-08 11:19:52 +00:00
}
2013-09-18 17:27:38 +00:00
free(cctx);
2010-03-08 11:19:52 +00:00
}
/* ** Multiplexing client support */
/* Exit signal handler */
static void
control_client_sighandler(int signo)
{
muxclient_terminate = signo;
}
/*
* Relay signal handler - used to pass some signals from mux client to
* mux master.
*/
static void
control_client_sigrelay(int signo)
{
int save_errno = errno;
if (muxserver_pid > 1)
kill(muxserver_pid, signo);
errno = save_errno;
}
static int
2023-08-10 16:16:53 +00:00
mux_client_read(int fd, struct sshbuf *b, size_t need, int timeout_ms)
2010-03-08 11:19:52 +00:00
{
2018-08-28 10:47:58 +00:00
size_t have;
2010-03-08 11:19:52 +00:00
ssize_t len;
u_char *p;
2018-08-28 10:47:58 +00:00
int r;
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_reserve(b, need, &p)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "reserve");
2010-03-08 11:19:52 +00:00
for (have = 0; have < need; ) {
if (muxclient_terminate) {
errno = EINTR;
return -1;
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
len = read(fd, p + have, need - have);
2021-02-14 21:00:25 +00:00
if (len == -1) {
2010-03-08 11:19:52 +00:00
switch (errno) {
#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
case EWOULDBLOCK:
#endif
case EAGAIN:
2023-10-04 12:06:41 +00:00
if (waitrfd(fd, &timeout_ms,
&muxclient_terminate) == -1 &&
errno != EINTR)
2023-08-10 16:16:53 +00:00
return -1; /* timeout */
2010-03-08 11:19:52 +00:00
/* FALLTHROUGH */
case EINTR:
continue;
default:
return -1;
}
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
if (len == 0) {
errno = EPIPE;
return -1;
}
2018-08-28 10:47:58 +00:00
have += (size_t)len;
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
return 0;
}
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
static int
2018-08-28 10:47:58 +00:00
mux_client_write_packet(int fd, struct sshbuf *m)
2010-03-08 11:19:52 +00:00
{
2018-08-28 10:47:58 +00:00
struct sshbuf *queue;
2010-03-08 11:19:52 +00:00
u_int have, need;
2018-08-28 10:47:58 +00:00
int r, oerrno, len;
const u_char *ptr;
2010-03-08 11:19:52 +00:00
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLOUT;
2018-08-28 10:47:58 +00:00
if ((queue = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_stringb(queue, m)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "enqueue");
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
need = sshbuf_len(queue);
ptr = sshbuf_ptr(queue);
2010-03-08 11:19:52 +00:00
for (have = 0; have < need; ) {
if (muxclient_terminate) {
2018-08-28 10:47:58 +00:00
sshbuf_free(queue);
2010-03-08 11:19:52 +00:00
errno = EINTR;
return -1;
}
len = write(fd, ptr + have, need - have);
2021-02-14 21:00:25 +00:00
if (len == -1) {
2010-03-08 11:19:52 +00:00
switch (errno) {
#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
case EWOULDBLOCK:
#endif
case EAGAIN:
(void)poll(&pfd, 1, -1);
/* FALLTHROUGH */
case EINTR:
continue;
default:
oerrno = errno;
2018-08-28 10:47:58 +00:00
sshbuf_free(queue);
2010-03-08 11:19:52 +00:00
errno = oerrno;
return -1;
}
}
if (len == 0) {
2018-08-28 10:47:58 +00:00
sshbuf_free(queue);
2010-03-08 11:19:52 +00:00
errno = EPIPE;
return -1;
}
have += (u_int)len;
2008-07-23 09:33:08 +00:00
}
2018-08-28 10:47:58 +00:00
sshbuf_free(queue);
2010-03-08 11:19:52 +00:00
return 0;
}
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
static int
2023-08-10 16:16:53 +00:00
mux_client_read_packet_timeout(int fd, struct sshbuf *m, int timeout_ms)
2010-03-08 11:19:52 +00:00
{
2018-08-28 10:47:58 +00:00
struct sshbuf *queue;
size_t need, have;
2015-01-05 16:09:55 +00:00
const u_char *ptr;
2018-08-28 10:47:58 +00:00
int r, oerrno;
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if ((queue = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2023-08-10 16:16:53 +00:00
if (mux_client_read(fd, queue, 4, timeout_ms) != 0) {
2010-03-08 11:19:52 +00:00
if ((oerrno = errno) == EPIPE)
2021-04-23 19:10:38 +00:00
debug3_f("read header failed: %s",
2013-09-18 17:27:38 +00:00
strerror(errno));
2018-08-28 10:47:58 +00:00
sshbuf_free(queue);
2010-03-08 11:19:52 +00:00
errno = oerrno;
return -1;
}
2018-08-28 10:47:58 +00:00
need = PEEK_U32(sshbuf_ptr(queue));
2023-08-10 16:16:53 +00:00
if (mux_client_read(fd, queue, need, timeout_ms) != 0) {
2010-03-08 11:19:52 +00:00
oerrno = errno;
2021-04-23 19:10:38 +00:00
debug3_f("read body failed: %s", strerror(errno));
2018-08-28 10:47:58 +00:00
sshbuf_free(queue);
2010-03-08 11:19:52 +00:00
errno = oerrno;
return -1;
}
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_string_direct(queue, &ptr, &have)) != 0 ||
(r = sshbuf_put(m, ptr, have)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "dequeue");
2018-08-28 10:47:58 +00:00
sshbuf_free(queue);
2010-03-08 11:19:52 +00:00
return 0;
}
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
static int
2023-08-10 16:16:53 +00:00
mux_client_read_packet(int fd, struct sshbuf *m)
{
return mux_client_read_packet_timeout(fd, m, -1);
}
static int
mux_client_hello_exchange(int fd, int timeout_ms)
2010-03-08 11:19:52 +00:00
{
2018-08-28 10:47:58 +00:00
struct sshbuf *m;
2010-03-08 11:19:52 +00:00
u_int type, ver;
2018-08-28 10:47:58 +00:00
int r, ret = -1;
2008-07-23 09:33:08 +00:00
2018-08-28 10:47:58 +00:00
if ((m = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(m, MUX_MSG_HELLO)) != 0 ||
(r = sshbuf_put_u32(m, SSHMUX_VER)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "assemble hello");
2010-03-08 11:19:52 +00:00
/* no extensions */
2008-07-23 09:33:08 +00:00
2018-08-28 10:47:58 +00:00
if (mux_client_write_packet(fd, m) != 0) {
2021-04-23 19:10:38 +00:00
debug_f("write packet: %s", strerror(errno));
2018-05-06 12:24:45 +00:00
goto out;
}
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
sshbuf_reset(m);
2010-03-08 11:19:52 +00:00
/* Read their HELLO */
2023-08-10 16:16:53 +00:00
if (mux_client_read_packet_timeout(fd, m, timeout_ms) != 0) {
2021-04-23 19:10:38 +00:00
debug_f("read packet failed");
2018-05-06 12:24:45 +00:00
goto out;
2010-03-08 11:19:52 +00:00
}
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &type)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse type");
2018-05-06 12:24:45 +00:00
if (type != MUX_MSG_HELLO) {
2021-04-23 19:10:38 +00:00
error_f("expected HELLO (%u) got %u", MUX_MSG_HELLO, type);
2018-05-06 12:24:45 +00:00
goto out;
}
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &ver)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse version");
2018-05-06 12:24:45 +00:00
if (ver != SSHMUX_VER) {
error("Unsupported multiplexing protocol version %d "
2010-03-08 11:19:52 +00:00
"(expected %d)", ver, SSHMUX_VER);
2018-05-06 12:24:45 +00:00
goto out;
}
2021-04-23 19:10:38 +00:00
debug2_f("master version %u", ver);
2010-03-08 11:19:52 +00:00
/* No extensions are presently defined */
2018-08-28 10:47:58 +00:00
while (sshbuf_len(m) > 0) {
char *name = NULL;
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
(r = sshbuf_skip_string(m)) != 0) { /* value */
2021-04-23 19:10:38 +00:00
error_fr(r, "parse extension");
2018-08-28 10:47:58 +00:00
goto out;
}
2010-03-08 11:19:52 +00:00
debug2("Unrecognised master extension \"%s\"", name);
2013-09-18 17:27:38 +00:00
free(name);
2008-07-23 09:33:08 +00:00
}
2018-05-06 12:24:45 +00:00
/* success */
ret = 0;
out:
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2018-05-06 12:24:45 +00:00
return ret;
2010-03-08 11:19:52 +00:00
}
static u_int
mux_client_request_alive(int fd)
{
2018-08-28 10:47:58 +00:00
struct sshbuf *m;
2010-03-08 11:19:52 +00:00
char *e;
u_int pid, type, rid;
2018-08-28 10:47:58 +00:00
int r;
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("entering");
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if ((m = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(m, MUX_C_ALIVE_CHECK)) != 0 ||
(r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "assemble");
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if (mux_client_write_packet(fd, m) != 0)
2021-04-23 19:10:38 +00:00
fatal_f("write packet: %s", strerror(errno));
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
sshbuf_reset(m);
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
/* Read their reply */
2018-08-28 10:47:58 +00:00
if (mux_client_read_packet(fd, m) != 0) {
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
return 0;
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &type)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse type");
2010-03-08 11:19:52 +00:00
if (type != MUX_S_ALIVE) {
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
fatal_f("master returned error: %s", e);
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &rid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse remote ID");
2018-08-28 10:47:58 +00:00
if (rid != muxclient_request_id)
2021-04-23 19:10:38 +00:00
fatal_f("out of sequence reply: my id %u theirs %u",
muxclient_request_id, rid);
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &pid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse PID");
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("done pid = %u", pid);
2010-03-08 11:19:52 +00:00
muxclient_request_id++;
return pid;
}
static void
mux_client_request_terminate(int fd)
{
2018-08-28 10:47:58 +00:00
struct sshbuf *m;
2010-03-08 11:19:52 +00:00
char *e;
u_int type, rid;
2018-08-28 10:47:58 +00:00
int r;
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("entering");
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if ((m = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(m, MUX_C_TERMINATE)) != 0 ||
(r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "request");
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if (mux_client_write_packet(fd, m) != 0)
2021-04-23 19:10:38 +00:00
fatal_f("write packet: %s", strerror(errno));
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
sshbuf_reset(m);
2010-03-08 11:19:52 +00:00
/* Read their reply */
2018-08-28 10:47:58 +00:00
if (mux_client_read_packet(fd, m) != 0) {
2010-03-08 11:19:52 +00:00
/* Remote end exited already */
if (errno == EPIPE) {
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
return;
}
2021-04-23 19:10:38 +00:00
fatal_f("read from master failed: %s", strerror(errno));
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &type)) != 0 ||
(r = sshbuf_get_u32(m, &rid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse");
2018-08-28 10:47:58 +00:00
if (rid != muxclient_request_id)
2021-04-23 19:10:38 +00:00
fatal_f("out of sequence reply: my id %u theirs %u",
muxclient_request_id, rid);
2010-03-08 11:19:52 +00:00
switch (type) {
case MUX_S_OK:
break;
case MUX_S_PERMISSION_DENIED:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
2010-03-08 11:19:52 +00:00
fatal("Master refused termination request: %s", e);
case MUX_S_FAILURE:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
fatal_f("termination request failed: %s", e);
2010-03-08 11:19:52 +00:00
default:
2021-04-23 19:10:38 +00:00
fatal_f("unexpected response from master 0x%08x", type);
2008-07-23 09:33:08 +00:00
}
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
muxclient_request_id++;
}
static int
2015-01-05 16:09:55 +00:00
mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd)
2010-03-08 11:19:52 +00:00
{
2018-08-28 10:47:58 +00:00
struct sshbuf *m;
2010-03-08 11:19:52 +00:00
char *e, *fwd_desc;
2018-08-28 10:47:58 +00:00
const char *lhost, *chost;
2010-03-08 11:19:52 +00:00
u_int type, rid;
2018-08-28 10:47:58 +00:00
int r;
2010-03-08 11:19:52 +00:00
fwd_desc = format_forward(ftype, fwd);
2012-08-29 15:46:01 +00:00
debug("Requesting %s %s",
cancel_flag ? "cancellation of" : "forwarding of", fwd_desc);
2013-09-18 17:27:38 +00:00
free(fwd_desc);
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
type = cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD;
if (fwd->listen_path != NULL)
lhost = fwd->listen_path;
else if (fwd->listen_host == NULL)
lhost = "";
else if (*fwd->listen_host == '\0')
lhost = "*";
else
lhost = fwd->listen_host;
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if (fwd->connect_path != NULL)
chost = fwd->connect_path;
else if (fwd->connect_host == NULL)
chost = "";
else
chost = fwd->connect_host;
if ((m = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(m, type)) != 0 ||
(r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
(r = sshbuf_put_u32(m, ftype)) != 0 ||
(r = sshbuf_put_cstring(m, lhost)) != 0 ||
(r = sshbuf_put_u32(m, fwd->listen_port)) != 0 ||
(r = sshbuf_put_cstring(m, chost)) != 0 ||
(r = sshbuf_put_u32(m, fwd->connect_port)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "request");
2018-08-28 10:47:58 +00:00
if (mux_client_write_packet(fd, m) != 0)
2021-04-23 19:10:38 +00:00
fatal_f("write packet: %s", strerror(errno));
2008-07-23 09:33:08 +00:00
2018-08-28 10:47:58 +00:00
sshbuf_reset(m);
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
/* Read their reply */
2018-08-28 10:47:58 +00:00
if (mux_client_read_packet(fd, m) != 0) {
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
return -1;
}
2008-07-23 09:33:08 +00:00
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &type)) != 0 ||
(r = sshbuf_get_u32(m, &rid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse");
2018-08-28 10:47:58 +00:00
if (rid != muxclient_request_id)
2021-04-23 19:10:38 +00:00
fatal_f("out of sequence reply: my id %u theirs %u",
muxclient_request_id, rid);
2018-08-28 10:47:58 +00:00
2010-03-08 11:19:52 +00:00
switch (type) {
case MUX_S_OK:
2008-07-23 09:33:08 +00:00
break;
2010-11-08 10:45:44 +00:00
case MUX_S_REMOTE_PORT:
2012-08-29 15:46:01 +00:00
if (cancel_flag)
2021-04-23 19:10:38 +00:00
fatal_f("got MUX_S_REMOTE_PORT for cancel");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &fwd->allocated_port)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse port");
2015-07-02 13:18:50 +00:00
verbose("Allocated port %u for remote forward to %s:%d",
2010-11-08 10:45:44 +00:00
fwd->allocated_port,
fwd->connect_host ? fwd->connect_host : "",
fwd->connect_port);
if (muxclient_command == SSHMUX_COMMAND_FORWARD)
2016-03-10 20:10:25 +00:00
fprintf(stdout, "%i\n", fwd->allocated_port);
2010-11-08 10:45:44 +00:00
break;
2010-03-08 11:19:52 +00:00
case MUX_S_PERMISSION_DENIED:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
error("Master refused forwarding request: %s", e);
return -1;
case MUX_S_FAILURE:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2021-04-23 19:10:38 +00:00
error_f("forwarding request failed: %s", e);
2010-03-08 11:19:52 +00:00
return -1;
2008-07-23 09:33:08 +00:00
default:
2021-04-23 19:10:38 +00:00
fatal_f("unexpected response from master 0x%08x", type);
2010-03-08 11:19:52 +00:00
}
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
muxclient_request_id++;
return 0;
}
static int
2012-08-29 15:46:01 +00:00
mux_client_forwards(int fd, int cancel_flag)
2010-03-08 11:19:52 +00:00
{
2012-08-29 15:46:01 +00:00
int i, ret = 0;
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("%s forwardings: %d local, %d remote",
2012-08-29 15:46:01 +00:00
cancel_flag ? "cancel" : "request",
2010-03-08 11:19:52 +00:00
options.num_local_forwards, options.num_remote_forwards);
/* XXX ExitOnForwardingFailure */
for (i = 0; i < options.num_local_forwards; i++) {
2012-08-29 15:46:01 +00:00
if (mux_client_forward(fd, cancel_flag,
2010-03-08 11:19:52 +00:00
options.local_forwards[i].connect_port == 0 ?
MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
options.local_forwards + i) != 0)
2012-08-29 15:46:01 +00:00
ret = -1;
2008-07-23 09:33:08 +00:00
}
2010-03-08 11:19:52 +00:00
for (i = 0; i < options.num_remote_forwards; i++) {
2012-08-29 15:46:01 +00:00
if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE,
2010-03-08 11:19:52 +00:00
options.remote_forwards + i) != 0)
2012-08-29 15:46:01 +00:00
ret = -1;
2010-03-08 11:19:52 +00:00
}
2012-08-29 15:46:01 +00:00
return ret;
2010-03-08 11:19:52 +00:00
}
static int
mux_client_request_session(int fd)
{
2018-08-28 10:47:58 +00:00
struct sshbuf *m;
char *e;
2021-08-30 19:14:33 +00:00
const char *term = NULL;
2022-10-04 15:10:40 +00:00
u_int i, echar, rid, sid, esid, exitval, type, exitval_seen;
2010-03-08 11:19:52 +00:00
extern char **environ;
2023-08-10 16:16:53 +00:00
int r, rawmode = 0;
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("entering");
2010-03-08 11:19:52 +00:00
if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
2021-04-23 19:10:38 +00:00
error_f("master alive request failed");
2010-03-08 11:19:52 +00:00
return -1;
}
2021-02-14 21:04:52 +00:00
ssh_signal(SIGPIPE, SIG_IGN);
2008-07-23 09:33:08 +00:00
2021-08-30 19:14:33 +00:00
if (options.stdin_null && stdfd_devnull(1, 0, 0) == -1)
2021-04-23 19:10:38 +00:00
fatal_f("stdfd_devnull failed");
2008-07-23 09:33:08 +00:00
2021-08-30 19:14:33 +00:00
if ((term = lookup_env_in_list("TERM", options.setenv,
options.num_setenv)) == NULL || *term == '\0')
term = getenv("TERM");
2018-08-28 10:47:58 +00:00
echar = 0xffffffff;
if (options.escape_char != SSH_ESCAPECHAR_NONE)
echar = (u_int)options.escape_char;
if ((m = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(m, MUX_C_NEW_SESSION)) != 0 ||
(r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
(r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
(r = sshbuf_put_u32(m, tty_flag)) != 0 ||
(r = sshbuf_put_u32(m, options.forward_x11)) != 0 ||
(r = sshbuf_put_u32(m, options.forward_agent)) != 0 ||
2021-08-30 19:14:33 +00:00
(r = sshbuf_put_u32(m, options.session_type == SESSION_TYPE_SUBSYSTEM)) != 0 ||
2018-08-28 10:47:58 +00:00
(r = sshbuf_put_u32(m, echar)) != 0 ||
2021-08-30 19:14:33 +00:00
(r = sshbuf_put_cstring(m, term == NULL ? "" : term)) != 0 ||
2018-08-28 10:47:58 +00:00
(r = sshbuf_put_stringb(m, command)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "request");
2018-08-28 10:47:58 +00:00
/* Pass environment */
2010-03-08 11:19:52 +00:00
if (options.num_send_env > 0 && environ != NULL) {
for (i = 0; environ[i] != NULL; i++) {
2018-08-28 10:47:58 +00:00
if (!env_permitted(environ[i]))
continue;
if ((r = sshbuf_put_cstring(m, environ[i])) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "request sendenv");
2010-03-08 11:19:52 +00:00
}
2008-07-23 09:33:08 +00:00
}
2018-08-28 10:47:58 +00:00
for (i = 0; i < options.num_setenv; i++) {
if ((r = sshbuf_put_cstring(m, options.setenv[i])) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "request setenv");
2018-08-28 10:47:58 +00:00
}
2008-07-23 09:33:08 +00:00
2018-08-28 10:47:58 +00:00
if (mux_client_write_packet(fd, m) != 0)
2021-04-23 19:10:38 +00:00
fatal_f("write packet: %s", strerror(errno));
2010-03-08 11:19:52 +00:00
/* Send the stdio file descriptors */
if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
mm_send_fd(fd, STDOUT_FILENO) == -1 ||
mm_send_fd(fd, STDERR_FILENO) == -1)
2021-04-23 19:10:38 +00:00
fatal_f("send fds failed");
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("session request sent");
2008-07-23 09:33:08 +00:00
2010-03-08 11:19:52 +00:00
/* Read their reply */
2018-08-28 10:47:58 +00:00
sshbuf_reset(m);
if (mux_client_read_packet(fd, m) != 0) {
2021-04-23 19:10:38 +00:00
error_f("read from master failed: %s", strerror(errno));
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
return -1;
}
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &type)) != 0 ||
(r = sshbuf_get_u32(m, &rid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse");
2018-08-28 10:47:58 +00:00
if (rid != muxclient_request_id)
2021-04-23 19:10:38 +00:00
fatal_f("out of sequence reply: my id %u theirs %u",
muxclient_request_id, rid);
2018-08-28 10:47:58 +00:00
2010-03-08 11:19:52 +00:00
switch (type) {
case MUX_S_SESSION_OPENED:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &sid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse session ID");
debug_f("master session id: %u", sid);
2010-03-08 11:19:52 +00:00
break;
case MUX_S_PERMISSION_DENIED:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
2011-02-17 11:47:40 +00:00
error("Master refused session request: %s", e);
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
return -1;
case MUX_S_FAILURE:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
error_f("session request failed: %s", e);
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
return -1;
default:
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2021-04-23 19:10:38 +00:00
error_f("unexpected response from master 0x%08x", type);
2010-03-08 11:19:52 +00:00
return -1;
}
muxclient_request_id++;
2008-07-23 09:33:08 +00:00
2016-03-10 20:10:25 +00:00
if (pledge("stdio proc tty", NULL) == -1)
2021-04-23 19:10:38 +00:00
fatal_f("pledge(): %s", strerror(errno));
2016-03-10 20:10:25 +00:00
platform_pledge_mux();
2021-02-14 21:04:52 +00:00
ssh_signal(SIGHUP, control_client_sighandler);
ssh_signal(SIGINT, control_client_sighandler);
ssh_signal(SIGTERM, control_client_sighandler);
ssh_signal(SIGWINCH, control_client_sigrelay);
2008-07-23 09:33:08 +00:00
2023-08-10 16:16:53 +00:00
if (options.fork_after_authentication)
daemon(1, 1);
else {
rawmode = tty_flag;
if (tty_flag) {
enter_raw_mode(
options.request_tty == REQUEST_TTY_FORCE);
}
}
2008-07-23 09:33:08 +00:00
/*
* Stick around until the controlee closes the client_fd.
2010-03-08 11:19:52 +00:00
* Before it does, it is expected to write an exit message.
* This process must read the value and wait for the closure of
* the client_fd; if this one closes early, the multiplex master will
* terminate early too (possibly losing data).
2008-07-23 09:33:08 +00:00
*/
2010-03-08 11:19:52 +00:00
for (exitval = 255, exitval_seen = 0;;) {
2018-08-28 10:47:58 +00:00
sshbuf_reset(m);
if (mux_client_read_packet(fd, m) != 0)
2008-07-23 09:33:08 +00:00
break;
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &type)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse type");
2011-09-28 08:14:41 +00:00
switch (type) {
case MUX_S_TTY_ALLOC_FAIL:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &esid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse session ID");
2018-08-28 10:47:58 +00:00
if (esid != sid)
2021-04-23 19:10:38 +00:00
fatal_f("tty alloc fail on unknown session: "
"my id %u theirs %u", sid, esid);
2011-09-28 08:14:41 +00:00
leave_raw_mode(options.request_tty ==
REQUEST_TTY_FORCE);
rawmode = 0;
continue;
case MUX_S_EXIT_MESSAGE:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &esid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse session ID");
2018-08-28 10:47:58 +00:00
if (esid != sid)
2021-04-23 19:10:38 +00:00
fatal_f("exit on unknown session: "
"my id %u theirs %u", sid, esid);
2011-09-28 08:14:41 +00:00
if (exitval_seen)
2021-04-23 19:10:38 +00:00
fatal_f("exitval sent twice");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &exitval)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse exitval");
2011-09-28 08:14:41 +00:00
exitval_seen = 1;
continue;
default:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
fatal_f("master returned error: %s", e);
2008-07-23 09:33:08 +00:00
}
}
2010-03-08 11:19:52 +00:00
close(fd);
2011-09-28 08:14:41 +00:00
if (rawmode)
leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
2010-03-08 11:19:52 +00:00
2008-07-23 09:33:08 +00:00
if (muxclient_terminate) {
2018-05-06 12:24:45 +00:00
debug2("Exiting on signal: %s", strsignal(muxclient_terminate));
2010-03-08 11:19:52 +00:00
exitval = 255;
} else if (!exitval_seen) {
2008-07-23 09:33:08 +00:00
debug2("Control master terminated unexpectedly");
2010-03-08 11:19:52 +00:00
exitval = 255;
2008-07-23 09:33:08 +00:00
} else
2010-03-08 11:19:52 +00:00
debug2("Received exit status from master %d", exitval);
2008-07-23 09:33:08 +00:00
2022-02-23 18:16:45 +00:00
if (tty_flag && options.log_level >= SYSLOG_LEVEL_INFO)
2008-07-23 09:33:08 +00:00
fprintf(stderr, "Shared connection to %s closed.\r\n", host);
2010-03-08 11:19:52 +00:00
exit(exitval);
}
2017-01-31 12:33:47 +00:00
static int
mux_client_proxy(int fd)
{
2018-08-28 10:47:58 +00:00
struct sshbuf *m;
2017-01-31 12:33:47 +00:00
char *e;
u_int type, rid;
2018-08-28 10:47:58 +00:00
int r;
if ((m = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(m, MUX_C_PROXY)) != 0 ||
(r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "request");
2018-08-28 10:47:58 +00:00
if (mux_client_write_packet(fd, m) != 0)
2021-04-23 19:10:38 +00:00
fatal_f("write packet: %s", strerror(errno));
2017-01-31 12:33:47 +00:00
2018-08-28 10:47:58 +00:00
sshbuf_reset(m);
2017-01-31 12:33:47 +00:00
/* Read their reply */
2018-08-28 10:47:58 +00:00
if (mux_client_read_packet(fd, m) != 0) {
sshbuf_free(m);
2017-01-31 12:33:47 +00:00
return 0;
}
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &type)) != 0 ||
(r = sshbuf_get_u32(m, &rid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse");
2018-08-28 10:47:58 +00:00
if (rid != muxclient_request_id)
2021-04-23 19:10:38 +00:00
fatal_f("out of sequence reply: my id %u theirs %u",
muxclient_request_id, rid);
2017-01-31 12:33:47 +00:00
if (type != MUX_S_PROXY) {
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
fatal_f("master returned error: %s", e);
2017-01-31 12:33:47 +00:00
}
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2017-01-31 12:33:47 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("done");
2017-01-31 12:33:47 +00:00
muxclient_request_id++;
return 0;
}
2010-03-08 11:19:52 +00:00
static int
mux_client_request_stdio_fwd(int fd)
{
2018-08-28 10:47:58 +00:00
struct sshbuf *m;
2010-03-08 11:19:52 +00:00
char *e;
u_int type, rid, sid;
2021-04-23 19:10:38 +00:00
int r;
2010-03-08 11:19:52 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("entering");
2010-03-08 11:19:52 +00:00
if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
2021-04-23 19:10:38 +00:00
error_f("master alive request failed");
2010-03-08 11:19:52 +00:00
return -1;
}
2021-02-14 21:04:52 +00:00
ssh_signal(SIGPIPE, SIG_IGN);
2010-03-08 11:19:52 +00:00
2021-08-30 19:14:33 +00:00
if (options.stdin_null && stdfd_devnull(1, 0, 0) == -1)
2021-04-23 19:10:38 +00:00
fatal_f("stdfd_devnull failed");
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if ((m = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(m, MUX_C_NEW_STDIO_FWD)) != 0 ||
(r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
(r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
(r = sshbuf_put_cstring(m, options.stdio_forward_host)) != 0 ||
(r = sshbuf_put_u32(m, options.stdio_forward_port)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "request");
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if (mux_client_write_packet(fd, m) != 0)
2021-04-23 19:10:38 +00:00
fatal_f("write packet: %s", strerror(errno));
2010-03-08 11:19:52 +00:00
/* Send the stdio file descriptors */
if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
mm_send_fd(fd, STDOUT_FILENO) == -1)
2021-04-23 19:10:38 +00:00
fatal_f("send fds failed");
2010-03-08 11:19:52 +00:00
2016-03-10 20:10:25 +00:00
if (pledge("stdio proc tty", NULL) == -1)
2021-04-23 19:10:38 +00:00
fatal_f("pledge(): %s", strerror(errno));
2016-03-10 20:10:25 +00:00
platform_pledge_mux();
2021-04-23 19:10:38 +00:00
debug3_f("stdio forward request sent");
2010-03-08 11:19:52 +00:00
/* Read their reply */
2018-08-28 10:47:58 +00:00
sshbuf_reset(m);
2010-03-08 11:19:52 +00:00
2018-08-28 10:47:58 +00:00
if (mux_client_read_packet(fd, m) != 0) {
2021-04-23 19:10:38 +00:00
error_f("read from master failed: %s", strerror(errno));
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2010-03-08 11:19:52 +00:00
return -1;
}
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &type)) != 0 ||
(r = sshbuf_get_u32(m, &rid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse");
2018-08-28 10:47:58 +00:00
if (rid != muxclient_request_id)
2021-04-23 19:10:38 +00:00
fatal_f("out of sequence reply: my id %u theirs %u",
muxclient_request_id, rid);
2010-03-08 11:19:52 +00:00
switch (type) {
case MUX_S_SESSION_OPENED:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &sid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse session ID");
debug_f("master session id: %u", sid);
2010-03-08 11:19:52 +00:00
break;
case MUX_S_PERMISSION_DENIED:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2011-02-17 11:47:40 +00:00
fatal("Master refused stdio forwarding request: %s", e);
2010-03-08 11:19:52 +00:00
case MUX_S_FAILURE:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2015-01-05 16:09:55 +00:00
fatal("Stdio forwarding request failed: %s", e);
2010-03-08 11:19:52 +00:00
default:
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2021-04-23 19:10:38 +00:00
error_f("unexpected response from master 0x%08x", type);
2010-03-08 11:19:52 +00:00
return -1;
}
muxclient_request_id++;
2021-02-14 21:04:52 +00:00
ssh_signal(SIGHUP, control_client_sighandler);
ssh_signal(SIGINT, control_client_sighandler);
ssh_signal(SIGTERM, control_client_sighandler);
ssh_signal(SIGWINCH, control_client_sigrelay);
2010-03-08 11:19:52 +00:00
/*
* Stick around until the controlee closes the client_fd.
*/
2018-08-28 10:47:58 +00:00
sshbuf_reset(m);
if (mux_client_read_packet(fd, m) != 0) {
2010-03-08 11:19:52 +00:00
if (errno == EPIPE ||
(errno == EINTR && muxclient_terminate != 0))
return 0;
2021-04-23 19:10:38 +00:00
fatal_f("mux_client_read_packet: %s", strerror(errno));
2010-03-08 11:19:52 +00:00
}
2021-04-23 19:10:38 +00:00
fatal_f("master returned unexpected message %u", type);
2010-03-08 11:19:52 +00:00
}
2011-09-28 08:14:41 +00:00
static void
mux_client_request_stop_listening(int fd)
{
2018-08-28 10:47:58 +00:00
struct sshbuf *m;
2011-09-28 08:14:41 +00:00
char *e;
u_int type, rid;
2018-08-28 10:47:58 +00:00
int r;
2011-09-28 08:14:41 +00:00
2021-04-23 19:10:38 +00:00
debug3_f("entering");
2011-09-28 08:14:41 +00:00
2018-08-28 10:47:58 +00:00
if ((m = sshbuf_new()) == NULL)
2021-04-23 19:10:38 +00:00
fatal_f("sshbuf_new");
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_put_u32(m, MUX_C_STOP_LISTENING)) != 0 ||
(r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "request");
2011-09-28 08:14:41 +00:00
2018-08-28 10:47:58 +00:00
if (mux_client_write_packet(fd, m) != 0)
2021-04-23 19:10:38 +00:00
fatal_f("write packet: %s", strerror(errno));
2011-09-28 08:14:41 +00:00
2018-08-28 10:47:58 +00:00
sshbuf_reset(m);
2011-09-28 08:14:41 +00:00
/* Read their reply */
2018-08-28 10:47:58 +00:00
if (mux_client_read_packet(fd, m) != 0)
2021-04-23 19:10:38 +00:00
fatal_f("read from master failed: %s", strerror(errno));
2011-09-28 08:14:41 +00:00
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_u32(m, &type)) != 0 ||
(r = sshbuf_get_u32(m, &rid)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse");
2018-08-28 10:47:58 +00:00
if (rid != muxclient_request_id)
2021-04-23 19:10:38 +00:00
fatal_f("out of sequence reply: my id %u theirs %u",
muxclient_request_id, rid);
2018-08-28 10:47:58 +00:00
2011-09-28 08:14:41 +00:00
switch (type) {
case MUX_S_OK:
break;
case MUX_S_PERMISSION_DENIED:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
2011-09-28 08:14:41 +00:00
fatal("Master refused stop listening request: %s", e);
case MUX_S_FAILURE:
2018-08-28 10:47:58 +00:00
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
2021-04-23 19:10:38 +00:00
fatal_fr(r, "parse error message");
fatal_f("stop listening request failed: %s", e);
2011-09-28 08:14:41 +00:00
default:
2021-04-23 19:10:38 +00:00
fatal_f("unexpected response from master 0x%08x", type);
2011-09-28 08:14:41 +00:00
}
2018-08-28 10:47:58 +00:00
sshbuf_free(m);
2011-09-28 08:14:41 +00:00
muxclient_request_id++;
}
2010-03-08 11:19:52 +00:00
/* Multiplex client main loop. */
2017-01-31 12:33:47 +00:00
int
2010-03-08 11:19:52 +00:00
muxclient(const char *path)
{
struct sockaddr_un addr;
2023-08-10 16:16:53 +00:00
int sock, timeout = options.connection_timeout, timeout_ms = -1;
2010-03-08 11:19:52 +00:00
u_int pid;
if (muxclient_command == 0) {
2017-01-31 12:29:48 +00:00
if (options.stdio_forward_host != NULL)
2010-03-08 11:19:52 +00:00
muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
else
muxclient_command = SSHMUX_COMMAND_OPEN;
}
switch (options.control_master) {
case SSHCTL_MASTER_AUTO:
case SSHCTL_MASTER_AUTO_ASK:
2023-12-18 15:59:40 +00:00
debug("auto-mux: Trying existing master at '%s'", path);
2010-03-08 11:19:52 +00:00
/* FALLTHROUGH */
case SSHCTL_MASTER_NO:
break;
default:
2017-01-31 12:33:47 +00:00
return -1;
2010-03-08 11:19:52 +00:00
}
memset(&addr, '\0', sizeof(addr));
addr.sun_family = AF_UNIX;
if (strlcpy(addr.sun_path, path,
sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
2017-01-31 12:33:47 +00:00
fatal("ControlPath too long ('%s' >= %u bytes)", path,
2021-04-23 19:13:32 +00:00
(unsigned int)sizeof(addr.sun_path));
2010-03-08 11:19:52 +00:00
2021-02-14 21:00:25 +00:00
if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
2021-04-23 19:10:38 +00:00
fatal_f("socket(): %s", strerror(errno));
2010-03-08 11:19:52 +00:00
2017-08-03 10:10:20 +00:00
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
2010-03-08 11:19:52 +00:00
switch (muxclient_command) {
case SSHMUX_COMMAND_OPEN:
case SSHMUX_COMMAND_STDIO_FWD:
break;
default:
fatal("Control socket connect(%.100s): %s", path,
strerror(errno));
}
2011-02-17 11:47:40 +00:00
if (errno == ECONNREFUSED &&
options.control_master != SSHCTL_MASTER_NO) {
debug("Stale control socket %.100s, unlinking", path);
unlink(path);
} else if (errno == ENOENT) {
2010-03-08 11:19:52 +00:00
debug("Control socket \"%.100s\" does not exist", path);
2011-02-17 11:47:40 +00:00
} else {
2010-03-08 11:19:52 +00:00
error("Control socket connect(%.100s): %s", path,
strerror(errno));
}
close(sock);
2017-01-31 12:33:47 +00:00
return -1;
2010-03-08 11:19:52 +00:00
}
set_nonblock(sock);
2023-08-10 16:16:53 +00:00
/* Timeout on initial connection only. */
if (timeout > 0 && timeout < INT_MAX / 1000)
timeout_ms = timeout * 1000;
if (mux_client_hello_exchange(sock, timeout_ms) != 0) {
2021-04-23 19:10:38 +00:00
error_f("master hello exchange failed");
2010-03-08 11:19:52 +00:00
close(sock);
2017-01-31 12:33:47 +00:00
return -1;
2010-03-08 11:19:52 +00:00
}
switch (muxclient_command) {
case SSHMUX_COMMAND_ALIVE_CHECK:
if ((pid = mux_client_request_alive(sock)) == 0)
2021-04-23 19:10:38 +00:00
fatal_f("master alive check failed");
2016-03-10 20:10:25 +00:00
fprintf(stderr, "Master running (pid=%u)\r\n", pid);
2010-03-08 11:19:52 +00:00
exit(0);
case SSHMUX_COMMAND_TERMINATE:
mux_client_request_terminate(sock);
2017-01-31 12:33:47 +00:00
if (options.log_level != SYSLOG_LEVEL_QUIET)
fprintf(stderr, "Exit request sent.\r\n");
2010-03-08 11:19:52 +00:00
exit(0);
2010-11-08 10:45:44 +00:00
case SSHMUX_COMMAND_FORWARD:
2012-08-29 15:46:01 +00:00
if (mux_client_forwards(sock, 0) != 0)
2021-04-23 19:10:38 +00:00
fatal_f("master forward request failed");
2010-11-08 10:45:44 +00:00
exit(0);
2010-03-08 11:19:52 +00:00
case SSHMUX_COMMAND_OPEN:
2012-08-29 15:46:01 +00:00
if (mux_client_forwards(sock, 0) != 0) {
2021-04-23 19:10:38 +00:00
error_f("master forward request failed");
2017-01-31 12:33:47 +00:00
return -1;
2010-03-08 11:19:52 +00:00
}
mux_client_request_session(sock);
2017-01-31 12:33:47 +00:00
return -1;
2010-03-08 11:19:52 +00:00
case SSHMUX_COMMAND_STDIO_FWD:
mux_client_request_stdio_fwd(sock);
exit(0);
2011-09-28 08:14:41 +00:00
case SSHMUX_COMMAND_STOP:
mux_client_request_stop_listening(sock);
2017-01-31 12:33:47 +00:00
if (options.log_level != SYSLOG_LEVEL_QUIET)
fprintf(stderr, "Stop listening request sent.\r\n");
2011-09-28 08:14:41 +00:00
exit(0);
2012-08-29 15:46:01 +00:00
case SSHMUX_COMMAND_CANCEL_FWD:
if (mux_client_forwards(sock, 1) != 0)
2021-04-23 19:10:38 +00:00
error_f("master cancel forward request failed");
2012-08-29 15:46:01 +00:00
exit(0);
2017-01-31 12:33:47 +00:00
case SSHMUX_COMMAND_PROXY:
mux_client_proxy(sock);
return (sock);
2010-03-08 11:19:52 +00:00
default:
fatal("unrecognised muxclient_command %d", muxclient_command);
}
2008-07-23 09:33:08 +00:00
}