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 $ */
51 #include <sys/types.h>
61 #if defined(HAVE_SECURITY_PAM_APPL_H)
62 #include <security/pam_appl.h>
63 #elif defined (HAVE_PAM_PAM_APPL_H)
64 #include <pam/pam_appl.h>
67 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
68 #ifdef PAM_SUN_CODEBASE
69 # define sshpam_const /* Solaris, HP-UX, AIX */
71 # define sshpam_const const /* LinuxPAM, OpenPAM */
74 /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
75 #ifdef PAM_SUN_CODEBASE
76 # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
78 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
87 #include "monitor_wrap.h"
94 #include "auth-options.h"
96 extern ServerOptions options;
97 extern Buffer loginmsg;
99 extern u_int utmp_len;
101 /* so we don't silently change behaviour */
102 #ifdef USE_POSIX_THREADS
103 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
107 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
108 * and generally a bad idea. Use at own risk and do not expect support if
111 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
114 * Avoid namespace clash when *not* using pthreads for systems *with*
115 * pthreads, which unconditionally define pthread_t via sys/types.h
118 typedef pthread_t sp_pthread_t;
120 typedef pid_t sp_pthread_t;
124 sp_pthread_t pam_thread;
130 static void sshpam_free_ctx(void *);
131 static struct pam_ctxt *cleanup_ctxt;
133 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
135 * Simulate threads with processes.
138 static int sshpam_thread_status = -1;
139 static mysig_t sshpam_oldsig;
142 sshpam_sigchld_handler(int sig)
144 signal(SIGCHLD, SIG_DFL);
145 if (cleanup_ctxt == NULL)
146 return; /* handler called after PAM cleanup, shouldn't happen */
147 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
149 /* PAM thread has not exitted, privsep slave must have */
150 kill(cleanup_ctxt->pam_thread, SIGTERM);
151 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
153 return; /* could not wait */
155 if (WIFSIGNALED(sshpam_thread_status) &&
156 WTERMSIG(sshpam_thread_status) == SIGTERM)
157 return; /* terminated by pthread_cancel */
158 if (!WIFEXITED(sshpam_thread_status))
159 fatal("PAM: authentication thread exited unexpectedly");
160 if (WEXITSTATUS(sshpam_thread_status) != 0)
161 fatal("PAM: authentication thread exited uncleanly");
166 pthread_exit(void *value)
173 pthread_create(sp_pthread_t *thread, const void *attr,
174 void *(*thread_start)(void *), void *arg)
177 struct pam_ctxt *ctx = arg;
179 sshpam_thread_status = -1;
180 switch ((pid = fork())) {
182 error("fork(): %s", strerror(errno));
185 close(ctx->pam_psock);
191 close(ctx->pam_csock);
193 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
199 pthread_cancel(sp_pthread_t thread)
201 signal(SIGCHLD, sshpam_oldsig);
202 return (kill(thread, SIGTERM));
207 pthread_join(sp_pthread_t thread, void **value)
211 if (sshpam_thread_status != -1)
212 return (sshpam_thread_status);
213 signal(SIGCHLD, sshpam_oldsig);
214 waitpid(thread, &status, 0);
220 static pam_handle_t *sshpam_handle = NULL;
221 static int sshpam_err = 0;
222 static int sshpam_authenticated = 0;
223 static int sshpam_session_open = 0;
224 static int sshpam_cred_established = 0;
225 static int sshpam_account_status = -1;
226 static char **sshpam_env = NULL;
227 static Authctxt *sshpam_authctxt = NULL;
228 static const char *sshpam_password = NULL;
229 static char badpw[] = "\b\n\r\177INCORRECT";
231 /* Some PAM implementations don't implement this */
232 #ifndef HAVE_PAM_GETENVLIST
234 pam_getenvlist(pam_handle_t *pamh)
237 * XXX - If necessary, we can still support envrionment passing
238 * for platforms without pam_getenvlist by searching for known
239 * env vars (e.g. KRB5CCNAME) from the PAM environment.
246 * Some platforms, notably Solaris, do not enforce password complexity
247 * rules during pam_chauthtok() if the real uid of the calling process
248 * is 0, on the assumption that it's being called by "passwd" run by root.
249 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
252 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
254 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
258 if (sshpam_authctxt == NULL)
259 fatal("PAM: sshpam_authctxt not initialized");
260 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
261 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
262 result = pam_chauthtok(pamh, flags);
263 if (setreuid(0, -1) == -1)
264 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
267 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
271 sshpam_password_change_required(int reqd)
273 debug3("%s %d", __func__, reqd);
274 if (sshpam_authctxt == NULL)
275 fatal("%s: PAM authctxt not initialized", __func__);
276 sshpam_authctxt->force_pwchange = reqd;
278 no_port_forwarding_flag |= 2;
279 no_agent_forwarding_flag |= 2;
280 no_x11_forwarding_flag |= 2;
282 no_port_forwarding_flag &= ~2;
283 no_agent_forwarding_flag &= ~2;
284 no_x11_forwarding_flag &= ~2;
288 /* Import regular and PAM environment from subprocess */
290 import_environments(Buffer *b)
296 debug3("PAM: %s entering", __func__);
298 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
299 /* Import variables set by do_pam_account */
300 sshpam_account_status = buffer_get_int(b);
301 sshpam_password_change_required(buffer_get_int(b));
303 /* Import environment from subprocess */
304 num_env = buffer_get_int(b);
306 fatal("%s: received %u environment variables, expected <= 1024",
308 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
309 debug3("PAM: num env strings %d", num_env);
310 for(i = 0; i < num_env; i++)
311 sshpam_env[i] = buffer_get_string(b, NULL);
313 sshpam_env[num_env] = NULL;
315 /* Import PAM environment from subprocess */
316 num_env = buffer_get_int(b);
317 debug("PAM: num PAM env strings %d", num_env);
318 for(i = 0; i < num_env; i++) {
319 env = buffer_get_string(b, NULL);
321 #ifdef HAVE_PAM_PUTENV
322 /* Errors are not fatal here */
323 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
324 error("PAM: pam_putenv: %s",
325 pam_strerror(sshpam_handle, sshpam_err));
333 * Conversation function for authentication thread.
336 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
337 struct pam_response **resp, void *data)
340 struct pam_ctxt *ctxt;
341 struct pam_response *reply;
344 debug3("PAM: %s entering, %d messages", __func__, n);
348 error("PAM: conversation function passed a null context");
349 return (PAM_CONV_ERR);
352 if (n <= 0 || n > PAM_MAX_NUM_MSG)
353 return (PAM_CONV_ERR);
355 if ((reply = calloc(n, sizeof(*reply))) == NULL)
356 return (PAM_CONV_ERR);
358 buffer_init(&buffer);
359 for (i = 0; i < n; ++i) {
360 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
361 case PAM_PROMPT_ECHO_OFF:
362 buffer_put_cstring(&buffer,
363 PAM_MSG_MEMBER(msg, i, msg));
364 if (ssh_msg_send(ctxt->pam_csock,
365 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
367 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
369 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
371 reply[i].resp = buffer_get_string(&buffer, NULL);
373 case PAM_PROMPT_ECHO_ON:
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)
379 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
381 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
383 reply[i].resp = buffer_get_string(&buffer, NULL);
386 buffer_put_cstring(&buffer,
387 PAM_MSG_MEMBER(msg, i, msg));
388 if (ssh_msg_send(ctxt->pam_csock,
389 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
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)
402 buffer_clear(&buffer);
404 buffer_free(&buffer);
406 return (PAM_SUCCESS);
409 for(i = 0; i < n; i++) {
410 if (reply[i].resp != NULL)
411 xfree(reply[i].resp);
414 buffer_free(&buffer);
415 return (PAM_CONV_ERR);
419 * Authentication thread.
422 sshpam_thread(void *ctxtp)
424 struct pam_ctxt *ctxt = ctxtp;
426 struct pam_conv sshpam_conv;
427 int flags = (options.permit_empty_passwd == 0 ?
428 PAM_DISALLOW_NULL_AUTHTOK : 0);
429 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
430 extern char **environ;
433 const char *pam_user;
434 const char **ptr_pam_user = &pam_user;
436 pam_get_item(sshpam_handle, PAM_USER,
437 (sshpam_const void **)ptr_pam_user);
440 if (sshpam_authctxt != NULL) {
441 setproctitle("%s [pam]",
442 sshpam_authctxt->valid ? pam_user : "unknown");
446 sshpam_conv.conv = sshpam_thread_conv;
447 sshpam_conv.appdata_ptr = ctxt;
449 if (sshpam_authctxt == NULL)
450 fatal("%s: PAM authctxt not initialized", __func__);
452 buffer_init(&buffer);
453 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
454 (const void *)&sshpam_conv);
455 if (sshpam_err != PAM_SUCCESS)
457 sshpam_err = pam_authenticate(sshpam_handle, flags);
458 if (sshpam_err != PAM_SUCCESS)
462 if (!do_pam_account()) {
463 sshpam_err = PAM_ACCT_EXPIRED;
466 if (sshpam_authctxt->force_pwchange) {
467 sshpam_err = pam_chauthtok(sshpam_handle,
468 PAM_CHANGE_EXPIRED_AUTHTOK);
469 if (sshpam_err != PAM_SUCCESS)
471 sshpam_password_change_required(0);
475 buffer_put_cstring(&buffer, "OK");
477 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
478 /* Export variables set by do_pam_account */
479 buffer_put_int(&buffer, sshpam_account_status);
480 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
482 /* Export any environment strings set in child */
483 for(i = 0; environ[i] != NULL; i++)
485 buffer_put_int(&buffer, i);
486 for(i = 0; environ[i] != NULL; i++)
487 buffer_put_cstring(&buffer, environ[i]);
489 /* Export any environment strings set by PAM in child */
490 env_from_pam = pam_getenvlist(sshpam_handle);
491 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
493 buffer_put_int(&buffer, i);
494 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
495 buffer_put_cstring(&buffer, env_from_pam[i]);
496 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
498 /* XXX - can't do much about an error here */
499 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
500 buffer_free(&buffer);
504 buffer_put_cstring(&buffer,
505 pam_strerror(sshpam_handle, sshpam_err));
506 /* XXX - can't do much about an error here */
507 if (sshpam_err == PAM_ACCT_EXPIRED)
508 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
510 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
511 buffer_free(&buffer);
514 return (NULL); /* Avoid warning for non-pthread case */
518 sshpam_thread_cleanup(void)
520 struct pam_ctxt *ctxt = cleanup_ctxt;
522 debug3("PAM: %s entering", __func__);
523 if (ctxt != NULL && ctxt->pam_thread != 0) {
524 pthread_cancel(ctxt->pam_thread);
525 pthread_join(ctxt->pam_thread, NULL);
526 close(ctxt->pam_psock);
527 close(ctxt->pam_csock);
528 memset(ctxt, 0, sizeof(*ctxt));
534 sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
535 struct pam_response **resp, void *data)
537 debug3("PAM: %s entering, %d messages", __func__, n);
538 return (PAM_CONV_ERR);
541 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
544 sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
545 struct pam_response **resp, void *data)
547 struct pam_response *reply;
551 debug3("PAM: %s called with %d messages", __func__, n);
554 if (n <= 0 || n > PAM_MAX_NUM_MSG)
555 return (PAM_CONV_ERR);
557 if ((reply = calloc(n, sizeof(*reply))) == NULL)
558 return (PAM_CONV_ERR);
560 for (i = 0; i < n; ++i) {
561 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
564 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
565 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
566 buffer_append(&loginmsg, "\n", 1 );
567 reply[i].resp_retcode = PAM_SUCCESS;
574 return (PAM_SUCCESS);
577 for(i = 0; i < n; i++) {
578 if (reply[i].resp != NULL)
579 xfree(reply[i].resp);
582 return (PAM_CONV_ERR);
585 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
590 debug("PAM: cleanup");
591 if (sshpam_handle == NULL)
593 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
594 if (sshpam_cred_established) {
595 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
596 sshpam_cred_established = 0;
598 if (sshpam_session_open) {
599 pam_close_session(sshpam_handle, PAM_SILENT);
600 sshpam_session_open = 0;
602 sshpam_authenticated = 0;
603 pam_end(sshpam_handle, sshpam_err);
604 sshpam_handle = NULL;
608 sshpam_init(Authctxt *authctxt)
610 extern char *__progname;
611 const char *pam_rhost, *pam_user, *user = authctxt->user;
612 const char **ptr_pam_user = &pam_user;
614 if (sshpam_handle != NULL) {
615 /* We already have a PAM context; check if the user matches */
616 sshpam_err = pam_get_item(sshpam_handle,
617 PAM_USER, (sshpam_const void **)ptr_pam_user);
618 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
620 pam_end(sshpam_handle, sshpam_err);
621 sshpam_handle = NULL;
623 debug("PAM: initializing for \"%s\"", user);
625 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
626 sshpam_authctxt = authctxt;
628 if (sshpam_err != PAM_SUCCESS) {
629 pam_end(sshpam_handle, sshpam_err);
630 sshpam_handle = NULL;
633 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
634 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
635 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
636 if (sshpam_err != PAM_SUCCESS) {
637 pam_end(sshpam_handle, sshpam_err);
638 sshpam_handle = NULL;
641 #ifdef PAM_TTY_KLUDGE
643 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
644 * sshd doesn't set the tty until too late in the auth process and
645 * may not even set one (for tty-less connections)
647 debug("PAM: setting PAM_TTY to \"ssh\"");
648 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
649 if (sshpam_err != PAM_SUCCESS) {
650 pam_end(sshpam_handle, sshpam_err);
651 sshpam_handle = NULL;
659 sshpam_init_ctx(Authctxt *authctxt)
661 struct pam_ctxt *ctxt;
664 debug3("PAM: %s entering", __func__);
666 * Refuse to start if we don't have PAM enabled or do_pam_account
667 * has previously failed.
669 if (!options.use_pam || sshpam_account_status == 0)
673 if (sshpam_init(authctxt) == -1) {
674 error("PAM: initialization failed");
678 ctxt = xmalloc(sizeof *ctxt);
679 memset(ctxt, 0, sizeof(*ctxt));
681 /* Start the authentication thread */
682 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
683 error("PAM: failed create sockets: %s", strerror(errno));
687 ctxt->pam_psock = socks[0];
688 ctxt->pam_csock = socks[1];
689 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
690 error("PAM: failed to start authentication thread: %s",
702 sshpam_query(void *ctx, char **name, char **info,
703 u_int *num, char ***prompts, u_int **echo_on)
706 struct pam_ctxt *ctxt = ctx;
712 debug3("PAM: %s entering", __func__);
713 buffer_init(&buffer);
716 *prompts = xmalloc(sizeof(char *));
719 *echo_on = xmalloc(sizeof(u_int));
720 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
721 type = buffer_get_char(&buffer);
722 msg = buffer_get_string(&buffer, NULL);
725 case PAM_PROMPT_ECHO_ON:
726 case PAM_PROMPT_ECHO_OFF:
728 len = plen + mlen + 1;
729 **prompts = xrealloc(**prompts, 1, len);
730 strlcpy(**prompts + plen, msg, len - plen);
732 **echo_on = (type == PAM_PROMPT_ECHO_ON);
737 /* accumulate messages */
738 len = plen + mlen + 2;
739 **prompts = xrealloc(**prompts, 1, len);
740 strlcpy(**prompts + plen, msg, len - plen);
742 strlcat(**prompts + plen, "\n", len - plen);
746 case PAM_ACCT_EXPIRED:
747 sshpam_account_status = 0;
750 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
751 if (**prompts != NULL && strlen(**prompts) != 0) {
762 if (**prompts != NULL) {
763 /* drain any accumulated messages */
764 debug("PAM: %s", **prompts);
765 buffer_append(&loginmsg, **prompts,
770 if (type == PAM_SUCCESS) {
771 if (!sshpam_authctxt->valid ||
772 (sshpam_authctxt->pw->pw_uid == 0 &&
773 options.permit_root_login != PERMIT_YES))
774 fatal("Internal error: PAM auth "
775 "succeeded when it should have "
777 import_environments(&buffer);
784 error("PAM: %s for %s%.100s from %.100s", msg,
785 sshpam_authctxt->valid ? "" : "illegal user ",
786 sshpam_authctxt->user,
787 get_remote_name_or_ip(utmp_len, options.use_dns));
800 /* XXX - see also comment in auth-chall.c:verify_response */
802 sshpam_respond(void *ctx, u_int num, char **resp)
805 struct pam_ctxt *ctxt = ctx;
807 debug2("PAM: %s entering, %u responses", __func__, num);
808 switch (ctxt->pam_done) {
810 sshpam_authenticated = 1;
818 error("PAM: expected one response, got %u", num);
821 buffer_init(&buffer);
822 if (sshpam_authctxt->valid &&
823 (sshpam_authctxt->pw->pw_uid != 0 ||
824 options.permit_root_login == PERMIT_YES))
825 buffer_put_cstring(&buffer, *resp);
827 buffer_put_cstring(&buffer, badpw);
828 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
829 buffer_free(&buffer);
832 buffer_free(&buffer);
837 sshpam_free_ctx(void *ctxtp)
839 struct pam_ctxt *ctxt = ctxtp;
841 debug3("PAM: %s entering", __func__);
842 sshpam_thread_cleanup();
845 * We don't call sshpam_cleanup() here because we may need the PAM
846 * handle at a later stage, e.g. when setting up a session. It's
847 * still on the cleanup list, so pam_end() *will* be called before
848 * the server process terminates.
852 KbdintDevice sshpam_device = {
860 KbdintDevice mm_sshpam_device = {
869 * This replaces auth-pam.c
872 start_pam(Authctxt *authctxt)
874 if (!options.use_pam)
875 fatal("PAM: initialisation requested when UsePAM=no");
877 if (sshpam_init(authctxt) == -1)
878 fatal("PAM: initialisation failed");
890 debug("%s: called", __func__);
891 if (sshpam_account_status != -1)
892 return (sshpam_account_status);
894 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
895 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
896 pam_strerror(sshpam_handle, sshpam_err));
898 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
899 sshpam_account_status = 0;
900 return (sshpam_account_status);
903 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
904 sshpam_password_change_required(1);
906 sshpam_account_status = 1;
907 return (sshpam_account_status);
911 do_pam_set_tty(const char *tty)
914 debug("PAM: setting PAM_TTY to \"%s\"", tty);
915 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
916 if (sshpam_err != PAM_SUCCESS)
917 fatal("PAM: failed to set PAM_TTY: %s",
918 pam_strerror(sshpam_handle, sshpam_err));
923 do_pam_setcred(int init)
925 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
926 (const void *)&store_conv);
927 if (sshpam_err != PAM_SUCCESS)
928 fatal("PAM: failed to set PAM_CONV: %s",
929 pam_strerror(sshpam_handle, sshpam_err));
931 debug("PAM: establishing credentials");
932 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
934 debug("PAM: reinitializing credentials");
935 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
937 if (sshpam_err == PAM_SUCCESS) {
938 sshpam_cred_established = 1;
941 if (sshpam_authenticated)
942 fatal("PAM: pam_setcred(): %s",
943 pam_strerror(sshpam_handle, sshpam_err));
945 debug("PAM: pam_setcred(): %s",
946 pam_strerror(sshpam_handle, sshpam_err));
950 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
951 struct pam_response **resp, void *data)
953 char input[PAM_MAX_MSG_SIZE];
954 struct pam_response *reply;
957 debug3("PAM: %s called with %d messages", __func__, n);
961 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
962 return (PAM_CONV_ERR);
964 if ((reply = calloc(n, sizeof(*reply))) == NULL)
965 return (PAM_CONV_ERR);
967 for (i = 0; i < n; ++i) {
968 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
969 case PAM_PROMPT_ECHO_OFF:
971 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
973 reply[i].resp_retcode = PAM_SUCCESS;
975 case PAM_PROMPT_ECHO_ON:
976 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
977 fgets(input, sizeof input, stdin);
978 if ((reply[i].resp = strdup(input)) == NULL)
980 reply[i].resp_retcode = PAM_SUCCESS;
984 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
985 reply[i].resp_retcode = PAM_SUCCESS;
992 return (PAM_SUCCESS);
995 for(i = 0; i < n; i++) {
996 if (reply[i].resp != NULL)
997 xfree(reply[i].resp);
1000 return (PAM_CONV_ERR);
1003 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
1006 * XXX this should be done in the authentication phase, but ssh1 doesn't
1010 do_pam_chauthtok(void)
1013 fatal("Password expired (unable to change with privsep)");
1014 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1015 (const void *)&tty_conv);
1016 if (sshpam_err != PAM_SUCCESS)
1017 fatal("PAM: failed to set PAM_CONV: %s",
1018 pam_strerror(sshpam_handle, sshpam_err));
1019 debug("PAM: changing password");
1020 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1021 if (sshpam_err != PAM_SUCCESS)
1022 fatal("PAM: pam_chauthtok(): %s",
1023 pam_strerror(sshpam_handle, sshpam_err));
1027 do_pam_session(void)
1029 debug3("PAM: opening session");
1030 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1031 (const void *)&store_conv);
1032 if (sshpam_err != PAM_SUCCESS)
1033 fatal("PAM: failed to set PAM_CONV: %s",
1034 pam_strerror(sshpam_handle, sshpam_err));
1035 sshpam_err = pam_open_session(sshpam_handle, 0);
1036 if (sshpam_err == PAM_SUCCESS)
1037 sshpam_session_open = 1;
1039 sshpam_session_open = 0;
1040 disable_forwarding();
1041 error("PAM: pam_open_session(): %s",
1042 pam_strerror(sshpam_handle, sshpam_err));
1048 is_pam_session_open(void)
1050 return sshpam_session_open;
1054 * Set a PAM environment string. We need to do this so that the session
1055 * modules can handle things like Kerberos/GSI credentials that appear
1056 * during the ssh authentication process.
1059 do_pam_putenv(char *name, char *value)
1062 #ifdef HAVE_PAM_PUTENV
1066 len = strlen(name) + strlen(value) + 2;
1067 compound = xmalloc(len);
1069 snprintf(compound, len, "%s=%s", name, value);
1070 ret = pam_putenv(sshpam_handle, compound);
1078 fetch_pam_child_environment(void)
1084 fetch_pam_environment(void)
1086 return (pam_getenvlist(sshpam_handle));
1090 free_pam_environment(char **env)
1097 for (envp = env; *envp; envp++)
1103 * "Blind" conversation function for password authentication. Assumes that
1104 * echo-off prompts are for the password and stores messages for later
1108 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1109 struct pam_response **resp, void *data)
1111 struct pam_response *reply;
1115 debug3("PAM: %s called with %d messages", __func__, n);
1119 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1120 return (PAM_CONV_ERR);
1122 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1123 return (PAM_CONV_ERR);
1124 memset(reply, 0, n * sizeof(*reply));
1126 for (i = 0; i < n; ++i) {
1127 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1128 case PAM_PROMPT_ECHO_OFF:
1129 if (sshpam_password == NULL)
1131 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1133 reply[i].resp_retcode = PAM_SUCCESS;
1137 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1139 buffer_append(&loginmsg,
1140 PAM_MSG_MEMBER(msg, i, msg), len);
1141 buffer_append(&loginmsg, "\n", 1);
1143 if ((reply[i].resp = strdup("")) == NULL)
1145 reply[i].resp_retcode = PAM_SUCCESS;
1152 return (PAM_SUCCESS);
1155 for(i = 0; i < n; i++) {
1156 if (reply[i].resp != NULL)
1157 xfree(reply[i].resp);
1160 return (PAM_CONV_ERR);
1163 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1166 * Attempt password authentication via PAM
1169 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1171 int flags = (options.permit_empty_passwd == 0 ?
1172 PAM_DISALLOW_NULL_AUTHTOK : 0);
1174 if (!options.use_pam || sshpam_handle == NULL)
1175 fatal("PAM: %s called when PAM disabled or failed to "
1176 "initialise.", __func__);
1178 sshpam_password = password;
1179 sshpam_authctxt = authctxt;
1182 * If the user logging in is invalid, or is root but is not permitted
1183 * by PermitRootLogin, use an invalid password to prevent leaking
1184 * information via timing (eg if the PAM config has a delay on fail).
1186 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1187 options.permit_root_login != PERMIT_YES))
1188 sshpam_password = badpw;
1190 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1191 (const void *)&passwd_conv);
1192 if (sshpam_err != PAM_SUCCESS)
1193 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1194 pam_strerror(sshpam_handle, sshpam_err));
1196 sshpam_err = pam_authenticate(sshpam_handle, flags);
1197 sshpam_password = NULL;
1198 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1199 debug("PAM: password authentication accepted for %.100s",
1203 debug("PAM: password authentication failed for %.100s: %s",
1204 authctxt->valid ? authctxt->user : "an illegal user",
1205 pam_strerror(sshpam_handle, sshpam_err));
1209 #endif /* USE_PAM */