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 "socket-netlink.h"
13 #include "string-util.h"
15 #include "time-util.h"
17 /* Recheck /etc/hosts at most once every 2s */
18 #define ETC_HOSTS_RECHECK_USEC (2*USEC_PER_SEC)
20 static void etc_hosts_item_free(EtcHostsItem
*item
) {
21 strv_free(item
->names
);
25 static void etc_hosts_item_by_name_free(EtcHostsItemByName
*item
) {
27 free(item
->addresses
);
31 void etc_hosts_free(EtcHosts
*hosts
) {
32 hosts
->by_address
= hashmap_free_with_destructor(hosts
->by_address
, etc_hosts_item_free
);
33 hosts
->by_name
= hashmap_free_with_destructor(hosts
->by_name
, etc_hosts_item_by_name_free
);
34 hosts
->no_address
= set_free_free(hosts
->no_address
);
37 void manager_etc_hosts_flush(Manager
*m
) {
38 etc_hosts_free(&m
->etc_hosts
);
39 m
->etc_hosts_mtime
= USEC_INFINITY
;
44 static int parse_line(EtcHosts
*hosts
, unsigned nr
, const char *line
) {
45 _cleanup_free_
char *address_str
= NULL
;
46 struct in_addr_data address
= {};
54 r
= extract_first_word(&line
, &address_str
, NULL
, EXTRACT_RELAX
);
56 return log_error_errno(r
, "/etc/hosts:%u: failed to extract address: %m", nr
);
57 assert(r
> 0); /* We already checked that the line is not empty, so it should contain *something* */
59 r
= in_addr_ifindex_from_string_auto(address_str
, &address
.family
, &address
.address
, NULL
);
61 log_warning_errno(r
, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr
, address_str
);
65 r
= in_addr_is_null(address
.family
, &address
.address
);
67 log_warning_errno(r
, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr
, address_str
);
71 /* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to
75 /* If this is a normal address, then simply add entry mapping it to the specified names */
77 item
= hashmap_get(hosts
->by_address
, &address
);
79 r
= hashmap_ensure_allocated(&hosts
->by_address
, &in_addr_data_hash_ops
);
83 item
= new(EtcHostsItem
, 1);
87 *item
= (EtcHostsItem
) {
91 r
= hashmap_put(hosts
->by_address
, &item
->address
, item
);
100 _cleanup_free_
char *name
= NULL
;
101 EtcHostsItemByName
*bn
;
103 r
= extract_first_word(&line
, &name
, NULL
, EXTRACT_RELAX
);
105 return log_error_errno(r
, "/etc/hosts:%u: couldn't extract hostname: %m", nr
);
111 r
= dns_name_is_valid_ldh(name
);
113 log_warning_errno(r
, "/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr
, name
);
117 if (is_localhost(name
))
118 /* Suppress the "localhost" line that is often seen */
122 /* Optimize the case where we don't need to store any addresses, by storing
123 * only the name in a dedicated Set instead of the hashmap */
125 r
= set_ensure_consume(&hosts
->no_address
, &dns_name_hash_ops
, TAKE_PTR(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 hostnames", 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 or ino/dev change? If not, there's no point in re-reading the file. */
227 if (timespec_load(&st
.st_mtim
) == m
->etc_hosts_mtime
&&
228 st
.st_ino
== m
->etc_hosts_ino
&& st
.st_dev
== m
->etc_hosts_dev
)
232 f
= fopen("/etc/hosts", "re");
235 return log_error_errno(errno
, "Failed to open /etc/hosts: %m");
237 manager_etc_hosts_flush(m
);
241 /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
243 r
= fstat(fileno(f
), &st
);
245 return log_error_errno(errno
, "Failed to fstat() /etc/hosts: %m");
247 r
= etc_hosts_parse(&m
->etc_hosts
, f
);
251 m
->etc_hosts_mtime
= timespec_load(&st
.st_mtim
);
252 m
->etc_hosts_ino
= st
.st_ino
;
253 m
->etc_hosts_dev
= st
.st_dev
;
254 m
->etc_hosts_last
= ts
;
259 int manager_etc_hosts_lookup(Manager
*m
, DnsQuestion
* q
, DnsAnswer
**answer
) {
260 bool found_a
= false, found_aaaa
= false;
261 struct in_addr_data k
= {};
262 EtcHostsItemByName
*bn
;
272 if (!m
->read_etc_hosts
)
275 (void) manager_etc_hosts_read(m
);
277 name
= dns_question_first_name(q
);
281 r
= dns_name_address(name
, &k
.family
, &k
.address
);
284 DnsResourceKey
*found_ptr
= NULL
;
286 item
= hashmap_get(m
->etc_hosts
.by_address
, &k
);
290 /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
291 * we'll only return if the request was for PTR. */
293 DNS_QUESTION_FOREACH(t
, q
) {
294 if (!IN_SET(t
->type
, DNS_TYPE_PTR
, DNS_TYPE_ANY
))
296 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
299 r
= dns_name_equal(dns_resource_key_name(t
), name
);
311 r
= dns_answer_reserve(answer
, strv_length(item
->names
));
315 STRV_FOREACH(n
, item
->names
) {
316 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
318 rr
= dns_resource_record_new(found_ptr
);
322 rr
->ptr
.name
= strdup(*n
);
326 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
335 bn
= hashmap_get(m
->etc_hosts
.by_name
, name
);
337 r
= dns_answer_reserve(answer
, bn
->n_addresses
);
341 /* Check if name was listed with no address. If yes, continue to return an answer. */
342 if (!set_contains(m
->etc_hosts
.no_address
, name
))
346 DNS_QUESTION_FOREACH(t
, q
) {
347 if (!IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
349 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
352 r
= dns_name_equal(dns_resource_key_name(t
), name
);
358 if (IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_ANY
))
360 if (IN_SET(t
->type
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
363 if (found_a
&& found_aaaa
)
367 for (i
= 0; bn
&& i
< bn
->n_addresses
; i
++) {
368 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
370 if ((!found_a
&& bn
->addresses
[i
]->family
== AF_INET
) ||
371 (!found_aaaa
&& bn
->addresses
[i
]->family
== AF_INET6
))
374 r
= dns_resource_record_new_address(&rr
, bn
->addresses
[i
]->family
, &bn
->addresses
[i
]->address
, bn
->name
);
378 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
383 return found_a
|| found_aaaa
;