--- embedaddon/sudo/plugins/sudoers/sudoreplay.c 2012/10/09 09:29:52 1.1.1.3 +++ embedaddon/sudo/plugins/sudoers/sudoreplay.c 2013/10/14 07:56:35 1.1.1.5 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2012 Todd C. Miller + * Copyright (c) 2009-2013 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -17,7 +17,6 @@ #include #include -#include #include #ifdef HAVE_SYS_SYSMACROS_H # include @@ -82,22 +81,26 @@ #ifdef HAVE_ZLIB_H # include #endif -#ifdef HAVE_SETLOCALE -# include -#endif #include #ifdef HAVE_STDBOOL_H # include #else # include "compat/stdbool.h" #endif /* HAVE_STDBOOL_H */ +#ifdef HAVE_GETOPT_LONG +# include +# else +# include "compat/getopt.h" +#endif /* HAVE_GETOPT_LONG */ #include #include "missing.h" #include "alloc.h" -#include "error.h" +#include "fatal.h" #include "gettext.h" +#include "logging.h" +#include "iolog.h" #include "sudo_plugin.h" #include "sudo_conf.h" #include "sudo_debug.h" @@ -106,31 +109,6 @@ # define LINE_MAX 2048 #endif -/* Must match the defines in iolog.c */ -#define IOFD_STDIN 0 -#define IOFD_STDOUT 1 -#define IOFD_STDERR 2 -#define IOFD_TTYIN 3 -#define IOFD_TTYOUT 4 -#define IOFD_TIMING 5 -#define IOFD_MAX 6 - -/* Bitmap of iofds to be replayed */ -unsigned int replay_filter = (1 << IOFD_STDOUT) | (1 << IOFD_STDERR) | - (1 << IOFD_TTYOUT); - -/* For getopt(3) */ -extern char *optarg; -extern int optind; - -union io_fd { - FILE *f; -#ifdef HAVE_ZLIB_H - gzFile g; -#endif - void *v; -}; - /* * Info present in the I/O log file */ @@ -150,7 +128,7 @@ struct log_info { * Handle expressions like: * ( user millert or user root ) and tty console and command /bin/sh */ -struct search_node { +static struct search_node { struct search_node *next; #define ST_EXPR 1 #define ST_TTY 2 @@ -181,22 +159,24 @@ struct search_node { } u; } *search_expr; -sudo_conv_t sudo_conv; /* NULL in non-plugin */ - #define STACK_NODE_SIZE 32 static struct search_node *node_stack[32]; static int stack_top; +static int timing_idx_adj = 0; + static const char *session_dir = _PATH_SUDO_IO_LOGDIR; -static union io_fd io_fds[IOFD_MAX]; -static const char *io_fnames[IOFD_MAX] = { - "/stdin", - "/stdout", - "/stderr", - "/ttyin", - "/ttyout", - "/timing" +static const char short_opts[] = "d:f:hlm:s:V"; +static struct option long_opts[] = { + { "directory", required_argument, NULL, 'd' }, + { "filter", required_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "list", no_argument, NULL, 'l' }, + { "max-wait", required_argument, NULL, 'm' }, + { "speed", required_argument, NULL, 's' }, + { "version", no_argument, NULL, 'V' }, + { NULL, no_argument, NULL, '\0' }, }; extern time_t get_date(char *); @@ -204,7 +184,6 @@ extern char *get_timestr(time_t, int); extern int term_raw(int, int); extern int term_restore(int, int); extern void get_ttysize(int *rowp, int *colp); -void cleanup(int); static int list_sessions(int, char **, const char *, const char *, const char *); static int parse_expr(struct search_node **, char **); @@ -212,11 +191,13 @@ static void check_input(int, double *); static void delay(double); static void help(void) __attribute__((__noreturn__)); static void usage(int); -static int open_io_fd(char *pathbuf, int len, const char *suffix, union io_fd *fdp); +static int open_io_fd(char *path, int len, struct io_log_file *iol); static int parse_timing(const char *buf, const char *decimal, int *idx, double *seconds, size_t *nbytes); static struct log_info *parse_logfile(char *logfile); static void free_log_info(struct log_info *li); static size_t atomic_writev(int fd, struct iovec *iov, int iovcnt); +static void sudoreplay_handler(int); +static void sudoreplay_cleanup(void); #ifdef HAVE_REGCOMP # define REGEX_T regex_t @@ -238,12 +219,15 @@ static size_t atomic_writev(int fd, struct iovec *iov, (s)[8] == '/' && (s)[9] == 'l' && (s)[10] == 'o' && (s)[11] == 'g' && \ (s)[12] == '\0') +__dso_public int main(int argc, char *argv[]); + int main(int argc, char *argv[]) { int ch, idx, plen, exitcode = 0, rows = 0, cols = 0; bool interactive = false, listonly = false, need_nlcr = false; - const char *id, *user = NULL, *pattern = NULL, *tty = NULL, *decimal = "."; + bool def_filter = true; + const char *decimal, *id, *user = NULL, *pattern = NULL, *tty = NULL; char path[PATH_MAX], buf[LINE_MAX], *cp, *ep; double seconds, to_wait, speed = 1.0, max_wait = 0; sigaction_t sa; @@ -264,33 +248,34 @@ main(int argc, char *argv[]) setprogname(argc > 0 ? argv[0] : "sudoreplay"); #endif -#ifdef HAVE_SETLOCALE - setlocale(LC_ALL, ""); + sudoers_setlocale(SUDOERS_LOCALE_USER, NULL); decimal = localeconv()->decimal_point; -#endif bindtextdomain("sudoers", LOCALEDIR); /* XXX - should have sudoreplay domain */ textdomain("sudoers"); + /* Register fatal/fatalx callback. */ + fatal_callback_register(sudoreplay_cleanup); + /* Read sudo.conf. */ - sudo_conf_read(); + sudo_conf_read(NULL); - while ((ch = getopt(argc, argv, "d:f:hlm:s:V")) != -1) { - switch(ch) { + while ((ch = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { + switch (ch) { case 'd': session_dir = optarg; break; case 'f': /* Set the replay filter. */ - replay_filter = 0; + def_filter = false; for (cp = strtok(optarg, ","); cp; cp = strtok(NULL, ",")) { if (strcmp(cp, "stdout") == 0) - SET(replay_filter, 1 << IOFD_STDOUT); + io_log_files[IOFD_STDOUT].enabled = true; else if (strcmp(cp, "stderr") == 0) - SET(replay_filter, 1 << IOFD_STDERR); + io_log_files[IOFD_STDERR].enabled = true; else if (strcmp(cp, "ttyout") == 0) - SET(replay_filter, 1 << IOFD_TTYOUT); + io_log_files[IOFD_TTYOUT].enabled = true; else - errorx(1, _("invalid filter option: %s"), optarg); + fatalx(_("invalid filter option: %s"), optarg); } break; case 'h': @@ -303,13 +288,13 @@ main(int argc, char *argv[]) errno = 0; max_wait = strtod(optarg, &ep); if (*ep != '\0' || errno != 0) - errorx(1, _("invalid max wait: %s"), optarg); + fatalx(_("invalid max wait: %s"), optarg); break; case 's': errno = 0; speed = strtod(optarg, &ep); if (*ep != '\0' || errno != 0) - errorx(1, _("invalid speed factor: %s"), optarg); + fatalx(_("invalid speed factor: %s"), optarg); break; case 'V': (void) printf(_("%s version %s\n"), getprogname(), PACKAGE_VERSION); @@ -331,29 +316,34 @@ main(int argc, char *argv[]) if (argc != 1) usage(1); + /* By default we replay stdout, stderr and ttyout. */ + if (def_filter) { + io_log_files[IOFD_STDOUT].enabled = true; + io_log_files[IOFD_STDERR].enabled = true; + io_log_files[IOFD_TTYOUT].enabled = true; + } + /* 6 digit ID in base 36, e.g. 01G712AB or free-form name */ id = argv[0]; if (VALID_ID(id)) { plen = snprintf(path, sizeof(path), "%s/%.2s/%.2s/%.2s/timing", session_dir, id, &id[2], &id[4]); if (plen <= 0 || plen >= sizeof(path)) - errorx(1, _("%s/%.2s/%.2s/%.2s/timing: %s"), session_dir, + fatalx(_("%s/%.2s/%.2s/%.2s/timing: %s"), session_dir, id, &id[2], &id[4], strerror(ENAMETOOLONG)); } else { plen = snprintf(path, sizeof(path), "%s/%s/timing", session_dir, id); if (plen <= 0 || plen >= sizeof(path)) - errorx(1, _("%s/%s/timing: %s"), session_dir, + fatalx(_("%s/%s/timing: %s"), session_dir, id, strerror(ENAMETOOLONG)); } plen -= 7; /* Open files for replay, applying replay filter for the -f flag. */ for (idx = 0; idx < IOFD_MAX; idx++) { - if (ISSET(replay_filter, 1 << idx) || idx == IOFD_TIMING) { - if (open_io_fd(path, plen, io_fnames[idx], &io_fds[idx]) == -1) - error(1, _("unable to open %s"), path); - } + if (open_io_fd(path, plen, &io_log_files[idx]) == -1) + fatal(_("unable to open %s"), path); } /* Parse log file. */ @@ -380,7 +370,7 @@ main(int argc, char *argv[]) memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESETHAND; - sa.sa_handler = cleanup; + sa.sa_handler = sudoreplay_handler; (void) sigaction(SIGINT, &sa, NULL); (void) sigaction(SIGKILL, &sa, NULL); (void) sigaction(SIGTERM, &sa, NULL); @@ -398,8 +388,7 @@ main(int argc, char *argv[]) if (ch != -1) (void) fcntl(STDIN_FILENO, F_SETFL, ch | O_NONBLOCK); if (!term_raw(STDIN_FILENO, 1)) - error(1, _("unable to set tty to raw mode")); - iovcnt = 0; + fatal(_("unable to set tty to raw mode")); iovmax = 32; iov = ecalloc(iovmax, sizeof(*iov)); } @@ -408,14 +397,14 @@ main(int argc, char *argv[]) * Timing file consists of line of the format: "%f %d\n" */ #ifdef HAVE_ZLIB_H - while (gzgets(io_fds[IOFD_TIMING].g, buf, sizeof(buf)) != NULL) { + while (gzgets(io_log_files[IOFD_TIMING].fd.g, buf, sizeof(buf)) != NULL) { #else - while (fgets(buf, sizeof(buf), io_fds[IOFD_TIMING].f) != NULL) { + while (fgets(buf, sizeof(buf), io_log_files[IOFD_TIMING].fd.f) != NULL) { #endif char last_char = '\0'; if (!parse_timing(buf, decimal, &idx, &seconds, &nbytes)) - errorx(1, _("invalid timing file line: %s"), buf); + fatalx(_("invalid timing file line: %s"), buf); if (interactive) check_input(STDIN_FILENO, &speed); @@ -426,8 +415,8 @@ main(int argc, char *argv[]) to_wait = max_wait; delay(to_wait); - /* Even if we are not relaying, we still have to delay. */ - if (io_fds[idx].v == NULL) + /* Even if we are not replaying, we still have to delay. */ + if (io_log_files[idx].fd.v == NULL) continue; /* Check whether we need to convert newline to CR LF pairs. */ @@ -441,9 +430,9 @@ main(int argc, char *argv[]) else len = nbytes; #ifdef HAVE_ZLIB_H - nread = gzread(io_fds[idx].g, buf, len); + nread = gzread(io_log_files[idx].fd.g, buf, len); #else - nread = fread(buf, 1, len, io_fds[idx].f); + nread = fread(buf, 1, len, io_log_files[idx].fd.f); #endif nbytes -= nread; @@ -498,7 +487,7 @@ main(int argc, char *argv[]) iovcnt = 1; } if (atomic_writev(STDOUT_FILENO, iov, iovcnt) == -1) - error(1, _("writing to standard output")); + fatal(_("writing to standard output")); } } term_restore(STDIN_FILENO, 1); @@ -527,25 +516,27 @@ delay(double secs) rval = nanosleep(&ts, &rts); } while (rval == -1 && errno == EINTR); if (rval == -1) { - error2(1, _("nanosleep: tv_sec %ld, tv_nsec %ld"), - (long)ts.tv_sec, (long)ts.tv_nsec); + fatal_nodebug("nanosleep: tv_sec %lld, tv_nsec %ld", + (long long)ts.tv_sec, (long)ts.tv_nsec); } } static int -open_io_fd(char *path, int len, const char *suffix, union io_fd *fdp) +open_io_fd(char *path, int len, struct io_log_file *iol) { debug_decl(open_io_fd, SUDO_DEBUG_UTIL) - path[len] = '\0'; - strlcat(path, suffix, PATH_MAX); + if (!iol->enabled) + debug_return_int(0); + path[len] = '\0'; + strlcat(path, iol->suffix, PATH_MAX); #ifdef HAVE_ZLIB_H - fdp->g = gzopen(path, "r"); + iol->fd.g = gzopen(path, "r"); #else - fdp->f = fopen(path, "r"); + iol->fd.f = fopen(path, "r"); #endif - debug_return_int(fdp->v ? 0 : -1); + debug_return_int(iol->fd.v ? 0 : -1); } /* @@ -640,7 +631,7 @@ parse_expr(struct search_node **headp, char *argv[]) continue; case 'c': /* command */ if (av[0][1] == '\0') - errorx(1, _("ambiguous expression \"%s\""), *av); + fatalx(_("ambiguous expression \"%s\""), *av); if (strncmp(*av, "cwd", strlen(*av)) == 0) type = ST_CWD; else if (strncmp(*av, "command", strlen(*av)) == 0) @@ -665,7 +656,7 @@ parse_expr(struct search_node **headp, char *argv[]) break; case 't': /* tty or to date */ if (av[0][1] == '\0') - errorx(1, _("ambiguous expression \"%s\""), *av); + fatalx(_("ambiguous expression \"%s\""), *av); if (strncmp(*av, "todate", strlen(*av)) == 0) type = ST_TODATE; else if (strncmp(*av, "tty", strlen(*av)) == 0) @@ -682,7 +673,7 @@ parse_expr(struct search_node **headp, char *argv[]) if (av[0][1] != '\0') goto bad; if (stack_top + 1 == STACK_NODE_SIZE) { - errorx(1, _("too many parenthesized expressions, max %d"), + fatalx(_("too many parenthesized expressions, max %d"), STACK_NODE_SIZE); } node_stack[stack_top++] = sn; @@ -693,13 +684,13 @@ parse_expr(struct search_node **headp, char *argv[]) goto bad; /* pop */ if (--stack_top < 0) - errorx(1, _("unmatched ')' in expression")); + fatalx(_("unmatched ')' in expression")); if (node_stack[stack_top]) sn->next = node_stack[stack_top]->next; debug_return_int(av - argv + 1); bad: default: - errorx(1, _("unknown search term \"%s\""), *av); + fatalx(_("unknown search term \"%s\""), *av); /* NOTREACHED */ } @@ -713,17 +704,17 @@ parse_expr(struct search_node **headp, char *argv[]) av += parse_expr(&newsn->u.expr, av + 1); } else { if (*(++av) == NULL) - errorx(1, _("%s requires an argument"), av[-1]); + fatalx(_("%s requires an argument"), av[-1]); #ifdef HAVE_REGCOMP if (type == ST_PATTERN) { if (regcomp(&newsn->u.cmdre, *av, REG_EXTENDED|REG_NOSUB) != 0) - errorx(1, _("invalid regular expression: %s"), *av); + fatalx(_("invalid regular expression: %s"), *av); } else #endif if (type == ST_TODATE || type == ST_FROMDATE) { newsn->u.tstamp = get_date(*av); if (newsn->u.tstamp == -1) - errorx(1, _("could not parse date \"%s\""), *av); + fatalx(_("could not parse date \"%s\""), *av); } else { newsn->u.ptr = *av; } @@ -736,11 +727,11 @@ parse_expr(struct search_node **headp, char *argv[]) sn = newsn; } if (stack_top) - errorx(1, _("unmatched '(' in expression")); + fatalx(_("unmatched '(' in expression")); if (or) - errorx(1, _("illegal trailing \"or\"")); + fatalx(_("illegal trailing \"or\"")); if (not) - errorx(1, _("illegal trailing \"!\"")); + fatalx(_("illegal trailing \"!\"")); debug_return_int(av - argv); } @@ -783,7 +774,7 @@ match_expr(struct search_node *head, struct log_info * if (rc && rc != REG_NOMATCH) { char buf[BUFSIZ]; regerror(rc, &sn->u.cmdre, buf, sizeof(buf)); - errorx(1, "%s", buf); + fatalx("%s", buf); } matched = rc == REG_NOMATCH ? 0 : 1; #else @@ -958,7 +949,7 @@ session_compare(const void *v1, const void *v2) return strcmp(s1, s2); } -/* XXX - always returns 0, calls error() on failure */ +/* XXX - always returns 0, calls fatal() on failure */ static int find_sessions(const char *dir, REGEX_T *re, const char *user, const char *tty) { @@ -977,13 +968,13 @@ find_sessions(const char *dir, REGEX_T *re, const char d = opendir(dir); if (d == NULL) - error(1, _("unable to open %s"), dir); + fatal(_("unable to open %s"), dir); /* XXX - would be faster to chdir and use relative names */ sdlen = strlcpy(pathbuf, dir, sizeof(pathbuf)); if (sdlen + 1 >= sizeof(pathbuf)) { errno = ENAMETOOLONG; - error(1, "%s/", dir); + fatal("%s/", dir); } pathbuf[sdlen++] = '/'; pathbuf[sdlen] = '\0'; @@ -1022,7 +1013,7 @@ find_sessions(const char *dir, REGEX_T *re, const char "%s/log", sessions[i]); if (len <= 0 || len >= sizeof(pathbuf) - sdlen) { errno = ENAMETOOLONG; - error(1, "%s/%s/log", dir, sessions[i]); + fatal("%s/%s/log", dir, sessions[i]); } efree(sessions[i]); @@ -1041,7 +1032,7 @@ find_sessions(const char *dir, REGEX_T *re, const char debug_return_int(0); } -/* XXX - always returns 0, calls error() on failure */ +/* XXX - always returns 0, calls fatal() on failure */ static int list_sessions(int argc, char **argv, const char *pattern, const char *user, const char *tty) @@ -1057,7 +1048,7 @@ list_sessions(int argc, char **argv, const char *patte if (pattern) { re = &rebuf; if (regcomp(re, pattern, REG_EXTENDED|REG_NOSUB) != 0) - errorx(1, _("invalid regex: %s"), pattern); + fatalx(_("invalid regular expression: %s"), pattern); } #else re = (char *) pattern; @@ -1135,9 +1126,13 @@ parse_timing(buf, decimal, idx, seconds, nbytes) /* Parse index */ ul = strtoul(buf, &ep, 10); - if (ul > IOFD_MAX) - goto bad; - *idx = (int)ul; + if (ul >= IOFD_TIMING) { + if (ul != 6) + goto bad; + /* work around a bug in timing files generated by sudo 1.8.7 */ + timing_idx_adj = 2; + } + *idx = (int)ul - timing_idx_adj; for (cp = ep + 1; isspace((unsigned char) *cp); cp++) continue; @@ -1181,10 +1176,10 @@ static void usage(int fatal) { fprintf(fatal ? stderr : stdout, - _("usage: %s [-h] [-d directory] [-m max_wait] [-s speed_factor] ID\n"), + _("usage: %s [-h] [-d dir] [-m num] [-s num] ID\n"), getprogname()); fprintf(fatal ? stderr : stdout, - _("usage: %s [-h] [-d directory] -l [search expression]\n"), + _("usage: %s [-h] [-d dir] -l [search expression]\n"), getprogname()); if (fatal) exit(1); @@ -1196,23 +1191,32 @@ help(void) (void) printf(_("%s - replay sudo session logs\n\n"), getprogname()); usage(0); (void) puts(_("\nOptions:\n" - " -d directory specify directory for session logs\n" - " -f filter specify which I/O type to display\n" - " -h display help message and exit\n" - " -l [expression] list available session IDs that match expression\n" - " -m max_wait max number of seconds to wait between events\n" - " -s speed_factor speed up or slow down output\n" - " -V display version information and exit")); + " -d, --directory=dir specify directory for session logs\n" + " -f, --filter=filter specify which I/O type(s) to display\n" + " -h, --help display help message and exit\n" + " -l, --list list available session IDs, with optional expression\n" + " -m, --max-wait=num max number of seconds to wait between events\n" + " -s, --speed=num speed up or slow down output\n" + " -V, --version display version information and exit")); exit(0); } /* - * Cleanup hook for error()/errorx() + * Cleanup hook for fatal()/fatalx() */ -void -cleanup(int signo) +static void +sudoreplay_cleanup(void) { term_restore(STDIN_FILENO, 0); - if (signo) - kill(getpid(), signo); +} + +/* + * Signal handler for SIGINT, SIGKILL, SIGTERM, SIGHUP + * Must be installed with SA_RESETHAND enabled. + */ +static void +sudoreplay_handler(int signo) +{ + term_restore(STDIN_FILENO, 0); + kill(getpid(), signo); }