1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "hostname-util.h"
6 #include "resolved-dns-synthesize.h"
7 #include "resolved-etc-hosts.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 static void etc_hosts_item_free(EtcHostsItem
*item
) {
16 strv_free(item
->names
);
20 static void etc_hosts_item_by_name_free(EtcHostsItemByName
*item
) {
22 free(item
->addresses
);
26 void etc_hosts_free(EtcHosts
*hosts
) {
27 hosts
->by_address
= hashmap_free_with_destructor(hosts
->by_address
, etc_hosts_item_free
);
28 hosts
->by_name
= hashmap_free_with_destructor(hosts
->by_name
, etc_hosts_item_by_name_free
);
29 hosts
->no_address
= set_free_free(hosts
->no_address
);
32 void manager_etc_hosts_flush(Manager
*m
) {
33 etc_hosts_free(&m
->etc_hosts
);
34 m
->etc_hosts_mtime
= USEC_INFINITY
;
37 static int parse_line(EtcHosts
*hosts
, unsigned nr
, const char *line
) {
38 _cleanup_free_
char *address_str
= NULL
;
39 struct in_addr_data address
= {};
47 r
= extract_first_word(&line
, &address_str
, NULL
, EXTRACT_RELAX
);
49 return log_error_errno(r
, "/etc/hosts:%u: failed to extract address: %m", nr
);
50 assert(r
> 0); /* We already checked that the line is not empty, so it should contain *something* */
52 r
= in_addr_ifindex_from_string_auto(address_str
, &address
.family
, &address
.address
, NULL
);
54 log_warning_errno(r
, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr
, address_str
);
58 r
= in_addr_is_null(address
.family
, &address
.address
);
60 log_warning_errno(r
, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr
, address_str
);
64 /* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to
68 /* If this is a normal address, then simply add entry mapping it to the specified names */
70 item
= hashmap_get(hosts
->by_address
, &address
);
72 r
= hashmap_ensure_allocated(&hosts
->by_address
, &in_addr_data_hash_ops
);
76 item
= new0(EtcHostsItem
, 1);
80 item
->address
= address
;
82 r
= hashmap_put(hosts
->by_address
, &item
->address
, item
);
91 _cleanup_free_
char *name
= NULL
;
92 EtcHostsItemByName
*bn
;
94 r
= extract_first_word(&line
, &name
, NULL
, EXTRACT_RELAX
);
96 return log_error_errno(r
, "/etc/hosts:%u: couldn't extract host name: %m", nr
);
102 r
= dns_name_is_valid_ldh(name
);
104 log_warning_errno(r
, "/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr
, name
);
108 if (is_localhost(name
))
109 /* Suppress the "localhost" line that is often seen */
113 /* Optimize the case where we don't need to store any addresses, by storing
114 * only the name in a dedicated Set instead of the hashmap */
116 r
= set_ensure_allocated(&hosts
->no_address
, &dns_name_hash_ops
);
120 r
= set_put(hosts
->no_address
, name
);
128 r
= strv_extend(&item
->names
, name
);
132 bn
= hashmap_get(hosts
->by_name
, name
);
134 r
= hashmap_ensure_allocated(&hosts
->by_name
, &dns_name_hash_ops
);
138 bn
= new0(EtcHostsItemByName
, 1);
142 r
= hashmap_put(hosts
->by_name
, name
, bn
);
148 bn
->name
= TAKE_PTR(name
);
151 if (!GREEDY_REALLOC(bn
->addresses
, bn
->n_allocated
, bn
->n_addresses
+ 1))
154 bn
->addresses
[bn
->n_addresses
++] = &item
->address
;
158 log_warning("/etc/hosts:%u: line is missing any host names", nr
);
163 int etc_hosts_parse(EtcHosts
*hosts
, FILE *f
) {
164 _cleanup_(etc_hosts_free
) EtcHosts t
= {};
169 _cleanup_free_
char *line
= NULL
;
172 r
= read_line(f
, LONG_LINE_MAX
, &line
);
174 return log_error_errno(r
, "Failed to read /etc/hosts: %m");
180 l
= strchr(line
, '#');
188 r
= parse_line(&t
, nr
, l
);
193 etc_hosts_free(hosts
);
195 t
= (EtcHosts
) {}; /* prevent cleanup */
199 static int manager_etc_hosts_read(Manager
*m
) {
200 _cleanup_fclose_
FILE *f
= NULL
;
205 assert_se(sd_event_now(m
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
207 /* See if we checked /etc/hosts recently already */
208 if (m
->etc_hosts_last
!= USEC_INFINITY
&& m
->etc_hosts_last
+ ETC_HOSTS_RECHECK_USEC
> ts
)
211 m
->etc_hosts_last
= ts
;
213 if (m
->etc_hosts_mtime
!= USEC_INFINITY
) {
214 if (stat("/etc/hosts", &st
) < 0) {
216 return log_error_errno(errno
, "Failed to stat /etc/hosts: %m");
218 manager_etc_hosts_flush(m
);
222 /* Did the mtime change? If not, there's no point in re-reading the file. */
223 if (timespec_load(&st
.st_mtim
) == m
->etc_hosts_mtime
)
227 f
= fopen("/etc/hosts", "re");
230 return log_error_errno(errno
, "Failed to open /etc/hosts: %m");
232 manager_etc_hosts_flush(m
);
236 /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
238 r
= fstat(fileno(f
), &st
);
240 return log_error_errno(errno
, "Failed to fstat() /etc/hosts: %m");
242 r
= etc_hosts_parse(&m
->etc_hosts
, f
);
246 m
->etc_hosts_mtime
= timespec_load(&st
.st_mtim
);
247 m
->etc_hosts_last
= ts
;
252 int manager_etc_hosts_lookup(Manager
*m
, DnsQuestion
* q
, DnsAnswer
**answer
) {
253 bool found_a
= false, found_aaaa
= false;
254 struct in_addr_data k
= {};
255 EtcHostsItemByName
*bn
;
265 if (!m
->read_etc_hosts
)
268 (void) manager_etc_hosts_read(m
);
270 name
= dns_question_first_name(q
);
274 r
= dns_name_address(name
, &k
.family
, &k
.address
);
277 DnsResourceKey
*found_ptr
= NULL
;
279 item
= hashmap_get(m
->etc_hosts
.by_address
, &k
);
283 /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
284 * we'll only return if the request was for PTR. */
286 DNS_QUESTION_FOREACH(t
, q
) {
287 if (!IN_SET(t
->type
, DNS_TYPE_PTR
, DNS_TYPE_ANY
))
289 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
292 r
= dns_name_equal(dns_resource_key_name(t
), name
);
304 r
= dns_answer_reserve(answer
, strv_length(item
->names
));
308 STRV_FOREACH(n
, item
->names
) {
309 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
311 rr
= dns_resource_record_new(found_ptr
);
315 rr
->ptr
.name
= strdup(*n
);
319 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
328 bn
= hashmap_get(m
->etc_hosts
.by_name
, name
);
330 r
= dns_answer_reserve(answer
, bn
->n_addresses
);
334 /* Check if name was listed with no address. If yes, continue to return an answer. */
335 if (!set_contains(m
->etc_hosts
.no_address
, name
))
339 DNS_QUESTION_FOREACH(t
, q
) {
340 if (!IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
342 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
345 r
= dns_name_equal(dns_resource_key_name(t
), name
);
351 if (IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_ANY
))
353 if (IN_SET(t
->type
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
356 if (found_a
&& found_aaaa
)
360 for (i
= 0; bn
&& i
< bn
->n_addresses
; i
++) {
361 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
363 if ((!found_a
&& bn
->addresses
[i
]->family
== AF_INET
) ||
364 (!found_aaaa
&& bn
->addresses
[i
]->family
== AF_INET6
))
367 r
= dns_resource_record_new_address(&rr
, bn
->addresses
[i
]->family
, &bn
->addresses
[i
]->address
, bn
->name
);
371 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
376 return found_a
|| found_aaaa
;