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