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>
56 #include <security/_pam_modutil.h>
59 #include <security/pam_appl.h>
60 #endif /* LINUX_PAM */
65 * PAM framework looks for this entry-point to pass control to the
66 * account management module.
69 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
70 int argc, const char **argv)
82 ctrl = _set_ctrl(pamh, flags, NULL, argc, argv);
84 retval = pam_get_item(pamh, PAM_USER, (const void **) &uname);
85 D(("user = `%s'", uname));
86 if (retval != PAM_SUCCESS || uname == NULL) {
87 _log_err(LOG_ALERT, pamh
88 ,"could not identify user (from uid=%d)"
90 return PAM_USER_UNKNOWN;
93 pwent = _pammodutil_getpwnam(pamh, uname);
95 _log_err(LOG_ALERT, pamh
96 ,"could not identify user (from getpwnam(%s))"
98 return PAM_USER_UNKNOWN;
101 if (!strcmp( pwent->pw_passwd, "*NP*" )) { /* NIS+ */
102 uid_t save_euid, save_uid;
104 save_euid = geteuid();
106 if (save_uid == pwent->pw_uid)
107 setreuid( save_euid, save_uid );
110 if (setreuid( -1, pwent->pw_uid ) == -1) {
113 if(setreuid( -1, pwent->pw_uid ) == -1)
114 return PAM_CRED_INSUFFICIENT;
117 spent = _pammodutil_getspnam (pamh, uname);
118 if (save_uid == pwent->pw_uid)
119 setreuid( save_uid, save_euid );
121 if (setreuid( -1, 0 ) == -1)
122 setreuid( save_uid, -1 );
123 setreuid( -1, save_euid );
126 } else if (_unix_shadowed (pwent))
127 spent = _pammodutil_getspnam (pamh, uname);
132 if (on(UNIX_BROKEN_SHADOW,ctrl))
136 return PAM_AUTHINFO_UNAVAIL; /* Couldn't get username from shadow */
138 curdays = time(NULL) / (60 * 60 * 24);
139 D(("today is %d, last change %d", curdays, spent->sp_lstchg));
140 if ((curdays > spent->sp_expire) && (spent->sp_expire != -1)
141 && (spent->sp_lstchg != 0)) {
142 _log_err(LOG_NOTICE, pamh
143 ,"account %s has expired (account expired)"
145 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
146 "Your account has expired; please contact your system administrator");
147 D(("account expired"));
148 return PAM_ACCT_EXPIRED;
150 if ((curdays > (spent->sp_lstchg + spent->sp_max + spent->sp_inact))
151 && (spent->sp_max != -1) && (spent->sp_inact != -1)
152 && (spent->sp_lstchg != 0)) {
153 _log_err(LOG_NOTICE, pamh
154 ,"account %s has expired (failed to change password)"
156 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
157 "Your account has expired; please contact your system administrator");
158 D(("account expired 2"));
159 return PAM_ACCT_EXPIRED;
161 D(("when was the last change"));
162 if (spent->sp_lstchg == 0) {
163 _log_err(LOG_NOTICE, pamh
164 ,"expired password for user %s (root enforced)"
166 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
167 "You are required to change your password immediately (root enforced)");
168 D(("need a new password"));
169 return PAM_NEW_AUTHTOK_REQD;
171 if (((spent->sp_lstchg + spent->sp_max) < curdays) && (spent->sp_max != -1)) {
172 _log_err(LOG_DEBUG, pamh
173 ,"expired password for user %s (password aged)"
175 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
176 "You are required to change your password immediately (password aged)");
177 D(("need a new password 2"));
178 return PAM_NEW_AUTHTOK_REQD;
180 if ((curdays > (spent->sp_lstchg + spent->sp_max - spent->sp_warn))
181 && (spent->sp_max != -1) && (spent->sp_warn != -1)) {
182 daysleft = (spent->sp_lstchg + spent->sp_max) - curdays;
183 _log_err(LOG_DEBUG, pamh
184 ,"password for user %s will expire in %d days"
186 snprintf(buf, 80, "Warning: your password will expire in %d day%.2s",
187 daysleft, daysleft == 1 ? "" : "s");
188 _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
197 /* static module data */
199 struct pam_module _pam_unix_acct_modstruct = {