]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
dd0bc0f1 LP |
2 | |
3 | #include "fd-util.h" | |
4 | #include "fileio.h" | |
5 | #include "hostname-util.h" | |
dd0bc0f1 | 6 | #include "resolved-dns-synthesize.h" |
7c777a77 | 7 | #include "resolved-etc-hosts.h" |
dd0bc0f1 LP |
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 | ||
a1e92eee | 15 | static void etc_hosts_item_free(EtcHostsItem *item) { |
37b7cc8d ZJS |
16 | strv_free(item->names); |
17 | free(item); | |
18 | } | |
dd0bc0f1 | 19 | |
a1e92eee | 20 | static void etc_hosts_item_by_name_free(EtcHostsItemByName *item) { |
37b7cc8d ZJS |
21 | free(item->name); |
22 | free(item->addresses); | |
23 | free(item); | |
24 | } | |
dd0bc0f1 | 25 | |
78fc21a1 | 26 | void etc_hosts_free(EtcHosts *hosts) { |
37b7cc8d ZJS |
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); | |
fd373593 | 29 | hosts->no_address = set_free_free(hosts->no_address); |
37b7cc8d | 30 | } |
dd0bc0f1 | 31 | |
37b7cc8d ZJS |
32 | void manager_etc_hosts_flush(Manager *m) { |
33 | etc_hosts_free(&m->etc_hosts); | |
dd0bc0f1 LP |
34 | m->etc_hosts_mtime = USEC_INFINITY; |
35 | } | |
36 | ||
37b7cc8d | 37 | static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) { |
0351eb11 YW |
38 | _cleanup_free_ char *address_str = NULL; |
39 | struct in_addr_data address = {}; | |
fd373593 | 40 | bool found = false; |
dd0bc0f1 | 41 | EtcHostsItem *item; |
dd0bc0f1 LP |
42 | int r; |
43 | ||
37b7cc8d | 44 | assert(hosts); |
0351eb11 | 45 | assert(line); |
dd0bc0f1 | 46 | |
0351eb11 YW |
47 | r = extract_first_word(&line, &address_str, NULL, EXTRACT_RELAX); |
48 | if (r < 0) | |
bd005277 ZJS |
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* */ | |
0351eb11 | 51 | |
7bf8c3de | 52 | r = in_addr_ifindex_from_string_auto(address_str, &address.family, &address.address, NULL); |
bd005277 ZJS |
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 | } | |
0351eb11 YW |
57 | |
58 | r = in_addr_is_null(address.family, &address.address); | |
bd005277 ZJS |
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 | } | |
dd0bc0f1 LP |
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 { | |
78fc21a1 | 68 | /* If this is a normal address, then simply add entry mapping it to the specified names */ |
dd0bc0f1 | 69 | |
37b7cc8d | 70 | item = hashmap_get(hosts->by_address, &address); |
0351eb11 | 71 | if (!item) { |
37b7cc8d | 72 | r = hashmap_ensure_allocated(&hosts->by_address, &in_addr_data_hash_ops); |
dd0bc0f1 LP |
73 | if (r < 0) |
74 | return log_oom(); | |
75 | ||
0351eb11 | 76 | item = new0(EtcHostsItem, 1); |
dd0bc0f1 LP |
77 | if (!item) |
78 | return log_oom(); | |
79 | ||
0351eb11 | 80 | item->address = address; |
dd0bc0f1 | 81 | |
37b7cc8d | 82 | r = hashmap_put(hosts->by_address, &item->address, item); |
dd0bc0f1 LP |
83 | if (r < 0) { |
84 | free(item); | |
85 | return log_oom(); | |
86 | } | |
87 | } | |
88 | } | |
89 | ||
0351eb11 YW |
90 | for (;;) { |
91 | _cleanup_free_ char *name = NULL; | |
dd0bc0f1 LP |
92 | EtcHostsItemByName *bn; |
93 | ||
0351eb11 YW |
94 | r = extract_first_word(&line, &name, NULL, EXTRACT_RELAX); |
95 | if (r < 0) | |
bd005277 | 96 | return log_error_errno(r, "/etc/hosts:%u: couldn't extract host name: %m", nr); |
0351eb11 YW |
97 | if (r == 0) |
98 | break; | |
99 | ||
fd373593 ZJS |
100 | found = true; |
101 | ||
7470cc4c | 102 | r = dns_name_is_valid_ldh(name); |
bd005277 ZJS |
103 | if (r <= 0) { |
104 | log_warning_errno(r, "/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr, name); | |
105 | continue; | |
106 | } | |
107 | ||
fd373593 | 108 | if (is_localhost(name)) |
0351eb11 | 109 | /* Suppress the "localhost" line that is often seen */ |
0351eb11 | 110 | continue; |
0351eb11 | 111 | |
fd373593 ZJS |
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); | |
0351eb11 YW |
117 | if (r < 0) |
118 | return log_oom(); | |
fd373593 ZJS |
119 | |
120 | r = set_put(hosts->no_address, name); | |
121 | if (r < 0) | |
122 | return r; | |
123 | ||
124 | TAKE_PTR(name); | |
125 | continue; | |
0351eb11 YW |
126 | } |
127 | ||
fd373593 ZJS |
128 | r = strv_extend(&item->names, name); |
129 | if (r < 0) | |
130 | return log_oom(); | |
131 | ||
37b7cc8d | 132 | bn = hashmap_get(hosts->by_name, name); |
dd0bc0f1 | 133 | if (!bn) { |
37b7cc8d | 134 | r = hashmap_ensure_allocated(&hosts->by_name, &dns_name_hash_ops); |
dd0bc0f1 LP |
135 | if (r < 0) |
136 | return log_oom(); | |
137 | ||
138 | bn = new0(EtcHostsItemByName, 1); | |
139 | if (!bn) | |
140 | return log_oom(); | |
141 | ||
37b7cc8d | 142 | r = hashmap_put(hosts->by_name, name, bn); |
dd0bc0f1 | 143 | if (r < 0) { |
dd0bc0f1 LP |
144 | free(bn); |
145 | return log_oom(); | |
146 | } | |
0351eb11 YW |
147 | |
148 | bn->name = TAKE_PTR(name); | |
dd0bc0f1 LP |
149 | } |
150 | ||
fd373593 ZJS |
151 | if (!GREEDY_REALLOC(bn->addresses, bn->n_allocated, bn->n_addresses + 1)) |
152 | return log_oom(); | |
dd0bc0f1 | 153 | |
fd373593 | 154 | bn->addresses[bn->n_addresses++] = &item->address; |
dd0bc0f1 LP |
155 | } |
156 | ||
baaa35ad | 157 | if (!found) |
bd005277 | 158 | log_warning("/etc/hosts:%u: line is missing any host names", nr); |
dd0bc0f1 | 159 | |
0351eb11 | 160 | return 0; |
dd0bc0f1 LP |
161 | } |
162 | ||
78fc21a1 | 163 | int etc_hosts_parse(EtcHosts *hosts, FILE *f) { |
37b7cc8d | 164 | _cleanup_(etc_hosts_free) EtcHosts t = {}; |
37b7cc8d ZJS |
165 | unsigned nr = 0; |
166 | int r; | |
167 | ||
7c777a77 LP |
168 | for (;;) { |
169 | _cleanup_free_ char *line = NULL; | |
37b7cc8d ZJS |
170 | char *l; |
171 | ||
7c777a77 LP |
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 | ||
37b7cc8d ZJS |
178 | nr++; |
179 | ||
bd005277 ZJS |
180 | l = strchr(line, '#'); |
181 | if (l) | |
182 | *l = '\0'; | |
183 | ||
37b7cc8d ZJS |
184 | l = strstrip(line); |
185 | if (isempty(l)) | |
186 | continue; | |
37b7cc8d ZJS |
187 | |
188 | r = parse_line(&t, nr, l); | |
189 | if (r < 0) | |
190 | return r; | |
191 | } | |
192 | ||
be19cdf3 | 193 | etc_hosts_free(hosts); |
37b7cc8d ZJS |
194 | *hosts = t; |
195 | t = (EtcHosts) {}; /* prevent cleanup */ | |
196 | return 0; | |
197 | } | |
198 | ||
452ca091 | 199 | static int manager_etc_hosts_read(Manager *m) { |
dd0bc0f1 | 200 | _cleanup_fclose_ FILE *f = NULL; |
dd0bc0f1 LP |
201 | struct stat st; |
202 | usec_t ts; | |
dd0bc0f1 LP |
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) { | |
37b7cc8d ZJS |
215 | if (errno != ENOENT) |
216 | return log_error_errno(errno, "Failed to stat /etc/hosts: %m"); | |
dd0bc0f1 | 217 | |
37b7cc8d ZJS |
218 | manager_etc_hosts_flush(m); |
219 | return 0; | |
dd0bc0f1 LP |
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) { | |
37b7cc8d ZJS |
229 | if (errno != ENOENT) |
230 | return log_error_errno(errno, "Failed to open /etc/hosts: %m"); | |
dd0bc0f1 | 231 | |
37b7cc8d ZJS |
232 | manager_etc_hosts_flush(m); |
233 | return 0; | |
dd0bc0f1 LP |
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 | ||
37b7cc8d | 242 | r = etc_hosts_parse(&m->etc_hosts, f); |
be19cdf3 | 243 | if (r < 0) |
37b7cc8d | 244 | return r; |
dd0bc0f1 LP |
245 | |
246 | m->etc_hosts_mtime = timespec_load(&st.st_mtim); | |
247 | m->etc_hosts_last = ts; | |
248 | ||
249 | return 1; | |
dd0bc0f1 LP |
250 | } |
251 | ||
252 | int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) { | |
253 | bool found_a = false, found_aaaa = false; | |
0351eb11 | 254 | struct in_addr_data k = {}; |
dd0bc0f1 | 255 | EtcHostsItemByName *bn; |
dd0bc0f1 LP |
256 | DnsResourceKey *t; |
257 | const char *name; | |
258 | unsigned i; | |
259 | int r; | |
260 | ||
261 | assert(m); | |
262 | assert(q); | |
263 | assert(answer); | |
264 | ||
86317087 YW |
265 | if (!m->read_etc_hosts) |
266 | return 0; | |
267 | ||
be19cdf3 | 268 | (void) manager_etc_hosts_read(m); |
dd0bc0f1 LP |
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 | ||
37b7cc8d | 279 | item = hashmap_get(m->etc_hosts.by_address, &k); |
dd0bc0f1 LP |
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 | ||
1c02e7ba | 292 | r = dns_name_equal(dns_resource_key_name(t), name); |
dd0bc0f1 LP |
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 | ||
37b7cc8d | 328 | bn = hashmap_get(m->etc_hosts.by_name, name); |
fd373593 ZJS |
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 | } | |
dd0bc0f1 LP |
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 | ||
1c02e7ba | 345 | r = dns_name_equal(dns_resource_key_name(t), name); |
dd0bc0f1 LP |
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 | ||
fd373593 | 360 | for (i = 0; bn && i < bn->n_addresses; i++) { |
dd0bc0f1 LP |
361 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
362 | ||
0351eb11 YW |
363 | if ((!found_a && bn->addresses[i]->family == AF_INET) || |
364 | (!found_aaaa && bn->addresses[i]->family == AF_INET6)) | |
dd0bc0f1 LP |
365 | continue; |
366 | ||
0351eb11 | 367 | r = dns_resource_record_new_address(&rr, bn->addresses[i]->family, &bn->addresses[i]->address, bn->name); |
dd0bc0f1 LP |
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 | ||
4050e04b | 376 | return found_a || found_aaaa; |
dd0bc0f1 | 377 | } |