mirror of
https://github.com/tmate-io/tmate-ssh-server.git
synced 2020-11-18 19:53:51 -08:00
Cleanup logging
This commit is contained in:
parent
7f26d860b5
commit
b97f8dc7d6
109
log.c
109
log.c
@ -22,7 +22,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <syslog.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@ -31,16 +30,11 @@
|
|||||||
|
|
||||||
FILE *log_file;
|
FILE *log_file;
|
||||||
|
|
||||||
struct logging_settings {
|
|
||||||
const char *program_name;
|
|
||||||
bool use_syslog;
|
|
||||||
int log_level;
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct logging_settings log_settings;
|
|
||||||
|
|
||||||
static void log_event_cb(int, const char *);
|
static void log_event_cb(int, const char *);
|
||||||
static void log_vwrite(int, const char *, va_list);
|
static void log_vwrite(const char *, va_list);
|
||||||
|
|
||||||
|
static int log_level;
|
||||||
|
char *log_prefix;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
log_event_cb(__unused int severity, const char *msg)
|
log_event_cb(__unused int severity, const char *msg)
|
||||||
@ -52,72 +46,69 @@ log_event_cb(__unused int severity, const char *msg)
|
|||||||
void
|
void
|
||||||
log_add_level(void)
|
log_add_level(void)
|
||||||
{
|
{
|
||||||
log_settings.log_level++;
|
log_level++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get log level. */
|
/* Get log level. */
|
||||||
int
|
int
|
||||||
log_get_level(void)
|
log_get_level(void)
|
||||||
{
|
{
|
||||||
return (log_settings.log_level);
|
return log_level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_logging(const char *program_name, bool use_syslog, int log_level)
|
void init_logging(int _log_level)
|
||||||
{
|
{
|
||||||
log_settings.log_level = log_level;
|
log_level = _log_level;
|
||||||
log_settings.use_syslog = use_syslog;
|
log_prefix = xstrdup("");
|
||||||
log_settings.program_name = xstrdup(program_name);
|
|
||||||
|
|
||||||
if (use_syslog) {
|
log_file = fdopen(dup(STDERR_FILENO), "a");
|
||||||
openlog(program_name, LOG_CONS | LOG_PID, LOG_USER);
|
if (!log_file)
|
||||||
setlogmask(LOG_UPTO(log_level));
|
exit(1);
|
||||||
} else {
|
|
||||||
log_file = fdopen(dup(STDERR_FILENO), "a");
|
|
||||||
if (!log_file)
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
event_set_log_callback(log_event_cb);
|
event_set_log_callback(log_event_cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write a log message. */
|
void set_log_prefix(char *_log_prefix)
|
||||||
__attribute__((__format__(__printf__, 2, 0)))
|
|
||||||
static void
|
|
||||||
log_vwrite(int level, const char *msg, va_list ap)
|
|
||||||
{
|
{
|
||||||
char *fmt = NULL;
|
free(log_prefix);
|
||||||
|
log_prefix = xstrdup(_log_prefix);
|
||||||
|
}
|
||||||
|
|
||||||
const char *token = tmate_session->obfuscated_session_token;
|
/* Write a log message. */
|
||||||
|
__attribute__((__format__(__printf__, 1, 0)))
|
||||||
|
static void
|
||||||
|
log_vwrite(const char *msg, va_list ap)
|
||||||
|
{
|
||||||
|
char *fmt, *out;
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
if (log_settings.log_level < level)
|
if (log_file == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (token) {
|
if (vasprintf(&fmt, msg, ap) == -1)
|
||||||
if (asprintf(&fmt, "[%s] %s", token, msg) < 0)
|
exit(1);
|
||||||
exit(1);
|
if (stravis(&out, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1)
|
||||||
msg = fmt;
|
exit(1);
|
||||||
}
|
|
||||||
|
|
||||||
if (log_settings.use_syslog) {
|
if (fprintf(log_file, "%s%s\n", log_prefix, out) == -1)
|
||||||
vsyslog(level, msg, ap);
|
exit(1);
|
||||||
} else {
|
|
||||||
fprintf(log_file, "<%d> ", level);
|
|
||||||
vfprintf(log_file, msg, ap);
|
|
||||||
fprintf(log_file, "\n");
|
|
||||||
fflush(log_file);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
fflush(log_file);
|
||||||
|
|
||||||
|
free(out);
|
||||||
free(fmt);
|
free(fmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Log a debug message. */
|
|
||||||
void
|
void
|
||||||
log_debug(const char *msg, ...)
|
log_emit(int level, const char *msg, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
|
if (log_level < level)
|
||||||
|
return;
|
||||||
|
|
||||||
va_start(ap, msg);
|
va_start(ap, msg);
|
||||||
log_vwrite(LOG_DEBUG, msg, ap);
|
log_vwrite(msg, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,7 +124,7 @@ fatal(const char *msg, ...)
|
|||||||
if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
|
if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
|
||||||
exit(1);
|
exit(1);
|
||||||
msg = fmt;
|
msg = fmt;
|
||||||
log_vwrite(LOG_CRIT, msg, ap);
|
log_vwrite(msg, ap);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,26 +140,6 @@ fatalx(const char *msg, ...)
|
|||||||
if (asprintf(&fmt, "fatal: %s", msg) == -1)
|
if (asprintf(&fmt, "fatal: %s", msg) == -1)
|
||||||
exit(1);
|
exit(1);
|
||||||
msg = fmt;
|
msg = fmt;
|
||||||
log_vwrite(LOG_CRIT, msg, ap);
|
log_vwrite(msg, ap);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((__format__(__printf__, 2, 0)))
|
|
||||||
void tmate_log(int level, const char *msg, ...)
|
|
||||||
{
|
|
||||||
char *fmt;
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
if (log_settings.log_level < level)
|
|
||||||
return;
|
|
||||||
|
|
||||||
va_start(ap, msg);
|
|
||||||
|
|
||||||
if (asprintf(&fmt, "(tmate) %s", msg) < 0)
|
|
||||||
exit(1);
|
|
||||||
msg = fmt;
|
|
||||||
log_vwrite(level, msg, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
free(fmt);
|
|
||||||
}
|
|
||||||
|
@ -1084,7 +1084,7 @@ server_client_dispatch(struct imsg *imsg, void *arg)
|
|||||||
|
|
||||||
if (!(c->flags & CLIENT_TMATE_AUTHENTICATED)) {
|
if (!(c->flags & CLIENT_TMATE_AUTHENTICATED)) {
|
||||||
control_write(c, "Authentication needed");
|
control_write(c, "Authentication needed");
|
||||||
tmate_warn("Dropping unauthenticated client");
|
tmate_info("Dropping unauthenticated client");
|
||||||
c->flags |= CLIENT_EXIT;
|
c->flags |= CLIENT_EXIT;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1105,7 +1105,7 @@ server_client_dispatch(struct imsg *imsg, void *arg)
|
|||||||
|
|
||||||
#ifdef TMATE
|
#ifdef TMATE
|
||||||
if (!(c->flags & CLIENT_IDENTIFIED)) {
|
if (!(c->flags & CLIENT_IDENTIFIED)) {
|
||||||
tmate_warn("dropping unidentified client message: %d", imsg->hdr.type);
|
tmate_info("dropping unidentified client message: %d", imsg->hdr.type);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
2
server.c
2
server.c
@ -247,6 +247,8 @@ server_loop(void)
|
|||||||
if (!TAILQ_EMPTY(&clients))
|
if (!TAILQ_EMPTY(&clients))
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
|
tmate_info("Session terminated");
|
||||||
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,7 +216,7 @@ bool would_tmate_session_allow_auth(const char *token, const char *pubkey)
|
|||||||
recv_msg.hdr.len == sizeof(recv_msg))
|
recv_msg.hdr.len == sizeof(recv_msg))
|
||||||
ret = recv_msg.allow;
|
ret = recv_msg.allow;
|
||||||
|
|
||||||
tmate_info("(preauth) allow=%d", ret);
|
tmate_debug("(preauth) allow=%d", ret);
|
||||||
|
|
||||||
out:
|
out:
|
||||||
if (sock_fd != -1)
|
if (sock_fd != -1)
|
||||||
|
@ -45,8 +45,8 @@ static void tmate_header(struct tmate_session *session,
|
|||||||
session->daemon_encoder.mpac_version = 4;
|
session->daemon_encoder.mpac_version = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmate_notice("Daemon header: client version: %s, protocol version: %d",
|
tmate_info("Daemon header: version=%s, protocol=%d",
|
||||||
session->client_version, session->client_protocol_version);
|
session->client_version, session->client_protocol_version);
|
||||||
|
|
||||||
if (tmate_has_websocket()) {
|
if (tmate_has_websocket()) {
|
||||||
/* If we have a websocket server, it takes care of all the following notificatons */
|
/* If we have a websocket server, it takes care of all the following notificatons */
|
||||||
@ -222,10 +222,10 @@ static void tmate_exec_cmd_str(__unused struct tmate_session *session,
|
|||||||
|
|
||||||
cmd_str = unpack_string(uk);
|
cmd_str = unpack_string(uk);
|
||||||
|
|
||||||
tmate_info("Local cmd: %s", cmd_str);
|
tmate_debug("Local cmd: %s", cmd_str);
|
||||||
|
|
||||||
if (cmd_string_parse(cmd_str, &cmdlist, NULL, 0, &cause) != 0) {
|
if (cmd_string_parse(cmd_str, &cmdlist, NULL, 0, &cause) != 0) {
|
||||||
tmate_info("parse error: %s", cause);
|
tmate_debug("parse error: %s", cause);
|
||||||
free(cause);
|
free(cause);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
@ -257,13 +257,13 @@ static void tmate_exec_cmd(__unused struct tmate_session *session,
|
|||||||
|
|
||||||
cmd = cmd_parse(argc, argv, NULL, 0, &cause);
|
cmd = cmd_parse(argc, argv, NULL, 0, &cause);
|
||||||
if (!cmd) {
|
if (!cmd) {
|
||||||
tmate_info("parse error: %s", cause);
|
tmate_debug("parse error: %s", cause);
|
||||||
free(cause);
|
free(cause);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd_str = cmd_print(cmd);
|
cmd_str = cmd_print(cmd);
|
||||||
tmate_info("Local cmd: %s", cmd_str);
|
tmate_debug("Local cmd: %s", cmd_str);
|
||||||
free(cmd_str);
|
free(cmd_str);
|
||||||
|
|
||||||
cmdlist = xcalloc(1, sizeof *cmdlist);
|
cmdlist = xcalloc(1, sizeof *cmdlist);
|
||||||
|
@ -147,7 +147,7 @@ int tmate_should_exec_cmd_locally(const struct cmd_entry *cmd)
|
|||||||
|
|
||||||
void tmate_client_cmd_str(int client_id, const char *cmd)
|
void tmate_client_cmd_str(int client_id, const char *cmd)
|
||||||
{
|
{
|
||||||
tmate_info("Remote cmd (cid=%d): %s", client_id, cmd);
|
tmate_debug("Remote cmd (cid=%d): %s", client_id, cmd);
|
||||||
|
|
||||||
pack(array, 3);
|
pack(array, 3);
|
||||||
pack(int, TMATE_IN_EXEC_CMD_STR);
|
pack(int, TMATE_IN_EXEC_CMD_STR);
|
||||||
@ -217,7 +217,7 @@ void tmate_client_cmd(int client_id, struct cmd *cmd)
|
|||||||
free(cmd_str);
|
free(cmd_str);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
tmate_info("Remote cmd (cid=%d): %s", client_id, cmd_str);
|
tmate_debug("Remote cmd (cid=%d): %s", client_id, cmd_str);
|
||||||
free(cmd_str);
|
free(cmd_str);
|
||||||
|
|
||||||
extract_cmd(cmd, &argc, &argv);
|
extract_cmd(cmd, &argc, &argv);
|
||||||
|
30
tmate-main.c
30
tmate-main.c
@ -16,7 +16,6 @@
|
|||||||
#include <term.h>
|
#include <term.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/syslog.h>
|
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include "tmate.h"
|
#include "tmate.h"
|
||||||
@ -32,9 +31,8 @@ struct tmate_settings _tmate_settings = {
|
|||||||
.bind_addr = NULL,
|
.bind_addr = NULL,
|
||||||
.websocket_port = TMATE_DEFAULT_WEBSOCKET_PORT,
|
.websocket_port = TMATE_DEFAULT_WEBSOCKET_PORT,
|
||||||
.tmate_host = NULL,
|
.tmate_host = NULL,
|
||||||
.log_level = LOG_NOTICE,
|
.log_level = LOG_INFO,
|
||||||
.use_proxy_protocol = false,
|
.use_proxy_protocol = false,
|
||||||
.use_syslog = false,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct tmate_settings *tmate_settings = &_tmate_settings;
|
struct tmate_settings *tmate_settings = &_tmate_settings;
|
||||||
@ -51,7 +49,7 @@ void request_server_termination(void)
|
|||||||
|
|
||||||
static void usage(void)
|
static void usage(void)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "usage: tmate-ssh-server [-b ip] [-h hostname] [-k keys_dir] [-p listen_port] [-q ssh_port_advertized] [-w websocket_hostname] [-z websocket_port] [-x] [-s] [-v]\n");
|
fprintf(stderr, "usage: tmate-ssh-server [-b ip] [-h hostname] [-k keys_dir] [-p listen_port] [-q ssh_port_advertized] [-w websocket_hostname] [-z websocket_port] [-x] [-v]\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char* get_full_hostname(void)
|
static char* get_full_hostname(void)
|
||||||
@ -71,7 +69,7 @@ static char* get_full_hostname(void)
|
|||||||
hints.ai_flags = AI_CANONNAME;
|
hints.ai_flags = AI_CANONNAME;
|
||||||
|
|
||||||
if ((gai_result = getaddrinfo(hostname, NULL, &hints, &info)) != 0) {
|
if ((gai_result = getaddrinfo(hostname, NULL, &hints, &info)) != 0) {
|
||||||
tmate_warn("cannot lookup hostname: %s", gai_strerror(gai_result));
|
tmate_info("cannot lookup hostname: %s", gai_strerror(gai_result));
|
||||||
return xstrdup(hostname);
|
return xstrdup(hostname);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +102,7 @@ int main(int argc, char **argv, char **envp)
|
|||||||
{
|
{
|
||||||
int opt;
|
int opt;
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "b:h:k:p:q:w:z:xsv")) != -1) {
|
while ((opt = getopt(argc, argv, "b:h:k:p:q:w:z:xv")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'b':
|
case 'b':
|
||||||
tmate_settings->bind_addr = xstrdup(optarg);
|
tmate_settings->bind_addr = xstrdup(optarg);
|
||||||
@ -130,9 +128,6 @@ int main(int argc, char **argv, char **envp)
|
|||||||
case 'x':
|
case 'x':
|
||||||
tmate_settings->use_proxy_protocol = true;
|
tmate_settings->use_proxy_protocol = true;
|
||||||
break;
|
break;
|
||||||
case 's':
|
|
||||||
tmate_settings->use_syslog = true;
|
|
||||||
break;
|
|
||||||
case 'v':
|
case 'v':
|
||||||
tmate_settings->log_level++;
|
tmate_settings->log_level++;
|
||||||
break;
|
break;
|
||||||
@ -142,8 +137,7 @@ int main(int argc, char **argv, char **envp)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init_logging("tmate-remote-tmux",
|
init_logging(tmate_settings->log_level);
|
||||||
tmate_settings->use_syslog, tmate_settings->log_level);
|
|
||||||
|
|
||||||
setup_locale();
|
setup_locale();
|
||||||
|
|
||||||
@ -196,11 +190,17 @@ void set_session_token(struct tmate_session *session, const char *token)
|
|||||||
xasprintf((char **)&session->obfuscated_session_token, "%.4s...",
|
xasprintf((char **)&session->obfuscated_session_token, "%.4s...",
|
||||||
session->session_token);
|
session->session_token);
|
||||||
|
|
||||||
memset(cmdline, 0, cmdline_end - cmdline);
|
size_t size = cmdline_end - cmdline;
|
||||||
sprintf(cmdline, "tmate-ssh-server [%s] %s %s",
|
memset(cmdline, 0, size);
|
||||||
|
snprintf(cmdline, size-1, "tmate-ssh-server [%s] %s %s",
|
||||||
tmate_session->obfuscated_session_token,
|
tmate_session->obfuscated_session_token,
|
||||||
session->ssh_client.role == TMATE_ROLE_DAEMON ? "(daemon)" : "(pty client)",
|
session->ssh_client.role == TMATE_ROLE_DAEMON ? "(daemon)" : "(pty client)",
|
||||||
session->ssh_client.ip_address);
|
session->ssh_client.ip_address);
|
||||||
|
|
||||||
|
char *log_prefix;
|
||||||
|
xasprintf(&log_prefix, "[%s] ", session->obfuscated_session_token);
|
||||||
|
set_log_prefix(log_prefix);
|
||||||
|
free(log_prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
void close_fds_except(int *fd_to_preserve, int num_fds)
|
void close_fds_except(int *fd_to_preserve, int num_fds)
|
||||||
@ -273,8 +273,8 @@ void get_in_jail(void)
|
|||||||
|
|
||||||
nice(1);
|
nice(1);
|
||||||
|
|
||||||
tmate_info("Dropped priviledges to %s (%d,%d), jailed in %s",
|
tmate_debug("Dropped priviledges to %s (%d,%d), jailed in %s",
|
||||||
TMATE_JAIL_USER, uid, gid, TMATE_WORKDIR "/jail");
|
TMATE_JAIL_USER, uid, gid, TMATE_WORKDIR "/jail");
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_ncurse(int fd, const char *name)
|
void setup_ncurse(int fd, const char *name)
|
||||||
|
@ -176,8 +176,7 @@ void tmate_spawn_pty_client(struct tmate_session *session)
|
|||||||
|
|
||||||
set_session_token(session, token);
|
set_session_token(session, token);
|
||||||
|
|
||||||
tmate_info("Spawning pty client for %s (%s)",
|
tmate_info("Spawning pty client ip=%s", client->ip_address);
|
||||||
client->ip_address, client->pubkey);
|
|
||||||
|
|
||||||
session->tmux_socket_fd = client_connect(session->ev_base, socket_path, 0);
|
session->tmux_socket_fd = client_connect(session->ev_base, socket_path, 0);
|
||||||
if (session->tmux_socket_fd < 0) {
|
if (session->tmux_socket_fd < 0) {
|
||||||
|
@ -59,8 +59,8 @@ static void on_daemon_encoder_write(void *userdata, struct evbuffer *buffer)
|
|||||||
|
|
||||||
written = ssh_channel_write(session->ssh_client.channel, buf, len);
|
written = ssh_channel_write(session->ssh_client.channel, buf, len);
|
||||||
if (written < 0) {
|
if (written < 0) {
|
||||||
tmate_warn("Error writing to channel: %s",
|
tmate_info("Error writing to channel: %s",
|
||||||
ssh_get_error(session->ssh_client.session));
|
ssh_get_error(session->ssh_client.session));
|
||||||
request_server_termination();
|
request_server_termination();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -156,8 +156,8 @@ void tmate_spawn_daemon(struct tmate_session *session)
|
|||||||
set_session_token(session, token);
|
set_session_token(session, token);
|
||||||
free(token);
|
free(token);
|
||||||
|
|
||||||
tmate_info("Spawning daemon for %s at %s (%s)",
|
tmate_info("Spawning daemon username=%s ip=%s",
|
||||||
client->username, client->ip_address, client->pubkey);
|
client->username, client->ip_address);
|
||||||
|
|
||||||
session->tmux_socket_fd = server_create_socket();
|
session->tmux_socket_fd = server_create_socket();
|
||||||
if (session->tmux_socket_fd < 0)
|
if (session->tmux_socket_fd < 0)
|
||||||
@ -197,8 +197,8 @@ static void handle_session_name_options(const char *name, __unused const char *v
|
|||||||
!strcmp(name, "tmate-session-name-ro")) {
|
!strcmp(name, "tmate-session-name-ro")) {
|
||||||
static bool warned;
|
static bool warned;
|
||||||
if (!warned) {
|
if (!warned) {
|
||||||
tmate_warn("/!\\ Named sessions are not supported without the websocket server");
|
tmate_info("Named sessions are not supported (no websocket server)");
|
||||||
tmate_notify("/!\\ Named sessions are not supported without the websocket server");
|
tmate_notify("Named sessions are not supported (no websocket server)");
|
||||||
}
|
}
|
||||||
warned = true;
|
warned = true;
|
||||||
}
|
}
|
||||||
|
@ -14,12 +14,12 @@ void tmate_dump_exec_response(struct tmate_session *session,
|
|||||||
ssh_channel_close(client->channel);
|
ssh_channel_close(client->channel);
|
||||||
|
|
||||||
if (event_base_loopexit(session->ev_base, NULL) < 0)
|
if (event_base_loopexit(session->ev_base, NULL) < 0)
|
||||||
tmate_fatal("cannot stop event loop");
|
tmate_fatal("Cannot stop event loop");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void on_websocket_error(struct tmate_session *session, __unused short events)
|
static void on_websocket_error(struct tmate_session *session, __unused short events)
|
||||||
{
|
{
|
||||||
tmate_warn("Lost websocket server connection");
|
tmate_info("Lost websocket server connection");
|
||||||
tmate_dump_exec_response(session, 1, "Internal Error\r\n");
|
tmate_dump_exec_response(session, 1, "Internal Error\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,6 +38,10 @@ static void tmate_client_exec_init(struct tmate_session *session)
|
|||||||
|
|
||||||
void tmate_spawn_exec(struct tmate_session *session)
|
void tmate_spawn_exec(struct tmate_session *session)
|
||||||
{
|
{
|
||||||
|
struct tmate_ssh_client *client = &session->ssh_client;
|
||||||
|
|
||||||
|
tmate_info("Spawning exec client ip=%s", client->ip_address);
|
||||||
|
|
||||||
close_fds_except((int[]){ssh_get_fd(session->ssh_client.session),
|
close_fds_except((int[]){ssh_get_fd(session->ssh_client.session),
|
||||||
log_file ? fileno(log_file) : -1,
|
log_file ? fileno(log_file) : -1,
|
||||||
session->websocket_fd}, 3);
|
session->websocket_fd}, 3);
|
||||||
|
@ -186,7 +186,7 @@ static void on_ssh_read(__unused evutil_socket_t fd, __unused short what, void *
|
|||||||
ssh_execute_message_callbacks(client->session);
|
ssh_execute_message_callbacks(client->session);
|
||||||
|
|
||||||
if (!ssh_is_connected(client->session)) {
|
if (!ssh_is_connected(client->session)) {
|
||||||
tmate_warn("SSH Disconnected");
|
tmate_debug("ssh disconnected");
|
||||||
|
|
||||||
event_del(&client->ev_ssh);
|
event_del(&client->ev_ssh);
|
||||||
|
|
||||||
@ -204,7 +204,7 @@ static void register_on_ssh_read(struct tmate_ssh_client *client)
|
|||||||
|
|
||||||
static void handle_sigalrm(__unused int sig)
|
static void handle_sigalrm(__unused int sig)
|
||||||
{
|
{
|
||||||
tmate_fatal_info("Connection grace period (%ds) passed", TMATE_SSH_GRACE_PERIOD);
|
tmate_fatal_quiet("Connection grace period (%ds) passed", TMATE_SSH_GRACE_PERIOD);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void client_bootstrap(struct tmate_session *_session)
|
static void client_bootstrap(struct tmate_session *_session)
|
||||||
@ -214,7 +214,7 @@ static void client_bootstrap(struct tmate_session *_session)
|
|||||||
ssh_event mainloop;
|
ssh_event mainloop;
|
||||||
ssh_session session = client->session;
|
ssh_session session = client->session;
|
||||||
|
|
||||||
tmate_info("Bootstrapping ssh client ip=%s", client->ip_address);
|
tmate_debug("Bootstrapping ssh client ip=%s", client->ip_address);
|
||||||
|
|
||||||
_session->ev_base = osdep_event_init();
|
_session->ev_base = osdep_event_init();
|
||||||
|
|
||||||
@ -247,14 +247,14 @@ static void client_bootstrap(struct tmate_session *_session)
|
|||||||
|
|
||||||
tmate_debug("Exchanging DH keys");
|
tmate_debug("Exchanging DH keys");
|
||||||
if (ssh_handle_key_exchange(session) < 0)
|
if (ssh_handle_key_exchange(session) < 0)
|
||||||
tmate_fatal_info("Error doing the key exchange: %s", ssh_get_error(session));
|
tmate_fatal_quiet("Error doing the key exchange: %s", ssh_get_error(session));
|
||||||
|
|
||||||
mainloop = ssh_event_new();
|
mainloop = ssh_event_new();
|
||||||
ssh_event_add_session(mainloop, session);
|
ssh_event_add_session(mainloop, session);
|
||||||
|
|
||||||
while (!client->role) {
|
while (!client->role) {
|
||||||
if (ssh_event_dopoll(mainloop, -1) == SSH_ERROR)
|
if (ssh_event_dopoll(mainloop, -1) == SSH_ERROR)
|
||||||
tmate_fatal_info("Error polling ssh socket: %s", ssh_get_error(session));
|
tmate_fatal_quiet("Error polling ssh socket: %s", ssh_get_error(session));
|
||||||
}
|
}
|
||||||
|
|
||||||
alarm(0);
|
alarm(0);
|
||||||
@ -298,7 +298,7 @@ static int get_client_ip_socket(int fd, char *dst, size_t len)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_single_line(int fd, char *dst, size_t len)
|
static int read_single_line(int fd, char *dst, size_t len)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* This reads exactly one line from fd.
|
* This reads exactly one line from fd.
|
||||||
@ -314,26 +314,30 @@ static void read_single_line(int fd, char *dst, size_t len)
|
|||||||
|
|
||||||
if (dst[i] == '\n') {
|
if (dst[i] == '\n') {
|
||||||
dst[i] = '\0';
|
dst[i] = '\0';
|
||||||
return;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tmate_fatal("Cannot read proxy header. Load balancer may be misconfigured");
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_client_ip_proxy_protocol(int fd, char *dst, size_t len)
|
static int get_client_ip_proxy_protocol(int fd, char *dst, size_t len)
|
||||||
{
|
{
|
||||||
char header[110];
|
char header[110];
|
||||||
int tok_num;
|
int tok_num;
|
||||||
const char *signature = "PROXY ";
|
|
||||||
|
|
||||||
if (read(fd, header, strlen(signature)) != (ssize_t)strlen(signature))
|
#define SIGNATURE "PROXY "
|
||||||
tmate_fatal("Cannot read proxy header");
|
ssize_t ret = read(fd, header, sizeof(SIGNATURE)-1);
|
||||||
|
if (ret <= 0)
|
||||||
|
tmate_fatal_quiet("Disconnected, health checker?");
|
||||||
|
if (ret != sizeof(SIGNATURE)-1)
|
||||||
|
return -1;
|
||||||
|
if (memcmp(header, SIGNATURE, sizeof(SIGNATURE)-1))
|
||||||
|
return -1;
|
||||||
|
#undef SIGNATURE
|
||||||
|
|
||||||
if (memcmp(header, signature, strlen(signature)))
|
if (read_single_line(fd, header, sizeof(header)) < 0)
|
||||||
tmate_fatal("No proxy header found. Load balancer may be misconfigured");
|
return -1;
|
||||||
|
|
||||||
read_single_line(fd, header, sizeof(header));
|
|
||||||
|
|
||||||
tmate_debug("proxy header: %s", header);
|
tmate_debug("proxy header: %s", header);
|
||||||
|
|
||||||
@ -344,7 +348,7 @@ static int get_client_ip_proxy_protocol(int fd, char *dst, size_t len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (tok_num != 5)
|
if (tok_num != 5)
|
||||||
tmate_fatal("Proxy header is invalid");
|
return -1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -360,7 +364,8 @@ static int get_client_ip(int fd, char *dst, size_t len)
|
|||||||
static void ssh_log_function(int priority, const char *function,
|
static void ssh_log_function(int priority, const char *function,
|
||||||
const char *buffer, __unused void *userdata)
|
const char *buffer, __unused void *userdata)
|
||||||
{
|
{
|
||||||
tmate_log(LOG_NOTICE + priority, "[%s] %s", function, buffer);
|
/* loglevel already applied */
|
||||||
|
log_emit(LOG_DEBUG, "[%s] %s", function, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int max(int a, int b)
|
static inline int max(int a, int b)
|
||||||
@ -380,7 +385,7 @@ static void ssh_import_key(ssh_bind bind, const char *keys_dir, const char *name
|
|||||||
if (access(path, F_OK) < 0)
|
if (access(path, F_OK) < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
tmate_notice("Loading key %s", path);
|
tmate_info("Loading key %s", path);
|
||||||
|
|
||||||
ssh_pki_import_privkey_file(path, NULL, NULL, NULL, &key);
|
ssh_pki_import_privkey_file(path, NULL, NULL, NULL, &key);
|
||||||
ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
|
ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
|
||||||
@ -391,7 +396,7 @@ static ssh_bind prepare_ssh(const char *keys_dir, const char *bind_addr, int por
|
|||||||
ssh_bind bind;
|
ssh_bind bind;
|
||||||
int ssh_log_level;
|
int ssh_log_level;
|
||||||
|
|
||||||
ssh_log_level = SSH_LOG_WARNING + max(log_get_level() - LOG_NOTICE, 0);
|
ssh_log_level = SSH_LOG_WARNING + max(log_get_level() - LOG_INFO, 0);
|
||||||
|
|
||||||
ssh_set_log_callback(ssh_log_function);
|
ssh_set_log_callback(ssh_log_function);
|
||||||
|
|
||||||
@ -411,7 +416,7 @@ static ssh_bind prepare_ssh(const char *keys_dir, const char *bind_addr, int por
|
|||||||
if (ssh_bind_listen(bind) < 0)
|
if (ssh_bind_listen(bind) < 0)
|
||||||
tmate_fatal("Error listening to socket: %s\n", ssh_get_error(bind));
|
tmate_fatal("Error listening to socket: %s\n", ssh_get_error(bind));
|
||||||
|
|
||||||
tmate_notice("Accepting connections on %s:%d", bind_addr ?: "", port);
|
tmate_info("Accepting connections on %s:%d", bind_addr ?: "", port);
|
||||||
|
|
||||||
return bind;
|
return bind;
|
||||||
}
|
}
|
||||||
@ -428,18 +433,18 @@ static void handle_sigchld(__unused int sig)
|
|||||||
*/
|
*/
|
||||||
#if 0
|
#if 0
|
||||||
if (WIFEXITED(status))
|
if (WIFEXITED(status))
|
||||||
tmate_info("Child %d exited (%d)", pid, WEXITSTATUS(status));
|
tmate_debug("Child %d exited (%d)", pid, WEXITSTATUS(status));
|
||||||
if (WIFSIGNALED(status))
|
if (WIFSIGNALED(status))
|
||||||
tmate_info("Child %d killed (%d)", pid, WTERMSIG(status));
|
tmate_debug("Child %d killed (%d)", pid, WTERMSIG(status));
|
||||||
if (WIFSTOPPED(status))
|
if (WIFSTOPPED(status))
|
||||||
tmate_info("Child %d stopped (%d)", pid, WSTOPSIG(status));
|
tmate_debug("Child %d stopped (%d)", pid, WSTOPSIG(status));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_sigsegv(__unused int sig)
|
static void handle_sigsegv(__unused int sig)
|
||||||
{
|
{
|
||||||
tmate_crit("CRASH, printing stack trace");
|
tmate_info("CRASH, printing stack trace");
|
||||||
tmate_print_stack_trace();
|
tmate_print_stack_trace();
|
||||||
tmate_fatal("CRASHED");
|
tmate_fatal("CRASHED");
|
||||||
}
|
}
|
||||||
@ -485,10 +490,14 @@ void tmate_ssh_server_main(struct tmate_session *session, const char *keys_dir,
|
|||||||
signal(SIGALRM, handle_sigalrm);
|
signal(SIGALRM, handle_sigalrm);
|
||||||
alarm(TMATE_SSH_GRACE_PERIOD);
|
alarm(TMATE_SSH_GRACE_PERIOD);
|
||||||
|
|
||||||
if (get_client_ip(fd, client->ip_address, sizeof(client->ip_address)) < 0)
|
if (get_client_ip(fd, client->ip_address, sizeof(client->ip_address)) < 0) {
|
||||||
tmate_fatal("Error getting client IP from connection");
|
if (tmate_settings->use_proxy_protocol)
|
||||||
|
tmate_fatal("Proxy header invalid. Load balancer may be misconfigured");
|
||||||
|
else
|
||||||
|
tmate_fatal("Error getting client IP from connection");
|
||||||
|
}
|
||||||
|
|
||||||
tmate_info("Connection accepted ip=%s", client->ip_address);
|
tmate_debug("Connection accepted ip=%s", client->ip_address);
|
||||||
|
|
||||||
if (ssh_bind_accept_fd(bind, client->session, fd) < 0)
|
if (ssh_bind_accept_fd(bind, client->session, fd) < 0)
|
||||||
tmate_fatal("Error accepting connection: %s", ssh_get_error(bind));
|
tmate_fatal("Error accepting connection: %s", ssh_get_error(bind));
|
||||||
|
@ -197,7 +197,7 @@ static void tmate_dispatch_websocket_message(struct tmate_session *session,
|
|||||||
dispatch(TMATE_CTL_RESIZE, ctl_resize);
|
dispatch(TMATE_CTL_RESIZE, ctl_resize);
|
||||||
dispatch(TMATE_CTL_EXEC_RESPONSE, ctl_ssh_exec_response);
|
dispatch(TMATE_CTL_EXEC_RESPONSE, ctl_ssh_exec_response);
|
||||||
dispatch(TMATE_CTL_RENAME_SESSION, ctl_rename_session);
|
dispatch(TMATE_CTL_RENAME_SESSION, ctl_rename_session);
|
||||||
default: tmate_warn("Bad websocket server message type: %d", cmd);
|
default: tmate_info("Bad websocket server message type: %d", cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,6 +237,9 @@ void tmate_notify_client_join(__unused struct tmate_session *session,
|
|||||||
void tmate_notify_client_left(__unused struct tmate_session *session,
|
void tmate_notify_client_left(__unused struct tmate_session *session,
|
||||||
struct client *c)
|
struct client *c)
|
||||||
{
|
{
|
||||||
|
if (!(c->flags & CLIENT_IDENTIFIED))
|
||||||
|
return;
|
||||||
|
|
||||||
tmate_info("Client left (cid=%d)", c->id);
|
tmate_info("Client left (cid=%d)", c->id);
|
||||||
|
|
||||||
if (!tmate_has_websocket())
|
if (!tmate_has_websocket())
|
||||||
@ -410,7 +413,7 @@ static int _tmate_connect_to_websocket(const char *hostname, int port)
|
|||||||
if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
|
if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
|
||||||
tmate_fatal("Can't set websocket server socket to non-blocking");
|
tmate_fatal("Can't set websocket server socket to non-blocking");
|
||||||
|
|
||||||
tmate_info("Connected to websocket server at %s:%d", hostname, port);
|
tmate_debug("Connected to websocket server at %s:%d", hostname, port);
|
||||||
|
|
||||||
return sockfd;
|
return sockfd;
|
||||||
}
|
}
|
||||||
|
24
tmate.h
24
tmate.h
@ -1,7 +1,6 @@
|
|||||||
#ifndef TMATE_H
|
#ifndef TMATE_H
|
||||||
#define TMATE_H
|
#define TMATE_H
|
||||||
|
|
||||||
#include <sys/syslog.h>
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <msgpack.h>
|
#include <msgpack.h>
|
||||||
#include <libssh/libssh.h>
|
#include <libssh/libssh.h>
|
||||||
@ -14,24 +13,10 @@ struct tmate_session;
|
|||||||
|
|
||||||
/* log.c */
|
/* log.c */
|
||||||
|
|
||||||
extern void init_logging(const char *program_name, bool use_syslog, int log_level);
|
#define tmate_debug(...) log_emit(LOG_DEBUG, __VA_ARGS__)
|
||||||
extern void printflike(2, 3) tmate_log(int level, const char *msg, ...);
|
#define tmate_info(...) log_emit(LOG_INFO, __VA_ARGS__)
|
||||||
|
#define tmate_fatal(...) fatalx( __VA_ARGS__)
|
||||||
#define tmate_debug(str, ...) tmate_log(LOG_DEBUG, str, ##__VA_ARGS__)
|
#define tmate_fatal_quiet(...) ({tmate_debug(__VA_ARGS__); exit(1);})
|
||||||
#define tmate_info(str, ...) tmate_log(LOG_INFO, str, ##__VA_ARGS__)
|
|
||||||
#define tmate_notice(str, ...) tmate_log(LOG_NOTICE, str, ##__VA_ARGS__)
|
|
||||||
#define tmate_warn(str, ...) tmate_log(LOG_WARNING, str, ##__VA_ARGS__)
|
|
||||||
#define tmate_crit(str, ...) tmate_log(LOG_CRIT, str, ##__VA_ARGS__)
|
|
||||||
#define tmate_fatal(str, ...) \
|
|
||||||
({ \
|
|
||||||
tmate_crit("fatal: " str, ##__VA_ARGS__); \
|
|
||||||
exit(1); \
|
|
||||||
})
|
|
||||||
#define tmate_fatal_info(str, ...) \
|
|
||||||
({ \
|
|
||||||
tmate_info("fatal: " str, ##__VA_ARGS__); \
|
|
||||||
exit(1); \
|
|
||||||
})
|
|
||||||
|
|
||||||
/* tmate-auth-keys.c */
|
/* tmate-auth-keys.c */
|
||||||
extern void tmate_hook_set_option_auth(const char *name, const char *val);
|
extern void tmate_hook_set_option_auth(const char *name, const char *val);
|
||||||
@ -217,7 +202,6 @@ struct tmate_settings {
|
|||||||
const char *bind_addr;
|
const char *bind_addr;
|
||||||
int log_level;
|
int log_level;
|
||||||
bool use_proxy_protocol;
|
bool use_proxy_protocol;
|
||||||
bool use_syslog;
|
|
||||||
};
|
};
|
||||||
extern struct tmate_settings *tmate_settings;
|
extern struct tmate_settings *tmate_settings;
|
||||||
|
|
||||||
|
8
tmux.h
8
tmux.h
@ -2386,7 +2386,13 @@ void log_add_level(void);
|
|||||||
int log_get_level(void);
|
int log_get_level(void);
|
||||||
void log_open(const char *);
|
void log_open(const char *);
|
||||||
void log_close(void);
|
void log_close(void);
|
||||||
void printflike(1, 2) log_debug(const char *, ...);
|
#define LOG_ERROR 0
|
||||||
|
#define LOG_INFO 1
|
||||||
|
#define LOG_DEBUG 2
|
||||||
|
#define log_debug(...) log_emit(LOG_DEBUG+1, __VA_ARGS__)
|
||||||
|
void init_logging(int log_level);
|
||||||
|
void set_log_prefix(char *_log_prefix);
|
||||||
|
void printflike(2, 3) log_emit(int level, const char *, ...);
|
||||||
__dead void printflike(1, 2) fatal(const char *, ...);
|
__dead void printflike(1, 2) fatal(const char *, ...);
|
||||||
__dead void printflike(1, 2) fatalx(const char *, ...);
|
__dead void printflike(1, 2) fatalx(const char *, ...);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user