1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "sd-netlink.h"
11 #include "alloc-util.h"
12 #include "bus-common-errors.h"
13 #include "bus-error.h"
14 #include "bus-locator.h"
15 #include "bus-map-properties.h"
16 #include "bus-message-util.h"
17 #include "dns-domain.h"
19 #include "format-table.h"
20 #include "format-util.h"
21 #include "gcrypt-util.h"
22 #include "hostname-util.h"
23 #include "main-func.h"
24 #include "missing_network.h"
25 #include "netlink-util.h"
26 #include "openssl-util.h"
28 #include "parse-argument.h"
29 #include "parse-util.h"
30 #include "pretty-print.h"
31 #include "process-util.h"
32 #include "resolvconf-compat.h"
33 #include "resolvectl.h"
34 #include "resolved-def.h"
35 #include "resolved-dns-packet.h"
36 #include "resolved-util.h"
37 #include "socket-netlink.h"
38 #include "sort-util.h"
39 #include "stdio-util.h"
40 #include "string-table.h"
42 #include "terminal-util.h"
44 #include "verb-log-control.h"
47 static int arg_family
= AF_UNSPEC
;
48 static int arg_ifindex
= 0;
49 static char *arg_ifname
= NULL
;
50 static uint16_t arg_type
= 0;
51 static uint16_t arg_class
= 0;
52 static bool arg_legend
= true;
53 static uint64_t arg_flags
= 0;
54 static PagerFlags arg_pager_flags
= 0;
55 bool arg_ifindex_permissive
= false; /* If true, don't generate an error if the specified interface index doesn't exist */
56 static const char *arg_service_family
= NULL
;
58 typedef enum RawType
{
63 static RawType arg_raw
= RAW_NONE
;
65 ExecutionMode arg_mode
= MODE_RESOLVE_HOST
;
67 char **arg_set_dns
= NULL
;
68 char **arg_set_domain
= NULL
;
69 static const char *arg_set_llmnr
= NULL
;
70 static const char *arg_set_mdns
= NULL
;
71 static const char *arg_set_dns_over_tls
= NULL
;
72 static const char *arg_set_dnssec
= NULL
;
73 static char **arg_set_nta
= NULL
;
75 STATIC_DESTRUCTOR_REGISTER(arg_ifname
, freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_set_dns
, strv_freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_set_domain
, strv_freep
);
78 STATIC_DESTRUCTOR_REGISTER(arg_set_nta
, strv_freep
);
80 typedef enum StatusMode
{
92 typedef struct InterfaceInfo
{
97 static int interface_info_compare(const InterfaceInfo
*a
, const InterfaceInfo
*b
) {
100 r
= CMP(a
->index
, b
->index
);
104 return strcmp_ptr(a
->name
, b
->name
);
107 int ifname_mangle(const char *s
) {
108 _cleanup_free_
char *iface
= NULL
;
117 ifi
= rtnl_resolve_interface(NULL
, iface
);
119 if (ifi
== -ENODEV
&& arg_ifindex_permissive
) {
120 log_debug("Interface '%s' not found, but -f specified, ignoring.", iface
);
124 return log_error_errno(ifi
, "Failed to resolve interface \"%s\": %m", iface
);
127 if (arg_ifindex
> 0 && arg_ifindex
!= ifi
)
128 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified multiple different interfaces. Refusing.");
131 free_and_replace(arg_ifname
, iface
);
136 int ifname_resolvconf_mangle(const char *s
) {
141 dot
= strchr(s
, '.');
143 _cleanup_free_
char *iface
= NULL
;
145 log_debug("Ignoring protocol specifier '%s'.", dot
+ 1);
146 iface
= strndup(s
, dot
- s
);
149 return ifname_mangle(iface
);
151 return ifname_mangle(s
);
154 static void print_source(uint64_t flags
, usec_t rtt
) {
161 printf("\n%s-- Information acquired via", ansi_grey());
163 printf(" protocol%s%s%s%s%s",
164 flags
& SD_RESOLVED_DNS
? " DNS" :"",
165 flags
& SD_RESOLVED_LLMNR_IPV4
? " LLMNR/IPv4" : "",
166 flags
& SD_RESOLVED_LLMNR_IPV6
? " LLMNR/IPv6" : "",
167 flags
& SD_RESOLVED_MDNS_IPV4
? " mDNS/IPv4" : "",
168 flags
& SD_RESOLVED_MDNS_IPV6
? " mDNS/IPv6" : "");
171 "%s-- Data is authenticated: %s; Data was acquired via local or encrypted transport: %s%s\n",
172 FORMAT_TIMESPAN(rtt
, 100),
175 yes_no(flags
& SD_RESOLVED_AUTHENTICATED
),
176 yes_no(flags
& SD_RESOLVED_CONFIDENTIAL
),
179 if ((flags
& (SD_RESOLVED_FROM_MASK
|SD_RESOLVED_SYNTHETIC
)) != 0)
180 printf("%s-- Data from:%s%s%s%s%s%s\n",
182 FLAGS_SET(flags
, SD_RESOLVED_SYNTHETIC
) ? " synthetic" : "",
183 FLAGS_SET(flags
, SD_RESOLVED_FROM_CACHE
) ? " cache" : "",
184 FLAGS_SET(flags
, SD_RESOLVED_FROM_ZONE
) ? " zone" : "",
185 FLAGS_SET(flags
, SD_RESOLVED_FROM_TRUST_ANCHOR
) ? " trust-anchor" : "",
186 FLAGS_SET(flags
, SD_RESOLVED_FROM_NETWORK
) ? " network" : "",
190 static void print_ifindex_comment(int printed_so_far
, int ifindex
) {
191 char ifname
[IF_NAMESIZE
];
197 r
= format_ifname(ifindex
, ifname
);
199 return (void) log_warning_errno(r
, "Failed to resolve interface name for index %i, ignoring: %m", ifindex
);
201 printf("%*s%s-- link: %s%s",
202 60 > printed_so_far
? 60 - printed_so_far
: 0, " ", /* Align comment to the 60th column */
203 ansi_grey(), ifname
, ansi_normal());
206 static int resolve_host(sd_bus
*bus
, const char *name
) {
207 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
208 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
209 const char *canonical
= NULL
;
217 log_debug("Resolving %s (family %s, interface %s).", name
, af_to_name(arg_family
) ?: "*", isempty(arg_ifname
) ? "*" : arg_ifname
);
219 r
= bus_message_new_method_call(bus
, &req
, bus_resolve_mgr
, "ResolveHostname");
221 return bus_log_create_error(r
);
223 r
= sd_bus_message_append(req
, "isit", arg_ifindex
, name
, arg_family
, arg_flags
);
225 return bus_log_create_error(r
);
227 ts
= now(CLOCK_MONOTONIC
);
229 r
= sd_bus_call(bus
, req
, SD_RESOLVED_QUERY_TIMEOUT_USEC
, &error
, &reply
);
231 return log_error_errno(r
, "%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
233 ts
= now(CLOCK_MONOTONIC
) - ts
;
235 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
237 return bus_log_parse_error(r
);
239 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
240 _cleanup_free_
char *pretty
= NULL
;
241 int ifindex
, family
, k
;
242 union in_addr_union a
;
244 assert_cc(sizeof(int) == sizeof(int32_t));
246 r
= sd_bus_message_read(reply
, "i", &ifindex
);
248 return bus_log_parse_error(r
);
250 sd_bus_error_free(&error
);
251 r
= bus_message_read_in_addr_auto(reply
, &error
, &family
, &a
);
252 if (r
< 0 && !sd_bus_error_has_name(&error
, SD_BUS_ERROR_INVALID_ARGS
))
253 return log_error_errno(r
, "%s: systemd-resolved returned invalid result: %s", name
, bus_error_message(&error
, r
));
255 r
= sd_bus_message_exit_container(reply
);
257 return bus_log_parse_error(r
);
259 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_INVALID_ARGS
)) {
260 log_debug_errno(r
, "%s: systemd-resolved returned invalid result, ignoring: %s", name
, bus_error_message(&error
, r
));
264 r
= in_addr_ifindex_to_string(family
, &a
, ifindex
, &pretty
);
266 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
268 k
= printf("%*s%s %s%s%s",
269 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
270 ansi_highlight(), pretty
, ansi_normal());
272 print_ifindex_comment(k
, ifindex
);
278 return bus_log_parse_error(r
);
280 r
= sd_bus_message_exit_container(reply
);
282 return bus_log_parse_error(r
);
284 r
= sd_bus_message_read(reply
, "st", &canonical
, &flags
);
286 return bus_log_parse_error(r
);
288 if (!streq(name
, canonical
))
289 printf("%*s%s (%s)\n",
290 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
294 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
295 "%s: no addresses found", name
);
297 print_source(flags
, ts
);
302 static int resolve_address(sd_bus
*bus
, int family
, const union in_addr_union
*address
, int ifindex
) {
303 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
304 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
305 _cleanup_free_
char *pretty
= NULL
;
312 assert(IN_SET(family
, AF_INET
, AF_INET6
));
316 ifindex
= arg_ifindex
;
318 r
= in_addr_ifindex_to_string(family
, address
, ifindex
, &pretty
);
322 log_debug("Resolving %s.", pretty
);
324 r
= bus_message_new_method_call(bus
, &req
, bus_resolve_mgr
, "ResolveAddress");
326 return bus_log_create_error(r
);
328 r
= sd_bus_message_append(req
, "ii", ifindex
, family
);
330 return bus_log_create_error(r
);
332 r
= sd_bus_message_append_array(req
, 'y', address
, FAMILY_ADDRESS_SIZE(family
));
334 return bus_log_create_error(r
);
336 r
= sd_bus_message_append(req
, "t", arg_flags
);
338 return bus_log_create_error(r
);
340 ts
= now(CLOCK_MONOTONIC
);
342 r
= sd_bus_call(bus
, req
, SD_RESOLVED_QUERY_TIMEOUT_USEC
, &error
, &reply
);
344 return log_error_errno(r
, "%s: resolve call failed: %s", pretty
, bus_error_message(&error
, r
));
346 ts
= now(CLOCK_MONOTONIC
) - ts
;
348 r
= sd_bus_message_enter_container(reply
, 'a', "(is)");
350 return bus_log_create_error(r
);
352 while ((r
= sd_bus_message_enter_container(reply
, 'r', "is")) > 0) {
356 assert_cc(sizeof(int) == sizeof(int32_t));
358 r
= sd_bus_message_read(reply
, "is", &ifindex
, &n
);
362 r
= sd_bus_message_exit_container(reply
);
366 k
= printf("%*s%s %s%s%s",
367 (int) strlen(pretty
), c
== 0 ? pretty
: "",
369 ansi_highlight(), n
, ansi_normal());
371 print_ifindex_comment(k
, ifindex
);
377 return bus_log_parse_error(r
);
379 r
= sd_bus_message_exit_container(reply
);
381 return bus_log_parse_error(r
);
383 r
= sd_bus_message_read(reply
, "t", &flags
);
385 return bus_log_parse_error(r
);
388 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
389 "%s: no names found", pretty
);
391 print_source(flags
, ts
);
396 static int output_rr_packet(const void *d
, size_t l
, int ifindex
) {
397 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
398 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
401 r
= dns_packet_new(&p
, DNS_PROTOCOL_DNS
, 0, DNS_PACKET_SIZE_MAX
);
405 p
->refuse_compression
= true;
407 r
= dns_packet_append_blob(p
, d
, l
, NULL
);
411 r
= dns_packet_read_rr(p
, &rr
, NULL
, NULL
);
413 return log_error_errno(r
, "Failed to parse RR: %m");
415 if (arg_raw
== RAW_PAYLOAD
) {
419 k
= dns_resource_record_payload(rr
, &data
);
421 return log_error_errno(k
, "Cannot dump RR: %m");
422 fwrite(data
, 1, k
, stdout
);
427 s
= dns_resource_record_to_string(rr
);
432 print_ifindex_comment(k
, ifindex
);
439 static int idna_candidate(const char *name
, char **ret
) {
440 _cleanup_free_
char *idnafied
= NULL
;
446 r
= dns_name_apply_idna(name
, &idnafied
);
448 return log_error_errno(r
, "Failed to apply IDNA to name '%s': %m", name
);
449 if (r
> 0 && !streq(name
, idnafied
)) {
450 *ret
= TAKE_PTR(idnafied
);
458 static bool single_label_nonsynthetic(const char *name
) {
459 _cleanup_free_
char *first_label
= NULL
;
462 if (!dns_name_is_single_label(name
))
465 if (is_localhost(name
) || is_gateway_hostname(name
))
468 r
= resolve_system_hostname(NULL
, &first_label
);
470 log_warning_errno(r
, "Failed to determine the hostname: %m");
474 return !streq(name
, first_label
);
477 static int resolve_record(sd_bus
*bus
, const char *name
, uint16_t class, uint16_t type
, bool warn_missing
) {
478 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
479 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
480 _cleanup_free_
char *idnafied
= NULL
;
481 bool needs_authentication
= false;
489 log_debug("Resolving %s %s %s (interface %s).", name
, dns_class_to_string(class), dns_type_to_string(type
), isempty(arg_ifname
) ? "*" : arg_ifname
);
491 if (dns_name_dot_suffixed(name
) == 0 && single_label_nonsynthetic(name
))
492 log_notice("(Note that search domains are not appended when --type= is specified. "
493 "Please specify fully qualified domain names, or remove --type= switch from invocation in order to request regular hostname resolution.)");
495 r
= idna_candidate(name
, &idnafied
);
499 log_notice("(Note that IDNA translation is not applied when --type= is specified. "
500 "Please specify translated domain names — i.e. '%s' — when resolving raw records, or remove --type= switch from invocation in order to request regular hostname resolution.",
503 r
= bus_message_new_method_call(bus
, &req
, bus_resolve_mgr
, "ResolveRecord");
505 return bus_log_create_error(r
);
507 r
= sd_bus_message_append(req
, "isqqt", arg_ifindex
, name
, class, type
, arg_flags
);
509 return bus_log_create_error(r
);
511 ts
= now(CLOCK_MONOTONIC
);
513 r
= sd_bus_call(bus
, req
, SD_RESOLVED_QUERY_TIMEOUT_USEC
, &error
, &reply
);
515 if (warn_missing
|| r
!= -ENXIO
)
516 log_error("%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
520 ts
= now(CLOCK_MONOTONIC
) - ts
;
522 r
= sd_bus_message_enter_container(reply
, 'a', "(iqqay)");
524 return bus_log_parse_error(r
);
526 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iqqay")) > 0) {
532 assert_cc(sizeof(int) == sizeof(int32_t));
534 r
= sd_bus_message_read(reply
, "iqq", &ifindex
, &c
, &t
);
536 return bus_log_parse_error(r
);
538 r
= sd_bus_message_read_array(reply
, 'y', &d
, &l
);
540 return bus_log_parse_error(r
);
542 r
= sd_bus_message_exit_container(reply
);
544 return bus_log_parse_error(r
);
546 if (arg_raw
== RAW_PACKET
) {
547 uint64_t u64
= htole64(l
);
549 fwrite(&u64
, sizeof(u64
), 1, stdout
);
550 fwrite(d
, 1, l
, stdout
);
552 r
= output_rr_packet(d
, l
, ifindex
);
557 if (dns_type_needs_authentication(t
))
558 needs_authentication
= true;
563 return bus_log_parse_error(r
);
565 r
= sd_bus_message_exit_container(reply
);
567 return bus_log_parse_error(r
);
569 r
= sd_bus_message_read(reply
, "t", &flags
);
571 return bus_log_parse_error(r
);
575 log_error("%s: no records found", name
);
579 print_source(flags
, ts
);
581 if ((flags
& SD_RESOLVED_AUTHENTICATED
) == 0 && needs_authentication
) {
584 fprintf(stderr
, "\n%s"
585 "WARNING: The resources shown contain cryptographic key data which could not be\n"
586 " authenticated. It is not suitable to authenticate any communication.\n"
587 " This is usually indication that DNSSEC authentication was not enabled\n"
588 " or is not available for the selected protocol or DNS servers.%s\n",
589 ansi_highlight_red(),
596 static int resolve_rfc4501(sd_bus
*bus
, const char *name
) {
597 uint16_t type
= 0, class = 0;
598 const char *p
, *q
, *n
;
603 assert(startswith(name
, "dns:"));
605 /* Parse RFC 4501 dns: URIs */
615 e
= strchr(p
+ 2, '/');
620 log_warning("DNS authority specification not supported; ignoring specified authority.");
627 n
= strndupa_safe(p
, q
- p
);
633 f
= startswith_no_case(q
, "class=");
635 _cleanup_free_
char *t
= NULL
;
639 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
640 "DNS class specified twice.");
642 e
= strchrnul(f
, ';');
643 t
= strndup(f
, e
- f
);
647 r
= dns_class_from_string(t
);
649 return log_error_errno(r
, "Unknown DNS class %s.", t
);
661 f
= startswith_no_case(q
, "type=");
663 _cleanup_free_
char *t
= NULL
;
667 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
668 "DNS type specified twice.");
670 e
= strchrnul(f
, ';');
671 t
= strndup(f
, e
- f
);
675 r
= dns_type_from_string(t
);
677 return log_error_errno(r
, "Unknown DNS type %s: %m", t
);
695 class = arg_class
?: DNS_CLASS_IN
;
697 type
= arg_type
?: DNS_TYPE_A
;
699 return resolve_record(bus
, n
, class, type
, true);
702 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
703 "Invalid DNS URI: %s", name
);
706 static int verb_query(int argc
, char **argv
, void *userdata
) {
707 sd_bus
*bus
= userdata
;
711 STRV_FOREACH(p
, argv
+ 1) {
712 q
= resolve_record(bus
, *p
, arg_class
, arg_type
, true);
718 STRV_FOREACH(p
, argv
+ 1) {
719 if (startswith(*p
, "dns:"))
720 q
= resolve_rfc4501(bus
, *p
);
723 union in_addr_union a
;
725 q
= in_addr_ifindex_from_string_auto(*p
, &family
, &a
, &ifindex
);
727 q
= resolve_address(bus
, family
, &a
, ifindex
);
729 q
= resolve_host(bus
, *p
);
738 static int resolve_service(sd_bus
*bus
, const char *name
, const char *type
, const char *domain
) {
739 const char *canonical_name
, *canonical_type
, *canonical_domain
;
740 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
741 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
752 name
= empty_to_null(name
);
753 type
= empty_to_null(type
);
756 log_debug("Resolving service \"%s\" of type %s in %s (family %s, interface %s).", name
, type
, domain
, af_to_name(arg_family
) ?: "*", isempty(arg_ifname
) ? "*" : arg_ifname
);
758 log_debug("Resolving service type %s of %s (family %s, interface %s).", type
, domain
, af_to_name(arg_family
) ?: "*", isempty(arg_ifname
) ? "*" : arg_ifname
);
760 log_debug("Resolving service type %s (family %s, interface %s).", domain
, af_to_name(arg_family
) ?: "*", isempty(arg_ifname
) ? "*" : arg_ifname
);
762 r
= bus_message_new_method_call(bus
, &req
, bus_resolve_mgr
, "ResolveService");
764 return bus_log_create_error(r
);
766 r
= sd_bus_message_append(req
, "isssit", arg_ifindex
, name
, type
, domain
, arg_family
, arg_flags
);
768 return bus_log_create_error(r
);
770 ts
= now(CLOCK_MONOTONIC
);
772 r
= sd_bus_call(bus
, req
, SD_RESOLVED_QUERY_TIMEOUT_USEC
, &error
, &reply
);
774 return log_error_errno(r
, "Resolve call failed: %s", bus_error_message(&error
, r
));
776 ts
= now(CLOCK_MONOTONIC
) - ts
;
778 r
= sd_bus_message_enter_container(reply
, 'a', "(qqqsa(iiay)s)");
780 return bus_log_parse_error(r
);
783 (name
? strlen(name
) + 1 : 0) +
784 (type
? strlen(type
) + 1 : 0) +
788 while ((r
= sd_bus_message_enter_container(reply
, 'r', "qqqsa(iiay)s")) > 0) {
789 uint16_t priority
, weight
, port
;
790 const char *hostname
, *canonical
;
792 r
= sd_bus_message_read(reply
, "qqqs", &priority
, &weight
, &port
, &hostname
);
794 return bus_log_parse_error(r
);
797 printf("%*s%s", (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? "/" : " ");
799 printf("%*s%s", (int) strlen(type
), c
== 0 ? type
: "", c
== 0 ? "/" : " ");
801 printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
802 (int) strlen(domain
), c
== 0 ? domain
: "",
807 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
809 return bus_log_parse_error(r
);
811 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
812 _cleanup_free_
char *pretty
= NULL
;
813 int ifindex
, family
, k
;
814 union in_addr_union a
;;
816 assert_cc(sizeof(int) == sizeof(int32_t));
818 r
= sd_bus_message_read(reply
, "i", &ifindex
);
820 return bus_log_parse_error(r
);
822 sd_bus_error_free(&error
);
823 r
= bus_message_read_in_addr_auto(reply
, &error
, &family
, &a
);
824 if (r
< 0 && !sd_bus_error_has_name(&error
, SD_BUS_ERROR_INVALID_ARGS
))
825 return log_error_errno(r
, "%s: systemd-resolved returned invalid result: %s", name
, bus_error_message(&error
, r
));
827 r
= sd_bus_message_exit_container(reply
);
829 return bus_log_parse_error(r
);
831 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_INVALID_ARGS
)) {
832 log_debug_errno(r
, "%s: systemd-resolved returned invalid result, ignoring: %s", name
, bus_error_message(&error
, r
));
836 r
= in_addr_ifindex_to_string(family
, &a
, ifindex
, &pretty
);
838 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
840 k
= printf("%*s%s", (int) indent
, "", pretty
);
841 print_ifindex_comment(k
, ifindex
);
845 return bus_log_parse_error(r
);
847 r
= sd_bus_message_exit_container(reply
);
849 return bus_log_parse_error(r
);
851 r
= sd_bus_message_read(reply
, "s", &canonical
);
853 return bus_log_parse_error(r
);
855 if (!streq(hostname
, canonical
))
856 printf("%*s(%s)\n", (int) indent
, "", canonical
);
858 r
= sd_bus_message_exit_container(reply
);
860 return bus_log_parse_error(r
);
865 return bus_log_parse_error(r
);
867 r
= sd_bus_message_exit_container(reply
);
869 return bus_log_parse_error(r
);
871 r
= sd_bus_message_enter_container(reply
, 'a', "ay");
873 return bus_log_parse_error(r
);
875 while ((r
= sd_bus_message_read_array(reply
, 'y', (const void**) &p
, &sz
)) > 0) {
876 _cleanup_free_
char *escaped
= NULL
;
878 escaped
= cescape_length(p
, sz
);
882 printf("%*s%s\n", (int) indent
, "", escaped
);
885 return bus_log_parse_error(r
);
887 r
= sd_bus_message_exit_container(reply
);
889 return bus_log_parse_error(r
);
891 r
= sd_bus_message_read(reply
, "ssst", &canonical_name
, &canonical_type
, &canonical_domain
, &flags
);
893 return bus_log_parse_error(r
);
895 canonical_name
= empty_to_null(canonical_name
);
896 canonical_type
= empty_to_null(canonical_type
);
898 if (!streq_ptr(name
, canonical_name
) ||
899 !streq_ptr(type
, canonical_type
) ||
900 !streq_ptr(domain
, canonical_domain
)) {
902 printf("%*s(", (int) indent
, "");
905 printf("%s/", canonical_name
);
907 printf("%s/", canonical_type
);
909 printf("%s)\n", canonical_domain
);
912 print_source(flags
, ts
);
917 static int verb_service(int argc
, char **argv
, void *userdata
) {
918 sd_bus
*bus
= userdata
;
921 return resolve_service(bus
, NULL
, NULL
, argv
[1]);
923 return resolve_service(bus
, NULL
, argv
[1], argv
[2]);
925 return resolve_service(bus
, argv
[1], argv
[2], argv
[3]);
928 static int resolve_openpgp(sd_bus
*bus
, const char *address
) {
929 const char *domain
, *full
;
931 _cleanup_free_
char *hashed
= NULL
;
936 domain
= strrchr(address
, '@');
938 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
939 "Address does not contain '@': \"%s\"", address
);
940 if (domain
== address
|| domain
[1] == '\0')
941 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
942 "Address starts or ends with '@': \"%s\"", address
);
945 r
= string_hashsum_sha256(address
, domain
- 1 - address
, &hashed
);
947 return log_error_errno(r
, "Hashing failed: %m");
949 strshorten(hashed
, 56);
951 full
= strjoina(hashed
, "._openpgpkey.", domain
);
952 log_debug("Looking up \"%s\".", full
);
954 r
= resolve_record(bus
, full
,
955 arg_class
?: DNS_CLASS_IN
,
956 arg_type
?: DNS_TYPE_OPENPGPKEY
, false);
958 if (IN_SET(r
, -ENXIO
, -ESRCH
)) { /* NXDOMAIN or NODATA? */
959 hashed
= mfree(hashed
);
960 r
= string_hashsum_sha224(address
, domain
- 1 - address
, &hashed
);
962 return log_error_errno(r
, "Hashing failed: %m");
964 full
= strjoina(hashed
, "._openpgpkey.", domain
);
965 log_debug("Looking up \"%s\".", full
);
967 return resolve_record(bus
, full
,
968 arg_class
?: DNS_CLASS_IN
,
969 arg_type
?: DNS_TYPE_OPENPGPKEY
, true);
975 static int verb_openpgp(int argc
, char **argv
, void *userdata
) {
976 sd_bus
*bus
= userdata
;
979 STRV_FOREACH(p
, argv
+ 1) {
980 q
= resolve_openpgp(bus
, *p
);
988 static int resolve_tlsa(sd_bus
*bus
, const char *family
, const char *address
) {
990 uint16_t port_num
= 443;
991 _cleanup_free_
char *full
= NULL
;
997 port
= strrchr(address
, ':');
999 r
= parse_ip_port(port
+ 1, &port_num
);
1001 return log_error_errno(r
, "Invalid port \"%s\".", port
+ 1);
1003 address
= strndupa_safe(address
, port
- address
);
1006 r
= asprintf(&full
, "_%u._%s.%s",
1013 log_debug("Looking up \"%s\".", full
);
1015 return resolve_record(bus
, full
,
1016 arg_class
?: DNS_CLASS_IN
,
1017 arg_type
?: DNS_TYPE_TLSA
, true);
1020 static bool service_family_is_valid(const char *s
) {
1021 return STR_IN_SET(s
, "tcp", "udp", "sctp");
1024 static int verb_tlsa(int argc
, char **argv
, void *userdata
) {
1025 sd_bus
*bus
= userdata
;
1026 char **args
= argv
+ 1;
1027 const char *family
= "tcp";
1030 if (service_family_is_valid(argv
[1])) {
1035 STRV_FOREACH(p
, args
) {
1036 q
= resolve_tlsa(bus
, family
, *p
);
1044 static int show_statistics(int argc
, char **argv
, void *userdata
) {
1045 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1046 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1047 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1048 sd_bus
*bus
= userdata
;
1049 uint64_t n_current_transactions
, n_total_transactions
,
1050 cache_size
, n_cache_hit
, n_cache_miss
,
1051 n_dnssec_secure
, n_dnssec_insecure
, n_dnssec_bogus
, n_dnssec_indeterminate
;
1052 int r
, dnssec_supported
;
1056 r
= bus_get_property_trivial(bus
, bus_resolve_mgr
, "DNSSECSupported", &error
, 'b', &dnssec_supported
);
1058 return log_error_errno(r
, "Failed to get DNSSEC supported state: %s", bus_error_message(&error
, r
));
1060 printf("DNSSEC supported by current servers: %s%s%s\n\n",
1062 yes_no(dnssec_supported
),
1065 r
= bus_get_property(bus
, bus_resolve_mgr
, "TransactionStatistics", &error
, &reply
, "(tt)");
1067 return log_error_errno(r
, "Failed to get transaction statistics: %s", bus_error_message(&error
, r
));
1069 r
= sd_bus_message_read(reply
, "(tt)",
1070 &n_current_transactions
,
1071 &n_total_transactions
);
1073 return bus_log_parse_error(r
);
1075 reply
= sd_bus_message_unref(reply
);
1077 r
= bus_get_property(bus
, bus_resolve_mgr
, "CacheStatistics", &error
, &reply
, "(ttt)");
1079 return log_error_errno(r
, "Failed to get cache statistics: %s", bus_error_message(&error
, r
));
1081 r
= sd_bus_message_read(reply
, "(ttt)",
1086 return bus_log_parse_error(r
);
1088 reply
= sd_bus_message_unref(reply
);
1090 r
= bus_get_property(bus
, bus_resolve_mgr
, "DNSSECStatistics", &error
, &reply
, "(tttt)");
1092 return log_error_errno(r
, "Failed to get DNSSEC statistics: %s", bus_error_message(&error
, r
));
1094 r
= sd_bus_message_read(reply
, "(tttt)",
1098 &n_dnssec_indeterminate
);
1100 return bus_log_parse_error(r
);
1102 table
= table_new("key", "value");
1106 table_set_header(table
, false);
1108 r
= table_add_many(table
,
1109 TABLE_STRING
, "Transactions",
1110 TABLE_SET_COLOR
, ansi_highlight(),
1112 TABLE_STRING
, "Current Transactions:",
1113 TABLE_SET_ALIGN_PERCENT
, 100,
1114 TABLE_UINT64
, n_current_transactions
,
1115 TABLE_STRING
, "Total Transactions:",
1116 TABLE_UINT64
, n_total_transactions
,
1117 TABLE_EMPTY
, TABLE_EMPTY
,
1118 TABLE_STRING
, "Cache",
1119 TABLE_SET_COLOR
, ansi_highlight(),
1120 TABLE_SET_ALIGN_PERCENT
, 0,
1122 TABLE_STRING
, "Current Cache Size:",
1123 TABLE_SET_ALIGN_PERCENT
, 100,
1124 TABLE_UINT64
, cache_size
,
1125 TABLE_STRING
, "Cache Hits:",
1126 TABLE_UINT64
, n_cache_hit
,
1127 TABLE_STRING
, "Cache Misses:",
1128 TABLE_UINT64
, n_cache_miss
,
1129 TABLE_EMPTY
, TABLE_EMPTY
,
1130 TABLE_STRING
, "DNSSEC Verdicts",
1131 TABLE_SET_COLOR
, ansi_highlight(),
1132 TABLE_SET_ALIGN_PERCENT
, 0,
1134 TABLE_STRING
, "Secure:",
1135 TABLE_SET_ALIGN_PERCENT
, 100,
1136 TABLE_UINT64
, n_dnssec_secure
,
1137 TABLE_STRING
, "Insecure:",
1138 TABLE_UINT64
, n_dnssec_insecure
,
1139 TABLE_STRING
, "Bogus:",
1140 TABLE_UINT64
, n_dnssec_bogus
,
1141 TABLE_STRING
, "Indeterminate:",
1142 TABLE_UINT64
, n_dnssec_indeterminate
);
1144 table_log_add_error(r
);
1146 r
= table_print(table
, NULL
);
1148 return table_log_print_error(r
);
1153 static int reset_statistics(int argc
, char **argv
, void *userdata
) {
1154 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1155 sd_bus
*bus
= userdata
;
1158 r
= bus_call_method(bus
, bus_resolve_mgr
, "ResetStatistics", &error
, NULL
, NULL
);
1160 return log_error_errno(r
, "Failed to reset statistics: %s", bus_error_message(&error
, r
));
1165 static int flush_caches(int argc
, char **argv
, void *userdata
) {
1166 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1167 sd_bus
*bus
= userdata
;
1170 r
= bus_call_method(bus
, bus_resolve_mgr
, "FlushCaches", &error
, NULL
, NULL
);
1172 return log_error_errno(r
, "Failed to flush caches: %s", bus_error_message(&error
, r
));
1177 static int reset_server_features(int argc
, char **argv
, void *userdata
) {
1178 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1179 sd_bus
*bus
= userdata
;
1182 r
= bus_call_method(bus
, bus_resolve_mgr
, "ResetServerFeatures", &error
, NULL
, NULL
);
1184 return log_error_errno(r
, "Failed to reset server features: %s", bus_error_message(&error
, r
));
1189 static int read_dns_server_one(sd_bus_message
*m
, bool with_ifindex
, bool extended
, char **ret
) {
1190 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1191 _cleanup_free_
char *pretty
= NULL
;
1192 int ifindex
, family
, r
, k
;
1193 union in_addr_union a
;
1194 const char *name
= NULL
;
1200 r
= sd_bus_message_enter_container(m
, 'r', with_ifindex
? (extended
? "iiayqs" : "iiay") : (extended
? "iayqs" : "iay"));
1205 r
= sd_bus_message_read(m
, "i", &ifindex
);
1210 k
= bus_message_read_in_addr_auto(m
, &error
, &family
, &a
);
1211 if (k
< 0 && !sd_bus_error_has_name(&error
, SD_BUS_ERROR_INVALID_ARGS
))
1215 r
= sd_bus_message_read(m
, "q", &port
);
1219 r
= sd_bus_message_read(m
, "s", &name
);
1224 r
= sd_bus_message_exit_container(m
);
1229 log_debug("Invalid DNS server, ignoring: %s", bus_error_message(&error
, k
));
1234 if (with_ifindex
&& ifindex
!= 0) {
1235 /* only show the global ones here */
1240 r
= in_addr_port_ifindex_name_to_string(family
, &a
, port
, ifindex
, name
, &pretty
);
1244 *ret
= TAKE_PTR(pretty
);
1249 static int map_link_dns_servers_internal(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
, bool extended
) {
1250 char ***l
= userdata
;
1258 r
= sd_bus_message_enter_container(m
, 'a', extended
? "(iayqs)" : "(iay)");
1263 _cleanup_free_
char *pretty
= NULL
;
1265 r
= read_dns_server_one(m
, false, extended
, &pretty
);
1271 if (isempty(pretty
))
1274 r
= strv_consume(l
, TAKE_PTR(pretty
));
1279 r
= sd_bus_message_exit_container(m
);
1286 static int map_link_dns_servers(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1287 return map_link_dns_servers_internal(bus
, member
, m
, error
, userdata
, false);
1290 static int map_link_dns_servers_ex(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1291 return map_link_dns_servers_internal(bus
, member
, m
, error
, userdata
, true);
1294 static int map_link_current_dns_server(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1298 return read_dns_server_one(m
, false, false, userdata
);
1301 static int map_link_current_dns_server_ex(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1305 return read_dns_server_one(m
, false, true, userdata
);
1308 static int read_domain_one(sd_bus_message
*m
, bool with_ifindex
, char **ret
) {
1309 _cleanup_free_
char *str
= NULL
;
1310 int ifindex
, route_only
, r
;
1317 r
= sd_bus_message_read(m
, "(isb)", &ifindex
, &domain
, &route_only
);
1319 r
= sd_bus_message_read(m
, "(sb)", &domain
, &route_only
);
1323 if (with_ifindex
&& ifindex
!= 0) {
1324 /* only show the global ones here */
1330 str
= strjoin("~", domain
);
1332 str
= strdup(domain
);
1336 *ret
= TAKE_PTR(str
);
1341 static int map_link_domains(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1342 char ***l
= userdata
;
1350 r
= sd_bus_message_enter_container(m
, 'a', "(sb)");
1355 _cleanup_free_
char *pretty
= NULL
;
1357 r
= read_domain_one(m
, false, &pretty
);
1363 if (isempty(pretty
))
1366 r
= strv_consume(l
, TAKE_PTR(pretty
));
1371 r
= sd_bus_message_exit_container(m
);
1380 static int status_print_strv_ifindex(int ifindex
, const char *ifname
, char **p
) {
1381 const unsigned indent
= strlen("Global: "); /* Use the same indentation everywhere to make things nice */
1385 printf("%s%nLink %i (%s)%n%s:", ansi_highlight(), &pos1
, ifindex
, ifname
, &pos2
, ansi_normal());
1387 printf("%s%nGlobal%n%s:", ansi_highlight(), &pos1
, &pos2
, ansi_normal());
1389 size_t cols
= columns(), position
= pos2
- pos1
+ 2;
1391 STRV_FOREACH(i
, p
) {
1392 size_t our_len
= utf8_console_width(*i
); /* This returns -1 on invalid utf-8 (which shouldn't happen).
1393 * If that happens, we'll just print one item per line. */
1395 if (position
<= indent
|| size_add(size_add(position
, 1), our_len
) < cols
) {
1397 position
= size_add(size_add(position
, 1), our_len
);
1399 printf("\n%*s%s", indent
, "", *i
);
1400 position
= size_add(our_len
, indent
);
1409 static int status_print_strv_global(char **p
) {
1410 return status_print_strv_ifindex(0, NULL
, p
);
1413 typedef struct LinkInfo
{
1414 uint64_t scopes_mask
;
1417 const char *dns_over_tls
;
1420 char *current_dns_ex
;
1425 bool dnssec_supported
;
1429 typedef struct GlobalInfo
{
1431 char *current_dns_ex
;
1434 char **fallback_dns
;
1435 char **fallback_dns_ex
;
1440 const char *dns_over_tls
;
1442 const char *resolv_conf_mode
;
1443 bool dnssec_supported
;
1446 static void link_info_clear(LinkInfo
*p
) {
1447 free(p
->current_dns
);
1448 free(p
->current_dns_ex
);
1450 strv_free(p
->dns_ex
);
1451 strv_free(p
->domains
);
1455 static void global_info_clear(GlobalInfo
*p
) {
1456 free(p
->current_dns
);
1457 free(p
->current_dns_ex
);
1459 strv_free(p
->dns_ex
);
1460 strv_free(p
->fallback_dns
);
1461 strv_free(p
->fallback_dns_ex
);
1462 strv_free(p
->domains
);
1466 static int dump_list(Table
*table
, const char *prefix
, char * const *l
) {
1469 if (strv_isempty(l
))
1472 r
= table_add_many(table
,
1473 TABLE_STRING
, prefix
,
1474 TABLE_STRV_WRAPPED
, l
);
1476 return table_log_add_error(r
);
1481 static int strv_extend_extended_bool(char ***strv
, const char *name
, const char *value
) {
1485 r
= parse_boolean(value
);
1487 return strv_extendf(strv
, "%s%s", plus_minus(r
), name
);
1490 return strv_extendf(strv
, "%s=%s", name
, value
?: "???");
1493 static char** link_protocol_status(const LinkInfo
*info
) {
1494 _cleanup_strv_free_
char **s
= NULL
;
1496 if (strv_extendf(&s
, "%sDefaultRoute", plus_minus(info
->default_route
)) < 0)
1499 if (strv_extend_extended_bool(&s
, "LLMNR", info
->llmnr
) < 0)
1502 if (strv_extend_extended_bool(&s
, "mDNS", info
->mdns
) < 0)
1505 if (strv_extend_extended_bool(&s
, "DNSOverTLS", info
->dns_over_tls
) < 0)
1508 if (strv_extendf(&s
, "DNSSEC=%s/%s",
1509 info
->dnssec
?: "???",
1510 info
->dnssec_supported
? "supported" : "unsupported") < 0)
1516 static char** global_protocol_status(const GlobalInfo
*info
) {
1517 _cleanup_strv_free_
char **s
= NULL
;
1519 if (strv_extend_extended_bool(&s
, "LLMNR", info
->llmnr
) < 0)
1522 if (strv_extend_extended_bool(&s
, "mDNS", info
->mdns
) < 0)
1525 if (strv_extend_extended_bool(&s
, "DNSOverTLS", info
->dns_over_tls
) < 0)
1528 if (strv_extendf(&s
, "DNSSEC=%s/%s",
1529 info
->dnssec
?: "???",
1530 info
->dnssec_supported
? "supported" : "unsupported") < 0)
1536 static int status_ifindex(sd_bus
*bus
, int ifindex
, const char *name
, StatusMode mode
, bool *empty_line
) {
1537 static const struct bus_properties_map property_map
[] = {
1538 { "ScopesMask", "t", NULL
, offsetof(LinkInfo
, scopes_mask
) },
1539 { "DNS", "a(iay)", map_link_dns_servers
, offsetof(LinkInfo
, dns
) },
1540 { "DNSEx", "a(iayqs)", map_link_dns_servers_ex
, offsetof(LinkInfo
, dns_ex
) },
1541 { "CurrentDNSServer", "(iay)", map_link_current_dns_server
, offsetof(LinkInfo
, current_dns
) },
1542 { "CurrentDNSServerEx", "(iayqs)", map_link_current_dns_server_ex
, offsetof(LinkInfo
, current_dns_ex
) },
1543 { "Domains", "a(sb)", map_link_domains
, offsetof(LinkInfo
, domains
) },
1544 { "DefaultRoute", "b", NULL
, offsetof(LinkInfo
, default_route
) },
1545 { "LLMNR", "s", NULL
, offsetof(LinkInfo
, llmnr
) },
1546 { "MulticastDNS", "s", NULL
, offsetof(LinkInfo
, mdns
) },
1547 { "DNSOverTLS", "s", NULL
, offsetof(LinkInfo
, dns_over_tls
) },
1548 { "DNSSEC", "s", NULL
, offsetof(LinkInfo
, dnssec
) },
1549 { "DNSSECNegativeTrustAnchors", "as", bus_map_strv_sort
, offsetof(LinkInfo
, ntas
) },
1550 { "DNSSECSupported", "b", NULL
, offsetof(LinkInfo
, dnssec_supported
) },
1553 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1554 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1555 _cleanup_(link_info_clear
) LinkInfo link_info
= {};
1556 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1557 _cleanup_free_
char *p
= NULL
;
1558 char ifi
[DECIMAL_STR_MAX(int)], ifname
[IF_NAMESIZE
];
1562 assert(ifindex
> 0);
1565 r
= format_ifname(ifindex
, ifname
);
1567 return log_error_errno(r
, "Failed to resolve interface name for %i: %m", ifindex
);
1572 xsprintf(ifi
, "%i", ifindex
);
1573 r
= sd_bus_path_encode("/org/freedesktop/resolve1/link", ifi
, &p
);
1577 r
= bus_map_all_properties(bus
,
1578 "org.freedesktop.resolve1",
1581 BUS_MAP_BOOLEAN_AS_BOOL
,
1586 return log_error_errno(r
, "Failed to get link data for %i: %s", ifindex
, bus_error_message(&error
, r
));
1588 pager_open(arg_pager_flags
);
1590 if (mode
== STATUS_DNS
)
1591 return status_print_strv_ifindex(ifindex
, name
, link_info
.dns_ex
?: link_info
.dns
);
1593 if (mode
== STATUS_DOMAIN
)
1594 return status_print_strv_ifindex(ifindex
, name
, link_info
.domains
);
1596 if (mode
== STATUS_NTA
)
1597 return status_print_strv_ifindex(ifindex
, name
, link_info
.ntas
);
1599 if (mode
== STATUS_DEFAULT_ROUTE
) {
1600 printf("%sLink %i (%s)%s: %s\n",
1601 ansi_highlight(), ifindex
, name
, ansi_normal(),
1602 yes_no(link_info
.default_route
));
1607 if (mode
== STATUS_LLMNR
) {
1608 printf("%sLink %i (%s)%s: %s\n",
1609 ansi_highlight(), ifindex
, name
, ansi_normal(),
1610 strna(link_info
.llmnr
));
1615 if (mode
== STATUS_MDNS
) {
1616 printf("%sLink %i (%s)%s: %s\n",
1617 ansi_highlight(), ifindex
, name
, ansi_normal(),
1618 strna(link_info
.mdns
));
1623 if (mode
== STATUS_PRIVATE
) {
1624 printf("%sLink %i (%s)%s: %s\n",
1625 ansi_highlight(), ifindex
, name
, ansi_normal(),
1626 strna(link_info
.dns_over_tls
));
1631 if (mode
== STATUS_DNSSEC
) {
1632 printf("%sLink %i (%s)%s: %s\n",
1633 ansi_highlight(), ifindex
, name
, ansi_normal(),
1634 strna(link_info
.dnssec
));
1639 if (empty_line
&& *empty_line
)
1640 fputc('\n', stdout
);
1642 printf("%sLink %i (%s)%s\n",
1643 ansi_highlight(), ifindex
, name
, ansi_normal());
1645 table
= table_new("key", "value");
1649 table_set_header(table
, false);
1651 r
= table_add_many(table
,
1652 TABLE_STRING
, "Current Scopes:",
1653 TABLE_SET_ALIGN_PERCENT
, 100);
1655 return table_log_add_error(r
);
1657 if (link_info
.scopes_mask
== 0)
1658 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "none");
1660 _cleanup_free_
char *buf
= NULL
;
1663 if (asprintf(&buf
, "%s%s%s%s%s",
1664 link_info
.scopes_mask
& SD_RESOLVED_DNS
? "DNS " : "",
1665 link_info
.scopes_mask
& SD_RESOLVED_LLMNR_IPV4
? "LLMNR/IPv4 " : "",
1666 link_info
.scopes_mask
& SD_RESOLVED_LLMNR_IPV6
? "LLMNR/IPv6 " : "",
1667 link_info
.scopes_mask
& SD_RESOLVED_MDNS_IPV4
? "mDNS/IPv4 " : "",
1668 link_info
.scopes_mask
& SD_RESOLVED_MDNS_IPV6
? "mDNS/IPv6 " : "") < 0)
1673 buf
[len
- 1] = '\0';
1675 r
= table_add_cell(table
, NULL
, TABLE_STRING
, buf
);
1678 return table_log_add_error(r
);
1680 _cleanup_strv_free_
char **pstatus
= link_protocol_status(&link_info
);
1684 r
= table_add_many(table
,
1685 TABLE_STRING
, "Protocols:",
1686 TABLE_STRV_WRAPPED
, pstatus
);
1688 return table_log_add_error(r
);
1690 if (link_info
.current_dns
) {
1691 r
= table_add_many(table
,
1692 TABLE_STRING
, "Current DNS Server:",
1693 TABLE_STRING
, link_info
.current_dns_ex
?: link_info
.current_dns
);
1695 return table_log_add_error(r
);
1698 r
= dump_list(table
, "DNS Servers:", link_info
.dns_ex
?: link_info
.dns
);
1702 r
= dump_list(table
, "DNS Domain:", link_info
.domains
);
1706 r
= table_print(table
, NULL
);
1708 return table_log_print_error(r
);
1716 static int map_global_dns_servers_internal(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
, bool extended
) {
1717 char ***l
= userdata
;
1725 r
= sd_bus_message_enter_container(m
, 'a', extended
? "(iiayqs)" : "(iiay)");
1730 _cleanup_free_
char *pretty
= NULL
;
1732 r
= read_dns_server_one(m
, true, extended
, &pretty
);
1738 if (isempty(pretty
))
1741 r
= strv_consume(l
, TAKE_PTR(pretty
));
1746 r
= sd_bus_message_exit_container(m
);
1753 static int map_global_dns_servers(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1754 return map_global_dns_servers_internal(bus
, member
, m
, error
, userdata
, false);
1757 static int map_global_dns_servers_ex(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1758 return map_global_dns_servers_internal(bus
, member
, m
, error
, userdata
, true);
1761 static int map_global_current_dns_server(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1765 return read_dns_server_one(m
, true, false, userdata
);
1768 static int map_global_current_dns_server_ex(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1772 return read_dns_server_one(m
, true, true, userdata
);
1775 static int map_global_domains(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1776 char ***l
= userdata
;
1784 r
= sd_bus_message_enter_container(m
, 'a', "(isb)");
1789 _cleanup_free_
char *pretty
= NULL
;
1791 r
= read_domain_one(m
, true, &pretty
);
1797 if (isempty(pretty
))
1800 r
= strv_consume(l
, TAKE_PTR(pretty
));
1805 r
= sd_bus_message_exit_container(m
);
1814 static int status_global(sd_bus
*bus
, StatusMode mode
, bool *empty_line
) {
1815 static const struct bus_properties_map property_map
[] = {
1816 { "DNS", "a(iiay)", map_global_dns_servers
, offsetof(GlobalInfo
, dns
) },
1817 { "DNSEx", "a(iiayqs)", map_global_dns_servers_ex
, offsetof(GlobalInfo
, dns_ex
) },
1818 { "FallbackDNS", "a(iiay)", map_global_dns_servers
, offsetof(GlobalInfo
, fallback_dns
) },
1819 { "FallbackDNSEx", "a(iiayqs)", map_global_dns_servers_ex
, offsetof(GlobalInfo
, fallback_dns_ex
) },
1820 { "CurrentDNSServer", "(iiay)", map_global_current_dns_server
, offsetof(GlobalInfo
, current_dns
) },
1821 { "CurrentDNSServerEx", "(iiayqs)", map_global_current_dns_server_ex
, offsetof(GlobalInfo
, current_dns_ex
) },
1822 { "Domains", "a(isb)", map_global_domains
, offsetof(GlobalInfo
, domains
) },
1823 { "DNSSECNegativeTrustAnchors", "as", bus_map_strv_sort
, offsetof(GlobalInfo
, ntas
) },
1824 { "LLMNR", "s", NULL
, offsetof(GlobalInfo
, llmnr
) },
1825 { "MulticastDNS", "s", NULL
, offsetof(GlobalInfo
, mdns
) },
1826 { "DNSOverTLS", "s", NULL
, offsetof(GlobalInfo
, dns_over_tls
) },
1827 { "DNSSEC", "s", NULL
, offsetof(GlobalInfo
, dnssec
) },
1828 { "DNSSECSupported", "b", NULL
, offsetof(GlobalInfo
, dnssec_supported
) },
1829 { "ResolvConfMode", "s", NULL
, offsetof(GlobalInfo
, resolv_conf_mode
) },
1832 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1833 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1834 _cleanup_(global_info_clear
) GlobalInfo global_info
= {};
1835 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1841 r
= bus_map_all_properties(bus
,
1842 "org.freedesktop.resolve1",
1843 "/org/freedesktop/resolve1",
1845 BUS_MAP_BOOLEAN_AS_BOOL
,
1850 return log_error_errno(r
, "Failed to get global data: %s", bus_error_message(&error
, r
));
1852 pager_open(arg_pager_flags
);
1854 if (mode
== STATUS_DNS
)
1855 return status_print_strv_global(global_info
.dns_ex
?: global_info
.dns
);
1857 if (mode
== STATUS_DOMAIN
)
1858 return status_print_strv_global(global_info
.domains
);
1860 if (mode
== STATUS_NTA
)
1861 return status_print_strv_global(global_info
.ntas
);
1863 if (mode
== STATUS_LLMNR
) {
1864 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1865 strna(global_info
.llmnr
));
1870 if (mode
== STATUS_MDNS
) {
1871 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1872 strna(global_info
.mdns
));
1877 if (mode
== STATUS_PRIVATE
) {
1878 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1879 strna(global_info
.dns_over_tls
));
1884 if (mode
== STATUS_DNSSEC
) {
1885 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1886 strna(global_info
.dnssec
));
1891 printf("%sGlobal%s\n", ansi_highlight(), ansi_normal());
1893 table
= table_new("key", "value");
1897 table_set_header(table
, false);
1899 _cleanup_strv_free_
char **pstatus
= global_protocol_status(&global_info
);
1903 r
= table_add_many(table
,
1904 TABLE_STRING
, "Protocols:",
1905 TABLE_SET_ALIGN_PERCENT
, 100,
1906 TABLE_STRV_WRAPPED
, pstatus
);
1908 return table_log_add_error(r
);
1910 if (global_info
.resolv_conf_mode
) {
1911 r
= table_add_many(table
,
1912 TABLE_STRING
, "resolv.conf mode:",
1913 TABLE_STRING
, global_info
.resolv_conf_mode
);
1915 return table_log_add_error(r
);
1918 if (global_info
.current_dns
) {
1919 r
= table_add_many(table
,
1920 TABLE_STRING
, "Current DNS Server:",
1921 TABLE_STRING
, global_info
.current_dns_ex
?: global_info
.current_dns
);
1923 return table_log_add_error(r
);
1926 r
= dump_list(table
, "DNS Servers:", global_info
.dns_ex
?: global_info
.dns
);
1930 r
= dump_list(table
, "Fallback DNS Servers:", global_info
.fallback_dns_ex
?: global_info
.fallback_dns
);
1934 r
= dump_list(table
, "DNS Domain:", global_info
.domains
);
1938 r
= table_print(table
, NULL
);
1940 return table_log_print_error(r
);
1947 static int status_all(sd_bus
*bus
, StatusMode mode
) {
1948 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1949 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1950 bool empty_line
= false;
1955 r
= status_global(bus
, mode
, &empty_line
);
1959 r
= sd_netlink_open(&rtnl
);
1961 return log_error_errno(r
, "Failed to connect to netlink: %m");
1963 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
1965 return rtnl_log_create_error(r
);
1967 r
= sd_netlink_message_request_dump(req
, true);
1969 return rtnl_log_create_error(r
);
1971 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
1973 return log_error_errno(r
, "Failed to enumerate links: %m");
1975 _cleanup_free_ InterfaceInfo
*infos
= NULL
;
1978 for (sd_netlink_message
*i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
1983 r
= sd_netlink_message_get_type(i
, &type
);
1985 return rtnl_log_parse_error(r
);
1987 if (type
!= RTM_NEWLINK
)
1990 r
= sd_rtnl_message_link_get_ifindex(i
, &ifindex
);
1992 return rtnl_log_parse_error(r
);
1994 if (ifindex
== LOOPBACK_IFINDEX
)
1997 r
= sd_netlink_message_read_string(i
, IFLA_IFNAME
, &name
);
1999 return rtnl_log_parse_error(r
);
2001 if (!GREEDY_REALLOC(infos
, n_infos
+ 1))
2004 infos
[n_infos
++] = (InterfaceInfo
) { ifindex
, name
};
2007 typesafe_qsort(infos
, n_infos
, interface_info_compare
);
2010 for (size_t i
= 0; i
< n_infos
; i
++) {
2011 int q
= status_ifindex(bus
, infos
[i
].index
, infos
[i
].name
, mode
, &empty_line
);
2012 if (q
< 0 && r
>= 0)
2019 static int verb_status(int argc
, char **argv
, void *userdata
) {
2020 sd_bus
*bus
= userdata
;
2021 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2025 bool empty_line
= false;
2027 STRV_FOREACH(ifname
, argv
+ 1) {
2030 ifindex
= rtnl_resolve_interface(&rtnl
, *ifname
);
2032 log_warning_errno(ifindex
, "Failed to resolve interface \"%s\", ignoring: %m", *ifname
);
2036 q
= status_ifindex(bus
, ifindex
, NULL
, STATUS_ALL
, &empty_line
);
2041 r
= status_all(bus
, STATUS_ALL
);
2046 static int call_dns(sd_bus
*bus
, char **dns
, const BusLocator
*locator
, sd_bus_error
*error
, bool extended
) {
2047 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
2050 r
= bus_message_new_method_call(bus
, &req
, locator
, extended
? "SetLinkDNSEx" : "SetLinkDNS");
2052 return bus_log_create_error(r
);
2054 r
= sd_bus_message_append(req
, "i", arg_ifindex
);
2056 return bus_log_create_error(r
);
2058 r
= sd_bus_message_open_container(req
, 'a', extended
? "(iayqs)" : "(iay)");
2060 return bus_log_create_error(r
);
2062 /* If only argument is the empty string, then call SetLinkDNS() with an
2063 * empty list, which will clear the list of domains for an interface. */
2064 if (!strv_equal(dns
, STRV_MAKE("")))
2065 STRV_FOREACH(p
, dns
) {
2066 _cleanup_free_
char *name
= NULL
;
2067 struct in_addr_data data
;
2071 r
= in_addr_port_ifindex_name_from_string_auto(*p
, &data
.family
, &data
.address
, &port
, &ifindex
, &name
);
2073 return log_error_errno(r
, "Failed to parse DNS server address: %s", *p
);
2075 if (ifindex
!= 0 && ifindex
!= arg_ifindex
)
2076 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid ifindex: %i", ifindex
);
2078 r
= sd_bus_message_open_container(req
, 'r', extended
? "iayqs" : "iay");
2080 return bus_log_create_error(r
);
2082 r
= sd_bus_message_append(req
, "i", data
.family
);
2084 return bus_log_create_error(r
);
2086 r
= sd_bus_message_append_array(req
, 'y', &data
.address
, FAMILY_ADDRESS_SIZE(data
.family
));
2088 return bus_log_create_error(r
);
2091 r
= sd_bus_message_append(req
, "q", port
);
2093 return bus_log_create_error(r
);
2095 r
= sd_bus_message_append(req
, "s", name
);
2097 return bus_log_create_error(r
);
2100 r
= sd_bus_message_close_container(req
);
2102 return bus_log_create_error(r
);
2105 r
= sd_bus_message_close_container(req
);
2107 return bus_log_create_error(r
);
2109 r
= sd_bus_call(bus
, req
, 0, error
, NULL
);
2110 if (r
< 0 && extended
&& sd_bus_error_has_name(error
, SD_BUS_ERROR_UNKNOWN_METHOD
)) {
2111 sd_bus_error_free(error
);
2112 return call_dns(bus
, dns
, locator
, error
, false);
2117 static int verb_dns(int argc
, char **argv
, void *userdata
) {
2118 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2119 sd_bus
*bus
= userdata
;
2125 r
= ifname_mangle(argv
[1]);
2130 if (arg_ifindex
<= 0)
2131 return status_all(bus
, STATUS_DNS
);
2134 return status_ifindex(bus
, arg_ifindex
, NULL
, STATUS_DNS
, NULL
);
2136 r
= call_dns(bus
, argv
+ 2, bus_resolve_mgr
, &error
, true);
2137 if (r
< 0 && sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
)) {
2138 sd_bus_error_free(&error
);
2140 r
= call_dns(bus
, argv
+ 2, bus_network_mgr
, &error
, true);
2143 if (arg_ifindex_permissive
&&
2144 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
2147 return log_error_errno(r
, "Failed to set DNS configuration: %s", bus_error_message(&error
, r
));
2153 static int call_domain(sd_bus
*bus
, char **domain
, const BusLocator
*locator
, sd_bus_error
*error
) {
2154 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
2157 r
= bus_message_new_method_call(bus
, &req
, locator
, "SetLinkDomains");
2159 return bus_log_create_error(r
);
2161 r
= sd_bus_message_append(req
, "i", arg_ifindex
);
2163 return bus_log_create_error(r
);
2165 r
= sd_bus_message_open_container(req
, 'a', "(sb)");
2167 return bus_log_create_error(r
);
2169 /* If only argument is the empty string, then call SetLinkDomains() with an
2170 * empty list, which will clear the list of domains for an interface. */
2171 if (!strv_equal(domain
, STRV_MAKE("")))
2172 STRV_FOREACH(p
, domain
) {
2175 n
= **p
== '~' ? *p
+ 1 : *p
;
2177 r
= dns_name_is_valid(n
);
2179 return log_error_errno(r
, "Failed to validate specified domain %s: %m", n
);
2181 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2182 "Domain not valid: %s",
2185 r
= sd_bus_message_append(req
, "(sb)", n
, **p
== '~');
2187 return bus_log_create_error(r
);
2190 r
= sd_bus_message_close_container(req
);
2192 return bus_log_create_error(r
);
2194 return sd_bus_call(bus
, req
, 0, error
, NULL
);
2197 static int verb_domain(int argc
, char **argv
, void *userdata
) {
2198 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2199 sd_bus
*bus
= userdata
;
2205 r
= ifname_mangle(argv
[1]);
2210 if (arg_ifindex
<= 0)
2211 return status_all(bus
, STATUS_DOMAIN
);
2214 return status_ifindex(bus
, arg_ifindex
, NULL
, STATUS_DOMAIN
, NULL
);
2216 r
= call_domain(bus
, argv
+ 2, bus_resolve_mgr
, &error
);
2217 if (r
< 0 && sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
)) {
2218 sd_bus_error_free(&error
);
2220 r
= call_domain(bus
, argv
+ 2, bus_network_mgr
, &error
);
2223 if (arg_ifindex_permissive
&&
2224 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
2227 return log_error_errno(r
, "Failed to set domain configuration: %s", bus_error_message(&error
, r
));
2233 static int verb_default_route(int argc
, char **argv
, void *userdata
) {
2234 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2235 sd_bus
*bus
= userdata
;
2241 r
= ifname_mangle(argv
[1]);
2246 if (arg_ifindex
<= 0)
2247 return status_all(bus
, STATUS_DEFAULT_ROUTE
);
2250 return status_ifindex(bus
, arg_ifindex
, NULL
, STATUS_DEFAULT_ROUTE
, NULL
);
2252 b
= parse_boolean(argv
[2]);
2254 return log_error_errno(b
, "Failed to parse boolean argument: %s", argv
[2]);
2256 r
= bus_call_method(bus
, bus_resolve_mgr
, "SetLinkDefaultRoute", &error
, NULL
, "ib", arg_ifindex
, b
);
2257 if (r
< 0 && sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
)) {
2258 sd_bus_error_free(&error
);
2260 r
= bus_call_method(bus
, bus_network_mgr
, "SetLinkDefaultRoute", &error
, NULL
, "ib", arg_ifindex
, b
);
2263 if (arg_ifindex_permissive
&&
2264 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
2267 return log_error_errno(r
, "Failed to set default route configuration: %s", bus_error_message(&error
, r
));
2273 static int verb_llmnr(int argc
, char **argv
, void *userdata
) {
2274 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2275 sd_bus
*bus
= userdata
;
2281 r
= ifname_mangle(argv
[1]);
2286 if (arg_ifindex
<= 0)
2287 return status_all(bus
, STATUS_LLMNR
);
2290 return status_ifindex(bus
, arg_ifindex
, NULL
, STATUS_LLMNR
, NULL
);
2292 r
= bus_call_method(bus
, bus_resolve_mgr
, "SetLinkLLMNR", &error
, NULL
, "is", arg_ifindex
, argv
[2]);
2293 if (r
< 0 && sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
)) {
2294 sd_bus_error_free(&error
);
2296 r
= bus_call_method(bus
, bus_network_mgr
, "SetLinkLLMNR", &error
, NULL
, "is", arg_ifindex
, argv
[2]);
2299 if (arg_ifindex_permissive
&&
2300 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
2303 return log_error_errno(r
, "Failed to set LLMNR configuration: %s", bus_error_message(&error
, r
));
2309 static int verb_mdns(int argc
, char **argv
, void *userdata
) {
2310 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2311 sd_bus
*bus
= userdata
;
2317 r
= ifname_mangle(argv
[1]);
2322 if (arg_ifindex
<= 0)
2323 return status_all(bus
, STATUS_MDNS
);
2326 return status_ifindex(bus
, arg_ifindex
, NULL
, STATUS_MDNS
, NULL
);
2328 r
= bus_call_method(bus
, bus_resolve_mgr
, "SetLinkMulticastDNS", &error
, NULL
, "is", arg_ifindex
, argv
[2]);
2329 if (r
< 0 && sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
)) {
2330 sd_bus_error_free(&error
);
2332 r
= bus_call_method(
2335 "SetLinkMulticastDNS",
2338 "is", arg_ifindex
, argv
[2]);
2341 if (arg_ifindex_permissive
&&
2342 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
2345 return log_error_errno(r
, "Failed to set MulticastDNS configuration: %s", bus_error_message(&error
, r
));
2351 static int verb_dns_over_tls(int argc
, char **argv
, void *userdata
) {
2352 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2353 sd_bus
*bus
= userdata
;
2359 r
= ifname_mangle(argv
[1]);
2364 if (arg_ifindex
<= 0)
2365 return status_all(bus
, STATUS_PRIVATE
);
2368 return status_ifindex(bus
, arg_ifindex
, NULL
, STATUS_PRIVATE
, NULL
);
2370 r
= bus_call_method(bus
, bus_resolve_mgr
, "SetLinkDNSOverTLS", &error
, NULL
, "is", arg_ifindex
, argv
[2]);
2371 if (r
< 0 && sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
)) {
2372 sd_bus_error_free(&error
);
2374 r
= bus_call_method(
2377 "SetLinkDNSOverTLS",
2380 "is", arg_ifindex
, argv
[2]);
2383 if (arg_ifindex_permissive
&&
2384 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
2387 return log_error_errno(r
, "Failed to set DNSOverTLS configuration: %s", bus_error_message(&error
, r
));
2393 static int verb_dnssec(int argc
, char **argv
, void *userdata
) {
2394 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2395 sd_bus
*bus
= userdata
;
2401 r
= ifname_mangle(argv
[1]);
2406 if (arg_ifindex
<= 0)
2407 return status_all(bus
, STATUS_DNSSEC
);
2410 return status_ifindex(bus
, arg_ifindex
, NULL
, STATUS_DNSSEC
, NULL
);
2412 r
= bus_call_method(bus
, bus_resolve_mgr
, "SetLinkDNSSEC", &error
, NULL
, "is", arg_ifindex
, argv
[2]);
2413 if (r
< 0 && sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
)) {
2414 sd_bus_error_free(&error
);
2416 r
= bus_call_method(bus
, bus_network_mgr
, "SetLinkDNSSEC", &error
, NULL
, "is", arg_ifindex
, argv
[2]);
2419 if (arg_ifindex_permissive
&&
2420 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
2423 return log_error_errno(r
, "Failed to set DNSSEC configuration: %s", bus_error_message(&error
, r
));
2429 static int call_nta(sd_bus
*bus
, char **nta
, const BusLocator
*locator
, sd_bus_error
*error
) {
2430 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
;
2433 r
= bus_message_new_method_call(bus
, &req
, locator
, "SetLinkDNSSECNegativeTrustAnchors");
2435 return bus_log_create_error(r
);
2437 r
= sd_bus_message_append(req
, "i", arg_ifindex
);
2439 return bus_log_create_error(r
);
2441 r
= sd_bus_message_append_strv(req
, nta
);
2443 return bus_log_create_error(r
);
2445 return sd_bus_call(bus
, req
, 0, error
, NULL
);
2448 static int verb_nta(int argc
, char **argv
, void *userdata
) {
2449 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2450 sd_bus
*bus
= userdata
;
2457 r
= ifname_mangle(argv
[1]);
2462 if (arg_ifindex
<= 0)
2463 return status_all(bus
, STATUS_NTA
);
2466 return status_ifindex(bus
, arg_ifindex
, NULL
, STATUS_NTA
, NULL
);
2468 /* If only argument is the empty string, then call SetLinkDNSSECNegativeTrustAnchors()
2469 * with an empty list, which will clear the list of domains for an interface. */
2470 clear
= strv_equal(argv
+ 2, STRV_MAKE(""));
2473 STRV_FOREACH(p
, argv
+ 2) {
2474 r
= dns_name_is_valid(*p
);
2476 return log_error_errno(r
, "Failed to validate specified domain %s: %m", *p
);
2478 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2479 "Domain not valid: %s",
2483 r
= call_nta(bus
, clear
? NULL
: argv
+ 2, bus_resolve_mgr
, &error
);
2484 if (r
< 0 && sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
)) {
2485 sd_bus_error_free(&error
);
2487 r
= call_nta(bus
, clear
? NULL
: argv
+ 2, bus_network_mgr
, &error
);
2490 if (arg_ifindex_permissive
&&
2491 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
2494 return log_error_errno(r
, "Failed to set DNSSEC NTA configuration: %s", bus_error_message(&error
, r
));
2500 static int verb_revert_link(int argc
, char **argv
, void *userdata
) {
2501 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2502 sd_bus
*bus
= userdata
;
2508 r
= ifname_mangle(argv
[1]);
2513 if (arg_ifindex
<= 0)
2514 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Interface argument required.");
2516 r
= bus_call_method(bus
, bus_resolve_mgr
, "RevertLink", &error
, NULL
, "i", arg_ifindex
);
2517 if (r
< 0 && sd_bus_error_has_name(&error
, BUS_ERROR_LINK_BUSY
)) {
2518 sd_bus_error_free(&error
);
2520 r
= bus_call_method(bus
, bus_network_mgr
, "RevertLinkDNS", &error
, NULL
, "i", arg_ifindex
);
2523 if (arg_ifindex_permissive
&&
2524 sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_LINK
))
2527 return log_error_errno(r
, "Failed to revert interface configuration: %s", bus_error_message(&error
, r
));
2533 static int verb_log_level(int argc
, char *argv
[], void *userdata
) {
2534 sd_bus
*bus
= userdata
;
2537 assert(IN_SET(argc
, 1, 2));
2539 return verb_log_control_common(bus
, "org.freedesktop.resolve1", argv
[0], argc
== 2 ? argv
[1] : NULL
);
2542 static void help_protocol_types(void) {
2544 puts("Known protocol types:");
2545 puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6\nmdns\nmdns-ipv4\nmdns-ipv6");
2548 static void help_dns_types(void) {
2550 puts("Known DNS RR types:");
2552 DUMP_STRING_TABLE(dns_type
, int, _DNS_TYPE_MAX
);
2555 static void help_dns_classes(void) {
2557 puts("Known DNS RR classes:");
2559 DUMP_STRING_TABLE(dns_class
, int, _DNS_CLASS_MAX
);
2562 static int compat_help(void) {
2563 _cleanup_free_
char *link
= NULL
;
2566 r
= terminal_urlify_man("resolvectl", "1", &link
);
2570 printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n"
2571 "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n"
2572 "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n"
2573 "%1$s [OPTIONS...] --statistics\n"
2574 "%1$s [OPTIONS...] --reset-statistics\n"
2576 "%2$sResolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%3$s\n\n"
2577 " -h --help Show this help\n"
2578 " --version Show package version\n"
2579 " --no-pager Do not pipe output into a pager\n"
2580 " -4 Resolve IPv4 addresses\n"
2581 " -6 Resolve IPv6 addresses\n"
2582 " -i --interface=INTERFACE Look on interface\n"
2583 " -p --protocol=PROTO|help Look via protocol\n"
2584 " -t --type=TYPE|help Query RR with DNS type\n"
2585 " -c --class=CLASS|help Query RR with DNS class\n"
2586 " --service Resolve service (SRV)\n"
2587 " --service-address=BOOL Resolve address for services (default: yes)\n"
2588 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
2589 " --openpgp Query OpenPGP public key\n"
2590 " --tlsa Query TLS public key\n"
2591 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
2592 " --search=BOOL Use search domains for single-label names\n"
2594 " --raw[=payload|packet] Dump the answer as binary data\n"
2595 " --legend=BOOL Print headers and additional info (default: yes)\n"
2596 " --statistics Show resolver statistics\n"
2597 " --reset-statistics Reset resolver statistics\n"
2598 " --status Show link and server status\n"
2599 " --flush-caches Flush all local DNS caches\n"
2600 " --reset-server-features\n"
2601 " Forget learnt DNS server feature levels\n"
2602 " --set-dns=SERVER Set per-interface DNS server address\n"
2603 " --set-domain=DOMAIN Set per-interface search domain\n"
2604 " --set-llmnr=MODE Set per-interface LLMNR mode\n"
2605 " --set-mdns=MODE Set per-interface MulticastDNS mode\n"
2606 " --set-dnsovertls=MODE Set per-interface DNS-over-TLS mode\n"
2607 " --set-dnssec=MODE Set per-interface DNSSEC mode\n"
2608 " --set-nta=DOMAIN Set per-interface DNSSEC NTA\n"
2609 " --revert Revert per-interface configuration\n"
2610 "\nSee the %4$s for details.\n",
2611 program_invocation_short_name
,
2619 static int native_help(void) {
2620 _cleanup_free_
char *link
= NULL
;
2623 r
= terminal_urlify_man("resolvectl", "1", &link
);
2627 printf("%s [OPTIONS...] COMMAND ...\n"
2629 "%sSend control commands to the network name resolution manager, or%s\n"
2630 "%sresolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%s\n"
2632 " query HOSTNAME|ADDRESS... Resolve domain names, IPv4 and IPv6 addresses\n"
2633 " service [[NAME] TYPE] DOMAIN Resolve service (SRV)\n"
2634 " openpgp EMAIL@DOMAIN... Query OpenPGP public key\n"
2635 " tlsa DOMAIN[:PORT]... Query TLS public key\n"
2636 " status [LINK...] Show link and server status\n"
2637 " statistics Show resolver statistics\n"
2638 " reset-statistics Reset resolver statistics\n"
2639 " flush-caches Flush all local DNS caches\n"
2640 " reset-server-features Forget learnt DNS server feature levels\n"
2641 " dns [LINK [SERVER...]] Get/set per-interface DNS server address\n"
2642 " domain [LINK [DOMAIN...]] Get/set per-interface search domain\n"
2643 " default-route [LINK [BOOL]] Get/set per-interface default route flag\n"
2644 " llmnr [LINK [MODE]] Get/set per-interface LLMNR mode\n"
2645 " mdns [LINK [MODE]] Get/set per-interface MulticastDNS mode\n"
2646 " dnsovertls [LINK [MODE]] Get/set per-interface DNS-over-TLS mode\n"
2647 " dnssec [LINK [MODE]] Get/set per-interface DNSSEC mode\n"
2648 " nta [LINK [DOMAIN...]] Get/set per-interface DNSSEC NTA\n"
2649 " revert LINK Revert per-interface configuration\n"
2650 " log-level [LEVEL] Get/set logging threshold for systemd-resolved\n"
2652 " -h --help Show this help\n"
2653 " --version Show package version\n"
2654 " --no-pager Do not pipe output into a pager\n"
2655 " -4 Resolve IPv4 addresses\n"
2656 " -6 Resolve IPv6 addresses\n"
2657 " -i --interface=INTERFACE Look on interface\n"
2658 " -p --protocol=PROTO|help Look via protocol\n"
2659 " -t --type=TYPE|help Query RR with DNS type\n"
2660 " -c --class=CLASS|help Query RR with DNS class\n"
2661 " --service-address=BOOL Resolve address for services (default: yes)\n"
2662 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
2663 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
2664 " --validate=BOOL Allow DNSSEC validation (default: yes)\n"
2665 " --synthesize=BOOL Allow synthetic response (default: yes)\n"
2666 " --cache=BOOL Allow response from cache (default: yes)\n"
2667 " --zone=BOOL Allow response from locally registered mDNS/LLMNR\n"
2668 " records (default: yes)\n"
2669 " --trust-anchor=BOOL Allow response from local trust anchor (default: yes)\n"
2670 " --network=BOOL Allow response from network (default: yes)\n"
2671 " --search=BOOL Use search domains for single-label names (default: yes)\n"
2672 " --raw[=payload|packet] Dump the answer as binary data\n"
2673 " --legend=BOOL Print headers and additional info (default: yes)\n"
2674 "\nSee the %s for details.\n",
2675 program_invocation_short_name
,
2685 static int verb_help(int argc
, char **argv
, void *userdata
) {
2686 return native_help();
2689 static int compat_parse_argv(int argc
, char *argv
[]) {
2691 ARG_VERSION
= 0x100,
2695 ARG_SERVICE_ADDRESS
,
2702 ARG_RESET_STATISTICS
,
2705 ARG_RESET_SERVER_FEATURES
,
2717 static const struct option options
[] = {
2718 { "help", no_argument
, NULL
, 'h' },
2719 { "version", no_argument
, NULL
, ARG_VERSION
},
2720 { "type", required_argument
, NULL
, 't' },
2721 { "class", required_argument
, NULL
, 'c' },
2722 { "legend", required_argument
, NULL
, ARG_LEGEND
},
2723 { "interface", required_argument
, NULL
, 'i' },
2724 { "protocol", required_argument
, NULL
, 'p' },
2725 { "cname", required_argument
, NULL
, ARG_CNAME
},
2726 { "service", no_argument
, NULL
, ARG_SERVICE
},
2727 { "service-address", required_argument
, NULL
, ARG_SERVICE_ADDRESS
},
2728 { "service-txt", required_argument
, NULL
, ARG_SERVICE_TXT
},
2729 { "openpgp", no_argument
, NULL
, ARG_OPENPGP
},
2730 { "tlsa", optional_argument
, NULL
, ARG_TLSA
},
2731 { "raw", optional_argument
, NULL
, ARG_RAW
},
2732 { "search", required_argument
, NULL
, ARG_SEARCH
},
2733 { "statistics", no_argument
, NULL
, ARG_STATISTICS
, },
2734 { "reset-statistics", no_argument
, NULL
, ARG_RESET_STATISTICS
},
2735 { "status", no_argument
, NULL
, ARG_STATUS
},
2736 { "flush-caches", no_argument
, NULL
, ARG_FLUSH_CACHES
},
2737 { "reset-server-features", no_argument
, NULL
, ARG_RESET_SERVER_FEATURES
},
2738 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2739 { "set-dns", required_argument
, NULL
, ARG_SET_DNS
},
2740 { "set-domain", required_argument
, NULL
, ARG_SET_DOMAIN
},
2741 { "set-llmnr", required_argument
, NULL
, ARG_SET_LLMNR
},
2742 { "set-mdns", required_argument
, NULL
, ARG_SET_MDNS
},
2743 { "set-dnsovertls", required_argument
, NULL
, ARG_SET_PRIVATE
},
2744 { "set-dnssec", required_argument
, NULL
, ARG_SET_DNSSEC
},
2745 { "set-nta", required_argument
, NULL
, ARG_SET_NTA
},
2746 { "revert", no_argument
, NULL
, ARG_REVERT_LINK
},
2755 while ((c
= getopt_long(argc
, argv
, "h46i:t:c:p:", options
, NULL
)) >= 0)
2759 return compat_help();
2765 arg_family
= AF_INET
;
2769 arg_family
= AF_INET6
;
2773 r
= ifname_mangle(optarg
);
2779 if (streq(optarg
, "help")) {
2784 r
= dns_type_from_string(optarg
);
2786 return log_error_errno(r
, "Failed to parse RR record type %s: %m", optarg
);
2788 arg_type
= (uint16_t) r
;
2789 assert((int) arg_type
== r
);
2791 arg_mode
= MODE_RESOLVE_RECORD
;
2795 if (streq(optarg
, "help")) {
2800 r
= dns_class_from_string(optarg
);
2802 return log_error_errno(r
, "Failed to parse RR record class %s: %m", optarg
);
2804 arg_class
= (uint16_t) r
;
2805 assert((int) arg_class
== r
);
2810 r
= parse_boolean_argument("--legend=", optarg
, &arg_legend
);
2816 if (streq(optarg
, "help")) {
2817 help_protocol_types();
2819 } else if (streq(optarg
, "dns"))
2820 arg_flags
|= SD_RESOLVED_DNS
;
2821 else if (streq(optarg
, "llmnr"))
2822 arg_flags
|= SD_RESOLVED_LLMNR
;
2823 else if (streq(optarg
, "llmnr-ipv4"))
2824 arg_flags
|= SD_RESOLVED_LLMNR_IPV4
;
2825 else if (streq(optarg
, "llmnr-ipv6"))
2826 arg_flags
|= SD_RESOLVED_LLMNR_IPV6
;
2827 else if (streq(optarg
, "mdns"))
2828 arg_flags
|= SD_RESOLVED_MDNS
;
2829 else if (streq(optarg
, "mdns-ipv4"))
2830 arg_flags
|= SD_RESOLVED_MDNS_IPV4
;
2831 else if (streq(optarg
, "mdns-ipv6"))
2832 arg_flags
|= SD_RESOLVED_MDNS_IPV6
;
2834 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2835 "Unknown protocol specifier: %s", optarg
);
2840 arg_mode
= MODE_RESOLVE_SERVICE
;
2844 arg_mode
= MODE_RESOLVE_OPENPGP
;
2848 arg_mode
= MODE_RESOLVE_TLSA
;
2849 if (!optarg
|| service_family_is_valid(optarg
))
2850 arg_service_family
= optarg
;
2852 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2853 "Unknown service family \"%s\".", optarg
);
2858 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
),
2859 "Refusing to write binary data to tty.");
2861 if (optarg
== NULL
|| streq(optarg
, "payload"))
2862 arg_raw
= RAW_PAYLOAD
;
2863 else if (streq(optarg
, "packet"))
2864 arg_raw
= RAW_PACKET
;
2866 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2867 "Unknown --raw specifier \"%s\".",
2874 r
= parse_boolean_argument("--cname=", optarg
, NULL
);
2877 SET_FLAG(arg_flags
, SD_RESOLVED_NO_CNAME
, r
== 0);
2880 case ARG_SERVICE_ADDRESS
:
2881 r
= parse_boolean_argument("--service-address=", optarg
, NULL
);
2884 SET_FLAG(arg_flags
, SD_RESOLVED_NO_ADDRESS
, r
== 0);
2887 case ARG_SERVICE_TXT
:
2888 r
= parse_boolean_argument("--service-txt=", optarg
, NULL
);
2891 SET_FLAG(arg_flags
, SD_RESOLVED_NO_TXT
, r
== 0);
2895 r
= parse_boolean_argument("--search=", optarg
, NULL
);
2898 SET_FLAG(arg_flags
, SD_RESOLVED_NO_SEARCH
, r
== 0);
2901 case ARG_STATISTICS
:
2902 arg_mode
= MODE_STATISTICS
;
2905 case ARG_RESET_STATISTICS
:
2906 arg_mode
= MODE_RESET_STATISTICS
;
2909 case ARG_FLUSH_CACHES
:
2910 arg_mode
= MODE_FLUSH_CACHES
;
2913 case ARG_RESET_SERVER_FEATURES
:
2914 arg_mode
= MODE_RESET_SERVER_FEATURES
;
2918 arg_mode
= MODE_STATUS
;
2922 arg_pager_flags
|= PAGER_DISABLE
;
2926 r
= strv_extend(&arg_set_dns
, optarg
);
2930 arg_mode
= MODE_SET_LINK
;
2933 case ARG_SET_DOMAIN
:
2934 r
= strv_extend(&arg_set_domain
, optarg
);
2938 arg_mode
= MODE_SET_LINK
;
2942 arg_set_llmnr
= optarg
;
2943 arg_mode
= MODE_SET_LINK
;
2947 arg_set_mdns
= optarg
;
2948 arg_mode
= MODE_SET_LINK
;
2951 case ARG_SET_PRIVATE
:
2952 arg_set_dns_over_tls
= optarg
;
2953 arg_mode
= MODE_SET_LINK
;
2956 case ARG_SET_DNSSEC
:
2957 arg_set_dnssec
= optarg
;
2958 arg_mode
= MODE_SET_LINK
;
2962 r
= strv_extend(&arg_set_nta
, optarg
);
2966 arg_mode
= MODE_SET_LINK
;
2969 case ARG_REVERT_LINK
:
2970 arg_mode
= MODE_REVERT_LINK
;
2977 assert_not_reached();
2980 if (arg_type
== 0 && arg_class
!= 0)
2981 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2982 "--class= may only be used in conjunction with --type=.");
2984 if (arg_type
!= 0 && arg_mode
== MODE_RESOLVE_SERVICE
)
2985 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2986 "--service and --type= may not be combined.");
2988 if (arg_type
!= 0 && arg_class
== 0)
2989 arg_class
= DNS_CLASS_IN
;
2991 if (arg_class
!= 0 && arg_type
== 0)
2992 arg_type
= DNS_TYPE_A
;
2994 if (IN_SET(arg_mode
, MODE_SET_LINK
, MODE_REVERT_LINK
)) {
2996 if (arg_ifindex
<= 0)
2997 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2998 "--set-dns=, --set-domain=, --set-llmnr=, --set-mdns=, --set-dnsovertls=, --set-dnssec=, --set-nta= and --revert require --interface=.");
3001 return 1 /* work to do */;
3004 static int native_parse_argv(int argc
, char *argv
[]) {
3006 ARG_VERSION
= 0x100,
3015 ARG_SERVICE_ADDRESS
,
3022 static const struct option options
[] = {
3023 { "help", no_argument
, NULL
, 'h' },
3024 { "version", no_argument
, NULL
, ARG_VERSION
},
3025 { "type", required_argument
, NULL
, 't' },
3026 { "class", required_argument
, NULL
, 'c' },
3027 { "legend", required_argument
, NULL
, ARG_LEGEND
},
3028 { "interface", required_argument
, NULL
, 'i' },
3029 { "protocol", required_argument
, NULL
, 'p' },
3030 { "cname", required_argument
, NULL
, ARG_CNAME
},
3031 { "validate", required_argument
, NULL
, ARG_VALIDATE
},
3032 { "synthesize", required_argument
, NULL
, ARG_SYNTHESIZE
},
3033 { "cache", required_argument
, NULL
, ARG_CACHE
},
3034 { "zone", required_argument
, NULL
, ARG_ZONE
},
3035 { "trust-anchor", required_argument
, NULL
, ARG_TRUST_ANCHOR
},
3036 { "network", required_argument
, NULL
, ARG_NETWORK
},
3037 { "service-address", required_argument
, NULL
, ARG_SERVICE_ADDRESS
},
3038 { "service-txt", required_argument
, NULL
, ARG_SERVICE_TXT
},
3039 { "raw", optional_argument
, NULL
, ARG_RAW
},
3040 { "search", required_argument
, NULL
, ARG_SEARCH
},
3041 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
3050 while ((c
= getopt_long(argc
, argv
, "h46i:t:c:p:", options
, NULL
)) >= 0)
3054 return native_help();
3060 arg_family
= AF_INET
;
3064 arg_family
= AF_INET6
;
3068 r
= ifname_mangle(optarg
);
3074 if (streq(optarg
, "help")) {
3079 r
= dns_type_from_string(optarg
);
3081 return log_error_errno(r
, "Failed to parse RR record type %s: %m", optarg
);
3083 arg_type
= (uint16_t) r
;
3084 assert((int) arg_type
== r
);
3089 if (streq(optarg
, "help")) {
3094 r
= dns_class_from_string(optarg
);
3096 return log_error_errno(r
, "Failed to parse RR record class %s: %m", optarg
);
3098 arg_class
= (uint16_t) r
;
3099 assert((int) arg_class
== r
);
3104 r
= parse_boolean_argument("--legend=", optarg
, &arg_legend
);
3110 if (streq(optarg
, "help")) {
3111 help_protocol_types();
3113 } else if (streq(optarg
, "dns"))
3114 arg_flags
|= SD_RESOLVED_DNS
;
3115 else if (streq(optarg
, "llmnr"))
3116 arg_flags
|= SD_RESOLVED_LLMNR
;
3117 else if (streq(optarg
, "llmnr-ipv4"))
3118 arg_flags
|= SD_RESOLVED_LLMNR_IPV4
;
3119 else if (streq(optarg
, "llmnr-ipv6"))
3120 arg_flags
|= SD_RESOLVED_LLMNR_IPV6
;
3121 else if (streq(optarg
, "mdns"))
3122 arg_flags
|= SD_RESOLVED_MDNS
;
3123 else if (streq(optarg
, "mdns-ipv4"))
3124 arg_flags
|= SD_RESOLVED_MDNS_IPV4
;
3125 else if (streq(optarg
, "mdns-ipv6"))
3126 arg_flags
|= SD_RESOLVED_MDNS_IPV6
;
3128 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3129 "Unknown protocol specifier: %s",
3136 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
),
3137 "Refusing to write binary data to tty.");
3139 if (optarg
== NULL
|| streq(optarg
, "payload"))
3140 arg_raw
= RAW_PAYLOAD
;
3141 else if (streq(optarg
, "packet"))
3142 arg_raw
= RAW_PACKET
;
3144 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3145 "Unknown --raw specifier \"%s\".",
3152 r
= parse_boolean_argument("--cname=", optarg
, NULL
);
3155 SET_FLAG(arg_flags
, SD_RESOLVED_NO_CNAME
, r
== 0);
3159 r
= parse_boolean_argument("--validate=", optarg
, NULL
);
3162 SET_FLAG(arg_flags
, SD_RESOLVED_NO_VALIDATE
, r
== 0);
3165 case ARG_SYNTHESIZE
:
3166 r
= parse_boolean_argument("--synthesize=", optarg
, NULL
);
3169 SET_FLAG(arg_flags
, SD_RESOLVED_NO_SYNTHESIZE
, r
== 0);
3173 r
= parse_boolean_argument("--cache=", optarg
, NULL
);
3176 SET_FLAG(arg_flags
, SD_RESOLVED_NO_CACHE
, r
== 0);
3180 r
= parse_boolean_argument("--zone=", optarg
, NULL
);
3183 SET_FLAG(arg_flags
, SD_RESOLVED_NO_ZONE
, r
== 0);
3186 case ARG_TRUST_ANCHOR
:
3187 r
= parse_boolean_argument("--trust-anchor=", optarg
, NULL
);
3190 SET_FLAG(arg_flags
, SD_RESOLVED_NO_TRUST_ANCHOR
, r
== 0);
3194 r
= parse_boolean_argument("--network=", optarg
, NULL
);
3197 SET_FLAG(arg_flags
, SD_RESOLVED_NO_NETWORK
, r
== 0);
3200 case ARG_SERVICE_ADDRESS
:
3201 r
= parse_boolean_argument("--service-address=", optarg
, NULL
);
3204 SET_FLAG(arg_flags
, SD_RESOLVED_NO_ADDRESS
, r
== 0);
3207 case ARG_SERVICE_TXT
:
3208 r
= parse_boolean_argument("--service-txt=", optarg
, NULL
);
3211 SET_FLAG(arg_flags
, SD_RESOLVED_NO_TXT
, r
== 0);
3215 r
= parse_boolean_argument("--search=", optarg
, NULL
);
3218 SET_FLAG(arg_flags
, SD_RESOLVED_NO_SEARCH
, r
== 0);
3222 arg_pager_flags
|= PAGER_DISABLE
;
3229 assert_not_reached();
3232 if (arg_type
== 0 && arg_class
!= 0)
3233 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3234 "--class= may only be used in conjunction with --type=.");
3236 if (arg_type
!= 0 && arg_class
== 0)
3237 arg_class
= DNS_CLASS_IN
;
3239 if (arg_class
!= 0 && arg_type
== 0)
3240 arg_type
= DNS_TYPE_A
;
3242 return 1 /* work to do */;
3245 static int native_main(int argc
, char *argv
[], sd_bus
*bus
) {
3247 static const Verb verbs
[] = {
3248 { "help", VERB_ANY
, VERB_ANY
, 0, verb_help
},
3249 { "status", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, verb_status
},
3250 { "query", 2, VERB_ANY
, 0, verb_query
},
3251 { "service", 2, 4, 0, verb_service
},
3252 { "openpgp", 2, VERB_ANY
, 0, verb_openpgp
},
3253 { "tlsa", 2, VERB_ANY
, 0, verb_tlsa
},
3254 { "statistics", VERB_ANY
, 1, 0, show_statistics
},
3255 { "reset-statistics", VERB_ANY
, 1, 0, reset_statistics
},
3256 { "flush-caches", VERB_ANY
, 1, 0, flush_caches
},
3257 { "reset-server-features", VERB_ANY
, 1, 0, reset_server_features
},
3258 { "dns", VERB_ANY
, VERB_ANY
, 0, verb_dns
},
3259 { "domain", VERB_ANY
, VERB_ANY
, 0, verb_domain
},
3260 { "default-route", VERB_ANY
, 3, 0, verb_default_route
},
3261 { "llmnr", VERB_ANY
, 3, 0, verb_llmnr
},
3262 { "mdns", VERB_ANY
, 3, 0, verb_mdns
},
3263 { "dnsovertls", VERB_ANY
, 3, 0, verb_dns_over_tls
},
3264 { "dnssec", VERB_ANY
, 3, 0, verb_dnssec
},
3265 { "nta", VERB_ANY
, VERB_ANY
, 0, verb_nta
},
3266 { "revert", VERB_ANY
, 2, 0, verb_revert_link
},
3267 { "log-level", VERB_ANY
, 2, 0, verb_log_level
},
3271 return dispatch_verb(argc
, argv
, verbs
, bus
);
3274 static int translate(const char *verb
, const char *single_arg
, size_t num_args
, char **args
, sd_bus
*bus
) {
3279 assert(num_args
== 0 || args
);
3281 num
= !!single_arg
+ num_args
+ 1;
3283 p
= fake
= newa0(char *, num
+ 1);
3284 *p
++ = (char *) verb
;
3286 *p
++ = (char *) single_arg
;
3287 for (size_t i
= 0; i
< num_args
; i
++)
3291 return native_main((int) num
, fake
, bus
);
3294 static int compat_main(int argc
, char *argv
[], sd_bus
*bus
) {
3298 case MODE_RESOLVE_HOST
:
3299 case MODE_RESOLVE_RECORD
:
3300 return translate("query", NULL
, argc
- optind
, argv
+ optind
, bus
);
3302 case MODE_RESOLVE_SERVICE
:
3303 return translate("service", NULL
, argc
- optind
, argv
+ optind
, bus
);
3305 case MODE_RESOLVE_OPENPGP
:
3306 return translate("openpgp", NULL
, argc
- optind
, argv
+ optind
, bus
);
3308 case MODE_RESOLVE_TLSA
:
3309 return translate("tlsa", arg_service_family
, argc
- optind
, argv
+ optind
, bus
);
3311 case MODE_STATISTICS
:
3312 return translate("statistics", NULL
, 0, NULL
, bus
);
3314 case MODE_RESET_STATISTICS
:
3315 return translate("reset-statistics", NULL
, 0, NULL
, bus
);
3317 case MODE_FLUSH_CACHES
:
3318 return translate("flush-caches", NULL
, 0, NULL
, bus
);
3320 case MODE_RESET_SERVER_FEATURES
:
3321 return translate("reset-server-features", NULL
, 0, NULL
, bus
);
3324 return translate("status", NULL
, argc
- optind
, argv
+ optind
, bus
);
3330 r
= translate("dns", arg_ifname
, strv_length(arg_set_dns
), arg_set_dns
, bus
);
3335 if (arg_set_domain
) {
3336 r
= translate("domain", arg_ifname
, strv_length(arg_set_domain
), arg_set_domain
, bus
);
3342 r
= translate("nta", arg_ifname
, strv_length(arg_set_nta
), arg_set_nta
, bus
);
3347 if (arg_set_llmnr
) {
3348 r
= translate("llmnr", arg_ifname
, 1, (char **) &arg_set_llmnr
, bus
);
3354 r
= translate("mdns", arg_ifname
, 1, (char **) &arg_set_mdns
, bus
);
3359 if (arg_set_dns_over_tls
) {
3360 r
= translate("dnsovertls", arg_ifname
, 1, (char **) &arg_set_dns_over_tls
, bus
);
3365 if (arg_set_dnssec
) {
3366 r
= translate("dnssec", arg_ifname
, 1, (char **) &arg_set_dnssec
, bus
);
3373 case MODE_REVERT_LINK
:
3376 return translate("revert", arg_ifname
, 0, NULL
, bus
);
3379 assert_not_reached();
3385 static int run(int argc
, char **argv
) {
3386 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
3389 setlocale(LC_ALL
, "");
3392 if (invoked_as(argv
, "resolvconf"))
3393 r
= resolvconf_parse_argv(argc
, argv
);
3394 else if (invoked_as(argv
, "systemd-resolve"))
3395 r
= compat_parse_argv(argc
, argv
);
3397 r
= native_parse_argv(argc
, argv
);
3401 r
= sd_bus_open_system(&bus
);
3403 return log_error_errno(r
, "sd_bus_open_system: %m");
3405 if (STR_IN_SET(program_invocation_short_name
, "systemd-resolve", "resolvconf"))
3406 return compat_main(argc
, argv
, bus
);
3408 return native_main(argc
, argv
, bus
);
3411 DEFINE_MAIN_FUNCTION(run
);