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