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 $");
36 #include <security/pam_appl.h>
44 #include "monitor_wrap.h"
52 #ifdef USE_POSIX_THREADS
56 * Simulate threads with processes.
58 typedef pid_t pthread_t;
61 pthread_exit(void *value __unused)
67 pthread_create(pthread_t *thread, const void *attr __unused,
68 void *(*thread_start)(void *), void *arg)
72 switch ((pid = fork())) {
74 error("fork(): %s", strerror(errno));
86 pthread_cancel(pthread_t thread)
88 return (kill(thread, SIGTERM));
92 pthread_join(pthread_t thread, void **value __unused)
96 waitpid(thread, &status, 0);
102 static pam_handle_t *pam_handle;
104 static int pam_authenticated;
105 static int pam_new_authtok_reqd;
106 static int pam_session_open;
107 static int pam_cred_established;
110 pthread_t pam_thread;
116 static void pam_free_ctx(void *);
119 * Conversation function for authentication thread.
122 pam_thread_conv(int n,
123 const struct pam_message **msg,
124 struct pam_response **resp,
128 struct pam_ctxt *ctxt;
132 if (n <= 0 || n > PAM_MAX_NUM_MSG)
133 return (PAM_CONV_ERR);
134 *resp = xmalloc(n * sizeof **resp);
135 buffer_init(&buffer);
136 for (i = 0; i < n; ++i) {
137 resp[i]->resp_retcode = 0;
138 resp[i]->resp = NULL;
139 switch (msg[i]->msg_style) {
140 case PAM_PROMPT_ECHO_OFF:
141 buffer_put_cstring(&buffer, msg[i]->msg);
142 ssh_msg_send(ctxt->pam_csock, msg[i]->msg_style, &buffer);
143 ssh_msg_recv(ctxt->pam_csock, &buffer);
144 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
146 resp[i]->resp = buffer_get_string(&buffer, NULL);
148 case PAM_PROMPT_ECHO_ON:
149 buffer_put_cstring(&buffer, msg[i]->msg);
150 ssh_msg_send(ctxt->pam_csock, msg[i]->msg_style, &buffer);
151 ssh_msg_recv(ctxt->pam_csock, &buffer);
152 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
154 resp[i]->resp = buffer_get_string(&buffer, NULL);
157 buffer_put_cstring(&buffer, msg[i]->msg);
158 ssh_msg_send(ctxt->pam_csock, msg[i]->msg_style, &buffer);
161 buffer_put_cstring(&buffer, msg[i]->msg);
162 ssh_msg_send(ctxt->pam_csock, msg[i]->msg_style, &buffer);
167 buffer_clear(&buffer);
169 buffer_free(&buffer);
170 return (PAM_SUCCESS);
176 buffer_free(&buffer);
177 return (PAM_CONV_ERR);
181 * Authentication thread.
184 pam_thread(void *ctxtp)
186 struct pam_ctxt *ctxt = ctxtp;
188 struct pam_conv pam_conv = { pam_thread_conv, ctxt };
190 #ifndef USE_POSIX_THREADS
192 const char *pam_user;
194 pam_get_item(pam_handle, PAM_USER, (const void **)&pam_user);
195 setproctitle("%s [pam]", pam_user);
198 buffer_init(&buffer);
199 pam_err = pam_set_item(pam_handle, PAM_CONV, (const void *)&pam_conv);
200 if (pam_err != PAM_SUCCESS)
202 pam_err = pam_authenticate(pam_handle, 0);
203 if (pam_err != PAM_SUCCESS)
205 pam_err = pam_acct_mgmt(pam_handle, 0);
206 if (pam_err != PAM_SUCCESS && pam_err != PAM_NEW_AUTHTOK_REQD)
208 buffer_put_cstring(&buffer, "OK");
209 ssh_msg_send(ctxt->pam_csock, pam_err, &buffer);
210 buffer_free(&buffer);
213 buffer_put_cstring(&buffer,
214 pam_strerror(pam_handle, pam_err));
215 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
216 buffer_free(&buffer);
221 pam_thread_cleanup(void *ctxtp)
223 struct pam_ctxt *ctxt = ctxtp;
225 pthread_cancel(ctxt->pam_thread);
226 pthread_join(ctxt->pam_thread, NULL);
227 close(ctxt->pam_psock);
228 close(ctxt->pam_csock);
233 const struct pam_message **msg,
234 struct pam_response **resp,
238 return (PAM_CONV_ERR);
241 static struct pam_conv null_conv = { pam_null_conv, NULL };
244 pam_cleanup(void *arg)
247 debug("PAM: cleanup");
248 pam_set_item(pam_handle, PAM_CONV, (const void *)&null_conv);
249 if (pam_cred_established) {
250 pam_setcred(pam_handle, PAM_DELETE_CRED);
251 pam_cred_established = 0;
253 if (pam_session_open) {
254 pam_close_session(pam_handle, PAM_SILENT);
255 pam_session_open = 0;
257 pam_authenticated = pam_new_authtok_reqd = 0;
258 pam_end(pam_handle, pam_err);
263 pam_init(const char *user)
265 extern ServerOptions options;
266 extern u_int utmp_len;
267 const char *pam_rhost, *pam_user;
269 if (pam_handle != NULL) {
270 /* We already have a PAM context; check if the user matches */
271 pam_err = pam_get_item(pam_handle,
272 PAM_USER, (const void **)&pam_user);
273 if (pam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
275 fatal_remove_cleanup(pam_cleanup, NULL);
276 pam_end(pam_handle, pam_err);
279 debug("PAM: initializing for \"%s\"", user);
280 pam_err = pam_start("sshd", user, &null_conv, &pam_handle);
281 if (pam_err != PAM_SUCCESS)
283 pam_rhost = get_remote_name_or_ip(utmp_len,
284 options.verify_reverse_mapping);
285 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
286 pam_err = pam_set_item(pam_handle, PAM_RHOST, pam_rhost);
287 if (pam_err != PAM_SUCCESS) {
288 pam_end(pam_handle, pam_err);
292 fatal_add_cleanup(pam_cleanup, NULL);
297 pam_init_ctx(Authctxt *authctxt)
299 struct pam_ctxt *ctxt;
303 if (pam_init(authctxt->user) == -1) {
304 error("PAM: initialization failed");
308 ctxt = xmalloc(sizeof *ctxt);
311 /* Start the authentication thread */
312 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
313 error("PAM: failed create sockets: %s", strerror(errno));
317 ctxt->pam_psock = socks[0];
318 ctxt->pam_csock = socks[1];
319 if (pthread_create(&ctxt->pam_thread, NULL, pam_thread, ctxt) == -1) {
320 error("PAM: failed to start authentication thread: %s",
327 fatal_add_cleanup(pam_thread_cleanup, ctxt);
332 pam_query(void *ctx, char **name, char **info,
333 u_int *num, char ***prompts, u_int **echo_on)
336 struct pam_ctxt *ctxt = ctx;
341 buffer_init(&buffer);
344 *prompts = xmalloc(sizeof(char *));
347 *echo_on = xmalloc(sizeof(u_int));
348 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
349 type = buffer_get_char(&buffer);
350 msg = buffer_get_string(&buffer, NULL);
352 case PAM_PROMPT_ECHO_ON:
353 case PAM_PROMPT_ECHO_OFF:
355 **prompts = xrealloc(**prompts, plen + strlen(msg) + 1);
356 plen += sprintf(**prompts + plen, "%s", msg);
357 **echo_on = (type == PAM_PROMPT_ECHO_ON);
362 /* accumulate messages */
363 **prompts = xrealloc(**prompts, plen + strlen(msg) + 1);
364 plen += sprintf(**prompts + plen, "%s", msg);
367 case PAM_NEW_AUTHTOK_REQD:
368 pam_new_authtok_reqd = 1;
372 if (**prompts != NULL) {
373 /* drain any accumulated messages */
374 #if 0 /* not compatible with privsep */
375 packet_start(SSH2_MSG_USERAUTH_BANNER);
376 packet_put_cstring(**prompts);
377 packet_put_cstring("");
384 if (type == PAM_SUCCESS) {
391 error("PAM: %s", msg);
404 pam_respond(void *ctx, u_int num, char **resp)
407 struct pam_ctxt *ctxt = ctx;
410 debug2("PAM: %s", __func__);
411 switch (ctxt->pam_done) {
413 pam_authenticated = 1;
421 error("PAM: expected one response, got %u", num);
424 buffer_init(&buffer);
425 buffer_put_cstring(&buffer, *resp);
426 ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer);
427 buffer_free(&buffer);
432 pam_free_ctx(void *ctxtp)
434 struct pam_ctxt *ctxt = ctxtp;
436 fatal_remove_cleanup(pam_thread_cleanup, ctxt);
437 pam_thread_cleanup(ctxtp);
440 * We don't call pam_cleanup() here because we may need the PAM
441 * handle at a later stage, e.g. when setting up a session. It's
442 * still on the cleanup list, so pam_end() *will* be called before
443 * the server process terminates.
447 KbdintDevice pam_device = {
455 KbdintDevice mm_pam_device = {
464 * This replaces auth-pam.c
467 start_pam(const char *user)
469 if (pam_init(user) == -1)
470 fatal("PAM: initialisation failed");
476 fatal_remove_cleanup(pam_cleanup, NULL);
481 do_pam_account(const char *user, const char *ruser)
488 do_pam_session(const char *user, const char *tty)
490 pam_err = pam_set_item(pam_handle, PAM_CONV, (const void *)&null_conv);
491 if (pam_err != PAM_SUCCESS)
492 fatal("PAM: failed to set PAM_CONV: %s",
493 pam_strerror(pam_handle, pam_err));
494 debug("PAM: setting PAM_TTY to \"%s\"", tty);
495 pam_err = pam_set_item(pam_handle, PAM_TTY, tty);
496 if (pam_err != PAM_SUCCESS)
497 fatal("PAM: failed to set PAM_TTY: %s",
498 pam_strerror(pam_handle, pam_err));
499 pam_err = pam_open_session(pam_handle, 0);
500 if (pam_err != PAM_SUCCESS)
501 fatal("PAM: pam_open_session(): %s",
502 pam_strerror(pam_handle, pam_err));
503 pam_session_open = 1;
507 do_pam_setcred(int init)
509 pam_err = pam_set_item(pam_handle, PAM_CONV, (const void *)&null_conv);
510 if (pam_err != PAM_SUCCESS)
511 fatal("PAM: failed to set PAM_CONV: %s",
512 pam_strerror(pam_handle, pam_err));
514 debug("PAM: establishing credentials");
515 pam_err = pam_setcred(pam_handle, PAM_ESTABLISH_CRED);
517 debug("PAM: reinitializing credentials");
518 pam_err = pam_setcred(pam_handle, PAM_REINITIALIZE_CRED);
520 if (pam_err == PAM_SUCCESS) {
521 pam_cred_established = 1;
524 if (pam_authenticated)
525 fatal("PAM: pam_setcred(): %s",
526 pam_strerror(pam_handle, pam_err));
528 debug("PAM: pam_setcred(): %s",
529 pam_strerror(pam_handle, pam_err));
533 is_pam_password_change_required(void)
535 return (pam_new_authtok_reqd);
539 pam_chauthtok_conv(int n,
540 const struct pam_message **msg,
541 struct pam_response **resp,
544 char input[PAM_MAX_MSG_SIZE];
547 if (n <= 0 || n > PAM_MAX_NUM_MSG)
548 return (PAM_CONV_ERR);
549 *resp = xmalloc(n * sizeof **resp);
550 for (i = 0; i < n; ++i) {
551 switch (msg[i]->msg_style) {
552 case PAM_PROMPT_ECHO_OFF:
554 read_passphrase(msg[i]->msg, RP_ALLOW_STDIN);
555 resp[i]->resp_retcode = PAM_SUCCESS;
557 case PAM_PROMPT_ECHO_ON:
558 fputs(msg[i]->msg, stderr);
559 fgets(input, sizeof input, stdin);
560 resp[i]->resp = xstrdup(input);
561 resp[i]->resp_retcode = PAM_SUCCESS;
565 fputs(msg[i]->msg, stderr);
566 resp[i]->resp_retcode = PAM_SUCCESS;
572 return (PAM_SUCCESS);
578 return (PAM_CONV_ERR);
582 * XXX this should be done in the authentication phase, but ssh1 doesn't
586 do_pam_chauthtok(void)
588 struct pam_conv pam_conv = { pam_chauthtok_conv, NULL };
591 fatal("PAM: chauthtok not supprted with privsep");
592 pam_err = pam_set_item(pam_handle, PAM_CONV, (const void *)&pam_conv);
593 if (pam_err != PAM_SUCCESS)
594 fatal("PAM: failed to set PAM_CONV: %s",
595 pam_strerror(pam_handle, pam_err));
596 debug("PAM: changing password");
597 pam_err = pam_chauthtok(pam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
598 if (pam_err != PAM_SUCCESS)
599 fatal("PAM: pam_chauthtok(): %s",
600 pam_strerror(pam_handle, pam_err));
604 print_pam_messages(void)
610 fetch_pam_environment(void)
612 #ifdef HAVE_PAM_GETENVLIST
613 debug("PAM: retrieving environment");
614 return (pam_getenvlist(pam_handle));
621 free_pam_environment(char **env)
625 for (envp = env; *envp; envp++)