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
33 RCSID("$FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.1.2.6 2003/04/07 09:56:46 des Exp $");
34 RCSID("$DragonFly: src/crypto/openssh/Attic/auth2-pam-freebsd.c,v 1.2 2003/06/17 04:24:36 dillon Exp $");
37 #include <security/pam_appl.h>
45 #include "monitor_wrap.h"
53 #ifdef USE_POSIX_THREADS
57 * Simulate threads with processes.
59 typedef pid_t pthread_t;
62 pthread_exit(void *value __unused)
68 pthread_create(pthread_t *thread, const void *attr __unused,
69 void *(*thread_start)(void *), void *arg)
73 switch ((pid = fork())) {
75 error("fork(): %s", strerror(errno));
87 pthread_cancel(pthread_t thread)
89 return (kill(thread, SIGTERM));
93 pthread_join(pthread_t thread, void **value __unused)
97 waitpid(thread, &status, 0);
103 static pam_handle_t *pam_handle;
105 static int pam_authenticated;
106 static int pam_new_authtok_reqd;
107 static int pam_session_open;
108 static int pam_cred_established;
111 pthread_t pam_thread;
117 static void pam_free_ctx(void *);
120 * Conversation function for authentication thread.
123 pam_thread_conv(int n,
124 const struct pam_message **msg,
125 struct pam_response **resp,
129 struct pam_ctxt *ctxt;
133 if (n <= 0 || n > PAM_MAX_NUM_MSG)
134 return (PAM_CONV_ERR);
135 *resp = xmalloc(n * sizeof **resp);
136 buffer_init(&buffer);
137 for (i = 0; i < n; ++i) {
138 resp[i]->resp_retcode = 0;
139 resp[i]->resp = NULL;
140 switch (msg[i]->msg_style) {
141 case PAM_PROMPT_ECHO_OFF:
142 buffer_put_cstring(&buffer, msg[i]->msg);
143 ssh_msg_send(ctxt->pam_csock, msg[i]->msg_style, &buffer);
144 ssh_msg_recv(ctxt->pam_csock, &buffer);
145 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
147 resp[i]->resp = buffer_get_string(&buffer, NULL);
149 case PAM_PROMPT_ECHO_ON:
150 buffer_put_cstring(&buffer, msg[i]->msg);
151 ssh_msg_send(ctxt->pam_csock, msg[i]->msg_style, &buffer);
152 ssh_msg_recv(ctxt->pam_csock, &buffer);
153 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
155 resp[i]->resp = buffer_get_string(&buffer, NULL);
158 buffer_put_cstring(&buffer, msg[i]->msg);
159 ssh_msg_send(ctxt->pam_csock, msg[i]->msg_style, &buffer);
162 buffer_put_cstring(&buffer, msg[i]->msg);
163 ssh_msg_send(ctxt->pam_csock, msg[i]->msg_style, &buffer);
168 buffer_clear(&buffer);
170 buffer_free(&buffer);
171 return (PAM_SUCCESS);
177 buffer_free(&buffer);
178 return (PAM_CONV_ERR);
182 * Authentication thread.
185 pam_thread(void *ctxtp)
187 struct pam_ctxt *ctxt = ctxtp;
189 struct pam_conv pam_conv = { pam_thread_conv, ctxt };
191 #ifndef USE_POSIX_THREADS
193 const char *pam_user;
195 pam_get_item(pam_handle, PAM_USER, (const void **)&pam_user);
196 setproctitle("%s [pam]", pam_user);
199 buffer_init(&buffer);
200 pam_err = pam_set_item(pam_handle, PAM_CONV, (const void *)&pam_conv);
201 if (pam_err != PAM_SUCCESS)
203 pam_err = pam_authenticate(pam_handle, 0);
204 if (pam_err != PAM_SUCCESS)
206 pam_err = pam_acct_mgmt(pam_handle, 0);
207 if (pam_err != PAM_SUCCESS && pam_err != PAM_NEW_AUTHTOK_REQD)
209 buffer_put_cstring(&buffer, "OK");
210 ssh_msg_send(ctxt->pam_csock, pam_err, &buffer);
211 buffer_free(&buffer);
214 buffer_put_cstring(&buffer,
215 pam_strerror(pam_handle, pam_err));
216 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
217 buffer_free(&buffer);
222 pam_thread_cleanup(void *ctxtp)
224 struct pam_ctxt *ctxt = ctxtp;
226 pthread_cancel(ctxt->pam_thread);
227 pthread_join(ctxt->pam_thread, NULL);
228 close(ctxt->pam_psock);
229 close(ctxt->pam_csock);
234 const struct pam_message **msg,
235 struct pam_response **resp,
239 return (PAM_CONV_ERR);
242 static struct pam_conv null_conv = { pam_null_conv, NULL };
245 pam_cleanup(void *arg)
248 debug("PAM: cleanup");
249 pam_set_item(pam_handle, PAM_CONV, (const void *)&null_conv);
250 if (pam_cred_established) {
251 pam_setcred(pam_handle, PAM_DELETE_CRED);
252 pam_cred_established = 0;
254 if (pam_session_open) {
255 pam_close_session(pam_handle, PAM_SILENT);
256 pam_session_open = 0;
258 pam_authenticated = pam_new_authtok_reqd = 0;
259 pam_end(pam_handle, pam_err);
264 pam_init(const char *user)
266 extern ServerOptions options;
267 extern u_int utmp_len;
268 const char *pam_rhost, *pam_user;
270 if (pam_handle != NULL) {
271 /* We already have a PAM context; check if the user matches */
272 pam_err = pam_get_item(pam_handle,
273 PAM_USER, (const void **)&pam_user);
274 if (pam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
276 fatal_remove_cleanup(pam_cleanup, NULL);
277 pam_end(pam_handle, pam_err);
280 debug("PAM: initializing for \"%s\"", user);
281 pam_err = pam_start("sshd", user, &null_conv, &pam_handle);
282 if (pam_err != PAM_SUCCESS)
284 pam_rhost = get_remote_name_or_ip(utmp_len,
285 options.verify_reverse_mapping);
286 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
287 pam_err = pam_set_item(pam_handle, PAM_RHOST, pam_rhost);
288 if (pam_err != PAM_SUCCESS) {
289 pam_end(pam_handle, pam_err);
293 fatal_add_cleanup(pam_cleanup, NULL);
298 pam_init_ctx(Authctxt *authctxt)
300 struct pam_ctxt *ctxt;
304 if (pam_init(authctxt->user) == -1) {
305 error("PAM: initialization failed");
309 ctxt = xmalloc(sizeof *ctxt);
312 /* Start the authentication thread */
313 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
314 error("PAM: failed create sockets: %s", strerror(errno));
318 ctxt->pam_psock = socks[0];
319 ctxt->pam_csock = socks[1];
320 if (pthread_create(&ctxt->pam_thread, NULL, pam_thread, ctxt) == -1) {
321 error("PAM: failed to start authentication thread: %s",
328 fatal_add_cleanup(pam_thread_cleanup, ctxt);
333 pam_query(void *ctx, char **name, char **info,
334 u_int *num, char ***prompts, u_int **echo_on)
337 struct pam_ctxt *ctxt = ctx;
342 buffer_init(&buffer);
345 *prompts = xmalloc(sizeof(char *));
348 *echo_on = xmalloc(sizeof(u_int));
349 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
350 type = buffer_get_char(&buffer);
351 msg = buffer_get_string(&buffer, NULL);
353 case PAM_PROMPT_ECHO_ON:
354 case PAM_PROMPT_ECHO_OFF:
356 **prompts = xrealloc(**prompts, plen + strlen(msg) + 1);
357 plen += sprintf(**prompts + plen, "%s", msg);
358 **echo_on = (type == PAM_PROMPT_ECHO_ON);
363 /* accumulate messages */
364 **prompts = xrealloc(**prompts, plen + strlen(msg) + 1);
365 plen += sprintf(**prompts + plen, "%s", msg);
368 case PAM_NEW_AUTHTOK_REQD:
369 pam_new_authtok_reqd = 1;
373 if (**prompts != NULL) {
374 /* drain any accumulated messages */
375 #if 0 /* not compatible with privsep */
376 packet_start(SSH2_MSG_USERAUTH_BANNER);
377 packet_put_cstring(**prompts);
378 packet_put_cstring("");
385 if (type == PAM_SUCCESS) {
392 error("PAM: %s", msg);
405 pam_respond(void *ctx, u_int num, char **resp)
408 struct pam_ctxt *ctxt = ctx;
411 debug2("PAM: %s", __func__);
412 switch (ctxt->pam_done) {
414 pam_authenticated = 1;
422 error("PAM: expected one response, got %u", num);
425 buffer_init(&buffer);
426 buffer_put_cstring(&buffer, *resp);
427 ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer);
428 buffer_free(&buffer);
433 pam_free_ctx(void *ctxtp)
435 struct pam_ctxt *ctxt = ctxtp;
437 fatal_remove_cleanup(pam_thread_cleanup, ctxt);
438 pam_thread_cleanup(ctxtp);
441 * We don't call pam_cleanup() here because we may need the PAM
442 * handle at a later stage, e.g. when setting up a session. It's
443 * still on the cleanup list, so pam_end() *will* be called before
444 * the server process terminates.
448 KbdintDevice pam_device = {
456 KbdintDevice mm_pam_device = {
465 * This replaces auth-pam.c
468 start_pam(const char *user)
470 if (pam_init(user) == -1)
471 fatal("PAM: initialisation failed");
477 fatal_remove_cleanup(pam_cleanup, NULL);
482 do_pam_account(const char *user, const char *ruser)
489 do_pam_session(const char *user, const char *tty)
491 pam_err = pam_set_item(pam_handle, PAM_CONV, (const void *)&null_conv);
492 if (pam_err != PAM_SUCCESS)
493 fatal("PAM: failed to set PAM_CONV: %s",
494 pam_strerror(pam_handle, pam_err));
495 debug("PAM: setting PAM_TTY to \"%s\"", tty);
496 pam_err = pam_set_item(pam_handle, PAM_TTY, tty);
497 if (pam_err != PAM_SUCCESS)
498 fatal("PAM: failed to set PAM_TTY: %s",
499 pam_strerror(pam_handle, pam_err));
500 pam_err = pam_open_session(pam_handle, 0);
501 if (pam_err != PAM_SUCCESS)
502 fatal("PAM: pam_open_session(): %s",
503 pam_strerror(pam_handle, pam_err));
504 pam_session_open = 1;
508 do_pam_setcred(int init)
510 pam_err = pam_set_item(pam_handle, PAM_CONV, (const void *)&null_conv);
511 if (pam_err != PAM_SUCCESS)
512 fatal("PAM: failed to set PAM_CONV: %s",
513 pam_strerror(pam_handle, pam_err));
515 debug("PAM: establishing credentials");
516 pam_err = pam_setcred(pam_handle, PAM_ESTABLISH_CRED);
518 debug("PAM: reinitializing credentials");
519 pam_err = pam_setcred(pam_handle, PAM_REINITIALIZE_CRED);
521 if (pam_err == PAM_SUCCESS) {
522 pam_cred_established = 1;
525 if (pam_authenticated)
526 fatal("PAM: pam_setcred(): %s",
527 pam_strerror(pam_handle, pam_err));
529 debug("PAM: pam_setcred(): %s",
530 pam_strerror(pam_handle, pam_err));
534 is_pam_password_change_required(void)
536 return (pam_new_authtok_reqd);
540 pam_chauthtok_conv(int n,
541 const struct pam_message **msg,
542 struct pam_response **resp,
545 char input[PAM_MAX_MSG_SIZE];
548 if (n <= 0 || n > PAM_MAX_NUM_MSG)
549 return (PAM_CONV_ERR);
550 *resp = xmalloc(n * sizeof **resp);
551 for (i = 0; i < n; ++i) {
552 switch (msg[i]->msg_style) {
553 case PAM_PROMPT_ECHO_OFF:
555 read_passphrase(msg[i]->msg, RP_ALLOW_STDIN);
556 resp[i]->resp_retcode = PAM_SUCCESS;
558 case PAM_PROMPT_ECHO_ON:
559 fputs(msg[i]->msg, stderr);
560 fgets(input, sizeof input, stdin);
561 resp[i]->resp = xstrdup(input);
562 resp[i]->resp_retcode = PAM_SUCCESS;
566 fputs(msg[i]->msg, stderr);
567 resp[i]->resp_retcode = PAM_SUCCESS;
573 return (PAM_SUCCESS);
579 return (PAM_CONV_ERR);
583 * XXX this should be done in the authentication phase, but ssh1 doesn't
587 do_pam_chauthtok(void)
589 struct pam_conv pam_conv = { pam_chauthtok_conv, NULL };
592 fatal("PAM: chauthtok not supprted with privsep");
593 pam_err = pam_set_item(pam_handle, PAM_CONV, (const void *)&pam_conv);
594 if (pam_err != PAM_SUCCESS)
595 fatal("PAM: failed to set PAM_CONV: %s",
596 pam_strerror(pam_handle, pam_err));
597 debug("PAM: changing password");
598 pam_err = pam_chauthtok(pam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
599 if (pam_err != PAM_SUCCESS)
600 fatal("PAM: pam_chauthtok(): %s",
601 pam_strerror(pam_handle, pam_err));
605 print_pam_messages(void)
611 fetch_pam_environment(void)
613 #ifdef HAVE_PAM_GETENVLIST
614 debug("PAM: retrieving environment");
615 return (pam_getenvlist(pam_handle));
622 free_pam_environment(char **env)
626 for (envp = env; *envp; envp++)