Blame SOURCES/nfs-utils-2.3.3-gssd-multithread-updates.patch

fc44f1
diff --git a/aclocal/libevent.m4 b/aclocal/libevent.m4
fc44f1
index b5ac00ff..0ebcb524 100644
fc44f1
--- a/aclocal/libevent.m4
fc44f1
+++ b/aclocal/libevent.m4
fc44f1
@@ -4,9 +4,13 @@ AC_DEFUN([AC_LIBEVENT], [
fc44f1
   dnl Check for libevent, but do not add -levent to LIBS
fc44f1
   AC_CHECK_LIB([event], [event_dispatch], [LIBEVENT=-levent],
fc44f1
                [AC_MSG_ERROR([libevent not found.])])
fc44f1
+  AC_CHECK_LIB([event_core], [event_base_dispatch], [LIBEVENT=-levent_core],
fc44f1
+  	[AC_MSG_ERROR([libevent2 not found.])])
fc44f1
   AC_SUBST(LIBEVENT)
fc44f1
 
fc44f1
   AC_CHECK_HEADERS([event.h], ,
fc44f1
                    [AC_MSG_ERROR([libevent headers not found.])])
fc44f1
+  AC_CHECK_HEADERS([event2/event.h], ,
fc44f1
+                   [AC_MSG_ERROR([libevent headers not found.])])
fc44f1
 
fc44f1
 ])dnl
fc44f1
diff --git a/utils/gssd/gss_names.c b/utils/gssd/gss_names.c
fc44f1
index 2a7f3a13..982b96f4 100644
fc44f1
--- a/utils/gssd/gss_names.c
fc44f1
+++ b/utils/gssd/gss_names.c
fc44f1
@@ -110,10 +110,12 @@ get_hostbased_client_name(gss_name_t client_name, gss_OID mech,
fc44f1
 	/* For Kerberos, transform the NT_KRB5_PRINCIPAL name to
fc44f1
 	 * an NT_HOSTBASED_SERVICE name */
fc44f1
 	if (g_OID_equal(&krb5oid, mech)) {
fc44f1
-		if (get_krb5_hostbased_name(&name, &cname) == 0)
fc44f1
-			*hostbased_name = cname;
fc44f1
+		if (get_krb5_hostbased_name(&name, &cname) != 0)
fc44f1
+			goto out_rel_buf;
fc44f1
+		*hostbased_name = cname;
fc44f1
 	} else {
fc44f1
 		printerr(1, "WARNING: unknown/unsupport mech OID\n");
fc44f1
+		goto out_rel_buf;
fc44f1
 	}
fc44f1
 
fc44f1
 	res = 0;
fc44f1
diff --git a/utils/gssd/gssd.c b/utils/gssd/gssd.c
fc44f1
index af66ed62..6f461fdf 100644
fc44f1
--- a/utils/gssd/gssd.c
fc44f1
+++ b/utils/gssd/gssd.c
fc44f1
@@ -64,7 +64,7 @@
fc44f1
 #include <fcntl.h>
fc44f1
 #include <dirent.h>
fc44f1
 #include <netdb.h>
fc44f1
-#include <event.h>
fc44f1
+#include <event2/event.h>
fc44f1
 
fc44f1
 #include "gssd.h"
fc44f1
 #include "err_util.h"
fc44f1
@@ -77,7 +77,7 @@ static char *pipefs_path = GSSD_PIPEFS_DIR;
fc44f1
 static DIR *pipefs_dir;
fc44f1
 static int pipefs_fd;
fc44f1
 static int inotify_fd;
fc44f1
-struct event inotify_ev;
fc44f1
+struct event *inotify_ev;
fc44f1
 
fc44f1
 char *keytabfile = GSSD_DEFAULT_KEYTAB_FILE;
fc44f1
 char **ccachesearch;
fc44f1
@@ -90,9 +90,9 @@ char *ccachedir = NULL;
fc44f1
 /* Avoid DNS reverse lookups on server names */
fc44f1
 static bool avoid_dns = true;
fc44f1
 static bool use_gssproxy = false;
fc44f1
-int thread_started = false;
fc44f1
-pthread_mutex_t pmutex = PTHREAD_MUTEX_INITIALIZER;
fc44f1
-pthread_cond_t pcond = PTHREAD_COND_INITIALIZER;
fc44f1
+pthread_mutex_t clp_lock = PTHREAD_MUTEX_INITIALIZER;
fc44f1
+static bool signal_received = false;
fc44f1
+static struct event_base *evbase = NULL;
fc44f1
 
fc44f1
 TAILQ_HEAD(topdir_list_head, topdir) topdir_list;
fc44f1
 
fc44f1
@@ -359,20 +359,28 @@ out:
fc44f1
 	free(port);
fc44f1
 }
fc44f1
 
fc44f1
+/* Actually frees clp and fields that might be used from other
fc44f1
+ * threads if was last reference.
fc44f1
+ */
fc44f1
 static void
fc44f1
-gssd_destroy_client(struct clnt_info *clp)
fc44f1
+gssd_free_client(struct clnt_info *clp)
fc44f1
 {
fc44f1
-	if (clp->krb5_fd >= 0) {
fc44f1
+	int refcnt;
fc44f1
+
fc44f1
+	pthread_mutex_lock(&clp_lock);
fc44f1
+	refcnt = --clp->refcount;
fc44f1
+	pthread_mutex_unlock(&clp_lock);
fc44f1
+	if (refcnt > 0)
fc44f1
+		return;
fc44f1
+
fc44f1
+	printerr(3, "freeing client %s\n", clp->relpath);
fc44f1
+
fc44f1
+	if (clp->krb5_fd >= 0)
fc44f1
 		close(clp->krb5_fd);
fc44f1
-		event_del(&clp->krb5_ev);
fc44f1
-	}
fc44f1
 
fc44f1
-	if (clp->gssd_fd >= 0) {
fc44f1
+	if (clp->gssd_fd >= 0)
fc44f1
 		close(clp->gssd_fd);
fc44f1
-		event_del(&clp->gssd_ev);
fc44f1
-	}
fc44f1
 
fc44f1
-	inotify_rm_watch(inotify_fd, clp->wd);
fc44f1
 	free(clp->relpath);
fc44f1
 	free(clp->servicename);
fc44f1
 	free(clp->servername);
fc44f1
@@ -380,6 +388,30 @@ gssd_destroy_client(struct clnt_info *clp)
fc44f1
 	free(clp);
fc44f1
 }
fc44f1
 
