home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / failure.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-05  |  3.7 KB  |  188 lines

  1. /*
  2.  * Copyright 1989, 1990, 1991, 1992, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include <sys/types.h>
  13. #include <fcntl.h>
  14. #include <time.h>
  15. #include <stdio.h>
  16. #ifndef    BSD
  17. #include <string.h>
  18. #include <memory.h>
  19. #else
  20. #include <strings.h>
  21. #define    strchr    index
  22. #define    strrchr    rindex
  23. #endif
  24. #ifdef    UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #include "faillog.h"
  28. #include "config.h"
  29.  
  30. #include <utmp.h>
  31.  
  32. #ifndef    lint
  33. static    char    _sccsid[] = "@(#)failure.c    3.2    20:36:32    3/7/92";
  34. #endif
  35.  
  36. #define    DAY    (24L*3600L)
  37. #define    YEAR    (365L*DAY)
  38. #define    NOW    (time ((time_t *) 0))
  39.  
  40. extern    struct    tm    *localtime ();
  41. extern    char    *asctime ();
  42. extern    void    failprint ();
  43. extern    char    *getdef_str();
  44.  
  45. /*
  46.  * failure - make failure entry
  47.  */
  48.  
  49. void
  50. failure (uid, tty, faillog)
  51. int    uid;
  52. char    *tty;
  53. struct    faillog    *faillog;
  54. {
  55.     int    fd;
  56.  
  57.     if ((fd = open (FAILFILE, O_RDWR)) < 0)
  58.         return;
  59.  
  60.     lseek (fd, (off_t) (sizeof *faillog) * uid, 0);
  61.     if (read (fd, (char *) faillog, sizeof *faillog)
  62.             != sizeof *faillog)
  63. #ifndef    BSD
  64.         memset ((void *) faillog, 0, sizeof *faillog);
  65. #else
  66.         bzero ((char *) faillog, sizeof *faillog);
  67. #endif
  68.  
  69.     if (faillog->fail_max == 0 || faillog->fail_cnt < faillog->fail_max)
  70.         faillog->fail_cnt++;
  71.  
  72.     strncpy (faillog->fail_line, tty, sizeof faillog->fail_line);
  73.     faillog->fail_time = time ((time_t *) 0);
  74.  
  75.     lseek (fd, (off_t) (sizeof *faillog) * uid, 0);
  76.     write (fd, (char *) faillog, sizeof *faillog);
  77.     close (fd);
  78. }
  79.  
  80. /*
  81.  * failcheck - check for failures > allowable
  82.  *
  83.  * failcheck() is called AFTER the password has been validated.
  84.  */
  85.  
  86. int
  87. failcheck (uid, faillog, failed)
  88. int    uid;
  89. struct    faillog    *faillog;
  90. int    failed;
  91. {
  92.     int    fd;
  93.     int    okay = 1;
  94.     struct    faillog    fail;
  95.  
  96.     if ((fd = open (FAILFILE, O_RDWR)) < 0)
  97.         return (1);
  98.  
  99.     lseek (fd, (off_t) (sizeof *faillog) * uid, 0);
  100.     if (read (fd, (char *) faillog, sizeof *faillog) == sizeof *faillog) {
  101.         if (faillog->fail_max != 0
  102.                 && faillog->fail_cnt >= faillog->fail_max)
  103.             okay = 0;
  104.     }
  105.     if (!failed && okay) {
  106.         fail = *faillog;
  107.         fail.fail_cnt = 0;
  108.  
  109.         lseek (fd, (off_t) sizeof fail * uid, 0);
  110.         write (fd, (char *) &fail, sizeof fail);
  111.     }
  112.     close (fd);
  113.  
  114.     return (okay);
  115. }
  116.  
  117. /*
  118.  * failprint - print line of failure information
  119.  */
  120.  
  121. void
  122. failprint (fail)
  123. struct    faillog    *fail;
  124. {
  125.     struct    tm    *tp;
  126. #ifdef    SVR4
  127.     char    lasttime[32];
  128. #else
  129.     char    *lasttime;
  130. #endif
  131.  
  132.     if (fail->fail_cnt == 0)
  133.         return;
  134.  
  135.     tp = localtime (&(fail->fail_time));
  136.  
  137. #if __STDC__
  138.     /*
  139.      * Only print as much date and time info as it needed to
  140.      * know when the failure was.
  141.      */
  142.  
  143.     if (NOW - fail->fail_time >= YEAR)
  144.         strftime(lasttime, sizeof lasttime, NULL, tp);
  145.     else if (NOW - fail->fail_time >= DAY)
  146.         strftime(lasttime, sizeof lasttime, "%A %T", tp);
  147.     else
  148.         strftime(lasttime, sizeof lasttime, "%T", tp);
  149. #else
  150.  
  151.     /*
  152.      * Do the same thing, but don't use strftime since it
  153.      * probably doesn't exist on this system
  154.      */
  155.  
  156.     lasttime = asctime (tp);
  157.     lasttime[24] = '\0';
  158.  
  159.     if (NOW - fail->fail_time < YEAR)
  160.         lasttime[19] = '\0';
  161.     if (NOW - fail->fail_time < DAY)
  162.         lasttime = lasttime + 11;
  163.  
  164.     if (*lasttime == ' ')
  165.         lasttime++;
  166. #endif    /* __STDC__ */
  167.     printf ("%d %s since last login.  Last was %s on %s.\n",
  168.         fail->fail_cnt, fail->fail_cnt > 1 ? "failures":"failure",
  169.         lasttime, fail->fail_line);
  170. }
  171.  
  172. void
  173. failtmp (failent)
  174. struct    utmp    *failent;
  175. {
  176.     int    fd;
  177.     char    *ftmp;
  178.  
  179.     if ((ftmp = getdef_str ("FTMP_FILE")) == 0)
  180.         return;
  181.  
  182.     if ((fd = open (ftmp, O_WRONLY|O_APPEND)) == -1)
  183.         return;
  184.  
  185.     write (fd, (char *) failent, sizeof *failent);
  186.     close (fd);
  187. }
  188.