pam_krb5.c revision 106862
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 106862 2002-11-13 17:44:29Z 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, ccache_check; 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, *cache_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 unique cache_name */ 407 asprintf(&cache_name, "MEMORY:/tmp/%s.%d", service, getpid()); 408 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache); 409 free(cache_name); 410 if (krbret != 0) { 411 PAM_VERBOSE_ERROR("Kerberos 5 error"); 412 PAM_LOG("Error krb5_cc_resolve(): %s", 413 krb5_get_err_text(pam_context, krbret)); 414 retval = PAM_SERVICE_ERR; 415 goto cleanup; 416 } 417 krbret = krb5_cc_initialize(pam_context, ccache, princ); 418 if (krbret != 0) { 419 PAM_VERBOSE_ERROR("Kerberos 5 error"); 420 PAM_LOG("Error krb5_cc_initialize(): %s", 421 krb5_get_err_text(pam_context, krbret)); 422 retval = PAM_SERVICE_ERR; 423 goto cleanup; 424 } 425 krbret = krb5_cc_store_cred(pam_context, ccache, &creds); 426 if (krbret != 0) { 427 PAM_VERBOSE_ERROR("Kerberos 5 error"); 428 PAM_LOG("Error krb5_cc_store_cred(): %s", 429 krb5_get_err_text(pam_context, krbret)); 430 krb5_cc_destroy(pam_context, ccache); 431 retval = PAM_SERVICE_ERR; 432 goto cleanup; 433 } 434 435 PAM_LOG("Credentials stashed"); 436 437 /* Verify them */ 438 if ((srvdup = strdup(service)) == NULL) { 439 retval = PAM_BUF_ERR; 440 goto cleanup; 441 } 442 krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup, 443 pam_test_option(&options, PAM_OPT_FORWARDABLE, NULL)); 444 free(srvdup); 445 if (krbret == -1) { 446 PAM_VERBOSE_ERROR("Kerberos 5 error"); 447 krb5_cc_destroy(pam_context, ccache); 448 retval = PAM_AUTH_ERR; 449 goto cleanup; 450 } 451 452 PAM_LOG("Credentials stash verified"); 453 454 retval = pam_get_data(pamh, "ccache", (const void **)&ccache_check); 455 if (retval == PAM_SUCCESS) { 456 krb5_cc_destroy(pam_context, ccache); 457 PAM_VERBOSE_ERROR("Kerberos 5 error"); 458 retval = PAM_AUTH_ERR; 459 goto cleanup; 460 } 461 462 PAM_LOG("Credentials stash not pre-existing"); 463 464 retval = pam_set_data(pamh, "ccache", ccache, cleanup_cache); 465 if (retval != 0) { 466 krb5_cc_destroy(pam_context, ccache); 467 PAM_VERBOSE_ERROR("Kerberos 5 error"); 468 retval = PAM_SERVICE_ERR; 469 goto cleanup; 470 } 471 472 PAM_LOG("Credentials stash saved"); 473 474cleanup: 475 krb5_free_cred_contents(pam_context, &creds); 476 PAM_LOG("Done cleanup"); 477cleanup2: 478 krb5_free_principal(pam_context, princ); 479 PAM_LOG("Done cleanup2"); 480cleanup3: 481 if (princ_name) 482 free(princ_name); 483 484 krb5_free_context(pam_context); 485 486 PAM_LOG("Done cleanup3"); 487 488 if (retval != PAM_SUCCESS) 489 PAM_VERBOSE_ERROR("Kerberos 5 refuses you"); 490 491 return (retval); 492} 493 494PAM_EXTERN int 495pam_sm_setcred(pam_handle_t *pamh, int flags, 496 int argc, const char *argv[]) 497{ 498 499 krb5_error_code krbret; 500 krb5_context pam_context; 501 krb5_principal princ; 502 krb5_creds creds; 503 krb5_ccache ccache_temp, ccache_perm; 504 krb5_cc_cursor cursor; 505 struct options options; 506 struct passwd *pwd = NULL; 507 int retval; 508 char *user; 509 char *cache_name, *cache_env_name, *p, *q; 510 511 uid_t euid; 512 gid_t egid; 513 514 pam_std_option(&options, other_options, argc, argv); 515 516 PAM_LOG("Options processed"); 517 518 if (flags & PAM_DELETE_CRED) 519 return (PAM_SUCCESS); 520 521 if (flags & PAM_REFRESH_CRED) 522 return (PAM_SUCCESS); 523 524 if (flags & PAM_REINITIALIZE_CRED) 525 return (PAM_SUCCESS); 526 527 if (!(flags & PAM_ESTABLISH_CRED)) 528 return (PAM_SERVICE_ERR); 529 530 PAM_LOG("Establishing credentials"); 531 532 /* Get username */ 533 retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 534 if (retval != PAM_SUCCESS) 535 return (retval); 536 537 PAM_LOG("Got user: %s", user); 538 539 krbret = krb5_init_context(&pam_context); 540 if (krbret != 0) { 541 PAM_LOG("Error krb5_init_context() failed"); 542 return (PAM_SERVICE_ERR); 543 } 544 545 PAM_LOG("Context initialised"); 546 547 euid = geteuid(); /* Usually 0 */ 548 egid = getegid(); 549 550 PAM_LOG("Got euid, egid: %d %d", euid, egid); 551 552 /* Retrieve the cache name */ 553 retval = pam_get_data(pamh, "ccache", (const void **)&ccache_temp); 554 if (retval != PAM_SUCCESS) 555 goto cleanup3; 556 557 /* Get the uid. This should exist. */ 558 pwd = getpwnam(user); 559 if (pwd == NULL) { 560 retval = PAM_USER_UNKNOWN; 561 goto cleanup3; 562 } 563 564 PAM_LOG("Done getpwnam()"); 565 566 /* Avoid following a symlink as root */ 567 if (setegid(pwd->pw_gid)) { 568 retval = PAM_SERVICE_ERR; 569 goto cleanup3; 570 } 571 if (seteuid(pwd->pw_uid)) { 572 retval = PAM_SERVICE_ERR; 573 goto cleanup3; 574 } 575 576 PAM_LOG("Done setegid() & seteuid()"); 577 578 /* Get the cache name */ 579 cache_name = NULL; 580 pam_test_option(&options, PAM_OPT_CCACHE, &cache_name); 581 if (cache_name == NULL) 582 asprintf(&cache_name, "FILE:/tmp/krb5cc_%d", pwd->pw_uid); 583 584 p = calloc(PATH_MAX + 16, sizeof(char)); 585 q = cache_name; 586 587 if (p == NULL) { 588 PAM_LOG("Error malloc(): failure"); 589 retval = PAM_BUF_ERR; 590 goto cleanup3; 591 } 592 cache_name = p; 593 594 /* convert %u and %p */ 595 while (*q) { 596 if (*q == '%') { 597 q++; 598 if (*q == 'u') { 599 sprintf(p, "%d", pwd->pw_uid); 600 p += strlen(p); 601 } 602 else if (*q == 'p') { 603 sprintf(p, "%d", getpid()); 604 p += strlen(p); 605 } 606 else { 607 /* Not a special token */ 608 *p++ = '%'; 609 q--; 610 } 611 q++; 612 } 613 else { 614 *p++ = *q++; 615 } 616 } 617 618 PAM_LOG("Got cache_name: %s", cache_name); 619 620 /* Initialize the new ccache */ 621 krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ); 622 if (krbret != 0) { 623 PAM_LOG("Error krb5_cc_get_principal(): %s", 624 krb5_get_err_text(pam_context, krbret)); 625 retval = PAM_SERVICE_ERR; 626 goto cleanup3; 627 } 628 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm); 629 if (krbret != 0) { 630 PAM_LOG("Error krb5_cc_resolve(): %s", 631 krb5_get_err_text(pam_context, krbret)); 632 retval = PAM_SERVICE_ERR; 633 goto cleanup2; 634 } 635 krbret = krb5_cc_initialize(pam_context, ccache_perm, princ); 636 if (krbret != 0) { 637 PAM_LOG("Error krb5_cc_initialize(): %s", 638 krb5_get_err_text(pam_context, krbret)); 639 retval = PAM_SERVICE_ERR; 640 goto cleanup2; 641 } 642 643 PAM_LOG("Cache initialised"); 644 645 /* Prepare for iteration over creds */ 646 krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor); 647 if (krbret != 0) { 648 PAM_LOG("Error krb5_cc_start_seq_get(): %s", 649 krb5_get_err_text(pam_context, krbret)); 650 krb5_cc_destroy(pam_context, ccache_perm); 651 retval = PAM_SERVICE_ERR; 652 goto cleanup2; 653 } 654 655 PAM_LOG("Prepared for iteration"); 656 657 /* Copy the creds (should be two of them) */ 658 while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp, 659 &cursor, &creds) == 0)) { 660 krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds); 661 if (krbret != 0) { 662 PAM_LOG("Error krb5_cc_store_cred(): %s", 663 krb5_get_err_text(pam_context, krbret)); 664 krb5_cc_destroy(pam_context, ccache_perm); 665 krb5_free_cred_contents(pam_context, &creds); 666 retval = PAM_SERVICE_ERR; 667 goto cleanup2; 668 } 669 krb5_free_cred_contents(pam_context, &creds); 670 PAM_LOG("Iteration"); 671 } 672 krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor); 673 674 PAM_LOG("Done iterating"); 675 676 if (strstr(cache_name, "FILE:") == cache_name) { 677 if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) { 678 PAM_LOG("Error chown(): %s", strerror(errno)); 679 krb5_cc_destroy(pam_context, ccache_perm); 680 retval = PAM_SERVICE_ERR; 681 goto cleanup2; 682 } 683 PAM_LOG("Done chown()"); 684 685 if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) { 686 PAM_LOG("Error chmod(): %s", strerror(errno)); 687 krb5_cc_destroy(pam_context, ccache_perm); 688 retval = PAM_SERVICE_ERR; 689 goto cleanup2; 690 } 691 PAM_LOG("Done chmod()"); 692 } 693 694 krb5_cc_close(pam_context, ccache_perm); 695 696 PAM_LOG("Cache closed"); 697 698 cache_env_name = malloc(strlen(cache_name) + 12); 699 if (!cache_env_name) { 700 PAM_LOG("Error malloc(): failure"); 701 krb5_cc_destroy(pam_context, ccache_perm); 702 retval = PAM_BUF_ERR; 703 goto cleanup2; 704 } 705 706 sprintf(cache_env_name, "KRB5CCNAME=%s", cache_name); 707 if ((retval = pam_putenv(pamh, cache_env_name)) != 0) { 708 PAM_LOG("Error pam_putenv(): %s", pam_strerror(pamh, retval)); 709 krb5_cc_destroy(pam_context, ccache_perm); 710 retval = PAM_SERVICE_ERR; 711 goto cleanup2; 712 } 713 714 PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name); 715 716cleanup2: 717 krb5_free_principal(pam_context, princ); 718 PAM_LOG("Done cleanup2"); 719cleanup3: 720 krb5_free_context(pam_context); 721 PAM_LOG("Done cleanup3"); 722 723 seteuid(euid); 724 setegid(egid); 725 726 PAM_LOG("Done seteuid() & setegid()"); 727 728 return (retval); 729} 730 731/* 732 * account management 733 */ 734PAM_EXTERN int 735pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused, 736 int argc, const char *argv[]) 737{ 738 krb5_error_code krbret; 739 krb5_context pam_context; 740 krb5_ccache ccache; 741 krb5_principal princ; 742 struct options options; 743 int retval; 744 const char *user; 745 746 pam_std_option(&options, other_options, argc, argv); 747 748 PAM_LOG("Options processed"); 749 750 retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 751 if (retval != PAM_SUCCESS) 752 return (retval); 753 754 PAM_LOG("Got user: %s", user); 755 756 retval = pam_get_data(pamh, "ccache", (const void **)&ccache); 757 if (retval != PAM_SUCCESS) 758 return (PAM_SUCCESS); 759 760 PAM_LOG("Got ccache"); 761 762 krbret = krb5_init_context(&pam_context); 763 if (krbret != 0) { 764 PAM_LOG("Error krb5_init_context() failed"); 765 return (PAM_PERM_DENIED); 766 } 767 768 PAM_LOG("Context initialised"); 769 770 krbret = krb5_cc_get_principal(pam_context, ccache, &princ); 771 if (krbret != 0) { 772 PAM_LOG("Error krb5_cc_get_principal(): %s", 773 krb5_get_err_text(pam_context, krbret)); 774 retval = PAM_PERM_DENIED;; 775 goto cleanup; 776 } 777 778 PAM_LOG("Got principal"); 779 780 if (krb5_kuserok(pam_context, princ, user)) 781 retval = PAM_SUCCESS; 782 else 783 retval = PAM_PERM_DENIED; 784 krb5_free_principal(pam_context, princ); 785 786 PAM_LOG("Done kuserok()"); 787 788cleanup: 789 krb5_free_context(pam_context); 790 PAM_LOG("Done cleanup"); 791 792 return (retval); 793 794} 795 796/* 797 * password management 798 */ 799PAM_EXTERN int 800pam_sm_chauthtok(pam_handle_t *pamh, int flags, 801 int argc, const char *argv[]) 802{ 803 krb5_error_code krbret; 804 krb5_context pam_context; 805 krb5_creds creds; 806 krb5_principal princ; 807 krb5_get_init_creds_opt opts; 808 krb5_data result_code_string, result_string; 809 struct options options; 810 int result_code, retval; 811 const char *user, *pass; 812 char *princ_name, *passdup; 813 814 pam_std_option(&options, other_options, argc, argv); 815 816 PAM_LOG("Options processed"); 817 818 if (!(flags & PAM_UPDATE_AUTHTOK)) 819 return (PAM_AUTHTOK_ERR); 820 821 retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 822 if (retval != PAM_SUCCESS) 823 return (retval); 824 825 PAM_LOG("Got user: %s", user); 826 827 krbret = krb5_init_context(&pam_context); 828 if (krbret != 0) { 829 PAM_LOG("Error krb5_init_context() failed"); 830 return (PAM_SERVICE_ERR); 831 } 832 833 PAM_LOG("Context initialised"); 834 835 krb5_get_init_creds_opt_init(&opts); 836 837 PAM_LOG("Credentials options initialised"); 838 839 /* Get principal name */ 840 krbret = krb5_parse_name(pam_context, user, &princ); 841 if (krbret != 0) { 842 PAM_LOG("Error krb5_parse_name(): %s", 843 krb5_get_err_text(pam_context, krbret)); 844 retval = PAM_USER_UNKNOWN; 845 goto cleanup3; 846 } 847 848 /* Now convert the principal name into something human readable */ 849 princ_name = NULL; 850 krbret = krb5_unparse_name(pam_context, princ, &princ_name); 851 if (krbret != 0) { 852 PAM_LOG("Error krb5_unparse_name(): %s", 853 krb5_get_err_text(pam_context, krbret)); 854 retval = PAM_SERVICE_ERR; 855 goto cleanup2; 856 } 857 858 PAM_LOG("Got principal: %s", princ_name); 859 860 /* Get password */ 861 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, PASSWORD_PROMPT); 862 if (retval != PAM_SUCCESS) 863 goto cleanup2; 864 865 PAM_LOG("Got password"); 866 867 memset(&creds, 0, sizeof(krb5_creds)); 868 krbret = krb5_get_init_creds_password(pam_context, &creds, princ, 869 pass, NULL, pamh, 0, "kadmin/changepw", &opts); 870 if (krbret != 0) { 871 PAM_LOG("Error krb5_get_init_creds_password()", 872 krb5_get_err_text(pam_context, krbret)); 873 retval = PAM_AUTH_ERR; 874 goto cleanup2; 875 } 876 877 PAM_LOG("Credentials established"); 878 879 /* Now get the new password */ 880 for (;;) { 881 retval = pam_get_authtok(pamh, 882 PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT); 883 if (retval != PAM_TRY_AGAIN) 884 break; 885 pam_error(pamh, "Mismatch; try again, EOF to quit."); 886 } 887 if (retval != PAM_SUCCESS) 888 goto cleanup; 889 890 PAM_LOG("Got new password"); 891 892 /* Change it */ 893 if ((passdup = strdup(pass)) == NULL) { 894 retval = PAM_BUF_ERR; 895 goto cleanup; 896 } 897 krbret = krb5_change_password(pam_context, &creds, passdup, 898 &result_code, &result_code_string, &result_string); 899 free(passdup); 900 if (krbret != 0) { 901 PAM_LOG("Error krb5_change_password(): %s", 902 krb5_get_err_text(pam_context, krbret)); 903 retval = PAM_AUTHTOK_ERR; 904 goto cleanup; 905 } 906 if (result_code) { 907 PAM_LOG("Error krb5_change_password(): (result_code)"); 908 retval = PAM_AUTHTOK_ERR; 909 goto cleanup; 910 } 911 912 PAM_LOG("Password changed"); 913 914 if (result_string.data) 915 free(result_string.data); 916 if (result_code_string.data) 917 free(result_code_string.data); 918 919cleanup: 920 krb5_free_cred_contents(pam_context, &creds); 921 PAM_LOG("Done cleanup"); 922cleanup2: 923 krb5_free_principal(pam_context, princ); 924 PAM_LOG("Done cleanup2"); 925cleanup3: 926 if (princ_name) 927 free(princ_name); 928 929 krb5_free_context(pam_context); 930 931 PAM_LOG("Done cleanup3"); 932 933 return (retval); 934} 935 936PAM_MODULE_ENTRY("pam_krb5"); 937 938/* 939 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c 940 * Modified by Sam Hartman <hartmans@mit.edu> to support PAM services 941 * for Debian. 942 * 943 * Verify the Kerberos ticket-granting ticket just retrieved for the 944 * user. If the Kerberos server doesn't respond, assume the user is 945 * trying to fake us out (since we DID just get a TGT from what is 946 * supposedly our KDC). If the host/<host> service is unknown (i.e., 947 * the local keytab doesn't have it), and we cannot find another 948 * service we do have, let her in. 949 * 950 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty. 951 */ 952static int 953verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache, 954 char *pam_service, int debug) 955{ 956 krb5_error_code retval; 957 krb5_principal princ; 958 krb5_keyblock *keyblock; 959 krb5_data packet; 960 krb5_auth_context auth_context; 961 char phost[BUFSIZ]; 962 const char *services[3], **service; 963 964 packet.data = 0; 965 966 /* If possible we want to try and verify the ticket we have 967 * received against a keytab. We will try multiple service 968 * principals, including at least the host principal and the PAM 969 * service principal. The host principal is preferred because access 970 * to that key is generally sufficient to compromise root, while the 971 * service key for this PAM service may be less carefully guarded. 972 * It is important to check the keytab first before the KDC so we do 973 * not get spoofed by a fake KDC. 974 */ 975 services[0] = "host"; 976 services[1] = pam_service; 977 services[2] = NULL; 978 keyblock = 0; 979 retval = -1; 980 for (service = &services[0]; *service != NULL; service++) { 981 retval = krb5_sname_to_principal(context, NULL, *service, 982 KRB5_NT_SRV_HST, &princ); 983 if (retval != 0) { 984 if (debug) 985 syslog(LOG_DEBUG, 986 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 987 "krb5_sname_to_principal()", 988 krb5_get_err_text(context, retval)); 989 return -1; 990 } 991 992 /* Extract the name directly. */ 993 strncpy(phost, compat_princ_component(context, princ, 1), 994 BUFSIZ); 995 phost[BUFSIZ - 1] = '\0'; 996 997 /* 998 * Do we have service/<host> keys? 999 * (use default/configured keytab, kvno IGNORE_VNO to get the 1000 * first match, and ignore enctype.) 1001 */ 1002 retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0, 1003 &keyblock); 1004 if (retval != 0) 1005 continue; 1006 break; 1007 } 1008 if (retval != 0) { /* failed to find key */ 1009 /* Keytab or service key does not exist */ 1010 if (debug) 1011 syslog(LOG_DEBUG, 1012 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 1013 "krb5_kt_read_service_key()", 1014 krb5_get_err_text(context, retval)); 1015 retval = 0; 1016 goto cleanup; 1017 } 1018 if (keyblock) 1019 krb5_free_keyblock(context, keyblock); 1020 1021 /* Talk to the kdc and construct the ticket. */ 1022 auth_context = NULL; 1023 retval = krb5_mk_req(context, &auth_context, 0, *service, phost, 1024 NULL, ccache, &packet); 1025 if (auth_context) { 1026 krb5_auth_con_free(context, auth_context); 1027 auth_context = NULL; /* setup for rd_req */ 1028 } 1029 if (retval) { 1030 if (debug) 1031 syslog(LOG_DEBUG, 1032 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 1033 "krb5_mk_req()", 1034 krb5_get_err_text(context, retval)); 1035 retval = -1; 1036 goto cleanup; 1037 } 1038 1039 /* Try to use the ticket. */ 1040 retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL, 1041 NULL, NULL); 1042 if (retval) { 1043 if (debug) 1044 syslog(LOG_DEBUG, 1045 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 1046 "krb5_rd_req()", 1047 krb5_get_err_text(context, retval)); 1048 retval = -1; 1049 } 1050 else 1051 retval = 1; 1052 1053cleanup: 1054 if (packet.data) 1055 compat_free_data_contents(context, &packet); 1056 krb5_free_principal(context, princ); 1057 return retval; 1058} 1059 1060/* Free the memory for cache_name. Called by pam_end() */ 1061static void 1062cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused) 1063{ 1064 krb5_context pam_context; 1065 krb5_ccache ccache; 1066 1067 if (krb5_init_context(&pam_context)) 1068 return; 1069 1070 ccache = (krb5_ccache)data; 1071 krb5_cc_destroy(pam_context, ccache); 1072 krb5_free_context(pam_context); 1073} 1074 1075#ifdef COMPAT_HEIMDAL 1076#ifdef COMPAT_MIT 1077#error This cannot be MIT and Heimdal compatible! 1078#endif 1079#endif 1080 1081#ifndef COMPAT_HEIMDAL 1082#ifndef COMPAT_MIT 1083#error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified! 1084#endif 1085#endif 1086 1087#ifdef COMPAT_HEIMDAL 1088static const char * 1089compat_princ_component(krb5_context context __unused, krb5_principal princ, int n) 1090{ 1091 return princ->name.name_string.val[n]; 1092} 1093 1094static void 1095compat_free_data_contents(krb5_context context __unused, krb5_data * data) 1096{ 1097 krb5_xfree(data->data); 1098} 1099#endif 1100 1101#ifdef COMPAT_MIT 1102static const char * 1103compat_princ_component(krb5_context context, krb5_principal princ, int n) 1104{ 1105 return krb5_princ_component(context, princ, n)->data; 1106} 1107 1108static void 1109compat_free_data_contents(krb5_context context, krb5_data * data) 1110{ 1111 krb5_free_data_contents(context, data); 1112} 1113#endif 1114