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

@@ -41,3 +41,4 @@ Makefile$
^plugins/sudoers/(cvtsudoers|sudoers|sudoreplay|testsudoers|tsdump|visudo|check_[a-z0-9_]+)$
^plugins/sudoers/.*\.(out|toke|err|json|ldif|sudo|ldif2sudo)$
^plugins/sudoers/regress/iolog_plugin/iolog$

View File

@@ -315,6 +315,8 @@ plugins/sudoers/interfaces.h
plugins/sudoers/iolog.c
plugins/sudoers/iolog.h
plugins/sudoers/iolog_path.c
plugins/sudoers/iolog_util.c
plugins/sudoers/iolog_util.h
plugins/sudoers/ldap.c
plugins/sudoers/ldap_conf.c
plugins/sudoers/ldap_util.c
@@ -461,6 +463,7 @@ plugins/sudoers/regress/env_match/check_env_pattern.c
plugins/sudoers/regress/env_match/data
plugins/sudoers/regress/iolog_path/check_iolog_path.c
plugins/sudoers/regress/iolog_path/data
plugins/sudoers/regress/iolog_plugin/check_iolog_plugin.c
plugins/sudoers/regress/logging/check_wrap.c
plugins/sudoers/regress/logging/check_wrap.in
plugins/sudoers/regress/logging/check_wrap.out.ok

View File

