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 <security/_pam_macros.h>
53 /* indicate that the following groups are defined */
55 #define PAM_SM_ACCOUNT
57 #include <security/pam_modules.h>
58 #include <security/pam_ext.h>
59 #include <security/pam_modutil.h>
62 #include "passverify.h"
64 int _unix_run_verify_binary(pam_handle_t *pamh, unsigned int ctrl,
65 const char *user, int *daysleft)
67 int retval=0, child, fds[2];
68 struct sigaction newsa, oldsa;
69 D(("running verify_binary"));
71 /* create a pipe for the messages */
73 D(("could not make pipe"));
74 pam_syslog(pamh, LOG_ERR, "Could not make pipe: %m");
79 if (off(UNIX_NOREAP, ctrl)) {
81 * This code arranges that the demise of the child does not cause
82 * the application to receive a signal it is not expecting - which
83 * may kill the application or worse.
85 * The "noreap" module argument is provided so that the admin can
86 * override this behavior.
88 memset(&newsa, '\0', sizeof(newsa));
89 newsa.sa_handler = SIG_DFL;
90 sigaction(SIGCHLD, &newsa, &oldsa);
98 static char *envp[] = { NULL };
99 char *args[] = { NULL, NULL, NULL, NULL };
101 /* reopen stdout as pipe */
102 dup2(fds[1], STDOUT_FILENO);
104 /* XXX - should really tidy up PAM here too */
106 if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
107 if (rlim.rlim_max >= MAX_FD_NO)
108 rlim.rlim_max = MAX_FD_NO;
109 for (i=0; i < (int)rlim.rlim_max; i++) {
110 if (i != STDOUT_FILENO) {
116 if (geteuid() == 0) {
117 /* must set the real uid to 0 so the helper will not error
118 out if pam is called from setuid binary (su, sudo...) */
122 /* exec binary helper */
123 args[0] = x_strdup(CHKPWD_HELPER);
124 args[1] = x_strdup(user);
125 args[2] = x_strdup("chkexpiry");
127 execve(CHKPWD_HELPER, args, envp);
129 pam_syslog(pamh, LOG_ERR, "helper binary execve failed: %m");
130 /* should not get here: exit with error */
131 D(("helper binary is not available"));
133 exit(PAM_AUTHINFO_UNAVAIL);
139 rc=waitpid(child, &retval, 0); /* wait for helper to complete */
141 pam_syslog(pamh, LOG_ERR, "unix_chkpwd waitpid returned %d: %m", rc);
142 retval = PAM_AUTH_ERR;
144 retval = WEXITSTATUS(retval);
145 rc = pam_modutil_read(fds[0], buf, sizeof(buf) - 1);
148 if (sscanf(buf,"%d", daysleft) != 1 )
149 retval = PAM_AUTH_ERR;
152 pam_syslog(pamh, LOG_ERR, "read unix_chkpwd output error %d: %m", rc);
153 retval = PAM_AUTH_ERR;
157 pam_syslog(pamh, LOG_ERR, "Fork failed: %m");
159 retval = PAM_AUTH_ERR;
164 if (off(UNIX_NOREAP, ctrl)) {
165 sigaction(SIGCHLD, &oldsa, NULL); /* restore old signal handler */
168 D(("Returning %d",retval));
173 * PAM framework looks for this entry-point to pass control to the
174 * account management module.
177 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
178 int argc, const char **argv)
181 const void *void_uname;
183 int retval, daysleft;
185 struct passwd *pwent;
190 ctrl = _set_ctrl(pamh, flags, NULL, NULL, argc, argv);
192 retval = pam_get_item(pamh, PAM_USER, &void_uname);
194 D(("user = `%s'", uname));
195 if (retval != PAM_SUCCESS || uname == NULL) {
196 pam_syslog(pamh, LOG_ALERT,
197 "could not identify user (from uid=%lu)",
198 (unsigned long int)getuid());
199 return PAM_USER_UNKNOWN;
202 retval = get_account_info(pamh, uname, &pwent, &spent);
203 if (retval == PAM_USER_UNKNOWN) {
204 pam_syslog(pamh, LOG_ALERT,
205 "could not identify user (from getpwnam(%s))",
210 if (retval == PAM_SUCCESS && spent == NULL)
213 if (retval == PAM_UNIX_RUN_HELPER) {
214 retval = _unix_run_verify_binary(pamh, ctrl, uname, &daysleft);
215 if (retval == PAM_AUTHINFO_UNAVAIL &&
216 on(UNIX_BROKEN_SHADOW, ctrl))
218 } else if (retval != PAM_SUCCESS) {
219 if (on(UNIX_BROKEN_SHADOW,ctrl))
224 retval = check_shadow_expiry(pamh, spent, &daysleft);
227 case PAM_ACCT_EXPIRED:
228 pam_syslog(pamh, LOG_NOTICE,
229 "account %s has expired (account expired)",
231 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
232 _("Your account has expired; please contact your system administrator"));
234 case PAM_NEW_AUTHTOK_REQD:
236 pam_syslog(pamh, LOG_NOTICE,
237 "expired password for user %s (root enforced)",
239 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
240 _("You are required to change your password immediately (root enforced)"));
242 pam_syslog(pamh, LOG_DEBUG,
243 "expired password for user %s (password aged)",
245 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
246 _("You are required to change your password immediately (password aged)"));
249 case PAM_AUTHTOK_EXPIRED:
250 pam_syslog(pamh, LOG_NOTICE,
251 "account %s has expired (failed to change password)",
253 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
254 _("Your account has expired; please contact your system administrator"));
256 case PAM_AUTHTOK_ERR:
257 retval = PAM_SUCCESS;
261 pam_syslog(pamh, LOG_DEBUG,
262 "password for user %s will expire in %d days",
264 #if defined HAVE_DNGETTEXT && defined ENABLE_NLS
265 snprintf (buf, sizeof (buf),
267 "Warning: your password will expire in %d day",
268 "Warning: your password will expire in %d days",
273 snprintf(buf, sizeof (buf),
274 _("Warning: your password will expire in %d day"),
277 snprintf(buf, sizeof (buf),
278 /* TRANSLATORS: only used if dngettext is not supported */
279 _("Warning: your password will expire in %d days"),
282 _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
292 /* static module data */
294 struct pam_module _pam_unix_acct_modstruct = {