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