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