fc44f1
+/* Called when removing from clnt_list to tear down event handling.
fc44f1
+ * Will then free clp if was last reference.
fc44f1
+ */
fc44f1
+static void
fc44f1
+gssd_destroy_client(struct clnt_info *clp)
fc44f1
+{
fc44f1
+	printerr(3, "destroying client %s\n", clp->relpath);
fc44f1
+
fc44f1
+	if (clp->krb5_ev) {
fc44f1
+		event_del(clp->krb5_ev);
fc44f1
+		event_free(clp->krb5_ev);
fc44f1
+		clp->krb5_ev = NULL;
fc44f1
+	}
fc44f1
+
fc44f1
+	if (clp->gssd_ev) {
fc44f1
+		event_del(clp->gssd_ev);
fc44f1
+		event_free(clp->gssd_ev);
fc44f1
+		clp->gssd_ev = NULL;
fc44f1
+	}
fc44f1
+
fc44f1
+	inotify_rm_watch(inotify_fd, clp->wd);
fc44f1
+	gssd_free_client(clp);
fc44f1
+}
fc44f1
+
fc44f1
 static void gssd_scan(void);
fc44f1
 
fc44f1
 static int
fc44f1
@@ -416,11 +448,21 @@ static struct clnt_upcall_info *alloc_upcall_info(struct clnt_info *clp)
fc44f1
 	info = malloc(sizeof(struct clnt_upcall_info));
fc44f1
 	if (info == NULL)
fc44f1
 		return NULL;
fc44f1
+
fc44f1
+	pthread_mutex_lock(&clp_lock);
fc44f1
+	clp->refcount++;
fc44f1
+	pthread_mutex_unlock(&clp_lock);
fc44f1
 	info->clp = clp;
fc44f1
 
fc44f1
 	return info;
fc44f1
 }
fc44f1
 
