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
= new0(EtcHostsItem
, 1);
87 item
->address
= address
;
89 r
= hashmap_put(hosts
->by_address
, &item
->address
, item
);
98 _cleanup_free_
char *name
= NULL
;
99 EtcHostsItemByName
*bn
;
101 r
= extract_first_word(&line
, &name
, NULL
, EXTRACT_RELAX
);
103 return log_error_errno(r
, "/etc/hosts:%u: couldn't extract hostname: %m", nr
);
109 r
= dns_name_is_valid_ldh(name
);
111 log_warning_errno(r
, "/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr
, name
);
115 if (is_localhost(name
))
116 /* Suppress the "localhost" line that is often seen */
120 /* Optimize the case where we don't need to store any addresses, by storing
121 * only the name in a dedicated Set instead of the hashmap */
123 r
= set_ensure_consume(&hosts
->no_address
, &dns_name_hash_ops
, TAKE_PTR(name
));
130 r
= strv_extend(&item
->names
, name
);
134 bn
= hashmap_get(hosts
->by_name
, name
);
136 r
= hashmap_ensure_allocated(&hosts
->by_name
, &dns_name_hash_ops
);
140 bn
= new0(EtcHostsItemByName
, 1);
144 r
= hashmap_put(hosts
->by_name
, name
, bn
);
150 bn
->name
= TAKE_PTR(name
);
153 if (!GREEDY_REALLOC(bn
->addresses
, bn
->n_allocated
, bn
->n_addresses
+ 1))
156 bn
->addresses
[bn
->n_addresses
++] = &item
->address
;
160 log_warning("/etc/hosts:%u: line is missing any hostnames", nr
);
165 int etc_hosts_parse(EtcHosts
*hosts
, FILE *f
) {
166 _cleanup_(etc_hosts_free
) EtcHosts t
= {};
171 _cleanup_free_
char *line
= NULL
;
174 r
= read_line(f
, LONG_LINE_MAX
, &line
);
176 return log_error_errno(r
, "Failed to read /etc/hosts: %m");
182 l
= strchr(line
, '#');
190 r
= parse_line(&t
, nr
, l
);
195 etc_hosts_free(hosts
);
197 t
= (EtcHosts
) {}; /* prevent cleanup */
201 static int manager_etc_hosts_read(Manager
*m
) {
202 _cleanup_fclose_
FILE *f
= NULL
;
207 assert_se(sd_event_now(m
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
209 /* See if we checked /etc/hosts recently already */
210 if (m
->etc_hosts_last
!= USEC_INFINITY
&& m
->etc_hosts_last
+ ETC_HOSTS_RECHECK_USEC
> ts
)
213 m
->etc_hosts_last
= ts
;
215 if (m
->etc_hosts_mtime
!= USEC_INFINITY
) {
216 if (stat("/etc/hosts", &st
) < 0) {
218 return log_error_errno(errno
, "Failed to stat /etc/hosts: %m");
220 manager_etc_hosts_flush(m
);
224 /* Did the mtime or ino/dev change? If not, there's no point in re-reading the file. */
225 if (timespec_load(&st
.st_mtim
) == m
->etc_hosts_mtime
&&
226 st
.st_ino
== m
->etc_hosts_ino
&& st
.st_dev
== m
->etc_hosts_dev
)
230 f
= fopen("/etc/hosts", "re");
233 return log_error_errno(errno
, "Failed to open /etc/hosts: %m");
235 manager_etc_hosts_flush(m
);
239 /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
241 r
= fstat(fileno(f
), &st
);
243 return log_error_errno(errno
, "Failed to fstat() /etc/hosts: %m");
245 r
= etc_hosts_parse(&m
->etc_hosts
, f
);
249 m
->etc_hosts_mtime
= timespec_load(&st
.st_mtim
);
250 m
->etc_hosts_ino
= st
.st_ino
;
251 m
->etc_hosts_dev
= st
.st_dev
;
252 m
->etc_hosts_last
= ts
;
257 int manager_etc_hosts_lookup(Manager
*m
, DnsQuestion
* q
, DnsAnswer
**answer
) {
258 bool found_a
= false, found_aaaa
= false;
259 struct in_addr_data k
= {};
260 EtcHostsItemByName
*bn
;
270 if (!m
->read_etc_hosts
)
273 (void) manager_etc_hosts_read(m
);
275 name
= dns_question_first_name(q
);
279 r
= dns_name_address(name
, &k
.family
, &k
.address
);
282 DnsResourceKey
*found_ptr
= NULL
;
284 item
= hashmap_get(m
->etc_hosts
.by_address
, &k
);
288 /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
289 * we'll only return if the request was for PTR. */
291 DNS_QUESTION_FOREACH(t
, q
) {
292 if (!IN_SET(t
->type
, DNS_TYPE_PTR
, DNS_TYPE_ANY
))
294 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
297 r
= dns_name_equal(dns_resource_key_name(t
), name
);
309 r
= dns_answer_reserve(answer
, strv_length(item
->names
));
313 STRV_FOREACH(n
, item
->names
) {
314 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
316 rr
= dns_resource_record_new(found_ptr
);
320 rr
->ptr
.name
= strdup(*n
);
324 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
333 bn
= hashmap_get(m
->etc_hosts
.by_name
, name
);
335 r
= dns_answer_reserve(answer
, bn
->n_addresses
);
339 /* Check if name was listed with no address. If yes, continue to return an answer. */
340 if (!set_contains(m
->etc_hosts
.no_address
, name
))
344 DNS_QUESTION_FOREACH(t
, q
) {
345 if (!IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
347 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
350 r
= dns_name_equal(dns_resource_key_name(t
), name
);
356 if (IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_ANY
))
358 if (IN_SET(t
->type
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
361 if (found_a
&& found_aaaa
)
365 for (i
= 0; bn
&& i
< bn
->n_addresses
; i
++) {
366 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
368 if ((!found_a
&& bn
->addresses
[i
]->family
== AF_INET
) ||
369 (!found_aaaa
&& bn
->addresses
[i
]->family
== AF_INET6
))
372 r
= dns_resource_record_new_address(&rr
, bn
->addresses
[i
]->family
, &bn
->addresses
[i
]->address
, bn
->name
);
376 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
381 return found_a
|| found_aaaa
;