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" |
| 89 | #include "buffer.h" |
| 90 | #include "key.h" |
| 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; |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 108 | extern Buffer 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; |
| 131 | #endif |
| 132 | |
| 133 | struct pam_ctxt { |
| 134 | sp_pthread_t pam_thread; |
| 135 | int pam_psock; |
| 136 | int pam_csock; |
| 137 | int pam_done; |
| 138 | }; |
| 139 | |
| 140 | static void sshpam_free_ctx(void *); |
| 141 | static struct pam_ctxt *cleanup_ctxt; |
| 142 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 143 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 144 | /* |
| 145 | * Simulate threads with processes. |
| 146 | */ |
Kevin Steves | 63007d4 | 2002-07-21 17:57:01 +0000 | [diff] [blame] | 147 | |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 148 | static int sshpam_thread_status = -1; |
| 149 | static mysig_t sshpam_oldsig; |
| 150 | |
Damien Miller | 94cf4c8 | 2005-07-17 17:04:47 +1000 | [diff] [blame] | 151 | static void |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 152 | sshpam_sigchld_handler(int sig) |
| 153 | { |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 154 | signal(SIGCHLD, SIG_DFL); |
Darren Tucker | 7ae0962 | 2004-01-14 23:07:56 +1100 | [diff] [blame] | 155 | if (cleanup_ctxt == NULL) |
| 156 | return; /* handler called after PAM cleanup, shouldn't happen */ |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 157 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG) |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 158 | <= 0) { |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 159 | /* PAM thread has not exitted, privsep slave must have */ |
| 160 | kill(cleanup_ctxt->pam_thread, SIGTERM); |
Damien Miller | 10358ab | 2016-07-22 14:06:36 +1000 | [diff] [blame] | 161 | while (waitpid(cleanup_ctxt->pam_thread, |
| 162 | &sshpam_thread_status, 0) == -1) { |
| 163 | if (errno == EINTR) |
| 164 | continue; |
| 165 | return; |
| 166 | } |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 167 | } |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 168 | if (WIFSIGNALED(sshpam_thread_status) && |
| 169 | WTERMSIG(sshpam_thread_status) == SIGTERM) |
| 170 | return; /* terminated by pthread_cancel */ |
| 171 | if (!WIFEXITED(sshpam_thread_status)) |
Darren Tucker | 57d4ca9 | 2007-08-10 14:32:34 +1000 | [diff] [blame] | 172 | sigdie("PAM: authentication thread exited unexpectedly"); |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 173 | if (WEXITSTATUS(sshpam_thread_status) != 0) |
Darren Tucker | 57d4ca9 | 2007-08-10 14:32:34 +1000 | [diff] [blame] | 174 | sigdie("PAM: authentication thread exited uncleanly"); |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 175 | } |
| 176 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 177 | /* ARGSUSED */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 178 | static void |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 179 | pthread_exit(void *value) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 180 | { |
| 181 | _exit(0); |
| 182 | } |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 183 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 184 | /* ARGSUSED */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 185 | static int |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 186 | pthread_create(sp_pthread_t *thread, const void *attr, |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 187 | void *(*thread_start)(void *), void *arg) |
| 188 | { |
| 189 | pid_t pid; |
Darren Tucker | 4f1adad | 2005-07-16 11:33:06 +1000 | [diff] [blame] | 190 | struct pam_ctxt *ctx = arg; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 191 | |
Darren Tucker | b9b6021 | 2004-03-04 20:03:54 +1100 | [diff] [blame] | 192 | sshpam_thread_status = -1; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 193 | switch ((pid = fork())) { |
| 194 | case -1: |
| 195 | error("fork(): %s", strerror(errno)); |
| 196 | return (-1); |
| 197 | case 0: |
Darren Tucker | 4f1adad | 2005-07-16 11:33:06 +1000 | [diff] [blame] | 198 | close(ctx->pam_psock); |
| 199 | ctx->pam_psock = -1; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 200 | thread_start(arg); |
| 201 | _exit(1); |
| 202 | default: |
| 203 | *thread = pid; |
Darren Tucker | 4f1adad | 2005-07-16 11:33:06 +1000 | [diff] [blame] | 204 | close(ctx->pam_csock); |
| 205 | ctx->pam_csock = -1; |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 206 | sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 207 | return (0); |
| 208 | } |
| 209 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 210 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 211 | static int |
| 212 | pthread_cancel(sp_pthread_t thread) |
| 213 | { |
Darren Tucker | 7ae0962 | 2004-01-14 23:07:56 +1100 | [diff] [blame] | 214 | signal(SIGCHLD, sshpam_oldsig); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 215 | return (kill(thread, SIGTERM)); |
| 216 | } |
| 217 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 218 | /* ARGSUSED */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 219 | static int |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 220 | pthread_join(sp_pthread_t thread, void **value) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 221 | { |
| 222 | int status; |
| 223 | |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 224 | if (sshpam_thread_status != -1) |
| 225 | return (sshpam_thread_status); |
| 226 | signal(SIGCHLD, sshpam_oldsig); |
Damien Miller | 10358ab | 2016-07-22 14:06:36 +1000 | [diff] [blame] | 227 | while (waitpid(thread, &status, 0) == -1) { |
| 228 | if (errno == EINTR) |
| 229 | continue; |
| 230 | fatal("%s: waitpid: %s", __func__, strerror(errno)); |
| 231 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 232 | return (status); |
| 233 | } |
| 234 | #endif |
| 235 | |
| 236 | |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 237 | static pam_handle_t *sshpam_handle = NULL; |
| 238 | static int sshpam_err = 0; |
| 239 | static int sshpam_authenticated = 0; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 240 | static int sshpam_session_open = 0; |
| 241 | static int sshpam_cred_established = 0; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 242 | static int sshpam_account_status = -1; |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 243 | static int sshpam_maxtries_reached = 0; |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 244 | static char **sshpam_env = NULL; |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 245 | static Authctxt *sshpam_authctxt = NULL; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 246 | static const char *sshpam_password = NULL; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 247 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 248 | /* Some PAM implementations don't implement this */ |
| 249 | #ifndef HAVE_PAM_GETENVLIST |
| 250 | static char ** |
| 251 | pam_getenvlist(pam_handle_t *pamh) |
| 252 | { |
| 253 | /* |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 254 | * XXX - If necessary, we can still support envrionment passing |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 255 | * for platforms without pam_getenvlist by searching for known |
| 256 | * env vars (e.g. KRB5CCNAME) from the PAM environment. |
| 257 | */ |
| 258 | return NULL; |
| 259 | } |
| 260 | #endif |
| 261 | |
Darren Tucker | 21dd089 | 2004-08-16 23:12:05 +1000 | [diff] [blame] | 262 | /* |
| 263 | * Some platforms, notably Solaris, do not enforce password complexity |
| 264 | * rules during pam_chauthtok() if the real uid of the calling process |
| 265 | * is 0, on the assumption that it's being called by "passwd" run by root. |
| 266 | * This wraps pam_chauthtok and sets/restore the real uid so PAM will do |
| 267 | * the right thing. |
| 268 | */ |
| 269 | #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID |
| 270 | static int |
| 271 | sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags) |
| 272 | { |
| 273 | int result; |
| 274 | |
| 275 | if (sshpam_authctxt == NULL) |
| 276 | fatal("PAM: sshpam_authctxt not initialized"); |
| 277 | if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1) |
| 278 | fatal("%s: setreuid failed: %s", __func__, strerror(errno)); |
| 279 | result = pam_chauthtok(pamh, flags); |
| 280 | if (setreuid(0, -1) == -1) |
| 281 | fatal("%s: setreuid failed: %s", __func__, strerror(errno)); |
| 282 | return result; |
| 283 | } |
| 284 | # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b))) |
| 285 | #endif |
| 286 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 287 | void |
Darren Tucker | 17db1c4 | 2004-06-19 12:54:38 +1000 | [diff] [blame] | 288 | sshpam_password_change_required(int reqd) |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 289 | { |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 290 | debug3("%s %d", __func__, reqd); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 291 | if (sshpam_authctxt == NULL) |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 292 | fatal("%s: PAM authctxt not initialized", __func__); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 293 | sshpam_authctxt->force_pwchange = reqd; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 294 | if (reqd) { |
| 295 | no_port_forwarding_flag |= 2; |
| 296 | no_agent_forwarding_flag |= 2; |
| 297 | no_x11_forwarding_flag |= 2; |
| 298 | } else { |
| 299 | no_port_forwarding_flag &= ~2; |
| 300 | no_agent_forwarding_flag &= ~2; |
| 301 | no_x11_forwarding_flag &= ~2; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 302 | } |
| 303 | } |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 304 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 305 | /* Import regular and PAM environment from subprocess */ |
| 306 | static void |
| 307 | import_environments(Buffer *b) |
| 308 | { |
| 309 | char *env; |
| 310 | u_int i, num_env; |
| 311 | int err; |
| 312 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 313 | debug3("PAM: %s entering", __func__); |
| 314 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 315 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 316 | /* Import variables set by do_pam_account */ |
| 317 | sshpam_account_status = buffer_get_int(b); |
Darren Tucker | 17db1c4 | 2004-06-19 12:54:38 +1000 | [diff] [blame] | 318 | sshpam_password_change_required(buffer_get_int(b)); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 319 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 320 | /* Import environment from subprocess */ |
| 321 | num_env = buffer_get_int(b); |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 322 | if (num_env > 1024) |
| 323 | fatal("%s: received %u environment variables, expected <= 1024", |
| 324 | __func__, num_env); |
| 325 | sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env)); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 326 | debug3("PAM: num env strings %d", num_env); |
| 327 | for(i = 0; i < num_env; i++) |
| 328 | sshpam_env[i] = buffer_get_string(b, NULL); |
| 329 | |
| 330 | sshpam_env[num_env] = NULL; |
| 331 | |
| 332 | /* Import PAM environment from subprocess */ |
| 333 | num_env = buffer_get_int(b); |
| 334 | debug("PAM: num PAM env strings %d", num_env); |
| 335 | for(i = 0; i < num_env; i++) { |
| 336 | env = buffer_get_string(b, NULL); |
| 337 | |
Darren Tucker | 8a1624c | 2003-11-18 12:45:35 +1100 | [diff] [blame] | 338 | #ifdef HAVE_PAM_PUTENV |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 339 | /* Errors are not fatal here */ |
| 340 | if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) { |
| 341 | error("PAM: pam_putenv: %s", |
| 342 | pam_strerror(sshpam_handle, sshpam_err)); |
| 343 | } |
Darren Tucker | 8a1624c | 2003-11-18 12:45:35 +1100 | [diff] [blame] | 344 | #endif |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 345 | } |
Darren Tucker | 4b385d4 | 2004-03-04 19:54:10 +1100 | [diff] [blame] | 346 | #endif |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 347 | } |
| 348 | |
Damien Miller | 9d5705a | 2000-09-16 16:09:27 +1100 | [diff] [blame] | 349 | /* |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 350 | * Conversation function for authentication thread. |
Damien Miller | 9d5705a | 2000-09-16 16:09:27 +1100 | [diff] [blame] | 351 | */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 352 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 353 | sshpam_thread_conv(int n, sshpam_const struct pam_message **msg, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 354 | struct pam_response **resp, void *data) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 355 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 356 | Buffer buffer; |
| 357 | struct pam_ctxt *ctxt; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 358 | struct pam_response *reply; |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 359 | int i; |
| 360 | |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 361 | debug3("PAM: %s entering, %d messages", __func__, n); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 362 | *resp = NULL; |
| 363 | |
Darren Tucker | 59e0602 | 2004-06-30 20:34:31 +1000 | [diff] [blame] | 364 | if (data == NULL) { |
| 365 | error("PAM: conversation function passed a null context"); |
| 366 | return (PAM_CONV_ERR); |
| 367 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 368 | ctxt = data; |
| 369 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 370 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 371 | |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 372 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 373 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 374 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 375 | buffer_init(&buffer); |
| 376 | for (i = 0; i < n; ++i) { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 377 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 378 | case PAM_PROMPT_ECHO_OFF: |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 379 | case PAM_PROMPT_ECHO_ON: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 380 | buffer_put_cstring(&buffer, |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 381 | PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 382 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 383 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
| 384 | goto fail; |
| 385 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) |
| 386 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 387 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
| 388 | goto fail; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 389 | reply[i].resp = buffer_get_string(&buffer, NULL); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 390 | break; |
| 391 | case PAM_ERROR_MSG: |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 392 | case PAM_TEXT_INFO: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 393 | buffer_put_cstring(&buffer, |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 394 | PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 395 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 396 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
| 397 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 398 | break; |
| 399 | default: |
| 400 | goto fail; |
| 401 | } |
| 402 | buffer_clear(&buffer); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 403 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 404 | buffer_free(&buffer); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 405 | *resp = reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 406 | return (PAM_SUCCESS); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 407 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 408 | fail: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 409 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 410 | free(reply[i].resp); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 411 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 412 | free(reply); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 413 | buffer_free(&buffer); |
| 414 | return (PAM_CONV_ERR); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 417 | /* |
| 418 | * Authentication thread. |
| 419 | */ |
| 420 | static void * |
| 421 | sshpam_thread(void *ctxtp) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 422 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 423 | struct pam_ctxt *ctxt = ctxtp; |
| 424 | Buffer buffer; |
Damien Miller | f4b6f10 | 2003-09-02 23:12:06 +1000 | [diff] [blame] | 425 | struct pam_conv sshpam_conv; |
Darren Tucker | 1f7e408 | 2004-07-01 14:00:14 +1000 | [diff] [blame] | 426 | int flags = (options.permit_empty_passwd == 0 ? |
| 427 | PAM_DISALLOW_NULL_AUTHTOK : 0); |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 428 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 429 | extern char **environ; |
| 430 | char **env_from_pam; |
| 431 | u_int i; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 432 | const char *pam_user; |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 433 | const char **ptr_pam_user = &pam_user; |
Darren Tucker | 54e1b22 | 2006-09-17 11:57:46 +1000 | [diff] [blame] | 434 | char *tz = getenv("TZ"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 435 | |
Darren Tucker | 53f8e78 | 2013-12-19 11:31:44 +1100 | [diff] [blame] | 436 | sshpam_err = pam_get_item(sshpam_handle, PAM_USER, |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 437 | (sshpam_const void **)ptr_pam_user); |
Darren Tucker | 53f8e78 | 2013-12-19 11:31:44 +1100 | [diff] [blame] | 438 | if (sshpam_err != PAM_SUCCESS) |
| 439 | goto auth_fail; |
Darren Tucker | 54e1b22 | 2006-09-17 11:57:46 +1000 | [diff] [blame] | 440 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 441 | environ[0] = NULL; |
Darren Tucker | 54e1b22 | 2006-09-17 11:57:46 +1000 | [diff] [blame] | 442 | if (tz != NULL) |
| 443 | if (setenv("TZ", tz, 1) == -1) |
| 444 | error("PAM: could not set TZ environment: %s", |
| 445 | strerror(errno)); |
Damien Miller | 2d2ed3d | 2004-07-21 20:54:47 +1000 | [diff] [blame] | 446 | |
| 447 | if (sshpam_authctxt != NULL) { |
| 448 | setproctitle("%s [pam]", |
| 449 | sshpam_authctxt->valid ? pam_user : "unknown"); |
| 450 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 451 | #endif |
| 452 | |
Damien Miller | f4b6f10 | 2003-09-02 23:12:06 +1000 | [diff] [blame] | 453 | sshpam_conv.conv = sshpam_thread_conv; |
| 454 | sshpam_conv.appdata_ptr = ctxt; |
| 455 | |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 456 | if (sshpam_authctxt == NULL) |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 457 | fatal("%s: PAM authctxt not initialized", __func__); |
| 458 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 459 | buffer_init(&buffer); |
| 460 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
| 461 | (const void *)&sshpam_conv); |
| 462 | if (sshpam_err != PAM_SUCCESS) |
| 463 | goto auth_fail; |
Darren Tucker | 1f7e408 | 2004-07-01 14:00:14 +1000 | [diff] [blame] | 464 | sshpam_err = pam_authenticate(sshpam_handle, flags); |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 465 | if (sshpam_err == PAM_MAXTRIES) |
| 466 | sshpam_set_maxtries_reached(1); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 467 | if (sshpam_err != PAM_SUCCESS) |
| 468 | goto auth_fail; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 469 | |
Darren Tucker | 608ec1f | 2017-03-29 09:50:54 +1100 | [diff] [blame] | 470 | if (!do_pam_account()) { |
| 471 | sshpam_err = PAM_ACCT_EXPIRED; |
| 472 | goto auth_fail; |
| 473 | } |
| 474 | if (sshpam_authctxt->force_pwchange) { |
| 475 | sshpam_err = pam_chauthtok(sshpam_handle, |
| 476 | PAM_CHANGE_EXPIRED_AUTHTOK); |
| 477 | if (sshpam_err != PAM_SUCCESS) |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 478 | goto auth_fail; |
Darren Tucker | 608ec1f | 2017-03-29 09:50:54 +1100 | [diff] [blame] | 479 | sshpam_password_change_required(0); |
Darren Tucker | c376c86 | 2003-12-18 16:08:59 +1100 | [diff] [blame] | 480 | } |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 481 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 482 | buffer_put_cstring(&buffer, "OK"); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 483 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 484 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 485 | /* Export variables set by do_pam_account */ |
| 486 | buffer_put_int(&buffer, sshpam_account_status); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 487 | buffer_put_int(&buffer, sshpam_authctxt->force_pwchange); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 488 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 489 | /* Export any environment strings set in child */ |
| 490 | for(i = 0; environ[i] != NULL; i++) |
| 491 | ; /* Count */ |
| 492 | buffer_put_int(&buffer, i); |
| 493 | for(i = 0; environ[i] != NULL; i++) |
| 494 | buffer_put_cstring(&buffer, environ[i]); |
| 495 | |
| 496 | /* Export any environment strings set by PAM in child */ |
| 497 | env_from_pam = pam_getenvlist(sshpam_handle); |
| 498 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) |
| 499 | ; /* Count */ |
| 500 | buffer_put_int(&buffer, i); |
| 501 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) |
| 502 | buffer_put_cstring(&buffer, env_from_pam[i]); |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 503 | #endif /* UNSUPPORTED_POSIX_THREADS_HACK */ |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 504 | |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 505 | /* XXX - can't do much about an error here */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 506 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); |
| 507 | buffer_free(&buffer); |
| 508 | pthread_exit(NULL); |
| 509 | |
| 510 | auth_fail: |
| 511 | buffer_put_cstring(&buffer, |
| 512 | pam_strerror(sshpam_handle, sshpam_err)); |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 513 | /* XXX - can't do much about an error here */ |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 514 | if (sshpam_err == PAM_ACCT_EXPIRED) |
| 515 | ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer); |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 516 | else if (sshpam_maxtries_reached) |
| 517 | ssh_msg_send(ctxt->pam_csock, PAM_MAXTRIES, &buffer); |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 518 | else |
| 519 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 520 | buffer_free(&buffer); |
| 521 | pthread_exit(NULL); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 522 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 523 | return (NULL); /* Avoid warning for non-pthread case */ |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 524 | } |
| 525 | |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 526 | void |
| 527 | sshpam_thread_cleanup(void) |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 528 | { |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 529 | struct pam_ctxt *ctxt = cleanup_ctxt; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 530 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 531 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 532 | if (ctxt != NULL && ctxt->pam_thread != 0) { |
| 533 | pthread_cancel(ctxt->pam_thread); |
| 534 | pthread_join(ctxt->pam_thread, NULL); |
| 535 | close(ctxt->pam_psock); |
| 536 | close(ctxt->pam_csock); |
| 537 | memset(ctxt, 0, sizeof(*ctxt)); |
| 538 | cleanup_ctxt = NULL; |
| 539 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 540 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 541 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 542 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 543 | sshpam_null_conv(int n, sshpam_const struct pam_message **msg, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 544 | struct pam_response **resp, void *data) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 545 | { |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 546 | debug3("PAM: %s entering, %d messages", __func__, n); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 547 | return (PAM_CONV_ERR); |
| 548 | } |
Damien Miller | 63dc3e9 | 2001-02-07 12:58:33 +1100 | [diff] [blame] | 549 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 550 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
| 551 | |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 552 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 553 | sshpam_store_conv(int n, sshpam_const struct pam_message **msg, |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 554 | struct pam_response **resp, void *data) |
| 555 | { |
| 556 | struct pam_response *reply; |
| 557 | int i; |
| 558 | size_t len; |
| 559 | |
| 560 | debug3("PAM: %s called with %d messages", __func__, n); |
| 561 | *resp = NULL; |
| 562 | |
| 563 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 564 | return (PAM_CONV_ERR); |
| 565 | |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 566 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 567 | return (PAM_CONV_ERR); |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 568 | |
| 569 | for (i = 0; i < n; ++i) { |
| 570 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 571 | case PAM_ERROR_MSG: |
| 572 | case PAM_TEXT_INFO: |
| 573 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); |
| 574 | buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len); |
| 575 | buffer_append(&loginmsg, "\n", 1 ); |
| 576 | reply[i].resp_retcode = PAM_SUCCESS; |
| 577 | break; |
| 578 | default: |
| 579 | goto fail; |
| 580 | } |
| 581 | } |
| 582 | *resp = reply; |
| 583 | return (PAM_SUCCESS); |
| 584 | |
| 585 | fail: |
| 586 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 587 | free(reply[i].resp); |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 588 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 589 | free(reply); |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 590 | return (PAM_CONV_ERR); |
| 591 | } |
| 592 | |
| 593 | static struct pam_conv store_conv = { sshpam_store_conv, NULL }; |
| 594 | |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 595 | void |
| 596 | sshpam_cleanup(void) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 597 | { |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 598 | if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor())) |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 599 | return; |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 600 | debug("PAM: cleanup"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 601 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 602 | if (sshpam_session_open) { |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 603 | debug("PAM: closing session"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 604 | pam_close_session(sshpam_handle, PAM_SILENT); |
| 605 | sshpam_session_open = 0; |
| 606 | } |
Darren Tucker | 622d5c5 | 2009-07-12 22:07:21 +1000 | [diff] [blame] | 607 | if (sshpam_cred_established) { |
| 608 | debug("PAM: deleting credentials"); |
| 609 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); |
| 610 | sshpam_cred_established = 0; |
| 611 | } |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 612 | sshpam_authenticated = 0; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 613 | pam_end(sshpam_handle, sshpam_err); |
| 614 | sshpam_handle = NULL; |
| 615 | } |
| 616 | |
| 617 | static int |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 618 | sshpam_init(Authctxt *authctxt) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 619 | { |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 620 | const char *pam_rhost, *pam_user, *user = authctxt->user; |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 621 | const char **ptr_pam_user = &pam_user; |
Damien Miller | 3ed9218 | 2016-03-08 14:01:29 -0800 | [diff] [blame] | 622 | struct ssh *ssh = active_state; /* XXX */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 623 | |
| 624 | if (sshpam_handle != NULL) { |
| 625 | /* We already have a PAM context; check if the user matches */ |
| 626 | sshpam_err = pam_get_item(sshpam_handle, |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 627 | PAM_USER, (sshpam_const void **)ptr_pam_user); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 628 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) |
| 629 | return (0); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 630 | pam_end(sshpam_handle, sshpam_err); |
| 631 | sshpam_handle = NULL; |
| 632 | } |
| 633 | debug("PAM: initializing for \"%s\"", user); |
Darren Tucker | c58c2ee | 2003-09-13 22:02:05 +1000 | [diff] [blame] | 634 | sshpam_err = |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 635 | pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 636 | sshpam_authctxt = authctxt; |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 637 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 638 | if (sshpam_err != PAM_SUCCESS) { |
| 639 | pam_end(sshpam_handle, sshpam_err); |
| 640 | sshpam_handle = NULL; |
| 641 | return (-1); |
| 642 | } |
Damien Miller | 3ed9218 | 2016-03-08 14:01:29 -0800 | [diff] [blame] | 643 | pam_rhost = auth_get_canonical_hostname(ssh, options.use_dns); |
Damien Miller | 4633720 | 2003-06-02 11:04:39 +1000 | [diff] [blame] | 644 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 645 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); |
| 646 | if (sshpam_err != PAM_SUCCESS) { |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 647 | pam_end(sshpam_handle, sshpam_err); |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 648 | sshpam_handle = NULL; |
| 649 | return (-1); |
| 650 | } |
| 651 | #ifdef PAM_TTY_KLUDGE |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 652 | /* |
| 653 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. |
| 654 | * 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] | 655 | * may not even set one (for tty-less connections) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 656 | */ |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 657 | debug("PAM: setting PAM_TTY to \"ssh\""); |
| 658 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); |
| 659 | if (sshpam_err != PAM_SUCCESS) { |
| 660 | pam_end(sshpam_handle, sshpam_err); |
| 661 | sshpam_handle = NULL; |
| 662 | return (-1); |
| 663 | } |
| 664 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 665 | return (0); |
| 666 | } |
| 667 | |
| 668 | static void * |
| 669 | sshpam_init_ctx(Authctxt *authctxt) |
| 670 | { |
| 671 | struct pam_ctxt *ctxt; |
| 672 | int socks[2]; |
| 673 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 674 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 675 | /* |
| 676 | * Refuse to start if we don't have PAM enabled or do_pam_account |
| 677 | * has previously failed. |
| 678 | */ |
| 679 | if (!options.use_pam || sshpam_account_status == 0) |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 680 | return NULL; |
| 681 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 682 | /* Initialize PAM */ |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 683 | if (sshpam_init(authctxt) == -1) { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 684 | error("PAM: initialization failed"); |
| 685 | return (NULL); |
| 686 | } |
| 687 | |
Darren Tucker | 29171e9 | 2007-05-20 15:20:08 +1000 | [diff] [blame] | 688 | ctxt = xcalloc(1, sizeof *ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 689 | |
| 690 | /* Start the authentication thread */ |
| 691 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { |
| 692 | error("PAM: failed create sockets: %s", strerror(errno)); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 693 | free(ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 694 | return (NULL); |
| 695 | } |
| 696 | ctxt->pam_psock = socks[0]; |
| 697 | ctxt->pam_csock = socks[1]; |
| 698 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { |
| 699 | error("PAM: failed to start authentication thread: %s", |
| 700 | strerror(errno)); |
| 701 | close(socks[0]); |
| 702 | close(socks[1]); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 703 | free(ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 704 | return (NULL); |
| 705 | } |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 706 | cleanup_ctxt = ctxt; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 707 | return (ctxt); |
| 708 | } |
| 709 | |
| 710 | static int |
| 711 | sshpam_query(void *ctx, char **name, char **info, |
| 712 | u_int *num, char ***prompts, u_int **echo_on) |
| 713 | { |
Damien Miller | 3ed9218 | 2016-03-08 14:01:29 -0800 | [diff] [blame] | 714 | struct ssh *ssh = active_state; /* XXX */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 715 | Buffer buffer; |
| 716 | struct pam_ctxt *ctxt = ctx; |
| 717 | size_t plen; |
| 718 | u_char type; |
| 719 | char *msg; |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 720 | size_t len, mlen; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 721 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 722 | debug3("PAM: %s entering", __func__); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 723 | buffer_init(&buffer); |
| 724 | *name = xstrdup(""); |
| 725 | *info = xstrdup(""); |
| 726 | *prompts = xmalloc(sizeof(char *)); |
| 727 | **prompts = NULL; |
| 728 | plen = 0; |
| 729 | *echo_on = xmalloc(sizeof(u_int)); |
| 730 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { |
| 731 | type = buffer_get_char(&buffer); |
| 732 | msg = buffer_get_string(&buffer, NULL); |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 733 | mlen = strlen(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 734 | switch (type) { |
| 735 | case PAM_PROMPT_ECHO_ON: |
| 736 | case PAM_PROMPT_ECHO_OFF: |
| 737 | *num = 1; |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 738 | len = plen + mlen + 1; |
Darren Tucker | d1680d3 | 2015-04-30 09:18:11 +1000 | [diff] [blame] | 739 | **prompts = xreallocarray(**prompts, 1, len); |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 740 | strlcpy(**prompts + plen, msg, len - plen); |
| 741 | plen += mlen; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 742 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 743 | free(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 744 | return (0); |
| 745 | case PAM_ERROR_MSG: |
| 746 | case PAM_TEXT_INFO: |
| 747 | /* accumulate messages */ |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 748 | len = plen + mlen + 2; |
Darren Tucker | d1680d3 | 2015-04-30 09:18:11 +1000 | [diff] [blame] | 749 | **prompts = xreallocarray(**prompts, 1, len); |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 750 | strlcpy(**prompts + plen, msg, len - plen); |
| 751 | plen += mlen; |
| 752 | strlcat(**prompts + plen, "\n", len - plen); |
| 753 | plen++; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 754 | free(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 755 | break; |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 756 | case PAM_ACCT_EXPIRED: |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 757 | case PAM_MAXTRIES: |
| 758 | if (type == PAM_ACCT_EXPIRED) |
| 759 | sshpam_account_status = 0; |
| 760 | if (type == PAM_MAXTRIES) |
| 761 | sshpam_set_maxtries_reached(1); |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 762 | /* FALLTHROUGH */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 763 | case PAM_AUTH_ERR: |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 764 | debug3("PAM: %s", pam_strerror(sshpam_handle, type)); |
Darren Tucker | 7b1e695 | 2005-09-28 22:33:27 +1000 | [diff] [blame] | 765 | if (**prompts != NULL && strlen(**prompts) != 0) { |
| 766 | *info = **prompts; |
| 767 | **prompts = NULL; |
| 768 | *num = 0; |
| 769 | **echo_on = 0; |
| 770 | ctxt->pam_done = -1; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 771 | free(msg); |
Darren Tucker | 7b1e695 | 2005-09-28 22:33:27 +1000 | [diff] [blame] | 772 | return 0; |
| 773 | } |
| 774 | /* FALLTHROUGH */ |
| 775 | case PAM_SUCCESS: |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 776 | if (**prompts != NULL) { |
| 777 | /* drain any accumulated messages */ |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 778 | debug("PAM: %s", **prompts); |
| 779 | buffer_append(&loginmsg, **prompts, |
| 780 | strlen(**prompts)); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 781 | free(**prompts); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 782 | **prompts = NULL; |
| 783 | } |
| 784 | if (type == PAM_SUCCESS) { |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 785 | if (!sshpam_authctxt->valid || |
| 786 | (sshpam_authctxt->pw->pw_uid == 0 && |
| 787 | options.permit_root_login != PERMIT_YES)) |
Darren Tucker | 36a3d60 | 2005-01-20 12:43:38 +1100 | [diff] [blame] | 788 | fatal("Internal error: PAM auth " |
| 789 | "succeeded when it should have " |
| 790 | "failed"); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 791 | import_environments(&buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 792 | *num = 0; |
| 793 | **echo_on = 0; |
| 794 | ctxt->pam_done = 1; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 795 | free(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 796 | return (0); |
| 797 | } |
Darren Tucker | 2a9bf4b | 2004-04-18 11:00:26 +1000 | [diff] [blame] | 798 | error("PAM: %s for %s%.100s from %.100s", msg, |
| 799 | sshpam_authctxt->valid ? "" : "illegal user ", |
| 800 | sshpam_authctxt->user, |
Damien Miller | 3ed9218 | 2016-03-08 14:01:29 -0800 | [diff] [blame] | 801 | auth_get_canonical_hostname(ssh, options.use_dns)); |
Darren Tucker | 439ce0d | 2003-10-09 14:20:15 +1000 | [diff] [blame] | 802 | /* FALLTHROUGH */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 803 | default: |
| 804 | *num = 0; |
| 805 | **echo_on = 0; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 806 | free(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 807 | ctxt->pam_done = -1; |
| 808 | return (-1); |
| 809 | } |
| 810 | } |
| 811 | return (-1); |
| 812 | } |
| 813 | |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 814 | /* |
| 815 | * Returns a junk password of identical length to that the user supplied. |
| 816 | * Used to mitigate timing attacks against crypt(3)/PAM stacks that |
| 817 | * vary processing time in proportion to password length. |
| 818 | */ |
| 819 | static char * |
| 820 | fake_password(const char *wire_password) |
| 821 | { |
| 822 | const char junk[] = "\b\n\r\177INCORRECT"; |
| 823 | char *ret = NULL; |
| 824 | size_t i, l = wire_password != NULL ? strlen(wire_password) : 0; |
| 825 | |
| 826 | if (l >= INT_MAX) |
| 827 | fatal("%s: password length too long: %zu", __func__, l); |
| 828 | |
| 829 | ret = malloc(l + 1); |
Darren Tucker | bee0167 | 2017-03-10 13:40:18 +1100 | [diff] [blame] | 830 | if (ret == NULL) |
| 831 | return NULL; |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 832 | for (i = 0; i < l; i++) |
| 833 | ret[i] = junk[i % (sizeof(junk) - 1)]; |
| 834 | ret[i] = '\0'; |
| 835 | return ret; |
| 836 | } |
| 837 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 838 | /* XXX - see also comment in auth-chall.c:verify_response */ |
| 839 | static int |
| 840 | sshpam_respond(void *ctx, u_int num, char **resp) |
| 841 | { |
| 842 | Buffer buffer; |
| 843 | struct pam_ctxt *ctxt = ctx; |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 844 | char *fake; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 845 | |
Darren Tucker | 1d4ebbf | 2006-01-29 16:46:13 +1100 | [diff] [blame] | 846 | debug2("PAM: %s entering, %u responses", __func__, num); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 847 | switch (ctxt->pam_done) { |
| 848 | case 1: |
| 849 | sshpam_authenticated = 1; |
| 850 | return (0); |
| 851 | case 0: |
| 852 | break; |
| 853 | default: |
| 854 | return (-1); |
| 855 | } |
| 856 | if (num != 1) { |
| 857 | error("PAM: expected one response, got %u", num); |
| 858 | return (-1); |
| 859 | } |
| 860 | buffer_init(&buffer); |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 861 | if (sshpam_authctxt->valid && |
| 862 | (sshpam_authctxt->pw->pw_uid != 0 || |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 863 | options.permit_root_login == PERMIT_YES)) |
Darren Tucker | 36a3d60 | 2005-01-20 12:43:38 +1100 | [diff] [blame] | 864 | buffer_put_cstring(&buffer, *resp); |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 865 | else { |
| 866 | fake = fake_password(*resp); |
| 867 | buffer_put_cstring(&buffer, fake); |
| 868 | free(fake); |
| 869 | } |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 870 | if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) { |
| 871 | buffer_free(&buffer); |
| 872 | return (-1); |
| 873 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 874 | buffer_free(&buffer); |
| 875 | return (1); |
| 876 | } |
| 877 | |
| 878 | static void |
| 879 | sshpam_free_ctx(void *ctxtp) |
| 880 | { |
| 881 | struct pam_ctxt *ctxt = ctxtp; |
| 882 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 883 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 884 | sshpam_thread_cleanup(); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 885 | free(ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 886 | /* |
| 887 | * We don't call sshpam_cleanup() here because we may need the PAM |
| 888 | * handle at a later stage, e.g. when setting up a session. It's |
| 889 | * still on the cleanup list, so pam_end() *will* be called before |
| 890 | * the server process terminates. |
| 891 | */ |
| 892 | } |
| 893 | |
| 894 | KbdintDevice sshpam_device = { |
| 895 | "pam", |
| 896 | sshpam_init_ctx, |
| 897 | sshpam_query, |
| 898 | sshpam_respond, |
| 899 | sshpam_free_ctx |
| 900 | }; |
| 901 | |
| 902 | KbdintDevice mm_sshpam_device = { |
| 903 | "pam", |
| 904 | mm_sshpam_init_ctx, |
| 905 | mm_sshpam_query, |
| 906 | mm_sshpam_respond, |
| 907 | mm_sshpam_free_ctx |
| 908 | }; |
| 909 | |
| 910 | /* |
| 911 | * This replaces auth-pam.c |
| 912 | */ |
| 913 | void |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 914 | start_pam(Authctxt *authctxt) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 915 | { |
Damien Miller | 9d507da | 2003-05-14 15:31:12 +1000 | [diff] [blame] | 916 | if (!options.use_pam) |
| 917 | fatal("PAM: initialisation requested when UsePAM=no"); |
| 918 | |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 919 | if (sshpam_init(authctxt) == -1) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 920 | fatal("PAM: initialisation failed"); |
| 921 | } |
| 922 | |
| 923 | void |
| 924 | finish_pam(void) |
| 925 | { |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 926 | sshpam_cleanup(); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 927 | } |
| 928 | |
Damien Miller | 94bc1e7 | 2017-07-28 14:50:59 +1000 | [diff] [blame] | 929 | static void |
| 930 | expose_authinfo(const char *caller) |
| 931 | { |
| 932 | char *auth_info; |
| 933 | |
| 934 | /* |
| 935 | * Expose authentication information to PAM. |
| 936 | * The enviornment variable is versioned. Please increment the |
| 937 | * version suffix if the format of session_info changes. |
| 938 | */ |
| 939 | if (sshpam_authctxt->session_info == NULL) |
| 940 | auth_info = xstrdup(""); |
| 941 | else if ((auth_info = sshbuf_dup_string( |
| 942 | sshpam_authctxt->session_info)) == NULL) |
| 943 | fatal("%s: sshbuf_dup_string failed", __func__); |
| 944 | |
| 945 | debug2("%s: auth information in SSH_AUTH_INFO_0", caller); |
| 946 | do_pam_putenv("SSH_AUTH_INFO_0", auth_info); |
| 947 | free(auth_info); |
| 948 | } |
| 949 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 950 | u_int |
| 951 | do_pam_account(void) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 952 | { |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 953 | debug("%s: called", __func__); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 954 | if (sshpam_account_status != -1) |
| 955 | return (sshpam_account_status); |
| 956 | |
Damien Miller | 94bc1e7 | 2017-07-28 14:50:59 +1000 | [diff] [blame] | 957 | expose_authinfo(__func__); |
| 958 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 959 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 960 | debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err, |
| 961 | pam_strerror(sshpam_handle, sshpam_err)); |
Damien Miller | 94cf4c8 | 2005-07-17 17:04:47 +1000 | [diff] [blame] | 962 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 963 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { |
| 964 | sshpam_account_status = 0; |
| 965 | return (sshpam_account_status); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 966 | } |
| 967 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 968 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) |
Darren Tucker | 17db1c4 | 2004-06-19 12:54:38 +1000 | [diff] [blame] | 969 | sshpam_password_change_required(1); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 970 | |
| 971 | sshpam_account_status = 1; |
| 972 | return (sshpam_account_status); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | void |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 976 | do_pam_setcred(int init) |
| 977 | { |
| 978 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 979 | (const void *)&store_conv); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 980 | if (sshpam_err != PAM_SUCCESS) |
| 981 | fatal("PAM: failed to set PAM_CONV: %s", |
| 982 | pam_strerror(sshpam_handle, sshpam_err)); |
| 983 | if (init) { |
| 984 | debug("PAM: establishing credentials"); |
| 985 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); |
| 986 | } else { |
| 987 | debug("PAM: reinitializing credentials"); |
| 988 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); |
| 989 | } |
| 990 | if (sshpam_err == PAM_SUCCESS) { |
| 991 | sshpam_cred_established = 1; |
| 992 | return; |
| 993 | } |
| 994 | if (sshpam_authenticated) |
| 995 | fatal("PAM: pam_setcred(): %s", |
| 996 | pam_strerror(sshpam_handle, sshpam_err)); |
| 997 | else |
| 998 | debug("PAM: pam_setcred(): %s", |
| 999 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1000 | } |
| 1001 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1002 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 1003 | sshpam_tty_conv(int n, sshpam_const struct pam_message **msg, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1004 | struct pam_response **resp, void *data) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1005 | { |
| 1006 | char input[PAM_MAX_MSG_SIZE]; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1007 | struct pam_response *reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1008 | int i; |
| 1009 | |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 1010 | debug3("PAM: %s called with %d messages", __func__, n); |
| 1011 | |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1012 | *resp = NULL; |
| 1013 | |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1014 | if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1015 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1016 | |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 1017 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1018 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1019 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1020 | for (i = 0; i < n; ++i) { |
| 1021 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 1022 | case PAM_PROMPT_ECHO_OFF: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1023 | reply[i].resp = |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1024 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1025 | RP_ALLOW_STDIN); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1026 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1027 | break; |
| 1028 | case PAM_PROMPT_ECHO_ON: |
Darren Tucker | 0947ddf | 2003-11-13 11:21:31 +1100 | [diff] [blame] | 1029 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
Darren Tucker | 2216471 | 2007-05-20 15:26:07 +1000 | [diff] [blame] | 1030 | if (fgets(input, sizeof input, stdin) == NULL) |
| 1031 | input[0] = '\0'; |
Damien Miller | a6fb77f | 2004-07-19 09:39:11 +1000 | [diff] [blame] | 1032 | if ((reply[i].resp = strdup(input)) == NULL) |
| 1033 | goto fail; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1034 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1035 | break; |
| 1036 | case PAM_ERROR_MSG: |
| 1037 | case PAM_TEXT_INFO: |
Darren Tucker | 0947ddf | 2003-11-13 11:21:31 +1100 | [diff] [blame] | 1038 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1039 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1040 | break; |
| 1041 | default: |
| 1042 | goto fail; |
| 1043 | } |
| 1044 | } |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1045 | *resp = reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1046 | return (PAM_SUCCESS); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1047 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1048 | fail: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1049 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1050 | free(reply[i].resp); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1051 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1052 | free(reply); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1053 | return (PAM_CONV_ERR); |
| 1054 | } |
| 1055 | |
Darren Tucker | 94befab | 2004-06-03 14:53:12 +1000 | [diff] [blame] | 1056 | static struct pam_conv tty_conv = { sshpam_tty_conv, NULL }; |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1057 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1058 | /* |
| 1059 | * XXX this should be done in the authentication phase, but ssh1 doesn't |
| 1060 | * support that |
| 1061 | */ |
| 1062 | void |
| 1063 | do_pam_chauthtok(void) |
| 1064 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1065 | if (use_privsep) |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1066 | fatal("Password expired (unable to change with privsep)"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1067 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1068 | (const void *)&tty_conv); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1069 | if (sshpam_err != PAM_SUCCESS) |
| 1070 | fatal("PAM: failed to set PAM_CONV: %s", |
| 1071 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1072 | debug("PAM: changing password"); |
| 1073 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); |
| 1074 | if (sshpam_err != PAM_SUCCESS) |
| 1075 | fatal("PAM: pam_chauthtok(): %s", |
| 1076 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1077 | } |
| 1078 | |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1079 | void |
| 1080 | do_pam_session(void) |
| 1081 | { |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 1082 | debug3("PAM: opening session"); |
Damien Miller | 94bc1e7 | 2017-07-28 14:50:59 +1000 | [diff] [blame] | 1083 | |
| 1084 | expose_authinfo(__func__); |
| 1085 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1086 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 5cf8ef7 | 2004-02-17 23:20:07 +1100 | [diff] [blame] | 1087 | (const void *)&store_conv); |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1088 | if (sshpam_err != PAM_SUCCESS) |
| 1089 | fatal("PAM: failed to set PAM_CONV: %s", |
| 1090 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1091 | sshpam_err = pam_open_session(sshpam_handle, 0); |
Darren Tucker | 69687f4 | 2004-09-11 22:17:26 +1000 | [diff] [blame] | 1092 | if (sshpam_err == PAM_SUCCESS) |
| 1093 | sshpam_session_open = 1; |
| 1094 | else { |
| 1095 | sshpam_session_open = 0; |
| 1096 | disable_forwarding(); |
| 1097 | error("PAM: pam_open_session(): %s", |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1098 | pam_strerror(sshpam_handle, sshpam_err)); |
Darren Tucker | 69687f4 | 2004-09-11 22:17:26 +1000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | } |
| 1102 | |
| 1103 | int |
| 1104 | is_pam_session_open(void) |
| 1105 | { |
| 1106 | return sshpam_session_open; |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1107 | } |
| 1108 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1109 | /* |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1110 | * Set a PAM environment string. We need to do this so that the session |
| 1111 | * modules can handle things like Kerberos/GSI credentials that appear |
| 1112 | * during the ssh authentication process. |
| 1113 | */ |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1114 | int |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1115 | do_pam_putenv(char *name, char *value) |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1116 | { |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1117 | int ret = 1; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1118 | #ifdef HAVE_PAM_PUTENV |
Damien Miller | f272809 | 2003-09-17 07:24:25 +1000 | [diff] [blame] | 1119 | char *compound; |
| 1120 | size_t len; |
| 1121 | |
| 1122 | len = strlen(name) + strlen(value) + 2; |
| 1123 | compound = xmalloc(len); |
| 1124 | |
| 1125 | snprintf(compound, len, "%s=%s", name, value); |
| 1126 | ret = pam_putenv(sshpam_handle, compound); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1127 | free(compound); |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1128 | #endif |
Damien Miller | f272809 | 2003-09-17 07:24:25 +1000 | [diff] [blame] | 1129 | |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1130 | return (ret); |
| 1131 | } |
| 1132 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1133 | char ** |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 1134 | fetch_pam_child_environment(void) |
| 1135 | { |
| 1136 | return sshpam_env; |
| 1137 | } |
| 1138 | |
| 1139 | char ** |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1140 | fetch_pam_environment(void) |
| 1141 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1142 | return (pam_getenvlist(sshpam_handle)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | void |
| 1146 | free_pam_environment(char **env) |
| 1147 | { |
| 1148 | char **envp; |
| 1149 | |
Damien Miller | e27c6cc | 2003-05-16 18:21:01 +1000 | [diff] [blame] | 1150 | if (env == NULL) |
| 1151 | return; |
| 1152 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1153 | for (envp = env; *envp; envp++) |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1154 | free(*envp); |
| 1155 | free(env); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1156 | } |
| 1157 | |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1158 | /* |
| 1159 | * "Blind" conversation function for password authentication. Assumes that |
| 1160 | * echo-off prompts are for the password and stores messages for later |
| 1161 | * display. |
| 1162 | */ |
| 1163 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 1164 | sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg, |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1165 | struct pam_response **resp, void *data) |
| 1166 | { |
| 1167 | struct pam_response *reply; |
| 1168 | int i; |
| 1169 | size_t len; |
| 1170 | |
| 1171 | debug3("PAM: %s called with %d messages", __func__, n); |
| 1172 | |
| 1173 | *resp = NULL; |
| 1174 | |
| 1175 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 1176 | return (PAM_CONV_ERR); |
| 1177 | |
Darren Tucker | 29171e9 | 2007-05-20 15:20:08 +1000 | [diff] [blame] | 1178 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1179 | return (PAM_CONV_ERR); |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1180 | |
| 1181 | for (i = 0; i < n; ++i) { |
| 1182 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 1183 | case PAM_PROMPT_ECHO_OFF: |
| 1184 | if (sshpam_password == NULL) |
| 1185 | goto fail; |
Damien Miller | a6fb77f | 2004-07-19 09:39:11 +1000 | [diff] [blame] | 1186 | if ((reply[i].resp = strdup(sshpam_password)) == NULL) |
| 1187 | goto fail; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1188 | reply[i].resp_retcode = PAM_SUCCESS; |
| 1189 | break; |
| 1190 | case PAM_ERROR_MSG: |
| 1191 | case PAM_TEXT_INFO: |
| 1192 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); |
| 1193 | if (len > 0) { |
| 1194 | buffer_append(&loginmsg, |
| 1195 | PAM_MSG_MEMBER(msg, i, msg), len); |
| 1196 | buffer_append(&loginmsg, "\n", 1); |
| 1197 | } |
Damien Miller | a6fb77f | 2004-07-19 09:39:11 +1000 | [diff] [blame] | 1198 | if ((reply[i].resp = strdup("")) == NULL) |
| 1199 | goto fail; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1200 | reply[i].resp_retcode = PAM_SUCCESS; |
| 1201 | break; |
| 1202 | default: |
| 1203 | goto fail; |
| 1204 | } |
| 1205 | } |
| 1206 | *resp = reply; |
| 1207 | return (PAM_SUCCESS); |
| 1208 | |
Damien Miller | 94cf4c8 | 2005-07-17 17:04:47 +1000 | [diff] [blame] | 1209 | fail: |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1210 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1211 | free(reply[i].resp); |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1212 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1213 | free(reply); |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1214 | return (PAM_CONV_ERR); |
| 1215 | } |
| 1216 | |
| 1217 | static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL }; |
| 1218 | |
| 1219 | /* |
| 1220 | * Attempt password authentication via PAM |
| 1221 | */ |
| 1222 | int |
| 1223 | sshpam_auth_passwd(Authctxt *authctxt, const char *password) |
| 1224 | { |
| 1225 | int flags = (options.permit_empty_passwd == 0 ? |
| 1226 | PAM_DISALLOW_NULL_AUTHTOK : 0); |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 1227 | char *fake = NULL; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1228 | |
| 1229 | if (!options.use_pam || sshpam_handle == NULL) |
| 1230 | fatal("PAM: %s called when PAM disabled or failed to " |
| 1231 | "initialise.", __func__); |
| 1232 | |
| 1233 | sshpam_password = password; |
| 1234 | sshpam_authctxt = authctxt; |
| 1235 | |
Darren Tucker | e061b15 | 2004-05-30 22:04:56 +1000 | [diff] [blame] | 1236 | /* |
| 1237 | * If the user logging in is invalid, or is root but is not permitted |
| 1238 | * by PermitRootLogin, use an invalid password to prevent leaking |
| 1239 | * information via timing (eg if the PAM config has a delay on fail). |
| 1240 | */ |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 1241 | if (!authctxt->valid || (authctxt->pw->pw_uid == 0 && |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 1242 | options.permit_root_login != PERMIT_YES)) |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 1243 | sshpam_password = fake = fake_password(password); |
Darren Tucker | e061b15 | 2004-05-30 22:04:56 +1000 | [diff] [blame] | 1244 | |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1245 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
| 1246 | (const void *)&passwd_conv); |
| 1247 | if (sshpam_err != PAM_SUCCESS) |
| 1248 | fatal("PAM: %s: failed to set PAM_CONV: %s", __func__, |
| 1249 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1250 | |
| 1251 | sshpam_err = pam_authenticate(sshpam_handle, flags); |
| 1252 | sshpam_password = NULL; |
Darren Tucker | 283b97f | 2016-07-15 13:49:44 +1000 | [diff] [blame] | 1253 | free(fake); |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 1254 | if (sshpam_err == PAM_MAXTRIES) |
| 1255 | sshpam_set_maxtries_reached(1); |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 1256 | if (sshpam_err == PAM_SUCCESS && authctxt->valid) { |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1257 | debug("PAM: password authentication accepted for %.100s", |
| 1258 | authctxt->user); |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 1259 | return 1; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1260 | } else { |
| 1261 | debug("PAM: password authentication failed for %.100s: %s", |
| 1262 | authctxt->valid ? authctxt->user : "an illegal user", |
| 1263 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1264 | return 0; |
| 1265 | } |
| 1266 | } |
Darren Tucker | 01558b7 | 2016-07-18 09:33:25 +1000 | [diff] [blame] | 1267 | |
| 1268 | int |
| 1269 | sshpam_get_maxtries_reached(void) |
| 1270 | { |
| 1271 | return sshpam_maxtries_reached; |
| 1272 | } |
| 1273 | |
| 1274 | void |
| 1275 | sshpam_set_maxtries_reached(int reached) |
| 1276 | { |
| 1277 | if (reached == 0 || sshpam_maxtries_reached) |
| 1278 | return; |
| 1279 | sshpam_maxtries_reached = 1; |
| 1280 | options.password_authentication = 0; |
| 1281 | options.kbd_interactive_authentication = 0; |
| 1282 | options.challenge_response_authentication = 0; |
| 1283 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1284 | #endif /* USE_PAM */ |