]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-bus.c
ethtool: add several new link modes
[thirdparty/systemd.git] / src / resolve / resolved-bus.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "bus-common-errors.h"
5 #include "bus-get-properties.h"
6 #include "bus-log-control-api.h"
7 #include "bus-message-util.h"
8 #include "bus-polkit.h"
9 #include "dns-domain.h"
10 #include "memory-util.h"
11 #include "missing_capability.h"
12 #include "resolved-bus.h"
13 #include "resolved-def.h"
14 #include "resolved-dns-synthesize.h"
15 #include "resolved-dnssd-bus.h"
16 #include "resolved-dnssd.h"
17 #include "resolved-link-bus.h"
18 #include "socket-netlink.h"
19 #include "stdio-util.h"
20 #include "strv.h"
21 #include "syslog-util.h"
22 #include "user-util.h"
23 #include "utf8.h"
24
25 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_resolve_support, resolve_support, ResolveSupport);
26
27 static int query_on_bus_track(sd_bus_track *t, void *userdata) {
28 DnsQuery *q = userdata;
29
30 assert(t);
31 assert(q);
32
33 if (!DNS_TRANSACTION_IS_LIVE(q->state))
34 return 0;
35
36 log_debug("Client of active query vanished, aborting query.");
37 dns_query_complete(q, DNS_TRANSACTION_ABORTED);
38 return 0;
39 }
40
41 static int dns_query_bus_track(DnsQuery *q, sd_bus_message *m) {
42 int r;
43
44 assert(q);
45 assert(m);
46
47 if (!q->bus_track) {
48 r = sd_bus_track_new(sd_bus_message_get_bus(m), &q->bus_track, query_on_bus_track, q);
49 if (r < 0)
50 return r;
51 }
52
53 r = sd_bus_track_add_sender(q->bus_track, m);
54 if (r < 0)
55 return r;
56
57 return 0;
58 }
59
60 static int reply_query_state(DnsQuery *q) {
61
62 assert(q);
63 assert(q->bus_request);
64
65 switch (q->state) {
66
67 case DNS_TRANSACTION_NO_SERVERS:
68 return sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
69
70 case DNS_TRANSACTION_TIMEOUT:
71 return sd_bus_reply_method_errorf(q->bus_request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
72
73 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
74 return sd_bus_reply_method_errorf(q->bus_request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
75
76 case DNS_TRANSACTION_INVALID_REPLY:
77 return sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
78
79 case DNS_TRANSACTION_ERRNO:
80 return sd_bus_reply_method_errnof(q->bus_request, q->answer_errno, "Lookup failed due to system error: %m");
81
82 case DNS_TRANSACTION_ABORTED:
83 return sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_ABORTED, "Query aborted");
84
85 case DNS_TRANSACTION_DNSSEC_FAILED:
86 return sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_DNSSEC_FAILED, "DNSSEC validation failed: %s",
87 dnssec_result_to_string(q->answer_dnssec_result));
88
89 case DNS_TRANSACTION_NO_TRUST_ANCHOR:
90 return sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_NO_TRUST_ANCHOR, "No suitable trust anchor known");
91
92 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED:
93 return sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_RR_TYPE_UNSUPPORTED, "Server does not support requested resource record type");
94
95 case DNS_TRANSACTION_NETWORK_DOWN:
96 return sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_NETWORK_DOWN, "Network is down");
97
98 case DNS_TRANSACTION_NOT_FOUND:
99 /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
100 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
101 return sd_bus_reply_method_errorf(q->bus_request, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", dns_query_string(q));
102
103 case DNS_TRANSACTION_RCODE_FAILURE: {
104 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
105
106 if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
107 sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", dns_query_string(q));
108 else {
109 const char *rc, *n;
110 char p[DECIMAL_STR_MAX(q->answer_rcode)];
111
112 rc = dns_rcode_to_string(q->answer_rcode);
113 if (!rc) {
114 xsprintf(p, "%i", q->answer_rcode);
115 rc = p;
116 }
117
118 n = strjoina(_BUS_ERROR_DNS, rc);
119 sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", dns_query_string(q), rc);
120 }
121
122 return sd_bus_reply_method_error(q->bus_request, &error);
123 }
124
125 case DNS_TRANSACTION_NULL:
126 case DNS_TRANSACTION_PENDING:
127 case DNS_TRANSACTION_VALIDATING:
128 case DNS_TRANSACTION_SUCCESS:
129 default:
130 assert_not_reached("Impossible state");
131 }
132 }
133
134 static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
135 int r;
136
137 assert(reply);
138 assert(rr);
139
140 r = sd_bus_message_open_container(reply, 'r', "iiay");
141 if (r < 0)
142 return r;
143
144 r = sd_bus_message_append(reply, "i", ifindex);
145 if (r < 0)
146 return r;
147
148 if (rr->key->type == DNS_TYPE_A) {
149 r = sd_bus_message_append(reply, "i", AF_INET);
150 if (r < 0)
151 return r;
152
153 r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
154
155 } else if (rr->key->type == DNS_TYPE_AAAA) {
156 r = sd_bus_message_append(reply, "i", AF_INET6);
157 if (r < 0)
158 return r;
159
160 r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
161 } else
162 return -EAFNOSUPPORT;
163
164 if (r < 0)
165 return r;
166
167 r = sd_bus_message_close_container(reply);
168 if (r < 0)
169 return r;
170
171 return 0;
172 }
173
174 static void bus_method_resolve_hostname_complete(DnsQuery *q) {
175 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
176 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
177 _cleanup_free_ char *normalized = NULL;
178 DnsQuestion *question;
179 DnsResourceRecord *rr;
180 unsigned added = 0;
181 int ifindex, r;
182
183 assert(q);
184
185 if (q->state != DNS_TRANSACTION_SUCCESS) {
186 r = reply_query_state(q);
187 goto finish;
188 }
189
190 r = dns_query_process_cname(q);
191 if (r == -ELOOP) {
192 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
193 goto finish;
194 }
195 if (r < 0)
196 goto finish;
197 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
198 return;
199
200 r = sd_bus_message_new_method_return(q->bus_request, &reply);
201 if (r < 0)
202 goto finish;
203
204 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
205 if (r < 0)
206 goto finish;
207
208 question = dns_query_question_for_protocol(q, q->answer_protocol);
209
210 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
211
212 r = dns_question_matches_rr(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
213 if (r < 0)
214 goto finish;
215 if (r == 0)
216 continue;
217
218 r = append_address(reply, rr, ifindex);
219 if (r < 0)
220 goto finish;
221
222 if (!canonical)
223 canonical = dns_resource_record_ref(rr);
224
225 added++;
226 }
227
228 if (added <= 0) {
229 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
230 goto finish;
231 }
232
233 r = sd_bus_message_close_container(reply);
234 if (r < 0)
235 goto finish;
236
237 /* The key names are not necessarily normalized, make sure that they are when we return them to our
238 * bus clients. */
239 assert(canonical);
240 r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
241 if (r < 0)
242 goto finish;
243
244 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
245 r = sd_bus_message_append(
246 reply, "st",
247 normalized,
248 SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
249 if (r < 0)
250 goto finish;
251
252 r = sd_bus_send(q->manager->bus, reply, NULL);
253
254 finish:
255 if (r < 0) {
256 log_error_errno(r, "Failed to send hostname reply: %m");
257 sd_bus_reply_method_errno(q->bus_request, r, NULL);
258 }
259
260 dns_query_free(q);
261 }
262
263 static int validate_and_mangle_ifindex_and_flags(int ifindex, uint64_t *flags, uint64_t ok, sd_bus_error *error) {
264 assert(flags);
265
266 /* Checks that the client supplied interface index and flags parameter actually are valid and make
267 * sense in our method call context. Specifically:
268 *
269 * 1. Checks that the interface index is either 0 (meaning *all* interfaces) or positive
270 *
271 * 2. Only the protocols flags and the NO_CNAME flag are set, at most. Plus additional flags specific
272 * to our method, passed in the "ok" parameter.
273 *
274 * 3. If zero protocol flags are specified it is automatically turned into *all* protocols. This way
275 * clients can simply pass 0 as flags and all will work as it should. They can also use this so
276 * that clients don't have to know all the protocols resolved implements, but can just specify 0
277 * to mean "all supported protocols".
278 */
279
280 if (ifindex < 0)
281 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
282
283 if (*flags & ~(SD_RESOLVED_PROTOCOLS_ALL|SD_RESOLVED_NO_CNAME|ok))
284 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
285
286 if ((*flags & SD_RESOLVED_PROTOCOLS_ALL) == 0) /* If no protocol is enabled, enable all */
287 *flags |= SD_RESOLVED_PROTOCOLS_ALL;
288
289 return 0;
290 }
291
292 static int parse_as_address(sd_bus_message *m, int ifindex, const char *hostname, int family, uint64_t flags) {
293 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
294 _cleanup_free_ char *canonical = NULL;
295 union in_addr_union parsed;
296 int r, ff, parsed_ifindex = 0;
297
298 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
299 * let's not attempt to look it up. */
300
301 r = in_addr_ifindex_from_string_auto(hostname, &ff, &parsed, &parsed_ifindex);
302 if (r < 0) /* not an address */
303 return 0;
304
305 if (family != AF_UNSPEC && ff != family)
306 return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address is not of the requested family.");
307 if (ifindex > 0 && parsed_ifindex > 0 && parsed_ifindex != ifindex)
308 return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address interface index does not match requested interface.");
309
310 if (parsed_ifindex > 0)
311 ifindex = parsed_ifindex;
312
313 r = sd_bus_message_new_method_return(m, &reply);
314 if (r < 0)
315 return r;
316
317 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
318 if (r < 0)
319 return r;
320
321 r = sd_bus_message_open_container(reply, 'r', "iiay");
322 if (r < 0)
323 return r;
324
325 r = sd_bus_message_append(reply, "ii", ifindex, ff);
326 if (r < 0)
327 return r;
328
329 r = sd_bus_message_append_array(reply, 'y', &parsed, FAMILY_ADDRESS_SIZE(ff));
330 if (r < 0)
331 return r;
332
333 r = sd_bus_message_close_container(reply);
334 if (r < 0)
335 return r;
336
337 r = sd_bus_message_close_container(reply);
338 if (r < 0)
339 return r;
340
341 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
342 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
343 * omissions are always done the same way). */
344 r = in_addr_ifindex_to_string(ff, &parsed, ifindex, &canonical);
345 if (r < 0)
346 return r;
347
348 r = sd_bus_message_append(reply, "st", canonical,
349 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags), ff, true));
350 if (r < 0)
351 return r;
352
353 return sd_bus_send(sd_bus_message_get_bus(m), reply, NULL);
354 }
355
356 static int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
357 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
358 Manager *m = userdata;
359 const char *hostname;
360 int family, ifindex;
361 uint64_t flags;
362 DnsQuery *q;
363 int r;
364
365 assert(message);
366 assert(m);
367
368 assert_cc(sizeof(int) == sizeof(int32_t));
369
370 r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
371 if (r < 0)
372 return r;
373
374 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
375 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
376
377 r = validate_and_mangle_ifindex_and_flags(ifindex, &flags, SD_RESOLVED_NO_SEARCH, error);
378 if (r < 0)
379 return r;
380
381 r = parse_as_address(message, ifindex, hostname, family, flags);
382 if (r != 0)
383 return r;
384
385 r = dns_name_is_valid(hostname);
386 if (r < 0)
387 return r;
388 if (r == 0)
389 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
390
391 r = dns_question_new_address(&question_utf8, family, hostname, false);
392 if (r < 0)
393 return r;
394
395 r = dns_question_new_address(&question_idna, family, hostname, true);
396 if (r < 0 && r != -EALREADY)
397 return r;
398
399 r = dns_query_new(m, &q, question_utf8, question_idna ?: question_utf8, ifindex, flags);
400 if (r < 0)
401 return r;
402
403 q->bus_request = sd_bus_message_ref(message);
404 q->request_family = family;
405 q->complete = bus_method_resolve_hostname_complete;
406
407 r = dns_query_bus_track(q, message);
408 if (r < 0)
409 goto fail;
410
411 r = dns_query_go(q);
412 if (r < 0)
413 goto fail;
414
415 return 1;
416
417 fail:
418 dns_query_free(q);
419 return r;
420 }
421
422 static void bus_method_resolve_address_complete(DnsQuery *q) {
423 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
424 DnsQuestion *question;
425 DnsResourceRecord *rr;
426 unsigned added = 0;
427 int ifindex, r;
428
429 assert(q);
430
431 if (q->state != DNS_TRANSACTION_SUCCESS) {
432 r = reply_query_state(q);
433 goto finish;
434 }
435
436 r = dns_query_process_cname(q);
437 if (r == -ELOOP) {
438 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
439 goto finish;
440 }
441 if (r < 0)
442 goto finish;
443 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
444 return;
445
446 r = sd_bus_message_new_method_return(q->bus_request, &reply);
447 if (r < 0)
448 goto finish;
449
450 r = sd_bus_message_open_container(reply, 'a', "(is)");
451 if (r < 0)
452 goto finish;
453
454 question = dns_query_question_for_protocol(q, q->answer_protocol);
455
456 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
457 _cleanup_free_ char *normalized = NULL;
458
459 r = dns_question_matches_rr(question, rr, NULL);
460 if (r < 0)
461 goto finish;
462 if (r == 0)
463 continue;
464
465 r = dns_name_normalize(rr->ptr.name, 0, &normalized);
466 if (r < 0)
467 goto finish;
468
469 r = sd_bus_message_append(reply, "(is)", ifindex, normalized);
470 if (r < 0)
471 goto finish;
472
473 added++;
474 }
475
476 if (added <= 0) {
477 _cleanup_free_ char *ip = NULL;
478
479 (void) in_addr_to_string(q->request_family, &q->request_address, &ip);
480 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_NO_SUCH_RR,
481 "Address '%s' does not have any RR of requested type", strnull(ip));
482 goto finish;
483 }
484
485 r = sd_bus_message_close_container(reply);
486 if (r < 0)
487 goto finish;
488
489 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
490 if (r < 0)
491 goto finish;
492
493 r = sd_bus_send(q->manager->bus, reply, NULL);
494
495 finish:
496 if (r < 0) {
497 log_error_errno(r, "Failed to send address reply: %m");
498 sd_bus_reply_method_errno(q->bus_request, r, NULL);
499 }
500
501 dns_query_free(q);
502 }
503
504 static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
505 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
506 Manager *m = userdata;
507 union in_addr_union a;
508 int family, ifindex;
509 uint64_t flags;
510 DnsQuery *q;
511 int r;
512
513 assert(message);
514 assert(m);
515
516 assert_cc(sizeof(int) == sizeof(int32_t));
517
518 r = sd_bus_message_read(message, "i", &ifindex);
519 if (r < 0)
520 return r;
521
522 r = bus_message_read_in_addr_auto(message, error, &family, &a);
523 if (r < 0)
524 return r;
525
526 r = sd_bus_message_read(message, "t", &flags);
527 if (r < 0)
528 return r;
529
530 r = validate_and_mangle_ifindex_and_flags(ifindex, &flags, 0, error);
531 if (r < 0)
532 return r;
533
534 r = dns_question_new_reverse(&question, family, &a);
535 if (r < 0)
536 return r;
537
538 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
539 if (r < 0)
540 return r;
541
542 q->bus_request = sd_bus_message_ref(message);
543 q->request_family = family;
544 q->request_address = a;
545 q->complete = bus_method_resolve_address_complete;
546
547 r = dns_query_bus_track(q, message);
548 if (r < 0)
549 goto fail;
550
551 r = dns_query_go(q);
552 if (r < 0)
553 goto fail;
554
555 return 1;
556
557 fail:
558 dns_query_free(q);
559 return r;
560 }
561
562 static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
563 int r;
564
565 assert(m);
566 assert(rr);
567
568 r = sd_bus_message_open_container(m, 'r', "iqqay");
569 if (r < 0)
570 return r;
571
572 r = sd_bus_message_append(m, "iqq",
573 ifindex,
574 rr->key->class,
575 rr->key->type);
576 if (r < 0)
577 return r;
578
579 r = dns_resource_record_to_wire_format(rr, false);
580 if (r < 0)
581 return r;
582
583 r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
584 if (r < 0)
585 return r;
586
587 return sd_bus_message_close_container(m);
588 }
589
590 static void bus_method_resolve_record_complete(DnsQuery *q) {
591 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
592 DnsResourceRecord *rr;
593 DnsQuestion *question;
594 unsigned added = 0;
595 int ifindex;
596 int r;
597
598 assert(q);
599
600 if (q->state != DNS_TRANSACTION_SUCCESS) {
601 r = reply_query_state(q);
602 goto finish;
603 }
604
605 r = dns_query_process_cname(q);
606 if (r == -ELOOP) {
607 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
608 goto finish;
609 }
610 if (r < 0)
611 goto finish;
612 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
613 return;
614
615 r = sd_bus_message_new_method_return(q->bus_request, &reply);
616 if (r < 0)
617 goto finish;
618
619 r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
620 if (r < 0)
621 goto finish;
622
623 question = dns_query_question_for_protocol(q, q->answer_protocol);
624
625 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
626 r = dns_question_matches_rr(question, rr, NULL);
627 if (r < 0)
628 goto finish;
629 if (r == 0)
630 continue;
631
632 r = bus_message_append_rr(reply, rr, ifindex);
633 if (r < 0)
634 goto finish;
635
636 added++;
637 }
638
639 if (added <= 0) {
640 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", dns_query_string(q));
641 goto finish;
642 }
643
644 r = sd_bus_message_close_container(reply);
645 if (r < 0)
646 goto finish;
647
648 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
649 if (r < 0)
650 goto finish;
651
652 r = sd_bus_send(q->manager->bus, reply, NULL);
653
654 finish:
655 if (r < 0) {
656 log_error_errno(r, "Failed to send record reply: %m");
657 sd_bus_reply_method_errno(q->bus_request, r, NULL);
658 }
659
660 dns_query_free(q);
661 }
662
663 static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
664 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
665 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
666 Manager *m = userdata;
667 uint16_t class, type;
668 const char *name;
669 int r, ifindex;
670 uint64_t flags;
671 DnsQuery *q;
672
673 assert(message);
674 assert(m);
675
676 assert_cc(sizeof(int) == sizeof(int32_t));
677
678 r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
679 if (r < 0)
680 return r;
681
682 r = dns_name_is_valid(name);
683 if (r < 0)
684 return r;
685 if (r == 0)
686 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
687
688 if (!dns_type_is_valid_query(type))
689 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified resource record type %" PRIu16 " may not be used in a query.", type);
690 if (dns_type_is_zone_transer(type))
691 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Zone transfers not permitted via this programming interface.");
692 if (dns_type_is_obsolete(type))
693 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
694
695 r = validate_and_mangle_ifindex_and_flags(ifindex, &flags, 0, error);
696 if (r < 0)
697 return r;
698
699 question = dns_question_new(1);
700 if (!question)
701 return -ENOMEM;
702
703 key = dns_resource_key_new(class, type, name);
704 if (!key)
705 return -ENOMEM;
706
707 r = dns_question_add(question, key);
708 if (r < 0)
709 return r;
710
711 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
712 if (r < 0)
713 return r;
714
715 /* Let's request that the TTL is fixed up for locally cached entries, after all we return it in the wire format
716 * blob */
717 q->clamp_ttl = true;
718
719 q->bus_request = sd_bus_message_ref(message);
720 q->complete = bus_method_resolve_record_complete;
721
722 r = dns_query_bus_track(q, message);
723 if (r < 0)
724 goto fail;
725
726 r = dns_query_go(q);
727 if (r < 0)
728 goto fail;
729
730 return 1;
731
732 fail:
733 dns_query_free(q);
734 return r;
735 }
736
737 static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
738 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
739 _cleanup_free_ char *normalized = NULL;
740 DnsQuery *aux;
741 int r;
742
743 assert(q);
744 assert(reply);
745 assert(rr);
746 assert(rr->key);
747
748 if (rr->key->type != DNS_TYPE_SRV)
749 return 0;
750
751 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
752 /* First, let's see if we could find an appropriate A or AAAA
753 * record for the SRV record */
754 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
755 DnsResourceRecord *zz;
756 DnsQuestion *question;
757
758 if (aux->state != DNS_TRANSACTION_SUCCESS)
759 continue;
760 if (aux->auxiliary_result != 0)
761 continue;
762
763 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
764
765 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
766 if (r < 0)
767 return r;
768 if (r == 0)
769 continue;
770
771 DNS_ANSWER_FOREACH(zz, aux->answer) {
772
773 r = dns_question_matches_rr(question, zz, NULL);
774 if (r < 0)
775 return r;
776 if (r == 0)
777 continue;
778
779 canonical = dns_resource_record_ref(zz);
780 break;
781 }
782
783 if (canonical)
784 break;
785 }
786
787 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
788 if (!canonical)
789 return 0;
790 }
791
792 r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
793 if (r < 0)
794 return r;
795
796 r = dns_name_normalize(rr->srv.name, 0, &normalized);
797 if (r < 0)
798 return r;
799
800 r = sd_bus_message_append(
801 reply,
802 "qqqs",
803 rr->srv.priority, rr->srv.weight, rr->srv.port, normalized);
804 if (r < 0)
805 return r;
806
807 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
808 if (r < 0)
809 return r;
810
811 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
812 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
813 DnsResourceRecord *zz;
814 DnsQuestion *question;
815 int ifindex;
816
817 if (aux->state != DNS_TRANSACTION_SUCCESS)
818 continue;
819 if (aux->auxiliary_result != 0)
820 continue;
821
822 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
823
824 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
825 if (r < 0)
826 return r;
827 if (r == 0)
828 continue;
829
830 DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
831
832 r = dns_question_matches_rr(question, zz, NULL);
833 if (r < 0)
834 return r;
835 if (r == 0)
836 continue;
837
838 r = append_address(reply, zz, ifindex);
839 if (r < 0)
840 return r;
841 }
842 }
843 }
844
845 r = sd_bus_message_close_container(reply);
846 if (r < 0)
847 return r;
848
849 if (canonical) {
850 normalized = mfree(normalized);
851
852 r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
853 if (r < 0)
854 return r;
855 }
856
857 /* Note that above we appended the hostname as encoded in the
858 * SRV, and here the canonical hostname this maps to. */
859 r = sd_bus_message_append(reply, "s", normalized);
860 if (r < 0)
861 return r;
862
863 r = sd_bus_message_close_container(reply);
864 if (r < 0)
865 return r;
866
867 return 1;
868 }
869
870 static int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
871 DnsTxtItem *i;
872 int r;
873
874 assert(reply);
875 assert(rr);
876 assert(rr->key);
877
878 if (rr->key->type != DNS_TYPE_TXT)
879 return 0;
880
881 LIST_FOREACH(items, i, rr->txt.items) {
882
883 if (i->length <= 0)
884 continue;
885
886 r = sd_bus_message_append_array(reply, 'y', i->data, i->length);
887 if (r < 0)
888 return r;
889 }
890
891 return 1;
892 }
893
894 static void resolve_service_all_complete(DnsQuery *q) {
895 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
896 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
897 _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL;
898 DnsQuestion *question;
899 DnsResourceRecord *rr;
900 unsigned added = 0;
901 DnsQuery *aux;
902 int r;
903
904 assert(q);
905
906 if (q->block_all_complete > 0)
907 return;
908
909 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
910 DnsQuery *bad = NULL;
911 bool have_success = false;
912
913 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
914
915 switch (aux->state) {
916
917 case DNS_TRANSACTION_PENDING:
918 /* If an auxiliary query is still pending, let's wait */
919 return;
920
921 case DNS_TRANSACTION_SUCCESS:
922 if (aux->auxiliary_result == 0)
923 have_success = true;
924 else
925 bad = aux;
926 break;
927
928 default:
929 bad = aux;
930 break;
931 }
932 }
933
934 if (!have_success) {
935 /* We can only return one error, hence pick the last error we encountered */
936
937 assert(bad);
938
939 if (bad->state == DNS_TRANSACTION_SUCCESS) {
940 assert(bad->auxiliary_result != 0);
941
942 if (bad->auxiliary_result == -ELOOP) {
943 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad));
944 goto finish;
945 }
946
947 r = bad->auxiliary_result;
948 goto finish;
949 }
950
951 r = reply_query_state(bad);
952 goto finish;
953 }
954 }
955
956 r = sd_bus_message_new_method_return(q->bus_request, &reply);
957 if (r < 0)
958 goto finish;
959
960 r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)");
961 if (r < 0)
962 goto finish;
963
964 question = dns_query_question_for_protocol(q, q->answer_protocol);
965
966 DNS_ANSWER_FOREACH(rr, q->answer) {
967 r = dns_question_matches_rr(question, rr, NULL);
968 if (r < 0)
969 goto finish;
970 if (r == 0)
971 continue;
972
973 r = append_srv(q, reply, rr);
974 if (r < 0)
975 goto finish;
976 if (r == 0) /* not an SRV record */
977 continue;
978
979 if (!canonical)
980 canonical = dns_resource_record_ref(rr);
981
982 added++;
983 }
984
985 if (added <= 0) {
986 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
987 goto finish;
988 }
989
990 r = sd_bus_message_close_container(reply);
991 if (r < 0)
992 goto finish;
993
994 r = sd_bus_message_open_container(reply, 'a', "ay");
995 if (r < 0)
996 goto finish;
997
998 DNS_ANSWER_FOREACH(rr, q->answer) {
999 r = dns_question_matches_rr(question, rr, NULL);
1000 if (r < 0)
1001 goto finish;
1002 if (r == 0)
1003 continue;
1004
1005 r = append_txt(reply, rr);
1006 if (r < 0)
1007 goto finish;
1008 }
1009
1010 r = sd_bus_message_close_container(reply);
1011 if (r < 0)
1012 goto finish;
1013
1014 assert(canonical);
1015 r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain);
1016 if (r < 0)
1017 goto finish;
1018
1019 r = sd_bus_message_append(
1020 reply,
1021 "ssst",
1022 name, type, domain,
1023 SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
1024 if (r < 0)
1025 goto finish;
1026
1027 r = sd_bus_send(q->manager->bus, reply, NULL);
1028
1029 finish:
1030 if (r < 0) {
1031 log_error_errno(r, "Failed to send service reply: %m");
1032 sd_bus_reply_method_errno(q->bus_request, r, NULL);
1033 }
1034
1035 dns_query_free(q);
1036 }
1037
1038 static void resolve_service_hostname_complete(DnsQuery *q) {
1039 int r;
1040
1041 assert(q);
1042 assert(q->auxiliary_for);
1043
1044 if (q->state != DNS_TRANSACTION_SUCCESS) {
1045 resolve_service_all_complete(q->auxiliary_for);
1046 return;
1047 }
1048
1049 r = dns_query_process_cname(q);
1050 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
1051 return;
1052
1053 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1054 q->auxiliary_result = r;
1055 resolve_service_all_complete(q->auxiliary_for);
1056 }
1057
1058 static int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) {
1059 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
1060 DnsQuery *aux;
1061 int r;
1062
1063 assert(q);
1064 assert(rr);
1065 assert(rr->key);
1066 assert(rr->key->type == DNS_TYPE_SRV);
1067
1068 /* OK, we found an SRV record for the service. Let's resolve
1069 * the hostname included in it */
1070
1071 r = dns_question_new_address(&question, q->request_family, rr->srv.name, false);
1072 if (r < 0)
1073 return r;
1074
1075 r = dns_query_new(q->manager, &aux, question, question, ifindex, q->flags|SD_RESOLVED_NO_SEARCH);
1076 if (r < 0)
1077 return r;
1078
1079 aux->request_family = q->request_family;
1080 aux->complete = resolve_service_hostname_complete;
1081
1082 r = dns_query_make_auxiliary(aux, q);
1083 if (r == -EAGAIN) {
1084 /* Too many auxiliary lookups? If so, don't complain,
1085 * let's just not add this one, we already have more
1086 * than enough */
1087
1088 dns_query_free(aux);
1089 return 0;
1090 }
1091 if (r < 0)
1092 goto fail;
1093
1094 /* Note that auxiliary queries do not track the original bus
1095 * client, only the primary request does that. */
1096
1097 r = dns_query_go(aux);
1098 if (r < 0)
1099 goto fail;
1100
1101 return 1;
1102
1103 fail:
1104 dns_query_free(aux);
1105 return r;
1106 }
1107
1108 static void bus_method_resolve_service_complete(DnsQuery *q) {
1109 bool has_root_domain = false;
1110 DnsResourceRecord *rr;
1111 DnsQuestion *question;
1112 unsigned found = 0;
1113 int ifindex, r;
1114
1115 assert(q);
1116
1117 if (q->state != DNS_TRANSACTION_SUCCESS) {
1118 r = reply_query_state(q);
1119 goto finish;
1120 }
1121
1122 r = dns_query_process_cname(q);
1123 if (r == -ELOOP) {
1124 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
1125 goto finish;
1126 }
1127 if (r < 0)
1128 goto finish;
1129 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
1130 return;
1131
1132 question = dns_query_question_for_protocol(q, q->answer_protocol);
1133
1134 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
1135 r = dns_question_matches_rr(question, rr, NULL);
1136 if (r < 0)
1137 goto finish;
1138 if (r == 0)
1139 continue;
1140
1141 if (rr->key->type != DNS_TYPE_SRV)
1142 continue;
1143
1144 if (dns_name_is_root(rr->srv.name)) {
1145 has_root_domain = true;
1146 continue;
1147 }
1148
1149 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1150 q->block_all_complete++;
1151 r = resolve_service_hostname(q, rr, ifindex);
1152 q->block_all_complete--;
1153
1154 if (r < 0)
1155 goto finish;
1156 }
1157
1158 found++;
1159 }
1160
1161 if (has_root_domain && found <= 0) {
1162 /* If there's exactly one SRV RR and it uses
1163 * the root domain as hostname, then the
1164 * service is explicitly not offered on the
1165 * domain. Report this as a recognizable
1166 * error. See RFC 2782, Section "Usage
1167 * Rules". */
1168 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide the requested service", dns_query_string(q));
1169 goto finish;
1170 }
1171
1172 if (found <= 0) {
1173 r = sd_bus_reply_method_errorf(q->bus_request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
1174 goto finish;
1175 }
1176
1177 /* Maybe we are already finished? check now... */
1178 resolve_service_all_complete(q);
1179 return;
1180
1181 finish:
1182 if (r < 0) {
1183 log_error_errno(r, "Failed to send service reply: %m");
1184 sd_bus_reply_method_errno(q->bus_request, r, NULL);
1185 }
1186
1187 dns_query_free(q);
1188 }
1189
1190 static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1191 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
1192 const char *name, *type, *domain;
1193 Manager *m = userdata;
1194 int family, ifindex;
1195 uint64_t flags;
1196 DnsQuery *q;
1197 int r;
1198
1199 assert(message);
1200 assert(m);
1201
1202 assert_cc(sizeof(int) == sizeof(int32_t));
1203
1204 r = sd_bus_message_read(message, "isssit", &ifindex, &name, &type, &domain, &family, &flags);
1205 if (r < 0)
1206 return r;
1207
1208 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
1209 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
1210
1211 if (isempty(name))
1212 name = NULL;
1213 else if (!dns_service_name_is_valid(name))
1214 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid service name '%s'", name);
1215
1216 if (isempty(type))
1217 type = NULL;
1218 else if (!dns_srv_type_is_valid(type))
1219 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid SRV service type '%s'", type);
1220
1221 r = dns_name_is_valid(domain);
1222 if (r < 0)
1223 return r;
1224 if (r == 0)
1225 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid domain '%s'", domain);
1226
1227 if (name && !type)
1228 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type.");
1229
1230 r = validate_and_mangle_ifindex_and_flags(ifindex, &flags, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS, error);
1231 if (r < 0)
1232 return r;
1233
1234 r = dns_question_new_service(&question_utf8, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), false);
1235 if (r < 0)
1236 return r;
1237
1238 r = dns_question_new_service(&question_idna, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), true);
1239 if (r < 0)
1240 return r;
1241
1242 r = dns_query_new(m, &q, question_utf8, question_idna, ifindex, flags|SD_RESOLVED_NO_SEARCH);
1243 if (r < 0)
1244 return r;
1245
1246 q->bus_request = sd_bus_message_ref(message);
1247 q->request_family = family;
1248 q->complete = bus_method_resolve_service_complete;
1249
1250 r = dns_query_bus_track(q, message);
1251 if (r < 0)
1252 goto fail;
1253
1254 r = dns_query_go(q);
1255 if (r < 0)
1256 goto fail;
1257
1258 return 1;
1259
1260 fail:
1261 dns_query_free(q);
1262 return r;
1263 }
1264
1265 int bus_dns_server_append(sd_bus_message *reply, DnsServer *s, bool with_ifindex, bool extended) {
1266 int r;
1267
1268 assert(reply);
1269
1270 if (!s) {
1271 if (with_ifindex) {
1272 if (extended)
1273 return sd_bus_message_append(reply, "(iiayqs)", 0, AF_UNSPEC, 0, 0, NULL);
1274 else
1275 return sd_bus_message_append(reply, "(iiay)", 0, AF_UNSPEC, 0);
1276 } else {
1277 if (extended)
1278 return sd_bus_message_append(reply, "(iayqs)", AF_UNSPEC, 0, 0, NULL);
1279 else
1280 return sd_bus_message_append(reply, "(iay)", AF_UNSPEC, 0);
1281 }
1282 }
1283
1284 r = sd_bus_message_open_container(reply, 'r', with_ifindex ? (extended ? "iiayqs" : "iiay") : (extended ? "iayqs" : "iay"));
1285 if (r < 0)
1286 return r;
1287
1288 if (with_ifindex) {
1289 r = sd_bus_message_append(reply, "i", dns_server_ifindex(s));
1290 if (r < 0)
1291 return r;
1292 }
1293
1294 r = sd_bus_message_append(reply, "i", s->family);
1295 if (r < 0)
1296 return r;
1297
1298 r = sd_bus_message_append_array(reply, 'y', &s->address, FAMILY_ADDRESS_SIZE(s->family));
1299 if (r < 0)
1300 return r;
1301
1302 if (extended) {
1303 r = sd_bus_message_append(reply, "q", s->port);
1304 if (r < 0)
1305 return r;
1306
1307 r = sd_bus_message_append(reply, "s", s->server_name);
1308 if (r < 0)
1309 return r;
1310 }
1311
1312 return sd_bus_message_close_container(reply);
1313 }
1314
1315 static int bus_property_get_dns_servers_internal(
1316 sd_bus *bus,
1317 const char *path,
1318 const char *interface,
1319 const char *property,
1320 sd_bus_message *reply,
1321 void *userdata,
1322 sd_bus_error *error,
1323 bool extended) {
1324
1325 Manager *m = userdata;
1326 DnsServer *s;
1327 Link *l;
1328 int r;
1329
1330 assert(reply);
1331 assert(m);
1332
1333 r = sd_bus_message_open_container(reply, 'a', extended ? "(iiayqs)" : "(iiay)");
1334 if (r < 0)
1335 return r;
1336
1337 LIST_FOREACH(servers, s, m->dns_servers) {
1338 r = bus_dns_server_append(reply, s, true, extended);
1339 if (r < 0)
1340 return r;
1341 }
1342
1343 HASHMAP_FOREACH(l, m->links)
1344 LIST_FOREACH(servers, s, l->dns_servers) {
1345 r = bus_dns_server_append(reply, s, true, extended);
1346 if (r < 0)
1347 return r;
1348 }
1349
1350 return sd_bus_message_close_container(reply);
1351 }
1352
1353 static int bus_property_get_dns_servers(
1354 sd_bus *bus,
1355 const char *path,
1356 const char *interface,
1357 const char *property,
1358 sd_bus_message *reply,
1359 void *userdata,
1360 sd_bus_error *error) {
1361 return bus_property_get_dns_servers_internal(bus, path, interface, property, reply, userdata, error, false);
1362 }
1363
1364 static int bus_property_get_dns_servers_ex(
1365 sd_bus *bus,
1366 const char *path,
1367 const char *interface,
1368 const char *property,
1369 sd_bus_message *reply,
1370 void *userdata,
1371 sd_bus_error *error) {
1372 return bus_property_get_dns_servers_internal(bus, path, interface, property, reply, userdata, error, true);
1373 }
1374
1375 static int bus_property_get_fallback_dns_servers_internal(
1376 sd_bus *bus,
1377 const char *path,
1378 const char *interface,
1379 const char *property,
1380 sd_bus_message *reply,
1381 void *userdata,
1382 sd_bus_error *error,
1383 bool extended) {
1384
1385 DnsServer *s, **f = userdata;
1386 int r;
1387
1388 assert(reply);
1389 assert(f);
1390
1391 r = sd_bus_message_open_container(reply, 'a', extended ? "(iiayqs)" : "(iiay)");
1392 if (r < 0)
1393 return r;
1394
1395 LIST_FOREACH(servers, s, *f) {
1396 r = bus_dns_server_append(reply, s, true, extended);
1397 if (r < 0)
1398 return r;
1399 }
1400
1401 return sd_bus_message_close_container(reply);
1402 }
1403
1404 static int bus_property_get_fallback_dns_servers(
1405 sd_bus *bus,
1406 const char *path,
1407 const char *interface,
1408 const char *property,
1409 sd_bus_message *reply,
1410 void *userdata,
1411 sd_bus_error *error) {
1412 return bus_property_get_fallback_dns_servers_internal(bus, path, interface, property, reply, userdata, error, false);
1413 }
1414
1415 static int bus_property_get_fallback_dns_servers_ex(
1416 sd_bus *bus,
1417 const char *path,
1418 const char *interface,
1419 const char *property,
1420 sd_bus_message *reply,
1421 void *userdata,
1422 sd_bus_error *error) {
1423 return bus_property_get_fallback_dns_servers_internal(bus, path, interface, property, reply, userdata, error, true);
1424 }
1425
1426 static int bus_property_get_current_dns_server_internal(
1427 sd_bus *bus,
1428 const char *path,
1429 const char *interface,
1430 const char *property,
1431 sd_bus_message *reply,
1432 void *userdata,
1433 sd_bus_error *error,
1434 bool extended) {
1435
1436 DnsServer *s;
1437
1438 assert(reply);
1439 assert(userdata);
1440
1441 s = *(DnsServer **) userdata;
1442
1443 return bus_dns_server_append(reply, s, true, extended);
1444 }
1445
1446 static int bus_property_get_current_dns_server(
1447 sd_bus *bus,
1448 const char *path,
1449 const char *interface,
1450 const char *property,
1451 sd_bus_message *reply,
1452 void *userdata,
1453 sd_bus_error *error) {
1454 return bus_property_get_current_dns_server_internal(bus, path, interface, property, reply, userdata, error, false);
1455 }
1456
1457 static int bus_property_get_current_dns_server_ex(
1458 sd_bus *bus,
1459 const char *path,
1460 const char *interface,
1461 const char *property,
1462 sd_bus_message *reply,
1463 void *userdata,
1464 sd_bus_error *error) {
1465 return bus_property_get_current_dns_server_internal(bus, path, interface, property, reply, userdata, error, true);
1466 }
1467
1468 static int bus_property_get_domains(
1469 sd_bus *bus,
1470 const char *path,
1471 const char *interface,
1472 const char *property,
1473 sd_bus_message *reply,
1474 void *userdata,
1475 sd_bus_error *error) {
1476
1477 Manager *m = userdata;
1478 DnsSearchDomain *d;
1479 Link *l;
1480 int r;
1481
1482 assert(reply);
1483 assert(m);
1484
1485 r = sd_bus_message_open_container(reply, 'a', "(isb)");
1486 if (r < 0)
1487 return r;
1488
1489 LIST_FOREACH(domains, d, m->search_domains) {
1490 r = sd_bus_message_append(reply, "(isb)", 0, d->name, d->route_only);
1491 if (r < 0)
1492 return r;
1493 }
1494
1495 HASHMAP_FOREACH(l, m->links) {
1496 LIST_FOREACH(domains, d, l->search_domains) {
1497 r = sd_bus_message_append(reply, "(isb)", l->ifindex, d->name, d->route_only);
1498 if (r < 0)
1499 return r;
1500 }
1501 }
1502
1503 return sd_bus_message_close_container(reply);
1504 }
1505
1506 static int bus_property_get_transaction_statistics(
1507 sd_bus *bus,
1508 const char *path,
1509 const char *interface,
1510 const char *property,
1511 sd_bus_message *reply,
1512 void *userdata,
1513 sd_bus_error *error) {
1514
1515 Manager *m = userdata;
1516
1517 assert(reply);
1518 assert(m);
1519
1520 return sd_bus_message_append(reply, "(tt)",
1521 (uint64_t) hashmap_size(m->dns_transactions),
1522 (uint64_t) m->n_transactions_total);
1523 }
1524
1525 static int bus_property_get_cache_statistics(
1526 sd_bus *bus,
1527 const char *path,
1528 const char *interface,
1529 const char *property,
1530 sd_bus_message *reply,
1531 void *userdata,
1532 sd_bus_error *error) {
1533
1534 uint64_t size = 0, hit = 0, miss = 0;
1535 Manager *m = userdata;
1536 DnsScope *s;
1537
1538 assert(reply);
1539 assert(m);
1540
1541 LIST_FOREACH(scopes, s, m->dns_scopes) {
1542 size += dns_cache_size(&s->cache);
1543 hit += s->cache.n_hit;
1544 miss += s->cache.n_miss;
1545 }
1546
1547 return sd_bus_message_append(reply, "(ttt)", size, hit, miss);
1548 }
1549
1550 static int bus_property_get_dnssec_statistics(
1551 sd_bus *bus,
1552 const char *path,
1553 const char *interface,
1554 const char *property,
1555 sd_bus_message *reply,
1556 void *userdata,
1557 sd_bus_error *error) {
1558
1559 Manager *m = userdata;
1560
1561 assert(reply);
1562 assert(m);
1563
1564 return sd_bus_message_append(reply, "(tttt)",
1565 (uint64_t) m->n_dnssec_verdict[DNSSEC_SECURE],
1566 (uint64_t) m->n_dnssec_verdict[DNSSEC_INSECURE],
1567 (uint64_t) m->n_dnssec_verdict[DNSSEC_BOGUS],
1568 (uint64_t) m->n_dnssec_verdict[DNSSEC_INDETERMINATE]);
1569 }
1570
1571 static int bus_property_get_ntas(
1572 sd_bus *bus,
1573 const char *path,
1574 const char *interface,
1575 const char *property,
1576 sd_bus_message *reply,
1577 void *userdata,
1578 sd_bus_error *error) {
1579
1580 Manager *m = userdata;
1581 const char *domain;
1582 int r;
1583
1584 assert(reply);
1585 assert(m);
1586
1587 r = sd_bus_message_open_container(reply, 'a', "s");
1588 if (r < 0)
1589 return r;
1590
1591 SET_FOREACH(domain, m->trust_anchor.negative_by_name) {
1592 r = sd_bus_message_append(reply, "s", domain);
1593 if (r < 0)
1594 return r;
1595 }
1596
1597 return sd_bus_message_close_container(reply);
1598 }
1599
1600 static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode, dns_stub_listener_mode, DnsStubListenerMode);
1601 static BUS_DEFINE_PROPERTY_GET(bus_property_get_dnssec_supported, "b", Manager, manager_dnssec_supported);
1602 static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dnssec_mode, "s", Manager, manager_get_dnssec_mode, dnssec_mode_to_string);
1603 static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dns_over_tls_mode, "s", Manager, manager_get_dns_over_tls_mode, dns_over_tls_mode_to_string);
1604
1605 static int bus_method_reset_statistics(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1606 Manager *m = userdata;
1607 DnsScope *s;
1608
1609 assert(message);
1610 assert(m);
1611
1612 LIST_FOREACH(scopes, s, m->dns_scopes)
1613 s->cache.n_hit = s->cache.n_miss = 0;
1614
1615 m->n_transactions_total = 0;
1616 zero(m->n_dnssec_verdict);
1617
1618 return sd_bus_reply_method_return(message, NULL);
1619 }
1620
1621 static int get_any_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) {
1622 Link *l;
1623
1624 assert(m);
1625 assert(ret);
1626
1627 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
1628 if (!l)
1629 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %i not known", ifindex);
1630
1631 *ret = l;
1632 return 0;
1633 }
1634
1635 static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
1636 int ifindex, r;
1637 Link *l;
1638
1639 assert(m);
1640 assert(message);
1641 assert(handler);
1642
1643 r = bus_message_read_ifindex(message, error, &ifindex);
1644 if (r < 0)
1645 return r;
1646
1647 r = get_any_link(m, ifindex, &l, error);
1648 if (r < 0)
1649 return r;
1650
1651 return handler(message, l, error);
1652 }
1653
1654 static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1655 return call_link_method(userdata, message, bus_link_method_set_dns_servers, error);
1656 }
1657
1658 static int bus_method_set_link_dns_servers_ex(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1659 return call_link_method(userdata, message, bus_link_method_set_dns_servers_ex, error);
1660 }
1661
1662 static int bus_method_set_link_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1663 return call_link_method(userdata, message, bus_link_method_set_domains, error);
1664 }
1665
1666 static int bus_method_set_link_default_route(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1667 return call_link_method(userdata, message, bus_link_method_set_default_route, error);
1668 }
1669
1670 static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1671 return call_link_method(userdata, message, bus_link_method_set_llmnr, error);
1672 }
1673
1674 static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1675 return call_link_method(userdata, message, bus_link_method_set_mdns, error);
1676 }
1677
1678 static int bus_method_set_link_dns_over_tls(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1679 return call_link_method(userdata, message, bus_link_method_set_dns_over_tls, error);
1680 }
1681
1682 static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1683 return call_link_method(userdata, message, bus_link_method_set_dnssec, error);
1684 }
1685
1686 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1687 return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error);
1688 }
1689
1690 static int bus_method_revert_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1691 return call_link_method(userdata, message, bus_link_method_revert, error);
1692 }
1693
1694 static int bus_method_get_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1695 _cleanup_free_ char *p = NULL;
1696 Manager *m = userdata;
1697 int r, ifindex;
1698 Link *l;
1699
1700 assert(message);
1701 assert(m);
1702
1703 r = bus_message_read_ifindex(message, error, &ifindex);
1704 if (r < 0)
1705 return r;
1706
1707 r = get_any_link(m, ifindex, &l, error);
1708 if (r < 0)
1709 return r;
1710
1711 p = link_bus_path(l);
1712 if (!p)
1713 return -ENOMEM;
1714
1715 return sd_bus_reply_method_return(message, "o", p);
1716 }
1717
1718 static int bus_method_flush_caches(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1719 Manager *m = userdata;
1720
1721 assert(message);
1722 assert(m);
1723
1724 manager_flush_caches(m);
1725
1726 return sd_bus_reply_method_return(message, NULL);
1727 }
1728
1729 static int bus_method_reset_server_features(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1730 Manager *m = userdata;
1731
1732 assert(message);
1733 assert(m);
1734
1735 manager_reset_server_features(m);
1736
1737 return sd_bus_reply_method_return(message, NULL);
1738 }
1739
1740 static int dnssd_service_on_bus_track(sd_bus_track *t, void *userdata) {
1741 DnssdService *s = userdata;
1742
1743 assert(t);
1744 assert(s);
1745
1746 log_debug("Client of active request vanished, destroying DNS-SD service.");
1747 dnssd_service_free(s);
1748
1749 return 0;
1750 }
1751
1752 static int bus_method_register_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1753 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1754 _cleanup_(dnssd_service_freep) DnssdService *service = NULL;
1755 _cleanup_(sd_bus_track_unrefp) sd_bus_track *bus_track = NULL;
1756 _cleanup_free_ char *path = NULL;
1757 Manager *m = userdata;
1758 DnssdService *s = NULL;
1759 const char *name;
1760 const char *name_template;
1761 const char *type;
1762 uid_t euid;
1763 int r;
1764
1765 assert(message);
1766 assert(m);
1767
1768 if (m->mdns_support != RESOLVE_SUPPORT_YES)
1769 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Support for MulticastDNS is disabled");
1770
1771 service = new0(DnssdService, 1);
1772 if (!service)
1773 return log_oom();
1774
1775 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1776 if (r < 0)
1777 return r;
1778
1779 r = sd_bus_creds_get_euid(creds, &euid);
1780 if (r < 0)
1781 return r;
1782 service->originator = euid;
1783
1784 r = sd_bus_message_read(message, "sssqqq", &name, &name_template, &type,
1785 &service->port, &service->priority,
1786 &service->weight);
1787 if (r < 0)
1788 return r;
1789
1790 s = hashmap_get(m->dnssd_services, name);
1791 if (s)
1792 return sd_bus_error_setf(error, BUS_ERROR_DNSSD_SERVICE_EXISTS, "DNS-SD service '%s' exists already", name);
1793
1794 if (!dnssd_srv_type_is_valid(type))
1795 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DNS-SD service type '%s' is invalid", type);
1796
1797 r = dnssd_render_instance_name(name_template, NULL);
1798 if (r < 0)
1799 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DNS-SD service name '%s' is invalid", name_template);
1800
1801 service->name = strdup(name);
1802 if (!service->name)
1803 return log_oom();
1804
1805 service->name_template = strdup(name_template);
1806 if (!service->name_template)
1807 return log_oom();
1808
1809 service->type = strdup(type);
1810 if (!service->type)
1811 return log_oom();
1812
1813 r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "a{say}");
1814 if (r < 0)
1815 return r;
1816
1817 while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "{say}")) > 0) {
1818 _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL;
1819 DnsTxtItem *last = NULL;
1820
1821 txt_data = new0(DnssdTxtData, 1);
1822 if (!txt_data)
1823 return log_oom();
1824
1825 while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_DICT_ENTRY, "say")) > 0) {
1826 const char *key;
1827 const void *value;
1828 size_t size;
1829 DnsTxtItem *i;
1830
1831 r = sd_bus_message_read(message, "s", &key);
1832 if (r < 0)
1833 return r;
1834
1835 if (isempty(key))
1836 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Keys in DNS-SD TXT RRs can't be empty");
1837
1838 if (!ascii_is_valid(key))
1839 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "TXT key '%s' contains non-ASCII symbols", key);
1840
1841 r = sd_bus_message_read_array(message, 'y', &value, &size);
1842 if (r < 0)
1843 return r;
1844
1845 r = dnssd_txt_item_new_from_data(key, value, size, &i);
1846 if (r < 0)
1847 return r;
1848
1849 LIST_INSERT_AFTER(items, txt_data->txt, last, i);
1850 last = i;
1851
1852 r = sd_bus_message_exit_container(message);
1853 if (r < 0)
1854 return r;
1855
1856 }
1857 if (r < 0)
1858 return r;
1859
1860 r = sd_bus_message_exit_container(message);
1861 if (r < 0)
1862 return r;
1863
1864 if (txt_data->txt) {
1865 LIST_PREPEND(items, service->txt_data_items, txt_data);
1866 txt_data = NULL;
1867 }
1868 }
1869 if (r < 0)
1870 return r;
1871
1872 r = sd_bus_message_exit_container(message);
1873 if (r < 0)
1874 return r;
1875
1876 if (!service->txt_data_items) {
1877 _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL;
1878
1879 txt_data = new0(DnssdTxtData, 1);
1880 if (!txt_data)
1881 return log_oom();
1882
1883 r = dns_txt_item_new_empty(&txt_data->txt);
1884 if (r < 0)
1885 return r;
1886
1887 LIST_PREPEND(items, service->txt_data_items, txt_data);
1888 txt_data = NULL;
1889 }
1890
1891 r = sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service->name, &path);
1892 if (r < 0)
1893 return r;
1894
1895 r = bus_verify_polkit_async(message, CAP_SYS_ADMIN,
1896 "org.freedesktop.resolve1.register-service",
1897 NULL, false, UID_INVALID,
1898 &m->polkit_registry, error);
1899 if (r < 0)
1900 return r;
1901 if (r == 0)
1902 return 1; /* Polkit will call us back */
1903
1904 r = hashmap_ensure_allocated(&m->dnssd_services, &string_hash_ops);
1905 if (r < 0)
1906 return r;
1907
1908 r = hashmap_put(m->dnssd_services, service->name, service);
1909 if (r < 0)
1910 return r;
1911
1912 r = sd_bus_track_new(sd_bus_message_get_bus(message), &bus_track, dnssd_service_on_bus_track, service);
1913 if (r < 0)
1914 return r;
1915
1916 r = sd_bus_track_add_sender(bus_track, message);
1917 if (r < 0)
1918 return r;
1919
1920 service->manager = m;
1921
1922 service = NULL;
1923
1924 manager_refresh_rrs(m);
1925
1926 return sd_bus_reply_method_return(message, "o", path);
1927 }
1928
1929 static int call_dnssd_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
1930 _cleanup_free_ char *name = NULL;
1931 DnssdService *s = NULL;
1932 const char *path;
1933 int r;
1934
1935 assert(m);
1936 assert(message);
1937 assert(handler);
1938
1939 r = sd_bus_message_read(message, "o", &path);
1940 if (r < 0)
1941 return r;
1942
1943 r = sd_bus_path_decode(path, "/org/freedesktop/resolve1/dnssd", &name);
1944 if (r == 0)
1945 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DNSSD_SERVICE, "DNS-SD service with object path '%s' does not exist", path);
1946 if (r < 0)
1947 return r;
1948
1949 s = hashmap_get(m->dnssd_services, name);
1950 if (!s)
1951 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DNSSD_SERVICE, "DNS-SD service '%s' not known", name);
1952
1953 return handler(message, s, error);
1954 }
1955
1956 static int bus_method_unregister_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1957 Manager *m = userdata;
1958
1959 assert(message);
1960 assert(m);
1961
1962 return call_dnssd_method(m, message, bus_dnssd_method_unregister, error);
1963 }
1964
1965 static const sd_bus_vtable resolve_vtable[] = {
1966 SD_BUS_VTABLE_START(0),
1967 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL, offsetof(Manager, llmnr_hostname), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1968 SD_BUS_PROPERTY("LLMNR", "s", bus_property_get_resolve_support, offsetof(Manager, llmnr_support), 0),
1969 SD_BUS_PROPERTY("MulticastDNS", "s", bus_property_get_resolve_support, offsetof(Manager, mdns_support), 0),
1970 SD_BUS_PROPERTY("DNSOverTLS", "s", bus_property_get_dns_over_tls_mode, 0, 0),
1971 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1972 SD_BUS_PROPERTY("DNSEx", "a(iiayqs)", bus_property_get_dns_servers_ex, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1973 SD_BUS_PROPERTY("FallbackDNS", "a(iiay)", bus_property_get_fallback_dns_servers, offsetof(Manager, fallback_dns_servers), SD_BUS_VTABLE_PROPERTY_CONST),
1974 SD_BUS_PROPERTY("FallbackDNSEx", "a(iiayqs)", bus_property_get_fallback_dns_servers_ex, offsetof(Manager, fallback_dns_servers), SD_BUS_VTABLE_PROPERTY_CONST),
1975 SD_BUS_PROPERTY("CurrentDNSServer", "(iiay)", bus_property_get_current_dns_server, offsetof(Manager, current_dns_server), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1976 SD_BUS_PROPERTY("CurrentDNSServerEx", "(iiayqs)", bus_property_get_current_dns_server_ex, offsetof(Manager, current_dns_server), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1977 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains, 0, 0),
1978 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics, 0, 0),
1979 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics, 0, 0),
1980 SD_BUS_PROPERTY("DNSSEC", "s", bus_property_get_dnssec_mode, 0, 0),
1981 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics, 0, 0),
1982 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported, 0, 0),
1983 SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_ntas, 0, 0),
1984 SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode, offsetof(Manager, dns_stub_listener_mode), 0),
1985
1986 SD_BUS_METHOD_WITH_ARGS("ResolveHostname",
1987 SD_BUS_ARGS("i", ifindex, "s", name, "i", family, "t", flags),
1988 SD_BUS_RESULT("a(iiay)", addresses, "s", canonical, "t", flags),
1989 bus_method_resolve_hostname,
1990 SD_BUS_VTABLE_UNPRIVILEGED),
1991 SD_BUS_METHOD_WITH_ARGS("ResolveAddress",
1992 SD_BUS_ARGS("i", ifindex, "i", family, "ay", address, "t", flags),
1993 SD_BUS_RESULT("a(is)", names, "t", flags),
1994 bus_method_resolve_address,
1995 SD_BUS_VTABLE_UNPRIVILEGED),
1996 SD_BUS_METHOD_WITH_ARGS("ResolveRecord",
1997 SD_BUS_ARGS("i", ifindex, "s", name, "q", class, "q", type, "t", flags),
1998 SD_BUS_RESULT("a(iqqay)", records, "t", flags),
1999 bus_method_resolve_record,
2000 SD_BUS_VTABLE_UNPRIVILEGED),
2001 SD_BUS_METHOD_WITH_ARGS("ResolveService",
2002 SD_BUS_ARGS("i", ifindex,
2003 "s", name,
2004 "s", type,
2005 "s", domain,
2006 "i", family,
2007 "t", flags),
2008 SD_BUS_RESULT("a(qqqsa(iiay)s)", srv_data,
2009 "aay", txt_data,
2010 "s", canonical_name,
2011 "s", canonical_type,
2012 "s", canonical_domain,
2013 "t", flags),
2014 bus_method_resolve_service,
2015 SD_BUS_VTABLE_UNPRIVILEGED),
2016 SD_BUS_METHOD_WITH_ARGS("GetLink",
2017 SD_BUS_ARGS("i", ifindex),
2018 SD_BUS_RESULT("o", path),
2019 bus_method_get_link,
2020 SD_BUS_VTABLE_UNPRIVILEGED),
2021 SD_BUS_METHOD_WITH_ARGS("SetLinkDNS",
2022 SD_BUS_ARGS("i", ifindex, "a(iay)", addresses),
2023 SD_BUS_NO_RESULT,
2024 bus_method_set_link_dns_servers,
2025 SD_BUS_VTABLE_UNPRIVILEGED),
2026 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSEx",
2027 SD_BUS_ARGS("i", ifindex, "a(iayqs)", addresses),
2028 SD_BUS_NO_RESULT,
2029 bus_method_set_link_dns_servers_ex,
2030 SD_BUS_VTABLE_UNPRIVILEGED),
2031 SD_BUS_METHOD_WITH_ARGS("SetLinkDomains",
2032 SD_BUS_ARGS("i", ifindex, "a(sb)", domains),
2033 SD_BUS_NO_RESULT,
2034 bus_method_set_link_domains,
2035 SD_BUS_VTABLE_UNPRIVILEGED),
2036 SD_BUS_METHOD_WITH_ARGS("SetLinkDefaultRoute",
2037 SD_BUS_ARGS("i", ifindex, "b", enable),
2038 SD_BUS_NO_RESULT,
2039 bus_method_set_link_default_route,
2040 SD_BUS_VTABLE_UNPRIVILEGED),
2041 SD_BUS_METHOD_WITH_ARGS("SetLinkLLMNR",
2042 SD_BUS_ARGS("i", ifindex, "s", mode),
2043 SD_BUS_NO_RESULT,
2044 bus_method_set_link_llmnr,
2045 SD_BUS_VTABLE_UNPRIVILEGED),
2046 SD_BUS_METHOD_WITH_ARGS("SetLinkMulticastDNS",
2047 SD_BUS_ARGS("i", ifindex, "s", mode),
2048 SD_BUS_NO_RESULT,
2049 bus_method_set_link_mdns,
2050 SD_BUS_VTABLE_UNPRIVILEGED),
2051 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSOverTLS",
2052 SD_BUS_ARGS("i", ifindex, "s", mode),
2053 SD_BUS_NO_RESULT,
2054 bus_method_set_link_dns_over_tls,
2055 SD_BUS_VTABLE_UNPRIVILEGED),
2056 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSEC",
2057 SD_BUS_ARGS("i", ifindex, "s", mode),
2058 SD_BUS_NO_RESULT,
2059 bus_method_set_link_dnssec,
2060 SD_BUS_VTABLE_UNPRIVILEGED),
2061 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSECNegativeTrustAnchors",
2062 SD_BUS_ARGS("i", ifindex, "as", names),
2063 SD_BUS_NO_RESULT,
2064 bus_method_set_link_dnssec_negative_trust_anchors,
2065 SD_BUS_VTABLE_UNPRIVILEGED),
2066 SD_BUS_METHOD_WITH_ARGS("RevertLink",
2067 SD_BUS_ARGS("i", ifindex),
2068 SD_BUS_NO_RESULT,
2069 bus_method_revert_link,
2070 SD_BUS_VTABLE_UNPRIVILEGED),
2071 SD_BUS_METHOD_WITH_ARGS("RegisterService",
2072 SD_BUS_ARGS("s", name,
2073 "s", name_template,
2074 "s", type,
2075 "q", service_port,
2076 "q", service_priority,
2077 "q", service_weight,
2078 "aa{say}", txt_datas),
2079 SD_BUS_RESULT("o", service_path),
2080 bus_method_register_service,
2081 SD_BUS_VTABLE_UNPRIVILEGED),
2082 SD_BUS_METHOD_WITH_ARGS("UnregisterService",
2083 SD_BUS_ARGS("o", service_path),
2084 SD_BUS_NO_RESULT,
2085 bus_method_unregister_service,
2086 SD_BUS_VTABLE_UNPRIVILEGED),
2087 SD_BUS_METHOD_WITH_ARGS("ResetStatistics",
2088 SD_BUS_NO_ARGS,
2089 SD_BUS_NO_RESULT,
2090 bus_method_reset_statistics,
2091 SD_BUS_VTABLE_UNPRIVILEGED),
2092 SD_BUS_METHOD_WITH_ARGS("FlushCaches",
2093 SD_BUS_NO_ARGS,
2094 SD_BUS_NO_RESULT,
2095 bus_method_flush_caches,
2096 SD_BUS_VTABLE_UNPRIVILEGED),
2097 SD_BUS_METHOD_WITH_ARGS("ResetServerFeatures",
2098 SD_BUS_NO_ARGS,
2099 SD_BUS_NO_RESULT,
2100 bus_method_reset_server_features,
2101 SD_BUS_VTABLE_UNPRIVILEGED),
2102
2103 SD_BUS_VTABLE_END,
2104 };
2105
2106 const BusObjectImplementation manager_object = {
2107 "/org/freedesktop/resolve1",
2108 "org.freedesktop.resolve1.Manager",
2109 .vtables = BUS_VTABLES(resolve_vtable),
2110 .children = BUS_IMPLEMENTATIONS(&link_object,
2111 &dnssd_object),
2112 };
2113
2114 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
2115 Manager *m = userdata;
2116 int b, r;
2117
2118 assert(message);
2119 assert(m);
2120
2121 r = sd_bus_message_read(message, "b", &b);
2122 if (r < 0) {
2123 bus_log_parse_error(r);
2124 return 0;
2125 }
2126
2127 if (b)
2128 return 0;
2129
2130 log_debug("Coming back from suspend, verifying all RRs...");
2131
2132 manager_verify_all(m);
2133 return 0;
2134 }
2135
2136 int manager_connect_bus(Manager *m) {
2137 int r;
2138
2139 assert(m);
2140
2141 if (m->bus)
2142 return 0;
2143
2144 r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-resolve");
2145 if (r < 0)
2146 return log_error_errno(r, "Failed to connect to system bus: %m");
2147
2148 r = bus_add_implementation(m->bus, &manager_object, m);
2149 if (r < 0)
2150 return r;
2151
2152 r = bus_log_control_api_register(m->bus);
2153 if (r < 0)
2154 return r;
2155
2156 r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.resolve1", 0, NULL, NULL);
2157 if (r < 0)
2158 return log_error_errno(r, "Failed to request name: %m");
2159
2160 r = sd_bus_attach_event(m->bus, m->event, 0);
2161 if (r < 0)
2162 return log_error_errno(r, "Failed to attach bus to event loop: %m");
2163
2164 r = sd_bus_match_signal_async(
2165 m->bus,
2166 NULL,
2167 "org.freedesktop.login1",
2168 "/org/freedesktop/login1",
2169 "org.freedesktop.login1.Manager",
2170 "PrepareForSleep",
2171 match_prepare_for_sleep,
2172 NULL,
2173 m);
2174 if (r < 0)
2175 log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m");
2176
2177 return 0;
2178 }
2179
2180 int _manager_send_changed(Manager *manager, const char *property, ...) {
2181 assert(manager);
2182
2183 char **l = strv_from_stdarg_alloca(property);
2184
2185 int r = sd_bus_emit_properties_changed_strv(
2186 manager->bus,
2187 "/org/freedesktop/resolve1",
2188 "org.freedesktop.resolve1.Manager",
2189 l);
2190 if (r < 0)
2191 log_notice_errno(r, "Failed to emit notification about changed property %s: %m", property);
2192 return r;
2193 }