1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "hostname-util.h"
6 #include "resolved-etc-hosts.h"
7 #include "resolved-dns-synthesize.h"
8 #include "string-util.h"
10 #include "time-util.h"
12 /* Recheck /etc/hosts at most once every 2s */
13 #define ETC_HOSTS_RECHECK_USEC (2*USEC_PER_SEC)
15 typedef struct EtcHostsItem
{
17 union in_addr_union address
;
22 typedef struct EtcHostsItemByName
{
26 size_t n_items
, n_allocated
;
29 void manager_etc_hosts_flush(Manager
*m
) {
31 EtcHostsItemByName
*bn
;
33 while ((item
= set_steal_first(m
->etc_hosts_by_address
))) {
34 strv_free(item
->names
);
38 while ((bn
= hashmap_steal_first(m
->etc_hosts_by_name
))) {
44 m
->etc_hosts_by_address
= set_free(m
->etc_hosts_by_address
);
45 m
->etc_hosts_by_name
= hashmap_free(m
->etc_hosts_by_name
);
47 m
->etc_hosts_mtime
= USEC_INFINITY
;
50 static void etc_hosts_item_hash_func(const void *p
, struct siphash
*state
) {
51 const EtcHostsItem
*item
= p
;
53 siphash24_compress(&item
->family
, sizeof(item
->family
), state
);
55 if (item
->family
== AF_INET
)
56 siphash24_compress(&item
->address
.in
, sizeof(item
->address
.in
), state
);
57 else if (item
->family
== AF_INET6
)
58 siphash24_compress(&item
->address
.in6
, sizeof(item
->address
.in6
), state
);
61 static int etc_hosts_item_compare_func(const void *a
, const void *b
) {
62 const EtcHostsItem
*x
= a
, *y
= b
;
64 if (x
->family
!= y
->family
)
65 return x
->family
- y
->family
;
67 if (x
->family
== AF_INET
)
68 return memcmp(&x
->address
.in
.s_addr
, &y
->address
.in
.s_addr
, sizeof(struct in_addr
));
70 if (x
->family
== AF_INET6
)
71 return memcmp(&x
->address
.in6
.s6_addr
, &y
->address
.in6
.s6_addr
, sizeof(struct in6_addr
));
73 return trivial_compare_func(a
, b
);
76 static const struct hash_ops etc_hosts_item_ops
= {
77 .hash
= etc_hosts_item_hash_func
,
78 .compare
= etc_hosts_item_compare_func
,
81 static int add_item(Manager
*m
, int family
, const union in_addr_union
*address
, char **names
) {
82 _cleanup_strv_free_
char **dummy
= names
;
94 r
= in_addr_is_null(family
, address
);
98 /* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to
102 /* If this is a normal address, then, simply add entry mapping it to the specified names */
104 item
= set_get(m
->etc_hosts_by_address
, &key
);
106 r
= strv_extend_strv(&item
->names
, names
, true);
111 r
= set_ensure_allocated(&m
->etc_hosts_by_address
, &etc_hosts_item_ops
);
115 item
= new(EtcHostsItem
, 1);
119 *item
= (EtcHostsItem
) {
125 r
= set_put(m
->etc_hosts_by_address
, item
);
135 STRV_FOREACH(n
, names
) {
136 EtcHostsItemByName
*bn
;
138 bn
= hashmap_get(m
->etc_hosts_by_name
, *n
);
140 r
= hashmap_ensure_allocated(&m
->etc_hosts_by_name
, &dns_name_hash_ops
);
144 bn
= new0(EtcHostsItemByName
, 1);
148 bn
->name
= strdup(*n
);
154 r
= hashmap_put(m
->etc_hosts_by_name
, bn
->name
, bn
);
163 if (!GREEDY_REALLOC(bn
->items
, bn
->n_allocated
, bn
->n_items
+1))
166 bn
->items
[bn
->n_items
++] = item
;
173 static int parse_line(Manager
*m
, unsigned nr
, const char *line
) {
174 _cleanup_free_
char *address
= NULL
;
175 _cleanup_strv_free_
char **names
= NULL
;
176 union in_addr_union in
;
177 bool suppressed
= false;
183 r
= extract_first_word(&line
, &address
, NULL
, EXTRACT_RELAX
);
185 return log_error_errno(r
, "Couldn't extract address, in line /etc/hosts:%u.", nr
);
187 log_error("Premature end of line, in line /etc/hosts:%u.", nr
);
191 r
= in_addr_from_string_auto(address
, &family
, &in
);
193 return log_error_errno(r
, "Address '%s' is invalid, in line /etc/hosts:%u.", address
, nr
);
196 _cleanup_free_
char *name
= NULL
;
198 r
= extract_first_word(&line
, &name
, NULL
, EXTRACT_RELAX
);
200 return log_error_errno(r
, "Couldn't extract host name, in line /etc/hosts:%u.", nr
);
204 r
= dns_name_is_valid(name
);
206 return log_error_errno(r
, "Hostname %s is not valid, ignoring, in line /etc/hosts:%u.", name
, nr
);
208 if (is_localhost(name
)) {
209 /* Suppress the "localhost" line that is often seen */
214 r
= strv_push(&names
, name
);
221 if (strv_isempty(names
)) {
226 log_error("Line is missing any host names, in line /etc/hosts:%u.", nr
);
230 /* Takes possession of the names strv */
231 r
= add_item(m
, family
, &in
, names
);
239 static int manager_etc_hosts_read(Manager
*m
) {
240 _cleanup_fclose_
FILE *f
= NULL
;
247 assert_se(sd_event_now(m
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
249 /* See if we checked /etc/hosts recently already */
250 if (m
->etc_hosts_last
!= USEC_INFINITY
&& m
->etc_hosts_last
+ ETC_HOSTS_RECHECK_USEC
> ts
)
253 m
->etc_hosts_last
= ts
;
255 if (m
->etc_hosts_mtime
!= USEC_INFINITY
) {
256 if (stat("/etc/hosts", &st
) < 0) {
257 if (errno
== ENOENT
) {
262 return log_error_errno(errno
, "Failed to stat /etc/hosts: %m");
265 /* Did the mtime change? If not, there's no point in re-reading the file. */
266 if (timespec_load(&st
.st_mtim
) == m
->etc_hosts_mtime
)
270 f
= fopen("/etc/hosts", "re");
272 if (errno
== ENOENT
) {
277 return log_error_errno(errno
, "Failed to open /etc/hosts: %m");
280 /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
282 r
= fstat(fileno(f
), &st
);
284 return log_error_errno(errno
, "Failed to fstat() /etc/hosts: %m");
286 manager_etc_hosts_flush(m
);
288 FOREACH_LINE(line
, f
, return log_error_errno(errno
, "Failed to read /etc/hosts: %m")) {
299 r
= parse_line(m
, nr
, l
);
300 if (r
== -ENOMEM
) /* On OOM we abandon the half-built-up structure. All other errors we ignore and proceed */
304 m
->etc_hosts_mtime
= timespec_load(&st
.st_mtim
);
305 m
->etc_hosts_last
= ts
;
310 manager_etc_hosts_flush(m
);
314 int manager_etc_hosts_lookup(Manager
*m
, DnsQuestion
* q
, DnsAnswer
**answer
) {
315 bool found_a
= false, found_aaaa
= false;
316 EtcHostsItemByName
*bn
;
327 if (!m
->read_etc_hosts
)
330 r
= manager_etc_hosts_read(m
);
334 name
= dns_question_first_name(q
);
338 r
= dns_name_address(name
, &k
.family
, &k
.address
);
341 DnsResourceKey
*found_ptr
= NULL
;
343 item
= set_get(m
->etc_hosts_by_address
, &k
);
347 /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
348 * we'll only return if the request was for PTR. */
350 DNS_QUESTION_FOREACH(t
, q
) {
351 if (!IN_SET(t
->type
, DNS_TYPE_PTR
, DNS_TYPE_ANY
))
353 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
356 r
= dns_name_equal(dns_resource_key_name(t
), name
);
368 r
= dns_answer_reserve(answer
, strv_length(item
->names
));
372 STRV_FOREACH(n
, item
->names
) {
373 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
375 rr
= dns_resource_record_new(found_ptr
);
379 rr
->ptr
.name
= strdup(*n
);
383 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
392 bn
= hashmap_get(m
->etc_hosts_by_name
, name
);
396 r
= dns_answer_reserve(answer
, bn
->n_items
);
400 DNS_QUESTION_FOREACH(t
, q
) {
401 if (!IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
403 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
406 r
= dns_name_equal(dns_resource_key_name(t
), name
);
412 if (IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_ANY
))
414 if (IN_SET(t
->type
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
417 if (found_a
&& found_aaaa
)
421 for (i
= 0; i
< bn
->n_items
; i
++) {
422 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
424 if ((!found_a
&& bn
->items
[i
]->family
== AF_INET
) ||
425 (!found_aaaa
&& bn
->items
[i
]->family
== AF_INET6
))
428 r
= dns_resource_record_new_address(&rr
, bn
->items
[i
]->family
, &bn
->items
[i
]->address
, bn
->name
);
432 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
437 return found_a
|| found_aaaa
;