]> granicus.if.org Git - sudo/commitdiff
Use getline() if the system has it, else use provide our own for sudoreplay.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 18 Sep 2009 01:16:56 +0000 (01:16 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 18 Sep 2009 01:16:56 +0000 (01:16 +0000)
Makefile.in
config.h.in
configure
configure.in
getline.c [new file with mode: 0644]
sudoreplay.c

index fcee8a509f77a9f8ac3286fd5819be8588409e6b..fd9ed57fdeb2bb9c0e71a831ec7bcf851daba9dd 100644 (file)
@@ -110,8 +110,8 @@ SRCS = aix.c alias.c alloc.c audit.c bsm_audit.c check.c closefrom.c \
        snprintf.c strcasecmp.c strerror.c strlcat.c strlcpy.c sudo.c \
        sudo_noexec.c sudo_edit.c sudo_nss.c term.c testsudoers.c tgetpass.c \
        toke.c toke.l tsgetgrpw.c utimes.c vasgroups.c visudo.c zero_bytes.c \
-       redblack.c selinux.c sesh.c sudoreplay.c getdate.c getdate.y timestr.c \
-       $(AUTH_SRCS)
+       redblack.c selinux.c sesh.c sudoreplay.c getdate.c getdate.y getline.c \
+       timestr.c $(AUTH_SRCS)
 
 AUTH_SRCS = auth/afs.c auth/aix_auth.c auth/bsdauth.c auth/dce.c auth/fwtk.c \
            auth/kerb4.c auth/kerb5.c auth/pam.c auth/passwd.c auth/rfc1938.c \
@@ -139,7 +139,7 @@ SUDO_OBJS = $(COMMON_OBJS) $(AUTH_OBJS) @SUDO_OBJS@ audit.o check.o env.o \
 VISUDO_OBJS = $(COMMON_OBJS) visudo.o fileops.o gettime.o goodpath.o \
              find_path.o pwutil.o
 
-REPLAY_OBJS = sudoreplay.o error.o alloc.o getdate.o timestr.o
+REPLAY_OBJS = alloc.o error.o getdate.o getline.o sudoreplay.o timestr.o
 
 TEST_OBJS = $(COMMON_OBJS) interfaces.o testsudoers.o tsgetgrpw.o tspwutil.o
 
@@ -266,6 +266,8 @@ getcwd.o: $(srcdir)/getcwd.c $(srcdir)/compat.h config.h
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/getcwd.c
 getdate.o: $(srcdir)/getdate.c config.h
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/getdate.c
+getline.o: $(srcdir)/getline.c config.h
+       $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/getline.c
 getprogname.o: $(srcdir)/getprogname.c config.h
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/getprogname.c
 getspwuid.o: $(srcdir)/getspwuid.c $(SUDODEP)
index b0a7f310f3c6fed7363cac9142e1a2d282610f41..97e91aec03e81cc029fed0ff05e14adc01c56142 100644 (file)
 /* Define to 1 if you have the `getifaddrs' function. */
 #undef HAVE_GETIFADDRS
 
+/* Define to 1 if you have the `getline' function. */
+#undef HAVE_GETLINE
+
 /* Define to 1 if you have the `getprogname' function. */
 #undef HAVE_GETPROGNAME
 
index 4176401750e5c32dc4039241d695950be5ab9d04..74ae61b9cf88cc99af514027628779d7d8d8a33a 100755 (executable)
--- a/configure
+++ b/configure
@@ -15829,7 +15829,8 @@ LIBS=$ac_save_LIBS
 
 
 
-for ac_func in dup2 strchr strrchr memchr memcpy memset sysconf tzset \
+
+for ac_func in dup2 strchr strrchr memchr memcpy memset sysconf tzset getline \
               strftime setrlimit initgroups getgroups fstat gettimeofday \
               regcomp setlocale getaddrinfo setsid setenv vhangup nanosleep
 do
index f95d87db91bac6fc3906b7f87a6c74b7328104f3..f75780d6344120dcfdf29fcfcc26ea1c7aca5c45 100644 (file)
@@ -1834,7 +1834,7 @@ dnl
 dnl Function checks
 dnl
 AC_FUNC_GETGROUPS
-AC_CHECK_FUNCS(dup2 strchr strrchr memchr memcpy memset sysconf tzset \
+AC_CHECK_FUNCS(dup2 strchr strrchr memchr memcpy memset sysconf tzset getline \
               strftime setrlimit initgroups getgroups fstat gettimeofday \
               regcomp setlocale getaddrinfo setsid setenv vhangup nanosleep)
 AC_CHECK_FUNCS(openpty, [AC_CHECK_HEADERS(util.h pty.h, [break])], [
diff --git a/getline.c b/getline.c
new file mode 100644 (file)
index 0000000..5a9821e
--- /dev/null
+++ b/getline.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2009 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * 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>
+#ifdef STDC_HEADERS
+# include <stdlib.h>
+# include <stddef.h>
+#else
+# ifdef HAVE_STDLIB_H
+#  include <stdlib.h>
+# endif
+#endif /* STDC_HEADERS */
+#ifdef HAVE_STRING_H
+# include <string.h>
+#else
+# ifdef HAVE_STRINGS_H
+#  include <strings.h>
+# endif
+#endif /* HAVE_STRING_H */
+#include <limits.h>
+
+#ifndef LINE_MAX
+# define LINE_MAX 2048
+#endif
+
+extern void *erealloc __P((void *, size_t));
+
+ssize_t
+getline(bufp, bufsizep, fp)
+    char **bufp;
+    size_t *bufsizep;
+    FILE *fp;
+{
+    char *buf;
+    size_t bufsize;
+    ssize_t len = 0;
+
+    buf = *bufp;
+    bufsize = *bufsizep;
+    if (buf == NULL || bufsize == 0) {
+       bufsize = LINE_MAX;
+       buf = erealloc(buf, LINE_MAX);
+    }
+
+    for (;;) {
+       if (fgets(buf + len, bufsize - len, fp) == NULL) {
+           len = -1;
+           break;
+       }
+       len = strlen(buf);
+       if (!len || buf[len - 1] == '\n' || feof(fp))
+           break;
+       bufsize *= 2;
+       buf = erealloc(buf, bufsize);
+    }
+    *bufp = buf;
+    *bufsizep = bufsize;
+    return(len);
+}
index 9005b36540bde2ab34e4bf15cacd1d5950d7a5af..dc3e3a8b340a5635c66daf8221837b2b4fb017a5 100644 (file)
@@ -151,10 +151,12 @@ extern void *erealloc __P((void *, size_t));
 extern void efree __P((void *));
 extern time_t get_date __P((char *));
 extern char *get_timestr __P((time_t, int));
+#ifndef HAVE_GETLINE
+extern ssize_t getline __P((char **, size_t *, FILE *));
+#endif
 
 static int list_sessions __P((int, char **, const char *, const char *, const char *));
 static int parse_expr __P((struct search_node **, char **));
-static ssize_t sudo_getln __P((char **, size_t *, FILE *));
 static void delay __P((double));
 static void usage __P((void));
 
@@ -167,23 +169,14 @@ static void usage __P((void));
 #define VALID_ID(s) (isalnum((s)[0]) && isalnum((s)[1]) && isalnum((s)[2]) && \
     isalnum((s)[3]) && isalnum((s)[4]) && isalnum((s)[5]) && (s)[6] == '\0')
 
-/*
- * TODO:
- *  add find-like search language?
- *  timestamp option? (begin,end)
- */
-
 int
 main(argc, argv)
     int argc;
     char **argv;
 {
-    int ch, plen;
-    int listonly = 0;
-    char path[PATH_MAX];
-    char buf[BUFSIZ];
-    const char *user = NULL, *id, *pattern = NULL, *tty = NULL;
-    char *cp, *ep;
+    int ch, plen, listonly = 0;
+    const char *id, *user = NULL, *pattern = NULL, *tty = NULL;
+    char path[PATH_MAX], buf[LINE_MAX], *cp, *ep;
     FILE *tfile, *sfile, *lfile;
     double seconds;
     unsigned long nbytes;
@@ -261,9 +254,10 @@ main(argc, argv)
     if (lfile == NULL)
        error(1, "unable to open %s", path);
 
-    sudo_getln(&cp, &len, lfile); /* log */
-    sudo_getln(&cp, &len, lfile); /* cwd */
-    sudo_getln(&cp, &len, lfile); /* command */
+    cp = NULL;
+    getline(&cp, &len, lfile); /* log */
+    getline(&cp, &len, lfile); /* cwd */
+    getline(&cp, &len, lfile); /* command */
     printf("Replaying sudo session: %s", cp);
     free(cp);
     fclose(lfile);
@@ -363,8 +357,7 @@ parse_expr(headp, argv)
     char **argv;
 {
     struct search_node *sn, *newsn;
-    char or = 0, not = 0, type;
-    char **av;
+    char or = 0, not = 0, type, **av;
 
     sn = *headp;
     for (av = argv; *av; av++) {
@@ -499,7 +492,7 @@ match_expr(head, log)
     int matched = 1, rc;
 
     for (sn = head; sn; sn = sn->next) {
-       /* If we have no match, skip up to the next OR entry. */
+       /* If we have no match, skip ahead to the next OR entry. */
        if (!matched && !sn->or)
            continue;
 
@@ -548,45 +541,6 @@ match_expr(head, log)
     return(matched);
 }
 
-static ssize_t
-sudo_getln(bufp, bufsizep, fp)
-    char **bufp;
-    size_t *bufsizep;
-    FILE *fp;
-{
-    char *buf;
-    size_t bufsize;
-    ssize_t len = 0;
-
-    if (*bufp == NULL) {
-       buf = emalloc(BUFSIZ);
-       bufsize = BUFSIZ;
-    } else {
-       buf = *bufp;
-       bufsize = *bufsizep;
-       if (!bufsize) {
-           buf = erealloc(buf, BUFSIZ);
-           bufsize = BUFSIZ;
-       }
-    }
-
-    for (;;) {
-       if (fgets(buf + len, bufsize - len, fp) == NULL) {
-           len = -1;
-           break;
-       }
-       len = strlen(buf);
-       if (!len || buf[len - 1] == '\n' || feof(fp))
-           break;
-       bufsize *= 2;
-       buf = erealloc(buf, bufsize);
-    }
-    *bufp = buf;
-    if (bufsizep)
-       *bufsizep = bufsize;
-    return(len);
-}
-
 static int
 list_session_dir(pathbuf, re, user, tty)
     char *pathbuf;
@@ -599,7 +553,7 @@ list_session_dir(pathbuf, re, user, tty)
     struct dirent *dp;
     char *buf = NULL, *cmd = NULL, *cwd = NULL, idstr[7], *cp;
     struct log_info li;
-    int plen;
+    size_t bufsize = 0, cwdsize = 0, cmdsize = 0, plen;
 
     plen = strlen(pathbuf);
     d = opendir(pathbuf);
@@ -622,22 +576,20 @@ list_session_dir(pathbuf, re, user, tty)
            warning("unable to open %s", pathbuf);
            continue;
        }
+
        /*
         * ID file has three lines:
         *  1) a log info line
         *  2) cwd
         *  3) command with args
         */
-       sudo_getln(&buf, NULL, fp);
-       sudo_getln(&cwd, NULL, fp);
-       sudo_getln(&cmd, NULL, fp);
-       fclose(fp);
-       if (buf == NULL || cwd == NULL || cmd == NULL) {
-           efree(buf);
-           efree(cwd);
-           efree(cmd);
+       if (getline(&buf, &bufsize, fp) == -1 ||
+           getline(&cwd, &cwdsize, fp) == -1 ||
+           getline(&cmd, &cmdsize, fp)) {
+           fclose(fp);
            continue;
        }
+       fclose(fp);
 
        /* crack the log line: timestamp:user:runas_user:runas_group:tty */
        buf[strcspn(buf, "\n")] = '\0';