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 DnsResourceRecord
*rr
;
149 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
150 r
= reply_query_state(q
);
154 r
= dns_query_process_cname(q
);
156 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
));
161 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
164 r
= sd_bus_message_new_method_return(q
->request
, &reply
);
168 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
172 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
173 DnsQuestion
*question
;
175 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
177 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
183 r
= append_address(reply
, rr
, ifindex
);
188 canonical
= dns_resource_record_ref(rr
);
194 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
));
198 r
= sd_bus_message_close_container(reply
);
202 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
204 r
= sd_bus_message_append(
206 DNS_RESOURCE_KEY_NAME(canonical
->key
),
207 SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, q
->answer_authenticated
));
211 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
215 log_error_errno(r
, "Failed to send hostname reply: %m");
216 sd_bus_reply_method_errno(q
->request
, r
, NULL
);
222 static int check_ifindex_flags(int ifindex
, uint64_t *flags
, uint64_t ok
, sd_bus_error
*error
) {
226 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
228 if (*flags
& ~(SD_RESOLVED_PROTOCOLS_ALL
|SD_RESOLVED_NO_CNAME
|ok
))
229 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid flags parameter");
231 if ((*flags
& SD_RESOLVED_PROTOCOLS_ALL
) == 0) /* If no protocol is enabled, enable all */
232 *flags
|= SD_RESOLVED_PROTOCOLS_ALL
;
237 static int parse_as_address(sd_bus_message
*m
, int ifindex
, const char *hostname
, int family
, uint64_t flags
) {
238 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
239 _cleanup_free_
char *canonical
= NULL
;
240 union in_addr_union parsed
;
243 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
244 * let's not attempt to look it up. */
246 r
= in_addr_from_string_auto(hostname
, &ff
, &parsed
);
247 if (r
< 0) /* not an address */
250 if (family
!= AF_UNSPEC
&& ff
!= family
)
251 return sd_bus_reply_method_errorf(m
, BUS_ERROR_NO_SUCH_RR
, "The specified address is not of the requested family.");
253 r
= sd_bus_message_new_method_return(m
, &reply
);
257 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
261 r
= sd_bus_message_open_container(reply
, 'r', "iiay");
265 r
= sd_bus_message_append(reply
, "ii", ifindex
, ff
);
269 r
= sd_bus_message_append_array(reply
, 'y', &parsed
, FAMILY_ADDRESS_SIZE(ff
));
273 r
= sd_bus_message_close_container(reply
);
277 r
= sd_bus_message_close_container(reply
);
281 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
282 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
283 * omissions are always done the same way). */
284 r
= in_addr_to_string(ff
, &parsed
, &canonical
);
288 r
= sd_bus_message_append(reply
, "st", canonical
,
289 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags
), ff
, true));
293 return sd_bus_send(sd_bus_message_get_bus(m
), reply
, NULL
);
296 static int bus_method_resolve_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
297 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
298 Manager
*m
= userdata
;
299 const char *hostname
;
308 assert_cc(sizeof(int) == sizeof(int32_t));
310 r
= sd_bus_message_read(message
, "isit", &ifindex
, &hostname
, &family
, &flags
);
314 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
315 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
317 r
= check_ifindex_flags(ifindex
, &flags
, SD_RESOLVED_NO_SEARCH
, error
);
321 r
= parse_as_address(message
, ifindex
, hostname
, family
, flags
);
325 r
= dns_name_is_valid(hostname
);
329 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid hostname '%s'", hostname
);
331 r
= dns_question_new_address(&question_utf8
, family
, hostname
, false);
335 r
= dns_question_new_address(&question_idna
, family
, hostname
, true);
339 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
, ifindex
, flags
);
343 q
->request
= sd_bus_message_ref(message
);
344 q
->request_family
= family
;
345 q
->complete
= bus_method_resolve_hostname_complete
;
346 q
->suppress_unroutable_family
= family
== AF_UNSPEC
;
348 r
= dns_query_bus_track(q
, message
);
363 static void bus_method_resolve_address_complete(DnsQuery
*q
) {
364 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
365 DnsQuestion
*question
;
366 DnsResourceRecord
*rr
;
372 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
373 r
= reply_query_state(q
);
377 r
= dns_query_process_cname(q
);
379 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
));
384 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
387 r
= sd_bus_message_new_method_return(q
->request
, &reply
);
391 r
= sd_bus_message_open_container(reply
, 'a', "(is)");
395 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
397 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
398 r
= dns_question_matches_rr(question
, rr
, NULL
);
404 r
= sd_bus_message_append(reply
, "(is)", ifindex
, rr
->ptr
.name
);
412 _cleanup_free_
char *ip
= NULL
;
414 in_addr_to_string(q
->request_family
, &q
->request_address
, &ip
);
415 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NO_SUCH_RR
, "Address '%s' does not have any RR of requested type", strna(ip
));
419 r
= sd_bus_message_close_container(reply
);
423 r
= sd_bus_message_append(reply
, "t", SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, q
->answer_authenticated
));
427 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
431 log_error_errno(r
, "Failed to send address reply: %m");
432 sd_bus_reply_method_errno(q
->request
, r
, NULL
);
438 static int bus_method_resolve_address(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
439 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
440 Manager
*m
= userdata
;
451 assert_cc(sizeof(int) == sizeof(int32_t));
453 r
= sd_bus_message_read(message
, "ii", &ifindex
, &family
);
457 if (!IN_SET(family
, AF_INET
, AF_INET6
))
458 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
460 r
= sd_bus_message_read_array(message
, 'y', &d
, &sz
);
464 if (sz
!= FAMILY_ADDRESS_SIZE(family
))
465 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid address size");
467 r
= sd_bus_message_read(message
, "t", &flags
);
471 r
= check_ifindex_flags(ifindex
, &flags
, 0, error
);
475 r
= dns_question_new_reverse(&question
, family
, d
);
479 r
= dns_query_new(m
, &q
, question
, question
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
483 q
->request
= sd_bus_message_ref(message
);
484 q
->request_family
= family
;
485 memcpy(&q
->request_address
, d
, sz
);
486 q
->complete
= bus_method_resolve_address_complete
;
488 r
= dns_query_bus_track(q
, message
);
503 static int bus_message_append_rr(sd_bus_message
*m
, DnsResourceRecord
*rr
, int ifindex
) {
509 r
= sd_bus_message_open_container(m
, 'r', "iqqay");
513 r
= sd_bus_message_append(m
, "iqq",
520 r
= dns_resource_record_to_wire_format(rr
, false);
524 r
= sd_bus_message_append_array(m
, 'y', rr
->wire_format
, rr
->wire_format_size
);
528 return sd_bus_message_close_container(m
);
531 static void bus_method_resolve_record_complete(DnsQuery
*q
) {
532 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
533 DnsResourceRecord
*rr
;
534 DnsQuestion
*question
;
541 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
542 r
= reply_query_state(q
);
546 r
= dns_query_process_cname(q
);
548 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
));
553 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
556 r
= sd_bus_message_new_method_return(q
->request
, &reply
);
560 r
= sd_bus_message_open_container(reply
, 'a', "(iqqay)");
564 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
566 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
567 r
= dns_question_matches_rr(question
, rr
, NULL
);
573 r
= bus_message_append_rr(reply
, rr
, ifindex
);
581 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
));
585 r
= sd_bus_message_close_container(reply
);
589 r
= sd_bus_message_append(reply
, "t", SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, q
->answer_authenticated
));
593 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
597 log_error_errno(r
, "Failed to send record reply: %m");
598 sd_bus_reply_method_errno(q
->request
, r
, NULL
);
604 static int bus_method_resolve_record(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
605 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
606 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
607 Manager
*m
= userdata
;
608 uint16_t class, type
;
617 assert_cc(sizeof(int) == sizeof(int32_t));
619 r
= sd_bus_message_read(message
, "isqqt", &ifindex
, &name
, &class, &type
, &flags
);
623 r
= dns_name_is_valid(name
);
627 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid name '%s'", name
);
629 if (!dns_type_is_valid_query(type
))
630 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified resource record type %" PRIu16
" may not be used in a query.", type
);
631 if (dns_type_is_obsolete(type
))
632 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Specified DNS resource record type %" PRIu16
" is obsolete.", type
);
634 r
= check_ifindex_flags(ifindex
, &flags
, 0, error
);
638 question
= dns_question_new(1);
642 key
= dns_resource_key_new(class, type
, name
);
646 r
= dns_question_add(question
, key
);
650 r
= dns_query_new(m
, &q
, question
, question
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
654 q
->request
= sd_bus_message_ref(message
);
655 q
->complete
= bus_method_resolve_record_complete
;
657 r
= dns_query_bus_track(q
, message
);
672 static int append_srv(DnsQuery
*q
, sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
673 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
682 if (rr
->key
->type
!= DNS_TYPE_SRV
)
685 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
686 /* First, let's see if we could find an appropriate A or AAAA
687 * record for the SRV record */
688 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
689 DnsResourceRecord
*zz
;
690 DnsQuestion
*question
;
692 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
694 if (aux
->auxiliary_result
!= 0)
697 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
699 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
705 DNS_ANSWER_FOREACH(zz
, aux
->answer
) {
707 r
= dns_question_matches_rr(question
, zz
, NULL
);
713 canonical
= dns_resource_record_ref(zz
);
721 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
726 r
= sd_bus_message_open_container(reply
, 'r', "qqqsa(iiay)s");
730 r
= sd_bus_message_append(
733 rr
->srv
.priority
, rr
->srv
.weight
, rr
->srv
.port
, rr
->srv
.name
);
737 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
741 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
742 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
743 DnsResourceRecord
*zz
;
744 DnsQuestion
*question
;
747 if (aux
->state
!= DNS_TRANSACTION_SUCCESS
)
749 if (aux
->auxiliary_result
!= 0)
752 question
= dns_query_question_for_protocol(aux
, aux
->answer_protocol
);
754 r
= dns_name_equal(dns_question_first_name(question
), rr
->srv
.name
);
760 DNS_ANSWER_FOREACH_IFINDEX(zz
, ifindex
, aux
->answer
) {
762 r
= dns_question_matches_rr(question
, zz
, NULL
);
768 r
= append_address(reply
, zz
, ifindex
);
775 r
= sd_bus_message_close_container(reply
);
779 /* Note that above we appended the hostname as encoded in the
780 * SRV, and here the canonical hostname this maps to. */
781 r
= sd_bus_message_append(reply
, "s", canonical
? DNS_RESOURCE_KEY_NAME(canonical
->key
) : rr
->srv
.name
);
785 r
= sd_bus_message_close_container(reply
);
792 static int append_txt(sd_bus_message
*reply
, DnsResourceRecord
*rr
) {
800 if (rr
->key
->type
!= DNS_TYPE_TXT
)
803 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
808 r
= sd_bus_message_append_array(reply
, 'y', i
->data
, i
->length
);
816 static void resolve_service_all_complete(DnsQuery
*q
) {
817 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*canonical
= NULL
;
818 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
819 _cleanup_free_
char *name
= NULL
, *type
= NULL
, *domain
= NULL
;
820 DnsQuestion
*question
;
821 DnsResourceRecord
*rr
;
828 if (q
->block_all_complete
> 0)
831 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
832 DnsQuery
*bad
= NULL
;
833 bool have_success
= false;
835 LIST_FOREACH(auxiliary_queries
, aux
, q
->auxiliary_queries
) {
837 switch (aux
->state
) {
839 case DNS_TRANSACTION_PENDING
:
840 /* If an auxiliary query is still pending, let's wait */
843 case DNS_TRANSACTION_SUCCESS
:
844 if (aux
->auxiliary_result
== 0)
857 /* We can only return one error, hence pick the last error we encountered */
861 if (bad
->state
== DNS_TRANSACTION_SUCCESS
) {
862 assert(bad
->auxiliary_result
!= 0);
864 if (bad
->auxiliary_result
== -ELOOP
) {
865 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
));
869 r
= bad
->auxiliary_result
;
873 r
= reply_query_state(bad
);
878 r
= sd_bus_message_new_method_return(q
->request
, &reply
);
882 r
= sd_bus_message_open_container(reply
, 'a', "(qqqsa(iiay)s)");
886 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
887 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
888 r
= dns_question_matches_rr(question
, rr
, NULL
);
894 r
= append_srv(q
, reply
, rr
);
897 if (r
== 0) /* not an SRV record */
901 canonical
= dns_resource_record_ref(rr
);
907 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
));
911 r
= sd_bus_message_close_container(reply
);
915 r
= sd_bus_message_open_container(reply
, 'a', "ay");
919 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
920 r
= dns_question_matches_rr(question
, rr
, NULL
);
926 r
= append_txt(reply
, rr
);
931 r
= sd_bus_message_close_container(reply
);
936 r
= dns_service_split(DNS_RESOURCE_KEY_NAME(canonical
->key
), &name
, &type
, &domain
);
940 r
= sd_bus_message_append(
944 SD_RESOLVED_FLAGS_MAKE(q
->answer_protocol
, q
->answer_family
, q
->answer_authenticated
));
948 r
= sd_bus_send(q
->manager
->bus
, reply
, NULL
);
952 log_error_errno(r
, "Failed to send service reply: %m");
953 sd_bus_reply_method_errno(q
->request
, r
, NULL
);
959 static void resolve_service_hostname_complete(DnsQuery
*q
) {
963 assert(q
->auxiliary_for
);
965 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
966 resolve_service_all_complete(q
->auxiliary_for
);
970 r
= dns_query_process_cname(q
);
971 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
974 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
975 q
->auxiliary_result
= r
;
976 resolve_service_all_complete(q
->auxiliary_for
);
979 static int resolve_service_hostname(DnsQuery
*q
, DnsResourceRecord
*rr
, int ifindex
) {
980 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
987 assert(rr
->key
->type
== DNS_TYPE_SRV
);
989 /* OK, we found an SRV record for the service. Let's resolve
990 * the hostname included in it */
992 r
= dns_question_new_address(&question
, q
->request_family
, rr
->srv
.name
, false);
996 r
= dns_query_new(q
->manager
, &aux
, question
, question
, ifindex
, q
->flags
|SD_RESOLVED_NO_SEARCH
);
1000 aux
->request_family
= q
->request_family
;
1001 aux
->complete
= resolve_service_hostname_complete
;
1003 r
= dns_query_make_auxiliary(aux
, q
);
1005 /* Too many auxiliary lookups? If so, don't complain,
1006 * let's just not add this one, we already have more
1009 dns_query_free(aux
);
1015 /* Note that auxiliary queries do not track the original bus
1016 * client, only the primary request does that. */
1018 r
= dns_query_go(aux
);
1025 dns_query_free(aux
);
1029 static void bus_method_resolve_service_complete(DnsQuery
*q
) {
1030 bool has_root_domain
= false;
1031 DnsResourceRecord
*rr
;
1032 DnsQuestion
*question
;
1038 if (q
->state
!= DNS_TRANSACTION_SUCCESS
) {
1039 r
= reply_query_state(q
);
1043 r
= dns_query_process_cname(q
);
1045 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
));
1050 if (r
== DNS_QUERY_RESTARTED
) /* This was a cname, and the query was restarted. */
1053 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1055 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, q
->answer
) {
1056 r
= dns_question_matches_rr(question
, rr
, NULL
);
1062 if (rr
->key
->type
!= DNS_TYPE_SRV
)
1065 if (dns_name_is_root(rr
->srv
.name
)) {
1066 has_root_domain
= true;
1070 if ((q
->flags
& SD_RESOLVED_NO_ADDRESS
) == 0) {
1071 q
->block_all_complete
++;
1072 r
= resolve_service_hostname(q
, rr
, ifindex
);
1073 q
->block_all_complete
--;
1082 if (has_root_domain
&& found
<= 0) {
1083 /* If there's exactly one SRV RR and it uses
1084 * the root domain as host name, then the
1085 * service is explicitly not offered on the
1086 * domain. Report this as a recognizable
1087 * error. See RFC 2782, Section "Usage
1089 r
= sd_bus_reply_method_errorf(q
->request
, BUS_ERROR_NO_SUCH_SERVICE
, "'%s' does not provide the requested service", dns_query_string(q
));
1094 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
));
1098 /* Maybe we are already finished? check now... */
1099 resolve_service_all_complete(q
);
1104 log_error_errno(r
, "Failed to send service reply: %m");
1105 sd_bus_reply_method_errno(q
->request
, r
, NULL
);
1111 static int bus_method_resolve_service(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1112 _cleanup_(dns_question_unrefp
) DnsQuestion
*question_idna
= NULL
, *question_utf8
= NULL
;
1113 const char *name
, *type
, *domain
;
1114 _cleanup_free_
char *n
= NULL
;
1115 Manager
*m
= userdata
;
1116 int family
, ifindex
;
1124 assert_cc(sizeof(int) == sizeof(int32_t));
1126 r
= sd_bus_message_read(message
, "isssit", &ifindex
, &name
, &type
, &domain
, &family
, &flags
);
1130 if (!IN_SET(family
, AF_INET
, AF_INET6
, AF_UNSPEC
))
1131 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown address family %i", family
);
1135 else if (!dns_service_name_is_valid(name
))
1136 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid service name '%s'", name
);
1140 else if (!dns_srv_type_is_valid(type
))
1141 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid SRV service type '%s'", type
);
1143 r
= dns_name_is_valid(domain
);
1147 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid domain '%s'", domain
);
1150 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Service name cannot be specified without service type.");
1152 r
= check_ifindex_flags(ifindex
, &flags
, SD_RESOLVED_NO_TXT
|SD_RESOLVED_NO_ADDRESS
, error
);
1156 r
= dns_question_new_service(&question_utf8
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), false);
1160 r
= dns_question_new_service(&question_idna
, name
, type
, domain
, !(flags
& SD_RESOLVED_NO_TXT
), true);
1164 r
= dns_query_new(m
, &q
, question_utf8
, question_idna
, ifindex
, flags
|SD_RESOLVED_NO_SEARCH
);
1168 q
->request
= sd_bus_message_ref(message
);
1169 q
->request_family
= family
;
1170 q
->complete
= bus_method_resolve_service_complete
;
1172 r
= dns_query_bus_track(q
, message
);
1176 r
= dns_query_go(q
);
1187 int bus_dns_server_append(sd_bus_message
*reply
, DnsServer
*s
, bool with_ifindex
) {
1193 r
= sd_bus_message_open_container(reply
, 'r', with_ifindex
? "iiay" : "iay");
1198 r
= sd_bus_message_append(reply
, "i", s
->link
? s
->link
->ifindex
: 0);
1203 r
= sd_bus_message_append(reply
, "i", s
->family
);
1207 r
= sd_bus_message_append_array(reply
, 'y', &s
->address
, FAMILY_ADDRESS_SIZE(s
->family
));
1211 return sd_bus_message_close_container(reply
);
1214 static int bus_property_get_dns_servers(
1217 const char *interface
,
1218 const char *property
,
1219 sd_bus_message
*reply
,
1221 sd_bus_error
*error
) {
1223 Manager
*m
= userdata
;
1233 r
= sd_bus_message_open_container(reply
, 'a', "(iiay)");
1237 LIST_FOREACH(servers
, s
, m
->dns_servers
) {
1238 r
= bus_dns_server_append(reply
, s
, true);
1245 HASHMAP_FOREACH(l
, m
->links
, i
) {
1246 LIST_FOREACH(servers
, s
, l
->dns_servers
) {
1247 r
= bus_dns_server_append(reply
, s
, true);
1255 LIST_FOREACH(servers
, s
, m
->fallback_dns_servers
) {
1256 r
= bus_dns_server_append(reply
, s
, true);
1262 return sd_bus_message_close_container(reply
);
1265 static int bus_property_get_domains(
1268 const char *interface
,
1269 const char *property
,
1270 sd_bus_message
*reply
,
1272 sd_bus_error
*error
) {
1274 Manager
*m
= userdata
;
1283 r
= sd_bus_message_open_container(reply
, 'a', "(isb)");
1287 LIST_FOREACH(domains
, d
, m
->search_domains
) {
1288 r
= sd_bus_message_append(reply
, "(isb)", 0, d
->name
, d
->route_only
);
1293 HASHMAP_FOREACH(l
, m
->links
, i
) {
1294 LIST_FOREACH(domains
, d
, l
->search_domains
) {
1295 r
= sd_bus_message_append(reply
, "(isb)", l
->ifindex
, d
->name
, d
->route_only
);
1301 return sd_bus_message_close_container(reply
);
1304 static int bus_property_get_transaction_statistics(
1307 const char *interface
,
1308 const char *property
,
1309 sd_bus_message
*reply
,
1311 sd_bus_error
*error
) {
1313 Manager
*m
= userdata
;
1318 return sd_bus_message_append(reply
, "(tt)",
1319 (uint64_t) hashmap_size(m
->dns_transactions
),
1320 (uint64_t) m
->n_transactions_total
);
1323 static int bus_property_get_cache_statistics(
1326 const char *interface
,
1327 const char *property
,
1328 sd_bus_message
*reply
,
1330 sd_bus_error
*error
) {
1332 uint64_t size
= 0, hit
= 0, miss
= 0;
1333 Manager
*m
= userdata
;
1339 LIST_FOREACH(scopes
, s
, m
->dns_scopes
) {
1340 size
+= dns_cache_size(&s
->cache
);
1341 hit
+= s
->cache
.n_hit
;
1342 miss
+= s
->cache
.n_miss
;
1345 return sd_bus_message_append(reply
, "(ttt)", size
, hit
, miss
);
1348 static int bus_property_get_dnssec_statistics(
1351 const char *interface
,
1352 const char *property
,
1353 sd_bus_message
*reply
,
1355 sd_bus_error
*error
) {
1357 Manager
*m
= userdata
;
1362 return sd_bus_message_append(reply
, "(tttt)",
1363 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_SECURE
],
1364 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INSECURE
],
1365 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_BOGUS
],
1366 (uint64_t) m
->n_dnssec_verdict
[DNSSEC_INDETERMINATE
]);
1369 static int bus_property_get_dnssec_supported(
1372 const char *interface
,
1373 const char *property
,
1374 sd_bus_message
*reply
,
1376 sd_bus_error
*error
) {
1378 Manager
*m
= userdata
;
1383 return sd_bus_message_append(reply
, "b", manager_dnssec_supported(m
));
1386 static int bus_method_reset_statistics(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1387 Manager
*m
= userdata
;
1393 LIST_FOREACH(scopes
, s
, m
->dns_scopes
)
1394 s
->cache
.n_hit
= s
->cache
.n_miss
= 0;
1396 m
->n_transactions_total
= 0;
1397 zero(m
->n_dnssec_verdict
);
1399 return sd_bus_reply_method_return(message
, NULL
);
1402 static int get_any_link(Manager
*m
, int ifindex
, Link
**ret
, sd_bus_error
*error
) {
1409 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid interface index");
1411 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
1413 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_LINK
, "Link %i not known", ifindex
);
1419 static int get_unmanaged_link(Manager
*m
, int ifindex
, Link
**ret
, sd_bus_error
*error
) {
1426 r
= get_any_link(m
, ifindex
, &l
, error
);
1430 if (l
->flags
& IFF_LOOPBACK
)
1431 return sd_bus_error_setf(error
, BUS_ERROR_LINK_BUSY
, "Link %s is loopback device.", l
->name
);
1433 return sd_bus_error_setf(error
, BUS_ERROR_LINK_BUSY
, "Link %s is managed.", l
->name
);
1439 static int call_link_method(Manager
*m
, sd_bus_message
*message
, sd_bus_message_handler_t handler
, sd_bus_error
*error
) {
1447 assert_cc(sizeof(int) == sizeof(int32_t));
1448 r
= sd_bus_message_read(message
, "i", &ifindex
);
1452 r
= get_unmanaged_link(m
, ifindex
, &l
, error
);
1456 return handler(message
, l
, error
);
1459 static int bus_method_set_link_dns_servers(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1460 return call_link_method(userdata
, message
, bus_link_method_set_dns_servers
, error
);
1463 static int bus_method_set_link_domains(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1464 return call_link_method(userdata
, message
, bus_link_method_set_domains
, error
);
1467 static int bus_method_set_link_llmnr(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1468 return call_link_method(userdata
, message
, bus_link_method_set_llmnr
, error
);
1471 static int bus_method_set_link_mdns(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1472 return call_link_method(userdata
, message
, bus_link_method_set_mdns
, error
);
1475 static int bus_method_set_link_dnssec(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1476 return call_link_method(userdata
, message
, bus_link_method_set_dnssec
, error
);
1479 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1480 return call_link_method(userdata
, message
, bus_link_method_set_dnssec_negative_trust_anchors
, error
);
1483 static int bus_method_revert_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1484 return call_link_method(userdata
, message
, bus_link_method_revert
, error
);
1487 static int bus_method_get_link(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1488 _cleanup_free_
char *p
= NULL
;
1489 Manager
*m
= userdata
;
1496 assert_cc(sizeof(int) == sizeof(int32_t));
1497 r
= sd_bus_message_read(message
, "i", &ifindex
);
1501 r
= get_any_link(m
, ifindex
, &l
, error
);
1505 p
= link_bus_path(l
);
1509 return sd_bus_reply_method_return(message
, "o", p
);
1512 static const sd_bus_vtable resolve_vtable
[] = {
1513 SD_BUS_VTABLE_START(0),
1514 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL
, offsetof(Manager
, llmnr_hostname
), 0),
1515 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers
, 0, 0),
1516 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains
, 0, 0),
1517 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics
, 0, 0),
1518 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics
, 0, 0),
1519 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics
, 0, 0),
1520 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported
, 0, 0),
1522 SD_BUS_METHOD("ResolveHostname", "isit", "a(iiay)st", bus_method_resolve_hostname
, SD_BUS_VTABLE_UNPRIVILEGED
),
1523 SD_BUS_METHOD("ResolveAddress", "iiayt", "a(is)t", bus_method_resolve_address
, SD_BUS_VTABLE_UNPRIVILEGED
),
1524 SD_BUS_METHOD("ResolveRecord", "isqqt", "a(iqqay)t", bus_method_resolve_record
, SD_BUS_VTABLE_UNPRIVILEGED
),
1525 SD_BUS_METHOD("ResolveService", "isssit", "a(qqqsa(iiay)s)aayssst", bus_method_resolve_service
, SD_BUS_VTABLE_UNPRIVILEGED
),
1526 SD_BUS_METHOD("ResetStatistics", NULL
, NULL
, bus_method_reset_statistics
, 0),
1527 SD_BUS_METHOD("GetLink", "i", "o", bus_method_get_link
, SD_BUS_VTABLE_UNPRIVILEGED
),
1528 SD_BUS_METHOD("SetLinkDNS", "ia(iay)", NULL
, bus_method_set_link_dns_servers
, 0),
1529 SD_BUS_METHOD("SetLinkDomains", "ia(sb)", NULL
, bus_method_set_link_domains
, 0),
1530 SD_BUS_METHOD("SetLinkLLMNR", "is", NULL
, bus_method_set_link_llmnr
, 0),
1531 SD_BUS_METHOD("SetLinkMulticastDNS", "is", NULL
, bus_method_set_link_mdns
, 0),
1532 SD_BUS_METHOD("SetLinkDNSSEC", "is", NULL
, bus_method_set_link_dnssec
, 0),
1533 SD_BUS_METHOD("SetLinkDNSSECNegativeTrustAnchors", "ias", NULL
, bus_method_set_link_dnssec_negative_trust_anchors
, 0),
1534 SD_BUS_METHOD("RevertLink", "i", NULL
, bus_method_revert_link
, 0),
1539 static int on_bus_retry(sd_event_source
*s
, usec_t usec
, void *userdata
) {
1540 Manager
*m
= userdata
;
1545 m
->bus_retry_event_source
= sd_event_source_unref(m
->bus_retry_event_source
);
1547 manager_connect_bus(m
);
1551 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
1552 Manager
*m
= userdata
;
1558 r
= sd_bus_message_read(message
, "b", &b
);
1560 log_debug_errno(r
, "Failed to parse PrepareForSleep signal: %m");
1567 log_debug("Coming back from suspend, verifying all RRs...");
1569 manager_verify_all(m
);
1573 int manager_connect_bus(Manager
*m
) {
1581 r
= sd_bus_default_system(&m
->bus
);
1583 /* We failed to connect? Yuck, we must be in early
1584 * boot. Let's try in 5s again. As soon as we have
1585 * kdbus we can stop doing this... */
1587 log_debug_errno(r
, "Failed to connect to bus, trying again in 5s: %m");
1589 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
);
1591 return log_error_errno(r
, "Failed to install bus reconnect time event: %m");
1593 (void) sd_event_source_set_description(m
->bus_retry_event_source
, "bus-retry");
1597 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable
, m
);
1599 return log_error_errno(r
, "Failed to register object: %m");
1601 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/resolve1/link", "org.freedesktop.resolve1.Link", link_vtable
, link_object_find
, m
);
1603 return log_error_errno(r
, "Failed to register link objects: %m");
1605 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/resolve1/link", link_node_enumerator
, m
);
1607 return log_error_errno(r
, "Failed to register link enumerator: %m");
1609 r
= sd_bus_request_name(m
->bus
, "org.freedesktop.resolve1", 0);
1611 return log_error_errno(r
, "Failed to register name: %m");
1613 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
1615 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1617 r
= sd_bus_add_match(m
->bus
, &m
->prepare_for_sleep_slot
,
1619 "sender='org.freedesktop.login1',"
1620 "interface='org.freedesktop.login1.Manager',"
1621 "member='PrepareForSleep',"
1622 "path='/org/freedesktop/login1'",
1623 match_prepare_for_sleep
,
1626 log_error_errno(r
, "Failed to add match for PrepareForSleep: %m");