SUMMARY: wtmp and wtmpx purging

MARK SAYER (MSAYER@cuscal.com.au)
Tue, 27 Jan 1998 11:42:00 +1100

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--Boundary_(ID_ytqOWrayUSitDk9IrDvmtA)
Content-type: MULTIPART/ALTERNATIVE;
BOUNDARY="Boundary_(ID_MK4PmUg/OfDWIIcy0wk2xQ)"

--Boundary_(ID_MK4PmUg/OfDWIIcy0wk2xQ)
Content-type: text/plain; CHARSET=US-ASCII

Okay - Thanks to all who responded - as usual there was many and varied
ideas. The most popular method seemed to be cat /dev/null >
/var/adm/wtmp etc.... Of course this blows all the data stored within
these files away.

A number of people pointed out modifying newsyslog....but that proved to
be too much effort...

The solution I am now using: 2 programs, wtmp.c and wtmpx.c, which are
executed once a week and purge up until 28 days prior to execution date.
This is exactly what I was after!! YAY!

A number of other people expressed interest in a solution (if I found
one) so I have included the files here:

Thanks again to all who responded - there are far to many of you to
mention here...I owe a LOT of people a LOT of beer....hehe

Mark.


--Boundary_(ID_MK4PmUg/OfDWIIcy0wk2xQ)
Content-type: text/html
Content-transfer-encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">

Okay - Thanks to all who responded - as usual there = was many and varied ideas. The most popular method seemed to be cat = /dev/null > /var/adm/wtmp etc.... Of course this blows all the data = stored within these files away.


A number of people pointed out modifying = newsyslog....but that proved to be too much effort...

The solution I am now using: 2 programs, wtmp.c and = wtmpx.c, which are executed once a week and purge up until 28 days = prior to execution date. This is exactly what I was after!! = YAY!


A number of other people expressed interest in a = solution (if I found one) so I have included the files here:

Thanks again to all who responded - there are far to = many of you to mention here...I owe a LOT of people a LOT of = beer....hehe


Mark.

 

--Boundary_(ID_MK4PmUg/OfDWIIcy0wk2xQ)--

--Boundary_(ID_ytqOWrayUSitDk9IrDvmtA)
Content-type: application/octet-stream; name=WTMP.C
Content-disposition: attachment; filename=WTMP.C

/*-----------------------------------------------------------------------
* Description: Source of the program . /equipe/sysadmin/bin/wtmpcut
* which permits the reduction of the file /var/adm/wtmp
* by removing entries older than the date specified.
* The number of days prior to today is the only
* pararamter. If no paramter is specified, a default
* of DELAY is used.
*----------------------------------------------------------------------*/

#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <utmp.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>

#define ORIGFILE "/var/adm/wtmp"
#define TEMPFILE "/export/local/sbin/wtmp_temp"
#define DELFILE "/export/local/sbin/wtmp_del"

#define DELAY (time_t) (45) /* in days */

int main(argc, argv)
int argc;
char **argv;
{
struct utmp wtmp_rec;
int old_fd, new_fd, del_fd;
int wtmp_rec_sz = sizeof(struct utmp);
time_t periode, temps_limite;
char garbage [80];



if (argc > 1)
periode = (time_t) atoi(argv[1]);
else
periode = DELAY;

/*
* Copy /var/adm/wtmp to /tmp/wtmp
*/
putenv("IFS=' \t\n'"); /* Security */
putenv("PATH=/usr/bin"); /* Security */
sprintf (garbage, "cp %s %s", ORIGFILE, TEMPFILE);
if (system(garbage) < 0) {
fprintf(stderr,"%s: Error in copying %s into %s.\nBye.\n",argv[0],
ORIGFILE, TEMPFILE);
exit(1);
} /* if */

/*
* Calculate the time limit in time_t
*/
temps_limite = time(NULL) - (periode * 24 * 3600);
fprintf(stdout,
"\n%s: The Date Limits is %s\n", argv[0], asctime(localtime(&temps_limite)));

/*
* Open the new file for writing
*/
if ((new_fd = open(ORIGFILE, O_WRONLY | O_TRUNC)) < 0) {
fprintf(stderr, "%s: Error %d in opening %s .\nBye.\n",
argv[0], errno, ORIGFILE);
exit(1);
} /* if */

/*
* Open the new del file for writing
*/
if ((del_fd = open(DELFILE, O_WRONLY | O_TRUNC | O_CREAT )) < 0) {
fprintf(stderr, "%s: Error %d in opening %s .\nBye.\n",
argv[0], errno, DELFILE);
exit(1);
} /* if */

/*
* Open the copy of wtmp for reading
*/
if ((old_fd = open(TEMPFILE, O_RDONLY)) < 0) {
fprintf(stderr, "%s: Error %d in opening %s.\nBye.\n",
argv[0], errno, TEMPFILE);
exit(1);
} /* if */

/*
* Until the end of file, compare dates, if older, Put them in del file
* When they are in the period, put them in /var/adm/wtmp.
*/
while (read(old_fd, &wtmp_rec, wtmp_rec_sz) == wtmp_rec_sz) {
if (wtmp_rec.ut_time >= temps_limite) {
if (write(new_fd, &wtmp_rec, wtmp_rec_sz) != wtmp_rec_sz) {
fprintf(stderr, "%s: Error %d n writing the record.\nBye.",
argv[0], errno);
exit(1);
} /* if */
} /* if */
else /* Del File Written */
{
if (write(del_fd, &wtmp_rec, wtmp_rec_sz) != wtmp_rec_sz) {
fprintf(stderr, "%s: Error %d n writing the record.\nBye.",
argv[0], errno);
exit(1);
} /* if */
}
} /* while */
unlink ( TEMPFILE );
exit(0); /* Nice Exit */

} /* main() */

