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 /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
37 #include <security/pam_appl.h>
45 #include "monitor_wrap.h"
52 #include "auth-options.h"
54 extern ServerOptions options;
55 extern Buffer loginmsg;
60 #ifdef USE_POSIX_THREADS
63 * Avoid namespace clash when *not* using pthreads for systems *with*
64 * pthreads, which unconditionally define pthread_t via sys/types.h
67 typedef pthread_t sp_pthread_t;
70 * Simulate threads with processes.
72 typedef pid_t sp_pthread_t;
75 pthread_exit(void *value __unused)
81 pthread_create(sp_pthread_t *thread, const void *attr __unused,
82 void *(*thread_start)(void *), void *arg)
86 switch ((pid = fork())) {
88 error("fork(): %s", strerror(errno));
100 pthread_cancel(sp_pthread_t thread)
102 return (kill(thread, SIGTERM));
106 pthread_join(sp_pthread_t thread, void **value __unused)
110 waitpid(thread, &status, 0);
116 static pam_handle_t *sshpam_handle = NULL;
117 static int sshpam_err = 0;
118 static int sshpam_authenticated = 0;
119 static int sshpam_new_authtok_reqd = 0;
120 static int sshpam_session_open = 0;
121 static int sshpam_cred_established = 0;
122 static int sshpam_account_status = -1;
123 static char **sshpam_env = NULL;
126 sp_pthread_t pam_thread;
132 static void sshpam_free_ctx(void *);
133 static struct pam_ctxt *cleanup_ctxt;
135 /* Some PAM implementations don't implement this */
136 #ifndef HAVE_PAM_GETENVLIST
138 pam_getenvlist(pam_handle_t *pamh)
141 * XXX - If necessary, we can still support envrionment passing
142 * for platforms without pam_getenvlist by searching for known
143 * env vars (e.g. KRB5CCNAME) from the PAM environment.
150 pam_password_change_required(int reqd)
152 sshpam_new_authtok_reqd = reqd;
154 no_port_forwarding_flag |= 2;
155 no_agent_forwarding_flag |= 2;
156 no_x11_forwarding_flag |= 2;
158 no_port_forwarding_flag &= ~2;
159 no_agent_forwarding_flag &= ~2;
160 no_x11_forwarding_flag &= ~2;
164 /* Import regular and PAM environment from subprocess */
166 import_environments(Buffer *b)
172 /* Import variables set by do_pam_account */
173 sshpam_account_status = buffer_get_int(b);
174 pam_password_change_required(buffer_get_int(b));
176 /* Import environment from subprocess */
177 num_env = buffer_get_int(b);
178 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
179 debug3("PAM: num env strings %d", num_env);
180 for(i = 0; i < num_env; i++)
181 sshpam_env[i] = buffer_get_string(b, NULL);
183 sshpam_env[num_env] = NULL;
185 /* Import PAM environment from subprocess */
186 num_env = buffer_get_int(b);
187 debug("PAM: num PAM env strings %d", num_env);
188 for(i = 0; i < num_env; i++) {
189 env = buffer_get_string(b, NULL);
191 #ifdef HAVE_PAM_PUTENV
192 /* Errors are not fatal here */
193 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
194 error("PAM: pam_putenv: %s",
195 pam_strerror(sshpam_handle, sshpam_err));
202 * Conversation function for authentication thread.
205 sshpam_thread_conv(int n, const struct pam_message **msg,
206 struct pam_response **resp, void *data)
209 struct pam_ctxt *ctxt;
210 struct pam_response *reply;
216 if (n <= 0 || n > PAM_MAX_NUM_MSG)
217 return (PAM_CONV_ERR);
219 if ((reply = malloc(n * sizeof(*reply))) == NULL)
220 return (PAM_CONV_ERR);
221 memset(reply, 0, n * sizeof(*reply));
223 buffer_init(&buffer);
224 for (i = 0; i < n; ++i) {
225 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
226 case PAM_PROMPT_ECHO_OFF:
227 buffer_put_cstring(&buffer,
228 PAM_MSG_MEMBER(msg, i, msg));
229 if (ssh_msg_send(ctxt->pam_csock,
230 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
232 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
234 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
236 reply[i].resp = buffer_get_string(&buffer, NULL);
238 case PAM_PROMPT_ECHO_ON:
239 buffer_put_cstring(&buffer,
240 PAM_MSG_MEMBER(msg, i, msg));
241 if (ssh_msg_send(ctxt->pam_csock,
242 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
244 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
246 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
248 reply[i].resp = buffer_get_string(&buffer, NULL);
251 buffer_put_cstring(&buffer,
252 PAM_MSG_MEMBER(msg, i, msg));
253 if (ssh_msg_send(ctxt->pam_csock,
254 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
258 buffer_put_cstring(&buffer,
259 PAM_MSG_MEMBER(msg, i, msg));
260 if (ssh_msg_send(ctxt->pam_csock,
261 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
267 buffer_clear(&buffer);
269 buffer_free(&buffer);
271 return (PAM_SUCCESS);
274 for(i = 0; i < n; i++) {
275 if (reply[i].resp != NULL)
276 xfree(reply[i].resp);
279 buffer_free(&buffer);
280 return (PAM_CONV_ERR);
284 * Authentication thread.
287 sshpam_thread(void *ctxtp)
289 struct pam_ctxt *ctxt = ctxtp;
291 struct pam_conv sshpam_conv;
292 #ifndef USE_POSIX_THREADS
293 extern char **environ;
296 const char *pam_user;
298 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
299 setproctitle("%s [pam]", pam_user);
303 sshpam_conv.conv = sshpam_thread_conv;
304 sshpam_conv.appdata_ptr = ctxt;
306 buffer_init(&buffer);
307 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
308 (const void *)&sshpam_conv);
309 if (sshpam_err != PAM_SUCCESS)
311 sshpam_err = pam_authenticate(sshpam_handle, 0);
312 if (sshpam_err != PAM_SUCCESS)
316 if (!do_pam_account())
318 if (sshpam_new_authtok_reqd) {
319 sshpam_err = pam_chauthtok(sshpam_handle,
320 PAM_CHANGE_EXPIRED_AUTHTOK);
321 if (sshpam_err != PAM_SUCCESS)
323 pam_password_change_required(0);
327 buffer_put_cstring(&buffer, "OK");
329 #ifndef USE_POSIX_THREADS
330 /* Export variables set by do_pam_account */
331 buffer_put_int(&buffer, sshpam_account_status);
332 buffer_put_int(&buffer, sshpam_new_authtok_reqd);
334 /* Export any environment strings set in child */
335 for(i = 0; environ[i] != NULL; i++)
337 buffer_put_int(&buffer, i);
338 for(i = 0; environ[i] != NULL; i++)
339 buffer_put_cstring(&buffer, environ[i]);
341 /* Export any environment strings set by PAM in child */
342 env_from_pam = pam_getenvlist(sshpam_handle);
343 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
345 buffer_put_int(&buffer, i);
346 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
347 buffer_put_cstring(&buffer, env_from_pam[i]);
348 #endif /* USE_POSIX_THREADS */
350 /* XXX - can't do much about an error here */
351 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
352 buffer_free(&buffer);
356 buffer_put_cstring(&buffer,
357 pam_strerror(sshpam_handle, sshpam_err));
358 /* XXX - can't do much about an error here */
359 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
360 buffer_free(&buffer);
363 return (NULL); /* Avoid warning for non-pthread case */
367 sshpam_thread_cleanup(void)
369 struct pam_ctxt *ctxt = cleanup_ctxt;
371 if (ctxt != NULL && ctxt->pam_thread != 0) {
372 pthread_cancel(ctxt->pam_thread);
373 pthread_join(ctxt->pam_thread, NULL);
374 close(ctxt->pam_psock);
375 close(ctxt->pam_csock);
376 memset(ctxt, 0, sizeof(*ctxt));
382 sshpam_null_conv(int n, const struct pam_message **msg,
383 struct pam_response **resp, void *data)
385 return (PAM_CONV_ERR);
388 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
393 debug("PAM: cleanup");
394 if (sshpam_handle == NULL)
396 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
397 if (sshpam_cred_established) {
398 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
399 sshpam_cred_established = 0;
401 if (sshpam_session_open) {
402 pam_close_session(sshpam_handle, PAM_SILENT);
403 sshpam_session_open = 0;
405 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
406 pam_end(sshpam_handle, sshpam_err);
407 sshpam_handle = NULL;
411 sshpam_init(const char *user)
413 extern u_int utmp_len;
414 extern char *__progname;
415 const char *pam_rhost, *pam_user;
417 if (sshpam_handle != NULL) {
418 /* We already have a PAM context; check if the user matches */
419 sshpam_err = pam_get_item(sshpam_handle,
420 PAM_USER, (const void **)&pam_user);
421 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
423 pam_end(sshpam_handle, sshpam_err);
424 sshpam_handle = NULL;
426 debug("PAM: initializing for \"%s\"", user);
428 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
429 if (sshpam_err != PAM_SUCCESS) {
430 pam_end(sshpam_handle, sshpam_err);
431 sshpam_handle = NULL;
434 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
435 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
436 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
437 if (sshpam_err != PAM_SUCCESS) {
438 pam_end(sshpam_handle, sshpam_err);
439 sshpam_handle = NULL;
442 #ifdef PAM_TTY_KLUDGE
444 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
445 * sshd doesn't set the tty until too late in the auth process and
446 * may not even set one (for tty-less connections)
448 debug("PAM: setting PAM_TTY to \"ssh\"");
449 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
450 if (sshpam_err != PAM_SUCCESS) {
451 pam_end(sshpam_handle, sshpam_err);
452 sshpam_handle = NULL;
460 sshpam_init_ctx(Authctxt *authctxt)
462 struct pam_ctxt *ctxt;
465 /* Refuse to start if we don't have PAM enabled */
466 if (!options.use_pam)
470 if (sshpam_init(authctxt->user) == -1) {
471 error("PAM: initialization failed");
475 ctxt = xmalloc(sizeof *ctxt);
476 memset(ctxt, 0, sizeof(*ctxt));
478 /* Start the authentication thread */
479 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
480 error("PAM: failed create sockets: %s", strerror(errno));
484 ctxt->pam_psock = socks[0];
485 ctxt->pam_csock = socks[1];
486 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
487 error("PAM: failed to start authentication thread: %s",
499 sshpam_query(void *ctx, char **name, char **info,
500 u_int *num, char ***prompts, u_int **echo_on)
503 struct pam_ctxt *ctxt = ctx;
509 buffer_init(&buffer);
512 *prompts = xmalloc(sizeof(char *));
515 *echo_on = xmalloc(sizeof(u_int));
516 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
517 type = buffer_get_char(&buffer);
518 msg = buffer_get_string(&buffer, NULL);
520 case PAM_PROMPT_ECHO_ON:
521 case PAM_PROMPT_ECHO_OFF:
523 len = plen + strlen(msg) + 1;
524 **prompts = xrealloc(**prompts, len);
525 plen += snprintf(**prompts + plen, len, "%s", msg);
526 **echo_on = (type == PAM_PROMPT_ECHO_ON);
531 /* accumulate messages */
532 len = plen + strlen(msg) + 2;
533 **prompts = xrealloc(**prompts, len);
534 plen += snprintf(**prompts + plen, len, "%s\n", msg);
539 if (**prompts != NULL) {
540 /* drain any accumulated messages */
541 debug("PAM: %s", **prompts);
542 buffer_append(&loginmsg, **prompts,
547 if (type == PAM_SUCCESS) {
548 import_environments(&buffer);
555 error("PAM: %s", msg);
568 /* XXX - see also comment in auth-chall.c:verify_response */
570 sshpam_respond(void *ctx, u_int num, char **resp)
573 struct pam_ctxt *ctxt = ctx;
575 debug2("PAM: %s", __func__);
576 switch (ctxt->pam_done) {
578 sshpam_authenticated = 1;
586 error("PAM: expected one response, got %u", num);
589 buffer_init(&buffer);
590 buffer_put_cstring(&buffer, *resp);
591 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
592 buffer_free(&buffer);
595 buffer_free(&buffer);
600 sshpam_free_ctx(void *ctxtp)
602 struct pam_ctxt *ctxt = ctxtp;
604 sshpam_thread_cleanup();
607 * We don't call sshpam_cleanup() here because we may need the PAM
608 * handle at a later stage, e.g. when setting up a session. It's
609 * still on the cleanup list, so pam_end() *will* be called before
610 * the server process terminates.
614 KbdintDevice sshpam_device = {
622 KbdintDevice mm_sshpam_device = {
631 * This replaces auth-pam.c
634 start_pam(const char *user)
636 if (!options.use_pam)
637 fatal("PAM: initialisation requested when UsePAM=no");
639 if (sshpam_init(user) == -1)
640 fatal("PAM: initialisation failed");
652 if (sshpam_account_status != -1)
653 return (sshpam_account_status);
655 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
656 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
658 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
659 sshpam_account_status = 0;
660 return (sshpam_account_status);
663 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
664 pam_password_change_required(1);
666 sshpam_account_status = 1;
667 return (sshpam_account_status);
671 do_pam_set_tty(const char *tty)
674 debug("PAM: setting PAM_TTY to \"%s\"", tty);
675 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
676 if (sshpam_err != PAM_SUCCESS)
677 fatal("PAM: failed to set PAM_TTY: %s",
678 pam_strerror(sshpam_handle, sshpam_err));
683 do_pam_setcred(int init)
685 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
686 (const void *)&null_conv);
687 if (sshpam_err != PAM_SUCCESS)
688 fatal("PAM: failed to set PAM_CONV: %s",
689 pam_strerror(sshpam_handle, sshpam_err));
691 debug("PAM: establishing credentials");
692 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
694 debug("PAM: reinitializing credentials");
695 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
697 if (sshpam_err == PAM_SUCCESS) {
698 sshpam_cred_established = 1;
701 if (sshpam_authenticated)
702 fatal("PAM: pam_setcred(): %s",
703 pam_strerror(sshpam_handle, sshpam_err));
705 debug("PAM: pam_setcred(): %s",
706 pam_strerror(sshpam_handle, sshpam_err));
710 is_pam_password_change_required(void)
712 return (sshpam_new_authtok_reqd);
716 pam_tty_conv(int n, const struct pam_message **msg,
717 struct pam_response **resp, void *data)
719 char input[PAM_MAX_MSG_SIZE];
720 struct pam_response *reply;
725 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
726 return (PAM_CONV_ERR);
728 if ((reply = malloc(n * sizeof(*reply))) == NULL)
729 return (PAM_CONV_ERR);
730 memset(reply, 0, n * sizeof(*reply));
732 for (i = 0; i < n; ++i) {
733 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
734 case PAM_PROMPT_ECHO_OFF:
736 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
738 reply[i].resp_retcode = PAM_SUCCESS;
740 case PAM_PROMPT_ECHO_ON:
741 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
742 fgets(input, sizeof input, stdin);
743 reply[i].resp = xstrdup(input);
744 reply[i].resp_retcode = PAM_SUCCESS;
748 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
749 reply[i].resp_retcode = PAM_SUCCESS;
756 return (PAM_SUCCESS);
759 for(i = 0; i < n; i++) {
760 if (reply[i].resp != NULL)
761 xfree(reply[i].resp);
764 return (PAM_CONV_ERR);
767 static struct pam_conv tty_conv = { pam_tty_conv, NULL };
770 * XXX this should be done in the authentication phase, but ssh1 doesn't
774 do_pam_chauthtok(void)
777 fatal("Password expired (unable to change with privsep)");
778 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
779 (const void *)&tty_conv);
780 if (sshpam_err != PAM_SUCCESS)
781 fatal("PAM: failed to set PAM_CONV: %s",
782 pam_strerror(sshpam_handle, sshpam_err));
783 debug("PAM: changing password");
784 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
785 if (sshpam_err != PAM_SUCCESS)
786 fatal("PAM: pam_chauthtok(): %s",
787 pam_strerror(sshpam_handle, sshpam_err));
793 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
794 (const void *)&tty_conv);
795 if (sshpam_err != PAM_SUCCESS)
796 fatal("PAM: failed to set PAM_CONV: %s",
797 pam_strerror(sshpam_handle, sshpam_err));
798 sshpam_err = pam_open_session(sshpam_handle, 0);
799 if (sshpam_err != PAM_SUCCESS)
800 fatal("PAM: pam_open_session(): %s",
801 pam_strerror(sshpam_handle, sshpam_err));
802 sshpam_session_open = 1;
806 * Set a PAM environment string. We need to do this so that the session
807 * modules can handle things like Kerberos/GSI credentials that appear
808 * during the ssh authentication process.
811 do_pam_putenv(char *name, char *value)
814 #ifdef HAVE_PAM_PUTENV
818 len = strlen(name) + strlen(value) + 2;
819 compound = xmalloc(len);
821 snprintf(compound, len, "%s=%s", name, value);
822 ret = pam_putenv(sshpam_handle, compound);
830 print_pam_messages(void)
836 fetch_pam_child_environment(void)
842 fetch_pam_environment(void)
844 return (pam_getenvlist(sshpam_handle));
848 free_pam_environment(char **env)
855 for (envp = env; *envp; envp++)