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-log-control-api.h"
7 #include "bus-message-util.h"
8 #include "bus-polkit.h"
9 #include "dns-domain.h"
10 #include "memory-util.h"
11 #include "missing_capability.h"
12 #include "resolved-bus.h"
13 #include "resolved-def.h"
14 #include "resolved-dns-synthesize.h"
15 #include "resolved-dnssd-bus.h"
16 #include "resolved-dnssd.h"
17 #include "resolved-link-bus.h"
18 #include "resolved-resolv-conf.h"
19 #include "socket-netlink.h"
20 #include "stdio-util.h"
22 #include "syslog-util.h"
23 #include "user-util.h"
26 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_resolve_support
, resolve_support
, ResolveSupport
);
28 static int query_on_bus_track(sd_bus_track
*t
, void *userdata
) {
29 DnsQuery
*q
= userdata
;
34 if (!DNS_TRANSACTION_IS_LIVE(q
->state
))
37 log_debug("Client of active query vanished, aborting query.");
38 dns_query_complete(q
, DNS_TRANSACTION_ABORTED
);
42 static int dns_query_bus_track(DnsQuery
*q
, sd_bus_message
*m
) {
49 r
= sd_bus_track_new(sd_bus_message_get_bus(m
), &q
->bus_track
, query_on_bus_track
, q
);
54 r
= sd_bus_track_add_sender(q
->bus_track
, m
);
61 static int reply_query_state(DnsQuery
*q
) {
64 assert(q
->bus_request
);
68 case DNS_TRANSACTION_NO_SERVERS
:
69 return sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_NO_NAME_SERVERS
, "No appropriate name servers or networks for name found");
71 case DNS_TRANSACTION_TIMEOUT
:
72 return sd_bus_reply_method_errorf(q
->bus_request
, SD_BUS_ERROR_TIMEOUT
, "Query timed out");
74 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
:
75 return sd_bus_reply_method_errorf(q
->bus_request
, SD_BUS_ERROR_TIMEOUT
, "All attempts to contact name servers or networks failed");
77 case DNS_TRANSACTION_INVALID_REPLY
:
78 return sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_INVALID_REPLY
, "Received invalid reply");
80 case DNS_TRANSACTION_ERRNO
:
81 return sd_bus_reply_method_errnof(q
->bus_request
, q
->answer_errno
, "Lookup failed due to system error: %m");
83 case DNS_TRANSACTION_ABORTED
:
84 return sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_ABORTED
, "Query aborted");
86 case DNS_TRANSACTION_DNSSEC_FAILED
:
87 return sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_DNSSEC_FAILED
, "DNSSEC validation failed: %s",
88 dnssec_result_to_string(q
->answer_dnssec_result
));
90 case DNS_TRANSACTION_NO_TRUST_ANCHOR
:
91 return sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_NO_TRUST_ANCHOR
, "No suitable trust anchor known");
93 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
:
94 return sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_RR_TYPE_UNSUPPORTED
, "Server does not support requested resource record type");
96 case DNS_TRANSACTION_NETWORK_DOWN
:
97 return sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_NETWORK_DOWN
, "Network is down");
99 case DNS_TRANSACTION_NOT_FOUND
:
100 /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
101 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
102 return sd_bus_reply_method_errorf(q
->bus_request
, _BUS_ERROR_DNS
"NXDOMAIN", "'%s' not found", dns_query_string(q
));
104 case DNS_TRANSACTION_RCODE_FAILURE
: {
105 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
107 if (q
->answer_rcode
== DNS_RCODE_NXDOMAIN
)
108 sd_bus_error_setf(&error
, _BUS_ERROR_DNS
"NXDOMAIN", "'%s' not found", dns_query_string(q
));
111 char p
[DECIMAL_STR_MAX(q
->answer_rcode
)];
113 rc
= dns_rcode_to_string(q
->answer_rcode
);
115 xsprintf(p
, "%i", q
->answer_rcode
);
119 n
= strjoina(_BUS_ERROR_DNS
, rc
);
120 sd_bus_error_setf(&error
, n
, "Could not resolve '%s', server or network returned error %s", dns_query_string(q
), rc
);
123 return sd_bus_reply_method_error(q
->bus_request
, &error
);
126 case DNS_TRANSACTION_NULL
:
127 case DNS_TRANSACTION_PENDING
:
128 case DNS_TRANSACTION_VALIDATING
:
129 case DNS_TRANSACTION_SUCCESS
:
131 assert_not_reached("Impossible state");
135 static int append_address(sd_bus_message
*reply
, DnsResourceRecord
*rr
, int ifindex
) {
141 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
145 r
= sd_bus_message_append(reply
, "i", ifindex
);
149 if (rr
->key
->type
== DNS_TYPE_A
) {
150 r
= sd_bus_message_append(reply
, "i", AF_INET
);
154 r
= sd_bus_message_append_array(reply
, 'y', &rr
->a
.in_addr
, sizeof(struct in_addr
));
156 } else if (rr
->key
->type
== DNS_TYPE_AAAA
) {
157 r
= sd_bus_message_append(reply
, "i", AF_INET6
);
161 r
= sd_bus_message_append_array(reply
, 'y', &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
));
163 return -EAFNOSUPPORT
;
168 r
= sd_bus_message_close_container(reply
);
175 static void bus_method_resolve_hostname_complete(DnsQuery
*q
) {
176 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
177 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
178 _cleanup_free_
char *normalized
= NULL
;
179 DnsQuestion
*question
;
180 DnsResourceRecord
*rr
;
186 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
187 r
= reply_query_state(q
);
191 r
= dns_query_process_cname(q
);
193 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
198 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
201 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
205 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
209 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
211 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
213 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
219 r
= append_address(reply
, rr
, ifindex
);
224 canonical
= dns_resource_record_ref(rr
);
230 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
234 r
= sd_bus_message_close_container(reply
);
238 /* The key names are not necessarily normalized, make sure that they are when we return them to our
241 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), 0, &normalized
);
245 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
246 r
= sd_bus_message_append(
249 SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, dns_query_fully_authenticated(q
)));
253 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
257 log_error_errno(r
, "Failed to send hostname reply: %m");
258 sd_bus_reply_method_errno(q
->bus_request
, r
, NULL
);
264 static int validate_and_mangle_flags(
268 sd_bus_error
*error
) {
272 /* Checks that the client supplied interface index and flags parameter actually are valid and make
273 * sense in our method call context. Specifically:
275 * 1. Checks that the interface index is either 0 (meaning *all* interfaces) or positive
277 * 2. Only the protocols flags and the NO_CNAME flag are set, at most. Plus additional flags specific
278 * to our method, passed in the "ok" parameter.
280 * 3. If zero protocol flags are specified it is automatically turned into *all* protocols. This way
281 * clients can simply pass 0 as flags and all will work as it should. They can also use this so
282 * that clients don't have to know all the protocols resolved implements, but can just specify 0
283 * to mean "all supported protocols".
286 if (*flags
& ~(SD_RESOLVED_PROTOCOLS_ALL
|SD_RESOLVED_NO_CNAME
|ok
))
287 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid flags parameter");
289 if ((*flags
& SD_RESOLVED_PROTOCOLS_ALL
) == 0) /* If no protocol is enabled, enable all */
290 *flags
|= SD_RESOLVED_PROTOCOLS_ALL
;
292 /* Imply SD_RESOLVED_NO_SEARCH if permitted and name is dot suffixed. */
293 if (name
&& FLAGS_SET(ok
, SD_RESOLVED_NO_SEARCH
) && dns_name_dot_suffixed(name
) > 0)
294 *flags
|= SD_RESOLVED_NO_SEARCH
;
299 static int parse_as_address(sd_bus_message
*m
, int ifindex
, const char *hostname
, int family
, uint64_t flags
) {
300 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
301 _cleanup_free_
char *canonical
= NULL
;
302 union in_addr_union parsed
;
303 int r
, ff
, parsed_ifindex
= 0;
305 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
306 * let's not attempt to look it up. */
308 r
= in_addr_ifindex_from_string_auto(hostname
, &ff
, &parsed
, &parsed_ifindex
);
309 if (r
< 0) /* not an address */
312 if (family
!= AF_UNSPEC
&& ff
!= family
)
313 return sd_bus_reply_method_errorf(m
, BUS_ERROR_NO_SUCH_RR
, "The specified address is not of the requested family.");
314 if (ifindex
> 0 && parsed_ifindex
> 0 && parsed_ifindex
!= ifindex
)
315 return sd_bus_reply_method_errorf(m
, BUS_ERROR_NO_SUCH_RR
, "The specified address interface index does not match requested interface.");
317 if (parsed_ifindex
> 0)
318 ifindex
= parsed_ifindex
;
320 r
= sd_bus_message_new_method_return(m
, &reply
);
324 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
328 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
332 r
= sd_bus_message_append(reply
, "ii", ifindex
, ff
);
336 r
= sd_bus_message_append_array(reply
, 'y', &parsed
, FAMILY_ADDRESS_SIZE(ff
));
340 r
= sd_bus_message_close_container(reply
);
344 r
= sd_bus_message_close_container(reply
);
348 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
349 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
350 * omissions are always done the same way). */
351 r
= in_addr_ifindex_to_string(ff
, &parsed
, ifindex
, &canonical
);
355 r
= sd_bus_message_append(reply
, "st", canonical
,
356 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags
), ff
, true));
360 return sd_bus_send(sd_bus_message_get_bus(m
), reply
, NULL
);
363 static int bus_method_resolve_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
364 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
365 Manager
*m
= userdata
;
366 const char *hostname
;
375 assert_cc(sizeof(int) == sizeof(int32_t));
377 r
= sd_bus_message_read(message
, "isit", &ifindex
, &hostname
, &family
, &flags
);
382 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
384 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
385 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
387 r
= validate_and_mangle_flags(hostname
, &flags
, SD_RESOLVED_NO_SEARCH
, error
);
391 r
= parse_as_address(message
, ifindex
, hostname
, family
, flags
);
395 r
= dns_name_is_valid(hostname
);
399 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid hostname '%s'", hostname
);
401 r
= dns_question_new_address(&question_utf8
, family
, hostname
, false);
405 r
= dns_question_new_address(&question_idna
, family
, hostname
, true);
406 if (r
< 0 && r
!= -EALREADY
)
409 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
?: question_utf8
, ifindex
, flags
);
413 q
->bus_request
= sd_bus_message_ref(message
);
414 q
->request_family
= family
;
415 q
->complete
= bus_method_resolve_hostname_complete
;
417 r
= dns_query_bus_track(q
, message
);
432 static void bus_method_resolve_address_complete(DnsQuery
*q
) {
433 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
434 DnsQuestion
*question
;
435 DnsResourceRecord
*rr
;
441 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
442 r
= reply_query_state(q
);
446 r
= dns_query_process_cname(q
);
448 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
453 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
456 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
460 r
= sd_bus_message_open_container(reply
, 'a', "(is)");
464 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
466 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
467 _cleanup_free_
char *normalized
= NULL
;
469 r
= dns_question_matches_rr(question
, rr
, NULL
);
475 r
= dns_name_normalize(rr
->ptr
.name
, 0, &normalized
);
479 r
= sd_bus_message_append(reply
, "(is)", ifindex
, normalized
);
487 _cleanup_free_
char *ip
= NULL
;
489 (void) in_addr_to_string(q
->request_family
, &q
->request_address
, &ip
);
490 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_NO_SUCH_RR
,
491 "Address '%s' does not have any RR of requested type", strnull(ip
));
495 r
= sd_bus_message_close_container(reply
);
499 r
= sd_bus_message_append(reply
, "t", SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, dns_query_fully_authenticated(q
)));
503 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
507 log_error_errno(r
, "Failed to send address reply: %m");
508 sd_bus_reply_method_errno(q
->bus_request
, r
, NULL
);
514 static int bus_method_resolve_address(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
515 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
516 Manager
*m
= userdata
;
517 union in_addr_union a
;
526 assert_cc(sizeof(int) == sizeof(int32_t));
528 r
= sd_bus_message_read(message
, "i", &ifindex
);
532 r
= bus_message_read_in_addr_auto(message
, error
, &family
, &a
);
536 r
= sd_bus_message_read(message
, "t", &flags
);
541 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
543 r
= validate_and_mangle_flags(NULL
, &flags
, 0, error
);
547 r
= dns_question_new_reverse(&question
, family
, &a
);
551 r
= dns_query_new(m
, &q
, question
, question
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
555 q
->bus_request
= sd_bus_message_ref(message
);
556 q
->request_family
= family
;
557 q
->request_address
= a
;
558 q
->complete
= bus_method_resolve_address_complete
;
560 r
= dns_query_bus_track(q
, message
);
575 static int bus_message_append_rr(sd_bus_message
*m
, DnsResourceRecord
*rr
, int ifindex
) {
581 r
= sd_bus_message_open_container(m
, 'r', "iqqay");
585 r
= sd_bus_message_append(m
, "iqq",
592 r
= dns_resource_record_to_wire_format(rr
, false);
596 r
= sd_bus_message_append_array(m
, 'y', rr
->wire_format
, rr
->wire_format_size
);
600 return sd_bus_message_close_container(m
);
603 static void bus_method_resolve_record_complete(DnsQuery
*q
) {
604 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
605 DnsResourceRecord
*rr
;
606 DnsQuestion
*question
;
613 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
614 r
= reply_query_state(q
);
618 r
= dns_query_process_cname(q
);
620 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
625 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
628 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
632 r
= sd_bus_message_open_container(reply
, 'a', "(iqqay)");
636 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
638 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
639 r
= dns_question_matches_rr(question
, rr
, NULL
);
645 r
= bus_message_append_rr(reply
, rr
, ifindex
);
653 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_NO_SUCH_RR
, "Name '%s' does not have any RR of the requested type", dns_query_string(q
));
657 r
= sd_bus_message_close_container(reply
);
661 r
= sd_bus_message_append(reply
, "t", SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, dns_query_fully_authenticated(q
)));
665 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
669 log_error_errno(r
, "Failed to send record reply: %m");
670 sd_bus_reply_method_errno(q
->bus_request
, r
, NULL
);
676 static int bus_method_resolve_record(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
677 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
678 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
679 Manager
*m
= userdata
;
680 uint16_t class, type
;
689 assert_cc(sizeof(int) == sizeof(int32_t));
691 r
= sd_bus_message_read(message
, "isqqt", &ifindex
, &name
, &class, &type
, &flags
);
696 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
698 r
= dns_name_is_valid(name
);
702 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid name '%s'", name
);
704 if (!dns_type_is_valid_query(type
))
705 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified resource record type %" PRIu16
" may not be used in a query.", type
);
706 if (dns_type_is_zone_transer(type
))
707 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Zone transfers not permitted via this programming interface.");
708 if (dns_type_is_obsolete(type
))
709 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Specified DNS resource record type %" PRIu16
" is obsolete.", type
);
711 r
= validate_and_mangle_flags(name
, &flags
, 0, error
);
715 question
= dns_question_new(1);
719 key
= dns_resource_key_new(class, type
, name
);
723 r
= dns_question_add(question
, key
);
727 r
= dns_query_new(m
, &q
, question
, question
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
731 /* Let's request that the TTL is fixed up for locally cached entries, after all we return it in the wire format
735 q
->bus_request
= sd_bus_message_ref(message
);
736 q
->complete
= bus_method_resolve_record_complete
;
738 r
= dns_query_bus_track(q
, message
);
753 static int append_srv(DnsQuery
*q
, sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
754 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
755 _cleanup_free_
char *normalized
= NULL
;
764 if (rr
->key
->type
!= DNS_TYPE_SRV
)
767 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
768 /* First, let's see if we could find an appropriate A or AAAA
769 * record for the SRV record */
770 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
771 DnsResourceRecord
*zz
;
772 DnsQuestion
*question
;
774 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
776 if (aux
->auxiliary_result
!= 0)
779 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
781 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
787 DNS_ANSWER_FOREACH(zz
, aux
->answer
) {
789 r
= dns_question_matches_rr(question
, zz
, NULL
);
795 canonical
= dns_resource_record_ref(zz
);
803 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
808 r
= sd_bus_message_open_container(reply
, 'r', "qqqsa(iiay)s");
812 r
= dns_name_normalize(rr
->srv
.name
, 0, &normalized
);
816 r
= sd_bus_message_append(
819 rr
->srv
.priority
, rr
->srv
.weight
, rr
->srv
.port
, normalized
);
823 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
827 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
828 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
829 DnsResourceRecord
*zz
;
830 DnsQuestion
*question
;
833 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
835 if (aux
->auxiliary_result
!= 0)
838 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
840 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
846 DNS_ANSWER_FOREACH_IFINDEX(zz
, ifindex
, aux
->answer
) {
848 r
= dns_question_matches_rr(question
, zz
, NULL
);
854 r
= append_address(reply
, zz
, ifindex
);
861 r
= sd_bus_message_close_container(reply
);
866 normalized
= mfree(normalized
);
868 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), 0, &normalized
);
873 /* Note that above we appended the hostname as encoded in the
874 * SRV, and here the canonical hostname this maps to. */
875 r
= sd_bus_message_append(reply
, "s", normalized
);
879 r
= sd_bus_message_close_container(reply
);
886 static int append_txt(sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
894 if (rr
->key
->type
!= DNS_TYPE_TXT
)
897 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
902 r
= sd_bus_message_append_array(reply
, 'y', i
->data
, i
->length
);
910 static void resolve_service_all_complete(DnsQuery
*q
) {
911 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
912 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
913 _cleanup_free_
char *name
= NULL
, *type
= NULL
, *domain
= NULL
;
914 DnsQuestion
*question
;
915 DnsResourceRecord
*rr
;
922 if (q
->block_all_complete
> 0)
925 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
926 DnsQuery
*bad
= NULL
;
927 bool have_success
= false;
929 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
931 switch (aux
->state
) {
933 case DNS_TRANSACTION_PENDING
:
934 /* If an auxiliary query is still pending, let's wait */
937 case DNS_TRANSACTION_SUCCESS
:
938 if (aux
->auxiliary_result
== 0)
951 /* We can only return one error, hence pick the last error we encountered */
955 if (bad
->state
== DNS_TRANSACTION_SUCCESS
) {
956 assert(bad
->auxiliary_result
!= 0);
958 if (bad
->auxiliary_result
== -ELOOP
) {
959 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad
));
963 r
= bad
->auxiliary_result
;
967 r
= reply_query_state(bad
);
972 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
976 r
= sd_bus_message_open_container(reply
, 'a', "(qqqsa(iiay)s)");
980 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
982 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
983 r
= dns_question_matches_rr(question
, rr
, NULL
);
989 r
= append_srv(q
, reply
, rr
);
992 if (r
== 0) /* not an SRV record */
996 canonical
= dns_resource_record_ref(rr
);
1002 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
1006 r
= sd_bus_message_close_container(reply
);
1010 r
= sd_bus_message_open_container(reply
, 'a', "ay");
1014 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1015 r
= dns_question_matches_rr(question
, rr
, NULL
);
1021 r
= append_txt(reply
, rr
);
1026 r
= sd_bus_message_close_container(reply
);
1031 r
= dns_service_split(dns_resource_key_name(canonical
->key
), &name
, &type
, &domain
);
1035 r
= sd_bus_message_append(
1039 SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, dns_query_fully_authenticated(q
)));
1043 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
1047 log_error_errno(r
, "Failed to send service reply: %m");
1048 sd_bus_reply_method_errno(q
->bus_request
, r
, NULL
);
1054 static void resolve_service_hostname_complete(DnsQuery
*q
) {
1058 assert(q
->auxiliary_for
);
1060 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1061 resolve_service_all_complete(q
->auxiliary_for
);
1065 r
= dns_query_process_cname(q
);
1066 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
1069 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1070 q
->auxiliary_result
= r
;
1071 resolve_service_all_complete(q
->auxiliary_for
);
1074 static int resolve_service_hostname(DnsQuery
*q
, DnsResourceRecord
*rr
, int ifindex
) {
1075 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
1082 assert(rr
->key
->type
== DNS_TYPE_SRV
);
1084 /* OK, we found an SRV record for the service. Let's resolve
1085 * the hostname included in it */
1087 r
= dns_question_new_address(&question
, q
->request_family
, rr
->srv
.name
, false);
1091 r
= dns_query_new(q
->manager
, &aux
, question
, question
, ifindex
, q
->flags
|SD_RESOLVED_NO_SEARCH
);
1095 aux
->request_family
= q
->request_family
;
1096 aux
->complete
= resolve_service_hostname_complete
;
1098 r
= dns_query_make_auxiliary(aux
, q
);
1100 /* Too many auxiliary lookups? If so, don't complain,
1101 * let's just not add this one, we already have more
1104 dns_query_free(aux
);
1110 /* Note that auxiliary queries do not track the original bus
1111 * client, only the primary request does that. */
1113 r
= dns_query_go(aux
);
1120 dns_query_free(aux
);
1124 static void bus_method_resolve_service_complete(DnsQuery
*q
) {
1125 bool has_root_domain
= false;
1126 DnsResourceRecord
*rr
;
1127 DnsQuestion
*question
;
1133 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1134 r
= reply_query_state(q
);
1138 r
= dns_query_process_cname(q
);
1140 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
1145 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
1148 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1150 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
1151 r
= dns_question_matches_rr(question
, rr
, NULL
);
1157 if (rr
->key
->type
!= DNS_TYPE_SRV
)
1160 if (dns_name_is_root(rr
->srv
.name
)) {
1161 has_root_domain
= true;
1165 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
1166 q
->block_all_complete
++;
1167 r
= resolve_service_hostname(q
, rr
, ifindex
);
1168 q
->block_all_complete
--;
1177 if (has_root_domain
&& found
<= 0) {
1178 /* If there's exactly one SRV RR and it uses
1179 * the root domain as hostname, then the
1180 * service is explicitly not offered on the
1181 * domain. Report this as a recognizable
1182 * error. See RFC 2782, Section "Usage
1184 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_NO_SUCH_SERVICE
, "'%s' does not provide the requested service", dns_query_string(q
));
1189 r
= sd_bus_reply_method_errorf(q
->bus_request
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
1193 /* Maybe we are already finished? check now... */
1194 resolve_service_all_complete(q
);
1199 log_error_errno(r
, "Failed to send service reply: %m");
1200 sd_bus_reply_method_errno(q
->bus_request
, r
, NULL
);
1206 static int bus_method_resolve_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1207 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
1208 const char *name
, *type
, *domain
;
1209 Manager
*m
= userdata
;
1210 int family
, ifindex
;
1218 assert_cc(sizeof(int) == sizeof(int32_t));
1220 r
= sd_bus_message_read(message
, "isssit", &ifindex
, &name
, &type
, &domain
, &family
, &flags
);
1225 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
1227 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
1228 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
1232 else if (!dns_service_name_is_valid(name
))
1233 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid service name '%s'", name
);
1237 else if (!dns_srv_type_is_valid(type
))
1238 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid SRV service type '%s'", type
);
1240 r
= dns_name_is_valid(domain
);
1244 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid domain '%s'", domain
);
1247 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Service name cannot be specified without service type.");
1249 r
= validate_and_mangle_flags(name
, &flags
, SD_RESOLVED_NO_TXT
|SD_RESOLVED_NO_ADDRESS
, error
);
1253 r
= dns_question_new_service(&question_utf8
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), false);
1257 r
= dns_question_new_service(&question_idna
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), true);
1261 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
1265 q
->bus_request
= sd_bus_message_ref(message
);
1266 q
->request_family
= family
;
1267 q
->complete
= bus_method_resolve_service_complete
;
1269 r
= dns_query_bus_track(q
, message
);
1273 r
= dns_query_go(q
);
1284 int bus_dns_server_append(sd_bus_message
*reply
, DnsServer
*s
, bool with_ifindex
, bool extended
) {
1292 return sd_bus_message_append(reply
, "(iiayqs)", 0, AF_UNSPEC
, 0, 0, NULL
);
1294 return sd_bus_message_append(reply
, "(iiay)", 0, AF_UNSPEC
, 0);
1297 return sd_bus_message_append(reply
, "(iayqs)", AF_UNSPEC
, 0, 0, NULL
);
1299 return sd_bus_message_append(reply
, "(iay)", AF_UNSPEC
, 0);
1303 r
= sd_bus_message_open_container(reply
, 'r', with_ifindex
? (extended
? "iiayqs" : "iiay") : (extended
? "iayqs" : "iay"));
1308 r
= sd_bus_message_append(reply
, "i", dns_server_ifindex(s
));
1313 r
= sd_bus_message_append(reply
, "i", s
->family
);
1317 r
= sd_bus_message_append_array(reply
, 'y', &s
->address
, FAMILY_ADDRESS_SIZE(s
->family
));
1322 r
= sd_bus_message_append(reply
, "q", s
->port
);
1326 r
= sd_bus_message_append(reply
, "s", s
->server_name
);
1331 return sd_bus_message_close_container(reply
);
1334 static int bus_property_get_dns_servers_internal(
1337 const char *interface
,
1338 const char *property
,
1339 sd_bus_message
*reply
,
1341 sd_bus_error
*error
,
1344 Manager
*m
= userdata
;
1352 r
= sd_bus_message_open_container(reply
, 'a', extended
? "(iiayqs)" : "(iiay)");
1356 LIST_FOREACH(servers
, s
, m
->dns_servers
) {
1357 r
= bus_dns_server_append(reply
, s
, true, extended
);
1362 HASHMAP_FOREACH(l
, m
->links
)
1363 LIST_FOREACH(servers
, s
, l
->dns_servers
) {
1364 r
= bus_dns_server_append(reply
, s
, true, extended
);
1369 return sd_bus_message_close_container(reply
);
1372 static int bus_property_get_dns_servers(
1375 const char *interface
,
1376 const char *property
,
1377 sd_bus_message
*reply
,
1379 sd_bus_error
*error
) {
1380 return bus_property_get_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1383 static int bus_property_get_dns_servers_ex(
1386 const char *interface
,
1387 const char *property
,
1388 sd_bus_message
*reply
,
1390 sd_bus_error
*error
) {
1391 return bus_property_get_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1394 static int bus_property_get_fallback_dns_servers_internal(
1397 const char *interface
,
1398 const char *property
,
1399 sd_bus_message
*reply
,
1401 sd_bus_error
*error
,
1404 DnsServer
*s
, **f
= userdata
;
1410 r
= sd_bus_message_open_container(reply
, 'a', extended
? "(iiayqs)" : "(iiay)");
1414 LIST_FOREACH(servers
, s
, *f
) {
1415 r
= bus_dns_server_append(reply
, s
, true, extended
);
1420 return sd_bus_message_close_container(reply
);
1423 static int bus_property_get_fallback_dns_servers(
1426 const char *interface
,
1427 const char *property
,
1428 sd_bus_message
*reply
,
1430 sd_bus_error
*error
) {
1431 return bus_property_get_fallback_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1434 static int bus_property_get_fallback_dns_servers_ex(
1437 const char *interface
,
1438 const char *property
,
1439 sd_bus_message
*reply
,
1441 sd_bus_error
*error
) {
1442 return bus_property_get_fallback_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1445 static int bus_property_get_current_dns_server_internal(
1448 const char *interface
,
1449 const char *property
,
1450 sd_bus_message
*reply
,
1452 sd_bus_error
*error
,
1460 s
= *(DnsServer
**) userdata
;
1462 return bus_dns_server_append(reply
, s
, true, extended
);
1465 static int bus_property_get_current_dns_server(
1468 const char *interface
,
1469 const char *property
,
1470 sd_bus_message
*reply
,
1472 sd_bus_error
*error
) {
1473 return bus_property_get_current_dns_server_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1476 static int bus_property_get_current_dns_server_ex(
1479 const char *interface
,
1480 const char *property
,
1481 sd_bus_message
*reply
,
1483 sd_bus_error
*error
) {
1484 return bus_property_get_current_dns_server_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1487 static int bus_property_get_domains(
1490 const char *interface
,
1491 const char *property
,
1492 sd_bus_message
*reply
,
1494 sd_bus_error
*error
) {
1496 Manager
*m
= userdata
;
1504 r
= sd_bus_message_open_container(reply
, 'a', "(isb)");
1508 LIST_FOREACH(domains
, d
, m
->search_domains
) {
1509 r
= sd_bus_message_append(reply
, "(isb)", 0, d
->name
, d
->route_only
);
1514 HASHMAP_FOREACH(l
, m
->links
) {
1515 LIST_FOREACH(domains
, d
, l
->search_domains
) {
1516 r
= sd_bus_message_append(reply
, "(isb)", l
->ifindex
, d
->name
, d
->route_only
);
1522 return sd_bus_message_close_container(reply
);
1525 static int bus_property_get_transaction_statistics(
1528 const char *interface
,
1529 const char *property
,
1530 sd_bus_message
*reply
,
1532 sd_bus_error
*error
) {
1534 Manager
*m
= userdata
;
1539 return sd_bus_message_append(reply
, "(tt)",
1540 (uint64_t) hashmap_size(m
->dns_transactions
),
1541 (uint64_t) m
->n_transactions_total
);
1544 static int bus_property_get_cache_statistics(
1547 const char *interface
,
1548 const char *property
,
1549 sd_bus_message
*reply
,
1551 sd_bus_error
*error
) {
1553 uint64_t size
= 0, hit
= 0, miss
= 0;
1554 Manager
*m
= userdata
;
1560 LIST_FOREACH(scopes
, s
, m
->dns_scopes
) {
1561 size
+= dns_cache_size(&s
->cache
);
1562 hit
+= s
->cache
.n_hit
;
1563 miss
+= s
->cache
.n_miss
;
1566 return sd_bus_message_append(reply
, "(ttt)", size
, hit
, miss
);
1569 static int bus_property_get_dnssec_statistics(
1572 const char *interface
,
1573 const char *property
,
1574 sd_bus_message
*reply
,
1576 sd_bus_error
*error
) {
1578 Manager
*m
= userdata
;
1583 return sd_bus_message_append(reply
, "(tttt)",
1584 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_SECURE
],
1585 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INSECURE
],
1586 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_BOGUS
],
1587 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INDETERMINATE
]);
1590 static int bus_property_get_ntas(
1593 const char *interface
,
1594 const char *property
,
1595 sd_bus_message
*reply
,
1597 sd_bus_error
*error
) {
1599 Manager
*m
= userdata
;
1606 r
= sd_bus_message_open_container(reply
, 'a', "s");
1610 SET_FOREACH(domain
, m
->trust_anchor
.negative_by_name
) {
1611 r
= sd_bus_message_append(reply
, "s", domain
);
1616 return sd_bus_message_close_container(reply
);
1619 static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode
, dns_stub_listener_mode
, DnsStubListenerMode
);
1620 static BUS_DEFINE_PROPERTY_GET(bus_property_get_dnssec_supported
, "b", Manager
, manager_dnssec_supported
);
1621 static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dnssec_mode
, "s", Manager
, manager_get_dnssec_mode
, dnssec_mode_to_string
);
1622 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
);
1624 static int bus_property_get_resolv_conf_mode(
1627 const char *interface
,
1628 const char *property
,
1629 sd_bus_message
*reply
,
1631 sd_bus_error
*error
) {
1637 r
= resolv_conf_mode();
1639 log_warning_errno(r
, "Failed to test /etc/resolv.conf mode, ignoring: %m");
1640 return sd_bus_message_append(reply
, "s", NULL
);
1643 return sd_bus_message_append(reply
, "s", resolv_conf_mode_to_string(r
));
1646 static int bus_method_reset_statistics(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1647 Manager
*m
= userdata
;
1653 LIST_FOREACH(scopes
, s
, m
->dns_scopes
)
1654 s
->cache
.n_hit
= s
->cache
.n_miss
= 0;
1656 m
->n_transactions_total
= 0;
1657 zero(m
->n_dnssec_verdict
);
1659 return sd_bus_reply_method_return(message
, NULL
);
1662 static int get_any_link(Manager
*m
, int ifindex
, Link
**ret
, sd_bus_error
*error
) {
1668 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
1670 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_LINK
, "Link %i not known", ifindex
);
1676 static int call_link_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
1684 r
= bus_message_read_ifindex(message
, error
, &ifindex
);
1688 r
= get_any_link(m
, ifindex
, &l
, error
);
1692 return handler(message
, l
, error
);
1695 static int bus_method_set_link_dns_servers(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1696 return call_link_method(userdata
, message
, bus_link_method_set_dns_servers
, error
);
1699 static int bus_method_set_link_dns_servers_ex(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1700 return call_link_method(userdata
, message
, bus_link_method_set_dns_servers_ex
, error
);
1703 static int bus_method_set_link_domains(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1704 return call_link_method(userdata
, message
, bus_link_method_set_domains
, error
);
1707 static int bus_method_set_link_default_route(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1708 return call_link_method(userdata
, message
, bus_link_method_set_default_route
, error
);
1711 static int bus_method_set_link_llmnr(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1712 return call_link_method(userdata
, message
, bus_link_method_set_llmnr
, error
);
1715 static int bus_method_set_link_mdns(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1716 return call_link_method(userdata
, message
, bus_link_method_set_mdns
, error
);
1719 static int bus_method_set_link_dns_over_tls(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1720 return call_link_method(userdata
, message
, bus_link_method_set_dns_over_tls
, error
);
1723 static int bus_method_set_link_dnssec(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1724 return call_link_method(userdata
, message
, bus_link_method_set_dnssec
, error
);
1727 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1728 return call_link_method(userdata
, message
, bus_link_method_set_dnssec_negative_trust_anchors
, error
);
1731 static int bus_method_revert_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1732 return call_link_method(userdata
, message
, bus_link_method_revert
, error
);
1735 static int bus_method_get_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1736 _cleanup_free_
char *p
= NULL
;
1737 Manager
*m
= userdata
;
1744 r
= bus_message_read_ifindex(message
, error
, &ifindex
);
1748 r
= get_any_link(m
, ifindex
, &l
, error
);
1752 p
= link_bus_path(l
);
1756 return sd_bus_reply_method_return(message
, "o", p
);
1759 static int bus_method_flush_caches(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1760 Manager
*m
= userdata
;
1765 manager_flush_caches(m
);
1767 return sd_bus_reply_method_return(message
, NULL
);
1770 static int bus_method_reset_server_features(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1771 Manager
*m
= userdata
;
1776 manager_reset_server_features(m
);
1778 return sd_bus_reply_method_return(message
, NULL
);
1781 static int dnssd_service_on_bus_track(sd_bus_track
*t
, void *userdata
) {
1782 DnssdService
*s
= userdata
;
1787 log_debug("Client of active request vanished, destroying DNS-SD service.");
1788 dnssd_service_free(s
);
1793 static int bus_method_register_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1794 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1795 _cleanup_(dnssd_service_freep
) DnssdService
*service
= NULL
;
1796 _cleanup_(sd_bus_track_unrefp
) sd_bus_track
*bus_track
= NULL
;
1797 _cleanup_free_
char *path
= NULL
;
1798 Manager
*m
= userdata
;
1799 DnssdService
*s
= NULL
;
1801 const char *name_template
;
1809 if (m
->mdns_support
!= RESOLVE_SUPPORT_YES
)
1810 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Support for MulticastDNS is disabled");
1812 service
= new0(DnssdService
, 1);
1816 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1820 r
= sd_bus_creds_get_euid(creds
, &euid
);
1823 service
->originator
= euid
;
1825 r
= sd_bus_message_read(message
, "sssqqq", &name
, &name_template
, &type
,
1826 &service
->port
, &service
->priority
,
1831 s
= hashmap_get(m
->dnssd_services
, name
);
1833 return sd_bus_error_setf(error
, BUS_ERROR_DNSSD_SERVICE_EXISTS
, "DNS-SD service '%s' exists already", name
);
1835 if (!dnssd_srv_type_is_valid(type
))
1836 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DNS-SD service type '%s' is invalid", type
);
1838 r
= dnssd_render_instance_name(name_template
, NULL
);
1840 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DNS-SD service name '%s' is invalid", name_template
);
1842 service
->name
= strdup(name
);
1846 service
->name_template
= strdup(name_template
);
1847 if (!service
->name_template
)
1850 service
->type
= strdup(type
);
1854 r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_ARRAY
, "a{say}");
1858 while ((r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_ARRAY
, "{say}")) > 0) {
1859 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
1860 DnsTxtItem
*last
= NULL
;
1862 txt_data
= new0(DnssdTxtData
, 1);
1866 while ((r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_DICT_ENTRY
, "say")) > 0) {
1872 r
= sd_bus_message_read(message
, "s", &key
);
1877 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Keys in DNS-SD TXT RRs can't be empty");
1879 if (!ascii_is_valid(key
))
1880 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "TXT key '%s' contains non-ASCII symbols", key
);
1882 r
= sd_bus_message_read_array(message
, 'y', &value
, &size
);
1886 r
= dnssd_txt_item_new_from_data(key
, value
, size
, &i
);
1890 LIST_INSERT_AFTER(items
, txt_data
->txt
, last
, i
);
1893 r
= sd_bus_message_exit_container(message
);
1901 r
= sd_bus_message_exit_container(message
);
1905 if (txt_data
->txt
) {
1906 LIST_PREPEND(items
, service
->txt_data_items
, txt_data
);
1913 r
= sd_bus_message_exit_container(message
);
1917 if (!service
->txt_data_items
) {
1918 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
1920 txt_data
= new0(DnssdTxtData
, 1);
1924 r
= dns_txt_item_new_empty(&txt_data
->txt
);
1928 LIST_PREPEND(items
, service
->txt_data_items
, txt_data
);
1932 r
= sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service
->name
, &path
);
1936 r
= bus_verify_polkit_async(message
, CAP_SYS_ADMIN
,
1937 "org.freedesktop.resolve1.register-service",
1938 NULL
, false, UID_INVALID
,
1939 &m
->polkit_registry
, error
);
1943 return 1; /* Polkit will call us back */
1945 r
= hashmap_ensure_allocated(&m
->dnssd_services
, &string_hash_ops
);
1949 r
= hashmap_put(m
->dnssd_services
, service
->name
, service
);
1953 r
= sd_bus_track_new(sd_bus_message_get_bus(message
), &bus_track
, dnssd_service_on_bus_track
, service
);
1957 r
= sd_bus_track_add_sender(bus_track
, message
);
1961 service
->manager
= m
;
1965 manager_refresh_rrs(m
);
1967 return sd_bus_reply_method_return(message
, "o", path
);
1970 static int call_dnssd_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
1971 _cleanup_free_
char *name
= NULL
;
1972 DnssdService
*s
= NULL
;
1980 r
= sd_bus_message_read(message
, "o", &path
);
1984 r
= sd_bus_path_decode(path
, "/org/freedesktop/resolve1/dnssd", &name
);
1986 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_DNSSD_SERVICE
, "DNS-SD service with object path '%s' does not exist", path
);
1990 s
= hashmap_get(m
->dnssd_services
, name
);
1992 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_DNSSD_SERVICE
, "DNS-SD service '%s' not known", name
);
1994 return handler(message
, s
, error
);
1997 static int bus_method_unregister_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1998 Manager
*m
= userdata
;
2003 return call_dnssd_method(m
, message
, bus_dnssd_method_unregister
, error
);
2006 static const sd_bus_vtable resolve_vtable
[] = {
2007 SD_BUS_VTABLE_START(0),
2008 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL
, offsetof(Manager
, llmnr_hostname
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2009 SD_BUS_PROPERTY("LLMNR", "s", bus_property_get_resolve_support
, offsetof(Manager
, llmnr_support
), 0),
2010 SD_BUS_PROPERTY("MulticastDNS", "s", bus_property_get_resolve_support
, offsetof(Manager
, mdns_support
), 0),
2011 SD_BUS_PROPERTY("DNSOverTLS", "s", bus_property_get_dns_over_tls_mode
, 0, 0),
2012 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2013 SD_BUS_PROPERTY("DNSEx", "a(iiayqs)", bus_property_get_dns_servers_ex
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2014 SD_BUS_PROPERTY("FallbackDNS", "a(iiay)", bus_property_get_fallback_dns_servers
, offsetof(Manager
, fallback_dns_servers
), SD_BUS_VTABLE_PROPERTY_CONST
),
2015 SD_BUS_PROPERTY("FallbackDNSEx", "a(iiayqs)", bus_property_get_fallback_dns_servers_ex
, offsetof(Manager
, fallback_dns_servers
), SD_BUS_VTABLE_PROPERTY_CONST
),
2016 SD_BUS_PROPERTY("CurrentDNSServer", "(iiay)", bus_property_get_current_dns_server
, offsetof(Manager
, current_dns_server
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2017 SD_BUS_PROPERTY("CurrentDNSServerEx", "(iiayqs)", bus_property_get_current_dns_server_ex
, offsetof(Manager
, current_dns_server
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2018 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains
, 0, 0),
2019 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics
, 0, 0),
2020 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics
, 0, 0),
2021 SD_BUS_PROPERTY("DNSSEC", "s", bus_property_get_dnssec_mode
, 0, 0),
2022 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics
, 0, 0),
2023 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported
, 0, 0),
2024 SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_ntas
, 0, 0),
2025 SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode
, offsetof(Manager
, dns_stub_listener_mode
), 0),
2026 SD_BUS_PROPERTY("ResolvConfMode", "s", bus_property_get_resolv_conf_mode
, 0, 0),
2028 SD_BUS_METHOD_WITH_ARGS("ResolveHostname",
2029 SD_BUS_ARGS("i", ifindex
, "s", name
, "i", family
, "t", flags
),
2030 SD_BUS_RESULT("a(iiay)", addresses
, "s", canonical
, "t", flags
),
2031 bus_method_resolve_hostname
,
2032 SD_BUS_VTABLE_UNPRIVILEGED
),
2033 SD_BUS_METHOD_WITH_ARGS("ResolveAddress",
2034 SD_BUS_ARGS("i", ifindex
, "i", family
, "ay", address
, "t", flags
),
2035 SD_BUS_RESULT("a(is)", names
, "t", flags
),
2036 bus_method_resolve_address
,
2037 SD_BUS_VTABLE_UNPRIVILEGED
),
2038 SD_BUS_METHOD_WITH_ARGS("ResolveRecord",
2039 SD_BUS_ARGS("i", ifindex
, "s", name
, "q", class, "q", type
, "t", flags
),
2040 SD_BUS_RESULT("a(iqqay)", records
, "t", flags
),
2041 bus_method_resolve_record
,
2042 SD_BUS_VTABLE_UNPRIVILEGED
),
2043 SD_BUS_METHOD_WITH_ARGS("ResolveService",
2044 SD_BUS_ARGS("i", ifindex
,
2050 SD_BUS_RESULT("a(qqqsa(iiay)s)", srv_data
,
2052 "s", canonical_name
,
2053 "s", canonical_type
,
2054 "s", canonical_domain
,
2056 bus_method_resolve_service
,
2057 SD_BUS_VTABLE_UNPRIVILEGED
),
2058 SD_BUS_METHOD_WITH_ARGS("GetLink",
2059 SD_BUS_ARGS("i", ifindex
),
2060 SD_BUS_RESULT("o", path
),
2061 bus_method_get_link
,
2062 SD_BUS_VTABLE_UNPRIVILEGED
),
2063 SD_BUS_METHOD_WITH_ARGS("SetLinkDNS",
2064 SD_BUS_ARGS("i", ifindex
, "a(iay)", addresses
),
2066 bus_method_set_link_dns_servers
,
2067 SD_BUS_VTABLE_UNPRIVILEGED
),
2068 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSEx",
2069 SD_BUS_ARGS("i", ifindex
, "a(iayqs)", addresses
),
2071 bus_method_set_link_dns_servers_ex
,
2072 SD_BUS_VTABLE_UNPRIVILEGED
),
2073 SD_BUS_METHOD_WITH_ARGS("SetLinkDomains",
2074 SD_BUS_ARGS("i", ifindex
, "a(sb)", domains
),
2076 bus_method_set_link_domains
,
2077 SD_BUS_VTABLE_UNPRIVILEGED
),
2078 SD_BUS_METHOD_WITH_ARGS("SetLinkDefaultRoute",
2079 SD_BUS_ARGS("i", ifindex
, "b", enable
),
2081 bus_method_set_link_default_route
,
2082 SD_BUS_VTABLE_UNPRIVILEGED
),
2083 SD_BUS_METHOD_WITH_ARGS("SetLinkLLMNR",
2084 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2086 bus_method_set_link_llmnr
,
2087 SD_BUS_VTABLE_UNPRIVILEGED
),
2088 SD_BUS_METHOD_WITH_ARGS("SetLinkMulticastDNS",
2089 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2091 bus_method_set_link_mdns
,
2092 SD_BUS_VTABLE_UNPRIVILEGED
),
2093 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSOverTLS",
2094 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2096 bus_method_set_link_dns_over_tls
,
2097 SD_BUS_VTABLE_UNPRIVILEGED
),
2098 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSEC",
2099 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2101 bus_method_set_link_dnssec
,
2102 SD_BUS_VTABLE_UNPRIVILEGED
),
2103 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSECNegativeTrustAnchors",
2104 SD_BUS_ARGS("i", ifindex
, "as", names
),
2106 bus_method_set_link_dnssec_negative_trust_anchors
,
2107 SD_BUS_VTABLE_UNPRIVILEGED
),
2108 SD_BUS_METHOD_WITH_ARGS("RevertLink",
2109 SD_BUS_ARGS("i", ifindex
),
2111 bus_method_revert_link
,
2112 SD_BUS_VTABLE_UNPRIVILEGED
),
2113 SD_BUS_METHOD_WITH_ARGS("RegisterService",
2114 SD_BUS_ARGS("s", name
,
2118 "q", service_priority
,
2119 "q", service_weight
,
2120 "aa{say}", txt_datas
),
2121 SD_BUS_RESULT("o", service_path
),
2122 bus_method_register_service
,
2123 SD_BUS_VTABLE_UNPRIVILEGED
),
2124 SD_BUS_METHOD_WITH_ARGS("UnregisterService",
2125 SD_BUS_ARGS("o", service_path
),
2127 bus_method_unregister_service
,
2128 SD_BUS_VTABLE_UNPRIVILEGED
),
2129 SD_BUS_METHOD_WITH_ARGS("ResetStatistics",
2132 bus_method_reset_statistics
,
2133 SD_BUS_VTABLE_UNPRIVILEGED
),
2134 SD_BUS_METHOD_WITH_ARGS("FlushCaches",
2137 bus_method_flush_caches
,
2138 SD_BUS_VTABLE_UNPRIVILEGED
),
2139 SD_BUS_METHOD_WITH_ARGS("ResetServerFeatures",
2142 bus_method_reset_server_features
,
2143 SD_BUS_VTABLE_UNPRIVILEGED
),
2148 const BusObjectImplementation manager_object
= {
2149 "/org/freedesktop/resolve1",
2150 "org.freedesktop.resolve1.Manager",
2151 .vtables
= BUS_VTABLES(resolve_vtable
),
2152 .children
= BUS_IMPLEMENTATIONS(&link_object
,
2156 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
2157 Manager
*m
= userdata
;
2163 r
= sd_bus_message_read(message
, "b", &b
);
2165 bus_log_parse_error(r
);
2172 log_debug("Coming back from suspend, verifying all RRs...");
2174 manager_verify_all(m
);
2178 int manager_connect_bus(Manager
*m
) {
2186 r
= bus_open_system_watch_bind_with_description(&m
->bus
, "bus-api-resolve");
2188 return log_error_errno(r
, "Failed to connect to system bus: %m");
2190 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
2194 r
= bus_log_control_api_register(m
->bus
);
2198 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.resolve1", 0, NULL
, NULL
);
2200 return log_error_errno(r
, "Failed to request name: %m");
2202 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
2204 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
2206 r
= sd_bus_match_signal_async(
2209 "org.freedesktop.login1",
2210 "/org/freedesktop/login1",
2211 "org.freedesktop.login1.Manager",
2213 match_prepare_for_sleep
,
2217 log_warning_errno(r
, "Failed to request match for PrepareForSleep, ignoring: %m");
2222 int _manager_send_changed(Manager
*manager
, const char *property
, ...) {
2225 char **l
= strv_from_stdarg_alloca(property
);
2227 int r
= sd_bus_emit_properties_changed_strv(
2229 "/org/freedesktop/resolve1",
2230 "org.freedesktop.resolve1.Manager",
2233 log_notice_errno(r
, "Failed to emit notification about changed property %s: %m", property
);