2 * Copyright (c) 2009-2012 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>
20 #include <sys/param.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
91 # include "compat/stdbool.h"
92 #endif /* HAVE_STDBOOL_H */
94 #include <pathnames.h>
100 #include "sudo_plugin.h"
101 #include "sudo_conf.h"
102 #include "sudo_debug.h"
105 # define LINE_MAX 2048
108 /* Must match the defines in iolog.c */
110 #define IOFD_STDOUT 1
111 #define IOFD_STDERR 2
113 #define IOFD_TTYOUT 4
114 #define IOFD_TIMING 5
117 /* Bitmap of iofds to be replayed */
118 unsigned int replay_filter = (1 << IOFD_STDOUT) | (1 << IOFD_STDERR) |
134 * Info present in the I/O log file
149 * Handle expressions like:
150 * ( user millert or user root ) and tty console and command /bin/sh
153 struct search_node *next;
158 #define ST_RUNASUSER 5
159 #define ST_RUNASGROUP 6
160 #define ST_FROMDATE 7
178 struct search_node *expr;
183 sudo_conv_t sudo_conv; /* NULL in non-plugin */
185 #define STACK_NODE_SIZE 32
186 static struct search_node *node_stack[32];
187 static int stack_top;
189 static const char *session_dir = _PATH_SUDO_IO_LOGDIR;
191 static union io_fd io_fds[IOFD_MAX];
192 static const char *io_fnames[IOFD_MAX] = {
201 extern time_t get_date(char *);
202 extern char *get_timestr(time_t, int);
203 extern int term_raw(int, int);
204 extern int term_restore(int, int);
205 extern void get_ttysize(int *rowp, int *colp);
208 static int list_sessions(int, char **, const char *, const char *, const char *);
209 static int parse_expr(struct search_node **, char **);
210 static void check_input(int, double *);
211 static void delay(double);
212 static void help(void) __attribute__((__noreturn__));
213 static void usage(int);
214 static int open_io_fd(char *pathbuf, int len, const char *suffix, union io_fd *fdp);
215 static int parse_timing(const char *buf, const char *decimal, int *idx, double *seconds, size_t *nbytes);
216 static struct log_info *parse_logfile(char *logfile);
217 static void free_log_info(struct log_info *li);
220 # define REGEX_T regex_t
222 # define REGEX_T char
225 #define VALID_ID(s) (isalnum((unsigned char)(s)[0]) && \
226 isalnum((unsigned char)(s)[1]) && isalnum((unsigned char)(s)[2]) && \
227 isalnum((unsigned char)(s)[3]) && isalnum((unsigned char)(s)[4]) && \
228 isalnum((unsigned char)(s)[5]) && (s)[6] == '\0')
230 #define IS_IDLOG(s) ( \
231 isalnum((unsigned char)(s)[0]) && isalnum((unsigned char)(s)[1]) && \
233 isalnum((unsigned char)(s)[3]) && isalnum((unsigned char)(s)[4]) && \
235 isalnum((unsigned char)(s)[6]) && isalnum((unsigned char)(s)[7]) && \
236 (s)[8] == '/' && (s)[9] == 'l' && (s)[10] == 'o' && (s)[11] == 'g' && \
240 main(int argc, char *argv[])
242 int ch, idx, plen, nready, exitcode = 0, rows = 0, cols = 0;
243 bool interactive = false, listonly = false;
244 const char *id, *user = NULL, *pattern = NULL, *tty = NULL, *decimal = ".";
245 char path[PATH_MAX], buf[LINE_MAX], *cp, *ep;
246 double seconds, to_wait, speed = 1.0, max_wait = 0;
249 size_t len, nbytes, nread, off;
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 #ifdef HAVE_SETLOCALE
266 setlocale(LC_ALL, "");
267 decimal = localeconv()->decimal_point;
269 bindtextdomain("sudoers", LOCALEDIR); /* XXX - should have sudoreplay domain */
270 textdomain("sudoers");
272 /* Read sudo.conf. */
275 while ((ch = getopt(argc, argv, "d:f:hlm:s:V")) != -1) {
278 session_dir = optarg;
281 /* Set the replay filter. */
283 for (cp = strtok(optarg, ","); cp; cp = strtok(NULL, ",")) {
284 if (strcmp(cp, "stdout") == 0)
285 SET(replay_filter, 1 << IOFD_STDOUT);
286 else if (strcmp(cp, "stderr") == 0)
287 SET(replay_filter, 1 << IOFD_STDERR);
288 else if (strcmp(cp, "ttyout") == 0)
289 SET(replay_filter, 1 << IOFD_TTYOUT);
291 errorx(1, _("invalid filter option: %s"), optarg);
302 max_wait = strtod(optarg, &ep);
303 if (*ep != '\0' || errno != 0)
304 errorx(1, _("invalid max wait: %s"), optarg);
308 speed = strtod(optarg, &ep);
309 if (*ep != '\0' || errno != 0)
310 errorx(1, _("invalid speed factor: %s"), optarg);
313 (void) printf(_("%s version %s\n"), getprogname(), PACKAGE_VERSION);
325 exitcode = list_sessions(argc, argv, pattern, user, tty);
332 /* 6 digit ID in base 36, e.g. 01G712AB or free-form name */
335 plen = snprintf(path, sizeof(path), "%s/%.2s/%.2s/%.2s/timing",
336 session_dir, id, &id[2], &id[4]);
337 if (plen <= 0 || plen >= sizeof(path))
338 errorx(1, _("%s/%.2s/%.2s/%.2s/timing: %s"), session_dir,
339 id, &id[2], &id[4], strerror(ENAMETOOLONG));
341 plen = snprintf(path, sizeof(path), "%s/%s/timing",
343 if (plen <= 0 || plen >= sizeof(path))
344 errorx(1, _("%s/%s/timing: %s"), session_dir,
345 id, strerror(ENAMETOOLONG));
349 /* Open files for replay, applying replay filter for the -f flag. */
350 for (idx = 0; idx < IOFD_MAX; idx++) {
351 if (ISSET(replay_filter, 1 << idx) || idx == IOFD_TIMING) {
352 if (open_io_fd(path, plen, io_fnames[idx], &io_fds[idx]) == -1)
353 error(1, _("unable to open %s"), path);
357 /* Parse log file. */
359 strlcat(path, "/log", sizeof(path));
360 if ((li = parse_logfile(path)) == NULL)
362 printf(_("Replaying sudo session: %s\n"), li->cmd);
364 /* Make sure the terminal is large enough. */
365 get_ttysize(&rows, &cols);
366 if (li->rows != 0 && li->cols != 0) {
367 if (li->rows > rows) {
368 printf(_("Warning: your terminal is too small to properly replay the log.\n"));
369 printf(_("Log geometry is %d x %d, your terminal's geometry is %d x %d."), li->rows, li->cols, rows, cols);
373 /* Done with parsed log file. */
378 memset(&sa, 0, sizeof(sa));
379 sigemptyset(&sa.sa_mask);
380 sa.sa_flags = SA_RESETHAND;
381 sa.sa_handler = cleanup;
382 (void) sigaction(SIGINT, &sa, NULL);
383 (void) sigaction(SIGKILL, &sa, NULL);
384 (void) sigaction(SIGTERM, &sa, NULL);
385 (void) sigaction(SIGHUP, &sa, NULL);
386 sa.sa_flags = SA_RESTART;
387 sa.sa_handler = SIG_IGN;
388 (void) sigaction(SIGTSTP, &sa, NULL);
389 (void) sigaction(SIGQUIT, &sa, NULL);
391 /* XXX - read user input from /dev/tty and set STDOUT to raw if not a pipe */
392 /* Set stdin to raw mode if it is a tty */
393 interactive = isatty(STDIN_FILENO);
395 ch = fcntl(STDIN_FILENO, F_GETFL, 0);
397 (void) fcntl(STDIN_FILENO, F_SETFL, ch | O_NONBLOCK);
398 if (!term_raw(STDIN_FILENO, 1))
399 error(1, _("unable to set tty to raw mode"));
401 fdsw = ecalloc(howmany(STDOUT_FILENO + 1, NFDBITS), sizeof(fd_mask));
404 * Timing file consists of line of the format: "%f %d\n"
407 while (gzgets(io_fds[IOFD_TIMING].g, buf, sizeof(buf)) != NULL) {
409 while (fgets(buf, sizeof(buf), io_fds[IOFD_TIMING].f) != NULL) {
411 if (!parse_timing(buf, decimal, &idx, &seconds, &nbytes))
412 errorx(1, _("invalid timing file line: %s"), buf);
415 check_input(STDIN_FILENO, &speed);
417 /* Adjust delay using speed factor and clamp to max_wait */
418 to_wait = seconds / speed;
419 if (max_wait && to_wait > max_wait)
423 /* Even if we are not relaying, we still have to delay. */
424 if (io_fds[idx].v == NULL)
427 /* All output is sent to stdout. */
428 while (nbytes != 0) {
429 if (nbytes > sizeof(buf))
434 nread = gzread(io_fds[idx].g, buf, len);
436 nread = fread(buf, 1, len, io_fds[idx].f);
441 /* no stdio, must be unbuffered */
442 nwritten = write(STDOUT_FILENO, buf + off, nread - off);
443 if (nwritten == -1) {
446 if (errno == EAGAIN) {
447 FD_SET(STDOUT_FILENO, fdsw);
449 nready = select(STDOUT_FILENO + 1, NULL, fdsw, NULL, NULL);
450 } while (nready == -1 && errno == EINTR);
454 error(1, _("writing to standard output"));
457 } while (nread > off);
460 term_restore(STDIN_FILENO, 1);
462 sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode);
469 struct timespec ts, rts;
473 * Typical max resolution is 1/HZ but we can't portably check that.
474 * If the interval is small enough, just ignore it.
480 rts.tv_nsec = (secs - (double) rts.tv_sec) * 1000000000.0;
482 memcpy(&ts, &rts, sizeof(ts));
483 rval = nanosleep(&ts, &rts);
484 } while (rval == -1 && errno == EINTR);
486 error2(1, _("nanosleep: tv_sec %ld, tv_nsec %ld"),
487 (long)ts.tv_sec, (long)ts.tv_nsec);
492 open_io_fd(char *path, int len, const char *suffix, union io_fd *fdp)
495 strlcat(path, suffix, PATH_MAX);
496 debug_decl(open_io_fd, SUDO_DEBUG_UTIL)
499 fdp->g = gzopen(path, "r");
501 fdp->f = fopen(path, "r");
503 debug_return_int(fdp->v ? 0 : -1);
507 * Build expression list from search args
510 parse_expr(struct search_node **headp, char *argv[])
512 struct search_node *sn, *newsn;
513 char or = 0, not = 0, type, **av;
514 debug_decl(parse_expr, SUDO_DEBUG_UTIL)
517 for (av = argv; *av; av++) {
519 case 'a': /* and (ignore) */
520 if (strncmp(*av, "and", strlen(*av)) != 0)
524 if (strncmp(*av, "or", strlen(*av)) != 0)
528 case '!': /* negate */
529 if (av[0][1] != '\0')
533 case 'c': /* command */
534 if (av[0][1] == '\0')
535 errorx(1, _("ambiguous expression \"%s\""), *av);
536 if (strncmp(*av, "cwd", strlen(*av)) == 0)
538 else if (strncmp(*av, "command", strlen(*av)) == 0)
543 case 'f': /* from date */
544 if (strncmp(*av, "fromdate", strlen(*av)) != 0)
548 case 'g': /* runas group */
549 if (strncmp(*av, "group", strlen(*av)) != 0)
551 type = ST_RUNASGROUP;
553 case 'r': /* runas user */
554 if (strncmp(*av, "runas", strlen(*av)) != 0)
558 case 't': /* tty or to date */
559 if (av[0][1] == '\0')
560 errorx(1, _("ambiguous expression \"%s\""), *av);
561 if (strncmp(*av, "todate", strlen(*av)) == 0)
563 else if (strncmp(*av, "tty", strlen(*av)) == 0)
569 if (strncmp(*av, "user", strlen(*av)) != 0)
573 case '(': /* start sub-expression */
574 if (av[0][1] != '\0')
576 if (stack_top + 1 == STACK_NODE_SIZE) {
577 errorx(1, _("too many parenthesized expressions, max %d"),
580 node_stack[stack_top++] = sn;
583 case ')': /* end sub-expression */
584 if (av[0][1] != '\0')
588 errorx(1, _("unmatched ')' in expression"));
589 if (node_stack[stack_top])
590 sn->next = node_stack[stack_top]->next;
591 debug_return_int(av - argv + 1);
594 errorx(1, _("unknown search term \"%s\""), *av);
598 /* Allocate new search node */
599 newsn = ecalloc(1, sizeof(*newsn));
602 newsn->negated = not;
603 /* newsn->next = NULL; */
604 if (type == ST_EXPR) {
605 av += parse_expr(&newsn->u.expr, av + 1);
608 errorx(1, _("%s requires an argument"), av[-1]);
610 if (type == ST_PATTERN) {
611 if (regcomp(&newsn->u.cmdre, *av, REG_EXTENDED|REG_NOSUB) != 0)
612 errorx(1, _("invalid regular expression: %s"), *av);
615 if (type == ST_TODATE || type == ST_FROMDATE) {
616 newsn->u.tstamp = get_date(*av);
617 if (newsn->u.tstamp == -1)
618 errorx(1, _("could not parse date \"%s\""), *av);
623 not = or = 0; /* reset state */
631 errorx(1, _("unmatched '(' in expression"));
633 errorx(1, _("illegal trailing \"or\""));
635 errorx(1, _("illegal trailing \"!\""));
637 debug_return_int(av - argv);
641 match_expr(struct search_node *head, struct log_info *log)
643 struct search_node *sn;
646 debug_decl(match_expr, SUDO_DEBUG_UTIL)
648 for (sn = head; sn; sn = sn->next) {
649 /* If we have no match, skip ahead to the next OR entry. */
650 if (!matched && !sn->or)
655 matched = match_expr(sn->u.expr, log);
658 matched = strcmp(sn->u.cwd, log->cwd) == 0;
661 matched = strcmp(sn->u.tty, log->tty) == 0;
664 matched = strcmp(sn->u.runas_group, log->runas_group) == 0;
667 matched = strcmp(sn->u.runas_user, log->runas_user) == 0;
670 matched = strcmp(sn->u.user, log->user) == 0;
674 rc = regexec(&sn->u.cmdre, log->cmd, 0, NULL, 0);
675 if (rc && rc != REG_NOMATCH) {
677 regerror(rc, &sn->u.cmdre, buf, sizeof(buf));
678 errorx(1, "%s", buf);
680 matched = rc == REG_NOMATCH ? 0 : 1;
682 matched = strstr(log.cmd, sn->u.pattern) != NULL;
686 matched = log->tstamp >= sn->u.tstamp;
689 matched = log->tstamp <= sn->u.tstamp;
695 debug_return_bool(matched);
698 static struct log_info *
699 parse_logfile(char *logfile)
702 char *buf = NULL, *cp, *ep;
703 size_t bufsize = 0, cwdsize = 0, cmdsize = 0;
704 struct log_info *li = NULL;
705 debug_decl(list_session, SUDO_DEBUG_UTIL)
707 fp = fopen(logfile, "r");
709 warning(_("unable to open %s"), logfile);
714 * ID file has three lines:
717 * 3) command with args
719 li = ecalloc(1, sizeof(*li));
720 if (getline(&buf, &bufsize, fp) == -1 ||
721 getline(&li->cwd, &cwdsize, fp) == -1 ||
722 getline(&li->cmd, &cmdsize, fp) == -1) {
726 /* Strip the newline from the cwd and command. */
727 li->cwd[strcspn(li->cwd, "\n")] = '\0';
728 li->cmd[strcspn(li->cmd, "\n")] = '\0';
731 * Crack the log line (rows and cols not present in old versions).
732 * timestamp:user:runas_user:runas_group:tty:rows:cols
734 buf[strcspn(buf, "\n")] = '\0';
737 if ((ep = strchr(buf, ':')) == NULL)
739 if ((li->tstamp = atoi(buf)) == 0)
744 if ((ep = strchr(cp, ':')) == NULL)
746 li->user = estrndup(cp, (size_t)(ep - cp));
750 if ((ep = strchr(cp, ':')) == NULL)
752 li->runas_user = estrndup(cp, (size_t)(ep - cp));
756 if ((ep = strchr(cp, ':')) == NULL)
759 li->runas_group = estrndup(cp, (size_t)(ep - cp));
761 /* tty, followed by optional rows + columns */
763 if ((ep = strchr(cp, ':')) == NULL) {
764 li->tty = estrdup(cp);
766 li->tty = estrndup(cp, (size_t)(ep - cp));
769 if ((ep = strchr(cp, ':')) != NULL) {
776 debug_return_ptr(li);
782 debug_return_ptr(NULL);
786 free_log_info(struct log_info *li)
791 efree(li->runas_user);
792 efree(li->runas_group);
800 list_session(char *logfile, REGEX_T *re, const char *user, const char *tty)
802 char idbuf[7], *idstr, *cp;
805 debug_decl(list_session, SUDO_DEBUG_UTIL)
807 if ((li = parse_logfile(logfile)) == NULL)
810 /* Match on search expression if there is one. */
811 if (search_expr && !match_expr(search_expr, li))
814 /* Convert from /var/log/sudo-sessions/00/00/01/log to 000001 */
815 cp = logfile + strlen(session_dir) + 1;
826 /* Not an id, just use the iolog_file portion. */
827 cp[strlen(cp) - 4] = '\0';
830 /* XXX - print rows + cols? */
831 printf("%s : %s : TTY=%s ; CWD=%s ; USER=%s ; ",
832 get_timestr(li->tstamp, 1), li->user, li->tty, li->cwd, li->runas_user);
834 printf("GROUP=%s ; ", li->runas_group);
835 printf("TSID=%s ; COMMAND=%s\n", idstr, li->cmd);
841 debug_return_int(rval);
845 session_compare(const void *v1, const void *v2)
847 const char *s1 = *(const char **)v1;
848 const char *s2 = *(const char **)v2;
849 return strcmp(s1, s2);
852 /* XXX - always returns 0, calls error() on failure */
854 find_sessions(const char *dir, REGEX_T *re, const char *user, const char *tty)
859 size_t sdlen, sessions_len = 0, sessions_size = 36*36;
861 char pathbuf[PATH_MAX], **sessions = NULL;
862 debug_decl(find_sessions, SUDO_DEBUG_UTIL)
866 error(1, _("unable to open %s"), dir);
868 /* XXX - would be faster to chdir and use relative names */
869 sdlen = strlcpy(pathbuf, dir, sizeof(pathbuf));
870 if (sdlen + 1 >= sizeof(pathbuf)) {
871 errno = ENAMETOOLONG;
872 error(1, "%s/", dir);
874 pathbuf[sdlen++] = '/';
875 pathbuf[sdlen] = '\0';
877 /* Store potential session dirs for sorting. */
878 sessions = emalloc2(sessions_size, sizeof(char *));
879 while ((dp = readdir(d)) != NULL) {
880 /* Skip "." and ".." */
881 if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
882 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
884 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
885 if (dp->d_type != DT_DIR)
889 /* Add name to session list. */
890 if (sessions_len + 1 > sessions_size) {
892 sessions = erealloc3(sessions, sessions_size, sizeof(char *));
894 sessions[sessions_len++] = estrdup(dp->d_name);
898 /* Sort and list the sessions. */
899 qsort(sessions, sessions_len, sizeof(char *), session_compare);
900 for (i = 0; i < sessions_len; i++) {
901 len = snprintf(&pathbuf[sdlen], sizeof(pathbuf) - sdlen,
902 "%s/log", sessions[i]);
903 if (len <= 0 || len >= sizeof(pathbuf) - sdlen) {
904 errno = ENAMETOOLONG;
905 error(1, "%s/%s/log", dir, sessions[i]);
909 /* Check for dir with a log file. */
910 if (lstat(pathbuf, &sb) == 0 && S_ISREG(sb.st_mode)) {
911 list_session(pathbuf, re, user, tty);
913 /* Strip off "/log" and recurse if a dir. */
914 pathbuf[sdlen + len - 4] = '\0';
915 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
916 if (lstat(pathbuf, &sb) == 0 && S_ISDIR(sb.st_mode))
918 find_sessions(pathbuf, re, user, tty);
926 /* XXX - always returns 0, calls error() on failure */
928 list_sessions(int argc, char **argv, const char *pattern, const char *user,
931 REGEX_T rebuf, *re = NULL;
932 debug_decl(list_sessions, SUDO_DEBUG_UTIL)
934 /* Parse search expression if present */
935 parse_expr(&search_expr, argv);
941 if (regcomp(re, pattern, REG_EXTENDED|REG_NOSUB) != 0)
942 errorx(1, _("invalid regex: %s"), pattern);
945 re = (char *) pattern;
946 #endif /* HAVE_REGCOMP */
948 debug_return_int(find_sessions(session_dir, re, user, tty));
952 * Check input for ' ', '<', '>'
956 check_input(int ttyfd, double *speed)
959 int nready, paused = 0;
963 debug_decl(check_input, SUDO_DEBUG_UTIL)
965 fdsr = ecalloc(howmany(ttyfd + 1, NFDBITS), sizeof(fd_mask));
971 nready = select(ttyfd + 1, fdsr, NULL, NULL, paused ? NULL : &tv);
974 n = read(ttyfd, &ch, 1);
998 * Parse a timing line, which is formatted as:
999 * index sleep_time num_bytes
1000 * Where index is IOFD_*, sleep_time is the number of seconds to sleep
1001 * before writing the data and num_bytes is the number of bytes to output.
1002 * Returns 1 on success and 0 on failure.
1005 parse_timing(buf, decimal, idx, seconds, nbytes)
1007 const char *decimal;
1014 double d, fract = 0;
1016 debug_decl(parse_timing, SUDO_DEBUG_UTIL)
1019 ul = strtoul(buf, &ep, 10);
1023 for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
1027 * Parse number of seconds. Sudo logs timing data in the C locale
1028 * but this may not match the current locale so we cannot use strtod().
1029 * Furthermore, sudo < 1.7.4 logged with the user's locale so we need
1030 * to be able to parse those logs too.
1033 l = strtol(cp, &ep, 10);
1034 if ((errno == ERANGE && (l == LONG_MAX || l == LONG_MIN)) ||
1035 l < 0 || l > INT_MAX ||
1036 (*ep != '.' && strncmp(ep, decimal, strlen(decimal)) != 0)) {
1039 *seconds = (double)l;
1040 cp = ep + (*ep == '.' ? 1 : strlen(decimal));
1042 while (isdigit((unsigned char) *cp)) {
1043 fract += (*cp - '0') / d;
1048 while (isspace((unsigned char) *cp))
1052 ul = strtoul(cp, &ep, 10);
1053 if (errno == ERANGE && ul == ULONG_MAX)
1055 *nbytes = (size_t)ul;
1057 debug_return_int(1);
1059 debug_return_int(0);
1065 fprintf(fatal ? stderr : stdout,
1066 _("usage: %s [-h] [-d directory] [-m max_wait] [-s speed_factor] ID\n"),
1068 fprintf(fatal ? stderr : stdout,
1069 _("usage: %s [-h] [-d directory] -l [search expression]\n"),
1078 (void) printf(_("%s - replay sudo session logs\n\n"), getprogname());
1080 (void) puts(_("\nOptions:\n"
1081 " -d directory specify directory for session logs\n"
1082 " -f filter specify which I/O type to display\n"
1083 " -h display help message and exit\n"
1084 " -l [expression] list available session IDs that match expression\n"
1085 " -m max_wait max number of seconds to wait between events\n"
1086 " -s speed_factor speed up or slow down output\n"
1087 " -V display version information and exit"));
1092 * Cleanup hook for error()/errorx()
1097 term_restore(STDIN_FILENO, 0);
1099 kill(getpid(), signo);