]> andersk Git - openssh.git/blobdiff - auth-pam.c
- (dtucker) [auth-pam.c] Bug #559 (last piece): Pass DISALLOW_NULL_AUTHTOK
[openssh.git] / auth-pam.c
index 1f310de5d7e2957ed15a25153d7d62ff3929ef56..76a43a350f28e61ce1ebde1e3f48f00045b9f928 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+/*
+ * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org>
+ * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
 
 /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
 #include "includes.h"
 RCSID("$Id$");
 
 #ifdef USE_PAM
+#if defined(HAVE_SECURITY_PAM_APPL_H)
 #include <security/pam_appl.h>
+#elif defined (HAVE_PAM_PAM_APPL_H)
+#include <pam/pam_appl.h>
+#endif
 
 #include "auth.h"
 #include "auth-pam.h"
@@ -45,7 +65,7 @@ RCSID("$Id$");
 #include "monitor_wrap.h"
 #include "msg.h"
 #include "packet.h"
-#include "readpass.h"
+#include "misc.h"
 #include "servconf.h"
 #include "ssh2.h"
 #include "xmalloc.h"
@@ -53,22 +73,61 @@ RCSID("$Id$");
 
 extern ServerOptions options;
 extern Buffer loginmsg;
-
-#define __unused
+extern int compat20;
+extern u_int utmp_len;
 
 #ifdef USE_POSIX_THREADS
 #include <pthread.h>
 /*
- * Avoid namespace clash when *not* using pthreads for systems *with* 
- * pthreads, which unconditionally define pthread_t via sys/types.h 
+ * Avoid namespace clash when *not* using pthreads for systems *with*
+ * pthreads, which unconditionally define pthread_t via sys/types.h
  * (e.g. Linux)
  */
-typedef pthread_t sp_pthread_t; 
+typedef pthread_t sp_pthread_t;
 #else
+typedef pid_t sp_pthread_t;
+#endif
+
+struct pam_ctxt {
+       sp_pthread_t     pam_thread;
+       int              pam_psock;
+       int              pam_csock;
+       int              pam_done;
+};
+
+static void sshpam_free_ctx(void *);
+static struct pam_ctxt *cleanup_ctxt;
+
+#ifndef USE_POSIX_THREADS
 /*
  * Simulate threads with processes.
  */
-typedef pid_t sp_pthread_t;
+
+static int sshpam_thread_status = -1;
+static mysig_t sshpam_oldsig;
+
+static void 
+sshpam_sigchld_handler(int sig)
+{
+       signal(SIGCHLD, SIG_DFL);
+       if (cleanup_ctxt == NULL)
+               return; /* handler called after PAM cleanup, shouldn't happen */
+       if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
+            == -1) {
+               /* PAM thread has not exitted, privsep slave must have */
+               kill(cleanup_ctxt->pam_thread, SIGTERM);
+               if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
+                   == -1)
+                       return; /* could not wait */
+       }
+       if (WIFSIGNALED(sshpam_thread_status) &&
+           WTERMSIG(sshpam_thread_status) == SIGTERM)
+               return; /* terminated by pthread_cancel */
+       if (!WIFEXITED(sshpam_thread_status))
+               fatal("PAM: authentication thread exited unexpectedly");
+       if (WEXITSTATUS(sshpam_thread_status) != 0)
+               fatal("PAM: authentication thread exited uncleanly");
+}
 
 static void
 pthread_exit(void *value __unused)
