117678
diff --git a/auth-krb5.c b/auth-krb5.c
117678
index a5a81ed2..63f877f2 100644
117678
--- a/auth-krb5.c
117678
+++ b/auth-krb5.c
117678
@@ -51,6 +51,7 @@
117678
 #include <unistd.h>
117678
 #include <string.h>
117678
 #include <krb5.h>
117678
+#include <profile.h>
117678
 
117678
 extern ServerOptions	 options;
117678
 
117678
@@ -77,7 +78,7 @@ auth_krb5_password(Authctxt *authctxt, const char *password)
117678
 #endif
117678
 	krb5_error_code problem;
117678
 	krb5_ccache ccache = NULL;
117678
-	int len;
117678
+	char *ticket_name = NULL;
117678
 	char *client, *platform_client;
117678
 	const char *errmsg;
117678
 
117678
@@ -163,7 +164,8 @@ auth_krb5_password(Authctxt *authctxt, const char *password)
117678
 		goto out;
117678
 	}
117678
 
117678
-	problem = ssh_krb5_cc_gen(authctxt->krb5_ctx, &authctxt->krb5_fwd_ccache);
117678
+	problem = ssh_krb5_cc_new_unique(authctxt->krb5_ctx,
117678
+	     &authctxt->krb5_fwd_ccache, &authctxt->krb5_set_env);
117678
 	if (problem)
117678
 		goto out;
117678
 
117678
@@ -172,21 +174,20 @@ auth_krb5_password(Authctxt *authctxt, const char *password)
117678
 	if (problem)
117678
 		goto out;
117678
 
117678
-	problem= krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
117678
+	problem = krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
117678
 				 &creds);
117678
 	if (problem)
117678
 		goto out;
117678
 #endif
117678
 
117678
-	authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
117678
+	problem = krb5_cc_get_full_name(authctxt->krb5_ctx,
117678
+	    authctxt->krb5_fwd_ccache, &ticket_name);
117678
 
117678
-	len = strlen(authctxt->krb5_ticket_file) + 6;
117678
-	authctxt->krb5_ccname = xmalloc(len);
117678
-	snprintf(authctxt->krb5_ccname, len, "FILE:%s",
117678
-	    authctxt->krb5_ticket_file);
117678
+	authctxt->krb5_ccname = xstrdup(ticket_name);
117678
+	krb5_free_string(authctxt->krb5_ctx, ticket_name);
117678
 
117678
 #ifdef USE_PAM
117678
-	if (options.use_pam)
117678
+	if (options.use_pam && authctxt->krb5_set_env)
117678
 		do_pam_putenv("KRB5CCNAME", authctxt->krb5_ccname);
117678
 #endif
117678
 
191bbb
@@ -222,11 +223,54 @@ auth_krb5_password(Authctxt *authctxt, const char *password)
117678
 void
117678
 krb5_cleanup_proc(Authctxt *authctxt)
