walters / rpms / nfs-utils

Forked from rpms/nfs-utils 6 years ago
Clone
6201609
commit bb3e50bd5c4f6bf94221ef69d4dc87e73d0e474b
6201609
Author: Chuck Lever <chuck.lever@oracle.com>
6201609
Date:   Tue Nov 25 08:15:51 2008 -0500
6201609
6201609
    showmount command: call nfs_getport instead of local getport
6201609
    
6201609
    Have the showmount command invoke the shared nfs_getport() function
6201609
    instead of its own local version.  This gives the showmount command
6201609
    immediate support for querying via rpcbindv3/v4 in addition to
6201609
    portmapper, and sets the stage for AF_INET6 support in showmount.
6201609
    
6201609
    Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
6201609
    Signed-off-by: Steve Dickson <steved@redhat.com>
6201609
6201609
commit e358039c9ffa8a4ead342e8a0cf0ff51a3a21af4
6201609
Author: Chuck Lever <chuck.lever@oracle.com>
6201609
Date:   Tue Nov 25 08:35:10 2008 -0500
6201609
6201609
    showmount command: Remove unused local getport() implementation
6201609
    
6201609
    Clean up: remove showmount.c's local getport() implementation, now that
6201609
    the showmount command uses the shared one.
6201609
    
6201609
    Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
6201609
    Signed-off-by: Steve Dickson <steved@redhat.com>
6201609
6201609
commit 1c96846ba3adeb59a61e0cf33cf4c94c0678853f
6201609
Author: Chuck Lever <chuck.lever@oracle.com>
6201609
Date:   Tue Nov 25 08:38:01 2008 -0500
6201609
6201609
    showmount command: move logic to acquire RPC client	handle out of main()
6201609
    
6201609
    In preparation to support IPv6 in the showmount command, extract the
6201609
    logic that parses/acquires the target hostname and converts it into an RPC
6201609
    client handle to contact the remote mountd service, and move it into its
6201609
    own function.
6201609
    
6201609
    Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
6201609
    Signed-off-by: Steve Dickson <steved@redhat.com>
6201609
6201609
commit f7020bd5d3ffca280690c6beba5fecdeb4d305f7
6201609
Author: Chuck Lever <chuck.lever@oracle.com>
6201609
Date:   Tue Nov 25 08:39:47 2008 -0500
6201609
6201609
    showmount command: support querying IPv6 servers
6201609
    
6201609
    Introduce a version of nfs_get_mount_client() that supports AF_INET6 and
6201609
    AF_INET server addresses.  If the TI-RPC library is not available when
6201609
    the showmount command is built, fall back to the legacy RPC user-space
6201609
    API.
6201609
    
6201609
    Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
6201609
    Signed-off-by: Steve Dickson <steved@redhat.com>
6201609
6201609
diff -up nfs-utils-1.1.4/utils/showmount/showmount.c.save nfs-utils-1.1.4/utils/showmount/showmount.c
6201609
--- nfs-utils-1.1.4/utils/showmount/showmount.c.save	2008-11-25 11:16:03.000000000 -0500
6201609
+++ nfs-utils-1.1.4/utils/showmount/showmount.c	2008-11-25 11:16:28.000000000 -0500
6201609
@@ -37,8 +37,9 @@
6201609
 #include <mount.h>
6201609
 #include <unistd.h>
6201609
 
6201609
+#include "nfsrpc.h"
6201609
+
6201609
 #define TIMEOUT_UDP	3
6201609
-#define TIMEOUT_TCP	10
6201609
 #define TOTAL_TIMEOUT	20
6201609
 
6201609
 static char *	version = "showmount for " VERSION;
6201609
@@ -49,6 +50,13 @@ static int	aflag = 0;
6201609
 static int	dflag = 0;
6201609
 static int	eflag = 0;
6201609
 
6201609
+static const char *nfs_sm_pgmtbl[] = {
6201609
+	"showmount",
6201609
+	"mount",
6201609
+	"mountd",
6201609
+	NULL,
6201609
+};
6201609
+
6201609
 static struct option longopts[] =
