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