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 #if defined(HAVE_SECURITY_PAM_APPL_H)
38 #include <security/pam_appl.h>
39 #elif defined (HAVE_PAM_PAM_APPL_H)
40 #include <pam/pam_appl.h>
49 #include "monitor_wrap.h"
56 #include "auth-options.h"
58 extern ServerOptions options;
59 extern Buffer loginmsg;
64 #ifdef USE_POSIX_THREADS
67 * Avoid namespace clash when *not* using pthreads for systems *with*
68 * pthreads, which unconditionally define pthread_t via sys/types.h
71 typedef pthread_t sp_pthread_t;
74 * Simulate threads with processes.
76 typedef pid_t sp_pthread_t;
79 pthread_exit(void *value __unused)
85 pthread_create(sp_pthread_t *thread, const void *attr __unused,
86 void *(*thread_start)(void *), void *arg)
90 switch ((pid = fork())) {
92 error("fork(): %s", strerror(errno));
104 pthread_cancel(sp_pthread_t thread)
106 return (kill(thread, SIGTERM));
110 pthread_join(sp_pthread_t thread, void **value __unused)
114 waitpid(thread, &status, 0);
120 static pam_handle_t *sshpam_handle = NULL;
121 static int sshpam_err = 0;
122 static int sshpam_authenticated = 0;
123 static int sshpam_new_authtok_reqd = 0;
124 static int sshpam_session_open = 0;
125 static int sshpam_cred_established = 0;
126 static int sshpam_account_status = -1;
127 static char **sshpam_env = NULL;
130 sp_pthread_t pam_thread;
136 static void sshpam_free_ctx(void *);
137 static struct pam_ctxt *cleanup_ctxt;
139 /* Some PAM implementations don't implement this */
140 #ifndef HAVE_PAM_GETENVLIST
142 pam_getenvlist(pam_handle_t *pamh)
145 * XXX - If necessary, we can still support envrionment passing
146 * for platforms without pam_getenvlist by searching for known
147 * env vars (e.g. KRB5CCNAME) from the PAM environment.
154 pam_password_change_required(int reqd)
156 sshpam_new_authtok_reqd = reqd;
158 no_port_forwarding_flag |= 2;
159 no_agent_forwarding_flag |= 2;
160 no_x11_forwarding_flag |= 2;
162 no_port_forwarding_flag &= ~2;
163 no_agent_forwarding_flag &= ~2;
164 no_x11_forwarding_flag &= ~2;
168 /* Import regular and PAM environment from subprocess */
170 import_environments(Buffer *b)
176 /* Import variables set by do_pam_account */
177 sshpam_account_status = buffer_get_int(b);
178 pam_password_change_required(buffer_get_int(b));
180 /* Import environment from subprocess */
181 num_env = buffer_get_int(b);
182 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
183 debug3("PAM: num env strings %d", num_env);
184 for(i = 0; i < num_env; i++)
185 sshpam_env[i] = buffer_get_string(b, NULL);
187 sshpam_env[num_env] = NULL;
189 /* Import PAM environment from subprocess */
190 num_env = buffer_get_int(b);
191 debug("PAM: num PAM env strings %d", num_env);
192 for(i = 0; i < num_env; i++) {
193 env = buffer_get_string(b, NULL);
195 #ifdef HAVE_PAM_PUTENV
196 /* Errors are not fatal here */
197 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
198 error("PAM: pam_putenv: %s",
199 pam_strerror(sshpam_handle, sshpam_err));
206 * Conversation function for authentication thread.
209 sshpam_thread_conv(int n, const struct pam_message **msg,
210 struct pam_response **resp, void *data)
213 struct pam_ctxt *ctxt;
214 struct pam_response *reply;
220 if (n <= 0 || n > PAM_MAX_NUM_MSG)
221 return (PAM_CONV_ERR);
223 if ((reply = malloc(n * sizeof(*reply))) == NULL)
224 return (PAM_CONV_ERR);
225 memset(reply, 0, n * sizeof(*reply));
227 buffer_init(&buffer);
228 for (i = 0; i < n; ++i) {
229 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
230 case PAM_PROMPT_ECHO_OFF:
231 buffer_put_cstring(&buffer,
232 PAM_MSG_MEMBER(msg, i, msg));
233 if (ssh_msg_send(ctxt->pam_csock,
234 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
236 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
238 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
240 reply[i].resp = buffer_get_string(&buffer, NULL);
242 case PAM_PROMPT_ECHO_ON:
243 buffer_put_cstring(&buffer,
244 PAM_MSG_MEMBER(msg, i, msg));
245 if (ssh_msg_send(ctxt->pam_csock,
246 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
248 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
250 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
252 reply[i].resp = buffer_get_string(&buffer, NULL);
255 buffer_put_cstring(&buffer,
256 PAM_MSG_MEMBER(msg, i, msg));
257 if (ssh_msg_send(ctxt->pam_csock,
258 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
262 buffer_put_cstring(&buffer,
263 PAM_MSG_MEMBER(msg, i, msg));
264 if (ssh_msg_send(ctxt->pam_csock,
265 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
271 buffer_clear(&buffer);
273 buffer_free(&buffer);
275 return (PAM_SUCCESS);
278 for(i = 0; i < n; i++) {
279 if (reply[i].resp != NULL)
280 xfree(reply[i].resp);
283 buffer_free(&buffer);
284 return (PAM_CONV_ERR);
288 * Authentication thread.
291 sshpam_thread(void *ctxtp)
293 struct pam_ctxt *ctxt = ctxtp;
295 struct pam_conv sshpam_conv;
296 #ifndef USE_POSIX_THREADS
297 extern char **environ;
300 const char *pam_user;
302 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
303 setproctitle("%s [pam]", pam_user);
307 sshpam_conv.conv = sshpam_thread_conv;
308 sshpam_conv.appdata_ptr = ctxt;
310 buffer_init(&buffer);
311 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
312 (const void *)&sshpam_conv);
313 if (sshpam_err != PAM_SUCCESS)
315 sshpam_err = pam_authenticate(sshpam_handle, 0);
316 if (sshpam_err != PAM_SUCCESS)
320 if (!do_pam_account())
322 if (sshpam_new_authtok_reqd) {
323 sshpam_err = pam_chauthtok(sshpam_handle,
324 PAM_CHANGE_EXPIRED_AUTHTOK);
325 if (sshpam_err != PAM_SUCCESS)
327 pam_password_change_required(0);
331 buffer_put_cstring(&buffer, "OK");
333 #ifndef USE_POSIX_THREADS
334 /* Export variables set by do_pam_account */
335 buffer_put_int(&buffer, sshpam_account_status);
336 buffer_put_int(&buffer, sshpam_new_authtok_reqd);
338 /* Export any environment strings set in child */
339 for(i = 0; environ[i] != NULL; i++)
341 buffer_put_int(&buffer, i);
342 for(i = 0; environ[i] != NULL; i++)
343 buffer_put_cstring(&buffer, environ[i]);
345 /* Export any environment strings set by PAM in child */
346 env_from_pam = pam_getenvlist(sshpam_handle);
347 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
349 buffer_put_int(&buffer, i);
350 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
351 buffer_put_cstring(&buffer, env_from_pam[i]);
352 #endif /* USE_POSIX_THREADS */
354 /* XXX - can't do much about an error here */
355 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
356 buffer_free(&buffer);
360 buffer_put_cstring(&buffer,
361 pam_strerror(sshpam_handle, sshpam_err));
362 /* XXX - can't do much about an error here */
363 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
364 buffer_free(&buffer);
367 return (NULL); /* Avoid warning for non-pthread case */
371 sshpam_thread_cleanup(void)
373 struct pam_ctxt *ctxt = cleanup_ctxt;
375 if (ctxt != NULL && ctxt->pam_thread != 0) {
376 pthread_cancel(ctxt->pam_thread);
377 pthread_join(ctxt->pam_thread, NULL);
378 close(ctxt->pam_psock);
379 close(ctxt->pam_csock);
380 memset(ctxt, 0, sizeof(*ctxt));
386 sshpam_null_conv(int n, const struct pam_message **msg,
387 struct pam_response **resp, void *data)
389 return (PAM_CONV_ERR);
392 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
397 debug("PAM: cleanup");
398 if (sshpam_handle == NULL)
400 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
401 if (sshpam_cred_established) {
402 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
403 sshpam_cred_established = 0;
405 if (sshpam_session_open) {
406 pam_close_session(sshpam_handle, PAM_SILENT);
407 sshpam_session_open = 0;
409 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
410 pam_end(sshpam_handle, sshpam_err);
411 sshpam_handle = NULL;
415 sshpam_init(const char *user)
417 extern u_int utmp_len;
418 extern char *__progname;
419 const char *pam_rhost, *pam_user;
421 if (sshpam_handle != NULL) {
422 /* We already have a PAM context; check if the user matches */
423 sshpam_err = pam_get_item(sshpam_handle,
424 PAM_USER, (const void **)&pam_user);
425 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
427 pam_end(sshpam_handle, sshpam_err);
428 sshpam_handle = NULL;
430 debug("PAM: initializing for \"%s\"", user);
432 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
433 if (sshpam_err != PAM_SUCCESS) {
434 pam_end(sshpam_handle, sshpam_err);
435 sshpam_handle = NULL;
438 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
439 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
440 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
441 if (sshpam_err != PAM_SUCCESS) {
442 pam_end(sshpam_handle, sshpam_err);
443 sshpam_handle = NULL;
446 #ifdef PAM_TTY_KLUDGE
448 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
449 * sshd doesn't set the tty until too late in the auth process and
450 * may not even set one (for tty-less connections)
452 debug("PAM: setting PAM_TTY to \"ssh\"");
453 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
454 if (sshpam_err != PAM_SUCCESS) {
455 pam_end(sshpam_handle, sshpam_err);
456 sshpam_handle = NULL;
464 sshpam_init_ctx(Authctxt *authctxt)
466 struct pam_ctxt *ctxt;
469 /* Refuse to start if we don't have PAM enabled */
470 if (!options.use_pam)
474 if (sshpam_init(authctxt->user) == -1) {
475 error("PAM: initialization failed");
479 ctxt = xmalloc(sizeof *ctxt);
480 memset(ctxt, 0, sizeof(*ctxt));
482 /* Start the authentication thread */
483 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
484 error("PAM: failed create sockets: %s", strerror(errno));
488 ctxt->pam_psock = socks[0];
489 ctxt->pam_csock = socks[1];
490 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
491 error("PAM: failed to start authentication thread: %s",
503 sshpam_query(void *ctx, char **name, char **info,
504 u_int *num, char ***prompts, u_int **echo_on)
507 struct pam_ctxt *ctxt = ctx;
513 buffer_init(&buffer);
516 *prompts = xmalloc(sizeof(char *));
519 *echo_on = xmalloc(sizeof(u_int));
520 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
521 type = buffer_get_char(&buffer);
522 msg = buffer_get_string(&buffer, NULL);
524 case PAM_PROMPT_ECHO_ON:
525 case PAM_PROMPT_ECHO_OFF:
527 len = plen + strlen(msg) + 1;
528 **prompts = xrealloc(**prompts, len);
529 plen += snprintf(**prompts + plen, len, "%s", msg);
530 **echo_on = (type == PAM_PROMPT_ECHO_ON);
535 /* accumulate messages */
536 len = plen + strlen(msg) + 2;
537 **prompts = xrealloc(**prompts, len);
538 plen += snprintf(**prompts + plen, len, "%s\n", msg);
543 if (**prompts != NULL) {
544 /* drain any accumulated messages */
545 debug("PAM: %s", **prompts);
546 buffer_append(&loginmsg, **prompts,
551 if (type == PAM_SUCCESS) {
552 import_environments(&buffer);
559 error("PAM: %s", msg);
572 /* XXX - see also comment in auth-chall.c:verify_response */
574 sshpam_respond(void *ctx, u_int num, char **resp)
577 struct pam_ctxt *ctxt = ctx;
579 debug2("PAM: %s", __func__);
580 switch (ctxt->pam_done) {
582 sshpam_authenticated = 1;
590 error("PAM: expected one response, got %u", num);
593 buffer_init(&buffer);
594 buffer_put_cstring(&buffer, *resp);
595 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
596 buffer_free(&buffer);
599 buffer_free(&buffer);
604 sshpam_free_ctx(void *ctxtp)
606 struct pam_ctxt *ctxt = ctxtp;
608 sshpam_thread_cleanup();
611 * We don't call sshpam_cleanup() here because we may need the PAM
612 * handle at a later stage, e.g. when setting up a session. It's
613 * still on the cleanup list, so pam_end() *will* be called before
614 * the server process terminates.
618 KbdintDevice sshpam_device = {
626 KbdintDevice mm_sshpam_device = {
635 * This replaces auth-pam.c
638 start_pam(const char *user)
640 if (!options.use_pam)
641 fatal("PAM: initialisation requested when UsePAM=no");
643 if (sshpam_init(user) == -1)
644 fatal("PAM: initialisation failed");
656 if (sshpam_account_status != -1)
657 return (sshpam_account_status);
659 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
660 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
662 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
663 sshpam_account_status = 0;
664 return (sshpam_account_status);
667 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
668 pam_password_change_required(1);
670 sshpam_account_status = 1;
671 return (sshpam_account_status);
675 do_pam_set_tty(const char *tty)
678 debug("PAM: setting PAM_TTY to \"%s\"", tty);
679 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
680 if (sshpam_err != PAM_SUCCESS)
681 fatal("PAM: failed to set PAM_TTY: %s",
682 pam_strerror(sshpam_handle, sshpam_err));
687 do_pam_setcred(int init)
689 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
690 (const void *)&null_conv);
691 if (sshpam_err != PAM_SUCCESS)
692 fatal("PAM: failed to set PAM_CONV: %s",
693 pam_strerror(sshpam_handle, sshpam_err));
695 debug("PAM: establishing credentials");
696 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
698 debug("PAM: reinitializing credentials");
699 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
701 if (sshpam_err == PAM_SUCCESS) {
702 sshpam_cred_established = 1;
705 if (sshpam_authenticated)
706 fatal("PAM: pam_setcred(): %s",
707 pam_strerror(sshpam_handle, sshpam_err));
709 debug("PAM: pam_setcred(): %s",
710 pam_strerror(sshpam_handle, sshpam_err));
714 is_pam_password_change_required(void)
716 return (sshpam_new_authtok_reqd);
720 pam_tty_conv(int n, const struct pam_message **msg,
721 struct pam_response **resp, void *data)
723 char input[PAM_MAX_MSG_SIZE];
724 struct pam_response *reply;
729 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
730 return (PAM_CONV_ERR);
732 if ((reply = malloc(n * sizeof(*reply))) == NULL)
733 return (PAM_CONV_ERR);
734 memset(reply, 0, n * sizeof(*reply));
736 for (i = 0; i < n; ++i) {
737 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
738 case PAM_PROMPT_ECHO_OFF:
740 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
742 reply[i].resp_retcode = PAM_SUCCESS;
744 case PAM_PROMPT_ECHO_ON:
745 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
746 fgets(input, sizeof input, stdin);
747 reply[i].resp = xstrdup(input);
748 reply[i].resp_retcode = PAM_SUCCESS;
752 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
753 reply[i].resp_retcode = PAM_SUCCESS;
760 return (PAM_SUCCESS);
763 for(i = 0; i < n; i++) {
764 if (reply[i].resp != NULL)
765 xfree(reply[i].resp);
768 return (PAM_CONV_ERR);
771 static struct pam_conv tty_conv = { pam_tty_conv, NULL };
774 * XXX this should be done in the authentication phase, but ssh1 doesn't
778 do_pam_chauthtok(void)
781 fatal("Password expired (unable to change with privsep)");
782 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
783 (const void *)&tty_conv);
784 if (sshpam_err != PAM_SUCCESS)
785 fatal("PAM: failed to set PAM_CONV: %s",
786 pam_strerror(sshpam_handle, sshpam_err));
787 debug("PAM: changing password");
788 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
789 if (sshpam_err != PAM_SUCCESS)
790 fatal("PAM: pam_chauthtok(): %s",
791 pam_strerror(sshpam_handle, sshpam_err));
797 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
798 (const void *)&tty_conv);
799 if (sshpam_err != PAM_SUCCESS)
800 fatal("PAM: failed to set PAM_CONV: %s",
801 pam_strerror(sshpam_handle, sshpam_err));
802 sshpam_err = pam_open_session(sshpam_handle, 0);
803 if (sshpam_err != PAM_SUCCESS)
804 fatal("PAM: pam_open_session(): %s",
805 pam_strerror(sshpam_handle, sshpam_err));
806 sshpam_session_open = 1;
810 * Set a PAM environment string. We need to do this so that the session
811 * modules can handle things like Kerberos/GSI credentials that appear
812 * during the ssh authentication process.
815 do_pam_putenv(char *name, char *value)
818 #ifdef HAVE_PAM_PUTENV
822 len = strlen(name) + strlen(value) + 2;
823 compound = xmalloc(len);
825 snprintf(compound, len, "%s=%s", name, value);
826 ret = pam_putenv(sshpam_handle, compound);
834 print_pam_messages(void)
840 fetch_pam_child_environment(void)
846 fetch_pam_environment(void)
848 return (pam_getenvlist(sshpam_handle));
852 free_pam_environment(char **env)
859 for (envp = env; *envp; envp++)