home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1989, 1990, John F. Haugh II
- * All rights reserved.
- *
- * Use, duplication, and disclosure prohibited without
- * the express written permission of the author.
- */
-
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <pwd.h>
- #include <time.h>
- #ifndef BSD
- #include <string.h>
- #include <memory.h>
- #else
- #include <strings.h>
- #define strchr index
- #define strrchr rindex
- #endif
- #include "config.h"
- #include "faillog.h"
-
- #ifndef lint
- static char _sccsid[] = "@(#)faillog.c 2.2 19:23:44 7/29/90";
- #endif
-
- FILE *fail; /* failure file stream */
- off_t user; /* one single user, specified on command line */
- int days; /* number of days to consider for print command */
- time_t seconds; /* that number of days in seconds */
- int max; /* maximum failure count for fail_max */
-
- int mflg; /* set fail_max for a given user */
- int rflg; /* reset fail_cnt for user or all user's */
- int uflg; /* set if user is a valid user id */
- int tflg; /* print is restricted to most recent days */
- struct faillog faillog; /* scratch structure to play with ... */
- struct stat statbuf; /* fstat buffer for file size */
-
- extern int optind;
- extern char *optarg;
- extern char *asctime ();
- extern struct passwd *getpwuid ();
- extern struct passwd *getpwnam ();
- extern struct passwd *getpwent ();
- extern struct tm *localtime ();
-
- #define DAY (24L*3600L)
- #define NOW (time ((time_t *) 0))
-
- main (argc, argv)
- int argc;
- char **argv;
- {
- char *mode;
- int uid = 0;
- int c;
- struct passwd *pwent;
-
- if (getuid () == 0) /* only root can update anything */
- mode = "r+";
- else /* all others can only look */
- mode = "r";
-
- if ((fail = fopen (FAILFILE, mode)) == (FILE *) 0) {
- perror (FAILFILE);
- exit (1);
- }
- while ((c = getopt (argc, argv, "m:pru:t:")) != EOF) {
- switch (c) {
- case 'm':
- max = atoi (optarg);
- setmax ();
- break;
- case 'p':
- print ();
- break;
- case 'r':
- reset ();
- break;
- case 'u':
- pwent = getpwnam (optarg);
- if (! pwent) {
- fprintf (stderr, "Unknown User: %s\n", optarg);
- exit (1);
- }
- uflg++;
- user = pwent->pw_uid;
- break;
- case 't':
- days = atoi (optarg);
- seconds = days * DAY;
- tflg++;
- break;
- }
- }
- fclose (fail);
- exit (0);
- }
-
- print ()
- {
- int uid;
- off_t offset;
-
- if (uflg) {
- offset = user * sizeof faillog;
- fstat (fileno (fail), &statbuf);
- if (offset >= statbuf.st_size)
- return;
-
- fseek (fail, (off_t) user * sizeof faillog, 0);
- if (fread ((char *) &faillog, sizeof faillog, 1, fail) == 1)
- print_one (&faillog, user);
- else
- perror (FAILFILE);
- } else {
- for (uid = 0;
- fread ((char *) &faillog, sizeof faillog, 1, fail) == 1;
- uid++) {
-
- if (faillog.fail_cnt == 0)
- continue;
-
- if (tflg && NOW - faillog.fail_time > seconds)
- continue;
-
- print_one (&faillog, uid);
- }
- }
- }
-
- print_one (faillog, uid)
- struct faillog *faillog;
- {
- static int once;
- char *cp;
- struct tm *tm;
- struct passwd *pwent;
-
- if (! once) {
- printf ("Username Failures Maximum Latest\n");
- once++;
- }
- pwent = getpwuid (uid);
- tm = localtime (&faillog->fail_time);
- cp = asctime (tm);
- cp[24] = '\0';
-
- if (pwent) {
- printf ("%-16s %4d %4d",
- pwent->pw_name, faillog->fail_cnt, faillog->fail_max);
- if (faillog->fail_time)
- printf (" %s on %s\n", cp, faillog->fail_line);
- else
- putchar ('\n');
- }
- }
-
- reset ()
- {
- int uid = 0;
-
- if (uflg)
- reset_one (user);
- else
- for (uid = 0;reset_one (uid);uid++)
- ;
- }
-
- reset_one (uid)
- int uid;
- {
- off_t offset;
-
- offset = uid * sizeof faillog;
- fstat (fileno (fail), &statbuf);
- if (offset >= statbuf.st_size)
- return (0);
-
- if (fseek (fail, offset, 0) != 0) {
- perror (FAILFILE);
- return (0);
- }
- if (fread ((char *) &faillog, sizeof faillog, 1, fail) != 1) {
- if (! feof (fail))
- perror (FAILFILE);
-
- return (0);
- }
- if (faillog.fail_cnt == 0)
- return (1); /* don't fill in no holes ... */
-
- faillog.fail_cnt = 0;
-
- if (fseek (fail, offset, 0) == 0
- && fwrite ((char *) &faillog, sizeof faillog, 1, fail) == 1) {
- fflush (fail);
- return (1);
- } else {
- perror (FAILFILE);
- }
- return (0);
- }
-
- setmax ()
- {
- int uid = 0;
- struct passwd *pwent;
-
- if (uflg) {
- setmax_one (user);
- } else {
- setpwent ();
- while (pwent = getpwent ())
- setmax_one (pwent->pw_uid);
- }
- }
-
- setmax_one (uid)
- int uid;
- {
- off_t offset;
-
- offset = uid * sizeof faillog;
-
- if (fseek (fail, offset, 0) != 0) {
- perror (FAILFILE);
- return;
- }
- if (fread ((char *) &faillog, sizeof faillog, 1, fail) != 1) {
- if (! feof (fail))
- perror (FAILFILE);
- } else {
- #ifndef BSD
- memset ((char *) &faillog, '\0', sizeof faillog);
- #else
- bzero ((char *) &faillog, sizeof faillog);
- #endif
- }
- faillog.fail_max = max;
-
- if (fseek (fail, offset, 0) == 0
- && fwrite ((char *) &faillog, sizeof faillog, 1, fail) == 1)
- fflush (fail);
- else
- perror (FAILFILE);
- }
-