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;
59 #ifdef USE_POSIX_THREADS
62 * Avoid namespace clash when *not* using pthreads for systems *with*
63 * pthreads, which unconditionally define pthread_t via sys/types.h
66 typedef pthread_t sp_pthread_t;
69 * Simulate threads with processes.
71 typedef pid_t sp_pthread_t;
74 pthread_exit(void *value __unused)
80 pthread_create(sp_pthread_t *thread, const void *attr __unused,
81 void *(*thread_start)(void *), void *arg)
85 switch ((pid = fork())) {
87 error("fork(): %s", strerror(errno));
99 pthread_cancel(sp_pthread_t thread)
101 return (kill(thread, SIGTERM));
105 pthread_join(sp_pthread_t thread, void **value __unused)
109 waitpid(thread, &status, 0);
115 static pam_handle_t *sshpam_handle = NULL;
116 static int sshpam_err = 0;
117 static int sshpam_authenticated = 0;
118 static int sshpam_new_authtok_reqd = 0;
119 static int sshpam_session_open = 0;
120 static int sshpam_cred_established = 0;
121 static char **sshpam_env = NULL;
124 sp_pthread_t pam_thread;
130 static void sshpam_free_ctx(void *);
131 static struct pam_ctxt *cleanup_ctxt;
133 /* Some PAM implementations don't implement this */
134 #ifndef HAVE_PAM_GETENVLIST
136 pam_getenvlist(pam_handle_t *pamh)
139 * XXX - If necessary, we can still support envrionment passing
140 * for platforms without pam_getenvlist by searching for known
141 * env vars (e.g. KRB5CCNAME) from the PAM environment.
147 /* Import regular and PAM environment from subprocess */
149 import_environments(Buffer *b)
155 /* Import environment from subprocess */
156 num_env = buffer_get_int(b);
157 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
158 debug3("PAM: num env strings %d", num_env);
159 for(i = 0; i < num_env; i++)
160 sshpam_env[i] = buffer_get_string(b, NULL);
162 sshpam_env[num_env] = NULL;
164 /* Import PAM environment from subprocess */
165 num_env = buffer_get_int(b);
166 debug("PAM: num PAM env strings %d", num_env);
167 for(i = 0; i < num_env; i++) {
168 env = buffer_get_string(b, NULL);
170 #ifdef HAVE_PAM_PUTENV
171 /* Errors are not fatal here */
172 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
173 error("PAM: pam_putenv: %s",
174 pam_strerror(sshpam_handle, sshpam_err));
181 * Conversation function for authentication thread.
184 sshpam_thread_conv(int n, const struct pam_message **msg,
185 struct pam_response **resp, void *data)
188 struct pam_ctxt *ctxt;
189 struct pam_response *reply;
195 if (n <= 0 || n > PAM_MAX_NUM_MSG)
196 return (PAM_CONV_ERR);
198 if ((reply = malloc(n * sizeof(*reply))) == NULL)
199 return (PAM_CONV_ERR);
200 memset(reply, 0, n * sizeof(*reply));
202 buffer_init(&buffer);
203 for (i = 0; i < n; ++i) {
204 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
205 case PAM_PROMPT_ECHO_OFF:
206 buffer_put_cstring(&buffer,
207 PAM_MSG_MEMBER(msg, i, msg));
208 if (ssh_msg_send(ctxt->pam_csock,
209 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
211 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
213 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
215 reply[i].resp = buffer_get_string(&buffer, NULL);
217 case PAM_PROMPT_ECHO_ON:
218 buffer_put_cstring(&buffer,
219 PAM_MSG_MEMBER(msg, i, msg));
220 if (ssh_msg_send(ctxt->pam_csock,
221 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
223 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
225 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
227 reply[i].resp = buffer_get_string(&buffer, NULL);
230 buffer_put_cstring(&buffer,
231 PAM_MSG_MEMBER(msg, i, msg));
232 if (ssh_msg_send(ctxt->pam_csock,
233 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
237 buffer_put_cstring(&buffer,
238 PAM_MSG_MEMBER(msg, i, msg));
239 if (ssh_msg_send(ctxt->pam_csock,
240 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
246 buffer_clear(&buffer);
248 buffer_free(&buffer);
250 return (PAM_SUCCESS);
253 for(i = 0; i < n; i++) {
254 if (reply[i].resp != NULL)
255 xfree(reply[i].resp);
258 buffer_free(&buffer);
259 return (PAM_CONV_ERR);
263 * Authentication thread.
266 sshpam_thread(void *ctxtp)
268 struct pam_ctxt *ctxt = ctxtp;
270 struct pam_conv sshpam_conv;
271 #ifndef USE_POSIX_THREADS
272 extern char **environ;
275 const char *pam_user;
277 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
278 setproctitle("%s [pam]", pam_user);
282 sshpam_conv.conv = sshpam_thread_conv;
283 sshpam_conv.appdata_ptr = ctxt;
285 buffer_init(&buffer);
286 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
287 (const void *)&sshpam_conv);
288 if (sshpam_err != PAM_SUCCESS)
290 sshpam_err = pam_authenticate(sshpam_handle, 0);
291 if (sshpam_err != PAM_SUCCESS)
293 buffer_put_cstring(&buffer, "OK");
295 #ifndef USE_POSIX_THREADS
296 /* Export any environment strings set in child */
297 for(i = 0; environ[i] != NULL; i++)
299 buffer_put_int(&buffer, i);
300 for(i = 0; environ[i] != NULL; i++)
301 buffer_put_cstring(&buffer, environ[i]);
303 /* Export any environment strings set by PAM in child */
304 env_from_pam = pam_getenvlist(sshpam_handle);
305 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
307 buffer_put_int(&buffer, i);
308 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
309 buffer_put_cstring(&buffer, env_from_pam[i]);
310 #endif /* USE_POSIX_THREADS */
312 /* XXX - can't do much about an error here */
313 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
314 buffer_free(&buffer);
318 buffer_put_cstring(&buffer,
319 pam_strerror(sshpam_handle, sshpam_err));
320 /* XXX - can't do much about an error here */
321 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
322 buffer_free(&buffer);
325 return (NULL); /* Avoid warning for non-pthread case */
329 sshpam_thread_cleanup(void)
331 struct pam_ctxt *ctxt = cleanup_ctxt;
333 if (ctxt != NULL && ctxt->pam_thread != 0) {
334 pthread_cancel(ctxt->pam_thread);
335 pthread_join(ctxt->pam_thread, NULL);
336 close(ctxt->pam_psock);
337 close(ctxt->pam_csock);
338 memset(ctxt, 0, sizeof(*ctxt));
344 sshpam_null_conv(int n, const struct pam_message **msg,
345 struct pam_response **resp, void *data)
347 return (PAM_CONV_ERR);
350 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
355 debug("PAM: cleanup");
356 if (sshpam_handle == NULL)
358 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
359 if (sshpam_cred_established) {
360 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
361 sshpam_cred_established = 0;
363 if (sshpam_session_open) {
364 pam_close_session(sshpam_handle, PAM_SILENT);
365 sshpam_session_open = 0;
367 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
368 pam_end(sshpam_handle, sshpam_err);
369 sshpam_handle = NULL;
373 sshpam_init(const char *user)
375 extern u_int utmp_len;
376 extern char *__progname;
377 const char *pam_rhost, *pam_user;
379 if (sshpam_handle != NULL) {
380 /* We already have a PAM context; check if the user matches */
381 sshpam_err = pam_get_item(sshpam_handle,
382 PAM_USER, (const void **)&pam_user);
383 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
385 pam_end(sshpam_handle, sshpam_err);
386 sshpam_handle = NULL;
388 debug("PAM: initializing for \"%s\"", user);
390 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
391 if (sshpam_err != PAM_SUCCESS) {
392 pam_end(sshpam_handle, sshpam_err);
393 sshpam_handle = NULL;
396 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
397 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
398 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
399 if (sshpam_err != PAM_SUCCESS) {
400 pam_end(sshpam_handle, sshpam_err);
401 sshpam_handle = NULL;
404 #ifdef PAM_TTY_KLUDGE
406 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
407 * sshd doesn't set the tty until too late in the auth process and
408 * may not even set one (for tty-less connections)
410 debug("PAM: setting PAM_TTY to \"ssh\"");
411 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
412 if (sshpam_err != PAM_SUCCESS) {
413 pam_end(sshpam_handle, sshpam_err);
414 sshpam_handle = NULL;
422 sshpam_init_ctx(Authctxt *authctxt)
424 struct pam_ctxt *ctxt;
427 /* Refuse to start if we don't have PAM enabled */
428 if (!options.use_pam)
432 if (sshpam_init(authctxt->user) == -1) {
433 error("PAM: initialization failed");
437 ctxt = xmalloc(sizeof *ctxt);
438 memset(ctxt, 0, sizeof(*ctxt));
440 /* Start the authentication thread */
441 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
442 error("PAM: failed create sockets: %s", strerror(errno));
446 ctxt->pam_psock = socks[0];
447 ctxt->pam_csock = socks[1];
448 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
449 error("PAM: failed to start authentication thread: %s",
461 sshpam_query(void *ctx, char **name, char **info,
462 u_int *num, char ***prompts, u_int **echo_on)
465 struct pam_ctxt *ctxt = ctx;
471 buffer_init(&buffer);
474 *prompts = xmalloc(sizeof(char *));
477 *echo_on = xmalloc(sizeof(u_int));
478 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
479 type = buffer_get_char(&buffer);
480 msg = buffer_get_string(&buffer, NULL);
482 case PAM_PROMPT_ECHO_ON:
483 case PAM_PROMPT_ECHO_OFF:
485 len = plen + strlen(msg) + 1;
486 **prompts = xrealloc(**prompts, len);
487 plen += snprintf(**prompts + plen, len, "%s", msg);
488 **echo_on = (type == PAM_PROMPT_ECHO_ON);
493 /* accumulate messages */
494 len = plen + strlen(msg) + 2;
495 **prompts = xrealloc(**prompts, len);
496 plen += snprintf(**prompts + plen, len, "%s\n", msg);
501 if (**prompts != NULL) {
502 /* drain any accumulated messages */
503 debug("PAM: %s", **prompts);
504 buffer_append(&loginmsg, **prompts,
509 if (type == PAM_SUCCESS) {
510 import_environments(&buffer);
517 error("PAM: %s", msg);
530 /* XXX - see also comment in auth-chall.c:verify_response */
532 sshpam_respond(void *ctx, u_int num, char **resp)
535 struct pam_ctxt *ctxt = ctx;
537 debug2("PAM: %s", __func__);
538 switch (ctxt->pam_done) {
540 sshpam_authenticated = 1;
548 error("PAM: expected one response, got %u", num);
551 buffer_init(&buffer);
552 buffer_put_cstring(&buffer, *resp);
553 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
554 buffer_free(&buffer);
557 buffer_free(&buffer);
562 sshpam_free_ctx(void *ctxtp)
564 struct pam_ctxt *ctxt = ctxtp;
566 sshpam_thread_cleanup();
569 * We don't call sshpam_cleanup() here because we may need the PAM
570 * handle at a later stage, e.g. when setting up a session. It's
571 * still on the cleanup list, so pam_end() *will* be called before
572 * the server process terminates.
576 KbdintDevice sshpam_device = {
584 KbdintDevice mm_sshpam_device = {
593 * This replaces auth-pam.c
596 start_pam(const char *user)
598 if (!options.use_pam)
599 fatal("PAM: initialisation requested when UsePAM=no");
601 if (sshpam_init(user) == -1)
602 fatal("PAM: initialisation failed");
614 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
615 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
617 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD)
620 if (sshpam_err == PAM_NEW_AUTHTOK_REQD) {
621 sshpam_new_authtok_reqd = 1;
623 /* Prevent forwardings until password changed */
624 no_port_forwarding_flag |= 2;
625 no_agent_forwarding_flag |= 2;
626 no_x11_forwarding_flag |= 2;
633 do_pam_set_tty(const char *tty)
636 debug("PAM: setting PAM_TTY to \"%s\"", tty);
637 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
638 if (sshpam_err != PAM_SUCCESS)
639 fatal("PAM: failed to set PAM_TTY: %s",
640 pam_strerror(sshpam_handle, sshpam_err));
645 do_pam_setcred(int init)
647 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
648 (const void *)&null_conv);
649 if (sshpam_err != PAM_SUCCESS)
650 fatal("PAM: failed to set PAM_CONV: %s",
651 pam_strerror(sshpam_handle, sshpam_err));
653 debug("PAM: establishing credentials");
654 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
656 debug("PAM: reinitializing credentials");
657 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
659 if (sshpam_err == PAM_SUCCESS) {
660 sshpam_cred_established = 1;
663 if (sshpam_authenticated)
664 fatal("PAM: pam_setcred(): %s",
665 pam_strerror(sshpam_handle, sshpam_err));
667 debug("PAM: pam_setcred(): %s",
668 pam_strerror(sshpam_handle, sshpam_err));
672 is_pam_password_change_required(void)
674 return (sshpam_new_authtok_reqd);
678 pam_tty_conv(int n, const struct pam_message **msg,
679 struct pam_response **resp, void *data)
681 char input[PAM_MAX_MSG_SIZE];
682 struct pam_response *reply;
687 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
688 return (PAM_CONV_ERR);
690 if ((reply = malloc(n * sizeof(*reply))) == NULL)
691 return (PAM_CONV_ERR);
692 memset(reply, 0, n * sizeof(*reply));
694 for (i = 0; i < n; ++i) {
695 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
696 case PAM_PROMPT_ECHO_OFF:
698 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
700 reply[i].resp_retcode = PAM_SUCCESS;
702 case PAM_PROMPT_ECHO_ON:
703 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
704 fgets(input, sizeof input, stdin);
705 reply[i].resp = xstrdup(input);
706 reply[i].resp_retcode = PAM_SUCCESS;
710 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
711 reply[i].resp_retcode = PAM_SUCCESS;
718 return (PAM_SUCCESS);
721 for(i = 0; i < n; i++) {
722 if (reply[i].resp != NULL)
723 xfree(reply[i].resp);
726 return (PAM_CONV_ERR);
729 static struct pam_conv tty_conv = { pam_tty_conv, NULL };
732 * XXX this should be done in the authentication phase, but ssh1 doesn't
736 do_pam_chauthtok(void)
739 fatal("Password expired (unable to change with privsep)");
740 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
741 (const void *)&tty_conv);
742 if (sshpam_err != PAM_SUCCESS)
743 fatal("PAM: failed to set PAM_CONV: %s",
744 pam_strerror(sshpam_handle, sshpam_err));
745 debug("PAM: changing password");
746 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
747 if (sshpam_err != PAM_SUCCESS)
748 fatal("PAM: pam_chauthtok(): %s",
749 pam_strerror(sshpam_handle, sshpam_err));
755 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
756 (const void *)&tty_conv);
757 if (sshpam_err != PAM_SUCCESS)
758 fatal("PAM: failed to set PAM_CONV: %s",
759 pam_strerror(sshpam_handle, sshpam_err));
760 sshpam_err = pam_open_session(sshpam_handle, 0);
761 if (sshpam_err != PAM_SUCCESS)
762 fatal("PAM: pam_open_session(): %s",
763 pam_strerror(sshpam_handle, sshpam_err));
764 sshpam_session_open = 1;
768 * Set a PAM environment string. We need to do this so that the session
769 * modules can handle things like Kerberos/GSI credentials that appear
770 * during the ssh authentication process.
773 do_pam_putenv(char *name, char *value)
776 #ifdef HAVE_PAM_PUTENV
780 len = strlen(name) + strlen(value) + 2;
781 compound = xmalloc(len);
783 snprintf(compound, len, "%s=%s", name, value);
784 ret = pam_putenv(sshpam_handle, compound);
792 print_pam_messages(void)
798 fetch_pam_child_environment(void)
804 fetch_pam_environment(void)
806 return (pam_getenvlist(sshpam_handle));
810 free_pam_environment(char **env)
817 for (envp = env; *envp; envp++)