117678
 {
117678
+	struct stat krb5_ccname_stat;
117678
+	char krb5_ccname[128], *krb5_ccname_dir_start, *krb5_ccname_dir_end;
117678
+
117678
 	debug("krb5_cleanup_proc called");
117678
 	if (authctxt->krb5_fwd_ccache) {
117678
-		krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
117678
+		krb5_context ctx = authctxt->krb5_ctx;
117678
+		krb5_cccol_cursor cursor;
117678
+		krb5_ccache ccache;
117678
+		int ret;
117678
+
117678
+		krb5_cc_destroy(ctx, authctxt->krb5_fwd_ccache);
117678
 		authctxt->krb5_fwd_ccache = NULL;
117678
+
117678
+		ret = krb5_cccol_cursor_new(ctx, &cursor);
117678
+		if (ret)
191bbb
+			goto out;
117678
+
117678
+		ret = krb5_cccol_cursor_next(ctx, cursor, &ccache);
117678
+		if (ret == 0 && ccache != NULL) {
117678
+			/* There is at least one other ccache in collection
117678
+			 * we can switch to */
117678
+			krb5_cc_switch(ctx, ccache);
117678
+		} else {
117678
+			/* Clean up the collection too */
117678
+			strncpy(krb5_ccname, authctxt->krb5_ccname, sizeof(krb5_ccname) - 10);
117678
+			krb5_ccname_dir_start = strchr(krb5_ccname, ':') + 1;
117678
+			*krb5_ccname_dir_start++ = '\0';
117678
+			if (strcmp(krb5_ccname, "DIR") == 0) {
117678
+
117678
+				strcat(krb5_ccname_dir_start, "/primary");
117678
+
117678
+				if (stat(krb5_ccname_dir_start, &krb5_ccname_stat) == 0) {
117678
+					if (unlink(krb5_ccname_dir_start) == 0) {
117678
+						krb5_ccname_dir_end = strrchr(krb5_ccname_dir_start, '/');
117678
+						*krb5_ccname_dir_end = '\0';
117678
+						if (rmdir(krb5_ccname_dir_start) == -1)
117678
+							debug("cache dir '%s' remove failed: %s",
117678
+							    krb5_ccname_dir_start, strerror(errno));
117678
+					}
117678
+					else
117678
+						debug("cache primary file '%s', remove failed: %s",
117678
+						    krb5_ccname_dir_start, strerror(errno));
117678
+				}
117678
+			}
117678
+		}
117678
+		krb5_cccol_cursor_free(ctx, &cursor);
117678
 	}
191bbb
+out:
117678
 	if (authctxt->krb5_user) {
117678
 		krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
191bbb
 		authctxt->krb5_user = NULL;
117678
@@ -237,36 +287,186 @@ krb5_cleanup_proc(Authctxt *authctxt)
117678
 	}
117678
 }
117678
 
