rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
b58e57
diff -up openssh-6.3p1/auth-krb5.c.ccache_name openssh-6.3p1/auth-krb5.c
b58e57
--- openssh-6.3p1/auth-krb5.c.ccache_name	2013-10-23 22:03:52.322950759 +0200
b58e57
+++ openssh-6.3p1/auth-krb5.c	2013-10-23 22:04:24.295799873 +0200
b58e57
@@ -50,7 +50,9 @@
b58e57
 #include <errno.h>
b58e57
 #include <unistd.h>
b58e57
 #include <string.h>
b58e57
+#include <sys/stat.h>
b58e57
 #include <krb5.h>
b58e57
+#include <profile.h>
b58e57
 
b58e57
 extern ServerOptions	 options;
b58e57
 
b58e57
@@ -91,6 +93,7 @@ auth_krb5_password(Authctxt *authctxt, c
b58e57
 #endif
b58e57
 	krb5_error_code problem;
b58e57
 	krb5_ccache ccache = NULL;
b58e57
+	const char *ccache_type;
b58e57
 	int len;
b58e57
 	char *client, *platform_client;
b58e57
 	const char *errmsg;
b58e57
@@ -191,12 +194,30 @@ auth_krb5_password(Authctxt *authctxt, c
b58e57
 		goto out;
b58e57
 #endif
b58e57
 
b58e57
+	ccache_type = krb5_cc_get_type(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
b58e57
 	authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
b58e57
 
b58e57
-	len = strlen(authctxt->krb5_ticket_file) + 6;
b58e57
+	if (authctxt->krb5_ticket_file[0] == ':')
b58e57
+		authctxt->krb5_ticket_file++;
b58e57
+
b58e57
+	len = strlen(authctxt->krb5_ticket_file) + strlen(ccache_type) + 2;
b58e57
 	authctxt->krb5_ccname = xmalloc(len);
b58e57
-	snprintf(authctxt->krb5_ccname, len, "FILE:%s",
b58e57
+
b58e57
+#ifdef USE_CCAPI
b58e57
+	snprintf(authctxt->krb5_ccname, len, "API:%s",
b58e57
 	    authctxt->krb5_ticket_file);
b58e57
+#else
b58e57
+	snprintf(authctxt->krb5_ccname, len, "%s:%s",
b58e57
+	    ccache_type, authctxt->krb5_ticket_file);
b58e57
+#endif
b58e57
+
b58e57
+	if (strcmp(ccache_type, "DIR") == 0) {
b58e57
+		char *p;
b58e57
+		p = strrchr(authctxt->krb5_ccname, '/');
b58e57
+		if (p)
b58e57
+			*p = '\0';
b58e57
+	}
b58e57
+
b58e57
 
b58e57
 #ifdef USE_PAM
b58e57
 	if (options.use_pam)
b58e57
@@ -235,10 +256,34 @@ auth_krb5_password(Authctxt *authctxt, c
b58e57
 void
b58e57
 krb5_cleanup_proc(Authctxt *authctxt)
b58e57
 {
b58e57
+	struct stat krb5_ccname_stat;
b58e57
+	char krb5_ccname[128], *krb5_ccname_dir_start, *krb5_ccname_dir_end;
b58e57
+
b58e57
 	debug("krb5_cleanup_proc called");
b58e57
 	if (authctxt->krb5_fwd_ccache) {
b58e57
 		krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
b58e57
 		authctxt->krb5_fwd_ccache = NULL;
b58e57
+
b58e57
+		strncpy(krb5_ccname, authctxt->krb5_ccname, sizeof(krb5_ccname) - 10);
b58e57
+		krb5_ccname_dir_start = strchr(krb5_ccname, ':') + 1;
b58e57
+		*krb5_ccname_dir_start++ = '\0';
b58e57
+		if (strcmp(krb5_ccname, "DIR") == 0) {
b58e57
+
b58e57
+			strcat(krb5_ccname_dir_start, "/primary");
b58e57
+
b58e57
+			if (stat(krb5_ccname_dir_start, &krb5_ccname_stat) == 0) {
b58e57
+				if (unlink(krb5_ccname_dir_start) == 0) {
b58e57
+					krb5_ccname_dir_end = strrchr(krb5_ccname_dir_start, '/');
b58e57
+					*krb5_ccname_dir_end = '\0';
b58e57
+					if (rmdir(krb5_ccname_dir_start) == -1)
b58e57
+						debug("cache dir '%s' remove failed: %s", krb5_ccname_dir_start, strerror(errno));
b58e57
+				}
b58e57
+				else
b58e57
+					debug("cache primary file '%s', remove failed: %s",
b58e57
+						krb5_ccname_dir_start, strerror(errno)
b58e57
+						);
b58e57
+			}
b58e57
+		}
b58e57
 	}
b58e57
 	if (authctxt->krb5_user) {
b58e57
 		krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
b58e57
@@ -250,34 +295,139 @@ krb5_cleanup_proc(Authctxt *authctxt)
b58e57
 	}
b58e57
 }
b58e57
 
b58e57
+int
b58e57
+ssh_asprintf_append(char **dsc, const char *fmt, ...) {
b58e57
+	char *src, *old;
b58e57
+	va_list ap;
b58e57
+	int i;
b58e57
+
b58e57
+	va_start(ap, fmt);
b58e57
+	i = vasprintf(&src, fmt, ap);
b58e57
+	va_end(ap);
b58e57
+
b58e57
+	if (i == -1 || src == NULL)
b58e57
+		return -1;
b58e57
+
b58e57
+	old = *dsc;
b58e57
+
b58e57
+	i = asprintf(dsc, "%s%s", *dsc, src);
b58e57
+	if (i == -1 || src == NULL) {
b58e57
+		free(src);
b58e57
+		return -1;
b58e57
+	}
b58e57
+
b58e57
+	free(old);
b58e57
+	free(src);
b58e57
+
b58e57
+	return i;
b58e57
+}
b58e57
+
b58e57
+int
b58e57
+ssh_krb5_expand_template(char **result, const char *template) {
b58e57
+	char *p_n, *p_o, *r, *tmp_template;
b58e57
+
b58e57
+	if (template == NULL)
b58e57
+		return -1;
b58e57
+
b58e57
+	tmp_template = p_n = p_o = xstrdup(template);
b58e57
+	r = xstrdup("");
b58e57
+
b58e57
+	while ((p_n = strstr(p_o, "%{")) != NULL) {
b58e57
+
b58e57
+		*p_n++ = '\0';
b58e57
+		if (ssh_asprintf_append(&r, "%s", p_o) == -1)
b58e57
+			goto cleanup;
b58e57
+
b58e57
+		if (strncmp(p_n, "{uid}", 5) == 0 || strncmp(p_n, "{euid}", 6) == 0 ||
b58e57
+			strncmp(p_n, "{USERID}", 8) == 0) {
b58e57
+			p_o = strchr(p_n, '}') + 1;
b58e57
+			if (ssh_asprintf_append(&r, "%d", geteuid()) == -1)
b58e57
+				goto cleanup;
b58e57
+			continue;
b58e57
+		}
b58e57
+		else if (strncmp(p_n, "{TEMP}", 6) == 0) {
b58e57
+			p_o = strchr(p_n, '}') + 1;
b58e57
+			if (ssh_asprintf_append(&r, "/tmp") == -1)
b58e57
+				goto cleanup;
b58e57
+			continue;
b58e57
+		} else {
b58e57
+			p_o = strchr(p_n, '}') + 1;
b58e57
+			p_o = '\0';
b58e57
+			debug("%s: unsupported token %s in %s", __func__, p_n, template);
b58e57
+			/* unknown token, fallback to the default */
b58e57
+			goto cleanup;
b58e57
+		}
b58e57
+	}
b58e57
+
b58e57
+	if (ssh_asprintf_append(&r, "%s", p_o) == -1)
b58e57
+		goto cleanup;
b58e57
+
b58e57
+	*result = r;
b58e57
+	free(tmp_template);
b58e57
+	return 0;
b58e57
+
b58e57
+cleanup:
b58e57
+	free(r);
b58e57
+	free(tmp_template);
b58e57
+	return -1;
b58e57
+}
b58e57
+
b58e57
+krb5_error_code
b58e57
+ssh_krb5_get_cctemplate(krb5_context ctx, char **ccname) {
b58e57
+	profile_t p;
b58e57
+	int ret = 0;
b58e57
+	char *value = NULL;
b58e57
+
b58e57
+	ret = krb5_get_profile(ctx, &p);
b58e57
+	if (ret)
b58e57
+		return ret;
b58e57
+
b58e57
+	ret = profile_get_string(p, "libdefaults", "default_ccache_name", NULL, NULL, &value);
b58e57
+	if (ret)
b58e57
+		return ret;
b58e57
+
b58e57
+	ret = ssh_krb5_expand_template(ccname, value);
b58e57
+
b58e57
+	return ret;
b58e57
+}
b58e57
+
b58e57
 #ifndef HEIMDAL
b58e57
 krb5_error_code
b58e57
 ssh_krb5_cc_gen(krb5_context ctx, krb5_ccache *ccache) {
b58e57
 	int tmpfd, ret, oerrno;
b58e57
-	char ccname[40];
b58e57
+	char *ccname;
b58e57
+#ifdef USE_CCAPI
b58e57
+	char cctemplate[] = "API:krb5cc_%d";
b58e57
+#else
b58e57
 	mode_t old_umask;
b58e57
+	char cctemplate[] = "FILE:/tmp/krb5cc_%d_XXXXXXXXXX";
b58e57
 
b58e57
-	ret = snprintf(ccname, sizeof(ccname),
b58e57
-	    "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
b58e57
-	if (ret < 0 || (size_t)ret >= sizeof(ccname))
b58e57
-		return ENOMEM;
b58e57
-
b58e57
-	old_umask = umask(0177);
b58e57
-	tmpfd = mkstemp(ccname + strlen("FILE:"));
b58e57
-	oerrno = errno;
b58e57
-	umask(old_umask);
b58e57
-	if (tmpfd == -1) {
b58e57
-		logit("mkstemp(): %.100s", strerror(oerrno));
b58e57
-		return oerrno;
b58e57
-	}
b58e57
+#endif
b58e57
+
b58e57
+	ret = ssh_krb5_get_cctemplate(ctx, &ccname);
b58e57
 
b58e57
-	if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
b58e57
+	if (ret) {
b58e57
+		ret = asprintf(&ccname, cctemplate, geteuid());
b58e57
+		if (ret == -1)
b58e57
+			return ENOMEM;
b58e57
+		old_umask = umask(0177);
b58e57
+		tmpfd = mkstemp(ccname + strlen("FILE:"));
b58e57
 		oerrno = errno;
b58e57
-		logit("fchmod(): %.100s", strerror(oerrno));
b58e57
+		umask(old_umask);
b58e57
+		if (tmpfd == -1) {
b58e57
+			logit("mkstemp(): %.100s", strerror(oerrno));
b58e57
+			return oerrno;
b58e57
+		}
b58e57
+
b58e57
+		if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
b58e57
+			oerrno = errno;
b58e57
+			logit("fchmod(): %.100s", strerror(oerrno));
b58e57
+			close(tmpfd);
b58e57
+			return oerrno;
b58e57
+		}
b58e57
 		close(tmpfd);
b58e57
-		return oerrno;
b58e57
 	}
b58e57
-	close(tmpfd);
b58e57
+	debug("%s: Setting ccname to %s", __func__, ccname);
b58e57
 
b58e57
 	return (krb5_cc_resolve(ctx, ccname, ccache));
b58e57
 }