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