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 */
52 #include <sys/types.h>
63 #if defined(HAVE_SECURITY_PAM_APPL_H)
64 #include <security/pam_appl.h>
65 #elif defined (HAVE_PAM_PAM_APPL_H)
66 #include <pam/pam_appl.h>
69 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
70 #ifdef PAM_SUN_CODEBASE
71 # define sshpam_const /* Solaris, HP-UX, AIX */
73 # define sshpam_const const /* LinuxPAM, OpenPAM */
76 /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
77 #ifdef PAM_SUN_CODEBASE
78 # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
80 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
96 #include "auth-options.h"
100 #include "monitor_wrap.h"
101 #include "blacklist_client.h"
103 extern ServerOptions options;
104 extern Buffer loginmsg;
106 extern u_int utmp_len;
108 /* so we don't silently change behaviour */
109 #ifdef USE_POSIX_THREADS
110 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
114 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
115 * and generally a bad idea. Use at own risk and do not expect support if
118 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
121 * Avoid namespace clash when *not* using pthreads for systems *with*
122 * pthreads, which unconditionally define pthread_t via sys/types.h
125 typedef pthread_t sp_pthread_t;
127 typedef pid_t sp_pthread_t;
131 sp_pthread_t pam_thread;
137 static void sshpam_free_ctx(void *);
138 static struct pam_ctxt *cleanup_ctxt;
140 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
142 * Simulate threads with processes.
145 static int sshpam_thread_status = -1;
146 static mysig_t sshpam_oldsig;
149 sshpam_sigchld_handler(int sig)
151 signal(SIGCHLD, SIG_DFL);
152 if (cleanup_ctxt == NULL)
153 return; /* handler called after PAM cleanup, shouldn't happen */
154 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
156 /* PAM thread has not exitted, privsep slave must have */
157 kill(cleanup_ctxt->pam_thread, SIGTERM);
158 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
160 return; /* could not wait */
162 if (WIFSIGNALED(sshpam_thread_status) &&
163 WTERMSIG(sshpam_thread_status) == SIGTERM)
164 return; /* terminated by pthread_cancel */
165 if (!WIFEXITED(sshpam_thread_status))
166 sigdie("PAM: authentication thread exited unexpectedly");
167 if (WEXITSTATUS(sshpam_thread_status) != 0)
168 sigdie("PAM: authentication thread exited uncleanly");
173 pthread_exit(void *value)
180 pthread_create(sp_pthread_t *thread, const void *attr,
181 void *(*thread_start)(void *), void *arg)
184 struct pam_ctxt *ctx = arg;
186 sshpam_thread_status = -1;
187 switch ((pid = fork())) {
189 error("fork(): %s", strerror(errno));
192 close(ctx->pam_psock);
198 close(ctx->pam_csock);
200 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
206 pthread_cancel(sp_pthread_t thread)
208 signal(SIGCHLD, sshpam_oldsig);
209 return (kill(thread, SIGTERM));
214 pthread_join(sp_pthread_t thread, void **value)
218 if (sshpam_thread_status != -1)
219 return (sshpam_thread_status);
220 signal(SIGCHLD, sshpam_oldsig);
221 waitpid(thread, &status, 0);
227 static pam_handle_t *sshpam_handle = NULL;
228 static int sshpam_err = 0;
229 static int sshpam_authenticated = 0;
230 static int sshpam_session_open = 0;
231 static int sshpam_cred_established = 0;
232 static int sshpam_account_status = -1;
233 static char **sshpam_env = NULL;
234 static Authctxt *sshpam_authctxt = NULL;
235 static const char *sshpam_password = NULL;
236 static char badpw[] = "\b\n\r\177INCORRECT";
238 /* Some PAM implementations don't implement this */
239 #ifndef HAVE_PAM_GETENVLIST
241 pam_getenvlist(pam_handle_t *pamh)
244 * XXX - If necessary, we can still support envrionment passing
245 * for platforms without pam_getenvlist by searching for known
246 * env vars (e.g. KRB5CCNAME) from the PAM environment.
253 * Some platforms, notably Solaris, do not enforce password complexity
254 * rules during pam_chauthtok() if the real uid of the calling process
255 * is 0, on the assumption that it's being called by "passwd" run by root.
256 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
259 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
261 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
265 if (sshpam_authctxt == NULL)
266 fatal("PAM: sshpam_authctxt not initialized");
267 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
268 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
269 result = pam_chauthtok(pamh, flags);
270 if (setreuid(0, -1) == -1)
271 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
274 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
278 sshpam_password_change_required(int reqd)
280 debug3("%s %d", __func__, reqd);
281 if (sshpam_authctxt == NULL)
282 fatal("%s: PAM authctxt not initialized", __func__);
283 sshpam_authctxt->force_pwchange = reqd;
285 no_port_forwarding_flag |= 2;
286 no_agent_forwarding_flag |= 2;
287 no_x11_forwarding_flag |= 2;
289 no_port_forwarding_flag &= ~2;
290 no_agent_forwarding_flag &= ~2;
291 no_x11_forwarding_flag &= ~2;
295 /* Import regular and PAM environment from subprocess */
297 import_environments(Buffer *b)
303 debug3("PAM: %s entering", __func__);
305 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
306 /* Import variables set by do_pam_account */
307 sshpam_account_status = buffer_get_int(b);
308 sshpam_password_change_required(buffer_get_int(b));
310 /* Import environment from subprocess */
311 num_env = buffer_get_int(b);
313 fatal("%s: received %u environment variables, expected <= 1024",
315 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
316 debug3("PAM: num env strings %d", num_env);
317 for(i = 0; i < num_env; i++)
318 sshpam_env[i] = buffer_get_string(b, NULL);
320 sshpam_env[num_env] = NULL;
322 /* Import PAM environment from subprocess */
323 num_env = buffer_get_int(b);
324 debug("PAM: num PAM env strings %d", num_env);
325 for(i = 0; i < num_env; i++) {
326 env = buffer_get_string(b, NULL);
328 #ifdef HAVE_PAM_PUTENV
329 /* Errors are not fatal here */
330 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
331 error("PAM: pam_putenv: %s",
332 pam_strerror(sshpam_handle, sshpam_err));
340 * Conversation function for authentication thread.
343 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
344 struct pam_response **resp, void *data)
347 struct pam_ctxt *ctxt;
348 struct pam_response *reply;
351 debug3("PAM: %s entering, %d messages", __func__, n);
355 error("PAM: conversation function passed a null context");
356 return (PAM_CONV_ERR);
359 if (n <= 0 || n > PAM_MAX_NUM_MSG)
360 return (PAM_CONV_ERR);
362 if ((reply = calloc(n, sizeof(*reply))) == NULL)
363 return (PAM_CONV_ERR);
365 buffer_init(&buffer);
366 for (i = 0; i < n; ++i) {
367 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
368 case PAM_PROMPT_ECHO_OFF:
369 buffer_put_cstring(&buffer,
370 PAM_MSG_MEMBER(msg, i, msg));
371 if (ssh_msg_send(ctxt->pam_csock,
372 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
374 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
376 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
378 reply[i].resp = buffer_get_string(&buffer, NULL);
380 case PAM_PROMPT_ECHO_ON:
381 buffer_put_cstring(&buffer,
382 PAM_MSG_MEMBER(msg, i, msg));
383 if (ssh_msg_send(ctxt->pam_csock,
384 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
386 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
388 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
390 reply[i].resp = buffer_get_string(&buffer, NULL);
393 buffer_put_cstring(&buffer,
394 PAM_MSG_MEMBER(msg, i, msg));
395 if (ssh_msg_send(ctxt->pam_csock,
396 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
400 buffer_put_cstring(&buffer,
401 PAM_MSG_MEMBER(msg, i, msg));
402 if (ssh_msg_send(ctxt->pam_csock,
403 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
409 buffer_clear(&buffer);
411 buffer_free(&buffer);
413 return (PAM_SUCCESS);
416 for(i = 0; i < n; i++) {
420 buffer_free(&buffer);
421 return (PAM_CONV_ERR);
425 * Authentication thread.
428 sshpam_thread(void *ctxtp)
430 struct pam_ctxt *ctxt = ctxtp;
432 struct pam_conv sshpam_conv;
433 int flags = (options.permit_empty_passwd == 0 ?
434 PAM_DISALLOW_NULL_AUTHTOK : 0);
435 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
436 extern char **environ;
439 const char *pam_user;
440 const char **ptr_pam_user = &pam_user;
441 char *tz = getenv("TZ");
443 sshpam_err = pam_get_item(sshpam_handle, PAM_USER,
444 (sshpam_const void **)ptr_pam_user);
445 if (sshpam_err != PAM_SUCCESS)
450 if (setenv("TZ", tz, 1) == -1)
451 error("PAM: could not set TZ environment: %s",
454 if (sshpam_authctxt != NULL) {
455 setproctitle("%s [pam]",
456 sshpam_authctxt->valid ? pam_user : "unknown");
460 sshpam_conv.conv = sshpam_thread_conv;
461 sshpam_conv.appdata_ptr = ctxt;
463 if (sshpam_authctxt == NULL)
464 fatal("%s: PAM authctxt not initialized", __func__);
466 buffer_init(&buffer);
467 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
468 (const void *)&sshpam_conv);
469 if (sshpam_err != PAM_SUCCESS)
471 sshpam_err = pam_authenticate(sshpam_handle, flags);
472 if (sshpam_err != PAM_SUCCESS)
476 if (!do_pam_account()) {
477 sshpam_err = PAM_ACCT_EXPIRED;
480 if (sshpam_authctxt->force_pwchange) {
481 sshpam_err = pam_chauthtok(sshpam_handle,
482 PAM_CHANGE_EXPIRED_AUTHTOK);
483 if (sshpam_err != PAM_SUCCESS)
485 sshpam_password_change_required(0);
489 buffer_put_cstring(&buffer, "OK");
491 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
492 /* Export variables set by do_pam_account */
493 buffer_put_int(&buffer, sshpam_account_status);
494 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
496 /* Export any environment strings set in child */
497 for(i = 0; environ[i] != NULL; i++)
499 buffer_put_int(&buffer, i);
500 for(i = 0; environ[i] != NULL; i++)
501 buffer_put_cstring(&buffer, environ[i]);
503 /* Export any environment strings set by PAM in child */
504 env_from_pam = pam_getenvlist(sshpam_handle);
505 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
507 buffer_put_int(&buffer, i);
508 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
509 buffer_put_cstring(&buffer, env_from_pam[i]);
510 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
512 /* XXX - can't do much about an error here */
513 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
514 buffer_free(&buffer);
518 buffer_put_cstring(&buffer,
519 pam_strerror(sshpam_handle, sshpam_err));
520 /* XXX - can't do much about an error here */
521 if (sshpam_err == PAM_ACCT_EXPIRED)
522 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
524 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
525 buffer_free(&buffer);
528 return (NULL); /* Avoid warning for non-pthread case */
532 sshpam_thread_cleanup(void)
534 struct pam_ctxt *ctxt = cleanup_ctxt;
536 debug3("PAM: %s entering", __func__);
537 if (ctxt != NULL && ctxt->pam_thread != 0) {
538 pthread_cancel(ctxt->pam_thread);
539 pthread_join(ctxt->pam_thread, NULL);
540 close(ctxt->pam_psock);
541 close(ctxt->pam_csock);
542 memset(ctxt, 0, sizeof(*ctxt));
548 sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
549 struct pam_response **resp, void *data)
551 debug3("PAM: %s entering, %d messages", __func__, n);
552 return (PAM_CONV_ERR);
555 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
558 sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
559 struct pam_response **resp, void *data)
561 struct pam_response *reply;
565 debug3("PAM: %s called with %d messages", __func__, n);
568 if (n <= 0 || n > PAM_MAX_NUM_MSG)
569 return (PAM_CONV_ERR);
571 if ((reply = calloc(n, sizeof(*reply))) == NULL)
572 return (PAM_CONV_ERR);
574 for (i = 0; i < n; ++i) {
575 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
578 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
579 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
580 buffer_append(&loginmsg, "\n", 1 );
581 reply[i].resp_retcode = PAM_SUCCESS;
588 return (PAM_SUCCESS);
591 for(i = 0; i < n; i++) {
595 return (PAM_CONV_ERR);
598 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
603 if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
605 debug("PAM: cleanup");
606 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
607 if (sshpam_session_open) {
608 debug("PAM: closing session");
609 pam_close_session(sshpam_handle, PAM_SILENT);
610 sshpam_session_open = 0;
612 if (sshpam_cred_established) {
613 debug("PAM: deleting credentials");
614 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
615 sshpam_cred_established = 0;
617 sshpam_authenticated = 0;
618 pam_end(sshpam_handle, sshpam_err);
619 sshpam_handle = NULL;
623 sshpam_init(Authctxt *authctxt)
625 extern char *__progname;
626 const char *pam_rhost, *pam_user, *user = authctxt->user;
627 const char **ptr_pam_user = &pam_user;
629 if (sshpam_handle != NULL) {
630 /* We already have a PAM context; check if the user matches */
631 sshpam_err = pam_get_item(sshpam_handle,
632 PAM_USER, (sshpam_const void **)ptr_pam_user);
633 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
635 pam_end(sshpam_handle, sshpam_err);
636 sshpam_handle = NULL;
638 debug("PAM: initializing for \"%s\"", user);
640 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
641 sshpam_authctxt = authctxt;
643 if (sshpam_err != PAM_SUCCESS) {
644 pam_end(sshpam_handle, sshpam_err);
645 sshpam_handle = NULL;
648 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
649 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
650 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
651 if (sshpam_err != PAM_SUCCESS) {
652 pam_end(sshpam_handle, sshpam_err);
653 sshpam_handle = NULL;
656 #ifdef PAM_TTY_KLUDGE
658 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
659 * sshd doesn't set the tty until too late in the auth process and
660 * may not even set one (for tty-less connections)
662 debug("PAM: setting PAM_TTY to \"ssh\"");
663 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
664 if (sshpam_err != PAM_SUCCESS) {
665 pam_end(sshpam_handle, sshpam_err);
666 sshpam_handle = NULL;
674 sshpam_init_ctx(Authctxt *authctxt)
676 struct pam_ctxt *ctxt;
679 debug3("PAM: %s entering", __func__);
681 * Refuse to start if we don't have PAM enabled or do_pam_account
682 * has previously failed.
684 if (!options.use_pam || sshpam_account_status == 0)
688 if (sshpam_init(authctxt) == -1) {
689 error("PAM: initialization failed");
693 ctxt = xcalloc(1, sizeof *ctxt);
695 /* Start the authentication thread */
696 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
697 error("PAM: failed create sockets: %s", strerror(errno));
701 ctxt->pam_psock = socks[0];
702 ctxt->pam_csock = socks[1];
703 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
704 error("PAM: failed to start authentication thread: %s",
716 sshpam_query(void *ctx, char **name, char **info,
717 u_int *num, char ***prompts, u_int **echo_on)
720 struct pam_ctxt *ctxt = ctx;
726 debug3("PAM: %s entering", __func__);
727 buffer_init(&buffer);
730 *prompts = xmalloc(sizeof(char *));
733 *echo_on = xmalloc(sizeof(u_int));
734 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
735 type = buffer_get_char(&buffer);
736 msg = buffer_get_string(&buffer, NULL);
739 case PAM_PROMPT_ECHO_ON:
740 case PAM_PROMPT_ECHO_OFF:
742 len = plen + mlen + 1;
743 **prompts = xreallocarray(**prompts, 1, len);
744 strlcpy(**prompts + plen, msg, len - plen);
746 **echo_on = (type == PAM_PROMPT_ECHO_ON);
751 /* accumulate messages */
752 len = plen + mlen + 2;
753 **prompts = xreallocarray(**prompts, 1, len);
754 strlcpy(**prompts + plen, msg, len - plen);
756 strlcat(**prompts + plen, "\n", len - plen);
760 case PAM_ACCT_EXPIRED:
761 sshpam_account_status = 0;
764 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
765 if (**prompts != NULL && strlen(**prompts) != 0) {
776 if (**prompts != NULL) {
777 /* drain any accumulated messages */
778 debug("PAM: %s", **prompts);
779 buffer_append(&loginmsg, **prompts,
784 if (type == PAM_SUCCESS) {
785 if (!sshpam_authctxt->valid ||
786 (sshpam_authctxt->pw->pw_uid == 0 &&
787 options.permit_root_login != PERMIT_YES))
788 fatal("Internal error: PAM auth "
789 "succeeded when it should have "
791 import_environments(&buffer);
798 BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
799 error("PAM: %s for %s%.100s from %.100s", msg,
800 sshpam_authctxt->valid ? "" : "illegal user ",
801 sshpam_authctxt->user,
802 get_remote_name_or_ip(utmp_len, options.use_dns));
815 /* XXX - see also comment in auth-chall.c:verify_response */
817 sshpam_respond(void *ctx, u_int num, char **resp)
820 struct pam_ctxt *ctxt = ctx;
822 debug2("PAM: %s entering, %u responses", __func__, num);
823 switch (ctxt->pam_done) {
825 sshpam_authenticated = 1;
833 error("PAM: expected one response, got %u", num);
836 buffer_init(&buffer);
837 if (sshpam_authctxt->valid &&
838 (sshpam_authctxt->pw->pw_uid != 0 ||
839 options.permit_root_login == PERMIT_YES))
840 buffer_put_cstring(&buffer, *resp);
842 buffer_put_cstring(&buffer, badpw);
843 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
844 buffer_free(&buffer);
847 buffer_free(&buffer);
852 sshpam_free_ctx(void *ctxtp)
854 struct pam_ctxt *ctxt = ctxtp;
856 debug3("PAM: %s entering", __func__);
857 sshpam_thread_cleanup();
860 * We don't call sshpam_cleanup() here because we may need the PAM
861 * handle at a later stage, e.g. when setting up a session. It's
862 * still on the cleanup list, so pam_end() *will* be called before
863 * the server process terminates.
867 KbdintDevice sshpam_device = {
875 KbdintDevice mm_sshpam_device = {
884 * This replaces auth-pam.c
887 start_pam(Authctxt *authctxt)
889 if (!options.use_pam)
890 fatal("PAM: initialisation requested when UsePAM=no");
892 if (sshpam_init(authctxt) == -1)
893 fatal("PAM: initialisation failed");
905 debug("%s: called", __func__);
906 if (sshpam_account_status != -1)
907 return (sshpam_account_status);
909 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
910 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
911 pam_strerror(sshpam_handle, sshpam_err));
913 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
914 sshpam_account_status = 0;
915 return (sshpam_account_status);
918 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
919 sshpam_password_change_required(1);
921 sshpam_account_status = 1;
922 return (sshpam_account_status);
926 do_pam_set_tty(const char *tty)
929 debug("PAM: setting PAM_TTY to \"%s\"", tty);
930 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
931 if (sshpam_err != PAM_SUCCESS)
932 fatal("PAM: failed to set PAM_TTY: %s",
933 pam_strerror(sshpam_handle, sshpam_err));
938 do_pam_setcred(int init)
940 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
941 (const void *)&store_conv);
942 if (sshpam_err != PAM_SUCCESS)
943 fatal("PAM: failed to set PAM_CONV: %s",
944 pam_strerror(sshpam_handle, sshpam_err));
946 debug("PAM: establishing credentials");
947 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
949 debug("PAM: reinitializing credentials");
950 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
952 if (sshpam_err == PAM_SUCCESS) {
953 sshpam_cred_established = 1;
956 if (sshpam_authenticated)
957 fatal("PAM: pam_setcred(): %s",
958 pam_strerror(sshpam_handle, sshpam_err));
960 debug("PAM: pam_setcred(): %s",
961 pam_strerror(sshpam_handle, sshpam_err));
965 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
966 struct pam_response **resp, void *data)
968 char input[PAM_MAX_MSG_SIZE];
969 struct pam_response *reply;
972 debug3("PAM: %s called with %d messages", __func__, n);
976 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
977 return (PAM_CONV_ERR);
979 if ((reply = calloc(n, sizeof(*reply))) == NULL)
980 return (PAM_CONV_ERR);
982 for (i = 0; i < n; ++i) {
983 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
984 case PAM_PROMPT_ECHO_OFF:
986 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
988 reply[i].resp_retcode = PAM_SUCCESS;
990 case PAM_PROMPT_ECHO_ON:
991 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
992 if (fgets(input, sizeof input, stdin) == NULL)
994 if ((reply[i].resp = strdup(input)) == NULL)
996 reply[i].resp_retcode = PAM_SUCCESS;
1000 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1001 reply[i].resp_retcode = PAM_SUCCESS;
1008 return (PAM_SUCCESS);
1011 for(i = 0; i < n; i++) {
1012 free(reply[i].resp);
1015 return (PAM_CONV_ERR);
1018 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
1021 * XXX this should be done in the authentication phase, but ssh1 doesn't
1025 do_pam_chauthtok(void)
1028 fatal("Password expired (unable to change with privsep)");
1029 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1030 (const void *)&tty_conv);
1031 if (sshpam_err != PAM_SUCCESS)
1032 fatal("PAM: failed to set PAM_CONV: %s",
1033 pam_strerror(sshpam_handle, sshpam_err));
1034 debug("PAM: changing password");
1035 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1036 if (sshpam_err != PAM_SUCCESS)
1037 fatal("PAM: pam_chauthtok(): %s",
1038 pam_strerror(sshpam_handle, sshpam_err));
1042 do_pam_session(void)
1044 debug3("PAM: opening session");
1045 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1046 (const void *)&store_conv);
1047 if (sshpam_err != PAM_SUCCESS)
1048 fatal("PAM: failed to set PAM_CONV: %s",
1049 pam_strerror(sshpam_handle, sshpam_err));
1050 sshpam_err = pam_open_session(sshpam_handle, 0);
1051 if (sshpam_err == PAM_SUCCESS)
1052 sshpam_session_open = 1;
1054 sshpam_session_open = 0;
1055 disable_forwarding();
1056 error("PAM: pam_open_session(): %s",
1057 pam_strerror(sshpam_handle, sshpam_err));
1063 is_pam_session_open(void)
1065 return sshpam_session_open;
1069 * Set a PAM environment string. We need to do this so that the session
1070 * modules can handle things like Kerberos/GSI credentials that appear
1071 * during the ssh authentication process.
1074 do_pam_putenv(char *name, char *value)
1077 #ifdef HAVE_PAM_PUTENV
1081 len = strlen(name) + strlen(value) + 2;
1082 compound = xmalloc(len);
1084 snprintf(compound, len, "%s=%s", name, value);
1085 ret = pam_putenv(sshpam_handle, compound);
1093 fetch_pam_child_environment(void)
1099 fetch_pam_environment(void)
1101 return (pam_getenvlist(sshpam_handle));
1105 free_pam_environment(char **env)
1112 for (envp = env; *envp; envp++)
1118 * "Blind" conversation function for password authentication. Assumes that
1119 * echo-off prompts are for the password and stores messages for later
1123 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1124 struct pam_response **resp, void *data)
1126 struct pam_response *reply;
1130 debug3("PAM: %s called with %d messages", __func__, n);
1134 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1135 return (PAM_CONV_ERR);
1137 if ((reply = calloc(n, sizeof(*reply))) == NULL)
1138 return (PAM_CONV_ERR);
1140 for (i = 0; i < n; ++i) {
1141 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1142 case PAM_PROMPT_ECHO_OFF:
1143 if (sshpam_password == NULL)
1145 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1147 reply[i].resp_retcode = PAM_SUCCESS;
1151 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1153 buffer_append(&loginmsg,
1154 PAM_MSG_MEMBER(msg, i, msg), len);
1155 buffer_append(&loginmsg, "\n", 1);
1157 if ((reply[i].resp = strdup("")) == NULL)
1159 reply[i].resp_retcode = PAM_SUCCESS;
1166 return (PAM_SUCCESS);
1169 for(i = 0; i < n; i++) {
1170 free(reply[i].resp);
1173 return (PAM_CONV_ERR);
1176 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1179 * Attempt password authentication via PAM
1182 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1184 int flags = (options.permit_empty_passwd == 0 ?
1185 PAM_DISALLOW_NULL_AUTHTOK : 0);
1187 if (!options.use_pam || sshpam_handle == NULL)
1188 fatal("PAM: %s called when PAM disabled or failed to "
1189 "initialise.", __func__);
1191 sshpam_password = password;
1192 sshpam_authctxt = authctxt;
1195 * If the user logging in is invalid, or is root but is not permitted
1196 * by PermitRootLogin, use an invalid password to prevent leaking
1197 * information via timing (eg if the PAM config has a delay on fail).
1199 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1200 options.permit_root_login != PERMIT_YES))
1201 sshpam_password = badpw;
1203 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1204 (const void *)&passwd_conv);
1205 if (sshpam_err != PAM_SUCCESS)
1206 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1207 pam_strerror(sshpam_handle, sshpam_err));
1209 sshpam_err = pam_authenticate(sshpam_handle, flags);
1210 sshpam_password = NULL;
1211 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1212 debug("PAM: password authentication accepted for %.100s",
1216 debug("PAM: password authentication failed for %.100s: %s",
1217 authctxt->valid ? authctxt->user : "an illegal user",
1218 pam_strerror(sshpam_handle, sshpam_err));
1222 #endif /* USE_PAM */