117678
-#ifndef HEIMDAL
117678
-krb5_error_code
117678
-ssh_krb5_cc_gen(krb5_context ctx, krb5_ccache *ccache) {
117678
-	int tmpfd, ret, oerrno;
117678
-	char ccname[40];
117678
-	mode_t old_umask;
117678
 
117678
-	ret = snprintf(ccname, sizeof(ccname),
117678
-	    "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
117678
-	if (ret < 0 || (size_t)ret >= sizeof(ccname))
117678
-		return ENOMEM;
117678
-
117678
-	old_umask = umask(0177);
117678
-	tmpfd = mkstemp(ccname + strlen("FILE:"));
117678
-	oerrno = errno;
117678
-	umask(old_umask);
117678
-	if (tmpfd == -1) {
117678
-		logit("mkstemp(): %.100s", strerror(oerrno));
117678
-		return oerrno;
117678
+#if !defined(HEIMDAL)
117678
+int
117678
+ssh_asprintf_append(char **dsc, const char *fmt, ...) {
117678
+	char *src, *old;
117678
+	va_list ap;
117678
+	int i;
117678
+
117678
+	va_start(ap, fmt);
117678
+	i = vasprintf(&src, fmt, ap);
117678
+	va_end(ap);
117678
+
117678
+	if (i == -1 || src == NULL)
117678
+		return -1;
117678
+
117678
+	old = *dsc;
117678
+
117678
+	i = asprintf(dsc, "%s%s", *dsc, src);
117678
+	if (i == -1 || src == NULL) {
117678
+		free(src);
117678
+		return -1;
117678
+	}
117678
+
117678
+	free(old);
117678
+	free(src);
117678
+
117678
+	return i;
117678
+}
117678
+
117678
+int
117678
+ssh_krb5_expand_template(char **result, const char *template) {
117678
+	char *p_n, *p_o, *r, *tmp_template;
117678
+
117678
+	debug3("%s: called, template = %s", __func__, template);
117678
+	if (template == NULL)
117678
+		return -1;
117678
+
117678
+	tmp_template = p_n = p_o = xstrdup(template);
117678
+	r = xstrdup("");
117678
+
117678
+	while ((p_n = strstr(p_o, "%{")) != NULL) {
117678
+
117678
+		*p_n++ = '\0';
117678
+		if (ssh_asprintf_append(&r, "%s", p_o) == -1)
117678
+			goto cleanup;
117678
+
117678
+		if (strncmp(p_n, "{uid}", 5) == 0 || strncmp(p_n, "{euid}", 6) == 0 ||
117678
+			strncmp(p_n, "{USERID}", 8) == 0) {
117678
+			p_o = strchr(p_n, '}') + 1;
117678
+			if (ssh_asprintf_append(&r, "%d", geteuid()) == -1)
117678
+				goto cleanup;
117678
+			continue;
117678
+		}
117678
+		else if (strncmp(p_n, "{TEMP}", 6) == 0) {
117678
+			p_o = strchr(p_n, '}') + 1;
117678
+			if (ssh_asprintf_append(&r, "/tmp") == -1)
117678
+				goto cleanup;
117678
+			continue;
117678
+		} else {
117678
+			p_o = strchr(p_n, '}') + 1;
117678
+			p_o = '\0';
117678
+			debug("%s: unsupported token %s in %s", __func__, p_n, template);
117678
+			/* unknown token, fallback to the default */
117678
+			goto cleanup;
117678
+		}
117678
 	}
117678
 
117678
-	if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
117678
+	if (ssh_asprintf_append(&r, "%s", p_o) == -1)
117678
+		goto cleanup;
117678
+
117678
+	*result = r;
117678
+	free(tmp_template);
117678
+	return 0;
117678
+
117678
+cleanup:
117678
+	free(r);
117678
+	free(tmp_template);
117678
+	return -1;
117678
+}
117678
+
117678
+krb5_error_code
117678
+ssh_krb5_get_cctemplate(krb5_context ctx, char **ccname) {
117678
+	profile_t p;
117678
+	int ret = 0;
117678
+	char *value = NULL;
117678
+
117678
+	debug3("%s: called", __func__);
117678
+	ret = krb5_get_profile(ctx, &p);
117678
+	if (ret)
117678
+		return ret;
117678
+
117678
+	ret = profile_get_string(p, "libdefaults", "default_ccache_name", NULL, NULL, &value);
117678
+	if (ret || !value)
117678
+		return ret;
117678
+
117678
+	ret = ssh_krb5_expand_template(ccname, value);
117678
+
117678
+	debug3("%s: returning with ccname = %s", __func__, *ccname);
117678
+	return ret;
117678
+}
117678
+
117678
+krb5_error_code
117678
+ssh_krb5_cc_new_unique(krb5_context ctx, krb5_ccache *ccache, int *need_environment) {
117678
+	int tmpfd, ret, oerrno, type_len;
117678
+	char *ccname = NULL;
117678
+	mode_t old_umask;
117678
+	char *type = NULL, *colon = NULL;
117678
+
117678
+	debug3("%s: called", __func__);
117678
+	if (need_environment)
117678
+		*need_environment = 0;
117678
+	ret = ssh_krb5_get_cctemplate(ctx, &ccname);
117678
+	if (ret || !ccname || options.kerberos_unique_ticket) {
117678
+		/* Otherwise, go with the old method */
117678
+		if (ccname)
117678
+			free(ccname);
117678
+		ccname = NULL;
117678
+
117678
+		ret = asprintf(&ccname,
117678
+		    "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
117678
+		if (ret < 0)
117678
+			return ENOMEM;
117678
+
117678
+		old_umask = umask(0177);
117678
+		tmpfd = mkstemp(ccname + strlen("FILE:"));
117678
 		oerrno = errno;
117678
-		logit("fchmod(): %.100s", strerror(oerrno));
117678
+		umask(old_umask);
117678
+		if (tmpfd == -1) {
117678
+			logit("mkstemp(): %.100s", strerror(oerrno));
117678
+			return oerrno;
117678
+		}
117678
+
117678
+		if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
117678
+			oerrno = errno;
117678
+			logit("fchmod(): %.100s", strerror(oerrno));
117678
+			close(tmpfd);
117678
+			return oerrno;
117678
+		}
117678
+		/* make sure the KRB5CCNAME is set for non-standard location */
117678
+		if (need_environment)
117678
+			*need_environment = 1;
117678
 		close(tmpfd);
117678
-		return oerrno;
117678
 	}
117678
-	close(tmpfd);
117678
 
117678
-	return (krb5_cc_resolve(ctx, ccname, ccache));
117678
+	debug3("%s: setting default ccname to %s", __func__, ccname);
117678
+	/* set the default with already expanded user IDs */
117678
+	ret = krb5_cc_set_default_name(ctx, ccname);
117678
+	if (ret)
117678
+		return ret;
117678
+
117678
+	if ((colon = strstr(ccname, ":")) != NULL) {
117678
+		type_len = colon - ccname;
117678
+		type = malloc((type_len + 1) * sizeof(char));
117678
+		if (type == NULL)
117678
+			return ENOMEM;
117678
+		strncpy(type, ccname, type_len);
117678
+		type[type_len] = 0;
117678
+	} else {
117678
+		type = strdup(ccname);
117678
+	}
117678
+
117678
+	/* If we have a credential cache from krb5.conf, we need to switch
117678
+	 * a primary cache for this collection, if it supports that (non-FILE)
117678
+	 */
117678
+	if (krb5_cc_support_switch(ctx, type)) {
117678
+		debug3("%s: calling cc_new_unique(%s)", __func__, ccname);
117678
+		ret = krb5_cc_new_unique(ctx, type, NULL, ccache);
117678
+		if (ret)
117678
+			return ret;
117678
+
117678
+		debug3("%s: calling cc_switch()", __func__);
117678
+		return krb5_cc_switch(ctx, *ccache);
117678
+	} else {
117678
+		/* Otherwise, we can not create a unique ccname here (either
117678
+		 * it is already unique from above or the type does not support
117678
+		 * collections
117678
+		 */
117678
+		debug3("%s: calling cc_resolve(%s)", __func__, ccname);
117678
+		return (krb5_cc_resolve(ctx, ccname, ccache));
117678
+	}
117678
 }
117678
 #endif /* !HEIMDAL */
117678
 #endif /* KRB5 */
117678
diff --git a/auth.h b/auth.h
117678
index 29491df9..fdab5040 100644
117678
--- a/auth.h
117678
+++ b/auth.h
117678
@@ -82,6 +82,7 @@ struct Authctxt {
117678
 	krb5_principal	 krb5_user;
117678
 	char		*krb5_ticket_file;
117678
 	char		*krb5_ccname;
117678
+	int		 krb5_set_env;
117678
 #endif
117678
 	struct sshbuf	*loginmsg;
117678
 
117678
@@ -243,6 +244,6 @@ int	 sys_auth_passwd(struct ssh *, const char *);
117678
 
117678
 #if defined(KRB5) && !defined(HEIMDAL)
117678
 #include <krb5.h>
117678
-krb5_error_code ssh_krb5_cc_gen(krb5_context, krb5_ccache *);
117678
+krb5_error_code ssh_krb5_cc_new_unique(krb5_context, krb5_ccache *, int *);
117678
 #endif
117678
 #endif
117678
diff --git a/gss-serv-krb5.c b/gss-serv-krb5.c
117678
index 795992d9..0623a107 100644
117678
--- a/gss-serv-krb5.c
117678
+++ b/gss-serv-krb5.c
117678
@@ -114,7 +114,7 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
117678
 /* This writes out any forwarded credentials from the structure populated
117678
  * during userauth. Called after we have setuid to the user */
117678
 
117678
-static void
117678
+static int
117678
 ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
117678
 {
117678
 	krb5_ccache ccache;
117678
@@ -121,16 +121,17 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
117678
 	krb5_error_code problem;
117678
 	krb5_principal princ;
117678
 	OM_uint32 maj_status, min_status;
117678
-	const char *new_ccname, *new_cctype;
117678
+	int len;
117678
 	const char *errmsg;
117678
+	int set_env = 0;
117678
 
117678
 	if (client->creds == NULL) {
117678
 		debug("No credentials stored");
117678
-		return;
117678
+		return 0;
117678
 	}
117678
 
117678
 	if (ssh_gssapi_krb5_init() == 0)
117678
-		return;
117678
+		return 0;
117678
 
117678
 #ifdef HEIMDAL
117678
 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
117678
@@ -144,14 +145,14 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
117678
 		krb5_get_err_text(krb_context, problem));
117678
 # endif
117678
 		krb5_free_error_message(krb_context, errmsg);
117678
-		return;
117678
+		return 0;
117678
 	}
117678
 #else
117678
-	if ((problem = ssh_krb5_cc_gen(krb_context, &ccache))) {
117678
+	if ((problem = ssh_krb5_cc_new_unique(krb_context, &ccache, &set_env)) != 0) {
117678
 		errmsg = krb5_get_error_message(krb_context, problem);
117678
-		logit("ssh_krb5_cc_gen(): %.100s", errmsg);
117678
+		logit("ssh_krb5_cc_new_unique(): %.100s", errmsg);
117678
 		krb5_free_error_message(krb_context, errmsg);
117678
-		return;
117678
+		return 0;
117678
 	}
117678
 #endif	/* #ifdef HEIMDAL */
117678
 
117678
@@ -160,7 +161,7 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
117678
 		errmsg = krb5_get_error_message(krb_context, problem);
117678
 		logit("krb5_parse_name(): %.100s", errmsg);
117678
 		krb5_free_error_message(krb_context, errmsg);
117678
-		return;
117678
+		return 0;
117678
 	}
117678
 
117678
 	if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
117678
@@ -169,7 +170,7 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
117678
 		krb5_free_error_message(krb_context, errmsg);
117678
 		krb5_free_principal(krb_context, princ);
117678
 		krb5_cc_destroy(krb_context, ccache);
117678
-		return;
117678
+		return 0;
117678
 	}
117678
 
117678
 	krb5_free_principal(krb_context, princ);
117678
@@ -178,37 +179,27 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
117678
 	    client->creds, ccache))) {
117678
 		logit("gss_krb5_copy_ccache() failed");
117678
 		krb5_cc_destroy(krb_context, ccache);
117678
-		return;
117678
+		return 0;
117678
 	}
