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
|
364 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid flags parameter");
366 if ((*flags
& SD_RESOLVED_PROTOCOLS_ALL
) == 0) /* If no protocol is enabled, enable all */
367 *flags
|= SD_RESOLVED_PROTOCOLS_ALL
;
369 /* Imply SD_RESOLVED_NO_SEARCH if permitted and name is dot suffixed. */
370 if (name
&& FLAGS_SET(ok
, SD_RESOLVED_NO_SEARCH
) && dns_name_dot_suffixed(name
) > 0)
371 *flags
|= SD_RESOLVED_NO_SEARCH
;
376 static int parse_as_address(sd_bus_message
*m
, int ifindex
, const char *hostname
, int family
, uint64_t flags
) {
377 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
378 _cleanup_free_
char *canonical
= NULL
;
379 union in_addr_union parsed
;
380 int r
, ff
, parsed_ifindex
= 0;
382 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
383 * let's not attempt to look it up. */
385 r
= in_addr_ifindex_from_string_auto(hostname
, &ff
, &parsed
, &parsed_ifindex
);
386 if (r
< 0) /* not an address */
389 if (family
!= AF_UNSPEC
&& ff
!= family
)
390 return sd_bus_reply_method_errorf(m
, BUS_ERROR_NO_SUCH_RR
, "The specified address is not of the requested family.");
391 if (ifindex
> 0 && parsed_ifindex
> 0 && parsed_ifindex
!= ifindex
)
392 return sd_bus_reply_method_errorf(m
, BUS_ERROR_NO_SUCH_RR
, "The specified address interface index does not match requested interface.");
394 if (parsed_ifindex
> 0)
395 ifindex
= parsed_ifindex
;
397 r
= sd_bus_message_new_method_return(m
, &reply
);
401 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
405 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
409 r
= sd_bus_message_append(reply
, "ii", ifindex
, ff
);
413 r
= sd_bus_message_append_array(reply
, 'y', &parsed
, FAMILY_ADDRESS_SIZE(ff
));
417 r
= sd_bus_message_close_container(reply
);
421 r
= sd_bus_message_close_container(reply
);
425 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
426 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
427 * omissions are always done the same way). */
428 r
= in_addr_ifindex_to_string(ff
, &parsed
, ifindex
, &canonical
);
432 r
= sd_bus_message_append(reply
, "st", canonical
,
433 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags
), ff
, true, true) |
434 SD_RESOLVED_SYNTHETIC
);
438 return sd_bus_send(sd_bus_message_get_bus(m
), reply
, NULL
);
441 void bus_client_log(sd_bus_message
*m
, const char *what
) {
442 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
443 const char *comm
= NULL
;
444 uid_t uid
= UID_INVALID
;
454 r
= sd_bus_query_sender_creds(m
, SD_BUS_CREDS_PID
|SD_BUS_CREDS_UID
|SD_BUS_CREDS_COMM
|SD_BUS_CREDS_AUGMENT
, &creds
);
456 return (void) log_debug_errno(r
, "Failed to query client credentials, ignoring: %m");
458 (void) sd_bus_creds_get_uid(creds
, &uid
);
459 (void) sd_bus_creds_get_pid(creds
, &pid
);
460 (void) sd_bus_creds_get_comm(creds
, &comm
);
462 log_debug("D-Bus %s request from client PID " PID_FMT
" (%s) with UID " UID_FMT
,
463 what
, pid
, strna(comm
), uid
);
466 static int bus_method_resolve_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
467 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
468 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
469 Manager
*m
= ASSERT_PTR(userdata
);
470 const char *hostname
;
477 assert_cc(sizeof(int) == sizeof(int32_t));
479 r
= sd_bus_message_read(message
, "isit", &ifindex
, &hostname
, &family
, &flags
);
484 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
486 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
487 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
489 r
= validate_and_mangle_flags(hostname
, &flags
, SD_RESOLVED_NO_SEARCH
, error
);
493 r
= parse_as_address(message
, ifindex
, hostname
, family
, flags
);
497 r
= dns_name_is_valid(hostname
);
501 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid hostname '%s'", hostname
);
503 r
= dns_question_new_address(&question_utf8
, family
, hostname
, false);
507 r
= dns_question_new_address(&question_idna
, family
, hostname
, true);
508 if (r
< 0 && r
!= -EALREADY
)
511 bus_client_log(message
, "hostname resolution");
513 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
?: question_utf8
, NULL
, ifindex
, flags
);
517 q
->bus_request
= sd_bus_message_ref(message
);
518 q
->request_family
= family
;
519 q
->complete
= bus_method_resolve_hostname_complete
;
521 r
= dns_query_bus_track(q
, message
);
533 static void bus_method_resolve_address_complete(DnsQuery
*query
) {
534 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
535 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
536 DnsQuestion
*question
;
537 DnsResourceRecord
*rr
;
543 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
544 r
= reply_query_state(q
);
548 r
= dns_query_process_cname_many(q
);
550 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
555 if (r
== DNS_QUERY_CNAME
) {
556 /* This was a cname, and the query was restarted. */
561 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
565 r
= sd_bus_message_open_container(reply
, 'a', "(is)");
569 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
571 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
572 _cleanup_free_
char *normalized
= NULL
;
574 r
= dns_question_matches_rr(question
, rr
, NULL
);
580 r
= dns_name_normalize(rr
->ptr
.name
, 0, &normalized
);
584 r
= sd_bus_message_append(reply
, "(is)", ifindex
, normalized
);
592 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
,
593 "Address %s does not have any RR of requested type",
594 IN_ADDR_TO_STRING(q
->request_family
, &q
->request_address
));
598 r
= sd_bus_message_close_container(reply
);
602 r
= sd_bus_message_append(reply
, "t", dns_query_reply_flags_make(q
));
606 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
607 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
611 log_error_errno(r
, "Failed to send address reply: %m");
612 (void) reply_method_errnof(q
, r
, NULL
);
616 static int bus_method_resolve_address(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
617 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
618 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
619 Manager
*m
= ASSERT_PTR(userdata
);
620 union in_addr_union a
;
627 assert_cc(sizeof(int) == sizeof(int32_t));
629 r
= sd_bus_message_read(message
, "i", &ifindex
);
633 r
= bus_message_read_in_addr_auto(message
, error
, &family
, &a
);
637 r
= sd_bus_message_read(message
, "t", &flags
);
642 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
644 r
= validate_and_mangle_flags(NULL
, &flags
, 0, error
);
648 r
= dns_question_new_reverse(&question
, family
, &a
);
652 bus_client_log(message
, "address resolution");
654 r
= dns_query_new(m
, &q
, question
, question
, NULL
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
658 q
->bus_request
= sd_bus_message_ref(message
);
659 q
->request_family
= family
;
660 q
->request_address
= a
;
661 q
->complete
= bus_method_resolve_address_complete
;
663 r
= dns_query_bus_track(q
, message
);
675 static int bus_message_append_rr(sd_bus_message
*m
, DnsResourceRecord
*rr
, int ifindex
) {
681 r
= sd_bus_message_open_container(m
, 'r', "iqqay");
685 r
= sd_bus_message_append(m
, "iqq",
692 r
= dns_resource_record_to_wire_format(rr
, false);
696 r
= sd_bus_message_append_array(m
, 'y', rr
->wire_format
, rr
->wire_format_size
);
700 return sd_bus_message_close_container(m
);
703 static void bus_method_resolve_record_complete(DnsQuery
*query
) {
704 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
705 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
706 DnsResourceRecord
*rr
;
707 DnsQuestion
*question
;
714 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
715 r
= reply_query_state(q
);
719 r
= dns_query_process_cname_many(q
);
721 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
726 if (r
== DNS_QUERY_CNAME
) {
727 /* This was a cname, and the query was restarted. */
732 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
736 r
= sd_bus_message_open_container(reply
, 'a', "(iqqay)");
740 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
742 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
743 r
= dns_question_matches_rr(question
, rr
, NULL
);
749 r
= bus_message_append_rr(reply
, rr
, ifindex
);
757 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
));
761 r
= sd_bus_message_close_container(reply
);
765 r
= sd_bus_message_append(reply
, "t", dns_query_reply_flags_make(q
));
769 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
770 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
774 log_error_errno(r
, "Failed to send record reply: %m");
775 (void) reply_method_errnof(q
, r
, NULL
);
779 static int bus_method_resolve_record(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
780 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
781 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
782 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
783 Manager
*m
= ASSERT_PTR(userdata
);
784 uint16_t class, type
;
791 assert_cc(sizeof(int) == sizeof(int32_t));
793 r
= sd_bus_message_read(message
, "isqqt", &ifindex
, &name
, &class, &type
, &flags
);
798 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
800 r
= dns_name_is_valid(name
);
804 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid name '%s'", name
);
806 if (!dns_type_is_valid_query(type
))
807 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified resource record type %" PRIu16
" may not be used in a query.", type
);
808 if (dns_type_is_zone_transer(type
))
809 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Zone transfers not permitted via this programming interface.");
810 if (dns_type_is_obsolete(type
))
811 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Specified DNS resource record type %" PRIu16
" is obsolete.", type
);
813 r
= validate_and_mangle_flags(name
, &flags
, 0, error
);
817 question
= dns_question_new(1);
821 key
= dns_resource_key_new(class, type
, name
);
825 r
= dns_question_add(question
, key
, 0);
829 bus_client_log(message
, "resource record resolution");
831 /* Setting SD_RESOLVED_CLAMP_TTL: let's request that the TTL is fixed up for locally cached entries,
832 * after all we return it in the wire format blob. */
833 r
= dns_query_new(m
, &q
, question
, question
, NULL
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
|SD_RESOLVED_CLAMP_TTL
);
837 q
->bus_request
= sd_bus_message_ref(message
);
838 q
->complete
= bus_method_resolve_record_complete
;
840 r
= dns_query_bus_track(q
, message
);
852 static int append_srv(DnsQuery
*q
, sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
853 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
854 _cleanup_free_
char *normalized
= NULL
;
862 if (rr
->key
->type
!= DNS_TYPE_SRV
)
865 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
866 /* First, let's see if we could find an appropriate A or AAAA
867 * record for the SRV record */
868 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
869 DnsResourceRecord
*zz
;
870 DnsQuestion
*question
;
872 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
874 if (aux
->auxiliary_result
!= 0)
877 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
879 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
885 DNS_ANSWER_FOREACH(zz
, aux
->answer
) {
887 r
= dns_question_matches_rr(question
, zz
, NULL
);
893 canonical
= dns_resource_record_ref(zz
);
901 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
906 r
= sd_bus_message_open_container(reply
, 'r', "qqqsa(iiay)s");
910 r
= dns_name_normalize(rr
->srv
.name
, 0, &normalized
);
914 r
= sd_bus_message_append(
917 rr
->srv
.priority
, rr
->srv
.weight
, rr
->srv
.port
, normalized
);
921 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
925 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
926 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
927 DnsResourceRecord
*zz
;
928 DnsQuestion
*question
;
931 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
933 if (aux
->auxiliary_result
!= 0)
936 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
938 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
944 DNS_ANSWER_FOREACH_IFINDEX(zz
, ifindex
, aux
->answer
) {
946 r
= dns_question_matches_rr(question
, zz
, NULL
);
952 r
= append_address(reply
, zz
, ifindex
);
959 r
= sd_bus_message_close_container(reply
);
964 normalized
= mfree(normalized
);
966 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), 0, &normalized
);
971 /* Note that above we appended the hostname as encoded in the
972 * SRV, and here the canonical hostname this maps to. */
973 r
= sd_bus_message_append(reply
, "s", normalized
);
977 r
= sd_bus_message_close_container(reply
);
984 static int append_txt(sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
991 if (rr
->key
->type
!= DNS_TYPE_TXT
)
994 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
999 r
= sd_bus_message_append_array(reply
, 'y', i
->data
, i
->length
);
1007 static void resolve_service_all_complete(DnsQuery
*query
) {
1008 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
1009 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1010 _cleanup_free_
char *name
= NULL
, *type
= NULL
, *domain
= NULL
;
1011 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
1012 DnsQuestion
*question
;
1013 DnsResourceRecord
*rr
;
1019 if (q
->block_all_complete
> 0) {
1024 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
1025 DnsQuery
*bad
= NULL
;
1026 bool have_success
= false;
1028 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
1030 switch (aux
->state
) {
1032 case DNS_TRANSACTION_PENDING
:
1033 /* If an auxiliary query is still pending, let's wait */
1037 case DNS_TRANSACTION_SUCCESS
:
1038 if (aux
->auxiliary_result
== 0)
1039 have_success
= true;
1050 if (!have_success
) {
1051 /* We can only return one error, hence pick the last error we encountered */
1055 if (bad
->state
== DNS_TRANSACTION_SUCCESS
) {
1056 assert(bad
->auxiliary_result
!= 0);
1058 if (bad
->auxiliary_result
== -ELOOP
) {
1059 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad
));
1063 assert(bad
->auxiliary_result
< 0);
1064 r
= bad
->auxiliary_result
;
1068 r
= reply_query_state(bad
);
1073 r
= sd_bus_message_new_method_return(q
->bus_request
, &reply
);
1077 r
= sd_bus_message_open_container(reply
, 'a', "(qqqsa(iiay)s)");
1081 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1083 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1084 r
= dns_question_matches_rr(question
, rr
, NULL
);
1090 r
= append_srv(q
, reply
, rr
);
1093 if (r
== 0) /* not an SRV record */
1097 canonical
= dns_resource_record_ref(rr
);
1103 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
1107 r
= sd_bus_message_close_container(reply
);
1111 r
= sd_bus_message_open_container(reply
, 'a', "ay");
1115 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1116 r
= dns_question_matches_rr(question
, rr
, NULL
);
1122 r
= append_txt(reply
, rr
);
1127 r
= sd_bus_message_close_container(reply
);
1132 r
= dns_service_split(dns_resource_key_name(canonical
->key
), &name
, &type
, &domain
);
1136 r
= sd_bus_message_append(
1140 dns_query_reply_flags_make(q
));
1144 q
->bus_request
= sd_bus_message_unref(q
->bus_request
);
1145 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
1149 log_error_errno(r
, "Failed to send service reply: %m");
1150 (void) reply_method_errnof(q
, r
, NULL
);
1154 static void resolve_service_hostname_complete(DnsQuery
*q
) {
1158 assert(q
->auxiliary_for
);
1160 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1161 resolve_service_all_complete(q
->auxiliary_for
);
1165 r
= dns_query_process_cname_many(q
);
1166 if (r
== DNS_QUERY_CNAME
) /* This was a cname, and the query was restarted. */
1169 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1170 q
->auxiliary_result
= r
< 0 ? r
: 0;
1171 resolve_service_all_complete(q
->auxiliary_for
);
1174 static int resolve_service_hostname(DnsQuery
*q
, DnsResourceRecord
*rr
, int ifindex
) {
1175 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
1176 _cleanup_(dns_query_freep
) DnsQuery
*aux
= NULL
;
1182 assert(rr
->key
->type
== DNS_TYPE_SRV
);
1184 /* OK, we found an SRV record for the service. Let's resolve
1185 * the hostname included in it */
1187 r
= dns_question_new_address(&question
, q
->request_family
, rr
->srv
.name
, false);
1191 r
= dns_query_new(q
->manager
, &aux
, question
, question
, NULL
, ifindex
, q
->flags
|SD_RESOLVED_NO_SEARCH
);
1195 aux
->request_family
= q
->request_family
;
1196 aux
->complete
= resolve_service_hostname_complete
;
1198 r
= dns_query_make_auxiliary(aux
, q
);
1200 /* Too many auxiliary lookups? If so, don't complain,
1201 * let's just not add this one, we already have more
1207 /* Note that auxiliary queries do not track the original bus
1208 * client, only the primary request does that. */
1210 r
= dns_query_go(aux
);
1218 static void bus_method_resolve_service_complete(DnsQuery
*query
) {
1219 _cleanup_(dns_query_freep
) DnsQuery
*q
= query
;
1220 bool has_root_domain
= false;
1221 DnsResourceRecord
*rr
;
1222 DnsQuestion
*question
;
1228 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1229 r
= reply_query_state(q
);
1233 r
= dns_query_process_cname_many(q
);
1235 r
= reply_method_errorf(q
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
1240 if (r
== DNS_QUERY_CNAME
) {
1241 /* This was a cname, and the query was restarted. */
1246 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1248 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
1249 r
= dns_question_matches_rr(question
, rr
, NULL
);
1255 if (rr
->key
->type
!= DNS_TYPE_SRV
)
1258 if (dns_name_is_root(rr
->srv
.name
)) {
1259 has_root_domain
= true;
1263 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
1264 q
->block_all_complete
++;
1265 r
= resolve_service_hostname(q
, rr
, ifindex
);
1266 q
->block_all_complete
--;
1275 if (has_root_domain
&& found
<= 0) {
1276 /* If there's exactly one SRV RR and it uses the root domain as hostname, then the service is
1277 * explicitly not offered on the domain. Report this as a recognizable error. See RFC 2782,
1278 * Section "Usage Rules". */
1279 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_SERVICE
, "'%s' does not provide the requested service", dns_query_string(q
));
1284 r
= reply_method_errorf(q
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
1288 /* Maybe we are already finished? check now... */
1289 resolve_service_all_complete(TAKE_PTR(q
));
1294 log_error_errno(r
, "Failed to send service reply: %m");
1295 (void) reply_method_errnof(q
, r
, NULL
);
1299 static int bus_method_resolve_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1300 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
1301 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
1302 const char *name
, *type
, *domain
;
1303 Manager
*m
= ASSERT_PTR(userdata
);
1304 int family
, ifindex
;
1310 assert_cc(sizeof(int) == sizeof(int32_t));
1312 r
= sd_bus_message_read(message
, "isssit", &ifindex
, &name
, &type
, &domain
, &family
, &flags
);
1317 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
1319 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
1320 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
1324 else if (!dns_service_name_is_valid(name
))
1325 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid service name '%s'", name
);
1329 else if (!dns_srv_type_is_valid(type
))
1330 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid SRV service type '%s'", type
);
1332 r
= dns_name_is_valid(domain
);
1336 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid domain '%s'", domain
);
1339 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Service name cannot be specified without service type.");
1341 r
= validate_and_mangle_flags(name
, &flags
, SD_RESOLVED_NO_TXT
|SD_RESOLVED_NO_ADDRESS
, error
);
1345 r
= dns_question_new_service(&question_utf8
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), false);
1349 r
= dns_question_new_service(&question_idna
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), true);
1353 bus_client_log(message
, "service resolution");
1355 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
, NULL
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
1359 q
->bus_request
= sd_bus_message_ref(message
);
1360 q
->request_family
= family
;
1361 q
->complete
= bus_method_resolve_service_complete
;
1363 r
= dns_query_bus_track(q
, message
);
1367 r
= dns_query_go(q
);
1375 int bus_dns_server_append(
1376 sd_bus_message
*reply
,
1378 bool with_ifindex
, /* include "ifindex" field */
1379 bool extended
) { /* also include port number and server name */
1387 return sd_bus_message_append(reply
, "(iiayqs)", 0, AF_UNSPEC
, 0, 0, NULL
);
1389 return sd_bus_message_append(reply
, "(iiay)", 0, AF_UNSPEC
, 0);
1392 return sd_bus_message_append(reply
, "(iayqs)", AF_UNSPEC
, 0, 0, NULL
);
1394 return sd_bus_message_append(reply
, "(iay)", AF_UNSPEC
, 0);
1398 r
= sd_bus_message_open_container(
1401 with_ifindex
? (extended
? "iiayqs" : "iiay") :
1402 (extended
? "iayqs" : "iay"));
1407 r
= sd_bus_message_append(reply
, "i", dns_server_ifindex(s
));
1412 r
= sd_bus_message_append(reply
, "i", s
->family
);
1416 r
= sd_bus_message_append_array(reply
, 'y', &s
->address
, FAMILY_ADDRESS_SIZE(s
->family
));
1421 r
= sd_bus_message_append(reply
, "q", s
->port
);
1425 r
= sd_bus_message_append(reply
, "s", s
->server_name
);
1430 return sd_bus_message_close_container(reply
);
1433 static int bus_property_get_dns_servers_internal(
1436 const char *interface
,
1437 const char *property
,
1438 sd_bus_message
*reply
,
1440 sd_bus_error
*error
,
1443 Manager
*m
= ASSERT_PTR(userdata
);
1449 r
= sd_bus_message_open_container(reply
, 'a', extended
? "(iiayqs)" : "(iiay)");
1453 LIST_FOREACH(servers
, s
, m
->dns_servers
) {
1454 r
= bus_dns_server_append(reply
, s
, true, extended
);
1459 HASHMAP_FOREACH(l
, m
->links
)
1460 LIST_FOREACH(servers
, s
, l
->dns_servers
) {
1461 r
= bus_dns_server_append(reply
, s
, true, extended
);
1466 return sd_bus_message_close_container(reply
);
1469 static int bus_property_get_dns_servers(
1472 const char *interface
,
1473 const char *property
,
1474 sd_bus_message
*reply
,
1476 sd_bus_error
*error
) {
1477 return bus_property_get_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1480 static int bus_property_get_dns_servers_ex(
1483 const char *interface
,
1484 const char *property
,
1485 sd_bus_message
*reply
,
1487 sd_bus_error
*error
) {
1488 return bus_property_get_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1491 static int bus_property_get_fallback_dns_servers_internal(
1494 const char *interface
,
1495 const char *property
,
1496 sd_bus_message
*reply
,
1498 sd_bus_error
*error
,
1501 DnsServer
**f
= ASSERT_PTR(userdata
);
1506 r
= sd_bus_message_open_container(reply
, 'a', extended
? "(iiayqs)" : "(iiay)");
1510 LIST_FOREACH(servers
, s
, *f
) {
1511 r
= bus_dns_server_append(reply
, s
, true, extended
);
1516 return sd_bus_message_close_container(reply
);
1519 static int bus_property_get_fallback_dns_servers(
1522 const char *interface
,
1523 const char *property
,
1524 sd_bus_message
*reply
,
1526 sd_bus_error
*error
) {
1527 return bus_property_get_fallback_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1530 static int bus_property_get_fallback_dns_servers_ex(
1533 const char *interface
,
1534 const char *property
,
1535 sd_bus_message
*reply
,
1537 sd_bus_error
*error
) {
1538 return bus_property_get_fallback_dns_servers_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1541 static int bus_property_get_current_dns_server_internal(
1544 const char *interface
,
1545 const char *property
,
1546 sd_bus_message
*reply
,
1548 sd_bus_error
*error
,
1556 s
= *(DnsServer
**) userdata
;
1558 return bus_dns_server_append(reply
, s
, true, extended
);
1561 static int bus_property_get_current_dns_server(
1564 const char *interface
,
1565 const char *property
,
1566 sd_bus_message
*reply
,
1568 sd_bus_error
*error
) {
1569 return bus_property_get_current_dns_server_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, false);
1572 static int bus_property_get_current_dns_server_ex(
1575 const char *interface
,
1576 const char *property
,
1577 sd_bus_message
*reply
,
1579 sd_bus_error
*error
) {
1580 return bus_property_get_current_dns_server_internal(bus
, path
, interface
, property
, reply
, userdata
, error
, true);
1583 static int bus_property_get_domains(
1586 const char *interface
,
1587 const char *property
,
1588 sd_bus_message
*reply
,
1590 sd_bus_error
*error
) {
1592 Manager
*m
= ASSERT_PTR(userdata
);
1598 r
= sd_bus_message_open_container(reply
, 'a', "(isb)");
1602 LIST_FOREACH(domains
, d
, m
->search_domains
) {
1603 r
= sd_bus_message_append(reply
, "(isb)", 0, d
->name
, d
->route_only
);
1608 HASHMAP_FOREACH(l
, m
->links
) {
1609 LIST_FOREACH(domains
, d
, l
->search_domains
) {
1610 r
= sd_bus_message_append(reply
, "(isb)", l
->ifindex
, d
->name
, d
->route_only
);
1616 return sd_bus_message_close_container(reply
);
1619 static int bus_property_get_transaction_statistics(
1622 const char *interface
,
1623 const char *property
,
1624 sd_bus_message
*reply
,
1626 sd_bus_error
*error
) {
1628 Manager
*m
= ASSERT_PTR(userdata
);
1632 return sd_bus_message_append(reply
, "(tt)",
1633 (uint64_t) hashmap_size(m
->dns_transactions
),
1634 (uint64_t) m
->n_transactions_total
);
1637 static int bus_property_get_cache_statistics(
1640 const char *interface
,
1641 const char *property
,
1642 sd_bus_message
*reply
,
1644 sd_bus_error
*error
) {
1646 uint64_t size
= 0, hit
= 0, miss
= 0;
1647 Manager
*m
= ASSERT_PTR(userdata
);
1651 LIST_FOREACH(scopes
, s
, m
->dns_scopes
) {
1652 size
+= dns_cache_size(&s
->cache
);
1653 hit
+= s
->cache
.n_hit
;
1654 miss
+= s
->cache
.n_miss
;
1657 return sd_bus_message_append(reply
, "(ttt)", size
, hit
, miss
);
1660 static int bus_property_get_dnssec_statistics(
1663 const char *interface
,
1664 const char *property
,
1665 sd_bus_message
*reply
,
1667 sd_bus_error
*error
) {
1669 Manager
*m
= ASSERT_PTR(userdata
);
1673 return sd_bus_message_append(reply
, "(tttt)",
1674 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_SECURE
],
1675 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INSECURE
],
1676 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_BOGUS
],
1677 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INDETERMINATE
]);
1680 static int bus_property_get_ntas(
1683 const char *interface
,
1684 const char *property
,
1685 sd_bus_message
*reply
,
1687 sd_bus_error
*error
) {
1689 Manager
*m
= ASSERT_PTR(userdata
);
1695 r
= sd_bus_message_open_container(reply
, 'a', "s");
1699 SET_FOREACH(domain
, m
->trust_anchor
.negative_by_name
) {
1700 r
= sd_bus_message_append(reply
, "s", domain
);
1705 return sd_bus_message_close_container(reply
);
1708 static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode
, dns_stub_listener_mode
, DnsStubListenerMode
);
1709 static BUS_DEFINE_PROPERTY_GET(bus_property_get_dnssec_supported
, "b", Manager
, manager_dnssec_supported
);
1710 static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dnssec_mode
, "s", Manager
, manager_get_dnssec_mode
, dnssec_mode_to_string
);
1711 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
);
1713 static int bus_property_get_resolv_conf_mode(
1716 const char *interface
,
1717 const char *property
,
1718 sd_bus_message
*reply
,
1720 sd_bus_error
*error
) {
1726 r
= resolv_conf_mode();
1728 log_warning_errno(r
, "Failed to test /etc/resolv.conf mode, ignoring: %m");
1729 return sd_bus_message_append(reply
, "s", NULL
);
1732 return sd_bus_message_append(reply
, "s", resolv_conf_mode_to_string(r
));
1735 static int bus_method_reset_statistics(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1736 Manager
*m
= ASSERT_PTR(userdata
);
1740 bus_client_log(message
, "statistics reset");
1742 LIST_FOREACH(scopes
, s
, m
->dns_scopes
)
1743 s
->cache
.n_hit
= s
->cache
.n_miss
= 0;
1745 m
->n_transactions_total
= 0;
1746 zero(m
->n_dnssec_verdict
);
1748 return sd_bus_reply_method_return(message
, NULL
);
1751 static int get_any_link(Manager
*m
, int ifindex
, Link
**ret
, sd_bus_error
*error
) {
1757 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
1759 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_LINK
, "Link %i not known", ifindex
);
1765 static int call_link_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
1773 r
= bus_message_read_ifindex(message
, error
, &ifindex
);
1777 r
= get_any_link(m
, ifindex
, &l
, error
);
1781 return handler(message
, l
, error
);
1784 static int bus_method_set_link_dns_servers(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1785 return call_link_method(userdata
, message
, bus_link_method_set_dns_servers
, error
);
1788 static int bus_method_set_link_dns_servers_ex(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1789 return call_link_method(userdata
, message
, bus_link_method_set_dns_servers_ex
, error
);
1792 static int bus_method_set_link_domains(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1793 return call_link_method(userdata
, message
, bus_link_method_set_domains
, error
);
1796 static int bus_method_set_link_default_route(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1797 return call_link_method(userdata
, message
, bus_link_method_set_default_route
, error
);
1800 static int bus_method_set_link_llmnr(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1801 return call_link_method(userdata
, message
, bus_link_method_set_llmnr
, error
);
1804 static int bus_method_set_link_mdns(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1805 return call_link_method(userdata
, message
, bus_link_method_set_mdns
, error
);
1808 static int bus_method_set_link_dns_over_tls(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1809 return call_link_method(userdata
, message
, bus_link_method_set_dns_over_tls
, error
);
1812 static int bus_method_set_link_dnssec(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1813 return call_link_method(userdata
, message
, bus_link_method_set_dnssec
, error
);
1816 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1817 return call_link_method(userdata
, message
, bus_link_method_set_dnssec_negative_trust_anchors
, error
);
1820 static int bus_method_revert_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1821 return call_link_method(userdata
, message
, bus_link_method_revert
, error
);
1824 static int bus_method_get_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1825 _cleanup_free_
char *p
= NULL
;
1826 Manager
*m
= ASSERT_PTR(userdata
);
1832 r
= bus_message_read_ifindex(message
, error
, &ifindex
);
1836 r
= get_any_link(m
, ifindex
, &l
, error
);
1840 p
= link_bus_path(l
);
1844 return sd_bus_reply_method_return(message
, "o", p
);
1847 static int bus_method_flush_caches(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1848 Manager
*m
= ASSERT_PTR(userdata
);
1852 bus_client_log(message
, "cache flush");
1854 manager_flush_caches(m
, LOG_INFO
);
1856 return sd_bus_reply_method_return(message
, NULL
);
1859 static int bus_method_reset_server_features(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1860 Manager
*m
= ASSERT_PTR(userdata
);
1864 bus_client_log(message
, "server feature reset");
1866 manager_reset_server_features(m
);
1868 return sd_bus_reply_method_return(message
, NULL
);
1871 static int dnssd_service_on_bus_track(sd_bus_track
*t
, void *userdata
) {
1872 DnssdService
*s
= ASSERT_PTR(userdata
);
1876 log_debug("Client of active request vanished, destroying DNS-SD service.");
1877 dnssd_service_free(s
);
1882 static int bus_method_register_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1883 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1884 _cleanup_(dnssd_service_freep
) DnssdService
*service
= NULL
;
1885 _cleanup_(sd_bus_track_unrefp
) sd_bus_track
*bus_track
= NULL
;
1886 const char *name
, *name_template
, *type
;
1887 _cleanup_free_
char *path
= NULL
;
1888 DnssdService
*s
= NULL
;
1889 Manager
*m
= ASSERT_PTR(userdata
);
1895 if (m
->mdns_support
!= RESOLVE_SUPPORT_YES
)
1896 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Support for MulticastDNS is disabled");
1898 service
= new0(DnssdService
, 1);
1902 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1906 r
= sd_bus_creds_get_euid(creds
, &euid
);
1909 service
->originator
= euid
;
1911 r
= sd_bus_message_read(message
, "sssqqq", &name
, &name_template
, &type
,
1912 &service
->port
, &service
->priority
,
1917 s
= hashmap_get(m
->dnssd_services
, name
);
1919 return sd_bus_error_setf(error
, BUS_ERROR_DNSSD_SERVICE_EXISTS
, "DNS-SD service '%s' exists already", name
);
1921 if (!dnssd_srv_type_is_valid(type
))
1922 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DNS-SD service type '%s' is invalid", type
);
1924 service
->name
= strdup(name
);
1928 service
->name_template
= strdup(name_template
);
1929 if (!service
->name_template
)
1932 service
->type
= strdup(type
);
1936 r
= dnssd_render_instance_name(m
, service
, NULL
);
1940 r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_ARRAY
, "a{say}");
1944 while ((r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_ARRAY
, "{say}")) > 0) {
1945 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
1946 DnsTxtItem
*last
= NULL
;
1948 txt_data
= new0(DnssdTxtData
, 1);
1952 while ((r
= sd_bus_message_enter_container(message
, SD_BUS_TYPE_DICT_ENTRY
, "say")) > 0) {
1958 r
= sd_bus_message_read(message
, "s", &key
);
1963 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Keys in DNS-SD TXT RRs can't be empty");
1965 if (!ascii_is_valid(key
))
1966 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "TXT key '%s' contains non-ASCII symbols", key
);
1968 r
= sd_bus_message_read_array(message
, 'y', &value
, &size
);
1972 r
= dnssd_txt_item_new_from_data(key
, value
, size
, &i
);
1976 LIST_INSERT_AFTER(items
, txt_data
->txts
, last
, i
);
1979 r
= sd_bus_message_exit_container(message
);
1987 r
= sd_bus_message_exit_container(message
);
1991 if (txt_data
->txts
) {
1992 LIST_PREPEND(items
, service
->txt_data_items
, txt_data
);
1999 r
= sd_bus_message_exit_container(message
);
2003 if (!service
->txt_data_items
) {
2004 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
2006 txt_data
= new0(DnssdTxtData
, 1);
2010 r
= dns_txt_item_new_empty(&txt_data
->txts
);
2014 LIST_PREPEND(items
, service
->txt_data_items
, txt_data
);
2018 r
= sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service
->name
, &path
);
2022 r
= bus_verify_polkit_async(message
, CAP_SYS_ADMIN
,
2023 "org.freedesktop.resolve1.register-service",
2024 NULL
, false, UID_INVALID
,
2025 &m
->polkit_registry
, error
);
2029 return 1; /* Polkit will call us back */
2031 r
= hashmap_ensure_put(&m
->dnssd_services
, &string_hash_ops
, service
->name
, service
);
2035 r
= sd_bus_track_new(sd_bus_message_get_bus(message
), &bus_track
, dnssd_service_on_bus_track
, service
);
2039 r
= sd_bus_track_add_sender(bus_track
, message
);
2043 service
->manager
= m
;
2047 manager_refresh_rrs(m
);
2049 return sd_bus_reply_method_return(message
, "o", path
);
2052 static int call_dnssd_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
2053 _cleanup_free_
char *name
= NULL
;
2054 DnssdService
*s
= NULL
;
2062 r
= sd_bus_message_read(message
, "o", &path
);
2066 r
= sd_bus_path_decode(path
, "/org/freedesktop/resolve1/dnssd", &name
);
2068 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_DNSSD_SERVICE
, "DNS-SD service with object path '%s' does not exist", path
);
2072 s
= hashmap_get(m
->dnssd_services
, name
);
2074 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_DNSSD_SERVICE
, "DNS-SD service '%s' not known", name
);
2076 return handler(message
, s
, error
);
2079 static int bus_method_unregister_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2080 Manager
*m
= ASSERT_PTR(userdata
);
2084 return call_dnssd_method(m
, message
, bus_dnssd_method_unregister
, error
);
2087 static const sd_bus_vtable resolve_vtable
[] = {
2088 SD_BUS_VTABLE_START(0),
2089 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL
, offsetof(Manager
, llmnr_hostname
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2090 SD_BUS_PROPERTY("LLMNR", "s", bus_property_get_resolve_support
, offsetof(Manager
, llmnr_support
), 0),
2091 SD_BUS_PROPERTY("MulticastDNS", "s", bus_property_get_resolve_support
, offsetof(Manager
, mdns_support
), 0),
2092 SD_BUS_PROPERTY("DNSOverTLS", "s", bus_property_get_dns_over_tls_mode
, 0, 0),
2093 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2094 SD_BUS_PROPERTY("DNSEx", "a(iiayqs)", bus_property_get_dns_servers_ex
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2095 SD_BUS_PROPERTY("FallbackDNS", "a(iiay)", bus_property_get_fallback_dns_servers
, offsetof(Manager
, fallback_dns_servers
), SD_BUS_VTABLE_PROPERTY_CONST
),
2096 SD_BUS_PROPERTY("FallbackDNSEx", "a(iiayqs)", bus_property_get_fallback_dns_servers_ex
, offsetof(Manager
, fallback_dns_servers
), SD_BUS_VTABLE_PROPERTY_CONST
),
2097 SD_BUS_PROPERTY("CurrentDNSServer", "(iiay)", bus_property_get_current_dns_server
, offsetof(Manager
, current_dns_server
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2098 SD_BUS_PROPERTY("CurrentDNSServerEx", "(iiayqs)", bus_property_get_current_dns_server_ex
, offsetof(Manager
, current_dns_server
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2099 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains
, 0, 0),
2100 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics
, 0, 0),
2101 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics
, 0, 0),
2102 SD_BUS_PROPERTY("DNSSEC", "s", bus_property_get_dnssec_mode
, 0, 0),
2103 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics
, 0, 0),
2104 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported
, 0, 0),
2105 SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_ntas
, 0, 0),
2106 SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode
, offsetof(Manager
, dns_stub_listener_mode
), 0),
2107 SD_BUS_PROPERTY("ResolvConfMode", "s", bus_property_get_resolv_conf_mode
, 0, 0),
2109 SD_BUS_METHOD_WITH_ARGS("ResolveHostname",
2110 SD_BUS_ARGS("i", ifindex
, "s", name
, "i", family
, "t", flags
),
2111 SD_BUS_RESULT("a(iiay)", addresses
, "s", canonical
, "t", flags
),
2112 bus_method_resolve_hostname
,
2113 SD_BUS_VTABLE_UNPRIVILEGED
),
2114 SD_BUS_METHOD_WITH_ARGS("ResolveAddress",
2115 SD_BUS_ARGS("i", ifindex
, "i", family
, "ay", address
, "t", flags
),
2116 SD_BUS_RESULT("a(is)", names
, "t", flags
),
2117 bus_method_resolve_address
,
2118 SD_BUS_VTABLE_UNPRIVILEGED
),
2119 SD_BUS_METHOD_WITH_ARGS("ResolveRecord",
2120 SD_BUS_ARGS("i", ifindex
, "s", name
, "q", class, "q", type
, "t", flags
),
2121 SD_BUS_RESULT("a(iqqay)", records
, "t", flags
),
2122 bus_method_resolve_record
,
2123 SD_BUS_VTABLE_UNPRIVILEGED
),
2124 SD_BUS_METHOD_WITH_ARGS("ResolveService",
2125 SD_BUS_ARGS("i", ifindex
,
2131 SD_BUS_RESULT("a(qqqsa(iiay)s)", srv_data
,
2133 "s", canonical_name
,
2134 "s", canonical_type
,
2135 "s", canonical_domain
,
2137 bus_method_resolve_service
,
2138 SD_BUS_VTABLE_UNPRIVILEGED
),
2139 SD_BUS_METHOD_WITH_ARGS("GetLink",
2140 SD_BUS_ARGS("i", ifindex
),
2141 SD_BUS_RESULT("o", path
),
2142 bus_method_get_link
,
2143 SD_BUS_VTABLE_UNPRIVILEGED
),
2144 SD_BUS_METHOD_WITH_ARGS("SetLinkDNS",
2145 SD_BUS_ARGS("i", ifindex
, "a(iay)", addresses
),
2147 bus_method_set_link_dns_servers
,
2148 SD_BUS_VTABLE_UNPRIVILEGED
),
2149 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSEx",
2150 SD_BUS_ARGS("i", ifindex
, "a(iayqs)", addresses
),
2152 bus_method_set_link_dns_servers_ex
,
2153 SD_BUS_VTABLE_UNPRIVILEGED
),
2154 SD_BUS_METHOD_WITH_ARGS("SetLinkDomains",
2155 SD_BUS_ARGS("i", ifindex
, "a(sb)", domains
),
2157 bus_method_set_link_domains
,
2158 SD_BUS_VTABLE_UNPRIVILEGED
),
2159 SD_BUS_METHOD_WITH_ARGS("SetLinkDefaultRoute",
2160 SD_BUS_ARGS("i", ifindex
, "b", enable
),
2162 bus_method_set_link_default_route
,
2163 SD_BUS_VTABLE_UNPRIVILEGED
),
2164 SD_BUS_METHOD_WITH_ARGS("SetLinkLLMNR",
2165 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2167 bus_method_set_link_llmnr
,
2168 SD_BUS_VTABLE_UNPRIVILEGED
),
2169 SD_BUS_METHOD_WITH_ARGS("SetLinkMulticastDNS",
2170 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2172 bus_method_set_link_mdns
,
2173 SD_BUS_VTABLE_UNPRIVILEGED
),
2174 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSOverTLS",
2175 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2177 bus_method_set_link_dns_over_tls
,
2178 SD_BUS_VTABLE_UNPRIVILEGED
),
2179 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSEC",
2180 SD_BUS_ARGS("i", ifindex
, "s", mode
),
2182 bus_method_set_link_dnssec
,
2183 SD_BUS_VTABLE_UNPRIVILEGED
),
2184 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSECNegativeTrustAnchors",
2185 SD_BUS_ARGS("i", ifindex
, "as", names
),
2187 bus_method_set_link_dnssec_negative_trust_anchors
,
2188 SD_BUS_VTABLE_UNPRIVILEGED
),
2189 SD_BUS_METHOD_WITH_ARGS("RevertLink",
2190 SD_BUS_ARGS("i", ifindex
),
2192 bus_method_revert_link
,
2193 SD_BUS_VTABLE_UNPRIVILEGED
),
2194 SD_BUS_METHOD_WITH_ARGS("RegisterService",
2195 SD_BUS_ARGS("s", name
,
2199 "q", service_priority
,
2200 "q", service_weight
,
2201 "aa{say}", txt_datas
),
2202 SD_BUS_RESULT("o", service_path
),
2203 bus_method_register_service
,
2204 SD_BUS_VTABLE_UNPRIVILEGED
),
2205 SD_BUS_METHOD_WITH_ARGS("UnregisterService",
2206 SD_BUS_ARGS("o", service_path
),
2208 bus_method_unregister_service
,
2209 SD_BUS_VTABLE_UNPRIVILEGED
),
2210 SD_BUS_METHOD_WITH_ARGS("ResetStatistics",
2213 bus_method_reset_statistics
,
2214 SD_BUS_VTABLE_UNPRIVILEGED
),
2215 SD_BUS_METHOD_WITH_ARGS("FlushCaches",
2218 bus_method_flush_caches
,
2219 SD_BUS_VTABLE_UNPRIVILEGED
),
2220 SD_BUS_METHOD_WITH_ARGS("ResetServerFeatures",
2223 bus_method_reset_server_features
,
2224 SD_BUS_VTABLE_UNPRIVILEGED
),
2229 const BusObjectImplementation manager_object
= {
2230 "/org/freedesktop/resolve1",
2231 "org.freedesktop.resolve1.Manager",
2232 .vtables
= BUS_VTABLES(resolve_vtable
),
2233 .children
= BUS_IMPLEMENTATIONS(&link_object
,
2237 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
2238 Manager
*m
= ASSERT_PTR(userdata
);
2243 r
= sd_bus_message_read(message
, "b", &b
);
2245 bus_log_parse_error(r
);
2252 log_debug("Coming back from suspend, verifying all RRs...");
2254 manager_verify_all(m
);
2258 int manager_connect_bus(Manager
*m
) {
2266 r
= bus_open_system_watch_bind_with_description(&m
->bus
, "bus-api-resolve");
2268 return log_error_errno(r
, "Failed to connect to system bus: %m");
2270 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
2274 r
= bus_log_control_api_register(m
->bus
);
2278 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.resolve1", 0, NULL
, NULL
);
2280 return log_error_errno(r
, "Failed to request name: %m");
2282 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
2284 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
2286 r
= bus_match_signal_async(
2291 match_prepare_for_sleep
,
2295 log_warning_errno(r
, "Failed to request match for PrepareForSleep, ignoring: %m");
2300 int _manager_send_changed(Manager
*manager
, const char *property
, ...) {
2303 if (sd_bus_is_ready(manager
->bus
) <= 0)
2306 char **l
= strv_from_stdarg_alloca(property
);
2308 int r
= sd_bus_emit_properties_changed_strv(
2310 "/org/freedesktop/resolve1",
2311 "org.freedesktop.resolve1.Manager",
2314 log_notice_errno(r
, "Failed to emit notification about changed property %s: %m", property
);