Move get_timestr() into its own source file so sudoreplay can use it.

This commit is contained in:
Todd C. Miller
2009-09-16 11:48:34 +00:00
parent 789d716017
commit 28b3a18137
4 changed files with 88 additions and 46 deletions

View File

@@ -110,7 +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 \ 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 \ 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 \ 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 $(AUTH_SRCS) redblack.c selinux.c sesh.c sudoreplay.c getdate.c getdate.y timestr.c \
$(AUTH_SRCS)
AUTH_SRCS = auth/afs.c auth/aix_auth.c auth/bsdauth.c auth/dce.c auth/fwtk.c \ 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 \ auth/kerb4.c auth/kerb5.c auth/pam.c auth/passwd.c auth/rfc1938.c \
@@ -132,12 +133,13 @@ COMMON_OBJS = gram.o alias.o alloc.o defaults.o error.o list.o match.o \
SUDO_OBJS = $(COMMON_OBJS) $(AUTH_OBJS) @SUDO_OBJS@ audit.o check.o env.o \ SUDO_OBJS = $(COMMON_OBJS) $(AUTH_OBJS) @SUDO_OBJS@ audit.o check.o env.o \
getspwuid.o gettime.o goodpath.o fileops.o find_path.o \ getspwuid.o gettime.o goodpath.o fileops.o find_path.o \
interfaces.o lbuf.o logging.o parse.o pwutil.o script.o \ interfaces.o lbuf.o logging.o parse.o pwutil.o script.o \
set_perms.o sudo.o sudo_edit.o sudo_nss.o term.o tgetpass.o set_perms.o sudo.o sudo_edit.o sudo_nss.o term.o tgetpass.o \
timestr.o
VISUDO_OBJS = $(COMMON_OBJS) visudo.o fileops.o gettime.o goodpath.o \ VISUDO_OBJS = $(COMMON_OBJS) visudo.o fileops.o gettime.o goodpath.o \
find_path.o pwutil.o find_path.o pwutil.o
REPLAY_OBJS = sudoreplay.o error.o alloc.o getdate.o REPLAY_OBJS = sudoreplay.o error.o alloc.o getdate.o timestr.o
TEST_OBJS = $(COMMON_OBJS) interfaces.o testsudoers.o tsgetgrpw.o tspwutil.o TEST_OBJS = $(COMMON_OBJS) interfaces.o testsudoers.o tsgetgrpw.o tspwutil.o
@@ -334,6 +336,8 @@ testsudoers.o: $(srcdir)/testsudoers.c $(SUDODEP) $(srcdir)/parse.h $(srcdir)/li
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/testsudoers.c $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/testsudoers.c
tgetpass.o: $(srcdir)/tgetpass.c $(SUDODEP) tgetpass.o: $(srcdir)/tgetpass.c $(SUDODEP)
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/tgetpass.c $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/tgetpass.c
timestr.o: $(srcdir)/timestr.c $(srcdir)/compat.h config.h
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/timestr.c
toke.o: $(devdir)/toke.c $(SUDODEP) $(srcdir)/parse.h $(srcdir)/list.h $(devdir)/gram.h toke.o: $(devdir)/toke.c $(SUDODEP) $(srcdir)/parse.h $(srcdir)/list.h $(devdir)/gram.h
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(devdir)/toke.c $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(devdir)/toke.c
tsgetgrpw.o: $(srcdir)/tsgetgrpw.c $(SUDODEP) tsgetgrpw.o: $(srcdir)/tsgetgrpw.c $(SUDODEP)

View File

