]>
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 | ||
37b7cc8d ZJS |
15 | static inline void etc_hosts_item_free(EtcHostsItem *item) { |
16 | strv_free(item->names); | |
17 | free(item); | |
18 | } | |
dd0bc0f1 | 19 | |
37b7cc8d ZJS |
20 | static inline void etc_hosts_item_by_name_free(EtcHostsItemByName *item) { |
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) | |
49 | return log_error_errno(r, "Couldn't extract address, in line /etc/hosts:%u.", nr); | |
baaa35ad ZJS |
50 | if (r == 0) |
51 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
52 | "Premature end of line, in line /etc/hosts:%u.", | |
53 | nr); | |
0351eb11 | 54 | |
7bf8c3de | 55 | r = in_addr_ifindex_from_string_auto(address_str, &address.family, &address.address, NULL); |
0351eb11 YW |
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); | |
dd0bc0f1 LP |
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 { | |
78fc21a1 | 67 | /* If this is a normal address, then simply add entry mapping it to the specified names */ |
dd0bc0f1 | 68 | |
37b7cc8d | 69 | item = hashmap_get(hosts->by_address, &address); |
0351eb11 | 70 | if (!item) { |
37b7cc8d | 71 | r = hashmap_ensure_allocated(&hosts->by_address, &in_addr_data_hash_ops); |
dd0bc0f1 LP |
72 | if (r < 0) |
73 | return log_oom(); | |
74 | ||
0351eb11 | 75 | item = new0(EtcHostsItem, 1); |
dd0bc0f1 LP |
76 | if (!item) |
77 | return log_oom(); | |
78 | ||
0351eb11 | 79 | item->address = address; |
dd0bc0f1 | 80 | |
37b7cc8d | 81 | r = hashmap_put(hosts->by_address, &item->address, item); |
dd0bc0f1 LP |
82 | if (r < 0) { |
83 | free(item); | |
84 | return log_oom(); | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
0351eb11 YW |
89 | for (;;) { |
90 | _cleanup_free_ char *name = NULL; | |
dd0bc0f1 LP |
91 | EtcHostsItemByName *bn; |
92 | ||
0351eb11 YW |
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 | ||
fd373593 ZJS |
103 | found = true; |
104 | ||
105 | if (is_localhost(name)) | |
0351eb11 | 106 | /* Suppress the "localhost" line that is often seen */ |
0351eb11 | 107 | continue; |
0351eb11 | 108 | |
fd373593 ZJS |
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); | |
0351eb11 YW |
114 | if (r < 0) |
115 | return log_oom(); | |
fd373593 ZJS |
116 | |
117 | r = set_put(hosts->no_address, name); | |
118 | if (r < 0) | |
119 | return r; | |
120 | ||
121 | TAKE_PTR(name); | |
122 | continue; | |
0351eb11 YW |
123 | } |
124 | ||
fd373593 ZJS |
125 | r = strv_extend(&item->names, name); |
126 | if (r < 0) | |
127 | return log_oom(); | |
128 | ||
37b7cc8d | 129 | bn = hashmap_get(hosts->by_name, name); |
dd0bc0f1 | 130 | if (!bn) { |
37b7cc8d | 131 | r = hashmap_ensure_allocated(&hosts->by_name, &dns_name_hash_ops); |
dd0bc0f1 LP |
132 | if (r < 0) |
133 | return log_oom(); | |
134 | ||
135 | bn = new0(EtcHostsItemByName, 1); | |
136 | if (!bn) | |
137 | return log_oom(); | |
138 | ||
37b7cc8d | 139 | r = hashmap_put(hosts->by_name, name, bn); |
dd0bc0f1 | 140 | if (r < 0) { |
dd0bc0f1 LP |
141 | free(bn); |
142 | return log_oom(); | |
143 | } | |
0351eb11 YW |
144 | |
145 | bn->name = TAKE_PTR(name); | |
dd0bc0f1 LP |
146 | } |
147 | ||
fd373593 ZJS |
148 | if (!GREEDY_REALLOC(bn->addresses, bn->n_allocated, bn->n_addresses + 1)) |
149 | return log_oom(); | |
dd0bc0f1 | 150 | |
fd373593 | 151 | bn->addresses[bn->n_addresses++] = &item->address; |
dd0bc0f1 LP |
152 | } |
153 | ||
baaa35ad ZJS |
154 | if (!found) |
155 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
156 | "Line is missing any host names, in line /etc/hosts:%u.", | |
157 | nr); | |
dd0bc0f1 | 158 | |
0351eb11 | 159 | return 0; |
dd0bc0f1 LP |
160 | } |
161 | ||
78fc21a1 | 162 | int etc_hosts_parse(EtcHosts *hosts, FILE *f) { |
37b7cc8d | 163 | _cleanup_(etc_hosts_free) EtcHosts t = {}; |
37b7cc8d ZJS |
164 | unsigned nr = 0; |
165 | int r; | |
166 | ||
7c777a77 LP |
167 | for (;;) { |
168 | _cleanup_free_ char *line = NULL; | |
37b7cc8d ZJS |
169 | char *l; |
170 | ||
7c777a77 LP |
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 | ||
37b7cc8d ZJS |
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 | ||
be19cdf3 | 190 | etc_hosts_free(hosts); |
37b7cc8d ZJS |
191 | *hosts = t; |
192 | t = (EtcHosts) {}; /* prevent cleanup */ | |
193 | return 0; | |
194 | } | |
195 | ||
452ca091 | 196 | static int manager_etc_hosts_read(Manager *m) { |
dd0bc0f1 | 197 | _cleanup_fclose_ FILE *f = NULL; |
dd0bc0f1 LP |
198 | struct stat st; |
199 | usec_t ts; | |
dd0bc0f1 LP |
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) { | |
37b7cc8d ZJS |
212 | if (errno != ENOENT) |
213 | return log_error_errno(errno, "Failed to stat /etc/hosts: %m"); | |
dd0bc0f1 | 214 | |
37b7cc8d ZJS |
215 | manager_etc_hosts_flush(m); |
216 | return 0; | |
dd0bc0f1 LP |
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) { | |
37b7cc8d ZJS |
226 | if (errno != ENOENT) |
227 | return log_error_errno(errno, "Failed to open /etc/hosts: %m"); | |
dd0bc0f1 | 228 | |
37b7cc8d ZJS |
229 | manager_etc_hosts_flush(m); |
230 | return 0; | |
dd0bc0f1 LP |
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 | ||
37b7cc8d | 239 | r = etc_hosts_parse(&m->etc_hosts, f); |
be19cdf3 | 240 | if (r < 0) |
37b7cc8d | 241 | return r; |
dd0bc0f1 LP |
242 | |
243 | m->etc_hosts_mtime = timespec_load(&st.st_mtim); | |
244 | m->etc_hosts_last = ts; | |
245 | ||
246 | return 1; | |
dd0bc0f1 LP |
247 | } |
248 | ||
249 | int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) { | |
250 | bool found_a = false, found_aaaa = false; | |
0351eb11 | 251 | struct in_addr_data k = {}; |
dd0bc0f1 | 252 | EtcHostsItemByName *bn; |
dd0bc0f1 LP |
253 | DnsResourceKey *t; |
254 | const char *name; | |
255 | unsigned i; | |
256 | int r; | |
257 | ||
258 | assert(m); | |
259 | assert(q); | |
260 | assert(answer); | |
261 | ||
86317087 YW |
262 | if (!m->read_etc_hosts) |
263 | return 0; | |
264 | ||
be19cdf3 | 265 | (void) manager_etc_hosts_read(m); |
dd0bc0f1 LP |
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 | ||
37b7cc8d | 276 | item = hashmap_get(m->etc_hosts.by_address, &k); |
dd0bc0f1 LP |
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 | ||
1c02e7ba | 289 | r = dns_name_equal(dns_resource_key_name(t), name); |
dd0bc0f1 LP |
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 | ||
37b7cc8d | 325 | bn = hashmap_get(m->etc_hosts.by_name, name); |
fd373593 ZJS |
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 | } | |
dd0bc0f1 LP |
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 | ||
1c02e7ba | 342 | r = dns_name_equal(dns_resource_key_name(t), name); |
dd0bc0f1 LP |
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 | ||
fd373593 | 357 | for (i = 0; bn && i < bn->n_addresses; i++) { |
dd0bc0f1 LP |
358 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
359 | ||
0351eb11 YW |
360 | if ((!found_a && bn->addresses[i]->family == AF_INET) || |
361 | (!found_aaaa && bn->addresses[i]->family == AF_INET6)) | |
dd0bc0f1 LP |
362 | continue; |
363 | ||
0351eb11 | 364 | r = dns_resource_record_new_address(&rr, bn->addresses[i]->family, &bn->addresses[i]->address, bn->name); |
dd0bc0f1 LP |
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 | ||
4050e04b | 373 | return found_a || found_aaaa; |
dd0bc0f1 | 374 | } |