9070b3
diff -up openssh-8.6p1/auth.h.ccache_name openssh-8.6p1/auth.h
9070b3
--- openssh-8.6p1/auth.h.ccache_name	2021-05-06 11:15:36.345143341 +0200
9070b3
+++ openssh-8.6p1/auth.h	2021-05-06 11:15:36.387143654 +0200
9070b3
@@ -83,6 +83,7 @@ struct Authctxt {
9070b3
 	krb5_principal	 krb5_user;
9070b3
 	char		*krb5_ticket_file;
9070b3
 	char		*krb5_ccname;
9070b3
+	int		 krb5_set_env;
9070b3
 #endif
9070b3
 	struct sshbuf	*loginmsg;
9070b3
 
9070b3
@@ -231,7 +232,7 @@ struct passwd *fakepw(void);
9070b3
 int	 sys_auth_passwd(struct ssh *, const char *);
9070b3
 
9070b3
 #if defined(KRB5) && !defined(HEIMDAL)
9070b3
-krb5_error_code ssh_krb5_cc_gen(krb5_context, krb5_ccache *);
9070b3
+krb5_error_code ssh_krb5_cc_new_unique(krb5_context, krb5_ccache *, int *);
9070b3
 #endif
9070b3
 
9070b3
 #endif /* AUTH_H */
9070b3
diff -up openssh-8.6p1/auth-krb5.c.ccache_name openssh-8.6p1/auth-krb5.c
9070b3
--- openssh-8.6p1/auth-krb5.c.ccache_name	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/auth-krb5.c	2021-05-06 11:28:40.195242317 +0200
9070b3
@@ -51,6 +51,7 @@
9070b3
 #include <unistd.h>
9070b3
 #include <string.h>
9070b3
 #include <krb5.h>
9070b3
+#include <profile.h>
9070b3
 
9070b3
 extern ServerOptions	 options;
9070b3
 
9070b3
@@ -77,7 +78,7 @@ auth_krb5_password(Authctxt *authctxt, c
9070b3
 #endif
9070b3
 	krb5_error_code problem;
9070b3
 	krb5_ccache ccache = NULL;
9070b3
-	int len;
9070b3
+	char *ticket_name = NULL;
9070b3
 	char *client, *platform_client;
9070b3
 	const char *errmsg;
9070b3
 
9070b3
@@ -163,8 +164,8 @@ auth_krb5_password(Authctxt *authctxt, c
9070b3
 		goto out;
9070b3
 	}
9070b3
 
9070b3
-	problem = ssh_krb5_cc_gen(authctxt->krb5_ctx,
9070b3
-	    &authctxt->krb5_fwd_ccache);
9070b3
+	problem = ssh_krb5_cc_new_unique(authctxt->krb5_ctx,
9070b3
+	     &authctxt->krb5_fwd_ccache, &authctxt->krb5_set_env);
9070b3
 	if (problem)
9070b3
 		goto out;
9070b3
 
9070b3
@@ -179,15 +180,14 @@ auth_krb5_password(Authctxt *authctxt, c
9070b3
 		goto out;
9070b3
 #endif
9070b3
 
9070b3
-	authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
9070b3
+	problem = krb5_cc_get_full_name(authctxt->krb5_ctx,
9070b3
+	    authctxt->krb5_fwd_ccache, &ticket_name);
9070b3
 
9070b3
-	len = strlen(authctxt->krb5_ticket_file) + 6;
9070b3
-	authctxt->krb5_ccname = xmalloc(len);
9070b3
-	snprintf(authctxt->krb5_ccname, len, "FILE:%s",
9070b3
-	    authctxt->krb5_ticket_file);
9070b3
+	authctxt->krb5_ccname = xstrdup(ticket_name);
9070b3
+	krb5_free_string(authctxt->krb5_ctx, ticket_name);
9070b3
 
9070b3
 #ifdef USE_PAM
9070b3
-	if (options.use_pam)
9070b3
+	if (options.use_pam && authctxt->krb5_set_env)
9070b3
 		do_pam_putenv("KRB5CCNAME", authctxt->krb5_ccname);
9070b3
 #endif
9070b3
 
9070b3
@@ -223,11 +223,54 @@ auth_krb5_password(Authctxt *authctxt, c
9070b3
 void
9070b3
 krb5_cleanup_proc(Authctxt *authctxt)
9070b3
 {
9070b3
+	struct stat krb5_ccname_stat;
9070b3
+	char krb5_ccname[128], *krb5_ccname_dir_start, *krb5_ccname_dir_end;
9070b3
+
9070b3
 	debug("krb5_cleanup_proc called");
9070b3
 	if (authctxt->krb5_fwd_ccache) {
9070b3
-		krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
9070b3
+		krb5_context ctx = authctxt->krb5_ctx;
9070b3
+		krb5_cccol_cursor cursor;
9070b3
+		krb5_ccache ccache;
9070b3
+		int ret;
9070b3
+
9070b3
+		krb5_cc_destroy(ctx, authctxt->krb5_fwd_ccache);
9070b3
 		authctxt->krb5_fwd_ccache = NULL;
9070b3
+
9070b3
+		ret = krb5_cccol_cursor_new(ctx, &cursor);
9070b3
+		if (ret)
9070b3
+			goto out;
9070b3
+
9070b3
+		ret = krb5_cccol_cursor_next(ctx, cursor, &ccache);
9070b3
+		if (ret == 0 && ccache != NULL) {
9070b3
+			/* There is at least one other ccache in collection
9070b3
+			 * we can switch to */
9070b3
+			krb5_cc_switch(ctx, ccache);
9070b3
+		} else if (authctxt->krb5_ccname != NULL) {
9070b3
+			/* Clean up the collection too */
9070b3
+			strncpy(krb5_ccname, authctxt->krb5_ccname, sizeof(krb5_ccname) - 10);
9070b3
+			krb5_ccname_dir_start = strchr(krb5_ccname, ':') + 1;
9070b3
+			*krb5_ccname_dir_start++ = '\0';
9070b3
+			if (strcmp(krb5_ccname, "DIR") == 0) {
9070b3
+
9070b3
+				strcat(krb5_ccname_dir_start, "/primary");
9070b3
+
9070b3
+				if (stat(krb5_ccname_dir_start, &krb5_ccname_stat) == 0) {
9070b3
+					if (unlink(krb5_ccname_dir_start) == 0) {
9070b3
+						krb5_ccname_dir_end = strrchr(krb5_ccname_dir_start, '/');
9070b3
+						*krb5_ccname_dir_end = '\0';
9070b3
+						if (rmdir(krb5_ccname_dir_start) == -1)
9070b3
+							debug("cache dir '%s' remove failed: %s",
9070b3
+							    krb5_ccname_dir_start, strerror(errno));
9070b3
+					}
9070b3
+					else
9070b3
+						debug("cache primary file '%s', remove failed: %s",
9070b3
+						    krb5_ccname_dir_start, strerror(errno));
9070b3
+				}
9070b3
+			}
9070b3
+		}
9070b3
+		krb5_cccol_cursor_free(ctx, &cursor);
9070b3
 	}
9070b3
+out:
9070b3
 	if (authctxt->krb5_user) {
9070b3
 		krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
9070b3
 		authctxt->krb5_user = NULL;
9070b3
@@ -238,36 +281,188 @@ krb5_cleanup_proc(Authctxt *authctxt)
9070b3
 	}
9070b3
 }
9070b3
 
9070b3
-#ifndef HEIMDAL
9070b3
+
9070b3
+#if !defined(HEIMDAL)
9070b3
+int
9070b3
+ssh_asprintf_append(char **dsc, const char *fmt, ...) {
9070b3
+	char *src, *old;
9070b3
+	va_list ap;
9070b3
+	int i;
9070b3
+
9070b3
+	va_start(ap, fmt);
9070b3
+	i = vasprintf(&src, fmt, ap);
9070b3
+	va_end(ap);
9070b3
+
9070b3
+	if (i == -1 || src == NULL)
9070b3
+		return -1;
9070b3
+
9070b3
+	old = *dsc;
9070b3
+
9070b3
+	i = asprintf(dsc, "%s%s", *dsc, src);
9070b3
+	if (i == -1 || src == NULL) {
9070b3
+		free(src);
9070b3
+		return -1;
9070b3
+	}
9070b3
+
9070b3
+	free(old);
9070b3
+	free(src);
9070b3
+
9070b3
+	return i;
9070b3
+}
9070b3
+
9070b3
+int
9070b3
+ssh_krb5_expand_template(char **result, const char *template) {
9070b3
+	char *p_n, *p_o, *r, *tmp_template;
9070b3
+
9070b3
+	debug3_f("called, template = %s", template);
9070b3
+	if (template == NULL)
9070b3
+		return -1;
9070b3
+
9070b3
+	tmp_template = p_n = p_o = xstrdup(template);
9070b3
+	r = xstrdup("");
9070b3
+
9070b3
+	while ((p_n = strstr(p_o, "%{")) != NULL) {
9070b3
+
9070b3
+		*p_n++ = '\0';
9070b3
+		if (ssh_asprintf_append(&r, "%s", p_o) == -1)
9070b3
+			goto cleanup;
9070b3
+
9070b3
+		if (strncmp(p_n, "{uid}", 5) == 0 || strncmp(p_n, "{euid}", 6) == 0 ||
9070b3
+			strncmp(p_n, "{USERID}", 8) == 0) {
9070b3
+			p_o = strchr(p_n, '}') + 1;
9070b3
+			if (ssh_asprintf_append(&r, "%d", geteuid()) == -1)
9070b3
+				goto cleanup;
9070b3
+			continue;
9070b3
+		}
9070b3
+		else if (strncmp(p_n, "{TEMP}", 6) == 0) {
9070b3
+			p_o = strchr(p_n, '}') + 1;
9070b3
+			if (ssh_asprintf_append(&r, "/tmp") == -1)
9070b3
+				goto cleanup;
9070b3
+			continue;
9070b3
+		} else {
9070b3
+			p_o = strchr(p_n, '}') + 1;
9070b3
+			*p_o = '\0';
9070b3
+			debug_f("unsupported token %s in %s", p_n, template);
9070b3
+			/* unknown token, fallback to the default */
9070b3
+			goto cleanup;
9070b3
+		}
9070b3
+	}
9070b3
+
9070b3
+	if (ssh_asprintf_append(&r, "%s", p_o) == -1)
9070b3
+		goto cleanup;
9070b3
+
9070b3
+	*result = r;
9070b3
+	free(tmp_template);
9070b3
+	return 0;
9070b3
+
9070b3
+cleanup:
9070b3
+	free(r);
9070b3
+	free(tmp_template);
9070b3
+	return -1;
9070b3
+}
9070b3
+
9070b3
+krb5_error_code
9070b3
+ssh_krb5_get_cctemplate(krb5_context ctx, char **ccname) {
9070b3
+	profile_t p;
9070b3
+	int ret = 0;
9070b3
+	char *value = NULL;
9070b3
+
9070b3
+	debug3_f("called");
9070b3
+	ret = krb5_get_profile(ctx, &p);
9070b3
+	if (ret)
9070b3
+		return ret;
9070b3
+
9070b3
+	ret = profile_get_string(p, "libdefaults", "default_ccache_name", NULL, NULL, &value);
9070b3
+	if (ret || !value)
9070b3
+		return ret;
9070b3
+
9070b3
+	ret = ssh_krb5_expand_template(ccname, value);
9070b3
+
9070b3
+	debug3_f("returning with ccname = %s", *ccname);
9070b3
+	return ret;
9070b3
+}
9070b3
+
9070b3
 krb5_error_code
9070b3
-ssh_krb5_cc_gen(krb5_context ctx, krb5_ccache *ccache) {
9070b3
-	int tmpfd, ret, oerrno;
9070b3
-	char ccname[40];
9070b3
+ssh_krb5_cc_new_unique(krb5_context ctx, krb5_ccache *ccache, int *need_environment) {
9070b3
+	int tmpfd, ret, oerrno, type_len;
9070b3
+	char *ccname = NULL;
9070b3
 	mode_t old_umask;
9070b3
+	char *type = NULL, *colon = NULL;
9070b3
 
9070b3
-	ret = snprintf(ccname, sizeof(ccname),
9070b3
-	    "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
9070b3
-	if (ret < 0 || (size_t)ret >= sizeof(ccname))
9070b3
-		return ENOMEM;
9070b3
-
9070b3
-	old_umask = umask(0177);
9070b3
-	tmpfd = mkstemp(ccname + strlen("FILE:"));
9070b3
-	oerrno = errno;
9070b3
-	umask(old_umask);
9070b3
-	if (tmpfd == -1) {
9070b3
-		logit("mkstemp(): %.100s", strerror(oerrno));
9070b3
-		return oerrno;
9070b3
-	}
9070b3
+	debug3_f("called");
9070b3
+	if (need_environment)
9070b3
+		*need_environment = 0;
9070b3
+	ret = ssh_krb5_get_cctemplate(ctx, &ccname);
9070b3
+	if (ret || !ccname || options.kerberos_unique_ccache) {
9070b3
+		/* Otherwise, go with the old method */
9070b3
+		if (ccname)
9070b3
+			free(ccname);
9070b3
+		ccname = NULL;
9070b3
+
9070b3
+		ret = asprintf(&ccname,
9070b3
+		    "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
9070b3
+		if (ret < 0)
9070b3
+			return ENOMEM;
9070b3
 
9070b3
-	if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
9070b3
+		old_umask = umask(0177);
9070b3
+		tmpfd = mkstemp(ccname + strlen("FILE:"));
9070b3
 		oerrno = errno;
9070b3
-		logit("fchmod(): %.100s", strerror(oerrno));
9070b3
+		umask(old_umask);
9070b3
+		if (tmpfd == -1) {
9070b3
+			logit("mkstemp(): %.100s", strerror(oerrno));
9070b3
+			return oerrno;
9070b3
+		}
9070b3
+
9070b3
+		if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
9070b3
+			oerrno = errno;
9070b3
+			logit("fchmod(): %.100s", strerror(oerrno));
9070b3
+			close(tmpfd);
9070b3
+			return oerrno;
9070b3
+		}
9070b3
+		/* make sure the KRB5CCNAME is set for non-standard location */
9070b3
+		if (need_environment)
9070b3
+			*need_environment = 1;
9070b3
 		close(tmpfd);
9070b3
-		return oerrno;
9070b3
 	}
9070b3
-	close(tmpfd);
9070b3
 
9070b3
-	return (krb5_cc_resolve(ctx, ccname, ccache));
9070b3
+	debug3_f("setting default ccname to %s", ccname);
9070b3
+	/* set the default with already expanded user IDs */
9070b3
+	ret = krb5_cc_set_default_name(ctx, ccname);
9070b3
+	if (ret)
9070b3
+		return ret;
9070b3
+
9070b3
+	if ((colon = strstr(ccname, ":")) != NULL) {
9070b3
+		type_len = colon - ccname;
9070b3
+		type = malloc((type_len + 1) * sizeof(char));
9070b3
+		if (type == NULL)
9070b3
+			return ENOMEM;
9070b3
+		strncpy(type, ccname, type_len);
9070b3
+		type[type_len] = 0;
9070b3
+	} else {
9070b3
+		type = strdup(ccname);
9070b3
+	}
9070b3
+
9070b3
+	/* If we have a credential cache from krb5.conf, we need to switch
9070b3
+	 * a primary cache for this collection, if it supports that (non-FILE)
9070b3
+	 */
9070b3
+	if (krb5_cc_support_switch(ctx, type)) {
9070b3
+		debug3_f("calling cc_new_unique(%s)", ccname);
9070b3
+		ret = krb5_cc_new_unique(ctx, type, NULL, ccache);
9070b3
+		free(type);
9070b3
+		if (ret)
9070b3
+			return ret;
9070b3
+
9070b3
+		debug3_f("calling cc_switch()");
9070b3
+		return krb5_cc_switch(ctx, *ccache);
9070b3
+	} else {
9070b3
+		/* Otherwise, we can not create a unique ccname here (either
9070b3
+		 * it is already unique from above or the type does not support
9070b3
+		 * collections
9070b3
+		 */
9070b3
+		free(type);
9070b3
+		debug3_f("calling cc_resolve(%s)", ccname);
9070b3
+		return (krb5_cc_resolve(ctx, ccname, ccache));
9070b3
+	}
9070b3
 }
9070b3
 #endif /* !HEIMDAL */
9070b3
 #endif /* KRB5 */
9070b3
diff -up openssh-8.6p1/gss-serv.c.ccache_name openssh-8.6p1/gss-serv.c
9070b3
--- openssh-8.6p1/gss-serv.c.ccache_name	2021-05-06 11:15:36.374143558 +0200
9070b3
+++ openssh-8.6p1/gss-serv.c	2021-05-06 11:15:36.387143654 +0200
9070b3
@@ -413,13 +413,15 @@ ssh_gssapi_cleanup_creds(void)
9070b3
 }
9070b3
 
9070b3
 /* As user */
9070b3
-void
9070b3
+int
9070b3
 ssh_gssapi_storecreds(void)
9070b3
 {
9070b3
 	if (gssapi_client.mech && gssapi_client.mech->storecreds) {
9070b3
-		(*gssapi_client.mech->storecreds)(&gssapi_client);
9070b3
+		return (*gssapi_client.mech->storecreds)(&gssapi_client);
9070b3
 	} else
9070b3
 		debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
9070b3
+
9070b3
+	return 0;
9070b3
 }
9070b3
 
9070b3
 /* This allows GSSAPI methods to do things to the child's environment based
9070b3
@@ -499,9 +501,7 @@ ssh_gssapi_rekey_creds(void) {
9070b3
 	char *envstr;
9070b3
 #endif
9070b3
 
9070b3
-	if (gssapi_client.store.filename == NULL &&
9070b3
-	    gssapi_client.store.envval == NULL &&
9070b3
-	    gssapi_client.store.envvar == NULL)
9070b3
+	if (gssapi_client.store.envval == NULL)
9070b3
 		return;
9070b3
 
9070b3
 	ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
9070b3
diff -up openssh-8.6p1/gss-serv-krb5.c.ccache_name openssh-8.6p1/gss-serv-krb5.c
9070b3
--- openssh-8.6p1/gss-serv-krb5.c.ccache_name	2021-05-06 11:15:36.384143632 +0200
9070b3
+++ openssh-8.6p1/gss-serv-krb5.c	2021-05-06 11:15:36.387143654 +0200
9070b3
@@ -267,7 +267,7 @@ ssh_gssapi_krb5_cmdok(krb5_principal pri
9070b3
 /* This writes out any forwarded credentials from the structure populated
9070b3
  * during userauth. Called after we have setuid to the user */
9070b3
 
9070b3
-static void
9070b3
+static int
9070b3
 ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
9070b3
 {
9070b3
 	krb5_ccache ccache;
9070b3
@@ -276,14 +276,15 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
9070b3
 	OM_uint32 maj_status, min_status;
9070b3
 	const char *new_ccname, *new_cctype;
9070b3
 	const char *errmsg;
9070b3
+	int set_env = 0;
9070b3
 
9070b3
 	if (client->creds == NULL) {
9070b3
 		debug("No credentials stored");
9070b3
-		return;
9070b3
+		return 0;
9070b3
 	}
9070b3
 
9070b3
 	if (ssh_gssapi_krb5_init() == 0)
9070b3
-		return;
9070b3
+		return 0;
9070b3
 
9070b3
 #ifdef HEIMDAL
9070b3
 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
9070b3
@@ -297,14 +298,14 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
9070b3
 		krb5_get_err_text(krb_context, problem));
9070b3
 # endif
9070b3
 		krb5_free_error_message(krb_context, errmsg);
9070b3
-		return;
9070b3
+		return 0;
9070b3
 	}
9070b3
 #else
9070b3
-	if ((problem = ssh_krb5_cc_gen(krb_context, &ccache))) {
9070b3
+	if ((problem = ssh_krb5_cc_new_unique(krb_context, &ccache, &set_env)) != 0) {
9070b3
 		errmsg = krb5_get_error_message(krb_context, problem);
9070b3
-		logit("ssh_krb5_cc_gen(): %.100s", errmsg);
9070b3
+		logit("ssh_krb5_cc_new_unique(): %.100s", errmsg);
9070b3
 		krb5_free_error_message(krb_context, errmsg);
9070b3
-		return;
9070b3
+		return 0;
9070b3
 	}
9070b3
 #endif	/* #ifdef HEIMDAL */
9070b3
 
9070b3
@@ -313,7 +314,7 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
9070b3
 		errmsg = krb5_get_error_message(krb_context, problem);
9070b3
 		logit("krb5_parse_name(): %.100s", errmsg);
9070b3
 		krb5_free_error_message(krb_context, errmsg);
9070b3
-		return;
9070b3
+		return 0;
9070b3
 	}
9070b3
 
9070b3
 	if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
9070b3
@@ -322,7 +323,7 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
9070b3
 		krb5_free_error_message(krb_context, errmsg);
9070b3
 		krb5_free_principal(krb_context, princ);
9070b3
 		krb5_cc_destroy(krb_context, ccache);
9070b3
-		return;
9070b3
+		return 0;
9070b3
 	}
9070b3
 
9070b3
 	krb5_free_principal(krb_context, princ);
9070b3
@@ -331,32 +332,21 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
9070b3
 	    client->creds, ccache))) {
9070b3
 		logit("gss_krb5_copy_ccache() failed");
9070b3
 		krb5_cc_destroy(krb_context, ccache);
9070b3
-		return;
9070b3
+		return 0;
9070b3
 	}
