1:255.16-alt1
[systemd_ALT.git] / src / resolve / resolved-bus.c
blobec9f4bcde2450e4eb049e8be48e031963b4c4f0a
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
4 #include "bus-common-errors.h"
5 #include "bus-get-properties.h"
6 #include "bus-locator.h"
7 #include "bus-log-control-api.h"
8 #include "bus-message-util.h"
9 #include "bus-polkit.h"
10 #include "dns-domain.h"
11 #include "format-util.h"
12 #include "memory-util.h"
13 #include "missing_capability.h"
14 #include "resolved-bus.h"
15 #include "resolved-def.h"
16 #include "resolved-dns-stream.h"
17 #include "resolved-dns-synthesize.h"
18 #include "resolved-dnssd-bus.h"
19 #include "resolved-dnssd.h"
20 #include "resolved-link-bus.h"
21 #include "resolved-resolv-conf.h"
22 #include "socket-netlink.h"
23 #include "stdio-util.h"
24 #include "strv.h"
25 #include "syslog-util.h"
26 #include "user-util.h"
27 #include "utf8.h"
29 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_resolve_support, resolve_support, ResolveSupport);
31 static int query_on_bus_track(sd_bus_track *t, void *userdata) {
32 DnsQuery *q = ASSERT_PTR(userdata);
34 assert(t);
36 if (!DNS_TRANSACTION_IS_LIVE(q->state))
37 return 0;
39 log_debug("Client of active query vanished, aborting query.");
40 dns_query_complete(q, DNS_TRANSACTION_ABORTED);
41 return 0;
44 static int dns_query_bus_track(DnsQuery *q, sd_bus_message *m) {
45 int r;
47 assert(q);
48 assert(m);
50 if (!q->bus_track) {
51 r = sd_bus_track_new(sd_bus_message_get_bus(m), &q->bus_track, query_on_bus_track, q);
52 if (r < 0)
53 return r;
56 r = sd_bus_track_add_sender(q->bus_track, m);
57 if (r < 0)
58 return r;
60 return 0;
63 static sd_bus_message *dns_query_steal_request(DnsQuery *q) {
64 assert(q);
66 /* Find the main query, it's the one that owns the message */
67 while (q->auxiliary_for)
68 q = q->auxiliary_for;
70 /* Let's take the request message out of the DnsQuery object, so that we never send requests twice */
71 return TAKE_PTR(q->bus_request);
74 _sd_printf_(3, 4) static int reply_method_errorf(
75 DnsQuery *query,
76 const char *error_name,
77 const char *format,
78 ...) {
80 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
81 va_list ap;
82 int r;
84 assert(query);
85 assert(format);
87 req = dns_query_steal_request(query);
88 if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
89 return 0;
91 va_start(ap, format);
92 r = sd_bus_reply_method_errorfv(req, error_name, format, ap);
93 va_end(ap);
95 return r;
98 _sd_printf_(3, 4) static int reply_method_errnof(
99 DnsQuery *query,
100 int err,
101 const char *format,
102 ...) {
104 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
105 int r;
107 assert(query);
109 req = dns_query_steal_request(query);
110 if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
111 return 0;
113 if (format) {
114 va_list ap;
116 va_start(ap, format);
117 r = sd_bus_reply_method_errnofv(req, err, format, ap);
118 va_end(ap);
119 } else
120 r = sd_bus_reply_method_errno(req, err, NULL);
122 return r;
125 static int reply_query_state(DnsQuery *q) {
126 assert(q);
128 switch (q->state) {
130 case DNS_TRANSACTION_NO_SERVERS:
131 return reply_method_errorf(q, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
133 case DNS_TRANSACTION_TIMEOUT:
134 return reply_method_errorf(q, SD_BUS_ERROR_TIMEOUT, "Query timed out");
136 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
137 return reply_method_errorf(q, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
139 case DNS_TRANSACTION_INVALID_REPLY:
140 return reply_method_errorf(q, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
142 case DNS_TRANSACTION_ERRNO:
143 return reply_method_errnof(q, q->answer_errno, "Lookup failed due to system error: %m");
145 case DNS_TRANSACTION_ABORTED:
146 return reply_method_errorf(q, BUS_ERROR_ABORTED, "Query aborted");
148 case DNS_TRANSACTION_DNSSEC_FAILED:
149 return reply_method_errorf(q, BUS_ERROR_DNSSEC_FAILED, "DNSSEC validation failed: %s",
150 dnssec_result_to_string(q->answer_dnssec_result));
152 case DNS_TRANSACTION_NO_TRUST_ANCHOR:
153 return reply_method_errorf(q, BUS_ERROR_NO_TRUST_ANCHOR, "No suitable trust anchor known");
155 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED:
156 return reply_method_errorf(q, BUS_ERROR_RR_TYPE_UNSUPPORTED, "Server does not support requested resource record type");
158 case DNS_TRANSACTION_NETWORK_DOWN:
159 return reply_method_errorf(q, BUS_ERROR_NETWORK_DOWN, "Network is down");
161 case DNS_TRANSACTION_NOT_FOUND:
162 /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
163 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
164 return reply_method_errorf(q, BUS_ERROR_DNS_NXDOMAIN, "'%s' not found", dns_query_string(q));
166 case DNS_TRANSACTION_NO_SOURCE:
167 return reply_method_errorf(q, BUS_ERROR_NO_SOURCE, "All suitable resolution sources turned off");
169 case DNS_TRANSACTION_STUB_LOOP:
170 return reply_method_errorf(q, BUS_ERROR_STUB_LOOP, "Configured DNS server loops back to us");
172 case DNS_TRANSACTION_RCODE_FAILURE: {
173 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
174 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
176 req = dns_query_steal_request(q);
177 if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
178 return 0;
180 if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
181 sd_bus_error_setf(&error, BUS_ERROR_DNS_NXDOMAIN, "Name '%s' not found", dns_query_string(q));
182 else {
183 const char *rc, *n;
185 rc = FORMAT_DNS_RCODE(q->answer_rcode);
186 n = strjoina(_BUS_ERROR_DNS, rc);
187 sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", dns_query_string(q), rc);
190 return sd_bus_reply_method_error(req, &error);
193 case DNS_TRANSACTION_NULL:
194 case DNS_TRANSACTION_PENDING:
195 case DNS_TRANSACTION_VALIDATING:
196 case DNS_TRANSACTION_SUCCESS:
197 default:
198 assert_not_reached();
202 static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
203 int r;
205 assert(reply);
206 assert(rr);
208 r = sd_bus_message_open_container(reply, 'r', "iiay");
209 if (r < 0)
210 return r;
212 r = sd_bus_message_append(reply, "i", ifindex);
213 if (r < 0)
214 return r;
216 if (rr->key->type == DNS_TYPE_A) {
217 r = sd_bus_message_append(reply, "i", AF_INET);
218 if (r < 0)
219 return r;
221 r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
223 } else if (rr->key->type == DNS_TYPE_AAAA) {
224 r = sd_bus_message_append(reply, "i", AF_INET6);
225 if (r < 0)
226 return r;
228 r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
229 } else
230 return -EAFNOSUPPORT;
232 if (r < 0)
233 return r;
235 r = sd_bus_message_close_container(reply);
236 if (r < 0)
237 return r;
239 return 0;
242 static void bus_method_resolve_hostname_complete(DnsQuery *query) {
243 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
244 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
245 _cleanup_(dns_query_freep) DnsQuery *q = query;
246 _cleanup_free_ char *normalized = NULL;
247 DnsQuestion *question;
248 DnsResourceRecord *rr;
249 unsigned added = 0;
250 int ifindex, r;
252 assert(q);
254 if (q->state != DNS_TRANSACTION_SUCCESS) {
255 r = reply_query_state(q);
256 goto finish;
259 r = dns_query_process_cname_many(q);
260 if (r == -ELOOP) {
261 r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
262 goto finish;
264 if (r < 0)
265 goto finish;
266 if (r == DNS_QUERY_CNAME) {
267 /* This was a cname, and the query was restarted. */
268 TAKE_PTR(q);
269 return;
272 r = sd_bus_message_new_method_return(q->bus_request, &reply);
273 if (r < 0)
274 goto finish;
276 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
277 if (r < 0)
278 goto finish;
280 question = dns_query_question_for_protocol(q, q->answer_protocol);
282 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
284 r = dns_question_matches_rr(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
285 if (r < 0)
286 goto finish;
287 if (r == 0)
288 continue;
290 r = append_address(reply, rr, ifindex);
291 if (r < 0)
292 goto finish;
294 if (!canonical)
295 canonical = dns_resource_record_ref(rr);
297 added++;
300 if (added <= 0) {
301 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
302 goto finish;
305 r = sd_bus_message_close_container(reply);
306 if (r < 0)
307 goto finish;
309 /* The key names are not necessarily normalized, make sure that they are when we return them to our
310 * bus clients. */
311 assert(canonical);
312 r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
313 if (r < 0)
314 goto finish;
316 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
317 r = sd_bus_message_append(
318 reply, "st",
319 normalized,
320 dns_query_reply_flags_make(q));
321 if (r < 0)
322 goto finish;
324 q->bus_request = sd_bus_message_unref(q->bus_request);
325 r = sd_bus_send(q->manager->bus, reply, NULL);
327 finish:
328 if (r < 0) {
329 log_error_errno(r, "Failed to send hostname reply: %m");
330 (void) reply_method_errnof(q, r, NULL);
334 static int validate_and_mangle_flags(
335 const char *name,
336 uint64_t *flags,
337 uint64_t ok,
338 sd_bus_error *error) {
340 assert(flags);
342 /* Checks that the client supplied interface index and flags parameter actually are valid and make
343 * sense in our method call context. Specifically:
345 * 1. Checks that the interface index is either 0 (meaning *all* interfaces) or positive
347 * 2. Only the protocols flags and a bunch of NO_XYZ flags are set, at most. Plus additional flags
348 * specific to our method, passed in the "ok" parameter.
350 * 3. If zero protocol flags are specified it is automatically turned into *all* protocols. This way
351 * clients can simply pass 0 as flags and all will work as it should. They can also use this so
352 * that clients don't have to know all the protocols resolved implements, but can just specify 0
353 * to mean "all supported protocols".
356 if (*flags & ~(SD_RESOLVED_PROTOCOLS_ALL|
357 SD_RESOLVED_NO_CNAME|
358 SD_RESOLVED_NO_VALIDATE|
359 SD_RESOLVED_NO_SYNTHESIZE|
360 SD_RESOLVED_NO_CACHE|
361 SD_RESOLVED_NO_ZONE|
362 SD_RESOLVED_NO_TRUST_ANCHOR|
363 SD_RESOLVED_NO_NETWORK|
364 SD_RESOLVED_NO_STALE|
365 ok))
366 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
368 if ((*flags & SD_RESOLVED_PROTOCOLS_ALL) == 0) /* If no protocol is enabled, enable all */
369 *flags |= SD_RESOLVED_PROTOCOLS_ALL;
371 /* Imply SD_RESOLVED_NO_SEARCH if permitted and name is dot suffixed. */
372 if (name && FLAGS_SET(ok, SD_RESOLVED_NO_SEARCH) && dns_name_dot_suffixed(name) > 0)
373 *flags |= SD_RESOLVED_NO_SEARCH;
375 return 0;
378 static int parse_as_address(sd_bus_message *m, int ifindex, const char *hostname, int family, uint64_t flags) {
379 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
380 _cleanup_free_ char *canonical = NULL;
381 union in_addr_union parsed;
382 int r, ff, parsed_ifindex = 0;
384 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
385 * let's not attempt to look it up. */
387 r = in_addr_ifindex_from_string_auto(hostname, &ff, &parsed, &parsed_ifindex);
388 if (r < 0) /* not an address */
389 return 0;
391 if (family != AF_UNSPEC && ff != family)
392 return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address is not of the requested family.");
393 if (ifindex > 0 && parsed_ifindex > 0 && parsed_ifindex != ifindex)
394 return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address interface index does not match requested interface.");
396 if (parsed_ifindex > 0)
397 ifindex = parsed_ifindex;
399 r = sd_bus_message_new_method_return(m, &reply);
400 if (r < 0)
401 return r;
403 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
404 if (r < 0)
405 return r;
407 r = sd_bus_message_open_container(reply, 'r', "iiay");
408 if (r < 0)
409 return r;
411 r = sd_bus_message_append(reply, "ii", ifindex, ff);
412 if (r < 0)
413 return r;
415 r = sd_bus_message_append_array(reply, 'y', &parsed, FAMILY_ADDRESS_SIZE(ff));
416 if (r < 0)
417 return r;
419 r = sd_bus_message_close_container(reply);
420 if (r < 0)
421 return r;
423 r = sd_bus_message_close_container(reply);
424 if (r < 0)
425 return r;
427 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
428 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
429 * omissions are always done the same way). */
430 r = in_addr_ifindex_to_string(ff, &parsed, ifindex, &canonical);
431 if (r < 0)
432 return r;
434 r = sd_bus_message_append(reply, "st", canonical,
435 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags), ff, true, true) |
436 SD_RESOLVED_SYNTHETIC);
437 if (r < 0)
438 return r;
440 return sd_bus_send(sd_bus_message_get_bus(m), reply, NULL);
443 void bus_client_log(sd_bus_message *m, const char *what) {
444 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
445 const char *comm = NULL;
446 uid_t uid = UID_INVALID;
447 pid_t pid = 0;
448 int r;
450 assert(m);
451 assert(what);
453 if (!DEBUG_LOGGING)
454 return;
456 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_COMM|SD_BUS_CREDS_AUGMENT, &creds);
457 if (r < 0)
458 return (void) log_debug_errno(r, "Failed to query client credentials, ignoring: %m");
460 (void) sd_bus_creds_get_uid(creds, &uid);
461 (void) sd_bus_creds_get_pid(creds, &pid);
462 (void) sd_bus_creds_get_comm(creds, &comm);
464 log_debug("D-Bus %s request from client PID " PID_FMT " (%s) with UID " UID_FMT,
465 what, pid, strna(comm), uid);
468 static int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
469 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
470 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
471 Manager *m = ASSERT_PTR(userdata);
472 const char *hostname;
473 int family, ifindex;
474 uint64_t flags;
475 int r;
477 assert(message);
479 assert_cc(sizeof(int) == sizeof(int32_t));
481 r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
482 if (r < 0)
483 return r;
485 if (ifindex < 0)
486 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
488 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
489 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
491 r = validate_and_mangle_flags(hostname, &flags, SD_RESOLVED_NO_SEARCH, error);
492 if (r < 0)
493 return r;
495 r = parse_as_address(message, ifindex, hostname, family, flags);
496 if (r != 0)
497 return r;
499 r = dns_name_is_valid(hostname);
500 if (r < 0)
501 return r;
502 if (r == 0)
503 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
505 r = dns_question_new_address(&question_utf8, family, hostname, false);
506 if (r < 0)
507 return r;
509 r = dns_question_new_address(&question_idna, family, hostname, true);
510 if (r < 0 && r != -EALREADY)
511 return r;
513 bus_client_log(message, "hostname resolution");
515 r = dns_query_new(m, &q, question_utf8, question_idna ?: question_utf8, NULL, ifindex, flags);
516 if (r < 0)
517 return r;
519 q->bus_request = sd_bus_message_ref(message);
520 q->request_family = family;
521 q->complete = bus_method_resolve_hostname_complete;
523 r = dns_query_bus_track(q, message);
524 if (r < 0)
525 return r;
527 r = dns_query_go(q);
528 if (r < 0)
529 return r;
531 TAKE_PTR(q);
532 return 1;
535 static void bus_method_resolve_address_complete(DnsQuery *query) {
536 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
537 _cleanup_(dns_query_freep) DnsQuery *q = query;
538 DnsQuestion *question;
539 DnsResourceRecord *rr;
540 unsigned added = 0;
541 int ifindex, r;
543 assert(q);
545 if (q->state != DNS_TRANSACTION_SUCCESS) {
546 r = reply_query_state(q);
547 goto finish;
550 r = dns_query_process_cname_many(q);
551 if (r == -ELOOP) {
552 r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
553 goto finish;
555 if (r < 0)
556 goto finish;
557 if (r == DNS_QUERY_CNAME) {
558 /* This was a cname, and the query was restarted. */
559 TAKE_PTR(q);
560 return;
563 r = sd_bus_message_new_method_return(q->bus_request, &reply);
564 if (r < 0)
565 goto finish;
567 r = sd_bus_message_open_container(reply, 'a', "(is)");
568 if (r < 0)
569 goto finish;
571 question = dns_query_question_for_protocol(q, q->answer_protocol);
573 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
574 _cleanup_free_ char *normalized = NULL;
576 r = dns_question_matches_rr(question, rr, NULL);
577 if (r < 0)
578 goto finish;
579 if (r == 0)
580 continue;
582 r = dns_name_normalize(rr->ptr.name, 0, &normalized);
583 if (r < 0)
584 goto finish;
586 r = sd_bus_message_append(reply, "(is)", ifindex, normalized);
587 if (r < 0)
588 goto finish;
590 added++;
593 if (added <= 0) {
594 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR,
595 "Address %s does not have any RR of requested type",
596 IN_ADDR_TO_STRING(q->request_family, &q->request_address));
597 goto finish;
600 r = sd_bus_message_close_container(reply);
601 if (r < 0)
602 goto finish;
604 r = sd_bus_message_append(reply, "t", dns_query_reply_flags_make(q));
605 if (r < 0)
606 goto finish;
608 q->bus_request = sd_bus_message_unref(q->bus_request);
609 r = sd_bus_send(q->manager->bus, reply, NULL);
611 finish:
612 if (r < 0) {
613 log_error_errno(r, "Failed to send address reply: %m");
614 (void) reply_method_errnof(q, r, NULL);
618 static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
619 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
620 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
621 Manager *m = ASSERT_PTR(userdata);
622 union in_addr_union a;
623 int family, ifindex;
624 uint64_t flags;
625 int r;
627 assert(message);
629 assert_cc(sizeof(int) == sizeof(int32_t));
631 r = sd_bus_message_read(message, "i", &ifindex);
632 if (r < 0)
633 return r;
635 r = bus_message_read_in_addr_auto(message, error, &family, &a);
636 if (r < 0)
637 return r;
639 r = sd_bus_message_read(message, "t", &flags);
640 if (r < 0)
641 return r;
643 if (ifindex < 0)
644 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
646 r = validate_and_mangle_flags(NULL, &flags, 0, error);
647 if (r < 0)
648 return r;
650 r = dns_question_new_reverse(&question, family, &a);
651 if (r < 0)
652 return r;
654 bus_client_log(message, "address resolution");
656 r = dns_query_new(m, &q, question, question, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH);
657 if (r < 0)
658 return r;
660 q->bus_request = sd_bus_message_ref(message);
661 q->request_family = family;
662 q->request_address = a;
663 q->complete = bus_method_resolve_address_complete;
665 r = dns_query_bus_track(q, message);
666 if (r < 0)
667 return r;
669 r = dns_query_go(q);
670 if (r < 0)
671 return r;
673 TAKE_PTR(q);
674 return 1;
677 static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
678 int r;
680 assert(m);
681 assert(rr);
683 r = sd_bus_message_open_container(m, 'r', "iqqay");
684 if (r < 0)
685 return r;
687 r = sd_bus_message_append(m, "iqq",
688 ifindex,
689 rr->key->class,
690 rr->key->type);
691 if (r < 0)
692 return r;
694 r = dns_resource_record_to_wire_format(rr, false);
695 if (r < 0)
696 return r;
698 r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
699 if (r < 0)
700 return r;
702 return sd_bus_message_close_container(m);
705 static void bus_method_resolve_record_complete(DnsQuery *query) {
706 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
707 _cleanup_(dns_query_freep) DnsQuery *q = query;
708 DnsResourceRecord *rr;
709 DnsQuestion *question;
710 unsigned added = 0;
711 int ifindex;
712 int r;
714 assert(q);
716 if (q->state != DNS_TRANSACTION_SUCCESS) {
717 r = reply_query_state(q);
718 goto finish;
721 r = dns_query_process_cname_many(q);
722 if (r == -ELOOP) {
723 r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
724 goto finish;
726 if (r < 0)
727 goto finish;
728 if (r == DNS_QUERY_CNAME) {
729 /* This was a cname, and the query was restarted. */
730 TAKE_PTR(q);
731 return;
734 r = sd_bus_message_new_method_return(q->bus_request, &reply);
735 if (r < 0)
736 goto finish;
738 r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
739 if (r < 0)
740 goto finish;
742 question = dns_query_question_for_protocol(q, q->answer_protocol);
744 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
745 r = dns_question_matches_rr(question, rr, NULL);
746 if (r < 0)
747 goto finish;
748 if (r == 0)
749 continue;
751 r = bus_message_append_rr(reply, rr, ifindex);
752 if (r < 0)
753 goto finish;
755 added++;
758 if (added <= 0) {
759 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", dns_query_string(q));
760 goto finish;
763 r = sd_bus_message_close_container(reply);
764 if (r < 0)
765 goto finish;
767 r = sd_bus_message_append(reply, "t", dns_query_reply_flags_make(q));
768 if (r < 0)
769 goto finish;
771 q->bus_request = sd_bus_message_unref(q->bus_request);
772 r = sd_bus_send(q->manager->bus, reply, NULL);
774 finish:
775 if (r < 0) {
776 log_error_errno(r, "Failed to send record reply: %m");
777 (void) reply_method_errnof(q, r, NULL);
781 static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
782 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
783 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
784 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
785 Manager *m = ASSERT_PTR(userdata);
786 uint16_t class, type;
787 const char *name;
788 int r, ifindex;
789 uint64_t flags;
791 assert(message);
793 assert_cc(sizeof(int) == sizeof(int32_t));
795 r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
796 if (r < 0)
797 return r;
799 if (ifindex < 0)
800 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
802 r = dns_name_is_valid(name);
803 if (r < 0)
804 return r;
805 if (r == 0)
806 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
808 if (!dns_type_is_valid_query(type))
809 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified resource record type %" PRIu16 " may not be used in a query.", type);
810 if (dns_type_is_zone_transer(type))
811 return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Zone transfers not permitted via this programming interface.");
812 if (dns_type_is_obsolete(type))
813 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
815 r = validate_and_mangle_flags(name, &flags, 0, error);
816 if (r < 0)
817 return r;
819 question = dns_question_new(1);
820 if (!question)
821 return -ENOMEM;
823 key = dns_resource_key_new(class, type, name);
824 if (!key)
825 return -ENOMEM;
827 r = dns_question_add(question, key, 0);
828 if (r < 0)
829 return r;
831 bus_client_log(message, "resource record resolution");
833 /* Setting SD_RESOLVED_CLAMP_TTL: let's request that the TTL is fixed up for locally cached entries,
834 * after all we return it in the wire format blob. */
835 r = dns_query_new(m, &q, question, question, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH|SD_RESOLVED_CLAMP_TTL);
836 if (r < 0)
837 return r;
839 q->bus_request = sd_bus_message_ref(message);
840 q->complete = bus_method_resolve_record_complete;
842 r = dns_query_bus_track(q, message);
843 if (r < 0)
844 return r;
846 r = dns_query_go(q);
847 if (r < 0)
848 return r;
850 TAKE_PTR(q);
851 return 1;
854 static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
855 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
856 _cleanup_free_ char *normalized = NULL;
857 int r;
859 assert(q);
860 assert(reply);
861 assert(rr);
862 assert(rr->key);
864 if (rr->key->type != DNS_TYPE_SRV)
865 return 0;
867 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
868 /* First, let's see if we could find an appropriate A or AAAA
869 * record for the SRV record */
870 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
871 DnsResourceRecord *zz;
872 DnsQuestion *question;
874 if (aux->state != DNS_TRANSACTION_SUCCESS)
875 continue;
876 if (aux->auxiliary_result != 0)
877 continue;
879 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
881 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
882 if (r < 0)
883 return r;
884 if (r == 0)
885 continue;
887 DNS_ANSWER_FOREACH(zz, aux->answer) {
889 r = dns_question_matches_rr(question, zz, NULL);
890 if (r < 0)
891 return r;
892 if (r == 0)
893 continue;
895 canonical = dns_resource_record_ref(zz);
896 break;
899 if (canonical)
900 break;
903 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
904 if (!canonical)
905 return 0;
908 r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
909 if (r < 0)
910 return r;
912 r = dns_name_normalize(rr->srv.name, 0, &normalized);
913 if (r < 0)
914 return r;
916 r = sd_bus_message_append(
917 reply,
918 "qqqs",
919 rr->srv.priority, rr->srv.weight, rr->srv.port, normalized);
920 if (r < 0)
921 return r;
923 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
924 if (r < 0)
925 return r;
927 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
928 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
929 DnsResourceRecord *zz;
930 DnsQuestion *question;
931 int ifindex;
933 if (aux->state != DNS_TRANSACTION_SUCCESS)
934 continue;
935 if (aux->auxiliary_result != 0)
936 continue;
938 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
940 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
941 if (r < 0)
942 return r;
943 if (r == 0)
944 continue;
946 DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
948 r = dns_question_matches_rr(question, zz, NULL);
949 if (r < 0)
950 return r;
951 if (r == 0)
952 continue;
954 r = append_address(reply, zz, ifindex);
955 if (r < 0)
956 return r;
961 r = sd_bus_message_close_container(reply);
962 if (r < 0)
963 return r;
965 if (canonical) {
966 normalized = mfree(normalized);
968 r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
969 if (r < 0)
970 return r;
973 /* Note that above we appended the hostname as encoded in the
974 * SRV, and here the canonical hostname this maps to. */
975 r = sd_bus_message_append(reply, "s", normalized);
976 if (r < 0)
977 return r;
979 r = sd_bus_message_close_container(reply);
980 if (r < 0)
981 return r;
983 return 1;
986 static int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
987 int r;
989 assert(reply);
990 assert(rr);
991 assert(rr->key);
993 if (rr->key->type != DNS_TYPE_TXT)
994 return 0;
996 LIST_FOREACH(items, i, rr->txt.items) {
998 if (i->length <= 0)
999 continue;
1001 r = sd_bus_message_append_array(reply, 'y', i->data, i->length);
1002 if (r < 0)
1003 return r;
1006 return 1;
1009 static void resolve_service_all_complete(DnsQuery *query) {
1010 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
1011 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1012 _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL;
1013 _cleanup_(dns_query_freep) DnsQuery *q = query;
1014 DnsQuestion *question;
1015 DnsResourceRecord *rr;
1016 unsigned added = 0;
1017 int r;
1019 assert(q);
1021 if (q->block_all_complete > 0) {
1022 TAKE_PTR(q);
1023 return;
1026 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1027 DnsQuery *bad = NULL;
1028 bool have_success = false;
1030 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
1032 switch (aux->state) {
1034 case DNS_TRANSACTION_PENDING:
1035 /* If an auxiliary query is still pending, let's wait */
1036 TAKE_PTR(q);
1037 return;
1039 case DNS_TRANSACTION_SUCCESS:
1040 if (aux->auxiliary_result == 0)
1041 have_success = true;
1042 else
1043 bad = aux;
1044 break;
1046 default:
1047 bad = aux;
1048 break;
1052 if (!have_success) {
1053 /* We can only return one error, hence pick the last error we encountered */
1055 assert(bad);
1057 if (bad->state == DNS_TRANSACTION_SUCCESS) {
1058 assert(bad->auxiliary_result != 0);
1060 if (bad->auxiliary_result == -ELOOP) {
1061 r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad));
1062 goto finish;
1065 assert(bad->auxiliary_result < 0);
1066 r = bad->auxiliary_result;
1067 goto finish;
1070 r = reply_query_state(bad);
1071 goto finish;
1075 r = sd_bus_message_new_method_return(q->bus_request, &reply);
1076 if (r < 0)
1077 goto finish;
1079 r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)");
1080 if (r < 0)
1081 goto finish;
1083 question = dns_query_question_for_protocol(q, q->answer_protocol);
1085 DNS_ANSWER_FOREACH(rr, q->answer) {
1086 r = dns_question_matches_rr(question, rr, NULL);
1087 if (r < 0)
1088 goto finish;
1089 if (r == 0)
1090 continue;
1092 r = append_srv(q, reply, rr);
1093 if (r < 0)
1094 goto finish;
1095 if (r == 0) /* not an SRV record */
1096 continue;
1098 if (!canonical)
1099 canonical = dns_resource_record_ref(rr);
1101 added++;
1104 if (added <= 0) {
1105 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
1106 goto finish;
1109 r = sd_bus_message_close_container(reply);
1110 if (r < 0)
1111 goto finish;
1113 r = sd_bus_message_open_container(reply, 'a', "ay");
1114 if (r < 0)
1115 goto finish;
1117 DNS_ANSWER_FOREACH(rr, q->answer) {
1118 r = dns_question_matches_rr(question, rr, NULL);
1119 if (r < 0)
1120 goto finish;
1121 if (r == 0)
1122 continue;
1124 r = append_txt(reply, rr);
1125 if (r < 0)
1126 goto finish;
1129 r = sd_bus_message_close_container(reply);
1130 if (r < 0)
1131 goto finish;
1133 assert(canonical);
1134 r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain);
1135 if (r < 0)
1136 goto finish;
1138 if (isempty(type)) {
1139 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide valid service", dns_query_string(q));
1140 goto finish;
1143 r = sd_bus_message_append(
1144 reply,
1145 "ssst",
1146 name, type, domain,
1147 dns_query_reply_flags_make(q));
1148 if (r < 0)
1149 goto finish;
1151 q->bus_request = sd_bus_message_unref(q->bus_request);
1152 r = sd_bus_send(q->manager->bus, reply, NULL);
1154 finish:
1155 if (r < 0) {
1156 log_error_errno(r, "Failed to send service reply: %m");
1157 (void) reply_method_errnof(q, r, NULL);
1161 static void resolve_service_hostname_complete(DnsQuery *q) {
1162 int r;
1164 assert(q);
1165 assert(q->auxiliary_for);
1167 if (q->state != DNS_TRANSACTION_SUCCESS) {
1168 resolve_service_all_complete(q->auxiliary_for);
1169 return;
1172 r = dns_query_process_cname_many(q);
1173 if (r == DNS_QUERY_CNAME) /* This was a cname, and the query was restarted. */
1174 return;
1176 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1177 q->auxiliary_result = r < 0 ? r : 0;
1178 resolve_service_all_complete(q->auxiliary_for);
1181 static int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) {
1182 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
1183 _cleanup_(dns_query_freep) DnsQuery *aux = NULL;
1184 int r;
1186 assert(q);
1187 assert(rr);
1188 assert(rr->key);
1189 assert(rr->key->type == DNS_TYPE_SRV);
1191 /* OK, we found an SRV record for the service. Let's resolve
1192 * the hostname included in it */
1194 r = dns_question_new_address(&question, q->request_family, rr->srv.name, false);
1195 if (r < 0)
1196 return r;
1198 r = dns_query_new(q->manager, &aux, question, question, NULL, ifindex, q->flags|SD_RESOLVED_NO_SEARCH);
1199 if (r < 0)
1200 return r;
1202 aux->bus_request = sd_bus_message_ref(q->bus_request);
1203 aux->request_family = q->request_family;
1204 aux->complete = resolve_service_hostname_complete;
1206 r = dns_query_make_auxiliary(aux, q);
1207 if (r == -EAGAIN)
1208 /* Too many auxiliary lookups? If so, don't complain,
1209 * let's just not add this one, we already have more
1210 * than enough */
1211 return 0;
1212 if (r < 0)
1213 return r;
1215 /* Note that auxiliary queries do not track the original bus
1216 * client, only the primary request does that. */
1218 r = dns_query_go(aux);
1219 if (r < 0)
1220 return r;
1222 TAKE_PTR(aux);
1223 return 1;
1226 static void bus_method_resolve_service_complete(DnsQuery *query) {
1227 _cleanup_(dns_query_freep) DnsQuery *q = query;
1228 bool has_root_domain = false;
1229 DnsResourceRecord *rr;
1230 DnsQuestion *question;
1231 unsigned found = 0;
1232 int ifindex, r;
1234 assert(q);
1236 if (q->state != DNS_TRANSACTION_SUCCESS) {
1237 r = reply_query_state(q);
1238 goto finish;
1241 r = dns_query_process_cname_many(q);
1242 if (r == -ELOOP) {
1243 r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
1244 goto finish;
1246 if (r < 0)
1247 goto finish;
1248 if (r == DNS_QUERY_CNAME) {
1249 /* This was a cname, and the query was restarted. */
1250 TAKE_PTR(q);
1251 return;
1254 question = dns_query_question_for_protocol(q, q->answer_protocol);
1256 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
1257 r = dns_question_matches_rr(question, rr, NULL);
1258 if (r < 0)
1259 goto finish;
1260 if (r == 0)
1261 continue;
1263 if (rr->key->type != DNS_TYPE_SRV)
1264 continue;
1266 if (dns_name_is_root(rr->srv.name)) {
1267 has_root_domain = true;
1268 continue;
1271 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1272 q->block_all_complete++;
1273 r = resolve_service_hostname(q, rr, ifindex);
1274 q->block_all_complete--;
1276 if (r < 0)
1277 goto finish;
1280 found++;
1283 if (has_root_domain && found <= 0) {
1284 /* If there's exactly one SRV RR and it uses the root domain as hostname, then the service is
1285 * explicitly not offered on the domain. Report this as a recognizable error. See RFC 2782,
1286 * Section "Usage Rules". */
1287 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide the requested service", dns_query_string(q));
1288 goto finish;
1291 if (found <= 0) {
1292 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
1293 goto finish;
1296 /* Maybe we are already finished? check now... */
1297 resolve_service_all_complete(TAKE_PTR(q));
1298 return;
1300 finish:
1301 if (r < 0) {
1302 log_error_errno(r, "Failed to send service reply: %m");
1303 (void) reply_method_errnof(q, r, NULL);
1307 static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1308 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
1309 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
1310 const char *name, *type, *domain;
1311 Manager *m = ASSERT_PTR(userdata);
1312 int family, ifindex;
1313 uint64_t flags;
1314 int r;
1316 assert(message);
1318 assert_cc(sizeof(int) == sizeof(int32_t));
1320 r = sd_bus_message_read(message, "isssit", &ifindex, &name, &type, &domain, &family, &flags);
1321 if (r < 0)
1322 return r;
1324 if (ifindex < 0)
1325 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
1327 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
1328 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
1330 if (isempty(name))
1331 name = NULL;
1332 else if (!dns_service_name_is_valid(name))
1333 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid service name '%s'", name);
1335 if (isempty(type))
1336 type = NULL;
1337 else if (!dns_srv_type_is_valid(type))
1338 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid SRV service type '%s'", type);
1340 r = dns_name_is_valid(domain);
1341 if (r < 0)
1342 return r;
1343 if (r == 0)
1344 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid domain '%s'", domain);
1346 if (name && !type)
1347 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type.");
1349 r = validate_and_mangle_flags(name, &flags, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS, error);
1350 if (r < 0)
1351 return r;
1353 r = dns_question_new_service(&question_utf8, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), false);
1354 if (r < 0)
1355 return r;
1357 r = dns_question_new_service(&question_idna, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), true);
1358 if (r < 0)
1359 return r;
1361 bus_client_log(message, "service resolution");
1363 r = dns_query_new(m, &q, question_utf8, question_idna, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH);
1364 if (r < 0)
1365 return r;
1367 q->bus_request = sd_bus_message_ref(message);
1368 q->request_family = family;
1369 q->complete = bus_method_resolve_service_complete;
1371 r = dns_query_bus_track(q, message);
1372 if (r < 0)
1373 return r;
1375 r = dns_query_go(q);
1376 if (r < 0)
1377 return r;
1379 TAKE_PTR(q);
1380 return 1;
1383 int bus_dns_server_append(
1384 sd_bus_message *reply,
1385 DnsServer *s,
1386 bool with_ifindex, /* include "ifindex" field */
1387 bool extended) { /* also include port number and server name */
1388 int r;
1390 assert(reply);
1392 if (!s) {
1393 if (with_ifindex) {
1394 if (extended)
1395 return sd_bus_message_append(reply, "(iiayqs)", 0, AF_UNSPEC, 0, 0, NULL);
1396 else
1397 return sd_bus_message_append(reply, "(iiay)", 0, AF_UNSPEC, 0);
1398 } else {
1399 if (extended)
1400 return sd_bus_message_append(reply, "(iayqs)", AF_UNSPEC, 0, 0, NULL);
1401 else
1402 return sd_bus_message_append(reply, "(iay)", AF_UNSPEC, 0);
1406 r = sd_bus_message_open_container(
1407 reply,
1408 'r',
1409 with_ifindex ? (extended ? "iiayqs" : "iiay") :
1410 (extended ? "iayqs" : "iay"));
1411 if (r < 0)
1412 return r;
1414 if (with_ifindex) {
1415 r = sd_bus_message_append(reply, "i", dns_server_ifindex(s));
1416 if (r < 0)
1417 return r;
1420 r = sd_bus_message_append(reply, "i", s->family);
1421 if (r < 0)
1422 return r;
1424 r = sd_bus_message_append_array(reply, 'y', &s->address, FAMILY_ADDRESS_SIZE(s->family));
1425 if (r < 0)
1426 return r;
1428 if (extended) {
1429 r = sd_bus_message_append(reply, "q", s->port);
1430 if (r < 0)
1431 return r;
1433 r = sd_bus_message_append(reply, "s", s->server_name);
1434 if (r < 0)
1435 return r;
1438 return sd_bus_message_close_container(reply);
1441 static int bus_property_get_dns_servers_internal(
1442 sd_bus *bus,
1443 const char *path,
1444 const char *interface,
1445 const char *property,
1446 sd_bus_message *reply,
1447 void *userdata,
1448 sd_bus_error *error,
1449 bool extended) {
1451 Manager *m = ASSERT_PTR(userdata);
1452 Link *l;
1453 int r;
1455 assert(reply);
1457 r = sd_bus_message_open_container(reply, 'a', extended ? "(iiayqs)" : "(iiay)");
1458 if (r < 0)
1459 return r;
1461 LIST_FOREACH(servers, s, m->dns_servers) {
1462 r = bus_dns_server_append(reply, s, true, extended);
1463 if (r < 0)
1464 return r;
1467 HASHMAP_FOREACH(l, m->links)
1468 LIST_FOREACH(servers, s, l->dns_servers) {
1469 r = bus_dns_server_append(reply, s, true, extended);
1470 if (r < 0)
1471 return r;
1474 return sd_bus_message_close_container(reply);
1477 static int bus_property_get_dns_servers(
1478 sd_bus *bus,
1479 const char *path,
1480 const char *interface,
1481 const char *property,
1482 sd_bus_message *reply,
1483 void *userdata,
1484 sd_bus_error *error) {
1485 return bus_property_get_dns_servers_internal(bus, path, interface, property, reply, userdata, error, false);
1488 static int bus_property_get_dns_servers_ex(
1489 sd_bus *bus,
1490 const char *path,
1491 const char *interface,
1492 const char *property,
1493 sd_bus_message *reply,
1494 void *userdata,
1495 sd_bus_error *error) {
1496 return bus_property_get_dns_servers_internal(bus, path, interface, property, reply, userdata, error, true);
1499 static int bus_property_get_fallback_dns_servers_internal(
1500 sd_bus *bus,
1501 const char *path,
1502 const char *interface,
1503 const char *property,
1504 sd_bus_message *reply,
1505 void *userdata,
1506 sd_bus_error *error,
1507 bool extended) {
1509 DnsServer **f = ASSERT_PTR(userdata);
1510 int r;
1512 assert(reply);
1514 r = sd_bus_message_open_container(reply, 'a', extended ? "(iiayqs)" : "(iiay)");
1515 if (r < 0)
1516 return r;
1518 LIST_FOREACH(servers, s, *f) {
1519 r = bus_dns_server_append(reply, s, true, extended);
1520 if (r < 0)
1521 return r;
1524 return sd_bus_message_close_container(reply);
1527 static int bus_property_get_fallback_dns_servers(
1528 sd_bus *bus,
1529 const char *path,
1530 const char *interface,
1531 const char *property,
1532 sd_bus_message *reply,
1533 void *userdata,
1534 sd_bus_error *error) {
1535 return bus_property_get_fallback_dns_servers_internal(bus, path, interface, property, reply, userdata, error, false);
1538 static int bus_property_get_fallback_dns_servers_ex(
1539 sd_bus *bus,
1540 const char *path,
1541 const char *interface,
1542 const char *property,
1543 sd_bus_message *reply,
1544 void *userdata,
1545 sd_bus_error *error) {
1546 return bus_property_get_fallback_dns_servers_internal(bus, path, interface, property, reply, userdata, error, true);
1549 static int bus_property_get_current_dns_server_internal(
1550 sd_bus *bus,
1551 const char *path,
1552 const char *interface,
1553 const char *property,
1554 sd_bus_message *reply,
1555 void *userdata,
1556 sd_bus_error *error,
1557 bool extended) {
1559 DnsServer *s;
1561 assert(reply);
1562 assert(userdata);
1564 s = *(DnsServer **) userdata;
1566 return bus_dns_server_append(reply, s, true, extended);
1569 static int bus_property_get_current_dns_server(
1570 sd_bus *bus,
1571 const char *path,
1572 const char *interface,
1573 const char *property,
1574 sd_bus_message *reply,
1575 void *userdata,
1576 sd_bus_error *error) {
1577 return bus_property_get_current_dns_server_internal(bus, path, interface, property, reply, userdata, error, false);
1580 static int bus_property_get_current_dns_server_ex(
1581 sd_bus *bus,
1582 const char *path,
1583 const char *interface,
1584 const char *property,
1585 sd_bus_message *reply,
1586 void *userdata,
1587 sd_bus_error *error) {
1588 return bus_property_get_current_dns_server_internal(bus, path, interface, property, reply, userdata, error, true);
1591 static int bus_property_get_domains(
1592 sd_bus *bus,
1593 const char *path,
1594 const char *interface,
1595 const char *property,
1596 sd_bus_message *reply,
1597 void *userdata,
1598 sd_bus_error *error) {
1600 Manager *m = ASSERT_PTR(userdata);
1601 Link *l;
1602 int r;
1604 assert(reply);
1606 r = sd_bus_message_open_container(reply, 'a', "(isb)");
1607 if (r < 0)
1608 return r;
1610 LIST_FOREACH(domains, d, m->search_domains) {
1611 r = sd_bus_message_append(reply, "(isb)", 0, d->name, d->route_only);
1612 if (r < 0)
1613 return r;
1616 HASHMAP_FOREACH(l, m->links) {
1617 LIST_FOREACH(domains, d, l->search_domains) {
1618 r = sd_bus_message_append(reply, "(isb)", l->ifindex, d->name, d->route_only);
1619 if (r < 0)
1620 return r;
1624 return sd_bus_message_close_container(reply);
1627 static int bus_property_get_transaction_statistics(
1628 sd_bus *bus,
1629 const char *path,
1630 const char *interface,
1631 const char *property,
1632 sd_bus_message *reply,
1633 void *userdata,
1634 sd_bus_error *error) {
1636 Manager *m = ASSERT_PTR(userdata);
1638 assert(reply);
1640 return sd_bus_message_append(reply, "(tt)",
1641 (uint64_t) hashmap_size(m->dns_transactions),
1642 (uint64_t) m->n_transactions_total);
1645 static int bus_property_get_cache_statistics(
1646 sd_bus *bus,
1647 const char *path,
1648 const char *interface,
1649 const char *property,
1650 sd_bus_message *reply,
1651 void *userdata,
1652 sd_bus_error *error) {
1654 uint64_t size = 0, hit = 0, miss = 0;
1655 Manager *m = ASSERT_PTR(userdata);
1657 assert(reply);
1659 LIST_FOREACH(scopes, s, m->dns_scopes) {
1660 size += dns_cache_size(&s->cache);
1661 hit += s->cache.n_hit;
1662 miss += s->cache.n_miss;
1665 return sd_bus_message_append(reply, "(ttt)", size, hit, miss);
1668 static int bus_property_get_dnssec_statistics(
1669 sd_bus *bus,
1670 const char *path,
1671 const char *interface,
1672 const char *property,
1673 sd_bus_message *reply,
1674 void *userdata,
1675 sd_bus_error *error) {
1677 Manager *m = ASSERT_PTR(userdata);
1679 assert(reply);
1681 return sd_bus_message_append(reply, "(tttt)",
1682 (uint64_t) m->n_dnssec_verdict[DNSSEC_SECURE],
1683 (uint64_t) m->n_dnssec_verdict[DNSSEC_INSECURE],
1684 (uint64_t) m->n_dnssec_verdict[DNSSEC_BOGUS],
1685 (uint64_t) m->n_dnssec_verdict[DNSSEC_INDETERMINATE]);
1688 static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode, dns_stub_listener_mode, DnsStubListenerMode);
1689 static BUS_DEFINE_PROPERTY_GET(bus_property_get_dnssec_supported, "b", Manager, manager_dnssec_supported);
1690 static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dnssec_mode, "s", Manager, manager_get_dnssec_mode, dnssec_mode_to_string);
1691 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);
1693 static int bus_property_get_resolv_conf_mode(
1694 sd_bus *bus,
1695 const char *path,
1696 const char *interface,
1697 const char *property,
1698 sd_bus_message *reply,
1699 void *userdata,
1700 sd_bus_error *error) {
1702 int r;
1704 assert(reply);
1706 r = resolv_conf_mode();
1707 if (r < 0) {
1708 log_warning_errno(r, "Failed to test /etc/resolv.conf mode, ignoring: %m");
1709 return sd_bus_message_append(reply, "s", NULL);
1712 return sd_bus_message_append(reply, "s", resolv_conf_mode_to_string(r));
1715 static int bus_method_reset_statistics(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1716 Manager *m = ASSERT_PTR(userdata);
1718 assert(message);
1720 bus_client_log(message, "statistics reset");
1722 dns_manager_reset_statistics(m);
1724 return sd_bus_reply_method_return(message, NULL);
1727 static int get_any_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) {
1728 Link *l;
1730 assert(m);
1731 assert(ret);
1733 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
1734 if (!l)
1735 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %i not known", ifindex);
1737 *ret = l;
1738 return 0;
1741 static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
1742 int ifindex, r;
1743 Link *l = NULL; /* avoid false maybe-uninitialized warning */
1745 assert(m);
1746 assert(message);
1747 assert(handler);
1749 r = bus_message_read_ifindex(message, error, &ifindex);
1750 if (r < 0)
1751 return r;
1753 r = get_any_link(m, ifindex, &l, error);
1754 if (r < 0)
1755 return r;
1757 return handler(message, l, error);
1760 static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1761 return call_link_method(userdata, message, bus_link_method_set_dns_servers, error);
1764 static int bus_method_set_link_dns_servers_ex(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1765 return call_link_method(userdata, message, bus_link_method_set_dns_servers_ex, error);
1768 static int bus_method_set_link_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1769 return call_link_method(userdata, message, bus_link_method_set_domains, error);
1772 static int bus_method_set_link_default_route(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1773 return call_link_method(userdata, message, bus_link_method_set_default_route, error);
1776 static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1777 return call_link_method(userdata, message, bus_link_method_set_llmnr, error);
1780 static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1781 return call_link_method(userdata, message, bus_link_method_set_mdns, error);
1784 static int bus_method_set_link_dns_over_tls(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1785 return call_link_method(userdata, message, bus_link_method_set_dns_over_tls, error);
1788 static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1789 return call_link_method(userdata, message, bus_link_method_set_dnssec, error);
1792 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1793 return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error);
1796 static int bus_method_revert_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1797 return call_link_method(userdata, message, bus_link_method_revert, error);
1800 static int bus_method_get_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1801 _cleanup_free_ char *p = NULL;
1802 Manager *m = ASSERT_PTR(userdata);
1803 int r, ifindex;
1804 Link *l = NULL; /* avoid false maybe-uninitialized warning */
1806 assert(message);
1808 r = bus_message_read_ifindex(message, error, &ifindex);
1809 if (r < 0)
1810 return r;
1812 r = get_any_link(m, ifindex, &l, error);
1813 if (r < 0)
1814 return r;
1816 p = link_bus_path(l);
1817 if (!p)
1818 return -ENOMEM;
1820 return sd_bus_reply_method_return(message, "o", p);
1823 static int bus_method_flush_caches(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1824 Manager *m = ASSERT_PTR(userdata);
1826 assert(message);
1828 bus_client_log(message, "cache flush");
1830 manager_flush_caches(m, LOG_INFO);
1832 return sd_bus_reply_method_return(message, NULL);
1835 static int bus_method_reset_server_features(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1836 Manager *m = ASSERT_PTR(userdata);
1838 assert(message);
1840 bus_client_log(message, "server feature reset");
1842 (void) dns_stream_disconnect_all(m);
1843 manager_reset_server_features(m);
1845 return sd_bus_reply_method_return(message, NULL);
1848 static int dnssd_service_on_bus_track(sd_bus_track *t, void *userdata) {
1849 DnssdService *s = ASSERT_PTR(userdata);
1851 assert(t);
1853 log_debug("Client of active request vanished, destroying DNS-SD service.");
1854 dnssd_service_free(s);
1856 return 0;
1859 static int bus_method_register_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1860 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1861 _cleanup_(dnssd_service_freep) DnssdService *service = NULL;
1862 _cleanup_(sd_bus_track_unrefp) sd_bus_track *bus_track = NULL;
1863 const char *name, *name_template, *type;
1864 _cleanup_free_ char *path = NULL;
1865 DnssdService *s = NULL;
1866 Manager *m = ASSERT_PTR(userdata);
1867 uid_t euid;
1868 int r;
1870 assert(message);
1872 if (m->mdns_support != RESOLVE_SUPPORT_YES)
1873 return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Support for MulticastDNS is disabled");
1875 service = new0(DnssdService, 1);
1876 if (!service)
1877 return log_oom();
1879 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1880 if (r < 0)
1881 return r;
1883 r = sd_bus_creds_get_euid(creds, &euid);
1884 if (r < 0)
1885 return r;
1886 service->originator = euid;
1888 r = sd_bus_message_read(message, "sssqqq", &name, &name_template, &type,
1889 &service->port, &service->priority,
1890 &service->weight);
1891 if (r < 0)
1892 return r;
1894 s = hashmap_get(m->dnssd_services, name);
1895 if (s)
1896 return sd_bus_error_setf(error, BUS_ERROR_DNSSD_SERVICE_EXISTS, "DNS-SD service '%s' exists already", name);
1898 if (!dnssd_srv_type_is_valid(type))
1899 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DNS-SD service type '%s' is invalid", type);
1901 service->name = strdup(name);
1902 if (!service->name)
1903 return log_oom();
1905 service->name_template = strdup(name_template);
1906 if (!service->name_template)
1907 return log_oom();
1909 service->type = strdup(type);
1910 if (!service->type)
1911 return log_oom();
1913 r = dnssd_render_instance_name(m, service, NULL);
1914 if (r < 0)
1915 return r;
1917 r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "a{say}");
1918 if (r < 0)
1919 return r;
1921 while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "{say}")) > 0) {
1922 _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL;
1923 DnsTxtItem *last = NULL;
1925 txt_data = new0(DnssdTxtData, 1);
1926 if (!txt_data)
1927 return log_oom();
1929 while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_DICT_ENTRY, "say")) > 0) {
1930 const char *key;
1931 const void *value;
1932 size_t size;
1933 DnsTxtItem *i;
1935 r = sd_bus_message_read(message, "s", &key);
1936 if (r < 0)
1937 return r;
1939 if (isempty(key))
1940 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Keys in DNS-SD TXT RRs can't be empty");
1942 if (!ascii_is_valid(key))
1943 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "TXT key '%s' contains non-ASCII symbols", key);
1945 r = sd_bus_message_read_array(message, 'y', &value, &size);
1946 if (r < 0)
1947 return r;
1949 r = dnssd_txt_item_new_from_data(key, value, size, &i);
1950 if (r < 0)
1951 return r;
1953 LIST_INSERT_AFTER(items, txt_data->txts, last, i);
1954 last = i;
1956 r = sd_bus_message_exit_container(message);
1957 if (r < 0)
1958 return r;
1961 if (r < 0)
1962 return r;
1964 r = sd_bus_message_exit_container(message);
1965 if (r < 0)
1966 return r;
1968 if (txt_data->txts) {
1969 LIST_PREPEND(items, service->txt_data_items, txt_data);
1970 txt_data = NULL;
1973 if (r < 0)
1974 return r;
1976 r = sd_bus_message_exit_container(message);
1977 if (r < 0)
1978 return r;
1980 if (!service->txt_data_items) {
1981 _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL;
1983 txt_data = new0(DnssdTxtData, 1);
1984 if (!txt_data)
1985 return log_oom();
1987 r = dns_txt_item_new_empty(&txt_data->txts);
1988 if (r < 0)
1989 return r;
1991 LIST_PREPEND(items, service->txt_data_items, txt_data);
1992 txt_data = NULL;
1995 r = sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service->name, &path);
1996 if (r < 0)
1997 return r;
1999 r = bus_verify_polkit_async(message, CAP_SYS_ADMIN,
2000 "org.freedesktop.resolve1.register-service",
2001 NULL, false, UID_INVALID,
2002 &m->polkit_registry, error);
2003 if (r < 0)
2004 return r;
2005 if (r == 0)
2006 return 1; /* Polkit will call us back */
2008 r = hashmap_ensure_put(&m->dnssd_services, &string_hash_ops, service->name, service);
2009 if (r < 0)
2010 return r;
2012 r = sd_bus_track_new(sd_bus_message_get_bus(message), &bus_track, dnssd_service_on_bus_track, service);
2013 if (r < 0)
2014 return r;
2016 r = sd_bus_track_add_sender(bus_track, message);
2017 if (r < 0)
2018 return r;
2020 service->manager = m;
2022 service = NULL;
2024 manager_refresh_rrs(m);
2026 return sd_bus_reply_method_return(message, "o", path);
2029 static int call_dnssd_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
2030 _cleanup_free_ char *name = NULL;
2031 DnssdService *s = NULL;
2032 const char *path;
2033 int r;
2035 assert(m);
2036 assert(message);
2037 assert(handler);
2039 r = sd_bus_message_read(message, "o", &path);
2040 if (r < 0)
2041 return r;
2043 r = sd_bus_path_decode(path, "/org/freedesktop/resolve1/dnssd", &name);
2044 if (r == 0)
2045 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DNSSD_SERVICE, "DNS-SD service with object path '%s' does not exist", path);
2046 if (r < 0)
2047 return r;
2049 s = hashmap_get(m->dnssd_services, name);
2050 if (!s)
2051 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DNSSD_SERVICE, "DNS-SD service '%s' not known", name);
2053 return handler(message, s, error);
2056 static int bus_method_unregister_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2057 Manager *m = ASSERT_PTR(userdata);
2059 assert(message);
2061 return call_dnssd_method(m, message, bus_dnssd_method_unregister, error);
2064 static const sd_bus_vtable resolve_vtable[] = {
2065 SD_BUS_VTABLE_START(0),
2066 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL, offsetof(Manager, llmnr_hostname), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2067 SD_BUS_PROPERTY("LLMNR", "s", bus_property_get_resolve_support, offsetof(Manager, llmnr_support), 0),
2068 SD_BUS_PROPERTY("MulticastDNS", "s", bus_property_get_resolve_support, offsetof(Manager, mdns_support), 0),
2069 SD_BUS_PROPERTY("DNSOverTLS", "s", bus_property_get_dns_over_tls_mode, 0, 0),
2070 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2071 SD_BUS_PROPERTY("DNSEx", "a(iiayqs)", bus_property_get_dns_servers_ex, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2072 SD_BUS_PROPERTY("FallbackDNS", "a(iiay)", bus_property_get_fallback_dns_servers, offsetof(Manager, fallback_dns_servers), SD_BUS_VTABLE_PROPERTY_CONST),
2073 SD_BUS_PROPERTY("FallbackDNSEx", "a(iiayqs)", bus_property_get_fallback_dns_servers_ex, offsetof(Manager, fallback_dns_servers), SD_BUS_VTABLE_PROPERTY_CONST),
2074 SD_BUS_PROPERTY("CurrentDNSServer", "(iiay)", bus_property_get_current_dns_server, offsetof(Manager, current_dns_server), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2075 SD_BUS_PROPERTY("CurrentDNSServerEx", "(iiayqs)", bus_property_get_current_dns_server_ex, offsetof(Manager, current_dns_server), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2076 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains, 0, 0),
2077 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics, 0, 0),
2078 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics, 0, 0),
2079 SD_BUS_PROPERTY("DNSSEC", "s", bus_property_get_dnssec_mode, 0, 0),
2080 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics, 0, 0),
2081 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported, 0, 0),
2082 SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_string_set, offsetof(Manager, trust_anchor.negative_by_name), 0),
2083 SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode, offsetof(Manager, dns_stub_listener_mode), 0),
2084 SD_BUS_PROPERTY("ResolvConfMode", "s", bus_property_get_resolv_conf_mode, 0, 0),
2086 SD_BUS_METHOD_WITH_ARGS("ResolveHostname",
2087 SD_BUS_ARGS("i", ifindex, "s", name, "i", family, "t", flags),
2088 SD_BUS_RESULT("a(iiay)", addresses, "s", canonical, "t", flags),
2089 bus_method_resolve_hostname,
2090 SD_BUS_VTABLE_UNPRIVILEGED),
2091 SD_BUS_METHOD_WITH_ARGS("ResolveAddress",
2092 SD_BUS_ARGS("i", ifindex, "i", family, "ay", address, "t", flags),
2093 SD_BUS_RESULT("a(is)", names, "t", flags),
2094 bus_method_resolve_address,
2095 SD_BUS_VTABLE_UNPRIVILEGED),
2096 SD_BUS_METHOD_WITH_ARGS("ResolveRecord",
2097 SD_BUS_ARGS("i", ifindex, "s", name, "q", class, "q", type, "t", flags),
2098 SD_BUS_RESULT("a(iqqay)", records, "t", flags),
2099 bus_method_resolve_record,
2100 SD_BUS_VTABLE_UNPRIVILEGED),
2101 SD_BUS_METHOD_WITH_ARGS("ResolveService",
2102 SD_BUS_ARGS("i", ifindex,
2103 "s", name,
2104 "s", type,
2105 "s", domain,
2106 "i", family,
2107 "t", flags),
2108 SD_BUS_RESULT("a(qqqsa(iiay)s)", srv_data,
2109 "aay", txt_data,
2110 "s", canonical_name,
2111 "s", canonical_type,
2112 "s", canonical_domain,
2113 "t", flags),
2114 bus_method_resolve_service,
2115 SD_BUS_VTABLE_UNPRIVILEGED),
2116 SD_BUS_METHOD_WITH_ARGS("GetLink",
2117 SD_BUS_ARGS("i", ifindex),
2118 SD_BUS_RESULT("o", path),
2119 bus_method_get_link,
2120 SD_BUS_VTABLE_UNPRIVILEGED),
2121 SD_BUS_METHOD_WITH_ARGS("SetLinkDNS",
2122 SD_BUS_ARGS("i", ifindex, "a(iay)", addresses),
2123 SD_BUS_NO_RESULT,
2124 bus_method_set_link_dns_servers,
2125 SD_BUS_VTABLE_UNPRIVILEGED),
2126 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSEx",
2127 SD_BUS_ARGS("i", ifindex, "a(iayqs)", addresses),
2128 SD_BUS_NO_RESULT,
2129 bus_method_set_link_dns_servers_ex,
2130 SD_BUS_VTABLE_UNPRIVILEGED),
2131 SD_BUS_METHOD_WITH_ARGS("SetLinkDomains",
2132 SD_BUS_ARGS("i", ifindex, "a(sb)", domains),
2133 SD_BUS_NO_RESULT,
2134 bus_method_set_link_domains,
2135 SD_BUS_VTABLE_UNPRIVILEGED),
2136 SD_BUS_METHOD_WITH_ARGS("SetLinkDefaultRoute",
2137 SD_BUS_ARGS("i", ifindex, "b", enable),
2138 SD_BUS_NO_RESULT,
2139 bus_method_set_link_default_route,
2140 SD_BUS_VTABLE_UNPRIVILEGED),
2141 SD_BUS_METHOD_WITH_ARGS("SetLinkLLMNR",
2142 SD_BUS_ARGS("i", ifindex, "s", mode),
2143 SD_BUS_NO_RESULT,
2144 bus_method_set_link_llmnr,
2145 SD_BUS_VTABLE_UNPRIVILEGED),
2146 SD_BUS_METHOD_WITH_ARGS("SetLinkMulticastDNS",
2147 SD_BUS_ARGS("i", ifindex, "s", mode),
2148 SD_BUS_NO_RESULT,
2149 bus_method_set_link_mdns,
2150 SD_BUS_VTABLE_UNPRIVILEGED),
2151 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSOverTLS",
2152 SD_BUS_ARGS("i", ifindex, "s", mode),
2153 SD_BUS_NO_RESULT,
2154 bus_method_set_link_dns_over_tls,
2155 SD_BUS_VTABLE_UNPRIVILEGED),
2156 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSEC",
2157 SD_BUS_ARGS("i", ifindex, "s", mode),
2158 SD_BUS_NO_RESULT,
2159 bus_method_set_link_dnssec,
2160 SD_BUS_VTABLE_UNPRIVILEGED),
2161 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSECNegativeTrustAnchors",
2162 SD_BUS_ARGS("i", ifindex, "as", names),
2163 SD_BUS_NO_RESULT,
2164 bus_method_set_link_dnssec_negative_trust_anchors,
2165 SD_BUS_VTABLE_UNPRIVILEGED),
2166 SD_BUS_METHOD_WITH_ARGS("RevertLink",
2167 SD_BUS_ARGS("i", ifindex),
2168 SD_BUS_NO_RESULT,
2169 bus_method_revert_link,
2170 SD_BUS_VTABLE_UNPRIVILEGED),
2171 SD_BUS_METHOD_WITH_ARGS("RegisterService",
2172 SD_BUS_ARGS("s", name,
2173 "s", name_template,
2174 "s", type,
2175 "q", service_port,
2176 "q", service_priority,
2177 "q", service_weight,
2178 "aa{say}", txt_datas),
2179 SD_BUS_RESULT("o", service_path),
2180 bus_method_register_service,
2181 SD_BUS_VTABLE_UNPRIVILEGED),
2182 SD_BUS_METHOD_WITH_ARGS("UnregisterService",
2183 SD_BUS_ARGS("o", service_path),
2184 SD_BUS_NO_RESULT,
2185 bus_method_unregister_service,
2186 SD_BUS_VTABLE_UNPRIVILEGED),
2187 SD_BUS_METHOD_WITH_ARGS("ResetStatistics",
2188 SD_BUS_NO_ARGS,
2189 SD_BUS_NO_RESULT,
2190 bus_method_reset_statistics,
2191 SD_BUS_VTABLE_UNPRIVILEGED),
2192 SD_BUS_METHOD_WITH_ARGS("FlushCaches",
2193 SD_BUS_NO_ARGS,
2194 SD_BUS_NO_RESULT,
2195 bus_method_flush_caches,
2196 SD_BUS_VTABLE_UNPRIVILEGED),
2197 SD_BUS_METHOD_WITH_ARGS("ResetServerFeatures",
2198 SD_BUS_NO_ARGS,
2199 SD_BUS_NO_RESULT,
2200 bus_method_reset_server_features,
2201 SD_BUS_VTABLE_UNPRIVILEGED),
2203 SD_BUS_VTABLE_END,
2206 const BusObjectImplementation manager_object = {
2207 "/org/freedesktop/resolve1",
2208 "org.freedesktop.resolve1.Manager",
2209 .vtables = BUS_VTABLES(resolve_vtable),
2210 .children = BUS_IMPLEMENTATIONS(&link_object,
2211 &dnssd_object),
2214 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
2215 Manager *m = ASSERT_PTR(userdata);
2216 int b, r;
2218 assert(message);
2220 r = sd_bus_message_read(message, "b", &b);
2221 if (r < 0) {
2222 bus_log_parse_error(r);
2223 return 0;
2226 if (b)
2227 return 0;
2229 log_debug("Coming back from suspend, closing all TCP connections...");
2230 (void) dns_stream_disconnect_all(m);
2232 log_debug("Coming back from suspend, resetting all probed server features...");
2233 manager_reset_server_features(m);
2235 log_debug("Coming back from suspend, verifying all RRs...");
2236 manager_verify_all(m);
2238 return 0;
2241 int manager_connect_bus(Manager *m) {
2242 int r;
2244 assert(m);
2246 if (m->bus)
2247 return 0;
2249 r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-resolve");
2250 if (r < 0)
2251 return log_error_errno(r, "Failed to connect to system bus: %m");
2253 r = bus_add_implementation(m->bus, &manager_object, m);
2254 if (r < 0)
2255 return r;
2257 r = bus_log_control_api_register(m->bus);
2258 if (r < 0)
2259 return r;
2261 r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.resolve1", 0, NULL, NULL);
2262 if (r < 0)
2263 return log_error_errno(r, "Failed to request name: %m");
2265 r = sd_bus_attach_event(m->bus, m->event, 0);
2266 if (r < 0)
2267 return log_error_errno(r, "Failed to attach bus to event loop: %m");
2269 r = bus_match_signal_async(
2270 m->bus,
2271 NULL,
2272 bus_login_mgr,
2273 "PrepareForSleep",
2274 match_prepare_for_sleep,
2275 NULL,
2277 if (r < 0)
2278 log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m");
2280 return 0;
2283 int _manager_send_changed(Manager *manager, const char *property, ...) {
2284 assert(manager);
2286 if (sd_bus_is_ready(manager->bus) <= 0)
2287 return 0;
2289 char **l = strv_from_stdarg_alloca(property);
2291 int r = sd_bus_emit_properties_changed_strv(
2292 manager->bus,
2293 "/org/freedesktop/resolve1",
2294 "org.freedesktop.resolve1.Manager",
2296 if (r < 0)
2297 log_notice_errno(r, "Failed to emit notification about changed property %s: %m", property);
2298 return r;