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