2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-09-02 07:15:27 +00:00

Fix duplicate free introduced in last rev

This commit is contained in:
Todd C. Miller
2012-04-12 15:17:00 -04:00
parent dfc90ff0b1
commit dffaeb9cb5

View File

@@ -213,7 +213,7 @@ static void help(void) __attribute__((__noreturn__));
static void usage(int); static void usage(int);
static int open_io_fd(char *pathbuf, int len, const char *suffix, union io_fd *fdp); static int open_io_fd(char *pathbuf, int len, const char *suffix, union io_fd *fdp);
static int parse_timing(const char *buf, const char *decimal, int *idx, double *seconds, size_t *nbytes); static int parse_timing(const char *buf, const char *decimal, int *idx, double *seconds, size_t *nbytes);
static int parse_logfile(char *logfile, struct log_info *li); static struct log_info *parse_logfile(char *logfile);
static void free_log_info(struct log_info *li); static void free_log_info(struct log_info *li);
#ifdef HAVE_REGCOMP #ifdef HAVE_REGCOMP
@@ -248,7 +248,7 @@ main(int argc, char *argv[])
sigaction_t sa; sigaction_t sa;
size_t len, nbytes, nread, off; size_t len, nbytes, nread, off;
ssize_t nwritten; ssize_t nwritten;
struct log_info li; struct log_info *li;
debug_decl(main, SUDO_DEBUG_MAIN) debug_decl(main, SUDO_DEBUG_MAIN)
#if defined(SUDO_DEVEL) && defined(__OpenBSD__) #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
@@ -357,21 +357,22 @@ main(int argc, char *argv[])
/* Parse log file. */ /* Parse log file. */
path[plen] = '\0'; path[plen] = '\0';
strlcat(path, "/log", sizeof(path)); strlcat(path, "/log", sizeof(path));
if (parse_logfile(path, &li) != 0) if ((li = parse_logfile(path)) == NULL)
exit(1); exit(1);
printf(_("Replaying sudo session: %s\n"), li.cmd); printf(_("Replaying sudo session: %s\n"), li->cmd);
/* Make sure the terminal is large enough. */ /* Make sure the terminal is large enough. */
get_ttysize(&rows, &cols); get_ttysize(&rows, &cols);
if (li.rows != 0 && li.cols != 0) { if (li->rows != 0 && li->cols != 0) {
if (li.rows > rows) { if (li->rows > rows) {
printf(_("Warning: your terminal is too small to properly replay the log.\n")); printf(_("Warning: your terminal is too small to properly replay the log.\n"));
printf(_("Log geometry is %d x %d, your terminal's geometry is %d x %d."), li.rows, li.cols, rows, cols); printf(_("Log geometry is %d x %d, your terminal's geometry is %d x %d."), li->rows, li->cols, rows, cols);
} }
} }
/* Done with parsed log file. */ /* Done with parsed log file. */
free_log_info(&li); free_log_info(li);
li = NULL;
fflush(stdout); fflush(stdout);
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
@@ -694,19 +695,19 @@ match_expr(struct search_node *head, struct log_info *log)
debug_return_bool(matched); debug_return_bool(matched);
} }
static int static struct log_info *
parse_logfile(char *logfile, struct log_info *li) parse_logfile(char *logfile)
{ {
FILE *fp; FILE *fp;
char *buf = NULL, *cmd = NULL, *cwd = NULL, *cp, *ep; char *buf = NULL, *cp, *ep;
size_t bufsize = 0, cwdsize = 0, cmdsize = 0; size_t bufsize = 0, cwdsize = 0, cmdsize = 0;
int rval = -1; struct log_info *li = NULL;
debug_decl(list_session, SUDO_DEBUG_UTIL) debug_decl(list_session, SUDO_DEBUG_UTIL)
fp = fopen(logfile, "r"); fp = fopen(logfile, "r");
if (fp == NULL) { if (fp == NULL) {
warning(_("unable to open %s"), logfile); warning(_("unable to open %s"), logfile);
goto done; goto bad;
} }
/* /*
@@ -715,19 +716,16 @@ parse_logfile(char *logfile, struct log_info *li)
* 2) cwd * 2) cwd
* 3) command with args * 3) command with args
*/ */
li = ecalloc(1, sizeof(*li));
if (getline(&buf, &bufsize, fp) == -1 || if (getline(&buf, &bufsize, fp) == -1 ||
getline(&cwd, &cwdsize, fp) == -1 || getline(&li->cwd, &cwdsize, fp) == -1 ||
getline(&cmd, &cmdsize, fp) == -1) { getline(&li->cmd, &cmdsize, fp) == -1) {
goto done; goto bad;
} }
memset(li, 0, sizeof(*li));
/* Strip the newline from the cwd and command. */ /* Strip the newline from the cwd and command. */
cwd[strcspn(cwd, "\n")] = '\0'; li->cwd[strcspn(li->cwd, "\n")] = '\0';
li->cwd = cwd; li->cmd[strcspn(li->cmd, "\n")] = '\0';
cmd[strcspn(cmd, "\n")] = '\0';
li->cmd = cmd;
/* /*
* Crack the log line (rows and cols not present in old versions). * Crack the log line (rows and cols not present in old versions).
@@ -737,26 +735,26 @@ parse_logfile(char *logfile, struct log_info *li)
/* timestamp */ /* timestamp */
if ((ep = strchr(buf, ':')) == NULL) if ((ep = strchr(buf, ':')) == NULL)
goto done; goto bad;
if ((li->tstamp = atoi(buf)) == 0) if ((li->tstamp = atoi(buf)) == 0)
goto done; goto bad;
/* user */ /* user */
cp = ep + 1; cp = ep + 1;
if ((ep = strchr(cp, ':')) == NULL) if ((ep = strchr(cp, ':')) == NULL)
goto done; goto bad;
li->user = estrndup(cp, (size_t)(ep - cp)); li->user = estrndup(cp, (size_t)(ep - cp));
/* runas user */ /* runas user */
cp = ep + 1; cp = ep + 1;
if ((ep = strchr(cp, ':')) == NULL) if ((ep = strchr(cp, ':')) == NULL)
goto done; goto bad;
li->runas_user = estrndup(cp, (size_t)(ep - cp)); li->runas_user = estrndup(cp, (size_t)(ep - cp));
/* runas group */ /* runas group */
cp = ep + 1; cp = ep + 1;
if ((ep = strchr(cp, ':')) == NULL) if ((ep = strchr(cp, ':')) == NULL)
goto done; goto bad;
if (cp != ep) if (cp != ep)
li->runas_group = estrndup(cp, (size_t)(ep - cp)); li->runas_group = estrndup(cp, (size_t)(ep - cp));
@@ -773,41 +771,44 @@ parse_logfile(char *logfile, struct log_info *li)
li->cols = atoi(cp); li->cols = atoi(cp);
} }
} }
rval = 0;
done:
fclose(fp); fclose(fp);
efree(buf); efree(buf);
if (rval != 0) debug_return_ptr(li);
free_log_info(li);
debug_return_int(rval); bad:
fclose(fp);
efree(buf);
free_log_info(li);
debug_return_ptr(NULL);
} }
static void static void
free_log_info(struct log_info *li) free_log_info(struct log_info *li)
{ {
efree(li->cwd); if (li != NULL) {
efree(li->user); efree(li->cwd);
efree(li->runas_user); efree(li->user);
efree(li->runas_group); efree(li->runas_user);
efree(li->tty); efree(li->runas_group);
efree(li->cmd); efree(li->tty);
efree(li->cmd);
efree(li);
}
} }
static int static int
list_session(char *logfile, REGEX_T *re, const char *user, const char *tty) list_session(char *logfile, REGEX_T *re, const char *user, const char *tty)
{ {
char idbuf[7], *idstr, *cp; char idbuf[7], *idstr, *cp;
struct log_info li; struct log_info *li;
int rval = -1; int rval = -1;
debug_decl(list_session, SUDO_DEBUG_UTIL) debug_decl(list_session, SUDO_DEBUG_UTIL)
if (parse_logfile(logfile, &li) != 0) if ((li = parse_logfile(logfile)) == NULL)
goto done; goto done;
/* Match on search expression if there is one. */ /* Match on search expression if there is one. */
if (search_expr && !match_expr(search_expr, &li)) if (search_expr && !match_expr(search_expr, li))
goto done; goto done;
/* Convert from /var/log/sudo-sessions/00/00/01/log to 000001 */ /* Convert from /var/log/sudo-sessions/00/00/01/log to 000001 */
@@ -828,15 +829,15 @@ list_session(char *logfile, REGEX_T *re, const char *user, const char *tty)
} }
/* XXX - print rows + cols? */ /* XXX - print rows + cols? */
printf("%s : %s : TTY=%s ; CWD=%s ; USER=%s ; ", printf("%s : %s : TTY=%s ; CWD=%s ; USER=%s ; ",
get_timestr(li.tstamp, 1), li.user, li.tty, li.cwd, li.runas_user); get_timestr(li->tstamp, 1), li->user, li->tty, li->cwd, li->runas_user);
if (li.runas_group) if (li->runas_group)
printf("GROUP=%s ; ", li.runas_group); printf("GROUP=%s ; ", li->runas_group);
printf("TSID=%s ; COMMAND=%s\n", idstr, li.cmd); printf("TSID=%s ; COMMAND=%s\n", idstr, li->cmd);
rval = 0; rval = 0;
done: done:
free_log_info(&li); free_log_info(li);
debug_return_int(rval); debug_return_int(rval);
} }