File 0011-Clean-up-the-way-we-handle-the-h-option-in-init_tran.patch of Package rpcbind.12560

From 038db9589e4cf64b0a7307132d08bfa4547d59b1 Mon Sep 17 00:00:00 2001
From: Olaf Kirch <okir@suse.de>
Date: Tue, 20 Aug 2013 10:10:41 +0200
Subject: [PATCH 11/24] Clean up the way we handle the -h option in
 init_transport

There's some odd realloc()ing going on, which is plain ugly.
Make the code a little more readable.

Signed-off-by: Olaf Kirch <okir@suse.de>
---
 src/rpcbind.c | 66 +++++++++++++++++++++++++++--------------------------------
 1 file changed, 30 insertions(+), 36 deletions(-)

diff --git a/src/rpcbind.c b/src/rpcbind.c
index c3679e2..3b753c6 100644
--- a/src/rpcbind.c
+++ b/src/rpcbind.c
@@ -632,52 +632,46 @@ init_transport(struct netconfig *nconf)
 		return (1);
 	}
 
-	if (nconf->nc_semantics == NC_TPI_CLTS) {
-		int nhostsbak;
-		int checkbind;
+	/* Check if the -h option was used to specify addresses to bind to.
+	 * The original purpose was to allow multihomed hosts to function
+	 * properly, making the reply originate from the same IP address
+	 * that it was sent to. We're solving this differently in the meantime
+	 * (using PKTINFO magic in libtirpc), but there may be other uses for
+	 * this option, like restricting rpcbind to certain "public" interfaces
+	 */
+	if (nhosts != 0 && nconf->nc_semantics == NC_TPI_CLTS) {
+		int numbound = 0, n, r;
 
-		/*
-		 * If no hosts were specified, just bind to INADDR_ANY.  Otherwise
-		 * make sure 127.0.0.1 is added to the list.
-		 */
-		nhostsbak = nhosts;
-		nhostsbak++;
-		hosts = realloc(hosts, nhostsbak * sizeof(char *));
-		if (nhostsbak == 1)
-			hosts[0] = "*";
-		else {
-			if (si.si_af == AF_INET) {
-				hosts[nhostsbak - 1] = "127.0.0.1";
-			} else if (si.si_af == AF_INET6) {
-				hosts[nhostsbak - 1] = "::1";
-			} else
-				return 1;
-		}
+		/* Ensure that we always bind to loopback */
+		switch (si.si_af) {
+		case AF_INET:
+			if (rpcbind_init_endpoint(nconf, "127.0.0.1") > 0)
+				numbound++;
+			break;
 
-	       /*
-		* Bind to specific IPs if asked to
-		*/
-		checkbind = 0;
-		while (nhostsbak > 0) {
-			int r;
+		case AF_INET6:
+			if (rpcbind_init_endpoint(nconf, "::1") > 0)
+				numbound++;
+			break;
+		}
 
-			--nhostsbak;
+		for (n = 0; n < nhosts; ++n) {
+			const char *hostname = hosts[n];
 
-			/*
-			 * If no hosts were specified, just bind to INADDR_ANY
-			 */
-			if (strcmp("*", hosts[nhostsbak]) == 0)
-				hosts[nhostsbak] = NULL;
+			/* In case someone gets the idea to specify "-h '*'" */
+			if (strcmp("*", hostname) == 0)
+				hostname = NULL;
 
-			r = rpcbind_init_endpoint(nconf, hosts[nhostsbak]);
+			r = rpcbind_init_endpoint(nconf, hostname);
 			if (r < 0)
 				return 1;
 			if (r > 0)
-				checkbind = 1;
+				numbound++;
 		}
-		if (!checkbind)
+
+		if (numbound == 0)
 			return 1;
-	} else {	/* NC_TPI_COTS */
+	} else {
 		if (rpcbind_init_endpoint(nconf, NULL) <= 0)
 			return 1;
 	}
-- 
1.7.12.4

openSUSE Build Service is sponsored by