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;
173 /* Some PAM implementations don't implement this */
174 #ifndef HAVE_PAM_GETENVLIST
176 pam_getenvlist(pam_handle_t *pamh)
179 * XXX - If necessary, we can still support envrionment passing
180 * for platforms without pam_getenvlist by searching for known
181 * env vars (e.g. KRB5CCNAME) from the PAM environment.
188 pam_password_change_required(int reqd)
190 debug3("%s %d", __func__, reqd);
191 if (sshpam_authctxt == NULL)
192 fatal("%s: PAM authctxt not initialized", __func__);
193 sshpam_authctxt->force_pwchange = reqd;
195 no_port_forwarding_flag |= 2;
196 no_agent_forwarding_flag |= 2;
197 no_x11_forwarding_flag |= 2;
199 no_port_forwarding_flag &= ~2;
200 no_agent_forwarding_flag &= ~2;
201 no_x11_forwarding_flag &= ~2;
205 /* Import regular and PAM environment from subprocess */
207 import_environments(Buffer *b)
213 debug3("PAM: %s entering", __func__);
215 #ifndef USE_POSIX_THREADS
216 /* Import variables set by do_pam_account */
217 sshpam_account_status = buffer_get_int(b);
218 pam_password_change_required(buffer_get_int(b));
220 /* Import environment from subprocess */
221 num_env = buffer_get_int(b);
222 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
223 debug3("PAM: num env strings %d", num_env);
224 for(i = 0; i < num_env; i++)
225 sshpam_env[i] = buffer_get_string(b, NULL);
227 sshpam_env[num_env] = NULL;
229 /* Import PAM environment from subprocess */
230 num_env = buffer_get_int(b);
231 debug("PAM: num PAM env strings %d", num_env);
232 for(i = 0; i < num_env; i++) {
233 env = buffer_get_string(b, NULL);
235 #ifdef HAVE_PAM_PUTENV
236 /* Errors are not fatal here */
237 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
238 error("PAM: pam_putenv: %s",
239 pam_strerror(sshpam_handle, sshpam_err));
247 * Conversation function for authentication thread.
250 sshpam_thread_conv(int n, const struct pam_message **msg,
251 struct pam_response **resp, void *data)
254 struct pam_ctxt *ctxt;
255 struct pam_response *reply;
258 debug3("PAM: %s entering, %d messages", __func__, n);
262 if (n <= 0 || n > PAM_MAX_NUM_MSG)
263 return (PAM_CONV_ERR);
265 if ((reply = malloc(n * sizeof(*reply))) == NULL)
266 return (PAM_CONV_ERR);
267 memset(reply, 0, n * sizeof(*reply));
269 buffer_init(&buffer);
270 for (i = 0; i < n; ++i) {
271 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
272 case PAM_PROMPT_ECHO_OFF:
273 buffer_put_cstring(&buffer,
274 PAM_MSG_MEMBER(msg, i, msg));
275 if (ssh_msg_send(ctxt->pam_csock,
276 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
278 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
280 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
282 reply[i].resp = buffer_get_string(&buffer, NULL);
284 case PAM_PROMPT_ECHO_ON:
285 buffer_put_cstring(&buffer,
286 PAM_MSG_MEMBER(msg, i, msg));
287 if (ssh_msg_send(ctxt->pam_csock,
288 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
290 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
292 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
294 reply[i].resp = buffer_get_string(&buffer, NULL);
297 buffer_put_cstring(&buffer,
298 PAM_MSG_MEMBER(msg, i, msg));
299 if (ssh_msg_send(ctxt->pam_csock,
300 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
304 buffer_put_cstring(&buffer,
305 PAM_MSG_MEMBER(msg, i, msg));
306 if (ssh_msg_send(ctxt->pam_csock,
307 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
313 buffer_clear(&buffer);
315 buffer_free(&buffer);
317 return (PAM_SUCCESS);
320 for(i = 0; i < n; i++) {
321 if (reply[i].resp != NULL)
322 xfree(reply[i].resp);
325 buffer_free(&buffer);
326 return (PAM_CONV_ERR);
330 * Authentication thread.
333 sshpam_thread(void *ctxtp)
335 struct pam_ctxt *ctxt = ctxtp;
337 struct pam_conv sshpam_conv;
338 #ifndef USE_POSIX_THREADS
339 extern char **environ;
342 const char *pam_user;
344 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
345 setproctitle("%s [pam]", pam_user);
349 sshpam_conv.conv = sshpam_thread_conv;
350 sshpam_conv.appdata_ptr = ctxt;
352 if (sshpam_authctxt == NULL)
353 fatal("%s: PAM authctxt not initialized", __func__);
355 buffer_init(&buffer);
356 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
357 (const void *)&sshpam_conv);
358 if (sshpam_err != PAM_SUCCESS)
360 sshpam_err = pam_authenticate(sshpam_handle, 0);
361 if (sshpam_err != PAM_SUCCESS)
365 if (!do_pam_account())
367 if (sshpam_authctxt->force_pwchange) {
368 sshpam_err = pam_chauthtok(sshpam_handle,
369 PAM_CHANGE_EXPIRED_AUTHTOK);
370 if (sshpam_err != PAM_SUCCESS)
372 pam_password_change_required(0);
376 buffer_put_cstring(&buffer, "OK");
378 #ifndef USE_POSIX_THREADS
379 /* Export variables set by do_pam_account */
380 buffer_put_int(&buffer, sshpam_account_status);
381 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
383 /* Export any environment strings set in child */
384 for(i = 0; environ[i] != NULL; i++)
386 buffer_put_int(&buffer, i);
387 for(i = 0; environ[i] != NULL; i++)
388 buffer_put_cstring(&buffer, environ[i]);
390 /* Export any environment strings set by PAM in child */
391 env_from_pam = pam_getenvlist(sshpam_handle);
392 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
394 buffer_put_int(&buffer, i);
395 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
396 buffer_put_cstring(&buffer, env_from_pam[i]);
397 #endif /* USE_POSIX_THREADS */
399 /* XXX - can't do much about an error here */
400 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
401 buffer_free(&buffer);
405 buffer_put_cstring(&buffer,
406 pam_strerror(sshpam_handle, sshpam_err));
407 /* XXX - can't do much about an error here */
408 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
409 buffer_free(&buffer);
412 return (NULL); /* Avoid warning for non-pthread case */
416 sshpam_thread_cleanup(void)
418 struct pam_ctxt *ctxt = cleanup_ctxt;
420 debug3("PAM: %s entering", __func__);
421 if (ctxt != NULL && ctxt->pam_thread != 0) {
422 pthread_cancel(ctxt->pam_thread);
423 pthread_join(ctxt->pam_thread, NULL);
424 close(ctxt->pam_psock);
425 close(ctxt->pam_csock);
426 memset(ctxt, 0, sizeof(*ctxt));
432 sshpam_null_conv(int n, const struct pam_message **msg,
433 struct pam_response **resp, void *data)
435 debug3("PAM: %s entering, %d messages", __func__, n);
436 return (PAM_CONV_ERR);
439 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
444 debug("PAM: cleanup");
445 if (sshpam_handle == NULL)
447 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
448 if (sshpam_cred_established) {
449 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
450 sshpam_cred_established = 0;
452 if (sshpam_session_open) {
453 pam_close_session(sshpam_handle, PAM_SILENT);
454 sshpam_session_open = 0;
456 sshpam_authenticated = 0;
457 pam_end(sshpam_handle, sshpam_err);
458 sshpam_handle = NULL;
462 sshpam_init(Authctxt *authctxt)
464 extern char *__progname;
465 const char *pam_rhost, *pam_user, *user = authctxt->user;
467 if (sshpam_handle != NULL) {
468 /* We already have a PAM context; check if the user matches */
469 sshpam_err = pam_get_item(sshpam_handle,
470 PAM_USER, (const void **)&pam_user);
471 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
473 pam_end(sshpam_handle, sshpam_err);
474 sshpam_handle = NULL;
476 debug("PAM: initializing for \"%s\"", user);
478 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
479 sshpam_authctxt = authctxt;
481 if (sshpam_err != PAM_SUCCESS) {
482 pam_end(sshpam_handle, sshpam_err);
483 sshpam_handle = NULL;
486 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
487 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
488 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
489 if (sshpam_err != PAM_SUCCESS) {
490 pam_end(sshpam_handle, sshpam_err);
491 sshpam_handle = NULL;
494 #ifdef PAM_TTY_KLUDGE
496 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
497 * sshd doesn't set the tty until too late in the auth process and
498 * may not even set one (for tty-less connections)
500 debug("PAM: setting PAM_TTY to \"ssh\"");
501 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
502 if (sshpam_err != PAM_SUCCESS) {
503 pam_end(sshpam_handle, sshpam_err);
504 sshpam_handle = NULL;
512 sshpam_init_ctx(Authctxt *authctxt)
514 struct pam_ctxt *ctxt;
517 debug3("PAM: %s entering", __func__);
518 /* Refuse to start if we don't have PAM enabled */
519 if (!options.use_pam)
523 if (sshpam_init(authctxt) == -1) {
524 error("PAM: initialization failed");
528 ctxt = xmalloc(sizeof *ctxt);
529 memset(ctxt, 0, sizeof(*ctxt));
531 /* Start the authentication thread */
532 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
533 error("PAM: failed create sockets: %s", strerror(errno));
537 ctxt->pam_psock = socks[0];
538 ctxt->pam_csock = socks[1];
539 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
540 error("PAM: failed to start authentication thread: %s",
552 sshpam_query(void *ctx, char **name, char **info,
553 u_int *num, char ***prompts, u_int **echo_on)
556 struct pam_ctxt *ctxt = ctx;
562 debug3("PAM: %s entering", __func__);
563 buffer_init(&buffer);
566 *prompts = xmalloc(sizeof(char *));
569 *echo_on = xmalloc(sizeof(u_int));
570 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
571 type = buffer_get_char(&buffer);
572 msg = buffer_get_string(&buffer, NULL);
574 case PAM_PROMPT_ECHO_ON:
575 case PAM_PROMPT_ECHO_OFF:
577 len = plen + strlen(msg) + 1;
578 **prompts = xrealloc(**prompts, len);
579 plen += snprintf(**prompts + plen, len, "%s", msg);
580 **echo_on = (type == PAM_PROMPT_ECHO_ON);
585 /* accumulate messages */
586 len = plen + strlen(msg) + 2;
587 **prompts = xrealloc(**prompts, len);
588 plen += snprintf(**prompts + plen, len, "%s\n", msg);
593 if (**prompts != NULL) {
594 /* drain any accumulated messages */
595 debug("PAM: %s", **prompts);
596 buffer_append(&loginmsg, **prompts,
601 if (type == PAM_SUCCESS) {
602 import_environments(&buffer);
609 error("PAM: %s for %s%.100s from %.100s", msg,
610 sshpam_authctxt->valid ? "" : "illegal user ",
611 sshpam_authctxt->user,
612 get_remote_name_or_ip(utmp_len, options.use_dns));
625 /* XXX - see also comment in auth-chall.c:verify_response */
627 sshpam_respond(void *ctx, u_int num, char **resp)
630 struct pam_ctxt *ctxt = ctx;
632 debug2("PAM: %s entering, %d responses", __func__, num);
633 switch (ctxt->pam_done) {
635 sshpam_authenticated = 1;
643 error("PAM: expected one response, got %u", num);
646 buffer_init(&buffer);
647 buffer_put_cstring(&buffer, *resp);
648 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
649 buffer_free(&buffer);
652 buffer_free(&buffer);
657 sshpam_free_ctx(void *ctxtp)
659 struct pam_ctxt *ctxt = ctxtp;
661 debug3("PAM: %s entering", __func__);
662 sshpam_thread_cleanup();
665 * We don't call sshpam_cleanup() here because we may need the PAM
666 * handle at a later stage, e.g. when setting up a session. It's
667 * still on the cleanup list, so pam_end() *will* be called before
668 * the server process terminates.
672 KbdintDevice sshpam_device = {
680 KbdintDevice mm_sshpam_device = {
689 * This replaces auth-pam.c
692 start_pam(Authctxt *authctxt)
694 if (!options.use_pam)
695 fatal("PAM: initialisation requested when UsePAM=no");
697 if (sshpam_init(authctxt) == -1)
698 fatal("PAM: initialisation failed");
710 if (sshpam_account_status != -1)
711 return (sshpam_account_status);
713 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
714 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
716 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
717 sshpam_account_status = 0;
718 return (sshpam_account_status);
721 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
722 pam_password_change_required(1);
724 sshpam_account_status = 1;
725 return (sshpam_account_status);
729 do_pam_set_tty(const char *tty)
732 debug("PAM: setting PAM_TTY to \"%s\"", tty);
733 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
734 if (sshpam_err != PAM_SUCCESS)
735 fatal("PAM: failed to set PAM_TTY: %s",
736 pam_strerror(sshpam_handle, sshpam_err));
741 do_pam_setcred(int init)
743 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
744 (const void *)&null_conv);
745 if (sshpam_err != PAM_SUCCESS)
746 fatal("PAM: failed to set PAM_CONV: %s",
747 pam_strerror(sshpam_handle, sshpam_err));
749 debug("PAM: establishing credentials");
750 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
752 debug("PAM: reinitializing credentials");
753 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
755 if (sshpam_err == PAM_SUCCESS) {
756 sshpam_cred_established = 1;
759 if (sshpam_authenticated)
760 fatal("PAM: pam_setcred(): %s",
761 pam_strerror(sshpam_handle, sshpam_err));
763 debug("PAM: pam_setcred(): %s",
764 pam_strerror(sshpam_handle, sshpam_err));
768 pam_tty_conv(int n, const struct pam_message **msg,
769 struct pam_response **resp, void *data)
771 char input[PAM_MAX_MSG_SIZE];
772 struct pam_response *reply;
775 debug3("PAM: %s called with %d messages", __func__, n);
779 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
780 return (PAM_CONV_ERR);
782 if ((reply = malloc(n * sizeof(*reply))) == NULL)
783 return (PAM_CONV_ERR);
784 memset(reply, 0, n * sizeof(*reply));
786 for (i = 0; i < n; ++i) {
787 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
788 case PAM_PROMPT_ECHO_OFF:
790 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
792 reply[i].resp_retcode = PAM_SUCCESS;
794 case PAM_PROMPT_ECHO_ON:
795 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
796 fgets(input, sizeof input, stdin);
797 reply[i].resp = xstrdup(input);
798 reply[i].resp_retcode = PAM_SUCCESS;
802 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
803 reply[i].resp_retcode = PAM_SUCCESS;
810 return (PAM_SUCCESS);
813 for(i = 0; i < n; i++) {
814 if (reply[i].resp != NULL)
815 xfree(reply[i].resp);
818 return (PAM_CONV_ERR);
821 static struct pam_conv tty_conv = { pam_tty_conv, NULL };
824 * XXX this should be done in the authentication phase, but ssh1 doesn't
828 do_pam_chauthtok(void)
831 fatal("Password expired (unable to change with privsep)");
832 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
833 (const void *)&tty_conv);
834 if (sshpam_err != PAM_SUCCESS)
835 fatal("PAM: failed to set PAM_CONV: %s",
836 pam_strerror(sshpam_handle, sshpam_err));
837 debug("PAM: changing password");
838 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
839 if (sshpam_err != PAM_SUCCESS)
840 fatal("PAM: pam_chauthtok(): %s",
841 pam_strerror(sshpam_handle, sshpam_err));
845 pam_store_conv(int n, const struct pam_message **msg,
846 struct pam_response **resp, void *data)
848 struct pam_response *reply;
852 debug3("PAM: %s called with %d messages", __func__, n);
855 if (n <= 0 || n > PAM_MAX_NUM_MSG)
856 return (PAM_CONV_ERR);
858 if ((reply = malloc(n * sizeof(*reply))) == NULL)
859 return (PAM_CONV_ERR);
860 memset(reply, 0, n * sizeof(*reply));
862 for (i = 0; i < n; ++i) {
863 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
866 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
867 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
868 buffer_append(&loginmsg, "\n", 1 );
869 reply[i].resp_retcode = PAM_SUCCESS;
876 return (PAM_SUCCESS);
879 for(i = 0; i < n; i++) {
880 if (reply[i].resp != NULL)
881 xfree(reply[i].resp);
884 return (PAM_CONV_ERR);
887 static struct pam_conv store_conv = { pam_store_conv, NULL };
892 debug3("PAM: opening session");
893 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
894 (const void *)&store_conv);
895 if (sshpam_err != PAM_SUCCESS)
896 fatal("PAM: failed to set PAM_CONV: %s",
897 pam_strerror(sshpam_handle, sshpam_err));
898 sshpam_err = pam_open_session(sshpam_handle, 0);
899 if (sshpam_err != PAM_SUCCESS)
900 fatal("PAM: pam_open_session(): %s",
901 pam_strerror(sshpam_handle, sshpam_err));
902 sshpam_session_open = 1;
906 * Set a PAM environment string. We need to do this so that the session
907 * modules can handle things like Kerberos/GSI credentials that appear
908 * during the ssh authentication process.
911 do_pam_putenv(char *name, char *value)
914 #ifdef HAVE_PAM_PUTENV
918 len = strlen(name) + strlen(value) + 2;
919 compound = xmalloc(len);
921 snprintf(compound, len, "%s=%s", name, value);
922 ret = pam_putenv(sshpam_handle, compound);
930 fetch_pam_child_environment(void)
936 fetch_pam_environment(void)
938 return (pam_getenvlist(sshpam_handle));
942 free_pam_environment(char **env)
949 for (envp = env; *envp; envp++)