1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
12 #include "sd-netlink.h"
15 #include "alloc-util.h"
16 #include "bus-common-errors.h"
17 #include "bus-error.h"
19 #include "dns-domain.h"
21 #include "gcrypt-util.h"
22 #include "in-addr-util.h"
23 #include "netlink-util.h"
25 #include "parse-util.h"
26 #include "resolvconf-compat.h"
27 #include "resolvectl.h"
28 #include "resolved-def.h"
29 #include "resolved-dns-packet.h"
31 #include "terminal-util.h"
33 static int arg_family
= AF_UNSPEC
;
35 static uint16_t arg_type
= 0;
36 static uint16_t arg_class
= 0;
37 static bool arg_legend
= true;
38 static uint64_t arg_flags
= 0;
39 static bool arg_no_pager
= false;
40 bool arg_ifindex_permissive
= false; /* If true, don't generate an error if the specified interface index doesn't exist */
42 typedef enum ServiceFamily
{
46 _SERVICE_FAMILY_INVALID
= -1,
48 static ServiceFamily arg_service_family
= SERVICE_FAMILY_TCP
;
50 typedef enum RawType
{
55 static RawType arg_raw
= RAW_NONE
;
57 ExecutionMode arg_mode
= MODE_RESOLVE_HOST
;
59 struct in_addr_data
*arg_set_dns
= NULL
;
60 size_t arg_n_set_dns
= 0;
61 char **arg_set_domain
= NULL
;
62 static char *arg_set_llmnr
= NULL
;
63 static char *arg_set_mdns
= NULL
;
64 static char *arg_set_dnssec
= NULL
;
65 static char **arg_set_nta
= NULL
;
67 static ServiceFamily
service_family_from_string(const char *s
) {
68 if (!s
|| streq(s
, "tcp"))
69 return SERVICE_FAMILY_TCP
;
71 return SERVICE_FAMILY_UDP
;
73 return SERVICE_FAMILY_SCTP
;
74 return _SERVICE_FAMILY_INVALID
;
77 static const char* service_family_to_string(ServiceFamily service
) {
79 case SERVICE_FAMILY_TCP
:
81 case SERVICE_FAMILY_UDP
:
83 case SERVICE_FAMILY_SCTP
:
86 assert_not_reached("invalid service");
90 static void print_source(uint64_t flags
, usec_t rtt
) {
91 char rtt_str
[FORMAT_TIMESTAMP_MAX
];
99 fputs("\n-- Information acquired via", stdout
);
102 printf(" protocol%s%s%s%s%s",
103 flags
& SD_RESOLVED_DNS
? " DNS" :"",
104 flags
& SD_RESOLVED_LLMNR_IPV4
? " LLMNR/IPv4" : "",
105 flags
& SD_RESOLVED_LLMNR_IPV6
? " LLMNR/IPv6" : "",
106 flags
& SD_RESOLVED_MDNS_IPV4
? " mDNS/IPv4" : "",
107 flags
& SD_RESOLVED_MDNS_IPV6
? " mDNS/IPv6" : "");
109 assert_se(format_timespan(rtt_str
, sizeof(rtt_str
), rtt
, 100));
111 printf(" in %s", rtt_str
);
116 printf("-- Data is authenticated: %s\n", yes_no(flags
& SD_RESOLVED_AUTHENTICATED
));
119 static int resolve_host(sd_bus
*bus
, const char *name
) {
121 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
122 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
123 const char *canonical
= NULL
;
124 char ifname
[IF_NAMESIZE
] = "";
132 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
133 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
135 log_debug("Resolving %s (family %s, interface %s).", name
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
137 r
= sd_bus_message_new_method_call(
140 "org.freedesktop.resolve1",
141 "/org/freedesktop/resolve1",
142 "org.freedesktop.resolve1.Manager",
145 return bus_log_create_error(r
);
147 r
= sd_bus_message_append(req
, "isit", arg_ifindex
, name
, arg_family
, arg_flags
);
149 return bus_log_create_error(r
);
151 ts
= now(CLOCK_MONOTONIC
);
153 r
= sd_bus_call(bus
, req
, SD_RESOLVED_QUERY_TIMEOUT_USEC
, &error
, &reply
);
155 return log_error_errno(r
, "%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
157 ts
= now(CLOCK_MONOTONIC
) - ts
;
159 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
161 return bus_log_parse_error(r
);
163 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
164 _cleanup_free_
char *pretty
= NULL
;
169 assert_cc(sizeof(int) == sizeof(int32_t));
171 r
= sd_bus_message_read(reply
, "ii", &ifindex
, &family
);
173 return bus_log_parse_error(r
);
175 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
177 return bus_log_parse_error(r
);
179 r
= sd_bus_message_exit_container(reply
);
181 return bus_log_parse_error(r
);
183 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
184 log_debug("%s: skipping entry with family %d (%s)", name
, family
, af_to_name(family
) ?: "unknown");
188 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
189 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name
, sz
, af_to_name(family
) ?: "unknown");
194 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
195 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
197 r
= in_addr_ifindex_to_string(family
, a
, ifindex
, &pretty
);
199 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
201 printf("%*s%s %s%s%s\n",
202 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
204 isempty(ifname
) ? "" : "%", ifname
);
209 return bus_log_parse_error(r
);
211 r
= sd_bus_message_exit_container(reply
);
213 return bus_log_parse_error(r
);
215 r
= sd_bus_message_read(reply
, "st", &canonical
, &flags
);
217 return bus_log_parse_error(r
);
219 if (!streq(name
, canonical
))
220 printf("%*s%s (%s)\n",
221 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
225 log_error("%s: no addresses found", name
);
229 print_source(flags
, ts
);
234 static int resolve_address(sd_bus
*bus
, int family
, const union in_addr_union
*address
, int ifindex
) {
235 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
236 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
237 _cleanup_free_
char *pretty
= NULL
;
238 char ifname
[IF_NAMESIZE
] = "";
245 assert(IN_SET(family
, AF_INET
, AF_INET6
));
249 ifindex
= arg_ifindex
;
251 r
= in_addr_ifindex_to_string(family
, address
, ifindex
, &pretty
);
255 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
256 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
258 log_debug("Resolving %s%s%s.", pretty
, isempty(ifname
) ? "" : "%", ifname
);
260 r
= sd_bus_message_new_method_call(
263 "org.freedesktop.resolve1",
264 "/org/freedesktop/resolve1",
265 "org.freedesktop.resolve1.Manager",
268 return bus_log_create_error(r
);
270 r
= sd_bus_message_append(req
, "ii", ifindex
, family
);
272 return bus_log_create_error(r
);
274 r
= sd_bus_message_append_array(req
, 'y', address
, FAMILY_ADDRESS_SIZE(family
));
276 return bus_log_create_error(r
);
278 r
= sd_bus_message_append(req
, "t", arg_flags
);
280 return bus_log_create_error(r
);
282 ts
= now(CLOCK_MONOTONIC
);
284 r
= sd_bus_call(bus
, req
, SD_RESOLVED_QUERY_TIMEOUT_USEC
, &error
, &reply
);
286 log_error("%s: resolve call failed: %s", pretty
, bus_error_message(&error
, r
));
290 ts
= now(CLOCK_MONOTONIC
) - ts
;
292 r
= sd_bus_message_enter_container(reply
, 'a', "(is)");
294 return bus_log_create_error(r
);
296 while ((r
= sd_bus_message_enter_container(reply
, 'r', "is")) > 0) {
299 assert_cc(sizeof(int) == sizeof(int32_t));
301 r
= sd_bus_message_read(reply
, "is", &ifindex
, &n
);
305 r
= sd_bus_message_exit_container(reply
);
310 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
311 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
313 printf("%*s%*s%*s%s %s\n",
314 (int) strlen(pretty
), c
== 0 ? pretty
: "",
315 isempty(ifname
) ? 0 : 1, c
> 0 || isempty(ifname
) ? "" : "%",
316 (int) strlen(ifname
), c
== 0 ? ifname
: "",
323 return bus_log_parse_error(r
);
325 r
= sd_bus_message_exit_container(reply
);
327 return bus_log_parse_error(r
);
329 r
= sd_bus_message_read(reply
, "t", &flags
);
331 return bus_log_parse_error(r
);
334 log_error("%s: no names found", pretty
);
338 print_source(flags
, ts
);
343 static int output_rr_packet(const void *d
, size_t l
, int ifindex
) {
344 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
345 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
347 char ifname
[IF_NAMESIZE
] = "";
349 r
= dns_packet_new(&p
, DNS_PROTOCOL_DNS
, 0, DNS_PACKET_SIZE_MAX
);
353 p
->refuse_compression
= true;
355 r
= dns_packet_append_blob(p
, d
, l
, NULL
);
359 r
= dns_packet_read_rr(p
, &rr
, NULL
, NULL
);
361 return log_error_errno(r
, "Failed to parse RR: %m");
363 if (arg_raw
== RAW_PAYLOAD
) {
367 k
= dns_resource_record_payload(rr
, &data
);
369 return log_error_errno(k
, "Cannot dump RR: %m");
370 fwrite(data
, 1, k
, stdout
);
374 s
= dns_resource_record_to_string(rr
);
378 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
379 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
381 printf("%s%s%s\n", s
, isempty(ifname
) ? "" : " # interface ", ifname
);
387 static int resolve_record(sd_bus
*bus
, const char *name
, uint16_t class, uint16_t type
, bool warn_missing
) {
388 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
389 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
390 char ifname
[IF_NAMESIZE
] = "";
395 bool needs_authentication
= false;
399 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
400 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
402 log_debug("Resolving %s %s %s (interface %s).", name
, dns_class_to_string(class), dns_type_to_string(type
), isempty(ifname
) ? "*" : ifname
);
404 r
= sd_bus_message_new_method_call(
407 "org.freedesktop.resolve1",
408 "/org/freedesktop/resolve1",
409 "org.freedesktop.resolve1.Manager",
412 return bus_log_create_error(r
);
414 r
= sd_bus_message_append(req
, "isqqt", arg_ifindex
, name
, class, type
, arg_flags
);
416 return bus_log_create_error(r
);
418 ts
= now(CLOCK_MONOTONIC
);
420 r
= sd_bus_call(bus
, req
, SD_RESOLVED_QUERY_TIMEOUT_USEC
, &error
, &reply
);
422 if (warn_missing
|| r
!= -ENXIO
)
423 log_error("%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
427 ts
= now(CLOCK_MONOTONIC
) - ts
;
429 r
= sd_bus_message_enter_container(reply
, 'a', "(iqqay)");
431 return bus_log_parse_error(r
);
433 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iqqay")) > 0) {
439 assert_cc(sizeof(int) == sizeof(int32_t));
441 r
= sd_bus_message_read(reply
, "iqq", &ifindex
, &c
, &t
);
443 return bus_log_parse_error(r
);
445 r
= sd_bus_message_read_array(reply
, 'y', &d
, &l
);
447 return bus_log_parse_error(r
);
449 r
= sd_bus_message_exit_container(reply
);
451 return bus_log_parse_error(r
);
453 if (arg_raw
== RAW_PACKET
) {
454 uint64_t u64
= htole64(l
);
456 fwrite(&u64
, sizeof(u64
), 1, stdout
);
457 fwrite(d
, 1, l
, stdout
);
459 r
= output_rr_packet(d
, l
, ifindex
);
464 if (dns_type_needs_authentication(t
))
465 needs_authentication
= true;
470 return bus_log_parse_error(r
);
472 r
= sd_bus_message_exit_container(reply
);
474 return bus_log_parse_error(r
);
476 r
= sd_bus_message_read(reply
, "t", &flags
);
478 return bus_log_parse_error(r
);
482 log_error("%s: no records found", name
);
486 print_source(flags
, ts
);
488 if ((flags
& SD_RESOLVED_AUTHENTICATED
) == 0 && needs_authentication
) {
491 fprintf(stderr
, "\n%s"
492 "WARNING: The resources shown contain cryptographic key data which could not be\n"
493 " authenticated. It is not suitable to authenticate any communication.\n"
494 " This is usually indication that DNSSEC authentication was not enabled\n"
495 " or is not available for the selected protocol or DNS servers.%s\n",
496 ansi_highlight_red(),
503 static int resolve_rfc4501(sd_bus
*bus
, const char *name
) {
504 uint16_t type
= 0, class = 0;
505 const char *p
, *q
, *n
;
510 assert(startswith(name
, "dns:"));
512 /* Parse RFC 4501 dns: URIs */
522 e
= strchr(p
+ 2, '/');
527 log_warning("DNS authority specification not supported; ignoring specified authority.");
534 n
= strndupa(p
, q
- p
);
540 f
= startswith_no_case(q
, "class=");
542 _cleanup_free_
char *t
= NULL
;
546 log_error("DNS class specified twice.");
550 e
= strchrnul(f
, ';');
551 t
= strndup(f
, e
- f
);
555 r
= dns_class_from_string(t
);
557 log_error("Unknown DNS class %s.", t
);
571 f
= startswith_no_case(q
, "type=");
573 _cleanup_free_
char *t
= NULL
;
577 log_error("DNS type specified twice.");
581 e
= strchrnul(f
, ';');
582 t
= strndup(f
, e
- f
);
586 r
= dns_type_from_string(t
);
588 log_error("Unknown DNS type %s.", t
);
608 class = arg_class
?: DNS_CLASS_IN
;
610 type
= arg_type
?: DNS_TYPE_A
;
612 return resolve_record(bus
, n
, class, type
, true);
615 log_error("Invalid DNS URI: %s", name
);
619 static int resolve_service(sd_bus
*bus
, const char *name
, const char *type
, const char *domain
) {
620 const char *canonical_name
, *canonical_type
, *canonical_domain
;
621 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
622 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
623 char ifname
[IF_NAMESIZE
] = "";
634 name
= empty_to_null(name
);
635 type
= empty_to_null(type
);
637 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
638 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
641 log_debug("Resolving service \"%s\" of type %s in %s (family %s, interface %s).", name
, type
, domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
643 log_debug("Resolving service type %s of %s (family %s, interface %s).", type
, domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
645 log_debug("Resolving service type %s (family %s, interface %s).", domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
647 r
= sd_bus_message_new_method_call(
650 "org.freedesktop.resolve1",
651 "/org/freedesktop/resolve1",
652 "org.freedesktop.resolve1.Manager",
655 return bus_log_create_error(r
);
657 r
= sd_bus_message_append(req
, "isssit", arg_ifindex
, name
, type
, domain
, arg_family
, arg_flags
);
659 return bus_log_create_error(r
);
661 ts
= now(CLOCK_MONOTONIC
);
663 r
= sd_bus_call(bus
, req
, SD_RESOLVED_QUERY_TIMEOUT_USEC
, &error
, &reply
);
665 return log_error_errno(r
, "Resolve call failed: %s", bus_error_message(&error
, r
));
667 ts
= now(CLOCK_MONOTONIC
) - ts
;
669 r
= sd_bus_message_enter_container(reply
, 'a', "(qqqsa(iiay)s)");
671 return bus_log_parse_error(r
);
674 (name
? strlen(name
) + 1 : 0) +
675 (type
? strlen(type
) + 1 : 0) +
679 while ((r
= sd_bus_message_enter_container(reply
, 'r', "qqqsa(iiay)s")) > 0) {
680 uint16_t priority
, weight
, port
;
681 const char *hostname
, *canonical
;
683 r
= sd_bus_message_read(reply
, "qqqs", &priority
, &weight
, &port
, &hostname
);
685 return bus_log_parse_error(r
);
688 printf("%*s%s", (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? "/" : " ");
690 printf("%*s%s", (int) strlen(type
), c
== 0 ? type
: "", c
== 0 ? "/" : " ");
692 printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
693 (int) strlen(domain
), c
== 0 ? domain
: "",
698 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
700 return bus_log_parse_error(r
);
702 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
703 _cleanup_free_
char *pretty
= NULL
;
707 assert_cc(sizeof(int) == sizeof(int32_t));
709 r
= sd_bus_message_read(reply
, "ii", &ifindex
, &family
);
711 return bus_log_parse_error(r
);
713 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
715 return bus_log_parse_error(r
);
717 r
= sd_bus_message_exit_container(reply
);
719 return bus_log_parse_error(r
);
721 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
722 log_debug("%s: skipping entry with family %d (%s)", name
, family
, af_to_name(family
) ?: "unknown");
726 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
727 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name
, sz
, af_to_name(family
) ?: "unknown");
732 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
733 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
735 r
= in_addr_to_string(family
, a
, &pretty
);
737 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
739 printf("%*s%s%s%s\n", (int) indent
, "", pretty
, isempty(ifname
) ? "" : "%s", ifname
);
742 return bus_log_parse_error(r
);
744 r
= sd_bus_message_exit_container(reply
);
746 return bus_log_parse_error(r
);
748 r
= sd_bus_message_read(reply
, "s", &canonical
);
750 return bus_log_parse_error(r
);
752 if (!streq(hostname
, canonical
))
753 printf("%*s(%s)\n", (int) indent
, "", canonical
);
755 r
= sd_bus_message_exit_container(reply
);
757 return bus_log_parse_error(r
);
762 return bus_log_parse_error(r
);
764 r
= sd_bus_message_exit_container(reply
);
766 return bus_log_parse_error(r
);
768 r
= sd_bus_message_enter_container(reply
, 'a', "ay");
770 return bus_log_parse_error(r
);
772 while ((r
= sd_bus_message_read_array(reply
, 'y', (const void**) &p
, &sz
)) > 0) {
773 _cleanup_free_
char *escaped
= NULL
;
775 escaped
= cescape_length(p
, sz
);
779 printf("%*s%s\n", (int) indent
, "", escaped
);
782 return bus_log_parse_error(r
);
784 r
= sd_bus_message_exit_container(reply
);
786 return bus_log_parse_error(r
);
788 r
= sd_bus_message_read(reply
, "ssst", &canonical_name
, &canonical_type
, &canonical_domain
, &flags
);
790 return bus_log_parse_error(r
);
792 canonical_name
= empty_to_null(canonical_name
);
793 canonical_type
= empty_to_null(canonical_type
);
795 if (!streq_ptr(name
, canonical_name
) ||
796 !streq_ptr(type
, canonical_type
) ||
797 !streq_ptr(domain
, canonical_domain
)) {
799 printf("%*s(", (int) indent
, "");
802 printf("%s/", canonical_name
);
804 printf("%s/", canonical_type
);
806 printf("%s)\n", canonical_domain
);
809 print_source(flags
, ts
);
814 static int resolve_openpgp(sd_bus
*bus
, const char *address
) {
815 const char *domain
, *full
;
817 _cleanup_free_
char *hashed
= NULL
;
822 domain
= strrchr(address
, '@');
824 log_error("Address does not contain '@': \"%s\"", address
);
826 } else if (domain
== address
|| domain
[1] == '\0') {
827 log_error("Address starts or ends with '@': \"%s\"", address
);
832 r
= string_hashsum_sha256(address
, domain
- 1 - address
, &hashed
);
834 return log_error_errno(r
, "Hashing failed: %m");
836 strshorten(hashed
, 56);
838 full
= strjoina(hashed
, "._openpgpkey.", domain
);
839 log_debug("Looking up \"%s\".", full
);
841 r
= resolve_record(bus
, full
,
842 arg_class
?: DNS_CLASS_IN
,
843 arg_type
?: DNS_TYPE_OPENPGPKEY
, false);
845 if (IN_SET(r
, -ENXIO
, -ESRCH
)) { /* NXDOMAIN or NODATA? */
847 r
= string_hashsum_sha224(address
, domain
- 1 - address
, &hashed
);
849 return log_error_errno(r
, "Hashing failed: %m");
851 full
= strjoina(hashed
, "._openpgpkey.", domain
);
852 log_debug("Looking up \"%s\".", full
);
854 return resolve_record(bus
, full
,
855 arg_class
?: DNS_CLASS_IN
,
856 arg_type
?: DNS_TYPE_OPENPGPKEY
, true);
862 static int resolve_tlsa(sd_bus
*bus
, const char *address
) {
864 uint16_t port_num
= 443;
865 _cleanup_free_
char *full
= NULL
;
871 port
= strrchr(address
, ':');
873 r
= parse_ip_port(port
+ 1, &port_num
);
875 return log_error_errno(r
, "Invalid port \"%s\".", port
+ 1);
877 address
= strndupa(address
, port
- address
);
880 r
= asprintf(&full
, "_%u.%s.%s",
882 service_family_to_string(arg_service_family
),
887 log_debug("Looking up \"%s\".", full
);
889 return resolve_record(bus
, full
,
890 arg_class
?: DNS_CLASS_IN
,
891 arg_type
?: DNS_TYPE_TLSA
, true);
894 static int show_statistics(sd_bus
*bus
) {
895 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
896 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
897 uint64_t n_current_transactions
, n_total_transactions
,
898 cache_size
, n_cache_hit
, n_cache_miss
,
899 n_dnssec_secure
, n_dnssec_insecure
, n_dnssec_bogus
, n_dnssec_indeterminate
;
900 int r
, dnssec_supported
;
904 r
= sd_bus_get_property_trivial(bus
,
905 "org.freedesktop.resolve1",
906 "/org/freedesktop/resolve1",
907 "org.freedesktop.resolve1.Manager",
913 return log_error_errno(r
, "Failed to get DNSSEC supported state: %s", bus_error_message(&error
, r
));
915 printf("DNSSEC supported by current servers: %s%s%s\n\n",
917 yes_no(dnssec_supported
),
920 r
= sd_bus_get_property(bus
,
921 "org.freedesktop.resolve1",
922 "/org/freedesktop/resolve1",
923 "org.freedesktop.resolve1.Manager",
924 "TransactionStatistics",
929 return log_error_errno(r
, "Failed to get transaction statistics: %s", bus_error_message(&error
, r
));
931 r
= sd_bus_message_read(reply
, "(tt)",
932 &n_current_transactions
,
933 &n_total_transactions
);
935 return bus_log_parse_error(r
);
937 printf("%sTransactions%s\n"
938 "Current Transactions: %" PRIu64
"\n"
939 " Total Transactions: %" PRIu64
"\n",
942 n_current_transactions
,
943 n_total_transactions
);
945 reply
= sd_bus_message_unref(reply
);
947 r
= sd_bus_get_property(bus
,
948 "org.freedesktop.resolve1",
949 "/org/freedesktop/resolve1",
950 "org.freedesktop.resolve1.Manager",
956 return log_error_errno(r
, "Failed to get cache statistics: %s", bus_error_message(&error
, r
));
958 r
= sd_bus_message_read(reply
, "(ttt)",
963 return bus_log_parse_error(r
);
965 printf("\n%sCache%s\n"
966 " Current Cache Size: %" PRIu64
"\n"
967 " Cache Hits: %" PRIu64
"\n"
968 " Cache Misses: %" PRIu64
"\n",
975 reply
= sd_bus_message_unref(reply
);
977 r
= sd_bus_get_property(bus
,
978 "org.freedesktop.resolve1",
979 "/org/freedesktop/resolve1",
980 "org.freedesktop.resolve1.Manager",
986 return log_error_errno(r
, "Failed to get DNSSEC statistics: %s", bus_error_message(&error
, r
));
988 r
= sd_bus_message_read(reply
, "(tttt)",
992 &n_dnssec_indeterminate
);
994 return bus_log_parse_error(r
);
996 printf("\n%sDNSSEC Verdicts%s\n"
997 " Secure: %" PRIu64
"\n"
998 " Insecure: %" PRIu64
"\n"
999 " Bogus: %" PRIu64
"\n"
1000 " Indeterminate: %" PRIu64
"\n",
1006 n_dnssec_indeterminate
);
1011 static int reset_statistics(sd_bus
*bus
) {
1012 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1015 r
= sd_bus_call_method(bus
,
1016 "org.freedesktop.resolve1",
1017 "/org/freedesktop/resolve1",
1018 "org.freedesktop.resolve1.Manager",
1024 return log_error_errno(r
, "Failed to reset statistics: %s", bus_error_message(&error
, r
));
1029 static int flush_caches(sd_bus
*bus
) {
1030 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1033 r
= sd_bus_call_method(bus
,
1034 "org.freedesktop.resolve1",
1035 "/org/freedesktop/resolve1",
1036 "org.freedesktop.resolve1.Manager",
1042 return log_error_errno(r
, "Failed to flush caches: %s", bus_error_message(&error
, r
));
1047 static int reset_server_features(sd_bus
*bus
) {
1048 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1051 r
= sd_bus_call_method(bus
,
1052 "org.freedesktop.resolve1",
1053 "/org/freedesktop/resolve1",
1054 "org.freedesktop.resolve1.Manager",
1055 "ResetServerFeatures",
1060 return log_error_errno(r
, "Failed to reset server features: %s", bus_error_message(&error
, r
));
1065 static int map_link_dns_servers(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1066 char ***l
= userdata
;
1074 r
= sd_bus_message_enter_container(m
, 'a', "(iay)");
1084 r
= sd_bus_message_enter_container(m
, 'r', "iay");
1090 r
= sd_bus_message_read(m
, "i", &family
);
1094 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
1098 r
= sd_bus_message_exit_container(m
);
1102 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
1103 log_debug("Unexpected family, ignoring.");
1107 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
1108 log_debug("Address size mismatch, ignoring.");
1112 r
= in_addr_to_string(family
, a
, &pretty
);
1116 r
= strv_consume(l
, pretty
);
1121 r
= sd_bus_message_exit_container(m
);
1128 static int map_link_domains(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1129 char ***l
= userdata
;
1137 r
= sd_bus_message_enter_container(m
, 'a', "(sb)");
1146 r
= sd_bus_message_read(m
, "(sb)", &domain
, &route_only
);
1153 pretty
= strappend("~", domain
);
1155 pretty
= strdup(domain
);
1159 r
= strv_consume(l
, pretty
);
1164 r
= sd_bus_message_exit_container(m
);
1171 static int status_ifindex(sd_bus
*bus
, int ifindex
, const char *name
, bool *empty_line
) {
1174 uint64_t scopes_mask
;
1181 bool dnssec_supported
;
1184 static const struct bus_properties_map property_map
[] = {
1185 { "ScopesMask", "t", NULL
, offsetof(struct link_info
, scopes_mask
) },
1186 { "DNS", "a(iay)", map_link_dns_servers
, offsetof(struct link_info
, dns
) },
1187 { "Domains", "a(sb)", map_link_domains
, offsetof(struct link_info
, domains
) },
1188 { "LLMNR", "s", NULL
, offsetof(struct link_info
, llmnr
) },
1189 { "MulticastDNS", "s", NULL
, offsetof(struct link_info
, mdns
) },
1190 { "DNSSEC", "s", NULL
, offsetof(struct link_info
, dnssec
) },
1191 { "DNSSECNegativeTrustAnchors", "as", NULL
, offsetof(struct link_info
, ntas
) },
1192 { "DNSSECSupported", "b", NULL
, offsetof(struct link_info
, dnssec_supported
) },
1196 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1197 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1198 _cleanup_free_
char *ifi
= NULL
, *p
= NULL
;
1199 char ifname
[IF_NAMESIZE
] = "";
1204 assert(ifindex
> 0);
1208 if (!if_indextoname(ifindex
, ifname
))
1209 return log_error_errno(errno
, "Failed to resolve interface name for %i: %m", ifindex
);
1214 if (asprintf(&ifi
, "%i", ifindex
) < 0)
1217 r
= sd_bus_path_encode("/org/freedesktop/resolve1/link", ifi
, &p
);
1221 r
= bus_map_all_properties(bus
,
1222 "org.freedesktop.resolve1",
1225 BUS_MAP_BOOLEAN_AS_BOOL
,
1230 log_error_errno(r
, "Failed to get link data for %i: %s", ifindex
, bus_error_message(&error
, r
));
1234 (void) pager_open(arg_no_pager
, false);
1237 fputc('\n', stdout
);
1239 printf("%sLink %i (%s)%s\n",
1240 ansi_highlight(), ifindex
, name
, ansi_normal());
1242 if (link_info
.scopes_mask
== 0)
1243 printf(" Current Scopes: none\n");
1245 printf(" Current Scopes:%s%s%s%s%s\n",
1246 link_info
.scopes_mask
& SD_RESOLVED_DNS
? " DNS" : "",
1247 link_info
.scopes_mask
& SD_RESOLVED_LLMNR_IPV4
? " LLMNR/IPv4" : "",
1248 link_info
.scopes_mask
& SD_RESOLVED_LLMNR_IPV6
? " LLMNR/IPv6" : "",
1249 link_info
.scopes_mask
& SD_RESOLVED_MDNS_IPV4
? " mDNS/IPv4" : "",
1250 link_info
.scopes_mask
& SD_RESOLVED_MDNS_IPV6
? " mDNS/IPv6" : "");
1252 printf(" LLMNR setting: %s\n"
1253 "MulticastDNS setting: %s\n"
1254 " DNSSEC setting: %s\n"
1255 " DNSSEC supported: %s\n",
1256 strna(link_info
.llmnr
),
1257 strna(link_info
.mdns
),
1258 strna(link_info
.dnssec
),
1259 yes_no(link_info
.dnssec_supported
));
1261 STRV_FOREACH(i
, link_info
.dns
) {
1263 i
== link_info
.dns
? "DNS Servers:" : " ",
1267 STRV_FOREACH(i
, link_info
.domains
) {
1269 i
== link_info
.domains
? "DNS Domain:" : " ",
1273 STRV_FOREACH(i
, link_info
.ntas
) {
1275 i
== link_info
.ntas
? "DNSSEC NTA:" : " ",
1284 strv_free(link_info
.dns
);
1285 strv_free(link_info
.domains
);
1286 strv_free(link_info
.ntas
);
1290 static int map_global_dns_servers(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1291 char ***l
= userdata
;
1299 r
= sd_bus_message_enter_container(m
, 'a', "(iiay)");
1306 int family
, ifindex
;
1309 r
= sd_bus_message_enter_container(m
, 'r', "iiay");
1315 r
= sd_bus_message_read(m
, "ii", &ifindex
, &family
);
1319 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
1323 r
= sd_bus_message_exit_container(m
);
1327 if (ifindex
!= 0) /* only show the global ones here */
1330 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
1331 log_debug("Unexpected family, ignoring.");
1335 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
1336 log_debug("Address size mismatch, ignoring.");
1340 r
= in_addr_to_string(family
, a
, &pretty
);
1344 r
= strv_consume(l
, pretty
);
1349 r
= sd_bus_message_exit_container(m
);
1356 static int map_global_domains(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1357 char ***l
= userdata
;
1365 r
= sd_bus_message_enter_container(m
, 'a', "(isb)");
1371 int route_only
, ifindex
;
1374 r
= sd_bus_message_read(m
, "(isb)", &ifindex
, &domain
, &route_only
);
1380 if (ifindex
!= 0) /* only show the global ones here */
1384 pretty
= strappend("~", domain
);
1386 pretty
= strdup(domain
);
1390 r
= strv_consume(l
, pretty
);
1395 r
= sd_bus_message_exit_container(m
);
1402 static int status_global(sd_bus
*bus
, bool *empty_line
) {
1404 struct global_info
{
1411 bool dnssec_supported
;
1414 static const struct bus_properties_map property_map
[] = {
1415 { "DNS", "a(iiay)", map_global_dns_servers
, offsetof(struct global_info
, dns
) },
1416 { "Domains", "a(isb)", map_global_domains
, offsetof(struct global_info
, domains
) },
1417 { "DNSSECNegativeTrustAnchors", "as", NULL
, offsetof(struct global_info
, ntas
) },
1418 { "LLMNR", "s", NULL
, offsetof(struct global_info
, llmnr
) },
1419 { "MulticastDNS", "s", NULL
, offsetof(struct global_info
, mdns
) },
1420 { "DNSSEC", "s", NULL
, offsetof(struct global_info
, dnssec
) },
1421 { "DNSSECSupported", "b", NULL
, offsetof(struct global_info
, dnssec_supported
) },
1425 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1426 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1433 r
= bus_map_all_properties(bus
,
1434 "org.freedesktop.resolve1",
1435 "/org/freedesktop/resolve1",
1437 BUS_MAP_BOOLEAN_AS_BOOL
,
1442 log_error_errno(r
, "Failed to get global data: %s", bus_error_message(&error
, r
));
1446 (void) pager_open(arg_no_pager
, false);
1448 printf("%sGlobal%s\n", ansi_highlight(), ansi_normal());
1450 printf(" LLMNR setting: %s\n"
1451 "MulticastDNS setting: %s\n"
1452 " DNSSEC setting: %s\n"
1453 " DNSSEC supported: %s\n",
1454 strna(global_info
.llmnr
),
1455 strna(global_info
.mdns
),
1456 strna(global_info
.dnssec
),
1457 yes_no(global_info
.dnssec_supported
));
1459 STRV_FOREACH(i
, global_info
.dns
) {
1461 i
== global_info
.dns
? "DNS Servers:" : " ",
1465 STRV_FOREACH(i
, global_info
.domains
) {
1467 i
== global_info
.domains
? "DNS Domain:" : " ",
1471 strv_sort(global_info
.ntas
);
1472 STRV_FOREACH(i
, global_info
.ntas
) {
1474 i
== global_info
.ntas
? "DNSSEC NTA:" : " ",
1483 strv_free(global_info
.dns
);
1484 strv_free(global_info
.domains
);
1485 strv_free(global_info
.ntas
);
1490 static int status_all(sd_bus
*bus
) {
1491 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1492 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1493 sd_netlink_message
*i
;
1494 bool empty_line
= false;
1499 r
= status_global(bus
, &empty_line
);
1503 r
= sd_netlink_open(&rtnl
);
1505 return log_error_errno(r
, "Failed to connect to netlink: %m");
1507 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
1509 return rtnl_log_create_error(r
);
1511 r
= sd_netlink_message_request_dump(req
, true);
1513 return rtnl_log_create_error(r
);
1515 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
1517 return log_error_errno(r
, "Failed to enumerate links: %m");
1520 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
1525 q
= sd_netlink_message_get_type(i
, &type
);
1527 return rtnl_log_parse_error(q
);
1529 if (type
!= RTM_NEWLINK
)
1532 q
= sd_rtnl_message_link_get_ifindex(i
, &ifindex
);
1534 return rtnl_log_parse_error(q
);
1536 if (ifindex
== LOOPBACK_IFINDEX
)
1539 q
= sd_netlink_message_read_string(i
, IFLA_IFNAME
, &name
);
1541 return rtnl_log_parse_error(q
);
1543 q
= status_ifindex(bus
, ifindex
, name
, &empty_line
);
1544 if (q
< 0 && r
>= 0)
1551 static int set_link(sd_bus
*bus
) {
1552 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1557 if (arg_n_set_dns
> 0) {
1558 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
1561 q
= sd_bus_message_new_method_call(
1564 "org.freedesktop.resolve1",
1565 "/org/freedesktop/resolve1",
1566 "org.freedesktop.resolve1.Manager",
1569 return bus_log_create_error(q
);
1571 q
= sd_bus_message_append(req
, "i", arg_ifindex
);
1573 return bus_log_create_error(q
);
1575 q
= sd_bus_message_open_container(req
, 'a', "(iay)");
1577 return bus_log_create_error(q
);
1579 for (i
= 0; i
< arg_n_set_dns
; i
++) {
1580 q
= sd_bus_message_open_container(req
, 'r', "iay");
1582 return bus_log_create_error(q
);
1584 q
= sd_bus_message_append(req
, "i", arg_set_dns
[i
].family
);
1586 return bus_log_create_error(q
);
1588 q
= sd_bus_message_append_array(req
, 'y', &arg_set_dns
[i
].address
, FAMILY_ADDRESS_SIZE(arg_set_dns
[i
].family
));
1590 return bus_log_create_error(q
);
1592 q
= sd_bus_message_close_container(req
);
1594 return bus_log_create_error(q
);
1597 q
= sd_bus_message_close_container(req
);
1599 return bus_log_create_error(q
);
1601 q
= sd_bus_call(bus
, req
, 0, &error
, NULL
);
1603 if (sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
))
1605 if (arg_ifindex_permissive
&&
1606 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
1609 log_error_errno(q
, "Failed to set DNS configuration: %s", bus_error_message(&error
, q
));
1615 if (!strv_isempty(arg_set_domain
)) {
1616 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
1619 q
= sd_bus_message_new_method_call(
1622 "org.freedesktop.resolve1",
1623 "/org/freedesktop/resolve1",
1624 "org.freedesktop.resolve1.Manager",
1627 return bus_log_create_error(q
);
1629 q
= sd_bus_message_append(req
, "i", arg_ifindex
);
1631 return bus_log_create_error(q
);
1633 q
= sd_bus_message_open_container(req
, 'a', "(sb)");
1635 return bus_log_create_error(q
);
1637 STRV_FOREACH(p
, arg_set_domain
) {
1640 n
= **p
== '~' ? *p
+ 1 : *p
;
1641 q
= sd_bus_message_append(req
, "(sb)", n
, **p
== '~');
1643 return bus_log_create_error(q
);
1646 q
= sd_bus_message_close_container(req
);
1648 return bus_log_create_error(q
);
1650 q
= sd_bus_call(bus
, req
, 0, &error
, NULL
);
1652 if (sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
))
1654 if (arg_ifindex_permissive
&&
1655 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
1658 log_error_errno(q
, "Failed to set domain configuration: %s", bus_error_message(&error
, q
));
1664 if (arg_set_llmnr
) {
1665 q
= sd_bus_call_method(bus
,
1666 "org.freedesktop.resolve1",
1667 "/org/freedesktop/resolve1",
1668 "org.freedesktop.resolve1.Manager",
1672 "is", arg_ifindex
, arg_set_llmnr
);
1674 if (sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
))
1677 log_error_errno(q
, "Failed to set LLMNR configuration: %s", bus_error_message(&error
, q
));
1684 q
= sd_bus_call_method(bus
,
1685 "org.freedesktop.resolve1",
1686 "/org/freedesktop/resolve1",
1687 "org.freedesktop.resolve1.Manager",
1688 "SetLinkMulticastDNS",
1691 "is", arg_ifindex
, arg_set_mdns
);
1693 if (sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
))
1696 log_error_errno(q
, "Failed to set MulticastDNS configuration: %s", bus_error_message(&error
, q
));
1702 if (arg_set_dnssec
) {
1703 q
= sd_bus_call_method(bus
,
1704 "org.freedesktop.resolve1",
1705 "/org/freedesktop/resolve1",
1706 "org.freedesktop.resolve1.Manager",
1710 "is", arg_ifindex
, arg_set_dnssec
);
1712 if (sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
))
1715 log_error_errno(q
, "Failed to set DNSSEC configuration: %s", bus_error_message(&error
, q
));
1721 if (!strv_isempty(arg_set_nta
)) {
1722 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
1724 q
= sd_bus_message_new_method_call(
1727 "org.freedesktop.resolve1",
1728 "/org/freedesktop/resolve1",
1729 "org.freedesktop.resolve1.Manager",
1730 "SetLinkDNSSECNegativeTrustAnchors");
1732 return bus_log_create_error(q
);
1734 q
= sd_bus_message_append(req
, "i", arg_ifindex
);
1736 return bus_log_create_error(q
);
1738 q
= sd_bus_message_append_strv(req
, arg_set_nta
);
1740 return bus_log_create_error(q
);
1742 q
= sd_bus_call(bus
, req
, 0, &error
, NULL
);
1744 if (sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
))
1747 log_error_errno(q
, "Failed to set DNSSEC NTA configuration: %s", bus_error_message(&error
, q
));
1757 char ifname
[IFNAMSIZ
];
1759 return log_error_errno(q
,
1760 "The specified interface %s is managed by systemd-networkd. Operation refused.\n"
1761 "Please configure DNS settings for systemd-networkd managed interfaces directly in their .network files.", strna(if_indextoname(arg_ifindex
, ifname
)));
1765 static int revert_link(sd_bus
*bus
) {
1766 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1771 r
= sd_bus_call_method(bus
,
1772 "org.freedesktop.resolve1",
1773 "/org/freedesktop/resolve1",
1774 "org.freedesktop.resolve1.Manager",
1780 if (arg_ifindex_permissive
&&
1781 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
1784 return log_error_errno(r
, "Failed to revert interface configuration: %s", bus_error_message(&error
, r
));
1790 static void help_protocol_types(void) {
1792 puts("Known protocol types:");
1793 puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6\nmdns\nmdns-ipv4\nmdns-ipv6");
1796 static void help_dns_types(void) {
1801 puts("Known DNS RR types:");
1802 for (i
= 0; i
< _DNS_TYPE_MAX
; i
++) {
1803 t
= dns_type_to_string(i
);
1809 static void help_dns_classes(void) {
1814 puts("Known DNS RR classes:");
1815 for (i
= 0; i
< _DNS_CLASS_MAX
; i
++) {
1816 t
= dns_class_to_string(i
);
1822 static void native_help(void) {
1823 printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n"
1824 "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n"
1825 "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n"
1826 "%1$s [OPTIONS...] --statistics\n"
1827 "%1$s [OPTIONS...] --reset-statistics\n"
1829 "Resolve domain names, IPv4 and IPv6 addresses, DNS records, and services.\n\n"
1830 " -h --help Show this help\n"
1831 " --version Show package version\n"
1832 " --no-pager Do not pipe output into a pager\n"
1833 " -4 Resolve IPv4 addresses\n"
1834 " -6 Resolve IPv6 addresses\n"
1835 " -i --interface=INTERFACE Look on interface\n"
1836 " -p --protocol=PROTO|help Look via protocol\n"
1837 " -t --type=TYPE|help Query RR with DNS type\n"
1838 " -c --class=CLASS|help Query RR with DNS class\n"
1839 " --service Resolve service (SRV)\n"
1840 " --service-address=BOOL Resolve address for services (default: yes)\n"
1841 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
1842 " --openpgp Query OpenPGP public key\n"
1843 " --tlsa Query TLS public key\n"
1844 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
1845 " --search=BOOL Use search domains for single-label names\n"
1847 " --raw[=payload|packet] Dump the answer as binary data\n"
1848 " --legend=BOOL Print headers and additional info (default: yes)\n"
1849 " --statistics Show resolver statistics\n"
1850 " --reset-statistics Reset resolver statistics\n"
1851 " --status Show link and server status\n"
1852 " --flush-caches Flush all local DNS caches\n"
1853 " --reset-server-features\n"
1854 " Forget learnt DNS server feature levels\n"
1855 " --set-dns=SERVER Set per-interface DNS server address\n"
1856 " --set-domain=DOMAIN Set per-interface search domain\n"
1857 " --set-llmnr=MODE Set per-interface LLMNR mode\n"
1858 " --set-mdns=MODE Set per-interface MulticastDNS mode\n"
1859 " --set-dnssec=MODE Set per-interface DNSSEC mode\n"
1860 " --set-nta=DOMAIN Set per-interface DNSSEC NTA\n"
1861 " --revert Revert per-interface configuration\n"
1862 , program_invocation_short_name
);
1865 static int native_parse_argv(int argc
, char *argv
[]) {
1867 ARG_VERSION
= 0x100,
1871 ARG_SERVICE_ADDRESS
,
1878 ARG_RESET_STATISTICS
,
1881 ARG_RESET_SERVER_FEATURES
,
1892 static const struct option options
[] = {
1893 { "help", no_argument
, NULL
, 'h' },
1894 { "version", no_argument
, NULL
, ARG_VERSION
},
1895 { "type", required_argument
, NULL
, 't' },
1896 { "class", required_argument
, NULL
, 'c' },
1897 { "legend", required_argument
, NULL
, ARG_LEGEND
},
1898 { "interface", required_argument
, NULL
, 'i' },
1899 { "protocol", required_argument
, NULL
, 'p' },
1900 { "cname", required_argument
, NULL
, ARG_CNAME
},
1901 { "service", no_argument
, NULL
, ARG_SERVICE
},
1902 { "service-address", required_argument
, NULL
, ARG_SERVICE_ADDRESS
},
1903 { "service-txt", required_argument
, NULL
, ARG_SERVICE_TXT
},
1904 { "openpgp", no_argument
, NULL
, ARG_OPENPGP
},
1905 { "tlsa", optional_argument
, NULL
, ARG_TLSA
},
1906 { "raw", optional_argument
, NULL
, ARG_RAW
},
1907 { "search", required_argument
, NULL
, ARG_SEARCH
},
1908 { "statistics", no_argument
, NULL
, ARG_STATISTICS
, },
1909 { "reset-statistics", no_argument
, NULL
, ARG_RESET_STATISTICS
},
1910 { "status", no_argument
, NULL
, ARG_STATUS
},
1911 { "flush-caches", no_argument
, NULL
, ARG_FLUSH_CACHES
},
1912 { "reset-server-features", no_argument
, NULL
, ARG_RESET_SERVER_FEATURES
},
1913 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1914 { "set-dns", required_argument
, NULL
, ARG_SET_DNS
},
1915 { "set-domain", required_argument
, NULL
, ARG_SET_DOMAIN
},
1916 { "set-llmnr", required_argument
, NULL
, ARG_SET_LLMNR
},
1917 { "set-mdns", required_argument
, NULL
, ARG_SET_MDNS
},
1918 { "set-dnssec", required_argument
, NULL
, ARG_SET_DNSSEC
},
1919 { "set-nta", required_argument
, NULL
, ARG_SET_NTA
},
1920 { "revert", no_argument
, NULL
, ARG_REVERT_LINK
},
1929 while ((c
= getopt_long(argc
, argv
, "h46i:t:c:p:", options
, NULL
)) >= 0)
1934 return 0; /* done */;
1940 arg_family
= AF_INET
;
1944 arg_family
= AF_INET6
;
1948 if (parse_ifindex(optarg
, &arg_ifindex
) < 0) {
1951 ifi
= if_nametoindex(optarg
);
1953 return log_error_errno(errno
, "Unknown interface %s: %m", optarg
);
1961 if (streq(optarg
, "help")) {
1966 r
= dns_type_from_string(optarg
);
1968 log_error("Failed to parse RR record type %s", optarg
);
1971 arg_type
= (uint16_t) r
;
1972 assert((int) arg_type
== r
);
1974 arg_mode
= MODE_RESOLVE_RECORD
;
1978 if (streq(optarg
, "help")) {
1983 r
= dns_class_from_string(optarg
);
1985 log_error("Failed to parse RR record class %s", optarg
);
1988 arg_class
= (uint16_t) r
;
1989 assert((int) arg_class
== r
);
1994 r
= parse_boolean(optarg
);
1996 return log_error_errno(r
, "Failed to parse --legend= argument");
2002 if (streq(optarg
, "help")) {
2003 help_protocol_types();
2005 } else if (streq(optarg
, "dns"))
2006 arg_flags
|= SD_RESOLVED_DNS
;
2007 else if (streq(optarg
, "llmnr"))
2008 arg_flags
|= SD_RESOLVED_LLMNR
;
2009 else if (streq(optarg
, "llmnr-ipv4"))
2010 arg_flags
|= SD_RESOLVED_LLMNR_IPV4
;
2011 else if (streq(optarg
, "llmnr-ipv6"))
2012 arg_flags
|= SD_RESOLVED_LLMNR_IPV6
;
2013 else if (streq(optarg
, "mdns"))
2014 arg_flags
|= SD_RESOLVED_MDNS
;
2015 else if (streq(optarg
, "mdns-ipv4"))
2016 arg_flags
|= SD_RESOLVED_MDNS_IPV4
;
2017 else if (streq(optarg
, "mdns-ipv6"))
2018 arg_flags
|= SD_RESOLVED_MDNS_IPV6
;
2020 log_error("Unknown protocol specifier: %s", optarg
);
2027 arg_mode
= MODE_RESOLVE_SERVICE
;
2031 arg_mode
= MODE_RESOLVE_OPENPGP
;
2035 arg_mode
= MODE_RESOLVE_TLSA
;
2036 arg_service_family
= service_family_from_string(optarg
);
2037 if (arg_service_family
< 0) {
2038 log_error("Unknown service family \"%s\".", optarg
);
2045 log_error("Refusing to write binary data to tty.");
2049 if (optarg
== NULL
|| streq(optarg
, "payload"))
2050 arg_raw
= RAW_PAYLOAD
;
2051 else if (streq(optarg
, "packet"))
2052 arg_raw
= RAW_PACKET
;
2054 log_error("Unknown --raw specifier \"%s\".", optarg
);
2062 r
= parse_boolean(optarg
);
2064 return log_error_errno(r
, "Failed to parse --cname= argument.");
2065 SET_FLAG(arg_flags
, SD_RESOLVED_NO_CNAME
, r
== 0);
2068 case ARG_SERVICE_ADDRESS
:
2069 r
= parse_boolean(optarg
);
2071 return log_error_errno(r
, "Failed to parse --service-address= argument.");
2072 SET_FLAG(arg_flags
, SD_RESOLVED_NO_ADDRESS
, r
== 0);
2075 case ARG_SERVICE_TXT
:
2076 r
= parse_boolean(optarg
);
2078 return log_error_errno(r
, "Failed to parse --service-txt= argument.");
2079 SET_FLAG(arg_flags
, SD_RESOLVED_NO_TXT
, r
== 0);
2083 r
= parse_boolean(optarg
);
2085 return log_error_errno(r
, "Failed to parse --search argument.");
2086 SET_FLAG(arg_flags
, SD_RESOLVED_NO_SEARCH
, r
== 0);
2089 case ARG_STATISTICS
:
2090 arg_mode
= MODE_STATISTICS
;
2093 case ARG_RESET_STATISTICS
:
2094 arg_mode
= MODE_RESET_STATISTICS
;
2097 case ARG_FLUSH_CACHES
:
2098 arg_mode
= MODE_FLUSH_CACHES
;
2101 case ARG_RESET_SERVER_FEATURES
:
2102 arg_mode
= MODE_RESET_SERVER_FEATURES
;
2106 arg_mode
= MODE_STATUS
;
2110 arg_no_pager
= true;
2114 struct in_addr_data data
, *n
;
2116 r
= in_addr_from_string_auto(optarg
, &data
.family
, &data
.address
);
2118 return log_error_errno(r
, "Failed to parse DNS server address: %s", optarg
);
2120 n
= reallocarray(arg_set_dns
, arg_n_set_dns
+ 1, sizeof(struct in_addr_data
));
2125 arg_set_dns
[arg_n_set_dns
++] = data
;
2126 arg_mode
= MODE_SET_LINK
;
2130 case ARG_SET_DOMAIN
: {
2133 p
= optarg
[0] == '~' ? optarg
+ 1 : optarg
;
2135 r
= dns_name_is_valid(p
);
2137 return log_error_errno(r
, "Failed to validate specified domain %s: %m", p
);
2139 log_error("Domain not valid: %s", p
);
2143 r
= strv_extend(&arg_set_domain
, optarg
);
2147 arg_mode
= MODE_SET_LINK
;
2152 r
= free_and_strdup(&arg_set_llmnr
, optarg
);
2156 arg_mode
= MODE_SET_LINK
;
2160 r
= free_and_strdup(&arg_set_mdns
, optarg
);
2164 arg_mode
= MODE_SET_LINK
;
2167 case ARG_SET_DNSSEC
:
2168 r
= free_and_strdup(&arg_set_dnssec
, optarg
);
2172 arg_mode
= MODE_SET_LINK
;
2176 r
= dns_name_is_valid(optarg
);
2178 return log_error_errno(r
, "Failed to validate specified domain %s: %m", optarg
);
2180 log_error("Domain not valid: %s", optarg
);
2184 r
= strv_extend(&arg_set_nta
, optarg
);
2188 arg_mode
= MODE_SET_LINK
;
2191 case ARG_REVERT_LINK
:
2192 arg_mode
= MODE_REVERT_LINK
;
2199 assert_not_reached("Unhandled option");
2202 if (arg_type
== 0 && arg_class
!= 0) {
2203 log_error("--class= may only be used in conjunction with --type=.");
2207 if (arg_type
!= 0 && arg_mode
== MODE_RESOLVE_SERVICE
) {
2208 log_error("--service and --type= may not be combined.");
2212 if (arg_type
!= 0 && arg_class
== 0)
2213 arg_class
= DNS_CLASS_IN
;
2215 if (arg_class
!= 0 && arg_type
== 0)
2216 arg_type
= DNS_TYPE_A
;
2218 if (IN_SET(arg_mode
, MODE_SET_LINK
, MODE_REVERT_LINK
)) {
2220 if (arg_ifindex
<= 0) {
2221 log_error("--set-dns=, --set-domain=, --set-llmnr=, --set-mdns=, --set-dnssec=, --set-nta= and --revert require --interface=.");
2225 if (arg_ifindex
== LOOPBACK_IFINDEX
) {
2226 log_error("Interface can't be the loopback interface (lo). Sorry.");
2231 return 1 /* work to do */;
2234 int main(int argc
, char **argv
) {
2235 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2238 log_parse_environment();
2241 if (streq(program_invocation_short_name
, "resolvconf"))
2242 r
= resolvconf_parse_argv(argc
, argv
);
2244 r
= native_parse_argv(argc
, argv
);
2248 r
= sd_bus_open_system(&bus
);
2250 log_error_errno(r
, "sd_bus_open_system: %m");
2256 case MODE_RESOLVE_HOST
:
2257 if (optind
>= argc
) {
2258 log_error("No arguments passed.");
2263 while (argv
[optind
]) {
2264 int family
, ifindex
, k
;
2265 union in_addr_union a
;
2267 if (startswith(argv
[optind
], "dns:"))
2268 k
= resolve_rfc4501(bus
, argv
[optind
]);
2270 k
= in_addr_ifindex_from_string_auto(argv
[optind
], &family
, &a
, &ifindex
);
2272 k
= resolve_address(bus
, family
, &a
, ifindex
);
2274 k
= resolve_host(bus
, argv
[optind
]);
2284 case MODE_RESOLVE_RECORD
:
2285 if (optind
>= argc
) {
2286 log_error("No arguments passed.");
2291 while (argv
[optind
]) {
2294 k
= resolve_record(bus
, argv
[optind
], arg_class
, arg_type
, true);
2302 case MODE_RESOLVE_SERVICE
:
2303 if (argc
< optind
+ 1) {
2304 log_error("Domain specification required.");
2308 } else if (argc
== optind
+ 1)
2309 r
= resolve_service(bus
, NULL
, NULL
, argv
[optind
]);
2310 else if (argc
== optind
+ 2)
2311 r
= resolve_service(bus
, NULL
, argv
[optind
], argv
[optind
+1]);
2312 else if (argc
== optind
+ 3)
2313 r
= resolve_service(bus
, argv
[optind
], argv
[optind
+1], argv
[optind
+2]);
2315 log_error("Too many arguments.");
2322 case MODE_RESOLVE_OPENPGP
:
2323 if (argc
< optind
+ 1) {
2324 log_error("E-mail address required.");
2331 while (optind
< argc
) {
2334 k
= resolve_openpgp(bus
, argv
[optind
++]);
2340 case MODE_RESOLVE_TLSA
:
2341 if (argc
< optind
+ 1) {
2342 log_error("Domain name required.");
2349 while (optind
< argc
) {
2352 k
= resolve_tlsa(bus
, argv
[optind
++]);
2358 case MODE_STATISTICS
:
2359 if (argc
> optind
) {
2360 log_error("Too many arguments.");
2365 r
= show_statistics(bus
);
2368 case MODE_RESET_STATISTICS
:
2369 if (argc
> optind
) {
2370 log_error("Too many arguments.");
2375 r
= reset_statistics(bus
);
2378 case MODE_FLUSH_CACHES
:
2379 if (argc
> optind
) {
2380 log_error("Too many arguments.");
2385 r
= flush_caches(bus
);
2388 case MODE_RESET_SERVER_FEATURES
:
2389 if (argc
> optind
) {
2390 log_error("Too many arguments.");
2395 r
= reset_server_features(bus
);
2400 if (argc
> optind
) {
2402 bool empty_line
= false;
2405 STRV_FOREACH(ifname
, argv
+ optind
) {
2408 q
= parse_ifindex(*ifname
, &ifindex
);
2410 ifindex
= if_nametoindex(*ifname
);
2412 log_error_errno(errno
, "Failed to resolve interface name '%s': %m", *ifname
);
2417 q
= status_ifindex(bus
, ifindex
, NULL
, &empty_line
);
2418 if (q
< 0 && r
>= 0)
2422 r
= status_all(bus
);
2427 if (argc
> optind
) {
2428 log_error("Too many arguments.");
2436 case MODE_REVERT_LINK
:
2437 if (argc
> optind
) {
2438 log_error("Too many arguments.");
2443 r
= revert_link(bus
);
2447 assert_not_reached("invalid mode");
2454 strv_free(arg_set_domain
);
2455 free(arg_set_llmnr
);
2457 free(arg_set_dnssec
);
2458 strv_free(arg_set_nta
);
2460 return r
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;