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