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 $xFreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
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 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
70 #ifdef PAM_SUN_CODEBASE
71 # define sshpam_const /* Solaris, HP-UX, AIX */
73 # define sshpam_const const /* LinuxPAM, OpenPAM */
76 /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
77 #ifdef PAM_SUN_CODEBASE
78 # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
80 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
96 #include "auth-options.h"
100 #include "monitor_wrap.h"
102 extern ServerOptions options;
103 extern Buffer loginmsg;
105 extern u_int utmp_len;
107 /* so we don't silently change behaviour */
108 #ifdef USE_POSIX_THREADS
109 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
113 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
114 * and generally a bad idea. Use at own risk and do not expect support if
117 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
120 * Avoid namespace clash when *not* using pthreads for systems *with*
121 * pthreads, which unconditionally define pthread_t via sys/types.h
124 typedef pthread_t sp_pthread_t;
126 typedef pid_t sp_pthread_t;
130 sp_pthread_t pam_thread;
136 static void sshpam_free_ctx(void *);
137 static struct pam_ctxt *cleanup_ctxt;
139 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
141 * Simulate threads with processes.
144 static int sshpam_thread_status = -1;
145 static mysig_t sshpam_oldsig;
148 sshpam_sigchld_handler(int sig)
150 signal(SIGCHLD, SIG_DFL);
151 if (cleanup_ctxt == NULL)
152 return; /* handler called after PAM cleanup, shouldn't happen */
153 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
155 /* PAM thread has not exitted, privsep slave must have */
156 kill(cleanup_ctxt->pam_thread, SIGTERM);
157 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
159 return; /* could not wait */
161 if (WIFSIGNALED(sshpam_thread_status) &&
162 WTERMSIG(sshpam_thread_status) == SIGTERM)
163 return; /* terminated by pthread_cancel */
164 if (!WIFEXITED(sshpam_thread_status))
165 fatal("PAM: authentication thread exited unexpectedly");
166 if (WEXITSTATUS(sshpam_thread_status) != 0)
167 fatal("PAM: authentication thread exited uncleanly");
172 pthread_exit(void *value)
179 pthread_create(sp_pthread_t *thread, const void *attr,
180 void *(*thread_start)(void *), void *arg)
183 struct pam_ctxt *ctx = arg;
185 sshpam_thread_status = -1;
186 switch ((pid = fork())) {
188 error("fork(): %s", strerror(errno));
191 close(ctx->pam_psock);
197 close(ctx->pam_csock);
199 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
205 pthread_cancel(sp_pthread_t thread)
207 signal(SIGCHLD, sshpam_oldsig);
208 return (kill(thread, SIGTERM));
213 pthread_join(sp_pthread_t thread, void **value)
217 if (sshpam_thread_status != -1)
218 return (sshpam_thread_status);
219 signal(SIGCHLD, sshpam_oldsig);
220 waitpid(thread, &status, 0);
226 static pam_handle_t *sshpam_handle = NULL;
227 static int sshpam_err = 0;
228 static int sshpam_authenticated = 0;
229 static int sshpam_session_open = 0;
230 static int sshpam_cred_established = 0;
231 static int sshpam_account_status = -1;
232 static char **sshpam_env = NULL;
233 static Authctxt *sshpam_authctxt = NULL;
234 static const char *sshpam_password = NULL;
235 static char badpw[] = "\b\n\r\177INCORRECT";
237 /* Some PAM implementations don't implement this */
238 #ifndef HAVE_PAM_GETENVLIST
240 pam_getenvlist(pam_handle_t *pamh)
243 * XXX - If necessary, we can still support envrionment passing
244 * for platforms without pam_getenvlist by searching for known
245 * env vars (e.g. KRB5CCNAME) from the PAM environment.
252 * Some platforms, notably Solaris, do not enforce password complexity
253 * rules during pam_chauthtok() if the real uid of the calling process
254 * is 0, on the assumption that it's being called by "passwd" run by root.
255 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
258 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
260 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
264 if (sshpam_authctxt == NULL)
265 fatal("PAM: sshpam_authctxt not initialized");
266 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
267 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
268 result = pam_chauthtok(pamh, flags);
269 if (setreuid(0, -1) == -1)
270 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
273 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
277 sshpam_password_change_required(int reqd)
279 debug3("%s %d", __func__, reqd);
280 if (sshpam_authctxt == NULL)
281 fatal("%s: PAM authctxt not initialized", __func__);
282 sshpam_authctxt->force_pwchange = reqd;
284 no_port_forwarding_flag |= 2;
285 no_agent_forwarding_flag |= 2;
286 no_x11_forwarding_flag |= 2;
288 no_port_forwarding_flag &= ~2;
289 no_agent_forwarding_flag &= ~2;
290 no_x11_forwarding_flag &= ~2;
294 /* Import regular and PAM environment from subprocess */
296 import_environments(Buffer *b)
302 debug3("PAM: %s entering", __func__);
304 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
305 /* Import variables set by do_pam_account */
306 sshpam_account_status = buffer_get_int(b);
307 sshpam_password_change_required(buffer_get_int(b));
309 /* Import environment from subprocess */
310 num_env = buffer_get_int(b);
312 fatal("%s: received %u environment variables, expected <= 1024",
314 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
315 debug3("PAM: num env strings %d", num_env);
316 for(i = 0; i < num_env; i++)
317 sshpam_env[i] = buffer_get_string(b, NULL);
319 sshpam_env[num_env] = NULL;
321 /* Import PAM environment from subprocess */
322 num_env = buffer_get_int(b);
323 debug("PAM: num PAM env strings %d", num_env);
324 for(i = 0; i < num_env; i++) {
325 env = buffer_get_string(b, NULL);
327 #ifdef HAVE_PAM_PUTENV
328 /* Errors are not fatal here */
329 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
330 error("PAM: pam_putenv: %s",
331 pam_strerror(sshpam_handle, sshpam_err));
339 * Conversation function for authentication thread.
342 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
343 struct pam_response **resp, void *data)
346 struct pam_ctxt *ctxt;
347 struct pam_response *reply;
350 debug3("PAM: %s entering, %d messages", __func__, n);
354 error("PAM: conversation function passed a null context");
355 return (PAM_CONV_ERR);
358 if (n <= 0 || n > PAM_MAX_NUM_MSG)
359 return (PAM_CONV_ERR);
361 if ((reply = calloc(n, sizeof(*reply))) == NULL)
362 return (PAM_CONV_ERR);
364 buffer_init(&buffer);
365 for (i = 0; i < n; ++i) {
366 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
367 case PAM_PROMPT_ECHO_OFF:
368 buffer_put_cstring(&buffer,
369 PAM_MSG_MEMBER(msg, i, msg));
370 if (ssh_msg_send(ctxt->pam_csock,
371 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
373 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
375 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
377 reply[i].resp = buffer_get_string(&buffer, NULL);
379 case PAM_PROMPT_ECHO_ON:
380 buffer_put_cstring(&buffer,
381 PAM_MSG_MEMBER(msg, i, msg));
382 if (ssh_msg_send(ctxt->pam_csock,
383 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
385 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
387 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
389 reply[i].resp = buffer_get_string(&buffer, NULL);
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)
399 buffer_put_cstring(&buffer,
400 PAM_MSG_MEMBER(msg, i, msg));
401 if (ssh_msg_send(ctxt->pam_csock,
402 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
408 buffer_clear(&buffer);
410 buffer_free(&buffer);
412 return (PAM_SUCCESS);
415 for(i = 0; i < n; i++) {
416 if (reply[i].resp != NULL)
417 xfree(reply[i].resp);
420 buffer_free(&buffer);
421 return (PAM_CONV_ERR);
425 * Authentication thread.
428 sshpam_thread(void *ctxtp)
430 struct pam_ctxt *ctxt = ctxtp;
432 struct pam_conv sshpam_conv;
433 int flags = (options.permit_empty_passwd == 0 ?
434 PAM_DISALLOW_NULL_AUTHTOK : 0);
435 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
436 extern char **environ;
439 const char *pam_user;
440 const char **ptr_pam_user = &pam_user;
441 char *tz = getenv("TZ");
443 pam_get_item(sshpam_handle, PAM_USER,
444 (sshpam_const void **)ptr_pam_user);
448 if (setenv("TZ", tz, 1) == -1)
449 error("PAM: could not set TZ environment: %s",
452 if (sshpam_authctxt != NULL) {
453 setproctitle("%s [pam]",
454 sshpam_authctxt->valid ? pam_user : "unknown");
458 sshpam_conv.conv = sshpam_thread_conv;
459 sshpam_conv.appdata_ptr = ctxt;
461 if (sshpam_authctxt == NULL)
462 fatal("%s: PAM authctxt not initialized", __func__);
464 buffer_init(&buffer);
465 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
466 (const void *)&sshpam_conv);
467 if (sshpam_err != PAM_SUCCESS)
469 sshpam_err = pam_authenticate(sshpam_handle, flags);
470 if (sshpam_err != PAM_SUCCESS)
474 if (!do_pam_account()) {
475 sshpam_err = PAM_ACCT_EXPIRED;
478 if (sshpam_authctxt->force_pwchange) {
479 sshpam_err = pam_chauthtok(sshpam_handle,
480 PAM_CHANGE_EXPIRED_AUTHTOK);
481 if (sshpam_err != PAM_SUCCESS)
483 sshpam_password_change_required(0);
487 buffer_put_cstring(&buffer, "OK");
489 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
490 /* Export variables set by do_pam_account */
491 buffer_put_int(&buffer, sshpam_account_status);
492 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
494 /* Export any environment strings set in child */
495 for(i = 0; environ[i] != NULL; i++)
497 buffer_put_int(&buffer, i);
498 for(i = 0; environ[i] != NULL; i++)
499 buffer_put_cstring(&buffer, environ[i]);
501 /* Export any environment strings set by PAM in child */
502 env_from_pam = pam_getenvlist(sshpam_handle);
503 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
505 buffer_put_int(&buffer, i);
506 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
507 buffer_put_cstring(&buffer, env_from_pam[i]);
508 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
510 /* XXX - can't do much about an error here */
511 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
512 buffer_free(&buffer);
516 buffer_put_cstring(&buffer,
517 pam_strerror(sshpam_handle, sshpam_err));
518 /* XXX - can't do much about an error here */
519 if (sshpam_err == PAM_ACCT_EXPIRED)
520 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
522 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
523 buffer_free(&buffer);
526 return (NULL); /* Avoid warning for non-pthread case */
530 sshpam_thread_cleanup(void)
532 struct pam_ctxt *ctxt = cleanup_ctxt;
534 debug3("PAM: %s entering", __func__);
535 if (ctxt != NULL && ctxt->pam_thread != 0) {
536 pthread_cancel(ctxt->pam_thread);
537 pthread_join(ctxt->pam_thread, NULL);
538 close(ctxt->pam_psock);
539 close(ctxt->pam_csock);
540 memset(ctxt, 0, sizeof(*ctxt));
546 sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
547 struct pam_response **resp, void *data)
549 debug3("PAM: %s entering, %d messages", __func__, n);
550 return (PAM_CONV_ERR);
553 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
556 sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
557 struct pam_response **resp, void *data)
559 struct pam_response *reply;
563 debug3("PAM: %s called with %d messages", __func__, n);
566 if (n <= 0 || n > PAM_MAX_NUM_MSG)
567 return (PAM_CONV_ERR);
569 if ((reply = calloc(n, sizeof(*reply))) == NULL)
570 return (PAM_CONV_ERR);
572 for (i = 0; i < n; ++i) {
573 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
576 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
577 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
578 buffer_append(&loginmsg, "\n", 1 );
579 reply[i].resp_retcode = PAM_SUCCESS;
586 return (PAM_SUCCESS);
589 for(i = 0; i < n; i++) {
590 if (reply[i].resp != NULL)
591 xfree(reply[i].resp);
594 return (PAM_CONV_ERR);
597 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
602 debug("PAM: cleanup");
603 if (sshpam_handle == NULL)
605 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
606 if (sshpam_cred_established) {
607 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
608 sshpam_cred_established = 0;
610 if (sshpam_session_open) {
611 pam_close_session(sshpam_handle, PAM_SILENT);
612 sshpam_session_open = 0;
614 sshpam_authenticated = 0;
615 pam_end(sshpam_handle, sshpam_err);
616 sshpam_handle = NULL;
620 sshpam_init(Authctxt *authctxt)
622 extern char *__progname;
623 const char *pam_rhost, *pam_user, *user = authctxt->user;
624 const char **ptr_pam_user = &pam_user;
626 if (sshpam_handle != NULL) {
627 /* We already have a PAM context; check if the user matches */
628 sshpam_err = pam_get_item(sshpam_handle,
629 PAM_USER, (sshpam_const void **)ptr_pam_user);
630 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
632 pam_end(sshpam_handle, sshpam_err);
633 sshpam_handle = NULL;
635 debug("PAM: initializing for \"%s\"", user);
637 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
638 sshpam_authctxt = authctxt;
640 if (sshpam_err != PAM_SUCCESS) {
641 pam_end(sshpam_handle, sshpam_err);
642 sshpam_handle = NULL;
645 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
646 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
647 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
648 if (sshpam_err != PAM_SUCCESS) {
649 pam_end(sshpam_handle, sshpam_err);
650 sshpam_handle = NULL;
653 #ifdef PAM_TTY_KLUDGE
655 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
656 * sshd doesn't set the tty until too late in the auth process and
657 * may not even set one (for tty-less connections)
659 debug("PAM: setting PAM_TTY to \"ssh\"");
660 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
661 if (sshpam_err != PAM_SUCCESS) {
662 pam_end(sshpam_handle, sshpam_err);
663 sshpam_handle = NULL;
671 sshpam_init_ctx(Authctxt *authctxt)
673 struct pam_ctxt *ctxt;
676 debug3("PAM: %s entering", __func__);
678 * Refuse to start if we don't have PAM enabled or do_pam_account
679 * has previously failed.
681 if (!options.use_pam || sshpam_account_status == 0)
685 if (sshpam_init(authctxt) == -1) {
686 error("PAM: initialization failed");
690 ctxt = xmalloc(sizeof *ctxt);
691 memset(ctxt, 0, sizeof(*ctxt));
693 /* Start the authentication thread */
694 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
695 error("PAM: failed create sockets: %s", strerror(errno));
699 ctxt->pam_psock = socks[0];
700 ctxt->pam_csock = socks[1];
701 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
702 error("PAM: failed to start authentication thread: %s",
714 sshpam_query(void *ctx, char **name, char **info,
715 u_int *num, char ***prompts, u_int **echo_on)
718 struct pam_ctxt *ctxt = ctx;
724 debug3("PAM: %s entering", __func__);
725 buffer_init(&buffer);
728 *prompts = xmalloc(sizeof(char *));
731 *echo_on = xmalloc(sizeof(u_int));
732 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
733 type = buffer_get_char(&buffer);
734 msg = buffer_get_string(&buffer, NULL);
737 case PAM_PROMPT_ECHO_ON:
738 case PAM_PROMPT_ECHO_OFF:
740 len = plen + mlen + 1;
741 **prompts = xrealloc(**prompts, 1, len);
742 strlcpy(**prompts + plen, msg, len - plen);
744 **echo_on = (type == PAM_PROMPT_ECHO_ON);
749 /* accumulate messages */
750 len = plen + mlen + 2;
751 **prompts = xrealloc(**prompts, 1, len);
752 strlcpy(**prompts + plen, msg, len - plen);
754 strlcat(**prompts + plen, "\n", len - plen);
758 case PAM_ACCT_EXPIRED:
759 sshpam_account_status = 0;
762 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
763 if (**prompts != NULL && strlen(**prompts) != 0) {
774 if (**prompts != NULL) {
775 /* drain any accumulated messages */
776 debug("PAM: %s", **prompts);
777 buffer_append(&loginmsg, **prompts,
782 if (type == PAM_SUCCESS) {
783 if (!sshpam_authctxt->valid ||
784 (sshpam_authctxt->pw->pw_uid == 0 &&
785 options.permit_root_login != PERMIT_YES))
786 fatal("Internal error: PAM auth "
787 "succeeded when it should have "
789 import_environments(&buffer);
796 error("PAM: %s for %s%.100s from %.100s", msg,
797 sshpam_authctxt->valid ? "" : "illegal user ",
798 sshpam_authctxt->user,
799 get_remote_name_or_ip(utmp_len, options.use_dns));
812 /* XXX - see also comment in auth-chall.c:verify_response */
814 sshpam_respond(void *ctx, u_int num, char **resp)
817 struct pam_ctxt *ctxt = ctx;
819 debug2("PAM: %s entering, %u responses", __func__, num);
820 switch (ctxt->pam_done) {
822 sshpam_authenticated = 1;
830 error("PAM: expected one response, got %u", num);
833 buffer_init(&buffer);
834 if (sshpam_authctxt->valid &&
835 (sshpam_authctxt->pw->pw_uid != 0 ||
836 options.permit_root_login == PERMIT_YES))
837 buffer_put_cstring(&buffer, *resp);
839 buffer_put_cstring(&buffer, badpw);
840 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
841 buffer_free(&buffer);
844 buffer_free(&buffer);
849 sshpam_free_ctx(void *ctxtp)
851 struct pam_ctxt *ctxt = ctxtp;
853 debug3("PAM: %s entering", __func__);
854 sshpam_thread_cleanup();
857 * We don't call sshpam_cleanup() here because we may need the PAM
858 * handle at a later stage, e.g. when setting up a session. It's
859 * still on the cleanup list, so pam_end() *will* be called before
860 * the server process terminates.
864 KbdintDevice sshpam_device = {
872 KbdintDevice mm_sshpam_device = {
881 * This replaces auth-pam.c
884 start_pam(Authctxt *authctxt)
886 if (!options.use_pam)
887 fatal("PAM: initialisation requested when UsePAM=no");
889 if (sshpam_init(authctxt) == -1)
890 fatal("PAM: initialisation failed");
902 debug("%s: called", __func__);
903 if (sshpam_account_status != -1)
904 return (sshpam_account_status);
906 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
907 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
908 pam_strerror(sshpam_handle, sshpam_err));
910 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
911 sshpam_account_status = 0;
912 return (sshpam_account_status);
915 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
916 sshpam_password_change_required(1);
918 sshpam_account_status = 1;
919 return (sshpam_account_status);
923 do_pam_set_tty(const char *tty)
926 debug("PAM: setting PAM_TTY to \"%s\"", tty);
927 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
928 if (sshpam_err != PAM_SUCCESS)
929 fatal("PAM: failed to set PAM_TTY: %s",
930 pam_strerror(sshpam_handle, sshpam_err));
935 do_pam_setcred(int init)
937 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
938 (const void *)&store_conv);
939 if (sshpam_err != PAM_SUCCESS)
940 fatal("PAM: failed to set PAM_CONV: %s",
941 pam_strerror(sshpam_handle, sshpam_err));
943 debug("PAM: establishing credentials");
944 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
946 debug("PAM: reinitializing credentials");
947 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
949 if (sshpam_err == PAM_SUCCESS) {
950 sshpam_cred_established = 1;
953 if (sshpam_authenticated)
954 fatal("PAM: pam_setcred(): %s",
955 pam_strerror(sshpam_handle, sshpam_err));
957 debug("PAM: pam_setcred(): %s",
958 pam_strerror(sshpam_handle, sshpam_err));
962 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
963 struct pam_response **resp, void *data)
965 char input[PAM_MAX_MSG_SIZE];
966 struct pam_response *reply;
969 debug3("PAM: %s called with %d messages", __func__, n);
973 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
974 return (PAM_CONV_ERR);
976 if ((reply = calloc(n, sizeof(*reply))) == NULL)
977 return (PAM_CONV_ERR);
979 for (i = 0; i < n; ++i) {
980 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
981 case PAM_PROMPT_ECHO_OFF:
983 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
985 reply[i].resp_retcode = PAM_SUCCESS;
987 case PAM_PROMPT_ECHO_ON:
988 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
989 fgets(input, sizeof input, stdin);
990 if ((reply[i].resp = strdup(input)) == NULL)
992 reply[i].resp_retcode = PAM_SUCCESS;
996 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
997 reply[i].resp_retcode = PAM_SUCCESS;
1004 return (PAM_SUCCESS);
1007 for(i = 0; i < n; i++) {
1008 if (reply[i].resp != NULL)
1009 xfree(reply[i].resp);
1012 return (PAM_CONV_ERR);
1015 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
1018 * XXX this should be done in the authentication phase, but ssh1 doesn't
1022 do_pam_chauthtok(void)
1025 fatal("Password expired (unable to change with privsep)");
1026 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1027 (const void *)&tty_conv);
1028 if (sshpam_err != PAM_SUCCESS)
1029 fatal("PAM: failed to set PAM_CONV: %s",
1030 pam_strerror(sshpam_handle, sshpam_err));
1031 debug("PAM: changing password");
1032 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1033 if (sshpam_err != PAM_SUCCESS)
1034 fatal("PAM: pam_chauthtok(): %s",
1035 pam_strerror(sshpam_handle, sshpam_err));
1039 do_pam_session(void)
1041 debug3("PAM: opening session");
1042 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1043 (const void *)&store_conv);
1044 if (sshpam_err != PAM_SUCCESS)
1045 fatal("PAM: failed to set PAM_CONV: %s",
1046 pam_strerror(sshpam_handle, sshpam_err));
1047 sshpam_err = pam_open_session(sshpam_handle, 0);
1048 if (sshpam_err == PAM_SUCCESS)
1049 sshpam_session_open = 1;
1051 sshpam_session_open = 0;
1052 disable_forwarding();
1053 error("PAM: pam_open_session(): %s",
1054 pam_strerror(sshpam_handle, sshpam_err));
1060 is_pam_session_open(void)
1062 return sshpam_session_open;
1066 * Set a PAM environment string. We need to do this so that the session
1067 * modules can handle things like Kerberos/GSI credentials that appear
1068 * during the ssh authentication process.
1071 do_pam_putenv(char *name, char *value)
1074 #ifdef HAVE_PAM_PUTENV
1078 len = strlen(name) + strlen(value) + 2;
1079 compound = xmalloc(len);
1081 snprintf(compound, len, "%s=%s", name, value);
1082 ret = pam_putenv(sshpam_handle, compound);
1090 fetch_pam_child_environment(void)
1096 fetch_pam_environment(void)
1098 return (pam_getenvlist(sshpam_handle));
1102 free_pam_environment(char **env)
1109 for (envp = env; *envp; envp++)
1115 * "Blind" conversation function for password authentication. Assumes that
1116 * echo-off prompts are for the password and stores messages for later
1120 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1121 struct pam_response **resp, void *data)
1123 struct pam_response *reply;
1127 debug3("PAM: %s called with %d messages", __func__, n);
1131 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1132 return (PAM_CONV_ERR);
1134 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1135 return (PAM_CONV_ERR);
1136 memset(reply, 0, n * sizeof(*reply));
1138 for (i = 0; i < n; ++i) {
1139 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1140 case PAM_PROMPT_ECHO_OFF:
1141 if (sshpam_password == NULL)
1143 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1145 reply[i].resp_retcode = PAM_SUCCESS;
1149 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1151 buffer_append(&loginmsg,
1152 PAM_MSG_MEMBER(msg, i, msg), len);
1153 buffer_append(&loginmsg, "\n", 1);
1155 if ((reply[i].resp = strdup("")) == NULL)
1157 reply[i].resp_retcode = PAM_SUCCESS;
1164 return (PAM_SUCCESS);
1167 for(i = 0; i < n; i++) {
1168 if (reply[i].resp != NULL)
1169 xfree(reply[i].resp);
1172 return (PAM_CONV_ERR);
1175 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1178 * Attempt password authentication via PAM
1181 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1183 int flags = (options.permit_empty_passwd == 0 ?
1184 PAM_DISALLOW_NULL_AUTHTOK : 0);
1186 if (!options.use_pam || sshpam_handle == NULL)
1187 fatal("PAM: %s called when PAM disabled or failed to "
1188 "initialise.", __func__);
1190 sshpam_password = password;
1191 sshpam_authctxt = authctxt;
1194 * If the user logging in is invalid, or is root but is not permitted
1195 * by PermitRootLogin, use an invalid password to prevent leaking
1196 * information via timing (eg if the PAM config has a delay on fail).
1198 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1199 options.permit_root_login != PERMIT_YES))
1200 sshpam_password = badpw;
1202 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1203 (const void *)&passwd_conv);
1204 if (sshpam_err != PAM_SUCCESS)
1205 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1206 pam_strerror(sshpam_handle, sshpam_err));
1208 sshpam_err = pam_authenticate(sshpam_handle, flags);
1209 sshpam_password = NULL;
1210 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1211 debug("PAM: password authentication accepted for %.100s",
1215 debug("PAM: password authentication failed for %.100s: %s",
1216 authctxt->valid ? authctxt->user : "an illegal user",
1217 pam_strerror(sshpam_handle, sshpam_err));
1221 #endif /* USE_PAM */