2 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org>
33 * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au>
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.
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.
48 /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
51 #include <sys/types.h>
61 #if defined(HAVE_SECURITY_PAM_APPL_H)
62 #include <security/pam_appl.h>
63 #elif defined (HAVE_PAM_PAM_APPL_H)
64 #include <pam/pam_appl.h>
67 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
68 #ifdef PAM_SUN_CODEBASE
69 # define sshpam_const /* Solaris, HP-UX, AIX */
71 # define sshpam_const const /* LinuxPAM, OpenPAM */
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)
78 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
86 #include "monitor_wrap.h"
93 #include "auth-options.h"
95 extern ServerOptions options;
96 extern Buffer loginmsg;
98 extern u_int utmp_len;
100 /* so we don't silently change behaviour */
101 #ifdef USE_POSIX_THREADS
102 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
106 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
107 * and generally a bad idea. Use at own risk and do not expect support if
110 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
113 * Avoid namespace clash when *not* using pthreads for systems *with*
114 * pthreads, which unconditionally define pthread_t via sys/types.h
117 typedef pthread_t sp_pthread_t;
119 typedef pid_t sp_pthread_t;
123 sp_pthread_t pam_thread;
129 static void sshpam_free_ctx(void *);
130 static struct pam_ctxt *cleanup_ctxt;
132 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
134 * Simulate threads with processes.
137 static int sshpam_thread_status = -1;
138 static mysig_t sshpam_oldsig;
141 sshpam_sigchld_handler(int sig)
143 signal(SIGCHLD, SIG_DFL);
144 if (cleanup_ctxt == NULL)
145 return; /* handler called after PAM cleanup, shouldn't happen */
146 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
148 /* PAM thread has not exitted, privsep slave must have */
149 kill(cleanup_ctxt->pam_thread, SIGTERM);
150 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
152 return; /* could not wait */
154 if (WIFSIGNALED(sshpam_thread_status) &&
155 WTERMSIG(sshpam_thread_status) == SIGTERM)
156 return; /* terminated by pthread_cancel */
157 if (!WIFEXITED(sshpam_thread_status))
158 fatal("PAM: authentication thread exited unexpectedly");
159 if (WEXITSTATUS(sshpam_thread_status) != 0)
160 fatal("PAM: authentication thread exited uncleanly");
165 pthread_exit(void *value)
172 pthread_create(sp_pthread_t *thread, const void *attr,
173 void *(*thread_start)(void *), void *arg)
176 struct pam_ctxt *ctx = arg;
178 sshpam_thread_status = -1;
179 switch ((pid = fork())) {
181 error("fork(): %s", strerror(errno));
184 close(ctx->pam_psock);
190 close(ctx->pam_csock);
192 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
198 pthread_cancel(sp_pthread_t thread)
200 signal(SIGCHLD, sshpam_oldsig);
201 return (kill(thread, SIGTERM));
206 pthread_join(sp_pthread_t thread, void **value)
210 if (sshpam_thread_status != -1)
211 return (sshpam_thread_status);
212 signal(SIGCHLD, sshpam_oldsig);
213 waitpid(thread, &status, 0);
219 static pam_handle_t *sshpam_handle = NULL;
220 static int sshpam_err = 0;
221 static int sshpam_authenticated = 0;
222 static int sshpam_session_open = 0;
223 static int sshpam_cred_established = 0;
224 static int sshpam_account_status = -1;
225 static char **sshpam_env = NULL;
226 static Authctxt *sshpam_authctxt = NULL;
227 static const char *sshpam_password = NULL;
228 static char badpw[] = "\b\n\r\177INCORRECT";
230 /* Some PAM implementations don't implement this */
231 #ifndef HAVE_PAM_GETENVLIST
233 pam_getenvlist(pam_handle_t *pamh)
236 * XXX - If necessary, we can still support envrionment passing
237 * for platforms without pam_getenvlist by searching for known
238 * env vars (e.g. KRB5CCNAME) from the PAM environment.
245 * Some platforms, notably Solaris, do not enforce password complexity
246 * rules during pam_chauthtok() if the real uid of the calling process
247 * is 0, on the assumption that it's being called by "passwd" run by root.
248 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
251 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
253 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
257 if (sshpam_authctxt == NULL)
258 fatal("PAM: sshpam_authctxt not initialized");
259 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
260 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
261 result = pam_chauthtok(pamh, flags);
262 if (setreuid(0, -1) == -1)
263 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
266 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
270 sshpam_password_change_required(int reqd)
272 debug3("%s %d", __func__, reqd);
273 if (sshpam_authctxt == NULL)
274 fatal("%s: PAM authctxt not initialized", __func__);
275 sshpam_authctxt->force_pwchange = reqd;
277 no_port_forwarding_flag |= 2;
278 no_agent_forwarding_flag |= 2;
279 no_x11_forwarding_flag |= 2;
281 no_port_forwarding_flag &= ~2;
282 no_agent_forwarding_flag &= ~2;
283 no_x11_forwarding_flag &= ~2;
287 /* Import regular and PAM environment from subprocess */
289 import_environments(Buffer *b)
295 debug3("PAM: %s entering", __func__);
297 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
298 /* Import variables set by do_pam_account */
299 sshpam_account_status = buffer_get_int(b);
300 sshpam_password_change_required(buffer_get_int(b));
302 /* Import environment from subprocess */
303 num_env = buffer_get_int(b);
305 fatal("%s: received %u environment variables, expected <= 1024",
307 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
308 debug3("PAM: num env strings %d", num_env);
309 for(i = 0; i < num_env; i++)
310 sshpam_env[i] = buffer_get_string(b, NULL);
312 sshpam_env[num_env] = NULL;
314 /* Import PAM environment from subprocess */
315 num_env = buffer_get_int(b);
316 debug("PAM: num PAM env strings %d", num_env);
317 for(i = 0; i < num_env; i++) {
318 env = buffer_get_string(b, NULL);
320 #ifdef HAVE_PAM_PUTENV
321 /* Errors are not fatal here */
322 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
323 error("PAM: pam_putenv: %s",
324 pam_strerror(sshpam_handle, sshpam_err));
332 * Conversation function for authentication thread.
335 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
336 struct pam_response **resp, void *data)
339 struct pam_ctxt *ctxt;
340 struct pam_response *reply;
343 debug3("PAM: %s entering, %d messages", __func__, n);
347 error("PAM: conversation function passed a null context");
348 return (PAM_CONV_ERR);
351 if (n <= 0 || n > PAM_MAX_NUM_MSG)
352 return (PAM_CONV_ERR);
354 if ((reply = calloc(n, sizeof(*reply))) == NULL)
355 return (PAM_CONV_ERR);
357 buffer_init(&buffer);
358 for (i = 0; i < n; ++i) {
359 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
360 case PAM_PROMPT_ECHO_OFF:
361 buffer_put_cstring(&buffer,
362 PAM_MSG_MEMBER(msg, i, msg));
363 if (ssh_msg_send(ctxt->pam_csock,
364 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
366 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
368 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
370 reply[i].resp = buffer_get_string(&buffer, NULL);
372 case PAM_PROMPT_ECHO_ON:
373 buffer_put_cstring(&buffer,
374 PAM_MSG_MEMBER(msg, i, msg));
375 if (ssh_msg_send(ctxt->pam_csock,
376 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
378 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
380 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
382 reply[i].resp = buffer_get_string(&buffer, NULL);
385 buffer_put_cstring(&buffer,
386 PAM_MSG_MEMBER(msg, i, msg));
387 if (ssh_msg_send(ctxt->pam_csock,
388 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
392 buffer_put_cstring(&buffer,
393 PAM_MSG_MEMBER(msg, i, msg));
394 if (ssh_msg_send(ctxt->pam_csock,
395 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
401 buffer_clear(&buffer);
403 buffer_free(&buffer);
405 return (PAM_SUCCESS);
408 for(i = 0; i < n; i++) {
409 if (reply[i].resp != NULL)
410 xfree(reply[i].resp);
413 buffer_free(&buffer);
414 return (PAM_CONV_ERR);
418 * Authentication thread.
421 sshpam_thread(void *ctxtp)
423 struct pam_ctxt *ctxt = ctxtp;
425 struct pam_conv sshpam_conv;
426 int flags = (options.permit_empty_passwd == 0 ?
427 PAM_DISALLOW_NULL_AUTHTOK : 0);
428 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
429 extern char **environ;
432 const char *pam_user;
433 const char **ptr_pam_user = &pam_user;
435 pam_get_item(sshpam_handle, PAM_USER,
436 (sshpam_const void **)ptr_pam_user);
439 if (sshpam_authctxt != NULL) {
440 setproctitle("%s [pam]",
441 sshpam_authctxt->valid ? pam_user : "unknown");
445 sshpam_conv.conv = sshpam_thread_conv;
446 sshpam_conv.appdata_ptr = ctxt;
448 if (sshpam_authctxt == NULL)
449 fatal("%s: PAM authctxt not initialized", __func__);
451 buffer_init(&buffer);
452 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
453 (const void *)&sshpam_conv);
454 if (sshpam_err != PAM_SUCCESS)
456 sshpam_err = pam_authenticate(sshpam_handle, flags);
457 if (sshpam_err != PAM_SUCCESS)
461 if (!do_pam_account()) {
462 sshpam_err = PAM_ACCT_EXPIRED;
465 if (sshpam_authctxt->force_pwchange) {
466 sshpam_err = pam_chauthtok(sshpam_handle,
467 PAM_CHANGE_EXPIRED_AUTHTOK);
468 if (sshpam_err != PAM_SUCCESS)
470 sshpam_password_change_required(0);
474 buffer_put_cstring(&buffer, "OK");
476 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
477 /* Export variables set by do_pam_account */
478 buffer_put_int(&buffer, sshpam_account_status);
479 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
481 /* Export any environment strings set in child */
482 for(i = 0; environ[i] != NULL; i++)
484 buffer_put_int(&buffer, i);
485 for(i = 0; environ[i] != NULL; i++)
486 buffer_put_cstring(&buffer, environ[i]);
488 /* Export any environment strings set by PAM in child */
489 env_from_pam = pam_getenvlist(sshpam_handle);
490 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
492 buffer_put_int(&buffer, i);
493 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
494 buffer_put_cstring(&buffer, env_from_pam[i]);
495 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
497 /* XXX - can't do much about an error here */
498 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
499 buffer_free(&buffer);
503 buffer_put_cstring(&buffer,
504 pam_strerror(sshpam_handle, sshpam_err));
505 /* XXX - can't do much about an error here */
506 if (sshpam_err == PAM_ACCT_EXPIRED)
507 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
509 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
510 buffer_free(&buffer);
513 return (NULL); /* Avoid warning for non-pthread case */
517 sshpam_thread_cleanup(void)
519 struct pam_ctxt *ctxt = cleanup_ctxt;
521 debug3("PAM: %s entering", __func__);
522 if (ctxt != NULL && ctxt->pam_thread != 0) {
523 pthread_cancel(ctxt->pam_thread);
524 pthread_join(ctxt->pam_thread, NULL);
525 close(ctxt->pam_psock);
526 close(ctxt->pam_csock);
527 memset(ctxt, 0, sizeof(*ctxt));
533 sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
534 struct pam_response **resp, void *data)
536 debug3("PAM: %s entering, %d messages", __func__, n);
537 return (PAM_CONV_ERR);
540 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
543 sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
544 struct pam_response **resp, void *data)
546 struct pam_response *reply;
550 debug3("PAM: %s called with %d messages", __func__, n);
553 if (n <= 0 || n > PAM_MAX_NUM_MSG)
554 return (PAM_CONV_ERR);
556 if ((reply = calloc(n, sizeof(*reply))) == NULL)
557 return (PAM_CONV_ERR);
559 for (i = 0; i < n; ++i) {
560 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
563 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
564 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
565 buffer_append(&loginmsg, "\n", 1 );
566 reply[i].resp_retcode = PAM_SUCCESS;
573 return (PAM_SUCCESS);
576 for(i = 0; i < n; i++) {
577 if (reply[i].resp != NULL)
578 xfree(reply[i].resp);
581 return (PAM_CONV_ERR);
584 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
589 debug("PAM: cleanup");
590 if (sshpam_handle == NULL)
592 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
593 if (sshpam_cred_established) {
594 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
595 sshpam_cred_established = 0;
597 if (sshpam_session_open) {
598 pam_close_session(sshpam_handle, PAM_SILENT);
599 sshpam_session_open = 0;
601 sshpam_authenticated = 0;
602 pam_end(sshpam_handle, sshpam_err);
603 sshpam_handle = NULL;
607 sshpam_init(Authctxt *authctxt)
609 extern char *__progname;
610 const char *pam_rhost, *pam_user, *user = authctxt->user;
611 const char **ptr_pam_user = &pam_user;
613 if (sshpam_handle != NULL) {
614 /* We already have a PAM context; check if the user matches */
615 sshpam_err = pam_get_item(sshpam_handle,
616 PAM_USER, (sshpam_const void **)ptr_pam_user);
617 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
619 pam_end(sshpam_handle, sshpam_err);
620 sshpam_handle = NULL;
622 debug("PAM: initializing for \"%s\"", user);
624 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
625 sshpam_authctxt = authctxt;
627 if (sshpam_err != PAM_SUCCESS) {
628 pam_end(sshpam_handle, sshpam_err);
629 sshpam_handle = NULL;
632 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
633 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
634 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
635 if (sshpam_err != PAM_SUCCESS) {
636 pam_end(sshpam_handle, sshpam_err);
637 sshpam_handle = NULL;
640 #ifdef PAM_TTY_KLUDGE
642 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
643 * sshd doesn't set the tty until too late in the auth process and
644 * may not even set one (for tty-less connections)
646 debug("PAM: setting PAM_TTY to \"ssh\"");
647 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
648 if (sshpam_err != PAM_SUCCESS) {
649 pam_end(sshpam_handle, sshpam_err);
650 sshpam_handle = NULL;
658 sshpam_init_ctx(Authctxt *authctxt)
660 struct pam_ctxt *ctxt;
663 debug3("PAM: %s entering", __func__);
665 * Refuse to start if we don't have PAM enabled or do_pam_account
666 * has previously failed.
668 if (!options.use_pam || sshpam_account_status == 0)
672 if (sshpam_init(authctxt) == -1) {
673 error("PAM: initialization failed");
677 ctxt = xmalloc(sizeof *ctxt);
678 memset(ctxt, 0, sizeof(*ctxt));
680 /* Start the authentication thread */
681 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
682 error("PAM: failed create sockets: %s", strerror(errno));
686 ctxt->pam_psock = socks[0];
687 ctxt->pam_csock = socks[1];
688 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
689 error("PAM: failed to start authentication thread: %s",
701 sshpam_query(void *ctx, char **name, char **info,
702 u_int *num, char ***prompts, u_int **echo_on)
705 struct pam_ctxt *ctxt = ctx;
711 debug3("PAM: %s entering", __func__);
712 buffer_init(&buffer);
715 *prompts = xmalloc(sizeof(char *));
718 *echo_on = xmalloc(sizeof(u_int));
719 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
720 type = buffer_get_char(&buffer);
721 msg = buffer_get_string(&buffer, NULL);
724 case PAM_PROMPT_ECHO_ON:
725 case PAM_PROMPT_ECHO_OFF:
727 len = plen + mlen + 1;
728 **prompts = xrealloc(**prompts, 1, len);
729 strlcpy(**prompts + plen, msg, len - plen);
731 **echo_on = (type == PAM_PROMPT_ECHO_ON);
736 /* accumulate messages */
737 len = plen + mlen + 2;
738 **prompts = xrealloc(**prompts, 1, len);
739 strlcpy(**prompts + plen, msg, len - plen);
741 strlcat(**prompts + plen, "\n", len - plen);
745 case PAM_ACCT_EXPIRED:
746 sshpam_account_status = 0;
749 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
750 if (**prompts != NULL && strlen(**prompts) != 0) {
761 if (**prompts != NULL) {
762 /* drain any accumulated messages */
763 debug("PAM: %s", **prompts);
764 buffer_append(&loginmsg, **prompts,
769 if (type == PAM_SUCCESS) {
770 if (!sshpam_authctxt->valid ||
771 (sshpam_authctxt->pw->pw_uid == 0 &&
772 options.permit_root_login != PERMIT_YES))
773 fatal("Internal error: PAM auth "
774 "succeeded when it should have "
776 import_environments(&buffer);
783 error("PAM: %s for %s%.100s from %.100s", msg,
784 sshpam_authctxt->valid ? "" : "illegal user ",
785 sshpam_authctxt->user,
786 get_remote_name_or_ip(utmp_len, options.use_dns));
799 /* XXX - see also comment in auth-chall.c:verify_response */
801 sshpam_respond(void *ctx, u_int num, char **resp)
804 struct pam_ctxt *ctxt = ctx;
806 debug2("PAM: %s entering, %u responses", __func__, num);
807 switch (ctxt->pam_done) {
809 sshpam_authenticated = 1;
817 error("PAM: expected one response, got %u", num);
820 buffer_init(&buffer);
821 if (sshpam_authctxt->valid &&
822 (sshpam_authctxt->pw->pw_uid != 0 ||
823 options.permit_root_login == PERMIT_YES))
824 buffer_put_cstring(&buffer, *resp);
826 buffer_put_cstring(&buffer, badpw);
827 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
828 buffer_free(&buffer);
831 buffer_free(&buffer);
836 sshpam_free_ctx(void *ctxtp)
838 struct pam_ctxt *ctxt = ctxtp;
840 debug3("PAM: %s entering", __func__);
841 sshpam_thread_cleanup();
844 * We don't call sshpam_cleanup() here because we may need the PAM
845 * handle at a later stage, e.g. when setting up a session. It's
846 * still on the cleanup list, so pam_end() *will* be called before
847 * the server process terminates.
851 KbdintDevice sshpam_device = {
859 KbdintDevice mm_sshpam_device = {
868 * This replaces auth-pam.c
871 start_pam(Authctxt *authctxt)
873 if (!options.use_pam)
874 fatal("PAM: initialisation requested when UsePAM=no");
876 if (sshpam_init(authctxt) == -1)
877 fatal("PAM: initialisation failed");
889 debug("%s: called", __func__);
890 if (sshpam_account_status != -1)
891 return (sshpam_account_status);
893 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
894 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
895 pam_strerror(sshpam_handle, sshpam_err));
897 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
898 sshpam_account_status = 0;
899 return (sshpam_account_status);
902 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
903 sshpam_password_change_required(1);
905 sshpam_account_status = 1;
906 return (sshpam_account_status);
910 do_pam_set_tty(const char *tty)
913 debug("PAM: setting PAM_TTY to \"%s\"", tty);
914 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
915 if (sshpam_err != PAM_SUCCESS)
916 fatal("PAM: failed to set PAM_TTY: %s",
917 pam_strerror(sshpam_handle, sshpam_err));
922 do_pam_setcred(int init)
924 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
925 (const void *)&store_conv);
926 if (sshpam_err != PAM_SUCCESS)
927 fatal("PAM: failed to set PAM_CONV: %s",
928 pam_strerror(sshpam_handle, sshpam_err));
930 debug("PAM: establishing credentials");
931 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
933 debug("PAM: reinitializing credentials");
934 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
936 if (sshpam_err == PAM_SUCCESS) {
937 sshpam_cred_established = 1;
940 if (sshpam_authenticated)
941 fatal("PAM: pam_setcred(): %s",
942 pam_strerror(sshpam_handle, sshpam_err));
944 debug("PAM: pam_setcred(): %s",
945 pam_strerror(sshpam_handle, sshpam_err));
949 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
950 struct pam_response **resp, void *data)
952 char input[PAM_MAX_MSG_SIZE];
953 struct pam_response *reply;
956 debug3("PAM: %s called with %d messages", __func__, n);
960 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
961 return (PAM_CONV_ERR);
963 if ((reply = calloc(n, sizeof(*reply))) == NULL)
964 return (PAM_CONV_ERR);
966 for (i = 0; i < n; ++i) {
967 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
968 case PAM_PROMPT_ECHO_OFF:
970 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
972 reply[i].resp_retcode = PAM_SUCCESS;
974 case PAM_PROMPT_ECHO_ON:
975 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
976 fgets(input, sizeof input, stdin);
977 if ((reply[i].resp = strdup(input)) == NULL)
979 reply[i].resp_retcode = PAM_SUCCESS;
983 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
984 reply[i].resp_retcode = PAM_SUCCESS;
991 return (PAM_SUCCESS);
994 for(i = 0; i < n; i++) {
995 if (reply[i].resp != NULL)
996 xfree(reply[i].resp);
999 return (PAM_CONV_ERR);
1002 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
1005 * XXX this should be done in the authentication phase, but ssh1 doesn't
1009 do_pam_chauthtok(void)
1012 fatal("Password expired (unable to change with privsep)");
1013 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1014 (const void *)&tty_conv);
1015 if (sshpam_err != PAM_SUCCESS)
1016 fatal("PAM: failed to set PAM_CONV: %s",
1017 pam_strerror(sshpam_handle, sshpam_err));
1018 debug("PAM: changing password");
1019 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1020 if (sshpam_err != PAM_SUCCESS)
1021 fatal("PAM: pam_chauthtok(): %s",
1022 pam_strerror(sshpam_handle, sshpam_err));
1026 do_pam_session(void)
1028 debug3("PAM: opening session");
1029 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1030 (const void *)&store_conv);
1031 if (sshpam_err != PAM_SUCCESS)
1032 fatal("PAM: failed to set PAM_CONV: %s",
1033 pam_strerror(sshpam_handle, sshpam_err));
1034 sshpam_err = pam_open_session(sshpam_handle, 0);
1035 if (sshpam_err == PAM_SUCCESS)
1036 sshpam_session_open = 1;
1038 sshpam_session_open = 0;
1039 disable_forwarding();
1040 error("PAM: pam_open_session(): %s",
1041 pam_strerror(sshpam_handle, sshpam_err));
1047 is_pam_session_open(void)
1049 return sshpam_session_open;
1053 * Set a PAM environment string. We need to do this so that the session
1054 * modules can handle things like Kerberos/GSI credentials that appear
1055 * during the ssh authentication process.
1058 do_pam_putenv(char *name, char *value)
1061 #ifdef HAVE_PAM_PUTENV
1065 len = strlen(name) + strlen(value) + 2;
1066 compound = xmalloc(len);
1068 snprintf(compound, len, "%s=%s", name, value);
1069 ret = pam_putenv(sshpam_handle, compound);
1077 fetch_pam_child_environment(void)
1083 fetch_pam_environment(void)
1085 return (pam_getenvlist(sshpam_handle));
1089 free_pam_environment(char **env)
1096 for (envp = env; *envp; envp++)
1102 * "Blind" conversation function for password authentication. Assumes that
1103 * echo-off prompts are for the password and stores messages for later
1107 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1108 struct pam_response **resp, void *data)
1110 struct pam_response *reply;
1114 debug3("PAM: %s called with %d messages", __func__, n);
1118 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1119 return (PAM_CONV_ERR);
1121 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1122 return (PAM_CONV_ERR);
1123 memset(reply, 0, n * sizeof(*reply));
1125 for (i = 0; i < n; ++i) {
1126 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1127 case PAM_PROMPT_ECHO_OFF:
1128 if (sshpam_password == NULL)
1130 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1132 reply[i].resp_retcode = PAM_SUCCESS;
1136 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1138 buffer_append(&loginmsg,
1139 PAM_MSG_MEMBER(msg, i, msg), len);
1140 buffer_append(&loginmsg, "\n", 1);
1142 if ((reply[i].resp = strdup("")) == NULL)
1144 reply[i].resp_retcode = PAM_SUCCESS;
1151 return (PAM_SUCCESS);
1154 for(i = 0; i < n; i++) {
1155 if (reply[i].resp != NULL)
1156 xfree(reply[i].resp);
1159 return (PAM_CONV_ERR);
1162 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1165 * Attempt password authentication via PAM
1168 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1170 int flags = (options.permit_empty_passwd == 0 ?
1171 PAM_DISALLOW_NULL_AUTHTOK : 0);
1173 if (!options.use_pam || sshpam_handle == NULL)
1174 fatal("PAM: %s called when PAM disabled or failed to "
1175 "initialise.", __func__);
1177 sshpam_password = password;
1178 sshpam_authctxt = authctxt;
1181 * If the user logging in is invalid, or is root but is not permitted
1182 * by PermitRootLogin, use an invalid password to prevent leaking
1183 * information via timing (eg if the PAM config has a delay on fail).
1185 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1186 options.permit_root_login != PERMIT_YES))
1187 sshpam_password = badpw;
1189 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1190 (const void *)&passwd_conv);
1191 if (sshpam_err != PAM_SUCCESS)
1192 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1193 pam_strerror(sshpam_handle, sshpam_err));
1195 sshpam_err = pam_authenticate(sshpam_handle, flags);
1196 sshpam_password = NULL;
1197 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1198 debug("PAM: password authentication accepted for %.100s",
1202 debug("PAM: password authentication failed for %.100s: %s",
1203 authctxt->valid ? authctxt->user : "an illegal user",
1204 pam_strerror(sshpam_handle, sshpam_err));
1208 #endif /* USE_PAM */