117678
 
117678
-	new_cctype = krb5_cc_get_type(krb_context, ccache);
117678
-	new_ccname = krb5_cc_get_name(krb_context, ccache);
117678
-
117678
-	client->store.envvar = "KRB5CCNAME";
117678
-#ifdef USE_CCAPI
117678
-	xasprintf(&client->store.envval, "API:%s", new_ccname);
117678
-#else
117678
-	if (new_ccname[0] == ':')
117678
-		new_ccname++;
117678
-	xasprintf(&client->store.envval, "%s:%s", new_cctype, new_ccname);
117678
-	if (strcmp(new_cctype, "DIR") == 0) {
117678
-		char *p;
117678
-		p = strrchr(client->store.envval, '/');
117678
-		if (p)
117678
-			*p = '\0';
117678
-	}
117678
-#endif
117678
+	if (set_env) {
117678
+		const char *filename = krb5_cc_get_name(krb_context, ccache);
117678
+		client->store.envvar = "KRB5CCNAME";
117678
+		len = strlen(filename) + 6;
117678
+		client->store.envval = xmalloc(len);
117678
+		snprintf(client->store.envval, len, "FILE:%s", filename);
117678
+	}
117678
 
117678
 #ifdef USE_PAM
117678
-	if (options.use_pam)
117678
+	if (options.use_pam && set_env)
117678
 		do_pam_putenv(client->store.envvar, client->store.envval);
