]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
dd0bc0f1 | 2 | |
ca78ad1d | 3 | #include <sys/stat.h> |
284d7641 | 4 | #include "sd-event.h" |
ca78ad1d | 5 | |
b78d73fa | 6 | #include "alloc-util.h" |
68527d30 | 7 | #include "dns-domain.h" |
284d7641 | 8 | #include "extract-word.h" |
dd0bc0f1 LP |
9 | #include "fd-util.h" |
10 | #include "fileio.h" | |
11 | #include "hostname-util.h" | |
284d7641 | 12 | #include "log.h" |
68527d30 DDM |
13 | #include "resolved-dns-answer.h" |
14 | #include "resolved-dns-question.h" | |
15 | #include "resolved-dns-rr.h" | |
7c777a77 | 16 | #include "resolved-etc-hosts.h" |
68527d30 | 17 | #include "resolved-manager.h" |
284d7641 | 18 | #include "set.h" |
5c3fa98d | 19 | #include "socket-netlink.h" |
36d892b7 | 20 | #include "stat-util.h" |
dd0bc0f1 | 21 | #include "string-util.h" |
dd0bc0f1 LP |
22 | #include "time-util.h" |
23 | ||
24 | /* Recheck /etc/hosts at most once every 2s */ | |
25 | #define ETC_HOSTS_RECHECK_USEC (2*USEC_PER_SEC) | |
26 | ||
bb5c7730 YW |
27 | static EtcHostsItemByAddress *etc_hosts_item_by_address_free(EtcHostsItemByAddress *item) { |
28 | if (!item) | |
29 | return NULL; | |
30 | ||
16a6bc5a | 31 | set_free(item->names); |
bb5c7730 | 32 | return mfree(item); |
37b7cc8d | 33 | } |
dd0bc0f1 | 34 | |
e4e34099 YW |
35 | DEFINE_TRIVIAL_CLEANUP_FUNC(EtcHostsItemByAddress*, etc_hosts_item_by_address_free); |
36 | ||
59dad407 YW |
37 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
38 | by_address_hash_ops, | |
39 | struct in_addr_data, | |
40 | in_addr_data_hash_func, | |
41 | in_addr_data_compare_func, | |
42 | EtcHostsItemByAddress, | |
43 | etc_hosts_item_by_address_free); | |
44 | ||
bb5c7730 YW |
45 | static EtcHostsItemByName *etc_hosts_item_by_name_free(EtcHostsItemByName *item) { |
46 | if (!item) | |
47 | return NULL; | |
48 | ||
37b7cc8d | 49 | free(item->name); |
16a6bc5a | 50 | set_free(item->addresses); |
bb5c7730 | 51 | return mfree(item); |
37b7cc8d | 52 | } |
dd0bc0f1 | 53 | |
e4e34099 YW |
54 | DEFINE_TRIVIAL_CLEANUP_FUNC(EtcHostsItemByName*, etc_hosts_item_by_name_free); |
55 | ||
59dad407 YW |
56 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
57 | by_name_hash_ops, | |
58 | char, | |
59 | dns_name_hash_func, | |
60 | dns_name_compare_func, | |
61 | EtcHostsItemByName, | |
62 | etc_hosts_item_by_name_free); | |
63 | ||
0f7bdf35 YW |
64 | void etc_hosts_clear(EtcHosts *hosts) { |
65 | assert(hosts); | |
66 | ||
59dad407 YW |
67 | hosts->by_address = hashmap_free(hosts->by_address); |
68 | hosts->by_name = hashmap_free(hosts->by_name); | |
232481a0 | 69 | hosts->no_address = set_free(hosts->no_address); |
37b7cc8d | 70 | } |
dd0bc0f1 | 71 | |
37b7cc8d | 72 | void manager_etc_hosts_flush(Manager *m) { |
0f7bdf35 | 73 | etc_hosts_clear(&m->etc_hosts); |
36d892b7 | 74 | m->etc_hosts_stat = (struct stat) {}; |
dd0bc0f1 LP |
75 | } |
76 | ||
37b7cc8d | 77 | static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) { |
0351eb11 YW |
78 | _cleanup_free_ char *address_str = NULL; |
79 | struct in_addr_data address = {}; | |
fd373593 | 80 | bool found = false; |
133eedad | 81 | EtcHostsItemByAddress *item; |
dd0bc0f1 LP |
82 | int r; |
83 | ||
37b7cc8d | 84 | assert(hosts); |
0351eb11 | 85 | assert(line); |
dd0bc0f1 | 86 | |
0351eb11 YW |
87 | r = extract_first_word(&line, &address_str, NULL, EXTRACT_RELAX); |
88 | if (r < 0) | |
bd005277 ZJS |
89 | return log_error_errno(r, "/etc/hosts:%u: failed to extract address: %m", nr); |
90 | assert(r > 0); /* We already checked that the line is not empty, so it should contain *something* */ | |
0351eb11 | 91 | |
7bf8c3de | 92 | r = in_addr_ifindex_from_string_auto(address_str, &address.family, &address.address, NULL); |
bd005277 ZJS |
93 | if (r < 0) { |
94 | log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str); | |
95 | return 0; | |
96 | } | |
0351eb11 | 97 | |
94876904 | 98 | r = in_addr_data_is_null(&address); |
bd005277 ZJS |
99 | if (r < 0) { |
100 | log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str); | |
101 | return 0; | |
102 | } | |
dd0bc0f1 LP |
103 | if (r > 0) |
104 | /* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to | |
105 | * nothing. */ | |
106 | item = NULL; | |
107 | else { | |
78fc21a1 | 108 | /* If this is a normal address, then simply add entry mapping it to the specified names */ |
dd0bc0f1 | 109 | |
37b7cc8d | 110 | item = hashmap_get(hosts->by_address, &address); |
0351eb11 | 111 | if (!item) { |
e4e34099 | 112 | _cleanup_(etc_hosts_item_by_address_freep) EtcHostsItemByAddress *new_item = NULL; |
dd0bc0f1 | 113 | |
e4e34099 YW |
114 | new_item = new(EtcHostsItemByAddress, 1); |
115 | if (!new_item) | |
dd0bc0f1 LP |
116 | return log_oom(); |
117 | ||
e4e34099 | 118 | *new_item = (EtcHostsItemByAddress) { |
1ed31408 LP |
119 | .address = address, |
120 | }; | |
dd0bc0f1 | 121 | |
59dad407 | 122 | r = hashmap_ensure_put(&hosts->by_address, &by_address_hash_ops, &new_item->address, new_item); |
e4e34099 | 123 | if (r < 0) |
dd0bc0f1 | 124 | return log_oom(); |
e4e34099 YW |
125 | |
126 | item = TAKE_PTR(new_item); | |
dd0bc0f1 LP |
127 | } |
128 | } | |
129 | ||
0351eb11 YW |
130 | for (;;) { |
131 | _cleanup_free_ char *name = NULL; | |
dd0bc0f1 LP |
132 | EtcHostsItemByName *bn; |
133 | ||
0351eb11 YW |
134 | r = extract_first_word(&line, &name, NULL, EXTRACT_RELAX); |
135 | if (r < 0) | |
38b38500 | 136 | return log_error_errno(r, "/etc/hosts:%u: couldn't extract hostname: %m", nr); |
0351eb11 YW |
137 | if (r == 0) |
138 | break; | |
139 | ||
7470cc4c | 140 | r = dns_name_is_valid_ldh(name); |
bd005277 | 141 | if (r <= 0) { |
98b1eb71 YW |
142 | if (r < 0) |
143 | log_warning_errno(r, "/etc/hosts:%u: Failed to check the validity of hostname \"%s\", ignoring: %m", nr, name); | |
144 | else | |
145 | log_warning("/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr, name); | |
bd005277 ZJS |
146 | continue; |
147 | } | |
148 | ||
10b0c818 YW |
149 | found = true; |
150 | ||
fd373593 ZJS |
151 | if (!item) { |
152 | /* Optimize the case where we don't need to store any addresses, by storing | |
153 | * only the name in a dedicated Set instead of the hashmap */ | |
154 | ||
232481a0 | 155 | r = set_ensure_consume(&hosts->no_address, &dns_name_hash_ops_free, TAKE_PTR(name)); |
fd373593 | 156 | if (r < 0) |
10b0c818 | 157 | return log_oom(); |
fd373593 | 158 | |
fd373593 | 159 | continue; |
0351eb11 YW |
160 | } |
161 | ||
37b7cc8d | 162 | bn = hashmap_get(hosts->by_name, name); |
dd0bc0f1 | 163 | if (!bn) { |
e4e34099 | 164 | _cleanup_(etc_hosts_item_by_name_freep) EtcHostsItemByName *new_item = NULL; |
16a6bc5a YW |
165 | _cleanup_free_ char *name_copy = NULL; |
166 | ||
167 | name_copy = strdup(name); | |
168 | if (!name_copy) | |
169 | return log_oom(); | |
dd0bc0f1 | 170 | |
e4e34099 YW |
171 | new_item = new(EtcHostsItemByName, 1); |
172 | if (!new_item) | |
dd0bc0f1 LP |
173 | return log_oom(); |
174 | ||
e4e34099 | 175 | *new_item = (EtcHostsItemByName) { |
16a6bc5a | 176 | .name = TAKE_PTR(name_copy), |
e4e34099 YW |
177 | }; |
178 | ||
59dad407 | 179 | r = hashmap_ensure_put(&hosts->by_name, &by_name_hash_ops, new_item->name, new_item); |
e4e34099 | 180 | if (r < 0) |
dd0bc0f1 | 181 | return log_oom(); |
0351eb11 | 182 | |
e4e34099 | 183 | bn = TAKE_PTR(new_item); |
dd0bc0f1 LP |
184 | } |
185 | ||
16a6bc5a YW |
186 | if (!set_contains(bn->addresses, &address)) { |
187 | _cleanup_free_ struct in_addr_data *address_copy = NULL; | |
188 | ||
189 | address_copy = newdup(struct in_addr_data, &address, 1); | |
190 | if (!address_copy) | |
191 | return log_oom(); | |
dd0bc0f1 | 192 | |
16a6bc5a YW |
193 | r = set_ensure_consume(&bn->addresses, &in_addr_data_hash_ops_free, TAKE_PTR(address_copy)); |
194 | if (r < 0) | |
195 | return log_oom(); | |
196 | } | |
197 | ||
1bd76a62 | 198 | r = set_ensure_put(&item->names, &dns_name_hash_ops_free, name); |
16a6bc5a YW |
199 | if (r < 0) |
200 | return log_oom(); | |
1bd76a62 DL |
201 | if (r == 0) /* the name is already listed */ |
202 | continue; | |
203 | /* | |
204 | * Keep track of the first name listed for this address. | |
205 | * This name will be used in responses as the canonical name. | |
206 | */ | |
207 | if (!item->canonical_name) | |
208 | item->canonical_name = name; | |
209 | TAKE_PTR(name); | |
dd0bc0f1 LP |
210 | } |
211 | ||
baaa35ad | 212 | if (!found) |
10b0c818 | 213 | log_warning("/etc/hosts:%u: line is missing any valid hostnames", nr); |
dd0bc0f1 | 214 | |
0351eb11 | 215 | return 0; |
dd0bc0f1 LP |
216 | } |
217 | ||
9ca875e8 LP |
218 | static void strip_localhost(EtcHosts *hosts) { |
219 | static const struct in_addr_data local_in_addrs[] = { | |
220 | { | |
221 | .family = AF_INET, | |
222 | #if __BYTE_ORDER == __LITTLE_ENDIAN | |
223 | /* We want constant expressions here, that's why we don't use htole32() here */ | |
224 | .address.in.s_addr = UINT32_C(0x0100007F), | |
225 | #else | |
226 | .address.in.s_addr = UINT32_C(0x7F000001), | |
227 | #endif | |
228 | }, | |
229 | { | |
230 | .family = AF_INET6, | |
231 | .address.in6 = IN6ADDR_LOOPBACK_INIT, | |
232 | }, | |
233 | }; | |
234 | ||
9ca875e8 LP |
235 | assert(hosts); |
236 | ||
237 | /* Removes the 'localhost' entry from what we loaded. But only if the mapping is exclusively between | |
238 | * 127.0.0.1 and localhost (or aliases to that we recognize). If there's any other name assigned to | |
239 | * it, we leave the entry in. | |
240 | * | |
241 | * This way our regular synthesizing can take over, but only if it would result in the exact same | |
242 | * mappings. */ | |
243 | ||
ddb8a639 | 244 | FOREACH_ELEMENT(local_in_addr, local_in_addrs) { |
9f1a1f20 YW |
245 | bool all_localhost, all_local_address; |
246 | EtcHostsItemByAddress *item; | |
16a6bc5a | 247 | const char *name; |
9ca875e8 | 248 | |
ddb8a639 | 249 | item = hashmap_get(hosts->by_address, local_in_addr); |
9ca875e8 LP |
250 | if (!item) |
251 | continue; | |
252 | ||
253 | /* Check whether all hostnames the loopback address points to are localhost ones */ | |
254 | all_localhost = true; | |
16a6bc5a YW |
255 | SET_FOREACH(name, item->names) |
256 | if (!is_localhost(name)) { | |
9ca875e8 LP |
257 | all_localhost = false; |
258 | break; | |
259 | } | |
260 | ||
261 | if (!all_localhost) /* Not all names are localhost, hence keep the entries for this address. */ | |
262 | continue; | |
263 | ||
264 | /* Now check if the names listed for this address actually all point back just to this | |
265 | * address (or the other loopback address). If not, let's stay away from this too. */ | |
9f1a1f20 | 266 | all_local_address = true; |
16a6bc5a | 267 | SET_FOREACH(name, item->names) { |
9ca875e8 | 268 | EtcHostsItemByName *n; |
16a6bc5a | 269 | struct in_addr_data *a; |
9ca875e8 | 270 | |
16a6bc5a | 271 | n = hashmap_get(hosts->by_name, name); |
9ca875e8 LP |
272 | if (!n) /* No reverse entry? Then almost certainly the entry already got deleted from |
273 | * the previous iteration of this loop, i.e. via the other protocol */ | |
274 | break; | |
275 | ||
276 | /* Now check if the addresses of this item are all localhost addresses */ | |
16a6bc5a YW |
277 | SET_FOREACH(a, n->addresses) |
278 | if (!in_addr_is_localhost(a->family, &a->address)) { | |
9ca875e8 LP |
279 | all_local_address = false; |
280 | break; | |
281 | } | |
282 | ||
9f1a1f20 | 283 | if (!all_local_address) |
9ca875e8 | 284 | break; |
9ca875e8 LP |
285 | } |
286 | ||
9f1a1f20 | 287 | if (!all_local_address) |
9ca875e8 LP |
288 | continue; |
289 | ||
16a6bc5a YW |
290 | SET_FOREACH(name, item->names) |
291 | etc_hosts_item_by_name_free(hashmap_remove(hosts->by_name, name)); | |
9ca875e8 | 292 | |
ddb8a639 | 293 | assert_se(hashmap_remove(hosts->by_address, local_in_addr) == item); |
133eedad | 294 | etc_hosts_item_by_address_free(item); |
9ca875e8 LP |
295 | } |
296 | } | |
297 | ||
78fc21a1 | 298 | int etc_hosts_parse(EtcHosts *hosts, FILE *f) { |
0f7bdf35 | 299 | _cleanup_(etc_hosts_clear) EtcHosts t = {}; |
37b7cc8d ZJS |
300 | unsigned nr = 0; |
301 | int r; | |
302 | ||
cfc28ee2 DT |
303 | assert(hosts); |
304 | ||
7c777a77 LP |
305 | for (;;) { |
306 | _cleanup_free_ char *line = NULL; | |
37b7cc8d ZJS |
307 | char *l; |
308 | ||
7c777a77 LP |
309 | r = read_line(f, LONG_LINE_MAX, &line); |
310 | if (r < 0) | |
311 | return log_error_errno(r, "Failed to read /etc/hosts: %m"); | |
312 | if (r == 0) | |
313 | break; | |
314 | ||
37b7cc8d ZJS |
315 | nr++; |
316 | ||
bd005277 ZJS |
317 | l = strchr(line, '#'); |
318 | if (l) | |
319 | *l = '\0'; | |
320 | ||
37b7cc8d ZJS |
321 | l = strstrip(line); |
322 | if (isempty(l)) | |
323 | continue; | |
37b7cc8d ZJS |
324 | |
325 | r = parse_line(&t, nr, l); | |
326 | if (r < 0) | |
327 | return r; | |
328 | } | |
329 | ||
9ca875e8 LP |
330 | strip_localhost(&t); |
331 | ||
0f7bdf35 | 332 | etc_hosts_clear(hosts); |
088d71f8 | 333 | *hosts = TAKE_STRUCT(t); |
37b7cc8d ZJS |
334 | return 0; |
335 | } | |
336 | ||
452ca091 | 337 | static int manager_etc_hosts_read(Manager *m) { |
dd0bc0f1 | 338 | _cleanup_fclose_ FILE *f = NULL; |
dd0bc0f1 LP |
339 | struct stat st; |
340 | usec_t ts; | |
dd0bc0f1 LP |
341 | int r; |
342 | ||
ba4e0427 | 343 | assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &ts) >= 0); |
dd0bc0f1 LP |
344 | |
345 | /* See if we checked /etc/hosts recently already */ | |
346 | if (m->etc_hosts_last != USEC_INFINITY && m->etc_hosts_last + ETC_HOSTS_RECHECK_USEC > ts) | |
347 | return 0; | |
348 | ||
349 | m->etc_hosts_last = ts; | |
350 | ||
1c248d7f | 351 | if (stat_is_set(&m->etc_hosts_stat)) { |
dd0bc0f1 | 352 | if (stat("/etc/hosts", &st) < 0) { |
37b7cc8d ZJS |
353 | if (errno != ENOENT) |
354 | return log_error_errno(errno, "Failed to stat /etc/hosts: %m"); | |
dd0bc0f1 | 355 | |
37b7cc8d ZJS |
356 | manager_etc_hosts_flush(m); |
357 | return 0; | |
dd0bc0f1 LP |
358 | } |
359 | ||
aa5408e2 | 360 | /* Did the mtime or ino/dev change? If not, there's no point in re-reading the file. */ |
36d892b7 | 361 | if (stat_inode_unmodified(&m->etc_hosts_stat, &st)) |
dd0bc0f1 LP |
362 | return 0; |
363 | } | |
364 | ||
365 | f = fopen("/etc/hosts", "re"); | |
366 | if (!f) { | |
37b7cc8d | 367 | if (errno != ENOENT) |
42ba9974 | 368 | return log_error_errno(errno, "Failed to open %s: %m", "/etc/hosts"); |
dd0bc0f1 | 369 | |
37b7cc8d ZJS |
370 | manager_etc_hosts_flush(m); |
371 | return 0; | |
dd0bc0f1 LP |
372 | } |
373 | ||
374 | /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next | |
375 | * invocation */ | |
376 | r = fstat(fileno(f), &st); | |
377 | if (r < 0) | |
378 | return log_error_errno(errno, "Failed to fstat() /etc/hosts: %m"); | |
379 | ||
37b7cc8d | 380 | r = etc_hosts_parse(&m->etc_hosts, f); |
be19cdf3 | 381 | if (r < 0) |
37b7cc8d | 382 | return r; |
dd0bc0f1 | 383 | |
36d892b7 | 384 | m->etc_hosts_stat = st; |
dd0bc0f1 LP |
385 | m->etc_hosts_last = ts; |
386 | ||
387 | return 1; | |
dd0bc0f1 LP |
388 | } |
389 | ||
0ff8f2a3 DL |
390 | static int answer_add_ptr(DnsAnswer *answer, DnsResourceKey *key, const char *name) { |
391 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
392 | ||
393 | rr = dns_resource_record_new(key); | |
394 | if (!rr) | |
395 | return -ENOMEM; | |
396 | ||
397 | rr->ptr.name = strdup(name); | |
398 | if (!rr->ptr.name) | |
399 | return -ENOMEM; | |
400 | ||
401 | return dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL); | |
402 | } | |
403 | ||
1ddc2f7f DL |
404 | static int answer_add_cname(DnsAnswer *answer, const char *name, const char *cname) { |
405 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
406 | ||
407 | rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, name); | |
408 | if (!rr) | |
409 | return -ENOMEM; | |
410 | ||
411 | rr->cname.name = strdup(cname); | |
412 | if (!rr->cname.name) | |
413 | return -ENOMEM; | |
414 | ||
415 | return dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL); | |
416 | } | |
417 | ||
418 | static int answer_add_addr(DnsAnswer *answer, const char *name, const struct in_addr_data *a) { | |
419 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
420 | int r; | |
421 | ||
422 | r = dns_resource_record_new_address(&rr, a->family, &a->address, name); | |
423 | if (r < 0) | |
424 | return r; | |
425 | ||
426 | return dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL); | |
427 | } | |
428 | ||
0f178863 YW |
429 | static int etc_hosts_lookup_by_address( |
430 | EtcHosts *hosts, | |
431 | DnsQuestion *q, | |
432 | const char *name, | |
433 | const struct in_addr_data *address, | |
434 | DnsAnswer **answer) { | |
435 | ||
436 | DnsResourceKey *t, *found_ptr = NULL; | |
437 | EtcHostsItemByAddress *item; | |
dd0bc0f1 LP |
438 | int r; |
439 | ||
0f178863 | 440 | assert(hosts); |
dd0bc0f1 | 441 | assert(q); |
0f178863 YW |
442 | assert(name); |
443 | assert(address); | |
dd0bc0f1 LP |
444 | assert(answer); |
445 | ||
0f178863 YW |
446 | item = hashmap_get(hosts->by_address, address); |
447 | if (!item) | |
86317087 YW |
448 | return 0; |
449 | ||
0f178863 YW |
450 | /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data |
451 | * we'll only return if the request was for PTR. */ | |
dd0bc0f1 | 452 | |
0f178863 YW |
453 | DNS_QUESTION_FOREACH(t, q) { |
454 | if (!IN_SET(t->type, DNS_TYPE_PTR, DNS_TYPE_ANY)) | |
455 | continue; | |
456 | if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY)) | |
457 | continue; | |
dd0bc0f1 | 458 | |
0f178863 YW |
459 | r = dns_name_equal(dns_resource_key_name(t), name); |
460 | if (r < 0) | |
461 | return r; | |
462 | if (r > 0) { | |
463 | found_ptr = t; | |
464 | break; | |
465 | } | |
466 | } | |
dd0bc0f1 | 467 | |
0f178863 | 468 | if (found_ptr) { |
16a6bc5a YW |
469 | const char *n; |
470 | ||
471 | r = dns_answer_reserve(answer, set_size(item->names)); | |
0f178863 YW |
472 | if (r < 0) |
473 | return r; | |
dd0bc0f1 | 474 | |
0ff8f2a3 DL |
475 | if (item->canonical_name) { |
476 | r = answer_add_ptr(*answer, found_ptr, item->canonical_name); | |
477 | if (r < 0) | |
478 | return r; | |
479 | } | |
dd0bc0f1 | 480 | |
0ff8f2a3 DL |
481 | SET_FOREACH(n, item->names) { |
482 | if (n == item->canonical_name) | |
483 | continue; | |
dd0bc0f1 | 484 | |
0ff8f2a3 | 485 | r = answer_add_ptr(*answer, found_ptr, n); |
dd0bc0f1 LP |
486 | if (r < 0) |
487 | return r; | |
0f178863 YW |
488 | } |
489 | } | |
dd0bc0f1 | 490 | |
0f178863 YW |
491 | return 1; |
492 | } | |
dd0bc0f1 | 493 | |
0f178863 YW |
494 | static int etc_hosts_lookup_by_name( |
495 | EtcHosts *hosts, | |
496 | DnsQuestion *q, | |
497 | const char *name, | |
498 | DnsAnswer **answer) { | |
dd0bc0f1 | 499 | |
40008b83 | 500 | bool question_for_a = false, question_for_aaaa = false; |
16a6bc5a | 501 | const struct in_addr_data *a; |
0f178863 YW |
502 | EtcHostsItemByName *item; |
503 | DnsResourceKey *t; | |
504 | int r; | |
dd0bc0f1 | 505 | |
0f178863 YW |
506 | assert(hosts); |
507 | assert(q); | |
508 | assert(name); | |
509 | assert(answer); | |
dd0bc0f1 | 510 | |
0f178863 YW |
511 | item = hashmap_get(hosts->by_name, name); |
512 | if (item) { | |
16a6bc5a | 513 | r = dns_answer_reserve(answer, set_size(item->addresses)); |
fd373593 ZJS |
514 | if (r < 0) |
515 | return r; | |
516 | } else { | |
517 | /* Check if name was listed with no address. If yes, continue to return an answer. */ | |
0f178863 | 518 | if (!set_contains(hosts->no_address, name)) |
fd373593 ZJS |
519 | return 0; |
520 | } | |
dd0bc0f1 | 521 | |
40008b83 | 522 | /* Determine whether we are looking for A and/or AAAA RRs */ |
dd0bc0f1 LP |
523 | DNS_QUESTION_FOREACH(t, q) { |
524 | if (!IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_ANY)) | |
525 | continue; | |
526 | if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY)) | |
527 | continue; | |
528 | ||
1c02e7ba | 529 | r = dns_name_equal(dns_resource_key_name(t), name); |
dd0bc0f1 LP |
530 | if (r < 0) |
531 | return r; | |
532 | if (r == 0) | |
533 | continue; | |
534 | ||
535 | if (IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_ANY)) | |
40008b83 | 536 | question_for_a = true; |
dd0bc0f1 | 537 | if (IN_SET(t->type, DNS_TYPE_AAAA, DNS_TYPE_ANY)) |
40008b83 | 538 | question_for_aaaa = true; |
dd0bc0f1 | 539 | |
40008b83 LP |
540 | if (question_for_a && question_for_aaaa) |
541 | break; /* We are looking for both, no need to continue loop */ | |
dd0bc0f1 LP |
542 | } |
543 | ||
863b620a | 544 | SET_FOREACH(a, item ? item->addresses : NULL) { |
1ddc2f7f DL |
545 | EtcHostsItemByAddress *item_by_addr; |
546 | const char *canonical_name; | |
dd0bc0f1 | 547 | |
40008b83 LP |
548 | if ((!question_for_a && a->family == AF_INET) || |
549 | (!question_for_aaaa && a->family == AF_INET6)) | |
dd0bc0f1 LP |
550 | continue; |
551 | ||
1ddc2f7f DL |
552 | item_by_addr = hashmap_get(hosts->by_address, a); |
553 | if (item_by_addr && item_by_addr->canonical_name) | |
554 | canonical_name = item_by_addr->canonical_name; | |
555 | else | |
556 | canonical_name = item->name; | |
557 | ||
558 | if (!streq(item->name, canonical_name)) { | |
559 | r = answer_add_cname(*answer, item->name, canonical_name); | |
560 | if (r < 0) | |
561 | return r; | |
562 | } | |
dd0bc0f1 | 563 | |
1ddc2f7f | 564 | r = answer_add_addr(*answer, canonical_name, a); |
dd0bc0f1 LP |
565 | if (r < 0) |
566 | return r; | |
567 | } | |
568 | ||
4b918962 | 569 | return true; /* We consider ourselves authoritative for the whole name, all RR types, not just A/AAAA */ |
dd0bc0f1 | 570 | } |
0f178863 YW |
571 | |
572 | int manager_etc_hosts_lookup(Manager *m, DnsQuestion *q, DnsAnswer **answer) { | |
573 | struct in_addr_data k; | |
574 | const char *name; | |
575 | ||
576 | assert(m); | |
577 | assert(q); | |
578 | assert(answer); | |
579 | ||
580 | if (!m->read_etc_hosts) | |
581 | return 0; | |
582 | ||
583 | (void) manager_etc_hosts_read(m); | |
584 | ||
585 | name = dns_question_first_name(q); | |
586 | if (!name) | |
587 | return 0; | |
588 | ||
589 | if (dns_name_address(name, &k.family, &k.address) > 0) | |
590 | return etc_hosts_lookup_by_address(&m->etc_hosts, q, name, &k, answer); | |
591 | ||
592 | return etc_hosts_lookup_by_name(&m->etc_hosts, q, name, answer); | |
593 | } |