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