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