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 #if defined(HAVE_SECURITY_PAM_APPL_H)
38 #include <security/pam_appl.h>
39 #elif defined (HAVE_PAM_PAM_APPL_H)
40 #include <pam/pam_appl.h>
49 #include "monitor_wrap.h"
56 #include "auth-options.h"
58 extern ServerOptions options;
59 extern Buffer loginmsg;
61 extern u_int utmp_len;
63 #ifdef USE_POSIX_THREADS
66 * Avoid namespace clash when *not* using pthreads for systems *with*
67 * pthreads, which unconditionally define pthread_t via sys/types.h
70 typedef pthread_t sp_pthread_t;
72 typedef pid_t sp_pthread_t;
76 sp_pthread_t pam_thread;
82 static void sshpam_free_ctx(void *);
83 static struct pam_ctxt *cleanup_ctxt;
85 #ifndef USE_POSIX_THREADS
87 * Simulate threads with processes.
90 static int sshpam_thread_status = -1;
91 static mysig_t sshpam_oldsig;
94 sshpam_sigchld_handler(int sig)
96 signal(SIGCHLD, SIG_DFL);
97 if (cleanup_ctxt == NULL)
98 return; /* handler called after PAM cleanup, shouldn't happen */
99 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
101 /* PAM thread has not exitted, privsep slave must have */
102 kill(cleanup_ctxt->pam_thread, SIGTERM);
103 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
105 return; /* could not wait */
107 if (WIFSIGNALED(sshpam_thread_status) &&
108 WTERMSIG(sshpam_thread_status) == SIGTERM)
109 return; /* terminated by pthread_cancel */
110 if (!WIFEXITED(sshpam_thread_status))
111 fatal("PAM: authentication thread exited unexpectedly");
112 if (WEXITSTATUS(sshpam_thread_status) != 0)
113 fatal("PAM: authentication thread exited uncleanly");
117 pthread_exit(void *value __unused)
123 pthread_create(sp_pthread_t *thread, const void *attr __unused,
124 void *(*thread_start)(void *), void *arg)
128 sshpam_thread_status = -1;
129 switch ((pid = fork())) {
131 error("fork(): %s", strerror(errno));
138 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
144 pthread_cancel(sp_pthread_t thread)
146 signal(SIGCHLD, sshpam_oldsig);
147 return (kill(thread, SIGTERM));
151 pthread_join(sp_pthread_t thread, void **value __unused)
155 if (sshpam_thread_status != -1)
156 return (sshpam_thread_status);
157 signal(SIGCHLD, sshpam_oldsig);
158 waitpid(thread, &status, 0);
164 static pam_handle_t *sshpam_handle = NULL;
165 static int sshpam_err = 0;
166 static int sshpam_authenticated = 0;
167 static int sshpam_session_open = 0;
168 static int sshpam_cred_established = 0;
169 static int sshpam_account_status = -1;
170 static char **sshpam_env = NULL;
171 static Authctxt *sshpam_authctxt = NULL;
172 static const char *sshpam_password = NULL;
174 /* Some PAM implementations don't implement this */
175 #ifndef HAVE_PAM_GETENVLIST
177 pam_getenvlist(pam_handle_t *pamh)
180 * XXX - If necessary, we can still support envrionment passing
181 * for platforms without pam_getenvlist by searching for known
182 * env vars (e.g. KRB5CCNAME) from the PAM environment.
189 pam_password_change_required(int reqd)
191 debug3("%s %d", __func__, reqd);
192 if (sshpam_authctxt == NULL)
193 fatal("%s: PAM authctxt not initialized", __func__);
194 sshpam_authctxt->force_pwchange = reqd;
196 no_port_forwarding_flag |= 2;
197 no_agent_forwarding_flag |= 2;
198 no_x11_forwarding_flag |= 2;
200 no_port_forwarding_flag &= ~2;
201 no_agent_forwarding_flag &= ~2;
202 no_x11_forwarding_flag &= ~2;
206 /* Import regular and PAM environment from subprocess */
208 import_environments(Buffer *b)
214 debug3("PAM: %s entering", __func__);
216 #ifndef USE_POSIX_THREADS
217 /* Import variables set by do_pam_account */
218 sshpam_account_status = buffer_get_int(b);
219 pam_password_change_required(buffer_get_int(b));
221 /* Import environment from subprocess */
222 num_env = buffer_get_int(b);
223 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
224 debug3("PAM: num env strings %d", num_env);
225 for(i = 0; i < num_env; i++)
226 sshpam_env[i] = buffer_get_string(b, NULL);
228 sshpam_env[num_env] = NULL;
230 /* Import PAM environment from subprocess */
231 num_env = buffer_get_int(b);
232 debug("PAM: num PAM env strings %d", num_env);
233 for(i = 0; i < num_env; i++) {
234 env = buffer_get_string(b, NULL);
236 #ifdef HAVE_PAM_PUTENV
237 /* Errors are not fatal here */
238 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
239 error("PAM: pam_putenv: %s",
240 pam_strerror(sshpam_handle, sshpam_err));
248 * Conversation function for authentication thread.
251 sshpam_thread_conv(int n, const struct pam_message **msg,
252 struct pam_response **resp, void *data)
255 struct pam_ctxt *ctxt;
256 struct pam_response *reply;
259 debug3("PAM: %s entering, %d messages", __func__, n);
263 if (n <= 0 || n > PAM_MAX_NUM_MSG)
264 return (PAM_CONV_ERR);
266 if ((reply = malloc(n * sizeof(*reply))) == NULL)
267 return (PAM_CONV_ERR);
268 memset(reply, 0, n * sizeof(*reply));
270 buffer_init(&buffer);
271 for (i = 0; i < n; ++i) {
272 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
273 case PAM_PROMPT_ECHO_OFF:
274 buffer_put_cstring(&buffer,
275 PAM_MSG_MEMBER(msg, i, msg));
276 if (ssh_msg_send(ctxt->pam_csock,
277 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
279 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
281 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
283 reply[i].resp = buffer_get_string(&buffer, NULL);
285 case PAM_PROMPT_ECHO_ON:
286 buffer_put_cstring(&buffer,
287 PAM_MSG_MEMBER(msg, i, msg));
288 if (ssh_msg_send(ctxt->pam_csock,
289 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
291 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
293 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
295 reply[i].resp = buffer_get_string(&buffer, NULL);
298 buffer_put_cstring(&buffer,
299 PAM_MSG_MEMBER(msg, i, msg));
300 if (ssh_msg_send(ctxt->pam_csock,
301 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
305 buffer_put_cstring(&buffer,
306 PAM_MSG_MEMBER(msg, i, msg));
307 if (ssh_msg_send(ctxt->pam_csock,
308 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
314 buffer_clear(&buffer);
316 buffer_free(&buffer);
318 return (PAM_SUCCESS);
321 for(i = 0; i < n; i++) {
322 if (reply[i].resp != NULL)
323 xfree(reply[i].resp);
326 buffer_free(&buffer);
327 return (PAM_CONV_ERR);
331 * Authentication thread.
334 sshpam_thread(void *ctxtp)
336 struct pam_ctxt *ctxt = ctxtp;
338 struct pam_conv sshpam_conv;
339 #ifndef USE_POSIX_THREADS
340 extern char **environ;
343 const char *pam_user;
345 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
346 setproctitle("%s [pam]", pam_user);
350 sshpam_conv.conv = sshpam_thread_conv;
351 sshpam_conv.appdata_ptr = ctxt;
353 if (sshpam_authctxt == NULL)
354 fatal("%s: PAM authctxt not initialized", __func__);
356 buffer_init(&buffer);
357 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
358 (const void *)&sshpam_conv);
359 if (sshpam_err != PAM_SUCCESS)
361 sshpam_err = pam_authenticate(sshpam_handle, 0);
362 if (sshpam_err != PAM_SUCCESS)
366 if (!do_pam_account())
368 if (sshpam_authctxt->force_pwchange) {
369 sshpam_err = pam_chauthtok(sshpam_handle,
370 PAM_CHANGE_EXPIRED_AUTHTOK);
371 if (sshpam_err != PAM_SUCCESS)
373 pam_password_change_required(0);
377 buffer_put_cstring(&buffer, "OK");
379 #ifndef USE_POSIX_THREADS
380 /* Export variables set by do_pam_account */
381 buffer_put_int(&buffer, sshpam_account_status);
382 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
384 /* Export any environment strings set in child */
385 for(i = 0; environ[i] != NULL; i++)
387 buffer_put_int(&buffer, i);
388 for(i = 0; environ[i] != NULL; i++)
389 buffer_put_cstring(&buffer, environ[i]);
391 /* Export any environment strings set by PAM in child */
392 env_from_pam = pam_getenvlist(sshpam_handle);
393 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
395 buffer_put_int(&buffer, i);
396 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
397 buffer_put_cstring(&buffer, env_from_pam[i]);
398 #endif /* USE_POSIX_THREADS */
400 /* XXX - can't do much about an error here */
401 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
402 buffer_free(&buffer);
406 buffer_put_cstring(&buffer,
407 pam_strerror(sshpam_handle, sshpam_err));
408 /* XXX - can't do much about an error here */
409 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
410 buffer_free(&buffer);
413 return (NULL); /* Avoid warning for non-pthread case */
417 sshpam_thread_cleanup(void)
419 struct pam_ctxt *ctxt = cleanup_ctxt;
421 debug3("PAM: %s entering", __func__);
422 if (ctxt != NULL && ctxt->pam_thread != 0) {
423 pthread_cancel(ctxt->pam_thread);
424 pthread_join(ctxt->pam_thread, NULL);
425 close(ctxt->pam_psock);
426 close(ctxt->pam_csock);
427 memset(ctxt, 0, sizeof(*ctxt));
433 sshpam_null_conv(int n, const struct pam_message **msg,
434 struct pam_response **resp, void *data)
436 debug3("PAM: %s entering, %d messages", __func__, n);
437 return (PAM_CONV_ERR);
440 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
445 debug("PAM: cleanup");
446 if (sshpam_handle == NULL)
448 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
449 if (sshpam_cred_established) {
450 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
451 sshpam_cred_established = 0;
453 if (sshpam_session_open) {
454 pam_close_session(sshpam_handle, PAM_SILENT);
455 sshpam_session_open = 0;
457 sshpam_authenticated = 0;
458 pam_end(sshpam_handle, sshpam_err);
459 sshpam_handle = NULL;
463 sshpam_init(Authctxt *authctxt)
465 extern char *__progname;
466 const char *pam_rhost, *pam_user, *user = authctxt->user;
468 if (sshpam_handle != NULL) {
469 /* We already have a PAM context; check if the user matches */
470 sshpam_err = pam_get_item(sshpam_handle,
471 PAM_USER, (const void **)&pam_user);
472 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
474 pam_end(sshpam_handle, sshpam_err);
475 sshpam_handle = NULL;
477 debug("PAM: initializing for \"%s\"", user);
479 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
480 sshpam_authctxt = authctxt;
482 if (sshpam_err != PAM_SUCCESS) {
483 pam_end(sshpam_handle, sshpam_err);
484 sshpam_handle = NULL;
487 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
488 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
489 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
490 if (sshpam_err != PAM_SUCCESS) {
491 pam_end(sshpam_handle, sshpam_err);
492 sshpam_handle = NULL;
495 #ifdef PAM_TTY_KLUDGE
497 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
498 * sshd doesn't set the tty until too late in the auth process and
499 * may not even set one (for tty-less connections)
501 debug("PAM: setting PAM_TTY to \"ssh\"");
502 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
503 if (sshpam_err != PAM_SUCCESS) {
504 pam_end(sshpam_handle, sshpam_err);
505 sshpam_handle = NULL;
513 sshpam_init_ctx(Authctxt *authctxt)
515 struct pam_ctxt *ctxt;
518 debug3("PAM: %s entering", __func__);
519 /* Refuse to start if we don't have PAM enabled */
520 if (!options.use_pam)
524 if (sshpam_init(authctxt) == -1) {
525 error("PAM: initialization failed");
529 ctxt = xmalloc(sizeof *ctxt);
530 memset(ctxt, 0, sizeof(*ctxt));
532 /* Start the authentication thread */
533 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
534 error("PAM: failed create sockets: %s", strerror(errno));
538 ctxt->pam_psock = socks[0];
539 ctxt->pam_csock = socks[1];
540 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
541 error("PAM: failed to start authentication thread: %s",
553 sshpam_query(void *ctx, char **name, char **info,
554 u_int *num, char ***prompts, u_int **echo_on)
557 struct pam_ctxt *ctxt = ctx;
563 debug3("PAM: %s entering", __func__);
564 buffer_init(&buffer);
567 *prompts = xmalloc(sizeof(char *));
570 *echo_on = xmalloc(sizeof(u_int));
571 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
572 type = buffer_get_char(&buffer);
573 msg = buffer_get_string(&buffer, NULL);
575 case PAM_PROMPT_ECHO_ON:
576 case PAM_PROMPT_ECHO_OFF:
578 len = plen + strlen(msg) + 1;
579 **prompts = xrealloc(**prompts, len);
580 plen += snprintf(**prompts + plen, len, "%s", msg);
581 **echo_on = (type == PAM_PROMPT_ECHO_ON);
586 /* accumulate messages */
587 len = plen + strlen(msg) + 2;
588 **prompts = xrealloc(**prompts, len);
589 plen += snprintf(**prompts + plen, len, "%s\n", msg);
594 if (**prompts != NULL) {
595 /* drain any accumulated messages */
596 debug("PAM: %s", **prompts);
597 buffer_append(&loginmsg, **prompts,
602 if (type == PAM_SUCCESS) {
603 import_environments(&buffer);
610 error("PAM: %s for %s%.100s from %.100s", msg,
611 sshpam_authctxt->valid ? "" : "illegal user ",
612 sshpam_authctxt->user,
613 get_remote_name_or_ip(utmp_len, options.use_dns));
626 /* XXX - see also comment in auth-chall.c:verify_response */
628 sshpam_respond(void *ctx, u_int num, char **resp)
631 struct pam_ctxt *ctxt = ctx;
633 debug2("PAM: %s entering, %d responses", __func__, num);
634 switch (ctxt->pam_done) {
636 sshpam_authenticated = 1;
644 error("PAM: expected one response, got %u", num);
647 buffer_init(&buffer);
648 buffer_put_cstring(&buffer, *resp);
649 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
650 buffer_free(&buffer);
653 buffer_free(&buffer);
658 sshpam_free_ctx(void *ctxtp)
660 struct pam_ctxt *ctxt = ctxtp;
662 debug3("PAM: %s entering", __func__);
663 sshpam_thread_cleanup();
666 * We don't call sshpam_cleanup() here because we may need the PAM
667 * handle at a later stage, e.g. when setting up a session. It's
668 * still on the cleanup list, so pam_end() *will* be called before
669 * the server process terminates.
673 KbdintDevice sshpam_device = {
681 KbdintDevice mm_sshpam_device = {
690 * This replaces auth-pam.c
693 start_pam(Authctxt *authctxt)
695 if (!options.use_pam)
696 fatal("PAM: initialisation requested when UsePAM=no");
698 if (sshpam_init(authctxt) == -1)
699 fatal("PAM: initialisation failed");
711 if (sshpam_account_status != -1)
712 return (sshpam_account_status);
714 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
715 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
717 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
718 sshpam_account_status = 0;
719 return (sshpam_account_status);
722 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
723 pam_password_change_required(1);
725 sshpam_account_status = 1;
726 return (sshpam_account_status);
730 do_pam_set_tty(const char *tty)
733 debug("PAM: setting PAM_TTY to \"%s\"", tty);
734 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
735 if (sshpam_err != PAM_SUCCESS)
736 fatal("PAM: failed to set PAM_TTY: %s",
737 pam_strerror(sshpam_handle, sshpam_err));
742 do_pam_setcred(int init)
744 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
745 (const void *)&null_conv);
746 if (sshpam_err != PAM_SUCCESS)
747 fatal("PAM: failed to set PAM_CONV: %s",
748 pam_strerror(sshpam_handle, sshpam_err));
750 debug("PAM: establishing credentials");
751 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
753 debug("PAM: reinitializing credentials");
754 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
756 if (sshpam_err == PAM_SUCCESS) {
757 sshpam_cred_established = 1;
760 if (sshpam_authenticated)
761 fatal("PAM: pam_setcred(): %s",
762 pam_strerror(sshpam_handle, sshpam_err));
764 debug("PAM: pam_setcred(): %s",
765 pam_strerror(sshpam_handle, sshpam_err));
769 pam_tty_conv(int n, const struct pam_message **msg,
770 struct pam_response **resp, void *data)
772 char input[PAM_MAX_MSG_SIZE];
773 struct pam_response *reply;
776 debug3("PAM: %s called with %d messages", __func__, n);
780 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
781 return (PAM_CONV_ERR);
783 if ((reply = malloc(n * sizeof(*reply))) == NULL)
784 return (PAM_CONV_ERR);
785 memset(reply, 0, n * sizeof(*reply));
787 for (i = 0; i < n; ++i) {
788 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
789 case PAM_PROMPT_ECHO_OFF:
791 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
793 reply[i].resp_retcode = PAM_SUCCESS;
795 case PAM_PROMPT_ECHO_ON:
796 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
797 fgets(input, sizeof input, stdin);
798 reply[i].resp = xstrdup(input);
799 reply[i].resp_retcode = PAM_SUCCESS;
803 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
804 reply[i].resp_retcode = PAM_SUCCESS;
811 return (PAM_SUCCESS);
814 for(i = 0; i < n; i++) {
815 if (reply[i].resp != NULL)
816 xfree(reply[i].resp);
819 return (PAM_CONV_ERR);
822 static struct pam_conv tty_conv = { pam_tty_conv, NULL };
825 * XXX this should be done in the authentication phase, but ssh1 doesn't
829 do_pam_chauthtok(void)
832 fatal("Password expired (unable to change with privsep)");
833 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
834 (const void *)&tty_conv);
835 if (sshpam_err != PAM_SUCCESS)
836 fatal("PAM: failed to set PAM_CONV: %s",
837 pam_strerror(sshpam_handle, sshpam_err));
838 debug("PAM: changing password");
839 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
840 if (sshpam_err != PAM_SUCCESS)
841 fatal("PAM: pam_chauthtok(): %s",
842 pam_strerror(sshpam_handle, sshpam_err));
846 pam_store_conv(int n, const struct pam_message **msg,
847 struct pam_response **resp, void *data)
849 struct pam_response *reply;
853 debug3("PAM: %s called with %d messages", __func__, n);
856 if (n <= 0 || n > PAM_MAX_NUM_MSG)
857 return (PAM_CONV_ERR);
859 if ((reply = malloc(n * sizeof(*reply))) == NULL)
860 return (PAM_CONV_ERR);
861 memset(reply, 0, n * sizeof(*reply));
863 for (i = 0; i < n; ++i) {
864 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
867 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
868 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
869 buffer_append(&loginmsg, "\n", 1 );
870 reply[i].resp_retcode = PAM_SUCCESS;
877 return (PAM_SUCCESS);
880 for(i = 0; i < n; i++) {
881 if (reply[i].resp != NULL)
882 xfree(reply[i].resp);
885 return (PAM_CONV_ERR);
888 static struct pam_conv store_conv = { pam_store_conv, NULL };
893 debug3("PAM: opening session");
894 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
895 (const void *)&store_conv);
896 if (sshpam_err != PAM_SUCCESS)
897 fatal("PAM: failed to set PAM_CONV: %s",
898 pam_strerror(sshpam_handle, sshpam_err));
899 sshpam_err = pam_open_session(sshpam_handle, 0);
900 if (sshpam_err != PAM_SUCCESS)
901 fatal("PAM: pam_open_session(): %s",
902 pam_strerror(sshpam_handle, sshpam_err));
903 sshpam_session_open = 1;
907 * Set a PAM environment string. We need to do this so that the session
908 * modules can handle things like Kerberos/GSI credentials that appear
909 * during the ssh authentication process.
912 do_pam_putenv(char *name, char *value)
915 #ifdef HAVE_PAM_PUTENV
919 len = strlen(name) + strlen(value) + 2;
920 compound = xmalloc(len);
922 snprintf(compound, len, "%s=%s", name, value);
923 ret = pam_putenv(sshpam_handle, compound);
931 fetch_pam_child_environment(void)
937 fetch_pam_environment(void)
939 return (pam_getenvlist(sshpam_handle));
943 free_pam_environment(char **env)
950 for (envp = env; *envp; envp++)
956 * "Blind" conversation function for password authentication. Assumes that
957 * echo-off prompts are for the password and stores messages for later
961 sshpam_passwd_conv(int n, const struct pam_message **msg,
962 struct pam_response **resp, void *data)
964 struct pam_response *reply;
968 debug3("PAM: %s called with %d messages", __func__, n);
972 if (n <= 0 || n > PAM_MAX_NUM_MSG)
973 return (PAM_CONV_ERR);
975 if ((reply = malloc(n * sizeof(*reply))) == NULL)
976 return (PAM_CONV_ERR);
977 memset(reply, 0, n * sizeof(*reply));
979 for (i = 0; i < n; ++i) {
980 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
981 case PAM_PROMPT_ECHO_OFF:
982 if (sshpam_password == NULL)
984 reply[i].resp = xstrdup(sshpam_password);
985 reply[i].resp_retcode = PAM_SUCCESS;
989 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
991 buffer_append(&loginmsg,
992 PAM_MSG_MEMBER(msg, i, msg), len);
993 buffer_append(&loginmsg, "\n", 1);
995 reply[i].resp = xstrdup("");
996 reply[i].resp_retcode = PAM_SUCCESS;
1003 return (PAM_SUCCESS);
1006 for(i = 0; i < n; i++) {
1007 if (reply[i].resp != NULL)
1008 xfree(reply[i].resp);
1011 return (PAM_CONV_ERR);
1014 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1017 * Attempt password authentication via PAM
1020 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1022 int flags = (options.permit_empty_passwd == 0 ?
1023 PAM_DISALLOW_NULL_AUTHTOK : 0);
1025 if (!options.use_pam || sshpam_handle == NULL)
1026 fatal("PAM: %s called when PAM disabled or failed to "
1027 "initialise.", __func__);
1029 sshpam_password = password;
1030 sshpam_authctxt = authctxt;
1032 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1033 (const void *)&passwd_conv);
1034 if (sshpam_err != PAM_SUCCESS)
1035 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1036 pam_strerror(sshpam_handle, sshpam_err));
1038 sshpam_err = pam_authenticate(sshpam_handle, flags);
1039 sshpam_password = NULL;
1040 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1041 debug("PAM: password authentication accepted for %.100s",
1045 debug("PAM: password authentication failed for %.100s: %s",
1046 authctxt->valid ? authctxt->user : "an illegal user",
1047 pam_strerror(sshpam_handle, sshpam_err));
1051 #endif /* USE_PAM */