1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
4 #include "bus-common-errors.h"
5 #include "bus-get-properties.h"
6 #include "bus-locator.h"
7 #include "bus-log-control-api.h"
8 #include "bus-message-util.h"
9 #include "bus-polkit.h"
10 #include "dns-domain.h"
11 #include "format-util.h"
12 #include "memory-util.h"
13 #include "missing_capability.h"
14 #include "resolved-bus.h"
15 #include "resolved-def.h"
16 #include "resolved-dns-synthesize.h"
17 #include "resolved-dnssd-bus.h"
18 #include "resolved-dnssd.h"
19 #include "resolved-link-bus.h"
20 #include "resolved-resolv-conf.h"
21 #include "socket-netlink.h"
22 #include "stdio-util.h"
24 #include "syslog-util.h"
25 #include "user-util.h"
28 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_resolve_support
, resolve_support
, ResolveSupport
);
30 static int query_on_bus_track(sd_bus_track
*t
, void *userdata
) {
31 DnsQuery
*q
= ASSERT_PTR(userdata
);
35 if (!DNS_TRANSACTION_IS_LIVE(q
->state
))
38 log_debug("Client of active query vanished, aborting query.");
39 dns_query_complete(q
, DNS_TRANSACTION_ABORTED
);
43 static int dns_query_bus_track(DnsQuery
*q
, sd_bus_message
*m
) {
50 r
= sd_bus_track_new(sd_bus_message_get_bus(m
), &q
->bus_track
, query_on_bus_track
, q
);
55 r
= sd_bus_track_add_sender(q
->bus_track
, m
);
62 static sd_bus_message
*dns_query_steal_request(DnsQuery
*q
) {
65 /* Find the main query, it's the one that owns the message */
66 while (q
->auxiliary_for
)
69 /* Let's take the request message out of the DnsQuery object, so that we never send requests twice */
70 return TAKE_PTR(q
->bus_request
);
73 _sd_printf_(3, 4) static int reply_method_errorf(
75 const char *error_name
,
79 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
86 req
= dns_query_steal_request(query
);
87 if (!req
) /* No bus message set anymore? then we already replied already, let's not answer a second time */
91 r
= sd_bus_reply_method_errorfv(req
, error_name
, format
, ap
);
97 _sd_printf_(3, 4) static int reply_method_errnof(
103 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
108 req
= dns_query_steal_request(query
);
109 if (!req
) /* No bus message set anymore? then we already replied already, let's not answer a second time */
115 va_start(ap
, format
);
116 r
= sd_bus_reply_method_errnofv(req
, err
, format
, ap
);
119 r
= sd_bus_reply_method_errno(req
, err
, NULL
);
124 static int reply_query_state(DnsQuery
*q
) {
129 case DNS_TRANSACTION_NO_SERVERS
:
130 return reply_method_errorf(q
, BUS_ERROR_NO_NAME_SERVERS
, "No appropriate name servers or networks for name found");
132 case DNS_TRANSACTION_TIMEOUT
:
133 return reply_method_errorf(q
, SD_BUS_ERROR_TIMEOUT
, "Query timed out");
135 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
:
136 return reply_method_errorf(q
, SD_BUS_ERROR_TIMEOUT
, "All attempts to contact name servers or networks failed");
138 case DNS_TRANSACTION_INVALID_REPLY
:
139 return reply_method_errorf(q
, BUS_ERROR_INVALID_REPLY
, "Received invalid reply");
141 case DNS_TRANSACTION_ERRNO
:
142 return reply_method_errnof(q
, q
->answer_errno
, "Lookup failed due to system error: %m");
144 case DNS_TRANSACTION_ABORTED
:
145 return reply_method_errorf(q
, BUS_ERROR_ABORTED
, "Query aborted");
147 case DNS_TRANSACTION_DNSSEC_FAILED
:
148 return reply_method_errorf(q
, BUS_ERROR_DNSSEC_FAILED
, "DNSSEC validation failed: %s",
149 dnssec_result_to_string(q
->answer_dnssec_result
));
151 case DNS_TRANSACTION_NO_TRUST_ANCHOR
:
152 return reply_method_errorf(q
, BUS_ERROR_NO_TRUST_ANCHOR
, "No suitable trust anchor known");
154 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
:
155 return reply_method_errorf(q
, BUS_ERROR_RR_TYPE_UNSUPPORTED
, "Server does not support requested resource record type");
157 case DNS_TRANSACTION_NETWORK_DOWN
:
158 return reply_method_errorf(q
, BUS_ERROR_NETWORK_DOWN
, "Network is down");
160 case DNS_TRANSACTION_NOT_FOUND
:
161 /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
162 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
163 return reply_method_errorf(q
, BUS_ERROR_DNS_NXDOMAIN
, "'%s' not found", dns_query_string(q
));
165 case DNS_TRANSACTION_NO_SOURCE
:
166 return reply_method_errorf(q
, BUS_ERROR_NO_SOURCE
, "All suitable resolution sources turned off");
168 case DNS_TRANSACTION_STUB_LOOP
:
169 return reply_method_errorf(q
, BUS_ERROR_STUB_LOOP
, "Configured DNS server loops back to us");
171 case DNS_TRANSACTION_RCODE_FAILURE
: {
172 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
173 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
175 req
= dns_query_steal_request(q
);
176 if (!req
) /* No bus message set anymore? then we already replied already, let's not answer a second time */
179 if (q
->answer_rcode
== DNS_RCODE_NXDOMAIN
)
180 sd_bus_error_setf(&error
, BUS_ERROR_DNS_NXDOMAIN
, "Name '%s' not found", dns_query_string(q
));
184 rc
= FORMAT_DNS_RCODE(q
->answer_rcode
);
185 n
= strjoina(_BUS_ERROR_DNS
, rc
);
186 sd_bus_error_setf(&error
, n
, "Could not resolve '%s', server or network returned error %s", dns_query_string(q
), rc
);
189 return sd_bus_reply_method_error(req
, &error
);
192 case DNS_TRANSACTION_NULL
:
193 case DNS_TRANSACTION_PENDING
:
194 case DNS_TRANSACTION_VALIDATING
:
195 case DNS_TRANSACTION_SUCCESS
:
197 assert_not_reached();
201 static int append_address(sd_bus_message
*reply
, DnsResourceRecord
*rr
, int ifindex
) {
207 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
211 r
= sd_bus_message_append(reply
, "i", ifindex
);
215 if (rr
->key
->type
== DNS_TYPE_A
) {
216 r
= sd_bus_message_append(reply
, "i", AF_INET
);
220 r
= sd_bus_message_append_array(reply
, 'y', &rr
->a
.in_addr
, sizeof(struct in_addr
));
222 } else if (rr
->key
->type
== DNS_TYPE_AAAA
) {
223 r
= sd_bus_message_append(reply
, "i", AF_INET6
);
227 r
= sd_bus_message_append_array(reply
, 'y', &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
));
229 return -EAFNOSUPPORT
;
234 r
= sd_bus_message_close_container(reply
);
241 static void bus_method_resolve_hostname_complete(DnsQuery
*query
) {
242 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
243 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
244 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
245 _cleanup_free_
char *normalized
= NULL
;
246 DnsQuestion
*question
;
247 DnsResourceRecord
*rr
;
253 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
254 r
= reply_query_state(q
);
258 r
= dns_query_process_cname_many(q
);
260 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
265 if (r
== DNS_QUERY_CNAME
) {
266 /* This was a cname, and the query was restarted. */
271 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
275 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
279 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
281 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
283 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
289 r
= append_address(reply
, rr
, ifindex
);
294 canonical
= dns_resource_record_ref(rr
);
300 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
304 r
= sd_bus_message_close_container(reply
);
308 /* The key names are not necessarily normalized, make sure that they are when we return them to our
311 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), 0, &normalized
);
315 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
316 r
= sd_bus_message_append(
319 dns_query_reply_flags_make(q
));
323 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
324 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
328 log_error_errno(r
, "Failed to send hostname reply: %m");
329 (void) reply_method_errnof(q
, r
, NULL
);
333 static int validate_and_mangle_flags(
337 sd_bus_error
*error
) {
341 /* Checks that the client supplied interface index and flags parameter actually are valid and make
342 * sense in our method call context. Specifically:
344 * 1. Checks that the interface index is either 0 (meaning *all* interfaces) or positive
346 * 2. Only the protocols flags and a bunch of NO_XYZ flags are set, at most. Plus additional flags
347 * specific to our method, passed in the "ok" parameter.
349 * 3. If zero protocol flags are specified it is automatically turned into *all* protocols. This way
350 * clients can simply pass 0 as flags and all will work as it should. They can also use this so
351 * that clients don't have to know all the protocols resolved implements, but can just specify 0
352 * to mean "all supported protocols".
355 if (*flags
& ~(SD_RESOLVED_PROTOCOLS_ALL
|
356 SD_RESOLVED_NO_CNAME
|
357 SD_RESOLVED_NO_VALIDATE
|
358 SD_RESOLVED_NO_SYNTHESIZE
|
359 SD_RESOLVED_NO_CACHE
|
361 SD_RESOLVED_NO_TRUST_ANCHOR
|
362 SD_RESOLVED_NO_NETWORK
|
363 SD_RESOLVED_NO_STALE
|
365 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid flags parameter");
367 if ((*flags
& SD_RESOLVED_PROTOCOLS_ALL
) == 0) /* If no protocol is enabled, enable all */
368 *flags
|= SD_RESOLVED_PROTOCOLS_ALL
;
370 /* Imply SD_RESOLVED_NO_SEARCH if permitted and name is dot suffixed. */
371 if (name
&& FLAGS_SET(ok
, SD_RESOLVED_NO_SEARCH
) && dns_name_dot_suffixed(name
) > 0)
372 *flags
|= SD_RESOLVED_NO_SEARCH
;
377 static int parse_as_address(sd_bus_message
*m
, int ifindex
, const char *hostname
, int family
, uint64_t flags
) {
378 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
379 _cleanup_free_
char *canonical
= NULL
;
380 union in_addr_union parsed
;
381 int r
, ff
, parsed_ifindex
= 0;
383 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
384 * let's not attempt to look it up. */
386 r
= in_addr_ifindex_from_string_auto(hostname
, &ff
, &parsed
, &parsed_ifindex
);
387 if (r
< 0) /* not an address */
390 if (family
!= AF_UNSPEC
&& ff
!= family
)
391 return sd_bus_reply_method_errorf(m
, BUS_ERROR_NO_SUCH_RR
, "The specified address is not of the requested family.");
392 if (ifindex
> 0 && parsed_ifindex
> 0 && parsed_ifindex
!= ifindex
)
393 return sd_bus_reply_method_errorf(m
, BUS_ERROR_NO_SUCH_RR
, "The specified address interface index does not match requested interface.");
395 if (parsed_ifindex
> 0)
396 ifindex
= parsed_ifindex
;
398 r
= sd_bus_message_new_method_return(m
, &reply
);
402 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
406 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
410 r
= sd_bus_message_append(reply
, "ii", ifindex
, ff
);
414 r
= sd_bus_message_append_array(reply
, 'y', &parsed
, FAMILY_ADDRESS_SIZE(ff
));
418 r
= sd_bus_message_close_container(reply
);
422 r
= sd_bus_message_close_container(reply
);
426 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
427 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
428 * omissions are always done the same way). */
429 r
= in_addr_ifindex_to_string(ff
, &parsed
, ifindex
, &canonical
);
433 r
= sd_bus_message_append(reply
, "st", canonical
,
434 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags
), ff
, true, true) |
435 SD_RESOLVED_SYNTHETIC
);
439 return sd_bus_send(sd_bus_message_get_bus(m
), reply
, NULL
);
442 void bus_client_log(sd_bus_message
*m
, const char *what
) {
443 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
444 const char *comm
= NULL
;
445 uid_t uid
= UID_INVALID
;
455 r
= sd_bus_query_sender_creds(m
, SD_BUS_CREDS_PID
|SD_BUS_CREDS_UID
|SD_BUS_CREDS_COMM
|SD_BUS_CREDS_AUGMENT
, &creds
);
457 return (void) log_debug_errno(r
, "Failed to query client credentials, ignoring: %m");
459 (void) sd_bus_creds_get_uid(creds
, &uid
);
460 (void) sd_bus_creds_get_pid(creds
, &pid
);
461 (void) sd_bus_creds_get_comm(creds
, &comm
);
463 log_debug("D-Bus %s request from client PID " PID_FMT
" (%s) with UID " UID_FMT
,
464 what
, pid
, strna(comm
), uid
);
467 static int bus_method_resolve_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
468 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
469 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
470 Manager
*m
= ASSERT_PTR(userdata
);
471 const char *hostname
;
478 assert_cc(sizeof(int) == sizeof(int32_t));
480 r
= sd_bus_message_read(message
, "isit", &ifindex
, &hostname
, &family
, &flags
);
485 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
487 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
488 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
490 r
= validate_and_mangle_flags(hostname
, &flags
, SD_RESOLVED_NO_SEARCH
, error
);
494 r
= parse_as_address(message
, ifindex
, hostname
, family
, flags
);
498 r
= dns_name_is_valid(hostname
);
502 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid hostname '%s'", hostname
);
504 r
= dns_question_new_address(&question_utf8
, family
, hostname
, false);
508 r
= dns_question_new_address(&question_idna
, family
, hostname
, true);
509 if (r
< 0 && r
!= -EALREADY
)
512 bus_client_log(message
, "hostname resolution");
514 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
?: question_utf8
, NULL
, ifindex
, flags
);
518 q
->bus_request
= sd_bus_message_ref(message
);
519 q
->request_family
= family
;
520 q
->complete
= bus_method_resolve_hostname_complete
;
522 r
= dns_query_bus_track(q
, message
);
534 static void bus_method_resolve_address_complete(DnsQuery
*query
) {
535 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
536 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
537 DnsQuestion
*question
;
538 DnsResourceRecord
*rr
;
544 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
545 r
= reply_query_state(q
);
549 r
= dns_query_process_cname_many(q
);
551 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
556 if (r
== DNS_QUERY_CNAME
) {
557 /* This was a cname, and the query was restarted. */
562 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
566 r
= sd_bus_message_open_container(reply
, 'a', "(is)");
570 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
572 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
573 _cleanup_free_
char *normalized
= NULL
;
575 r
= dns_question_matches_rr(question
, rr
, NULL
);
581 r
= dns_name_normalize(rr
->ptr
.name
, 0, &normalized
);
585 r
= sd_bus_message_append(reply
, "(is)", ifindex
, normalized
);
593 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
,
594 "Address %s does not have any RR of requested type",
595 IN_ADDR_TO_STRING(q
->request_family
, &q
->request_address
));
599 r
= sd_bus_message_close_container(reply
);
603 r
= sd_bus_message_append(reply
, "t", dns_query_reply_flags_make(q
));
607 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
608 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
612 log_error_errno(r
, "Failed to send address reply: %m");
613 (void) reply_method_errnof(q
, r
, NULL
);
617 static int bus_method_resolve_address(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
618 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
619 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
620 Manager
*m
= ASSERT_PTR(userdata
);
621 union in_addr_union a
;
628 assert_cc(sizeof(int) == sizeof(int32_t));
630 r
= sd_bus_message_read(message
, "i", &ifindex
);
634 r
= bus_message_read_in_addr_auto(message
, error
, &family
, &a
);
638 r
= sd_bus_message_read(message
, "t", &flags
);
643 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
645 r
= validate_and_mangle_flags(NULL
, &flags
, 0, error
);
649 r
= dns_question_new_reverse(&question
, family
, &a
);
653 bus_client_log(message
, "address resolution");
655 r
= dns_query_new(m
, &q
, question
, question
, NULL
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
659 q
->bus_request
= sd_bus_message_ref(message
);
660 q
->request_family
= family
;
661 q
->request_address
= a
;
662 q
->complete
= bus_method_resolve_address_complete
;
664 r
= dns_query_bus_track(q
, message
);
676 static int bus_message_append_rr(sd_bus_message
*m
, DnsResourceRecord
*rr
, int ifindex
) {
682 r
= sd_bus_message_open_container(m
, 'r', "iqqay");
686 r
= sd_bus_message_append(m
, "iqq",
693 r
= dns_resource_record_to_wire_format(rr
, false);
697 r
= sd_bus_message_append_array(m
, 'y', rr
->wire_format
, rr
->wire_format_size
);
701 return sd_bus_message_close_container(m
);
704 static void bus_method_resolve_record_complete(DnsQuery
*query
) {
705 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
706 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
707 DnsResourceRecord
*rr
;
708 DnsQuestion
*question
;
715 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
716 r
= reply_query_state(q
);
720 r
= dns_query_process_cname_many(q
);
722 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
727 if (r
== DNS_QUERY_CNAME
) {
728 /* This was a cname, and the query was restarted. */
733 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
737 r
= sd_bus_message_open_container(reply
, 'a', "(iqqay)");
741 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
743 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
744 r
= dns_question_matches_rr(question
, rr
, NULL
);
750 r
= bus_message_append_rr(reply
, rr
, ifindex
);
758 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
));
762 r
= sd_bus_message_close_container(reply
);
766 r
= sd_bus_message_append(reply
, "t", dns_query_reply_flags_make(q
));
770 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
771 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
775 log_error_errno(r
, "Failed to send record reply: %m");
776 (void) reply_method_errnof(q
, r
, NULL
);
780 static int bus_method_resolve_record(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
781 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
782 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
783 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
784 Manager
*m
= ASSERT_PTR(userdata
);
785 uint16_t class, type
;
792 assert_cc(sizeof(int) == sizeof(int32_t));
794 r
= sd_bus_message_read(message
, "isqqt", &ifindex
, &name
, &class, &type
, &flags
);
799 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
801 r
= dns_name_is_valid(name
);
805 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid name '%s'", name
);
807 if (!dns_type_is_valid_query(type
))
808 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified resource record type %" PRIu16
" may not be used in a query.", type
);
809 if (dns_type_is_zone_transfer(type
))
810 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Zone transfers not permitted via this programming interface.");
811 if (dns_type_is_obsolete(type
))
812 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Specified DNS resource record type %" PRIu16
" is obsolete.", type
);
814 r
= validate_and_mangle_flags(name
, &flags
, 0, error
);
818 question
= dns_question_new(1);
822 key
= dns_resource_key_new(class, type
, name
);
826 r
= dns_question_add(question
, key
, 0);
830 bus_client_log(message
, "resource record resolution");
832 /* Setting SD_RESOLVED_CLAMP_TTL: let's request that the TTL is fixed up for locally cached entries,
833 * after all we return it in the wire format blob. */
834 r
= dns_query_new(m
, &q
, question
, question
, NULL
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
|SD_RESOLVED_CLAMP_TTL
);
838 q
->bus_request
= sd_bus_message_ref(message
);
839 q
->complete
= bus_method_resolve_record_complete
;
841 r
= dns_query_bus_track(q
, message
);
853 static int append_srv(DnsQuery
*q
, sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
854 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
855 _cleanup_free_
char *normalized
= NULL
;
863 if (rr
->key
->type
!= DNS_TYPE_SRV
)
866 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
867 /* First, let's see if we could find an appropriate A or AAAA
868 * record for the SRV record */
869 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
870 DnsResourceRecord
*zz
;
871 DnsQuestion
*question
;
873 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
875 if (aux
->auxiliary_result
!= 0)
878 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
880 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
886 DNS_ANSWER_FOREACH(zz
, aux
->answer
) {
888 r
= dns_question_matches_rr(question
, zz
, NULL
);
894 canonical
= dns_resource_record_ref(zz
);
902 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
907 r
= sd_bus_message_open_container(reply
, 'r', "qqqsa(iiay)s");
911 r
= dns_name_normalize(rr
->srv
.name
, 0, &normalized
);
915 r
= sd_bus_message_append(
918 rr
->srv
.priority
, rr
->srv
.weight
, rr
->srv
.port
, normalized
);
922 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
926 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
927 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
928 DnsResourceRecord
*zz
;
929 DnsQuestion
*question
;
932 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
934 if (aux
->auxiliary_result
!= 0)
937 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
939 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
945 DNS_ANSWER_FOREACH_IFINDEX(zz
, ifindex
, aux
->answer
) {
947 r
= dns_question_matches_rr(question
, zz
, NULL
);
953 r
= append_address(reply
, zz
, ifindex
);
960 r
= sd_bus_message_close_container(reply
);
965 normalized
= mfree(normalized
);
967 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), 0, &normalized
);
972 /* Note that above we appended the hostname as encoded in the
973 * SRV, and here the canonical hostname this maps to. */
974 r
= sd_bus_message_append(reply
, "s", normalized
);
978 r
= sd_bus_message_close_container(reply
);
985 static int append_txt(sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
992 if (rr
->key
->type
!= DNS_TYPE_TXT
)
995 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
1000 r
= sd_bus_message_append_array(reply
, 'y', i
->data
, i
->length
);
1008 static void resolve_service_all_complete(DnsQuery
*query
) {
1009 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
1010 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1011 _cleanup_free_
char *name
= NULL
, *type
= NULL
, *domain
= NULL
;
1012 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
1013 DnsQuestion
*question
;
1014 DnsResourceRecord
*rr
;
1020 if (q
->block_all_complete
> 0) {
1025 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
1026 DnsQuery
*bad
= NULL
;
1027 bool have_success
= false;
1029 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
1031 switch (aux
->state
) {
1033 case DNS_TRANSACTION_PENDING
:
1034 /* If an auxiliary query is still pending, let's wait */
1038 case DNS_TRANSACTION_SUCCESS
:
1039 if (aux
->auxiliary_result
== 0)
1040 have_success
= true;
1051 if (!have_success
) {
1052 /* We can only return one error, hence pick the last error we encountered */
1056 if (bad
->state
== DNS_TRANSACTION_SUCCESS
) {
1057 assert(bad
->auxiliary_result
!= 0);
1059 if (bad
->auxiliary_result
== -ELOOP
) {
1060 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad
));
1064 assert(bad
->auxiliary_result
< 0);
1065 r
= bad
->auxiliary_result
;
1069 r
= reply_query_state(bad
);
1074 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
1078 r
= sd_bus_message_open_container(reply
, 'a', "(qqqsa(iiay)s)");
1082 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1084 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1085 r
= dns_question_matches_rr(question
, rr
, NULL
);
1091 r
= append_srv(q
, reply
, rr
);
1094 if (r
== 0) /* not an SRV record */
1098 canonical
= dns_resource_record_ref(rr
);
1104 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
1108 r
= sd_bus_message_close_container(reply
);
1112 r
= sd_bus_message_open_container(reply
, 'a', "ay");
1116 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1117 r
= dns_question_matches_rr(question
, rr
, NULL
);
1123 r
= append_txt(reply
, rr
);
1128 r
= sd_bus_message_close_container(reply
);
1133 r
= dns_service_split(dns_resource_key_name(canonical
->key
), &name
, &type
, &domain
);
1137 r
= sd_bus_message_append(
1141 dns_query_reply_flags_make(q
));
1145 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
1146 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
1150 log_error_errno(r
, "Failed to send service reply: %m");
1151 (void) reply_method_errnof(q
, r
, NULL
);
1155 static void resolve_service_hostname_complete(DnsQuery
*q
) {
1159 assert(q
->auxiliary_for
);
1161 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1162 resolve_service_all_complete(q
->auxiliary_for
);
1166 r
= dns_query_process_cname_many(q
);
1167 if (r
== DNS_QUERY_CNAME
) /* This was a cname, and the query was restarted. */
1170 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1171 q
->auxiliary_result
= r
< 0 ? r
: 0;
1172 resolve_service_all_complete(q
->auxiliary_for
);
1175 static int resolve_service_hostname(DnsQuery
*q
, DnsResourceRecord
*rr
, int ifindex
) {
1176 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
1177 _cleanup_(dns_query_freep
) DnsQuery
*aux
= NULL
;
1183 assert(rr
->key
->type
== DNS_TYPE_SRV
);
1185 /* OK, we found an SRV record for the service. Let's resolve
1186 * the hostname included in it */
1188 r
= dns_question_new_address(&question
, q
->request_family
, rr
->srv
.name
, false);
1192 r
= dns_query_new(q
->manager
, &aux
, question
, question
, NULL
, ifindex
, q
->flags
|SD_RESOLVED_NO_SEARCH
);
1196 aux
->request_family
= q
->request_family
;
1197 aux
->complete
= resolve_service_hostname_complete
;
1199 r
= dns_query_make_auxiliary(aux
, q
);
1201 /* Too many auxiliary lookups? If so, don't complain,
1202 * let's just not add this one, we already have more
1208 /* Note that auxiliary queries do not track the original bus
1209 * client, only the primary request does that. */
1211 r
= dns_query_go(aux
);
1219 static void bus_method_resolve_service_complete(DnsQuery
*query
) {
1220 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
1221 bool has_root_domain
= false;
1222 DnsResourceRecord
*rr
;
1223 DnsQuestion
*question
;
1229 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1230 r
= reply_query_state(q
);
1234 r
= dns_query_process_cname_many(q
);
1236 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
1241 if (r
== DNS_QUERY_CNAME
) {
1242 /* This was a cname, and the query was restarted. */
1247 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1249 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
1250 r
= dns_question_matches_rr(question
, rr
, NULL
);
1256 if (rr
->key
->type
!= DNS_TYPE_SRV
)
1259 if (dns_name_is_root(rr
->srv
.name
)) {
1260 has_root_domain
= true;
1264 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
1265 q
->block_all_complete
++;
1266 r
= resolve_service_hostname(q
, rr
, ifindex
);
1267 q
->block_all_complete
--;
1276 if (has_root_domain
&& found
<= 0) {
1277 /* If there's exactly one SRV RR and it uses the root domain as hostname, then the service is
1278 * explicitly not offered on the domain. Report this as a recognizable error. See RFC 2782,
1279 * Section "Usage Rules". */
1280 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_SERVICE
, "'%s' does not provide the requested service", dns_query_string(q
));
1285 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
1289 /* Maybe we are already finished? check now... */
1290 resolve_service_all_complete(TAKE_PTR(q
));
1295 log_error_errno(r
, "Failed to send service reply: %m");
1296 (void) reply_method_errnof(q
, r
, NULL
);
1300 static int bus_method_resolve_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1301 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
1302 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
1303 const char *name
, *type
, *domain
;
1304 Manager
*m
= ASSERT_PTR(userdata
);
1305 int family
, ifindex
;
1311 assert_cc(sizeof(int) == sizeof(int32_t));
1313 r
= sd_bus_message_read(message
, "isssit", &ifindex
, &name
, &type
, &domain
, &family
, &flags
);
1318 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
1320 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
1321 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
1325 else if (!dns_service_name_is_valid(name
))
1326 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid service name '%s'", name
);
1330 else if (!dns_srv_type_is_valid(type
))
1331 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid SRV service type '%s'", type
);
1333 r
= dns_name_is_valid(domain
);
1337 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid domain '%s'", domain
);
1340 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Service name cannot be specified without service type.");
1342 r
= validate_and_mangle_flags(name
, &flags
, SD_RESOLVED_NO_TXT
|SD_RESOLVED_NO_ADDRESS
, error
);
1346 r
= dns_question_new_service(&question_utf8
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), false);
1350 r
= dns_question_new_service(&question_idna
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), true);
1354 bus_client_log(message
, "service resolution");
1356 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
, NULL
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
1360 q
->bus_request
= sd_bus_message_ref(message
);
1361 q
->request_family
= family
;
1362 q
->complete
= bus_method_resolve_service_complete
;
1364 r
= dns_query_bus_track(q
, message
);
1368 r
= dns_query_go(q
);
1376 int bus_dns_server_append(
1377 sd_bus_message
*reply
,
1379 bool with_ifindex
, /* include "ifindex" field */
1380 bool extended
) { /* also include port number and server name */
1388 return sd_bus_message_append(reply
, "(iiayqs)", 0, AF_UNSPEC
, 0, 0, NULL
);
1390 return sd_bus_message_append(reply
, "(iiay)", 0, AF_UNSPEC
, 0);
1393 return sd_bus_message_append(reply
, "(iayqs)", AF_UNSPEC
, 0, 0, NULL
);
1395 return sd_bus_message_append(reply
, "(iay)", AF_UNSPEC
, 0);
1399 r
= sd_bus_message_open_container(
1402 with_ifindex
? (extended
? "iiayqs" : "iiay") :
1403 (extended
? "iayqs" : "iay"));
1408 r
= sd_bus_message_append(reply
, "i", dns_server_ifindex(s
));
1413 r
= sd_bus_message_append(reply
, "i", s
->family
);
1417 r
= sd_bus_message_append_array(reply
, 'y', &s
->address
, FAMILY_ADDRESS_SIZE(s
->family
));
1422 r
= sd_bus_message_append(reply
, "q", s
->port
);
1426 r
= sd_bus_message_append(reply
, "s", s
->server_name
);
1431 return sd_bus_message_close_container(reply
);
1434 static int bus_property_get_dns_servers_internal(
1437 const char *interface
,
1438 const char *property
,
1439 sd_bus_message
*reply
,
1441 sd_bus_error
*error
,
1444 Manager
*m
= ASSERT_PTR(userdata
);
1450 r
= sd_bus_message_open_container(reply
, 'a', extended
? "(iiayqs)" : "(iiay)");
1454 LIST_FOREACH(servers
, s
, m
->dns_servers
) {
1455 r
= bus_dns_server_append(reply
, s
, true, extended
);
1460 HASHMAP_FOREACH(l
, m
->links
)
1461 LIST_FOREACH(servers
, s
, l
->dns_servers
) {
1462 r
= bus_dns_server_append(reply
, s
, true, extended
);
1467 return sd_bus_message_close_container(reply
);
1470 static int bus_property_get_dns_servers(
1473 const char *interface
,
1474 const char *property
,
1475 sd_bus_message
*reply
,
1477 sd_bus_error
*error
) {
1478 return bus_property_get_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1481 static int bus_property_get_dns_servers_ex(
1484 const char *interface
,
1485 const char *property
,
1486 sd_bus_message
*reply
,
1488 sd_bus_error
*error
) {
1489 return bus_property_get_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1492 static int bus_property_get_fallback_dns_servers_internal(
1495 const char *interface
,
1496 const char *property
,
1497 sd_bus_message
*reply
,
1499 sd_bus_error
*error
,
1502 DnsServer
**f
= ASSERT_PTR(userdata
);
1507 r
= sd_bus_message_open_container(reply
, 'a', extended
? "(iiayqs)" : "(iiay)");
1511 LIST_FOREACH(servers
, s
, *f
) {
1512 r
= bus_dns_server_append(reply
, s
, true, extended
);
1517 return sd_bus_message_close_container(reply
);
1520 static int bus_property_get_fallback_dns_servers(
1523 const char *interface
,
1524 const char *property
,
1525 sd_bus_message
*reply
,
1527 sd_bus_error
*error
) {
1528 return bus_property_get_fallback_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1531 static int bus_property_get_fallback_dns_servers_ex(
1534 const char *interface
,
1535 const char *property
,
1536 sd_bus_message
*reply
,
1538 sd_bus_error
*error
) {
1539 return bus_property_get_fallback_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1542 static int bus_property_get_current_dns_server_internal(
1545 const char *interface
,
1546 const char *property
,
1547 sd_bus_message
*reply
,
1549 sd_bus_error
*error
,
1557 s
= *(DnsServer
**) userdata
;
1559 return bus_dns_server_append(reply
, s
, true, extended
);
1562 static int bus_property_get_current_dns_server(
1565 const char *interface
,
1566 const char *property
,
1567 sd_bus_message
*reply
,
1569 sd_bus_error
*error
) {
1570 return bus_property_get_current_dns_server_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1573 static int bus_property_get_current_dns_server_ex(
1576 const char *interface
,
1577 const char *property
,
1578 sd_bus_message
*reply
,
1580 sd_bus_error
*error
) {
1581 return bus_property_get_current_dns_server_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1584 static int bus_property_get_domains(
1587 const char *interface
,
1588 const char *property
,
1589 sd_bus_message
*reply
,
1591 sd_bus_error
*error
) {
1593 Manager
*m
= ASSERT_PTR(userdata
);
1599 r
= sd_bus_message_open_container(reply
, 'a', "(isb)");
1603 LIST_FOREACH(domains
, d
, m
->search_domains
) {
1604 r
= sd_bus_message_append(reply
, "(isb)", 0, d
->name
, d
->route_only
);
1609 HASHMAP_FOREACH(l
, m
->links
) {
1610 LIST_FOREACH(domains
, d
, l
->search_domains
) {
1611 r
= sd_bus_message_append(reply
, "(isb)", l
->ifindex
, d
->name
, d
->route_only
);
1617 return sd_bus_message_close_container(reply
);
1620 static int bus_property_get_transaction_statistics(
1623 const char *interface
,
1624 const char *property
,
1625 sd_bus_message
*reply
,
1627 sd_bus_error
*error
) {
1629 Manager
*m
= ASSERT_PTR(userdata
);
1633 return sd_bus_message_append(reply
, "(tt)",
1634 (uint64_t) hashmap_size(m
->dns_transactions
),
1635 (uint64_t) m
->n_transactions_total
);
1638 static int bus_property_get_cache_statistics(
1641 const char *interface
,
1642 const char *property
,
1643 sd_bus_message
*reply
,
1645 sd_bus_error
*error
) {
1647 uint64_t size
= 0, hit
= 0, miss
= 0;
1648 Manager
*m
= ASSERT_PTR(userdata
);
1652 LIST_FOREACH(scopes
, s
, m
->dns_scopes
) {
1653 size
+= dns_cache_size(&s
->cache
);
1654 hit
+= s
->cache
.n_hit
;
1655 miss
+= s
->cache
.n_miss
;
1658 return sd_bus_message_append(reply
, "(ttt)", size
, hit
, miss
);
1661 static int bus_property_get_dnssec_statistics(
1664 const char *interface
,
1665 const char *property
,
1666 sd_bus_message
*reply
,
1668 sd_bus_error
*error
) {
1670 Manager
*m
= ASSERT_PTR(userdata
);
1674 return sd_bus_message_append(reply
, "(tttt)",
1675 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_SECURE
],
1676 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INSECURE
],
1677 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_BOGUS
],
1678 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INDETERMINATE
]);
1681 static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode
, dns_stub_listener_mode
, DnsStubListenerMode
);
1682 static BUS_DEFINE_PROPERTY_GET(bus_property_get_dnssec_supported
, "b", Manager
, manager_dnssec_supported
);
1683 static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dnssec_mode
, "s", Manager
, manager_get_dnssec_mode
, dnssec_mode_to_string
);
1684 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
);
1686 static int bus_property_get_resolv_conf_mode(
1689 const char *interface
,
1690 const char *property
,
1691 sd_bus_message
*reply
,
1693 sd_bus_error
*error
) {
1699 r
= resolv_conf_mode();
1701 log_warning_errno(r
, "Failed to test /etc/resolv.conf mode, ignoring: %m");
1702 return sd_bus_message_append(reply
, "s", NULL
);
1705 return sd_bus_message_append(reply
, "s", resolv_conf_mode_to_string(r
));
1708 static int bus_method_reset_statistics(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1709 Manager
*m
= ASSERT_PTR(userdata
);
1713 bus_client_log(message
, "statistics reset");
1715 dns_manager_reset_statistics(m
);
1717 return sd_bus_reply_method_return(message
, NULL
);
1720 static int get_any_link(Manager
*m
, int ifindex
, Link
**ret
, sd_bus_error
*error
) {
1726 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
1728 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_LINK
, "Link %i not known", ifindex
);
1734 static int call_link_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
1742 r
= bus_message_read_ifindex(message
, error
, &ifindex
);
1746 r
= get_any_link(m
, ifindex
, &l
, error
);
1750 return handler(message
, l
, error
);
1753 static int bus_method_set_link_dns_servers(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1754 return call_link_method(userdata
, message
, bus_link_method_set_dns_servers
, error
);
1757 static int bus_method_set_link_dns_servers_ex(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1758 return call_link_method(userdata
, message
, bus_link_method_set_dns_servers_ex
, error
);
1761 static int bus_method_set_link_domains(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1762 return call_link_method(userdata
, message
, bus_link_method_set_domains
, error
);
1765 static int bus_method_set_link_default_route(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1766 return call_link_method(userdata
, message
, bus_link_method_set_default_route
, error
);
1769 static int bus_method_set_link_llmnr(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1770 return call_link_method(userdata
, message
, bus_link_method_set_llmnr
, error
);
1773 static int bus_method_set_link_mdns(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1774 return call_link_method(userdata
, message
, bus_link_method_set_mdns
, error
);
1777 static int bus_method_set_link_dns_over_tls(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1778 return call_link_method(userdata
, message
, bus_link_method_set_dns_over_tls
, error
);
1781 static int bus_method_set_link_dnssec(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1782 return call_link_method(userdata
, message
, bus_link_method_set_dnssec
, error
);
1785 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1786 return call_link_method(userdata
, message
, bus_link_method_set_dnssec_negative_trust_anchors
, error
);
1789 static int bus_method_revert_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1790 return call_link_method(userdata
, message
, bus_link_method_revert
, error
);
1793 static int bus_method_get_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1794 _cleanup_free_
char *p
= NULL
;
1795 Manager
*m
= ASSERT_PTR(userdata
);
1801 r
= bus_message_read_ifindex(message
, error
, &ifindex
);
1805 r
= get_any_link(m
, ifindex
, &l
, error
);
1809 p
= link_bus_path(l
);
1813 return sd_bus_reply_method_return(message
, "o", p
);
1816 static int bus_method_flush_caches(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1817 Manager
*m
= ASSERT_PTR(userdata
);
1821 bus_client_log(message
, "cache flush");
1823 manager_flush_caches(m
, LOG_INFO
);
1825 return sd_bus_reply_method_return(message
, NULL
);
1828 static int bus_method_reset_server_features(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1829 Manager
*m
= ASSERT_PTR(userdata
);
1833 bus_client_log(message
, "server feature reset");
1835 manager_reset_server_features(m
);
1837 return sd_bus_reply_method_return(message
, NULL
);
1840 static int dnssd_service_on_bus_track(sd_bus_track
*t
, void *userdata
) {
1841 DnssdService
*s
= ASSERT_PTR(userdata
);
1845 log_debug("Client of active request vanished, destroying DNS-SD service.");
1846 dnssd_service_free(s
);
1851 static int bus_method_register_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1852 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1853 _cleanup_(dnssd_service_freep
) DnssdService
*service
= NULL
;
1854 _cleanup_(sd_bus_track_unrefp
) sd_bus_track
*bus_track
= NULL
;
1855 const char *name
, *name_template
, *type
;
1856 _cleanup_free_
char *path
= NULL
;
1857 DnssdService
*s
= NULL
;
1858 Manager
*m
= ASSERT_PTR(userdata
);
1864 if (m
->mdns_support
!= RESOLVE_SUPPORT_YES
)
1865 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Support for MulticastDNS is disabled");
1867 service
= new0(DnssdService
, 1);
1871 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1875 r
= sd_bus_creds_get_euid(creds
, &euid
);
1878 service
->originator
= euid
;
1880 r
= sd_bus_message_read(message
, "sssqqq", &name
, &name_template
, &type
,
1881 &service
->port
, &service
->priority
,
1886 s
= hashmap_get(m
->dnssd_services
, name
);
1888 return sd_bus_error_setf(error
, BUS_ERROR_DNSSD_SERVICE_EXISTS
, "DNS-SD service '%s' exists already", name
);
1890 if (!dnssd_srv_type_is_valid(type
))
1891 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DNS-SD service type '%s' is invalid", type
);
1893 service
->name
= strdup(name
);
1897 service
->name_template
= strdup(name_template
);
1898 if (!service
->name_template
)
1901 service
->type
= strdup(type
);
1905 r
= dnssd_render_instance_name(m
, service
, NULL
);
1909 r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_ARRAY
, "a{say}");
1913 while ((r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_ARRAY
, "{say}")) > 0) {
1914 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
1915 DnsTxtItem
*last
= NULL
;
1917 txt_data
= new0(DnssdTxtData
, 1);
1921 while ((r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_DICT_ENTRY
, "say")) > 0) {
1927 r
= sd_bus_message_read(message
, "s", &key
);
1932 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Keys in DNS-SD TXT RRs can't be empty");
1934 if (!ascii_is_valid(key
))
1935 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "TXT key '%s' contains non-ASCII symbols", key
);
1937 r
= sd_bus_message_read_array(message
, 'y', &value
, &size
);
1941 r
= dnssd_txt_item_new_from_data(key
, value
, size
, &i
);
1945 LIST_INSERT_AFTER(items
, txt_data
->txts
, last
, i
);
1948 r
= sd_bus_message_exit_container(message
);
1956 r
= sd_bus_message_exit_container(message
);
1960 if (txt_data
->txts
) {
1961 LIST_PREPEND(items
, service
->txt_data_items
, txt_data
);
1968 r
= sd_bus_message_exit_container(message
);
1972 if (!service
->txt_data_items
) {
1973 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
1975 txt_data
= new0(DnssdTxtData
, 1);
1979 r
= dns_txt_item_new_empty(&txt_data
->txts
);
1983 LIST_PREPEND(items
, service
->txt_data_items
, txt_data
);
1987 r
= sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service
->name
, &path
);
1991 r
= bus_verify_polkit_async(
1993 "org.freedesktop.resolve1.register-service",
1994 /* details= */ NULL
,
1995 &m
->polkit_registry
,
2000 return 1; /* Polkit will call us back */
2002 r
= hashmap_ensure_put(&m
->dnssd_services
, &string_hash_ops
, service
->name
, service
);
2006 r
= sd_bus_track_new(sd_bus_message_get_bus(message
), &bus_track
, dnssd_service_on_bus_track
, service
);
2010 r
= sd_bus_track_add_sender(bus_track
, message
);
2014 service
->manager
= m
;
2018 manager_refresh_rrs(m
);
2020 return sd_bus_reply_method_return(message
, "o", path
);
2023 static int call_dnssd_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
2024 _cleanup_free_
char *name
= NULL
;
2025 DnssdService
*s
= NULL
;
2033 r
= sd_bus_message_read(message
, "o", &path
);
2037 r
= sd_bus_path_decode(path
, "/org/freedesktop/resolve1/dnssd", &name
);
2039 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_DNSSD_SERVICE
, "DNS-SD service with object path '%s' does not exist", path
);
2043 s
= hashmap_get(m
->dnssd_services
, name
);
2045 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_DNSSD_SERVICE
, "DNS-SD service '%s' not known", name
);
2047 return handler(message
, s
, error
);
2050 static int bus_method_unregister_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2051 Manager
*m
= ASSERT_PTR(userdata
);
2055 return call_dnssd_method(m
, message
, bus_dnssd_method_unregister
, error
);
2058 static const sd_bus_vtable resolve_vtable
[] = {
2059 SD_BUS_VTABLE_START(0),
2060 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL
, offsetof(Manager
, llmnr_hostname
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2061 SD_BUS_PROPERTY("LLMNR", "s", bus_property_get_resolve_support
, offsetof(Manager
, llmnr_support
), 0),
2062 SD_BUS_PROPERTY("MulticastDNS", "s", bus_property_get_resolve_support
, offsetof(Manager
, mdns_support
), 0),
2063 SD_BUS_PROPERTY("DNSOverTLS", "s", bus_property_get_dns_over_tls_mode
, 0, 0),
2064 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2065 SD_BUS_PROPERTY("DNSEx", "a(iiayqs)", bus_property_get_dns_servers_ex
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2066 SD_BUS_PROPERTY("FallbackDNS", "a(iiay)", bus_property_get_fallback_dns_servers
, offsetof(Manager
, fallback_dns_servers
), SD_BUS_VTABLE_PROPERTY_CONST
),
2067 SD_BUS_PROPERTY("FallbackDNSEx", "a(iiayqs)", bus_property_get_fallback_dns_servers_ex
, offsetof(Manager
, fallback_dns_servers
), SD_BUS_VTABLE_PROPERTY_CONST
),
2068 SD_BUS_PROPERTY("CurrentDNSServer", "(iiay)", bus_property_get_current_dns_server
, offsetof(Manager
, current_dns_server
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2069 SD_BUS_PROPERTY("CurrentDNSServerEx", "(iiayqs)", bus_property_get_current_dns_server_ex
, offsetof(Manager
, current_dns_server
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2070 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains
, 0, 0),
2071 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics
, 0, 0),
2072 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics
, 0, 0),
2073 SD_BUS_PROPERTY("DNSSEC", "s", bus_property_get_dnssec_mode
, 0, 0),
2074 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics
, 0, 0),
2075 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported
, 0, 0),
2076 SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_string_set
, offsetof(Manager
, trust_anchor
.negative_by_name
), 0),
2077 SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode
, offsetof(Manager
, dns_stub_listener_mode
), 0),
2078 SD_BUS_PROPERTY("ResolvConfMode", "s", bus_property_get_resolv_conf_mode
, 0, 0),
2080 SD_BUS_METHOD_WITH_ARGS("ResolveHostname",
2081 SD_BUS_ARGS("i", ifindex
, "s", name
, "i", family
, "t", flags
),
2082 SD_BUS_RESULT("a(iiay)", addresses
, "s", canonical
, "t", flags
),
2083 bus_method_resolve_hostname
,
2084 SD_BUS_VTABLE_UNPRIVILEGED
),
2085 SD_BUS_METHOD_WITH_ARGS("ResolveAddress",
2086 SD_BUS_ARGS("i", ifindex
, "i", family
, "ay", address
, "t", flags
),
2087 SD_BUS_RESULT("a(is)", names
, "t", flags
),
2088 bus_method_resolve_address
,
2089 SD_BUS_VTABLE_UNPRIVILEGED
),
2090 SD_BUS_METHOD_WITH_ARGS("ResolveRecord",
2091 SD_BUS_ARGS("i", ifindex
, "s", name
, "q", class, "q", type
, "t", flags
),
2092 SD_BUS_RESULT("a(iqqay)", records
, "t", flags
),
2093 bus_method_resolve_record
,
2094 SD_BUS_VTABLE_UNPRIVILEGED
),
2095 SD_BUS_METHOD_WITH_ARGS("ResolveService",
2096 SD_BUS_ARGS("i", ifindex
,
2102 SD_BUS_RESULT("a(qqqsa(iiay)s)", srv_data
,
2104 "s", canonical_name
,
2105 "s", canonical_type
,
2106 "s", canonical_domain
,
2108 bus_method_resolve_service
,
2109 SD_BUS_VTABLE_UNPRIVILEGED
),
2110 SD_BUS_METHOD_WITH_ARGS("GetLink",
2111 SD_BUS_ARGS("i", ifindex
),
2112 SD_BUS_RESULT("o", path
),
2113 bus_method_get_link
,
2114 SD_BUS_VTABLE_UNPRIVILEGED
),
2115 SD_BUS_METHOD_WITH_ARGS("SetLinkDNS",
2116 SD_BUS_ARGS("i", ifindex
, "a(iay)", addresses
),
2118 bus_method_set_link_dns_servers
,
2119 SD_BUS_VTABLE_UNPRIVILEGED
),
2120 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSEx",
2121 SD_BUS_ARGS("i", ifindex
, "a(iayqs)", addresses
),
2123 bus_method_set_link_dns_servers_ex
,
2124 SD_BUS_VTABLE_UNPRIVILEGED
),
2125 SD_BUS_METHOD_WITH_ARGS("SetLinkDomains",
2126 SD_BUS_ARGS("i", ifindex
, "a(sb)", domains
),
2128 bus_method_set_link_domains
,
2129 SD_BUS_VTABLE_UNPRIVILEGED
),
2130 SD_BUS_METHOD_WITH_ARGS("SetLinkDefaultRoute",
2131 SD_BUS_ARGS("i", ifindex
, "b", enable
),
2133 bus_method_set_link_default_route
,
2134 SD_BUS_VTABLE_UNPRIVILEGED
),
2135 SD_BUS_METHOD_WITH_ARGS("SetLinkLLMNR",
2136 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2138 bus_method_set_link_llmnr
,
2139 SD_BUS_VTABLE_UNPRIVILEGED
),
2140 SD_BUS_METHOD_WITH_ARGS("SetLinkMulticastDNS",
2141 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2143 bus_method_set_link_mdns
,
2144 SD_BUS_VTABLE_UNPRIVILEGED
),
2145 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSOverTLS",
2146 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2148 bus_method_set_link_dns_over_tls
,
2149 SD_BUS_VTABLE_UNPRIVILEGED
),
2150 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSEC",
2151 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2153 bus_method_set_link_dnssec
,
2154 SD_BUS_VTABLE_UNPRIVILEGED
),
2155 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSECNegativeTrustAnchors",
2156 SD_BUS_ARGS("i", ifindex
, "as", names
),
2158 bus_method_set_link_dnssec_negative_trust_anchors
,
2159 SD_BUS_VTABLE_UNPRIVILEGED
),
2160 SD_BUS_METHOD_WITH_ARGS("RevertLink",
2161 SD_BUS_ARGS("i", ifindex
),
2163 bus_method_revert_link
,
2164 SD_BUS_VTABLE_UNPRIVILEGED
),
2165 SD_BUS_METHOD_WITH_ARGS("RegisterService",
2166 SD_BUS_ARGS("s", name
,
2170 "q", service_priority
,
2171 "q", service_weight
,
2172 "aa{say}", txt_datas
),
2173 SD_BUS_RESULT("o", service_path
),
2174 bus_method_register_service
,
2175 SD_BUS_VTABLE_UNPRIVILEGED
),
2176 SD_BUS_METHOD_WITH_ARGS("UnregisterService",
2177 SD_BUS_ARGS("o", service_path
),
2179 bus_method_unregister_service
,
2180 SD_BUS_VTABLE_UNPRIVILEGED
),
2181 SD_BUS_METHOD_WITH_ARGS("ResetStatistics",
2184 bus_method_reset_statistics
,
2185 SD_BUS_VTABLE_UNPRIVILEGED
),
2186 SD_BUS_METHOD_WITH_ARGS("FlushCaches",
2189 bus_method_flush_caches
,
2190 SD_BUS_VTABLE_UNPRIVILEGED
),
2191 SD_BUS_METHOD_WITH_ARGS("ResetServerFeatures",
2194 bus_method_reset_server_features
,
2195 SD_BUS_VTABLE_UNPRIVILEGED
),
2200 const BusObjectImplementation manager_object
= {
2201 "/org/freedesktop/resolve1",
2202 "org.freedesktop.resolve1.Manager",
2203 .vtables
= BUS_VTABLES(resolve_vtable
),
2204 .children
= BUS_IMPLEMENTATIONS(&link_object
,
2208 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
2209 Manager
*m
= ASSERT_PTR(userdata
);
2214 r
= sd_bus_message_read(message
, "b", &b
);
2216 bus_log_parse_error(r
);
2223 log_debug("Coming back from suspend, verifying all RRs...");
2225 manager_verify_all(m
);
2229 int manager_connect_bus(Manager
*m
) {
2237 r
= bus_open_system_watch_bind_with_description(&m
->bus
, "bus-api-resolve");
2239 return log_error_errno(r
, "Failed to connect to system bus: %m");
2241 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
2245 r
= bus_log_control_api_register(m
->bus
);
2249 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.resolve1", 0, NULL
, NULL
);
2251 return log_error_errno(r
, "Failed to request name: %m");
2253 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
2255 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
2257 r
= bus_match_signal_async(
2262 match_prepare_for_sleep
,
2266 log_warning_errno(r
, "Failed to request match for PrepareForSleep, ignoring: %m");
2271 int _manager_send_changed(Manager
*manager
, const char *property
, ...) {
2274 if (sd_bus_is_ready(manager
->bus
) <= 0)
2277 char **l
= strv_from_stdarg_alloca(property
);
2279 int r
= sd_bus_emit_properties_changed_strv(
2281 "/org/freedesktop/resolve1",
2282 "org.freedesktop.resolve1.Manager",
2285 log_notice_errno(r
, "Failed to emit notification about changed property %s: %m", property
);