xref: /illumos-gate/usr/src/lib/pam_modules/krb5/krb5_authenticate.c (revision cbea7aca3fd7787405cbdbd93752998f03dfc25f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a0709436Smp153739  * Common Development and Distribution License (the "License").
6a0709436Smp153739  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215ad42b1bSSurya Prakki 
227c478bd9Sstevel@tonic-gate /*
23488060a6SWill Fiveash  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24*cbea7acaSDominik Hassler  *
25*cbea7acaSDominik Hassler  * Copyright 2023 OmniOS Community Edition (OmniOSce) Association.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
297c478bd9Sstevel@tonic-gate #include <security/pam_modules.h>
307c478bd9Sstevel@tonic-gate #include <security/pam_impl.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <pwd.h>
377c478bd9Sstevel@tonic-gate #include <syslog.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <k5-int.h>
407c478bd9Sstevel@tonic-gate #include "profile/prof_int.h"
417c478bd9Sstevel@tonic-gate #include <netdb.h>
427c478bd9Sstevel@tonic-gate #include <ctype.h>
437c478bd9Sstevel@tonic-gate #include "utils.h"
447c478bd9Sstevel@tonic-gate #include "krb5_repository.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	KRB5_DEFAULT_OPTIONS 0
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int forwardable_flag = 0;
497c478bd9Sstevel@tonic-gate int renewable_flag = 0;
507c478bd9Sstevel@tonic-gate int proxiable_flag = 0;
517c478bd9Sstevel@tonic-gate int no_address_flag = 0;
527c478bd9Sstevel@tonic-gate profile_options_boolean config_option[] = {
537c478bd9Sstevel@tonic-gate 	{ "forwardable", &forwardable_flag, 0 },
547c478bd9Sstevel@tonic-gate 	{ "renewable",  &renewable_flag, 0 },
557c478bd9Sstevel@tonic-gate 	{ "proxiable", &proxiable_flag, 0 },
567c478bd9Sstevel@tonic-gate 	{ "no_addresses", &no_address_flag, 0 },
577c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
587c478bd9Sstevel@tonic-gate };
597c478bd9Sstevel@tonic-gate char *renew_timeval;
607c478bd9Sstevel@tonic-gate char *life_timeval;
617c478bd9Sstevel@tonic-gate profile_option_strings config_times[] = {
627c478bd9Sstevel@tonic-gate 	{ "max_life", &life_timeval, 0 },
637c478bd9Sstevel@tonic-gate 	{ "max_renewable_life",  &renew_timeval, 0 },
647c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate char *realmdef[] = { "realms", NULL, NULL, NULL };
677c478bd9Sstevel@tonic-gate char *appdef[] = { "appdefaults", "kinit", NULL };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	krb_realm (*(realmdef + 1))
707c478bd9Sstevel@tonic-gate 
71*cbea7acaSDominik Hassler int	attempt_krb5_auth(pam_handle_t *, krb5_module_data_t *, const char *,
7270f41fc1SWill Fiveash 	char **, boolean_t);
737c478bd9Sstevel@tonic-gate void	krb5_cleanup(pam_handle_t *, void *, int);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate extern errcode_t profile_get_options_boolean();
767c478bd9Sstevel@tonic-gate extern errcode_t profile_get_options_string();
772278144aSsemery extern int krb5_verifypw(char *, char *, int);
787c478bd9Sstevel@tonic-gate extern krb5_error_code krb5_verify_init_creds(krb5_context,
797c478bd9Sstevel@tonic-gate 		krb5_creds *, krb5_principal, krb5_keytab, krb5_ccache *,
807c478bd9Sstevel@tonic-gate 		krb5_verify_init_creds_opt *);
813125ebfcSsemery extern krb5_error_code __krb5_get_init_creds_password(krb5_context,
823125ebfcSsemery 		krb5_creds *, krb5_principal, char *, krb5_prompter_fct, void *,
833125ebfcSsemery 		krb5_deltat, char *, krb5_get_init_creds_opt *,
843125ebfcSsemery 		krb5_kdc_rep **);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * pam_sm_authenticate		- Authenticate user
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate int
pam_sm_authenticate(pam_handle_t * pamh,int flags,int argc,const char ** argv)90*cbea7acaSDominik Hassler pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
917c478bd9Sstevel@tonic-gate {
92*cbea7acaSDominik Hassler 	const char *user = NULL;
937c478bd9Sstevel@tonic-gate 	int err;
947c478bd9Sstevel@tonic-gate 	int result = PAM_AUTH_ERR;
957c478bd9Sstevel@tonic-gate 	/* pam.conf options */
967c478bd9Sstevel@tonic-gate 	int debug = 0;
977c478bd9Sstevel@tonic-gate 	int warn = 1;
987c478bd9Sstevel@tonic-gate 	/* return an error on password expire */
997c478bd9Sstevel@tonic-gate 	int err_on_exp = 0;
1007c478bd9Sstevel@tonic-gate 	int i;
1017c478bd9Sstevel@tonic-gate 	char *password = NULL;
1027c478bd9Sstevel@tonic-gate 	uid_t pw_uid;
1037c478bd9Sstevel@tonic-gate 	krb5_module_data_t *kmd = NULL;
1047c478bd9Sstevel@tonic-gate 	krb5_repository_data_t *krb5_data = NULL;
105*cbea7acaSDominik Hassler 	const pam_repository_t *rep_data = NULL;
10670f41fc1SWill Fiveash 	boolean_t do_pkinit = FALSE;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
1097c478bd9Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0) {
1107c478bd9Sstevel@tonic-gate 			debug = 1;
1117c478bd9Sstevel@tonic-gate 		} else if (strcmp(argv[i], "nowarn") == 0) {
1127c478bd9Sstevel@tonic-gate 			warn = 0;
1137c478bd9Sstevel@tonic-gate 		} else if (strcmp(argv[i], "err_on_exp") == 0) {
1147c478bd9Sstevel@tonic-gate 			err_on_exp = 1;
11570f41fc1SWill Fiveash 		} else if (strcmp(argv[i], "pkinit") == 0) {
11670f41fc1SWill Fiveash 			do_pkinit = TRUE;
1177c478bd9Sstevel@tonic-gate 		} else {
1183bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_ERR,
11970f41fc1SWill Fiveash 			    "PAM-KRB5 (auth) unrecognized option %s", argv[i]);
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	if (flags & PAM_SILENT) warn = 0;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (debug)
1253bfb48feSsemery 		__pam_log(LOG_AUTH | LOG_DEBUG,
1267c478bd9Sstevel@tonic-gate 		    "PAM-KRB5 (auth): pam_sm_authenticate flags=%d",
1277c478bd9Sstevel@tonic-gate 		    flags);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * pam_get_data could fail if we are being called for the first time
1317c478bd9Sstevel@tonic-gate 	 * or if the module is not found, PAM_NO_MODULE_DATA is not an error
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	err = pam_get_data(pamh, KRB5_DATA, (const void **)&kmd);
1347c478bd9Sstevel@tonic-gate 	if (!(err == PAM_SUCCESS || err == PAM_NO_MODULE_DATA))
1353bfb48feSsemery 		return (PAM_SYSTEM_ERR);
1367c478bd9Sstevel@tonic-gate 
13770f41fc1SWill Fiveash 	/*
13870f41fc1SWill Fiveash 	 * If pam_krb5 was stacked higher in the auth stack and did PKINIT
13970f41fc1SWill Fiveash 	 * preauth sucessfully then this instance is a fallback to password
14070f41fc1SWill Fiveash 	 * based preauth and should just return PAM_IGNORE.
14170f41fc1SWill Fiveash 	 *
14270f41fc1SWill Fiveash 	 * The else clause is handled further down.
14370f41fc1SWill Fiveash 	 */
14470f41fc1SWill Fiveash 	if (kmd != NULL) {
14570f41fc1SWill Fiveash 		if (++(kmd->auth_calls) > 2) {
14670f41fc1SWill Fiveash 			/*
14770f41fc1SWill Fiveash 			 * pam_krb5 has been stacked > 2 times in the auth
14870f41fc1SWill Fiveash 			 * stack.  Clear out the current kmd and proceed as if
14970f41fc1SWill Fiveash 			 * this is the first time pam_krb5 auth has been called.
15070f41fc1SWill Fiveash 			 */
15170f41fc1SWill Fiveash 			if (debug) {
15270f41fc1SWill Fiveash 				__pam_log(LOG_AUTH | LOG_DEBUG,
15370f41fc1SWill Fiveash 				    "PAM-KRB5 (auth): stacked more than"
15470f41fc1SWill Fiveash 				    " two times, clearing kmd");
15570f41fc1SWill Fiveash 			}
15670f41fc1SWill Fiveash 			/* clear out/free current kmd */
15770f41fc1SWill Fiveash 			err = pam_set_data(pamh, KRB5_DATA, NULL, NULL);
15870f41fc1SWill Fiveash 			if (err != PAM_SUCCESS) {
15970f41fc1SWill Fiveash 				krb5_cleanup(pamh, kmd, err);
16070f41fc1SWill Fiveash 				result = err;
16170f41fc1SWill Fiveash 				goto out;
16270f41fc1SWill Fiveash 			}
16370f41fc1SWill Fiveash 			kmd = NULL;
16470f41fc1SWill Fiveash 		} else if (kmd->auth_calls == 2 &&
16570f41fc1SWill Fiveash 		    kmd->auth_status == PAM_SUCCESS) {
16670f41fc1SWill Fiveash 			/*
16770f41fc1SWill Fiveash 			 * The previous instance of pam_krb5 succeeded and this
16870f41fc1SWill Fiveash 			 * instance was a fall back in case it didn't succeed so
16970f41fc1SWill Fiveash 			 * return ignore.
17070f41fc1SWill Fiveash 			 */
17170f41fc1SWill Fiveash 			if (debug) {
17270f41fc1SWill Fiveash 				__pam_log(LOG_AUTH | LOG_DEBUG,
17370f41fc1SWill Fiveash 				    "PAM-KRB5 (auth): PKINIT succeeded "
17470f41fc1SWill Fiveash 				    "earlier so returning PAM_IGNORE");
17570f41fc1SWill Fiveash 			}
17670f41fc1SWill Fiveash 			return (PAM_IGNORE);
17770f41fc1SWill Fiveash 		}
17870f41fc1SWill Fiveash 	}
17970f41fc1SWill Fiveash 
180*cbea7acaSDominik Hassler 	(void) pam_get_item(pamh, PAM_USER, (const void **)&user);
18170f41fc1SWill Fiveash 
18270f41fc1SWill Fiveash 	if (user == NULL || *user == '\0') {
18370f41fc1SWill Fiveash 		if (do_pkinit) {
18470f41fc1SWill Fiveash 			/*
18570f41fc1SWill Fiveash 			 * If doing PKINIT it is okay to prompt for the user
18670f41fc1SWill Fiveash 			 * name.
18770f41fc1SWill Fiveash 			 */
188*cbea7acaSDominik Hassler 			if ((err = pam_get_user(pamh, &user,
189*cbea7acaSDominik Hassler 			    NULL)) != PAM_SUCCESS) {
19070f41fc1SWill Fiveash 				if (debug) {
19170f41fc1SWill Fiveash 					__pam_log(LOG_AUTH | LOG_DEBUG,
19270f41fc1SWill Fiveash 					    "PAM-KRB5 (auth): get user failed: "
19370f41fc1SWill Fiveash 					    "%s", pam_strerror(pamh, err));
19470f41fc1SWill Fiveash 				}
19570f41fc1SWill Fiveash 				return (err);
19670f41fc1SWill Fiveash 			}
19770f41fc1SWill Fiveash 		} else {
19870f41fc1SWill Fiveash 			if (debug)
19970f41fc1SWill Fiveash 				__pam_log(LOG_AUTH | LOG_DEBUG,
20070f41fc1SWill Fiveash 				    "PAM-KRB5 (auth): user empty or null");
20170f41fc1SWill Fiveash 			return (PAM_USER_UNKNOWN);
20270f41fc1SWill Fiveash 		}
20370f41fc1SWill Fiveash 	}
20470f41fc1SWill Fiveash 
20570f41fc1SWill Fiveash 	/* make sure a password entry exists for this user */
20670f41fc1SWill Fiveash 	if (!get_pw_uid(user, &pw_uid))
20770f41fc1SWill Fiveash 		return (PAM_USER_UNKNOWN);
20870f41fc1SWill Fiveash 
2097c478bd9Sstevel@tonic-gate 	if (kmd == NULL) {
2107c478bd9Sstevel@tonic-gate 		kmd = calloc(1, sizeof (krb5_module_data_t));
2117c478bd9Sstevel@tonic-gate 		if (kmd == NULL) {
2127c478bd9Sstevel@tonic-gate 			result = PAM_BUF_ERR;
2137c478bd9Sstevel@tonic-gate 			goto out;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		err = pam_set_data(pamh, KRB5_DATA, kmd, &krb5_cleanup);
2177c478bd9Sstevel@tonic-gate 		if (err != PAM_SUCCESS) {
2187c478bd9Sstevel@tonic-gate 			free(kmd);
2197c478bd9Sstevel@tonic-gate 			result = err;
2207c478bd9Sstevel@tonic-gate 			goto out;
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if (!kmd->env) {
2257c478bd9Sstevel@tonic-gate 		char buffer[512];
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		if (snprintf(buffer, sizeof (buffer),
2287c478bd9Sstevel@tonic-gate 		    "%s=FILE:/tmp/krb5cc_%d",
2297c478bd9Sstevel@tonic-gate 		    KRB5_ENV_CCNAME, (int)pw_uid) >= sizeof (buffer)) {
2307c478bd9Sstevel@tonic-gate 			result = PAM_SYSTEM_ERR;
2317c478bd9Sstevel@tonic-gate 			goto out;
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		/* we MUST copy this to the heap for the putenv to work! */
2357c478bd9Sstevel@tonic-gate 		kmd->env = strdup(buffer);
2367c478bd9Sstevel@tonic-gate 		if (!kmd->env) {
2377c478bd9Sstevel@tonic-gate 			result = PAM_BUF_ERR;
2387c478bd9Sstevel@tonic-gate 			goto out;
2397c478bd9Sstevel@tonic-gate 		} else {
2407c478bd9Sstevel@tonic-gate 			if (putenv(kmd->env)) {
2417c478bd9Sstevel@tonic-gate 				result = PAM_SYSTEM_ERR;
2427c478bd9Sstevel@tonic-gate 				goto out;
2437c478bd9Sstevel@tonic-gate 			}
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
24767c90040Ssemery 	if (kmd->user != NULL)
24867c90040Ssemery 		free(kmd->user);
2493bfb48feSsemery 	if ((kmd->user = strdup(user)) == NULL) {
2503bfb48feSsemery 		result = PAM_BUF_ERR;
2513bfb48feSsemery 		goto out;
2523bfb48feSsemery 	}
2533bfb48feSsemery 
2547c478bd9Sstevel@tonic-gate 	kmd->auth_status = PAM_AUTH_ERR;
2557c478bd9Sstevel@tonic-gate 	kmd->debug = debug;
2567c478bd9Sstevel@tonic-gate 	kmd->warn = warn;
2577c478bd9Sstevel@tonic-gate 	kmd->err_on_exp = err_on_exp;
2587c478bd9Sstevel@tonic-gate 	kmd->ccache = NULL;
2597c478bd9Sstevel@tonic-gate 	kmd->kcontext = NULL;
2607c478bd9Sstevel@tonic-gate 	kmd->password = NULL;
2617c478bd9Sstevel@tonic-gate 	kmd->age_status = PAM_SUCCESS;
2627c478bd9Sstevel@tonic-gate 	(void) memset((char *)&kmd->initcreds, 0, sizeof (krb5_creds));
26370f41fc1SWill Fiveash 	kmd->auth_calls = 1;
26470f41fc1SWill Fiveash 	kmd->preauth_type = do_pkinit ? KRB_PKINIT : KRB_PASSWD;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * For apps that already did krb5 auth exchange...
2687c478bd9Sstevel@tonic-gate 	 * Now that we've created the kmd structure, we can
2697c478bd9Sstevel@tonic-gate 	 * return SUCCESS.  'kmd' may be needed later by other
2707c478bd9Sstevel@tonic-gate 	 * PAM functions, thats why we wait until this point to
2717c478bd9Sstevel@tonic-gate 	 * return.
2727c478bd9Sstevel@tonic-gate 	 */
273*cbea7acaSDominik Hassler 	(void) pam_get_item(pamh, PAM_REPOSITORY, (const void **)&rep_data);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (rep_data != NULL) {
2767c478bd9Sstevel@tonic-gate 		if (strcmp(rep_data->type, KRB5_REPOSITORY_NAME) != 0) {
2777c478bd9Sstevel@tonic-gate 			if (debug)
2783bfb48feSsemery 				__pam_log(LOG_AUTH | LOG_DEBUG,
2793bfb48feSsemery 				    "PAM-KRB5 (auth): wrong"
2807c478bd9Sstevel@tonic-gate 				    "repository found (%s), returning "
2817c478bd9Sstevel@tonic-gate 				    "PAM_IGNORE", rep_data->type);
2827c478bd9Sstevel@tonic-gate 			return (PAM_IGNORE);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		if (rep_data->scope_len == sizeof (krb5_repository_data_t)) {
2857c478bd9Sstevel@tonic-gate 			krb5_data = (krb5_repository_data_t *)rep_data->scope;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			if (krb5_data->flags ==
2887c478bd9Sstevel@tonic-gate 			    SUNW_PAM_KRB5_ALREADY_AUTHENTICATED &&
2897c478bd9Sstevel@tonic-gate 			    krb5_data->principal != NULL &&
2907c478bd9Sstevel@tonic-gate 			    strlen(krb5_data->principal)) {
2917c478bd9Sstevel@tonic-gate 				if (debug)
2923bfb48feSsemery 					__pam_log(LOG_AUTH | LOG_DEBUG,
2937c478bd9Sstevel@tonic-gate 					    "PAM-KRB5 (auth): Principal "
2947c478bd9Sstevel@tonic-gate 					    "%s already authenticated",
2957c478bd9Sstevel@tonic-gate 					    krb5_data->principal);
2967c478bd9Sstevel@tonic-gate 				kmd->auth_status = PAM_SUCCESS;
2977c478bd9Sstevel@tonic-gate 				return (PAM_SUCCESS);
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/*
3037c478bd9Sstevel@tonic-gate 	 * if root key exists in the keytab, it's a random key so no
3047c478bd9Sstevel@tonic-gate 	 * need to prompt for pw and we just return IGNORE.
3057c478bd9Sstevel@tonic-gate 	 *
3067c478bd9Sstevel@tonic-gate 	 * note we don't need to force a prompt for pw as authtok_get
3077c478bd9Sstevel@tonic-gate 	 * is required to be stacked above this module.
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	if ((strcmp(user, ROOT_UNAME) == 0) &&
3107c478bd9Sstevel@tonic-gate 	    key_in_keytab(user, debug)) {
3117c478bd9Sstevel@tonic-gate 		if (debug)
3123bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_DEBUG,
3137c478bd9Sstevel@tonic-gate 			    "PAM-KRB5 (auth): "
3147c478bd9Sstevel@tonic-gate 			    "key for '%s' in keytab, returning IGNORE", user);
3157c478bd9Sstevel@tonic-gate 		result = PAM_IGNORE;
3167c478bd9Sstevel@tonic-gate 		goto out;
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
319*cbea7acaSDominik Hassler 	(void) pam_get_item(pamh, PAM_AUTHTOK, (const void **)&password);
3207c478bd9Sstevel@tonic-gate 
32170f41fc1SWill Fiveash 	result = attempt_krb5_auth(pamh, kmd, user, &password, 1);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate out:
3247c478bd9Sstevel@tonic-gate 	if (kmd) {
3257c478bd9Sstevel@tonic-gate 		if (debug)
3263bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_DEBUG,
3277c478bd9Sstevel@tonic-gate 			    "PAM-KRB5 (auth): pam_sm_auth finalize"
3287c478bd9Sstevel@tonic-gate 			    " ccname env, result =%d, env ='%s',"
3297c478bd9Sstevel@tonic-gate 			    " age = %d, status = %d",
3307c478bd9Sstevel@tonic-gate 			    result, kmd->env ? kmd->env : "<null>",
3317c478bd9Sstevel@tonic-gate 			    kmd->age_status, kmd->auth_status);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		if (kmd->env &&
3347c478bd9Sstevel@tonic-gate 		    !(kmd->age_status == PAM_NEW_AUTHTOK_REQD &&
3357c478bd9Sstevel@tonic-gate 		    kmd->auth_status == PAM_SUCCESS)) {
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 			if (result == PAM_SUCCESS) {
3397c478bd9Sstevel@tonic-gate 				/*
3407c478bd9Sstevel@tonic-gate 				 * Put ccname into the pamh so that login
3417c478bd9Sstevel@tonic-gate 				 * apps can pick this up when they run
3427c478bd9Sstevel@tonic-gate 				 * pam_getenvlist().
3437c478bd9Sstevel@tonic-gate 				 */
3447c478bd9Sstevel@tonic-gate 				if ((result = pam_putenv(pamh, kmd->env))
3457c478bd9Sstevel@tonic-gate 				    != PAM_SUCCESS) {
3467c478bd9Sstevel@tonic-gate 					/* should not happen but... */
3473bfb48feSsemery 					__pam_log(LOG_AUTH | LOG_ERR,
3487c478bd9Sstevel@tonic-gate 					    "PAM-KRB5 (auth):"
3493bfb48feSsemery 					    " pam_putenv failed: result: %d",
3507c478bd9Sstevel@tonic-gate 					    result);
3517c478bd9Sstevel@tonic-gate 					goto cleanupccname;
3527c478bd9Sstevel@tonic-gate 				}
3537c478bd9Sstevel@tonic-gate 			} else {
3547c478bd9Sstevel@tonic-gate 			cleanupccname:
3557c478bd9Sstevel@tonic-gate 				/* for lack of a Solaris unputenv() */
3567c478bd9Sstevel@tonic-gate 				krb5_unsetenv(KRB5_ENV_CCNAME);
3577c478bd9Sstevel@tonic-gate 				free(kmd->env);
3587c478bd9Sstevel@tonic-gate 				kmd->env = NULL;
3597c478bd9Sstevel@tonic-gate 			}
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 		kmd->auth_status = result;
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (debug)
3653bfb48feSsemery 		__pam_log(LOG_AUTH | LOG_DEBUG,
3667c478bd9Sstevel@tonic-gate 		    "PAM-KRB5 (auth): end: %s", pam_strerror(pamh, result));
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	return (result);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
37170f41fc1SWill Fiveash static krb5_error_code
pam_krb5_prompter(krb5_context ctx,void * data,const char * name,const char * banner,int num_prompts,krb5_prompt prompts[])372*cbea7acaSDominik Hassler pam_krb5_prompter(krb5_context ctx, void *data, const char *name,
373*cbea7acaSDominik Hassler     const char *banner, int num_prompts, krb5_prompt prompts[])
37470f41fc1SWill Fiveash {
375dde44d91SWill Fiveash 	krb5_error_code rc = KRB5_LIBOS_CANTREADPWD;
37670f41fc1SWill Fiveash 	pam_handle_t *pamh = (pam_handle_t *)data;
377*cbea7acaSDominik Hassler 	const struct pam_conv	*pam_convp;
378dde44d91SWill Fiveash 	struct pam_message *msgs = NULL;
379dde44d91SWill Fiveash 	struct pam_response *ret_respp = NULL;
38070f41fc1SWill Fiveash 	int i;
38170f41fc1SWill Fiveash 	krb5_prompt_type *prompt_type = krb5_get_prompt_types(ctx);
38270f41fc1SWill Fiveash 	char tmpbuf[PAM_MAX_MSG_SIZE];
38370f41fc1SWill Fiveash 
384dde44d91SWill Fiveash 	if (prompts) {
385dde44d91SWill Fiveash 		assert(num_prompts > 0);
386dde44d91SWill Fiveash 	}
38770f41fc1SWill Fiveash 	/*
38870f41fc1SWill Fiveash 	 * Because this function should never be used for password prompts,
38970f41fc1SWill Fiveash 	 * disallow password prompts.
39070f41fc1SWill Fiveash 	 */
39170f41fc1SWill Fiveash 	for (i = 0; i < num_prompts; i++) {
392a1219d13SWill Fiveash 		switch (prompt_type[i]) {
393a1219d13SWill Fiveash 		case KRB5_PROMPT_TYPE_PASSWORD:
394a1219d13SWill Fiveash 		case KRB5_PROMPT_TYPE_NEW_PASSWORD:
395a1219d13SWill Fiveash 		case KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN:
396dde44d91SWill Fiveash 			return (rc);
39770f41fc1SWill Fiveash 		}
398a1219d13SWill Fiveash 	}
39970f41fc1SWill Fiveash 
400*cbea7acaSDominik Hassler 	if (pam_get_item(pamh, PAM_CONV, (const void **)&pam_convp) !=
401*cbea7acaSDominik Hassler 	    PAM_SUCCESS) {
40270f41fc1SWill Fiveash 		return (rc);
40370f41fc1SWill Fiveash 	}
40470f41fc1SWill Fiveash 	if (pam_convp == NULL) {
405dde44d91SWill Fiveash 		return (rc);
40670f41fc1SWill Fiveash 	}
40770f41fc1SWill Fiveash 
40870f41fc1SWill Fiveash 	msgs = (struct pam_message *)calloc(num_prompts,
40970f41fc1SWill Fiveash 	    sizeof (struct pam_message));
41070f41fc1SWill Fiveash 	if (msgs == NULL) {
411dde44d91SWill Fiveash 		return (rc);
41270f41fc1SWill Fiveash 	}
41370f41fc1SWill Fiveash 	(void) memset(msgs, 0, sizeof (struct pam_message) * num_prompts);
41470f41fc1SWill Fiveash 
41570f41fc1SWill Fiveash 	for (i = 0; i < num_prompts; i++) {
41670f41fc1SWill Fiveash 		/* convert krb prompt style to PAM style */
41770f41fc1SWill Fiveash 		if (prompts[i].hidden) {
41870f41fc1SWill Fiveash 			msgs[i].msg_style = PAM_PROMPT_ECHO_OFF;
41970f41fc1SWill Fiveash 		} else {
42070f41fc1SWill Fiveash 			msgs[i].msg_style = PAM_PROMPT_ECHO_ON;
42170f41fc1SWill Fiveash 		}
42270f41fc1SWill Fiveash 		/*
42370f41fc1SWill Fiveash 		 * krb expects the prompting function to append ": " to the
42470f41fc1SWill Fiveash 		 * prompt string.
42570f41fc1SWill Fiveash 		 */
42670f41fc1SWill Fiveash 		if (snprintf(tmpbuf, sizeof (tmpbuf), "%s: ",
42770f41fc1SWill Fiveash 		    prompts[i].prompt) < 0) {
42870f41fc1SWill Fiveash 			goto cleanup;
42970f41fc1SWill Fiveash 		}
43070f41fc1SWill Fiveash 		msgs[i].msg = strdup(tmpbuf);
43170f41fc1SWill Fiveash 		if (msgs[i].msg == NULL) {
43270f41fc1SWill Fiveash 			goto cleanup;
43370f41fc1SWill Fiveash 		}
43470f41fc1SWill Fiveash 	}
43570f41fc1SWill Fiveash 
43670f41fc1SWill Fiveash 	/*
43770f41fc1SWill Fiveash 	 * Call PAM conv function to display the prompt.
43870f41fc1SWill Fiveash 	 */
43970f41fc1SWill Fiveash 
440*cbea7acaSDominik Hassler 	if ((pam_convp->conv)(num_prompts, (const struct pam_message **)&msgs,
441*cbea7acaSDominik Hassler 	    &ret_respp, pam_convp->appdata_ptr) == PAM_SUCCESS) {
44270f41fc1SWill Fiveash 		for (i = 0; i < num_prompts; i++) {
44370f41fc1SWill Fiveash 			/* convert PAM response to krb prompt reply format */
444dde44d91SWill Fiveash 			assert(prompts[i].reply->data != NULL);
445dde44d91SWill Fiveash 			assert(ret_respp[i].resp != NULL);
446dde44d91SWill Fiveash 
447dde44d91SWill Fiveash 			if (strlcpy(prompts[i].reply->data,
448dde44d91SWill Fiveash 			    ret_respp[i].resp, prompts[i].reply->length) >=
449dde44d91SWill Fiveash 			    prompts[i].reply->length) {
450dde44d91SWill Fiveash 				char errmsg[1][PAM_MAX_MSG_SIZE];
451dde44d91SWill Fiveash 
452dde44d91SWill Fiveash 				(void) snprintf(errmsg[0], PAM_MAX_MSG_SIZE,
453dde44d91SWill Fiveash 				    "%s", dgettext(TEXT_DOMAIN,
454dde44d91SWill Fiveash 				    "Reply too long: "));
455dde44d91SWill Fiveash 				(void) __pam_display_msg(pamh, PAM_ERROR_MSG,
456dde44d91SWill Fiveash 				    1, errmsg, NULL);
457dde44d91SWill Fiveash 				goto cleanup;
458dde44d91SWill Fiveash 			} else {
459dde44d91SWill Fiveash 				char *retp;
460dde44d91SWill Fiveash 
46170f41fc1SWill Fiveash 				/*
462dde44d91SWill Fiveash 				 * newline must be replaced with \0 terminator
46370f41fc1SWill Fiveash 				 */
464dde44d91SWill Fiveash 				retp = strchr(prompts[i].reply->data, '\n');
465dde44d91SWill Fiveash 				if (retp != NULL)
466dde44d91SWill Fiveash 					*retp = '\0';
467dde44d91SWill Fiveash 				/* NULL terminator should not be counted */
468dde44d91SWill Fiveash 				prompts[i].reply->length =
469dde44d91SWill Fiveash 				    strlen(prompts[i].reply->data);
470dde44d91SWill Fiveash 			}
471dde44d91SWill Fiveash 		}
472dde44d91SWill Fiveash 		rc = 0;
47370f41fc1SWill Fiveash 	}
47470f41fc1SWill Fiveash 
47570f41fc1SWill Fiveash cleanup:
47670f41fc1SWill Fiveash 	for (i = 0; i < num_prompts; i++) {
47770f41fc1SWill Fiveash 		if (msgs[i].msg) {
47870f41fc1SWill Fiveash 			free(msgs[i].msg);
47970f41fc1SWill Fiveash 		}
480dde44d91SWill Fiveash 		if (ret_respp[i].resp) {
481dde44d91SWill Fiveash 			/* 0 out sensitive data before free() */
482dde44d91SWill Fiveash 			(void) memset(ret_respp[i].resp, 0,
483dde44d91SWill Fiveash 			    strlen(ret_respp[i].resp));
484dde44d91SWill Fiveash 			free(ret_respp[i].resp);
485dde44d91SWill Fiveash 		}
48670f41fc1SWill Fiveash 	}
48770f41fc1SWill Fiveash 	free(msgs);
488dde44d91SWill Fiveash 	free(ret_respp);
48970f41fc1SWill Fiveash 	return (rc);
49070f41fc1SWill Fiveash }
49170f41fc1SWill Fiveash 
4927c478bd9Sstevel@tonic-gate int
attempt_krb5_auth(pam_handle_t * pamh,krb5_module_data_t * kmd,const char * user,char ** krb5_pass,boolean_t verify_tik)4937c478bd9Sstevel@tonic-gate attempt_krb5_auth(
49470f41fc1SWill Fiveash 	pam_handle_t *pamh,
4957c478bd9Sstevel@tonic-gate 	krb5_module_data_t	*kmd,
496*cbea7acaSDominik Hassler 	const char	*user,
4977c478bd9Sstevel@tonic-gate 	char		**krb5_pass,
4980be37caaSsemery 	boolean_t	verify_tik)
4997c478bd9Sstevel@tonic-gate {
50067c90040Ssemery 	krb5_principal	me = NULL, clientp = NULL;
50167c90040Ssemery 	krb5_principal	server = NULL, serverp = NULL;
5027c478bd9Sstevel@tonic-gate 	krb5_creds	*my_creds;
5037c478bd9Sstevel@tonic-gate 	krb5_timestamp	now;
5047c478bd9Sstevel@tonic-gate 	krb5_error_code	code = 0;
5057c478bd9Sstevel@tonic-gate 	char		kuser[2*MAXHOSTNAMELEN];
5067c478bd9Sstevel@tonic-gate 	krb5_deltat	lifetime;
5077c478bd9Sstevel@tonic-gate 	krb5_deltat	rlife;
5087c478bd9Sstevel@tonic-gate 	krb5_deltat	krb5_max_duration;
5097c478bd9Sstevel@tonic-gate 	int		options = KRB5_DEFAULT_OPTIONS;
5107c478bd9Sstevel@tonic-gate 	krb5_data tgtname = {
5117c478bd9Sstevel@tonic-gate 		0,
5127c478bd9Sstevel@tonic-gate 		KRB5_TGS_NAME_SIZE,
5137c478bd9Sstevel@tonic-gate 		KRB5_TGS_NAME
5147c478bd9Sstevel@tonic-gate 	};
515488060a6SWill Fiveash 	krb5_get_init_creds_opt *opts = NULL;
5163125ebfcSsemery 	krb5_kdc_rep *as_reply = NULL;
5177c478bd9Sstevel@tonic-gate 	/*
5187c478bd9Sstevel@tonic-gate 	 * "result" should not be assigned PAM_SUCCESS unless
5197c478bd9Sstevel@tonic-gate 	 * authentication has succeeded and there are no other errors.
5207c478bd9Sstevel@tonic-gate 	 *
5217c478bd9Sstevel@tonic-gate 	 * "code" is sometimes used for PAM codes, sometimes for krb5
5227c478bd9Sstevel@tonic-gate 	 * codes.  Be careful.
5237c478bd9Sstevel@tonic-gate 	 */
5247c478bd9Sstevel@tonic-gate 	int result = PAM_AUTH_ERR;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (kmd->debug)
5273bfb48feSsemery 		__pam_log(LOG_AUTH | LOG_DEBUG,
5287c478bd9Sstevel@tonic-gate 		    "PAM-KRB5 (auth): attempt_krb5_auth: start: user='%s'",
5297c478bd9Sstevel@tonic-gate 		    user ? user : "<null>");
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/* need to free context with krb5_free_context */
5328ce3ffdfSPeter Shoults 	if (code = krb5_init_secure_context(&kmd->kcontext)) {
5333bfb48feSsemery 		__pam_log(LOG_AUTH | LOG_ERR,
5347c478bd9Sstevel@tonic-gate 		    "PAM-KRB5 (auth): Error initializing "
5353bfb48feSsemery 		    "krb5: %s",
5367c478bd9Sstevel@tonic-gate 		    error_message(code));
5377c478bd9Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
540*cbea7acaSDominik Hassler 	if ((code = get_kmd_kuser(kmd->kcontext, user, kuser,
5417c478bd9Sstevel@tonic-gate 	    2 * MAXHOSTNAMELEN)) != 0) {
5427c478bd9Sstevel@tonic-gate 		/* get_kmd_kuser returns proper PAM error statuses */
5437c478bd9Sstevel@tonic-gate 		return (code);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if ((code = krb5_parse_name(kmd->kcontext, kuser, &me)) != 0) {
5477c478bd9Sstevel@tonic-gate 		krb5_free_context(kmd->kcontext);
5487c478bd9Sstevel@tonic-gate 		kmd->kcontext = NULL;
5493bfb48feSsemery 		return (PAM_SYSTEM_ERR);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/* call krb5_free_cred_contents() on error */
5537c478bd9Sstevel@tonic-gate 	my_creds = &kmd->initcreds;
5547c478bd9Sstevel@tonic-gate 
5553bfb48feSsemery 	if ((code =
5563bfb48feSsemery 	    krb5_copy_principal(kmd->kcontext, me, &my_creds->client))) {
5573bfb48feSsemery 		result = PAM_SYSTEM_ERR;
5587c478bd9Sstevel@tonic-gate 		goto out_err;
5593bfb48feSsemery 	}
56067c90040Ssemery 	clientp = my_creds->client;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (code = krb5_build_principal_ext(kmd->kcontext, &server,
5637c478bd9Sstevel@tonic-gate 	    krb5_princ_realm(kmd->kcontext, me)->length,
5647c478bd9Sstevel@tonic-gate 	    krb5_princ_realm(kmd->kcontext, me)->data,
5657c478bd9Sstevel@tonic-gate 	    tgtname.length, tgtname.data,
5667c478bd9Sstevel@tonic-gate 	    krb5_princ_realm(kmd->kcontext, me)->length,
5677c478bd9Sstevel@tonic-gate 	    krb5_princ_realm(kmd->kcontext, me)->data, 0)) {
5683bfb48feSsemery 		__pam_log(LOG_AUTH | LOG_ERR,
5697c478bd9Sstevel@tonic-gate 		    "PAM-KRB5 (auth): attempt_krb5_auth: "
5703bfb48feSsemery 		    "krb5_build_princ_ext failed: %s",
5717c478bd9Sstevel@tonic-gate 		    error_message(code));
5723bfb48feSsemery 		result = PAM_SYSTEM_ERR;
5737c478bd9Sstevel@tonic-gate 		goto out;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (code = krb5_copy_principal(kmd->kcontext, server,
5777c478bd9Sstevel@tonic-gate 	    &my_creds->server)) {
5783bfb48feSsemery 		result = PAM_SYSTEM_ERR;
5797c478bd9Sstevel@tonic-gate 		goto out_err;
5807c478bd9Sstevel@tonic-gate 	}
58167c90040Ssemery 	serverp = my_creds->server;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (code = krb5_timeofday(kmd->kcontext, &now)) {
5843bfb48feSsemery 		__pam_log(LOG_AUTH | LOG_ERR,
5857c478bd9Sstevel@tonic-gate 		    "PAM-KRB5 (auth): attempt_krb5_auth: "
5863bfb48feSsemery 		    "krb5_timeofday failed: %s",
5877c478bd9Sstevel@tonic-gate 		    error_message(code));
5883bfb48feSsemery 		result = PAM_SYSTEM_ERR;
5897c478bd9Sstevel@tonic-gate 		goto out;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * set the values for lifetime and rlife to be the maximum
5947c478bd9Sstevel@tonic-gate 	 * possible
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	krb5_max_duration = KRB5_KDB_EXPIRATION - now - 60*60;
5977c478bd9Sstevel@tonic-gate 	lifetime = krb5_max_duration;
5987c478bd9Sstevel@tonic-gate 	rlife = krb5_max_duration;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6017c478bd9Sstevel@tonic-gate 	 * Let us get the values for various options
6027c478bd9Sstevel@tonic-gate 	 * from Kerberos configuration file
6037c478bd9Sstevel@tonic-gate 	 */
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	krb_realm = krb5_princ_realm(kmd->kcontext, me)->data;
6067c478bd9Sstevel@tonic-gate 	profile_get_options_boolean(kmd->kcontext->profile,
6077c478bd9Sstevel@tonic-gate 	    realmdef, config_option);
6087c478bd9Sstevel@tonic-gate 	profile_get_options_boolean(kmd->kcontext->profile,
6097c478bd9Sstevel@tonic-gate 	    appdef, config_option);
6107c478bd9Sstevel@tonic-gate 	profile_get_options_string(kmd->kcontext->profile,
6117c478bd9Sstevel@tonic-gate 	    realmdef, config_times);
6127c478bd9Sstevel@tonic-gate 	profile_get_options_string(kmd->kcontext->profile,
6137c478bd9Sstevel@tonic-gate 	    appdef, config_times);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (renew_timeval) {
6167c478bd9Sstevel@tonic-gate 		code = krb5_string_to_deltat(renew_timeval, &rlife);
6177c478bd9Sstevel@tonic-gate 		if (code != 0 || rlife == 0 || rlife > krb5_max_duration) {
6183bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_ERR,
6197c478bd9Sstevel@tonic-gate 			    "PAM-KRB5 (auth): Bad max_renewable_life "
6203bfb48feSsemery 			    " value '%s' in Kerberos config file",
6217c478bd9Sstevel@tonic-gate 			    renew_timeval);
6227c478bd9Sstevel@tonic-gate 			result = PAM_SYSTEM_ERR;
6237c478bd9Sstevel@tonic-gate 			goto out;
6247c478bd9Sstevel@tonic-gate 		}
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 	if (life_timeval) {
6277c478bd9Sstevel@tonic-gate 		code = krb5_string_to_deltat(life_timeval, &lifetime);
6287c478bd9Sstevel@tonic-gate 		if (code != 0 || lifetime == 0 ||
6297c478bd9Sstevel@tonic-gate 		    lifetime > krb5_max_duration) {
6303bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_ERR,
6313bfb48feSsemery 			    "lifetime value '%s' in Kerberos config file",
6327c478bd9Sstevel@tonic-gate 			    life_timeval);
6337c478bd9Sstevel@tonic-gate 			result = PAM_SYSTEM_ERR;
6347c478bd9Sstevel@tonic-gate 			goto out;
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 	/*  start timer when request gets to KDC */
6387c478bd9Sstevel@tonic-gate 	my_creds->times.starttime = 0;
6397c478bd9Sstevel@tonic-gate 	my_creds->times.endtime = now + lifetime;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if (options & KDC_OPT_RENEWABLE) {
6427c478bd9Sstevel@tonic-gate 		my_creds->times.renew_till = now + rlife;
6437c478bd9Sstevel@tonic-gate 	} else
6447c478bd9Sstevel@tonic-gate 		my_creds->times.renew_till = 0;
6457c478bd9Sstevel@tonic-gate 
646488060a6SWill Fiveash 	code = krb5_get_init_creds_opt_alloc(kmd->kcontext, &opts);
647488060a6SWill Fiveash 	if (code != 0) {
648488060a6SWill Fiveash 		__pam_log(LOG_AUTH | LOG_ERR,
649488060a6SWill Fiveash 		    "Error allocating gic opts: %s",
650488060a6SWill Fiveash 		    error_message(code));
651488060a6SWill Fiveash 		result = PAM_SYSTEM_ERR;
652488060a6SWill Fiveash 		goto out;
653488060a6SWill Fiveash 	}
654488060a6SWill Fiveash 
655488060a6SWill Fiveash 	krb5_get_init_creds_opt_set_tkt_life(opts, lifetime);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (proxiable_flag) { 		/* Set in config file */
6587c478bd9Sstevel@tonic-gate 		if (kmd->debug)
6593bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_DEBUG,
6607c478bd9Sstevel@tonic-gate 			    "PAM-KRB5 (auth): Proxiable tickets "
6613bfb48feSsemery 			    "requested");
662488060a6SWill Fiveash 		krb5_get_init_creds_opt_set_proxiable(opts, TRUE);
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 	if (forwardable_flag) {
6657c478bd9Sstevel@tonic-gate 		if (kmd->debug)
6663bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_DEBUG,
6677c478bd9Sstevel@tonic-gate 			    "PAM-KRB5 (auth): Forwardable tickets "
6683bfb48feSsemery 			    "requested");
669488060a6SWill Fiveash 		krb5_get_init_creds_opt_set_forwardable(opts, TRUE);
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 	if (renewable_flag) {
6727c478bd9Sstevel@tonic-gate 		if (kmd->debug)
6733bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_DEBUG,
6747c478bd9Sstevel@tonic-gate 			    "PAM-KRB5 (auth): Renewable tickets "
6753bfb48feSsemery 			    "requested");
676488060a6SWill Fiveash 		krb5_get_init_creds_opt_set_renew_life(opts, rlife);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 	if (no_address_flag) {
6797c478bd9Sstevel@tonic-gate 		if (kmd->debug)
6803bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_DEBUG,
6817c478bd9Sstevel@tonic-gate 			    "PAM-KRB5 (auth): Addressless tickets "
6823bfb48feSsemery 			    "requested");
683488060a6SWill Fiveash 		krb5_get_init_creds_opt_set_address_list(opts, NULL);
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
68770f41fc1SWill Fiveash 	 * mech_krb5 interprets empty passwords as NULL passwords and tries to
68870f41fc1SWill Fiveash 	 * read a password from stdin. Since we are in pam this is bad and
68970f41fc1SWill Fiveash 	 * should not be allowed.
69070f41fc1SWill Fiveash 	 *
69170f41fc1SWill Fiveash 	 * Note, the logic now is that if the preauth_type is PKINIT then
69270f41fc1SWill Fiveash 	 * provide a proper PAMcentric prompt function that the underlying
69370f41fc1SWill Fiveash 	 * PKINIT preauth plugin will use to prompt for the PIN.
69470f41fc1SWill Fiveash 	 */
69570f41fc1SWill Fiveash 	if (kmd->preauth_type == KRB_PKINIT) {
69670f41fc1SWill Fiveash 		/*
69770f41fc1SWill Fiveash 		 * Do PKINIT preauth
69870f41fc1SWill Fiveash 		 *
69970f41fc1SWill Fiveash 		 * Note: we want to limit preauth types to just those for PKINIT
70070f41fc1SWill Fiveash 		 * but krb5_get_init_creds() doesn't support that at this point.
70170f41fc1SWill Fiveash 		 * Instead we rely on pam_krb5_prompter() to limit prompts to
70270f41fc1SWill Fiveash 		 * non-password types.  So all we can do here is set the preauth
70370f41fc1SWill Fiveash 		 * list so krb5_get_init_creds() will try that first.
70470f41fc1SWill Fiveash 		 */
70570f41fc1SWill Fiveash 		krb5_preauthtype pk_pa_list[] = {
70670f41fc1SWill Fiveash 			KRB5_PADATA_PK_AS_REQ,
70770f41fc1SWill Fiveash 			KRB5_PADATA_PK_AS_REQ_OLD
70870f41fc1SWill Fiveash 		};
709488060a6SWill Fiveash 		krb5_get_init_creds_opt_set_preauth_list(opts, pk_pa_list, 2);
71070f41fc1SWill Fiveash 
711488060a6SWill Fiveash 		if (*krb5_pass == NULL || strlen(*krb5_pass) != 0) {
712488060a6SWill Fiveash 			if (*krb5_pass != NULL) {
713488060a6SWill Fiveash 				/* treat the krb5_pass as a PIN */
714488060a6SWill Fiveash 				code = krb5_get_init_creds_opt_set_pa(
715488060a6SWill Fiveash 				    kmd->kcontext, opts, "PIN", *krb5_pass);
716488060a6SWill Fiveash 			}
717488060a6SWill Fiveash 
718488060a6SWill Fiveash 			if (!code) {
719488060a6SWill Fiveash 				code = __krb5_get_init_creds_password(
720488060a6SWill Fiveash 				    kmd->kcontext,
72170f41fc1SWill Fiveash 				    my_creds,
72270f41fc1SWill Fiveash 				    me,
72370f41fc1SWill Fiveash 				    NULL, /* clear text passwd */
72470f41fc1SWill Fiveash 				    pam_krb5_prompter, /* prompter */
72570f41fc1SWill Fiveash 				    pamh, /* prompter data */
72670f41fc1SWill Fiveash 				    0, /* start time */
72770f41fc1SWill Fiveash 				    NULL, /* defaults to krbtgt@REALM */
728488060a6SWill Fiveash 				    opts,
72970f41fc1SWill Fiveash 				    &as_reply);
730488060a6SWill Fiveash 			}
73170f41fc1SWill Fiveash 		} else {
732488060a6SWill Fiveash 			/* invalid PIN */
73370f41fc1SWill Fiveash 			code = KRB5KRB_AP_ERR_BAD_INTEGRITY;
73470f41fc1SWill Fiveash 		}
73570f41fc1SWill Fiveash 	} else {
73670f41fc1SWill Fiveash 		/*
73770f41fc1SWill Fiveash 		 * Do password based preauths
73870f41fc1SWill Fiveash 		 *
73970f41fc1SWill Fiveash 		 * See earlier PKINIT comment.  We are doing something similar
74070f41fc1SWill Fiveash 		 * here but we do not pass in a prompter (we assume
74170f41fc1SWill Fiveash 		 * pam_authtok_get has already prompted for that).
7427c478bd9Sstevel@tonic-gate 		 */
7437c478bd9Sstevel@tonic-gate 		if (*krb5_pass == NULL || strlen(*krb5_pass) == 0) {
7447c478bd9Sstevel@tonic-gate 			code = KRB5KRB_AP_ERR_BAD_INTEGRITY;
7457c478bd9Sstevel@tonic-gate 		} else {
74670f41fc1SWill Fiveash 			krb5_preauthtype pk_pa_list[] = {
74770f41fc1SWill Fiveash 				KRB5_PADATA_ENC_TIMESTAMP
74870f41fc1SWill Fiveash 			};
74970f41fc1SWill Fiveash 
750488060a6SWill Fiveash 			krb5_get_init_creds_opt_set_preauth_list(opts,
75170f41fc1SWill Fiveash 			    pk_pa_list, 1);
7523125ebfcSsemery 
7533125ebfcSsemery 			/*
75470f41fc1SWill Fiveash 			 * We call our own private version of gic_pwd, because
75570f41fc1SWill Fiveash 			 * we need more information, such as password/account
75670f41fc1SWill Fiveash 			 * expiration, that is found in the as_reply.  The
75770f41fc1SWill Fiveash 			 * "prompter" interface is not granular enough for PAM
75870f41fc1SWill Fiveash 			 * to make use of.
7593125ebfcSsemery 			 */
7603125ebfcSsemery 			code = __krb5_get_init_creds_password(kmd->kcontext,
7617c478bd9Sstevel@tonic-gate 			    my_creds,
7627c478bd9Sstevel@tonic-gate 			    me,
7637c478bd9Sstevel@tonic-gate 			    *krb5_pass,	/* clear text passwd */
7647c478bd9Sstevel@tonic-gate 			    NULL,	/* prompter */
7657c478bd9Sstevel@tonic-gate 			    NULL,	/* data */
7667c478bd9Sstevel@tonic-gate 			    0,		/* start time */
7677c478bd9Sstevel@tonic-gate 			    NULL,	/* defaults to krbtgt@REALM */
768488060a6SWill Fiveash 			    opts,
7693125ebfcSsemery 			    &as_reply);
7707c478bd9Sstevel@tonic-gate 		}
77170f41fc1SWill Fiveash 	}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (kmd->debug)
7743bfb48feSsemery 		__pam_log(LOG_AUTH | LOG_DEBUG,
7757c478bd9Sstevel@tonic-gate 		    "PAM-KRB5 (auth): attempt_krb5_auth: "
7767c478bd9Sstevel@tonic-gate 		    "krb5_get_init_creds_password returns: %s",
7777c478bd9Sstevel@tonic-gate 		    code == 0 ? "SUCCESS" : error_message(code));
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	switch (code) {
7807c478bd9Sstevel@tonic-gate 	case 0:
7817c478bd9Sstevel@tonic-gate 		/* got a tgt, let's verify it */
7827c478bd9Sstevel@tonic-gate 		if (verify_tik) {
7837c478bd9Sstevel@tonic-gate 			krb5_verify_init_creds_opt vopts;
7847c478bd9Sstevel@tonic-gate 
785a0709436Smp153739 			krb5_principal sp = NULL;
786a0709436Smp153739 			char kt_name[MAX_KEYTAB_NAME_LEN];
787a0709436Smp153739 			char *fqdn;
788a0709436Smp153739 
7897c478bd9Sstevel@tonic-gate 			krb5_verify_init_creds_opt_init(&vopts);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 			code = krb5_verify_init_creds(kmd->kcontext,
7927c478bd9Sstevel@tonic-gate 			    my_creds,
7937c478bd9Sstevel@tonic-gate 			    NULL,	/* defaults to host/localhost@REALM */
7947c478bd9Sstevel@tonic-gate 			    NULL,
7957c478bd9Sstevel@tonic-gate 			    NULL,
7967c478bd9Sstevel@tonic-gate 			    &vopts);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 			if (code) {
7997c478bd9Sstevel@tonic-gate 				result = PAM_SYSTEM_ERR;
8007c478bd9Sstevel@tonic-gate 
801a0709436Smp153739 				/*
8020be37caaSsemery 				 * Give a better error message when the
8030be37caaSsemery 				 * keytable entry isn't found or the keytab
8040be37caaSsemery 				 * file cannot be found.
805a0709436Smp153739 				 */
806a0709436Smp153739 				if (krb5_sname_to_principal(kmd->kcontext, NULL,
807a0709436Smp153739 				    NULL, KRB5_NT_SRV_HST, &sp))
808a0709436Smp153739 					fqdn = "<fqdn>";
809a0709436Smp153739 				else
810a0709436Smp153739 					fqdn = sp->data[1].data;
811a0709436Smp153739 
812a0709436Smp153739 				if (krb5_kt_default_name(kmd->kcontext, kt_name,
813a0709436Smp153739 				    sizeof (kt_name)))
814b0c1f5b7SWill Fiveash 					(void) strlcpy(kt_name,
815a0709436Smp153739 					    "default keytab",
816a0709436Smp153739 					    sizeof (kt_name));
817a0709436Smp153739 
818a0709436Smp153739 				switch (code) {
819a0709436Smp153739 				case KRB5_KT_NOTFOUND:
8203bfb48feSsemery 					__pam_log(LOG_AUTH | LOG_ERR,
8217c478bd9Sstevel@tonic-gate 					    "PAM-KRB5 (auth): "
822a0709436Smp153739 					    "krb5_verify_init_creds failed:"
823a0709436Smp153739 					    " Key table entry \"host/%s\""
8243bfb48feSsemery 					    " not found in %s",
825a0709436Smp153739 					    fqdn, kt_name);
826a0709436Smp153739 					break;
827a0709436Smp153739 				case ENOENT:
8283bfb48feSsemery 					__pam_log(LOG_AUTH | LOG_ERR,
829a0709436Smp153739 					    "PAM-KRB5 (auth): "
830a0709436Smp153739 					    "krb5_verify_init_creds failed:"
831a0709436Smp153739 					    " Keytab file \"%s\""
8323bfb48feSsemery 					    " does not exist.\n",
833a0709436Smp153739 					    kt_name);
834a0709436Smp153739 					break;
835a0709436Smp153739 				default:
8363bfb48feSsemery 					__pam_log(LOG_AUTH | LOG_ERR,
837a0709436Smp153739 					    "PAM-KRB5 (auth): "
838a0709436Smp153739 					    "krb5_verify_init_creds failed:"
8393bfb48feSsemery 					    " %s",
8407c478bd9Sstevel@tonic-gate 					    error_message(code));
841a0709436Smp153739 					break;
842a0709436Smp153739 				}
843a0709436Smp153739 
844a0709436Smp153739 				if (sp)
845a0709436Smp153739 					krb5_free_principal(kmd->kcontext, sp);
8467c478bd9Sstevel@tonic-gate 			}
8477c478bd9Sstevel@tonic-gate 		}
8483125ebfcSsemery 
8493125ebfcSsemery 		if (code == 0)
8503125ebfcSsemery 			kmd->expiration = as_reply->enc_part2->key_exp;
8513125ebfcSsemery 
8527c478bd9Sstevel@tonic-gate 		break;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
8557c478bd9Sstevel@tonic-gate 		/*
8567c478bd9Sstevel@tonic-gate 		 * Since this principal is not part of the local
8577c478bd9Sstevel@tonic-gate 		 * Kerberos realm, we just return PAM_USER_UNKNOWN.
8587c478bd9Sstevel@tonic-gate 		 */
8597c478bd9Sstevel@tonic-gate 		result = PAM_USER_UNKNOWN;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		if (kmd->debug)
8623bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_DEBUG,
8633bfb48feSsemery 			    "PAM-KRB5 (auth): attempt_krb5_auth:"
8647c478bd9Sstevel@tonic-gate 			    " User is not part of the local Kerberos"
8657c478bd9Sstevel@tonic-gate 			    " realm: %s", error_message(code));
8667c478bd9Sstevel@tonic-gate 		break;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	case KRB5KDC_ERR_PREAUTH_FAILED:
8697c478bd9Sstevel@tonic-gate 	case KRB5KRB_AP_ERR_BAD_INTEGRITY:
8707c478bd9Sstevel@tonic-gate 		/*
8717c478bd9Sstevel@tonic-gate 		 * We could be trying the password from a previous
8727c478bd9Sstevel@tonic-gate 		 * pam authentication module, but we don't want to
8737c478bd9Sstevel@tonic-gate 		 * generate an error if the unix password is different
8747c478bd9Sstevel@tonic-gate 		 * than the Kerberos password...
8757c478bd9Sstevel@tonic-gate 		 */
8767c478bd9Sstevel@tonic-gate 		break;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	case KRB5KDC_ERR_KEY_EXP:
8797c478bd9Sstevel@tonic-gate 		if (!kmd->err_on_exp) {
8807c478bd9Sstevel@tonic-gate 			/*
88170f41fc1SWill Fiveash 			 * Request a tik for changepw service and it will tell
88270f41fc1SWill Fiveash 			 * us if pw is good or not. If PKINIT is being done it
88370f41fc1SWill Fiveash 			 * is possible that *krb5_pass may be NULL so check for
88470f41fc1SWill Fiveash 			 * that.  If that is the case this function will return
88570f41fc1SWill Fiveash 			 * an error.
8867c478bd9Sstevel@tonic-gate 			 */
88770f41fc1SWill Fiveash 			if (*krb5_pass != NULL) {
88870f41fc1SWill Fiveash 				code = krb5_verifypw(kuser, *krb5_pass,
88970f41fc1SWill Fiveash 				    kmd->debug);
89070f41fc1SWill Fiveash 				if (kmd->debug) {
8913bfb48feSsemery 					__pam_log(LOG_AUTH | LOG_DEBUG,
89270f41fc1SWill Fiveash 					    "PAM-KRB5 (auth): "
89370f41fc1SWill Fiveash 					    "attempt_krb5_auth: "
8942278144aSsemery 					    "verifypw %d", code);
89570f41fc1SWill Fiveash 				}
8967c478bd9Sstevel@tonic-gate 				if (code == 0) {
89770f41fc1SWill Fiveash 					/*
89870f41fc1SWill Fiveash 					 * pw is good, set age status for
89970f41fc1SWill Fiveash 					 * acct_mgmt.
90070f41fc1SWill Fiveash 					 */
9017c478bd9Sstevel@tonic-gate 					kmd->age_status = PAM_NEW_AUTHTOK_REQD;
9027c478bd9Sstevel@tonic-gate 				}
9037c478bd9Sstevel@tonic-gate 			}
90470f41fc1SWill Fiveash 
90570f41fc1SWill Fiveash 		}
9067c478bd9Sstevel@tonic-gate 		break;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	default:
9097c478bd9Sstevel@tonic-gate 		result = PAM_SYSTEM_ERR;
9107c478bd9Sstevel@tonic-gate 		if (kmd->debug)
9113bfb48feSsemery 			__pam_log(LOG_AUTH | LOG_DEBUG,
9123bfb48feSsemery 			    "PAM-KRB5 (auth): error %d - %s",
9137c478bd9Sstevel@tonic-gate 			    code, error_message(code));
9147c478bd9Sstevel@tonic-gate 		break;
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if (code == 0) {
9187c478bd9Sstevel@tonic-gate 		/*
91970f41fc1SWill Fiveash 		 * success for the entered pw or PKINIT succeeded.
9207c478bd9Sstevel@tonic-gate 		 *
9217c478bd9Sstevel@tonic-gate 		 * we can't rely on the pw in PAM_AUTHTOK
9227c478bd9Sstevel@tonic-gate 		 * to be the (correct) krb5 one so
9237c478bd9Sstevel@tonic-gate 		 * store krb5 pw in module data for
92470f41fc1SWill Fiveash 		 * use in acct_mgmt.  Note that *krb5_pass may be NULL if we're
92570f41fc1SWill Fiveash 		 * doing PKINIT.
9267c478bd9Sstevel@tonic-gate 		 */
92770f41fc1SWill Fiveash 		if (*krb5_pass != NULL &&
92870f41fc1SWill Fiveash 		    !(kmd->password = strdup(*krb5_pass))) {
92970f41fc1SWill Fiveash 			__pam_log(LOG_AUTH | LOG_ERR,
93070f41fc1SWill Fiveash 			    "Cannot strdup password");
9317c478bd9Sstevel@tonic-gate 			result = PAM_BUF_ERR;
9327c478bd9Sstevel@tonic-gate 			goto out_err;
9337c478bd9Sstevel@tonic-gate 		}
93470f41fc1SWill Fiveash 
9357c478bd9Sstevel@tonic-gate 		result = PAM_SUCCESS;
9367c478bd9Sstevel@tonic-gate 		goto out;
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate out_err:
9407c478bd9Sstevel@tonic-gate 	/* jump (or reach) here if error and cred cache has been init */
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	if (kmd->debug)
9433bfb48feSsemery 		__pam_log(LOG_AUTH | LOG_DEBUG,
9447c478bd9Sstevel@tonic-gate 		    "PAM-KRB5 (auth): clearing initcreds in "
9457c478bd9Sstevel@tonic-gate 		    "pam_authenticate()");
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	krb5_free_cred_contents(kmd->kcontext, &kmd->initcreds);
9487c478bd9Sstevel@tonic-gate 	(void) memset((char *)&kmd->initcreds, 0, sizeof (krb5_creds));
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate out:
9517c478bd9Sstevel@tonic-gate 	if (server)
9527c478bd9Sstevel@tonic-gate 		krb5_free_principal(kmd->kcontext, server);
9537c478bd9Sstevel@tonic-gate 	if (me)
9547c478bd9Sstevel@tonic-gate 		krb5_free_principal(kmd->kcontext, me);
9553125ebfcSsemery 	if (as_reply)
9563125ebfcSsemery 		krb5_free_kdc_rep(kmd->kcontext, as_reply);
95767c90040Ssemery 
95867c90040Ssemery 	/*
95967c90040Ssemery 	 * clientp or serverp could be NULL in certain error cases in this
96067c90040Ssemery 	 * function.  mycreds->[client|server] could also be NULL in case
96167c90040Ssemery 	 * of error in this function, see out_err above.  The pointers clientp
96267c90040Ssemery 	 * and serverp reference the input argument in my_creds for
96367c90040Ssemery 	 * get_init_creds and must be freed if the input argument does not
96467c90040Ssemery 	 * match the output argument, which occurs during a successful call
96567c90040Ssemery 	 * to get_init_creds.
96667c90040Ssemery 	 */
96767c90040Ssemery 	if (clientp && my_creds->client && clientp != my_creds->client)
96867c90040Ssemery 		krb5_free_principal(kmd->kcontext, clientp);
96967c90040Ssemery 	if (serverp && my_creds->server && serverp != my_creds->server)
97067c90040Ssemery 		krb5_free_principal(kmd->kcontext, serverp);
97167c90040Ssemery 
9727c478bd9Sstevel@tonic-gate 	if (kmd->kcontext) {
9737c478bd9Sstevel@tonic-gate 		krb5_free_context(kmd->kcontext);
9747c478bd9Sstevel@tonic-gate 		kmd->kcontext = NULL;
9757c478bd9Sstevel@tonic-gate 	}
976488060a6SWill Fiveash 	if (opts)
977488060a6SWill Fiveash 		krb5_get_init_creds_opt_free(kmd->kcontext, opts);
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	if (kmd->debug)
9803bfb48feSsemery 		__pam_log(LOG_AUTH | LOG_DEBUG,
9817c478bd9Sstevel@tonic-gate 		    "PAM-KRB5 (auth): attempt_krb5_auth returning %d",
9827c478bd9Sstevel@tonic-gate 		    result);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	return (kmd->auth_status = result);
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9887c478bd9Sstevel@tonic-gate void
krb5_cleanup(pam_handle_t * pamh,void * data,int pam_status)9897c478bd9Sstevel@tonic-gate krb5_cleanup(pam_handle_t *pamh, void *data, int pam_status)
9907c478bd9Sstevel@tonic-gate {
9917c478bd9Sstevel@tonic-gate 	krb5_module_data_t *kmd = (krb5_module_data_t *)data;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	if (kmd == NULL)
9947c478bd9Sstevel@tonic-gate 		return;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	if (kmd->debug) {
9973bfb48feSsemery 		__pam_log(LOG_AUTH | LOG_DEBUG,
9983bfb48feSsemery 		    "PAM-KRB5 (auth): krb5_cleanup auth_status = %d",
9997c478bd9Sstevel@tonic-gate 		    kmd->auth_status);
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	/*
100367c90040Ssemery 	 * Apps could be calling pam_end here, so we should always clean
100467c90040Ssemery 	 * up regardless of success or failure here.
10057c478bd9Sstevel@tonic-gate 	 */
100667c90040Ssemery 	if (kmd->ccache)
10075ad42b1bSSurya Prakki 		(void) krb5_cc_close(kmd->kcontext, kmd->ccache);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if (kmd->password) {
10107c478bd9Sstevel@tonic-gate 		(void) memset(kmd->password, 0, strlen(kmd->password));
10117c478bd9Sstevel@tonic-gate 		free(kmd->password);
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 
101467c90040Ssemery 	if (kmd->user)
10153bfb48feSsemery 		free(kmd->user);
10163bfb48feSsemery 
101767c90040Ssemery 	if (kmd->env)
101867c90040Ssemery 		free(kmd->env);
101967c90040Ssemery 
10207c478bd9Sstevel@tonic-gate 	krb5_free_cred_contents(kmd->kcontext, &kmd->initcreds);
10217c478bd9Sstevel@tonic-gate 	(void) memset((char *)&kmd->initcreds, 0, sizeof (krb5_creds));
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	free(kmd);
10247c478bd9Sstevel@tonic-gate }
1025