Home | History | Annotate | Line # | Download | only in pam_krb5
pam_krb5.c revision 1.4
      1 /*	$NetBSD: pam_krb5.c,v 1.4 2005/02/26 18:03:37 thorpej Exp $	*/
      2 
      3 /*-
      4  * This pam_krb5 module contains code that is:
      5  *   Copyright (c) Derrick J. Brashear, 1996. All rights reserved.
      6  *   Copyright (c) Frank Cusack, 1999-2001. All rights reserved.
      7  *   Copyright (c) Jacques A. Vidrine, 2000-2001. All rights reserved.
      8  *   Copyright (c) Nicolas Williams, 2001. All rights reserved.
      9  *   Copyright (c) Perot Systems Corporation, 2001. All rights reserved.
     10  *   Copyright (c) Mark R V Murray, 2001.  All rights reserved.
     11  *   Copyright (c) Networks Associates Technology, Inc., 2002-2005.
     12  *       All rights reserved.
     13  *
     14  * Portions of this software were developed for the FreeBSD Project by
     15  * ThinkSec AS and NAI Labs, the Security Research Division of Network
     16  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
     17  * ("CBOSS"), as part of the DARPA CHATS research program.
     18  *
     19  * Redistribution and use in source and binary forms, with or without
     20  * modification, are permitted provided that the following conditions
     21  * are met:
     22  * 1. Redistributions of source code must retain the above copyright
     23  *    notices, and the entire permission notice in its entirety,
     24  *    including the disclaimer of warranties.
     25  * 2. Redistributions in binary form must reproduce the above copyright
     26  *    notice, this list of conditions and the following disclaimer in the
     27  *    documentation and/or other materials provided with the distribution.
     28  * 3. The name of the author may not be used to endorse or promote
     29  *    products derived from this software without specific prior
     30  *    written permission.
     31  *
     32  * ALTERNATIVELY, this product may be distributed under the terms of
     33  * the GNU Public License, in which case the provisions of the GPL are
     34  * required INSTEAD OF the above restrictions.  (This clause is
     35  * necessary due to a potential bad interaction between the GPL and
     36  * the restrictions contained in a BSD-style copyright.)
     37  *
     38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
     39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     41  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     44  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     46  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     47  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
     48  * OF THE POSSIBILITY OF SUCH DAMAGE.
     49  *
     50  */
     51 
     52 #include <sys/cdefs.h>
     53 #ifdef __FreeBSD__
     54 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_krb5/pam_krb5.c,v 1.22 2005/01/24 16:49:50 rwatson Exp $");
     55 #else
     56 __RCSID("$NetBSD: pam_krb5.c,v 1.4 2005/02/26 18:03:37 thorpej Exp $");
     57 #endif
     58 
     59 #include <sys/types.h>
     60 #include <sys/stat.h>
     61 #include <errno.h>
     62 #include <limits.h>
     63 #include <pwd.h>
     64 #include <stdio.h>
     65 #include <stdlib.h>
     66 #include <string.h>
     67 #include <syslog.h>
     68 #include <unistd.h>
     69 
     70 #include <krb5/krb5.h>
     71 #include <krb5/com_err.h>
     72 
     73 #define	PAM_SM_AUTH
     74 #define	PAM_SM_ACCOUNT
     75 #define	PAM_SM_PASSWORD
     76 
     77 #include <security/pam_appl.h>
     78 #include <security/pam_modules.h>
     79 #include <security/pam_mod_misc.h>
     80 #include <security/openpam.h>
     81 
     82 #define	COMPAT_HEIMDAL
     83 /* #define	COMPAT_MIT */
     84 
     85 static int	verify_krb_v5_tgt(krb5_context, krb5_ccache, char *, int);
     86 static void	cleanup_cache(pam_handle_t *, void *, int);
     87 static const	char *compat_princ_component(krb5_context, krb5_principal, int);
     88 static void	compat_free_data_contents(krb5_context, krb5_data *);
     89 
     90 #define USER_PROMPT		"Username: "
     91 #define PASSWORD_PROMPT		"Password:"
     92 #define NEW_PASSWORD_PROMPT	"New Password:"
     93 
     94 #define PAM_OPT_CCACHE		"ccache"
     95 #define PAM_OPT_DEBUG		"debug"
     96 #define PAM_OPT_FORWARDABLE	"forwardable"
     97 #define PAM_OPT_NO_CCACHE	"no_ccache"
     98 #define PAM_OPT_REUSE_CCACHE	"reuse_ccache"
     99 
    100 /*
    101  * authentication management
    102  */
    103 PAM_EXTERN int
    104 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
    105     int argc __unused, const char *argv[] __unused)
    106 {
    107 	krb5_error_code krbret;
    108 	krb5_context pam_context;
    109 	krb5_creds creds;
    110 	krb5_principal princ;
    111 	krb5_ccache ccache;
    112 	krb5_get_init_creds_opt opts;
    113 	struct passwd *pwd;
    114 	int retval;
    115 	void *ccache_data;
    116 	const char *user, *pass;
    117 	const void *sourceuser, *service;
    118 	char *principal, *princ_name, *ccache_name, luser[32], *srvdup;
    119 
    120 	retval = pam_get_user(pamh, &user, USER_PROMPT);
    121 	if (retval != PAM_SUCCESS)
    122 		return (retval);
    123 
    124 	PAM_LOG("Got user: %s", user);
    125 
    126 	retval = pam_get_item(pamh, PAM_RUSER, &sourceuser);
    127 	if (retval != PAM_SUCCESS)
    128 		return (retval);
    129 
    130 	PAM_LOG("Got ruser: %s", (const char *)sourceuser);
    131 
    132 	service = NULL;
    133 	pam_get_item(pamh, PAM_SERVICE, &service);
    134 	if (service == NULL)
    135 		service = "unknown";
    136 
    137 	PAM_LOG("Got service: %s", (const char *)service);
    138 
    139 	krbret = krb5_init_context(&pam_context);
    140 	if (krbret != 0) {
    141 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    142 		return (PAM_SERVICE_ERR);
    143 	}
    144 
    145 	PAM_LOG("Context initialised");
    146 
    147 	krb5_get_init_creds_opt_init(&opts);
    148 
    149 	if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
    150 		krb5_get_init_creds_opt_set_forwardable(&opts, 1);
    151 
    152 	PAM_LOG("Credentials initialised");
    153 
    154 	krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE);
    155 	if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) {
    156 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    157 		retval = PAM_SERVICE_ERR;
    158 		goto cleanup3;
    159 	}
    160 
    161 	PAM_LOG("Done krb5_cc_register()");
    162 
    163 	/* Get principal name */
    164 	if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
    165 		asprintf(&principal, "%s/%s", (const char *)sourceuser, user);
    166 	else
    167 		principal = strdup(user);
    168 
    169 	PAM_LOG("Created principal: %s", principal);
    170 
    171 	krbret = krb5_parse_name(pam_context, principal, &princ);
    172 	free(principal);
    173 	if (krbret != 0) {
    174 		PAM_LOG("Error krb5_parse_name(): %s",
    175 		    krb5_get_err_text(pam_context, krbret));
    176 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    177 		retval = PAM_SERVICE_ERR;
    178 		goto cleanup3;
    179 	}
    180 
    181 	PAM_LOG("Done krb5_parse_name()");
    182 
    183 	/* Now convert the principal name into something human readable */
    184 	princ_name = NULL;
    185 	krbret = krb5_unparse_name(pam_context, princ, &princ_name);
    186 	if (krbret != 0) {
    187 		PAM_LOG("Error krb5_unparse_name(): %s",
    188 		    krb5_get_err_text(pam_context, krbret));
    189 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    190 		retval = PAM_SERVICE_ERR;
    191 		goto cleanup2;
    192 	}
    193 
    194 	PAM_LOG("Got principal: %s", princ_name);
    195 
    196 	/* Get password */
    197 	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
    198 	if (retval != PAM_SUCCESS)
    199 		goto cleanup2;
    200 
    201 	PAM_LOG("Got password");
    202 
    203 	/* Verify the local user exists (AFTER getting the password) */
    204 	if (strchr(user, '@')) {
    205 		/* get a local account name for this principal */
    206 		krbret = krb5_aname_to_localname(pam_context, princ,
    207 		    sizeof(luser), luser);
    208 		if (krbret != 0) {
    209 			PAM_VERBOSE_ERROR("Kerberos 5 error");
    210 			PAM_LOG("Error krb5_aname_to_localname(): %s",
    211 			    krb5_get_err_text(pam_context, krbret));
    212 			retval = PAM_USER_UNKNOWN;
    213 			goto cleanup2;
    214 		}
    215 
    216 		retval = pam_set_item(pamh, PAM_USER, luser);
    217 		if (retval != PAM_SUCCESS)
    218 			goto cleanup2;
    219 
    220 		PAM_LOG("PAM_USER Redone");
    221 	}
    222 
    223 	pwd = getpwnam(user);
    224 	if (pwd == NULL) {
    225 		retval = PAM_USER_UNKNOWN;
    226 		goto cleanup2;
    227 	}
    228 
    229 	PAM_LOG("Done getpwnam()");
    230 
    231 	/* Get a TGT */
    232 	memset(&creds, 0, sizeof(krb5_creds));
    233 	krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
    234 	    pass, NULL, pamh, 0, NULL, &opts);
    235 	if (krbret != 0) {
    236 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    237 		PAM_LOG("Error krb5_get_init_creds_password(): %s",
    238 		    krb5_get_err_text(pam_context, krbret));
    239 		retval = PAM_AUTH_ERR;
    240 		goto cleanup2;
    241 	}
    242 
    243 	PAM_LOG("Got TGT");
    244 
    245 	/* Generate a temporary cache */
    246 	krbret = krb5_cc_gen_new(pam_context, &krb5_mcc_ops, &ccache);
    247 	if (krbret != 0) {
    248 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    249 		PAM_LOG("Error krb5_cc_gen_new(): %s",
    250 		    krb5_get_err_text(pam_context, krbret));
    251 		retval = PAM_SERVICE_ERR;
    252 		goto cleanup;
    253 	}
    254 	krbret = krb5_cc_initialize(pam_context, ccache, princ);
    255 	if (krbret != 0) {
    256 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    257 		PAM_LOG("Error krb5_cc_initialize(): %s",
    258 		    krb5_get_err_text(pam_context, krbret));
    259 		retval = PAM_SERVICE_ERR;
    260 		goto cleanup;
    261 	}
    262 	krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
    263 	if (krbret != 0) {
    264 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    265 		PAM_LOG("Error krb5_cc_store_cred(): %s",
    266 		    krb5_get_err_text(pam_context, krbret));
    267 		krb5_cc_destroy(pam_context, ccache);
    268 		retval = PAM_SERVICE_ERR;
    269 		goto cleanup;
    270 	}
    271 
    272 	PAM_LOG("Credentials stashed");
    273 
    274 	/* Verify them */
    275 	if ((srvdup = strdup(service)) == NULL) {
    276 		retval = PAM_BUF_ERR;
    277 		goto cleanup;
    278 	}
    279 	krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
    280 	    openpam_get_option(pamh, PAM_OPT_DEBUG) ? 1 : 0);
    281 	free(srvdup);
    282 	if (krbret == -1) {
    283 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    284 		krb5_cc_destroy(pam_context, ccache);
    285 		retval = PAM_AUTH_ERR;
    286 		goto cleanup;
    287 	}
    288 
    289 	PAM_LOG("Credentials stash verified");
    290 
    291 	retval = pam_get_data(pamh, "ccache", &ccache_data);
    292 	if (retval == PAM_SUCCESS) {
    293 		krb5_cc_destroy(pam_context, ccache);
    294 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    295 		retval = PAM_AUTH_ERR;
    296 		goto cleanup;
    297 	}
    298 
    299 	PAM_LOG("Credentials stash not pre-existing");
    300 
    301 	asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
    302 		ccache), krb5_cc_get_name(pam_context, ccache));
    303 	if (ccache_name == NULL) {
    304 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    305 		retval = PAM_BUF_ERR;
    306 		goto cleanup;
    307 	}
    308 	retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache);
    309 	if (retval != 0) {
    310 		krb5_cc_destroy(pam_context, ccache);
    311 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    312 		retval = PAM_SERVICE_ERR;
    313 		goto cleanup;
    314 	}
    315 
    316 	PAM_LOG("Credentials stash saved");
    317 
    318 cleanup:
    319 	krb5_free_cred_contents(pam_context, &creds);
    320 	PAM_LOG("Done cleanup");
    321 cleanup2:
    322 	krb5_free_principal(pam_context, princ);
    323 	PAM_LOG("Done cleanup2");
    324 cleanup3:
    325 	if (princ_name)
    326 		free(princ_name);
    327 
    328 	krb5_free_context(pam_context);
    329 
    330 	PAM_LOG("Done cleanup3");
    331 
    332 	if (retval != PAM_SUCCESS)
    333 		PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
    334 
    335 	return (retval);
    336 }
    337 
    338 PAM_EXTERN int
    339 pam_sm_setcred(pam_handle_t *pamh, int flags,
    340     int argc __unused, const char *argv[] __unused)
    341 {
    342 
    343 	krb5_error_code krbret;
    344 	krb5_context pam_context;
    345 	krb5_principal princ;
    346 	krb5_creds creds;
    347 	krb5_ccache ccache_temp, ccache_perm;
    348 	krb5_cc_cursor cursor;
    349 	struct passwd *pwd = NULL;
    350 	int retval;
    351 	const char *cache_name, *q;
    352 	const void *user;
    353 	void *cache_data;
    354 	char *cache_name_buf = NULL, *p;
    355 
    356 	uid_t euid;
    357 	gid_t egid;
    358 
    359 	if (flags & PAM_DELETE_CRED)
    360 		return (PAM_SUCCESS);
    361 
    362 	if (flags & PAM_REFRESH_CRED)
    363 		return (PAM_SUCCESS);
    364 
    365 	if (flags & PAM_REINITIALIZE_CRED)
    366 		return (PAM_SUCCESS);
    367 
    368 	if (!(flags & PAM_ESTABLISH_CRED))
    369 		return (PAM_SERVICE_ERR);
    370 
    371 	/* If a persistent cache isn't desired, stop now. */
    372 	if (openpam_get_option(pamh, PAM_OPT_NO_CCACHE))
    373 		return (PAM_SUCCESS);
    374 
    375 	PAM_LOG("Establishing credentials");
    376 
    377 	/* Get username */
    378 	retval = pam_get_item(pamh, PAM_USER, &user);
    379 	if (retval != PAM_SUCCESS)
    380 		return (retval);
    381 
    382 	PAM_LOG("Got user: %s", (const char *)user);
    383 
    384 	krbret = krb5_init_context(&pam_context);
    385 	if (krbret != 0) {
    386 		PAM_LOG("Error krb5_init_context() failed");
    387 		return (PAM_SERVICE_ERR);
    388 	}
    389 
    390 	PAM_LOG("Context initialised");
    391 
    392 	euid = geteuid();	/* Usually 0 */
    393 	egid = getegid();
    394 
    395 	PAM_LOG("Got euid, egid: %d %d", euid, egid);
    396 
    397 	/* Retrieve the temporary cache */
    398 	retval = pam_get_data(pamh, "ccache", &cache_data);
    399 	if (retval != PAM_SUCCESS) {
    400 		retval = PAM_CRED_UNAVAIL;
    401 		goto cleanup3;
    402 	}
    403 	krbret = krb5_cc_resolve(pam_context, cache_data, &ccache_temp);
    404 	if (krbret != 0) {
    405 		PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)cache_data,
    406 		    krb5_get_err_text(pam_context, krbret));
    407 		retval = PAM_SERVICE_ERR;
    408 		goto cleanup3;
    409 	}
    410 
    411 	/* Get the uid. This should exist. */
    412 	pwd = getpwnam(user);
    413 	if (pwd == NULL) {
    414 		retval = PAM_USER_UNKNOWN;
    415 		goto cleanup3;
    416 	}
    417 
    418 	PAM_LOG("Done getpwnam()");
    419 
    420 	/* Avoid following a symlink as root */
    421 	if (setegid(pwd->pw_gid)) {
    422 		retval = PAM_SERVICE_ERR;
    423 		goto cleanup3;
    424 	}
    425 	if (seteuid(pwd->pw_uid)) {
    426 		retval = PAM_SERVICE_ERR;
    427 		goto cleanup3;
    428 	}
    429 
    430 	PAM_LOG("Done setegid() & seteuid()");
    431 
    432 	/* Get the cache name */
    433 	cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
    434 	if (cache_name == NULL) {
    435 		asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
    436 		cache_name = cache_name_buf;
    437 	}
    438 
    439 	p = calloc(PATH_MAX + 16, sizeof(char));
    440 	q = cache_name;
    441 
    442 	if (p == NULL) {
    443 		PAM_LOG("Error malloc(): failure");
    444 		retval = PAM_BUF_ERR;
    445 		goto cleanup3;
    446 	}
    447 	cache_name = p;
    448 
    449 	/* convert %u and %p */
    450 	while (*q) {
    451 		if (*q == '%') {
    452 			q++;
    453 			if (*q == 'u') {
    454 				sprintf(p, "%d", pwd->pw_uid);
    455 				p += strlen(p);
    456 			}
    457 			else if (*q == 'p') {
    458 				sprintf(p, "%d", getpid());
    459 				p += strlen(p);
    460 			}
    461 			else {
    462 				/* Not a special token */
    463 				*p++ = '%';
    464 				q--;
    465 			}
    466 			q++;
    467 		}
    468 		else {
    469 			*p++ = *q++;
    470 		}
    471 	}
    472 
    473 	PAM_LOG("Got cache_name: %s", cache_name);
    474 
    475 	/* Initialize the new ccache */
    476 	krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
    477 	if (krbret != 0) {
    478 		PAM_LOG("Error krb5_cc_get_principal(): %s",
    479 		    krb5_get_err_text(pam_context, krbret));
    480 		retval = PAM_SERVICE_ERR;
    481 		goto cleanup3;
    482 	}
    483 	krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
    484 	if (krbret != 0) {
    485 		PAM_LOG("Error krb5_cc_resolve(): %s",
    486 		    krb5_get_err_text(pam_context, krbret));
    487 		retval = PAM_SERVICE_ERR;
    488 		goto cleanup2;
    489 	}
    490 	krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
    491 	if (krbret != 0) {
    492 		PAM_LOG("Error krb5_cc_initialize(): %s",
    493 		    krb5_get_err_text(pam_context, krbret));
    494 		retval = PAM_SERVICE_ERR;
    495 		goto cleanup2;
    496 	}
    497 
    498 	PAM_LOG("Cache initialised");
    499 
    500 	/* Prepare for iteration over creds */
    501 	krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
    502 	if (krbret != 0) {
    503 		PAM_LOG("Error krb5_cc_start_seq_get(): %s",
    504 		    krb5_get_err_text(pam_context, krbret));
    505 		krb5_cc_destroy(pam_context, ccache_perm);
    506 		retval = PAM_SERVICE_ERR;
    507 		goto cleanup2;
    508 	}
    509 
    510 	PAM_LOG("Prepared for iteration");
    511 
    512 	/* Copy the creds (should be two of them) */
    513 	while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
    514 				&cursor, &creds) == 0)) {
    515 		krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
    516 		if (krbret != 0) {
    517 			PAM_LOG("Error krb5_cc_store_cred(): %s",
    518 			    krb5_get_err_text(pam_context, krbret));
    519 			krb5_cc_destroy(pam_context, ccache_perm);
    520 			krb5_free_cred_contents(pam_context, &creds);
    521 			retval = PAM_SERVICE_ERR;
    522 			goto cleanup2;
    523 		}
    524 		krb5_free_cred_contents(pam_context, &creds);
    525 		PAM_LOG("Iteration");
    526 	}
    527 	krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
    528 
    529 	PAM_LOG("Done iterating");
    530 
    531 	if (strstr(cache_name, "FILE:") == cache_name) {
    532 		if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) {
    533 			PAM_LOG("Error chown(): %s", strerror(errno));
    534 			krb5_cc_destroy(pam_context, ccache_perm);
    535 			retval = PAM_SERVICE_ERR;
    536 			goto cleanup2;
    537 		}
    538 		PAM_LOG("Done chown()");
    539 
    540 		if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) {
    541 			PAM_LOG("Error chmod(): %s", strerror(errno));
    542 			krb5_cc_destroy(pam_context, ccache_perm);
    543 			retval = PAM_SERVICE_ERR;
    544 			goto cleanup2;
    545 		}
    546 		PAM_LOG("Done chmod()");
    547 	}
    548 
    549 	krb5_cc_close(pam_context, ccache_perm);
    550 
    551 	PAM_LOG("Cache closed");
    552 
    553 	retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1);
    554 	if (retval != PAM_SUCCESS) {
    555 		PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
    556 		krb5_cc_destroy(pam_context, ccache_perm);
    557 		retval = PAM_SERVICE_ERR;
    558 		goto cleanup2;
    559 	}
    560 
    561 	PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
    562 
    563 cleanup2:
    564 	krb5_free_principal(pam_context, princ);
    565 	PAM_LOG("Done cleanup2");
    566 cleanup3:
    567 	krb5_free_context(pam_context);
    568 	PAM_LOG("Done cleanup3");
    569 
    570 	seteuid(euid);
    571 	setegid(egid);
    572 
    573 	PAM_LOG("Done seteuid() & setegid()");
    574 
    575 	if (cache_name_buf != NULL)
    576 		free(cache_name_buf);
    577 
    578 	return (retval);
    579 }
    580 
    581 /*
    582  * account management
    583  */
    584 PAM_EXTERN int
    585 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
    586     int argc __unused, const char *argv[] __unused)
    587 {
    588 	krb5_error_code krbret;
    589 	krb5_context pam_context;
    590 	krb5_ccache ccache;
    591 	krb5_principal princ;
    592 	int retval;
    593 	const void *user;
    594 	void *ccache_name;
    595 
    596 	retval = pam_get_item(pamh, PAM_USER, &user);
    597 	if (retval != PAM_SUCCESS)
    598 		return (retval);
    599 
    600 	PAM_LOG("Got user: %s", (const char *)user);
    601 
    602 	retval = pam_get_data(pamh, "ccache", &ccache_name);
    603 	if (retval != PAM_SUCCESS)
    604 		return (PAM_SUCCESS);
    605 
    606 	PAM_LOG("Got credentials");
    607 
    608 	krbret = krb5_init_context(&pam_context);
    609 	if (krbret != 0) {
    610 		PAM_LOG("Error krb5_init_context() failed");
    611 		return (PAM_PERM_DENIED);
    612 	}
    613 
    614 	PAM_LOG("Context initialised");
    615 
    616 	krbret = krb5_cc_resolve(pam_context, (const char *)ccache_name, &ccache);
    617 	if (krbret != 0) {
    618 		PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)ccache_name,
    619 		    krb5_get_err_text(pam_context, krbret));
    620 		krb5_free_context(pam_context);
    621 		return (PAM_PERM_DENIED);
    622 	}
    623 
    624 	PAM_LOG("Got ccache %s", (const char *)ccache_name);
    625 
    626 
    627 	krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
    628 	if (krbret != 0) {
    629 		PAM_LOG("Error krb5_cc_get_principal(): %s",
    630 		    krb5_get_err_text(pam_context, krbret));
    631 		retval = PAM_PERM_DENIED;;
    632 		goto cleanup;
    633 	}
    634 
    635 	PAM_LOG("Got principal");
    636 
    637 	if (krb5_kuserok(pam_context, princ, (const char *)user))
    638 		retval = PAM_SUCCESS;
    639 	else
    640 		retval = PAM_PERM_DENIED;
    641 	krb5_free_principal(pam_context, princ);
    642 
    643 	PAM_LOG("Done kuserok()");
    644 
    645 cleanup:
    646 	krb5_free_context(pam_context);
    647 	PAM_LOG("Done cleanup");
    648 
    649 	return (retval);
    650 
    651 }
    652 
    653 /*
    654  * password management
    655  */
    656 PAM_EXTERN int
    657 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
    658     int argc __unused, const char *argv[] __unused)
    659 {
    660 	krb5_error_code krbret;
    661 	krb5_context pam_context;
    662 	krb5_creds creds;
    663 	krb5_principal princ;
    664 	krb5_get_init_creds_opt opts;
    665 	krb5_data result_code_string, result_string;
    666 	int result_code, retval;
    667 	const char *pass;
    668 	const void *user;
    669 	char *princ_name, *passdup;
    670 
    671 	if (flags & PAM_PRELIM_CHECK) {
    672 		/* Nothing to do here. */
    673 		return (PAM_SUCCESS);
    674 	}
    675 
    676 	if (!(flags & PAM_UPDATE_AUTHTOK)) {
    677 		PAM_LOG("Illegal flags argument");
    678 		return (PAM_ABORT);
    679 	}
    680 
    681 	retval = pam_get_item(pamh, PAM_USER, &user);
    682 	if (retval != PAM_SUCCESS)
    683 		return (retval);
    684 
    685 	PAM_LOG("Got user: %s", (const char *)user);
    686 
    687 	krbret = krb5_init_context(&pam_context);
    688 	if (krbret != 0) {
    689 		PAM_LOG("Error krb5_init_context() failed");
    690 		return (PAM_SERVICE_ERR);
    691 	}
    692 
    693 	PAM_LOG("Context initialised");
    694 
    695 	krb5_get_init_creds_opt_init(&opts);
    696 
    697 	PAM_LOG("Credentials options initialised");
    698 
    699 	/* Get principal name */
    700 	krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
    701 	if (krbret != 0) {
    702 		PAM_LOG("Error krb5_parse_name(): %s",
    703 		    krb5_get_err_text(pam_context, krbret));
    704 		retval = PAM_USER_UNKNOWN;
    705 		goto cleanup3;
    706 	}
    707 
    708 	/* Now convert the principal name into something human readable */
    709 	princ_name = NULL;
    710 	krbret = krb5_unparse_name(pam_context, princ, &princ_name);
    711 	if (krbret != 0) {
    712 		PAM_LOG("Error krb5_unparse_name(): %s",
    713 		    krb5_get_err_text(pam_context, krbret));
    714 		retval = PAM_SERVICE_ERR;
    715 		goto cleanup2;
    716 	}
    717 
    718 	PAM_LOG("Got principal: %s", princ_name);
    719 
    720 	/* Get password */
    721 	retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, PASSWORD_PROMPT);
    722 	if (retval != PAM_SUCCESS)
    723 		goto cleanup2;
    724 
    725 	PAM_LOG("Got password");
    726 
    727 	memset(&creds, 0, sizeof(krb5_creds));
    728 	krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
    729 	    pass, NULL, pamh, 0, "kadmin/changepw", &opts);
    730 	if (krbret != 0) {
    731 		PAM_LOG("Error krb5_get_init_creds_password(): %s",
    732 		    krb5_get_err_text(pam_context, krbret));
    733 		retval = PAM_AUTH_ERR;
    734 		goto cleanup2;
    735 	}
    736 
    737 	PAM_LOG("Credentials established");
    738 
    739 	/* Now get the new password */
    740 	for (;;) {
    741 		retval = pam_get_authtok(pamh,
    742 		    PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
    743 		if (retval != PAM_TRY_AGAIN)
    744 			break;
    745 		pam_error(pamh, "Mismatch; try again, EOF to quit.");
    746 	}
    747 	if (retval != PAM_SUCCESS)
    748 		goto cleanup;
    749 
    750 	PAM_LOG("Got new password");
    751 
    752 	/* Change it */
    753 	if ((passdup = strdup(pass)) == NULL) {
    754 		retval = PAM_BUF_ERR;
    755 		goto cleanup;
    756 	}
    757 	krbret = krb5_change_password(pam_context, &creds, passdup,
    758 	    &result_code, &result_code_string, &result_string);
    759 	free(passdup);
    760 	if (krbret != 0) {
    761 		PAM_LOG("Error krb5_change_password(): %s",
    762 		    krb5_get_err_text(pam_context, krbret));
    763 		retval = PAM_AUTHTOK_ERR;
    764 		goto cleanup;
    765 	}
    766 	if (result_code) {
    767 		PAM_LOG("Error krb5_change_password(): (result_code)");
    768 		retval = PAM_AUTHTOK_ERR;
    769 		goto cleanup;
    770 	}
    771 
    772 	PAM_LOG("Password changed");
    773 
    774 	if (result_string.data)
    775 		free(result_string.data);
    776 	if (result_code_string.data)
    777 		free(result_code_string.data);
    778 
    779 cleanup:
    780 	krb5_free_cred_contents(pam_context, &creds);
    781 	PAM_LOG("Done cleanup");
    782 cleanup2:
    783 	krb5_free_principal(pam_context, princ);
    784 	PAM_LOG("Done cleanup2");
    785 cleanup3:
    786 	if (princ_name)
    787 		free(princ_name);
    788 
    789 	krb5_free_context(pam_context);
    790 
    791 	PAM_LOG("Done cleanup3");
    792 
    793 	return (retval);
    794 }
    795 
    796 PAM_MODULE_ENTRY("pam_krb5");
    797 
    798 /*
    799  * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
    800  * Modified by Sam Hartman <hartmans (at) mit.edu> to support PAM services
    801  * for Debian.
    802  *
    803  * Verify the Kerberos ticket-granting ticket just retrieved for the
    804  * user.  If the Kerberos server doesn't respond, assume the user is
    805  * trying to fake us out (since we DID just get a TGT from what is
    806  * supposedly our KDC).  If the host/<host> service is unknown (i.e.,
    807  * the local keytab doesn't have it), and we cannot find another
    808  * service we do have, let her in.
    809  *
    810  * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
    811  */
    812 /* ARGSUSED */
    813 static int
    814 verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
    815     char *pam_service, int debug)
    816 {
    817 	krb5_error_code retval;
    818 	krb5_principal princ;
    819 	krb5_keyblock *keyblock;
    820 	krb5_data packet;
    821 	krb5_auth_context auth_context;
    822 	char phost[BUFSIZ];
    823 	const char *services[3], **service;
    824 
    825 	packet.data = 0;
    826 
    827 	/* If possible we want to try and verify the ticket we have
    828 	 * received against a keytab.  We will try multiple service
    829 	 * principals, including at least the host principal and the PAM
    830 	 * service principal.  The host principal is preferred because access
    831 	 * to that key is generally sufficient to compromise root, while the
    832 	 * service key for this PAM service may be less carefully guarded.
    833 	 * It is important to check the keytab first before the KDC so we do
    834 	 * not get spoofed by a fake KDC.
    835 	 */
    836 	services[0] = "host";
    837 	services[1] = pam_service;
    838 	services[2] = NULL;
    839 	keyblock = 0;
    840 	retval = -1;
    841 	for (service = &services[0]; *service != NULL; service++) {
    842 		retval = krb5_sname_to_principal(context, NULL, *service,
    843 		    KRB5_NT_SRV_HST, &princ);
    844 		if (retval != 0) {
    845 			if (debug)
    846 				syslog(LOG_DEBUG,
    847 				    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
    848 				    "krb5_sname_to_principal()",
    849 				    krb5_get_err_text(context, retval));
    850 			return -1;
    851 		}
    852 
    853 		/* Extract the name directly. */
    854 		strncpy(phost, compat_princ_component(context, princ, 1),
    855 		    BUFSIZ);
    856 		phost[BUFSIZ - 1] = '\0';
    857 
    858 		/*
    859 		 * Do we have service/<host> keys?
    860 		 * (use default/configured keytab, kvno IGNORE_VNO to get the
    861 		 * first match, and ignore enctype.)
    862 		 */
    863 		retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
    864 		    &keyblock);
    865 		if (retval != 0)
    866 			continue;
    867 		break;
    868 	}
    869 	if (retval != 0) {	/* failed to find key */
    870 		/* Keytab or service key does not exist */
    871 		if (debug)
    872 			syslog(LOG_DEBUG,
    873 			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
    874 			    "krb5_kt_read_service_key()",
    875 			    krb5_get_err_text(context, retval));
    876 		retval = 0;
    877 		goto cleanup;
    878 	}
    879 	if (keyblock)
    880 		krb5_free_keyblock(context, keyblock);
    881 
    882 	/* Talk to the kdc and construct the ticket. */
    883 	auth_context = NULL;
    884 	retval = krb5_mk_req(context, &auth_context, 0, *service, phost,
    885 		NULL, ccache, &packet);
    886 	if (auth_context) {
    887 		krb5_auth_con_free(context, auth_context);
    888 		auth_context = NULL;	/* setup for rd_req */
    889 	}
    890 	if (retval) {
    891 		if (debug)
    892 			syslog(LOG_DEBUG,
    893 			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
    894 			    "krb5_mk_req()",
    895 			    krb5_get_err_text(context, retval));
    896 		retval = -1;
    897 		goto cleanup;
    898 	}
    899 
    900 	/* Try to use the ticket. */
    901 	retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
    902 	    NULL, NULL);
    903 	if (retval) {
    904 		if (debug)
    905 			syslog(LOG_DEBUG,
    906 			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
    907 			    "krb5_rd_req()",
    908 			    krb5_get_err_text(context, retval));
    909 		retval = -1;
    910 	}
    911 	else
    912 		retval = 1;
    913 
    914 cleanup:
    915 	if (packet.data)
    916 		compat_free_data_contents(context, &packet);
    917 	krb5_free_principal(context, princ);
    918 	return retval;
    919 }
    920 
    921 /* Free the memory for cache_name. Called by pam_end() */
    922 /* ARGSUSED */
    923 static void
    924 cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
    925 {
    926 	krb5_context pam_context;
    927 	krb5_ccache ccache;
    928 	krb5_error_code krbret;
    929 
    930 	if (krb5_init_context(&pam_context))
    931 		return;
    932 
    933 	krbret = krb5_cc_resolve(pam_context, data, &ccache);
    934 	if (krbret == 0)
    935 		krb5_cc_destroy(pam_context, ccache);
    936 	krb5_free_context(pam_context);
    937 	free(data);
    938 }
    939 
    940 #ifdef COMPAT_HEIMDAL
    941 #ifdef COMPAT_MIT
    942 #error This cannot be MIT and Heimdal compatible!
    943 #endif
    944 #endif
    945 
    946 #ifndef COMPAT_HEIMDAL
    947 #ifndef COMPAT_MIT
    948 #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
    949 #endif
    950 #endif
    951 
    952 #ifdef COMPAT_HEIMDAL
    953 /* ARGSUSED */
    954 static const char *
    955 compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
    956 {
    957 	return princ->name.name_string.val[n];
    958 }
    959 
    960 /* ARGSUSED */
    961 static void
    962 compat_free_data_contents(krb5_context context __unused, krb5_data * data)
    963 {
    964 	krb5_xfree(data->data);
    965 }
    966 #endif
    967 
    968 #ifdef COMPAT_MIT
    969 static const char *
    970 compat_princ_component(krb5_context context, krb5_principal princ, int n)
    971 {
    972 	return krb5_princ_component(context, princ, n)->data;
    973 }
    974 
    975 static void
    976 compat_free_data_contents(krb5_context context, krb5_data * data)
    977 {
    978 	krb5_free_data_contents(context, data);
    979 }
    980 #endif
    981