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