2 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project by ThinkSec AS and
6 * NAI Labs, the Security Research Division of Network Associates, Inc.
7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8 * DARPA CHATS research program.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org>
33 * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au>
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
39 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
52 #include <sys/types.h>
58 #if defined(HAVE_SECURITY_PAM_APPL_H)
59 #include <security/pam_appl.h>
60 #elif defined (HAVE_PAM_PAM_APPL_H)
61 #include <pam/pam_appl.h>
64 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
65 #ifdef PAM_SUN_CODEBASE
66 # define sshpam_const /* Solaris, HP-UX, AIX */
68 # define sshpam_const const /* LinuxPAM, OpenPAM */
77 #include "monitor_wrap.h"
84 #include "auth-options.h"
86 extern ServerOptions options;
87 extern Buffer loginmsg;
89 extern u_int utmp_len;
91 /* so we don't silently change behaviour */
92 #ifdef USE_POSIX_THREADS
93 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
97 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
98 * and generally a bad idea. Use at own risk and do not expect support if
101 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
104 * Avoid namespace clash when *not* using pthreads for systems *with*
105 * pthreads, which unconditionally define pthread_t via sys/types.h
108 typedef pthread_t sp_pthread_t;
110 typedef pid_t sp_pthread_t;
114 sp_pthread_t pam_thread;
120 static void sshpam_free_ctx(void *);
121 static struct pam_ctxt *cleanup_ctxt;
123 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
125 * Simulate threads with processes.
128 static int sshpam_thread_status = -1;
129 static mysig_t sshpam_oldsig;
132 sshpam_sigchld_handler(int sig)
134 signal(SIGCHLD, SIG_DFL);
135 if (cleanup_ctxt == NULL)
136 return; /* handler called after PAM cleanup, shouldn't happen */
137 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
139 /* PAM thread has not exitted, privsep slave must have */
140 kill(cleanup_ctxt->pam_thread, SIGTERM);
141 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
143 return; /* could not wait */
145 if (WIFSIGNALED(sshpam_thread_status) &&
146 WTERMSIG(sshpam_thread_status) == SIGTERM)
147 return; /* terminated by pthread_cancel */
148 if (!WIFEXITED(sshpam_thread_status))
149 fatal("PAM: authentication thread exited unexpectedly");
150 if (WEXITSTATUS(sshpam_thread_status) != 0)
151 fatal("PAM: authentication thread exited uncleanly");
155 pthread_exit(void *value __unused)
161 pthread_create(sp_pthread_t *thread, const void *attr __unused,
162 void *(*thread_start)(void *), void *arg)
165 struct pam_ctxt *ctx = arg;
167 sshpam_thread_status = -1;
168 switch ((pid = fork())) {
170 error("fork(): %s", strerror(errno));
173 close(ctx->pam_psock);
179 close(ctx->pam_csock);
181 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
187 pthread_cancel(sp_pthread_t thread)
189 signal(SIGCHLD, sshpam_oldsig);
190 return (kill(thread, SIGTERM));
194 pthread_join(sp_pthread_t thread, void **value __unused)
198 if (sshpam_thread_status != -1)
199 return (sshpam_thread_status);
200 signal(SIGCHLD, sshpam_oldsig);
201 waitpid(thread, &status, 0);
207 static pam_handle_t *sshpam_handle = NULL;
208 static int sshpam_err = 0;
209 static int sshpam_authenticated = 0;
210 static int sshpam_session_open = 0;
211 static int sshpam_cred_established = 0;
212 static int sshpam_account_status = -1;
213 static char **sshpam_env = NULL;
214 static Authctxt *sshpam_authctxt = NULL;
215 static const char *sshpam_password = NULL;
216 static char badpw[] = "\b\n\r\177INCORRECT";
218 /* Some PAM implementations don't implement this */
219 #ifndef HAVE_PAM_GETENVLIST
221 pam_getenvlist(pam_handle_t *pamh)
224 * XXX - If necessary, we can still support envrionment passing
225 * for platforms without pam_getenvlist by searching for known
226 * env vars (e.g. KRB5CCNAME) from the PAM environment.
233 * Some platforms, notably Solaris, do not enforce password complexity
234 * rules during pam_chauthtok() if the real uid of the calling process
235 * is 0, on the assumption that it's being called by "passwd" run by root.
236 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
239 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
241 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
245 if (sshpam_authctxt == NULL)
246 fatal("PAM: sshpam_authctxt not initialized");
247 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
248 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
249 result = pam_chauthtok(pamh, flags);
250 if (setreuid(0, -1) == -1)
251 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
254 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
258 sshpam_password_change_required(int reqd)
260 debug3("%s %d", __func__, reqd);
261 if (sshpam_authctxt == NULL)
262 fatal("%s: PAM authctxt not initialized", __func__);
263 sshpam_authctxt->force_pwchange = reqd;
265 no_port_forwarding_flag |= 2;
266 no_agent_forwarding_flag |= 2;
267 no_x11_forwarding_flag |= 2;
269 no_port_forwarding_flag &= ~2;
270 no_agent_forwarding_flag &= ~2;
271 no_x11_forwarding_flag &= ~2;
275 /* Import regular and PAM environment from subprocess */
277 import_environments(Buffer *b)
283 debug3("PAM: %s entering", __func__);
285 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
286 /* Import variables set by do_pam_account */
287 sshpam_account_status = buffer_get_int(b);
288 sshpam_password_change_required(buffer_get_int(b));
290 /* Import environment from subprocess */
291 num_env = buffer_get_int(b);
292 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
293 debug3("PAM: num env strings %d", num_env);
294 for(i = 0; i < num_env; i++)
295 sshpam_env[i] = buffer_get_string(b, NULL);
297 sshpam_env[num_env] = NULL;
299 /* Import PAM environment from subprocess */
300 num_env = buffer_get_int(b);
301 debug("PAM: num PAM env strings %d", num_env);
302 for(i = 0; i < num_env; i++) {
303 env = buffer_get_string(b, NULL);
305 #ifdef HAVE_PAM_PUTENV
306 /* Errors are not fatal here */
307 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
308 error("PAM: pam_putenv: %s",
309 pam_strerror(sshpam_handle, sshpam_err));
317 * Conversation function for authentication thread.
320 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
321 struct pam_response **resp, void *data)
324 struct pam_ctxt *ctxt;
325 struct pam_response *reply;
328 debug3("PAM: %s entering, %d messages", __func__, n);
332 error("PAM: conversation function passed a null context");
333 return (PAM_CONV_ERR);
336 if (n <= 0 || n > PAM_MAX_NUM_MSG)
337 return (PAM_CONV_ERR);
339 if ((reply = malloc(n * sizeof(*reply))) == NULL)
340 return (PAM_CONV_ERR);
341 memset(reply, 0, n * sizeof(*reply));
343 buffer_init(&buffer);
344 for (i = 0; i < n; ++i) {
345 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
346 case PAM_PROMPT_ECHO_OFF:
347 buffer_put_cstring(&buffer,
348 PAM_MSG_MEMBER(msg, i, msg));
349 if (ssh_msg_send(ctxt->pam_csock,
350 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
352 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
354 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
356 reply[i].resp = buffer_get_string(&buffer, NULL);
358 case PAM_PROMPT_ECHO_ON:
359 buffer_put_cstring(&buffer,
360 PAM_MSG_MEMBER(msg, i, msg));
361 if (ssh_msg_send(ctxt->pam_csock,
362 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
364 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
366 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
368 reply[i].resp = buffer_get_string(&buffer, NULL);
371 buffer_put_cstring(&buffer,
372 PAM_MSG_MEMBER(msg, i, msg));
373 if (ssh_msg_send(ctxt->pam_csock,
374 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
378 buffer_put_cstring(&buffer,
379 PAM_MSG_MEMBER(msg, i, msg));
380 if (ssh_msg_send(ctxt->pam_csock,
381 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
387 buffer_clear(&buffer);
389 buffer_free(&buffer);
391 return (PAM_SUCCESS);
394 for(i = 0; i < n; i++) {
395 if (reply[i].resp != NULL)
396 xfree(reply[i].resp);
399 buffer_free(&buffer);
400 return (PAM_CONV_ERR);
404 * Authentication thread.
407 sshpam_thread(void *ctxtp)
409 struct pam_ctxt *ctxt = ctxtp;
411 struct pam_conv sshpam_conv;
412 int flags = (options.permit_empty_passwd == 0 ?
413 PAM_DISALLOW_NULL_AUTHTOK : 0);
414 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
415 extern char **environ;
418 const char *pam_user;
419 const char **ptr_pam_user = &pam_user;
421 pam_get_item(sshpam_handle, PAM_USER,
422 (sshpam_const void **)ptr_pam_user);
425 if (sshpam_authctxt != NULL) {
426 setproctitle("%s [pam]",
427 sshpam_authctxt->valid ? pam_user : "unknown");
431 sshpam_conv.conv = sshpam_thread_conv;
432 sshpam_conv.appdata_ptr = ctxt;
434 if (sshpam_authctxt == NULL)
435 fatal("%s: PAM authctxt not initialized", __func__);
437 buffer_init(&buffer);
438 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
439 (const void *)&sshpam_conv);
440 if (sshpam_err != PAM_SUCCESS)
442 sshpam_err = pam_authenticate(sshpam_handle, flags);
443 if (sshpam_err != PAM_SUCCESS)
447 if (!do_pam_account())
449 if (sshpam_authctxt->force_pwchange) {
450 sshpam_err = pam_chauthtok(sshpam_handle,
451 PAM_CHANGE_EXPIRED_AUTHTOK);
452 if (sshpam_err != PAM_SUCCESS)
454 sshpam_password_change_required(0);
458 buffer_put_cstring(&buffer, "OK");
460 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
461 /* Export variables set by do_pam_account */
462 buffer_put_int(&buffer, sshpam_account_status);
463 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
465 /* Export any environment strings set in child */
466 for(i = 0; environ[i] != NULL; i++)
468 buffer_put_int(&buffer, i);
469 for(i = 0; environ[i] != NULL; i++)
470 buffer_put_cstring(&buffer, environ[i]);
472 /* Export any environment strings set by PAM in child */
473 env_from_pam = pam_getenvlist(sshpam_handle);
474 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
476 buffer_put_int(&buffer, i);
477 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
478 buffer_put_cstring(&buffer, env_from_pam[i]);
479 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
481 /* XXX - can't do much about an error here */
482 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
483 buffer_free(&buffer);
487 buffer_put_cstring(&buffer,
488 pam_strerror(sshpam_handle, sshpam_err));
489 /* XXX - can't do much about an error here */
490 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
491 buffer_free(&buffer);
494 return (NULL); /* Avoid warning for non-pthread case */
498 sshpam_thread_cleanup(void)
500 struct pam_ctxt *ctxt = cleanup_ctxt;
502 debug3("PAM: %s entering", __func__);
503 if (ctxt != NULL && ctxt->pam_thread != 0) {
504 pthread_cancel(ctxt->pam_thread);
505 pthread_join(ctxt->pam_thread, NULL);
506 close(ctxt->pam_psock);
507 close(ctxt->pam_csock);
508 memset(ctxt, 0, sizeof(*ctxt));
514 sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
515 struct pam_response **resp, void *data)
517 debug3("PAM: %s entering, %d messages", __func__, n);
518 return (PAM_CONV_ERR);
521 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
524 sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
525 struct pam_response **resp, void *data)
527 struct pam_response *reply;
531 debug3("PAM: %s called with %d messages", __func__, n);
534 if (n <= 0 || n > PAM_MAX_NUM_MSG)
535 return (PAM_CONV_ERR);
537 if ((reply = malloc(n * sizeof(*reply))) == NULL)
538 return (PAM_CONV_ERR);
539 memset(reply, 0, n * sizeof(*reply));
541 for (i = 0; i < n; ++i) {
542 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
545 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
546 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
547 buffer_append(&loginmsg, "\n", 1 );
548 reply[i].resp_retcode = PAM_SUCCESS;
555 return (PAM_SUCCESS);
558 for(i = 0; i < n; i++) {
559 if (reply[i].resp != NULL)
560 xfree(reply[i].resp);
563 return (PAM_CONV_ERR);
566 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
571 debug("PAM: cleanup");
572 if (sshpam_handle == NULL)
574 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
575 if (sshpam_cred_established) {
576 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
577 sshpam_cred_established = 0;
579 if (sshpam_session_open) {
580 pam_close_session(sshpam_handle, PAM_SILENT);
581 sshpam_session_open = 0;
583 sshpam_authenticated = 0;
584 pam_end(sshpam_handle, sshpam_err);
585 sshpam_handle = NULL;
589 sshpam_init(Authctxt *authctxt)
591 extern char *__progname;
592 const char *pam_rhost, *pam_user, *user = authctxt->user;
593 const char **ptr_pam_user = &pam_user;
595 if (sshpam_handle != NULL) {
596 /* We already have a PAM context; check if the user matches */
597 sshpam_err = pam_get_item(sshpam_handle,
598 PAM_USER, (sshpam_const void **)ptr_pam_user);
599 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
601 pam_end(sshpam_handle, sshpam_err);
602 sshpam_handle = NULL;
604 debug("PAM: initializing for \"%s\"", user);
606 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
607 sshpam_authctxt = authctxt;
609 if (sshpam_err != PAM_SUCCESS) {
610 pam_end(sshpam_handle, sshpam_err);
611 sshpam_handle = NULL;
614 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
615 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
616 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
617 if (sshpam_err != PAM_SUCCESS) {
618 pam_end(sshpam_handle, sshpam_err);
619 sshpam_handle = NULL;
622 #ifdef PAM_TTY_KLUDGE
624 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
625 * sshd doesn't set the tty until too late in the auth process and
626 * may not even set one (for tty-less connections)
628 debug("PAM: setting PAM_TTY to \"ssh\"");
629 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
630 if (sshpam_err != PAM_SUCCESS) {
631 pam_end(sshpam_handle, sshpam_err);
632 sshpam_handle = NULL;
640 sshpam_init_ctx(Authctxt *authctxt)
642 struct pam_ctxt *ctxt;
645 debug3("PAM: %s entering", __func__);
646 /* Refuse to start if we don't have PAM enabled */
647 if (!options.use_pam)
651 if (sshpam_init(authctxt) == -1) {
652 error("PAM: initialization failed");
656 ctxt = xmalloc(sizeof *ctxt);
657 memset(ctxt, 0, sizeof(*ctxt));
659 /* Start the authentication thread */
660 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
661 error("PAM: failed create sockets: %s", strerror(errno));
665 ctxt->pam_psock = socks[0];
666 ctxt->pam_csock = socks[1];
667 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
668 error("PAM: failed to start authentication thread: %s",
680 sshpam_query(void *ctx, char **name, char **info,
681 u_int *num, char ***prompts, u_int **echo_on)
684 struct pam_ctxt *ctxt = ctx;
690 debug3("PAM: %s entering", __func__);
691 buffer_init(&buffer);
694 *prompts = xmalloc(sizeof(char *));
697 *echo_on = xmalloc(sizeof(u_int));
698 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
699 type = buffer_get_char(&buffer);
700 msg = buffer_get_string(&buffer, NULL);
703 case PAM_PROMPT_ECHO_ON:
704 case PAM_PROMPT_ECHO_OFF:
706 len = plen + mlen + 1;
707 **prompts = xrealloc(**prompts, len);
708 strlcpy(**prompts + plen, msg, len - plen);
710 **echo_on = (type == PAM_PROMPT_ECHO_ON);
715 /* accumulate messages */
716 len = plen + mlen + 2;
717 **prompts = xrealloc(**prompts, len);
718 strlcpy(**prompts + plen, msg, len - plen);
720 strlcat(**prompts + plen, "\n", len - plen);
725 debug3("PAM: PAM_AUTH_ERR");
726 if (**prompts != NULL && strlen(**prompts) != 0) {
736 if (**prompts != NULL) {
737 /* drain any accumulated messages */
738 debug("PAM: %s", **prompts);
739 buffer_append(&loginmsg, **prompts,
744 if (type == PAM_SUCCESS) {
745 if (!sshpam_authctxt->valid ||
746 (sshpam_authctxt->pw->pw_uid == 0 &&
747 options.permit_root_login != PERMIT_YES))
748 fatal("Internal error: PAM auth "
749 "succeeded when it should have "
751 import_environments(&buffer);
758 error("PAM: %s for %s%.100s from %.100s", msg,
759 sshpam_authctxt->valid ? "" : "illegal user ",
760 sshpam_authctxt->user,
761 get_remote_name_or_ip(utmp_len, options.use_dns));
774 /* XXX - see also comment in auth-chall.c:verify_response */
776 sshpam_respond(void *ctx, u_int num, char **resp)
779 struct pam_ctxt *ctxt = ctx;
781 debug2("PAM: %s entering, %u responses", __func__, num);
782 switch (ctxt->pam_done) {
784 sshpam_authenticated = 1;
792 error("PAM: expected one response, got %u", num);
795 buffer_init(&buffer);
796 if (sshpam_authctxt->valid &&
797 (sshpam_authctxt->pw->pw_uid != 0 ||
798 options.permit_root_login == PERMIT_YES))
799 buffer_put_cstring(&buffer, *resp);
801 buffer_put_cstring(&buffer, badpw);
802 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
803 buffer_free(&buffer);
806 buffer_free(&buffer);
811 sshpam_free_ctx(void *ctxtp)
813 struct pam_ctxt *ctxt = ctxtp;
815 debug3("PAM: %s entering", __func__);
816 sshpam_thread_cleanup();
819 * We don't call sshpam_cleanup() here because we may need the PAM
820 * handle at a later stage, e.g. when setting up a session. It's
821 * still on the cleanup list, so pam_end() *will* be called before
822 * the server process terminates.
826 KbdintDevice sshpam_device = {
834 KbdintDevice mm_sshpam_device = {
843 * This replaces auth-pam.c
846 start_pam(Authctxt *authctxt)
848 if (!options.use_pam)
849 fatal("PAM: initialisation requested when UsePAM=no");
851 if (sshpam_init(authctxt) == -1)
852 fatal("PAM: initialisation failed");
864 debug("%s: called", __func__);
865 if (sshpam_account_status != -1)
866 return (sshpam_account_status);
868 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
869 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
870 pam_strerror(sshpam_handle, sshpam_err));
872 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
873 sshpam_account_status = 0;
874 return (sshpam_account_status);
877 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
878 sshpam_password_change_required(1);
880 sshpam_account_status = 1;
881 return (sshpam_account_status);
885 do_pam_set_tty(const char *tty)
888 debug("PAM: setting PAM_TTY to \"%s\"", tty);
889 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
890 if (sshpam_err != PAM_SUCCESS)
891 fatal("PAM: failed to set PAM_TTY: %s",
892 pam_strerror(sshpam_handle, sshpam_err));
897 do_pam_setcred(int init)
899 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
900 (const void *)&store_conv);
901 if (sshpam_err != PAM_SUCCESS)
902 fatal("PAM: failed to set PAM_CONV: %s",
903 pam_strerror(sshpam_handle, sshpam_err));
905 debug("PAM: establishing credentials");
906 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
908 debug("PAM: reinitializing credentials");
909 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
911 if (sshpam_err == PAM_SUCCESS) {
912 sshpam_cred_established = 1;
915 if (sshpam_authenticated)
916 fatal("PAM: pam_setcred(): %s",
917 pam_strerror(sshpam_handle, sshpam_err));
919 debug("PAM: pam_setcred(): %s",
920 pam_strerror(sshpam_handle, sshpam_err));
924 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
925 struct pam_response **resp, void *data)
927 char input[PAM_MAX_MSG_SIZE];
928 struct pam_response *reply;
931 debug3("PAM: %s called with %d messages", __func__, n);
935 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
936 return (PAM_CONV_ERR);
938 if ((reply = malloc(n * sizeof(*reply))) == NULL)
939 return (PAM_CONV_ERR);
940 memset(reply, 0, n * sizeof(*reply));
942 for (i = 0; i < n; ++i) {
943 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
944 case PAM_PROMPT_ECHO_OFF:
946 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
948 reply[i].resp_retcode = PAM_SUCCESS;
950 case PAM_PROMPT_ECHO_ON:
951 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
952 fgets(input, sizeof input, stdin);
953 if ((reply[i].resp = strdup(input)) == NULL)
955 reply[i].resp_retcode = PAM_SUCCESS;
959 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
960 reply[i].resp_retcode = PAM_SUCCESS;
967 return (PAM_SUCCESS);
970 for(i = 0; i < n; i++) {
971 if (reply[i].resp != NULL)
972 xfree(reply[i].resp);
975 return (PAM_CONV_ERR);
978 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
981 * XXX this should be done in the authentication phase, but ssh1 doesn't
985 do_pam_chauthtok(void)
988 fatal("Password expired (unable to change with privsep)");
989 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
990 (const void *)&tty_conv);
991 if (sshpam_err != PAM_SUCCESS)
992 fatal("PAM: failed to set PAM_CONV: %s",
993 pam_strerror(sshpam_handle, sshpam_err));
994 debug("PAM: changing password");
995 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
996 if (sshpam_err != PAM_SUCCESS)
997 fatal("PAM: pam_chauthtok(): %s",
998 pam_strerror(sshpam_handle, sshpam_err));
1002 do_pam_session(void)
1004 debug3("PAM: opening session");
1005 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1006 (const void *)&store_conv);
1007 if (sshpam_err != PAM_SUCCESS)
1008 fatal("PAM: failed to set PAM_CONV: %s",
1009 pam_strerror(sshpam_handle, sshpam_err));
1010 sshpam_err = pam_open_session(sshpam_handle, 0);
1011 if (sshpam_err == PAM_SUCCESS)
1012 sshpam_session_open = 1;
1014 sshpam_session_open = 0;
1015 disable_forwarding();
1016 error("PAM: pam_open_session(): %s",
1017 pam_strerror(sshpam_handle, sshpam_err));
1023 is_pam_session_open(void)
1025 return sshpam_session_open;
1029 * Set a PAM environment string. We need to do this so that the session
1030 * modules can handle things like Kerberos/GSI credentials that appear
1031 * during the ssh authentication process.
1034 do_pam_putenv(char *name, char *value)
1037 #ifdef HAVE_PAM_PUTENV
1041 len = strlen(name) + strlen(value) + 2;
1042 compound = xmalloc(len);
1044 snprintf(compound, len, "%s=%s", name, value);
1045 ret = pam_putenv(sshpam_handle, compound);
1053 fetch_pam_child_environment(void)
1059 fetch_pam_environment(void)
1061 return (pam_getenvlist(sshpam_handle));
1065 free_pam_environment(char **env)
1072 for (envp = env; *envp; envp++)
1078 * "Blind" conversation function for password authentication. Assumes that
1079 * echo-off prompts are for the password and stores messages for later
1083 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1084 struct pam_response **resp, void *data)
1086 struct pam_response *reply;
1090 debug3("PAM: %s called with %d messages", __func__, n);
1094 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1095 return (PAM_CONV_ERR);
1097 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1098 return (PAM_CONV_ERR);
1099 memset(reply, 0, n * sizeof(*reply));
1101 for (i = 0; i < n; ++i) {
1102 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1103 case PAM_PROMPT_ECHO_OFF:
1104 if (sshpam_password == NULL)
1106 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1108 reply[i].resp_retcode = PAM_SUCCESS;
1112 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1114 buffer_append(&loginmsg,
1115 PAM_MSG_MEMBER(msg, i, msg), len);
1116 buffer_append(&loginmsg, "\n", 1);
1118 if ((reply[i].resp = strdup("")) == NULL)
1120 reply[i].resp_retcode = PAM_SUCCESS;
1127 return (PAM_SUCCESS);
1130 for(i = 0; i < n; i++) {
1131 if (reply[i].resp != NULL)
1132 xfree(reply[i].resp);
1135 return (PAM_CONV_ERR);
1138 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1141 * Attempt password authentication via PAM
1144 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1146 int flags = (options.permit_empty_passwd == 0 ?
1147 PAM_DISALLOW_NULL_AUTHTOK : 0);
1149 if (!options.use_pam || sshpam_handle == NULL)
1150 fatal("PAM: %s called when PAM disabled or failed to "
1151 "initialise.", __func__);
1153 sshpam_password = password;
1154 sshpam_authctxt = authctxt;
1157 * If the user logging in is invalid, or is root but is not permitted
1158 * by PermitRootLogin, use an invalid password to prevent leaking
1159 * information via timing (eg if the PAM config has a delay on fail).
1161 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1162 options.permit_root_login != PERMIT_YES))
1163 sshpam_password = badpw;
1165 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1166 (const void *)&passwd_conv);
1167 if (sshpam_err != PAM_SUCCESS)
1168 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1169 pam_strerror(sshpam_handle, sshpam_err));
1171 sshpam_err = pam_authenticate(sshpam_handle, flags);
1172 sshpam_password = NULL;
1173 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1174 debug("PAM: password authentication accepted for %.100s",
1178 debug("PAM: password authentication failed for %.100s: %s",
1179 authctxt->valid ? authctxt->user : "an illegal user",
1180 pam_strerror(sshpam_handle, sshpam_err));
1184 #endif /* USE_PAM */