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 $ */
50 RCSID("$Id: auth-pam.c,v 1.126 2005/07/17 07:18:50 djm Exp $");
54 #if defined(HAVE_SECURITY_PAM_APPL_H)
55 #include <security/pam_appl.h>
56 #elif defined (HAVE_PAM_PAM_APPL_H)
57 #include <pam/pam_appl.h>
60 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
61 #ifdef PAM_SUN_CODEBASE
62 # define sshpam_const /* Solaris, HP-UX, AIX */
64 # define sshpam_const const /* LinuxPAM, OpenPAM */
73 #include "monitor_wrap.h"
80 #include "auth-options.h"
82 extern ServerOptions options;
83 extern Buffer loginmsg;
85 extern u_int utmp_len;
87 /* so we don't silently change behaviour */
88 #ifdef USE_POSIX_THREADS
89 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
93 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
94 * and generally a bad idea. Use at own risk and do not expect support if
97 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
100 * Avoid namespace clash when *not* using pthreads for systems *with*
101 * pthreads, which unconditionally define pthread_t via sys/types.h
104 typedef pthread_t sp_pthread_t;
106 typedef pid_t sp_pthread_t;
110 sp_pthread_t pam_thread;
116 static void sshpam_free_ctx(void *);
117 static struct pam_ctxt *cleanup_ctxt;
119 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
121 * Simulate threads with processes.
124 static int sshpam_thread_status = -1;
125 static mysig_t sshpam_oldsig;
128 sshpam_sigchld_handler(int sig)
130 signal(SIGCHLD, SIG_DFL);
131 if (cleanup_ctxt == NULL)
132 return; /* handler called after PAM cleanup, shouldn't happen */
133 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
135 /* PAM thread has not exitted, privsep slave must have */
136 kill(cleanup_ctxt->pam_thread, SIGTERM);
137 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
139 return; /* could not wait */
141 if (WIFSIGNALED(sshpam_thread_status) &&
142 WTERMSIG(sshpam_thread_status) == SIGTERM)
143 return; /* terminated by pthread_cancel */
144 if (!WIFEXITED(sshpam_thread_status))
145 fatal("PAM: authentication thread exited unexpectedly");
146 if (WEXITSTATUS(sshpam_thread_status) != 0)
147 fatal("PAM: authentication thread exited uncleanly");
151 pthread_exit(void *value __unused)
157 pthread_create(sp_pthread_t *thread, const void *attr __unused,
158 void *(*thread_start)(void *), void *arg)
161 struct pam_ctxt *ctx = arg;
163 sshpam_thread_status = -1;
164 switch ((pid = fork())) {
166 error("fork(): %s", strerror(errno));
169 close(ctx->pam_psock);
175 close(ctx->pam_csock);
177 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
183 pthread_cancel(sp_pthread_t thread)
185 signal(SIGCHLD, sshpam_oldsig);
186 return (kill(thread, SIGTERM));
190 pthread_join(sp_pthread_t thread, void **value __unused)
194 if (sshpam_thread_status != -1)
195 return (sshpam_thread_status);
196 signal(SIGCHLD, sshpam_oldsig);
197 waitpid(thread, &status, 0);
203 static pam_handle_t *sshpam_handle = NULL;
204 static int sshpam_err = 0;
205 static int sshpam_authenticated = 0;
206 static int sshpam_session_open = 0;
207 static int sshpam_cred_established = 0;
208 static int sshpam_account_status = -1;
209 static char **sshpam_env = NULL;
210 static Authctxt *sshpam_authctxt = NULL;
211 static const char *sshpam_password = NULL;
212 static char badpw[] = "\b\n\r\177INCORRECT";
214 /* Some PAM implementations don't implement this */
215 #ifndef HAVE_PAM_GETENVLIST
217 pam_getenvlist(pam_handle_t *pamh)
220 * XXX - If necessary, we can still support envrionment passing
221 * for platforms without pam_getenvlist by searching for known
222 * env vars (e.g. KRB5CCNAME) from the PAM environment.
229 * Some platforms, notably Solaris, do not enforce password complexity
230 * rules during pam_chauthtok() if the real uid of the calling process
231 * is 0, on the assumption that it's being called by "passwd" run by root.
232 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
235 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
237 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
241 if (sshpam_authctxt == NULL)
242 fatal("PAM: sshpam_authctxt not initialized");
243 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
244 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
245 result = pam_chauthtok(pamh, flags);
246 if (setreuid(0, -1) == -1)
247 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
250 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
254 sshpam_password_change_required(int reqd)
256 debug3("%s %d", __func__, reqd);
257 if (sshpam_authctxt == NULL)
258 fatal("%s: PAM authctxt not initialized", __func__);
259 sshpam_authctxt->force_pwchange = reqd;
261 no_port_forwarding_flag |= 2;
262 no_agent_forwarding_flag |= 2;
263 no_x11_forwarding_flag |= 2;
265 no_port_forwarding_flag &= ~2;
266 no_agent_forwarding_flag &= ~2;
267 no_x11_forwarding_flag &= ~2;
271 /* Import regular and PAM environment from subprocess */
273 import_environments(Buffer *b)
279 debug3("PAM: %s entering", __func__);
281 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
282 /* Import variables set by do_pam_account */
283 sshpam_account_status = buffer_get_int(b);
284 sshpam_password_change_required(buffer_get_int(b));
286 /* Import environment from subprocess */
287 num_env = buffer_get_int(b);
288 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
289 debug3("PAM: num env strings %d", num_env);
290 for(i = 0; i < num_env; i++)
291 sshpam_env[i] = buffer_get_string(b, NULL);
293 sshpam_env[num_env] = NULL;
295 /* Import PAM environment from subprocess */
296 num_env = buffer_get_int(b);
297 debug("PAM: num PAM env strings %d", num_env);
298 for(i = 0; i < num_env; i++) {
299 env = buffer_get_string(b, NULL);
301 #ifdef HAVE_PAM_PUTENV
302 /* Errors are not fatal here */
303 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
304 error("PAM: pam_putenv: %s",
305 pam_strerror(sshpam_handle, sshpam_err));
313 * Conversation function for authentication thread.
316 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
317 struct pam_response **resp, void *data)
320 struct pam_ctxt *ctxt;
321 struct pam_response *reply;
324 debug3("PAM: %s entering, %d messages", __func__, n);
328 error("PAM: conversation function passed a null context");
329 return (PAM_CONV_ERR);
332 if (n <= 0 || n > PAM_MAX_NUM_MSG)
333 return (PAM_CONV_ERR);
335 if ((reply = malloc(n * sizeof(*reply))) == NULL)
336 return (PAM_CONV_ERR);
337 memset(reply, 0, n * sizeof(*reply));
339 buffer_init(&buffer);
340 for (i = 0; i < n; ++i) {
341 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
342 case PAM_PROMPT_ECHO_OFF:
343 buffer_put_cstring(&buffer,
344 PAM_MSG_MEMBER(msg, i, msg));
345 if (ssh_msg_send(ctxt->pam_csock,
346 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
348 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
350 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
352 reply[i].resp = buffer_get_string(&buffer, NULL);
354 case PAM_PROMPT_ECHO_ON:
355 buffer_put_cstring(&buffer,
356 PAM_MSG_MEMBER(msg, i, msg));
357 if (ssh_msg_send(ctxt->pam_csock,
358 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
360 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
362 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
364 reply[i].resp = buffer_get_string(&buffer, NULL);
367 buffer_put_cstring(&buffer,
368 PAM_MSG_MEMBER(msg, i, msg));
369 if (ssh_msg_send(ctxt->pam_csock,
370 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
374 buffer_put_cstring(&buffer,
375 PAM_MSG_MEMBER(msg, i, msg));
376 if (ssh_msg_send(ctxt->pam_csock,
377 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
383 buffer_clear(&buffer);
385 buffer_free(&buffer);
387 return (PAM_SUCCESS);
390 for(i = 0; i < n; i++) {
391 if (reply[i].resp != NULL)
392 xfree(reply[i].resp);
395 buffer_free(&buffer);
396 return (PAM_CONV_ERR);
400 * Authentication thread.
403 sshpam_thread(void *ctxtp)
405 struct pam_ctxt *ctxt = ctxtp;
407 struct pam_conv sshpam_conv;
408 int flags = (options.permit_empty_passwd == 0 ?
409 PAM_DISALLOW_NULL_AUTHTOK : 0);
410 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
411 extern char **environ;
414 const char *pam_user;
415 const char **ptr_pam_user = &pam_user;
417 pam_get_item(sshpam_handle, PAM_USER,
418 (sshpam_const void **)ptr_pam_user);
421 if (sshpam_authctxt != NULL) {
422 setproctitle("%s [pam]",
423 sshpam_authctxt->valid ? pam_user : "unknown");
427 sshpam_conv.conv = sshpam_thread_conv;
428 sshpam_conv.appdata_ptr = ctxt;
430 if (sshpam_authctxt == NULL)
431 fatal("%s: PAM authctxt not initialized", __func__);
433 buffer_init(&buffer);
434 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
435 (const void *)&sshpam_conv);
436 if (sshpam_err != PAM_SUCCESS)
438 sshpam_err = pam_authenticate(sshpam_handle, flags);
439 if (sshpam_err != PAM_SUCCESS)
443 if (!do_pam_account())
445 if (sshpam_authctxt->force_pwchange) {
446 sshpam_err = pam_chauthtok(sshpam_handle,
447 PAM_CHANGE_EXPIRED_AUTHTOK);
448 if (sshpam_err != PAM_SUCCESS)
450 sshpam_password_change_required(0);
454 buffer_put_cstring(&buffer, "OK");
456 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
457 /* Export variables set by do_pam_account */
458 buffer_put_int(&buffer, sshpam_account_status);
459 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
461 /* Export any environment strings set in child */
462 for(i = 0; environ[i] != NULL; i++)
464 buffer_put_int(&buffer, i);
465 for(i = 0; environ[i] != NULL; i++)
466 buffer_put_cstring(&buffer, environ[i]);
468 /* Export any environment strings set by PAM in child */
469 env_from_pam = pam_getenvlist(sshpam_handle);
470 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
472 buffer_put_int(&buffer, i);
473 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
474 buffer_put_cstring(&buffer, env_from_pam[i]);
475 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
477 /* XXX - can't do much about an error here */
478 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
479 buffer_free(&buffer);
483 buffer_put_cstring(&buffer,
484 pam_strerror(sshpam_handle, sshpam_err));
485 /* XXX - can't do much about an error here */
486 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
487 buffer_free(&buffer);
490 return (NULL); /* Avoid warning for non-pthread case */
494 sshpam_thread_cleanup(void)
496 struct pam_ctxt *ctxt = cleanup_ctxt;
498 debug3("PAM: %s entering", __func__);
499 if (ctxt != NULL && ctxt->pam_thread != 0) {
500 pthread_cancel(ctxt->pam_thread);
501 pthread_join(ctxt->pam_thread, NULL);
502 close(ctxt->pam_psock);
503 close(ctxt->pam_csock);
504 memset(ctxt, 0, sizeof(*ctxt));
510 sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
511 struct pam_response **resp, void *data)
513 debug3("PAM: %s entering, %d messages", __func__, n);
514 return (PAM_CONV_ERR);
517 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
520 sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
521 struct pam_response **resp, void *data)
523 struct pam_response *reply;
527 debug3("PAM: %s called with %d messages", __func__, n);
530 if (n <= 0 || n > PAM_MAX_NUM_MSG)
531 return (PAM_CONV_ERR);
533 if ((reply = malloc(n * sizeof(*reply))) == NULL)
534 return (PAM_CONV_ERR);
535 memset(reply, 0, n * sizeof(*reply));
537 for (i = 0; i < n; ++i) {
538 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
541 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
542 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
543 buffer_append(&loginmsg, "\n", 1 );
544 reply[i].resp_retcode = PAM_SUCCESS;
551 return (PAM_SUCCESS);
554 for(i = 0; i < n; i++) {
555 if (reply[i].resp != NULL)
556 xfree(reply[i].resp);
559 return (PAM_CONV_ERR);
562 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
567 debug("PAM: cleanup");
568 if (sshpam_handle == NULL)
570 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
571 if (sshpam_cred_established) {
572 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
573 sshpam_cred_established = 0;
575 if (sshpam_session_open) {
576 pam_close_session(sshpam_handle, PAM_SILENT);
577 sshpam_session_open = 0;
579 sshpam_authenticated = 0;
580 pam_end(sshpam_handle, sshpam_err);
581 sshpam_handle = NULL;
585 sshpam_init(Authctxt *authctxt)
587 extern char *__progname;
588 const char *pam_rhost, *pam_user, *user = authctxt->user;
589 const char **ptr_pam_user = &pam_user;
591 if (sshpam_handle != NULL) {
592 /* We already have a PAM context; check if the user matches */
593 sshpam_err = pam_get_item(sshpam_handle,
594 PAM_USER, (sshpam_const void **)ptr_pam_user);
595 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
597 pam_end(sshpam_handle, sshpam_err);
598 sshpam_handle = NULL;
600 debug("PAM: initializing for \"%s\"", user);
602 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
603 sshpam_authctxt = authctxt;
605 if (sshpam_err != PAM_SUCCESS) {
606 pam_end(sshpam_handle, sshpam_err);
607 sshpam_handle = NULL;
610 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
611 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
612 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
613 if (sshpam_err != PAM_SUCCESS) {
614 pam_end(sshpam_handle, sshpam_err);
615 sshpam_handle = NULL;
618 #ifdef PAM_TTY_KLUDGE
620 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
621 * sshd doesn't set the tty until too late in the auth process and
622 * may not even set one (for tty-less connections)
624 debug("PAM: setting PAM_TTY to \"ssh\"");
625 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
626 if (sshpam_err != PAM_SUCCESS) {
627 pam_end(sshpam_handle, sshpam_err);
628 sshpam_handle = NULL;
636 sshpam_init_ctx(Authctxt *authctxt)
638 struct pam_ctxt *ctxt;
641 debug3("PAM: %s entering", __func__);
642 /* Refuse to start if we don't have PAM enabled */
643 if (!options.use_pam)
647 if (sshpam_init(authctxt) == -1) {
648 error("PAM: initialization failed");
652 ctxt = xmalloc(sizeof *ctxt);
653 memset(ctxt, 0, sizeof(*ctxt));
655 /* Start the authentication thread */
656 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
657 error("PAM: failed create sockets: %s", strerror(errno));
661 ctxt->pam_psock = socks[0];
662 ctxt->pam_csock = socks[1];
663 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
664 error("PAM: failed to start authentication thread: %s",
676 sshpam_query(void *ctx, char **name, char **info,
677 u_int *num, char ***prompts, u_int **echo_on)
680 struct pam_ctxt *ctxt = ctx;
686 debug3("PAM: %s entering", __func__);
687 buffer_init(&buffer);
690 *prompts = xmalloc(sizeof(char *));
693 *echo_on = xmalloc(sizeof(u_int));
694 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
695 type = buffer_get_char(&buffer);
696 msg = buffer_get_string(&buffer, NULL);
699 case PAM_PROMPT_ECHO_ON:
700 case PAM_PROMPT_ECHO_OFF:
702 len = plen + mlen + 1;
703 **prompts = xrealloc(**prompts, len);
704 strlcpy(**prompts + plen, msg, len - plen);
706 **echo_on = (type == PAM_PROMPT_ECHO_ON);
711 /* accumulate messages */
712 len = plen + mlen + 2;
713 **prompts = xrealloc(**prompts, len);
714 strlcpy(**prompts + plen, msg, len - plen);
716 strlcat(**prompts + plen, "\n", len - plen);
722 if (**prompts != NULL) {
723 /* drain any accumulated messages */
724 debug("PAM: %s", **prompts);
725 buffer_append(&loginmsg, **prompts,
730 if (type == PAM_SUCCESS) {
731 if (!sshpam_authctxt->valid ||
732 (sshpam_authctxt->pw->pw_uid == 0 &&
733 options.permit_root_login != PERMIT_YES))
734 fatal("Internal error: PAM auth "
735 "succeeded when it should have "
737 import_environments(&buffer);
744 error("PAM: %s for %s%.100s from %.100s", msg,
745 sshpam_authctxt->valid ? "" : "illegal user ",
746 sshpam_authctxt->user,
747 get_remote_name_or_ip(utmp_len, options.use_dns));
760 /* XXX - see also comment in auth-chall.c:verify_response */
762 sshpam_respond(void *ctx, u_int num, char **resp)
765 struct pam_ctxt *ctxt = ctx;
767 debug2("PAM: %s entering, %d responses", __func__, num);
768 switch (ctxt->pam_done) {
770 sshpam_authenticated = 1;
778 error("PAM: expected one response, got %u", num);
781 buffer_init(&buffer);
782 if (sshpam_authctxt->valid &&
783 (sshpam_authctxt->pw->pw_uid != 0 ||
784 options.permit_root_login == PERMIT_YES))
785 buffer_put_cstring(&buffer, *resp);
787 buffer_put_cstring(&buffer, badpw);
788 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
789 buffer_free(&buffer);
792 buffer_free(&buffer);
797 sshpam_free_ctx(void *ctxtp)
799 struct pam_ctxt *ctxt = ctxtp;
801 debug3("PAM: %s entering", __func__);
802 sshpam_thread_cleanup();
805 * We don't call sshpam_cleanup() here because we may need the PAM
806 * handle at a later stage, e.g. when setting up a session. It's
807 * still on the cleanup list, so pam_end() *will* be called before
808 * the server process terminates.
812 KbdintDevice sshpam_device = {
820 KbdintDevice mm_sshpam_device = {
829 * This replaces auth-pam.c
832 start_pam(Authctxt *authctxt)
834 if (!options.use_pam)
835 fatal("PAM: initialisation requested when UsePAM=no");
837 if (sshpam_init(authctxt) == -1)
838 fatal("PAM: initialisation failed");
850 debug("%s: called", __func__);
851 if (sshpam_account_status != -1)
852 return (sshpam_account_status);
854 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
855 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
856 pam_strerror(sshpam_handle, sshpam_err));
858 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
859 sshpam_account_status = 0;
860 return (sshpam_account_status);
863 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
864 sshpam_password_change_required(1);
866 sshpam_account_status = 1;
867 return (sshpam_account_status);
871 do_pam_set_tty(const char *tty)
874 debug("PAM: setting PAM_TTY to \"%s\"", tty);
875 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
876 if (sshpam_err != PAM_SUCCESS)
877 fatal("PAM: failed to set PAM_TTY: %s",
878 pam_strerror(sshpam_handle, sshpam_err));
883 do_pam_setcred(int init)
885 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
886 (const void *)&store_conv);
887 if (sshpam_err != PAM_SUCCESS)
888 fatal("PAM: failed to set PAM_CONV: %s",
889 pam_strerror(sshpam_handle, sshpam_err));
891 debug("PAM: establishing credentials");
892 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
894 debug("PAM: reinitializing credentials");
895 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
897 if (sshpam_err == PAM_SUCCESS) {
898 sshpam_cred_established = 1;
901 if (sshpam_authenticated)
902 fatal("PAM: pam_setcred(): %s",
903 pam_strerror(sshpam_handle, sshpam_err));
905 debug("PAM: pam_setcred(): %s",
906 pam_strerror(sshpam_handle, sshpam_err));
910 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
911 struct pam_response **resp, void *data)
913 char input[PAM_MAX_MSG_SIZE];
914 struct pam_response *reply;
917 debug3("PAM: %s called with %d messages", __func__, n);
921 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
922 return (PAM_CONV_ERR);
924 if ((reply = malloc(n * sizeof(*reply))) == NULL)
925 return (PAM_CONV_ERR);
926 memset(reply, 0, n * sizeof(*reply));
928 for (i = 0; i < n; ++i) {
929 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
930 case PAM_PROMPT_ECHO_OFF:
932 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
934 reply[i].resp_retcode = PAM_SUCCESS;
936 case PAM_PROMPT_ECHO_ON:
937 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
938 fgets(input, sizeof input, stdin);
939 if ((reply[i].resp = strdup(input)) == NULL)
941 reply[i].resp_retcode = PAM_SUCCESS;
945 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
946 reply[i].resp_retcode = PAM_SUCCESS;
953 return (PAM_SUCCESS);
956 for(i = 0; i < n; i++) {
957 if (reply[i].resp != NULL)
958 xfree(reply[i].resp);
961 return (PAM_CONV_ERR);
964 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
967 * XXX this should be done in the authentication phase, but ssh1 doesn't
971 do_pam_chauthtok(void)
974 fatal("Password expired (unable to change with privsep)");
975 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
976 (const void *)&tty_conv);
977 if (sshpam_err != PAM_SUCCESS)
978 fatal("PAM: failed to set PAM_CONV: %s",
979 pam_strerror(sshpam_handle, sshpam_err));
980 debug("PAM: changing password");
981 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
982 if (sshpam_err != PAM_SUCCESS)
983 fatal("PAM: pam_chauthtok(): %s",
984 pam_strerror(sshpam_handle, sshpam_err));
990 debug3("PAM: opening session");
991 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
992 (const void *)&store_conv);
993 if (sshpam_err != PAM_SUCCESS)
994 fatal("PAM: failed to set PAM_CONV: %s",
995 pam_strerror(sshpam_handle, sshpam_err));
996 sshpam_err = pam_open_session(sshpam_handle, 0);
997 if (sshpam_err == PAM_SUCCESS)
998 sshpam_session_open = 1;
1000 sshpam_session_open = 0;
1001 disable_forwarding();
1002 error("PAM: pam_open_session(): %s",
1003 pam_strerror(sshpam_handle, sshpam_err));
1009 is_pam_session_open(void)
1011 return sshpam_session_open;
1015 * Set a PAM environment string. We need to do this so that the session
1016 * modules can handle things like Kerberos/GSI credentials that appear
1017 * during the ssh authentication process.
1020 do_pam_putenv(char *name, char *value)
1023 #ifdef HAVE_PAM_PUTENV
1027 len = strlen(name) + strlen(value) + 2;
1028 compound = xmalloc(len);
1030 snprintf(compound, len, "%s=%s", name, value);
1031 ret = pam_putenv(sshpam_handle, compound);
1039 fetch_pam_child_environment(void)
1045 fetch_pam_environment(void)
1047 return (pam_getenvlist(sshpam_handle));
1051 free_pam_environment(char **env)
1058 for (envp = env; *envp; envp++)
1064 * "Blind" conversation function for password authentication. Assumes that
1065 * echo-off prompts are for the password and stores messages for later
1069 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1070 struct pam_response **resp, void *data)
1072 struct pam_response *reply;
1076 debug3("PAM: %s called with %d messages", __func__, n);
1080 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1081 return (PAM_CONV_ERR);
1083 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1084 return (PAM_CONV_ERR);
1085 memset(reply, 0, n * sizeof(*reply));
1087 for (i = 0; i < n; ++i) {
1088 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1089 case PAM_PROMPT_ECHO_OFF:
1090 if (sshpam_password == NULL)
1092 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1094 reply[i].resp_retcode = PAM_SUCCESS;
1098 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1100 buffer_append(&loginmsg,
1101 PAM_MSG_MEMBER(msg, i, msg), len);
1102 buffer_append(&loginmsg, "\n", 1);
1104 if ((reply[i].resp = strdup("")) == NULL)
1106 reply[i].resp_retcode = PAM_SUCCESS;
1113 return (PAM_SUCCESS);
1116 for(i = 0; i < n; i++) {
1117 if (reply[i].resp != NULL)
1118 xfree(reply[i].resp);
1121 return (PAM_CONV_ERR);
1124 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1127 * Attempt password authentication via PAM
1130 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1132 int flags = (options.permit_empty_passwd == 0 ?
1133 PAM_DISALLOW_NULL_AUTHTOK : 0);
1135 if (!options.use_pam || sshpam_handle == NULL)
1136 fatal("PAM: %s called when PAM disabled or failed to "
1137 "initialise.", __func__);
1139 sshpam_password = password;
1140 sshpam_authctxt = authctxt;
1143 * If the user logging in is invalid, or is root but is not permitted
1144 * by PermitRootLogin, use an invalid password to prevent leaking
1145 * information via timing (eg if the PAM config has a delay on fail).
1147 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1148 options.permit_root_login != PERMIT_YES))
1149 sshpam_password = badpw;
1151 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1152 (const void *)&passwd_conv);
1153 if (sshpam_err != PAM_SUCCESS)
1154 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1155 pam_strerror(sshpam_handle, sshpam_err));
1157 sshpam_err = pam_authenticate(sshpam_handle, flags);
1158 sshpam_password = NULL;
1159 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1160 debug("PAM: password authentication accepted for %.100s",
1164 debug("PAM: password authentication failed for %.100s: %s",
1165 authctxt->valid ? authctxt->user : "an illegal user",
1166 pam_strerror(sshpam_handle, sshpam_err));
1170 #endif /* USE_PAM */