@@ -141,8 +141,8 @@ SHELL = @SHELL@
PROGS = sudoers.la visudo sudoreplay cvtsudoers testsudoers
TEST_PROGS = check_addr check_base64 check_digest check_env_pattern \
check_fill check_gentime check_hexchar check_iolog_path \
TEST_PROGS = check_addr check_base64 check_digest check_env_pattern check_fill \
check_gentime check_hexchar check_iolog_path check_iolog_plugin \
check_wrap check_starttime @SUDOERS_TEST_PROGS@
AUTH_OBJS = sudo_auth.lo @AUTH_OBJS@
@@ -167,7 +167,7 @@ CVTSUDOERS_OBJS = cvtsudoers.o cvtsudoers_json.o cvtsudoers_ldif.o \
cvtsudoers_pwutil.o fmtsudoers.lo locale.lo stubs.o \
sudo_printf.o ldap_util.lo
REPLAY_OBJS = getdate.o sudoreplay.o
REPLAY_OBJS = getdate.o sudoreplay.o iolog_util.lo
TEST_OBJS = fmtsudoers.lo group_plugin.lo interfaces.lo locale.lo net_ifs.o \
sudo_printf.o testsudoers.o tsgetgrpw.o
@@ -192,6 +192,10 @@ CHECK_HEXCHAR_OBJS = check_hexchar.o hexchar.lo sudoers_debug.lo
CHECK_IOLOG_PATH_OBJS = check_iolog_path.o iolog_path.lo locale.lo \
pwutil.lo pwutil_impl.lo redblack.lo sudoers_debug.lo
CHECK_IOLOG_PLUGIN_OBJS = check_iolog_plugin.o iolog.lo iolog_path.lo \
iolog_util.lo locale.lo mkdir_parents.lo pwutil.lo \
pwutil_impl.lo redblack.lo sudoers_debug.lo
CHECK_SYMBOLS_OBJS = check_symbols.o
CHECK_STARTTIME_OBJS = check_starttime.o starttime.lo sudoers_debug.lo
@@ -277,6 +281,9 @@ check_hexchar: $(CHECK_HEXCHAR_OBJS) $(LT_LIBS)
check_iolog_path: $(CHECK_IOLOG_PATH_OBJS) $(LT_LIBS)
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(CHECK_IOLOG_PATH_OBJS) $(LDFLAGS) $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(LIBS)
check_iolog_plugin: $(CHECK_IOLOG_PLUGIN_OBJS) $(LT_LIBS)
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(CHECK_IOLOG_PLUGIN_OBJS) $(LDFLAGS) $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(LIBS) @ZLIB@
check_starttime: $(CHECK_STARTTIME_OBJS) $(LT_LIBS)
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(CHECK_STARTTIME_OBJS) $(LDFLAGS) $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(LIBS)
@@ -410,6 +417,7 @@ check: $(TEST_PROGS) visudo testsudoers cvtsudoers
./check_gentime || rval=`expr $$rval + $$?`; \
./check_hexchar || rval=`expr $$rval + $$?`; \
./check_iolog_path $(srcdir)/regress/iolog_path/data || rval=`expr $$rval + $$?`; \
./check_iolog_plugin $(srcdir)/regress/iolog_plugin/iolog || rval=`expr $$rval + $$?`; \
./check_starttime || rval=`expr $$rval + $$?`; \
if test -f check_symbols; then \
./check_symbols .libs/sudoers.so $(shlib_exp) || rval=`expr $$rval + $$?`; \
@@ -709,6 +717,18 @@ check_iolog_path.o: $(srcdir)/regress/iolog_path/check_iolog_path.c \
$(srcdir)/sudoers.h $(srcdir)/sudoers_debug.h \
$(top_builddir)/config.h $(top_builddir)/pathnames.h
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/regress/iolog_path/check_iolog_path.c
check_iolog_plugin.o: $(srcdir)/regress/iolog_plugin/check_iolog_plugin.c \
$(devdir)/def_data.c $(devdir)/def_data.h \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_fatal.h $(incdir)/sudo_gettext.h \
$(incdir)/sudo_plugin.h $(incdir)/sudo_queue.h \
$(incdir)/sudo_util.h $(srcdir)/defaults.h \
$(srcdir)/iolog_util.h $(srcdir)/logging.h \
$(srcdir)/parse.h $(srcdir)/sudo_nss.h \
$(srcdir)/sudoers.h $(srcdir)/sudoers_debug.h \
$(top_builddir)/config.h $(top_builddir)/pathnames.h
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/regress/iolog_plugin/check_iolog_plugin.c
check_starttime.o: $(srcdir)/regress/starttime/check_starttime.c \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_fatal.h $(incdir)/sudo_util.h \
@@ -971,6 +991,12 @@ iolog_path.lo: $(srcdir)/iolog_path.c $(devdir)/def_data.h \
$(srcdir)/sudoers_debug.h $(top_builddir)/config.h \
$(top_builddir)/pathnames.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/iolog_path.c
iolog_util.lo: $(srcdir)/iolog_util.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_fatal.h $(incdir)/sudo_gettext.h \
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
$(srcdir)/iolog_util.h $(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/iolog_util.c
kerb5.lo: $(authdir)/kerb5.c $(devdir)/def_data.h $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_fatal.h $(incdir)/sudo_gettext.h \
@@ -1313,8 +1339,8 @@ sudoreplay.o: $(srcdir)/sudoreplay.c $(incdir)/compat/getopt.h \
$(incdir)/sudo_event.h $(incdir)/sudo_fatal.h \
$(incdir)/sudo_gettext.h $(incdir)/sudo_plugin.h \
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h $(srcdir)/iolog.h \
$(srcdir)/logging.h $(top_builddir)/config.h \
$(top_builddir)/pathnames.h
$(srcdir)/iolog_util.h $(srcdir)/logging.h \
$(top_builddir)/config.h $(top_builddir)/pathnames.h
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/sudoreplay.c
testsudoers.o: $(srcdir)/testsudoers.c $(devdir)/def_data.h $(devdir)/gram.h \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,389 @@
/*
* Copyright (c) 2018 Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <errno.h>
#include <pwd.h>
#include <time.h>
#include <unistd.h>
#define SUDO_ERROR_WRAP 0
#include "sudoers.h"
#include "def_data.c" /* for iolog_path.c */
#include "sudo_plugin.h"
#include "iolog_util.h"
extern struct io_plugin sudoers_io;
struct sudo_user sudo_user;
struct passwd *list_pw;
sudo_printf_t sudo_printf;
sudo_conv_t sudo_conv;
__dso_public int main(int argc, char *argv[], char *envp[]);
static void
usage(void)
{
fprintf(stderr, "usage: %s pathname\n", getprogname());
exit(1);
}
static int
sudo_printf_int(int msg_type, const char *fmt, ...)
{
va_list ap;
int len;
switch (msg_type) {
case SUDO_CONV_INFO_MSG:
va_start(ap, fmt);
len = vfprintf(stdout, fmt, ap);
va_end(ap);
break;
case SUDO_CONV_ERROR_MSG:
va_start(ap, fmt);
len = vfprintf(stderr, fmt, ap);
va_end(ap);
break;
default:
len = -1;
errno = EINVAL;
break;
}
return len;
}
bool
validate_iolog_info(const char *logfile)
{
time_t now;
struct log_info *info;
time(&now);
/* Parse log file. */
if ((info = parse_logfile(logfile)) == NULL)
return false;
if (strcmp(info->cwd, "/") != 0) {
sudo_warnx("bad cwd: want \"/\", got \"%s\"", info->cwd);
return false;
}
if (strcmp(info->user, "nobody") != 0) {
sudo_warnx("bad user: want \"nobody\" got \"%s\"", info->user);
return false;
}
if (strcmp(info->runas_user, "root") != 0) {
sudo_warnx("bad runas_user: want \"root\" got \"%s\"", info->runas_user);
return false;
}
if (info->runas_group != NULL) {
sudo_warnx("bad runas_group: want \"\" got \"%s\"", info->runas_user);
return false;
}
if (strcmp(info->tty, "/dev/console") != 0) {
sudo_warnx("bad tty: want \"/dev/console\" got \"%s\"", info->tty);
return false;
}
if (strcmp(info->cmd, "/usr/bin/id") != 0) {
sudo_warnx("bad command: want \"/usr/bin/id\" got \"%s\"", info->cmd);
return false;
}
if (info->rows != 24) {
sudo_warnx("bad rows: want 24 got %d", info->rows);
return false;
}
if (info->cols != 80) {
sudo_warnx("bad cols: want 80 got %d", info->cols);
return false;
}
if (info->tstamp < now || info->tstamp > now + 30) {
sudo_warnx("bad tstamp: want %lld got %lld", (long long)now,
(long long)info->tstamp);
return false;
}
return true;
}
bool
validate_timing(FILE *fp, int recno, int type, unsigned int p1, unsigned int p2)
{
struct timing_closure timing;
char buf[LINE_MAX];
double delay;
if (!fgets(buf, sizeof(buf), fp)) {
sudo_warn("unable to read timing file");
return false;
}
buf[strcspn(buf, "\n")] = '\0';
if (!parse_timing(buf, &delay, &timing)) {
sudo_warnx("invalid timing file line: %s", buf);
return false;
}
if (timing.idx != type) {
sudo_warnx("record %d: want type %d, got type %d", recno, type,
timing.idx);
return false;
}
if (type == IOFD_TIMING) {
if (timing.u.winsize.rows != (int)p1) {
sudo_warnx("record %d: want %u rows, got %u", recno, p1,
timing.u.winsize.rows);
return false;
}
if (timing.u.winsize.cols != (int)p2) {
sudo_warnx("record %d: want %u cols, got %u", recno, p2,
timing.u.winsize.cols);
return false;
}
} else {
if (timing.u.nbytes != p1) {
sudo_warnx("record %d: want len %u, got type %zu", recno, p1,
timing.u.nbytes);
return false;
}
}
if (delay > 0.01) {
sudo_warnx("record %d: got excessive delay %f", recno, delay);
return false;
}
return true;
}
int
main(int argc, char *argv[], char *envp[])
{
struct passwd pw, rpw;
int rc, tests = 0, errors = 0;
int cmnd_argc = 1;
const char *iolog_dir;
char iolog_path[PATH_MAX];
char buf[1024];
FILE *fp;
char *cmnd_argv[] = {
"/usr/bin/id",
NULL
};
char *user_info[] = {
"cols=80",
"lines=24",
"cwd=/",
"tty=/dev/console",
"user=nobody",
NULL
};
char *command_info[] = {
"command=/usr/bin/id",
iolog_path,
"iolog_stdin=true",
"iolog_stdout=true",
"iolog_stderr=true",
"iolog_ttyin=true",
"iolog_ttyout=true",
"iolog_compress=false",
"iolog_mode=0644",
"runas_gid=0",
"runas_egid=0",
"runas_uid=0",
"runas_euid=0",
NULL
};
char *settings[] = {
NULL
};
const char output[] = "uid=0(root) gid=0(wheel)\r\n";
initprogname(argc > 0 ? argv[0] : "check_iolog_plugin");
if (argc != 2)
usage();
iolog_dir = argv[1];
/* Set path to the iolog directory the user passed in. */
snprintf(iolog_path, sizeof(iolog_path), "iolog_path=%s", iolog_dir);
/* Set iolog uid/gid to invoking user. */
iolog_uid = geteuid();
iolog_gid = getegid();
/* Bare minimum to link. */
memset(&pw, 0, sizeof(pw));
memset(&rpw, 0, sizeof(rpw));
sudo_user.pw = &pw;
sudo_user._runas_pw = &rpw;
/* Test open endpoint. */
rc = sudoers_io.open(SUDO_API_VERSION, NULL, sudo_printf_int, settings,
user_info, command_info, cmnd_argc, cmnd_argv, envp, NULL);
tests++;
if (rc != 1) {
sudo_warnx("I/O log open endpoint failed");
errors++;
goto done;
}
/* Validate I/O log info file. */
tests++;
snprintf(iolog_path, sizeof(iolog_path), "%s/log", iolog_dir);
if (!validate_iolog_info(iolog_path))
errors++;
/* Test log_ttyout endpoint. */
rc = sudoers_io.log_ttyout(output, strlen(output));
tests++;
if (rc != 1) {
sudo_warnx("I/O log_ttyout endpoint failed");
errors++;
goto done;
}
/* Test change_winsize endpoint (twice). */
rc = sudoers_io.change_winsize(32, 128);
tests++;
if (rc != 1) {
sudo_warnx("I/O change_winsize endpoint failed");
errors++;
goto done;
}
rc = sudoers_io.change_winsize(24, 80);
tests++;
if (rc != 1) {
sudo_warnx("I/O change_winsize endpoint failed");
errors++;
goto done;
}
/* Close the plugin. */
sudoers_io.close(0, 0);
/* Validate the timing file. */
snprintf(iolog_path, sizeof(iolog_path), "%s/timing", iolog_dir);
tests++;
if ((fp = fopen(iolog_path, "r")) == NULL) {
sudo_warn("unable to open %s", iolog_path);
errors++;
goto done;
}
/* Line 1: output of id command. */
if (!validate_timing(fp, 1, IOFD_TTYOUT, strlen(output), 0)) {
errors++;
goto done;
}
/* Line 2: window size change. */
if (!validate_timing(fp, 2, IOFD_TIMING, 32, 128)) {
errors++;
goto done;
}
/* Line 3: window size change. */
if (!validate_timing(fp, 3, IOFD_TIMING, 24, 80)) {
errors++;
goto done;
}
/* Validate ttyout log file. */
snprintf(iolog_path, sizeof(iolog_path), "%s/ttyout", iolog_dir);
tests++;
fclose(fp);
if ((fp = fopen(iolog_path, "r")) == NULL) {
sudo_warn("unable to open %s", iolog_path);
errors++;
goto done;
}
if (!fgets(buf, sizeof(buf), fp)) {
sudo_warn("unable to read %s", iolog_path);
errors++;
goto done;
}
if (strcmp(buf, output) != 0) {
sudo_warnx("ttylog mismatch: want \"%s\", got \"%s\"", output, buf);
errors++;
goto done;
}
done:
if (tests != 0) {
printf("iolog_plugin: %d test%s run, %d errors, %d%% success rate\n",
tests, tests == 1 ? "" : "s", errors,
(tests - errors) * 100 / tests);
}
exit(errors);
}
/* Stub functions */
bool
set_perms(int perm)
{
return true;
}
bool
restore_perms(void)
{
return true;
}
bool
log_warning(int flags, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sudo_vwarn_nodebug(fmt, ap);
va_end(ap);
return true;
}
bool
log_warningx(int flags, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sudo_vwarnx_nodebug(fmt, ap);
va_end(ap);
return true;
}

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,6 +1286,7 @@ 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:
if (log->runas_group != NULL)
res = strcmp(sn->u.runas_group, log->runas_group) == 0;
break;
case ST_RUNASUSER:
@@ -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)
{