Commit debb16cf authored by Dan Williams's avatar Dan Williams

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

Instead of always mapping the current hostname to 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.
parent 171dcf51
......@@ -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"
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)
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';
errno = 0;
......@@ -232,7 +233,7 @@ nm_policy_set_system_hostname (const char *new_hostname,
} 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_HOSTNAME)))
|| (!new_hostname && !strcmp (old_hostname, FALLBACK_HOSTNAME4)))
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
* /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,
&changed)) {
/* error updating /etc/hosts; fallback to localhost.localdomain */
nm_log_info (LOGD_DNS, "Setting system hostname to '" FALLBACK_HOSTNAME "' (error updating /etc/hosts)");
ret = sethostname (FALLBACK_HOSTNAME, strlen (FALLBACK_HOSTNAME));
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_HOSTNAME, errno, strerror (errno));
FALLBACK_HOSTNAME4, errno, strerror (errno));
This diff is collapsed.
......@@ -24,7 +24,8 @@
#include <glib.h>
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 *ip6_addr,
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,
gsize existing_len,
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);
#endif /* NM_POLICY_HOSTS_H */
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment