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/>.
27 #include "alloc-util.h"
28 #include "bus-error.h"
31 #include "in-addr-util.h"
32 #include "gcrypt-util.h"
33 #include "parse-util.h"
34 #include "resolved-def.h"
35 #include "resolved-dns-packet.h"
36 #include "terminal-util.h"
38 #define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC)
40 static int arg_family
= AF_UNSPEC
;
41 static int arg_ifindex
= 0;
42 static uint16_t arg_type
= 0;
43 static uint16_t arg_class
= 0;
44 static bool arg_legend
= true;
45 static uint64_t arg_flags
= 0;
53 MODE_RESET_STATISTICS
,
54 } arg_mode
= MODE_RESOLVE_HOST
;
56 static void print_source(uint64_t flags
, usec_t rtt
) {
57 char rtt_str
[FORMAT_TIMESTAMP_MAX
];
65 fputs("\n-- Information acquired via", stdout
);
68 printf(" protocol%s%s%s%s%s",
69 flags
& SD_RESOLVED_DNS
? " DNS" :"",
70 flags
& SD_RESOLVED_LLMNR_IPV4
? " LLMNR/IPv4" : "",
71 flags
& SD_RESOLVED_LLMNR_IPV6
? " LLMNR/IPv6" : "",
72 flags
& SD_RESOLVED_MDNS_IPV4
? "mDNS/IPv4" : "",
73 flags
& SD_RESOLVED_MDNS_IPV6
? "mDNS/IPv6" : "");
75 assert_se(format_timespan(rtt_str
, sizeof(rtt_str
), rtt
, 100));
77 printf(" in %s", rtt_str
);
82 printf("-- Data is authenticated: %s\n", yes_no(flags
& SD_RESOLVED_AUTHENTICATED
));
85 static int resolve_host(sd_bus
*bus
, const char *name
) {
87 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
88 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
89 const char *canonical
= NULL
;
90 char ifname
[IF_NAMESIZE
] = "";
98 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
99 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
101 log_debug("Resolving %s (family %s, interface %s).", name
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
103 r
= sd_bus_message_new_method_call(
106 "org.freedesktop.resolve1",
107 "/org/freedesktop/resolve1",
108 "org.freedesktop.resolve1.Manager",
111 return bus_log_create_error(r
);
113 r
= sd_bus_message_append(req
, "isit", arg_ifindex
, name
, arg_family
, arg_flags
);
115 return bus_log_create_error(r
);
117 ts
= now(CLOCK_MONOTONIC
);
119 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
121 return log_error_errno(r
, "%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
123 ts
= now(CLOCK_MONOTONIC
) - ts
;
125 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
127 return bus_log_parse_error(r
);
129 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
130 _cleanup_free_
char *pretty
= NULL
;
135 assert_cc(sizeof(int) == sizeof(int32_t));
137 r
= sd_bus_message_read(reply
, "ii", &ifindex
, &family
);
139 return bus_log_parse_error(r
);
141 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
143 return bus_log_parse_error(r
);
145 r
= sd_bus_message_exit_container(reply
);
147 return bus_log_parse_error(r
);
149 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
150 log_debug("%s: skipping entry with family %d (%s)", name
, family
, af_to_name(family
) ?: "unknown");
154 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
155 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name
, sz
, af_to_name(family
) ?: "unknown");
160 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
161 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
163 r
= in_addr_to_string(family
, a
, &pretty
);
165 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
167 printf("%*s%s %s%s%s\n",
168 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
170 isempty(ifname
) ? "" : "%", ifname
);
175 return bus_log_parse_error(r
);
177 r
= sd_bus_message_exit_container(reply
);
179 return bus_log_parse_error(r
);
181 r
= sd_bus_message_read(reply
, "st", &canonical
, &flags
);
183 return bus_log_parse_error(r
);
185 if (!streq(name
, canonical
))
186 printf("%*s%s (%s)\n",
187 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
191 log_error("%s: no addresses found", name
);
195 print_source(flags
, ts
);
200 static int resolve_address(sd_bus
*bus
, int family
, const union in_addr_union
*address
, int ifindex
) {
201 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
202 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
203 _cleanup_free_
char *pretty
= NULL
;
204 char ifname
[IF_NAMESIZE
] = "";
211 assert(IN_SET(family
, AF_INET
, AF_INET6
));
215 ifindex
= arg_ifindex
;
217 r
= in_addr_to_string(family
, address
, &pretty
);
221 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
222 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
224 log_debug("Resolving %s%s%s.", pretty
, isempty(ifname
) ? "" : "%", ifname
);
226 r
= sd_bus_message_new_method_call(
229 "org.freedesktop.resolve1",
230 "/org/freedesktop/resolve1",
231 "org.freedesktop.resolve1.Manager",
234 return bus_log_create_error(r
);
236 r
= sd_bus_message_append(req
, "ii", ifindex
, family
);
238 return bus_log_create_error(r
);
240 r
= sd_bus_message_append_array(req
, 'y', address
, FAMILY_ADDRESS_SIZE(family
));
242 return bus_log_create_error(r
);
244 r
= sd_bus_message_append(req
, "t", arg_flags
);
246 return bus_log_create_error(r
);
248 ts
= now(CLOCK_MONOTONIC
);
250 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
252 log_error("%s: resolve call failed: %s", pretty
, bus_error_message(&error
, r
));
256 ts
= now(CLOCK_MONOTONIC
) - ts
;
258 r
= sd_bus_message_enter_container(reply
, 'a', "(is)");
260 return bus_log_create_error(r
);
262 while ((r
= sd_bus_message_enter_container(reply
, 'r', "is")) > 0) {
265 assert_cc(sizeof(int) == sizeof(int32_t));
267 r
= sd_bus_message_read(reply
, "is", &ifindex
, &n
);
271 r
= sd_bus_message_exit_container(reply
);
276 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
277 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
279 printf("%*s%*s%*s%s %s\n",
280 (int) strlen(pretty
), c
== 0 ? pretty
: "",
281 isempty(ifname
) ? 0 : 1, c
> 0 || isempty(ifname
) ? "" : "%",
282 (int) strlen(ifname
), c
== 0 ? ifname
: "",
289 return bus_log_parse_error(r
);
291 r
= sd_bus_message_exit_container(reply
);
293 return bus_log_parse_error(r
);
295 r
= sd_bus_message_read(reply
, "t", &flags
);
297 return bus_log_parse_error(r
);
300 log_error("%s: no names found", pretty
);
304 print_source(flags
, ts
);
309 static int parse_address(const char *s
, int *family
, union in_addr_union
*address
, int *ifindex
) {
310 const char *percent
, *a
;
314 percent
= strchr(s
, '%');
316 if (parse_ifindex(percent
+1, &ifi
) < 0) {
317 ifi
= if_nametoindex(percent
+1);
322 a
= strndupa(s
, percent
- s
);
326 r
= in_addr_from_string_auto(a
, family
, address
);
334 static int resolve_record(sd_bus
*bus
, const char *name
, uint16_t class, uint16_t type
) {
335 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
336 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
337 char ifname
[IF_NAMESIZE
] = "";
342 bool needs_authentication
= false;
346 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
347 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
349 log_debug("Resolving %s %s %s (interface %s).", name
, dns_class_to_string(class), dns_type_to_string(type
), isempty(ifname
) ? "*" : ifname
);
351 r
= sd_bus_message_new_method_call(
354 "org.freedesktop.resolve1",
355 "/org/freedesktop/resolve1",
356 "org.freedesktop.resolve1.Manager",
359 return bus_log_create_error(r
);
361 r
= sd_bus_message_append(req
, "isqqt", arg_ifindex
, name
, class, type
, arg_flags
);
363 return bus_log_create_error(r
);
365 ts
= now(CLOCK_MONOTONIC
);
367 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
369 log_error("%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
373 ts
= now(CLOCK_MONOTONIC
) - ts
;
375 r
= sd_bus_message_enter_container(reply
, 'a', "(iqqay)");
377 return bus_log_parse_error(r
);
379 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iqqay")) > 0) {
380 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
381 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
388 assert_cc(sizeof(int) == sizeof(int32_t));
390 r
= sd_bus_message_read(reply
, "iqq", &ifindex
, &c
, &t
);
392 return bus_log_parse_error(r
);
394 r
= sd_bus_message_read_array(reply
, 'y', &d
, &l
);
396 return bus_log_parse_error(r
);
398 r
= sd_bus_message_exit_container(reply
);
400 return bus_log_parse_error(r
);
402 r
= dns_packet_new(&p
, DNS_PROTOCOL_DNS
, 0);
406 p
->refuse_compression
= true;
408 r
= dns_packet_append_blob(p
, d
, l
, NULL
);
412 r
= dns_packet_read_rr(p
, &rr
, NULL
, NULL
);
414 return log_error_errno(r
, "Failed to parse RR: %m");
416 s
= dns_resource_record_to_string(rr
);
421 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
422 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
424 printf("%s%s%s\n", s
, isempty(ifname
) ? "" : " # interface ", ifname
);
426 if (dns_type_needs_authentication(t
))
427 needs_authentication
= true;
432 return bus_log_parse_error(r
);
434 r
= sd_bus_message_exit_container(reply
);
436 return bus_log_parse_error(r
);
438 r
= sd_bus_message_read(reply
, "t", &flags
);
440 return bus_log_parse_error(r
);
443 log_error("%s: no records found", name
);
447 print_source(flags
, ts
);
449 if ((flags
& SD_RESOLVED_AUTHENTICATED
) == 0 && needs_authentication
) {
452 fprintf(stderr
, "\n%s"
453 "WARNING: The resources shown contain cryptographic key data which could not be\n"
454 " authenticated. It is not suitable to authenticate any communication.\n"
455 " This is usually indication that DNSSEC authentication was not enabled\n"
456 " or is not available for the selected protocol or DNS servers.%s\n",
457 ansi_highlight_red(),
464 static int resolve_rfc4501(sd_bus
*bus
, const char *name
) {
465 uint16_t type
= 0, class = 0;
466 const char *p
, *q
, *n
;
471 assert(startswith(name
, "dns:"));
473 /* Parse RFC 4501 dns: URIs */
483 e
= strchr(p
+ 2, '/');
488 log_warning("DNS authority specification not supported; ignoring specified authority.");
495 n
= strndupa(p
, q
- p
);
501 f
= startswith_no_case(q
, "class=");
503 _cleanup_free_
char *t
= NULL
;
507 log_error("DNS class specified twice.");
511 e
= strchrnul(f
, ';');
512 t
= strndup(f
, e
- f
);
516 r
= dns_class_from_string(t
);
518 log_error("Unknown DNS class %s.", t
);
532 f
= startswith_no_case(q
, "type=");
534 _cleanup_free_
char *t
= NULL
;
538 log_error("DNS type specified twice.");
542 e
= strchrnul(f
, ';');
543 t
= strndup(f
, e
- f
);
547 r
= dns_type_from_string(t
);
549 log_error("Unknown DNS type %s.", t
);
569 class = arg_class
?: DNS_CLASS_IN
;
571 type
= arg_type
?: DNS_TYPE_A
;
573 return resolve_record(bus
, n
, class, type
);
576 log_error("Invalid DNS URI: %s", name
);
580 static int resolve_service(sd_bus
*bus
, const char *name
, const char *type
, const char *domain
) {
581 const char *canonical_name
, *canonical_type
, *canonical_domain
;
582 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
583 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
584 char ifname
[IF_NAMESIZE
] = "";
600 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
601 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
604 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
);
606 log_debug("Resolving service type %s of %s (family %s, interface %s).", type
, domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
608 log_debug("Resolving service type %s (family %s, interface %s).", domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
610 r
= sd_bus_message_new_method_call(
613 "org.freedesktop.resolve1",
614 "/org/freedesktop/resolve1",
615 "org.freedesktop.resolve1.Manager",
618 return bus_log_create_error(r
);
620 r
= sd_bus_message_append(req
, "isssit", arg_ifindex
, name
, type
, domain
, arg_family
, arg_flags
);
622 return bus_log_create_error(r
);
624 ts
= now(CLOCK_MONOTONIC
);
626 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
628 return log_error_errno(r
, "Resolve call failed: %s", bus_error_message(&error
, r
));
630 ts
= now(CLOCK_MONOTONIC
) - ts
;
632 r
= sd_bus_message_enter_container(reply
, 'a', "(qqqsa(iiay)s)");
634 return bus_log_parse_error(r
);
637 (name
? strlen(name
) + 1 : 0) +
638 (type
? strlen(type
) + 1 : 0) +
642 while ((r
= sd_bus_message_enter_container(reply
, 'r', "qqqsa(iiay)s")) > 0) {
643 uint16_t priority
, weight
, port
;
644 const char *hostname
, *canonical
;
646 r
= sd_bus_message_read(reply
, "qqqs", &priority
, &weight
, &port
, &hostname
);
648 return bus_log_parse_error(r
);
651 printf("%*s%s", (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? "/" : " ");
653 printf("%*s%s", (int) strlen(type
), c
== 0 ? type
: "", c
== 0 ? "/" : " ");
655 printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
656 (int) strlen(domain
), c
== 0 ? domain
: "",
661 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
663 return bus_log_parse_error(r
);
665 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
666 _cleanup_free_
char *pretty
= NULL
;
670 assert_cc(sizeof(int) == sizeof(int32_t));
672 r
= sd_bus_message_read(reply
, "ii", &ifindex
, &family
);
674 return bus_log_parse_error(r
);
676 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
678 return bus_log_parse_error(r
);
680 r
= sd_bus_message_exit_container(reply
);
682 return bus_log_parse_error(r
);
684 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
685 log_debug("%s: skipping entry with family %d (%s)", name
, family
, af_to_name(family
) ?: "unknown");
689 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
690 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name
, sz
, af_to_name(family
) ?: "unknown");
695 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
696 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
698 r
= in_addr_to_string(family
, a
, &pretty
);
700 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
702 printf("%*s%s%s%s\n", (int) indent
, "", pretty
, isempty(ifname
) ? "" : "%s", ifname
);
705 return bus_log_parse_error(r
);
707 r
= sd_bus_message_exit_container(reply
);
709 return bus_log_parse_error(r
);
711 r
= sd_bus_message_read(reply
, "s", &canonical
);
713 return bus_log_parse_error(r
);
715 if (!streq(hostname
, canonical
))
716 printf("%*s(%s)\n", (int) indent
, "", canonical
);
718 r
= sd_bus_message_exit_container(reply
);
720 return bus_log_parse_error(r
);
725 return bus_log_parse_error(r
);
727 r
= sd_bus_message_exit_container(reply
);
729 return bus_log_parse_error(r
);
731 r
= sd_bus_message_enter_container(reply
, 'a', "ay");
733 return bus_log_parse_error(r
);
736 while ((r
= sd_bus_message_read_array(reply
, 'y', (const void**) &p
, &sz
)) > 0) {
737 _cleanup_free_
char *escaped
= NULL
;
739 escaped
= cescape_length(p
, sz
);
743 printf("%*s%s\n", (int) indent
, "", escaped
);
747 return bus_log_parse_error(r
);
749 r
= sd_bus_message_exit_container(reply
);
751 return bus_log_parse_error(r
);
753 r
= sd_bus_message_read(reply
, "ssst", &canonical_name
, &canonical_type
, &canonical_domain
, &flags
);
755 return bus_log_parse_error(r
);
757 if (isempty(canonical_name
))
758 canonical_name
= NULL
;
759 if (isempty(canonical_type
))
760 canonical_type
= NULL
;
762 if (!streq_ptr(name
, canonical_name
) ||
763 !streq_ptr(type
, canonical_type
) ||
764 !streq_ptr(domain
, canonical_domain
)) {
766 printf("%*s(", (int) indent
, "");
769 printf("%s/", canonical_name
);
771 printf("%s/", canonical_type
);
773 printf("%s)\n", canonical_domain
);
776 print_source(flags
, ts
);
781 static int resolve_openpgp(sd_bus
*bus
, const char *address
) {
782 const char *domain
, *full
;
784 _cleanup_free_
char *hashed
= NULL
;
789 domain
= strrchr(address
, '@');
791 log_error("Address does not contain '@': \"%s\"", address
);
793 } else if (domain
== address
|| domain
[1] == '\0') {
794 log_error("Address starts or ends with '@': \"%s\"", address
);
799 r
= string_hashsum(address
, domain
- 1 - address
, GCRY_MD_SHA224
, &hashed
);
801 return log_error_errno(r
, "Hashing failed: %m");
803 full
= strjoina(hashed
, "._openpgpkey.", domain
);
804 log_debug("Looking up \"%s\".", full
);
806 return resolve_record(bus
, full
,
807 arg_class
?: DNS_CLASS_IN
,
808 arg_type
?: DNS_TYPE_OPENPGPKEY
);
811 static int show_statistics(sd_bus
*bus
) {
812 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
813 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
814 uint64_t n_current_transactions
, n_total_transactions
,
815 cache_size
, n_cache_hit
, n_cache_miss
,
816 n_dnssec_secure
, n_dnssec_insecure
, n_dnssec_bogus
, n_dnssec_indeterminate
;
817 int r
, dnssec_supported
;
821 r
= sd_bus_get_property_trivial(bus
,
822 "org.freedesktop.resolve1",
823 "/org/freedesktop/resolve1",
824 "org.freedesktop.resolve1.Manager",
830 return log_error_errno(r
, "Failed to get DNSSEC supported state: %s", bus_error_message(&error
, r
));
832 printf("DNSSEC supported by current servers: %s%s%s\n\n",
834 yes_no(dnssec_supported
),
837 r
= sd_bus_get_property(bus
,
838 "org.freedesktop.resolve1",
839 "/org/freedesktop/resolve1",
840 "org.freedesktop.resolve1.Manager",
841 "TransactionStatistics",
846 return log_error_errno(r
, "Failed to get transaction statistics: %s", bus_error_message(&error
, r
));
848 r
= sd_bus_message_read(reply
, "(tt)",
849 &n_current_transactions
,
850 &n_total_transactions
);
852 return bus_log_parse_error(r
);
854 printf("%sTransactions%s\n"
855 "Current Transactions: %" PRIu64
"\n"
856 " Total Transactions: %" PRIu64
"\n",
859 n_current_transactions
,
860 n_total_transactions
);
862 reply
= sd_bus_message_unref(reply
);
864 r
= sd_bus_get_property(bus
,
865 "org.freedesktop.resolve1",
866 "/org/freedesktop/resolve1",
867 "org.freedesktop.resolve1.Manager",
873 return log_error_errno(r
, "Failed to get cache statistics: %s", bus_error_message(&error
, r
));
875 r
= sd_bus_message_read(reply
, "(ttt)",
880 return bus_log_parse_error(r
);
882 printf("\n%sCache%s\n"
883 " Current Cache Size: %" PRIu64
"\n"
884 " Cache Hits: %" PRIu64
"\n"
885 " Cache Misses: %" PRIu64
"\n",
892 reply
= sd_bus_message_unref(reply
);
894 r
= sd_bus_get_property(bus
,
895 "org.freedesktop.resolve1",
896 "/org/freedesktop/resolve1",
897 "org.freedesktop.resolve1.Manager",
903 return log_error_errno(r
, "Failed to get DNSSEC statistics: %s", bus_error_message(&error
, r
));
905 r
= sd_bus_message_read(reply
, "(tttt)",
909 &n_dnssec_indeterminate
);
911 return bus_log_parse_error(r
);
913 printf("\n%sDNSSEC Verdicts%s\n"
914 " Secure: %" PRIu64
"\n"
915 " Insecure: %" PRIu64
"\n"
916 " Bogus: %" PRIu64
"\n"
917 " Indeterminate: %" PRIu64
"\n",
923 n_dnssec_indeterminate
);
928 static int reset_statistics(sd_bus
*bus
) {
929 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
932 r
= sd_bus_call_method(bus
,
933 "org.freedesktop.resolve1",
934 "/org/freedesktop/resolve1",
935 "org.freedesktop.resolve1.Manager",
941 return log_error_errno(r
, "Failed to reset statistics: %s", bus_error_message(&error
, r
));
946 static void help_protocol_types(void) {
948 puts("Known protocol types:");
949 puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6");
952 static void help_dns_types(void) {
957 puts("Known DNS RR types:");
958 for (i
= 0; i
< _DNS_TYPE_MAX
; i
++) {
959 t
= dns_type_to_string(i
);
965 static void help_dns_classes(void) {
970 puts("Known DNS RR classes:");
971 for (i
= 0; i
< _DNS_CLASS_MAX
; i
++) {
972 t
= dns_class_to_string(i
);
978 static void help(void) {
979 printf("%s [OPTIONS...] NAME...\n"
980 "%s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n\n"
981 "Resolve domain names, IPv4 and IPv6 addresses, DNS resource records, and services.\n\n"
982 " -h --help Show this help\n"
983 " --version Show package version\n"
984 " -4 Resolve IPv4 addresses\n"
985 " -6 Resolve IPv6 addresses\n"
986 " -i --interface=INTERFACE Look on interface\n"
987 " -p --protocol=PROTOCOL|help Look via protocol\n"
988 " -t --type=TYPE|help Query RR with DNS type\n"
989 " -c --class=CLASS|help Query RR with DNS class\n"
990 " --service Resolve service (SRV)\n"
991 " --service-address=BOOL Do [not] resolve address for services\n"
992 " --service-txt=BOOL Do [not] resolve TXT records for services\n"
993 " --openpgp Query OpenPGP public key\n"
994 " --cname=BOOL Do [not] follow CNAME redirects\n"
995 " --search=BOOL Do [not] use search domains\n"
996 " --legend=BOOL Do [not] print column headers and meta information\n"
997 " --statistics Show resolver statistics\n"
998 " --reset-statistics Reset resolver statistics\n"
999 , program_invocation_short_name
, program_invocation_short_name
);
1002 static int parse_argv(int argc
, char *argv
[]) {
1004 ARG_VERSION
= 0x100,
1008 ARG_SERVICE_ADDRESS
,
1013 ARG_RESET_STATISTICS
,
1016 static const struct option options
[] = {
1017 { "help", no_argument
, NULL
, 'h' },
1018 { "version", no_argument
, NULL
, ARG_VERSION
},
1019 { "type", required_argument
, NULL
, 't' },
1020 { "class", required_argument
, NULL
, 'c' },
1021 { "legend", required_argument
, NULL
, ARG_LEGEND
},
1022 { "interface", required_argument
, NULL
, 'i' },
1023 { "protocol", required_argument
, NULL
, 'p' },
1024 { "cname", required_argument
, NULL
, ARG_CNAME
},
1025 { "service", no_argument
, NULL
, ARG_SERVICE
},
1026 { "service-address", required_argument
, NULL
, ARG_SERVICE_ADDRESS
},
1027 { "service-txt", required_argument
, NULL
, ARG_SERVICE_TXT
},
1028 { "openpgp", no_argument
, NULL
, ARG_OPENPGP
},
1029 { "search", required_argument
, NULL
, ARG_SEARCH
},
1030 { "statistics", no_argument
, NULL
, ARG_STATISTICS
, },
1031 { "reset-statistics", no_argument
, NULL
, ARG_RESET_STATISTICS
},
1040 while ((c
= getopt_long(argc
, argv
, "h46i:t:c:p:", options
, NULL
)) >= 0)
1045 return 0; /* done */;
1051 arg_family
= AF_INET
;
1055 arg_family
= AF_INET6
;
1061 if (parse_ifindex(optarg
, &ifi
) >= 0)
1064 ifi
= if_nametoindex(optarg
);
1066 return log_error_errno(errno
, "Unknown interface %s: %m", optarg
);
1075 if (streq(optarg
, "help")) {
1080 r
= dns_type_from_string(optarg
);
1082 log_error("Failed to parse RR record type %s", optarg
);
1085 arg_type
= (uint16_t) r
;
1086 assert((int) arg_type
== r
);
1088 arg_mode
= MODE_RESOLVE_RECORD
;
1092 if (streq(optarg
, "help")) {
1097 r
= dns_class_from_string(optarg
);
1099 log_error("Failed to parse RR record class %s", optarg
);
1102 arg_class
= (uint16_t) r
;
1103 assert((int) arg_class
== r
);
1108 r
= parse_boolean(optarg
);
1110 return log_error_errno(r
, "Failed to parse --legend= argument");
1116 if (streq(optarg
, "help")) {
1117 help_protocol_types();
1119 } else if (streq(optarg
, "dns"))
1120 arg_flags
|= SD_RESOLVED_DNS
;
1121 else if (streq(optarg
, "llmnr"))
1122 arg_flags
|= SD_RESOLVED_LLMNR
;
1123 else if (streq(optarg
, "llmnr-ipv4"))
1124 arg_flags
|= SD_RESOLVED_LLMNR_IPV4
;
1125 else if (streq(optarg
, "llmnr-ipv6"))
1126 arg_flags
|= SD_RESOLVED_LLMNR_IPV6
;
1128 log_error("Unknown protocol specifier: %s", optarg
);
1135 arg_mode
= MODE_RESOLVE_SERVICE
;
1139 arg_mode
= MODE_RESOLVE_OPENPGP
;
1143 r
= parse_boolean(optarg
);
1145 return log_error_errno(r
, "Failed to parse --cname= argument.");
1147 arg_flags
|= SD_RESOLVED_NO_CNAME
;
1149 arg_flags
&= ~SD_RESOLVED_NO_CNAME
;
1152 case ARG_SERVICE_ADDRESS
:
1153 r
= parse_boolean(optarg
);
1155 return log_error_errno(r
, "Failed to parse --service-address= argument.");
1157 arg_flags
|= SD_RESOLVED_NO_ADDRESS
;
1159 arg_flags
&= ~SD_RESOLVED_NO_ADDRESS
;
1162 case ARG_SERVICE_TXT
:
1163 r
= parse_boolean(optarg
);
1165 return log_error_errno(r
, "Failed to parse --service-txt= argument.");
1167 arg_flags
|= SD_RESOLVED_NO_TXT
;
1169 arg_flags
&= ~SD_RESOLVED_NO_TXT
;
1173 r
= parse_boolean(optarg
);
1175 return log_error_errno(r
, "Failed to parse --search argument.");
1177 arg_flags
|= SD_RESOLVED_NO_SEARCH
;
1179 arg_flags
&= ~SD_RESOLVED_NO_SEARCH
;
1182 case ARG_STATISTICS
:
1183 arg_mode
= MODE_STATISTICS
;
1186 case ARG_RESET_STATISTICS
:
1187 arg_mode
= MODE_RESET_STATISTICS
;
1194 assert_not_reached("Unhandled option");
1197 if (arg_type
== 0 && arg_class
!= 0) {
1198 log_error("--class= may only be used in conjunction with --type=.");
1202 if (arg_type
!= 0 && arg_mode
!= MODE_RESOLVE_RECORD
) {
1203 log_error("--service and --type= may not be combined.");
1207 if (arg_type
!= 0 && arg_class
== 0)
1208 arg_class
= DNS_CLASS_IN
;
1210 if (arg_class
!= 0 && arg_type
== 0)
1211 arg_type
= DNS_TYPE_A
;
1213 return 1 /* work to do */;
1216 int main(int argc
, char **argv
) {
1217 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1220 log_parse_environment();
1223 r
= parse_argv(argc
, argv
);
1227 r
= sd_bus_open_system(&bus
);
1229 log_error_errno(r
, "sd_bus_open_system: %m");
1235 case MODE_RESOLVE_HOST
:
1236 if (optind
>= argc
) {
1237 log_error("No arguments passed.");
1242 while (argv
[optind
]) {
1243 int family
, ifindex
, k
;
1244 union in_addr_union a
;
1246 if (startswith(argv
[optind
], "dns:"))
1247 k
= resolve_rfc4501(bus
, argv
[optind
]);
1249 k
= parse_address(argv
[optind
], &family
, &a
, &ifindex
);
1251 k
= resolve_address(bus
, family
, &a
, ifindex
);
1253 k
= resolve_host(bus
, argv
[optind
]);
1263 case MODE_RESOLVE_RECORD
:
1264 if (optind
>= argc
) {
1265 log_error("No arguments passed.");
1270 while (argv
[optind
]) {
1273 k
= resolve_record(bus
, argv
[optind
], arg_class
, arg_type
);
1281 case MODE_RESOLVE_SERVICE
:
1282 if (argc
< optind
+ 1) {
1283 log_error("Domain specification required.");
1287 } else if (argc
== optind
+ 1)
1288 r
= resolve_service(bus
, NULL
, NULL
, argv
[optind
]);
1289 else if (argc
== optind
+ 2)
1290 r
= resolve_service(bus
, NULL
, argv
[optind
], argv
[optind
+1]);
1291 else if (argc
== optind
+ 3)
1292 r
= resolve_service(bus
, argv
[optind
], argv
[optind
+1], argv
[optind
+2]);
1294 log_error("Too many arguments.");
1301 case MODE_RESOLVE_OPENPGP
:
1302 if (argc
< optind
+ 1) {
1303 log_error("E-mail address required.");
1310 while (optind
< argc
) {
1313 k
= resolve_openpgp(bus
, argv
[optind
++]);
1319 case MODE_STATISTICS
:
1320 if (argc
> optind
) {
1321 log_error("Too many arguments.");
1326 r
= show_statistics(bus
);
1329 case MODE_RESET_STATISTICS
:
1330 if (argc
> optind
) {
1331 log_error("Too many arguments.");
1336 r
= reset_statistics(bus
);
1341 return r
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;