49b67f
autofs-5.1.7 - add xdr_exports()
49b67f
49b67f
From: Ian Kent <raven@themaw.net>
49b67f
49b67f
Add an xdr_exports() function to get NFS exports from a server.
49b67f
49b67f
Signed-off-by: Ian Kent <raven@themaw.net>
49b67f
---
49b67f
 CHANGELOG              |    3 +
49b67f
 include/rpc_subs.h     |   14 ++++++
49b67f
 lib/rpc_subs.c         |  120 +++++++++++++++++++++++++++++++++++-------------
49b67f
 modules/lookup_hosts.c |   25 +++-------
49b67f
 4 files changed, 112 insertions(+), 50 deletions(-)
49b67f
49b67f
--- autofs-5.1.4.orig/CHANGELOG
49b67f
+++ autofs-5.1.4/CHANGELOG
49b67f
@@ -1,3 +1,6 @@
49b67f
+
49b67f
+- add xdr_exports().
49b67f
+
49b67f
 xx/xx/2018 autofs-5.1.5
49b67f
 - fix flag file permission.
49b67f
 - fix directory create permission.
49b67f
--- autofs-5.1.4.orig/include/rpc_subs.h
49b67f
+++ autofs-5.1.4/include/rpc_subs.h
49b67f
@@ -17,6 +17,7 @@
49b67f
 #define _RPC_SUBS_H
49b67f
 
49b67f
 #include <rpc/rpc.h>
49b67f
+#include <rpc/types.h>
49b67f
 #include <rpc/pmap_prot.h>
49b67f
 #include <nfs/nfs.h>
49b67f
 #include <linux/nfs2.h>
49b67f
@@ -47,6 +48,17 @@
49b67f
 
49b67f
 #define HOST_ENT_BUF_SIZE       2048
49b67f
 
