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 */
52 #include <sys/types.h>
63 #if defined(HAVE_SECURITY_PAM_APPL_H)
64 #include <security/pam_appl.h>
65 #elif defined (HAVE_PAM_PAM_APPL_H)
66 #include <pam/pam_appl.h>
69 #if !defined(SSHD_PAM_SERVICE)
70 extern char *__progname;
71 # define SSHD_PAM_SERVICE __progname
74 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
75 #ifdef PAM_SUN_CODEBASE
76 # define sshpam_const /* Solaris, HP-UX, SunOS */
78 # define sshpam_const const /* LinuxPAM, OpenPAM, AIX */
81 /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
82 #ifdef PAM_SUN_CODEBASE
83 # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
85 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
101 #include "auth-options.h"
105 #include "monitor_wrap.h"
106 #include "blacklist_client.h"
108 extern ServerOptions options;
109 extern Buffer loginmsg;
111 extern u_int utmp_len;
113 /* so we don't silently change behaviour */
114 #ifdef USE_POSIX_THREADS
115 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
119 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
120 * and generally a bad idea. Use at own risk and do not expect support if
123 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
126 * Avoid namespace clash when *not* using pthreads for systems *with*
127 * pthreads, which unconditionally define pthread_t via sys/types.h
130 typedef pthread_t sp_pthread_t;
132 typedef pid_t sp_pthread_t;
136 sp_pthread_t pam_thread;
142 static void sshpam_free_ctx(void *);
143 static struct pam_ctxt *cleanup_ctxt;
145 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
147 * Simulate threads with processes.
150 static int sshpam_thread_status = -1;
151 static mysig_t sshpam_oldsig;
154 sshpam_sigchld_handler(int sig)
156 signal(SIGCHLD, SIG_DFL);
157 if (cleanup_ctxt == NULL)
158 return; /* handler called after PAM cleanup, shouldn't happen */
159 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
161 /* PAM thread has not exitted, privsep slave must have */
162 kill(cleanup_ctxt->pam_thread, SIGTERM);
163 while (waitpid(cleanup_ctxt->pam_thread,
164 &sshpam_thread_status, 0) == -1) {
170 if (WIFSIGNALED(sshpam_thread_status) &&
171 WTERMSIG(sshpam_thread_status) == SIGTERM)
172 return; /* terminated by pthread_cancel */
173 if (!WIFEXITED(sshpam_thread_status))
174 sigdie("PAM: authentication thread exited unexpectedly");
175 if (WEXITSTATUS(sshpam_thread_status) != 0)
176 sigdie("PAM: authentication thread exited uncleanly");
181 pthread_exit(void *value)
188 pthread_create(sp_pthread_t *thread, const void *attr,
189 void *(*thread_start)(void *), void *arg)
192 struct pam_ctxt *ctx = arg;
194 sshpam_thread_status = -1;
195 switch ((pid = fork())) {
197 error("fork(): %s", strerror(errno));
200 close(ctx->pam_psock);
206 close(ctx->pam_csock);
208 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
214 pthread_cancel(sp_pthread_t thread)
216 signal(SIGCHLD, sshpam_oldsig);
217 return (kill(thread, SIGTERM));
222 pthread_join(sp_pthread_t thread, void **value)
226 if (sshpam_thread_status != -1)
227 return (sshpam_thread_status);
228 signal(SIGCHLD, sshpam_oldsig);
229 while (waitpid(thread, &status, 0) == -1) {
232 fatal("%s: waitpid: %s", __func__, strerror(errno));
239 static pam_handle_t *sshpam_handle = NULL;
240 static int sshpam_err = 0;
241 static int sshpam_authenticated = 0;
242 static int sshpam_session_open = 0;
243 static int sshpam_cred_established = 0;
244 static int sshpam_account_status = -1;
245 static int sshpam_maxtries_reached = 0;
246 static char **sshpam_env = NULL;
247 static Authctxt *sshpam_authctxt = NULL;
248 static const char *sshpam_password = NULL;
250 /* Some PAM implementations don't implement this */
251 #ifndef HAVE_PAM_GETENVLIST
253 pam_getenvlist(pam_handle_t *pamh)
256 * XXX - If necessary, we can still support envrionment passing
257 * for platforms without pam_getenvlist by searching for known
258 * env vars (e.g. KRB5CCNAME) from the PAM environment.
265 * Some platforms, notably Solaris, do not enforce password complexity
266 * rules during pam_chauthtok() if the real uid of the calling process
267 * is 0, on the assumption that it's being called by "passwd" run by root.
268 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
271 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
273 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
277 if (sshpam_authctxt == NULL)
278 fatal("PAM: sshpam_authctxt not initialized");
279 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
280 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
281 result = pam_chauthtok(pamh, flags);
282 if (setreuid(0, -1) == -1)
283 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
286 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
290 sshpam_password_change_required(int reqd)
292 debug3("%s %d", __func__, reqd);
293 if (sshpam_authctxt == NULL)
294 fatal("%s: PAM authctxt not initialized", __func__);
295 sshpam_authctxt->force_pwchange = reqd;
297 no_port_forwarding_flag |= 2;
298 no_agent_forwarding_flag |= 2;
299 no_x11_forwarding_flag |= 2;
301 no_port_forwarding_flag &= ~2;
302 no_agent_forwarding_flag &= ~2;
303 no_x11_forwarding_flag &= ~2;
307 /* Import regular and PAM environment from subprocess */
309 import_environments(Buffer *b)
315 debug3("PAM: %s entering", __func__);
317 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
318 /* Import variables set by do_pam_account */
319 sshpam_account_status = buffer_get_int(b);
320 sshpam_password_change_required(buffer_get_int(b));
322 /* Import environment from subprocess */
323 num_env = buffer_get_int(b);
325 fatal("%s: received %u environment variables, expected <= 1024",
327 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
328 debug3("PAM: num env strings %d", num_env);
329 for(i = 0; i < num_env; i++)
330 sshpam_env[i] = buffer_get_string(b, NULL);
332 sshpam_env[num_env] = NULL;
334 /* Import PAM environment from subprocess */
335 num_env = buffer_get_int(b);
336 debug("PAM: num PAM env strings %d", num_env);
337 for(i = 0; i < num_env; i++) {
338 env = buffer_get_string(b, NULL);
340 #ifdef HAVE_PAM_PUTENV
341 /* Errors are not fatal here */
342 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
343 error("PAM: pam_putenv: %s",
344 pam_strerror(sshpam_handle, sshpam_err));
352 * Conversation function for authentication thread.
355 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
356 struct pam_response **resp, void *data)
359 struct pam_ctxt *ctxt;
360 struct pam_response *reply;
363 debug3("PAM: %s entering, %d messages", __func__, n);
367 error("PAM: conversation function passed a null context");
368 return (PAM_CONV_ERR);
371 if (n <= 0 || n > PAM_MAX_NUM_MSG)
372 return (PAM_CONV_ERR);
374 if ((reply = calloc(n, sizeof(*reply))) == NULL)
375 return (PAM_CONV_ERR);
377 buffer_init(&buffer);
378 for (i = 0; i < n; ++i) {
379 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
380 case PAM_PROMPT_ECHO_OFF:
381 case PAM_PROMPT_ECHO_ON:
382 buffer_put_cstring(&buffer,
383 PAM_MSG_MEMBER(msg, i, msg));
384 if (ssh_msg_send(ctxt->pam_csock,
385 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
387 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
389 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
391 reply[i].resp = buffer_get_string(&buffer, NULL);
395 buffer_put_cstring(&buffer,
396 PAM_MSG_MEMBER(msg, i, msg));
397 if (ssh_msg_send(ctxt->pam_csock,
398 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
404 buffer_clear(&buffer);
406 buffer_free(&buffer);
408 return (PAM_SUCCESS);
411 for(i = 0; i < n; i++) {
415 buffer_free(&buffer);
416 return (PAM_CONV_ERR);
420 * Authentication thread.
423 sshpam_thread(void *ctxtp)
425 struct pam_ctxt *ctxt = ctxtp;
427 struct pam_conv sshpam_conv;
428 int flags = (options.permit_empty_passwd == 0 ?
429 PAM_DISALLOW_NULL_AUTHTOK : 0);
430 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
431 extern char **environ;
434 const char *pam_user;
435 const char **ptr_pam_user = &pam_user;
436 char *tz = getenv("TZ");
438 sshpam_err = pam_get_item(sshpam_handle, PAM_USER,
439 (sshpam_const void **)ptr_pam_user);
440 if (sshpam_err != PAM_SUCCESS)
445 if (setenv("TZ", tz, 1) == -1)
446 error("PAM: could not set TZ environment: %s",
449 if (sshpam_authctxt != NULL) {
450 setproctitle("%s [pam]",
451 sshpam_authctxt->valid ? pam_user : "unknown");
455 sshpam_conv.conv = sshpam_thread_conv;
456 sshpam_conv.appdata_ptr = ctxt;
458 if (sshpam_authctxt == NULL)
459 fatal("%s: PAM authctxt not initialized", __func__);
461 buffer_init(&buffer);
462 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
463 (const void *)&sshpam_conv);
464 if (sshpam_err != PAM_SUCCESS)
466 sshpam_err = pam_authenticate(sshpam_handle, flags);
467 if (sshpam_err == PAM_MAXTRIES)
468 sshpam_set_maxtries_reached(1);
469 if (sshpam_err != PAM_SUCCESS)
473 if (!do_pam_account()) {
474 sshpam_err = PAM_ACCT_EXPIRED;
477 if (sshpam_authctxt->force_pwchange) {
478 sshpam_err = pam_chauthtok(sshpam_handle,
479 PAM_CHANGE_EXPIRED_AUTHTOK);
480 if (sshpam_err != PAM_SUCCESS)
482 sshpam_password_change_required(0);
486 buffer_put_cstring(&buffer, "OK");
488 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
489 /* Export variables set by do_pam_account */
490 buffer_put_int(&buffer, sshpam_account_status);
491 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
493 /* Export any environment strings set in child */
494 for(i = 0; environ[i] != NULL; i++)
496 buffer_put_int(&buffer, i);
497 for(i = 0; environ[i] != NULL; i++)
498 buffer_put_cstring(&buffer, environ[i]);
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++)
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]);
507 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
509 /* XXX - can't do much about an error here */
510 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
511 buffer_free(&buffer);
515 buffer_put_cstring(&buffer,
516 pam_strerror(sshpam_handle, sshpam_err));
517 /* XXX - can't do much about an error here */
518 if (sshpam_err == PAM_ACCT_EXPIRED)
519 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
520 else if (sshpam_maxtries_reached)
521 ssh_msg_send(ctxt->pam_csock, PAM_MAXTRIES, &buffer);
523 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
524 buffer_free(&buffer);
527 return (NULL); /* Avoid warning for non-pthread case */
531 sshpam_thread_cleanup(void)
533 struct pam_ctxt *ctxt = cleanup_ctxt;
535 debug3("PAM: %s entering", __func__);
536 if (ctxt != NULL && ctxt->pam_thread != 0) {
537 pthread_cancel(ctxt->pam_thread);
538 pthread_join(ctxt->pam_thread, NULL);
539 close(ctxt->pam_psock);
540 close(ctxt->pam_csock);
541 memset(ctxt, 0, sizeof(*ctxt));
547 sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
548 struct pam_response **resp, void *data)
550 debug3("PAM: %s entering, %d messages", __func__, n);
551 return (PAM_CONV_ERR);
554 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
557 sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
558 struct pam_response **resp, void *data)
560 struct pam_response *reply;
564 debug3("PAM: %s called with %d messages", __func__, n);
567 if (n <= 0 || n > PAM_MAX_NUM_MSG)
568 return (PAM_CONV_ERR);
570 if ((reply = calloc(n, sizeof(*reply))) == NULL)
571 return (PAM_CONV_ERR);
573 for (i = 0; i < n; ++i) {
574 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
577 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
578 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
579 buffer_append(&loginmsg, "\n", 1 );
580 reply[i].resp_retcode = PAM_SUCCESS;
587 return (PAM_SUCCESS);
590 for(i = 0; i < n; i++) {
594 return (PAM_CONV_ERR);
597 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
602 if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
604 debug("PAM: cleanup");
605 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
606 if (sshpam_session_open) {
607 debug("PAM: closing session");
608 pam_close_session(sshpam_handle, PAM_SILENT);
609 sshpam_session_open = 0;
611 if (sshpam_cred_established) {
612 debug("PAM: deleting credentials");
613 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
614 sshpam_cred_established = 0;
616 sshpam_authenticated = 0;
617 pam_end(sshpam_handle, sshpam_err);
618 sshpam_handle = NULL;
622 sshpam_init(Authctxt *authctxt)
624 const char *pam_rhost, *pam_user, *user = authctxt->user;
625 const char **ptr_pam_user = &pam_user;
626 struct ssh *ssh = active_state; /* XXX */
628 if (sshpam_handle != NULL) {
629 /* We already have a PAM context; check if the user matches */
630 sshpam_err = pam_get_item(sshpam_handle,
631 PAM_USER, (sshpam_const void **)ptr_pam_user);
632 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
634 pam_end(sshpam_handle, sshpam_err);
635 sshpam_handle = NULL;
637 debug("PAM: initializing for \"%s\"", user);
639 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
640 sshpam_authctxt = authctxt;
642 if (sshpam_err != PAM_SUCCESS) {
643 pam_end(sshpam_handle, sshpam_err);
644 sshpam_handle = NULL;
647 pam_rhost = auth_get_canonical_hostname(ssh, options.use_dns);
648 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
649 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
650 if (sshpam_err != PAM_SUCCESS) {
651 pam_end(sshpam_handle, sshpam_err);
652 sshpam_handle = NULL;
655 #ifdef PAM_TTY_KLUDGE
657 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
658 * sshd doesn't set the tty until too late in the auth process and
659 * may not even set one (for tty-less connections)
661 debug("PAM: setting PAM_TTY to \"ssh\"");
662 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
663 if (sshpam_err != PAM_SUCCESS) {
664 pam_end(sshpam_handle, sshpam_err);
665 sshpam_handle = NULL;
673 sshpam_init_ctx(Authctxt *authctxt)
675 struct pam_ctxt *ctxt;
678 debug3("PAM: %s entering", __func__);
680 * Refuse to start if we don't have PAM enabled or do_pam_account
681 * has previously failed.
683 if (!options.use_pam || sshpam_account_status == 0)
687 if (sshpam_init(authctxt) == -1) {
688 error("PAM: initialization failed");
692 ctxt = xcalloc(1, sizeof *ctxt);
694 /* Start the authentication thread */
695 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
696 error("PAM: failed create sockets: %s", strerror(errno));
700 ctxt->pam_psock = socks[0];
701 ctxt->pam_csock = socks[1];
702 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
703 error("PAM: failed to start authentication thread: %s",
715 sshpam_query(void *ctx, char **name, char **info,
716 u_int *num, char ***prompts, u_int **echo_on)
718 struct ssh *ssh = active_state; /* XXX */
720 struct pam_ctxt *ctxt = ctx;
726 debug3("PAM: %s entering", __func__);
727 buffer_init(&buffer);
730 *prompts = xmalloc(sizeof(char *));
733 *echo_on = xmalloc(sizeof(u_int));
734 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
735 type = buffer_get_char(&buffer);
736 msg = buffer_get_string(&buffer, NULL);
739 case PAM_PROMPT_ECHO_ON:
740 case PAM_PROMPT_ECHO_OFF:
742 len = plen + mlen + 1;
743 **prompts = xreallocarray(**prompts, 1, len);
744 strlcpy(**prompts + plen, msg, len - plen);
746 **echo_on = (type == PAM_PROMPT_ECHO_ON);
751 /* accumulate messages */
752 len = plen + mlen + 2;
753 **prompts = xreallocarray(**prompts, 1, len);
754 strlcpy(**prompts + plen, msg, len - plen);
756 strlcat(**prompts + plen, "\n", len - plen);
760 case PAM_ACCT_EXPIRED:
762 if (type == PAM_ACCT_EXPIRED)
763 sshpam_account_status = 0;
764 if (type == PAM_MAXTRIES)
765 sshpam_set_maxtries_reached(1);
768 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
769 if (**prompts != NULL && strlen(**prompts) != 0) {
780 if (**prompts != NULL) {
781 /* drain any accumulated messages */
782 debug("PAM: %s", **prompts);
783 buffer_append(&loginmsg, **prompts,
788 if (type == PAM_SUCCESS) {
789 if (!sshpam_authctxt->valid ||
790 (sshpam_authctxt->pw->pw_uid == 0 &&
791 options.permit_root_login != PERMIT_YES))
792 fatal("Internal error: PAM auth "
793 "succeeded when it should have "
795 import_environments(&buffer);
802 BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
803 error("PAM: %s for %s%.100s from %.100s", msg,
804 sshpam_authctxt->valid ? "" : "illegal user ",
805 sshpam_authctxt->user,
806 auth_get_canonical_hostname(ssh, options.use_dns));
820 * Returns a junk password of identical length to that the user supplied.
821 * Used to mitigate timing attacks against crypt(3)/PAM stacks that
822 * vary processing time in proportion to password length.
825 fake_password(const char *wire_password)
827 const char junk[] = "\b\n\r\177INCORRECT";
829 size_t i, l = wire_password != NULL ? strlen(wire_password) : 0;
832 fatal("%s: password length too long: %zu", __func__, l);
835 for (i = 0; i < l; i++)
836 ret[i] = junk[i % (sizeof(junk) - 1)];
841 /* XXX - see also comment in auth-chall.c:verify_response */
843 sshpam_respond(void *ctx, u_int num, char **resp)
846 struct pam_ctxt *ctxt = ctx;
849 debug2("PAM: %s entering, %u responses", __func__, num);
850 switch (ctxt->pam_done) {
852 sshpam_authenticated = 1;
860 error("PAM: expected one response, got %u", num);
863 buffer_init(&buffer);
864 if (sshpam_authctxt->valid &&
865 (sshpam_authctxt->pw->pw_uid != 0 ||
866 options.permit_root_login == PERMIT_YES))
867 buffer_put_cstring(&buffer, *resp);
869 fake = fake_password(*resp);
870 buffer_put_cstring(&buffer, fake);
873 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
874 buffer_free(&buffer);
877 buffer_free(&buffer);
882 sshpam_free_ctx(void *ctxtp)
884 struct pam_ctxt *ctxt = ctxtp;
886 debug3("PAM: %s entering", __func__);
887 sshpam_thread_cleanup();
890 * We don't call sshpam_cleanup() here because we may need the PAM
891 * handle at a later stage, e.g. when setting up a session. It's
892 * still on the cleanup list, so pam_end() *will* be called before
893 * the server process terminates.
897 KbdintDevice sshpam_device = {
905 KbdintDevice mm_sshpam_device = {
914 * This replaces auth-pam.c
917 start_pam(Authctxt *authctxt)
919 if (!options.use_pam)
920 fatal("PAM: initialisation requested when UsePAM=no");
922 if (sshpam_init(authctxt) == -1)
923 fatal("PAM: initialisation failed");
935 debug("%s: called", __func__);
936 if (sshpam_account_status != -1)
937 return (sshpam_account_status);
939 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
940 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
941 pam_strerror(sshpam_handle, sshpam_err));
943 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
944 sshpam_account_status = 0;
945 return (sshpam_account_status);
948 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
949 sshpam_password_change_required(1);
951 sshpam_account_status = 1;
952 return (sshpam_account_status);
956 do_pam_setcred(int init)
958 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
959 (const void *)&store_conv);
960 if (sshpam_err != PAM_SUCCESS)
961 fatal("PAM: failed to set PAM_CONV: %s",
962 pam_strerror(sshpam_handle, sshpam_err));
964 debug("PAM: establishing credentials");
965 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
967 debug("PAM: reinitializing credentials");
968 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
970 if (sshpam_err == PAM_SUCCESS) {
971 sshpam_cred_established = 1;
974 if (sshpam_authenticated)
975 fatal("PAM: pam_setcred(): %s",
976 pam_strerror(sshpam_handle, sshpam_err));
978 debug("PAM: pam_setcred(): %s",
979 pam_strerror(sshpam_handle, sshpam_err));
983 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
984 struct pam_response **resp, void *data)
986 char input[PAM_MAX_MSG_SIZE];
987 struct pam_response *reply;
990 debug3("PAM: %s called with %d messages", __func__, n);
994 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
995 return (PAM_CONV_ERR);
997 if ((reply = calloc(n, sizeof(*reply))) == NULL)
998 return (PAM_CONV_ERR);
1000 for (i = 0; i < n; ++i) {
1001 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1002 case PAM_PROMPT_ECHO_OFF:
1004 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
1006 reply[i].resp_retcode = PAM_SUCCESS;
1008 case PAM_PROMPT_ECHO_ON:
1009 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1010 if (fgets(input, sizeof input, stdin) == NULL)
1012 if ((reply[i].resp = strdup(input)) == NULL)
1014 reply[i].resp_retcode = PAM_SUCCESS;
1018 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1019 reply[i].resp_retcode = PAM_SUCCESS;
1026 return (PAM_SUCCESS);
1029 for(i = 0; i < n; i++) {
1030 free(reply[i].resp);
1033 return (PAM_CONV_ERR);
1036 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
1039 * XXX this should be done in the authentication phase, but ssh1 doesn't
1043 do_pam_chauthtok(void)
1046 fatal("Password expired (unable to change with privsep)");
1047 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1048 (const void *)&tty_conv);
1049 if (sshpam_err != PAM_SUCCESS)
1050 fatal("PAM: failed to set PAM_CONV: %s",
1051 pam_strerror(sshpam_handle, sshpam_err));
1052 debug("PAM: changing password");
1053 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1054 if (sshpam_err != PAM_SUCCESS)
1055 fatal("PAM: pam_chauthtok(): %s",
1056 pam_strerror(sshpam_handle, sshpam_err));
1060 do_pam_session(void)
1062 debug3("PAM: opening session");
1063 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1064 (const void *)&store_conv);
1065 if (sshpam_err != PAM_SUCCESS)
1066 fatal("PAM: failed to set PAM_CONV: %s",
1067 pam_strerror(sshpam_handle, sshpam_err));
1068 sshpam_err = pam_open_session(sshpam_handle, 0);
1069 if (sshpam_err == PAM_SUCCESS)
1070 sshpam_session_open = 1;
1072 sshpam_session_open = 0;
1073 disable_forwarding();
1074 error("PAM: pam_open_session(): %s",
1075 pam_strerror(sshpam_handle, sshpam_err));
1081 is_pam_session_open(void)
1083 return sshpam_session_open;
1087 * Set a PAM environment string. We need to do this so that the session
1088 * modules can handle things like Kerberos/GSI credentials that appear
1089 * during the ssh authentication process.
1092 do_pam_putenv(char *name, char *value)
1095 #ifdef HAVE_PAM_PUTENV
1099 len = strlen(name) + strlen(value) + 2;
1100 compound = xmalloc(len);
1102 snprintf(compound, len, "%s=%s", name, value);
1103 ret = pam_putenv(sshpam_handle, compound);
1111 fetch_pam_child_environment(void)
1117 fetch_pam_environment(void)
1119 return (pam_getenvlist(sshpam_handle));
1123 free_pam_environment(char **env)
1130 for (envp = env; *envp; envp++)
1136 * "Blind" conversation function for password authentication. Assumes that
1137 * echo-off prompts are for the password and stores messages for later
1141 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1142 struct pam_response **resp, void *data)
1144 struct pam_response *reply;
1148 debug3("PAM: %s called with %d messages", __func__, n);
1152 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1153 return (PAM_CONV_ERR);
1155 if ((reply = calloc(n, sizeof(*reply))) == NULL)
1156 return (PAM_CONV_ERR);
1158 for (i = 0; i < n; ++i) {
1159 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1160 case PAM_PROMPT_ECHO_OFF:
1161 if (sshpam_password == NULL)
1163 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1165 reply[i].resp_retcode = PAM_SUCCESS;
1169 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1171 buffer_append(&loginmsg,
1172 PAM_MSG_MEMBER(msg, i, msg), len);
1173 buffer_append(&loginmsg, "\n", 1);
1175 if ((reply[i].resp = strdup("")) == NULL)
1177 reply[i].resp_retcode = PAM_SUCCESS;
1184 return (PAM_SUCCESS);
1187 for(i = 0; i < n; i++) {
1188 free(reply[i].resp);
1191 return (PAM_CONV_ERR);
1194 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1197 * Attempt password authentication via PAM
1200 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1202 int flags = (options.permit_empty_passwd == 0 ?
1203 PAM_DISALLOW_NULL_AUTHTOK : 0);
1206 if (!options.use_pam || sshpam_handle == NULL)
1207 fatal("PAM: %s called when PAM disabled or failed to "
1208 "initialise.", __func__);
1210 sshpam_password = password;
1211 sshpam_authctxt = authctxt;
1214 * If the user logging in is invalid, or is root but is not permitted
1215 * by PermitRootLogin, use an invalid password to prevent leaking
1216 * information via timing (eg if the PAM config has a delay on fail).
1218 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1219 options.permit_root_login != PERMIT_YES))
1220 sshpam_password = fake = fake_password(password);
1222 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1223 (const void *)&passwd_conv);
1224 if (sshpam_err != PAM_SUCCESS)
1225 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1226 pam_strerror(sshpam_handle, sshpam_err));
1228 sshpam_err = pam_authenticate(sshpam_handle, flags);
1229 sshpam_password = NULL;
1231 if (sshpam_err == PAM_MAXTRIES)
1232 sshpam_set_maxtries_reached(1);
1233 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1234 debug("PAM: password authentication accepted for %.100s",
1238 debug("PAM: password authentication failed for %.100s: %s",
1239 authctxt->valid ? authctxt->user : "an illegal user",
1240 pam_strerror(sshpam_handle, sshpam_err));
1246 sshpam_get_maxtries_reached(void)
1248 return sshpam_maxtries_reached;
1252 sshpam_set_maxtries_reached(int reached)
1254 if (reached == 0 || sshpam_maxtries_reached)
1256 sshpam_maxtries_reached = 1;
1257 options.password_authentication = 0;
1258 options.kbd_interactive_authentication = 0;
1259 options.challenge_response_authentication = 0;
1261 #endif /* USE_PAM */