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