From cb096341a69b9c6106a4ee1ec551378ea34b3908 Mon Sep 17 00:00:00 2001 From: "Leigh B. Stoller" Date: Tue, 8 Jan 2002 18:30:21 +0000 Subject: [PATCH] Fix up genlastlog to avoid Y2K like problems. Basically, syslog does not include a year in the output, so I was using the current year to init the tm structure. However, the log file could span from the old year to the new year, and so all the dates could be wrong. Using Mac's suggestion, look at the final time stamp, and if its in the future, reset it back one year. Also add [-a #] option to roll through the specified number of rotation files, to make the job of reinitting all the records easier. I ran it as "genlastlog -a 7", which makes it process logins.7.gz through logins.0.gz, before doing the current log. --- security/genlastlog.c | 69 ++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/security/genlastlog.c b/security/genlastlog.c index 54ef6dde8..7126dbc0b 100644 --- a/security/genlastlog.c +++ b/security/genlastlog.c @@ -76,7 +76,7 @@ static int deadfl; static void usage(void) { - fprintf(stderr, "Usage: %s\n", progname); + fprintf(stderr, "Usage: %s [-a]\n", progname); exit(-1); } @@ -93,11 +93,26 @@ main(int argc, char **argv) gzFile *infp; char buf[BUFSIZ], *bp; struct hostent *he; - int errors = 0; - + int ch, errors = 0; + int backcount = 0; + progname = argv[0]; - if (argc != 1) + while ((ch = getopt(argc, argv, "a:")) != -1) + switch(ch) { + case 'a': + backcount = atoi(optarg); + break; + + case 'h': + case '?': + default: + usage(); + } + argc -= optind; + argv += optind; + + if (argc) usage(); openlog("genlastlog", LOG_PID, LOG_USER); @@ -116,24 +131,27 @@ main(int argc, char **argv) if (bp = strchr(opshostname, '.')) *bp = 0; - sprintf(buf, "%s/%s.0.gz", USERSVAR, LOGINS); + while (backcount) { + sprintf(buf, "%s/%s.%d.gz", USERSVAR, LOGINS, backcount); - /* - * Use setjmp and timer to prevent NFS lockup. - */ - if (setjmp(deadline) == 0) { - alarm(15); - - if ((infp = gzopen(buf, "r")) == NULL) { - syslog(LOG_ERR, "Opening %s: %m", buf); - errors++; - } - else { - doit(infp); - gzclose(infp); + /* + * Use setjmp and timer to prevent NFS lockup. + */ + if (setjmp(deadline) == 0) { + alarm(15); + + if ((infp = gzopen(buf, "r")) == NULL) { + syslog(LOG_ERR, "Opening %s: %m", buf); + errors++; + } + else { + doit(infp); + gzclose(infp); + } } + backcount--; + alarm(0); } - alarm(0); sprintf(buf, "%s/%s", USERSVAR, LOGINS); @@ -181,7 +199,7 @@ doit(gzFile *infp) if (skip) { skip = 0; continue; - } + } /* * Thank dog for strptime! Convert the syslog timestamp @@ -194,6 +212,17 @@ doit(gzFile *infp) } ll_time = mktime(&tm); + /* + * If the constructed time is in the future, then we have + * year off by one (cause we are possibly looking at files + * created in the previous year). Set the year back by one, + * and redo. + */ + if (ll_time > curtime) { + tm.tm_year--; + ll_time = mktime(&tm); + } + /* * Scanf the next part, which looks like: * -- GitLab