]> granicus.if.org Git - shadow/blob - libmisc/isexpired.c
Updated copyright dates.
[shadow] / libmisc / isexpired.c
1 /*
2  * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 1997, Marek Michałkiewicz
4  * Copyright (c) 2001 - 2005, Tomasz Kłoczko
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the copyright holders or contributors may not be used to
16  *    endorse or promote products derived from this software without
17  *    specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
23  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /*
33  * Extracted from age.c and made part of libshadow.a - may be useful
34  * in other shadow-aware programs.  --marekm
35  */
36
37 #include <config.h>
38
39 #include <sys/types.h>
40 #include "prototypes.h"
41 #include "defines.h"
42 #include <pwd.h>
43 #include <time.h>
44
45 #ident "$Id$"
46
47
48 /*
49  * isexpired - determine if account is expired yet
50  *
51  *      isexpired calculates the expiration date based on the
52  *      password expiration criteria.
53  */
54  /*ARGSUSED*/ int isexpired (const struct passwd *pw, const struct spwd *sp)
55 {
56         long now;
57
58         now = time ((time_t *) 0) / SCALE;
59
60         if (!sp)
61                 sp = pwd_to_spwd (pw);
62
63         /*
64          * Quick and easy - there is an expired account field
65          * along with an inactive account field.  Do the expired
66          * one first since it is worse.
67          */
68
69         if (sp->sp_expire > 0 && now >= sp->sp_expire)
70                 return 3;
71
72         /*
73          * Last changed date 1970-01-01 (not very likely) means that
74          * the password must be changed on next login (passwd -e).
75          *
76          * The check for "x" is a workaround for RedHat NYS libc bug -
77          * if /etc/shadow doesn't exist, getspnam() still succeeds and
78          * returns sp_lstchg==0 (must change password) instead of -1!
79          */
80         if (sp->sp_lstchg == 0 && !strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING))
81                 return 1;
82
83         if (sp->sp_lstchg > 0 && sp->sp_max >= 0 && sp->sp_inact >= 0 &&
84             now >= sp->sp_lstchg + sp->sp_max + sp->sp_inact)
85                 return 2;
86
87         /*
88          * The last and max fields must be present for an account
89          * to have an expired password.  A maximum of >10000 days
90          * is considered to be infinite.
91          */
92
93         if (sp->sp_lstchg == -1 ||
94             sp->sp_max == -1 || sp->sp_max >= (10000L * DAY / SCALE))
95                 return 0;
96
97         /*
98          * Calculate today's day and the day on which the password
99          * is going to expire.  If that date has already passed,
100          * the password has expired.
101          */
102
103         if (now >= sp->sp_lstchg + sp->sp_max)
104                 return 1;
105         return 0;
106 }