[PATCH] policy: Don't lose hostnames mapped to 127.0.0.1 and ::1



---
 src/nm-policy-hosts.c |   54 +++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/src/nm-policy-hosts.c b/src/nm-policy-hosts.c
index 0403b03..e7d20bd 100644
--- a/src/nm-policy-hosts.c
+++ b/src/nm-policy-hosts.c
@@ -63,6 +63,41 @@ is_local_mapping (const char *str, gboolean ip6, const char *hostname)
 	        && nm_policy_hosts_find_token (str, hostname ? hostname : fallback));
 }
 
+static void
+add_local_mapping (GString *contents, const char *line, gboolean ip6,
+        const char *fallback, const char *hostname, const char *short_hostname)
+{
+	const char *addr = ip6 ? "::1" : "127.0.0.1";
+	const char *short_fallback = ip6 ? "localhost6" : "localhost";
+	const char *start, *p;
+	gboolean add_line = FALSE;
+
+	start = line + strlen (addr);
+	p = start;
+	/* Walk through the line to find the next whitespace character */
+	while (p <= line + strlen (line)) {
+		if (isblank (*p) || (*p == '\0')) {
+			if ((p > start) && *start &&
+					/* Skip fallback and hostname tokens */
+					strncmp (start, short_fallback, p - start) &&
+					(!fallback || strncmp (start, fallback, p - start)) &&
+					(!short_hostname || strncmp (start, short_hostname, p - start)) &&
+					(!hostname || strncmp (start, hostname, p - start))) {
+				if (!add_line) {
+					g_string_append (contents, addr);
+					add_line = TRUE;
+				}
+				g_string_append_c (contents, '\t');
+				g_string_append_len (contents, start, p - start);
+			}
+			start = p + 1;
+		}
+		p++;
+	}
+	if (add_line)
+		g_string_append_c (contents, '\n');
+}
+
 static gboolean
 ip4_addr_matches (const char *str, const char *ip4_addr)
 {
@@ -283,18 +318,17 @@ nm_policy_get_etc_hosts (const char **lines,
 		}
 
 		/* Don't add the original line if it is a localhost mapping */
-		if (is_local_mapping (*line, FALSE, "localhost"))
-			add_line = FALSE;
-		else if (is_local_mapping (*line, FALSE, fallback_hostname4))
+		if (is_local_mapping (*line, FALSE, "localhost") ||
+				is_local_mapping (*line, FALSE, fallback_hostname4) ||
+				is_local_mapping (*line, FALSE, hostname)) {
+			add_local_mapping (contents, *line, FALSE, fallback_hostname4, hostname, short_hostname);
 			add_line = FALSE;
-		else if (is_local_mapping (*line, FALSE, hostname))
-			add_line = FALSE;
-		else if (is_local_mapping (*line, TRUE, "localhost6"))
-			add_line = FALSE;
-		else if (is_local_mapping (*line, TRUE, fallback_hostname6))
-			add_line = FALSE;
-		else if (is_local_mapping (*line, TRUE, hostname))
+		} else if (is_local_mapping (*line, TRUE, "localhost6") ||
+				is_local_mapping (*line, TRUE, fallback_hostname6) ||
+				is_local_mapping (*line, TRUE, hostname)) {
+			add_local_mapping (contents, *line, TRUE, fallback_hostname6, hostname, short_hostname);
 			add_line = FALSE;
+		}
 
 		if (add_line && !strstr (*line, ADDED_TAG)) {
 			g_string_append (contents, *line);
-- 
1.7.2.3



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]