]> granicus.if.org Git - linux-pam/blobdiff - modules/pam_unix/support.c
Relevant BUGIDs: debian#514437 rhbz#487216
[linux-pam] / modules / pam_unix / support.c
index 3b1feb96464d86f8f27c78f9cb4851aa8f41eeff..faec20dc4c879e92c5163eb00e947e2f8767c802 100644 (file)
@@ -1,14 +1,13 @@
-/* 
- * $Id$
- *
+/*
  * Copyright information at end of file.
  */
 
-#define _BSD_SOURCE
+#include "config.h"
 
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <stdio.h>
 #include <string.h>
 #include <malloc.h>
 #include <pwd.h>
 #include <utmp.h>
 #include <errno.h>
 #include <signal.h>
+#include <ctype.h>
+#include <syslog.h>
+#include <sys/resource.h>
+#include <rpcsvc/ypclnt.h>
 
 #include <security/_pam_macros.h>
 #include <security/pam_modules.h>
-#include <security/_pam_modutil.h>
+#include <security/pam_ext.h>
+#include <security/pam_modutil.h>
 
-#include "md5.h"
 #include "support.h"
-
-extern char *crypt(const char *key, const char *salt);
-extern char *bigcrypt(const char *key, const char *salt);
-
-/* syslogging function for errors and other information */
-
-void _log_err(int err, pam_handle_t *pamh, const char *format,...)
-{
-       char *service = NULL;
-       char logname[256];
-       va_list args;
-
-       pam_get_item(pamh, PAM_SERVICE, (const void **) &service);
-       if (service) {
-               strncpy(logname, service, sizeof(logname));
-               logname[sizeof(logname) - 1 - strlen("(pam_unix)")] = '\0';
-               strncat(logname, "(pam_unix)", strlen("(pam_unix)"));
-       } else {
-               strncpy(logname, "pam_unix", sizeof(logname) - 1);
-       }
-
-       va_start(args, format);
-       openlog(logname, LOG_CONS | LOG_PID, LOG_AUTH);
-       vsyslog(err, format, args);
-       va_end(args);
-       closelog();
-}
+#include "passverify.h"
+#ifdef WITH_SELINUX
+#include <selinux/selinux.h>
+#define SELINUX_ENABLED is_selinux_enabled()>0
+#else
+#define SELINUX_ENABLED 0
+#endif
 
 /* this is a front-end for module-application conversations */
 
-static int converse(pam_handle_t * pamh, int ctrl, int nargs
-                   ,struct pam_message **message
-                   ,struct pam_response **response)
-{
-       int retval;
-       struct pam_conv *conv;
-
-       D(("begin to converse"));
-
-       retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
-       if (retval == PAM_SUCCESS) {
-
-               retval = conv->conv(nargs, (const struct pam_message **) message
-                                   ,response, conv->appdata_ptr);
-
-               D(("returned from application's conversation function"));
-
-               if (retval != PAM_SUCCESS && on(UNIX_DEBUG, ctrl)) {
-                       _log_err(LOG_DEBUG, pamh, "conversation failure [%s]"
-                                ,pam_strerror(pamh, retval));
-               }
-       } else if (retval != PAM_CONV_AGAIN) {
-               _log_err(LOG_ERR, pamh
-                        ,"couldn't obtain coversation function [%s]"
-                        ,pam_strerror(pamh, retval));
-       }
-       D(("ready to return from module conversation"));
-
-       return retval;          /* propagate error status */
-}
-
-int _make_remark(pam_handle_t * pamh, unsigned int ctrl
-                      ,int type, const char *text)
+int _make_remark(pam_handle_t * pamh, unsigned int ctrl,
+                   int type, const char *text)
 {
        int retval = PAM_SUCCESS;
 
        if (off(UNIX__QUIET, ctrl)) {
-               struct pam_message *pmsg[1], msg[1];
-               struct pam_response *resp;
-
-               pmsg[0] = &msg[0];
-               msg[0].msg = text;
-               msg[0].msg_style = type;
-
-               resp = NULL;
-               retval = converse(pamh, ctrl, 1, pmsg, &resp);
-
-               if (resp) {
-                       _pam_drop_reply(resp, 1);
-               }
+               retval = pam_prompt(pamh, type, NULL, "%s", text);
        }
        return retval;
 }
