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_modutil.h>
65 #include <security/pam_appl.h>
66 #endif /* LINUX_PAM */
74 struct spwd *_unix_run_verify_binary(pam_handle_t *pamh, unsigned int ctrl, const char *user)
76 int retval=0, child, fds[2];
77 void (*sighandler)(int) = NULL;
78 D(("running verify_binary"));
80 /* create a pipe for the messages */
82 D(("could not make pipe"));
83 _log_err(LOG_ERR, pamh, "Could not make pipe %s",strerror(errno));
88 if (off(UNIX_NOREAP, ctrl)) {
90 * This code arranges that the demise of the child does not cause
91 * the application to receive a signal it is not expecting - which
92 * may kill the application or worse.
94 * The "noreap" module argument is provided so that the admin can
95 * override this behavior.
97 sighandler = signal(SIGCHLD, SIG_DFL);
105 static char *envp[] = { NULL };
106 char *args[] = { NULL, NULL, NULL, NULL };
109 /* reopen stdin as pipe */
111 dup2(fds[1], STDOUT_FILENO);
113 /* XXX - should really tidy up PAM here too */
115 if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
116 for (i=2; i < rlim.rlim_max; i++) {
117 if ((unsigned int)fds[1] != i) {
122 /* exec binary helper */
123 args[0] = x_strdup(CHKPWD_HELPER);
124 args[1] = x_strdup(user);
125 args[2] = x_strdup("verify");
127 execve(CHKPWD_HELPER, args, envp);
129 _log_err(LOG_ERR, pamh, "helper binary execve failed: %s",strerror(errno));
130 /* should not get here: exit with error */
132 D(("helper binary is not available"));
133 exit(PAM_AUTHINFO_UNAVAIL);
139 rc=waitpid(child, &retval, 0); /* wait for helper to complete */
141 _log_err(LOG_ERR, pamh, "unix_chkpwd waitpid returned %d: %s", rc, strerror(errno));
142 retval = PAM_AUTH_ERR;
144 retval = WEXITSTATUS(retval);
145 if (retval != PAM_AUTHINFO_UNAVAIL) {
146 rc = pam_modutil_read(fds[0], buf, sizeof(buf) - 1);
149 if (sscanf(buf,"%ld:%ld:%ld:%ld:%ld:%ld",
150 &spwd.sp_lstchg, /* last password change */
151 &spwd.sp_min, /* days until change allowed. */
152 &spwd.sp_max, /* days before change required */
153 &spwd.sp_warn, /* days warning for expiration */
154 &spwd.sp_inact, /* days before account inactive */
155 &spwd.sp_expire) /* date when account expires */ != 6 ) retval = PAM_AUTH_ERR;
158 _log_err(LOG_ERR, pamh, " ERROR %d:%s \n",rc, strerror(errno)); retval = PAM_AUTH_ERR;
163 _log_err(LOG_ERR, pamh, "Fork failed %s \n",strerror(errno));
165 retval = PAM_AUTH_ERR;
169 if (sighandler != NULL) {
170 (void) signal(SIGCHLD, sighandler); /* restore old signal handler */
172 D(("Returning %d",retval));
173 if (retval != PAM_SUCCESS) {
183 * PAM framework looks for this entry-point to pass control to the
184 * account management module.
187 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
188 int argc, const char **argv)
192 int retval, daysleft;
195 struct passwd *pwent;
200 ctrl = _set_ctrl(pamh, flags, NULL, argc, argv);
202 retval = pam_get_item(pamh, PAM_USER, &uname);
203 D(("user = `%s'", uname));
204 if (retval != PAM_SUCCESS || uname == NULL) {
205 _log_err(LOG_ALERT, pamh
206 ,"could not identify user (from uid=%d)"
208 return PAM_USER_UNKNOWN;
211 pwent = pam_modutil_getpwnam(pamh, uname);
213 _log_err(LOG_ALERT, pamh
214 ,"could not identify user (from getpwnam(%s))"
216 return PAM_USER_UNKNOWN;
219 if (!strcmp( pwent->pw_passwd, "*NP*" )) { /* NIS+ */
220 uid_t save_euid, save_uid;
222 save_euid = geteuid();
224 if (save_uid == pwent->pw_uid)
225 setreuid( save_euid, save_uid );
228 if (setreuid( -1, pwent->pw_uid ) == -1) {
231 if(setreuid( -1, pwent->pw_uid ) == -1)
232 return PAM_CRED_INSUFFICIENT;
235 spent = pam_modutil_getspnam (pamh, uname);
236 if (save_uid == pwent->pw_uid)
237 setreuid( save_uid, save_euid );
239 if (setreuid( -1, 0 ) == -1)
240 setreuid( save_uid, -1 );
241 setreuid( -1, save_euid );
244 } else if (_unix_shadowed (pwent))
245 spent = pam_modutil_getspnam (pamh, uname);
250 if (!spent && SELINUX_ENABLED )
251 spent = _unix_run_verify_binary(pamh, ctrl, uname);
255 if (on(UNIX_BROKEN_SHADOW,ctrl))
259 return PAM_AUTHINFO_UNAVAIL; /* Couldn't get username from shadow */
261 curdays = time(NULL) / (60 * 60 * 24);
262 D(("today is %d, last change %d", curdays, spent->sp_lstchg));
263 if ((curdays > spent->sp_expire) && (spent->sp_expire != -1)) {
264 _log_err(LOG_NOTICE, pamh
265 ,"account %s has expired (account expired)"
267 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
268 _("Your account has expired; please contact your system administrator"));
269 D(("account expired"));
270 return PAM_ACCT_EXPIRED;
272 if (spent->sp_lstchg == 0) {
273 _log_err(LOG_NOTICE, pamh
274 ,"expired password for user %s (root enforced)"
276 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
277 _("You are required to change your password immediately (root enforced)"));
278 D(("need a new password"));
279 return PAM_NEW_AUTHTOK_REQD;
281 if (curdays < spent->sp_lstchg) {
282 _log_err(LOG_DEBUG, pamh
283 ,"account %s has password changed in future"
287 if ((curdays - spent->sp_lstchg > spent->sp_max)
288 && (curdays - spent->sp_lstchg > spent->sp_inact)
289 && (curdays - spent->sp_lstchg > spent->sp_max + spent->sp_inact)
290 && (spent->sp_max != -1) && (spent->sp_inact != -1)) {
291 _log_err(LOG_NOTICE, pamh
292 ,"account %s has expired (failed to change password)"
294 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
295 _("Your account has expired; please contact your system administrator"));
296 D(("account expired 2"));
297 return PAM_ACCT_EXPIRED;
299 if ((curdays - spent->sp_lstchg > spent->sp_max) && (spent->sp_max != -1)) {
300 _log_err(LOG_DEBUG, pamh
301 ,"expired password for user %s (password aged)"
303 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
304 _("You are required to change your password immediately (password aged)"));
305 D(("need a new password 2"));
306 return PAM_NEW_AUTHTOK_REQD;
308 if ((curdays - spent->sp_lstchg > spent->sp_max - spent->sp_warn)
309 && (spent->sp_max != -1) && (spent->sp_warn != -1)) {
310 daysleft = (spent->sp_lstchg + spent->sp_max) - curdays;
311 _log_err(LOG_DEBUG, pamh
312 ,"password for user %s will expire in %d days"
314 snprintf(buf, sizeof (buf), _("Warning: your password will expire in %d day%.2s"),
315 daysleft, daysleft == 1 ? "" : "s");
316 _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
325 /* static module data */
327 struct pam_module _pam_unix_acct_modstruct = {