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