]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolve-tool.c
resolved: also add a way to flush all caches via the bus
[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
25 #include "af-list.h"
26 #include "alloc-util.h"
27 #include "bus-error.h"
28 #include "bus-util.h"
29 #include "escape.h"
30 #include "in-addr-util.h"
31 #include "gcrypt-util.h"
32 #include "parse-util.h"
33 #include "resolved-def.h"
34 #include "resolved-dns-packet.h"
35 #include "terminal-util.h"
36
37 #define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC)
38
39 static int arg_family = AF_UNSPEC;
40 static int arg_ifindex = 0;
41 static uint16_t arg_type = 0;
42 static uint16_t arg_class = 0;
43 static bool arg_legend = true;
44 static uint64_t arg_flags = 0;
45
46 typedef enum ServiceFamily {
47 SERVICE_FAMILY_TCP,
48 SERVICE_FAMILY_UDP,
49 SERVICE_FAMILY_SCTP,
50 _SERVICE_FAMILY_INVALID = -1,
51 } ServiceFamily;
52 static ServiceFamily arg_service_family = SERVICE_FAMILY_TCP;
53
54 typedef enum RawType {
55 RAW_NONE,
56 RAW_PAYLOAD,
57 RAW_PACKET,
58 } RawType;
59 static RawType arg_raw = RAW_NONE;
60
61 static enum {
62 MODE_RESOLVE_HOST,
63 MODE_RESOLVE_RECORD,
64 MODE_RESOLVE_SERVICE,
65 MODE_RESOLVE_OPENPGP,
66 MODE_RESOLVE_TLSA,
67 MODE_STATISTICS,
68 MODE_RESET_STATISTICS,
69 MODE_FLUSH_CACHES,
70 } arg_mode = MODE_RESOLVE_HOST;
71
72 static ServiceFamily service_family_from_string(const char *s) {
73 if (s == NULL || streq(s, "tcp"))
74 return SERVICE_FAMILY_TCP;
75 if (streq(s, "udp"))
76 return SERVICE_FAMILY_UDP;
77 if (streq(s, "sctp"))
78 return SERVICE_FAMILY_SCTP;
79 return _SERVICE_FAMILY_INVALID;
80 }
81
82 static const char* service_family_to_string(ServiceFamily service) {
83 switch(service) {
84 case SERVICE_FAMILY_TCP:
85 return "_tcp";
86 case SERVICE_FAMILY_UDP:
87 return "_udp";
88 case SERVICE_FAMILY_SCTP:
89 return "_sctp";
90 default:
91 assert_not_reached("invalid service");
92 }
93 }
94
95 static void print_source(uint64_t flags, usec_t rtt) {
96 char rtt_str[FORMAT_TIMESTAMP_MAX];
97
98 if (!arg_legend)
99 return;
100
101 if (flags == 0)
102 return;
103
104 fputs("\n-- Information acquired via", stdout);
105
106 if (flags != 0)
107 printf(" protocol%s%s%s%s%s",
108 flags & SD_RESOLVED_DNS ? " DNS" :"",
109 flags & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "",
110 flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "",
111 flags & SD_RESOLVED_MDNS_IPV4 ? "mDNS/IPv4" : "",
112 flags & SD_RESOLVED_MDNS_IPV6 ? "mDNS/IPv6" : "");
113
114 assert_se(format_timespan(rtt_str, sizeof(rtt_str), rtt, 100));
115
116 printf(" in %s", rtt_str);
117
118 fputc('.', stdout);
119 fputc('\n', stdout);
120
121 printf("-- Data is authenticated: %s\n", yes_no(flags & SD_RESOLVED_AUTHENTICATED));
122 }
123
124 static int resolve_host(sd_bus *bus, const char *name) {
125
126 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
127 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
128 const char *canonical = NULL;
129 char ifname[IF_NAMESIZE] = "";
130 unsigned c = 0;
131 int r;
132 uint64_t flags;
133 usec_t ts;
134
135 assert(name);
136
137 if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname))
138 return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex);
139
140 log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
141
142 r = sd_bus_message_new_method_call(
143 bus,
144 &req,
145 "org.freedesktop.resolve1",
146 "/org/freedesktop/resolve1",
147 "org.freedesktop.resolve1.Manager",
148 "ResolveHostname");
149 if (r < 0)
150 return bus_log_create_error(r);
151
152 r = sd_bus_message_append(req, "isit", arg_ifindex, name, arg_family, arg_flags);
153 if (r < 0)
154 return bus_log_create_error(r);
155
156 ts = now(CLOCK_MONOTONIC);
157
158 r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply);
159 if (r < 0)
160 return log_error_errno(r, "%s: resolve call failed: %s", name, bus_error_message(&error, r));
161
162 ts = now(CLOCK_MONOTONIC) - ts;
163
164 r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
165 if (r < 0)
166 return bus_log_parse_error(r);
167
168 while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
169 _cleanup_free_ char *pretty = NULL;
170 int ifindex, family;
171 const void *a;
172 size_t sz;
173
174 assert_cc(sizeof(int) == sizeof(int32_t));
175
176 r = sd_bus_message_read(reply, "ii", &ifindex, &family);
177 if (r < 0)
178 return bus_log_parse_error(r);
179
180 r = sd_bus_message_read_array(reply, 'y', &a, &sz);
181 if (r < 0)
182 return bus_log_parse_error(r);
183
184 r = sd_bus_message_exit_container(reply);
185 if (r < 0)
186 return bus_log_parse_error(r);
187
188 if (!IN_SET(family, AF_INET, AF_INET6)) {
189 log_debug("%s: skipping entry with family %d (%s)", name, family, af_to_name(family) ?: "unknown");
190 continue;
191 }
192
193 if (sz != FAMILY_ADDRESS_SIZE(family)) {
194 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown");
195 return -EINVAL;
196 }
197
198 ifname[0] = 0;
199 if (ifindex > 0 && !if_indextoname(ifindex, ifname))
200 log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex);
201
202 r = in_addr_to_string(family, a, &pretty);
203 if (r < 0)
204 return log_error_errno(r, "Failed to print address for %s: %m", name);
205
206 printf("%*s%s %s%s%s\n",
207 (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
208 pretty,
209 isempty(ifname) ? "" : "%", ifname);
210
211 c++;
212 }
213 if (r < 0)
214 return bus_log_parse_error(r);
215
216 r = sd_bus_message_exit_container(reply);
217 if (r < 0)
218 return bus_log_parse_error(r);
219
220 r = sd_bus_message_read(reply, "st", &canonical, &flags);
221 if (r < 0)
222 return bus_log_parse_error(r);
223
224 if (!streq(name, canonical))
225 printf("%*s%s (%s)\n",
226 (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
227 canonical);
228
229 if (c == 0) {
230 log_error("%s: no addresses found", name);
231 return -ESRCH;
232 }
233
234 print_source(flags, ts);
235
236 return 0;
237 }
238
239 static int resolve_address(sd_bus *bus, int family, const union in_addr_union *address, int ifindex) {
240 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
241 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
242 _cleanup_free_ char *pretty = NULL;
243 char ifname[IF_NAMESIZE] = "";
244 uint64_t flags;
245 unsigned c = 0;
246 usec_t ts;
247 int r;
248
249 assert(bus);
250 assert(IN_SET(family, AF_INET, AF_INET6));
251 assert(address);
252
253 if (ifindex <= 0)
254 ifindex = arg_ifindex;
255
256 r = in_addr_to_string(family, address, &pretty);
257 if (r < 0)
258 return log_oom();
259
260 if (ifindex > 0 && !if_indextoname(ifindex, ifname))
261 return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex);
262
263 log_debug("Resolving %s%s%s.", pretty, isempty(ifname) ? "" : "%", ifname);
264
265 r = sd_bus_message_new_method_call(
266 bus,
267 &req,
268 "org.freedesktop.resolve1",
269 "/org/freedesktop/resolve1",
270 "org.freedesktop.resolve1.Manager",
271 "ResolveAddress");
272 if (r < 0)
273 return bus_log_create_error(r);
274
275 r = sd_bus_message_append(req, "ii", ifindex, family);
276 if (r < 0)
277 return bus_log_create_error(r);
278
279 r = sd_bus_message_append_array(req, 'y', address, FAMILY_ADDRESS_SIZE(family));
280 if (r < 0)
281 return bus_log_create_error(r);
282
283 r = sd_bus_message_append(req, "t", arg_flags);
284 if (r < 0)
285 return bus_log_create_error(r);
286
287 ts = now(CLOCK_MONOTONIC);
288
289 r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply);
290 if (r < 0) {
291 log_error("%s: resolve call failed: %s", pretty, bus_error_message(&error, r));
292 return r;
293 }
294
295 ts = now(CLOCK_MONOTONIC) - ts;
296
297 r = sd_bus_message_enter_container(reply, 'a', "(is)");
298 if (r < 0)
299 return bus_log_create_error(r);
300
301 while ((r = sd_bus_message_enter_container(reply, 'r', "is")) > 0) {
302 const char *n;
303
304 assert_cc(sizeof(int) == sizeof(int32_t));
305
306 r = sd_bus_message_read(reply, "is", &ifindex, &n);
307 if (r < 0)
308 return r;
309
310 r = sd_bus_message_exit_container(reply);
311 if (r < 0)
312 return r;
313
314 ifname[0] = 0;
315 if (ifindex > 0 && !if_indextoname(ifindex, ifname))
316 log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex);
317
318 printf("%*s%*s%*s%s %s\n",
319 (int) strlen(pretty), c == 0 ? pretty : "",
320 isempty(ifname) ? 0 : 1, c > 0 || isempty(ifname) ? "" : "%",
321 (int) strlen(ifname), c == 0 ? ifname : "",
322 c == 0 ? ":" : " ",
323 n);
324
325 c++;
326 }
327 if (r < 0)
328 return bus_log_parse_error(r);
329
330 r = sd_bus_message_exit_container(reply);
331 if (r < 0)
332 return bus_log_parse_error(r);
333
334 r = sd_bus_message_read(reply, "t", &flags);
335 if (r < 0)
336 return bus_log_parse_error(r);
337
338 if (c == 0) {
339 log_error("%s: no names found", pretty);
340 return -ESRCH;
341 }
342
343 print_source(flags, ts);
344
345 return 0;
346 }
347
348 static int parse_address(const char *s, int *family, union in_addr_union *address, int *ifindex) {
349 const char *percent, *a;
350 int ifi = 0;
351 int r;
352
353 percent = strchr(s, '%');
354 if (percent) {
355 if (parse_ifindex(percent+1, &ifi) < 0) {
356 ifi = if_nametoindex(percent+1);
357 if (ifi <= 0)
358 return -EINVAL;
359 }
360
361 a = strndupa(s, percent - s);
362 } else
363 a = s;
364
365 r = in_addr_from_string_auto(a, family, address);
366 if (r < 0)
367 return r;
368
369 *ifindex = ifi;
370 return 0;
371 }
372
373 static int output_rr_packet(const void *d, size_t l, int ifindex) {
374 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
375 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
376 int r;
377 char ifname[IF_NAMESIZE] = "";
378
379 r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0);
380 if (r < 0)
381 return log_oom();
382
383 p->refuse_compression = true;
384
385 r = dns_packet_append_blob(p, d, l, NULL);
386 if (r < 0)
387 return log_oom();
388
389 r = dns_packet_read_rr(p, &rr, NULL, NULL);
390 if (r < 0)
391 return log_error_errno(r, "Failed to parse RR: %m");
392
393 if (arg_raw == RAW_PAYLOAD) {
394 void *data;
395 ssize_t k;
396
397 k = dns_resource_record_payload(rr, &data);
398 if (k < 0)
399 return log_error_errno(k, "Cannot dump RR: %m");
400 fwrite(data, 1, k, stdout);
401 } else {
402 const char *s;
403
404 s = dns_resource_record_to_string(rr);
405 if (!s)
406 return log_oom();
407
408 if (ifindex > 0 && !if_indextoname(ifindex, ifname))
409 log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex);
410
411 printf("%s%s%s\n", s, isempty(ifname) ? "" : " # interface ", ifname);
412 }
413
414 return 0;
415 }
416
417 static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type) {
418 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
419 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
420 char ifname[IF_NAMESIZE] = "";
421 unsigned n = 0;
422 uint64_t flags;
423 int r;
424 usec_t ts;
425 bool needs_authentication = false;
426
427 assert(name);
428
429 if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname))
430 return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex);
431
432 log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(class), dns_type_to_string(type), isempty(ifname) ? "*" : ifname);
433
434 r = sd_bus_message_new_method_call(
435 bus,
436 &req,
437 "org.freedesktop.resolve1",
438 "/org/freedesktop/resolve1",
439 "org.freedesktop.resolve1.Manager",
440 "ResolveRecord");
441 if (r < 0)
442 return bus_log_create_error(r);
443
444 r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, class, type, arg_flags);
445 if (r < 0)
446 return bus_log_create_error(r);
447
448 ts = now(CLOCK_MONOTONIC);
449
450 r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply);
451 if (r < 0) {
452 log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r));
453 return r;
454 }
455
456 ts = now(CLOCK_MONOTONIC) - ts;
457
458 r = sd_bus_message_enter_container(reply, 'a', "(iqqay)");
459 if (r < 0)
460 return bus_log_parse_error(r);
461
462 while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) {
463 uint16_t c, t;
464 int ifindex;
465 const void *d;
466 size_t l;
467
468 assert_cc(sizeof(int) == sizeof(int32_t));
469
470 r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t);
471 if (r < 0)
472 return bus_log_parse_error(r);
473
474 r = sd_bus_message_read_array(reply, 'y', &d, &l);
475 if (r < 0)
476 return bus_log_parse_error(r);
477
478 r = sd_bus_message_exit_container(reply);
479 if (r < 0)
480 return bus_log_parse_error(r);
481
482 if (arg_raw == RAW_PACKET) {
483 uint64_t u64 = htole64(l);
484
485 fwrite(&u64, sizeof(u64), 1, stdout);
486 fwrite(d, 1, l, stdout);
487 } else {
488 r = output_rr_packet(d, l, ifindex);
489 if (r < 0)
490 return r;
491 }
492
493 if (dns_type_needs_authentication(t))
494 needs_authentication = true;
495
496 n++;
497 }
498 if (r < 0)
499 return bus_log_parse_error(r);
500
501 r = sd_bus_message_exit_container(reply);
502 if (r < 0)
503 return bus_log_parse_error(r);
504
505 r = sd_bus_message_read(reply, "t", &flags);
506 if (r < 0)
507 return bus_log_parse_error(r);
508
509 if (n == 0) {
510 log_error("%s: no records found", name);
511 return -ESRCH;
512 }
513
514 print_source(flags, ts);
515
516 if ((flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) {
517 fflush(stdout);
518
519 fprintf(stderr, "\n%s"
520 "WARNING: The resources shown contain cryptographic key data which could not be\n"
521 " authenticated. It is not suitable to authenticate any communication.\n"
522 " This is usually indication that DNSSEC authentication was not enabled\n"
523 " or is not available for the selected protocol or DNS servers.%s\n",
524 ansi_highlight_red(),
525 ansi_normal());
526 }
527
528 return 0;
529 }
530
531 static int resolve_rfc4501(sd_bus *bus, const char *name) {
532 uint16_t type = 0, class = 0;
533 const char *p, *q, *n;
534 int r;
535
536 assert(bus);
537 assert(name);
538 assert(startswith(name, "dns:"));
539
540 /* Parse RFC 4501 dns: URIs */
541
542 p = name + 4;
543
544 if (p[0] == '/') {
545 const char *e;
546
547 if (p[1] != '/')
548 goto invalid;
549
550 e = strchr(p + 2, '/');
551 if (!e)
552 goto invalid;
553
554 if (e != p + 2)
555 log_warning("DNS authority specification not supported; ignoring specified authority.");
556
557 p = e + 1;
558 }
559
560 q = strchr(p, '?');
561 if (q) {
562 n = strndupa(p, q - p);
563 q++;
564
565 for (;;) {
566 const char *f;
567
568 f = startswith_no_case(q, "class=");
569 if (f) {
570 _cleanup_free_ char *t = NULL;
571 const char *e;
572
573 if (class != 0) {
574 log_error("DNS class specified twice.");
575 return -EINVAL;
576 }
577
578 e = strchrnul(f, ';');
579 t = strndup(f, e - f);
580 if (!t)
581 return log_oom();
582
583 r = dns_class_from_string(t);
584 if (r < 0) {
585 log_error("Unknown DNS class %s.", t);
586 return -EINVAL;
587 }
588
589 class = r;
590
591 if (*e == ';') {
592 q = e + 1;
593 continue;
594 }
595
596 break;
597 }
598
599 f = startswith_no_case(q, "type=");
600 if (f) {
601 _cleanup_free_ char *t = NULL;
602 const char *e;
603
604 if (type != 0) {
605 log_error("DNS type specified twice.");
606 return -EINVAL;
607 }
608
609 e = strchrnul(f, ';');
610 t = strndup(f, e - f);
611 if (!t)
612 return log_oom();
613
614 r = dns_type_from_string(t);
615 if (r < 0) {
616 log_error("Unknown DNS type %s.", t);
617 return -EINVAL;
618 }
619
620 type = r;
621
622 if (*e == ';') {
623 q = e + 1;
624 continue;
625 }
626
627 break;
628 }
629
630 goto invalid;
631 }
632 } else
633 n = p;
634
635 if (class == 0)
636 class = arg_class ?: DNS_CLASS_IN;
637 if (type == 0)
638 type = arg_type ?: DNS_TYPE_A;
639
640 return resolve_record(bus, n, class, type);
641
642 invalid:
643 log_error("Invalid DNS URI: %s", name);
644 return -EINVAL;
645 }
646
647 static int resolve_service(sd_bus *bus, const char *name, const char *type, const char *domain) {
648 const char *canonical_name, *canonical_type, *canonical_domain;
649 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
650 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
651 char ifname[IF_NAMESIZE] = "";
652 size_t indent, sz;
653 uint64_t flags;
654 const char *p;
655 unsigned c;
656 usec_t ts;
657 int r;
658
659 assert(bus);
660 assert(domain);
661
662 name = empty_to_null(name);
663 type = empty_to_null(type);
664
665 if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname))
666 return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex);
667
668 if (name)
669 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);
670 else if (type)
671 log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
672 else
673 log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
674
675 r = sd_bus_message_new_method_call(
676 bus,
677 &req,
678 "org.freedesktop.resolve1",
679 "/org/freedesktop/resolve1",
680 "org.freedesktop.resolve1.Manager",
681 "ResolveService");
682 if (r < 0)
683 return bus_log_create_error(r);
684
685 r = sd_bus_message_append(req, "isssit", arg_ifindex, name, type, domain, arg_family, arg_flags);
686 if (r < 0)
687 return bus_log_create_error(r);
688
689 ts = now(CLOCK_MONOTONIC);
690
691 r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply);
692 if (r < 0)
693 return log_error_errno(r, "Resolve call failed: %s", bus_error_message(&error, r));
694
695 ts = now(CLOCK_MONOTONIC) - ts;
696
697 r = sd_bus_message_enter_container(reply, 'a', "(qqqsa(iiay)s)");
698 if (r < 0)
699 return bus_log_parse_error(r);
700
701 indent =
702 (name ? strlen(name) + 1 : 0) +
703 (type ? strlen(type) + 1 : 0) +
704 strlen(domain) + 2;
705
706 c = 0;
707 while ((r = sd_bus_message_enter_container(reply, 'r', "qqqsa(iiay)s")) > 0) {
708 uint16_t priority, weight, port;
709 const char *hostname, *canonical;
710
711 r = sd_bus_message_read(reply, "qqqs", &priority, &weight, &port, &hostname);
712 if (r < 0)
713 return bus_log_parse_error(r);
714
715 if (name)
716 printf("%*s%s", (int) strlen(name), c == 0 ? name : "", c == 0 ? "/" : " ");
717 if (type)
718 printf("%*s%s", (int) strlen(type), c == 0 ? type : "", c == 0 ? "/" : " ");
719
720 printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
721 (int) strlen(domain), c == 0 ? domain : "",
722 c == 0 ? ":" : " ",
723 hostname, port,
724 priority, weight);
725
726 r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
727 if (r < 0)
728 return bus_log_parse_error(r);
729
730 while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
731 _cleanup_free_ char *pretty = NULL;
732 int ifindex, family;
733 const void *a;
734
735 assert_cc(sizeof(int) == sizeof(int32_t));
736
737 r = sd_bus_message_read(reply, "ii", &ifindex, &family);
738 if (r < 0)
739 return bus_log_parse_error(r);
740
741 r = sd_bus_message_read_array(reply, 'y', &a, &sz);
742 if (r < 0)
743 return bus_log_parse_error(r);
744
745 r = sd_bus_message_exit_container(reply);
746 if (r < 0)
747 return bus_log_parse_error(r);
748
749 if (!IN_SET(family, AF_INET, AF_INET6)) {
750 log_debug("%s: skipping entry with family %d (%s)", name, family, af_to_name(family) ?: "unknown");
751 continue;
752 }
753
754 if (sz != FAMILY_ADDRESS_SIZE(family)) {
755 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown");
756 return -EINVAL;
757 }
758
759 ifname[0] = 0;
760 if (ifindex > 0 && !if_indextoname(ifindex, ifname))
761 log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex);
762
763 r = in_addr_to_string(family, a, &pretty);
764 if (r < 0)
765 return log_error_errno(r, "Failed to print address for %s: %m", name);
766
767 printf("%*s%s%s%s\n", (int) indent, "", pretty, isempty(ifname) ? "" : "%s", ifname);
768 }
769 if (r < 0)
770 return bus_log_parse_error(r);
771
772 r = sd_bus_message_exit_container(reply);
773 if (r < 0)
774 return bus_log_parse_error(r);
775
776 r = sd_bus_message_read(reply, "s", &canonical);
777 if (r < 0)
778 return bus_log_parse_error(r);
779
780 if (!streq(hostname, canonical))
781 printf("%*s(%s)\n", (int) indent, "", canonical);
782
783 r = sd_bus_message_exit_container(reply);
784 if (r < 0)
785 return bus_log_parse_error(r);
786
787 c++;
788 }
789 if (r < 0)
790 return bus_log_parse_error(r);
791
792 r = sd_bus_message_exit_container(reply);
793 if (r < 0)
794 return bus_log_parse_error(r);
795
796 r = sd_bus_message_enter_container(reply, 'a', "ay");
797 if (r < 0)
798 return bus_log_parse_error(r);
799
800 c = 0;
801 while ((r = sd_bus_message_read_array(reply, 'y', (const void**) &p, &sz)) > 0) {
802 _cleanup_free_ char *escaped = NULL;
803
804 escaped = cescape_length(p, sz);
805 if (!escaped)
806 return log_oom();
807
808 printf("%*s%s\n", (int) indent, "", escaped);
809 c++;
810 }
811 if (r < 0)
812 return bus_log_parse_error(r);
813
814 r = sd_bus_message_exit_container(reply);
815 if (r < 0)
816 return bus_log_parse_error(r);
817
818 r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags);
819 if (r < 0)
820 return bus_log_parse_error(r);
821
822 canonical_name = empty_to_null(canonical_name);
823 canonical_type = empty_to_null(canonical_type);
824
825 if (!streq_ptr(name, canonical_name) ||
826 !streq_ptr(type, canonical_type) ||
827 !streq_ptr(domain, canonical_domain)) {
828
829 printf("%*s(", (int) indent, "");
830
831 if (canonical_name)
832 printf("%s/", canonical_name);
833 if (canonical_type)
834 printf("%s/", canonical_type);
835
836 printf("%s)\n", canonical_domain);
837 }
838
839 print_source(flags, ts);
840
841 return 0;
842 }
843
844 static int resolve_openpgp(sd_bus *bus, const char *address) {
845 const char *domain, *full;
846 int r;
847 _cleanup_free_ char *hashed = NULL;
848
849 assert(bus);
850 assert(address);
851
852 domain = strrchr(address, '@');
853 if (!domain) {
854 log_error("Address does not contain '@': \"%s\"", address);
855 return -EINVAL;
856 } else if (domain == address || domain[1] == '\0') {
857 log_error("Address starts or ends with '@': \"%s\"", address);
858 return -EINVAL;
859 }
860 domain++;
861
862 r = string_hashsum_sha224(address, domain - 1 - address, &hashed);
863 if (r < 0)
864 return log_error_errno(r, "Hashing failed: %m");
865
866 full = strjoina(hashed, "._openpgpkey.", domain);
867 log_debug("Looking up \"%s\".", full);
868
869 return resolve_record(bus, full,
870 arg_class ?: DNS_CLASS_IN,
871 arg_type ?: DNS_TYPE_OPENPGPKEY);
872 }
873
874 static int resolve_tlsa(sd_bus *bus, const char *address) {
875 const char *port;
876 uint16_t port_num = 443;
877 _cleanup_free_ char *full = NULL;
878 int r;
879
880 assert(bus);
881 assert(address);
882
883 port = strrchr(address, ':');
884 if (port) {
885 r = safe_atou16(port + 1, &port_num);
886 if (r < 0 || port_num == 0)
887 return log_error_errno(r, "Invalid port \"%s\".", port + 1);
888
889 address = strndupa(address, port - address);
890 }
891
892 r = asprintf(&full, "_%u.%s.%s",
893 port_num,
894 service_family_to_string(arg_service_family),
895 address);
896 if (r < 0)
897 return log_oom();
898
899 log_debug("Looking up \"%s\".", full);
900
901 return resolve_record(bus, full,
902 arg_class ?: DNS_CLASS_IN,
903 arg_type ?: DNS_TYPE_TLSA);
904 }
905
906 static int show_statistics(sd_bus *bus) {
907 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
908 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
909 uint64_t n_current_transactions, n_total_transactions,
910 cache_size, n_cache_hit, n_cache_miss,
911 n_dnssec_secure, n_dnssec_insecure, n_dnssec_bogus, n_dnssec_indeterminate;
912 int r, dnssec_supported;
913
914 assert(bus);
915
916 r = sd_bus_get_property_trivial(bus,
917 "org.freedesktop.resolve1",
918 "/org/freedesktop/resolve1",
919 "org.freedesktop.resolve1.Manager",
920 "DNSSECSupported",
921 &error,
922 'b',
923 &dnssec_supported);
924 if (r < 0)
925 return log_error_errno(r, "Failed to get DNSSEC supported state: %s", bus_error_message(&error, r));
926
927 printf("DNSSEC supported by current servers: %s%s%s\n\n",
928 ansi_highlight(),
929 yes_no(dnssec_supported),
930 ansi_normal());
931
932 r = sd_bus_get_property(bus,
933 "org.freedesktop.resolve1",
934 "/org/freedesktop/resolve1",
935 "org.freedesktop.resolve1.Manager",
936 "TransactionStatistics",
937 &error,
938 &reply,
939 "(tt)");
940 if (r < 0)
941 return log_error_errno(r, "Failed to get transaction statistics: %s", bus_error_message(&error, r));
942
943 r = sd_bus_message_read(reply, "(tt)",
944 &n_current_transactions,
945 &n_total_transactions);
946 if (r < 0)
947 return bus_log_parse_error(r);
948
949 printf("%sTransactions%s\n"
950 "Current Transactions: %" PRIu64 "\n"
951 " Total Transactions: %" PRIu64 "\n",
952 ansi_highlight(),
953 ansi_normal(),
954 n_current_transactions,
955 n_total_transactions);
956
957 reply = sd_bus_message_unref(reply);
958
959 r = sd_bus_get_property(bus,
960 "org.freedesktop.resolve1",
961 "/org/freedesktop/resolve1",
962 "org.freedesktop.resolve1.Manager",
963 "CacheStatistics",
964 &error,
965 &reply,
966 "(ttt)");
967 if (r < 0)
968 return log_error_errno(r, "Failed to get cache statistics: %s", bus_error_message(&error, r));
969
970 r = sd_bus_message_read(reply, "(ttt)",
971 &cache_size,
972 &n_cache_hit,
973 &n_cache_miss);
974 if (r < 0)
975 return bus_log_parse_error(r);
976
977 printf("\n%sCache%s\n"
978 " Current Cache Size: %" PRIu64 "\n"
979 " Cache Hits: %" PRIu64 "\n"
980 " Cache Misses: %" PRIu64 "\n",
981 ansi_highlight(),
982 ansi_normal(),
983 cache_size,
984 n_cache_hit,
985 n_cache_miss);
986
987 reply = sd_bus_message_unref(reply);
988
989 r = sd_bus_get_property(bus,
990 "org.freedesktop.resolve1",
991 "/org/freedesktop/resolve1",
992 "org.freedesktop.resolve1.Manager",
993 "DNSSECStatistics",
994 &error,
995 &reply,
996 "(tttt)");
997 if (r < 0)
998 return log_error_errno(r, "Failed to get DNSSEC statistics: %s", bus_error_message(&error, r));
999
1000 r = sd_bus_message_read(reply, "(tttt)",
1001 &n_dnssec_secure,
1002 &n_dnssec_insecure,
1003 &n_dnssec_bogus,
1004 &n_dnssec_indeterminate);
1005 if (r < 0)
1006 return bus_log_parse_error(r);
1007
1008 printf("\n%sDNSSEC Verdicts%s\n"
1009 " Secure: %" PRIu64 "\n"
1010 " Insecure: %" PRIu64 "\n"
1011 " Bogus: %" PRIu64 "\n"
1012 " Indeterminate: %" PRIu64 "\n",
1013 ansi_highlight(),
1014 ansi_normal(),
1015 n_dnssec_secure,
1016 n_dnssec_insecure,
1017 n_dnssec_bogus,
1018 n_dnssec_indeterminate);
1019
1020 return 0;
1021 }
1022
1023 static int reset_statistics(sd_bus *bus) {
1024 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1025 int r;
1026
1027 r = sd_bus_call_method(bus,
1028 "org.freedesktop.resolve1",
1029 "/org/freedesktop/resolve1",
1030 "org.freedesktop.resolve1.Manager",
1031 "ResetStatistics",
1032 &error,
1033 NULL,
1034 NULL);
1035 if (r < 0)
1036 return log_error_errno(r, "Failed to reset statistics: %s", bus_error_message(&error, r));
1037
1038 return 0;
1039 }
1040
1041 static int flush_caches(sd_bus *bus) {
1042 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1043 int r;
1044
1045 r = sd_bus_call_method(bus,
1046 "org.freedesktop.resolve1",
1047 "/org/freedesktop/resolve1",
1048 "org.freedesktop.resolve1.Manager",
1049 "FlushCaches",
1050 &error,
1051 NULL,
1052 NULL);
1053 if (r < 0)
1054 return log_error_errno(r, "Failed to flush caches: %s", bus_error_message(&error, r));
1055
1056 return 0;
1057 }
1058
1059 static void help_protocol_types(void) {
1060 if (arg_legend)
1061 puts("Known protocol types:");
1062 puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6");
1063 }
1064
1065 static void help_dns_types(void) {
1066 int i;
1067 const char *t;
1068
1069 if (arg_legend)
1070 puts("Known DNS RR types:");
1071 for (i = 0; i < _DNS_TYPE_MAX; i++) {
1072 t = dns_type_to_string(i);
1073 if (t)
1074 puts(t);
1075 }
1076 }
1077
1078 static void help_dns_classes(void) {
1079 int i;
1080 const char *t;
1081
1082 if (arg_legend)
1083 puts("Known DNS RR classes:");
1084 for (i = 0; i < _DNS_CLASS_MAX; i++) {
1085 t = dns_class_to_string(i);
1086 if (t)
1087 puts(t);
1088 }
1089 }
1090
1091 static void help(void) {
1092 printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n"
1093 "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n"
1094 "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n"
1095 "%1$s [OPTIONS...] --statistics\n"
1096 "%1$s [OPTIONS...] --reset-statistics\n"
1097 "\n"
1098 "Resolve domain names, IPv4 and IPv6 addresses, DNS resource records, and services.\n\n"
1099 " -h --help Show this help\n"
1100 " --version Show package version\n"
1101 " -4 Resolve IPv4 addresses\n"
1102 " -6 Resolve IPv6 addresses\n"
1103 " -i --interface=INTERFACE Look on interface\n"
1104 " -p --protocol=PROTO|help Look via protocol\n"
1105 " -t --type=TYPE|help Query RR with DNS type\n"
1106 " -c --class=CLASS|help Query RR with DNS class\n"
1107 " --service Resolve service (SRV)\n"
1108 " --service-address=BOOL Resolve address for services (default: yes)\n"
1109 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
1110 " --openpgp Query OpenPGP public key\n"
1111 " --tlsa Query TLS public key\n"
1112 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
1113 " --search=BOOL Use search domains for single-label names\n"
1114 " (default: yes)\n"
1115 " --raw[=payload|packet] Dump the answer as binary data\n"
1116 " --legend=BOOL Print headers and additional info (default: yes)\n"
1117 " --statistics Show resolver statistics\n"
1118 " --reset-statistics Reset resolver statistics\n"
1119 " --flush-caches Flush all local DNS caches\n"
1120 , program_invocation_short_name);
1121 }
1122
1123 static int parse_argv(int argc, char *argv[]) {
1124 enum {
1125 ARG_VERSION = 0x100,
1126 ARG_LEGEND,
1127 ARG_SERVICE,
1128 ARG_CNAME,
1129 ARG_SERVICE_ADDRESS,
1130 ARG_SERVICE_TXT,
1131 ARG_OPENPGP,
1132 ARG_TLSA,
1133 ARG_RAW,
1134 ARG_SEARCH,
1135 ARG_STATISTICS,
1136 ARG_RESET_STATISTICS,
1137 ARG_FLUSH_CACHES,
1138 };
1139
1140 static const struct option options[] = {
1141 { "help", no_argument, NULL, 'h' },
1142 { "version", no_argument, NULL, ARG_VERSION },
1143 { "type", required_argument, NULL, 't' },
1144 { "class", required_argument, NULL, 'c' },
1145 { "legend", required_argument, NULL, ARG_LEGEND },
1146 { "interface", required_argument, NULL, 'i' },
1147 { "protocol", required_argument, NULL, 'p' },
1148 { "cname", required_argument, NULL, ARG_CNAME },
1149 { "service", no_argument, NULL, ARG_SERVICE },
1150 { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS },
1151 { "service-txt", required_argument, NULL, ARG_SERVICE_TXT },
1152 { "openpgp", no_argument, NULL, ARG_OPENPGP },
1153 { "tlsa", optional_argument, NULL, ARG_TLSA },
1154 { "raw", optional_argument, NULL, ARG_RAW },
1155 { "search", required_argument, NULL, ARG_SEARCH },
1156 { "statistics", no_argument, NULL, ARG_STATISTICS, },
1157 { "reset-statistics", no_argument, NULL, ARG_RESET_STATISTICS },
1158 { "flush-caches", no_argument, NULL, ARG_FLUSH_CACHES },
1159 {}
1160 };
1161
1162 int c, r;
1163
1164 assert(argc >= 0);
1165 assert(argv);
1166
1167 while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0)
1168 switch(c) {
1169
1170 case 'h':
1171 help();
1172 return 0; /* done */;
1173
1174 case ARG_VERSION:
1175 return version();
1176
1177 case '4':
1178 arg_family = AF_INET;
1179 break;
1180
1181 case '6':
1182 arg_family = AF_INET6;
1183 break;
1184
1185 case 'i': {
1186 int ifi;
1187
1188 if (parse_ifindex(optarg, &ifi) >= 0)
1189 arg_ifindex = ifi;
1190 else {
1191 ifi = if_nametoindex(optarg);
1192 if (ifi <= 0)
1193 return log_error_errno(errno, "Unknown interface %s: %m", optarg);
1194
1195 arg_ifindex = ifi;
1196 }
1197
1198 break;
1199 }
1200
1201 case 't':
1202 if (streq(optarg, "help")) {
1203 help_dns_types();
1204 return 0;
1205 }
1206
1207 r = dns_type_from_string(optarg);
1208 if (r < 0) {
1209 log_error("Failed to parse RR record type %s", optarg);
1210 return r;
1211 }
1212 arg_type = (uint16_t) r;
1213 assert((int) arg_type == r);
1214
1215 arg_mode = MODE_RESOLVE_RECORD;
1216 break;
1217
1218 case 'c':
1219 if (streq(optarg, "help")) {
1220 help_dns_classes();
1221 return 0;
1222 }
1223
1224 r = dns_class_from_string(optarg);
1225 if (r < 0) {
1226 log_error("Failed to parse RR record class %s", optarg);
1227 return r;
1228 }
1229 arg_class = (uint16_t) r;
1230 assert((int) arg_class == r);
1231
1232 break;
1233
1234 case ARG_LEGEND:
1235 r = parse_boolean(optarg);
1236 if (r < 0)
1237 return log_error_errno(r, "Failed to parse --legend= argument");
1238
1239 arg_legend = r;
1240 break;
1241
1242 case 'p':
1243 if (streq(optarg, "help")) {
1244 help_protocol_types();
1245 return 0;
1246 } else if (streq(optarg, "dns"))
1247 arg_flags |= SD_RESOLVED_DNS;
1248 else if (streq(optarg, "llmnr"))
1249 arg_flags |= SD_RESOLVED_LLMNR;
1250 else if (streq(optarg, "llmnr-ipv4"))
1251 arg_flags |= SD_RESOLVED_LLMNR_IPV4;
1252 else if (streq(optarg, "llmnr-ipv6"))
1253 arg_flags |= SD_RESOLVED_LLMNR_IPV6;
1254 else {
1255 log_error("Unknown protocol specifier: %s", optarg);
1256 return -EINVAL;
1257 }
1258
1259 break;
1260
1261 case ARG_SERVICE:
1262 arg_mode = MODE_RESOLVE_SERVICE;
1263 break;
1264
1265 case ARG_OPENPGP:
1266 arg_mode = MODE_RESOLVE_OPENPGP;
1267 break;
1268
1269 case ARG_TLSA:
1270 arg_mode = MODE_RESOLVE_TLSA;
1271 arg_service_family = service_family_from_string(optarg);
1272 if (arg_service_family < 0) {
1273 log_error("Unknown service family \"%s\".", optarg);
1274 return -EINVAL;
1275 }
1276 break;
1277
1278 case ARG_RAW:
1279 if (on_tty()) {
1280 log_error("Refusing to write binary data to tty.");
1281 return -ENOTTY;
1282 }
1283
1284 if (optarg == NULL || streq(optarg, "payload"))
1285 arg_raw = RAW_PAYLOAD;
1286 else if (streq(optarg, "packet"))
1287 arg_raw = RAW_PACKET;
1288 else {
1289 log_error("Unknown --raw specifier \"%s\".", optarg);
1290 return -EINVAL;
1291 }
1292
1293 arg_legend = false;
1294 break;
1295
1296 case ARG_CNAME:
1297 r = parse_boolean(optarg);
1298 if (r < 0)
1299 return log_error_errno(r, "Failed to parse --cname= argument.");
1300 SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0);
1301 break;
1302
1303 case ARG_SERVICE_ADDRESS:
1304 r = parse_boolean(optarg);
1305 if (r < 0)
1306 return log_error_errno(r, "Failed to parse --service-address= argument.");
1307 SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0);
1308 break;
1309
1310 case ARG_SERVICE_TXT:
1311 r = parse_boolean(optarg);
1312 if (r < 0)
1313 return log_error_errno(r, "Failed to parse --service-txt= argument.");
1314 SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0);
1315 break;
1316
1317 case ARG_SEARCH:
1318 r = parse_boolean(optarg);
1319 if (r < 0)
1320 return log_error_errno(r, "Failed to parse --search argument.");
1321 SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0);
1322 break;
1323
1324 case ARG_STATISTICS:
1325 arg_mode = MODE_STATISTICS;
1326 break;
1327
1328 case ARG_RESET_STATISTICS:
1329 arg_mode = MODE_RESET_STATISTICS;
1330 break;
1331
1332 case ARG_FLUSH_CACHES:
1333 arg_mode = MODE_FLUSH_CACHES;
1334 break;
1335
1336 case '?':
1337 return -EINVAL;
1338
1339 default:
1340 assert_not_reached("Unhandled option");
1341 }
1342
1343 if (arg_type == 0 && arg_class != 0) {
1344 log_error("--class= may only be used in conjunction with --type=.");
1345 return -EINVAL;
1346 }
1347
1348 if (arg_type != 0 && arg_mode == MODE_RESOLVE_SERVICE) {
1349 log_error("--service and --type= may not be combined.");
1350 return -EINVAL;
1351 }
1352
1353 if (arg_type != 0 && arg_class == 0)
1354 arg_class = DNS_CLASS_IN;
1355
1356 if (arg_class != 0 && arg_type == 0)
1357 arg_type = DNS_TYPE_A;
1358
1359 return 1 /* work to do */;
1360 }
1361
1362 int main(int argc, char **argv) {
1363 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1364 int r;
1365
1366 log_parse_environment();
1367 log_open();
1368
1369 r = parse_argv(argc, argv);
1370 if (r <= 0)
1371 goto finish;
1372
1373 r = sd_bus_open_system(&bus);
1374 if (r < 0) {
1375 log_error_errno(r, "sd_bus_open_system: %m");
1376 goto finish;
1377 }
1378
1379 switch (arg_mode) {
1380
1381 case MODE_RESOLVE_HOST:
1382 if (optind >= argc) {
1383 log_error("No arguments passed.");
1384 r = -EINVAL;
1385 goto finish;
1386 }
1387
1388 while (argv[optind]) {
1389 int family, ifindex, k;
1390 union in_addr_union a;
1391
1392 if (startswith(argv[optind], "dns:"))
1393 k = resolve_rfc4501(bus, argv[optind]);
1394 else {
1395 k = parse_address(argv[optind], &family, &a, &ifindex);
1396 if (k >= 0)
1397 k = resolve_address(bus, family, &a, ifindex);
1398 else
1399 k = resolve_host(bus, argv[optind]);
1400 }
1401
1402 if (r == 0)
1403 r = k;
1404
1405 optind++;
1406 }
1407 break;
1408
1409 case MODE_RESOLVE_RECORD:
1410 if (optind >= argc) {
1411 log_error("No arguments passed.");
1412 r = -EINVAL;
1413 goto finish;
1414 }
1415
1416 while (argv[optind]) {
1417 int k;
1418
1419 k = resolve_record(bus, argv[optind], arg_class, arg_type);
1420 if (r == 0)
1421 r = k;
1422
1423 optind++;
1424 }
1425 break;
1426
1427 case MODE_RESOLVE_SERVICE:
1428 if (argc < optind + 1) {
1429 log_error("Domain specification required.");
1430 r = -EINVAL;
1431 goto finish;
1432
1433 } else if (argc == optind + 1)
1434 r = resolve_service(bus, NULL, NULL, argv[optind]);
1435 else if (argc == optind + 2)
1436 r = resolve_service(bus, NULL, argv[optind], argv[optind+1]);
1437 else if (argc == optind + 3)
1438 r = resolve_service(bus, argv[optind], argv[optind+1], argv[optind+2]);
1439 else {
1440 log_error("Too many arguments.");
1441 r = -EINVAL;
1442 goto finish;
1443 }
1444
1445 break;
1446
1447 case MODE_RESOLVE_OPENPGP:
1448 if (argc < optind + 1) {
1449 log_error("E-mail address required.");
1450 r = -EINVAL;
1451 goto finish;
1452
1453 }
1454
1455 r = 0;
1456 while (optind < argc) {
1457 int k;
1458
1459 k = resolve_openpgp(bus, argv[optind++]);
1460 if (k < 0)
1461 r = k;
1462 }
1463 break;
1464
1465 case MODE_RESOLVE_TLSA:
1466 if (argc < optind + 1) {
1467 log_error("Domain name required.");
1468 r = -EINVAL;
1469 goto finish;
1470
1471 }
1472
1473 r = 0;
1474 while (optind < argc) {
1475 int k;
1476
1477 k = resolve_tlsa(bus, argv[optind++]);
1478 if (k < 0)
1479 r = k;
1480 }
1481 break;
1482
1483 case MODE_STATISTICS:
1484 if (argc > optind) {
1485 log_error("Too many arguments.");
1486 r = -EINVAL;
1487 goto finish;
1488 }
1489
1490 r = show_statistics(bus);
1491 break;
1492
1493 case MODE_RESET_STATISTICS:
1494 if (argc > optind) {
1495 log_error("Too many arguments.");
1496 r = -EINVAL;
1497 goto finish;
1498 }
1499
1500 r = reset_statistics(bus);
1501 break;
1502
1503 case MODE_FLUSH_CACHES:
1504 if (argc > optind) {
1505 log_error("Too many arguments.");
1506 r = -EINVAL;
1507 goto finish;
1508 }
1509
1510 r = flush_caches(bus);
1511 break;
1512 }
1513
1514 finish:
1515 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1516 }