fc44f1
+void free_upcall_info(struct clnt_upcall_info *info)
fc44f1
+{
fc44f1
+	gssd_free_client(info->clp);
fc44f1
+	free(info);
fc44f1
+}
fc44f1
+
fc44f1
 /* For each upcall read the upcall info into the buffer, then create a
fc44f1
  * thread in a detached state so that resources are released back into
fc44f1
  * the system without the need for a join.
fc44f1
@@ -438,13 +480,13 @@ gssd_clnt_gssd_cb(int UNUSED(fd), short UNUSED(which), void *data)
fc44f1
 	info->lbuflen = read(clp->gssd_fd, info->lbuf, sizeof(info->lbuf));
fc44f1
 	if (info->lbuflen <= 0 || info->lbuf[info->lbuflen-1] != '\n') {
fc44f1
 		printerr(0, "WARNING: %s: failed reading request\n", __func__);
fc44f1
-		free(info);
fc44f1
+		free_upcall_info(info);
fc44f1
 		return;
fc44f1
 	}
fc44f1
 	info->lbuf[info->lbuflen-1] = 0;
fc44f1
 
fc44f1
 	if (start_upcall_thread(handle_gssd_upcall, info))
fc44f1
-		free(info);
fc44f1
+		free_upcall_info(info);
fc44f1
 }
fc44f1
 
fc44f1
 static void
fc44f1
@@ -461,12 +503,12 @@ gssd_clnt_krb5_cb(int UNUSED(fd), short UNUSED(which), void *data)
fc44f1
 			sizeof(info->uid)) < (ssize_t)sizeof(info->uid)) {
fc44f1
 		printerr(0, "WARNING: %s: failed reading uid from krb5 "
fc44f1
 			 "upcall pipe: %s\n", __func__, strerror(errno));
fc44f1
-		free(info);
fc44f1
+		free_upcall_info(info);
fc44f1
 		return;
fc44f1
 	}
fc44f1
 
fc44f1
 	if (start_upcall_thread(handle_krb5_upcall, info))
fc44f1
-		free(info);
fc44f1
+		free_upcall_info(info);
fc44f1
 }
fc44f1
 
fc44f1
 static struct clnt_info *
fc44f1
@@ -501,6 +543,7 @@ gssd_get_clnt(struct topdir *tdi, const char *name)
fc44f1
 	clp->name = clp->relpath + strlen(tdi->name) + 1;
fc44f1
 	clp->krb5_fd = -1;
fc44f1
 	clp->gssd_fd = -1;
fc44f1
+	clp->refcount = 1;
fc44f1
 
fc44f1
 	TAILQ_INSERT_HEAD(&tdi->clnt_list, clp, list);
fc44f1
 	return clp;
fc44f1
@@ -535,15 +578,15 @@ gssd_scan_clnt(struct clnt_info *clp)
fc44f1
 		clp->krb5_fd = openat(clntfd, "krb5", O_RDWR | O_NONBLOCK);
fc44f1
 
fc44f1
 	if (gssd_was_closed && clp->gssd_fd >= 0) {
fc44f1
-		event_set(&clp->gssd_ev, clp->gssd_fd, EV_READ | EV_PERSIST,
fc44f1
-			  gssd_clnt_gssd_cb, clp);
fc44f1
-		event_add(&clp->gssd_ev, NULL);
fc44f1
+		clp->gssd_ev = event_new(evbase, clp->gssd_fd, EV_READ | EV_PERSIST,
fc44f1
+					 gssd_clnt_gssd_cb, clp);
fc44f1
+		event_add(clp->gssd_ev, NULL);
fc44f1
 	}
fc44f1
 
fc44f1
 	if (krb5_was_closed && clp->krb5_fd >= 0) {
fc44f1
-		event_set(&clp->krb5_ev, clp->krb5_fd, EV_READ | EV_PERSIST,
fc44f1
-			  gssd_clnt_krb5_cb, clp);
fc44f1
-		event_add(&clp->krb5_ev, NULL);
fc44f1
+		clp->krb5_ev = event_new(evbase, clp->krb5_fd, EV_READ | EV_PERSIST,
fc44f1
+					 gssd_clnt_krb5_cb, clp);
fc44f1
+		event_add(clp->krb5_ev, NULL);
fc44f1
 	}
fc44f1
 
fc44f1
 	if (clp->krb5_fd == -1 && clp->gssd_fd == -1)
fc44f1
@@ -649,7 +692,7 @@ gssd_scan_topdir(const char *name)
fc44f1
 		if (clp->scanned)
fc44f1
 			continue;
fc44f1
 
fc44f1
-		printerr(3, "destroying client %s\n", clp->relpath);
fc44f1
+		printerr(3, "orphaned client %s\n", clp->relpath);
fc44f1
 		saveprev = clp->list.tqe_prev;
fc44f1
 		TAILQ_REMOVE(&tdi->clnt_list, clp, list);
fc44f1
 		gssd_destroy_client(clp);
fc44f1
@@ -746,12 +789,16 @@ gssd_inotify_clnt(struct topdir *tdi, struct clnt_info *clp, const struct inotif
fc44f1
 	} else if (ev->mask & IN_DELETE) {
fc44f1
 		if (!strcmp(ev->name, "gssd") && clp->gssd_fd >= 0) {
fc44f1
 			close(clp->gssd_fd);
fc44f1
-			event_del(&clp->gssd_ev);
fc44f1
+			event_del(clp->gssd_ev);
fc44f1
+			event_free(clp->gssd_ev);
fc44f1
+			clp->gssd_ev = NULL;
fc44f1
 			clp->gssd_fd = -1;
fc44f1
 
fc44f1
 		} else if (!strcmp(ev->name, "krb5") && clp->krb5_fd >= 0) {
fc44f1
 			close(clp->krb5_fd);
fc44f1
-			event_del(&clp->krb5_ev);
fc44f1
+			event_del(clp->krb5_ev);
fc44f1
+			event_free(clp->krb5_ev);
fc44f1
+			clp->krb5_ev = NULL;
fc44f1
 			clp->krb5_fd = -1;
fc44f1
 		}
fc44f1
 
fc44f1
@@ -824,10 +871,15 @@ found:
fc44f1
 static void
fc44f1
 sig_die(int signal)
fc44f1
 {
fc44f1
-	if (root_uses_machine_creds)
fc44f1
-		gssd_destroy_krb5_machine_creds();
fc44f1
+	if (signal_received) {
fc44f1
+		gssd_destroy_krb5_principals(root_uses_machine_creds);
fc44f1
+		printerr(1, "forced exiting on signal %d\n", signal);
fc44f1
+		exit(0);
fc44f1
+	}
fc44f1
+
fc44f1
+	signal_received = true;
fc44f1
 	printerr(1, "exiting on signal %d\n", signal);
fc44f1
-	exit(0);
fc44f1
+	event_base_loopexit(evbase, NULL);
fc44f1
 }
fc44f1
 
fc44f1
 static void
fc44f1
@@ -884,9 +936,10 @@ main(int argc, char *argv[])
fc44f1
 	int rpc_verbosity = 0;
fc44f1
 	int opt;
fc44f1
 	int i;
fc44f1
+	int rc;
fc44f1
 	extern char *optarg;
fc44f1
 	char *progname;
fc44f1
-	struct event sighup_ev;
fc44f1
+	struct event *sighup_ev;
fc44f1
 
fc44f1
 	read_gss_conf();
fc44f1
 
fc44f1
@@ -1025,7 +1078,11 @@ main(int argc, char *argv[])
fc44f1
 	if (gssd_check_mechs() != 0)
fc44f1
 		errx(1, "Problem with gssapi library");
fc44f1
 
fc44f1
-	event_init();
fc44f1
+	evbase = event_base_new();
fc44f1
+	if (!evbase) {
fc44f1
+		printerr(0, "ERROR: failed to create event base\n");
fc44f1
+		exit(EXIT_FAILURE);
fc44f1
+	}
fc44f1
 
fc44f1
 	pipefs_dir = opendir(pipefs_path);
fc44f1
 	if (!pipefs_dir) {
fc44f1
@@ -1047,18 +1104,43 @@ main(int argc, char *argv[])
fc44f1
 
fc44f1
 	signal(SIGINT, sig_die);
fc44f1
 	signal(SIGTERM, sig_die);
fc44f1
-	signal_set(&sighup_ev, SIGHUP, gssd_scan_cb, NULL);
fc44f1
-	signal_add(&sighup_ev, NULL);
fc44f1
-	event_set(&inotify_ev, inotify_fd, EV_READ | EV_PERSIST, gssd_inotify_cb, NULL);
fc44f1
-	event_add(&inotify_ev, NULL);
fc44f1
+	sighup_ev = evsignal_new(evbase, SIGHUP, gssd_scan_cb, NULL);
fc44f1
+	evsignal_add(sighup_ev, NULL);
fc44f1
+	inotify_ev = event_new(evbase, inotify_fd, EV_READ | EV_PERSIST,
fc44f1
+			       gssd_inotify_cb, NULL);
fc44f1
+	event_add(inotify_ev, NULL);
fc44f1
 
fc44f1
 	TAILQ_INIT(&topdir_list);
fc44f1
 	gssd_scan();
fc44f1
 	daemon_ready();
fc44f1
 
fc44f1
-	event_dispatch();
fc44f1
+	rc = event_base_dispatch(evbase);
fc44f1
 
fc44f1
-	printerr(0, "ERROR: event_dispatch() returned!\n");
fc44f1
-	return EXIT_FAILURE;
fc44f1
-}
fc44f1
+	printerr(0, "event_dispatch() returned %i!\n", rc);
fc44f1
+
fc44f1
+	gssd_destroy_krb5_principals(root_uses_machine_creds);
fc44f1
+
fc44f1
+	while (!TAILQ_EMPTY(&topdir_list)) {
fc44f1
+		struct topdir *tdi = TAILQ_FIRST(&topdir_list);
fc44f1
+		TAILQ_REMOVE(&topdir_list, tdi, list);
fc44f1
+		while (!TAILQ_EMPTY(&tdi->clnt_list)) {
fc44f1
+			struct clnt_info *clp = TAILQ_FIRST(&tdi->clnt_list);
fc44f1
+			TAILQ_REMOVE(&tdi->clnt_list, clp, list);
fc44f1
+			gssd_destroy_client(clp);
fc44f1
+		}
fc44f1
+		free(tdi);
fc44f1
+	}
fc44f1
+
fc44f1
+	event_free(inotify_ev);
fc44f1
+	event_free(sighup_ev);
fc44f1
+	event_base_free(evbase);
fc44f1
+
fc44f1
+	close(inotify_fd);
fc44f1
+	close(pipefs_fd);
fc44f1
+	closedir(pipefs_dir);
fc44f1
 
fc44f1
+	free(preferred_realm);
fc44f1
+	free(ccachesearch);
fc44f1
+
fc44f1
+	return rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
fc44f1
+}
fc44f1
diff --git a/utils/gssd/gssd.h b/utils/gssd/gssd.h
fc44f1
index f4f59754..1e8c58d4 100644
fc44f1
--- a/utils/gssd/gssd.h
fc44f1
+++ b/utils/gssd/gssd.h
fc44f1
@@ -62,13 +62,10 @@ extern int			root_uses_machine_creds;
fc44f1
 extern unsigned int 		context_timeout;
fc44f1
 extern unsigned int rpc_timeout;
fc44f1
 extern char			*preferred_realm;
fc44f1
-extern pthread_mutex_t ple_lock;
fc44f1
-extern pthread_cond_t pcond;
fc44f1
-extern pthread_mutex_t pmutex;
fc44f1
-extern int thread_started;
fc44f1
 
fc44f1
 struct clnt_info {
fc44f1
 	TAILQ_ENTRY(clnt_info)	list;
fc44f1
+	int			refcount;
fc44f1
 	int			wd;
fc44f1
 	bool			scanned;
fc44f1
 	char			*name;
fc44f1
@@ -79,9 +76,9 @@ struct clnt_info {
fc44f1
 	int			vers;
fc44f1
 	char			*protocol;
fc44f1
 	int			krb5_fd;
fc44f1
-	struct event		krb5_ev;
fc44f1
+	struct event		*krb5_ev;
fc44f1
 	int			gssd_fd;
fc44f1
-	struct event		gssd_ev;
fc44f1
+	struct event		*gssd_ev;
fc44f1
 	struct			sockaddr_storage addr;
fc44f1
 };
fc44f1
 
fc44f1
@@ -94,6 +91,7 @@ struct clnt_upcall_info {
fc44f1
 
fc44f1
 void handle_krb5_upcall(struct clnt_upcall_info *clp);
fc44f1
 void handle_gssd_upcall(struct clnt_upcall_info *clp);
fc44f1
+void free_upcall_info(struct clnt_upcall_info *info);
fc44f1
 
fc44f1
 
fc44f1
 #endif /* _RPC_GSSD_H_ */
