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