Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2002 Networks Associates Technology, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This software was developed for the FreeBSD Project by ThinkSec AS and |
| 6 | * NAI Labs, the Security Research Division of Network Associates, Inc. |
| 7 | * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the |
| 8 | * DARPA CHATS research program. |
Damien Miller | e69f18c | 2000-06-12 16:38:54 +1000 | [diff] [blame] | 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
Damien Miller | e69f18c | 2000-06-12 16:38:54 +1000 | [diff] [blame] | 18 | * |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | * SUCH DAMAGE. |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 30 | */ |
Damien Miller | 26314f6 | 2004-06-01 11:28:20 +1000 | [diff] [blame] | 31 | /* |
| 32 | * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org> |
| 33 | * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au> |
| 34 | * |
| 35 | * Permission to use, copy, modify, and distribute this software for any |
| 36 | * purpose with or without fee is hereby granted, provided that the above |
| 37 | * copyright notice and this permission notice appear in all copies. |
| 38 | * |
| 39 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 40 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 41 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 42 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 43 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 44 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 45 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 46 | */ |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 47 | |
Damien Miller | 5ef4b0f | 2016-01-27 17:45:56 +1100 | [diff] [blame] | 48 | /* Based on FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des */ |
| 49 | |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 50 | #include "includes.h" |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 51 | |
| 52 | #include <sys/types.h> |
| 53 | #include <sys/stat.h> |
| 54 | #include <sys/wait.h> |
Darren Tucker | 341dae5 | 2006-07-13 08:45:14 +1000 | [diff] [blame] | 55 | |
| 56 | #include <errno.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 57 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 58 | #include <stdarg.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 59 | #include <string.h> |
| 60 | #include <unistd.h> |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 61 | |
| 62 | #ifdef USE_PAM |
Damien Miller | 0f47c53 | 2004-01-02 18:01:30 +1100 | [diff] [blame] | 63 | #if defined(HAVE_SECURITY_PAM_APPL_H) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 64 | #include <security/pam_appl.h> |
Damien Miller | 0f47c53 | 2004-01-02 18:01:30 +1100 | [diff] [blame] | 65 | #elif defined (HAVE_PAM_PAM_APPL_H) |
| 66 | #include <pam/pam_appl.h> |
| 67 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 68 | |
Damien Miller | 8bd81e1 | 2016-08-16 13:30:56 +1000 | [diff] [blame] | 69 | #if !defined(SSHD_PAM_SERVICE) |
| 70 | extern char *__progname; |
| 71 | # define SSHD_PAM_SERVICE __progname |
| 72 | #endif |
| 73 | |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 74 | /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */ |
| 75 | #ifdef PAM_SUN_CODEBASE |
Darren Tucker | 39c0cec | 2016-05-20 10:01:58 +1000 | [diff] [blame] | 76 | # define sshpam_const /* Solaris, HP-UX, SunOS */ |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 77 | #else |
Darren Tucker | 39c0cec | 2016-05-20 10:01:58 +1000 | [diff] [blame] | 78 | # define sshpam_const const /* LinuxPAM, OpenPAM, AIX */ |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 79 | #endif |
| 80 | |
Damien Miller | 2ab323e | 2006-08-05 12:43:32 +1000 | [diff] [blame] | 81 | /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */ |
| 82 | #ifdef PAM_SUN_CODEBASE |
| 83 | # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member) |
| 84 | #else |
| 85 | # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member) |
| 86 | #endif |
| 87 | |
Damien Miller | 75bb664 | 2006-08-05 14:07:20 +1000 | [diff] [blame] | 88 | #include "xmalloc.h" |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 89 | #include "sshbuf.h" |
| 90 | #include "ssherr.h" |
Damien Miller | 75bb664 | 2006-08-05 14:07:20 +1000 | [diff] [blame] | 91 | #include "hostfile.h" |
Kevin Steves | e683e76 | 2002-04-04 19:02:28 +0000 | [diff] [blame] | 92 | #include "auth.h" |
Damien Miller | 63dc3e9 | 2001-02-07 12:58:33 +1100 | [diff] [blame] | 93 | #include "auth-pam.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 94 | #include "canohost.h" |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 95 | #include "log.h" |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 96 | #include "msg.h" |
| 97 | #include "packet.h" |
Darren Tucker | b6db172 | 2004-05-13 17:29:35 +1000 | [diff] [blame] | 98 | #include "misc.h" |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 99 | #include "servconf.h" |
| 100 | #include "ssh2.h" |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 101 | #include "auth-options.h" |
Damien Miller | 75bb664 | 2006-08-05 14:07:20 +1000 | [diff] [blame] | 102 | #ifdef GSSAPI |
| 103 | #include "ssh-gss.h" |
| 104 | #endif |
| 105 | #include "monitor_wrap.h" |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 106 | |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 107 | extern ServerOptions options; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 108 | extern struct sshbuf *loginmsg; |
Darren Tucker | 2a9bf4b | 2004-04-18 11:00:26 +1000 | [diff] [blame] | 109 | extern u_int utmp_len; |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 110 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 111 | /* so we don't silently change behaviour */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 112 | #ifdef USE_POSIX_THREADS |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 113 | # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK" |
| 114 | #endif |
| 115 | |
| 116 | /* |
| 117 | * Formerly known as USE_POSIX_THREADS, using this is completely unsupported |
| 118 | * and generally a bad idea. Use at own risk and do not expect support if |
| 119 | * this breaks. |
| 120 | */ |
| 121 | #ifdef UNSUPPORTED_POSIX_THREADS_HACK |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 122 | #include <pthread.h> |
| 123 | /* |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 124 | * Avoid namespace clash when *not* using pthreads for systems *with* |
| 125 | * pthreads, which unconditionally define pthread_t via sys/types.h |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 126 | * (e.g. Linux) |
| 127 | */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 128 | typedef pthread_t sp_pthread_t; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 129 | #else |
Darren Tucker | 1b27c8f | 2004-01-13 22:35:58 +1100 | [diff] [blame] | 130 | typedef pid_t sp_pthread_t; |
Damien Miller | 48f54b9 | 2018-09-13 12:13:50 +1000 | [diff] [blame] | 131 | #define pthread_exit fake_pthread_exit |
| 132 | #define pthread_create fake_pthread_create |
| 133 | #define pthread_cancel fake_pthread_cancel |
| 134 | #define pthread_join fake_pthread_join |
Darren Tucker | 1b27c8f | 2004-01-13 22:35:58 +1100 | [diff] [blame] | 135 | #endif |
| 136 | |
| 137 | struct pam_ctxt { |
| 138 | sp_pthread_t pam_thread; |
| 139 | int pam_psock; |
| 140 | int pam_csock; |
| 141 | int pam_done; |
| 142 | }; |
| 143 | |
| 144 | static void sshpam_free_ctx(void *); |
| 145 | static struct pam_ctxt *cleanup_ctxt; |
| 146 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 147 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 148 | /* |
| 149 | * Simulate threads with processes. |
| 150 | */ |
Kevin Steves | 63007d4 | 2002-07-21 17:57:01 +0000 | [diff] [blame] | 151 | |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 152 | static int sshpam_thread_status = -1; |
| 153 | static mysig_t sshpam_oldsig; |
| 154 | |
Damien Miller | 94cf4c8 | 2005-07-17 17:04:47 +1000 | [diff] [blame] | 155 | static void |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 156 | sshpam_sigchld_handler(int sig) |
| 157 | { |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 158 | signal(SIGCHLD, SIG_DFL); |
Darren Tucker | 7ae0962 | 2004-01-14 23:07:56 +1100 | [diff] [blame] | 159 | if (cleanup_ctxt == NULL) |
| 160 | return; /* handler called after PAM cleanup, shouldn't happen */ |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 161 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG) |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 162 | <= 0) { |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 163 | /* PAM thread has not exitted, privsep slave must have */ |
| 164 | kill(cleanup_ctxt->pam_thread, SIGTERM); |
Damien Miller | 10358ab | 2016-07-22 14:06:36 +1000 | [diff] [blame] | 165 | while (waitpid(cleanup_ctxt->pam_thread, |
| 166 | &sshpam_thread_status, 0) == -1) { |
| 167 | if (errno == EINTR) |
| 168 | continue; |
| 169 | return; |
| 170 | } |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 171 | } |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 172 | if (WIFSIGNALED(sshpam_thread_status) && |
| 173 | WTERMSIG(sshpam_thread_status) == SIGTERM) |
| 174 | return; /* terminated by pthread_cancel */ |
| 175 | if (!WIFEXITED(sshpam_thread_status)) |
Darren Tucker | 57d4ca9 | 2007-08-10 14:32:34 +1000 | [diff] [blame] | 176 | sigdie("PAM: authentication thread exited unexpectedly"); |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 177 | if (WEXITSTATUS(sshpam_thread_status) != 0) |
Darren Tucker | 57d4ca9 | 2007-08-10 14:32:34 +1000 | [diff] [blame] | 178 | sigdie("PAM: authentication thread exited uncleanly"); |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 179 | } |
| 180 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 181 | /* ARGSUSED */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 182 | static void |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 183 | pthread_exit(void *value) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 184 | { |
| 185 | _exit(0); |
| 186 | } |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 187 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 188 | /* ARGSUSED */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 189 | static int |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 190 | pthread_create(sp_pthread_t *thread, const void *attr, |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 191 | void *(*thread_start)(void *), void *arg) |
| 192 | { |
| 193 | pid_t pid; |
Darren Tucker | 4f1adad | 2005-07-16 11:33:06 +1000 | [diff] [blame] | 194 | struct pam_ctxt *ctx = arg; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 195 | |
Darren Tucker | b9b6021 | 2004-03-04 20:03:54 +1100 | [diff] [blame] | 196 | sshpam_thread_status = -1; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 197 | switch ((pid = fork())) { |
| 198 | case -1: |
| 199 | error("fork(): %s", strerror(errno)); |
Darren Tucker | d220b67 | 2019-06-07 14:26:54 +1000 | [diff] [blame] | 200 | return errno; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 201 | case 0: |
Darren Tucker | 4f1adad | 2005-07-16 11:33:06 +1000 | [diff] [blame] | 202 | close(ctx->pam_psock); |
| 203 | ctx->pam_psock = -1; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 204 | thread_start(arg); |
| 205 | _exit(1); |
| 206 | default: |
| 207 | *thread = pid; |
Darren Tucker | 4f1adad | 2005-07-16 11:33:06 +1000 | [diff] [blame] | 208 | close(ctx->pam_csock); |
| 209 | ctx->pam_csock = -1; |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 210 | sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 211 | return (0); |
| 212 | } |
| 213 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 214 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 215 | static int |
| 216 | pthread_cancel(sp_pthread_t thread) |
| 217 | { |
Darren Tucker | 7ae0962 | 2004-01-14 23:07:56 +1100 | [diff] [blame] | 218 | signal(SIGCHLD, sshpam_oldsig); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 219 | return (kill(thread, SIGTERM)); |
| 220 | } |
| 221 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 222 | /* ARGSUSED */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 223 | static int |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 224 | pthread_join(sp_pthread_t thread, void **value) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 225 | { |
| 226 | int status; |
| 227 | |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 228 | if (sshpam_thread_status != -1) |
| 229 | return (sshpam_thread_status); |
| 230 | signal(SIGCHLD, sshpam_oldsig); |
Damien Miller | 10358ab | 2016-07-22 14:06:36 +1000 | [diff] [blame] | 231 | while (waitpid(thread, &status, 0) == -1) { |
| 232 | if (errno == EINTR) |
| 233 | continue; |
| 234 | fatal("%s: waitpid: %s", __func__, strerror(errno)); |
| 235 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 236 | return (status); |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 241 | static pam_handle_t *sshpam_handle = NULL; |
| 242 | static int sshpam_err = 0; |
| 243 | static int sshpam_authenticated = 0; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 244 | static int sshpam_session_open = 0; |
| 245 | static int sshpam_cred_established = 0; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 246 | static int sshpam_account_status = -1; |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 247 | static int sshpam_maxtries_reached = 0; |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 248 | static char **sshpam_env = NULL; |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 249 | static Authctxt *sshpam_authctxt = NULL; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 250 | static const char *sshpam_password = NULL; |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 251 | static char *sshpam_rhost = NULL; |
| 252 | static char *sshpam_laddr = NULL; |
| 253 | static char *sshpam_conninfo = NULL; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 254 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 255 | /* Some PAM implementations don't implement this */ |
| 256 | #ifndef HAVE_PAM_GETENVLIST |
| 257 | static char ** |
| 258 | pam_getenvlist(pam_handle_t *pamh) |
| 259 | { |
| 260 | /* |
Darren Tucker | fc0340f | 2019-06-08 00:10:59 +1000 | [diff] [blame] | 261 | * XXX - If necessary, we can still support environment passing |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 262 | * for platforms without pam_getenvlist by searching for known |
| 263 | * env vars (e.g. KRB5CCNAME) from the PAM environment. |
| 264 | */ |
| 265 | return NULL; |
| 266 | } |
| 267 | #endif |
| 268 | |
Darren Tucker | 606077e | 2019-05-17 13:14:12 +1000 | [diff] [blame] | 269 | #ifndef HAVE_PAM_PUTENV |
| 270 | static int |
| 271 | pam_putenv(pam_handle_t *pamh, const char *name_value) |
| 272 | { |
| 273 | return PAM_SUCCESS; |
| 274 | } |
| 275 | #endif /* HAVE_PAM_PUTENV */ |
| 276 | |
Darren Tucker | 21dd089 | 2004-08-16 23:12:05 +1000 | [diff] [blame] | 277 | /* |
| 278 | * Some platforms, notably Solaris, do not enforce password complexity |
| 279 | * rules during pam_chauthtok() if the real uid of the calling process |
| 280 | * is 0, on the assumption that it's being called by "passwd" run by root. |
| 281 | * This wraps pam_chauthtok and sets/restore the real uid so PAM will do |
| 282 | * the right thing. |
| 283 | */ |
| 284 | #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID |
| 285 | static int |
| 286 | sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags) |
| 287 | { |
| 288 | int result; |
| 289 | |
| 290 | if (sshpam_authctxt == NULL) |
| 291 | fatal("PAM: sshpam_authctxt not initialized"); |
| 292 | if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1) |
| 293 | fatal("%s: setreuid failed: %s", __func__, strerror(errno)); |
| 294 | result = pam_chauthtok(pamh, flags); |
| 295 | if (setreuid(0, -1) == -1) |
| 296 | fatal("%s: setreuid failed: %s", __func__, strerror(errno)); |
| 297 | return result; |
| 298 | } |
| 299 | # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b))) |
| 300 | #endif |
| 301 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 302 | void |
Darren Tucker | 17db1c4 | 2004-06-19 12:54:38 +1000 | [diff] [blame] | 303 | sshpam_password_change_required(int reqd) |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 304 | { |
Darren Tucker | 13ef4cf | 2018-03-03 16:21:20 +1100 | [diff] [blame] | 305 | extern struct sshauthopt *auth_opts; |
| 306 | static int saved_port, saved_agent, saved_x11; |
| 307 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 308 | debug3("%s %d", __func__, reqd); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 309 | if (sshpam_authctxt == NULL) |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 310 | fatal("%s: PAM authctxt not initialized", __func__); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 311 | sshpam_authctxt->force_pwchange = reqd; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 312 | if (reqd) { |
Darren Tucker | 13ef4cf | 2018-03-03 16:21:20 +1100 | [diff] [blame] | 313 | saved_port = auth_opts->permit_port_forwarding_flag; |
| 314 | saved_agent = auth_opts->permit_agent_forwarding_flag; |
| 315 | saved_x11 = auth_opts->permit_x11_forwarding_flag; |
| 316 | auth_opts->permit_port_forwarding_flag = 0; |
| 317 | auth_opts->permit_agent_forwarding_flag = 0; |
| 318 | auth_opts->permit_x11_forwarding_flag = 0; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 319 | } else { |
Darren Tucker | 13ef4cf | 2018-03-03 16:21:20 +1100 | [diff] [blame] | 320 | if (saved_port) |
| 321 | auth_opts->permit_port_forwarding_flag = saved_port; |
| 322 | if (saved_agent) |
| 323 | auth_opts->permit_agent_forwarding_flag = saved_agent; |
| 324 | if (saved_x11) |
| 325 | auth_opts->permit_x11_forwarding_flag = saved_x11; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 326 | } |
| 327 | } |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 328 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 329 | /* Import regular and PAM environment from subprocess */ |
| 330 | static void |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 331 | import_environments(struct sshbuf *b) |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 332 | { |
| 333 | char *env; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 334 | u_int n, i, num_env; |
| 335 | int r; |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 336 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 337 | debug3("PAM: %s entering", __func__); |
| 338 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 339 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 340 | /* Import variables set by do_pam_account */ |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 341 | if ((r = sshbuf_get_u32(b, &n)) != 0) |
| 342 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 343 | if (n > INT_MAX) |
| 344 | fatal("%s: invalid PAM account status %u", __func__, n); |
| 345 | sshpam_account_status = (int)n; |
| 346 | if ((r = sshbuf_get_u32(b, &n)) != 0) |
| 347 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 348 | sshpam_password_change_required(n != 0); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 349 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 350 | /* Import environment from subprocess */ |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 351 | if ((r = sshbuf_get_u32(b, &num_env)) != 0) |
| 352 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 353 | if (num_env > 1024) |
| 354 | fatal("%s: received %u environment variables, expected <= 1024", |
| 355 | __func__, num_env); |
| 356 | sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env)); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 357 | debug3("PAM: num env strings %d", num_env); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 358 | for(i = 0; i < num_env; i++) { |
| 359 | if ((r = sshbuf_get_cstring(b, &(sshpam_env[i]), NULL)) != 0) |
| 360 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 361 | } |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 362 | sshpam_env[num_env] = NULL; |
| 363 | |
| 364 | /* Import PAM environment from subprocess */ |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 365 | if ((r = sshbuf_get_u32(b, &num_env)) != 0) |
| 366 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 367 | debug("PAM: num PAM env strings %d", num_env); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 368 | for (i = 0; i < num_env; i++) { |
| 369 | if ((r = sshbuf_get_cstring(b, &env, NULL)) != 0) |
| 370 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 371 | /* Errors are not fatal here */ |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 372 | if ((r = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) { |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 373 | error("PAM: pam_putenv: %s", |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 374 | pam_strerror(sshpam_handle, r)); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 375 | } |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 376 | /* XXX leak env? */ |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 377 | } |
Darren Tucker | 4b385d4 | 2004-03-04 19:54:10 +1100 | [diff] [blame] | 378 | #endif |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 379 | } |
| 380 | |
Damien Miller | 9d5705a | 2000-09-16 16:09:27 +1100 | [diff] [blame] | 381 | /* |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 382 | * Conversation function for authentication thread. |
Damien Miller | 9d5705a | 2000-09-16 16:09:27 +1100 | [diff] [blame] | 383 | */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 384 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 385 | sshpam_thread_conv(int n, sshpam_const struct pam_message **msg, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 386 | struct pam_response **resp, void *data) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 387 | { |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 388 | struct sshbuf *buffer; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 389 | struct pam_ctxt *ctxt; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 390 | struct pam_response *reply; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 391 | int r, i; |
| 392 | u_char status; |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 393 | |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 394 | debug3("PAM: %s entering, %d messages", __func__, n); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 395 | *resp = NULL; |
| 396 | |
Darren Tucker | 59e0602 | 2004-06-30 20:34:31 +1000 | [diff] [blame] | 397 | if (data == NULL) { |
| 398 | error("PAM: conversation function passed a null context"); |
| 399 | return (PAM_CONV_ERR); |
| 400 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 401 | ctxt = data; |
| 402 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 403 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 404 | |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 405 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 406 | return PAM_CONV_ERR; |
| 407 | if ((buffer = sshbuf_new()) == NULL) { |
| 408 | free(reply); |
| 409 | return PAM_CONV_ERR; |
| 410 | } |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 411 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 412 | for (i = 0; i < n; ++i) { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 413 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 414 | case PAM_PROMPT_ECHO_OFF: |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 415 | case PAM_PROMPT_ECHO_ON: |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 416 | if ((r = sshbuf_put_cstring(buffer, |
| 417 | PAM_MSG_MEMBER(msg, i, msg))) != 0) |
| 418 | fatal("%s: buffer error: %s", |
| 419 | __func__, ssh_err(r)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 420 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 421 | PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1) |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 422 | goto fail; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 423 | |
| 424 | if (ssh_msg_recv(ctxt->pam_csock, buffer) == -1) |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 425 | goto fail; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 426 | if ((r = sshbuf_get_u8(buffer, &status)) != 0) |
| 427 | fatal("%s: buffer error: %s", |
| 428 | __func__, ssh_err(r)); |
| 429 | if (status != PAM_AUTHTOK) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 430 | goto fail; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 431 | if ((r = sshbuf_get_cstring(buffer, |
| 432 | &reply[i].resp, NULL)) != 0) |
| 433 | fatal("%s: buffer error: %s", |
| 434 | __func__, ssh_err(r)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 435 | break; |
| 436 | case PAM_ERROR_MSG: |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 437 | case PAM_TEXT_INFO: |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 438 | if ((r = sshbuf_put_cstring(buffer, |
| 439 | PAM_MSG_MEMBER(msg, i, msg))) != 0) |
| 440 | fatal("%s: buffer error: %s", |
| 441 | __func__, ssh_err(r)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 442 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 443 | PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1) |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 444 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 445 | break; |
| 446 | default: |
| 447 | goto fail; |
| 448 | } |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 449 | sshbuf_reset(buffer); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 450 | } |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 451 | sshbuf_free(buffer); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 452 | *resp = reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 453 | return (PAM_SUCCESS); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 454 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 455 | fail: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 456 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 457 | free(reply[i].resp); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 458 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 459 | free(reply); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 460 | sshbuf_free(buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 461 | return (PAM_CONV_ERR); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 464 | /* |
| 465 | * Authentication thread. |
| 466 | */ |
| 467 | static void * |
| 468 | sshpam_thread(void *ctxtp) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 469 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 470 | struct pam_ctxt *ctxt = ctxtp; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 471 | struct sshbuf *buffer = NULL; |
Damien Miller | f4b6f10 | 2003-09-02 23:12:06 +1000 | [diff] [blame] | 472 | struct pam_conv sshpam_conv; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 473 | int r, flags = (options.permit_empty_passwd == 0 ? |
Darren Tucker | 1f7e408 | 2004-07-01 14:00:14 +1000 | [diff] [blame] | 474 | PAM_DISALLOW_NULL_AUTHTOK : 0); |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 475 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 476 | extern char **environ; |
| 477 | char **env_from_pam; |
| 478 | u_int i; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 479 | const char *pam_user; |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 480 | const char **ptr_pam_user = &pam_user; |
Darren Tucker | 54e1b22 | 2006-09-17 11:57:46 +1000 | [diff] [blame] | 481 | char *tz = getenv("TZ"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 482 | |
Darren Tucker | 53f8e78 | 2013-12-19 11:31:44 +1100 | [diff] [blame] | 483 | sshpam_err = pam_get_item(sshpam_handle, PAM_USER, |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 484 | (sshpam_const void **)ptr_pam_user); |
Darren Tucker | 53f8e78 | 2013-12-19 11:31:44 +1100 | [diff] [blame] | 485 | if (sshpam_err != PAM_SUCCESS) |
| 486 | goto auth_fail; |
Darren Tucker | 54e1b22 | 2006-09-17 11:57:46 +1000 | [diff] [blame] | 487 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 488 | environ[0] = NULL; |
Darren Tucker | 54e1b22 | 2006-09-17 11:57:46 +1000 | [diff] [blame] | 489 | if (tz != NULL) |
| 490 | if (setenv("TZ", tz, 1) == -1) |
| 491 | error("PAM: could not set TZ environment: %s", |
| 492 | strerror(errno)); |
Damien Miller | 2d2ed3d | 2004-07-21 20:54:47 +1000 | [diff] [blame] | 493 | |
| 494 | if (sshpam_authctxt != NULL) { |
| 495 | setproctitle("%s [pam]", |
| 496 | sshpam_authctxt->valid ? pam_user : "unknown"); |
| 497 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 498 | #endif |
| 499 | |
Damien Miller | f4b6f10 | 2003-09-02 23:12:06 +1000 | [diff] [blame] | 500 | sshpam_conv.conv = sshpam_thread_conv; |
| 501 | sshpam_conv.appdata_ptr = ctxt; |
| 502 | |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 503 | if (sshpam_authctxt == NULL) |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 504 | fatal("%s: PAM authctxt not initialized", __func__); |
| 505 | |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 506 | if ((buffer = sshbuf_new()) == NULL) |
| 507 | fatal("%s: sshbuf_new failed", __func__); |
| 508 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 509 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
| 510 | (const void *)&sshpam_conv); |
| 511 | if (sshpam_err != PAM_SUCCESS) |
| 512 | goto auth_fail; |
Darren Tucker | 1f7e408 | 2004-07-01 14:00:14 +1000 | [diff] [blame] | 513 | sshpam_err = pam_authenticate(sshpam_handle, flags); |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 514 | if (sshpam_err == PAM_MAXTRIES) |
| 515 | sshpam_set_maxtries_reached(1); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 516 | if (sshpam_err != PAM_SUCCESS) |
| 517 | goto auth_fail; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 518 | |
Darren Tucker | 608ec1f | 2017-03-29 09:50:54 +1100 | [diff] [blame] | 519 | if (!do_pam_account()) { |
| 520 | sshpam_err = PAM_ACCT_EXPIRED; |
| 521 | goto auth_fail; |
| 522 | } |
| 523 | if (sshpam_authctxt->force_pwchange) { |
| 524 | sshpam_err = pam_chauthtok(sshpam_handle, |
| 525 | PAM_CHANGE_EXPIRED_AUTHTOK); |
| 526 | if (sshpam_err != PAM_SUCCESS) |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 527 | goto auth_fail; |
Darren Tucker | 608ec1f | 2017-03-29 09:50:54 +1100 | [diff] [blame] | 528 | sshpam_password_change_required(0); |
Darren Tucker | c376c86 | 2003-12-18 16:08:59 +1100 | [diff] [blame] | 529 | } |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 530 | |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 531 | if ((r = sshbuf_put_cstring(buffer, "OK")) != 0) |
| 532 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 533 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 534 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 535 | /* Export variables set by do_pam_account */ |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 536 | if ((r = sshbuf_put_u32(buffer, sshpam_account_status)) != 0 || |
| 537 | (r = sshbuf_put_u32(buffer, sshpam_authctxt->force_pwchange)) != 0) |
| 538 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 539 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 540 | /* Export any environment strings set in child */ |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 541 | for (i = 0; environ[i] != NULL; i++) { |
| 542 | /* Count */ |
| 543 | if (i > INT_MAX) |
Darren Tucker | fc0340f | 2019-06-08 00:10:59 +1000 | [diff] [blame] | 544 | fatal("%s: too many environment strings", __func__); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 545 | } |
| 546 | if ((r = sshbuf_put_u32(buffer, i)) != 0) |
| 547 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 548 | for (i = 0; environ[i] != NULL; i++) { |
| 549 | if ((r = sshbuf_put_cstring(buffer, environ[i])) != 0) |
| 550 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 551 | } |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 552 | /* Export any environment strings set by PAM in child */ |
| 553 | env_from_pam = pam_getenvlist(sshpam_handle); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 554 | for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) { |
| 555 | /* Count */ |
| 556 | if (i > INT_MAX) |
Darren Tucker | fc0340f | 2019-06-08 00:10:59 +1000 | [diff] [blame] | 557 | fatal("%s: too many PAM environment strings", __func__); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 558 | } |
| 559 | if ((r = sshbuf_put_u32(buffer, i)) != 0) |
| 560 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 561 | for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) { |
| 562 | if ((r = sshbuf_put_cstring(buffer, env_from_pam[i])) != 0) |
| 563 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 564 | } |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 565 | #endif /* UNSUPPORTED_POSIX_THREADS_HACK */ |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 566 | |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 567 | /* XXX - can't do much about an error here */ |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 568 | ssh_msg_send(ctxt->pam_csock, sshpam_err, buffer); |
| 569 | sshbuf_free(buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 570 | pthread_exit(NULL); |
| 571 | |
| 572 | auth_fail: |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 573 | if ((r = sshbuf_put_cstring(buffer, |
| 574 | pam_strerror(sshpam_handle, sshpam_err))) != 0) |
| 575 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 576 | /* XXX - can't do much about an error here */ |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 577 | if (sshpam_err == PAM_ACCT_EXPIRED) |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 578 | ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, buffer); |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 579 | else if (sshpam_maxtries_reached) |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 580 | ssh_msg_send(ctxt->pam_csock, PAM_MAXTRIES, buffer); |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 581 | else |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 582 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, buffer); |
| 583 | sshbuf_free(buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 584 | pthread_exit(NULL); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 585 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 586 | return (NULL); /* Avoid warning for non-pthread case */ |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 587 | } |
| 588 | |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 589 | void |
| 590 | sshpam_thread_cleanup(void) |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 591 | { |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 592 | struct pam_ctxt *ctxt = cleanup_ctxt; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 593 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 594 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 595 | if (ctxt != NULL && ctxt->pam_thread != 0) { |
| 596 | pthread_cancel(ctxt->pam_thread); |
| 597 | pthread_join(ctxt->pam_thread, NULL); |
| 598 | close(ctxt->pam_psock); |
| 599 | close(ctxt->pam_csock); |
| 600 | memset(ctxt, 0, sizeof(*ctxt)); |
| 601 | cleanup_ctxt = NULL; |
| 602 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 603 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 604 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 605 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 606 | sshpam_null_conv(int n, sshpam_const struct pam_message **msg, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 607 | struct pam_response **resp, void *data) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 608 | { |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 609 | debug3("PAM: %s entering, %d messages", __func__, n); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 610 | return (PAM_CONV_ERR); |
| 611 | } |
Damien Miller | 63dc3e9 | 2001-02-07 12:58:33 +1100 | [diff] [blame] | 612 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 613 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
| 614 | |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 615 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 616 | sshpam_store_conv(int n, sshpam_const struct pam_message **msg, |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 617 | struct pam_response **resp, void *data) |
| 618 | { |
| 619 | struct pam_response *reply; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 620 | int r, i; |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 621 | |
| 622 | debug3("PAM: %s called with %d messages", __func__, n); |
| 623 | *resp = NULL; |
| 624 | |
| 625 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 626 | return (PAM_CONV_ERR); |
| 627 | |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 628 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 629 | return (PAM_CONV_ERR); |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 630 | |
| 631 | for (i = 0; i < n; ++i) { |
| 632 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 633 | case PAM_ERROR_MSG: |
| 634 | case PAM_TEXT_INFO: |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 635 | if ((r = sshbuf_putf(loginmsg, "%s\n", |
| 636 | PAM_MSG_MEMBER(msg, i, msg))) != 0) |
| 637 | fatal("%s: buffer error: %s", |
| 638 | __func__, ssh_err(r)); |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 639 | reply[i].resp_retcode = PAM_SUCCESS; |
| 640 | break; |
| 641 | default: |
| 642 | goto fail; |
| 643 | } |
| 644 | } |
| 645 | *resp = reply; |
| 646 | return (PAM_SUCCESS); |
| 647 | |
| 648 | fail: |
| 649 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 650 | free(reply[i].resp); |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 651 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 652 | free(reply); |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 653 | return (PAM_CONV_ERR); |
| 654 | } |
| 655 | |
| 656 | static struct pam_conv store_conv = { sshpam_store_conv, NULL }; |
| 657 | |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 658 | void |
| 659 | sshpam_cleanup(void) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 660 | { |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 661 | if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor())) |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 662 | return; |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 663 | debug("PAM: cleanup"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 664 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 665 | if (sshpam_session_open) { |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 666 | debug("PAM: closing session"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 667 | pam_close_session(sshpam_handle, PAM_SILENT); |
| 668 | sshpam_session_open = 0; |
| 669 | } |
Darren Tucker | 622d5c5 | 2009-07-12 22:07:21 +1000 | [diff] [blame] | 670 | if (sshpam_cred_established) { |
| 671 | debug("PAM: deleting credentials"); |
| 672 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); |
| 673 | sshpam_cred_established = 0; |
| 674 | } |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 675 | sshpam_authenticated = 0; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 676 | pam_end(sshpam_handle, sshpam_err); |
| 677 | sshpam_handle = NULL; |
| 678 | } |
| 679 | |
| 680 | static int |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 681 | sshpam_init(struct ssh *ssh, Authctxt *authctxt) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 682 | { |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 683 | const char *pam_user, *user = authctxt->user; |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 684 | const char **ptr_pam_user = &pam_user; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 685 | |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 686 | if (sshpam_handle == NULL) { |
| 687 | if (ssh == NULL) { |
| 688 | fatal("%s: called initially with no " |
| 689 | "packet context", __func__); |
| 690 | } |
| 691 | } if (sshpam_handle != NULL) { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 692 | /* We already have a PAM context; check if the user matches */ |
| 693 | sshpam_err = pam_get_item(sshpam_handle, |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 694 | PAM_USER, (sshpam_const void **)ptr_pam_user); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 695 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) |
| 696 | return (0); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 697 | pam_end(sshpam_handle, sshpam_err); |
| 698 | sshpam_handle = NULL; |
| 699 | } |
| 700 | debug("PAM: initializing for \"%s\"", user); |
Darren Tucker | c58c2ee | 2003-09-13 22:02:05 +1000 | [diff] [blame] | 701 | sshpam_err = |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 702 | pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 703 | sshpam_authctxt = authctxt; |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 704 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 705 | if (sshpam_err != PAM_SUCCESS) { |
| 706 | pam_end(sshpam_handle, sshpam_err); |
| 707 | sshpam_handle = NULL; |
| 708 | return (-1); |
| 709 | } |
Damien Miller | 8a22ffa | 2018-12-07 15:41:16 +1100 | [diff] [blame] | 710 | |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 711 | if (ssh != NULL && sshpam_rhost == NULL) { |
| 712 | /* |
| 713 | * We need to cache these as we don't have packet context |
| 714 | * during the kbdint flow. |
| 715 | */ |
| 716 | sshpam_rhost = xstrdup(auth_get_canonical_hostname(ssh, |
| 717 | options.use_dns)); |
| 718 | sshpam_laddr = get_local_ipaddr( |
| 719 | ssh_packet_get_connection_in(ssh)); |
| 720 | xasprintf(&sshpam_conninfo, "SSH_CONNECTION=%.50s %d %.50s %d", |
| 721 | ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), |
| 722 | sshpam_laddr, ssh_local_port(ssh)); |
| 723 | } |
| 724 | if (sshpam_rhost != NULL) { |
| 725 | debug("PAM: setting PAM_RHOST to \"%s\"", sshpam_rhost); |
| 726 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, |
| 727 | sshpam_rhost); |
| 728 | if (sshpam_err != PAM_SUCCESS) { |
| 729 | pam_end(sshpam_handle, sshpam_err); |
| 730 | sshpam_handle = NULL; |
| 731 | return (-1); |
| 732 | } |
| 733 | /* Put SSH_CONNECTION in the PAM environment too */ |
| 734 | pam_putenv(sshpam_handle, sshpam_conninfo); |
| 735 | } |
Damien Miller | 8a22ffa | 2018-12-07 15:41:16 +1100 | [diff] [blame] | 736 | |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 737 | #ifdef PAM_TTY_KLUDGE |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 738 | /* |
| 739 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. |
| 740 | * sshd doesn't set the tty until too late in the auth process and |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 741 | * may not even set one (for tty-less connections) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 742 | */ |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 743 | debug("PAM: setting PAM_TTY to \"ssh\""); |
| 744 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); |
| 745 | if (sshpam_err != PAM_SUCCESS) { |
| 746 | pam_end(sshpam_handle, sshpam_err); |
| 747 | sshpam_handle = NULL; |
| 748 | return (-1); |
| 749 | } |
| 750 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 751 | return (0); |
| 752 | } |
| 753 | |
Damien Miller | e8f4745 | 2018-04-06 14:11:44 +1000 | [diff] [blame] | 754 | static void |
| 755 | expose_authinfo(const char *caller) |
| 756 | { |
| 757 | char *auth_info; |
| 758 | |
| 759 | /* |
| 760 | * Expose authentication information to PAM. |
| 761 | * The environment variable is versioned. Please increment the |
| 762 | * version suffix if the format of session_info changes. |
| 763 | */ |
| 764 | if (sshpam_authctxt->session_info == NULL) |
| 765 | auth_info = xstrdup(""); |
| 766 | else if ((auth_info = sshbuf_dup_string( |
| 767 | sshpam_authctxt->session_info)) == NULL) |
| 768 | fatal("%s: sshbuf_dup_string failed", __func__); |
| 769 | |
| 770 | debug2("%s: auth information in SSH_AUTH_INFO_0", caller); |
| 771 | do_pam_putenv("SSH_AUTH_INFO_0", auth_info); |
| 772 | free(auth_info); |
| 773 | } |
| 774 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 775 | static void * |
| 776 | sshpam_init_ctx(Authctxt *authctxt) |
| 777 | { |
| 778 | struct pam_ctxt *ctxt; |
Darren Tucker | d220b67 | 2019-06-07 14:26:54 +1000 | [diff] [blame] | 779 | int result, socks[2]; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 780 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 781 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 782 | /* |
| 783 | * Refuse to start if we don't have PAM enabled or do_pam_account |
| 784 | * has previously failed. |
| 785 | */ |
| 786 | if (!options.use_pam || sshpam_account_status == 0) |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 787 | return NULL; |
| 788 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 789 | /* Initialize PAM */ |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 790 | if (sshpam_init(NULL, authctxt) == -1) { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 791 | error("PAM: initialization failed"); |
| 792 | return (NULL); |
| 793 | } |
| 794 | |
Damien Miller | e8f4745 | 2018-04-06 14:11:44 +1000 | [diff] [blame] | 795 | expose_authinfo(__func__); |
Darren Tucker | 29171e9 | 2007-05-20 15:20:08 +1000 | [diff] [blame] | 796 | ctxt = xcalloc(1, sizeof *ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 797 | |
| 798 | /* Start the authentication thread */ |
| 799 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { |
| 800 | error("PAM: failed create sockets: %s", strerror(errno)); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 801 | free(ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 802 | return (NULL); |
| 803 | } |
| 804 | ctxt->pam_psock = socks[0]; |
| 805 | ctxt->pam_csock = socks[1]; |
Elliott Hughes | 1bd4f7f | 2019-04-25 13:36:27 -0700 | [diff] [blame] | 806 | result = pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt); |
| 807 | if (result != 0) { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 808 | error("PAM: failed to start authentication thread: %s", |
Elliott Hughes | 1bd4f7f | 2019-04-25 13:36:27 -0700 | [diff] [blame] | 809 | strerror(result)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 810 | close(socks[0]); |
| 811 | close(socks[1]); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 812 | free(ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 813 | return (NULL); |
| 814 | } |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 815 | cleanup_ctxt = ctxt; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 816 | return (ctxt); |
| 817 | } |
| 818 | |
| 819 | static int |
| 820 | sshpam_query(void *ctx, char **name, char **info, |
| 821 | u_int *num, char ***prompts, u_int **echo_on) |
| 822 | { |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 823 | struct sshbuf *buffer; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 824 | struct pam_ctxt *ctxt = ctx; |
| 825 | size_t plen; |
| 826 | u_char type; |
| 827 | char *msg; |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 828 | size_t len, mlen; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 829 | int r; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 830 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 831 | debug3("PAM: %s entering", __func__); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 832 | if ((buffer = sshbuf_new()) == NULL) |
| 833 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 834 | *name = xstrdup(""); |
| 835 | *info = xstrdup(""); |
| 836 | *prompts = xmalloc(sizeof(char *)); |
| 837 | **prompts = NULL; |
| 838 | plen = 0; |
| 839 | *echo_on = xmalloc(sizeof(u_int)); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 840 | while (ssh_msg_recv(ctxt->pam_psock, buffer) == 0) { |
| 841 | if ((r = sshbuf_get_u8(buffer, &type)) != 0 || |
| 842 | (r = sshbuf_get_cstring(buffer, &msg, &mlen)) != 0) |
| 843 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 844 | switch (type) { |
| 845 | case PAM_PROMPT_ECHO_ON: |
| 846 | case PAM_PROMPT_ECHO_OFF: |
| 847 | *num = 1; |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 848 | len = plen + mlen + 1; |
Darren Tucker | d1680d3 | 2015-04-30 09:18:11 +1000 | [diff] [blame] | 849 | **prompts = xreallocarray(**prompts, 1, len); |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 850 | strlcpy(**prompts + plen, msg, len - plen); |
| 851 | plen += mlen; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 852 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 853 | free(msg); |
Damien Miller | ec0e624 | 2019-09-13 13:14:39 +1000 | [diff] [blame] | 854 | sshbuf_free(buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 855 | return (0); |
| 856 | case PAM_ERROR_MSG: |
| 857 | case PAM_TEXT_INFO: |
| 858 | /* accumulate messages */ |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 859 | len = plen + mlen + 2; |
Darren Tucker | d1680d3 | 2015-04-30 09:18:11 +1000 | [diff] [blame] | 860 | **prompts = xreallocarray(**prompts, 1, len); |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 861 | strlcpy(**prompts + plen, msg, len - plen); |
| 862 | plen += mlen; |
| 863 | strlcat(**prompts + plen, "\n", len - plen); |
| 864 | plen++; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 865 | free(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 866 | break; |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 867 | case PAM_ACCT_EXPIRED: |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 868 | case PAM_MAXTRIES: |
| 869 | if (type == PAM_ACCT_EXPIRED) |
| 870 | sshpam_account_status = 0; |
| 871 | if (type == PAM_MAXTRIES) |
| 872 | sshpam_set_maxtries_reached(1); |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 873 | /* FALLTHROUGH */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 874 | case PAM_AUTH_ERR: |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 875 | debug3("PAM: %s", pam_strerror(sshpam_handle, type)); |
Darren Tucker | 7b1e695 | 2005-09-28 22:33:27 +1000 | [diff] [blame] | 876 | if (**prompts != NULL && strlen(**prompts) != 0) { |
| 877 | *info = **prompts; |
| 878 | **prompts = NULL; |
| 879 | *num = 0; |
| 880 | **echo_on = 0; |
| 881 | ctxt->pam_done = -1; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 882 | free(msg); |
Damien Miller | ec0e624 | 2019-09-13 13:14:39 +1000 | [diff] [blame] | 883 | sshbuf_free(buffer); |
Darren Tucker | 7b1e695 | 2005-09-28 22:33:27 +1000 | [diff] [blame] | 884 | return 0; |
| 885 | } |
| 886 | /* FALLTHROUGH */ |
| 887 | case PAM_SUCCESS: |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 888 | if (**prompts != NULL) { |
| 889 | /* drain any accumulated messages */ |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 890 | debug("PAM: %s", **prompts); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 891 | if ((r = sshbuf_put(loginmsg, **prompts, |
| 892 | strlen(**prompts))) != 0) |
| 893 | fatal("%s: buffer error: %s", |
| 894 | __func__, ssh_err(r)); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 895 | free(**prompts); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 896 | **prompts = NULL; |
| 897 | } |
| 898 | if (type == PAM_SUCCESS) { |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 899 | if (!sshpam_authctxt->valid || |
| 900 | (sshpam_authctxt->pw->pw_uid == 0 && |
| 901 | options.permit_root_login != PERMIT_YES)) |
Darren Tucker | 36a3d60 | 2005-01-20 12:43:38 +1100 | [diff] [blame] | 902 | fatal("Internal error: PAM auth " |
| 903 | "succeeded when it should have " |
| 904 | "failed"); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 905 | import_environments(buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 906 | *num = 0; |
| 907 | **echo_on = 0; |
| 908 | ctxt->pam_done = 1; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 909 | free(msg); |
Damien Miller | ec0e624 | 2019-09-13 13:14:39 +1000 | [diff] [blame] | 910 | sshbuf_free(buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 911 | return (0); |
| 912 | } |
Darren Tucker | 2a9bf4b | 2004-04-18 11:00:26 +1000 | [diff] [blame] | 913 | error("PAM: %s for %s%.100s from %.100s", msg, |
| 914 | sshpam_authctxt->valid ? "" : "illegal user ", |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 915 | sshpam_authctxt->user, sshpam_rhost); |
Darren Tucker | 439ce0d | 2003-10-09 14:20:15 +1000 | [diff] [blame] | 916 | /* FALLTHROUGH */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 917 | default: |
| 918 | *num = 0; |
| 919 | **echo_on = 0; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 920 | free(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 921 | ctxt->pam_done = -1; |
Damien Miller | ec0e624 | 2019-09-13 13:14:39 +1000 | [diff] [blame] | 922 | sshbuf_free(buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 923 | return (-1); |
| 924 | } |
| 925 | } |
Damien Miller | ec0e624 | 2019-09-13 13:14:39 +1000 | [diff] [blame] | 926 | sshbuf_free(buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 927 | return (-1); |
| 928 | } |
| 929 | |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 930 | /* |
| 931 | * Returns a junk password of identical length to that the user supplied. |
| 932 | * Used to mitigate timing attacks against crypt(3)/PAM stacks that |
| 933 | * vary processing time in proportion to password length. |
| 934 | */ |
| 935 | static char * |
| 936 | fake_password(const char *wire_password) |
| 937 | { |
| 938 | const char junk[] = "\b\n\r\177INCORRECT"; |
| 939 | char *ret = NULL; |
| 940 | size_t i, l = wire_password != NULL ? strlen(wire_password) : 0; |
| 941 | |
| 942 | if (l >= INT_MAX) |
| 943 | fatal("%s: password length too long: %zu", __func__, l); |
| 944 | |
| 945 | ret = malloc(l + 1); |
Darren Tucker | bee0167 | 2017-03-10 13:40:18 +1100 | [diff] [blame] | 946 | if (ret == NULL) |
| 947 | return NULL; |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 948 | for (i = 0; i < l; i++) |
| 949 | ret[i] = junk[i % (sizeof(junk) - 1)]; |
| 950 | ret[i] = '\0'; |
| 951 | return ret; |
| 952 | } |
| 953 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 954 | /* XXX - see also comment in auth-chall.c:verify_response */ |
| 955 | static int |
| 956 | sshpam_respond(void *ctx, u_int num, char **resp) |
| 957 | { |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 958 | struct sshbuf *buffer; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 959 | struct pam_ctxt *ctxt = ctx; |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 960 | char *fake; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 961 | int r; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 962 | |
Darren Tucker | 1d4ebbf | 2006-01-29 16:46:13 +1100 | [diff] [blame] | 963 | debug2("PAM: %s entering, %u responses", __func__, num); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 964 | switch (ctxt->pam_done) { |
| 965 | case 1: |
| 966 | sshpam_authenticated = 1; |
| 967 | return (0); |
| 968 | case 0: |
| 969 | break; |
| 970 | default: |
| 971 | return (-1); |
| 972 | } |
| 973 | if (num != 1) { |
| 974 | error("PAM: expected one response, got %u", num); |
| 975 | return (-1); |
| 976 | } |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 977 | if ((buffer = sshbuf_new()) == NULL) |
| 978 | fatal("%s: sshbuf_new failed", __func__); |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 979 | if (sshpam_authctxt->valid && |
| 980 | (sshpam_authctxt->pw->pw_uid != 0 || |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 981 | options.permit_root_login == PERMIT_YES)) { |
| 982 | if ((r = sshbuf_put_cstring(buffer, *resp)) != 0) |
| 983 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 984 | } else { |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 985 | fake = fake_password(*resp); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 986 | if ((r = sshbuf_put_cstring(buffer, fake)) != 0) |
| 987 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 988 | free(fake); |
| 989 | } |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 990 | if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, buffer) == -1) { |
| 991 | sshbuf_free(buffer); |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 992 | return (-1); |
| 993 | } |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 994 | sshbuf_free(buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 995 | return (1); |
| 996 | } |
| 997 | |
| 998 | static void |
| 999 | sshpam_free_ctx(void *ctxtp) |
| 1000 | { |
| 1001 | struct pam_ctxt *ctxt = ctxtp; |
| 1002 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 1003 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 1004 | sshpam_thread_cleanup(); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1005 | free(ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1006 | /* |
| 1007 | * We don't call sshpam_cleanup() here because we may need the PAM |
| 1008 | * handle at a later stage, e.g. when setting up a session. It's |
| 1009 | * still on the cleanup list, so pam_end() *will* be called before |
| 1010 | * the server process terminates. |
| 1011 | */ |
| 1012 | } |
| 1013 | |
| 1014 | KbdintDevice sshpam_device = { |
| 1015 | "pam", |
| 1016 | sshpam_init_ctx, |
| 1017 | sshpam_query, |
| 1018 | sshpam_respond, |
| 1019 | sshpam_free_ctx |
| 1020 | }; |
| 1021 | |
| 1022 | KbdintDevice mm_sshpam_device = { |
| 1023 | "pam", |
| 1024 | mm_sshpam_init_ctx, |
| 1025 | mm_sshpam_query, |
| 1026 | mm_sshpam_respond, |
| 1027 | mm_sshpam_free_ctx |
| 1028 | }; |
| 1029 | |
| 1030 | /* |
| 1031 | * This replaces auth-pam.c |
| 1032 | */ |
| 1033 | void |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 1034 | start_pam(struct ssh *ssh) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1035 | { |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 1036 | Authctxt *authctxt = (Authctxt *)ssh->authctxt; |
| 1037 | |
Damien Miller | 9d507da | 2003-05-14 15:31:12 +1000 | [diff] [blame] | 1038 | if (!options.use_pam) |
| 1039 | fatal("PAM: initialisation requested when UsePAM=no"); |
| 1040 | |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 1041 | if (sshpam_init(ssh, authctxt) == -1) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1042 | fatal("PAM: initialisation failed"); |
| 1043 | } |
| 1044 | |
| 1045 | void |
| 1046 | finish_pam(void) |
| 1047 | { |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 1048 | sshpam_cleanup(); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1049 | } |
| 1050 | |
Damien Miller | 94bc1e7 | 2017-07-28 14:50:59 +1000 | [diff] [blame] | 1051 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1052 | u_int |
| 1053 | do_pam_account(void) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1054 | { |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 1055 | debug("%s: called", __func__); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 1056 | if (sshpam_account_status != -1) |
| 1057 | return (sshpam_account_status); |
| 1058 | |
Damien Miller | 94bc1e7 | 2017-07-28 14:50:59 +1000 | [diff] [blame] | 1059 | expose_authinfo(__func__); |
| 1060 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1061 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 1062 | debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err, |
| 1063 | pam_strerror(sshpam_handle, sshpam_err)); |
Damien Miller | 94cf4c8 | 2005-07-17 17:04:47 +1000 | [diff] [blame] | 1064 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 1065 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { |
| 1066 | sshpam_account_status = 0; |
| 1067 | return (sshpam_account_status); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1068 | } |
| 1069 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 1070 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) |
Darren Tucker | 17db1c4 | 2004-06-19 12:54:38 +1000 | [diff] [blame] | 1071 | sshpam_password_change_required(1); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 1072 | |
| 1073 | sshpam_account_status = 1; |
| 1074 | return (sshpam_account_status); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | void |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1078 | do_pam_setcred(int init) |
| 1079 | { |
| 1080 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 1081 | (const void *)&store_conv); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1082 | if (sshpam_err != PAM_SUCCESS) |
| 1083 | fatal("PAM: failed to set PAM_CONV: %s", |
| 1084 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1085 | if (init) { |
| 1086 | debug("PAM: establishing credentials"); |
| 1087 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); |
| 1088 | } else { |
| 1089 | debug("PAM: reinitializing credentials"); |
| 1090 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); |
| 1091 | } |
| 1092 | if (sshpam_err == PAM_SUCCESS) { |
| 1093 | sshpam_cred_established = 1; |
| 1094 | return; |
| 1095 | } |
| 1096 | if (sshpam_authenticated) |
| 1097 | fatal("PAM: pam_setcred(): %s", |
| 1098 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1099 | else |
| 1100 | debug("PAM: pam_setcred(): %s", |
| 1101 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1102 | } |
| 1103 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1104 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 1105 | sshpam_tty_conv(int n, sshpam_const struct pam_message **msg, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1106 | struct pam_response **resp, void *data) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1107 | { |
| 1108 | char input[PAM_MAX_MSG_SIZE]; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1109 | struct pam_response *reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1110 | int i; |
| 1111 | |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 1112 | debug3("PAM: %s called with %d messages", __func__, n); |
| 1113 | |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1114 | *resp = NULL; |
| 1115 | |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1116 | if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1117 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1118 | |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 1119 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1120 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1121 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1122 | for (i = 0; i < n; ++i) { |
| 1123 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 1124 | case PAM_PROMPT_ECHO_OFF: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1125 | reply[i].resp = |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1126 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1127 | RP_ALLOW_STDIN); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1128 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1129 | break; |
| 1130 | case PAM_PROMPT_ECHO_ON: |
Darren Tucker | 0947ddf | 2003-11-13 11:21:31 +1100 | [diff] [blame] | 1131 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
Darren Tucker | 2216471 | 2007-05-20 15:26:07 +1000 | [diff] [blame] | 1132 | if (fgets(input, sizeof input, stdin) == NULL) |
| 1133 | input[0] = '\0'; |
Damien Miller | a6fb77f | 2004-07-19 09:39:11 +1000 | [diff] [blame] | 1134 | if ((reply[i].resp = strdup(input)) == NULL) |
| 1135 | goto fail; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1136 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1137 | break; |
| 1138 | case PAM_ERROR_MSG: |
| 1139 | case PAM_TEXT_INFO: |
Darren Tucker | 0947ddf | 2003-11-13 11:21:31 +1100 | [diff] [blame] | 1140 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1141 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1142 | break; |
| 1143 | default: |
| 1144 | goto fail; |
| 1145 | } |
| 1146 | } |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1147 | *resp = reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1148 | return (PAM_SUCCESS); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1149 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1150 | fail: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1151 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1152 | free(reply[i].resp); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1153 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1154 | free(reply); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1155 | return (PAM_CONV_ERR); |
| 1156 | } |
| 1157 | |
Darren Tucker | 94befab | 2004-06-03 14:53:12 +1000 | [diff] [blame] | 1158 | static struct pam_conv tty_conv = { sshpam_tty_conv, NULL }; |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1159 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1160 | /* |
| 1161 | * XXX this should be done in the authentication phase, but ssh1 doesn't |
| 1162 | * support that |
| 1163 | */ |
| 1164 | void |
| 1165 | do_pam_chauthtok(void) |
| 1166 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1167 | if (use_privsep) |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1168 | fatal("Password expired (unable to change with privsep)"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1169 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1170 | (const void *)&tty_conv); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1171 | if (sshpam_err != PAM_SUCCESS) |
| 1172 | fatal("PAM: failed to set PAM_CONV: %s", |
| 1173 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1174 | debug("PAM: changing password"); |
| 1175 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); |
| 1176 | if (sshpam_err != PAM_SUCCESS) |
| 1177 | fatal("PAM: pam_chauthtok(): %s", |
| 1178 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1179 | } |
| 1180 | |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1181 | void |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 1182 | do_pam_session(struct ssh *ssh) |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1183 | { |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 1184 | debug3("PAM: opening session"); |
Damien Miller | 94bc1e7 | 2017-07-28 14:50:59 +1000 | [diff] [blame] | 1185 | |
| 1186 | expose_authinfo(__func__); |
| 1187 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1188 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 5cf8ef7 | 2004-02-17 23:20:07 +1100 | [diff] [blame] | 1189 | (const void *)&store_conv); |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1190 | if (sshpam_err != PAM_SUCCESS) |
| 1191 | fatal("PAM: failed to set PAM_CONV: %s", |
| 1192 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1193 | sshpam_err = pam_open_session(sshpam_handle, 0); |
Darren Tucker | 69687f4 | 2004-09-11 22:17:26 +1000 | [diff] [blame] | 1194 | if (sshpam_err == PAM_SUCCESS) |
| 1195 | sshpam_session_open = 1; |
| 1196 | else { |
| 1197 | sshpam_session_open = 0; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 1198 | auth_restrict_session(ssh); |
Darren Tucker | 69687f4 | 2004-09-11 22:17:26 +1000 | [diff] [blame] | 1199 | error("PAM: pam_open_session(): %s", |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1200 | pam_strerror(sshpam_handle, sshpam_err)); |
Darren Tucker | 69687f4 | 2004-09-11 22:17:26 +1000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | } |
| 1204 | |
| 1205 | int |
| 1206 | is_pam_session_open(void) |
| 1207 | { |
| 1208 | return sshpam_session_open; |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1209 | } |
| 1210 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1211 | /* |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1212 | * Set a PAM environment string. We need to do this so that the session |
| 1213 | * modules can handle things like Kerberos/GSI credentials that appear |
| 1214 | * during the ssh authentication process. |
| 1215 | */ |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1216 | int |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1217 | do_pam_putenv(char *name, char *value) |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1218 | { |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1219 | int ret = 1; |
Damien Miller | f272809 | 2003-09-17 07:24:25 +1000 | [diff] [blame] | 1220 | char *compound; |
| 1221 | size_t len; |
| 1222 | |
| 1223 | len = strlen(name) + strlen(value) + 2; |
| 1224 | compound = xmalloc(len); |
| 1225 | |
| 1226 | snprintf(compound, len, "%s=%s", name, value); |
| 1227 | ret = pam_putenv(sshpam_handle, compound); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1228 | free(compound); |
Damien Miller | f272809 | 2003-09-17 07:24:25 +1000 | [diff] [blame] | 1229 | |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1230 | return (ret); |
| 1231 | } |
| 1232 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1233 | char ** |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 1234 | fetch_pam_child_environment(void) |
| 1235 | { |
| 1236 | return sshpam_env; |
| 1237 | } |
| 1238 | |
| 1239 | char ** |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1240 | fetch_pam_environment(void) |
| 1241 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1242 | return (pam_getenvlist(sshpam_handle)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | void |
| 1246 | free_pam_environment(char **env) |
| 1247 | { |
| 1248 | char **envp; |
| 1249 | |
Damien Miller | e27c6cc | 2003-05-16 18:21:01 +1000 | [diff] [blame] | 1250 | if (env == NULL) |
| 1251 | return; |
| 1252 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1253 | for (envp = env; *envp; envp++) |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1254 | free(*envp); |
| 1255 | free(env); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1256 | } |
| 1257 | |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1258 | /* |
| 1259 | * "Blind" conversation function for password authentication. Assumes that |
| 1260 | * echo-off prompts are for the password and stores messages for later |
| 1261 | * display. |
| 1262 | */ |
| 1263 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 1264 | sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg, |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1265 | struct pam_response **resp, void *data) |
| 1266 | { |
| 1267 | struct pam_response *reply; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 1268 | int r, i; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1269 | size_t len; |
| 1270 | |
| 1271 | debug3("PAM: %s called with %d messages", __func__, n); |
| 1272 | |
| 1273 | *resp = NULL; |
| 1274 | |
| 1275 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 1276 | return (PAM_CONV_ERR); |
| 1277 | |
Darren Tucker | 29171e9 | 2007-05-20 15:20:08 +1000 | [diff] [blame] | 1278 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1279 | return (PAM_CONV_ERR); |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1280 | |
| 1281 | for (i = 0; i < n; ++i) { |
| 1282 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 1283 | case PAM_PROMPT_ECHO_OFF: |
| 1284 | if (sshpam_password == NULL) |
| 1285 | goto fail; |
Damien Miller | a6fb77f | 2004-07-19 09:39:11 +1000 | [diff] [blame] | 1286 | if ((reply[i].resp = strdup(sshpam_password)) == NULL) |
| 1287 | goto fail; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1288 | reply[i].resp_retcode = PAM_SUCCESS; |
| 1289 | break; |
| 1290 | case PAM_ERROR_MSG: |
| 1291 | case PAM_TEXT_INFO: |
| 1292 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); |
| 1293 | if (len > 0) { |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 1294 | if ((r = sshbuf_putf(loginmsg, "%s\n", |
| 1295 | PAM_MSG_MEMBER(msg, i, msg))) != 0) |
| 1296 | fatal("%s: buffer error: %s", |
| 1297 | __func__, ssh_err(r)); |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1298 | } |
Damien Miller | a6fb77f | 2004-07-19 09:39:11 +1000 | [diff] [blame] | 1299 | if ((reply[i].resp = strdup("")) == NULL) |
| 1300 | goto fail; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1301 | reply[i].resp_retcode = PAM_SUCCESS; |
| 1302 | break; |
| 1303 | default: |
| 1304 | goto fail; |
| 1305 | } |
| 1306 | } |
| 1307 | *resp = reply; |
| 1308 | return (PAM_SUCCESS); |
| 1309 | |
Damien Miller | 94cf4c8 | 2005-07-17 17:04:47 +1000 | [diff] [blame] | 1310 | fail: |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1311 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1312 | free(reply[i].resp); |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1313 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1314 | free(reply); |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1315 | return (PAM_CONV_ERR); |
| 1316 | } |
| 1317 | |
| 1318 | static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL }; |
| 1319 | |
| 1320 | /* |
| 1321 | * Attempt password authentication via PAM |
| 1322 | */ |
| 1323 | int |
| 1324 | sshpam_auth_passwd(Authctxt *authctxt, const char *password) |
| 1325 | { |
| 1326 | int flags = (options.permit_empty_passwd == 0 ? |
| 1327 | PAM_DISALLOW_NULL_AUTHTOK : 0); |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 1328 | char *fake = NULL; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1329 | |
| 1330 | if (!options.use_pam || sshpam_handle == NULL) |
| 1331 | fatal("PAM: %s called when PAM disabled or failed to " |
| 1332 | "initialise.", __func__); |
| 1333 | |
| 1334 | sshpam_password = password; |
| 1335 | sshpam_authctxt = authctxt; |
| 1336 | |
Darren Tucker | e061b15 | 2004-05-30 22:04:56 +1000 | [diff] [blame] | 1337 | /* |
| 1338 | * If the user logging in is invalid, or is root but is not permitted |
| 1339 | * by PermitRootLogin, use an invalid password to prevent leaking |
| 1340 | * information via timing (eg if the PAM config has a delay on fail). |
| 1341 | */ |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 1342 | if (!authctxt->valid || (authctxt->pw->pw_uid == 0 && |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 1343 | options.permit_root_login != PERMIT_YES)) |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 1344 | sshpam_password = fake = fake_password(password); |
Darren Tucker | e061b15 | 2004-05-30 22:04:56 +1000 | [diff] [blame] | 1345 | |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1346 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
| 1347 | (const void *)&passwd_conv); |
| 1348 | if (sshpam_err != PAM_SUCCESS) |
| 1349 | fatal("PAM: %s: failed to set PAM_CONV: %s", __func__, |
| 1350 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1351 | |
| 1352 | sshpam_err = pam_authenticate(sshpam_handle, flags); |
| 1353 | sshpam_password = NULL; |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 1354 | free(fake); |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 1355 | if (sshpam_err == PAM_MAXTRIES) |
| 1356 | sshpam_set_maxtries_reached(1); |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 1357 | if (sshpam_err == PAM_SUCCESS && authctxt->valid) { |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1358 | debug("PAM: password authentication accepted for %.100s", |
| 1359 | authctxt->user); |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 1360 | return 1; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1361 | } else { |
| 1362 | debug("PAM: password authentication failed for %.100s: %s", |
| 1363 | authctxt->valid ? authctxt->user : "an illegal user", |
| 1364 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1365 | return 0; |
| 1366 | } |
| 1367 | } |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 1368 | |
| 1369 | int |
| 1370 | sshpam_get_maxtries_reached(void) |
| 1371 | { |
| 1372 | return sshpam_maxtries_reached; |
| 1373 | } |
| 1374 | |
| 1375 | void |
| 1376 | sshpam_set_maxtries_reached(int reached) |
| 1377 | { |
| 1378 | if (reached == 0 || sshpam_maxtries_reached) |
| 1379 | return; |
| 1380 | sshpam_maxtries_reached = 1; |
| 1381 | options.password_authentication = 0; |
| 1382 | options.kbd_interactive_authentication = 0; |
| 1383 | options.challenge_response_authentication = 0; |
| 1384 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1385 | #endif /* USE_PAM */ |