181477Smarkm/*-
2109069Snectar * This pam_krb5 module contains code that is:
3109069Snectar *   Copyright (c) Derrick J. Brashear, 1996. All rights reserved.
4109069Snectar *   Copyright (c) Frank Cusack, 1999-2001. All rights reserved.
5109069Snectar *   Copyright (c) Jacques A. Vidrine, 2000-2001. All rights reserved.
6109069Snectar *   Copyright (c) Nicolas Williams, 2001. All rights reserved.
7109069Snectar *   Copyright (c) Perot Systems Corporation, 2001. All rights reserved.
8109069Snectar *   Copyright (c) Mark R V Murray, 2001.  All rights reserved.
9140667Srwatson *   Copyright (c) Networks Associates Technology, Inc., 2002-2005.
10109069Snectar *       All rights reserved.
1194564Sdes *
1293984Sdes * Portions of this software were developed for the FreeBSD Project by
1393984Sdes * ThinkSec AS and NAI Labs, the Security Research Division of Network
1493984Sdes * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
1593984Sdes * ("CBOSS"), as part of the DARPA CHATS research program.
16110274Sdes *
1781477Smarkm * Redistribution and use in source and binary forms, with or without
1881477Smarkm * modification, are permitted provided that the following conditions
1981477Smarkm * are met:
2081477Smarkm * 1. Redistributions of source code must retain the above copyright
21109069Snectar *    notices, and the entire permission notice in its entirety,
2281477Smarkm *    including the disclaimer of warranties.
2381477Smarkm * 2. Redistributions in binary form must reproduce the above copyright
2481477Smarkm *    notice, this list of conditions and the following disclaimer in the
2581477Smarkm *    documentation and/or other materials provided with the distribution.
2681477Smarkm * 3. The name of the author may not be used to endorse or promote
2781477Smarkm *    products derived from this software without specific prior
2881477Smarkm *    written permission.
29110274Sdes *
3081477Smarkm * ALTERNATIVELY, this product may be distributed under the terms of
3181477Smarkm * the GNU Public License, in which case the provisions of the GPL are
3281477Smarkm * required INSTEAD OF the above restrictions.  (This clause is
3381477Smarkm * necessary due to a potential bad interaction between the GPL and
3481477Smarkm * the restrictions contained in a BSD-style copyright.)
35110274Sdes *
3681477Smarkm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
3781477Smarkm * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3881477Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3981477Smarkm * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
4081477Smarkm * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
4181477Smarkm * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
4281477Smarkm * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4381477Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4481477Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4581477Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4681477Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
4794564Sdes *
4881477Smarkm */
4981477Smarkm
5084218Sdillon#include <sys/cdefs.h>
5184218Sdillon__FBSDID("$FreeBSD: releng/10.2/lib/libpam/modules/pam_krb5/pam_krb5.c 241844 2012-10-22 03:00:37Z eadler $");
5284218Sdillon
5381477Smarkm#include <sys/types.h>
5481477Smarkm#include <sys/stat.h>
5581477Smarkm#include <errno.h>
5681477Smarkm#include <limits.h>
5781477Smarkm#include <pwd.h>
5881477Smarkm#include <stdio.h>
5981477Smarkm#include <stdlib.h>
6093984Sdes#include <string.h>
6181477Smarkm#include <syslog.h>
6281477Smarkm#include <unistd.h>
6381477Smarkm
6481477Smarkm#include <krb5.h>
6581477Smarkm#include <com_err.h>
6681477Smarkm
6781477Smarkm#define	PAM_SM_AUTH
6881477Smarkm#define	PAM_SM_ACCOUNT
6981477Smarkm#define	PAM_SM_PASSWORD
7081477Smarkm
7181477Smarkm#include <security/pam_appl.h>
7281477Smarkm#include <security/pam_modules.h>
7390229Sdes#include <security/pam_mod_misc.h>
74115465Sdes#include <security/openpam.h>
7581477Smarkm
7681477Smarkm#define	COMPAT_HEIMDAL
7781477Smarkm/* #define	COMPAT_MIT */
7881477Smarkm
7981477Smarkmstatic int	verify_krb_v5_tgt(krb5_context, krb5_ccache, char *, int);
8081477Smarkmstatic void	cleanup_cache(pam_handle_t *, void *, int);
8181477Smarkmstatic const	char *compat_princ_component(krb5_context, krb5_principal, int);
8281477Smarkmstatic void	compat_free_data_contents(krb5_context, krb5_data *);
8381477Smarkm
8481477Smarkm#define USER_PROMPT		"Username: "
8585485Ssobomax#define PASSWORD_PROMPT		"Password:"
8685485Ssobomax#define NEW_PASSWORD_PROMPT	"New Password:"
8781477Smarkm
88115465Sdes#define PAM_OPT_CCACHE		"ccache"
89140667Srwatson#define PAM_OPT_DEBUG		"debug"
90115465Sdes#define PAM_OPT_FORWARDABLE	"forwardable"
91115465Sdes#define PAM_OPT_NO_CCACHE	"no_ccache"
92207553Smm#define PAM_OPT_NO_USER_CHECK	"no_user_check"
93115465Sdes#define PAM_OPT_REUSE_CCACHE	"reuse_ccache"
94239062Sdfr#define PAM_OPT_NO_USER_CHECK	"no_user_check"
9581477Smarkm
96233406Sstas#define	PAM_LOG_KRB5_ERR(ctx, rv, fmt, ...)				\
97233406Sstas	do {								\
98233406Sstas		const char *krb5msg = krb5_get_error_message(ctx, rv);	\
99233406Sstas		PAM_LOG(fmt ": %s", ##__VA_ARGS__, krb5msg);		\
100233406Sstas		krb5_free_error_message(ctx, krb5msg);			\
101233406Sstas	} while (0)
102233406Sstas
10381477Smarkm/*
10481477Smarkm * authentication management
10581477Smarkm */
10681477SmarkmPAM_EXTERN int
10794564Sdespam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
108115465Sdes    int argc __unused, const char *argv[] __unused)
10981477Smarkm{
11081477Smarkm	krb5_error_code krbret;
11181477Smarkm	krb5_context pam_context;
11281477Smarkm	krb5_creds creds;
11381477Smarkm	krb5_principal princ;
114106864Snectar	krb5_ccache ccache;
115233406Sstas	krb5_get_init_creds_opt *opts;
11681477Smarkm	struct passwd *pwd;
11781477Smarkm	int retval;
118174837Sdes	const void *ccache_data;
119123454Sdes	const char *user, *pass;
120125650Sdes	const void *sourceuser, *service;
121106864Snectar	char *principal, *princ_name, *ccache_name, luser[32], *srvdup;
12281477Smarkm
12381477Smarkm	retval = pam_get_user(pamh, &user, USER_PROMPT);
12481477Smarkm	if (retval != PAM_SUCCESS)
12594564Sdes		return (retval);
12681477Smarkm
12781477Smarkm	PAM_LOG("Got user: %s", user);
12881477Smarkm
129123454Sdes	retval = pam_get_item(pamh, PAM_RUSER, &sourceuser);
13081477Smarkm	if (retval != PAM_SUCCESS)
13194564Sdes		return (retval);
13281477Smarkm
133123454Sdes	PAM_LOG("Got ruser: %s", (const char *)sourceuser);
13481477Smarkm
13581477Smarkm	service = NULL;
136123454Sdes	pam_get_item(pamh, PAM_SERVICE, &service);
13781477Smarkm	if (service == NULL)
13881477Smarkm		service = "unknown";
13981477Smarkm
140123454Sdes	PAM_LOG("Got service: %s", (const char *)service);
14181477Smarkm
14281477Smarkm	krbret = krb5_init_context(&pam_context);
14381477Smarkm	if (krbret != 0) {
14481477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
14594564Sdes		return (PAM_SERVICE_ERR);
14681477Smarkm	}
14781477Smarkm
14881477Smarkm	PAM_LOG("Context initialised");
14981477Smarkm
15081477Smarkm	krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE);
15181477Smarkm	if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) {
15281477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
15381477Smarkm		retval = PAM_SERVICE_ERR;
15481477Smarkm		goto cleanup3;
15581477Smarkm	}
15681477Smarkm
15781477Smarkm	PAM_LOG("Done krb5_cc_register()");
15881477Smarkm
15981477Smarkm	/* Get principal name */
160115465Sdes	if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
161123454Sdes		asprintf(&principal, "%s/%s", (const char *)sourceuser, user);
16281477Smarkm	else
16381477Smarkm		principal = strdup(user);
16481477Smarkm
16581477Smarkm	PAM_LOG("Created principal: %s", principal);
16681477Smarkm
16781477Smarkm	krbret = krb5_parse_name(pam_context, principal, &princ);
16881477Smarkm	free(principal);
16981477Smarkm	if (krbret != 0) {
170233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret, "Error krb5_parse_name()");
17181477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
17281477Smarkm		retval = PAM_SERVICE_ERR;
17381477Smarkm		goto cleanup3;
17481477Smarkm	}
17581477Smarkm
17681477Smarkm	PAM_LOG("Done krb5_parse_name()");
17781477Smarkm
17881477Smarkm	/* Now convert the principal name into something human readable */
17981477Smarkm	princ_name = NULL;
18081477Smarkm	krbret = krb5_unparse_name(pam_context, princ, &princ_name);
18181477Smarkm	if (krbret != 0) {
182233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
183233406Sstas		    "Error krb5_unparse_name()");
18481477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
18581477Smarkm		retval = PAM_SERVICE_ERR;
18681477Smarkm		goto cleanup2;
18781477Smarkm	}
18881477Smarkm
18981477Smarkm	PAM_LOG("Got principal: %s", princ_name);
19081477Smarkm
19181477Smarkm	/* Get password */
19293984Sdes	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
19381477Smarkm	if (retval != PAM_SUCCESS)
19481477Smarkm		goto cleanup2;
19581477Smarkm
19681477Smarkm	PAM_LOG("Got password");
19781477Smarkm
198207553Smm	if (openpam_get_option(pamh, PAM_OPT_NO_USER_CHECK))
199207553Smm		PAM_LOG("Skipping local user check");
200207553Smm	else {
201207553Smm
202207555Smm		/* Verify the local user exists (AFTER getting the password) */
203207555Smm		if (strchr(user, '@')) {
204207555Smm			/* get a local account name for this principal */
205207555Smm			krbret = krb5_aname_to_localname(pam_context, princ,
206207555Smm			    sizeof(luser), luser);
207207555Smm			if (krbret != 0) {
208207555Smm				PAM_VERBOSE_ERROR("Kerberos 5 error");
209233406Sstas				PAM_LOG_KRB5_ERR(pam_context, krbret,
210233406Sstas				    "Error krb5_aname_to_localname()");
211207555Smm				retval = PAM_USER_UNKNOWN;
212207555Smm				goto cleanup2;
213207555Smm			}
214207555Smm
215207555Smm			retval = pam_set_item(pamh, PAM_USER, luser);
216207555Smm			if (retval != PAM_SUCCESS)
217207555Smm				goto cleanup2;
218207555Smm
219207555Smm			PAM_LOG("PAM_USER Redone");
220207555Smm		}
221207555Smm
222239062Sdfr		if (!openpam_get_option(pamh, PAM_OPT_NO_USER_CHECK)) {
223239062Sdfr			pwd = getpwnam(user);
224239062Sdfr			if (pwd == NULL) {
225239062Sdfr				retval = PAM_USER_UNKNOWN;
226239062Sdfr				goto cleanup2;
227239062Sdfr			}
22881477Smarkm		}
22981477Smarkm
230207555Smm		PAM_LOG("Done getpwnam()");
23181477Smarkm	}
23281477Smarkm
233233406Sstas	/* Initialize credentials request options. */
234233406Sstas	krbret = krb5_get_init_creds_opt_alloc(pam_context, &opts);
235233406Sstas	if (krbret != 0) {
236233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
237233406Sstas		    "Error krb5_get_init_creds_opt_alloc()");
238233406Sstas		PAM_VERBOSE_ERROR("Kerberos 5 error");
239233406Sstas		retval = PAM_SERVICE_ERR;
240233406Sstas		goto cleanup2;
241233406Sstas	}
242233406Sstas
243233406Sstas	if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
244233406Sstas		krb5_get_init_creds_opt_set_forwardable(opts, 1);
245233406Sstas
246233406Sstas	PAM_LOG("Credential options initialised");
247233406Sstas
24881477Smarkm	/* Get a TGT */
24981477Smarkm	memset(&creds, 0, sizeof(krb5_creds));
25081477Smarkm	krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
251233406Sstas	    pass, NULL, pamh, 0, NULL, opts);
252233406Sstas	krb5_get_init_creds_opt_free(pam_context, opts);
25381477Smarkm	if (krbret != 0) {
25481477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
255233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
256233406Sstas		    "Error krb5_get_init_creds_password()");
25781477Smarkm		retval = PAM_AUTH_ERR;
25881477Smarkm		goto cleanup2;
25981477Smarkm	}
26081477Smarkm
26181477Smarkm	PAM_LOG("Got TGT");
26281477Smarkm
263106864Snectar	/* Generate a temporary cache */
264233406Sstas	krbret = krb5_cc_new_unique(pam_context, krb5_cc_type_memory, NULL, &ccache);
26581477Smarkm	if (krbret != 0) {
26681477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
267233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
268233406Sstas		    "Error krb5_cc_new_unique()");
26981477Smarkm		retval = PAM_SERVICE_ERR;
27081477Smarkm		goto cleanup;
27181477Smarkm	}
27281477Smarkm	krbret = krb5_cc_initialize(pam_context, ccache, princ);
27381477Smarkm	if (krbret != 0) {
27481477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
275233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
276233406Sstas		    "Error krb5_cc_initialize()");
27781477Smarkm		retval = PAM_SERVICE_ERR;
27881477Smarkm		goto cleanup;
27981477Smarkm	}
28081477Smarkm	krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
28181477Smarkm	if (krbret != 0) {
28281477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
283233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
284233406Sstas		    "Error krb5_cc_store_cred()");
28581477Smarkm		krb5_cc_destroy(pam_context, ccache);
28681477Smarkm		retval = PAM_SERVICE_ERR;
28781477Smarkm		goto cleanup;
28881477Smarkm	}
28981477Smarkm
29081477Smarkm	PAM_LOG("Credentials stashed");
29181477Smarkm
29281477Smarkm	/* Verify them */
29393984Sdes	if ((srvdup = strdup(service)) == NULL) {
29493984Sdes		retval = PAM_BUF_ERR;
29593984Sdes		goto cleanup;
29693984Sdes	}
29793984Sdes	krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
298140667Srwatson	    openpam_get_option(pamh, PAM_OPT_DEBUG) ? 1 : 0);
29993984Sdes	free(srvdup);
30093984Sdes	if (krbret == -1) {
30181477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
30281477Smarkm		krb5_cc_destroy(pam_context, ccache);
30381477Smarkm		retval = PAM_AUTH_ERR;
30481477Smarkm		goto cleanup;
30581477Smarkm	}
30681477Smarkm
30781477Smarkm	PAM_LOG("Credentials stash verified");
30881477Smarkm
309125650Sdes	retval = pam_get_data(pamh, "ccache", &ccache_data);
31081477Smarkm	if (retval == PAM_SUCCESS) {
31181477Smarkm		krb5_cc_destroy(pam_context, ccache);
31281477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
31381477Smarkm		retval = PAM_AUTH_ERR;
31481477Smarkm		goto cleanup;
31581477Smarkm	}
31681477Smarkm
31781477Smarkm	PAM_LOG("Credentials stash not pre-existing");
31881477Smarkm
319106864Snectar	asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
320106864Snectar		ccache), krb5_cc_get_name(pam_context, ccache));
321106864Snectar	if (ccache_name == NULL) {
322106864Snectar		PAM_VERBOSE_ERROR("Kerberos 5 error");
323106864Snectar		retval = PAM_BUF_ERR;
324106864Snectar		goto cleanup;
325106864Snectar	}
326106864Snectar	retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache);
32781477Smarkm	if (retval != 0) {
32881477Smarkm		krb5_cc_destroy(pam_context, ccache);
32981477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 error");
33081477Smarkm		retval = PAM_SERVICE_ERR;
33181477Smarkm		goto cleanup;
33281477Smarkm	}
33381477Smarkm
33481477Smarkm	PAM_LOG("Credentials stash saved");
33581477Smarkm
33681477Smarkmcleanup:
33781477Smarkm	krb5_free_cred_contents(pam_context, &creds);
33881477Smarkm	PAM_LOG("Done cleanup");
33981477Smarkmcleanup2:
34081477Smarkm	krb5_free_principal(pam_context, princ);
34181477Smarkm	if (princ_name)
34281477Smarkm		free(princ_name);
343239099Sdim	PAM_LOG("Done cleanup2");
34481477Smarkm
345239099Sdimcleanup3:
34681477Smarkm	krb5_free_context(pam_context);
34781477Smarkm
34881477Smarkm	PAM_LOG("Done cleanup3");
34981477Smarkm
35081477Smarkm	if (retval != PAM_SUCCESS)
35181477Smarkm		PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
35281477Smarkm
35394564Sdes	return (retval);
35481477Smarkm}
35581477Smarkm
35681477SmarkmPAM_EXTERN int
35794564Sdespam_sm_setcred(pam_handle_t *pamh, int flags,
358115465Sdes    int argc __unused, const char *argv[] __unused)
35981477Smarkm{
360147810Skensmith#ifdef _FREEFALL_CONFIG
361147810Skensmith	return (PAM_SUCCESS);
362147810Skensmith#else
36381477Smarkm
36481477Smarkm	krb5_error_code krbret;
36581477Smarkm	krb5_context pam_context;
36681477Smarkm	krb5_principal princ;
36781477Smarkm	krb5_creds creds;
36881477Smarkm	krb5_ccache ccache_temp, ccache_perm;
36981477Smarkm	krb5_cc_cursor cursor;
37081477Smarkm	struct passwd *pwd = NULL;
37181477Smarkm	int retval;
372123454Sdes	const char *cache_name, *q;
373125650Sdes	const void *user;
374174837Sdes	const void *cache_data;
375115465Sdes	char *cache_name_buf = NULL, *p;
37681477Smarkm
37781477Smarkm	uid_t euid;
37881477Smarkm	gid_t egid;
37981477Smarkm
38081477Smarkm	if (flags & PAM_DELETE_CRED)
38194564Sdes		return (PAM_SUCCESS);
38281477Smarkm
38381477Smarkm	if (flags & PAM_REFRESH_CRED)
38494564Sdes		return (PAM_SUCCESS);
38581477Smarkm
38681477Smarkm	if (flags & PAM_REINITIALIZE_CRED)
38794564Sdes		return (PAM_SUCCESS);
38881477Smarkm
38981477Smarkm	if (!(flags & PAM_ESTABLISH_CRED))
39094564Sdes		return (PAM_SERVICE_ERR);
39181477Smarkm
392140747Srwatson	/* If a persistent cache isn't desired, stop now. */
393207553Smm	if (openpam_get_option(pamh, PAM_OPT_NO_CCACHE) ||
394207553Smm		openpam_get_option(pamh, PAM_OPT_NO_USER_CHECK))
395140747Srwatson		return (PAM_SUCCESS);
396140747Srwatson
39781477Smarkm	PAM_LOG("Establishing credentials");
39881477Smarkm
39981477Smarkm	/* Get username */
400123454Sdes	retval = pam_get_item(pamh, PAM_USER, &user);
40181477Smarkm	if (retval != PAM_SUCCESS)
40294564Sdes		return (retval);
40381477Smarkm
404123454Sdes	PAM_LOG("Got user: %s", (const char *)user);
40581477Smarkm
40681477Smarkm	krbret = krb5_init_context(&pam_context);
40781477Smarkm	if (krbret != 0) {
408106862Snectar		PAM_LOG("Error krb5_init_context() failed");
40994564Sdes		return (PAM_SERVICE_ERR);
41081477Smarkm	}
41181477Smarkm
41281477Smarkm	PAM_LOG("Context initialised");
41381477Smarkm
41481477Smarkm	euid = geteuid();	/* Usually 0 */
41581477Smarkm	egid = getegid();
41681477Smarkm
41781477Smarkm	PAM_LOG("Got euid, egid: %d %d", euid, egid);
41881477Smarkm
419106864Snectar	/* Retrieve the temporary cache */
420123454Sdes	retval = pam_get_data(pamh, "ccache", &cache_data);
421110056Snectar	if (retval != PAM_SUCCESS) {
422110056Snectar		retval = PAM_CRED_UNAVAIL;
42381477Smarkm		goto cleanup3;
424110056Snectar	}
425123454Sdes	krbret = krb5_cc_resolve(pam_context, cache_data, &ccache_temp);
426106864Snectar	if (krbret != 0) {
427233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
428233406Sstas		    "Error krb5_cc_resolve(\"%s\")", (const char *)cache_data);
429110056Snectar		retval = PAM_SERVICE_ERR;
430106864Snectar		goto cleanup3;
431106864Snectar	}
43281477Smarkm
43381477Smarkm	/* Get the uid. This should exist. */
43481477Smarkm	pwd = getpwnam(user);
43581477Smarkm	if (pwd == NULL) {
43681477Smarkm		retval = PAM_USER_UNKNOWN;
43781477Smarkm		goto cleanup3;
43881477Smarkm	}
43981477Smarkm
44081477Smarkm	PAM_LOG("Done getpwnam()");
44181477Smarkm
44281477Smarkm	/* Avoid following a symlink as root */
44381477Smarkm	if (setegid(pwd->pw_gid)) {
44481477Smarkm		retval = PAM_SERVICE_ERR;
44581477Smarkm		goto cleanup3;
44681477Smarkm	}
44781477Smarkm	if (seteuid(pwd->pw_uid)) {
44881477Smarkm		retval = PAM_SERVICE_ERR;
44981477Smarkm		goto cleanup3;
45081477Smarkm	}
45181477Smarkm
45281477Smarkm	PAM_LOG("Done setegid() & seteuid()");
45381477Smarkm
45481477Smarkm	/* Get the cache name */
455115465Sdes	cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
456115465Sdes	if (cache_name == NULL) {
457115465Sdes		asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
458115465Sdes		cache_name = cache_name_buf;
459115465Sdes	}
46081477Smarkm
46181477Smarkm	p = calloc(PATH_MAX + 16, sizeof(char));
46281477Smarkm	q = cache_name;
46381477Smarkm
46481477Smarkm	if (p == NULL) {
46581477Smarkm		PAM_LOG("Error malloc(): failure");
46681477Smarkm		retval = PAM_BUF_ERR;
46781477Smarkm		goto cleanup3;
46881477Smarkm	}
46981477Smarkm	cache_name = p;
47081477Smarkm
47181477Smarkm	/* convert %u and %p */
47281477Smarkm	while (*q) {
47381477Smarkm		if (*q == '%') {
47481477Smarkm			q++;
47581477Smarkm			if (*q == 'u') {
47681477Smarkm				sprintf(p, "%d", pwd->pw_uid);
47781477Smarkm				p += strlen(p);
47881477Smarkm			}
47981477Smarkm			else if (*q == 'p') {
48081477Smarkm				sprintf(p, "%d", getpid());
48181477Smarkm				p += strlen(p);
48281477Smarkm			}
48381477Smarkm			else {
48481477Smarkm				/* Not a special token */
48581477Smarkm				*p++ = '%';
48681477Smarkm				q--;
48781477Smarkm			}
48881477Smarkm			q++;
48981477Smarkm		}
49081477Smarkm		else {
49181477Smarkm			*p++ = *q++;
49281477Smarkm		}
49381477Smarkm	}
49481477Smarkm
49581477Smarkm	PAM_LOG("Got cache_name: %s", cache_name);
49681477Smarkm
49781477Smarkm	/* Initialize the new ccache */
49881477Smarkm	krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
49981477Smarkm	if (krbret != 0) {
500233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
501233406Sstas		    "Error krb5_cc_get_principal()");
50281477Smarkm		retval = PAM_SERVICE_ERR;
50381477Smarkm		goto cleanup3;
50481477Smarkm	}
50581477Smarkm	krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
50681477Smarkm	if (krbret != 0) {
507233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret, "Error krb5_cc_resolve()");
50881477Smarkm		retval = PAM_SERVICE_ERR;
50981477Smarkm		goto cleanup2;
51081477Smarkm	}
51181477Smarkm	krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
51281477Smarkm	if (krbret != 0) {
513233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
514233406Sstas		    "Error krb5_cc_initialize()");
51581477Smarkm		retval = PAM_SERVICE_ERR;
51681477Smarkm		goto cleanup2;
51781477Smarkm	}
51881477Smarkm
51981477Smarkm	PAM_LOG("Cache initialised");
52081477Smarkm
52181477Smarkm	/* Prepare for iteration over creds */
52281477Smarkm	krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
52381477Smarkm	if (krbret != 0) {
524233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
525233406Sstas		    "Error krb5_cc_start_seq_get()");
52681477Smarkm		krb5_cc_destroy(pam_context, ccache_perm);
52781477Smarkm		retval = PAM_SERVICE_ERR;
52881477Smarkm		goto cleanup2;
52981477Smarkm	}
53081477Smarkm
53181477Smarkm	PAM_LOG("Prepared for iteration");
53281477Smarkm
53381477Smarkm	/* Copy the creds (should be two of them) */
53481477Smarkm	while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
53581477Smarkm				&cursor, &creds) == 0)) {
53681477Smarkm		krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
53781477Smarkm		if (krbret != 0) {
538233406Sstas			PAM_LOG_KRB5_ERR(pam_context, krbret,
539233406Sstas			    "Error krb5_cc_store_cred()");
54081477Smarkm			krb5_cc_destroy(pam_context, ccache_perm);
54181477Smarkm			krb5_free_cred_contents(pam_context, &creds);
54281477Smarkm			retval = PAM_SERVICE_ERR;
54381477Smarkm			goto cleanup2;
54481477Smarkm		}
54581477Smarkm		krb5_free_cred_contents(pam_context, &creds);
54681477Smarkm		PAM_LOG("Iteration");
54781477Smarkm	}
54881477Smarkm	krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
54981477Smarkm
55081477Smarkm	PAM_LOG("Done iterating");
55181477Smarkm
55281477Smarkm	if (strstr(cache_name, "FILE:") == cache_name) {
55381477Smarkm		if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) {
55481477Smarkm			PAM_LOG("Error chown(): %s", strerror(errno));
55581477Smarkm			krb5_cc_destroy(pam_context, ccache_perm);
55681477Smarkm			retval = PAM_SERVICE_ERR;
55781477Smarkm			goto cleanup2;
55881477Smarkm		}
55981477Smarkm		PAM_LOG("Done chown()");
56081477Smarkm
56181477Smarkm		if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) {
56281477Smarkm			PAM_LOG("Error chmod(): %s", strerror(errno));
56381477Smarkm			krb5_cc_destroy(pam_context, ccache_perm);
56481477Smarkm			retval = PAM_SERVICE_ERR;
56581477Smarkm			goto cleanup2;
56681477Smarkm		}
56781477Smarkm		PAM_LOG("Done chmod()");
56881477Smarkm	}
56981477Smarkm
57081477Smarkm	krb5_cc_close(pam_context, ccache_perm);
57181477Smarkm
57281477Smarkm	PAM_LOG("Cache closed");
57381477Smarkm
574115465Sdes	retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1);
575115465Sdes	if (retval != PAM_SUCCESS) {
576115465Sdes		PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
57781477Smarkm		krb5_cc_destroy(pam_context, ccache_perm);
57881477Smarkm		retval = PAM_SERVICE_ERR;
57981477Smarkm		goto cleanup2;
58081477Smarkm	}
58181477Smarkm
58281477Smarkm	PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
58381477Smarkm
58481477Smarkmcleanup2:
58581477Smarkm	krb5_free_principal(pam_context, princ);
58681477Smarkm	PAM_LOG("Done cleanup2");
58781477Smarkmcleanup3:
58881477Smarkm	krb5_free_context(pam_context);
58981477Smarkm	PAM_LOG("Done cleanup3");
59081477Smarkm
59181477Smarkm	seteuid(euid);
59281477Smarkm	setegid(egid);
59381477Smarkm
59481477Smarkm	PAM_LOG("Done seteuid() & setegid()");
59594564Sdes
596115465Sdes	if (cache_name_buf != NULL)
597115465Sdes		free(cache_name_buf);
598115465Sdes
59994564Sdes	return (retval);
600147810Skensmith#endif
60181477Smarkm}
60281477Smarkm
60394564Sdes/*
60481477Smarkm * account management
60581477Smarkm */
60681477SmarkmPAM_EXTERN int
60794564Sdespam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
608115465Sdes    int argc __unused, const char *argv[] __unused)
60981477Smarkm{
61081477Smarkm	krb5_error_code krbret;
61181477Smarkm	krb5_context pam_context;
61281477Smarkm	krb5_ccache ccache;
61381477Smarkm	krb5_principal princ;
61481477Smarkm	int retval;
615125650Sdes	const void *user;
616174837Sdes	const void *ccache_name;
61781477Smarkm
618123454Sdes	retval = pam_get_item(pamh, PAM_USER, &user);
61981477Smarkm	if (retval != PAM_SUCCESS)
62094564Sdes		return (retval);
62181477Smarkm
622123454Sdes	PAM_LOG("Got user: %s", (const char *)user);
62381477Smarkm
624123454Sdes	retval = pam_get_data(pamh, "ccache", &ccache_name);
625110275Sdes	if (retval != PAM_SUCCESS)
626110275Sdes		return (PAM_SUCCESS);
627110275Sdes
628110275Sdes	PAM_LOG("Got credentials");
629110275Sdes
63081477Smarkm	krbret = krb5_init_context(&pam_context);
63181477Smarkm	if (krbret != 0) {
632106862Snectar		PAM_LOG("Error krb5_init_context() failed");
63394564Sdes		return (PAM_PERM_DENIED);
63481477Smarkm	}
63581477Smarkm
63681477Smarkm	PAM_LOG("Context initialised");
63781477Smarkm
638123454Sdes	krbret = krb5_cc_resolve(pam_context, (const char *)ccache_name, &ccache);
639106864Snectar	if (krbret != 0) {
640233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
641233406Sstas		    "Error krb5_cc_resolve(\"%s\")", (const char *)ccache_name);
642106864Snectar		krb5_free_context(pam_context);
643106864Snectar		return (PAM_PERM_DENIED);
644106864Snectar	}
645106864Snectar
646123454Sdes	PAM_LOG("Got ccache %s", (const char *)ccache_name);
647106864Snectar
648106864Snectar
64981477Smarkm	krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
65081477Smarkm	if (krbret != 0) {
651233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
652233406Sstas		    "Error krb5_cc_get_principal()");
653241844Seadler		retval = PAM_PERM_DENIED;
65481477Smarkm		goto cleanup;
65581477Smarkm	}
65681477Smarkm
65781477Smarkm	PAM_LOG("Got principal");
65881477Smarkm
659123454Sdes	if (krb5_kuserok(pam_context, princ, (const char *)user))
66081477Smarkm		retval = PAM_SUCCESS;
66181477Smarkm	else
66281477Smarkm		retval = PAM_PERM_DENIED;
66381477Smarkm	krb5_free_principal(pam_context, princ);
66481477Smarkm
66581477Smarkm	PAM_LOG("Done kuserok()");
66681477Smarkm
66781477Smarkmcleanup:
66881477Smarkm	krb5_free_context(pam_context);
66981477Smarkm	PAM_LOG("Done cleanup");
67081477Smarkm
67194564Sdes	return (retval);
67281477Smarkm
67381477Smarkm}
67481477Smarkm
67594564Sdes/*
67681477Smarkm * password management
67781477Smarkm */
67881477SmarkmPAM_EXTERN int
67994564Sdespam_sm_chauthtok(pam_handle_t *pamh, int flags,
680115465Sdes    int argc __unused, const char *argv[] __unused)
68181477Smarkm{
68281477Smarkm	krb5_error_code krbret;
68381477Smarkm	krb5_context pam_context;
68481477Smarkm	krb5_creds creds;
68581477Smarkm	krb5_principal princ;
686233406Sstas	krb5_get_init_creds_opt *opts;
68781477Smarkm	krb5_data result_code_string, result_string;
68881477Smarkm	int result_code, retval;
689123454Sdes	const char *pass;
690123454Sdes	const void *user;
69193984Sdes	char *princ_name, *passdup;
69281477Smarkm
69381477Smarkm	if (!(flags & PAM_UPDATE_AUTHTOK))
69494564Sdes		return (PAM_AUTHTOK_ERR);
69581477Smarkm
696123454Sdes	retval = pam_get_item(pamh, PAM_USER, &user);
69781477Smarkm	if (retval != PAM_SUCCESS)
69894564Sdes		return (retval);
69981477Smarkm
700123454Sdes	PAM_LOG("Got user: %s", (const char *)user);
70181477Smarkm
70281477Smarkm	krbret = krb5_init_context(&pam_context);
70381477Smarkm	if (krbret != 0) {
704106862Snectar		PAM_LOG("Error krb5_init_context() failed");
70594564Sdes		return (PAM_SERVICE_ERR);
70681477Smarkm	}
70781477Smarkm
70881477Smarkm	PAM_LOG("Context initialised");
70981477Smarkm
71081477Smarkm	/* Get principal name */
711123454Sdes	krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
71281477Smarkm	if (krbret != 0) {
713233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
714233406Sstas		    "Error krb5_parse_name()");
71581477Smarkm		retval = PAM_USER_UNKNOWN;
71681477Smarkm		goto cleanup3;
71781477Smarkm	}
71881477Smarkm
71981477Smarkm	/* Now convert the principal name into something human readable */
72081477Smarkm	princ_name = NULL;
72181477Smarkm	krbret = krb5_unparse_name(pam_context, princ, &princ_name);
72281477Smarkm	if (krbret != 0) {
723233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
724233406Sstas		    "Error krb5_unparse_name()");
72581477Smarkm		retval = PAM_SERVICE_ERR;
72681477Smarkm		goto cleanup2;
72781477Smarkm	}
72881477Smarkm
72981477Smarkm	PAM_LOG("Got principal: %s", princ_name);
73081477Smarkm
73181477Smarkm	/* Get password */
73293984Sdes	retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, PASSWORD_PROMPT);
73381477Smarkm	if (retval != PAM_SUCCESS)
73481477Smarkm		goto cleanup2;
73581477Smarkm
73681477Smarkm	PAM_LOG("Got password");
73781477Smarkm
738233406Sstas	/* Initialize credentials request options. */
739233406Sstas	krbret = krb5_get_init_creds_opt_alloc(pam_context, &opts);
740233406Sstas	if (krbret != 0) {
741233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
742233406Sstas		    "Error krb5_get_init_creds_opt_alloc()");
743233406Sstas		PAM_VERBOSE_ERROR("Kerberos 5 error");
744233406Sstas		retval = PAM_SERVICE_ERR;
745233406Sstas		goto cleanup2;
746233406Sstas	}
747233406Sstas
748233406Sstas	PAM_LOG("Credentials options initialised");
749233406Sstas
75081477Smarkm	memset(&creds, 0, sizeof(krb5_creds));
75181477Smarkm	krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
752233406Sstas	    pass, NULL, pamh, 0, "kadmin/changepw", opts);
753233406Sstas	krb5_get_init_creds_opt_free(pam_context, opts);
75481477Smarkm	if (krbret != 0) {
755233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
756233406Sstas		    "Error krb5_get_init_creds_password()");
75781477Smarkm		retval = PAM_AUTH_ERR;
75881477Smarkm		goto cleanup2;
75981477Smarkm	}
76081477Smarkm
76181477Smarkm	PAM_LOG("Credentials established");
76281477Smarkm
76381477Smarkm	/* Now get the new password */
76493984Sdes	for (;;) {
76593984Sdes		retval = pam_get_authtok(pamh,
76693984Sdes		    PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
76793984Sdes		if (retval != PAM_TRY_AGAIN)
76893984Sdes			break;
76993984Sdes		pam_error(pamh, "Mismatch; try again, EOF to quit.");
77093984Sdes	}
77181477Smarkm	if (retval != PAM_SUCCESS)
77281477Smarkm		goto cleanup;
77381477Smarkm
77493984Sdes	PAM_LOG("Got new password");
77581477Smarkm
77693984Sdes	/* Change it */
77793984Sdes	if ((passdup = strdup(pass)) == NULL) {
77893984Sdes		retval = PAM_BUF_ERR;
77981477Smarkm		goto cleanup;
78081477Smarkm	}
781233406Sstas	krbret = krb5_set_password(pam_context, &creds, passdup, NULL,
78281477Smarkm	    &result_code, &result_code_string, &result_string);
78393984Sdes	free(passdup);
78481477Smarkm	if (krbret != 0) {
785233406Sstas		PAM_LOG_KRB5_ERR(pam_context, krbret,
786233406Sstas		    "Error krb5_change_password()");
78781477Smarkm		retval = PAM_AUTHTOK_ERR;
78881477Smarkm		goto cleanup;
78981477Smarkm	}
79081477Smarkm	if (result_code) {
79181477Smarkm		PAM_LOG("Error krb5_change_password(): (result_code)");
79281477Smarkm		retval = PAM_AUTHTOK_ERR;
79381477Smarkm		goto cleanup;
79481477Smarkm	}
79581477Smarkm
79681477Smarkm	PAM_LOG("Password changed");
79781477Smarkm
79881477Smarkm	if (result_string.data)
79981477Smarkm		free(result_string.data);
80081477Smarkm	if (result_code_string.data)
80181477Smarkm		free(result_code_string.data);
80281477Smarkm
80381477Smarkmcleanup:
80481477Smarkm	krb5_free_cred_contents(pam_context, &creds);
80581477Smarkm	PAM_LOG("Done cleanup");
80681477Smarkmcleanup2:
80781477Smarkm	krb5_free_principal(pam_context, princ);
80881477Smarkm	if (princ_name)
80981477Smarkm		free(princ_name);
810239099Sdim	PAM_LOG("Done cleanup2");
81181477Smarkm
812239099Sdimcleanup3:
81381477Smarkm	krb5_free_context(pam_context);
81481477Smarkm
81581477Smarkm	PAM_LOG("Done cleanup3");
81681477Smarkm
81794564Sdes	return (retval);
81881477Smarkm}
81981477Smarkm
82081477SmarkmPAM_MODULE_ENTRY("pam_krb5");
82181477Smarkm
82281477Smarkm/*
82381477Smarkm * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
82481477Smarkm * Modified by Sam Hartman <hartmans@mit.edu> to support PAM services
82581477Smarkm * for Debian.
82681477Smarkm *
82781477Smarkm * Verify the Kerberos ticket-granting ticket just retrieved for the
82881477Smarkm * user.  If the Kerberos server doesn't respond, assume the user is
82981477Smarkm * trying to fake us out (since we DID just get a TGT from what is
83081477Smarkm * supposedly our KDC).  If the host/<host> service is unknown (i.e.,
83181477Smarkm * the local keytab doesn't have it), and we cannot find another
83281477Smarkm * service we do have, let her in.
83381477Smarkm *
83481477Smarkm * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
83581477Smarkm */
836111985Smarkm/* ARGSUSED */
83781477Smarkmstatic int
83881477Smarkmverify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
83981477Smarkm    char *pam_service, int debug)
84081477Smarkm{
84181477Smarkm	krb5_error_code retval;
84281477Smarkm	krb5_principal princ;
84381477Smarkm	krb5_keyblock *keyblock;
84481477Smarkm	krb5_data packet;
84581477Smarkm	krb5_auth_context auth_context;
84689760Smarkm	char phost[BUFSIZ];
84789760Smarkm	const char *services[3], **service;
84881477Smarkm
84981477Smarkm	packet.data = 0;
85081477Smarkm
85181477Smarkm	/* If possible we want to try and verify the ticket we have
85281477Smarkm	 * received against a keytab.  We will try multiple service
85381477Smarkm	 * principals, including at least the host principal and the PAM
85481477Smarkm	 * service principal.  The host principal is preferred because access
85581477Smarkm	 * to that key is generally sufficient to compromise root, while the
85681477Smarkm	 * service key for this PAM service may be less carefully guarded.
85781477Smarkm	 * It is important to check the keytab first before the KDC so we do
85881477Smarkm	 * not get spoofed by a fake KDC.
85981477Smarkm	 */
86081477Smarkm	services[0] = "host";
86181477Smarkm	services[1] = pam_service;
86281477Smarkm	services[2] = NULL;
86381477Smarkm	keyblock = 0;
86481477Smarkm	retval = -1;
86581477Smarkm	for (service = &services[0]; *service != NULL; service++) {
86681477Smarkm		retval = krb5_sname_to_principal(context, NULL, *service,
86781477Smarkm		    KRB5_NT_SRV_HST, &princ);
86881477Smarkm		if (retval != 0) {
869233406Sstas			if (debug) {
870233406Sstas				const char *msg = krb5_get_error_message(
871233406Sstas				    context, retval);
872106862Snectar				syslog(LOG_DEBUG,
873106862Snectar				    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
874233406Sstas				    "krb5_sname_to_principal()", msg);
875233406Sstas				krb5_free_error_message(context, msg);
876233406Sstas			}
87781477Smarkm			return -1;
87881477Smarkm		}
87981477Smarkm
88081477Smarkm		/* Extract the name directly. */
88181477Smarkm		strncpy(phost, compat_princ_component(context, princ, 1),
88281477Smarkm		    BUFSIZ);
88381477Smarkm		phost[BUFSIZ - 1] = '\0';
88481477Smarkm
88581477Smarkm		/*
88694564Sdes		 * Do we have service/<host> keys?
88794564Sdes		 * (use default/configured keytab, kvno IGNORE_VNO to get the
88894564Sdes		 * first match, and ignore enctype.)
88994564Sdes		 */
89081477Smarkm		retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
89181477Smarkm		    &keyblock);
89281477Smarkm		if (retval != 0)
89381477Smarkm			continue;
89481477Smarkm		break;
89581477Smarkm	}
89681477Smarkm	if (retval != 0) {	/* failed to find key */
89781477Smarkm		/* Keytab or service key does not exist */
898233406Sstas		if (debug) {
899233406Sstas			const char *msg = krb5_get_error_message(context,
900233406Sstas			    retval);
901106862Snectar			syslog(LOG_DEBUG,
902106862Snectar			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
903233406Sstas			    "krb5_kt_read_service_key()", msg);
904233406Sstas			krb5_free_error_message(context, msg);
905233406Sstas		}
90681477Smarkm		retval = 0;
90781477Smarkm		goto cleanup;
90881477Smarkm	}
90981477Smarkm	if (keyblock)
91081477Smarkm		krb5_free_keyblock(context, keyblock);
91181477Smarkm
91281477Smarkm	/* Talk to the kdc and construct the ticket. */
91381477Smarkm	auth_context = NULL;
91481477Smarkm	retval = krb5_mk_req(context, &auth_context, 0, *service, phost,
91581477Smarkm		NULL, ccache, &packet);
91681477Smarkm	if (auth_context) {
91781477Smarkm		krb5_auth_con_free(context, auth_context);
91881477Smarkm		auth_context = NULL;	/* setup for rd_req */
91981477Smarkm	}
92081477Smarkm	if (retval) {
921233406Sstas		if (debug) {
922233406Sstas			const char *msg = krb5_get_error_message(context,
923233406Sstas			    retval);
924106862Snectar			syslog(LOG_DEBUG,
925106862Snectar			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
926233406Sstas			    "krb5_mk_req()", msg);
927233406Sstas			krb5_free_error_message(context, msg);
928233406Sstas		}
92981477Smarkm		retval = -1;
93081477Smarkm		goto cleanup;
93181477Smarkm	}
93281477Smarkm
93381477Smarkm	/* Try to use the ticket. */
93481477Smarkm	retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
93581477Smarkm	    NULL, NULL);
93681477Smarkm	if (retval) {
937233406Sstas		if (debug) {
938233406Sstas			const char *msg = krb5_get_error_message(context,
939233406Sstas			    retval);
940106862Snectar			syslog(LOG_DEBUG,
941106862Snectar			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
942233406Sstas			    "krb5_rd_req()", msg);
943233406Sstas			krb5_free_error_message(context, msg);
944233406Sstas		}
94581477Smarkm		retval = -1;
94681477Smarkm	}
94781477Smarkm	else
94881477Smarkm		retval = 1;
94981477Smarkm
95081477Smarkmcleanup:
95181477Smarkm	if (packet.data)
95281477Smarkm		compat_free_data_contents(context, &packet);
95381477Smarkm	krb5_free_principal(context, princ);
95481477Smarkm	return retval;
95581477Smarkm}
95681477Smarkm
95781477Smarkm/* Free the memory for cache_name. Called by pam_end() */
958111985Smarkm/* ARGSUSED */
95981477Smarkmstatic void
96089760Smarkmcleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
96181477Smarkm{
96281477Smarkm	krb5_context pam_context;
96381477Smarkm	krb5_ccache ccache;
964106864Snectar	krb5_error_code krbret;
96581477Smarkm
96681477Smarkm	if (krb5_init_context(&pam_context))
96781477Smarkm		return;
96881477Smarkm
969106864Snectar	krbret = krb5_cc_resolve(pam_context, data, &ccache);
970106864Snectar	if (krbret == 0)
971106864Snectar		krb5_cc_destroy(pam_context, ccache);
97281477Smarkm	krb5_free_context(pam_context);
973106864Snectar	free(data);
97481477Smarkm}
97581477Smarkm
97681477Smarkm#ifdef COMPAT_HEIMDAL
97781477Smarkm#ifdef COMPAT_MIT
97881477Smarkm#error This cannot be MIT and Heimdal compatible!
97981477Smarkm#endif
98081477Smarkm#endif
98181477Smarkm
98281477Smarkm#ifndef COMPAT_HEIMDAL
98381477Smarkm#ifndef COMPAT_MIT
98481477Smarkm#error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
98581477Smarkm#endif
98681477Smarkm#endif
98781477Smarkm
98881477Smarkm#ifdef COMPAT_HEIMDAL
989111985Smarkm/* ARGSUSED */
99081477Smarkmstatic const char *
99189760Smarkmcompat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
99281477Smarkm{
99381477Smarkm	return princ->name.name_string.val[n];
99481477Smarkm}
99581477Smarkm
996111985Smarkm/* ARGSUSED */
99781477Smarkmstatic void
99889760Smarkmcompat_free_data_contents(krb5_context context __unused, krb5_data * data)
99981477Smarkm{
100081477Smarkm	krb5_xfree(data->data);
100181477Smarkm}
100281477Smarkm#endif
100381477Smarkm
100481477Smarkm#ifdef COMPAT_MIT
100581477Smarkmstatic const char *
100681477Smarkmcompat_princ_component(krb5_context context, krb5_principal princ, int n)
100781477Smarkm{
100881477Smarkm	return krb5_princ_component(context, princ, n)->data;
100981477Smarkm}
101081477Smarkm
101181477Smarkmstatic void
101281477Smarkmcompat_free_data_contents(krb5_context context, krb5_data * data)
101381477Smarkm{
101481477Smarkm	krb5_free_data_contents(context, data);
101581477Smarkm}
101681477Smarkm#endif
1017