]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-etc-hosts.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / resolve / resolved-etc-hosts.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
dd0bc0f1 2/***
dd0bc0f1 3 Copyright 2016 Lennart Poettering
dd0bc0f1
LP
4***/
5
6#include "fd-util.h"
7#include "fileio.h"
8#include "hostname-util.h"
9#include "resolved-etc-hosts.h"
10#include "resolved-dns-synthesize.h"
11#include "string-util.h"
12#include "strv.h"
13#include "time-util.h"
14
15/* Recheck /etc/hosts at most once every 2s */
16#define ETC_HOSTS_RECHECK_USEC (2*USEC_PER_SEC)
17
18typedef struct EtcHostsItem {
19 int family;
20 union in_addr_union address;
21
22 char **names;
23} EtcHostsItem;
24
25typedef struct EtcHostsItemByName {
26 char *name;
27
28 EtcHostsItem **items;
29 size_t n_items, n_allocated;
30} EtcHostsItemByName;
31
32void manager_etc_hosts_flush(Manager *m) {
33 EtcHostsItem *item;
34 EtcHostsItemByName *bn;
35
36 while ((item = set_steal_first(m->etc_hosts_by_address))) {
37 strv_free(item->names);
38 free(item);
39 }
40
41 while ((bn = hashmap_steal_first(m->etc_hosts_by_name))) {
42 free(bn->name);
43 free(bn->items);
44 free(bn);
45 }
46
47 m->etc_hosts_by_address = set_free(m->etc_hosts_by_address);
48 m->etc_hosts_by_name = hashmap_free(m->etc_hosts_by_name);
49
50 m->etc_hosts_mtime = USEC_INFINITY;
51}
52
53static void etc_hosts_item_hash_func(const void *p, struct siphash *state) {
54 const EtcHostsItem *item = p;
55
56 siphash24_compress(&item->family, sizeof(item->family), state);
57
58 if (item->family == AF_INET)
59 siphash24_compress(&item->address.in, sizeof(item->address.in), state);
60 else if (item->family == AF_INET6)
61 siphash24_compress(&item->address.in6, sizeof(item->address.in6), state);
62}
63
64static int etc_hosts_item_compare_func(const void *a, const void *b) {
65 const EtcHostsItem *x = a, *y = b;
66
ec76e139 67 if (x->family != y->family)
dd0bc0f1
LP
68 return x->family - y->family;
69
70 if (x->family == AF_INET)
71 return memcmp(&x->address.in.s_addr, &y->address.in.s_addr, sizeof(struct in_addr));
72
73 if (x->family == AF_INET6)
74 return memcmp(&x->address.in6.s6_addr, &y->address.in6.s6_addr, sizeof(struct in6_addr));
75
76 return trivial_compare_func(a, b);
77}
78
79static const struct hash_ops etc_hosts_item_ops = {
80 .hash = etc_hosts_item_hash_func,
81 .compare = etc_hosts_item_compare_func,
82};
83
84static int add_item(Manager *m, int family, const union in_addr_union *address, char **names) {
85
86 EtcHostsItem key = {
87 .family = family,
88 .address = *address,
89 };
90 EtcHostsItem *item;
91 char **n;
92 int r;
93
94 assert(m);
95 assert(address);
96
97 r = in_addr_is_null(family, address);
98 if (r < 0)
99 return r;
100 if (r > 0)
101 /* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to
102 * nothing. */
103 item = NULL;
104 else {
105 /* If this is a normal address, then, simply add entry mapping it to the specified names */
106
107 item = set_get(m->etc_hosts_by_address, &key);
108 if (item) {
109 r = strv_extend_strv(&item->names, names, true);
110 if (r < 0)
111 return log_oom();
112 } else {
113
114 r = set_ensure_allocated(&m->etc_hosts_by_address, &etc_hosts_item_ops);
115 if (r < 0)
116 return log_oom();
117
118 item = new0(EtcHostsItem, 1);
119 if (!item)
120 return log_oom();
121
122 item->family = family;
123 item->address = *address;
124 item->names = names;
125
126 r = set_put(m->etc_hosts_by_address, item);
127 if (r < 0) {
128 free(item);
129 return log_oom();
130 }
131 }
132 }
133
134 STRV_FOREACH(n, names) {
135 EtcHostsItemByName *bn;
136
137 bn = hashmap_get(m->etc_hosts_by_name, *n);
138 if (!bn) {
139 r = hashmap_ensure_allocated(&m->etc_hosts_by_name, &dns_name_hash_ops);
140 if (r < 0)
141 return log_oom();
142
143 bn = new0(EtcHostsItemByName, 1);
144 if (!bn)
145 return log_oom();
146
147 bn->name = strdup(*n);
148 if (!bn->name) {
149 free(bn);
150 return log_oom();
151 }
152
153 r = hashmap_put(m->etc_hosts_by_name, bn->name, bn);
154 if (r < 0) {
155 free(bn->name);
156 free(bn);
157 return log_oom();
158 }
159 }
160
161 if (item) {
162 if (!GREEDY_REALLOC(bn->items, bn->n_allocated, bn->n_items+1))
163 return log_oom();
164
165 bn->items[bn->n_items++] = item;
166 }
167 }
168
169 return 0;
170}
171
172static int parse_line(Manager *m, unsigned nr, const char *line) {
173 _cleanup_free_ char *address = NULL;
174 _cleanup_strv_free_ char **names = NULL;
175 union in_addr_union in;
176 bool suppressed = false;
177 int family, r;
178
179 assert(m);
180 assert(line);
181
182 r = extract_first_word(&line, &address, NULL, EXTRACT_RELAX);
183 if (r < 0)
184 return log_error_errno(r, "Couldn't extract address, in line /etc/hosts:%u.", nr);
185 if (r == 0) {
186 log_error("Premature end of line, in line /etc/hosts:%u.", nr);
187 return -EINVAL;
188 }
189
190 r = in_addr_from_string_auto(address, &family, &in);
191 if (r < 0)
192 return log_error_errno(r, "Address '%s' is invalid, in line /etc/hosts:%u.", address, nr);
193
194 for (;;) {
195 _cleanup_free_ char *name = NULL;
196
197 r = extract_first_word(&line, &name, NULL, EXTRACT_RELAX);
198 if (r < 0)
199 return log_error_errno(r, "Couldn't extract host name, in line /etc/hosts:%u.", nr);
200 if (r == 0)
201 break;
202
203 r = dns_name_is_valid(name);
204 if (r <= 0)
205 return log_error_errno(r, "Hostname %s is not valid, ignoring, in line /etc/hosts:%u.", name, nr);
206
207 if (is_localhost(name)) {
208 /* Suppress the "localhost" line that is often seen */
209 suppressed = true;
210 continue;
211 }
212
213 r = strv_push(&names, name);
214 if (r < 0)
215 return log_oom();
216
217 name = NULL;
218 }
219
220 if (strv_isempty(names)) {
221
222 if (suppressed)
223 return 0;
224
225 log_error("Line is missing any host names, in line /etc/hosts:%u.", nr);
226 return -EINVAL;
227 }
228
229 /* Takes possession of the names strv */
230 r = add_item(m, family, &in, names);
231 if (r < 0)
232 return r;
233
234 names = NULL;
235 return r;
236}
237
238int manager_etc_hosts_read(Manager *m) {
239 _cleanup_fclose_ FILE *f = NULL;
240 char line[LINE_MAX];
241 struct stat st;
242 usec_t ts;
243 unsigned nr = 0;
244 int r;
245
246 assert_se(sd_event_now(m->event, clock_boottime_or_monotonic(), &ts) >= 0);
247
248 /* See if we checked /etc/hosts recently already */
249 if (m->etc_hosts_last != USEC_INFINITY && m->etc_hosts_last + ETC_HOSTS_RECHECK_USEC > ts)
250 return 0;
251
252 m->etc_hosts_last = ts;
253
254 if (m->etc_hosts_mtime != USEC_INFINITY) {
255 if (stat("/etc/hosts", &st) < 0) {
256 if (errno == ENOENT) {
257 r = 0;
258 goto clear;
259 }
260
261 return log_error_errno(errno, "Failed to stat /etc/hosts: %m");
262 }
263
264 /* Did the mtime change? If not, there's no point in re-reading the file. */
265 if (timespec_load(&st.st_mtim) == m->etc_hosts_mtime)
266 return 0;
267 }
268
269 f = fopen("/etc/hosts", "re");
270 if (!f) {
271 if (errno == ENOENT) {
272 r = 0;
273 goto clear;
274 }
275
276 return log_error_errno(errno, "Failed to open /etc/hosts: %m");
277 }
278
279 /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
280 * invocation */
281 r = fstat(fileno(f), &st);
282 if (r < 0)
283 return log_error_errno(errno, "Failed to fstat() /etc/hosts: %m");
284
285 manager_etc_hosts_flush(m);
286
287 FOREACH_LINE(line, f, return log_error_errno(errno, "Failed to read /etc/hosts: %m")) {
288 char *l;
289
313cefa1 290 nr++;
dd0bc0f1
LP
291
292 l = strstrip(line);
293 if (isempty(l))
294 continue;
295 if (l[0] == '#')
296 continue;
297
298 r = parse_line(m, nr, l);
299 if (r == -ENOMEM) /* On OOM we abandon the half-built-up structure. All other errors we ignore and proceed */
300 goto clear;
301 }
302
303 m->etc_hosts_mtime = timespec_load(&st.st_mtim);
304 m->etc_hosts_last = ts;
305
306 return 1;
307
308clear:
309 manager_etc_hosts_flush(m);
310 return r;
311}
312
313int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) {
314 bool found_a = false, found_aaaa = false;
315 EtcHostsItemByName *bn;
316 EtcHostsItem k = {};
317 DnsResourceKey *t;
318 const char *name;
319 unsigned i;
320 int r;
321
322 assert(m);
323 assert(q);
324 assert(answer);
325
326 r = manager_etc_hosts_read(m);
327 if (r < 0)
328 return r;
329
330 name = dns_question_first_name(q);
331 if (!name)
332 return 0;
333
334 r = dns_name_address(name, &k.family, &k.address);
335 if (r > 0) {
336 EtcHostsItem *item;
337 DnsResourceKey *found_ptr = NULL;
338
339 item = set_get(m->etc_hosts_by_address, &k);
340 if (!item)
341 return 0;
342
343 /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
344 * we'll only return if the request was for PTR. */
345
346 DNS_QUESTION_FOREACH(t, q) {
347 if (!IN_SET(t->type, DNS_TYPE_PTR, DNS_TYPE_ANY))
348 continue;
349 if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY))
350 continue;
351
1c02e7ba 352 r = dns_name_equal(dns_resource_key_name(t), name);
dd0bc0f1
LP
353 if (r < 0)
354 return r;
355 if (r > 0) {
356 found_ptr = t;
357 break;
358 }
359 }
360
361 if (found_ptr) {
362 char **n;
363
364 r = dns_answer_reserve(answer, strv_length(item->names));
365 if (r < 0)
366 return r;
367
368 STRV_FOREACH(n, item->names) {
369 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
370
371 rr = dns_resource_record_new(found_ptr);
372 if (!rr)
373 return -ENOMEM;
374
375 rr->ptr.name = strdup(*n);
376 if (!rr->ptr.name)
377 return -ENOMEM;
378
379 r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
380 if (r < 0)
381 return r;
382 }
383 }
384
385 return 1;
386 }
387
388 bn = hashmap_get(m->etc_hosts_by_name, name);
389 if (!bn)
390 return 0;
391
392 r = dns_answer_reserve(answer, bn->n_items);
393 if (r < 0)
394 return r;
395
396 DNS_QUESTION_FOREACH(t, q) {
397 if (!IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_ANY))
398 continue;
399 if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY))
400 continue;
401
1c02e7ba 402 r = dns_name_equal(dns_resource_key_name(t), name);
dd0bc0f1
LP
403 if (r < 0)
404 return r;
405 if (r == 0)
406 continue;
407
408 if (IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_ANY))
409 found_a = true;
410 if (IN_SET(t->type, DNS_TYPE_AAAA, DNS_TYPE_ANY))
411 found_aaaa = true;
412
413 if (found_a && found_aaaa)
414 break;
415 }
416
417 for (i = 0; i < bn->n_items; i++) {
418 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
419
4050e04b
MP
420 if ((!found_a && bn->items[i]->family == AF_INET) ||
421 (!found_aaaa && bn->items[i]->family == AF_INET6))
dd0bc0f1
LP
422 continue;
423
424 r = dns_resource_record_new_address(&rr, bn->items[i]->family, &bn->items[i]->address, bn->name);
425 if (r < 0)
426 return r;
427
428 r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
429 if (r < 0)
430 return r;
431 }
432
4050e04b 433 return found_a || found_aaaa;
dd0bc0f1 434}