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