@@ -82,6 +141,7 @@ pthread_create(sp_pthread_t *thread, const void *attr __unused,
 {
        pid_t pid;
 
+       sshpam_thread_status = -1;
        switch ((pid = fork())) {
        case -1:
                error("fork(): %s", strerror(errno));
@@ -91,6 +151,7 @@ pthread_create(sp_pthread_t *thread, const void *attr __unused,
                _exit(1);
        default:
                *thread = pid;
+               sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
                return (0);
        }
 }
@@ -98,6 +159,7 @@ pthread_create(sp_pthread_t *thread, const void *attr __unused,
 static int
 pthread_cancel(sp_pthread_t thread)
 {
+       signal(SIGCHLD, sshpam_oldsig);
        return (kill(thread, SIGTERM));
 }
 
@@ -106,6 +168,9 @@ pthread_join(sp_pthread_t thread, void **value __unused)
 {
        int status;
 
+       if (sshpam_thread_status != -1)
+               return (sshpam_thread_status);
+       signal(SIGCHLD, sshpam_oldsig);
        waitpid(thread, &status, 0);
        return (status);
 }
@@ -115,20 +180,12 @@ pthread_join(sp_pthread_t thread, void **value __unused)
 static pam_handle_t *sshpam_handle = NULL;
 static int sshpam_err = 0;
 static int sshpam_authenticated = 0;
-static int sshpam_new_authtok_reqd = 0;
 static int sshpam_session_open = 0;
 static int sshpam_cred_established = 0;
+static int sshpam_account_status = -1;
 static char **sshpam_env = NULL;
-
-struct pam_ctxt {
-       sp_pthread_t     pam_thread;
-       int              pam_psock;
-       int              pam_csock;
-       int              pam_done;
-};
-
-static void sshpam_free_ctx(void *);
-static struct pam_ctxt *cleanup_ctxt;
+static Authctxt *sshpam_authctxt = NULL;
+static const char *sshpam_password = NULL;
 
 /* Some PAM implementations don't implement this */
 #ifndef HAVE_PAM_GETENVLIST
@@ -136,7 +193,7 @@ static char **
 pam_getenvlist(pam_handle_t *pamh)
 {
        /*
-        * XXX - If necessary, we can still support envrionment passing 
+        * XXX - If necessary, we can still support envrionment passing
         * for platforms without pam_getenvlist by searching for known
         * env vars (e.g. KRB5CCNAME) from the PAM environment.
         */
@@ -144,6 +201,24 @@ pam_getenvlist(pam_handle_t *pamh)
 }
 #endif
 
+void
+sshpam_password_change_required(int reqd)
+{
+       debug3("%s %d", __func__, reqd);
+       if (sshpam_authctxt == NULL)
+               fatal("%s: PAM authctxt not initialized", __func__);
+       sshpam_authctxt->force_pwchange = reqd;
+       if (reqd) {
+               no_port_forwarding_flag |= 2;
+               no_agent_forwarding_flag |= 2;
+               no_x11_forwarding_flag |= 2;
+       } else {
+               no_port_forwarding_flag &= ~2;
+               no_agent_forwarding_flag &= ~2;
+               no_x11_forwarding_flag &= ~2;
+       }
+}
+
 /* Import regular and PAM environment from subprocess */
 static void
 import_environments(Buffer *b)
@@ -152,6 +227,13 @@ import_environments(Buffer *b)
        u_int i, num_env;
        int err;
 
+       debug3("PAM: %s entering", __func__);
+
+#ifndef USE_POSIX_THREADS
+       /* Import variables set by do_pam_account */
+       sshpam_account_status = buffer_get_int(b);
+       sshpam_password_change_required(buffer_get_int(b));
+
        /* Import environment from subprocess */
        num_env = buffer_get_int(b);
        sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
@@ -175,13 +257,14 @@ import_environments(Buffer *b)
                }
 #endif
        }
+#endif
 }
 
 /*
  * Conversation function for authentication thread.
  */
 static int
