2 * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #ifdef HAVE_SYS_SYSMACROS_H
22 # include <sys/sysmacros.h>
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_SELECT_H
29 #include <sys/select.h>
30 #endif /* HAVE_SYS_SELECT_H */
39 #endif /* STDC_HEADERS */
41 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
45 #endif /* HAVE_STRING_H */
48 #endif /* HAVE_STRINGS_H */
51 #endif /* HAVE_UNISTD_H */
52 #if TIME_WITH_SYS_TIME
55 #ifndef HAVE_STRUCT_TIMESPEC
56 # include "compat/timespec.h"
64 # define NAMLEN(dirent) strlen((dirent)->d_name)
66 # define dirent direct
67 # define NAMLEN(dirent) (dirent)->d_namlen
68 # ifdef HAVE_SYS_NDIR_H
69 # include <sys/ndir.h>
71 # ifdef HAVE_SYS_DIR_H
88 # include "compat/stdbool.h"
89 #endif /* HAVE_STDBOOL_H */
91 #include <pathnames.h>
98 #include "sudo_plugin.h"
99 #include "sudo_conf.h"
100 #include "sudo_debug.h"
103 # define LINE_MAX 2048
106 /* Must match the defines in iolog.c */
108 #define IOFD_STDOUT 1
109 #define IOFD_STDERR 2
111 #define IOFD_TTYOUT 4
112 #define IOFD_TIMING 5
115 /* Bitmap of iofds to be replayed */
116 unsigned int replay_filter = (1 << IOFD_STDOUT) | (1 << IOFD_STDERR) |
132 * Info present in the I/O log file
147 * Handle expressions like:
148 * ( user millert or user root ) and tty console and command /bin/sh
151 struct search_node *next;
156 #define ST_RUNASUSER 5
157 #define ST_RUNASGROUP 6
158 #define ST_FROMDATE 7
176 struct search_node *expr;
181 #define STACK_NODE_SIZE 32
182 static struct search_node *node_stack[32];
183 static int stack_top;
185 static const char *session_dir = _PATH_SUDO_IO_LOGDIR;
187 static union io_fd io_fds[IOFD_MAX];
188 static const char *io_fnames[IOFD_MAX] = {
197 extern time_t get_date(char *);
198 extern char *get_timestr(time_t, int);
199 extern int term_raw(int, int);
200 extern int term_restore(int, int);
201 extern void get_ttysize(int *rowp, int *colp);
203 static int list_sessions(int, char **, const char *, const char *, const char *);
204 static int parse_expr(struct search_node **, char **);
205 static void check_input(int, double *);
206 static void delay(double);
207 static void help(void) __attribute__((__noreturn__));
208 static void usage(int);
209 static int open_io_fd(char *pathbuf, int len, const char *suffix, union io_fd *fdp);
210 static int parse_timing(const char *buf, const char *decimal, int *idx, double *seconds, size_t *nbytes);
211 static struct log_info *parse_logfile(char *logfile);
212 static void free_log_info(struct log_info *li);
213 static size_t atomic_writev(int fd, struct iovec *iov, int iovcnt);
214 static void sudoreplay_handler(int);
215 static void sudoreplay_cleanup(void);
218 # define REGEX_T regex_t
220 # define REGEX_T char
223 #define VALID_ID(s) (isalnum((unsigned char)(s)[0]) && \
224 isalnum((unsigned char)(s)[1]) && isalnum((unsigned char)(s)[2]) && \
225 isalnum((unsigned char)(s)[3]) && isalnum((unsigned char)(s)[4]) && \
226 isalnum((unsigned char)(s)[5]) && (s)[6] == '\0')
228 #define IS_IDLOG(s) ( \
229 isalnum((unsigned char)(s)[0]) && isalnum((unsigned char)(s)[1]) && \
231 isalnum((unsigned char)(s)[3]) && isalnum((unsigned char)(s)[4]) && \
233 isalnum((unsigned char)(s)[6]) && isalnum((unsigned char)(s)[7]) && \
234 (s)[8] == '/' && (s)[9] == 'l' && (s)[10] == 'o' && (s)[11] == 'g' && \
237 __dso_public int main(int argc, char *argv[]);
240 main(int argc, char *argv[])
242 int ch, idx, plen, exitcode = 0, rows = 0, cols = 0;
243 bool interactive = false, listonly = false, need_nlcr = false;
244 const char *decimal, *id, *user = NULL, *pattern = NULL, *tty = NULL;
245 char path[PATH_MAX], buf[LINE_MAX], *cp, *ep;
246 double seconds, to_wait, speed = 1.0, max_wait = 0;
248 size_t len, nbytes, nread;
250 struct iovec *iov = NULL;
251 int iovcnt = 0, iovmax = 0;
252 debug_decl(main, SUDO_DEBUG_MAIN)
254 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
256 extern char *malloc_options;
257 malloc_options = "AFGJPR";
261 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
262 setprogname(argc > 0 ? argv[0] : "sudoreplay");
265 sudoers_setlocale(SUDOERS_LOCALE_USER, NULL);
266 decimal = localeconv()->decimal_point;
267 bindtextdomain("sudoers", LOCALEDIR); /* XXX - should have sudoreplay domain */
268 textdomain("sudoers");
270 /* Register fatal/fatalx callback. */
271 fatal_callback_register(sudoreplay_cleanup);
273 /* Read sudo.conf. */
274 sudo_conf_read(NULL);
276 while ((ch = getopt(argc, argv, "d:f:hlm:s:V")) != -1) {
279 session_dir = optarg;
282 /* Set the replay filter. */
284 for (cp = strtok(optarg, ","); cp; cp = strtok(NULL, ",")) {
285 if (strcmp(cp, "stdout") == 0)
286 SET(replay_filter, 1 << IOFD_STDOUT);
287 else if (strcmp(cp, "stderr") == 0)
288 SET(replay_filter, 1 << IOFD_STDERR);
289 else if (strcmp(cp, "ttyout") == 0)
290 SET(replay_filter, 1 << IOFD_TTYOUT);
292 fatalx(_("invalid filter option: %s"), optarg);
303 max_wait = strtod(optarg, &ep);
304 if (*ep != '\0' || errno != 0)
305 fatalx(_("invalid max wait: %s"), optarg);
309 speed = strtod(optarg, &ep);
310 if (*ep != '\0' || errno != 0)
311 fatalx(_("invalid speed factor: %s"), optarg);
314 (void) printf(_("%s version %s\n"), getprogname(), PACKAGE_VERSION);
326 exitcode = list_sessions(argc, argv, pattern, user, tty);
333 /* 6 digit ID in base 36, e.g. 01G712AB or free-form name */
336 plen = snprintf(path, sizeof(path), "%s/%.2s/%.2s/%.2s/timing",
337 session_dir, id, &id[2], &id[4]);
338 if (plen <= 0 || plen >= sizeof(path))
339 fatalx(_("%s/%.2s/%.2s/%.2s/timing: %s"), session_dir,
340 id, &id[2], &id[4], strerror(ENAMETOOLONG));
342 plen = snprintf(path, sizeof(path), "%s/%s/timing",
344 if (plen <= 0 || plen >= sizeof(path))
345 fatalx(_("%s/%s/timing: %s"), session_dir,
346 id, strerror(ENAMETOOLONG));
350 /* Open files for replay, applying replay filter for the -f flag. */
351 for (idx = 0; idx < IOFD_MAX; idx++) {
352 if (ISSET(replay_filter, 1 << idx) || idx == IOFD_TIMING) {
353 if (open_io_fd(path, plen, io_fnames[idx], &io_fds[idx]) == -1)
354 fatal(_("unable to open %s"), path);
358 /* Parse log file. */
360 strlcat(path, "/log", sizeof(path));
361 if ((li = parse_logfile(path)) == NULL)
363 printf(_("Replaying sudo session: %s\n"), li->cmd);
365 /* Make sure the terminal is large enough. */
366 get_ttysize(&rows, &cols);
367 if (li->rows != 0 && li->cols != 0) {
368 if (li->rows > rows) {
369 printf(_("Warning: your terminal is too small to properly replay the log.\n"));
370 printf(_("Log geometry is %d x %d, your terminal's geometry is %d x %d."), li->rows, li->cols, rows, cols);
374 /* Done with parsed log file. */
379 memset(&sa, 0, sizeof(sa));
380 sigemptyset(&sa.sa_mask);
381 sa.sa_flags = SA_RESETHAND;
382 sa.sa_handler = sudoreplay_handler;
383 (void) sigaction(SIGINT, &sa, NULL);
384 (void) sigaction(SIGKILL, &sa, NULL);
385 (void) sigaction(SIGTERM, &sa, NULL);
386 (void) sigaction(SIGHUP, &sa, NULL);
387 sa.sa_flags = SA_RESTART;
388 sa.sa_handler = SIG_IGN;
389 (void) sigaction(SIGTSTP, &sa, NULL);
390 (void) sigaction(SIGQUIT, &sa, NULL);
392 /* XXX - read user input from /dev/tty and set STDOUT to raw if not a pipe */
393 /* Set stdin to raw mode if it is a tty */
394 interactive = isatty(STDIN_FILENO);
396 ch = fcntl(STDIN_FILENO, F_GETFL, 0);
398 (void) fcntl(STDIN_FILENO, F_SETFL, ch | O_NONBLOCK);
399 if (!term_raw(STDIN_FILENO, 1))
400 fatal(_("unable to set tty to raw mode"));
402 iov = ecalloc(iovmax, sizeof(*iov));
406 * Timing file consists of line of the format: "%f %d\n"
409 while (gzgets(io_fds[IOFD_TIMING].g, buf, sizeof(buf)) != NULL) {
411 while (fgets(buf, sizeof(buf), io_fds[IOFD_TIMING].f) != NULL) {
413 char last_char = '\0';
415 if (!parse_timing(buf, decimal, &idx, &seconds, &nbytes))
416 fatalx(_("invalid timing file line: %s"), buf);
419 check_input(STDIN_FILENO, &speed);
421 /* Adjust delay using speed factor and clamp to max_wait */
422 to_wait = seconds / speed;
423 if (max_wait && to_wait > max_wait)
427 /* Even if we are not relaying, we still have to delay. */
428 if (io_fds[idx].v == NULL)
431 /* Check whether we need to convert newline to CR LF pairs. */
433 need_nlcr = (idx == IOFD_STDOUT || idx == IOFD_STDERR);
435 /* All output is sent to stdout. */
436 while (nbytes != 0) {
437 if (nbytes > sizeof(buf))
442 nread = gzread(io_fds[idx].g, buf, len);
444 nread = fread(buf, 1, len, io_fds[idx].f);
448 /* Convert newline to carriage return + linefeed if needed. */
450 size_t remainder = nread;
455 /* Handle a "\r\n" pair that spans a buffer. */
456 if (last_char == '\r' && buf[0] == '\n') {
460 while ((ep = memchr(ep + 1, '\n', remainder)) != NULL) {
461 /* Is there already a carriage return? */
462 if (cp != ep && ep[-1] == '\r') {
463 remainder = (size_t)(&buf[nread - 1] - ep);
467 /* Store the line in iov followed by \r\n pair. */
468 if (iovcnt + 3 > iovmax) {
470 iov = erealloc3(iov, iovmax, sizeof(*iov));
472 linelen = (size_t)(ep - cp) + 1;
473 iov[iovcnt].iov_base = cp;
474 iov[iovcnt].iov_len = linelen - 1; /* not including \n */
476 iov[iovcnt].iov_base = "\r\n";
477 iov[iovcnt].iov_len = 2;
480 remainder -= linelen;
482 if (cp - buf != nread) {
484 * Partial line without a linefeed or multiple lines
487 iov[iovcnt].iov_base = cp;
488 iov[iovcnt].iov_len = nread - (cp - buf);
491 last_char = buf[nread - 1]; /* stash last char of old buffer */
493 /* No conversion needed. */
494 iov[0].iov_base = buf;
495 iov[0].iov_len = nread;
498 if (atomic_writev(STDOUT_FILENO, iov, iovcnt) == -1)
499 fatal(_("writing to standard output"));
502 term_restore(STDIN_FILENO, 1);
504 sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode);
511 struct timespec ts, rts;
515 * Typical max resolution is 1/HZ but we can't portably check that.
516 * If the interval is small enough, just ignore it.
522 rts.tv_nsec = (secs - (double) rts.tv_sec) * 1000000000.0;
524 memcpy(&ts, &rts, sizeof(ts));
525 rval = nanosleep(&ts, &rts);
526 } while (rval == -1 && errno == EINTR);
528 fatal_nodebug("nanosleep: tv_sec %lld, tv_nsec %ld",
529 (long long)ts.tv_sec, (long)ts.tv_nsec);
534 open_io_fd(char *path, int len, const char *suffix, union io_fd *fdp)
536 debug_decl(open_io_fd, SUDO_DEBUG_UTIL)
539 strlcat(path, suffix, PATH_MAX);
542 fdp->g = gzopen(path, "r");
544 fdp->f = fopen(path, "r");
546 debug_return_int(fdp->v ? 0 : -1);
550 * Call writev(), restarting as needed and handling EAGAIN since
551 * fd may be in non-blocking mode.
554 atomic_writev(int fd, struct iovec *iov, int iovcnt)
556 ssize_t n, nwritten = 0;
557 size_t count, remainder, nbytes = 0;
559 debug_decl(atomic_writev, SUDO_DEBUG_UTIL)
561 for (i = 0; i < iovcnt; i++)
562 nbytes += iov[i].iov_len;
565 n = writev(STDOUT_FILENO, iov, iovcnt);
568 remainder = nbytes - nwritten;
571 /* short writev, adjust iov and do the rest. */
575 count += iov[i].iov_len;
576 if (count == remainder) {
581 if (count > remainder) {
582 size_t off = (count - remainder);
583 /* XXX - side effect prevents iov from being const */
584 iov[i].iov_base = (char *)iov[i].iov_base + off;
585 iov[i].iov_len -= off;
593 if (n == 0 || errno == EAGAIN) {
597 FD_SET(STDOUT_FILENO, &fdsw);
599 nready = select(STDOUT_FILENO + 1, NULL, &fdsw, NULL, NULL);
600 } while (nready == -1 && errno == EINTR);
609 debug_return_size_t(nwritten);
613 * Build expression list from search args
616 parse_expr(struct search_node **headp, char *argv[])
618 struct search_node *sn, *newsn;
619 char or = 0, not = 0, type, **av;
620 debug_decl(parse_expr, SUDO_DEBUG_UTIL)
623 for (av = argv; *av; av++) {
625 case 'a': /* and (ignore) */
626 if (strncmp(*av, "and", strlen(*av)) != 0)
630 if (strncmp(*av, "or", strlen(*av)) != 0)
634 case '!': /* negate */
635 if (av[0][1] != '\0')
639 case 'c': /* command */
640 if (av[0][1] == '\0')
641 fatalx(_("ambiguous expression \"%s\""), *av);
642 if (strncmp(*av, "cwd", strlen(*av)) == 0)
644 else if (strncmp(*av, "command", strlen(*av)) == 0)
649 case 'f': /* from date */
650 if (strncmp(*av, "fromdate", strlen(*av)) != 0)
654 case 'g': /* runas group */
655 if (strncmp(*av, "group", strlen(*av)) != 0)
657 type = ST_RUNASGROUP;
659 case 'r': /* runas user */
660 if (strncmp(*av, "runas", strlen(*av)) != 0)
664 case 't': /* tty or to date */
665 if (av[0][1] == '\0')
666 fatalx(_("ambiguous expression \"%s\""), *av);
667 if (strncmp(*av, "todate", strlen(*av)) == 0)
669 else if (strncmp(*av, "tty", strlen(*av)) == 0)
675 if (strncmp(*av, "user", strlen(*av)) != 0)
679 case '(': /* start sub-expression */
680 if (av[0][1] != '\0')
682 if (stack_top + 1 == STACK_NODE_SIZE) {
683 fatalx(_("too many parenthesized expressions, max %d"),
686 node_stack[stack_top++] = sn;
689 case ')': /* end sub-expression */
690 if (av[0][1] != '\0')
694 fatalx(_("unmatched ')' in expression"));
695 if (node_stack[stack_top])
696 sn->next = node_stack[stack_top]->next;
697 debug_return_int(av - argv + 1);
700 fatalx(_("unknown search term \"%s\""), *av);
704 /* Allocate new search node */
705 newsn = ecalloc(1, sizeof(*newsn));
708 newsn->negated = not;
709 /* newsn->next = NULL; */
710 if (type == ST_EXPR) {
711 av += parse_expr(&newsn->u.expr, av + 1);
714 fatalx(_("%s requires an argument"), av[-1]);
716 if (type == ST_PATTERN) {
717 if (regcomp(&newsn->u.cmdre, *av, REG_EXTENDED|REG_NOSUB) != 0)
718 fatalx(_("invalid regular expression: %s"), *av);
721 if (type == ST_TODATE || type == ST_FROMDATE) {
722 newsn->u.tstamp = get_date(*av);
723 if (newsn->u.tstamp == -1)
724 fatalx(_("could not parse date \"%s\""), *av);
729 not = or = 0; /* reset state */
737 fatalx(_("unmatched '(' in expression"));
739 fatalx(_("illegal trailing \"or\""));
741 fatalx(_("illegal trailing \"!\""));
743 debug_return_int(av - argv);
747 match_expr(struct search_node *head, struct log_info *log)
749 struct search_node *sn;
752 debug_decl(match_expr, SUDO_DEBUG_UTIL)
754 for (sn = head; sn; sn = sn->next) {
755 /* If we have no match, skip ahead to the next OR entry. */
756 if (!matched && !sn->or)
761 matched = match_expr(sn->u.expr, log);
764 matched = strcmp(sn->u.cwd, log->cwd) == 0;
767 matched = strcmp(sn->u.tty, log->tty) == 0;
770 matched = strcmp(sn->u.runas_group, log->runas_group) == 0;
773 matched = strcmp(sn->u.runas_user, log->runas_user) == 0;
776 matched = strcmp(sn->u.user, log->user) == 0;
780 rc = regexec(&sn->u.cmdre, log->cmd, 0, NULL, 0);
781 if (rc && rc != REG_NOMATCH) {
783 regerror(rc, &sn->u.cmdre, buf, sizeof(buf));
786 matched = rc == REG_NOMATCH ? 0 : 1;
788 matched = strstr(log.cmd, sn->u.pattern) != NULL;
792 matched = log->tstamp >= sn->u.tstamp;
795 matched = log->tstamp <= sn->u.tstamp;
801 debug_return_bool(matched);
804 static struct log_info *
805 parse_logfile(char *logfile)
808 char *buf = NULL, *cp, *ep;
809 size_t bufsize = 0, cwdsize = 0, cmdsize = 0;
810 struct log_info *li = NULL;
811 debug_decl(list_session, SUDO_DEBUG_UTIL)
813 fp = fopen(logfile, "r");
815 warning(_("unable to open %s"), logfile);
820 * ID file has three lines:
823 * 3) command with args
825 li = ecalloc(1, sizeof(*li));
826 if (getline(&buf, &bufsize, fp) == -1 ||
827 getline(&li->cwd, &cwdsize, fp) == -1 ||
828 getline(&li->cmd, &cmdsize, fp) == -1) {
832 /* Strip the newline from the cwd and command. */
833 li->cwd[strcspn(li->cwd, "\n")] = '\0';
834 li->cmd[strcspn(li->cmd, "\n")] = '\0';
837 * Crack the log line (rows and cols not present in old versions).
838 * timestamp:user:runas_user:runas_group:tty:rows:cols
840 buf[strcspn(buf, "\n")] = '\0';
843 if ((ep = strchr(buf, ':')) == NULL)
845 if ((li->tstamp = atoi(buf)) == 0)
850 if ((ep = strchr(cp, ':')) == NULL)
852 li->user = estrndup(cp, (size_t)(ep - cp));
856 if ((ep = strchr(cp, ':')) == NULL)
858 li->runas_user = estrndup(cp, (size_t)(ep - cp));
862 if ((ep = strchr(cp, ':')) == NULL)
865 li->runas_group = estrndup(cp, (size_t)(ep - cp));
867 /* tty, followed by optional rows + columns */
869 if ((ep = strchr(cp, ':')) == NULL) {
870 li->tty = estrdup(cp);
872 li->tty = estrndup(cp, (size_t)(ep - cp));
875 if ((ep = strchr(cp, ':')) != NULL) {
882 debug_return_ptr(li);
889 debug_return_ptr(NULL);
893 free_log_info(struct log_info *li)
898 efree(li->runas_user);
899 efree(li->runas_group);
907 list_session(char *logfile, REGEX_T *re, const char *user, const char *tty)
909 char idbuf[7], *idstr, *cp;
912 debug_decl(list_session, SUDO_DEBUG_UTIL)
914 if ((li = parse_logfile(logfile)) == NULL)
917 /* Match on search expression if there is one. */
918 if (search_expr && !match_expr(search_expr, li))
921 /* Convert from /var/log/sudo-sessions/00/00/01/log to 000001 */
922 cp = logfile + strlen(session_dir) + 1;
933 /* Not an id, just use the iolog_file portion. */
934 cp[strlen(cp) - 4] = '\0';
937 /* XXX - print rows + cols? */
938 printf("%s : %s : TTY=%s ; CWD=%s ; USER=%s ; ",
939 get_timestr(li->tstamp, 1), li->user, li->tty, li->cwd, li->runas_user);
941 printf("GROUP=%s ; ", li->runas_group);
942 printf("TSID=%s ; COMMAND=%s\n", idstr, li->cmd);
948 debug_return_int(rval);
952 session_compare(const void *v1, const void *v2)
954 const char *s1 = *(const char **)v1;
955 const char *s2 = *(const char **)v2;
956 return strcmp(s1, s2);
959 /* XXX - always returns 0, calls fatal() on failure */
961 find_sessions(const char *dir, REGEX_T *re, const char *user, const char *tty)
966 size_t sdlen, sessions_len = 0, sessions_size = 36*36;
968 char pathbuf[PATH_MAX], **sessions = NULL;
969 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
970 bool checked_type = true;
972 const bool checked_type = false;
974 debug_decl(find_sessions, SUDO_DEBUG_UTIL)
978 fatal(_("unable to open %s"), dir);
980 /* XXX - would be faster to chdir and use relative names */
981 sdlen = strlcpy(pathbuf, dir, sizeof(pathbuf));
982 if (sdlen + 1 >= sizeof(pathbuf)) {
983 errno = ENAMETOOLONG;
986 pathbuf[sdlen++] = '/';
987 pathbuf[sdlen] = '\0';
989 /* Store potential session dirs for sorting. */
990 sessions = emalloc2(sessions_size, sizeof(char *));
991 while ((dp = readdir(d)) != NULL) {
992 /* Skip "." and ".." */
993 if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
994 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
996 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
998 if (dp->d_type != DT_DIR) {
999 /* Not all file systems support d_type. */
1000 if (dp->d_type != DT_UNKNOWN)
1002 checked_type = false;
1007 /* Add name to session list. */
1008 if (sessions_len + 1 > sessions_size) {
1009 sessions_size <<= 1;
1010 sessions = erealloc3(sessions, sessions_size, sizeof(char *));
1012 sessions[sessions_len++] = estrdup(dp->d_name);
1016 /* Sort and list the sessions. */
1017 qsort(sessions, sessions_len, sizeof(char *), session_compare);
1018 for (i = 0; i < sessions_len; i++) {
1019 len = snprintf(&pathbuf[sdlen], sizeof(pathbuf) - sdlen,
1020 "%s/log", sessions[i]);
1021 if (len <= 0 || len >= sizeof(pathbuf) - sdlen) {
1022 errno = ENAMETOOLONG;
1023 fatal("%s/%s/log", dir, sessions[i]);
1027 /* Check for dir with a log file. */
1028 if (lstat(pathbuf, &sb) == 0 && S_ISREG(sb.st_mode)) {
1029 list_session(pathbuf, re, user, tty);
1031 /* Strip off "/log" and recurse if a dir. */
1032 pathbuf[sdlen + len - 4] = '\0';
1033 if (checked_type || (lstat(pathbuf, &sb) == 0 && S_ISDIR(sb.st_mode)))
1034 find_sessions(pathbuf, re, user, tty);
1039 debug_return_int(0);
1042 /* XXX - always returns 0, calls fatal() on failure */
1044 list_sessions(int argc, char **argv, const char *pattern, const char *user,
1047 REGEX_T rebuf, *re = NULL;
1048 debug_decl(list_sessions, SUDO_DEBUG_UTIL)
1050 /* Parse search expression if present */
1051 parse_expr(&search_expr, argv);
1054 /* optional regex */
1057 if (regcomp(re, pattern, REG_EXTENDED|REG_NOSUB) != 0)
1058 fatalx(_("invalid regular expression: %s"), pattern);
1061 re = (char *) pattern;
1062 #endif /* HAVE_REGCOMP */
1064 debug_return_int(find_sessions(session_dir, re, user, tty));
1068 * Check input for ' ', '<', '>'
1072 check_input(int ttyfd, double *speed)
1075 int nready, paused = 0;
1079 debug_decl(check_input, SUDO_DEBUG_UTIL)
1081 fdsr = ecalloc(howmany(ttyfd + 1, NFDBITS), sizeof(fd_mask));
1083 FD_SET(ttyfd, fdsr);
1087 nready = select(ttyfd + 1, fdsr, NULL, NULL, paused ? NULL : &tv);
1090 n = read(ttyfd, &ch, 1);
1114 * Parse a timing line, which is formatted as:
1115 * index sleep_time num_bytes
1116 * Where index is IOFD_*, sleep_time is the number of seconds to sleep
1117 * before writing the data and num_bytes is the number of bytes to output.
1118 * Returns 1 on success and 0 on failure.
1121 parse_timing(buf, decimal, idx, seconds, nbytes)
1123 const char *decimal;
1130 double d, fract = 0;
1132 debug_decl(parse_timing, SUDO_DEBUG_UTIL)
1135 ul = strtoul(buf, &ep, 10);
1139 for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
1143 * Parse number of seconds. Sudo logs timing data in the C locale
1144 * but this may not match the current locale so we cannot use strtod().
1145 * Furthermore, sudo < 1.7.4 logged with the user's locale so we need
1146 * to be able to parse those logs too.
1149 l = strtol(cp, &ep, 10);
1150 if ((errno == ERANGE && (l == LONG_MAX || l == LONG_MIN)) ||
1151 l < 0 || l > INT_MAX ||
1152 (*ep != '.' && strncmp(ep, decimal, strlen(decimal)) != 0)) {
1155 *seconds = (double)l;
1156 cp = ep + (*ep == '.' ? 1 : strlen(decimal));
1158 while (isdigit((unsigned char) *cp)) {
1159 fract += (*cp - '0') / d;
1164 while (isspace((unsigned char) *cp))
1168 ul = strtoul(cp, &ep, 10);
1169 if (errno == ERANGE && ul == ULONG_MAX)
1171 *nbytes = (size_t)ul;
1173 debug_return_int(1);
1175 debug_return_int(0);
1181 fprintf(fatal ? stderr : stdout,
1182 _("usage: %s [-h] [-d directory] [-m max_wait] [-s speed_factor] ID\n"),
1184 fprintf(fatal ? stderr : stdout,
1185 _("usage: %s [-h] [-d directory] -l [search expression]\n"),
1194 (void) printf(_("%s - replay sudo session logs\n\n"), getprogname());
1196 (void) puts(_("\nOptions:\n"
1197 " -d directory specify directory for session logs\n"
1198 " -f filter specify which I/O type to display\n"
1199 " -h display help message and exit\n"
1200 " -l [expression] list available session IDs that match expression\n"
1201 " -m max_wait max number of seconds to wait between events\n"
1202 " -s speed_factor speed up or slow down output\n"
1203 " -V display version information and exit"));
1208 * Cleanup hook for fatal()/fatalx()
1211 sudoreplay_cleanup(void)
1213 term_restore(STDIN_FILENO, 0);
1217 * Signal handler for SIGINT, SIGKILL, SIGTERM, SIGHUP
1218 * Must be installed with SA_RESETHAND enabled.
1221 sudoreplay_handler(int signo)
1223 term_restore(STDIN_FILENO, 0);
1224 kill(getpid(), signo);