2 * Main coding by Elliot Lee <sopwith@redhat.com>, Red Hat Software.
4 * Copyright (c) Jan Rêkorajski, 1999.
5 * Copyright (c) Red Hat, Inc., 2007, 2008.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
20 * ALTERNATIVELY, this product may be distributed under the terms of
21 * the GNU Public License, in which case the provisions of the GPL are
22 * required INSTEAD OF the above restrictions. (This clause is
23 * necessary due to a potential bad interaction between the GPL and
24 * the restrictions contained in a BSD-style copyright.)
26 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
36 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include <sys/types.h>
52 #include <time.h> /* for time() */
58 #include <rpcsvc/yp_prot.h>
59 #include <rpcsvc/ypclnt.h>
65 static int selinux_enabled=-1;
66 #include <selinux/selinux.h>
67 #define SELINUX_ENABLED (selinux_enabled!=-1 ? selinux_enabled : (selinux_enabled=is_selinux_enabled()>0))
70 #include <security/_pam_macros.h>
72 /* indicate the following groups are defined */
74 #define PAM_SM_PASSWORD
76 #include <security/pam_modules.h>
77 #include <security/pam_ext.h>
78 #include <security/pam_modutil.h>
83 #include "passverify.h"
86 #if !((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1))
87 extern int getrpcport(const char *host, unsigned long prognum,
88 unsigned long versnum, unsigned int proto);
89 #endif /* GNU libc 2.1 */
93 Gets in username (has to be done) from the calling program
94 Does authentication of user (only if we are not running as root)
95 Gets new password/checks for sanity
101 #define _UNIX_OLD_AUTHTOK "-UN*X-OLD-PASS"
102 #define _UNIX_NEW_AUTHTOK "-UN*X-NEW-PASS"
104 #define MAX_PASSWD_TRIES 3
106 static char *getNISserver(pam_handle_t *pamh)
112 if ((err = yp_get_default_domain(&domainname)) != 0) {
113 pam_syslog(pamh, LOG_WARNING, "can't get local yp domain: %s",
117 if ((err = yp_master(domainname, "passwd.byname", &master)) != 0) {
118 pam_syslog(pamh, LOG_WARNING, "can't find the master ypserver: %s",
122 port = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE, IPPROTO_UDP);
124 pam_syslog(pamh, LOG_WARNING,
125 "yppasswdd not running on NIS master host");
128 if (port >= IPPORT_RESERVED) {
129 pam_syslog(pamh, LOG_WARNING,
130 "yppasswd daemon running on illegal port");
138 static int _unix_run_update_binary(pam_handle_t *pamh, unsigned int ctrl, const char *user,
139 const char *fromwhat, const char *towhat, int remember)
141 int retval, child, fds[2];
142 struct sigaction newsa, oldsa;
145 /* create a pipe for the password */
146 if (pipe(fds) != 0) {
147 D(("could not make pipe"));
151 if (off(UNIX_NOREAP, ctrl)) {
153 * This code arranges that the demise of the child does not cause
154 * the application to receive a signal it is not expecting - which
155 * may kill the application or worse.
157 * The "noreap" module argument is provided so that the admin can
158 * override this behavior.
160 memset(&newsa, '\0', sizeof(newsa));
161 newsa.sa_handler = SIG_DFL;
162 sigaction(SIGCHLD, &newsa, &oldsa);
170 static char *envp[] = { NULL };
171 char *args[] = { NULL, NULL, NULL, NULL, NULL, NULL };
174 /* XXX - should really tidy up PAM here too */
176 /* reopen stdin as pipe */
177 dup2(fds[0], STDIN_FILENO);
179 if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
180 if (rlim.rlim_max >= MAX_FD_NO)
181 rlim.rlim_max = MAX_FD_NO;
182 for (i=0; i < (int)rlim.rlim_max; i++) {
183 if (i != STDIN_FILENO)
188 if (SELINUX_ENABLED && geteuid() == 0) {
189 /* must set the real uid to 0 so the helper will not error
190 out if pam is called from setuid binary (su, sudo...) */
194 /* exec binary helper */
195 args[0] = x_strdup(UPDATE_HELPER);
196 args[1] = x_strdup(user);
197 args[2] = x_strdup("update");
198 if (on(UNIX_SHADOW, ctrl))
199 args[3] = x_strdup("1");
201 args[3] = x_strdup("0");
203 snprintf(buffer, sizeof(buffer), "%d", remember);
204 args[4] = x_strdup(buffer);
206 execve(UPDATE_HELPER, args, envp);
208 /* should not get here: exit with error */
209 D(("helper binary is not available"));
210 exit(PAM_AUTHINFO_UNAVAIL);
211 } else if (child > 0) {
213 /* if the stored password is NULL */
216 pam_modutil_write(fds[1], fromwhat, strlen(fromwhat)+1);
218 pam_modutil_write(fds[1], "", 1);
220 pam_modutil_write(fds[1], towhat, strlen(towhat)+1);
223 pam_modutil_write(fds[1], "", 1);
225 close(fds[0]); /* close here to avoid possible SIGPIPE above */
227 rc=waitpid(child, &retval, 0); /* wait for helper to complete */
229 pam_syslog(pamh, LOG_ERR, "unix_update waitpid failed: %m");
230 retval = PAM_AUTH_ERR;
232 retval = WEXITSTATUS(retval);
238 retval = PAM_AUTH_ERR;
241 if (off(UNIX_NOREAP, ctrl)) {
242 sigaction(SIGCHLD, &oldsa, NULL); /* restore old signal handler */
249 static int check_old_password(const char *forwho, const char *newpass)
251 static char buf[16384];
252 char *s_luser, *s_uid, *s_npas, *s_pas;
253 int retval = PAM_SUCCESS;
256 opwfile = fopen(OLD_PASSWORDS_FILE, "r");
260 while (fgets(buf, 16380, opwfile)) {
261 if (!strncmp(buf, forwho, strlen(forwho))) {
263 buf[strlen(buf) - 1] = '\0';
264 s_luser = strtok_r(buf, ":,", &sptr);
265 s_uid = strtok_r(NULL, ":,", &sptr);
266 s_npas = strtok_r(NULL, ":,", &sptr);
267 s_pas = strtok_r(NULL, ":,", &sptr);
268 while (s_pas != NULL) {
269 char *md5pass = Goodcrypt_md5(newpass, s_pas);
270 if (!strcmp(md5pass, s_pas)) {
271 _pam_delete(md5pass);
272 retval = PAM_AUTHTOK_ERR;
275 s_pas = strtok_r(NULL, ":,", &sptr);
276 _pam_delete(md5pass);
286 static int _do_setpass(pam_handle_t* pamh, const char *forwho,
287 const char *fromwhat,
288 char *towhat, unsigned int ctrl, int remember)
290 struct passwd *pwd = NULL;
297 pwd = getpwnam(forwho);
300 retval = PAM_AUTHTOK_ERR;
304 if (on(UNIX_NIS, ctrl) && _unix_comesfromsource(pamh, forwho, 0, 1)) {
305 if ((master=getNISserver(pamh)) != NULL) {
306 struct timeval timeout;
307 struct yppasswd yppwd;
312 /* Unlock passwd file to avoid deadlock */
316 /* Initialize password information */
317 yppwd.newpw.pw_passwd = pwd->pw_passwd;
318 yppwd.newpw.pw_name = pwd->pw_name;
319 yppwd.newpw.pw_uid = pwd->pw_uid;
320 yppwd.newpw.pw_gid = pwd->pw_gid;
321 yppwd.newpw.pw_gecos = pwd->pw_gecos;
322 yppwd.newpw.pw_dir = pwd->pw_dir;
323 yppwd.newpw.pw_shell = pwd->pw_shell;
324 yppwd.oldpass = fromwhat ? strdup (fromwhat) : strdup ("");
325 yppwd.newpw.pw_passwd = towhat;
327 D(("Set password %s for %s", yppwd.newpw.pw_passwd, forwho));
329 /* The yppasswd.x file said `unix authentication required',
330 * so I added it. This is the only reason it is in here.
331 * My yppasswdd doesn't use it, but maybe some others out there
334 clnt = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
335 clnt->cl_auth = authunix_create_default();
336 memset((char *) &status, '\0', sizeof(status));
339 err = clnt_call(clnt, YPPASSWDPROC_UPDATE,
340 (xdrproc_t) xdr_yppasswd, (char *) &yppwd,
341 (xdrproc_t) xdr_int, (char *) &status,
344 free (yppwd.oldpass);
347 _make_remark(pamh, ctrl, PAM_TEXT_INFO,
350 D(("Error while changing NIS password.\n"));
352 D(("The password has%s been changed on %s.",
353 (err || status) ? " not" : "", master));
354 pam_syslog(pamh, LOG_NOTICE, "password%s changed for %s on %s",
355 (err || status) ? " not" : "", pwd->pw_name, master);
357 auth_destroy(clnt->cl_auth);
360 _make_remark(pamh, ctrl, PAM_TEXT_INFO,
361 _("NIS password could not be changed."));
362 retval = PAM_TRY_AGAIN;
368 retval = PAM_TRY_AGAIN;
372 if (_unix_comesfromsource(pamh, forwho, 1, 0)) {
374 if (lock_pwdf() != PAM_SUCCESS) {
375 return PAM_AUTHTOK_LOCK_BUSY;
379 if (unix_selinux_confined())
380 return _unix_run_update_binary(pamh, ctrl, forwho, fromwhat, towhat, remember);
382 /* first, save old password */
383 if (save_old_password(pamh, forwho, fromwhat, remember)) {
384 retval = PAM_AUTHTOK_ERR;
387 if (on(UNIX_SHADOW, ctrl) || is_pwd_shadowed(pwd)) {
388 retval = unix_update_shadow(pamh, forwho, towhat);
389 if (retval == PAM_SUCCESS)
390 if (!is_pwd_shadowed(pwd))
391 retval = unix_update_passwd(pamh, forwho, "x");
393 retval = unix_update_passwd(pamh, forwho, towhat);
404 static int _unix_verify_shadow(pam_handle_t *pamh, const char *user, unsigned int ctrl)
406 struct passwd *pwent = NULL; /* Password and shadow password */
407 struct spwd *spent = NULL; /* file entries for the user */
411 retval = get_account_info(pamh, user, &pwent, &spent);
412 if (retval == PAM_USER_UNKNOWN) {
416 if (retval == PAM_SUCCESS && spent == NULL)
419 if (retval == PAM_UNIX_RUN_HELPER) {
420 retval = _unix_run_verify_binary(pamh, ctrl, user, &daysleft);
421 if (retval == PAM_AUTH_ERR || retval == PAM_USER_UNKNOWN)
424 else if (retval == PAM_SUCCESS)
425 retval = check_shadow_expiry(pamh, spent, &daysleft);
427 if (on(UNIX__IAMROOT, ctrl) || retval == PAM_NEW_AUTHTOK_REQD)
433 static int _pam_unix_approve_pass(pam_handle_t * pamh
435 ,const char *pass_old
436 ,const char *pass_new)
439 const char *remark = NULL;
440 int retval = PAM_SUCCESS;
442 D(("&new=%p, &old=%p", pass_old, pass_new));
443 D(("new=[%s]", pass_new));
444 D(("old=[%s]", pass_old));
446 if (pass_new == NULL || (pass_old && !strcmp(pass_old, pass_new))) {
447 if (on(UNIX_DEBUG, ctrl)) {
448 pam_syslog(pamh, LOG_DEBUG, "bad authentication token");
450 _make_remark(pamh, ctrl, PAM_ERROR_MSG, pass_new == NULL ?
451 _("No password supplied") : _("Password unchanged"));
452 return PAM_AUTHTOK_ERR;
455 * if one wanted to hardwire authentication token strength
456 * checking this would be the place - AGM
459 retval = pam_get_item(pamh, PAM_USER, &user);
460 if (retval != PAM_SUCCESS) {
461 if (on(UNIX_DEBUG, ctrl)) {
462 pam_syslog(pamh, LOG_ERR, "Can not get username");
463 return PAM_AUTHTOK_ERR;
466 if (off(UNIX__IAMROOT, ctrl)) {
467 if (strlen(pass_new) < 6)
468 remark = _("You must choose a longer password");
469 D(("length check [%s]", remark));
470 if (on(UNIX_REMEMBER_PASSWD, ctrl)) {
471 if ((retval = check_old_password(user, pass_new)) == PAM_AUTHTOK_ERR)
472 remark = _("Password has been already used. Choose another.");
473 if (retval == PAM_ABORT) {
474 pam_syslog(pamh, LOG_ERR, "can't open %s file to check old passwords",
481 _make_remark(pamh, ctrl, PAM_ERROR_MSG, remark);
482 retval = PAM_AUTHTOK_ERR;
488 PAM_EXTERN int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
489 int argc, const char **argv)
491 unsigned int ctrl, lctrl;
496 /* <DO NOT free() THESE> */
498 const void *pass_old, *pass_new;
499 /* </DO NOT free() THESE> */
503 ctrl = _set_ctrl(pamh, flags, &remember, &rounds, argc, argv);
506 * First get the name of a user
508 retval = pam_get_user(pamh, &user, NULL);
509 if (retval == PAM_SUCCESS) {
511 * Various libraries at various times have had bugs related to
512 * '+' or '-' as the first character of a user name. Don't
515 if (user == NULL || user[0] == '-' || user[0] == '+') {
516 pam_syslog(pamh, LOG_ERR, "bad username [%s]", user);
517 return PAM_USER_UNKNOWN;
519 if (retval == PAM_SUCCESS && on(UNIX_DEBUG, ctrl))
520 pam_syslog(pamh, LOG_DEBUG, "username [%s] obtained",
523 if (on(UNIX_DEBUG, ctrl))
524 pam_syslog(pamh, LOG_DEBUG,
525 "password - could not identify user");
529 D(("Got username of %s", user));
532 * Before we do anything else, check to make sure that the user's
533 * info is in one of the databases we can modify from this module,
534 * which currently is 'files' and 'nis'. We have to do this because
535 * getpwnam() doesn't tell you *where* the information it gives you
536 * came from, nor should it. That's our job.
538 if (_unix_comesfromsource(pamh, user, 1, on(UNIX_NIS, ctrl)) == 0) {
539 pam_syslog(pamh, LOG_DEBUG,
540 "user \"%s\" does not exist in /etc/passwd%s",
541 user, on(UNIX_NIS, ctrl) ? " or NIS" : "");
542 return PAM_USER_UNKNOWN;
545 _unix_getpwnam(pamh, user, 1, 1, &pwd);
547 pam_syslog(pamh, LOG_DEBUG,
548 "user \"%s\" has corrupted passwd entry",
550 return PAM_USER_UNKNOWN;
555 * This is not an AUTH module!
557 if (on(UNIX__NONULL, ctrl))
558 set(UNIX__NULLOK, ctrl);
560 if (on(UNIX__PRELIM, ctrl)) {
562 * obtain and verify the current password (OLDAUTHTOK) for
569 if (_unix_blankpasswd(pamh, ctrl, user)) {
571 } else if (off(UNIX__IAMROOT, ctrl)) {
572 /* instruct user what is happening */
573 if (asprintf(&Announce, _("Changing password for %s."),
575 pam_syslog(pamh, LOG_CRIT,
576 "password - out of memory");
581 set(UNIX__OLD_PASSWD, lctrl);
582 retval = _unix_read_password(pamh, lctrl
584 ,_("(current) UNIX password: ")
590 if (retval != PAM_SUCCESS) {
591 pam_syslog(pamh, LOG_NOTICE,
592 "password - (old) token not obtained");
595 /* verify that this is the password for this user */
597 retval = _unix_verify_password(pamh, user, pass_old, ctrl);
599 D(("process run by root so do nothing this time around"));
601 retval = PAM_SUCCESS; /* root doesn't have too */
604 if (retval != PAM_SUCCESS) {
605 D(("Authentication failed"));
609 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
611 if (retval != PAM_SUCCESS) {
612 pam_syslog(pamh, LOG_CRIT,
613 "failed to set PAM_OLDAUTHTOK");
615 retval = _unix_verify_shadow(pamh,user, ctrl);
616 if (retval == PAM_AUTHTOK_ERR) {
617 if (off(UNIX__IAMROOT, ctrl))
618 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
619 _("You must wait longer to change your password"));
621 retval = PAM_SUCCESS;
623 } else if (on(UNIX__UPDATE, ctrl)) {
625 * tpass is used below to store the _pam_md() return; it
626 * should be _pam_delete()'d.
633 * obtain the proposed password
639 * get the old token back. NULL was ok only if root [at this
640 * point we assume that this has already been enforced on a
641 * previous call to this function].
644 if (off(UNIX_NOT_SET_PASS, ctrl)) {
645 retval = pam_get_item(pamh, PAM_OLDAUTHTOK
648 retval = pam_get_data(pamh, _UNIX_OLD_AUTHTOK
650 if (retval == PAM_NO_MODULE_DATA) {
651 retval = PAM_SUCCESS;
655 D(("pass_old [%s]", pass_old));
657 if (retval != PAM_SUCCESS) {
658 pam_syslog(pamh, LOG_NOTICE, "user not authenticated");
662 D(("get new password now"));
666 if (on(UNIX_USE_AUTHTOK, lctrl)) {
667 set(UNIX_USE_FIRST_PASS, lctrl);
670 retval = PAM_AUTHTOK_ERR;
671 while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
673 * use_authtok is to force the use of a previously entered
674 * password -- needed for pluggable password strength checking
677 retval = _unix_read_password(pamh, lctrl
679 ,_("Enter new UNIX password: ")
680 ,_("Retype new UNIX password: ")
684 if (retval != PAM_SUCCESS) {
685 if (on(UNIX_DEBUG, ctrl)) {
686 pam_syslog(pamh, LOG_ALERT,
687 "password - new password not obtained");
689 pass_old = NULL; /* tidy up */
692 D(("returned to _unix_chauthtok"));
695 * At this point we know who the user is and what they
696 * propose as their new password. Verify that the new
697 * password is acceptable.
700 if (*(const char *)pass_new == '\0') { /* "\0" password = NULL */
703 retval = _pam_unix_approve_pass(pamh, ctrl, pass_old, pass_new);
705 if (retval != PAM_SUCCESS && off(UNIX_NOT_SET_PASS, ctrl)) {
706 pam_set_item(pamh, PAM_AUTHTOK, NULL);
710 if (retval != PAM_SUCCESS) {
711 pam_syslog(pamh, LOG_NOTICE,
712 "new password not acceptable");
713 pass_new = pass_old = NULL; /* tidy up */
716 if (lock_pwdf() != PAM_SUCCESS) {
717 return PAM_AUTHTOK_LOCK_BUSY;
721 retval = _unix_verify_password(pamh, user, pass_old, ctrl);
722 if (retval != PAM_SUCCESS) {
723 pam_syslog(pamh, LOG_NOTICE, "user password changed by another process");
729 retval = _unix_verify_shadow(pamh, user, ctrl);
730 if (retval != PAM_SUCCESS) {
731 pam_syslog(pamh, LOG_NOTICE, "user shadow entry expired");
736 retval = _pam_unix_approve_pass(pamh, ctrl, pass_old, pass_new);
737 if (retval != PAM_SUCCESS) {
738 pam_syslog(pamh, LOG_NOTICE,
739 "new password not acceptable 2");
740 pass_new = pass_old = NULL; /* tidy up */
746 * By reaching here we have approved the passwords and must now
747 * rebuild the password database file.
751 * First we encrypt the new password.
754 tpass = create_password_hash(pamh, pass_new, ctrl, rounds);
756 pam_syslog(pamh, LOG_CRIT,
757 "out of memory for password");
758 pass_new = pass_old = NULL; /* tidy up */
763 D(("password processed"));
765 /* update the password database(s) -- race conditions..? */
767 retval = _do_setpass(pamh, user, pass_old, tpass, ctrl,
769 /* _do_setpass has called unlock_pwdf for us */
772 pass_old = pass_new = NULL;
773 } else { /* something has broken with the module */
774 pam_syslog(pamh, LOG_ALERT,
775 "password received unknown request");
779 D(("retval was %d", retval));
785 /* static module data */
787 struct pam_module _pam_unix_passwd_modstruct = {