]>
Commit | Line | Data |
---|---|---|
05114c74 | 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. | |
09564242 | 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. | |
09564242 | 18 | * |
05114c74 | 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. | |
a5c9cd31 | 30 | */ |
93c5ef94 | 31 | /* |
32 | * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org> | |
33 | * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au> | |
34 | * | |
35 | * Permission to use, copy, modify, and distribute this software for any | |
36 | * purpose with or without fee is hereby granted, provided that the above | |
37 | * copyright notice and this permission notice appear in all copies. | |
38 | * | |
39 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
40 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
41 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
42 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
43 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
44 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
45 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
46 | */ | |
a5c9cd31 | 47 | |
5ff453c0 | 48 | /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */ |
a5c9cd31 | 49 | #include "includes.h" |
5ff453c0 | 50 | RCSID("$Id$"); |
a5c9cd31 | 51 | |
52 | #ifdef USE_PAM | |
2511d104 | 53 | #if defined(HAVE_SECURITY_PAM_APPL_H) |
05114c74 | 54 | #include <security/pam_appl.h> |
2511d104 | 55 | #elif defined (HAVE_PAM_PAM_APPL_H) |
56 | #include <pam/pam_appl.h> | |
57 | #endif | |
05114c74 | 58 | |
fde58bd4 | 59 | #include "auth.h" |
5c377b3b | 60 | #include "auth-pam.h" |
05114c74 | 61 | #include "buffer.h" |
62 | #include "bufaux.h" | |
42f11eb2 | 63 | #include "canohost.h" |
05114c74 | 64 | #include "log.h" |
65 | #include "monitor_wrap.h" | |
66 | #include "msg.h" | |
67 | #include "packet.h" | |
0f3ee929 | 68 | #include "misc.h" |
05114c74 | 69 | #include "servconf.h" |
70 | #include "ssh2.h" | |
71 | #include "xmalloc.h" | |
5b9e2464 | 72 | #include "auth-options.h" |
a5c9cd31 | 73 | |
7fceb20d | 74 | extern ServerOptions options; |
3eaf3960 | 75 | extern Buffer loginmsg; |
b3ef7fb7 | 76 | extern int compat20; |
1e08e787 | 77 | extern u_int utmp_len; |
7fceb20d | 78 | |
05114c74 | 79 | #ifdef USE_POSIX_THREADS |
80 | #include <pthread.h> | |
81 | /* | |
aff51935 | 82 | * Avoid namespace clash when *not* using pthreads for systems *with* |
83 | * pthreads, which unconditionally define pthread_t via sys/types.h | |
05114c74 | 84 | * (e.g. Linux) |
85 | */ | |
aff51935 | 86 | typedef pthread_t sp_pthread_t; |
05114c74 | 87 | #else |
e47e681f | 88 | typedef pid_t sp_pthread_t; |
89 | #endif | |
90 | ||
91 | struct pam_ctxt { | |
92 | sp_pthread_t pam_thread; | |
93 | int pam_psock; | |
94 | int pam_csock; | |
95 | int pam_done; | |
96 | }; | |
97 | ||
98 | static void sshpam_free_ctx(void *); | |
99 | static struct pam_ctxt *cleanup_ctxt; | |
100 | ||
101 | #ifndef USE_POSIX_THREADS | |
05114c74 | 102 | /* |
103 | * Simulate threads with processes. | |
104 | */ | |
a5c9cd31 | 105 | |
100e6910 | 106 | static int sshpam_thread_status = -1; |
107 | static mysig_t sshpam_oldsig; | |
108 | ||
109 | static void | |
110 | sshpam_sigchld_handler(int sig) | |
111 | { | |
8a956cda | 112 | signal(SIGCHLD, SIG_DFL); |
28b49ff8 | 113 | if (cleanup_ctxt == NULL) |
114 | return; /* handler called after PAM cleanup, shouldn't happen */ | |
8a956cda | 115 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG) |
116 | == -1) { | |
117 | /* PAM thread has not exitted, privsep slave must have */ | |
118 | kill(cleanup_ctxt->pam_thread, SIGTERM); | |
119 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0) | |
120 | == -1) | |
121 | return; /* could not wait */ | |
122 | } | |
100e6910 | 123 | if (WIFSIGNALED(sshpam_thread_status) && |
124 | WTERMSIG(sshpam_thread_status) == SIGTERM) | |
125 | return; /* terminated by pthread_cancel */ | |
126 | if (!WIFEXITED(sshpam_thread_status)) | |
127 | fatal("PAM: authentication thread exited unexpectedly"); | |
128 | if (WEXITSTATUS(sshpam_thread_status) != 0) | |
129 | fatal("PAM: authentication thread exited uncleanly"); | |
130 | } | |
131 | ||
05114c74 | 132 | static void |
133 | pthread_exit(void *value __unused) | |
134 | { | |
135 | _exit(0); | |
136 | } | |
5daf7064 | 137 | |
05114c74 | 138 | static int |
139 | pthread_create(sp_pthread_t *thread, const void *attr __unused, | |
140 | void *(*thread_start)(void *), void *arg) | |
141 | { | |
142 | pid_t pid; | |
143 | ||
355fbf31 | 144 | sshpam_thread_status = -1; |
05114c74 | 145 | switch ((pid = fork())) { |
146 | case -1: | |
147 | error("fork(): %s", strerror(errno)); | |
148 | return (-1); | |
149 | case 0: | |
150 | thread_start(arg); | |
151 | _exit(1); | |
152 | default: | |
153 | *thread = pid; | |
100e6910 | 154 | sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler); |
05114c74 | 155 | return (0); |
156 | } | |
157 | } | |
a5c9cd31 | 158 | |
05114c74 | 159 | static int |
160 | pthread_cancel(sp_pthread_t thread) | |
8c9fe09e | 161 | { |
28b49ff8 | 162 | signal(SIGCHLD, sshpam_oldsig); |
05114c74 | 163 | return (kill(thread, SIGTERM)); |
8c9fe09e | 164 | } |
165 | ||
05114c74 | 166 | static int |
167 | pthread_join(sp_pthread_t thread, void **value __unused) | |
8c9fe09e | 168 | { |
05114c74 | 169 | int status; |
170 | ||
100e6910 | 171 | if (sshpam_thread_status != -1) |
172 | return (sshpam_thread_status); | |
173 | signal(SIGCHLD, sshpam_oldsig); | |
05114c74 | 174 | waitpid(thread, &status, 0); |
175 | return (status); | |
8c9fe09e | 176 | } |
05114c74 | 177 | #endif |
178 | ||
179 | ||
0a23d79f | 180 | static pam_handle_t *sshpam_handle = NULL; |
181 | static int sshpam_err = 0; | |
182 | static int sshpam_authenticated = 0; | |
0a23d79f | 183 | static int sshpam_session_open = 0; |
184 | static int sshpam_cred_established = 0; | |
b3ef7fb7 | 185 | static int sshpam_account_status = -1; |
2212fc98 | 186 | static char **sshpam_env = NULL; |
d948154a | 187 | static Authctxt *sshpam_authctxt = NULL; |
5de92f17 | 188 | static const char *sshpam_password = NULL; |
05114c74 | 189 | |
2212fc98 | 190 | /* Some PAM implementations don't implement this */ |
191 | #ifndef HAVE_PAM_GETENVLIST | |
192 | static char ** | |
193 | pam_getenvlist(pam_handle_t *pamh) | |
194 | { | |
195 | /* | |
aff51935 | 196 | * XXX - If necessary, we can still support envrionment passing |
2212fc98 | 197 | * for platforms without pam_getenvlist by searching for known |
198 | * env vars (e.g. KRB5CCNAME) from the PAM environment. | |
199 | */ | |
200 | return NULL; | |
201 | } | |
202 | #endif | |
203 | ||
b3ef7fb7 | 204 | void |
c10bb2ce | 205 | sshpam_password_change_required(int reqd) |
b3ef7fb7 | 206 | { |
90f3c272 | 207 | debug3("%s %d", __func__, reqd); |
d948154a | 208 | if (sshpam_authctxt == NULL) |
529d73ab | 209 | fatal("%s: PAM authctxt not initialized", __func__); |
d948154a | 210 | sshpam_authctxt->force_pwchange = reqd; |
b3ef7fb7 | 211 | if (reqd) { |
212 | no_port_forwarding_flag |= 2; | |
213 | no_agent_forwarding_flag |= 2; | |
214 | no_x11_forwarding_flag |= 2; | |
215 | } else { | |
216 | no_port_forwarding_flag &= ~2; | |
217 | no_agent_forwarding_flag &= ~2; | |
218 | no_x11_forwarding_flag &= ~2; | |
b3ef7fb7 | 219 | } |
220 | } | |
dd1fb864 | 221 | |
2212fc98 | 222 | /* Import regular and PAM environment from subprocess */ |
223 | static void | |
224 | import_environments(Buffer *b) | |
225 | { | |
226 | char *env; | |
227 | u_int i, num_env; | |
228 | int err; | |
229 | ||
90f3c272 | 230 | debug3("PAM: %s entering", __func__); |
231 | ||
a1e0095d | 232 | #ifndef USE_POSIX_THREADS |
b3ef7fb7 | 233 | /* Import variables set by do_pam_account */ |
234 | sshpam_account_status = buffer_get_int(b); | |
c10bb2ce | 235 | sshpam_password_change_required(buffer_get_int(b)); |
b3ef7fb7 | 236 | |
2212fc98 | 237 | /* Import environment from subprocess */ |
238 | num_env = buffer_get_int(b); | |
239 | sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env)); | |
240 | debug3("PAM: num env strings %d", num_env); | |
241 | for(i = 0; i < num_env; i++) | |
242 | sshpam_env[i] = buffer_get_string(b, NULL); | |
243 | ||
244 | sshpam_env[num_env] = NULL; | |
245 | ||
246 | /* Import PAM environment from subprocess */ | |
247 | num_env = buffer_get_int(b); | |
248 | debug("PAM: num PAM env strings %d", num_env); | |
249 | for(i = 0; i < num_env; i++) { | |
250 | env = buffer_get_string(b, NULL); | |
251 | ||
f79a6165 | 252 | #ifdef HAVE_PAM_PUTENV |
2212fc98 | 253 | /* Errors are not fatal here */ |
254 | if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) { | |
255 | error("PAM: pam_putenv: %s", | |
256 | pam_strerror(sshpam_handle, sshpam_err)); | |
257 | } | |
f79a6165 | 258 | #endif |
2212fc98 | 259 | } |
a1e0095d | 260 | #endif |
2212fc98 | 261 | } |
262 | ||
ad55cd03 | 263 | /* |
05114c74 | 264 | * Conversation function for authentication thread. |
ad55cd03 | 265 | */ |
05114c74 | 266 | static int |
5b9e2464 | 267 | sshpam_thread_conv(int n, const struct pam_message **msg, |
268 | struct pam_response **resp, void *data) | |
a5c9cd31 | 269 | { |
05114c74 | 270 | Buffer buffer; |
271 | struct pam_ctxt *ctxt; | |
0a23d79f | 272 | struct pam_response *reply; |
05114c74 | 273 | int i; |
274 | ||
60922169 | 275 | debug3("PAM: %s entering, %d messages", __func__, n); |
0a23d79f | 276 | *resp = NULL; |
277 | ||
d506e25f | 278 | if (data == NULL) { |
279 | error("PAM: conversation function passed a null context"); | |
280 | return (PAM_CONV_ERR); | |
281 | } | |
05114c74 | 282 | ctxt = data; |
283 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
284 | return (PAM_CONV_ERR); | |
0a23d79f | 285 | |
286 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
287 | return (PAM_CONV_ERR); | |
288 | memset(reply, 0, n * sizeof(*reply)); | |
289 | ||
05114c74 | 290 | buffer_init(&buffer); |
291 | for (i = 0; i < n; ++i) { | |
05114c74 | 292 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
293 | case PAM_PROMPT_ECHO_OFF: | |
aff51935 | 294 | buffer_put_cstring(&buffer, |
0a23d79f | 295 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 296 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 297 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
298 | goto fail; | |
aff51935 | 299 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) |
7b2a0de3 | 300 | goto fail; |
05114c74 | 301 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
302 | goto fail; | |
0a23d79f | 303 | reply[i].resp = buffer_get_string(&buffer, NULL); |
05114c74 | 304 | break; |
305 | case PAM_PROMPT_ECHO_ON: | |
aff51935 | 306 | buffer_put_cstring(&buffer, |
0a23d79f | 307 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 308 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 309 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
310 | goto fail; | |
311 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) | |
312 | goto fail; | |
05114c74 | 313 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
314 | goto fail; | |
0a23d79f | 315 | reply[i].resp = buffer_get_string(&buffer, NULL); |
05114c74 | 316 | break; |
317 | case PAM_ERROR_MSG: | |
aff51935 | 318 | buffer_put_cstring(&buffer, |
0a23d79f | 319 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 320 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 321 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
322 | goto fail; | |
05114c74 | 323 | break; |
324 | case PAM_TEXT_INFO: | |
aff51935 | 325 | buffer_put_cstring(&buffer, |
0a23d79f | 326 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 327 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 328 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
329 | goto fail; | |
05114c74 | 330 | break; |
331 | default: | |
332 | goto fail; | |
a5c9cd31 | 333 | } |
05114c74 | 334 | buffer_clear(&buffer); |
a5c9cd31 | 335 | } |
05114c74 | 336 | buffer_free(&buffer); |
0a23d79f | 337 | *resp = reply; |
05114c74 | 338 | return (PAM_SUCCESS); |
0a23d79f | 339 | |
05114c74 | 340 | fail: |
0a23d79f | 341 | for(i = 0; i < n; i++) { |
342 | if (reply[i].resp != NULL) | |
343 | xfree(reply[i].resp); | |
344 | } | |
345 | xfree(reply); | |
05114c74 | 346 | buffer_free(&buffer); |
347 | return (PAM_CONV_ERR); | |
348 | } | |
a5c9cd31 | 349 | |
05114c74 | 350 | /* |
351 | * Authentication thread. | |
352 | */ | |
353 | static void * | |
354 | sshpam_thread(void *ctxtp) | |
355 | { | |
356 | struct pam_ctxt *ctxt = ctxtp; | |
357 | Buffer buffer; | |
c53917a9 | 358 | struct pam_conv sshpam_conv; |
05114c74 | 359 | #ifndef USE_POSIX_THREADS |
2212fc98 | 360 | extern char **environ; |
361 | char **env_from_pam; | |
362 | u_int i; | |
05114c74 | 363 | const char *pam_user; |
364 | ||
365 | pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user); | |
366 | setproctitle("%s [pam]", pam_user); | |
2212fc98 | 367 | environ[0] = NULL; |
05114c74 | 368 | #endif |
a5c9cd31 | 369 | |
c53917a9 | 370 | sshpam_conv.conv = sshpam_thread_conv; |
371 | sshpam_conv.appdata_ptr = ctxt; | |
372 | ||
d948154a | 373 | if (sshpam_authctxt == NULL) |
529d73ab | 374 | fatal("%s: PAM authctxt not initialized", __func__); |
375 | ||
05114c74 | 376 | buffer_init(&buffer); |
377 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
378 | (const void *)&sshpam_conv); | |
379 | if (sshpam_err != PAM_SUCCESS) | |
380 | goto auth_fail; | |
381 | sshpam_err = pam_authenticate(sshpam_handle, 0); | |
382 | if (sshpam_err != PAM_SUCCESS) | |
383 | goto auth_fail; | |
b3ef7fb7 | 384 | |
2935db92 | 385 | if (compat20) { |
b3ef7fb7 | 386 | if (!do_pam_account()) |
387 | goto auth_fail; | |
d948154a | 388 | if (sshpam_authctxt->force_pwchange) { |
b3ef7fb7 | 389 | sshpam_err = pam_chauthtok(sshpam_handle, |
390 | PAM_CHANGE_EXPIRED_AUTHTOK); | |
391 | if (sshpam_err != PAM_SUCCESS) | |
392 | goto auth_fail; | |
c10bb2ce | 393 | sshpam_password_change_required(0); |
b3ef7fb7 | 394 | } |
2935db92 | 395 | } |
b3ef7fb7 | 396 | |
05114c74 | 397 | buffer_put_cstring(&buffer, "OK"); |
2212fc98 | 398 | |
399 | #ifndef USE_POSIX_THREADS | |
b3ef7fb7 | 400 | /* Export variables set by do_pam_account */ |
401 | buffer_put_int(&buffer, sshpam_account_status); | |
d948154a | 402 | buffer_put_int(&buffer, sshpam_authctxt->force_pwchange); |
b3ef7fb7 | 403 | |
2212fc98 | 404 | /* Export any environment strings set in child */ |
405 | for(i = 0; environ[i] != NULL; i++) | |
406 | ; /* Count */ | |
407 | buffer_put_int(&buffer, i); | |
408 | for(i = 0; environ[i] != NULL; i++) | |
409 | buffer_put_cstring(&buffer, environ[i]); | |
410 | ||
411 | /* Export any environment strings set by PAM in child */ | |
412 | env_from_pam = pam_getenvlist(sshpam_handle); | |
413 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
414 | ; /* Count */ | |
415 | buffer_put_int(&buffer, i); | |
416 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
417 | buffer_put_cstring(&buffer, env_from_pam[i]); | |
418 | #endif /* USE_POSIX_THREADS */ | |
419 | ||
7b2a0de3 | 420 | /* XXX - can't do much about an error here */ |
05114c74 | 421 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); |
422 | buffer_free(&buffer); | |
423 | pthread_exit(NULL); | |
424 | ||
425 | auth_fail: | |
426 | buffer_put_cstring(&buffer, | |
427 | pam_strerror(sshpam_handle, sshpam_err)); | |
7b2a0de3 | 428 | /* XXX - can't do much about an error here */ |
05114c74 | 429 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); |
430 | buffer_free(&buffer); | |
431 | pthread_exit(NULL); | |
b6453d99 | 432 | |
05114c74 | 433 | return (NULL); /* Avoid warning for non-pthread case */ |
a5c9cd31 | 434 | } |
435 | ||
c6630044 | 436 | void |
437 | sshpam_thread_cleanup(void) | |
a5c9cd31 | 438 | { |
c6630044 | 439 | struct pam_ctxt *ctxt = cleanup_ctxt; |
440 | ||
90f3c272 | 441 | debug3("PAM: %s entering", __func__); |
c6630044 | 442 | if (ctxt != NULL && ctxt->pam_thread != 0) { |
443 | pthread_cancel(ctxt->pam_thread); | |
444 | pthread_join(ctxt->pam_thread, NULL); | |
445 | close(ctxt->pam_psock); | |
446 | close(ctxt->pam_csock); | |
447 | memset(ctxt, 0, sizeof(*ctxt)); | |
448 | cleanup_ctxt = NULL; | |
449 | } | |
05114c74 | 450 | } |
a5c9cd31 | 451 | |
05114c74 | 452 | static int |
5b9e2464 | 453 | sshpam_null_conv(int n, const struct pam_message **msg, |
454 | struct pam_response **resp, void *data) | |
05114c74 | 455 | { |
60922169 | 456 | debug3("PAM: %s entering, %d messages", __func__, n); |
05114c74 | 457 | return (PAM_CONV_ERR); |
458 | } | |
a5c9cd31 | 459 | |
05114c74 | 460 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
461 | ||
c6630044 | 462 | void |
463 | sshpam_cleanup(void) | |
05114c74 | 464 | { |
05114c74 | 465 | debug("PAM: cleanup"); |
0a23d79f | 466 | if (sshpam_handle == NULL) |
467 | return; | |
05114c74 | 468 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); |
469 | if (sshpam_cred_established) { | |
470 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); | |
471 | sshpam_cred_established = 0; | |
472 | } | |
473 | if (sshpam_session_open) { | |
474 | pam_close_session(sshpam_handle, PAM_SILENT); | |
475 | sshpam_session_open = 0; | |
a5c9cd31 | 476 | } |
dd1fb864 | 477 | sshpam_authenticated = 0; |
05114c74 | 478 | pam_end(sshpam_handle, sshpam_err); |
479 | sshpam_handle = NULL; | |
a5c9cd31 | 480 | } |
481 | ||
05114c74 | 482 | static int |
529d73ab | 483 | sshpam_init(Authctxt *authctxt) |
a5c9cd31 | 484 | { |
74678fb9 | 485 | extern char *__progname; |
529d73ab | 486 | const char *pam_rhost, *pam_user, *user = authctxt->user; |
05114c74 | 487 | |
488 | if (sshpam_handle != NULL) { | |
489 | /* We already have a PAM context; check if the user matches */ | |
490 | sshpam_err = pam_get_item(sshpam_handle, | |
491 | PAM_USER, (const void **)&pam_user); | |
492 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) | |
493 | return (0); | |
05114c74 | 494 | pam_end(sshpam_handle, sshpam_err); |
495 | sshpam_handle = NULL; | |
496 | } | |
497 | debug("PAM: initializing for \"%s\"", user); | |
01224183 | 498 | sshpam_err = |
499 | pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle); | |
d948154a | 500 | sshpam_authctxt = authctxt; |
529d73ab | 501 | |
5ff453c0 | 502 | if (sshpam_err != PAM_SUCCESS) { |
503 | pam_end(sshpam_handle, sshpam_err); | |
504 | sshpam_handle = NULL; | |
05114c74 | 505 | return (-1); |
5ff453c0 | 506 | } |
c5a7d788 | 507 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); |
05d62329 | 508 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); |
05114c74 | 509 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); |
5ff453c0 | 510 | if (sshpam_err != PAM_SUCCESS) { |
5b9e2464 | 511 | pam_end(sshpam_handle, sshpam_err); |
5ff453c0 | 512 | sshpam_handle = NULL; |
513 | return (-1); | |
514 | } | |
515 | #ifdef PAM_TTY_KLUDGE | |
aff51935 | 516 | /* |
517 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. | |
518 | * sshd doesn't set the tty until too late in the auth process and | |
5ff453c0 | 519 | * may not even set one (for tty-less connections) |
aff51935 | 520 | */ |
5ff453c0 | 521 | debug("PAM: setting PAM_TTY to \"ssh\""); |
522 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); | |
05114c74 | 523 | if (sshpam_err != PAM_SUCCESS) { |
524 | pam_end(sshpam_handle, sshpam_err); | |
525 | sshpam_handle = NULL; | |
526 | return (-1); | |
a5c9cd31 | 527 | } |
5ff453c0 | 528 | #endif |
05114c74 | 529 | return (0); |
a5c9cd31 | 530 | } |
531 | ||
05114c74 | 532 | static void * |
533 | sshpam_init_ctx(Authctxt *authctxt) | |
a5c9cd31 | 534 | { |
05114c74 | 535 | struct pam_ctxt *ctxt; |
536 | int socks[2]; | |
2b87da3b | 537 | |
90f3c272 | 538 | debug3("PAM: %s entering", __func__); |
7fceb20d | 539 | /* Refuse to start if we don't have PAM enabled */ |
540 | if (!options.use_pam) | |
541 | return NULL; | |
542 | ||
05114c74 | 543 | /* Initialize PAM */ |
529d73ab | 544 | if (sshpam_init(authctxt) == -1) { |
05114c74 | 545 | error("PAM: initialization failed"); |
546 | return (NULL); | |
547 | } | |
5c377b3b | 548 | |
05114c74 | 549 | ctxt = xmalloc(sizeof *ctxt); |
c6630044 | 550 | memset(ctxt, 0, sizeof(*ctxt)); |
05114c74 | 551 | |
552 | /* Start the authentication thread */ | |
553 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { | |
554 | error("PAM: failed create sockets: %s", strerror(errno)); | |
555 | xfree(ctxt); | |
556 | return (NULL); | |
557 | } | |
558 | ctxt->pam_psock = socks[0]; | |
559 | ctxt->pam_csock = socks[1]; | |
560 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { | |
561 | error("PAM: failed to start authentication thread: %s", | |
562 | strerror(errno)); | |
563 | close(socks[0]); | |
564 | close(socks[1]); | |
565 | xfree(ctxt); | |
566 | return (NULL); | |
a5c9cd31 | 567 | } |
c6630044 | 568 | cleanup_ctxt = ctxt; |
05114c74 | 569 | return (ctxt); |
570 | } | |
a5c9cd31 | 571 | |
05114c74 | 572 | static int |
573 | sshpam_query(void *ctx, char **name, char **info, | |
574 | u_int *num, char ***prompts, u_int **echo_on) | |
575 | { | |
576 | Buffer buffer; | |
577 | struct pam_ctxt *ctxt = ctx; | |
578 | size_t plen; | |
579 | u_char type; | |
580 | char *msg; | |
cbdeccf3 | 581 | size_t len; |
05114c74 | 582 | |
90f3c272 | 583 | debug3("PAM: %s entering", __func__); |
05114c74 | 584 | buffer_init(&buffer); |
585 | *name = xstrdup(""); | |
586 | *info = xstrdup(""); | |
587 | *prompts = xmalloc(sizeof(char *)); | |
588 | **prompts = NULL; | |
589 | plen = 0; | |
590 | *echo_on = xmalloc(sizeof(u_int)); | |
591 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { | |
592 | type = buffer_get_char(&buffer); | |
593 | msg = buffer_get_string(&buffer, NULL); | |
594 | switch (type) { | |
595 | case PAM_PROMPT_ECHO_ON: | |
596 | case PAM_PROMPT_ECHO_OFF: | |
597 | *num = 1; | |
cbdeccf3 | 598 | len = plen + strlen(msg) + 1; |
599 | **prompts = xrealloc(**prompts, len); | |
600 | plen += snprintf(**prompts + plen, len, "%s", msg); | |
05114c74 | 601 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
602 | xfree(msg); | |
603 | return (0); | |
604 | case PAM_ERROR_MSG: | |
605 | case PAM_TEXT_INFO: | |
606 | /* accumulate messages */ | |
4f1b45b4 | 607 | len = plen + strlen(msg) + 2; |
cbdeccf3 | 608 | **prompts = xrealloc(**prompts, len); |
4f1b45b4 | 609 | plen += snprintf(**prompts + plen, len, "%s\n", msg); |
05114c74 | 610 | xfree(msg); |
5daf7064 | 611 | break; |
05114c74 | 612 | case PAM_SUCCESS: |
613 | case PAM_AUTH_ERR: | |
614 | if (**prompts != NULL) { | |
615 | /* drain any accumulated messages */ | |
3eaf3960 | 616 | debug("PAM: %s", **prompts); |
617 | buffer_append(&loginmsg, **prompts, | |
618 | strlen(**prompts)); | |
05114c74 | 619 | xfree(**prompts); |
620 | **prompts = NULL; | |
621 | } | |
622 | if (type == PAM_SUCCESS) { | |
2212fc98 | 623 | import_environments(&buffer); |
05114c74 | 624 | *num = 0; |
625 | **echo_on = 0; | |
626 | ctxt->pam_done = 1; | |
627 | xfree(msg); | |
628 | return (0); | |
629 | } | |
1e08e787 | 630 | error("PAM: %s for %s%.100s from %.100s", msg, |
631 | sshpam_authctxt->valid ? "" : "illegal user ", | |
632 | sshpam_authctxt->user, | |
633 | get_remote_name_or_ip(utmp_len, options.use_dns)); | |
e250770b | 634 | /* FALLTHROUGH */ |
5daf7064 | 635 | default: |
05114c74 | 636 | *num = 0; |
637 | **echo_on = 0; | |
638 | xfree(msg); | |
639 | ctxt->pam_done = -1; | |
640 | return (-1); | |
641 | } | |
a5c9cd31 | 642 | } |
05114c74 | 643 | return (-1); |
a5c9cd31 | 644 | } |
645 | ||
05114c74 | 646 | /* XXX - see also comment in auth-chall.c:verify_response */ |
647 | static int | |
648 | sshpam_respond(void *ctx, u_int num, char **resp) | |
a5c9cd31 | 649 | { |
05114c74 | 650 | Buffer buffer; |
651 | struct pam_ctxt *ctxt = ctx; | |
652 | ||
90f3c272 | 653 | debug2("PAM: %s entering, %d responses", __func__, num); |
05114c74 | 654 | switch (ctxt->pam_done) { |
655 | case 1: | |
656 | sshpam_authenticated = 1; | |
657 | return (0); | |
658 | case 0: | |
659 | break; | |
660 | default: | |
661 | return (-1); | |
a5c9cd31 | 662 | } |
05114c74 | 663 | if (num != 1) { |
664 | error("PAM: expected one response, got %u", num); | |
665 | return (-1); | |
666 | } | |
667 | buffer_init(&buffer); | |
668 | buffer_put_cstring(&buffer, *resp); | |
7b2a0de3 | 669 | if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) { |
670 | buffer_free(&buffer); | |
671 | return (-1); | |
672 | } | |
05114c74 | 673 | buffer_free(&buffer); |
674 | return (1); | |
a5c9cd31 | 675 | } |
676 | ||
05114c74 | 677 | static void |
678 | sshpam_free_ctx(void *ctxtp) | |
a5c9cd31 | 679 | { |
05114c74 | 680 | struct pam_ctxt *ctxt = ctxtp; |
39ce53de | 681 | |
90f3c272 | 682 | debug3("PAM: %s entering", __func__); |
c6630044 | 683 | sshpam_thread_cleanup(); |
05114c74 | 684 | xfree(ctxt); |
685 | /* | |
686 | * We don't call sshpam_cleanup() here because we may need the PAM | |
687 | * handle at a later stage, e.g. when setting up a session. It's | |
688 | * still on the cleanup list, so pam_end() *will* be called before | |
689 | * the server process terminates. | |
690 | */ | |
ad55cd03 | 691 | } |
692 | ||
05114c74 | 693 | KbdintDevice sshpam_device = { |
694 | "pam", | |
695 | sshpam_init_ctx, | |
696 | sshpam_query, | |
697 | sshpam_respond, | |
698 | sshpam_free_ctx | |
699 | }; | |
700 | ||
701 | KbdintDevice mm_sshpam_device = { | |
702 | "pam", | |
703 | mm_sshpam_init_ctx, | |
704 | mm_sshpam_query, | |
705 | mm_sshpam_respond, | |
706 | mm_sshpam_free_ctx | |
707 | }; | |
2919e060 | 708 | |
2b87da3b | 709 | /* |
05114c74 | 710 | * This replaces auth-pam.c |
ad55cd03 | 711 | */ |
05114c74 | 712 | void |
529d73ab | 713 | start_pam(Authctxt *authctxt) |
ad55cd03 | 714 | { |
817e6d38 | 715 | if (!options.use_pam) |
716 | fatal("PAM: initialisation requested when UsePAM=no"); | |
717 | ||
529d73ab | 718 | if (sshpam_init(authctxt) == -1) |
05114c74 | 719 | fatal("PAM: initialisation failed"); |
a5c9cd31 | 720 | } |
721 | ||
05114c74 | 722 | void |
723 | finish_pam(void) | |
a5c9cd31 | 724 | { |
c6630044 | 725 | sshpam_cleanup(); |
a5c9cd31 | 726 | } |
727 | ||
5b9e2464 | 728 | u_int |
729 | do_pam_account(void) | |
a5c9cd31 | 730 | { |
b3ef7fb7 | 731 | if (sshpam_account_status != -1) |
732 | return (sshpam_account_status); | |
733 | ||
5b9e2464 | 734 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
90f3c272 | 735 | debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err); |
b3ef7fb7 | 736 | |
737 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { | |
738 | sshpam_account_status = 0; | |
739 | return (sshpam_account_status); | |
5b9e2464 | 740 | } |
741 | ||
b3ef7fb7 | 742 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) |
c10bb2ce | 743 | sshpam_password_change_required(1); |
b3ef7fb7 | 744 | |
745 | sshpam_account_status = 1; | |
746 | return (sshpam_account_status); | |
05114c74 | 747 | } |
46c76c63 | 748 | |
49e82bb9 | 749 | void |
750 | do_pam_set_tty(const char *tty) | |
751 | { | |
26b3608b | 752 | if (tty != NULL) { |
753 | debug("PAM: setting PAM_TTY to \"%s\"", tty); | |
754 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty); | |
755 | if (sshpam_err != PAM_SUCCESS) | |
756 | fatal("PAM: failed to set PAM_TTY: %s", | |
757 | pam_strerror(sshpam_handle, sshpam_err)); | |
758 | } | |
05114c74 | 759 | } |
cbd7492e | 760 | |
05114c74 | 761 | void |
762 | do_pam_setcred(int init) | |
763 | { | |
764 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
765 | (const void *)&null_conv); | |
766 | if (sshpam_err != PAM_SUCCESS) | |
767 | fatal("PAM: failed to set PAM_CONV: %s", | |
768 | pam_strerror(sshpam_handle, sshpam_err)); | |
769 | if (init) { | |
770 | debug("PAM: establishing credentials"); | |
771 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); | |
772 | } else { | |
773 | debug("PAM: reinitializing credentials"); | |
774 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); | |
775 | } | |
776 | if (sshpam_err == PAM_SUCCESS) { | |
777 | sshpam_cred_established = 1; | |
778 | return; | |
779 | } | |
780 | if (sshpam_authenticated) | |
781 | fatal("PAM: pam_setcred(): %s", | |
782 | pam_strerror(sshpam_handle, sshpam_err)); | |
783 | else | |
784 | debug("PAM: pam_setcred(): %s", | |
785 | pam_strerror(sshpam_handle, sshpam_err)); | |
a5c9cd31 | 786 | } |
787 | ||
05114c74 | 788 | static int |
8600a4ab | 789 | sshpam_tty_conv(int n, const struct pam_message **msg, |
5b9e2464 | 790 | struct pam_response **resp, void *data) |
ee48c949 | 791 | { |
05114c74 | 792 | char input[PAM_MAX_MSG_SIZE]; |
0a23d79f | 793 | struct pam_response *reply; |
ee48c949 | 794 | int i; |
795 | ||
60922169 | 796 | debug3("PAM: %s called with %d messages", __func__, n); |
797 | ||
0a23d79f | 798 | *resp = NULL; |
799 | ||
3eaf3960 | 800 | if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) |
05114c74 | 801 | return (PAM_CONV_ERR); |
0a23d79f | 802 | |
803 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
804 | return (PAM_CONV_ERR); | |
805 | memset(reply, 0, n * sizeof(*reply)); | |
806 | ||
05114c74 | 807 | for (i = 0; i < n; ++i) { |
808 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
809 | case PAM_PROMPT_ECHO_OFF: | |
0a23d79f | 810 | reply[i].resp = |
aff51935 | 811 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
05114c74 | 812 | RP_ALLOW_STDIN); |
0a23d79f | 813 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 814 | break; |
815 | case PAM_PROMPT_ECHO_ON: | |
74117b26 | 816 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
05114c74 | 817 | fgets(input, sizeof input, stdin); |
0a23d79f | 818 | reply[i].resp = xstrdup(input); |
819 | reply[i].resp_retcode = PAM_SUCCESS; | |
05114c74 | 820 | break; |
821 | case PAM_ERROR_MSG: | |
822 | case PAM_TEXT_INFO: | |
74117b26 | 823 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
0a23d79f | 824 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 825 | break; |
826 | default: | |
827 | goto fail; | |
828 | } | |
ee48c949 | 829 | } |
0a23d79f | 830 | *resp = reply; |
05114c74 | 831 | return (PAM_SUCCESS); |
0a23d79f | 832 | |
05114c74 | 833 | fail: |
0a23d79f | 834 | for(i = 0; i < n; i++) { |
835 | if (reply[i].resp != NULL) | |
836 | xfree(reply[i].resp); | |
837 | } | |
838 | xfree(reply); | |
05114c74 | 839 | return (PAM_CONV_ERR); |
ee48c949 | 840 | } |
841 | ||
8600a4ab | 842 | static struct pam_conv tty_conv = { sshpam_tty_conv, NULL }; |
3eaf3960 | 843 | |
05114c74 | 844 | /* |
845 | * XXX this should be done in the authentication phase, but ssh1 doesn't | |
846 | * support that | |
847 | */ | |
848 | void | |
849 | do_pam_chauthtok(void) | |
a5c9cd31 | 850 | { |
05114c74 | 851 | if (use_privsep) |
5b9e2464 | 852 | fatal("Password expired (unable to change with privsep)"); |
05114c74 | 853 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
3eaf3960 | 854 | (const void *)&tty_conv); |
05114c74 | 855 | if (sshpam_err != PAM_SUCCESS) |
856 | fatal("PAM: failed to set PAM_CONV: %s", | |
857 | pam_strerror(sshpam_handle, sshpam_err)); | |
858 | debug("PAM: changing password"); | |
859 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); | |
860 | if (sshpam_err != PAM_SUCCESS) | |
861 | fatal("PAM: pam_chauthtok(): %s", | |
862 | pam_strerror(sshpam_handle, sshpam_err)); | |
5daf7064 | 863 | } |
864 | ||
ef687c66 | 865 | static int |
8600a4ab | 866 | sshpam_store_conv(int n, const struct pam_message **msg, |
ef687c66 | 867 | struct pam_response **resp, void *data) |
868 | { | |
869 | struct pam_response *reply; | |
870 | int i; | |
871 | size_t len; | |
872 | ||
873 | debug3("PAM: %s called with %d messages", __func__, n); | |
874 | *resp = NULL; | |
875 | ||
876 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
877 | return (PAM_CONV_ERR); | |
878 | ||
879 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
880 | return (PAM_CONV_ERR); | |
881 | memset(reply, 0, n * sizeof(*reply)); | |
882 | ||
883 | for (i = 0; i < n; ++i) { | |
884 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
885 | case PAM_ERROR_MSG: | |
886 | case PAM_TEXT_INFO: | |
887 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); | |
888 | buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len); | |
889 | buffer_append(&loginmsg, "\n", 1 ); | |
890 | reply[i].resp_retcode = PAM_SUCCESS; | |
891 | break; | |
892 | default: | |
893 | goto fail; | |
894 | } | |
895 | } | |
896 | *resp = reply; | |
897 | return (PAM_SUCCESS); | |
898 | ||
899 | fail: | |
900 | for(i = 0; i < n; i++) { | |
901 | if (reply[i].resp != NULL) | |
902 | xfree(reply[i].resp); | |
903 | } | |
904 | xfree(reply); | |
905 | return (PAM_CONV_ERR); | |
906 | } | |
907 | ||
8600a4ab | 908 | static struct pam_conv store_conv = { sshpam_store_conv, NULL }; |
ef687c66 | 909 | |
3eaf3960 | 910 | void |
911 | do_pam_session(void) | |
912 | { | |
dd1fb864 | 913 | debug3("PAM: opening session"); |
aff51935 | 914 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
ef687c66 | 915 | (const void *)&store_conv); |
3eaf3960 | 916 | if (sshpam_err != PAM_SUCCESS) |
917 | fatal("PAM: failed to set PAM_CONV: %s", | |
918 | pam_strerror(sshpam_handle, sshpam_err)); | |
919 | sshpam_err = pam_open_session(sshpam_handle, 0); | |
920 | if (sshpam_err != PAM_SUCCESS) | |
921 | fatal("PAM: pam_open_session(): %s", | |
922 | pam_strerror(sshpam_handle, sshpam_err)); | |
923 | sshpam_session_open = 1; | |
924 | } | |
925 | ||
aff51935 | 926 | /* |
749560dd | 927 | * Set a PAM environment string. We need to do this so that the session |
928 | * modules can handle things like Kerberos/GSI credentials that appear | |
929 | * during the ssh authentication process. | |
930 | */ | |
749560dd | 931 | int |
aff51935 | 932 | do_pam_putenv(char *name, char *value) |
749560dd | 933 | { |
749560dd | 934 | int ret = 1; |
b6453d99 | 935 | #ifdef HAVE_PAM_PUTENV |
263c65df | 936 | char *compound; |
937 | size_t len; | |
938 | ||
939 | len = strlen(name) + strlen(value) + 2; | |
940 | compound = xmalloc(len); | |
941 | ||
942 | snprintf(compound, len, "%s=%s", name, value); | |
943 | ret = pam_putenv(sshpam_handle, compound); | |
944 | xfree(compound); | |
749560dd | 945 | #endif |
263c65df | 946 | |
749560dd | 947 | return (ret); |
948 | } | |
949 | ||
2212fc98 | 950 | char ** |
951 | fetch_pam_child_environment(void) | |
952 | { | |
953 | return sshpam_env; | |
954 | } | |
955 | ||
05114c74 | 956 | char ** |
957 | fetch_pam_environment(void) | |
958 | { | |
05114c74 | 959 | return (pam_getenvlist(sshpam_handle)); |
05114c74 | 960 | } |
5c377b3b | 961 | |
05114c74 | 962 | void |
963 | free_pam_environment(char **env) | |
964 | { | |
965 | char **envp; | |
5daf7064 | 966 | |
0eb6370a | 967 | if (env == NULL) |
968 | return; | |
969 | ||
05114c74 | 970 | for (envp = env; *envp; envp++) |
971 | xfree(*envp); | |
972 | xfree(env); | |
a5c9cd31 | 973 | } |
974 | ||
5de92f17 | 975 | /* |
976 | * "Blind" conversation function for password authentication. Assumes that | |
977 | * echo-off prompts are for the password and stores messages for later | |
978 | * display. | |
979 | */ | |
980 | static int | |
981 | sshpam_passwd_conv(int n, const struct pam_message **msg, | |
982 | struct pam_response **resp, void *data) | |
983 | { | |
984 | struct pam_response *reply; | |
985 | int i; | |
986 | size_t len; | |
987 | ||
988 | debug3("PAM: %s called with %d messages", __func__, n); | |
989 | ||
990 | *resp = NULL; | |
991 | ||
992 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
993 | return (PAM_CONV_ERR); | |
994 | ||
995 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
996 | return (PAM_CONV_ERR); | |
997 | memset(reply, 0, n * sizeof(*reply)); | |
998 | ||
999 | for (i = 0; i < n; ++i) { | |
1000 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
1001 | case PAM_PROMPT_ECHO_OFF: | |
1002 | if (sshpam_password == NULL) | |
1003 | goto fail; | |
1004 | reply[i].resp = xstrdup(sshpam_password); | |
1005 | reply[i].resp_retcode = PAM_SUCCESS; | |
1006 | break; | |
1007 | case PAM_ERROR_MSG: | |
1008 | case PAM_TEXT_INFO: | |
1009 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); | |
1010 | if (len > 0) { | |
1011 | buffer_append(&loginmsg, | |
1012 | PAM_MSG_MEMBER(msg, i, msg), len); | |
1013 | buffer_append(&loginmsg, "\n", 1); | |
1014 | } | |
1015 | reply[i].resp = xstrdup(""); | |
1016 | reply[i].resp_retcode = PAM_SUCCESS; | |
1017 | break; | |
1018 | default: | |
1019 | goto fail; | |
1020 | } | |
1021 | } | |
1022 | *resp = reply; | |
1023 | return (PAM_SUCCESS); | |
1024 | ||
1025 | fail: | |
1026 | for(i = 0; i < n; i++) { | |
1027 | if (reply[i].resp != NULL) | |
1028 | xfree(reply[i].resp); | |
1029 | } | |
1030 | xfree(reply); | |
1031 | return (PAM_CONV_ERR); | |
1032 | } | |
1033 | ||
1034 | static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL }; | |
1035 | ||
1036 | /* | |
1037 | * Attempt password authentication via PAM | |
1038 | */ | |
1039 | int | |
1040 | sshpam_auth_passwd(Authctxt *authctxt, const char *password) | |
1041 | { | |
1042 | int flags = (options.permit_empty_passwd == 0 ? | |
1043 | PAM_DISALLOW_NULL_AUTHTOK : 0); | |
0e716148 | 1044 | static char badpw[] = "\b\n\r\177INCORRECT"; |
5de92f17 | 1045 | |
1046 | if (!options.use_pam || sshpam_handle == NULL) | |
1047 | fatal("PAM: %s called when PAM disabled or failed to " | |
1048 | "initialise.", __func__); | |
1049 | ||
1050 | sshpam_password = password; | |
1051 | sshpam_authctxt = authctxt; | |
1052 | ||
0e716148 | 1053 | /* |
1054 | * If the user logging in is invalid, or is root but is not permitted | |
1055 | * by PermitRootLogin, use an invalid password to prevent leaking | |
1056 | * information via timing (eg if the PAM config has a delay on fail). | |
1057 | */ | |
1058 | if (!authctxt->valid || (authctxt->pw->pw_uid == 0 && | |
1059 | options.permit_root_login != PERMIT_YES)) | |
1060 | sshpam_password = badpw; | |
1061 | ||
5de92f17 | 1062 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
1063 | (const void *)&passwd_conv); | |
1064 | if (sshpam_err != PAM_SUCCESS) | |
1065 | fatal("PAM: %s: failed to set PAM_CONV: %s", __func__, | |
1066 | pam_strerror(sshpam_handle, sshpam_err)); | |
1067 | ||
1068 | sshpam_err = pam_authenticate(sshpam_handle, flags); | |
1069 | sshpam_password = NULL; | |
1070 | if (sshpam_err == PAM_SUCCESS && authctxt->valid) { | |
1071 | debug("PAM: password authentication accepted for %.100s", | |
1072 | authctxt->user); | |
1073 | return 1; | |
1074 | } else { | |
1075 | debug("PAM: password authentication failed for %.100s: %s", | |
1076 | authctxt->valid ? authctxt->user : "an illegal user", | |
1077 | pam_strerror(sshpam_handle, sshpam_err)); | |
1078 | return 0; | |
1079 | } | |
1080 | } | |
a5c9cd31 | 1081 | #endif /* USE_PAM */ |