1
0
mirror of https://github.com/tmate-io/tmate-ssh-server.git synced 2020-11-18 19:53:51 -08:00

Connections are now using tokens

This commit is contained in:
Nicolas Viennot 2013-06-11 02:22:44 -04:00
parent 3b4e6bb9cc
commit 4ae8ba3270
5 changed files with 150 additions and 30 deletions

View File

@ -32,6 +32,7 @@
#include <unistd.h> #include <unistd.h>
#include "tmux.h" #include "tmux.h"
#include "tmate.h"
struct imsgbuf client_ibuf; struct imsgbuf client_ibuf;
struct event client_event; struct event client_event;
@ -206,8 +207,8 @@ client_main(int argc, char **argv, int flags)
} }
#ifdef TMATE_SLAVE #ifdef TMATE_SLAVE
cmdflags &= ~CMD_STARTSERVER; fd = tmux_socket_fd;
#endif #else
/* /*
* Check if this could be a nested session, if the command can't nest: * Check if this could be a nested session, if the command can't nest:
@ -227,6 +228,7 @@ client_main(int argc, char **argv, int flags)
fprintf(stderr, "failed to connect to server\n"); fprintf(stderr, "failed to connect to server\n");
return (1); return (1);
} }
#endif
/* Set process title, log and signals now this is the client. */ /* Set process title, log and signals now this is the client. */
#ifdef HAVE_SETPROCTITLE #ifdef HAVE_SETPROCTITLE
@ -293,8 +295,9 @@ client_main(int argc, char **argv, int flags)
/* Print the exit message, if any, and exit. */ /* Print the exit message, if any, and exit. */
if (client_attached) { if (client_attached) {
if (client_exitreason != CLIENT_EXIT_NONE && !login_shell) if (client_exitreason != CLIENT_EXIT_NONE && !login_shell) {
printf("[%s]\n", client_exit_message()); printf("[%s]\r\n", client_exit_message());
}
ppid = getppid(); ppid = getppid();
if (client_exittype == MSG_DETACHKILL && ppid > 1) if (client_exittype == MSG_DETACHKILL && ppid > 1)

View File

@ -97,9 +97,7 @@ server_create_socket(void)
fatal("listen failed"); fatal("listen failed");
setblocking(fd, 0); setblocking(fd, 0);
server_update_socket(); server_update_socket(); return (fd);
return (fd);
} }
/* Fork new server. */ /* Fork new server. */
@ -162,9 +160,10 @@ server_start(int lockfd, char *lockfile)
setproctitle("server (%s)", socket_path); setproctitle("server (%s)", socket_path);
#endif #endif
#ifdef TMATE_SLAVE
server_fd = tmux_socket_fd;
#else
server_fd = server_create_socket(); server_fd = server_create_socket();
#ifndef TMATE_SLAVE
server_client_create(pair[1]); server_client_create(pair[1]);
unlink(lockfile); unlink(lockfile);
@ -397,10 +396,12 @@ server_signal_callback(int sig, unused short events, unused void *data)
server_child_signal(); server_child_signal();
break; break;
case SIGUSR1: case SIGUSR1:
#ifndef TMATE_SLAVE
event_del(&server_ev_accept); event_del(&server_ev_accept);
close(server_fd); close(server_fd);
server_fd = server_create_socket(); server_fd = server_create_socket();
server_add_accept(0); server_add_accept(0);
#endif
break; break;
} }
} }

View File

