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() */
61 #include <sys/resource.h>
63 #include <security/_pam_macros.h>
65 /* indicate the following groups are defined */
68 # include "pam_unix_static.h"
70 # define PAM_SM_PASSWORD
73 #include <security/pam_modules.h>
74 #include <security/pam_ext.h>
75 #include <security/pam_modutil.h>
79 #include "passverify.h"
82 #if (HAVE_YP_GET_DEFAULT_DOMAIN || HAVE_GETDOMAINNAME) && HAVE_YP_MASTER
89 # if HAVE_RPCSVC_YP_PROT_H
90 # include <rpcsvc/yp_prot.h>
93 # if HAVE_RPCSVC_YPCLNT_H
94 # include <rpcsvc/ypclnt.h>
97 # include "yppasswd.h"
99 # if !HAVE_DECL_GETRPCPORT
100 extern int getrpcport(const char *host, unsigned long prognum,
101 unsigned long versnum, unsigned int proto);
102 # endif /* GNU libc 2.1 */
107 Gets in username (has to be done) from the calling program
108 Does authentication of user (only if we are not running as root)
109 Gets new password/checks for sanity
115 #define _UNIX_OLD_AUTHTOK "-UN*X-OLD-PASS"
116 #define _UNIX_NEW_AUTHTOK "-UN*X-NEW-PASS"
118 #define MAX_PASSWD_TRIES 3
121 static char *getNISserver(pam_handle_t *pamh, unsigned int ctrl)
127 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
128 if ((err = yp_get_default_domain(&domainname)) != 0) {
129 pam_syslog(pamh, LOG_WARNING, "can't get local yp domain: %s",
133 #elif defined(HAVE_GETDOMAINNAME)
134 char domainname_res[256];
136 if (getdomainname (domainname_res, sizeof (domainname_res)) == 0)
138 if (strcmp (domainname_res, "(none)") == 0)
140 /* If domainname is not set, some systems will return "(none)" */
141 domainname_res[0] = '\0';
143 domainname = domainname_res;
145 else domainname = NULL;
148 if ((err = yp_master(domainname, "passwd.byname", &master)) != 0) {
149 pam_syslog(pamh, LOG_WARNING, "can't find the master ypserver: %s",
153 port = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE, IPPROTO_UDP);
155 pam_syslog(pamh, LOG_WARNING,
156 "yppasswdd not running on NIS master host");
159 if (port >= IPPORT_RESERVED) {
160 pam_syslog(pamh, LOG_WARNING,
161 "yppasswd daemon running on illegal port");
164 if (on(UNIX_DEBUG, ctrl)) {
165 pam_syslog(pamh, LOG_DEBUG, "Use NIS server on %s with port %d",
174 static int _unix_run_update_binary(pam_handle_t *pamh, unsigned int ctrl, const char *user,
175 const char *fromwhat, const char *towhat, int remember)
177 int retval, child, fds[2];
178 struct sigaction newsa, oldsa;
181 /* create a pipe for the password */
182 if (pipe(fds) != 0) {
183 D(("could not make pipe"));
187 if (off(UNIX_NOREAP, ctrl)) {
189 * This code arranges that the demise of the child does not cause
190 * the application to receive a signal it is not expecting - which
191 * may kill the application or worse.
193 * The "noreap" module argument is provided so that the admin can
194 * override this behavior.
196 memset(&newsa, '\0', sizeof(newsa));
197 newsa.sa_handler = SIG_DFL;
198 sigaction(SIGCHLD, &newsa, &oldsa);
204 static char *envp[] = { NULL };
205 const char *args[] = { NULL, NULL, NULL, NULL, NULL, NULL };
208 /* XXX - should really tidy up PAM here too */
210 /* reopen stdin as pipe */
211 if (dup2(fds[0], STDIN_FILENO) != STDIN_FILENO) {
212 pam_syslog(pamh, LOG_ERR, "dup2 of %s failed: %m", "stdin");
213 _exit(PAM_AUTHINFO_UNAVAIL);
216 if (pam_modutil_sanitize_helper_fds(pamh, PAM_MODUTIL_IGNORE_FD,
218 PAM_MODUTIL_PIPE_FD) < 0) {
219 _exit(PAM_AUTHINFO_UNAVAIL);
222 /* exec binary helper */
223 args[0] = UPDATE_HELPER;
226 if (on(UNIX_SHADOW, ctrl))
231 snprintf(buffer, sizeof(buffer), "%d", remember);
234 execve(UPDATE_HELPER, (char *const *) args, envp);
236 /* should not get here: exit with error */
237 D(("helper binary is not available"));
238 _exit(PAM_AUTHINFO_UNAVAIL);
239 } else if (child > 0) {
241 /* if the stored password is NULL */
244 pam_modutil_write(fds[1], fromwhat, strlen(fromwhat)+1);
246 pam_modutil_write(fds[1], "", 1);
248 pam_modutil_write(fds[1], towhat, strlen(towhat)+1);
251 pam_modutil_write(fds[1], "", 1);
253 close(fds[0]); /* close here to avoid possible SIGPIPE above */
255 /* wait for helper to complete: */
256 while ((rc=waitpid(child, &retval, 0)) < 0 && errno == EINTR);
258 pam_syslog(pamh, LOG_ERR, "unix_update waitpid failed: %m");
259 retval = PAM_AUTHTOK_ERR;
260 } else if (!WIFEXITED(retval)) {
261 pam_syslog(pamh, LOG_ERR, "unix_update abnormal exit: %d", retval);
262 retval = PAM_AUTHTOK_ERR;
264 retval = WEXITSTATUS(retval);
270 retval = PAM_AUTH_ERR;
273 if (off(UNIX_NOREAP, ctrl)) {
274 sigaction(SIGCHLD, &oldsa, NULL); /* restore old signal handler */
281 static int check_old_password(const char *forwho, const char *newpass)
283 static char buf[16384];
284 char *s_luser, *s_uid, *s_npas, *s_pas;
285 int retval = PAM_SUCCESS;
287 size_t len = strlen(forwho);
289 opwfile = fopen(OLD_PASSWORDS_FILE, "r");
293 while (fgets(buf, 16380, opwfile)) {
294 if (!strncmp(buf, forwho, len) && (buf[len] == ':' ||
297 buf[strlen(buf) - 1] = '\0';
298 s_luser = strtok_r(buf, ":,", &sptr);
299 s_uid = strtok_r(NULL, ":,", &sptr);
300 s_npas = strtok_r(NULL, ":,", &sptr);
301 s_pas = strtok_r(NULL, ":,", &sptr);
302 while (s_pas != NULL) {
303 char *md5pass = Goodcrypt_md5(newpass, s_pas);
304 if (!strcmp(md5pass, s_pas)) {
305 _pam_delete(md5pass);
306 retval = PAM_AUTHTOK_ERR;
309 s_pas = strtok_r(NULL, ":,", &sptr);
310 _pam_delete(md5pass);
320 static int _do_setpass(pam_handle_t* pamh, const char *forwho,
321 const char *fromwhat,
322 char *towhat, unsigned int ctrl, int remember)
324 struct passwd *pwd = NULL;
331 pwd = getpwnam(forwho);
334 retval = PAM_AUTHTOK_ERR;
338 if (on(UNIX_NIS, ctrl) && _unix_comesfromsource(pamh, forwho, 0, 1)) {
340 if ((master=getNISserver(pamh, ctrl)) != NULL) {
341 struct timeval timeout;
342 struct yppasswd yppwd;
347 /* Unlock passwd file to avoid deadlock */
351 /* Initialize password information */
352 yppwd.newpw.pw_passwd = pwd->pw_passwd;
353 yppwd.newpw.pw_name = pwd->pw_name;
354 yppwd.newpw.pw_uid = pwd->pw_uid;
355 yppwd.newpw.pw_gid = pwd->pw_gid;
356 yppwd.newpw.pw_gecos = pwd->pw_gecos;
357 yppwd.newpw.pw_dir = pwd->pw_dir;
358 yppwd.newpw.pw_shell = pwd->pw_shell;
359 yppwd.oldpass = fromwhat ? strdup (fromwhat) : strdup ("");
360 yppwd.newpw.pw_passwd = towhat;
362 D(("Set password %s for %s", yppwd.newpw.pw_passwd, forwho));
364 /* The yppasswd.x file said `unix authentication required',
365 * so I added it. This is the only reason it is in here.
366 * My yppasswdd doesn't use it, but maybe some others out there
369 clnt = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
370 clnt->cl_auth = authunix_create_default();
371 memset((char *) &status, '\0', sizeof(status));
374 err = clnt_call(clnt, YPPASSWDPROC_UPDATE,
375 (xdrproc_t) xdr_yppasswd, (char *) &yppwd,
376 (xdrproc_t) xdr_int, (char *) &status,
379 free (yppwd.oldpass);
382 _make_remark(pamh, ctrl, PAM_TEXT_INFO,
385 D(("Error while changing NIS password.\n"));
387 D(("The password has%s been changed on %s.",
388 (err || status) ? " not" : "", master));
389 pam_syslog(pamh, LOG_NOTICE, "password%s changed for %s on %s",
390 (err || status) ? " not" : "", pwd->pw_name, master);
392 auth_destroy(clnt->cl_auth);
395 _make_remark(pamh, ctrl, PAM_TEXT_INFO,
396 _("NIS password could not be changed."));
397 retval = PAM_TRY_AGAIN;
403 retval = PAM_TRY_AGAIN;
406 if (on(UNIX_DEBUG, ctrl)) {
407 pam_syslog(pamh, LOG_DEBUG, "No NIS support available");
410 retval = PAM_TRY_AGAIN;
414 if (_unix_comesfromsource(pamh, forwho, 1, 0)) {
416 if (lock_pwdf() != PAM_SUCCESS) {
417 return PAM_AUTHTOK_LOCK_BUSY;
421 if (unix_selinux_confined())
422 return _unix_run_update_binary(pamh, ctrl, forwho, fromwhat, towhat, remember);
424 /* first, save old password */
425 if (save_old_password(pamh, forwho, fromwhat, remember)) {
426 retval = PAM_AUTHTOK_ERR;
429 if (on(UNIX_SHADOW, ctrl) || is_pwd_shadowed(pwd)) {
430 retval = unix_update_shadow(pamh, forwho, towhat);
431 if (retval == PAM_SUCCESS)
432 if (!is_pwd_shadowed(pwd))
433 retval = unix_update_passwd(pamh, forwho, "x");
435 retval = unix_update_passwd(pamh, forwho, towhat);
446 static int _unix_verify_shadow(pam_handle_t *pamh, const char *user, unsigned int ctrl)
448 struct passwd *pwent = NULL; /* Password and shadow password */
449 struct spwd *spent = NULL; /* file entries for the user */
453 retval = get_account_info(pamh, user, &pwent, &spent);
454 if (retval == PAM_USER_UNKNOWN) {
458 if (retval == PAM_SUCCESS && spent == NULL)
461 if (retval == PAM_UNIX_RUN_HELPER) {
462 retval = _unix_run_verify_binary(pamh, ctrl, user, &daysleft);
463 if (retval == PAM_AUTH_ERR || retval == PAM_USER_UNKNOWN)
466 else if (retval == PAM_SUCCESS)
467 retval = check_shadow_expiry(pamh, spent, &daysleft);
469 if (on(UNIX__IAMROOT, ctrl) || retval == PAM_NEW_AUTHTOK_REQD)
475 static int _pam_unix_approve_pass(pam_handle_t * pamh
477 ,const char *pass_old
478 ,const char *pass_new,
482 const char *remark = NULL;
483 int retval = PAM_SUCCESS;
485 D(("&new=%p, &old=%p", pass_old, pass_new));
486 D(("new=[%s]", pass_new));
487 D(("old=[%s]", pass_old));
489 if (pass_new == NULL || (pass_old && !strcmp(pass_old, pass_new))) {
490 if (on(UNIX_DEBUG, ctrl)) {
491 pam_syslog(pamh, LOG_DEBUG, "bad authentication token");
493 _make_remark(pamh, ctrl, PAM_ERROR_MSG, pass_new == NULL ?
494 _("No password supplied") : _("Password unchanged"));
495 return PAM_AUTHTOK_ERR;
498 * if one wanted to hardwire authentication token strength
499 * checking this would be the place - AGM
502 retval = pam_get_item(pamh, PAM_USER, &user);
503 if (retval != PAM_SUCCESS) {
504 if (on(UNIX_DEBUG, ctrl)) {
505 pam_syslog(pamh, LOG_ERR, "Can not get username");
506 return PAM_AUTHTOK_ERR;
509 if (off(UNIX__IAMROOT, ctrl)) {
510 if (strlen(pass_new) < pass_min_len)
511 remark = _("You must choose a longer password");
512 D(("length check [%s]", remark));
513 if (on(UNIX_REMEMBER_PASSWD, ctrl)) {
514 if ((retval = check_old_password(user, pass_new)) == PAM_AUTHTOK_ERR)
515 remark = _("Password has been already used. Choose another.");
516 if (retval == PAM_ABORT) {
517 pam_syslog(pamh, LOG_ERR, "can't open %s file to check old passwords",
524 _make_remark(pamh, ctrl, PAM_ERROR_MSG, remark);
525 retval = PAM_AUTHTOK_ERR;
531 pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
533 unsigned int ctrl, lctrl;
537 int pass_min_len = 0;
539 /* <DO NOT free() THESE> */
541 const void *pass_old, *pass_new;
542 /* </DO NOT free() THESE> */
546 ctrl = _set_ctrl(pamh, flags, &remember, &rounds, &pass_min_len,
550 * First get the name of a user
552 retval = pam_get_user(pamh, &user, NULL);
553 if (retval == PAM_SUCCESS) {
555 * Various libraries at various times have had bugs related to
556 * '+' or '-' as the first character of a user name. Don't
559 if (user == NULL || user[0] == '-' || user[0] == '+') {
560 pam_syslog(pamh, LOG_ERR, "bad username [%s]", user);
561 return PAM_USER_UNKNOWN;
563 if (retval == PAM_SUCCESS && on(UNIX_DEBUG, ctrl))
564 pam_syslog(pamh, LOG_DEBUG, "username [%s] obtained",
567 if (on(UNIX_DEBUG, ctrl))
568 pam_syslog(pamh, LOG_DEBUG,
569 "password - could not identify user");
573 D(("Got username of %s", user));
576 * Before we do anything else, check to make sure that the user's
577 * info is in one of the databases we can modify from this module,
578 * which currently is 'files' and 'nis'. We have to do this because
579 * getpwnam() doesn't tell you *where* the information it gives you
580 * came from, nor should it. That's our job.
582 if (_unix_comesfromsource(pamh, user, 1, on(UNIX_NIS, ctrl)) == 0) {
583 pam_syslog(pamh, LOG_DEBUG,
584 "user \"%s\" does not exist in /etc/passwd%s",
585 user, on(UNIX_NIS, ctrl) ? " or NIS" : "");
586 return PAM_USER_UNKNOWN;
589 _unix_getpwnam(pamh, user, 1, 1, &pwd);
591 pam_syslog(pamh, LOG_DEBUG,
592 "user \"%s\" has corrupted passwd entry",
594 return PAM_USER_UNKNOWN;
599 * This is not an AUTH module!
601 if (on(UNIX__NONULL, ctrl))
602 set(UNIX__NULLOK, ctrl);
604 if (on(UNIX__PRELIM, ctrl)) {
606 * obtain and verify the current password (OLDAUTHTOK) for
613 if (_unix_blankpasswd(pamh, ctrl, user)) {
615 } else if (off(UNIX__IAMROOT, ctrl) ||
616 (on(UNIX_NIS, ctrl) && _unix_comesfromsource(pamh, user, 0, 1))) {
617 /* instruct user what is happening */
618 if (asprintf(&Announce, _("Changing password for %s."),
620 pam_syslog(pamh, LOG_CRIT,
621 "password - out of memory");
626 set(UNIX__OLD_PASSWD, lctrl);
627 retval = _unix_read_password(pamh, lctrl
629 ,_("(current) UNIX password: ")
635 if (retval != PAM_SUCCESS) {
636 pam_syslog(pamh, LOG_NOTICE,
637 "password - (old) token not obtained");
640 /* verify that this is the password for this user */
642 retval = _unix_verify_password(pamh, user, pass_old, ctrl);
644 D(("process run by root so do nothing this time around"));
646 retval = PAM_SUCCESS; /* root doesn't have too */
649 if (retval != PAM_SUCCESS) {
650 D(("Authentication failed"));
654 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
656 if (retval != PAM_SUCCESS) {
657 pam_syslog(pamh, LOG_CRIT,
658 "failed to set PAM_OLDAUTHTOK");
660 retval = _unix_verify_shadow(pamh,user, ctrl);
661 if (retval == PAM_AUTHTOK_ERR) {
662 if (off(UNIX__IAMROOT, ctrl))
663 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
664 _("You must wait longer to change your password"));
666 retval = PAM_SUCCESS;
668 } else if (on(UNIX__UPDATE, ctrl)) {
670 * tpass is used below to store the _pam_md() return; it
671 * should be _pam_delete()'d.
678 * obtain the proposed password
684 * get the old token back. NULL was ok only if root [at this
685 * point we assume that this has already been enforced on a
686 * previous call to this function].
689 if (off(UNIX_NOT_SET_PASS, ctrl)) {
690 retval = pam_get_item(pamh, PAM_OLDAUTHTOK
693 retval = pam_get_data(pamh, _UNIX_OLD_AUTHTOK
695 if (retval == PAM_NO_MODULE_DATA) {
696 retval = PAM_SUCCESS;
700 D(("pass_old [%s]", pass_old));
702 if (retval != PAM_SUCCESS) {
703 pam_syslog(pamh, LOG_NOTICE, "user not authenticated");
707 D(("get new password now"));
711 if (on(UNIX_USE_AUTHTOK, lctrl)) {
712 set(UNIX_USE_FIRST_PASS, lctrl);
715 retval = PAM_AUTHTOK_ERR;
716 while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
718 * use_authtok is to force the use of a previously entered
719 * password -- needed for pluggable password strength checking
722 retval = _unix_read_password(pamh, lctrl
724 ,_("Enter new UNIX password: ")
725 ,_("Retype new UNIX password: ")
729 if (retval != PAM_SUCCESS) {
730 if (on(UNIX_DEBUG, ctrl)) {
731 pam_syslog(pamh, LOG_ALERT,
732 "password - new password not obtained");
734 pass_old = NULL; /* tidy up */
737 D(("returned to _unix_chauthtok"));
740 * At this point we know who the user is and what they
741 * propose as their new password. Verify that the new
742 * password is acceptable.
745 if (*(const char *)pass_new == '\0') { /* "\0" password = NULL */
748 retval = _pam_unix_approve_pass(pamh, ctrl, pass_old,
749 pass_new, pass_min_len);
751 if (retval != PAM_SUCCESS && off(UNIX_NOT_SET_PASS, ctrl)) {
752 pam_set_item(pamh, PAM_AUTHTOK, NULL);
756 if (retval != PAM_SUCCESS) {
757 pam_syslog(pamh, LOG_NOTICE,
758 "new password not acceptable");
759 pass_new = pass_old = NULL; /* tidy up */
762 if (lock_pwdf() != PAM_SUCCESS) {
763 return PAM_AUTHTOK_LOCK_BUSY;
767 retval = _unix_verify_password(pamh, user, pass_old, ctrl);
768 if (retval != PAM_SUCCESS) {
769 pam_syslog(pamh, LOG_NOTICE, "user password changed by another process");
775 retval = _unix_verify_shadow(pamh, user, ctrl);
776 if (retval != PAM_SUCCESS) {
777 pam_syslog(pamh, LOG_NOTICE, "user shadow entry expired");
782 retval = _pam_unix_approve_pass(pamh, ctrl, pass_old, pass_new,
784 if (retval != PAM_SUCCESS) {
785 pam_syslog(pamh, LOG_NOTICE,
786 "new password not acceptable 2");
787 pass_new = pass_old = NULL; /* tidy up */
793 * By reaching here we have approved the passwords and must now
794 * rebuild the password database file.
798 * First we encrypt the new password.
801 tpass = create_password_hash(pamh, pass_new, ctrl, rounds);
803 pam_syslog(pamh, LOG_CRIT,
804 "crypt() failure or out of memory for password");
805 pass_new = pass_old = NULL; /* tidy up */
810 D(("password processed"));
812 /* update the password database(s) -- race conditions..? */
814 retval = _do_setpass(pamh, user, pass_old, tpass, ctrl,
816 /* _do_setpass has called unlock_pwdf for us */
819 pass_old = pass_new = NULL;
820 } else { /* something has broken with the module */
821 pam_syslog(pamh, LOG_ALERT,
822 "password received unknown request");
826 D(("retval was %d", retval));