1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "hostname-util.h"
10 #include "resolved-dns-synthesize.h"
11 #include "resolved-etc-hosts.h"
12 #include "socket-netlink.h"
13 #include "stat-util.h"
14 #include "string-util.h"
16 #include "time-util.h"
18 /* Recheck /etc/hosts at most once every 2s */
19 #define ETC_HOSTS_RECHECK_USEC (2*USEC_PER_SEC)
21 static EtcHostsItemByAddress
*etc_hosts_item_by_address_free(EtcHostsItemByAddress
*item
) {
25 set_free(item
->names
);
29 DEFINE_TRIVIAL_CLEANUP_FUNC(EtcHostsItemByAddress
*, etc_hosts_item_by_address_free
);
31 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
34 in_addr_data_hash_func
,
35 in_addr_data_compare_func
,
36 EtcHostsItemByAddress
,
37 etc_hosts_item_by_address_free
);
39 static EtcHostsItemByName
*etc_hosts_item_by_name_free(EtcHostsItemByName
*item
) {
44 set_free(item
->addresses
);
48 DEFINE_TRIVIAL_CLEANUP_FUNC(EtcHostsItemByName
*, etc_hosts_item_by_name_free
);
50 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
54 dns_name_compare_func
,
56 etc_hosts_item_by_name_free
);
58 void etc_hosts_clear(EtcHosts
*hosts
) {
61 hosts
->by_address
= hashmap_free(hosts
->by_address
);
62 hosts
->by_name
= hashmap_free(hosts
->by_name
);
63 hosts
->no_address
= set_free(hosts
->no_address
);
66 void manager_etc_hosts_flush(Manager
*m
) {
67 etc_hosts_clear(&m
->etc_hosts
);
68 m
->etc_hosts_stat
= (struct stat
) {};
71 static int parse_line(EtcHosts
*hosts
, unsigned nr
, const char *line
) {
72 _cleanup_free_
char *address_str
= NULL
;
73 struct in_addr_data address
= {};
75 EtcHostsItemByAddress
*item
;
81 r
= extract_first_word(&line
, &address_str
, NULL
, EXTRACT_RELAX
);
83 return log_error_errno(r
, "/etc/hosts:%u: failed to extract address: %m", nr
);
84 assert(r
> 0); /* We already checked that the line is not empty, so it should contain *something* */
86 r
= in_addr_ifindex_from_string_auto(address_str
, &address
.family
, &address
.address
, NULL
);
88 log_warning_errno(r
, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr
, address_str
);
92 r
= in_addr_data_is_null(&address
);
94 log_warning_errno(r
, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr
, address_str
);
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
= hashmap_get(hosts
->by_address
, &address
);
106 _cleanup_(etc_hosts_item_by_address_freep
) EtcHostsItemByAddress
*new_item
= NULL
;
108 new_item
= new(EtcHostsItemByAddress
, 1);
112 *new_item
= (EtcHostsItemByAddress
) {
116 r
= hashmap_ensure_put(&hosts
->by_address
, &by_address_hash_ops
, &new_item
->address
, new_item
);
120 item
= TAKE_PTR(new_item
);
125 _cleanup_free_
char *name
= NULL
;
126 EtcHostsItemByName
*bn
;
128 r
= extract_first_word(&line
, &name
, NULL
, EXTRACT_RELAX
);
130 return log_error_errno(r
, "/etc/hosts:%u: couldn't extract hostname: %m", nr
);
134 r
= dns_name_is_valid_ldh(name
);
137 log_warning_errno(r
, "/etc/hosts:%u: Failed to check the validity of hostname \"%s\", ignoring: %m", nr
, name
);
139 log_warning("/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr
, name
);
146 /* Optimize the case where we don't need to store any addresses, by storing
147 * only the name in a dedicated Set instead of the hashmap */
149 r
= set_ensure_consume(&hosts
->no_address
, &dns_name_hash_ops_free
, TAKE_PTR(name
));
156 bn
= hashmap_get(hosts
->by_name
, name
);
158 _cleanup_(etc_hosts_item_by_name_freep
) EtcHostsItemByName
*new_item
= NULL
;
159 _cleanup_free_
char *name_copy
= NULL
;
161 name_copy
= strdup(name
);
165 new_item
= new(EtcHostsItemByName
, 1);
169 *new_item
= (EtcHostsItemByName
) {
170 .name
= TAKE_PTR(name_copy
),
173 r
= hashmap_ensure_put(&hosts
->by_name
, &by_name_hash_ops
, new_item
->name
, new_item
);
177 bn
= TAKE_PTR(new_item
);
180 if (!set_contains(bn
->addresses
, &address
)) {
181 _cleanup_free_
struct in_addr_data
*address_copy
= NULL
;
183 address_copy
= newdup(struct in_addr_data
, &address
, 1);
187 r
= set_ensure_consume(&bn
->addresses
, &in_addr_data_hash_ops_free
, TAKE_PTR(address_copy
));
192 r
= set_ensure_consume(&item
->names
, &dns_name_hash_ops_free
, TAKE_PTR(name
));
198 log_warning("/etc/hosts:%u: line is missing any valid hostnames", nr
);
203 static void strip_localhost(EtcHosts
*hosts
) {
204 static const struct in_addr_data local_in_addrs
[] = {
207 #if __BYTE_ORDER == __LITTLE_ENDIAN
208 /* We want constant expressions here, that's why we don't use htole32() here */
209 .address
.in
.s_addr
= UINT32_C(0x0100007F),
211 .address
.in
.s_addr
= UINT32_C(0x7F000001),
216 .address
.in6
= IN6ADDR_LOOPBACK_INIT
,
222 /* Removes the 'localhost' entry from what we loaded. But only if the mapping is exclusively between
223 * 127.0.0.1 and localhost (or aliases to that we recognize). If there's any other name assigned to
224 * it, we leave the entry in.
226 * This way our regular synthesizing can take over, but only if it would result in the exact same
229 for (size_t j
= 0; j
< ELEMENTSOF(local_in_addrs
); j
++) {
230 bool all_localhost
, all_local_address
;
231 EtcHostsItemByAddress
*item
;
234 item
= hashmap_get(hosts
->by_address
, local_in_addrs
+ j
);
238 /* Check whether all hostnames the loopback address points to are localhost ones */
239 all_localhost
= true;
240 SET_FOREACH(name
, item
->names
)
241 if (!is_localhost(name
)) {
242 all_localhost
= false;
246 if (!all_localhost
) /* Not all names are localhost, hence keep the entries for this address. */
249 /* Now check if the names listed for this address actually all point back just to this
250 * address (or the other loopback address). If not, let's stay away from this too. */
251 all_local_address
= true;
252 SET_FOREACH(name
, item
->names
) {
253 EtcHostsItemByName
*n
;
254 struct in_addr_data
*a
;
256 n
= hashmap_get(hosts
->by_name
, name
);
257 if (!n
) /* No reverse entry? Then almost certainly the entry already got deleted from
258 * the previous iteration of this loop, i.e. via the other protocol */
261 /* Now check if the addresses of this item are all localhost addresses */
262 SET_FOREACH(a
, n
->addresses
)
263 if (!in_addr_is_localhost(a
->family
, &a
->address
)) {
264 all_local_address
= false;
268 if (!all_local_address
)
272 if (!all_local_address
)
275 SET_FOREACH(name
, item
->names
)
276 etc_hosts_item_by_name_free(hashmap_remove(hosts
->by_name
, name
));
278 assert_se(hashmap_remove(hosts
->by_address
, local_in_addrs
+ j
) == item
);
279 etc_hosts_item_by_address_free(item
);
283 int etc_hosts_parse(EtcHosts
*hosts
, FILE *f
) {
284 _cleanup_(etc_hosts_clear
) EtcHosts t
= {};
289 _cleanup_free_
char *line
= NULL
;
292 r
= read_line(f
, LONG_LINE_MAX
, &line
);
294 return log_error_errno(r
, "Failed to read /etc/hosts: %m");
300 l
= strchr(line
, '#');
308 r
= parse_line(&t
, nr
, l
);
315 etc_hosts_clear(hosts
);
316 *hosts
= TAKE_STRUCT(t
);
320 static int manager_etc_hosts_read(Manager
*m
) {
321 _cleanup_fclose_
FILE *f
= NULL
;
326 assert_se(sd_event_now(m
->event
, CLOCK_BOOTTIME
, &ts
) >= 0);
328 /* See if we checked /etc/hosts recently already */
329 if (m
->etc_hosts_last
!= USEC_INFINITY
&& m
->etc_hosts_last
+ ETC_HOSTS_RECHECK_USEC
> ts
)
332 m
->etc_hosts_last
= ts
;
334 if (m
->etc_hosts_stat
.st_mode
!= 0) {
335 if (stat("/etc/hosts", &st
) < 0) {
337 return log_error_errno(errno
, "Failed to stat /etc/hosts: %m");
339 manager_etc_hosts_flush(m
);
343 /* Did the mtime or ino/dev change? If not, there's no point in re-reading the file. */
344 if (stat_inode_unmodified(&m
->etc_hosts_stat
, &st
))
348 f
= fopen("/etc/hosts", "re");
351 return log_error_errno(errno
, "Failed to open /etc/hosts: %m");
353 manager_etc_hosts_flush(m
);
357 /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
359 r
= fstat(fileno(f
), &st
);
361 return log_error_errno(errno
, "Failed to fstat() /etc/hosts: %m");
363 r
= etc_hosts_parse(&m
->etc_hosts
, f
);
367 m
->etc_hosts_stat
= st
;
368 m
->etc_hosts_last
= ts
;
373 static int etc_hosts_lookup_by_address(
377 const struct in_addr_data
*address
,
378 DnsAnswer
**answer
) {
380 DnsResourceKey
*t
, *found_ptr
= NULL
;
381 EtcHostsItemByAddress
*item
;
390 item
= hashmap_get(hosts
->by_address
, address
);
394 /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
395 * we'll only return if the request was for PTR. */
397 DNS_QUESTION_FOREACH(t
, q
) {
398 if (!IN_SET(t
->type
, DNS_TYPE_PTR
, DNS_TYPE_ANY
))
400 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
403 r
= dns_name_equal(dns_resource_key_name(t
), name
);
415 r
= dns_answer_reserve(answer
, set_size(item
->names
));
419 SET_FOREACH(n
, item
->names
) {
420 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
422 rr
= dns_resource_record_new(found_ptr
);
426 rr
->ptr
.name
= strdup(n
);
430 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
, NULL
);
439 static int etc_hosts_lookup_by_name(
443 DnsAnswer
**answer
) {
445 bool found_a
= false, found_aaaa
= false;
446 const struct in_addr_data
*a
;
447 EtcHostsItemByName
*item
;
456 item
= hashmap_get(hosts
->by_name
, name
);
458 r
= dns_answer_reserve(answer
, set_size(item
->addresses
));
462 /* Check if name was listed with no address. If yes, continue to return an answer. */
463 if (!set_contains(hosts
->no_address
, name
))
467 DNS_QUESTION_FOREACH(t
, q
) {
468 if (!IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
470 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
473 r
= dns_name_equal(dns_resource_key_name(t
), name
);
479 if (IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_ANY
))
481 if (IN_SET(t
->type
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
484 if (found_a
&& found_aaaa
)
488 SET_FOREACH(a
, item
? item
->addresses
: NULL
) {
489 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
491 if ((!found_a
&& a
->family
== AF_INET
) ||
492 (!found_aaaa
&& a
->family
== AF_INET6
))
495 r
= dns_resource_record_new_address(&rr
, a
->family
, &a
->address
, item
->name
);
499 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
, NULL
);
504 return found_a
|| found_aaaa
;
507 int manager_etc_hosts_lookup(Manager
*m
, DnsQuestion
*q
, DnsAnswer
**answer
) {
508 struct in_addr_data k
;
515 if (!m
->read_etc_hosts
)
518 (void) manager_etc_hosts_read(m
);
520 name
= dns_question_first_name(q
);
524 if (dns_name_address(name
, &k
.family
, &k
.address
) > 0)
525 return etc_hosts_lookup_by_address(&m
->etc_hosts
, q
, name
, &k
, answer
);
527 return etc_hosts_lookup_by_name(&m
->etc_hosts
, q
, name
, answer
);