Home | History | Annotate | Line # | Download | only in pam_krb5
pam_krb5.c revision 1.8
      1 /*	$NetBSD: pam_krb5.c,v 1.8 2005/04/19 03:15:35 christos 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.8 2005/04/19 03:15:35 christos 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		"%s's 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, pwres;
    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 	char password_prompt[80];
    120 	char pwbuf[1024];
    121 
    122 	retval = pam_get_user(pamh, &user, USER_PROMPT);
    123 	if (retval != PAM_SUCCESS)
    124 		return (retval);
    125 
    126 	PAM_LOG("Got user: %s", user);
    127 
    128 	retval = pam_get_item(pamh, PAM_RUSER, &sourceuser);
    129 	if (retval != PAM_SUCCESS)
    130 		return (retval);
    131 
    132 	PAM_LOG("Got ruser: %s", (const char *)sourceuser);
    133 
    134 	service = NULL;
    135 	pam_get_item(pamh, PAM_SERVICE, &service);
    136 	if (service == NULL)
    137 		service = "unknown";
    138 
    139 	PAM_LOG("Got service: %s", (const char *)service);
    140 
    141 	krbret = krb5_init_context(&pam_context);
    142 	if (krbret != 0) {
    143 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    144 		return (PAM_SERVICE_ERR);
    145 	}
    146 
    147 	PAM_LOG("Context initialised");
    148 
    149 	krb5_get_init_creds_opt_init(&opts);
    150 
    151 	if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
    152 		krb5_get_init_creds_opt_set_forwardable(&opts, 1);
    153 
    154 	PAM_LOG("Credentials initialised");
    155 
    156 	krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE);
    157 	if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) {
    158 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    159 		retval = PAM_SERVICE_ERR;
    160 		goto cleanup3;
    161 	}
    162 
    163 	PAM_LOG("Done krb5_cc_register()");
    164 
    165 	/* Get principal name */
    166 	if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
    167 		asprintf(&principal, "%s/%s", (const char *)sourceuser, user);
    168 	else
    169 		principal = strdup(user);
    170 
    171 	PAM_LOG("Created principal: %s", principal);
    172 
    173 	krbret = krb5_parse_name(pam_context, principal, &princ);
    174 	free(principal);
    175 	if (krbret != 0) {
    176 		PAM_LOG("Error krb5_parse_name(): %s",
    177 		    krb5_get_err_text(pam_context, krbret));
    178 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    179 		retval = PAM_SERVICE_ERR;
    180 		goto cleanup3;
    181 	}
    182 
    183 	PAM_LOG("Done krb5_parse_name()");
    184 
    185 	/* Now convert the principal name into something human readable */
    186 	princ_name = NULL;
    187 	krbret = krb5_unparse_name(pam_context, princ, &princ_name);
    188 	if (krbret != 0) {
    189 		PAM_LOG("Error krb5_unparse_name(): %s",
    190 		    krb5_get_err_text(pam_context, krbret));
    191 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    192 		retval = PAM_SERVICE_ERR;
    193 		goto cleanup2;
    194 	}
    195 
    196 	PAM_LOG("Got principal: %s", princ_name);
    197 
    198 	/* Get password */
    199 	(void) snprintf(password_prompt, sizeof(password_prompt),
    200 	    PASSWORD_PROMPT, princ_name);
    201 	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, password_prompt);
    202 	if (retval != PAM_SUCCESS)
    203 		goto cleanup2;
    204 
    205 	PAM_LOG("Got password");
    206 
    207 	/* Verify the local user exists (AFTER getting the password) */
    208 	if (strchr(user, '@')) {
    209 		/* get a local account name for this principal */
    210 		krbret = krb5_aname_to_localname(pam_context, princ,
    211 		    sizeof(luser), luser);
    212 		if (krbret != 0) {
    213 			PAM_VERBOSE_ERROR("Kerberos 5 error");
    214 			PAM_LOG("Error krb5_aname_to_localname(): %s",
    215 			    krb5_get_err_text(pam_context, krbret));
    216 			retval = PAM_USER_UNKNOWN;
    217 			goto cleanup2;
    218 		}
    219 
    220 		retval = pam_set_item(pamh, PAM_USER, luser);
    221 		if (retval != PAM_SUCCESS)
    222 			goto cleanup2;
    223 
    224 		PAM_LOG("PAM_USER Redone");
    225 	}
    226 
    227 	if (getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
    228 	    pwd == NULL) {
    229 		retval = PAM_USER_UNKNOWN;
    230 		goto cleanup2;
    231 	}
    232 
    233 	PAM_LOG("Done getpwnam_r()");
    234 
    235 	/* Get a TGT */
    236 	memset(&creds, 0, sizeof(krb5_creds));
    237 	krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
    238 	    pass, NULL, pamh, 0, NULL, &opts);
    239 	if (krbret != 0) {
    240 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    241 		PAM_LOG("Error krb5_get_init_creds_password(): %s",
    242 		    krb5_get_err_text(pam_context, krbret));
    243 		retval = PAM_AUTH_ERR;
    244 		goto cleanup2;
    245 	}
    246 
    247 	PAM_LOG("Got TGT");
    248 
    249 	/* Generate a temporary cache */
    250 	krbret = krb5_cc_gen_new(pam_context, &krb5_mcc_ops, &ccache);
    251 	if (krbret != 0) {
    252 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    253 		PAM_LOG("Error krb5_cc_gen_new(): %s",
    254 		    krb5_get_err_text(pam_context, krbret));
    255 		retval = PAM_SERVICE_ERR;
    256 		goto cleanup;
    257 	}
    258 	krbret = krb5_cc_initialize(pam_context, ccache, princ);
    259 	if (krbret != 0) {
    260 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    261 		PAM_LOG("Error krb5_cc_initialize(): %s",
    262 		    krb5_get_err_text(pam_context, krbret));
    263 		retval = PAM_SERVICE_ERR;
    264 		goto cleanup;
    265 	}
    266 	krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
    267 	if (krbret != 0) {
    268 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    269 		PAM_LOG("Error krb5_cc_store_cred(): %s",
    270 		    krb5_get_err_text(pam_context, krbret));
    271 		krb5_cc_destroy(pam_context, ccache);
    272 		retval = PAM_SERVICE_ERR;
    273 		goto cleanup;
    274 	}
    275 
    276 	PAM_LOG("Credentials stashed");
    277 
    278 	/* Verify them */
    279 	if ((srvdup = strdup(service)) == NULL) {
    280 		retval = PAM_BUF_ERR;
    281 		goto cleanup;
    282 	}
    283 	krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
    284 	    openpam_get_option(pamh, PAM_OPT_DEBUG) ? 1 : 0);
    285 	free(srvdup);
    286 	if (krbret == -1) {
    287 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    288 		krb5_cc_destroy(pam_context, ccache);
    289 		retval = PAM_AUTH_ERR;
    290 		goto cleanup;
    291 	}
    292 
    293 	PAM_LOG("Credentials stash verified");
    294 
    295 	retval = pam_get_data(pamh, "ccache", &ccache_data);
    296 	if (retval == PAM_SUCCESS) {
    297 		krb5_cc_destroy(pam_context, ccache);
    298 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    299 		retval = PAM_AUTH_ERR;
    300 		goto cleanup;
    301 	}
    302 
    303 	PAM_LOG("Credentials stash not pre-existing");
    304 
    305 	asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
    306 		ccache), krb5_cc_get_name(pam_context, ccache));
    307 	if (ccache_name == NULL) {
    308 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    309 		retval = PAM_BUF_ERR;
    310 		goto cleanup;
    311 	}
    312 	retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache);
    313 	if (retval != 0) {
    314 		krb5_cc_destroy(pam_context, ccache);
    315 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    316 		retval = PAM_SERVICE_ERR;
    317 		goto cleanup;
    318 	}
    319 
    320 	PAM_LOG("Credentials stash saved");
    321 
    322 cleanup:
    323 	krb5_free_cred_contents(pam_context, &creds);
    324 	PAM_LOG("Done cleanup");
    325 cleanup2:
    326 	krb5_free_principal(pam_context, princ);
    327 	PAM_LOG("Done cleanup2");
    328 cleanup3:
    329 	if (princ_name)
    330 		free(princ_name);
    331 
    332 	krb5_free_context(pam_context);
    333 
    334 	PAM_LOG("Done cleanup3");
    335 
    336 	if (retval != PAM_SUCCESS)
    337 		PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
    338 
    339 	return (retval);
    340 }
    341 
    342 PAM_EXTERN int
    343 pam_sm_setcred(pam_handle_t *pamh, int flags,
    344     int argc __unused, const char *argv[] __unused)
    345 {
    346 
    347 	krb5_error_code krbret;
    348 	krb5_context pam_context;
    349 	krb5_principal princ;
    350 	krb5_creds creds;
    351 	krb5_ccache ccache_temp, ccache_perm;
    352 	krb5_cc_cursor cursor;
    353 	struct passwd *pwd = NULL, pwres;
    354 	int retval;
    355 	const char *cache_name, *q;
    356 	const void *user;
    357 	void *cache_data;
    358 	char *cache_name_buf = NULL, *p;
    359 	char pwbuf[1024];
    360 
    361 	uid_t euid;
    362 	gid_t egid;
    363 
    364 	if (flags & PAM_DELETE_CRED)
    365 		return (PAM_SUCCESS);
    366 
    367 	if (flags & PAM_REFRESH_CRED)
    368 		return (PAM_SUCCESS);
    369 
    370 	if (flags & PAM_REINITIALIZE_CRED)
    371 		return (PAM_SUCCESS);
    372 
    373 	if (!(flags & PAM_ESTABLISH_CRED))
    374 		return (PAM_SERVICE_ERR);
    375 
    376 	/* If a persistent cache isn't desired, stop now. */
    377 	if (openpam_get_option(pamh, PAM_OPT_NO_CCACHE))
    378 		return (PAM_SUCCESS);
    379 
    380 	PAM_LOG("Establishing credentials");
    381 
    382 	/* Get username */
    383 	retval = pam_get_item(pamh, PAM_USER, &user);
    384 	if (retval != PAM_SUCCESS)
    385 		return (retval);
    386 
    387 	PAM_LOG("Got user: %s", (const char *)user);
    388 
    389 	krbret = krb5_init_context(&pam_context);
    390 	if (krbret != 0) {
    391 		PAM_LOG("Error krb5_init_context() failed");
    392 		return (PAM_SERVICE_ERR);
    393 	}
    394 
    395 	PAM_LOG("Context initialised");
    396 
    397 	euid = geteuid();	/* Usually 0 */
    398 	egid = getegid();
    399 
    400 	PAM_LOG("Got euid, egid: %d %d", euid, egid);
    401 
    402 	/* Retrieve the temporary cache */
    403 	retval = pam_get_data(pamh, "ccache", &cache_data);
    404 	if (retval != PAM_SUCCESS) {
    405 		retval = PAM_CRED_UNAVAIL;
    406 		goto cleanup3;
    407 	}
    408 	krbret = krb5_cc_resolve(pam_context, cache_data, &ccache_temp);
    409 	if (krbret != 0) {
    410 		PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)cache_data,
    411 		    krb5_get_err_text(pam_context, krbret));
    412 		retval = PAM_SERVICE_ERR;
    413 		goto cleanup3;
    414 	}
    415 
    416 	/* Get the uid. This should exist. */
    417 	if (getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0) {
    418 		retval = PAM_USER_UNKNOWN;
    419 		goto cleanup3;
    420 	}
    421 
    422 	PAM_LOG("Done getpwnam_r()");
    423 
    424 	/* Avoid following a symlink as root */
    425 	if (setegid(pwd->pw_gid)) {
    426 		retval = PAM_SERVICE_ERR;
    427 		goto cleanup3;
    428 	}
    429 	if (seteuid(pwd->pw_uid)) {
    430 		retval = PAM_SERVICE_ERR;
    431 		goto cleanup3;
    432 	}
    433 
    434 	PAM_LOG("Done setegid() & seteuid()");
    435 
    436 	/* Get the cache name */
    437 	cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
    438 	if (cache_name == NULL) {
    439 		asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
    440 		cache_name = cache_name_buf;
    441 	}
    442 
    443 	p = calloc(PATH_MAX + 16, sizeof(char));
    444 	q = cache_name;
    445 
    446 	if (p == NULL) {
    447 		PAM_LOG("Error malloc(): failure");
    448 		retval = PAM_BUF_ERR;
    449 		goto cleanup3;
    450 	}
    451 	cache_name = p;
    452 
    453 	/* convert %u and %p */
    454 	while (*q) {
    455 		if (*q == '%') {
    456 			q++;
    457 			if (*q == 'u') {
    458 				sprintf(p, "%d", pwd->pw_uid);
    459 				p += strlen(p);
    460 			}
    461 			else if (*q == 'p') {
    462 				sprintf(p, "%d", getpid());
    463 				p += strlen(p);
    464 			}
    465 			else {
    466 				/* Not a special token */
    467 				*p++ = '%';
    468 				q--;
    469 			}
    470 			q++;
    471 		}
    472 		else {
    473 			*p++ = *q++;
    474 		}
    475 	}
    476 
    477 	PAM_LOG("Got cache_name: %s", cache_name);
    478 
    479 	/* Initialize the new ccache */
    480 	krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
    481 	if (krbret != 0) {
    482 		PAM_LOG("Error krb5_cc_get_principal(): %s",
    483 		    krb5_get_err_text(pam_context, krbret));
    484 		retval = PAM_SERVICE_ERR;
    485 		goto cleanup3;
    486 	}
    487 	krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
    488 	if (krbret != 0) {
    489 		PAM_LOG("Error krb5_cc_resolve(): %s",
    490 		    krb5_get_err_text(pam_context, krbret));
    491 		retval = PAM_SERVICE_ERR;
    492 		goto cleanup2;
    493 	}
    494 	krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
    495 	if (krbret != 0) {
    496 		PAM_LOG("Error krb5_cc_initialize(): %s",
    497 		    krb5_get_err_text(pam_context, krbret));
    498 		retval = PAM_SERVICE_ERR;
    499 		goto cleanup2;
    500 	}
    501 
    502 	PAM_LOG("Cache initialised");
    503 
    504 	/* Prepare for iteration over creds */
    505 	krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
    506 	if (krbret != 0) {
    507 		PAM_LOG("Error krb5_cc_start_seq_get(): %s",
    508 		    krb5_get_err_text(pam_context, krbret));
    509 		krb5_cc_destroy(pam_context, ccache_perm);
    510 		retval = PAM_SERVICE_ERR;
    511 		goto cleanup2;
    512 	}
    513 
    514 	PAM_LOG("Prepared for iteration");
    515 
    516 	/* Copy the creds (should be two of them) */
    517 	while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
    518 				&cursor, &creds) == 0)) {
    519 		krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
    520 		if (krbret != 0) {
    521 			PAM_LOG("Error krb5_cc_store_cred(): %s",
    522 			    krb5_get_err_text(pam_context, krbret));
    523 			krb5_cc_destroy(pam_context, ccache_perm);
    524 			krb5_free_cred_contents(pam_context, &creds);
    525 			retval = PAM_SERVICE_ERR;
    526 			goto cleanup2;
    527 		}
    528 		krb5_free_cred_contents(pam_context, &creds);
    529 		PAM_LOG("Iteration");
    530 	}
    531 	krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
    532 
    533 	PAM_LOG("Done iterating");
    534 
    535 	if (strstr(cache_name, "FILE:") == cache_name) {
    536 		if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) {
    537 			PAM_LOG("Error chown(): %s", strerror(errno));
    538 			krb5_cc_destroy(pam_context, ccache_perm);
    539 			retval = PAM_SERVICE_ERR;
    540 			goto cleanup2;
    541 		}
    542 		PAM_LOG("Done chown()");
    543 
    544 		if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) {
    545 			PAM_LOG("Error chmod(): %s", strerror(errno));
    546 			krb5_cc_destroy(pam_context, ccache_perm);
    547 			retval = PAM_SERVICE_ERR;
    548 			goto cleanup2;
    549 		}
    550 		PAM_LOG("Done chmod()");
    551 	}
    552 
    553 	krb5_cc_close(pam_context, ccache_perm);
    554 
    555 	PAM_LOG("Cache closed");
    556 
    557 	retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1);
    558 	if (retval != PAM_SUCCESS) {
    559 		PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
    560 		krb5_cc_destroy(pam_context, ccache_perm);
    561 		retval = PAM_SERVICE_ERR;
    562 		goto cleanup2;
    563 	}
    564 
    565 	PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
    566 
    567 cleanup2:
    568 	krb5_free_principal(pam_context, princ);
    569 	PAM_LOG("Done cleanup2");
    570 cleanup3:
    571 	krb5_free_context(pam_context);
    572 	PAM_LOG("Done cleanup3");
    573 
    574 	seteuid(euid);
    575 	setegid(egid);
    576 
    577 	PAM_LOG("Done seteuid() & setegid()");
    578 
    579 	if (cache_name_buf != NULL)
    580 		free(cache_name_buf);
    581 
    582 	return (retval);
    583 }
    584 
    585 /*
    586  * account management
    587  */
    588 PAM_EXTERN int
    589 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
    590     int argc __unused, const char *argv[] __unused)
    591 {
    592 	krb5_error_code krbret;
    593 	krb5_context pam_context;
    594 	krb5_ccache ccache;
    595 	krb5_principal princ;
    596 	int retval;
    597 	const void *user;
    598 	void *ccache_name;
    599 
    600 	retval = pam_get_item(pamh, PAM_USER, &user);
    601 	if (retval != PAM_SUCCESS)
    602 		return (retval);
    603 
    604 	PAM_LOG("Got user: %s", (const char *)user);
    605 
    606 	retval = pam_get_data(pamh, "ccache", &ccache_name);
    607 	if (retval != PAM_SUCCESS)
    608 		return (PAM_SUCCESS);
    609 
    610 	PAM_LOG("Got credentials");
    611 
    612 	krbret = krb5_init_context(&pam_context);
    613 	if (krbret != 0) {
    614 		PAM_LOG("Error krb5_init_context() failed");
    615 		return (PAM_PERM_DENIED);
    616 	}
    617 
    618 	PAM_LOG("Context initialised");
    619 
    620 	krbret = krb5_cc_resolve(pam_context, (const char *)ccache_name, &ccache);
    621 	if (krbret != 0) {
    622 		PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)ccache_name,
    623 		    krb5_get_err_text(pam_context, krbret));
    624 		krb5_free_context(pam_context);
    625 		return (PAM_PERM_DENIED);
    626 	}
    627 
    628 	PAM_LOG("Got ccache %s", (const char *)ccache_name);
    629 
    630 
    631 	krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
    632 	if (krbret != 0) {
    633 		PAM_LOG("Error krb5_cc_get_principal(): %s",
    634 		    krb5_get_err_text(pam_context, krbret));
    635 		retval = PAM_PERM_DENIED;;
    636 		goto cleanup;
    637 	}
    638 
    639 	PAM_LOG("Got principal");
    640 
    641 	if (krb5_kuserok(pam_context, princ, (const char *)user))
    642 		retval = PAM_SUCCESS;
    643 	else
    644 		retval = PAM_PERM_DENIED;
    645 	krb5_free_principal(pam_context, princ);
    646 
    647 	PAM_LOG("Done kuserok()");
    648 
    649 cleanup:
    650 	krb5_free_context(pam_context);
    651 	PAM_LOG("Done cleanup");
    652 
    653 	return (retval);
    654 
    655 }
    656 
    657 /*
    658  * password management
    659  */
    660 PAM_EXTERN int
    661 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
    662     int argc __unused, const char *argv[] __unused)
    663 {
    664 	krb5_error_code krbret;
    665 	krb5_context pam_context;
    666 	krb5_creds creds;
    667 	krb5_principal princ;
    668 	krb5_get_init_creds_opt opts;
    669 	krb5_data result_code_string, result_string;
    670 	int result_code, retval;
    671 	const char *pass;
    672 	const void *user;
    673 	char *princ_name, *passdup;
    674 	char password_prompt[80];
    675 
    676 	if (flags & PAM_PRELIM_CHECK) {
    677 		/* Nothing to do here. */
    678 		return (PAM_SUCCESS);
    679 	}
    680 
    681 	if (!(flags & PAM_UPDATE_AUTHTOK)) {
    682 		PAM_LOG("Illegal flags argument");
    683 		return (PAM_ABORT);
    684 	}
    685 
    686 	retval = pam_get_item(pamh, PAM_USER, &user);
    687 	if (retval != PAM_SUCCESS)
    688 		return (retval);
    689 
    690 	PAM_LOG("Got user: %s", (const char *)user);
    691 
    692 	krbret = krb5_init_context(&pam_context);
    693 	if (krbret != 0) {
    694 		PAM_LOG("Error krb5_init_context() failed");
    695 		return (PAM_SERVICE_ERR);
    696 	}
    697 
    698 	PAM_LOG("Context initialised");
    699 
    700 	krb5_get_init_creds_opt_init(&opts);
    701 
    702 	krb5_get_init_creds_opt_set_tkt_life(&opts, 300);
    703 	krb5_get_init_creds_opt_set_forwardable(&opts, FALSE);
    704 	krb5_get_init_creds_opt_set_proxiable(&opts, FALSE);
    705 
    706 	PAM_LOG("Credentials options initialised");
    707 
    708 	/* Get principal name */
    709 	krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
    710 	if (krbret != 0) {
    711 		PAM_LOG("Error krb5_parse_name(): %s",
    712 		    krb5_get_err_text(pam_context, krbret));
    713 		retval = PAM_USER_UNKNOWN;
    714 		goto cleanup3;
    715 	}
    716 
    717 	/* Now convert the principal name into something human readable */
    718 	princ_name = NULL;
    719 	krbret = krb5_unparse_name(pam_context, princ, &princ_name);
    720 	if (krbret != 0) {
    721 		PAM_LOG("Error krb5_unparse_name(): %s",
    722 		    krb5_get_err_text(pam_context, krbret));
    723 		retval = PAM_SERVICE_ERR;
    724 		goto cleanup2;
    725 	}
    726 
    727 	PAM_LOG("Got principal: %s", princ_name);
    728 
    729 	/* Get password */
    730 	(void) snprintf(password_prompt, sizeof(password_prompt),
    731 	    PASSWORD_PROMPT, princ_name);
    732 	retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, password_prompt);
    733 	if (retval != PAM_SUCCESS)
    734 		goto cleanup2;
    735 
    736 	PAM_LOG("Got password");
    737 
    738 	memset(&creds, 0, sizeof(krb5_creds));
    739 	krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
    740 	    pass, NULL, pamh, 0, "kadmin/changepw", &opts);
    741 	if (krbret != 0) {
    742 		PAM_LOG("Error krb5_get_init_creds_password(): %s",
    743 		    krb5_get_err_text(pam_context, krbret));
    744 		retval = PAM_AUTH_ERR;
    745 		goto cleanup2;
    746 	}
    747 
    748 	PAM_LOG("Credentials established");
    749 
    750 	/* Now get the new password */
    751 	for (;;) {
    752 		retval = pam_get_authtok(pamh,
    753 		    PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
    754 		if (retval != PAM_TRY_AGAIN)
    755 			break;
    756 		pam_error(pamh, "Mismatch; try again, EOF to quit.");
    757 	}
    758 	if (retval != PAM_SUCCESS)
    759 		goto cleanup;
    760 
    761 	PAM_LOG("Got new password");
    762 
    763 	/* Change it */
    764 	if ((passdup = strdup(pass)) == NULL) {
    765 		retval = PAM_BUF_ERR;
    766 		goto cleanup;
    767 	}
    768 
    769 	krb5_data_zero(&result_code_string);
    770 	krb5_data_zero(&result_string);
    771 
    772 	krbret = krb5_change_password(pam_context, &creds, passdup,
    773 	    &result_code, &result_code_string, &result_string);
    774 	free(passdup);
    775 	if (krbret != 0) {
    776 		pam_error(pamh, "Unable to set password: %s",
    777 		    krb5_get_err_text(pam_context, krbret));
    778 		retval = PAM_AUTHTOK_ERR;
    779 		goto cleanup;
    780 	}
    781 	if (result_code) {
    782 		pam_info(pamh, "%s%s%.*s",
    783 		    krb5_passwd_result_to_string(pam_context, result_code),
    784 		    result_string.length > 0 ? ": " : "",
    785 		    (int)result_string.length,
    786 		    result_string.length > 0 ? (char *)result_string.data : "");
    787 		retval = PAM_AUTHTOK_ERR;
    788 	} else {
    789 		PAM_LOG("Password changed");
    790 	}
    791 
    792 	krb5_data_free(&result_string);
    793 	krb5_data_free(&result_code_string);
    794 
    795 cleanup:
    796 	krb5_free_cred_contents(pam_context, &creds);
    797 	PAM_LOG("Done cleanup");
    798 cleanup2:
    799 	krb5_free_principal(pam_context, princ);
    800 	PAM_LOG("Done cleanup2");
    801 cleanup3:
    802 	if (princ_name)
    803 		free(princ_name);
    804 
    805 	krb5_free_context(pam_context);
    806 
    807 	PAM_LOG("Done cleanup3");
    808 
    809 	return (retval);
    810 }
    811 
    812 PAM_MODULE_ENTRY("pam_krb5");
    813 
    814 /*
    815  * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
    816  * Modified by Sam Hartman <hartmans (at) mit.edu> to support PAM services
    817  * for Debian.
    818  *
    819  * Verify the Kerberos ticket-granting ticket just retrieved for the
    820  * user.  If the Kerberos server doesn't respond, assume the user is
    821  * trying to fake us out (since we DID just get a TGT from what is
    822  * supposedly our KDC).  If the host/<host> service is unknown (i.e.,
    823  * the local keytab doesn't have it), and we cannot find another
    824  * service we do have, let her in.
    825  *
    826  * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
    827  */
    828 /* ARGSUSED */
    829 static int
    830 verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
    831     char *pam_service, int debug)
    832 {
    833 	krb5_error_code retval;
    834 	krb5_principal princ;
    835 	krb5_keyblock *keyblock;
    836 	krb5_data packet;
    837 	krb5_auth_context auth_context;
    838 	char phost[BUFSIZ];
    839 	const char *services[3], **service;
    840 
    841 	packet.data = 0;
    842 
    843 	/* If possible we want to try and verify the ticket we have
    844 	 * received against a keytab.  We will try multiple service
    845 	 * principals, including at least the host principal and the PAM
    846 	 * service principal.  The host principal is preferred because access
    847 	 * to that key is generally sufficient to compromise root, while the
    848 	 * service key for this PAM service may be less carefully guarded.
    849 	 * It is important to check the keytab first before the KDC so we do
    850 	 * not get spoofed by a fake KDC.
    851 	 */
    852 	services[0] = "host";
    853 	services[1] = pam_service;
    854 	services[2] = NULL;
    855 	keyblock = 0;
    856 	retval = -1;
    857 	for (service = &services[0]; *service != NULL; service++) {
    858 		retval = krb5_sname_to_principal(context, NULL, *service,
    859 		    KRB5_NT_SRV_HST, &princ);
    860 		if (retval != 0) {
    861 			if (debug)
    862 				syslog(LOG_DEBUG,
    863 				    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
    864 				    "krb5_sname_to_principal()",
    865 				    krb5_get_err_text(context, retval));
    866 			return -1;
    867 		}
    868 
    869 		/* Extract the name directly. */
    870 		strncpy(phost, compat_princ_component(context, princ, 1),
    871 		    BUFSIZ);
    872 		phost[BUFSIZ - 1] = '\0';
    873 
    874 		/*
    875 		 * Do we have service/<host> keys?
    876 		 * (use default/configured keytab, kvno IGNORE_VNO to get the
    877 		 * first match, and ignore enctype.)
    878 		 */
    879 		retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
    880 		    &keyblock);
    881 		if (retval != 0)
    882 			continue;
    883 		break;
    884 	}
    885 	if (retval != 0) {	/* failed to find key */
    886 		/* Keytab or service key does not exist */
    887 		if (debug)
    888 			syslog(LOG_DEBUG,
    889 			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
    890 			    "krb5_kt_read_service_key()",
    891 			    krb5_get_err_text(context, retval));
    892 		retval = 0;
    893 		goto cleanup;
    894 	}
    895 	if (keyblock)
    896 		krb5_free_keyblock(context, keyblock);
    897 
    898 	/* Talk to the kdc and construct the ticket. */
    899 	auth_context = NULL;
    900 	retval = krb5_mk_req(context, &auth_context, 0, *service, phost,
    901 		NULL, ccache, &packet);
    902 	if (auth_context) {
    903 		krb5_auth_con_free(context, auth_context);
    904 		auth_context = NULL;	/* setup for rd_req */
    905 	}
    906 	if (retval) {
    907 		if (debug)
    908 			syslog(LOG_DEBUG,
    909 			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
    910 			    "krb5_mk_req()",
    911 			    krb5_get_err_text(context, retval));
    912 		retval = -1;
    913 		goto cleanup;
    914 	}
    915 
    916 	/* Try to use the ticket. */
    917 	retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
    918 	    NULL, NULL);
    919 	if (retval) {
    920 		if (debug)
    921 			syslog(LOG_DEBUG,
    922 			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
    923 			    "krb5_rd_req()",
    924 			    krb5_get_err_text(context, retval));
    925 		retval = -1;
    926 	}
    927 	else
    928 		retval = 1;
    929 
    930 cleanup:
    931 	if (packet.data)
    932 		compat_free_data_contents(context, &packet);
    933 	krb5_free_principal(context, princ);
    934 	return retval;
    935 }
    936 
    937 /* Free the memory for cache_name. Called by pam_end() */
    938 /* ARGSUSED */
    939 static void
    940 cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
    941 {
    942 	krb5_context pam_context;
    943 	krb5_ccache ccache;
    944 	krb5_error_code krbret;
    945 
    946 	if (krb5_init_context(&pam_context))
    947 		return;
    948 
    949 	krbret = krb5_cc_resolve(pam_context, data, &ccache);
    950 	if (krbret == 0)
    951 		krb5_cc_destroy(pam_context, ccache);
    952 	krb5_free_context(pam_context);
    953 	free(data);
    954 }
    955 
    956 #ifdef COMPAT_HEIMDAL
    957 #ifdef COMPAT_MIT
    958 #error This cannot be MIT and Heimdal compatible!
    959 #endif
    960 #endif
    961 
    962 #ifndef COMPAT_HEIMDAL
    963 #ifndef COMPAT_MIT
    964 #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
    965 #endif
    966 #endif
    967 
    968 #ifdef COMPAT_HEIMDAL
    969 /* ARGSUSED */
    970 static const char *
    971 compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
    972 {
    973 	return princ->name.name_string.val[n];
    974 }
    975 
    976 /* ARGSUSED */
    977 static void
    978 compat_free_data_contents(krb5_context context __unused, krb5_data * data)
    979 {
    980 	krb5_xfree(data->data);
    981 }
    982 #endif
    983 
    984 #ifdef COMPAT_MIT
    985 static const char *
    986 compat_princ_component(krb5_context context, krb5_principal princ, int n)
    987 {
    988 	return krb5_princ_component(context, princ, n)->data;
    989 }
    990 
    991 static void
    992 compat_free_data_contents(krb5_context context, krb5_data * data)
    993 {
    994 	krb5_free_data_contents(context, data);
    995 }
    996 #endif
    997