fc44f1
diff --git a/utils/gssd/gssd_proc.c b/utils/gssd/gssd_proc.c
fc44f1
index bfcf3f09..ae3ebe81 100644
fc44f1
--- a/utils/gssd/gssd_proc.c
fc44f1
+++ b/utils/gssd/gssd_proc.c
fc44f1
@@ -149,9 +149,10 @@ do_downcall(int k5_fd, uid_t uid, struct authgss_private_data *pd,
fc44f1
 	char    *buf = NULL, *p = NULL, *end = NULL;
fc44f1
 	unsigned int timeout = context_timeout;
fc44f1
 	unsigned int buf_size = 0;
fc44f1
+	pthread_t tid = pthread_self();
fc44f1
 
fc44f1
-	printerr(2, "doing downcall: lifetime_rec=%u acceptor=%.*s\n",
fc44f1
-		lifetime_rec, acceptor->length, acceptor->value);
fc44f1
+	printerr(2, "do_downcall(0x%x): lifetime_rec=%u acceptor=%.*s\n",
fc44f1
+		tid, lifetime_rec, acceptor->length, acceptor->value);
fc44f1
 	buf_size = sizeof(uid) + sizeof(timeout) + sizeof(pd->pd_seq_win) +
fc44f1
 		sizeof(pd->pd_ctx_hndl.length) + pd->pd_ctx_hndl.length +
fc44f1
 		sizeof(context_token->length) + context_token->length +
fc44f1
@@ -177,7 +178,7 @@ do_downcall(int k5_fd, uid_t uid, struct authgss_private_data *pd,
fc44f1
 	return;
fc44f1
 out_err:
fc44f1
 	free(buf);
fc44f1
-	printerr(1, "Failed to write downcall!\n");
fc44f1
+	printerr(1, "do_downcall(0x%x): Failed to write downcall!\n", tid);
fc44f1
 	return;
fc44f1
 }
fc44f1
 
fc44f1
@@ -231,7 +232,7 @@ populate_port(struct sockaddr *sa, const socklen_t salen,
fc44f1
 	switch (sa->sa_family) {
fc44f1
 	case AF_INET:
fc44f1
 		if (s4->sin_port != 0) {
fc44f1
-			printerr(2, "DEBUG: port already set to %d\n",
fc44f1
+			printerr(4, "DEBUG: port already set to %d\n",
fc44f1
 				 ntohs(s4->sin_port));
fc44f1
 			return 1;
fc44f1
 		}
fc44f1
@@ -239,7 +240,7 @@ populate_port(struct sockaddr *sa, const socklen_t salen,
fc44f1
 #ifdef IPV6_SUPPORTED
fc44f1
 	case AF_INET6:
fc44f1
 		if (s6->sin6_port != 0) {
fc44f1
-			printerr(2, "DEBUG: port already set to %d\n",
fc44f1
+			printerr(4, "DEBUG: port already set to %d\n",
fc44f1
 				 ntohs(s6->sin6_port));
fc44f1
 			return 1;
fc44f1
 		}
fc44f1
@@ -544,7 +545,7 @@ krb5_use_machine_creds(struct clnt_info *clp, uid_t uid,
fc44f1
 		uid, tgtname);
fc44f1
 
fc44f1
 	do {
fc44f1
-		gssd_refresh_krb5_machine_credential(clp->servername, NULL,
fc44f1
+		gssd_refresh_krb5_machine_credential(clp->servername,
fc44f1
 						     service, srchost);
fc44f1
 	/*
fc44f1
 	 * Get a list of credential cache names and try each
fc44f1
@@ -726,7 +727,7 @@ handle_krb5_upcall(struct clnt_upcall_info *info)
fc44f1
 	printerr(2, "\n%s: uid %d (%s)\n", __func__, info->uid, clp->relpath);
fc44f1
 
fc44f1
 	process_krb5_upcall(clp, info->uid, clp->krb5_fd, NULL, NULL, NULL);
fc44f1
-	free(info);
fc44f1
+	free_upcall_info(info);
fc44f1
 }
fc44f1
 
fc44f1
 void
fc44f1
@@ -743,8 +744,10 @@ handle_gssd_upcall(struct clnt_upcall_info *info)
fc44f1
 	char			*enctypes = NULL;
fc44f1
 	char			*upcall_str;
fc44f1
 	char			*pbuf = info->lbuf;
fc44f1
+	pthread_t tid = pthread_self();
fc44f1
 
fc44f1
-	printerr(2, "%s: '%s' (%s)\n", __func__, info->lbuf, clp->relpath);
fc44f1
+	printerr(2, "\n%s(0x%x): '%s' (%s)\n", __func__, tid, 
fc44f1
+		info->lbuf, clp->relpath);
fc44f1
 
fc44f1
 	upcall_str = strdup(info->lbuf);
fc44f1
 	if (upcall_str == NULL) {
fc44f1
@@ -826,6 +829,6 @@ handle_gssd_upcall(struct clnt_upcall_info *info)
fc44f1
 out:
fc44f1
 	free(upcall_str);
fc44f1
 out_nomem:
fc44f1
-	free(info);
fc44f1
+	free_upcall_info(info);
fc44f1
 	return;
fc44f1
 }
fc44f1
diff --git a/utils/gssd/krb5_util.c b/utils/gssd/krb5_util.c
fc44f1
index 26e51edf..d675c3a4 100644
fc44f1
--- a/utils/gssd/krb5_util.c
fc44f1
+++ b/utils/gssd/krb5_util.c
fc44f1
@@ -126,9 +126,28 @@
fc44f1
 #include "gss_util.h"
fc44f1
 #include "krb5_util.h"
fc44f1
 
fc44f1
+/*
fc44f1
+ * List of principals from our keytab that we
fc44f1
+ * will try to use to obtain credentials
fc44f1
+ * (known as a principal list entry (ple))
fc44f1
+ */
fc44f1
+struct gssd_k5_kt_princ {
fc44f1
+	struct gssd_k5_kt_princ *next;
fc44f1
+	// Only protect against deletion, not modification
fc44f1
+	int refcount;
fc44f1
+	// Only set during creation in new_ple()
fc44f1
+	krb5_principal princ;
fc44f1
+	char *realm;
fc44f1
+	// Modified during usage by gssd_get_single_krb5_cred()
fc44f1
+	char *ccname;
fc44f1
+	krb5_timestamp endtime;
fc44f1
+};
fc44f1
+
fc44f1
+
fc44f1
 /* Global list of principals/cache file names for machine credentials */
fc44f1
-struct gssd_k5_kt_princ *gssd_k5_kt_princ_list = NULL;
fc44f1
-pthread_mutex_t ple_lock = PTHREAD_MUTEX_INITIALIZER;
fc44f1
+static struct gssd_k5_kt_princ *gssd_k5_kt_princ_list = NULL;
fc44f1
+/* This mutex protects list modification & ple->ccname */
fc44f1
+static pthread_mutex_t ple_lock = PTHREAD_MUTEX_INITIALIZER;
fc44f1
 
fc44f1
 #ifdef HAVE_SET_ALLOWABLE_ENCTYPES
fc44f1
 int limit_to_legacy_enctypes = 0;
fc44f1
@@ -146,6 +165,18 @@ static int gssd_get_single_krb5_cred(krb5_context context,
fc44f1
 static int query_krb5_ccache(const char* cred_cache, char **ret_princname,
fc44f1
 		char **ret_realm);
fc44f1
 
fc44f1
+static void release_ple(krb5_context context, struct gssd_k5_kt_princ *ple)
fc44f1
+{
fc44f1
+	if (--ple->refcount)
fc44f1
+		return;
fc44f1
+
fc44f1
+	printerr(3, "freeing cached principal (ccname=%s, realm=%s)\n", ple->ccname, ple->realm);
fc44f1
+	krb5_free_principal(context, ple->princ);
fc44f1
+	free(ple->ccname);
fc44f1
+	free(ple->realm);
fc44f1
+	free(ple);
fc44f1
+}
fc44f1
+
fc44f1
 /*
fc44f1
  * Called from the scandir function to weed out potential krb5
fc44f1
  * credentials cache files
fc44f1
@@ -352,12 +383,15 @@ gssd_get_single_krb5_cred(krb5_context context,
fc44f1
 	 * 300 because clock skew must be within 300sec for kerberos
fc44f1
 	 */
fc44f1
 	now += 300;
fc44f1
+	pthread_mutex_lock(&ple_lock);
fc44f1
 	if (ple->ccname && ple->endtime > now && !nocache) {
fc44f1
 		printerr(3, "INFO: Credentials in CC '%s' are good until %d\n",
fc44f1
 			 ple->ccname, ple->endtime);
fc44f1
 		code = 0;
fc44f1
+		pthread_mutex_unlock(&ple_lock);
fc44f1
 		goto out;
fc44f1
 	}
fc44f1
+	pthread_mutex_unlock(&ple_lock);
fc44f1
 
fc44f1
 	if ((code = krb5_kt_get_name(context, kt, kt_name, BUFSIZ))) {
fc44f1
 		printerr(0, "ERROR: Unable to get keytab name in "
fc44f1
@@ -410,6 +444,7 @@ gssd_get_single_krb5_cred(krb5_context context,
fc44f1
 	 * Initialize cache file which we're going to be using
fc44f1
 	 */
fc44f1
 
fc44f1
+	pthread_mutex_lock(&ple_lock);
fc44f1
 	if (use_memcache)
fc44f1
 	    cache_type = "MEMORY";
fc44f1
 	else
fc44f1
@@ -419,15 +454,18 @@ gssd_get_single_krb5_cred(krb5_context context,
fc44f1
 		ccachesearch[0], GSSD_DEFAULT_CRED_PREFIX,
fc44f1
 		GSSD_DEFAULT_MACHINE_CRED_SUFFIX, ple->realm);
fc44f1
 	ple->endtime = my_creds.times.endtime;
fc44f1
-	if (ple->ccname != NULL)
fc44f1
+	if (ple->ccname == NULL || strcmp(ple->ccname, cc_name) != 0) {
fc44f1
 		free(ple->ccname);
fc44f1
-	ple->ccname = strdup(cc_name);
fc44f1
-	if (ple->ccname == NULL) {
fc44f1
-		printerr(0, "ERROR: no storage to duplicate credentials "
fc44f1
-			    "cache name '%s'\n", cc_name);
fc44f1
-		code = ENOMEM;
fc44f1
-		goto out;
fc44f1
+		ple->ccname = strdup(cc_name);
fc44f1
+		if (ple->ccname == NULL) {
fc44f1
+			printerr(0, "ERROR: no storage to duplicate credentials "
fc44f1
+				    "cache name '%s'\n", cc_name);
fc44f1
+			code = ENOMEM;
fc44f1
+			pthread_mutex_unlock(&ple_lock);
fc44f1
+			goto out;
fc44f1
+		}
fc44f1
 	}
fc44f1
+	pthread_mutex_unlock(&ple_lock);
fc44f1
 	if ((code = krb5_cc_resolve(context, cc_name, &ccache))) {
fc44f1
 		k5err = gssd_k5_err_msg(context, code);
fc44f1
 		printerr(0, "ERROR: %s while opening credential cache '%s'\n",
fc44f1
@@ -465,6 +503,7 @@ gssd_get_single_krb5_cred(krb5_context context,
fc44f1
 
fc44f1
 /*
fc44f1
  * Given a principal, find a matching ple structure
fc44f1
+ * Called with mutex held
fc44f1
  */
fc44f1
 static struct gssd_k5_kt_princ *
fc44f1
 find_ple_by_princ(krb5_context context, krb5_principal princ)
fc44f1
@@ -481,6 +520,7 @@ find_ple_by_princ(krb5_context context, krb5_principal princ)
fc44f1
 
fc44f1
 /*
fc44f1
  * Create, initialize, and add a new ple structure to the global list
fc44f1
+ * Called with mutex held
fc44f1
  */
fc44f1
 static struct gssd_k5_kt_princ *
fc44f1
 new_ple(krb5_context context, krb5_principal princ)
fc44f1
@@ -532,6 +572,7 @@ new_ple(krb5_context context, krb5_principal princ)
fc44f1
 			p->next = ple;
fc44f1
 	}
fc44f1
 
fc44f1
+	ple->refcount = 1;
fc44f1
 	return ple;
fc44f1
 outerr:
fc44f1
 	if (ple) {
fc44f1
@@ -550,13 +591,14 @@ get_ple_by_princ(krb5_context context, krb5_principal princ)
fc44f1
 {
fc44f1
 	struct gssd_k5_kt_princ *ple;
fc44f1
 
fc44f1
-	/* Need to serialize list if we ever become multi-threaded! */
fc44f1
-
fc44f1
 	pthread_mutex_lock(&ple_lock);
fc44f1
 	ple = find_ple_by_princ(context, princ);
fc44f1
 	if (ple == NULL) {
fc44f1
 		ple = new_ple(context, princ);
fc44f1
 	}
fc44f1
+	if (ple != NULL) {
fc44f1
+		ple->refcount++;
fc44f1
+	}
fc44f1
 	pthread_mutex_unlock(&ple_lock);
fc44f1
 
fc44f1
 	return ple;
fc44f1
@@ -721,6 +763,8 @@ gssd_search_krb5_keytab(krb5_context context, krb5_keytab kt,
fc44f1
 				retval = ENOMEM;
fc44f1
 				k5_free_kt_entry(context, kte);
fc44f1
 			} else {
fc44f1
+				release_ple(context, ple);
fc44f1
+				ple = NULL;
fc44f1
 				retval = 0;
fc44f1
 				*found = 1;
fc44f1
 			}
fc44f1
@@ -796,12 +840,12 @@ find_keytab_entry(krb5_context context, krb5_keytab kt,
fc44f1
 	/* Compute the active directory machine name HOST$ */
fc44f1
 	krb5_appdefault_string(context, "nfs", NULL, "ad_principal_name", 
fc44f1
 		notsetstr, &adhostoverride);
fc44f1
-	if (strcmp(adhostoverride, notsetstr) != 0) {
fc44f1
-	        printerr (1, 
fc44f1
-				"AD host string overridden with \"%s\" from appdefaults\n", 
fc44f1
-				adhostoverride);
fc44f1
-	        /* No overflow: Windows cannot handle strings longer than 19 chars */
fc44f1
-	        strcpy(myhostad, adhostoverride);
fc44f1
+	if (adhostoverride && strcmp(adhostoverride, notsetstr) != 0) {
fc44f1
+		printerr(1,
fc44f1
+			 "AD host string overridden with \"%s\" from appdefaults\n",
fc44f1
+			 adhostoverride);
fc44f1
+		/* No overflow: Windows cannot handle strings longer than 19 chars */
fc44f1
+		strcpy(myhostad, adhostoverride);
fc44f1
 	} else {
fc44f1
 	        strcpy(myhostad, myhostname);
fc44f1
 	        for (i = 0; myhostad[i] != 0; ++i) {
fc44f1
@@ -928,7 +972,7 @@ find_keytab_entry(krb5_context context, krb5_keytab kt,
fc44f1
 					tried_upper = 1;
fc44f1
 				}
fc44f1
 			} else {
fc44f1
-				printerr(3, "Success getting keytab entry for '%s'\n",spn);
fc44f1
+				printerr(2, "Success getting keytab entry for '%s'\n",spn);
fc44f1
 				retval = 0;
fc44f1
 				goto out;
fc44f1
 			}
fc44f1
@@ -1053,6 +1097,93 @@ err_cache:
fc44f1
 	return (*ret_princname && *ret_realm);
fc44f1
 }
fc44f1
 
fc44f1
+/*
fc44f1
+ * Obtain (or refresh if necessary) Kerberos machine credentials
fc44f1
+ * If a ple is passed in, it's reference will be released
fc44f1
+ */
fc44f1
+static int
fc44f1
+gssd_refresh_krb5_machine_credential_internal(char *hostname,
fc44f1
+				     struct gssd_k5_kt_princ *ple,
fc44f1
+				     char *service, char *srchost)
fc44f1
+{
fc44f1
+	krb5_error_code code = 0;
fc44f1
+	krb5_context context;
fc44f1
+	krb5_keytab kt = NULL;;
fc44f1
+	int retval = 0;
fc44f1
+	char *k5err = NULL;
fc44f1
+	const char *svcnames[] = { "$", "root", "nfs", "host", NULL };
fc44f1
+
fc44f1
+	printerr(2, "%s: hostname=%s ple=%p service=%s srchost=%s\n",
fc44f1
+		__func__, hostname, ple, service, srchost);
fc44f1
+
fc44f1
+	/*
fc44f1
+	 * If a specific service name was specified, use it.
fc44f1
+	 * Otherwise, use the default list.
fc44f1
+	 */
fc44f1
+	if (service != NULL && strcmp(service, "*") != 0) {
fc44f1
+		svcnames[0] = service;
fc44f1
+		svcnames[1] = NULL;
fc44f1
+	}
fc44f1
+	if (hostname == NULL && ple == NULL)
fc44f1
+		return EINVAL;
fc44f1
+
fc44f1
+	code = krb5_init_context(&context);
fc44f1
+	if (code) {
fc44f1
+		k5err = gssd_k5_err_msg(NULL, code);
fc44f1
+		printerr(0, "ERROR: %s: %s while initializing krb5 context\n",
fc44f1
+			 __func__, k5err);
fc44f1
+		retval = code;
fc44f1
+		goto out;
fc44f1
+	}
fc44f1
+
fc44f1
+	if ((code = krb5_kt_resolve(context, keytabfile, &kt))) {
fc44f1
+		k5err = gssd_k5_err_msg(context, code);
fc44f1
+		printerr(0, "ERROR: %s: %s while resolving keytab '%s'\n",
fc44f1
+			 __func__, k5err, keytabfile);
fc44f1
+		goto out_free_context;
fc44f1
+	}
fc44f1
+
fc44f1
+	if (ple == NULL) {
fc44f1
+		krb5_keytab_entry kte;
fc44f1
+
fc44f1
+		code = find_keytab_entry(context, kt, srchost, hostname,
fc44f1
+					 &kte, svcnames);
fc44f1
+		if (code) {
fc44f1
+			printerr(0, "ERROR: %s: no usable keytab entry found "
fc44f1
+				 "in keytab %s for connection with host %s\n",
fc44f1
+				 __FUNCTION__, keytabfile, hostname);
fc44f1
+			retval = code;
fc44f1
+			goto out_free_kt;
fc44f1
+		}
fc44f1
+
fc44f1
+		ple = get_ple_by_princ(context, kte.principal);
fc44f1
+		k5_free_kt_entry(context, &kte);
fc44f1
+		if (ple == NULL) {
fc44f1
+			char *pname;
fc44f1
+			if ((krb5_unparse_name(context, kte.principal, &pname))) {
fc44f1
+				pname = NULL;
fc44f1
+			}
fc44f1
+			printerr(0, "ERROR: %s: Could not locate or create "
fc44f1
+				 "ple struct for principal %s for connection "
fc44f1
+				 "with host %s\n",
fc44f1
+				 __FUNCTION__, pname ? pname : "<unparsable>",
fc44f1
+				 hostname);
fc44f1
+			if (pname) k5_free_unparsed_name(context, pname);
fc44f1
+			goto out_free_kt;
fc44f1
+		}
fc44f1
+	}
fc44f1
+	retval = gssd_get_single_krb5_cred(context, kt, ple, 0);
fc44f1
+out_free_kt:
fc44f1
+	krb5_kt_close(context, kt);
fc44f1
+out_free_context:
fc44f1
+	if (ple)
fc44f1
+		release_ple(context, ple);
fc44f1
+	krb5_free_context(context);
fc44f1
+out:
fc44f1
+	free(k5err);
fc44f1
+	return retval;
fc44f1
+}
fc44f1
+
fc44f1
 /*==========================*/
fc44f1
 /*===  External routines ===*/
fc44f1
 /*==========================*/
fc44f1
@@ -1146,37 +1277,56 @@ gssd_get_krb5_machine_cred_list(char ***list)
fc44f1
 		goto out;
fc44f1
 	}
fc44f1
 
fc44f1
-	/* Need to serialize list if we ever become multi-threaded! */
fc44f1
-
fc44f1
+	pthread_mutex_lock(&ple_lock);
fc44f1
 	for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
fc44f1
-		if (ple->ccname) {
fc44f1
-			/* Make sure cred is up-to-date before returning it */
fc44f1
-			retval = gssd_refresh_krb5_machine_credential(NULL, ple,
fc44f1
-								      NULL, NULL);
fc44f1
-			if (retval)
fc44f1
-				continue;
fc44f1
-			if (i + 1 > listsize) {
fc44f1
-				listsize += listinc;
fc44f1
-				l = (char **)
fc44f1
-					realloc(l, listsize * sizeof(char *));
fc44f1
-				if (l == NULL) {
fc44f1
-					retval = ENOMEM;
fc44f1
-					goto out;
fc44f1
-				}
fc44f1
-			}
fc44f1
-			if ((l[i++] = strdup(ple->ccname)) == NULL) {
fc44f1
+		if (!ple->ccname)
fc44f1
+			continue;
fc44f1
+
fc44f1
+		/* Take advantage of the fact we only remove the ple
fc44f1
+		 * from the list during shutdown. If it's modified
fc44f1
+		 * concurrently at worst we'll just miss a new entry
fc44f1
+		 * before the current ple
fc44f1
+		 *
fc44f1
+		 * gssd_refresh_krb5_machine_credential_internal() will
fc44f1
+		 * release the ple refcount
fc44f1
+		 */
fc44f1
+		ple->refcount++;
fc44f1
+		pthread_mutex_unlock(&ple_lock);
fc44f1
+		/* Make sure cred is up-to-date before returning it */
fc44f1
+		retval = gssd_refresh_krb5_machine_credential_internal(NULL, ple,
fc44f1
+								       NULL, NULL);
fc44f1
+		pthread_mutex_lock(&ple_lock);
fc44f1
+		if (gssd_k5_kt_princ_list == NULL) {
fc44f1
+			/* Looks like we did shutdown... abort */
fc44f1
+			l[i] = NULL;
fc44f1
+			gssd_free_krb5_machine_cred_list(l);
fc44f1
+			retval = ENOMEM;
fc44f1
+			goto out_lock;
fc44f1
+		}
fc44f1
+		if (retval)
fc44f1
+			continue;
fc44f1
+		if (i + 1 > listsize) {
fc44f1
+			listsize += listinc;
fc44f1
+			l = (char **)
fc44f1
+				realloc(l, listsize * sizeof(char *));
fc44f1
+			if (l == NULL) {
fc44f1
 				retval = ENOMEM;
fc44f1
-				goto out;
fc44f1
+				goto out_lock;
fc44f1
 			}
fc44f1
 		}
fc44f1
+		if ((l[i++] = strdup(ple->ccname)) == NULL) {
fc44f1
+			retval = ENOMEM;
fc44f1
+			goto out_lock;
fc44f1
+		}
fc44f1
 	}
fc44f1
 	if (i > 0) {
fc44f1
 		l[i] = NULL;
fc44f1
 		*list = l;
fc44f1
 		retval = 0;
fc44f1
-		goto out;
fc44f1
 	} else
fc44f1
 		free((void *)l);
fc44f1
+out_lock:
fc44f1
+	pthread_mutex_unlock(&ple_lock);
fc44f1
   out:
fc44f1
 	return retval;
fc44f1
 }
fc44f1
@@ -1201,7 +1351,7 @@ gssd_free_krb5_machine_cred_list(char **list)
fc44f1
  * Called upon exit.  Destroys machine credentials.
fc44f1
  */
fc44f1
 void
fc44f1
-gssd_destroy_krb5_machine_creds(void)
fc44f1
+gssd_destroy_krb5_principals(int destroy_machine_creds)
fc44f1
 {
fc44f1
 	krb5_context context;
fc44f1
 	krb5_error_code code = 0;
fc44f1
@@ -1213,33 +1363,38 @@ gssd_destroy_krb5_machine_creds(void)
fc44f1
 	if (code) {
fc44f1
 		k5err = gssd_k5_err_msg(NULL, code);
fc44f1
 		printerr(0, "ERROR: %s while initializing krb5\n", k5err);
fc44f1
-		goto out;
fc44f1
+		free(k5err);
fc44f1
+		return;
fc44f1
 	}
fc44f1
 
fc44f1
-	for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
fc44f1
-		if (!ple->ccname)
fc44f1
-			continue;
fc44f1
-		if ((code = krb5_cc_resolve(context, ple->ccname, &ccache))) {
fc44f1
-			k5err = gssd_k5_err_msg(context, code);
fc44f1
-			printerr(0, "WARNING: %s while resolving credential "
fc44f1
-				    "cache '%s' for destruction\n", k5err,
fc44f1
-				    ple->ccname);
fc44f1
-			krb5_free_string(context, k5err);
fc44f1
-			k5err = NULL;
fc44f1
-			continue;
fc44f1
-		}
fc44f1
+	pthread_mutex_lock(&ple_lock);
fc44f1
+	while (gssd_k5_kt_princ_list) {
fc44f1
+		ple = gssd_k5_kt_princ_list;
fc44f1
+		gssd_k5_kt_princ_list = ple->next;
fc44f1
 
fc44f1
-		if ((code = krb5_cc_destroy(context, ccache))) {
fc44f1
-			k5err = gssd_k5_err_msg(context, code);
fc44f1
-			printerr(0, "WARNING: %s while destroying credential "
fc44f1
-				    "cache '%s'\n", k5err, ple->ccname);
fc44f1
-			krb5_free_string(context, k5err);
fc44f1
-			k5err = NULL;
fc44f1
+		if (destroy_machine_creds && ple->ccname) {
fc44f1
+			if ((code = krb5_cc_resolve(context, ple->ccname, &ccache))) {
fc44f1
+				k5err = gssd_k5_err_msg(context, code);
fc44f1
+				printerr(0, "WARNING: %s while resolving credential "
fc44f1
+					    "cache '%s' for destruction\n", k5err,
fc44f1
+					    ple->ccname);
fc44f1
+				free(k5err);
fc44f1
+				k5err = NULL;
fc44f1
+			}
fc44f1
+
fc44f1
+			if (!code && (code = krb5_cc_destroy(context, ccache))) {
fc44f1
+				k5err = gssd_k5_err_msg(context, code);
fc44f1
+				printerr(0, "WARNING: %s while destroying credential "
fc44f1
+					    "cache '%s'\n", k5err, ple->ccname);
fc44f1
+				free(k5err);
fc44f1
+				k5err = NULL;
fc44f1
+			}
fc44f1
 		}
fc44f1
+
fc44f1
+		release_ple(context, ple);
fc44f1
 	}
fc44f1
+	pthread_mutex_unlock(&ple_lock);
fc44f1
 	krb5_free_context(context);
fc44f1
-  out:
fc44f1
-	krb5_free_string(context, k5err);
fc44f1
 }
fc44f1
 
fc44f1
 /*
fc44f1
@@ -1247,83 +1402,10 @@ gssd_destroy_krb5_machine_creds(void)
fc44f1
  */
fc44f1
 int
fc44f1
 gssd_refresh_krb5_machine_credential(char *hostname,
fc44f1
-				     struct gssd_k5_kt_princ *ple, 
fc44f1
 				     char *service, char *srchost)
fc44f1
 {
fc44f1
-	krb5_error_code code = 0;
fc44f1
-	krb5_context context;
fc44f1
-	krb5_keytab kt = NULL;;
fc44f1
-	int retval = 0;
fc44f1
-	char *k5err = NULL;
fc44f1
-	const char *svcnames[] = { "$", "root", "nfs", "host", NULL };
fc44f1
-
fc44f1
-	printerr(2, "%s: hostname=%s ple=%p service=%s srchost=%s\n",
fc44f1
-		__func__, hostname, ple, service, srchost);
fc44f1
-
fc44f1
-	/*
fc44f1
-	 * If a specific service name was specified, use it.
fc44f1
-	 * Otherwise, use the default list.
fc44f1
-	 */
fc44f1
-	if (service != NULL && strcmp(service, "*") != 0) {
fc44f1
-		svcnames[0] = service;
fc44f1
-		svcnames[1] = NULL;
fc44f1
-	}
fc44f1
-	if (hostname == NULL && ple == NULL)
fc44f1
-		return EINVAL;
fc44f1
-
fc44f1
-	code = krb5_init_context(&context);
fc44f1
-	if (code) {
fc44f1
-		k5err = gssd_k5_err_msg(NULL, code);
fc44f1
-		printerr(0, "ERROR: %s: %s while initializing krb5 context\n",
fc44f1
-			 __func__, k5err);
fc44f1
-		retval = code;
fc44f1
-		goto out;
fc44f1
-	}
fc44f1
-
fc44f1
-	if ((code = krb5_kt_resolve(context, keytabfile, &kt))) {
fc44f1
-		k5err = gssd_k5_err_msg(context, code);
fc44f1
-		printerr(0, "ERROR: %s: %s while resolving keytab '%s'\n",
fc44f1
-			 __func__, k5err, keytabfile);
fc44f1
-		goto out_free_context;
fc44f1
-	}
fc44f1
-
fc44f1
-	if (ple == NULL) {
fc44f1
-		krb5_keytab_entry kte;
fc44f1
-
fc44f1
-		code = find_keytab_entry(context, kt, srchost, hostname,
fc44f1
-					 &kte, svcnames);
fc44f1
-		if (code) {
fc44f1
-			printerr(0, "ERROR: %s: no usable keytab entry found "
fc44f1
-				 "in keytab %s for connection with host %s\n",
fc44f1
-				 __FUNCTION__, keytabfile, hostname);
fc44f1
-			retval = code;
fc44f1
-			goto out_free_kt;
fc44f1
-		}
fc44f1
-
fc44f1
-		ple = get_ple_by_princ(context, kte.principal);
fc44f1
-		k5_free_kt_entry(context, &kte);
fc44f1
-		if (ple == NULL) {
fc44f1
-			char *pname;
fc44f1
-			if ((krb5_unparse_name(context, kte.principal, &pname))) {
fc44f1
-				pname = NULL;
fc44f1
-			}
fc44f1
-			printerr(0, "ERROR: %s: Could not locate or create "
fc44f1
-				 "ple struct for principal %s for connection "
fc44f1
-				 "with host %s\n",
fc44f1
-				 __FUNCTION__, pname ? pname : "<unparsable>",
fc44f1
-				 hostname);
fc44f1
-			if (pname) k5_free_unparsed_name(context, pname);
fc44f1
-			goto out_free_kt;
fc44f1
-		}
fc44f1
-	}
fc44f1
-	retval = gssd_get_single_krb5_cred(context, kt, ple, 0);
fc44f1
-out_free_kt:
fc44f1
-	krb5_kt_close(context, kt);
fc44f1
-out_free_context:
fc44f1
-	krb5_free_context(context);
fc44f1
-out:
fc44f1
-	krb5_free_string(context, k5err);
fc44f1
-	return retval;
fc44f1
+    return gssd_refresh_krb5_machine_credential_internal(hostname, NULL,
fc44f1
+							 service, srchost);
fc44f1
 }
fc44f1
 
fc44f1
 /*
fc44f1
diff --git a/utils/gssd/krb5_util.h b/utils/gssd/krb5_util.h
fc44f1
index b000b444..2415205a 100644
fc44f1
--- a/utils/gssd/krb5_util.h
fc44f1
+++ b/utils/gssd/krb5_util.h
fc44f1
@@ -9,27 +9,13 @@
fc44f1
 #include "gss_oids.h"
fc44f1
 #endif
fc44f1
 
fc44f1
-/*
fc44f1
- * List of principals from our keytab that we
fc44f1
- * will try to use to obtain credentials
fc44f1
- * (known as a principal list entry (ple))
fc44f1
- */
fc44f1
-struct gssd_k5_kt_princ {
fc44f1
-	struct gssd_k5_kt_princ *next;
fc44f1
-	krb5_principal princ;
fc44f1
-	char *ccname;
fc44f1
-	char *realm;
fc44f1
-	krb5_timestamp endtime;
fc44f1
-};
fc44f1
-
fc44f1
 
fc44f1
 int gssd_setup_krb5_user_gss_ccache(uid_t uid, char *servername,
fc44f1
 				     char *dirname);
fc44f1
 int  gssd_get_krb5_machine_cred_list(char ***list);
fc44f1
 void gssd_free_krb5_machine_cred_list(char **list);
fc44f1
-void gssd_destroy_krb5_machine_creds(void);
fc44f1
+void gssd_destroy_krb5_principals(int destroy_machine_creds);
fc44f1
 int  gssd_refresh_krb5_machine_credential(char *hostname,
fc44f1
-					  struct gssd_k5_kt_princ *ple, 
fc44f1
 					  char *service, char *srchost);
fc44f1
 char *gssd_k5_err_msg(krb5_context context, krb5_error_code code);
fc44f1
 void gssd_k5_get_default_realm(char **def_realm);