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