--- embedaddon/sudo/plugins/sudoers/sudoreplay.c 2012/05/29 12:26:49 1.1.1.2 +++ embedaddon/sudo/plugins/sudoers/sudoreplay.c 2013/07/22 10:46:12 1.1.1.4 @@ -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,7 @@ #include #include -#include +#include #ifdef HAVE_SYS_SYSMACROS_H # include #endif @@ -81,9 +81,6 @@ #ifdef HAVE_ZLIB_H # include #endif -#ifdef HAVE_SETLOCALE -# include -#endif #include #ifdef HAVE_STDBOOL_H # include @@ -97,6 +94,7 @@ #include "alloc.h" #include "error.h" #include "gettext.h" +#include "logging.h" #include "sudo_plugin.h" #include "sudo_conf.h" #include "sudo_debug.h" @@ -180,8 +178,6 @@ 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; @@ -203,7 +199,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 **); @@ -215,6 +210,9 @@ static int open_io_fd(char *pathbuf, int len, const ch 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 @@ -236,19 +234,21 @@ static void free_log_info(struct log_info *li); (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, nready, exitcode = 0, rows = 0, cols = 0; - bool interactive = false, listonly = false; - const char *id, *user = NULL, *pattern = NULL, *tty = NULL, *decimal = "."; + int ch, idx, plen, exitcode = 0, rows = 0, cols = 0; + bool interactive = false, listonly = false, need_nlcr = false; + 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; - fd_set *fdsw; sigaction_t sa; - size_t len, nbytes, nread, off; - ssize_t nwritten; + size_t len, nbytes, nread; struct log_info *li; + struct iovec *iov = NULL; + int iovcnt = 0, iovmax = 0; debug_decl(main, SUDO_DEBUG_MAIN) #if defined(SUDO_DEVEL) && defined(__OpenBSD__) @@ -262,15 +262,16 @@ 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) { @@ -288,7 +289,7 @@ main(int argc, char *argv[]) else if (strcmp(cp, "ttyout") == 0) SET(replay_filter, 1 << IOFD_TTYOUT); else - errorx(1, _("invalid filter option: %s"), optarg); + fatalx(_("invalid filter option: %s"), optarg); } break; case 'h': @@ -301,13 +302,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); @@ -335,13 +336,13 @@ main(int argc, char *argv[]) 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; @@ -350,7 +351,7 @@ main(int argc, char *argv[]) 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); + fatal(_("unable to open %s"), path); } } @@ -378,7 +379,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); @@ -396,9 +397,10 @@ 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")); + fatal(_("unable to set tty to raw mode")); + iovmax = 32; + iov = ecalloc(iovmax, sizeof(*iov)); } - fdsw = ecalloc(howmany(STDOUT_FILENO + 1, NFDBITS), sizeof(fd_mask)); /* * Timing file consists of line of the format: "%f %d\n" @@ -408,8 +410,10 @@ main(int argc, char *argv[]) #else while (fgets(buf, sizeof(buf), io_fds[IOFD_TIMING].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); @@ -424,6 +428,10 @@ main(int argc, char *argv[]) if (io_fds[idx].v == NULL) continue; + /* Check whether we need to convert newline to CR LF pairs. */ + if (interactive) + need_nlcr = (idx == IOFD_STDOUT || idx == IOFD_STDERR); + /* All output is sent to stdout. */ while (nbytes != 0) { if (nbytes > sizeof(buf)) @@ -436,25 +444,59 @@ main(int argc, char *argv[]) nread = fread(buf, 1, len, io_fds[idx].f); #endif nbytes -= nread; - off = 0; - do { - /* no stdio, must be unbuffered */ - nwritten = write(STDOUT_FILENO, buf + off, nread - off); - if (nwritten == -1) { - if (errno == EINTR) - continue; - if (errno == EAGAIN) { - FD_SET(STDOUT_FILENO, fdsw); - do { - nready = select(STDOUT_FILENO + 1, NULL, fdsw, NULL, NULL); - } while (nready == -1 && errno == EINTR); - if (nready == 1) - continue; + + /* Convert newline to carriage return + linefeed if needed. */ + if (need_nlcr) { + size_t remainder = nread; + size_t linelen; + iovcnt = 0; + cp = buf; + ep = cp - 1; + /* Handle a "\r\n" pair that spans a buffer. */ + if (last_char == '\r' && buf[0] == '\n') { + ep++; + remainder--; + } + while ((ep = memchr(ep + 1, '\n', remainder)) != NULL) { + /* Is there already a carriage return? */ + if (cp != ep && ep[-1] == '\r') { + remainder = (size_t)(&buf[nread - 1] - ep); + continue; } - error(1, _("writing to standard output")); + + /* Store the line in iov followed by \r\n pair. */ + if (iovcnt + 3 > iovmax) { + iovmax <<= 1; + iov = erealloc3(iov, iovmax, sizeof(*iov)); + } + linelen = (size_t)(ep - cp) + 1; + iov[iovcnt].iov_base = cp; + iov[iovcnt].iov_len = linelen - 1; /* not including \n */ + iovcnt++; + iov[iovcnt].iov_base = "\r\n"; + iov[iovcnt].iov_len = 2; + iovcnt++; + cp = ep + 1; + remainder -= linelen; } - off += nwritten; - } while (nread > off); + if (cp - buf != nread) { + /* + * Partial line without a linefeed or multiple lines + * with \r\n pairs. + */ + iov[iovcnt].iov_base = cp; + iov[iovcnt].iov_len = nread - (cp - buf); + iovcnt++; + } + last_char = buf[nread - 1]; /* stash last char of old buffer */ + } else { + /* No conversion needed. */ + iov[0].iov_base = buf; + iov[0].iov_len = nread; + iovcnt = 1; + } + if (atomic_writev(STDOUT_FILENO, iov, iovcnt) == -1) + fatal(_("writing to standard output")); } } term_restore(STDIN_FILENO, 1); @@ -483,17 +525,18 @@ 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) { + debug_decl(open_io_fd, SUDO_DEBUG_UTIL) + path[len] = '\0'; strlcat(path, suffix, PATH_MAX); - debug_decl(open_io_fd, SUDO_DEBUG_UTIL) #ifdef HAVE_ZLIB_H fdp->g = gzopen(path, "r"); @@ -504,6 +547,69 @@ open_io_fd(char *path, int len, const char *suffix, un } /* + * Call writev(), restarting as needed and handling EAGAIN since + * fd may be in non-blocking mode. + */ +static size_t +atomic_writev(int fd, struct iovec *iov, int iovcnt) +{ + ssize_t n, nwritten = 0; + size_t count, remainder, nbytes = 0; + int i; + debug_decl(atomic_writev, SUDO_DEBUG_UTIL) + + for (i = 0; i < iovcnt; i++) + nbytes += iov[i].iov_len; + + for (;;) { + n = writev(STDOUT_FILENO, iov, iovcnt); + if (n > 0) { + nwritten += n; + remainder = nbytes - nwritten; + if (remainder == 0) + break; + /* short writev, adjust iov and do the rest. */ + count = 0; + i = iovcnt; + while (i--) { + count += iov[i].iov_len; + if (count == remainder) { + iov += i; + iovcnt -= i; + break; + } + if (count > remainder) { + size_t off = (count - remainder); + /* XXX - side effect prevents iov from being const */ + iov[i].iov_base = (char *)iov[i].iov_base + off; + iov[i].iov_len -= off; + iov += i; + iovcnt -= i; + break; + } + } + continue; + } + if (n == 0 || errno == EAGAIN) { + int nready; + fd_set fdsw; + FD_ZERO(&fdsw); + FD_SET(STDOUT_FILENO, &fdsw); + do { + nready = select(STDOUT_FILENO + 1, NULL, &fdsw, NULL, NULL); + } while (nready == -1 && errno == EINTR); + if (nready == 1) + continue; + } + if (errno == EINTR) + continue; + nwritten = -1; + break; + } + debug_return_size_t(nwritten); +} + +/* * Build expression list from search args */ static int @@ -532,7 +638,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) @@ -557,7 +663,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) @@ -574,7 +680,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; @@ -585,13 +691,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 */ } @@ -605,17 +711,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; } @@ -628,11 +734,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); } @@ -675,7 +781,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 @@ -776,7 +882,8 @@ parse_logfile(char *logfile) debug_return_ptr(li); bad: - fclose(fp); + if (fp != NULL) + fclose(fp); efree(buf); free_log_info(li); debug_return_ptr(NULL); @@ -849,7 +956,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) { @@ -859,17 +966,22 @@ find_sessions(const char *dir, REGEX_T *re, const char size_t sdlen, sessions_len = 0, sessions_size = 36*36; int i, len; char pathbuf[PATH_MAX], **sessions = NULL; +#ifdef HAVE_STRUCT_DIRENT_D_TYPE + bool checked_type = true; +#else + const bool checked_type = false; +#endif debug_decl(find_sessions, SUDO_DEBUG_UTIL) 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'; @@ -882,8 +994,14 @@ find_sessions(const char *dir, REGEX_T *re, const char (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) continue; #ifdef HAVE_STRUCT_DIRENT_D_TYPE - if (dp->d_type != DT_DIR) - continue; + if (checked_type) { + if (dp->d_type != DT_DIR) { + /* Not all file systems support d_type. */ + if (dp->d_type != DT_UNKNOWN) + continue; + checked_type = false; + } + } #endif /* Add name to session list. */ @@ -902,7 +1020,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]); @@ -912,9 +1030,7 @@ find_sessions(const char *dir, REGEX_T *re, const char } else { /* Strip off "/log" and recurse if a dir. */ pathbuf[sdlen + len - 4] = '\0'; -#ifndef HAVE_STRUCT_DIRENT_D_TYPE - if (lstat(pathbuf, &sb) == 0 && S_ISDIR(sb.st_mode)) -#endif + if (checked_type || (lstat(pathbuf, &sb) == 0 && S_ISDIR(sb.st_mode))) find_sessions(pathbuf, re, user, tty); } } @@ -923,7 +1039,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) @@ -939,7 +1055,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; @@ -1089,12 +1205,21 @@ help(void) } /* - * 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); }