9070b3
 
9070b3
 	new_cctype = krb5_cc_get_type(krb_context, ccache);
9070b3
 	new_ccname = krb5_cc_get_name(krb_context, ccache);
9070b3
-
9070b3
-	client->store.envvar = "KRB5CCNAME";
9070b3
-#ifdef USE_CCAPI
9070b3
-	xasprintf(&client->store.envval, "API:%s", new_ccname);
9070b3
-	client->store.filename = NULL;
9070b3
-#else
9070b3
-	if (new_ccname[0] == ':')
9070b3
-		new_ccname++;
9070b3
 	xasprintf(&client->store.envval, "%s:%s", new_cctype, new_ccname);
9070b3
-	if (strcmp(new_cctype, "DIR") == 0) {
9070b3
-		char *p;
9070b3
-		p = strrchr(client->store.envval, '/');
9070b3
-		if (p)
9070b3
-			*p = '\0';
9070b3
+
9070b3
+	if (set_env) {
9070b3
+		client->store.envvar = "KRB5CCNAME";
9070b3
 	}
9070b3
 	if ((strcmp(new_cctype, "FILE") == 0) || (strcmp(new_cctype, "DIR") == 0))
9070b3
 		client->store.filename = xstrdup(new_ccname);
9070b3
-#endif
9070b3
 
9070b3
 #ifdef USE_PAM
9070b3
-	if (options.use_pam)
9070b3
+	if (options.use_pam && set_env)
9070b3
 		do_pam_putenv(client->store.envvar, client->store.envval);
9070b3
 #endif
9070b3
 
9070b3
@@ -364,7 +354,7 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
9070b3
 
9070b3
 	client->store.data = krb_context;
9070b3
 
9070b3
-	return;
9070b3
+	return set_env;
9070b3
 }
