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