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