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 "format-util.h"
11 #include "memory-util.h"
12 #include "missing_capability.h"
13 #include "resolved-bus.h"
14 #include "resolved-def.h"
15 #include "resolved-dns-synthesize.h"
16 #include "resolved-dnssd-bus.h"
17 #include "resolved-dnssd.h"
18 #include "resolved-link-bus.h"
19 #include "resolved-resolv-conf.h"
20 #include "socket-netlink.h"
21 #include "stdio-util.h"
23 #include "syslog-util.h"
24 #include "user-util.h"
27 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_resolve_support
, resolve_support
, ResolveSupport
);
29 static int query_on_bus_track(sd_bus_track
*t
, void *userdata
) {
30 DnsQuery
*q
= ASSERT_PTR(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 sd_bus_message
*dns_query_steal_request(DnsQuery
*q
) {
64 /* Find the main query, it's the one that owns the message */
65 while (q
->auxiliary_for
)
68 /* Let's take the request message out of the DnsQuery object, so that we never send requests twice */
69 return TAKE_PTR(q
->bus_request
);
72 _sd_printf_(3, 4) static int reply_method_errorf(
74 const char *error_name
,
78 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
85 req
= dns_query_steal_request(query
);
86 if (!req
) /* No bus message set anymore? then we already replied already, let's not answer a second time */
90 r
= sd_bus_reply_method_errorfv(req
, error_name
, format
, ap
);
96 _sd_printf_(3, 4) static int reply_method_errnof(
102 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
107 req
= dns_query_steal_request(query
);
108 if (!req
) /* No bus message set anymore? then we already replied already, let's not answer a second time */
114 va_start(ap
, format
);
115 r
= sd_bus_reply_method_errnofv(req
, err
, format
, ap
);
118 r
= sd_bus_reply_method_errno(req
, err
, NULL
);
123 static int reply_query_state(DnsQuery
*q
) {
128 case DNS_TRANSACTION_NO_SERVERS
:
129 return reply_method_errorf(q
, BUS_ERROR_NO_NAME_SERVERS
, "No appropriate name servers or networks for name found");
131 case DNS_TRANSACTION_TIMEOUT
:
132 return reply_method_errorf(q
, SD_BUS_ERROR_TIMEOUT
, "Query timed out");
134 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
:
135 return reply_method_errorf(q
, SD_BUS_ERROR_TIMEOUT
, "All attempts to contact name servers or networks failed");
137 case DNS_TRANSACTION_INVALID_REPLY
:
138 return reply_method_errorf(q
, BUS_ERROR_INVALID_REPLY
, "Received invalid reply");
140 case DNS_TRANSACTION_ERRNO
:
141 return reply_method_errnof(q
, q
->answer_errno
, "Lookup failed due to system error: %m");
143 case DNS_TRANSACTION_ABORTED
:
144 return reply_method_errorf(q
, BUS_ERROR_ABORTED
, "Query aborted");
146 case DNS_TRANSACTION_DNSSEC_FAILED
:
147 return reply_method_errorf(q
, BUS_ERROR_DNSSEC_FAILED
, "DNSSEC validation failed: %s",
148 dnssec_result_to_string(q
->answer_dnssec_result
));
150 case DNS_TRANSACTION_NO_TRUST_ANCHOR
:
151 return reply_method_errorf(q
, BUS_ERROR_NO_TRUST_ANCHOR
, "No suitable trust anchor known");
153 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
:
154 return reply_method_errorf(q
, BUS_ERROR_RR_TYPE_UNSUPPORTED
, "Server does not support requested resource record type");
156 case DNS_TRANSACTION_NETWORK_DOWN
:
157 return reply_method_errorf(q
, BUS_ERROR_NETWORK_DOWN
, "Network is down");
159 case DNS_TRANSACTION_NOT_FOUND
:
160 /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
161 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
162 return reply_method_errorf(q
, BUS_ERROR_DNS_NXDOMAIN
, "'%s' not found", dns_query_string(q
));
164 case DNS_TRANSACTION_NO_SOURCE
:
165 return reply_method_errorf(q
, BUS_ERROR_NO_SOURCE
, "All suitable resolution sources turned off");
167 case DNS_TRANSACTION_STUB_LOOP
:
168 return reply_method_errorf(q
, BUS_ERROR_STUB_LOOP
, "Configured DNS server loops back to us");
170 case DNS_TRANSACTION_RCODE_FAILURE
: {
171 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
172 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
174 req
= dns_query_steal_request(q
);
175 if (!req
) /* No bus message set anymore? then we already replied already, let's not answer a second time */
178 if (q
->answer_rcode
== DNS_RCODE_NXDOMAIN
)
179 sd_bus_error_setf(&error
, BUS_ERROR_DNS_NXDOMAIN
, "Name '%s' not found", dns_query_string(q
));
183 rc
= FORMAT_DNS_RCODE(q
->answer_rcode
);
184 n
= strjoina(_BUS_ERROR_DNS
, rc
);
185 sd_bus_error_setf(&error
, n
, "Could not resolve '%s', server or network returned error %s", dns_query_string(q
), rc
);
188 return sd_bus_reply_method_error(req
, &error
);
191 case DNS_TRANSACTION_NULL
:
192 case DNS_TRANSACTION_PENDING
:
193 case DNS_TRANSACTION_VALIDATING
:
194 case DNS_TRANSACTION_SUCCESS
:
196 assert_not_reached();
200 static int append_address(sd_bus_message
*reply
, DnsResourceRecord
*rr
, int ifindex
) {
206 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
210 r
= sd_bus_message_append(reply
, "i", ifindex
);
214 if (rr
->key
->type
== DNS_TYPE_A
) {
215 r
= sd_bus_message_append(reply
, "i", AF_INET
);
219 r
= sd_bus_message_append_array(reply
, 'y', &rr
->a
.in_addr
, sizeof(struct in_addr
));
221 } else if (rr
->key
->type
== DNS_TYPE_AAAA
) {
222 r
= sd_bus_message_append(reply
, "i", AF_INET6
);
226 r
= sd_bus_message_append_array(reply
, 'y', &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
));
228 return -EAFNOSUPPORT
;
233 r
= sd_bus_message_close_container(reply
);
240 static void bus_method_resolve_hostname_complete(DnsQuery
*query
) {
241 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
242 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
243 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
244 _cleanup_free_
char *normalized
= NULL
;
245 DnsQuestion
*question
;
246 DnsResourceRecord
*rr
;
252 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
253 r
= reply_query_state(q
);
257 r
= dns_query_process_cname_many(q
);
259 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
264 if (r
== DNS_QUERY_CNAME
) {
265 /* This was a cname, and the query was restarted. */
270 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
274 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
278 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
280 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
282 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
288 r
= append_address(reply
, rr
, ifindex
);
293 canonical
= dns_resource_record_ref(rr
);
299 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
303 r
= sd_bus_message_close_container(reply
);
307 /* The key names are not necessarily normalized, make sure that they are when we return them to our
310 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), 0, &normalized
);
314 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
315 r
= sd_bus_message_append(
318 dns_query_reply_flags_make(q
));
322 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
323 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
327 log_error_errno(r
, "Failed to send hostname reply: %m");
328 (void) reply_method_errnof(q
, r
, NULL
);
332 static int validate_and_mangle_flags(
336 sd_bus_error
*error
) {
340 /* Checks that the client supplied interface index and flags parameter actually are valid and make
341 * sense in our method call context. Specifically:
343 * 1. Checks that the interface index is either 0 (meaning *all* interfaces) or positive
345 * 2. Only the protocols flags and a bunch of NO_XYZ flags are set, at most. Plus additional flags
346 * specific to our method, passed in the "ok" parameter.
348 * 3. If zero protocol flags are specified it is automatically turned into *all* protocols. This way
349 * clients can simply pass 0 as flags and all will work as it should. They can also use this so
350 * that clients don't have to know all the protocols resolved implements, but can just specify 0
351 * to mean "all supported protocols".
354 if (*flags
& ~(SD_RESOLVED_PROTOCOLS_ALL
|
355 SD_RESOLVED_NO_CNAME
|
356 SD_RESOLVED_NO_VALIDATE
|
357 SD_RESOLVED_NO_SYNTHESIZE
|
358 SD_RESOLVED_NO_CACHE
|
360 SD_RESOLVED_NO_TRUST_ANCHOR
|
361 SD_RESOLVED_NO_NETWORK
|
363 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid flags parameter");
365 if ((*flags
& SD_RESOLVED_PROTOCOLS_ALL
) == 0) /* If no protocol is enabled, enable all */
366 *flags
|= SD_RESOLVED_PROTOCOLS_ALL
;
368 /* Imply SD_RESOLVED_NO_SEARCH if permitted and name is dot suffixed. */
369 if (name
&& FLAGS_SET(ok
, SD_RESOLVED_NO_SEARCH
) && dns_name_dot_suffixed(name
) > 0)
370 *flags
|= SD_RESOLVED_NO_SEARCH
;
375 static int parse_as_address(sd_bus_message
*m
, int ifindex
, const char *hostname
, int family
, uint64_t flags
) {
376 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
377 _cleanup_free_
char *canonical
= NULL
;
378 union in_addr_union parsed
;
379 int r
, ff
, parsed_ifindex
= 0;
381 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
382 * let's not attempt to look it up. */
384 r
= in_addr_ifindex_from_string_auto(hostname
, &ff
, &parsed
, &parsed_ifindex
);
385 if (r
< 0) /* not an address */
388 if (family
!= AF_UNSPEC
&& ff
!= family
)
389 return sd_bus_reply_method_errorf(m
, BUS_ERROR_NO_SUCH_RR
, "The specified address is not of the requested family.");
390 if (ifindex
> 0 && parsed_ifindex
> 0 && parsed_ifindex
!= ifindex
)
391 return sd_bus_reply_method_errorf(m
, BUS_ERROR_NO_SUCH_RR
, "The specified address interface index does not match requested interface.");
393 if (parsed_ifindex
> 0)
394 ifindex
= parsed_ifindex
;
396 r
= sd_bus_message_new_method_return(m
, &reply
);
400 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
404 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
408 r
= sd_bus_message_append(reply
, "ii", ifindex
, ff
);
412 r
= sd_bus_message_append_array(reply
, 'y', &parsed
, FAMILY_ADDRESS_SIZE(ff
));
416 r
= sd_bus_message_close_container(reply
);
420 r
= sd_bus_message_close_container(reply
);
424 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
425 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
426 * omissions are always done the same way). */
427 r
= in_addr_ifindex_to_string(ff
, &parsed
, ifindex
, &canonical
);
431 r
= sd_bus_message_append(reply
, "st", canonical
,
432 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags
), ff
, true, true) |
433 SD_RESOLVED_SYNTHETIC
);
437 return sd_bus_send(sd_bus_message_get_bus(m
), reply
, NULL
);
440 void bus_client_log(sd_bus_message
*m
, const char *what
) {
441 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
442 const char *comm
= NULL
;
443 uid_t uid
= UID_INVALID
;
453 r
= sd_bus_query_sender_creds(m
, SD_BUS_CREDS_PID
|SD_BUS_CREDS_UID
|SD_BUS_CREDS_COMM
|SD_BUS_CREDS_AUGMENT
, &creds
);
455 return (void) log_debug_errno(r
, "Failed to query client credentials, ignoring: %m");
457 (void) sd_bus_creds_get_uid(creds
, &uid
);
458 (void) sd_bus_creds_get_pid(creds
, &pid
);
459 (void) sd_bus_creds_get_comm(creds
, &comm
);
461 log_debug("D-Bus %s request from client PID " PID_FMT
" (%s) with UID " UID_FMT
,
462 what
, pid
, strna(comm
), uid
);
465 static int bus_method_resolve_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
466 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
467 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
468 Manager
*m
= ASSERT_PTR(userdata
);
469 const char *hostname
;
476 assert_cc(sizeof(int) == sizeof(int32_t));
478 r
= sd_bus_message_read(message
, "isit", &ifindex
, &hostname
, &family
, &flags
);
483 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
485 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
486 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
488 r
= validate_and_mangle_flags(hostname
, &flags
, SD_RESOLVED_NO_SEARCH
, error
);
492 r
= parse_as_address(message
, ifindex
, hostname
, family
, flags
);
496 r
= dns_name_is_valid(hostname
);
500 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid hostname '%s'", hostname
);
502 r
= dns_question_new_address(&question_utf8
, family
, hostname
, false);
506 r
= dns_question_new_address(&question_idna
, family
, hostname
, true);
507 if (r
< 0 && r
!= -EALREADY
)
510 bus_client_log(message
, "hostname resolution");
512 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
?: question_utf8
, NULL
, ifindex
, flags
);
516 q
->bus_request
= sd_bus_message_ref(message
);
517 q
->request_family
= family
;
518 q
->complete
= bus_method_resolve_hostname_complete
;
520 r
= dns_query_bus_track(q
, message
);
532 static void bus_method_resolve_address_complete(DnsQuery
*query
) {
533 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
534 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
535 DnsQuestion
*question
;
536 DnsResourceRecord
*rr
;
542 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
543 r
= reply_query_state(q
);
547 r
= dns_query_process_cname_many(q
);
549 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
554 if (r
== DNS_QUERY_CNAME
) {
555 /* This was a cname, and the query was restarted. */
560 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
564 r
= sd_bus_message_open_container(reply
, 'a', "(is)");
568 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
570 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
571 _cleanup_free_
char *normalized
= NULL
;
573 r
= dns_question_matches_rr(question
, rr
, NULL
);
579 r
= dns_name_normalize(rr
->ptr
.name
, 0, &normalized
);
583 r
= sd_bus_message_append(reply
, "(is)", ifindex
, normalized
);
591 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
,
592 "Address %s does not have any RR of requested type",
593 IN_ADDR_TO_STRING(q
->request_family
, &q
->request_address
));
597 r
= sd_bus_message_close_container(reply
);
601 r
= sd_bus_message_append(reply
, "t", dns_query_reply_flags_make(q
));
605 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
606 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
610 log_error_errno(r
, "Failed to send address reply: %m");
611 (void) reply_method_errnof(q
, r
, NULL
);
615 static int bus_method_resolve_address(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
616 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
617 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
618 Manager
*m
= ASSERT_PTR(userdata
);
619 union in_addr_union a
;
626 assert_cc(sizeof(int) == sizeof(int32_t));
628 r
= sd_bus_message_read(message
, "i", &ifindex
);
632 r
= bus_message_read_in_addr_auto(message
, error
, &family
, &a
);
636 r
= sd_bus_message_read(message
, "t", &flags
);
641 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
643 r
= validate_and_mangle_flags(NULL
, &flags
, 0, error
);
647 r
= dns_question_new_reverse(&question
, family
, &a
);
651 bus_client_log(message
, "address resolution");
653 r
= dns_query_new(m
, &q
, question
, question
, NULL
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
657 q
->bus_request
= sd_bus_message_ref(message
);
658 q
->request_family
= family
;
659 q
->request_address
= a
;
660 q
->complete
= bus_method_resolve_address_complete
;
662 r
= dns_query_bus_track(q
, message
);
674 static int bus_message_append_rr(sd_bus_message
*m
, DnsResourceRecord
*rr
, int ifindex
) {
680 r
= sd_bus_message_open_container(m
, 'r', "iqqay");
684 r
= sd_bus_message_append(m
, "iqq",
691 r
= dns_resource_record_to_wire_format(rr
, false);
695 r
= sd_bus_message_append_array(m
, 'y', rr
->wire_format
, rr
->wire_format_size
);
699 return sd_bus_message_close_container(m
);
702 static void bus_method_resolve_record_complete(DnsQuery
*query
) {
703 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
704 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
705 DnsResourceRecord
*rr
;
706 DnsQuestion
*question
;
713 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
714 r
= reply_query_state(q
);
718 r
= dns_query_process_cname_many(q
);
720 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
725 if (r
== DNS_QUERY_CNAME
) {
726 /* This was a cname, and the query was restarted. */
731 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
735 r
= sd_bus_message_open_container(reply
, 'a', "(iqqay)");
739 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
741 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
742 r
= dns_question_matches_rr(question
, rr
, NULL
);
748 r
= bus_message_append_rr(reply
, rr
, ifindex
);
756 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
, "Name '%s' does not have any RR of the requested type", dns_query_string(q
));
760 r
= sd_bus_message_close_container(reply
);
764 r
= sd_bus_message_append(reply
, "t", dns_query_reply_flags_make(q
));
768 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
769 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
773 log_error_errno(r
, "Failed to send record reply: %m");
774 (void) reply_method_errnof(q
, r
, NULL
);
778 static int bus_method_resolve_record(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
779 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
780 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
781 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
782 Manager
*m
= ASSERT_PTR(userdata
);
783 uint16_t class, type
;
790 assert_cc(sizeof(int) == sizeof(int32_t));
792 r
= sd_bus_message_read(message
, "isqqt", &ifindex
, &name
, &class, &type
, &flags
);
797 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
799 r
= dns_name_is_valid(name
);
803 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid name '%s'", name
);
805 if (!dns_type_is_valid_query(type
))
806 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified resource record type %" PRIu16
" may not be used in a query.", type
);
807 if (dns_type_is_zone_transer(type
))
808 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Zone transfers not permitted via this programming interface.");
809 if (dns_type_is_obsolete(type
))
810 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Specified DNS resource record type %" PRIu16
" is obsolete.", type
);
812 r
= validate_and_mangle_flags(name
, &flags
, 0, error
);
816 question
= dns_question_new(1);
820 key
= dns_resource_key_new(class, type
, name
);
824 r
= dns_question_add(question
, key
, 0);
828 bus_client_log(message
, "resource record resolution");
830 /* Setting SD_RESOLVED_CLAMP_TTL: let's request that the TTL is fixed up for locally cached entries,
831 * after all we return it in the wire format blob. */
832 r
= dns_query_new(m
, &q
, question
, question
, NULL
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
|SD_RESOLVED_CLAMP_TTL
);
836 q
->bus_request
= sd_bus_message_ref(message
);
837 q
->complete
= bus_method_resolve_record_complete
;
839 r
= dns_query_bus_track(q
, message
);
851 static int append_srv(DnsQuery
*q
, sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
852 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
853 _cleanup_free_
char *normalized
= NULL
;
861 if (rr
->key
->type
!= DNS_TYPE_SRV
)
864 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
865 /* First, let's see if we could find an appropriate A or AAAA
866 * record for the SRV record */
867 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
868 DnsResourceRecord
*zz
;
869 DnsQuestion
*question
;
871 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
873 if (aux
->auxiliary_result
!= 0)
876 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
878 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
884 DNS_ANSWER_FOREACH(zz
, aux
->answer
) {
886 r
= dns_question_matches_rr(question
, zz
, NULL
);
892 canonical
= dns_resource_record_ref(zz
);
900 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
905 r
= sd_bus_message_open_container(reply
, 'r', "qqqsa(iiay)s");
909 r
= dns_name_normalize(rr
->srv
.name
, 0, &normalized
);
913 r
= sd_bus_message_append(
916 rr
->srv
.priority
, rr
->srv
.weight
, rr
->srv
.port
, normalized
);
920 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
924 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
925 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
926 DnsResourceRecord
*zz
;
927 DnsQuestion
*question
;
930 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
932 if (aux
->auxiliary_result
!= 0)
935 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
937 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
943 DNS_ANSWER_FOREACH_IFINDEX(zz
, ifindex
, aux
->answer
) {
945 r
= dns_question_matches_rr(question
, zz
, NULL
);
951 r
= append_address(reply
, zz
, ifindex
);
958 r
= sd_bus_message_close_container(reply
);
963 normalized
= mfree(normalized
);
965 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), 0, &normalized
);
970 /* Note that above we appended the hostname as encoded in the
971 * SRV, and here the canonical hostname this maps to. */
972 r
= sd_bus_message_append(reply
, "s", normalized
);
976 r
= sd_bus_message_close_container(reply
);
983 static int append_txt(sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
990 if (rr
->key
->type
!= DNS_TYPE_TXT
)
993 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
998 r
= sd_bus_message_append_array(reply
, 'y', i
->data
, i
->length
);
1006 static void resolve_service_all_complete(DnsQuery
*query
) {
1007 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
1008 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1009 _cleanup_free_
char *name
= NULL
, *type
= NULL
, *domain
= NULL
;
1010 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
1011 DnsQuestion
*question
;
1012 DnsResourceRecord
*rr
;
1018 if (q
->block_all_complete
> 0) {
1023 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
1024 DnsQuery
*bad
= NULL
;
1025 bool have_success
= false;
1027 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
1029 switch (aux
->state
) {
1031 case DNS_TRANSACTION_PENDING
:
1032 /* If an auxiliary query is still pending, let's wait */
1036 case DNS_TRANSACTION_SUCCESS
:
1037 if (aux
->auxiliary_result
== 0)
1038 have_success
= true;
1049 if (!have_success
) {
1050 /* We can only return one error, hence pick the last error we encountered */
1054 if (bad
->state
== DNS_TRANSACTION_SUCCESS
) {
1055 assert(bad
->auxiliary_result
!= 0);
1057 if (bad
->auxiliary_result
== -ELOOP
) {
1058 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad
));
1062 assert(bad
->auxiliary_result
< 0);
1063 r
= bad
->auxiliary_result
;
1067 r
= reply_query_state(bad
);
1072 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
1076 r
= sd_bus_message_open_container(reply
, 'a', "(qqqsa(iiay)s)");
1080 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1082 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1083 r
= dns_question_matches_rr(question
, rr
, NULL
);
1089 r
= append_srv(q
, reply
, rr
);
1092 if (r
== 0) /* not an SRV record */
1096 canonical
= dns_resource_record_ref(rr
);
1102 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
1106 r
= sd_bus_message_close_container(reply
);
1110 r
= sd_bus_message_open_container(reply
, 'a', "ay");
1114 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1115 r
= dns_question_matches_rr(question
, rr
, NULL
);
1121 r
= append_txt(reply
, rr
);
1126 r
= sd_bus_message_close_container(reply
);
1131 r
= dns_service_split(dns_resource_key_name(canonical
->key
), &name
, &type
, &domain
);
1135 r
= sd_bus_message_append(
1139 dns_query_reply_flags_make(q
));
1143 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
1144 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
1148 log_error_errno(r
, "Failed to send service reply: %m");
1149 (void) reply_method_errnof(q
, r
, NULL
);
1153 static void resolve_service_hostname_complete(DnsQuery
*q
) {
1157 assert(q
->auxiliary_for
);
1159 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1160 resolve_service_all_complete(q
->auxiliary_for
);
1164 r
= dns_query_process_cname_many(q
);
1165 if (r
== DNS_QUERY_CNAME
) /* This was a cname, and the query was restarted. */
1168 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1169 q
->auxiliary_result
= r
< 0 ? r
: 0;
1170 resolve_service_all_complete(q
->auxiliary_for
);
1173 static int resolve_service_hostname(DnsQuery
*q
, DnsResourceRecord
*rr
, int ifindex
) {
1174 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
1175 _cleanup_(dns_query_freep
) DnsQuery
*aux
= NULL
;
1181 assert(rr
->key
->type
== DNS_TYPE_SRV
);
1183 /* OK, we found an SRV record for the service. Let's resolve
1184 * the hostname included in it */
1186 r
= dns_question_new_address(&question
, q
->request_family
, rr
->srv
.name
, false);
1190 r
= dns_query_new(q
->manager
, &aux
, question
, question
, NULL
, ifindex
, q
->flags
|SD_RESOLVED_NO_SEARCH
);
1194 aux
->request_family
= q
->request_family
;
1195 aux
->complete
= resolve_service_hostname_complete
;
1197 r
= dns_query_make_auxiliary(aux
, q
);
1199 /* Too many auxiliary lookups? If so, don't complain,
1200 * let's just not add this one, we already have more
1206 /* Note that auxiliary queries do not track the original bus
1207 * client, only the primary request does that. */
1209 r
= dns_query_go(aux
);
1217 static void bus_method_resolve_service_complete(DnsQuery
*query
) {
1218 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
1219 bool has_root_domain
= false;
1220 DnsResourceRecord
*rr
;
1221 DnsQuestion
*question
;
1227 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1228 r
= reply_query_state(q
);
1232 r
= dns_query_process_cname_many(q
);
1234 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
1239 if (r
== DNS_QUERY_CNAME
) {
1240 /* This was a cname, and the query was restarted. */
1245 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1247 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
1248 r
= dns_question_matches_rr(question
, rr
, NULL
);
1254 if (rr
->key
->type
!= DNS_TYPE_SRV
)
1257 if (dns_name_is_root(rr
->srv
.name
)) {
1258 has_root_domain
= true;
1262 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
1263 q
->block_all_complete
++;
1264 r
= resolve_service_hostname(q
, rr
, ifindex
);
1265 q
->block_all_complete
--;
1274 if (has_root_domain
&& found
<= 0) {
1275 /* If there's exactly one SRV RR and it uses the root domain as hostname, then the service is
1276 * explicitly not offered on the domain. Report this as a recognizable error. See RFC 2782,
1277 * Section "Usage Rules". */
1278 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_SERVICE
, "'%s' does not provide the requested service", dns_query_string(q
));
1283 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
1287 /* Maybe we are already finished? check now... */
1288 resolve_service_all_complete(TAKE_PTR(q
));
1293 log_error_errno(r
, "Failed to send service reply: %m");
1294 (void) reply_method_errnof(q
, r
, NULL
);
1298 static int bus_method_resolve_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1299 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
1300 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
1301 const char *name
, *type
, *domain
;
1302 Manager
*m
= ASSERT_PTR(userdata
);
1303 int family
, ifindex
;
1309 assert_cc(sizeof(int) == sizeof(int32_t));
1311 r
= sd_bus_message_read(message
, "isssit", &ifindex
, &name
, &type
, &domain
, &family
, &flags
);
1316 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
1318 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
1319 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
1323 else if (!dns_service_name_is_valid(name
))
1324 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid service name '%s'", name
);
1328 else if (!dns_srv_type_is_valid(type
))
1329 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid SRV service type '%s'", type
);
1331 r
= dns_name_is_valid(domain
);
1335 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid domain '%s'", domain
);
1338 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Service name cannot be specified without service type.");
1340 r
= validate_and_mangle_flags(name
, &flags
, SD_RESOLVED_NO_TXT
|SD_RESOLVED_NO_ADDRESS
, error
);
1344 r
= dns_question_new_service(&question_utf8
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), false);
1348 r
= dns_question_new_service(&question_idna
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), true);
1352 bus_client_log(message
, "service resolution");
1354 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
, NULL
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
1358 q
->bus_request
= sd_bus_message_ref(message
);
1359 q
->request_family
= family
;
1360 q
->complete
= bus_method_resolve_service_complete
;
1362 r
= dns_query_bus_track(q
, message
);
1366 r
= dns_query_go(q
);
1374 int bus_dns_server_append(
1375 sd_bus_message
*reply
,
1377 bool with_ifindex
, /* include "ifindex" field */
1378 bool extended
) { /* also include port number and server name */
1386 return sd_bus_message_append(reply
, "(iiayqs)", 0, AF_UNSPEC
, 0, 0, NULL
);
1388 return sd_bus_message_append(reply
, "(iiay)", 0, AF_UNSPEC
, 0);
1391 return sd_bus_message_append(reply
, "(iayqs)", AF_UNSPEC
, 0, 0, NULL
);
1393 return sd_bus_message_append(reply
, "(iay)", AF_UNSPEC
, 0);
1397 r
= sd_bus_message_open_container(
1400 with_ifindex
? (extended
? "iiayqs" : "iiay") :
1401 (extended
? "iayqs" : "iay"));
1406 r
= sd_bus_message_append(reply
, "i", dns_server_ifindex(s
));
1411 r
= sd_bus_message_append(reply
, "i", s
->family
);
1415 r
= sd_bus_message_append_array(reply
, 'y', &s
->address
, FAMILY_ADDRESS_SIZE(s
->family
));
1420 r
= sd_bus_message_append(reply
, "q", s
->port
);
1424 r
= sd_bus_message_append(reply
, "s", s
->server_name
);
1429 return sd_bus_message_close_container(reply
);
1432 static int bus_property_get_dns_servers_internal(
1435 const char *interface
,
1436 const char *property
,
1437 sd_bus_message
*reply
,
1439 sd_bus_error
*error
,
1442 Manager
*m
= ASSERT_PTR(userdata
);
1448 r
= sd_bus_message_open_container(reply
, 'a', extended
? "(iiayqs)" : "(iiay)");
1452 LIST_FOREACH(servers
, s
, m
->dns_servers
) {
1453 r
= bus_dns_server_append(reply
, s
, true, extended
);
1458 HASHMAP_FOREACH(l
, m
->links
)
1459 LIST_FOREACH(servers
, s
, l
->dns_servers
) {
1460 r
= bus_dns_server_append(reply
, s
, true, extended
);
1465 return sd_bus_message_close_container(reply
);
1468 static int bus_property_get_dns_servers(
1471 const char *interface
,
1472 const char *property
,
1473 sd_bus_message
*reply
,
1475 sd_bus_error
*error
) {
1476 return bus_property_get_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1479 static int bus_property_get_dns_servers_ex(
1482 const char *interface
,
1483 const char *property
,
1484 sd_bus_message
*reply
,
1486 sd_bus_error
*error
) {
1487 return bus_property_get_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1490 static int bus_property_get_fallback_dns_servers_internal(
1493 const char *interface
,
1494 const char *property
,
1495 sd_bus_message
*reply
,
1497 sd_bus_error
*error
,
1500 DnsServer
**f
= ASSERT_PTR(userdata
);
1505 r
= sd_bus_message_open_container(reply
, 'a', extended
? "(iiayqs)" : "(iiay)");
1509 LIST_FOREACH(servers
, s
, *f
) {
1510 r
= bus_dns_server_append(reply
, s
, true, extended
);
1515 return sd_bus_message_close_container(reply
);
1518 static int bus_property_get_fallback_dns_servers(
1521 const char *interface
,
1522 const char *property
,
1523 sd_bus_message
*reply
,
1525 sd_bus_error
*error
) {
1526 return bus_property_get_fallback_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1529 static int bus_property_get_fallback_dns_servers_ex(
1532 const char *interface
,
1533 const char *property
,
1534 sd_bus_message
*reply
,
1536 sd_bus_error
*error
) {
1537 return bus_property_get_fallback_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1540 static int bus_property_get_current_dns_server_internal(
1543 const char *interface
,
1544 const char *property
,
1545 sd_bus_message
*reply
,
1547 sd_bus_error
*error
,
1555 s
= *(DnsServer
**) userdata
;
1557 return bus_dns_server_append(reply
, s
, true, extended
);
1560 static int bus_property_get_current_dns_server(
1563 const char *interface
,
1564 const char *property
,
1565 sd_bus_message
*reply
,
1567 sd_bus_error
*error
) {
1568 return bus_property_get_current_dns_server_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1571 static int bus_property_get_current_dns_server_ex(
1574 const char *interface
,
1575 const char *property
,
1576 sd_bus_message
*reply
,
1578 sd_bus_error
*error
) {
1579 return bus_property_get_current_dns_server_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1582 static int bus_property_get_domains(
1585 const char *interface
,
1586 const char *property
,
1587 sd_bus_message
*reply
,
1589 sd_bus_error
*error
) {
1591 Manager
*m
= ASSERT_PTR(userdata
);
1597 r
= sd_bus_message_open_container(reply
, 'a', "(isb)");
1601 LIST_FOREACH(domains
, d
, m
->search_domains
) {
1602 r
= sd_bus_message_append(reply
, "(isb)", 0, d
->name
, d
->route_only
);
1607 HASHMAP_FOREACH(l
, m
->links
) {
1608 LIST_FOREACH(domains
, d
, l
->search_domains
) {
1609 r
= sd_bus_message_append(reply
, "(isb)", l
->ifindex
, d
->name
, d
->route_only
);
1615 return sd_bus_message_close_container(reply
);
1618 static int bus_property_get_transaction_statistics(
1621 const char *interface
,
1622 const char *property
,
1623 sd_bus_message
*reply
,
1625 sd_bus_error
*error
) {
1627 Manager
*m
= ASSERT_PTR(userdata
);
1631 return sd_bus_message_append(reply
, "(tt)",
1632 (uint64_t) hashmap_size(m
->dns_transactions
),
1633 (uint64_t) m
->n_transactions_total
);
1636 static int bus_property_get_cache_statistics(
1639 const char *interface
,
1640 const char *property
,
1641 sd_bus_message
*reply
,
1643 sd_bus_error
*error
) {
1645 uint64_t size
= 0, hit
= 0, miss
= 0;
1646 Manager
*m
= ASSERT_PTR(userdata
);
1650 LIST_FOREACH(scopes
, s
, m
->dns_scopes
) {
1651 size
+= dns_cache_size(&s
->cache
);
1652 hit
+= s
->cache
.n_hit
;
1653 miss
+= s
->cache
.n_miss
;
1656 return sd_bus_message_append(reply
, "(ttt)", size
, hit
, miss
);
1659 static int bus_property_get_dnssec_statistics(
1662 const char *interface
,
1663 const char *property
,
1664 sd_bus_message
*reply
,
1666 sd_bus_error
*error
) {
1668 Manager
*m
= ASSERT_PTR(userdata
);
1672 return sd_bus_message_append(reply
, "(tttt)",
1673 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_SECURE
],
1674 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INSECURE
],
1675 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_BOGUS
],
1676 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INDETERMINATE
]);
1679 static int bus_property_get_ntas(
1682 const char *interface
,
1683 const char *property
,
1684 sd_bus_message
*reply
,
1686 sd_bus_error
*error
) {
1688 Manager
*m
= ASSERT_PTR(userdata
);
1694 r
= sd_bus_message_open_container(reply
, 'a', "s");
1698 SET_FOREACH(domain
, m
->trust_anchor
.negative_by_name
) {
1699 r
= sd_bus_message_append(reply
, "s", domain
);
1704 return sd_bus_message_close_container(reply
);
1707 static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode
, dns_stub_listener_mode
, DnsStubListenerMode
);
1708 static BUS_DEFINE_PROPERTY_GET(bus_property_get_dnssec_supported
, "b", Manager
, manager_dnssec_supported
);
1709 static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dnssec_mode
, "s", Manager
, manager_get_dnssec_mode
, dnssec_mode_to_string
);
1710 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
);
1712 static int bus_property_get_resolv_conf_mode(
1715 const char *interface
,
1716 const char *property
,
1717 sd_bus_message
*reply
,
1719 sd_bus_error
*error
) {
1725 r
= resolv_conf_mode();
1727 log_warning_errno(r
, "Failed to test /etc/resolv.conf mode, ignoring: %m");
1728 return sd_bus_message_append(reply
, "s", NULL
);
1731 return sd_bus_message_append(reply
, "s", resolv_conf_mode_to_string(r
));
1734 static int bus_method_reset_statistics(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1735 Manager
*m
= ASSERT_PTR(userdata
);
1739 bus_client_log(message
, "statistics reset");
1741 LIST_FOREACH(scopes
, s
, m
->dns_scopes
)
1742 s
->cache
.n_hit
= s
->cache
.n_miss
= 0;
1744 m
->n_transactions_total
= 0;
1745 zero(m
->n_dnssec_verdict
);
1747 return sd_bus_reply_method_return(message
, NULL
);
1750 static int get_any_link(Manager
*m
, int ifindex
, Link
**ret
, sd_bus_error
*error
) {
1756 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
1758 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_LINK
, "Link %i not known", ifindex
);
1764 static int call_link_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
1772 r
= bus_message_read_ifindex(message
, error
, &ifindex
);
1776 r
= get_any_link(m
, ifindex
, &l
, error
);
1780 return handler(message
, l
, error
);
1783 static int bus_method_set_link_dns_servers(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1784 return call_link_method(userdata
, message
, bus_link_method_set_dns_servers
, error
);
1787 static int bus_method_set_link_dns_servers_ex(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1788 return call_link_method(userdata
, message
, bus_link_method_set_dns_servers_ex
, error
);
1791 static int bus_method_set_link_domains(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1792 return call_link_method(userdata
, message
, bus_link_method_set_domains
, error
);
1795 static int bus_method_set_link_default_route(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1796 return call_link_method(userdata
, message
, bus_link_method_set_default_route
, error
);
1799 static int bus_method_set_link_llmnr(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1800 return call_link_method(userdata
, message
, bus_link_method_set_llmnr
, error
);
1803 static int bus_method_set_link_mdns(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1804 return call_link_method(userdata
, message
, bus_link_method_set_mdns
, error
);
1807 static int bus_method_set_link_dns_over_tls(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1808 return call_link_method(userdata
, message
, bus_link_method_set_dns_over_tls
, error
);
1811 static int bus_method_set_link_dnssec(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1812 return call_link_method(userdata
, message
, bus_link_method_set_dnssec
, error
);
1815 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1816 return call_link_method(userdata
, message
, bus_link_method_set_dnssec_negative_trust_anchors
, error
);
1819 static int bus_method_revert_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1820 return call_link_method(userdata
, message
, bus_link_method_revert
, error
);
1823 static int bus_method_get_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1824 _cleanup_free_
char *p
= NULL
;
1825 Manager
*m
= ASSERT_PTR(userdata
);
1831 r
= bus_message_read_ifindex(message
, error
, &ifindex
);
1835 r
= get_any_link(m
, ifindex
, &l
, error
);
1839 p
= link_bus_path(l
);
1843 return sd_bus_reply_method_return(message
, "o", p
);
1846 static int bus_method_flush_caches(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1847 Manager
*m
= ASSERT_PTR(userdata
);
1851 bus_client_log(message
, "cache flush");
1853 manager_flush_caches(m
, LOG_INFO
);
1855 return sd_bus_reply_method_return(message
, NULL
);
1858 static int bus_method_reset_server_features(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1859 Manager
*m
= ASSERT_PTR(userdata
);
1863 bus_client_log(message
, "server feature reset");
1865 manager_reset_server_features(m
);
1867 return sd_bus_reply_method_return(message
, NULL
);
1870 static int dnssd_service_on_bus_track(sd_bus_track
*t
, void *userdata
) {
1871 DnssdService
*s
= ASSERT_PTR(userdata
);
1875 log_debug("Client of active request vanished, destroying DNS-SD service.");
1876 dnssd_service_free(s
);
1881 static int bus_method_register_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1882 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1883 _cleanup_(dnssd_service_freep
) DnssdService
*service
= NULL
;
1884 _cleanup_(sd_bus_track_unrefp
) sd_bus_track
*bus_track
= NULL
;
1885 const char *name
, *name_template
, *type
;
1886 _cleanup_free_
char *path
= NULL
;
1887 DnssdService
*s
= NULL
;
1888 Manager
*m
= ASSERT_PTR(userdata
);
1894 if (m
->mdns_support
!= RESOLVE_SUPPORT_YES
)
1895 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Support for MulticastDNS is disabled");
1897 service
= new0(DnssdService
, 1);
1901 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1905 r
= sd_bus_creds_get_euid(creds
, &euid
);
1908 service
->originator
= euid
;
1910 r
= sd_bus_message_read(message
, "sssqqq", &name
, &name_template
, &type
,
1911 &service
->port
, &service
->priority
,
1916 s
= hashmap_get(m
->dnssd_services
, name
);
1918 return sd_bus_error_setf(error
, BUS_ERROR_DNSSD_SERVICE_EXISTS
, "DNS-SD service '%s' exists already", name
);
1920 if (!dnssd_srv_type_is_valid(type
))
1921 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DNS-SD service type '%s' is invalid", type
);
1923 service
->name
= strdup(name
);
1927 service
->name_template
= strdup(name_template
);
1928 if (!service
->name_template
)
1931 service
->type
= strdup(type
);
1935 r
= dnssd_render_instance_name(m
, service
, NULL
);
1939 r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_ARRAY
, "a{say}");
1943 while ((r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_ARRAY
, "{say}")) > 0) {
1944 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
1945 DnsTxtItem
*last
= NULL
;
1947 txt_data
= new0(DnssdTxtData
, 1);
1951 while ((r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_DICT_ENTRY
, "say")) > 0) {
1957 r
= sd_bus_message_read(message
, "s", &key
);
1962 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Keys in DNS-SD TXT RRs can't be empty");
1964 if (!ascii_is_valid(key
))
1965 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "TXT key '%s' contains non-ASCII symbols", key
);
1967 r
= sd_bus_message_read_array(message
, 'y', &value
, &size
);
1971 r
= dnssd_txt_item_new_from_data(key
, value
, size
, &i
);
1975 LIST_INSERT_AFTER(items
, txt_data
->txts
, last
, i
);
1978 r
= sd_bus_message_exit_container(message
);
1986 r
= sd_bus_message_exit_container(message
);
1990 if (txt_data
->txts
) {
1991 LIST_PREPEND(items
, service
->txt_data_items
, txt_data
);
1998 r
= sd_bus_message_exit_container(message
);
2002 if (!service
->txt_data_items
) {
2003 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
2005 txt_data
= new0(DnssdTxtData
, 1);
2009 r
= dns_txt_item_new_empty(&txt_data
->txts
);
2013 LIST_PREPEND(items
, service
->txt_data_items
, txt_data
);
2017 r
= sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service
->name
, &path
);
2021 r
= bus_verify_polkit_async(message
, CAP_SYS_ADMIN
,
2022 "org.freedesktop.resolve1.register-service",
2023 NULL
, false, UID_INVALID
,
2024 &m
->polkit_registry
, error
);
2028 return 1; /* Polkit will call us back */
2030 r
= hashmap_ensure_put(&m
->dnssd_services
, &string_hash_ops
, service
->name
, service
);
2034 r
= sd_bus_track_new(sd_bus_message_get_bus(message
), &bus_track
, dnssd_service_on_bus_track
, service
);
2038 r
= sd_bus_track_add_sender(bus_track
, message
);
2042 service
->manager
= m
;
2046 manager_refresh_rrs(m
);
2048 return sd_bus_reply_method_return(message
, "o", path
);
2051 static int call_dnssd_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
2052 _cleanup_free_
char *name
= NULL
;
2053 DnssdService
*s
= NULL
;
2061 r
= sd_bus_message_read(message
, "o", &path
);
2065 r
= sd_bus_path_decode(path
, "/org/freedesktop/resolve1/dnssd", &name
);
2067 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_DNSSD_SERVICE
, "DNS-SD service with object path '%s' does not exist", path
);
2071 s
= hashmap_get(m
->dnssd_services
, name
);
2073 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_DNSSD_SERVICE
, "DNS-SD service '%s' not known", name
);
2075 return handler(message
, s
, error
);
2078 static int bus_method_unregister_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2079 Manager
*m
= ASSERT_PTR(userdata
);
2083 return call_dnssd_method(m
, message
, bus_dnssd_method_unregister
, error
);
2086 static const sd_bus_vtable resolve_vtable
[] = {
2087 SD_BUS_VTABLE_START(0),
2088 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL
, offsetof(Manager
, llmnr_hostname
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2089 SD_BUS_PROPERTY("LLMNR", "s", bus_property_get_resolve_support
, offsetof(Manager
, llmnr_support
), 0),
2090 SD_BUS_PROPERTY("MulticastDNS", "s", bus_property_get_resolve_support
, offsetof(Manager
, mdns_support
), 0),
2091 SD_BUS_PROPERTY("DNSOverTLS", "s", bus_property_get_dns_over_tls_mode
, 0, 0),
2092 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2093 SD_BUS_PROPERTY("DNSEx", "a(iiayqs)", bus_property_get_dns_servers_ex
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2094 SD_BUS_PROPERTY("FallbackDNS", "a(iiay)", bus_property_get_fallback_dns_servers
, offsetof(Manager
, fallback_dns_servers
), SD_BUS_VTABLE_PROPERTY_CONST
),
2095 SD_BUS_PROPERTY("FallbackDNSEx", "a(iiayqs)", bus_property_get_fallback_dns_servers_ex
, offsetof(Manager
, fallback_dns_servers
), SD_BUS_VTABLE_PROPERTY_CONST
),
2096 SD_BUS_PROPERTY("CurrentDNSServer", "(iiay)", bus_property_get_current_dns_server
, offsetof(Manager
, current_dns_server
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2097 SD_BUS_PROPERTY("CurrentDNSServerEx", "(iiayqs)", bus_property_get_current_dns_server_ex
, offsetof(Manager
, current_dns_server
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2098 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains
, 0, 0),
2099 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics
, 0, 0),
2100 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics
, 0, 0),
2101 SD_BUS_PROPERTY("DNSSEC", "s", bus_property_get_dnssec_mode
, 0, 0),
2102 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics
, 0, 0),
2103 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported
, 0, 0),
2104 SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_ntas
, 0, 0),
2105 SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode
, offsetof(Manager
, dns_stub_listener_mode
), 0),
2106 SD_BUS_PROPERTY("ResolvConfMode", "s", bus_property_get_resolv_conf_mode
, 0, 0),
2108 SD_BUS_METHOD_WITH_ARGS("ResolveHostname",
2109 SD_BUS_ARGS("i", ifindex
, "s", name
, "i", family
, "t", flags
),
2110 SD_BUS_RESULT("a(iiay)", addresses
, "s", canonical
, "t", flags
),
2111 bus_method_resolve_hostname
,
2112 SD_BUS_VTABLE_UNPRIVILEGED
),
2113 SD_BUS_METHOD_WITH_ARGS("ResolveAddress",
2114 SD_BUS_ARGS("i", ifindex
, "i", family
, "ay", address
, "t", flags
),
2115 SD_BUS_RESULT("a(is)", names
, "t", flags
),
2116 bus_method_resolve_address
,
2117 SD_BUS_VTABLE_UNPRIVILEGED
),
2118 SD_BUS_METHOD_WITH_ARGS("ResolveRecord",
2119 SD_BUS_ARGS("i", ifindex
, "s", name
, "q", class, "q", type
, "t", flags
),
2120 SD_BUS_RESULT("a(iqqay)", records
, "t", flags
),
2121 bus_method_resolve_record
,
2122 SD_BUS_VTABLE_UNPRIVILEGED
),
2123 SD_BUS_METHOD_WITH_ARGS("ResolveService",
2124 SD_BUS_ARGS("i", ifindex
,
2130 SD_BUS_RESULT("a(qqqsa(iiay)s)", srv_data
,
2132 "s", canonical_name
,
2133 "s", canonical_type
,
2134 "s", canonical_domain
,
2136 bus_method_resolve_service
,
2137 SD_BUS_VTABLE_UNPRIVILEGED
),
2138 SD_BUS_METHOD_WITH_ARGS("GetLink",
2139 SD_BUS_ARGS("i", ifindex
),
2140 SD_BUS_RESULT("o", path
),
2141 bus_method_get_link
,
2142 SD_BUS_VTABLE_UNPRIVILEGED
),
2143 SD_BUS_METHOD_WITH_ARGS("SetLinkDNS",
2144 SD_BUS_ARGS("i", ifindex
, "a(iay)", addresses
),
2146 bus_method_set_link_dns_servers
,
2147 SD_BUS_VTABLE_UNPRIVILEGED
),
2148 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSEx",
2149 SD_BUS_ARGS("i", ifindex
, "a(iayqs)", addresses
),
2151 bus_method_set_link_dns_servers_ex
,
2152 SD_BUS_VTABLE_UNPRIVILEGED
),
2153 SD_BUS_METHOD_WITH_ARGS("SetLinkDomains",
2154 SD_BUS_ARGS("i", ifindex
, "a(sb)", domains
),
2156 bus_method_set_link_domains
,
2157 SD_BUS_VTABLE_UNPRIVILEGED
),
2158 SD_BUS_METHOD_WITH_ARGS("SetLinkDefaultRoute",
2159 SD_BUS_ARGS("i", ifindex
, "b", enable
),
2161 bus_method_set_link_default_route
,
2162 SD_BUS_VTABLE_UNPRIVILEGED
),
2163 SD_BUS_METHOD_WITH_ARGS("SetLinkLLMNR",
2164 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2166 bus_method_set_link_llmnr
,
2167 SD_BUS_VTABLE_UNPRIVILEGED
),
2168 SD_BUS_METHOD_WITH_ARGS("SetLinkMulticastDNS",
2169 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2171 bus_method_set_link_mdns
,
2172 SD_BUS_VTABLE_UNPRIVILEGED
),
2173 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSOverTLS",
2174 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2176 bus_method_set_link_dns_over_tls
,
2177 SD_BUS_VTABLE_UNPRIVILEGED
),
2178 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSEC",
2179 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2181 bus_method_set_link_dnssec
,
2182 SD_BUS_VTABLE_UNPRIVILEGED
),
2183 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSECNegativeTrustAnchors",
2184 SD_BUS_ARGS("i", ifindex
, "as", names
),
2186 bus_method_set_link_dnssec_negative_trust_anchors
,
2187 SD_BUS_VTABLE_UNPRIVILEGED
),
2188 SD_BUS_METHOD_WITH_ARGS("RevertLink",
2189 SD_BUS_ARGS("i", ifindex
),
2191 bus_method_revert_link
,
2192 SD_BUS_VTABLE_UNPRIVILEGED
),
2193 SD_BUS_METHOD_WITH_ARGS("RegisterService",
2194 SD_BUS_ARGS("s", name
,
2198 "q", service_priority
,
2199 "q", service_weight
,
2200 "aa{say}", txt_datas
),
2201 SD_BUS_RESULT("o", service_path
),
2202 bus_method_register_service
,
2203 SD_BUS_VTABLE_UNPRIVILEGED
),
2204 SD_BUS_METHOD_WITH_ARGS("UnregisterService",
2205 SD_BUS_ARGS("o", service_path
),
2207 bus_method_unregister_service
,
2208 SD_BUS_VTABLE_UNPRIVILEGED
),
2209 SD_BUS_METHOD_WITH_ARGS("ResetStatistics",
2212 bus_method_reset_statistics
,
2213 SD_BUS_VTABLE_UNPRIVILEGED
),
2214 SD_BUS_METHOD_WITH_ARGS("FlushCaches",
2217 bus_method_flush_caches
,
2218 SD_BUS_VTABLE_UNPRIVILEGED
),
2219 SD_BUS_METHOD_WITH_ARGS("ResetServerFeatures",
2222 bus_method_reset_server_features
,
2223 SD_BUS_VTABLE_UNPRIVILEGED
),
2228 const BusObjectImplementation manager_object
= {
2229 "/org/freedesktop/resolve1",
2230 "org.freedesktop.resolve1.Manager",
2231 .vtables
= BUS_VTABLES(resolve_vtable
),
2232 .children
= BUS_IMPLEMENTATIONS(&link_object
,
2236 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
2237 Manager
*m
= ASSERT_PTR(userdata
);
2242 r
= sd_bus_message_read(message
, "b", &b
);
2244 bus_log_parse_error(r
);
2251 log_debug("Coming back from suspend, verifying all RRs...");
2253 manager_verify_all(m
);
2257 int manager_connect_bus(Manager
*m
) {
2265 r
= bus_open_system_watch_bind_with_description(&m
->bus
, "bus-api-resolve");
2267 return log_error_errno(r
, "Failed to connect to system bus: %m");
2269 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
2273 r
= bus_log_control_api_register(m
->bus
);
2277 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.resolve1", 0, NULL
, NULL
);
2279 return log_error_errno(r
, "Failed to request name: %m");
2281 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
2283 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
2285 r
= sd_bus_match_signal_async(
2288 "org.freedesktop.login1",
2289 "/org/freedesktop/login1",
2290 "org.freedesktop.login1.Manager",
2292 match_prepare_for_sleep
,
2296 log_warning_errno(r
, "Failed to request match for PrepareForSleep, ignoring: %m");
2301 int _manager_send_changed(Manager
*manager
, const char *property
, ...) {
2304 if (sd_bus_is_ready(manager
->bus
) <= 0)
2307 char **l
= strv_from_stdarg_alloca(property
);
2309 int r
= sd_bus_emit_properties_changed_strv(
2311 "/org/freedesktop/resolve1",
2312 "org.freedesktop.resolve1.Manager",
2315 log_notice_errno(r
, "Failed to emit notification about changed property %s: %m", property
);