Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2002 Networks Associates Technology, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 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. |
Damien Miller | e69f18c | 2000-06-12 16:38:54 +1000 | [diff] [blame] | 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 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. |
Damien Miller | e69f18c | 2000-06-12 16:38:54 +1000 | [diff] [blame] | 18 | * |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 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 |
| 29 | * SUCH DAMAGE. |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 30 | */ |
| 31 | |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 32 | /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */ |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 33 | #include "includes.h" |
Darren Tucker | 2a9bf4b | 2004-04-18 11:00:26 +1000 | [diff] [blame] | 34 | RCSID("$Id: auth-pam.c,v 1.100 2004/04/18 01:00:26 dtucker Exp $"); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 35 | |
| 36 | #ifdef USE_PAM |
Damien Miller | 0f47c53 | 2004-01-02 18:01:30 +1100 | [diff] [blame] | 37 | #if defined(HAVE_SECURITY_PAM_APPL_H) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 38 | #include <security/pam_appl.h> |
Damien Miller | 0f47c53 | 2004-01-02 18:01:30 +1100 | [diff] [blame] | 39 | #elif defined (HAVE_PAM_PAM_APPL_H) |
| 40 | #include <pam/pam_appl.h> |
| 41 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 42 | |
Kevin Steves | e683e76 | 2002-04-04 19:02:28 +0000 | [diff] [blame] | 43 | #include "auth.h" |
Damien Miller | 63dc3e9 | 2001-02-07 12:58:33 +1100 | [diff] [blame] | 44 | #include "auth-pam.h" |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 45 | #include "buffer.h" |
| 46 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 47 | #include "canohost.h" |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 48 | #include "log.h" |
| 49 | #include "monitor_wrap.h" |
| 50 | #include "msg.h" |
| 51 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 52 | #include "readpass.h" |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 53 | #include "servconf.h" |
| 54 | #include "ssh2.h" |
| 55 | #include "xmalloc.h" |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 56 | #include "auth-options.h" |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 57 | |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 58 | extern ServerOptions options; |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 59 | extern Buffer loginmsg; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 60 | extern int compat20; |
Darren Tucker | 2a9bf4b | 2004-04-18 11:00:26 +1000 | [diff] [blame] | 61 | extern u_int utmp_len; |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 62 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 63 | #ifdef USE_POSIX_THREADS |
| 64 | #include <pthread.h> |
| 65 | /* |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 66 | * Avoid namespace clash when *not* using pthreads for systems *with* |
| 67 | * pthreads, which unconditionally define pthread_t via sys/types.h |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 68 | * (e.g. Linux) |
| 69 | */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 70 | typedef pthread_t sp_pthread_t; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 71 | #else |
Darren Tucker | 1b27c8f | 2004-01-13 22:35:58 +1100 | [diff] [blame] | 72 | typedef pid_t sp_pthread_t; |
| 73 | #endif |
| 74 | |
| 75 | struct pam_ctxt { |
| 76 | sp_pthread_t pam_thread; |
| 77 | int pam_psock; |
| 78 | int pam_csock; |
| 79 | int pam_done; |
| 80 | }; |
| 81 | |
| 82 | static void sshpam_free_ctx(void *); |
| 83 | static struct pam_ctxt *cleanup_ctxt; |
| 84 | |
| 85 | #ifndef USE_POSIX_THREADS |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 86 | /* |
| 87 | * Simulate threads with processes. |
| 88 | */ |
Kevin Steves | 63007d4 | 2002-07-21 17:57:01 +0000 | [diff] [blame] | 89 | |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 90 | static int sshpam_thread_status = -1; |
| 91 | static mysig_t sshpam_oldsig; |
| 92 | |
| 93 | static void |
| 94 | sshpam_sigchld_handler(int sig) |
| 95 | { |
Darren Tucker | 7ae0962 | 2004-01-14 23:07:56 +1100 | [diff] [blame] | 96 | if (cleanup_ctxt == NULL) |
| 97 | return; /* handler called after PAM cleanup, shouldn't happen */ |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 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"); |
| 107 | } |
| 108 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 109 | static void |
| 110 | pthread_exit(void *value __unused) |
| 111 | { |
| 112 | _exit(0); |
| 113 | } |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 114 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 115 | static int |
| 116 | pthread_create(sp_pthread_t *thread, const void *attr __unused, |
| 117 | void *(*thread_start)(void *), void *arg) |
| 118 | { |
| 119 | pid_t pid; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 120 | |
Darren Tucker | b9b6021 | 2004-03-04 20:03:54 +1100 | [diff] [blame] | 121 | sshpam_thread_status = -1; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 122 | switch ((pid = fork())) { |
| 123 | case -1: |
| 124 | error("fork(): %s", strerror(errno)); |
| 125 | return (-1); |
| 126 | case 0: |
| 127 | thread_start(arg); |
| 128 | _exit(1); |
| 129 | default: |
| 130 | *thread = pid; |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 131 | sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 132 | return (0); |
| 133 | } |
| 134 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 135 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 136 | static int |
| 137 | pthread_cancel(sp_pthread_t thread) |
| 138 | { |
Darren Tucker | 7ae0962 | 2004-01-14 23:07:56 +1100 | [diff] [blame] | 139 | signal(SIGCHLD, sshpam_oldsig); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 140 | return (kill(thread, SIGTERM)); |
| 141 | } |
| 142 | |
| 143 | static int |
| 144 | pthread_join(sp_pthread_t thread, void **value __unused) |
| 145 | { |
| 146 | int status; |
| 147 | |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 148 | if (sshpam_thread_status != -1) |
| 149 | return (sshpam_thread_status); |
| 150 | signal(SIGCHLD, sshpam_oldsig); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 151 | waitpid(thread, &status, 0); |
| 152 | return (status); |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 157 | static pam_handle_t *sshpam_handle = NULL; |
| 158 | static int sshpam_err = 0; |
| 159 | static int sshpam_authenticated = 0; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 160 | static int sshpam_session_open = 0; |
| 161 | static int sshpam_cred_established = 0; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 162 | static int sshpam_account_status = -1; |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 163 | static char **sshpam_env = NULL; |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 164 | static Authctxt *sshpam_authctxt = NULL; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 165 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 166 | /* Some PAM implementations don't implement this */ |
| 167 | #ifndef HAVE_PAM_GETENVLIST |
| 168 | static char ** |
| 169 | pam_getenvlist(pam_handle_t *pamh) |
| 170 | { |
| 171 | /* |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 172 | * XXX - If necessary, we can still support envrionment passing |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 173 | * for platforms without pam_getenvlist by searching for known |
| 174 | * env vars (e.g. KRB5CCNAME) from the PAM environment. |
| 175 | */ |
| 176 | return NULL; |
| 177 | } |
| 178 | #endif |
| 179 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 180 | void |
| 181 | pam_password_change_required(int reqd) |
| 182 | { |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 183 | debug3("%s %d", __func__, reqd); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 184 | if (sshpam_authctxt == NULL) |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 185 | fatal("%s: PAM authctxt not initialized", __func__); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 186 | sshpam_authctxt->force_pwchange = reqd; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 187 | if (reqd) { |
| 188 | no_port_forwarding_flag |= 2; |
| 189 | no_agent_forwarding_flag |= 2; |
| 190 | no_x11_forwarding_flag |= 2; |
| 191 | } else { |
| 192 | no_port_forwarding_flag &= ~2; |
| 193 | no_agent_forwarding_flag &= ~2; |
| 194 | no_x11_forwarding_flag &= ~2; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 195 | } |
| 196 | } |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 197 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 198 | /* Import regular and PAM environment from subprocess */ |
| 199 | static void |
| 200 | import_environments(Buffer *b) |
| 201 | { |
| 202 | char *env; |
| 203 | u_int i, num_env; |
| 204 | int err; |
| 205 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 206 | debug3("PAM: %s entering", __func__); |
| 207 | |
Darren Tucker | 4b385d4 | 2004-03-04 19:54:10 +1100 | [diff] [blame] | 208 | #ifndef USE_POSIX_THREADS |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 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)); |
| 212 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 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); |
| 219 | |
| 220 | sshpam_env[num_env] = NULL; |
| 221 | |
| 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); |
| 227 | |
Darren Tucker | 8a1624c | 2003-11-18 12:45:35 +1100 | [diff] [blame] | 228 | #ifdef HAVE_PAM_PUTENV |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 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)); |
| 233 | } |
Darren Tucker | 8a1624c | 2003-11-18 12:45:35 +1100 | [diff] [blame] | 234 | #endif |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 235 | } |
Darren Tucker | 4b385d4 | 2004-03-04 19:54:10 +1100 | [diff] [blame] | 236 | #endif |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 237 | } |
| 238 | |
Damien Miller | 9d5705a | 2000-09-16 16:09:27 +1100 | [diff] [blame] | 239 | /* |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 240 | * Conversation function for authentication thread. |
Damien Miller | 9d5705a | 2000-09-16 16:09:27 +1100 | [diff] [blame] | 241 | */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 242 | static int |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 243 | sshpam_thread_conv(int n, const struct pam_message **msg, |
| 244 | struct pam_response **resp, void *data) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 245 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 246 | Buffer buffer; |
| 247 | struct pam_ctxt *ctxt; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 248 | struct pam_response *reply; |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 249 | int i; |
| 250 | |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 251 | debug3("PAM: %s entering, %d messages", __func__, n); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 252 | *resp = NULL; |
| 253 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 254 | ctxt = data; |
| 255 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 256 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 257 | |
| 258 | if ((reply = malloc(n * sizeof(*reply))) == NULL) |
| 259 | return (PAM_CONV_ERR); |
| 260 | memset(reply, 0, n * sizeof(*reply)); |
| 261 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 262 | buffer_init(&buffer); |
| 263 | for (i = 0; i < n; ++i) { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 264 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 265 | case PAM_PROMPT_ECHO_OFF: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 266 | buffer_put_cstring(&buffer, |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 267 | PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 268 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 269 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
| 270 | goto fail; |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 271 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 272 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 273 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
| 274 | goto fail; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 275 | reply[i].resp = buffer_get_string(&buffer, NULL); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 276 | break; |
| 277 | case PAM_PROMPT_ECHO_ON: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 278 | buffer_put_cstring(&buffer, |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 279 | PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 280 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 281 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
| 282 | goto fail; |
| 283 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) |
| 284 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 285 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
| 286 | goto fail; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 287 | reply[i].resp = buffer_get_string(&buffer, NULL); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 288 | break; |
| 289 | case PAM_ERROR_MSG: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 290 | buffer_put_cstring(&buffer, |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 291 | PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 292 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 293 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
| 294 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 295 | break; |
| 296 | case PAM_TEXT_INFO: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 297 | buffer_put_cstring(&buffer, |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 298 | PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 299 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 300 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
| 301 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 302 | break; |
| 303 | default: |
| 304 | goto fail; |
| 305 | } |
| 306 | buffer_clear(&buffer); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 307 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 308 | buffer_free(&buffer); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 309 | *resp = reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 310 | return (PAM_SUCCESS); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 311 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 312 | fail: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 313 | for(i = 0; i < n; i++) { |
| 314 | if (reply[i].resp != NULL) |
| 315 | xfree(reply[i].resp); |
| 316 | } |
| 317 | xfree(reply); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 318 | buffer_free(&buffer); |
| 319 | return (PAM_CONV_ERR); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 322 | /* |
| 323 | * Authentication thread. |
| 324 | */ |
| 325 | static void * |
| 326 | sshpam_thread(void *ctxtp) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 327 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 328 | struct pam_ctxt *ctxt = ctxtp; |
| 329 | Buffer buffer; |
Damien Miller | f4b6f10 | 2003-09-02 23:12:06 +1000 | [diff] [blame] | 330 | struct pam_conv sshpam_conv; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 331 | #ifndef USE_POSIX_THREADS |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 332 | extern char **environ; |
| 333 | char **env_from_pam; |
| 334 | u_int i; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 335 | const char *pam_user; |
| 336 | |
| 337 | pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user); |
| 338 | setproctitle("%s [pam]", pam_user); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 339 | environ[0] = NULL; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 340 | #endif |
| 341 | |
Damien Miller | f4b6f10 | 2003-09-02 23:12:06 +1000 | [diff] [blame] | 342 | sshpam_conv.conv = sshpam_thread_conv; |
| 343 | sshpam_conv.appdata_ptr = ctxt; |
| 344 | |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 345 | if (sshpam_authctxt == NULL) |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 346 | fatal("%s: PAM authctxt not initialized", __func__); |
| 347 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 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) |
| 352 | goto auth_fail; |
| 353 | sshpam_err = pam_authenticate(sshpam_handle, 0); |
| 354 | if (sshpam_err != PAM_SUCCESS) |
| 355 | goto auth_fail; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 356 | |
Darren Tucker | c376c86 | 2003-12-18 16:08:59 +1100 | [diff] [blame] | 357 | if (compat20) { |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 358 | if (!do_pam_account()) |
| 359 | goto auth_fail; |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 360 | if (sshpam_authctxt->force_pwchange) { |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 361 | sshpam_err = pam_chauthtok(sshpam_handle, |
| 362 | PAM_CHANGE_EXPIRED_AUTHTOK); |
| 363 | if (sshpam_err != PAM_SUCCESS) |
| 364 | goto auth_fail; |
| 365 | pam_password_change_required(0); |
| 366 | } |
Darren Tucker | c376c86 | 2003-12-18 16:08:59 +1100 | [diff] [blame] | 367 | } |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 368 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 369 | buffer_put_cstring(&buffer, "OK"); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 370 | |
| 371 | #ifndef USE_POSIX_THREADS |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 372 | /* Export variables set by do_pam_account */ |
| 373 | buffer_put_int(&buffer, sshpam_account_status); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 374 | buffer_put_int(&buffer, sshpam_authctxt->force_pwchange); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 375 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 376 | /* Export any environment strings set in child */ |
| 377 | for(i = 0; environ[i] != NULL; i++) |
| 378 | ; /* Count */ |
| 379 | buffer_put_int(&buffer, i); |
| 380 | for(i = 0; environ[i] != NULL; i++) |
| 381 | buffer_put_cstring(&buffer, environ[i]); |
| 382 | |
| 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++) |
| 386 | ; /* Count */ |
| 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 */ |
| 391 | |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 392 | /* XXX - can't do much about an error here */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 393 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); |
| 394 | buffer_free(&buffer); |
| 395 | pthread_exit(NULL); |
| 396 | |
| 397 | auth_fail: |
| 398 | buffer_put_cstring(&buffer, |
| 399 | pam_strerror(sshpam_handle, sshpam_err)); |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 400 | /* XXX - can't do much about an error here */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 401 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); |
| 402 | buffer_free(&buffer); |
| 403 | pthread_exit(NULL); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 404 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 405 | return (NULL); /* Avoid warning for non-pthread case */ |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 406 | } |
| 407 | |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 408 | void |
| 409 | sshpam_thread_cleanup(void) |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 410 | { |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 411 | struct pam_ctxt *ctxt = cleanup_ctxt; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 412 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 413 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 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)); |
| 420 | cleanup_ctxt = NULL; |
| 421 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 422 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 423 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 424 | static int |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 425 | sshpam_null_conv(int n, const struct pam_message **msg, |
| 426 | struct pam_response **resp, void *data) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 427 | { |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 428 | debug3("PAM: %s entering, %d messages", __func__, n); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 429 | return (PAM_CONV_ERR); |
| 430 | } |
Damien Miller | 63dc3e9 | 2001-02-07 12:58:33 +1100 | [diff] [blame] | 431 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 432 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
| 433 | |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 434 | void |
| 435 | sshpam_cleanup(void) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 436 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 437 | debug("PAM: cleanup"); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 438 | if (sshpam_handle == NULL) |
| 439 | return; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 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; |
| 444 | } |
| 445 | if (sshpam_session_open) { |
| 446 | pam_close_session(sshpam_handle, PAM_SILENT); |
| 447 | sshpam_session_open = 0; |
| 448 | } |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 449 | sshpam_authenticated = 0; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 450 | pam_end(sshpam_handle, sshpam_err); |
| 451 | sshpam_handle = NULL; |
| 452 | } |
| 453 | |
| 454 | static int |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 455 | sshpam_init(Authctxt *authctxt) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 456 | { |
Darren Tucker | 455813b | 2003-09-13 22:12:11 +1000 | [diff] [blame] | 457 | extern char *__progname; |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 458 | const char *pam_rhost, *pam_user, *user = authctxt->user; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 459 | |
| 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) |
| 465 | return (0); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 466 | pam_end(sshpam_handle, sshpam_err); |
| 467 | sshpam_handle = NULL; |
| 468 | } |
| 469 | debug("PAM: initializing for \"%s\"", user); |
Darren Tucker | c58c2ee | 2003-09-13 22:02:05 +1000 | [diff] [blame] | 470 | sshpam_err = |
| 471 | pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 472 | sshpam_authctxt = authctxt; |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 473 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 474 | if (sshpam_err != PAM_SUCCESS) { |
| 475 | pam_end(sshpam_handle, sshpam_err); |
| 476 | sshpam_handle = NULL; |
| 477 | return (-1); |
| 478 | } |
Damien Miller | 3a961dc | 2003-06-03 10:25:48 +1000 | [diff] [blame] | 479 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); |
Damien Miller | 4633720 | 2003-06-02 11:04:39 +1000 | [diff] [blame] | 480 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 481 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); |
| 482 | if (sshpam_err != PAM_SUCCESS) { |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 483 | pam_end(sshpam_handle, sshpam_err); |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 484 | sshpam_handle = NULL; |
| 485 | return (-1); |
| 486 | } |
| 487 | #ifdef PAM_TTY_KLUDGE |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 488 | /* |
| 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 |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 491 | * may not even set one (for tty-less connections) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 492 | */ |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 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; |
| 498 | return (-1); |
| 499 | } |
| 500 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 501 | return (0); |
| 502 | } |
| 503 | |
| 504 | static void * |
| 505 | sshpam_init_ctx(Authctxt *authctxt) |
| 506 | { |
| 507 | struct pam_ctxt *ctxt; |
| 508 | int socks[2]; |
| 509 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 510 | debug3("PAM: %s entering", __func__); |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 511 | /* Refuse to start if we don't have PAM enabled */ |
| 512 | if (!options.use_pam) |
| 513 | return NULL; |
| 514 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 515 | /* Initialize PAM */ |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 516 | if (sshpam_init(authctxt) == -1) { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 517 | error("PAM: initialization failed"); |
| 518 | return (NULL); |
| 519 | } |
| 520 | |
| 521 | ctxt = xmalloc(sizeof *ctxt); |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 522 | memset(ctxt, 0, sizeof(*ctxt)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 523 | |
| 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)); |
| 527 | xfree(ctxt); |
| 528 | return (NULL); |
| 529 | } |
| 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", |
| 534 | strerror(errno)); |
| 535 | close(socks[0]); |
| 536 | close(socks[1]); |
| 537 | xfree(ctxt); |
| 538 | return (NULL); |
| 539 | } |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 540 | cleanup_ctxt = ctxt; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 541 | return (ctxt); |
| 542 | } |
| 543 | |
| 544 | static int |
| 545 | sshpam_query(void *ctx, char **name, char **info, |
| 546 | u_int *num, char ***prompts, u_int **echo_on) |
| 547 | { |
| 548 | Buffer buffer; |
| 549 | struct pam_ctxt *ctxt = ctx; |
| 550 | size_t plen; |
| 551 | u_char type; |
| 552 | char *msg; |
Damien Miller | 7f2d795 | 2003-07-30 14:53:11 +1000 | [diff] [blame] | 553 | size_t len; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 554 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 555 | debug3("PAM: %s entering", __func__); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 556 | buffer_init(&buffer); |
| 557 | *name = xstrdup(""); |
| 558 | *info = xstrdup(""); |
| 559 | *prompts = xmalloc(sizeof(char *)); |
| 560 | **prompts = NULL; |
| 561 | plen = 0; |
| 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); |
| 566 | switch (type) { |
| 567 | case PAM_PROMPT_ECHO_ON: |
| 568 | case PAM_PROMPT_ECHO_OFF: |
| 569 | *num = 1; |
Damien Miller | 7f2d795 | 2003-07-30 14:53:11 +1000 | [diff] [blame] | 570 | len = plen + strlen(msg) + 1; |
| 571 | **prompts = xrealloc(**prompts, len); |
| 572 | plen += snprintf(**prompts + plen, len, "%s", msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 573 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
| 574 | xfree(msg); |
| 575 | return (0); |
| 576 | case PAM_ERROR_MSG: |
| 577 | case PAM_TEXT_INFO: |
| 578 | /* accumulate messages */ |
Darren Tucker | ae52b7c | 2003-11-13 19:52:31 +1100 | [diff] [blame] | 579 | len = plen + strlen(msg) + 2; |
Damien Miller | 7f2d795 | 2003-07-30 14:53:11 +1000 | [diff] [blame] | 580 | **prompts = xrealloc(**prompts, len); |
Darren Tucker | ae52b7c | 2003-11-13 19:52:31 +1100 | [diff] [blame] | 581 | plen += snprintf(**prompts + plen, len, "%s\n", msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 582 | xfree(msg); |
| 583 | break; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 584 | case PAM_SUCCESS: |
| 585 | case PAM_AUTH_ERR: |
| 586 | if (**prompts != NULL) { |
| 587 | /* drain any accumulated messages */ |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 588 | debug("PAM: %s", **prompts); |
| 589 | buffer_append(&loginmsg, **prompts, |
| 590 | strlen(**prompts)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 591 | xfree(**prompts); |
| 592 | **prompts = NULL; |
| 593 | } |
| 594 | if (type == PAM_SUCCESS) { |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 595 | import_environments(&buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 596 | *num = 0; |
| 597 | **echo_on = 0; |
| 598 | ctxt->pam_done = 1; |
| 599 | xfree(msg); |
| 600 | return (0); |
| 601 | } |
Darren Tucker | 2a9bf4b | 2004-04-18 11:00:26 +1000 | [diff] [blame] | 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)); |
Darren Tucker | 439ce0d | 2003-10-09 14:20:15 +1000 | [diff] [blame] | 606 | /* FALLTHROUGH */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 607 | default: |
| 608 | *num = 0; |
| 609 | **echo_on = 0; |
| 610 | xfree(msg); |
| 611 | ctxt->pam_done = -1; |
| 612 | return (-1); |
| 613 | } |
| 614 | } |
| 615 | return (-1); |
| 616 | } |
| 617 | |
| 618 | /* XXX - see also comment in auth-chall.c:verify_response */ |
| 619 | static int |
| 620 | sshpam_respond(void *ctx, u_int num, char **resp) |
| 621 | { |
| 622 | Buffer buffer; |
| 623 | struct pam_ctxt *ctxt = ctx; |
| 624 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 625 | debug2("PAM: %s entering, %d responses", __func__, num); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 626 | switch (ctxt->pam_done) { |
| 627 | case 1: |
| 628 | sshpam_authenticated = 1; |
| 629 | return (0); |
| 630 | case 0: |
| 631 | break; |
| 632 | default: |
| 633 | return (-1); |
| 634 | } |
| 635 | if (num != 1) { |
| 636 | error("PAM: expected one response, got %u", num); |
| 637 | return (-1); |
| 638 | } |
| 639 | buffer_init(&buffer); |
| 640 | buffer_put_cstring(&buffer, *resp); |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 641 | if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) { |
| 642 | buffer_free(&buffer); |
| 643 | return (-1); |
| 644 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 645 | buffer_free(&buffer); |
| 646 | return (1); |
| 647 | } |
| 648 | |
| 649 | static void |
| 650 | sshpam_free_ctx(void *ctxtp) |
| 651 | { |
| 652 | struct pam_ctxt *ctxt = ctxtp; |
| 653 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 654 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 655 | sshpam_thread_cleanup(); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 656 | xfree(ctxt); |
| 657 | /* |
| 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. |
| 662 | */ |
| 663 | } |
| 664 | |
| 665 | KbdintDevice sshpam_device = { |
| 666 | "pam", |
| 667 | sshpam_init_ctx, |
| 668 | sshpam_query, |
| 669 | sshpam_respond, |
| 670 | sshpam_free_ctx |
| 671 | }; |
| 672 | |
| 673 | KbdintDevice mm_sshpam_device = { |
| 674 | "pam", |
| 675 | mm_sshpam_init_ctx, |
| 676 | mm_sshpam_query, |
| 677 | mm_sshpam_respond, |
| 678 | mm_sshpam_free_ctx |
| 679 | }; |
| 680 | |
| 681 | /* |
| 682 | * This replaces auth-pam.c |
| 683 | */ |
| 684 | void |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 685 | start_pam(Authctxt *authctxt) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 686 | { |
Damien Miller | 9d507da | 2003-05-14 15:31:12 +1000 | [diff] [blame] | 687 | if (!options.use_pam) |
| 688 | fatal("PAM: initialisation requested when UsePAM=no"); |
| 689 | |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 690 | if (sshpam_init(authctxt) == -1) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 691 | fatal("PAM: initialisation failed"); |
| 692 | } |
| 693 | |
| 694 | void |
| 695 | finish_pam(void) |
| 696 | { |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 697 | sshpam_cleanup(); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 698 | } |
| 699 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 700 | u_int |
| 701 | do_pam_account(void) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 702 | { |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 703 | if (sshpam_account_status != -1) |
| 704 | return (sshpam_account_status); |
| 705 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 706 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 707 | debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 708 | |
| 709 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { |
| 710 | sshpam_account_status = 0; |
| 711 | return (sshpam_account_status); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 712 | } |
| 713 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 714 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) |
| 715 | pam_password_change_required(1); |
| 716 | |
| 717 | sshpam_account_status = 1; |
| 718 | return (sshpam_account_status); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void |
Damien Miller | 341c6e6 | 2003-09-02 23:18:52 +1000 | [diff] [blame] | 722 | do_pam_set_tty(const char *tty) |
| 723 | { |
Darren Tucker | f38db7f | 2003-08-08 13:43:37 +1000 | [diff] [blame] | 724 | if (tty != NULL) { |
| 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)); |
| 730 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | void |
| 734 | do_pam_setcred(int init) |
| 735 | { |
| 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)); |
| 741 | if (init) { |
| 742 | debug("PAM: establishing credentials"); |
| 743 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); |
| 744 | } else { |
| 745 | debug("PAM: reinitializing credentials"); |
| 746 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); |
| 747 | } |
| 748 | if (sshpam_err == PAM_SUCCESS) { |
| 749 | sshpam_cred_established = 1; |
| 750 | return; |
| 751 | } |
| 752 | if (sshpam_authenticated) |
| 753 | fatal("PAM: pam_setcred(): %s", |
| 754 | pam_strerror(sshpam_handle, sshpam_err)); |
| 755 | else |
| 756 | debug("PAM: pam_setcred(): %s", |
| 757 | pam_strerror(sshpam_handle, sshpam_err)); |
| 758 | } |
| 759 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 760 | static int |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 761 | pam_tty_conv(int n, const struct pam_message **msg, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 762 | struct pam_response **resp, void *data) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 763 | { |
| 764 | char input[PAM_MAX_MSG_SIZE]; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 765 | struct pam_response *reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 766 | int i; |
| 767 | |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 768 | debug3("PAM: %s called with %d messages", __func__, n); |
| 769 | |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 770 | *resp = NULL; |
| 771 | |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 772 | if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 773 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 774 | |
| 775 | if ((reply = malloc(n * sizeof(*reply))) == NULL) |
| 776 | return (PAM_CONV_ERR); |
| 777 | memset(reply, 0, n * sizeof(*reply)); |
| 778 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 779 | for (i = 0; i < n; ++i) { |
| 780 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 781 | case PAM_PROMPT_ECHO_OFF: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 782 | reply[i].resp = |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 783 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 784 | RP_ALLOW_STDIN); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 785 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 786 | break; |
| 787 | case PAM_PROMPT_ECHO_ON: |
Darren Tucker | 0947ddf | 2003-11-13 11:21:31 +1100 | [diff] [blame] | 788 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 789 | fgets(input, sizeof input, stdin); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 790 | reply[i].resp = xstrdup(input); |
| 791 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 792 | break; |
| 793 | case PAM_ERROR_MSG: |
| 794 | case PAM_TEXT_INFO: |
Darren Tucker | 0947ddf | 2003-11-13 11:21:31 +1100 | [diff] [blame] | 795 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 796 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 797 | break; |
| 798 | default: |
| 799 | goto fail; |
| 800 | } |
| 801 | } |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 802 | *resp = reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 803 | return (PAM_SUCCESS); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 804 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 805 | fail: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 806 | for(i = 0; i < n; i++) { |
| 807 | if (reply[i].resp != NULL) |
| 808 | xfree(reply[i].resp); |
| 809 | } |
| 810 | xfree(reply); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 811 | return (PAM_CONV_ERR); |
| 812 | } |
| 813 | |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 814 | static struct pam_conv tty_conv = { pam_tty_conv, NULL }; |
| 815 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 816 | /* |
| 817 | * XXX this should be done in the authentication phase, but ssh1 doesn't |
| 818 | * support that |
| 819 | */ |
| 820 | void |
| 821 | do_pam_chauthtok(void) |
| 822 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 823 | if (use_privsep) |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 824 | fatal("Password expired (unable to change with privsep)"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 825 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 826 | (const void *)&tty_conv); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 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)); |
| 835 | } |
| 836 | |
Darren Tucker | 5cf8ef7 | 2004-02-17 23:20:07 +1100 | [diff] [blame] | 837 | static int |
| 838 | pam_store_conv(int n, const struct pam_message **msg, |
| 839 | struct pam_response **resp, void *data) |
| 840 | { |
| 841 | struct pam_response *reply; |
| 842 | int i; |
| 843 | size_t len; |
| 844 | |
| 845 | debug3("PAM: %s called with %d messages", __func__, n); |
| 846 | *resp = NULL; |
| 847 | |
| 848 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 849 | return (PAM_CONV_ERR); |
| 850 | |
| 851 | if ((reply = malloc(n * sizeof(*reply))) == NULL) |
| 852 | return (PAM_CONV_ERR); |
| 853 | memset(reply, 0, n * sizeof(*reply)); |
| 854 | |
| 855 | for (i = 0; i < n; ++i) { |
| 856 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 857 | case PAM_ERROR_MSG: |
| 858 | case PAM_TEXT_INFO: |
| 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; |
| 863 | break; |
| 864 | default: |
| 865 | goto fail; |
| 866 | } |
| 867 | } |
| 868 | *resp = reply; |
| 869 | return (PAM_SUCCESS); |
| 870 | |
| 871 | fail: |
| 872 | for(i = 0; i < n; i++) { |
| 873 | if (reply[i].resp != NULL) |
| 874 | xfree(reply[i].resp); |
| 875 | } |
| 876 | xfree(reply); |
| 877 | return (PAM_CONV_ERR); |
| 878 | } |
| 879 | |
| 880 | static struct pam_conv store_conv = { pam_store_conv, NULL }; |
| 881 | |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 882 | void |
| 883 | do_pam_session(void) |
| 884 | { |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 885 | debug3("PAM: opening session"); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 886 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 5cf8ef7 | 2004-02-17 23:20:07 +1100 | [diff] [blame] | 887 | (const void *)&store_conv); |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 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; |
| 896 | } |
| 897 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 898 | /* |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 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. |
| 902 | */ |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 903 | int |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 904 | do_pam_putenv(char *name, char *value) |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 905 | { |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 906 | int ret = 1; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 907 | #ifdef HAVE_PAM_PUTENV |
Damien Miller | f272809 | 2003-09-17 07:24:25 +1000 | [diff] [blame] | 908 | char *compound; |
| 909 | size_t len; |
| 910 | |
| 911 | len = strlen(name) + strlen(value) + 2; |
| 912 | compound = xmalloc(len); |
| 913 | |
| 914 | snprintf(compound, len, "%s=%s", name, value); |
| 915 | ret = pam_putenv(sshpam_handle, compound); |
| 916 | xfree(compound); |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 917 | #endif |
Damien Miller | f272809 | 2003-09-17 07:24:25 +1000 | [diff] [blame] | 918 | |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 919 | return (ret); |
| 920 | } |
| 921 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 922 | char ** |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 923 | fetch_pam_child_environment(void) |
| 924 | { |
| 925 | return sshpam_env; |
| 926 | } |
| 927 | |
| 928 | char ** |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 929 | fetch_pam_environment(void) |
| 930 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 931 | return (pam_getenvlist(sshpam_handle)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | void |
| 935 | free_pam_environment(char **env) |
| 936 | { |
| 937 | char **envp; |
| 938 | |
Damien Miller | e27c6cc | 2003-05-16 18:21:01 +1000 | [diff] [blame] | 939 | if (env == NULL) |
| 940 | return; |
| 941 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 942 | for (envp = env; *envp; envp++) |
| 943 | xfree(*envp); |
| 944 | xfree(env); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | #endif /* USE_PAM */ |