kentpeacock / rpms / openssh

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