]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <fcntl.h> | |
4 | #include <netinet/in.h> | |
5 | #include <poll.h> | |
6 | #include <sys/ioctl.h> | |
7 | #include <sys/stat.h> | |
8 | #include <sys/types.h> | |
9 | #include <unistd.h> | |
10 | ||
11 | #if HAVE_LIBIDN2 | |
12 | #include <idn2.h> | |
13 | #endif | |
14 | ||
15 | #include "af-list.h" | |
16 | #include "alloc-util.h" | |
17 | #include "bus-util.h" | |
18 | #include "dirent-util.h" | |
19 | #include "dns-domain.h" | |
20 | #include "fd-util.h" | |
21 | #include "fileio.h" | |
22 | #include "hostname-util.h" | |
23 | #include "io-util.h" | |
24 | #include "missing_network.h" | |
25 | #include "netlink-util.h" | |
26 | #include "network-internal.h" | |
27 | #include "ordered-set.h" | |
28 | #include "parse-util.h" | |
29 | #include "random-util.h" | |
30 | #include "resolved-bus.h" | |
31 | #include "resolved-conf.h" | |
32 | #include "resolved-dns-stub.h" | |
33 | #include "resolved-dnssd.h" | |
34 | #include "resolved-etc-hosts.h" | |
35 | #include "resolved-llmnr.h" | |
36 | #include "resolved-manager.h" | |
37 | #include "resolved-mdns.h" | |
38 | #include "resolved-resolv-conf.h" | |
39 | #include "socket-util.h" | |
40 | #include "string-table.h" | |
41 | #include "string-util.h" | |
42 | #include "utf8.h" | |
43 | ||
44 | #define SEND_TIMEOUT_USEC (200 * USEC_PER_MSEC) | |
45 | ||
46 | static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { | |
47 | Manager *m = userdata; | |
48 | uint16_t type; | |
49 | Link *l; | |
50 | int ifindex, r; | |
51 | ||
52 | assert(rtnl); | |
53 | assert(m); | |
54 | assert(mm); | |
55 | ||
56 | r = sd_netlink_message_get_type(mm, &type); | |
57 | if (r < 0) | |
58 | goto fail; | |
59 | ||
60 | r = sd_rtnl_message_link_get_ifindex(mm, &ifindex); | |
61 | if (r < 0) | |
62 | goto fail; | |
63 | ||
64 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
65 | ||
66 | switch (type) { | |
67 | ||
68 | case RTM_NEWLINK:{ | |
69 | bool is_new = !l; | |
70 | ||
71 | if (!l) { | |
72 | r = link_new(m, &l, ifindex); | |
73 | if (r < 0) | |
74 | goto fail; | |
75 | } | |
76 | ||
77 | r = link_process_rtnl(l, mm); | |
78 | if (r < 0) | |
79 | goto fail; | |
80 | ||
81 | r = link_update(l); | |
82 | if (r < 0) | |
83 | goto fail; | |
84 | ||
85 | if (is_new) | |
86 | log_debug("Found new link %i/%s", ifindex, l->ifname); | |
87 | ||
88 | break; | |
89 | } | |
90 | ||
91 | case RTM_DELLINK: | |
92 | if (l) { | |
93 | log_debug("Removing link %i/%s", l->ifindex, l->ifname); | |
94 | link_remove_user(l); | |
95 | link_free(l); | |
96 | } | |
97 | ||
98 | break; | |
99 | } | |
100 | ||
101 | return 0; | |
102 | ||
103 | fail: | |
104 | log_warning_errno(r, "Failed to process RTNL link message: %m"); | |
105 | return 0; | |
106 | } | |
107 | ||
108 | static int manager_process_address(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { | |
109 | Manager *m = userdata; | |
110 | union in_addr_union address; | |
111 | uint16_t type; | |
112 | int r, ifindex, family; | |
113 | LinkAddress *a; | |
114 | Link *l; | |
115 | ||
116 | assert(rtnl); | |
117 | assert(mm); | |
118 | assert(m); | |
119 | ||
120 | r = sd_netlink_message_get_type(mm, &type); | |
121 | if (r < 0) | |
122 | goto fail; | |
123 | ||
124 | r = sd_rtnl_message_addr_get_ifindex(mm, &ifindex); | |
125 | if (r < 0) | |
126 | goto fail; | |
127 | ||
128 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
129 | if (!l) | |
130 | return 0; | |
131 | ||
132 | r = sd_rtnl_message_addr_get_family(mm, &family); | |
133 | if (r < 0) | |
134 | goto fail; | |
135 | ||
136 | switch (family) { | |
137 | ||
138 | case AF_INET: | |
139 | r = sd_netlink_message_read_in_addr(mm, IFA_LOCAL, &address.in); | |
140 | if (r < 0) { | |
141 | r = sd_netlink_message_read_in_addr(mm, IFA_ADDRESS, &address.in); | |
142 | if (r < 0) | |
143 | goto fail; | |
144 | } | |
145 | ||
146 | break; | |
147 | ||
148 | case AF_INET6: | |
149 | r = sd_netlink_message_read_in6_addr(mm, IFA_LOCAL, &address.in6); | |
150 | if (r < 0) { | |
151 | r = sd_netlink_message_read_in6_addr(mm, IFA_ADDRESS, &address.in6); | |
152 | if (r < 0) | |
153 | goto fail; | |
154 | } | |
155 | ||
156 | break; | |
157 | ||
158 | default: | |
159 | return 0; | |
160 | } | |
161 | ||
162 | a = link_find_address(l, family, &address); | |
163 | ||
164 | switch (type) { | |
165 | ||
166 | case RTM_NEWADDR: | |
167 | ||
168 | if (!a) { | |
169 | r = link_address_new(l, &a, family, &address); | |
170 | if (r < 0) | |
171 | return r; | |
172 | } | |
173 | ||
174 | r = link_address_update_rtnl(a, mm); | |
175 | if (r < 0) | |
176 | return r; | |
177 | ||
178 | break; | |
179 | ||
180 | case RTM_DELADDR: | |
181 | link_address_free(a); | |
182 | break; | |
183 | } | |
184 | ||
185 | return 0; | |
186 | ||
187 | fail: | |
188 | log_warning_errno(r, "Failed to process RTNL address message: %m"); | |
189 | return 0; | |
190 | } | |
191 | ||
192 | static int manager_rtnl_listen(Manager *m) { | |
193 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
194 | sd_netlink_message *i; | |
195 | int r; | |
196 | ||
197 | assert(m); | |
198 | ||
199 | /* First, subscribe to interfaces coming and going */ | |
200 | r = sd_netlink_open(&m->rtnl); | |
201 | if (r < 0) | |
202 | return r; | |
203 | ||
204 | r = sd_netlink_attach_event(m->rtnl, m->event, SD_EVENT_PRIORITY_IMPORTANT); | |
205 | if (r < 0) | |
206 | return r; | |
207 | ||
208 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, manager_process_link, NULL, m, "resolve-NEWLINK"); | |
209 | if (r < 0) | |
210 | return r; | |
211 | ||
212 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, manager_process_link, NULL, m, "resolve-DELLINK"); | |
213 | if (r < 0) | |
214 | return r; | |
215 | ||
216 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, manager_process_address, NULL, m, "resolve-NEWADDR"); | |
217 | if (r < 0) | |
218 | return r; | |
219 | ||
220 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, manager_process_address, NULL, m, "resolve-DELADDR"); | |
221 | if (r < 0) | |
222 | return r; | |
223 | ||
224 | /* Then, enumerate all links */ | |
225 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); | |
226 | if (r < 0) | |
227 | return r; | |
228 | ||
229 | r = sd_netlink_message_request_dump(req, true); | |
230 | if (r < 0) | |
231 | return r; | |
232 | ||
233 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
234 | if (r < 0) | |
235 | return r; | |
236 | ||
237 | for (i = reply; i; i = sd_netlink_message_next(i)) { | |
238 | r = manager_process_link(m->rtnl, i, m); | |
239 | if (r < 0) | |
240 | return r; | |
241 | } | |
242 | ||
243 | req = sd_netlink_message_unref(req); | |
244 | reply = sd_netlink_message_unref(reply); | |
245 | ||
246 | /* Finally, enumerate all addresses, too */ | |
247 | r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC); | |
248 | if (r < 0) | |
249 | return r; | |
250 | ||
251 | r = sd_netlink_message_request_dump(req, true); | |
252 | if (r < 0) | |
253 | return r; | |
254 | ||
255 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
256 | if (r < 0) | |
257 | return r; | |
258 | ||
259 | for (i = reply; i; i = sd_netlink_message_next(i)) { | |
260 | r = manager_process_address(m->rtnl, i, m); | |
261 | if (r < 0) | |
262 | return r; | |
263 | } | |
264 | ||
265 | return r; | |
266 | } | |
267 | ||
268 | static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
269 | Manager *m = userdata; | |
270 | Iterator i; | |
271 | Link *l; | |
272 | int r; | |
273 | ||
274 | assert(m); | |
275 | ||
276 | sd_network_monitor_flush(m->network_monitor); | |
277 | ||
278 | HASHMAP_FOREACH(l, m->links, i) { | |
279 | r = link_update(l); | |
280 | if (r < 0) | |
281 | log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex); | |
282 | } | |
283 | ||
284 | (void) manager_write_resolv_conf(m); | |
285 | (void) manager_send_changed(m, "DNS"); | |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
290 | static int manager_network_monitor_listen(Manager *m) { | |
291 | int r, fd, events; | |
292 | ||
293 | assert(m); | |
294 | ||
295 | r = sd_network_monitor_new(&m->network_monitor, NULL); | |
296 | if (r < 0) | |
297 | return r; | |
298 | ||
299 | fd = sd_network_monitor_get_fd(m->network_monitor); | |
300 | if (fd < 0) | |
301 | return fd; | |
302 | ||
303 | events = sd_network_monitor_get_events(m->network_monitor); | |
304 | if (events < 0) | |
305 | return events; | |
306 | ||
307 | r = sd_event_add_io(m->event, &m->network_event_source, fd, events, &on_network_event, m); | |
308 | if (r < 0) | |
309 | return r; | |
310 | ||
311 | r = sd_event_source_set_priority(m->network_event_source, SD_EVENT_PRIORITY_IMPORTANT+5); | |
312 | if (r < 0) | |
313 | return r; | |
314 | ||
315 | (void) sd_event_source_set_description(m->network_event_source, "network-monitor"); | |
316 | ||
317 | return 0; | |
318 | } | |
319 | ||
320 | static int determine_hostname(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) { | |
321 | _cleanup_free_ char *h = NULL, *n = NULL; | |
322 | #if HAVE_LIBIDN2 | |
323 | _cleanup_free_ char *utf8 = NULL; | |
324 | #elif HAVE_LIBIDN | |
325 | int k; | |
326 | #endif | |
327 | char label[DNS_LABEL_MAX]; | |
328 | const char *p, *decoded; | |
329 | int r; | |
330 | ||
331 | assert(full_hostname); | |
332 | assert(llmnr_hostname); | |
333 | assert(mdns_hostname); | |
334 | ||
335 | /* Extract and normalize the first label of the locally configured hostname, and check it's not "localhost". */ | |
336 | ||
337 | r = gethostname_strict(&h); | |
338 | if (r < 0) | |
339 | return log_debug_errno(r, "Can't determine system hostname: %m"); | |
340 | ||
341 | p = h; | |
342 | r = dns_label_unescape(&p, label, sizeof label, 0); | |
343 | if (r < 0) | |
344 | return log_error_errno(r, "Failed to unescape host name: %m"); | |
345 | if (r == 0) | |
346 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
347 | "Couldn't find a single label in hostname."); | |
348 | ||
349 | #if HAVE_LIBIDN2 | |
350 | r = idn2_to_unicode_8z8z(label, &utf8, 0); | |
351 | if (r != IDN2_OK) | |
352 | return log_error("Failed to undo IDNA: %s", idn2_strerror(r)); | |
353 | assert(utf8_is_valid(utf8)); | |
354 | ||
355 | r = strlen(utf8); | |
356 | decoded = utf8; | |
357 | #elif HAVE_LIBIDN | |
358 | k = dns_label_undo_idna(label, r, label, sizeof label); | |
359 | if (k < 0) | |
360 | return log_error_errno(k, "Failed to undo IDNA: %m"); | |
361 | if (k > 0) | |
362 | r = k; | |
363 | ||
364 | if (!utf8_is_valid(label)) | |
365 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
366 | "System hostname is not UTF-8 clean."); | |
367 | decoded = label; | |
368 | #else | |
369 | decoded = label; /* no decoding */ | |
370 | #endif | |
371 | ||
372 | r = dns_label_escape_new(decoded, r, &n); | |
373 | if (r < 0) | |
374 | return log_error_errno(r, "Failed to escape host name: %m"); | |
375 | ||
376 | if (is_localhost(n)) | |
377 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
378 | "System hostname is 'localhost', ignoring."); | |
379 | ||
380 | r = dns_name_concat(n, "local", 0, mdns_hostname); | |
381 | if (r < 0) | |
382 | return log_error_errno(r, "Failed to determine mDNS hostname: %m"); | |
383 | ||
384 | *llmnr_hostname = TAKE_PTR(n); | |
385 | *full_hostname = TAKE_PTR(h); | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
390 | static const char *fallback_hostname(void) { | |
391 | ||
392 | /* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it to be | |
393 | * "localhost" even if that's the compiled in hostname. In this case, let's revert to "linux" instead. */ | |
394 | ||
395 | if (is_localhost(FALLBACK_HOSTNAME)) | |
396 | return "linux"; | |
397 | ||
398 | return FALLBACK_HOSTNAME; | |
399 | } | |
400 | ||
401 | static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) { | |
402 | _cleanup_free_ char *n = NULL, *m = NULL; | |
403 | char label[DNS_LABEL_MAX], *h; | |
404 | const char *p; | |
405 | int r; | |
406 | ||
407 | assert(full_hostname); | |
408 | assert(llmnr_hostname); | |
409 | assert(mdns_hostname); | |
410 | ||
411 | p = fallback_hostname(); | |
412 | r = dns_label_unescape(&p, label, sizeof label, 0); | |
413 | if (r < 0) | |
414 | return log_error_errno(r, "Failed to unescape fallback host name: %m"); | |
415 | ||
416 | assert(r > 0); /* The fallback hostname must have at least one label */ | |
417 | ||
418 | r = dns_label_escape_new(label, r, &n); | |
419 | if (r < 0) | |
420 | return log_error_errno(r, "Failed to escape fallback hostname: %m"); | |
421 | ||
422 | r = dns_name_concat(n, "local", 0, &m); | |
423 | if (r < 0) | |
424 | return log_error_errno(r, "Failed to concatenate mDNS hostname: %m"); | |
425 | ||
426 | h = strdup(fallback_hostname()); | |
427 | if (!h) | |
428 | return log_oom(); | |
429 | ||
430 | *llmnr_hostname = TAKE_PTR(n); | |
431 | *mdns_hostname = TAKE_PTR(m); | |
432 | ||
433 | *full_hostname = h; | |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
438 | static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) { | |
439 | _cleanup_free_ char *full_hostname = NULL, *llmnr_hostname = NULL, *mdns_hostname = NULL; | |
440 | Manager *m = userdata; | |
441 | int r; | |
442 | ||
443 | assert(m); | |
444 | ||
445 | r = determine_hostname(&full_hostname, &llmnr_hostname, &mdns_hostname); | |
446 | if (r < 0) | |
447 | return 0; /* ignore invalid hostnames */ | |
448 | ||
449 | if (streq(full_hostname, m->full_hostname) && | |
450 | streq(llmnr_hostname, m->llmnr_hostname) && | |
451 | streq(mdns_hostname, m->mdns_hostname)) | |
452 | return 0; | |
453 | ||
454 | log_info("System hostname changed to '%s'.", full_hostname); | |
455 | ||
456 | free_and_replace(m->full_hostname, full_hostname); | |
457 | free_and_replace(m->llmnr_hostname, llmnr_hostname); | |
458 | free_and_replace(m->mdns_hostname, mdns_hostname); | |
459 | ||
460 | manager_refresh_rrs(m); | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | static int manager_watch_hostname(Manager *m) { | |
466 | int r; | |
467 | ||
468 | assert(m); | |
469 | ||
470 | m->hostname_fd = open("/proc/sys/kernel/hostname", | |
471 | O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
472 | if (m->hostname_fd < 0) { | |
473 | log_warning_errno(errno, "Failed to watch hostname: %m"); | |
474 | return 0; | |
475 | } | |
476 | ||
477 | r = sd_event_add_io(m->event, &m->hostname_event_source, m->hostname_fd, 0, on_hostname_change, m); | |
478 | if (r < 0) { | |
479 | if (r == -EPERM) | |
480 | /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */ | |
481 | m->hostname_fd = safe_close(m->hostname_fd); | |
482 | else | |
483 | return log_error_errno(r, "Failed to add hostname event source: %m"); | |
484 | } | |
485 | ||
486 | (void) sd_event_source_set_description(m->hostname_event_source, "hostname"); | |
487 | ||
488 | r = determine_hostname(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname); | |
489 | if (r < 0) { | |
490 | log_info("Defaulting to hostname '%s'.", fallback_hostname()); | |
491 | ||
492 | r = make_fallback_hostnames(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname); | |
493 | if (r < 0) | |
494 | return r; | |
495 | } else | |
496 | log_info("Using system hostname '%s'.", m->full_hostname); | |
497 | ||
498 | return 0; | |
499 | } | |
500 | ||
501 | static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
502 | _cleanup_free_ char *buffer = NULL; | |
503 | _cleanup_fclose_ FILE *f = NULL; | |
504 | Manager *m = userdata; | |
505 | DnsServer *server; | |
506 | size_t size = 0; | |
507 | DnsScope *scope; | |
508 | Iterator i; | |
509 | Link *l; | |
510 | ||
511 | assert(s); | |
512 | assert(si); | |
513 | assert(m); | |
514 | ||
515 | f = open_memstream_unlocked(&buffer, &size); | |
516 | if (!f) | |
517 | return log_oom(); | |
518 | ||
519 | LIST_FOREACH(scopes, scope, m->dns_scopes) | |
520 | dns_scope_dump(scope, f); | |
521 | ||
522 | LIST_FOREACH(servers, server, m->dns_servers) | |
523 | dns_server_dump(server, f); | |
524 | LIST_FOREACH(servers, server, m->fallback_dns_servers) | |
525 | dns_server_dump(server, f); | |
526 | HASHMAP_FOREACH(l, m->links, i) | |
527 | LIST_FOREACH(servers, server, l->dns_servers) | |
528 | dns_server_dump(server, f); | |
529 | ||
530 | if (fflush_and_check(f) < 0) | |
531 | return log_oom(); | |
532 | ||
533 | log_dump(LOG_INFO, buffer); | |
534 | return 0; | |
535 | } | |
536 | ||
537 | static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
538 | Manager *m = userdata; | |
539 | ||
540 | assert(s); | |
541 | assert(si); | |
542 | assert(m); | |
543 | ||
544 | manager_flush_caches(m); | |
545 | ||
546 | return 0; | |
547 | } | |
548 | ||
549 | static int manager_sigrtmin1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
550 | Manager *m = userdata; | |
551 | ||
552 | assert(s); | |
553 | assert(si); | |
554 | assert(m); | |
555 | ||
556 | manager_reset_server_features(m); | |
557 | return 0; | |
558 | } | |
559 | ||
560 | int manager_new(Manager **ret) { | |
561 | _cleanup_(manager_freep) Manager *m = NULL; | |
562 | int r; | |
563 | ||
564 | assert(ret); | |
565 | ||
566 | m = new(Manager, 1); | |
567 | if (!m) | |
568 | return -ENOMEM; | |
569 | ||
570 | *m = (Manager) { | |
571 | .llmnr_ipv4_udp_fd = -1, | |
572 | .llmnr_ipv6_udp_fd = -1, | |
573 | .llmnr_ipv4_tcp_fd = -1, | |
574 | .llmnr_ipv6_tcp_fd = -1, | |
575 | .mdns_ipv4_fd = -1, | |
576 | .mdns_ipv6_fd = -1, | |
577 | .dns_stub_udp_fd = -1, | |
578 | .dns_stub_tcp_fd = -1, | |
579 | .hostname_fd = -1, | |
580 | ||
581 | .llmnr_support = RESOLVE_SUPPORT_YES, | |
582 | .mdns_support = RESOLVE_SUPPORT_YES, | |
583 | .dnssec_mode = DEFAULT_DNSSEC_MODE, | |
584 | .dns_over_tls_mode = DEFAULT_DNS_OVER_TLS_MODE, | |
585 | .enable_cache = DNS_CACHE_MODE_YES, | |
586 | .dns_stub_listener_mode = DNS_STUB_LISTENER_YES, | |
587 | .read_resolv_conf = true, | |
588 | .need_builtin_fallbacks = true, | |
589 | .etc_hosts_last = USEC_INFINITY, | |
590 | .etc_hosts_mtime = USEC_INFINITY, | |
591 | .read_etc_hosts = true, | |
592 | }; | |
593 | ||
594 | r = dns_trust_anchor_load(&m->trust_anchor); | |
595 | if (r < 0) | |
596 | return r; | |
597 | ||
598 | r = manager_parse_config_file(m); | |
599 | if (r < 0) | |
600 | log_warning_errno(r, "Failed to parse configuration file: %m"); | |
601 | ||
602 | #if ENABLE_DNS_OVER_TLS | |
603 | r = dnstls_manager_init(m); | |
604 | if (r < 0) | |
605 | return r; | |
606 | #endif | |
607 | ||
608 | r = sd_event_default(&m->event); | |
609 | if (r < 0) | |
610 | return r; | |
611 | ||
612 | (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); | |
613 | (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); | |
614 | ||
615 | (void) sd_event_set_watchdog(m->event, true); | |
616 | ||
617 | r = manager_watch_hostname(m); | |
618 | if (r < 0) | |
619 | return r; | |
620 | ||
621 | r = dnssd_load(m); | |
622 | if (r < 0) | |
623 | log_warning_errno(r, "Failed to load DNS-SD configuration files: %m"); | |
624 | ||
625 | r = dns_scope_new(m, &m->unicast_scope, NULL, DNS_PROTOCOL_DNS, AF_UNSPEC); | |
626 | if (r < 0) | |
627 | return r; | |
628 | ||
629 | r = manager_network_monitor_listen(m); | |
630 | if (r < 0) | |
631 | return r; | |
632 | ||
633 | r = manager_rtnl_listen(m); | |
634 | if (r < 0) | |
635 | return r; | |
636 | ||
637 | r = manager_connect_bus(m); | |
638 | if (r < 0) | |
639 | return r; | |
640 | ||
641 | (void) sd_event_add_signal(m->event, &m->sigusr1_event_source, SIGUSR1, manager_sigusr1, m); | |
642 | (void) sd_event_add_signal(m->event, &m->sigusr2_event_source, SIGUSR2, manager_sigusr2, m); | |
643 | (void) sd_event_add_signal(m->event, &m->sigrtmin1_event_source, SIGRTMIN+1, manager_sigrtmin1, m); | |
644 | ||
645 | manager_cleanup_saved_user(m); | |
646 | ||
647 | *ret = TAKE_PTR(m); | |
648 | ||
649 | return 0; | |
650 | } | |
651 | ||
652 | int manager_start(Manager *m) { | |
653 | int r; | |
654 | ||
655 | assert(m); | |
656 | ||
657 | r = manager_dns_stub_start(m); | |
658 | if (r < 0) | |
659 | return r; | |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
664 | Manager *manager_free(Manager *m) { | |
665 | Link *l; | |
666 | DnssdService *s; | |
667 | ||
668 | if (!m) | |
669 | return NULL; | |
670 | ||
671 | dns_server_unlink_all(m->dns_servers); | |
672 | dns_server_unlink_all(m->fallback_dns_servers); | |
673 | dns_search_domain_unlink_all(m->search_domains); | |
674 | ||
675 | while ((l = hashmap_first(m->links))) | |
676 | link_free(l); | |
677 | ||
678 | while (m->dns_queries) | |
679 | dns_query_free(m->dns_queries); | |
680 | ||
681 | dns_scope_free(m->unicast_scope); | |
682 | ||
683 | /* At this point only orphaned streams should remain. All others should have been freed already by their | |
684 | * owners */ | |
685 | while (m->dns_streams) | |
686 | dns_stream_unref(m->dns_streams); | |
687 | ||
688 | #if ENABLE_DNS_OVER_TLS | |
689 | dnstls_manager_free(m); | |
690 | #endif | |
691 | ||
692 | hashmap_free(m->links); | |
693 | hashmap_free(m->dns_transactions); | |
694 | ||
695 | sd_event_source_unref(m->network_event_source); | |
696 | sd_network_monitor_unref(m->network_monitor); | |
697 | ||
698 | sd_netlink_unref(m->rtnl); | |
699 | sd_event_source_unref(m->rtnl_event_source); | |
700 | ||
701 | manager_llmnr_stop(m); | |
702 | manager_mdns_stop(m); | |
703 | manager_dns_stub_stop(m); | |
704 | ||
705 | bus_verify_polkit_async_registry_free(m->polkit_registry); | |
706 | ||
707 | sd_bus_flush_close_unref(m->bus); | |
708 | ||
709 | sd_event_source_unref(m->sigusr1_event_source); | |
710 | sd_event_source_unref(m->sigusr2_event_source); | |
711 | sd_event_source_unref(m->sigrtmin1_event_source); | |
712 | ||
713 | sd_event_unref(m->event); | |
714 | ||
715 | dns_resource_key_unref(m->llmnr_host_ipv4_key); | |
716 | dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
717 | dns_resource_key_unref(m->mdns_host_ipv4_key); | |
718 | dns_resource_key_unref(m->mdns_host_ipv6_key); | |
719 | ||
720 | sd_event_source_unref(m->hostname_event_source); | |
721 | safe_close(m->hostname_fd); | |
722 | ||
723 | free(m->full_hostname); | |
724 | free(m->llmnr_hostname); | |
725 | free(m->mdns_hostname); | |
726 | ||
727 | while ((s = hashmap_first(m->dnssd_services))) | |
728 | dnssd_service_free(s); | |
729 | hashmap_free(m->dnssd_services); | |
730 | ||
731 | dns_trust_anchor_flush(&m->trust_anchor); | |
732 | manager_etc_hosts_flush(m); | |
733 | ||
734 | return mfree(m); | |
735 | } | |
736 | ||
737 | int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) { | |
738 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; | |
739 | union { | |
740 | struct cmsghdr header; /* For alignment */ | |
741 | uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo)) | |
742 | + CMSG_SPACE(int) /* ttl/hoplimit */ | |
743 | + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */]; | |
744 | } control; | |
745 | union sockaddr_union sa; | |
746 | struct iovec iov; | |
747 | struct msghdr mh = { | |
748 | .msg_name = &sa.sa, | |
749 | .msg_namelen = sizeof(sa), | |
750 | .msg_iov = &iov, | |
751 | .msg_iovlen = 1, | |
752 | .msg_control = &control, | |
753 | .msg_controllen = sizeof(control), | |
754 | }; | |
755 | struct cmsghdr *cmsg; | |
756 | ssize_t ms, l; | |
757 | int r; | |
758 | ||
759 | assert(m); | |
760 | assert(fd >= 0); | |
761 | assert(ret); | |
762 | ||
763 | ms = next_datagram_size_fd(fd); | |
764 | if (ms < 0) | |
765 | return ms; | |
766 | ||
767 | r = dns_packet_new(&p, protocol, ms, DNS_PACKET_SIZE_MAX); | |
768 | if (r < 0) | |
769 | return r; | |
770 | ||
771 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->allocated); | |
772 | ||
773 | l = recvmsg(fd, &mh, 0); | |
774 | if (l < 0) { | |
775 | if (IN_SET(errno, EAGAIN, EINTR)) | |
776 | return 0; | |
777 | ||
778 | return -errno; | |
779 | } | |
780 | if (l == 0) | |
781 | return 0; | |
782 | ||
783 | assert(!(mh.msg_flags & MSG_CTRUNC)); | |
784 | assert(!(mh.msg_flags & MSG_TRUNC)); | |
785 | ||
786 | p->size = (size_t) l; | |
787 | ||
788 | p->family = sa.sa.sa_family; | |
789 | p->ipproto = IPPROTO_UDP; | |
790 | if (p->family == AF_INET) { | |
791 | p->sender.in = sa.in.sin_addr; | |
792 | p->sender_port = be16toh(sa.in.sin_port); | |
793 | } else if (p->family == AF_INET6) { | |
794 | p->sender.in6 = sa.in6.sin6_addr; | |
795 | p->sender_port = be16toh(sa.in6.sin6_port); | |
796 | p->ifindex = sa.in6.sin6_scope_id; | |
797 | } else | |
798 | return -EAFNOSUPPORT; | |
799 | ||
800 | CMSG_FOREACH(cmsg, &mh) { | |
801 | ||
802 | if (cmsg->cmsg_level == IPPROTO_IPV6) { | |
803 | assert(p->family == AF_INET6); | |
804 | ||
805 | switch (cmsg->cmsg_type) { | |
806 | ||
807 | case IPV6_PKTINFO: { | |
808 | struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg); | |
809 | ||
810 | if (p->ifindex <= 0) | |
811 | p->ifindex = i->ipi6_ifindex; | |
812 | ||
813 | p->destination.in6 = i->ipi6_addr; | |
814 | break; | |
815 | } | |
816 | ||
817 | case IPV6_HOPLIMIT: | |
818 | p->ttl = *(int *) CMSG_DATA(cmsg); | |
819 | break; | |
820 | ||
821 | } | |
822 | } else if (cmsg->cmsg_level == IPPROTO_IP) { | |
823 | assert(p->family == AF_INET); | |
824 | ||
825 | switch (cmsg->cmsg_type) { | |
826 | ||
827 | case IP_PKTINFO: { | |
828 | struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg); | |
829 | ||
830 | if (p->ifindex <= 0) | |
831 | p->ifindex = i->ipi_ifindex; | |
832 | ||
833 | p->destination.in = i->ipi_addr; | |
834 | break; | |
835 | } | |
836 | ||
837 | case IP_TTL: | |
838 | p->ttl = *(int *) CMSG_DATA(cmsg); | |
839 | break; | |
840 | } | |
841 | } | |
842 | } | |
843 | ||
844 | /* The Linux kernel sets the interface index to the loopback | |
845 | * device if the packet came from the local host since it | |
846 | * avoids the routing table in such a case. Let's unset the | |
847 | * interface index in such a case. */ | |
848 | if (p->ifindex == LOOPBACK_IFINDEX) | |
849 | p->ifindex = 0; | |
850 | ||
851 | if (protocol != DNS_PROTOCOL_DNS) { | |
852 | /* If we don't know the interface index still, we look for the | |
853 | * first local interface with a matching address. Yuck! */ | |
854 | if (p->ifindex <= 0) | |
855 | p->ifindex = manager_find_ifindex(m, p->family, &p->destination); | |
856 | } | |
857 | ||
858 | *ret = TAKE_PTR(p); | |
859 | ||
860 | return 1; | |
861 | } | |
862 | ||
863 | static int sendmsg_loop(int fd, struct msghdr *mh, int flags) { | |
864 | int r; | |
865 | ||
866 | assert(fd >= 0); | |
867 | assert(mh); | |
868 | ||
869 | for (;;) { | |
870 | if (sendmsg(fd, mh, flags) >= 0) | |
871 | return 0; | |
872 | ||
873 | if (errno == EINTR) | |
874 | continue; | |
875 | ||
876 | if (errno != EAGAIN) | |
877 | return -errno; | |
878 | ||
879 | r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC); | |
880 | if (r < 0) | |
881 | return r; | |
882 | if (r == 0) | |
883 | return -ETIMEDOUT; | |
884 | } | |
885 | } | |
886 | ||
887 | static int write_loop(int fd, void *message, size_t length) { | |
888 | int r; | |
889 | ||
890 | assert(fd >= 0); | |
891 | assert(message); | |
892 | ||
893 | for (;;) { | |
894 | if (write(fd, message, length) >= 0) | |
895 | return 0; | |
896 | ||
897 | if (errno == EINTR) | |
898 | continue; | |
899 | ||
900 | if (errno != EAGAIN) | |
901 | return -errno; | |
902 | ||
903 | r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC); | |
904 | if (r < 0) | |
905 | return r; | |
906 | if (r == 0) | |
907 | return -ETIMEDOUT; | |
908 | } | |
909 | } | |
910 | ||
911 | int manager_write(Manager *m, int fd, DnsPacket *p) { | |
912 | int r; | |
913 | ||
914 | log_debug("Sending %s packet with id %" PRIu16 ".", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p)); | |
915 | ||
916 | r = write_loop(fd, DNS_PACKET_DATA(p), p->size); | |
917 | if (r < 0) | |
918 | return r; | |
919 | ||
920 | return 0; | |
921 | } | |
922 | ||
923 | static int manager_ipv4_send( | |
924 | Manager *m, | |
925 | int fd, | |
926 | int ifindex, | |
927 | const struct in_addr *destination, | |
928 | uint16_t port, | |
929 | const struct in_addr *source, | |
930 | DnsPacket *p) { | |
931 | union { | |
932 | struct cmsghdr header; /* For alignment */ | |
933 | uint8_t buffer[CMSG_SPACE(sizeof(struct in_pktinfo))]; | |
934 | } control = {}; | |
935 | union sockaddr_union sa; | |
936 | struct iovec iov; | |
937 | struct msghdr mh = { | |
938 | .msg_iov = &iov, | |
939 | .msg_iovlen = 1, | |
940 | .msg_name = &sa.sa, | |
941 | .msg_namelen = sizeof(sa.in), | |
942 | }; | |
943 | ||
944 | assert(m); | |
945 | assert(fd >= 0); | |
946 | assert(destination); | |
947 | assert(port > 0); | |
948 | assert(p); | |
949 | ||
950 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size); | |
951 | ||
952 | sa = (union sockaddr_union) { | |
953 | .in.sin_family = AF_INET, | |
954 | .in.sin_addr = *destination, | |
955 | .in.sin_port = htobe16(port), | |
956 | }; | |
957 | ||
958 | if (ifindex > 0) { | |
959 | struct cmsghdr *cmsg; | |
960 | struct in_pktinfo *pi; | |
961 | ||
962 | mh.msg_control = &control; | |
963 | mh.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)); | |
964 | ||
965 | cmsg = CMSG_FIRSTHDR(&mh); | |
966 | cmsg->cmsg_len = mh.msg_controllen; | |
967 | cmsg->cmsg_level = IPPROTO_IP; | |
968 | cmsg->cmsg_type = IP_PKTINFO; | |
969 | ||
970 | pi = (struct in_pktinfo*) CMSG_DATA(cmsg); | |
971 | pi->ipi_ifindex = ifindex; | |
972 | ||
973 | if (source) | |
974 | pi->ipi_spec_dst = *source; | |
975 | } | |
976 | ||
977 | return sendmsg_loop(fd, &mh, 0); | |
978 | } | |
979 | ||
980 | static int manager_ipv6_send( | |
981 | Manager *m, | |
982 | int fd, | |
983 | int ifindex, | |
984 | const struct in6_addr *destination, | |
985 | uint16_t port, | |
986 | const struct in6_addr *source, | |
987 | DnsPacket *p) { | |
988 | ||
989 | union { | |
990 | struct cmsghdr header; /* For alignment */ | |
991 | uint8_t buffer[CMSG_SPACE(sizeof(struct in6_pktinfo))]; | |
992 | } control = {}; | |
993 | union sockaddr_union sa; | |
994 | struct iovec iov; | |
995 | struct msghdr mh = { | |
996 | .msg_iov = &iov, | |
997 | .msg_iovlen = 1, | |
998 | .msg_name = &sa.sa, | |
999 | .msg_namelen = sizeof(sa.in6), | |
1000 | }; | |
1001 | ||
1002 | assert(m); | |
1003 | assert(fd >= 0); | |
1004 | assert(destination); | |
1005 | assert(port > 0); | |
1006 | assert(p); | |
1007 | ||
1008 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size); | |
1009 | ||
1010 | sa = (union sockaddr_union) { | |
1011 | .in6.sin6_family = AF_INET6, | |
1012 | .in6.sin6_addr = *destination, | |
1013 | .in6.sin6_port = htobe16(port), | |
1014 | .in6.sin6_scope_id = ifindex, | |
1015 | }; | |
1016 | ||
1017 | if (ifindex > 0) { | |
1018 | struct cmsghdr *cmsg; | |
1019 | struct in6_pktinfo *pi; | |
1020 | ||
1021 | mh.msg_control = &control; | |
1022 | mh.msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo)); | |
1023 | ||
1024 | cmsg = CMSG_FIRSTHDR(&mh); | |
1025 | cmsg->cmsg_len = mh.msg_controllen; | |
1026 | cmsg->cmsg_level = IPPROTO_IPV6; | |
1027 | cmsg->cmsg_type = IPV6_PKTINFO; | |
1028 | ||
1029 | pi = (struct in6_pktinfo*) CMSG_DATA(cmsg); | |
1030 | pi->ipi6_ifindex = ifindex; | |
1031 | ||
1032 | if (source) | |
1033 | pi->ipi6_addr = *source; | |
1034 | } | |
1035 | ||
1036 | return sendmsg_loop(fd, &mh, 0); | |
1037 | } | |
1038 | ||
1039 | int manager_send( | |
1040 | Manager *m, | |
1041 | int fd, | |
1042 | int ifindex, | |
1043 | int family, | |
1044 | const union in_addr_union *destination, | |
1045 | uint16_t port, | |
1046 | const union in_addr_union *source, | |
1047 | DnsPacket *p) { | |
1048 | ||
1049 | assert(m); | |
1050 | assert(fd >= 0); | |
1051 | assert(destination); | |
1052 | assert(port > 0); | |
1053 | assert(p); | |
1054 | ||
1055 | log_debug("Sending %s packet with id %" PRIu16 " on interface %i/%s.", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p), ifindex, af_to_name(family)); | |
1056 | ||
1057 | if (family == AF_INET) | |
1058 | return manager_ipv4_send(m, fd, ifindex, &destination->in, port, source ? &source->in : NULL, p); | |
1059 | if (family == AF_INET6) | |
1060 | return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, source ? &source->in6 : NULL, p); | |
1061 | ||
1062 | return -EAFNOSUPPORT; | |
1063 | } | |
1064 | ||
1065 | uint32_t manager_find_mtu(Manager *m) { | |
1066 | uint32_t mtu = 0; | |
1067 | Link *l; | |
1068 | Iterator i; | |
1069 | ||
1070 | /* If we don't know on which link a DNS packet would be | |
1071 | * delivered, let's find the largest MTU that works on all | |
1072 | * interfaces we know of */ | |
1073 | ||
1074 | HASHMAP_FOREACH(l, m->links, i) { | |
1075 | if (l->mtu <= 0) | |
1076 | continue; | |
1077 | ||
1078 | if (mtu <= 0 || l->mtu < mtu) | |
1079 | mtu = l->mtu; | |
1080 | } | |
1081 | ||
1082 | return mtu; | |
1083 | } | |
1084 | ||
1085 | int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) { | |
1086 | LinkAddress *a; | |
1087 | ||
1088 | assert(m); | |
1089 | ||
1090 | a = manager_find_link_address(m, family, in_addr); | |
1091 | if (a) | |
1092 | return a->link->ifindex; | |
1093 | ||
1094 | return 0; | |
1095 | } | |
1096 | ||
1097 | void manager_refresh_rrs(Manager *m) { | |
1098 | Iterator i; | |
1099 | Link *l; | |
1100 | DnssdService *s; | |
1101 | ||
1102 | assert(m); | |
1103 | ||
1104 | m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key); | |
1105 | m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
1106 | m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key); | |
1107 | m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key); | |
1108 | ||
1109 | if (m->mdns_support == RESOLVE_SUPPORT_YES) | |
1110 | HASHMAP_FOREACH(s, m->dnssd_services, i) | |
1111 | if (dnssd_update_rrs(s) < 0) | |
1112 | log_warning("Failed to refresh DNS-SD service '%s'", s->name); | |
1113 | ||
1114 | HASHMAP_FOREACH(l, m->links, i) { | |
1115 | link_add_rrs(l, true); | |
1116 | link_add_rrs(l, false); | |
1117 | } | |
1118 | } | |
1119 | ||
1120 | static int manager_next_random_name(const char *old, char **ret_new) { | |
1121 | const char *p; | |
1122 | uint64_t u, a; | |
1123 | char *n; | |
1124 | ||
1125 | p = strchr(old, 0); | |
1126 | assert(p); | |
1127 | ||
1128 | while (p > old) { | |
1129 | if (!strchr(DIGITS, p[-1])) | |
1130 | break; | |
1131 | ||
1132 | p--; | |
1133 | } | |
1134 | ||
1135 | if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0) | |
1136 | u = 1; | |
1137 | ||
1138 | /* Add a random number to the old value. This way we can avoid | |
1139 | * that two hosts pick the same hostname, win on IPv4 and lose | |
1140 | * on IPv6 (or vice versa), and pick the same hostname | |
1141 | * replacement hostname, ad infinitum. We still want the | |
1142 | * numbers to go up monotonically, hence we just add a random | |
1143 | * value 1..10 */ | |
1144 | ||
1145 | random_bytes(&a, sizeof(a)); | |
1146 | u += 1 + a % 10; | |
1147 | ||
1148 | if (asprintf(&n, "%.*s%" PRIu64, (int) (p - old), old, u) < 0) | |
1149 | return -ENOMEM; | |
1150 | ||
1151 | *ret_new = n; | |
1152 | ||
1153 | return 0; | |
1154 | } | |
1155 | ||
1156 | int manager_next_hostname(Manager *m) { | |
1157 | _cleanup_free_ char *h = NULL, *k = NULL; | |
1158 | int r; | |
1159 | ||
1160 | assert(m); | |
1161 | ||
1162 | r = manager_next_random_name(m->llmnr_hostname, &h); | |
1163 | if (r < 0) | |
1164 | return r; | |
1165 | ||
1166 | r = dns_name_concat(h, "local", 0, &k); | |
1167 | if (r < 0) | |
1168 | return r; | |
1169 | ||
1170 | log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h); | |
1171 | ||
1172 | free_and_replace(m->llmnr_hostname, h); | |
1173 | free_and_replace(m->mdns_hostname, k); | |
1174 | ||
1175 | manager_refresh_rrs(m); | |
1176 | ||
1177 | return 0; | |
1178 | } | |
1179 | ||
1180 | LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) { | |
1181 | Iterator i; | |
1182 | Link *l; | |
1183 | ||
1184 | assert(m); | |
1185 | ||
1186 | HASHMAP_FOREACH(l, m->links, i) { | |
1187 | LinkAddress *a; | |
1188 | ||
1189 | a = link_find_address(l, family, in_addr); | |
1190 | if (a) | |
1191 | return a; | |
1192 | } | |
1193 | ||
1194 | return NULL; | |
1195 | } | |
1196 | ||
1197 | bool manager_our_packet(Manager *m, DnsPacket *p) { | |
1198 | assert(m); | |
1199 | assert(p); | |
1200 | ||
1201 | return !!manager_find_link_address(m, p->family, &p->sender); | |
1202 | } | |
1203 | ||
1204 | DnsScope* manager_find_scope(Manager *m, DnsPacket *p) { | |
1205 | Link *l; | |
1206 | ||
1207 | assert(m); | |
1208 | assert(p); | |
1209 | ||
1210 | l = hashmap_get(m->links, INT_TO_PTR(p->ifindex)); | |
1211 | if (!l) | |
1212 | return NULL; | |
1213 | ||
1214 | switch (p->protocol) { | |
1215 | case DNS_PROTOCOL_LLMNR: | |
1216 | if (p->family == AF_INET) | |
1217 | return l->llmnr_ipv4_scope; | |
1218 | else if (p->family == AF_INET6) | |
1219 | return l->llmnr_ipv6_scope; | |
1220 | ||
1221 | break; | |
1222 | ||
1223 | case DNS_PROTOCOL_MDNS: | |
1224 | if (p->family == AF_INET) | |
1225 | return l->mdns_ipv4_scope; | |
1226 | else if (p->family == AF_INET6) | |
1227 | return l->mdns_ipv6_scope; | |
1228 | ||
1229 | break; | |
1230 | ||
1231 | default: | |
1232 | break; | |
1233 | } | |
1234 | ||
1235 | return NULL; | |
1236 | } | |
1237 | ||
1238 | void manager_verify_all(Manager *m) { | |
1239 | DnsScope *s; | |
1240 | ||
1241 | assert(m); | |
1242 | ||
1243 | LIST_FOREACH(scopes, s, m->dns_scopes) | |
1244 | dns_zone_verify_all(&s->zone); | |
1245 | } | |
1246 | ||
1247 | int manager_is_own_hostname(Manager *m, const char *name) { | |
1248 | int r; | |
1249 | ||
1250 | assert(m); | |
1251 | assert(name); | |
1252 | ||
1253 | if (m->llmnr_hostname) { | |
1254 | r = dns_name_equal(name, m->llmnr_hostname); | |
1255 | if (r != 0) | |
1256 | return r; | |
1257 | } | |
1258 | ||
1259 | if (m->mdns_hostname) { | |
1260 | r = dns_name_equal(name, m->mdns_hostname); | |
1261 | if (r != 0) | |
1262 | return r; | |
1263 | } | |
1264 | ||
1265 | if (m->full_hostname) | |
1266 | return dns_name_equal(name, m->full_hostname); | |
1267 | ||
1268 | return 0; | |
1269 | } | |
1270 | ||
1271 | int manager_compile_dns_servers(Manager *m, OrderedSet **dns) { | |
1272 | DnsServer *s; | |
1273 | Iterator i; | |
1274 | Link *l; | |
1275 | int r; | |
1276 | ||
1277 | assert(m); | |
1278 | assert(dns); | |
1279 | ||
1280 | r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops); | |
1281 | if (r < 0) | |
1282 | return r; | |
1283 | ||
1284 | /* First add the system-wide servers and domains */ | |
1285 | LIST_FOREACH(servers, s, m->dns_servers) { | |
1286 | r = ordered_set_put(*dns, s); | |
1287 | if (r == -EEXIST) | |
1288 | continue; | |
1289 | if (r < 0) | |
1290 | return r; | |
1291 | } | |
1292 | ||
1293 | /* Then, add the per-link servers */ | |
1294 | HASHMAP_FOREACH(l, m->links, i) { | |
1295 | LIST_FOREACH(servers, s, l->dns_servers) { | |
1296 | r = ordered_set_put(*dns, s); | |
1297 | if (r == -EEXIST) | |
1298 | continue; | |
1299 | if (r < 0) | |
1300 | return r; | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | /* If we found nothing, add the fallback servers */ | |
1305 | if (ordered_set_isempty(*dns)) { | |
1306 | LIST_FOREACH(servers, s, m->fallback_dns_servers) { | |
1307 | r = ordered_set_put(*dns, s); | |
1308 | if (r == -EEXIST) | |
1309 | continue; | |
1310 | if (r < 0) | |
1311 | return r; | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | return 0; | |
1316 | } | |
1317 | ||
1318 | /* filter_route is a tri-state: | |
1319 | * < 0: no filtering | |
1320 | * = 0 or false: return only domains which should be used for searching | |
1321 | * > 0 or true: return only domains which are for routing only | |
1322 | */ | |
1323 | int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) { | |
1324 | DnsSearchDomain *d; | |
1325 | Iterator i; | |
1326 | Link *l; | |
1327 | int r; | |
1328 | ||
1329 | assert(m); | |
1330 | assert(domains); | |
1331 | ||
1332 | r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops); | |
1333 | if (r < 0) | |
1334 | return r; | |
1335 | ||
1336 | LIST_FOREACH(domains, d, m->search_domains) { | |
1337 | ||
1338 | if (filter_route >= 0 && | |
1339 | d->route_only != !!filter_route) | |
1340 | continue; | |
1341 | ||
1342 | r = ordered_set_put(*domains, d->name); | |
1343 | if (r == -EEXIST) | |
1344 | continue; | |
1345 | if (r < 0) | |
1346 | return r; | |
1347 | } | |
1348 | ||
1349 | HASHMAP_FOREACH(l, m->links, i) { | |
1350 | ||
1351 | LIST_FOREACH(domains, d, l->search_domains) { | |
1352 | ||
1353 | if (filter_route >= 0 && | |
1354 | d->route_only != !!filter_route) | |
1355 | continue; | |
1356 | ||
1357 | r = ordered_set_put(*domains, d->name); | |
1358 | if (r == -EEXIST) | |
1359 | continue; | |
1360 | if (r < 0) | |
1361 | return r; | |
1362 | } | |
1363 | } | |
1364 | ||
1365 | return 0; | |
1366 | } | |
1367 | ||
1368 | DnssecMode manager_get_dnssec_mode(Manager *m) { | |
1369 | assert(m); | |
1370 | ||
1371 | if (m->dnssec_mode != _DNSSEC_MODE_INVALID) | |
1372 | return m->dnssec_mode; | |
1373 | ||
1374 | return DNSSEC_NO; | |
1375 | } | |
1376 | ||
1377 | bool manager_dnssec_supported(Manager *m) { | |
1378 | DnsServer *server; | |
1379 | Iterator i; | |
1380 | Link *l; | |
1381 | ||
1382 | assert(m); | |
1383 | ||
1384 | if (manager_get_dnssec_mode(m) == DNSSEC_NO) | |
1385 | return false; | |
1386 | ||
1387 | server = manager_get_dns_server(m); | |
1388 | if (server && !dns_server_dnssec_supported(server)) | |
1389 | return false; | |
1390 | ||
1391 | HASHMAP_FOREACH(l, m->links, i) | |
1392 | if (!link_dnssec_supported(l)) | |
1393 | return false; | |
1394 | ||
1395 | return true; | |
1396 | } | |
1397 | ||
1398 | DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m) { | |
1399 | assert(m); | |
1400 | ||
1401 | if (m->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) | |
1402 | return m->dns_over_tls_mode; | |
1403 | ||
1404 | return DNS_OVER_TLS_NO; | |
1405 | } | |
1406 | ||
1407 | void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) { | |
1408 | ||
1409 | assert(verdict >= 0); | |
1410 | assert(verdict < _DNSSEC_VERDICT_MAX); | |
1411 | ||
1412 | if (DEBUG_LOGGING) { | |
1413 | char s[DNS_RESOURCE_KEY_STRING_MAX]; | |
1414 | ||
1415 | log_debug("Found verdict for lookup %s: %s", | |
1416 | dns_resource_key_to_string(key, s, sizeof s), | |
1417 | dnssec_verdict_to_string(verdict)); | |
1418 | } | |
1419 | ||
1420 | m->n_dnssec_verdict[verdict]++; | |
1421 | } | |
1422 | ||
1423 | bool manager_routable(Manager *m, int family) { | |
1424 | Iterator i; | |
1425 | Link *l; | |
1426 | ||
1427 | assert(m); | |
1428 | ||
1429 | /* Returns true if the host has at least one interface with a routable address of the specified type */ | |
1430 | ||
1431 | HASHMAP_FOREACH(l, m->links, i) | |
1432 | if (link_relevant(l, family, false)) | |
1433 | return true; | |
1434 | ||
1435 | return false; | |
1436 | } | |
1437 | ||
1438 | void manager_flush_caches(Manager *m) { | |
1439 | DnsScope *scope; | |
1440 | ||
1441 | assert(m); | |
1442 | ||
1443 | LIST_FOREACH(scopes, scope, m->dns_scopes) | |
1444 | dns_cache_flush(&scope->cache); | |
1445 | ||
1446 | log_info("Flushed all caches."); | |
1447 | } | |
1448 | ||
1449 | void manager_reset_server_features(Manager *m) { | |
1450 | Iterator i; | |
1451 | Link *l; | |
1452 | ||
1453 | dns_server_reset_features_all(m->dns_servers); | |
1454 | dns_server_reset_features_all(m->fallback_dns_servers); | |
1455 | ||
1456 | HASHMAP_FOREACH(l, m->links, i) | |
1457 | dns_server_reset_features_all(l->dns_servers); | |
1458 | ||
1459 | log_info("Resetting learnt feature levels on all servers."); | |
1460 | } | |
1461 | ||
1462 | void manager_cleanup_saved_user(Manager *m) { | |
1463 | _cleanup_closedir_ DIR *d = NULL; | |
1464 | struct dirent *de; | |
1465 | int r; | |
1466 | ||
1467 | assert(m); | |
1468 | ||
1469 | /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface | |
1470 | * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can | |
1471 | * be restarted without losing this data. */ | |
1472 | ||
1473 | d = opendir("/run/systemd/resolve/netif/"); | |
1474 | if (!d) { | |
1475 | if (errno == ENOENT) | |
1476 | return; | |
1477 | ||
1478 | log_warning_errno(errno, "Failed to open interface directory: %m"); | |
1479 | return; | |
1480 | } | |
1481 | ||
1482 | FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) { | |
1483 | _cleanup_free_ char *p = NULL; | |
1484 | int ifindex; | |
1485 | Link *l; | |
1486 | ||
1487 | if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG)) | |
1488 | continue; | |
1489 | ||
1490 | if (dot_or_dot_dot(de->d_name)) | |
1491 | continue; | |
1492 | ||
1493 | r = parse_ifindex(de->d_name, &ifindex); | |
1494 | if (r < 0) /* Probably some temporary file from a previous run. Delete it */ | |
1495 | goto rm; | |
1496 | ||
1497 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
1498 | if (!l) /* link vanished */ | |
1499 | goto rm; | |
1500 | ||
1501 | if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */ | |
1502 | goto rm; | |
1503 | ||
1504 | continue; | |
1505 | ||
1506 | rm: | |
1507 | p = path_join("/run/systemd/resolve/netif", de->d_name); | |
1508 | if (!p) { | |
1509 | log_oom(); | |
1510 | return; | |
1511 | } | |
1512 | ||
1513 | (void) unlink(p); | |
1514 | } | |
1515 | } | |
1516 | ||
1517 | bool manager_next_dnssd_names(Manager *m) { | |
1518 | Iterator i; | |
1519 | DnssdService *s; | |
1520 | bool tried = false; | |
1521 | int r; | |
1522 | ||
1523 | assert(m); | |
1524 | ||
1525 | HASHMAP_FOREACH(s, m->dnssd_services, i) { | |
1526 | _cleanup_free_ char * new_name = NULL; | |
1527 | ||
1528 | if (!s->withdrawn) | |
1529 | continue; | |
1530 | ||
1531 | r = manager_next_random_name(s->name_template, &new_name); | |
1532 | if (r < 0) { | |
1533 | log_warning_errno(r, "Failed to get new name for service '%s': %m", s->name); | |
1534 | continue; | |
1535 | } | |
1536 | ||
1537 | free_and_replace(s->name_template, new_name); | |
1538 | ||
1539 | s->withdrawn = false; | |
1540 | ||
1541 | tried = true; | |
1542 | } | |
1543 | ||
1544 | if (tried) | |
1545 | manager_refresh_rrs(m); | |
1546 | ||
1547 | return tried; | |
1548 | } |