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