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