]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-etc-hosts.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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-dns-synthesize.h"
7 #include "resolved-etc-hosts.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 unsigned nr = 0;
165 int r;
166
167 for (;;) {
168 _cleanup_free_ char *line = NULL;
169 char *l;
170
171 r = read_line(f, LONG_LINE_MAX, &line);
172 if (r < 0)
173 return log_error_errno(r, "Failed to read /etc/hosts: %m");
174 if (r == 0)
175 break;
176
177 nr++;
178
179 l = strstrip(line);
180 if (isempty(l))
181 continue;
182 if (l[0] == '#')
183 continue;
184
185 r = parse_line(&t, nr, l);
186 if (r < 0)
187 return r;
188 }
189
190 etc_hosts_free(hosts);
191 *hosts = t;
192 t = (EtcHosts) {}; /* prevent cleanup */
193 return 0;
194 }
195
196 static int manager_etc_hosts_read(Manager *m) {
197 _cleanup_fclose_ FILE *f = NULL;
198 struct stat st;
199 usec_t ts;
200 int r;
201
202 assert_se(sd_event_now(m->event, clock_boottime_or_monotonic(), &ts) >= 0);
203
204 /* See if we checked /etc/hosts recently already */
205 if (m->etc_hosts_last != USEC_INFINITY && m->etc_hosts_last + ETC_HOSTS_RECHECK_USEC > ts)
206 return 0;
207
208 m->etc_hosts_last = ts;
209
210 if (m->etc_hosts_mtime != USEC_INFINITY) {
211 if (stat("/etc/hosts", &st) < 0) {
212 if (errno != ENOENT)
213 return log_error_errno(errno, "Failed to stat /etc/hosts: %m");
214
215 manager_etc_hosts_flush(m);
216 return 0;
217 }
218
219 /* Did the mtime change? If not, there's no point in re-reading the file. */
220 if (timespec_load(&st.st_mtim) == m->etc_hosts_mtime)
221 return 0;
222 }
223
224 f = fopen("/etc/hosts", "re");
225 if (!f) {
226 if (errno != ENOENT)
227 return log_error_errno(errno, "Failed to open /etc/hosts: %m");
228
229 manager_etc_hosts_flush(m);
230 return 0;
231 }
232
233 /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
234 * invocation */
235 r = fstat(fileno(f), &st);
236 if (r < 0)
237 return log_error_errno(errno, "Failed to fstat() /etc/hosts: %m");
238
239 r = etc_hosts_parse(&m->etc_hosts, f);
240 if (r < 0)
241 return r;
242
243 m->etc_hosts_mtime = timespec_load(&st.st_mtim);
244 m->etc_hosts_last = ts;
245
246 return 1;
247 }
248
249 int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) {
250 bool found_a = false, found_aaaa = false;
251 struct in_addr_data k = {};
252 EtcHostsItemByName *bn;
253 DnsResourceKey *t;
254 const char *name;
255 unsigned i;
256 int r;
257
258 assert(m);
259 assert(q);
260 assert(answer);
261
262 if (!m->read_etc_hosts)
263 return 0;
264
265 (void) manager_etc_hosts_read(m);
266
267 name = dns_question_first_name(q);
268 if (!name)
269 return 0;
270
271 r = dns_name_address(name, &k.family, &k.address);
272 if (r > 0) {
273 EtcHostsItem *item;
274 DnsResourceKey *found_ptr = NULL;
275
276 item = hashmap_get(m->etc_hosts.by_address, &k);
277 if (!item)
278 return 0;
279
280 /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
281 * we'll only return if the request was for PTR. */
282
283 DNS_QUESTION_FOREACH(t, q) {
284 if (!IN_SET(t->type, DNS_TYPE_PTR, DNS_TYPE_ANY))
285 continue;
286 if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY))
287 continue;
288
289 r = dns_name_equal(dns_resource_key_name(t), name);
290 if (r < 0)
291 return r;
292 if (r > 0) {
293 found_ptr = t;
294 break;
295 }
296 }
297
298 if (found_ptr) {
299 char **n;
300
301 r = dns_answer_reserve(answer, strv_length(item->names));
302 if (r < 0)
303 return r;
304
305 STRV_FOREACH(n, item->names) {
306 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
307
308 rr = dns_resource_record_new(found_ptr);
309 if (!rr)
310 return -ENOMEM;
311
312 rr->ptr.name = strdup(*n);
313 if (!rr->ptr.name)
314 return -ENOMEM;
315
316 r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
317 if (r < 0)
318 return r;
319 }
320 }
321
322 return 1;
323 }
324
325 bn = hashmap_get(m->etc_hosts.by_name, name);
326 if (bn) {
327 r = dns_answer_reserve(answer, bn->n_addresses);
328 if (r < 0)
329 return r;
330 } else {
331 /* Check if name was listed with no address. If yes, continue to return an answer. */
332 if (!set_contains(m->etc_hosts.no_address, name))
333 return 0;
334 }
335
336 DNS_QUESTION_FOREACH(t, q) {
337 if (!IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_ANY))
338 continue;
339 if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY))
340 continue;
341
342 r = dns_name_equal(dns_resource_key_name(t), name);
343 if (r < 0)
344 return r;
345 if (r == 0)
346 continue;
347
348 if (IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_ANY))
349 found_a = true;
350 if (IN_SET(t->type, DNS_TYPE_AAAA, DNS_TYPE_ANY))
351 found_aaaa = true;
352
353 if (found_a && found_aaaa)
354 break;
355 }
356
357 for (i = 0; bn && i < bn->n_addresses; i++) {
358 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
359
360 if ((!found_a && bn->addresses[i]->family == AF_INET) ||
361 (!found_aaaa && bn->addresses[i]->family == AF_INET6))
362 continue;
363
364 r = dns_resource_record_new_address(&rr, bn->addresses[i]->family, &bn->addresses[i]->address, bn->name);
365 if (r < 0)
366 return r;
367
368 r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
369 if (r < 0)
370 return r;
371 }
372
373 return found_a || found_aaaa;
374 }