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