117678
 #endif
117678
 
117678
 	krb5_cc_close(krb_context, ccache);
117678
 
117678
 	client->store.data = krb_context;
117678
 
117678
-	return;
117678
+	return set_env;
117678
 }
117678
 
117678
 int
117678
diff --git a/gss-serv.c b/gss-serv.c
117678
index 6cae720e..16e55cbc 100644
117678
--- a/gss-serv.c
117678
+++ b/gss-serv.c
117678
@@ -320,13 +320,15 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
117678
 }
117678
 
117678
 /* As user */
117678
-void
117678
+int
117678
 ssh_gssapi_storecreds(void)
117678
 {
117678
 	if (gssapi_client.mech && gssapi_client.mech->storecreds) {
117678
-		(*gssapi_client.mech->storecreds)(&gssapi_client);
117678
+		return (*gssapi_client.mech->storecreds)(&gssapi_client);
117678
 	} else
117678
 		debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
117678
+
117678
+	return 0;
117678
 }
117678
 
117678
 /* This allows GSSAPI methods to do things to the childs environment based
117678
diff --git a/servconf.c b/servconf.c
117678
index cb578658..a6e01df2 100644
117678
--- a/servconf.c
117678
+++ b/servconf.c
117678
@@ -122,6 +122,7 @@ initialize_server_options(ServerOptions *options)
117678
 	options->kerberos_or_local_passwd = -1;
117678
 	options->kerberos_ticket_cleanup = -1;
117678
 	options->kerberos_get_afs_token = -1;
117678
+	options->kerberos_unique_ticket = -1;
117678
 	options->gss_authentication=-1;
117678
 	options->gss_keyex = -1;
117678
 	options->gss_cleanup_creds = -1;
117678
@@ -315,6 +316,8 @@ fill_default_server_options(ServerOptions *options)
117678
 		options->kerberos_ticket_cleanup = 1;
117678
 	if (options->kerberos_get_afs_token == -1)
117678
 		options->kerberos_get_afs_token = 0;
117678
+	if (options->kerberos_unique_ticket == -1)
117678
+		options->kerberos_unique_ticket = 0;
117678
 	if (options->gss_authentication == -1)
117678
 		options->gss_authentication = 0;
117678
 	if (options->gss_keyex == -1)
117678
@@ -447,7 +450,8 @@ typedef enum {
117678
 	sPermitRootLogin, sLogFacility, sLogLevel,
117678
 	sRhostsRSAAuthentication, sRSAAuthentication,
117678
 	sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
117678
-	sKerberosGetAFSToken, sChallengeResponseAuthentication,
117678
+	sKerberosGetAFSToken, sKerberosUniqueTicket,
117678
+	sChallengeResponseAuthentication,
117678
 	sPasswordAuthentication, sKbdInteractiveAuthentication,
117678
 	sListenAddress, sAddressFamily,
117678
 	sPrintMotd, sPrintLastLog, sIgnoreRhosts,
117678
@@ -526,11 +530,13 @@ static struct {
117678
 #else
117678
 	{ "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
117678
 #endif
117678
+	{ "kerberosuniqueticket", sKerberosUniqueTicket, SSHCFG_GLOBAL },
117678
 #else
117678
 	{ "kerberosauthentication", sUnsupported, SSHCFG_ALL },
117678
 	{ "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
117678
 	{ "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
117678
 	{ "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
117678
+	{ "kerberosuniqueticket", sUnsupported, SSHCFG_GLOBAL },
117678
 #endif
117678
 	{ "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
117678
 	{ "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
117678
@@ -1437,6 +1443,10 @@ process_server_config_line(ServerOptions *options, char *line,
117678
 		intptr = &options->kerberos_get_afs_token;
117678
 		goto parse_flag;
117678
 
117678
+	case sKerberosUniqueTicket:
117678
+		intptr = &options->kerberos_unique_ticket;
117678
+		goto parse_flag;
117678
+
117678
 	case sGssAuthentication:
117678
 		intptr = &options->gss_authentication;
117678
 		goto parse_flag;
117678
@@ -2507,6 +2517,7 @@ dump_config(ServerOptions *o)
117678
 # ifdef USE_AFS
117678
 	dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
117678
 # endif
117678
+	dump_cfg_fmtint(sKerberosUniqueTicket, o->kerberos_unique_ticket);
117678
 #endif
117678
 #ifdef GSSAPI
117678
 	dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
117678
diff --git a/servconf.h b/servconf.h
117678
index db8362c6..4fa42d64 100644
117678
--- a/servconf.h
117678
+++ b/servconf.h
117678
@@ -123,6 +123,8 @@ typedef struct {
117678
 						 * file on logout. */