@@ -112,8 +52,8 @@ int _make_remark(pam_handle_t * pamh, unsigned int ctrl
  * set the control flags for the UNIX module.
  */
 
-int _set_ctrl(pam_handle_t *pamh, int flags, int *remember, int argc,
-              const char **argv)
+int _set_ctrl(pam_handle_t *pamh, int flags, int *remember, int *rounds,
+             int argc, const char **argv)
 {
        unsigned int ctrl;
 
@@ -135,10 +75,6 @@ int _set_ctrl(pam_handle_t *pamh, int flags, int *remember, int argc,
                D(("PRELIM_CHECK"));
                set(UNIX__PRELIM, ctrl);
        }
-       if (flags & PAM_DISALLOW_NULL_AUTHTOK) {
-               D(("DISALLOW_NULL_AUTHTOK"));
-               set(UNIX__NONULL, ctrl);
-       }
        if (flags & PAM_SILENT) {
                D(("SILENT"));
                set(UNIX__QUIET, ctrl);
@@ -158,7 +94,7 @@ int _set_ctrl(pam_handle_t *pamh, int flags, int *remember, int argc,
                }
 
                if (j >= UNIX_CTRLS_) {
-                       _log_err(LOG_ERR, pamh,
+                       pam_syslog(pamh, LOG_ERR,
                                 "unrecognized option [%s]", *argv);
                } else {
                        ctrl &= unix_args[j].mask;      /* for turning things off */
@@ -173,11 +109,38 @@ int _set_ctrl(pam_handle_t *pamh, int flags, int *remember, int argc,
                                                *remember = 400;
                                }
                        }
+                       if (rounds != NULL && j == UNIX_ALGO_ROUNDS)
+                               *rounds = strtol(*argv + 7, NULL, 10);
                }
 
                ++argv;         /* step to next argument */
        }
 
+       if (flags & PAM_DISALLOW_NULL_AUTHTOK) {
+               D(("DISALLOW_NULL_AUTHTOK"));
+               set(UNIX__NONULL, ctrl);
+       }
+       
+       /* Set default rounds for blowfish */
+       if (on(UNIX_BLOWFISH_PASS, ctrl) && off(UNIX_ALGO_ROUNDS, ctrl)) {
+               *rounds = 5;
+               set(UNIX_ALGO_ROUNDS, ctrl);
+       }
+       
+       /* Enforce sane "rounds" values */
+       if (on(UNIX_ALGO_ROUNDS, ctrl)) {
+               if (on(UNIX_BLOWFISH_PASS, ctrl)) {
+                       if (*rounds < 4 || *rounds > 31)
+                               *rounds = 5;
+               } else if (on(UNIX_SHA256_PASS, ctrl) || on(UNIX_SHA512_PASS, ctrl)) {
+                       if ((*rounds < 1000) || (*rounds == INT_MAX))
+                               /* don't care about bogus values */
+                               unset(UNIX_ALGO_ROUNDS, ctrl);
+                       if (*rounds >= 10000000)
+                               *rounds = 9999999;
+               }
+       }
+
        /* auditing is a more sensitive version of debug */
 
        if (on(UNIX_AUDIT, ctrl)) {
@@ -189,7 +152,7 @@ int _set_ctrl(pam_handle_t *pamh, int flags, int *remember, int argc,
        return ctrl;
 }
 
-static void _cleanup(pam_handle_t * pamh, void *x, int error_status)
+static void _cleanup(pam_handle_t * pamh UNUSED, void *x, int error_status UNUSED)
 {
        _pam_delete(x);
 }
@@ -221,10 +184,10 @@ struct _pam_failed_auth {
 static void _cleanup_failures(pam_handle_t * pamh, void *fl, int err)
 {
        int quiet;
-       const char *service = NULL;
-       const char *ruser = NULL;
-       const char *rhost = NULL;
-       const char *tty = NULL;
+       const void *service = NULL;
+       const void *ruser = NULL;
+       const void *rhost = NULL;
+       const void *tty = NULL;
        struct _pam_failed_auth *failure;
 
        D(("called"));
@@ -240,32 +203,32 @@ static void _cleanup_failures(pam_handle_t * pamh, void *fl, int err)
                        /* log the number of authentication failures */
                        if (failure->count > 1) {
                                (void) pam_get_item(pamh, PAM_SERVICE,
-                                                   (const void **)&service);
+                                                   &service);
                                (void) pam_get_item(pamh, PAM_RUSER,
-                                                   (const void **)&ruser);
+                                                   &ruser);
                                (void) pam_get_item(pamh, PAM_RHOST,
-                                                   (const void **)&rhost);
+                                                   &rhost);
                                (void) pam_get_item(pamh, PAM_TTY,
-                                                   (const void **)&tty);
-                               _log_err(LOG_NOTICE, pamh,
+                                                   &tty);
+                               pam_syslog(pamh, LOG_NOTICE,
                                         "%d more authentication failure%s; "
                                         "logname=%s uid=%d euid=%d "
                                         "tty=%s ruser=%s rhost=%s "
                                         "%s%s",
                                         failure->count - 1, failure->count == 2 ? "" : "s",
                                         failure->name, failure->uid, failure->euid,
-                                        tty ? tty : "", ruser ? ruser : "",
-                                        rhost ? rhost : "",
+                                        tty ? (const char *)tty : "", ruser ? (const char *)ruser : "",
+                                        rhost ? (const char *)rhost : "",
                                         (failure->user && failure->user[0] != '\0')
                                          ? " user=" : "", failure->user
                                );
 
                                if (failure->count > UNIX_MAX_RETRIES) {
-                                       _log_err(LOG_ALERT, pamh
-                                                ,"service(%s) ignoring max retries; %d > %d"
-                                                ,service == NULL ? "**unknown**" : service
-                                                ,failure->count
-                                                ,UNIX_MAX_RETRIES);
+                                       pam_syslog(pamh, LOG_ALERT,
+                                                "service(%s) ignoring max retries; %d > %d",
+                                                service == NULL ? "**unknown**" : (const char *)service,
+                                                failure->count,
+                                                UNIX_MAX_RETRIES);
                                }
                        }
                }
@@ -276,123 +239,162 @@ static void _cleanup_failures(pam_handle_t * pamh, void *fl, int err)
 }
 
 /*
- * _unix_blankpasswd() is a quick check for a blank password
- *
- * returns TRUE if user does not have a password
- * - to avoid prompting for one in such cases (CG)
+ * _unix_getpwnam() searches only /etc/passwd and NIS to find user information
  */
