1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
10 #include "hostname-util.h"
11 #include "resolved-etc-hosts.h"
12 #include "resolved-dns-synthesize.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 typedef struct EtcHostsItem
{
22 union in_addr_union address
;
27 typedef struct EtcHostsItemByName
{
31 size_t n_items
, n_allocated
;
34 void manager_etc_hosts_flush(Manager
*m
) {
36 EtcHostsItemByName
*bn
;
38 while ((item
= set_steal_first(m
->etc_hosts_by_address
))) {
39 strv_free(item
->names
);
43 while ((bn
= hashmap_steal_first(m
->etc_hosts_by_name
))) {
49 m
->etc_hosts_by_address
= set_free(m
->etc_hosts_by_address
);
50 m
->etc_hosts_by_name
= hashmap_free(m
->etc_hosts_by_name
);
52 m
->etc_hosts_mtime
= USEC_INFINITY
;
55 static void etc_hosts_item_hash_func(const void *p
, struct siphash
*state
) {
56 const EtcHostsItem
*item
= p
;
58 siphash24_compress(&item
->family
, sizeof(item
->family
), state
);
60 if (item
->family
== AF_INET
)
61 siphash24_compress(&item
->address
.in
, sizeof(item
->address
.in
), state
);
62 else if (item
->family
== AF_INET6
)
63 siphash24_compress(&item
->address
.in6
, sizeof(item
->address
.in6
), state
);
66 static int etc_hosts_item_compare_func(const void *a
, const void *b
) {
67 const EtcHostsItem
*x
= a
, *y
= b
;
69 if (x
->family
!= y
->family
)
70 return x
->family
- y
->family
;
72 if (x
->family
== AF_INET
)
73 return memcmp(&x
->address
.in
.s_addr
, &y
->address
.in
.s_addr
, sizeof(struct in_addr
));
75 if (x
->family
== AF_INET6
)
76 return memcmp(&x
->address
.in6
.s6_addr
, &y
->address
.in6
.s6_addr
, sizeof(struct in6_addr
));
78 return trivial_compare_func(a
, b
);
81 static const struct hash_ops etc_hosts_item_ops
= {
82 .hash
= etc_hosts_item_hash_func
,
83 .compare
= etc_hosts_item_compare_func
,
86 static int add_item(Manager
*m
, int family
, const union in_addr_union
*address
, char **names
) {
99 r
= in_addr_is_null(family
, address
);
103 /* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to
107 /* If this is a normal address, then, simply add entry mapping it to the specified names */
109 item
= set_get(m
->etc_hosts_by_address
, &key
);
111 r
= strv_extend_strv(&item
->names
, names
, true);
116 r
= set_ensure_allocated(&m
->etc_hosts_by_address
, &etc_hosts_item_ops
);
120 item
= new0(EtcHostsItem
, 1);
124 item
->family
= family
;
125 item
->address
= *address
;
128 r
= set_put(m
->etc_hosts_by_address
, item
);
136 STRV_FOREACH(n
, names
) {
137 EtcHostsItemByName
*bn
;
139 bn
= hashmap_get(m
->etc_hosts_by_name
, *n
);
141 r
= hashmap_ensure_allocated(&m
->etc_hosts_by_name
, &dns_name_hash_ops
);
145 bn
= new0(EtcHostsItemByName
, 1);
149 bn
->name
= strdup(*n
);
155 r
= hashmap_put(m
->etc_hosts_by_name
, bn
->name
, bn
);
164 if (!GREEDY_REALLOC(bn
->items
, bn
->n_allocated
, bn
->n_items
+1))
167 bn
->items
[bn
->n_items
++] = item
;
174 static int parse_line(Manager
*m
, unsigned nr
, const char *line
) {
175 _cleanup_free_
char *address
= NULL
;
176 _cleanup_strv_free_
char **names
= NULL
;
177 union in_addr_union in
;
178 bool suppressed
= false;
184 r
= extract_first_word(&line
, &address
, NULL
, EXTRACT_RELAX
);
186 return log_error_errno(r
, "Couldn't extract address, in line /etc/hosts:%u.", nr
);
188 log_error("Premature end of line, in line /etc/hosts:%u.", nr
);
192 r
= in_addr_from_string_auto(address
, &family
, &in
);
194 return log_error_errno(r
, "Address '%s' is invalid, in line /etc/hosts:%u.", address
, nr
);
197 _cleanup_free_
char *name
= NULL
;
199 r
= extract_first_word(&line
, &name
, NULL
, EXTRACT_RELAX
);
201 return log_error_errno(r
, "Couldn't extract host name, in line /etc/hosts:%u.", nr
);
205 r
= dns_name_is_valid(name
);
207 return log_error_errno(r
, "Hostname %s is not valid, ignoring, in line /etc/hosts:%u.", name
, nr
);
209 if (is_localhost(name
)) {
210 /* Suppress the "localhost" line that is often seen */
215 r
= strv_push(&names
, name
);
222 if (strv_isempty(names
)) {
227 log_error("Line is missing any host names, in line /etc/hosts:%u.", nr
);
231 /* Takes possession of the names strv */
232 r
= add_item(m
, family
, &in
, names
);
240 int manager_etc_hosts_read(Manager
*m
) {
241 _cleanup_fclose_
FILE *f
= NULL
;
248 assert_se(sd_event_now(m
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
250 /* See if we checked /etc/hosts recently already */
251 if (m
->etc_hosts_last
!= USEC_INFINITY
&& m
->etc_hosts_last
+ ETC_HOSTS_RECHECK_USEC
> ts
)
254 m
->etc_hosts_last
= ts
;
256 if (m
->etc_hosts_mtime
!= USEC_INFINITY
) {
257 if (stat("/etc/hosts", &st
) < 0) {
258 if (errno
== ENOENT
) {
263 return log_error_errno(errno
, "Failed to stat /etc/hosts: %m");
266 /* Did the mtime change? If not, there's no point in re-reading the file. */
267 if (timespec_load(&st
.st_mtim
) == m
->etc_hosts_mtime
)
271 f
= fopen("/etc/hosts", "re");
273 if (errno
== ENOENT
) {
278 return log_error_errno(errno
, "Failed to open /etc/hosts: %m");
281 /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
283 r
= fstat(fileno(f
), &st
);
285 return log_error_errno(errno
, "Failed to fstat() /etc/hosts: %m");
287 manager_etc_hosts_flush(m
);
289 FOREACH_LINE(line
, f
, return log_error_errno(errno
, "Failed to read /etc/hosts: %m")) {
300 r
= parse_line(m
, nr
, l
);
301 if (r
== -ENOMEM
) /* On OOM we abandon the half-built-up structure. All other errors we ignore and proceed */
305 m
->etc_hosts_mtime
= timespec_load(&st
.st_mtim
);
306 m
->etc_hosts_last
= ts
;
311 manager_etc_hosts_flush(m
);
315 int manager_etc_hosts_lookup(Manager
*m
, DnsQuestion
* q
, DnsAnswer
**answer
) {
316 bool found_a
= false, found_aaaa
= false;
317 EtcHostsItemByName
*bn
;
328 r
= manager_etc_hosts_read(m
);
332 name
= dns_question_first_name(q
);
336 r
= dns_name_address(name
, &k
.family
, &k
.address
);
339 DnsResourceKey
*found_ptr
= NULL
;
341 item
= set_get(m
->etc_hosts_by_address
, &k
);
345 /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
346 * we'll only return if the request was for PTR. */
348 DNS_QUESTION_FOREACH(t
, q
) {
349 if (!IN_SET(t
->type
, DNS_TYPE_PTR
, DNS_TYPE_ANY
))
351 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
354 r
= dns_name_equal(dns_resource_key_name(t
), name
);
366 r
= dns_answer_reserve(answer
, strv_length(item
->names
));
370 STRV_FOREACH(n
, item
->names
) {
371 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
373 rr
= dns_resource_record_new(found_ptr
);
377 rr
->ptr
.name
= strdup(*n
);
381 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
390 bn
= hashmap_get(m
->etc_hosts_by_name
, name
);
394 r
= dns_answer_reserve(answer
, bn
->n_items
);
398 DNS_QUESTION_FOREACH(t
, q
) {
399 if (!IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
401 if (!IN_SET(t
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
404 r
= dns_name_equal(dns_resource_key_name(t
), name
);
410 if (IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_ANY
))
412 if (IN_SET(t
->type
, DNS_TYPE_AAAA
, DNS_TYPE_ANY
))
415 if (found_a
&& found_aaaa
)
419 for (i
= 0; i
< bn
->n_items
; i
++) {
420 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
422 if ((!found_a
&& bn
->items
[i
]->family
== AF_INET
) ||
423 (!found_aaaa
&& bn
->items
[i
]->family
== AF_INET6
))
426 r
= dns_resource_record_new_address(&rr
, bn
->items
[i
]->family
, &bn
->items
[i
]->address
, bn
->name
);
430 r
= dns_answer_add(*answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
435 return found_a
|| found_aaaa
;