2 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project by ThinkSec AS and
6 * NAI Labs, the Security Research Division of Network Associates, Inc.
7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8 * DARPA CHATS research program.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org>
33 * Copyright (c) 2003,2004,2006 Darren Tucker <dtucker@zip.com.au>
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
39 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
51 #include <sys/types.h>
62 #if defined(HAVE_SECURITY_PAM_APPL_H)
63 #include <security/pam_appl.h>
64 #elif defined (HAVE_PAM_PAM_APPL_H)
65 #include <pam/pam_appl.h>
68 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
69 #ifdef PAM_SUN_CODEBASE
70 # define sshpam_const /* Solaris, HP-UX, AIX */
72 # define sshpam_const const /* LinuxPAM, OpenPAM */
75 /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
76 #ifdef PAM_SUN_CODEBASE
77 # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
79 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
95 #include "auth-options.h"
99 #include "monitor_wrap.h"
101 extern ServerOptions options;
102 extern Buffer loginmsg;
104 extern u_int utmp_len;
106 /* so we don't silently change behaviour */
107 #ifdef USE_POSIX_THREADS
108 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
112 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
113 * and generally a bad idea. Use at own risk and do not expect support if
116 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
119 * Avoid namespace clash when *not* using pthreads for systems *with*
120 * pthreads, which unconditionally define pthread_t via sys/types.h
123 typedef pthread_t sp_pthread_t;
125 typedef pid_t sp_pthread_t;
129 sp_pthread_t pam_thread;
135 static void sshpam_free_ctx(void *);
136 static struct pam_ctxt *cleanup_ctxt;
138 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
140 * Simulate threads with processes.
143 static int sshpam_thread_status = -1;
144 static mysig_t sshpam_oldsig;
147 sshpam_sigchld_handler(int sig)
149 signal(SIGCHLD, SIG_DFL);
150 if (cleanup_ctxt == NULL)
151 return; /* handler called after PAM cleanup, shouldn't happen */
152 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
154 /* PAM thread has not exitted, privsep slave must have */
155 kill(cleanup_ctxt->pam_thread, SIGTERM);
156 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
158 return; /* could not wait */
160 if (WIFSIGNALED(sshpam_thread_status) &&
161 WTERMSIG(sshpam_thread_status) == SIGTERM)
162 return; /* terminated by pthread_cancel */
163 if (!WIFEXITED(sshpam_thread_status))
164 sigdie("PAM: authentication thread exited unexpectedly");
165 if (WEXITSTATUS(sshpam_thread_status) != 0)
166 sigdie("PAM: authentication thread exited uncleanly");
171 pthread_exit(void *value)
178 pthread_create(sp_pthread_t *thread, const void *attr,
179 void *(*thread_start)(void *), void *arg)
182 struct pam_ctxt *ctx = arg;
184 sshpam_thread_status = -1;
185 switch ((pid = fork())) {
187 error("fork(): %s", strerror(errno));
190 close(ctx->pam_psock);
196 close(ctx->pam_csock);
198 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
204 pthread_cancel(sp_pthread_t thread)
206 signal(SIGCHLD, sshpam_oldsig);
207 return (kill(thread, SIGTERM));
212 pthread_join(sp_pthread_t thread, void **value)
216 if (sshpam_thread_status != -1)
217 return (sshpam_thread_status);
218 signal(SIGCHLD, sshpam_oldsig);
219 waitpid(thread, &status, 0);
225 static pam_handle_t *sshpam_handle = NULL;
226 static int sshpam_err = 0;
227 static int sshpam_authenticated = 0;
228 static int sshpam_session_open = 0;
229 static int sshpam_cred_established = 0;
230 static int sshpam_account_status = -1;
231 static char **sshpam_env = NULL;
232 static Authctxt *sshpam_authctxt = NULL;
233 static const char *sshpam_password = NULL;
234 static char badpw[] = "\b\n\r\177INCORRECT";
236 /* Some PAM implementations don't implement this */
237 #ifndef HAVE_PAM_GETENVLIST
239 pam_getenvlist(pam_handle_t *pamh)
242 * XXX - If necessary, we can still support envrionment passing
243 * for platforms without pam_getenvlist by searching for known
244 * env vars (e.g. KRB5CCNAME) from the PAM environment.
251 * Some platforms, notably Solaris, do not enforce password complexity
252 * rules during pam_chauthtok() if the real uid of the calling process
253 * is 0, on the assumption that it's being called by "passwd" run by root.
254 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
257 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
259 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
263 if (sshpam_authctxt == NULL)
264 fatal("PAM: sshpam_authctxt not initialized");
265 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
266 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
267 result = pam_chauthtok(pamh, flags);
268 if (setreuid(0, -1) == -1)
269 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
272 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
276 sshpam_getpw(const char *user)
280 if ((pw = getpwnam(user)) != NULL)
283 debug("PAM: faking passwd struct for user '%.100s'", user);
284 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
286 pw->pw_name = xstrdup(user); /* XXX leak */
287 pw->pw_shell = "/bin/true";
288 pw->pw_gecos = "sshd fake PAM user";
293 sshpam_check_userchanged(void)
299 debug("sshpam_check_userchanged");
300 sshpam_err = pam_get_item(sshpam_handle, PAM_USER, &user);
301 if (sshpam_err != PAM_SUCCESS)
302 fatal("PAM: could not get PAM_USER: %s",
303 pam_strerror(sshpam_handle, sshpam_err));
304 if (strcmp(user, sshpam_authctxt->pw->pw_name) != 0) {
305 debug("PAM: user mapped from '%.100s' to '%.100s'",
306 sshpam_authctxt->pw->pw_name, user);
307 if ((pw = getpwnam(user)) == NULL)
308 fatal("PAM: could not get passwd entry for user "
309 "'%.100s' provided by PAM_USER", user);
310 pwfree(sshpam_authctxt->pw);
311 sshpam_authctxt->pw = pw;
312 sshpam_authctxt->valid = allowed_user(pw);
313 debug("PAM: user '%.100s' now %svalid", user,
314 sshpam_authctxt->valid ? "" : "in");
319 sshpam_password_change_required(int reqd)
321 debug3("%s %d", __func__, reqd);
322 if (sshpam_authctxt == NULL)
323 fatal("%s: PAM authctxt not initialized", __func__);
324 sshpam_authctxt->force_pwchange = reqd;
326 no_port_forwarding_flag |= 2;
327 no_agent_forwarding_flag |= 2;
328 no_x11_forwarding_flag |= 2;
330 no_port_forwarding_flag &= ~2;
331 no_agent_forwarding_flag &= ~2;
332 no_x11_forwarding_flag &= ~2;
336 /* Import regular and PAM environment from subprocess */
338 import_environments(Buffer *b)
344 debug3("PAM: %s entering", __func__);
346 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
347 /* Import variables set by do_pam_account */
348 sshpam_account_status = buffer_get_int(b);
349 sshpam_password_change_required(buffer_get_int(b));
350 if (options.permit_pam_user_change) {
351 user = buffer_get_string(b, NULL);
352 debug("PAM: got username '%.100s' from thread", user);
353 if ((err = pam_set_item(sshpam_handle, PAM_USER, user)) != PAM_SUCCESS)
354 fatal("PAM: failed to set PAM_USER: %s",
355 pam_strerror(sshpam_handle, err));
356 pwfree(sshpam_authctxt->pw);
357 sshpam_authctxt->pw = pwcopy(sshpam_getpw(user));
360 /* Import environment from subprocess */
361 num_env = buffer_get_int(b);
363 fatal("%s: received %u environment variables, expected <= 1024",
365 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
366 debug3("PAM: num env strings %d", num_env);
367 for(i = 0; i < num_env; i++)
368 sshpam_env[i] = buffer_get_string(b, NULL);
370 sshpam_env[num_env] = NULL;
372 /* Import PAM environment from subprocess */
373 num_env = buffer_get_int(b);
374 debug("PAM: num PAM env strings %d", num_env);
375 for(i = 0; i < num_env; i++) {
376 env = buffer_get_string(b, NULL);
378 #ifdef HAVE_PAM_PUTENV
379 /* Errors are not fatal here */
380 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
381 error("PAM: pam_putenv: %s",
382 pam_strerror(sshpam_handle, sshpam_err));
390 * Conversation function for authentication thread.
393 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
394 struct pam_response **resp, void *data)
397 struct pam_ctxt *ctxt;
398 struct pam_response *reply;
401 debug3("PAM: %s entering, %d messages", __func__, n);
405 error("PAM: conversation function passed a null context");
406 return (PAM_CONV_ERR);
409 if (n <= 0 || n > PAM_MAX_NUM_MSG)
410 return (PAM_CONV_ERR);
412 if ((reply = calloc(n, sizeof(*reply))) == NULL)
413 return (PAM_CONV_ERR);
415 buffer_init(&buffer);
416 for (i = 0; i < n; ++i) {
417 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
418 case PAM_PROMPT_ECHO_OFF:
419 buffer_put_cstring(&buffer,
420 PAM_MSG_MEMBER(msg, i, msg));
421 if (ssh_msg_send(ctxt->pam_csock,
422 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
424 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
426 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
428 reply[i].resp = buffer_get_string(&buffer, NULL);
430 case PAM_PROMPT_ECHO_ON:
431 buffer_put_cstring(&buffer,
432 PAM_MSG_MEMBER(msg, i, msg));
433 if (ssh_msg_send(ctxt->pam_csock,
434 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
436 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
438 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
440 reply[i].resp = buffer_get_string(&buffer, NULL);
443 buffer_put_cstring(&buffer,
444 PAM_MSG_MEMBER(msg, i, msg));
445 if (ssh_msg_send(ctxt->pam_csock,
446 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
450 buffer_put_cstring(&buffer,
451 PAM_MSG_MEMBER(msg, i, msg));
452 if (ssh_msg_send(ctxt->pam_csock,
453 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
459 buffer_clear(&buffer);
461 buffer_free(&buffer);
463 return (PAM_SUCCESS);
466 for(i = 0; i < n; i++) {
467 if (reply[i].resp != NULL)
468 xfree(reply[i].resp);
471 buffer_free(&buffer);
472 return (PAM_CONV_ERR);
476 * Authentication thread.
479 sshpam_thread(void *ctxtp)
481 struct pam_ctxt *ctxt = ctxtp;
483 struct pam_conv sshpam_conv;
484 int flags = (options.permit_empty_passwd == 0 ?
485 PAM_DISALLOW_NULL_AUTHTOK : 0);
486 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
487 extern char **environ;
490 const char *pam_user;
491 const char **ptr_pam_user = &pam_user;
492 char *tz = getenv("TZ");
494 pam_get_item(sshpam_handle, PAM_USER,
495 (sshpam_const void **)ptr_pam_user);
499 if (setenv("TZ", tz, 1) == -1)
500 error("PAM: could not set TZ environment: %s",
503 if (sshpam_authctxt != NULL) {
504 setproctitle("%s [pam]",
505 sshpam_authctxt->valid ? pam_user : "unknown");
509 sshpam_conv.conv = sshpam_thread_conv;
510 sshpam_conv.appdata_ptr = ctxt;
512 if (sshpam_authctxt == NULL)
513 fatal("%s: PAM authctxt not initialized", __func__);
515 buffer_init(&buffer);
516 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
517 (const void *)&sshpam_conv);
518 if (sshpam_err != PAM_SUCCESS)
520 sshpam_err = pam_authenticate(sshpam_handle, flags);
521 if (sshpam_err != PAM_SUCCESS)
524 if (options.permit_pam_user_change) {
525 sshpam_check_userchanged();
528 if (!do_pam_account()) {
529 sshpam_err = PAM_ACCT_EXPIRED;
532 if (sshpam_authctxt->force_pwchange) {
533 sshpam_err = pam_chauthtok(sshpam_handle,
534 PAM_CHANGE_EXPIRED_AUTHTOK);
535 if (sshpam_err != PAM_SUCCESS)
537 sshpam_password_change_required(0);
541 buffer_put_cstring(&buffer, "OK");
543 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
544 /* Export variables set by do_pam_account */
545 buffer_put_int(&buffer, sshpam_account_status);
546 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
547 if (options.permit_pam_user_change) {
548 buffer_put_cstring(&buffer, sshpam_authctxt->pw->pw_name);
551 /* Export any environment strings set in child */
552 for(i = 0; environ[i] != NULL; i++)
554 buffer_put_int(&buffer, i);
555 for(i = 0; environ[i] != NULL; i++)
556 buffer_put_cstring(&buffer, environ[i]);
558 /* Export any environment strings set by PAM in child */
559 env_from_pam = pam_getenvlist(sshpam_handle);
560 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
562 buffer_put_int(&buffer, i);
563 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
564 buffer_put_cstring(&buffer, env_from_pam[i]);
565 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
567 /* XXX - can't do much about an error here */
568 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
569 buffer_free(&buffer);
573 buffer_put_cstring(&buffer,
574 pam_strerror(sshpam_handle, sshpam_err));
575 /* XXX - can't do much about an error here */
576 if (sshpam_err == PAM_ACCT_EXPIRED)
577 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
579 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
580 buffer_free(&buffer);
583 return (NULL); /* Avoid warning for non-pthread case */
587 sshpam_thread_cleanup(void)
589 struct pam_ctxt *ctxt = cleanup_ctxt;
591 debug3("PAM: %s entering", __func__);
592 if (ctxt != NULL && ctxt->pam_thread != 0) {
593 pthread_cancel(ctxt->pam_thread);
594 pthread_join(ctxt->pam_thread, NULL);
595 close(ctxt->pam_psock);
596 close(ctxt->pam_csock);
597 memset(ctxt, 0, sizeof(*ctxt));
603 sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
604 struct pam_response **resp, void *data)
606 debug3("PAM: %s entering, %d messages", __func__, n);
607 return (PAM_CONV_ERR);
610 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
613 sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
614 struct pam_response **resp, void *data)
616 struct pam_response *reply;
620 debug3("PAM: %s called with %d messages", __func__, n);
623 if (n <= 0 || n > PAM_MAX_NUM_MSG)
624 return (PAM_CONV_ERR);
626 if ((reply = calloc(n, sizeof(*reply))) == NULL)
627 return (PAM_CONV_ERR);
629 for (i = 0; i < n; ++i) {
630 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
633 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
634 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
635 buffer_append(&loginmsg, "\n", 1 );
636 reply[i].resp_retcode = PAM_SUCCESS;
643 return (PAM_SUCCESS);
646 for(i = 0; i < n; i++) {
647 if (reply[i].resp != NULL)
648 xfree(reply[i].resp);
651 return (PAM_CONV_ERR);
654 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
659 if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
661 debug("PAM: cleanup");
662 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
663 if (sshpam_session_open) {
664 debug("PAM: closing session");
665 pam_close_session(sshpam_handle, PAM_SILENT);
666 sshpam_session_open = 0;
668 if (sshpam_cred_established) {
669 debug("PAM: deleting credentials");
670 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
671 sshpam_cred_established = 0;
673 sshpam_authenticated = 0;
674 pam_end(sshpam_handle, sshpam_err);
675 sshpam_handle = NULL;
679 sshpam_init(Authctxt *authctxt)
681 extern char *__progname;
682 const char *pam_rhost, *pam_user, *user = authctxt->user;
683 const char **ptr_pam_user = &pam_user;
685 if (sshpam_handle != NULL) {
686 /* We already have a PAM context; check if the user matches */
687 sshpam_err = pam_get_item(sshpam_handle,
688 PAM_USER, (sshpam_const void **)ptr_pam_user);
689 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
691 pam_end(sshpam_handle, sshpam_err);
692 sshpam_handle = NULL;
694 debug("PAM: initializing for \"%s\"", user);
696 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
697 sshpam_authctxt = authctxt;
699 if (sshpam_err != PAM_SUCCESS) {
700 pam_end(sshpam_handle, sshpam_err);
701 sshpam_handle = NULL;
704 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
705 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
706 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
707 if (sshpam_err != PAM_SUCCESS) {
708 pam_end(sshpam_handle, sshpam_err);
709 sshpam_handle = NULL;
712 #ifdef PAM_TTY_KLUDGE
714 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
715 * sshd doesn't set the tty until too late in the auth process and
716 * may not even set one (for tty-less connections)
718 debug("PAM: setting PAM_TTY to \"ssh\"");
719 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
720 if (sshpam_err != PAM_SUCCESS) {
721 pam_end(sshpam_handle, sshpam_err);
722 sshpam_handle = NULL;
730 sshpam_init_ctx(Authctxt *authctxt)
732 struct pam_ctxt *ctxt;
735 debug3("PAM: %s entering", __func__);
737 * Refuse to start if we don't have PAM enabled or do_pam_account
738 * has previously failed.
740 if (!options.use_pam || sshpam_account_status == 0)
744 if (sshpam_init(authctxt) == -1) {
745 error("PAM: initialization failed");
749 ctxt = xcalloc(1, sizeof *ctxt);
751 /* Start the authentication thread */
752 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
753 error("PAM: failed create sockets: %s", strerror(errno));
757 ctxt->pam_psock = socks[0];
758 ctxt->pam_csock = socks[1];
759 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
760 error("PAM: failed to start authentication thread: %s",
772 sshpam_query(void *ctx, char **name, char **info,
773 u_int *num, char ***prompts, u_int **echo_on)
776 struct pam_ctxt *ctxt = ctx;
782 debug3("PAM: %s entering", __func__);
783 buffer_init(&buffer);
786 *prompts = xmalloc(sizeof(char *));
789 *echo_on = xmalloc(sizeof(u_int));
790 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
791 type = buffer_get_char(&buffer);
792 msg = buffer_get_string(&buffer, NULL);
795 case PAM_PROMPT_ECHO_ON:
796 case PAM_PROMPT_ECHO_OFF:
798 len = plen + mlen + 1;
799 **prompts = xrealloc(**prompts, 1, len);
800 strlcpy(**prompts + plen, msg, len - plen);
802 **echo_on = (type == PAM_PROMPT_ECHO_ON);
807 /* accumulate messages */
808 len = plen + mlen + 2;
809 **prompts = xrealloc(**prompts, 1, len);
810 strlcpy(**prompts + plen, msg, len - plen);
812 strlcat(**prompts + plen, "\n", len - plen);
816 case PAM_ACCT_EXPIRED:
817 sshpam_account_status = 0;
820 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
821 if (**prompts != NULL && strlen(**prompts) != 0) {
832 if (**prompts != NULL) {
833 /* drain any accumulated messages */
834 debug("PAM: %s", **prompts);
835 buffer_append(&loginmsg, **prompts,
840 if (type == PAM_SUCCESS) {
841 if (!sshpam_authctxt->valid ||
842 (sshpam_authctxt->pw->pw_uid == 0 &&
843 options.permit_root_login != PERMIT_YES))
844 fatal("Internal error: PAM auth "
845 "succeeded when it should have "
847 import_environments(&buffer);
854 error("PAM: %s for %s%.100s from %.100s", msg,
855 sshpam_authctxt->valid ? "" : "illegal user ",
856 sshpam_authctxt->user,
857 get_remote_name_or_ip(utmp_len, options.use_dns));
870 /* XXX - see also comment in auth-chall.c:verify_response */
872 sshpam_respond(void *ctx, u_int num, char **resp)
875 struct pam_ctxt *ctxt = ctx;
877 debug2("PAM: %s entering, %u responses", __func__, num);
878 switch (ctxt->pam_done) {
880 sshpam_authenticated = 1;
888 error("PAM: expected one response, got %u", num);
891 buffer_init(&buffer);
892 if (sshpam_authctxt->valid &&
893 (sshpam_authctxt->pw->pw_uid != 0 ||
894 options.permit_root_login == PERMIT_YES))
895 buffer_put_cstring(&buffer, *resp);
897 buffer_put_cstring(&buffer, badpw);
898 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
899 buffer_free(&buffer);
902 buffer_free(&buffer);
907 sshpam_free_ctx(void *ctxtp)
909 struct pam_ctxt *ctxt = ctxtp;
911 debug3("PAM: %s entering", __func__);
912 sshpam_thread_cleanup();
915 * We don't call sshpam_cleanup() here because we may need the PAM
916 * handle at a later stage, e.g. when setting up a session. It's
917 * still on the cleanup list, so pam_end() *will* be called before
918 * the server process terminates.
922 KbdintDevice sshpam_device = {
930 KbdintDevice mm_sshpam_device = {
939 * This replaces auth-pam.c
942 start_pam(Authctxt *authctxt)
944 if (!options.use_pam)
945 fatal("PAM: initialisation requested when UsePAM=no");
947 if (sshpam_init(authctxt) == -1)
948 fatal("PAM: initialisation failed");
960 debug("%s: called", __func__);
961 if (sshpam_account_status != -1)
962 return (sshpam_account_status);
964 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
965 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
966 pam_strerror(sshpam_handle, sshpam_err));
968 if (options.permit_pam_user_change) {
969 sshpam_check_userchanged();
970 if (getpwnam(sshpam_authctxt->pw->pw_name) == NULL)
971 fatal("PAM: completed authentication but PAM account invalid");
974 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
975 sshpam_account_status = 0;
976 return (sshpam_account_status);
979 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
980 sshpam_password_change_required(1);
982 sshpam_account_status = 1;
983 return (sshpam_account_status);
987 do_pam_set_tty(const char *tty)
990 debug("PAM: setting PAM_TTY to \"%s\"", tty);
991 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
992 if (sshpam_err != PAM_SUCCESS)
993 fatal("PAM: failed to set PAM_TTY: %s",
994 pam_strerror(sshpam_handle, sshpam_err));
999 do_pam_setcred(int init)
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));
1007 debug("PAM: establishing credentials");
1008 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
1010 debug("PAM: reinitializing credentials");
1011 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
1013 if (sshpam_err == PAM_SUCCESS) {
1014 sshpam_cred_established = 1;
1017 if (sshpam_authenticated)
1018 fatal("PAM: pam_setcred(): %s",
1019 pam_strerror(sshpam_handle, sshpam_err));
1021 debug("PAM: pam_setcred(): %s",
1022 pam_strerror(sshpam_handle, sshpam_err));
1026 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
1027 struct pam_response **resp, void *data)
1029 char input[PAM_MAX_MSG_SIZE];
1030 struct pam_response *reply;
1033 debug3("PAM: %s called with %d messages", __func__, n);
1037 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
1038 return (PAM_CONV_ERR);
1040 if ((reply = calloc(n, sizeof(*reply))) == NULL)
1041 return (PAM_CONV_ERR);
1043 for (i = 0; i < n; ++i) {
1044 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1045 case PAM_PROMPT_ECHO_OFF:
1047 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
1049 reply[i].resp_retcode = PAM_SUCCESS;
1051 case PAM_PROMPT_ECHO_ON:
1052 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1053 if (fgets(input, sizeof input, stdin) == NULL)
1055 if ((reply[i].resp = strdup(input)) == NULL)
1057 reply[i].resp_retcode = PAM_SUCCESS;
1061 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1062 reply[i].resp_retcode = PAM_SUCCESS;
1069 return (PAM_SUCCESS);
1072 for(i = 0; i < n; i++) {
1073 if (reply[i].resp != NULL)
1074 xfree(reply[i].resp);
1077 return (PAM_CONV_ERR);
1080 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
1083 * XXX this should be done in the authentication phase, but ssh1 doesn't
1087 do_pam_chauthtok(void)
1090 fatal("Password expired (unable to change with privsep)");
1091 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1092 (const void *)&tty_conv);
1093 if (sshpam_err != PAM_SUCCESS)
1094 fatal("PAM: failed to set PAM_CONV: %s",
1095 pam_strerror(sshpam_handle, sshpam_err));
1096 debug("PAM: changing password");
1097 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1098 if (sshpam_err != PAM_SUCCESS)
1099 fatal("PAM: pam_chauthtok(): %s",
1100 pam_strerror(sshpam_handle, sshpam_err));
1104 do_pam_session(void)
1106 debug3("PAM: opening session");
1107 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1108 (const void *)&store_conv);
1109 if (sshpam_err != PAM_SUCCESS)
1110 fatal("PAM: failed to set PAM_CONV: %s",
1111 pam_strerror(sshpam_handle, sshpam_err));
1112 sshpam_err = pam_open_session(sshpam_handle, 0);
1113 if (sshpam_err == PAM_SUCCESS)
1114 sshpam_session_open = 1;
1116 sshpam_session_open = 0;
1117 disable_forwarding();
1118 error("PAM: pam_open_session(): %s",
1119 pam_strerror(sshpam_handle, sshpam_err));
1125 is_pam_session_open(void)
1127 return sshpam_session_open;
1131 * Set a PAM environment string. We need to do this so that the session
1132 * modules can handle things like Kerberos/GSI credentials that appear
1133 * during the ssh authentication process.
1136 do_pam_putenv(char *name, char *value)
1139 #ifdef HAVE_PAM_PUTENV
1143 len = strlen(name) + strlen(value) + 2;
1144 compound = xmalloc(len);
1146 snprintf(compound, len, "%s=%s", name, value);
1147 ret = pam_putenv(sshpam_handle, compound);
1155 fetch_pam_child_environment(void)
1161 fetch_pam_environment(void)
1163 return (pam_getenvlist(sshpam_handle));
1167 free_pam_environment(char **env)
1174 for (envp = env; *envp; envp++)
1180 * "Blind" conversation function for password authentication. Assumes that
1181 * echo-off prompts are for the password and stores messages for later
1185 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1186 struct pam_response **resp, void *data)
1188 struct pam_response *reply;
1192 debug3("PAM: %s called with %d messages", __func__, n);
1196 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1197 return (PAM_CONV_ERR);
1199 if ((reply = calloc(n, sizeof(*reply))) == NULL)
1200 return (PAM_CONV_ERR);
1202 for (i = 0; i < n; ++i) {
1203 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1204 case PAM_PROMPT_ECHO_OFF:
1205 if (sshpam_password == NULL)
1207 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1209 reply[i].resp_retcode = PAM_SUCCESS;
1213 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1215 buffer_append(&loginmsg,
1216 PAM_MSG_MEMBER(msg, i, msg), len);
1217 buffer_append(&loginmsg, "\n", 1);
1219 if ((reply[i].resp = strdup("")) == NULL)
1221 reply[i].resp_retcode = PAM_SUCCESS;
1228 return (PAM_SUCCESS);
1231 for(i = 0; i < n; i++) {
1232 if (reply[i].resp != NULL)
1233 xfree(reply[i].resp);
1236 return (PAM_CONV_ERR);
1239 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1242 * Attempt password authentication via PAM
1245 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1247 int flags = (options.permit_empty_passwd == 0 ?
1248 PAM_DISALLOW_NULL_AUTHTOK : 0);
1250 if (!options.use_pam || sshpam_handle == NULL)
1251 fatal("PAM: %s called when PAM disabled or failed to "
1252 "initialise.", __func__);
1254 sshpam_password = password;
1255 sshpam_authctxt = authctxt;
1258 * If the user logging in is invalid, or is root but is not permitted
1259 * by PermitRootLogin, use an invalid password to prevent leaking
1260 * information via timing (eg if the PAM config has a delay on fail).
1262 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1263 options.permit_root_login != PERMIT_YES))
1264 sshpam_password = badpw;
1266 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1267 (const void *)&passwd_conv);
1268 if (sshpam_err != PAM_SUCCESS)
1269 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1270 pam_strerror(sshpam_handle, sshpam_err));
1272 sshpam_err = pam_authenticate(sshpam_handle, flags);
1273 if (options.permit_pam_user_change) {
1274 sshpam_check_userchanged();
1276 sshpam_password = NULL;
1277 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1278 debug("PAM: password authentication accepted for %.100s",
1282 debug("PAM: password authentication failed for %.100s: %s",
1283 authctxt->valid ? authctxt->user : "an illegal user",
1284 pam_strerror(sshpam_handle, sshpam_err));
1288 #endif /* USE_PAM */