]> andersk Git - openssh.git/blame - auth-pam.c
- stevesk@cvs.openbsd.org 2006/07/23 01:11:05
[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 */
93c5ef94 31/*
32 * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org>
33 * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au>
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 */
a5c9cd31 47
5ff453c0 48/* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
a5c9cd31 49#include "includes.h"
50
b5b88c19 51#include <sys/types.h>
52#include <sys/stat.h>
53#include <sys/wait.h>
4352744e 54
55#include <errno.h>
b5b88c19 56#include <signal.h>
57
a5c9cd31 58#ifdef USE_PAM
2511d104 59#if defined(HAVE_SECURITY_PAM_APPL_H)
05114c74 60#include <security/pam_appl.h>
2511d104 61#elif defined (HAVE_PAM_PAM_APPL_H)
62#include <pam/pam_appl.h>
63#endif
05114c74 64
44171182 65/* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
66#ifdef PAM_SUN_CODEBASE
67# define sshpam_const /* Solaris, HP-UX, AIX */
68#else
69# define sshpam_const const /* LinuxPAM, OpenPAM */
70#endif
71
fde58bd4 72#include "auth.h"
5c377b3b 73#include "auth-pam.h"
05114c74 74#include "buffer.h"
75#include "bufaux.h"
42f11eb2 76#include "canohost.h"
05114c74 77#include "log.h"
78#include "monitor_wrap.h"
79#include "msg.h"
80#include "packet.h"
0f3ee929 81#include "misc.h"
05114c74 82#include "servconf.h"
83#include "ssh2.h"
84#include "xmalloc.h"
5b9e2464 85#include "auth-options.h"
a5c9cd31 86
7fceb20d 87extern ServerOptions options;
3eaf3960 88extern Buffer loginmsg;
b3ef7fb7 89extern int compat20;
1e08e787 90extern u_int utmp_len;
7fceb20d 91
b4143b2a 92/* so we don't silently change behaviour */
05114c74 93#ifdef USE_POSIX_THREADS
b4143b2a 94# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
95#endif
96
97/*
98 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
99 * and generally a bad idea. Use at own risk and do not expect support if
100 * this breaks.
101 */
102#ifdef UNSUPPORTED_POSIX_THREADS_HACK
05114c74 103#include <pthread.h>
104/*
aff51935 105 * Avoid namespace clash when *not* using pthreads for systems *with*
106 * pthreads, which unconditionally define pthread_t via sys/types.h
05114c74 107 * (e.g. Linux)
108 */
aff51935 109typedef pthread_t sp_pthread_t;
05114c74 110#else
e47e681f 111typedef pid_t sp_pthread_t;
112#endif
113
114struct pam_ctxt {
115 sp_pthread_t pam_thread;
116 int pam_psock;
117 int pam_csock;
118 int pam_done;
119};
120
121static void sshpam_free_ctx(void *);
122static struct pam_ctxt *cleanup_ctxt;
123
b4143b2a 124#ifndef UNSUPPORTED_POSIX_THREADS_HACK
05114c74 125/*
126 * Simulate threads with processes.
127 */
a5c9cd31 128
100e6910 129static int sshpam_thread_status = -1;
130static mysig_t sshpam_oldsig;
131
d1cf9a87 132static void
100e6910 133sshpam_sigchld_handler(int sig)
134{
8a956cda 135 signal(SIGCHLD, SIG_DFL);
28b49ff8 136 if (cleanup_ctxt == NULL)
137 return; /* handler called after PAM cleanup, shouldn't happen */
8a956cda 138 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
ed9e8be3 139 <= 0) {
8a956cda 140 /* PAM thread has not exitted, privsep slave must have */
141 kill(cleanup_ctxt->pam_thread, SIGTERM);
142 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
930d0441 143 <= 0)
8a956cda 144 return; /* could not wait */
145 }
100e6910 146 if (WIFSIGNALED(sshpam_thread_status) &&
147 WTERMSIG(sshpam_thread_status) == SIGTERM)
148 return; /* terminated by pthread_cancel */
149 if (!WIFEXITED(sshpam_thread_status))
150 fatal("PAM: authentication thread exited unexpectedly");
151 if (WEXITSTATUS(sshpam_thread_status) != 0)
152 fatal("PAM: authentication thread exited uncleanly");
153}
154
05114c74 155static void
156pthread_exit(void *value __unused)
157{
158 _exit(0);
159}
5daf7064 160
05114c74 161static int
162pthread_create(sp_pthread_t *thread, const void *attr __unused,
163 void *(*thread_start)(void *), void *arg)
164{
165 pid_t pid;
5d001c70 166 struct pam_ctxt *ctx = arg;
05114c74 167
355fbf31 168 sshpam_thread_status = -1;
05114c74 169 switch ((pid = fork())) {
170 case -1:
171 error("fork(): %s", strerror(errno));
172 return (-1);
173 case 0:
5d001c70 174 close(ctx->pam_psock);
175 ctx->pam_psock = -1;
05114c74 176 thread_start(arg);
177 _exit(1);
178 default:
179 *thread = pid;
5d001c70 180 close(ctx->pam_csock);
181 ctx->pam_csock = -1;
100e6910 182 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
05114c74 183 return (0);
184 }
185}
a5c9cd31 186
05114c74 187static int
188pthread_cancel(sp_pthread_t thread)
8c9fe09e 189{
28b49ff8 190 signal(SIGCHLD, sshpam_oldsig);
05114c74 191 return (kill(thread, SIGTERM));
8c9fe09e 192}
193
05114c74 194static int
195pthread_join(sp_pthread_t thread, void **value __unused)
8c9fe09e 196{
05114c74 197 int status;
198
100e6910 199 if (sshpam_thread_status != -1)
200 return (sshpam_thread_status);
201 signal(SIGCHLD, sshpam_oldsig);
05114c74 202 waitpid(thread, &status, 0);
203 return (status);
8c9fe09e 204}
05114c74 205#endif
206
207
0a23d79f 208static pam_handle_t *sshpam_handle = NULL;
209static int sshpam_err = 0;
210static int sshpam_authenticated = 0;
0a23d79f 211static int sshpam_session_open = 0;
212static int sshpam_cred_established = 0;
b3ef7fb7 213static int sshpam_account_status = -1;
2212fc98 214static char **sshpam_env = NULL;
d948154a 215static Authctxt *sshpam_authctxt = NULL;
5de92f17 216static const char *sshpam_password = NULL;
af0e5c2f 217static char badpw[] = "\b\n\r\177INCORRECT";
05114c74 218
2212fc98 219/* Some PAM implementations don't implement this */
220#ifndef HAVE_PAM_GETENVLIST
221static char **
222pam_getenvlist(pam_handle_t *pamh)
223{
224 /*
aff51935 225 * XXX - If necessary, we can still support envrionment passing
2212fc98 226 * for platforms without pam_getenvlist by searching for known
227 * env vars (e.g. KRB5CCNAME) from the PAM environment.
228 */
229 return NULL;
230}
231#endif
232
059d3165 233/*
234 * Some platforms, notably Solaris, do not enforce password complexity
235 * rules during pam_chauthtok() if the real uid of the calling process
236 * is 0, on the assumption that it's being called by "passwd" run by root.
237 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
238 * the right thing.
239 */
240#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
241static int
242sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
243{
244 int result;
245
246 if (sshpam_authctxt == NULL)
247 fatal("PAM: sshpam_authctxt not initialized");
248 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
249 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
250 result = pam_chauthtok(pamh, flags);
251 if (setreuid(0, -1) == -1)
252 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
253 return result;
254}
255# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
256#endif
257
b3ef7fb7 258void
c10bb2ce 259sshpam_password_change_required(int reqd)
b3ef7fb7 260{
90f3c272 261 debug3("%s %d", __func__, reqd);
d948154a 262 if (sshpam_authctxt == NULL)
529d73ab 263 fatal("%s: PAM authctxt not initialized", __func__);
d948154a 264 sshpam_authctxt->force_pwchange = reqd;
b3ef7fb7 265 if (reqd) {
266 no_port_forwarding_flag |= 2;
267 no_agent_forwarding_flag |= 2;
268 no_x11_forwarding_flag |= 2;
269 } else {
270 no_port_forwarding_flag &= ~2;
271 no_agent_forwarding_flag &= ~2;
272 no_x11_forwarding_flag &= ~2;
b3ef7fb7 273 }
274}
dd1fb864 275
2212fc98 276/* Import regular and PAM environment from subprocess */
277static void
278import_environments(Buffer *b)
279{
280 char *env;
281 u_int i, num_env;
282 int err;
283
90f3c272 284 debug3("PAM: %s entering", __func__);
285
b4143b2a 286#ifndef UNSUPPORTED_POSIX_THREADS_HACK
b3ef7fb7 287 /* Import variables set by do_pam_account */
288 sshpam_account_status = buffer_get_int(b);
c10bb2ce 289 sshpam_password_change_required(buffer_get_int(b));
b3ef7fb7 290
2212fc98 291 /* Import environment from subprocess */
292 num_env = buffer_get_int(b);
01d35895 293 if (num_env > 1024)
294 fatal("%s: received %u environment variables, expected <= 1024",
295 __func__, num_env);
296 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
2212fc98 297 debug3("PAM: num env strings %d", num_env);
298 for(i = 0; i < num_env; i++)
299 sshpam_env[i] = buffer_get_string(b, NULL);
300
301 sshpam_env[num_env] = NULL;
302
303 /* Import PAM environment from subprocess */
304 num_env = buffer_get_int(b);
305 debug("PAM: num PAM env strings %d", num_env);
306 for(i = 0; i < num_env; i++) {
307 env = buffer_get_string(b, NULL);
308
f79a6165 309#ifdef HAVE_PAM_PUTENV
2212fc98 310 /* Errors are not fatal here */
311 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
312 error("PAM: pam_putenv: %s",
313 pam_strerror(sshpam_handle, sshpam_err));
314 }
f79a6165 315#endif
2212fc98 316 }
a1e0095d 317#endif
2212fc98 318}
319
ad55cd03 320/*
05114c74 321 * Conversation function for authentication thread.
ad55cd03 322 */
05114c74 323static int
44171182 324sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
5b9e2464 325 struct pam_response **resp, void *data)
a5c9cd31 326{
05114c74 327 Buffer buffer;
328 struct pam_ctxt *ctxt;
0a23d79f 329 struct pam_response *reply;
05114c74 330 int i;
331
60922169 332 debug3("PAM: %s entering, %d messages", __func__, n);
0a23d79f 333 *resp = NULL;
334
d506e25f 335 if (data == NULL) {
336 error("PAM: conversation function passed a null context");
337 return (PAM_CONV_ERR);
338 }
05114c74 339 ctxt = data;
340 if (n <= 0 || n > PAM_MAX_NUM_MSG)
341 return (PAM_CONV_ERR);
0a23d79f 342
01d35895 343 if ((reply = calloc(n, sizeof(*reply))) == NULL)
0a23d79f 344 return (PAM_CONV_ERR);
0a23d79f 345
05114c74 346 buffer_init(&buffer);
347 for (i = 0; i < n; ++i) {
05114c74 348 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
349 case PAM_PROMPT_ECHO_OFF:
aff51935 350 buffer_put_cstring(&buffer,
0a23d79f 351 PAM_MSG_MEMBER(msg, i, msg));
aff51935 352 if (ssh_msg_send(ctxt->pam_csock,
7b2a0de3 353 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
354 goto fail;
aff51935 355 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
7b2a0de3 356 goto fail;
05114c74 357 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
358 goto fail;
0a23d79f 359 reply[i].resp = buffer_get_string(&buffer, NULL);
05114c74 360 break;
361 case PAM_PROMPT_ECHO_ON:
aff51935 362 buffer_put_cstring(&buffer,
0a23d79f 363 PAM_MSG_MEMBER(msg, i, msg));
aff51935 364 if (ssh_msg_send(ctxt->pam_csock,
7b2a0de3 365 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
366 goto fail;
367 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
368 goto fail;
05114c74 369 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
370 goto fail;
0a23d79f 371 reply[i].resp = buffer_get_string(&buffer, NULL);
05114c74 372 break;
373 case PAM_ERROR_MSG:
aff51935 374 buffer_put_cstring(&buffer,
0a23d79f 375 PAM_MSG_MEMBER(msg, i, msg));
aff51935 376 if (ssh_msg_send(ctxt->pam_csock,
7b2a0de3 377 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
378 goto fail;
05114c74 379 break;
380 case PAM_TEXT_INFO:
aff51935 381 buffer_put_cstring(&buffer,
0a23d79f 382 PAM_MSG_MEMBER(msg, i, msg));
aff51935 383 if (ssh_msg_send(ctxt->pam_csock,
7b2a0de3 384 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
385 goto fail;
05114c74 386 break;
387 default:
388 goto fail;
a5c9cd31 389 }
05114c74 390 buffer_clear(&buffer);
a5c9cd31 391 }
05114c74 392 buffer_free(&buffer);
0a23d79f 393 *resp = reply;
05114c74 394 return (PAM_SUCCESS);
0a23d79f 395
05114c74 396 fail:
0a23d79f 397 for(i = 0; i < n; i++) {
398 if (reply[i].resp != NULL)
399 xfree(reply[i].resp);
400 }
401 xfree(reply);
05114c74 402 buffer_free(&buffer);
403 return (PAM_CONV_ERR);
404}
a5c9cd31 405
05114c74 406/*
407 * Authentication thread.
408 */
409static void *
410sshpam_thread(void *ctxtp)
411{
412 struct pam_ctxt *ctxt = ctxtp;
413 Buffer buffer;
c53917a9 414 struct pam_conv sshpam_conv;
e4472e7e 415 int flags = (options.permit_empty_passwd == 0 ?
416 PAM_DISALLOW_NULL_AUTHTOK : 0);
b4143b2a 417#ifndef UNSUPPORTED_POSIX_THREADS_HACK
2212fc98 418 extern char **environ;
419 char **env_from_pam;
420 u_int i;
05114c74 421 const char *pam_user;
44171182 422 const char **ptr_pam_user = &pam_user;
05114c74 423
44171182 424 pam_get_item(sshpam_handle, PAM_USER,
425 (sshpam_const void **)ptr_pam_user);
2212fc98 426 environ[0] = NULL;
da97d54d 427
428 if (sshpam_authctxt != NULL) {
429 setproctitle("%s [pam]",
430 sshpam_authctxt->valid ? pam_user : "unknown");
431 }
05114c74 432#endif
a5c9cd31 433
c53917a9 434 sshpam_conv.conv = sshpam_thread_conv;
435 sshpam_conv.appdata_ptr = ctxt;
436
d948154a 437 if (sshpam_authctxt == NULL)
529d73ab 438 fatal("%s: PAM authctxt not initialized", __func__);
439
05114c74 440 buffer_init(&buffer);
441 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
442 (const void *)&sshpam_conv);
443 if (sshpam_err != PAM_SUCCESS)
444 goto auth_fail;
e4472e7e 445 sshpam_err = pam_authenticate(sshpam_handle, flags);
05114c74 446 if (sshpam_err != PAM_SUCCESS)
447 goto auth_fail;
b3ef7fb7 448
2935db92 449 if (compat20) {
5aa56df0 450 if (!do_pam_account()) {
451 sshpam_err = PAM_ACCT_EXPIRED;
b3ef7fb7 452 goto auth_fail;
5aa56df0 453 }
d948154a 454 if (sshpam_authctxt->force_pwchange) {
b3ef7fb7 455 sshpam_err = pam_chauthtok(sshpam_handle,
456 PAM_CHANGE_EXPIRED_AUTHTOK);
457 if (sshpam_err != PAM_SUCCESS)
458 goto auth_fail;
c10bb2ce 459 sshpam_password_change_required(0);
b3ef7fb7 460 }
2935db92 461 }
b3ef7fb7 462
05114c74 463 buffer_put_cstring(&buffer, "OK");
2212fc98 464
b4143b2a 465#ifndef UNSUPPORTED_POSIX_THREADS_HACK
b3ef7fb7 466 /* Export variables set by do_pam_account */
467 buffer_put_int(&buffer, sshpam_account_status);
d948154a 468 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
b3ef7fb7 469
2212fc98 470 /* Export any environment strings set in child */
471 for(i = 0; environ[i] != NULL; i++)
472 ; /* Count */
473 buffer_put_int(&buffer, i);
474 for(i = 0; environ[i] != NULL; i++)
475 buffer_put_cstring(&buffer, environ[i]);
476
477 /* Export any environment strings set by PAM in child */
478 env_from_pam = pam_getenvlist(sshpam_handle);
479 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
480 ; /* Count */
481 buffer_put_int(&buffer, i);
482 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
483 buffer_put_cstring(&buffer, env_from_pam[i]);
b4143b2a 484#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
2212fc98 485
7b2a0de3 486 /* XXX - can't do much about an error here */
05114c74 487 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
488 buffer_free(&buffer);
489 pthread_exit(NULL);
490
491 auth_fail:
492 buffer_put_cstring(&buffer,
493 pam_strerror(sshpam_handle, sshpam_err));
7b2a0de3 494 /* XXX - can't do much about an error here */
5aa56df0 495 if (sshpam_err == PAM_ACCT_EXPIRED)
496 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
497 else
498 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
05114c74 499 buffer_free(&buffer);
500 pthread_exit(NULL);
b6453d99 501
05114c74 502 return (NULL); /* Avoid warning for non-pthread case */
a5c9cd31 503}
504
c6630044 505void
506sshpam_thread_cleanup(void)
a5c9cd31 507{
c6630044 508 struct pam_ctxt *ctxt = cleanup_ctxt;
509
90f3c272 510 debug3("PAM: %s entering", __func__);
c6630044 511 if (ctxt != NULL && ctxt->pam_thread != 0) {
512 pthread_cancel(ctxt->pam_thread);
513 pthread_join(ctxt->pam_thread, NULL);
514 close(ctxt->pam_psock);
515 close(ctxt->pam_csock);
516 memset(ctxt, 0, sizeof(*ctxt));
517 cleanup_ctxt = NULL;
518 }
05114c74 519}
a5c9cd31 520
05114c74 521static int
44171182 522sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
5b9e2464 523 struct pam_response **resp, void *data)
05114c74 524{
60922169 525 debug3("PAM: %s entering, %d messages", __func__, n);
05114c74 526 return (PAM_CONV_ERR);
527}
a5c9cd31 528
05114c74 529static struct pam_conv null_conv = { sshpam_null_conv, NULL };
530
ab17aac2 531static int
44171182 532sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
ab17aac2 533 struct pam_response **resp, void *data)
534{
535 struct pam_response *reply;
536 int i;
537 size_t len;
538
539 debug3("PAM: %s called with %d messages", __func__, n);
540 *resp = NULL;
541
542 if (n <= 0 || n > PAM_MAX_NUM_MSG)
543 return (PAM_CONV_ERR);
544
01d35895 545 if ((reply = calloc(n, sizeof(*reply))) == NULL)
ab17aac2 546 return (PAM_CONV_ERR);
ab17aac2 547
548 for (i = 0; i < n; ++i) {
549 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
550 case PAM_ERROR_MSG:
551 case PAM_TEXT_INFO:
552 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
553 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
554 buffer_append(&loginmsg, "\n", 1 );
555 reply[i].resp_retcode = PAM_SUCCESS;
556 break;
557 default:
558 goto fail;
559 }
560 }
561 *resp = reply;
562 return (PAM_SUCCESS);
563
564 fail:
565 for(i = 0; i < n; i++) {
566 if (reply[i].resp != NULL)
567 xfree(reply[i].resp);
568 }
569 xfree(reply);
570 return (PAM_CONV_ERR);
571}
572
573static struct pam_conv store_conv = { sshpam_store_conv, NULL };
574
c6630044 575void
576sshpam_cleanup(void)
05114c74 577{
05114c74 578 debug("PAM: cleanup");
0a23d79f 579 if (sshpam_handle == NULL)
580 return;
05114c74 581 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
582 if (sshpam_cred_established) {
583 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
584 sshpam_cred_established = 0;
585 }
586 if (sshpam_session_open) {
587 pam_close_session(sshpam_handle, PAM_SILENT);
588 sshpam_session_open = 0;
a5c9cd31 589 }
dd1fb864 590 sshpam_authenticated = 0;
05114c74 591 pam_end(sshpam_handle, sshpam_err);
592 sshpam_handle = NULL;
a5c9cd31 593}
594
05114c74 595static int
529d73ab 596sshpam_init(Authctxt *authctxt)
a5c9cd31 597{
74678fb9 598 extern char *__progname;
529d73ab 599 const char *pam_rhost, *pam_user, *user = authctxt->user;
44171182 600 const char **ptr_pam_user = &pam_user;
05114c74 601
602 if (sshpam_handle != NULL) {
603 /* We already have a PAM context; check if the user matches */
604 sshpam_err = pam_get_item(sshpam_handle,
44171182 605 PAM_USER, (sshpam_const void **)ptr_pam_user);
05114c74 606 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
607 return (0);
05114c74 608 pam_end(sshpam_handle, sshpam_err);
609 sshpam_handle = NULL;
610 }
611 debug("PAM: initializing for \"%s\"", user);
01224183 612 sshpam_err =
ba6dd90e 613 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
d948154a 614 sshpam_authctxt = authctxt;
529d73ab 615
5ff453c0 616 if (sshpam_err != PAM_SUCCESS) {
617 pam_end(sshpam_handle, sshpam_err);
618 sshpam_handle = NULL;
05114c74 619 return (-1);
5ff453c0 620 }
c5a7d788 621 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
05d62329 622 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
05114c74 623 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
5ff453c0 624 if (sshpam_err != PAM_SUCCESS) {
5b9e2464 625 pam_end(sshpam_handle, sshpam_err);
5ff453c0 626 sshpam_handle = NULL;
627 return (-1);
628 }
629#ifdef PAM_TTY_KLUDGE
aff51935 630 /*
631 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
632 * sshd doesn't set the tty until too late in the auth process and
5ff453c0 633 * may not even set one (for tty-less connections)
aff51935 634 */
5ff453c0 635 debug("PAM: setting PAM_TTY to \"ssh\"");
636 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
05114c74 637 if (sshpam_err != PAM_SUCCESS) {
638 pam_end(sshpam_handle, sshpam_err);
639 sshpam_handle = NULL;
640 return (-1);
a5c9cd31 641 }
5ff453c0 642#endif
05114c74 643 return (0);
a5c9cd31 644}
645
05114c74 646static void *
647sshpam_init_ctx(Authctxt *authctxt)
a5c9cd31 648{
05114c74 649 struct pam_ctxt *ctxt;
650 int socks[2];
2b87da3b 651
90f3c272 652 debug3("PAM: %s entering", __func__);
5aa56df0 653 /*
654 * Refuse to start if we don't have PAM enabled or do_pam_account
655 * has previously failed.
656 */
657 if (!options.use_pam || sshpam_account_status == 0)
7fceb20d 658 return NULL;
659
05114c74 660 /* Initialize PAM */
529d73ab 661 if (sshpam_init(authctxt) == -1) {
05114c74 662 error("PAM: initialization failed");
663 return (NULL);
664 }
5c377b3b 665
05114c74 666 ctxt = xmalloc(sizeof *ctxt);
c6630044 667 memset(ctxt, 0, sizeof(*ctxt));
05114c74 668
669 /* Start the authentication thread */
670 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
671 error("PAM: failed create sockets: %s", strerror(errno));
672 xfree(ctxt);
673 return (NULL);
674 }
675 ctxt->pam_psock = socks[0];
676 ctxt->pam_csock = socks[1];
677 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
678 error("PAM: failed to start authentication thread: %s",
679 strerror(errno));
680 close(socks[0]);
681 close(socks[1]);
682 xfree(ctxt);
683 return (NULL);
a5c9cd31 684 }
c6630044 685 cleanup_ctxt = ctxt;
05114c74 686 return (ctxt);
687}
a5c9cd31 688
05114c74 689static int
690sshpam_query(void *ctx, char **name, char **info,
691 u_int *num, char ***prompts, u_int **echo_on)
692{
693 Buffer buffer;
694 struct pam_ctxt *ctxt = ctx;
695 size_t plen;
696 u_char type;
697 char *msg;
bbe58934 698 size_t len, mlen;
05114c74 699
90f3c272 700 debug3("PAM: %s entering", __func__);
05114c74 701 buffer_init(&buffer);
702 *name = xstrdup("");
703 *info = xstrdup("");
704 *prompts = xmalloc(sizeof(char *));
705 **prompts = NULL;
706 plen = 0;
707 *echo_on = xmalloc(sizeof(u_int));
708 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
709 type = buffer_get_char(&buffer);
710 msg = buffer_get_string(&buffer, NULL);
bbe58934 711 mlen = strlen(msg);
05114c74 712 switch (type) {
713 case PAM_PROMPT_ECHO_ON:
714 case PAM_PROMPT_ECHO_OFF:
715 *num = 1;
bbe58934 716 len = plen + mlen + 1;
c5d10563 717 **prompts = xrealloc(**prompts, 1, len);
bbe58934 718 strlcpy(**prompts + plen, msg, len - plen);
719 plen += mlen;
05114c74 720 **echo_on = (type == PAM_PROMPT_ECHO_ON);
721 xfree(msg);
722 return (0);
723 case PAM_ERROR_MSG:
724 case PAM_TEXT_INFO:
725 /* accumulate messages */
bbe58934 726 len = plen + mlen + 2;
c5d10563 727 **prompts = xrealloc(**prompts, 1, len);
bbe58934 728 strlcpy(**prompts + plen, msg, len - plen);
729 plen += mlen;
730 strlcat(**prompts + plen, "\n", len - plen);
731 plen++;
05114c74 732 xfree(msg);
5daf7064 733 break;
5aa56df0 734 case PAM_ACCT_EXPIRED:
735 sshpam_account_status = 0;
736 /* FALLTHROUGH */
05114c74 737 case PAM_AUTH_ERR:
5aa56df0 738 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
cafa6a80 739 if (**prompts != NULL && strlen(**prompts) != 0) {
740 *info = **prompts;
741 **prompts = NULL;
742 *num = 0;
743 **echo_on = 0;
744 ctxt->pam_done = -1;
08a3f952 745 xfree(msg);
cafa6a80 746 return 0;
747 }
748 /* FALLTHROUGH */
749 case PAM_SUCCESS:
05114c74 750 if (**prompts != NULL) {
751 /* drain any accumulated messages */
3eaf3960 752 debug("PAM: %s", **prompts);
753 buffer_append(&loginmsg, **prompts,
754 strlen(**prompts));
05114c74 755 xfree(**prompts);
756 **prompts = NULL;
757 }
758 if (type == PAM_SUCCESS) {
2d5104e2 759 if (!sshpam_authctxt->valid ||
760 (sshpam_authctxt->pw->pw_uid == 0 &&
761 options.permit_root_login != PERMIT_YES))
af0e5c2f 762 fatal("Internal error: PAM auth "
763 "succeeded when it should have "
764 "failed");
2212fc98 765 import_environments(&buffer);
05114c74 766 *num = 0;
767 **echo_on = 0;
768 ctxt->pam_done = 1;
769 xfree(msg);
770 return (0);
771 }
1e08e787 772 error("PAM: %s for %s%.100s from %.100s", msg,
773 sshpam_authctxt->valid ? "" : "illegal user ",
774 sshpam_authctxt->user,
775 get_remote_name_or_ip(utmp_len, options.use_dns));
e250770b 776 /* FALLTHROUGH */
5daf7064 777 default:
05114c74 778 *num = 0;
779 **echo_on = 0;
780 xfree(msg);
781 ctxt->pam_done = -1;
782 return (-1);
783 }
a5c9cd31 784 }
05114c74 785 return (-1);
a5c9cd31 786}
787
05114c74 788/* XXX - see also comment in auth-chall.c:verify_response */
789static int
790sshpam_respond(void *ctx, u_int num, char **resp)
a5c9cd31 791{
05114c74 792 Buffer buffer;
793 struct pam_ctxt *ctxt = ctx;
794
912acf53 795 debug2("PAM: %s entering, %u responses", __func__, num);
05114c74 796 switch (ctxt->pam_done) {
797 case 1:
798 sshpam_authenticated = 1;
799 return (0);
800 case 0:
801 break;
802 default:
803 return (-1);
a5c9cd31 804 }
05114c74 805 if (num != 1) {
806 error("PAM: expected one response, got %u", num);
807 return (-1);
808 }
809 buffer_init(&buffer);
2d5104e2 810 if (sshpam_authctxt->valid &&
811 (sshpam_authctxt->pw->pw_uid != 0 ||
ed9e8be3 812 options.permit_root_login == PERMIT_YES))
af0e5c2f 813 buffer_put_cstring(&buffer, *resp);
814 else
815 buffer_put_cstring(&buffer, badpw);
7b2a0de3 816 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
817 buffer_free(&buffer);
818 return (-1);
819 }
05114c74 820 buffer_free(&buffer);
821 return (1);
a5c9cd31 822}
823
05114c74 824static void
825sshpam_free_ctx(void *ctxtp)
a5c9cd31 826{
05114c74 827 struct pam_ctxt *ctxt = ctxtp;
39ce53de 828
90f3c272 829 debug3("PAM: %s entering", __func__);
c6630044 830 sshpam_thread_cleanup();
05114c74 831 xfree(ctxt);
832 /*
833 * We don't call sshpam_cleanup() here because we may need the PAM
834 * handle at a later stage, e.g. when setting up a session. It's
835 * still on the cleanup list, so pam_end() *will* be called before
836 * the server process terminates.
837 */
ad55cd03 838}
839
05114c74 840KbdintDevice sshpam_device = {
841 "pam",
842 sshpam_init_ctx,
843 sshpam_query,
844 sshpam_respond,
845 sshpam_free_ctx
846};
847
848KbdintDevice mm_sshpam_device = {
849 "pam",
850 mm_sshpam_init_ctx,
851 mm_sshpam_query,
852 mm_sshpam_respond,
853 mm_sshpam_free_ctx
854};
2919e060 855
2b87da3b 856/*
05114c74 857 * This replaces auth-pam.c
ad55cd03 858 */
05114c74 859void
529d73ab 860start_pam(Authctxt *authctxt)
ad55cd03 861{
817e6d38 862 if (!options.use_pam)
863 fatal("PAM: initialisation requested when UsePAM=no");
864
529d73ab 865 if (sshpam_init(authctxt) == -1)
05114c74 866 fatal("PAM: initialisation failed");
a5c9cd31 867}
868
05114c74 869void
870finish_pam(void)
a5c9cd31 871{
c6630044 872 sshpam_cleanup();
a5c9cd31 873}
874
5b9e2464 875u_int
876do_pam_account(void)
a5c9cd31 877{
ba6dd90e 878 debug("%s: called", __func__);
b3ef7fb7 879 if (sshpam_account_status != -1)
880 return (sshpam_account_status);
881
5b9e2464 882 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
ba6dd90e 883 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
884 pam_strerror(sshpam_handle, sshpam_err));
d1cf9a87 885
b3ef7fb7 886 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
887 sshpam_account_status = 0;
888 return (sshpam_account_status);
5b9e2464 889 }
890
b3ef7fb7 891 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
c10bb2ce 892 sshpam_password_change_required(1);
b3ef7fb7 893
894 sshpam_account_status = 1;
895 return (sshpam_account_status);
05114c74 896}
46c76c63 897
49e82bb9 898void
899do_pam_set_tty(const char *tty)
900{
26b3608b 901 if (tty != NULL) {
902 debug("PAM: setting PAM_TTY to \"%s\"", tty);
903 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
904 if (sshpam_err != PAM_SUCCESS)
905 fatal("PAM: failed to set PAM_TTY: %s",
906 pam_strerror(sshpam_handle, sshpam_err));
907 }
05114c74 908}
cbd7492e 909
05114c74 910void
911do_pam_setcred(int init)
912{
913 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
ba6dd90e 914 (const void *)&store_conv);
05114c74 915 if (sshpam_err != PAM_SUCCESS)
916 fatal("PAM: failed to set PAM_CONV: %s",
917 pam_strerror(sshpam_handle, sshpam_err));
918 if (init) {
919 debug("PAM: establishing credentials");
920 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
921 } else {
922 debug("PAM: reinitializing credentials");
923 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
924 }
925 if (sshpam_err == PAM_SUCCESS) {
926 sshpam_cred_established = 1;
927 return;
928 }
929 if (sshpam_authenticated)
930 fatal("PAM: pam_setcred(): %s",
931 pam_strerror(sshpam_handle, sshpam_err));
932 else
933 debug("PAM: pam_setcred(): %s",
934 pam_strerror(sshpam_handle, sshpam_err));
a5c9cd31 935}
936
05114c74 937static int
44171182 938sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
5b9e2464 939 struct pam_response **resp, void *data)
ee48c949 940{
05114c74 941 char input[PAM_MAX_MSG_SIZE];
0a23d79f 942 struct pam_response *reply;
ee48c949 943 int i;
944
60922169 945 debug3("PAM: %s called with %d messages", __func__, n);
946
0a23d79f 947 *resp = NULL;
948
3eaf3960 949 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
05114c74 950 return (PAM_CONV_ERR);
0a23d79f 951
01d35895 952 if ((reply = calloc(n, sizeof(*reply))) == NULL)
0a23d79f 953 return (PAM_CONV_ERR);
0a23d79f 954
05114c74 955 for (i = 0; i < n; ++i) {
956 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
957 case PAM_PROMPT_ECHO_OFF:
0a23d79f 958 reply[i].resp =
aff51935 959 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
05114c74 960 RP_ALLOW_STDIN);
0a23d79f 961 reply[i].resp_retcode = PAM_SUCCESS;
05114c74 962 break;
963 case PAM_PROMPT_ECHO_ON:
74117b26 964 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
05114c74 965 fgets(input, sizeof input, stdin);
8936b151 966 if ((reply[i].resp = strdup(input)) == NULL)
967 goto fail;
0a23d79f 968 reply[i].resp_retcode = PAM_SUCCESS;
05114c74 969 break;
970 case PAM_ERROR_MSG:
971 case PAM_TEXT_INFO:
74117b26 972 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
0a23d79f 973 reply[i].resp_retcode = PAM_SUCCESS;
05114c74 974 break;
975 default:
976 goto fail;
977 }
ee48c949 978 }
0a23d79f 979 *resp = reply;
05114c74 980 return (PAM_SUCCESS);
0a23d79f 981
05114c74 982 fail:
0a23d79f 983 for(i = 0; i < n; i++) {
984 if (reply[i].resp != NULL)
985 xfree(reply[i].resp);
986 }
987 xfree(reply);
05114c74 988 return (PAM_CONV_ERR);
ee48c949 989}
990
8600a4ab 991static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
3eaf3960 992
05114c74 993/*
994 * XXX this should be done in the authentication phase, but ssh1 doesn't
995 * support that
996 */
997void
998do_pam_chauthtok(void)
a5c9cd31 999{
05114c74 1000 if (use_privsep)
5b9e2464 1001 fatal("Password expired (unable to change with privsep)");
05114c74 1002 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
3eaf3960 1003 (const void *)&tty_conv);
05114c74 1004 if (sshpam_err != PAM_SUCCESS)
1005 fatal("PAM: failed to set PAM_CONV: %s",
1006 pam_strerror(sshpam_handle, sshpam_err));
1007 debug("PAM: changing password");
1008 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1009 if (sshpam_err != PAM_SUCCESS)
1010 fatal("PAM: pam_chauthtok(): %s",
1011 pam_strerror(sshpam_handle, sshpam_err));
5daf7064 1012}
1013
3eaf3960 1014void
1015do_pam_session(void)
1016{
dd1fb864 1017 debug3("PAM: opening session");
aff51935 1018 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
ef687c66 1019 (const void *)&store_conv);
3eaf3960 1020 if (sshpam_err != PAM_SUCCESS)
1021 fatal("PAM: failed to set PAM_CONV: %s",
1022 pam_strerror(sshpam_handle, sshpam_err));
1023 sshpam_err = pam_open_session(sshpam_handle, 0);
abdec250 1024 if (sshpam_err == PAM_SUCCESS)
1025 sshpam_session_open = 1;
1026 else {
1027 sshpam_session_open = 0;
1028 disable_forwarding();
1029 error("PAM: pam_open_session(): %s",
3eaf3960 1030 pam_strerror(sshpam_handle, sshpam_err));
abdec250 1031 }
1032
1033}
1034
1035int
1036is_pam_session_open(void)
1037{
1038 return sshpam_session_open;
3eaf3960 1039}
1040
aff51935 1041/*
749560dd 1042 * Set a PAM environment string. We need to do this so that the session
1043 * modules can handle things like Kerberos/GSI credentials that appear
1044 * during the ssh authentication process.
1045 */
749560dd 1046int
aff51935 1047do_pam_putenv(char *name, char *value)
749560dd 1048{
749560dd 1049 int ret = 1;
b6453d99 1050#ifdef HAVE_PAM_PUTENV
263c65df 1051 char *compound;
1052 size_t len;
1053
1054 len = strlen(name) + strlen(value) + 2;
1055 compound = xmalloc(len);
1056
1057 snprintf(compound, len, "%s=%s", name, value);
1058 ret = pam_putenv(sshpam_handle, compound);
1059 xfree(compound);
749560dd 1060#endif
263c65df 1061
749560dd 1062 return (ret);
1063}
1064
2212fc98 1065char **
1066fetch_pam_child_environment(void)
1067{
1068 return sshpam_env;
1069}
1070
05114c74 1071char **
1072fetch_pam_environment(void)
1073{
05114c74 1074 return (pam_getenvlist(sshpam_handle));
05114c74 1075}
5c377b3b 1076
05114c74 1077void
1078free_pam_environment(char **env)
1079{
1080 char **envp;
5daf7064 1081
0eb6370a 1082 if (env == NULL)
1083 return;
1084
05114c74 1085 for (envp = env; *envp; envp++)
1086 xfree(*envp);
1087 xfree(env);
a5c9cd31 1088}
1089
5de92f17 1090/*
1091 * "Blind" conversation function for password authentication. Assumes that
1092 * echo-off prompts are for the password and stores messages for later
1093 * display.
1094 */
1095static int
44171182 1096sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
5de92f17 1097 struct pam_response **resp, void *data)
1098{
1099 struct pam_response *reply;
1100 int i;
1101 size_t len;
1102
1103 debug3("PAM: %s called with %d messages", __func__, n);
1104
1105 *resp = NULL;
1106
1107 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1108 return (PAM_CONV_ERR);
1109
1110 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1111 return (PAM_CONV_ERR);
1112 memset(reply, 0, n * sizeof(*reply));
1113
1114 for (i = 0; i < n; ++i) {
1115 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1116 case PAM_PROMPT_ECHO_OFF:
1117 if (sshpam_password == NULL)
1118 goto fail;
8936b151 1119 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1120 goto fail;
5de92f17 1121 reply[i].resp_retcode = PAM_SUCCESS;
1122 break;
1123 case PAM_ERROR_MSG:
1124 case PAM_TEXT_INFO:
1125 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1126 if (len > 0) {
1127 buffer_append(&loginmsg,
1128 PAM_MSG_MEMBER(msg, i, msg), len);
1129 buffer_append(&loginmsg, "\n", 1);
1130 }
8936b151 1131 if ((reply[i].resp = strdup("")) == NULL)
1132 goto fail;
5de92f17 1133 reply[i].resp_retcode = PAM_SUCCESS;
1134 break;
1135 default:
1136 goto fail;
1137 }
1138 }
1139 *resp = reply;
1140 return (PAM_SUCCESS);
1141
d1cf9a87 1142 fail:
5de92f17 1143 for(i = 0; i < n; i++) {
1144 if (reply[i].resp != NULL)
1145 xfree(reply[i].resp);
1146 }
1147 xfree(reply);
1148 return (PAM_CONV_ERR);
1149}
1150
1151static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1152
1153/*
1154 * Attempt password authentication via PAM
1155 */
1156int
1157sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1158{
1159 int flags = (options.permit_empty_passwd == 0 ?
1160 PAM_DISALLOW_NULL_AUTHTOK : 0);
1161
1162 if (!options.use_pam || sshpam_handle == NULL)
1163 fatal("PAM: %s called when PAM disabled or failed to "
1164 "initialise.", __func__);
1165
1166 sshpam_password = password;
1167 sshpam_authctxt = authctxt;
1168
0e716148 1169 /*
1170 * If the user logging in is invalid, or is root but is not permitted
1171 * by PermitRootLogin, use an invalid password to prevent leaking
1172 * information via timing (eg if the PAM config has a delay on fail).
1173 */
2d5104e2 1174 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
ed9e8be3 1175 options.permit_root_login != PERMIT_YES))
0e716148 1176 sshpam_password = badpw;
1177
5de92f17 1178 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1179 (const void *)&passwd_conv);
1180 if (sshpam_err != PAM_SUCCESS)
1181 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1182 pam_strerror(sshpam_handle, sshpam_err));
1183
1184 sshpam_err = pam_authenticate(sshpam_handle, flags);
1185 sshpam_password = NULL;
2d5104e2 1186 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
5de92f17 1187 debug("PAM: password authentication accepted for %.100s",
1188 authctxt->user);
ed9e8be3 1189 return 1;
5de92f17 1190 } else {
1191 debug("PAM: password authentication failed for %.100s: %s",
1192 authctxt->valid ? authctxt->user : "an illegal user",
1193 pam_strerror(sshpam_handle, sshpam_err));
1194 return 0;
1195 }
1196}
a5c9cd31 1197#endif /* USE_PAM */
This page took 0.387553 seconds and 5 git commands to generate.