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