Blame SOURCES/nfs-utils-1.3.0-rpcgssd-debug.patch

851484
diff -up nfs-utils-1.3.0/aclocal/libtirpc.m4.orig nfs-utils-1.3.0/aclocal/libtirpc.m4
851484
--- nfs-utils-1.3.0/aclocal/libtirpc.m4.orig	2014-03-25 11:12:07.000000000 -0400
851484
+++ nfs-utils-1.3.0/aclocal/libtirpc.m4	2016-04-15 11:42:49.532156526 -0400
851484
@@ -2,61 +2,61 @@ dnl Checks for TI-RPC library and header
851484
 dnl
851484
 AC_DEFUN([AC_LIBTIRPC], [
851484
 
851484
+  AS_IF(
851484
+    [test "$enable_tirpc" != "no"],
851484
+    [PKG_CHECK_MODULES([TIRPC], [libtirpc],
851484
+                      [LIBTIRPC="${TIRPC_LIBS}"
851484
+                       AM_CPPFLAGS="${AM_CPPFLAGS} ${TIRPC_CFLAGS}"
851484
+                       AC_DEFINE([HAVE_LIBTIRPC], [1],
851484
+                                 [Define to 1 if you have and wish to use libtirpc.])],
851484
+                      [AC_LIBTIRPC_OLD
851484
+                       AS_IF([test "$enable_tirpc" = "yes" -a -z "${LIBTIRPC}"],
851484
+                             [AC_MSG_ERROR([libtirpc not found.])])])])
851484
+
851484
+     AS_IF([test -n "${LIBTIRPC}"],
851484
+           [AC_CHECK_LIB([tirpc], [authgss_free_private_data],
851484
+                         [AC_DEFINE([HAVE_AUTHGSS_FREE_PRIVATE_DATA], [1],
851484
+                                    [Define to 1 if your rpcsec library provides authgss_free_private_data])],,
851484
+                         [${LIBS}])])
851484
+
851484
+     AS_IF([test -n "${LIBTIRPC}"],
851484
+           [AC_CHECK_LIB([tirpc], [libtirpc_set_debug],
851484
+                         [AC_DEFINE([HAVE_LIBTIRPC_SET_DEBUG], [1],
851484
+                                    [Define to 1 if your tirpc library provides libtirpc_set_debug])],,
851484
+                         [${LIBS}])])
851484
+
851484
+  AC_SUBST([AM_CPPFLAGS])
851484
+  AC_SUBST(LIBTIRPC)
851484
+
851484
+])dnl
851484
+
851484
+dnl Old way of checking libtirpc without pkg-config
851484
+dnl This can go away when virtually all libtirpc provide a .pc file
851484
+dnl
851484
+AC_DEFUN([AC_LIBTIRPC_OLD], [
851484
+
851484
   AC_ARG_WITH([tirpcinclude],
851484
               [AC_HELP_STRING([--with-tirpcinclude=DIR],
851484
                               [use TI-RPC headers in DIR])],
851484
               [tirpc_header_dir=$withval],
851484
               [tirpc_header_dir=/usr/include/tirpc])
851484
 
851484
-  dnl if --enable-tirpc was specifed, the following components
851484
-  dnl must be present, and we set up HAVE_ macros for them.
851484
-
851484
-  if test "$enable_tirpc" != "no"; then
851484
-
851484
-    dnl look for the library
851484
-    AC_CHECK_LIB([tirpc], [clnt_tli_create], [:],
851484
-                 [if test "$enable_tirpc" = "yes"; then
851484
-			AC_MSG_ERROR([libtirpc not found.])
851484
-		  else
851484
-			AC_MSG_WARN([libtirpc not found. TIRPC disabled!])
851484
-			enable_tirpc="no"
851484
-		  fi])
851484
-  fi
851484
-
851484
-  if test "$enable_tirpc" != "no"; then
851484
-
851484
-    dnl Check if library contains authgss_free_private_data
851484
-    AC_CHECK_LIB([tirpc], [authgss_free_private_data], [have_free_private_data=yes],
851484
-			[have_free_private_data=no])
851484
-  fi
851484
-
851484
-  if test "$enable_tirpc" != "no"; then
851484
-    dnl also must have the headers installed where we expect
851484
-    dnl look for headers; add -I compiler option if found
851484
-    AC_CHECK_HEADERS([${tirpc_header_dir}/netconfig.h],
851484
-    		      AC_SUBST([AM_CPPFLAGS], ["-I${tirpc_header_dir}"]),
851484
-		      [if test "$enable_tirpc" = "yes"; then
851484
-			 AC_MSG_ERROR([libtirpc headers not found.])
851484
-		       else
851484
-			 AC_MSG_WARN([libtirpc headers not found. TIRPC disabled!])
851484
-			 enable_tirpc="no"
851484
-		       fi])
851484
-
851484
-  fi
851484
-
851484
-  dnl now set $LIBTIRPC accordingly
851484
-  if test "$enable_tirpc" != "no"; then
851484
-    AC_DEFINE([HAVE_LIBTIRPC], 1,
851484
-              [Define to 1 if you have and wish to use libtirpc.])
851484
-    LIBTIRPC="-ltirpc"
851484
-    if test "$have_free_private_data" = "yes"; then
851484
-      AC_DEFINE([HAVE_AUTHGSS_FREE_PRIVATE_DATA], 1,
851484
-	      [Define to 1 if your rpcsec library provides authgss_free_private_data,])
851484
-    fi
851484
-  else
851484
-    LIBTIRPC=""
851484
-  fi
851484
-
851484
-  AC_SUBST(LIBTIRPC)
851484
+  dnl Look for the library
851484
+  AC_CHECK_LIB([tirpc], [clnt_tli_create],
851484
+               [has_libtirpc="yes"],
851484
+               [has_libtirpc="no"])
851484
+
851484
+  dnl Also must have the headers installed where we expect
851484
+  dnl to look for headers; add -I compiler option if found
851484
+  AS_IF([test "$has_libtirpc" = "yes"],
851484
+        [AC_CHECK_HEADERS([${tirpc_header_dir}/netconfig.h],
851484
+                          [AC_SUBST([AM_CPPFLAGS], ["-I${tirpc_header_dir}"])],
851484
+                          [has_libtirpc="no"])])
851484
+
851484
+  dnl Now set $LIBTIRPC accordingly
851484
+  AS_IF([test "$has_libtirpc" = "yes"],
851484
+        [AC_DEFINE([HAVE_LIBTIRPC], [1],
851484
+                   [Define to 1 if you have and wish to use libtirpc.])
851484
+         LIBTIRPC="-ltirpc"])
851484
 
851484
 ])dnl
851484
diff -up nfs-utils-1.3.0/support/include/nfslib.h.orig nfs-utils-1.3.0/support/include/nfslib.h
851484
--- nfs-utils-1.3.0/support/include/nfslib.h.orig	2016-04-15 11:42:13.930460892 -0400
851484
+++ nfs-utils-1.3.0/support/include/nfslib.h	2016-04-15 11:42:38.365938345 -0400
851484
@@ -17,6 +17,7 @@
851484
 #include <sys/socket.h>
851484
 #include <netinet/in.h>
851484
 #include <stdio.h>
851484
+#include <stdbool.h>
851484
 #include <paths.h>
851484
 #include <rpcsvc/nfs_prot.h>
851484
 #include <nfs/nfs.h>
851484
@@ -129,8 +130,8 @@ void			fendrmtabent(FILE *fp);
851484
 void			frewindrmtabent(FILE *fp);
851484
 
851484
 /* mydaemon */
851484
-void mydaemon(int nochdir, int noclose, int *pipefds);
851484
-void release_parent(int *pipefds);
851484
+void daemon_init(bool fg);
851484
+void daemon_ready(void);
851484
 
851484
 /*
851484
  * wildmat borrowed from INN
851484
@@ -182,6 +183,9 @@ size_t  strlcpy(char *, const char *, si
851484
 ssize_t atomicio(ssize_t (*f) (int, void*, size_t),
851484
 		 int, void *, size_t);
851484
 
851484
+#ifdef HAVE_LIBTIRPC_SET_DEBUG
851484
+void  libtirpc_set_debug(char *name, int level, int use_stderr);
851484
+#endif
851484
 
851484
 #define UNUSED(x) UNUSED_ ## x __attribute__((unused))
851484
 
851484
diff -up nfs-utils-1.3.0/support/nfs/mydaemon.c.orig nfs-utils-1.3.0/support/nfs/mydaemon.c
851484
--- nfs-utils-1.3.0/support/nfs/mydaemon.c.orig	2014-03-25 11:12:07.000000000 -0400
851484
+++ nfs-utils-1.3.0/support/nfs/mydaemon.c	2016-04-15 11:42:38.366938365 -0400
851484
@@ -46,56 +46,61 @@
851484
 #include <errno.h>
851484
 #include <unistd.h>
851484
 #include <stdio.h>
851484
+#include <stdbool.h>
851484
 #include <stdlib.h>
851484
 #include <string.h>
851484
 #include <xlog.h>
851484
 
851484
+#include "nfslib.h"
851484
+
851484
+static int pipefds[2] = { -1, -1};
851484
+
851484
 /**
851484
- * mydaemon - daemonize, but have parent wait to exit
851484
- * @nochdir:	skip chdir()'ing the child to / after forking if true
851484
- * @noclose:	skip closing stdin/stdout/stderr if true
851484
- * @pipefds:	pointer to 2 element array of pipefds
851484
+ * daemon_init - initial daemon setup
851484
+ * @fg:		whether to run in the foreground
851484
  *
851484
  * This function is like daemon(), but with our own special sauce to delay
851484
  * the exit of the parent until the child is set up properly. A pipe is created
851484
  * between parent and child. The parent process will wait to exit until the
851484
- * child dies or writes a '1' on the pipe signaling that it started
851484
- * successfully.
851484
+ * child dies or writes an int on the pipe signaling its status.
851484
  */
851484
 void
