From beff818f25ae69cfe8501e18271b2509320de8df Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 26 Aug 2018 15:55:38 +0200 Subject: Added session recording with https://github.com/kilobyte/termrec --- src/recording.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/recording.c (limited to 'src/recording.c') diff --git a/src/recording.c b/src/recording.c new file mode 100644 index 0000000..6c0cdab --- /dev/null +++ b/src/recording.c @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../config.h" +#include "recording.h" +#include "state.h" + +#ifdef SESSION_RECORDING +static recorder recorder_handle = NULL; + +void clean_recorder(void) +{ + ttyrec_w_close(recorder_handle); + recorder_handle = NULL; +} + +static char * // this char * is to be freed from the calling code +make_filename(void) +{ + char * format = LOG_FILENAME_FORMAT; + char * filename = NULL; + unsigned int fname_pos = 0; + unsigned int format_pos = 0; + + filename = malloc(LOG_FILENAME_MAX_LEN+1); + + size_t format_len = strlen(format); + while (format_pos < format_len + 1 && fname_pos < LOG_FILENAME_MAX_LEN +1) { + if (format[format_pos] == '$') { + format_pos++; + if (format[format_pos] == 'd') { + time_t t; + struct tm * tm; + time(&t); + tm = localtime(&t); + fname_pos += strftime(filename + fname_pos, LOG_FILENAME_MAX_LEN - fname_pos, "%F", tm); + } else if (format[format_pos] == 'h') { + const char * hostname = state_get_ssh_destination(); + size_t len = strlen(hostname); + strcpy(filename + fname_pos, hostname); + fname_pos += len; + } else if (format[format_pos] == 'u') { + const char * username = state_get_username(); + size_t len = strlen(username); + strcpy(filename + fname_pos, username); + fname_pos += len; + } else if (format[format_pos] == 'i') { + sprintf(filename + fname_pos, "%d", state_get_session_id()); + fname_pos += strlen(filename + fname_pos); + } + format_pos++; + } else { + filename[fname_pos] = format[format_pos]; + if (filename[fname_pos] == '/') { // We create the corresponding directory if it doesn't exist + filename[fname_pos+1] = '\0'; + DIR* dir = opendir(filename); + if (dir) + closedir(dir); + else { + int ret = mkdir(filename, LOG_DIRECTORY_MODE); + if (ret != 0) { + fprintf(stderr, "Couldn't create log directory %s : %s\n", filename, strerror( errno )); + } + } + } + format_pos++; + fname_pos++; + } + } + + if (filename[fname_pos-1] != '\0') { + fprintf(stderr, "Log file name is too long, check LOG_FILENAME_FORMAT and LOG_FILENAME_MAX_LEN\n"); + free(filename); + filename = NULL; + } + return filename; +} + +char // returns 0 if ok, 1 otherwise +init_recorder(void) +{ + char * filename = make_filename(); + if (filename == NULL) + return 1; + struct timeval tm; + if (gettimeofday(&tm, NULL) != 0) { + fprintf(stderr, "OUPS gettimeofday failed!\n"); + return 1; + } + recorder_handle = ttyrec_w_open(-1, "ttyrec", filename, &tm); + free(filename); + if (recorder_handle == NULL) { + fprintf(stderr, "Couldn't open the session termrec log file.\n"); + return 1; + } + + return 0; +} + +char // returns 0 if ok, greater than 0 otherwise +record(void* data, size_t len) +{ + if(recorder_handle == NULL) + return 0; + + struct timeval tm; + if (gettimeofday(&tm, NULL) != 0) { + fprintf(stderr, "OUPS gettimeofday failed!\n"); + return 1; + } + if (ttyrec_w_write(recorder_handle, &tm, data, (int) len) == 0) { + fprintf(stderr, "OUPS ttyrec_w_write failed!\n"); + return 2; + } + return 0; +} +#endif -- cgit v1.2.3