]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolvectl.c
varlink,json: introduce new varlink_dispatch() helper
[thirdparty/systemd.git] / src / resolve / resolvectl.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
bdef7319 2
bdef7319 3#include <getopt.h>
ca78ad1d 4#include <locale.h>
cf0fbc49 5#include <net/if.h>
bdef7319
ZJS
6
7#include "sd-bus.h"
be371fe0 8#include "sd-netlink.h"
3f6fd1ba
LP
9
10#include "af-list.h"
b5efdb8a 11#include "alloc-util.h"
d6b4d1c7 12#include "build.h"
14965b94 13#include "bus-common-errors.h"
bdef7319 14#include "bus-error.h"
9b71e4ab 15#include "bus-locator.h"
807542be 16#include "bus-map-properties.h"
a574b7d1 17#include "bus-message-util.h"
14965b94 18#include "dns-domain.h"
fffbf1dc 19#include "errno-list.h"
45ec7efb 20#include "escape.h"
29e15e98 21#include "format-table.h"
518a66ec 22#include "format-util.h"
4ac2ca1b 23#include "gcrypt-util.h"
058946d1 24#include "hostname-util.h"
fffbf1dc 25#include "json.h"
f6aa6190 26#include "main-func.h"
ef118d00 27#include "missing_network.h"
be371fe0 28#include "netlink-util.h"
7e8facb3 29#include "openssl-util.h"
be371fe0 30#include "pager.h"
599c7c54 31#include "parse-argument.h"
6bedfcbb 32#include "parse-util.h"
294bf0c3 33#include "pretty-print.h"
2306d177 34#include "process-util.h"
088c1363 35#include "resolvconf-compat.h"
c38a03df 36#include "resolve-util.h"
c2e84cab 37#include "resolvectl.h"
51323288 38#include "resolved-def.h"
3f6fd1ba 39#include "resolved-dns-packet.h"
058946d1 40#include "resolved-util.h"
5c3fa98d 41#include "socket-netlink.h"
eb107675 42#include "sort-util.h"
957d9df3 43#include "stdio-util.h"
5c828e66 44#include "string-table.h"
be371fe0 45#include "strv.h"
a150ff5e 46#include "terminal-util.h"
7c502303 47#include "utf8.h"
fffbf1dc 48#include "varlink.h"
a87b151a 49#include "verb-log-control.h"
a7a4c60a 50#include "verbs.h"
2d4c5cbc 51
bdef7319 52static int arg_family = AF_UNSPEC;
a661dc36
YW
53static int arg_ifindex = 0;
54static char *arg_ifname = NULL;
4b548ef3 55static uint16_t arg_type = 0;
2d4c5cbc 56static uint16_t arg_class = 0;
b93312f5 57static bool arg_legend = true;
51323288 58static uint64_t arg_flags = 0;
fffbf1dc 59static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
0221d68a 60static PagerFlags arg_pager_flags = 0;
088c1363 61bool arg_ifindex_permissive = false; /* If true, don't generate an error if the specified interface index doesn't exist */
a7a4c60a 62static const char *arg_service_family = NULL;
82d1d240 63
dab48ea6
ZJS
64typedef enum RawType {
65 RAW_NONE,
66 RAW_PAYLOAD,
67 RAW_PACKET,
68} RawType;
dab48ea6 69static RawType arg_raw = RAW_NONE;
a150ff5e 70
088c1363
LP
71ExecutionMode arg_mode = MODE_RESOLVE_HOST;
72
a7a4c60a 73char **arg_set_dns = NULL;
088c1363 74char **arg_set_domain = NULL;
a7a4c60a
YW
75static const char *arg_set_llmnr = NULL;
76static const char *arg_set_mdns = NULL;
c9299be2 77static const char *arg_set_dns_over_tls = NULL;
a7a4c60a 78static const char *arg_set_dnssec = NULL;
14965b94
LP
79static char **arg_set_nta = NULL;
80
f6aa6190
YW
81STATIC_DESTRUCTOR_REGISTER(arg_ifname, freep);
82STATIC_DESTRUCTOR_REGISTER(arg_set_dns, strv_freep);
83STATIC_DESTRUCTOR_REGISTER(arg_set_domain, strv_freep);
84STATIC_DESTRUCTOR_REGISTER(arg_set_nta, strv_freep);
85
a7a4c60a
YW
86typedef enum StatusMode {
87 STATUS_ALL,
88 STATUS_DNS,
89 STATUS_DOMAIN,
f2fd3cdb 90 STATUS_DEFAULT_ROUTE,
a7a4c60a
YW
91 STATUS_LLMNR,
92 STATUS_MDNS,
d050561a 93 STATUS_PRIVATE,
a7a4c60a
YW
94 STATUS_DNSSEC,
95 STATUS_NTA,
96} StatusMode;
97
eb107675
ZJS
98typedef struct InterfaceInfo {
99 int index;
100 const char *name;
101} InterfaceInfo;
102
103static int interface_info_compare(const InterfaceInfo *a, const InterfaceInfo *b) {
104 int r;
105
106 r = CMP(a->index, b->index);
107 if (r != 0)
108 return r;
109
110 return strcmp_ptr(a->name, b->name);
111}
112
7d4e8503
YW
113int ifname_mangle_full(const char *s, bool drop_protocol_specifier) {
114 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
115 _cleanup_strv_free_ char **found = NULL;
116 int r;
a661dc36
YW
117
118 assert(s);
119
7d4e8503
YW
120 if (drop_protocol_specifier) {
121 _cleanup_free_ char *buf = NULL;
122 int ifindex_longest_name = -ENODEV;
a661dc36 123
7d4e8503 124 /* When invoked as resolvconf, drop the protocol specifier(s) at the end. */
8e5385b4 125
7d4e8503
YW
126 buf = strdup(s);
127 if (!buf)
128 return log_oom();
a661dc36 129
7d4e8503
YW
130 for (;;) {
131 r = rtnl_resolve_interface(&rtnl, buf);
132 if (r > 0) {
133 if (ifindex_longest_name <= 0)
134 ifindex_longest_name = r;
a661dc36 135
7d4e8503
YW
136 r = strv_extend(&found, buf);
137 if (r < 0)
138 return log_oom();
139 }
a661dc36 140
7d4e8503
YW
141 char *dot = strrchr(buf, '.');
142 if (!dot)
143 break;
a661dc36 144
7d4e8503
YW
145 *dot = '\0';
146 }
7875170f 147
7d4e8503
YW
148 unsigned n = strv_length(found);
149 if (n > 1) {
150 _cleanup_free_ char *joined = NULL;
7875170f 151
7d4e8503
YW
152 joined = strv_join(found, ", ");
153 log_warning("Found multiple interfaces (%s) matching with '%s'. Using '%s' (ifindex=%i).",
154 strna(joined), s, found[0], ifindex_longest_name);
7875170f 155
7d4e8503
YW
156 } else if (n == 1) {
157 const char *proto;
158
159 proto = ASSERT_PTR(startswith(s, found[0]));
160 if (!isempty(proto))
161 log_info("Dropped protocol specifier '%s' from '%s'. Using '%s' (ifindex=%i).",
162 proto, s, found[0], ifindex_longest_name);
163 }
164
165 r = ifindex_longest_name;
7875170f 166 } else
7d4e8503
YW
167 r = rtnl_resolve_interface(&rtnl, s);
168 if (r < 0) {
169 if (ERRNO_IS_DEVICE_ABSENT(r) && arg_ifindex_permissive) {
170 log_debug_errno(r, "Interface '%s' not found, but -f specified, ignoring: %m", s);
171 return 0; /* done */
172 }
173 return log_error_errno(r, "Failed to resolve interface \"%s\": %m", s);
174 }
175
176 if (arg_ifindex > 0 && arg_ifindex != r)
177 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified multiple different interfaces. Refusing.");
178
179 arg_ifindex = r;
180 return free_and_strdup_warn(&arg_ifname, found ? found[0] : s); /* found */
7875170f
MC
181}
182
78c6a153 183static void print_source(uint64_t flags, usec_t rtt) {
51323288
LP
184 if (!arg_legend)
185 return;
186
78c6a153 187 if (flags == 0)
51323288
LP
188 return;
189
ec4b9671 190 printf("\n%s-- Information acquired via", ansi_grey());
51323288 191
283ec789
YW
192 printf(" protocol%s%s%s%s%s",
193 flags & SD_RESOLVED_DNS ? " DNS" :"",
194 flags & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "",
195 flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "",
196 flags & SD_RESOLVED_MDNS_IPV4 ? " mDNS/IPv4" : "",
197 flags & SD_RESOLVED_MDNS_IPV6 ? " mDNS/IPv6" : "");
51323288 198
ec4b9671 199 printf(" in %s.%s\n"
43fc4baa 200 "%s-- Data is authenticated: %s; Data was acquired via local or encrypted transport: %s%s\n",
5291f26d
ZJS
201 FORMAT_TIMESPAN(rtt, 100),
202 ansi_normal(),
43fc4baa
LP
203 ansi_grey(),
204 yes_no(flags & SD_RESOLVED_AUTHENTICATED),
205 yes_no(flags & SD_RESOLVED_CONFIDENTIAL),
206 ansi_normal());
5c1790d1
LP
207
208 if ((flags & (SD_RESOLVED_FROM_MASK|SD_RESOLVED_SYNTHETIC)) != 0)
209 printf("%s-- Data from:%s%s%s%s%s%s\n",
210 ansi_grey(),
211 FLAGS_SET(flags, SD_RESOLVED_SYNTHETIC) ? " synthetic" : "",
212 FLAGS_SET(flags, SD_RESOLVED_FROM_CACHE) ? " cache" : "",
213 FLAGS_SET(flags, SD_RESOLVED_FROM_ZONE) ? " zone" : "",
214 FLAGS_SET(flags, SD_RESOLVED_FROM_TRUST_ANCHOR) ? " trust-anchor" : "",
215 FLAGS_SET(flags, SD_RESOLVED_FROM_NETWORK) ? " network" : "",
216 ansi_normal());
51323288 217}
bdef7319 218
ff4a77c3 219static void print_ifindex_comment(int printed_so_far, int ifindex) {
01afd0f7
YW
220 char ifname[IF_NAMESIZE];
221 int r;
74998408 222
ff4a77c3
LP
223 if (ifindex <= 0)
224 return;
931851e8 225
01afd0f7
YW
226 r = format_ifname(ifindex, ifname);
227 if (r < 0)
228 return (void) log_warning_errno(r, "Failed to resolve interface name for index %i, ignoring: %m", ifindex);
229
230 printf("%*s%s-- link: %s%s",
231 60 > printed_so_far ? 60 - printed_so_far : 0, " ", /* Align comment to the 60th column */
232 ansi_grey(), ifname, ansi_normal());
51323288 233}
bdef7319 234
bbb86efa
ZJS
235static int resolve_host_error(const char *name, int r, const sd_bus_error *error) {
236 if (sd_bus_error_has_name(error, BUS_ERROR_DNS_NXDOMAIN))
237 return log_error_errno(r, "%s: %s", name, bus_error_message(error, r));
238
239 return log_error_errno(r, "%s: resolve call failed: %s", name, bus_error_message(error, r));
240}
241
79266746 242static int resolve_host(sd_bus *bus, const char *name) {
4afd3348
LP
243 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
244 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
79266746 245 const char *canonical = NULL;
bdef7319 246 unsigned c = 0;
51323288 247 uint64_t flags;
74998408 248 usec_t ts;
a661dc36 249 int r;
bdef7319
ZJS
250
251 assert(name);
252
a661dc36 253 log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname);
bdef7319 254
d96f9abc 255 r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveHostname");
02dd6e18
LP
256 if (r < 0)
257 return bus_log_create_error(r);
bdef7319 258
51323288 259 r = sd_bus_message_append(req, "isit", arg_ifindex, name, arg_family, arg_flags);
02dd6e18
LP
260 if (r < 0)
261 return bus_log_create_error(r);
bdef7319 262
74998408
TG
263 ts = now(CLOCK_MONOTONIC);
264
4cbfd62b 265 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
45ec7efb 266 if (r < 0)
bbb86efa 267 return resolve_host_error(name, r, &error);
bdef7319 268
74998408
TG
269 ts = now(CLOCK_MONOTONIC) - ts;
270
78c6a153 271 r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
02dd6e18
LP
272 if (r < 0)
273 return bus_log_parse_error(r);
bdef7319 274
78c6a153 275 while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
bdef7319 276 _cleanup_free_ char *pretty = NULL;
ff4a77c3 277 int ifindex, family, k;
a574b7d1 278 union in_addr_union a;
78c6a153
LP
279
280 assert_cc(sizeof(int) == sizeof(int32_t));
bdef7319 281
a574b7d1 282 r = sd_bus_message_read(reply, "i", &ifindex);
02dd6e18
LP
283 if (r < 0)
284 return bus_log_parse_error(r);
bdef7319 285
a574b7d1
YW
286 sd_bus_error_free(&error);
287 r = bus_message_read_in_addr_auto(reply, &error, &family, &a);
288 if (r < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS))
289 return log_error_errno(r, "%s: systemd-resolved returned invalid result: %s", name, bus_error_message(&error, r));
bdef7319 290
bdef7319 291 r = sd_bus_message_exit_container(reply);
02dd6e18
LP
292 if (r < 0)
293 return bus_log_parse_error(r);
bdef7319 294
a574b7d1
YW
295 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) {
296 log_debug_errno(r, "%s: systemd-resolved returned invalid result, ignoring: %s", name, bus_error_message(&error, r));
bdef7319
ZJS
297 continue;
298 }
299
a574b7d1 300 r = in_addr_ifindex_to_string(family, &a, ifindex, &pretty);
78c6a153
LP
301 if (r < 0)
302 return log_error_errno(r, "Failed to print address for %s: %m", name);
bdef7319 303
38585af3 304 k = printf("%*s%s %s%s%s",
ff4a77c3 305 (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
38585af3 306 ansi_highlight(), pretty, ansi_normal());
ff4a77c3
LP
307
308 print_ifindex_comment(k, ifindex);
309 fputc('\n', stdout);
bdef7319
ZJS
310
311 c++;
312 }
79266746
LP
313 if (r < 0)
314 return bus_log_parse_error(r);
315
316 r = sd_bus_message_exit_container(reply);
317 if (r < 0)
318 return bus_log_parse_error(r);
319
51323288 320 r = sd_bus_message_read(reply, "st", &canonical, &flags);
79266746
LP
321 if (r < 0)
322 return bus_log_parse_error(r);
323
ece174c5 324 if (!streq(name, canonical))
79266746
LP
325 printf("%*s%s (%s)\n",
326 (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
327 canonical);
bdef7319 328
d7a0f1f4
FS
329 if (c == 0)
330 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
331 "%s: no addresses found", name);
79266746 332
78c6a153 333 print_source(flags, ts);
51323288 334
79266746
LP
335 return 0;
336}
337
338static int resolve_address(sd_bus *bus, int family, const union in_addr_union *address, int ifindex) {
4afd3348
LP
339 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
340 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
79266746 341 _cleanup_free_ char *pretty = NULL;
51323288 342 uint64_t flags;
79266746 343 unsigned c = 0;
74998408 344 usec_t ts;
79266746
LP
345 int r;
346
347 assert(bus);
348 assert(IN_SET(family, AF_INET, AF_INET6));
349 assert(address);
350
78c6a153
LP
351 if (ifindex <= 0)
352 ifindex = arg_ifindex;
353
145fab1e 354 r = in_addr_ifindex_to_string(family, address, ifindex, &pretty);
79266746
LP
355 if (r < 0)
356 return log_oom();
357
0889b815 358 log_debug("Resolving %s.", pretty);
79266746 359
d96f9abc 360 r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveAddress");
79266746
LP
361 if (r < 0)
362 return bus_log_create_error(r);
363
51323288 364 r = sd_bus_message_append(req, "ii", ifindex, family);
79266746
LP
365 if (r < 0)
366 return bus_log_create_error(r);
367
368 r = sd_bus_message_append_array(req, 'y', address, FAMILY_ADDRESS_SIZE(family));
369 if (r < 0)
370 return bus_log_create_error(r);
371
51323288 372 r = sd_bus_message_append(req, "t", arg_flags);
79266746
LP
373 if (r < 0)
374 return bus_log_create_error(r);
375
74998408
TG
376 ts = now(CLOCK_MONOTONIC);
377
4cbfd62b 378 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
4ae25393
YW
379 if (r < 0)
380 return log_error_errno(r, "%s: resolve call failed: %s", pretty, bus_error_message(&error, r));
79266746 381
74998408
TG
382 ts = now(CLOCK_MONOTONIC) - ts;
383
78c6a153 384 r = sd_bus_message_enter_container(reply, 'a', "(is)");
79266746
LP
385 if (r < 0)
386 return bus_log_create_error(r);
387
78c6a153
LP
388 while ((r = sd_bus_message_enter_container(reply, 'r', "is")) > 0) {
389 const char *n;
0889b815 390 int k;
78c6a153
LP
391
392 assert_cc(sizeof(int) == sizeof(int32_t));
79266746 393
78c6a153
LP
394 r = sd_bus_message_read(reply, "is", &ifindex, &n);
395 if (r < 0)
396 return r;
397
398 r = sd_bus_message_exit_container(reply);
399 if (r < 0)
400 return r;
401
38585af3
LP
402 k = printf("%*s%s %s%s%s",
403 (int) strlen(pretty), c == 0 ? pretty : "",
404 c == 0 ? ":" : " ",
405 ansi_highlight(), n, ansi_normal());
78c6a153 406
0889b815
LP
407 print_ifindex_comment(k, ifindex);
408 fputc('\n', stdout);
79266746
LP
409
410 c++;
bdef7319 411 }
79266746
LP
412 if (r < 0)
413 return bus_log_parse_error(r);
bdef7319 414
02dd6e18
LP
415 r = sd_bus_message_exit_container(reply);
416 if (r < 0)
417 return bus_log_parse_error(r);
418
51323288
LP
419 r = sd_bus_message_read(reply, "t", &flags);
420 if (r < 0)
421 return bus_log_parse_error(r);
422
d7a0f1f4
FS
423 if (c == 0)
424 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
425 "%s: no names found", pretty);
79266746 426
78c6a153 427 print_source(flags, ts);
51323288 428
79266746
LP
429 return 0;
430}
431
dab48ea6
ZJS
432static int output_rr_packet(const void *d, size_t l, int ifindex) {
433 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
dab48ea6 434 int r;
dab48ea6 435
ab26cdf7 436 r = dns_resource_record_new_from_raw(&rr, d, l);
dab48ea6
ZJS
437 if (r < 0)
438 return log_error_errno(r, "Failed to parse RR: %m");
439
440 if (arg_raw == RAW_PAYLOAD) {
441 void *data;
442 ssize_t k;
443
444 k = dns_resource_record_payload(rr, &data);
445 if (k < 0)
446 return log_error_errno(k, "Cannot dump RR: %m");
447 fwrite(data, 1, k, stdout);
448 } else {
449 const char *s;
0889b815 450 int k;
dab48ea6
ZJS
451
452 s = dns_resource_record_to_string(rr);
453 if (!s)
454 return log_oom();
455
0889b815
LP
456 k = printf("%s", s);
457 print_ifindex_comment(k, ifindex);
458 fputc('\n', stdout);
dab48ea6
ZJS
459 }
460
461 return 0;
462}
463
018b642a
LP
464static int idna_candidate(const char *name, char **ret) {
465 _cleanup_free_ char *idnafied = NULL;
466 int r;
467
468 assert(name);
469 assert(ret);
470
471 r = dns_name_apply_idna(name, &idnafied);
472 if (r < 0)
473 return log_error_errno(r, "Failed to apply IDNA to name '%s': %m", name);
474 if (r > 0 && !streq(name, idnafied)) {
475 *ret = TAKE_PTR(idnafied);
476 return true;
477 }
478
479 *ret = NULL;
480 return false;
481}
482
058946d1
ZJS
483static bool single_label_nonsynthetic(const char *name) {
484 _cleanup_free_ char *first_label = NULL;
485 int r;
486
487 if (!dns_name_is_single_label(name))
488 return false;
489
17f244e8
LP
490 if (is_localhost(name) ||
491 is_gateway_hostname(name) ||
492 is_outbound_hostname(name) ||
493 is_dns_stub_hostname(name) ||
494 is_dns_proxy_stub_hostname(name))
058946d1
ZJS
495 return false;
496
497 r = resolve_system_hostname(NULL, &first_label);
498 if (r < 0) {
499 log_warning_errno(r, "Failed to determine the hostname: %m");
500 return false;
501 }
502
503 return !streq(name, first_label);
504}
505
a60f4d0b 506static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type, bool warn_missing) {
4afd3348
LP
507 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
508 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
018b642a
LP
509 _cleanup_free_ char *idnafied = NULL;
510 bool needs_authentication = false;
2d4c5cbc 511 unsigned n = 0;
51323288 512 uint64_t flags;
74998408 513 usec_t ts;
018b642a 514 int r;
2d4c5cbc
LP
515
516 assert(name);
517
a661dc36 518 log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(class), dns_type_to_string(type), isempty(arg_ifname) ? "*" : arg_ifname);
2d4c5cbc 519
200b4f3d 520 if (dns_name_dot_suffixed(name) == 0 && single_label_nonsynthetic(name))
20e994b3
ZJS
521 log_notice("(Note that search domains are not appended when --type= is specified. "
522 "Please specify fully qualified domain names, or remove --type= switch from invocation in order to request regular hostname resolution.)");
018b642a
LP
523
524 r = idna_candidate(name, &idnafied);
525 if (r < 0)
526 return r;
527 if (r > 0)
20e994b3 528 log_notice("(Note that IDNA translation is not applied when --type= is specified. "
018b642a
LP
529 "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.",
530 idnafied);
531
d96f9abc 532 r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveRecord");
2d4c5cbc
LP
533 if (r < 0)
534 return bus_log_create_error(r);
535
c1dafe4f 536 r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, class, type, arg_flags);
2d4c5cbc
LP
537 if (r < 0)
538 return bus_log_create_error(r);
539
74998408
TG
540 ts = now(CLOCK_MONOTONIC);
541
4cbfd62b 542 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
2d4c5cbc 543 if (r < 0) {
a60f4d0b
SS
544 if (warn_missing || r != -ENXIO)
545 log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r));
2d4c5cbc
LP
546 return r;
547 }
548
74998408
TG
549 ts = now(CLOCK_MONOTONIC) - ts;
550
78c6a153 551 r = sd_bus_message_enter_container(reply, 'a', "(iqqay)");
51323288
LP
552 if (r < 0)
553 return bus_log_parse_error(r);
554
78c6a153 555 while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) {
2d4c5cbc 556 uint16_t c, t;
78c6a153 557 int ifindex;
2d4c5cbc
LP
558 const void *d;
559 size_t l;
560
78c6a153
LP
561 assert_cc(sizeof(int) == sizeof(int32_t));
562
563 r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t);
2d4c5cbc
LP
564 if (r < 0)
565 return bus_log_parse_error(r);
566
567 r = sd_bus_message_read_array(reply, 'y', &d, &l);
568 if (r < 0)
569 return bus_log_parse_error(r);
570
571 r = sd_bus_message_exit_container(reply);
572 if (r < 0)
573 return bus_log_parse_error(r);
574
dab48ea6
ZJS
575 if (arg_raw == RAW_PACKET) {
576 uint64_t u64 = htole64(l);
2d4c5cbc 577
dab48ea6
ZJS
578 fwrite(&u64, sizeof(u64), 1, stdout);
579 fwrite(d, 1, l, stdout);
2e74028a 580 } else {
dab48ea6
ZJS
581 r = output_rr_packet(d, l, ifindex);
582 if (r < 0)
583 return r;
2e74028a 584 }
78c6a153 585
41815a4a
LP
586 if (dns_type_needs_authentication(t))
587 needs_authentication = true;
588
2d4c5cbc
LP
589 n++;
590 }
591 if (r < 0)
592 return bus_log_parse_error(r);
593
594 r = sd_bus_message_exit_container(reply);
595 if (r < 0)
596 return bus_log_parse_error(r);
597
51323288
LP
598 r = sd_bus_message_read(reply, "t", &flags);
599 if (r < 0)
600 return bus_log_parse_error(r);
601
2d4c5cbc 602 if (n == 0) {
a60f4d0b
SS
603 if (warn_missing)
604 log_error("%s: no records found", name);
2d4c5cbc
LP
605 return -ESRCH;
606 }
607
78c6a153 608 print_source(flags, ts);
51323288 609
41815a4a
LP
610 if ((flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) {
611 fflush(stdout);
612
613 fprintf(stderr, "\n%s"
614 "WARNING: The resources shown contain cryptographic key data which could not be\n"
615 " authenticated. It is not suitable to authenticate any communication.\n"
616 " This is usually indication that DNSSEC authentication was not enabled\n"
617 " or is not available for the selected protocol or DNS servers.%s\n",
618 ansi_highlight_red(),
619 ansi_normal());
620 }
621
2d4c5cbc
LP
622 return 0;
623}
624
c1dafe4f
LP
625static int resolve_rfc4501(sd_bus *bus, const char *name) {
626 uint16_t type = 0, class = 0;
627 const char *p, *q, *n;
628 int r;
629
630 assert(bus);
631 assert(name);
632 assert(startswith(name, "dns:"));
633
634 /* Parse RFC 4501 dns: URIs */
635
636 p = name + 4;
637
638 if (p[0] == '/') {
639 const char *e;
640
641 if (p[1] != '/')
642 goto invalid;
643
644 e = strchr(p + 2, '/');
645 if (!e)
646 goto invalid;
647
648 if (e != p + 2)
649 log_warning("DNS authority specification not supported; ignoring specified authority.");
650
651 p = e + 1;
652 }
653
654 q = strchr(p, '?');
655 if (q) {
2f82562b 656 n = strndupa_safe(p, q - p);
c1dafe4f
LP
657 q++;
658
659 for (;;) {
660 const char *f;
661
662 f = startswith_no_case(q, "class=");
663 if (f) {
664 _cleanup_free_ char *t = NULL;
665 const char *e;
666
baaa35ad
ZJS
667 if (class != 0)
668 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
669 "DNS class specified twice.");
c1dafe4f
LP
670
671 e = strchrnul(f, ';');
672 t = strndup(f, e - f);
673 if (!t)
674 return log_oom();
675
676 r = dns_class_from_string(t);
baaa35ad 677 if (r < 0)
7211c853 678 return log_error_errno(r, "Unknown DNS class %s.", t);
c1dafe4f
LP
679
680 class = r;
681
682 if (*e == ';') {
683 q = e + 1;
684 continue;
685 }
686
687 break;
688 }
689
690 f = startswith_no_case(q, "type=");
691 if (f) {
692 _cleanup_free_ char *t = NULL;
693 const char *e;
694
baaa35ad
ZJS
695 if (type != 0)
696 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
697 "DNS type specified twice.");
c1dafe4f
LP
698
699 e = strchrnul(f, ';');
700 t = strndup(f, e - f);
701 if (!t)
702 return log_oom();
703
704 r = dns_type_from_string(t);
baaa35ad 705 if (r < 0)
7211c853 706 return log_error_errno(r, "Unknown DNS type %s: %m", t);
c1dafe4f
LP
707
708 type = r;
709
710 if (*e == ';') {
711 q = e + 1;
712 continue;
713 }
714
715 break;
716 }
717
718 goto invalid;
719 }
720 } else
721 n = p;
722
c1dafe4f 723 if (class == 0)
4ac2ca1b
ZJS
724 class = arg_class ?: DNS_CLASS_IN;
725 if (type == 0)
726 type = arg_type ?: DNS_TYPE_A;
c1dafe4f 727
a60f4d0b 728 return resolve_record(bus, n, class, type, true);
c1dafe4f
LP
729
730invalid:
baaa35ad
ZJS
731 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
732 "Invalid DNS URI: %s", name);
c1dafe4f
LP
733}
734
a7a4c60a
YW
735static int verb_query(int argc, char **argv, void *userdata) {
736 sd_bus *bus = userdata;
a7a4c60a
YW
737 int q, r = 0;
738
739 if (arg_type != 0)
740 STRV_FOREACH(p, argv + 1) {
741 q = resolve_record(bus, *p, arg_class, arg_type, true);
742 if (q < 0)
743 r = q;
744 }
745
746 else
747 STRV_FOREACH(p, argv + 1) {
748 if (startswith(*p, "dns:"))
749 q = resolve_rfc4501(bus, *p);
750 else {
751 int family, ifindex;
752 union in_addr_union a;
753
754 q = in_addr_ifindex_from_string_auto(*p, &family, &a, &ifindex);
755 if (q >= 0)
756 q = resolve_address(bus, family, &a, ifindex);
757 else
758 q = resolve_host(bus, *p);
759 }
760 if (q < 0)
761 r = q;
762 }
763
764 return r;
765}
766
45ec7efb
LP
767static int resolve_service(sd_bus *bus, const char *name, const char *type, const char *domain) {
768 const char *canonical_name, *canonical_type, *canonical_domain;
4afd3348
LP
769 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
770 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
45ec7efb
LP
771 size_t indent, sz;
772 uint64_t flags;
773 const char *p;
774 unsigned c;
775 usec_t ts;
776 int r;
777
778 assert(bus);
779 assert(domain);
780
3c6f7c34
LP
781 name = empty_to_null(name);
782 type = empty_to_null(type);
45ec7efb 783
45ec7efb 784 if (name)
a661dc36 785 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);
45ec7efb 786 else if (type)
a661dc36 787 log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname);
45ec7efb 788 else
a661dc36 789 log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname);
45ec7efb 790
d96f9abc 791 r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveService");
45ec7efb
LP
792 if (r < 0)
793 return bus_log_create_error(r);
794
795 r = sd_bus_message_append(req, "isssit", arg_ifindex, name, type, domain, arg_family, arg_flags);
796 if (r < 0)
797 return bus_log_create_error(r);
798
799 ts = now(CLOCK_MONOTONIC);
800
4cbfd62b 801 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
45ec7efb
LP
802 if (r < 0)
803 return log_error_errno(r, "Resolve call failed: %s", bus_error_message(&error, r));
804
805 ts = now(CLOCK_MONOTONIC) - ts;
806
807 r = sd_bus_message_enter_container(reply, 'a', "(qqqsa(iiay)s)");
808 if (r < 0)
809 return bus_log_parse_error(r);
810
811 indent =
812 (name ? strlen(name) + 1 : 0) +
813 (type ? strlen(type) + 1 : 0) +
814 strlen(domain) + 2;
815
816 c = 0;
817 while ((r = sd_bus_message_enter_container(reply, 'r', "qqqsa(iiay)s")) > 0) {
818 uint16_t priority, weight, port;
819 const char *hostname, *canonical;
820
821 r = sd_bus_message_read(reply, "qqqs", &priority, &weight, &port, &hostname);
822 if (r < 0)
823 return bus_log_parse_error(r);
824
825 if (name)
826 printf("%*s%s", (int) strlen(name), c == 0 ? name : "", c == 0 ? "/" : " ");
827 if (type)
828 printf("%*s%s", (int) strlen(type), c == 0 ? type : "", c == 0 ? "/" : " ");
829
830 printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
831 (int) strlen(domain), c == 0 ? domain : "",
832 c == 0 ? ":" : " ",
833 hostname, port,
834 priority, weight);
835
836 r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
837 if (r < 0)
838 return bus_log_parse_error(r);
839
840 while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
841 _cleanup_free_ char *pretty = NULL;
0889b815 842 int ifindex, family, k;
aea29a30 843 union in_addr_union a;
45ec7efb
LP
844
845 assert_cc(sizeof(int) == sizeof(int32_t));
846
a574b7d1 847 r = sd_bus_message_read(reply, "i", &ifindex);
45ec7efb
LP
848 if (r < 0)
849 return bus_log_parse_error(r);
850
a574b7d1
YW
851 sd_bus_error_free(&error);
852 r = bus_message_read_in_addr_auto(reply, &error, &family, &a);
853 if (r < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS))
854 return log_error_errno(r, "%s: systemd-resolved returned invalid result: %s", name, bus_error_message(&error, r));
45ec7efb
LP
855
856 r = sd_bus_message_exit_container(reply);
857 if (r < 0)
858 return bus_log_parse_error(r);
859
a574b7d1
YW
860 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) {
861 log_debug_errno(r, "%s: systemd-resolved returned invalid result, ignoring: %s", name, bus_error_message(&error, r));
45ec7efb
LP
862 continue;
863 }
864
a574b7d1 865 r = in_addr_ifindex_to_string(family, &a, ifindex, &pretty);
45ec7efb
LP
866 if (r < 0)
867 return log_error_errno(r, "Failed to print address for %s: %m", name);
868
0889b815
LP
869 k = printf("%*s%s", (int) indent, "", pretty);
870 print_ifindex_comment(k, ifindex);
871 fputc('\n', stdout);
45ec7efb
LP
872 }
873 if (r < 0)
874 return bus_log_parse_error(r);
875
876 r = sd_bus_message_exit_container(reply);
877 if (r < 0)
878 return bus_log_parse_error(r);
879
880 r = sd_bus_message_read(reply, "s", &canonical);
881 if (r < 0)
882 return bus_log_parse_error(r);
883
884 if (!streq(hostname, canonical))
885 printf("%*s(%s)\n", (int) indent, "", canonical);
886
887 r = sd_bus_message_exit_container(reply);
888 if (r < 0)
889 return bus_log_parse_error(r);
890
891 c++;
892 }
893 if (r < 0)
894 return bus_log_parse_error(r);
895
896 r = sd_bus_message_exit_container(reply);
897 if (r < 0)
898 return bus_log_parse_error(r);
899
900 r = sd_bus_message_enter_container(reply, 'a', "ay");
901 if (r < 0)
902 return bus_log_parse_error(r);
903
45ec7efb
LP
904 while ((r = sd_bus_message_read_array(reply, 'y', (const void**) &p, &sz)) > 0) {
905 _cleanup_free_ char *escaped = NULL;
906
907 escaped = cescape_length(p, sz);
908 if (!escaped)
909 return log_oom();
910
911 printf("%*s%s\n", (int) indent, "", escaped);
45ec7efb
LP
912 }
913 if (r < 0)
914 return bus_log_parse_error(r);
915
916 r = sd_bus_message_exit_container(reply);
917 if (r < 0)
918 return bus_log_parse_error(r);
919
920 r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags);
921 if (r < 0)
922 return bus_log_parse_error(r);
923
3c6f7c34
LP
924 canonical_name = empty_to_null(canonical_name);
925 canonical_type = empty_to_null(canonical_type);
45ec7efb
LP
926
927 if (!streq_ptr(name, canonical_name) ||
928 !streq_ptr(type, canonical_type) ||
929 !streq_ptr(domain, canonical_domain)) {
930
931 printf("%*s(", (int) indent, "");
932
933 if (canonical_name)
934 printf("%s/", canonical_name);
935 if (canonical_type)
936 printf("%s/", canonical_type);
937
938 printf("%s)\n", canonical_domain);
939 }
940
941 print_source(flags, ts);
942
943 return 0;
944}
945
a7a4c60a
YW
946static int verb_service(int argc, char **argv, void *userdata) {
947 sd_bus *bus = userdata;
948
949 if (argc == 2)
950 return resolve_service(bus, NULL, NULL, argv[1]);
951 else if (argc == 3)
952 return resolve_service(bus, NULL, argv[1], argv[2]);
953 else
954 return resolve_service(bus, argv[1], argv[2], argv[3]);
955}
956
4ac2ca1b
ZJS
957static int resolve_openpgp(sd_bus *bus, const char *address) {
958 const char *domain, *full;
959 int r;
960 _cleanup_free_ char *hashed = NULL;
961
962 assert(bus);
963 assert(address);
964
965 domain = strrchr(address, '@');
baaa35ad
ZJS
966 if (!domain)
967 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
968 "Address does not contain '@': \"%s\"", address);
969 if (domain == address || domain[1] == '\0')
970 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
971 "Address starts or ends with '@': \"%s\"", address);
4ac2ca1b
ZJS
972 domain++;
973
a60f4d0b 974 r = string_hashsum_sha256(address, domain - 1 - address, &hashed);
4ac2ca1b
ZJS
975 if (r < 0)
976 return log_error_errno(r, "Hashing failed: %m");
977
a60f4d0b
SS
978 strshorten(hashed, 56);
979
4ac2ca1b
ZJS
980 full = strjoina(hashed, "._openpgpkey.", domain);
981 log_debug("Looking up \"%s\".", full);
982
a60f4d0b
SS
983 r = resolve_record(bus, full,
984 arg_class ?: DNS_CLASS_IN,
985 arg_type ?: DNS_TYPE_OPENPGPKEY, false);
986
987 if (IN_SET(r, -ENXIO, -ESRCH)) { /* NXDOMAIN or NODATA? */
670e95ae 988 hashed = mfree(hashed);
a60f4d0b
SS
989 r = string_hashsum_sha224(address, domain - 1 - address, &hashed);
990 if (r < 0)
991 return log_error_errno(r, "Hashing failed: %m");
992
993 full = strjoina(hashed, "._openpgpkey.", domain);
994 log_debug("Looking up \"%s\".", full);
995
996 return resolve_record(bus, full,
997 arg_class ?: DNS_CLASS_IN,
998 arg_type ?: DNS_TYPE_OPENPGPKEY, true);
999 }
1000
1001 return r;
4ac2ca1b
ZJS
1002}
1003
a7a4c60a
YW
1004static int verb_openpgp(int argc, char **argv, void *userdata) {
1005 sd_bus *bus = userdata;
a7a4c60a
YW
1006 int q, r = 0;
1007
1008 STRV_FOREACH(p, argv + 1) {
1009 q = resolve_openpgp(bus, *p);
1010 if (q < 0)
1011 r = q;
1012 }
1013
1014 return r;
1015}
1016
ebbc70e5 1017static int resolve_tlsa(sd_bus *bus, const char *family, const char *address) {
82d1d240
ZJS
1018 const char *port;
1019 uint16_t port_num = 443;
1020 _cleanup_free_ char *full = NULL;
1021 int r;
1022
1023 assert(bus);
1024 assert(address);
1025
1026 port = strrchr(address, ':');
1027 if (port) {
10452f7c
SS
1028 r = parse_ip_port(port + 1, &port_num);
1029 if (r < 0)
82d1d240
ZJS
1030 return log_error_errno(r, "Invalid port \"%s\".", port + 1);
1031
2f82562b 1032 address = strndupa_safe(address, port - address);
82d1d240
ZJS
1033 }
1034
ebbc70e5 1035 r = asprintf(&full, "_%u._%s.%s",
82d1d240 1036 port_num,
ebbc70e5 1037 family,
82d1d240
ZJS
1038 address);
1039 if (r < 0)
1040 return log_oom();
1041
1042 log_debug("Looking up \"%s\".", full);
1043
1044 return resolve_record(bus, full,
1045 arg_class ?: DNS_CLASS_IN,
a60f4d0b 1046 arg_type ?: DNS_TYPE_TLSA, true);
82d1d240
ZJS
1047}
1048
ebbc70e5
YW
1049static bool service_family_is_valid(const char *s) {
1050 return STR_IN_SET(s, "tcp", "udp", "sctp");
1051}
1052
a7a4c60a
YW
1053static int verb_tlsa(int argc, char **argv, void *userdata) {
1054 sd_bus *bus = userdata;
de010b0b 1055 char **args = argv + 1;
ebbc70e5 1056 const char *family = "tcp";
a7a4c60a
YW
1057 int q, r = 0;
1058
ebbc70e5
YW
1059 if (service_family_is_valid(argv[1])) {
1060 family = argv[1];
a7a4c60a 1061 args++;
ebbc70e5 1062 }
a7a4c60a
YW
1063
1064 STRV_FOREACH(p, args) {
1065 q = resolve_tlsa(bus, family, *p);
1066 if (q < 0)
1067 r = q;
1068 }
1069
1070 return r;
1071}
1072
1073static int show_statistics(int argc, char **argv, void *userdata) {
29e15e98 1074 _cleanup_(table_unrefp) Table *table = NULL;
a67e5c6e 1075 JsonVariant *reply = NULL;
bc837621
KV
1076 _cleanup_(varlink_unrefp) Varlink *vl = NULL;
1077 int r;
a150ff5e 1078
bc837621 1079 r = varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor");
593f665c 1080 if (r < 0)
bc837621 1081 return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m");
593f665c 1082
bc837621 1083 r = varlink_call(vl, "io.systemd.Resolve.Monitor.DumpStatistics", NULL, &reply, NULL, 0);
a150ff5e 1084 if (r < 0)
bc837621 1085 return log_error_errno(r, "Failed to issue DumpStatistics() varlink call: %m");
a150ff5e 1086
bc837621 1087 if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
a67e5c6e 1088 return json_variant_dump(reply, arg_json_format_flags, NULL, NULL);
a150ff5e 1089
bc837621
KV
1090 struct statistics {
1091 JsonVariant *transactions;
1092 JsonVariant *cache;
1093 JsonVariant *dnssec;
1094 } statistics;
1095
1096 static const JsonDispatch statistics_dispatch_table[] = {
1097 { "transactions", JSON_VARIANT_OBJECT, json_dispatch_variant_noref, offsetof(struct statistics, transactions), JSON_MANDATORY },
1098 { "cache", JSON_VARIANT_OBJECT, json_dispatch_variant_noref, offsetof(struct statistics, cache), JSON_MANDATORY },
1099 { "dnssec", JSON_VARIANT_OBJECT, json_dispatch_variant_noref, offsetof(struct statistics, dnssec), JSON_MANDATORY },
1100 {},
1101 };
1102
f1b622a0 1103 r = json_dispatch(reply, statistics_dispatch_table, JSON_LOG, &statistics);
f7700834 1104 if (r < 0)
bc837621
KV
1105 return r;
1106
1107 struct transactions {
1108 uint64_t n_current_transactions;
1109 uint64_t n_transactions_total;
1110 uint64_t n_timeouts_total;
1111 uint64_t n_timeouts_served_stale_total;
1112 uint64_t n_failure_responses_total;
1113 uint64_t n_failure_responses_served_stale_total;
1114 } transactions;
1115
1116 static const JsonDispatch transactions_dispatch_table[] = {
1117 { "currentTransactions", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct transactions, n_current_transactions), JSON_MANDATORY },
1118 { "totalTransactions", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct transactions, n_transactions_total), JSON_MANDATORY },
1119 { "totalTimeouts", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct transactions, n_timeouts_total), JSON_MANDATORY },
1120 { "totalTimeoutsServedStale", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct transactions, n_timeouts_served_stale_total), JSON_MANDATORY },
1121 { "totalFailedResponses", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_total), JSON_MANDATORY },
1122 { "totalFailedResponsesServedStale", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_served_stale_total), JSON_MANDATORY },
1123 {},
1124 };
a150ff5e 1125
f1b622a0 1126 r = json_dispatch(statistics.transactions, transactions_dispatch_table, JSON_LOG, &transactions);
a150ff5e 1127 if (r < 0)
bc837621 1128 return r;
a150ff5e 1129
bc837621
KV
1130 struct cache {
1131 uint64_t cache_size;
1132 uint64_t n_cache_hit;
1133 uint64_t n_cache_miss;
1134 } cache;
a150ff5e 1135
bc837621
KV
1136 static const JsonDispatch cache_dispatch_table[] = {
1137 { "size", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct cache, cache_size), JSON_MANDATORY },
1138 { "hits", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct cache, n_cache_hit), JSON_MANDATORY },
1139 { "misses", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct cache, n_cache_miss), JSON_MANDATORY },
1140 {},
1141 };
1142
f1b622a0 1143 r = json_dispatch(statistics.cache, cache_dispatch_table, JSON_LOG, &cache);
f7700834 1144 if (r < 0)
bc837621
KV
1145 return r;
1146
1147 struct dnsssec {
1148 uint64_t n_dnssec_secure;
1149 uint64_t n_dnssec_insecure;
1150 uint64_t n_dnssec_bogus;
1151 uint64_t n_dnssec_indeterminate;
1152 } dnsssec;
1153
1154 static const JsonDispatch dnssec_dispatch_table[] = {
1155 { "secure", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_secure), JSON_MANDATORY },
1156 { "insecure", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_insecure), JSON_MANDATORY },
1157 { "bogus", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_bogus), JSON_MANDATORY },
1158 { "indeterminate", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_indeterminate), JSON_MANDATORY },
1159 {},
1160 };
a150ff5e 1161
f1b622a0 1162 r = json_dispatch(statistics.dnssec, dnssec_dispatch_table, JSON_LOG, &dnsssec);
a150ff5e 1163 if (r < 0)
bc837621 1164 return r;
a150ff5e 1165
37a50123 1166 table = table_new_vertical();
29e15e98
YW
1167 if (!table)
1168 return log_oom();
1169
29e15e98
YW
1170 r = table_add_many(table,
1171 TABLE_STRING, "Transactions",
1172 TABLE_SET_COLOR, ansi_highlight(),
37a50123 1173 TABLE_SET_ALIGN_PERCENT, 0,
29e15e98 1174 TABLE_EMPTY,
37a50123 1175 TABLE_FIELD, "Current Transactions",
29e15e98 1176 TABLE_SET_ALIGN_PERCENT, 100,
bc837621 1177 TABLE_UINT64, transactions.n_current_transactions,
37a50123
LP
1178 TABLE_SET_ALIGN_PERCENT, 100,
1179 TABLE_FIELD, "Total Transactions",
bc837621 1180 TABLE_UINT64, transactions.n_transactions_total,
29e15e98
YW
1181 TABLE_EMPTY, TABLE_EMPTY,
1182 TABLE_STRING, "Cache",
1183 TABLE_SET_COLOR, ansi_highlight(),
1184 TABLE_SET_ALIGN_PERCENT, 0,
1185 TABLE_EMPTY,
37a50123 1186 TABLE_FIELD, "Current Cache Size",
29e15e98 1187 TABLE_SET_ALIGN_PERCENT, 100,
bc837621 1188 TABLE_UINT64, cache.cache_size,
37a50123 1189 TABLE_FIELD, "Cache Hits",
bc837621 1190 TABLE_UINT64, cache.n_cache_hit,
37a50123 1191 TABLE_FIELD, "Cache Misses",
bc837621
KV
1192 TABLE_UINT64, cache.n_cache_miss,
1193 TABLE_EMPTY, TABLE_EMPTY,
1194 TABLE_STRING, "Failure Transactions",
1195 TABLE_SET_COLOR, ansi_highlight(),
1196 TABLE_SET_ALIGN_PERCENT, 0,
1197 TABLE_EMPTY,
1198 TABLE_FIELD, "Total Timeouts",
1199 TABLE_SET_ALIGN_PERCENT, 100,
1200 TABLE_UINT64, transactions.n_timeouts_total,
1201 TABLE_FIELD, "Total Timeouts (Stale Data Served)",
1202 TABLE_UINT64, transactions.n_timeouts_served_stale_total,
1203 TABLE_FIELD, "Total Failure Responses",
1204 TABLE_UINT64, transactions.n_failure_responses_total,
1205 TABLE_FIELD, "Total Failure Responses (Stale Data Served)",
1206 TABLE_UINT64, transactions.n_failure_responses_served_stale_total,
29e15e98
YW
1207 TABLE_EMPTY, TABLE_EMPTY,
1208 TABLE_STRING, "DNSSEC Verdicts",
1209 TABLE_SET_COLOR, ansi_highlight(),
1210 TABLE_SET_ALIGN_PERCENT, 0,
1211 TABLE_EMPTY,
37a50123 1212 TABLE_FIELD, "Secure",
29e15e98 1213 TABLE_SET_ALIGN_PERCENT, 100,
bc837621 1214 TABLE_UINT64, dnsssec.n_dnssec_secure,
37a50123 1215 TABLE_FIELD, "Insecure",
bc837621 1216 TABLE_UINT64, dnsssec.n_dnssec_insecure,
37a50123 1217 TABLE_FIELD, "Bogus",
bc837621 1218 TABLE_UINT64, dnsssec.n_dnssec_bogus,
e30b4c13 1219 TABLE_FIELD, "Indeterminate",
bc837621
KV
1220 TABLE_UINT64, dnsssec.n_dnssec_indeterminate
1221 );
29e15e98 1222 if (r < 0)
85840949 1223 return table_log_add_error(r);
29e15e98
YW
1224
1225 r = table_print(table, NULL);
1226 if (r < 0)
4b6607d9 1227 return table_log_print_error(r);
a150ff5e
LP
1228
1229 return 0;
1230}
1231
a7a4c60a 1232static int reset_statistics(int argc, char **argv, void *userdata) {
a67e5c6e 1233 JsonVariant *reply = NULL;
bc837621 1234 _cleanup_(varlink_unrefp) Varlink *vl = NULL;
a150ff5e
LP
1235 int r;
1236
bc837621 1237 r = varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor");
a150ff5e 1238 if (r < 0)
bc837621
KV
1239 return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m");
1240
1241 r = varlink_call(vl, "io.systemd.Resolve.Monitor.ResetStatistics", NULL, &reply, NULL, 0);
1242 if (r < 0)
1243 return log_error_errno(r, "Failed to issue ResetStatistics() varlink call: %m");
1244
bc837621
KV
1245 if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
1246 return json_variant_dump(reply, arg_json_format_flags, NULL, NULL);
a150ff5e
LP
1247
1248 return 0;
1249}
1250
a7a4c60a 1251static int flush_caches(int argc, char **argv, void *userdata) {
ba35662f 1252 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
a7a4c60a 1253 sd_bus *bus = userdata;
ba35662f
LP
1254 int r;
1255
d96f9abc 1256 r = bus_call_method(bus, bus_resolve_mgr, "FlushCaches", &error, NULL, NULL);
ba35662f
LP
1257 if (r < 0)
1258 return log_error_errno(r, "Failed to flush caches: %s", bus_error_message(&error, r));
1259
1260 return 0;
1261}
1262
a7a4c60a 1263static int reset_server_features(int argc, char **argv, void *userdata) {
d55b0463 1264 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
a7a4c60a 1265 sd_bus *bus = userdata;
d55b0463
LP
1266 int r;
1267
d96f9abc 1268 r = bus_call_method(bus, bus_resolve_mgr, "ResetServerFeatures", &error, NULL, NULL);
d55b0463
LP
1269 if (r < 0)
1270 return log_error_errno(r, "Failed to reset server features: %s", bus_error_message(&error, r));
1271
1272 return 0;
1273}
1274
889a1b9f
LP
1275static int read_dns_server_one(
1276 sd_bus_message *m,
1277 bool with_ifindex, /* read "ifindex" reply that also carries an interface index */
1278 bool extended, /* read "extended" reply, i.e. with port number and server name */
1279 bool only_global, /* suppress entries with an (non-loopback) ifindex set (i.e. which are specific to some interface) */
1280 char **ret) {
1281
a574b7d1 1282 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
58f48a56 1283 _cleanup_free_ char *pretty = NULL;
a574b7d1 1284 union in_addr_union a;
a747e71c 1285 const char *name = NULL;
a5e6c849
LP
1286 int32_t ifindex = 0;
1287 int family, r, k;
a747e71c 1288 uint16_t port = 0;
58f48a56
YW
1289
1290 assert(m);
1291 assert(ret);
1292
5707fb12
LP
1293 r = sd_bus_message_enter_container(
1294 m,
1295 'r',
1296 with_ifindex ? (extended ? "iiayqs" : "iiay") :
1297 (extended ? "iayqs" : "iay"));
58f48a56
YW
1298 if (r <= 0)
1299 return r;
1300
1301 if (with_ifindex) {
1302 r = sd_bus_message_read(m, "i", &ifindex);
1303 if (r < 0)
1304 return r;
1305 }
1306
a574b7d1
YW
1307 k = bus_message_read_in_addr_auto(m, &error, &family, &a);
1308 if (k < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS))
1309 return k;
58f48a56 1310
a747e71c
YW
1311 if (extended) {
1312 r = sd_bus_message_read(m, "q", &port);
1313 if (r < 0)
1314 return r;
1315
1316 r = sd_bus_message_read(m, "s", &name);
1317 if (r < 0)
1318 return r;
1319 }
1320
58f48a56
YW
1321 r = sd_bus_message_exit_container(m);
1322 if (r < 0)
1323 return r;
1324
a574b7d1
YW
1325 if (k < 0) {
1326 log_debug("Invalid DNS server, ignoring: %s", bus_error_message(&error, k));
58f48a56
YW
1327 *ret = NULL;
1328 return 1;
1329 }
1330
889a1b9f
LP
1331 if (only_global && ifindex > 0 && ifindex != LOOPBACK_IFINDEX) {
1332 /* This one has an (non-loopback) ifindex set, and we were told to suppress those. Hence do so. */
58f48a56
YW
1333 *ret = NULL;
1334 return 1;
1335 }
1336
a574b7d1 1337 r = in_addr_port_ifindex_name_to_string(family, &a, port, ifindex, name, &pretty);
58f48a56
YW
1338 if (r < 0)
1339 return r;
1340
1341 *ret = TAKE_PTR(pretty);
58f48a56
YW
1342 return 1;
1343}
1344
a747e71c 1345static int map_link_dns_servers_internal(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata, bool extended) {
99534007 1346 char ***l = ASSERT_PTR(userdata);
be371fe0
LP
1347 int r;
1348
1349 assert(bus);
1350 assert(member);
1351 assert(m);
be371fe0 1352
a747e71c 1353 r = sd_bus_message_enter_container(m, 'a', extended ? "(iayqs)" : "(iay)");
be371fe0
LP
1354 if (r < 0)
1355 return r;
1356
1357 for (;;) {
6abdec98 1358 _cleanup_free_ char *pretty = NULL;
be371fe0 1359
889a1b9f 1360 r = read_dns_server_one(m, /* with_ifindex= */ false, extended, /* only_global= */ false, &pretty);
be371fe0
LP
1361 if (r < 0)
1362 return r;
1363 if (r == 0)
1364 break;
1365
58f48a56 1366 if (isempty(pretty))
be371fe0 1367 continue;
be371fe0 1368
6abdec98 1369 r = strv_consume(l, TAKE_PTR(pretty));
be371fe0
LP
1370 if (r < 0)
1371 return r;
1372 }
1373
1374 r = sd_bus_message_exit_container(m);
1375 if (r < 0)
1376 return r;
1377
1378 return 0;
1379}
1380
a747e71c
YW
1381static int map_link_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1382 return map_link_dns_servers_internal(bus, member, m, error, userdata, false);
1383}
1384
1385static int map_link_dns_servers_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1386 return map_link_dns_servers_internal(bus, member, m, error, userdata, true);
1387}
1388
446c6415
YW
1389static int map_link_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1390 assert(m);
1391 assert(userdata);
1392
889a1b9f 1393 return read_dns_server_one(m, /* with_ifindex= */ false, /* extended= */ false, /* only_global= */ false, userdata);
a747e71c
YW
1394}
1395
1396static int map_link_current_dns_server_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1397 assert(m);
1398 assert(userdata);
1399
889a1b9f 1400 return read_dns_server_one(m, /* with_ifindex= */ false, /* extended= */ true, /* only_global= */ false, userdata);
446c6415
YW
1401}
1402
c513bb6e
YW
1403static int read_domain_one(sd_bus_message *m, bool with_ifindex, char **ret) {
1404 _cleanup_free_ char *str = NULL;
1405 int ifindex, route_only, r;
1406 const char *domain;
1407
1408 assert(m);
1409 assert(ret);
1410
1411 if (with_ifindex)
1412 r = sd_bus_message_read(m, "(isb)", &ifindex, &domain, &route_only);
1413 else
1414 r = sd_bus_message_read(m, "(sb)", &domain, &route_only);
1415 if (r <= 0)
1416 return r;
1417
1418 if (with_ifindex && ifindex != 0) {
1419 /* only show the global ones here */
1420 *ret = NULL;
1421 return 1;
1422 }
1423
1424 if (route_only)
b910cc72 1425 str = strjoin("~", domain);
c513bb6e
YW
1426 else
1427 str = strdup(domain);
1428 if (!str)
1429 return -ENOMEM;
1430
1431 *ret = TAKE_PTR(str);
1432
1433 return 1;
1434}
1435
be371fe0 1436static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
99534007 1437 char ***l = ASSERT_PTR(userdata);
be371fe0
LP
1438 int r;
1439
1440 assert(bus);
1441 assert(member);
1442 assert(m);
be371fe0
LP
1443
1444 r = sd_bus_message_enter_container(m, 'a', "(sb)");
1445 if (r < 0)
1446 return r;
1447
1448 for (;;) {
6abdec98 1449 _cleanup_free_ char *pretty = NULL;
be371fe0 1450
c513bb6e 1451 r = read_domain_one(m, false, &pretty);
be371fe0
LP
1452 if (r < 0)
1453 return r;
1454 if (r == 0)
1455 break;
1456
c513bb6e
YW
1457 if (isempty(pretty))
1458 continue;
be371fe0 1459
6abdec98 1460 r = strv_consume(l, TAKE_PTR(pretty));
be371fe0
LP
1461 if (r < 0)
1462 return r;
1463 }
1464
1465 r = sd_bus_message_exit_container(m);
1466 if (r < 0)
1467 return r;
1468
af781878
ZJS
1469 strv_sort(*l);
1470
be371fe0
LP
1471 return 0;
1472}
1473
a7a4c60a 1474static int status_print_strv_ifindex(int ifindex, const char *ifname, char **p) {
7c502303
ZJS
1475 const unsigned indent = strlen("Global: "); /* Use the same indentation everywhere to make things nice */
1476 int pos1, pos2;
1477
1478 if (ifname)
1479 printf("%s%nLink %i (%s)%n%s:", ansi_highlight(), &pos1, ifindex, ifname, &pos2, ansi_normal());
1480 else
1481 printf("%s%nGlobal%n%s:", ansi_highlight(), &pos1, &pos2, ansi_normal());
1482
1483 size_t cols = columns(), position = pos2 - pos1 + 2;
a7a4c60a 1484
7c502303
ZJS
1485 STRV_FOREACH(i, p) {
1486 size_t our_len = utf8_console_width(*i); /* This returns -1 on invalid utf-8 (which shouldn't happen).
1487 * If that happens, we'll just print one item per line. */
a7a4c60a 1488
b0e3d799 1489 if (position <= indent || size_add(size_add(position, 1), our_len) < cols) {
7c502303 1490 printf(" %s", *i);
b0e3d799 1491 position = size_add(size_add(position, 1), our_len);
7c502303 1492 } else {
f996072f 1493 printf("\n%*s%s", (int) indent, "", *i);
b0e3d799 1494 position = size_add(our_len, indent);
7c502303
ZJS
1495 }
1496 }
a7a4c60a
YW
1497
1498 printf("\n");
1499
1500 return 0;
1501}
1502
7c502303
ZJS
1503static int status_print_strv_global(char **p) {
1504 return status_print_strv_ifindex(0, NULL, p);
1505}
1506
80b8c3d7 1507typedef struct LinkInfo {
906119c0
YW
1508 uint64_t scopes_mask;
1509 const char *llmnr;
1510 const char *mdns;
1511 const char *dns_over_tls;
1512 const char *dnssec;
1513 char *current_dns;
a747e71c 1514 char *current_dns_ex;
906119c0 1515 char **dns;
a747e71c 1516 char **dns_ex;
906119c0
YW
1517 char **domains;
1518 char **ntas;
1519 bool dnssec_supported;
f2fd3cdb 1520 bool default_route;
80b8c3d7 1521} LinkInfo;
906119c0 1522
80b8c3d7
ZJS
1523typedef struct GlobalInfo {
1524 char *current_dns;
1525 char *current_dns_ex;
1526 char **dns;
1527 char **dns_ex;
1528 char **fallback_dns;
1529 char **fallback_dns_ex;
1530 char **domains;
1531 char **ntas;
1532 const char *llmnr;
1533 const char *mdns;
1534 const char *dns_over_tls;
1535 const char *dnssec;
1536 const char *resolv_conf_mode;
1537 bool dnssec_supported;
1538} GlobalInfo;
1539
1540static void link_info_clear(LinkInfo *p) {
1541 free(p->current_dns);
1542 free(p->current_dns_ex);
1543 strv_free(p->dns);
1544 strv_free(p->dns_ex);
1545 strv_free(p->domains);
1546 strv_free(p->ntas);
1547}
1548
1549static void global_info_clear(GlobalInfo *p) {
906119c0 1550 free(p->current_dns);
a747e71c 1551 free(p->current_dns_ex);
906119c0 1552 strv_free(p->dns);
a747e71c 1553 strv_free(p->dns_ex);
80b8c3d7
ZJS
1554 strv_free(p->fallback_dns);
1555 strv_free(p->fallback_dns_ex);
906119c0
YW
1556 strv_free(p->domains);
1557 strv_free(p->ntas);
1558}
be371fe0 1559
37a50123 1560static int dump_list(Table *table, const char *field, char * const *l) {
29e15e98
YW
1561 int r;
1562
1563 if (strv_isempty(l))
1564 return 0;
1565
1566 r = table_add_many(table,
37a50123 1567 TABLE_FIELD, field,
f08a64c5 1568 TABLE_STRV_WRAPPED, l);
29e15e98
YW
1569 if (r < 0)
1570 return table_log_add_error(r);
1571
1572 return 0;
1573}
1574
fe37e5a5
ZJS
1575static int strv_extend_extended_bool(char ***strv, const char *name, const char *value) {
1576 int r;
1577
1578 if (value) {
1579 r = parse_boolean(value);
1580 if (r >= 0)
1581 return strv_extendf(strv, "%s%s", plus_minus(r), name);
1582 }
1583
1584 return strv_extendf(strv, "%s=%s", name, value ?: "???");
1585}
1586
7d1e1afe 1587static char** link_protocol_status(const LinkInfo *info) {
fe37e5a5
ZJS
1588 _cleanup_strv_free_ char **s = NULL;
1589
1590 if (strv_extendf(&s, "%sDefaultRoute", plus_minus(info->default_route)) < 0)
1591 return NULL;
1592
1593 if (strv_extend_extended_bool(&s, "LLMNR", info->llmnr) < 0)
1594 return NULL;
1595
1596 if (strv_extend_extended_bool(&s, "mDNS", info->mdns) < 0)
1597 return NULL;
1598
1599 if (strv_extend_extended_bool(&s, "DNSOverTLS", info->dns_over_tls) < 0)
1600 return NULL;
1601
1602 if (strv_extendf(&s, "DNSSEC=%s/%s",
1603 info->dnssec ?: "???",
1604 info->dnssec_supported ? "supported" : "unsupported") < 0)
1605 return NULL;
1606
7d1e1afe 1607 return TAKE_PTR(s);
fe37e5a5
ZJS
1608}
1609
7d1e1afe 1610static char** global_protocol_status(const GlobalInfo *info) {
fe37e5a5
ZJS
1611 _cleanup_strv_free_ char **s = NULL;
1612
1613 if (strv_extend_extended_bool(&s, "LLMNR", info->llmnr) < 0)
1614 return NULL;
1615
1616 if (strv_extend_extended_bool(&s, "mDNS", info->mdns) < 0)
1617 return NULL;
1618
1619 if (strv_extend_extended_bool(&s, "DNSOverTLS", info->dns_over_tls) < 0)
1620 return NULL;
1621
1622 if (strv_extendf(&s, "DNSSEC=%s/%s",
1623 info->dnssec ?: "???",
1624 info->dnssec_supported ? "supported" : "unsupported") < 0)
1625 return NULL;
1626
7d1e1afe 1627 return TAKE_PTR(s);
fe37e5a5
ZJS
1628}
1629
906119c0 1630static int status_ifindex(sd_bus *bus, int ifindex, const char *name, StatusMode mode, bool *empty_line) {
be371fe0 1631 static const struct bus_properties_map property_map[] = {
80b8c3d7
ZJS
1632 { "ScopesMask", "t", NULL, offsetof(LinkInfo, scopes_mask) },
1633 { "DNS", "a(iay)", map_link_dns_servers, offsetof(LinkInfo, dns) },
1634 { "DNSEx", "a(iayqs)", map_link_dns_servers_ex, offsetof(LinkInfo, dns_ex) },
1635 { "CurrentDNSServer", "(iay)", map_link_current_dns_server, offsetof(LinkInfo, current_dns) },
1636 { "CurrentDNSServerEx", "(iayqs)", map_link_current_dns_server_ex, offsetof(LinkInfo, current_dns_ex) },
1637 { "Domains", "a(sb)", map_link_domains, offsetof(LinkInfo, domains) },
1638 { "DefaultRoute", "b", NULL, offsetof(LinkInfo, default_route) },
1639 { "LLMNR", "s", NULL, offsetof(LinkInfo, llmnr) },
1640 { "MulticastDNS", "s", NULL, offsetof(LinkInfo, mdns) },
1641 { "DNSOverTLS", "s", NULL, offsetof(LinkInfo, dns_over_tls) },
1642 { "DNSSEC", "s", NULL, offsetof(LinkInfo, dnssec) },
af781878 1643 { "DNSSECNegativeTrustAnchors", "as", bus_map_strv_sort, offsetof(LinkInfo, ntas) },
80b8c3d7 1644 { "DNSSECSupported", "b", NULL, offsetof(LinkInfo, dnssec_supported) },
be371fe0
LP
1645 {}
1646 };
f9e0eefc 1647 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f37f8a61 1648 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
80b8c3d7 1649 _cleanup_(link_info_clear) LinkInfo link_info = {};
29e15e98 1650 _cleanup_(table_unrefp) Table *table = NULL;
957d9df3 1651 _cleanup_free_ char *p = NULL;
01afd0f7 1652 char ifi[DECIMAL_STR_MAX(int)], ifname[IF_NAMESIZE];
be371fe0
LP
1653 int r;
1654
1655 assert(bus);
1656 assert(ifindex > 0);
be371fe0
LP
1657
1658 if (!name) {
01afd0f7
YW
1659 r = format_ifname(ifindex, ifname);
1660 if (r < 0)
1661 return log_error_errno(r, "Failed to resolve interface name for %i: %m", ifindex);
be371fe0
LP
1662
1663 name = ifname;
1664 }
1665
957d9df3 1666 xsprintf(ifi, "%i", ifindex);
be371fe0
LP
1667 r = sd_bus_path_encode("/org/freedesktop/resolve1/link", ifi, &p);
1668 if (r < 0)
1669 return log_oom();
1670
1671 r = bus_map_all_properties(bus,
1672 "org.freedesktop.resolve1",
1673 p,
1674 property_map,
a7e4861c 1675 BUS_MAP_BOOLEAN_AS_BOOL,
f9e0eefc 1676 &error,
f37f8a61 1677 &m,
be371fe0 1678 &link_info);
906119c0
YW
1679 if (r < 0)
1680 return log_error_errno(r, "Failed to get link data for %i: %s", ifindex, bus_error_message(&error, r));
be371fe0 1681
384c2c32 1682 pager_open(arg_pager_flags);
be371fe0 1683
906119c0 1684 if (mode == STATUS_DNS)
a747e71c 1685 return status_print_strv_ifindex(ifindex, name, link_info.dns_ex ?: link_info.dns);
a7a4c60a 1686
906119c0
YW
1687 if (mode == STATUS_DOMAIN)
1688 return status_print_strv_ifindex(ifindex, name, link_info.domains);
a7a4c60a 1689
906119c0
YW
1690 if (mode == STATUS_NTA)
1691 return status_print_strv_ifindex(ifindex, name, link_info.ntas);
a7a4c60a 1692
f2fd3cdb
LP
1693 if (mode == STATUS_DEFAULT_ROUTE) {
1694 printf("%sLink %i (%s)%s: %s\n",
1695 ansi_highlight(), ifindex, name, ansi_normal(),
1696 yes_no(link_info.default_route));
1697
1698 return 0;
1699 }
1700
a7a4c60a
YW
1701 if (mode == STATUS_LLMNR) {
1702 printf("%sLink %i (%s)%s: %s\n",
1703 ansi_highlight(), ifindex, name, ansi_normal(),
1704 strna(link_info.llmnr));
1705
906119c0 1706 return 0;
a7a4c60a
YW
1707 }
1708
1709 if (mode == STATUS_MDNS) {
1710 printf("%sLink %i (%s)%s: %s\n",
1711 ansi_highlight(), ifindex, name, ansi_normal(),
1712 strna(link_info.mdns));
1713
906119c0 1714 return 0;
a7a4c60a
YW
1715 }
1716
d050561a
IT
1717 if (mode == STATUS_PRIVATE) {
1718 printf("%sLink %i (%s)%s: %s\n",
1719 ansi_highlight(), ifindex, name, ansi_normal(),
c9299be2 1720 strna(link_info.dns_over_tls));
d050561a 1721
906119c0 1722 return 0;
d050561a
IT
1723 }
1724
a7a4c60a
YW
1725 if (mode == STATUS_DNSSEC) {
1726 printf("%sLink %i (%s)%s: %s\n",
1727 ansi_highlight(), ifindex, name, ansi_normal(),
1728 strna(link_info.dnssec));
1729
906119c0 1730 return 0;
a7a4c60a
YW
1731 }
1732
1733 if (empty_line && *empty_line)
be371fe0
LP
1734 fputc('\n', stdout);
1735
1736 printf("%sLink %i (%s)%s\n",
1737 ansi_highlight(), ifindex, name, ansi_normal());
1738
37a50123 1739 table = table_new_vertical();
29e15e98
YW
1740 if (!table)
1741 return log_oom();
1742
29e15e98 1743 r = table_add_many(table,
37a50123
LP
1744 TABLE_FIELD, "Current Scopes",
1745 TABLE_SET_MINIMUM_WIDTH, 19);
29e15e98
YW
1746 if (r < 0)
1747 return table_log_add_error(r);
1748
be371fe0 1749 if (link_info.scopes_mask == 0)
29e15e98
YW
1750 r = table_add_cell(table, NULL, TABLE_STRING, "none");
1751 else {
1752 _cleanup_free_ char *buf = NULL;
1753 size_t len;
1754
1755 if (asprintf(&buf, "%s%s%s%s%s",
1756 link_info.scopes_mask & SD_RESOLVED_DNS ? "DNS " : "",
1757 link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV4 ? "LLMNR/IPv4 " : "",
1758 link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV6 ? "LLMNR/IPv6 " : "",
1759 link_info.scopes_mask & SD_RESOLVED_MDNS_IPV4 ? "mDNS/IPv4 " : "",
1760 link_info.scopes_mask & SD_RESOLVED_MDNS_IPV6 ? "mDNS/IPv6 " : "") < 0)
1761 return log_oom();
be371fe0 1762
29e15e98
YW
1763 len = strlen(buf);
1764 assert(len > 0);
1765 buf[len - 1] = '\0';
be371fe0 1766
29e15e98
YW
1767 r = table_add_cell(table, NULL, TABLE_STRING, buf);
1768 }
1769 if (r < 0)
1770 return table_log_add_error(r);
1771
7d1e1afe 1772 _cleanup_strv_free_ char **pstatus = link_protocol_status(&link_info);
fe37e5a5
ZJS
1773 if (!pstatus)
1774 return log_oom();
1775
29e15e98 1776 r = table_add_many(table,
37a50123 1777 TABLE_FIELD, "Protocols",
7d1e1afe 1778 TABLE_STRV_WRAPPED, pstatus);
29e15e98
YW
1779 if (r < 0)
1780 return table_log_add_error(r);
1781
1782 if (link_info.current_dns) {
1783 r = table_add_many(table,
37a50123 1784 TABLE_FIELD, "Current DNS Server",
a747e71c 1785 TABLE_STRING, link_info.current_dns_ex ?: link_info.current_dns);
29e15e98
YW
1786 if (r < 0)
1787 return table_log_add_error(r);
be371fe0
LP
1788 }
1789
37a50123 1790 r = dump_list(table, "DNS Servers", link_info.dns_ex ?: link_info.dns);
29e15e98
YW
1791 if (r < 0)
1792 return r;
1793
37a50123 1794 r = dump_list(table, "DNS Domain", link_info.domains);
29e15e98
YW
1795 if (r < 0)
1796 return r;
1797
29e15e98
YW
1798 r = table_print(table, NULL);
1799 if (r < 0)
4b6607d9 1800 return table_log_print_error(r);
29e15e98 1801
a7a4c60a
YW
1802 if (empty_line)
1803 *empty_line = true;
be371fe0 1804
906119c0 1805 return 0;
be371fe0
LP
1806}
1807
5707fb12
LP
1808static int map_global_dns_servers_internal(
1809 sd_bus *bus,
1810 const char *member,
1811 sd_bus_message *m,
1812 sd_bus_error *error,
1813 void *userdata,
1814 bool extended) {
1815
99534007 1816 char ***l = ASSERT_PTR(userdata);
be371fe0
LP
1817 int r;
1818
1819 assert(bus);
1820 assert(member);
1821 assert(m);
be371fe0 1822
a747e71c 1823 r = sd_bus_message_enter_container(m, 'a', extended ? "(iiayqs)" : "(iiay)");
be371fe0
LP
1824 if (r < 0)
1825 return r;
1826
1827 for (;;) {
6abdec98 1828 _cleanup_free_ char *pretty = NULL;
be371fe0 1829
889a1b9f 1830 r = read_dns_server_one(m, /* with_ifindex= */ true, extended, /* only_global= */ true, &pretty);
be371fe0
LP
1831 if (r < 0)
1832 return r;
1833 if (r == 0)
1834 break;
1835
58f48a56 1836 if (isempty(pretty))
be371fe0
LP
1837 continue;
1838
6abdec98 1839 r = strv_consume(l, TAKE_PTR(pretty));
be371fe0
LP
1840 if (r < 0)
1841 return r;
1842 }
1843
1844 r = sd_bus_message_exit_container(m);
1845 if (r < 0)
1846 return r;
1847
1848 return 0;
1849}
1850
a747e71c 1851static int map_global_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
5707fb12 1852 return map_global_dns_servers_internal(bus, member, m, error, userdata, /* extended= */ false);
a747e71c
YW
1853}
1854
1855static int map_global_dns_servers_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
5707fb12 1856 return map_global_dns_servers_internal(bus, member, m, error, userdata, /* extended= */ true);
a747e71c
YW
1857}
1858
446c6415 1859static int map_global_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
889a1b9f 1860 return read_dns_server_one(m, /* with_ifindex= */ true, /* extended= */ false, /* only_global= */ true, userdata);
a747e71c
YW
1861}
1862
1863static int map_global_current_dns_server_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
889a1b9f 1864 return read_dns_server_one(m, /* with_ifindex= */ true, /* extended= */ true, /* only_global= */ true, userdata);
446c6415
YW
1865}
1866
be371fe0 1867static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
99534007 1868 char ***l = ASSERT_PTR(userdata);
be371fe0
LP
1869 int r;
1870
1871 assert(bus);
1872 assert(member);
1873 assert(m);
be371fe0
LP
1874
1875 r = sd_bus_message_enter_container(m, 'a', "(isb)");
1876 if (r < 0)
1877 return r;
1878
1879 for (;;) {
6abdec98 1880 _cleanup_free_ char *pretty = NULL;
be371fe0 1881
c513bb6e 1882 r = read_domain_one(m, true, &pretty);
be371fe0
LP
1883 if (r < 0)
1884 return r;
1885 if (r == 0)
1886 break;
1887
c513bb6e 1888 if (isempty(pretty))
be371fe0
LP
1889 continue;
1890
6abdec98 1891 r = strv_consume(l, TAKE_PTR(pretty));
be371fe0
LP
1892 if (r < 0)
1893 return r;
1894 }
1895
1896 r = sd_bus_message_exit_container(m);
1897 if (r < 0)
1898 return r;
1899
af781878 1900 strv_sort(*l);
a7a4c60a
YW
1901
1902 return 0;
1903}
1904
906119c0 1905static int status_global(sd_bus *bus, StatusMode mode, bool *empty_line) {
be371fe0 1906 static const struct bus_properties_map property_map[] = {
80b8c3d7
ZJS
1907 { "DNS", "a(iiay)", map_global_dns_servers, offsetof(GlobalInfo, dns) },
1908 { "DNSEx", "a(iiayqs)", map_global_dns_servers_ex, offsetof(GlobalInfo, dns_ex) },
1909 { "FallbackDNS", "a(iiay)", map_global_dns_servers, offsetof(GlobalInfo, fallback_dns) },
1910 { "FallbackDNSEx", "a(iiayqs)", map_global_dns_servers_ex, offsetof(GlobalInfo, fallback_dns_ex) },
1911 { "CurrentDNSServer", "(iiay)", map_global_current_dns_server, offsetof(GlobalInfo, current_dns) },
1912 { "CurrentDNSServerEx", "(iiayqs)", map_global_current_dns_server_ex, offsetof(GlobalInfo, current_dns_ex) },
1913 { "Domains", "a(isb)", map_global_domains, offsetof(GlobalInfo, domains) },
af781878 1914 { "DNSSECNegativeTrustAnchors", "as", bus_map_strv_sort, offsetof(GlobalInfo, ntas) },
80b8c3d7
ZJS
1915 { "LLMNR", "s", NULL, offsetof(GlobalInfo, llmnr) },
1916 { "MulticastDNS", "s", NULL, offsetof(GlobalInfo, mdns) },
1917 { "DNSOverTLS", "s", NULL, offsetof(GlobalInfo, dns_over_tls) },
1918 { "DNSSEC", "s", NULL, offsetof(GlobalInfo, dnssec) },
1919 { "DNSSECSupported", "b", NULL, offsetof(GlobalInfo, dnssec_supported) },
1920 { "ResolvConfMode", "s", NULL, offsetof(GlobalInfo, resolv_conf_mode) },
be371fe0
LP
1921 {}
1922 };
f9e0eefc 1923 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f37f8a61 1924 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
80b8c3d7 1925 _cleanup_(global_info_clear) GlobalInfo global_info = {};
29e15e98 1926 _cleanup_(table_unrefp) Table *table = NULL;
be371fe0
LP
1927 int r;
1928
1929 assert(bus);
1930 assert(empty_line);
1931
1932 r = bus_map_all_properties(bus,
1933 "org.freedesktop.resolve1",
1934 "/org/freedesktop/resolve1",
1935 property_map,
a7e4861c 1936 BUS_MAP_BOOLEAN_AS_BOOL,
f9e0eefc 1937 &error,
f37f8a61 1938 &m,
be371fe0 1939 &global_info);
906119c0
YW
1940 if (r < 0)
1941 return log_error_errno(r, "Failed to get global data: %s", bus_error_message(&error, r));
be371fe0 1942
384c2c32 1943 pager_open(arg_pager_flags);
be371fe0 1944
906119c0 1945 if (mode == STATUS_DNS)
a747e71c 1946 return status_print_strv_global(global_info.dns_ex ?: global_info.dns);
a7a4c60a 1947
906119c0
YW
1948 if (mode == STATUS_DOMAIN)
1949 return status_print_strv_global(global_info.domains);
a7a4c60a 1950
906119c0
YW
1951 if (mode == STATUS_NTA)
1952 return status_print_strv_global(global_info.ntas);
a7a4c60a
YW
1953
1954 if (mode == STATUS_LLMNR) {
1955 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1956 strna(global_info.llmnr));
1957
906119c0 1958 return 0;
a7a4c60a
YW
1959 }
1960
1961 if (mode == STATUS_MDNS) {
1962 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1963 strna(global_info.mdns));
1964
906119c0 1965 return 0;
a7a4c60a
YW
1966 }
1967
d050561a
IT
1968 if (mode == STATUS_PRIVATE) {
1969 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
c9299be2 1970 strna(global_info.dns_over_tls));
d050561a 1971
906119c0 1972 return 0;
d050561a
IT
1973 }
1974
a7a4c60a
YW
1975 if (mode == STATUS_DNSSEC) {
1976 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1977 strna(global_info.dnssec));
1978
906119c0 1979 return 0;
a7a4c60a
YW
1980 }
1981
be371fe0 1982 printf("%sGlobal%s\n", ansi_highlight(), ansi_normal());
11d6e9e9 1983
37a50123 1984 table = table_new_vertical();
29e15e98
YW
1985 if (!table)
1986 return log_oom();
be371fe0 1987
7d1e1afe 1988 _cleanup_strv_free_ char **pstatus = global_protocol_status(&global_info);
fe37e5a5
ZJS
1989 if (!pstatus)
1990 return log_oom();
1991
29e15e98 1992 r = table_add_many(table,
37a50123
LP
1993 TABLE_FIELD, "Protocols",
1994 TABLE_SET_MINIMUM_WIDTH, 19,
7d1e1afe 1995 TABLE_STRV_WRAPPED, pstatus);
29e15e98
YW
1996 if (r < 0)
1997 return table_log_add_error(r);
1998
147a5046
LP
1999 if (global_info.resolv_conf_mode) {
2000 r = table_add_many(table,
37a50123 2001 TABLE_FIELD, "resolv.conf mode",
147a5046
LP
2002 TABLE_STRING, global_info.resolv_conf_mode);
2003 if (r < 0)
2004 return table_log_add_error(r);
2005 }
2006
29e15e98
YW
2007 if (global_info.current_dns) {
2008 r = table_add_many(table,
37a50123 2009 TABLE_FIELD, "Current DNS Server",
a747e71c 2010 TABLE_STRING, global_info.current_dns_ex ?: global_info.current_dns);
29e15e98
YW
2011 if (r < 0)
2012 return table_log_add_error(r);
4b320ac5
YW
2013 }
2014
ef503f1c 2015 r = dump_list(table, "DNS Servers", global_info.dns_ex ?: global_info.dns);
29e15e98
YW
2016 if (r < 0)
2017 return r;
2018
ef503f1c 2019 r = dump_list(table, "Fallback DNS Servers", global_info.fallback_dns_ex ?: global_info.fallback_dns);
29e15e98
YW
2020 if (r < 0)
2021 return r;
2022
ef503f1c 2023 r = dump_list(table, "DNS Domain", global_info.domains);
29e15e98
YW
2024 if (r < 0)
2025 return r;
be371fe0 2026
29e15e98
YW
2027 r = table_print(table, NULL);
2028 if (r < 0)
4b6607d9 2029 return table_log_print_error(r);
be371fe0
LP
2030
2031 *empty_line = true;
2032
906119c0 2033 return 0;
be371fe0
LP
2034}
2035
a7a4c60a 2036static int status_all(sd_bus *bus, StatusMode mode) {
be371fe0
LP
2037 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2038 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
96ace31d 2039 bool empty_line = false;
be371fe0
LP
2040 int r;
2041
2042 assert(bus);
2043
a7a4c60a 2044 r = status_global(bus, mode, &empty_line);
be371fe0
LP
2045 if (r < 0)
2046 return r;
2047
2048 r = sd_netlink_open(&rtnl);
2049 if (r < 0)
2050 return log_error_errno(r, "Failed to connect to netlink: %m");
2051
2052 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
2053 if (r < 0)
2054 return rtnl_log_create_error(r);
2055
24c0f385 2056 r = sd_netlink_message_set_request_dump(req, true);
be371fe0
LP
2057 if (r < 0)
2058 return rtnl_log_create_error(r);
2059
2060 r = sd_netlink_call(rtnl, req, 0, &reply);
2061 if (r < 0)
2062 return log_error_errno(r, "Failed to enumerate links: %m");
2063
eb107675 2064 _cleanup_free_ InterfaceInfo *infos = NULL;
319a4f4b 2065 size_t n_infos = 0;
eb107675 2066
c9d243cd 2067 for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) {
be371fe0 2068 const char *name;
eb107675 2069 int ifindex;
be371fe0
LP
2070 uint16_t type;
2071
eb107675
ZJS
2072 r = sd_netlink_message_get_type(i, &type);
2073 if (r < 0)
2074 return rtnl_log_parse_error(r);
be371fe0
LP
2075
2076 if (type != RTM_NEWLINK)
2077 continue;
2078
eb107675
ZJS
2079 r = sd_rtnl_message_link_get_ifindex(i, &ifindex);
2080 if (r < 0)
2081 return rtnl_log_parse_error(r);
be371fe0
LP
2082
2083 if (ifindex == LOOPBACK_IFINDEX)
2084 continue;
2085
eb107675
ZJS
2086 r = sd_netlink_message_read_string(i, IFLA_IFNAME, &name);
2087 if (r < 0)
2088 return rtnl_log_parse_error(r);
be371fe0 2089
319a4f4b 2090 if (!GREEDY_REALLOC(infos, n_infos + 1))
eb107675
ZJS
2091 return log_oom();
2092
2093 infos[n_infos++] = (InterfaceInfo) { ifindex, name };
2094 }
2095
2096 typesafe_qsort(infos, n_infos, interface_info_compare);
2097
2098 r = 0;
2099 for (size_t i = 0; i < n_infos; i++) {
2100 int q = status_ifindex(bus, infos[i].index, infos[i].name, mode, &empty_line);
be371fe0
LP
2101 if (q < 0 && r >= 0)
2102 r = q;
2103 }
2104
2105 return r;
2106}
2107
a7a4c60a
YW
2108static int verb_status(int argc, char **argv, void *userdata) {
2109 sd_bus *bus = userdata;
957d9df3 2110 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
597da51b 2111 int r = 0;
14965b94 2112
a7a4c60a 2113 if (argc > 1) {
a7a4c60a 2114 bool empty_line = false;
14965b94 2115
a7a4c60a 2116 STRV_FOREACH(ifname, argv + 1) {
597da51b 2117 int ifindex, q;
14965b94 2118
f6e49154 2119 ifindex = rtnl_resolve_interface(&rtnl, *ifname);
597da51b 2120 if (ifindex < 0) {
d308bb99 2121 log_warning_errno(ifindex, "Failed to resolve interface \"%s\", ignoring: %m", *ifname);
a7a4c60a 2122 continue;
8e5385b4 2123 }
14965b94 2124
a7a4c60a
YW
2125 q = status_ifindex(bus, ifindex, NULL, STATUS_ALL, &empty_line);
2126 if (q < 0)
2127 r = q;
2128 }
2129 } else
2130 r = status_all(bus, STATUS_ALL);
14965b94 2131
a7a4c60a
YW
2132 return r;
2133}
14965b94 2134
b1881e83 2135static int call_dns(sd_bus *bus, char **dns, const BusLocator *locator, sd_bus_error *error, bool extended) {
a7a4c60a 2136 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
a661dc36 2137 int r;
14965b94 2138
b1881e83 2139 r = bus_message_new_method_call(bus, &req, locator, extended ? "SetLinkDNSEx" : "SetLinkDNS");
a7a4c60a
YW
2140 if (r < 0)
2141 return bus_log_create_error(r);
14965b94 2142
a661dc36 2143 r = sd_bus_message_append(req, "i", arg_ifindex);
a7a4c60a
YW
2144 if (r < 0)
2145 return bus_log_create_error(r);
14965b94 2146
b1881e83 2147 r = sd_bus_message_open_container(req, 'a', extended ? "(iayqs)" : "(iay)");
a7a4c60a
YW
2148 if (r < 0)
2149 return bus_log_create_error(r);
14965b94 2150
06c28aa0
FB
2151 /* If only argument is the empty string, then call SetLinkDNS() with an
2152 * empty list, which will clear the list of domains for an interface. */
65856bf2
YW
2153 if (!strv_equal(dns, STRV_MAKE("")))
2154 STRV_FOREACH(p, dns) {
b1881e83 2155 _cleanup_free_ char *name = NULL;
06c28aa0 2156 struct in_addr_data data;
b1881e83
YW
2157 uint16_t port;
2158 int ifindex;
14965b94 2159
b1881e83 2160 r = in_addr_port_ifindex_name_from_string_auto(*p, &data.family, &data.address, &port, &ifindex, &name);
06c28aa0
FB
2161 if (r < 0)
2162 return log_error_errno(r, "Failed to parse DNS server address: %s", *p);
14965b94 2163
b1881e83
YW
2164 if (ifindex != 0 && ifindex != arg_ifindex)
2165 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid ifindex: %i", ifindex);
2166
2167 r = sd_bus_message_open_container(req, 'r', extended ? "iayqs" : "iay");
06c28aa0
FB
2168 if (r < 0)
2169 return bus_log_create_error(r);
14965b94 2170
06c28aa0
FB
2171 r = sd_bus_message_append(req, "i", data.family);
2172 if (r < 0)
2173 return bus_log_create_error(r);
14965b94 2174
06c28aa0
FB
2175 r = sd_bus_message_append_array(req, 'y', &data.address, FAMILY_ADDRESS_SIZE(data.family));
2176 if (r < 0)
2177 return bus_log_create_error(r);
a7a4c60a 2178
b1881e83
YW
2179 if (extended) {
2180 r = sd_bus_message_append(req, "q", port);
2181 if (r < 0)
2182 return bus_log_create_error(r);
2183
2184 r = sd_bus_message_append(req, "s", name);
2185 if (r < 0)
2186 return bus_log_create_error(r);
2187 }
2188
06c28aa0
FB
2189 r = sd_bus_message_close_container(req);
2190 if (r < 0)
2191 return bus_log_create_error(r);
2192 }
14965b94 2193
a7a4c60a
YW
2194 r = sd_bus_message_close_container(req);
2195 if (r < 0)
2196 return bus_log_create_error(r);
2197
b1881e83 2198 r = sd_bus_call(bus, req, 0, error, NULL);
f527c6fa
YW
2199 if (r < 0 && extended && sd_bus_error_has_name(error, SD_BUS_ERROR_UNKNOWN_METHOD)) {
2200 sd_bus_error_free(error);
b1881e83 2201 return call_dns(bus, dns, locator, error, false);
f527c6fa 2202 }
b1881e83 2203 return r;
a7a4c60a
YW
2204}
2205
65856bf2 2206static int verb_dns(int argc, char **argv, void *userdata) {
a7a4c60a 2207 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
99534007 2208 sd_bus *bus = ASSERT_PTR(userdata);
a661dc36 2209 int r;
a7a4c60a 2210
d1293049 2211 if (argc >= 2) {
df87a53d 2212 r = ifname_mangle(argv[1]);
d1293049
LP
2213 if (r < 0)
2214 return r;
2215 }
a7a4c60a 2216
d1293049 2217 if (arg_ifindex <= 0)
65856bf2 2218 return status_all(bus, STATUS_DNS);
14965b94 2219
d1293049 2220 if (argc < 3)
65856bf2
YW
2221 return status_ifindex(bus, arg_ifindex, NULL, STATUS_DNS, NULL);
2222
b1881e83 2223 r = call_dns(bus, argv + 2, bus_resolve_mgr, &error, true);
65856bf2
YW
2224 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2225 sd_bus_error_free(&error);
2226
b1881e83 2227 r = call_dns(bus, argv + 2, bus_network_mgr, &error, true);
65856bf2
YW
2228 }
2229 if (r < 0) {
2230 if (arg_ifindex_permissive &&
2231 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2232 return 0;
2233
2234 return log_error_errno(r, "Failed to set DNS configuration: %s", bus_error_message(&error, r));
2235 }
2236
2237 return 0;
2238}
2239
d96f9abc 2240static int call_domain(sd_bus *bus, char **domain, const BusLocator *locator, sd_bus_error *error) {
65856bf2 2241 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
65856bf2 2242 int r;
14965b94 2243
d96f9abc 2244 r = bus_message_new_method_call(bus, &req, locator, "SetLinkDomains");
a7a4c60a
YW
2245 if (r < 0)
2246 return bus_log_create_error(r);
14965b94 2247
a661dc36 2248 r = sd_bus_message_append(req, "i", arg_ifindex);
a7a4c60a
YW
2249 if (r < 0)
2250 return bus_log_create_error(r);
14965b94 2251
a7a4c60a
YW
2252 r = sd_bus_message_open_container(req, 'a', "(sb)");
2253 if (r < 0)
2254 return bus_log_create_error(r);
2255
06c28aa0
FB
2256 /* If only argument is the empty string, then call SetLinkDomains() with an
2257 * empty list, which will clear the list of domains for an interface. */
65856bf2
YW
2258 if (!strv_equal(domain, STRV_MAKE("")))
2259 STRV_FOREACH(p, domain) {
06c28aa0 2260 const char *n;
14965b94 2261
06c28aa0 2262 n = **p == '~' ? *p + 1 : *p;
14965b94 2263
06c28aa0
FB
2264 r = dns_name_is_valid(n);
2265 if (r < 0)
2266 return log_error_errno(r, "Failed to validate specified domain %s: %m", n);
d7a0f1f4
FS
2267 if (r == 0)
2268 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2269 "Domain not valid: %s",
2270 n);
a7a4c60a 2271
06c28aa0
FB
2272 r = sd_bus_message_append(req, "(sb)", n, **p == '~');
2273 if (r < 0)
2274 return bus_log_create_error(r);
2275 }
14965b94 2276
a7a4c60a
YW
2277 r = sd_bus_message_close_container(req);
2278 if (r < 0)
2279 return bus_log_create_error(r);
2280
65856bf2
YW
2281 return sd_bus_call(bus, req, 0, error, NULL);
2282}
2283
2284static int verb_domain(int argc, char **argv, void *userdata) {
2285 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
99534007 2286 sd_bus *bus = ASSERT_PTR(userdata);
65856bf2
YW
2287 int r;
2288
65856bf2
YW
2289 if (argc >= 2) {
2290 r = ifname_mangle(argv[1]);
2291 if (r < 0)
2292 return r;
2293 }
2294
2295 if (arg_ifindex <= 0)
2296 return status_all(bus, STATUS_DOMAIN);
2297
2298 if (argc < 3)
2299 return status_ifindex(bus, arg_ifindex, NULL, STATUS_DOMAIN, NULL);
2300
d96f9abc 2301 r = call_domain(bus, argv + 2, bus_resolve_mgr, &error);
65856bf2
YW
2302 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2303 sd_bus_error_free(&error);
14965b94 2304
d96f9abc 2305 r = call_domain(bus, argv + 2, bus_network_mgr, &error);
65856bf2
YW
2306 }
2307 if (r < 0) {
a7a4c60a
YW
2308 if (arg_ifindex_permissive &&
2309 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2310 return 0;
14965b94 2311
a7a4c60a 2312 return log_error_errno(r, "Failed to set domain configuration: %s", bus_error_message(&error, r));
14965b94 2313 }
a7a4c60a
YW
2314
2315 return 0;
14965b94
LP
2316}
2317
f2fd3cdb
LP
2318static int verb_default_route(int argc, char **argv, void *userdata) {
2319 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
99534007 2320 sd_bus *bus = ASSERT_PTR(userdata);
f2fd3cdb
LP
2321 int r, b;
2322
f2fd3cdb
LP
2323 if (argc >= 2) {
2324 r = ifname_mangle(argv[1]);
2325 if (r < 0)
2326 return r;
2327 }
2328
2329 if (arg_ifindex <= 0)
2330 return status_all(bus, STATUS_DEFAULT_ROUTE);
2331
2332 if (argc < 3)
2333 return status_ifindex(bus, arg_ifindex, NULL, STATUS_DEFAULT_ROUTE, NULL);
2334
2335 b = parse_boolean(argv[2]);
2336 if (b < 0)
2337 return log_error_errno(b, "Failed to parse boolean argument: %s", argv[2]);
2338
d96f9abc 2339 r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDefaultRoute", &error, NULL, "ib", arg_ifindex, b);
65856bf2
YW
2340 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2341 sd_bus_error_free(&error);
2342
d96f9abc 2343 r = bus_call_method(bus, bus_network_mgr, "SetLinkDefaultRoute", &error, NULL, "ib", arg_ifindex, b);
65856bf2 2344 }
f2fd3cdb 2345 if (r < 0) {
f2fd3cdb
LP
2346 if (arg_ifindex_permissive &&
2347 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2348 return 0;
2349
2350 return log_error_errno(r, "Failed to set default route configuration: %s", bus_error_message(&error, r));
2351 }
2352
2353 return 0;
2354}
2355
a7a4c60a 2356static int verb_llmnr(int argc, char **argv, void *userdata) {
14965b94 2357 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
c38a03df
YW
2358 _cleanup_free_ char *global_llmnr_support_str = NULL;
2359 ResolveSupport global_llmnr_support, llmnr_support;
99534007 2360 sd_bus *bus = ASSERT_PTR(userdata);
a661dc36 2361 int r;
14965b94 2362
d1293049 2363 if (argc >= 2) {
df87a53d 2364 r = ifname_mangle(argv[1]);
d1293049
LP
2365 if (r < 0)
2366 return r;
2367 }
a7a4c60a 2368
d1293049
LP
2369 if (arg_ifindex <= 0)
2370 return status_all(bus, STATUS_LLMNR);
a7a4c60a 2371
d1293049 2372 if (argc < 3)
a661dc36 2373 return status_ifindex(bus, arg_ifindex, NULL, STATUS_LLMNR, NULL);
a7a4c60a 2374
c38a03df
YW
2375 llmnr_support = resolve_support_from_string(argv[2]);
2376 if (llmnr_support < 0)
2377 return log_error_errno(llmnr_support, "Invalid LLMNR setting: %s", argv[2]);
2378
2379 r = bus_get_property_string(bus, bus_resolve_mgr, "LLMNR", &error, &global_llmnr_support_str);
2380 if (r < 0)
2381 return log_error_errno(r, "Failed to get the global LLMNR support state: %s", bus_error_message(&error, r));
2382
2383 global_llmnr_support = resolve_support_from_string(global_llmnr_support_str);
2384 if (global_llmnr_support < 0)
2385 return log_error_errno(global_llmnr_support, "Received invalid global LLMNR setting: %s", global_llmnr_support_str);
2386
2387 if (global_llmnr_support < llmnr_support)
2388 log_warning("Setting LLMNR support level \"%s\" for \"%s\", but the global support level is \"%s\".",
2389 argv[2], arg_ifname, global_llmnr_support_str);
2390
d96f9abc 2391 r = bus_call_method(bus, bus_resolve_mgr, "SetLinkLLMNR", &error, NULL, "is", arg_ifindex, argv[2]);
65856bf2
YW
2392 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2393 sd_bus_error_free(&error);
2394
d96f9abc 2395 r = bus_call_method(bus, bus_network_mgr, "SetLinkLLMNR", &error, NULL, "is", arg_ifindex, argv[2]);
65856bf2 2396 }
088c1363
LP
2397 if (r < 0) {
2398 if (arg_ifindex_permissive &&
2399 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2400 return 0;
2401
a7a4c60a 2402 return log_error_errno(r, "Failed to set LLMNR configuration: %s", bus_error_message(&error, r));
088c1363 2403 }
14965b94
LP
2404
2405 return 0;
2406}
2407
a7a4c60a
YW
2408static int verb_mdns(int argc, char **argv, void *userdata) {
2409 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
c38a03df
YW
2410 _cleanup_free_ char *global_mdns_support_str = NULL;
2411 ResolveSupport global_mdns_support, mdns_support;
99534007 2412 sd_bus *bus = ASSERT_PTR(userdata);
a661dc36 2413 int r;
ba82da3b 2414
d1293049 2415 if (argc >= 2) {
df87a53d 2416 r = ifname_mangle(argv[1]);
d1293049
LP
2417 if (r < 0)
2418 return r;
2419 }
a7a4c60a 2420
d1293049
LP
2421 if (arg_ifindex <= 0)
2422 return status_all(bus, STATUS_MDNS);
a7a4c60a 2423
d1293049 2424 if (argc < 3)
a661dc36 2425 return status_ifindex(bus, arg_ifindex, NULL, STATUS_MDNS, NULL);
a7a4c60a 2426
c38a03df
YW
2427 mdns_support = resolve_support_from_string(argv[2]);
2428 if (mdns_support < 0)
2429 return log_error_errno(mdns_support, "Invalid mDNS setting: %s", argv[2]);
2430
2431 r = bus_get_property_string(bus, bus_resolve_mgr, "MulticastDNS", &error, &global_mdns_support_str);
2432 if (r < 0)
2433 return log_error_errno(r, "Failed to get the global mDNS support state: %s", bus_error_message(&error, r));
2434
2435 global_mdns_support = resolve_support_from_string(global_mdns_support_str);
2436 if (global_mdns_support < 0)
2437 return log_error_errno(global_mdns_support, "Received invalid global mDNS setting: %s", global_mdns_support_str);
2438
2439 if (global_mdns_support < mdns_support)
2440 log_warning("Setting mDNS support level \"%s\" for \"%s\", but the global support level is \"%s\".",
2441 argv[2], arg_ifname, global_mdns_support_str);
2442
d96f9abc 2443 r = bus_call_method(bus, bus_resolve_mgr, "SetLinkMulticastDNS", &error, NULL, "is", arg_ifindex, argv[2]);
65856bf2
YW
2444 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2445 sd_bus_error_free(&error);
2446
d96f9abc 2447 r = bus_call_method(
65856bf2 2448 bus,
d96f9abc 2449 bus_network_mgr,
65856bf2
YW
2450 "SetLinkMulticastDNS",
2451 &error,
2452 NULL,
2453 "is", arg_ifindex, argv[2]);
2454 }
a7a4c60a 2455 if (r < 0) {
a7a4c60a
YW
2456 if (arg_ifindex_permissive &&
2457 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2458 return 0;
2459
2460 return log_error_errno(r, "Failed to set MulticastDNS configuration: %s", bus_error_message(&error, r));
2461 }
2462
2463 return 0;
2464}
2465
c9299be2 2466static int verb_dns_over_tls(int argc, char **argv, void *userdata) {
d050561a 2467 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
99534007 2468 sd_bus *bus = ASSERT_PTR(userdata);
a661dc36 2469 int r;
d050561a 2470
d1293049 2471 if (argc >= 2) {
df87a53d 2472 r = ifname_mangle(argv[1]);
d1293049
LP
2473 if (r < 0)
2474 return r;
2475 }
d050561a 2476
d1293049
LP
2477 if (arg_ifindex <= 0)
2478 return status_all(bus, STATUS_PRIVATE);
d050561a 2479
d1293049 2480 if (argc < 3)
a661dc36 2481 return status_ifindex(bus, arg_ifindex, NULL, STATUS_PRIVATE, NULL);
d050561a 2482
d96f9abc 2483 r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDNSOverTLS", &error, NULL, "is", arg_ifindex, argv[2]);
65856bf2
YW
2484 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2485 sd_bus_error_free(&error);
2486
d96f9abc 2487 r = bus_call_method(
65856bf2 2488 bus,
d96f9abc 2489 bus_network_mgr,
65856bf2
YW
2490 "SetLinkDNSOverTLS",
2491 &error,
2492 NULL,
2493 "is", arg_ifindex, argv[2]);
2494 }
d050561a 2495 if (r < 0) {
d050561a
IT
2496 if (arg_ifindex_permissive &&
2497 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2498 return 0;
2499
c9299be2 2500 return log_error_errno(r, "Failed to set DNSOverTLS configuration: %s", bus_error_message(&error, r));
d050561a
IT
2501 }
2502
2503 return 0;
2504}
2505
a7a4c60a
YW
2506static int verb_dnssec(int argc, char **argv, void *userdata) {
2507 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
99534007 2508 sd_bus *bus = ASSERT_PTR(userdata);
a661dc36 2509 int r;
a7a4c60a 2510
d1293049 2511 if (argc >= 2) {
df87a53d 2512 r = ifname_mangle(argv[1]);
d1293049
LP
2513 if (r < 0)
2514 return r;
2515 }
a7a4c60a 2516
d1293049
LP
2517 if (arg_ifindex <= 0)
2518 return status_all(bus, STATUS_DNSSEC);
a7a4c60a 2519
d1293049 2520 if (argc < 3)
a661dc36 2521 return status_ifindex(bus, arg_ifindex, NULL, STATUS_DNSSEC, NULL);
a7a4c60a 2522
d96f9abc 2523 r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDNSSEC", &error, NULL, "is", arg_ifindex, argv[2]);
65856bf2
YW
2524 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2525 sd_bus_error_free(&error);
2526
d96f9abc 2527 r = bus_call_method(bus, bus_network_mgr, "SetLinkDNSSEC", &error, NULL, "is", arg_ifindex, argv[2]);
65856bf2 2528 }
a7a4c60a 2529 if (r < 0) {
a7a4c60a
YW
2530 if (arg_ifindex_permissive &&
2531 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2532 return 0;
2533
2534 return log_error_errno(r, "Failed to set DNSSEC configuration: %s", bus_error_message(&error, r));
2535 }
2536
2537 return 0;
2538}
2539
d96f9abc 2540static int call_nta(sd_bus *bus, char **nta, const BusLocator *locator, sd_bus_error *error) {
65856bf2
YW
2541 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
2542 int r;
2543
d96f9abc 2544 r = bus_message_new_method_call(bus, &req, locator, "SetLinkDNSSECNegativeTrustAnchors");
65856bf2
YW
2545 if (r < 0)
2546 return bus_log_create_error(r);
2547
2548 r = sd_bus_message_append(req, "i", arg_ifindex);
2549 if (r < 0)
2550 return bus_log_create_error(r);
2551
2552 r = sd_bus_message_append_strv(req, nta);
2553 if (r < 0)
2554 return bus_log_create_error(r);
2555
2556 return sd_bus_call(bus, req, 0, error, NULL);
2557}
2558
a7a4c60a
YW
2559static int verb_nta(int argc, char **argv, void *userdata) {
2560 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
99534007 2561 sd_bus *bus = ASSERT_PTR(userdata);
a661dc36 2562 int r;
06c28aa0 2563 bool clear;
a7a4c60a 2564
d1293049 2565 if (argc >= 2) {
df87a53d 2566 r = ifname_mangle(argv[1]);
d1293049
LP
2567 if (r < 0)
2568 return r;
2569 }
a7a4c60a 2570
d1293049
LP
2571 if (arg_ifindex <= 0)
2572 return status_all(bus, STATUS_NTA);
a7a4c60a 2573
d1293049 2574 if (argc < 3)
a661dc36 2575 return status_ifindex(bus, arg_ifindex, NULL, STATUS_NTA, NULL);
a7a4c60a 2576
06c28aa0
FB
2577 /* If only argument is the empty string, then call SetLinkDNSSECNegativeTrustAnchors()
2578 * with an empty list, which will clear the list of domains for an interface. */
2579 clear = strv_equal(argv + 2, STRV_MAKE(""));
2580
2581 if (!clear)
2582 STRV_FOREACH(p, argv + 2) {
2583 r = dns_name_is_valid(*p);
2584 if (r < 0)
2585 return log_error_errno(r, "Failed to validate specified domain %s: %m", *p);
d7a0f1f4
FS
2586 if (r == 0)
2587 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2588 "Domain not valid: %s",
2589 *p);
a7a4c60a 2590 }
a7a4c60a 2591
d96f9abc 2592 r = call_nta(bus, clear ? NULL : argv + 2, bus_resolve_mgr, &error);
65856bf2
YW
2593 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2594 sd_bus_error_free(&error);
a7a4c60a 2595
d96f9abc 2596 r = call_nta(bus, clear ? NULL : argv + 2, bus_network_mgr, &error);
65856bf2 2597 }
a7a4c60a 2598 if (r < 0) {
a7a4c60a
YW
2599 if (arg_ifindex_permissive &&
2600 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2601 return 0;
2602
2603 return log_error_errno(r, "Failed to set DNSSEC NTA configuration: %s", bus_error_message(&error, r));
2604 }
2605
2606 return 0;
2607}
2608
2609static int verb_revert_link(int argc, char **argv, void *userdata) {
2610 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
99534007 2611 sd_bus *bus = ASSERT_PTR(userdata);
a661dc36 2612 int r;
a7a4c60a 2613
d1293049 2614 if (argc >= 2) {
df87a53d 2615 r = ifname_mangle(argv[1]);
d1293049
LP
2616 if (r < 0)
2617 return r;
2618 }
2619
2620 if (arg_ifindex <= 0)
2621 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Interface argument required.");
a7a4c60a 2622
d96f9abc 2623 r = bus_call_method(bus, bus_resolve_mgr, "RevertLink", &error, NULL, "i", arg_ifindex);
65856bf2
YW
2624 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2625 sd_bus_error_free(&error);
2626
d96f9abc 2627 r = bus_call_method(bus, bus_network_mgr, "RevertLinkDNS", &error, NULL, "i", arg_ifindex);
65856bf2 2628 }
a7a4c60a
YW
2629 if (r < 0) {
2630 if (arg_ifindex_permissive &&
2631 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2632 return 0;
2633
2634 return log_error_errno(r, "Failed to revert interface configuration: %s", bus_error_message(&error, r));
2635 }
2636
2637 return 0;
2638}
2639
df957849 2640static int verb_log_level(int argc, char *argv[], void *userdata) {
99534007 2641 sd_bus *bus = ASSERT_PTR(userdata);
df957849 2642
b98416e1 2643 assert(IN_SET(argc, 1, 2));
df957849 2644
a87b151a 2645 return verb_log_control_common(bus, "org.freedesktop.resolve1", argv[0], argc == 2 ? argv[1] : NULL);
df957849
ZJS
2646}
2647
fffbf1dc
LP
2648static int print_question(char prefix, const char *color, JsonVariant *question) {
2649 JsonVariant *q = NULL;
2650 int r;
2651
2652 assert(color);
2653
2654 JSON_VARIANT_ARRAY_FOREACH(q, question) {
2655 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
2656 char buf[DNS_RESOURCE_KEY_STRING_MAX];
2657
ce74fb09 2658 r = dns_resource_key_from_json(q, &key);
fffbf1dc
LP
2659 if (r < 0) {
2660 log_warning_errno(r, "Received monitor message with invalid question key, ignoring: %m");
2661 continue;
2662 }
2663
2664 printf("%s%s %c%s: %s\n",
2665 color,
2666 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
2667 prefix,
2668 ansi_normal(),
2669 dns_resource_key_to_string(key, buf, sizeof(buf)));
2670 }
2671
2672 return 0;
2673}
2674
2675static int print_answer(JsonVariant *answer) {
2676 JsonVariant *a;
2677 int r;
2678
2679 JSON_VARIANT_ARRAY_FOREACH(a, answer) {
2680 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2681 _cleanup_free_ void *d = NULL;
2682 JsonVariant *jraw;
2683 const char *s;
2684 size_t l;
2685
2686 jraw = json_variant_by_key(a, "raw");
2687 if (!jraw) {
2688 log_warning("Received monitor answer lacking valid raw data, ignoring.");
2689 continue;
2690 }
2691
2692 r = json_variant_unbase64(jraw, &d, &l);
2693 if (r < 0) {
2694 log_warning_errno(r, "Failed to undo base64 encoding of monitor answer raw data, ignoring.");
2695 continue;
2696 }
2697
2698 r = dns_resource_record_new_from_raw(&rr, d, l);
2699 if (r < 0) {
64ebc0da 2700 log_warning_errno(r, "Failed to parse monitor answer RR, ignoring: %m");
fffbf1dc
LP
2701 continue;
2702 }
2703
2704 s = dns_resource_record_to_string(rr);
2705 if (!s)
2706 return log_oom();
2707
2708 printf("%s%s A%s: %s\n",
2709 ansi_highlight_yellow(),
2710 special_glyph(SPECIAL_GLYPH_ARROW_LEFT),
2711 ansi_normal(),
2712 s);
2713 }
2714
2715 return 0;
2716}
2717
2718static void monitor_query_dump(JsonVariant *v) {
2719 _cleanup_(json_variant_unrefp) JsonVariant *question = NULL, *answer = NULL, *collected_questions = NULL;
2720 int rcode = -1, error = 0, r;
2721 const char *state = NULL;
2722
2723 assert(v);
2724
2725 JsonDispatch dispatch_table[] = {
2726 { "question", JSON_VARIANT_ARRAY, json_dispatch_variant, PTR_TO_SIZE(&question), JSON_MANDATORY },
2727 { "answer", JSON_VARIANT_ARRAY, json_dispatch_variant, PTR_TO_SIZE(&answer), 0 },
2728 { "collectedQuestions", JSON_VARIANT_ARRAY, json_dispatch_variant, PTR_TO_SIZE(&collected_questions), 0 },
2729 { "state", JSON_VARIANT_STRING, json_dispatch_const_string, PTR_TO_SIZE(&state), JSON_MANDATORY },
2730 { "rcode", JSON_VARIANT_INTEGER, json_dispatch_int, PTR_TO_SIZE(&rcode), 0 },
2731 { "errno", JSON_VARIANT_INTEGER, json_dispatch_int, PTR_TO_SIZE(&error), 0 },
2732 {}
2733 };
2734
f1b622a0 2735 r = json_dispatch(v, dispatch_table, 0, NULL);
fffbf1dc
LP
2736 if (r < 0)
2737 return (void) log_warning("Received malformed monitor message, ignoring.");
2738
2739 /* First show the current question */
2740 print_question('Q', ansi_highlight_cyan(), question);
2741
2742 /* And then show the questions that led to this one in case this was a CNAME chain */
2743 print_question('C', ansi_highlight_grey(), collected_questions);
2744
2745 printf("%s%s S%s: %s\n",
2746 streq_ptr(state, "success") ? ansi_highlight_green() : ansi_highlight_red(),
2747 special_glyph(SPECIAL_GLYPH_ARROW_LEFT),
2748 ansi_normal(),
2749 strna(streq_ptr(state, "errno") ? errno_to_name(error) :
2750 streq_ptr(state, "rcode-failure") ? dns_rcode_to_string(rcode) :
2751 state));
2752
2753 print_answer(answer);
2754}
2755
2756static int monitor_reply(
2757 Varlink *link,
2758 JsonVariant *parameters,
2759 const char *error_id,
2760 VarlinkReplyFlags flags,
2761 void *userdata) {
2762
2763 assert(link);
2764
2765 if (error_id) {
2766 bool disconnect;
2767
2768 disconnect = streq(error_id, VARLINK_ERROR_DISCONNECTED);
2769 if (disconnect)
2770 log_info("Disconnected.");
2771 else
2772 log_error("Varlink error: %s", error_id);
2773
2774 (void) sd_event_exit(ASSERT_PTR(varlink_get_event(link)), disconnect ? EXIT_SUCCESS : EXIT_FAILURE);
2775 return 0;
2776 }
2777
2778 if (json_variant_by_key(parameters, "ready")) {
2779 /* The first message coming in will just indicate that we are now subscribed. We let our
2780 * caller know if they asked for it. Once the caller sees this they should know that we are
2781 * not going to miss any queries anymore. */
2782 (void) sd_notify(/* unset_environment=false */ false, "READY=1");
2783 return 0;
2784 }
2785
2786 if (arg_json_format_flags & JSON_FORMAT_OFF) {
2787 monitor_query_dump(parameters);
2788 printf("\n");
2789 } else
2790 json_variant_dump(parameters, arg_json_format_flags, NULL, NULL);
2791
2792 fflush(stdout);
2793
2794 return 0;
2795}
2796
2797static int verb_monitor(int argc, char *argv[], void *userdata) {
2798 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
2799 _cleanup_(varlink_unrefp) Varlink *vl = NULL;
2800 int r, c;
2801
2802 r = sd_event_default(&event);
2803 if (r < 0)
2804 return log_error_errno(r, "Failed to get event loop: %m");
2805
2806 r = sd_event_set_signal_exit(event, true);
2807 if (r < 0)
2808 return log_error_errno(r, "Failed to enable exit on SIGINT/SIGTERM: %m");
2809
2810 r = varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor");
2811 if (r < 0)
2812 return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m");
2813
2814 r = varlink_set_relative_timeout(vl, USEC_INFINITY); /* We want the monitor to run basically forever */
2815 if (r < 0)
2816 return log_error_errno(r, "Failed to set varlink time-out: %m");
2817
2818 r = varlink_attach_event(vl, event, SD_EVENT_PRIORITY_NORMAL);
2819 if (r < 0)
2820 return log_error_errno(r, "Failed to attach varlink connection to event loop: %m");
2821
2822 r = varlink_bind_reply(vl, monitor_reply);
2823 if (r < 0)
2824 return log_error_errno(r, "Failed to bind reply callback to varlink connection: %m");
2825
2826 r = varlink_observe(vl, "io.systemd.Resolve.Monitor.SubscribeQueryResults", NULL);
2827 if (r < 0)
2828 return log_error_errno(r, "Failed to issue SubscribeQueryResults() varlink call: %m");
2829
2830 r = sd_event_loop(event);
2831 if (r < 0)
2832 return log_error_errno(r, "Failed to run event loop: %m");
2833
2834 r = sd_event_get_exit_code(event, &c);
2835 if (r < 0)
2836 return log_error_errno(r, "Failed to get exit code: %m");
2837
2838 return c;
2839}
2840
6050e8b5
LP
2841static int dump_cache_item(JsonVariant *item) {
2842
2843 struct item_info {
2844 JsonVariant *key;
2845 JsonVariant *rrs;
2846 const char *type;
2847 uint64_t until;
2848 } item_info = {};
2849
2850 static const JsonDispatch dispatch_table[] = {
2851 { "key", JSON_VARIANT_OBJECT, json_dispatch_variant_noref, offsetof(struct item_info, key), JSON_MANDATORY },
2852 { "rrs", JSON_VARIANT_ARRAY, json_dispatch_variant_noref, offsetof(struct item_info, rrs), 0 },
2853 { "type", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct item_info, type), 0 },
2854 { "until", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct item_info, until), 0 },
2855 {},
2856 };
2857
2858 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL;
2859 int r, c = 0;
2860
f1b622a0 2861 r = json_dispatch(item, dispatch_table, JSON_LOG, &item_info);
6050e8b5
LP
2862 if (r < 0)
2863 return r;
2864
2865 r = dns_resource_key_from_json(item_info.key, &k);
2866 if (r < 0)
2867 return log_error_errno(r, "Failed to turn JSON data to resource key: %m");
2868
2869 if (item_info.type)
2870 printf("%s %s%s%s\n", DNS_RESOURCE_KEY_TO_STRING(k), ansi_highlight_red(), item_info.type, ansi_normal());
2871 else {
2872 JsonVariant *i;
2873
2874 JSON_VARIANT_ARRAY_FOREACH(i, item_info.rrs) {
2875 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2876 _cleanup_free_ void *data = NULL;
2877 JsonVariant *raw;
2878 size_t size;
2879
2880 raw = json_variant_by_key(i, "raw");
2881 if (!raw)
2882 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "raw field missing from RR JSON data.");
2883
2884 r = json_variant_unbase64(raw, &data, &size);
2885 if (r < 0)
2886 return log_error_errno(r, "Unable to decode raw RR JSON data: %m");
2887
2888 r = dns_resource_record_new_from_raw(&rr, data, size);
2889 if (r < 0)
2890 return log_error_errno(r, "Failed to parse DNS data: %m");
2891
2892 printf("%s\n", dns_resource_record_to_string(rr));
2893 c++;
2894 }
2895 }
2896
2897 return c;
2898}
2899
2900static int dump_cache_scope(JsonVariant *scope) {
2901
2902 struct scope_info {
2903 const char *protocol;
2904 int family;
2905 int ifindex;
2906 const char *ifname;
2907 JsonVariant *cache;
2908 } scope_info = {
2909 .family = AF_UNSPEC,
2910 };
2911 JsonVariant *i;
2912 int r, c = 0;
2913
2914 static const JsonDispatch dispatch_table[] = {
2915 { "protocol", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct scope_info, protocol), JSON_MANDATORY },
2916 { "family", JSON_VARIANT_INTEGER, json_dispatch_int, offsetof(struct scope_info, family), 0 },
2917 { "ifindex", JSON_VARIANT_INTEGER, json_dispatch_int, offsetof(struct scope_info, ifindex), 0 },
2918 { "ifname", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct scope_info, ifname), 0 },
2919 { "cache", JSON_VARIANT_ARRAY, json_dispatch_variant_noref, offsetof(struct scope_info, cache), JSON_MANDATORY },
2920 {},
2921 };
2922
f1b622a0 2923 r = json_dispatch(scope, dispatch_table, JSON_LOG, &scope_info);
6050e8b5
LP
2924 if (r < 0)
2925 return r;
2926
2927 printf("%sScope protocol=%s", ansi_underline(), scope_info.protocol);
2928
2929 if (scope_info.family != AF_UNSPEC)
2930 printf(" family=%s", af_to_name(scope_info.family));
2931
2932 if (scope_info.ifindex > 0)
2933 printf(" ifindex=%i", scope_info.ifindex);
2934 if (scope_info.ifname)
2935 printf(" ifname=%s", scope_info.ifname);
2936
2937 printf("%s\n", ansi_normal());
2938
2939 JSON_VARIANT_ARRAY_FOREACH(i, scope_info.cache) {
2940 r = dump_cache_item(i);
2941 if (r < 0)
2942 return r;
2943
2944 c += r;
2945 }
2946
2947 if (c == 0)
2948 printf("%sNo entries.%s\n\n", ansi_grey(), ansi_normal());
2949 else
2950 printf("\n");
2951
2952 return 0;
2953}
2954
2955static int verb_show_cache(int argc, char *argv[], void *userdata) {
43b49c0f 2956 JsonVariant *reply = NULL, *d = NULL;
6050e8b5
LP
2957 _cleanup_(varlink_unrefp) Varlink *vl = NULL;
2958 int r;
2959
2960 r = varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor");
2961 if (r < 0)
2962 return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m");
2963
2964 r = varlink_call(vl, "io.systemd.Resolve.Monitor.DumpCache", NULL, &reply, NULL, 0);
2965 if (r < 0)
2966 return log_error_errno(r, "Failed to issue DumpCache() varlink call: %m");
2967
2968 d = json_variant_by_key(reply, "dump");
2969 if (!d)
2970 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
2971 "DumpCache() response is missing 'dump' key.");
2972
2973 if (!json_variant_is_array(d))
2974 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
2975 "DumpCache() response 'dump' field not an array");
2976
2977 if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
2978 JsonVariant *i;
2979
2980 JSON_VARIANT_ARRAY_FOREACH(i, d) {
2981 r = dump_cache_scope(i);
2982 if (r < 0)
2983 return r;
2984 }
2985
2986 return 0;
2987 }
2988
2989 return json_variant_dump(d, arg_json_format_flags, NULL, NULL);
2990}
2991
bc837621
KV
2992static int dump_server_state(JsonVariant *server) {
2993 _cleanup_(table_unrefp) Table *table = NULL;
2994 TableCell *cell;
2995
2996 struct server_state {
2997 const char *server_name;
2998 const char *type;
2999 const char *ifname;
0319a28e 3000 int ifindex;
bc837621
KV
3001 const char *verified_feature_level;
3002 const char *possible_feature_level;
3003 const char *dnssec_mode;
a67e5c6e 3004 bool dnssec_supported;
bc837621
KV
3005 size_t received_udp_fragment_max;
3006 uint64_t n_failed_udp;
3007 uint64_t n_failed_tcp;
3008 bool packet_truncated;
3009 bool packet_bad_opt;
3010 bool packet_rrsig_missing;
3011 bool packet_invalid;
3012 bool packet_do_off;
0319a28e
LP
3013 } server_state = {
3014 .ifindex = -1,
3015 };
bc837621
KV
3016
3017 int r;
3018
3019 static const JsonDispatch dispatch_table[] = {
a67e5c6e
KV
3020 { "Server", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, server_name), JSON_MANDATORY },
3021 { "Type", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, type), JSON_MANDATORY },
3022 { "Interface", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, ifname), 0 },
0319a28e 3023 { "InterfaceIndex", JSON_VARIANT_INTEGER, json_dispatch_int, offsetof(struct server_state, ifindex), 0 },
a67e5c6e
KV
3024 { "VerifiedFeatureLevel", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, verified_feature_level), 0 },
3025 { "PossibleFeatureLevel", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, possible_feature_level), 0 },
3026 { "DNSSECMode", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, dnssec_mode), JSON_MANDATORY },
3027 { "DNSSECSupported", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, dnssec_supported), JSON_MANDATORY },
3028 { "ReceivedUDPFragmentMax", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct server_state, received_udp_fragment_max), JSON_MANDATORY },
3029 { "FailedUDPAttempts", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct server_state, n_failed_udp), JSON_MANDATORY },
3030 { "FailedTCPAttempts", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct server_state, n_failed_tcp), JSON_MANDATORY },
3031 { "PacketTruncated", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, packet_truncated), JSON_MANDATORY },
3032 { "PacketBadOpt", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, packet_bad_opt), JSON_MANDATORY },
3033 { "PacketRRSIGMissing", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, packet_rrsig_missing), JSON_MANDATORY },
3034 { "PacketInvalid", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, packet_invalid), JSON_MANDATORY },
3035 { "PacketDoOff", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, packet_do_off), JSON_MANDATORY },
bc837621
KV
3036 {},
3037 };
3038
f1b622a0 3039 r = json_dispatch(server, dispatch_table, JSON_LOG|JSON_PERMISSIVE, &server_state);
bc837621
KV
3040 if (r < 0)
3041 return r;
3042
3043 table = table_new_vertical();
3044 if (!table)
3045 return log_oom();
3046
3047 assert_se(cell = table_get_cell(table, 0, 0));
3048 (void) table_set_ellipsize_percent(table, cell, 100);
3049 (void) table_set_align_percent(table, cell, 0);
3050
3051 r = table_add_cell_stringf(table, NULL, "Server: %s", server_state.server_name);
3052 if (r < 0)
3053 return table_log_add_error(r);
3054
3055 r = table_add_many(table,
3056 TABLE_EMPTY,
3057 TABLE_FIELD, "Type",
3058 TABLE_SET_ALIGN_PERCENT, 100,
3059 TABLE_STRING, server_state.type);
3060 if (r < 0)
3061 return table_log_add_error(r);
3062
3063 if (server_state.ifname) {
3064 r = table_add_many(table,
3065 TABLE_FIELD, "Interface",
bc837621
KV
3066 TABLE_STRING, server_state.ifname);
3067 if (r < 0)
3068 return table_log_add_error(r);
3069 }
3070
0319a28e
LP
3071 if (server_state.ifindex >= 0) {
3072 r = table_add_many(table,
3073 TABLE_FIELD, "Interface Index",
3074 TABLE_INT, server_state.ifindex);
3075 if (r < 0)
3076 return table_log_add_error(r);
3077 }
3078
bc837621
KV
3079 if (server_state.verified_feature_level) {
3080 r = table_add_many(table,
3081 TABLE_FIELD, "Verified feature level",
3082 TABLE_STRING, server_state.verified_feature_level);
3083 if (r < 0)
3084 return table_log_add_error(r);
3085 }
3086
3087 if (server_state.possible_feature_level) {
3088 r = table_add_many(table,
3089 TABLE_FIELD, "Possible feature level",
3090 TABLE_STRING, server_state.possible_feature_level);
3091 if (r < 0)
3092 return table_log_add_error(r);
3093 }
3094
3095 r = table_add_many(table,
3096 TABLE_FIELD, "DNSSEC Mode",
3097 TABLE_STRING, server_state.dnssec_mode,
a67e5c6e
KV
3098 TABLE_FIELD, "DNSSEC Supported",
3099 TABLE_STRING, yes_no(server_state.dnssec_supported),
bc837621
KV
3100 TABLE_FIELD, "Maximum UDP fragment size received",
3101 TABLE_UINT64, server_state.received_udp_fragment_max,
3102 TABLE_FIELD, "Failed UDP attempts",
3103 TABLE_UINT64, server_state.n_failed_udp,
3104 TABLE_FIELD, "Failed TCP attempts",
3105 TABLE_UINT64, server_state.n_failed_tcp,
3106 TABLE_FIELD, "Seen truncated packet",
3107 TABLE_STRING, yes_no(server_state.packet_truncated),
3108 TABLE_FIELD, "Seen OPT RR getting lost",
3109 TABLE_STRING, yes_no(server_state.packet_bad_opt),
3110 TABLE_FIELD, "Seen RRSIG RR missing",
3111 TABLE_STRING, yes_no(server_state.packet_rrsig_missing),
3112 TABLE_FIELD, "Seen invalid packet",
3113 TABLE_STRING, yes_no(server_state.packet_invalid),
3114 TABLE_FIELD, "Server dropped DO flag",
3115 TABLE_STRING, yes_no(server_state.packet_do_off),
3116 TABLE_SET_ALIGN_PERCENT, 0,
3117 TABLE_EMPTY, TABLE_EMPTY);
3118
3119 if (r < 0)
3120 return table_log_add_error(r);
3121
3122 r = table_print(table, NULL);
3123 if (r < 0)
3124 return table_log_print_error(r);
3125
3126 return 0;
3127}
3128
3129static int verb_show_server_state(int argc, char *argv[], void *userdata) {
3130 JsonVariant *reply = NULL, *d = NULL;
3131 _cleanup_(varlink_unrefp) Varlink *vl = NULL;
3132 int r;
3133
3134 r = varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor");
3135 if (r < 0)
3136 return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m");
3137
3138 r = varlink_call(vl, "io.systemd.Resolve.Monitor.DumpServerState", NULL, &reply, NULL, 0);
3139 if (r < 0)
3140 return log_error_errno(r, "Failed to issue DumpServerState() varlink call: %m");
3141
3142 d = json_variant_by_key(reply, "dump");
3143 if (!d)
3144 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
3145 "DumpCache() response is missing 'dump' key.");
3146
3147 if (!json_variant_is_array(d))
3148 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
3149 "DumpCache() response 'dump' field not an array");
3150
3151 if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
3152 JsonVariant *i;
3153
3154 JSON_VARIANT_ARRAY_FOREACH(i, d) {
3155 r = dump_server_state(i);
3156 if (r < 0)
3157 return r;
3158 }
3159
3160 return 0;
3161 }
3162
3163 return json_variant_dump(d, arg_json_format_flags, NULL, NULL);
3164}
3165
a7a4c60a
YW
3166static void help_protocol_types(void) {
3167 if (arg_legend)
3168 puts("Known protocol types:");
5cf4b2e5
LP
3169 puts("dns\n"
3170 "llmnr\n"
3171 "llmnr-ipv4\n"
3172 "llmnr-ipv6\n"
3173 "mdns\n"
3174 "mdns-ipv4\n"
3175 "mdns-ipv6");
a7a4c60a
YW
3176}
3177
3178static void help_dns_types(void) {
a7a4c60a
YW
3179 if (arg_legend)
3180 puts("Known DNS RR types:");
5c828e66
LP
3181
3182 DUMP_STRING_TABLE(dns_type, int, _DNS_TYPE_MAX);
b93312f5
ZJS
3183}
3184
3185static void help_dns_classes(void) {
b93312f5 3186 if (arg_legend)
09b1fe14 3187 puts("Known DNS RR classes:");
5c828e66
LP
3188
3189 DUMP_STRING_TABLE(dns_class, int, _DNS_CLASS_MAX);
b93312f5
ZJS
3190}
3191
37ec0fdd
LP
3192static int compat_help(void) {
3193 _cleanup_free_ char *link = NULL;
3194 int r;
3195
3196 r = terminal_urlify_man("resolvectl", "1", &link);
3197 if (r < 0)
3198 return log_oom();
3199
1ace2438
ZJS
3200 printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n"
3201 "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n"
3202 "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n"
3203 "%1$s [OPTIONS...] --statistics\n"
3204 "%1$s [OPTIONS...] --reset-statistics\n"
3205 "\n"
353b2baa 3206 "%2$sResolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%3$s\n\n"
1ace2438
ZJS
3207 " -h --help Show this help\n"
3208 " --version Show package version\n"
be371fe0 3209 " --no-pager Do not pipe output into a pager\n"
1ace2438
ZJS
3210 " -4 Resolve IPv4 addresses\n"
3211 " -6 Resolve IPv6 addresses\n"
3212 " -i --interface=INTERFACE Look on interface\n"
3213 " -p --protocol=PROTO|help Look via protocol\n"
3214 " -t --type=TYPE|help Query RR with DNS type\n"
3215 " -c --class=CLASS|help Query RR with DNS class\n"
3216 " --service Resolve service (SRV)\n"
3217 " --service-address=BOOL Resolve address for services (default: yes)\n"
3218 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
3219 " --openpgp Query OpenPGP public key\n"
82d1d240 3220 " --tlsa Query TLS public key\n"
1ace2438
ZJS
3221 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
3222 " --search=BOOL Use search domains for single-label names\n"
3223 " (default: yes)\n"
dab48ea6 3224 " --raw[=payload|packet] Dump the answer as binary data\n"
1ace2438
ZJS
3225 " --legend=BOOL Print headers and additional info (default: yes)\n"
3226 " --statistics Show resolver statistics\n"
3227 " --reset-statistics Reset resolver statistics\n"
be371fe0 3228 " --status Show link and server status\n"
ba35662f 3229 " --flush-caches Flush all local DNS caches\n"
d55b0463
LP
3230 " --reset-server-features\n"
3231 " Forget learnt DNS server feature levels\n"
14965b94
LP
3232 " --set-dns=SERVER Set per-interface DNS server address\n"
3233 " --set-domain=DOMAIN Set per-interface search domain\n"
3234 " --set-llmnr=MODE Set per-interface LLMNR mode\n"
3235 " --set-mdns=MODE Set per-interface MulticastDNS mode\n"
c9299be2 3236 " --set-dnsovertls=MODE Set per-interface DNS-over-TLS mode\n"
14965b94
LP
3237 " --set-dnssec=MODE Set per-interface DNSSEC mode\n"
3238 " --set-nta=DOMAIN Set per-interface DNSSEC NTA\n"
3239 " --revert Revert per-interface configuration\n"
bc556335
DDM
3240 "\nSee the %4$s for details.\n",
3241 program_invocation_short_name,
3242 ansi_highlight(),
3243 ansi_normal(),
3244 link);
37ec0fdd
LP
3245
3246 return 0;
bdef7319
ZJS
3247}
3248
37ec0fdd
LP
3249static int native_help(void) {
3250 _cleanup_free_ char *link = NULL;
3251 int r;
3252
3253 r = terminal_urlify_man("resolvectl", "1", &link);
3254 if (r < 0)
3255 return log_oom();
3256
353b2baa 3257 printf("%s [OPTIONS...] COMMAND ...\n"
a7a4c60a 3258 "\n"
353b2baa
LP
3259 "%sSend control commands to the network name resolution manager, or%s\n"
3260 "%sresolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%s\n"
e1fac8a6 3261 "\nCommands:\n"
a7a4c60a
YW
3262 " query HOSTNAME|ADDRESS... Resolve domain names, IPv4 and IPv6 addresses\n"
3263 " service [[NAME] TYPE] DOMAIN Resolve service (SRV)\n"
3264 " openpgp EMAIL@DOMAIN... Query OpenPGP public key\n"
3265 " tlsa DOMAIN[:PORT]... Query TLS public key\n"
3266 " status [LINK...] Show link and server status\n"
3267 " statistics Show resolver statistics\n"
3268 " reset-statistics Reset resolver statistics\n"
3269 " flush-caches Flush all local DNS caches\n"
3270 " reset-server-features Forget learnt DNS server feature levels\n"
fffbf1dc 3271 " monitor Monitor DNS queries\n"
6050e8b5 3272 " show-cache Show cache contents\n"
bc837621 3273 " show-server-state Show servers state\n"
a7a4c60a
YW
3274 " dns [LINK [SERVER...]] Get/set per-interface DNS server address\n"
3275 " domain [LINK [DOMAIN...]] Get/set per-interface search domain\n"
f2fd3cdb 3276 " default-route [LINK [BOOL]] Get/set per-interface default route flag\n"
a7a4c60a
YW
3277 " llmnr [LINK [MODE]] Get/set per-interface LLMNR mode\n"
3278 " mdns [LINK [MODE]] Get/set per-interface MulticastDNS mode\n"
c9299be2 3279 " dnsovertls [LINK [MODE]] Get/set per-interface DNS-over-TLS mode\n"
a7a4c60a
YW
3280 " dnssec [LINK [MODE]] Get/set per-interface DNSSEC mode\n"
3281 " nta [LINK [DOMAIN...]] Get/set per-interface DNSSEC NTA\n"
3282 " revert LINK Revert per-interface configuration\n"
bde4bc9b 3283 " log-level [LEVEL] Get/set logging threshold for systemd-resolved\n"
353b2baa 3284 "\nOptions:\n"
e1fac8a6
ZJS
3285 " -h --help Show this help\n"
3286 " --version Show package version\n"
3287 " --no-pager Do not pipe output into a pager\n"
3288 " -4 Resolve IPv4 addresses\n"
3289 " -6 Resolve IPv6 addresses\n"
3290 " -i --interface=INTERFACE Look on interface\n"
3291 " -p --protocol=PROTO|help Look via protocol\n"
3292 " -t --type=TYPE|help Query RR with DNS type\n"
3293 " -c --class=CLASS|help Query RR with DNS class\n"
3294 " --service-address=BOOL Resolve address for services (default: yes)\n"
3295 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
3296 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
d711322c
LP
3297 " --validate=BOOL Allow DNSSEC validation (default: yes)\n"
3298 " --synthesize=BOOL Allow synthetic response (default: yes)\n"
3299 " --cache=BOOL Allow response from cache (default: yes)\n"
5ed91481 3300 " --stale-data=BOOL Allow response from cache with stale data (default: yes)\n"
d711322c
LP
3301 " --zone=BOOL Allow response from locally registered mDNS/LLMNR\n"
3302 " records (default: yes)\n"
fffbf1dc
LP
3303 " --trust-anchor=BOOL Allow response from local trust anchor (default:\n"
3304 " yes)\n"
d711322c 3305 " --network=BOOL Allow response from network (default: yes)\n"
fffbf1dc
LP
3306 " --search=BOOL Use search domains for single-label names (default:\n"
3307 " yes)\n"
e1fac8a6
ZJS
3308 " --raw[=payload|packet] Dump the answer as binary data\n"
3309 " --legend=BOOL Print headers and additional info (default: yes)\n"
fffbf1dc
LP
3310 " --json=MODE Output as JSON\n"
3311 " -j Same as --json=pretty on tty, --json=short\n"
3312 " otherwise\n"
bc556335
DDM
3313 "\nSee the %s for details.\n",
3314 program_invocation_short_name,
3315 ansi_highlight(),
3316 ansi_normal(),
3317 ansi_highlight(),
3318 ansi_normal(),
3319 link);
37ec0fdd
LP
3320
3321 return 0;
a7a4c60a
YW
3322}
3323
3324static int verb_help(int argc, char **argv, void *userdata) {
37ec0fdd 3325 return native_help();
a7a4c60a
YW
3326}
3327
3328static int compat_parse_argv(int argc, char *argv[]) {
bdef7319
ZJS
3329 enum {
3330 ARG_VERSION = 0x100,
dad29dff 3331 ARG_LEGEND,
45ec7efb
LP
3332 ARG_SERVICE,
3333 ARG_CNAME,
3334 ARG_SERVICE_ADDRESS,
3335 ARG_SERVICE_TXT,
4ac2ca1b 3336 ARG_OPENPGP,
82d1d240 3337 ARG_TLSA,
2e74028a 3338 ARG_RAW,
801ad6a6 3339 ARG_SEARCH,
a150ff5e
LP
3340 ARG_STATISTICS,
3341 ARG_RESET_STATISTICS,
be371fe0 3342 ARG_STATUS,
ba35662f 3343 ARG_FLUSH_CACHES,
d55b0463 3344 ARG_RESET_SERVER_FEATURES,
be371fe0 3345 ARG_NO_PAGER,
14965b94
LP
3346 ARG_SET_DNS,
3347 ARG_SET_DOMAIN,
3348 ARG_SET_LLMNR,
3349 ARG_SET_MDNS,
d050561a 3350 ARG_SET_PRIVATE,
14965b94
LP
3351 ARG_SET_DNSSEC,
3352 ARG_SET_NTA,
3353 ARG_REVERT_LINK,
bdef7319
ZJS
3354 };
3355
3356 static const struct option options[] = {
d55b0463
LP
3357 { "help", no_argument, NULL, 'h' },
3358 { "version", no_argument, NULL, ARG_VERSION },
3359 { "type", required_argument, NULL, 't' },
3360 { "class", required_argument, NULL, 'c' },
3361 { "legend", required_argument, NULL, ARG_LEGEND },
3362 { "interface", required_argument, NULL, 'i' },
3363 { "protocol", required_argument, NULL, 'p' },
3364 { "cname", required_argument, NULL, ARG_CNAME },
3365 { "service", no_argument, NULL, ARG_SERVICE },
3366 { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS },
3367 { "service-txt", required_argument, NULL, ARG_SERVICE_TXT },
3368 { "openpgp", no_argument, NULL, ARG_OPENPGP },
3369 { "tlsa", optional_argument, NULL, ARG_TLSA },
3370 { "raw", optional_argument, NULL, ARG_RAW },
3371 { "search", required_argument, NULL, ARG_SEARCH },
3372 { "statistics", no_argument, NULL, ARG_STATISTICS, },
3373 { "reset-statistics", no_argument, NULL, ARG_RESET_STATISTICS },
3374 { "status", no_argument, NULL, ARG_STATUS },
3375 { "flush-caches", no_argument, NULL, ARG_FLUSH_CACHES },
3376 { "reset-server-features", no_argument, NULL, ARG_RESET_SERVER_FEATURES },
3377 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
14965b94
LP
3378 { "set-dns", required_argument, NULL, ARG_SET_DNS },
3379 { "set-domain", required_argument, NULL, ARG_SET_DOMAIN },
3380 { "set-llmnr", required_argument, NULL, ARG_SET_LLMNR },
3381 { "set-mdns", required_argument, NULL, ARG_SET_MDNS },
c9299be2 3382 { "set-dnsovertls", required_argument, NULL, ARG_SET_PRIVATE },
14965b94
LP
3383 { "set-dnssec", required_argument, NULL, ARG_SET_DNSSEC },
3384 { "set-nta", required_argument, NULL, ARG_SET_NTA },
3385 { "revert", no_argument, NULL, ARG_REVERT_LINK },
bdef7319
ZJS
3386 {}
3387 };
3388
2d4c5cbc 3389 int c, r;
bdef7319
ZJS
3390
3391 assert(argc >= 0);
3392 assert(argv);
3393
51323288 3394 while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0)
79893116 3395 switch (c) {
bdef7319
ZJS
3396
3397 case 'h':
37ec0fdd 3398 return compat_help();
bdef7319
ZJS
3399
3400 case ARG_VERSION:
3f6fd1ba 3401 return version();
bdef7319
ZJS
3402
3403 case '4':
3404 arg_family = AF_INET;
3405 break;
3406
3407 case '6':
3408 arg_family = AF_INET6;
3409 break;
3410
27d8af3e 3411 case 'i':
df87a53d 3412 r = ifname_mangle(optarg);
a7a4c60a
YW
3413 if (r < 0)
3414 return r;
2d4c5cbc
LP
3415 break;
3416
3417 case 't':
b93312f5
ZJS
3418 if (streq(optarg, "help")) {
3419 help_dns_types();
3420 return 0;
3421 }
3422
4b548ef3 3423 r = dns_type_from_string(optarg);
7211c853
ZJS
3424 if (r < 0)
3425 return log_error_errno(r, "Failed to parse RR record type %s: %m", optarg);
3426
4b548ef3
LP
3427 arg_type = (uint16_t) r;
3428 assert((int) arg_type == r);
b93312f5 3429
a150ff5e 3430 arg_mode = MODE_RESOLVE_RECORD;
2d4c5cbc
LP
3431 break;
3432
3433 case 'c':
b93312f5
ZJS
3434 if (streq(optarg, "help")) {
3435 help_dns_classes();
3436 return 0;
3437 }
3438
4b548ef3 3439 r = dns_class_from_string(optarg);
7211c853
ZJS
3440 if (r < 0)
3441 return log_error_errno(r, "Failed to parse RR record class %s: %m", optarg);
3442
4b548ef3
LP
3443 arg_class = (uint16_t) r;
3444 assert((int) arg_class == r);
b93312f5
ZJS
3445
3446 break;
3447
dad29dff 3448 case ARG_LEGEND:
599c7c54 3449 r = parse_boolean_argument("--legend=", optarg, &arg_legend);
45ec7efb 3450 if (r < 0)
599c7c54 3451 return r;
bdef7319
ZJS
3452 break;
3453
51323288 3454 case 'p':
ba82da3b
ZJS
3455 if (streq(optarg, "help")) {
3456 help_protocol_types();
3457 return 0;
3458 } else if (streq(optarg, "dns"))
51323288
LP
3459 arg_flags |= SD_RESOLVED_DNS;
3460 else if (streq(optarg, "llmnr"))
3461 arg_flags |= SD_RESOLVED_LLMNR;
3462 else if (streq(optarg, "llmnr-ipv4"))
3463 arg_flags |= SD_RESOLVED_LLMNR_IPV4;
3464 else if (streq(optarg, "llmnr-ipv6"))
3465 arg_flags |= SD_RESOLVED_LLMNR_IPV6;
062aabb9
DR
3466 else if (streq(optarg, "mdns"))
3467 arg_flags |= SD_RESOLVED_MDNS;
3468 else if (streq(optarg, "mdns-ipv4"))
3469 arg_flags |= SD_RESOLVED_MDNS_IPV4;
3470 else if (streq(optarg, "mdns-ipv6"))
3471 arg_flags |= SD_RESOLVED_MDNS_IPV6;
baaa35ad
ZJS
3472 else
3473 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3474 "Unknown protocol specifier: %s", optarg);
51323288
LP
3475
3476 break;
3477
45ec7efb 3478 case ARG_SERVICE:
a150ff5e 3479 arg_mode = MODE_RESOLVE_SERVICE;
45ec7efb
LP
3480 break;
3481
4ac2ca1b
ZJS
3482 case ARG_OPENPGP:
3483 arg_mode = MODE_RESOLVE_OPENPGP;
3484 break;
3485
82d1d240
ZJS
3486 case ARG_TLSA:
3487 arg_mode = MODE_RESOLVE_TLSA;
ebbc70e5
YW
3488 if (!optarg || service_family_is_valid(optarg))
3489 arg_service_family = optarg;
baaa35ad
ZJS
3490 else
3491 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3492 "Unknown service family \"%s\".", optarg);
82d1d240
ZJS
3493 break;
3494
2e74028a 3495 case ARG_RAW:
baaa35ad
ZJS
3496 if (on_tty())
3497 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY),
3498 "Refusing to write binary data to tty.");
2e74028a 3499
dab48ea6
ZJS
3500 if (optarg == NULL || streq(optarg, "payload"))
3501 arg_raw = RAW_PAYLOAD;
3502 else if (streq(optarg, "packet"))
3503 arg_raw = RAW_PACKET;
baaa35ad
ZJS
3504 else
3505 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3506 "Unknown --raw specifier \"%s\".",
3507 optarg);
dab48ea6 3508
2e74028a
ZJS
3509 arg_legend = false;
3510 break;
3511
45ec7efb 3512 case ARG_CNAME:
c3470872 3513 r = parse_boolean_argument("--cname=", optarg, NULL);
45ec7efb 3514 if (r < 0)
c3470872 3515 return r;
5883ff60 3516 SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0);
45ec7efb
LP
3517 break;
3518
3519 case ARG_SERVICE_ADDRESS:
c3470872 3520 r = parse_boolean_argument("--service-address=", optarg, NULL);
45ec7efb 3521 if (r < 0)
c3470872 3522 return r;
5883ff60 3523 SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0);
45ec7efb
LP
3524 break;
3525
3526 case ARG_SERVICE_TXT:
c3470872 3527 r = parse_boolean_argument("--service-txt=", optarg, NULL);
45ec7efb 3528 if (r < 0)
c3470872 3529 return r;
5883ff60 3530 SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0);
45ec7efb
LP
3531 break;
3532
801ad6a6 3533 case ARG_SEARCH:
c3470872 3534 r = parse_boolean_argument("--search=", optarg, NULL);
801ad6a6 3535 if (r < 0)
c3470872 3536 return r;
5883ff60 3537 SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0);
801ad6a6
LP
3538 break;
3539
a150ff5e
LP
3540 case ARG_STATISTICS:
3541 arg_mode = MODE_STATISTICS;
3542 break;
3543
3544 case ARG_RESET_STATISTICS:
3545 arg_mode = MODE_RESET_STATISTICS;
3546 break;
3547
ba35662f
LP
3548 case ARG_FLUSH_CACHES:
3549 arg_mode = MODE_FLUSH_CACHES;
3550 break;
3551
d55b0463
LP
3552 case ARG_RESET_SERVER_FEATURES:
3553 arg_mode = MODE_RESET_SERVER_FEATURES;
3554 break;
3555
be371fe0
LP
3556 case ARG_STATUS:
3557 arg_mode = MODE_STATUS;
3558 break;
3559
3560 case ARG_NO_PAGER:
0221d68a 3561 arg_pager_flags |= PAGER_DISABLE;
be371fe0
LP
3562 break;
3563
a7a4c60a
YW
3564 case ARG_SET_DNS:
3565 r = strv_extend(&arg_set_dns, optarg);
14965b94 3566 if (r < 0)
14965b94 3567 return log_oom();
14965b94 3568
14965b94
LP
3569 arg_mode = MODE_SET_LINK;
3570 break;
14965b94 3571
a7a4c60a 3572 case ARG_SET_DOMAIN:
14965b94
LP
3573 r = strv_extend(&arg_set_domain, optarg);
3574 if (r < 0)
3575 return log_oom();
3576
3577 arg_mode = MODE_SET_LINK;
3578 break;
14965b94
LP
3579
3580 case ARG_SET_LLMNR:
a7a4c60a 3581 arg_set_llmnr = optarg;
14965b94
LP
3582 arg_mode = MODE_SET_LINK;
3583 break;
3584
3585 case ARG_SET_MDNS:
a7a4c60a 3586 arg_set_mdns = optarg;
14965b94
LP
3587 arg_mode = MODE_SET_LINK;
3588 break;
3589
d050561a 3590 case ARG_SET_PRIVATE:
c9299be2 3591 arg_set_dns_over_tls = optarg;
d050561a
IT
3592 arg_mode = MODE_SET_LINK;
3593 break;
3594
14965b94 3595 case ARG_SET_DNSSEC:
a7a4c60a 3596 arg_set_dnssec = optarg;
14965b94
LP
3597 arg_mode = MODE_SET_LINK;
3598 break;
3599
3600 case ARG_SET_NTA:
14965b94
LP
3601 r = strv_extend(&arg_set_nta, optarg);
3602 if (r < 0)
3603 return log_oom();
3604
3605 arg_mode = MODE_SET_LINK;
3606 break;
3607
3608 case ARG_REVERT_LINK:
3609 arg_mode = MODE_REVERT_LINK;
3610 break;
3611
bdef7319
ZJS
3612 case '?':
3613 return -EINVAL;
3614
3615 default:
04499a70 3616 assert_not_reached();
bdef7319
ZJS
3617 }
3618
baaa35ad
ZJS
3619 if (arg_type == 0 && arg_class != 0)
3620 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3621 "--class= may only be used in conjunction with --type=.");
45ec7efb 3622
baaa35ad
ZJS
3623 if (arg_type != 0 && arg_mode == MODE_RESOLVE_SERVICE)
3624 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3625 "--service and --type= may not be combined.");
2d4c5cbc
LP
3626
3627 if (arg_type != 0 && arg_class == 0)
3628 arg_class = DNS_CLASS_IN;
3629
c1dafe4f
LP
3630 if (arg_class != 0 && arg_type == 0)
3631 arg_type = DNS_TYPE_A;
3632
14965b94
LP
3633 if (IN_SET(arg_mode, MODE_SET_LINK, MODE_REVERT_LINK)) {
3634
baaa35ad
ZJS
3635 if (arg_ifindex <= 0)
3636 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3637 "--set-dns=, --set-domain=, --set-llmnr=, --set-mdns=, --set-dnsovertls=, --set-dnssec=, --set-nta= and --revert require --interface=.");
14965b94
LP
3638 }
3639
bdef7319
ZJS
3640 return 1 /* work to do */;
3641}
3642
a7a4c60a
YW
3643static int native_parse_argv(int argc, char *argv[]) {
3644 enum {
3645 ARG_VERSION = 0x100,
3646 ARG_LEGEND,
3647 ARG_CNAME,
d711322c
LP
3648 ARG_VALIDATE,
3649 ARG_SYNTHESIZE,
3650 ARG_CACHE,
3651 ARG_ZONE,
3652 ARG_TRUST_ANCHOR,
3653 ARG_NETWORK,
a7a4c60a
YW
3654 ARG_SERVICE_ADDRESS,
3655 ARG_SERVICE_TXT,
3656 ARG_RAW,
3657 ARG_SEARCH,
3658 ARG_NO_PAGER,
fffbf1dc 3659 ARG_JSON,
5ed91481 3660 ARG_STALE_DATA
a7a4c60a 3661 };
bdef7319 3662
a7a4c60a
YW
3663 static const struct option options[] = {
3664 { "help", no_argument, NULL, 'h' },
3665 { "version", no_argument, NULL, ARG_VERSION },
3666 { "type", required_argument, NULL, 't' },
3667 { "class", required_argument, NULL, 'c' },
3668 { "legend", required_argument, NULL, ARG_LEGEND },
3669 { "interface", required_argument, NULL, 'i' },
3670 { "protocol", required_argument, NULL, 'p' },
3671 { "cname", required_argument, NULL, ARG_CNAME },
d711322c
LP
3672 { "validate", required_argument, NULL, ARG_VALIDATE },
3673 { "synthesize", required_argument, NULL, ARG_SYNTHESIZE },
3674 { "cache", required_argument, NULL, ARG_CACHE },
3675 { "zone", required_argument, NULL, ARG_ZONE },
3676 { "trust-anchor", required_argument, NULL, ARG_TRUST_ANCHOR },
3677 { "network", required_argument, NULL, ARG_NETWORK },
a7a4c60a
YW
3678 { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS },
3679 { "service-txt", required_argument, NULL, ARG_SERVICE_TXT },
3680 { "raw", optional_argument, NULL, ARG_RAW },
3681 { "search", required_argument, NULL, ARG_SEARCH },
3682 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
fffbf1dc 3683 { "json", required_argument, NULL, ARG_JSON },
5ed91481 3684 { "stale-data", required_argument, NULL, ARG_STALE_DATA },
a7a4c60a
YW
3685 {}
3686 };
bdef7319 3687
a7a4c60a 3688 int c, r;
79266746 3689
a7a4c60a
YW
3690 assert(argc >= 0);
3691 assert(argv);
bdef7319 3692
fffbf1dc 3693 while ((c = getopt_long(argc, argv, "h46i:t:c:p:j", options, NULL)) >= 0)
79893116 3694 switch (c) {
45ec7efb 3695
a7a4c60a 3696 case 'h':
37ec0fdd 3697 return native_help();
a7a4c60a
YW
3698
3699 case ARG_VERSION:
3700 return version();
a150ff5e 3701
a7a4c60a
YW
3702 case '4':
3703 arg_family = AF_INET;
3704 break;
a150ff5e 3705
a7a4c60a
YW
3706 case '6':
3707 arg_family = AF_INET6;
3708 break;
3709
3710 case 'i':
df87a53d 3711 r = ifname_mangle(optarg);
a7a4c60a
YW
3712 if (r < 0)
3713 return r;
a7a4c60a
YW
3714 break;
3715
3716 case 't':
3717 if (streq(optarg, "help")) {
3718 help_dns_types();
3719 return 0;
3720 }
3721
3722 r = dns_type_from_string(optarg);
7211c853
ZJS
3723 if (r < 0)
3724 return log_error_errno(r, "Failed to parse RR record type %s: %m", optarg);
3725
a7a4c60a
YW
3726 arg_type = (uint16_t) r;
3727 assert((int) arg_type == r);
3728
3729 break;
3730
3731 case 'c':
3732 if (streq(optarg, "help")) {
3733 help_dns_classes();
3734 return 0;
3735 }
3736
3737 r = dns_class_from_string(optarg);
7211c853
ZJS
3738 if (r < 0)
3739 return log_error_errno(r, "Failed to parse RR record class %s: %m", optarg);
3740
a7a4c60a
YW
3741 arg_class = (uint16_t) r;
3742 assert((int) arg_class == r);
3743
3744 break;
3745
3746 case ARG_LEGEND:
599c7c54 3747 r = parse_boolean_argument("--legend=", optarg, &arg_legend);
a7a4c60a 3748 if (r < 0)
599c7c54 3749 return r;
a7a4c60a
YW
3750 break;
3751
3752 case 'p':
3753 if (streq(optarg, "help")) {
3754 help_protocol_types();
3755 return 0;
3756 } else if (streq(optarg, "dns"))
3757 arg_flags |= SD_RESOLVED_DNS;
3758 else if (streq(optarg, "llmnr"))
3759 arg_flags |= SD_RESOLVED_LLMNR;
3760 else if (streq(optarg, "llmnr-ipv4"))
3761 arg_flags |= SD_RESOLVED_LLMNR_IPV4;
3762 else if (streq(optarg, "llmnr-ipv6"))
3763 arg_flags |= SD_RESOLVED_LLMNR_IPV6;
3764 else if (streq(optarg, "mdns"))
3765 arg_flags |= SD_RESOLVED_MDNS;
3766 else if (streq(optarg, "mdns-ipv4"))
3767 arg_flags |= SD_RESOLVED_MDNS_IPV4;
3768 else if (streq(optarg, "mdns-ipv6"))
3769 arg_flags |= SD_RESOLVED_MDNS_IPV6;
baaa35ad
ZJS
3770 else
3771 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3772 "Unknown protocol specifier: %s",
3773 optarg);
a150ff5e 3774
a7a4c60a 3775 break;
a150ff5e 3776
a7a4c60a 3777 case ARG_RAW:
baaa35ad
ZJS
3778 if (on_tty())
3779 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY),
3780 "Refusing to write binary data to tty.");
a150ff5e 3781
a7a4c60a
YW
3782 if (optarg == NULL || streq(optarg, "payload"))
3783 arg_raw = RAW_PAYLOAD;
3784 else if (streq(optarg, "packet"))
3785 arg_raw = RAW_PACKET;
baaa35ad
ZJS
3786 else
3787 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3788 "Unknown --raw specifier \"%s\".",
3789 optarg);
a150ff5e 3790
a7a4c60a
YW
3791 arg_legend = false;
3792 break;
a150ff5e 3793
a7a4c60a 3794 case ARG_CNAME:
c3470872 3795 r = parse_boolean_argument("--cname=", optarg, NULL);
a7a4c60a 3796 if (r < 0)
c3470872 3797 return r;
a7a4c60a
YW
3798 SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0);
3799 break;
a150ff5e 3800
d711322c 3801 case ARG_VALIDATE:
c3470872 3802 r = parse_boolean_argument("--validate=", optarg, NULL);
d711322c 3803 if (r < 0)
c3470872 3804 return r;
d711322c
LP
3805 SET_FLAG(arg_flags, SD_RESOLVED_NO_VALIDATE, r == 0);
3806 break;
3807
3808 case ARG_SYNTHESIZE:
c3470872 3809 r = parse_boolean_argument("--synthesize=", optarg, NULL);
d711322c 3810 if (r < 0)
c3470872 3811 return r;
d711322c
LP
3812 SET_FLAG(arg_flags, SD_RESOLVED_NO_SYNTHESIZE, r == 0);
3813 break;
3814
3815 case ARG_CACHE:
c3470872 3816 r = parse_boolean_argument("--cache=", optarg, NULL);
d711322c 3817 if (r < 0)
c3470872 3818 return r;
d711322c
LP
3819 SET_FLAG(arg_flags, SD_RESOLVED_NO_CACHE, r == 0);
3820 break;
3821
5ed91481
KV
3822 case ARG_STALE_DATA:
3823 r = parse_boolean_argument("--stale-data=", optarg, NULL);
3824 if (r < 0)
3825 return r;
3826 SET_FLAG(arg_flags, SD_RESOLVED_NO_STALE, r == 0);
3827 break;
3828
d711322c 3829 case ARG_ZONE:
c3470872 3830 r = parse_boolean_argument("--zone=", optarg, NULL);
d711322c 3831 if (r < 0)
c3470872 3832 return r;
d711322c
LP
3833 SET_FLAG(arg_flags, SD_RESOLVED_NO_ZONE, r == 0);
3834 break;
3835
3836 case ARG_TRUST_ANCHOR:
c3470872 3837 r = parse_boolean_argument("--trust-anchor=", optarg, NULL);
d711322c 3838 if (r < 0)
c3470872 3839 return r;
d711322c
LP
3840 SET_FLAG(arg_flags, SD_RESOLVED_NO_TRUST_ANCHOR, r == 0);
3841 break;
3842
3843 case ARG_NETWORK:
c3470872 3844 r = parse_boolean_argument("--network=", optarg, NULL);
d711322c 3845 if (r < 0)
c3470872 3846 return r;
d711322c
LP
3847 SET_FLAG(arg_flags, SD_RESOLVED_NO_NETWORK, r == 0);
3848 break;
3849
a7a4c60a 3850 case ARG_SERVICE_ADDRESS:
c3470872 3851 r = parse_boolean_argument("--service-address=", optarg, NULL);
a7a4c60a 3852 if (r < 0)
c3470872 3853 return r;
a7a4c60a
YW
3854 SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0);
3855 break;
a150ff5e 3856
a7a4c60a 3857 case ARG_SERVICE_TXT:
c3470872 3858 r = parse_boolean_argument("--service-txt=", optarg, NULL);
a7a4c60a 3859 if (r < 0)
c3470872 3860 return r;
a7a4c60a
YW
3861 SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0);
3862 break;
45ec7efb 3863
a7a4c60a 3864 case ARG_SEARCH:
c3470872 3865 r = parse_boolean_argument("--search=", optarg, NULL);
a7a4c60a 3866 if (r < 0)
c3470872 3867 return r;
a7a4c60a
YW
3868 SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0);
3869 break;
79266746 3870
a7a4c60a 3871 case ARG_NO_PAGER:
0221d68a 3872 arg_pager_flags |= PAGER_DISABLE;
a7a4c60a
YW
3873 break;
3874
fffbf1dc
LP
3875 case ARG_JSON:
3876 r = parse_json_argument(optarg, &arg_json_format_flags);
3877 if (r <= 0)
3878 return r;
3879
3880 break;
3881
3882 case 'j':
3883 arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO;
3884 break;
3885
a7a4c60a
YW
3886 case '?':
3887 return -EINVAL;
4ac2ca1b 3888
a7a4c60a 3889 default:
04499a70 3890 assert_not_reached();
4ac2ca1b
ZJS
3891 }
3892
baaa35ad
ZJS
3893 if (arg_type == 0 && arg_class != 0)
3894 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3895 "--class= may only be used in conjunction with --type=.");
4ac2ca1b 3896
a7a4c60a
YW
3897 if (arg_type != 0 && arg_class == 0)
3898 arg_class = DNS_CLASS_IN;
4ac2ca1b 3899
a7a4c60a
YW
3900 if (arg_class != 0 && arg_type == 0)
3901 arg_type = DNS_TYPE_A;
82d1d240 3902
a7a4c60a
YW
3903 return 1 /* work to do */;
3904}
82d1d240 3905
a7a4c60a
YW
3906static int native_main(int argc, char *argv[], sd_bus *bus) {
3907
3908 static const Verb verbs[] = {
3909 { "help", VERB_ANY, VERB_ANY, 0, verb_help },
3910 { "status", VERB_ANY, VERB_ANY, VERB_DEFAULT, verb_status },
3911 { "query", 2, VERB_ANY, 0, verb_query },
3912 { "service", 2, 4, 0, verb_service },
3913 { "openpgp", 2, VERB_ANY, 0, verb_openpgp },
3914 { "tlsa", 2, VERB_ANY, 0, verb_tlsa },
3915 { "statistics", VERB_ANY, 1, 0, show_statistics },
3916 { "reset-statistics", VERB_ANY, 1, 0, reset_statistics },
3917 { "flush-caches", VERB_ANY, 1, 0, flush_caches },
3918 { "reset-server-features", VERB_ANY, 1, 0, reset_server_features },
3919 { "dns", VERB_ANY, VERB_ANY, 0, verb_dns },
3920 { "domain", VERB_ANY, VERB_ANY, 0, verb_domain },
f2fd3cdb 3921 { "default-route", VERB_ANY, 3, 0, verb_default_route },
a7a4c60a
YW
3922 { "llmnr", VERB_ANY, 3, 0, verb_llmnr },
3923 { "mdns", VERB_ANY, 3, 0, verb_mdns },
efe55c81 3924 { "dnsovertls", VERB_ANY, 3, 0, verb_dns_over_tls },
a7a4c60a
YW
3925 { "dnssec", VERB_ANY, 3, 0, verb_dnssec },
3926 { "nta", VERB_ANY, VERB_ANY, 0, verb_nta },
d1293049 3927 { "revert", VERB_ANY, 2, 0, verb_revert_link },
df957849 3928 { "log-level", VERB_ANY, 2, 0, verb_log_level },
fffbf1dc 3929 { "monitor", VERB_ANY, 1, 0, verb_monitor },
6050e8b5 3930 { "show-cache", VERB_ANY, 1, 0, verb_show_cache },
bc837621 3931 { "show-server-state", VERB_ANY, 1, 0, verb_show_server_state},
a7a4c60a
YW
3932 {}
3933 };
82d1d240 3934
a7a4c60a
YW
3935 return dispatch_verb(argc, argv, verbs, bus);
3936}
82d1d240 3937
da6053d0 3938static int translate(const char *verb, const char *single_arg, size_t num_args, char **args, sd_bus *bus) {
a7a4c60a 3939 char **fake, **p;
c9d243cd 3940 size_t num;
bdef7319 3941
a7a4c60a
YW
3942 assert(verb);
3943 assert(num_args == 0 || args);
a150ff5e 3944
a7a4c60a 3945 num = !!single_arg + num_args + 1;
79266746 3946
a7a4c60a
YW
3947 p = fake = newa0(char *, num + 1);
3948 *p++ = (char *) verb;
3949 if (single_arg)
3950 *p++ = (char *) single_arg;
c9d243cd 3951 for (size_t i = 0; i < num_args; i++)
a7a4c60a 3952 *p++ = args[i];
ba35662f 3953
a7a4c60a 3954 optind = 0;
da6053d0 3955 return native_main((int) num, fake, bus);
a7a4c60a 3956}
ba35662f 3957
a7a4c60a
YW
3958static int compat_main(int argc, char *argv[], sd_bus *bus) {
3959 int r = 0;
be371fe0 3960
a7a4c60a
YW
3961 switch (arg_mode) {
3962 case MODE_RESOLVE_HOST:
3963 case MODE_RESOLVE_RECORD:
3964 return translate("query", NULL, argc - optind, argv + optind, bus);
d55b0463 3965
a7a4c60a
YW
3966 case MODE_RESOLVE_SERVICE:
3967 return translate("service", NULL, argc - optind, argv + optind, bus);
d55b0463 3968
a7a4c60a
YW
3969 case MODE_RESOLVE_OPENPGP:
3970 return translate("openpgp", NULL, argc - optind, argv + optind, bus);
be371fe0 3971
a7a4c60a
YW
3972 case MODE_RESOLVE_TLSA:
3973 return translate("tlsa", arg_service_family, argc - optind, argv + optind, bus);
be371fe0 3974
a7a4c60a
YW
3975 case MODE_STATISTICS:
3976 return translate("statistics", NULL, 0, NULL, bus);
3977
3978 case MODE_RESET_STATISTICS:
3979 return translate("reset-statistics", NULL, 0, NULL, bus);
3980
3981 case MODE_FLUSH_CACHES:
3982 return translate("flush-caches", NULL, 0, NULL, bus);
3983
3984 case MODE_RESET_SERVER_FEATURES:
3985 return translate("reset-server-features", NULL, 0, NULL, bus);
be371fe0 3986
a7a4c60a
YW
3987 case MODE_STATUS:
3988 return translate("status", NULL, argc - optind, argv + optind, bus);
14965b94 3989
14965b94 3990 case MODE_SET_LINK:
a661dc36
YW
3991 assert(arg_ifname);
3992
a7a4c60a
YW
3993 if (arg_set_dns) {
3994 r = translate("dns", arg_ifname, strv_length(arg_set_dns), arg_set_dns, bus);
3995 if (r < 0)
3996 return r;
3997 }
3998
3999 if (arg_set_domain) {
4000 r = translate("domain", arg_ifname, strv_length(arg_set_domain), arg_set_domain, bus);
4001 if (r < 0)
4002 return r;
14965b94
LP
4003 }
4004
a7a4c60a
YW
4005 if (arg_set_nta) {
4006 r = translate("nta", arg_ifname, strv_length(arg_set_nta), arg_set_nta, bus);
4007 if (r < 0)
4008 return r;
4009 }
14965b94 4010
a7a4c60a
YW
4011 if (arg_set_llmnr) {
4012 r = translate("llmnr", arg_ifname, 1, (char **) &arg_set_llmnr, bus);
4013 if (r < 0)
4014 return r;
4015 }
4016
4017 if (arg_set_mdns) {
4018 r = translate("mdns", arg_ifname, 1, (char **) &arg_set_mdns, bus);
4019 if (r < 0)
4020 return r;
4021 }
4022
c9299be2
IT
4023 if (arg_set_dns_over_tls) {
4024 r = translate("dnsovertls", arg_ifname, 1, (char **) &arg_set_dns_over_tls, bus);
d050561a
IT
4025 if (r < 0)
4026 return r;
4027 }
4028
a7a4c60a
YW
4029 if (arg_set_dnssec) {
4030 r = translate("dnssec", arg_ifname, 1, (char **) &arg_set_dnssec, bus);
4031 if (r < 0)
4032 return r;
14965b94
LP
4033 }
4034
a7a4c60a
YW
4035 return r;
4036
4037 case MODE_REVERT_LINK:
a661dc36
YW
4038 assert(arg_ifname);
4039
a7a4c60a 4040 return translate("revert", arg_ifname, 0, NULL, bus);
088c1363
LP
4041
4042 case _MODE_INVALID:
04499a70 4043 assert_not_reached();
bdef7319
ZJS
4044 }
4045
a7a4c60a
YW
4046 return 0;
4047}
4048
f6aa6190
YW
4049static int run(int argc, char **argv) {
4050 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
427eeb44 4051 bool compat = false;
a7a4c60a
YW
4052 int r;
4053
4054 setlocale(LC_ALL, "");
d2acb93d 4055 log_setup();
a7a4c60a 4056
427eeb44
YW
4057 if (invoked_as(argv, "resolvconf")) {
4058 compat = true;
a7a4c60a 4059 r = resolvconf_parse_argv(argc, argv);
427eeb44
YW
4060 } else if (invoked_as(argv, "systemd-resolve")) {
4061 compat = true;
a7a4c60a 4062 r = compat_parse_argv(argc, argv);
427eeb44 4063 } else
a7a4c60a
YW
4064 r = native_parse_argv(argc, argv);
4065 if (r <= 0)
f6aa6190 4066 return r;
a7a4c60a
YW
4067
4068 r = sd_bus_open_system(&bus);
f6aa6190
YW
4069 if (r < 0)
4070 return log_error_errno(r, "sd_bus_open_system: %m");
a7a4c60a 4071
427eeb44 4072 if (compat)
f6aa6190 4073 return compat_main(argc, argv, bus);
be371fe0 4074
f6aa6190 4075 return native_main(argc, argv, bus);
bdef7319 4076}
f6aa6190
YW
4077
4078DEFINE_MAIN_FUNCTION(run);