core: also use /etc/hosts for hostname resolution

Before introducing the hostname lookup via nm-daemon-helper and
systemd-resolved, we used GLib's GResolver which internally relies on
the libc resolver and generally also returns results from /etc/hosts.

With the new mechanism we only ask to systemd-resolved (with
NO_SYNTHESIZE) or perform the lookup via the "dns" NSS module. In both
ways, /etc/hosts is not evaluated.

Since users relied on having the hostname resolved via /etc/hosts,
restore that behavior. Now, after trying the resolution via
systemd-resolved and the "dns" NSS module, we also try via the "files"
NSS module which reads /etc/hosts.

Fixes: 27eae4043b ('device: add a nm_device_resolve_address()')
This commit is contained in:
Beniamino Galvani
2024-06-19 20:29:37 +02:00
parent 229bebfae9
commit 410afccb32
2 changed files with 39 additions and 13 deletions

3
NEWS
View File

@@ -11,6 +11,9 @@ USE AT YOUR OWN RISK. NOT RECOMMENDED FOR PRODUCTION USE!
* Support matching a OVS system interface by MAC address.
* Add a timeout option to connectivity checking.
* Support configuring veth interfaces in nmtui.
* When looking up the system hostname from the reverse DNS lookup of
addresses configured on interfaces, NetworkManager now takes into
account the content of /etc/hosts.
=============================================
NetworkManager-1.48

View File

@@ -245,14 +245,36 @@ resolve_addr_helper_cb(GObject *source, GAsyncResult *result, gpointer user_data
resolve_addr_complete(info, g_steal_pointer(&output), g_steal_pointer(&error));
}
typedef enum {
RESOLVE_ADDR_SERVICE_NONE = 0x0,
RESOLVE_ADDR_SERVICE_DNS = 0x1,
RESOLVE_ADDR_SERVICE_FILES = 0x2,
} ResolveAddrService;
static void
resolve_addr_spawn_helper(ResolveAddrInfo *info)
resolve_addr_spawn_helper(ResolveAddrInfo *info, ResolveAddrService services)
{
char addr_str[NM_INET_ADDRSTRLEN];
char addr_str[NM_INET_ADDRSTRLEN];
char str[256];
char *s = str;
gsize len = sizeof(str);
gboolean comma = FALSE;
nm_assert(services != RESOLVE_ADDR_SERVICE_NONE);
nm_assert((services & ~(RESOLVE_ADDR_SERVICE_DNS | RESOLVE_ADDR_SERVICE_FILES)) == 0);
if (services & RESOLVE_ADDR_SERVICE_DNS) {
nm_strbuf_append(&s, &len, "%sdns", comma ? "," : "");
comma = TRUE;
}
if (services & RESOLVE_ADDR_SERVICE_FILES) {
nm_strbuf_append(&s, &len, "%sfiles", comma ? "," : "");
comma = TRUE;
}
nm_inet_ntop(info->addr_family, &info->address, addr_str);
_LOG2D(info, "start lookup via nm-daemon-helper");
nm_utils_spawn_helper(NM_MAKE_STRV("resolve-address", addr_str),
_LOG2D(info, "start lookup via nm-daemon-helper using services: %s", str);
nm_utils_spawn_helper(NM_MAKE_STRV("resolve-address", addr_str, str),
g_task_get_cancellable(info->task),
resolve_addr_helper_cb,
info);
@@ -282,27 +304,28 @@ resolve_addr_resolved_cb(NMDnsSystemdResolved *resolved,
dbus_error = g_dbus_error_get_remote_error(error);
if (NM_STR_HAS_PREFIX(dbus_error, "org.freedesktop.resolve1.")) {
/* systemd-resolved is enabled but it couldn't resolve the
* address via DNS. Don't fall back to spawning the helper,
* because the helper will possibly ask again to
* address via DNS. Spawn again the helper to check if we
* can find a result in /etc/hosts. Don't enable the 'dns'
* service otherwise the helper will possibly ask again to
* systemd-resolved (via /etc/resolv.conf), potentially using
* other protocols than DNS or returning synthetic results.
*
* Consider the error as the final indication that the address
* can't be resolved.
*
* See: https://www.freedesktop.org/wiki/Software/systemd/resolved/#commonerrors
*/
resolve_addr_complete(info, NULL, g_error_copy(error));
resolve_addr_spawn_helper(info, RESOLVE_ADDR_SERVICE_FILES);
return;
}
resolve_addr_spawn_helper(info);
/* systemd-resolved couldn't be contacted, use the helper */
resolve_addr_spawn_helper(info, RESOLVE_ADDR_SERVICE_DNS | RESOLVE_ADDR_SERVICE_FILES);
return;
}
if (names_len == 0) {
_LOG2D(info, "systemd-resolved returned no result");
resolve_addr_complete(info, g_strdup(""), NULL);
/* We passed the NO_SYNTHESIZE flag and so systemd-resolved
* didn't look into /etc/hosts. Spawn the helper for that. */
resolve_addr_spawn_helper(info, RESOLVE_ADDR_SERVICE_FILES);
return;
}
@@ -366,7 +389,7 @@ nm_device_resolve_address(int addr_family,
return;
}
resolve_addr_spawn_helper(info);
resolve_addr_spawn_helper(info, RESOLVE_ADDR_SERVICE_DNS | RESOLVE_ADDR_SERVICE_FILES);
}
char *