117678
 	int     kerberos_get_afs_token;		/* If true, try to get AFS token if
117678
 						 * authenticated with Kerberos. */
117678
+	int     kerberos_unique_ticket;		/* If true, the aquired ticket will
117678
+						 * be stored in per-session ccache */
117678
 	int     gss_authentication;	/* If true, permit GSSAPI authentication */
117678
 	int     gss_keyex;		/* If true, permit GSSAPI key exchange */
117678
 	int     gss_cleanup_creds;	/* If true, destroy cred cache on logout */
117678
diff --git a/session.c b/session.c
117678
index 85df6a27..480a5ead 100644
117678
--- a/session.c
117678
+++ b/session.c
117678
@@ -1033,7 +1033,8 @@ do_setup_env(struct ssh *ssh, Session *s, const char *shell)
117678
 	/* Allow any GSSAPI methods that we've used to alter
117678
 	 * the childs environment as they see fit
117678
 	 */
117678
-	ssh_gssapi_do_child(&env, &envsize);
117678
+	if (s->authctxt->krb5_set_env)
117678
+		ssh_gssapi_do_child(&env, &envsize);
117678
 #endif
117678
 
117678
 	/* Set basic environment. */
117678
@@ -1105,7 +1106,7 @@ do_setup_env(struct ssh *ssh, Session *s, const char *shell)
117678
 	}
