2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2018-07-31 11:01:21 +02:00
|
|
|
|
2018-11-23 15:52:38 +01:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2018-07-31 11:01:21 +02:00
|
|
|
#include "fd-util.h"
|
2018-11-23 15:52:38 +01:00
|
|
|
#include "fileio.h"
|
2018-07-31 11:01:21 +02:00
|
|
|
#include "fs-util.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "resolved-etc-hosts.h"
|
2018-11-23 15:52:38 +01:00
|
|
|
#include "strv.h"
|
2019-01-20 18:21:09 +01:00
|
|
|
#include "tests.h"
|
2018-11-30 21:05:27 +01:00
|
|
|
#include "tmpfile-util.h"
|
2018-07-31 11:01:21 +02:00
|
|
|
|
|
|
|
static void test_parse_etc_hosts_system(void) {
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
|
|
|
|
2018-11-21 14:55:07 +01:00
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
|
2018-08-03 16:48:53 +02:00
|
|
|
f = fopen("/etc/hosts", "re");
|
2018-07-31 11:01:21 +02:00
|
|
|
if (!f) {
|
2018-12-06 15:54:46 +01:00
|
|
|
assert_se(errno == ENOENT);
|
2018-07-31 11:01:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cleanup_(etc_hosts_free) EtcHosts hosts = {};
|
|
|
|
assert_se(etc_hosts_parse(&hosts, f) == 0);
|
|
|
|
}
|
|
|
|
|
2018-11-23 15:52:38 +01:00
|
|
|
#define address_equal_4(_addr, _address) \
|
|
|
|
((_addr)->family == AF_INET && \
|
|
|
|
!memcmp(&(_addr)->address.in, &(struct in_addr) { .s_addr = (_address) }, 4))
|
|
|
|
|
|
|
|
#define address_equal_6(_addr, ...) \
|
|
|
|
((_addr)->family == AF_INET6 && \
|
|
|
|
!memcmp(&(_addr)->address.in6, &(struct in6_addr) { .s6_addr = __VA_ARGS__}, 16) )
|
|
|
|
|
2018-11-21 14:55:07 +01:00
|
|
|
static void test_parse_etc_hosts(void) {
|
2018-07-31 11:01:21 +02:00
|
|
|
_cleanup_(unlink_tempfilep) char
|
|
|
|
t[] = "/tmp/test-resolved-etc-hosts.XXXXXX";
|
|
|
|
|
2018-11-21 14:55:07 +01:00
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
|
2018-07-31 11:01:21 +02:00
|
|
|
int fd;
|
|
|
|
_cleanup_fclose_ FILE *f;
|
2018-11-23 15:52:38 +01:00
|
|
|
const char *s;
|
2018-07-31 11:01:21 +02:00
|
|
|
|
2018-11-21 14:55:07 +01:00
|
|
|
fd = mkostemp_safe(t);
|
|
|
|
assert_se(fd >= 0);
|
|
|
|
|
|
|
|
f = fdopen(fd, "r+");
|
|
|
|
assert_se(f);
|
2018-11-23 15:52:38 +01:00
|
|
|
fputs("1.2.3.4 some.where\n"
|
|
|
|
"1.2.3.5 some.where\n"
|
|
|
|
"1.2.3.6 dash dash-dash.where-dash\n"
|
|
|
|
"1.2.3.7 bad-dash- -bad-dash -bad-dash.bad-\n"
|
|
|
|
"1.2.3.8\n"
|
|
|
|
"1.2.3.9 before.comment # within.comment\n"
|
|
|
|
"1.2.3.10 before.comment#within.comment2\n"
|
|
|
|
"1.2.3.11 before.comment# within.comment3\n"
|
|
|
|
"1.2.3.12 before.comment#\n"
|
|
|
|
"1.2.3 short.address\n"
|
|
|
|
"1.2.3.4.5 long.address\n"
|
|
|
|
"1::2::3 multi.colon\n"
|
|
|
|
|
|
|
|
"::0 some.where some.other\n"
|
2020-06-23 08:31:16 +02:00
|
|
|
"0.0.0.0 deny.listed\n"
|
2018-11-23 15:52:38 +01:00
|
|
|
"::5\t\t\t \tsome.where\tsome.other foobar.foo.foo\t\t\t\n"
|
|
|
|
" \n", f);
|
|
|
|
assert_se(fflush_and_check(f) >= 0);
|
2018-11-21 14:55:07 +01:00
|
|
|
rewind(f);
|
2018-07-31 11:01:21 +02:00
|
|
|
|
|
|
|
_cleanup_(etc_hosts_free) EtcHosts hosts = {};
|
|
|
|
assert_se(etc_hosts_parse(&hosts, f) == 0);
|
|
|
|
|
|
|
|
EtcHostsItemByName *bn;
|
|
|
|
assert_se(bn = hashmap_get(hosts.by_name, "some.where"));
|
|
|
|
assert_se(bn->n_addresses == 3);
|
|
|
|
assert_se(bn->n_allocated >= 3);
|
2018-11-23 15:52:38 +01:00
|
|
|
assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.4")));
|
|
|
|
assert_se(address_equal_4(bn->addresses[1], inet_addr("1.2.3.5")));
|
|
|
|
assert_se(address_equal_6(bn->addresses[2], {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}));
|
2018-07-31 11:01:21 +02:00
|
|
|
|
2018-11-23 15:52:38 +01:00
|
|
|
assert_se(bn = hashmap_get(hosts.by_name, "dash"));
|
|
|
|
assert_se(bn->n_addresses == 1);
|
|
|
|
assert_se(bn->n_allocated >= 1);
|
|
|
|
assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.6")));
|
|
|
|
|
|
|
|
assert_se(bn = hashmap_get(hosts.by_name, "dash-dash.where-dash"));
|
|
|
|
assert_se(bn->n_addresses == 1);
|
|
|
|
assert_se(bn->n_allocated >= 1);
|
|
|
|
assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.6")));
|
|
|
|
|
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
|
|
|
/* See https://tools.ietf.org/html/rfc1035#section-2.3.1 */
|
|
|
|
FOREACH_STRING(s, "bad-dash-", "-bad-dash", "-bad-dash.bad-")
|
|
|
|
assert_se(!hashmap_get(hosts.by_name, s));
|
2018-07-31 11:01:21 +02:00
|
|
|
|
2018-11-23 15:52:38 +01:00
|
|
|
assert_se(bn = hashmap_get(hosts.by_name, "before.comment"));
|
|
|
|
assert_se(bn->n_addresses == 4);
|
|
|
|
assert_se(bn->n_allocated >= 4);
|
|
|
|
assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.9")));
|
|
|
|
assert_se(address_equal_4(bn->addresses[1], inet_addr("1.2.3.10")));
|
|
|
|
assert_se(address_equal_4(bn->addresses[2], inet_addr("1.2.3.11")));
|
|
|
|
assert_se(address_equal_4(bn->addresses[3], inet_addr("1.2.3.12")));
|
|
|
|
|
|
|
|
assert(!hashmap_get(hosts.by_name, "within.comment"));
|
|
|
|
assert(!hashmap_get(hosts.by_name, "within.comment2"));
|
|
|
|
assert(!hashmap_get(hosts.by_name, "within.comment3"));
|
|
|
|
assert(!hashmap_get(hosts.by_name, "#"));
|
|
|
|
|
|
|
|
assert(!hashmap_get(hosts.by_name, "short.address"));
|
|
|
|
assert(!hashmap_get(hosts.by_name, "long.address"));
|
|
|
|
assert(!hashmap_get(hosts.by_name, "multi.colon"));
|
|
|
|
assert_se(!set_contains(hosts.no_address, "short.address"));
|
|
|
|
assert_se(!set_contains(hosts.no_address, "long.address"));
|
|
|
|
assert_se(!set_contains(hosts.no_address, "multi.colon"));
|
2018-07-31 11:01:21 +02:00
|
|
|
|
|
|
|
assert_se(bn = hashmap_get(hosts.by_name, "some.other"));
|
|
|
|
assert_se(bn->n_addresses == 1);
|
|
|
|
assert_se(bn->n_allocated >= 1);
|
2018-11-23 15:52:38 +01:00
|
|
|
assert_se(address_equal_6(bn->addresses[0], {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}));
|
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
|
|
|
|
|
|
|
assert_se( set_contains(hosts.no_address, "some.where"));
|
|
|
|
assert_se( set_contains(hosts.no_address, "some.other"));
|
2020-06-23 08:31:16 +02:00
|
|
|
assert_se( set_contains(hosts.no_address, "deny.listed"));
|
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
|
|
|
assert_se(!set_contains(hosts.no_address, "foobar.foo.foo"));
|
2018-07-31 11:01:21 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 14:55:07 +01:00
|
|
|
static void test_parse_file(const char *fname) {
|
|
|
|
_cleanup_(etc_hosts_free) EtcHosts hosts = {};
|
|
|
|
_cleanup_fclose_ FILE *f;
|
|
|
|
|
|
|
|
log_info("/* %s(\"%s\") */", __func__, fname);
|
|
|
|
|
|
|
|
assert_se(f = fopen(fname, "re"));
|
|
|
|
assert_se(etc_hosts_parse(&hosts, f) == 0);
|
|
|
|
}
|
|
|
|
|
2018-07-31 11:01:21 +02:00
|
|
|
int main(int argc, char **argv) {
|
2019-01-20 18:21:09 +01:00
|
|
|
test_setup_logging(LOG_DEBUG);
|
2018-07-31 11:01:21 +02:00
|
|
|
|
2018-11-21 14:55:07 +01:00
|
|
|
if (argc == 1) {
|
2018-07-31 11:01:21 +02:00
|
|
|
test_parse_etc_hosts_system();
|
2018-11-21 14:55:07 +01:00
|
|
|
test_parse_etc_hosts();
|
|
|
|
} else
|
|
|
|
test_parse_file(argv[1]);
|
2018-07-31 11:01:21 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|