policy: map hostname to current IP address (bgo #619931)

Instead of always mapping the current hostname to 127.0.0.1 or
whatever the user mapped it to manually, make sure the hostname
maps to the default device's IPv4 and IPv6 address if there's
a default device.

This helps out services that do a lookup on the machine hostname
to determine the IP address, which while a broken behavior (since
there are too many edge-cases) is pretty wide-spread and thus
we should support it.
This commit is contained in:
Dan Williams
2010-06-21 21:18:25 -07:00
parent 171dcf51b5
commit debb16cfc0
4 changed files with 431 additions and 105 deletions

View File

@@ -205,7 +205,8 @@ hostname_thread_is_dead (HostnameThread *ht)
/************************************************************************/ /************************************************************************/
#define FALLBACK_HOSTNAME "localhost.localdomain" #define FALLBACK_HOSTNAME4 "localhost.localdomain"
#define FALLBACK_HOSTNAME6 "localhost6.localdomain6"
gboolean gboolean
nm_policy_set_system_hostname (const char *new_hostname, nm_policy_set_system_hostname (const char *new_hostname,
@@ -221,7 +222,7 @@ nm_policy_set_system_hostname (const char *new_hostname,
if (new_hostname) if (new_hostname)
g_warn_if_fail (strlen (new_hostname)); g_warn_if_fail (strlen (new_hostname));
name = (new_hostname && strlen (new_hostname)) ? new_hostname : FALLBACK_HOSTNAME; name = (new_hostname && strlen (new_hostname)) ? new_hostname : FALLBACK_HOSTNAME4;
old_hostname[HOST_NAME_MAX] = '\0'; old_hostname[HOST_NAME_MAX] = '\0';
errno = 0; errno = 0;
@@ -232,7 +233,7 @@ nm_policy_set_system_hostname (const char *new_hostname,
} else { } else {
/* Don't set the hostname if it isn't actually changing */ /* Don't set the hostname if it isn't actually changing */
if ( (new_hostname && !strcmp (old_hostname, new_hostname)) if ( (new_hostname && !strcmp (old_hostname, new_hostname))
|| (!new_hostname && !strcmp (old_hostname, FALLBACK_HOSTNAME))) || (!new_hostname && !strcmp (old_hostname, FALLBACK_HOSTNAME4)))
set_hostname = FALSE; set_hostname = FALSE;
} }
@@ -253,13 +254,18 @@ nm_policy_set_system_hostname (const char *new_hostname,
* nm_policy_hosts_update_etc_hosts() will just return and won't touch * nm_policy_hosts_update_etc_hosts() will just return and won't touch
* /etc/hosts at all. * /etc/hosts at all.
*/ */
if (!nm_policy_hosts_update_etc_hosts (name, FALLBACK_HOSTNAME, ip4_addr, ip6_addr, &changed)) { if (!nm_policy_hosts_update_etc_hosts (name,
FALLBACK_HOSTNAME4,
FALLBACK_HOSTNAME6,
ip4_addr,
ip6_addr,
&changed)) {
/* error updating /etc/hosts; fallback to localhost.localdomain */ /* error updating /etc/hosts; fallback to localhost.localdomain */
nm_log_info (LOGD_DNS, "Setting system hostname to '" FALLBACK_HOSTNAME "' (error updating /etc/hosts)"); nm_log_info (LOGD_DNS, "Setting system hostname to '" FALLBACK_HOSTNAME4 "' (error updating /etc/hosts)");
ret = sethostname (FALLBACK_HOSTNAME, strlen (FALLBACK_HOSTNAME)); ret = sethostname (FALLBACK_HOSTNAME4, strlen (FALLBACK_HOSTNAME4));
if (ret != 0) { if (ret != 0) {
nm_log_warn (LOGD_DNS, "couldn't set the fallback system hostname (%s): (%d) %s", nm_log_warn (LOGD_DNS, "couldn't set the fallback system hostname (%s): (%d) %s",
FALLBACK_HOSTNAME, errno, strerror (errno)); FALLBACK_HOSTNAME4, errno, strerror (errno));
} }
} }

View File

@@ -23,6 +23,7 @@
#include <errno.h> #include <errno.h>
#include <netdb.h> #include <netdb.h>
#include <ctype.h> #include <ctype.h>
#include <arpa/inet.h>
#include "nm-policy-hosts.h" #include "nm-policy-hosts.h"
#include "nm-logging.h" #include "nm-logging.h"
@@ -53,59 +54,133 @@ nm_policy_hosts_find_token (const char *line, const char *token)
} }
static gboolean static gboolean
is_local_mapping (const char *str, const char *hostname) is_local_mapping (const char *str, gboolean ip6, const char *hostname)
{ {
return ( !strncmp (str, "127.0.0.1", strlen ("127.0.0.1")) const char *addr = ip6 ? "::1" : "127.0.0.1";
&& nm_policy_hosts_find_token (str, hostname ? hostname : "localhost")); const char *fallback = ip6 ? "localhost6" : "localhost";
return ( !strncmp (str, addr, strlen (addr))
&& nm_policy_hosts_find_token (str, hostname ? hostname : fallback));
} }
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
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;
}
#define ADDED_TAG "# Added by NetworkManager"
GString * GString *
nm_policy_get_etc_hosts (const char **lines, nm_policy_get_etc_hosts (const char **lines,
gsize existing_len, gsize existing_len,
const char *hostname, const char *hostname,
const char *fallback_hostname, const char *fallback_hostname4,
const char *fallback_hostname6,
const char *ip4_addr,
const char *ip6_addr,
GError **error) GError **error)
{ {
GString *contents = NULL; GString *contents = NULL;
const char **line; const char **line;
gboolean found_host_nonlocal = FALSE; gboolean found_localhost4 = FALSE;
gboolean found_host = FALSE; gboolean found_localhost6 = FALSE;
gboolean found_localhost = FALSE; gboolean found_host4 = FALSE;
gboolean found_host6 = FALSE;
gboolean found_user_host4 = FALSE;
gboolean found_user_host6 = FALSE;
gboolean initial_comments = TRUE; gboolean initial_comments = TRUE;
gboolean added = FALSE; gboolean added = FALSE;
gboolean hostname4_is_fallback;
gboolean hostname6_is_fallback;
g_return_val_if_fail (lines != NULL, FALSE); g_return_val_if_fail (lines != NULL, FALSE);
g_return_val_if_fail (hostname != NULL, FALSE); g_return_val_if_fail (hostname != NULL, FALSE);
/* /etc/hosts needs at least two things: hostname4_is_fallback = !strcmp (hostname, fallback_hostname4);
hostname6_is_fallback = !strcmp (hostname, fallback_hostname6);
/* We need the following in /etc/hosts:
* *
* 1) current hostname mapped to any address * 1) current hostname mapped to current IPv4 addresses if IPv4 is active
* 2) 'localhost' mapped to 127.0.0.1 * 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 both these conditions exist in /etc/hosts, we don't need to bother * If all these things exist we don't need to bother updating the file.
* updating the file.
*/ */
/* Look for the two cases from above */ if (!ip4_addr)
for (line = lines; lines && *line; line++) { found_host4 = TRUE;
if (strlen (*line) && (*line[0] != '#')) { if (!ip6_addr)
if (nm_policy_hosts_find_token (*line, hostname)) { found_host6 = TRUE;
if (!is_local_mapping (*line, "localhost")) {
/* hostname is not on a 127.0.0.1 line or the line does not
* contain 'localhost'.
*/
found_host_nonlocal = TRUE;
}
found_host = TRUE;
}
if (is_local_mapping (*line, "localhost")) { /* Look for the four cases from above */
/* a 127.0.0.1 line containing 'localhost' */ for (line = lines; lines && *line; line++) {
found_localhost = TRUE; if (!strlen (*line) || (*line[0] == '#'))
continue;
if (nm_policy_hosts_find_token (*line, 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))
found_user_host4 = TRUE;
}
if (ip6_addr && ip6_addr_matches (*line, ip6_addr)) {
found_host6 = TRUE;
if (!strstr (*line, ADDED_TAG))
found_user_host6 = TRUE;
} }
} }
if (found_localhost && found_host) if (is_local_mapping (*line, FALSE, "localhost")) {
/* a 127.0.0.1 line containing 'localhost' */
found_localhost4 = TRUE;
} else if (is_local_mapping (*line, TRUE, "localhost6")) {
/* a ::1 line containing 'localhost6' */
found_localhost6 = TRUE;
}
if (found_localhost4 && found_host4 && found_localhost6 && found_host6)
return NULL; /* No update required */ return NULL; /* No update required */
} }
@@ -115,11 +190,13 @@ nm_policy_get_etc_hosts (const char **lines,
return NULL; return NULL;
} }
/* Construct the new hosts file; replace any 127.0.0.1 entry that is at the /* Construct the new hosts file; replace any 127.0.0.1/::1 entry that is
* beginning of the file or right after initial comments and contains * at the beginning of the file or right after initial comments and contains
* the string 'localhost'. If there is no 127.0.0.1 entry at the beginning * the string 'localhost' (for IPv4) or 'localhost6' (for IPv6). If there
* or after initial comments that contains 'localhost', add one there * is no 127.0.0.1 or ::1 entry at the beginning or after initial comments
* and ignore any other 127.0.0.1 entries that contain 'localhost'. * that contains 'localhost' or 'localhost6', add one there
* and ignore any other 127.0.0.1/::1 entries that contain 'localhost' or
* 'localhost6'.
*/ */
for (line = lines, initial_comments = TRUE; lines && *line; line++) { for (line = lines, initial_comments = TRUE; lines && *line; line++) {
gboolean add_line = TRUE; gboolean add_line = TRUE;
@@ -128,33 +205,49 @@ nm_policy_get_etc_hosts (const char **lines,
if (strlen (*line) && initial_comments && (*line[0] != '#')) { if (strlen (*line) && initial_comments && (*line[0] != '#')) {
initial_comments = FALSE; initial_comments = FALSE;
/* If some other line contained the hostname but not 'localhost', /* If the user added their own mapping for the hostname, just make
* make a simple localhost mapping and assume the user knows what * a simple 'localhost' mapping and assume the user knows what they
* they are doing with their manual hostname entry. Otherwise if * are doing with their manual hostname entry. Otherwise if the
* the hostname wasn't found somewhere else, add it to the localhost * hostname wasn't found somewhere else, add it to the localhost
* mapping line to make sure it's mapped to something. * mapping line to make sure it's mapped to something.
*/ */
if (found_host_nonlocal)
g_string_append (contents, "127.0.0.1");
else
g_string_append_printf (contents, "127.0.0.1\t%s", hostname);
if (strcmp (hostname, fallback_hostname)) { /* IPv4 localhost line */
g_string_append_printf (contents, "\t%s", fallback_hostname); g_string_append (contents, "127.0.0.1");
/* Don't add a standalone 'localhost.localdomain' 127 mapping */ if (!hostname4_is_fallback && !ip4_addr && !found_user_host4)
if (is_local_mapping (*line, fallback_hostname)) g_string_append_printf (contents, "\t%s", hostname);
add_line = FALSE; g_string_append_printf (contents, "\t%s\tlocalhost\n", fallback_hostname4);
}
/* 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);
g_string_append_printf (contents, "\t%s\tlocalhost6\n", fallback_hostname6);
/* Add the address mappings */
if (!hostname4_is_fallback && ip4_addr && !found_user_host4)
g_string_append_printf (contents, "%s\t%s\t%s\n", ip4_addr, hostname, ADDED_TAG);
if (!hostname6_is_fallback && ip6_addr && !found_user_host6)
g_string_append_printf (contents, "%s\t%s\t%s\n", ip6_addr, hostname, ADDED_TAG);
g_string_append (contents, "\tlocalhost\n");
added = TRUE; added = TRUE;
/* Don't add the original line if it is a 'localhost' mapping */
if (is_local_mapping (*line, "localhost"))
add_line = FALSE;
} }
if (add_line) { /* 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)) {
g_string_append (contents, *line); g_string_append (contents, *line);
/* Only append the new line if this isn't the last line in the file */ /* Only append the new line if this isn't the last line in the file */
if (*(line+1)) if (*(line+1))
@@ -164,9 +257,16 @@ nm_policy_get_etc_hosts (const char **lines,
/* Hmm, /etc/hosts was empty for some reason */ /* Hmm, /etc/hosts was empty for some reason */
if (!added) { if (!added) {
g_string_append (contents, "# Do not remove the following line, or various programs\n"); 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"); g_string_append (contents, "# that require network functionality will fail.\n");
g_string_append_printf (contents, "127.0.0.1\t%s\tlocalhost\n", fallback_hostname); 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);
/* Add the address mappings */
if (!hostname4_is_fallback && ip4_addr)
g_string_append_printf (contents, "%s\t%s\t%s\n", ip4_addr, hostname, ADDED_TAG);
if (!hostname6_is_fallback && ip6_addr)
g_string_append_printf (contents, "%s\t%s\t%s\n", ip6_addr, hostname, ADDED_TAG);
} }
return contents; return contents;
@@ -174,7 +274,8 @@ nm_policy_get_etc_hosts (const char **lines,
gboolean gboolean
nm_policy_hosts_update_etc_hosts (const char *hostname, nm_policy_hosts_update_etc_hosts (const char *hostname,
const char *fallback_hostname, const char *fallback_hostname4,
const char *fallback_hostname6,
const char *ip4_addr, const char *ip4_addr,
const char *ip6_addr, const char *ip6_addr,
gboolean *out_changed) gboolean *out_changed)
@@ -189,8 +290,6 @@ nm_policy_hosts_update_etc_hosts (const char *hostname,
g_return_val_if_fail (hostname != NULL, FALSE); g_return_val_if_fail (hostname != NULL, FALSE);
g_return_val_if_fail (out_changed != NULL, FALSE); g_return_val_if_fail (out_changed != NULL, FALSE);
g_message ("%s: ip4 '%s', ip6 '%s'", __func__, ip4_addr, ip6_addr);
if (!g_file_get_contents (SYSCONFDIR "/hosts", &contents, &contents_len, &error)) { if (!g_file_get_contents (SYSCONFDIR "/hosts", &contents, &contents_len, &error)) {
nm_log_warn (LOGD_DNS, "couldn't read " SYSCONFDIR "/hosts: (%d) %s", nm_log_warn (LOGD_DNS, "couldn't read " SYSCONFDIR "/hosts: (%d) %s",
error ? error->code : 0, error ? error->code : 0,
@@ -204,7 +303,10 @@ g_message ("%s: ip4 '%s', ip6 '%s'", __func__, ip4_addr, ip6_addr);
new_contents = nm_policy_get_etc_hosts ((const char **) lines, new_contents = nm_policy_get_etc_hosts ((const char **) lines,
contents_len, contents_len,
hostname, hostname,
fallback_hostname, fallback_hostname4,
fallback_hostname6,
ip4_addr,
ip6_addr,
&error); &error);
g_strfreev (lines); g_strfreev (lines);
g_free (contents); g_free (contents);

View File

@@ -24,7 +24,8 @@
#include <glib.h> #include <glib.h>
gboolean nm_policy_hosts_update_etc_hosts (const char *hostname, gboolean nm_policy_hosts_update_etc_hosts (const char *hostname,
const char *fallback_hostname, const char *fallback_hostname4,
const char *fallback_hostname6,
const char *ip4_addr, const char *ip4_addr,
const char *ip6_addr, const char *ip6_addr,
gboolean *out_changed); gboolean *out_changed);
@@ -35,7 +36,10 @@ gboolean nm_policy_hosts_find_token (const char *line, const char *token);
GString *nm_policy_get_etc_hosts (const char **lines, GString *nm_policy_get_etc_hosts (const char **lines,
gsize existing_len, gsize existing_len,
const char *hostname, const char *hostname,
const char *fallback_hostname, const char *fallback_hostname4,
const char *fallback_hostname6,
const char *ip4_addr,
const char *ip6_addr,
GError **error); GError **error);
#endif /* NM_POLICY_HOSTS_H */ #endif /* NM_POLICY_HOSTS_H */

View File

@@ -23,12 +23,15 @@
#include "nm-policy-hosts.h" #include "nm-policy-hosts.h"
#define FALLBACK_HOSTNAME "localhost.localdomain" #define FALLBACK_HOSTNAME4 "localhost.localdomain"
#define FALLBACK_HOSTNAME6 "localhost6.localdomain6"
static void static void
test_generic (const char *before, test_generic (const char *before,
const char *after, const char *after,
const char *hostname, const char *hostname,
const char *ip4_addr,
const char *ip6_addr,
gboolean expect_error) gboolean expect_error)
{ {
char **lines; char **lines;
@@ -40,7 +43,10 @@ test_generic (const char *before,
newc = nm_policy_get_etc_hosts ((const char **) lines, newc = nm_policy_get_etc_hosts ((const char **) lines,
strlen (before), strlen (before),
hostname, hostname,
FALLBACK_HOSTNAME, FALLBACK_HOSTNAME4,
FALLBACK_HOSTNAME6,
ip4_addr,
ip6_addr,
&error); &error);
g_strfreev (lines); g_strfreev (lines);
@@ -50,6 +56,14 @@ test_generic (const char *before,
g_clear_error (&error); g_clear_error (&error);
} else if (after == NULL) { } else if (after == NULL) {
/* No change to /etc/hosts required */ /* No change to /etc/hosts required */
#if 0
if (newc != NULL) {
g_message ("\n- NEW ---------------------------------\n"
"%s"
"- EXPECTED NONE -------------------------\n",
newc->str);
}
#endif
g_assert (newc == NULL); g_assert (newc == NULL);
g_assert (error == NULL); g_assert (error == NULL);
} else { } else {
@@ -57,10 +71,12 @@ test_generic (const char *before,
g_assert (error == NULL); g_assert (error == NULL);
#if 0 #if 0
g_message ("\n--------------------------------------\n" g_message ("\n- NEW ---------------------------------\n"
"%s" "%s"
"--------------------------------------", "- EXPECTED ------------------------------\n"
newc->str); "%s"
"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
newc->str, after);
#endif #endif
g_assert (strlen (newc->str) == strlen (after)); g_assert (strlen (newc->str) == strlen (after));
g_assert (strcmp (newc->str, after) == 0); g_assert (strcmp (newc->str, after) == 0);
@@ -80,7 +96,7 @@ static const char *generic_before = \
static void static void
test_hosts_generic (void) test_hosts_generic (void)
{ {
test_generic (generic_before, NULL, "localhost.localdomain", FALSE); test_generic (generic_before, NULL, "localhost.localdomain", NULL, NULL, FALSE);
} }
/*******************************************/ /*******************************************/
@@ -93,20 +109,19 @@ static const char *generic_no_boilerplate_before = \
static void static void
test_hosts_generic_no_boilerplate (void) test_hosts_generic_no_boilerplate (void)
{ {
test_generic (generic_no_boilerplate_before, NULL, "localhost.localdomain", FALSE); test_generic (generic_no_boilerplate_before, NULL, "localhost.localdomain", NULL, NULL, FALSE);
} }
/*******************************************/ /*******************************************/
static const char *generic_no_boilerplate_no_lh_before = \ static const char *generic_no_boilerplate_no_lh_before = \
"127.0.0.1 localhost.localdomain\n" "127.0.0.1 localhost.localdomain\n"
"::1 localhost6.localdomain6 localhost6\n" "::1 localhost6.localdomain6 localhost6\n"
"127.0.0.1 lcmd.us.intellitxt.com\n"; "127.0.0.1 lcmd.us.intellitxt.com\n";
static const char *generic_no_boilerplate_no_lh_after = \ static const char *generic_no_boilerplate_no_lh_after = \
"127.0.0.1 localhost\n" "127.0.0.1 localhost.localdomain localhost\n"
"127.0.0.1 localhost.localdomain\n" "::1 localhost6.localdomain6 localhost6\n"
"::1 localhost6.localdomain6 localhost6\n"
"127.0.0.1 lcmd.us.intellitxt.com\n"; "127.0.0.1 lcmd.us.intellitxt.com\n";
static void static void
@@ -115,6 +130,8 @@ test_hosts_generic_no_boilerplate_no_lh (void)
test_generic (generic_no_boilerplate_no_lh_before, test_generic (generic_no_boilerplate_no_lh_before,
generic_no_boilerplate_no_lh_after, generic_no_boilerplate_no_lh_after,
"localhost.localdomain", "localhost.localdomain",
NULL,
NULL,
FALSE); FALSE);
} }
@@ -128,7 +145,7 @@ static const char *generic_no_boilerplate_no_lh_no_host_before = \
static const char *generic_no_boilerplate_no_lh_no_host_after = \ static const char *generic_no_boilerplate_no_lh_no_host_after = \
"127.0.0.1 comet localhost.localdomain localhost\n" "127.0.0.1 comet localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n" "::1 comet localhost6.localdomain6 localhost6\n"
"127.0.0.1 lcmd.us.intellitxt.com\n"; "127.0.0.1 lcmd.us.intellitxt.com\n";
static void static void
@@ -137,6 +154,8 @@ test_hosts_generic_no_boilerplate_no_lh_no_host (void)
test_generic (generic_no_boilerplate_no_lh_no_host_before, test_generic (generic_no_boilerplate_no_lh_no_host_before,
generic_no_boilerplate_no_lh_no_host_after, generic_no_boilerplate_no_lh_no_host_after,
"comet", "comet",
NULL,
NULL,
FALSE); FALSE);
} }
@@ -151,32 +170,48 @@ static const char *named_generic_before = \
static void static void
test_hosts_named_generic (void) test_hosts_named_generic (void)
{ {
test_generic (named_generic_before, NULL, "playboy", FALSE); test_generic (named_generic_before, NULL, "playboy", NULL, NULL, FALSE);
} }
/*******************************************/ /*******************************************/
static const char *named_non127_before = \ static const char *named4_non127_before = \
"# Do not remove the following line, or various programs\n" "# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n" "# that require network functionality will fail.\n"
"127.0.0.1 localhost.localdomain localhost\n" "127.0.0.1 localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n" "::1 tomcat localhost6.localdomain6 localhost6\n"
"127.0.0.1 lcmd.us.intellitxt.com\n" "127.0.0.1 lcmd.us.intellitxt.com\n"
"192.168.1.2 tomcat\n"; "192.168.1.2 tomcat\n";
static void static void
test_hosts_named_non127 (void) test_hosts_named4_non127 (void)
{ {
test_generic (named_non127_before, NULL, "tomcat", FALSE); test_generic (named4_non127_before, NULL, "tomcat", "192.168.1.2", NULL, FALSE);
} }
/*******************************************/ /*******************************************/
static const char *named2_non127_before = \ 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" "# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n" "# that require network functionality will fail.\n"
"127.0.0.1 localhost.localdomain localhost\n" "127.0.0.1 localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n" "::1 tomcat localhost6.localdomain6 localhost6\n"
"127.0.0.1 lcmd.us.intellitxt.com\n" "127.0.0.1 lcmd.us.intellitxt.com\n"
"192.168.1.2 tomcat\n" "192.168.1.2 tomcat\n"
"127.0.0.1 lcmd.us.intellitxt.com\n" "127.0.0.1 lcmd.us.intellitxt.com\n"
@@ -184,9 +219,28 @@ static const char *named2_non127_before = \
"127.0.0.1 cdn5.tribalfusion.com\n"; "127.0.0.1 cdn5.tribalfusion.com\n";
static void static void
test_hosts_named2_non127 (void) test_hosts_named4_non127_more (void)
{ {
test_generic (named2_non127_before, NULL, "tomcat", FALSE); 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);
} }
/*******************************************/ /*******************************************/
@@ -203,14 +257,14 @@ static const char *named_no_lh_after = \
"# Do not remove the following line, or various programs\n" "# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n" "# that require network functionality will fail.\n"
"127.0.0.1 localhost.localdomain localhost\n" "127.0.0.1 localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n" "::1 tomcat localhost6.localdomain6 localhost6\n"
"127.0.0.1 lcmd.us.intellitxt.com\n" "127.0.0.1 lcmd.us.intellitxt.com\n"
"192.168.1.2 tomcat\n"; "192.168.1.2 tomcat\n";
static void static void
test_hosts_named_no_localhost (void) test_hosts_named_no_localhost (void)
{ {
test_generic (named_no_lh_before, named_no_lh_after, "tomcat", FALSE); test_generic (named_no_lh_before, named_no_lh_after, "tomcat", "192.168.1.2", NULL, FALSE);
} }
/*******************************************/ /*******************************************/
@@ -225,15 +279,14 @@ static const char *no_lh_before = \
static const char *no_lh_after = \ static const char *no_lh_after = \
"# Do not remove the following line, or various programs\n" "# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n" "# that require network functionality will fail.\n"
"127.0.0.1 localhost.localdomain localhost\n" "127.0.0.1 tomcat localhost.localdomain localhost\n"
"127.0.0.1 tomcat\n" "::1 tomcat localhost6.localdomain6 localhost6\n"
"::1 localhost6.localdomain6 localhost6\n"
"127.0.0.1 lcmd.us.intellitxt.com\n"; "127.0.0.1 lcmd.us.intellitxt.com\n";
static void static void
test_hosts_no_localhost (void) test_hosts_no_localhost (void)
{ {
test_generic (no_lh_before, no_lh_after, "tomcat", FALSE); test_generic (no_lh_before, no_lh_after, "tomcat", NULL, NULL, FALSE);
} }
/*******************************************/ /*******************************************/
@@ -247,12 +300,12 @@ static const char *named_last_before = \
static void static void
test_hosts_named_last (void) test_hosts_named_last (void)
{ {
test_generic (named_last_before, NULL, "sparcbook.ausil.us", FALSE); test_generic (named_last_before, NULL, "sparcbook.ausil.us", NULL, NULL, FALSE);
} }
/*******************************************/ /*******************************************/
static const char *no_host_before = \ static const char *no_host4_before = \
"# Do not remove the following line, or various programs\n" "# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n" "# that require network functionality will fail.\n"
"::1 localhost6.localdomain6 localhost6\n" "::1 localhost6.localdomain6 localhost6\n"
@@ -262,11 +315,11 @@ static const char *no_host_before = \
"127.0.0.1 cdn5.tribalfusion.com\n" "127.0.0.1 cdn5.tribalfusion.com\n"
"127.0.0.1 a.tribalfusion.com\n"; "127.0.0.1 a.tribalfusion.com\n";
static const char *no_host_after = \ static const char *no_host4_after = \
"# Do not remove the following line, or various programs\n" "# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n" "# that require network functionality will fail.\n"
"127.0.0.1 comet localhost.localdomain localhost\n" "127.0.0.1 comet localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n" "::1 comet localhost6.localdomain6 localhost6\n"
"\n" "\n"
"127.0.0.1 lcmd.us.intellitxt.com\n" "127.0.0.1 lcmd.us.intellitxt.com\n"
"127.0.0.1 srx.main.ebayrtm.com\n" "127.0.0.1 srx.main.ebayrtm.com\n"
@@ -274,9 +327,162 @@ static const char *no_host_after = \
"127.0.0.1 a.tribalfusion.com\n"; "127.0.0.1 a.tribalfusion.com\n";
static void static void
test_hosts_no_host (void) test_hosts_no_host4 (void)
{ {
test_generic (no_host_before, no_host_after, "comet", FALSE); 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_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 = \
"# 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 # Added by NetworkManager\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_named46_non127_other4 (void)
{
test_generic (named46_non127_other4_before, named46_non127_other4_after, "comet", "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"
"127.0.0.1 localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n"
"3001:abba::3234 comet # Added by NetworkManager\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 *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 = \
"# 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 # Added by NetworkManager\n"
"3001:abba::3234 comet # Added by NetworkManager\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 *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";
static const char *named46_non127_wrong_after = \
"# 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 # Added by NetworkManager\n"
"3001:abba::3234 comet # Added by NetworkManager\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);
} }
/*******************************************/ /*******************************************/
@@ -318,7 +524,7 @@ static const char *long_before = \
static void static void
test_hosts_long (void) test_hosts_long (void)
{ {
test_generic (long_before, NULL, "comet", FALSE); test_generic (long_before, NULL, "comet", NULL, NULL, FALSE);
} }
/*******************************************/ /*******************************************/
@@ -381,12 +587,20 @@ int main (int argc, char **argv)
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, 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_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_named_generic, NULL));
g_test_suite_add (suite, TESTCASE (test_hosts_named_non127, NULL)); g_test_suite_add (suite, TESTCASE (test_hosts_named4_non127, NULL));
g_test_suite_add (suite, TESTCASE (test_hosts_named2_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_other4, NULL));
g_test_suite_add (suite, TESTCASE (test_hosts_named46_non127_other6, 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_named_no_localhost, 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_no_localhost, NULL));
g_test_suite_add (suite, TESTCASE (test_hosts_named_last, NULL)); g_test_suite_add (suite, TESTCASE (test_hosts_named_last, NULL));
g_test_suite_add (suite, TESTCASE (test_hosts_no_host, 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_long, NULL));
return g_test_run (); return g_test_run ();