core: preserve custom local-mapped hostnames in /etc/hosts (rh #627269)

This commit is contained in:
Dan Williams 2010-10-08 09:44:44 -05:00
parent c28c070c1c
commit 10e568e9dd
3 changed files with 669 additions and 1 deletions

View File

@ -20,7 +20,7 @@ Name: NetworkManager
Summary: Network connection manager and user applications
Epoch: 1
Version: 0.8.1
Release: 7%{snapshot}%{?dist}
Release: 8%{snapshot}%{?dist}
Group: System Environment/Base
License: GPLv2+
URL: http://www.gnome.org/projects/NetworkManager/
@ -32,6 +32,8 @@ Patch1: nm-applet-internal-buildfixes.patch
Patch2: explain-dns1-dns2.patch
Patch3: nm-applet-no-notifications.patch
Patch4: nm-remove-stale-hosts-mappings.patch
Patch5: nm-preserve-custom-hostnames.patch
Patch6: nm-prevent-hostname-dup.patch
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
Requires(post): chkconfig
@ -163,6 +165,8 @@ tar -xjf %{SOURCE1}
%patch2 -p1 -b .explain-dns1-dns2
%patch3 -p1 -b .no-notifications
%patch4 -p1 -b .remove-stale-hosts-mappings
%patch5 -p1 -b .preserve-custom-hostnames
%patch6 -p1 -b .prevent-hostname-dup
%build
@ -416,6 +420,9 @@ fi
%{_datadir}/gtk-doc/html/libnm-util/*
%changelog
* Thu Oct 7 2010 Dan Williams <dcbw@redhat.com> - 0.8.1-8
- core: preserve custom local-mapped hostnames in /etc/hosts (rh #627269)
* Thu Oct 7 2010 Dan Williams <dcbw@redhat.com> - 0.8.1-7
- core: remove stale /etc/hosts mappings (rh #630146)

View File

@ -0,0 +1,561 @@
commit 052ca428d9c4862112a905cd07fd126bbe7192ee
Author: Dan Williams <dcbw@redhat.com>
Date: Thu Oct 7 18:08:52 2010 -0500
policy: preserve custom hostnames on local-mapping lines (rh #627269)
Try to preserve custom hostnames (ie, anything not a localhost* variant,
the current hostname, or the previous hostname) when rewriting the
127.0.0.1/::1 localhost mapping lines.
diff --git a/src/nm-policy-hostname.c b/src/nm-policy-hostname.c
index 0a49f9e..42a2e0f 100644
--- a/src/nm-policy-hostname.c
+++ b/src/nm-policy-hostname.c
@@ -217,7 +217,7 @@ nm_policy_set_system_hostname (const char *new_hostname,
char old_hostname[HOST_NAME_MAX + 1];
int ret = 0;
const char *name;
- gboolean set_hostname = TRUE, changed = FALSE;
+ gboolean set_hostname = TRUE, changed = FALSE, old_valid = TRUE;
if (new_hostname)
g_warn_if_fail (strlen (new_hostname));
@@ -230,11 +230,15 @@ nm_policy_set_system_hostname (const char *new_hostname,
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) {
@@ -255,6 +259,7 @@ nm_policy_set_system_hostname (const char *new_hostname,
* /etc/hosts at all.
*/
if (!nm_policy_hosts_update_etc_hosts (name,
+ old_valid ? old_hostname : NULL,
FALLBACK_HOSTNAME4,
FALLBACK_HOSTNAME6,
ip4_addr,
diff --git a/src/nm-policy-hosts.c b/src/nm-policy-hosts.c
index 1b57d42..ddc828d 100644
--- a/src/nm-policy-hosts.c
+++ b/src/nm-policy-hosts.c
@@ -28,6 +28,9 @@
#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)
{
@@ -56,7 +59,7 @@ nm_policy_hosts_find_token (const char *line, const char *token)
static gboolean
is_local_mapping (const char *str, gboolean ip6, const char *hostname)
{
- const char *addr = ip6 ? "::1" : "127.0.0.1";
+ const char *addr = ip6 ? IP6_LH : IP4_LH;
const char *fallback = ip6 ? "localhost6" : "localhost";
return ( !strncmp (str, addr, strlen (addr))
@@ -137,12 +140,71 @@ ip6_addr_matches (const char *str, const char *ip6_addr)
return memcmp (&found, &given, sizeof (found)) == 0;
}
+static char *
+get_custom_hostnames (const char *line,
+ const char *hostname,
+ const char *old_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 (!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,
@@ -163,7 +225,10 @@ nm_policy_get_etc_hosts (const char **lines,
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);
@@ -181,21 +246,20 @@ nm_policy_get_etc_hosts (const char **lines,
* If all these things exist we don't need to bother updating the file.
*/
- if (!ip4_addr) {
- found_host4 = TRUE;
+ if (!ip4_addr)
host4_before = TRUE;
- }
- if (!ip6_addr) {
- found_host6 = TRUE;
+ if (!ip6_addr)
host6_before = TRUE;
- }
/* Look for the four cases from above */
for (line = lines; lines && *line; line++) {
- if (!strlen (*line) || (*line[0] == '#'))
+ gboolean found_hostname = FALSE;
+
+ if ((*line[0] == '\0') || (*line[0] == '#'))
continue;
- if (nm_policy_hosts_find_token (*line, hostname)) {
+ 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;
@@ -211,7 +275,7 @@ nm_policy_get_etc_hosts (const char **lines,
* so make sure we update /etc/hosts.
*/
if (is_ip4_addr (*line))
- found_host4 = FALSE;
+ no_stale = FALSE;
}
if (ip6_addr && ip6_addr_matches (*line, ip6_addr)) {
@@ -228,26 +292,54 @@ nm_policy_get_etc_hosts (const char **lines,
* so make sure we update /etc/hosts.
*/
if (is_ip6_addr (*line))
- found_host6 = FALSE;
+ no_stale = FALSE;
}
}
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);
+ 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);
+ 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;
+ }
}
- if (found_localhost4 && found_host4 && found_localhost6 && found_host6 && host4_before && host6_before)
- return NULL; /* No update required */
+ 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");
- return NULL;
+ goto out;
}
/* Find the short hostname, like 'foo' from 'foo.bar.baz'; we want to
@@ -277,8 +369,6 @@ nm_policy_get_etc_hosts (const char **lines,
* 'localhost6'.
*/
for (line = lines, initial_comments = TRUE; lines && *line; line++) {
- gboolean add_line = TRUE;
-
/* This is the first line after the initial comments */
if (strlen (*line) && initial_comments && (*line[0] != '#')) {
initial_comments = FALSE;
@@ -311,7 +401,10 @@ nm_policy_get_etc_hosts (const char **lines,
if (short_hostname)
g_string_append_printf (contents, "\t%s", short_hostname);
}
- g_string_append_printf (contents, "\t%s\tlocalhost\n", fallback_hostname4);
+ 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");
@@ -320,26 +413,23 @@ nm_policy_get_etc_hosts (const char **lines,
if (short_hostname)
g_string_append_printf (contents, "\t%s", short_hostname);
}
- g_string_append_printf (contents, "\t%s\tlocalhost6\n", fallback_hostname6);
+ 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"))
- add_line = FALSE;
- else if (is_local_mapping (*line, FALSE, fallback_hostname4))
- 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))
- add_line = FALSE;
-
- if (add_line && !strstr (*line, ADDED_TAG)) {
+ 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))
@@ -370,12 +460,16 @@ nm_policy_get_etc_hosts (const char **lines,
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;
}
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,
@@ -405,6 +499,7 @@ nm_policy_hosts_update_etc_hosts (const char *hostname,
new_contents = nm_policy_get_etc_hosts ((const char **) lines,
contents_len,
hostname,
+ old_hostname,
fallback_hostname4,
fallback_hostname6,
ip4_addr,
diff --git a/src/nm-policy-hosts.h b/src/nm-policy-hosts.h
index 76288bf..ebaaf0f 100644
--- a/src/nm-policy-hosts.h
+++ b/src/nm-policy-hosts.h
@@ -24,6 +24,7 @@
#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,
@@ -36,6 +37,7 @@ 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,
diff --git a/src/tests/test-policy-hosts.c b/src/tests/test-policy-hosts.c
index e6775f3..3c27af2 100644
--- a/src/tests/test-policy-hosts.c
+++ b/src/tests/test-policy-hosts.c
@@ -26,6 +26,8 @@
#define FALLBACK_HOSTNAME4 "localhost.localdomain"
#define FALLBACK_HOSTNAME6 "localhost6.localdomain6"
+#define DEBUG 0
+
static void
test_generic (const char *before,
const char *after,
@@ -43,6 +45,7 @@ test_generic (const char *before,
newc = nm_policy_get_etc_hosts ((const char **) lines,
strlen (before),
hostname,
+ NULL,
FALLBACK_HOSTNAME4,
FALLBACK_HOSTNAME6,
ip4_addr,
@@ -56,11 +59,11 @@ test_generic (const char *before,
g_clear_error (&error);
} else if (after == NULL) {
/* No change to /etc/hosts required */
-#if 0
+#if DEBUG
if (newc != NULL) {
g_message ("\n- NEW ---------------------------------\n"
"%s"
- "- EXPECTED NONE -------------------------\n",
+ "+ EXPECTED NONE +++++++++++++++++++++++++\n",
newc->str);
}
#endif
@@ -70,10 +73,10 @@ test_generic (const char *before,
g_assert (newc != NULL);
g_assert (error == NULL);
-#if 0
+#if DEBUG
g_message ("\n- NEW ---------------------------------\n"
"%s"
- "- EXPECTED ------------------------------\n"
+ "+ EXPECTED ++++++++++++++++++++++++++++++\n"
"%s"
"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
newc->str, after);
@@ -167,10 +170,17 @@ static const char *named_generic_before = \
"::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, NULL, "playboy", NULL, NULL, FALSE);
+ test_generic (named_generic_before, named_generic_after, "playboy", NULL, NULL, FALSE);
}
/*******************************************/
@@ -609,24 +619,114 @@ static const char *long_before = \
"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"
- "127.0.0.1 ads.as4x.tmcs.net\n"
- "127.0.0.1 n4403ad.doubleclick.net\n"
- "127.0.0.1 www.assoc-amazon.com\n"
- "127.0.0.1 s25.sitemeter.com\n"
- "127.0.0.1 adlog.com.com\n"
- "127.0.0.1 ahs.laptopmag.com\n"
- "127.0.0.1 altfarm.mediaplex.com\n"
- "127.0.0.1 ads.addynamix.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";
+ "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"
+ "\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, NULL, "comet", NULL, NULL, FALSE);
+ 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);
}
/*******************************************/
@@ -712,6 +812,10 @@ int main (int argc, char **argv)
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));
return g_test_run ();
}

View File

@ -0,0 +1,100 @@
commit bbf3f12d7b2c79c49d87d41e6cf7b65ac7782071
Author: Dan Williams <dcbw@redhat.com>
Date: Thu Oct 7 18:24:59 2010 -0500
policy: prevent hostname duplication in /etc/hosts
diff --git a/src/nm-policy-hosts.c b/src/nm-policy-hosts.c
index ddc828d..a6bb083 100644
--- a/src/nm-policy-hosts.c
+++ b/src/nm-policy-hosts.c
@@ -143,7 +143,9 @@ ip6_addr_matches (const char *str, const char *ip6_addr)
static char *
get_custom_hostnames (const char *line,
const char *hostname,
- const char *old_hostname)
+ const char *old_hostname,
+ const char *short_hostname,
+ const char *fallback_hostname)
{
char **items = NULL, **iter;
guint start = 0;
@@ -173,6 +175,10 @@ get_custom_hostnames (const char *line,
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"))
@@ -236,6 +242,24 @@ nm_policy_get_etc_hosts (const char **lines,
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
@@ -299,7 +323,7 @@ nm_policy_get_etc_hosts (const char **lines,
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);
+ 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,
@@ -313,7 +337,7 @@ nm_policy_get_etc_hosts (const char **lines,
} else if (is_local_mapping (*line, TRUE, "localhost6")) {
/* a ::1 line containing 'localhost6' */
found_localhost6 = TRUE;
- custom6 = get_custom_hostnames (*line, hostname, old_hostname);
+ 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,
@@ -342,24 +366,6 @@ nm_policy_get_etc_hosts (const char **lines,
goto out;
}
- /* 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;
- }
- }
-
/* 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