--Boundary_(ID_ytqOWrayUSitDk9IrDvmtA)
Content-type: application/octet-stream; name=WTMPX.C
Content-disposition: attachment; filename=WTMPX.C

/*----------------------------------------------------------------------*
* *
* Description: Source du fichier exec. /equipe/sysadmin/bin/wtmpxcut *
* qui permet de reduire le fichier /var/adm/wtmpx en enle-*
* vant toutes les entrees plus vieilles qu'une certaine *
* date specifiee en parametre. Si aucun parametre n'est *
* specifie, on utilise une valeur par defaut (DELAIS). *
* *
*----------------------------------------------------------------------*/

#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <utmpx.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>

#define ORIGFILE "/var/adm/wtmpx"
#define TEMPFILE "/export/local/sbin/wtmpx_temp"
#define DELFILE "/export/local/sbin/wtmpx_del"

#define DELAIS (time_t) (45) /* in days */

int main(argc, argv)
int argc;
char **argv;
{
struct utmpx wtmpx_rec;
int old_fd, new_fd, del_fd;
int wtmpx_rec_sz = sizeof(wtmpx_rec);
time_t periode, temps_limite;
char garbage[80];

if (argc > 1)
periode = (time_t) atoi(argv[1]);
else
periode = DELAIS;

/*
* Copie de /var/adm/wtmpx into /tmp/wtmpx
*/
putenv("IFS=' \t\n'"); /* Securite */
putenv("PATH=/usr/bin"); /* Securite */
sprintf(garbage,"cp %s %s", ORIGFILE, TEMPFILE);
if (system(garbage) < 0) {
fprintf(stderr,"%s: Error in copying %s into %s.\nBye.\n",argv[0],
ORIGFILE, TEMPFILE);
exit(1);
} /* if */

/*
* Calcule du temps limite en time_t
*/
temps_limite = time(NULL) - (periode * 24 * 3600);
fprintf(stdout,
"\n%s: The date limit is %s\n", argv[0], asctime(localtime(&temps_limite)));

/*
* Open the new file for writing
*/
if ((new_fd = open(ORIGFILE, O_WRONLY | O_TRUNC)) < 0) {
fprintf(stderr, "%s: Error %d in writing %s .\nBye.\n",
argv[0], errno, ORIGFILE);
exit(1);
} /* if */

/*
* Open the del file for writing
*/
if ((del_fd = open(DELFILE, O_WRONLY | O_TRUNC | O_CREAT )) < 0) {
fprintf(stderr, "%s: Error %d in writing %s.\nBye.\n",
argv[0], errno, DELFILE);
exit(1);
} /* if */

/*
* Open the copy of wtmp for readonly
*/
if ((old_fd = open(TEMPFILE, O_RDONLY)) < 0) {
fprintf(stderr, "%s: Error %d in opening %s.\nBye.\n",
argv[0], errno, TEMPFILE);
exit(1);
} /* if */

/*
* En partant du debut du fichier, on compare les dates
* et si elles sont trop vieilles, on les oublies.
* Lorsqu'elles sont dans la periode, on les garde dans
* le fichier /var/adm/wtmpx.
*/
while (read(old_fd, &wtmpx_rec, wtmpx_rec_sz) == wtmpx_rec_sz) {
if (wtmpx_rec.ut_xtime >= temps_limite) {
if (write(new_fd, &wtmpx_rec, wtmpx_rec_sz) != wtmpx_rec_sz) {
fprintf(stderr, "%s: Error %d in writing the record.\nBye.",
argv[0], errno);
exit(1);
} /* if */
} /* if */
else {
if (write(del_fd, &wtmpx_rec, wtmpx_rec_sz) != wtmpx_rec_sz) {
fprintf(stderr, "%s: Error %d in writing the record.\nBye.",
argv[0], errno);
exit(1);
} /* if */
}
} /* while */

unlink (TEMPFILE);
exit(0); /* Nice Exit */

} /* main() */

--Boundary_(ID_ytqOWrayUSitDk9IrDvmtA)--