version 1.209.2.1, 2004/08/19 04:13:27 |
version 1.209.2.2, 2005/03/10 17:15:05 |
|
|
#include "match.h" |
#include "match.h" |
#include "msg.h" |
#include "msg.h" |
#include "monitor_fdpass.h" |
#include "monitor_fdpass.h" |
|
#include "uidswap.h" |
|
|
#ifdef SMARTCARD |
#ifdef SMARTCARD |
#include "scard.h" |
#include "scard.h" |
|
|
/* fd to control socket */ |
/* fd to control socket */ |
int control_fd = -1; |
int control_fd = -1; |
|
|
|
/* Multiplexing control command */ |
|
static u_int mux_command = SSHMUX_COMMAND_OPEN; |
|
|
/* Only used in control client mode */ |
/* Only used in control client mode */ |
volatile sig_atomic_t control_client_terminate = 0; |
volatile sig_atomic_t control_client_terminate = 0; |
u_int control_server_pid = 0; |
u_int control_server_pid = 0; |
|
|
usage(void) |
usage(void) |
{ |
{ |
fprintf(stderr, |
fprintf(stderr, |
"usage: ssh [-1246AaCfghkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n" |
"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n" |
" [-D port] [-e escape_char] [-F configfile] [-i identity_file]\n" |
" [-D port] [-e escape_char] [-F configfile]\n" |
" [-L port:host:hostport] [-l login_name] [-m mac_spec] [-o option]\n" |
" [-i identity_file] [-L [bind_address:]port:host:hostport]\n" |
" [-p port] [-R port:host:hostport] [-S ctl] [user@]hostname [command]\n" |
" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n" |
|
" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n" |
|
" [user@]hostname [command]\n" |
); |
); |
exit(1); |
exit(1); |
} |
} |
|
|
main(int ac, char **av) |
main(int ac, char **av) |
{ |
{ |
int i, opt, exit_status; |
int i, opt, exit_status; |
u_short fwd_port, fwd_host_port; |
|
char sfwd_port[6], sfwd_host_port[6]; |
|
char *p, *cp, *line, buf[256]; |
char *p, *cp, *line, buf[256]; |
struct stat st; |
struct stat st; |
struct passwd *pw; |
struct passwd *pw; |
int dummy; |
int dummy; |
extern int optind, optreset; |
extern int optind, optreset; |
extern char *optarg; |
extern char *optarg; |
|
Forward fwd; |
|
|
/* |
/* |
* Save the original real uid. It will be needed later (uid-swapping |
* Save the original real uid. It will be needed later (uid-swapping |
|
|
|
|
again: |
again: |
while ((opt = getopt(ac, av, |
while ((opt = getopt(ac, av, |
"1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNPR:S:TVXY")) != -1) { |
"1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNO:PR:S:TVXY")) != -1) { |
switch (opt) { |
switch (opt) { |
case '1': |
case '1': |
options.protocol = SSH_PROTO_1; |
options.protocol = SSH_PROTO_1; |
|
|
case 'g': |
case 'g': |
options.gateway_ports = 1; |
options.gateway_ports = 1; |
break; |
break; |
|
case 'O': |
|
if (strcmp(optarg, "check") == 0) |
|
mux_command = SSHMUX_COMMAND_ALIVE_CHECK; |
|
else if (strcmp(optarg, "exit") == 0) |
|
mux_command = SSHMUX_COMMAND_TERMINATE; |
|
else |
|
fatal("Invalid multiplex command."); |
|
break; |
case 'P': /* deprecated */ |
case 'P': /* deprecated */ |
options.use_privileged_port = 0; |
options.use_privileged_port = 0; |
break; |
break; |
|
|
case 'i': |
case 'i': |
if (stat(optarg, &st) < 0) { |
if (stat(optarg, &st) < 0) { |
fprintf(stderr, "Warning: Identity file %s " |
fprintf(stderr, "Warning: Identity file %s " |
"does not exist.\n", optarg); |
"not accessible: %s.\n", optarg, |
|
strerror(errno)); |
break; |
break; |
} |
} |
if (options.num_identity_files >= |
if (options.num_identity_files >= |
|
|
options.log_level++; |
options.log_level++; |
break; |
break; |
} |
} |
/* fallthrough */ |
/* FALLTHROUGH */ |
case 'V': |
case 'V': |
fprintf(stderr, "%s, %s\n", |
fprintf(stderr, "%s, %s\n", |
SSH_VERSION, SSLeay_version(SSLEAY_VERSION)); |
SSH_VERSION, SSLeay_version(SSLEAY_VERSION)); |
|
|
break; |
break; |
|
|
case 'L': |
case 'L': |
case 'R': |
if (parse_forward(&fwd, optarg)) |
if (sscanf(optarg, "%5[0-9]:%255[^:]:%5[0-9]", |
add_local_forward(&options, &fwd); |
sfwd_port, buf, sfwd_host_port) != 3 && |
else { |
sscanf(optarg, "%5[0-9]/%255[^/]/%5[0-9]", |
|
sfwd_port, buf, sfwd_host_port) != 3) { |
|
fprintf(stderr, |
fprintf(stderr, |
"Bad forwarding specification '%s'\n", |
"Bad local forwarding specification '%s'\n", |
optarg); |
optarg); |
usage(); |
exit(1); |
/* NOTREACHED */ |
|
} |
} |
if ((fwd_port = a2port(sfwd_port)) == 0 || |
break; |
(fwd_host_port = a2port(sfwd_host_port)) == 0) { |
|
|
case 'R': |
|
if (parse_forward(&fwd, optarg)) { |
|
add_remote_forward(&options, &fwd); |
|
} else { |
fprintf(stderr, |
fprintf(stderr, |
"Bad forwarding port(s) '%s'\n", optarg); |
"Bad remote forwarding specification " |
|
"'%s'\n", optarg); |
exit(1); |
exit(1); |
} |
} |
if (opt == 'L') |
|
add_local_forward(&options, fwd_port, buf, |
|
fwd_host_port); |
|
else if (opt == 'R') |
|
add_remote_forward(&options, fwd_port, buf, |
|
fwd_host_port); |
|
break; |
break; |
|
|
case 'D': |
case 'D': |
fwd_port = a2port(optarg); |
cp = p = xstrdup(optarg); |
if (fwd_port == 0) { |
memset(&fwd, '\0', sizeof(fwd)); |
|
fwd.connect_host = "socks"; |
|
if ((fwd.listen_host = hpdelim(&cp)) == NULL) { |
|
fprintf(stderr, "Bad dynamic forwarding " |
|
"specification '%.100s'\n", optarg); |
|
exit(1); |
|
} |
|
if (cp != NULL) { |
|
fwd.listen_port = a2port(cp); |
|
fwd.listen_host = cleanhostname(fwd.listen_host); |
|
} else { |
|
fwd.listen_port = a2port(fwd.listen_host); |
|
fwd.listen_host = ""; |
|
} |
|
|
|
if (fwd.listen_port == 0) { |
fprintf(stderr, "Bad dynamic port '%s'\n", |
fprintf(stderr, "Bad dynamic port '%s'\n", |
optarg); |
optarg); |
exit(1); |
exit(1); |
} |
} |
add_local_forward(&options, fwd_port, "socks", 0); |
add_local_forward(&options, &fwd); |
|
xfree(p); |
break; |
break; |
|
|
case 'C': |
case 'C': |
|
|
* user's home directory if it happens to be on a NFS volume where |
* user's home directory if it happens to be on a NFS volume where |
* root is mapped to nobody. |
* root is mapped to nobody. |
*/ |
*/ |
seteuid(original_real_uid); |
if (original_effective_uid == 0) { |
setuid(original_real_uid); |
PRIV_START; |
|
permanently_set_uid(pw); |
|
} |
|
|
/* |
/* |
* Now that we are back to our own permissions, create ~/.ssh |
* Now that we are back to our own permissions, create ~/.ssh |
|
|
|
|
/* Initiate local TCP/IP port forwardings. */ |
/* Initiate local TCP/IP port forwardings. */ |
for (i = 0; i < options.num_local_forwards; i++) { |
for (i = 0; i < options.num_local_forwards; i++) { |
debug("Connections to local port %d forwarded to remote address %.200s:%d", |
debug("Local connections to %.200s:%d forwarded to remote " |
options.local_forwards[i].port, |
"address %.200s:%d", |
options.local_forwards[i].host, |
(options.local_forwards[i].listen_host == NULL) ? |
options.local_forwards[i].host_port); |
(options.gateway_ports ? "*" : "LOCALHOST") : |
|
options.local_forwards[i].listen_host, |
|
options.local_forwards[i].listen_port, |
|
options.local_forwards[i].connect_host, |
|
options.local_forwards[i].connect_port); |
success += channel_setup_local_fwd_listener( |
success += channel_setup_local_fwd_listener( |
options.local_forwards[i].port, |
options.local_forwards[i].listen_host, |
options.local_forwards[i].host, |
options.local_forwards[i].listen_port, |
options.local_forwards[i].host_port, |
options.local_forwards[i].connect_host, |
|
options.local_forwards[i].connect_port, |
options.gateway_ports); |
options.gateway_ports); |
} |
} |
if (i > 0 && success == 0) |
if (i > 0 && success == 0) |
|
|
|
|
/* Initiate remote TCP/IP port forwardings. */ |
/* Initiate remote TCP/IP port forwardings. */ |
for (i = 0; i < options.num_remote_forwards; i++) { |
for (i = 0; i < options.num_remote_forwards; i++) { |
debug("Connections to remote port %d forwarded to local address %.200s:%d", |
debug("Remote connections from %.200s:%d forwarded to " |
options.remote_forwards[i].port, |
"local address %.200s:%d", |
options.remote_forwards[i].host, |
options.remote_forwards[i].listen_host, |
options.remote_forwards[i].host_port); |
options.remote_forwards[i].listen_port, |
|
options.remote_forwards[i].connect_host, |
|
options.remote_forwards[i].connect_port); |
channel_request_remote_forwarding( |
channel_request_remote_forwarding( |
options.remote_forwards[i].port, |
options.remote_forwards[i].listen_host, |
options.remote_forwards[i].host, |
options.remote_forwards[i].listen_port, |
options.remote_forwards[i].host_port); |
options.remote_forwards[i].connect_host, |
|
options.remote_forwards[i].connect_port); |
} |
} |
} |
} |
|
|
|
|
return; |
return; |
debug("remote forward %s for: listen %d, connect %s:%d", |
debug("remote forward %s for: listen %d, connect %s:%d", |
type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", |
type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", |
options.remote_forwards[i].port, |
options.remote_forwards[i].listen_port, |
options.remote_forwards[i].host, |
options.remote_forwards[i].connect_host, |
options.remote_forwards[i].host_port); |
options.remote_forwards[i].connect_port); |
if (type == SSH2_MSG_REQUEST_FAILURE) |
if (type == SSH2_MSG_REQUEST_FAILURE) |
logit("Warning: remote port forwarding failed for listen port %d", |
logit("Warning: remote port forwarding failed for listen " |
options.remote_forwards[i].port); |
"port %d", options.remote_forwards[i].listen_port); |
} |
} |
|
|
static void |
static void |
|
|
control_client(const char *path) |
control_client(const char *path) |
{ |
{ |
struct sockaddr_un addr; |
struct sockaddr_un addr; |
int i, r, sock, exitval, num_env; |
int i, r, fd, sock, exitval, num_env; |
Buffer m; |
Buffer m; |
char *cp; |
char *term; |
extern char **environ; |
extern char **environ; |
|
u_int flags; |
|
|
|
if (stdin_null_flag) { |
|
if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1) |
|
fatal("open(/dev/null): %s", strerror(errno)); |
|
if (dup2(fd, STDIN_FILENO) == -1) |
|
fatal("dup2: %s", strerror(errno)); |
|
if (fd > STDERR_FILENO) |
|
close(fd); |
|
} |
|
|
memset(&addr, '\0', sizeof(addr)); |
memset(&addr, '\0', sizeof(addr)); |
addr.sun_family = AF_UNIX; |
addr.sun_family = AF_UNIX; |
addr.sun_len = offsetof(struct sockaddr_un, sun_path) + |
addr.sun_len = offsetof(struct sockaddr_un, sun_path) + |
|
|
if (connect(sock, (struct sockaddr*)&addr, addr.sun_len) == -1) |
if (connect(sock, (struct sockaddr*)&addr, addr.sun_len) == -1) |
fatal("Couldn't connect to %s: %s", path, strerror(errno)); |
fatal("Couldn't connect to %s: %s", path, strerror(errno)); |
|
|
if ((cp = getenv("TERM")) == NULL) |
if ((term = getenv("TERM")) == NULL) |
cp = ""; |
term = ""; |
|
|
|
flags = 0; |
|
if (tty_flag) |
|
flags |= SSHMUX_FLAG_TTY; |
|
if (subsystem_flag) |
|
flags |= SSHMUX_FLAG_SUBSYS; |
|
|
buffer_init(&m); |
buffer_init(&m); |
|
|
/* Get PID of controlee */ |
/* Send our command to server */ |
|
buffer_put_int(&m, mux_command); |
|
buffer_put_int(&m, flags); |
|
if (ssh_msg_send(sock, /* version */1, &m) == -1) |
|
fatal("%s: msg_send", __func__); |
|
buffer_clear(&m); |
|
|
|
/* Get authorisation status and PID of controlee */ |
if (ssh_msg_recv(sock, &m) == -1) |
if (ssh_msg_recv(sock, &m) == -1) |
fatal("%s: msg_recv", __func__); |
fatal("%s: msg_recv", __func__); |
if (buffer_get_char(&m) != 0) |
if (buffer_get_char(&m) != 1) |
fatal("%s: wrong version", __func__); |
fatal("%s: wrong version", __func__); |
/* Connection allowed? */ |
|
if (buffer_get_int(&m) != 1) |
if (buffer_get_int(&m) != 1) |
fatal("Connection to master denied"); |
fatal("Connection to master denied"); |
control_server_pid = buffer_get_int(&m); |
control_server_pid = buffer_get_int(&m); |
|
|
buffer_clear(&m); |
buffer_clear(&m); |
buffer_put_int(&m, tty_flag); |
|
buffer_put_int(&m, subsystem_flag); |
|
buffer_put_cstring(&m, cp); |
|
|
|
|
switch (mux_command) { |
|
case SSHMUX_COMMAND_ALIVE_CHECK: |
|
fprintf(stderr, "Master running (pid=%d)\r\n", |
|
control_server_pid); |
|
exit(0); |
|
case SSHMUX_COMMAND_TERMINATE: |
|
fprintf(stderr, "Exit request sent.\r\n"); |
|
exit(0); |
|
case SSHMUX_COMMAND_OPEN: |
|
/* continue below */ |
|
break; |
|
default: |
|
fatal("silly mux_command %d", mux_command); |
|
} |
|
|
|
/* SSHMUX_COMMAND_OPEN */ |
|
buffer_put_cstring(&m, term); |
buffer_append(&command, "\0", 1); |
buffer_append(&command, "\0", 1); |
buffer_put_cstring(&m, buffer_ptr(&command)); |
buffer_put_cstring(&m, buffer_ptr(&command)); |
|
|
|
|
} |
} |
} |
} |
|
|
if (ssh_msg_send(sock, /* version */0, &m) == -1) |
if (ssh_msg_send(sock, /* version */1, &m) == -1) |
fatal("%s: msg_send", __func__); |
fatal("%s: msg_send", __func__); |
|
|
mm_send_fd(sock, STDIN_FILENO); |
mm_send_fd(sock, STDIN_FILENO); |
|
|
buffer_clear(&m); |
buffer_clear(&m); |
if (ssh_msg_recv(sock, &m) == -1) |
if (ssh_msg_recv(sock, &m) == -1) |
fatal("%s: msg_recv", __func__); |
fatal("%s: msg_recv", __func__); |
if (buffer_get_char(&m) != 0) |
if (buffer_get_char(&m) != 1) |
fatal("%s: master returned error", __func__); |
fatal("%s: wrong version", __func__); |
buffer_free(&m); |
buffer_free(&m); |
|
|
|
signal(SIGHUP, control_client_sighandler); |
signal(SIGINT, control_client_sighandler); |
signal(SIGINT, control_client_sighandler); |
signal(SIGTERM, control_client_sighandler); |
signal(SIGTERM, control_client_sighandler); |
signal(SIGWINCH, control_client_sigrelay); |
signal(SIGWINCH, control_client_sigrelay); |