From 6c41248dadaa900fce1392cf7b647c2f38292aa4 Mon Sep 17 00:00:00 2001 From: Nicolas Viennot Date: Thu, 7 Nov 2019 08:17:51 -0500 Subject: [PATCH] Be more persmissive for session tokens (useful for named sessions) --- tmate-auth-keys.c | 2 +- tmate-main.c | 10 +++++++++- tmate-ssh-client-pty.c | 16 ++++++---------- tmate.h | 2 +- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/tmate-auth-keys.c b/tmate-auth-keys.c index 4c9b4e91..5f81dde2 100644 --- a/tmate-auth-keys.c +++ b/tmate-auth-keys.c @@ -167,7 +167,7 @@ bool would_tmate_session_allow_auth(const char *token, const char *pubkey) int sock_fd = -1; int ret = true; - if (tmate_validated_session_token(token) < 0) + if (tmate_validate_session_token(token) < 0) goto out; char *sock_path = get_socket_path(token); diff --git a/tmate-main.c b/tmate-main.c index c19e98e7..1a25846f 100644 --- a/tmate-main.c +++ b/tmate-main.c @@ -173,10 +173,18 @@ int main(int argc, char **argv, char **envp) return 0; } -char *get_socket_path(const char *token) +char *get_socket_path(const char *_token) { char *path; + char *token = xstrdup(_token); + + for (char *c = token; *c; c++) { + if (*c == '/' || *c == '.') + *c = '_'; + } + xasprintf(&path, TMATE_WORKDIR "/sessions/%s", token); + free(token); return path; } diff --git a/tmate-ssh-client-pty.c b/tmate-ssh-client-pty.c index 80df5403..6379104c 100644 --- a/tmate-ssh-client-pty.c +++ b/tmate-ssh-client-pty.c @@ -133,24 +133,20 @@ static void ssh_echo(struct tmate_ssh_client *ssh_client, /* - * Session tokens are filesystem sensitive, - * so we must be very careful with / and . + * Note: get_socket_path() replaces '/' and '.' by '_' to + * avoid wondering around the file system. */ static char valid_digits[] = "abcdefghjklmnopqrstuvwxyz" "ABCDEFGHJKLMNOPQRSTUVWXYZ" - "0123456789-_"; + "0123456789-_/"; -int tmate_validated_session_token(const char *token) +int tmate_validate_session_token(const char *token) { int len; int i; - if (!memcmp("ro-", token, 3)) - token += 3; - len = strlen(token); - - if (len != TMATE_TOKEN_LEN) + if (len <= 2) return -1; for (i = 0; i < len; i++) { @@ -173,7 +169,7 @@ void tmate_spawn_pty_client(struct tmate_session *session) int slave_pty; int ret; - if (tmate_validated_session_token(token) < 0) { + if (tmate_validate_session_token(token) < 0) { ssh_echo(client, BAD_TOKEN_ERROR_STR); tmate_fatal("Invalid token"); } diff --git a/tmate.h b/tmate.h index 65296061..67bf714d 100644 --- a/tmate.h +++ b/tmate.h @@ -142,7 +142,7 @@ extern void tmate_dump_exec_response(struct tmate_session *session, /* tmate-ssh-client-pty.c */ extern void tmate_spawn_pty_client(struct tmate_session *session); -extern int tmate_validated_session_token(const char *token); +extern int tmate_validate_session_token(const char *token); /* tmate-ssh-server.c */