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;
115 static int sshpam_err;
116 static int sshpam_authenticated;
117 static int sshpam_new_authtok_reqd;
118 static int sshpam_session_open;
119 static int sshpam_cred_established;
122 sp_pthread_t pam_thread;
128 static void sshpam_free_ctx(void *);
131 * Conversation function for authentication thread.
134 sshpam_thread_conv(int n, const struct pam_message **msg,
135 struct pam_response **resp, void *data)
138 struct pam_ctxt *ctxt;
142 if (n <= 0 || n > PAM_MAX_NUM_MSG)
143 return (PAM_CONV_ERR);
144 *resp = xmalloc(n * sizeof **resp);
145 buffer_init(&buffer);
146 for (i = 0; i < n; ++i) {
147 resp[i]->resp_retcode = 0;
148 resp[i]->resp = NULL;
149 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
150 case PAM_PROMPT_ECHO_OFF:
151 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
152 ssh_msg_send(ctxt->pam_csock,
153 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
154 ssh_msg_recv(ctxt->pam_csock, &buffer);
155 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
157 resp[i]->resp = buffer_get_string(&buffer, NULL);
159 case PAM_PROMPT_ECHO_ON:
160 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
161 ssh_msg_send(ctxt->pam_csock,
162 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
163 ssh_msg_recv(ctxt->pam_csock, &buffer);
164 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
166 resp[i]->resp = buffer_get_string(&buffer, NULL);
169 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
170 ssh_msg_send(ctxt->pam_csock,
171 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
174 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
175 ssh_msg_send(ctxt->pam_csock,
176 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
181 buffer_clear(&buffer);
183 buffer_free(&buffer);
184 return (PAM_SUCCESS);
190 buffer_free(&buffer);
191 return (PAM_CONV_ERR);
195 * Authentication thread.
198 sshpam_thread(void *ctxtp)
200 struct pam_ctxt *ctxt = ctxtp;
202 struct pam_conv sshpam_conv;
203 #ifndef USE_POSIX_THREADS
204 const char *pam_user;
206 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
207 setproctitle("%s [pam]", pam_user);
210 sshpam_conv.conv = sshpam_thread_conv;
211 sshpam_conv.appdata_ptr = ctxt;
213 buffer_init(&buffer);
214 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
215 (const void *)&sshpam_conv);
216 if (sshpam_err != PAM_SUCCESS)
218 sshpam_err = pam_authenticate(sshpam_handle, 0);
219 if (sshpam_err != PAM_SUCCESS)
221 buffer_put_cstring(&buffer, "OK");
222 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
223 buffer_free(&buffer);
227 buffer_put_cstring(&buffer,
228 pam_strerror(sshpam_handle, sshpam_err));
229 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
230 buffer_free(&buffer);
233 return (NULL); /* Avoid warning for non-pthread case */
237 sshpam_thread_cleanup(void *ctxtp)
239 struct pam_ctxt *ctxt = ctxtp;
241 pthread_cancel(ctxt->pam_thread);
242 pthread_join(ctxt->pam_thread, NULL);
243 close(ctxt->pam_psock);
244 close(ctxt->pam_csock);
248 sshpam_null_conv(int n, const struct pam_message **msg,
249 struct pam_response **resp, void *data)
251 return (PAM_CONV_ERR);
254 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
257 sshpam_cleanup(void *arg)
260 debug("PAM: cleanup");
261 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
262 if (sshpam_cred_established) {
263 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
264 sshpam_cred_established = 0;
266 if (sshpam_session_open) {
267 pam_close_session(sshpam_handle, PAM_SILENT);
268 sshpam_session_open = 0;
270 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
271 pam_end(sshpam_handle, sshpam_err);
272 sshpam_handle = NULL;
276 sshpam_init(const char *user)
278 extern u_int utmp_len;
279 const char *pam_rhost, *pam_user;
281 if (sshpam_handle != NULL) {
282 /* We already have a PAM context; check if the user matches */
283 sshpam_err = pam_get_item(sshpam_handle,
284 PAM_USER, (const void **)&pam_user);
285 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
287 fatal_remove_cleanup(sshpam_cleanup, NULL);
288 pam_end(sshpam_handle, sshpam_err);
289 sshpam_handle = NULL;
291 debug("PAM: initializing for \"%s\"", user);
292 sshpam_err = pam_start("sshd", user, &null_conv, &sshpam_handle);
293 if (sshpam_err != PAM_SUCCESS) {
294 pam_end(sshpam_handle, sshpam_err);
295 sshpam_handle = NULL;
298 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
299 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
300 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
301 if (sshpam_err != PAM_SUCCESS) {
302 pam_end(sshpam_handle, sshpam_err);
303 sshpam_handle = NULL;
306 #ifdef PAM_TTY_KLUDGE
308 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
309 * sshd doesn't set the tty until too late in the auth process and
310 * may not even set one (for tty-less connections)
312 debug("PAM: setting PAM_TTY to \"ssh\"");
313 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
314 if (sshpam_err != PAM_SUCCESS) {
315 pam_end(sshpam_handle, sshpam_err);
316 sshpam_handle = NULL;
320 fatal_add_cleanup(sshpam_cleanup, NULL);
325 sshpam_init_ctx(Authctxt *authctxt)
327 struct pam_ctxt *ctxt;
330 /* Refuse to start if we don't have PAM enabled */
331 if (!options.use_pam)
335 if (sshpam_init(authctxt->user) == -1) {
336 error("PAM: initialization failed");
340 ctxt = xmalloc(sizeof *ctxt);
343 /* Start the authentication thread */
344 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
345 error("PAM: failed create sockets: %s", strerror(errno));
349 ctxt->pam_psock = socks[0];
350 ctxt->pam_csock = socks[1];
351 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
352 error("PAM: failed to start authentication thread: %s",
359 fatal_add_cleanup(sshpam_thread_cleanup, ctxt);
364 sshpam_query(void *ctx, char **name, char **info,
365 u_int *num, char ***prompts, u_int **echo_on)
368 struct pam_ctxt *ctxt = ctx;
374 buffer_init(&buffer);
377 *prompts = xmalloc(sizeof(char *));
380 *echo_on = xmalloc(sizeof(u_int));
381 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
382 type = buffer_get_char(&buffer);
383 msg = buffer_get_string(&buffer, NULL);
385 case PAM_PROMPT_ECHO_ON:
386 case PAM_PROMPT_ECHO_OFF:
388 len = plen + strlen(msg) + 1;
389 **prompts = xrealloc(**prompts, len);
390 plen += snprintf(**prompts + plen, len, "%s", msg);
391 **echo_on = (type == PAM_PROMPT_ECHO_ON);
396 /* accumulate messages */
397 len = plen + strlen(msg) + 1;
398 **prompts = xrealloc(**prompts, len);
399 plen += snprintf(**prompts + plen, len, "%s", msg);
404 if (**prompts != NULL) {
405 /* drain any accumulated messages */
406 #if 0 /* XXX - not compatible with privsep */
407 packet_start(SSH2_MSG_USERAUTH_BANNER);
408 packet_put_cstring(**prompts);
409 packet_put_cstring("");
416 if (type == PAM_SUCCESS) {
423 error("PAM: %s", msg);
435 /* XXX - see also comment in auth-chall.c:verify_response */
437 sshpam_respond(void *ctx, u_int num, char **resp)
440 struct pam_ctxt *ctxt = ctx;
442 debug2("PAM: %s", __func__);
443 switch (ctxt->pam_done) {
445 sshpam_authenticated = 1;
453 error("PAM: expected one response, got %u", num);
456 buffer_init(&buffer);
457 buffer_put_cstring(&buffer, *resp);
458 ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer);
459 buffer_free(&buffer);
464 sshpam_free_ctx(void *ctxtp)
466 struct pam_ctxt *ctxt = ctxtp;
468 fatal_remove_cleanup(sshpam_thread_cleanup, ctxt);
469 sshpam_thread_cleanup(ctxtp);
472 * We don't call sshpam_cleanup() here because we may need the PAM
473 * handle at a later stage, e.g. when setting up a session. It's
474 * still on the cleanup list, so pam_end() *will* be called before
475 * the server process terminates.
479 KbdintDevice sshpam_device = {
487 KbdintDevice mm_sshpam_device = {
496 * This replaces auth-pam.c
499 start_pam(const char *user)
501 if (!options.use_pam)
502 fatal("PAM: initialisation requested when UsePAM=no");
504 if (sshpam_init(user) == -1)
505 fatal("PAM: initialisation failed");
511 fatal_remove_cleanup(sshpam_cleanup, NULL);
512 sshpam_cleanup(NULL);
518 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
519 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
521 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD)
524 if (sshpam_err == PAM_NEW_AUTHTOK_REQD) {
525 sshpam_new_authtok_reqd = 1;
527 /* Prevent forwardings until password changed */
528 no_port_forwarding_flag |= 2;
529 no_agent_forwarding_flag |= 2;
530 no_x11_forwarding_flag |= 2;
537 do_pam_session(const char *user, const char *tty)
539 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
540 (const void *)&null_conv);
541 if (sshpam_err != PAM_SUCCESS)
542 fatal("PAM: failed to set PAM_CONV: %s",
543 pam_strerror(sshpam_handle, sshpam_err));
545 debug("PAM: setting PAM_TTY to \"%s\"", tty);
546 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
547 if (sshpam_err != PAM_SUCCESS)
548 fatal("PAM: failed to set PAM_TTY: %s",
549 pam_strerror(sshpam_handle, sshpam_err));
551 sshpam_err = pam_open_session(sshpam_handle, 0);
552 if (sshpam_err != PAM_SUCCESS)
553 fatal("PAM: pam_open_session(): %s",
554 pam_strerror(sshpam_handle, sshpam_err));
555 sshpam_session_open = 1;
559 do_pam_setcred(int init)
561 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
562 (const void *)&null_conv);
563 if (sshpam_err != PAM_SUCCESS)
564 fatal("PAM: failed to set PAM_CONV: %s",
565 pam_strerror(sshpam_handle, sshpam_err));
567 debug("PAM: establishing credentials");
568 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
570 debug("PAM: reinitializing credentials");
571 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
573 if (sshpam_err == PAM_SUCCESS) {
574 sshpam_cred_established = 1;
577 if (sshpam_authenticated)
578 fatal("PAM: pam_setcred(): %s",
579 pam_strerror(sshpam_handle, sshpam_err));
581 debug("PAM: pam_setcred(): %s",
582 pam_strerror(sshpam_handle, sshpam_err));
586 is_pam_password_change_required(void)
588 return (sshpam_new_authtok_reqd);
592 pam_chauthtok_conv(int n, const struct pam_message **msg,
593 struct pam_response **resp, void *data)
595 char input[PAM_MAX_MSG_SIZE];
598 if (n <= 0 || n > PAM_MAX_NUM_MSG)
599 return (PAM_CONV_ERR);
600 *resp = xmalloc(n * sizeof **resp);
601 for (i = 0; i < n; ++i) {
602 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
603 case PAM_PROMPT_ECHO_OFF:
605 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
607 resp[i]->resp_retcode = PAM_SUCCESS;
609 case PAM_PROMPT_ECHO_ON:
610 fputs(PAM_MSG_MEMBER(msg, i, msg), stderr);
611 fgets(input, sizeof input, stdin);
612 resp[i]->resp = xstrdup(input);
613 resp[i]->resp_retcode = PAM_SUCCESS;
617 fputs(PAM_MSG_MEMBER(msg, i, msg), stderr);
618 resp[i]->resp_retcode = PAM_SUCCESS;
624 return (PAM_SUCCESS);
630 return (PAM_CONV_ERR);
634 * XXX this should be done in the authentication phase, but ssh1 doesn't
638 do_pam_chauthtok(void)
640 struct pam_conv pam_conv;
642 pam_conv.conv = pam_chauthtok_conv;
643 pam_conv.appdata_ptr = NULL;
646 fatal("Password expired (unable to change with privsep)");
647 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
648 (const void *)&pam_conv);
649 if (sshpam_err != PAM_SUCCESS)
650 fatal("PAM: failed to set PAM_CONV: %s",
651 pam_strerror(sshpam_handle, sshpam_err));
652 debug("PAM: changing password");
653 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
654 if (sshpam_err != PAM_SUCCESS)
655 fatal("PAM: pam_chauthtok(): %s",
656 pam_strerror(sshpam_handle, sshpam_err));
660 * Set a PAM environment string. We need to do this so that the session
661 * modules can handle things like Kerberos/GSI credentials that appear
662 * during the ssh authentication process.
666 do_pam_putenv(char *name, char *value)
671 #ifdef HAVE_PAM_PUTENV
672 compound = xmalloc(strlen(name)+strlen(value)+2);
674 sprintf(compound,"%s=%s",name,value);
675 ret = pam_putenv(sshpam_handle,compound);
683 print_pam_messages(void)
689 fetch_pam_environment(void)
691 #ifdef HAVE_PAM_GETENVLIST
692 debug("PAM: retrieving environment");
693 return (pam_getenvlist(sshpam_handle));
700 free_pam_environment(char **env)
707 for (envp = env; *envp; envp++)