9070b3
 
9070b3
 int
9070b3
diff -up openssh-8.6p1/servconf.c.ccache_name openssh-8.6p1/servconf.c
9070b3
--- openssh-8.6p1/servconf.c.ccache_name	2021-05-06 11:15:36.377143580 +0200
9070b3
+++ openssh-8.6p1/servconf.c	2021-05-06 11:15:36.388143662 +0200
9070b3
@@ -136,6 +136,7 @@ initialize_server_options(ServerOptions
9070b3
 	options->kerberos_or_local_passwd = -1;
9070b3
 	options->kerberos_ticket_cleanup = -1;
9070b3
 	options->kerberos_get_afs_token = -1;
9070b3
+	options->kerberos_unique_ccache = -1;
9070b3
 	options->gss_authentication=-1;
9070b3
 	options->gss_keyex = -1;
9070b3
 	options->gss_cleanup_creds = -1;
9070b3
@@ -359,6 +360,8 @@ fill_default_server_options(ServerOption
9070b3
 		options->kerberos_ticket_cleanup = 1;
9070b3
 	if (options->kerberos_get_afs_token == -1)
9070b3
 		options->kerberos_get_afs_token = 0;
9070b3
+	if (options->kerberos_unique_ccache == -1)
9070b3
+		options->kerberos_unique_ccache = 0;
9070b3
 	if (options->gss_authentication == -1)
9070b3
 		options->gss_authentication = 0;
9070b3
 	if (options->gss_keyex == -1)
9070b3
@@ -506,7 +509,8 @@ typedef enum {
9070b3
	sPort, sHostKeyFile, sLoginGraceTime,
9070b3
	sPermitRootLogin, sLogFacility, sLogLevel, sLogVerbose,
9070b3
	sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
9070b3
-	sKerberosGetAFSToken, sChallengeResponseAuthentication,
9070b3
+	sKerberosGetAFSToken, sKerberosUniqueCCache,
9070b3
+	sChallengeResponseAuthentication,
9070b3
	sPasswordAuthentication, sKbdInteractiveAuthentication,
9070b3
	sListenAddress, sAddressFamily,
9070b3
	sPrintMotd, sPrintLastLog, sIgnoreRhosts,
9070b3
@@ -593,11 +597,13 @@ static struct {
9070b3
 #else
9070b3
 	{ "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
9070b3
 #endif
9070b3
+	{ "kerberosuniqueccache", sKerberosUniqueCCache, SSHCFG_GLOBAL },
9070b3
 #else
9070b3
 	{ "kerberosauthentication", sUnsupported, SSHCFG_ALL },
9070b3
 	{ "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
9070b3
 	{ "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
9070b3
 	{ "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
9070b3
+	{ "kerberosuniqueccache", sUnsupported, SSHCFG_GLOBAL },
9070b3
 #endif
9070b3
 	{ "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
9070b3
 	{ "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
9070b3
@@ -1573,6 +1579,10 @@ process_server_config_line_depth(ServerO
9070b3
 		intptr = &options->kerberos_get_afs_token;
9070b3
 		goto parse_flag;
9070b3
 
9070b3
+	case sKerberosUniqueCCache:
9070b3
+		intptr = &options->kerberos_unique_ccache;
9070b3
+		goto parse_flag;
9070b3
+
9070b3
 	case sGssAuthentication:
9070b3
 		intptr = &options->gss_authentication;
9070b3
 		goto parse_flag;
9070b3
@@ -2891,6 +2901,7 @@ dump_config(ServerOptions *o)
9070b3
 # ifdef USE_AFS
9070b3
 	dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
9070b3
 # endif
9070b3
+	dump_cfg_fmtint(sKerberosUniqueCCache, o->kerberos_unique_ccache);
9070b3
 #endif
9070b3
 #ifdef GSSAPI
9070b3
 	dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
9070b3
diff -up openssh-8.6p1/servconf.h.ccache_name openssh-8.6p1/servconf.h
9070b3
--- openssh-8.6p1/servconf.h.ccache_name	2021-05-06 11:15:36.377143580 +0200
9070b3
+++ openssh-8.6p1/servconf.h	2021-05-06 11:15:36.397143729 +0200
9070b3
@@ -140,6 +140,8 @@ typedef struct {
9070b3
 						 * file on logout. */
9070b3
 	int     kerberos_get_afs_token;		/* If true, try to get AFS token if
9070b3
 						 * authenticated with Kerberos. */
9070b3
+	int     kerberos_unique_ccache;		/* If true, the acquired ticket will
9070b3
+						 * be stored in per-session ccache */
9070b3
 	int     gss_authentication;	/* If true, permit GSSAPI authentication */
9070b3
 	int     gss_keyex;		/* If true, permit GSSAPI key exchange */
9070b3
 	int     gss_cleanup_creds;	/* If true, destroy cred cache on logout */
9070b3
diff -up openssh-8.6p1/session.c.ccache_name openssh-8.6p1/session.c
9070b3
--- openssh-8.6p1/session.c.ccache_name	2021-05-06 11:15:36.384143632 +0200
9070b3
+++ openssh-8.6p1/session.c	2021-05-06 11:15:36.397143729 +0200
9070b3
@@ -1038,7 +1038,8 @@ do_setup_env(struct ssh *ssh, Session *s
9070b3
 	/* Allow any GSSAPI methods that we've used to alter
9070b3
 	 * the child's environment as they see fit
9070b3
 	 */
9070b3
-	ssh_gssapi_do_child(&env, &envsize);
9070b3
+	if (s->authctxt->krb5_set_env)
9070b3
+		ssh_gssapi_do_child(&env, &envsize);
9070b3
 #endif
9070b3
 
9070b3
 	/* Set basic environment. */
9070b3
@@ -1114,7 +1115,7 @@ do_setup_env(struct ssh *ssh, Session *s
9070b3
 	}
9070b3
 #endif
9070b3
 #ifdef KRB5
9070b3
-	if (s->authctxt->krb5_ccname)
9070b3
+	if (s->authctxt->krb5_ccname && s->authctxt->krb5_set_env)
9070b3
 		child_set_env(&env, &envsize, "KRB5CCNAME",
9070b3
 		    s->authctxt->krb5_ccname);
9070b3
 #endif
9070b3
diff -up openssh-8.6p1/sshd.c.ccache_name openssh-8.6p1/sshd.c
9070b3
--- openssh-8.6p1/sshd.c.ccache_name	2021-05-06 11:15:36.380143602 +0200
9070b3
+++ openssh-8.6p1/sshd.c	2021-05-06 11:15:36.398143736 +0200
9070b3
@@ -2284,7 +2284,7 @@ main(int ac, char **av)
9070b3
 #ifdef GSSAPI
9070b3
 	if (options.gss_authentication) {
9070b3
 		temporarily_use_uid(authctxt->pw);
9070b3
-		ssh_gssapi_storecreds();
9070b3
+		authctxt->krb5_set_env = ssh_gssapi_storecreds();
9070b3
 		restore_uid();
9070b3
 	}
9070b3
 #endif
9070b3
diff -up openssh-8.6p1/sshd_config.5.ccache_name openssh-8.6p1/sshd_config.5
9070b3
--- openssh-8.6p1/sshd_config.5.ccache_name	2021-05-06 11:15:36.380143602 +0200
9070b3
+++ openssh-8.6p1/sshd_config.5	2021-05-06 11:15:36.398143736 +0200
9070b3
@@ -939,6 +939,14 @@ Specifies whether to automatically destr
9070b3
 file on logout.
9070b3
 The default is
9070b3
 .Cm yes .
9070b3
+.It Cm KerberosUniqueCCache
9070b3
+Specifies whether to store the acquired tickets in the per-session credential
9070b3
+cache under /tmp/ or whether to use per-user credential cache as configured in
9070b3
+.Pa /etc/krb5.conf .
9070b3
+The default value
9070b3
+.Cm no
9070b3
+can lead to overwriting previous tickets by subseqent connections to the same
9070b3
+user account.
9070b3
 .It Cm KexAlgorithms
9070b3
 Specifies the available KEX (Key Exchange) algorithms.
9070b3
 Multiple algorithms must be comma-separated.
9070b3
diff -up openssh-8.6p1/ssh-gss.h.ccache_name openssh-8.6p1/ssh-gss.h
9070b3
--- openssh-8.6p1/ssh-gss.h.ccache_name	2021-05-06 11:15:36.384143632 +0200
9070b3
+++ openssh-8.6p1/ssh-gss.h	2021-05-06 11:15:36.398143736 +0200
9070b3
@@ -114,7 +114,7 @@ typedef struct ssh_gssapi_mech_struct {
9070b3
 	int (*dochild) (ssh_gssapi_client *);
9070b3
 	int (*userok) (ssh_gssapi_client *, char *);
9070b3
 	int (*localname) (ssh_gssapi_client *, char **);
9070b3
-	void (*storecreds) (ssh_gssapi_client *);
9070b3
+	int (*storecreds) (ssh_gssapi_client *);
9070b3
 	int (*updatecreds) (ssh_gssapi_ccache *, ssh_gssapi_client *);
9070b3
 } ssh_gssapi_mech;
9070b3
 
9070b3
@@ -175,7 +175,7 @@ int ssh_gssapi_userok(char *name, struct
9070b3
 OM_uint32 ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
9070b3
 void ssh_gssapi_do_child(char ***, u_int *);
9070b3
 void ssh_gssapi_cleanup_creds(void);
9070b3
-void ssh_gssapi_storecreds(void);
9070b3
+int ssh_gssapi_storecreds(void);
9070b3
 const char *ssh_gssapi_displayname(void);
9070b3
 
9070b3
 char *ssh_gssapi_server_mechanisms(void);