851484
-mydaemon(int nochdir, int noclose, int *pipefds)
851484
+daemon_init(bool fg)
851484
 {
851484
 	int pid, status, tempfd;
851484
 
851484
+	if (fg)
851484
+		return;
851484
+
851484
 	if (pipe(pipefds) < 0) {
851484
 		xlog_err("mydaemon: pipe() failed: errno %d (%s)\n",
851484
 			 errno, strerror(errno));
851484
-		exit(1);
851484
+		exit(EXIT_FAILURE);
851484
 	}
851484
-	if ((pid = fork ()) < 0) {
851484
+
851484
+	pid = fork();
851484
+	if (pid < 0) {
851484
 		xlog_err("mydaemon: fork() failed: errno %d (%s)\n",
851484
 			 errno, strerror(errno));
851484
-		exit(1);
851484
+		exit(EXIT_FAILURE);
851484
 	}
851484
 
851484
-	if (pid != 0) {
851484
-		/*
851484
-		 * Parent. Wait for status from child.
851484
-		 */
851484
+	if (pid > 0) {
851484
+		/* Parent */
851484
 		close(pipefds[1]);
851484
-		if (read(pipefds[0], &status, 1) != 1)
851484
-			exit(1);
851484
-		exit (0);
851484
+		if (read(pipefds[0], &status, sizeof(status)) != sizeof(status))
851484
+			exit(EXIT_FAILURE);
851484
+		exit(status);
851484
 	}
851484
-	/* Child.	*/
851484
+
851484
+	/* Child */
851484
 	close(pipefds[0]);
851484
 	setsid ();
851484
-	if (nochdir == 0) {
851484
-		if (chdir ("/") == -1) {
851484
-			xlog_err("mydaemon: chdir() failed: errno %d (%s)\n",
851484
-				 errno, strerror(errno));
851484
-			exit(1);
851484
-		}
851484
+
851484
+	if (chdir ("/")) {
851484
+		xlog_err("mydaemon: chdir() failed: errno %d (%s)\n",
851484
+			 errno, strerror(errno));
851484
+		exit(EXIT_FAILURE);
851484
 	}
851484
 
851484
 	while (pipefds[1] <= 2) {
851484
@@ -103,41 +108,39 @@ mydaemon(int nochdir, int noclose, int *
851484
 		if (pipefds[1] < 0) {
851484
 			xlog_err("mydaemon: dup() failed: errno %d (%s)\n",
851484
 				 errno, strerror(errno));
851484
-			exit(1);
851484
+			exit(EXIT_FAILURE);
851484
 		}
851484
 	}
851484
 
851484
-	if (noclose == 0) {
851484
-		tempfd = open("/dev/null", O_RDWR);
851484
-		if (tempfd >= 0) {
851484
-			dup2(tempfd, 0);
851484
-			dup2(tempfd, 1);
851484
-			dup2(tempfd, 2);
851484
-			close(tempfd);
851484
-		} else {
851484
-			xlog_err("mydaemon: can't open /dev/null: errno %d "
851484
-				 "(%s)\n", errno, strerror(errno));
851484
-			exit(1);
851484
-		}
851484
+	tempfd = open("/dev/null", O_RDWR);
851484
+	if (tempfd < 0) {
851484
+		xlog_err("mydaemon: can't open /dev/null: errno %d "
851484
+			 "(%s)\n", errno, strerror(errno));
851484
+		exit(EXIT_FAILURE);
851484
 	}
851484
 
851484
-	return;
851484
+	dup2(tempfd, 0);
851484
+	dup2(tempfd, 1);
851484
+	dup2(tempfd, 2);
851484
+	closelog();
851484
+	dup2(pipefds[1], 3);
851484
+	pipefds[1] = 3;
851484
+	closeall(4);
851484
 }
851484
 
851484
 /**
851484
- * release_parent - tell the parent that it can exit now
851484
- * @pipefds:	pipefd array that was previously passed to mydaemon()
851484
+ * daemon_ready - tell interested parties that the daemon is ready
851484
  *
851484
- * This function tells the parent process of mydaemon() that it's now clear
851484
- * to exit(0).
851484
+ * This function tells e.g. the parent process that the daemon is up
851484
+ * and running.
851484
  */
851484
 void
851484
-release_parent(int *pipefds)
851484
+daemon_ready(void)
851484
 {
851484
-	int status;
851484
+	int status = 0;
851484
 
851484
 	if (pipefds[1] > 0) {
851484
-		if (write(pipefds[1], &status, 1) != 1) {
851484
+		if (write(pipefds[1], &status, sizeof(status)) != sizeof(status)) {
851484
 			xlog_err("WARN: writing to parent pipe failed: errno "
851484
 				 "%d (%s)\n", errno, strerror(errno));
851484
 		}
851484
diff -up nfs-utils-1.3.0/support/nfs/svc_create.c.orig nfs-utils-1.3.0/support/nfs/svc_create.c
851484
--- nfs-utils-1.3.0/support/nfs/svc_create.c.orig	2016-04-15 11:42:13.931460911 -0400
851484
+++ nfs-utils-1.3.0/support/nfs/svc_create.c	2016-04-15 11:42:38.366938365 -0400
851484
@@ -133,7 +133,7 @@ svc_create_bindaddr(struct netconfig *nc
851484
 		hint.ai_family = AF_INET6;
851484
 #endif	/* IPV6_SUPPORTED */
851484
 	else {
851484
-		xlog(D_GENERAL, "Unrecognized bind address family: %s",
851484
+		xlog(L_ERROR, "Unrecognized bind address family: %s",
851484
 			nconf->nc_protofmly);
851484
 		return NULL;
851484
 	}
851484
@@ -143,7 +143,7 @@ svc_create_bindaddr(struct netconfig *nc
851484
 	else if (strcmp(nconf->nc_proto, NC_TCP) == 0)
851484
 		hint.ai_protocol = (int)IPPROTO_TCP;
851484
 	else {
851484
-		xlog(D_GENERAL, "Unrecognized bind address protocol: %s",
851484
+		xlog(L_ERROR, "Unrecognized bind address protocol: %s",
851484
 			nconf->nc_proto);
851484
 		return NULL;
851484
 	}
851484
@@ -275,7 +275,7 @@ svc_create_nconf_rand_port(const char *n
851484
 	xprt = svc_tli_create(RPC_ANYFD, nconf, &bindaddr, 0, 0);
851484
 	freeaddrinfo(ai);
851484
 	if (xprt == NULL) {
851484
-		xlog(D_GENERAL, "Failed to create listener xprt "
851484
+		xlog(L_ERROR, "Failed to create listener xprt "
851484
 			"(%s, %u, %s)", name, version, nconf->nc_netid);
851484
 		return 0;
851484
 	}
851484
@@ -286,10 +286,12 @@ svc_create_nconf_rand_port(const char *n
851484
 		return 0;
851484
 	}
851484
 
851484
+	rpc_createerr.cf_stat = rpc_createerr.cf_error.re_errno = 0;
851484
 	if (!svc_reg(xprt, program, version, dispatch, nconf)) {
851484
 		/* svc_reg(3) destroys @xprt in this case */
851484
-		xlog(D_GENERAL, "Failed to register (%s, %u, %s)",
851484
-				name, version, nconf->nc_netid);
851484
+		xlog(L_ERROR, "Failed to register (%s, %u, %s): %s",
851484
+				name, version, nconf->nc_netid, 
851484
+				clnt_spcreateerror("svc_reg() err"));
851484
 		return 0;
851484
 	}
851484
 
851484
diff -up nfs-utils-1.3.0/support/nfs/svc_socket.c.orig nfs-utils-1.3.0/support/nfs/svc_socket.c
851484
--- nfs-utils-1.3.0/support/nfs/svc_socket.c.orig	2016-04-15 11:42:13.931460911 -0400
851484
+++ nfs-utils-1.3.0/support/nfs/svc_socket.c	2016-04-15 11:42:38.367938385 -0400
851484
@@ -24,6 +24,7 @@
851484
 #include <sys/socket.h>
851484
 #include <sys/fcntl.h>
851484
 #include <errno.h>
851484
+#include "xlog.h"
851484
 
851484
 #ifdef _LIBC
851484
 # include <libintl.h>
851484
@@ -90,9 +91,9 @@ svcsock_nonblock(int sock)
851484
 	 * connection.
851484
 	 */
851484
 	if ((flags = fcntl(sock, F_GETFL)) < 0)
851484
-		perror(_("svc_socket: can't get socket flags"));
851484
+		xlog(L_ERROR, "svc_socket: can't get socket flags: %m");
851484
 	else if (fcntl(sock, F_SETFL, flags|O_NONBLOCK) < 0)
851484
-		perror(_("svc_socket: can't set socket flags"));
851484
+		xlog(L_ERROR, "svc_socket: can't set socket flags: %m");
851484
 	else
851484
 		return sock;
851484
 
851484
@@ -110,7 +111,7 @@ svc_socket (u_long number, int type, int
851484
 
851484
   if ((sock = __socket (AF_INET, type, protocol)) < 0)
851484
     {
851484
-      perror (_("svc_socket: socket creation problem"));
851484
+      xlog(L_ERROR, "svc_socket: socket creation problem: %m");
851484
       return sock;
851484
     }
851484
 
851484
@@ -121,7 +122,7 @@ svc_socket (u_long number, int type, int
851484
 			sizeof (ret));
851484
       if (ret < 0)
851484
 	{
851484
-	  perror (_("svc_socket: socket reuse problem"));
851484
+	  xlog(L_ERROR, "svc_socket: socket reuse problem: %m");
851484
 	  return ret;
851484
 	}
851484
     }
851484
@@ -132,7 +133,7 @@ svc_socket (u_long number, int type, int
851484
 
851484
   if (bind(sock, (struct sockaddr *) &addr, len) < 0)
851484
     {
851484
-      perror (_("svc_socket: bind problem"));
851484
+      xlog(L_ERROR, "svc_socket: bind problem: %m");
851484
       (void) __close(sock);
851484
       sock = -1;
851484
     }
851484
diff -up nfs-utils-1.3.0/utils/gssd/context_heimdal.c.orig nfs-utils-1.3.0/utils/gssd/context_heimdal.c
851484
--- nfs-utils-1.3.0/utils/gssd/context_heimdal.c.orig	2014-03-25 11:12:07.000000000 -0400
851484
+++ nfs-utils-1.3.0/utils/gssd/context_heimdal.c	2016-04-15 11:42:38.367938385 -0400
851484
@@ -260,7 +260,7 @@ serialize_krb5_ctx(gss_ctx_id_t *_ctx, g
851484
 	if (write_heimdal_seq_key(&p, end, ctx)) goto out_err;
851484
 
851484
 	buf->length = p - (char *)buf->value;
851484
-	printerr(2, "serialize_krb5_ctx: returning buffer "
851484
+	printerr(4, "serialize_krb5_ctx: returning buffer "
851484
 		    "with %d bytes\n", buf->length);
851484
 
851484
 	return 0;
851484
diff -up nfs-utils-1.3.0/utils/gssd/context_lucid.c.orig nfs-utils-1.3.0/utils/gssd/context_lucid.c
851484
--- nfs-utils-1.3.0/utils/gssd/context_lucid.c.orig	2014-03-25 11:12:07.000000000 -0400
851484
+++ nfs-utils-1.3.0/utils/gssd/context_lucid.c	2016-04-15 11:42:38.367938385 -0400
851484
@@ -206,7 +206,7 @@ prepare_krb5_rfc4121_buffer(gss_krb5_luc
851484
 	if (WRITE_BYTES(&p, end, lctx->send_seq)) goto out_err;
851484
 
851484
 	/* Protocol 0 here implies DES3 or RC4 */
851484
-	printerr(2, "%s: protocol %d\n", __FUNCTION__, lctx->protocol);
851484
+	printerr(4, "%s: protocol %d\n", __FUNCTION__, lctx->protocol);
851484
 	if (lctx->protocol == 0) {
851484
 		enctype = lctx->rfc1964_kd.ctx_key.type;
851484
 		keysize = lctx->rfc1964_kd.ctx_key.length;
851484
@@ -219,7 +219,7 @@ prepare_krb5_rfc4121_buffer(gss_krb5_luc
851484
 			keysize = lctx->cfx_kd.ctx_key.length;
851484
 		}
851484
 	}
851484
-	printerr(2, "%s: serializing key with enctype %d and size %d\n",
851484
+	printerr(4, "%s: serializing key with enctype %d and size %d\n",
851484
 		 __FUNCTION__, enctype, keysize);
851484
 
851484
 	if (WRITE_BYTES(&p, end, enctype)) goto out_err;
851484
@@ -265,7 +265,7 @@ serialize_krb5_ctx(gss_ctx_id_t *ctx, gs
851484
 	gss_krb5_lucid_context_v1_t *lctx = 0;
851484
 	int retcode = 0;
851484
 
851484
-	printerr(2, "DEBUG: %s: lucid version!\n", __FUNCTION__);
851484
+	printerr(4, "DEBUG: %s: lucid version!\n", __FUNCTION__);
851484
 	maj_stat = gss_export_lucid_sec_context(&min_stat, ctx,
851484
 						1, &return_ctx);
851484
 	if (maj_stat != GSS_S_COMPLETE) {
851484
diff -up nfs-utils-1.3.0/utils/gssd/gssd.c.orig nfs-utils-1.3.0/utils/gssd/gssd.c
851484
--- nfs-utils-1.3.0/utils/gssd/gssd.c.orig	2016-04-15 11:42:13.917460638 -0400
851484
+++ nfs-utils-1.3.0/utils/gssd/gssd.c	2016-04-15 11:42:38.369938424 -0400
851484
@@ -1,7 +1,7 @@
851484
 /*
851484
   gssd.c
851484
 
851484
-  Copyright (c) 2000 The Regents of the University of Michigan.
851484
+  Copyright (c) 2000, 2004 The Regents of the University of Michigan.
851484
   All rights reserved.
851484
 
851484
   Copyright (c) 2000 Dug Song <dugsong@UMICH.EDU>.
851484
@@ -40,9 +40,18 @@
851484
 #include <config.h>
851484
 #endif	/* HAVE_CONFIG_H */
851484
 
851484
+#ifndef _GNU_SOURCE
851484
+#define _GNU_SOURCE
851484
+#endif
851484
+
851484
 #include <sys/param.h>
851484
 #include <sys/socket.h>
851484
+#include <sys/time.h>
851484
+#include <sys/resource.h>
851484
+#include <sys/inotify.h>
851484
 #include <rpc/rpc.h>
851484
+#include <netinet/in.h>
851484
+#include <arpa/inet.h>
851484
 
851484
 #include <unistd.h>
851484
 #include <err.h>
851484
@@ -51,41 +60,684 @@
851484
 #include <stdlib.h>
851484
 #include <string.h>
851484
 #include <signal.h>
851484
+#include <memory.h>
851484
+#include <fcntl.h>
851484
+#include <dirent.h>
851484
+#include <netdb.h>
851484
+#include <event.h>
851484
+
851484
 #include "gssd.h"
851484
 #include "err_util.h"
851484
 #include "gss_util.h"
851484
 #include "krb5_util.h"
851484
 #include "nfslib.h"
851484
 
851484
-char pipefs_dir[PATH_MAX] = GSSD_PIPEFS_DIR;
851484
-char keytabfile[PATH_MAX] = GSSD_DEFAULT_KEYTAB_FILE;
851484
-char ccachedir[PATH_MAX] = GSSD_DEFAULT_CRED_DIR ":" GSSD_USER_CRED_DIR;
851484
-char *ccachesearch[GSSD_MAX_CCACHE_SEARCH + 1];
851484
+static char *pipefs_path = GSSD_PIPEFS_DIR;
851484
+static DIR *pipefs_dir;
851484
+static int pipefs_fd;
851484
+static int inotify_fd;
851484
+struct event inotify_ev;
851484
+
851484
+char *keytabfile = GSSD_DEFAULT_KEYTAB_FILE;
851484
+char **ccachesearch;
851484
 int  use_memcache = 0;
851484
 int  root_uses_machine_creds = 1;
851484
 unsigned int  context_timeout = 0;
851484
 unsigned int  rpc_timeout = 5;
851484
 char *preferred_realm = NULL;
851484
-int pipefds[2] = { -1, -1 };
851484
+/* Avoid DNS reverse lookups on server names */
851484
+static bool avoid_dns = true;
851484
+
851484
+
851484
+TAILQ_HEAD(topdir_list_head, topdir) topdir_list;
851484
+
851484
+struct topdir {
851484
+	TAILQ_ENTRY(topdir) list;
851484
+	TAILQ_HEAD(clnt_list_head, clnt_info) clnt_list;
851484
+	int wd;
851484
+	char name[];
851484
+};
851484
+
851484
+/*
851484
+ * topdir_list:
851484
+ *	linked list of struct topdir with basic data about a topdir.
851484
+ *
851484
+ * clnt_list:
851484
+ *      linked list of struct clnt_info with basic data about a clntXXX dir,
851484
+ *      one per topdir.
851484
+ *
851484
+ * Directory structure: created by the kernel
851484
+ *      {rpc_pipefs}/{topdir}/clntXX      : one per rpc_clnt struct in the kernel
851484
+ *      {rpc_pipefs}/{topdir}/clntXX/krb5 : read uid for which kernel wants
851484
+ *					    a context, write the resulting context
851484
+ *      {rpc_pipefs}/{topdir}/clntXX/info : stores info such as server name
851484
+ *      {rpc_pipefs}/{topdir}/clntXX/gssd : pipe for all gss mechanisms using
851484
+ *					    a text-based string of parameters
851484
+ *
851484
+ * Algorithm:
851484
+ *      Poll all {rpc_pipefs}/{topdir}/clntXX/YYYY files.  When data is ready,
851484
+ *      read and process; performs rpcsec_gss context initialization protocol to
851484
+ *      get a cred for that user.  Writes result to corresponding krb5 file
851484
+ *      in a form the kernel code will understand.
851484
+ *      In addition, we make sure we are notified whenever anything is
851484
+ *      created or destroyed in {rpc_pipefs} or in any of the clntXX directories,
851484
+ *      and rescan the whole {rpc_pipefs} when this happens.
851484
+ */
851484
+
851484
+/*
851484
+ * convert a presentation address string to a sockaddr_storage struct. Returns
851484
+ * true on success or false on failure.
851484
+ *
851484
+ * Note that we do not populate the sin6_scope_id field here for IPv6 addrs.
851484
+ * gssd nececessarily relies on hostname resolution and DNS AAAA records
851484
+ * do not generally contain scope-id's. This means that GSSAPI auth really
851484
+ * can't work with IPv6 link-local addresses.
851484
+ *
851484
+ * We *could* consider changing this if we did something like adopt the
851484
+ * Microsoft "standard" of using the ipv6-literal.net domainname, but it's
851484
+ * not really feasible at present.
851484
+ */
851484
+static bool
851484
+gssd_addrstr_to_sockaddr(struct sockaddr *sa, const char *node, const char *port)
851484
+{
851484
+	int rc;
851484
+	struct addrinfo *res;
851484
+	struct addrinfo hints = { .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV };
851484
+
851484
+#ifndef IPV6_SUPPORTED
851484
+	hints.ai_family = AF_INET;
851484
+#endif /* IPV6_SUPPORTED */
851484
+
851484
+	rc = getaddrinfo(node, port, &hints, &res;;
851484
+	if (rc) {
851484
+		printerr(0, "ERROR: unable to convert %s|%s to sockaddr: %s\n",
851484
+			 node, port,
851484
+			 rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc));
851484
+		return false;
851484
+	}
851484
+
851484
+#ifdef IPV6_SUPPORTED
851484
+	/*
851484
+	 * getnameinfo ignores the scopeid. If the address turns out to have
851484
+	 * a non-zero scopeid, we can't use it -- the resolved host might be
851484
+	 * completely different from the one intended.
851484
+	 */
851484
+	if (res->ai_addr->sa_family == AF_INET6) {
851484
+		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)res->ai_addr;
851484
+		if (sin6->sin6_scope_id) {
851484
+			printerr(0, "ERROR: address %s has non-zero "
851484
+				    "sin6_scope_id!\n", node);
851484
+			freeaddrinfo(res);
851484
+			return false;
851484
+		}
851484
+	}
851484
+#endif /* IPV6_SUPPORTED */
851484
+
851484
+	memcpy(sa, res->ai_addr, res->ai_addrlen);
851484
+	freeaddrinfo(res);
851484
+	return true;
851484
+}
851484
+
851484
+/*
851484
+ * convert a sockaddr to a hostname
851484
+ */
851484
+static char *
851484
+gssd_get_servername(const char *name, const struct sockaddr *sa, const char *addr)
851484
+{
851484
+	socklen_t		addrlen;
851484
+	int			err;
851484
+	char			hbuf[NI_MAXHOST];
851484
+	unsigned char		buf[sizeof(struct in6_addr)];
851484
+
851484
+	while (avoid_dns) {
851484
+		/*
851484
+		 * Determine if this is a server name, or an IP address.
851484
+		 * If it is an IP address, do the DNS lookup otherwise
851484
+		 * skip the DNS lookup.
851484
+		 */
851484
+		if (strchr(name, '.') == NULL)
851484
+			break; /* local name */
851484
+		else if (inet_pton(AF_INET, name, buf) == 1)
851484
+			break; /* IPv4 address */
851484
+		else if (inet_pton(AF_INET6, name, buf) == 1)
851484
+			break; /* IPv6 addrss */
851484
+
851484
+		return strdup(name);
851484
+	}
851484
+
851484
+	switch (sa->sa_family) {
851484
+	case AF_INET:
851484
+		addrlen = sizeof(struct sockaddr_in);
851484
+		break;
851484
+#ifdef IPV6_SUPPORTED
851484
+	case AF_INET6:
851484
+		addrlen = sizeof(struct sockaddr_in6);
851484
+		break;
851484
+#endif /* IPV6_SUPPORTED */
851484
+	default:
851484
+		printerr(0, "ERROR: unrecognized addr family %d\n",
851484
+			 sa->sa_family);
851484
+		return NULL;
851484
+	}
851484
+
851484
+	err = getnameinfo(sa, addrlen, hbuf, sizeof(hbuf), NULL, 0,
851484
+			  NI_NAMEREQD);
851484
+	if (err) {
851484
+		printerr(0, "ERROR: unable to resolve %s to hostname: %s\n",
851484
+			 addr, err == EAI_SYSTEM ? strerror(errno) :
851484
+						   gai_strerror(err));
851484
+		return NULL;
851484
+	}
851484
+
851484
+	return strdup(hbuf);
851484
+}
851484
+
851484
+static void
851484
+gssd_read_service_info(int dirfd, struct clnt_info *clp)
851484
+{
851484
+	int fd;
851484
+	FILE *info = NULL;
851484
+	int numfields;
851484
+	char *server = NULL;
851484
+	char *service = NULL;
851484
+	int program;
851484
+	int version;
851484
+	char *address = NULL;
851484
+	char *protoname = NULL;
851484
+	char *port = NULL;
851484
+	char *servername = NULL;
851484
+
851484
+	fd = openat(dirfd, "info", O_RDONLY);
851484
+	if (fd < 0) {
851484
+		printerr(0, "ERROR: can't open %s/info: %s\n",
851484
+			 clp->relpath, strerror(errno));
851484
+		goto fail;
851484
+	}
851484
+
851484
+	info = fdopen(fd, "r");
851484
+	if (!info) {
851484
+		printerr(0, "ERROR: can't fdopen %s/info: %s\n",
851484
+			 clp->relpath, strerror(errno));
851484
+		close(fd);
851484
+		goto fail;
851484
+	}
851484
+
851484
+	/*
851484
+	 * Some history:
851484
+	 *
851484
+	 * The first three lines were added with rpc_pipefs in 2003-01-13.
851484
+	 * (commit af2f003391786fb632889c02142c941b212ba4ff)
851484
+	 * 
851484
+	 * The 'protocol' line was added in 2003-06-11.
851484
+	 * (commit 9bd741ae48785d0c0e75cf906ff66f893d600c2d)
851484
+	 *
851484
+	 * The 'port' line was added in 2007-09-26.
851484
+	 * (commit bf19aacecbeebccb2c3d150a8bd9416b7dba81fe)
851484
+	 */
851484
+	numfields = fscanf(info,
851484
+			   "RPC server: %ms\n"
851484
+			   "service: %ms (%d) version %d\n"
851484
+			   "address: %ms\n"
851484
+			   "protocol: %ms\n"
851484
+			   "port: %ms\n",
851484
+			   &server,
851484
+			   &service, &program, &version,
851484
+			   &address,
851484
+			   &protoname,
851484
+			   &port);
851484
+
851484
+
851484
+	switch (numfields) {
851484
+	case 5:
851484
+		protoname = strdup("tcp");
851484
+		if (!protoname)
851484
+			goto fail;
851484
+		/* fall through */
851484
+	case 6:
851484
+		/* fall through */
851484
+	case 7:
851484
+		break;
851484
+	default:
851484
+		goto fail;
851484
+	}
851484
+
851484
+	if (!gssd_addrstr_to_sockaddr((struct sockaddr *)&clp->addr,
851484
+				 address, port ? port : ""))
851484
+		goto fail;
851484
+
851484
+	servername = gssd_get_servername(server, (struct sockaddr *)&clp->addr, address);
851484
+	if (!servername)
851484
+		goto fail;
851484
+
851484
+	if (asprintf(&clp->servicename, "%s@%s", service, servername) < 0)
851484
+		goto fail;
851484
+
851484
+	clp->servername = servername;
851484
+	clp->prog = program;
851484
+	clp->vers = version;
851484
+	clp->protocol = protoname;
851484
+
851484
+	goto out;
851484
+
851484
+fail:
851484
+	printerr(0, "ERROR: failed to parse %s/info\n", clp->relpath);
851484
+	free(servername);
851484
+	free(protoname);
851484
+	clp->servicename = NULL;
851484
+	clp->servername = NULL;
851484
+	clp->prog = 0;
851484
+	clp->vers = 0;
851484
+	clp->protocol = NULL;
851484
+out:
851484
+	if (info)
851484
+		fclose(info);
851484
+
851484
+	free(server);
851484
+	free(service);
851484
+	free(address);
851484
+	free(port);
851484
+}
851484
+
851484
+static void
851484
+gssd_destroy_client(struct clnt_info *clp)
851484
+{
851484
+	if (clp->krb5_fd >= 0) {
851484
+		close(clp->krb5_fd);
851484
+		event_del(&clp->krb5_ev);
851484
+	}
851484
+
851484
+	if (clp->gssd_fd >= 0) {
851484
+		close(clp->gssd_fd);
851484
+		event_del(&clp->gssd_ev);
851484
+	}
851484
+
851484
+	inotify_rm_watch(inotify_fd, clp->wd);
851484
+	free(clp->relpath);
851484
+	free(clp->servicename);
851484
+	free(clp->servername);
851484
+	free(clp->protocol);
851484
+	free(clp);
851484
+}
851484
+
851484
+static void gssd_scan(void);
851484
+
851484
+static void
851484
+gssd_clnt_gssd_cb(int UNUSED(fd), short UNUSED(which), void *data)
851484
+{
851484
+	struct clnt_info *clp = data;
851484
+
851484
+	handle_gssd_upcall(clp);
851484
+}
851484
+
851484
+static void
851484
+gssd_clnt_krb5_cb(int UNUSED(fd), short UNUSED(which), void *data)
851484
+{
851484
+	struct clnt_info *clp = data;
851484
+
851484
+	handle_krb5_upcall(clp);
851484
+}
851484
+
851484
+static struct clnt_info *
851484
+gssd_get_clnt(struct topdir *tdi, const char *name)
851484
+{
851484
+	struct clnt_info *clp;
851484
+
851484
+	TAILQ_FOREACH(clp, &tdi->clnt_list, list)
851484
+		if (!strcmp(clp->name, name))
851484
+			return clp;
851484
+
851484
+	clp = calloc(1, sizeof(struct clnt_info));
851484
+	if (!clp) {
851484
+		printerr(0, "ERROR: can't malloc clnt_info: %s\n",
851484
+			 strerror(errno));
851484
+		return NULL;
851484
+	}
851484
+
851484
+	if (asprintf(&clp->relpath, "%s/%s", tdi->name, name) < 0) {
851484
+		clp->relpath = NULL;
851484
+		goto out;
851484
+	}
851484
+
851484
+	clp->wd = inotify_add_watch(inotify_fd, clp->relpath, IN_CREATE | IN_DELETE);
851484
+	if (clp->wd < 0) {
851484
+		if (errno != ENOENT)
851484
+			printerr(0, "ERROR: inotify_add_watch failed for %s: %s\n",
851484
+			 	clp->relpath, strerror(errno));
851484
+		goto out;
851484
+	}
851484
+
851484
+	clp->name = clp->relpath + strlen(tdi->name) + 1;
851484
+	clp->krb5_fd = -1;
851484
+	clp->gssd_fd = -1;
851484
+
851484
+	TAILQ_INSERT_HEAD(&tdi->clnt_list, clp, list);
851484
+	return clp;
851484
+
851484
+out:
851484
+	free(clp->relpath);
851484
+	free(clp);
851484
+	return NULL;
851484
+}
851484
+
851484
+static int
851484
+gssd_scan_clnt(struct clnt_info *clp)
851484
+{
851484
+	int clntfd;
851484
+	bool gssd_was_closed;
851484
+	bool krb5_was_closed;
851484
+
851484
+	gssd_was_closed = clp->gssd_fd < 0 ? true : false;
851484
+	krb5_was_closed = clp->krb5_fd < 0 ? true : false;
851484
+
851484
+	clntfd = openat(pipefs_fd, clp->relpath, O_RDONLY);
851484
+	if (clntfd < 0) {
851484
+		printerr(0, "ERROR: can't openat %s: %s\n",
851484
+			 clp->relpath, strerror(errno));
851484
+		return -1;
851484
+	}
851484
+
851484
+	if (clp->gssd_fd == -1)
851484
+		clp->gssd_fd = openat(clntfd, "gssd", O_RDWR | O_NONBLOCK);
851484
+
851484
+	if (clp->gssd_fd == -1 && clp->krb5_fd == -1)
851484
+		clp->krb5_fd = openat(clntfd, "krb5", O_RDWR | O_NONBLOCK);
851484
+
851484
+	if (gssd_was_closed && clp->gssd_fd >= 0) {
851484
+		event_set(&clp->gssd_ev, clp->gssd_fd, EV_READ | EV_PERSIST,
851484
+			  gssd_clnt_gssd_cb, clp);
851484
+		event_add(&clp->gssd_ev, NULL);
851484
+	}
851484
+
851484
+	if (krb5_was_closed && clp->krb5_fd >= 0) {
851484
+		event_set(&clp->krb5_ev, clp->krb5_fd, EV_READ | EV_PERSIST,
851484
+			  gssd_clnt_krb5_cb, clp);
851484
+		event_add(&clp->krb5_ev, NULL);
851484
+	}
851484
+
851484
+	if (clp->krb5_fd == -1 && clp->gssd_fd == -1)
851484
+		/* not fatal, files might appear later */
851484
+		goto out;
851484
+
851484
+	if (clp->prog == 0)
851484
+		gssd_read_service_info(clntfd, clp);
851484
+
851484
+out:
851484
+	close(clntfd);
851484
+	clp->scanned = true;
851484
+	return 0;
851484
+}
851484
+
851484
+static int
851484
+gssd_create_clnt(struct topdir *tdi, const char *name)
851484
+{
851484
+	struct clnt_info *clp;
851484
+
851484
+	clp = gssd_get_clnt(tdi, name);
851484
+	if (!clp)
851484
+		return -1;
851484
+
851484
+	return gssd_scan_clnt(clp);
851484
+}
851484
 
851484
-void
851484
+static struct topdir *
851484
+gssd_get_topdir(const char *name)
851484
+{
851484
+	struct topdir *tdi;
851484
+
851484
+	TAILQ_FOREACH(tdi, &topdir_list, list)
851484
+		if (!strcmp(tdi->name, name))
851484
+			return tdi;
851484
+
851484
+	tdi = malloc(sizeof(*tdi) + strlen(name) + 1);
851484
+	if (!tdi) {
851484
+		printerr(0, "ERROR: Couldn't allocate struct topdir\n");
851484
+		return NULL;
851484
+	}
851484
+
851484
+	tdi->wd = inotify_add_watch(inotify_fd, name, IN_CREATE);
851484
+	if (tdi->wd < 0) {
851484
+		printerr(0, "ERROR: inotify_add_watch failed for top dir %s: %s\n",
851484
+			 tdi->name, strerror(errno));
851484
+		free(tdi);
851484
+		return NULL;
851484
+	}
851484
+
851484
+	strcpy(tdi->name, name);
851484
+	TAILQ_INIT(&tdi->clnt_list);
851484
+
851484
+	TAILQ_INSERT_HEAD(&topdir_list, tdi, list);
851484
+	return tdi;
851484
+}
851484
+
851484
+static void
851484
+gssd_scan_topdir(const char *name)
851484
+{
851484
+	struct topdir *tdi;
851484
+	int dfd;
851484
+	DIR *dir;
851484
+	struct clnt_info *clp;
851484
+	struct dirent *d;
851484
+
851484
+	tdi = gssd_get_topdir(name);
851484
+	if (!tdi)
851484
+		return;
851484
+
851484
+	dfd = openat(pipefs_fd, tdi->name, O_RDONLY);
851484
+	if (dfd < 0) {
851484
+		printerr(0, "ERROR: can't openat %s: %s\n",
851484
+			 tdi->name, strerror(errno));
851484
+		return;
851484
+	}
851484
+
851484
+	dir = fdopendir(dfd);
851484
+	if (!dir) {
851484
+		printerr(0, "ERROR: can't fdopendir %s: %s\n",
851484
+			 tdi->name, strerror(errno));
851484
+		return;
851484
+	}
851484
+
851484
+	TAILQ_FOREACH(clp, &tdi->clnt_list, list)
851484
+		clp->scanned = false;
851484
+
851484
+	while ((d = readdir(dir))) {
851484
+		if (d->d_type != DT_DIR)
851484
+			continue;
851484
+
851484
+		if (strncmp(d->d_name, "clnt", strlen("clnt")))
851484
+			continue;
851484
+
851484
+		gssd_create_clnt(tdi, d->d_name);
851484
+	}
851484
+
851484
+	closedir(dir);
851484
+
851484
+	TAILQ_FOREACH(clp, &tdi->clnt_list, list) {
851484
+		void *saveprev;
851484
+
851484
+		if (clp->scanned)
851484
+			continue;
851484
+
851484
+		printerr(3, "destroying client %s\n", clp->relpath);
851484
+		saveprev = clp->list.tqe_prev;
851484
+		TAILQ_REMOVE(&tdi->clnt_list, clp, list);
851484
+		gssd_destroy_client(clp);
851484
+		clp = saveprev;
851484
+	}
851484
+}
851484
+
851484
+static void
851484
+gssd_scan(void)
851484
+{
851484
+	struct dirent *d;
851484
+
851484
+	printerr(3, "doing a full rescan\n");
851484
+	rewinddir(pipefs_dir);
851484
+
851484
+	while ((d = readdir(pipefs_dir))) {
851484
+		if (d->d_type != DT_DIR)
851484
+			continue;
851484
+
851484
+		if (d->d_name[0] == '.')
851484
+			continue;
851484
+
851484
+		gssd_scan_topdir(d->d_name);
851484
+	}
851484
+
851484
+	if (TAILQ_EMPTY(&topdir_list)) {
851484
+		printerr(0, "ERROR: the rpc_pipefs directory is empty!\n");
851484
+		exit(EXIT_FAILURE);
851484
+	}
851484
+}
851484
+
851484
+static void
851484
+gssd_scan_cb(int UNUSED(fd), short UNUSED(which), void *UNUSED(data))
851484
+{
851484
+	gssd_scan();
851484
+}
851484
+
851484
+static bool
851484
+gssd_inotify_topdir(struct topdir *tdi, const struct inotify_event *ev)
851484
+{
851484
+	printerr(5, "inotify event for topdir (%s) - "
851484
+		 "ev->wd (%d) ev->name (%s) ev->mask (0x%08x)\n",
851484
+		 tdi->name, ev->wd, ev->len > 0 ? ev->name : "", ev->mask);
851484
+
851484
+	if (ev->mask & IN_IGNORED) {
851484
+		printerr(0, "ERROR: topdir disappeared!\n");
851484
+		return false;
851484
+	}
851484
+
851484
+	if (ev->len == 0)
851484
+		return false;
851484
+
851484
+	if (ev->mask & IN_CREATE) {
851484
+		if (!(ev->mask & IN_ISDIR))
851484
+			return true;
851484
+
851484
+		if (strncmp(ev->name, "clnt", strlen("clnt")))
851484
+			return true;
851484
+
851484
+		if (gssd_create_clnt(tdi, ev->name))
851484
+			return false;
851484
+
851484
+		return true;
851484
+	} 
851484
+
851484
+	return false;
851484
+}
851484
+
851484
+static bool
851484
+gssd_inotify_clnt(struct topdir *tdi, struct clnt_info *clp, const struct inotify_event *ev)
851484
+{
851484
+	printerr(5, "inotify event for clntdir (%s) - "
851484
+		 "ev->wd (%d) ev->name (%s) ev->mask (0x%08x)\n",
851484
+		 clp->relpath, ev->wd, ev->len > 0 ? ev->name : "", ev->mask);
851484
+
851484
+	if (ev->mask & IN_IGNORED) {
851484
+		TAILQ_REMOVE(&tdi->clnt_list, clp, list);
851484
+		gssd_destroy_client(clp);
851484
+		return true;
851484
+	}
851484
+
851484
+	if (ev->len == 0)
851484
+		return false;
851484
+
851484
+	if (ev->mask & IN_CREATE) {
851484
+		if (!strcmp(ev->name, "gssd") ||
851484
+		    !strcmp(ev->name, "krb5") ||
851484
+		    !strcmp(ev->name, "info"))
851484
+			if (gssd_scan_clnt(clp))
851484
+				return false;
851484
+
851484
+		return true;
851484
+
851484
+	} else if (ev->mask & IN_DELETE) {
851484
+		if (!strcmp(ev->name, "gssd") && clp->gssd_fd >= 0) {
851484
+			close(clp->gssd_fd);
851484
+			event_del(&clp->gssd_ev);
851484
+			clp->gssd_fd = -1;
851484
+
851484
+		} else if (!strcmp(ev->name, "krb5") && clp->krb5_fd >= 0) {
851484
+			close(clp->krb5_fd);
851484
+			event_del(&clp->krb5_ev);
851484
+			clp->krb5_fd = -1;
851484
+		}
851484
+
851484
+		return true;
851484
+	}
851484
+
851484
+	return false;
851484
+}
851484
+
851484
+static void
851484
+gssd_inotify_cb(int ifd, short UNUSED(which), void *UNUSED(data))
851484
+{
851484
+	bool rescan = false;
851484
+	struct topdir *tdi;
851484
+	struct clnt_info *clp;
851484
+
851484
+	while (true) {
851484
+		char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
851484
+		const struct inotify_event *ev;
851484
+		ssize_t len;
851484
+		char *ptr;
851484
+
851484
+		len = read(ifd, buf, sizeof(buf));
851484
+		if (len == -1 && errno == EINTR)
851484
+			continue;
851484
+
851484
+		if (len <= 0)
851484
+			break;
851484
+
851484
+		for (ptr = buf; ptr < buf + len;
851484
+		     ptr += sizeof(struct inotify_event) + ev->len) {
851484
+			ev = (const struct inotify_event *)ptr;
851484
+
851484
+			if (ev->mask & IN_Q_OVERFLOW) {
851484
+				printerr(0, "ERROR: inotify queue overflow\n");
851484
+				rescan = true;
851484
+				break;
851484
+			}
851484
+
851484
+			TAILQ_FOREACH(tdi, &topdir_list, list) {
851484
+				if (tdi->wd == ev->wd) {
851484
+					if (!gssd_inotify_topdir(tdi, ev))
851484
+						rescan = true;
851484
+					goto found;
851484
+				}
851484
+
851484
+				TAILQ_FOREACH(clp, &tdi->clnt_list, list) {
851484
+					if (clp->wd == ev->wd) {
851484
+						if (!gssd_inotify_clnt(tdi, clp, ev))
851484
+							rescan = true;
851484
+						goto found;
851484
+					}
851484
+				}
851484
+			}
851484
+
851484
+found:
851484
+			if (!tdi) {
851484
+				printerr(5, "inotify event for unknown wd!!! - "
851484
+					 "ev->wd (%d) ev->name (%s) ev->mask (0x%08x)\n",
851484
+					 ev->wd, ev->len > 0 ? ev->name : "", ev->mask);
851484
+				rescan = true;
851484
+			}
851484
+		}
851484
+	}
851484
+
851484
+	if (rescan)
851484
+		gssd_scan();
851484
+}
851484
+
851484
+static void
851484
 sig_die(int signal)
851484
 {
851484
-	/* destroy krb5 machine creds */
851484
 	if (root_uses_machine_creds)
851484
 		gssd_destroy_krb5_machine_creds();
851484
 	printerr(1, "exiting on signal %d\n", signal);
851484
 	exit(0);
851484
 }
851484
 
851484
-void
851484
-sig_hup(int signal)
851484
-{
851484
-	/* don't exit on SIGHUP */
851484
-	printerr(1, "Received SIGHUP(%d)... Ignoring.\n", signal);
851484
-	return;
851484
-}
851484
-
851484
 static void
851484
 usage(char *progname)
851484
 {
851484
@@ -104,8 +756,9 @@ main(int argc, char *argv[])
851484
 	int i;
851484
 	extern char *optarg;
851484
 	char *progname;
851484
+	char *ccachedir = NULL;
851484
+	struct event sighup_ev;
851484
 
851484
-	memset(ccachesearch, 0, sizeof(ccachesearch));
851484
 	while ((opt = getopt(argc, argv, "DfvrlmnMp:k:d:t:T:R:")) != -1) {
851484
 		switch (opt) {
851484
 			case 'f':
851484
@@ -127,19 +780,13 @@ main(int argc, char *argv[])
851484
 				rpc_verbosity++;
851484
 				break;
851484
 			case 'p':
851484
-				strncpy(pipefs_dir, optarg, sizeof(pipefs_dir));
851484
-				if (pipefs_dir[sizeof(pipefs_dir)-1] != '\0')
851484
-					errx(1, "pipefs path name too long");
851484
+				pipefs_path = optarg;
851484
 				break;
851484
 			case 'k':
851484
-				strncpy(keytabfile, optarg, sizeof(keytabfile));
851484
-				if (keytabfile[sizeof(keytabfile)-1] != '\0')
851484
-					errx(1, "keytab path name too long");
851484
+				keytabfile = optarg;
851484
 				break;
851484
 			case 'd':
851484
-				strncpy(ccachedir, optarg, sizeof(ccachedir));
851484
-				if (ccachedir[sizeof(ccachedir)-1] != '\0')
851484
-					errx(1, "ccachedir path name too long");
851484
+				ccachedir = optarg;
851484
 				break;
851484
 			case 't':
851484
 				context_timeout = atoi(optarg);
851484
@@ -158,7 +805,7 @@ main(int argc, char *argv[])
851484
 #endif
851484
 				break;
851484
 			case 'D':
851484
-				avoid_dns = 0;
851484
+				avoid_dns = false;
851484
 				break;
851484
 			default:
851484
 				usage(argv[0]);
851484
@@ -174,15 +821,41 @@ main(int argc, char *argv[])
851484
 	 * the results of getpw*.
851484
 	 */
851484
 	if (setenv("HOME", "/", 1)) {
851484
-		printerr(1, "Unable to set $HOME: %s\n", strerror(errno));
851484
+		printerr(0, "gssd: Unable to set $HOME: %s\n", strerror(errno));
851484
 		exit(1);
851484
 	}
851484
 
851484
-	i = 0;
851484
-	ccachesearch[i++] = strtok(ccachedir, ":");
851484
-	do {
851484
-		ccachesearch[i++] = strtok(NULL, ":");
851484
-	} while (ccachesearch[i-1] != NULL && i < GSSD_MAX_CCACHE_SEARCH);
851484
+	if (ccachedir) {
851484
+		char *ccachedir_copy;
851484
+		char *ptr;
851484
+
851484
+		for (ptr = ccachedir, i = 2; *ptr; ptr++)
851484
+			if (*ptr == ':')
851484
+				i++;
851484
+
851484
+		ccachesearch = malloc(i * sizeof(char *));
851484
+	       	ccachedir_copy = strdup(ccachedir);
851484
+		if (!ccachedir_copy || !ccachesearch) {
851484
+			printerr(0, "malloc failure\n");
851484
+			exit(EXIT_FAILURE);
851484
+		}
851484
+
851484
+		i = 0;
851484
+		ccachesearch[i++] = strtok(ccachedir, ":");
851484
+		while(ccachesearch[i - 1])
851484
+			ccachesearch[i++] = strtok(NULL, ":");
851484
+
851484
+	} else {
851484
+		ccachesearch = malloc(3 * sizeof(char *));
851484
+		if (!ccachesearch) {
851484
+			printerr(0, "malloc failure\n");
851484
+			exit(EXIT_FAILURE);
851484
+		}
851484
+
851484
+		ccachesearch[0] = GSSD_DEFAULT_CRED_DIR;
851484
+		ccachesearch[1] = GSSD_USER_CRED_DIR;
851484
+		ccachesearch[2] = NULL;
851484
+	}
851484
 
851484
 	if (preferred_realm == NULL)
851484
 		gssd_k5_get_default_realm(&preferred_realm);
851484
@@ -197,6 +870,13 @@ main(int argc, char *argv[])
851484
 	if (verbosity && rpc_verbosity == 0)
851484
 		rpc_verbosity = verbosity;
851484
 	authgss_set_debug_level(rpc_verbosity);
851484
+#elif HAVE_LIBTIRPC_SET_DEBUG
851484
+	/*
851484
+	 * Only set the libtirpc debug level if explicitly requested via -r...
851484
+	 * gssd is chatty enough as it is.
851484
+	 */
851484
+	if (rpc_verbosity > 0)
851484
+		libtirpc_set_debug(progname, rpc_verbosity, fg);
851484
 #else
851484
         if (rpc_verbosity > 0)
851484
 		printerr(0, "Warning: rpcsec_gss library does not "
851484
@@ -206,14 +886,42 @@ main(int argc, char *argv[])
851484
 	if (gssd_check_mechs() != 0)
851484
 		errx(1, "Problem with gssapi library");
851484
 
851484
-	if (!fg)
851484
-		mydaemon(0, 0, pipefds);
851484
+	daemon_init(fg);
851484
+
851484
+	event_init();
851484
+
851484
+	pipefs_dir = opendir(pipefs_path);
851484
+	if (!pipefs_dir) {
851484
+		printerr(0, "ERROR: opendir(%s) failed: %s\n", pipefs_path, strerror(errno));
851484
+		exit(EXIT_FAILURE);
851484
+	}
851484
+
851484
+	pipefs_fd = dirfd(pipefs_dir);
851484
+	if (fchdir(pipefs_fd)) {
851484
+		printerr(0, "ERROR: fchdir(%s) failed: %s\n", pipefs_path, strerror(errno));
851484
+		exit(EXIT_FAILURE);
851484
+	}
851484
+
851484
+	inotify_fd = inotify_init1(IN_NONBLOCK);
851484
+	if (inotify_fd == -1) {
851484
+		printerr(0, "ERROR: inotify_init1 failed: %s\n", strerror(errno));
851484
+		exit(EXIT_FAILURE);
851484
+	}
851484
 
851484
 	signal(SIGINT, sig_die);
851484
 	signal(SIGTERM, sig_die);
851484
-	signal(SIGHUP, sig_hup);
851484
+	signal_set(&sighup_ev, SIGHUP, gssd_scan_cb, NULL);
851484
+	signal_add(&sighup_ev, NULL);
851484
+	event_set(&inotify_ev, inotify_fd, EV_READ | EV_PERSIST, gssd_inotify_cb, NULL);
851484
+	event_add(&inotify_ev, NULL);
851484
+
851484
+	TAILQ_INIT(&topdir_list);
851484
+	gssd_scan();
851484
+	daemon_ready();
851484
 
851484
-	gssd_run();
851484
-	printerr(0, "gssd_run returned!\n");
851484
-	abort();
851484
+	event_dispatch();
851484
+
851484
+	printerr(0, "ERROR: event_dispatch() returned!\n");
851484
+	return EXIT_FAILURE;
851484
 }
851484
+
851484
diff -up nfs-utils-1.3.0/utils/gssd/gssd.h.orig nfs-utils-1.3.0/utils/gssd/gssd.h
851484
--- nfs-utils-1.3.0/utils/gssd/gssd.h.orig	2016-04-15 11:42:13.917460638 -0400
851484
+++ nfs-utils-1.3.0/utils/gssd/gssd.h	2016-04-15 11:42:38.369938424 -0400
851484
@@ -34,14 +34,12 @@
851484
 #include <sys/types.h>
851484
 #include <sys/queue.h>
851484
 #include <gssapi/gssapi.h>
851484
+#include <event.h>
851484
+#include <stdbool.h>
851484
 
851484
-#define MAX_FILE_NAMELEN	32
851484
-#define FD_ALLOC_BLOCK		256
851484
 #ifndef GSSD_PIPEFS_DIR
851484
 #define GSSD_PIPEFS_DIR		"/var/lib/nfs/rpc_pipefs"
851484
 #endif
851484
-#define INFO			"info"
851484
-#define KRB5			"krb5"
851484
 #define DNOTIFY_SIGNAL		(SIGRTMIN + 3)
851484
 
851484
 #define GSSD_DEFAULT_CRED_DIR			"/tmp"
851484
@@ -50,60 +48,40 @@
851484
 #define GSSD_DEFAULT_MACHINE_CRED_SUFFIX	"machine"
851484
 #define GSSD_DEFAULT_KEYTAB_FILE		"/etc/krb5.keytab"
851484
 #define GSSD_SERVICE_NAME			"nfs"
851484
-#define GSSD_SERVICE_NAME_LEN			3
851484
-#define GSSD_MAX_CCACHE_SEARCH			16
851484
 
851484
 /*
851484
  * The gss mechanisms that we can handle
851484
  */
851484
 enum {AUTHTYPE_KRB5, AUTHTYPE_LIPKEY};
851484
 
851484
-
851484
-
851484
-extern char			pipefs_dir[PATH_MAX];
851484
-extern char			keytabfile[PATH_MAX];
851484
-extern char			*ccachesearch[];
851484
+extern char		       *keytabfile;
851484
+extern char		      **ccachesearch;
851484
 extern int			use_memcache;
851484
 extern int			root_uses_machine_creds;
851484
 extern unsigned int 		context_timeout;
851484
 extern unsigned int rpc_timeout;
851484
 extern char			*preferred_realm;
851484
-extern int			pipefds[2];
851484
-
851484
-TAILQ_HEAD(clnt_list_head, clnt_info) clnt_list;
851484
 
851484
 struct clnt_info {
851484
 	TAILQ_ENTRY(clnt_info)	list;
851484
-	char			*dirname;
851484
-	char			*pdir;
851484
-	int			dir_fd;
851484
+	int			wd;
851484
+	bool			scanned;
851484
+	char			*name;
851484
+	char			*relpath;
851484
 	char			*servicename;
851484
 	char			*servername;
851484
 	int			prog;
851484
 	int			vers;
851484
 	char			*protocol;
851484
 	int			krb5_fd;
851484
-	int			krb5_poll_index;
851484
-	int			krb5_close_me;
851484
-	int                     gssd_fd;
851484
-	int                     gssd_poll_index;
851484
-	int			gssd_close_me;
851484
-	struct sockaddr_storage addr;
851484
-};
851484
-
851484
-TAILQ_HEAD(topdirs_list_head, topdirs_info) topdirs_list;
851484
-
851484
-struct topdirs_info {
851484
-	TAILQ_ENTRY(topdirs_info)   list;
851484
-	char			*dirname;
851484
-	int			fd;
851484
+	struct event		krb5_ev;
851484
+	int			gssd_fd;
851484
+	struct event		gssd_ev;
851484
+	struct			sockaddr_storage addr;
851484
 };
851484
 
851484
-void init_client_list(void);
851484
-int update_client_list(void);
851484
 void handle_krb5_upcall(struct clnt_info *clp);
851484
 void handle_gssd_upcall(struct clnt_info *clp);
851484
-void gssd_run(void);
851484
 
851484
 
851484
 #endif /* _RPC_GSSD_H_ */
851484
diff -up nfs-utils-1.3.0/utils/gssd/gssd_proc.c.orig nfs-utils-1.3.0/utils/gssd/gssd_proc.c
851484
--- nfs-utils-1.3.0/utils/gssd/gssd_proc.c.orig	2016-04-15 11:42:13.949461263 -0400
851484
+++ nfs-utils-1.3.0/utils/gssd/gssd_proc.c	2016-04-15 11:42:38.371938463 -0400
851484
@@ -9,6 +9,7 @@
851484
   Copyright (c) 2002 Marius Aamodt Eriksen <marius@UMICH.EDU>.
851484
   Copyright (c) 2002 Bruce Fields <bfields@UMICH.EDU>
851484
   Copyright (c) 2004 Kevin Coffman <kwc@umich.edu>
851484
+  Copyright (c) 2014 David H?rdeman <david@hardeman.nu>
851484
   All rights reserved, all wrongs reversed.
851484
 
851484
   Redistribution and use in source and binary forms, with or without
851484
@@ -52,7 +53,6 @@
851484
 #include <sys/socket.h>
851484
 #include <arpa/inet.h>
851484
 #include <sys/fsuid.h>
851484
-#include <sys/resource.h>
851484
 
851484
 #include <stdio.h>
851484
 #include <stdlib.h>
851484
@@ -79,548 +79,6 @@
851484
 #include "nfslib.h"
851484
 #include "gss_names.h"
851484
 
851484
-/*
851484
- * pollarray:
851484
- *      array of struct pollfd suitable to pass to poll. initialized to
851484
- *      zero - a zero struct is ignored by poll() because the events mask is 0.
851484
- *
851484
- * clnt_list:
851484
- *      linked list of struct clnt_info which associates a clntXXX directory
851484
- *	with an index into pollarray[], and other basic data about that client.
851484
- *
851484
- * Directory structure: created by the kernel
851484
- *      {rpc_pipefs}/{dir}/clntXX         : one per rpc_clnt struct in the kernel
851484
- *      {rpc_pipefs}/{dir}/clntXX/krb5    : read uid for which kernel wants
851484
- *					    a context, write the resulting context
851484
- *      {rpc_pipefs}/{dir}/clntXX/info    : stores info such as server name
851484
- *      {rpc_pipefs}/{dir}/clntXX/gssd    : pipe for all gss mechanisms using
851484
- *					    a text-based string of parameters
851484
- *
851484
- * Algorithm:
851484
- *      Poll all {rpc_pipefs}/{dir}/clntXX/YYYY files.  When data is ready,
851484
- *      read and process; performs rpcsec_gss context initialization protocol to
851484
- *      get a cred for that user.  Writes result to corresponding krb5 file
851484
- *      in a form the kernel code will understand.
851484
- *      In addition, we make sure we are notified whenever anything is
851484
- *      created or destroyed in {rpc_pipefs} or in any of the clntXX directories,
851484
- *      and rescan the whole {rpc_pipefs} when this happens.
851484
- */
851484
-
851484
-struct pollfd * pollarray;
851484
-
851484
-unsigned long pollsize;  /* the size of pollaray (in pollfd's) */
851484
-
851484
-/* Avoid DNS reverse lookups on server names */
851484
-int avoid_dns = 1;
851484
-
851484
-/*
851484
- * convert a presentation address string to a sockaddr_storage struct. Returns
851484
- * true on success or false on failure.
851484
- *
851484
- * Note that we do not populate the sin6_scope_id field here for IPv6 addrs.
851484
- * gssd nececessarily relies on hostname resolution and DNS AAAA records
851484
- * do not generally contain scope-id's. This means that GSSAPI auth really
851484
- * can't work with IPv6 link-local addresses.
851484
- *
851484
- * We *could* consider changing this if we did something like adopt the
851484
- * Microsoft "standard" of using the ipv6-literal.net domainname, but it's
851484
- * not really feasible at present.
851484
- */
851484
-static int
851484
-addrstr_to_sockaddr(struct sockaddr *sa, const char *node, const char *port)
851484
-{
851484
-	int rc;
851484
-	struct addrinfo *res;
851484
-	struct addrinfo hints = { .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV };
851484
-
851484
-#ifndef IPV6_SUPPORTED
851484
-	hints.ai_family = AF_INET;
851484
-#endif /* IPV6_SUPPORTED */
851484
-
851484
-	rc = getaddrinfo(node, port, &hints, &res;;
851484
-	if (rc) {
851484
-		printerr(0, "ERROR: unable to convert %s|%s to sockaddr: %s\n",
851484
-			 node, port, rc == EAI_SYSTEM ? strerror(errno) :
851484
-						gai_strerror(rc));
851484
-		return 0;
851484
-	}
851484
-
851484
-#ifdef IPV6_SUPPORTED
851484
-	/*
851484
-	 * getnameinfo ignores the scopeid. If the address turns out to have
851484
-	 * a non-zero scopeid, we can't use it -- the resolved host might be
851484
-	 * completely different from the one intended.
851484
-	 */
851484
-	if (res->ai_addr->sa_family == AF_INET6) {
851484
-		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)res->ai_addr;
851484
-		if (sin6->sin6_scope_id) {
851484
-			printerr(0, "ERROR: address %s has non-zero "
851484
-				    "sin6_scope_id!\n", node);
851484
-			freeaddrinfo(res);
851484
-			return 0;
851484
-		}
851484
-	}
851484
-#endif /* IPV6_SUPPORTED */
851484
-
851484
-	memcpy(sa, res->ai_addr, res->ai_addrlen);
851484
-	freeaddrinfo(res);
851484
-	return 1;
851484
-}
851484
-
851484
-/*
851484
- * convert a sockaddr to a hostname
851484
- */
851484
-static char *
851484
-get_servername(const char *name, const struct sockaddr *sa, const char *addr)
851484
-{
851484
-	socklen_t		addrlen;
851484
-	int			err;
851484
-	char			*hostname;
851484
-	char			hbuf[NI_MAXHOST];
851484
-	unsigned char		buf[sizeof(struct in6_addr)];
851484
-
851484
-	if (avoid_dns) {
851484
-		/*
851484
-		 * Determine if this is a server name, or an IP address.
851484
-		 * If it is an IP address, do the DNS lookup otherwise
851484
-		 * skip the DNS lookup.
851484
-		 */
851484
-		int is_fqdn = 1;
851484
-		if (strchr(name, '.') == NULL)
851484
-			is_fqdn = 0; /* local name */
851484
-		else if (inet_pton(AF_INET, name, buf) == 1)
851484
-			is_fqdn = 0; /* IPv4 address */
851484
-		else if (inet_pton(AF_INET6, name, buf) == 1)
851484
-			is_fqdn = 0; /* IPv6 addrss */
851484
-
851484
-		if (is_fqdn) {
851484
-			return strdup(name);
851484
-		}
851484
-		/* Sorry, cannot avoid dns after all */
851484
-	}
851484
-
851484
-	switch (sa->sa_family) {
851484
-	case AF_INET:
851484
-		addrlen = sizeof(struct sockaddr_in);
851484
-		break;
851484
-#ifdef IPV6_SUPPORTED
851484
-	case AF_INET6:
851484
-		addrlen = sizeof(struct sockaddr_in6);
851484
-		break;
851484
-#endif /* IPV6_SUPPORTED */
851484
-	default:
851484
-		printerr(0, "ERROR: unrecognized addr family %d\n",
851484
-			 sa->sa_family);
851484
-		return NULL;
851484
-	}
851484
-
851484
-	err = getnameinfo(sa, addrlen, hbuf, sizeof(hbuf), NULL, 0,
851484
-			  NI_NAMEREQD);
851484
-	if (err) {
851484
-		printerr(0, "ERROR: unable to resolve %s to hostname: %s\n",
851484
-			 addr, err == EAI_SYSTEM ? strerror(errno) :
851484
-						   gai_strerror(err));
851484
-		return NULL;
851484
-	}
851484
-
851484
-	hostname = strdup(hbuf);
851484
-
851484
-	return hostname;
851484
-}
851484
-
851484
-/* XXX buffer problems: */
851484
-static int
851484
-read_service_info(char *info_file_name, char **servicename, char **servername,
851484
-		  int *prog, int *vers, char **protocol,
851484
-		  struct sockaddr *addr) {
851484
-#define INFOBUFLEN 256
851484
-	char		buf[INFOBUFLEN + 1];
851484
-	static char	server[128];
851484
-	int		nbytes;
851484
-	static char	service[128];
851484
-	static char	address[128];
851484
-	char		program[16];
851484
-	char		version[16];
851484
-	char		protoname[16];
851484
-	char		port[128];
851484
-	char		*p;
851484
-	int		fd = -1;
851484
-	int		numfields;
851484
-
851484
-	*servicename = *servername = *protocol = NULL;
851484
-
851484
-	if ((fd = open(info_file_name, O_RDONLY)) == -1) {
851484
-		printerr(0, "ERROR: can't open %s: %s\n", info_file_name,
851484
-			 strerror(errno));
851484
-		goto fail;
851484
-	}
851484
-	if ((nbytes = read(fd, buf, INFOBUFLEN)) == -1)
851484
-		goto fail;
851484
-	close(fd);
851484
-	fd = -1;
851484
-	buf[nbytes] = '\0';
851484
-
851484
-	numfields = sscanf(buf,"RPC server: %127s\n"
851484
-		   "service: %127s %15s version %15s\n"
851484
-		   "address: %127s\n"
851484
-		   "protocol: %15s\n",
851484
-		   server,
851484
-		   service, program, version,
851484
-		   address,
851484
-		   protoname);
851484
-
851484
-	if (numfields == 5) {
851484
-		strcpy(protoname, "tcp");
851484
-	} else if (numfields != 6) {
851484
-		goto fail;
851484
-	}
851484
-
851484
-	port[0] = '\0';
851484
-	if ((p = strstr(buf, "port")) != NULL)
851484
-		sscanf(p, "port: %127s\n", port);
851484
-
851484
-	/* get program, and version numbers */
851484
-	*prog = atoi(program + 1); /* skip open paren */
851484
-	*vers = atoi(version);
851484
-
851484
-	if (!addrstr_to_sockaddr(addr, address, port))
851484
-		goto fail;
851484
-
851484
-	*servername = get_servername(server, addr, address);
851484
-	if (*servername == NULL)
851484
-		goto fail;
851484
-
851484
-	nbytes = snprintf(buf, INFOBUFLEN, "%s@%s", service, *servername);
851484
-	if (nbytes > INFOBUFLEN)
851484
-		goto fail;
851484
-
851484
-	if (!(*servicename = calloc(strlen(buf) + 1, 1)))
851484
-		goto fail;
851484
-	memcpy(*servicename, buf, strlen(buf));
851484
-
851484
-	if (!(*protocol = strdup(protoname)))
851484
-		goto fail;
851484
-	return 0;
851484
-fail:
851484
-	printerr(0, "ERROR: failed to read service info\n");
851484
-	if (fd != -1) close(fd);
851484
-	free(*servername);
851484
-	free(*servicename);
851484
-	free(*protocol);
851484
-	*servicename = *servername = *protocol = NULL;
851484
-	return -1;
851484
-}
851484
-
851484
-static void
851484
-destroy_client(struct clnt_info *clp)
851484
-{
851484
-	if (clp->krb5_poll_index != -1)
851484
-		memset(&pollarray[clp->krb5_poll_index], 0,
851484
-					sizeof(struct pollfd));
851484
-	if (clp->gssd_poll_index != -1)
851484
-		memset(&pollarray[clp->gssd_poll_index], 0,
851484
-					sizeof(struct pollfd));
851484
-	if (clp->dir_fd != -1) close(clp->dir_fd);
851484
-	if (clp->krb5_fd != -1) close(clp->krb5_fd);
851484
-	if (clp->gssd_fd != -1) close(clp->gssd_fd);
851484
-	free(clp->dirname);
851484
-	free(clp->pdir);
851484
-	free(clp->servicename);
851484
-	free(clp->servername);
851484
-	free(clp->protocol);
851484
-	free(clp);
851484
-}
851484
-
851484
-static struct clnt_info *
851484
-insert_new_clnt(void)
851484
-{
851484
-	struct clnt_info	*clp = NULL;
851484
-
851484
-	if (!(clp = (struct clnt_info *)calloc(1,sizeof(struct clnt_info)))) {
851484
-		printerr(0, "ERROR: can't malloc clnt_info: %s\n",
851484
-			 strerror(errno));
851484
-		goto out;
851484
-	}
851484
-	clp->krb5_poll_index = -1;
851484
-	clp->gssd_poll_index = -1;
851484
-	clp->krb5_fd = -1;
851484
-	clp->gssd_fd = -1;
851484
-	clp->dir_fd = -1;
851484
-
851484
-	TAILQ_INSERT_HEAD(&clnt_list, clp, list);
851484
-out:
851484
-	return clp;
851484
-}
851484
-
851484
-static int
851484
-process_clnt_dir_files(struct clnt_info * clp)
851484
-{
851484
-	char	name[PATH_MAX];
851484
-	char	gname[PATH_MAX];
851484
-	char	info_file_name[PATH_MAX];
851484
-
851484
-	if (clp->gssd_close_me) {
851484
-		printerr(2, "Closing 'gssd' pipe for %s\n", clp->dirname);
851484
-		close(clp->gssd_fd);
851484
-		memset(&pollarray[clp->gssd_poll_index], 0,
851484
-			sizeof(struct pollfd));
851484
-		clp->gssd_fd = -1;
851484
-		clp->gssd_poll_index = -1;
851484
-		clp->gssd_close_me = 0;
851484
-	}
851484
-	if (clp->krb5_close_me) {
851484
-		printerr(2, "Closing 'krb5' pipe for %s\n", clp->dirname);
851484
-		close(clp->krb5_fd);
851484
-		memset(&pollarray[clp->krb5_poll_index], 0,
851484
-			sizeof(struct pollfd));
851484
-		clp->krb5_fd = -1;
851484
-		clp->krb5_poll_index = -1;
851484
-		clp->krb5_close_me = 0;
851484
-	}
851484
-
851484
-	if (clp->gssd_fd == -1) {
851484
-		snprintf(gname, sizeof(gname), "%s/gssd", clp->dirname);
851484
-		clp->gssd_fd = open(gname, O_RDWR);
851484
-	}
851484
-	if (clp->gssd_fd == -1) {
851484
-		if (clp->krb5_fd == -1) {
851484
-			snprintf(name, sizeof(name), "%s/krb5", clp->dirname);
851484
-			clp->krb5_fd = open(name, O_RDWR);
851484
-		}
851484
-
851484
-		/* If we opened a gss-specific pipe, let's try opening
851484
-		 * the new upcall pipe again. If we succeed, close
851484
-		 * gss-specific pipe(s).
851484
-		 */
851484
-		if (clp->krb5_fd != -1) {
851484
-			clp->gssd_fd = open(gname, O_RDWR);
851484
-			if (clp->gssd_fd != -1) {
851484
-				if (clp->krb5_fd != -1)
851484
-					close(clp->krb5_fd);
851484
-				clp->krb5_fd = -1;
851484
-			}
851484
-		}
851484
-	}
851484
-
851484
-	if ((clp->krb5_fd == -1) && (clp->gssd_fd == -1))
851484
-		return -1;
851484
-	snprintf(info_file_name, sizeof(info_file_name), "%s/info",
851484
-			clp->dirname);
851484
-	if (clp->prog == 0)
851484
-		read_service_info(info_file_name, &clp->servicename,
851484
-				  &clp->servername, &clp->prog, &clp->vers,
851484
-				  &clp->protocol, (struct sockaddr *) &clp->addr);
851484
-	return 0;
851484
-}
851484
-
851484
-static int
851484
-get_poll_index(int *ind)
851484
-{
851484
-	unsigned int i;
851484
-
851484
-	*ind = -1;
851484
-	for (i=0; i
851484
-		if (pollarray[i].events == 0) {
851484
-			*ind = i;
851484
-			break;
851484
-		}
851484
-	}
851484
-	if (*ind == -1) {
851484
-		printerr(0, "ERROR: No pollarray slots open\n");
851484
-		return -1;
851484
-	}
851484
-	return 0;
851484
-}
851484
-
851484
-
851484
-static int
851484
-insert_clnt_poll(struct clnt_info *clp)
851484
-{
851484
-	if ((clp->gssd_fd != -1) && (clp->gssd_poll_index == -1)) {
851484
-		if (get_poll_index(&clp->gssd_poll_index)) {
851484
-			printerr(0, "ERROR: Too many gssd clients\n");
851484
-			return -1;
851484
-		}
851484
-		pollarray[clp->gssd_poll_index].fd = clp->gssd_fd;
851484
-		pollarray[clp->gssd_poll_index].events |= POLLIN;
851484
-	}
851484
-
851484
-	if ((clp->krb5_fd != -1) && (clp->krb5_poll_index == -1)) {
851484
-		if (get_poll_index(&clp->krb5_poll_index)) {
851484
-			printerr(0, "ERROR: Too many krb5 clients\n");
851484
-			return -1;
851484
-		}
851484
-		pollarray[clp->krb5_poll_index].fd = clp->krb5_fd;
851484
-		pollarray[clp->krb5_poll_index].events |= POLLIN;
851484
-	}
851484
-
851484
-	return 0;
851484
-}
851484
-
851484
-static void
851484
-process_clnt_dir(char *dir, char *pdir)
851484
-{
851484
-	struct clnt_info *	clp;
851484
-
851484
-	if (!(clp = insert_new_clnt()))
851484
-		goto fail_destroy_client;
851484
-
851484
-	if (!(clp->pdir = strdup(pdir)))
851484
-		goto fail_destroy_client;
851484
-
851484
-	/* An extra for the '/', and an extra for the null */
851484
-	if (!(clp->dirname = calloc(strlen(dir) + strlen(pdir) + 2, 1))) {
851484
-		goto fail_destroy_client;
851484
-	}
851484
-	sprintf(clp->dirname, "%s/%s", pdir, dir);
851484
-	if ((clp->dir_fd = open(clp->dirname, O_RDONLY)) == -1) {
851484
-		if (errno != ENOENT)
851484
-			printerr(0, "ERROR: can't open %s: %s\n",
851484
-				 clp->dirname, strerror(errno));
851484
-		goto fail_destroy_client;
851484
-	}
851484
-	fcntl(clp->dir_fd, F_SETSIG, DNOTIFY_SIGNAL);
851484
-	fcntl(clp->dir_fd, F_NOTIFY, DN_CREATE | DN_DELETE | DN_MULTISHOT);
851484
-
851484
-	if (process_clnt_dir_files(clp))
851484
-		goto fail_keep_client;
851484
-
851484
-	if (insert_clnt_poll(clp))
851484
-		goto fail_destroy_client;
851484
-
851484
-	return;
851484
-
851484
-fail_destroy_client:
851484
-	if (clp) {
851484
-		TAILQ_REMOVE(&clnt_list, clp, list);
851484
-		destroy_client(clp);
851484
-	}
851484
-fail_keep_client:
851484
-	/* We couldn't find some subdirectories, but we keep the client
851484
-	 * around in case we get a notification on the directory when the
851484
-	 * subdirectories are created. */
851484
-	return;
851484
-}
851484
-
851484
-void
851484
-init_client_list(void)
851484
-{
851484
-	struct rlimit rlim;
851484
-	TAILQ_INIT(&clnt_list);
851484
-	/* Eventually plan to grow/shrink poll array: */
851484
-	pollsize = FD_ALLOC_BLOCK;
851484
-	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0 &&
851484
-	    rlim.rlim_cur != RLIM_INFINITY)
851484
-		pollsize = rlim.rlim_cur;
851484
-	pollarray = calloc(pollsize, sizeof(struct pollfd));
851484
-}
851484
-
851484
-/*
851484
- * This is run after a DNOTIFY signal, and should clear up any
851484
- * directories that are no longer around, and re-scan any existing
851484
- * directories, since the DNOTIFY could have been in there.
851484
- */
851484
-static void
851484
-update_old_clients(struct dirent **namelist, int size, char *pdir)
851484
-{
851484
-	struct clnt_info *clp;
851484
-	void *saveprev;
851484
-	int i, stillhere;
851484
-	char fname[PATH_MAX];
851484
-
851484
-	for (clp = clnt_list.tqh_first; clp != NULL; clp = clp->list.tqe_next) {
851484
-		/* only compare entries in the global list that are from the
851484
-		 * same pipefs parent directory as "pdir"
851484
-		 */
851484
-		if (strcmp(clp->pdir, pdir) != 0) continue;
851484
-
851484
-		stillhere = 0;
851484
-		for (i=0; i < size; i++) {
851484
-			snprintf(fname, sizeof(fname), "%s/%s",
851484
-				 pdir, namelist[i]->d_name);
851484
-			if (strcmp(clp->dirname, fname) == 0) {
851484
-				stillhere = 1;
851484
-				break;
851484
-			}
851484
-		}
851484
-		if (!stillhere) {
851484
-			printerr(2, "destroying client %s\n", clp->dirname);
851484
-			saveprev = clp->list.tqe_prev;
851484
-			TAILQ_REMOVE(&clnt_list, clp, list);
851484
-			destroy_client(clp);
851484
-			clp = saveprev;
851484
-		}
851484
-	}
851484
-	for (clp = clnt_list.tqh_first; clp != NULL; clp = clp->list.tqe_next) {
851484
-		if (!process_clnt_dir_files(clp))
851484
-			insert_clnt_poll(clp);
851484
-	}
851484
-}
851484
-
851484
-/* Search for a client by directory name, return 1 if found, 0 otherwise */
851484
-static int
851484
-find_client(char *dirname, char *pdir)
851484
-{
851484
-	struct clnt_info	*clp;
851484
-	char fname[PATH_MAX];
851484
-
851484
-	for (clp = clnt_list.tqh_first; clp != NULL; clp = clp->list.tqe_next) {
851484
-		snprintf(fname, sizeof(fname), "%s/%s", pdir, dirname);
851484
-		if (strcmp(clp->dirname, fname) == 0)
851484
-			return 1;
851484
-	}
851484
-	return 0;
851484
-}
851484
-
851484
-static int
851484
-process_pipedir(char *pipe_name)
851484
-{
851484
-	struct dirent **namelist;
851484
-	int i, j;
851484
-
851484
-	if (chdir(pipe_name) < 0) {
851484
-		printerr(0, "ERROR: can't chdir to %s: %s\n",
851484
-			 pipe_name, strerror(errno));
851484
-		return -1;
851484
-	}
851484
-
851484
-	j = scandir(pipe_name, &namelist, NULL, alphasort);
851484
-	if (j < 0) {
851484
-		printerr(0, "ERROR: can't scandir %s: %s\n",
851484
-			 pipe_name, strerror(errno));
851484
-		return -1;
851484
-	}
851484
-
851484
-	update_old_clients(namelist, j, pipe_name);
851484
-	for (i=0; i < j; i++) {
851484
-		if (!strncmp(namelist[i]->d_name, "clnt", 4)
851484
-		    && !find_client(namelist[i]->d_name, pipe_name))
851484
-			process_clnt_dir(namelist[i]->d_name, pipe_name);
851484
-		free(namelist[i]);
851484
-	}
851484
-
851484
-	free(namelist);
851484
-
851484
-	return 0;
851484
-}
851484
-
851484
-/* Used to read (and re-read) list of clients, set up poll array. */
851484
-int
851484
-update_client_list(void)
851484
-{
851484
-	int retval = -1;
851484
-	struct topdirs_info *tdi;
851484
-
851484
-	TAILQ_FOREACH(tdi, &topdirs_list, list) {
851484
-		retval = process_pipedir(tdi->dirname);
851484
-		if (retval)
851484
-			printerr(1, "WARNING: error processing %s\n",
851484
-				 tdi->dirname);
851484
-
851484
-	}
851484
-	return retval;
851484
-}
851484
-
851484
 /* Encryption types supported by the kernel rpcsec_gss code */
851484
 int num_krb5_enctypes = 0;
851484
 krb5_enctype *krb5_enctypes = NULL;
851484
@@ -691,7 +149,7 @@ do_downcall(int k5_fd, uid_t uid, struct
851484
 	unsigned int timeout = context_timeout;
851484
 	unsigned int buf_size = 0;
851484
 
851484
-	printerr(1, "doing downcall: lifetime_rec=%u acceptor=%.*s\n",
851484
+	printerr(2, "doing downcall: lifetime_rec=%u acceptor=%.*s\n",
851484
 		lifetime_rec, acceptor->length, acceptor->value);
851484
 	buf_size = sizeof(uid) + sizeof(timeout) + sizeof(pd->pd_seq_win) +
851484
 		sizeof(pd->pd_ctx_hndl.length) + pd->pd_ctx_hndl.length +
851484
@@ -730,7 +188,7 @@ do_error_downcall(int k5_fd, uid_t uid,
851484
 	unsigned int timeout = 0;
851484
 	int	zero = 0;
851484
 
851484
-	printerr(1, "doing error downcall\n");
851484
+	printerr(2, "doing error downcall\n");
851484
 
851484
 	if (WRITE_BYTES(&p, end, uid)) goto out_err;
851484
 	if (WRITE_BYTES(&p, end, timeout)) goto out_err;
851484
@@ -772,7 +230,7 @@ populate_port(struct sockaddr *sa, const
851484
 	switch (sa->sa_family) {
851484
 	case AF_INET:
851484
 		if (s4->sin_port != 0) {
851484
-			printerr(2, "DEBUG: port already set to %d\n",
851484
+			printerr(4, "DEBUG: port already set to %d\n",
851484
 				 ntohs(s4->sin_port));
851484
 			return 1;
851484
 		}
851484
@@ -780,7 +238,7 @@ populate_port(struct sockaddr *sa, const
851484
 #ifdef IPV6_SUPPORTED
851484
 	case AF_INET6:
851484
 		if (s6->sin6_port != 0) {
851484
-			printerr(2, "DEBUG: port already set to %d\n",
851484
+			printerr(4, "DEBUG: port already set to %d\n",
851484
 				 ntohs(s6->sin6_port));
851484
 			return 1;
851484
 		}
851484
@@ -941,7 +399,7 @@ create_auth_rpc_client(struct clnt_info
851484
 	auth = authgss_create_default(rpc_clnt, tgtname, &sec);
851484
 	if (!auth) {
851484
 		/* Our caller should print appropriate message */
851484
-		printerr(2, "WARNING: Failed to create krb5 context for "
851484
+		printerr(1, "WARNING: Failed to create krb5 context for "
851484
 			    "user with uid %d for server %s\n",
851484
 			 uid, tgtname);
851484
 		goto out_fail;
851484
@@ -1032,7 +490,7 @@ krb5_not_machine_creds(struct clnt_info
851484
 	char		**dname;
851484
 	int		err, resp = -1;
851484
 
851484
-	printerr(1, "krb5_not_machine_creds: uid %d tgtname %s\n", 
851484
+	printerr(2, "krb5_not_machine_creds: uid %d tgtname %s\n", 
851484
 		uid, tgtname);
851484
 
851484
 	*chg_err = change_identity(uid);
851484
@@ -1079,7 +537,7 @@ krb5_use_machine_creds(struct clnt_info
851484
 	int	nocache = 0;
851484
 	int	success = 0;
851484
 
851484
-	printerr(1, "krb5_use_machine_creds: uid %d tgtname %s\n", 
851484
+	printerr(2, "krb5_use_machine_creds: uid %d tgtname %s\n", 
851484
 		uid, tgtname);
851484
 
851484
 	do {
851484
@@ -1149,8 +607,6 @@ process_krb5_upcall(struct clnt_info *cl
851484
 	gss_OID			mech;
851484
 	gss_buffer_desc		acceptor  = {0};
851484
 
851484
-	printerr(1, "handling krb5 upcall (%s)\n", clp->dirname);
851484
-
851484
 	token.length = 0;
851484
 	token.value = NULL;
851484
 	memset(&pd, 0, sizeof(struct authgss_private_data));
851484
@@ -1176,8 +632,6 @@ process_krb5_upcall(struct clnt_info *cl
851484
 	 * used for this case is not important.
851484
 	 *
851484
 	 */
851484
-	printerr(2, "%s: service is '%s'\n", __func__,
851484
-		 service ? service : "<null>");
851484
 	if (uid != 0 || (uid == 0 && root_uses_machine_creds == 0 &&
851484
 				service == NULL)) {
851484
 
851484
@@ -1191,7 +645,7 @@ process_krb5_upcall(struct clnt_info *cl
851484
 			/* Child: fall through to rest of function */
851484
 			childpid = getpid();
851484
 			unsetenv("KRB5CCNAME");
851484
-			printerr(1, "CHILD forked pid %d \n", childpid);
851484
+			printerr(2, "CHILD forked pid %d \n", childpid);
851484
 			break;
851484
 		case -1:
851484
 			/* fork() failed! */
851484
@@ -1224,9 +678,7 @@ no_fork:
851484
 			if (auth == NULL)
851484
 				goto out_return_error;
851484
 		} else {
851484
-			printerr(1, "WARNING: Failed to create krb5 context "
851484
-				 "for user with uid %d for server %s\n",
851484
-				 uid, clp->servername);
851484
+			/* krb5_not_machine_creds logs the error */
851484
 			goto out_return_error;
851484
 		}
851484
 	}
851484
@@ -1257,7 +709,7 @@ no_fork:
851484
 	 * try to use it after this point.
851484
 	 */
851484
 	if (serialize_context_for_kernel(&pd.pd_ctx, &token, &krb5oid, NULL)) {
851484
-		printerr(0, "WARNING: Failed to serialize krb5 context for "
851484
+		printerr(1, "WARNING: Failed to serialize krb5 context for "
851484
 			    "user with uid %d for server %s\n",
851484
 			 uid, clp->servername);
851484
 		goto out_return_error;
851484
@@ -1300,6 +752,8 @@ handle_krb5_upcall(struct clnt_info *clp
851484
 		return;
851484
 	}
851484
 
851484
+	printerr(2, "\n%s: uid %d (%s)\n", __func__, uid, clp->relpath);
851484
+
851484
 	process_krb5_upcall(clp, uid, clp->krb5_fd, NULL, NULL);
851484
 }
851484
 
851484
@@ -1311,85 +765,66 @@ handle_gssd_upcall(struct clnt_info *clp
851484
 	int			lbuflen = 0;
851484
 	char			*p;
851484
 	char			*mech = NULL;
851484
+	char			*uidstr = NULL;
851484
 	char			*target = NULL;
851484
 	char			*service = NULL;
851484
 	char			*enctypes = NULL;
851484
 
851484
-	printerr(1, "handling gssd upcall (%s)\n", clp->dirname);
851484
-
851484
 	if (readline(clp->gssd_fd, &lbuf, &lbuflen) != 1) {
851484
 		printerr(0, "WARNING: handle_gssd_upcall: "
851484
 			    "failed reading request\n");
851484
 		return;
851484
 	}
851484
-	printerr(2, "%s: '%s'\n", __func__, lbuf);
851484
 
851484
-	/* find the mechanism name */
851484
-	if ((p = strstr(lbuf, "mech=")) != NULL) {
851484
-		mech = malloc(lbuflen);
851484
-		if (!mech)
851484
-			goto out;
851484
-		if (sscanf(p, "mech=%s", mech) != 1) {
851484
-			printerr(0, "WARNING: handle_gssd_upcall: "
851484
-				    "failed to parse gss mechanism name "
851484
-				    "in upcall string '%s'\n", lbuf);
851484
-			goto out;
851484
-		}
851484
-	} else {
851484
+	printerr(2, "\n%s: '%s' (%s)\n", __func__, lbuf, clp->relpath);
851484
+
851484
+	for (p = strtok(lbuf, " "); p; p = strtok(NULL, " ")) {
851484
+		if (!strncmp(p, "mech=", strlen("mech=")))
851484
+			mech = p + strlen("mech=");
851484
+		else if (!strncmp(p, "uid=", strlen("uid=")))
851484
+			uidstr = p + strlen("uid=");
851484
+		else if (!strncmp(p, "enctypes=", strlen("enctypes=")))
851484
+			enctypes = p + strlen("enctypes=");
851484
+		else if (!strncmp(p, "target=", strlen("target=")))
851484
+			target = p + strlen("target=");
851484
+		else if (!strncmp(p, "service=", strlen("service=")))
851484
+			service = p + strlen("service=");
851484
+	}
851484
+
851484
+	if (!mech || strlen(mech) < 1) {
851484
 		printerr(0, "WARNING: handle_gssd_upcall: "
851484
 			    "failed to find gss mechanism name "
851484
 			    "in upcall string '%s'\n", lbuf);
851484
-		goto out;
851484
+		return;
851484
 	}
851484
 
851484
-	/* read uid */
851484
-	if ((p = strstr(lbuf, "uid=")) != NULL) {
851484
-		if (sscanf(p, "uid=%d", &uid) != 1) {
851484
-			printerr(0, "WARNING: handle_gssd_upcall: "
851484
-				    "failed to parse uid "
851484
-				    "in upcall string '%s'\n", lbuf);
851484
-			goto out;
851484
-		}
851484
-	} else {
851484
+	if (uidstr) {
851484
+		uid = (uid_t)strtol(uidstr, &p, 10);
851484
+		if (p == uidstr || *p != '\0')
851484
+			uidstr = NULL;
851484
+	}
851484
+
851484
+	if (!uidstr) {
851484
 		printerr(0, "WARNING: handle_gssd_upcall: "
851484
 			    "failed to find uid "
851484
 			    "in upcall string '%s'\n", lbuf);
851484
-		goto out;
851484
+		return;
851484
 	}
851484
 
851484
-	/* read supported encryption types if supplied */
851484
-	if ((p = strstr(lbuf, "enctypes=")) != NULL) {
851484
-		enctypes = malloc(lbuflen);
851484
-		if (!enctypes)
851484
-			goto out;
851484
-		if (sscanf(p, "enctypes=%s", enctypes) != 1) {
851484
-			printerr(0, "WARNING: handle_gssd_upcall: "
851484
-				    "failed to parse encryption types "
851484
-				    "in upcall string '%s'\n", lbuf);
851484
-			goto out;
851484
-		}
851484
-		if (parse_enctypes(enctypes) != 0) {
851484
-			printerr(0, "WARNING: handle_gssd_upcall: "
851484
-				"parsing encryption types failed: errno %d\n", errno);
851484
-		}
851484
+	if (enctypes && parse_enctypes(enctypes) != 0) {
851484
+		printerr(0, "WARNING: handle_gssd_upcall: "
851484
+			 "parsing encryption types failed: errno %d\n", errno);
851484
+		return;
851484
 	}
851484
 
851484
-	/* read target name */
851484
-	if ((p = strstr(lbuf, "target=")) != NULL) {
851484
-		target = malloc(lbuflen);
851484
-		if (!target)
851484
-			goto out;
851484
-		if (sscanf(p, "target=%s", target) != 1) {
851484
-			printerr(0, "WARNING: handle_gssd_upcall: "
851484
-				    "failed to parse target name "
851484
-				    "in upcall string '%s'\n", lbuf);
851484
-			goto out;
851484
-		}
851484
+	if (target && strlen(target) < 1) {
851484
+		printerr(0, "WARNING: handle_gssd_upcall: "
851484
+			 "failed to parse target name "
851484
+			 "in upcall string '%s'\n", lbuf);
851484
+		return;
851484
 	}
851484
 
851484
 	/*
851484
-	 * read the service name
851484
-	 *
851484
 	 * The presence of attribute "service=" indicates that machine
851484
 	 * credentials should be used for this request.  If the value
851484
 	 * is "*", then any machine credentials available can be used.
851484
@@ -1397,16 +832,11 @@ handle_gssd_upcall(struct clnt_info *clp
851484
 	 * the specified service name (always "nfs" for now) should be
851484
 	 * used.
851484
 	 */
851484
-	if ((p = strstr(lbuf, "service=")) != NULL) {
851484
-		service = malloc(lbuflen);
851484
-		if (!service)
851484
-			goto out;
851484
-		if (sscanf(p, "service=%s", service) != 1) {
851484
-			printerr(0, "WARNING: handle_gssd_upcall: "
851484
-				    "failed to parse service type "
851484
-				    "in upcall string '%s'\n", lbuf);
851484
-			goto out;
851484
-		}
851484
+	if (service && strlen(service) < 1) {
851484
+		printerr(0, "WARNING: handle_gssd_upcall: "
851484
+			 "failed to parse service type "
851484
+			 "in upcall string '%s'\n", lbuf);
851484
+		return;
851484
 	}
851484
 
851484
 	if (strcmp(mech, "krb5") == 0 && clp->servername)
851484
@@ -1417,13 +847,5 @@ handle_gssd_upcall(struct clnt_info *clp
851484
 				 "received unknown gss mech '%s'\n", mech);
851484
 		do_error_downcall(clp->gssd_fd, uid, -EACCES);
851484
 	}
851484
-
851484
-out:
851484
-	free(lbuf);
851484
-	free(mech);
851484
-	free(enctypes);
851484
-	free(target);
851484
-	free(service);
851484
-	return;
851484
 }
851484
 
851484
diff -up nfs-utils-1.3.0/utils/gssd/gss_util.h.orig nfs-utils-1.3.0/utils/gssd/gss_util.h
851484
--- nfs-utils-1.3.0/utils/gssd/gss_util.h.orig	2014-03-25 11:12:07.000000000 -0400
851484
+++ nfs-utils-1.3.0/utils/gssd/gss_util.h	2016-04-15 11:42:38.368938404 -0400
851484
@@ -52,6 +52,4 @@ int gssd_check_mechs(void);
851484
 		gss_krb5_set_allowable_enctypes(min, cred, num, types)
851484
 #endif
851484
 
851484
-extern int avoid_dns;
851484
-
851484
 #endif /* _GSS_UTIL_H_ */
851484
diff -up nfs-utils-1.3.0/utils/gssd/krb5_util.c.orig nfs-utils-1.3.0/utils/gssd/krb5_util.c
851484
--- nfs-utils-1.3.0/utils/gssd/krb5_util.c.orig	2016-04-15 11:42:13.953461341 -0400
851484
+++ nfs-utils-1.3.0/utils/gssd/krb5_util.c	2016-04-15 11:42:38.372938482 -0400
851484
@@ -356,7 +356,7 @@ gssd_get_single_krb5_cred(krb5_context c
851484
 	 */
851484
 	now += 300;
851484
 	if (ple->ccname && ple->endtime > now && !nocache) {
851484
-		printerr(2, "INFO: Credentials in CC '%s' are good until %d\n",
851484
+		printerr(3, "INFO: Credentials in CC '%s' are good until %d\n",
851484
 			 ple->ccname, ple->endtime);
851484
 		code = 0;
851484
 		goto out;
851484
@@ -383,7 +383,7 @@ gssd_get_single_krb5_cred(krb5_context c
851484
 			 "tickets.  May have problems behind a NAT.\n");
851484
 #ifdef TEST_SHORT_LIFETIME
851484
 	/* set a short lifetime (for debugging only!) */
851484
-	printerr(0, "WARNING: Using (debug) short machine cred lifetime!\n");
851484
+	printerr(1, "WARNING: Using (debug) short machine cred lifetime!\n");
851484
 	krb5_get_init_creds_opt_set_tkt_life(init_opts, 5*60);
851484
 #endif
851484
 	opts = init_opts;
851484
@@ -451,8 +451,7 @@ gssd_get_single_krb5_cred(krb5_context c
851484
 	}
851484
 
851484
 	code = 0;
851484
-	printerr(2, "Successfully obtained machine credentials for "
851484
-		 "principal '%s' stored in ccache '%s'\n", pname, cc_name);
851484
+	printerr(2, "%s: principal '%s' ccache:'%s'\n", __func__, pname, cc_name);
851484
   out:
851484
 #if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS
851484
 	if (init_opts)
851484
@@ -477,7 +476,7 @@ gssd_set_krb5_ccache_name(char *ccname)
851484
 #ifdef USE_GSS_KRB5_CCACHE_NAME
851484
 	u_int	maj_stat, min_stat;
851484
 
851484
-	printerr(2, "using gss_krb5_ccache_name to select krb5 ccache %s\n",
851484
+	printerr(3, "using gss_krb5_ccache_name to select krb5 ccache %s\n",
851484
 		 ccname);
851484
 	maj_stat = gss_krb5_ccache_name(&min_stat, ccname, NULL);
851484
 	if (maj_stat != GSS_S_COMPLETE) {
851484
@@ -492,7 +491,7 @@ gssd_set_krb5_ccache_name(char *ccname)
851484
 	 * function above for which there is no generic gssapi
851484
 	 * equivalent.)
851484
 	 */
851484
-	printerr(2, "using environment variable to select krb5 ccache %s\n",
851484
+	printerr(3, "using environment variable to select krb5 ccache %s\n",
851484
 		 ccname);
851484
 	setenv("KRB5CCNAME", ccname, 1);
851484
 #endif
851484
@@ -1093,8 +1092,8 @@ gssd_setup_krb5_user_gss_ccache(uid_t ui
851484
 	struct dirent		*d;
851484
 	int			err, i, j;
851484
 
851484
-	printerr(2, "getting credentials for client with uid %u for "
851484
-		    "server %s\n", uid, servername);
851484
+	printerr(3, "looking for client creds with uid %u for "
851484
+		    "server %s in %s\n", uid, servername, dirpattern);
851484
 
851484
 	for (i = 0, j = 0; dirpattern[i] != '\0'; i++) {
851484
 		switch (dirpattern[i]) {
851484
@@ -1410,16 +1409,21 @@ gssd_acquire_krb5_cred(gss_cred_id_t *gs
851484
 int
851484
 gssd_acquire_user_cred(gss_cred_id_t *gss_cred)
851484
 {
851484
-	OM_uint32 min_stat;
851484
+	OM_uint32 maj_stat, min_stat;
851484
 	int ret;
851484
 
851484
 	ret = gssd_acquire_krb5_cred(gss_cred);
851484
 
851484
 	/* force validation of cred to check for expiry */
851484
 	if (ret == 0) {
851484
-		if (gss_inquire_cred(&min_stat, *gss_cred, NULL, NULL,
851484
-				     NULL, NULL) != GSS_S_COMPLETE)
851484
-			ret = -1;
851484
+		maj_stat = gss_inquire_cred(&min_stat, *gss_cred, 
851484
+			NULL, NULL, NULL, NULL);
851484
+		if (maj_stat != GSS_S_COMPLETE) {
851484
+			if (get_verbosity() > 0)
851484
+				pgsserr("gss_inquire_cred",
851484
+					maj_stat, min_stat, &krb5oid);
851484
+				ret = -1;
851484
+			}
851484
 	}
851484
 
851484
 	return ret;
851484
diff -up nfs-utils-1.3.0/utils/gssd/Makefile.am.orig nfs-utils-1.3.0/utils/gssd/Makefile.am
851484
--- nfs-utils-1.3.0/utils/gssd/Makefile.am.orig	2016-04-15 11:42:13.942461126 -0400
851484
+++ nfs-utils-1.3.0/utils/gssd/Makefile.am	2016-04-15 11:42:38.367938385 -0400
851484
@@ -29,7 +29,6 @@ COMMON_SRCS = \
851484
 gssd_SOURCES = \
851484
 	$(COMMON_SRCS) \
851484
 	gssd.c \
851484
-	gssd_main_loop.c \
851484
 	gssd_proc.c \
851484
 	krb5_util.c \
851484
 	\
851484
@@ -37,12 +36,23 @@ gssd_SOURCES = \
851484
 	krb5_util.h \
851484
 	write_bytes.h
851484
 
851484
-gssd_LDADD =	../../support/nfs/libnfs.a \
851484
-		$(RPCSECGSS_LIBS) $(KRBLIBS) $(GSSAPI_LIBS)
851484
-gssd_LDFLAGS = $(KRBLDFLAGS) $(LIBTIRPC)
851484
+gssd_LDADD = \
851484
+	../../support/nfs/libnfs.a \
851484
+	$(LIBEVENT) \
851484
+	$(RPCSECGSS_LIBS) \
851484
+	$(KRBLIBS) \
851484
+	$(GSSAPI_LIBS) \
851484
+	$(LIBTIRPC)
851484
 
851484
-gssd_CFLAGS = $(AM_CFLAGS) $(CFLAGS) \
851484
-	      $(RPCSECGSS_CFLAGS) $(KRBCFLAGS) $(GSSAPI_CFLAGS)
851484
+gssd_LDFLAGS = \
851484
+	$(KRBLDFLAGS)
851484
+
851484
+gssd_CFLAGS = \
851484
+	$(AM_CFLAGS) \
851484
+	$(CFLAGS) \
851484
+	$(RPCSECGSS_CFLAGS) \
851484
+	$(KRBCFLAGS) \
851484
+	$(GSSAPI_CFLAGS)
851484
 
851484
 svcgssd_SOURCES = \
851484
 	$(COMMON_SRCS) \
851484
diff -up nfs-utils-1.3.0/utils/gssd/svcgssd.c.orig nfs-utils-1.3.0/utils/gssd/svcgssd.c
851484
--- nfs-utils-1.3.0/utils/gssd/svcgssd.c.orig	2014-03-25 11:12:07.000000000 -0400
851484
+++ nfs-utils-1.3.0/utils/gssd/svcgssd.c	2016-04-15 11:42:38.372938482 -0400
851484
@@ -62,8 +62,6 @@
851484
 #include "gss_util.h"
851484
 #include "err_util.h"
851484
 
851484
-static int pipefds[2] = { -1, -1 };
851484
-
851484
 void
851484
 sig_die(int signal)
851484
 {
851484
@@ -137,6 +135,13 @@ main(int argc, char *argv[])
851484
 	if (verbosity && rpc_verbosity == 0)
851484
 		rpc_verbosity = verbosity;
851484
 	authgss_set_debug_level(rpc_verbosity);
851484
+#elif HAVE_LIBTIRPC_SET_DEBUG
851484
+        /*
851484
+	 * Only set the libtirpc debug level if explicitly requested via -r...
851484
+	 * svcgssd is chatty enough as it is.
851484
+	 */
851484
+        if (rpc_verbosity > 0)
851484
+                libtirpc_set_debug(progname, rpc_verbosity, fg);
851484
 #else
851484
 	if (rpc_verbosity > 0)
851484
 		printerr(0, "Warning: rpcsec_gss library does not "
851484
@@ -157,8 +162,7 @@ main(int argc, char *argv[])
851484
 		exit(1);
851484
 	}
851484
 
851484
-	if (!fg)
851484
-		mydaemon(0, 0, pipefds);
851484
+	daemon_init(fg);
851484
 
851484
 	signal(SIGINT, sig_die);
851484
 	signal(SIGTERM, sig_die);
851484
@@ -187,8 +191,7 @@ main(int argc, char *argv[])
851484
 		}
851484
 	}
851484
 
851484
-	if (!fg)
851484
-		release_parent(pipefds);
851484
+	daemon_ready();
851484
 
851484
 	nfs4_init_name_mapping(NULL); /* XXX: should only do this once */
851484
 	gssd_run();
851484
diff -up nfs-utils-1.3.0/utils/idmapd/idmapd.c.orig nfs-utils-1.3.0/utils/idmapd/idmapd.c
851484
--- nfs-utils-1.3.0/utils/idmapd/idmapd.c.orig	2014-03-25 11:12:07.000000000 -0400
851484
+++ nfs-utils-1.3.0/utils/idmapd/idmapd.c	2016-04-15 11:42:38.373938502 -0400
851484
@@ -164,7 +164,6 @@ static char pipefsdir[PATH_MAX];
851484
 static char *nobodyuser, *nobodygroup;
851484
 static uid_t nobodyuid;
851484
 static gid_t nobodygid;
851484
-static int pipefds[2] = { -1, -1 };
851484
 
851484
 /* Used by conffile.c in libnfs.a */
851484
 char *conf_path;
851484
@@ -302,8 +301,7 @@ main(int argc, char **argv)
851484
 	if (nfs4_init_name_mapping(conf_path))
851484
 		errx(1, "Unable to create name to user id mappings.");
851484
 
851484
-	if (!fg)
851484
-		mydaemon(0, 0, pipefds);
851484
+	daemon_init(fg);
851484
 
851484
 	event_init();
851484
 
851484
@@ -380,7 +378,7 @@ main(int argc, char **argv)
851484
 	if (nfsdret != 0 && fd == 0)
851484
 		xlog_err("main: Neither NFS client nor NFSd found");
851484
 
851484
-	release_parent(pipefds);
851484
+	daemon_ready();
851484
 
851484
 	if (event_dispatch() < 0)
851484
 		xlog_err("main: event_dispatch returns errno %d (%s)",
851484
diff -up nfs-utils-1.3.0/utils/statd/statd.c.orig nfs-utils-1.3.0/utils/statd/statd.c
851484
--- nfs-utils-1.3.0/utils/statd/statd.c.orig	2014-03-25 11:12:07.000000000 -0400
851484
+++ nfs-utils-1.3.0/utils/statd/statd.c	2016-04-15 11:42:38.373938502 -0400
851484
@@ -248,13 +248,12 @@ int main (int argc, char **argv)
851484
 	int nlm_udp = 0, nlm_tcp = 0;
851484
 	struct rlimit rlim;
851484
 
851484
-	int pipefds[2] = { -1, -1};
851484
-	char status;
851484
-
851484
 	/* Default: daemon mode, no other options */
851484
 	run_mode = 0;
851484
-	xlog_stderr(0);
851484
-	xlog_syslog(1);
851484
+
851484
+	/* Log to stderr if there's an error during startup */
851484
+	xlog_stderr(1);
851484
+	xlog_syslog(0);
851484
 
851484
 	/* Set the basename */
851484
 	if ((name_p = strrchr(argv[0],'/')) != NULL) {
851484
@@ -394,52 +393,17 @@ int main (int argc, char **argv)
851484
 		simulator (--argc, ++argv);	/* simulator() does exit() */
851484
 #endif
851484
 
851484
-	if (!(run_mode & MODE_NODAEMON)) {
851484
-		int tempfd;
851484
-
851484
-		if (pipe(pipefds)<0) {
851484
-			perror("statd: unable to create pipe");
851484
-			exit(1);
851484
-		}
851484
-		if ((pid = fork ()) < 0) {
851484
-			perror ("statd: Could not fork");
851484
-			exit (1);
851484
-		} else if (pid != 0) {
851484
-			/* Parent.
851484
-			 * Wait for status from child.
851484
-			 */
851484
-			close(pipefds[1]);
851484
-			if (read(pipefds[0], &status, 1) != 1)
851484
-				exit(1);
851484
-			exit (0);
851484
-		}
851484
-		/* Child.	*/
851484
-		close(pipefds[0]);
851484
-		setsid ();
851484
-
851484
-		while (pipefds[1] <= 2) {
851484
-			pipefds[1] = dup(pipefds[1]);
851484
-			if (pipefds[1]<0) {
851484
-				perror("statd: dup");
851484
-				exit(1);
851484
-			}
851484
-		}
851484
-		tempfd = open("/dev/null", O_RDWR);
851484
-		dup2(tempfd, 0);
851484
-		dup2(tempfd, 1);
851484
-		dup2(tempfd, 2);
851484
-		dup2(pipefds[1], 3);
851484
-		pipefds[1] = 3;
851484
-		closeall(4);
851484
-	}
851484
-
851484
-	/* Child. */
851484
+	daemon_init((run_mode & MODE_NODAEMON));
851484
 
851484
 	if (run_mode & MODE_LOG_STDERR) {
851484
 		xlog_syslog(0);
851484
 		xlog_stderr(1);
851484
 		xlog_config(D_ALL, 1);
851484
+	} else {
851484
+		xlog_syslog(1);
851484
+		xlog_stderr(0);
851484
 	}
851484
+
851484
 	xlog_open(name_p);
851484
 	xlog(L_NOTICE, "Version " VERSION " starting");
851484
 
851484
@@ -512,16 +476,8 @@ int main (int argc, char **argv)
851484
 	}
851484
 	atexit(statd_unregister);
851484
 
851484
-	/* If we got this far, we have successfully started, so notify parent */
851484
-	if (pipefds[1] > 0) {
851484
-		status = 0;
851484
-		if (write(pipefds[1], &status, 1) != 1) {
851484
-			xlog_warn("writing to parent pipe failed: errno %d (%s)\n",
851484
-				errno, strerror(errno));
851484
-		}
851484
-		close(pipefds[1]);
851484
-		pipefds[1] = -1;
851484
-	}
851484
+	/* If we got this far, we have successfully started */
851484
+	daemon_ready();
851484
 
851484
 	for (;;) {
851484
 		/*