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