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.
43 #include <sys/types.h>
47 #include <time.h> /* for time() */
51 #include <selinux/selinux.h>
52 #define SELINUX_ENABLED is_selinux_enabled()>0
55 #include <security/_pam_macros.h>
57 /* indicate that the following groups are defined */
59 #define PAM_SM_ACCOUNT
61 #include <security/pam_modules.h>
62 #include <security/pam_ext.h>
63 #include <security/pam_modutil.h>
71 struct spwd *_unix_run_verify_binary(pam_handle_t *pamh, unsigned int ctrl, const char *user)
73 int retval=0, child, fds[2];
74 void (*sighandler)(int) = NULL;
75 D(("running verify_binary"));
77 /* create a pipe for the messages */
79 D(("could not make pipe"));
80 pam_syslog(pamh, LOG_ERR, "Could not make pipe: %m");
85 if (off(UNIX_NOREAP, ctrl)) {
87 * This code arranges that the demise of the child does not cause
88 * the application to receive a signal it is not expecting - which
89 * may kill the application or worse.
91 * The "noreap" module argument is provided so that the admin can
92 * override this behavior.
94 sighandler = signal(SIGCHLD, SIG_DFL);
102 static char *envp[] = { NULL };
103 char *args[] = { NULL, NULL, NULL, NULL };
106 /* reopen stdin as pipe */
108 dup2(fds[1], STDOUT_FILENO);
110 /* XXX - should really tidy up PAM here too */
112 if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
113 for (i=2; i < rlim.rlim_max; i++) {
114 if ((unsigned int)fds[1] != i) {
119 /* exec binary helper */
120 args[0] = x_strdup(CHKPWD_HELPER);
121 args[1] = x_strdup(user);
122 args[2] = x_strdup("verify");
124 execve(CHKPWD_HELPER, args, envp);
126 pam_syslog(pamh, LOG_ERR, "helper binary execve failed: %m");
127 /* should not get here: exit with error */
129 D(("helper binary is not available"));
130 exit(PAM_AUTHINFO_UNAVAIL);
136 rc=waitpid(child, &retval, 0); /* wait for helper to complete */
138 pam_syslog(pamh, LOG_ERR, "unix_chkpwd waitpid returned %d: %m", rc);
139 retval = PAM_AUTH_ERR;
141 retval = WEXITSTATUS(retval);
142 if (retval != PAM_AUTHINFO_UNAVAIL) {
143 rc = pam_modutil_read(fds[0], buf, sizeof(buf) - 1);
146 if (sscanf(buf,"%ld:%ld:%ld:%ld:%ld:%ld",
147 &spwd.sp_lstchg, /* last password change */
148 &spwd.sp_min, /* days until change allowed. */
149 &spwd.sp_max, /* days before change required */
150 &spwd.sp_warn, /* days warning for expiration */
151 &spwd.sp_inact, /* days before account inactive */
152 &spwd.sp_expire) /* date when account expires */ != 6 ) retval = PAM_AUTH_ERR;
155 pam_syslog(pamh, LOG_ERR, " ERROR %d: %m", rc); retval = PAM_AUTH_ERR;
160 pam_syslog(pamh, LOG_ERR, "Fork failed: %m");
162 retval = PAM_AUTH_ERR;
166 if (sighandler != NULL) {
167 (void) signal(SIGCHLD, sighandler); /* restore old signal handler */
169 D(("Returning %d",retval));
170 if (retval != PAM_SUCCESS) {
180 * PAM framework looks for this entry-point to pass control to the
181 * account management module.
184 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
185 int argc, const char **argv)
188 const void *void_uname;
190 int retval, daysleft;
193 struct passwd *pwent;
198 ctrl = _set_ctrl(pamh, flags, NULL, argc, argv);
200 retval = pam_get_item(pamh, PAM_USER, &void_uname);
202 D(("user = `%s'", uname));
203 if (retval != PAM_SUCCESS || uname == NULL) {
204 pam_syslog(pamh, LOG_ALERT,
205 "could not identify user (from uid=%d)",
207 return PAM_USER_UNKNOWN;
210 pwent = pam_modutil_getpwnam(pamh, uname);
212 pam_syslog(pamh, LOG_ALERT,
213 "could not identify user (from getpwnam(%s))",
215 return PAM_USER_UNKNOWN;
218 if (!strcmp( pwent->pw_passwd, "*NP*" )) { /* NIS+ */
219 uid_t save_euid, save_uid;
221 save_euid = geteuid();
223 if (save_uid == pwent->pw_uid)
224 setreuid( save_euid, save_uid );
227 if (setreuid( -1, pwent->pw_uid ) == -1) {
230 if(setreuid( -1, pwent->pw_uid ) == -1)
231 return PAM_CRED_INSUFFICIENT;
234 spent = pam_modutil_getspnam (pamh, uname);
235 if (save_uid == pwent->pw_uid)
236 setreuid( save_uid, save_euid );
238 if (setreuid( -1, 0 ) == -1)
239 setreuid( save_uid, -1 );
240 setreuid( -1, save_euid );
243 } else if (_unix_shadowed (pwent))
244 spent = pam_modutil_getspnam (pamh, uname);
249 if (!spent && SELINUX_ENABLED )
250 spent = _unix_run_verify_binary(pamh, ctrl, uname);
254 if (on(UNIX_BROKEN_SHADOW,ctrl))
258 return PAM_AUTHINFO_UNAVAIL; /* Couldn't get username from shadow */
260 curdays = time(NULL) / (60 * 60 * 24);
261 D(("today is %d, last change %d", curdays, spent->sp_lstchg));
262 if ((curdays > spent->sp_expire) && (spent->sp_expire != -1)) {
263 pam_syslog(pamh, LOG_NOTICE,
264 "account %s has expired (account expired)",
266 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
267 _("Your account has expired; please contact your system administrator"));
268 D(("account expired"));
269 return PAM_ACCT_EXPIRED;
271 if (spent->sp_lstchg == 0) {
272 pam_syslog(pamh, LOG_NOTICE,
273 "expired password for user %s (root enforced)",
275 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
276 _("You are required to change your password immediately (root enforced)"));
277 D(("need a new password"));
278 return PAM_NEW_AUTHTOK_REQD;
280 if (curdays < spent->sp_lstchg) {
281 pam_syslog(pamh, LOG_DEBUG,
282 "account %s has password changed in future",
286 if ((curdays - spent->sp_lstchg > spent->sp_max)
287 && (curdays - spent->sp_lstchg > spent->sp_inact)
288 && (curdays - spent->sp_lstchg > spent->sp_max + spent->sp_inact)
289 && (spent->sp_max != -1) && (spent->sp_inact != -1)) {
290 pam_syslog(pamh, LOG_NOTICE,
291 "account %s has expired (failed to change password)",
293 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
294 _("Your account has expired; please contact your system administrator"));
295 D(("account expired 2"));
296 return PAM_ACCT_EXPIRED;
298 if ((curdays - spent->sp_lstchg > spent->sp_max) && (spent->sp_max != -1)) {
299 pam_syslog(pamh, LOG_DEBUG,
300 "expired password for user %s (password aged)",
302 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
303 _("You are required to change your password immediately (password aged)"));
304 D(("need a new password 2"));
305 return PAM_NEW_AUTHTOK_REQD;
307 if ((curdays - spent->sp_lstchg > spent->sp_max - spent->sp_warn)
308 && (spent->sp_max != -1) && (spent->sp_warn != -1)) {
309 daysleft = (spent->sp_lstchg + spent->sp_max) - curdays;
310 pam_syslog(pamh, LOG_DEBUG,
311 "password for user %s will expire in %d days",
313 snprintf(buf, sizeof (buf), _("Warning: your password will expire in %d day%.2s"),
314 daysleft, daysleft == 1 ? "" : "s");
315 _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
324 /* static module data */
326 struct pam_module _pam_unix_acct_modstruct = {