@ -1,8 +1,16 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <libssh/libssh.h>
#include "tmate.h" #include "tmate.h"
struct tmate_encoder *tmate_encoder;
int tmux_socket_fd;
const char *tmate_session_token;
extern int server_create_socket(void);
extern int client_connect(char *path, int start_server);
static void usage(void) static void usage(void)
{ {
fprintf(stderr, "usage: tmate-slave [-p PORT]\n"); fprintf(stderr, "usage: tmate-slave [-p PORT]\n");
@ -14,8 +22,6 @@ int main(int argc, char **argv)
int port = 22; int port = 22;
char *log_path = NULL; /* stderr */ char *log_path = NULL; /* stderr */
strcpy(socket_path, "/tmp/tmate-slave");
while ((opt = getopt(argc, argv, "p:l:v")) != -1) { while ((opt = getopt(argc, argv, "p:l:v")) != -1) {
switch (opt) { switch (opt) {
case 'p': case 'p':
@ -38,16 +44,62 @@ int main(int argc, char **argv)
return 0; return 0;
} }
struct tmate_encoder *tmate_encoder; static void set_session_token(const char *token)
void tmate_spawn_slave_server(struct tmate_ssh_client *client)
{ {
tmate_session_token = xstrdup(token);
strcpy(socket_path, "/tmp/tmate-slave-");
strcat(socket_path, token);
}
static char tmate_token_digits[] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
#define NUM_DIGITS (sizeof(tmate_token_digits) - 1)
static char *get_random_token(void)
{
int i;
char *token = xmalloc(TMATE_TOKEN_LEN + 1);
ssh_get_random(token, TMATE_TOKEN_LEN, 0);
for (i = 0; i < TMATE_TOKEN_LEN; i++)
token[i] = tmate_token_digits[token[i] % NUM_DIGITS];
token[i] = 0;
return token;
}
static int validate_token(const char *token)
{
int len = strlen(token);
int i;
if (len != TMATE_TOKEN_LEN)
return -1;
for (i = 0; i < len; i++) {
if (!strchr(tmate_token_digits, token[i]))
return -1;
}
return 0;
}
static void tmate_spawn_slave_server(struct tmate_ssh_client *client)
{
char *token;
struct tmate_encoder encoder; struct tmate_encoder encoder;
struct tmate_decoder decoder; struct tmate_decoder decoder;
tmate_debug("Spawn tmux slave server"); token = get_random_token();
set_session_token(token);
free(token);
ev_base = osdep_event_init(); tmate_debug("Spawning tmux slave server %s", tmate_session_token);
tmux_socket_fd = server_create_socket();
if (tmux_socket_fd < 0)
tmate_fatal("Cannot create to the tmux socket");
tmate_encoder_init(&encoder); tmate_encoder_init(&encoder);
tmate_decoder_init(&decoder); tmate_decoder_init(&decoder);
@ -59,22 +111,68 @@ void tmate_spawn_slave_server(struct tmate_ssh_client *client)
/* never reached */ /* never reached */
} }
void tmate_spawn_slave_client(struct tmate_ssh_client *ssh_client) static void ssh_echo(struct tmate_ssh_client *ssh_client,
const char *str)
{
ssh_channel_write(ssh_client->channel, str, strlen(str));
}
#define BAD_TOKEN_ERROR_STR \
" " "\r\n" \
" Dear guest," "\r\n" \
" " "\r\n" \
" There isn't much I can do without a valid session token." "\r\n" \
" Feel free to reach out if you are having issues." "\r\n" \
" " "\r\n" \
" Thanks," "\r\n" \
" Nico" "\r\n" \
" " "\r\n"
#define EXPIRED_TOKEN_ERROR_STR \
" " "\r\n" \
" Dear guest," "\r\n" \
" " "\r\n" \
" The provided session token is invalid, or has expired." "\r\n" \
" Feel free to reach out if you are having issues." "\r\n" \
" " "\r\n" \
" Thanks," "\r\n" \
" Nico" "\r\n" \
" " "\r\n"
static void random_sleep(void)
{
usleep(50000 + (rand() % 50000));
}
static void tmate_spawn_slave_client(struct tmate_ssh_client *ssh_client)
{ {
struct tmate_ssh_client_pty _client; struct tmate_ssh_client_pty _client;
struct tmate_ssh_client_pty *client = &_client; struct tmate_ssh_client_pty *client = &_client;
char *argv[] = {(char *)"attach", NULL};
char *token = ssh_client->username;
int slave_pty; int slave_pty;
int ret; int ret;
char *argv[] = {(char *)"attach", NULL};
if (validate_token(token) < 0) {
ssh_echo(ssh_client, BAD_TOKEN_ERROR_STR);
tmate_fatal("Bad token");
}
set_session_token(token);
tmate_debug("Spawn tmux slave client %s", tmate_session_token);
tmux_socket_fd = client_connect(socket_path, 0);
if (tmux_socket_fd < 0) {
random_sleep(); /* for timing attacks */
ssh_echo(ssh_client, EXPIRED_TOKEN_ERROR_STR);
tmate_fatal("Expired token");
}
client->session = ssh_client->session; client->session = ssh_client->session;
client->channel = ssh_client->channel; client->channel = ssh_client->channel;
client->winsize_pty = ssh_client->winsize_pty; client->winsize_pty = ssh_client->winsize_pty;
tmate_debug("Spawn tmux slave client");
ev_base = osdep_event_init();
if (openpty(&client->pty, &slave_pty, NULL, NULL, NULL) < 0) if (openpty(&client->pty, &slave_pty, NULL, NULL, NULL) < 0)
tmate_fatal("Cannot allocate pty"); tmate_fatal("Cannot allocate pty");
@ -89,3 +187,13 @@ void tmate_spawn_slave_client(struct tmate_ssh_client *ssh_client)
tmate_flush_pty(client); tmate_flush_pty(client);
exit(ret); exit(ret);
} }
void tmate_spawn_slave(struct tmate_ssh_client *client)
{
ev_base = osdep_event_init();
if (client->role == TMATE_ROLE_SERVER)
tmate_spawn_slave_server(client);
else
tmate_spawn_slave_client(client);
}

View File

@ -105,18 +105,19 @@ static int init_client_step(struct tmate_ssh_client *client,
if (ssh_message_type(msg) == SSH_REQUEST_CHANNEL && if (ssh_message_type(msg) == SSH_REQUEST_CHANNEL &&
ssh_message_subtype(msg) == SSH_CHANNEL_REQUEST_SUBSYSTEM && ssh_message_subtype(msg) == SSH_CHANNEL_REQUEST_SUBSYSTEM &&
!strcmp(ssh_message_channel_request_subsystem(msg), "tmate")) { !strcmp(ssh_message_channel_request_subsystem(msg), "tmate")) {
alarm(0); client->role = TMATE_ROLE_SERVER;
ssh_message_channel_request_reply_success(msg);
tmate_spawn_slave_server(client);
/* never reached */
} }
/* shell request (slave client) */ /* shell request (slave client) */
if (ssh_message_type(msg) == SSH_REQUEST_CHANNEL && if (ssh_message_type(msg) == SSH_REQUEST_CHANNEL &&
ssh_message_subtype(msg) == SSH_CHANNEL_REQUEST_SHELL) { ssh_message_subtype(msg) == SSH_CHANNEL_REQUEST_SHELL) {
client->role = TMATE_ROLE_CLIENT;
}
if (client->role) {
alarm(0); alarm(0);
ssh_message_channel_request_reply_success(msg); ssh_message_channel_request_reply_success(msg);
tmate_spawn_slave_client(client); tmate_spawn_slave(client);
/* never reached */ /* never reached */
} }

13
tmate.h
View File

@ -60,10 +60,15 @@ extern void tmate_decoder_commit(struct tmate_decoder *decoder, size_t len);
typedef struct ssh_session_struct* ssh_session; typedef struct ssh_session_struct* ssh_session;
typedef struct ssh_channel_struct* ssh_channel; typedef struct ssh_channel_struct* ssh_channel;
#define TMATE_ROLE_SERVER 1
#define TMATE_ROLE_CLIENT 2
struct tmate_ssh_client { struct tmate_ssh_client {
ssh_session session; ssh_session session;
ssh_channel channel; ssh_channel channel;
int role;
struct tmate_encoder *encoder; struct tmate_encoder *encoder;
struct tmate_decoder *decoder; struct tmate_decoder *decoder;
@ -102,10 +107,12 @@ extern void tmate_ssh_server_main(int port);
/* tmate-slave.c */ /* tmate-slave.c */
extern struct tmate_encoder *tmate_encoder; #define TMATE_TOKEN_LEN 25
extern void tmate_spawn_slave_server(struct tmate_ssh_client *client); extern struct tmate_encoder *tmate_encoder;
extern void tmate_spawn_slave_client(struct tmate_ssh_client *client); extern int tmux_socket_fd;
extern void tmate_spawn_slave(struct tmate_ssh_client *client);
/* tmate-debug.c */ /* tmate-debug.c */
extern void tmate_print_trace(void); extern void tmate_print_trace(void);