home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2287 / failure.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.9 KB  |  160 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Use, duplication, and disclosure prohibited without
  6.  * the express written permission of the author.
  7.  */
  8.  
  9. #include <sys/types.h>
  10. #include <fcntl.h>
  11. #include <time.h>
  12. #ifndef    BSD
  13. #include <string.h>
  14. #include <memory.h>
  15. #else
  16. #include <strings.h>
  17. #define    strchr    index
  18. #define    strrchr    rindex
  19. #endif
  20. #include "faillog.h"
  21. #include "config.h"
  22.  
  23. #ifdef    FTMP
  24. #include <utmp.h>
  25. #endif
  26.  
  27. #ifndef    lint
  28. static    char    _sccsid[] = "@(#)failure.c    2.3    19:23:48    7/29/90";
  29. #endif
  30.  
  31. #ifdef    FAILLOG
  32.  
  33. #define    DAY    (24L*3600L)
  34. #define    YEAR    (365L*DAY)
  35. #define    NOW    (time ((time_t *) 0))
  36.  
  37. extern    struct    tm    *localtime ();
  38. extern    char    *asctime ();
  39. extern    void    failprint ();
  40.  
  41. /*
  42.  * failure - make failure entry
  43.  */
  44.  
  45. void
  46. failure (uid, tty, faillog)
  47. int    uid;
  48. char    *tty;
  49. struct    faillog    *faillog;
  50. {
  51.     int    fd;
  52.  
  53.     if ((fd = open (FAILFILE, O_RDWR)) < 0)
  54.         return;
  55.  
  56.     lseek (fd, (off_t) (sizeof *faillog) * uid, 0);
  57.     if (read (fd, (char *) faillog, sizeof *faillog)
  58.             != sizeof *faillog)
  59. #ifndef    BSD
  60.         memset ((void *) faillog, '\0', sizeof *faillog);
  61. #else
  62.         bzero ((char *) faillog, sizeof *faillog);
  63. #endif
  64.  
  65.     if (faillog->fail_max == 0 || faillog->fail_cnt < faillog->fail_max)
  66.         faillog->fail_cnt++;
  67.  
  68.     strncpy (faillog->fail_line, tty, sizeof faillog->fail_line);
  69.     faillog->fail_time = time ((time_t *) 0);
  70.  
  71.     lseek (fd, (off_t) (sizeof *faillog) * uid, 0);
  72.     write (fd, (char *) faillog, sizeof *faillog);
  73.     close (fd);
  74. }
  75.  
  76. /*
  77.  * failcheck - check for failures > allowable
  78.  *
  79.  * failcheck() is called AFTER the password has been validated.
  80.  */
  81.  
  82. int
  83. failcheck (uid, faillog, failed)
  84. int    uid;
  85. struct    faillog    *faillog;
  86. {
  87.     int    fd;
  88.     int    okay = 1;
  89.     struct    faillog    fail;
  90.  
  91.     if ((fd = open (FAILFILE, O_RDWR)) < 0)
  92.         return (1);
  93.  
  94.     lseek (fd, (off_t) (sizeof *faillog) * uid, 0);
  95.     if (read (fd, (char *) faillog, sizeof *faillog) == sizeof *faillog) {
  96.         if (faillog->fail_max != 0
  97.                 && faillog->fail_cnt >= faillog->fail_max)
  98.             okay = 0;
  99.     }
  100.     if (!failed && okay) {
  101.         fail = *faillog;
  102.         fail.fail_cnt = 0;
  103.  
  104.         lseek (fd, (off_t) sizeof fail * uid, 0);
  105.         write (fd, (char *) &fail, sizeof fail);
  106.     }
  107.     close (fd);
  108.  
  109.     return (okay);
  110. }
  111.  
  112. /*
  113.  * failprint - print line of failure information
  114.  */
  115.  
  116. void
  117. failprint (uid, fail)
  118. struct    faillog    *fail;
  119. {
  120.     int    fd;
  121.     struct    tm    *tp;
  122.     char    *lasttime;
  123.  
  124.     if (fail->fail_cnt == 0)
  125.         return;
  126.  
  127.     tp = localtime (&fail->fail_time);
  128.     lasttime = asctime (tp);
  129.     lasttime[24] = '\0';
  130.  
  131.     if (NOW - fail->fail_time < YEAR)
  132.         lasttime[19] = '\0';
  133.     if (NOW - fail->fail_time < DAY)
  134.         lasttime = lasttime + 11;
  135.  
  136.     if (*lasttime == ' ')
  137.         lasttime++;
  138.  
  139.     printf ("%d %s since last login.  Last was %s on %s.\n",
  140.         fail->fail_cnt, fail->fail_cnt > 1 ? "failures":"failure",
  141.         lasttime, fail->fail_line);
  142. }
  143. #endif
  144.  
  145. #ifdef    FTMP
  146.  
  147. void
  148. failtmp (failent)
  149. struct    utmp    *failent;
  150. {
  151.     int    fd;
  152.  
  153.     if ((fd = open (FTMP, O_WRONLY|O_APPEND)) == -1)
  154.         return;
  155.  
  156.     write (fd, (char *) failent, sizeof *failent);
  157.     close (fd);
  158. }
  159. #endif
  160.