1ef745ce1048d3eeda33e883d0c315d3c169ff29
[tac_plus.git] / expire.c
1 /* 
2    Copyright (c) 1995-1998 by Cisco systems, Inc.
3
4    Permission to use, copy, modify, and distribute this software for
5    any purpose and without fee is hereby granted, provided that this
6    copyright and permission notice appear on all copies of the
7    software and supporting documentation, the name of Cisco Systems,
8    Inc. not be used in advertising or publicity pertaining to
9    distribution of the program without specific prior permission, and
10    notice be given in supporting documentation that modification,
11    copying and distribution is by permission of Cisco Systems, Inc.
12
13    Cisco Systems, Inc. makes no representations about the suitability
14    of this software for any purpose.  THIS SOFTWARE IS PROVIDED ``AS
15    IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
16    WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17    FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20 #include "tac_plus.h"
21 #include "expire.h"
22
23 /*
24  * check a date for expiry. If the field specifies
25  * a shell return PW_OK
26  *
27  * Return PW_OK if not expired
28  * Return PW_EXPIRING if expiry is coming soon
29  * Return PW_EXPIRED  if already expired
30  */
31
32 #define SEC_IN_DAY (24*60*60)
33 #define WARNING_PERIOD 14
34
35 static char *monthname[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
36 "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
37 static long days_ere_month[] = {0, 31, 59, 90, 120, 151,
38 181, 212, 243, 273, 304, 334};
39
40 int
41 check_expiration(date)
42 char *date;
43 {
44     long day, month, year, leaps, now, expiration, warning;
45     char monthstr[10];
46     int i;
47
48     monthstr[0] = '\0';
49
50     /* If no date or a shell, let it pass.  (Backward compatibility.) */
51     if (!date || (strlen(date) == 0) || (*date == '/'))
52         return (PW_OK);
53
54     /* Parse date string.  Fail it upon error. */
55     if (sscanf(date, "%s %d %d", monthstr, &day, &year) != 3)
56         return (PW_EXPIRED);
57
58     for(i=0; i < 3; i++) {
59         monthstr[i] = toupper(monthstr[i]);
60     }
61
62     /* Compute the expiration date in days. */
63     for (month = 0; month < 12; month++)
64         if (strncmp(monthstr, monthname[month], 3) == 0)
65             break;
66
67     if (month > 11)
68         return (PW_EXPIRED);
69
70     leaps = (year - 1969) / 4 + (((year % 4) == 0) && (month > 2));
71     expiration = (((year - 1970) * 365) + days_ere_month[month] + (day - 1) + leaps);
72     warning = expiration - WARNING_PERIOD;
73
74     /* Get the current time (to the day) */
75     now = time(NULL) / SEC_IN_DAY;
76
77     if (now > expiration)
78         return (PW_EXPIRED);
79
80     if (now > warning)
81         return (PW_EXPIRING);
82
83     return (PW_OK);
84 }