1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "hostname-util.h"
10 #include "resolved-dns-synthesize.h"
11 #include "resolved-etc-hosts.h"
12 #include "string-util.h"
14 #include "time-util.h"
16 /* Recheck /etc/hosts at most once every 2s */
17 #define ETC_HOSTS_RECHECK_USEC (2*USEC_PER_SEC)
19 static void etc_hosts_item_free(EtcHostsItem
*item
) {
20 strv_free(item
->names
);
24 static void etc_hosts_item_by_name_free(EtcHostsItemByName
*item
) {
26 free(item
->addresses
);
30 void etc_hosts_free(EtcHosts
*hosts
) {
31 hosts
->by_address
= hashmap_free_with_destructor(hosts
->by_address
, etc_hosts_item_free
);
32 hosts
->by_name
= hashmap_free_with_destructor(hosts
->by_name
, etc_hosts_item_by_name_free
);
33 hosts
->no_address
= set_free_free(hosts
->no_address
);
36 void manager_etc_hosts_flush(Manager
*m
) {
37 etc_hosts_free(&m
->etc_hosts
);
38 m
->etc_hosts_mtime
= USEC_INFINITY
;
41 static int parse_line(EtcHosts
*hosts
, unsigned nr
, const char *line
) {
42 _cleanup_free_
char *address_str
= NULL
;
43 struct in_addr_data address
= {};
51 r
= extract_first_word(&line
, &address_str
, NULL
, EXTRACT_RELAX
);
53 return log_error_errno(r
, "/etc/hosts:%u: failed to extract address: %m", nr
);
54 assert(r
> 0); /* We already checked that the line is not empty, so it should contain *something* */
56 r
= in_addr_ifindex_from_string_auto(address_str
, &address
.family
, &address
.address
, NULL
);
58 log_warning_errno(r
, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr
, address_str
);
62 r
= in_addr_is_null(address
.family
, &address
.address
);
64 log_warning_errno(r
, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr
, address_str
);
68 /* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to
72 /* If this is a normal address, then simply add entry mapping it to the specified names */
74 item
= hashmap_get(hosts
->by_address
, &address
);
76 r
= hashmap_ensure_allocated(&hosts
->by_address
, &in_addr_data_hash_ops
);
80 item
= new0(EtcHostsItem
, 1);
84 item
->address
= address
;
86 r
= hashmap_put(hosts
->by_address
, &item
->address
, item
);
95 _cleanup_free_
char *name
= NULL
;
96 EtcHostsItemByName
*bn
;
98 r
= extract_first_word(&line
, &name
, NULL
, EXTRACT_RELAX
);
100 return log_error_errno(r
, "/etc/hosts:%u: couldn't extract host name: %m", nr
);
106 r
= dns_name_is_valid_ldh(name
);
108 log_warning_errno(r
, "/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr
, name
);
112 if (is_localhost(name
))
113 /* Suppress the "localhost" line that is often seen */
117 /* Optimize the case where we don't need to store any addresses, by storing
118 * only the name in a dedicated Set instead of the hashmap */
120 r
= set_ensure_allocated(&hosts
->no_address
, &dns_name_hash_ops
);
124 r
= set_put(hosts
->no_address
, name
);
132 r
= strv_extend(&item
->names
, name
);
136 bn
= hashmap_get(hosts
->by_name
, name
);
138 r
= hashmap_ensure_allocated(&hosts
->by_name
, &dns_name_hash_ops
);
142 bn
= new0(EtcHostsItemByName
, 1);
146 r
= hashmap_put(hosts
->by_name
, name
, bn
);
152 bn
->name
= TAKE_PTR(name
);
155 if (!GREEDY_REALLOC(bn
->addresses
, bn
->n_allocated
, bn
->n_addresses
+ 1))
158 bn
->addresses
[bn
->n_addresses
++] = &item
->address
;
162 log_warning("/etc/hosts:%u: line is missing any host names", nr
);
167 int etc_hosts_parse(EtcHosts
*hosts
, FILE *f
) {
168 _cleanup_(etc_hosts_free
) EtcHosts t
= {};
173 _cleanup_free_
char *line
= NULL
;
176 r
= read_line(f
, LONG_LINE_MAX
, &line
);
178 return log_error_errno(r
, "Failed to read /etc/hosts: %m");
184 l
= strchr(line
, '#');
192 r
= parse_line(&t
, nr
, l
);
197 etc_hosts_free(hosts
);
199 t
= (EtcHosts
) {}; /* prevent cleanup */
203 static int manager_etc_hosts_read(Manager
*m
) {
204 _cleanup_fclose_
FILE *f
= NULL
;
209 assert_se(sd_event_now(m
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
211 /* See if we checked /etc/hosts recently already */
212 if (m
->etc_hosts_last
!= USEC_INFINITY
&& m
->etc_hosts_last
+ ETC_HOSTS_RECHECK_USEC
> ts
)
215 m
->etc_hosts_last
= ts
;
217 if (m
->etc_hosts_mtime
!= USEC_INFINITY
) {
218 if (stat("/etc/hosts", &st
) < 0) {
220 return log_error_errno(errno
, "Failed to stat /etc/hosts: %m");
222 manager_etc_hosts_flush(m
);
226 /* Did the mtime change? If not, there's no point in re-reading the file. */
227 if (timespec_load(&st
.st_mtim
) == m
->etc_hosts_mtime
)
231 f
= fopen("/etc/hosts", "re");
234 return log_error_errno(errno
, "Failed to open /etc/hosts: %m");
236 manager_etc_hosts_flush(m
);
240 /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
242 r
= fstat(fileno(f
), &st
);
244 return log_error_errno(errno
, "Failed to fstat() /etc/hosts: %m");
246 r
= etc_hosts_parse(&m
->etc_hosts
, f
);
250 m
->etc_hosts_mtime
= timespec_load(&st
.st_mtim
);
251 m
->etc_hosts_last
= ts
;
256 int manager_etc_hosts_lookup(Manager
*m
, DnsQuestion
* q
, DnsAnswer
**answer
) {
257 bool found_a
= false, found_aaaa
= false;
258 struct in_addr_data k
= {};
259 EtcHostsItemByName
*bn
;
269 if (!m
->read_etc_hosts
)
272 (void) manager_etc_hosts_read(m
);
274 name
= dns_question_first_name(q
);
278 r
= dns_name_address(name
, &k
.family
, &k
.address
);
281 DnsResourceKey
*found_ptr
= NULL
;
283 item
= hashmap_get(m
->etc_hosts
.by_address
, &k
);
287 /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
288 * we'll only return if the request was for PTR. */
290 DNS_QUESTION_FOREACH(t
, q
) {
291 if (!IN_SET(t
->type
, DNS_TYPE_PTR
, DNS_TYPE_ANY
))
293 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
296 r
= dns_name_equal(dns_resource_key_name(t
), name
);
308 r
= dns_answer_reserve(answer
, strv_length(item
->names
));
312 STRV_FOREACH(n
, item
->names
) {
313 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
315 rr
= dns_resource_record_new(found_ptr
);
319 rr
->ptr
.name
= strdup(*n
);
323 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
332 bn
= hashmap_get(m
->etc_hosts
.by_name
, name
);
334 r
= dns_answer_reserve(answer
, bn
->n_addresses
);
338 /* Check if name was listed with no address. If yes, continue to return an answer. */
339 if (!set_contains(m
->etc_hosts
.no_address
, name
))
343 DNS_QUESTION_FOREACH(t
, q
) {
344 if (!IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
346 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
349 r
= dns_name_equal(dns_resource_key_name(t
), name
);
355 if (IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_ANY
))
357 if (IN_SET(t
->type
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
360 if (found_a
&& found_aaaa
)
364 for (i
= 0; bn
&& i
< bn
->n_addresses
; i
++) {
365 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
367 if ((!found_a
&& bn
->addresses
[i
]->family
== AF_INET
) ||
368 (!found_aaaa
&& bn
->addresses
[i
]->family
== AF_INET6
))
371 r
= dns_resource_record_new_address(&rr
, bn
->addresses
[i
]->family
, &bn
->addresses
[i
]->address
, bn
->name
);
375 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
380 return found_a
|| found_aaaa
;