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