Blame SOURCES/autofs-5.0.8-fix-ipv6-libtirpc-getport.patch

4d476f
autofs-5.0.8 - fix ipv6 libtirpc getport
4d476f
4d476f
From: Ian Kent <ikent@redhat.com>
4d476f
4d476f
The method that was being used to obtain a service port number
4d476f
when using libtirpc was wrong.
4d476f
---
4d476f
 CHANGELOG      |    1 
4d476f
 lib/rpc_subs.c |  283 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
4d476f
 2 files changed, 267 insertions(+), 17 deletions(-)
4d476f
4d476f
--- autofs-5.0.7.orig/CHANGELOG
4d476f
+++ autofs-5.0.7/CHANGELOG
4d476f
@@ -64,6 +64,7 @@
4d476f
 - fix get_nfs_info() probe.
4d476f
 - fix portmap lookup.
4d476f
 - only probe specific nfs version if requested.
4d476f
+- fix ipv6 libtirpc getport.
4d476f
 
4d476f
 25/07/2012 autofs-5.0.7
4d476f
 =======================
4d476f
--- autofs-5.0.7.orig/lib/rpc_subs.c
4d476f
+++ autofs-5.0.7/lib/rpc_subs.c
4d476f
@@ -234,6 +234,28 @@ static int rpc_do_create_client(struct s
4d476f
 
4d476f
 	return 0;
4d476f
 }
4d476f
+static int rpc_getport(struct conn_info *info,
4d476f
+		       struct pmap *parms, CLIENT *client)
4d476f
+{
4d476f
+	enum clnt_stat status;
4d476f
+
4d476f
+	/*
4d476f
+	 * Check to see if server is up otherwise a getport will take
4d476f
+	 * forever to timeout.
4d476f
+	 */
4d476f
+	status = clnt_call(client, PMAPPROC_NULL,
4d476f
+			 (xdrproc_t) xdr_void, 0, (xdrproc_t) xdr_void, 0,
4d476f
+			 info->timeout);
4d476f
+
4d476f
+	if (status == RPC_SUCCESS) {
4d476f
+		status = clnt_call(client, PMAPPROC_GETPORT,
4d476f
+				 (xdrproc_t) xdr_pmap, (caddr_t) parms,
4d476f
+				 (xdrproc_t) xdr_u_short, (caddr_t) port,
4d476f
+				 info->timeout);
4d476f
+	}
4d476f
+
4d476f
+	return status;
4d476f
+}
4d476f
 #else
4d476f
 static int rpc_do_create_client(struct sockaddr *addr, struct conn_info *info, int *fd, CLIENT **client)
