2 This file is part of systemd.
4 Copyright 2014 Zbigniew Jędrzejewski-Szmek
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/>.
26 #include "alloc-util.h"
27 #include "bus-error.h"
30 #include "in-addr-util.h"
31 #include "gcrypt-util.h"
32 #include "parse-util.h"
33 #include "resolved-def.h"
34 #include "resolved-dns-packet.h"
35 #include "terminal-util.h"
37 #define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC)
39 static int arg_family
= AF_UNSPEC
;
40 static int arg_ifindex
= 0;
41 static uint16_t arg_type
= 0;
42 static uint16_t arg_class
= 0;
43 static bool arg_legend
= true;
44 static uint64_t arg_flags
= 0;
46 typedef enum ServiceFamily
{
50 _SERVICE_FAMILY_INVALID
= -1,
52 static ServiceFamily arg_service_family
= SERVICE_FAMILY_TCP
;
54 typedef enum RawType
{
59 static RawType arg_raw
= RAW_NONE
;
68 MODE_RESET_STATISTICS
,
69 } arg_mode
= MODE_RESOLVE_HOST
;
71 static ServiceFamily
service_family_from_string(const char *s
) {
72 if (s
== NULL
|| streq(s
, "tcp"))
73 return SERVICE_FAMILY_TCP
;
75 return SERVICE_FAMILY_UDP
;
77 return SERVICE_FAMILY_SCTP
;
78 return _SERVICE_FAMILY_INVALID
;
81 static const char* service_family_to_string(ServiceFamily service
) {
83 case SERVICE_FAMILY_TCP
:
85 case SERVICE_FAMILY_UDP
:
87 case SERVICE_FAMILY_SCTP
:
90 assert_not_reached("invalid service");
94 static void print_source(uint64_t flags
, usec_t rtt
) {
95 char rtt_str
[FORMAT_TIMESTAMP_MAX
];
103 fputs("\n-- Information acquired via", stdout
);
106 printf(" protocol%s%s%s%s%s",
107 flags
& SD_RESOLVED_DNS
? " DNS" :"",
108 flags
& SD_RESOLVED_LLMNR_IPV4
? " LLMNR/IPv4" : "",
109 flags
& SD_RESOLVED_LLMNR_IPV6
? " LLMNR/IPv6" : "",
110 flags
& SD_RESOLVED_MDNS_IPV4
? "mDNS/IPv4" : "",
111 flags
& SD_RESOLVED_MDNS_IPV6
? "mDNS/IPv6" : "");
113 assert_se(format_timespan(rtt_str
, sizeof(rtt_str
), rtt
, 100));
115 printf(" in %s", rtt_str
);
120 printf("-- Data is authenticated: %s\n", yes_no(flags
& SD_RESOLVED_AUTHENTICATED
));
123 static int resolve_host(sd_bus
*bus
, const char *name
) {
125 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
126 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
127 const char *canonical
= NULL
;
128 char ifname
[IF_NAMESIZE
] = "";
136 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
137 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
139 log_debug("Resolving %s (family %s, interface %s).", name
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
141 r
= sd_bus_message_new_method_call(
144 "org.freedesktop.resolve1",
145 "/org/freedesktop/resolve1",
146 "org.freedesktop.resolve1.Manager",
149 return bus_log_create_error(r
);
151 r
= sd_bus_message_append(req
, "isit", arg_ifindex
, name
, arg_family
, arg_flags
);
153 return bus_log_create_error(r
);
155 ts
= now(CLOCK_MONOTONIC
);
157 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
159 return log_error_errno(r
, "%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
161 ts
= now(CLOCK_MONOTONIC
) - ts
;
163 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
165 return bus_log_parse_error(r
);
167 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
168 _cleanup_free_
char *pretty
= NULL
;
173 assert_cc(sizeof(int) == sizeof(int32_t));
175 r
= sd_bus_message_read(reply
, "ii", &ifindex
, &family
);
177 return bus_log_parse_error(r
);
179 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
181 return bus_log_parse_error(r
);
183 r
= sd_bus_message_exit_container(reply
);
185 return bus_log_parse_error(r
);
187 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
188 log_debug("%s: skipping entry with family %d (%s)", name
, family
, af_to_name(family
) ?: "unknown");
192 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
193 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name
, sz
, af_to_name(family
) ?: "unknown");
198 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
199 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
201 r
= in_addr_to_string(family
, a
, &pretty
);
203 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
205 printf("%*s%s %s%s%s\n",
206 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
208 isempty(ifname
) ? "" : "%", ifname
);
213 return bus_log_parse_error(r
);
215 r
= sd_bus_message_exit_container(reply
);
217 return bus_log_parse_error(r
);
219 r
= sd_bus_message_read(reply
, "st", &canonical
, &flags
);
221 return bus_log_parse_error(r
);
223 if (!streq(name
, canonical
))
224 printf("%*s%s (%s)\n",
225 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
229 log_error("%s: no addresses found", name
);
233 print_source(flags
, ts
);
238 static int resolve_address(sd_bus
*bus
, int family
, const union in_addr_union
*address
, int ifindex
) {
239 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
240 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
241 _cleanup_free_
char *pretty
= NULL
;
242 char ifname
[IF_NAMESIZE
] = "";
249 assert(IN_SET(family
, AF_INET
, AF_INET6
));
253 ifindex
= arg_ifindex
;
255 r
= in_addr_to_string(family
, address
, &pretty
);
259 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
260 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
262 log_debug("Resolving %s%s%s.", pretty
, isempty(ifname
) ? "" : "%", ifname
);
264 r
= sd_bus_message_new_method_call(
267 "org.freedesktop.resolve1",
268 "/org/freedesktop/resolve1",
269 "org.freedesktop.resolve1.Manager",
272 return bus_log_create_error(r
);
274 r
= sd_bus_message_append(req
, "ii", ifindex
, family
);
276 return bus_log_create_error(r
);
278 r
= sd_bus_message_append_array(req
, 'y', address
, FAMILY_ADDRESS_SIZE(family
));
280 return bus_log_create_error(r
);
282 r
= sd_bus_message_append(req
, "t", arg_flags
);
284 return bus_log_create_error(r
);
286 ts
= now(CLOCK_MONOTONIC
);
288 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
290 log_error("%s: resolve call failed: %s", pretty
, bus_error_message(&error
, r
));
294 ts
= now(CLOCK_MONOTONIC
) - ts
;
296 r
= sd_bus_message_enter_container(reply
, 'a', "(is)");
298 return bus_log_create_error(r
);
300 while ((r
= sd_bus_message_enter_container(reply
, 'r', "is")) > 0) {
303 assert_cc(sizeof(int) == sizeof(int32_t));
305 r
= sd_bus_message_read(reply
, "is", &ifindex
, &n
);
309 r
= sd_bus_message_exit_container(reply
);
314 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
315 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
317 printf("%*s%*s%*s%s %s\n",
318 (int) strlen(pretty
), c
== 0 ? pretty
: "",
319 isempty(ifname
) ? 0 : 1, c
> 0 || isempty(ifname
) ? "" : "%",
320 (int) strlen(ifname
), c
== 0 ? ifname
: "",
327 return bus_log_parse_error(r
);
329 r
= sd_bus_message_exit_container(reply
);
331 return bus_log_parse_error(r
);
333 r
= sd_bus_message_read(reply
, "t", &flags
);
335 return bus_log_parse_error(r
);
338 log_error("%s: no names found", pretty
);
342 print_source(flags
, ts
);
347 static int parse_address(const char *s
, int *family
, union in_addr_union
*address
, int *ifindex
) {
348 const char *percent
, *a
;
352 percent
= strchr(s
, '%');
354 if (parse_ifindex(percent
+1, &ifi
) < 0) {
355 ifi
= if_nametoindex(percent
+1);
360 a
= strndupa(s
, percent
- s
);
364 r
= in_addr_from_string_auto(a
, family
, address
);
372 static int output_rr_packet(const void *d
, size_t l
, int ifindex
) {
373 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
374 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
376 char ifname
[IF_NAMESIZE
] = "";
378 r
= dns_packet_new(&p
, DNS_PROTOCOL_DNS
, 0);
382 p
->refuse_compression
= true;
384 r
= dns_packet_append_blob(p
, d
, l
, NULL
);
388 r
= dns_packet_read_rr(p
, &rr
, NULL
, NULL
);
390 return log_error_errno(r
, "Failed to parse RR: %m");
392 if (arg_raw
== RAW_PAYLOAD
) {
396 k
= dns_resource_record_payload(rr
, &data
);
398 return log_error_errno(k
, "Cannot dump RR: %m");
399 fwrite(data
, 1, k
, stdout
);
403 s
= dns_resource_record_to_string(rr
);
407 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
408 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
410 printf("%s%s%s\n", s
, isempty(ifname
) ? "" : " # interface ", ifname
);
416 static int resolve_record(sd_bus
*bus
, const char *name
, uint16_t class, uint16_t type
) {
417 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
418 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
419 char ifname
[IF_NAMESIZE
] = "";
424 bool needs_authentication
= false;
428 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
429 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
431 log_debug("Resolving %s %s %s (interface %s).", name
, dns_class_to_string(class), dns_type_to_string(type
), isempty(ifname
) ? "*" : ifname
);
433 r
= sd_bus_message_new_method_call(
436 "org.freedesktop.resolve1",
437 "/org/freedesktop/resolve1",
438 "org.freedesktop.resolve1.Manager",
441 return bus_log_create_error(r
);
443 r
= sd_bus_message_append(req
, "isqqt", arg_ifindex
, name
, class, type
, arg_flags
);
445 return bus_log_create_error(r
);
447 ts
= now(CLOCK_MONOTONIC
);
449 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
451 log_error("%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
455 ts
= now(CLOCK_MONOTONIC
) - ts
;
457 r
= sd_bus_message_enter_container(reply
, 'a', "(iqqay)");
459 return bus_log_parse_error(r
);
461 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iqqay")) > 0) {
467 assert_cc(sizeof(int) == sizeof(int32_t));
469 r
= sd_bus_message_read(reply
, "iqq", &ifindex
, &c
, &t
);
471 return bus_log_parse_error(r
);
473 r
= sd_bus_message_read_array(reply
, 'y', &d
, &l
);
475 return bus_log_parse_error(r
);
477 r
= sd_bus_message_exit_container(reply
);
479 return bus_log_parse_error(r
);
481 if (arg_raw
== RAW_PACKET
) {
482 uint64_t u64
= htole64(l
);
484 fwrite(&u64
, sizeof(u64
), 1, stdout
);
485 fwrite(d
, 1, l
, stdout
);
487 r
= output_rr_packet(d
, l
, ifindex
);
492 if (dns_type_needs_authentication(t
))
493 needs_authentication
= true;
498 return bus_log_parse_error(r
);
500 r
= sd_bus_message_exit_container(reply
);
502 return bus_log_parse_error(r
);
504 r
= sd_bus_message_read(reply
, "t", &flags
);
506 return bus_log_parse_error(r
);
509 log_error("%s: no records found", name
);
513 print_source(flags
, ts
);
515 if ((flags
& SD_RESOLVED_AUTHENTICATED
) == 0 && needs_authentication
) {
518 fprintf(stderr
, "\n%s"
519 "WARNING: The resources shown contain cryptographic key data which could not be\n"
520 " authenticated. It is not suitable to authenticate any communication.\n"
521 " This is usually indication that DNSSEC authentication was not enabled\n"
522 " or is not available for the selected protocol or DNS servers.%s\n",
523 ansi_highlight_red(),
530 static int resolve_rfc4501(sd_bus
*bus
, const char *name
) {
531 uint16_t type
= 0, class = 0;
532 const char *p
, *q
, *n
;
537 assert(startswith(name
, "dns:"));
539 /* Parse RFC 4501 dns: URIs */
549 e
= strchr(p
+ 2, '/');
554 log_warning("DNS authority specification not supported; ignoring specified authority.");
561 n
= strndupa(p
, q
- p
);
567 f
= startswith_no_case(q
, "class=");
569 _cleanup_free_
char *t
= NULL
;
573 log_error("DNS class specified twice.");
577 e
= strchrnul(f
, ';');
578 t
= strndup(f
, e
- f
);
582 r
= dns_class_from_string(t
);
584 log_error("Unknown DNS class %s.", t
);
598 f
= startswith_no_case(q
, "type=");
600 _cleanup_free_
char *t
= NULL
;
604 log_error("DNS type specified twice.");
608 e
= strchrnul(f
, ';');
609 t
= strndup(f
, e
- f
);
613 r
= dns_type_from_string(t
);
615 log_error("Unknown DNS type %s.", t
);
635 class = arg_class
?: DNS_CLASS_IN
;
637 type
= arg_type
?: DNS_TYPE_A
;
639 return resolve_record(bus
, n
, class, type
);
642 log_error("Invalid DNS URI: %s", name
);
646 static int resolve_service(sd_bus
*bus
, const char *name
, const char *type
, const char *domain
) {
647 const char *canonical_name
, *canonical_type
, *canonical_domain
;
648 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
649 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
650 char ifname
[IF_NAMESIZE
] = "";
666 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
667 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
670 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
);
672 log_debug("Resolving service type %s of %s (family %s, interface %s).", type
, domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
674 log_debug("Resolving service type %s (family %s, interface %s).", domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
676 r
= sd_bus_message_new_method_call(
679 "org.freedesktop.resolve1",
680 "/org/freedesktop/resolve1",
681 "org.freedesktop.resolve1.Manager",
684 return bus_log_create_error(r
);
686 r
= sd_bus_message_append(req
, "isssit", arg_ifindex
, name
, type
, domain
, arg_family
, arg_flags
);
688 return bus_log_create_error(r
);
690 ts
= now(CLOCK_MONOTONIC
);
692 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
694 return log_error_errno(r
, "Resolve call failed: %s", bus_error_message(&error
, r
));
696 ts
= now(CLOCK_MONOTONIC
) - ts
;
698 r
= sd_bus_message_enter_container(reply
, 'a', "(qqqsa(iiay)s)");
700 return bus_log_parse_error(r
);
703 (name
? strlen(name
) + 1 : 0) +
704 (type
? strlen(type
) + 1 : 0) +
708 while ((r
= sd_bus_message_enter_container(reply
, 'r', "qqqsa(iiay)s")) > 0) {
709 uint16_t priority
, weight
, port
;
710 const char *hostname
, *canonical
;
712 r
= sd_bus_message_read(reply
, "qqqs", &priority
, &weight
, &port
, &hostname
);
714 return bus_log_parse_error(r
);
717 printf("%*s%s", (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? "/" : " ");
719 printf("%*s%s", (int) strlen(type
), c
== 0 ? type
: "", c
== 0 ? "/" : " ");
721 printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
722 (int) strlen(domain
), c
== 0 ? domain
: "",
727 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
729 return bus_log_parse_error(r
);
731 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
732 _cleanup_free_
char *pretty
= NULL
;
736 assert_cc(sizeof(int) == sizeof(int32_t));
738 r
= sd_bus_message_read(reply
, "ii", &ifindex
, &family
);
740 return bus_log_parse_error(r
);
742 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
744 return bus_log_parse_error(r
);
746 r
= sd_bus_message_exit_container(reply
);
748 return bus_log_parse_error(r
);
750 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
751 log_debug("%s: skipping entry with family %d (%s)", name
, family
, af_to_name(family
) ?: "unknown");
755 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
756 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name
, sz
, af_to_name(family
) ?: "unknown");
761 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
762 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
764 r
= in_addr_to_string(family
, a
, &pretty
);
766 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
768 printf("%*s%s%s%s\n", (int) indent
, "", pretty
, isempty(ifname
) ? "" : "%s", ifname
);
771 return bus_log_parse_error(r
);
773 r
= sd_bus_message_exit_container(reply
);
775 return bus_log_parse_error(r
);
777 r
= sd_bus_message_read(reply
, "s", &canonical
);
779 return bus_log_parse_error(r
);
781 if (!streq(hostname
, canonical
))
782 printf("%*s(%s)\n", (int) indent
, "", canonical
);
784 r
= sd_bus_message_exit_container(reply
);
786 return bus_log_parse_error(r
);
791 return bus_log_parse_error(r
);
793 r
= sd_bus_message_exit_container(reply
);
795 return bus_log_parse_error(r
);
797 r
= sd_bus_message_enter_container(reply
, 'a', "ay");
799 return bus_log_parse_error(r
);
802 while ((r
= sd_bus_message_read_array(reply
, 'y', (const void**) &p
, &sz
)) > 0) {
803 _cleanup_free_
char *escaped
= NULL
;
805 escaped
= cescape_length(p
, sz
);
809 printf("%*s%s\n", (int) indent
, "", escaped
);
813 return bus_log_parse_error(r
);
815 r
= sd_bus_message_exit_container(reply
);
817 return bus_log_parse_error(r
);
819 r
= sd_bus_message_read(reply
, "ssst", &canonical_name
, &canonical_type
, &canonical_domain
, &flags
);
821 return bus_log_parse_error(r
);
823 if (isempty(canonical_name
))
824 canonical_name
= NULL
;
825 if (isempty(canonical_type
))
826 canonical_type
= NULL
;
828 if (!streq_ptr(name
, canonical_name
) ||
829 !streq_ptr(type
, canonical_type
) ||
830 !streq_ptr(domain
, canonical_domain
)) {
832 printf("%*s(", (int) indent
, "");
835 printf("%s/", canonical_name
);
837 printf("%s/", canonical_type
);
839 printf("%s)\n", canonical_domain
);
842 print_source(flags
, ts
);
847 static int resolve_openpgp(sd_bus
*bus
, const char *address
) {
848 const char *domain
, *full
;
850 _cleanup_free_
char *hashed
= NULL
;
855 domain
= strrchr(address
, '@');
857 log_error("Address does not contain '@': \"%s\"", address
);
859 } else if (domain
== address
|| domain
[1] == '\0') {
860 log_error("Address starts or ends with '@': \"%s\"", address
);
865 r
= string_hashsum_sha224(address
, domain
- 1 - address
, &hashed
);
867 return log_error_errno(r
, "Hashing failed: %m");
869 full
= strjoina(hashed
, "._openpgpkey.", domain
);
870 log_debug("Looking up \"%s\".", full
);
872 return resolve_record(bus
, full
,
873 arg_class
?: DNS_CLASS_IN
,
874 arg_type
?: DNS_TYPE_OPENPGPKEY
);
877 static int resolve_tlsa(sd_bus
*bus
, const char *address
) {
879 uint16_t port_num
= 443;
880 _cleanup_free_
char *full
= NULL
;
886 port
= strrchr(address
, ':');
888 r
= safe_atou16(port
+ 1, &port_num
);
889 if (r
< 0 || port_num
== 0)
890 return log_error_errno(r
, "Invalid port \"%s\".", port
+ 1);
892 address
= strndupa(address
, port
- address
);
895 r
= asprintf(&full
, "_%u.%s.%s",
897 service_family_to_string(arg_service_family
),
902 log_debug("Looking up \"%s\".", full
);
904 return resolve_record(bus
, full
,
905 arg_class
?: DNS_CLASS_IN
,
906 arg_type
?: DNS_TYPE_TLSA
);
909 static int show_statistics(sd_bus
*bus
) {
910 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
911 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
912 uint64_t n_current_transactions
, n_total_transactions
,
913 cache_size
, n_cache_hit
, n_cache_miss
,
914 n_dnssec_secure
, n_dnssec_insecure
, n_dnssec_bogus
, n_dnssec_indeterminate
;
915 int r
, dnssec_supported
;
919 r
= sd_bus_get_property_trivial(bus
,
920 "org.freedesktop.resolve1",
921 "/org/freedesktop/resolve1",
922 "org.freedesktop.resolve1.Manager",
928 return log_error_errno(r
, "Failed to get DNSSEC supported state: %s", bus_error_message(&error
, r
));
930 printf("DNSSEC supported by current servers: %s%s%s\n\n",
932 yes_no(dnssec_supported
),
935 r
= sd_bus_get_property(bus
,
936 "org.freedesktop.resolve1",
937 "/org/freedesktop/resolve1",
938 "org.freedesktop.resolve1.Manager",
939 "TransactionStatistics",
944 return log_error_errno(r
, "Failed to get transaction statistics: %s", bus_error_message(&error
, r
));
946 r
= sd_bus_message_read(reply
, "(tt)",
947 &n_current_transactions
,
948 &n_total_transactions
);
950 return bus_log_parse_error(r
);
952 printf("%sTransactions%s\n"
953 "Current Transactions: %" PRIu64
"\n"
954 " Total Transactions: %" PRIu64
"\n",
957 n_current_transactions
,
958 n_total_transactions
);
960 reply
= sd_bus_message_unref(reply
);
962 r
= sd_bus_get_property(bus
,
963 "org.freedesktop.resolve1",
964 "/org/freedesktop/resolve1",
965 "org.freedesktop.resolve1.Manager",
971 return log_error_errno(r
, "Failed to get cache statistics: %s", bus_error_message(&error
, r
));
973 r
= sd_bus_message_read(reply
, "(ttt)",
978 return bus_log_parse_error(r
);
980 printf("\n%sCache%s\n"
981 " Current Cache Size: %" PRIu64
"\n"
982 " Cache Hits: %" PRIu64
"\n"
983 " Cache Misses: %" PRIu64
"\n",
990 reply
= sd_bus_message_unref(reply
);
992 r
= sd_bus_get_property(bus
,
993 "org.freedesktop.resolve1",
994 "/org/freedesktop/resolve1",
995 "org.freedesktop.resolve1.Manager",
1001 return log_error_errno(r
, "Failed to get DNSSEC statistics: %s", bus_error_message(&error
, r
));
1003 r
= sd_bus_message_read(reply
, "(tttt)",
1007 &n_dnssec_indeterminate
);
1009 return bus_log_parse_error(r
);
1011 printf("\n%sDNSSEC Verdicts%s\n"
1012 " Secure: %" PRIu64
"\n"
1013 " Insecure: %" PRIu64
"\n"
1014 " Bogus: %" PRIu64
"\n"
1015 " Indeterminate: %" PRIu64
"\n",
1021 n_dnssec_indeterminate
);
1026 static int reset_statistics(sd_bus
*bus
) {
1027 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1030 r
= sd_bus_call_method(bus
,
1031 "org.freedesktop.resolve1",
1032 "/org/freedesktop/resolve1",
1033 "org.freedesktop.resolve1.Manager",
1039 return log_error_errno(r
, "Failed to reset statistics: %s", bus_error_message(&error
, r
));
1044 static void help_protocol_types(void) {
1046 puts("Known protocol types:");
1047 puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6");
1050 static void help_dns_types(void) {
1055 puts("Known DNS RR types:");
1056 for (i
= 0; i
< _DNS_TYPE_MAX
; i
++) {
1057 t
= dns_type_to_string(i
);
1063 static void help_dns_classes(void) {
1068 puts("Known DNS RR classes:");
1069 for (i
= 0; i
< _DNS_CLASS_MAX
; i
++) {
1070 t
= dns_class_to_string(i
);
1076 static void help(void) {
1077 printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n"
1078 "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n"
1079 "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n"
1080 "%1$s [OPTIONS...] --statistics\n"
1081 "%1$s [OPTIONS...] --reset-statistics\n"
1083 "Resolve domain names, IPv4 and IPv6 addresses, DNS resource records, and services.\n\n"
1084 " -h --help Show this help\n"
1085 " --version Show package version\n"
1086 " -4 Resolve IPv4 addresses\n"
1087 " -6 Resolve IPv6 addresses\n"
1088 " -i --interface=INTERFACE Look on interface\n"
1089 " -p --protocol=PROTO|help Look via protocol\n"
1090 " -t --type=TYPE|help Query RR with DNS type\n"
1091 " -c --class=CLASS|help Query RR with DNS class\n"
1092 " --service Resolve service (SRV)\n"
1093 " --service-address=BOOL Resolve address for services (default: yes)\n"
1094 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
1095 " --openpgp Query OpenPGP public key\n"
1096 " --tlsa Query TLS public key\n"
1097 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
1098 " --search=BOOL Use search domains for single-label names\n"
1100 " --raw[=payload|packet] Dump the answer as binary data\n"
1101 " --legend=BOOL Print headers and additional info (default: yes)\n"
1102 " --statistics Show resolver statistics\n"
1103 " --reset-statistics Reset resolver statistics\n"
1104 , program_invocation_short_name
);
1107 static int parse_argv(int argc
, char *argv
[]) {
1109 ARG_VERSION
= 0x100,
1113 ARG_SERVICE_ADDRESS
,
1120 ARG_RESET_STATISTICS
,
1123 static const struct option options
[] = {
1124 { "help", no_argument
, NULL
, 'h' },
1125 { "version", no_argument
, NULL
, ARG_VERSION
},
1126 { "type", required_argument
, NULL
, 't' },
1127 { "class", required_argument
, NULL
, 'c' },
1128 { "legend", required_argument
, NULL
, ARG_LEGEND
},
1129 { "interface", required_argument
, NULL
, 'i' },
1130 { "protocol", required_argument
, NULL
, 'p' },
1131 { "cname", required_argument
, NULL
, ARG_CNAME
},
1132 { "service", no_argument
, NULL
, ARG_SERVICE
},
1133 { "service-address", required_argument
, NULL
, ARG_SERVICE_ADDRESS
},
1134 { "service-txt", required_argument
, NULL
, ARG_SERVICE_TXT
},
1135 { "openpgp", no_argument
, NULL
, ARG_OPENPGP
},
1136 { "tlsa", optional_argument
, NULL
, ARG_TLSA
},
1137 { "raw", optional_argument
, NULL
, ARG_RAW
},
1138 { "search", required_argument
, NULL
, ARG_SEARCH
},
1139 { "statistics", no_argument
, NULL
, ARG_STATISTICS
, },
1140 { "reset-statistics", no_argument
, NULL
, ARG_RESET_STATISTICS
},
1149 while ((c
= getopt_long(argc
, argv
, "h46i:t:c:p:", options
, NULL
)) >= 0)
1154 return 0; /* done */;
1160 arg_family
= AF_INET
;
1164 arg_family
= AF_INET6
;
1170 if (parse_ifindex(optarg
, &ifi
) >= 0)
1173 ifi
= if_nametoindex(optarg
);
1175 return log_error_errno(errno
, "Unknown interface %s: %m", optarg
);
1184 if (streq(optarg
, "help")) {
1189 r
= dns_type_from_string(optarg
);
1191 log_error("Failed to parse RR record type %s", optarg
);
1194 arg_type
= (uint16_t) r
;
1195 assert((int) arg_type
== r
);
1197 arg_mode
= MODE_RESOLVE_RECORD
;
1201 if (streq(optarg
, "help")) {
1206 r
= dns_class_from_string(optarg
);
1208 log_error("Failed to parse RR record class %s", optarg
);
1211 arg_class
= (uint16_t) r
;
1212 assert((int) arg_class
== r
);
1217 r
= parse_boolean(optarg
);
1219 return log_error_errno(r
, "Failed to parse --legend= argument");
1225 if (streq(optarg
, "help")) {
1226 help_protocol_types();
1228 } else if (streq(optarg
, "dns"))
1229 arg_flags
|= SD_RESOLVED_DNS
;
1230 else if (streq(optarg
, "llmnr"))
1231 arg_flags
|= SD_RESOLVED_LLMNR
;
1232 else if (streq(optarg
, "llmnr-ipv4"))
1233 arg_flags
|= SD_RESOLVED_LLMNR_IPV4
;
1234 else if (streq(optarg
, "llmnr-ipv6"))
1235 arg_flags
|= SD_RESOLVED_LLMNR_IPV6
;
1237 log_error("Unknown protocol specifier: %s", optarg
);
1244 arg_mode
= MODE_RESOLVE_SERVICE
;
1248 arg_mode
= MODE_RESOLVE_OPENPGP
;
1252 arg_mode
= MODE_RESOLVE_TLSA
;
1253 arg_service_family
= service_family_from_string(optarg
);
1254 if (arg_service_family
< 0) {
1255 log_error("Unknown service family \"%s\".", optarg
);
1262 log_error("Refusing to write binary data to tty.");
1266 if (optarg
== NULL
|| streq(optarg
, "payload"))
1267 arg_raw
= RAW_PAYLOAD
;
1268 else if (streq(optarg
, "packet"))
1269 arg_raw
= RAW_PACKET
;
1271 log_error("Unknown --raw specifier \"%s\".", optarg
);
1279 r
= parse_boolean(optarg
);
1281 return log_error_errno(r
, "Failed to parse --cname= argument.");
1282 SET_FLAG(arg_flags
, SD_RESOLVED_NO_CNAME
, r
== 0);
1285 case ARG_SERVICE_ADDRESS
:
1286 r
= parse_boolean(optarg
);
1288 return log_error_errno(r
, "Failed to parse --service-address= argument.");
1289 SET_FLAG(arg_flags
, SD_RESOLVED_NO_ADDRESS
, r
== 0);
1292 case ARG_SERVICE_TXT
:
1293 r
= parse_boolean(optarg
);
1295 return log_error_errno(r
, "Failed to parse --service-txt= argument.");
1296 SET_FLAG(arg_flags
, SD_RESOLVED_NO_TXT
, r
== 0);
1300 r
= parse_boolean(optarg
);
1302 return log_error_errno(r
, "Failed to parse --search argument.");
1303 SET_FLAG(arg_flags
, SD_RESOLVED_NO_SEARCH
, r
== 0);
1306 case ARG_STATISTICS
:
1307 arg_mode
= MODE_STATISTICS
;
1310 case ARG_RESET_STATISTICS
:
1311 arg_mode
= MODE_RESET_STATISTICS
;
1318 assert_not_reached("Unhandled option");
1321 if (arg_type
== 0 && arg_class
!= 0) {
1322 log_error("--class= may only be used in conjunction with --type=.");
1326 if (arg_type
!= 0 && arg_mode
== MODE_RESOLVE_SERVICE
) {
1327 log_error("--service and --type= may not be combined.");
1331 if (arg_type
!= 0 && arg_class
== 0)
1332 arg_class
= DNS_CLASS_IN
;
1334 if (arg_class
!= 0 && arg_type
== 0)
1335 arg_type
= DNS_TYPE_A
;
1337 return 1 /* work to do */;
1340 int main(int argc
, char **argv
) {
1341 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1344 log_parse_environment();
1347 r
= parse_argv(argc
, argv
);
1351 r
= sd_bus_open_system(&bus
);
1353 log_error_errno(r
, "sd_bus_open_system: %m");
1359 case MODE_RESOLVE_HOST
:
1360 if (optind
>= argc
) {
1361 log_error("No arguments passed.");
1366 while (argv
[optind
]) {
1367 int family
, ifindex
, k
;
1368 union in_addr_union a
;
1370 if (startswith(argv
[optind
], "dns:"))
1371 k
= resolve_rfc4501(bus
, argv
[optind
]);
1373 k
= parse_address(argv
[optind
], &family
, &a
, &ifindex
);
1375 k
= resolve_address(bus
, family
, &a
, ifindex
);
1377 k
= resolve_host(bus
, argv
[optind
]);
1387 case MODE_RESOLVE_RECORD
:
1388 if (optind
>= argc
) {
1389 log_error("No arguments passed.");
1394 while (argv
[optind
]) {
1397 k
= resolve_record(bus
, argv
[optind
], arg_class
, arg_type
);
1405 case MODE_RESOLVE_SERVICE
:
1406 if (argc
< optind
+ 1) {
1407 log_error("Domain specification required.");
1411 } else if (argc
== optind
+ 1)
1412 r
= resolve_service(bus
, NULL
, NULL
, argv
[optind
]);
1413 else if (argc
== optind
+ 2)
1414 r
= resolve_service(bus
, NULL
, argv
[optind
], argv
[optind
+1]);
1415 else if (argc
== optind
+ 3)
1416 r
= resolve_service(bus
, argv
[optind
], argv
[optind
+1], argv
[optind
+2]);
1418 log_error("Too many arguments.");
1425 case MODE_RESOLVE_OPENPGP
:
1426 if (argc
< optind
+ 1) {
1427 log_error("E-mail address required.");
1434 while (optind
< argc
) {
1437 k
= resolve_openpgp(bus
, argv
[optind
++]);
1443 case MODE_RESOLVE_TLSA
:
1444 if (argc
< optind
+ 1) {
1445 log_error("Domain name required.");
1452 while (optind
< argc
) {
1455 k
= resolve_tlsa(bus
, argv
[optind
++]);
1461 case MODE_STATISTICS
:
1462 if (argc
> optind
) {
1463 log_error("Too many arguments.");
1468 r
= show_statistics(bus
);
1471 case MODE_RESET_STATISTICS
:
1472 if (argc
> optind
) {
1473 log_error("Too many arguments.");
1478 r
= reset_statistics(bus
);
1483 return r
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;