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