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