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.128 2006/01/29 05:46:13 dtucker 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);
721 debug3("PAM: PAM_AUTH_ERR");
722 if (**prompts != NULL && strlen(**prompts) != 0) {
732 if (**prompts != NULL) {
733 /* drain any accumulated messages */
734 debug("PAM: %s", **prompts);
735 buffer_append(&loginmsg, **prompts,
740 if (type == PAM_SUCCESS) {
741 if (!sshpam_authctxt->valid ||
742 (sshpam_authctxt->pw->pw_uid == 0 &&
743 options.permit_root_login != PERMIT_YES))
744 fatal("Internal error: PAM auth "
745 "succeeded when it should have "
747 import_environments(&buffer);
754 error("PAM: %s for %s%.100s from %.100s", msg,
755 sshpam_authctxt->valid ? "" : "illegal user ",
756 sshpam_authctxt->user,
757 get_remote_name_or_ip(utmp_len, options.use_dns));
770 /* XXX - see also comment in auth-chall.c:verify_response */
772 sshpam_respond(void *ctx, u_int num, char **resp)
775 struct pam_ctxt *ctxt = ctx;
777 debug2("PAM: %s entering, %u responses", __func__, num);
778 switch (ctxt->pam_done) {
780 sshpam_authenticated = 1;
788 error("PAM: expected one response, got %u", num);
791 buffer_init(&buffer);
792 if (sshpam_authctxt->valid &&
793 (sshpam_authctxt->pw->pw_uid != 0 ||
794 options.permit_root_login == PERMIT_YES))
795 buffer_put_cstring(&buffer, *resp);
797 buffer_put_cstring(&buffer, badpw);
798 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
799 buffer_free(&buffer);
802 buffer_free(&buffer);
807 sshpam_free_ctx(void *ctxtp)
809 struct pam_ctxt *ctxt = ctxtp;
811 debug3("PAM: %s entering", __func__);
812 sshpam_thread_cleanup();
815 * We don't call sshpam_cleanup() here because we may need the PAM
816 * handle at a later stage, e.g. when setting up a session. It's
817 * still on the cleanup list, so pam_end() *will* be called before
818 * the server process terminates.
822 KbdintDevice sshpam_device = {
830 KbdintDevice mm_sshpam_device = {
839 * This replaces auth-pam.c
842 start_pam(Authctxt *authctxt)
844 if (!options.use_pam)
845 fatal("PAM: initialisation requested when UsePAM=no");
847 if (sshpam_init(authctxt) == -1)
848 fatal("PAM: initialisation failed");
860 debug("%s: called", __func__);
861 if (sshpam_account_status != -1)
862 return (sshpam_account_status);
864 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
865 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
866 pam_strerror(sshpam_handle, sshpam_err));
868 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
869 sshpam_account_status = 0;
870 return (sshpam_account_status);
873 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
874 sshpam_password_change_required(1);
876 sshpam_account_status = 1;
877 return (sshpam_account_status);
881 do_pam_set_tty(const char *tty)
884 debug("PAM: setting PAM_TTY to \"%s\"", tty);
885 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
886 if (sshpam_err != PAM_SUCCESS)
887 fatal("PAM: failed to set PAM_TTY: %s",
888 pam_strerror(sshpam_handle, sshpam_err));
893 do_pam_setcred(int init)
895 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
896 (const void *)&store_conv);
897 if (sshpam_err != PAM_SUCCESS)
898 fatal("PAM: failed to set PAM_CONV: %s",
899 pam_strerror(sshpam_handle, sshpam_err));
901 debug("PAM: establishing credentials");
902 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
904 debug("PAM: reinitializing credentials");
905 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
907 if (sshpam_err == PAM_SUCCESS) {
908 sshpam_cred_established = 1;
911 if (sshpam_authenticated)
912 fatal("PAM: pam_setcred(): %s",
913 pam_strerror(sshpam_handle, sshpam_err));
915 debug("PAM: pam_setcred(): %s",
916 pam_strerror(sshpam_handle, sshpam_err));
920 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
921 struct pam_response **resp, void *data)
923 char input[PAM_MAX_MSG_SIZE];
924 struct pam_response *reply;
927 debug3("PAM: %s called with %d messages", __func__, n);
931 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
932 return (PAM_CONV_ERR);
934 if ((reply = malloc(n * sizeof(*reply))) == NULL)
935 return (PAM_CONV_ERR);
936 memset(reply, 0, n * sizeof(*reply));
938 for (i = 0; i < n; ++i) {
939 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
940 case PAM_PROMPT_ECHO_OFF:
942 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
944 reply[i].resp_retcode = PAM_SUCCESS;
946 case PAM_PROMPT_ECHO_ON:
947 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
948 fgets(input, sizeof input, stdin);
949 if ((reply[i].resp = strdup(input)) == NULL)
951 reply[i].resp_retcode = PAM_SUCCESS;
955 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
956 reply[i].resp_retcode = PAM_SUCCESS;
963 return (PAM_SUCCESS);
966 for(i = 0; i < n; i++) {
967 if (reply[i].resp != NULL)
968 xfree(reply[i].resp);
971 return (PAM_CONV_ERR);
974 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
977 * XXX this should be done in the authentication phase, but ssh1 doesn't
981 do_pam_chauthtok(void)
984 fatal("Password expired (unable to change with privsep)");
985 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
986 (const void *)&tty_conv);
987 if (sshpam_err != PAM_SUCCESS)
988 fatal("PAM: failed to set PAM_CONV: %s",
989 pam_strerror(sshpam_handle, sshpam_err));
990 debug("PAM: changing password");
991 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
992 if (sshpam_err != PAM_SUCCESS)
993 fatal("PAM: pam_chauthtok(): %s",
994 pam_strerror(sshpam_handle, sshpam_err));
1000 debug3("PAM: opening session");
1001 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1002 (const void *)&store_conv);
1003 if (sshpam_err != PAM_SUCCESS)
1004 fatal("PAM: failed to set PAM_CONV: %s",
1005 pam_strerror(sshpam_handle, sshpam_err));
1006 sshpam_err = pam_open_session(sshpam_handle, 0);
1007 if (sshpam_err == PAM_SUCCESS)
1008 sshpam_session_open = 1;
1010 sshpam_session_open = 0;
1011 disable_forwarding();
1012 error("PAM: pam_open_session(): %s",
1013 pam_strerror(sshpam_handle, sshpam_err));
1019 is_pam_session_open(void)
1021 return sshpam_session_open;
1025 * Set a PAM environment string. We need to do this so that the session
1026 * modules can handle things like Kerberos/GSI credentials that appear
1027 * during the ssh authentication process.
1030 do_pam_putenv(char *name, char *value)
1033 #ifdef HAVE_PAM_PUTENV
1037 len = strlen(name) + strlen(value) + 2;
1038 compound = xmalloc(len);
1040 snprintf(compound, len, "%s=%s", name, value);
1041 ret = pam_putenv(sshpam_handle, compound);
1049 fetch_pam_child_environment(void)
1055 fetch_pam_environment(void)
1057 return (pam_getenvlist(sshpam_handle));
1061 free_pam_environment(char **env)
1068 for (envp = env; *envp; envp++)
1074 * "Blind" conversation function for password authentication. Assumes that
1075 * echo-off prompts are for the password and stores messages for later
1079 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1080 struct pam_response **resp, void *data)
1082 struct pam_response *reply;
1086 debug3("PAM: %s called with %d messages", __func__, n);
1090 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1091 return (PAM_CONV_ERR);
1093 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1094 return (PAM_CONV_ERR);
1095 memset(reply, 0, n * sizeof(*reply));
1097 for (i = 0; i < n; ++i) {
1098 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1099 case PAM_PROMPT_ECHO_OFF:
1100 if (sshpam_password == NULL)
1102 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1104 reply[i].resp_retcode = PAM_SUCCESS;
1108 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1110 buffer_append(&loginmsg,
1111 PAM_MSG_MEMBER(msg, i, msg), len);
1112 buffer_append(&loginmsg, "\n", 1);
1114 if ((reply[i].resp = strdup("")) == NULL)
1116 reply[i].resp_retcode = PAM_SUCCESS;
1123 return (PAM_SUCCESS);
1126 for(i = 0; i < n; i++) {
1127 if (reply[i].resp != NULL)
1128 xfree(reply[i].resp);
1131 return (PAM_CONV_ERR);
1134 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1137 * Attempt password authentication via PAM
1140 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1142 int flags = (options.permit_empty_passwd == 0 ?
1143 PAM_DISALLOW_NULL_AUTHTOK : 0);
1145 if (!options.use_pam || sshpam_handle == NULL)
1146 fatal("PAM: %s called when PAM disabled or failed to "
1147 "initialise.", __func__);
1149 sshpam_password = password;
1150 sshpam_authctxt = authctxt;
1153 * If the user logging in is invalid, or is root but is not permitted
1154 * by PermitRootLogin, use an invalid password to prevent leaking
1155 * information via timing (eg if the PAM config has a delay on fail).
1157 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1158 options.permit_root_login != PERMIT_YES))
1159 sshpam_password = badpw;
1161 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1162 (const void *)&passwd_conv);
1163 if (sshpam_err != PAM_SUCCESS)
1164 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1165 pam_strerror(sshpam_handle, sshpam_err));
1167 sshpam_err = pam_authenticate(sshpam_handle, flags);
1168 sshpam_password = NULL;
1169 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1170 debug("PAM: password authentication accepted for %.100s",
1174 debug("PAM: password authentication failed for %.100s: %s",
1175 authctxt->valid ? authctxt->user : "an illegal user",
1176 pam_strerror(sshpam_handle, sshpam_err));
1180 #endif /* USE_PAM */