6201609
 {
6201609
 	{ "all", 0, 0, 'a' },
6201609
@@ -77,6 +85,33 @@ static void usage(FILE *fp, int n)
6201609
 	exit(n);
6201609
 }
6201609
 
6201609
+#ifdef HAVE_CLNT_CREATE
6201609
+
6201609
+/*
6201609
+ * Generate an RPC client handle connected to the mountd service
6201609
+ * at @hostname, or die trying.
6201609
+ *
6201609
+ * Supports both AF_INET and AF_INET6 server addresses.
6201609
+ */
6201609
+static CLIENT *nfs_get_mount_client(const char *hostname)
6201609
+{
6201609
+	rpcprog_t program = nfs_getrpcbyname(MOUNTPROG, nfs_sm_pgmtbl);
6201609
+	CLIENT *client;
6201609
+
6201609
+	client = clnt_create(hostname, program, MOUNTVERS, "tcp");
6201609
+	if (client)
6201609
+		return client;
6201609
+
6201609
+	client = clnt_create(hostname, program, MOUNTVERS, "udp");
6201609
+	if (client)
6201609
+		return client;
6201609
+
6201609
+	clnt_pcreateerror("clnt_create");
6201609
+	exit(1);
6201609
+}
6201609
+
6201609
+#else	/* HAVE_CLNT_CREATE */
6201609
+
6201609
 /*
6201609
  *  Perform a non-blocking connect on the socket fd.
6201609
  *
6201609
@@ -149,120 +184,77 @@ done:
6201609
 	return ret;
6201609
 }
6201609
 
6201609
-static unsigned short getport(struct sockaddr_in *addr,
6201609
-			 unsigned long prog, unsigned long vers, int prot)
6201609
+/*
6201609
+ * Generate an RPC client handle connected to the mountd service
6201609
+ * at @hostname, or die trying.
6201609
+ *
6201609
+ * Supports only AF_INET server addresses.
6201609
+ */
6201609
+static CLIENT *nfs_get_mount_client(const char *hostname)
6201609
 {
6201609
-	CLIENT *client;
6201609
-	enum clnt_stat status;
6201609
-	struct pmap parms;
6201609
-	int ret, sock;
6201609
-	struct sockaddr_in laddr, saddr;
6201609
-	struct timeval tout = {0, 0};
6201609
-	socklen_t len;
6201609
-	unsigned int send_sz = 0;
6201609
-	unsigned int recv_sz = 0;
6201609
-	unsigned short port;
6201609
-
6201609
-	memset(&laddr, 0, sizeof(laddr));
6201609
-	memset(&saddr, 0, sizeof(saddr));
6201609
-	memset(&parms, 0, sizeof(parms));
6201609
-
6201609
-	memcpy(&saddr, addr, sizeof(saddr));
6201609
-	saddr.sin_port = htons(PMAPPORT);
6201609
+	struct hostent *hp;
6201609
+	struct sockaddr_in server_addr;
6201609
+	struct timeval pertry_timeout;
6201609
+	CLIENT *mclient = NULL;
6201609
+	int ret, msock;
6201609
 
6201609
-	if (prot == IPPROTO_TCP) {
6201609
-		sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
6201609
-		if (sock == -1) {
6201609
-			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
6201609
-			rpc_createerr.cf_error.re_errno = errno;
6201609
-			return 0;
6201609
+	if (inet_aton(hostname, &server_addr.sin_addr)) {
6201609
+		server_addr.sin_family = AF_INET;
6201609
+	}
6201609
+	else {
6201609
+		if ((hp = gethostbyname(hostname)) == NULL) {
6201609
+			fprintf(stderr, "%s: can't get address for %s\n",
6201609
+				program_name, hostname);
6201609
+			exit(1);
6201609
 		}
6201609
+		server_addr.sin_family = AF_INET;
6201609
+		memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
6201609
+	}
6201609
 
6201609
-		tout.tv_sec = TIMEOUT_TCP;
6201609
-
6201609
-		ret = connect_nb(sock, &saddr, &tout);
6201609
-		if (ret != 0) {
6201609
-			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
6201609
-			rpc_createerr.cf_error.re_errno = errno;
6201609
-			close(sock);
6201609
-			return 0;
6201609
-		}
6201609
-		client = clnttcp_create(&saddr,
6201609
-					PMAPPROG, PMAPVERS, &sock,
6201609
-					0, 0);
6201609
-	} else {
6201609
-		/*
6201609
-		 * bind to any unused port.  If we left this up to the rpc
6201609
-		 * layer, it would bind to a reserved port, which has been shown
6201609
-		 * to exhaust the reserved port range in some situations.
6201609
-		 */
6201609
-		sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
6201609
-		if (sock == -1) {
6201609
-			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
6201609
-			rpc_createerr.cf_error.re_errno = errno;
6201609
-			return 0;
6201609
-		}
6201609
-
6201609
-		laddr.sin_family = AF_INET;
6201609
-		laddr.sin_port = 0;
6201609
-		laddr.sin_addr.s_addr = htonl(INADDR_ANY);
6201609
-
6201609
-		tout.tv_sec = TIMEOUT_UDP;
6201609
-
6201609
-		send_sz = RPCSMALLMSGSIZE;
6201609
-		recv_sz = RPCSMALLMSGSIZE;
6201609
-
6201609
-		len = sizeof(struct sockaddr_in);
6201609
-		if (bind(sock, (struct sockaddr *)&laddr, len) < 0) {
6201609
-			close(sock);
6201609
-			sock = RPC_ANYSOCK;
6201609
-			/* FALLTHROUGH */
6201609
-		}
6201609
-		client = clntudp_bufcreate(&saddr, PMAPPROG, PMAPVERS,
6201609
-					   tout, &sock, send_sz, recv_sz);
6201609
-	}
6201609
-
6201609
-	if (!client) {
6201609
-		close(sock);
6201609
-		rpc_createerr.cf_stat = RPC_RPCBFAILURE;
6201609
-		return 0;
6201609
-	}
6201609
-
6201609
-	clnt_control(client, CLSET_FD_CLOSE, NULL);
6201609
-
6201609
-	parms.pm_prog = prog;
6201609
-	parms.pm_vers = vers;
6201609
-	parms.pm_prot = prot;
6201609
-
6201609
-	status = clnt_call(client, PMAPPROC_GETPORT,
6201609
-			   (xdrproc_t) xdr_pmap, (caddr_t) &parms,
6201609
-			   (xdrproc_t) xdr_u_short, (caddr_t) &port,
6201609
-			   tout);
6201609
-
6201609
-	if (status != RPC_SUCCESS) {
6201609
-		clnt_geterr(client, &rpc_createerr.cf_error);
6201609
-		rpc_createerr.cf_stat = status;
6201609
-		clnt_destroy(client);
6201609
-		return 0;
6201609
-	} else if (port == 0) {
6201609
-		rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
6201609
+	msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
6201609
+	if (msock != -1) {
6201609
+		if (nfs_getport_ping((struct sockaddr *)&server_addr,
6201609
+					sizeof(server_addr), MOUNTPROG,
6201609
+					MOUNTVERS, IPPROTO_TCP)) {
6201609
+			ret = connect_nb(msock, &server_addr, 0);
6201609
+			if (ret == 0)
6201609
+				mclient = clnttcp_create(&server_addr,
6201609
+						MOUNTPROG, MOUNTVERS, &msock,
6201609
+						0, 0);
6201609
+			else
6201609
+				close(msock);
6201609
+		} else
6201609
+			close(msock);
6201609
 	}
6201609
 
6201609
-	clnt_destroy(client);
6201609
+	if (!mclient) {
6201609
+		if (nfs_getport_ping((struct sockaddr *)&server_addr,
6201609
+					sizeof(server_addr), MOUNTPROG,
6201609
+					MOUNTVERS, IPPROTO_UDP)) {
6201609
+			clnt_pcreateerror("showmount");
6201609
+			exit(1);
6201609
+		}
6201609
+		msock = RPC_ANYSOCK;
6201609
+		pertry_timeout.tv_sec = TIMEOUT_UDP;
6201609
+		pertry_timeout.tv_usec = 0;
6201609
+		if ((mclient = clntudp_create(&server_addr,
6201609
+		    MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
6201609
+			clnt_pcreateerror("mount clntudp_create");
6201609
+			exit(1);
6201609
+		}
6201609
+	}
6201609
 
6201609
-	return htons(port);
6201609
+	return mclient;
6201609
 }
6201609
 
6201609
+#endif	/* HAVE_CLNT_CREATE */
6201609
+
6201609
 int main(int argc, char **argv)
6201609
 {
6201609
 	char hostname_buf[MAXHOSTLEN];
6201609
 	char *hostname;
6201609
 	enum clnt_stat clnt_stat;
6201609
-	struct hostent *hp;
6201609
-	struct sockaddr_in server_addr;
6201609
-	int ret, msock;
6201609
 	struct timeval total_timeout;
6201609
-	struct timeval pertry_timeout;
6201609
 	int c;
6201609
 	CLIENT *mclient;
6201609
 	groups grouplist;
6201609
@@ -334,54 +326,7 @@ int main(int argc, char **argv)
6201609
 		break;
6201609
 	}
6201609
 
6201609
-	if (inet_aton(hostname, &server_addr.sin_addr)) {
6201609
-		server_addr.sin_family = AF_INET;
6201609
-	}
6201609
-	else {
6201609
-		if ((hp = gethostbyname(hostname)) == NULL) {
6201609
-			fprintf(stderr, "%s: can't get address for %s\n",
6201609
-				program_name, hostname);
6201609
-			exit(1);
6201609
-		}
6201609
-		server_addr.sin_family = AF_INET;
6201609
-		memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
6201609
-	}
6201609
-
6201609
-	/* create mount deamon client */
6201609
-
6201609
-	mclient = NULL;
6201609
-	msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
6201609
-	if (msock != -1) {
6201609
-		server_addr.sin_port = getport(&server_addr,
6201609
-					 MOUNTPROG, MOUNTVERS, IPPROTO_TCP);
6201609
-		if (server_addr.sin_port) {
6201609
-			ret = connect_nb(msock, &server_addr, 0);
6201609
-			if (ret == 0) /* success */
6201609
-				mclient = clnttcp_create(&server_addr,
6201609
-						MOUNTPROG, MOUNTVERS, &msock,
6201609
-						0, 0);
6201609
-			else
6201609
-				close(msock);
6201609
-		} else
6201609
-			close(msock);
6201609
-	}
6201609
-
6201609
-	if (!mclient) {
6201609
-		server_addr.sin_port = getport(&server_addr,
6201609
-					 MOUNTPROG, MOUNTVERS, IPPROTO_UDP);
6201609
-		if (!server_addr.sin_port) {
6201609
-			clnt_pcreateerror("showmount");
6201609
-			exit(1);
6201609
-		}
6201609
-		msock = RPC_ANYSOCK;
6201609
-		pertry_timeout.tv_sec = TIMEOUT_UDP;
6201609
-		pertry_timeout.tv_usec = 0;
6201609
-		if ((mclient = clntudp_create(&server_addr,
6201609
-		    MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
6201609
-			clnt_pcreateerror("mount clntudp_create");
6201609
-			exit(1);
6201609
-		}
6201609
-	}
6201609
+	mclient = nfs_get_mount_client(hostname);
6201609
 	mclient->cl_auth = authunix_create_default();
6201609
 	total_timeout.tv_sec = TOTAL_TIMEOUT;
6201609
 	total_timeout.tv_usec = 0;