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"
25 #include "syslog-util.h"
26 #include "user-util.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
);
36 if (!DNS_TRANSACTION_IS_LIVE(q
->state
))
39 log_debug("Client of active query vanished, aborting query.");
40 dns_query_complete(q
, DNS_TRANSACTION_ABORTED
);
44 static int dns_query_bus_track(DnsQuery
*q
, sd_bus_message
*m
) {
51 r
= sd_bus_track_new(sd_bus_message_get_bus(m
), &q
->bus_track
, query_on_bus_track
, q
);
56 r
= sd_bus_track_add_sender(q
->bus_track
, m
);
63 static sd_bus_message
*dns_query_steal_request(DnsQuery
*q
) {
66 /* Find the main query, it's the one that owns the message */
67 while (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(
76 const char *error_name
,
80 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
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 */
92 r
= sd_bus_reply_method_errorfv(req
, error_name
, format
, ap
);
98 _sd_printf_(3, 4) static int reply_method_errnof(
104 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
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 */
116 va_start(ap
, format
);
117 r
= sd_bus_reply_method_errnofv(req
, err
, format
, ap
);
120 r
= sd_bus_reply_method_errno(req
, err
, NULL
);
125 static int reply_query_state(DnsQuery
*q
) {
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 */
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
));
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
:
198 assert_not_reached();
202 static int append_address(sd_bus_message
*reply
, DnsResourceRecord
*rr
, int ifindex
) {
208 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
212 r
= sd_bus_message_append(reply
, "i", ifindex
);
216 if (rr
->key
->type
== DNS_TYPE_A
) {
217 r
= sd_bus_message_append(reply
, "i", AF_INET
);
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
);
228 r
= sd_bus_message_append_array(reply
, 'y', &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
));
230 return -EAFNOSUPPORT
;
235 r
= sd_bus_message_close_container(reply
);
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
;
254 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
255 r
= reply_query_state(q
);
259 r
= dns_query_process_cname_many(q
);
261 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
266 if (r
== DNS_QUERY_CNAME
) {
267 /* This was a cname, and the query was restarted. */
272 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
276 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
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
));
290 r
= append_address(reply
, rr
, ifindex
);
295 canonical
= dns_resource_record_ref(rr
);
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
));
305 r
= sd_bus_message_close_container(reply
);
309 /* The key names are not necessarily normalized, make sure that they are when we return them to our
312 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), 0, &normalized
);
316 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
317 r
= sd_bus_message_append(
320 dns_query_reply_flags_make(q
));
324 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
325 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
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(
338 sd_bus_error
*error
) {
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
|
362 SD_RESOLVED_NO_TRUST_ANCHOR
|
363 SD_RESOLVED_NO_NETWORK
|
364 SD_RESOLVED_NO_STALE
|
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
;
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 */
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
);
403 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
407 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
411 r
= sd_bus_message_append(reply
, "ii", ifindex
, ff
);
415 r
= sd_bus_message_append_array(reply
, 'y', &parsed
, FAMILY_ADDRESS_SIZE(ff
));
419 r
= sd_bus_message_close_container(reply
);
423 r
= sd_bus_message_close_container(reply
);
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
);
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
);
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
;
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
);
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
;
479 assert_cc(sizeof(int) == sizeof(int32_t));
481 r
= sd_bus_message_read(message
, "isit", &ifindex
, &hostname
, &family
, &flags
);
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
);
495 r
= parse_as_address(message
, ifindex
, hostname
, family
, flags
);
499 r
= dns_name_is_valid(hostname
);
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);
509 r
= dns_question_new_address(&question_idna
, family
, hostname
, true);
510 if (r
< 0 && r
!= -EALREADY
)
513 bus_client_log(message
, "hostname resolution");
515 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
?: question_utf8
, NULL
, ifindex
, flags
);
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
);
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
;
545 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
546 r
= reply_query_state(q
);
550 r
= dns_query_process_cname_many(q
);
552 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
557 if (r
== DNS_QUERY_CNAME
) {
558 /* This was a cname, and the query was restarted. */
563 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
567 r
= sd_bus_message_open_container(reply
, 'a', "(is)");
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
);
582 r
= dns_name_normalize(rr
->ptr
.name
, 0, &normalized
);
586 r
= sd_bus_message_append(reply
, "(is)", ifindex
, normalized
);
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
));
600 r
= sd_bus_message_close_container(reply
);
604 r
= sd_bus_message_append(reply
, "t", dns_query_reply_flags_make(q
));
608 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
609 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
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
;
629 assert_cc(sizeof(int) == sizeof(int32_t));
631 r
= sd_bus_message_read(message
, "i", &ifindex
);
635 r
= bus_message_read_in_addr_auto(message
, error
, &family
, &a
);
639 r
= sd_bus_message_read(message
, "t", &flags
);
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
);
650 r
= dns_question_new_reverse(&question
, family
, &a
);
654 bus_client_log(message
, "address resolution");
656 r
= dns_query_new(m
, &q
, question
, question
, NULL
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
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
);
677 static int bus_message_append_rr(sd_bus_message
*m
, DnsResourceRecord
*rr
, int ifindex
) {
683 r
= sd_bus_message_open_container(m
, 'r', "iqqay");
687 r
= sd_bus_message_append(m
, "iqq",
694 r
= dns_resource_record_to_wire_format(rr
, false);
698 r
= sd_bus_message_append_array(m
, 'y', rr
->wire_format
, rr
->wire_format_size
);
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
;
716 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
717 r
= reply_query_state(q
);
721 r
= dns_query_process_cname_many(q
);
723 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
728 if (r
== DNS_QUERY_CNAME
) {
729 /* This was a cname, and the query was restarted. */
734 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
738 r
= sd_bus_message_open_container(reply
, 'a', "(iqqay)");
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
);
751 r
= bus_message_append_rr(reply
, rr
, ifindex
);
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
));
763 r
= sd_bus_message_close_container(reply
);
767 r
= sd_bus_message_append(reply
, "t", dns_query_reply_flags_make(q
));
771 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
772 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
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
;
793 assert_cc(sizeof(int) == sizeof(int32_t));
795 r
= sd_bus_message_read(message
, "isqqt", &ifindex
, &name
, &class, &type
, &flags
);
800 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
802 r
= dns_name_is_valid(name
);
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
);
819 question
= dns_question_new(1);
823 key
= dns_resource_key_new(class, type
, name
);
827 r
= dns_question_add(question
, key
, 0);
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
);
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
);
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
;
864 if (rr
->key
->type
!= DNS_TYPE_SRV
)
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
)
876 if (aux
->auxiliary_result
!= 0)
879 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
881 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
887 DNS_ANSWER_FOREACH(zz
, aux
->answer
) {
889 r
= dns_question_matches_rr(question
, zz
, NULL
);
895 canonical
= dns_resource_record_ref(zz
);
903 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
908 r
= sd_bus_message_open_container(reply
, 'r', "qqqsa(iiay)s");
912 r
= dns_name_normalize(rr
->srv
.name
, 0, &normalized
);
916 r
= sd_bus_message_append(
919 rr
->srv
.priority
, rr
->srv
.weight
, rr
->srv
.port
, normalized
);
923 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
927 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
928 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
929 DnsResourceRecord
*zz
;
930 DnsQuestion
*question
;
933 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
935 if (aux
->auxiliary_result
!= 0)
938 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
940 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
946 DNS_ANSWER_FOREACH_IFINDEX(zz
, ifindex
, aux
->answer
) {
948 r
= dns_question_matches_rr(question
, zz
, NULL
);
954 r
= append_address(reply
, zz
, ifindex
);
961 r
= sd_bus_message_close_container(reply
);
966 normalized
= mfree(normalized
);
968 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), 0, &normalized
);
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
);
979 r
= sd_bus_message_close_container(reply
);
986 static int append_txt(sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
993 if (rr
->key
->type
!= DNS_TYPE_TXT
)
996 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
1001 r
= sd_bus_message_append_array(reply
, 'y', i
->data
, i
->length
);
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
;
1021 if (q
->block_all_complete
> 0) {
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 */
1039 case DNS_TRANSACTION_SUCCESS
:
1040 if (aux
->auxiliary_result
== 0)
1041 have_success
= true;
1052 if (!have_success
) {
1053 /* We can only return one error, hence pick the last error we encountered */
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
));
1065 assert(bad
->auxiliary_result
< 0);
1066 r
= bad
->auxiliary_result
;
1070 r
= reply_query_state(bad
);
1075 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
1079 r
= sd_bus_message_open_container(reply
, 'a', "(qqqsa(iiay)s)");
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
);
1092 r
= append_srv(q
, reply
, rr
);
1095 if (r
== 0) /* not an SRV record */
1099 canonical
= dns_resource_record_ref(rr
);
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
));
1109 r
= sd_bus_message_close_container(reply
);
1113 r
= sd_bus_message_open_container(reply
, 'a', "ay");
1117 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1118 r
= dns_question_matches_rr(question
, rr
, NULL
);
1124 r
= append_txt(reply
, rr
);
1129 r
= sd_bus_message_close_container(reply
);
1134 r
= dns_service_split(dns_resource_key_name(canonical
->key
), &name
, &type
, &domain
);
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
));
1143 r
= sd_bus_message_append(
1147 dns_query_reply_flags_make(q
));
1151 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
1152 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
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
) {
1165 assert(q
->auxiliary_for
);
1167 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1168 resolve_service_all_complete(q
->auxiliary_for
);
1172 r
= dns_query_process_cname_many(q
);
1173 if (r
== DNS_QUERY_CNAME
) /* This was a cname, and the query was restarted. */
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
;
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);
1198 r
= dns_query_new(q
->manager
, &aux
, question
, question
, NULL
, ifindex
, q
->flags
|SD_RESOLVED_NO_SEARCH
);
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
);
1208 /* Too many auxiliary lookups? If so, don't complain,
1209 * let's just not add this one, we already have more
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
);
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
;
1236 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1237 r
= reply_query_state(q
);
1241 r
= dns_query_process_cname_many(q
);
1243 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
1248 if (r
== DNS_QUERY_CNAME
) {
1249 /* This was a cname, and the query was restarted. */
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
);
1263 if (rr
->key
->type
!= DNS_TYPE_SRV
)
1266 if (dns_name_is_root(rr
->srv
.name
)) {
1267 has_root_domain
= true;
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
--;
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
));
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
));
1296 /* Maybe we are already finished? check now... */
1297 resolve_service_all_complete(TAKE_PTR(q
));
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
;
1318 assert_cc(sizeof(int) == sizeof(int32_t));
1320 r
= sd_bus_message_read(message
, "isssit", &ifindex
, &name
, &type
, &domain
, &family
, &flags
);
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
);
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
);
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
);
1344 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid domain '%s'", domain
);
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
);
1353 r
= dns_question_new_service(&question_utf8
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), false);
1357 r
= dns_question_new_service(&question_idna
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), true);
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
);
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
);
1375 r
= dns_query_go(q
);
1383 int bus_dns_server_append(
1384 sd_bus_message
*reply
,
1386 bool with_ifindex
, /* include "ifindex" field */
1387 bool extended
) { /* also include port number and server name */
1395 return sd_bus_message_append(reply
, "(iiayqs)", 0, AF_UNSPEC
, 0, 0, NULL
);
1397 return sd_bus_message_append(reply
, "(iiay)", 0, AF_UNSPEC
, 0);
1400 return sd_bus_message_append(reply
, "(iayqs)", AF_UNSPEC
, 0, 0, NULL
);
1402 return sd_bus_message_append(reply
, "(iay)", AF_UNSPEC
, 0);
1406 r
= sd_bus_message_open_container(
1409 with_ifindex
? (extended
? "iiayqs" : "iiay") :
1410 (extended
? "iayqs" : "iay"));
1415 r
= sd_bus_message_append(reply
, "i", dns_server_ifindex(s
));
1420 r
= sd_bus_message_append(reply
, "i", s
->family
);
1424 r
= sd_bus_message_append_array(reply
, 'y', &s
->address
, FAMILY_ADDRESS_SIZE(s
->family
));
1429 r
= sd_bus_message_append(reply
, "q", s
->port
);
1433 r
= sd_bus_message_append(reply
, "s", s
->server_name
);
1438 return sd_bus_message_close_container(reply
);
1441 static int bus_property_get_dns_servers_internal(
1444 const char *interface
,
1445 const char *property
,
1446 sd_bus_message
*reply
,
1448 sd_bus_error
*error
,
1451 Manager
*m
= ASSERT_PTR(userdata
);
1457 r
= sd_bus_message_open_container(reply
, 'a', extended
? "(iiayqs)" : "(iiay)");
1461 LIST_FOREACH(servers
, s
, m
->dns_servers
) {
1462 r
= bus_dns_server_append(reply
, s
, true, extended
);
1467 HASHMAP_FOREACH(l
, m
->links
)
1468 LIST_FOREACH(servers
, s
, l
->dns_servers
) {
1469 r
= bus_dns_server_append(reply
, s
, true, extended
);
1474 return sd_bus_message_close_container(reply
);
1477 static int bus_property_get_dns_servers(
1480 const char *interface
,
1481 const char *property
,
1482 sd_bus_message
*reply
,
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(
1491 const char *interface
,
1492 const char *property
,
1493 sd_bus_message
*reply
,
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(
1502 const char *interface
,
1503 const char *property
,
1504 sd_bus_message
*reply
,
1506 sd_bus_error
*error
,
1509 DnsServer
**f
= ASSERT_PTR(userdata
);
1514 r
= sd_bus_message_open_container(reply
, 'a', extended
? "(iiayqs)" : "(iiay)");
1518 LIST_FOREACH(servers
, s
, *f
) {
1519 r
= bus_dns_server_append(reply
, s
, true, extended
);
1524 return sd_bus_message_close_container(reply
);
1527 static int bus_property_get_fallback_dns_servers(
1530 const char *interface
,
1531 const char *property
,
1532 sd_bus_message
*reply
,
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(
1541 const char *interface
,
1542 const char *property
,
1543 sd_bus_message
*reply
,
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(
1552 const char *interface
,
1553 const char *property
,
1554 sd_bus_message
*reply
,
1556 sd_bus_error
*error
,
1564 s
= *(DnsServer
**) userdata
;
1566 return bus_dns_server_append(reply
, s
, true, extended
);
1569 static int bus_property_get_current_dns_server(
1572 const char *interface
,
1573 const char *property
,
1574 sd_bus_message
*reply
,
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(
1583 const char *interface
,
1584 const char *property
,
1585 sd_bus_message
*reply
,
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(
1594 const char *interface
,
1595 const char *property
,
1596 sd_bus_message
*reply
,
1598 sd_bus_error
*error
) {
1600 Manager
*m
= ASSERT_PTR(userdata
);
1606 r
= sd_bus_message_open_container(reply
, 'a', "(isb)");
1610 LIST_FOREACH(domains
, d
, m
->search_domains
) {
1611 r
= sd_bus_message_append(reply
, "(isb)", 0, d
->name
, d
->route_only
);
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
);
1624 return sd_bus_message_close_container(reply
);
1627 static int bus_property_get_transaction_statistics(
1630 const char *interface
,
1631 const char *property
,
1632 sd_bus_message
*reply
,
1634 sd_bus_error
*error
) {
1636 Manager
*m
= ASSERT_PTR(userdata
);
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(
1648 const char *interface
,
1649 const char *property
,
1650 sd_bus_message
*reply
,
1652 sd_bus_error
*error
) {
1654 uint64_t size
= 0, hit
= 0, miss
= 0;
1655 Manager
*m
= ASSERT_PTR(userdata
);
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(
1671 const char *interface
,
1672 const char *property
,
1673 sd_bus_message
*reply
,
1675 sd_bus_error
*error
) {
1677 Manager
*m
= ASSERT_PTR(userdata
);
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(
1696 const char *interface
,
1697 const char *property
,
1698 sd_bus_message
*reply
,
1700 sd_bus_error
*error
) {
1706 r
= resolv_conf_mode();
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
);
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
) {
1733 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
1735 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_LINK
, "Link %i not known", ifindex
);
1741 static int call_link_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
1743 Link
*l
= NULL
; /* avoid false maybe-uninitialized warning */
1749 r
= bus_message_read_ifindex(message
, error
, &ifindex
);
1753 r
= get_any_link(m
, ifindex
, &l
, error
);
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
);
1804 Link
*l
= NULL
; /* avoid false maybe-uninitialized warning */
1808 r
= bus_message_read_ifindex(message
, error
, &ifindex
);
1812 r
= get_any_link(m
, ifindex
, &l
, error
);
1816 p
= link_bus_path(l
);
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
);
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
);
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
);
1853 log_debug("Client of active request vanished, destroying DNS-SD service.");
1854 dnssd_service_free(s
);
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
);
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);
1879 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1883 r
= sd_bus_creds_get_euid(creds
, &euid
);
1886 service
->originator
= euid
;
1888 r
= sd_bus_message_read(message
, "sssqqq", &name
, &name_template
, &type
,
1889 &service
->port
, &service
->priority
,
1894 s
= hashmap_get(m
->dnssd_services
, name
);
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
);
1905 service
->name_template
= strdup(name_template
);
1906 if (!service
->name_template
)
1909 service
->type
= strdup(type
);
1913 r
= dnssd_render_instance_name(m
, service
, NULL
);
1917 r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_ARRAY
, "a{say}");
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);
1929 while ((r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_DICT_ENTRY
, "say")) > 0) {
1935 r
= sd_bus_message_read(message
, "s", &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
);
1949 r
= dnssd_txt_item_new_from_data(key
, value
, size
, &i
);
1953 LIST_INSERT_AFTER(items
, txt_data
->txts
, last
, i
);
1956 r
= sd_bus_message_exit_container(message
);
1964 r
= sd_bus_message_exit_container(message
);
1968 if (txt_data
->txts
) {
1969 LIST_PREPEND(items
, service
->txt_data_items
, txt_data
);
1976 r
= sd_bus_message_exit_container(message
);
1980 if (!service
->txt_data_items
) {
1981 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
1983 txt_data
= new0(DnssdTxtData
, 1);
1987 r
= dns_txt_item_new_empty(&txt_data
->txts
);
1991 LIST_PREPEND(items
, service
->txt_data_items
, txt_data
);
1995 r
= sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service
->name
, &path
);
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
);
2006 return 1; /* Polkit will call us back */
2008 r
= hashmap_ensure_put(&m
->dnssd_services
, &string_hash_ops
, service
->name
, service
);
2012 r
= sd_bus_track_new(sd_bus_message_get_bus(message
), &bus_track
, dnssd_service_on_bus_track
, service
);
2016 r
= sd_bus_track_add_sender(bus_track
, message
);
2020 service
->manager
= m
;
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
;
2039 r
= sd_bus_message_read(message
, "o", &path
);
2043 r
= sd_bus_path_decode(path
, "/org/freedesktop/resolve1/dnssd", &name
);
2045 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_DNSSD_SERVICE
, "DNS-SD service with object path '%s' does not exist", path
);
2049 s
= hashmap_get(m
->dnssd_services
, name
);
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
);
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
,
2108 SD_BUS_RESULT("a(qqqsa(iiay)s)", srv_data
,
2110 "s", canonical_name
,
2111 "s", canonical_type
,
2112 "s", canonical_domain
,
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
),
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
),
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
),
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
),
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
),
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
),
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
),
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
),
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
),
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
),
2169 bus_method_revert_link
,
2170 SD_BUS_VTABLE_UNPRIVILEGED
),
2171 SD_BUS_METHOD_WITH_ARGS("RegisterService",
2172 SD_BUS_ARGS("s", name
,
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
),
2185 bus_method_unregister_service
,
2186 SD_BUS_VTABLE_UNPRIVILEGED
),
2187 SD_BUS_METHOD_WITH_ARGS("ResetStatistics",
2190 bus_method_reset_statistics
,
2191 SD_BUS_VTABLE_UNPRIVILEGED
),
2192 SD_BUS_METHOD_WITH_ARGS("FlushCaches",
2195 bus_method_flush_caches
,
2196 SD_BUS_VTABLE_UNPRIVILEGED
),
2197 SD_BUS_METHOD_WITH_ARGS("ResetServerFeatures",
2200 bus_method_reset_server_features
,
2201 SD_BUS_VTABLE_UNPRIVILEGED
),
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
,
2214 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
2215 Manager
*m
= ASSERT_PTR(userdata
);
2220 r
= sd_bus_message_read(message
, "b", &b
);
2222 bus_log_parse_error(r
);
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
);
2241 int manager_connect_bus(Manager
*m
) {
2249 r
= bus_open_system_watch_bind_with_description(&m
->bus
, "bus-api-resolve");
2251 return log_error_errno(r
, "Failed to connect to system bus: %m");
2253 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
2257 r
= bus_log_control_api_register(m
->bus
);
2261 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.resolve1", 0, NULL
, NULL
);
2263 return log_error_errno(r
, "Failed to request name: %m");
2265 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
2267 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
2269 r
= bus_match_signal_async(
2274 match_prepare_for_sleep
,
2278 log_warning_errno(r
, "Failed to request match for PrepareForSleep, ignoring: %m");
2283 int _manager_send_changed(Manager
*manager
, const char *property
, ...) {
2286 if (sd_bus_is_ready(manager
->bus
) <= 0)
2289 char **l
= strv_from_stdarg_alloca(property
);
2291 int r
= sd_bus_emit_properties_changed_strv(
2293 "/org/freedesktop/resolve1",
2294 "org.freedesktop.resolve1.Manager",
2297 log_notice_errno(r
, "Failed to emit notification about changed property %s: %m", property
);