pam_krb5.c revision 115465
1181834Sroberto/*- 2181834Sroberto * This pam_krb5 module contains code that is: 3181834Sroberto * Copyright (c) Derrick J. Brashear, 1996. All rights reserved. 4181834Sroberto * Copyright (c) Frank Cusack, 1999-2001. All rights reserved. 5181834Sroberto * Copyright (c) Jacques A. Vidrine, 2000-2001. All rights reserved. 6181834Sroberto * Copyright (c) Nicolas Williams, 2001. All rights reserved. 7181834Sroberto * Copyright (c) Perot Systems Corporation, 2001. All rights reserved. 8181834Sroberto * Copyright (c) Mark R V Murray, 2001. All rights reserved. 9181834Sroberto * Copyright (c) Networks Associates Technology, Inc., 2002. 10181834Sroberto * All rights reserved. 11181834Sroberto * 12181834Sroberto * Portions of this software were developed for the FreeBSD Project by 13181834Sroberto * ThinkSec AS and NAI Labs, the Security Research Division of Network 14181834Sroberto * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 15181834Sroberto * ("CBOSS"), as part of the DARPA CHATS research program. 16181834Sroberto * 17181834Sroberto * Redistribution and use in source and binary forms, with or without 18181834Sroberto * modification, are permitted provided that the following conditions 19181834Sroberto * are met: 20181834Sroberto * 1. Redistributions of source code must retain the above copyright 21181834Sroberto * notices, and the entire permission notice in its entirety, 22181834Sroberto * including the disclaimer of warranties. 23181834Sroberto * 2. Redistributions in binary form must reproduce the above copyright 24181834Sroberto * notice, this list of conditions and the following disclaimer in the 25181834Sroberto * documentation and/or other materials provided with the distribution. 26181834Sroberto * 3. The name of the author may not be used to endorse or promote 27181834Sroberto * products derived from this software without specific prior 28181834Sroberto * written permission. 29181834Sroberto * 30181834Sroberto * ALTERNATIVELY, this product may be distributed under the terms of 31181834Sroberto * the GNU Public License, in which case the provisions of the GPL are 32181834Sroberto * required INSTEAD OF the above restrictions. (This clause is 33181834Sroberto * necessary due to a potential bad interaction between the GPL and 34181834Sroberto * the restrictions contained in a BSD-style copyright.) 35181834Sroberto * 36181834Sroberto * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 37181834Sroberto * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 38181834Sroberto * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 39181834Sroberto * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 40181834Sroberto * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 41181834Sroberto * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 42181834Sroberto * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43181834Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44181834Sroberto * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45181834Sroberto * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46181834Sroberto * OF THE POSSIBILITY OF SUCH DAMAGE. 47181834Sroberto * 48181834Sroberto */ 49181834Sroberto 50181834Sroberto#include <sys/cdefs.h> 51181834Sroberto__FBSDID("$FreeBSD: head/lib/libpam/modules/pam_krb5/pam_krb5.c 115465 2003-05-31 16:55:07Z des $"); 52181834Sroberto 53181834Sroberto#include <sys/types.h> 54181834Sroberto#include <sys/stat.h> 55181834Sroberto#include <errno.h> 56181834Sroberto#include <limits.h> 57181834Sroberto#include <pwd.h> 58181834Sroberto#include <stdio.h> 59181834Sroberto#include <stdlib.h> 60181834Sroberto#include <string.h> 61181834Sroberto#include <syslog.h> 62181834Sroberto#include <unistd.h> 63181834Sroberto 64181834Sroberto#include <krb5.h> 65181834Sroberto#include <com_err.h> 66181834Sroberto 67181834Sroberto#define PAM_SM_AUTH 68181834Sroberto#define PAM_SM_ACCOUNT 69181834Sroberto#define PAM_SM_PASSWORD 70181834Sroberto 71181834Sroberto#include <security/pam_appl.h> 72181834Sroberto#include <security/pam_modules.h> 73181834Sroberto#include <security/pam_mod_misc.h> 74181834Sroberto#include <security/openpam.h> 75181834Sroberto 76181834Sroberto#define COMPAT_HEIMDAL 77181834Sroberto/* #define COMPAT_MIT */ 78181834Sroberto 79181834Srobertostatic int verify_krb_v5_tgt(krb5_context, krb5_ccache, char *, int); 80181834Srobertostatic void cleanup_cache(pam_handle_t *, void *, int); 81181834Srobertostatic const char *compat_princ_component(krb5_context, krb5_principal, int); 82181834Srobertostatic void compat_free_data_contents(krb5_context, krb5_data *); 83181834Sroberto 84181834Sroberto#define USER_PROMPT "Username: " 85181834Sroberto#define PASSWORD_PROMPT "Password:" 86181834Sroberto#define NEW_PASSWORD_PROMPT "New Password:" 87181834Sroberto 88181834Sroberto#define PAM_OPT_CCACHE "ccache" 89181834Sroberto#define PAM_OPT_FORWARDABLE "forwardable" 90181834Sroberto#define PAM_OPT_NO_CCACHE "no_ccache" 91181834Sroberto#define PAM_OPT_REUSE_CCACHE "reuse_ccache" 92181834Sroberto 93181834Sroberto/* 94181834Sroberto * authentication management 95181834Sroberto */ 96181834SrobertoPAM_EXTERN int 97181834Srobertopam_sm_authenticate(pam_handle_t *pamh, int flags __unused, 98181834Sroberto int argc __unused, const char *argv[] __unused) 99181834Sroberto{ 100181834Sroberto krb5_error_code krbret; 101181834Sroberto krb5_context pam_context; 102181834Sroberto krb5_creds creds; 103181834Sroberto krb5_principal princ; 104181834Sroberto krb5_ccache ccache; 105181834Sroberto krb5_get_init_creds_opt opts; 106181834Sroberto struct passwd *pwd; 107181834Sroberto int retval; 108181834Sroberto const char *sourceuser, *user, *pass, *service; 109181834Sroberto char *principal, *princ_name, *ccache_name, luser[32], *srvdup; 110181834Sroberto 111181834Sroberto retval = pam_get_user(pamh, &user, USER_PROMPT); 112181834Sroberto if (retval != PAM_SUCCESS) 113181834Sroberto return (retval); 114181834Sroberto 115181834Sroberto PAM_LOG("Got user: %s", user); 116181834Sroberto 117181834Sroberto retval = pam_get_item(pamh, PAM_RUSER, (const void **)&sourceuser); 118181834Sroberto if (retval != PAM_SUCCESS) 119181834Sroberto return (retval); 120181834Sroberto 121181834Sroberto PAM_LOG("Got ruser: %s", sourceuser); 122181834Sroberto 123181834Sroberto service = NULL; 124181834Sroberto pam_get_item(pamh, PAM_SERVICE, (const void **)&service); 125181834Sroberto if (service == NULL) 126181834Sroberto service = "unknown"; 127181834Sroberto 128181834Sroberto PAM_LOG("Got service: %s", service); 129181834Sroberto 130181834Sroberto krbret = krb5_init_context(&pam_context); 131181834Sroberto if (krbret != 0) { 132181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 133181834Sroberto return (PAM_SERVICE_ERR); 134181834Sroberto } 135181834Sroberto 136181834Sroberto PAM_LOG("Context initialised"); 137181834Sroberto 138181834Sroberto krb5_get_init_creds_opt_init(&opts); 139181834Sroberto 140181834Sroberto if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE)) 141181834Sroberto krb5_get_init_creds_opt_set_forwardable(&opts, 1); 142181834Sroberto 143181834Sroberto PAM_LOG("Credentials initialised"); 144181834Sroberto 145181834Sroberto krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE); 146181834Sroberto if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) { 147181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 148181834Sroberto retval = PAM_SERVICE_ERR; 149181834Sroberto goto cleanup3; 150181834Sroberto } 151181834Sroberto 152181834Sroberto PAM_LOG("Done krb5_cc_register()"); 153181834Sroberto 154181834Sroberto /* Get principal name */ 155181834Sroberto if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) 156181834Sroberto asprintf(&principal, "%s/%s", sourceuser, user); 157181834Sroberto else 158181834Sroberto principal = strdup(user); 159181834Sroberto 160181834Sroberto PAM_LOG("Created principal: %s", principal); 161181834Sroberto 162181834Sroberto krbret = krb5_parse_name(pam_context, principal, &princ); 163181834Sroberto free(principal); 164181834Sroberto if (krbret != 0) { 165181834Sroberto PAM_LOG("Error krb5_parse_name(): %s", 166181834Sroberto krb5_get_err_text(pam_context, krbret)); 167181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 168181834Sroberto retval = PAM_SERVICE_ERR; 169181834Sroberto goto cleanup3; 170181834Sroberto } 171181834Sroberto 172181834Sroberto PAM_LOG("Done krb5_parse_name()"); 173181834Sroberto 174181834Sroberto /* Now convert the principal name into something human readable */ 175181834Sroberto princ_name = NULL; 176181834Sroberto krbret = krb5_unparse_name(pam_context, princ, &princ_name); 177181834Sroberto if (krbret != 0) { 178181834Sroberto PAM_LOG("Error krb5_unparse_name(): %s", 179181834Sroberto krb5_get_err_text(pam_context, krbret)); 180181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 181181834Sroberto retval = PAM_SERVICE_ERR; 182181834Sroberto goto cleanup2; 183181834Sroberto } 184181834Sroberto 185181834Sroberto PAM_LOG("Got principal: %s", princ_name); 186181834Sroberto 187181834Sroberto /* Get password */ 188181834Sroberto retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT); 189181834Sroberto if (retval != PAM_SUCCESS) 190181834Sroberto goto cleanup2; 191181834Sroberto 192181834Sroberto PAM_LOG("Got password"); 193181834Sroberto 194181834Sroberto /* Verify the local user exists (AFTER getting the password) */ 195181834Sroberto if (strchr(user, '@')) { 196181834Sroberto /* get a local account name for this principal */ 197181834Sroberto krbret = krb5_aname_to_localname(pam_context, princ, 198181834Sroberto sizeof(luser), luser); 199181834Sroberto if (krbret != 0) { 200181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 201181834Sroberto PAM_LOG("Error krb5_aname_to_localname(): %s", 202181834Sroberto krb5_get_err_text(pam_context, krbret)); 203181834Sroberto retval = PAM_USER_UNKNOWN; 204181834Sroberto goto cleanup2; 205181834Sroberto } 206181834Sroberto 207181834Sroberto retval = pam_set_item(pamh, PAM_USER, luser); 208181834Sroberto if (retval != PAM_SUCCESS) 209181834Sroberto goto cleanup2; 210181834Sroberto 211181834Sroberto retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 212181834Sroberto if (retval != PAM_SUCCESS) 213181834Sroberto goto cleanup2; 214181834Sroberto 215181834Sroberto PAM_LOG("PAM_USER Redone"); 216181834Sroberto } 217181834Sroberto 218181834Sroberto pwd = getpwnam(user); 219181834Sroberto if (pwd == NULL) { 220181834Sroberto retval = PAM_USER_UNKNOWN; 221181834Sroberto goto cleanup2; 222181834Sroberto } 223181834Sroberto 224181834Sroberto PAM_LOG("Done getpwnam()"); 225181834Sroberto 226181834Sroberto /* Get a TGT */ 227181834Sroberto memset(&creds, 0, sizeof(krb5_creds)); 228181834Sroberto krbret = krb5_get_init_creds_password(pam_context, &creds, princ, 229181834Sroberto pass, NULL, pamh, 0, NULL, &opts); 230181834Sroberto if (krbret != 0) { 231181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 232181834Sroberto PAM_LOG("Error krb5_get_init_creds_password(): %s", 233181834Sroberto krb5_get_err_text(pam_context, krbret)); 234181834Sroberto retval = PAM_AUTH_ERR; 235181834Sroberto goto cleanup2; 236181834Sroberto } 237181834Sroberto 238181834Sroberto PAM_LOG("Got TGT"); 239181834Sroberto 240181834Sroberto /* Generate a temporary cache */ 241181834Sroberto krbret = krb5_cc_gen_new(pam_context, &krb5_mcc_ops, &ccache); 242181834Sroberto if (krbret != 0) { 243181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 244181834Sroberto PAM_LOG("Error krb5_cc_gen_new(): %s", 245181834Sroberto krb5_get_err_text(pam_context, krbret)); 246181834Sroberto retval = PAM_SERVICE_ERR; 247181834Sroberto goto cleanup; 248181834Sroberto } 249181834Sroberto krbret = krb5_cc_initialize(pam_context, ccache, princ); 250181834Sroberto if (krbret != 0) { 251181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 252181834Sroberto PAM_LOG("Error krb5_cc_initialize(): %s", 253181834Sroberto krb5_get_err_text(pam_context, krbret)); 254181834Sroberto retval = PAM_SERVICE_ERR; 255181834Sroberto goto cleanup; 256181834Sroberto } 257181834Sroberto krbret = krb5_cc_store_cred(pam_context, ccache, &creds); 258181834Sroberto if (krbret != 0) { 259181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 260181834Sroberto PAM_LOG("Error krb5_cc_store_cred(): %s", 261181834Sroberto krb5_get_err_text(pam_context, krbret)); 262181834Sroberto krb5_cc_destroy(pam_context, ccache); 263181834Sroberto retval = PAM_SERVICE_ERR; 264181834Sroberto goto cleanup; 265181834Sroberto } 266181834Sroberto 267181834Sroberto PAM_LOG("Credentials stashed"); 268181834Sroberto 269181834Sroberto /* Verify them */ 270181834Sroberto if ((srvdup = strdup(service)) == NULL) { 271181834Sroberto retval = PAM_BUF_ERR; 272181834Sroberto goto cleanup; 273181834Sroberto } 274181834Sroberto krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup, 275181834Sroberto openpam_get_option(pamh, PAM_OPT_FORWARDABLE) ? 1 : 0); 276181834Sroberto free(srvdup); 277181834Sroberto if (krbret == -1) { 278181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 279181834Sroberto krb5_cc_destroy(pam_context, ccache); 280181834Sroberto retval = PAM_AUTH_ERR; 281181834Sroberto goto cleanup; 282181834Sroberto } 283181834Sroberto 284181834Sroberto PAM_LOG("Credentials stash verified"); 285181834Sroberto 286181834Sroberto retval = pam_get_data(pamh, "ccache", (const void **)&ccache_name); 287181834Sroberto if (retval == PAM_SUCCESS) { 288181834Sroberto krb5_cc_destroy(pam_context, ccache); 289181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 290181834Sroberto retval = PAM_AUTH_ERR; 291181834Sroberto goto cleanup; 292181834Sroberto } 293181834Sroberto 294181834Sroberto PAM_LOG("Credentials stash not pre-existing"); 295181834Sroberto 296181834Sroberto asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context, 297181834Sroberto ccache), krb5_cc_get_name(pam_context, ccache)); 298181834Sroberto if (ccache_name == NULL) { 299181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 300181834Sroberto retval = PAM_BUF_ERR; 301181834Sroberto goto cleanup; 302181834Sroberto } 303181834Sroberto retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache); 304181834Sroberto if (retval != 0) { 305181834Sroberto krb5_cc_destroy(pam_context, ccache); 306181834Sroberto PAM_VERBOSE_ERROR("Kerberos 5 error"); 307181834Sroberto retval = PAM_SERVICE_ERR; 308181834Sroberto goto cleanup; 309181834Sroberto } 310181834Sroberto 311181834Sroberto PAM_LOG("Credentials stash saved"); 312181834Sroberto 313181834Srobertocleanup: 314181834Sroberto krb5_free_cred_contents(pam_context, &creds); 315181834Sroberto PAM_LOG("Done cleanup"); 316181834Srobertocleanup2: 317181834Sroberto krb5_free_principal(pam_context, princ); 318181834Sroberto PAM_LOG("Done cleanup2"); 319181834Srobertocleanup3: 320 if (princ_name) 321 free(princ_name); 322 323 krb5_free_context(pam_context); 324 325 PAM_LOG("Done cleanup3"); 326 327 if (retval != PAM_SUCCESS) 328 PAM_VERBOSE_ERROR("Kerberos 5 refuses you"); 329 330 return (retval); 331} 332 333PAM_EXTERN int 334pam_sm_setcred(pam_handle_t *pamh, int flags, 335 int argc __unused, const char *argv[] __unused) 336{ 337 338 krb5_error_code krbret; 339 krb5_context pam_context; 340 krb5_principal princ; 341 krb5_creds creds; 342 krb5_ccache ccache_temp, ccache_perm; 343 krb5_cc_cursor cursor; 344 struct passwd *pwd = NULL; 345 int retval; 346 const char *cache_name, *q, *user; 347 char *cache_name_buf = NULL, *p; 348 349 uid_t euid; 350 gid_t egid; 351 352 if (flags & PAM_DELETE_CRED) 353 return (PAM_SUCCESS); 354 355 if (flags & PAM_REFRESH_CRED) 356 return (PAM_SUCCESS); 357 358 if (flags & PAM_REINITIALIZE_CRED) 359 return (PAM_SUCCESS); 360 361 if (!(flags & PAM_ESTABLISH_CRED)) 362 return (PAM_SERVICE_ERR); 363 364 PAM_LOG("Establishing credentials"); 365 366 /* Get username */ 367 retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 368 if (retval != PAM_SUCCESS) 369 return (retval); 370 371 PAM_LOG("Got user: %s", user); 372 373 krbret = krb5_init_context(&pam_context); 374 if (krbret != 0) { 375 PAM_LOG("Error krb5_init_context() failed"); 376 return (PAM_SERVICE_ERR); 377 } 378 379 PAM_LOG("Context initialised"); 380 381 euid = geteuid(); /* Usually 0 */ 382 egid = getegid(); 383 384 PAM_LOG("Got euid, egid: %d %d", euid, egid); 385 386 /* Retrieve the temporary cache */ 387 retval = pam_get_data(pamh, "ccache", (const void **)&cache_name); 388 if (retval != PAM_SUCCESS) { 389 retval = PAM_CRED_UNAVAIL; 390 goto cleanup3; 391 } 392 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_temp); 393 if (krbret != 0) { 394 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", cache_name, 395 krb5_get_err_text(pam_context, krbret)); 396 retval = PAM_SERVICE_ERR; 397 goto cleanup3; 398 } 399 400 /* Get the uid. This should exist. */ 401 pwd = getpwnam(user); 402 if (pwd == NULL) { 403 retval = PAM_USER_UNKNOWN; 404 goto cleanup3; 405 } 406 407 PAM_LOG("Done getpwnam()"); 408 409 /* Avoid following a symlink as root */ 410 if (setegid(pwd->pw_gid)) { 411 retval = PAM_SERVICE_ERR; 412 goto cleanup3; 413 } 414 if (seteuid(pwd->pw_uid)) { 415 retval = PAM_SERVICE_ERR; 416 goto cleanup3; 417 } 418 419 PAM_LOG("Done setegid() & seteuid()"); 420 421 /* Get the cache name */ 422 cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE); 423 if (cache_name == NULL) { 424 asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid); 425 cache_name = cache_name_buf; 426 } 427 428 p = calloc(PATH_MAX + 16, sizeof(char)); 429 q = cache_name; 430 431 if (p == NULL) { 432 PAM_LOG("Error malloc(): failure"); 433 retval = PAM_BUF_ERR; 434 goto cleanup3; 435 } 436 cache_name = p; 437 438 /* convert %u and %p */ 439 while (*q) { 440 if (*q == '%') { 441 q++; 442 if (*q == 'u') { 443 sprintf(p, "%d", pwd->pw_uid); 444 p += strlen(p); 445 } 446 else if (*q == 'p') { 447 sprintf(p, "%d", getpid()); 448 p += strlen(p); 449 } 450 else { 451 /* Not a special token */ 452 *p++ = '%'; 453 q--; 454 } 455 q++; 456 } 457 else { 458 *p++ = *q++; 459 } 460 } 461 462 PAM_LOG("Got cache_name: %s", cache_name); 463 464 /* Initialize the new ccache */ 465 krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ); 466 if (krbret != 0) { 467 PAM_LOG("Error krb5_cc_get_principal(): %s", 468 krb5_get_err_text(pam_context, krbret)); 469 retval = PAM_SERVICE_ERR; 470 goto cleanup3; 471 } 472 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm); 473 if (krbret != 0) { 474 PAM_LOG("Error krb5_cc_resolve(): %s", 475 krb5_get_err_text(pam_context, krbret)); 476 retval = PAM_SERVICE_ERR; 477 goto cleanup2; 478 } 479 krbret = krb5_cc_initialize(pam_context, ccache_perm, princ); 480 if (krbret != 0) { 481 PAM_LOG("Error krb5_cc_initialize(): %s", 482 krb5_get_err_text(pam_context, krbret)); 483 retval = PAM_SERVICE_ERR; 484 goto cleanup2; 485 } 486 487 PAM_LOG("Cache initialised"); 488 489 /* Prepare for iteration over creds */ 490 krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor); 491 if (krbret != 0) { 492 PAM_LOG("Error krb5_cc_start_seq_get(): %s", 493 krb5_get_err_text(pam_context, krbret)); 494 krb5_cc_destroy(pam_context, ccache_perm); 495 retval = PAM_SERVICE_ERR; 496 goto cleanup2; 497 } 498 499 PAM_LOG("Prepared for iteration"); 500 501 /* Copy the creds (should be two of them) */ 502 while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp, 503 &cursor, &creds) == 0)) { 504 krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds); 505 if (krbret != 0) { 506 PAM_LOG("Error krb5_cc_store_cred(): %s", 507 krb5_get_err_text(pam_context, krbret)); 508 krb5_cc_destroy(pam_context, ccache_perm); 509 krb5_free_cred_contents(pam_context, &creds); 510 retval = PAM_SERVICE_ERR; 511 goto cleanup2; 512 } 513 krb5_free_cred_contents(pam_context, &creds); 514 PAM_LOG("Iteration"); 515 } 516 krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor); 517 518 PAM_LOG("Done iterating"); 519 520 if (strstr(cache_name, "FILE:") == cache_name) { 521 if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) { 522 PAM_LOG("Error chown(): %s", strerror(errno)); 523 krb5_cc_destroy(pam_context, ccache_perm); 524 retval = PAM_SERVICE_ERR; 525 goto cleanup2; 526 } 527 PAM_LOG("Done chown()"); 528 529 if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) { 530 PAM_LOG("Error chmod(): %s", strerror(errno)); 531 krb5_cc_destroy(pam_context, ccache_perm); 532 retval = PAM_SERVICE_ERR; 533 goto cleanup2; 534 } 535 PAM_LOG("Done chmod()"); 536 } 537 538 krb5_cc_close(pam_context, ccache_perm); 539 540 PAM_LOG("Cache closed"); 541 542 retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1); 543 if (retval != PAM_SUCCESS) { 544 PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval)); 545 krb5_cc_destroy(pam_context, ccache_perm); 546 retval = PAM_SERVICE_ERR; 547 goto cleanup2; 548 } 549 550 PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name); 551 552cleanup2: 553 krb5_free_principal(pam_context, princ); 554 PAM_LOG("Done cleanup2"); 555cleanup3: 556 krb5_free_context(pam_context); 557 PAM_LOG("Done cleanup3"); 558 559 seteuid(euid); 560 setegid(egid); 561 562 PAM_LOG("Done seteuid() & setegid()"); 563 564 if (cache_name_buf != NULL) 565 free(cache_name_buf); 566 567 return (retval); 568} 569 570/* 571 * account management 572 */ 573PAM_EXTERN int 574pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused, 575 int argc __unused, const char *argv[] __unused) 576{ 577 krb5_error_code krbret; 578 krb5_context pam_context; 579 krb5_ccache ccache; 580 krb5_principal princ; 581 int retval; 582 const char *user, *ccache_name; 583 584 retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 585 if (retval != PAM_SUCCESS) 586 return (retval); 587 588 PAM_LOG("Got user: %s", user); 589 590 retval = pam_get_data(pamh, "ccache", (const void **)&ccache_name); 591 if (retval != PAM_SUCCESS) 592 return (PAM_SUCCESS); 593 594 PAM_LOG("Got credentials"); 595 596 krbret = krb5_init_context(&pam_context); 597 if (krbret != 0) { 598 PAM_LOG("Error krb5_init_context() failed"); 599 return (PAM_PERM_DENIED); 600 } 601 602 PAM_LOG("Context initialised"); 603 604 krbret = krb5_cc_resolve(pam_context, ccache_name, &ccache); 605 if (krbret != 0) { 606 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", ccache_name, 607 krb5_get_err_text(pam_context, krbret)); 608 krb5_free_context(pam_context); 609 return (PAM_PERM_DENIED); 610 } 611 612 PAM_LOG("Got ccache %s", ccache_name); 613 614 615 krbret = krb5_cc_get_principal(pam_context, ccache, &princ); 616 if (krbret != 0) { 617 PAM_LOG("Error krb5_cc_get_principal(): %s", 618 krb5_get_err_text(pam_context, krbret)); 619 retval = PAM_PERM_DENIED;; 620 goto cleanup; 621 } 622 623 PAM_LOG("Got principal"); 624 625 if (krb5_kuserok(pam_context, princ, user)) 626 retval = PAM_SUCCESS; 627 else 628 retval = PAM_PERM_DENIED; 629 krb5_free_principal(pam_context, princ); 630 631 PAM_LOG("Done kuserok()"); 632 633cleanup: 634 krb5_free_context(pam_context); 635 PAM_LOG("Done cleanup"); 636 637 return (retval); 638 639} 640 641/* 642 * password management 643 */ 644PAM_EXTERN int 645pam_sm_chauthtok(pam_handle_t *pamh, int flags, 646 int argc __unused, const char *argv[] __unused) 647{ 648 krb5_error_code krbret; 649 krb5_context pam_context; 650 krb5_creds creds; 651 krb5_principal princ; 652 krb5_get_init_creds_opt opts; 653 krb5_data result_code_string, result_string; 654 int result_code, retval; 655 const char *user, *pass; 656 char *princ_name, *passdup; 657 658 if (!(flags & PAM_UPDATE_AUTHTOK)) 659 return (PAM_AUTHTOK_ERR); 660 661 retval = pam_get_item(pamh, PAM_USER, (const void **)&user); 662 if (retval != PAM_SUCCESS) 663 return (retval); 664 665 PAM_LOG("Got user: %s", user); 666 667 krbret = krb5_init_context(&pam_context); 668 if (krbret != 0) { 669 PAM_LOG("Error krb5_init_context() failed"); 670 return (PAM_SERVICE_ERR); 671 } 672 673 PAM_LOG("Context initialised"); 674 675 krb5_get_init_creds_opt_init(&opts); 676 677 PAM_LOG("Credentials options initialised"); 678 679 /* Get principal name */ 680 krbret = krb5_parse_name(pam_context, user, &princ); 681 if (krbret != 0) { 682 PAM_LOG("Error krb5_parse_name(): %s", 683 krb5_get_err_text(pam_context, krbret)); 684 retval = PAM_USER_UNKNOWN; 685 goto cleanup3; 686 } 687 688 /* Now convert the principal name into something human readable */ 689 princ_name = NULL; 690 krbret = krb5_unparse_name(pam_context, princ, &princ_name); 691 if (krbret != 0) { 692 PAM_LOG("Error krb5_unparse_name(): %s", 693 krb5_get_err_text(pam_context, krbret)); 694 retval = PAM_SERVICE_ERR; 695 goto cleanup2; 696 } 697 698 PAM_LOG("Got principal: %s", princ_name); 699 700 /* Get password */ 701 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, PASSWORD_PROMPT); 702 if (retval != PAM_SUCCESS) 703 goto cleanup2; 704 705 PAM_LOG("Got password"); 706 707 memset(&creds, 0, sizeof(krb5_creds)); 708 krbret = krb5_get_init_creds_password(pam_context, &creds, princ, 709 pass, NULL, pamh, 0, "kadmin/changepw", &opts); 710 if (krbret != 0) { 711 PAM_LOG("Error krb5_get_init_creds_password()", 712 krb5_get_err_text(pam_context, krbret)); 713 retval = PAM_AUTH_ERR; 714 goto cleanup2; 715 } 716 717 PAM_LOG("Credentials established"); 718 719 /* Now get the new password */ 720 for (;;) { 721 retval = pam_get_authtok(pamh, 722 PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT); 723 if (retval != PAM_TRY_AGAIN) 724 break; 725 pam_error(pamh, "Mismatch; try again, EOF to quit."); 726 } 727 if (retval != PAM_SUCCESS) 728 goto cleanup; 729 730 PAM_LOG("Got new password"); 731 732 /* Change it */ 733 if ((passdup = strdup(pass)) == NULL) { 734 retval = PAM_BUF_ERR; 735 goto cleanup; 736 } 737 krbret = krb5_change_password(pam_context, &creds, passdup, 738 &result_code, &result_code_string, &result_string); 739 free(passdup); 740 if (krbret != 0) { 741 PAM_LOG("Error krb5_change_password(): %s", 742 krb5_get_err_text(pam_context, krbret)); 743 retval = PAM_AUTHTOK_ERR; 744 goto cleanup; 745 } 746 if (result_code) { 747 PAM_LOG("Error krb5_change_password(): (result_code)"); 748 retval = PAM_AUTHTOK_ERR; 749 goto cleanup; 750 } 751 752 PAM_LOG("Password changed"); 753 754 if (result_string.data) 755 free(result_string.data); 756 if (result_code_string.data) 757 free(result_code_string.data); 758 759cleanup: 760 krb5_free_cred_contents(pam_context, &creds); 761 PAM_LOG("Done cleanup"); 762cleanup2: 763 krb5_free_principal(pam_context, princ); 764 PAM_LOG("Done cleanup2"); 765cleanup3: 766 if (princ_name) 767 free(princ_name); 768 769 krb5_free_context(pam_context); 770 771 PAM_LOG("Done cleanup3"); 772 773 return (retval); 774} 775 776PAM_MODULE_ENTRY("pam_krb5"); 777 778/* 779 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c 780 * Modified by Sam Hartman <hartmans@mit.edu> to support PAM services 781 * for Debian. 782 * 783 * Verify the Kerberos ticket-granting ticket just retrieved for the 784 * user. If the Kerberos server doesn't respond, assume the user is 785 * trying to fake us out (since we DID just get a TGT from what is 786 * supposedly our KDC). If the host/<host> service is unknown (i.e., 787 * the local keytab doesn't have it), and we cannot find another 788 * service we do have, let her in. 789 * 790 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty. 791 */ 792/* ARGSUSED */ 793static int 794verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache, 795 char *pam_service, int debug) 796{ 797 krb5_error_code retval; 798 krb5_principal princ; 799 krb5_keyblock *keyblock; 800 krb5_data packet; 801 krb5_auth_context auth_context; 802 char phost[BUFSIZ]; 803 const char *services[3], **service; 804 805 packet.data = 0; 806 807 /* If possible we want to try and verify the ticket we have 808 * received against a keytab. We will try multiple service 809 * principals, including at least the host principal and the PAM 810 * service principal. The host principal is preferred because access 811 * to that key is generally sufficient to compromise root, while the 812 * service key for this PAM service may be less carefully guarded. 813 * It is important to check the keytab first before the KDC so we do 814 * not get spoofed by a fake KDC. 815 */ 816 services[0] = "host"; 817 services[1] = pam_service; 818 services[2] = NULL; 819 keyblock = 0; 820 retval = -1; 821 for (service = &services[0]; *service != NULL; service++) { 822 retval = krb5_sname_to_principal(context, NULL, *service, 823 KRB5_NT_SRV_HST, &princ); 824 if (retval != 0) { 825 if (debug) 826 syslog(LOG_DEBUG, 827 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 828 "krb5_sname_to_principal()", 829 krb5_get_err_text(context, retval)); 830 return -1; 831 } 832 833 /* Extract the name directly. */ 834 strncpy(phost, compat_princ_component(context, princ, 1), 835 BUFSIZ); 836 phost[BUFSIZ - 1] = '\0'; 837 838 /* 839 * Do we have service/<host> keys? 840 * (use default/configured keytab, kvno IGNORE_VNO to get the 841 * first match, and ignore enctype.) 842 */ 843 retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0, 844 &keyblock); 845 if (retval != 0) 846 continue; 847 break; 848 } 849 if (retval != 0) { /* failed to find key */ 850 /* Keytab or service key does not exist */ 851 if (debug) 852 syslog(LOG_DEBUG, 853 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 854 "krb5_kt_read_service_key()", 855 krb5_get_err_text(context, retval)); 856 retval = 0; 857 goto cleanup; 858 } 859 if (keyblock) 860 krb5_free_keyblock(context, keyblock); 861 862 /* Talk to the kdc and construct the ticket. */ 863 auth_context = NULL; 864 retval = krb5_mk_req(context, &auth_context, 0, *service, phost, 865 NULL, ccache, &packet); 866 if (auth_context) { 867 krb5_auth_con_free(context, auth_context); 868 auth_context = NULL; /* setup for rd_req */ 869 } 870 if (retval) { 871 if (debug) 872 syslog(LOG_DEBUG, 873 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 874 "krb5_mk_req()", 875 krb5_get_err_text(context, retval)); 876 retval = -1; 877 goto cleanup; 878 } 879 880 /* Try to use the ticket. */ 881 retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL, 882 NULL, NULL); 883 if (retval) { 884 if (debug) 885 syslog(LOG_DEBUG, 886 "pam_krb5: verify_krb_v5_tgt(): %s: %s", 887 "krb5_rd_req()", 888 krb5_get_err_text(context, retval)); 889 retval = -1; 890 } 891 else 892 retval = 1; 893 894cleanup: 895 if (packet.data) 896 compat_free_data_contents(context, &packet); 897 krb5_free_principal(context, princ); 898 return retval; 899} 900 901/* Free the memory for cache_name. Called by pam_end() */ 902/* ARGSUSED */ 903static void 904cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused) 905{ 906 krb5_context pam_context; 907 krb5_ccache ccache; 908 krb5_error_code krbret; 909 910 if (krb5_init_context(&pam_context)) 911 return; 912 913 krbret = krb5_cc_resolve(pam_context, data, &ccache); 914 if (krbret == 0) 915 krb5_cc_destroy(pam_context, ccache); 916 krb5_free_context(pam_context); 917 free(data); 918} 919 920#ifdef COMPAT_HEIMDAL 921#ifdef COMPAT_MIT 922#error This cannot be MIT and Heimdal compatible! 923#endif 924#endif 925 926#ifndef COMPAT_HEIMDAL 927#ifndef COMPAT_MIT 928#error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified! 929#endif 930#endif 931 932#ifdef COMPAT_HEIMDAL 933/* ARGSUSED */ 934static const char * 935compat_princ_component(krb5_context context __unused, krb5_principal princ, int n) 936{ 937 return princ->name.name_string.val[n]; 938} 939 940/* ARGSUSED */ 941static void 942compat_free_data_contents(krb5_context context __unused, krb5_data * data) 943{ 944 krb5_xfree(data->data); 945} 946#endif 947 948#ifdef COMPAT_MIT 949static const char * 950compat_princ_component(krb5_context context, krb5_principal princ, int n) 951{ 952 return krb5_princ_component(context, princ, n)->data; 953} 954 955static void 956compat_free_data_contents(krb5_context context, krb5_data * data) 957{ 958 krb5_free_data_contents(context, data); 959} 960#endif 961