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