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