49b67f
+struct hostinfo {
49b67f
+	char *name;
49b67f
+	struct hostinfo *next;
49b67f
+};
49b67f
+
49b67f
+struct exportinfo {
49b67f
+	char *dir;
49b67f
+	struct hostinfo *hosts;
49b67f
+	struct exportinfo *next;
49b67f
+};
49b67f
+
49b67f
 struct conn_info {
49b67f
 	const char *host;
49b67f
 	struct sockaddr *addr;
49b67f
@@ -71,6 +83,8 @@ int rpc_portmap_getport(struct conn_info
49b67f
 int rpc_ping_proto(struct conn_info *);
49b67f
 int rpc_ping(const char *, int, unsigned int, long, long, unsigned int);
49b67f
 double monotonic_elapsed(struct timespec, struct timespec);
49b67f
+struct exportinfo *rpc_get_exports(const char *host, long seconds, long micros, unsigned int option);
49b67f
+void rpc_exports_free(struct exportinfo *exports);
49b67f
 const char *get_addr_string(struct sockaddr *, char *, socklen_t);
49b67f
 
49b67f
 #endif
49b67f
--- autofs-5.1.4.orig/lib/rpc_subs.c
49b67f
+++ autofs-5.1.4/lib/rpc_subs.c
49b67f
@@ -41,7 +41,6 @@ const rpcprog_t rpcb_prog = PMAPPROG;
49b67f
 const rpcvers_t rpcb_version = PMAPVERS;
49b67f
 #endif
49b67f
 
49b67f
-#include "mount.h"
49b67f
 #include "rpc_subs.h"
49b67f
 #include "replicated.h"
49b67f
 #include "automount.h"
49b67f
@@ -58,6 +57,17 @@ const rpcvers_t rpcb_version = PMAPVERS;
49b67f
 
49b67f
 #define MAX_NETWORK_LEN		255
49b67f
 
49b67f
+#define EXPPATHLEN 1024
49b67f
+#define EXPNAMELEN 255
49b67f
+
49b67f
+#define MOUNTPROG 100005
49b67f
+
49b67f
+#define MOUNTVERS	1
49b67f
+#define MOUNTVERS_NFSV3 3
49b67f
+#define MOUNTVERS_POSIX 2
49b67f
+
49b67f
+#define MOUNTPROC_EXPORT 5
49b67f
+
49b67f
 /* Get numeric value of the n bits starting at position p */
49b67f
 #define getbits(x, p, n)      ((x >> (p + 1 - n)) & ~(~0 << n))
49b67f
 
49b67f
@@ -1102,7 +1112,55 @@ double monotonic_elapsed(struct timespec
49b67f
 	return t2 - t1;
49b67f
 }
49b67f
 
49b67f
-static int rpc_get_exports_proto(struct conn_info *info, exports *exp)
49b67f
+static bool_t xdr_host(XDR *xdrs, struct hostinfo *host)
49b67f
+{
49b67f
+	if (!xdr_string(xdrs, &host->name, EXPNAMELEN))
49b67f
+		return FALSE;
49b67f
+	return TRUE;
49b67f
+}
49b67f
+
49b67f
+static bool_t xdr_hosts(XDR *xdrs, struct hostinfo **hosts)
49b67f
+{
49b67f
+	unsigned int size = sizeof(struct hostinfo);
49b67f
+	char **host;
49b67f
+
49b67f
+	host = (char **) hosts;
49b67f
+	while (1) {
49b67f
+		if (!xdr_pointer(xdrs, host, size, (xdrproc_t) xdr_host))
49b67f
+			return FALSE;
49b67f
+		if (!*host)
49b67f
+			break;
49b67f
+		host = (char **) &((struct hostinfo *) *host)->next;
49b67f
+	}
49b67f
+	return TRUE;
49b67f
+}
49b67f
+
49b67f
+static bool_t xdr_export(XDR *xdrs, struct exportinfo *export)
49b67f
+{
49b67f
+	if (!xdr_string(xdrs, &export->dir, EXPPATHLEN))
49b67f
+		return FALSE;
49b67f
+	if (!xdr_hosts(xdrs, &export->hosts))
49b67f
+		return FALSE;
49b67f
+	return TRUE;
49b67f
+}
49b67f
+
49b67f
+bool_t xdr_exports(XDR *xdrs, struct exportinfo **exports)
49b67f
+{
49b67f
+	unsigned int size = sizeof(struct exportinfo);
49b67f
+	char **export;
49b67f
+
49b67f
+	export = (char **) exports;
49b67f
+	while (1) {
49b67f
+		if (!xdr_pointer(xdrs, export, size, (xdrproc_t) xdr_export))
49b67f
+			return FALSE;
49b67f
+		if (!*export)
49b67f
+			break;
49b67f
+		export = (char **) &((struct exportinfo *) *export)->next;
49b67f
+	}
49b67f
+	return TRUE;
49b67f
+}
49b67f
+
49b67f
+static int rpc_get_exports_proto(struct conn_info *info, struct exportinfo **exports)
49b67f
 {
49b67f
 	CLIENT *client;
49b67f
 	enum clnt_stat status;
49b67f
@@ -1133,7 +1191,7 @@ static int rpc_get_exports_proto(struct
49b67f
 	while (1) {
49b67f
 		status = clnt_call(client, MOUNTPROC_EXPORT,
49b67f
 				 (xdrproc_t) xdr_void, NULL,
49b67f
-				 (xdrproc_t) xdr_exports, (caddr_t) exp,
49b67f
+				 (xdrproc_t) xdr_exports, (caddr_t) exports,
49b67f
 				 info->timeout);
49b67f
 		if (status == RPC_SUCCESS)
49b67f
 			break;
49b67f
@@ -1168,41 +1226,43 @@ static int rpc_get_exports_proto(struct
49b67f
 	return 1;
49b67f
 }
49b67f
 
49b67f
-static void rpc_export_free(exports item)
49b67f
+static void rpc_export_free(struct exportinfo *export)
49b67f
 {
49b67f
-	groups grp;
49b67f
-	groups tmp;
49b67f
+	struct hostinfo *host, *tmp;
49b67f
 
49b67f
-	if (item->ex_dir)
49b67f
-		free(item->ex_dir);
49b67f
+	if (export->dir)
49b67f
+		free(export->dir);
49b67f
 
49b67f
-	grp = item->ex_groups;
49b67f
-	while (grp) {
49b67f
-		if (grp->gr_name)
49b67f
-			free(grp->gr_name);
49b67f
-		tmp = grp;
49b67f
-		grp = grp->gr_next;
49b67f
+	host = export->hosts;
49b67f
+	while (host) {
49b67f
+		if (host->name)
49b67f
+			free(host->name);
49b67f
+		tmp = host;
49b67f
+		host = host->next;
49b67f
 		free(tmp);
49b67f
 	}
49b67f
-	free(item);
49b67f
+	free(export);
49b67f
 }
49b67f
 
49b67f
-void rpc_exports_free(exports list)
49b67f
+void rpc_exports_free(struct exportinfo *exports)
49b67f
 {
49b67f
-	exports tmp;
49b67f
+	struct exportinfo *export, *tmp;
49b67f
 
49b67f
-	while (list) {
49b67f
-		tmp = list;
49b67f
-		list = list->ex_next;
49b67f
+	export = exports;
49b67f
+	while (export) {
49b67f
+		tmp = export;
49b67f
+		export = export->next;
49b67f
 		rpc_export_free(tmp);
49b67f
 	}
49b67f
 	return;
49b67f
 }
49b67f
 
49b67f
-exports rpc_get_exports(const char *host, long seconds, long micros, unsigned int option)
49b67f
+struct exportinfo *rpc_get_exports(const char *host,
49b67f
+				   long seconds, long micros,
49b67f
+				   unsigned int option)
49b67f
 {
49b67f
 	struct conn_info info;
49b67f
-	exports exportlist;
49b67f
+	struct exportinfo *exports = NULL;
49b67f
 	struct pmap parms;
49b67f
 	int status;
49b67f
 
49b67f
@@ -1231,11 +1291,9 @@ exports rpc_get_exports(const char *host
49b67f
 	if (status < 0)
49b67f
 		goto try_tcp;
49b67f
 
49b67f
-	memset(&exportlist, '\0', sizeof(exportlist));
49b67f
-
49b67f
-	status = rpc_get_exports_proto(&info, &exportlist);
49b67f
+	status = rpc_get_exports_proto(&info, &exports);
49b67f
 	if (status)
49b67f
-		return exportlist;
49b67f
+		return exports;
49b67f
 
49b67f
 try_tcp:
49b67f
 	info.proto = IPPROTO_TCP;
49b67f
@@ -1246,13 +1304,11 @@ try_tcp:
49b67f
 	if (status < 0)
49b67f
 		return NULL;
49b67f
 
49b67f
-	memset(&exportlist, '\0', sizeof(exportlist));
49b67f
-
49b67f
-	status = rpc_get_exports_proto(&info, &exportlist);
49b67f
+	status = rpc_get_exports_proto(&info, &exports);
49b67f
 	if (!status)
49b67f
 		return NULL;
49b67f
 
49b67f
-	return exportlist;
49b67f
+	return exports;
49b67f
 }
49b67f
 
49b67f
 const char *get_addr_string(struct sockaddr *sa, char *name, socklen_t len)
49b67f
--- autofs-5.1.4.orig/modules/lookup_hosts.c
49b67f
+++ autofs-5.1.4/modules/lookup_hosts.c
49b67f
@@ -20,14 +20,6 @@
49b67f
 #include <sys/stat.h>
49b67f
 #include <netdb.h>
49b67f
 
49b67f
-/* 
49b67f
- * Avoid annoying compiler noise by using an alternate name for
49b67f
- * typedef name in mount.h
49b67f
- */
49b67f
-#define name __dummy_type_name
49b67f
-#include "mount.h"
49b67f
-#undef name
49b67f
-
49b67f
 #define MODULE_LOOKUP
49b67f
 #include "automount.h"
49b67f
 #include "nsswitch.h"
49b67f
@@ -43,9 +35,6 @@ struct lookup_context {
49b67f
 
49b67f
 int lookup_version = AUTOFS_LOOKUP_VERSION;	/* Required by protocol */
49b67f
 
49b67f
-exports rpc_get_exports(const char *host, long seconds, long micros, unsigned int option);
49b67f
-void rpc_exports_free(exports list);
49b67f
-
49b67f
 int lookup_init(const char *mapfmt,
49b67f
 		int argc, const char *const *argv, void **context)
49b67f
 {
49b67f
@@ -99,7 +88,7 @@ static char *get_exports(struct autofs_p
49b67f
 {
49b67f
 	char buf[MAX_ERR_BUF];
49b67f
 	char *mapent;
49b67f
-	exports exp, this;
49b67f
+	struct exportinfo *exp, *this;
49b67f
 
49b67f
 	debug(ap->logopt, MODPREFIX "fetchng export list for %s", host);
49b67f
 
49b67f
@@ -111,7 +100,7 @@ static char *get_exports(struct autofs_p
49b67f
 		if (mapent) {
49b67f
 			int len = strlen(mapent) + 1;
49b67f
 
49b67f
-			len += strlen(host) + 2*(strlen(this->ex_dir) + 2) + 3;
49b67f
+			len += strlen(host) + 2*(strlen(this->dir) + 2) + 3;
49b67f
 			mapent = realloc(mapent, len);
49b67f
 			if (!mapent) {
49b67f
 				char *estr;
49b67f
@@ -121,10 +110,10 @@ static char *get_exports(struct autofs_p
49b67f
 				return NULL;
49b67f
 			}
49b67f
 			strcat(mapent, " \"");
49b67f
-			strcat(mapent, this->ex_dir);
49b67f
+			strcat(mapent, this->dir);
49b67f
 			strcat(mapent, "\"");
49b67f
 		} else {
49b67f
-			int len = 2*(strlen(this->ex_dir) + 2) + strlen(host) + 3;
49b67f
+			int len = 2*(strlen(this->dir) + 2) + strlen(host) + 3;
49b67f
 
49b67f
 			mapent = malloc(len);
49b67f
 			if (!mapent) {
49b67f
@@ -135,16 +124,16 @@ static char *get_exports(struct autofs_p
49b67f
 				return NULL;
49b67f
 			}
49b67f
 			strcpy(mapent, "\"");
49b67f
-			strcat(mapent, this->ex_dir);
49b67f
+			strcat(mapent, this->dir);
49b67f
 			strcat(mapent, "\"");
49b67f
 		}
49b67f
 		strcat(mapent, " \"");
49b67f
 		strcat(mapent, host);
49b67f
 		strcat(mapent, ":");
49b67f
-		strcat(mapent, this->ex_dir);
49b67f
+		strcat(mapent, this->dir);
49b67f
 		strcat(mapent, "\"");
49b67f
 
49b67f
-		this = this->ex_next;
49b67f
+		this = this->next;
49b67f
 	}
49b67f
 	rpc_exports_free(exp);
49b67f