2013-07-22 23:06:28 +02:00
|
|
|
#include <event2/dns.h>
|
|
|
|
#include <event2/util.h>
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
2016-06-05 18:54:25 +02:00
|
|
|
#include <netinet/in.h>
|
2013-07-22 23:06:28 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "tmate.h"
|
|
|
|
|
2016-03-27 00:00:16 +01:00
|
|
|
#define TMATE_DNS_RETRY_TIMEOUT 2
|
|
|
|
#define TMATE_RECONNECT_RETRY_TIMEOUT 2
|
2013-07-22 23:06:28 +02:00
|
|
|
|
|
|
|
struct tmate_session tmate_session;
|
|
|
|
|
|
|
|
static void lookup_and_connect(void);
|
|
|
|
|
2015-12-23 11:41:41 +01:00
|
|
|
static void on_dns_retry(__unused evutil_socket_t fd, __unused short what,
|
2019-11-06 01:29:18 +01:00
|
|
|
void *arg)
|
2013-07-22 23:06:28 +02:00
|
|
|
{
|
2019-11-06 01:29:18 +01:00
|
|
|
struct tmate_session *session = arg;
|
|
|
|
|
|
|
|
assert(session->ev_dns_retry);
|
|
|
|
event_free(session->ev_dns_retry);
|
|
|
|
session->ev_dns_retry = NULL;
|
|
|
|
|
2013-07-22 23:06:28 +02:00
|
|
|
lookup_and_connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dns_cb(int errcode, struct evutil_addrinfo *addr, void *ptr)
|
|
|
|
{
|
|
|
|
struct evutil_addrinfo *ai;
|
2013-10-04 23:36:46 +02:00
|
|
|
const char *host = ptr;
|
2013-07-22 23:06:28 +02:00
|
|
|
|
|
|
|
if (errcode) {
|
2019-11-06 01:29:18 +01:00
|
|
|
struct tmate_session *session = &tmate_session;
|
2013-07-22 23:06:28 +02:00
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
if (session->ev_dns_retry)
|
|
|
|
return;
|
2013-07-22 23:06:28 +02:00
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
struct timeval tv = { .tv_sec = TMATE_DNS_RETRY_TIMEOUT, .tv_usec = 0 };
|
2013-07-22 23:06:28 +02:00
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
session->ev_dns_retry = evtimer_new(session->ev_base, on_dns_retry, session);
|
|
|
|
if (!session->ev_dns_retry)
|
|
|
|
tmate_fatal("out of memory");
|
|
|
|
evtimer_add(session->ev_dns_retry, &tv);
|
|
|
|
|
|
|
|
tmate_status_message("%s lookup failure. Retrying in %d seconds (%s)",
|
|
|
|
host, TMATE_DNS_RETRY_TIMEOUT,
|
|
|
|
evutil_gai_strerror(errcode));
|
2013-07-22 23:06:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-04 23:36:46 +02:00
|
|
|
tmate_status_message("Connecting to %s...", host);
|
2013-07-22 23:06:28 +02:00
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
int i, num_clients = 0;
|
|
|
|
for (ai = addr; ai; ai = ai->ai_next)
|
|
|
|
num_clients++;
|
|
|
|
|
|
|
|
struct tmate_ssh_client *ssh_clients[num_clients];
|
|
|
|
|
|
|
|
for (ai = addr, i = 0; ai; ai = ai->ai_next, i++) {
|
2013-07-22 23:06:28 +02:00
|
|
|
char buf[128];
|
|
|
|
const char *ip = NULL;
|
|
|
|
if (ai->ai_family == AF_INET) {
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
|
|
|
|
ip = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, 128);
|
|
|
|
} else if (ai->ai_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr;
|
|
|
|
ip = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, 128);
|
|
|
|
}
|
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
ssh_clients[i] = tmate_ssh_client_alloc(&tmate_session, ip);
|
2013-07-22 23:06:28 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
for (i = 0; i < num_clients; i++)
|
|
|
|
connect_ssh_client(ssh_clients[i]);
|
|
|
|
|
2013-07-22 23:06:28 +02:00
|
|
|
evutil_freeaddrinfo(addr);
|
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
evdns_base_free(tmate_session.ev_dnsbase, 0);
|
|
|
|
tmate_session.ev_dnsbase = NULL;
|
2013-07-22 23:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void lookup_and_connect(void)
|
|
|
|
{
|
|
|
|
struct evutil_addrinfo hints;
|
2013-10-04 23:36:46 +02:00
|
|
|
const char *tmate_server_host;
|
2013-07-22 23:06:28 +02:00
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
assert(!tmate_session.ev_dnsbase);
|
|
|
|
tmate_session.ev_dnsbase = evdns_base_new(tmate_session.ev_base, 1);
|
2015-12-23 11:41:41 +01:00
|
|
|
if (!tmate_session.ev_dnsbase)
|
2013-07-22 23:06:28 +02:00
|
|
|
tmate_fatal("Cannot initialize the DNS lookup service");
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
2016-01-02 15:45:34 +01:00
|
|
|
hints.ai_flags = EVUTIL_AI_ADDRCONFIG;
|
2013-07-22 23:06:28 +02:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
|
2015-12-24 05:19:03 +01:00
|
|
|
tmate_server_host = options_get_string(global_options,
|
2013-10-04 23:36:46 +02:00
|
|
|
"tmate-server-host");
|
2019-11-03 11:11:37 +01:00
|
|
|
tmate_debug("Looking up %s...", tmate_server_host);
|
2015-12-23 11:41:41 +01:00
|
|
|
(void)evdns_getaddrinfo(tmate_session.ev_dnsbase, tmate_server_host, NULL,
|
|
|
|
&hints, dns_cb, (void *)tmate_server_host);
|
2013-07-22 23:06:28 +02:00
|
|
|
}
|
|
|
|
|
2016-01-01 22:44:01 +01:00
|
|
|
static void __tmate_session_init(struct tmate_session *session,
|
|
|
|
struct event_base *base)
|
2013-11-07 01:07:47 +01:00
|
|
|
{
|
2016-01-01 22:44:01 +01:00
|
|
|
memset(session, 0, sizeof(*session));
|
2013-11-07 01:07:47 +01:00
|
|
|
|
2016-01-01 22:44:01 +01:00
|
|
|
session->ev_base = base;
|
2015-12-23 11:41:41 +01:00
|
|
|
|
2016-01-01 22:44:01 +01:00
|
|
|
/*
|
|
|
|
* Early initialization of encoder because we need to parse
|
|
|
|
* config files to get the server configs, but while we are parsing
|
|
|
|
* config files, we need to buffer bind commands and all for the
|
|
|
|
* slave.
|
|
|
|
* Decoder is setup later.
|
|
|
|
*/
|
|
|
|
tmate_encoder_init(&session->encoder, NULL, &tmate_session);
|
2013-07-22 23:06:28 +02:00
|
|
|
|
2016-01-01 22:44:01 +01:00
|
|
|
session->min_sx = -1;
|
|
|
|
session->min_sy = -1;
|
2013-07-23 01:45:34 +02:00
|
|
|
|
2016-01-01 22:44:01 +01:00
|
|
|
TAILQ_INIT(&session->clients);
|
|
|
|
}
|
2013-07-23 01:45:34 +02:00
|
|
|
|
2016-01-01 22:44:01 +01:00
|
|
|
void tmate_session_init(struct event_base *base)
|
|
|
|
{
|
|
|
|
__tmate_session_init(&tmate_session, base);
|
2013-07-22 23:06:28 +02:00
|
|
|
tmate_write_header();
|
|
|
|
}
|
2013-07-23 00:05:44 +02:00
|
|
|
|
2019-11-07 15:08:17 +01:00
|
|
|
static void send_authorized_keys(void)
|
2019-11-04 22:43:59 +01:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
path = options_get_string(global_options, "tmate-authorized-keys");
|
|
|
|
if (strlen(path) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
path = xstrdup(path);
|
|
|
|
tmate_info("Using %s for access control", path);
|
|
|
|
|
|
|
|
FILE *f;
|
|
|
|
char *line;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (path[0] == '~' && path[1] == '/') {
|
|
|
|
const char *home = find_home();
|
|
|
|
if (home) {
|
|
|
|
char *new_path;
|
|
|
|
xasprintf(&new_path, "%s%s", home, &path[1]);
|
|
|
|
free(path);
|
|
|
|
path = new_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((f = fopen(path, "r")) == NULL) {
|
|
|
|
cfg_add_cause("%s: %s", path, strerror(errno));
|
|
|
|
free(path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((line = fparseln(f, &len, NULL, NULL, 0)) != NULL) {
|
|
|
|
if (len == 0)
|
|
|
|
continue;
|
|
|
|
tmate_set_val("authorized_keys", line);
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ferror(f))
|
|
|
|
cfg_add_cause("%s: %s", path, strerror(errno));
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
2013-07-23 00:05:44 +02:00
|
|
|
void tmate_session_start(void)
|
|
|
|
{
|
2016-03-27 00:00:16 +01:00
|
|
|
/*
|
|
|
|
* We split init and start because:
|
2013-07-23 00:05:44 +02:00
|
|
|
* - We need to process the tmux config file during the connection as
|
|
|
|
* we are setting up the tmate identity.
|
|
|
|
* - While we are parsing the config file, we need to be able to
|
|
|
|
* serialize it, and so we need a worker encoder.
|
|
|
|
*/
|
2019-11-07 19:31:28 +01:00
|
|
|
if (tmate_foreground) {
|
|
|
|
tmate_set_val("foreground", "true");
|
|
|
|
tmate_info("To connect to the session locally, run: tmate -S %s attach", socket_path);
|
|
|
|
} else {
|
2019-11-06 03:32:21 +01:00
|
|
|
cfg_add_cause("%s", "To see these messages again, run: tmate show-messages");
|
2019-11-06 18:34:18 +01:00
|
|
|
cfg_add_cause("%s", "Press <Enter> to dismiss");
|
2019-11-06 03:32:21 +01:00
|
|
|
cfg_add_cause("%s", "-----------------------------------------------------");
|
|
|
|
}
|
|
|
|
|
2019-11-04 22:43:59 +01:00
|
|
|
send_authorized_keys();
|
2019-09-19 05:35:07 +02:00
|
|
|
tmate_write_ready();
|
2013-07-23 00:05:44 +02:00
|
|
|
lookup_and_connect();
|
|
|
|
}
|
2016-03-27 00:00:16 +01:00
|
|
|
|
|
|
|
static void on_reconnect_retry(__unused evutil_socket_t fd, __unused short what, void *arg)
|
|
|
|
{
|
|
|
|
struct tmate_session *session = arg;
|
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
assert(session->ev_connection_retry);
|
|
|
|
event_free(session->ev_connection_retry);
|
|
|
|
session->ev_connection_retry = NULL;
|
|
|
|
|
2016-03-27 00:00:16 +01:00
|
|
|
if (session->last_server_ip) {
|
|
|
|
/*
|
|
|
|
* We have a previous server ip. Let's try that again first,
|
|
|
|
* but then connect to any server if it fails again.
|
|
|
|
*/
|
2019-11-06 01:29:18 +01:00
|
|
|
struct tmate_ssh_client *c = tmate_ssh_client_alloc(session,
|
|
|
|
session->last_server_ip);
|
|
|
|
connect_ssh_client(c);
|
2016-03-27 00:00:16 +01:00
|
|
|
free(session->last_server_ip);
|
|
|
|
session->last_server_ip = NULL;
|
|
|
|
} else {
|
|
|
|
lookup_and_connect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 05:29:49 +02:00
|
|
|
void tmate_reconnect_session(struct tmate_session *session, const char *message)
|
2016-03-27 00:00:16 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We no longer have an SSH connection. Time to reconnect.
|
|
|
|
* We'll reuse some of the session information if we can,
|
|
|
|
* and we'll try to reconnect to the same server if possible,
|
|
|
|
* to avoid an SSH connection string change.
|
|
|
|
*/
|
|
|
|
struct timeval tv = { .tv_sec = TMATE_RECONNECT_RETRY_TIMEOUT, .tv_usec = 0 };
|
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
if (session->ev_connection_retry)
|
|
|
|
return;
|
|
|
|
|
|
|
|
session->ev_connection_retry = evtimer_new(session->ev_base, on_reconnect_retry, session);
|
|
|
|
if (!session->ev_connection_retry)
|
|
|
|
tmate_fatal("out of memory");
|
|
|
|
evtimer_add(session->ev_connection_retry, &tv);
|
2016-03-27 00:00:16 +01:00
|
|
|
|
2019-11-06 01:29:18 +01:00
|
|
|
if (message && !tmate_foreground)
|
2016-03-29 05:29:49 +02:00
|
|
|
tmate_status_message("Reconnecting... (%s)", message);
|
|
|
|
else
|
|
|
|
tmate_status_message("Reconnecting...");
|
2016-03-27 00:00:16 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This says that we'll need to send a snapshot of the current state.
|
|
|
|
*/
|
|
|
|
session->reconnected = true;
|
|
|
|
}
|