]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolvectl.c
resolvectl: rename systemd-resolve to resolvectl
[thirdparty/systemd.git] / src / resolve / resolvectl.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
6 ***/
7
8 #include <getopt.h>
9 #include <net/if.h>
10
11 #include "sd-bus.h"
12 #include "sd-netlink.h"
13
14 #include "af-list.h"
15 #include "alloc-util.h"
16 #include "bus-common-errors.h"
17 #include "bus-error.h"
18 #include "bus-util.h"
19 #include "dns-domain.h"
20 #include "escape.h"
21 #include "gcrypt-util.h"
22 #include "in-addr-util.h"
23 #include "netlink-util.h"
24 #include "pager.h"
25 #include "parse-util.h"
26 #include "resolvconf-compat.h"
27 #include "resolvectl.h"
28 #include "resolved-def.h"
29 #include "resolved-dns-packet.h"
30 #include "strv.h"
31 #include "terminal-util.h"
32
33 static int arg_family = AF_UNSPEC;
34 int arg_ifindex = 0;
35 static uint16_t arg_type = 0;
36 static uint16_t arg_class = 0;
37 static bool arg_legend = true;
38 static uint64_t arg_flags = 0;
39 static bool arg_no_pager = false;
40 bool arg_ifindex_permissive = false; /* If true, don't generate an error if the specified interface index doesn't exist */
41
42 typedef enum ServiceFamily {
43 SERVICE_FAMILY_TCP,
44 SERVICE_FAMILY_UDP,
45 SERVICE_FAMILY_SCTP,
46 _SERVICE_FAMILY_INVALID = -1,
47 } ServiceFamily;
48 static ServiceFamily arg_service_family = SERVICE_FAMILY_TCP;
49
50 typedef enum RawType {
51 RAW_NONE,
52 RAW_PAYLOAD,
53 RAW_PACKET,
54 } RawType;
55 static RawType arg_raw = RAW_NONE;
56
57 ExecutionMode arg_mode = MODE_RESOLVE_HOST;
58
59 struct in_addr_data *arg_set_dns = NULL;
60 size_t arg_n_set_dns = 0;
61 char **arg_set_domain = NULL;
62 static char *arg_set_llmnr = NULL;
63 static char *arg_set_mdns = NULL;
64 static char *arg_set_dnssec = NULL;
65 static char **arg_set_nta = NULL;
66
67 static ServiceFamily service_family_from_string(const char *s) {
68 if (!s || streq(s, "tcp"))
69 return SERVICE_FAMILY_TCP;
70 if (streq(s, "udp"))
71 return SERVICE_FAMILY_UDP;
72 if (streq(s, "sctp"))
73 return SERVICE_FAMILY_SCTP;
74 return _SERVICE_FAMILY_INVALID;
75 }
76
77 static const char* service_family_to_string(ServiceFamily service) {
78 switch(service) {
79 case SERVICE_FAMILY_TCP:
80 return "_tcp";
81 case SERVICE_FAMILY_UDP:
82 return "_udp";
83 case SERVICE_FAMILY_SCTP:
84 return "_sctp";
85 default:
86 assert_not_reached("invalid service");
87 }
88 }
89
90 static void print_source(uint64_t flags, usec_t rtt) {
91 char rtt_str[FORMAT_TIMESTAMP_MAX];
92
93 if (!arg_legend)
94 return;
95
96 if (flags == 0)
97 return;
98
99 fputs("\n-- Information acquired via", stdout);
100
101 if (flags != 0)
102 printf(" protocol%s%s%s%s%s",
103 flags & SD_RESOLVED_DNS ? " DNS" :"",
104 flags & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "",
105 flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "",
106 flags & SD_RESOLVED_MDNS_IPV4 ? " mDNS/IPv4" : "",
107 flags & SD_RESOLVED_MDNS_IPV6 ? " mDNS/IPv6" : "");
108
109 assert_se(format_timespan(rtt_str, sizeof(rtt_str), rtt, 100));
110
111 printf(" in %s", rtt_str);
112
113 fputc('.', stdout);
114 fputc('\n', stdout);
115
116 printf("-- Data is authenticated: %s\n", yes_no(flags & SD_RESOLVED_AUTHENTICATED));
117 }
118
119 static int resolve_host(sd_bus *bus, const char *name) {
120
121 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
122 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
123 const char *canonical = NULL;
124 char ifname[IF_NAMESIZE] = "";
125 unsigned c = 0;
126 int r;
127 uint64_t flags;
128 usec_t ts;
129
130 assert(name);
131
132 if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname))
133 return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex);
134
135 log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
136
137 r = sd_bus_message_new_method_call(
138 bus,
139 &req,
140 "org.freedesktop.resolve1",
141 "/org/freedesktop/resolve1",
142 "org.freedesktop.resolve1.Manager",
143 "ResolveHostname");
144 if (r < 0)
145 return bus_log_create_error(r);
146
147 r = sd_bus_message_append(req, "isit", arg_ifindex, name, arg_family, arg_flags);
148 if (r < 0)
149 return bus_log_create_error(r);
150
151 ts = now(CLOCK_MONOTONIC);
152
153 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
154 if (r < 0)
155 return log_error_errno(r, "%s: resolve call failed: %s", name, bus_error_message(&error, r));
156
157 ts = now(CLOCK_MONOTONIC) - ts;
158
159 r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
160 if (r < 0)
161 return bus_log_parse_error(r);
162
163 while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
164 _cleanup_free_ char *pretty = NULL;
165 int ifindex, family;
166 const void *a;
167 size_t sz;
168
169 assert_cc(sizeof(int) == sizeof(int32_t));
170
171 r = sd_bus_message_read(reply, "ii", &ifindex, &family);
172 if (r < 0)
173 return bus_log_parse_error(r);
174
175 r = sd_bus_message_read_array(reply, 'y', &a, &sz);
176 if (r < 0)
177 return bus_log_parse_error(r);
178
179 r = sd_bus_message_exit_container(reply);
180 if (r < 0)
181 return bus_log_parse_error(r);
182
183 if (!IN_SET(family, AF_INET, AF_INET6)) {
184 log_debug("%s: skipping entry with family %d (%s)", name, family, af_to_name(family) ?: "unknown");
185 continue;
186 }
187
188 if (sz != FAMILY_ADDRESS_SIZE(family)) {
189 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown");
190 return -EINVAL;
191 }
192
193 ifname[0] = 0;
194 if (ifindex > 0 && !if_indextoname(ifindex, ifname))
195 log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex);
196
197 r = in_addr_ifindex_to_string(family, a, ifindex, &pretty);
198 if (r < 0)
199 return log_error_errno(r, "Failed to print address for %s: %m", name);
200
201 printf("%*s%s %s%s%s\n",
202 (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
203 pretty,
204 isempty(ifname) ? "" : "%", ifname);
205
206 c++;
207 }
208 if (r < 0)
209 return bus_log_parse_error(r);
210
211 r = sd_bus_message_exit_container(reply);
212 if (r < 0)
213 return bus_log_parse_error(r);
214
215 r = sd_bus_message_read(reply, "st", &canonical, &flags);
216 if (r < 0)
217 return bus_log_parse_error(r);
218
219 if (!streq(name, canonical))
220 printf("%*s%s (%s)\n",
221 (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
222 canonical);
223
224 if (c == 0) {
225 log_error("%s: no addresses found", name);
226 return -ESRCH;
227 }
228
229 print_source(flags, ts);
230
231 return 0;
232 }
233
234 static int resolve_address(sd_bus *bus, int family, const union in_addr_union *address, int ifindex) {
235 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
236 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
237 _cleanup_free_ char *pretty = NULL;
238 char ifname[IF_NAMESIZE] = "";
239 uint64_t flags;
240 unsigned c = 0;
241 usec_t ts;
242 int r;
243
244 assert(bus);
245 assert(IN_SET(family, AF_INET, AF_INET6));
246 assert(address);
247
248 if (ifindex <= 0)
249 ifindex = arg_ifindex;
250
251 r = in_addr_ifindex_to_string(family, address, ifindex, &pretty);
252 if (r < 0)
253 return log_oom();
254
255 if (ifindex > 0 && !if_indextoname(ifindex, ifname))
256 return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex);
257
258 log_debug("Resolving %s%s%s.", pretty, isempty(ifname) ? "" : "%", ifname);
259
260 r = sd_bus_message_new_method_call(
261 bus,
262 &req,
263 "org.freedesktop.resolve1",
264 "/org/freedesktop/resolve1",
265 "org.freedesktop.resolve1.Manager",
266 "ResolveAddress");
267 if (r < 0)
268 return bus_log_create_error(r);
269
270 r = sd_bus_message_append(req, "ii", ifindex, family);
271 if (r < 0)
272 return bus_log_create_error(r);
273
274 r = sd_bus_message_append_array(req, 'y', address, FAMILY_ADDRESS_SIZE(family));
275 if (r < 0)
276 return bus_log_create_error(r);
277
278 r = sd_bus_message_append(req, "t", arg_flags);
279 if (r < 0)
280 return bus_log_create_error(r);
281
282 ts = now(CLOCK_MONOTONIC);
283
284 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
285 if (r < 0) {
286 log_error("%s: resolve call failed: %s", pretty, bus_error_message(&error, r));
287 return r;
288 }
289
290 ts = now(CLOCK_MONOTONIC) - ts;
291
292 r = sd_bus_message_enter_container(reply, 'a', "(is)");
293 if (r < 0)
294 return bus_log_create_error(r);
295
296 while ((r = sd_bus_message_enter_container(reply, 'r', "is")) > 0) {
297 const char *n;
298
299 assert_cc(sizeof(int) == sizeof(int32_t));
300
301 r = sd_bus_message_read(reply, "is", &ifindex, &n);
302 if (r < 0)
303 return r;
304
305 r = sd_bus_message_exit_container(reply);
306 if (r < 0)
307 return r;
308
309 ifname[0] = 0;
310 if (ifindex > 0 && !if_indextoname(ifindex, ifname))
311 log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex);
312
313 printf("%*s%*s%*s%s %s\n",
314 (int) strlen(pretty), c == 0 ? pretty : "",
315 isempty(ifname) ? 0 : 1, c > 0 || isempty(ifname) ? "" : "%",
316 (int) strlen(ifname), c == 0 ? ifname : "",
317 c == 0 ? ":" : " ",
318 n);
319
320 c++;
321 }
322 if (r < 0)
323 return bus_log_parse_error(r);
324
325 r = sd_bus_message_exit_container(reply);
326 if (r < 0)
327 return bus_log_parse_error(r);
328
329 r = sd_bus_message_read(reply, "t", &flags);
330 if (r < 0)
331 return bus_log_parse_error(r);
332
333 if (c == 0) {
334 log_error("%s: no names found", pretty);
335 return -ESRCH;
336 }
337
338 print_source(flags, ts);
339
340 return 0;
341 }
342
343 static int output_rr_packet(const void *d, size_t l, int ifindex) {
344 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
345 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
346 int r;
347 char ifname[IF_NAMESIZE] = "";
348
349 r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX);
350 if (r < 0)
351 return log_oom();
352
353 p->refuse_compression = true;
354
355 r = dns_packet_append_blob(p, d, l, NULL);
356 if (r < 0)
357 return log_oom();
358
359 r = dns_packet_read_rr(p, &rr, NULL, NULL);
360 if (r < 0)
361 return log_error_errno(r, "Failed to parse RR: %m");
362
363 if (arg_raw == RAW_PAYLOAD) {
364 void *data;
365 ssize_t k;
366
367 k = dns_resource_record_payload(rr, &data);
368 if (k < 0)
369 return log_error_errno(k, "Cannot dump RR: %m");
370 fwrite(data, 1, k, stdout);
371 } else {
372 const char *s;
373
374 s = dns_resource_record_to_string(rr);
375 if (!s)
376 return log_oom();
377
378 if (ifindex > 0 && !if_indextoname(ifindex, ifname))
379 log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex);
380
381 printf("%s%s%s\n", s, isempty(ifname) ? "" : " # interface ", ifname);
382 }
383
384 return 0;
385 }
386
387 static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type, bool warn_missing) {
388 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
389 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
390 char ifname[IF_NAMESIZE] = "";
391 unsigned n = 0;
392 uint64_t flags;
393 int r;
394 usec_t ts;
395 bool needs_authentication = false;
396
397 assert(name);
398
399 if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname))
400 return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex);
401
402 log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(class), dns_type_to_string(type), isempty(ifname) ? "*" : ifname);
403
404 r = sd_bus_message_new_method_call(
405 bus,
406 &req,
407 "org.freedesktop.resolve1",
408 "/org/freedesktop/resolve1",
409 "org.freedesktop.resolve1.Manager",
410 "ResolveRecord");
411 if (r < 0)
412 return bus_log_create_error(r);
413
414 r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, class, type, arg_flags);
415 if (r < 0)
416 return bus_log_create_error(r);
417
418 ts = now(CLOCK_MONOTONIC);
419
420 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
421 if (r < 0) {
422 if (warn_missing || r != -ENXIO)
423 log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r));
424 return r;
425 }
426
427 ts = now(CLOCK_MONOTONIC) - ts;
428
429 r = sd_bus_message_enter_container(reply, 'a', "(iqqay)");
430 if (r < 0)
431 return bus_log_parse_error(r);
432
433 while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) {
434 uint16_t c, t;
435 int ifindex;
436 const void *d;
437 size_t l;
438
439 assert_cc(sizeof(int) == sizeof(int32_t));
440
441 r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t);
442 if (r < 0)
443 return bus_log_parse_error(r);
444
445 r = sd_bus_message_read_array(reply, 'y', &d, &l);
446 if (r < 0)
447 return bus_log_parse_error(r);
448
449 r = sd_bus_message_exit_container(reply);
450 if (r < 0)
451 return bus_log_parse_error(r);
452
453 if (arg_raw == RAW_PACKET) {
454 uint64_t u64 = htole64(l);
455
456 fwrite(&u64, sizeof(u64), 1, stdout);
457 fwrite(d, 1, l, stdout);
458 } else {
459 r = output_rr_packet(d, l, ifindex);
460 if (r < 0)
461 return r;
462 }
463
464 if (dns_type_needs_authentication(t))
465 needs_authentication = true;
466
467 n++;
468 }
469 if (r < 0)
470 return bus_log_parse_error(r);
471
472 r = sd_bus_message_exit_container(reply);
473 if (r < 0)
474 return bus_log_parse_error(r);
475
476 r = sd_bus_message_read(reply, "t", &flags);
477 if (r < 0)
478 return bus_log_parse_error(r);
479
480 if (n == 0) {
481 if (warn_missing)
482 log_error("%s: no records found", name);
483 return -ESRCH;
484 }
485
486 print_source(flags, ts);
487
488 if ((flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) {
489 fflush(stdout);
490
491 fprintf(stderr, "\n%s"
492 "WARNING: The resources shown contain cryptographic key data which could not be\n"
493 " authenticated. It is not suitable to authenticate any communication.\n"
494 " This is usually indication that DNSSEC authentication was not enabled\n"
495 " or is not available for the selected protocol or DNS servers.%s\n",
496 ansi_highlight_red(),
497 ansi_normal());
498 }
499
500 return 0;
501 }
502
503 static int resolve_rfc4501(sd_bus *bus, const char *name) {
504 uint16_t type = 0, class = 0;
505 const char *p, *q, *n;
506 int r;
507
508 assert(bus);
509 assert(name);
510 assert(startswith(name, "dns:"));
511
512 /* Parse RFC 4501 dns: URIs */
513
514 p = name + 4;
515
516 if (p[0] == '/') {
517 const char *e;
518
519 if (p[1] != '/')
520 goto invalid;
521
522 e = strchr(p + 2, '/');
523 if (!e)
524 goto invalid;
525
526 if (e != p + 2)
527 log_warning("DNS authority specification not supported; ignoring specified authority.");
528
529 p = e + 1;
530 }
531
532 q = strchr(p, '?');
533 if (q) {
534 n = strndupa(p, q - p);
535 q++;
536
537 for (;;) {
538 const char *f;
539
540 f = startswith_no_case(q, "class=");
541 if (f) {
542 _cleanup_free_ char *t = NULL;
543 const char *e;
544
545 if (class != 0) {
546 log_error("DNS class specified twice.");
547 return -EINVAL;
548 }
549
550 e = strchrnul(f, ';');
551 t = strndup(f, e - f);
552 if (!t)
553 return log_oom();
554
555 r = dns_class_from_string(t);
556 if (r < 0) {
557 log_error("Unknown DNS class %s.", t);
558 return -EINVAL;
559 }
560
561 class = r;
562
563 if (*e == ';') {
564 q = e + 1;
565 continue;
566 }
567
568 break;
569 }
570
571 f = startswith_no_case(q, "type=");
572 if (f) {
573 _cleanup_free_ char *t = NULL;
574 const char *e;
575
576 if (type != 0) {
577 log_error("DNS type specified twice.");
578 return -EINVAL;
579 }
580
581 e = strchrnul(f, ';');
582 t = strndup(f, e - f);
583 if (!t)
584 return log_oom();
585
586 r = dns_type_from_string(t);
587 if (r < 0) {
588 log_error("Unknown DNS type %s.", t);
589 return -EINVAL;
590 }
591
592 type = r;
593
594 if (*e == ';') {
595 q = e + 1;
596 continue;
597 }
598
599 break;
600 }
601
602 goto invalid;
603 }
604 } else
605 n = p;
606
607 if (class == 0)
608 class = arg_class ?: DNS_CLASS_IN;
609 if (type == 0)
610 type = arg_type ?: DNS_TYPE_A;
611
612 return resolve_record(bus, n, class, type, true);
613
614 invalid:
615 log_error("Invalid DNS URI: %s", name);
616 return -EINVAL;
617 }
618
619 static int resolve_service(sd_bus *bus, const char *name, const char *type, const char *domain) {
620 const char *canonical_name, *canonical_type, *canonical_domain;
621 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
622 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
623 char ifname[IF_NAMESIZE] = "";
624 size_t indent, sz;
625 uint64_t flags;
626 const char *p;
627 unsigned c;
628 usec_t ts;
629 int r;
630
631 assert(bus);
632 assert(domain);
633
634 name = empty_to_null(name);
635 type = empty_to_null(type);
636
637 if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname))
638 return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex);
639
640 if (name)
641 log_debug("Resolving service \"%s\" of type %s in %s (family %s, interface %s).", name, type, domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
642 else if (type)
643 log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
644 else
645 log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
646
647 r = sd_bus_message_new_method_call(
648 bus,
649 &req,
650 "org.freedesktop.resolve1",
651 "/org/freedesktop/resolve1",
652 "org.freedesktop.resolve1.Manager",
653 "ResolveService");
654 if (r < 0)
655 return bus_log_create_error(r);
656
657 r = sd_bus_message_append(req, "isssit", arg_ifindex, name, type, domain, arg_family, arg_flags);
658 if (r < 0)
659 return bus_log_create_error(r);
660
661 ts = now(CLOCK_MONOTONIC);
662
663 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
664 if (r < 0)
665 return log_error_errno(r, "Resolve call failed: %s", bus_error_message(&error, r));
666
667 ts = now(CLOCK_MONOTONIC) - ts;
668
669 r = sd_bus_message_enter_container(reply, 'a', "(qqqsa(iiay)s)");
670 if (r < 0)
671 return bus_log_parse_error(r);
672
673 indent =
674 (name ? strlen(name) + 1 : 0) +
675 (type ? strlen(type) + 1 : 0) +
676 strlen(domain) + 2;
677
678 c = 0;
679 while ((r = sd_bus_message_enter_container(reply, 'r', "qqqsa(iiay)s")) > 0) {
680 uint16_t priority, weight, port;
681 const char *hostname, *canonical;
682
683 r = sd_bus_message_read(reply, "qqqs", &priority, &weight, &port, &hostname);
684 if (r < 0)
685 return bus_log_parse_error(r);
686
687 if (name)
688 printf("%*s%s", (int) strlen(name), c == 0 ? name : "", c == 0 ? "/" : " ");
689 if (type)
690 printf("%*s%s", (int) strlen(type), c == 0 ? type : "", c == 0 ? "/" : " ");
691
692 printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
693 (int) strlen(domain), c == 0 ? domain : "",
694 c == 0 ? ":" : " ",
695 hostname, port,
696 priority, weight);
697
698 r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
699 if (r < 0)
700 return bus_log_parse_error(r);
701
702 while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
703 _cleanup_free_ char *pretty = NULL;
704 int ifindex, family;
705 const void *a;
706
707 assert_cc(sizeof(int) == sizeof(int32_t));
708
709 r = sd_bus_message_read(reply, "ii", &ifindex, &family);
710 if (r < 0)
711 return bus_log_parse_error(r);
712
713 r = sd_bus_message_read_array(reply, 'y', &a, &sz);
714 if (r < 0)
715 return bus_log_parse_error(r);
716
717 r = sd_bus_message_exit_container(reply);
718 if (r < 0)
719 return bus_log_parse_error(r);
720
721 if (!IN_SET(family, AF_INET, AF_INET6)) {
722 log_debug("%s: skipping entry with family %d (%s)", name, family, af_to_name(family) ?: "unknown");
723 continue;
724 }
725
726 if (sz != FAMILY_ADDRESS_SIZE(family)) {
727 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown");
728 return -EINVAL;
729 }
730
731 ifname[0] = 0;
732 if (ifindex > 0 && !if_indextoname(ifindex, ifname))
733 log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex);
734
735 r = in_addr_to_string(family, a, &pretty);
736 if (r < 0)
737 return log_error_errno(r, "Failed to print address for %s: %m", name);
738
739 printf("%*s%s%s%s\n", (int) indent, "", pretty, isempty(ifname) ? "" : "%s", ifname);
740 }
741 if (r < 0)
742 return bus_log_parse_error(r);
743
744 r = sd_bus_message_exit_container(reply);
745 if (r < 0)
746 return bus_log_parse_error(r);
747
748 r = sd_bus_message_read(reply, "s", &canonical);
749 if (r < 0)
750 return bus_log_parse_error(r);
751
752 if (!streq(hostname, canonical))
753 printf("%*s(%s)\n", (int) indent, "", canonical);
754
755 r = sd_bus_message_exit_container(reply);
756 if (r < 0)
757 return bus_log_parse_error(r);
758
759 c++;
760 }
761 if (r < 0)
762 return bus_log_parse_error(r);
763
764 r = sd_bus_message_exit_container(reply);
765 if (r < 0)
766 return bus_log_parse_error(r);
767
768 r = sd_bus_message_enter_container(reply, 'a', "ay");
769 if (r < 0)
770 return bus_log_parse_error(r);
771
772 while ((r = sd_bus_message_read_array(reply, 'y', (const void**) &p, &sz)) > 0) {
773 _cleanup_free_ char *escaped = NULL;
774
775 escaped = cescape_length(p, sz);
776 if (!escaped)
777 return log_oom();
778
779 printf("%*s%s\n", (int) indent, "", escaped);
780 }
781 if (r < 0)
782 return bus_log_parse_error(r);
783
784 r = sd_bus_message_exit_container(reply);
785 if (r < 0)
786 return bus_log_parse_error(r);
787
788 r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags);
789 if (r < 0)
790 return bus_log_parse_error(r);
791
792 canonical_name = empty_to_null(canonical_name);
793 canonical_type = empty_to_null(canonical_type);
794
795 if (!streq_ptr(name, canonical_name) ||
796 !streq_ptr(type, canonical_type) ||
797 !streq_ptr(domain, canonical_domain)) {
798
799 printf("%*s(", (int) indent, "");
800
801 if (canonical_name)
802 printf("%s/", canonical_name);
803 if (canonical_type)
804 printf("%s/", canonical_type);
805
806 printf("%s)\n", canonical_domain);
807 }
808
809 print_source(flags, ts);
810
811 return 0;
812 }
813
814 static int resolve_openpgp(sd_bus *bus, const char *address) {
815 const char *domain, *full;
816 int r;
817 _cleanup_free_ char *hashed = NULL;
818
819 assert(bus);
820 assert(address);
821
822 domain = strrchr(address, '@');
823 if (!domain) {
824 log_error("Address does not contain '@': \"%s\"", address);
825 return -EINVAL;
826 } else if (domain == address || domain[1] == '\0') {
827 log_error("Address starts or ends with '@': \"%s\"", address);
828 return -EINVAL;
829 }
830 domain++;
831
832 r = string_hashsum_sha256(address, domain - 1 - address, &hashed);
833 if (r < 0)
834 return log_error_errno(r, "Hashing failed: %m");
835
836 strshorten(hashed, 56);
837
838 full = strjoina(hashed, "._openpgpkey.", domain);
839 log_debug("Looking up \"%s\".", full);
840
841 r = resolve_record(bus, full,
842 arg_class ?: DNS_CLASS_IN,
843 arg_type ?: DNS_TYPE_OPENPGPKEY, false);
844
845 if (IN_SET(r, -ENXIO, -ESRCH)) { /* NXDOMAIN or NODATA? */
846 hashed = NULL;
847 r = string_hashsum_sha224(address, domain - 1 - address, &hashed);
848 if (r < 0)
849 return log_error_errno(r, "Hashing failed: %m");
850
851 full = strjoina(hashed, "._openpgpkey.", domain);
852 log_debug("Looking up \"%s\".", full);
853
854 return resolve_record(bus, full,
855 arg_class ?: DNS_CLASS_IN,
856 arg_type ?: DNS_TYPE_OPENPGPKEY, true);
857 }
858
859 return r;
860 }
861
862 static int resolve_tlsa(sd_bus *bus, const char *address) {
863 const char *port;
864 uint16_t port_num = 443;
865 _cleanup_free_ char *full = NULL;
866 int r;
867
868 assert(bus);
869 assert(address);
870
871 port = strrchr(address, ':');
872 if (port) {
873 r = parse_ip_port(port + 1, &port_num);
874 if (r < 0)
875 return log_error_errno(r, "Invalid port \"%s\".", port + 1);
876
877 address = strndupa(address, port - address);
878 }
879
880 r = asprintf(&full, "_%u.%s.%s",
881 port_num,
882 service_family_to_string(arg_service_family),
883 address);
884 if (r < 0)
885 return log_oom();
886
887 log_debug("Looking up \"%s\".", full);
888
889 return resolve_record(bus, full,
890 arg_class ?: DNS_CLASS_IN,
891 arg_type ?: DNS_TYPE_TLSA, true);
892 }
893
894 static int show_statistics(sd_bus *bus) {
895 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
896 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
897 uint64_t n_current_transactions, n_total_transactions,
898 cache_size, n_cache_hit, n_cache_miss,
899 n_dnssec_secure, n_dnssec_insecure, n_dnssec_bogus, n_dnssec_indeterminate;
900 int r, dnssec_supported;
901
902 assert(bus);
903
904 r = sd_bus_get_property_trivial(bus,
905 "org.freedesktop.resolve1",
906 "/org/freedesktop/resolve1",
907 "org.freedesktop.resolve1.Manager",
908 "DNSSECSupported",
909 &error,
910 'b',
911 &dnssec_supported);
912 if (r < 0)
913 return log_error_errno(r, "Failed to get DNSSEC supported state: %s", bus_error_message(&error, r));
914
915 printf("DNSSEC supported by current servers: %s%s%s\n\n",
916 ansi_highlight(),
917 yes_no(dnssec_supported),
918 ansi_normal());
919
920 r = sd_bus_get_property(bus,
921 "org.freedesktop.resolve1",
922 "/org/freedesktop/resolve1",
923 "org.freedesktop.resolve1.Manager",
924 "TransactionStatistics",
925 &error,
926 &reply,
927 "(tt)");
928 if (r < 0)
929 return log_error_errno(r, "Failed to get transaction statistics: %s", bus_error_message(&error, r));
930
931 r = sd_bus_message_read(reply, "(tt)",
932 &n_current_transactions,
933 &n_total_transactions);
934 if (r < 0)
935 return bus_log_parse_error(r);
936
937 printf("%sTransactions%s\n"
938 "Current Transactions: %" PRIu64 "\n"
939 " Total Transactions: %" PRIu64 "\n",
940 ansi_highlight(),
941 ansi_normal(),
942 n_current_transactions,
943 n_total_transactions);
944
945 reply = sd_bus_message_unref(reply);
946
947 r = sd_bus_get_property(bus,
948 "org.freedesktop.resolve1",
949 "/org/freedesktop/resolve1",
950 "org.freedesktop.resolve1.Manager",
951 "CacheStatistics",
952 &error,
953 &reply,
954 "(ttt)");
955 if (r < 0)
956 return log_error_errno(r, "Failed to get cache statistics: %s", bus_error_message(&error, r));
957
958 r = sd_bus_message_read(reply, "(ttt)",
959 &cache_size,
960 &n_cache_hit,
961 &n_cache_miss);
962 if (r < 0)
963 return bus_log_parse_error(r);
964
965 printf("\n%sCache%s\n"
966 " Current Cache Size: %" PRIu64 "\n"
967 " Cache Hits: %" PRIu64 "\n"
968 " Cache Misses: %" PRIu64 "\n",
969 ansi_highlight(),
970 ansi_normal(),
971 cache_size,
972 n_cache_hit,
973 n_cache_miss);
974
975 reply = sd_bus_message_unref(reply);
976
977 r = sd_bus_get_property(bus,
978 "org.freedesktop.resolve1",
979 "/org/freedesktop/resolve1",
980 "org.freedesktop.resolve1.Manager",
981 "DNSSECStatistics",
982 &error,
983 &reply,
984 "(tttt)");
985 if (r < 0)
986 return log_error_errno(r, "Failed to get DNSSEC statistics: %s", bus_error_message(&error, r));
987
988 r = sd_bus_message_read(reply, "(tttt)",
989 &n_dnssec_secure,
990 &n_dnssec_insecure,
991 &n_dnssec_bogus,
992 &n_dnssec_indeterminate);
993 if (r < 0)
994 return bus_log_parse_error(r);
995
996 printf("\n%sDNSSEC Verdicts%s\n"
997 " Secure: %" PRIu64 "\n"
998 " Insecure: %" PRIu64 "\n"
999 " Bogus: %" PRIu64 "\n"
1000 " Indeterminate: %" PRIu64 "\n",
1001 ansi_highlight(),
1002 ansi_normal(),
1003 n_dnssec_secure,
1004 n_dnssec_insecure,
1005 n_dnssec_bogus,
1006 n_dnssec_indeterminate);
1007
1008 return 0;
1009 }
1010
1011 static int reset_statistics(sd_bus *bus) {
1012 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1013 int r;
1014
1015 r = sd_bus_call_method(bus,
1016 "org.freedesktop.resolve1",
1017 "/org/freedesktop/resolve1",
1018 "org.freedesktop.resolve1.Manager",
1019 "ResetStatistics",
1020 &error,
1021 NULL,
1022 NULL);
1023 if (r < 0)
1024 return log_error_errno(r, "Failed to reset statistics: %s", bus_error_message(&error, r));
1025
1026 return 0;
1027 }
1028
1029 static int flush_caches(sd_bus *bus) {
1030 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1031 int r;
1032
1033 r = sd_bus_call_method(bus,
1034 "org.freedesktop.resolve1",
1035 "/org/freedesktop/resolve1",
1036 "org.freedesktop.resolve1.Manager",
1037 "FlushCaches",
1038 &error,
1039 NULL,
1040 NULL);
1041 if (r < 0)
1042 return log_error_errno(r, "Failed to flush caches: %s", bus_error_message(&error, r));
1043
1044 return 0;
1045 }
1046
1047 static int reset_server_features(sd_bus *bus) {
1048 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1049 int r;
1050
1051 r = sd_bus_call_method(bus,
1052 "org.freedesktop.resolve1",
1053 "/org/freedesktop/resolve1",
1054 "org.freedesktop.resolve1.Manager",
1055 "ResetServerFeatures",
1056 &error,
1057 NULL,
1058 NULL);
1059 if (r < 0)
1060 return log_error_errno(r, "Failed to reset server features: %s", bus_error_message(&error, r));
1061
1062 return 0;
1063 }
1064
1065 static int map_link_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1066 char ***l = userdata;
1067 int r;
1068
1069 assert(bus);
1070 assert(member);
1071 assert(m);
1072 assert(l);
1073
1074 r = sd_bus_message_enter_container(m, 'a', "(iay)");
1075 if (r < 0)
1076 return r;
1077
1078 for (;;) {
1079 const void *a;
1080 char *pretty;
1081 int family;
1082 size_t sz;
1083
1084 r = sd_bus_message_enter_container(m, 'r', "iay");
1085 if (r < 0)
1086 return r;
1087 if (r == 0)
1088 break;
1089
1090 r = sd_bus_message_read(m, "i", &family);
1091 if (r < 0)
1092 return r;
1093
1094 r = sd_bus_message_read_array(m, 'y', &a, &sz);
1095 if (r < 0)
1096 return r;
1097
1098 r = sd_bus_message_exit_container(m);
1099 if (r < 0)
1100 return r;
1101
1102 if (!IN_SET(family, AF_INET, AF_INET6)) {
1103 log_debug("Unexpected family, ignoring.");
1104 continue;
1105 }
1106
1107 if (sz != FAMILY_ADDRESS_SIZE(family)) {
1108 log_debug("Address size mismatch, ignoring.");
1109 continue;
1110 }
1111
1112 r = in_addr_to_string(family, a, &pretty);
1113 if (r < 0)
1114 return r;
1115
1116 r = strv_consume(l, pretty);
1117 if (r < 0)
1118 return r;
1119 }
1120
1121 r = sd_bus_message_exit_container(m);
1122 if (r < 0)
1123 return r;
1124
1125 return 0;
1126 }
1127
1128 static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1129 char ***l = userdata;
1130 int r;
1131
1132 assert(bus);
1133 assert(member);
1134 assert(m);
1135 assert(l);
1136
1137 r = sd_bus_message_enter_container(m, 'a', "(sb)");
1138 if (r < 0)
1139 return r;
1140
1141 for (;;) {
1142 const char *domain;
1143 int route_only;
1144 char *pretty;
1145
1146 r = sd_bus_message_read(m, "(sb)", &domain, &route_only);
1147 if (r < 0)
1148 return r;
1149 if (r == 0)
1150 break;
1151
1152 if (route_only)
1153 pretty = strappend("~", domain);
1154 else
1155 pretty = strdup(domain);
1156 if (!pretty)
1157 return -ENOMEM;
1158
1159 r = strv_consume(l, pretty);
1160 if (r < 0)
1161 return r;
1162 }
1163
1164 r = sd_bus_message_exit_container(m);
1165 if (r < 0)
1166 return r;
1167
1168 return 0;
1169 }
1170
1171 static int status_ifindex(sd_bus *bus, int ifindex, const char *name, bool *empty_line) {
1172
1173 struct link_info {
1174 uint64_t scopes_mask;
1175 const char *llmnr;
1176 const char *mdns;
1177 const char *dnssec;
1178 char **dns;
1179 char **domains;
1180 char **ntas;
1181 bool dnssec_supported;
1182 } link_info = {};
1183
1184 static const struct bus_properties_map property_map[] = {
1185 { "ScopesMask", "t", NULL, offsetof(struct link_info, scopes_mask) },
1186 { "DNS", "a(iay)", map_link_dns_servers, offsetof(struct link_info, dns) },
1187 { "Domains", "a(sb)", map_link_domains, offsetof(struct link_info, domains) },
1188 { "LLMNR", "s", NULL, offsetof(struct link_info, llmnr) },
1189 { "MulticastDNS", "s", NULL, offsetof(struct link_info, mdns) },
1190 { "DNSSEC", "s", NULL, offsetof(struct link_info, dnssec) },
1191 { "DNSSECNegativeTrustAnchors", "as", NULL, offsetof(struct link_info, ntas) },
1192 { "DNSSECSupported", "b", NULL, offsetof(struct link_info, dnssec_supported) },
1193 {}
1194 };
1195
1196 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1197 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1198 _cleanup_free_ char *ifi = NULL, *p = NULL;
1199 char ifname[IF_NAMESIZE] = "";
1200 char **i;
1201 int r;
1202
1203 assert(bus);
1204 assert(ifindex > 0);
1205 assert(empty_line);
1206
1207 if (!name) {
1208 if (!if_indextoname(ifindex, ifname))
1209 return log_error_errno(errno, "Failed to resolve interface name for %i: %m", ifindex);
1210
1211 name = ifname;
1212 }
1213
1214 if (asprintf(&ifi, "%i", ifindex) < 0)
1215 return log_oom();
1216
1217 r = sd_bus_path_encode("/org/freedesktop/resolve1/link", ifi, &p);
1218 if (r < 0)
1219 return log_oom();
1220
1221 r = bus_map_all_properties(bus,
1222 "org.freedesktop.resolve1",
1223 p,
1224 property_map,
1225 BUS_MAP_BOOLEAN_AS_BOOL,
1226 &error,
1227 &m,
1228 &link_info);
1229 if (r < 0) {
1230 log_error_errno(r, "Failed to get link data for %i: %s", ifindex, bus_error_message(&error, r));
1231 goto finish;
1232 }
1233
1234 (void) pager_open(arg_no_pager, false);
1235
1236 if (*empty_line)
1237 fputc('\n', stdout);
1238
1239 printf("%sLink %i (%s)%s\n",
1240 ansi_highlight(), ifindex, name, ansi_normal());
1241
1242 if (link_info.scopes_mask == 0)
1243 printf(" Current Scopes: none\n");
1244 else
1245 printf(" Current Scopes:%s%s%s%s%s\n",
1246 link_info.scopes_mask & SD_RESOLVED_DNS ? " DNS" : "",
1247 link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "",
1248 link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "",
1249 link_info.scopes_mask & SD_RESOLVED_MDNS_IPV4 ? " mDNS/IPv4" : "",
1250 link_info.scopes_mask & SD_RESOLVED_MDNS_IPV6 ? " mDNS/IPv6" : "");
1251
1252 printf(" LLMNR setting: %s\n"
1253 "MulticastDNS setting: %s\n"
1254 " DNSSEC setting: %s\n"
1255 " DNSSEC supported: %s\n",
1256 strna(link_info.llmnr),
1257 strna(link_info.mdns),
1258 strna(link_info.dnssec),
1259 yes_no(link_info.dnssec_supported));
1260
1261 STRV_FOREACH(i, link_info.dns) {
1262 printf(" %s %s\n",
1263 i == link_info.dns ? "DNS Servers:" : " ",
1264 *i);
1265 }
1266
1267 STRV_FOREACH(i, link_info.domains) {
1268 printf(" %s %s\n",
1269 i == link_info.domains ? "DNS Domain:" : " ",
1270 *i);
1271 }
1272
1273 STRV_FOREACH(i, link_info.ntas) {
1274 printf(" %s %s\n",
1275 i == link_info.ntas ? "DNSSEC NTA:" : " ",
1276 *i);
1277 }
1278
1279 *empty_line = true;
1280
1281 r = 0;
1282
1283 finish:
1284 strv_free(link_info.dns);
1285 strv_free(link_info.domains);
1286 strv_free(link_info.ntas);
1287 return r;
1288 }
1289
1290 static int map_global_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1291 char ***l = userdata;
1292 int r;
1293
1294 assert(bus);
1295 assert(member);
1296 assert(m);
1297 assert(l);
1298
1299 r = sd_bus_message_enter_container(m, 'a', "(iiay)");
1300 if (r < 0)
1301 return r;
1302
1303 for (;;) {
1304 const void *a;
1305 char *pretty;
1306 int family, ifindex;
1307 size_t sz;
1308
1309 r = sd_bus_message_enter_container(m, 'r', "iiay");
1310 if (r < 0)
1311 return r;
1312 if (r == 0)
1313 break;
1314
1315 r = sd_bus_message_read(m, "ii", &ifindex, &family);
1316 if (r < 0)
1317 return r;
1318
1319 r = sd_bus_message_read_array(m, 'y', &a, &sz);
1320 if (r < 0)
1321 return r;
1322
1323 r = sd_bus_message_exit_container(m);
1324 if (r < 0)
1325 return r;
1326
1327 if (ifindex != 0) /* only show the global ones here */
1328 continue;
1329
1330 if (!IN_SET(family, AF_INET, AF_INET6)) {
1331 log_debug("Unexpected family, ignoring.");
1332 continue;
1333 }
1334
1335 if (sz != FAMILY_ADDRESS_SIZE(family)) {
1336 log_debug("Address size mismatch, ignoring.");
1337 continue;
1338 }
1339
1340 r = in_addr_to_string(family, a, &pretty);
1341 if (r < 0)
1342 return r;
1343
1344 r = strv_consume(l, pretty);
1345 if (r < 0)
1346 return r;
1347 }
1348
1349 r = sd_bus_message_exit_container(m);
1350 if (r < 0)
1351 return r;
1352
1353 return 0;
1354 }
1355
1356 static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1357 char ***l = userdata;
1358 int r;
1359
1360 assert(bus);
1361 assert(member);
1362 assert(m);
1363 assert(l);
1364
1365 r = sd_bus_message_enter_container(m, 'a', "(isb)");
1366 if (r < 0)
1367 return r;
1368
1369 for (;;) {
1370 const char *domain;
1371 int route_only, ifindex;
1372 char *pretty;
1373
1374 r = sd_bus_message_read(m, "(isb)", &ifindex, &domain, &route_only);
1375 if (r < 0)
1376 return r;
1377 if (r == 0)
1378 break;
1379
1380 if (ifindex != 0) /* only show the global ones here */
1381 continue;
1382
1383 if (route_only)
1384 pretty = strappend("~", domain);
1385 else
1386 pretty = strdup(domain);
1387 if (!pretty)
1388 return -ENOMEM;
1389
1390 r = strv_consume(l, pretty);
1391 if (r < 0)
1392 return r;
1393 }
1394
1395 r = sd_bus_message_exit_container(m);
1396 if (r < 0)
1397 return r;
1398
1399 return 0;
1400 }
1401
1402 static int status_global(sd_bus *bus, bool *empty_line) {
1403
1404 struct global_info {
1405 char **dns;
1406 char **domains;
1407 char **ntas;
1408 const char *llmnr;
1409 const char *mdns;
1410 const char *dnssec;
1411 bool dnssec_supported;
1412 } global_info = {};
1413
1414 static const struct bus_properties_map property_map[] = {
1415 { "DNS", "a(iiay)", map_global_dns_servers, offsetof(struct global_info, dns) },
1416 { "Domains", "a(isb)", map_global_domains, offsetof(struct global_info, domains) },
1417 { "DNSSECNegativeTrustAnchors", "as", NULL, offsetof(struct global_info, ntas) },
1418 { "LLMNR", "s", NULL, offsetof(struct global_info, llmnr) },
1419 { "MulticastDNS", "s", NULL, offsetof(struct global_info, mdns) },
1420 { "DNSSEC", "s", NULL, offsetof(struct global_info, dnssec) },
1421 { "DNSSECSupported", "b", NULL, offsetof(struct global_info, dnssec_supported) },
1422 {}
1423 };
1424
1425 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1426 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1427 char **i;
1428 int r;
1429
1430 assert(bus);
1431 assert(empty_line);
1432
1433 r = bus_map_all_properties(bus,
1434 "org.freedesktop.resolve1",
1435 "/org/freedesktop/resolve1",
1436 property_map,
1437 BUS_MAP_BOOLEAN_AS_BOOL,
1438 &error,
1439 &m,
1440 &global_info);
1441 if (r < 0) {
1442 log_error_errno(r, "Failed to get global data: %s", bus_error_message(&error, r));
1443 goto finish;
1444 }
1445
1446 (void) pager_open(arg_no_pager, false);
1447
1448 printf("%sGlobal%s\n", ansi_highlight(), ansi_normal());
1449
1450 printf(" LLMNR setting: %s\n"
1451 "MulticastDNS setting: %s\n"
1452 " DNSSEC setting: %s\n"
1453 " DNSSEC supported: %s\n",
1454 strna(global_info.llmnr),
1455 strna(global_info.mdns),
1456 strna(global_info.dnssec),
1457 yes_no(global_info.dnssec_supported));
1458
1459 STRV_FOREACH(i, global_info.dns) {
1460 printf(" %s %s\n",
1461 i == global_info.dns ? "DNS Servers:" : " ",
1462 *i);
1463 }
1464
1465 STRV_FOREACH(i, global_info.domains) {
1466 printf(" %s %s\n",
1467 i == global_info.domains ? "DNS Domain:" : " ",
1468 *i);
1469 }
1470
1471 strv_sort(global_info.ntas);
1472 STRV_FOREACH(i, global_info.ntas) {
1473 printf(" %s %s\n",
1474 i == global_info.ntas ? "DNSSEC NTA:" : " ",
1475 *i);
1476 }
1477
1478 *empty_line = true;
1479
1480 r = 0;
1481
1482 finish:
1483 strv_free(global_info.dns);
1484 strv_free(global_info.domains);
1485 strv_free(global_info.ntas);
1486
1487 return r;
1488 }
1489
1490 static int status_all(sd_bus *bus) {
1491 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1492 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
1493 sd_netlink_message *i;
1494 bool empty_line = false;
1495 int r;
1496
1497 assert(bus);
1498
1499 r = status_global(bus, &empty_line);
1500 if (r < 0)
1501 return r;
1502
1503 r = sd_netlink_open(&rtnl);
1504 if (r < 0)
1505 return log_error_errno(r, "Failed to connect to netlink: %m");
1506
1507 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
1508 if (r < 0)
1509 return rtnl_log_create_error(r);
1510
1511 r = sd_netlink_message_request_dump(req, true);
1512 if (r < 0)
1513 return rtnl_log_create_error(r);
1514
1515 r = sd_netlink_call(rtnl, req, 0, &reply);
1516 if (r < 0)
1517 return log_error_errno(r, "Failed to enumerate links: %m");
1518
1519 r = 0;
1520 for (i = reply; i; i = sd_netlink_message_next(i)) {
1521 const char *name;
1522 int ifindex, q;
1523 uint16_t type;
1524
1525 q = sd_netlink_message_get_type(i, &type);
1526 if (q < 0)
1527 return rtnl_log_parse_error(q);
1528
1529 if (type != RTM_NEWLINK)
1530 continue;
1531
1532 q = sd_rtnl_message_link_get_ifindex(i, &ifindex);
1533 if (q < 0)
1534 return rtnl_log_parse_error(q);
1535
1536 if (ifindex == LOOPBACK_IFINDEX)
1537 continue;
1538
1539 q = sd_netlink_message_read_string(i, IFLA_IFNAME, &name);
1540 if (q < 0)
1541 return rtnl_log_parse_error(q);
1542
1543 q = status_ifindex(bus, ifindex, name, &empty_line);
1544 if (q < 0 && r >= 0)
1545 r = q;
1546 }
1547
1548 return r;
1549 }
1550
1551 static int set_link(sd_bus *bus) {
1552 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1553 int r = 0, q;
1554
1555 assert(bus);
1556
1557 if (arg_n_set_dns > 0) {
1558 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
1559 size_t i;
1560
1561 q = sd_bus_message_new_method_call(
1562 bus,
1563 &req,
1564 "org.freedesktop.resolve1",
1565 "/org/freedesktop/resolve1",
1566 "org.freedesktop.resolve1.Manager",
1567 "SetLinkDNS");
1568 if (q < 0)
1569 return bus_log_create_error(q);
1570
1571 q = sd_bus_message_append(req, "i", arg_ifindex);
1572 if (q < 0)
1573 return bus_log_create_error(q);
1574
1575 q = sd_bus_message_open_container(req, 'a', "(iay)");
1576 if (q < 0)
1577 return bus_log_create_error(q);
1578
1579 for (i = 0; i < arg_n_set_dns; i++) {
1580 q = sd_bus_message_open_container(req, 'r', "iay");
1581 if (q < 0)
1582 return bus_log_create_error(q);
1583
1584 q = sd_bus_message_append(req, "i", arg_set_dns[i].family);
1585 if (q < 0)
1586 return bus_log_create_error(q);
1587
1588 q = sd_bus_message_append_array(req, 'y', &arg_set_dns[i].address, FAMILY_ADDRESS_SIZE(arg_set_dns[i].family));
1589 if (q < 0)
1590 return bus_log_create_error(q);
1591
1592 q = sd_bus_message_close_container(req);
1593 if (q < 0)
1594 return bus_log_create_error(q);
1595 }
1596
1597 q = sd_bus_message_close_container(req);
1598 if (q < 0)
1599 return bus_log_create_error(q);
1600
1601 q = sd_bus_call(bus, req, 0, &error, NULL);
1602 if (q < 0) {
1603 if (sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY))
1604 goto is_managed;
1605 if (arg_ifindex_permissive &&
1606 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
1607 return 0;
1608
1609 log_error_errno(q, "Failed to set DNS configuration: %s", bus_error_message(&error, q));
1610 if (r == 0)
1611 r = q;
1612 }
1613 }
1614
1615 if (!strv_isempty(arg_set_domain)) {
1616 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
1617 char **p;
1618
1619 q = sd_bus_message_new_method_call(
1620 bus,
1621 &req,
1622 "org.freedesktop.resolve1",
1623 "/org/freedesktop/resolve1",
1624 "org.freedesktop.resolve1.Manager",
1625 "SetLinkDomains");
1626 if (q < 0)
1627 return bus_log_create_error(q);
1628
1629 q = sd_bus_message_append(req, "i", arg_ifindex);
1630 if (q < 0)
1631 return bus_log_create_error(q);
1632
1633 q = sd_bus_message_open_container(req, 'a', "(sb)");
1634 if (q < 0)
1635 return bus_log_create_error(q);
1636
1637 STRV_FOREACH(p, arg_set_domain) {
1638 const char *n;
1639
1640 n = **p == '~' ? *p + 1 : *p;
1641 q = sd_bus_message_append(req, "(sb)", n, **p == '~');
1642 if (q < 0)
1643 return bus_log_create_error(q);
1644 }
1645
1646 q = sd_bus_message_close_container(req);
1647 if (q < 0)
1648 return bus_log_create_error(q);
1649
1650 q = sd_bus_call(bus, req, 0, &error, NULL);
1651 if (q < 0) {
1652 if (sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY))
1653 goto is_managed;
1654 if (arg_ifindex_permissive &&
1655 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
1656 return 0;
1657
1658 log_error_errno(q, "Failed to set domain configuration: %s", bus_error_message(&error, q));
1659 if (r == 0)
1660 r = q;
1661 }
1662 }
1663
1664 if (arg_set_llmnr) {
1665 q = sd_bus_call_method(bus,
1666 "org.freedesktop.resolve1",
1667 "/org/freedesktop/resolve1",
1668 "org.freedesktop.resolve1.Manager",
1669 "SetLinkLLMNR",
1670 &error,
1671 NULL,
1672 "is", arg_ifindex, arg_set_llmnr);
1673 if (q < 0) {
1674 if (sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY))
1675 goto is_managed;
1676
1677 log_error_errno(q, "Failed to set LLMNR configuration: %s", bus_error_message(&error, q));
1678 if (r == 0)
1679 r = q;
1680 }
1681 }
1682
1683 if (arg_set_mdns) {
1684 q = sd_bus_call_method(bus,
1685 "org.freedesktop.resolve1",
1686 "/org/freedesktop/resolve1",
1687 "org.freedesktop.resolve1.Manager",
1688 "SetLinkMulticastDNS",
1689 &error,
1690 NULL,
1691 "is", arg_ifindex, arg_set_mdns);
1692 if (q < 0) {
1693 if (sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY))
1694 goto is_managed;
1695
1696 log_error_errno(q, "Failed to set MulticastDNS configuration: %s", bus_error_message(&error, q));
1697 if (r == 0)
1698 r = q;
1699 }
1700 }
1701
1702 if (arg_set_dnssec) {
1703 q = sd_bus_call_method(bus,
1704 "org.freedesktop.resolve1",
1705 "/org/freedesktop/resolve1",
1706 "org.freedesktop.resolve1.Manager",
1707 "SetLinkDNSSEC",
1708 &error,
1709 NULL,
1710 "is", arg_ifindex, arg_set_dnssec);
1711 if (q < 0) {
1712 if (sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY))
1713 goto is_managed;
1714
1715 log_error_errno(q, "Failed to set DNSSEC configuration: %s", bus_error_message(&error, q));
1716 if (r == 0)
1717 r = q;
1718 }
1719 }
1720
1721 if (!strv_isempty(arg_set_nta)) {
1722 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
1723
1724 q = sd_bus_message_new_method_call(
1725 bus,
1726 &req,
1727 "org.freedesktop.resolve1",
1728 "/org/freedesktop/resolve1",
1729 "org.freedesktop.resolve1.Manager",
1730 "SetLinkDNSSECNegativeTrustAnchors");
1731 if (q < 0)
1732 return bus_log_create_error(q);
1733
1734 q = sd_bus_message_append(req, "i", arg_ifindex);
1735 if (q < 0)
1736 return bus_log_create_error(q);
1737
1738 q = sd_bus_message_append_strv(req, arg_set_nta);
1739 if (q < 0)
1740 return bus_log_create_error(q);
1741
1742 q = sd_bus_call(bus, req, 0, &error, NULL);
1743 if (q < 0) {
1744 if (sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY))
1745 goto is_managed;
1746
1747 log_error_errno(q, "Failed to set DNSSEC NTA configuration: %s", bus_error_message(&error, q));
1748 if (r == 0)
1749 r = q;
1750 }
1751 }
1752
1753 return r;
1754
1755 is_managed:
1756 {
1757 char ifname[IFNAMSIZ];
1758
1759 return log_error_errno(q,
1760 "The specified interface %s is managed by systemd-networkd. Operation refused.\n"
1761 "Please configure DNS settings for systemd-networkd managed interfaces directly in their .network files.", strna(if_indextoname(arg_ifindex, ifname)));
1762 }
1763 }
1764
1765 static int revert_link(sd_bus *bus) {
1766 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1767 int r;
1768
1769 assert(bus);
1770
1771 r = sd_bus_call_method(bus,
1772 "org.freedesktop.resolve1",
1773 "/org/freedesktop/resolve1",
1774 "org.freedesktop.resolve1.Manager",
1775 "RevertLink",
1776 &error,
1777 NULL,
1778 "i", arg_ifindex);
1779 if (r < 0) {
1780 if (arg_ifindex_permissive &&
1781 sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK))
1782 return 0;
1783
1784 return log_error_errno(r, "Failed to revert interface configuration: %s", bus_error_message(&error, r));
1785 }
1786
1787 return 0;
1788 }
1789
1790 static void help_protocol_types(void) {
1791 if (arg_legend)
1792 puts("Known protocol types:");
1793 puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6\nmdns\nmdns-ipv4\nmdns-ipv6");
1794 }
1795
1796 static void help_dns_types(void) {
1797 const char *t;
1798 int i;
1799
1800 if (arg_legend)
1801 puts("Known DNS RR types:");
1802 for (i = 0; i < _DNS_TYPE_MAX; i++) {
1803 t = dns_type_to_string(i);
1804 if (t)
1805 puts(t);
1806 }
1807 }
1808
1809 static void help_dns_classes(void) {
1810 const char *t;
1811 int i;
1812
1813 if (arg_legend)
1814 puts("Known DNS RR classes:");
1815 for (i = 0; i < _DNS_CLASS_MAX; i++) {
1816 t = dns_class_to_string(i);
1817 if (t)
1818 puts(t);
1819 }
1820 }
1821
1822 static void native_help(void) {
1823 printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n"
1824 "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n"
1825 "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n"
1826 "%1$s [OPTIONS...] --statistics\n"
1827 "%1$s [OPTIONS...] --reset-statistics\n"
1828 "\n"
1829 "Resolve domain names, IPv4 and IPv6 addresses, DNS records, and services.\n\n"
1830 " -h --help Show this help\n"
1831 " --version Show package version\n"
1832 " --no-pager Do not pipe output into a pager\n"
1833 " -4 Resolve IPv4 addresses\n"
1834 " -6 Resolve IPv6 addresses\n"
1835 " -i --interface=INTERFACE Look on interface\n"
1836 " -p --protocol=PROTO|help Look via protocol\n"
1837 " -t --type=TYPE|help Query RR with DNS type\n"
1838 " -c --class=CLASS|help Query RR with DNS class\n"
1839 " --service Resolve service (SRV)\n"
1840 " --service-address=BOOL Resolve address for services (default: yes)\n"
1841 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
1842 " --openpgp Query OpenPGP public key\n"
1843 " --tlsa Query TLS public key\n"
1844 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
1845 " --search=BOOL Use search domains for single-label names\n"
1846 " (default: yes)\n"
1847 " --raw[=payload|packet] Dump the answer as binary data\n"
1848 " --legend=BOOL Print headers and additional info (default: yes)\n"
1849 " --statistics Show resolver statistics\n"
1850 " --reset-statistics Reset resolver statistics\n"
1851 " --status Show link and server status\n"
1852 " --flush-caches Flush all local DNS caches\n"
1853 " --reset-server-features\n"
1854 " Forget learnt DNS server feature levels\n"
1855 " --set-dns=SERVER Set per-interface DNS server address\n"
1856 " --set-domain=DOMAIN Set per-interface search domain\n"
1857 " --set-llmnr=MODE Set per-interface LLMNR mode\n"
1858 " --set-mdns=MODE Set per-interface MulticastDNS mode\n"
1859 " --set-dnssec=MODE Set per-interface DNSSEC mode\n"
1860 " --set-nta=DOMAIN Set per-interface DNSSEC NTA\n"
1861 " --revert Revert per-interface configuration\n"
1862 , program_invocation_short_name);
1863 }
1864
1865 static int native_parse_argv(int argc, char *argv[]) {
1866 enum {
1867 ARG_VERSION = 0x100,
1868 ARG_LEGEND,
1869 ARG_SERVICE,
1870 ARG_CNAME,
1871 ARG_SERVICE_ADDRESS,
1872 ARG_SERVICE_TXT,
1873 ARG_OPENPGP,
1874 ARG_TLSA,
1875 ARG_RAW,
1876 ARG_SEARCH,
1877 ARG_STATISTICS,
1878 ARG_RESET_STATISTICS,
1879 ARG_STATUS,
1880 ARG_FLUSH_CACHES,
1881 ARG_RESET_SERVER_FEATURES,
1882 ARG_NO_PAGER,
1883 ARG_SET_DNS,
1884 ARG_SET_DOMAIN,
1885 ARG_SET_LLMNR,
1886 ARG_SET_MDNS,
1887 ARG_SET_DNSSEC,
1888 ARG_SET_NTA,
1889 ARG_REVERT_LINK,
1890 };
1891
1892 static const struct option options[] = {
1893 { "help", no_argument, NULL, 'h' },
1894 { "version", no_argument, NULL, ARG_VERSION },
1895 { "type", required_argument, NULL, 't' },
1896 { "class", required_argument, NULL, 'c' },
1897 { "legend", required_argument, NULL, ARG_LEGEND },
1898 { "interface", required_argument, NULL, 'i' },
1899 { "protocol", required_argument, NULL, 'p' },
1900 { "cname", required_argument, NULL, ARG_CNAME },
1901 { "service", no_argument, NULL, ARG_SERVICE },
1902 { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS },
1903 { "service-txt", required_argument, NULL, ARG_SERVICE_TXT },
1904 { "openpgp", no_argument, NULL, ARG_OPENPGP },
1905 { "tlsa", optional_argument, NULL, ARG_TLSA },
1906 { "raw", optional_argument, NULL, ARG_RAW },
1907 { "search", required_argument, NULL, ARG_SEARCH },
1908 { "statistics", no_argument, NULL, ARG_STATISTICS, },
1909 { "reset-statistics", no_argument, NULL, ARG_RESET_STATISTICS },
1910 { "status", no_argument, NULL, ARG_STATUS },
1911 { "flush-caches", no_argument, NULL, ARG_FLUSH_CACHES },
1912 { "reset-server-features", no_argument, NULL, ARG_RESET_SERVER_FEATURES },
1913 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1914 { "set-dns", required_argument, NULL, ARG_SET_DNS },
1915 { "set-domain", required_argument, NULL, ARG_SET_DOMAIN },
1916 { "set-llmnr", required_argument, NULL, ARG_SET_LLMNR },
1917 { "set-mdns", required_argument, NULL, ARG_SET_MDNS },
1918 { "set-dnssec", required_argument, NULL, ARG_SET_DNSSEC },
1919 { "set-nta", required_argument, NULL, ARG_SET_NTA },
1920 { "revert", no_argument, NULL, ARG_REVERT_LINK },
1921 {}
1922 };
1923
1924 int c, r;
1925
1926 assert(argc >= 0);
1927 assert(argv);
1928
1929 while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0)
1930 switch(c) {
1931
1932 case 'h':
1933 native_help();
1934 return 0; /* done */;
1935
1936 case ARG_VERSION:
1937 return version();
1938
1939 case '4':
1940 arg_family = AF_INET;
1941 break;
1942
1943 case '6':
1944 arg_family = AF_INET6;
1945 break;
1946
1947 case 'i':
1948 if (parse_ifindex(optarg, &arg_ifindex) < 0) {
1949 int ifi;
1950
1951 ifi = if_nametoindex(optarg);
1952 if (ifi <= 0)
1953 return log_error_errno(errno, "Unknown interface %s: %m", optarg);
1954
1955 arg_ifindex = ifi;
1956 }
1957
1958 break;
1959
1960 case 't':
1961 if (streq(optarg, "help")) {
1962 help_dns_types();
1963 return 0;
1964 }
1965
1966 r = dns_type_from_string(optarg);
1967 if (r < 0) {
1968 log_error("Failed to parse RR record type %s", optarg);
1969 return r;
1970 }
1971 arg_type = (uint16_t) r;
1972 assert((int) arg_type == r);
1973
1974 arg_mode = MODE_RESOLVE_RECORD;
1975 break;
1976
1977 case 'c':
1978 if (streq(optarg, "help")) {
1979 help_dns_classes();
1980 return 0;
1981 }
1982
1983 r = dns_class_from_string(optarg);
1984 if (r < 0) {
1985 log_error("Failed to parse RR record class %s", optarg);
1986 return r;
1987 }
1988 arg_class = (uint16_t) r;
1989 assert((int) arg_class == r);
1990
1991 break;
1992
1993 case ARG_LEGEND:
1994 r = parse_boolean(optarg);
1995 if (r < 0)
1996 return log_error_errno(r, "Failed to parse --legend= argument");
1997
1998 arg_legend = r;
1999 break;
2000
2001 case 'p':
2002 if (streq(optarg, "help")) {
2003 help_protocol_types();
2004 return 0;
2005 } else if (streq(optarg, "dns"))
2006 arg_flags |= SD_RESOLVED_DNS;
2007 else if (streq(optarg, "llmnr"))
2008 arg_flags |= SD_RESOLVED_LLMNR;
2009 else if (streq(optarg, "llmnr-ipv4"))
2010 arg_flags |= SD_RESOLVED_LLMNR_IPV4;
2011 else if (streq(optarg, "llmnr-ipv6"))
2012 arg_flags |= SD_RESOLVED_LLMNR_IPV6;
2013 else if (streq(optarg, "mdns"))
2014 arg_flags |= SD_RESOLVED_MDNS;
2015 else if (streq(optarg, "mdns-ipv4"))
2016 arg_flags |= SD_RESOLVED_MDNS_IPV4;
2017 else if (streq(optarg, "mdns-ipv6"))
2018 arg_flags |= SD_RESOLVED_MDNS_IPV6;
2019 else {
2020 log_error("Unknown protocol specifier: %s", optarg);
2021 return -EINVAL;
2022 }
2023
2024 break;
2025
2026 case ARG_SERVICE:
2027 arg_mode = MODE_RESOLVE_SERVICE;
2028 break;
2029
2030 case ARG_OPENPGP:
2031 arg_mode = MODE_RESOLVE_OPENPGP;
2032 break;
2033
2034 case ARG_TLSA:
2035 arg_mode = MODE_RESOLVE_TLSA;
2036 arg_service_family = service_family_from_string(optarg);
2037 if (arg_service_family < 0) {
2038 log_error("Unknown service family \"%s\".", optarg);
2039 return -EINVAL;
2040 }
2041 break;
2042
2043 case ARG_RAW:
2044 if (on_tty()) {
2045 log_error("Refusing to write binary data to tty.");
2046 return -ENOTTY;
2047 }
2048
2049 if (optarg == NULL || streq(optarg, "payload"))
2050 arg_raw = RAW_PAYLOAD;
2051 else if (streq(optarg, "packet"))
2052 arg_raw = RAW_PACKET;
2053 else {
2054 log_error("Unknown --raw specifier \"%s\".", optarg);
2055 return -EINVAL;
2056 }
2057
2058 arg_legend = false;
2059 break;
2060
2061 case ARG_CNAME:
2062 r = parse_boolean(optarg);
2063 if (r < 0)
2064 return log_error_errno(r, "Failed to parse --cname= argument.");
2065 SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0);
2066 break;
2067
2068 case ARG_SERVICE_ADDRESS:
2069 r = parse_boolean(optarg);
2070 if (r < 0)
2071 return log_error_errno(r, "Failed to parse --service-address= argument.");
2072 SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0);
2073 break;
2074
2075 case ARG_SERVICE_TXT:
2076 r = parse_boolean(optarg);
2077 if (r < 0)
2078 return log_error_errno(r, "Failed to parse --service-txt= argument.");
2079 SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0);
2080 break;
2081
2082 case ARG_SEARCH:
2083 r = parse_boolean(optarg);
2084 if (r < 0)
2085 return log_error_errno(r, "Failed to parse --search argument.");
2086 SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0);
2087 break;
2088
2089 case ARG_STATISTICS:
2090 arg_mode = MODE_STATISTICS;
2091 break;
2092
2093 case ARG_RESET_STATISTICS:
2094 arg_mode = MODE_RESET_STATISTICS;
2095 break;
2096
2097 case ARG_FLUSH_CACHES:
2098 arg_mode = MODE_FLUSH_CACHES;
2099 break;
2100
2101 case ARG_RESET_SERVER_FEATURES:
2102 arg_mode = MODE_RESET_SERVER_FEATURES;
2103 break;
2104
2105 case ARG_STATUS:
2106 arg_mode = MODE_STATUS;
2107 break;
2108
2109 case ARG_NO_PAGER:
2110 arg_no_pager = true;
2111 break;
2112
2113 case ARG_SET_DNS: {
2114 struct in_addr_data data, *n;
2115
2116 r = in_addr_from_string_auto(optarg, &data.family, &data.address);
2117 if (r < 0)
2118 return log_error_errno(r, "Failed to parse DNS server address: %s", optarg);
2119
2120 n = reallocarray(arg_set_dns, arg_n_set_dns + 1, sizeof(struct in_addr_data));
2121 if (!n)
2122 return log_oom();
2123 arg_set_dns = n;
2124
2125 arg_set_dns[arg_n_set_dns++] = data;
2126 arg_mode = MODE_SET_LINK;
2127 break;
2128 }
2129
2130 case ARG_SET_DOMAIN: {
2131 const char *p;
2132
2133 p = optarg[0] == '~' ? optarg + 1 : optarg;
2134
2135 r = dns_name_is_valid(p);
2136 if (r < 0)
2137 return log_error_errno(r, "Failed to validate specified domain %s: %m", p);
2138 if (r == 0) {
2139 log_error("Domain not valid: %s", p);
2140 return -EINVAL;
2141 }
2142
2143 r = strv_extend(&arg_set_domain, optarg);
2144 if (r < 0)
2145 return log_oom();
2146
2147 arg_mode = MODE_SET_LINK;
2148 break;
2149 }
2150
2151 case ARG_SET_LLMNR:
2152 r = free_and_strdup(&arg_set_llmnr, optarg);
2153 if (r < 0)
2154 return log_oom();
2155
2156 arg_mode = MODE_SET_LINK;
2157 break;
2158
2159 case ARG_SET_MDNS:
2160 r = free_and_strdup(&arg_set_mdns, optarg);
2161 if (r < 0)
2162 return log_oom();
2163
2164 arg_mode = MODE_SET_LINK;
2165 break;
2166
2167 case ARG_SET_DNSSEC:
2168 r = free_and_strdup(&arg_set_dnssec, optarg);
2169 if (r < 0)
2170 return log_oom();
2171
2172 arg_mode = MODE_SET_LINK;
2173 break;
2174
2175 case ARG_SET_NTA:
2176 r = dns_name_is_valid(optarg);
2177 if (r < 0)
2178 return log_error_errno(r, "Failed to validate specified domain %s: %m", optarg);
2179 if (r == 0) {
2180 log_error("Domain not valid: %s", optarg);
2181 return -EINVAL;
2182 }
2183
2184 r = strv_extend(&arg_set_nta, optarg);
2185 if (r < 0)
2186 return log_oom();
2187
2188 arg_mode = MODE_SET_LINK;
2189 break;
2190
2191 case ARG_REVERT_LINK:
2192 arg_mode = MODE_REVERT_LINK;
2193 break;
2194
2195 case '?':
2196 return -EINVAL;
2197
2198 default:
2199 assert_not_reached("Unhandled option");
2200 }
2201
2202 if (arg_type == 0 && arg_class != 0) {
2203 log_error("--class= may only be used in conjunction with --type=.");
2204 return -EINVAL;
2205 }
2206
2207 if (arg_type != 0 && arg_mode == MODE_RESOLVE_SERVICE) {
2208 log_error("--service and --type= may not be combined.");
2209 return -EINVAL;
2210 }
2211
2212 if (arg_type != 0 && arg_class == 0)
2213 arg_class = DNS_CLASS_IN;
2214
2215 if (arg_class != 0 && arg_type == 0)
2216 arg_type = DNS_TYPE_A;
2217
2218 if (IN_SET(arg_mode, MODE_SET_LINK, MODE_REVERT_LINK)) {
2219
2220 if (arg_ifindex <= 0) {
2221 log_error("--set-dns=, --set-domain=, --set-llmnr=, --set-mdns=, --set-dnssec=, --set-nta= and --revert require --interface=.");
2222 return -EINVAL;
2223 }
2224
2225 if (arg_ifindex == LOOPBACK_IFINDEX) {
2226 log_error("Interface can't be the loopback interface (lo). Sorry.");
2227 return -EINVAL;
2228 }
2229 }
2230
2231 return 1 /* work to do */;
2232 }
2233
2234 int main(int argc, char **argv) {
2235 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2236 int r;
2237
2238 log_parse_environment();
2239 log_open();
2240
2241 if (streq(program_invocation_short_name, "resolvconf"))
2242 r = resolvconf_parse_argv(argc, argv);
2243 else
2244 r = native_parse_argv(argc, argv);
2245 if (r <= 0)
2246 goto finish;
2247
2248 r = sd_bus_open_system(&bus);
2249 if (r < 0) {
2250 log_error_errno(r, "sd_bus_open_system: %m");
2251 goto finish;
2252 }
2253
2254 switch (arg_mode) {
2255
2256 case MODE_RESOLVE_HOST:
2257 if (optind >= argc) {
2258 log_error("No arguments passed.");
2259 r = -EINVAL;
2260 goto finish;
2261 }
2262
2263 while (argv[optind]) {
2264 int family, ifindex, k;
2265 union in_addr_union a;
2266
2267 if (startswith(argv[optind], "dns:"))
2268 k = resolve_rfc4501(bus, argv[optind]);
2269 else {
2270 k = in_addr_ifindex_from_string_auto(argv[optind], &family, &a, &ifindex);
2271 if (k >= 0)
2272 k = resolve_address(bus, family, &a, ifindex);
2273 else
2274 k = resolve_host(bus, argv[optind]);
2275 }
2276
2277 if (r == 0)
2278 r = k;
2279
2280 optind++;
2281 }
2282 break;
2283
2284 case MODE_RESOLVE_RECORD:
2285 if (optind >= argc) {
2286 log_error("No arguments passed.");
2287 r = -EINVAL;
2288 goto finish;
2289 }
2290
2291 while (argv[optind]) {
2292 int k;
2293
2294 k = resolve_record(bus, argv[optind], arg_class, arg_type, true);
2295 if (r == 0)
2296 r = k;
2297
2298 optind++;
2299 }
2300 break;
2301
2302 case MODE_RESOLVE_SERVICE:
2303 if (argc < optind + 1) {
2304 log_error("Domain specification required.");
2305 r = -EINVAL;
2306 goto finish;
2307
2308 } else if (argc == optind + 1)
2309 r = resolve_service(bus, NULL, NULL, argv[optind]);
2310 else if (argc == optind + 2)
2311 r = resolve_service(bus, NULL, argv[optind], argv[optind+1]);
2312 else if (argc == optind + 3)
2313 r = resolve_service(bus, argv[optind], argv[optind+1], argv[optind+2]);
2314 else {
2315 log_error("Too many arguments.");
2316 r = -EINVAL;
2317 goto finish;
2318 }
2319
2320 break;
2321
2322 case MODE_RESOLVE_OPENPGP:
2323 if (argc < optind + 1) {
2324 log_error("E-mail address required.");
2325 r = -EINVAL;
2326 goto finish;
2327
2328 }
2329
2330 r = 0;
2331 while (optind < argc) {
2332 int k;
2333
2334 k = resolve_openpgp(bus, argv[optind++]);
2335 if (k < 0)
2336 r = k;
2337 }
2338 break;
2339
2340 case MODE_RESOLVE_TLSA:
2341 if (argc < optind + 1) {
2342 log_error("Domain name required.");
2343 r = -EINVAL;
2344 goto finish;
2345
2346 }
2347
2348 r = 0;
2349 while (optind < argc) {
2350 int k;
2351
2352 k = resolve_tlsa(bus, argv[optind++]);
2353 if (k < 0)
2354 r = k;
2355 }
2356 break;
2357
2358 case MODE_STATISTICS:
2359 if (argc > optind) {
2360 log_error("Too many arguments.");
2361 r = -EINVAL;
2362 goto finish;
2363 }
2364
2365 r = show_statistics(bus);
2366 break;
2367
2368 case MODE_RESET_STATISTICS:
2369 if (argc > optind) {
2370 log_error("Too many arguments.");
2371 r = -EINVAL;
2372 goto finish;
2373 }
2374
2375 r = reset_statistics(bus);
2376 break;
2377
2378 case MODE_FLUSH_CACHES:
2379 if (argc > optind) {
2380 log_error("Too many arguments.");
2381 r = -EINVAL;
2382 goto finish;
2383 }
2384
2385 r = flush_caches(bus);
2386 break;
2387
2388 case MODE_RESET_SERVER_FEATURES:
2389 if (argc > optind) {
2390 log_error("Too many arguments.");
2391 r = -EINVAL;
2392 goto finish;
2393 }
2394
2395 r = reset_server_features(bus);
2396 break;
2397
2398 case MODE_STATUS:
2399
2400 if (argc > optind) {
2401 char **ifname;
2402 bool empty_line = false;
2403
2404 r = 0;
2405 STRV_FOREACH(ifname, argv + optind) {
2406 int ifindex, q;
2407
2408 q = parse_ifindex(*ifname, &ifindex);
2409 if (q < 0) {
2410 ifindex = if_nametoindex(*ifname);
2411 if (ifindex <= 0) {
2412 log_error_errno(errno, "Failed to resolve interface name '%s': %m", *ifname);
2413 continue;
2414 }
2415 }
2416
2417 q = status_ifindex(bus, ifindex, NULL, &empty_line);
2418 if (q < 0 && r >= 0)
2419 r = q;
2420 }
2421 } else
2422 r = status_all(bus);
2423
2424 break;
2425
2426 case MODE_SET_LINK:
2427 if (argc > optind) {
2428 log_error("Too many arguments.");
2429 r = -EINVAL;
2430 goto finish;
2431 }
2432
2433 r = set_link(bus);
2434 break;
2435
2436 case MODE_REVERT_LINK:
2437 if (argc > optind) {
2438 log_error("Too many arguments.");
2439 r = -EINVAL;
2440 goto finish;
2441 }
2442
2443 r = revert_link(bus);
2444 break;
2445
2446 case _MODE_INVALID:
2447 assert_not_reached("invalid mode");
2448 }
2449
2450 finish:
2451 pager_close();
2452
2453 free(arg_set_dns);
2454 strv_free(arg_set_domain);
2455 free(arg_set_llmnr);
2456 free(arg_set_mdns);
2457 free(arg_set_dnssec);
2458 strv_free(arg_set_nta);
2459
2460 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2461 }