@@ -65,7 +65,6 @@ static void do_syslog __P((int, char *));
static void do_logfile __P((char *)); static void do_logfile __P((char *));
static void send_mail __P((char *)); static void send_mail __P((char *));
static int should_mail __P((int)); static int should_mail __P((int));
static char *get_timestr __P((void));
static void mysyslog __P((int, const char *, ...)); static void mysyslog __P((int, const char *, ...));
static char *new_logline __P((const char *, int)); static char *new_logline __P((const char *, int));
@@ -201,17 +200,17 @@ do_logfile(msg)
if (def_loglinelen == 0) { if (def_loglinelen == 0) {
/* Don't pretty-print long log file lines (hard to grep) */ /* Don't pretty-print long log file lines (hard to grep) */
if (def_log_host) if (def_log_host)
(void) fprintf(fp, "%s : %s : HOST=%s : %s\n", get_timestr(), (void) fprintf(fp, "%s : %s : HOST=%s : %s\n",
user_name, user_shost, msg); get_timestr(def_log_year), user_name, user_shost, msg);
else else
(void) fprintf(fp, "%s : %s : %s\n", get_timestr(), (void) fprintf(fp, "%s : %s : %s\n", get_timestr(def_log_year),
user_name, msg); user_name, msg);
} else { } else {
if (def_log_host) if (def_log_host)
easprintf(&full_line, "%s : %s : HOST=%s : %s", get_timestr(), easprintf(&full_line, "%s : %s : HOST=%s : %s",
user_name, user_shost, msg); get_timestr(def_log_year), user_name, user_shost, msg);
else else
easprintf(&full_line, "%s : %s : %s", get_timestr(), easprintf(&full_line, "%s : %s : %s", get_timestr(def_log_year),
user_name, msg); user_name, msg);
/* /*
@@ -591,7 +590,7 @@ send_mail(line)
(void) fputc(*p, mail); (void) fputc(*p, mail);
} }
(void) fprintf(mail, "\n\n%s : %s : %s : %s\n\n", user_host, (void) fprintf(mail, "\n\n%s : %s : %s : %s\n\n", user_host,
get_timestr(), user_name, line); get_timestr(def_log_year), user_name, line);
fclose(mail); fclose(mail);
do { do {
#ifdef HAVE_WAITPID #ifdef HAVE_WAITPID
@@ -617,41 +616,6 @@ should_mail(status)
(def_mail_no_perms && !ISSET(status, VALIDATE_OK))); (def_mail_no_perms && !ISSET(status, VALIDATE_OK)));
} }
/*
* Return an ascii string with the current date + time
* Uses strftime() if available, else falls back to ctime().
*/
static char *
get_timestr()
{
char *s;
time_t now = time((time_t) 0);
#ifdef HAVE_STRFTIME
static char buf[128];
struct tm *timeptr;
timeptr = localtime(&now);
if (def_log_year)
s = "%h %e %T %Y";
else
s = "%h %e %T";
/* strftime() does not guarantee to NUL-terminate so we must check. */
buf[sizeof(buf) - 1] = '\0';
if (strftime(buf, sizeof(buf), s, timeptr) && buf[sizeof(buf) - 1] == '\0')
return(buf);
#endif /* HAVE_STRFTIME */
s = ctime(&now) + 4; /* skip day of the week */
if (def_log_year)
s[20] = '\0'; /* avoid the newline */
else
s[15] = '\0'; /* don't care about year */
return(s);
}
#define LL_TTY_STR "TTY=" #define LL_TTY_STR "TTY="
#define LL_CWD_STR "PWD=" /* XXX - should be CWD= */ #define LL_CWD_STR "PWD=" /* XXX - should be CWD= */
#define LL_USER_STR "USER=" #define LL_USER_STR "USER="

1
sudo.h
View File

@@ -334,6 +334,7 @@ int term_copy __P((int, int));
int term_noecho __P((int)); int term_noecho __P((int));
int term_raw __P((int)); int term_raw __P((int));
int term_restore __P((int)); int term_restore __P((int));
char *get_timestr __P((int));
YY_DECL; YY_DECL;
/* Only provide extern declarations outside of sudo.c. */ /* Only provide extern declarations outside of sudo.c. */

73
timestr.c Normal file
View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 1999, 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 */
#include <time.h>
#include "compat.h"
#ifndef lint
__unused static const char rcsid[] = "$Sudo$";
#endif /* lint */
char *get_timestr __P((int));
/*
* Return an ascii string with the current date + time
* Uses strftime() if available, else falls back to ctime().
*/
char *
get_timestr(log_year)
int log_year;
{
char *s;
time_t now = time((time_t) 0);
#ifdef HAVE_STRFTIME
static char buf[128];
struct tm *timeptr;
timeptr = localtime(&now);
if (log_year)
s = "%h %e %T %Y";
else
s = "%h %e %T";
/* strftime() does not guarantee to NUL-terminate so we must check. */
buf[sizeof(buf) - 1] = '\0';
if (strftime(buf, sizeof(buf), s, timeptr) && buf[sizeof(buf) - 1] == '\0')
return(buf);
#endif /* HAVE_STRFTIME */
s = ctime(&now) + 4; /* skip day of the week */
if (log_year)
s[20] = '\0'; /* avoid the newline */
else
s[15] = '\0'; /* don't care about year */
return(s);
}