pam_krb5.c revision 106864
1/*- 2 * Copyright 2001 Mark R V Murray 3 * Copyright Frank Cusack fcusack@fcusack.com 1999-2000 4 * All rights reserved 5 * Copyright (c) 2002 Networks Associates Technology, Inc. 6 * All rights reserved. 7 * 8 * Portions of this software were developed for the FreeBSD Project by 9 * ThinkSec AS and NAI Labs, the Security Research Division of Network 10 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 11 * ("CBOSS"), as part of the DARPA CHATS research program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, and the entire permission notice in its entirety, 18 * including the disclaimer of warranties. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. The name of the author may not be used to endorse or promote 23 * products derived from this software without specific prior 24 * written permission. 25 * 26 * ALTERNATIVELY, this product may be distributed under the terms of 27 * the GNU Public License, in which case the provisions of the GPL are 28 * required INSTEAD OF the above restrictions. (This clause is 29 * necessary due to a potential bad interaction between the GPL and 30 * the restrictions contained in a BSD-style copyright.) 31 * 32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 35 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 42 * OF THE POSSIBILITY OF SUCH DAMAGE. 43 * --------------------------------------------------------------------------- 44 * 45 * This software may contain code from Naomaru Itoi: 46 * 47 * PAM-kerberos5 module Copyright notice. 48 * Naomaru Itoi <itoi@eecs.umich.edu>, June 24, 1997. 49 * 50 * ---------------------------------------------------------------------------- 51 * COPYRIGHT (c) 1997 52 * THE REGENTS OF THE UNIVERSITY OF MICHIGAN 53 * ALL RIGHTS RESERVED 54 * 55 * PERMISSION IS GRANTED TO USE, COPY, CREATE DERIVATIVE WORKS AND REDISTRIBUTE 56 * THIS SOFTWARE AND SUCH DERIVATIVE WORKS FOR ANY PURPOSE, SO LONG AS THE NAME 57 * OF THE UNIVERSITY OF MICHIGAN IS NOT USED IN ANY ADVERTISING OR PUBLICITY 58 * PERTAINING TO THE USE OR DISTRIBUTION OF THIS SOFTWARE WITHOUT SPECIFIC, 59 * WRITTEN PRIOR AUTHORIZATION. IF THE ABOVE COPYRIGHT NOTICE OR ANY OTHER 60 * IDENTIFICATION OF THE UNIVERSITY OF MICHIGAN IS INCLUDED IN ANY COPY OF ANY 61 * PORTION OF THIS SOFTWARE, THEN THE DISCLAIMER BELOW MUST ALSO BE INCLUDED. 62 * 63 * THE SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE UNIVERSITY OF 64 * MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND WITHOUT WARRANTY BY THE 65 * UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING 66 * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABITILY AND FITNESS FOR A 67 * PARTICULAR PURPOSE. THE REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE 68 * LIABLE FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR 69 * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING OUT OF OR IN 70 * CONNECTION WITH THE USE OF THE SOFTWARE, EVEN IF IT HAS BEEN OR IS HEREAFTER 71 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 72 * 73 * PAM-kerberos5 module is written based on PAM-kerberos4 module 74 * by Derrick J. Brashear and kerberos5-1.0pl1 by M.I.T. kerberos team. 75 * Permission to use, copy, modify, distribute this software is hereby 76 * granted, as long as it is granted by Derrick J. Brashear and 77 * M.I.T. kerberos team. Followings are their copyright information. 78 * ---------------------------------------------------------------------------- 79 * 80 * This software may contain code from Derrick J. Brashear: 81 * 82 * 83 * Copyright (c) Derrick J. Brashear, 1996. All rights reserved 84 * 85 * Redistribution and use in source and binary forms, with or without 86 * modification, are permitted provided that the following conditions 87 * are met: 88 * 1. Redistributions of source code must retain the above copyright 89 * notice, and the entire permission notice in its entirety, 90 * including the disclaimer of warranties. 91 * 2. Redistributions in binary form must reproduce the above copyright 92 * notice, this list of conditions and the following disclaimer in the 93 * documentation and/or other materials provided with the distribution. 94 * 3. The name of the author may not be used to endorse or promote 95 * products derived from this software without specific prior 96 * written permission. 97 * 98 * ALTERNATIVELY, this product may be distributed under the terms of 99 * the GNU Public License, in which case the provisions of the GPL are 100 * required INSTEAD OF the above restrictions. (This clause is 101 * necessary due to a potential bad interaction between the GPL and 102 * the restrictions contained in a BSD-style copyright.) 103 * 104 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 105 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 106 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 107 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 108 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 109 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 110 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 111 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 112 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 113 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 114 * OF THE POSSIBILITY OF SUCH DAMAGE. 115 * 116 * ---------------------------------------------------------------------------- 117 * 118 * This software may contain code from MIT Kerberos 5: 119 * 120 * Copyright Notice and Legal Administrivia 121 * ---------------------------------------- 122 * 123 * Copyright (C) 1996 by the Massachusetts Institute of Technology. 124 * 125 * All rights reserved. 126 * 127 * Export of this software from the United States of America may require 128 * a specific license from the United States Government. It is the 129 * responsibility of any person or organization contemplating export to 130 * obtain such a license before exporting. 131 * 132 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 133 * distribute this software and its documentation for any purpose and 134 * without fee is hereby granted, provided that the above copyright 135 * notice appear in all copies and that both that copyright notice and 136 * this permission notice appear in supporting documentation, and that 137 * the name of M.I.T. not be used in advertising or publicity pertaining 138 * to distribution of the software without specific, written prior 139 * permission. M.I.T. makes no representations about the suitability of 140 * this software for any purpose. It is provided "as is" without express 141 * or implied warranty. 142 * 143 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 144 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 145 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 146 * 147 * Individual source code files are copyright MIT, Cygnus Support, 148 * OpenVision, Oracle, Sun Soft, and others. 149 * 150 * Project Athena, Athena, Athena MUSE, Discuss, Hesiod, Kerberos, Moira, 151 * and Zephyr are trademarks of the Massachusetts Institute of Technology 152 * (MIT). No commercial use of these trademarks may be made without 153 * prior written permission of MIT. 154 * 155 * "Commercial use" means use of a name in a product or other for-profit 156 * manner. It does NOT prevent a commercial firm from referring to the 157 * MIT trademarks in order to convey information (although in doing so, 158 * recognition of their trademark status should be given). 159 * 160 * The following copyright and permission notice applies to the 161 * OpenVision Kerberos Administration system located in kadmin/create, 162 * kadmin/dbutil, kadmin/passwd, kadmin/server, lib/kadm5, and portions 163 * of lib/rpc: 164 * 165 * Copyright, OpenVision Technologies, Inc., 1996, All Rights Reserved 166 * 167 * WARNING: Retrieving the OpenVision Kerberos Administration system 168 * source code, as described below, indicates your acceptance of the 169 * following terms. If you do not agree to the following terms, do not 170 * retrieve the OpenVision Kerberos administration system. 171 * 172 * You may freely use and distribute the Source Code and Object Code 173 * compiled from it, with or without modification, but this Source 174 * Code is provided to you "AS IS" EXCLUSIVE OF ANY WARRANTY, 175 * INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY OR 176 * FITNESS FOR A PARTICULAR PURPOSE, OR ANY OTHER WARRANTY, WHETHER 177 * EXPRESS OR IMPLIED. IN NO EVENT WILL OPENVISION HAVE ANY LIABILITY 178 * FOR ANY LOST PROFITS, LOSS OF DATA OR COSTS OF PROCUREMENT OF 179 * SUBSTITUTE GOODS OR SERVICES, OR FOR ANY SPECIAL, INDIRECT, OR 180 * CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, INCLUDING, 181 * WITHOUT LIMITATION, THOSE RESULTING FROM THE USE OF THE SOURCE 182 * CODE, OR THE FAILURE OF THE SOURCE CODE TO PERFORM, OR FOR ANY 183 * OTHER REASON. 184 * 185 * OpenVision retains all copyrights in the donated Source Code. OpenVision 186 * also retains copyright to derivative works of the Source Code, whether 187 * created by OpenVision or by a third party. The OpenVision copyright 188 * notice must be preserved if derivative works are made based on the 189 * donated Source Code. 190 * 191 * OpenVision Technologies, Inc. has donated this Kerberos 192 * Administration system to MIT for inclusion in the standard 193 * Kerberos 5 distribution. This donation underscores our 194 * commitment to continuing Kerberos technology development 195 * and our gratitude for the valuable work which has been 196 * performed by MIT and the Kerberos community. 197 * 198 */ 199 200#include <sys/cdefs.h> 201__FBSDID("$FreeBSD: head/lib/libpam/modules/pam_krb5/pam_krb5.c 106864 2002-11-13 17:46:15Z nectar $"); 202 203#include <sys/types.h> 204#include <sys/stat.h> 205#include <errno.h> 206#include <limits.h> 207#include <pwd.h> 208#include <stdio.h> 209#include <stdlib.h> 210#include <string.h> 211#include <syslog.h> 212#include <unistd.h> 213 214#include <krb5.h> 215#include <com_err.h> 216 217#define PAM_SM_AUTH 218#define PAM_SM_ACCOUNT 219#define PAM_SM_PASSWORD 220 221#include <security/pam_appl.h> 222#include <security/pam_modules.h> 223#include <security/pam_mod_misc.h> 224 225#define COMPAT_HEIMDAL 226/* #define COMPAT_MIT */ 227 228static int verify_krb_v5_tgt(krb5_context, krb5_ccache, char *, int); 229static void cleanup_cache(pam_handle_t *, void *, int); 230static const char *compat_princ_component(krb5_context, krb5_principal, int); 231static void compat_free_data_contents(krb5_context, krb5_data *); 232 233#define USER_PROMPT "Username: " 234#define PASSWORD_PROMPT "Password:" 235#define NEW_PASSWORD_PROMPT "New Password:" 236 237enum { 238 PAM_OPT_AUTH_AS_SELF = PAM_OPT_STD_MAX, 239 PAM_OPT_CCACHE, 240 PAM_OPT_FORWARDABLE, 241 PAM_OPT_NO_CCACHE, 242 PAM_OPT_REUSE_CCACHE 243}; 244 245static struct opttab other_options[] = { 246 { "auth_as_self", PAM_OPT_AUTH_AS_SELF }, 247 { "ccache", PAM_OPT_CCACHE }, 248 { "forwardable", PAM_OPT_FORWARDABLE }, 249 { "no_ccache", PAM_OPT_NO_CCACHE }, 250 { "reuse_ccache", PAM_OPT_REUSE_CCACHE }, 251 { NULL, 0 } 252}; 253 254/* 255 * authentication management 256 */ 257PAM_EXTERN int 258pam_sm_authenticate(pam_handle_t *pamh, int flags __unused, 259 int argc, const char *argv[]) 260{ 261 krb5_error_code krbret; 262 krb5_context pam_context; 263 krb5_creds creds; 264 krb5_principal princ; 265 krb5_ccache ccache; 266 krb5_get_init_creds_opt opts; 267 struct options options; 268 struct passwd *pwd; 269 int retval; 270 const char *sourceuser, *user, *pass, *service; 271 char *principal, *princ_name, *ccache_name, luser[32], *srvdup; 272 273 pam_std_option(&options, other_options, argc, argv); 274 275 PAM_LOG("Options processed"); 276 277 retval = pam_get_user(pamh, &user, USER_PROMPT); 278 if (retval != PAM_SUCCESS) 279 return (retval); 280 281 PAM_LOG("Got user: %s", user); 282 283 retval = pam_get_item(pamh, PAM_RUSER, (const void **)&sourceuser); 284 if (retval != PAM_SUCCESS) 285 return (retval); 286 287 PAM_LOG("Got ruser: %s", sourceuser); 288 289 service = NULL; 290 pam_get_item(pamh, PAM_SERVICE, (const void **)&service); 291 if (service == NULL) 292 service = "unknown"; 293 294 PAM_LOG("Got service: %s", service); 295 296 krbret = krb5_init_context(&pam_context); 297 if (krbret != 0) { 298 PAM_VERBOSE_ERROR("Kerberos 5 error"); 299 return (PAM_SERVICE_ERR); 300 } 301 302 PAM_LOG("Context initialised"); 303 304 krb5_get_init_creds_opt_init(&opts); 305 306 if (pam_test_option(&options, PAM_OPT_FORWARDABLE, NULL)) 307 krb5_get_init_creds_opt_set_forwardable(&opts, 1); 308 309 PAM_LOG("Credentials initialised"); 310 311 krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE); 312 if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) { 313 PAM_VERBOSE_ERROR("Kerberos 5 error"); 314 retval = PAM_SERVICE_ERR; 315 goto cleanup3; 316 } 317 318 PAM_LOG("Done krb5_cc_register()"); 319 320 /* Get principal name */ 321 if (pam_test_option(&options, PAM_OPT_AUTH_AS_SELF, NULL)) 322 asprintf(&principal, "%s/%s", sourceuser, user); 323 else 324 principal = strdup(user); 325 326 PAM_LOG("Created principal: %s", principal); 327 328 krbret = krb5_parse_name(pam_context, principal, &princ); 329 free(principal); 330 if (krbret != 0) { 331 PAM_LOG("Error krb5_parse_name(): %s", 332 krb5_get_err_text(pam_context, krbret)); 333 PAM_VERBOSE_ERROR("Kerberos 5 error"); 334 retval = PAM_SERVICE_ERR; 335 goto cleanup3; 336 } 337 338 PAM_LOG("Done krb5_parse_name()"); 339 340 /* Now convert the principal name into something human readable */ 341 princ_name = NULL; 342 krbret = krb5_unparse_name(pam_context, princ, &princ_name); 343 if (krbret != 0) { 344 PAM_LOG("Error krb5_unparse_name(): %s", 345 krb5_get_err_text(pam_context, krbret)); 346 PAM_VERBOSE_ERROR("Kerberos 5 error"); 347 retval = PAM_SERVICE_ERR; 348 goto cleanup2; 349 } 350 351 PAM_LOG("Got principal: %s", princ_name); 352 353 /* Get password */ 354 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT); 355 if (retval != PAM_SUCCESS) 356 goto cleanup2; 357 358 PAM_LOG("Got password"); 359 360 /* Verify the local user exists (AFTER getting the password) */ 361 if (strchr(user, '@')) { 362 /* get a local account name for this principal */ 363 krbret = krb5_aname_to_localname(pam_context, princ, 364 sizeof(luser), luser); 365 if (krbret != 0) { 366 PAM_VERBOSE_ERROR("Kerberos 5 error"); 367 PAM_LOG("Error krb5_aname_to_localname(): %s", 368 krb5_get_err_text(pam_context, krbret)); 369 retval = PAM_USER_UNKNOWN; 370 goto cleanup2; 371 } 372 373 retval = pam_set_item(pamh, PAM_USER, luser); 374 if (retval != PAM_SUCCESS) 375 goto cleanup2; 376 377 retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 378 if (retval != PAM_SUCCESS) 379 goto cleanup2; 380 381 PAM_LOG("PAM_USER Redone"); 382 } 383 384 pwd = getpwnam(user); 385 if (pwd == NULL) { 386 retval = PAM_USER_UNKNOWN; 387 goto cleanup2; 388 } 389 390 PAM_LOG("Done getpwnam()"); 391 392 /* Get a TGT */ 393 memset(&creds, 0, sizeof(krb5_creds)); 394 krbret = krb5_get_init_creds_password(pam_context, &creds, princ, 395 pass, NULL, pamh, 0, NULL, &opts); 396 if (krbret != 0) { 397 PAM_VERBOSE_ERROR("Kerberos 5 error"); 398 PAM_LOG("Error krb5_get_init_creds_password(): %s", 399 krb5_get_err_text(pam_context, krbret)); 400 retval = PAM_AUTH_ERR; 401 goto cleanup2; 402 } 403 404 PAM_LOG("Got TGT"); 405 406 /* Generate a temporary cache */ 407 krbret = krb5_cc_gen_new(pam_context, &krb5_mcc_ops, &ccache); 408 if (krbret != 0) { 409 PAM_VERBOSE_ERROR("Kerberos 5 error"); 410 PAM_LOG("Error krb5_cc_gen_new(): %s", 411 krb5_get_err_text(pam_context, krbret)); 412 retval = PAM_SERVICE_ERR; 413 goto cleanup; 414 } 415 krbret = krb5_cc_initialize(pam_context, ccache, princ); 416 if (krbret != 0) { 417 PAM_VERBOSE_ERROR("Kerberos 5 error"); 418 PAM_LOG("Error krb5_cc_initialize(): %s", 419 krb5_get_err_text(pam_context, krbret)); 420 retval = PAM_SERVICE_ERR; 421 goto cleanup; 422 } 423 krbret = krb5_cc_store_cred(pam_context, ccache, &creds); 424 if (krbret != 0) { 425 PAM_VERBOSE_ERROR("Kerberos 5 error"); 426 PAM_LOG("Error krb5_cc_store_cred(): %s", 427 krb5_get_err_text(pam_context, krbret)); 428 krb5_cc_destroy(pam_context, ccache); 429 retval = PAM_SERVICE_ERR; 430 goto cleanup; 431 } 432 433 PAM_LOG("Credentials stashed"); 434 435 /* Verify them */ 436 if ((srvdup = strdup(service)) == NULL) { 437 retval = PAM_BUF_ERR; 438 goto cleanup; 439 } 440 krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup, 441 pam_test_option(&options, PAM_OPT_FORWARDABLE, NULL)); 442 free(srvdup); 443 if (krbret == -1) { 444 PAM_VERBOSE_ERROR("Kerberos 5 error"); 445 krb5_cc_destroy(pam_context, ccache); 446 retval = PAM_AUTH_ERR; 447 goto cleanup; 448 } 449 450 PAM_LOG("Credentials stash verified"); 451 452 retval = pam_get_data(pamh, "ccache", (const void **)&ccache_name); 453 if (retval == PAM_SUCCESS) { 454 krb5_cc_destroy(pam_context, ccache); 455 PAM_VERBOSE_ERROR("Kerberos 5 error"); 456 retval = PAM_AUTH_ERR; 457 goto cleanup; 458 } 459 460 PAM_LOG("Credentials stash not pre-existing"); 461 462 asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context, 463 ccache), krb5_cc_get_name(pam_context, ccache)); 464 if (ccache_name == NULL) { 465 PAM_VERBOSE_ERROR("Kerberos 5 error"); 466 retval = PAM_BUF_ERR; 467 goto cleanup; 468 } 469 retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache); 470 if (retval != 0) { 471 krb5_cc_destroy(pam_context, ccache); 472 PAM_VERBOSE_ERROR("Kerberos 5 error"); 473 retval = PAM_SERVICE_ERR; 474 goto cleanup; 475 } 476 477 PAM_LOG("Credentials stash saved"); 478 479cleanup: 480 krb5_free_cred_contents(pam_context, &creds); 481 PAM_LOG("Done cleanup"); 482cleanup2: 483 krb5_free_principal(pam_context, princ); 484 PAM_LOG("Done cleanup2"); 485cleanup3: 486 if (princ_name) 487 free(princ_name); 488 489 krb5_free_context(pam_context); 490 491 PAM_LOG("Done cleanup3"); 492 493 if (retval != PAM_SUCCESS) 494 PAM_VERBOSE_ERROR("Kerberos 5 refuses you"); 495 496 return (retval); 497} 498 499PAM_EXTERN int 500pam_sm_setcred(pam_handle_t *pamh, int flags, 501 int argc, const char *argv[]) 502{ 503 504 krb5_error_code krbret; 505 krb5_context pam_context; 506 krb5_principal princ; 507 krb5_creds creds; 508 krb5_ccache ccache_temp, ccache_perm; 509 krb5_cc_cursor cursor; 510 struct options options; 511 struct passwd *pwd = NULL; 512 int retval; 513 char *user; 514 char *cache_name, *cache_env_name, *p, *q; 515 516 uid_t euid; 517 gid_t egid; 518 519 pam_std_option(&options, other_options, argc, argv); 520 521 PAM_LOG("Options processed"); 522 523 if (flags & PAM_DELETE_CRED) 524 return (PAM_SUCCESS); 525 526 if (flags & PAM_REFRESH_CRED) 527 return (PAM_SUCCESS); 528 529 if (flags & PAM_REINITIALIZE_CRED) 530 return (PAM_SUCCESS); 531 532 if (!(flags & PAM_ESTABLISH_CRED)) 533 return (PAM_SERVICE_ERR); 534 535 PAM_LOG("Establishing credentials"); 536 537 /* Get username */ 538 retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 539 if (retval != PAM_SUCCESS) 540 return (retval); 541 542 PAM_LOG("Got user: %s", user); 543 544 krbret = krb5_init_context(&pam_context); 545 if (krbret != 0) { 546 PAM_LOG("Error krb5_init_context() failed"); 547 return (PAM_SERVICE_ERR); 548 } 549 550 PAM_LOG("Context initialised"); 551 552 euid = geteuid(); /* Usually 0 */ 553 egid = getegid(); 554 555 PAM_LOG("Got euid, egid: %d %d", euid, egid); 556 557 /* Retrieve the temporary cache */ 558 retval = pam_get_data(pamh, "ccache", (const void **)&cache_name); 559 if (retval != PAM_SUCCESS) 560 goto cleanup3; 561 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_temp); 562 if (krbret != 0) { 563 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", cache_name, 564 krb5_get_err_text(pam_context, krbret)); 565 goto cleanup3; 566 } 567 568 /* Get the uid. This should exist. */ 569 pwd = getpwnam(user); 570 if (pwd == NULL) { 571 retval = PAM_USER_UNKNOWN; 572 goto cleanup3; 573 } 574 575 PAM_LOG("Done getpwnam()"); 576 577 /* Avoid following a symlink as root */ 578 if (setegid(pwd->pw_gid)) { 579 retval = PAM_SERVICE_ERR; 580 goto cleanup3; 581 } 582 if (seteuid(pwd->pw_uid)) { 583 retval = PAM_SERVICE_ERR; 584 goto cleanup3; 585 } 586 587 PAM_LOG("Done setegid() & seteuid()"); 588 589 /* Get the cache name */ 590 cache_name = NULL; 591 pam_test_option(&options, PAM_OPT_CCACHE, &cache_name); 592 if (cache_name == NULL) 593 asprintf(&cache_name, "FILE:/tmp/krb5cc_%d", pwd->pw_uid); 594 595 p = calloc(PATH_MAX + 16, sizeof(char)); 596 q = cache_name; 597 598 if (p == NULL) { 599 PAM_LOG("Error malloc(): failure"); 600 retval = PAM_BUF_ERR; 601 goto cleanup3; 602 } 603 cache_name = p; 604 605 /* convert %u and %p */ 606 while (*q) { 607 if (*q == '%') { 608 q++; 609 if (*q == 'u') { 610 sprintf(p, "%d", pwd->pw_uid); 611 p += strlen(p); 612 } 613 else if (*q == 'p') { 614 sprintf(p, "%d", getpid()); 615 p += strlen(p); 616 } 617 else { 618 /* Not a special token */ 619 *p++ = '%'; 620 q--; 621 } 622 q++; 623 } 624 else { 625 *p++ = *q++; 626 } 627 } 628 629 PAM_LOG("Got cache_name: %s", cache_name); 630 631 /* Initialize the new ccache */ 632 krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ); 633 if (krbret != 0) { 634 PAM_LOG("Error krb5_cc_get_principal(): %s", 635 krb5_get_err_text(pam_context, krbret)); 636 retval = PAM_SERVICE_ERR; 637 goto cleanup3; 638 } 639 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm); 640 if (krbret != 0) { 641 PAM_LOG("Error krb5_cc_resolve(): %s", 642 krb5_get_err_text(pam_context, krbret)); 643 retval = PAM_SERVICE_ERR; 644 goto cleanup2; 645 } 646 krbret = krb5_cc_initialize(pam_context, ccache_perm, princ); 647 if (krbret != 0) { 648 PAM_LOG("Error krb5_cc_initialize(): %s", 649 krb5_get_err_text(pam_context, krbret)); 650 retval = PAM_SERVICE_ERR; 651 goto cleanup2; 652 } 653 654 PAM_LOG("Cache initialised"); 655 656 /* Prepare for iteration over creds */ 657 krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor); 658 if (krbret != 0) { 659 PAM_LOG("Error krb5_cc_start_seq_get(): %s", 660 krb5_get_err_text(pam_context, krbret)); 661 krb5_cc_destroy(pam_context, ccache_perm); 662 retval = PAM_SERVICE_ERR; 663 goto cleanup2; 664 } 665 666 PAM_LOG("Prepared for iteration"); 667 668 /* Copy the creds (should be two of them) */ 669 while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp, 670 &cursor, &creds) == 0)) { 671 krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds); 672 if (krbret != 0) { 673 PAM_LOG("Error krb5_cc_store_cred(): %s", 674 krb5_get_err_text(pam_context, krbret)); 675 krb5_cc_destroy(pam_context, ccache_perm); 676 krb5_free_cred_contents(pam_context, &creds); 677 retval = PAM_SERVICE_ERR; 678 goto cleanup2; 679 } 680 krb5_free_cred_contents(pam_context, &creds); 681 PAM_LOG("Iteration"); 682 } 683 krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor); 684 685 PAM_LOG("Done iterating"); 686 687 if (strstr(cache_name, "FILE:") == cache_name) { 688 if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) { 689 PAM_LOG("Error chown(): %s", strerror(errno)); 690 krb5_cc_destroy(pam_context, ccache_perm); 691 retval = PAM_SERVICE_ERR; 692 goto cleanup2; 693 } 694 PAM_LOG("Done chown()"); 695 696 if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) { 697 PAM_LOG("Error chmod(): %s", strerror(errno)); 698 krb5_cc_destroy(pam_context, ccache_perm); 699 retval = PAM_SERVICE_ERR; 700 goto cleanup2; 701 } 702 PAM_LOG("Done chmod()"); 703 } 704 705 krb5_cc_close(pam_context, ccache_perm); 706 707 PAM_LOG("Cache closed"); 708 709 cache_env_name = malloc(strlen(cache_name) + 12); 710 if (!cache_env_name) { 711 PAM_LOG("Error malloc(): failure"); 712 krb5_cc_destroy(pam_context, ccache_perm); 713 retval = PAM_BUF_ERR; 714 goto cleanup2; 715 } 716 717 sprintf(cache_env_name, "KRB5CCNAME=%s", cache_name); 718 if ((retval = pam_putenv(pamh, cache_env_name)) != 0) { 719 PAM_LOG("Error pam_putenv(): %s", pam_strerror(pamh, retval)); 720 krb5_cc_destroy(pam_context, ccache_perm); 721 retval = PAM_SERVICE_ERR; 722 goto cleanup2; 723 } 724 725 PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name); 726 727cleanup2: 728 krb5_free_principal(pam_context, princ); 729 PAM_LOG("Done cleanup2"); 730cleanup3: 731 krb5_free_context(pam_context); 732 PAM_LOG("Done cleanup3"); 733 734 seteuid(euid); 735 setegid(egid); 736 737 PAM_LOG("Done seteuid() & setegid()"); 738 739 return (retval); 740} 741 742/* 743 * account management 744 */ 745PAM_EXTERN int 746pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused, 747 int argc, const char *argv[]) 748{ 749 krb5_error_code krbret; 750 krb5_context pam_context; 751 krb5_ccache ccache; 752 krb5_principal princ; 753 struct options options; 754 int retval; 755 const char *user, *ccache_name; 756 757 pam_std_option(&options, other_options, argc, argv); 758 759 PAM_LOG("Options processed"); 760 761 retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 762 if (retval != PAM_SUCCESS) 763 return (retval); 764 765 PAM_LOG("Got user: %s", user); 766 767 krbret = krb5_init_context(&pam_context); 768 if (krbret != 0) { 769 PAM_LOG("Error krb5_init_context() failed"); 770 return (PAM_PERM_DENIED); 771 } 772 773 PAM_LOG("Context initialised"); 774 775 retval = pam_get_data(pamh, "ccache", (const void **)&ccache_name); 776 if (retval != PAM_SUCCESS) 777 return (PAM_SUCCESS); 778 krbret = krb5_cc_resolve(pam_context, ccache_name, &ccache); 779 if (krbret != 0) { 780 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", ccache_name, 781 krb5_get_err_text(pam_context, krbret)); 782 krb5_free_context(pam_context); 783 return (PAM_PERM_DENIED); 784 } 785 786 PAM_LOG("Got ccache %s", ccache_name); 787 788 789 krbret = krb5_cc_get_principal(pam_context, ccache, &princ); 790 if (krbret != 0) { 791 PAM_LOG("Error krb5_cc_get_principal(): %s", 792 krb5_get_err_text(pam_context, krbret)); 793 retval = PAM_PERM_DENIED;; 794 goto cleanup; 795 } 796 797 PAM_LOG("Got principal"); 798 799 if (krb5_kuserok(pam_context, princ, user)) 800 retval = PAM_SUCCESS; 801 else 802 retval = PAM_PERM_DENIED; 803 krb5_free_principal(pam_context, princ); 804 805 PAM_LOG("Done kuserok()"); 806 807cleanup: 808 krb5_free_context(pam_context); 809 PAM_LOG("Done cleanup"); 810 811 return (retval); 812 813} 814 815/* 816 * password management 817 */ 818PAM_EXTERN int 819pam_sm_chauthtok(pam_handle_t *pamh, int flags, 820 int argc, const char *argv[]) 821{ 822 krb5_error_code krbret; 823 krb5_context pam_context; 824 krb5_creds creds; 825 krb5_principal princ; 826 krb5_get_init_creds_opt opts; 827 krb5_data result_code_string, result_string; 828 struct options options; 829 int result_code, retval; 830 const char *user, *pass; 831 char *princ_name, *passdup; 832 833 pam_std_option(&options, other_options, argc, argv); 834 835 PAM_LOG("Options processed"); 836 837 if (!(flags & PAM_UPDATE_AUTHTOK)) 838 return (PAM_AUTHTOK_ERR); 839 840 retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 841 if (retval != PAM_SUCCESS) 842 return (retval); 843 844 PAM_LOG("Got user: %s", user); 845 846 krbret = krb5_init_context(&pam_context); 847 if (krbret != 0) { 848 PAM_LOG("Error krb5_init_context() failed"); 849 return (PAM_SERVICE_ERR); 850 } 851 852 PAM_LOG("Context initialised"); 853 854 krb5_get_init_creds_opt_init(&opts); 855 856 PAM_LOG("Credentials options initialised"); 857 858 /* Get principal name */ 859 krbret = krb5_parse_name(pam_context, user, &princ); 860 if (krbret != 0) { 861 PAM_LOG("Error krb5_parse_name(): %s", 862 krb5_get_err_text(pam_context, krbret)); 863 retval = PAM_USER_UNKNOWN; 864 goto cleanup3; 865 } 866 867 /* Now convert the principal name into something human readable */ 868 princ_name = NULL; 869 krbret = krb5_unparse_name(pam_context, princ, &princ_name); 870 if (krbret != 0) { 871 PAM_LOG("Error krb5_unparse_name(): %s", 872 krb5_get_err_text(pam_context, krbret)); 873 retval = PAM_SERVICE_ERR; 874 goto cleanup2; 875 } 876 877 PAM_LOG("Got principal: %s", princ_name); 878 879 /* Get password */ 880 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, PASSWORD_PROMPT); 881 if (retval != PAM_SUCCESS) 882 goto cleanup2; 883 884 PAM_LOG("Got password"); 885 886 memset(&creds, 0, sizeof(krb5_creds)); 887 krbret = krb5_get_init_creds_password(pam_context, &creds, princ, 888 pass, NULL, pamh, 0, "kadmin/changepw", &opts); 889 if (krbret != 0) { 890 PAM_LOG("Error krb5_get_init_creds_password()", 891 krb5_get_err_text(pam_context, krbret)); 892 retval = PAM_AUTH_ERR; 893 goto cleanup2; 894 } 895 896 PAM_LOG("Credentials established"); 897 898 /* Now get the new password */ 899 for (;;) { 900 retval = pam_get_authtok(pamh, 901 PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT); 902 if (retval != PAM_TRY_AGAIN) 903 break; 904 pam_error(pamh, "Mismatch; try again, EOF to quit."); 905 } 906 if (retval != PAM_SUCCESS) 907 goto cleanup; 908 909 PAM_LOG("Got new password"); 910 911 /* Change it */ 912 if ((passdup = strdup(pass)) == NULL) { 913 retval = PAM_BUF_ERR; 914 goto cleanup; 915 } 916 krbret = krb5_change_password(pam_context, &creds, passdup, 917 &result_code, &result_code_string, &result_string); 918 free(passdup); 919 if (krbret != 0) { 920 PAM_LOG("Error krb5_change_password(): %s", 921 krb5_get_err_text(pam_context, krbret)); 922 retval = PAM_AUTHTOK_ERR; 923 goto cleanup; 924 } 925 if (result_code) { 926 PAM_LOG("Error krb5_change_password(): (result_code)"); 927 retval = PAM_AUTHTOK_ERR; 928 goto cleanup; 929 } 930 931 PAM_LOG("Password changed"); 932 933 if (result_string.data) 934 free(result_string.data); 935 if (result_code_string.data) 936 free(result_code_string.data); 937 938cleanup: 939 krb5_free_cred_contents(pam_context, &creds); 940 PAM_LOG("Done cleanup"); 941cleanup2: 942 krb5_free_principal(pam_context, princ); 943 PAM_LOG("Done cleanup2"); 944cleanup3: 945 if (princ_name) 946 free(princ_name); 947 948 krb5_free_context(pam_context); 949 950 PAM_LOG("Done cleanup3"); 951 952 return (retval); 953} 954 955PAM_MODULE_ENTRY("pam_krb5"); 956 957/* 958 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c 959 * Modified by Sam Hartman <hartmans@mit.edu> to support PAM services 960 * for Debian. 961 * 962 * Verify the Kerberos ticket-granting ticket just retrieved for the 963 * user. If the Kerberos server doesn't respond, assume the user is 964 * trying to fake us out (since we DID just get a TGT from what is 965 * supposedly our KDC). If the host/<host> service is unknown (i.e., 966 * the local keytab doesn't have it), and we cannot find another 967 * service we do have, let her in. 968 * 969 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty. 970 */ 971static int 972verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache, 973 char *pam_service, int debug) 974{ 975 krb5_error_code retval; 976 krb5_principal princ; 977 krb5_keyblock *keyblock; 978 krb5_data packet; 979 krb5_auth_context auth_context; 980 char phost[BUFSIZ]; 981 const char *services[3], **service; 982 983 packet.data = 0; 984 985 /* If possible we want to try and verify the ticket we have 986 * received against a keytab. We will try multiple service 987 * principals, including at least the host principal and the PAM 988 * service principal. The host principal is preferred because access 989 * to that key is generally sufficient to compromise root, while the 990 * service key for this PAM service may be less carefully guarded. 991 * It is important to check the keytab first before the KDC so we do 992 * not get spoofed by a fake KDC. 993 */ 994 services[0] = "host"; 995 services[1] = pam_service; 996 services[2] = NULL; 997 keyblock = 0; 998 retval = -1; 999 for (service = &services[0]; *service != NULL; service++) { 1000 retval = krb5_sname_to_principal(context, NULL, *service, 1001 KRB5_NT_SRV_HST, &princ); 1002 if (retval != 0) { 1003 if (debug) 1004 syslog(LOG_DEBUG, 1005 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 1006 "krb5_sname_to_principal()", 1007 krb5_get_err_text(context, retval)); 1008 return -1; 1009 } 1010 1011 /* Extract the name directly. */ 1012 strncpy(phost, compat_princ_component(context, princ, 1), 1013 BUFSIZ); 1014 phost[BUFSIZ - 1] = '\0'; 1015 1016 /* 1017 * Do we have service/<host> keys? 1018 * (use default/configured keytab, kvno IGNORE_VNO to get the 1019 * first match, and ignore enctype.) 1020 */ 1021 retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0, 1022 &keyblock); 1023 if (retval != 0) 1024 continue; 1025 break; 1026 } 1027 if (retval != 0) { /* failed to find key */ 1028 /* Keytab or service key does not exist */ 1029 if (debug) 1030 syslog(LOG_DEBUG, 1031 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 1032 "krb5_kt_read_service_key()", 1033 krb5_get_err_text(context, retval)); 1034 retval = 0; 1035 goto cleanup; 1036 } 1037 if (keyblock) 1038 krb5_free_keyblock(context, keyblock); 1039 1040 /* Talk to the kdc and construct the ticket. */ 1041 auth_context = NULL; 1042 retval = krb5_mk_req(context, &auth_context, 0, *service, phost, 1043 NULL, ccache, &packet); 1044 if (auth_context) { 1045 krb5_auth_con_free(context, auth_context); 1046 auth_context = NULL; /* setup for rd_req */ 1047 } 1048 if (retval) { 1049 if (debug) 1050 syslog(LOG_DEBUG, 1051 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 1052 "krb5_mk_req()", 1053 krb5_get_err_text(context, retval)); 1054 retval = -1; 1055 goto cleanup; 1056 } 1057 1058 /* Try to use the ticket. */ 1059 retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL, 1060 NULL, NULL); 1061 if (retval) { 1062 if (debug) 1063 syslog(LOG_DEBUG, 1064 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 1065 "krb5_rd_req()", 1066 krb5_get_err_text(context, retval)); 1067 retval = -1; 1068 } 1069 else 1070 retval = 1; 1071 1072cleanup: 1073 if (packet.data) 1074 compat_free_data_contents(context, &packet); 1075 krb5_free_principal(context, princ); 1076 return retval; 1077} 1078 1079/* Free the memory for cache_name. Called by pam_end() */ 1080static void 1081cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused) 1082{ 1083 krb5_context pam_context; 1084 krb5_ccache ccache; 1085 krb5_error_code krbret; 1086 1087 if (krb5_init_context(&pam_context)) 1088 return; 1089 1090 krbret = krb5_cc_resolve(pam_context, data, &ccache); 1091 if (krbret == 0) 1092 krb5_cc_destroy(pam_context, ccache); 1093 krb5_free_context(pam_context); 1094 free(data); 1095} 1096 1097#ifdef COMPAT_HEIMDAL 1098#ifdef COMPAT_MIT 1099#error This cannot be MIT and Heimdal compatible! 1100#endif 1101#endif 1102 1103#ifndef COMPAT_HEIMDAL 1104#ifndef COMPAT_MIT 1105#error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified! 1106#endif 1107#endif 1108 1109#ifdef COMPAT_HEIMDAL 1110static const char * 1111compat_princ_component(krb5_context context __unused, krb5_principal princ, int n) 1112{ 1113 return princ->name.name_string.val[n]; 1114} 1115 1116static void 1117compat_free_data_contents(krb5_context context __unused, krb5_data * data) 1118{ 1119 krb5_xfree(data->data); 1120} 1121#endif 1122 1123#ifdef COMPAT_MIT 1124static const char * 1125compat_princ_component(krb5_context context, krb5_principal princ, int n) 1126{ 1127 return krb5_princ_component(context, princ, n)->data; 1128} 1129 1130static void 1131compat_free_data_contents(krb5_context context, krb5_data * data) 1132{ 1133 krb5_free_data_contents(context, data); 1134} 1135#endif 1136