Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include <sys/stat.h>
4 : #include <sys/types.h>
5 : #include <unistd.h>
6 :
7 : #include "fd-util.h"
8 : #include "fileio.h"
9 : #include "hostname-util.h"
10 : #include "resolved-dns-synthesize.h"
11 : #include "resolved-etc-hosts.h"
12 : #include "string-util.h"
13 : #include "strv.h"
14 : #include "time-util.h"
15 :
16 : /* Recheck /etc/hosts at most once every 2s */
17 : #define ETC_HOSTS_RECHECK_USEC (2*USEC_PER_SEC)
18 :
19 24 : static void etc_hosts_item_free(EtcHostsItem *item) {
20 24 : strv_free(item->names);
21 24 : free(item);
22 24 : }
23 :
24 22 : static void etc_hosts_item_by_name_free(EtcHostsItemByName *item) {
25 22 : free(item->name);
26 22 : free(item->addresses);
27 22 : free(item);
28 22 : }
29 :
30 6 : void etc_hosts_free(EtcHosts *hosts) {
31 30 : hosts->by_address = hashmap_free_with_destructor(hosts->by_address, etc_hosts_item_free);
32 28 : hosts->by_name = hashmap_free_with_destructor(hosts->by_name, etc_hosts_item_by_name_free);
33 6 : hosts->no_address = set_free_free(hosts->no_address);
34 6 : }
35 :
36 0 : void manager_etc_hosts_flush(Manager *m) {
37 0 : etc_hosts_free(&m->etc_hosts);
38 0 : m->etc_hosts_mtime = USEC_INFINITY;
39 0 : }
40 :
41 29 : static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) {
42 29 : _cleanup_free_ char *address_str = NULL;
43 29 : struct in_addr_data address = {};
44 29 : bool found = false;
45 : EtcHostsItem *item;
46 : int r;
47 :
48 29 : assert(hosts);
49 29 : assert(line);
50 :
51 29 : r = extract_first_word(&line, &address_str, NULL, EXTRACT_RELAX);
52 29 : if (r < 0)
53 0 : return log_error_errno(r, "/etc/hosts:%u: failed to extract address: %m", nr);
54 29 : assert(r > 0); /* We already checked that the line is not empty, so it should contain *something* */
55 :
56 29 : r = in_addr_ifindex_from_string_auto(address_str, &address.family, &address.address, NULL);
57 29 : if (r < 0) {
58 3 : log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str);
59 3 : return 0;
60 : }
61 :
62 26 : r = in_addr_is_null(address.family, &address.address);
63 26 : if (r < 0) {
64 0 : log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str);
65 0 : return 0;
66 : }
67 26 : if (r > 0)
68 : /* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to
69 : * nothing. */
70 2 : item = NULL;
71 : else {
72 : /* If this is a normal address, then simply add entry mapping it to the specified names */
73 :
74 24 : item = hashmap_get(hosts->by_address, &address);
75 24 : if (!item) {
76 24 : r = hashmap_ensure_allocated(&hosts->by_address, &in_addr_data_hash_ops);
77 24 : if (r < 0)
78 0 : return log_oom();
79 :
80 24 : item = new0(EtcHostsItem, 1);
81 24 : if (!item)
82 0 : return log_oom();
83 :
84 24 : item->address = address;
85 :
86 24 : r = hashmap_put(hosts->by_address, &item->address, item);
87 24 : if (r < 0) {
88 0 : free(item);
89 0 : return log_oom();
90 : }
91 : }
92 : }
93 :
94 38 : for (;;) {
95 64 : _cleanup_free_ char *name = NULL;
96 : EtcHostsItemByName *bn;
97 :
98 64 : r = extract_first_word(&line, &name, NULL, EXTRACT_RELAX);
99 64 : if (r < 0)
100 0 : return log_error_errno(r, "/etc/hosts:%u: couldn't extract host name: %m", nr);
101 64 : if (r == 0)
102 26 : break;
103 :
104 38 : found = true;
105 :
106 38 : r = dns_name_is_valid_ldh(name);
107 38 : if (r <= 0) {
108 3 : log_warning_errno(r, "/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr, name);
109 3 : continue;
110 : }
111 :
112 35 : if (is_localhost(name))
113 : /* Suppress the "localhost" line that is often seen */
114 2 : continue;
115 :
116 33 : if (!item) {
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 */
119 :
120 3 : r = set_ensure_allocated(&hosts->no_address, &dns_name_hash_ops);
121 3 : if (r < 0)
122 0 : return log_oom();
123 :
124 3 : r = set_put(hosts->no_address, name);
125 3 : if (r < 0)
126 0 : return r;
127 :
128 3 : TAKE_PTR(name);
129 3 : continue;
130 : }
131 :
132 30 : r = strv_extend(&item->names, name);
133 30 : if (r < 0)
134 0 : return log_oom();
135 :
136 30 : bn = hashmap_get(hosts->by_name, name);
137 30 : if (!bn) {
138 22 : r = hashmap_ensure_allocated(&hosts->by_name, &dns_name_hash_ops);
139 22 : if (r < 0)
140 0 : return log_oom();
141 :
142 22 : bn = new0(EtcHostsItemByName, 1);
143 22 : if (!bn)
144 0 : return log_oom();
145 :
146 22 : r = hashmap_put(hosts->by_name, name, bn);
147 22 : if (r < 0) {
148 0 : free(bn);
149 0 : return log_oom();
150 : }
151 :
152 22 : bn->name = TAKE_PTR(name);
153 : }
154 :
155 30 : if (!GREEDY_REALLOC(bn->addresses, bn->n_allocated, bn->n_addresses + 1))
156 0 : return log_oom();
157 :
158 30 : bn->addresses[bn->n_addresses++] = &item->address;
159 : }
160 :
161 26 : if (!found)
162 1 : log_warning("/etc/hosts:%u: line is missing any host names", nr);
163 :
164 26 : return 0;
165 : }
166 :
167 2 : int etc_hosts_parse(EtcHosts *hosts, FILE *f) {
168 2 : _cleanup_(etc_hosts_free) EtcHosts t = {};
169 2 : unsigned nr = 0;
170 : int r;
171 :
172 35 : for (;;) {
173 37 : _cleanup_free_ char *line = NULL;
174 : char *l;
175 :
176 37 : r = read_line(f, LONG_LINE_MAX, &line);
177 37 : if (r < 0)
178 0 : return log_error_errno(r, "Failed to read /etc/hosts: %m");
179 37 : if (r == 0)
180 2 : break;
181 :
182 35 : nr++;
183 :
184 35 : l = strchr(line, '#');
185 35 : if (l)
186 7 : *l = '\0';
187 :
188 35 : l = strstrip(line);
189 35 : if (isempty(l))
190 6 : continue;
191 :
192 29 : r = parse_line(&t, nr, l);
193 29 : if (r < 0)
194 0 : return r;
195 : }
196 :
197 2 : etc_hosts_free(hosts);
198 2 : *hosts = t;
199 2 : t = (EtcHosts) {}; /* prevent cleanup */
200 2 : return 0;
201 : }
202 :
203 0 : static int manager_etc_hosts_read(Manager *m) {
204 0 : _cleanup_fclose_ FILE *f = NULL;
205 : struct stat st;
206 : usec_t ts;
207 : int r;
208 :
209 0 : assert_se(sd_event_now(m->event, clock_boottime_or_monotonic(), &ts) >= 0);
210 :
211 : /* See if we checked /etc/hosts recently already */
212 0 : if (m->etc_hosts_last != USEC_INFINITY && m->etc_hosts_last + ETC_HOSTS_RECHECK_USEC > ts)
213 0 : return 0;
214 :
215 0 : m->etc_hosts_last = ts;
216 :
217 0 : if (m->etc_hosts_mtime != USEC_INFINITY) {
218 0 : if (stat("/etc/hosts", &st) < 0) {
219 0 : if (errno != ENOENT)
220 0 : return log_error_errno(errno, "Failed to stat /etc/hosts: %m");
221 :
222 0 : manager_etc_hosts_flush(m);
223 0 : return 0;
224 : }
225 :
226 : /* Did the mtime change? If not, there's no point in re-reading the file. */
227 0 : if (timespec_load(&st.st_mtim) == m->etc_hosts_mtime)
228 0 : return 0;
229 : }
230 :
231 0 : f = fopen("/etc/hosts", "re");
232 0 : if (!f) {
233 0 : if (errno != ENOENT)
234 0 : return log_error_errno(errno, "Failed to open /etc/hosts: %m");
235 :
236 0 : manager_etc_hosts_flush(m);
237 0 : return 0;
238 : }
239 :
240 : /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
241 : * invocation */
242 0 : r = fstat(fileno(f), &st);
243 0 : if (r < 0)
244 0 : return log_error_errno(errno, "Failed to fstat() /etc/hosts: %m");
245 :
246 0 : r = etc_hosts_parse(&m->etc_hosts, f);
247 0 : if (r < 0)
248 0 : return r;
249 :
250 0 : m->etc_hosts_mtime = timespec_load(&st.st_mtim);
251 0 : m->etc_hosts_last = ts;
252 :
253 0 : return 1;
254 : }
255 :
256 0 : int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) {
257 0 : bool found_a = false, found_aaaa = false;
258 0 : struct in_addr_data k = {};
259 : EtcHostsItemByName *bn;
260 : DnsResourceKey *t;
261 : const char *name;
262 : unsigned i;
263 : int r;
264 :
265 0 : assert(m);
266 0 : assert(q);
267 0 : assert(answer);
268 :
269 0 : if (!m->read_etc_hosts)
270 0 : return 0;
271 :
272 0 : (void) manager_etc_hosts_read(m);
273 :
274 0 : name = dns_question_first_name(q);
275 0 : if (!name)
276 0 : return 0;
277 :
278 0 : r = dns_name_address(name, &k.family, &k.address);
279 0 : if (r > 0) {
280 : EtcHostsItem *item;
281 0 : DnsResourceKey *found_ptr = NULL;
282 :
283 0 : item = hashmap_get(m->etc_hosts.by_address, &k);
284 0 : if (!item)
285 0 : return 0;
286 :
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. */
289 :
290 0 : DNS_QUESTION_FOREACH(t, q) {
291 0 : if (!IN_SET(t->type, DNS_TYPE_PTR, DNS_TYPE_ANY))
292 0 : continue;
293 0 : if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY))
294 0 : continue;
295 :
296 0 : r = dns_name_equal(dns_resource_key_name(t), name);
297 0 : if (r < 0)
298 0 : return r;
299 0 : if (r > 0) {
300 0 : found_ptr = t;
301 0 : break;
302 : }
303 : }
304 :
305 0 : if (found_ptr) {
306 : char **n;
307 :
308 0 : r = dns_answer_reserve(answer, strv_length(item->names));
309 0 : if (r < 0)
310 0 : return r;
311 :
312 0 : STRV_FOREACH(n, item->names) {
313 0 : _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
314 :
315 0 : rr = dns_resource_record_new(found_ptr);
316 0 : if (!rr)
317 0 : return -ENOMEM;
318 :
319 0 : rr->ptr.name = strdup(*n);
320 0 : if (!rr->ptr.name)
321 0 : return -ENOMEM;
322 :
323 0 : r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
324 0 : if (r < 0)
325 0 : return r;
326 : }
327 : }
328 :
329 0 : return 1;
330 : }
331 :
332 0 : bn = hashmap_get(m->etc_hosts.by_name, name);
333 0 : if (bn) {
334 0 : r = dns_answer_reserve(answer, bn->n_addresses);
335 0 : if (r < 0)
336 0 : return r;
337 : } else {
338 : /* Check if name was listed with no address. If yes, continue to return an answer. */
339 0 : if (!set_contains(m->etc_hosts.no_address, name))
340 0 : return 0;
341 : }
342 :
343 0 : DNS_QUESTION_FOREACH(t, q) {
344 0 : if (!IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_ANY))
345 0 : continue;
346 0 : if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY))
347 0 : continue;
348 :
349 0 : r = dns_name_equal(dns_resource_key_name(t), name);
350 0 : if (r < 0)
351 0 : return r;
352 0 : if (r == 0)
353 0 : continue;
354 :
355 0 : if (IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_ANY))
356 0 : found_a = true;
357 0 : if (IN_SET(t->type, DNS_TYPE_AAAA, DNS_TYPE_ANY))
358 0 : found_aaaa = true;
359 :
360 0 : if (found_a && found_aaaa)
361 0 : break;
362 : }
363 :
364 0 : for (i = 0; bn && i < bn->n_addresses; i++) {
365 0 : _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
366 :
367 0 : if ((!found_a && bn->addresses[i]->family == AF_INET) ||
368 0 : (!found_aaaa && bn->addresses[i]->family == AF_INET6))
369 0 : continue;
370 :
371 0 : r = dns_resource_record_new_address(&rr, bn->addresses[i]->family, &bn->addresses[i]->address, bn->name);
372 0 : if (r < 0)
373 0 : return r;
374 :
375 0 : r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
376 0 : if (r < 0)
377 0 : return r;
378 : }
379 :
380 0 : return found_a || found_aaaa;
381 : }
|