2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-01-22 12:24:20 +01:00
|
|
|
#include "fd-util.h"
|
|
|
|
#include "fileio.h"
|
|
|
|
#include "hostname-util.h"
|
|
|
|
#include "resolved-dns-synthesize.h"
|
2018-10-18 16:16:52 +02:00
|
|
|
#include "resolved-etc-hosts.h"
|
2020-01-10 17:22:37 +01:00
|
|
|
#include "socket-netlink.h"
|
2020-11-06 14:56:16 +01:00
|
|
|
#include "stat-util.h"
|
2016-01-22 12:24:20 +01:00
|
|
|
#include "string-util.h"
|
|
|
|
#include "strv.h"
|
|
|
|
#include "time-util.h"
|
|
|
|
|
|
|
|
/* Recheck /etc/hosts at most once every 2s */
|
|
|
|
#define ETC_HOSTS_RECHECK_USEC (2*USEC_PER_SEC)
|
|
|
|
|
2019-01-15 08:12:28 +01:00
|
|
|
static void etc_hosts_item_free(EtcHostsItem *item) {
|
2018-07-31 10:01:46 +02:00
|
|
|
strv_free(item->names);
|
|
|
|
free(item);
|
|
|
|
}
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2019-01-15 08:12:28 +01:00
|
|
|
static void etc_hosts_item_by_name_free(EtcHostsItemByName *item) {
|
2018-07-31 10:01:46 +02:00
|
|
|
free(item->name);
|
|
|
|
free(item->addresses);
|
|
|
|
free(item);
|
|
|
|
}
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2018-07-31 11:01:21 +02:00
|
|
|
void etc_hosts_free(EtcHosts *hosts) {
|
2018-07-31 10:01:46 +02:00
|
|
|
hosts->by_address = hashmap_free_with_destructor(hosts->by_address, etc_hosts_item_free);
|
|
|
|
hosts->by_name = hashmap_free_with_destructor(hosts->by_name, etc_hosts_item_by_name_free);
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
hosts->no_address = set_free_free(hosts->no_address);
|
2018-07-31 10:01:46 +02:00
|
|
|
}
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
void manager_etc_hosts_flush(Manager *m) {
|
|
|
|
etc_hosts_free(&m->etc_hosts);
|
2020-11-06 14:56:16 +01:00
|
|
|
m->etc_hosts_stat = (struct stat) {};
|
2016-01-22 12:24:20 +01:00
|
|
|
}
|
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) {
|
2018-07-31 08:46:28 +02:00
|
|
|
_cleanup_free_ char *address_str = NULL;
|
|
|
|
struct in_addr_data address = {};
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
bool found = false;
|
2016-01-22 12:24:20 +01:00
|
|
|
EtcHostsItem *item;
|
|
|
|
int r;
|
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
assert(hosts);
|
2018-07-31 08:46:28 +02:00
|
|
|
assert(line);
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2018-07-31 08:46:28 +02:00
|
|
|
r = extract_first_word(&line, &address_str, NULL, EXTRACT_RELAX);
|
|
|
|
if (r < 0)
|
2018-11-23 15:52:38 +01:00
|
|
|
return log_error_errno(r, "/etc/hosts:%u: failed to extract address: %m", nr);
|
|
|
|
assert(r > 0); /* We already checked that the line is not empty, so it should contain *something* */
|
2018-07-31 08:46:28 +02:00
|
|
|
|
2018-07-31 09:12:13 +02:00
|
|
|
r = in_addr_ifindex_from_string_auto(address_str, &address.family, &address.address, NULL);
|
2018-11-23 15:52:38 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-31 08:46:28 +02:00
|
|
|
|
|
|
|
r = in_addr_is_null(address.family, &address.address);
|
2018-11-23 15:52:38 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-22 12:24:20 +01:00
|
|
|
if (r > 0)
|
|
|
|
/* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to
|
|
|
|
* nothing. */
|
|
|
|
item = NULL;
|
|
|
|
else {
|
2018-07-31 11:01:21 +02:00
|
|
|
/* If this is a normal address, then simply add entry mapping it to the specified names */
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
item = hashmap_get(hosts->by_address, &address);
|
2018-07-31 08:46:28 +02:00
|
|
|
if (!item) {
|
2018-07-31 10:01:46 +02:00
|
|
|
r = hashmap_ensure_allocated(&hosts->by_address, &in_addr_data_hash_ops);
|
2016-01-22 12:24:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
2020-10-27 14:28:25 +01:00
|
|
|
item = new(EtcHostsItem, 1);
|
2016-01-22 12:24:20 +01:00
|
|
|
if (!item)
|
|
|
|
return log_oom();
|
|
|
|
|
2020-10-27 14:28:25 +01:00
|
|
|
*item = (EtcHostsItem) {
|
|
|
|
.address = address,
|
|
|
|
};
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
r = hashmap_put(hosts->by_address, &item->address, item);
|
2016-01-22 12:24:20 +01:00
|
|
|
if (r < 0) {
|
|
|
|
free(item);
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 08:46:28 +02:00
|
|
|
for (;;) {
|
|
|
|
_cleanup_free_ char *name = NULL;
|
2016-01-22 12:24:20 +01:00
|
|
|
EtcHostsItemByName *bn;
|
|
|
|
|
2018-07-31 08:46:28 +02:00
|
|
|
r = extract_first_word(&line, &name, NULL, EXTRACT_RELAX);
|
|
|
|
if (r < 0)
|
2020-04-14 10:37:40 +02:00
|
|
|
return log_error_errno(r, "/etc/hosts:%u: couldn't extract hostname: %m", nr);
|
2018-07-31 08:46:28 +02:00
|
|
|
if (r == 0)
|
|
|
|
break;
|
|
|
|
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
found = true;
|
|
|
|
|
resolve: reject host names with leading or trailing dashes in /etc/hosts
https://tools.ietf.org/html/rfc1035#section-2.3.1 says (approximately)
that only letters, numbers, and non-leading non-trailing dashes are allowed
(for entries with A/AAAA records). We set no restrictions.
hosts(5) says:
> Host names may contain only alphanumeric characters, minus signs ("-"), and
> periods ("."). They must begin with an alphabetic character and end with an
> alphanumeric character.
nss-files follows those rules, and will ignore names in /etc/hosts that do not
follow this rule.
Let's follow the documented rules for /etc/hosts. In particular, this makes us
consitent with nss-files, reducing surprises for the user.
I'm pretty sure we should apply stricter filtering to names received over DNS
and LLMNR and MDNS, but it's a bigger project, because the rules differ
depepending on which level the label appears (rules for top-level names are
stricter), and this patch takes the minimalistic approach and only changes
behaviour for /etc/hosts.
Escape syntax is also disallowed in /etc/hosts, even if the resulting character
would be allowed. Other tools that parse /etc/hosts do not support this, and
there is no need to use it because no allowed characters benefit from escaping.
2018-11-21 22:58:13 +01:00
|
|
|
r = dns_name_is_valid_ldh(name);
|
2018-11-23 15:52:38 +01:00
|
|
|
if (r <= 0) {
|
|
|
|
log_warning_errno(r, "/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr, name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
if (!item) {
|
|
|
|
/* Optimize the case where we don't need to store any addresses, by storing
|
|
|
|
* only the name in a dedicated Set instead of the hashmap */
|
|
|
|
|
2020-06-05 14:03:30 +02:00
|
|
|
r = set_ensure_consume(&hosts->no_address, &dns_name_hash_ops, TAKE_PTR(name));
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
continue;
|
2018-07-31 08:46:28 +02:00
|
|
|
}
|
|
|
|
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
r = strv_extend(&item->names, name);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
bn = hashmap_get(hosts->by_name, name);
|
2016-01-22 12:24:20 +01:00
|
|
|
if (!bn) {
|
2018-07-31 10:01:46 +02:00
|
|
|
r = hashmap_ensure_allocated(&hosts->by_name, &dns_name_hash_ops);
|
2016-01-22 12:24:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
bn = new0(EtcHostsItemByName, 1);
|
|
|
|
if (!bn)
|
|
|
|
return log_oom();
|
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
r = hashmap_put(hosts->by_name, name, bn);
|
2016-01-22 12:24:20 +01:00
|
|
|
if (r < 0) {
|
|
|
|
free(bn);
|
|
|
|
return log_oom();
|
|
|
|
}
|
2018-07-31 08:46:28 +02:00
|
|
|
|
|
|
|
bn->name = TAKE_PTR(name);
|
2016-01-22 12:24:20 +01:00
|
|
|
}
|
|
|
|
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
if (!GREEDY_REALLOC(bn->addresses, bn->n_allocated, bn->n_addresses + 1))
|
|
|
|
return log_oom();
|
2016-01-22 12:24:20 +01:00
|
|
|
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
bn->addresses[bn->n_addresses++] = &item->address;
|
2016-01-22 12:24:20 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!found)
|
2020-04-14 10:37:40 +02:00
|
|
|
log_warning("/etc/hosts:%u: line is missing any hostnames", nr);
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2018-07-31 08:46:28 +02:00
|
|
|
return 0;
|
2016-01-22 12:24:20 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 23:30:25 +01:00
|
|
|
static void strip_localhost(EtcHosts *hosts) {
|
|
|
|
static const struct in_addr_data local_in_addrs[] = {
|
|
|
|
{
|
|
|
|
.family = AF_INET,
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
/* We want constant expressions here, that's why we don't use htole32() here */
|
|
|
|
.address.in.s_addr = UINT32_C(0x0100007F),
|
|
|
|
#else
|
|
|
|
.address.in.s_addr = UINT32_C(0x7F000001),
|
|
|
|
#endif
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.family = AF_INET6,
|
|
|
|
.address.in6 = IN6ADDR_LOOPBACK_INIT,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
EtcHostsItem *item;
|
|
|
|
|
|
|
|
assert(hosts);
|
|
|
|
|
|
|
|
/* Removes the 'localhost' entry from what we loaded. But only if the mapping is exclusively between
|
|
|
|
* 127.0.0.1 and localhost (or aliases to that we recognize). If there's any other name assigned to
|
|
|
|
* it, we leave the entry in.
|
|
|
|
*
|
|
|
|
* This way our regular synthesizing can take over, but only if it would result in the exact same
|
|
|
|
* mappings. */
|
|
|
|
|
|
|
|
for (size_t j = 0; j < ELEMENTSOF(local_in_addrs); j++) {
|
|
|
|
bool all_localhost, in_order;
|
|
|
|
char **i;
|
|
|
|
|
|
|
|
item = hashmap_get(hosts->by_address, local_in_addrs + j);
|
|
|
|
if (!item)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Check whether all hostnames the loopback address points to are localhost ones */
|
|
|
|
all_localhost = true;
|
|
|
|
STRV_FOREACH(i, item->names)
|
|
|
|
if (!is_localhost(*i)) {
|
|
|
|
all_localhost = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!all_localhost) /* Not all names are localhost, hence keep the entries for this address. */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Now check if the names listed for this address actually all point back just to this
|
|
|
|
* address (or the other loopback address). If not, let's stay away from this too. */
|
|
|
|
in_order = true;
|
|
|
|
STRV_FOREACH(i, item->names) {
|
|
|
|
EtcHostsItemByName *n;
|
|
|
|
bool all_local_address;
|
|
|
|
|
|
|
|
n = hashmap_get(hosts->by_name, *i);
|
|
|
|
if (!n) /* No reverse entry? Then almost certainly the entry already got deleted from
|
|
|
|
* the previous iteration of this loop, i.e. via the other protocol */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Now check if the addresses of this item are all localhost addresses */
|
|
|
|
all_local_address = true;
|
|
|
|
for (size_t m = 0; m < n->n_addresses; m++)
|
|
|
|
if (!in_addr_is_localhost(n->addresses[m]->family, &n->addresses[m]->address)) {
|
|
|
|
all_local_address = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!all_local_address) {
|
|
|
|
in_order = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_order)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
STRV_FOREACH(i, item->names) {
|
|
|
|
EtcHostsItemByName *n;
|
|
|
|
|
|
|
|
n = hashmap_remove(hosts->by_name, *i);
|
|
|
|
if (n)
|
|
|
|
etc_hosts_item_by_name_free(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_se(hashmap_remove(hosts->by_address, local_in_addrs + j) == item);
|
|
|
|
etc_hosts_item_free(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 11:01:21 +02:00
|
|
|
int etc_hosts_parse(EtcHosts *hosts, FILE *f) {
|
2018-07-31 10:01:46 +02:00
|
|
|
_cleanup_(etc_hosts_free) EtcHosts t = {};
|
|
|
|
unsigned nr = 0;
|
|
|
|
int r;
|
|
|
|
|
2018-10-18 16:16:52 +02:00
|
|
|
for (;;) {
|
|
|
|
_cleanup_free_ char *line = NULL;
|
2018-07-31 10:01:46 +02:00
|
|
|
char *l;
|
|
|
|
|
2018-10-18 16:16:52 +02:00
|
|
|
r = read_line(f, LONG_LINE_MAX, &line);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to read /etc/hosts: %m");
|
|
|
|
if (r == 0)
|
|
|
|
break;
|
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
nr++;
|
|
|
|
|
2018-11-23 15:52:38 +01:00
|
|
|
l = strchr(line, '#');
|
|
|
|
if (l)
|
|
|
|
*l = '\0';
|
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
l = strstrip(line);
|
|
|
|
if (isempty(l))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = parse_line(&t, nr, l);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:30:25 +01:00
|
|
|
strip_localhost(&t);
|
|
|
|
|
2018-08-01 12:41:54 +02:00
|
|
|
etc_hosts_free(hosts);
|
2018-07-31 10:01:46 +02:00
|
|
|
*hosts = t;
|
|
|
|
t = (EtcHosts) {}; /* prevent cleanup */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-28 13:36:13 +02:00
|
|
|
static int manager_etc_hosts_read(Manager *m) {
|
2016-01-22 12:24:20 +01:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
|
|
|
struct stat st;
|
|
|
|
usec_t ts;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_se(sd_event_now(m->event, clock_boottime_or_monotonic(), &ts) >= 0);
|
|
|
|
|
|
|
|
/* See if we checked /etc/hosts recently already */
|
|
|
|
if (m->etc_hosts_last != USEC_INFINITY && m->etc_hosts_last + ETC_HOSTS_RECHECK_USEC > ts)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
m->etc_hosts_last = ts;
|
|
|
|
|
2020-11-06 14:56:16 +01:00
|
|
|
if (m->etc_hosts_stat.st_mode != 0) {
|
2016-01-22 12:24:20 +01:00
|
|
|
if (stat("/etc/hosts", &st) < 0) {
|
2018-07-31 10:01:46 +02:00
|
|
|
if (errno != ENOENT)
|
|
|
|
return log_error_errno(errno, "Failed to stat /etc/hosts: %m");
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
manager_etc_hosts_flush(m);
|
|
|
|
return 0;
|
2016-01-22 12:24:20 +01:00
|
|
|
}
|
|
|
|
|
2020-03-22 21:40:18 +01:00
|
|
|
/* Did the mtime or ino/dev change? If not, there's no point in re-reading the file. */
|
2020-11-06 14:56:16 +01:00
|
|
|
if (stat_inode_unmodified(&m->etc_hosts_stat, &st))
|
2016-01-22 12:24:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = fopen("/etc/hosts", "re");
|
|
|
|
if (!f) {
|
2018-07-31 10:01:46 +02:00
|
|
|
if (errno != ENOENT)
|
|
|
|
return log_error_errno(errno, "Failed to open /etc/hosts: %m");
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
manager_etc_hosts_flush(m);
|
|
|
|
return 0;
|
2016-01-22 12:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
|
|
|
|
* invocation */
|
|
|
|
r = fstat(fileno(f), &st);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(errno, "Failed to fstat() /etc/hosts: %m");
|
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
r = etc_hosts_parse(&m->etc_hosts, f);
|
2018-08-01 12:41:54 +02:00
|
|
|
if (r < 0)
|
2018-07-31 10:01:46 +02:00
|
|
|
return r;
|
2016-01-22 12:24:20 +01:00
|
|
|
|
2020-11-06 14:56:16 +01:00
|
|
|
m->etc_hosts_stat = st;
|
2016-01-22 12:24:20 +01:00
|
|
|
m->etc_hosts_last = ts;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) {
|
|
|
|
bool found_a = false, found_aaaa = false;
|
2018-07-31 08:46:28 +02:00
|
|
|
struct in_addr_data k = {};
|
2016-01-22 12:24:20 +01:00
|
|
|
EtcHostsItemByName *bn;
|
|
|
|
DnsResourceKey *t;
|
|
|
|
const char *name;
|
|
|
|
unsigned i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(q);
|
|
|
|
assert(answer);
|
|
|
|
|
2018-07-28 14:46:00 +02:00
|
|
|
if (!m->read_etc_hosts)
|
|
|
|
return 0;
|
|
|
|
|
2018-08-01 12:41:54 +02:00
|
|
|
(void) manager_etc_hosts_read(m);
|
2016-01-22 12:24:20 +01:00
|
|
|
|
|
|
|
name = dns_question_first_name(q);
|
|
|
|
if (!name)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = dns_name_address(name, &k.family, &k.address);
|
|
|
|
if (r > 0) {
|
|
|
|
EtcHostsItem *item;
|
|
|
|
DnsResourceKey *found_ptr = NULL;
|
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
item = hashmap_get(m->etc_hosts.by_address, &k);
|
2016-01-22 12:24:20 +01:00
|
|
|
if (!item)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
|
|
|
|
* we'll only return if the request was for PTR. */
|
|
|
|
|
|
|
|
DNS_QUESTION_FOREACH(t, q) {
|
|
|
|
if (!IN_SET(t->type, DNS_TYPE_PTR, DNS_TYPE_ANY))
|
|
|
|
continue;
|
|
|
|
if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY))
|
|
|
|
continue;
|
|
|
|
|
2016-02-13 20:54:15 +01:00
|
|
|
r = dns_name_equal(dns_resource_key_name(t), name);
|
2016-01-22 12:24:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r > 0) {
|
|
|
|
found_ptr = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_ptr) {
|
|
|
|
char **n;
|
|
|
|
|
|
|
|
r = dns_answer_reserve(answer, strv_length(item->names));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
STRV_FOREACH(n, item->names) {
|
|
|
|
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
|
|
|
|
|
|
|
|
rr = dns_resource_record_new(found_ptr);
|
|
|
|
if (!rr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
rr->ptr.name = strdup(*n);
|
|
|
|
if (!rr->ptr.name)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-07-31 10:01:46 +02:00
|
|
|
bn = hashmap_get(m->etc_hosts.by_name, name);
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
if (bn) {
|
|
|
|
r = dns_answer_reserve(answer, bn->n_addresses);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
/* Check if name was listed with no address. If yes, continue to return an answer. */
|
|
|
|
if (!set_contains(m->etc_hosts.no_address, name))
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-22 12:24:20 +01:00
|
|
|
|
|
|
|
DNS_QUESTION_FOREACH(t, q) {
|
|
|
|
if (!IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_ANY))
|
|
|
|
continue;
|
|
|
|
if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY))
|
|
|
|
continue;
|
|
|
|
|
2016-02-13 20:54:15 +01:00
|
|
|
r = dns_name_equal(dns_resource_key_name(t), name);
|
2016-01-22 12:24:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_ANY))
|
|
|
|
found_a = true;
|
|
|
|
if (IN_SET(t->type, DNS_TYPE_AAAA, DNS_TYPE_ANY))
|
|
|
|
found_aaaa = true;
|
|
|
|
|
|
|
|
if (found_a && found_aaaa)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
for (i = 0; bn && i < bn->n_addresses; i++) {
|
2016-01-22 12:24:20 +01:00
|
|
|
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
|
|
|
|
|
2018-07-31 08:46:28 +02:00
|
|
|
if ((!found_a && bn->addresses[i]->family == AF_INET) ||
|
|
|
|
(!found_aaaa && bn->addresses[i]->family == AF_INET6))
|
2016-01-22 12:24:20 +01:00
|
|
|
continue;
|
|
|
|
|
2018-07-31 08:46:28 +02:00
|
|
|
r = dns_resource_record_new_address(&rr, bn->addresses[i]->family, &bn->addresses[i]->address, bn->name);
|
2016-01-22 12:24:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-12-22 07:58:02 +01:00
|
|
|
return found_a || found_aaaa;
|
2016-01-22 12:24:20 +01:00
|
|
|
}
|