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 | 3a961dc | 2003-06-03 10:25:48 +1000 | [diff] [blame] | 34 | RCSID("$Id: auth-pam.c,v 1.64 2003/06/03 00:25:48 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 | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 52 | |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 53 | extern ServerOptions options; |
| 54 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 55 | #define __unused |
Kevin Steves | 85ecbe7 | 2001-04-20 17:43:47 +0000 | [diff] [blame] | 56 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 57 | #ifdef USE_POSIX_THREADS |
| 58 | #include <pthread.h> |
| 59 | /* |
| 60 | * Avoid namespace clash when *not* using pthreads for systems *with* |
| 61 | * pthreads, which unconditionally define pthread_t via sys/types.h |
| 62 | * (e.g. Linux) |
| 63 | */ |
| 64 | typedef pthread_t sp_pthread_t; |
| 65 | #else |
| 66 | /* |
| 67 | * Simulate threads with processes. |
| 68 | */ |
| 69 | typedef pid_t sp_pthread_t; |
Kevin Steves | 63007d4 | 2002-07-21 17:57:01 +0000 | [diff] [blame] | 70 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 71 | static void |
| 72 | pthread_exit(void *value __unused) |
| 73 | { |
| 74 | _exit(0); |
| 75 | } |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 76 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 77 | static int |
| 78 | pthread_create(sp_pthread_t *thread, const void *attr __unused, |
| 79 | void *(*thread_start)(void *), void *arg) |
| 80 | { |
| 81 | pid_t pid; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 82 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 83 | switch ((pid = fork())) { |
| 84 | case -1: |
| 85 | error("fork(): %s", strerror(errno)); |
| 86 | return (-1); |
| 87 | case 0: |
| 88 | thread_start(arg); |
| 89 | _exit(1); |
| 90 | default: |
| 91 | *thread = pid; |
| 92 | return (0); |
| 93 | } |
| 94 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 95 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 96 | static int |
| 97 | pthread_cancel(sp_pthread_t thread) |
| 98 | { |
| 99 | return (kill(thread, SIGTERM)); |
| 100 | } |
| 101 | |
| 102 | static int |
| 103 | pthread_join(sp_pthread_t thread, void **value __unused) |
| 104 | { |
| 105 | int status; |
| 106 | |
| 107 | waitpid(thread, &status, 0); |
| 108 | return (status); |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | |
| 113 | static pam_handle_t *sshpam_handle; |
| 114 | static int sshpam_err; |
| 115 | static int sshpam_authenticated; |
| 116 | static int sshpam_new_authtok_reqd; |
| 117 | static int sshpam_session_open; |
| 118 | static int sshpam_cred_established; |
| 119 | |
| 120 | struct pam_ctxt { |
| 121 | sp_pthread_t pam_thread; |
| 122 | int pam_psock; |
| 123 | int pam_csock; |
| 124 | int pam_done; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 125 | }; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 126 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 127 | static void sshpam_free_ctx(void *); |
Damien Miller | 9d5705a | 2000-09-16 16:09:27 +1100 | [diff] [blame] | 128 | |
| 129 | /* |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 130 | * Conversation function for authentication thread. |
Damien Miller | 9d5705a | 2000-09-16 16:09:27 +1100 | [diff] [blame] | 131 | */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 132 | static int |
| 133 | sshpam_thread_conv(int n, |
| 134 | const struct pam_message **msg, |
| 135 | struct pam_response **resp, |
| 136 | void *data) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 137 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 138 | Buffer buffer; |
| 139 | struct pam_ctxt *ctxt; |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 140 | int i; |
| 141 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 142 | ctxt = data; |
| 143 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 144 | return (PAM_CONV_ERR); |
| 145 | *resp = xmalloc(n * sizeof **resp); |
| 146 | buffer_init(&buffer); |
| 147 | for (i = 0; i < n; ++i) { |
| 148 | resp[i]->resp_retcode = 0; |
| 149 | resp[i]->resp = NULL; |
| 150 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 151 | case PAM_PROMPT_ECHO_OFF: |
| 152 | buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg)); |
| 153 | ssh_msg_send(ctxt->pam_csock, |
| 154 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); |
| 155 | ssh_msg_recv(ctxt->pam_csock, &buffer); |
| 156 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
| 157 | goto fail; |
| 158 | resp[i]->resp = buffer_get_string(&buffer, NULL); |
| 159 | break; |
| 160 | case PAM_PROMPT_ECHO_ON: |
| 161 | buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg)); |
| 162 | ssh_msg_send(ctxt->pam_csock, |
| 163 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); |
| 164 | ssh_msg_recv(ctxt->pam_csock, &buffer); |
| 165 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
| 166 | goto fail; |
| 167 | resp[i]->resp = buffer_get_string(&buffer, NULL); |
| 168 | break; |
| 169 | case PAM_ERROR_MSG: |
| 170 | buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg)); |
| 171 | ssh_msg_send(ctxt->pam_csock, |
| 172 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); |
| 173 | break; |
| 174 | case PAM_TEXT_INFO: |
| 175 | buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg)); |
| 176 | ssh_msg_send(ctxt->pam_csock, |
| 177 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); |
| 178 | break; |
| 179 | default: |
| 180 | goto fail; |
| 181 | } |
| 182 | buffer_clear(&buffer); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 183 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 184 | buffer_free(&buffer); |
| 185 | return (PAM_SUCCESS); |
| 186 | fail: |
| 187 | while (i) |
| 188 | xfree(resp[--i]); |
| 189 | xfree(*resp); |
| 190 | *resp = NULL; |
| 191 | buffer_free(&buffer); |
| 192 | return (PAM_CONV_ERR); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 195 | /* |
| 196 | * Authentication thread. |
| 197 | */ |
| 198 | static void * |
| 199 | sshpam_thread(void *ctxtp) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 200 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 201 | struct pam_ctxt *ctxt = ctxtp; |
| 202 | Buffer buffer; |
| 203 | struct pam_conv sshpam_conv = { sshpam_thread_conv, ctxt }; |
| 204 | #ifndef USE_POSIX_THREADS |
| 205 | const char *pam_user; |
| 206 | |
| 207 | pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user); |
| 208 | setproctitle("%s [pam]", pam_user); |
| 209 | #endif |
| 210 | |
| 211 | buffer_init(&buffer); |
| 212 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
| 213 | (const void *)&sshpam_conv); |
| 214 | if (sshpam_err != PAM_SUCCESS) |
| 215 | goto auth_fail; |
| 216 | sshpam_err = pam_authenticate(sshpam_handle, 0); |
| 217 | if (sshpam_err != PAM_SUCCESS) |
| 218 | goto auth_fail; |
| 219 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
| 220 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) |
| 221 | goto auth_fail; |
| 222 | buffer_put_cstring(&buffer, "OK"); |
| 223 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); |
| 224 | buffer_free(&buffer); |
| 225 | pthread_exit(NULL); |
| 226 | |
| 227 | auth_fail: |
| 228 | buffer_put_cstring(&buffer, |
| 229 | pam_strerror(sshpam_handle, sshpam_err)); |
| 230 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); |
| 231 | buffer_free(&buffer); |
| 232 | pthread_exit(NULL); |
| 233 | |
| 234 | return (NULL); /* Avoid warning for non-pthread case */ |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 235 | } |
| 236 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 237 | static void |
| 238 | sshpam_thread_cleanup(void *ctxtp) |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 239 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 240 | struct pam_ctxt *ctxt = ctxtp; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 241 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 242 | pthread_cancel(ctxt->pam_thread); |
| 243 | pthread_join(ctxt->pam_thread, NULL); |
| 244 | close(ctxt->pam_psock); |
| 245 | close(ctxt->pam_csock); |
| 246 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 247 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 248 | static int |
| 249 | sshpam_null_conv(int n, |
| 250 | const struct pam_message **msg, |
| 251 | struct pam_response **resp, |
| 252 | void *data) |
| 253 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 254 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 255 | return (PAM_CONV_ERR); |
| 256 | } |
Damien Miller | 63dc3e9 | 2001-02-07 12:58:33 +1100 | [diff] [blame] | 257 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 258 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
| 259 | |
| 260 | static void |
| 261 | sshpam_cleanup(void *arg) |
| 262 | { |
| 263 | (void)arg; |
| 264 | debug("PAM: cleanup"); |
| 265 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); |
| 266 | if (sshpam_cred_established) { |
| 267 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); |
| 268 | sshpam_cred_established = 0; |
| 269 | } |
| 270 | if (sshpam_session_open) { |
| 271 | pam_close_session(sshpam_handle, PAM_SILENT); |
| 272 | sshpam_session_open = 0; |
| 273 | } |
| 274 | sshpam_authenticated = sshpam_new_authtok_reqd = 0; |
| 275 | pam_end(sshpam_handle, sshpam_err); |
| 276 | sshpam_handle = NULL; |
| 277 | } |
| 278 | |
| 279 | static int |
| 280 | sshpam_init(const char *user) |
| 281 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 282 | extern u_int utmp_len; |
| 283 | const char *pam_rhost, *pam_user; |
| 284 | |
| 285 | if (sshpam_handle != NULL) { |
| 286 | /* We already have a PAM context; check if the user matches */ |
| 287 | sshpam_err = pam_get_item(sshpam_handle, |
| 288 | PAM_USER, (const void **)&pam_user); |
| 289 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) |
| 290 | return (0); |
| 291 | fatal_remove_cleanup(sshpam_cleanup, NULL); |
| 292 | pam_end(sshpam_handle, sshpam_err); |
| 293 | sshpam_handle = NULL; |
| 294 | } |
| 295 | debug("PAM: initializing for \"%s\"", user); |
| 296 | sshpam_err = pam_start("sshd", user, &null_conv, &sshpam_handle); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 297 | if (sshpam_err != PAM_SUCCESS) { |
| 298 | pam_end(sshpam_handle, sshpam_err); |
| 299 | sshpam_handle = NULL; |
| 300 | return (-1); |
| 301 | } |
Damien Miller | 3a961dc | 2003-06-03 10:25:48 +1000 | [diff] [blame] | 302 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); |
Damien Miller | 4633720 | 2003-06-02 11:04:39 +1000 | [diff] [blame] | 303 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 304 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); |
| 305 | if (sshpam_err != PAM_SUCCESS) { |
| 306 | pam_end(sshpam_handle, sshpam_err); |
| 307 | sshpam_handle = NULL; |
| 308 | return (-1); |
| 309 | } |
| 310 | #ifdef PAM_TTY_KLUDGE |
| 311 | /* |
| 312 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. |
| 313 | * sshd doesn't set the tty until too late in the auth process and |
| 314 | * may not even set one (for tty-less connections) |
| 315 | */ |
| 316 | debug("PAM: setting PAM_TTY to \"ssh\""); |
| 317 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); |
| 318 | if (sshpam_err != PAM_SUCCESS) { |
| 319 | pam_end(sshpam_handle, sshpam_err); |
| 320 | sshpam_handle = NULL; |
| 321 | return (-1); |
| 322 | } |
| 323 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 324 | fatal_add_cleanup(sshpam_cleanup, NULL); |
| 325 | return (0); |
| 326 | } |
| 327 | |
| 328 | static void * |
| 329 | sshpam_init_ctx(Authctxt *authctxt) |
| 330 | { |
| 331 | struct pam_ctxt *ctxt; |
| 332 | int socks[2]; |
| 333 | |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 334 | /* Refuse to start if we don't have PAM enabled */ |
| 335 | if (!options.use_pam) |
| 336 | return NULL; |
| 337 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 338 | /* Initialize PAM */ |
| 339 | if (sshpam_init(authctxt->user) == -1) { |
| 340 | error("PAM: initialization failed"); |
| 341 | return (NULL); |
| 342 | } |
| 343 | |
| 344 | ctxt = xmalloc(sizeof *ctxt); |
| 345 | ctxt->pam_done = 0; |
| 346 | |
| 347 | /* Start the authentication thread */ |
| 348 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { |
| 349 | error("PAM: failed create sockets: %s", strerror(errno)); |
| 350 | xfree(ctxt); |
| 351 | return (NULL); |
| 352 | } |
| 353 | ctxt->pam_psock = socks[0]; |
| 354 | ctxt->pam_csock = socks[1]; |
| 355 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { |
| 356 | error("PAM: failed to start authentication thread: %s", |
| 357 | strerror(errno)); |
| 358 | close(socks[0]); |
| 359 | close(socks[1]); |
| 360 | xfree(ctxt); |
| 361 | return (NULL); |
| 362 | } |
| 363 | fatal_add_cleanup(sshpam_thread_cleanup, ctxt); |
| 364 | return (ctxt); |
| 365 | } |
| 366 | |
| 367 | static int |
| 368 | sshpam_query(void *ctx, char **name, char **info, |
| 369 | u_int *num, char ***prompts, u_int **echo_on) |
| 370 | { |
| 371 | Buffer buffer; |
| 372 | struct pam_ctxt *ctxt = ctx; |
| 373 | size_t plen; |
| 374 | u_char type; |
| 375 | char *msg; |
| 376 | |
| 377 | buffer_init(&buffer); |
| 378 | *name = xstrdup(""); |
| 379 | *info = xstrdup(""); |
| 380 | *prompts = xmalloc(sizeof(char *)); |
| 381 | **prompts = NULL; |
| 382 | plen = 0; |
| 383 | *echo_on = xmalloc(sizeof(u_int)); |
| 384 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { |
| 385 | type = buffer_get_char(&buffer); |
| 386 | msg = buffer_get_string(&buffer, NULL); |
| 387 | switch (type) { |
| 388 | case PAM_PROMPT_ECHO_ON: |
| 389 | case PAM_PROMPT_ECHO_OFF: |
| 390 | *num = 1; |
| 391 | **prompts = xrealloc(**prompts, plen + strlen(msg) + 1); |
| 392 | plen += sprintf(**prompts + plen, "%s", msg); |
| 393 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
| 394 | xfree(msg); |
| 395 | return (0); |
| 396 | case PAM_ERROR_MSG: |
| 397 | case PAM_TEXT_INFO: |
| 398 | /* accumulate messages */ |
| 399 | **prompts = xrealloc(**prompts, plen + strlen(msg) + 1); |
| 400 | plen += sprintf(**prompts + plen, "%s", msg); |
| 401 | xfree(msg); |
| 402 | break; |
| 403 | case PAM_NEW_AUTHTOK_REQD: |
| 404 | sshpam_new_authtok_reqd = 1; |
| 405 | /* FALLTHROUGH */ |
| 406 | case PAM_SUCCESS: |
| 407 | case PAM_AUTH_ERR: |
| 408 | if (**prompts != NULL) { |
| 409 | /* drain any accumulated messages */ |
| 410 | #if 0 /* XXX - not compatible with privsep */ |
| 411 | packet_start(SSH2_MSG_USERAUTH_BANNER); |
| 412 | packet_put_cstring(**prompts); |
| 413 | packet_put_cstring(""); |
| 414 | packet_send(); |
| 415 | packet_write_wait(); |
| 416 | #endif |
| 417 | xfree(**prompts); |
| 418 | **prompts = NULL; |
| 419 | } |
| 420 | if (type == PAM_SUCCESS) { |
| 421 | *num = 0; |
| 422 | **echo_on = 0; |
| 423 | ctxt->pam_done = 1; |
| 424 | xfree(msg); |
| 425 | return (0); |
| 426 | } |
| 427 | error("PAM: %s", msg); |
| 428 | default: |
| 429 | *num = 0; |
| 430 | **echo_on = 0; |
| 431 | xfree(msg); |
| 432 | ctxt->pam_done = -1; |
| 433 | return (-1); |
| 434 | } |
| 435 | } |
| 436 | return (-1); |
| 437 | } |
| 438 | |
| 439 | /* XXX - see also comment in auth-chall.c:verify_response */ |
| 440 | static int |
| 441 | sshpam_respond(void *ctx, u_int num, char **resp) |
| 442 | { |
| 443 | Buffer buffer; |
| 444 | struct pam_ctxt *ctxt = ctx; |
| 445 | |
| 446 | debug2("PAM: %s", __func__); |
| 447 | switch (ctxt->pam_done) { |
| 448 | case 1: |
| 449 | sshpam_authenticated = 1; |
| 450 | return (0); |
| 451 | case 0: |
| 452 | break; |
| 453 | default: |
| 454 | return (-1); |
| 455 | } |
| 456 | if (num != 1) { |
| 457 | error("PAM: expected one response, got %u", num); |
| 458 | return (-1); |
| 459 | } |
| 460 | buffer_init(&buffer); |
| 461 | buffer_put_cstring(&buffer, *resp); |
| 462 | ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer); |
| 463 | buffer_free(&buffer); |
| 464 | return (1); |
| 465 | } |
| 466 | |
| 467 | static void |
| 468 | sshpam_free_ctx(void *ctxtp) |
| 469 | { |
| 470 | struct pam_ctxt *ctxt = ctxtp; |
| 471 | |
| 472 | fatal_remove_cleanup(sshpam_thread_cleanup, ctxt); |
| 473 | sshpam_thread_cleanup(ctxtp); |
| 474 | xfree(ctxt); |
| 475 | /* |
| 476 | * We don't call sshpam_cleanup() here because we may need the PAM |
| 477 | * handle at a later stage, e.g. when setting up a session. It's |
| 478 | * still on the cleanup list, so pam_end() *will* be called before |
| 479 | * the server process terminates. |
| 480 | */ |
| 481 | } |
| 482 | |
| 483 | KbdintDevice sshpam_device = { |
| 484 | "pam", |
| 485 | sshpam_init_ctx, |
| 486 | sshpam_query, |
| 487 | sshpam_respond, |
| 488 | sshpam_free_ctx |
| 489 | }; |
| 490 | |
| 491 | KbdintDevice mm_sshpam_device = { |
| 492 | "pam", |
| 493 | mm_sshpam_init_ctx, |
| 494 | mm_sshpam_query, |
| 495 | mm_sshpam_respond, |
| 496 | mm_sshpam_free_ctx |
| 497 | }; |
| 498 | |
| 499 | /* |
| 500 | * This replaces auth-pam.c |
| 501 | */ |
| 502 | void |
| 503 | start_pam(const char *user) |
| 504 | { |
Damien Miller | 9d507da | 2003-05-14 15:31:12 +1000 | [diff] [blame] | 505 | if (!options.use_pam) |
| 506 | fatal("PAM: initialisation requested when UsePAM=no"); |
| 507 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 508 | if (sshpam_init(user) == -1) |
| 509 | fatal("PAM: initialisation failed"); |
| 510 | } |
| 511 | |
| 512 | void |
| 513 | finish_pam(void) |
| 514 | { |
| 515 | fatal_remove_cleanup(sshpam_cleanup, NULL); |
| 516 | sshpam_cleanup(NULL); |
| 517 | } |
| 518 | |
| 519 | int |
| 520 | do_pam_account(const char *user, const char *ruser) |
| 521 | { |
| 522 | /* XXX */ |
| 523 | return (1); |
| 524 | } |
| 525 | |
| 526 | void |
| 527 | do_pam_session(const char *user, const char *tty) |
| 528 | { |
| 529 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
| 530 | (const void *)&null_conv); |
| 531 | if (sshpam_err != PAM_SUCCESS) |
| 532 | fatal("PAM: failed to set PAM_CONV: %s", |
| 533 | pam_strerror(sshpam_handle, sshpam_err)); |
| 534 | debug("PAM: setting PAM_TTY to \"%s\"", tty); |
| 535 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty); |
| 536 | if (sshpam_err != PAM_SUCCESS) |
| 537 | fatal("PAM: failed to set PAM_TTY: %s", |
| 538 | pam_strerror(sshpam_handle, sshpam_err)); |
| 539 | sshpam_err = pam_open_session(sshpam_handle, 0); |
| 540 | if (sshpam_err != PAM_SUCCESS) |
| 541 | fatal("PAM: pam_open_session(): %s", |
| 542 | pam_strerror(sshpam_handle, sshpam_err)); |
| 543 | sshpam_session_open = 1; |
| 544 | } |
| 545 | |
| 546 | void |
| 547 | do_pam_setcred(int init) |
| 548 | { |
| 549 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
| 550 | (const void *)&null_conv); |
| 551 | if (sshpam_err != PAM_SUCCESS) |
| 552 | fatal("PAM: failed to set PAM_CONV: %s", |
| 553 | pam_strerror(sshpam_handle, sshpam_err)); |
| 554 | if (init) { |
| 555 | debug("PAM: establishing credentials"); |
| 556 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); |
| 557 | } else { |
| 558 | debug("PAM: reinitializing credentials"); |
| 559 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); |
| 560 | } |
| 561 | if (sshpam_err == PAM_SUCCESS) { |
| 562 | sshpam_cred_established = 1; |
| 563 | return; |
| 564 | } |
| 565 | if (sshpam_authenticated) |
| 566 | fatal("PAM: pam_setcred(): %s", |
| 567 | pam_strerror(sshpam_handle, sshpam_err)); |
| 568 | else |
| 569 | debug("PAM: pam_setcred(): %s", |
| 570 | pam_strerror(sshpam_handle, sshpam_err)); |
| 571 | } |
| 572 | |
| 573 | int |
| 574 | is_pam_password_change_required(void) |
| 575 | { |
| 576 | return (sshpam_new_authtok_reqd); |
| 577 | } |
| 578 | |
| 579 | static int |
| 580 | pam_chauthtok_conv(int n, |
| 581 | const struct pam_message **msg, |
| 582 | struct pam_response **resp, |
| 583 | void *data) |
| 584 | { |
| 585 | char input[PAM_MAX_MSG_SIZE]; |
| 586 | int i; |
| 587 | |
| 588 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 589 | return (PAM_CONV_ERR); |
| 590 | *resp = xmalloc(n * sizeof **resp); |
| 591 | for (i = 0; i < n; ++i) { |
| 592 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 593 | case PAM_PROMPT_ECHO_OFF: |
| 594 | resp[i]->resp = |
| 595 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
| 596 | RP_ALLOW_STDIN); |
| 597 | resp[i]->resp_retcode = PAM_SUCCESS; |
| 598 | break; |
| 599 | case PAM_PROMPT_ECHO_ON: |
| 600 | fputs(PAM_MSG_MEMBER(msg, i, msg), stderr); |
| 601 | fgets(input, sizeof input, stdin); |
| 602 | resp[i]->resp = xstrdup(input); |
| 603 | resp[i]->resp_retcode = PAM_SUCCESS; |
| 604 | break; |
| 605 | case PAM_ERROR_MSG: |
| 606 | case PAM_TEXT_INFO: |
| 607 | fputs(PAM_MSG_MEMBER(msg, i, msg), stderr); |
| 608 | resp[i]->resp_retcode = PAM_SUCCESS; |
| 609 | break; |
| 610 | default: |
| 611 | goto fail; |
| 612 | } |
| 613 | } |
| 614 | return (PAM_SUCCESS); |
| 615 | fail: |
| 616 | while (i) |
| 617 | xfree(resp[--i]); |
| 618 | xfree(*resp); |
| 619 | *resp = NULL; |
| 620 | return (PAM_CONV_ERR); |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * XXX this should be done in the authentication phase, but ssh1 doesn't |
| 625 | * support that |
| 626 | */ |
| 627 | void |
| 628 | do_pam_chauthtok(void) |
| 629 | { |
| 630 | struct pam_conv pam_conv = { pam_chauthtok_conv, NULL }; |
| 631 | |
| 632 | if (use_privsep) |
| 633 | fatal("PAM: chauthtok not supprted with privsep"); |
| 634 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
| 635 | (const void *)&pam_conv); |
| 636 | if (sshpam_err != PAM_SUCCESS) |
| 637 | fatal("PAM: failed to set PAM_CONV: %s", |
| 638 | pam_strerror(sshpam_handle, sshpam_err)); |
| 639 | debug("PAM: changing password"); |
| 640 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); |
| 641 | if (sshpam_err != PAM_SUCCESS) |
| 642 | fatal("PAM: pam_chauthtok(): %s", |
| 643 | pam_strerror(sshpam_handle, sshpam_err)); |
| 644 | } |
| 645 | |
| 646 | void |
| 647 | print_pam_messages(void) |
| 648 | { |
| 649 | /* XXX */ |
| 650 | } |
| 651 | |
| 652 | char ** |
| 653 | fetch_pam_environment(void) |
| 654 | { |
| 655 | #ifdef HAVE_PAM_GETENVLIST |
| 656 | debug("PAM: retrieving environment"); |
| 657 | return (pam_getenvlist(sshpam_handle)); |
| 658 | #else |
| 659 | return (NULL); |
| 660 | #endif |
| 661 | } |
| 662 | |
| 663 | void |
| 664 | free_pam_environment(char **env) |
| 665 | { |
| 666 | char **envp; |
| 667 | |
Damien Miller | e27c6cc | 2003-05-16 18:21:01 +1000 | [diff] [blame] | 668 | if (env == NULL) |
| 669 | return; |
| 670 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 671 | for (envp = env; *envp; envp++) |
| 672 | xfree(*envp); |
| 673 | xfree(env); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | #endif /* USE_PAM */ |