Add regress test for I/O log plugin endpoints

This commit is contained in:
Todd C. Miller
2018-08-23 15:35:02 -06:00
parent 0d36ea9325
commit aaad554cda
6 changed files with 664 additions and 501 deletions

View File

@@ -61,6 +61,7 @@
#include "sudo_fatal.h"
#include "logging.h"
#include "iolog.h"
#include "iolog_util.h"
#include "sudo_queue.h"
#include "sudo_plugin.h"
#include "sudo_conf.h"
@@ -74,21 +75,6 @@
# include "compat/getopt.h"
#endif /* HAVE_GETOPT_LONG */
/*
* Info present in the I/O log file
*/
struct log_info {
char *cwd;
char *user;
char *runas_user;
char *runas_group;
char *tty;
char *cmd;
time_t tstamp;
int rows;
int cols;
};
struct replay_closure {
struct sudo_event_base *evbase;
struct sudo_event *delay_ev;
@@ -99,18 +85,7 @@ struct replay_closure {
struct sudo_event *sigquit_ev;
struct sudo_event *sigterm_ev;
struct sudo_event *sigtstp_ev;
struct timing_closure {
const char *decimal;
struct timeval *max_delay;
int idx;
union {
struct {
int rows;
int cols;
} winsize;
size_t nbytes; // XXX
} u;
} timing;
struct timing_closure timing;
bool interactive;
struct io_buffer {
unsigned int len; /* buffer length (how much produced) */
@@ -155,8 +130,6 @@ struct search_node {
static struct search_node_list search_expr = STAILQ_HEAD_INITIALIZER(search_expr);
static int timing_idx_adj;
static double speed_factor = 1.0;
static const char *session_dir = _PATH_SUDO_IO_LOGDIR;
@@ -188,10 +161,7 @@ extern time_t get_date(char *);
static int list_sessions(int, char **, const char *, const char *, const char *);
static int open_io_fd(char *path, int len, struct io_log_file *iol);
static int parse_expr(struct search_node_list *, char **, bool);
static bool parse_timing(const char *buf, double *seconds, struct timing_closure *timing);
static struct log_info *parse_logfile(char *logfile);
static void read_keyboard(int fd, int what, void *v);
static void free_log_info(struct log_info *li);
static void help(void) __attribute__((__noreturn__));
static int replay_session(struct timeval *max_wait, const char *decimal, bool interactive);
static void sudoreplay_cleanup(void);
@@ -1300,7 +1270,7 @@ static bool
match_expr(struct search_node_list *head, struct log_info *log, bool last_match)
{
struct search_node *sn;
bool res, matched = last_match;
bool res = false, matched = last_match;
int rc;
debug_decl(match_expr, SUDO_DEBUG_UTIL)
@@ -1316,7 +1286,8 @@ match_expr(struct search_node_list *head, struct log_info *log, bool last_match)
res = strcmp(sn->u.tty, log->tty) == 0;
break;
case ST_RUNASGROUP:
res = strcmp(sn->u.runas_group, log->runas_group) == 0;
if (log->runas_group != NULL)
res = strcmp(sn->u.runas_group, log->runas_group) == 0;
break;
case ST_RUNASUSER:
res = strcmp(sn->u.runas_user, log->runas_user) == 0;
@@ -1351,146 +1322,6 @@ match_expr(struct search_node_list *head, struct log_info *log, bool last_match)
debug_return_bool(matched);
}
static struct log_info *
parse_logfile(char *logfile)
{
FILE *fp;
char *buf = NULL, *cp, *ep;
const char *errstr;
size_t bufsize = 0, cwdsize = 0, cmdsize = 0;
struct log_info *li = NULL;
debug_decl(parse_logfile, SUDO_DEBUG_UTIL)
fp = fopen(logfile, "r");
if (fp == NULL) {
sudo_warn(U_("unable to open %s"), logfile);
goto bad;
}
/*
* ID file has three lines:
* 1) a log info line
* 2) cwd
* 3) command with args
*/
if ((li = calloc(1, sizeof(*li))) == NULL)
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
if (getline(&buf, &bufsize, fp) == -1 ||
getline(&li->cwd, &cwdsize, fp) == -1 ||
getline(&li->cmd, &cmdsize, fp) == -1) {
sudo_warn(U_("%s: invalid log file"), logfile);
goto bad;
}
/* Strip the newline from the cwd and command. */
li->cwd[strcspn(li->cwd, "\n")] = '\0';
li->cmd[strcspn(li->cmd, "\n")] = '\0';
/*
* Crack the log line (rows and cols not present in old versions).
* timestamp:user:runas_user:runas_group:tty:rows:cols
* XXX - probably better to use strtok and switch on the state.
*/
buf[strcspn(buf, "\n")] = '\0';
cp = buf;
/* timestamp */
if ((ep = strchr(cp, ':')) == NULL) {
sudo_warn(U_("%s: time stamp field is missing"), logfile);
goto bad;
}
*ep = '\0';
li->tstamp = sizeof(time_t) == 4 ? strtonum(cp, INT_MIN, INT_MAX, &errstr) :
strtonum(cp, LLONG_MIN, LLONG_MAX, &errstr);
if (errstr != NULL) {
sudo_warn(U_("%s: time stamp %s: %s"), logfile, cp, errstr);
goto bad;
}
/* user */
cp = ep + 1;
if ((ep = strchr(cp, ':')) == NULL) {
sudo_warn(U_("%s: user field is missing"), logfile);
goto bad;
}
if ((li->user = strndup(cp, (size_t)(ep - cp))) == NULL)
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
/* runas user */
cp = ep + 1;
if ((ep = strchr(cp, ':')) == NULL) {
sudo_warn(U_("%s: runas user field is missing"), logfile);
goto bad;
}
if ((li->runas_user = strndup(cp, (size_t)(ep - cp))) == NULL)
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
/* runas group */
cp = ep + 1;
if ((ep = strchr(cp, ':')) == NULL) {
sudo_warn(U_("%s: runas group field is missing"), logfile);
goto bad;
}
if (cp != ep) {
if ((li->runas_group = strndup(cp, (size_t)(ep - cp))) == NULL)
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
}
/* tty, followed by optional rows + columns */
cp = ep + 1;
if ((ep = strchr(cp, ':')) == NULL) {
/* just the tty */
if ((li->tty = strdup(cp)) == NULL)
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
} else {
/* tty followed by rows + columns */
if ((li->tty = strndup(cp, (size_t)(ep - cp))) == NULL)
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
cp = ep + 1;
/* need to NULL out separator to use strtonum() */
if ((ep = strchr(cp, ':')) != NULL) {
*ep = '\0';
}
li->rows = strtonum(cp, 1, INT_MAX, &errstr);
if (errstr != NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"%s: tty rows %s: %s", logfile, cp, errstr);
}
if (ep != NULL) {
cp = ep + 1;
li->cols = strtonum(cp, 1, INT_MAX, &errstr);
if (errstr != NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"%s: tty cols %s: %s", logfile, cp, errstr);
}
}
}
fclose(fp);
free(buf);
debug_return_ptr(li);
bad:
if (fp != NULL)
fclose(fp);
free(buf);
free_log_info(li);
debug_return_ptr(NULL);
}
static void
free_log_info(struct log_info *li)
{
if (li != NULL) {
free(li->cwd);
free(li->user);
free(li->runas_user);
free(li->runas_group);
free(li->tty);
free(li->cmd);
free(li);
}
}
static int
list_session(char *logfile, regex_t *re, const char *user, const char *tty)
{
@@ -1743,93 +1574,6 @@ read_keyboard(int fd, int what, void *v)
debug_return;
}
/*
* Parse a timing line, which is formatted as:
* index sleep_time num_bytes
* Where index is IOFD_*, sleep_time is the number of seconds to sleep
* before writing the data and num_bytes is the number of bytes to output.
* Returns true on success and false on failure.
*/
static bool
parse_timing(const char *buf, double *seconds, struct timing_closure *timing)
{
unsigned long ul;
long l;
double d, fract = 0;
char *cp, *ep;
debug_decl(parse_timing, SUDO_DEBUG_UTIL)
/* Parse index */
ul = strtoul(buf, &ep, 10);
if (ep == buf || !isspace((unsigned char) *ep))
goto bad;
if (ul >= IOFD_MAX) {
if (ul != 6)
goto bad;
/* work around a bug in timing files generated by sudo 1.8.7 */
timing_idx_adj = 2;
}
timing->idx = (int)ul - timing_idx_adj;
for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
continue;
/*
* Parse number of seconds. Sudo logs timing data in the C locale
* but this may not match the current locale so we cannot use strtod().
* Furthermore, sudo < 1.7.4 logged with the user's locale so we need
* to be able to parse those logs too.
*/
errno = 0;
l = strtol(cp, &ep, 10);
if (ep == cp || (*ep != '.' && strncmp(ep, timing->decimal, strlen(timing->decimal)) != 0))
goto bad;
if (l < 0 || l > INT_MAX || (errno == ERANGE && l == LONG_MAX))
goto bad;
*seconds = (double)l;
cp = ep + (*ep == '.' ? 1 : strlen(timing->decimal));
d = 10.0;
while (isdigit((unsigned char) *cp)) {
fract += (*cp - '0') / d;
d *= 10;
cp++;
}
*seconds += fract;
while (isspace((unsigned char) *cp))
cp++;
if (timing->idx == IOFD_TIMING) {
errno = 0;
ul = strtoul(cp, &ep, 10);
if (ep == cp || !isspace((unsigned char) *ep))
goto bad;
if (ul > INT_MAX || (errno == ERANGE && ul == ULONG_MAX))
goto bad;
timing->u.winsize.rows = (int)ul;
for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
continue;
errno = 0;
ul = strtoul(cp, &ep, 10);
if (ep == cp || *ep != '\0')
goto bad;
if (ul > INT_MAX || (errno == ERANGE && ul == ULONG_MAX))
goto bad;
timing->u.winsize.cols = (int)ul;
} else {
errno = 0;
ul = strtoul(cp, &ep, 10);
if (ep == cp || *ep != '\0')
goto bad;
if (ul > SIZE_MAX || (errno == ERANGE && ul == ULONG_MAX))
goto bad;
timing->u.nbytes = (size_t)ul;
}
debug_return_bool(true);
bad:
debug_return_bool(false);
}
static void
usage(int fatal)
{