]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-etc-hosts.c
resolve: adjust warning
[thirdparty/systemd.git] / src / resolve / resolved-etc-hosts.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
dd0bc0f1 2
ca78ad1d
ZJS
3#include <sys/stat.h>
4#include <sys/types.h>
5#include <unistd.h>
6
dd0bc0f1
LP
7#include "fd-util.h"
8#include "fileio.h"
9#include "hostname-util.h"
dd0bc0f1 10#include "resolved-dns-synthesize.h"
7c777a77 11#include "resolved-etc-hosts.h"
5c3fa98d 12#include "socket-netlink.h"
36d892b7 13#include "stat-util.h"
dd0bc0f1
LP
14#include "string-util.h"
15#include "strv.h"
16#include "time-util.h"
17
18/* Recheck /etc/hosts at most once every 2s */
19#define ETC_HOSTS_RECHECK_USEC (2*USEC_PER_SEC)
20
bb5c7730
YW
21static EtcHostsItemByAddress *etc_hosts_item_by_address_free(EtcHostsItemByAddress *item) {
22 if (!item)
23 return NULL;
24
37b7cc8d 25 strv_free(item->names);
bb5c7730 26 return mfree(item);
37b7cc8d 27}
dd0bc0f1 28
e4e34099
YW
29DEFINE_TRIVIAL_CLEANUP_FUNC(EtcHostsItemByAddress*, etc_hosts_item_by_address_free);
30
bb5c7730
YW
31static EtcHostsItemByName *etc_hosts_item_by_name_free(EtcHostsItemByName *item) {
32 if (!item)
33 return NULL;
34
37b7cc8d
ZJS
35 free(item->name);
36 free(item->addresses);
bb5c7730 37 return mfree(item);
37b7cc8d 38}
dd0bc0f1 39
e4e34099
YW
40DEFINE_TRIVIAL_CLEANUP_FUNC(EtcHostsItemByName*, etc_hosts_item_by_name_free);
41
0f7bdf35
YW
42void etc_hosts_clear(EtcHosts *hosts) {
43 assert(hosts);
44
133eedad 45 hosts->by_address = hashmap_free_with_destructor(hosts->by_address, etc_hosts_item_by_address_free);
37b7cc8d 46 hosts->by_name = hashmap_free_with_destructor(hosts->by_name, etc_hosts_item_by_name_free);
fd373593 47 hosts->no_address = set_free_free(hosts->no_address);
37b7cc8d 48}
dd0bc0f1 49
37b7cc8d 50void manager_etc_hosts_flush(Manager *m) {
0f7bdf35 51 etc_hosts_clear(&m->etc_hosts);
36d892b7 52 m->etc_hosts_stat = (struct stat) {};
dd0bc0f1
LP
53}
54
37b7cc8d 55static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) {
0351eb11
YW
56 _cleanup_free_ char *address_str = NULL;
57 struct in_addr_data address = {};
fd373593 58 bool found = false;
133eedad 59 EtcHostsItemByAddress *item;
dd0bc0f1
LP
60 int r;
61
37b7cc8d 62 assert(hosts);
0351eb11 63 assert(line);
dd0bc0f1 64
0351eb11
YW
65 r = extract_first_word(&line, &address_str, NULL, EXTRACT_RELAX);
66 if (r < 0)
bd005277
ZJS
67 return log_error_errno(r, "/etc/hosts:%u: failed to extract address: %m", nr);
68 assert(r > 0); /* We already checked that the line is not empty, so it should contain *something* */
0351eb11 69
7bf8c3de 70 r = in_addr_ifindex_from_string_auto(address_str, &address.family, &address.address, NULL);
bd005277
ZJS
71 if (r < 0) {
72 log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str);
73 return 0;
74 }
0351eb11 75
94876904 76 r = in_addr_data_is_null(&address);
bd005277
ZJS
77 if (r < 0) {
78 log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str);
79 return 0;
80 }
dd0bc0f1
LP
81 if (r > 0)
82 /* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to
83 * nothing. */
84 item = NULL;
85 else {
78fc21a1 86 /* If this is a normal address, then simply add entry mapping it to the specified names */
dd0bc0f1 87
37b7cc8d 88 item = hashmap_get(hosts->by_address, &address);
0351eb11 89 if (!item) {
e4e34099 90 _cleanup_(etc_hosts_item_by_address_freep) EtcHostsItemByAddress *new_item = NULL;
dd0bc0f1 91
e4e34099
YW
92 new_item = new(EtcHostsItemByAddress, 1);
93 if (!new_item)
dd0bc0f1
LP
94 return log_oom();
95
e4e34099 96 *new_item = (EtcHostsItemByAddress) {
1ed31408
LP
97 .address = address,
98 };
dd0bc0f1 99
e4e34099
YW
100 r = hashmap_ensure_put(&hosts->by_address, &in_addr_data_hash_ops, &new_item->address, new_item);
101 if (r < 0)
dd0bc0f1 102 return log_oom();
e4e34099
YW
103
104 item = TAKE_PTR(new_item);
dd0bc0f1
LP
105 }
106 }
107
0351eb11
YW
108 for (;;) {
109 _cleanup_free_ char *name = NULL;
dd0bc0f1
LP
110 EtcHostsItemByName *bn;
111
0351eb11
YW
112 r = extract_first_word(&line, &name, NULL, EXTRACT_RELAX);
113 if (r < 0)
38b38500 114 return log_error_errno(r, "/etc/hosts:%u: couldn't extract hostname: %m", nr);
0351eb11
YW
115 if (r == 0)
116 break;
117
7470cc4c 118 r = dns_name_is_valid_ldh(name);
bd005277 119 if (r <= 0) {
98b1eb71
YW
120 if (r < 0)
121 log_warning_errno(r, "/etc/hosts:%u: Failed to check the validity of hostname \"%s\", ignoring: %m", nr, name);
122 else
123 log_warning("/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr, name);
bd005277
ZJS
124 continue;
125 }
126
10b0c818
YW
127 found = true;
128
fd373593
ZJS
129 if (!item) {
130 /* Optimize the case where we don't need to store any addresses, by storing
131 * only the name in a dedicated Set instead of the hashmap */
132
ab9dd0b9 133 r = set_ensure_consume(&hosts->no_address, &dns_name_hash_ops, TAKE_PTR(name));
fd373593 134 if (r < 0)
10b0c818 135 return log_oom();
fd373593 136
fd373593 137 continue;
0351eb11
YW
138 }
139
eb164c51 140 r = strv_extend_with_size(&item->names, &item->n_names, name);
fd373593
ZJS
141 if (r < 0)
142 return log_oom();
143
37b7cc8d 144 bn = hashmap_get(hosts->by_name, name);
dd0bc0f1 145 if (!bn) {
e4e34099 146 _cleanup_(etc_hosts_item_by_name_freep) EtcHostsItemByName *new_item = NULL;
dd0bc0f1 147
e4e34099
YW
148 new_item = new(EtcHostsItemByName, 1);
149 if (!new_item)
dd0bc0f1
LP
150 return log_oom();
151
e4e34099
YW
152 *new_item = (EtcHostsItemByName) {
153 .name = TAKE_PTR(name),
154 };
155
156 r = hashmap_ensure_put(&hosts->by_name, &dns_name_hash_ops, new_item->name, new_item);
157 if (r < 0)
dd0bc0f1 158 return log_oom();
0351eb11 159
e4e34099 160 bn = TAKE_PTR(new_item);
dd0bc0f1
LP
161 }
162
319a4f4b 163 if (!GREEDY_REALLOC(bn->addresses, bn->n_addresses + 1))
fd373593 164 return log_oom();
dd0bc0f1 165
fd373593 166 bn->addresses[bn->n_addresses++] = &item->address;
dd0bc0f1
LP
167 }
168
baaa35ad 169 if (!found)
10b0c818 170 log_warning("/etc/hosts:%u: line is missing any valid hostnames", nr);
dd0bc0f1 171
0351eb11 172 return 0;
dd0bc0f1
LP
173}
174
9ca875e8
LP
175static void strip_localhost(EtcHosts *hosts) {
176 static const struct in_addr_data local_in_addrs[] = {
177 {
178 .family = AF_INET,
179#if __BYTE_ORDER == __LITTLE_ENDIAN
180 /* We want constant expressions here, that's why we don't use htole32() here */
181 .address.in.s_addr = UINT32_C(0x0100007F),
182#else
183 .address.in.s_addr = UINT32_C(0x7F000001),
184#endif
185 },
186 {
187 .family = AF_INET6,
188 .address.in6 = IN6ADDR_LOOPBACK_INIT,
189 },
190 };
191
9ca875e8
LP
192 assert(hosts);
193
194 /* Removes the 'localhost' entry from what we loaded. But only if the mapping is exclusively between
195 * 127.0.0.1 and localhost (or aliases to that we recognize). If there's any other name assigned to
196 * it, we leave the entry in.
197 *
198 * This way our regular synthesizing can take over, but only if it would result in the exact same
199 * mappings. */
200
201 for (size_t j = 0; j < ELEMENTSOF(local_in_addrs); j++) {
9f1a1f20
YW
202 bool all_localhost, all_local_address;
203 EtcHostsItemByAddress *item;
9ca875e8
LP
204
205 item = hashmap_get(hosts->by_address, local_in_addrs + j);
206 if (!item)
207 continue;
208
209 /* Check whether all hostnames the loopback address points to are localhost ones */
210 all_localhost = true;
211 STRV_FOREACH(i, item->names)
212 if (!is_localhost(*i)) {
213 all_localhost = false;
214 break;
215 }
216
217 if (!all_localhost) /* Not all names are localhost, hence keep the entries for this address. */
218 continue;
219
220 /* Now check if the names listed for this address actually all point back just to this
221 * address (or the other loopback address). If not, let's stay away from this too. */
9f1a1f20 222 all_local_address = true;
9ca875e8
LP
223 STRV_FOREACH(i, item->names) {
224 EtcHostsItemByName *n;
9ca875e8
LP
225
226 n = hashmap_get(hosts->by_name, *i);
227 if (!n) /* No reverse entry? Then almost certainly the entry already got deleted from
228 * the previous iteration of this loop, i.e. via the other protocol */
229 break;
230
231 /* Now check if the addresses of this item are all localhost addresses */
9ca875e8
LP
232 for (size_t m = 0; m < n->n_addresses; m++)
233 if (!in_addr_is_localhost(n->addresses[m]->family, &n->addresses[m]->address)) {
234 all_local_address = false;
235 break;
236 }
237
9f1a1f20 238 if (!all_local_address)
9ca875e8 239 break;
9ca875e8
LP
240 }
241
9f1a1f20 242 if (!all_local_address)
9ca875e8
LP
243 continue;
244
bb5c7730
YW
245 STRV_FOREACH(i, item->names)
246 etc_hosts_item_by_name_free(hashmap_remove(hosts->by_name, *i));
9ca875e8
LP
247
248 assert_se(hashmap_remove(hosts->by_address, local_in_addrs + j) == item);
133eedad 249 etc_hosts_item_by_address_free(item);
9ca875e8
LP
250 }
251}
252
78fc21a1 253int etc_hosts_parse(EtcHosts *hosts, FILE *f) {
0f7bdf35 254 _cleanup_(etc_hosts_clear) EtcHosts t = {};
37b7cc8d
ZJS
255 unsigned nr = 0;
256 int r;
257
7c777a77
LP
258 for (;;) {
259 _cleanup_free_ char *line = NULL;
37b7cc8d
ZJS
260 char *l;
261
7c777a77
LP
262 r = read_line(f, LONG_LINE_MAX, &line);
263 if (r < 0)
264 return log_error_errno(r, "Failed to read /etc/hosts: %m");
265 if (r == 0)
266 break;
267
37b7cc8d
ZJS
268 nr++;
269
bd005277
ZJS
270 l = strchr(line, '#');
271 if (l)
272 *l = '\0';
273
37b7cc8d
ZJS
274 l = strstrip(line);
275 if (isempty(l))
276 continue;
37b7cc8d
ZJS
277
278 r = parse_line(&t, nr, l);
279 if (r < 0)
280 return r;
281 }
282
9ca875e8
LP
283 strip_localhost(&t);
284
0f7bdf35 285 etc_hosts_clear(hosts);
37b7cc8d
ZJS
286 *hosts = t;
287 t = (EtcHosts) {}; /* prevent cleanup */
288 return 0;
289}
290
452ca091 291static int manager_etc_hosts_read(Manager *m) {
dd0bc0f1 292 _cleanup_fclose_ FILE *f = NULL;
dd0bc0f1
LP
293 struct stat st;
294 usec_t ts;
dd0bc0f1
LP
295 int r;
296
ba4e0427 297 assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &ts) >= 0);
dd0bc0f1
LP
298
299 /* See if we checked /etc/hosts recently already */
300 if (m->etc_hosts_last != USEC_INFINITY && m->etc_hosts_last + ETC_HOSTS_RECHECK_USEC > ts)
301 return 0;
302
303 m->etc_hosts_last = ts;
304
36d892b7 305 if (m->etc_hosts_stat.st_mode != 0) {
dd0bc0f1 306 if (stat("/etc/hosts", &st) < 0) {
37b7cc8d
ZJS
307 if (errno != ENOENT)
308 return log_error_errno(errno, "Failed to stat /etc/hosts: %m");
dd0bc0f1 309
37b7cc8d
ZJS
310 manager_etc_hosts_flush(m);
311 return 0;
dd0bc0f1
LP
312 }
313
aa5408e2 314 /* Did the mtime or ino/dev change? If not, there's no point in re-reading the file. */
36d892b7 315 if (stat_inode_unmodified(&m->etc_hosts_stat, &st))
dd0bc0f1
LP
316 return 0;
317 }
318
319 f = fopen("/etc/hosts", "re");
320 if (!f) {
37b7cc8d
ZJS
321 if (errno != ENOENT)
322 return log_error_errno(errno, "Failed to open /etc/hosts: %m");
dd0bc0f1 323
37b7cc8d
ZJS
324 manager_etc_hosts_flush(m);
325 return 0;
dd0bc0f1
LP
326 }
327
328 /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
329 * invocation */
330 r = fstat(fileno(f), &st);
331 if (r < 0)
332 return log_error_errno(errno, "Failed to fstat() /etc/hosts: %m");
333
37b7cc8d 334 r = etc_hosts_parse(&m->etc_hosts, f);
be19cdf3 335 if (r < 0)
37b7cc8d 336 return r;
dd0bc0f1 337
36d892b7 338 m->etc_hosts_stat = st;
dd0bc0f1
LP
339 m->etc_hosts_last = ts;
340
341 return 1;
dd0bc0f1
LP
342}
343
344int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) {
345 bool found_a = false, found_aaaa = false;
0351eb11 346 struct in_addr_data k = {};
dd0bc0f1 347 EtcHostsItemByName *bn;
dd0bc0f1
LP
348 DnsResourceKey *t;
349 const char *name;
350 unsigned i;
351 int r;
352
353 assert(m);
354 assert(q);
355 assert(answer);
356
86317087
YW
357 if (!m->read_etc_hosts)
358 return 0;
359
be19cdf3 360 (void) manager_etc_hosts_read(m);
dd0bc0f1
LP
361
362 name = dns_question_first_name(q);
363 if (!name)
364 return 0;
365
366 r = dns_name_address(name, &k.family, &k.address);
367 if (r > 0) {
133eedad 368 EtcHostsItemByAddress *item;
dd0bc0f1
LP
369 DnsResourceKey *found_ptr = NULL;
370
37b7cc8d 371 item = hashmap_get(m->etc_hosts.by_address, &k);
dd0bc0f1
LP
372 if (!item)
373 return 0;
374
375 /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
376 * we'll only return if the request was for PTR. */
377
378 DNS_QUESTION_FOREACH(t, q) {
379 if (!IN_SET(t->type, DNS_TYPE_PTR, DNS_TYPE_ANY))
380 continue;
381 if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY))
382 continue;
383
1c02e7ba 384 r = dns_name_equal(dns_resource_key_name(t), name);
dd0bc0f1
LP
385 if (r < 0)
386 return r;
387 if (r > 0) {
388 found_ptr = t;
389 break;
390 }
391 }
392
393 if (found_ptr) {
eb164c51 394 r = dns_answer_reserve(answer, item->n_names);
dd0bc0f1
LP
395 if (r < 0)
396 return r;
397
398 STRV_FOREACH(n, item->names) {
399 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
400
401 rr = dns_resource_record_new(found_ptr);
402 if (!rr)
403 return -ENOMEM;
404
405 rr->ptr.name = strdup(*n);
406 if (!rr->ptr.name)
407 return -ENOMEM;
408
04617bf8 409 r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL);
dd0bc0f1
LP
410 if (r < 0)
411 return r;
412 }
413 }
414
415 return 1;
416 }
417
37b7cc8d 418 bn = hashmap_get(m->etc_hosts.by_name, name);
fd373593
ZJS
419 if (bn) {
420 r = dns_answer_reserve(answer, bn->n_addresses);
421 if (r < 0)
422 return r;
423 } else {
424 /* Check if name was listed with no address. If yes, continue to return an answer. */
425 if (!set_contains(m->etc_hosts.no_address, name))
426 return 0;
427 }
dd0bc0f1
LP
428
429 DNS_QUESTION_FOREACH(t, q) {
430 if (!IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_ANY))
431 continue;
432 if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY))
433 continue;
434
1c02e7ba 435 r = dns_name_equal(dns_resource_key_name(t), name);
dd0bc0f1
LP
436 if (r < 0)
437 return r;
438 if (r == 0)
439 continue;
440
441 if (IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_ANY))
442 found_a = true;
443 if (IN_SET(t->type, DNS_TYPE_AAAA, DNS_TYPE_ANY))
444 found_aaaa = true;
445
446 if (found_a && found_aaaa)
447 break;
448 }
449
fd373593 450 for (i = 0; bn && i < bn->n_addresses; i++) {
dd0bc0f1
LP
451 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
452
0351eb11
YW
453 if ((!found_a && bn->addresses[i]->family == AF_INET) ||
454 (!found_aaaa && bn->addresses[i]->family == AF_INET6))
dd0bc0f1
LP
455 continue;
456
0351eb11 457 r = dns_resource_record_new_address(&rr, bn->addresses[i]->family, &bn->addresses[i]->address, bn->name);
dd0bc0f1
LP
458 if (r < 0)
459 return r;
460
04617bf8 461 r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL);
dd0bc0f1
LP
462 if (r < 0)
463 return r;
464 }
465
4050e04b 466 return found_a || found_aaaa;
dd0bc0f1 467}