kentpeacock / rpms / openssh

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