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