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 \
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
$(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)
--- /dev/null
+/*
+ * 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);
+}
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));
#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;
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);
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++) {
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;
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;
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);
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';