-sshpam_thread_conv(int n, const struct pam_message **msg,
+sshpam_thread_conv(int n, struct pam_message **msg,
     struct pam_response **resp, void *data)
 {
        Buffer buffer;
@@ -189,8 +272,13 @@ sshpam_thread_conv(int n, const struct pam_message **msg,
        struct pam_response *reply;
        int i;
 
+       debug3("PAM: %s entering, %d messages", __func__, n);
        *resp = NULL;
 
+       if (data == NULL) {
+               error("PAM: conversation function passed a null context");
+               return (PAM_CONV_ERR);
+       }
        ctxt = data;
        if (n <= 0 || n > PAM_MAX_NUM_MSG)
                return (PAM_CONV_ERR);
@@ -203,21 +291,21 @@ sshpam_thread_conv(int n, const struct pam_message **msg,
        for (i = 0; i < n; ++i) {
                switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
                case PAM_PROMPT_ECHO_OFF:
-                       buffer_put_cstring(&buffer, 
+                       buffer_put_cstring(&buffer,
                            PAM_MSG_MEMBER(msg, i, msg));
-                       if (ssh_msg_send(ctxt->pam_csock, 
+                       if (ssh_msg_send(ctxt->pam_csock,
                            PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
                                goto fail;
-                       if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) 
+                       if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
                                goto fail;
                        if (buffer_get_char(&buffer) != PAM_AUTHTOK)
                                goto fail;
                        reply[i].resp = buffer_get_string(&buffer, NULL);
                        break;
                case PAM_PROMPT_ECHO_ON:
-                       buffer_put_cstring(&buffer, 
+                       buffer_put_cstring(&buffer,
                            PAM_MSG_MEMBER(msg, i, msg));
-                       if (ssh_msg_send(ctxt->pam_csock, 
+                       if (ssh_msg_send(ctxt->pam_csock,
                            PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
                                goto fail;
                        if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
@@ -227,16 +315,16 @@ sshpam_thread_conv(int n, const struct pam_message **msg,
                        reply[i].resp = buffer_get_string(&buffer, NULL);
                        break;
                case PAM_ERROR_MSG:
-                       buffer_put_cstring(&buffer, 
+                       buffer_put_cstring(&buffer,
                            PAM_MSG_MEMBER(msg, i, msg));
-                       if (ssh_msg_send(ctxt->pam_csock, 
+                       if (ssh_msg_send(ctxt->pam_csock,
                            PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
                                goto fail;
                        break;
                case PAM_TEXT_INFO:
-                       buffer_put_cstring(&buffer, 
+                       buffer_put_cstring(&buffer,
                            PAM_MSG_MEMBER(msg, i, msg));
-                       if (ssh_msg_send(ctxt->pam_csock, 
+                       if (ssh_msg_send(ctxt->pam_csock,
                            PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
                                goto fail;
                        break;
@@ -268,13 +356,15 @@ sshpam_thread(void *ctxtp)
        struct pam_ctxt *ctxt = ctxtp;
        Buffer buffer;
        struct pam_conv sshpam_conv;
+       int flags = (options.permit_empty_passwd == 0 ?
+           PAM_DISALLOW_NULL_AUTHTOK : 0);
 #ifndef USE_POSIX_THREADS
        extern char **environ;
        char **env_from_pam;
        u_int i;
        const char *pam_user;
 
-       pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
+       pam_get_item(sshpam_handle, PAM_USER, (void **)&pam_user);
        setproctitle("%s [pam]", pam_user);
        environ[0] = NULL;
 #endif
@@ -282,17 +372,37 @@ sshpam_thread(void *ctxtp)
        sshpam_conv.conv = sshpam_thread_conv;
        sshpam_conv.appdata_ptr = ctxt;
 
+       if (sshpam_authctxt == NULL)
+               fatal("%s: PAM authctxt not initialized", __func__);
+
        buffer_init(&buffer);
        sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
            (const void *)&sshpam_conv);
        if (sshpam_err != PAM_SUCCESS)
                goto auth_fail;
-       sshpam_err = pam_authenticate(sshpam_handle, 0);
+       sshpam_err = pam_authenticate(sshpam_handle, flags);
        if (sshpam_err != PAM_SUCCESS)
                goto auth_fail;
+
+       if (compat20) {
+               if (!do_pam_account())
+                       goto auth_fail;
+               if (sshpam_authctxt->force_pwchange) {
+                       sshpam_err = pam_chauthtok(sshpam_handle,
+                           PAM_CHANGE_EXPIRED_AUTHTOK);
+                       if (sshpam_err != PAM_SUCCESS)
+                               goto auth_fail;
+                       sshpam_password_change_required(0);
+               }
+       }
+
        buffer_put_cstring(&buffer, "OK");
 
 #ifndef USE_POSIX_THREADS
+       /* Export variables set by do_pam_account */
+       buffer_put_int(&buffer, sshpam_account_status);
+       buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
+
        /* Export any environment strings set in child */
        for(i = 0; environ[i] != NULL; i++)
                ; /* Count */
@@ -321,7 +431,7 @@ sshpam_thread(void *ctxtp)
        ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
        buffer_free(&buffer);
        pthread_exit(NULL);
-       
+
        return (NULL); /* Avoid warning for non-pthread case */
 }
 
@@ -330,6 +440,7 @@ sshpam_thread_cleanup(void)
 {
        struct pam_ctxt *ctxt = cleanup_ctxt;
 
+       debug3("PAM: %s entering", __func__);
        if (ctxt != NULL && ctxt->pam_thread != 0) {
                pthread_cancel(ctxt->pam_thread);
                pthread_join(ctxt->pam_thread, NULL);
@@ -341,9 +452,10 @@ sshpam_thread_cleanup(void)
 }
 
 static int
-sshpam_null_conv(int n, const struct pam_message **msg,
+sshpam_null_conv(int n, struct pam_message **msg,
     struct pam_response **resp, void *data)
 {
+       debug3("PAM: %s entering, %d messages", __func__, n);
        return (PAM_CONV_ERR);
 }
 
@@ -364,22 +476,21 @@ sshpam_cleanup(void)
                pam_close_session(sshpam_handle, PAM_SILENT);
                sshpam_session_open = 0;
        }
-       sshpam_authenticated = sshpam_new_authtok_reqd = 0;
+       sshpam_authenticated = 0;
        pam_end(sshpam_handle, sshpam_err);
        sshpam_handle = NULL;
 }
 
 static int
-sshpam_init(const char *user)
+sshpam_init(Authctxt *authctxt)
 {
-       extern u_int utmp_len;
        extern char *__progname;
-       const char *pam_rhost, *pam_user;
+       const char *pam_rhost, *pam_user, *user = authctxt->user;
 
        if (sshpam_handle != NULL) {
                /* We already have a PAM context; check if the user matches */
                sshpam_err = pam_get_item(sshpam_handle,
-                   PAM_USER, (const void **)&pam_user);
+                   PAM_USER, (void **)&pam_user);
                if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
                        return (0);
                pam_end(sshpam_handle, sshpam_err);
@@ -388,6 +499,8 @@ sshpam_init(const char *user)
        debug("PAM: initializing for \"%s\"", user);
        sshpam_err =
            pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
+       sshpam_authctxt = authctxt;
+
        if (sshpam_err != PAM_SUCCESS) {
                pam_end(sshpam_handle, sshpam_err);
                sshpam_handle = NULL;
@@ -402,11 +515,11 @@ sshpam_init(const char *user)
                return (-1);
        }
 #ifdef PAM_TTY_KLUDGE
-        /*
-         * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
-         * sshd doesn't set the tty until too late in the auth process and 
+       /*
+        * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
+        * sshd doesn't set the tty until too late in the auth process and
         * may not even set one (for tty-less connections)
-         */
+        */
        debug("PAM: setting PAM_TTY to \"ssh\"");
        sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
        if (sshpam_err != PAM_SUCCESS) {
@@ -424,12 +537,13 @@ sshpam_init_ctx(Authctxt *authctxt)
        struct pam_ctxt *ctxt;
        int socks[2];
 
+       debug3("PAM: %s entering", __func__);
        /* Refuse to start if we don't have PAM enabled */
        if (!options.use_pam)
                return NULL;
 
        /* Initialize PAM */
-       if (sshpam_init(authctxt->user) == -1) {
+       if (sshpam_init(authctxt) == -1) {
                error("PAM: initialization failed");
                return (NULL);
        }
@@ -468,6 +582,7 @@ sshpam_query(void *ctx, char **name, char **info,
        char *msg;
        size_t len;
 
+       debug3("PAM: %s entering", __func__);
        buffer_init(&buffer);
        *name = xstrdup("");
        *info = xstrdup("");
@@ -514,7 +629,10 @@ sshpam_query(void *ctx, char **name, char **info,
                                xfree(msg);
                                return (0);
                        }
-                       error("PAM: %s", msg);
+                       error("PAM: %s for %s%.100s from %.100s", msg,
+                           sshpam_authctxt->valid ? "" : "illegal user ",
+                           sshpam_authctxt->user,
+                           get_remote_name_or_ip(utmp_len, options.use_dns));
                        /* FALLTHROUGH */
                default:
                        *num = 0;
@@ -534,7 +652,7 @@ sshpam_respond(void *ctx, u_int num, char **resp)
        Buffer buffer;
        struct pam_ctxt *ctxt = ctx;
 
-       debug2("PAM: %s", __func__);
+       debug2("PAM: %s entering, %d responses", __func__, num);
        switch (ctxt->pam_done) {
        case 1:
                sshpam_authenticated = 1;
@@ -563,6 +681,7 @@ sshpam_free_ctx(void *ctxtp)
 {
        struct pam_ctxt *ctxt = ctxtp;
 
+       debug3("PAM: %s entering", __func__);
        sshpam_thread_cleanup();
        xfree(ctxt);
        /*
@@ -593,12 +712,12 @@ KbdintDevice mm_sshpam_device = {
  * This replaces auth-pam.c
  */
 void
-start_pam(const char *user)
+start_pam(Authctxt *authctxt)
 {
        if (!options.use_pam)
                fatal("PAM: initialisation requested when UsePAM=no");
 
-       if (sshpam_init(user) == -1)
+       if (sshpam_init(authctxt) == -1)
                fatal("PAM: initialisation failed");
 }
 
@@ -611,22 +730,22 @@ finish_pam(void)
 u_int
 do_pam_account(void)
 {
+       if (sshpam_account_status != -1)
+               return (sshpam_account_status);
+
        sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
-       debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
+       debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
        
-       if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD)
-               return (0);
-
-       if (sshpam_err == PAM_NEW_AUTHTOK_REQD) {
-               sshpam_new_authtok_reqd = 1;
-
-               /* Prevent forwardings until password changed */
-               no_port_forwarding_flag |= 2;
-               no_agent_forwarding_flag |= 2;
-               no_x11_forwarding_flag |= 2;
+       if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
+               sshpam_account_status = 0;
+               return (sshpam_account_status);
        }
 
-       return (1);
+       if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
+               sshpam_password_change_required(1);
+
+       sshpam_account_status = 1;
+       return (sshpam_account_status);
 }
 
 void
@@ -668,20 +787,16 @@ do_pam_setcred(int init)
                    pam_strerror(sshpam_handle, sshpam_err));
 }
 
-int
-is_pam_password_change_required(void)
-{
-       return (sshpam_new_authtok_reqd);
-}
-
 static int
-pam_tty_conv(int n, const struct pam_message **msg,
+sshpam_tty_conv(int n, struct pam_message **msg,
     struct pam_response **resp, void *data)
 {
        char input[PAM_MAX_MSG_SIZE];
        struct pam_response *reply;
        int i;
 
+       debug3("PAM: %s called with %d messages", __func__, n);
+
        *resp = NULL;
 
        if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
@@ -695,7 +810,7 @@ pam_tty_conv(int n, const struct pam_message **msg,
                switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
                case PAM_PROMPT_ECHO_OFF:
                        reply[i].resp =
-                           read_passphrase(PAM_MSG_MEMBER(msg, i, msg), 
+                           read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
                            RP_ALLOW_STDIN);
                        reply[i].resp_retcode = PAM_SUCCESS;
                        break;
@@ -726,7 +841,7 @@ pam_tty_conv(int n, const struct pam_message **msg,
        return (PAM_CONV_ERR);
 }
 
-static struct pam_conv tty_conv = { pam_tty_conv, NULL };
+static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
 
 /*
  * XXX this should be done in the authentication phase, but ssh1 doesn't
@@ -749,11 +864,57 @@ do_pam_chauthtok(void)
                    pam_strerror(sshpam_handle, sshpam_err));
 }
 
+static int
+sshpam_store_conv(int n, struct pam_message **msg,
+    struct pam_response **resp, void *data)
+{
+       struct pam_response *reply;
+       int i;
+       size_t len;
+
+       debug3("PAM: %s called with %d messages", __func__, n);
+       *resp = NULL;
+
+       if (n <= 0 || n > PAM_MAX_NUM_MSG)
+               return (PAM_CONV_ERR);
+
+       if ((reply = malloc(n * sizeof(*reply))) == NULL)
+               return (PAM_CONV_ERR);
+       memset(reply, 0, n * sizeof(*reply));
+
+       for (i = 0; i < n; ++i) {
+               switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
+               case PAM_ERROR_MSG:
+               case PAM_TEXT_INFO:
+                       len = strlen(PAM_MSG_MEMBER(msg, i, msg));
+                       buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
+                       buffer_append(&loginmsg, "\n", 1 );
+                       reply[i].resp_retcode = PAM_SUCCESS;
+                       break;
+               default:
+                       goto fail;
+               }
+       }
+       *resp = reply;
+       return (PAM_SUCCESS);
+
+ fail:
+       for(i = 0; i < n; i++) {
+               if (reply[i].resp != NULL)
+                       xfree(reply[i].resp);
+       }
+       xfree(reply);
+       return (PAM_CONV_ERR);
+}
+
+static struct pam_conv store_conv = { sshpam_store_conv, NULL };
+
 void
 do_pam_session(void)
 {
-       sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, 
-           (const void *)&tty_conv);
+       debug3("PAM: opening session");
+       sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
+           (const void *)&store_conv);
        if (sshpam_err != PAM_SUCCESS)
                fatal("PAM: failed to set PAM_CONV: %s",
                    pam_strerror(sshpam_handle, sshpam_err));
@@ -764,16 +925,16 @@ do_pam_session(void)
        sshpam_session_open = 1;
 }
 
-/* 
+/*
  * Set a PAM environment string. We need to do this so that the session
  * modules can handle things like Kerberos/GSI credentials that appear
  * during the ssh authentication process.
  */
 int
-do_pam_putenv(char *name, char *value) 
+do_pam_putenv(char *name, char *value)
 {
        int ret = 1;
-#ifdef HAVE_PAM_PUTENV 
+#ifdef HAVE_PAM_PUTENV
        char *compound;
        size_t len;
 
@@ -788,12 +949,6 @@ do_pam_putenv(char *name, char *value)
        return (ret);
 }
 
-void
-print_pam_messages(void)
-{
-       /* XXX */
-}
-
 char **
 fetch_pam_child_environment(void)
 {
@@ -819,4 +974,110 @@ free_pam_environment(char **env)
        xfree(env);
 }
 
+/*
+ * "Blind" conversation function for password authentication.  Assumes that
+ * echo-off prompts are for the password and stores messages for later
+ * display.
+ */
+static int
+sshpam_passwd_conv(int n, struct pam_message **msg,
+    struct pam_response **resp, void *data)
+{
+       struct pam_response *reply;
+       int i;
+       size_t len;
+
+       debug3("PAM: %s called with %d messages", __func__, n);
+
+       *resp = NULL;
+
+       if (n <= 0 || n > PAM_MAX_NUM_MSG)
+               return (PAM_CONV_ERR);
+
+       if ((reply = malloc(n * sizeof(*reply))) == NULL)
+               return (PAM_CONV_ERR);
+       memset(reply, 0, n * sizeof(*reply));
+
+       for (i = 0; i < n; ++i) {
+               switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
+               case PAM_PROMPT_ECHO_OFF:
+                       if (sshpam_password == NULL)
+                               goto fail;
+                       reply[i].resp = xstrdup(sshpam_password);
+                       reply[i].resp_retcode = PAM_SUCCESS;
+                       break;
+               case PAM_ERROR_MSG:
+               case PAM_TEXT_INFO:
+                       len = strlen(PAM_MSG_MEMBER(msg, i, msg));
+                       if (len > 0) {
+                               buffer_append(&loginmsg,
+                                   PAM_MSG_MEMBER(msg, i, msg), len);
+                               buffer_append(&loginmsg, "\n", 1);
+                       }
+                       reply[i].resp = xstrdup("");
+                       reply[i].resp_retcode = PAM_SUCCESS;
+                       break;
+               default:
+                       goto fail;
+               }
+       }
+       *resp = reply;
+       return (PAM_SUCCESS);
+
+ fail: 
+       for(i = 0; i < n; i++) {
+               if (reply[i].resp != NULL)
+                       xfree(reply[i].resp);
+       }
+       xfree(reply);
+       return (PAM_CONV_ERR);
+}
+
+static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
+
+/*
+ * Attempt password authentication via PAM
+ */
+int
+sshpam_auth_passwd(Authctxt *authctxt, const char *password)
+{
+       int flags = (options.permit_empty_passwd == 0 ?
+           PAM_DISALLOW_NULL_AUTHTOK : 0);
+       static char badpw[] = "\b\n\r\177INCORRECT";
+
+       if (!options.use_pam || sshpam_handle == NULL)
+               fatal("PAM: %s called when PAM disabled or failed to "
+                   "initialise.", __func__);
+
+       sshpam_password = password;
+       sshpam_authctxt = authctxt;
+
+       /*
+        * If the user logging in is invalid, or is root but is not permitted
+        * by PermitRootLogin, use an invalid password to prevent leaking
+        * information via timing (eg if the PAM config has a delay on fail).
+        */
+       if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
+            options.permit_root_login != PERMIT_YES))
+               sshpam_password = badpw;
+
+       sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
+           (const void *)&passwd_conv);
+       if (sshpam_err != PAM_SUCCESS)
+               fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
+                   pam_strerror(sshpam_handle, sshpam_err));
+
+       sshpam_err = pam_authenticate(sshpam_handle, flags);
+       sshpam_password = NULL;
+       if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
+               debug("PAM: password authentication accepted for %.100s",
+                   authctxt->user);
+               return 1;
+       } else {
+               debug("PAM: password authentication failed for %.100s: %s",
+                   authctxt->valid ? authctxt->user : "an illegal user",
+                   pam_strerror(sshpam_handle, sshpam_err));
+               return 0;
+       }
+}
 #endif /* USE_PAM */
This page took 0.070396 seconds and 4 git commands to generate.