4d476f
 {
4d476f
@@ -267,9 +289,6 @@ static int rpc_do_create_client(struct s
4d476f
 		laddr = (struct sockaddr *) &in4_laddr;
4d476f
 		in4_raddr->sin_port = htons(info->port);
4d476f
 		slen = sizeof(struct sockaddr_in);
4d476f
-		/* Use rpcbind v2 for AF_INET */
4d476f
-		if (info->program == rpcb_prog)
4d476f
-			info->version = PMAPVERS;
4d476f
 	} else if (addr->sa_family == AF_INET6) {
4d476f
 		struct sockaddr_in6 *in6_raddr = (struct sockaddr_in6 *) addr;
4d476f
 		in6_laddr.sin6_family = AF_INET6;
4d476f
@@ -324,6 +343,244 @@ static int rpc_do_create_client(struct s
4d476f
 
4d476f
 	return 0;
4d476f
 }
4d476f
+
4d476f
+/*
4d476f
+ * Thankfully nfs-utils had already dealt with this.
4d476f
+ * Thanks to Chuck Lever for his nfs-utils patch series, much of
4d476f
+ * which is used here.
4d476f
+ */
4d476f
+static pthread_mutex_t proto_mutex = PTHREAD_MUTEX_INITIALIZER;
4d476f
+
4d476f
+static enum clnt_stat rpc_get_netid(const sa_family_t family,
4d476f
+				    const int protocol, char **netid)
4d476f
+{
4d476f
+	char *nc_protofmly, *nc_proto, *nc_netid;
4d476f
+	struct netconfig *nconf;
4d476f
+	struct protoent *proto;
4d476f
+	void *handle;
4d476f
+
4d476f
+	switch (family) {
4d476f
+	case AF_LOCAL:
4d476f
+	case AF_INET:
4d476f
+		nc_protofmly = NC_INET;
4d476f
+		break;
4d476f
+	case AF_INET6:
4d476f
+		nc_protofmly = NC_INET6;
4d476f
+		break;
4d476f
+	default:
4d476f
+		return RPC_UNKNOWNPROTO;
4d476f
+        }
4d476f
+
4d476f
+	pthread_mutex_lock(&proto_mutex);
4d476f
+	proto = getprotobynumber(protocol);
4d476f
+	if (!proto) {
4d476f
+		pthread_mutex_unlock(&proto_mutex);
4d476f
+		return RPC_UNKNOWNPROTO;
4d476f
+	}
4d476f
+	nc_proto = strdup(proto->p_name);
4d476f
+	pthread_mutex_unlock(&proto_mutex);
4d476f
+	if (!nc_proto)
4d476f
+		return RPC_SYSTEMERROR;
4d476f
+
4d476f
+	handle = setnetconfig();
4d476f
+	while ((nconf = getnetconfig(handle)) != NULL) {
4d476f
+		if (nconf->nc_protofmly != NULL &&
4d476f
+		    strcmp(nconf->nc_protofmly, nc_protofmly) != 0)
4d476f
+			continue;
4d476f
+		if (nconf->nc_proto != NULL &&
4d476f
+		    strcmp(nconf->nc_proto, nc_proto) != 0)
4d476f
+			continue;
4d476f
+
4d476f
+		nc_netid = strdup(nconf->nc_netid);
4d476f
+		if (!nc_netid) {
4d476f
+			free(nc_proto);
4d476f
+			return RPC_SYSTEMERROR;
4d476f
+		}
4d476f
+
4d476f
+		*netid = nc_netid;
4d476f
+	}
4d476f
+	endnetconfig(handle);
4d476f
+	free(nc_proto);
4d476f
+
4d476f
+	return RPC_SUCCESS;
4d476f
+}
4d476f
+
4d476f
+static char *rpc_sockaddr2universal(const struct sockaddr *addr)
4d476f
+{
4d476f
+	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *) addr;
4d476f
+	const struct sockaddr_un *sun = (const struct sockaddr_un *) addr;
4d476f
+	const struct sockaddr_in *sin = (const struct sockaddr_in *) addr;
4d476f
+	char buf[INET6_ADDRSTRLEN + 8 /* for port information */];
4d476f
+	uint16_t port;
4d476f
+	size_t count;
4d476f
+	char *result;
4d476f
+	int len;
4d476f
+
4d476f
+	switch (addr->sa_family) {
4d476f
+	case AF_LOCAL:
4d476f
+		return strndup(sun->sun_path, sizeof(sun->sun_path));
4d476f
+	case AF_INET:
4d476f
+		if (inet_ntop(AF_INET, (const void *)&sin->sin_addr.s_addr,
4d476f
+					buf, (socklen_t)sizeof(buf)) == NULL)
4d476f
+			goto out_err;
4d476f
+		port = ntohs(sin->sin_port);
4d476f
+		break;
4d476f
+	case AF_INET6:
4d476f
+		if (inet_ntop(AF_INET6, (const void *)&sin6->sin6_addr,
4d476f
+					buf, (socklen_t)sizeof(buf)) == NULL)
4d476f
+			goto out_err;
4d476f
+		port = ntohs(sin6->sin6_port);
4d476f
+		break;
4d476f
+	default:
4d476f
+		goto out_err;
4d476f
+	}
4d476f
+
4d476f
+	count = sizeof(buf) - strlen(buf);
4d476f
+	len = snprintf(buf + strlen(buf), count, ".%u.%u",
4d476f
+			(unsigned)(port >> 8), (unsigned)(port & 0xff));
4d476f
+	/* before glibc 2.0.6, snprintf(3) could return -1 */
4d476f
+	if (len < 0 || (size_t)len > count)
4d476f
+		goto out_err;
4d476f
+
4d476f
+	result = strdup(buf);
4d476f
+	return result;
4d476f
+
4d476f
+out_err:
4d476f
+        return NULL;
4d476f
+}
4d476f
+
4d476f
+static int rpc_universal2port(const char *uaddr)
4d476f
+{
4d476f
+	char *addrstr;
4d476f
+	char *p, *endptr;
4d476f
+	unsigned long portlo, porthi;
4d476f
+	int port = -1;
4d476f
+
4d476f
+	addrstr = strdup(uaddr);
4d476f
+	if (!addrstr)
4d476f
+		return -1;
4d476f
+
4d476f
+	p = strrchr(addrstr, '.');
4d476f
+	if (!p)
4d476f
+		goto out;
4d476f
+
4d476f
+	portlo = strtoul(p + 1, &endptr, 10);
4d476f
+	if (*endptr != '\0' || portlo > 255)
4d476f
+		goto out;
4d476f
+	*p = '\0';
4d476f
+
4d476f
+        p = strrchr(addrstr, '.');
4d476f
+        if (!p)
4d476f
+                goto out;
4d476f
+
4d476f
+        porthi = strtoul(p + 1, &endptr, 10);
4d476f
+        if (*endptr != '\0' || porthi > 255)
4d476f
+                goto out;
4d476f
+        *p = '\0';
4d476f
+
4d476f
+        port = (porthi << 8) | portlo;
4d476f
+
4d476f
+out:
4d476f
+	free(addrstr);
4d476f
+	return port;
4d476f
+}
4d476f
+
4d476f
+static enum clnt_stat rpc_rpcb_getport(CLIENT *client,
4d476f
+				       struct rpcb *parms,
4d476f
+				       struct timeval timeout,
4d476f
+				       unsigned short *port)
4d476f
+{
4d476f
+	rpcvers_t rpcb_version;
4d476f
+	struct rpc_err rpcerr;
4d476f
+	int s_port = 0;
4d476f
+
4d476f
+	for (rpcb_version = RPCBVERS_4;
4d476f
+	     rpcb_version >= RPCBVERS_3;
4d476f
+	     rpcb_version--) {
4d476f
+		enum clnt_stat status;
4d476f
+		char *uaddr = NULL;
4d476f
+
4d476f
+		CLNT_CONTROL(client, CLSET_VERS, (void *) &rpcb_version);
4d476f
+		status = CLNT_CALL(client, (rpcproc_t) RPCBPROC_GETADDR,
4d476f
+				  (xdrproc_t) xdr_rpcb, (void *) parms,
4d476f
+				  (xdrproc_t) xdr_wrapstring, (void *) &uaddr,
4d476f
+				  timeout);
4d476f
+
4d476f
+		switch (status) {
4d476f
+		case RPC_SUCCESS:
4d476f
+			if ((uaddr == NULL) || (uaddr[0] == '\0'))
4d476f
+				return RPC_PROGNOTREGISTERED;
4d476f
+
4d476f
+			s_port = rpc_universal2port(uaddr);
4d476f
+			xdr_free((xdrproc_t) xdr_wrapstring, (char *) &uaddr);
4d476f
+			if (s_port == -1) {
4d476f
+				return RPC_N2AXLATEFAILURE;
4d476f
+			}
4d476f
+			*port = s_port;
4d476f
+			return RPC_SUCCESS;
4d476f
+
4d476f
+		case RPC_PROGVERSMISMATCH:
4d476f
+			clnt_geterr(client, &rpcerr);
4d476f
+			if (rpcerr.re_vers.low > RPCBVERS4)
4d476f
+				return status;
4d476f
+			continue;
4d476f
+		case RPC_PROCUNAVAIL:
4d476f
+		case RPC_PROGUNAVAIL:
4d476f
+			continue;
4d476f
+		default:
4d476f
+                        /* Most likely RPC_TIMEDOUT or RPC_CANTRECV */
4d476f
+			return status;
4d476f
+		}
4d476f
+	}
4d476f
+
4d476f
+        if (s_port == 0)
4d476f
+		return RPC_PROGNOTREGISTERED;
4d476f
+
4d476f
+        return RPC_PROCUNAVAIL;
4d476f
+}
4d476f
+
4d476f
+static enum clnt_stat rpc_getport(struct conn_info *info,
4d476f
+				  struct pmap *parms, CLIENT *client,
4d476f
+				  unsigned short *port)
4d476f
+{
4d476f
+	enum clnt_stat status;
4d476f
+	struct sockaddr *paddr, addr;
4d476f
+	struct rpcb rpcb_parms;
4d476f
+	char *netid, *raddr;
4d476f
+
4d476f
+	if (info->addr)
4d476f
+		paddr = info->addr;
4d476f
+	else {
4d476f
+		if (!clnt_control(client, CLGET_SERVER_ADDR, (char *) &addr))
4d476f
+			return RPC_UNKNOWNADDR;
4d476f
+		paddr = &addr;
4d476f
+	}
4d476f
+
4d476f
+	netid = NULL;
4d476f
+	status = rpc_get_netid(paddr->sa_family, info->proto, &netid);
4d476f
+	if (status != RPC_SUCCESS)
4d476f
+		return status;
4d476f
+
4d476f
+	raddr = rpc_sockaddr2universal(paddr);
4d476f
+	if (!raddr) {
4d476f
+		free(netid);
4d476f
+		return RPC_UNKNOWNADDR;
4d476f
+	}
4d476f
+
4d476f
+	memset(&rpcb_parms, 0, sizeof(rpcb_parms));
4d476f
+	rpcb_parms.r_prog   = parms->pm_prog;
4d476f
+	rpcb_parms.r_vers   = parms->pm_vers;
4d476f
+	rpcb_parms.r_netid  = netid;
4d476f
+	rpcb_parms.r_addr   = raddr;
4d476f
+	rpcb_parms.r_owner  = "";
4d476f
+
4d476f
+	status = rpc_rpcb_getport(client, &rpcb_parms, info->timeout, port);
4d476f
+
4d476f
+	free(netid);
4d476f
+	free(raddr);
4d476f
+
4d476f
+	return status;
4d476f
+}
4d476f
 #endif
4d476f
 
4d476f
 #if defined(HAVE_GETRPCBYNAME) || defined(HAVE_GETSERVBYNAME)
4d476f
@@ -647,20 +904,7 @@ int rpc_portmap_getport(struct conn_info
4d476f
 			return ret;
4d476f
 	}
4d476f
 
4d476f
-	/*
4d476f
-	 * Check to see if server is up otherwise a getport will take
4d476f
-	 * forever to timeout.
4d476f
-	 */
4d476f
-	status = clnt_call(client, PMAPPROC_NULL,
4d476f
-			 (xdrproc_t) xdr_void, 0, (xdrproc_t) xdr_void, 0,
4d476f
-			 pmap_info.timeout);
4d476f
-
4d476f
-	if (status == RPC_SUCCESS) {
4d476f
-		status = clnt_call(client, PMAPPROC_GETPORT,
4d476f
-				 (xdrproc_t) xdr_pmap, (caddr_t) parms,
4d476f
-				 (xdrproc_t) xdr_u_short, (caddr_t) port,
4d476f
-				 pmap_info.timeout);
4d476f
-	}
4d476f
+	status = rpc_getport(&pmap_info, parms, client, port);
4d476f
 
4d476f
 	if (!info->client) {
4d476f
 		/*
4d476f
@@ -867,6 +1111,11 @@ static int rpc_get_exports_proto(struct
4d476f
 	clnt_control(client, CLSET_RETRY_TIMEOUT, (char *) &info->timeout);
4d476f
 
4d476f
 	client->cl_auth = authunix_create_default();
4d476f
+	if (client->cl_auth == NULL) {
4d476f
+		error(LOGOPT_ANY, "auth create failed");
4d476f
+		clnt_destroy(client);
4d476f
+		return 0;
4d476f
+	}
4d476f
 
4d476f
 	vers_entry = 0;
4d476f
 	while (1) {