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;
58 #ifdef USE_POSIX_THREADS
61 * Avoid namespace clash when *not* using pthreads for systems *with*
62 * pthreads, which unconditionally define pthread_t via sys/types.h
65 typedef pthread_t sp_pthread_t;
68 * Simulate threads with processes.
70 typedef pid_t sp_pthread_t;
73 pthread_exit(void *value __unused)
79 pthread_create(sp_pthread_t *thread, const void *attr __unused,
80 void *(*thread_start)(void *), void *arg)
84 switch ((pid = fork())) {
86 error("fork(): %s", strerror(errno));
98 pthread_cancel(sp_pthread_t thread)
100 return (kill(thread, SIGTERM));
104 pthread_join(sp_pthread_t thread, void **value __unused)
108 waitpid(thread, &status, 0);
114 static pam_handle_t *sshpam_handle = NULL;
115 static int sshpam_err = 0;
116 static int sshpam_authenticated = 0;
117 static int sshpam_new_authtok_reqd = 0;
118 static int sshpam_session_open = 0;
119 static int sshpam_cred_established = 0;
122 sp_pthread_t pam_thread;
128 static void sshpam_free_ctx(void *);
129 static struct pam_ctxt *cleanup_ctxt;
132 * Conversation function for authentication thread.
135 sshpam_thread_conv(int n, const struct pam_message **msg,
136 struct pam_response **resp, void *data)
139 struct pam_ctxt *ctxt;
140 struct pam_response *reply;
146 if (n <= 0 || n > PAM_MAX_NUM_MSG)
147 return (PAM_CONV_ERR);
149 if ((reply = malloc(n * sizeof(*reply))) == NULL)
150 return (PAM_CONV_ERR);
151 memset(reply, 0, n * sizeof(*reply));
153 buffer_init(&buffer);
154 for (i = 0; i < n; ++i) {
155 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
156 case PAM_PROMPT_ECHO_OFF:
157 buffer_put_cstring(&buffer,
158 PAM_MSG_MEMBER(msg, i, msg));
159 ssh_msg_send(ctxt->pam_csock,
160 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
161 ssh_msg_recv(ctxt->pam_csock, &buffer);
162 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
164 reply[i].resp = buffer_get_string(&buffer, NULL);
166 case PAM_PROMPT_ECHO_ON:
167 buffer_put_cstring(&buffer,
168 PAM_MSG_MEMBER(msg, i, msg));
169 ssh_msg_send(ctxt->pam_csock,
170 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
171 ssh_msg_recv(ctxt->pam_csock, &buffer);
172 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
174 reply[i].resp = buffer_get_string(&buffer, NULL);
177 buffer_put_cstring(&buffer,
178 PAM_MSG_MEMBER(msg, i, msg));
179 ssh_msg_send(ctxt->pam_csock,
180 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
183 buffer_put_cstring(&buffer,
184 PAM_MSG_MEMBER(msg, i, msg));
185 ssh_msg_send(ctxt->pam_csock,
186 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
191 buffer_clear(&buffer);
193 buffer_free(&buffer);
195 return (PAM_SUCCESS);
198 for(i = 0; i < n; i++) {
199 if (reply[i].resp != NULL)
200 xfree(reply[i].resp);
203 buffer_free(&buffer);
204 return (PAM_CONV_ERR);
208 * Authentication thread.
211 sshpam_thread(void *ctxtp)
213 struct pam_ctxt *ctxt = ctxtp;
215 struct pam_conv sshpam_conv;
216 #ifndef USE_POSIX_THREADS
217 const char *pam_user;
219 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
220 setproctitle("%s [pam]", pam_user);
223 sshpam_conv.conv = sshpam_thread_conv;
224 sshpam_conv.appdata_ptr = ctxt;
226 buffer_init(&buffer);
227 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
228 (const void *)&sshpam_conv);
229 if (sshpam_err != PAM_SUCCESS)
231 sshpam_err = pam_authenticate(sshpam_handle, 0);
232 if (sshpam_err != PAM_SUCCESS)
234 buffer_put_cstring(&buffer, "OK");
235 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
236 buffer_free(&buffer);
240 buffer_put_cstring(&buffer,
241 pam_strerror(sshpam_handle, sshpam_err));
242 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
243 buffer_free(&buffer);
246 return (NULL); /* Avoid warning for non-pthread case */
250 sshpam_thread_cleanup(void)
252 struct pam_ctxt *ctxt = cleanup_ctxt;
254 if (ctxt != NULL && ctxt->pam_thread != 0) {
255 pthread_cancel(ctxt->pam_thread);
256 pthread_join(ctxt->pam_thread, NULL);
257 close(ctxt->pam_psock);
258 close(ctxt->pam_csock);
259 memset(ctxt, 0, sizeof(*ctxt));
265 sshpam_null_conv(int n, const struct pam_message **msg,
266 struct pam_response **resp, void *data)
268 return (PAM_CONV_ERR);
271 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
276 debug("PAM: cleanup");
277 if (sshpam_handle == NULL)
279 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
280 if (sshpam_cred_established) {
281 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
282 sshpam_cred_established = 0;
284 if (sshpam_session_open) {
285 pam_close_session(sshpam_handle, PAM_SILENT);
286 sshpam_session_open = 0;
288 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
289 pam_end(sshpam_handle, sshpam_err);
290 sshpam_handle = NULL;
294 sshpam_init(const char *user)
296 extern u_int utmp_len;
297 extern char *__progname;
298 const char *pam_rhost, *pam_user;
300 if (sshpam_handle != NULL) {
301 /* We already have a PAM context; check if the user matches */
302 sshpam_err = pam_get_item(sshpam_handle,
303 PAM_USER, (const void **)&pam_user);
304 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
306 pam_end(sshpam_handle, sshpam_err);
307 sshpam_handle = NULL;
309 debug("PAM: initializing for \"%s\"", user);
311 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
312 if (sshpam_err != PAM_SUCCESS) {
313 pam_end(sshpam_handle, sshpam_err);
314 sshpam_handle = NULL;
317 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
318 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
319 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
320 if (sshpam_err != PAM_SUCCESS) {
321 pam_end(sshpam_handle, sshpam_err);
322 sshpam_handle = NULL;
325 #ifdef PAM_TTY_KLUDGE
327 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
328 * sshd doesn't set the tty until too late in the auth process and
329 * may not even set one (for tty-less connections)
331 debug("PAM: setting PAM_TTY to \"ssh\"");
332 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
333 if (sshpam_err != PAM_SUCCESS) {
334 pam_end(sshpam_handle, sshpam_err);
335 sshpam_handle = NULL;
343 sshpam_init_ctx(Authctxt *authctxt)
345 struct pam_ctxt *ctxt;
348 /* Refuse to start if we don't have PAM enabled */
349 if (!options.use_pam)
353 if (sshpam_init(authctxt->user) == -1) {
354 error("PAM: initialization failed");
358 ctxt = xmalloc(sizeof *ctxt);
359 memset(ctxt, 0, sizeof(*ctxt));
361 /* Start the authentication thread */
362 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
363 error("PAM: failed create sockets: %s", strerror(errno));
367 ctxt->pam_psock = socks[0];
368 ctxt->pam_csock = socks[1];
369 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
370 error("PAM: failed to start authentication thread: %s",
382 sshpam_query(void *ctx, char **name, char **info,
383 u_int *num, char ***prompts, u_int **echo_on)
386 struct pam_ctxt *ctxt = ctx;
392 buffer_init(&buffer);
395 *prompts = xmalloc(sizeof(char *));
398 *echo_on = xmalloc(sizeof(u_int));
399 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
400 type = buffer_get_char(&buffer);
401 msg = buffer_get_string(&buffer, NULL);
403 case PAM_PROMPT_ECHO_ON:
404 case PAM_PROMPT_ECHO_OFF:
406 len = plen + strlen(msg) + 1;
407 **prompts = xrealloc(**prompts, len);
408 plen += snprintf(**prompts + plen, len, "%s", msg);
409 **echo_on = (type == PAM_PROMPT_ECHO_ON);
414 /* accumulate messages */
415 len = plen + strlen(msg) + 1;
416 **prompts = xrealloc(**prompts, len);
417 plen += snprintf(**prompts + plen, len, "%s", msg);
422 if (**prompts != NULL) {
423 /* drain any accumulated messages */
424 #if 0 /* XXX - not compatible with privsep */
425 packet_start(SSH2_MSG_USERAUTH_BANNER);
426 packet_put_cstring(**prompts);
427 packet_put_cstring("");
434 if (type == PAM_SUCCESS) {
441 error("PAM: %s", msg);
453 /* XXX - see also comment in auth-chall.c:verify_response */
455 sshpam_respond(void *ctx, u_int num, char **resp)
458 struct pam_ctxt *ctxt = ctx;
460 debug2("PAM: %s", __func__);
461 switch (ctxt->pam_done) {
463 sshpam_authenticated = 1;
471 error("PAM: expected one response, got %u", num);
474 buffer_init(&buffer);
475 buffer_put_cstring(&buffer, *resp);
476 ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer);
477 buffer_free(&buffer);
482 sshpam_free_ctx(void *ctxtp)
484 struct pam_ctxt *ctxt = ctxtp;
486 sshpam_thread_cleanup();
489 * We don't call sshpam_cleanup() here because we may need the PAM
490 * handle at a later stage, e.g. when setting up a session. It's
491 * still on the cleanup list, so pam_end() *will* be called before
492 * the server process terminates.
496 KbdintDevice sshpam_device = {
504 KbdintDevice mm_sshpam_device = {
513 * This replaces auth-pam.c
516 start_pam(const char *user)
518 if (!options.use_pam)
519 fatal("PAM: initialisation requested when UsePAM=no");
521 if (sshpam_init(user) == -1)
522 fatal("PAM: initialisation failed");
534 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
535 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
537 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD)
540 if (sshpam_err == PAM_NEW_AUTHTOK_REQD) {
541 sshpam_new_authtok_reqd = 1;
543 /* Prevent forwardings until password changed */
544 no_port_forwarding_flag |= 2;
545 no_agent_forwarding_flag |= 2;
546 no_x11_forwarding_flag |= 2;
555 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
556 (const void *)&null_conv);
557 if (sshpam_err != PAM_SUCCESS)
558 fatal("PAM: failed to set PAM_CONV: %s",
559 pam_strerror(sshpam_handle, sshpam_err));
560 sshpam_err = pam_open_session(sshpam_handle, 0);
561 if (sshpam_err != PAM_SUCCESS)
562 fatal("PAM: pam_open_session(): %s",
563 pam_strerror(sshpam_handle, sshpam_err));
564 sshpam_session_open = 1;
568 do_pam_set_tty(const char *tty)
571 debug("PAM: setting PAM_TTY to \"%s\"", tty);
572 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
573 if (sshpam_err != PAM_SUCCESS)
574 fatal("PAM: failed to set PAM_TTY: %s",
575 pam_strerror(sshpam_handle, sshpam_err));
580 do_pam_setcred(int init)
582 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
583 (const void *)&null_conv);
584 if (sshpam_err != PAM_SUCCESS)
585 fatal("PAM: failed to set PAM_CONV: %s",
586 pam_strerror(sshpam_handle, sshpam_err));
588 debug("PAM: establishing credentials");
589 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
591 debug("PAM: reinitializing credentials");
592 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
594 if (sshpam_err == PAM_SUCCESS) {
595 sshpam_cred_established = 1;
598 if (sshpam_authenticated)
599 fatal("PAM: pam_setcred(): %s",
600 pam_strerror(sshpam_handle, sshpam_err));
602 debug("PAM: pam_setcred(): %s",
603 pam_strerror(sshpam_handle, sshpam_err));
607 is_pam_password_change_required(void)
609 return (sshpam_new_authtok_reqd);
613 pam_chauthtok_conv(int n, const struct pam_message **msg,
614 struct pam_response **resp, void *data)
616 char input[PAM_MAX_MSG_SIZE];
617 struct pam_response *reply;
622 if (n <= 0 || n > PAM_MAX_NUM_MSG)
623 return (PAM_CONV_ERR);
625 if ((reply = malloc(n * sizeof(*reply))) == NULL)
626 return (PAM_CONV_ERR);
627 memset(reply, 0, n * sizeof(*reply));
629 for (i = 0; i < n; ++i) {
630 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
631 case PAM_PROMPT_ECHO_OFF:
633 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
635 reply[i].resp_retcode = PAM_SUCCESS;
637 case PAM_PROMPT_ECHO_ON:
638 fputs(PAM_MSG_MEMBER(msg, i, msg), stderr);
639 fgets(input, sizeof input, stdin);
640 reply[i].resp = xstrdup(input);
641 reply[i].resp_retcode = PAM_SUCCESS;
645 fputs(PAM_MSG_MEMBER(msg, i, msg), stderr);
646 reply[i].resp_retcode = PAM_SUCCESS;
653 return (PAM_SUCCESS);
656 for(i = 0; i < n; i++) {
657 if (reply[i].resp != NULL)
658 xfree(reply[i].resp);
661 return (PAM_CONV_ERR);
665 * XXX this should be done in the authentication phase, but ssh1 doesn't
669 do_pam_chauthtok(void)
671 struct pam_conv pam_conv;
673 pam_conv.conv = pam_chauthtok_conv;
674 pam_conv.appdata_ptr = NULL;
677 fatal("Password expired (unable to change with privsep)");
678 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
679 (const void *)&pam_conv);
680 if (sshpam_err != PAM_SUCCESS)
681 fatal("PAM: failed to set PAM_CONV: %s",
682 pam_strerror(sshpam_handle, sshpam_err));
683 debug("PAM: changing password");
684 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
685 if (sshpam_err != PAM_SUCCESS)
686 fatal("PAM: pam_chauthtok(): %s",
687 pam_strerror(sshpam_handle, sshpam_err));
691 * Set a PAM environment string. We need to do this so that the session
692 * modules can handle things like Kerberos/GSI credentials that appear
693 * during the ssh authentication process.
697 do_pam_putenv(char *name, char *value)
700 #ifdef HAVE_PAM_PUTENV
704 len = strlen(name) + strlen(value) + 2;
705 compound = xmalloc(len);
707 snprintf(compound, len, "%s=%s", name, value);
708 ret = pam_putenv(sshpam_handle, compound);
716 print_pam_messages(void)
722 fetch_pam_environment(void)
724 #ifdef HAVE_PAM_GETENVLIST
725 debug("PAM: retrieving environment");
726 return (pam_getenvlist(sshpam_handle));
733 free_pam_environment(char **env)
740 for (envp = env; *envp; envp++)