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 $ */
34 RCSID("$Id: auth-pam.c,v 1.100 2004/04/18 01:00:26 dtucker 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 if (cleanup_ctxt == NULL)
97 return; /* handler called after PAM cleanup, shouldn't happen */
98 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0) == -1)
99 return; /* couldn't wait for process */
100 if (WIFSIGNALED(sshpam_thread_status) &&
101 WTERMSIG(sshpam_thread_status) == SIGTERM)
102 return; /* terminated by pthread_cancel */
103 if (!WIFEXITED(sshpam_thread_status))
104 fatal("PAM: authentication thread exited unexpectedly");
105 if (WEXITSTATUS(sshpam_thread_status) != 0)
106 fatal("PAM: authentication thread exited uncleanly");
110 pthread_exit(void *value __unused)
116 pthread_create(sp_pthread_t *thread, const void *attr __unused,
117 void *(*thread_start)(void *), void *arg)
121 sshpam_thread_status = -1;
122 switch ((pid = fork())) {
124 error("fork(): %s", strerror(errno));
131 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
137 pthread_cancel(sp_pthread_t thread)
139 signal(SIGCHLD, sshpam_oldsig);
140 return (kill(thread, SIGTERM));
144 pthread_join(sp_pthread_t thread, void **value __unused)
148 if (sshpam_thread_status != -1)
149 return (sshpam_thread_status);
150 signal(SIGCHLD, sshpam_oldsig);
151 waitpid(thread, &status, 0);
157 static pam_handle_t *sshpam_handle = NULL;
158 static int sshpam_err = 0;
159 static int sshpam_authenticated = 0;
160 static int sshpam_session_open = 0;
161 static int sshpam_cred_established = 0;
162 static int sshpam_account_status = -1;
163 static char **sshpam_env = NULL;
164 static Authctxt *sshpam_authctxt = NULL;
166 /* Some PAM implementations don't implement this */
167 #ifndef HAVE_PAM_GETENVLIST
169 pam_getenvlist(pam_handle_t *pamh)
172 * XXX - If necessary, we can still support envrionment passing
173 * for platforms without pam_getenvlist by searching for known
174 * env vars (e.g. KRB5CCNAME) from the PAM environment.
181 pam_password_change_required(int reqd)
183 debug3("%s %d", __func__, reqd);
184 if (sshpam_authctxt == NULL)
185 fatal("%s: PAM authctxt not initialized", __func__);
186 sshpam_authctxt->force_pwchange = reqd;
188 no_port_forwarding_flag |= 2;
189 no_agent_forwarding_flag |= 2;
190 no_x11_forwarding_flag |= 2;
192 no_port_forwarding_flag &= ~2;
193 no_agent_forwarding_flag &= ~2;
194 no_x11_forwarding_flag &= ~2;
198 /* Import regular and PAM environment from subprocess */
200 import_environments(Buffer *b)
206 debug3("PAM: %s entering", __func__);
208 #ifndef USE_POSIX_THREADS
209 /* Import variables set by do_pam_account */
210 sshpam_account_status = buffer_get_int(b);
211 pam_password_change_required(buffer_get_int(b));
213 /* Import environment from subprocess */
214 num_env = buffer_get_int(b);
215 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
216 debug3("PAM: num env strings %d", num_env);
217 for(i = 0; i < num_env; i++)
218 sshpam_env[i] = buffer_get_string(b, NULL);
220 sshpam_env[num_env] = NULL;
222 /* Import PAM environment from subprocess */
223 num_env = buffer_get_int(b);
224 debug("PAM: num PAM env strings %d", num_env);
225 for(i = 0; i < num_env; i++) {
226 env = buffer_get_string(b, NULL);
228 #ifdef HAVE_PAM_PUTENV
229 /* Errors are not fatal here */
230 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
231 error("PAM: pam_putenv: %s",
232 pam_strerror(sshpam_handle, sshpam_err));
240 * Conversation function for authentication thread.
243 sshpam_thread_conv(int n, const struct pam_message **msg,
244 struct pam_response **resp, void *data)
247 struct pam_ctxt *ctxt;
248 struct pam_response *reply;
251 debug3("PAM: %s entering, %d messages", __func__, n);
255 if (n <= 0 || n > PAM_MAX_NUM_MSG)
256 return (PAM_CONV_ERR);
258 if ((reply = malloc(n * sizeof(*reply))) == NULL)
259 return (PAM_CONV_ERR);
260 memset(reply, 0, n * sizeof(*reply));
262 buffer_init(&buffer);
263 for (i = 0; i < n; ++i) {
264 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
265 case PAM_PROMPT_ECHO_OFF:
266 buffer_put_cstring(&buffer,
267 PAM_MSG_MEMBER(msg, i, msg));
268 if (ssh_msg_send(ctxt->pam_csock,
269 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
271 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
273 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
275 reply[i].resp = buffer_get_string(&buffer, NULL);
277 case PAM_PROMPT_ECHO_ON:
278 buffer_put_cstring(&buffer,
279 PAM_MSG_MEMBER(msg, i, msg));
280 if (ssh_msg_send(ctxt->pam_csock,
281 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
283 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
285 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
287 reply[i].resp = buffer_get_string(&buffer, NULL);
290 buffer_put_cstring(&buffer,
291 PAM_MSG_MEMBER(msg, i, msg));
292 if (ssh_msg_send(ctxt->pam_csock,
293 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
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)
306 buffer_clear(&buffer);
308 buffer_free(&buffer);
310 return (PAM_SUCCESS);
313 for(i = 0; i < n; i++) {
314 if (reply[i].resp != NULL)
315 xfree(reply[i].resp);
318 buffer_free(&buffer);
319 return (PAM_CONV_ERR);
323 * Authentication thread.
326 sshpam_thread(void *ctxtp)
328 struct pam_ctxt *ctxt = ctxtp;
330 struct pam_conv sshpam_conv;
331 #ifndef USE_POSIX_THREADS
332 extern char **environ;
335 const char *pam_user;
337 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
338 setproctitle("%s [pam]", pam_user);
342 sshpam_conv.conv = sshpam_thread_conv;
343 sshpam_conv.appdata_ptr = ctxt;
345 if (sshpam_authctxt == NULL)
346 fatal("%s: PAM authctxt not initialized", __func__);
348 buffer_init(&buffer);
349 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
350 (const void *)&sshpam_conv);
351 if (sshpam_err != PAM_SUCCESS)
353 sshpam_err = pam_authenticate(sshpam_handle, 0);
354 if (sshpam_err != PAM_SUCCESS)
358 if (!do_pam_account())
360 if (sshpam_authctxt->force_pwchange) {
361 sshpam_err = pam_chauthtok(sshpam_handle,
362 PAM_CHANGE_EXPIRED_AUTHTOK);
363 if (sshpam_err != PAM_SUCCESS)
365 pam_password_change_required(0);
369 buffer_put_cstring(&buffer, "OK");
371 #ifndef USE_POSIX_THREADS
372 /* Export variables set by do_pam_account */
373 buffer_put_int(&buffer, sshpam_account_status);
374 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
376 /* Export any environment strings set in child */
377 for(i = 0; environ[i] != NULL; i++)
379 buffer_put_int(&buffer, i);
380 for(i = 0; environ[i] != NULL; i++)
381 buffer_put_cstring(&buffer, environ[i]);
383 /* Export any environment strings set by PAM in child */
384 env_from_pam = pam_getenvlist(sshpam_handle);
385 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
387 buffer_put_int(&buffer, i);
388 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
389 buffer_put_cstring(&buffer, env_from_pam[i]);
390 #endif /* USE_POSIX_THREADS */
392 /* XXX - can't do much about an error here */
393 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
394 buffer_free(&buffer);
398 buffer_put_cstring(&buffer,
399 pam_strerror(sshpam_handle, sshpam_err));
400 /* XXX - can't do much about an error here */
401 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
402 buffer_free(&buffer);
405 return (NULL); /* Avoid warning for non-pthread case */
409 sshpam_thread_cleanup(void)
411 struct pam_ctxt *ctxt = cleanup_ctxt;
413 debug3("PAM: %s entering", __func__);
414 if (ctxt != NULL && ctxt->pam_thread != 0) {
415 pthread_cancel(ctxt->pam_thread);
416 pthread_join(ctxt->pam_thread, NULL);
417 close(ctxt->pam_psock);
418 close(ctxt->pam_csock);
419 memset(ctxt, 0, sizeof(*ctxt));
425 sshpam_null_conv(int n, const struct pam_message **msg,
426 struct pam_response **resp, void *data)
428 debug3("PAM: %s entering, %d messages", __func__, n);
429 return (PAM_CONV_ERR);
432 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
437 debug("PAM: cleanup");
438 if (sshpam_handle == NULL)
440 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
441 if (sshpam_cred_established) {
442 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
443 sshpam_cred_established = 0;
445 if (sshpam_session_open) {
446 pam_close_session(sshpam_handle, PAM_SILENT);
447 sshpam_session_open = 0;
449 sshpam_authenticated = 0;
450 pam_end(sshpam_handle, sshpam_err);
451 sshpam_handle = NULL;
455 sshpam_init(Authctxt *authctxt)
457 extern char *__progname;
458 const char *pam_rhost, *pam_user, *user = authctxt->user;
460 if (sshpam_handle != NULL) {
461 /* We already have a PAM context; check if the user matches */
462 sshpam_err = pam_get_item(sshpam_handle,
463 PAM_USER, (const void **)&pam_user);
464 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
466 pam_end(sshpam_handle, sshpam_err);
467 sshpam_handle = NULL;
469 debug("PAM: initializing for \"%s\"", user);
471 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
472 sshpam_authctxt = authctxt;
474 if (sshpam_err != PAM_SUCCESS) {
475 pam_end(sshpam_handle, sshpam_err);
476 sshpam_handle = NULL;
479 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
480 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
481 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
482 if (sshpam_err != PAM_SUCCESS) {
483 pam_end(sshpam_handle, sshpam_err);
484 sshpam_handle = NULL;
487 #ifdef PAM_TTY_KLUDGE
489 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
490 * sshd doesn't set the tty until too late in the auth process and
491 * may not even set one (for tty-less connections)
493 debug("PAM: setting PAM_TTY to \"ssh\"");
494 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
495 if (sshpam_err != PAM_SUCCESS) {
496 pam_end(sshpam_handle, sshpam_err);
497 sshpam_handle = NULL;
505 sshpam_init_ctx(Authctxt *authctxt)
507 struct pam_ctxt *ctxt;
510 debug3("PAM: %s entering", __func__);
511 /* Refuse to start if we don't have PAM enabled */
512 if (!options.use_pam)
516 if (sshpam_init(authctxt) == -1) {
517 error("PAM: initialization failed");
521 ctxt = xmalloc(sizeof *ctxt);
522 memset(ctxt, 0, sizeof(*ctxt));
524 /* Start the authentication thread */
525 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
526 error("PAM: failed create sockets: %s", strerror(errno));
530 ctxt->pam_psock = socks[0];
531 ctxt->pam_csock = socks[1];
532 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
533 error("PAM: failed to start authentication thread: %s",
545 sshpam_query(void *ctx, char **name, char **info,
546 u_int *num, char ***prompts, u_int **echo_on)
549 struct pam_ctxt *ctxt = ctx;
555 debug3("PAM: %s entering", __func__);
556 buffer_init(&buffer);
559 *prompts = xmalloc(sizeof(char *));
562 *echo_on = xmalloc(sizeof(u_int));
563 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
564 type = buffer_get_char(&buffer);
565 msg = buffer_get_string(&buffer, NULL);
567 case PAM_PROMPT_ECHO_ON:
568 case PAM_PROMPT_ECHO_OFF:
570 len = plen + strlen(msg) + 1;
571 **prompts = xrealloc(**prompts, len);
572 plen += snprintf(**prompts + plen, len, "%s", msg);
573 **echo_on = (type == PAM_PROMPT_ECHO_ON);
578 /* accumulate messages */
579 len = plen + strlen(msg) + 2;
580 **prompts = xrealloc(**prompts, len);
581 plen += snprintf(**prompts + plen, len, "%s\n", msg);
586 if (**prompts != NULL) {
587 /* drain any accumulated messages */
588 debug("PAM: %s", **prompts);
589 buffer_append(&loginmsg, **prompts,
594 if (type == PAM_SUCCESS) {
595 import_environments(&buffer);
602 error("PAM: %s for %s%.100s from %.100s", msg,
603 sshpam_authctxt->valid ? "" : "illegal user ",
604 sshpam_authctxt->user,
605 get_remote_name_or_ip(utmp_len, options.use_dns));
618 /* XXX - see also comment in auth-chall.c:verify_response */
620 sshpam_respond(void *ctx, u_int num, char **resp)
623 struct pam_ctxt *ctxt = ctx;
625 debug2("PAM: %s entering, %d responses", __func__, num);
626 switch (ctxt->pam_done) {
628 sshpam_authenticated = 1;
636 error("PAM: expected one response, got %u", num);
639 buffer_init(&buffer);
640 buffer_put_cstring(&buffer, *resp);
641 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
642 buffer_free(&buffer);
645 buffer_free(&buffer);
650 sshpam_free_ctx(void *ctxtp)
652 struct pam_ctxt *ctxt = ctxtp;
654 debug3("PAM: %s entering", __func__);
655 sshpam_thread_cleanup();
658 * We don't call sshpam_cleanup() here because we may need the PAM
659 * handle at a later stage, e.g. when setting up a session. It's
660 * still on the cleanup list, so pam_end() *will* be called before
661 * the server process terminates.
665 KbdintDevice sshpam_device = {
673 KbdintDevice mm_sshpam_device = {
682 * This replaces auth-pam.c
685 start_pam(Authctxt *authctxt)
687 if (!options.use_pam)
688 fatal("PAM: initialisation requested when UsePAM=no");
690 if (sshpam_init(authctxt) == -1)
691 fatal("PAM: initialisation failed");
703 if (sshpam_account_status != -1)
704 return (sshpam_account_status);
706 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
707 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
709 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
710 sshpam_account_status = 0;
711 return (sshpam_account_status);
714 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
715 pam_password_change_required(1);
717 sshpam_account_status = 1;
718 return (sshpam_account_status);
722 do_pam_set_tty(const char *tty)
725 debug("PAM: setting PAM_TTY to \"%s\"", tty);
726 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
727 if (sshpam_err != PAM_SUCCESS)
728 fatal("PAM: failed to set PAM_TTY: %s",
729 pam_strerror(sshpam_handle, sshpam_err));
734 do_pam_setcred(int init)
736 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
737 (const void *)&null_conv);
738 if (sshpam_err != PAM_SUCCESS)
739 fatal("PAM: failed to set PAM_CONV: %s",
740 pam_strerror(sshpam_handle, sshpam_err));
742 debug("PAM: establishing credentials");
743 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
745 debug("PAM: reinitializing credentials");
746 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
748 if (sshpam_err == PAM_SUCCESS) {
749 sshpam_cred_established = 1;
752 if (sshpam_authenticated)
753 fatal("PAM: pam_setcred(): %s",
754 pam_strerror(sshpam_handle, sshpam_err));
756 debug("PAM: pam_setcred(): %s",
757 pam_strerror(sshpam_handle, sshpam_err));
761 pam_tty_conv(int n, const struct pam_message **msg,
762 struct pam_response **resp, void *data)
764 char input[PAM_MAX_MSG_SIZE];
765 struct pam_response *reply;
768 debug3("PAM: %s called with %d messages", __func__, n);
772 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
773 return (PAM_CONV_ERR);
775 if ((reply = malloc(n * sizeof(*reply))) == NULL)
776 return (PAM_CONV_ERR);
777 memset(reply, 0, n * sizeof(*reply));
779 for (i = 0; i < n; ++i) {
780 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
781 case PAM_PROMPT_ECHO_OFF:
783 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
785 reply[i].resp_retcode = PAM_SUCCESS;
787 case PAM_PROMPT_ECHO_ON:
788 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
789 fgets(input, sizeof input, stdin);
790 reply[i].resp = xstrdup(input);
791 reply[i].resp_retcode = PAM_SUCCESS;
795 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
796 reply[i].resp_retcode = PAM_SUCCESS;
803 return (PAM_SUCCESS);
806 for(i = 0; i < n; i++) {
807 if (reply[i].resp != NULL)
808 xfree(reply[i].resp);
811 return (PAM_CONV_ERR);
814 static struct pam_conv tty_conv = { pam_tty_conv, NULL };
817 * XXX this should be done in the authentication phase, but ssh1 doesn't
821 do_pam_chauthtok(void)
824 fatal("Password expired (unable to change with privsep)");
825 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
826 (const void *)&tty_conv);
827 if (sshpam_err != PAM_SUCCESS)
828 fatal("PAM: failed to set PAM_CONV: %s",
829 pam_strerror(sshpam_handle, sshpam_err));
830 debug("PAM: changing password");
831 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
832 if (sshpam_err != PAM_SUCCESS)
833 fatal("PAM: pam_chauthtok(): %s",
834 pam_strerror(sshpam_handle, sshpam_err));
838 pam_store_conv(int n, const struct pam_message **msg,
839 struct pam_response **resp, void *data)
841 struct pam_response *reply;
845 debug3("PAM: %s called with %d messages", __func__, n);
848 if (n <= 0 || n > PAM_MAX_NUM_MSG)
849 return (PAM_CONV_ERR);
851 if ((reply = malloc(n * sizeof(*reply))) == NULL)
852 return (PAM_CONV_ERR);
853 memset(reply, 0, n * sizeof(*reply));
855 for (i = 0; i < n; ++i) {
856 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
859 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
860 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
861 buffer_append(&loginmsg, "\n", 1 );
862 reply[i].resp_retcode = PAM_SUCCESS;
869 return (PAM_SUCCESS);
872 for(i = 0; i < n; i++) {
873 if (reply[i].resp != NULL)
874 xfree(reply[i].resp);
877 return (PAM_CONV_ERR);
880 static struct pam_conv store_conv = { pam_store_conv, NULL };
885 debug3("PAM: opening session");
886 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
887 (const void *)&store_conv);
888 if (sshpam_err != PAM_SUCCESS)
889 fatal("PAM: failed to set PAM_CONV: %s",
890 pam_strerror(sshpam_handle, sshpam_err));
891 sshpam_err = pam_open_session(sshpam_handle, 0);
892 if (sshpam_err != PAM_SUCCESS)
893 fatal("PAM: pam_open_session(): %s",
894 pam_strerror(sshpam_handle, sshpam_err));
895 sshpam_session_open = 1;
899 * Set a PAM environment string. We need to do this so that the session
900 * modules can handle things like Kerberos/GSI credentials that appear
901 * during the ssh authentication process.
904 do_pam_putenv(char *name, char *value)
907 #ifdef HAVE_PAM_PUTENV
911 len = strlen(name) + strlen(value) + 2;
912 compound = xmalloc(len);
914 snprintf(compound, len, "%s=%s", name, value);
915 ret = pam_putenv(sshpam_handle, compound);
923 fetch_pam_child_environment(void)
929 fetch_pam_environment(void)
931 return (pam_getenvlist(sshpam_handle));
935 free_pam_environment(char **env)
942 for (envp = env; *envp; envp++)