2 * Copyright Elliot Lee, 1996. All rights reserved.
3 * Copyright Jan Rêkorajski, 1999. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, and the entire permission notice in its entirety,
10 * including the disclaimer of warranties.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior
18 * ALTERNATIVELY, this product may be distributed under the terms of
19 * the GNU Public License, in which case the provisions of the GPL are
20 * required INSTEAD OF the above restrictions. (This clause is
21 * necessary due to a potential bad interaction between the GPL and
22 * the restrictions contained in a BSD-style copyright.)
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34 * OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <security/_pam_aconf.h>
43 #include <sys/types.h>
47 #include <time.h> /* for time() */
49 #include <security/_pam_macros.h>
51 /* indicate that the following groups are defined */
53 #define PAM_SM_ACCOUNT
55 #include <security/pam_modules.h>
58 #include <security/pam_appl.h>
59 #endif /* LINUX_PAM */
64 * PAM framework looks for this entry-point to pass control to the
65 * account management module.
68 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
69 int argc, const char **argv)
81 ctrl = _set_ctrl(flags, NULL, argc, argv);
83 retval = pam_get_item(pamh, PAM_USER, (const void **) &uname);
84 D(("user = `%s'", uname));
85 if (retval != PAM_SUCCESS || uname == NULL) {
87 ,"could not identify user (from uid=%d)"
89 return PAM_USER_UNKNOWN;
92 pwent = getpwnam(uname);
95 ,"could not identify user (from getpwnam(%s))"
97 return PAM_USER_UNKNOWN;
100 if (!strcmp( pwent->pw_passwd, "*NP*" )) { /* NIS+ */
101 uid_t save_euid, save_uid;
103 save_euid = geteuid();
105 if (save_uid == pwent->pw_uid)
106 setreuid( save_euid, save_uid );
109 if (setreuid( -1, pwent->pw_uid ) == -1) {
112 if(setreuid( -1, pwent->pw_uid ) == -1)
113 return PAM_CRED_INSUFFICIENT;
116 spent = getspnam( uname );
117 if (save_uid == pwent->pw_uid)
118 setreuid( save_uid, save_euid );
120 if (setreuid( -1, 0 ) == -1)
121 setreuid( save_uid, -1 );
122 setreuid( -1, save_euid );
125 } else if (!strcmp( pwent->pw_passwd, "x" )) {
126 spent = getspnam(uname);
132 return PAM_AUTHINFO_UNAVAIL; /* Couldn't get username from shadow */
134 curdays = time(NULL) / (60 * 60 * 24);
135 D(("today is %d, last change %d", curdays, spent->sp_lstchg));
136 if ((curdays > spent->sp_expire) && (spent->sp_expire != -1)
137 && (spent->sp_lstchg != 0)) {
139 ,"account %s has expired (account expired)"
141 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
142 "Your account has expired; please contact your system administrator");
143 D(("account expired"));
144 return PAM_ACCT_EXPIRED;
146 if ((curdays > (spent->sp_lstchg + spent->sp_max + spent->sp_inact))
147 && (spent->sp_max != -1) && (spent->sp_inact != -1)
148 && (spent->sp_lstchg != 0)) {
150 ,"account %s has expired (failed to change password)"
152 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
153 "Your account has expired; please contact your system administrator");
154 D(("account expired 2"));
155 return PAM_ACCT_EXPIRED;
157 D(("when was the last change"));
158 if (spent->sp_lstchg == 0) {
160 ,"expired password for user %s (root enforced)"
162 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
163 "You are required to change your password immediately (root enforced)");
164 D(("need a new password"));
165 return PAM_NEW_AUTHTOK_REQD;
167 if (((spent->sp_lstchg + spent->sp_max) < curdays) && (spent->sp_max != -1)) {
169 ,"expired password for user %s (password aged)"
171 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
172 "You are required to change your password immediately (password aged)");
173 D(("need a new password 2"));
174 return PAM_NEW_AUTHTOK_REQD;
176 if ((curdays > (spent->sp_lstchg + spent->sp_max - spent->sp_warn))
177 && (spent->sp_max != -1) && (spent->sp_warn != -1)) {
178 daysleft = (spent->sp_lstchg + spent->sp_max) - curdays;
180 ,"password for user %s will expire in %d days"
182 snprintf(buf, 80, "Warning: your password will expire in %d day%.2s",
183 daysleft, daysleft == 1 ? "" : "s");
184 _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
193 /* static module data */
195 struct pam_module _pam_unix_acct_modstruct = {