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() */
62 #include <security/_pam_macros.h>
64 /* indicate the following groups are defined */
67 # include "pam_unix_static.h"
69 # define PAM_SM_PASSWORD
72 #include <security/pam_modules.h>
73 #include <security/pam_ext.h>
74 #include <security/pam_modutil.h>
78 #include "passverify.h"
81 #if (HAVE_YP_GET_DEFAULT_DOMAIN || HAVE_GETDOMAINNAME) && HAVE_YP_MASTER
88 # if HAVE_RPCSVC_YP_PROT_H
89 # include <rpcsvc/yp_prot.h>
92 # if HAVE_RPCSVC_YPCLNT_H
93 # include <rpcsvc/ypclnt.h>
96 # include "yppasswd.h"
98 # if !HAVE_DECL_GETRPCPORT
99 extern int getrpcport(const char *host, unsigned long prognum,
100 unsigned long versnum, unsigned int proto);
101 # endif /* GNU libc 2.1 */
106 Gets in username (has to be done) from the calling program
107 Does authentication of user (only if we are not running as root)
108 Gets new password/checks for sanity
114 #define _UNIX_OLD_AUTHTOK "-UN*X-OLD-PASS"
115 #define _UNIX_NEW_AUTHTOK "-UN*X-NEW-PASS"
117 #define MAX_PASSWD_TRIES 3
120 static char *getNISserver(pam_handle_t *pamh, unsigned int ctrl)
126 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
127 if ((err = yp_get_default_domain(&domainname)) != 0) {
128 pam_syslog(pamh, LOG_WARNING, "can't get local yp domain: %s",
132 #elif defined(HAVE_GETDOMAINNAME)
133 char domainname_res[256];
135 if (getdomainname (domainname_res, sizeof (domainname_res)) == 0)
137 if (strcmp (domainname_res, "(none)") == 0)
139 /* If domainname is not set, some systems will return "(none)" */
140 domainname_res[0] = '\0';
142 domainname = domainname_res;
144 else domainname = NULL;
147 if ((err = yp_master(domainname, "passwd.byname", &master)) != 0) {
148 pam_syslog(pamh, LOG_WARNING, "can't find the master ypserver: %s",
152 port = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE, IPPROTO_UDP);
154 pam_syslog(pamh, LOG_WARNING,
155 "yppasswdd not running on NIS master host");
158 if (port >= IPPORT_RESERVED) {
159 pam_syslog(pamh, LOG_WARNING,
160 "yppasswd daemon running on illegal port");
163 if (on(UNIX_DEBUG, ctrl)) {
164 pam_syslog(pamh, LOG_DEBUG, "Use NIS server on %s with port %d",
173 static int _unix_run_update_binary(pam_handle_t *pamh, unsigned int ctrl, const char *user,
174 const char *fromwhat, const char *towhat, int remember)
176 int retval, child, fds[2];
177 struct sigaction newsa, oldsa;
180 /* create a pipe for the password */
181 if (pipe(fds) != 0) {
182 D(("could not make pipe"));
186 if (off(UNIX_NOREAP, ctrl)) {
188 * This code arranges that the demise of the child does not cause
189 * the application to receive a signal it is not expecting - which
190 * may kill the application or worse.
192 * The "noreap" module argument is provided so that the admin can
193 * override this behavior.
195 memset(&newsa, '\0', sizeof(newsa));
196 newsa.sa_handler = SIG_DFL;
197 sigaction(SIGCHLD, &newsa, &oldsa);
205 static char *envp[] = { NULL };
206 char *args[] = { NULL, NULL, NULL, NULL, NULL, NULL };
209 /* XXX - should really tidy up PAM here too */
211 /* reopen stdin as pipe */
212 dup2(fds[0], STDIN_FILENO);
214 if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
215 if (rlim.rlim_max >= MAX_FD_NO)
216 rlim.rlim_max = MAX_FD_NO;
217 for (i=0; i < (int)rlim.rlim_max; i++) {
218 if (i != STDIN_FILENO)
223 /* exec binary helper */
224 args[0] = x_strdup(UPDATE_HELPER);
225 args[1] = x_strdup(user);
226 args[2] = x_strdup("update");
227 if (on(UNIX_SHADOW, ctrl))
228 args[3] = x_strdup("1");
230 args[3] = x_strdup("0");
232 snprintf(buffer, sizeof(buffer), "%d", remember);
233 args[4] = x_strdup(buffer);
235 execve(UPDATE_HELPER, args, envp);
237 /* should not get here: exit with error */
238 D(("helper binary is not available"));
239 _exit(PAM_AUTHINFO_UNAVAIL);
240 } else if (child > 0) {
242 /* if the stored password is NULL */
245 pam_modutil_write(fds[1], fromwhat, strlen(fromwhat)+1);
247 pam_modutil_write(fds[1], "", 1);
249 pam_modutil_write(fds[1], towhat, strlen(towhat)+1);
252 pam_modutil_write(fds[1], "", 1);
254 close(fds[0]); /* close here to avoid possible SIGPIPE above */
256 rc=waitpid(child, &retval, 0); /* wait for helper to complete */
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 /* instruct user what is happening */
617 if (asprintf(&Announce, _("Changing password for %s."),
619 pam_syslog(pamh, LOG_CRIT,
620 "password - out of memory");
625 set(UNIX__OLD_PASSWD, lctrl);
626 retval = _unix_read_password(pamh, lctrl
628 ,_("(current) UNIX password: ")
634 if (retval != PAM_SUCCESS) {
635 pam_syslog(pamh, LOG_NOTICE,
636 "password - (old) token not obtained");
639 /* verify that this is the password for this user */
641 retval = _unix_verify_password(pamh, user, pass_old, ctrl);
643 D(("process run by root so do nothing this time around"));
645 retval = PAM_SUCCESS; /* root doesn't have too */
648 if (retval != PAM_SUCCESS) {
649 D(("Authentication failed"));
653 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
655 if (retval != PAM_SUCCESS) {
656 pam_syslog(pamh, LOG_CRIT,
657 "failed to set PAM_OLDAUTHTOK");
659 retval = _unix_verify_shadow(pamh,user, ctrl);
660 if (retval == PAM_AUTHTOK_ERR) {
661 if (off(UNIX__IAMROOT, ctrl))
662 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
663 _("You must wait longer to change your password"));
665 retval = PAM_SUCCESS;
667 } else if (on(UNIX__UPDATE, ctrl)) {
669 * tpass is used below to store the _pam_md() return; it
670 * should be _pam_delete()'d.
677 * obtain the proposed password
683 * get the old token back. NULL was ok only if root [at this
684 * point we assume that this has already been enforced on a
685 * previous call to this function].
688 if (off(UNIX_NOT_SET_PASS, ctrl)) {
689 retval = pam_get_item(pamh, PAM_OLDAUTHTOK
692 retval = pam_get_data(pamh, _UNIX_OLD_AUTHTOK
694 if (retval == PAM_NO_MODULE_DATA) {
695 retval = PAM_SUCCESS;
699 D(("pass_old [%s]", pass_old));
701 if (retval != PAM_SUCCESS) {
702 pam_syslog(pamh, LOG_NOTICE, "user not authenticated");
706 D(("get new password now"));
710 if (on(UNIX_USE_AUTHTOK, lctrl)) {
711 set(UNIX_USE_FIRST_PASS, lctrl);
714 retval = PAM_AUTHTOK_ERR;
715 while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
717 * use_authtok is to force the use of a previously entered
718 * password -- needed for pluggable password strength checking
721 retval = _unix_read_password(pamh, lctrl
723 ,_("Enter new UNIX password: ")
724 ,_("Retype new UNIX password: ")
728 if (retval != PAM_SUCCESS) {
729 if (on(UNIX_DEBUG, ctrl)) {
730 pam_syslog(pamh, LOG_ALERT,
731 "password - new password not obtained");
733 pass_old = NULL; /* tidy up */
736 D(("returned to _unix_chauthtok"));
739 * At this point we know who the user is and what they
740 * propose as their new password. Verify that the new
741 * password is acceptable.
744 if (*(const char *)pass_new == '\0') { /* "\0" password = NULL */
747 retval = _pam_unix_approve_pass(pamh, ctrl, pass_old,
748 pass_new, pass_min_len);
750 if (retval != PAM_SUCCESS && off(UNIX_NOT_SET_PASS, ctrl)) {
751 pam_set_item(pamh, PAM_AUTHTOK, NULL);
755 if (retval != PAM_SUCCESS) {
756 pam_syslog(pamh, LOG_NOTICE,
757 "new password not acceptable");
758 pass_new = pass_old = NULL; /* tidy up */
761 if (lock_pwdf() != PAM_SUCCESS) {
762 return PAM_AUTHTOK_LOCK_BUSY;
766 retval = _unix_verify_password(pamh, user, pass_old, ctrl);
767 if (retval != PAM_SUCCESS) {
768 pam_syslog(pamh, LOG_NOTICE, "user password changed by another process");
774 retval = _unix_verify_shadow(pamh, user, ctrl);
775 if (retval != PAM_SUCCESS) {
776 pam_syslog(pamh, LOG_NOTICE, "user shadow entry expired");
781 retval = _pam_unix_approve_pass(pamh, ctrl, pass_old, pass_new,
783 if (retval != PAM_SUCCESS) {
784 pam_syslog(pamh, LOG_NOTICE,
785 "new password not acceptable 2");
786 pass_new = pass_old = NULL; /* tidy up */
792 * By reaching here we have approved the passwords and must now
793 * rebuild the password database file.
797 * First we encrypt the new password.
800 tpass = create_password_hash(pamh, pass_new, ctrl, rounds);
802 pam_syslog(pamh, LOG_CRIT,
803 "crypt() failure or out of memory for password");
804 pass_new = pass_old = NULL; /* tidy up */
809 D(("password processed"));
811 /* update the password database(s) -- race conditions..? */
813 retval = _do_setpass(pamh, user, pass_old, tpass, ctrl,
815 /* _do_setpass has called unlock_pwdf for us */
818 pass_old = pass_new = NULL;
819 } else { /* something has broken with the module */
820 pam_syslog(pamh, LOG_ALERT,
821 "password received unknown request");
825 D(("retval was %d", retval));