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