+static void _unix_cleanup(pam_handle_t *pamh UNUSED, void *data, int error_status UNUSED)
+{
+       free(data);
+}
 
-int _unix_blankpasswd(unsigned int ctrl, const char *name)
+int _unix_getpwnam(pam_handle_t *pamh, const char *name,
+                  int files, int nis, struct passwd **ret)
 {
-       struct passwd *pwd = NULL;
-       struct spwd *spwdent = NULL;
-       char *salt = NULL;
-       int retval;
-#if HAVE_GETPWNAM_R
-       char *buf = NULL;
-       int bufsize = 0;
-       struct passwd pwd_buf;
+       FILE *passwd;
+       char buf[16384];
+       int matched = 0, buflen;
+       char *slogin, *spasswd, *suid, *sgid, *sgecos, *shome, *sshell, *p;
+
+       memset(buf, 0, sizeof(buf));
+
+       if (!matched && files) {
+               int userlen = strlen(name);
+               passwd = fopen("/etc/passwd", "r");
+               if (passwd != NULL) {
+                       while (fgets(buf, sizeof(buf), passwd) != NULL) {
+                               if ((buf[userlen] == ':') &&
+                                   (strncmp(name, buf, userlen) == 0)) {
+                                       p = buf + strlen(buf) - 1;
+                                       while (isspace(*p) && (p >= buf)) {
+                                               *p-- = '\0';
+                                       }
+                                       matched = 1;
+                                       break;
+                               }
+                       }
+                       fclose(passwd);
+               }
+       }
 
-       pwd = &pwd_buf;
-#endif
+       if (!matched && nis) {
+               char *userinfo = NULL, *domain = NULL;
+               int len = 0, i;
+               len = yp_get_default_domain(&domain);
+               if (len == YPERR_SUCCESS) {
+                       len = yp_bind(domain);
+               }
+               if (len == YPERR_SUCCESS) {
+                       i = yp_match(domain, "passwd.byname", name,
+                                    strlen(name), &userinfo, &len);
+                       yp_unbind(domain);
+                       if ((i == YPERR_SUCCESS) && ((size_t)len < sizeof(buf))) {
+                               strncpy(buf, userinfo, sizeof(buf) - 1);
+                               buf[sizeof(buf) - 1] = '\0';
+                               matched = 1;
+                       }
+               }
+       }
 
-       D(("called"));
+       if (matched && (ret != NULL)) {
+               *ret = NULL;
 
-       /*
-        * This function does not have to be too smart if something goes
-        * wrong, return FALSE and let this case to be treated somewhere
-        * else (CG)
-        */
+               slogin = buf;
 
-       if (on(UNIX__NONULL, ctrl))
-               return 0;       /* will fail but don't let on yet */
+               spasswd = strchr(slogin, ':');
+               if (spasswd == NULL) {
+                       return matched;
+               }
+               *spasswd++ = '\0';
 
-       /* UNIX passwords area */
+               suid = strchr(spasswd, ':');
+               if (suid == NULL) {
+                       return matched;
+               }
+               *suid++ = '\0';
 
-       /* Get password file entry... */
-#if HAVE_GETPWNAM_R
-       bufsize = 1024;
-       buf = malloc(bufsize);
+               sgid = strchr(suid, ':');
+               if (sgid == NULL) {
+                       return matched;
+               }
+               *sgid++ = '\0';
 
-       if ((retval = getpwnam_r(name, pwd, buf, bufsize, &pwd))) {
-               pwd = NULL;
-       }
-       while (retval == ERANGE) {
-               bufsize += 1024;
-               buf = realloc(buf, bufsize);
-               if ((retval = getpwnam_r(name, pwd, buf, bufsize, &pwd))) {
-                       pwd = NULL;
+               sgecos = strchr(sgid, ':');
+               if (sgecos == NULL) {
+                       return matched;
                }
-       }
-#else
-       pwd = getpwnam(name);
-#endif
+               *sgecos++ = '\0';
 
-       if (pwd != NULL) {
-               if (strcmp( pwd->pw_passwd, "*NP*" ) == 0)
-               { /* NIS+ */                 
-                       uid_t save_euid, save_uid;
-       
-                       save_euid = geteuid();
-                       save_uid = getuid();
-                       if (save_uid == pwd->pw_uid)
-                               setreuid( save_euid, save_uid );
-                       else  {
-                               setreuid( 0, -1 );
-                               if (setreuid( -1, pwd->pw_uid ) == -1) {
-                                       setreuid( -1, 0 );
-                                       setreuid( 0, -1 );
-                                       if(setreuid( -1, pwd->pw_uid ) == -1)
-                                               /* Will fail elsewhere. */
-#if HAVE_GETPWNAM_R
-                                               if (buf)
-                                                       free(buf);
-#endif
-                                               return 0;
-                               }
-                       }
-       
-                       spwdent = getspnam( name );
-                       if (save_uid == pwd->pw_uid)
-                               setreuid( save_uid, save_euid );
-                       else {
-                               if (setreuid( -1, 0 ) == -1)
-                               setreuid( save_uid, -1 );
-                               setreuid( -1, save_euid );
-                       }
-               } else if (strcmp(pwd->pw_passwd, "x") == 0) {
-                       /*
-                        * ...and shadow password file entry for this user,
-                        * if shadowing is enabled
-                        */
-                       spwdent = getspnam(name);
+               shome = strchr(sgecos, ':');
+               if (shome == NULL) {
+                       return matched;
                }
-               if (spwdent)
-                       salt = x_strdup(spwdent->sp_pwdp);
-               else
-                       salt = x_strdup(pwd->pw_passwd);
-       }
-       /* Does this user have a password? */
-       if (salt == NULL) {
-               retval = 0;
-       } else {
-               if (strlen(salt) == 0)
-                       retval = 1;
-               else
-                       retval = 0;
-       }
+               *shome++ = '\0';
 
-       /* tidy up */
+               sshell = strchr(shome, ':');
+               if (sshell == NULL) {
+                       return matched;
+               }
+               *sshell++ = '\0';
+
+               buflen = sizeof(struct passwd) +
+                        strlen(slogin) + 1 +
+                        strlen(spasswd) + 1 +
+                        strlen(suid) + 1 +
+                        strlen(sgid) + 1 +
+                        strlen(sgecos) + 1 +
+                        strlen(shome) + 1 +
+                        strlen(sshell) + 1;
+               *ret = malloc(buflen);
+               if (*ret == NULL) {
+                       return matched;
+               }
+               memset(*ret, '\0', buflen);
 
-       if (salt)
-               _pam_delete(salt);
+               (*ret)->pw_uid = strtol(suid, &p, 10);
+               if ((strlen(suid) == 0) || (*p != '\0')) {
+                       free(*ret);
+                       *ret = NULL;
+                       return matched;
+               }
 
-#if HAVE_GETPWNAM_R
-       if (buf)
-               free(buf);
-#endif
+               (*ret)->pw_gid = strtol(sgid, &p, 10);
+               if ((strlen(sgid) == 0) || (*p != '\0')) {
+                       free(*ret);
+                       *ret = NULL;
+                       return matched;
+               }
 
-       return retval;
+               p = ((char*)(*ret)) + sizeof(struct passwd);
+               (*ret)->pw_name = strcpy(p, slogin);
+               p += strlen(p) + 1;
+               (*ret)->pw_passwd = strcpy(p, spasswd);
+               p += strlen(p) + 1;
+               (*ret)->pw_gecos = strcpy(p, sgecos);
+               p += strlen(p) + 1;
+               (*ret)->pw_dir = strcpy(p, shome);
+               p += strlen(p) + 1;
+               (*ret)->pw_shell = strcpy(p, sshell);
+
+               snprintf(buf, sizeof(buf), "_pam_unix_getpwnam_%s", name);
+
+               if (pam_set_data(pamh, buf,
+                                *ret, _unix_cleanup) != PAM_SUCCESS) {
+                       free(*ret);
+                       *ret = NULL;
+               }
+       }
+
+       return matched;
+}
+
+/*
+ * _unix_comsefromsource() is a quick check to see if information about a given
+ * user comes from a particular source (just files and nis for now)
+ *
+ */
+int _unix_comesfromsource(pam_handle_t *pamh,
+                         const char *name, int files, int nis)
+{
+       return _unix_getpwnam(pamh, name, files, nis, NULL);
 }
 
 /*
@@ -430,18 +432,39 @@ static int _unix_run_helper_binary(pam_handle_t *pamh, const char *passwd,
     /* fork */
     child = fork();
     if (child == 0) {
+        int i=0;
+        struct rlimit rlim;
        static char *envp[] = { NULL };
-       char *args[] = { NULL, NULL, NULL };
+       char *args[] = { NULL, NULL, NULL, NULL };
 
        /* XXX - should really tidy up PAM here too */
 
        /* reopen stdin as pipe */
-       close(fds[1]);
        dup2(fds[0], STDIN_FILENO);
 
+       if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
+          if (rlim.rlim_max >= MAX_FD_NO)
+                rlim.rlim_max = MAX_FD_NO;
+         for (i=0; i < (int)rlim.rlim_max; i++) {
+               if (i != STDIN_FILENO)
+                  close(i);
+         }
+       }
+
+       if (geteuid() == 0) {
+          /* must set the real uid to 0 so the helper will not error
+            out if pam is called from setuid binary (su, sudo...) */
+         setuid(0);
+       }
+
        /* exec binary helper */
-       args[0] = x_strdup(CHKPWD_HELPER);
+       args[0] = strdup(CHKPWD_HELPER);
        args[1] = x_strdup(user);
+       if (off(UNIX__NONULL, ctrl)) {  /* this means we've succeeded */
+         args[2]=strdup("nullok");
+       } else {
+         args[2]=strdup("nonull");
+       }
 
        execve(CHKPWD_HELPER, args, envp);
 
@@ -451,11 +474,7 @@ static int _unix_run_helper_binary(pam_handle_t *pamh, const char *passwd,
     } else if (child > 0) {
        /* wait for child */
        /* if the stored password is NULL */
-       if (off(UNIX__NONULL, ctrl)) {  /* this means we've succeeded */
-           write(fds[1], "nullok\0\0", 8);
-       } else {
-           write(fds[1], "nonull\0\0", 8);
-       }
+        int rc=0;
        if (passwd != NULL) {            /* send the password to the child */
            write(fds[1], passwd, strlen(passwd)+1);
            passwd = NULL;
@@ -464,14 +483,21 @@ static int _unix_run_helper_binary(pam_handle_t *pamh, const char *passwd,
        }
        close(fds[0]);       /* close here to avoid possible SIGPIPE above */
        close(fds[1]);
-       (void) waitpid(child, &retval, 0);  /* wait for helper to complete */
-       retval = (retval == 0) ? PAM_SUCCESS:PAM_AUTH_ERR;
+       rc=waitpid(child, &retval, 0);  /* wait for helper to complete */
+       if (rc<0) {
+         pam_syslog(pamh, LOG_ERR, "unix_chkpwd waitpid returned %d: %m", rc);
+         retval = PAM_AUTH_ERR;
+       } else {
+         retval = WEXITSTATUS(retval);
+       }
     } else {
        D(("fork failed"));
+       close(fds[0]);
+       close(fds[1]);
        retval = PAM_AUTH_ERR;
     }
 
-    if (sighandler != NULL) {
+    if (sighandler != SIG_ERR) {
         (void) signal(SIGCHLD, sighandler);   /* restore old signal handler */
     }
 
@@ -479,16 +505,70 @@ static int _unix_run_helper_binary(pam_handle_t *pamh, const char *passwd,
     return retval;
 }
 
+/*
+ * _unix_blankpasswd() is a quick check for a blank password
+ *
+ * returns TRUE if user does not have a password
+ * - to avoid prompting for one in such cases (CG)
+ */
+
+int
+_unix_blankpasswd (pam_handle_t *pamh, unsigned int ctrl, const char *name)
+{
+       struct passwd *pwd = NULL;
+       char *salt = NULL;
+       int retval;
+
+       D(("called"));
+
+       /*
+        * This function does not have to be too smart if something goes
+        * wrong, return FALSE and let this case to be treated somewhere
+        * else (CG)
+        */
+
+       if (on(UNIX__NONULL, ctrl))
+               return 0;       /* will fail but don't let on yet */
+
+       /* UNIX passwords area */
+
+       retval = get_pwd_hash(pamh, name, &pwd, &salt);
+
+       if (retval == PAM_UNIX_RUN_HELPER) {
+               /* salt will not be set here so we can return immediately */
+               if (_unix_run_helper_binary(pamh, NULL, ctrl, name) == PAM_SUCCESS)
+                       return 1;
+               else
+                       return 0;
+       }
+
+       /* Does this user have a password? */
+       if (salt == NULL) {
+               retval = 0;
+       } else {
+               if (strlen(salt) == 0)
+                       retval = 1;
+               else
+                       retval = 0;
+       }
+
+       /* tidy up */
+
+       if (salt)
+               _pam_delete(salt);
+
+       return retval;
+}
+
 int _unix_verify_password(pam_handle_t * pamh, const char *name
                          ,const char *p, unsigned int ctrl)
 {
        struct passwd *pwd = NULL;
-       struct spwd *spwdent = NULL;
        char *salt = NULL;
-       char *pp = NULL;
        char *data_name;
        int retval;
 
+
        D(("called"));
 
 #ifdef HAVE_PAM_FAIL_DELAY
@@ -502,126 +582,41 @@ int _unix_verify_password(pam_handle_t * pamh, const char *name
 
        D(("locating user's record"));
 
-       /* UNIX passwords area */
-       pwd = getpwnam(name);   /* Get password file entry... */
-
-       if (pwd != NULL) {
-               if (strcmp( pwd->pw_passwd, "*NP*" ) == 0)
-               { /* NIS+ */                 
-                       uid_t save_euid, save_uid;
-       
-                       save_euid = geteuid();
-                       save_uid = getuid();
-                       if (save_uid == pwd->pw_uid)
-                               setreuid( save_euid, save_uid );
-                       else  {
-                               setreuid( 0, -1 );
-                               if (setreuid( -1, pwd->pw_uid ) == -1) {
-                                       setreuid( -1, 0 );
-                                       setreuid( 0, -1 );
-                                       if(setreuid( -1, pwd->pw_uid ) == -1)
-                                               return PAM_CRED_INSUFFICIENT;
-                               }
-                       }
-       
-                       spwdent = getspnam( name );
-                       if (save_uid == pwd->pw_uid)
-                               setreuid( save_uid, save_euid );
-                       else {
-                               if (setreuid( -1, 0 ) == -1)
-                               setreuid( save_uid, -1 );
-                               setreuid( -1, save_euid );
-                       }
-               } else if (strcmp(pwd->pw_passwd, "x") == 0) {
-                       /*
-                        * ...and shadow password file entry for this user,
-                        * if shadowing is enabled
-                        */
-                       spwdent = getspnam(name);
-               }
-               if (spwdent)
-                       salt = x_strdup(spwdent->sp_pwdp);
-               else
-                       salt = x_strdup(pwd->pw_passwd);
-       }
+       retval = get_pwd_hash(pamh, name, &pwd, &salt);
 
        data_name = (char *) malloc(sizeof(FAIL_PREFIX) + strlen(name));
        if (data_name == NULL) {
-               _log_err(LOG_CRIT, pamh, "no memory for data-name");
+               pam_syslog(pamh, LOG_CRIT, "no memory for data-name");
        } else {
                strcpy(data_name, FAIL_PREFIX);
                strcpy(data_name + sizeof(FAIL_PREFIX) - 1, name);
        }
 
-       retval = PAM_SUCCESS;
-       if (pwd == NULL || salt == NULL || strlen(salt) == 1) {
-               if (geteuid()) {
-                       /* we are not root perhaps this is the reason? Run helper */
+       if (retval != PAM_SUCCESS) {
+               if (retval == PAM_UNIX_RUN_HELPER) {
                        D(("running helper binary"));
                        retval = _unix_run_helper_binary(pamh, p, ctrl, name);
-                       if (pwd == NULL && !on(UNIX_AUDIT,ctrl)
-                           && retval != PAM_SUCCESS)
-                       {
-                               name = NULL;
-                       }
                } else {
                        D(("user's record unavailable"));
+                       p = NULL;
                        if (on(UNIX_AUDIT, ctrl)) {
                                /* this might be a typo and the user has given a password
                                   instead of a username. Careful with this. */
-                               _log_err(LOG_ALERT, pamh,
+                               pam_syslog(pamh, LOG_WARNING,
                                         "check pass; user (%s) unknown", name);
                        } else {
                                name = NULL;
-                               _log_err(LOG_ALERT, pamh,
-                                        "check pass; user unknown");
+                               if (on(UNIX_DEBUG, ctrl) || pwd == NULL) {
+                                   pam_syslog(pamh, LOG_WARNING,
+                                           "check pass; user unknown");
+                               } else {
+                                   /* don't log failure as another pam module can succeed */
+                                   goto cleanup;
+                               }
                        }
-                       p = NULL;
-                       retval = PAM_AUTHINFO_UNAVAIL;
                }
        } else {
-           int salt_len = strlen(salt);
-           if (!salt_len) {
-               /* the stored password is NULL */
-               if (off(UNIX__NONULL, ctrl)) {/* this means we've succeeded */
-                   D(("user has empty password - access granted"));
-                   retval = PAM_SUCCESS;
-               } else {
-                   D(("user has empty password - access denied"));
-                   retval = PAM_AUTH_ERR;
-               }
-           } else if (!p || (*salt == '*') || (salt_len < 13)) {
-               retval = PAM_AUTH_ERR;
-           } else {
-               if (!strncmp(salt, "$1$", 3)) {
-                   pp = Goodcrypt_md5(p, salt);
-                   if (strcmp(pp, salt) != 0) {
-                       _pam_delete(pp);
-                       pp = Brokencrypt_md5(p, salt);
-                   }
-               } else {
-                   pp = bigcrypt(p, salt);
-               }
-               p = NULL;               /* no longer needed here */
-
-               /* the moment of truth -- do we agree with the password? */
-               D(("comparing state of pp[%s] and salt[%s]", pp, salt));
-
-               /*
-                * Note, we are comparing the bigcrypt of the password with
-                * the contents of the password field. If the latter was
-                * encrypted with regular crypt (and not bigcrypt) it will
-                * have been truncated for storage relative to the output
-                * of bigcrypt here. As such we need to compare only the
-                * stored string with the subset of bigcrypt's result.
-                * Bug 521314: The strncmp comparison is for legacy support.
-                */
-               if (strncmp(pp, salt, salt_len) == 0) {
-                   retval = PAM_SUCCESS;
-               } else {
-                   retval = PAM_AUTH_ERR;
-               }
-           }
+               retval = verify_pwd_hash(p, salt, off(UNIX__NONULL, ctrl));
        }
 
        if (retval == PAM_SUCCESS) {
@@ -640,8 +635,10 @@ int _unix_verify_password(pam_handle_t * pamh, const char *name
                        if (new != NULL) {
 
                            const char *login_name;
+                           const void *void_old;
+
 
-                           login_name = _pammodutil_getlogin(pamh);
+                           login_name = pam_modutil_getlogin(pamh);
                            if (login_name == NULL) {
                                login_name = "";
                            }
@@ -652,7 +649,11 @@ int _unix_verify_password(pam_handle_t * pamh, const char *name
                                new->name = x_strdup(login_name);
 
                                /* any previous failures for this user ? */
-                               pam_get_data(pamh, data_name, (const void **) &old);
+                               if (pam_get_data(pamh, data_name, &void_old)
+                                   == PAM_SUCCESS)
+                                       old = void_old;
+                               else
+                                       old = NULL;
 
                                if (old != NULL) {
                                        new->count = old->count + 1;
@@ -660,29 +661,29 @@ int _unix_verify_password(pam_handle_t * pamh, const char *name
                                                retval = PAM_MAXTRIES;
                                        }
                                } else {
-                                       const char *service=NULL;
-                                       const char *ruser=NULL;
-                                       const char *rhost=NULL;
-                                       const char *tty=NULL;
+                                       const void *service=NULL;
+                                       const void *ruser=NULL;
+                                       const void *rhost=NULL;
+                                       const void *tty=NULL;
 
                                        (void) pam_get_item(pamh, PAM_SERVICE,
-                                                           (const void **)&service);
+                                                           &service);
                                        (void) pam_get_item(pamh, PAM_RUSER,
-                                                           (const void **)&ruser);
+                                                           &ruser);
                                        (void) pam_get_item(pamh, PAM_RHOST,
-                                                           (const void **)&rhost);
+                                                           &rhost);
                                        (void) pam_get_item(pamh, PAM_TTY,
-                                                           (const void **)&tty);
+                                                           &tty);
 
-                                       _log_err(LOG_NOTICE, pamh,
+                                       pam_syslog(pamh, LOG_NOTICE,
                                                 "authentication failure; "
                                                 "logname=%s uid=%d euid=%d "
                                                 "tty=%s ruser=%s rhost=%s "
                                                 "%s%s",
                                                 new->name, new->uid, new->euid,
-                                                tty ? tty : "",
-                                                ruser ? ruser : "",
-                                                rhost ? rhost : "",
+                                                tty ? (const char *)tty : "",
+                                                ruser ? (const char *)ruser : "",
+                                                rhost ? (const char *)rhost : "",
                                                 (new->user && new->user[0] != '\0')
                                                  ? " user=" : "",
                                                 new->user
@@ -693,18 +694,17 @@ int _unix_verify_password(pam_handle_t * pamh, const char *name
                                pam_set_data(pamh, data_name, new, _cleanup_failures);
 
                        } else {
-                               _log_err(LOG_CRIT, pamh,
+                               pam_syslog(pamh, LOG_CRIT,
                                         "no memory for failure recorder");
                        }
                }
        }
 
+cleanup:
        if (data_name)
                _pam_delete(data_name);
        if (salt)
                _pam_delete(salt);
-       if (pp)
-               _pam_delete(pp);
 
        D(("done [%d].", retval));
 
@@ -721,10 +721,10 @@ int _unix_read_password(pam_handle_t * pamh
                        ,const char *prompt1
                        ,const char *prompt2
                        ,const char *data_name
-                       ,const char **pass)
+                       ,const void **pass)
 {
        int authtok_flag;
-       int retval;
+       int retval = PAM_SUCCESS;
        char *token;
 
        D(("called"));
@@ -746,20 +746,20 @@ int _unix_read_password(pam_handle_t * pamh
         */
 
        if (on(UNIX_TRY_FIRST_PASS, ctrl) || on(UNIX_USE_FIRST_PASS, ctrl)) {
-               retval = pam_get_item(pamh, authtok_flag, (const void **) pass);
+               retval = pam_get_item(pamh, authtok_flag, pass);
                if (retval != PAM_SUCCESS) {
                        /* very strange. */
-                       _log_err(LOG_ALERT, pamh
-                                ,"pam_get_item returned error to unix-read-password"
+                       pam_syslog(pamh, LOG_ALERT,
+                                "pam_get_item returned error to unix-read-password"
                            );
                        return retval;
                } else if (*pass != NULL) {     /* we have a password! */
                        return PAM_SUCCESS;
-               } else if (on(UNIX_USE_FIRST_PASS, ctrl)) {
-                       return PAM_AUTHTOK_RECOVER_ERR;         /* didn't work */
                } else if (on(UNIX_USE_AUTHTOK, ctrl)
                           && off(UNIX__OLD_PASSWD, ctrl)) {
-                       return PAM_AUTHTOK_RECOVER_ERR;
+                       return PAM_AUTHTOK_ERR;
+               } else if (on(UNIX_USE_FIRST_PASS, ctrl)) {
+                       return PAM_AUTHTOK_RECOVERY_ERR;          /* didn't work */
                }
        }
        /*
@@ -768,77 +768,62 @@ int _unix_read_password(pam_handle_t * pamh
         */
 
        {
-               struct pam_message msg[3], *pmsg[3];
-               struct pam_response *resp;
-               int i, replies;
-
-               /* prepare to converse */
+               int replies=1;
+               char *resp[2] = { NULL, NULL };
 
                if (comment != NULL && off(UNIX__QUIET, ctrl)) {
-                       pmsg[0] = &msg[0];
-                       msg[0].msg_style = PAM_TEXT_INFO;
-                       msg[0].msg = comment;
-                       i = 1;
-               } else {
-                       i = 0;
+                       retval = pam_info(pamh, "%s", comment);
                }
 
-               pmsg[i] = &msg[i];
-               msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
-               msg[i++].msg = prompt1;
-               replies = 1;
+               if (retval == PAM_SUCCESS) {
+                       retval = pam_prompt(pamh, PAM_PROMPT_ECHO_OFF,
+                           &resp[0], "%s", prompt1);
 
-               if (prompt2 != NULL) {
-                       pmsg[i] = &msg[i];
-                       msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
-                       msg[i++].msg = prompt2;
-                       ++replies;
+                       if (retval == PAM_SUCCESS && prompt2 != NULL) {
+                               retval = pam_prompt(pamh, PAM_PROMPT_ECHO_OFF,
+                                   &resp[1], "%s", prompt2);
+                               ++replies;
+                       }
                }
-               /* so call the conversation expecting i responses */
-               resp = NULL;
-               retval = converse(pamh, ctrl, i, pmsg, &resp);
-
-               if (resp != NULL) {
 
+               if (resp[0] != NULL && resp[replies-1] != NULL) {
                        /* interpret the response */
 
                        if (retval == PAM_SUCCESS) {    /* a good conversation */
 
-                               token = x_strdup(resp[i - replies].resp);
+                               token = resp[0];
                                if (token != NULL) {
                                        if (replies == 2) {
-
                                                /* verify that password entered correctly */
-                                               if (!resp[i - 1].resp
-                                                   || strcmp(token, resp[i - 1].resp)) {
-                                                       _pam_delete(token);     /* mistyped */
-                                                       retval = PAM_AUTHTOK_RECOVER_ERR;
-                                                       _make_remark(pamh, ctrl
-                                                                   ,PAM_ERROR_MSG, MISTYPED_PASS);
+                                               if (strcmp(token, resp[replies - 1])) {
+                                                       /* mistyped */
+                                                       retval = PAM_AUTHTOK_RECOVERY_ERR;
+                                                       _make_remark(pamh, ctrl,
+                                                           PAM_ERROR_MSG, MISTYPED_PASS);
                                                }
                                        }
                                } else {
-                                       _log_err(LOG_NOTICE, pamh
-                                                ,"could not recover authentication token");
+                                       pam_syslog(pamh, LOG_NOTICE,
+                                                   "could not recover authentication token");
                                }
 
                        }
-                       /*
-                        * tidy up the conversation (resp_retcode) is ignored
-                        * -- what is it for anyway? AGM
-                        */
-
-                       _pam_drop_reply(resp, i);
 
                } else {
                        retval = (retval == PAM_SUCCESS)
-                           ? PAM_AUTHTOK_RECOVER_ERR : retval;
+                           ? PAM_AUTHTOK_RECOVERY_ERR : retval;
                }
+
+               resp[0] = NULL;
+               if (replies > 1)
+                       _pam_delete(resp[1]);
        }
 
        if (retval != PAM_SUCCESS) {
+               _pam_delete(token);
+
                if (on(UNIX_DEBUG, ctrl))
-                       _log_err(LOG_DEBUG, pamh,
+                       pam_syslog(pamh, LOG_DEBUG,
                                 "unable to obtain a password");
                return retval;
        }
@@ -851,12 +836,11 @@ int _unix_read_password(pam_handle_t * pamh
                retval = pam_set_item(pamh, authtok_flag, token);
                _pam_delete(token);     /* clean it up */
                if (retval != PAM_SUCCESS
-                   || (retval = pam_get_item(pamh, authtok_flag
-                                             ,(const void **) pass))
+                   || (retval = pam_get_item(pamh, authtok_flag, pass))
                    != PAM_SUCCESS) {
 
                        *pass = NULL;
-                       _log_err(LOG_CRIT, pamh, "error manipulating password");
+                       pam_syslog(pamh, LOG_CRIT, "error manipulating password");
                        return retval;
 
                }
@@ -868,9 +852,9 @@ int _unix_read_password(pam_handle_t * pamh
 
                retval = pam_set_data(pamh, data_name, (void *) token, _cleanup);
                if (retval != PAM_SUCCESS) {
-                       _log_err(LOG_CRIT, pamh
-                                ,"error manipulating password data [%s]"
-                                ,pam_strerror(pamh, retval));
+                       pam_syslog(pamh, LOG_CRIT,
+                                "error manipulating password data [%s]",
+                                pam_strerror(pamh, retval));
                        _pam_delete(token);
                        return retval;
                }
@@ -886,6 +870,7 @@ int _unix_read_password(pam_handle_t * pamh
  * Copyright (c) Andrew G. Morgan 1996-8.
  * Copyright (c) Alex O. Yuriev, 1996.
  * Copyright (c) Cristian Gafton 1996.
+ * Copyright (c) Red Hat, Inc. 2007.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -899,13 +884,13 @@ int _unix_read_password(pam_handle_t * pamh
  * 3. The name of the author may not be used to endorse or promote
  *    products derived from this software without specific prior
  *    written permission.
- * 
+ *
  * ALTERNATIVELY, this product may be distributed under the terms of
  * the GNU Public License, in which case the provisions of the GPL are
  * required INSTEAD OF the above restrictions.  (This clause is
  * necessary due to a potential bad interaction between the GPL and
  * the restrictions contained in a BSD-style copyright.)
- * 
+ *
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE