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,2006 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>
62 #if defined(HAVE_SECURITY_PAM_APPL_H)
63 #include <security/pam_appl.h>
64 #elif defined (HAVE_PAM_PAM_APPL_H)
65 #include <pam/pam_appl.h>
68 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
69 #ifdef PAM_SUN_CODEBASE
70 # define sshpam_const /* Solaris, HP-UX, AIX */
72 # define sshpam_const const /* LinuxPAM, OpenPAM */
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)
79 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
95 #include "auth-options.h"
99 #include "monitor_wrap.h"
101 extern ServerOptions options;
102 extern Buffer loginmsg;
104 extern u_int utmp_len;
106 /* so we don't silently change behaviour */
107 #ifdef USE_POSIX_THREADS
108 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
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
116 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
119 * Avoid namespace clash when *not* using pthreads for systems *with*
120 * pthreads, which unconditionally define pthread_t via sys/types.h
123 typedef pthread_t sp_pthread_t;
125 #define pthread_create openssh_pthread_create
126 #define pthread_exit openssh_pthread_exit
127 #define pthread_cancel openssh_pthread_cancel
128 #define pthread_join openssh_pthread_join
129 typedef pid_t sp_pthread_t;
133 sp_pthread_t pam_thread;
139 static void sshpam_free_ctx(void *);
140 static struct pam_ctxt *cleanup_ctxt;
142 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
144 * Simulate threads with processes.
147 static int sshpam_thread_status = -1;
148 static mysig_t sshpam_oldsig;
151 sshpam_sigchld_handler(int sig)
153 signal(SIGCHLD, SIG_DFL);
154 if (cleanup_ctxt == NULL)
155 return; /* handler called after PAM cleanup, shouldn't happen */
156 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
158 /* PAM thread has not exitted, privsep slave must have */
159 kill(cleanup_ctxt->pam_thread, SIGTERM);
160 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
162 return; /* could not wait */
164 if (WIFSIGNALED(sshpam_thread_status) &&
165 WTERMSIG(sshpam_thread_status) == SIGTERM)
166 return; /* terminated by pthread_cancel */
167 if (!WIFEXITED(sshpam_thread_status))
168 sigdie("PAM: authentication thread exited unexpectedly");
169 if (WEXITSTATUS(sshpam_thread_status) != 0)
170 sigdie("PAM: authentication thread exited uncleanly");
175 pthread_exit(void *value)
182 pthread_create(sp_pthread_t *thread, const void *attr,
183 void *(*thread_start)(void *), void *arg)
186 struct pam_ctxt *ctx = arg;
188 sshpam_thread_status = -1;
189 switch ((pid = fork())) {
191 error("fork(): %s", strerror(errno));
194 close(ctx->pam_psock);
200 close(ctx->pam_csock);
202 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
208 pthread_cancel(sp_pthread_t thread)
210 signal(SIGCHLD, sshpam_oldsig);
211 return (kill(thread, SIGTERM));
216 pthread_join(sp_pthread_t thread, void **value)
220 if (sshpam_thread_status != -1)
221 return (sshpam_thread_status);
222 signal(SIGCHLD, sshpam_oldsig);
223 waitpid(thread, &status, 0);
229 static pam_handle_t *sshpam_handle = NULL;
230 static int sshpam_err = 0;
231 static int sshpam_authenticated = 0;
232 static int sshpam_session_open = 0;
233 static int sshpam_cred_established = 0;
234 static int sshpam_account_status = -1;
235 static char **sshpam_env = NULL;
236 static Authctxt *sshpam_authctxt = NULL;
237 static const char *sshpam_password = NULL;
238 static char badpw[] = "\b\n\r\177INCORRECT";
240 /* Some PAM implementations don't implement this */
241 #ifndef HAVE_PAM_GETENVLIST
243 pam_getenvlist(pam_handle_t *pamh)
246 * XXX - If necessary, we can still support envrionment passing
247 * for platforms without pam_getenvlist by searching for known
248 * env vars (e.g. KRB5CCNAME) from the PAM environment.
255 * Some platforms, notably Solaris, do not enforce password complexity
256 * rules during pam_chauthtok() if the real uid of the calling process
257 * is 0, on the assumption that it's being called by "passwd" run by root.
258 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
261 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
263 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
267 if (sshpam_authctxt == NULL)
268 fatal("PAM: sshpam_authctxt not initialized");
269 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
270 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
271 result = pam_chauthtok(pamh, flags);
272 if (setreuid(0, -1) == -1)
273 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
276 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
280 sshpam_getpw(const char *user)
284 if ((pw = getpwnam(user)) != NULL)
287 debug("PAM: faking passwd struct for user '%.100s'", user);
288 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
290 pw->pw_name = xstrdup(user); /* XXX leak */
291 pw->pw_shell = "/bin/true";
292 pw->pw_gecos = "sshd fake PAM user";
297 sshpam_check_userchanged(void)
303 debug("sshpam_check_userchanged");
304 sshpam_err = pam_get_item(sshpam_handle, PAM_USER, &user);
305 if (sshpam_err != PAM_SUCCESS)
306 fatal("PAM: could not get PAM_USER: %s",
307 pam_strerror(sshpam_handle, sshpam_err));
308 if (strcmp(user, sshpam_authctxt->pw->pw_name) != 0) {
309 debug("PAM: user mapped from '%.100s' to '%.100s'",
310 sshpam_authctxt->pw->pw_name, user);
311 if ((pw = getpwnam(user)) == NULL)
312 fatal("PAM: could not get passwd entry for user "
313 "'%.100s' provided by PAM_USER", user);
314 pwfree(sshpam_authctxt->pw);
315 sshpam_authctxt->pw = pw;
316 sshpam_authctxt->valid = allowed_user(pw);
317 debug("PAM: user '%.100s' now %svalid", user,
318 sshpam_authctxt->valid ? "" : "in");
323 sshpam_password_change_required(int reqd)
325 debug3("%s %d", __func__, reqd);
326 if (sshpam_authctxt == NULL)
327 fatal("%s: PAM authctxt not initialized", __func__);
328 sshpam_authctxt->force_pwchange = reqd;
330 no_port_forwarding_flag |= 2;
331 no_agent_forwarding_flag |= 2;
332 no_x11_forwarding_flag |= 2;
334 no_port_forwarding_flag &= ~2;
335 no_agent_forwarding_flag &= ~2;
336 no_x11_forwarding_flag &= ~2;
340 /* Import regular and PAM environment from subprocess */
342 import_environments(Buffer *b)
348 debug3("PAM: %s entering", __func__);
350 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
351 /* Import variables set by do_pam_account */
352 sshpam_account_status = buffer_get_int(b);
353 sshpam_password_change_required(buffer_get_int(b));
355 /* Import environment from subprocess */
356 num_env = buffer_get_int(b);
358 fatal("%s: received %u environment variables, expected <= 1024",
360 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
361 debug3("PAM: num env strings %d", num_env);
362 for(i = 0; i < num_env; i++)
363 sshpam_env[i] = buffer_get_string(b, NULL);
365 sshpam_env[num_env] = NULL;
367 /* Import PAM environment from subprocess */
368 num_env = buffer_get_int(b);
369 debug("PAM: num PAM env strings %d", num_env);
370 for(i = 0; i < num_env; i++) {
371 env = buffer_get_string(b, NULL);
373 #ifdef HAVE_PAM_PUTENV
374 /* Errors are not fatal here */
375 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
376 error("PAM: pam_putenv: %s",
377 pam_strerror(sshpam_handle, sshpam_err));
385 * Conversation function for authentication thread.
388 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
389 struct pam_response **resp, void *data)
392 struct pam_ctxt *ctxt;
393 struct pam_response *reply;
396 debug3("PAM: %s entering, %d messages", __func__, n);
400 error("PAM: conversation function passed a null context");
401 return (PAM_CONV_ERR);
404 if (n <= 0 || n > PAM_MAX_NUM_MSG)
405 return (PAM_CONV_ERR);
407 if ((reply = calloc(n, sizeof(*reply))) == NULL)
408 return (PAM_CONV_ERR);
410 buffer_init(&buffer);
411 for (i = 0; i < n; ++i) {
412 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
413 case PAM_PROMPT_ECHO_OFF:
414 buffer_put_cstring(&buffer,
415 PAM_MSG_MEMBER(msg, i, msg));
416 if (ssh_msg_send(ctxt->pam_csock,
417 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
419 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
421 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
423 reply[i].resp = buffer_get_string(&buffer, NULL);
425 case PAM_PROMPT_ECHO_ON:
426 buffer_put_cstring(&buffer,
427 PAM_MSG_MEMBER(msg, i, msg));
428 if (ssh_msg_send(ctxt->pam_csock,
429 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
431 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
433 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
435 reply[i].resp = buffer_get_string(&buffer, NULL);
438 buffer_put_cstring(&buffer,
439 PAM_MSG_MEMBER(msg, i, msg));
440 if (ssh_msg_send(ctxt->pam_csock,
441 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
445 buffer_put_cstring(&buffer,
446 PAM_MSG_MEMBER(msg, i, msg));
447 if (ssh_msg_send(ctxt->pam_csock,
448 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
454 buffer_clear(&buffer);
456 buffer_free(&buffer);
458 return (PAM_SUCCESS);
461 for(i = 0; i < n; i++) {
462 if (reply[i].resp != NULL)
463 xfree(reply[i].resp);
466 buffer_free(&buffer);
467 return (PAM_CONV_ERR);
471 * Authentication thread.
474 sshpam_thread(void *ctxtp)
476 struct pam_ctxt *ctxt = ctxtp;
478 struct pam_conv sshpam_conv;
479 int flags = (options.permit_empty_passwd == 0 ?
480 PAM_DISALLOW_NULL_AUTHTOK : 0);
481 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
482 extern char **environ;
485 const char *pam_user;
486 const char **ptr_pam_user = &pam_user;
487 char *tz = getenv("TZ");
489 pam_get_item(sshpam_handle, PAM_USER,
490 (sshpam_const void **)ptr_pam_user);
494 if (setenv("TZ", tz, 1) == -1)
495 error("PAM: could not set TZ environment: %s",
498 if (sshpam_authctxt != NULL) {
499 setproctitle("%s [pam]",
500 sshpam_authctxt->valid ? pam_user : "unknown");
504 sshpam_conv.conv = sshpam_thread_conv;
505 sshpam_conv.appdata_ptr = ctxt;
507 if (sshpam_authctxt == NULL)
508 fatal("%s: PAM authctxt not initialized", __func__);
510 buffer_init(&buffer);
511 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
512 (const void *)&sshpam_conv);
513 if (sshpam_err != PAM_SUCCESS)
515 sshpam_err = pam_authenticate(sshpam_handle, flags);
516 if (sshpam_err != PAM_SUCCESS)
520 if (!do_pam_account()) {
521 sshpam_err = PAM_ACCT_EXPIRED;
524 if (sshpam_authctxt->force_pwchange) {
525 sshpam_err = pam_chauthtok(sshpam_handle,
526 PAM_CHANGE_EXPIRED_AUTHTOK);
527 if (sshpam_err != PAM_SUCCESS)
529 sshpam_password_change_required(0);
533 buffer_put_cstring(&buffer, "OK");
535 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
536 /* Export variables set by do_pam_account */
537 buffer_put_int(&buffer, sshpam_account_status);
538 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
540 /* Export any environment strings set in child */
541 for(i = 0; environ[i] != NULL; i++)
543 buffer_put_int(&buffer, i);
544 for(i = 0; environ[i] != NULL; i++)
545 buffer_put_cstring(&buffer, environ[i]);
547 /* Export any environment strings set by PAM in child */
548 env_from_pam = pam_getenvlist(sshpam_handle);
549 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
551 buffer_put_int(&buffer, i);
552 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
553 buffer_put_cstring(&buffer, env_from_pam[i]);
554 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
556 /* XXX - can't do much about an error here */
557 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
558 buffer_free(&buffer);
562 buffer_put_cstring(&buffer,
563 pam_strerror(sshpam_handle, sshpam_err));
564 /* XXX - can't do much about an error here */
565 if (sshpam_err == PAM_ACCT_EXPIRED)
566 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
568 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
569 buffer_free(&buffer);
572 return (NULL); /* Avoid warning for non-pthread case */
576 sshpam_thread_cleanup(void)
578 struct pam_ctxt *ctxt = cleanup_ctxt;
580 debug3("PAM: %s entering", __func__);
581 if (ctxt != NULL && ctxt->pam_thread != 0) {
582 pthread_cancel(ctxt->pam_thread);
583 pthread_join(ctxt->pam_thread, NULL);
584 close(ctxt->pam_psock);
585 close(ctxt->pam_csock);
586 memset(ctxt, 0, sizeof(*ctxt));
592 sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
593 struct pam_response **resp, void *data)
595 debug3("PAM: %s entering, %d messages", __func__, n);
596 return (PAM_CONV_ERR);
599 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
602 sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
603 struct pam_response **resp, void *data)
605 struct pam_response *reply;
609 debug3("PAM: %s called with %d messages", __func__, n);
612 if (n <= 0 || n > PAM_MAX_NUM_MSG)
613 return (PAM_CONV_ERR);
615 if ((reply = calloc(n, sizeof(*reply))) == NULL)
616 return (PAM_CONV_ERR);
618 for (i = 0; i < n; ++i) {
619 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
622 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
623 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
624 buffer_append(&loginmsg, "\n", 1 );
625 reply[i].resp_retcode = PAM_SUCCESS;
632 return (PAM_SUCCESS);
635 for(i = 0; i < n; i++) {
636 if (reply[i].resp != NULL)
637 xfree(reply[i].resp);
640 return (PAM_CONV_ERR);
643 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
648 if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
650 debug("PAM: cleanup");
651 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
652 if (sshpam_session_open) {
653 debug("PAM: closing session");
654 pam_close_session(sshpam_handle, PAM_SILENT);
655 sshpam_session_open = 0;
657 if (sshpam_cred_established) {
658 debug("PAM: deleting credentials");
659 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
660 sshpam_cred_established = 0;
662 sshpam_authenticated = 0;
663 pam_end(sshpam_handle, sshpam_err);
664 sshpam_handle = NULL;
668 sshpam_init(Authctxt *authctxt)
670 extern char *__progname;
671 const char *pam_rhost, *pam_user, *user = authctxt->user;
672 const char **ptr_pam_user = &pam_user;
674 if (sshpam_handle != NULL) {
675 /* We already have a PAM context; check if the user matches */
676 sshpam_err = pam_get_item(sshpam_handle,
677 PAM_USER, (sshpam_const void **)ptr_pam_user);
678 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
680 pam_end(sshpam_handle, sshpam_err);
681 sshpam_handle = NULL;
683 debug("PAM: initializing for \"%s\"", user);
685 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
686 sshpam_authctxt = authctxt;
688 if (sshpam_err != PAM_SUCCESS) {
689 pam_end(sshpam_handle, sshpam_err);
690 sshpam_handle = NULL;
693 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
694 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
695 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
696 if (sshpam_err != PAM_SUCCESS) {
697 pam_end(sshpam_handle, sshpam_err);
698 sshpam_handle = NULL;
701 #ifdef PAM_TTY_KLUDGE
703 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
704 * sshd doesn't set the tty until too late in the auth process and
705 * may not even set one (for tty-less connections)
707 debug("PAM: setting PAM_TTY to \"ssh\"");
708 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
709 if (sshpam_err != PAM_SUCCESS) {
710 pam_end(sshpam_handle, sshpam_err);
711 sshpam_handle = NULL;
719 sshpam_init_ctx(Authctxt *authctxt)
721 struct pam_ctxt *ctxt;
724 debug3("PAM: %s entering", __func__);
726 * Refuse to start if we don't have PAM enabled or do_pam_account
727 * has previously failed.
729 if (!options.use_pam || sshpam_account_status == 0)
733 if (sshpam_init(authctxt) == -1) {
734 error("PAM: initialization failed");
738 ctxt = xcalloc(1, sizeof *ctxt);
740 /* Start the authentication thread */
741 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
742 error("PAM: failed create sockets: %s", strerror(errno));
746 ctxt->pam_psock = socks[0];
747 ctxt->pam_csock = socks[1];
748 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
749 error("PAM: failed to start authentication thread: %s",
761 sshpam_query(void *ctx, char **name, char **info,
762 u_int *num, char ***prompts, u_int **echo_on)
765 struct pam_ctxt *ctxt = ctx;
771 debug3("PAM: %s entering", __func__);
772 buffer_init(&buffer);
775 *prompts = xmalloc(sizeof(char *));
778 *echo_on = xmalloc(sizeof(u_int));
779 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
780 type = buffer_get_char(&buffer);
781 msg = buffer_get_string(&buffer, NULL);
784 case PAM_PROMPT_ECHO_ON:
785 case PAM_PROMPT_ECHO_OFF:
787 len = plen + mlen + 1;
788 **prompts = xrealloc(**prompts, 1, len);
789 strlcpy(**prompts + plen, msg, len - plen);
791 **echo_on = (type == PAM_PROMPT_ECHO_ON);
796 /* accumulate messages */
797 len = plen + mlen + 2;
798 **prompts = xrealloc(**prompts, 1, len);
799 strlcpy(**prompts + plen, msg, len - plen);
801 strlcat(**prompts + plen, "\n", len - plen);
805 case PAM_ACCT_EXPIRED:
806 sshpam_account_status = 0;
809 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
810 if (**prompts != NULL && strlen(**prompts) != 0) {
821 if (**prompts != NULL) {
822 /* drain any accumulated messages */
823 debug("PAM: %s", **prompts);
824 buffer_append(&loginmsg, **prompts,
829 if (type == PAM_SUCCESS) {
830 if (!sshpam_authctxt->valid ||
831 (sshpam_authctxt->pw->pw_uid == 0 &&
832 options.permit_root_login != PERMIT_YES))
833 fatal("Internal error: PAM auth "
834 "succeeded when it should have "
836 import_environments(&buffer);
843 error("PAM: %s for %s%.100s from %.100s", msg,
844 sshpam_authctxt->valid ? "" : "illegal user ",
845 sshpam_authctxt->user,
846 get_remote_name_or_ip(utmp_len, options.use_dns));
859 /* XXX - see also comment in auth-chall.c:verify_response */
861 sshpam_respond(void *ctx, u_int num, char **resp)
864 struct pam_ctxt *ctxt = ctx;
866 debug2("PAM: %s entering, %u responses", __func__, num);
867 switch (ctxt->pam_done) {
869 sshpam_authenticated = 1;
877 error("PAM: expected one response, got %u", num);
880 buffer_init(&buffer);
881 if (sshpam_authctxt->valid &&
882 (sshpam_authctxt->pw->pw_uid != 0 ||
883 options.permit_root_login == PERMIT_YES))
884 buffer_put_cstring(&buffer, *resp);
886 buffer_put_cstring(&buffer, badpw);
887 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
888 buffer_free(&buffer);
891 buffer_free(&buffer);
896 sshpam_free_ctx(void *ctxtp)
898 struct pam_ctxt *ctxt = ctxtp;
900 debug3("PAM: %s entering", __func__);
901 sshpam_thread_cleanup();
904 * We don't call sshpam_cleanup() here because we may need the PAM
905 * handle at a later stage, e.g. when setting up a session. It's
906 * still on the cleanup list, so pam_end() *will* be called before
907 * the server process terminates.
911 KbdintDevice sshpam_device = {
919 KbdintDevice mm_sshpam_device = {
928 * This replaces auth-pam.c
931 start_pam(Authctxt *authctxt)
933 if (!options.use_pam)
934 fatal("PAM: initialisation requested when UsePAM=no");
936 if (sshpam_init(authctxt) == -1)
937 fatal("PAM: initialisation failed");
949 debug("%s: called", __func__);
950 if (sshpam_account_status != -1)
951 return (sshpam_account_status);
953 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
954 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
955 pam_strerror(sshpam_handle, sshpam_err));
957 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
958 sshpam_account_status = 0;
959 return (sshpam_account_status);
962 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
963 sshpam_password_change_required(1);
965 sshpam_account_status = 1;
966 return (sshpam_account_status);
970 do_pam_set_tty(const char *tty)
973 debug("PAM: setting PAM_TTY to \"%s\"", tty);
974 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
975 if (sshpam_err != PAM_SUCCESS)
976 fatal("PAM: failed to set PAM_TTY: %s",
977 pam_strerror(sshpam_handle, sshpam_err));
982 do_pam_setcred(int init)
984 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
985 (const void *)&store_conv);
986 if (sshpam_err != PAM_SUCCESS)
987 fatal("PAM: failed to set PAM_CONV: %s",
988 pam_strerror(sshpam_handle, sshpam_err));
990 debug("PAM: establishing credentials");
991 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
993 debug("PAM: reinitializing credentials");
994 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
996 if (sshpam_err == PAM_SUCCESS) {
997 sshpam_cred_established = 1;
1000 if (sshpam_authenticated)
1001 fatal("PAM: pam_setcred(): %s",
1002 pam_strerror(sshpam_handle, sshpam_err));
1004 debug("PAM: pam_setcred(): %s",
1005 pam_strerror(sshpam_handle, sshpam_err));
1009 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
1010 struct pam_response **resp, void *data)
1012 char input[PAM_MAX_MSG_SIZE];
1013 struct pam_response *reply;
1016 debug3("PAM: %s called with %d messages", __func__, n);
1020 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
1021 return (PAM_CONV_ERR);
1023 if ((reply = calloc(n, sizeof(*reply))) == NULL)
1024 return (PAM_CONV_ERR);
1026 for (i = 0; i < n; ++i) {
1027 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1028 case PAM_PROMPT_ECHO_OFF:
1030 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
1032 reply[i].resp_retcode = PAM_SUCCESS;
1034 case PAM_PROMPT_ECHO_ON:
1035 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1036 if (fgets(input, sizeof input, stdin) == NULL)
1038 if ((reply[i].resp = strdup(input)) == NULL)
1040 reply[i].resp_retcode = PAM_SUCCESS;
1044 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1045 reply[i].resp_retcode = PAM_SUCCESS;
1052 return (PAM_SUCCESS);
1055 for(i = 0; i < n; i++) {
1056 if (reply[i].resp != NULL)
1057 xfree(reply[i].resp);
1060 return (PAM_CONV_ERR);
1063 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
1066 * XXX this should be done in the authentication phase, but ssh1 doesn't
1070 do_pam_chauthtok(void)
1073 fatal("Password expired (unable to change with privsep)");
1074 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1075 (const void *)&tty_conv);
1076 if (sshpam_err != PAM_SUCCESS)
1077 fatal("PAM: failed to set PAM_CONV: %s",
1078 pam_strerror(sshpam_handle, sshpam_err));
1079 debug("PAM: changing password");
1080 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1081 if (sshpam_err != PAM_SUCCESS)
1082 fatal("PAM: pam_chauthtok(): %s",
1083 pam_strerror(sshpam_handle, sshpam_err));
1087 do_pam_session(void)
1089 debug3("PAM: opening session");
1090 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1091 (const void *)&store_conv);
1092 if (sshpam_err != PAM_SUCCESS)
1093 fatal("PAM: failed to set PAM_CONV: %s",
1094 pam_strerror(sshpam_handle, sshpam_err));
1095 sshpam_err = pam_open_session(sshpam_handle, 0);
1096 if (sshpam_err == PAM_SUCCESS)
1097 sshpam_session_open = 1;
1099 sshpam_session_open = 0;
1100 disable_forwarding();
1101 error("PAM: pam_open_session(): %s",
1102 pam_strerror(sshpam_handle, sshpam_err));
1108 is_pam_session_open(void)
1110 return sshpam_session_open;
1114 * Set a PAM environment string. We need to do this so that the session
1115 * modules can handle things like Kerberos/GSI credentials that appear
1116 * during the ssh authentication process.
1119 do_pam_putenv(char *name, char *value)
1122 #ifdef HAVE_PAM_PUTENV
1126 len = strlen(name) + strlen(value) + 2;
1127 compound = xmalloc(len);
1129 snprintf(compound, len, "%s=%s", name, value);
1130 ret = pam_putenv(sshpam_handle, compound);
1138 fetch_pam_child_environment(void)
1144 fetch_pam_environment(void)
1146 return (pam_getenvlist(sshpam_handle));
1150 free_pam_environment(char **env)
1157 for (envp = env; *envp; envp++)
1163 * "Blind" conversation function for password authentication. Assumes that
1164 * echo-off prompts are for the password and stores messages for later
1168 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1169 struct pam_response **resp, void *data)
1171 struct pam_response *reply;
1175 debug3("PAM: %s called with %d messages", __func__, n);
1179 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1180 return (PAM_CONV_ERR);
1182 if ((reply = calloc(n, sizeof(*reply))) == NULL)
1183 return (PAM_CONV_ERR);
1185 for (i = 0; i < n; ++i) {
1186 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1187 case PAM_PROMPT_ECHO_OFF:
1188 if (sshpam_password == NULL)
1190 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1192 reply[i].resp_retcode = PAM_SUCCESS;
1196 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1198 buffer_append(&loginmsg,
1199 PAM_MSG_MEMBER(msg, i, msg), len);
1200 buffer_append(&loginmsg, "\n", 1);
1202 if ((reply[i].resp = strdup("")) == NULL)
1204 reply[i].resp_retcode = PAM_SUCCESS;
1211 return (PAM_SUCCESS);
1214 for(i = 0; i < n; i++) {
1215 if (reply[i].resp != NULL)
1216 xfree(reply[i].resp);
1219 return (PAM_CONV_ERR);
1222 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1225 * Attempt password authentication via PAM
1228 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1230 int flags = (options.permit_empty_passwd == 0 ?
1231 PAM_DISALLOW_NULL_AUTHTOK : 0);
1233 if (!options.use_pam || sshpam_handle == NULL)
1234 fatal("PAM: %s called when PAM disabled or failed to "
1235 "initialise.", __func__);
1237 sshpam_password = password;
1238 sshpam_authctxt = authctxt;
1241 * If the user logging in is invalid, or is root but is not permitted
1242 * by PermitRootLogin, use an invalid password to prevent leaking
1243 * information via timing (eg if the PAM config has a delay on fail).
1245 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1246 options.permit_root_login != PERMIT_YES))
1247 sshpam_password = badpw;
1249 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1250 (const void *)&passwd_conv);
1251 if (sshpam_err != PAM_SUCCESS)
1252 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1253 pam_strerror(sshpam_handle, sshpam_err));
1255 sshpam_err = pam_authenticate(sshpam_handle, flags);
1256 sshpam_password = NULL;
1257 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1258 debug("PAM: password authentication accepted for %.100s",
1262 debug("PAM: password authentication failed for %.100s: %s",
1263 authctxt->valid ? authctxt->user : "an illegal user",
1264 pam_strerror(sshpam_handle, sshpam_err));
1268 #endif /* USE_PAM */