117678
 #endif
117678
 #ifdef KRB5
117678
-	if (s->authctxt->krb5_ccname)
117678
+	if (s->authctxt->krb5_ccname && s->authctxt->krb5_set_env)
117678
 		child_set_env(&env, &envsize, "KRB5CCNAME",
117678
 		    s->authctxt->krb5_ccname);
117678
 #endif
117678
diff --git a/ssh-gss.h b/ssh-gss.h
117678
index 6593e422..245178af 100644
117678
--- a/ssh-gss.h
117678
+++ b/ssh-gss.h
117678
@@ -62,7 +62,6 @@
117678
 #define KEX_GSS_GEX_SHA1_ID				"gss-gex-sha1-"
117678
 
117678
 typedef struct {
117678
-	char *filename;
117678
 	char *envvar;
117678
 	char *envval;
117678
 	struct passwd *owner;
117678
@@ -83,7 +82,7 @@ typedef struct ssh_gssapi_mech_struct {
117678
 	int (*dochild) (ssh_gssapi_client *);
117678
 	int (*userok) (ssh_gssapi_client *, char *);
117678
 	int (*localname) (ssh_gssapi_client *, char **);
117678
-	void (*storecreds) (ssh_gssapi_client *);
117678
+	int (*storecreds) (ssh_gssapi_client *);
117678
 	int (*updatecreds) (ssh_gssapi_ccache *, ssh_gssapi_client *);
117678
 } ssh_gssapi_mech;
117678
 
117678
@@ -127,7 +126,7 @@ int ssh_gssapi_userok(char *name);
117678
 OM_uint32 ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
117678
 void ssh_gssapi_do_child(char ***, u_int *);
117678
 void ssh_gssapi_cleanup_creds(void);
117678
-void ssh_gssapi_storecreds(void);
117678
+int ssh_gssapi_storecreds(void);
117678
 const char *ssh_gssapi_displayname(void);
117678
 
117678
 char *ssh_gssapi_server_mechanisms(void);
117678
diff --git a/sshd.c b/sshd.c
117678
index edbe815c..89514e8a 100644
117678
--- a/sshd.c
117678
+++ b/sshd.c
117678
@@ -2162,7 +2162,7 @@ main(int ac, char **av)
117678
 #ifdef GSSAPI
117678
 	if (options.gss_authentication) {
117678
 		temporarily_use_uid(authctxt->pw);
117678
-		ssh_gssapi_storecreds();
117678
+		authctxt->krb5_set_env = ssh_gssapi_storecreds();
117678
 		restore_uid();
117678
 	}
117678
 #endif
117678
diff --git a/sshd_config.5 b/sshd_config.5
117678
index c0683d4a..2349f477 100644
117678
--- a/sshd_config.5
117678
+++ b/sshd_config.5
117678
@@ -860,6 +860,12 @@ Specifies whether to automatically destroy the user's ticket cache
117678
 file on logout.
117678
 The default is
117678
 .Cm yes .
117678
+.It Cm KerberosUniqueTicket
117678
+Specifies whether to store the aquired tickets in the per-session credential
117678
+cache or whether to use per-user credential cache, which might overwrite
117678
+tickets aquired in different sessions of the same user.
117678
+The default is
117678
+.Cm no .
117678
 .It Cm KexAlgorithms
117678
 Specifies the available KEX (Key Exchange) algorithms.
117678
 Multiple algorithms must be comma-separated.