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