vishalmishra434 / rpms / openssh

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