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