]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolvectl.c
Merge pull request #15472 from keszybz/dbus-api-docs
[thirdparty/systemd.git] / src / resolve / resolvectl.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <getopt.h>
4 #include <locale.h>
5 #include <net/if.h>
6
7 #include "sd-bus.h"
8 #include "sd-netlink.h"
9
10 #include "af-list.h"
11 #include "alloc-util.h"
12 #include "bus-common-errors.h"
13 #include "bus-error.h"
14 #include "bus-util.h"
15 #include "dns-domain.h"
16 #include "escape.h"
17 #include "format-table.h"
18 #include "format-util.h"
19 #include "gcrypt-util.h"
20 #include "main-func.h"
21 #include "missing_network.h"
22 #include "netlink-util.h"
23 #include "pager.h"
24 #include "parse-util.h"
25 #include "pretty-print.h"
26 #include "resolvconf-compat.h"
27 #include "resolvectl.h"
28 #include "resolved-def.h"
29 #include "resolved-dns-packet.h"
30 #include "socket-netlink.h"
31 #include "stdio-util.h"
32 #include "string-table.h"
33 #include "strv.h"
34 #include "terminal-util.h"
35 #include "verbs.h"
36
37 static int arg_family = AF_UNSPEC;
38 static int arg_ifindex = 0;
39 static char *arg_ifname = NULL;
40 static uint16_t arg_type = 0;
41 static uint16_t arg_class = 0;
42 static bool arg_legend = true;
43 static uint64_t arg_flags = 0;
44 static PagerFlags arg_pager_flags = 0;
45 bool arg_ifindex_permissive = false; /* If true, don't generate an error if the specified interface index doesn't exist */
46 static const char *arg_service_family = NULL;
47
48 typedef enum RawType {
49 RAW_NONE,
50 RAW_PAYLOAD,
51 RAW_PACKET,
52 } RawType;
53 static RawType arg_raw = RAW_NONE;
54
55 ExecutionMode arg_mode = MODE_RESOLVE_HOST;
56
57 char **arg_set_dns = NULL;
58 char **arg_set_domain = NULL;
59 static const char *arg_set_llmnr = NULL;
60 static const char *arg_set_mdns = NULL;
61 static const char *arg_set_dns_over_tls = NULL;
62 static const char *arg_set_dnssec = NULL;
63 static char **arg_set_nta = NULL;
64
65 STATIC_DESTRUCTOR_REGISTER(arg_ifname, freep);
66 STATIC_DESTRUCTOR_REGISTER(arg_set_dns, strv_freep);
67 STATIC_DESTRUCTOR_REGISTER(arg_set_domain, strv_freep);
68 STATIC_DESTRUCTOR_REGISTER(arg_set_nta, strv_freep);
69
70 typedef enum StatusMode {
71 STATUS_ALL,
72 STATUS_DNS,
73 STATUS_DOMAIN,
74 STATUS_DEFAULT_ROUTE,
75 STATUS_LLMNR,
76 STATUS_MDNS,
77 STATUS_PRIVATE,
78 STATUS_DNSSEC,
79 STATUS_NTA,
80 } StatusMode;
81
82 int ifname_mangle(const char *s) {
83 _cleanup_free_ char *iface = NULL;
84 const char *dot;
85 int ifi;
86
87 assert(s);
88
89 dot = strchr(s, '.');
90 if (dot) {
91 log_debug("Ignoring protocol specifier '%s'.", dot + 1);
92 iface = strndup(s, dot - s);
93
94 } else
95 iface = strdup(s);
96 if (!iface)
97 return log_oom();
98
99 ifi = resolve_interface(NULL, iface);
100 if (ifi < 0) {
101 if (ifi == -ENODEV && arg_ifindex_permissive) {
102 log_debug("Interface '%s' not found, but -f specified, ignoring.", iface);
103 return 0; /* done */
104 }
105
106 return log_error_errno(ifi, "Failed to resolve interface \"%s\": %m", iface);
107 }
108
109 if (arg_ifindex > 0 && arg_ifindex != ifi)
110 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified multiple different interfaces. Refusing.");
111
112 arg_ifindex = ifi;
113 free_and_replace(arg_ifname, iface);
114
115 return 1;
116 }
117
118 static void print_source(uint64_t flags, usec_t rtt) {
119 char rtt_str[FORMAT_TIMESTAMP_MAX];
120
121 if (!arg_legend)
122 return;
123
124 if (flags == 0)
125 return;
126
127 printf("\n%s-- Information acquired via", ansi_grey());
128
129 if (flags != 0)
130 printf(" protocol%s%s%s%s%s",
131 flags & SD_RESOLVED_DNS ? " DNS" :"",
132 flags & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "",
133 flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "",
134 flags & SD_RESOLVED_MDNS_IPV4 ? " mDNS/IPv4" : "",
135 flags & SD_RESOLVED_MDNS_IPV6 ? " mDNS/IPv6" : "");
136
137 assert_se(format_timespan(rtt_str, sizeof(rtt_str), rtt, 100));
138
139 printf(" in %s.%s\n"
140 "%s-- Data is authenticated: %s%s\n",
141 rtt_str, ansi_normal(),
142 ansi_grey(), yes_no(flags & SD_RESOLVED_AUTHENTICATED), ansi_normal());
143 }
144
145 static void print_ifindex_comment(int printed_so_far, int ifindex) {
146 char ifname[IF_NAMESIZE + 1];
147
148 if (ifindex <= 0)
149 return;
150
151 if (!format_ifname(ifindex, ifname))
152 log_warning_errno(errno, "Failed to resolve interface name for index %i, ignoring: %m", ifindex);
153 else
154 printf("%*s%s-- link: %s%s",
155 60 > printed_so_far ? 60 - printed_so_far : 0, " ", /* Align comment to the 60th column */
156 ansi_grey(), ifname, ansi_normal());
157 }
158
159 static int resolve_host(sd_bus *bus, const char *name) {
160 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
161 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
162 const char *canonical = NULL;
163 unsigned c = 0;
164 uint64_t flags;
165 usec_t ts;
166 int r;
167
168 assert(name);
169
170 log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname);
171
172 r = sd_bus_message_new_method_call(
173 bus,
174 &req,
175 "org.freedesktop.resolve1",
176 "/org/freedesktop/resolve1",
177 "org.freedesktop.resolve1.Manager",
178 "ResolveHostname");
179 if (r < 0)
180 return bus_log_create_error(r);
181
182 r = sd_bus_message_append(req, "isit", arg_ifindex, name, arg_family, arg_flags);
183 if (r < 0)
184 return bus_log_create_error(r);
185
186 ts = now(CLOCK_MONOTONIC);
187
188 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
189 if (r < 0)
190 return log_error_errno(r, "%s: resolve call failed: %s", name, bus_error_message(&error, r));
191
192 ts = now(CLOCK_MONOTONIC) - ts;
193
194 r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
195 if (r < 0)
196 return bus_log_parse_error(r);
197
198 while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
199 _cleanup_free_ char *pretty = NULL;
200 int ifindex, family, k;
201 const void *a;
202 size_t sz;
203
204 assert_cc(sizeof(int) == sizeof(int32_t));
205
206 r = sd_bus_message_read(reply, "ii", &ifindex, &family);
207 if (r < 0)
208 return bus_log_parse_error(r);
209
210 r = sd_bus_message_read_array(reply, 'y', &a, &sz);
211 if (r < 0)
212 return bus_log_parse_error(r);
213
214 r = sd_bus_message_exit_container(reply);
215 if (r < 0)
216 return bus_log_parse_error(r);
217
218 if (!IN_SET(family, AF_INET, AF_INET6)) {
219 log_debug("%s: skipping entry with family %d (%s)", name, family, af_to_name(family) ?: "unknown");
220 continue;
221 }
222
223 if (sz != FAMILY_ADDRESS_SIZE(family)) {
224 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown");
225 return -EINVAL;
226 }
227
228 r = in_addr_ifindex_to_string(family, a, ifindex, &pretty);
229 if (r < 0)
230 return log_error_errno(r, "Failed to print address for %s: %m", name);
231
232 k = printf("%*s%s %s%s%s",
233 (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
234 ansi_highlight(), pretty, ansi_normal());
235
236 print_ifindex_comment(k, ifindex);
237 fputc('\n', stdout);
238
239 c++;
240 }
241 if (r < 0)
242 return bus_log_parse_error(r);
243
244 r = sd_bus_message_exit_container(reply);
245 if (r < 0)
246 return bus_log_parse_error(r);
247
248 r = sd_bus_message_read(reply, "st", &canonical, &flags);
249 if (r < 0)
250 return bus_log_parse_error(r);
251
252 if (!streq(name, canonical))
253 printf("%*s%s (%s)\n",
254 (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
255 canonical);
256
257 if (c == 0) {
258 log_error("%s: no addresses found", name);
259 return -ESRCH;
260 }
261
262 print_source(flags, ts);
263
264 return 0;
265 }
266
267 static int resolve_address(sd_bus *bus, int family, const union in_addr_union *address, int ifindex) {
268 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
269 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
270 _cleanup_free_ char *pretty = NULL;
271 uint64_t flags;
272 unsigned c = 0;
273 usec_t ts;
274 int r;
275
276 assert(bus);
277 assert(IN_SET(family, AF_INET, AF_INET6));
278 assert(address);
279
280 if (ifindex <= 0)
281 ifindex = arg_ifindex;
282
283 r = in_addr_ifindex_to_string(family, address, ifindex, &pretty);
284 if (r < 0)
285 return log_oom();
286
287 log_debug("Resolving %s.", pretty);
288
289 r = sd_bus_message_new_method_call(
290 bus,
291 &req,
292 "org.freedesktop.resolve1",
293 "/org/freedesktop/resolve1",
294 "org.freedesktop.resolve1.Manager",
295 "ResolveAddress");
296 if (r < 0)
297 return bus_log_create_error(r);
298
299 r = sd_bus_message_append(req, "ii", ifindex, family);
300 if (r < 0)
301 return bus_log_create_error(r);
302
303 r = sd_bus_message_append_array(req, 'y', address, FAMILY_ADDRESS_SIZE(family));
304 if (r < 0)
305 return bus_log_create_error(r);
306
307 r = sd_bus_message_append(req, "t", arg_flags);
308 if (r < 0)
309 return bus_log_create_error(r);
310
311 ts = now(CLOCK_MONOTONIC);
312
313 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
314 if (r < 0)
315 return log_error_errno(r, "%s: resolve call failed: %s", pretty, bus_error_message(&error, r));
316
317 ts = now(CLOCK_MONOTONIC) - ts;
318
319 r = sd_bus_message_enter_container(reply, 'a', "(is)");
320 if (r < 0)
321 return bus_log_create_error(r);
322
323 while ((r = sd_bus_message_enter_container(reply, 'r', "is")) > 0) {
324 const char *n;
325 int k;
326
327 assert_cc(sizeof(int) == sizeof(int32_t));
328
329 r = sd_bus_message_read(reply, "is", &ifindex, &n);
330 if (r < 0)
331 return r;
332
333 r = sd_bus_message_exit_container(reply);
334 if (r < 0)
335 return r;
336
337 k = printf("%*s%s %s%s%s",
338 (int) strlen(pretty), c == 0 ? pretty : "",
339 c == 0 ? ":" : " ",
340 ansi_highlight(), n, ansi_normal());
341
342 print_ifindex_comment(k, ifindex);
343 fputc('\n', stdout);
344
345 c++;
346 }
347 if (r < 0)
348 return bus_log_parse_error(r);
349
350 r = sd_bus_message_exit_container(reply);
351 if (r < 0)
352 return bus_log_parse_error(r);
353
354 r = sd_bus_message_read(reply, "t", &flags);
355 if (r < 0)
356 return bus_log_parse_error(r);
357
358 if (c == 0) {
359 log_error("%s: no names found", pretty);
360 return -ESRCH;
361 }
362
363 print_source(flags, ts);
364
365 return 0;
366 }
367
368 static int output_rr_packet(const void *d, size_t l, int ifindex) {
369 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
370 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
371 int r;
372
373 r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX);
374 if (r < 0)
375 return log_oom();
376
377 p->refuse_compression = true;
378
379 r = dns_packet_append_blob(p, d, l, NULL);
380 if (r < 0)
381 return log_oom();
382
383 r = dns_packet_read_rr(p, &rr, NULL, NULL);
384 if (r < 0)
385 return log_error_errno(r, "Failed to parse RR: %m");
386
387 if (arg_raw == RAW_PAYLOAD) {
388 void *data;
389 ssize_t k;
390
391 k = dns_resource_record_payload(rr, &data);
392 if (k < 0)
393 return log_error_errno(k, "Cannot dump RR: %m");
394 fwrite(data, 1, k, stdout);
395 } else {
396 const char *s;
397 int k;
398
399 s = dns_resource_record_to_string(rr);
400 if (!s)
401 return log_oom();
402
403 k = printf("%s", s);
404 print_ifindex_comment(k, ifindex);
405 fputc('\n', stdout);
406 }
407
408 return 0;
409 }
410
411 static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type, bool warn_missing) {
412 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
413 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
414 unsigned n = 0;
415 uint64_t flags;
416 int r;
417 usec_t ts;
418 bool needs_authentication = false;
419
420 assert(name);
421
422 log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(class), dns_type_to_string(type), isempty(arg_ifname) ? "*" : arg_ifname);
423
424 r = sd_bus_message_new_method_call(
425 bus,
426 &req,
427 "org.freedesktop.resolve1",
428 "/org/freedesktop/resolve1",
429 "org.freedesktop.resolve1.Manager",
430 "ResolveRecord");
431 if (r < 0)
432 return bus_log_create_error(r);
433
434 r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, class, type, arg_flags);
435 if (r < 0)
436 return bus_log_create_error(r);
437
438 ts = now(CLOCK_MONOTONIC);
439
440 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
441 if (r < 0) {
442 if (warn_missing || r != -ENXIO)
443 log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r));
444 return r;
445 }
446
447 ts = now(CLOCK_MONOTONIC) - ts;
448
449 r = sd_bus_message_enter_container(reply, 'a', "(iqqay)");
450 if (r < 0)
451 return bus_log_parse_error(r);
452
453 while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) {
454 uint16_t c, t;
455 int ifindex;
456 const void *d;
457 size_t l;
458
459 assert_cc(sizeof(int) == sizeof(int32_t));
460
461 r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t);
462 if (r < 0)
463 return bus_log_parse_error(r);
464
465 r = sd_bus_message_read_array(reply, 'y', &d, &l);
466 if (r < 0)
467 return bus_log_parse_error(r);
468
469 r = sd_bus_message_exit_container(reply);
470 if (r < 0)
471 return bus_log_parse_error(r);
472
473 if (arg_raw == RAW_PACKET) {
474 uint64_t u64 = htole64(l);
475
476 fwrite(&u64, sizeof(u64), 1, stdout);
477 fwrite(d, 1, l, stdout);
478 } else {
479 r = output_rr_packet(d, l, ifindex);
480 if (r < 0)
481 return r;
482 }
483
484 if (dns_type_needs_authentication(t))
485 needs_authentication = true;
486
487 n++;
488 }
489 if (r < 0)
490 return bus_log_parse_error(r);
491
492 r = sd_bus_message_exit_container(reply);
493 if (r < 0)
494 return bus_log_parse_error(r);
495
496 r = sd_bus_message_read(reply, "t", &flags);
497 if (r < 0)
498 return bus_log_parse_error(r);
499
500 if (n == 0) {
501 if (warn_missing)
502 log_error("%s: no records found", name);
503 return -ESRCH;
504 }
505
506 print_source(flags, ts);
507
508 if ((flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) {
509 fflush(stdout);
510
511 fprintf(stderr, "\n%s"
512 "WARNING: The resources shown contain cryptographic key data which could not be\n"
513 " authenticated. It is not suitable to authenticate any communication.\n"
514 " This is usually indication that DNSSEC authentication was not enabled\n"
515 " or is not available for the selected protocol or DNS servers.%s\n",
516 ansi_highlight_red(),
517 ansi_normal());
518 }
519
520 return 0;
521 }
522
523 static int resolve_rfc4501(sd_bus *bus, const char *name) {
524 uint16_t type = 0, class = 0;
525 const char *p, *q, *n;
526 int r;
527
528 assert(bus);
529 assert(name);
530 assert(startswith(name, "dns:"));
531
532 /* Parse RFC 4501 dns: URIs */
533
534 p = name + 4;
535
536 if (p[0] == '/') {
537 const char *e;
538
539 if (p[1] != '/')
540 goto invalid;
541
542 e = strchr(p + 2, '/');
543 if (!e)
544 goto invalid;
545
546 if (e != p + 2)
547 log_warning("DNS authority specification not supported; ignoring specified authority.");
548
549 p = e + 1;
550 }
551
552 q = strchr(p, '?');
553 if (q) {
554 n = strndupa(p, q - p);
555 q++;
556
557 for (;;) {
558 const char *f;
559
560 f = startswith_no_case(q, "class=");
561 if (f) {
562 _cleanup_free_ char *t = NULL;
563 const char *e;
564
565 if (class != 0)
566 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
567 "DNS class specified twice.");
568
569 e = strchrnul(f, ';');
570 t = strndup(f, e - f);
571 if (!t)
572 return log_oom();
573
574 r = dns_class_from_string(t);
575 if (r < 0)
576 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
577 "Unknown DNS class %s.", t);
578
579 class = r;
580
581 if (*e == ';') {
582 q = e + 1;
583 continue;
584 }
585
586 break;
587 }
588
589 f = startswith_no_case(q, "type=");
590 if (f) {
591 _cleanup_free_ char *t = NULL;
592 const char *e;
593
594 if (type != 0)
595 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
596 "DNS type specified twice.");
597
598 e = strchrnul(f, ';');
599 t = strndup(f, e - f);
600 if (!t)
601 return log_oom();
602
603 r = dns_type_from_string(t);
604 if (r < 0)
605 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
606 "Unknown DNS type %s.", t);
607
608 type = r;
609
610 if (*e == ';') {
611 q = e + 1;
612 continue;
613 }
614
615 break;
616 }
617
618 goto invalid;
619 }
620 } else
621 n = p;
622
623 if (class == 0)
624 class = arg_class ?: DNS_CLASS_IN;
625 if (type == 0)
626 type = arg_type ?: DNS_TYPE_A;
627
628 return resolve_record(bus, n, class, type, true);
629
630 invalid:
631 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
632 "Invalid DNS URI: %s", name);
633 }
634
635 static int verb_query(int argc, char **argv, void *userdata) {
636 sd_bus *bus = userdata;
637 char **p;
638 int q, r = 0;
639
640 if (arg_type != 0)
641 STRV_FOREACH(p, argv + 1) {
642 q = resolve_record(bus, *p, arg_class, arg_type, true);
643 if (q < 0)
644 r = q;
645 }
646
647 else
648 STRV_FOREACH(p, argv + 1) {
649 if (startswith(*p, "dns:"))
650 q = resolve_rfc4501(bus, *p);
651 else {
652 int family, ifindex;
653 union in_addr_union a;
654
655 q = in_addr_ifindex_from_string_auto(*p, &family, &a, &ifindex);
656 if (q >= 0)
657 q = resolve_address(bus, family, &a, ifindex);
658 else
659 q = resolve_host(bus, *p);
660 }
661 if (q < 0)
662 r = q;
663 }
664
665 return r;
666 }
667
668 static int resolve_service(sd_bus *bus, const char *name, const char *type, const char *domain) {
669 const char *canonical_name, *canonical_type, *canonical_domain;
670 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
671 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
672 size_t indent, sz;
673 uint64_t flags;
674 const char *p;
675 unsigned c;
676 usec_t ts;
677 int r;
678
679 assert(bus);
680 assert(domain);
681
682 name = empty_to_null(name);
683 type = empty_to_null(type);
684
685 if (name)
686 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);
687 else if (type)
688 log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname);
689 else
690 log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname);
691
692 r = sd_bus_message_new_method_call(
693 bus,
694 &req,
695 "org.freedesktop.resolve1",
696 "/org/freedesktop/resolve1",
697 "org.freedesktop.resolve1.Manager",
698 "ResolveService");
699 if (r < 0)
700 return bus_log_create_error(r);
701
702 r = sd_bus_message_append(req, "isssit", arg_ifindex, name, type, domain, arg_family, arg_flags);
703 if (r < 0)
704 return bus_log_create_error(r);
705
706 ts = now(CLOCK_MONOTONIC);
707
708 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
709 if (r < 0)
710 return log_error_errno(r, "Resolve call failed: %s", bus_error_message(&error, r));
711
712 ts = now(CLOCK_MONOTONIC) - ts;
713
714 r = sd_bus_message_enter_container(reply, 'a', "(qqqsa(iiay)s)");
715 if (r < 0)
716 return bus_log_parse_error(r);
717
718 indent =
719 (name ? strlen(name) + 1 : 0) +
720 (type ? strlen(type) + 1 : 0) +
721 strlen(domain) + 2;
722
723 c = 0;
724 while ((r = sd_bus_message_enter_container(reply, 'r', "qqqsa(iiay)s")) > 0) {
725 uint16_t priority, weight, port;
726 const char *hostname, *canonical;
727
728 r = sd_bus_message_read(reply, "qqqs", &priority, &weight, &port, &hostname);
729 if (r < 0)
730 return bus_log_parse_error(r);
731
732 if (name)
733 printf("%*s%s", (int) strlen(name), c == 0 ? name : "", c == 0 ? "/" : " ");
734 if (type)
735 printf("%*s%s", (int) strlen(type), c == 0 ? type : "", c == 0 ? "/" : " ");
736
737 printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
738 (int) strlen(domain), c == 0 ? domain : "",
739 c == 0 ? ":" : " ",
740 hostname, port,
741 priority, weight);
742
743 r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
744 if (r < 0)
745 return bus_log_parse_error(r);
746
747 while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
748 _cleanup_free_ char *pretty = NULL;
749 int ifindex, family, k;
750 const void *a;
751
752 assert_cc(sizeof(int) == sizeof(int32_t));
753
754 r = sd_bus_message_read(reply, "ii", &ifindex, &family);
755 if (r < 0)
756 return bus_log_parse_error(r);
757
758 r = sd_bus_message_read_array(reply, 'y', &a, &sz);
759 if (r < 0)
760 return bus_log_parse_error(r);
761
762 r = sd_bus_message_exit_container(reply);
763 if (r < 0)
764 return bus_log_parse_error(r);
765
766 if (!IN_SET(family, AF_INET, AF_INET6)) {
767 log_debug("%s: skipping entry with family %d (%s)", name, family, af_to_name(family) ?: "unknown");
768 continue;
769 }
770
771 if (sz != FAMILY_ADDRESS_SIZE(family)) {
772 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown");
773 return -EINVAL;
774 }
775
776 r = in_addr_ifindex_to_string(family, a, ifindex, &pretty);
777 if (r < 0)
778 return log_error_errno(r, "Failed to print address for %s: %m", name);
779
780 k = printf("%*s%s", (int) indent, "", pretty);
781 print_ifindex_comment(k, ifindex);
782 fputc('\n', stdout);
783 }
784 if (r < 0)
785 return bus_log_parse_error(r);
786
787 r = sd_bus_message_exit_container(reply);
788 if (r < 0)
789 return bus_log_parse_error(r);
790
791 r = sd_bus_message_read(reply, "s", &canonical);
792 if (r < 0)
793 return bus_log_parse_error(r);
794
795 if (!streq(hostname, canonical))
796 printf("%*s(%s)\n", (int) indent, "", canonical);
797
798 r = sd_bus_message_exit_container(reply);
799 if (r < 0)
800 return bus_log_parse_error(r);
801
802 c++;
803 }
804 if (r < 0)
805 return bus_log_parse_error(r);
806
807 r = sd_bus_message_exit_container(reply);
808 if (r < 0)
809 return bus_log_parse_error(r);
810
811 r = sd_bus_message_enter_container(reply, 'a', "ay");
812 if (r < 0)
813 return bus_log_parse_error(r);
814
815 while ((r = sd_bus_message_read_array(reply, 'y', (const void**) &p, &sz)) > 0) {
816 _cleanup_free_ char *escaped = NULL;
817
818 escaped = cescape_length(p, sz);
819 if (!escaped)
820 return log_oom();
821
822 printf("%*s%s\n", (int) indent, "", escaped);
823 }
824 if (r < 0)
825 return bus_log_parse_error(r);
826
827 r = sd_bus_message_exit_container(reply);
828 if (r < 0)
829 return bus_log_parse_error(r);
830
831 r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags);
832 if (r < 0)
833 return bus_log_parse_error(r);
834
835 canonical_name = empty_to_null(canonical_name);
836 canonical_type = empty_to_null(canonical_type);
837
838 if (!streq_ptr(name, canonical_name) ||
839 !streq_ptr(type, canonical_type) ||
840 !streq_ptr(domain, canonical_domain)) {
841
842 printf("%*s(", (int) indent, "");
843
844 if (canonical_name)
845 printf("%s/", canonical_name);
846 if (canonical_type)
847 printf("%s/", canonical_type);
848
849 printf("%s)\n", canonical_domain);
850 }
851
852 print_source(flags, ts);
853
854 return 0;
855 }
856
857 static int verb_service(int argc, char **argv, void *userdata) {
858 sd_bus *bus = userdata;
859
860 if (argc == 2)
861 return resolve_service(bus, NULL, NULL, argv[1]);
862 else if (argc == 3)
863 return resolve_service(bus, NULL, argv[1], argv[2]);
864 else
865 return resolve_service(bus, argv[1], argv[2], argv[3]);
866 }
867
868 static int resolve_openpgp(sd_bus *bus, const char *address) {
869 const char *domain, *full;
870 int r;
871 _cleanup_free_ char *hashed = NULL;
872
873 assert(bus);
874 assert(address);
875
876 domain = strrchr(address, '@');
877 if (!domain)
878 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
879 "Address does not contain '@': \"%s\"", address);
880 if (domain == address || domain[1] == '\0')
881 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
882 "Address starts or ends with '@': \"%s\"", address);
883 domain++;
884
885 r = string_hashsum_sha256(address, domain - 1 - address, &hashed);
886 if (r < 0)
887 return log_error_errno(r, "Hashing failed: %m");
888
889 strshorten(hashed, 56);
890
891 full = strjoina(hashed, "._openpgpkey.", domain);
892 log_debug("Looking up \"%s\".", full);
893
894 r = resolve_record(bus, full,
895 arg_class ?: DNS_CLASS_IN,
896 arg_type ?: DNS_TYPE_OPENPGPKEY, false);
897
898 if (IN_SET(r, -ENXIO, -ESRCH)) { /* NXDOMAIN or NODATA? */
899 hashed = mfree(hashed);
900 r = string_hashsum_sha224(address, domain - 1 - address, &hashed);
901 if (r < 0)
902 return log_error_errno(r, "Hashing failed: %m");
903
904 full = strjoina(hashed, "._openpgpkey.", domain);
905 log_debug("Looking up \"%s\".", full);
906
907 return resolve_record(bus, full,
908 arg_class ?: DNS_CLASS_IN,
909 arg_type ?: DNS_TYPE_OPENPGPKEY, true);
910 }
911
912 return r;
913 }
914
915 static int verb_openpgp(int argc, char **argv, void *userdata) {
916 sd_bus *bus = userdata;
917 char **p;
918 int q, r = 0;
919
920 STRV_FOREACH(p, argv + 1) {
921 q = resolve_openpgp(bus, *p);
922 if (q < 0)
923 r = q;
924 }
925
926 return r;
927 }
928
929 static int resolve_tlsa(sd_bus *bus, const char *family, const char *address) {
930 const char *port;
931 uint16_t port_num = 443;
932 _cleanup_free_ char *full = NULL;
933 int r;
934
935 assert(bus);
936 assert(address);
937
938 port = strrchr(address, ':');
939 if (port) {
940 r = parse_ip_port(port + 1, &port_num);
941 if (r < 0)
942 return log_error_errno(r, "Invalid port \"%s\".", port + 1);
943
944 address = strndupa(address, port - address);
945 }
946
947 r = asprintf(&full, "_%u._%s.%s",
948 port_num,
949 family,
950 address);
951 if (r < 0)
952 return log_oom();
953
954 log_debug("Looking up \"%s\".", full);
955
956 return resolve_record(bus, full,
957 arg_class ?: DNS_CLASS_IN,
958 arg_type ?: DNS_TYPE_TLSA, true);
959 }
960
961 static bool service_family_is_valid(const char *s) {
962 return STR_IN_SET(s, "tcp", "udp", "sctp");
963 }
964
965 static int verb_tlsa(int argc, char **argv, void *userdata) {
966 sd_bus *bus = userdata;
967 char **p, **args = argv + 1;
968 const char *family = "tcp";
969 int q, r = 0;
970
971 if (service_family_is_valid(argv[1])) {
972 family = argv[1];
973 args++;
974 }
975
976 STRV_FOREACH(p, args) {
977 q = resolve_tlsa(bus, family, *p);
978 if (q < 0)
979 r = q;
980 }
981
982 return r;
983 }
984
985 static int show_statistics(int argc, char **argv, void *userdata) {
986 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
987 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
988 _cleanup_(table_unrefp) Table *table = NULL;
989 sd_bus *bus = userdata;
990 uint64_t n_current_transactions, n_total_transactions,
991 cache_size, n_cache_hit, n_cache_miss,
992 n_dnssec_secure, n_dnssec_insecure, n_dnssec_bogus, n_dnssec_indeterminate;
993 int r, dnssec_supported;
994
995 assert(bus);
996
997 r = sd_bus_get_property_trivial(bus,
998 "org.freedesktop.resolve1",
999 "/org/freedesktop/resolve1",
1000 "org.freedesktop.resolve1.Manager",
1001 "DNSSECSupported",
1002 &error,
1003 'b',
1004 &dnssec_supported);
1005 if (r < 0)
1006 return log_error_errno(r, "Failed to get DNSSEC supported state: %s", bus_error_message(&error, r));
1007
1008 printf("DNSSEC supported by current servers: %s%s%s\n\n",
1009 ansi_highlight(),
1010 yes_no(dnssec_supported),
1011 ansi_normal());
1012
1013 r = sd_bus_get_property(bus,
1014 "org.freedesktop.resolve1",
1015 "/org/freedesktop/resolve1",
1016 "org.freedesktop.resolve1.Manager",
1017 "TransactionStatistics",
1018 &error,
1019 &reply,
1020 "(tt)");
1021 if (r < 0)
1022 return log_error_errno(r, "Failed to get transaction statistics: %s", bus_error_message(&error, r));
1023
1024 r = sd_bus_message_read(reply, "(tt)",
1025 &n_current_transactions,
1026 &n_total_transactions);
1027 if (r < 0)
1028 return bus_log_parse_error(r);
1029
1030 reply = sd_bus_message_unref(reply);
1031
1032 r = sd_bus_get_property(bus,
1033 "org.freedesktop.resolve1",
1034 "/org/freedesktop/resolve1",
1035 "org.freedesktop.resolve1.Manager",
1036 "CacheStatistics",
1037 &error,
1038 &reply,
1039 "(ttt)");
1040 if (r < 0)
1041 return log_error_errno(r, "Failed to get cache statistics: %s", bus_error_message(&error, r));
1042
1043 r = sd_bus_message_read(reply, "(ttt)",
1044 &cache_size,
1045 &n_cache_hit,
1046 &n_cache_miss);
1047 if (r < 0)
1048 return bus_log_parse_error(r);
1049
1050 reply = sd_bus_message_unref(reply);
1051
1052 r = sd_bus_get_property(bus,
1053 "org.freedesktop.resolve1",
1054 "/org/freedesktop/resolve1",
1055 "org.freedesktop.resolve1.Manager",
1056 "DNSSECStatistics",
1057 &error,
1058 &reply,
1059 "(tttt)");
1060 if (r < 0)
1061 return log_error_errno(r, "Failed to get DNSSEC statistics: %s", bus_error_message(&error, r));
1062
1063 r = sd_bus_message_read(reply, "(tttt)",
1064 &n_dnssec_secure,
1065 &n_dnssec_insecure,
1066 &n_dnssec_bogus,
1067 &n_dnssec_indeterminate);
1068 if (r < 0)
1069 return bus_log_parse_error(r);
1070
1071 table = table_new("key", "value");
1072 if (!table)
1073 return log_oom();
1074
1075 table_set_header(table, false);
1076
1077 r = table_add_many(table,
1078 TABLE_STRING, "Transactions",
1079 TABLE_SET_COLOR, ansi_highlight(),
1080 TABLE_EMPTY,
1081 TABLE_STRING, "Current Transactions:",
1082 TABLE_SET_ALIGN_PERCENT, 100,
1083 TABLE_UINT64, n_current_transactions,
1084 TABLE_STRING, "Total Transactions:",
1085 TABLE_UINT64, n_total_transactions,
1086 TABLE_EMPTY, TABLE_EMPTY,
1087 TABLE_STRING, "Cache",
1088 TABLE_SET_COLOR, ansi_highlight(),
1089 TABLE_SET_ALIGN_PERCENT, 0,
1090 TABLE_EMPTY,
1091 TABLE_STRING, "Current Cache Size:",
1092 TABLE_SET_ALIGN_PERCENT, 100,
1093 TABLE_UINT64, cache_size,
1094 TABLE_STRING, "Cache Hits:",
1095 TABLE_UINT64, n_cache_hit,
1096 TABLE_STRING, "Cache Misses:",
1097 TABLE_UINT64, n_cache_miss,
1098 TABLE_EMPTY, TABLE_EMPTY,
1099 TABLE_STRING, "DNSSEC Verdicts",
1100 TABLE_SET_COLOR, ansi_highlight(),
1101 TABLE_SET_ALIGN_PERCENT, 0,
1102 TABLE_EMPTY,
1103 TABLE_STRING, "Secure:",
1104 TABLE_SET_ALIGN_PERCENT, 100,
1105 TABLE_UINT64, n_dnssec_secure,
1106 TABLE_STRING, "Insecure:",
1107 TABLE_UINT64, n_dnssec_insecure,
1108 TABLE_STRING, "Bogus:",
1109 TABLE_UINT64, n_dnssec_bogus,
1110 TABLE_STRING, "Indeterminate:",
1111 TABLE_UINT64, n_dnssec_indeterminate);
1112 if (r < 0)
1113 table_log_add_error(r);
1114
1115 r = table_print(table, NULL);
1116 if (r < 0)
1117 return log_error_errno(r, "Failed to print table: %m");
1118
1119 return 0;
1120 }
1121
1122 static int reset_statistics(int argc, char **argv, void *userdata) {
1123 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1124 sd_bus *bus = userdata;
1125 int r;
1126
1127 r = sd_bus_call_method(bus,
1128 "org.freedesktop.resolve1",
1129 "/org/freedesktop/resolve1",
1130 "org.freedesktop.resolve1.Manager",
1131 "ResetStatistics",
1132 &error,
1133 NULL,
1134 NULL);
1135 if (r < 0)
1136 return log_error_errno(r, "Failed to reset statistics: %s", bus_error_message(&error, r));
1137
1138 return 0;
1139 }
1140
1141 static int flush_caches(int argc, char **argv, void *userdata) {
1142 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1143 sd_bus *bus = userdata;
1144 int r;
1145
1146 r = sd_bus_call_method(bus,
1147 "org.freedesktop.resolve1",
1148 "/org/freedesktop/resolve1",
1149 "org.freedesktop.resolve1.Manager",
1150 "FlushCaches",
1151 &error,
1152 NULL,
1153 NULL);
1154 if (r < 0)
1155 return log_error_errno(r, "Failed to flush caches: %s", bus_error_message(&error, r));
1156
1157 return 0;
1158 }
1159
1160 static int reset_server_features(int argc, char **argv, void *userdata) {
1161 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1162 sd_bus *bus = userdata;
1163 int r;
1164
1165 r = sd_bus_call_method(bus,
1166 "org.freedesktop.resolve1",
1167 "/org/freedesktop/resolve1",
1168 "org.freedesktop.resolve1.Manager",
1169 "ResetServerFeatures",
1170 &error,
1171 NULL,
1172 NULL);
1173 if (r < 0)
1174 return log_error_errno(r, "Failed to reset server features: %s", bus_error_message(&error, r));
1175
1176 return 0;
1177 }
1178
1179 static int read_dns_server_one(sd_bus_message *m, bool with_ifindex, char **ret) {
1180 _cleanup_free_ char *pretty = NULL;
1181 int ifindex, family, r;
1182 const void *a;
1183 size_t sz;
1184
1185 assert(m);
1186 assert(ret);
1187
1188 r = sd_bus_message_enter_container(m, 'r', with_ifindex ? "iiay" : "iay");
1189 if (r <= 0)
1190 return r;
1191
1192 if (with_ifindex) {
1193 r = sd_bus_message_read(m, "i", &ifindex);
1194 if (r < 0)
1195 return r;
1196 }
1197
1198 r = sd_bus_message_read(m, "i", &family);
1199 if (r < 0)
1200 return r;
1201
1202 r = sd_bus_message_read_array(m, 'y', &a, &sz);
1203 if (r < 0)
1204 return r;
1205
1206 r = sd_bus_message_exit_container(m);
1207 if (r < 0)
1208 return r;
1209
1210 if (with_ifindex && ifindex != 0) {
1211 /* only show the global ones here */
1212 *ret = NULL;
1213 return 1;
1214 }
1215
1216 if (!IN_SET(family, AF_INET, AF_INET6)) {
1217 log_debug("Unexpected family, ignoring: %i", family);
1218
1219 *ret = NULL;
1220 return 1;
1221 }
1222
1223 if (sz != FAMILY_ADDRESS_SIZE(family)) {
1224 log_debug("Address size mismatch, ignoring.");
1225
1226 *ret = NULL;
1227 return 1;
1228 }
1229
1230 r = in_addr_to_string(family, a, &pretty);
1231 if (r < 0)
1232 return r;
1233
1234 *ret = TAKE_PTR(pretty);
1235
1236 return 1;
1237 }
1238
1239 static int map_link_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1240 char ***l = userdata;
1241 int r;
1242
1243 assert(bus);
1244 assert(member);
1245 assert(m);
1246 assert(l);
1247
1248 r = sd_bus_message_enter_container(m, 'a', "(iay)");
1249 if (r < 0)
1250 return r;
1251
1252 for (;;) {
1253 _cleanup_free_ char *pretty = NULL;
1254
1255 r = read_dns_server_one(m, false, &pretty);
1256 if (r < 0)
1257 return r;
1258 if (r == 0)
1259 break;
1260
1261 if (isempty(pretty))
1262 continue;
1263
1264 r = strv_consume(l, TAKE_PTR(pretty));
1265 if (r < 0)
1266 return r;
1267 }
1268
1269 r = sd_bus_message_exit_container(m);
1270 if (r < 0)
1271 return r;
1272
1273 return 0;
1274 }
1275
1276 static int map_link_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1277 assert(m);
1278 assert(userdata);
1279
1280 return read_dns_server_one(m, false, userdata);
1281 }
1282
1283 static int read_domain_one(sd_bus_message *m, bool with_ifindex, char **ret) {
1284 _cleanup_free_ char *str = NULL;
1285 int ifindex, route_only, r;
1286 const char *domain;
1287
1288 assert(m);
1289 assert(ret);
1290
1291 if (with_ifindex)
1292 r = sd_bus_message_read(m, "(isb)", &ifindex, &domain, &route_only);
1293 else
1294 r = sd_bus_message_read(m, "(sb)", &domain, &route_only);
1295 if (r <= 0)
1296 return r;
1297
1298 if (with_ifindex && ifindex != 0) {
1299 /* only show the global ones here */
1300 *ret = NULL;
1301 return 1;
1302 }
1303
1304 if (route_only)
1305 str = strjoin("~", domain);
1306 else
1307 str = strdup(domain);
1308 if (!str)
1309 return -ENOMEM;
1310
1311 *ret = TAKE_PTR(str);
1312
1313 return 1;
1314 }
1315
1316 static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1317 char ***l = userdata;
1318 int r;
1319
1320 assert(bus);
1321 assert(member);
1322 assert(m);
1323 assert(l);
1324
1325 r = sd_bus_message_enter_container(m, 'a', "(sb)");
1326 if (r < 0)
1327 return r;
1328
1329 for (;;) {
1330 _cleanup_free_ char *pretty = NULL;
1331
1332 r = read_domain_one(m, false, &pretty);
1333 if (r < 0)
1334 return r;
1335 if (r == 0)
1336 break;
1337
1338 if (isempty(pretty))
1339 continue;
1340
1341 r = strv_consume(l, TAKE_PTR(pretty));
1342 if (r < 0)
1343 return r;
1344 }
1345
1346 r = sd_bus_message_exit_container(m);
1347 if (r < 0)
1348 return r;
1349
1350 return 0;
1351 }
1352
1353 static int status_print_strv_ifindex(int ifindex, const char *ifname, char **p) {
1354 char **i;
1355
1356 printf("%sLink %i (%s)%s:",
1357 ansi_highlight(), ifindex, ifname, ansi_normal());
1358
1359 STRV_FOREACH(i, p)
1360 printf(" %s", *i);
1361
1362 printf("\n");
1363
1364 return 0;
1365 }
1366
1367 struct link_info {
1368 uint64_t scopes_mask;
1369 const char *llmnr;
1370 const char *mdns;
1371 const char *dns_over_tls;
1372 const char *dnssec;
1373 char *current_dns;
1374 char **dns;
1375 char **domains;
1376 char **ntas;
1377 bool dnssec_supported;
1378 bool default_route;
1379 };
1380
1381 static void link_info_clear(struct link_info *p) {
1382 free(p->current_dns);
1383 strv_free(p->dns);
1384 strv_free(p->domains);
1385 strv_free(p->ntas);
1386 }
1387
1388 static int dump_list(Table *table, const char *prefix, char * const *l) {
1389 int r;
1390
1391 if (strv_isempty(l))
1392 return 0;
1393
1394 r = table_add_many(table,
1395 TABLE_STRING, prefix,
1396 TABLE_STRV, l);
1397 if (r < 0)
1398 return table_log_add_error(r);
1399
1400 return 0;
1401 }
1402
1403 static int status_ifindex(sd_bus *bus, int ifindex, const char *name, StatusMode mode, bool *empty_line) {
1404 static const struct bus_properties_map property_map[] = {
1405 { "ScopesMask", "t", NULL, offsetof(struct link_info, scopes_mask) },
1406 { "DNS", "a(iay)", map_link_dns_servers, offsetof(struct link_info, dns) },
1407 { "CurrentDNSServer", "(iay)", map_link_current_dns_server, offsetof(struct link_info, current_dns) },
1408 { "Domains", "a(sb)", map_link_domains, offsetof(struct link_info, domains) },
1409 { "DefaultRoute", "b", NULL, offsetof(struct link_info, default_route) },
1410 { "LLMNR", "s", NULL, offsetof(struct link_info, llmnr) },
1411 { "MulticastDNS", "s", NULL, offsetof(struct link_info, mdns) },
1412 { "DNSOverTLS", "s", NULL, offsetof(struct link_info, dns_over_tls) },
1413 { "DNSSEC", "s", NULL, offsetof(struct link_info, dnssec) },
1414 { "DNSSECNegativeTrustAnchors", "as", NULL, offsetof(struct link_info, ntas) },
1415 { "DNSSECSupported", "b", NULL, offsetof(struct link_info, dnssec_supported) },
1416 {}
1417 };
1418 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1419 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1420 _cleanup_(link_info_clear) struct link_info link_info = {};
1421 _cleanup_(table_unrefp) Table *table = NULL;
1422 _cleanup_free_ char *p = NULL;
1423 char ifi[DECIMAL_STR_MAX(int)], ifname[IF_NAMESIZE + 1] = "";
1424 int r;
1425
1426 assert(bus);
1427 assert(ifindex > 0);
1428
1429 if (!name) {
1430 if (!format_ifname(ifindex, ifname))
1431 return log_error_errno(errno, "Failed to resolve interface name for %i: %m", ifindex);
1432
1433 name = ifname;
1434 }
1435
1436 xsprintf(ifi, "%i", ifindex);
1437 r = sd_bus_path_encode("/org/freedesktop/resolve1/link", ifi, &p);
1438 if (r < 0)
1439 return log_oom();
1440
1441 r = bus_map_all_properties(bus,
1442 "org.freedesktop.resolve1",
1443 p,
1444 property_map,
1445 BUS_MAP_BOOLEAN_AS_BOOL,
1446 &error,
1447 &m,
1448 &link_info);
1449 if (r < 0)
1450 return log_error_errno(r, "Failed to get link data for %i: %s", ifindex, bus_error_message(&error, r));
1451
1452 (void) pager_open(arg_pager_flags);
1453
1454 if (mode == STATUS_DNS)
1455 return status_print_strv_ifindex(ifindex, name, link_info.dns);
1456
1457 if (mode == STATUS_DOMAIN)
1458 return status_print_strv_ifindex(ifindex, name, link_info.domains);
1459
1460 if (mode == STATUS_NTA)
1461 return status_print_strv_ifindex(ifindex, name, link_info.ntas);
1462
1463 if (mode == STATUS_DEFAULT_ROUTE) {
1464 printf("%sLink %i (%s)%s: %s\n",
1465 ansi_highlight(), ifindex, name, ansi_normal(),
1466 yes_no(link_info.default_route));
1467
1468 return 0;
1469 }
1470
1471 if (mode == STATUS_LLMNR) {
1472 printf("%sLink %i (%s)%s: %s\n",
1473 ansi_highlight(), ifindex, name, ansi_normal(),
1474 strna(link_info.llmnr));
1475
1476 return 0;
1477 }
1478
1479 if (mode == STATUS_MDNS) {
1480 printf("%sLink %i (%s)%s: %s\n",
1481 ansi_highlight(), ifindex, name, ansi_normal(),
1482 strna(link_info.mdns));
1483
1484 return 0;
1485 }
1486
1487 if (mode == STATUS_PRIVATE) {
1488 printf("%sLink %i (%s)%s: %s\n",
1489 ansi_highlight(), ifindex, name, ansi_normal(),
1490 strna(link_info.dns_over_tls));
1491
1492 return 0;
1493 }
1494
1495 if (mode == STATUS_DNSSEC) {
1496 printf("%sLink %i (%s)%s: %s\n",
1497 ansi_highlight(), ifindex, name, ansi_normal(),
1498 strna(link_info.dnssec));
1499
1500 return 0;
1501 }
1502
1503 if (empty_line && *empty_line)
1504 fputc('\n', stdout);
1505
1506 printf("%sLink %i (%s)%s\n",
1507 ansi_highlight(), ifindex, name, ansi_normal());
1508
1509 table = table_new("key", "value");
1510 if (!table)
1511 return log_oom();
1512
1513 table_set_header(table, false);
1514
1515 r = table_add_many(table,
1516 TABLE_STRING, "Current Scopes:",
1517 TABLE_SET_ALIGN_PERCENT, 100);
1518 if (r < 0)
1519 return table_log_add_error(r);
1520
1521 if (link_info.scopes_mask == 0)
1522 r = table_add_cell(table, NULL, TABLE_STRING, "none");
1523 else {
1524 _cleanup_free_ char *buf = NULL;
1525 size_t len;
1526
1527 if (asprintf(&buf, "%s%s%s%s%s",
1528 link_info.scopes_mask & SD_RESOLVED_DNS ? "DNS " : "",
1529 link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV4 ? "LLMNR/IPv4 " : "",
1530 link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV6 ? "LLMNR/IPv6 " : "",
1531 link_info.scopes_mask & SD_RESOLVED_MDNS_IPV4 ? "mDNS/IPv4 " : "",
1532 link_info.scopes_mask & SD_RESOLVED_MDNS_IPV6 ? "mDNS/IPv6 " : "") < 0)
1533 return log_oom();
1534
1535 len = strlen(buf);
1536 assert(len > 0);
1537 buf[len - 1] = '\0';
1538
1539 r = table_add_cell(table, NULL, TABLE_STRING, buf);
1540 }
1541 if (r < 0)
1542 return table_log_add_error(r);
1543
1544 r = table_add_many(table,
1545 TABLE_STRING, "DefaultRoute setting:",
1546 TABLE_BOOLEAN, link_info.default_route,
1547 TABLE_STRING, "LLMNR setting:",
1548 TABLE_STRING, strna(link_info.llmnr),
1549 TABLE_STRING, "MulticastDNS setting:",
1550 TABLE_STRING, strna(link_info.mdns),
1551 TABLE_STRING, "DNSOverTLS setting:",
1552 TABLE_STRING, strna(link_info.dns_over_tls),
1553 TABLE_STRING, "DNSSEC setting:",
1554 TABLE_STRING, strna(link_info.dnssec),
1555 TABLE_STRING, "DNSSEC supported:",
1556 TABLE_BOOLEAN, link_info.dnssec_supported);
1557 if (r < 0)
1558 return table_log_add_error(r);
1559
1560 if (link_info.current_dns) {
1561 r = table_add_many(table,
1562 TABLE_STRING, "Current DNS Server:",
1563 TABLE_STRING, link_info.current_dns);
1564 if (r < 0)
1565 return table_log_add_error(r);
1566 }
1567
1568 r = dump_list(table, "DNS Servers:", link_info.dns);
1569 if (r < 0)
1570 return r;
1571
1572 r = dump_list(table, "DNS Domain:", link_info.domains);
1573 if (r < 0)
1574 return r;
1575
1576 r = dump_list(table, "DNSSEC NTA:", link_info.ntas);
1577 if (r < 0)
1578 return r;
1579
1580 r = table_print(table, NULL);
1581 if (r < 0)
1582 return log_error_errno(r, "Failed to print table: %m");
1583
1584 if (empty_line)
1585 *empty_line = true;
1586
1587 return 0;
1588 }
1589
1590 static int map_global_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1591 char ***l = userdata;
1592 int r;
1593
1594 assert(bus);
1595 assert(member);
1596 assert(m);
1597 assert(l);
1598
1599 r = sd_bus_message_enter_container(m, 'a', "(iiay)");
1600 if (r < 0)
1601 return r;
1602
1603 for (;;) {
1604 _cleanup_free_ char *pretty = NULL;
1605
1606 r = read_dns_server_one(m, true, &pretty);
1607 if (r < 0)
1608 return r;
1609 if (r == 0)
1610 break;
1611
1612 if (isempty(pretty))
1613 continue;
1614
1615 r = strv_consume(l, TAKE_PTR(pretty));
1616 if (r < 0)
1617 return r;
1618 }
1619
1620 r = sd_bus_message_exit_container(m);
1621 if (r < 0)
1622 return r;
1623
1624 return 0;
1625 }
1626
1627 static int map_global_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1628 assert(m);
1629 assert(userdata);
1630
1631 return read_dns_server_one(m, true, userdata);
1632 }
1633
1634 static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1635 char ***l = userdata;
1636 int r;
1637
1638 assert(bus);
1639 assert(member);
1640 assert(m);
1641 assert(l);
1642
1643 r = sd_bus_message_enter_container(m, 'a', "(isb)");
1644 if (r < 0)
1645 return r;
1646
1647 for (;;) {
1648 _cleanup_free_ char *pretty = NULL;
1649
1650 r = read_domain_one(m, true, &pretty);
1651 if (r < 0)
1652 return r;
1653 if (r == 0)
1654 break;
1655
1656 if (isempty(pretty))
1657 continue;
1658
1659 r = strv_consume(l, TAKE_PTR(pretty));
1660 if (r < 0)
1661 return r;
1662 }
1663
1664 r = sd_bus_message_exit_container(m);
1665 if (r < 0)
1666 return r;
1667
1668 return 0;
1669 }
1670
1671 static int status_print_strv_global(char **p) {
1672 char **i;
1673
1674 printf("%sGlobal%s:", ansi_highlight(), ansi_normal());
1675
1676 STRV_FOREACH(i, p)
1677 printf(" %s", *i);
1678
1679 printf("\n");
1680
1681 return 0;
1682 }
1683
1684 struct global_info {
1685 char *current_dns;
1686 char **dns;
1687 char **fallback_dns;
1688 char **domains;
1689 char **ntas;
1690 const char *llmnr;
1691 const char *mdns;
1692 const char *dns_over_tls;
1693 const char *dnssec;
1694 bool dnssec_supported;
1695 };
1696
1697 static void global_info_clear(struct global_info *p) {
1698 free(p->current_dns);
1699 strv_free(p->dns);
1700 strv_free(p->fallback_dns);
1701 strv_free(p->domains);
1702 strv_free(p->ntas);
1703 }
1704
1705 static int status_global(sd_bus *bus, StatusMode mode, bool *empty_line) {
1706 static const struct bus_properties_map property_map[] = {
1707 { "DNS", "a(iiay)", map_global_dns_servers, offsetof(struct global_info, dns) },
1708 { "FallbackDNS", "a(iiay)", map_global_dns_servers, offsetof(struct global_info, fallback_dns) },
1709 { "CurrentDNSServer", "(iiay)", map_global_current_dns_server, offsetof(struct global_info, current_dns) },
1710 { "Domains", "a(isb)", map_global_domains, offsetof(struct global_info, domains) },
1711 { "DNSSECNegativeTrustAnchors", "as", NULL, offsetof(struct global_info, ntas) },
1712 { "LLMNR", "s", NULL, offsetof(struct global_info, llmnr) },
1713 { "MulticastDNS", "s", NULL, offsetof(struct global_info, mdns) },
1714 { "DNSOverTLS", "s", NULL, offsetof(struct global_info, dns_over_tls) },
1715 { "DNSSEC", "s", NULL, offsetof(struct global_info, dnssec) },
1716 { "DNSSECSupported", "b", NULL, offsetof(struct global_info, dnssec_supported) },
1717 {}
1718 };
1719 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1720 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1721 _cleanup_(global_info_clear) struct global_info global_info = {};
1722 _cleanup_(table_unrefp) Table *table = NULL;
1723 int r;
1724
1725 assert(bus);
1726 assert(empty_line);
1727
1728 r = bus_map_all_properties(bus,
1729 "org.freedesktop.resolve1",
1730 "/org/freedesktop/resolve1",
1731 property_map,
1732 BUS_MAP_BOOLEAN_AS_BOOL,
1733 &error,
1734 &m,
1735 &global_info);
1736 if (r < 0)
1737 return log_error_errno(r, "Failed to get global data: %s", bus_error_message(&error, r));
1738
1739 (void) pager_open(arg_pager_flags);
1740
1741 if (mode == STATUS_DNS)
1742 return status_print_strv_global(global_info.dns);
1743
1744 if (mode == STATUS_DOMAIN)
1745 return status_print_strv_global(global_info.domains);
1746
1747 if (mode == STATUS_NTA)
1748 return status_print_strv_global(global_info.ntas);
1749
1750 if (mode == STATUS_LLMNR) {
1751 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1752 strna(global_info.llmnr));
1753
1754 return 0;
1755 }
1756
1757 if (mode == STATUS_MDNS) {
1758 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1759 strna(global_info.mdns));
1760
1761 return 0;
1762 }
1763
1764 if (mode == STATUS_PRIVATE) {
1765 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1766 strna(global_info.dns_over_tls));
1767
1768 return 0;
1769 }
1770
1771 if (mode == STATUS_DNSSEC) {
1772 printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(),
1773 strna(global_info.dnssec));
1774
1775 return 0;
1776 }
1777
1778 printf("%sGlobal%s\n", ansi_highlight(), ansi_normal());
1779
1780 table = table_new("key", "value");
1781 if (!table)
1782 return log_oom();
1783
1784 table_set_header(table, false);
1785
1786 r = table_add_many(table,
1787 TABLE_STRING, "LLMNR setting:",
1788 TABLE_SET_ALIGN_PERCENT, 100,
1789 TABLE_STRING, strna(global_info.llmnr),
1790 TABLE_STRING, "MulticastDNS setting:",
1791 TABLE_STRING, strna(global_info.mdns),
1792 TABLE_STRING, "DNSOverTLS setting:",
1793 TABLE_STRING, strna(global_info.dns_over_tls),
1794 TABLE_STRING, "DNSSEC setting:",
1795 TABLE_STRING, strna(global_info.dnssec),
1796 TABLE_STRING, "DNSSEC supported:",
1797 TABLE_BOOLEAN, global_info.dnssec_supported);
1798 if (r < 0)
1799 return table_log_add_error(r);
1800
1801 if (global_info.current_dns) {
1802 r = table_add_many(table,
1803 TABLE_STRING, "Current DNS Server:",
1804 TABLE_STRING, global_info.current_dns);
1805 if (r < 0)
1806 return table_log_add_error(r);
1807 }
1808
1809 r = dump_list(table, "DNS Servers:", global_info.dns);
1810 if (r < 0)
1811 return r;
1812
1813 r = dump_list(table, "Fallback DNS Servers:", global_info.fallback_dns);
1814 if (r < 0)
1815 return r;
1816
1817 r = dump_list(table, "DNS Domain:", global_info.domains);
1818 if (r < 0)
1819 return r;
1820
1821 strv_sort(global_info.ntas);
1822 r = dump_list(table, "DNSSEC NTA:", global_info.ntas);
1823 if (r < 0)
1824 return r;
1825
1826 r = table_print(table, NULL);
1827 if (r < 0)
1828 return log_error_errno(r, "Failed to print table: %m");
1829
1830 *empty_line = true;
1831
1832 return 0;
1833 }
1834
1835 static int status_all(sd_bus *bus, StatusMode mode) {
1836 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1837 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
1838 sd_netlink_message *i;
1839 bool empty_line = false;
1840 int r;
1841
1842 assert(bus);
1843
1844 r = status_global(bus, mode, &empty_line);
1845 if (r < 0)
1846 return r;
1847
1848 r = sd_netlink_open(&rtnl);
1849 if (r < 0)
1850 return log_error_errno(r, "Failed to connect to netlink: %m");
1851
1852 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
1853 if (r < 0)
1854 return rtnl_log_create_error(r);
1855
1856 r = sd_netlink_message_request_dump(req, true);
1857 if (r < 0)
1858 return rtnl_log_create_error(r);
1859
1860 r = sd_netlink_call(rtnl, req, 0, &reply);
1861 if (r < 0)
1862 return log_error_errno(r, "Failed to enumerate links: %m");
1863
1864 r = 0;
1865 for (i = reply; i; i = sd_netlink_message_next(i)) {
1866 const char *name;
1867 int ifindex, q;
1868 uint16_t type;
1869
1870 q = sd_netlink_message_get_type(i, &type);
1871 if (q < 0)
1872 return rtnl_log_parse_error(q);
1873
1874 if (type != RTM_NEWLINK)
1875 continue;
1876
1877 q = sd_rtnl_message_link_get_ifindex(i, &ifindex);
1878 if (q < 0)
1879 return rtnl_log_parse_error(q);
1880
1881 if (ifindex == LOOPBACK_IFINDEX)
1882 continue;
1883
1884 q = sd_netlink_message_read_string(i, IFLA_IFNAME, &name);
1885 if (q < 0)
1886 return rtnl_log_parse_error(q);
1887
1888 q = status_ifindex(bus, ifindex, name, mode, &empty_line);
1889 if (q < 0 && r >= 0)
1890 r = q;
1891 }
1892
1893 return r;
1894 }
1895
1896 static int verb_status(int argc, char **argv, void *userdata) {
1897 sd_bus *bus = userdata;
1898 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
1899 int r = 0;
1900
1901 if (argc > 1) {
1902 char **ifname;
1903 bool empty_line = false;
1904
1905 STRV_FOREACH(ifname, argv + 1) {
1906 int ifindex, q;
1907
1908 ifindex = resolve_interface(&rtnl, *ifname);
1909 if (ifindex < 0) {
1910 log_warning_errno(ifindex, "Failed to resolve interface \"%s\", ignoring: %m", *ifname);
1911 continue;
1912 }
1913
1914 q = status_ifindex(bus, ifindex, NULL, STATUS_ALL, &empty_line);
1915 if (q < 0)
1916 r = q;
1917 }
1918 } else
1919 r = status_all(bus, STATUS_ALL);
1920
1921 return r;
1922 }
1923
1924 static int call_dns(sd_bus *bus, char **dns, const char *destination, const char *path, const char *interface, sd_bus_error *error) {
1925 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
1926 char **p;
1927 int r;
1928
1929 r = sd_bus_message_new_method_call(
1930 bus,
1931 &req,
1932 destination,
1933 path,
1934 interface,
1935 "SetLinkDNS");
1936 if (r < 0)
1937 return bus_log_create_error(r);
1938
1939 r = sd_bus_message_append(req, "i", arg_ifindex);
1940 if (r < 0)
1941 return bus_log_create_error(r);
1942
1943 r = sd_bus_message_open_container(req, 'a', "(iay)");
1944 if (r < 0)
1945 return bus_log_create_error(r);
1946
1947 /* If only argument is the empty string, then call SetLinkDNS() with an
1948 * empty list, which will clear the list of domains for an interface. */
1949 if (!strv_equal(dns, STRV_MAKE("")))
1950 STRV_FOREACH(p, dns) {
1951 struct in_addr_data data;
1952
1953 r = in_addr_from_string_auto(*p, &data.family, &data.address);
1954 if (r < 0)
1955 return log_error_errno(r, "Failed to parse DNS server address: %s", *p);
1956
1957 r = sd_bus_message_open_container(req, 'r', "iay");
1958 if (r < 0)
1959 return bus_log_create_error(r);
1960
1961 r = sd_bus_message_append(req, "i", data.family);
1962 if (r < 0)
1963 return bus_log_create_error(r);
1964
1965 r = sd_bus_message_append_array(req, 'y', &data.address, FAMILY_ADDRESS_SIZE(data.family));
1966 if (r < 0)
1967 return bus_log_create_error(r);
1968
1969 r = sd_bus_message_close_container(req);
1970 if (r < 0)
1971 return bus_log_create_error(r);
1972 }
1973
1974 r = sd_bus_message_close_container(req);
1975 if (r < 0)
1976 return bus_log_create_error(r);
1977
1978 return sd_bus_call(bus, req, 0, error, NULL);
1979 }
1980
1981 static int verb_dns(int argc, char **argv, void *userdata) {
1982 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1983 sd_bus *bus = userdata;
1984 int r;
1985
1986 assert(bus);
1987
1988 if (argc >= 2) {
1989 r = ifname_mangle(argv[1]);
1990 if (r < 0)
1991 return r;
1992 }
1993
1994 if (arg_ifindex <= 0)
1995 return status_all(bus, STATUS_DNS);
1996
1997 if (argc < 3)
1998 return status_ifindex(bus, arg_ifindex, NULL, STATUS_DNS, NULL);
1999
2000 r = call_dns(bus, argv + 2,
2001 "org.freedesktop.resolve1",
2002 "/org/freedesktop/resolve1",
2003 "org.freedesktop.resolve1.Manager",
2004 &error);
2005 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2006 sd_bus_error_free(&error);
2007
2008 r = call_dns(bus, argv + 2,
2009 "org.freedesktop.network1",
2010 "/org/freedesktop/network1",
2011 "org.freedesktop.network1.Manager",
2012 &error);
2013 }
2014 if (r < 0) {
2015 if (arg_ifindex_permissive &&
2016 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2017 return 0;
2018
2019 return log_error_errno(r, "Failed to set DNS configuration: %s", bus_error_message(&error, r));
2020 }
2021
2022 return 0;
2023 }
2024
2025 static int call_domain(sd_bus *bus, char **domain, const char *destination, const char *path, const char *interface, sd_bus_error *error) {
2026 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
2027 char **p;
2028 int r;
2029
2030 r = sd_bus_message_new_method_call(
2031 bus,
2032 &req,
2033 destination,
2034 path,
2035 interface,
2036 "SetLinkDomains");
2037 if (r < 0)
2038 return bus_log_create_error(r);
2039
2040 r = sd_bus_message_append(req, "i", arg_ifindex);
2041 if (r < 0)
2042 return bus_log_create_error(r);
2043
2044 r = sd_bus_message_open_container(req, 'a', "(sb)");
2045 if (r < 0)
2046 return bus_log_create_error(r);
2047
2048 /* If only argument is the empty string, then call SetLinkDomains() with an
2049 * empty list, which will clear the list of domains for an interface. */
2050 if (!strv_equal(domain, STRV_MAKE("")))
2051 STRV_FOREACH(p, domain) {
2052 const char *n;
2053
2054 n = **p == '~' ? *p + 1 : *p;
2055
2056 r = dns_name_is_valid(n);
2057 if (r < 0)
2058 return log_error_errno(r, "Failed to validate specified domain %s: %m", n);
2059 if (r == 0) {
2060 log_error("Domain not valid: %s", n);
2061 return -EINVAL;
2062 }
2063
2064 r = sd_bus_message_append(req, "(sb)", n, **p == '~');
2065 if (r < 0)
2066 return bus_log_create_error(r);
2067 }
2068
2069 r = sd_bus_message_close_container(req);
2070 if (r < 0)
2071 return bus_log_create_error(r);
2072
2073 return sd_bus_call(bus, req, 0, error, NULL);
2074 }
2075
2076 static int verb_domain(int argc, char **argv, void *userdata) {
2077 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2078 sd_bus *bus = userdata;
2079 int r;
2080
2081 assert(bus);
2082
2083 if (argc >= 2) {
2084 r = ifname_mangle(argv[1]);
2085 if (r < 0)
2086 return r;
2087 }
2088
2089 if (arg_ifindex <= 0)
2090 return status_all(bus, STATUS_DOMAIN);
2091
2092 if (argc < 3)
2093 return status_ifindex(bus, arg_ifindex, NULL, STATUS_DOMAIN, NULL);
2094
2095 r = call_domain(bus, argv + 2,
2096 "org.freedesktop.resolve1",
2097 "/org/freedesktop/resolve1",
2098 "org.freedesktop.resolve1.Manager",
2099 &error);
2100 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2101 sd_bus_error_free(&error);
2102
2103 r = call_domain(bus, argv + 2,
2104 "org.freedesktop.network1",
2105 "/org/freedesktop/network1",
2106 "org.freedesktop.network1.Manager",
2107 &error);
2108 }
2109 if (r < 0) {
2110 if (arg_ifindex_permissive &&
2111 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2112 return 0;
2113
2114 return log_error_errno(r, "Failed to set domain configuration: %s", bus_error_message(&error, r));
2115 }
2116
2117 return 0;
2118 }
2119
2120 static int verb_default_route(int argc, char **argv, void *userdata) {
2121 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2122 sd_bus *bus = userdata;
2123 int r, b;
2124
2125 assert(bus);
2126
2127 if (argc >= 2) {
2128 r = ifname_mangle(argv[1]);
2129 if (r < 0)
2130 return r;
2131 }
2132
2133 if (arg_ifindex <= 0)
2134 return status_all(bus, STATUS_DEFAULT_ROUTE);
2135
2136 if (argc < 3)
2137 return status_ifindex(bus, arg_ifindex, NULL, STATUS_DEFAULT_ROUTE, NULL);
2138
2139 b = parse_boolean(argv[2]);
2140 if (b < 0)
2141 return log_error_errno(b, "Failed to parse boolean argument: %s", argv[2]);
2142
2143 r = sd_bus_call_method(
2144 bus,
2145 "org.freedesktop.resolve1",
2146 "/org/freedesktop/resolve1",
2147 "org.freedesktop.resolve1.Manager",
2148 "SetLinkDefaultRoute",
2149 &error,
2150 NULL,
2151 "ib", arg_ifindex, b);
2152 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2153 sd_bus_error_free(&error);
2154
2155 r = sd_bus_call_method(
2156 bus,
2157 "org.freedesktop.network1",
2158 "/org/freedesktop/network1",
2159 "org.freedesktop.network1.Manager",
2160 "SetLinkDefaultRoute",
2161 &error,
2162 NULL,
2163 "ib", arg_ifindex, b);
2164 }
2165 if (r < 0) {
2166 if (arg_ifindex_permissive &&
2167 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2168 return 0;
2169
2170 return log_error_errno(r, "Failed to set default route configuration: %s", bus_error_message(&error, r));
2171 }
2172
2173 return 0;
2174 }
2175
2176 static int verb_llmnr(int argc, char **argv, void *userdata) {
2177 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2178 sd_bus *bus = userdata;
2179 int r;
2180
2181 assert(bus);
2182
2183 if (argc >= 2) {
2184 r = ifname_mangle(argv[1]);
2185 if (r < 0)
2186 return r;
2187 }
2188
2189 if (arg_ifindex <= 0)
2190 return status_all(bus, STATUS_LLMNR);
2191
2192 if (argc < 3)
2193 return status_ifindex(bus, arg_ifindex, NULL, STATUS_LLMNR, NULL);
2194
2195 r = sd_bus_call_method(
2196 bus,
2197 "org.freedesktop.resolve1",
2198 "/org/freedesktop/resolve1",
2199 "org.freedesktop.resolve1.Manager",
2200 "SetLinkLLMNR",
2201 &error,
2202 NULL,
2203 "is", arg_ifindex, argv[2]);
2204 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2205 sd_bus_error_free(&error);
2206
2207 r = sd_bus_call_method(
2208 bus,
2209 "org.freedesktop.network1",
2210 "/org/freedesktop/network1",
2211 "org.freedesktop.network1.Manager",
2212 "SetLinkLLMNR",
2213 &error,
2214 NULL,
2215 "is", arg_ifindex, argv[2]);
2216 }
2217 if (r < 0) {
2218 if (arg_ifindex_permissive &&
2219 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2220 return 0;
2221
2222 return log_error_errno(r, "Failed to set LLMNR configuration: %s", bus_error_message(&error, r));
2223 }
2224
2225 return 0;
2226 }
2227
2228 static int verb_mdns(int argc, char **argv, void *userdata) {
2229 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2230 sd_bus *bus = userdata;
2231 int r;
2232
2233 assert(bus);
2234
2235 if (argc >= 2) {
2236 r = ifname_mangle(argv[1]);
2237 if (r < 0)
2238 return r;
2239 }
2240
2241 if (arg_ifindex <= 0)
2242 return status_all(bus, STATUS_MDNS);
2243
2244 if (argc < 3)
2245 return status_ifindex(bus, arg_ifindex, NULL, STATUS_MDNS, NULL);
2246
2247 r = sd_bus_call_method(
2248 bus,
2249 "org.freedesktop.resolve1",
2250 "/org/freedesktop/resolve1",
2251 "org.freedesktop.resolve1.Manager",
2252 "SetLinkMulticastDNS",
2253 &error,
2254 NULL,
2255 "is", arg_ifindex, argv[2]);
2256 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2257 sd_bus_error_free(&error);
2258
2259 r = sd_bus_call_method(
2260 bus,
2261 "org.freedesktop.network1",
2262 "/org/freedesktop/network1",
2263 "org.freedesktop.network1.Manager",
2264 "SetLinkMulticastDNS",
2265 &error,
2266 NULL,
2267 "is", arg_ifindex, argv[2]);
2268 }
2269 if (r < 0) {
2270 if (arg_ifindex_permissive &&
2271 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2272 return 0;
2273
2274 return log_error_errno(r, "Failed to set MulticastDNS configuration: %s", bus_error_message(&error, r));
2275 }
2276
2277 return 0;
2278 }
2279
2280 static int verb_dns_over_tls(int argc, char **argv, void *userdata) {
2281 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2282 sd_bus *bus = userdata;
2283 int r;
2284
2285 assert(bus);
2286
2287 if (argc >= 2) {
2288 r = ifname_mangle(argv[1]);
2289 if (r < 0)
2290 return r;
2291 }
2292
2293 if (arg_ifindex <= 0)
2294 return status_all(bus, STATUS_PRIVATE);
2295
2296 if (argc < 3)
2297 return status_ifindex(bus, arg_ifindex, NULL, STATUS_PRIVATE, NULL);
2298
2299 r = sd_bus_call_method(
2300 bus,
2301 "org.freedesktop.resolve1",
2302 "/org/freedesktop/resolve1",
2303 "org.freedesktop.resolve1.Manager",
2304 "SetLinkDNSOverTLS",
2305 &error,
2306 NULL,
2307 "is", arg_ifindex, argv[2]);
2308 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2309 sd_bus_error_free(&error);
2310
2311 r = sd_bus_call_method(
2312 bus,
2313 "org.freedesktop.network1",
2314 "/org/freedesktop/network1",
2315 "org.freedesktop.network1.Manager",
2316 "SetLinkDNSOverTLS",
2317 &error,
2318 NULL,
2319 "is", arg_ifindex, argv[2]);
2320 }
2321 if (r < 0) {
2322 if (arg_ifindex_permissive &&
2323 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2324 return 0;
2325
2326 return log_error_errno(r, "Failed to set DNSOverTLS configuration: %s", bus_error_message(&error, r));
2327 }
2328
2329 return 0;
2330 }
2331
2332 static int verb_dnssec(int argc, char **argv, void *userdata) {
2333 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2334 sd_bus *bus = userdata;
2335 int r;
2336
2337 assert(bus);
2338
2339 if (argc >= 2) {
2340 r = ifname_mangle(argv[1]);
2341 if (r < 0)
2342 return r;
2343 }
2344
2345 if (arg_ifindex <= 0)
2346 return status_all(bus, STATUS_DNSSEC);
2347
2348 if (argc < 3)
2349 return status_ifindex(bus, arg_ifindex, NULL, STATUS_DNSSEC, NULL);
2350
2351 r = sd_bus_call_method(
2352 bus,
2353 "org.freedesktop.resolve1",
2354 "/org/freedesktop/resolve1",
2355 "org.freedesktop.resolve1.Manager",
2356 "SetLinkDNSSEC",
2357 &error,
2358 NULL,
2359 "is", arg_ifindex, argv[2]);
2360 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2361 sd_bus_error_free(&error);
2362
2363 r = sd_bus_call_method(
2364 bus,
2365 "org.freedesktop.network1",
2366 "/org/freedesktop/network1",
2367 "org.freedesktop.network1.Manager",
2368 "SetLinkDNSSEC",
2369 &error,
2370 NULL,
2371 "is", arg_ifindex, argv[2]);
2372 }
2373 if (r < 0) {
2374 if (arg_ifindex_permissive &&
2375 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2376 return 0;
2377
2378 return log_error_errno(r, "Failed to set DNSSEC configuration: %s", bus_error_message(&error, r));
2379 }
2380
2381 return 0;
2382 }
2383
2384 static int call_nta(sd_bus *bus, char **nta, const char *destination, const char *path, const char *interface, sd_bus_error *error) {
2385 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
2386 int r;
2387
2388 r = sd_bus_message_new_method_call(
2389 bus,
2390 &req,
2391 destination,
2392 path,
2393 interface,
2394 "SetLinkDNSSECNegativeTrustAnchors");
2395 if (r < 0)
2396 return bus_log_create_error(r);
2397
2398 r = sd_bus_message_append(req, "i", arg_ifindex);
2399 if (r < 0)
2400 return bus_log_create_error(r);
2401
2402 r = sd_bus_message_append_strv(req, nta);
2403 if (r < 0)
2404 return bus_log_create_error(r);
2405
2406 return sd_bus_call(bus, req, 0, error, NULL);
2407 }
2408
2409 static int verb_nta(int argc, char **argv, void *userdata) {
2410 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2411 sd_bus *bus = userdata;
2412 char **p;
2413 int r;
2414 bool clear;
2415
2416 assert(bus);
2417
2418 if (argc >= 2) {
2419 r = ifname_mangle(argv[1]);
2420 if (r < 0)
2421 return r;
2422 }
2423
2424 if (arg_ifindex <= 0)
2425 return status_all(bus, STATUS_NTA);
2426
2427 if (argc < 3)
2428 return status_ifindex(bus, arg_ifindex, NULL, STATUS_NTA, NULL);
2429
2430 /* If only argument is the empty string, then call SetLinkDNSSECNegativeTrustAnchors()
2431 * with an empty list, which will clear the list of domains for an interface. */
2432 clear = strv_equal(argv + 2, STRV_MAKE(""));
2433
2434 if (!clear)
2435 STRV_FOREACH(p, argv + 2) {
2436 r = dns_name_is_valid(*p);
2437 if (r < 0)
2438 return log_error_errno(r, "Failed to validate specified domain %s: %m", *p);
2439 if (r == 0) {
2440 log_error("Domain not valid: %s", *p);
2441 return -EINVAL;
2442 }
2443 }
2444
2445 r = call_nta(bus, clear ? NULL : argv + 2,
2446 "org.freedesktop.resolve1",
2447 "/org/freedesktop/resolve1",
2448 "org.freedesktop.resolve1.Manager",
2449 &error);
2450 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2451 sd_bus_error_free(&error);
2452
2453 r = call_nta(bus, clear ? NULL : argv + 2,
2454 "org.freedesktop.network1",
2455 "/org/freedesktop/network1",
2456 "org.freedesktop.network1.Manager",
2457 &error);
2458 }
2459 if (r < 0) {
2460 if (arg_ifindex_permissive &&
2461 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2462 return 0;
2463
2464 return log_error_errno(r, "Failed to set DNSSEC NTA configuration: %s", bus_error_message(&error, r));
2465 }
2466
2467 return 0;
2468 }
2469
2470 static int verb_revert_link(int argc, char **argv, void *userdata) {
2471 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2472 sd_bus *bus = userdata;
2473 int r;
2474
2475 assert(bus);
2476
2477 if (argc >= 2) {
2478 r = ifname_mangle(argv[1]);
2479 if (r < 0)
2480 return r;
2481 }
2482
2483 if (arg_ifindex <= 0)
2484 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Interface argument required.");
2485
2486 r = sd_bus_call_method(
2487 bus,
2488 "org.freedesktop.resolve1",
2489 "/org/freedesktop/resolve1",
2490 "org.freedesktop.resolve1.Manager",
2491 "RevertLink",
2492 &error,
2493 NULL,
2494 "i", arg_ifindex);
2495 if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) {
2496 sd_bus_error_free(&error);
2497
2498 r = sd_bus_call_method(
2499 bus,
2500 "org.freedesktop.network1",
2501 "/org/freedesktop/network1",
2502 "org.freedesktop.network1.Manager",
2503 "RevertLinkDNS",
2504 &error,
2505 NULL,
2506 "i", arg_ifindex);
2507 }
2508 if (r < 0) {
2509 if (arg_ifindex_permissive &&
2510 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
2511 return 0;
2512
2513 return log_error_errno(r, "Failed to revert interface configuration: %s", bus_error_message(&error, r));
2514 }
2515
2516 return 0;
2517 }
2518
2519 static int verb_log_level(int argc, char *argv[], void *userdata) {
2520 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2521 sd_bus *bus = userdata;
2522 int r;
2523
2524 assert(bus);
2525
2526 if (argc == 1) {
2527 _cleanup_free_ char *level = NULL;
2528
2529 r = sd_bus_get_property_string(
2530 bus,
2531 "org.freedesktop.resolve1",
2532 "/org/freedesktop/LogControl1",
2533 "org.freedesktop.LogControl1",
2534 "LogLevel",
2535 &error,
2536 &level);
2537 if (r < 0)
2538 return log_error_errno(r, "Failed to get log level: %s", bus_error_message(&error, r));
2539
2540 puts(level);
2541
2542 } else {
2543 assert(argc == 2);
2544
2545 r = sd_bus_set_property(
2546 bus,
2547 "org.freedesktop.resolve1",
2548 "/org/freedesktop/LogControl1",
2549 "org.freedesktop.LogControl1",
2550 "LogLevel",
2551 &error,
2552 "s",
2553 argv[1]);
2554 if (r < 0)
2555 return log_error_errno(r, "Failed to set log level: %s", bus_error_message(&error, r));
2556 }
2557
2558 return 0;
2559 }
2560
2561 static void help_protocol_types(void) {
2562 if (arg_legend)
2563 puts("Known protocol types:");
2564 puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6\nmdns\nmdns-ipv4\nmdns-ipv6");
2565 }
2566
2567 static void help_dns_types(void) {
2568 if (arg_legend)
2569 puts("Known DNS RR types:");
2570
2571 DUMP_STRING_TABLE(dns_type, int, _DNS_TYPE_MAX);
2572 }
2573
2574 static void help_dns_classes(void) {
2575 if (arg_legend)
2576 puts("Known DNS RR classes:");
2577
2578 DUMP_STRING_TABLE(dns_class, int, _DNS_CLASS_MAX);
2579 }
2580
2581 static int compat_help(void) {
2582 _cleanup_free_ char *link = NULL;
2583 int r;
2584
2585 r = terminal_urlify_man("resolvectl", "1", &link);
2586 if (r < 0)
2587 return log_oom();
2588
2589 printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n"
2590 "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n"
2591 "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n"
2592 "%1$s [OPTIONS...] --statistics\n"
2593 "%1$s [OPTIONS...] --reset-statistics\n"
2594 "\n"
2595 "%2$sResolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%3$s\n\n"
2596 " -h --help Show this help\n"
2597 " --version Show package version\n"
2598 " --no-pager Do not pipe output into a pager\n"
2599 " -4 Resolve IPv4 addresses\n"
2600 " -6 Resolve IPv6 addresses\n"
2601 " -i --interface=INTERFACE Look on interface\n"
2602 " -p --protocol=PROTO|help Look via protocol\n"
2603 " -t --type=TYPE|help Query RR with DNS type\n"
2604 " -c --class=CLASS|help Query RR with DNS class\n"
2605 " --service Resolve service (SRV)\n"
2606 " --service-address=BOOL Resolve address for services (default: yes)\n"
2607 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
2608 " --openpgp Query OpenPGP public key\n"
2609 " --tlsa Query TLS public key\n"
2610 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
2611 " --search=BOOL Use search domains for single-label names\n"
2612 " (default: yes)\n"
2613 " --raw[=payload|packet] Dump the answer as binary data\n"
2614 " --legend=BOOL Print headers and additional info (default: yes)\n"
2615 " --statistics Show resolver statistics\n"
2616 " --reset-statistics Reset resolver statistics\n"
2617 " --status Show link and server status\n"
2618 " --flush-caches Flush all local DNS caches\n"
2619 " --reset-server-features\n"
2620 " Forget learnt DNS server feature levels\n"
2621 " --set-dns=SERVER Set per-interface DNS server address\n"
2622 " --set-domain=DOMAIN Set per-interface search domain\n"
2623 " --set-llmnr=MODE Set per-interface LLMNR mode\n"
2624 " --set-mdns=MODE Set per-interface MulticastDNS mode\n"
2625 " --set-dnsovertls=MODE Set per-interface DNS-over-TLS mode\n"
2626 " --set-dnssec=MODE Set per-interface DNSSEC mode\n"
2627 " --set-nta=DOMAIN Set per-interface DNSSEC NTA\n"
2628 " --revert Revert per-interface configuration\n"
2629 "\nSee the %4$s for details.\n"
2630 , program_invocation_short_name
2631 , ansi_highlight()
2632 , ansi_normal()
2633 , link
2634 );
2635
2636 return 0;
2637 }
2638
2639 static int native_help(void) {
2640 _cleanup_free_ char *link = NULL;
2641 int r;
2642
2643 r = terminal_urlify_man("resolvectl", "1", &link);
2644 if (r < 0)
2645 return log_oom();
2646
2647 printf("%s [OPTIONS...] COMMAND ...\n"
2648 "\n"
2649 "%sSend control commands to the network name resolution manager, or%s\n"
2650 "%sresolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%s\n"
2651 "\nCommands:\n"
2652 " query HOSTNAME|ADDRESS... Resolve domain names, IPv4 and IPv6 addresses\n"
2653 " service [[NAME] TYPE] DOMAIN Resolve service (SRV)\n"
2654 " openpgp EMAIL@DOMAIN... Query OpenPGP public key\n"
2655 " tlsa DOMAIN[:PORT]... Query TLS public key\n"
2656 " status [LINK...] Show link and server status\n"
2657 " statistics Show resolver statistics\n"
2658 " reset-statistics Reset resolver statistics\n"
2659 " flush-caches Flush all local DNS caches\n"
2660 " reset-server-features Forget learnt DNS server feature levels\n"
2661 " dns [LINK [SERVER...]] Get/set per-interface DNS server address\n"
2662 " domain [LINK [DOMAIN...]] Get/set per-interface search domain\n"
2663 " default-route [LINK [BOOL]] Get/set per-interface default route flag\n"
2664 " llmnr [LINK [MODE]] Get/set per-interface LLMNR mode\n"
2665 " mdns [LINK [MODE]] Get/set per-interface MulticastDNS mode\n"
2666 " dnsovertls [LINK [MODE]] Get/set per-interface DNS-over-TLS mode\n"
2667 " dnssec [LINK [MODE]] Get/set per-interface DNSSEC mode\n"
2668 " nta [LINK [DOMAIN...]] Get/set per-interface DNSSEC NTA\n"
2669 " revert LINK Revert per-interface configuration\n"
2670 "\nOptions:\n"
2671 " -h --help Show this help\n"
2672 " --version Show package version\n"
2673 " --no-pager Do not pipe output into a pager\n"
2674 " -4 Resolve IPv4 addresses\n"
2675 " -6 Resolve IPv6 addresses\n"
2676 " -i --interface=INTERFACE Look on interface\n"
2677 " -p --protocol=PROTO|help Look via protocol\n"
2678 " -t --type=TYPE|help Query RR with DNS type\n"
2679 " -c --class=CLASS|help Query RR with DNS class\n"
2680 " --service-address=BOOL Resolve address for services (default: yes)\n"
2681 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
2682 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
2683 " --search=BOOL Use search domains for single-label names\n"
2684 " (default: yes)\n"
2685 " --raw[=payload|packet] Dump the answer as binary data\n"
2686 " --legend=BOOL Print headers and additional info (default: yes)\n"
2687 "\nSee the %s for details.\n"
2688 , program_invocation_short_name
2689 , ansi_highlight()
2690 , ansi_normal()
2691 , ansi_highlight()
2692 , ansi_normal()
2693 , link
2694 );
2695
2696 return 0;
2697 }
2698
2699 static int verb_help(int argc, char **argv, void *userdata) {
2700 return native_help();
2701 }
2702
2703 static int compat_parse_argv(int argc, char *argv[]) {
2704 enum {
2705 ARG_VERSION = 0x100,
2706 ARG_LEGEND,
2707 ARG_SERVICE,
2708 ARG_CNAME,
2709 ARG_SERVICE_ADDRESS,
2710 ARG_SERVICE_TXT,
2711 ARG_OPENPGP,
2712 ARG_TLSA,
2713 ARG_RAW,
2714 ARG_SEARCH,
2715 ARG_STATISTICS,
2716 ARG_RESET_STATISTICS,
2717 ARG_STATUS,
2718 ARG_FLUSH_CACHES,
2719 ARG_RESET_SERVER_FEATURES,
2720 ARG_NO_PAGER,
2721 ARG_SET_DNS,
2722 ARG_SET_DOMAIN,
2723 ARG_SET_LLMNR,
2724 ARG_SET_MDNS,
2725 ARG_SET_PRIVATE,
2726 ARG_SET_DNSSEC,
2727 ARG_SET_NTA,
2728 ARG_REVERT_LINK,
2729 };
2730
2731 static const struct option options[] = {
2732 { "help", no_argument, NULL, 'h' },
2733 { "version", no_argument, NULL, ARG_VERSION },
2734 { "type", required_argument, NULL, 't' },
2735 { "class", required_argument, NULL, 'c' },
2736 { "legend", required_argument, NULL, ARG_LEGEND },
2737 { "interface", required_argument, NULL, 'i' },
2738 { "protocol", required_argument, NULL, 'p' },
2739 { "cname", required_argument, NULL, ARG_CNAME },
2740 { "service", no_argument, NULL, ARG_SERVICE },
2741 { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS },
2742 { "service-txt", required_argument, NULL, ARG_SERVICE_TXT },
2743 { "openpgp", no_argument, NULL, ARG_OPENPGP },
2744 { "tlsa", optional_argument, NULL, ARG_TLSA },
2745 { "raw", optional_argument, NULL, ARG_RAW },
2746 { "search", required_argument, NULL, ARG_SEARCH },
2747 { "statistics", no_argument, NULL, ARG_STATISTICS, },
2748 { "reset-statistics", no_argument, NULL, ARG_RESET_STATISTICS },
2749 { "status", no_argument, NULL, ARG_STATUS },
2750 { "flush-caches", no_argument, NULL, ARG_FLUSH_CACHES },
2751 { "reset-server-features", no_argument, NULL, ARG_RESET_SERVER_FEATURES },
2752 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
2753 { "set-dns", required_argument, NULL, ARG_SET_DNS },
2754 { "set-domain", required_argument, NULL, ARG_SET_DOMAIN },
2755 { "set-llmnr", required_argument, NULL, ARG_SET_LLMNR },
2756 { "set-mdns", required_argument, NULL, ARG_SET_MDNS },
2757 { "set-dnsovertls", required_argument, NULL, ARG_SET_PRIVATE },
2758 { "set-dnssec", required_argument, NULL, ARG_SET_DNSSEC },
2759 { "set-nta", required_argument, NULL, ARG_SET_NTA },
2760 { "revert", no_argument, NULL, ARG_REVERT_LINK },
2761 {}
2762 };
2763
2764 int c, r;
2765
2766 assert(argc >= 0);
2767 assert(argv);
2768
2769 while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0)
2770 switch(c) {
2771
2772 case 'h':
2773 return compat_help();
2774
2775 case ARG_VERSION:
2776 return version();
2777
2778 case '4':
2779 arg_family = AF_INET;
2780 break;
2781
2782 case '6':
2783 arg_family = AF_INET6;
2784 break;
2785
2786 case 'i':
2787 r = ifname_mangle(optarg);
2788 if (r < 0)
2789 return r;
2790 break;
2791
2792 case 't':
2793 if (streq(optarg, "help")) {
2794 help_dns_types();
2795 return 0;
2796 }
2797
2798 r = dns_type_from_string(optarg);
2799 if (r < 0) {
2800 log_error("Failed to parse RR record type %s", optarg);
2801 return r;
2802 }
2803 arg_type = (uint16_t) r;
2804 assert((int) arg_type == r);
2805
2806 arg_mode = MODE_RESOLVE_RECORD;
2807 break;
2808
2809 case 'c':
2810 if (streq(optarg, "help")) {
2811 help_dns_classes();
2812 return 0;
2813 }
2814
2815 r = dns_class_from_string(optarg);
2816 if (r < 0) {
2817 log_error("Failed to parse RR record class %s", optarg);
2818 return r;
2819 }
2820 arg_class = (uint16_t) r;
2821 assert((int) arg_class == r);
2822
2823 break;
2824
2825 case ARG_LEGEND:
2826 r = parse_boolean(optarg);
2827 if (r < 0)
2828 return log_error_errno(r, "Failed to parse --legend= argument");
2829
2830 arg_legend = r;
2831 break;
2832
2833 case 'p':
2834 if (streq(optarg, "help")) {
2835 help_protocol_types();
2836 return 0;
2837 } else if (streq(optarg, "dns"))
2838 arg_flags |= SD_RESOLVED_DNS;
2839 else if (streq(optarg, "llmnr"))
2840 arg_flags |= SD_RESOLVED_LLMNR;
2841 else if (streq(optarg, "llmnr-ipv4"))
2842 arg_flags |= SD_RESOLVED_LLMNR_IPV4;
2843 else if (streq(optarg, "llmnr-ipv6"))
2844 arg_flags |= SD_RESOLVED_LLMNR_IPV6;
2845 else if (streq(optarg, "mdns"))
2846 arg_flags |= SD_RESOLVED_MDNS;
2847 else if (streq(optarg, "mdns-ipv4"))
2848 arg_flags |= SD_RESOLVED_MDNS_IPV4;
2849 else if (streq(optarg, "mdns-ipv6"))
2850 arg_flags |= SD_RESOLVED_MDNS_IPV6;
2851 else
2852 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2853 "Unknown protocol specifier: %s", optarg);
2854
2855 break;
2856
2857 case ARG_SERVICE:
2858 arg_mode = MODE_RESOLVE_SERVICE;
2859 break;
2860
2861 case ARG_OPENPGP:
2862 arg_mode = MODE_RESOLVE_OPENPGP;
2863 break;
2864
2865 case ARG_TLSA:
2866 arg_mode = MODE_RESOLVE_TLSA;
2867 if (!optarg || service_family_is_valid(optarg))
2868 arg_service_family = optarg;
2869 else
2870 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2871 "Unknown service family \"%s\".", optarg);
2872 break;
2873
2874 case ARG_RAW:
2875 if (on_tty())
2876 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY),
2877 "Refusing to write binary data to tty.");
2878
2879 if (optarg == NULL || streq(optarg, "payload"))
2880 arg_raw = RAW_PAYLOAD;
2881 else if (streq(optarg, "packet"))
2882 arg_raw = RAW_PACKET;
2883 else
2884 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2885 "Unknown --raw specifier \"%s\".",
2886 optarg);
2887
2888 arg_legend = false;
2889 break;
2890
2891 case ARG_CNAME:
2892 r = parse_boolean(optarg);
2893 if (r < 0)
2894 return log_error_errno(r, "Failed to parse --cname= argument.");
2895 SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0);
2896 break;
2897
2898 case ARG_SERVICE_ADDRESS:
2899 r = parse_boolean(optarg);
2900 if (r < 0)
2901 return log_error_errno(r, "Failed to parse --service-address= argument.");
2902 SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0);
2903 break;
2904
2905 case ARG_SERVICE_TXT:
2906 r = parse_boolean(optarg);
2907 if (r < 0)
2908 return log_error_errno(r, "Failed to parse --service-txt= argument.");
2909 SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0);
2910 break;
2911
2912 case ARG_SEARCH:
2913 r = parse_boolean(optarg);
2914 if (r < 0)
2915 return log_error_errno(r, "Failed to parse --search argument.");
2916 SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0);
2917 break;
2918
2919 case ARG_STATISTICS:
2920 arg_mode = MODE_STATISTICS;
2921 break;
2922
2923 case ARG_RESET_STATISTICS:
2924 arg_mode = MODE_RESET_STATISTICS;
2925 break;
2926
2927 case ARG_FLUSH_CACHES:
2928 arg_mode = MODE_FLUSH_CACHES;
2929 break;
2930
2931 case ARG_RESET_SERVER_FEATURES:
2932 arg_mode = MODE_RESET_SERVER_FEATURES;
2933 break;
2934
2935 case ARG_STATUS:
2936 arg_mode = MODE_STATUS;
2937 break;
2938
2939 case ARG_NO_PAGER:
2940 arg_pager_flags |= PAGER_DISABLE;
2941 break;
2942
2943 case ARG_SET_DNS:
2944 r = strv_extend(&arg_set_dns, optarg);
2945 if (r < 0)
2946 return log_oom();
2947
2948 arg_mode = MODE_SET_LINK;
2949 break;
2950
2951 case ARG_SET_DOMAIN:
2952 r = strv_extend(&arg_set_domain, optarg);
2953 if (r < 0)
2954 return log_oom();
2955
2956 arg_mode = MODE_SET_LINK;
2957 break;
2958
2959 case ARG_SET_LLMNR:
2960 arg_set_llmnr = optarg;
2961 arg_mode = MODE_SET_LINK;
2962 break;
2963
2964 case ARG_SET_MDNS:
2965 arg_set_mdns = optarg;
2966 arg_mode = MODE_SET_LINK;
2967 break;
2968
2969 case ARG_SET_PRIVATE:
2970 arg_set_dns_over_tls = optarg;
2971 arg_mode = MODE_SET_LINK;
2972 break;
2973
2974 case ARG_SET_DNSSEC:
2975 arg_set_dnssec = optarg;
2976 arg_mode = MODE_SET_LINK;
2977 break;
2978
2979 case ARG_SET_NTA:
2980 r = strv_extend(&arg_set_nta, optarg);
2981 if (r < 0)
2982 return log_oom();
2983
2984 arg_mode = MODE_SET_LINK;
2985 break;
2986
2987 case ARG_REVERT_LINK:
2988 arg_mode = MODE_REVERT_LINK;
2989 break;
2990
2991 case '?':
2992 return -EINVAL;
2993
2994 default:
2995 assert_not_reached("Unhandled option");
2996 }
2997
2998 if (arg_type == 0 && arg_class != 0)
2999 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3000 "--class= may only be used in conjunction with --type=.");
3001
3002 if (arg_type != 0 && arg_mode == MODE_RESOLVE_SERVICE)
3003 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3004 "--service and --type= may not be combined.");
3005
3006 if (arg_type != 0 && arg_class == 0)
3007 arg_class = DNS_CLASS_IN;
3008
3009 if (arg_class != 0 && arg_type == 0)
3010 arg_type = DNS_TYPE_A;
3011
3012 if (IN_SET(arg_mode, MODE_SET_LINK, MODE_REVERT_LINK)) {
3013
3014 if (arg_ifindex <= 0)
3015 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3016 "--set-dns=, --set-domain=, --set-llmnr=, --set-mdns=, --set-dnsovertls=, --set-dnssec=, --set-nta= and --revert require --interface=.");
3017 }
3018
3019 return 1 /* work to do */;
3020 }
3021
3022 static int native_parse_argv(int argc, char *argv[]) {
3023 enum {
3024 ARG_VERSION = 0x100,
3025 ARG_LEGEND,
3026 ARG_CNAME,
3027 ARG_SERVICE_ADDRESS,
3028 ARG_SERVICE_TXT,
3029 ARG_RAW,
3030 ARG_SEARCH,
3031 ARG_NO_PAGER,
3032 };
3033
3034 static const struct option options[] = {
3035 { "help", no_argument, NULL, 'h' },
3036 { "version", no_argument, NULL, ARG_VERSION },
3037 { "type", required_argument, NULL, 't' },
3038 { "class", required_argument, NULL, 'c' },
3039 { "legend", required_argument, NULL, ARG_LEGEND },
3040 { "interface", required_argument, NULL, 'i' },
3041 { "protocol", required_argument, NULL, 'p' },
3042 { "cname", required_argument, NULL, ARG_CNAME },
3043 { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS },
3044 { "service-txt", required_argument, NULL, ARG_SERVICE_TXT },
3045 { "raw", optional_argument, NULL, ARG_RAW },
3046 { "search", required_argument, NULL, ARG_SEARCH },
3047 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
3048 {}
3049 };
3050
3051 int c, r;
3052
3053 assert(argc >= 0);
3054 assert(argv);
3055
3056 while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0)
3057 switch(c) {
3058
3059 case 'h':
3060 return native_help();
3061
3062 case ARG_VERSION:
3063 return version();
3064
3065 case '4':
3066 arg_family = AF_INET;
3067 break;
3068
3069 case '6':
3070 arg_family = AF_INET6;
3071 break;
3072
3073 case 'i':
3074 r = ifname_mangle(optarg);
3075 if (r < 0)
3076 return r;
3077 break;
3078
3079 case 't':
3080 if (streq(optarg, "help")) {
3081 help_dns_types();
3082 return 0;
3083 }
3084
3085 r = dns_type_from_string(optarg);
3086 if (r < 0) {
3087 log_error("Failed to parse RR record type %s", optarg);
3088 return r;
3089 }
3090 arg_type = (uint16_t) r;
3091 assert((int) arg_type == r);
3092
3093 break;
3094
3095 case 'c':
3096 if (streq(optarg, "help")) {
3097 help_dns_classes();
3098 return 0;
3099 }
3100
3101 r = dns_class_from_string(optarg);
3102 if (r < 0) {
3103 log_error("Failed to parse RR record class %s", optarg);
3104 return r;
3105 }
3106 arg_class = (uint16_t) r;
3107 assert((int) arg_class == r);
3108
3109 break;
3110
3111 case ARG_LEGEND:
3112 r = parse_boolean(optarg);
3113 if (r < 0)
3114 return log_error_errno(r, "Failed to parse --legend= argument");
3115
3116 arg_legend = r;
3117 break;
3118
3119 case 'p':
3120 if (streq(optarg, "help")) {
3121 help_protocol_types();
3122 return 0;
3123 } else if (streq(optarg, "dns"))
3124 arg_flags |= SD_RESOLVED_DNS;
3125 else if (streq(optarg, "llmnr"))
3126 arg_flags |= SD_RESOLVED_LLMNR;
3127 else if (streq(optarg, "llmnr-ipv4"))
3128 arg_flags |= SD_RESOLVED_LLMNR_IPV4;
3129 else if (streq(optarg, "llmnr-ipv6"))
3130 arg_flags |= SD_RESOLVED_LLMNR_IPV6;
3131 else if (streq(optarg, "mdns"))
3132 arg_flags |= SD_RESOLVED_MDNS;
3133 else if (streq(optarg, "mdns-ipv4"))
3134 arg_flags |= SD_RESOLVED_MDNS_IPV4;
3135 else if (streq(optarg, "mdns-ipv6"))
3136 arg_flags |= SD_RESOLVED_MDNS_IPV6;
3137 else
3138 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3139 "Unknown protocol specifier: %s",
3140 optarg);
3141
3142 break;
3143
3144 case ARG_RAW:
3145 if (on_tty())
3146 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY),
3147 "Refusing to write binary data to tty.");
3148
3149 if (optarg == NULL || streq(optarg, "payload"))
3150 arg_raw = RAW_PAYLOAD;
3151 else if (streq(optarg, "packet"))
3152 arg_raw = RAW_PACKET;
3153 else
3154 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3155 "Unknown --raw specifier \"%s\".",
3156 optarg);
3157
3158 arg_legend = false;
3159 break;
3160
3161 case ARG_CNAME:
3162 r = parse_boolean(optarg);
3163 if (r < 0)
3164 return log_error_errno(r, "Failed to parse --cname= argument.");
3165 SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0);
3166 break;
3167
3168 case ARG_SERVICE_ADDRESS:
3169 r = parse_boolean(optarg);
3170 if (r < 0)
3171 return log_error_errno(r, "Failed to parse --service-address= argument.");
3172 SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0);
3173 break;
3174
3175 case ARG_SERVICE_TXT:
3176 r = parse_boolean(optarg);
3177 if (r < 0)
3178 return log_error_errno(r, "Failed to parse --service-txt= argument.");
3179 SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0);
3180 break;
3181
3182 case ARG_SEARCH:
3183 r = parse_boolean(optarg);
3184 if (r < 0)
3185 return log_error_errno(r, "Failed to parse --search argument.");
3186 SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0);
3187 break;
3188
3189 case ARG_NO_PAGER:
3190 arg_pager_flags |= PAGER_DISABLE;
3191 break;
3192
3193 case '?':
3194 return -EINVAL;
3195
3196 default:
3197 assert_not_reached("Unhandled option");
3198 }
3199
3200 if (arg_type == 0 && arg_class != 0)
3201 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3202 "--class= may only be used in conjunction with --type=.");
3203
3204 if (arg_type != 0 && arg_class == 0)
3205 arg_class = DNS_CLASS_IN;
3206
3207 if (arg_class != 0 && arg_type == 0)
3208 arg_type = DNS_TYPE_A;
3209
3210 return 1 /* work to do */;
3211 }
3212
3213 static int native_main(int argc, char *argv[], sd_bus *bus) {
3214
3215 static const Verb verbs[] = {
3216 { "help", VERB_ANY, VERB_ANY, 0, verb_help },
3217 { "status", VERB_ANY, VERB_ANY, VERB_DEFAULT, verb_status },
3218 { "query", 2, VERB_ANY, 0, verb_query },
3219 { "service", 2, 4, 0, verb_service },
3220 { "openpgp", 2, VERB_ANY, 0, verb_openpgp },
3221 { "tlsa", 2, VERB_ANY, 0, verb_tlsa },
3222 { "statistics", VERB_ANY, 1, 0, show_statistics },
3223 { "reset-statistics", VERB_ANY, 1, 0, reset_statistics },
3224 { "flush-caches", VERB_ANY, 1, 0, flush_caches },
3225 { "reset-server-features", VERB_ANY, 1, 0, reset_server_features },
3226 { "dns", VERB_ANY, VERB_ANY, 0, verb_dns },
3227 { "domain", VERB_ANY, VERB_ANY, 0, verb_domain },
3228 { "default-route", VERB_ANY, 3, 0, verb_default_route },
3229 { "llmnr", VERB_ANY, 3, 0, verb_llmnr },
3230 { "mdns", VERB_ANY, 3, 0, verb_mdns },
3231 { "dnsovertls", VERB_ANY, 3, 0, verb_dns_over_tls },
3232 { "dnssec", VERB_ANY, 3, 0, verb_dnssec },
3233 { "nta", VERB_ANY, VERB_ANY, 0, verb_nta },
3234 { "revert", VERB_ANY, 2, 0, verb_revert_link },
3235 { "log-level", VERB_ANY, 2, 0, verb_log_level },
3236 {}
3237 };
3238
3239 return dispatch_verb(argc, argv, verbs, bus);
3240 }
3241
3242 static int translate(const char *verb, const char *single_arg, size_t num_args, char **args, sd_bus *bus) {
3243 char **fake, **p;
3244 size_t num, i;
3245
3246 assert(verb);
3247 assert(num_args == 0 || args);
3248
3249 num = !!single_arg + num_args + 1;
3250
3251 p = fake = newa0(char *, num + 1);
3252 *p++ = (char *) verb;
3253 if (single_arg)
3254 *p++ = (char *) single_arg;
3255 for (i = 0; i < num_args; i++)
3256 *p++ = args[i];
3257
3258 optind = 0;
3259 return native_main((int) num, fake, bus);
3260 }
3261
3262 static int compat_main(int argc, char *argv[], sd_bus *bus) {
3263 int r = 0;
3264
3265 switch (arg_mode) {
3266 case MODE_RESOLVE_HOST:
3267 case MODE_RESOLVE_RECORD:
3268 return translate("query", NULL, argc - optind, argv + optind, bus);
3269
3270 case MODE_RESOLVE_SERVICE:
3271 return translate("service", NULL, argc - optind, argv + optind, bus);
3272
3273 case MODE_RESOLVE_OPENPGP:
3274 return translate("openpgp", NULL, argc - optind, argv + optind, bus);
3275
3276 case MODE_RESOLVE_TLSA:
3277 return translate("tlsa", arg_service_family, argc - optind, argv + optind, bus);
3278
3279 case MODE_STATISTICS:
3280 return translate("statistics", NULL, 0, NULL, bus);
3281
3282 case MODE_RESET_STATISTICS:
3283 return translate("reset-statistics", NULL, 0, NULL, bus);
3284
3285 case MODE_FLUSH_CACHES:
3286 return translate("flush-caches", NULL, 0, NULL, bus);
3287
3288 case MODE_RESET_SERVER_FEATURES:
3289 return translate("reset-server-features", NULL, 0, NULL, bus);
3290
3291 case MODE_STATUS:
3292 return translate("status", NULL, argc - optind, argv + optind, bus);
3293
3294 case MODE_SET_LINK:
3295 assert(arg_ifname);
3296
3297 if (arg_set_dns) {
3298 r = translate("dns", arg_ifname, strv_length(arg_set_dns), arg_set_dns, bus);
3299 if (r < 0)
3300 return r;
3301 }
3302
3303 if (arg_set_domain) {
3304 r = translate("domain", arg_ifname, strv_length(arg_set_domain), arg_set_domain, bus);
3305 if (r < 0)
3306 return r;
3307 }
3308
3309 if (arg_set_nta) {
3310 r = translate("nta", arg_ifname, strv_length(arg_set_nta), arg_set_nta, bus);
3311 if (r < 0)
3312 return r;
3313 }
3314
3315 if (arg_set_llmnr) {
3316 r = translate("llmnr", arg_ifname, 1, (char **) &arg_set_llmnr, bus);
3317 if (r < 0)
3318 return r;
3319 }
3320
3321 if (arg_set_mdns) {
3322 r = translate("mdns", arg_ifname, 1, (char **) &arg_set_mdns, bus);
3323 if (r < 0)
3324 return r;
3325 }
3326
3327 if (arg_set_dns_over_tls) {
3328 r = translate("dnsovertls", arg_ifname, 1, (char **) &arg_set_dns_over_tls, bus);
3329 if (r < 0)
3330 return r;
3331 }
3332
3333 if (arg_set_dnssec) {
3334 r = translate("dnssec", arg_ifname, 1, (char **) &arg_set_dnssec, bus);
3335 if (r < 0)
3336 return r;
3337 }
3338
3339 return r;
3340
3341 case MODE_REVERT_LINK:
3342 assert(arg_ifname);
3343
3344 return translate("revert", arg_ifname, 0, NULL, bus);
3345
3346 case _MODE_INVALID:
3347 assert_not_reached("invalid mode");
3348 }
3349
3350 return 0;
3351 }
3352
3353 static int run(int argc, char **argv) {
3354 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
3355 int r;
3356
3357 setlocale(LC_ALL, "");
3358 log_show_color(true);
3359 log_parse_environment();
3360 log_open();
3361
3362 if (streq(program_invocation_short_name, "resolvconf"))
3363 r = resolvconf_parse_argv(argc, argv);
3364 else if (streq(program_invocation_short_name, "systemd-resolve"))
3365 r = compat_parse_argv(argc, argv);
3366 else
3367 r = native_parse_argv(argc, argv);
3368 if (r <= 0)
3369 return r;
3370
3371 r = sd_bus_open_system(&bus);
3372 if (r < 0)
3373 return log_error_errno(r, "sd_bus_open_system: %m");
3374
3375 if (STR_IN_SET(program_invocation_short_name, "systemd-resolve", "resolvconf"))
3376 return compat_main(argc, argv, bus);
3377
3378 return native_main(argc, argv, bus);
3379 }
3380
3381 DEFINE_MAIN_FUNCTION(run);