2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
21 #include "bus-common-errors.h"
23 #include "dns-domain.h"
24 #include "resolved-bus.h"
25 #include "resolved-def.h"
26 #include "resolved-dns-synthesize.h"
27 #include "resolved-link-bus.h"
29 static int reply_query_state(DnsQuery
*q
) {
33 case DNS_TRANSACTION_NO_SERVERS
:
34 return sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NO_NAME_SERVERS
, "No appropriate name servers or networks for name found");
36 case DNS_TRANSACTION_TIMEOUT
:
37 return sd_bus_reply_method_errorf(q
->request
, SD_BUS_ERROR_TIMEOUT
, "Query timed out");
39 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
:
40 return sd_bus_reply_method_errorf(q
->request
, SD_BUS_ERROR_TIMEOUT
, "All attempts to contact name servers or networks failed");
42 case DNS_TRANSACTION_INVALID_REPLY
:
43 return sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_INVALID_REPLY
, "Received invalid reply");
45 case DNS_TRANSACTION_ERRNO
:
46 return sd_bus_reply_method_errnof(q
->request
, q
->answer_errno
, "Lookup failed due to system error: %m");
48 case DNS_TRANSACTION_ABORTED
:
49 return sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_ABORTED
, "Query aborted");
51 case DNS_TRANSACTION_DNSSEC_FAILED
:
52 return sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_DNSSEC_FAILED
, "DNSSEC validation failed: %s",
53 dnssec_result_to_string(q
->answer_dnssec_result
));
55 case DNS_TRANSACTION_NO_TRUST_ANCHOR
:
56 return sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NO_TRUST_ANCHOR
, "No suitable trust anchor known");
58 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
:
59 return sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_RR_TYPE_UNSUPPORTED
, "Server does not support requested resource record type");
61 case DNS_TRANSACTION_NETWORK_DOWN
:
62 return sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NETWORK_DOWN
, "Network is down");
64 case DNS_TRANSACTION_NOT_FOUND
:
65 /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
66 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
67 return sd_bus_reply_method_errorf(q
->request
, _BUS_ERROR_DNS
"NXDOMAIN", "'%s' not found", dns_query_string(q
));
69 case DNS_TRANSACTION_RCODE_FAILURE
: {
70 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
72 if (q
->answer_rcode
== DNS_RCODE_NXDOMAIN
)
73 sd_bus_error_setf(&error
, _BUS_ERROR_DNS
"NXDOMAIN", "'%s' not found", dns_query_string(q
));
76 char p
[DECIMAL_STR_MAX(q
->answer_rcode
)];
78 rc
= dns_rcode_to_string(q
->answer_rcode
);
80 sprintf(p
, "%i", q
->answer_rcode
);
84 n
= strjoina(_BUS_ERROR_DNS
, rc
);
85 sd_bus_error_setf(&error
, n
, "Could not resolve '%s', server or network returned error %s", dns_query_string(q
), rc
);
88 return sd_bus_reply_method_error(q
->request
, &error
);
91 case DNS_TRANSACTION_NULL
:
92 case DNS_TRANSACTION_PENDING
:
93 case DNS_TRANSACTION_VALIDATING
:
94 case DNS_TRANSACTION_SUCCESS
:
96 assert_not_reached("Impossible state");
100 static int append_address(sd_bus_message
*reply
, DnsResourceRecord
*rr
, int ifindex
) {
106 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
110 r
= sd_bus_message_append(reply
, "i", ifindex
);
114 if (rr
->key
->type
== DNS_TYPE_A
) {
115 r
= sd_bus_message_append(reply
, "i", AF_INET
);
119 r
= sd_bus_message_append_array(reply
, 'y', &rr
->a
.in_addr
, sizeof(struct in_addr
));
121 } else if (rr
->key
->type
== DNS_TYPE_AAAA
) {
122 r
= sd_bus_message_append(reply
, "i", AF_INET6
);
126 r
= sd_bus_message_append_array(reply
, 'y', &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
));
128 return -EAFNOSUPPORT
;
133 r
= sd_bus_message_close_container(reply
);
140 static void bus_method_resolve_hostname_complete(DnsQuery
*q
) {
141 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
142 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
143 _cleanup_free_
char *normalized
= NULL
;
144 DnsResourceRecord
*rr
;
150 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
151 r
= reply_query_state(q
);
155 r
= dns_query_process_cname(q
);
157 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
162 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
165 r
= sd_bus_message_new_method_return(q
->request
, &reply
);
169 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
173 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
174 DnsQuestion
*question
;
176 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
178 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
184 r
= append_address(reply
, rr
, ifindex
);
189 canonical
= dns_resource_record_ref(rr
);
195 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
199 r
= sd_bus_message_close_container(reply
);
203 /* The key names are not necessarily normalized, make sure that they are when we return them to our bus
205 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), &normalized
);
209 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
211 r
= sd_bus_message_append(
214 SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, q
->answer_authenticated
));
218 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
222 log_error_errno(r
, "Failed to send hostname reply: %m");
223 sd_bus_reply_method_errno(q
->request
, r
, NULL
);
229 static int check_ifindex_flags(int ifindex
, uint64_t *flags
, uint64_t ok
, sd_bus_error
*error
) {
233 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
235 if (*flags
& ~(SD_RESOLVED_PROTOCOLS_ALL
|SD_RESOLVED_NO_CNAME
|ok
))
236 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid flags parameter");
238 if ((*flags
& SD_RESOLVED_PROTOCOLS_ALL
) == 0) /* If no protocol is enabled, enable all */
239 *flags
|= SD_RESOLVED_PROTOCOLS_ALL
;
244 static int parse_as_address(sd_bus_message
*m
, int ifindex
, const char *hostname
, int family
, uint64_t flags
) {
245 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
246 _cleanup_free_
char *canonical
= NULL
;
247 union in_addr_union parsed
;
250 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
251 * let's not attempt to look it up. */
253 r
= in_addr_from_string_auto(hostname
, &ff
, &parsed
);
254 if (r
< 0) /* not an address */
257 if (family
!= AF_UNSPEC
&& ff
!= family
)
258 return sd_bus_reply_method_errorf(m
, BUS_ERROR_NO_SUCH_RR
, "The specified address is not of the requested family.");
260 r
= sd_bus_message_new_method_return(m
, &reply
);
264 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
268 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
272 r
= sd_bus_message_append(reply
, "ii", ifindex
, ff
);
276 r
= sd_bus_message_append_array(reply
, 'y', &parsed
, FAMILY_ADDRESS_SIZE(ff
));
280 r
= sd_bus_message_close_container(reply
);
284 r
= sd_bus_message_close_container(reply
);
288 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
289 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
290 * omissions are always done the same way). */
291 r
= in_addr_to_string(ff
, &parsed
, &canonical
);
295 r
= sd_bus_message_append(reply
, "st", canonical
,
296 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags
), ff
, true));
300 return sd_bus_send(sd_bus_message_get_bus(m
), reply
, NULL
);
303 static int bus_method_resolve_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
304 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
305 Manager
*m
= userdata
;
306 const char *hostname
;
315 assert_cc(sizeof(int) == sizeof(int32_t));
317 r
= sd_bus_message_read(message
, "isit", &ifindex
, &hostname
, &family
, &flags
);
321 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
322 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
324 r
= check_ifindex_flags(ifindex
, &flags
, SD_RESOLVED_NO_SEARCH
, error
);
328 r
= parse_as_address(message
, ifindex
, hostname
, family
, flags
);
332 r
= dns_name_is_valid(hostname
);
336 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid hostname '%s'", hostname
);
338 r
= dns_question_new_address(&question_utf8
, family
, hostname
, false);
342 r
= dns_question_new_address(&question_idna
, family
, hostname
, true);
346 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
, ifindex
, flags
);
350 q
->request
= sd_bus_message_ref(message
);
351 q
->request_family
= family
;
352 q
->complete
= bus_method_resolve_hostname_complete
;
353 q
->suppress_unroutable_family
= family
== AF_UNSPEC
;
355 r
= dns_query_bus_track(q
, message
);
370 static void bus_method_resolve_address_complete(DnsQuery
*q
) {
371 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
372 DnsQuestion
*question
;
373 DnsResourceRecord
*rr
;
379 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
380 r
= reply_query_state(q
);
384 r
= dns_query_process_cname(q
);
386 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
391 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
394 r
= sd_bus_message_new_method_return(q
->request
, &reply
);
398 r
= sd_bus_message_open_container(reply
, 'a', "(is)");
402 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
404 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
405 _cleanup_free_
char *normalized
= NULL
;
407 r
= dns_question_matches_rr(question
, rr
, NULL
);
413 r
= dns_name_normalize(rr
->ptr
.name
, &normalized
);
417 r
= sd_bus_message_append(reply
, "(is)", ifindex
, normalized
);
425 _cleanup_free_
char *ip
= NULL
;
427 (void) in_addr_to_string(q
->request_family
, &q
->request_address
, &ip
);
428 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NO_SUCH_RR
,
429 "Address '%s' does not have any RR of requested type", strnull(ip
));
433 r
= sd_bus_message_close_container(reply
);
437 r
= sd_bus_message_append(reply
, "t", SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, q
->answer_authenticated
));
441 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
445 log_error_errno(r
, "Failed to send address reply: %m");
446 sd_bus_reply_method_errno(q
->request
, r
, NULL
);
452 static int bus_method_resolve_address(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
453 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
454 Manager
*m
= userdata
;
465 assert_cc(sizeof(int) == sizeof(int32_t));
467 r
= sd_bus_message_read(message
, "ii", &ifindex
, &family
);
471 if (!IN_SET(family
, AF_INET
, AF_INET6
))
472 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
474 r
= sd_bus_message_read_array(message
, 'y', &d
, &sz
);
478 if (sz
!= FAMILY_ADDRESS_SIZE(family
))
479 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid address size");
481 r
= sd_bus_message_read(message
, "t", &flags
);
485 r
= check_ifindex_flags(ifindex
, &flags
, 0, error
);
489 r
= dns_question_new_reverse(&question
, family
, d
);
493 r
= dns_query_new(m
, &q
, question
, question
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
497 q
->request
= sd_bus_message_ref(message
);
498 q
->request_family
= family
;
499 memcpy(&q
->request_address
, d
, sz
);
500 q
->complete
= bus_method_resolve_address_complete
;
502 r
= dns_query_bus_track(q
, message
);
517 static int bus_message_append_rr(sd_bus_message
*m
, DnsResourceRecord
*rr
, int ifindex
) {
523 r
= sd_bus_message_open_container(m
, 'r', "iqqay");
527 r
= sd_bus_message_append(m
, "iqq",
534 r
= dns_resource_record_to_wire_format(rr
, false);
538 r
= sd_bus_message_append_array(m
, 'y', rr
->wire_format
, rr
->wire_format_size
);
542 return sd_bus_message_close_container(m
);
545 static void bus_method_resolve_record_complete(DnsQuery
*q
) {
546 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
547 DnsResourceRecord
*rr
;
548 DnsQuestion
*question
;
555 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
556 r
= reply_query_state(q
);
560 r
= dns_query_process_cname(q
);
562 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
567 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
570 r
= sd_bus_message_new_method_return(q
->request
, &reply
);
574 r
= sd_bus_message_open_container(reply
, 'a', "(iqqay)");
578 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
580 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
581 r
= dns_question_matches_rr(question
, rr
, NULL
);
587 r
= bus_message_append_rr(reply
, rr
, ifindex
);
595 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NO_SUCH_RR
, "Name '%s' does not have any RR of the requested type", dns_query_string(q
));
599 r
= sd_bus_message_close_container(reply
);
603 r
= sd_bus_message_append(reply
, "t", SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, q
->answer_authenticated
));
607 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
611 log_error_errno(r
, "Failed to send record reply: %m");
612 sd_bus_reply_method_errno(q
->request
, r
, NULL
);
618 static int bus_method_resolve_record(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
619 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
620 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
621 Manager
*m
= userdata
;
622 uint16_t class, type
;
631 assert_cc(sizeof(int) == sizeof(int32_t));
633 r
= sd_bus_message_read(message
, "isqqt", &ifindex
, &name
, &class, &type
, &flags
);
637 r
= dns_name_is_valid(name
);
641 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid name '%s'", name
);
643 if (!dns_type_is_valid_query(type
))
644 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified resource record type %" PRIu16
" may not be used in a query.", type
);
645 if (dns_type_is_obsolete(type
))
646 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Specified DNS resource record type %" PRIu16
" is obsolete.", type
);
648 r
= check_ifindex_flags(ifindex
, &flags
, 0, error
);
652 question
= dns_question_new(1);
656 key
= dns_resource_key_new(class, type
, name
);
660 r
= dns_question_add(question
, key
);
664 r
= dns_query_new(m
, &q
, question
, question
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
668 q
->request
= sd_bus_message_ref(message
);
669 q
->complete
= bus_method_resolve_record_complete
;
671 r
= dns_query_bus_track(q
, message
);
686 static int append_srv(DnsQuery
*q
, sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
687 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
688 _cleanup_free_
char *normalized
= NULL
;
697 if (rr
->key
->type
!= DNS_TYPE_SRV
)
700 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
701 /* First, let's see if we could find an appropriate A or AAAA
702 * record for the SRV record */
703 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
704 DnsResourceRecord
*zz
;
705 DnsQuestion
*question
;
707 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
709 if (aux
->auxiliary_result
!= 0)
712 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
714 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
720 DNS_ANSWER_FOREACH(zz
, aux
->answer
) {
722 r
= dns_question_matches_rr(question
, zz
, NULL
);
728 canonical
= dns_resource_record_ref(zz
);
736 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
741 r
= sd_bus_message_open_container(reply
, 'r', "qqqsa(iiay)s");
745 r
= dns_name_normalize(rr
->srv
.name
, &normalized
);
749 r
= sd_bus_message_append(
752 rr
->srv
.priority
, rr
->srv
.weight
, rr
->srv
.port
, normalized
);
756 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
760 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
761 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
762 DnsResourceRecord
*zz
;
763 DnsQuestion
*question
;
766 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
768 if (aux
->auxiliary_result
!= 0)
771 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
773 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
779 DNS_ANSWER_FOREACH_IFINDEX(zz
, ifindex
, aux
->answer
) {
781 r
= dns_question_matches_rr(question
, zz
, NULL
);
787 r
= append_address(reply
, zz
, ifindex
);
794 r
= sd_bus_message_close_container(reply
);
799 normalized
= mfree(normalized
);
801 r
= dns_name_normalize(dns_resource_key_name(canonical
->key
), &normalized
);
806 /* Note that above we appended the hostname as encoded in the
807 * SRV, and here the canonical hostname this maps to. */
808 r
= sd_bus_message_append(reply
, "s", normalized
);
812 r
= sd_bus_message_close_container(reply
);
819 static int append_txt(sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
827 if (rr
->key
->type
!= DNS_TYPE_TXT
)
830 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
835 r
= sd_bus_message_append_array(reply
, 'y', i
->data
, i
->length
);
843 static void resolve_service_all_complete(DnsQuery
*q
) {
844 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
845 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
846 _cleanup_free_
char *name
= NULL
, *type
= NULL
, *domain
= NULL
;
847 DnsQuestion
*question
;
848 DnsResourceRecord
*rr
;
855 if (q
->block_all_complete
> 0)
858 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
859 DnsQuery
*bad
= NULL
;
860 bool have_success
= false;
862 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
864 switch (aux
->state
) {
866 case DNS_TRANSACTION_PENDING
:
867 /* If an auxiliary query is still pending, let's wait */
870 case DNS_TRANSACTION_SUCCESS
:
871 if (aux
->auxiliary_result
== 0)
884 /* We can only return one error, hence pick the last error we encountered */
888 if (bad
->state
== DNS_TRANSACTION_SUCCESS
) {
889 assert(bad
->auxiliary_result
!= 0);
891 if (bad
->auxiliary_result
== -ELOOP
) {
892 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad
));
896 r
= bad
->auxiliary_result
;
900 r
= reply_query_state(bad
);
905 r
= sd_bus_message_new_method_return(q
->request
, &reply
);
909 r
= sd_bus_message_open_container(reply
, 'a', "(qqqsa(iiay)s)");
913 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
914 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
915 r
= dns_question_matches_rr(question
, rr
, NULL
);
921 r
= append_srv(q
, reply
, rr
);
924 if (r
== 0) /* not an SRV record */
928 canonical
= dns_resource_record_ref(rr
);
934 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
938 r
= sd_bus_message_close_container(reply
);
942 r
= sd_bus_message_open_container(reply
, 'a', "ay");
946 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
947 r
= dns_question_matches_rr(question
, rr
, NULL
);
953 r
= append_txt(reply
, rr
);
958 r
= sd_bus_message_close_container(reply
);
963 r
= dns_service_split(dns_resource_key_name(canonical
->key
), &name
, &type
, &domain
);
967 r
= sd_bus_message_append(
971 SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, q
->answer_authenticated
));
975 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
979 log_error_errno(r
, "Failed to send service reply: %m");
980 sd_bus_reply_method_errno(q
->request
, r
, NULL
);
986 static void resolve_service_hostname_complete(DnsQuery
*q
) {
990 assert(q
->auxiliary_for
);
992 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
993 resolve_service_all_complete(q
->auxiliary_for
);
997 r
= dns_query_process_cname(q
);
998 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
1001 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1002 q
->auxiliary_result
= r
;
1003 resolve_service_all_complete(q
->auxiliary_for
);
1006 static int resolve_service_hostname(DnsQuery
*q
, DnsResourceRecord
*rr
, int ifindex
) {
1007 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
1014 assert(rr
->key
->type
== DNS_TYPE_SRV
);
1016 /* OK, we found an SRV record for the service. Let's resolve
1017 * the hostname included in it */
1019 r
= dns_question_new_address(&question
, q
->request_family
, rr
->srv
.name
, false);
1023 r
= dns_query_new(q
->manager
, &aux
, question
, question
, ifindex
, q
->flags
|SD_RESOLVED_NO_SEARCH
);
1027 aux
->request_family
= q
->request_family
;
1028 aux
->complete
= resolve_service_hostname_complete
;
1030 r
= dns_query_make_auxiliary(aux
, q
);
1032 /* Too many auxiliary lookups? If so, don't complain,
1033 * let's just not add this one, we already have more
1036 dns_query_free(aux
);
1042 /* Note that auxiliary queries do not track the original bus
1043 * client, only the primary request does that. */
1045 r
= dns_query_go(aux
);
1052 dns_query_free(aux
);
1056 static void bus_method_resolve_service_complete(DnsQuery
*q
) {
1057 bool has_root_domain
= false;
1058 DnsResourceRecord
*rr
;
1059 DnsQuestion
*question
;
1065 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1066 r
= reply_query_state(q
);
1070 r
= dns_query_process_cname(q
);
1072 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_CNAME_LOOP
, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q
));
1077 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
1080 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1082 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
1083 r
= dns_question_matches_rr(question
, rr
, NULL
);
1089 if (rr
->key
->type
!= DNS_TYPE_SRV
)
1092 if (dns_name_is_root(rr
->srv
.name
)) {
1093 has_root_domain
= true;
1097 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
1098 q
->block_all_complete
++;
1099 r
= resolve_service_hostname(q
, rr
, ifindex
);
1100 q
->block_all_complete
--;
1109 if (has_root_domain
&& found
<= 0) {
1110 /* If there's exactly one SRV RR and it uses
1111 * the root domain as host name, then the
1112 * service is explicitly not offered on the
1113 * domain. Report this as a recognizable
1114 * error. See RFC 2782, Section "Usage
1116 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NO_SUCH_SERVICE
, "'%s' does not provide the requested service", dns_query_string(q
));
1121 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NO_SUCH_RR
, "'%s' does not have any RR of the requested type", dns_query_string(q
));
1125 /* Maybe we are already finished? check now... */
1126 resolve_service_all_complete(q
);
1131 log_error_errno(r
, "Failed to send service reply: %m");
1132 sd_bus_reply_method_errno(q
->request
, r
, NULL
);
1138 static int bus_method_resolve_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1139 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
1140 const char *name
, *type
, *domain
;
1141 Manager
*m
= userdata
;
1142 int family
, ifindex
;
1150 assert_cc(sizeof(int) == sizeof(int32_t));
1152 r
= sd_bus_message_read(message
, "isssit", &ifindex
, &name
, &type
, &domain
, &family
, &flags
);
1156 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
1157 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
1161 else if (!dns_service_name_is_valid(name
))
1162 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid service name '%s'", name
);
1166 else if (!dns_srv_type_is_valid(type
))
1167 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid SRV service type '%s'", type
);
1169 r
= dns_name_is_valid(domain
);
1173 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid domain '%s'", domain
);
1176 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Service name cannot be specified without service type.");
1178 r
= check_ifindex_flags(ifindex
, &flags
, SD_RESOLVED_NO_TXT
|SD_RESOLVED_NO_ADDRESS
, error
);
1182 r
= dns_question_new_service(&question_utf8
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), false);
1186 r
= dns_question_new_service(&question_idna
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), true);
1190 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
1194 q
->request
= sd_bus_message_ref(message
);
1195 q
->request_family
= family
;
1196 q
->complete
= bus_method_resolve_service_complete
;
1198 r
= dns_query_bus_track(q
, message
);
1202 r
= dns_query_go(q
);
1213 int bus_dns_server_append(sd_bus_message
*reply
, DnsServer
*s
, bool with_ifindex
) {
1219 r
= sd_bus_message_open_container(reply
, 'r', with_ifindex
? "iiay" : "iay");
1224 r
= sd_bus_message_append(reply
, "i", dns_server_ifindex(s
));
1229 r
= sd_bus_message_append(reply
, "i", s
->family
);
1233 r
= sd_bus_message_append_array(reply
, 'y', &s
->address
, FAMILY_ADDRESS_SIZE(s
->family
));
1237 return sd_bus_message_close_container(reply
);
1240 static int bus_property_get_dns_servers(
1243 const char *interface
,
1244 const char *property
,
1245 sd_bus_message
*reply
,
1247 sd_bus_error
*error
) {
1249 Manager
*m
= userdata
;
1259 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
1263 LIST_FOREACH(servers
, s
, m
->dns_servers
) {
1264 r
= bus_dns_server_append(reply
, s
, true);
1271 HASHMAP_FOREACH(l
, m
->links
, i
) {
1272 LIST_FOREACH(servers
, s
, l
->dns_servers
) {
1273 r
= bus_dns_server_append(reply
, s
, true);
1281 LIST_FOREACH(servers
, s
, m
->fallback_dns_servers
) {
1282 r
= bus_dns_server_append(reply
, s
, true);
1288 return sd_bus_message_close_container(reply
);
1291 static int bus_property_get_domains(
1294 const char *interface
,
1295 const char *property
,
1296 sd_bus_message
*reply
,
1298 sd_bus_error
*error
) {
1300 Manager
*m
= userdata
;
1309 r
= sd_bus_message_open_container(reply
, 'a', "(isb)");
1313 LIST_FOREACH(domains
, d
, m
->search_domains
) {
1314 r
= sd_bus_message_append(reply
, "(isb)", 0, d
->name
, d
->route_only
);
1319 HASHMAP_FOREACH(l
, m
->links
, i
) {
1320 LIST_FOREACH(domains
, d
, l
->search_domains
) {
1321 r
= sd_bus_message_append(reply
, "(isb)", l
->ifindex
, d
->name
, d
->route_only
);
1327 return sd_bus_message_close_container(reply
);
1330 static int bus_property_get_transaction_statistics(
1333 const char *interface
,
1334 const char *property
,
1335 sd_bus_message
*reply
,
1337 sd_bus_error
*error
) {
1339 Manager
*m
= userdata
;
1344 return sd_bus_message_append(reply
, "(tt)",
1345 (uint64_t) hashmap_size(m
->dns_transactions
),
1346 (uint64_t) m
->n_transactions_total
);
1349 static int bus_property_get_cache_statistics(
1352 const char *interface
,
1353 const char *property
,
1354 sd_bus_message
*reply
,
1356 sd_bus_error
*error
) {
1358 uint64_t size
= 0, hit
= 0, miss
= 0;
1359 Manager
*m
= userdata
;
1365 LIST_FOREACH(scopes
, s
, m
->dns_scopes
) {
1366 size
+= dns_cache_size(&s
->cache
);
1367 hit
+= s
->cache
.n_hit
;
1368 miss
+= s
->cache
.n_miss
;
1371 return sd_bus_message_append(reply
, "(ttt)", size
, hit
, miss
);
1374 static int bus_property_get_dnssec_statistics(
1377 const char *interface
,
1378 const char *property
,
1379 sd_bus_message
*reply
,
1381 sd_bus_error
*error
) {
1383 Manager
*m
= userdata
;
1388 return sd_bus_message_append(reply
, "(tttt)",
1389 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_SECURE
],
1390 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INSECURE
],
1391 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_BOGUS
],
1392 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INDETERMINATE
]);
1395 static int bus_property_get_dnssec_supported(
1398 const char *interface
,
1399 const char *property
,
1400 sd_bus_message
*reply
,
1402 sd_bus_error
*error
) {
1404 Manager
*m
= userdata
;
1409 return sd_bus_message_append(reply
, "b", manager_dnssec_supported(m
));
1412 static int bus_method_reset_statistics(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1413 Manager
*m
= userdata
;
1419 LIST_FOREACH(scopes
, s
, m
->dns_scopes
)
1420 s
->cache
.n_hit
= s
->cache
.n_miss
= 0;
1422 m
->n_transactions_total
= 0;
1423 zero(m
->n_dnssec_verdict
);
1425 return sd_bus_reply_method_return(message
, NULL
);
1428 static int get_any_link(Manager
*m
, int ifindex
, Link
**ret
, sd_bus_error
*error
) {
1435 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
1437 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
1439 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_LINK
, "Link %i not known", ifindex
);
1445 static int get_unmanaged_link(Manager
*m
, int ifindex
, Link
**ret
, sd_bus_error
*error
) {
1452 r
= get_any_link(m
, ifindex
, &l
, error
);
1456 if (l
->flags
& IFF_LOOPBACK
)
1457 return sd_bus_error_setf(error
, BUS_ERROR_LINK_BUSY
, "Link %s is loopback device.", l
->name
);
1459 return sd_bus_error_setf(error
, BUS_ERROR_LINK_BUSY
, "Link %s is managed.", l
->name
);
1465 static int call_link_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
1473 assert_cc(sizeof(int) == sizeof(int32_t));
1474 r
= sd_bus_message_read(message
, "i", &ifindex
);
1478 r
= get_unmanaged_link(m
, ifindex
, &l
, error
);
1482 return handler(message
, l
, error
);
1485 static int bus_method_set_link_dns_servers(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1486 return call_link_method(userdata
, message
, bus_link_method_set_dns_servers
, error
);
1489 static int bus_method_set_link_domains(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1490 return call_link_method(userdata
, message
, bus_link_method_set_domains
, error
);
1493 static int bus_method_set_link_llmnr(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1494 return call_link_method(userdata
, message
, bus_link_method_set_llmnr
, error
);
1497 static int bus_method_set_link_mdns(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1498 return call_link_method(userdata
, message
, bus_link_method_set_mdns
, error
);
1501 static int bus_method_set_link_dnssec(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1502 return call_link_method(userdata
, message
, bus_link_method_set_dnssec
, error
);
1505 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1506 return call_link_method(userdata
, message
, bus_link_method_set_dnssec_negative_trust_anchors
, error
);
1509 static int bus_method_revert_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1510 return call_link_method(userdata
, message
, bus_link_method_revert
, error
);
1513 static int bus_method_get_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1514 _cleanup_free_
char *p
= NULL
;
1515 Manager
*m
= userdata
;
1522 assert_cc(sizeof(int) == sizeof(int32_t));
1523 r
= sd_bus_message_read(message
, "i", &ifindex
);
1527 r
= get_any_link(m
, ifindex
, &l
, error
);
1531 p
= link_bus_path(l
);
1535 return sd_bus_reply_method_return(message
, "o", p
);
1538 static const sd_bus_vtable resolve_vtable
[] = {
1539 SD_BUS_VTABLE_START(0),
1540 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL
, offsetof(Manager
, llmnr_hostname
), 0),
1541 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers
, 0, 0),
1542 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains
, 0, 0),
1543 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics
, 0, 0),
1544 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics
, 0, 0),
1545 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics
, 0, 0),
1546 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported
, 0, 0),
1548 SD_BUS_METHOD("ResolveHostname", "isit", "a(iiay)st", bus_method_resolve_hostname
, SD_BUS_VTABLE_UNPRIVILEGED
),
1549 SD_BUS_METHOD("ResolveAddress", "iiayt", "a(is)t", bus_method_resolve_address
, SD_BUS_VTABLE_UNPRIVILEGED
),
1550 SD_BUS_METHOD("ResolveRecord", "isqqt", "a(iqqay)t", bus_method_resolve_record
, SD_BUS_VTABLE_UNPRIVILEGED
),
1551 SD_BUS_METHOD("ResolveService", "isssit", "a(qqqsa(iiay)s)aayssst", bus_method_resolve_service
, SD_BUS_VTABLE_UNPRIVILEGED
),
1552 SD_BUS_METHOD("ResetStatistics", NULL
, NULL
, bus_method_reset_statistics
, 0),
1553 SD_BUS_METHOD("GetLink", "i", "o", bus_method_get_link
, SD_BUS_VTABLE_UNPRIVILEGED
),
1554 SD_BUS_METHOD("SetLinkDNS", "ia(iay)", NULL
, bus_method_set_link_dns_servers
, 0),
1555 SD_BUS_METHOD("SetLinkDomains", "ia(sb)", NULL
, bus_method_set_link_domains
, 0),
1556 SD_BUS_METHOD("SetLinkLLMNR", "is", NULL
, bus_method_set_link_llmnr
, 0),
1557 SD_BUS_METHOD("SetLinkMulticastDNS", "is", NULL
, bus_method_set_link_mdns
, 0),
1558 SD_BUS_METHOD("SetLinkDNSSEC", "is", NULL
, bus_method_set_link_dnssec
, 0),
1559 SD_BUS_METHOD("SetLinkDNSSECNegativeTrustAnchors", "ias", NULL
, bus_method_set_link_dnssec_negative_trust_anchors
, 0),
1560 SD_BUS_METHOD("RevertLink", "i", NULL
, bus_method_revert_link
, 0),
1565 static int on_bus_retry(sd_event_source
*s
, usec_t usec
, void *userdata
) {
1566 Manager
*m
= userdata
;
1571 m
->bus_retry_event_source
= sd_event_source_unref(m
->bus_retry_event_source
);
1573 manager_connect_bus(m
);
1577 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
1578 Manager
*m
= userdata
;
1584 r
= sd_bus_message_read(message
, "b", &b
);
1586 log_debug_errno(r
, "Failed to parse PrepareForSleep signal: %m");
1593 log_debug("Coming back from suspend, verifying all RRs...");
1595 manager_verify_all(m
);
1599 int manager_connect_bus(Manager
*m
) {
1607 r
= sd_bus_default_system(&m
->bus
);
1609 /* We failed to connect? Yuck, we must be in early
1610 * boot. Let's try in 5s again. As soon as we have
1611 * kdbus we can stop doing this... */
1613 log_debug_errno(r
, "Failed to connect to bus, trying again in 5s: %m");
1615 r
= sd_event_add_time(m
->event
, &m
->bus_retry_event_source
, CLOCK_MONOTONIC
, now(CLOCK_MONOTONIC
) + 5*USEC_PER_SEC
, 0, on_bus_retry
, m
);
1617 return log_error_errno(r
, "Failed to install bus reconnect time event: %m");
1619 (void) sd_event_source_set_description(m
->bus_retry_event_source
, "bus-retry");
1623 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable
, m
);
1625 return log_error_errno(r
, "Failed to register object: %m");
1627 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/resolve1/link", "org.freedesktop.resolve1.Link", link_vtable
, link_object_find
, m
);
1629 return log_error_errno(r
, "Failed to register link objects: %m");
1631 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/resolve1/link", link_node_enumerator
, m
);
1633 return log_error_errno(r
, "Failed to register link enumerator: %m");
1635 r
= sd_bus_request_name(m
->bus
, "org.freedesktop.resolve1", 0);
1637 return log_error_errno(r
, "Failed to register name: %m");
1639 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
1641 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1643 r
= sd_bus_add_match(m
->bus
, &m
->prepare_for_sleep_slot
,
1645 "sender='org.freedesktop.login1',"
1646 "interface='org.freedesktop.login1.Manager',"
1647 "member='PrepareForSleep',"
1648 "path='/org/freedesktop/login1'",
1649 match_prepare_for_sleep
,
1652 log_error_errno(r
, "Failed to add match for PrepareForSleep: %m");