diff options
Diffstat (limited to 'network/NetworkManager/patches/policy-stop-touching-etc-hosts.patch')
-rw-r--r-- | network/NetworkManager/patches/policy-stop-touching-etc-hosts.patch | 1703 |
1 files changed, 1703 insertions, 0 deletions
diff --git a/network/NetworkManager/patches/policy-stop-touching-etc-hosts.patch b/network/NetworkManager/patches/policy-stop-touching-etc-hosts.patch new file mode 100644 index 0000000000..7591479091 --- /dev/null +++ b/network/NetworkManager/patches/policy-stop-touching-etc-hosts.patch @@ -0,0 +1,1703 @@ +From 6fe0413366fc02ba3ffdd24db670852bc134110f Mon Sep 17 00:00:00 2001 +From: Dan Williams <dcbw@redhat.com> +Date: Fri, 17 Dec 2010 09:22:11 -0600 +Subject: [PATCH 1/8] policy: stop touching /etc/hosts + +Handling of /etc/hosts is highly site- and admin- specific in +many more complex cases, and it's exceedingly hard and error- +prone for NetworkManager to handle all those cases. So remove +this functionality entirely. That's not a big loss, as it +turns out there's a much more elegant solution. + +The only requirement is that the machine's hostname map back +to an IP address owned by the machine. That requirement can +be satisifed by nss-myhostname or even possibly the distro's +installer. If the user does not want nss-myhostname then it +can be uninstalled. Distros should use a "recommends" feature +in their packaging system so that the NetworkManager package +does *not* have a hard requirement on nss-myhostname. Thus +everyone is happy; things Just Work when nss-myhostname is +installed, but more advanced users can uninstall it and +customize /etc/hosts as they wish. + +Another alternative is a dispatcher script that listents for +the 'hostname' event, and updates /etc/hosts according to the +administrator's preference. +--- + src/main.c | 4 + + src/nm-policy-hostname.c | 56 +--- + src/nm-policy-hostname.h | 5 +- + src/nm-policy-hosts.c | 520 ++-------------------------- + src/nm-policy-hosts.h | 20 +- + src/nm-policy.c | 42 +--- + src/tests/test-policy-hosts.c | 761 ++--------------------------------------- + 7 files changed, 76 insertions(+), 1332 deletions(-) + +diff --git a/src/main.c b/src/main.c +index 7e75f05..2db7345 100644 +--- a/src/main.c ++++ b/src/main.c +@@ -49,6 +49,7 @@ + #include "nm-netlink-monitor.h" + #include "nm-vpn-manager.h" + #include "nm-logging.h" ++#include "nm-policy-hosts.h" + + #if !defined(NM_DIST_VERSION) + # define NM_DIST_VERSION VERSION +@@ -713,6 +714,9 @@ main (int argc, char *argv[]) + goto done; + } + ++ /* Clean leftover "# Added by NetworkManager" entries from /etc/hosts */ ++ nm_policy_hosts_clean_etc_hosts (); ++ + nm_manager_start (manager); + + /* Bring up the loopback interface. */ +diff --git a/src/nm-policy-hostname.c b/src/nm-policy-hostname.c +index 42a2e0f..4fe69c5 100644 +--- a/src/nm-policy-hostname.c ++++ b/src/nm-policy-hostname.c +@@ -30,7 +30,6 @@ + + #include "nm-logging.h" + #include "nm-policy-hostname.h" +-#include "nm-policy-hosts.h" + + /************************************************************************/ + +@@ -206,74 +205,39 @@ hostname_thread_is_dead (HostnameThread *ht) + /************************************************************************/ + + #define FALLBACK_HOSTNAME4 "localhost.localdomain" +-#define FALLBACK_HOSTNAME6 "localhost6.localdomain6" + + gboolean +-nm_policy_set_system_hostname (const char *new_hostname, +- const char *ip4_addr, +- const char *ip6_addr, +- const char *msg) ++nm_policy_set_system_hostname (const char *new_hostname, const char *msg) + { + char old_hostname[HOST_NAME_MAX + 1]; +- int ret = 0; + const char *name; +- gboolean set_hostname = TRUE, changed = FALSE, old_valid = TRUE; ++ int ret; + + if (new_hostname) + g_warn_if_fail (strlen (new_hostname)); + +- name = (new_hostname && strlen (new_hostname)) ? new_hostname : FALLBACK_HOSTNAME4; +- + old_hostname[HOST_NAME_MAX] = '\0'; + errno = 0; + ret = gethostname (old_hostname, HOST_NAME_MAX); + if (ret != 0) { + nm_log_warn (LOGD_DNS, "couldn't get the system hostname: (%d) %s", + errno, strerror (errno)); +- old_valid = FALSE; + } else { + /* Don't set the hostname if it isn't actually changing */ + if ( (new_hostname && !strcmp (old_hostname, new_hostname)) + || (!new_hostname && !strcmp (old_hostname, FALLBACK_HOSTNAME4))) +- set_hostname = FALSE; +- +- if (old_hostname[0] == '\0') +- old_valid = FALSE; +- } +- +- if (set_hostname) { +- nm_log_info (LOGD_DNS, "Setting system hostname to '%s' (%s)", name, msg); +- ret = sethostname (name, strlen (name)); +- if (ret != 0) { +- nm_log_warn (LOGD_DNS, "couldn't set the system hostname to '%s': (%d) %s", +- name, errno, strerror (errno)); + return FALSE; +- } + } + +- /* But even if the hostname isn't changing, always try updating /etc/hosts +- * just in case the hostname changed while NM wasn't running; we need to +- * make sure that /etc/hosts has valid mappings for '127.0.0.1' and the +- * current system hostname. If those exist, +- * nm_policy_hosts_update_etc_hosts() will just return and won't touch +- * /etc/hosts at all. +- */ +- if (!nm_policy_hosts_update_etc_hosts (name, +- old_valid ? old_hostname : NULL, +- FALLBACK_HOSTNAME4, +- FALLBACK_HOSTNAME6, +- ip4_addr, +- ip6_addr, +- &changed)) { +- /* error updating /etc/hosts; fallback to localhost.localdomain */ +- nm_log_info (LOGD_DNS, "Setting system hostname to '" FALLBACK_HOSTNAME4 "' (error updating /etc/hosts)"); +- ret = sethostname (FALLBACK_HOSTNAME4, strlen (FALLBACK_HOSTNAME4)); +- if (ret != 0) { +- nm_log_warn (LOGD_DNS, "couldn't set the fallback system hostname (%s): (%d) %s", +- FALLBACK_HOSTNAME4, errno, strerror (errno)); +- } ++ name = (new_hostname && strlen (new_hostname)) ? new_hostname : FALLBACK_HOSTNAME4; ++ ++ nm_log_info (LOGD_DNS, "Setting system hostname to '%s' (%s)", name, msg); ++ ret = sethostname (name, strlen (name)); ++ if (ret != 0) { ++ nm_log_warn (LOGD_DNS, "couldn't set the system hostname to '%s': (%d) %s", ++ name, errno, strerror (errno)); + } + +- return changed; ++ return (ret == 0); + } + +diff --git a/src/nm-policy-hostname.h b/src/nm-policy-hostname.h +index 9c76884..e76713f 100644 +--- a/src/nm-policy-hostname.h ++++ b/src/nm-policy-hostname.h +@@ -24,10 +24,7 @@ + + #include <glib.h> + +-gboolean nm_policy_set_system_hostname (const char *new_hostname, +- const char *ip4_addr, +- const char *ip6_addr, +- const char *msg); ++gboolean nm_policy_set_system_hostname (const char *new_hostname, const char *msg); + + + typedef struct HostnameThread HostnameThread; +diff --git a/src/nm-policy-hosts.c b/src/nm-policy-hosts.c +index 7f9cff8..8bbd1d3 100644 +--- a/src/nm-policy-hosts.c ++++ b/src/nm-policy-hosts.c +@@ -20,526 +20,74 @@ + + #include <config.h> + #include <string.h> +-#include <unistd.h> +-#include <errno.h> +-#include <netdb.h> +-#include <ctype.h> +-#include <arpa/inet.h> + + #include "nm-policy-hosts.h" + #include "nm-logging.h" + +-#define IP4_LH "127.0.0.1" +-#define IP6_LH "::1" +- +-gboolean +-nm_policy_hosts_find_token (const char *line, const char *token) +-{ +- const char *start = line, *p = line; +- +- g_return_val_if_fail (line != NULL, FALSE); +- g_return_val_if_fail (token != NULL, FALSE); +- g_return_val_if_fail (strlen (token) > 0, FALSE); +- +- /* Walk through the line to find the next whitespace character */ +- while (p <= line + strlen (line)) { +- if (isblank (*p) || (*p == '\0')) { +- /* Token starts with 'start' and ends with 'end' */ +- if ((p > start) && *start && (p - start == strlen (token)) && !strncmp (start, token, (p - start))) +- return TRUE; /* found */ +- +- /* not found; advance start and continue looking */ +- start = p + 1; +- } +- p++; +- } +- +- return FALSE; +-} +- +-static gboolean +-is_local_mapping (const char *str, gboolean ip6, const char *hostname) +-{ +- const char *addr = ip6 ? IP6_LH : IP4_LH; +- const char *fallback = ip6 ? "localhost6" : "localhost"; +- +- return ( !strncmp (str, addr, strlen (addr)) +- && nm_policy_hosts_find_token (str, hostname ? hostname : fallback)); +-} +- +-static gboolean +-is_ip4_addr (const char *str) +-{ +- struct in_addr found; +- char buf[INET_ADDRSTRLEN + 2]; +- const char *p = str; +- guint32 i = 0; +- +- memset (buf, 0, sizeof (buf)); +- while (*p && !isblank (*p) && (i < sizeof (buf))) +- buf[i++] = *p++; +- +- return inet_pton (AF_INET, buf, &found) == 1 ? TRUE : FALSE; +-} +- +-static gboolean +-ip4_addr_matches (const char *str, const char *ip4_addr) +-{ +- struct in_addr found, given; +- char buf[INET_ADDRSTRLEN + 2]; +- const char *p = str; +- guint32 i = 0; +- +- g_return_val_if_fail (ip4_addr != NULL, FALSE); +- +- memset (buf, 0, sizeof (buf)); +- while (*p && !isblank (*p) && (i < sizeof (buf))) +- buf[i++] = *p++; +- +- if (inet_pton (AF_INET, buf, &found) != 1) +- return FALSE; +- if (inet_pton (AF_INET, ip4_addr, &given) != 1) +- return FALSE; +- +- return memcmp (&found, &given, sizeof (found)) == 0; +-} +- +-static gboolean +-is_ip6_addr (const char *str) +-{ +- struct in6_addr found; +- char buf[INET6_ADDRSTRLEN + 2]; +- const char *p = str; +- guint32 i = 0; +- +- memset (buf, 0, sizeof (buf)); +- while (*p && !isblank (*p) && (i < sizeof (buf))) +- buf[i++] = *p++; +- +- return inet_pton (AF_INET6, buf, &found) == 1 ? TRUE : FALSE; +-} +- +-static gboolean +-ip6_addr_matches (const char *str, const char *ip6_addr) +-{ +- struct in6_addr found, given; +- char buf[INET6_ADDRSTRLEN + 2]; +- const char *p = str; +- guint32 i = 0; +- +- g_return_val_if_fail (ip6_addr != NULL, FALSE); +- +- memset (buf, 0, sizeof (buf)); +- while (*p && !isblank (*p) && (i < sizeof (buf))) +- buf[i++] = *p++; +- +- if (inet_pton (AF_INET6, buf, &found) != 1) +- return FALSE; +- if (inet_pton (AF_INET6, ip6_addr, &given) != 1) +- return FALSE; +- +- return memcmp (&found, &given, sizeof (found)) == 0; +-} +- +-static char * +-get_custom_hostnames (const char *line, +- const char *hostname, +- const char *old_hostname, +- const char *short_hostname, +- const char *fallback_hostname) +-{ +- char **items = NULL, **iter; +- guint start = 0; +- GString *str = NULL; +- char *custom = NULL; +- +- g_return_val_if_fail (line != NULL, NULL); +- +- if (!strncmp (line, IP4_LH, strlen (IP4_LH))) +- start = strlen (IP4_LH); +- else if (!strncmp (line, IP6_LH, strlen (IP6_LH))) +- start = strlen (IP6_LH); +- +- g_return_val_if_fail (start > 0, NULL); +- +- /* Split the line into tokens */ +- items = g_strsplit_set (line + start, " \t", -1); +- if (!items) +- return NULL; +- +- str = g_string_sized_new (50); +- /* Ignore current & old hostnames, and localhost-anything */ +- for (iter = items; iter && *iter; iter++) { +- if (*iter[0] == '\0') +- continue; +- if (hostname && !strcmp (*iter, hostname)) +- continue; +- if (old_hostname && !strcmp (*iter, old_hostname)) +- continue; +- if (short_hostname && !strcmp (*iter, short_hostname)) +- continue; +- if (fallback_hostname && !strcmp (*iter, fallback_hostname)) +- continue; +- if (!strcmp (*iter, "localhost")) +- continue; +- if (!strcmp (*iter, "localhost6")) +- continue; +- if (!strcmp (*iter, "localhost.localdomain")) +- continue; +- if (!strcmp (*iter, "localhost4.localdomain4")) +- continue; +- if (!strcmp (*iter, "localhost6.localdomain6")) +- continue; +- +- /* Found a custom hostname */ +- g_string_append_c (str, '\t'); +- g_string_append (str, *iter); +- } +- +- if (str->len) +- custom = g_string_free (str, FALSE); +- else +- g_string_free (str, TRUE); +- +- g_strfreev (items); +- return custom; +-} +- + #define ADDED_TAG "# Added by NetworkManager" + + GString * +-nm_policy_get_etc_hosts (const char **lines, +- gsize existing_len, +- const char *hostname, +- const char *old_hostname, +- const char *fallback_hostname4, +- const char *fallback_hostname6, +- const char *ip4_addr, +- const char *ip6_addr, +- GError **error) ++nm_policy_get_etc_hosts (const char *contents, gsize contents_len) + { +- GString *contents = NULL; +- const char **line; +- gboolean found_localhost4 = FALSE; +- gboolean found_localhost6 = FALSE; +- gboolean found_host4 = FALSE; +- gboolean found_host6 = FALSE; +- gboolean found_user_host4 = FALSE; +- gboolean found_user_host6 = FALSE; +- gboolean initial_comments = TRUE; +- gboolean added = FALSE; +- gboolean hostname4_is_fallback; +- gboolean hostname6_is_fallback; +- gboolean host4_before = FALSE; +- gboolean host6_before = FALSE; +- gboolean no_stale = TRUE; +- char *short_hostname = NULL; +- char *custom4 = NULL; +- char *custom6 = NULL; +- +- g_return_val_if_fail (lines != NULL, FALSE); +- g_return_val_if_fail (hostname != NULL, FALSE); +- +- hostname4_is_fallback = !strcmp (hostname, fallback_hostname4); +- hostname6_is_fallback = !strcmp (hostname, fallback_hostname6); +- +- /* Find the short hostname, like 'foo' from 'foo.bar.baz'; we want to +- * make sure that the entries we add for this host also include the short +- * hostname too so that if the resolver does not answer queries for the +- * machine's actual hostname/domain, that stuff like 'ping foo' still works. +- */ +- if (!hostname4_is_fallback || !hostname6_is_fallback) { +- char *dot; +- +- short_hostname = g_strdup (hostname); +- dot = strchr (short_hostname, '.'); +- if (dot && *(dot+1)) +- *dot = '\0'; +- else { +- g_free (short_hostname); +- short_hostname = NULL; +- } +- } +- +- /* We need the following in /etc/hosts: +- * +- * 1) current hostname mapped to current IPv4 addresses if IPv4 is active +- * 2) current hostname mapped to current IPv6 addresses if IPv6 is active +- * 3) 'localhost' mapped to 127.0.0.1 +- * 4) 'localhost6' mapped to ::1 +- * +- * If all these things exist we don't need to bother updating the file. +- */ +- +- if (!ip4_addr) +- host4_before = TRUE; +- if (!ip6_addr) +- host6_before = TRUE; +- +- /* Look for the four cases from above */ +- for (line = lines; lines && *line; line++) { +- gboolean found_hostname = FALSE; +- +- if ((*line[0] == '\0') || (*line[0] == '#')) +- continue; ++ char **lines = NULL, **iter; ++ GString *new_contents = NULL; + +- found_hostname = nm_policy_hosts_find_token (*line, hostname); +- if (found_hostname) { +- /* Found the current hostname on this line */ +- if (ip4_addr && ip4_addr_matches (*line, ip4_addr)) { +- found_host4 = TRUE; +- if (strstr (*line, ADDED_TAG)) { +- if (!host4_before) +- host4_before = !found_localhost4; +- } else { +- found_user_host4 = TRUE; +- host4_before = TRUE; /* Ignore if user added mapping manually */ +- } +- } else if (!ip4_addr && strstr (*line, ADDED_TAG)) { +- /* If this is a stale NM-added IPv4 entry we need to remove it, +- * so make sure we update /etc/hosts. +- */ +- if (is_ip4_addr (*line)) +- no_stale = FALSE; +- } ++ if (contents_len == 0 || !strstr (contents, ADDED_TAG)) ++ return NULL; + +- if (ip6_addr && ip6_addr_matches (*line, ip6_addr)) { +- found_host6 = TRUE; +- if (strstr (*line, ADDED_TAG)) { +- if (!host6_before) +- host6_before = !found_localhost6; +- } else { +- found_user_host6 = TRUE; +- host6_before = TRUE; /* Ignore if user added mapping manually */ +- } +- } else if (!ip6_addr && strstr (*line, ADDED_TAG)) { +- /* If this is a stale NM-added IPv6 entry we need to remove it, +- * so make sure we update /etc/hosts. +- */ +- if (is_ip6_addr (*line)) +- no_stale = FALSE; +- } +- } ++ new_contents = g_string_sized_new (contents_len); + +- if (is_local_mapping (*line, FALSE, "localhost")) { +- /* a 127.0.0.1 line containing 'localhost' */ +- found_localhost4 = TRUE; +- custom4 = get_custom_hostnames (*line, hostname, old_hostname, short_hostname, fallback_hostname4); +- if (!ip4_addr) { +- /* If there's no IP-specific mapping for the current hostname +- * but that hostname is present on in the local mapping line, +- * we've found our IPv4 hostname mapping. If the hostname is +- * the fallback *IPv6* hostname it's not going to show up in +- * the IPv4 local mapping though, so fake it. +- */ +- if (hostname6_is_fallback || found_hostname) +- found_host4 = TRUE; +- } +- } else if (is_local_mapping (*line, TRUE, "localhost6")) { +- /* a ::1 line containing 'localhost6' */ +- found_localhost6 = TRUE; +- custom6 = get_custom_hostnames (*line, hostname, old_hostname, short_hostname, fallback_hostname6); +- if (!ip6_addr) { +- /* If there's no IP-specific mapping for the current hostname +- * but that hostname is present on in the local mapping line, +- * we've found our IPv6 hostname mapping. If the hostname is +- * the fallback *IPv4* hostname it's not going to show up in +- * the IPv6 local mapping though, so fake it. +- */ +- if (hostname4_is_fallback || found_hostname) +- found_host6 = TRUE; +- } ++ /* Remove "# Added ..." lines */ ++ lines = g_strsplit_set (contents, "\n\r", -1); ++ for (iter = lines; iter && *iter; iter++) { ++ if (!strstr (*iter, ADDED_TAG)) { ++ g_string_append (new_contents, *iter); ++ g_string_append_c (new_contents, '\n'); + } +- +- if ( found_localhost4 +- && found_host4 +- && found_localhost6 +- && found_host6 +- && host4_before +- && host6_before +- && no_stale) +- goto out; /* No update required */ +- } +- +- contents = g_string_sized_new (existing_len ? existing_len + 100 : 200); +- if (!contents) { +- g_set_error_literal (error, 0, 0, "not enough memory"); +- goto out; + } ++ g_strfreev (lines); + +- /* Construct the new hosts file; replace any 127.0.0.1/::1 entry that is +- * at the beginning of the file or right after initial comments and contains +- * the string 'localhost' (for IPv4) or 'localhost6' (for IPv6). If there +- * is no 127.0.0.1 or ::1 entry at the beginning or after initial comments +- * that contains 'localhost' or 'localhost6', add one there +- * and ignore any other 127.0.0.1/::1 entries that contain 'localhost' or +- * 'localhost6'. ++ /* Remove last blank line at end of file, if one exists; this is ++ * an artifact of how g_strsplit_set() works. + */ +- for (line = lines, initial_comments = TRUE; lines && *line; line++) { +- /* This is the first line after the initial comments */ +- if (strlen (*line) && initial_comments && (*line[0] != '#')) { +- initial_comments = FALSE; +- +- /* If the user added their own mapping for the hostname, just make +- * a simple 'localhost' mapping and assume the user knows what they +- * are doing with their manual hostname entry. Otherwise if the +- * hostname wasn't found somewhere else, add it to the localhost +- * mapping line to make sure it's mapped to something. +- */ +- +- /* Add the address mappings first so they take precedence */ +- if (!hostname4_is_fallback && ip4_addr && !found_user_host4) { +- g_string_append_printf (contents, "%s\t%s", ip4_addr, hostname); +- if (short_hostname) +- g_string_append_printf (contents, "\t%s", short_hostname); +- g_string_append_printf (contents, "\t%s\n", ADDED_TAG); +- } +- if (!hostname6_is_fallback && ip6_addr && !found_user_host6) { +- g_string_append_printf (contents, "%s\t%s", ip6_addr, hostname); +- if (short_hostname) +- g_string_append_printf (contents, "\t%s", short_hostname); +- g_string_append_printf (contents, "\t%s\n", ADDED_TAG); +- } ++ if ( (new_contents->len > 2) ++ && (new_contents->str[new_contents->len - 1] == '\n')) ++ g_string_truncate (new_contents, new_contents->len - 1); + +- /* IPv4 localhost line */ +- g_string_append (contents, "127.0.0.1"); +- if (!hostname4_is_fallback && !ip4_addr && !found_user_host4) { +- g_string_append_printf (contents, "\t%s", hostname); +- if (short_hostname) +- g_string_append_printf (contents, "\t%s", short_hostname); +- } +- g_string_append_printf (contents, "\t%s\tlocalhost", fallback_hostname4); +- if (custom4) +- g_string_append (contents, custom4); +- g_string_append_c (contents, '\n'); +- +- /* IPv6 localhost line */ +- g_string_append (contents, "::1"); +- if (!hostname6_is_fallback && !hostname4_is_fallback && !ip6_addr && !found_user_host6) { +- g_string_append_printf (contents, "\t%s", hostname); +- if (short_hostname) +- g_string_append_printf (contents, "\t%s", short_hostname); +- } +- g_string_append_printf (contents, "\t%s\tlocalhost6", fallback_hostname6); +- if (custom6) +- g_string_append (contents, custom6); +- g_string_append_c (contents, '\n'); +- +- added = TRUE; +- } +- +- /* Don't add the original line if it is a localhost mapping */ +- if ( !is_local_mapping (*line, FALSE, "localhost") +- && !is_local_mapping (*line, FALSE, fallback_hostname4) +- && !is_local_mapping (*line, FALSE, hostname) +- && !is_local_mapping (*line, TRUE, "localhost6") +- && !is_local_mapping (*line, TRUE, fallback_hostname6) +- && !is_local_mapping (*line, TRUE, hostname) +- && !strstr (*line, ADDED_TAG)) { +- +- g_string_append (contents, *line); +- /* Only append the new line if this isn't the last line in the file */ +- if (*(line+1)) +- g_string_append_c (contents, '\n'); +- } +- } +- +- /* Hmm, /etc/hosts was empty for some reason */ +- if (!added) { +- g_string_append (contents, "# Do not remove the following lines, or various programs\n"); +- g_string_append (contents, "# that require network functionality will fail.\n"); +- +- /* Add the address mappings first so they take precedence */ +- if (!hostname4_is_fallback && ip4_addr) { +- g_string_append_printf (contents, "%s\t%s", ip4_addr, hostname); +- if (short_hostname) +- g_string_append_printf (contents, "\t%s", short_hostname); +- g_string_append_printf (contents, "\t%s\n", ADDED_TAG); +- } +- if (!hostname6_is_fallback && ip6_addr) { +- g_string_append_printf (contents, "%s\t%s", ip6_addr, hostname); +- if (short_hostname) +- g_string_append_printf (contents, "\t%s", short_hostname); +- g_string_append_printf (contents, "\t%s\n", ADDED_TAG); +- } +- +- g_string_append_printf (contents, "127.0.0.1\t%s\tlocalhost\n", fallback_hostname4); +- g_string_append_printf (contents, "::1\t%s\tlocalhost6\n", fallback_hostname6); +- } +- +-out: +- g_free (custom4); +- g_free (custom6); +- g_free (short_hostname); +- return contents; ++ return new_contents; + } + +-gboolean +-nm_policy_hosts_update_etc_hosts (const char *hostname, +- const char *old_hostname, +- const char *fallback_hostname4, +- const char *fallback_hostname6, +- const char *ip4_addr, +- const char *ip6_addr, +- gboolean *out_changed) ++/* remove any leftover "# Added by NetworkManager" lines */ ++void ++nm_policy_hosts_clean_etc_hosts (void) + { + char *contents = NULL; +- char **lines = NULL; +- GError *error = NULL; +- GString *new_contents = NULL; + gsize contents_len = 0; +- gboolean success = FALSE; +- +- g_return_val_if_fail (hostname != NULL, FALSE); +- g_return_val_if_fail (out_changed != NULL, FALSE); ++ GError *error = NULL; ++ GString *new; + + if (!g_file_get_contents (SYSCONFDIR "/hosts", &contents, &contents_len, &error)) { + nm_log_warn (LOGD_DNS, "couldn't read " SYSCONFDIR "/hosts: (%d) %s", + error ? error->code : 0, + (error && error->message) ? error->message : "(unknown)"); + g_clear_error (&error); +- return FALSE; ++ return; + } + +- /* Get the new /etc/hosts contents */ +- lines = g_strsplit_set (contents, "\n\r", 0); +- new_contents = nm_policy_get_etc_hosts ((const char **) lines, +- contents_len, +- hostname, +- old_hostname, +- fallback_hostname4, +- fallback_hostname6, +- ip4_addr, +- ip6_addr, +- &error); +- g_strfreev (lines); +- g_free (contents); +- +- if (new_contents) { +- nm_log_info (LOGD_DNS, "Updating /etc/hosts with new system hostname"); ++ new = nm_policy_get_etc_hosts (contents, contents_len); ++ if (new && new->len) { ++ nm_log_info (LOGD_DNS, "Cleaning leftovers from /etc/hosts"); + + g_clear_error (&error); +- /* And actually update /etc/hosts */ +- if (!g_file_set_contents (SYSCONFDIR "/hosts", new_contents->str, -1, &error)) { +- nm_log_warn (LOGD_DNS, "couldn't update " SYSCONFDIR "/hosts: (%d) %s", +- error ? error->code : 0, +- (error && error->message) ? error->message : "(unknown)"); ++ if (!g_file_set_contents (SYSCONFDIR "/hosts", new->str, -1, &error)) { ++ nm_log_dbg (LOGD_DNS, "couldn't update " SYSCONFDIR "/hosts: (%d) %s", ++ error ? error->code : 0, ++ (error && error->message) ? error->message : "(unknown)"); + g_clear_error (&error); +- } else { +- success = TRUE; +- *out_changed = TRUE; + } +- +- g_string_free (new_contents, TRUE); +- } else if (!error) { +- /* No change required */ +- success = TRUE; +- } else { +- nm_log_warn (LOGD_DNS, "couldn't read " SYSCONFDIR "/hosts: (%d) %s", +- error->code, error->message ? error->message : "(unknown)"); +- g_clear_error (&error); + } + +- return success; ++ if (new) ++ g_string_free (new, TRUE); + } + +diff --git a/src/nm-policy-hosts.h b/src/nm-policy-hosts.h +index ebaaf0f..9f4bf9a 100644 +--- a/src/nm-policy-hosts.h ++++ b/src/nm-policy-hosts.h +@@ -23,26 +23,10 @@ + + #include <glib.h> + +-gboolean nm_policy_hosts_update_etc_hosts (const char *hostname, +- const char *old_hostname, +- const char *fallback_hostname4, +- const char *fallback_hostname6, +- const char *ip4_addr, +- const char *ip6_addr, +- gboolean *out_changed); ++void nm_policy_hosts_clean_etc_hosts (void); + + /* Only for testcases; don't use outside of nm-policy-hosts.c */ +-gboolean nm_policy_hosts_find_token (const char *line, const char *token); +- +-GString *nm_policy_get_etc_hosts (const char **lines, +- gsize existing_len, +- const char *hostname, +- const char *old_hostname, +- const char *fallback_hostname4, +- const char *fallback_hostname6, +- const char *ip4_addr, +- const char *ip6_addr, +- GError **error); ++GString *nm_policy_get_etc_hosts (const char *contents, gsize contents_len); + + #endif /* NM_POLICY_HOSTS_H */ + +diff --git a/src/nm-policy.c b/src/nm-policy.c +index e8a18d0..709be09 100644 +--- a/src/nm-policy.c ++++ b/src/nm-policy.c +@@ -42,7 +42,6 @@ + #include "nm-system.h" + #include "nm-dns-manager.h" + #include "nm-vpn-manager.h" +-#include "nm-policy-hosts.h" + #include "nm-policy-hostname.h" + + struct NMPolicy { +@@ -233,9 +232,6 @@ _set_hostname (NMPolicy *policy, + const char *new_hostname, + const char *msg) + { +- char ip4_addr[INET_ADDRSTRLEN + 1]; +- char ip6_addr[INET6_ADDRSTRLEN + 1]; +- + if (change_hostname) { + NMDnsManager *dns_mgr; + +@@ -247,43 +243,7 @@ _set_hostname (NMPolicy *policy, + g_object_unref (dns_mgr); + } + +- /* Get the default IPv4 and IPv6 addresses so we can assign +- * the hostname to them in /etc/hosts. +- */ +- memset (ip4_addr, 0, sizeof (ip4_addr)); +- if (policy->default_device4) { +- NMIP4Config *config = NULL; +- NMIP4Address *addr = NULL; +- +- config = nm_device_get_ip4_config (policy->default_device4); +- if (config) +- addr = nm_ip4_config_get_address (config, 0); +- +- if (addr) { +- struct in_addr tmp; +- +- tmp.s_addr = nm_ip4_address_get_address (addr); +- inet_ntop (AF_INET, &tmp, ip4_addr, sizeof (ip4_addr)); +- } +- } +- +- memset (ip6_addr, 0, sizeof (ip6_addr)); +- if (policy->default_device6) { +- NMIP6Config *config = NULL; +- NMIP6Address *addr = NULL; +- +- config = nm_device_get_ip6_config (policy->default_device6); +- if (config) +- addr = nm_ip6_config_get_address (config, 0); +- +- if (addr) +- inet_ntop (AF_INET6, nm_ip6_address_get_address (addr), ip6_addr, sizeof (ip6_addr)); +- } +- +- if (nm_policy_set_system_hostname (policy->cur_hostname, +- strlen (ip4_addr) ? ip4_addr : NULL, +- strlen (ip6_addr) ? ip6_addr : NULL, +- msg)) ++ if (nm_policy_set_system_hostname (policy->cur_hostname, msg)) + nm_utils_call_dispatcher ("hostname", NULL, NULL, NULL); + } + +diff --git a/src/tests/test-policy-hosts.c b/src/tests/test-policy-hosts.c +index 8865c42..62862e7 100644 +--- a/src/tests/test-policy-hosts.c ++++ b/src/tests/test-policy-hosts.c +@@ -23,41 +23,17 @@ + + #include "nm-policy-hosts.h" + +-#define FALLBACK_HOSTNAME4 "localhost.localdomain" +-#define FALLBACK_HOSTNAME6 "localhost6.localdomain6" +- +-#define DEBUG 0 ++#define DEBUG 1 + + static void +-test_generic (const char *before, +- const char *after, +- const char *hostname, +- const char *ip4_addr, +- const char *ip6_addr, +- gboolean expect_error) ++test_generic (const char *before, const char *after) + { +- char **lines; + GString *newc; +- GError *error = NULL; + + /* Get the new /etc/hosts contents */ +- lines = g_strsplit_set (before, "\n\r", 0); +- newc = nm_policy_get_etc_hosts ((const char **) lines, +- strlen (before), +- hostname, +- NULL, +- FALLBACK_HOSTNAME4, +- FALLBACK_HOSTNAME6, +- ip4_addr, +- ip6_addr, +- &error); +- g_strfreev (lines); ++ newc = nm_policy_get_etc_hosts (before, strlen (before)); + +- if (expect_error) { +- g_assert (newc == NULL); +- g_assert (error != NULL); +- g_clear_error (&error); +- } else if (after == NULL) { ++ if (after == NULL) { + /* No change to /etc/hosts required */ + #if DEBUG + if (newc != NULL) { +@@ -68,10 +44,8 @@ test_generic (const char *before, + } + #endif + g_assert (newc == NULL); +- g_assert (error == NULL); + } else { + g_assert (newc != NULL); +- g_assert (error == NULL); + + #if DEBUG + g_message ("\n- NEW ---------------------------------\n" +@@ -81,7 +55,6 @@ test_generic (const char *before, + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", + newc->str, after); + #endif +- g_assert (strlen (newc->str) == strlen (after)); + g_assert (strcmp (newc->str, after) == 0); + g_string_free (newc, TRUE); + } +@@ -99,7 +72,7 @@ static const char *generic_before = \ + static void + test_hosts_generic (void) + { +- test_generic (generic_before, NULL, "localhost.localdomain", NULL, NULL, FALSE); ++ test_generic (generic_before, NULL); + } + + /*******************************************/ +@@ -112,311 +85,25 @@ static const char *generic_no_boilerplate_before = \ + static void + test_hosts_generic_no_boilerplate (void) + { +- test_generic (generic_no_boilerplate_before, NULL, "localhost.localdomain", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *generic_no_boilerplate_no_lh_before = \ +- "127.0.0.1 localhost.localdomain\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static const char *generic_no_boilerplate_no_lh_after = \ +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_generic_no_boilerplate_no_lh (void) +-{ +- test_generic (generic_no_boilerplate_no_lh_before, +- generic_no_boilerplate_no_lh_after, +- "localhost.localdomain", +- NULL, +- NULL, +- FALSE); +-} +- +-/*******************************************/ +- +- +-static const char *generic_no_boilerplate_no_lh_no_host_before = \ +- "127.0.0.1 localhost.localdomain\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static const char *generic_no_boilerplate_no_lh_no_host_after = \ +- "127.0.0.1 comet localhost.localdomain localhost\n" +- "::1 comet localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_generic_no_boilerplate_no_lh_no_host (void) +-{ +- test_generic (generic_no_boilerplate_no_lh_no_host_before, +- generic_no_boilerplate_no_lh_no_host_after, +- "comet", +- NULL, +- NULL, +- FALSE); +-} +- +-/*******************************************/ +-static const char *named_generic_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 playboy localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static const char *named_generic_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 playboy localhost.localdomain localhost\n" +- "::1 playboy localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_named_generic (void) +-{ +- test_generic (named_generic_before, named_generic_after, "playboy", NULL, NULL, FALSE); ++ test_generic (generic_no_boilerplate_before, NULL); + } + + /*******************************************/ + +-static const char *named4_non127_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 tomcat localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "192.168.1.2 tomcat\n"; +- +-static void +-test_hosts_named4_non127 (void) +-{ +- test_generic (named4_non127_before, NULL, "tomcat", "192.168.1.2", NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *named6_non127_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 tomcat localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "3001:abba::3234 tomcat\n"; +- +-static void +-test_hosts_named6_non127 (void) +-{ +- test_generic (named6_non127_before, NULL, "tomcat", NULL, "3001:abba::3234", FALSE); +-} +- +-/*******************************************/ +- +-static const char *named4_non127_more_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 tomcat localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "192.168.1.2 tomcat\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "127.0.0.1 srx.main.ebayrtm.com\n" +- "127.0.0.1 cdn5.tribalfusion.com\n"; +- +-static void +-test_hosts_named4_non127_more (void) +-{ +- test_generic (named4_non127_more_before, NULL, "tomcat", "192.168.1.2", NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *named6_non127_more_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 tomcat localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "3001:abba::3234 tomcat\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "127.0.0.1 srx.main.ebayrtm.com\n" +- "127.0.0.1 cdn5.tribalfusion.com\n"; +- +-static void +-test_hosts_named6_non127_more (void) +-{ +- test_generic (named6_non127_more_before, NULL, "tomcat", NULL, "3001:abba::3234", FALSE); +-} +- +-/*******************************************/ +- +-static const char *named_no_lh_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "192.168.1.2 tomcat\n"; +- +-static const char *named_no_lh_after = \ ++static const char *leftover_before = \ + "# Do not remove the following line, or various programs\n" + "# that require network functionality will fail.\n" ++ "192.168.1.2 comet # Added by NetworkManager\n" + "127.0.0.1 localhost.localdomain localhost\n" +- "::1 tomcat localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "192.168.1.2 tomcat\n"; +- +-static void +-test_hosts_named_no_localhost (void) +-{ +- test_generic (named_no_lh_before, named_no_lh_after, "tomcat", "192.168.1.2", NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *no_lh_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 tomcat\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static const char *no_lh_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 tomcat localhost.localdomain localhost\n" +- "::1 tomcat localhost6.localdomain6 localhost6\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_no_localhost (void) +-{ +- test_generic (no_lh_before, no_lh_after, "tomcat", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *named_last_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 sparcbook.ausil.us\n" +- "::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 sparcbook.ausil.us\n"; +- +-static void +-test_hosts_named_last (void) +-{ +- test_generic (named_last_before, NULL, "sparcbook.ausil.us", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *no_host4_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "127.0.0.1 srx.main.ebayrtm.com\n" +- "127.0.0.1 cdn5.tribalfusion.com\n" +- "127.0.0.1 a.tribalfusion.com\n"; +- +-static const char *no_host4_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 comet localhost.localdomain localhost\n" +- "::1 comet localhost6.localdomain6 localhost6\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "127.0.0.1 srx.main.ebayrtm.com\n" +- "127.0.0.1 cdn5.tribalfusion.com\n" +- "127.0.0.1 a.tribalfusion.com\n"; +- +-static void +-test_hosts_no_host4 (void) +-{ +- test_generic (no_host4_before, no_host4_after, "comet", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *no_host6_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static const char *no_host6_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 comet localhost.localdomain localhost\n" +- "::1 comet localhost6.localdomain6 localhost6\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_no_host6 (void) +-{ +- test_generic (no_host6_before, no_host6_after, "comet", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *named46_non127_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "192.168.1.2 comet\n" +- "3001:abba::3234 comet\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_named46_non127 (void) +-{ +- test_generic (named46_non127_before, NULL, "comet", "192.168.1.2", "3001:abba::3234", FALSE); +-} +- +-/*******************************************/ +- +-static const char *named46_non127_long_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "192.168.1.2 comet.space comet\n" +- "3001:abba::3234 comet.space comet\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_named46_non127_long (void) +-{ +- test_generic (named46_non127_long_before, NULL, "comet.space", "192.168.1.2", "3001:abba::3234", FALSE); +-} +- +-/*******************************************/ +- +-static const char *named46_non127_other4_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" + "::1 localhost6.localdomain6 localhost6\n" + "192.168.1.3 comet\n" + "3001:abba::3234 comet\n" + "\n" + "127.0.0.1 lcmd.us.intellitxt.com\n"; + +-static const char *named46_non127_other4_after = \ ++static const char *leftover_after = \ + "# Do not remove the following line, or various programs\n" + "# that require network functionality will fail.\n" +- "192.168.1.2 comet # Added by NetworkManager\n" + "127.0.0.1 localhost.localdomain localhost\n" + "::1 localhost6.localdomain6 localhost6\n" + "192.168.1.3 comet\n" +@@ -425,416 +112,44 @@ static const char *named46_non127_other4_after = \ + "127.0.0.1 lcmd.us.intellitxt.com\n"; + + static void +-test_hosts_named46_non127_other4 (void) ++test_hosts_leftover (void) + { +- test_generic (named46_non127_other4_before, named46_non127_other4_after, "comet", "192.168.1.2", "3001:abba::3234", FALSE); ++ test_generic (leftover_before, leftover_after); + } + + /*******************************************/ + +-static const char *named46_non127_other4_long_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "192.168.1.3 comet.space\n" +- "3001:abba::3234 comet.space\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static const char *named46_non127_other4_long_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "192.168.1.2 comet.space comet # Added by NetworkManager\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "192.168.1.3 comet.space\n" +- "3001:abba::3234 comet.space\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_named46_non127_other4_long (void) +-{ +- test_generic (named46_non127_other4_long_before, named46_non127_other4_long_after, "comet.space", "192.168.1.2", "3001:abba::3234", FALSE); +-} +- +-/*******************************************/ +- +-static const char *named46_non127_other6_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "192.168.1.2 comet\n" +- "3001:abba::9675 comet\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static const char *named46_non127_other6_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "3001:abba::3234 comet # Added by NetworkManager\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "192.168.1.2 comet\n" +- "3001:abba::9675 comet\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_named46_non127_other6 (void) +-{ +- test_generic (named46_non127_other6_before, named46_non127_other6_after, "comet", "192.168.1.2", "3001:abba::3234", FALSE); +-} +- +-/*******************************************/ +- +-static const char *named46_non127_other6_long_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "192.168.1.2 comet.space\n" +- "3001:abba::9675 comet.space\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static const char *named46_non127_other6_long_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "3001:abba::3234 comet.space comet # Added by NetworkManager\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "192.168.1.2 comet.space\n" +- "3001:abba::9675 comet.space\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_named46_non127_other6_long (void) +-{ +- test_generic (named46_non127_other6_long_before, named46_non127_other6_long_after, "comet.space", "192.168.1.2", "3001:abba::3234", FALSE); +-} +- +-/*******************************************/ +- +-static const char *unnamed46_non127_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static const char *unnamed46_non127_after = \ ++static const char *leftover_double_newline_before = \ + "# Do not remove the following line, or various programs\n" + "# that require network functionality will fail.\n" + "192.168.1.2 comet # Added by NetworkManager\n" +- "3001:abba::3234 comet # Added by NetworkManager\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_unnamed46_non127 (void) +-{ +- test_generic (unnamed46_non127_before, unnamed46_non127_after, "comet", "192.168.1.2", "3001:abba::3234", FALSE); +-} +- +-/*******************************************/ +- +-static const char *unnamed46_non127_long_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static const char *unnamed46_non127_long_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "192.168.1.2 comet.space comet # Added by NetworkManager\n" +- "3001:abba::3234 comet.space comet # Added by NetworkManager\n" + "127.0.0.1 localhost.localdomain localhost\n" + "::1 localhost6.localdomain6 localhost6\n" ++ "192.168.1.3 comet\n" ++ "3001:abba::3234 comet\n" + "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_unnamed46_non127_long (void) +-{ +- test_generic (unnamed46_non127_long_before, unnamed46_non127_long_after, "comet.space", "192.168.1.2", "3001:abba::3234", FALSE); +-} +- +-/*******************************************/ +- +-static const char *named46_non127_wrong_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "192.168.1.3 comet # Added by NetworkManager\n" +- "3001:abba::9876 comet # Added by NetworkManager\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; ++ "127.0.0.1 lcmd.us.intellitxt.com\n" ++ "\n"; + +-static const char *named46_non127_wrong_after = \ ++static const char *leftover_double_newline_after = \ + "# Do not remove the following line, or various programs\n" + "# that require network functionality will fail.\n" +- "192.168.1.2 comet # Added by NetworkManager\n" +- "3001:abba::3234 comet # Added by NetworkManager\n" + "127.0.0.1 localhost.localdomain localhost\n" + "::1 localhost6.localdomain6 localhost6\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n"; +- +-static void +-test_hosts_named46_non127_wrong (void) +-{ +- test_generic (named46_non127_wrong_before, named46_non127_wrong_after, "comet", "192.168.1.2", "3001:abba::3234", FALSE); +-} +- +-/*******************************************/ +- +-static const char *long_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost comet\n" +- "::1 localhost6.localdomain6 localhost6\n" +- "\n" +- "127.0.0.1 lcmd.us.intellitxt.com\n" +- "127.0.0.1 adserver.adtech.de\n" +- "127.0.0.1 a.as-us.falkag.net\n" +- "127.0.0.1 a.as-eu.falkag.net\n" +- "127.0.0.1 ads.doubleclick.com\n" +- "\n" +- "# random comment\n" +- "127.0.0.1 m1.2mdn.net\n" +- "127.0.0.1 ds.serving-sys.com\n" +- "127.0.0.1 pagead2.googlesyndication.com\n" +- "127.0.0.1 ad.doubleclick.com\n" +- "127.0.0.1 ad.doubleclick.net\n" +- "127.0.0.1 oascentral.movietickets.com\n" +- "127.0.0.1 view.atdmt.com\n" +- "127.0.0.1 ads.chumcity.com\n"; +- +-static const char *long_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 comet localhost.localdomain localhost\n" +- "::1 comet localhost6.localdomain6 localhost6\n" ++ "192.168.1.3 comet\n" ++ "3001:abba::3234 comet\n" + "\n" + "127.0.0.1 lcmd.us.intellitxt.com\n" +- "127.0.0.1 adserver.adtech.de\n" +- "127.0.0.1 a.as-us.falkag.net\n" +- "127.0.0.1 a.as-eu.falkag.net\n" +- "127.0.0.1 ads.doubleclick.com\n" +- "\n" +- "# random comment\n" +- "127.0.0.1 m1.2mdn.net\n" +- "127.0.0.1 ds.serving-sys.com\n" +- "127.0.0.1 pagead2.googlesyndication.com\n" +- "127.0.0.1 ad.doubleclick.com\n" +- "127.0.0.1 ad.doubleclick.net\n" +- "127.0.0.1 oascentral.movietickets.com\n" +- "127.0.0.1 view.atdmt.com\n" +- "127.0.0.1 ads.chumcity.com\n"; +- +-static void +-test_hosts_long (void) +-{ +- test_generic (long_before, long_after, "comet", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *custom4_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost pintglass\n" +- "::1 localhost6.localdomain6 localhost6\n"; +- +-static const char *custom4_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 comet localhost.localdomain localhost pintglass\n" +- "::1 comet localhost6.localdomain6 localhost6\n"; +- +-static void +-test_hosts_custom4 (void) +-{ +- test_generic (custom4_before, custom4_after, "comet", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *custom6_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6 pintglass\n"; +- +-static const char *custom6_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 comet localhost.localdomain localhost\n" +- "::1 comet localhost6.localdomain6 localhost6 pintglass\n"; +- +-static void +-test_hosts_custom6 (void) +-{ +- test_generic (custom6_before, custom6_after, "comet", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *custom46_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost shotglass\n" +- "::1 localhost6.localdomain6 localhost6 pintglass\n"; +- +-static const char *custom46_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 comet localhost.localdomain localhost shotglass\n" +- "::1 comet localhost6.localdomain6 localhost6 pintglass\n"; +- +-static void +-test_hosts_custom46 (void) +-{ +- test_generic (custom46_before, custom46_after, "comet", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *custom46_mixed_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 localhost.localdomain localhost shotglass\n" +- "::1 localhost6.localdomain6 localhost6 pintglass\n"; +- +-static const char *custom46_mixed_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 comet localhost.localdomain localhost shotglass\n" +- "::1 comet localhost6.localdomain6 localhost6 pintglass\n"; +- +-static void +-test_hosts_custom46_mixed (void) +-{ +- test_generic (custom46_mixed_before, custom46_mixed_after, "comet", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *stale4_removed_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "1.2.3.4 comet # Added by NetworkManager\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 comet localhost6.localdomain6 localhost6\n"; +- +-static const char *stale4_removed_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 comet localhost.localdomain localhost\n" +- "::1 comet localhost6.localdomain6 localhost6\n"; ++ "\n"; + + static void +-test_hosts_stale4_removed (void) ++test_hosts_leftover_double_newline (void) + { +- test_generic (stale4_removed_before, stale4_removed_after, "comet", NULL, NULL, FALSE); ++ test_generic (leftover_double_newline_before, leftover_double_newline_after); + } + + /*******************************************/ + +-static const char *stale6_removed_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "3001:abba::3234 comet # Added by NetworkManager\n" +- "127.0.0.1 comet localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n"; +- +-static const char *stale6_removed_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 comet localhost.localdomain localhost\n" +- "::1 comet localhost6.localdomain6 localhost6\n"; +- +-static void +-test_hosts_stale6_removed (void) +-{ +- test_generic (stale6_removed_before, stale6_removed_after, "comet", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-static const char *stale46_removed_before = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "1.2.3.4 comet # Added by NetworkManager\n" +- "3001:abba::3234 comet # Added by NetworkManager\n" +- "127.0.0.1 localhost.localdomain localhost\n" +- "::1 localhost6.localdomain6 localhost6\n"; +- +-static const char *stale46_removed_after = \ +- "# Do not remove the following line, or various programs\n" +- "# that require network functionality will fail.\n" +- "127.0.0.1 comet localhost.localdomain localhost\n" +- "::1 comet localhost6.localdomain6 localhost6\n"; +- +-static void +-test_hosts_stale46_removed (void) +-{ +- test_generic (stale46_removed_before, stale46_removed_after, "comet", NULL, NULL, FALSE); +-} +- +-/*******************************************/ +- +-typedef struct { +- const char *line; +- const char *token; +- gboolean expected; +-} Foo; +- +-static Foo foo[] = { +- /* Using \t here to easily differentiate tabs vs. spaces for testing */ +- { "127.0.0.1\tfoobar\tblah", "blah", TRUE }, +- { "", "blah", FALSE }, +- { "1.1.1.1\tbork\tfoo", "blah", FALSE }, +- { "127.0.0.1 foobar\tblah", "blah", TRUE }, +- { "127.0.0.1 foobar blah", "blah", TRUE }, +- { "127.0.0.1 localhost", "localhost.localdomain", FALSE }, +- { "192.168.1.1 blah borkbork", "blah", TRUE }, +- { "192.168.1.1 foobar\tblah borkbork", "blah", TRUE }, +- { "192.168.1.1\tfoobar\tblah\tborkbork", "blah", TRUE }, +- { "192.168.1.1 \tfoobar \tblah \tborkbork\t ", "blah", TRUE }, +- { "\t\t\t\t \t\t\tasdfadf a\t\t\t\t\t \t\t\t\t\t ", "blah", FALSE }, +- { NULL, NULL, FALSE } +-}; +- +-static void +-test_find_token (void) +-{ +- Foo *iter = &foo[0]; +- +- while (iter->line) { +- gboolean found; +- +- found = nm_policy_hosts_find_token (iter->line, iter->token); +- if (found != iter->expected) { +- g_warning ("find-token: unexpected token result %d for '%s' <= '%s' (expected %d)", +- found, iter->line, iter->token, iter->expected); +- } +- g_assert (found == iter->expected); +- iter++; +- } +-} +- + #if GLIB_CHECK_VERSION(2,25,12) + typedef GTestFixtureFunc TCFunc; + #else +@@ -851,38 +166,10 @@ int main (int argc, char **argv) + + suite = g_test_get_root (); + +- g_test_suite_add (suite, TESTCASE (test_find_token, NULL)); + g_test_suite_add (suite, TESTCASE (test_hosts_generic, NULL)); + g_test_suite_add (suite, TESTCASE (test_hosts_generic_no_boilerplate, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_generic_no_boilerplate_no_lh, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_generic_no_boilerplate_no_lh_no_host, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named_generic, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named4_non127, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named6_non127, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named4_non127_more, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named6_non127_more, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named46_non127, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named46_non127_long, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named46_non127_other4, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named46_non127_other4_long, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named46_non127_other6, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named46_non127_other6_long, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named46_non127_wrong, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_unnamed46_non127, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_unnamed46_non127_long, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named_no_localhost, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_no_localhost, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_named_last, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_no_host4, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_no_host6, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_long, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_custom4, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_custom6, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_custom46, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_custom46_mixed, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_stale4_removed, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_stale6_removed, NULL)); +- g_test_suite_add (suite, TESTCASE (test_hosts_stale46_removed, NULL)); ++ g_test_suite_add (suite, TESTCASE (test_hosts_leftover, NULL)); ++ g_test_suite_add (suite, TESTCASE (test_hosts_leftover_double_newline, NULL)); + + return g_test_run (); + } +-- +1.7.3.4 + |