Release bumped to "gts4".
[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
21 #include "tac_plus.h"
22
23 #include <ctype.h>
24 #include <time.h>
25 #include <string.h>
26 #include <stdio.h>
27
28 #include "expire.h"
29
30
31 /*
32  * check a date for expiry. If the field specifies
33  * a shell return PW_OK
34  *
35  * Return PW_OK if not expired
36  * Return PW_EXPIRING if expiry is coming soon
37  * Return PW_EXPIRED  if already expired
38  */
39
40 #define SEC_IN_DAY (24*60*60)
41 #define WARNING_PERIOD 14
42
43 static char *monthname[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
44 "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
45 static long days_ere_month[] = {0, 31, 59, 90, 120, 151,
46 181, 212, 243, 273, 304, 334};
47
48 int check_expiration TAC_ARGS((const char *date));
49
50 int
51 check_expiration(date)
52 const char *date;
53 {
54     long day, month, year, leaps, now, expiration, warning;
55     char monthstr[10];
56     int i;
57
58     monthstr[0] = '\0';
59
60     /* If no date or a shell, let it pass.  (Backward compatibility.) */
61     if (!date || (strlen(date) == 0) || (*date == '/'))
62         return (PW_OK);
63
64     /* Parse date string.  Fail it upon error. */
65     if (sscanf(date, "%s %ld %ld", monthstr, &day, &year) != 3)
66         return (PW_EXPIRED);
67
68     for(i=0; i < 3; i++) {
69         monthstr[i] = toupper((int) monthstr[i]);
70     }
71
72     /* Compute the expiration date in days. */
73     for (month = 0; month < 12; month++)
74         if (strncmp(monthstr, monthname[month], 3) == 0)
75             break;
76
77     if (month > 11)
78         return (PW_EXPIRED);
79
80     leaps = (year - 1969) / 4 + (((year % 4) == 0) && (month > 2));
81     expiration = (((year - 1970) * 365) + days_ere_month[month] + (day - 1) + leaps);
82     warning = expiration - WARNING_PERIOD;
83
84     /* Get the current time (to the day) */
85     now = time(NULL) / SEC_IN_DAY;
86
87     if (now > expiration)
88         return (PW_EXPIRED);
89
90     if (now > warning)
91         return (PW_EXPIRING);
92
93     return (PW_OK);
94 }