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