]>
Commit | Line | Data |
---|---|---|
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 | ||
4ac2ca1b | 20 | #include <gcrypt.h> |
bdef7319 | 21 | #include <getopt.h> |
cf0fbc49 | 22 | #include <net/if.h> |
bdef7319 ZJS |
23 | |
24 | #include "sd-bus.h" | |
3f6fd1ba LP |
25 | |
26 | #include "af-list.h" | |
b5efdb8a | 27 | #include "alloc-util.h" |
bdef7319 | 28 | #include "bus-error.h" |
3f6fd1ba | 29 | #include "bus-util.h" |
45ec7efb | 30 | #include "escape.h" |
bdef7319 | 31 | #include "in-addr-util.h" |
4ac2ca1b | 32 | #include "gcrypt-util.h" |
6bedfcbb | 33 | #include "parse-util.h" |
51323288 | 34 | #include "resolved-def.h" |
3f6fd1ba | 35 | #include "resolved-dns-packet.h" |
a150ff5e | 36 | #include "terminal-util.h" |
2d4c5cbc | 37 | |
bdef7319 ZJS |
38 | #define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC) |
39 | ||
40 | static int arg_family = AF_UNSPEC; | |
41 | static int arg_ifindex = 0; | |
4b548ef3 | 42 | static uint16_t arg_type = 0; |
2d4c5cbc | 43 | static uint16_t arg_class = 0; |
b93312f5 | 44 | static bool arg_legend = true; |
51323288 | 45 | static uint64_t arg_flags = 0; |
dab48ea6 | 46 | |
82d1d240 ZJS |
47 | typedef enum ServiceFamily { |
48 | SERVICE_FAMILY_TCP, | |
49 | SERVICE_FAMILY_UDP, | |
50 | SERVICE_FAMILY_SCTP, | |
51 | _SERVICE_FAMILY_INVALID = -1, | |
52 | } ServiceFamily; | |
53 | static ServiceFamily arg_service_family = SERVICE_FAMILY_TCP; | |
54 | ||
dab48ea6 ZJS |
55 | typedef enum RawType { |
56 | RAW_NONE, | |
57 | RAW_PAYLOAD, | |
58 | RAW_PACKET, | |
59 | } RawType; | |
dab48ea6 | 60 | static RawType arg_raw = RAW_NONE; |
a150ff5e LP |
61 | |
62 | static enum { | |
63 | MODE_RESOLVE_HOST, | |
64 | MODE_RESOLVE_RECORD, | |
65 | MODE_RESOLVE_SERVICE, | |
4ac2ca1b | 66 | MODE_RESOLVE_OPENPGP, |
82d1d240 | 67 | MODE_RESOLVE_TLSA, |
a150ff5e LP |
68 | MODE_STATISTICS, |
69 | MODE_RESET_STATISTICS, | |
70 | } arg_mode = MODE_RESOLVE_HOST; | |
51323288 | 71 | |
82d1d240 ZJS |
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 | ||
78c6a153 | 95 | static 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 | 124 | static 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 | |
bdef7319 | 202 | r = in_addr_to_string(family, a, &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 | ||
239 | static 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 | ||
79266746 LP |
256 | r = in_addr_to_string(family, address, &pretty); |
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 | ||
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) { | |
6ad623a3 | 355 | if (parse_ifindex(percent+1, &ifi) < 0) { |
79266746 LP |
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; | |
02dd6e18 | 370 | return 0; |
bdef7319 ZJS |
371 | } |
372 | ||
dab48ea6 ZJS |
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 | ||
c1dafe4f | 417 | static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type) { |
4afd3348 LP |
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; | |
78c6a153 | 420 | char ifname[IF_NAMESIZE] = ""; |
2d4c5cbc | 421 | unsigned n = 0; |
51323288 | 422 | uint64_t flags; |
78c6a153 | 423 | int r; |
74998408 | 424 | usec_t ts; |
41815a4a | 425 | bool needs_authentication = false; |
2d4c5cbc LP |
426 | |
427 | assert(name); | |
428 | ||
78c6a153 LP |
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 | ||
c1dafe4f | 432 | log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(class), dns_type_to_string(type), isempty(ifname) ? "*" : ifname); |
2d4c5cbc LP |
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 | ||
c1dafe4f | 444 | r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, class, type, arg_flags); |
2d4c5cbc LP |
445 | if (r < 0) |
446 | return bus_log_create_error(r); | |
447 | ||
74998408 TG |
448 | ts = now(CLOCK_MONOTONIC); |
449 | ||
2d4c5cbc LP |
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 | ||
74998408 TG |
456 | ts = now(CLOCK_MONOTONIC) - ts; |
457 | ||
78c6a153 | 458 | r = sd_bus_message_enter_container(reply, 'a', "(iqqay)"); |
51323288 LP |
459 | if (r < 0) |
460 | return bus_log_parse_error(r); | |
461 | ||
78c6a153 | 462 | while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) { |
2d4c5cbc | 463 | uint16_t c, t; |
78c6a153 | 464 | int ifindex; |
2d4c5cbc LP |
465 | const void *d; |
466 | size_t l; | |
467 | ||
78c6a153 LP |
468 | assert_cc(sizeof(int) == sizeof(int32_t)); |
469 | ||
470 | r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t); | |
2d4c5cbc LP |
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 | ||
dab48ea6 ZJS |
482 | if (arg_raw == RAW_PACKET) { |
483 | uint64_t u64 = htole64(l); | |
2d4c5cbc | 484 | |
dab48ea6 ZJS |
485 | fwrite(&u64, sizeof(u64), 1, stdout); |
486 | fwrite(d, 1, l, stdout); | |
2e74028a | 487 | } else { |
dab48ea6 ZJS |
488 | r = output_rr_packet(d, l, ifindex); |
489 | if (r < 0) | |
490 | return r; | |
2e74028a | 491 | } |
78c6a153 | 492 | |
41815a4a LP |
493 | if (dns_type_needs_authentication(t)) |
494 | needs_authentication = true; | |
495 | ||
2d4c5cbc LP |
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 | ||
51323288 LP |
505 | r = sd_bus_message_read(reply, "t", &flags); |
506 | if (r < 0) | |
507 | return bus_log_parse_error(r); | |
508 | ||
2d4c5cbc LP |
509 | if (n == 0) { |
510 | log_error("%s: no records found", name); | |
511 | return -ESRCH; | |
512 | } | |
513 | ||
78c6a153 | 514 | print_source(flags, ts); |
51323288 | 515 | |
41815a4a LP |
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 | ||
2d4c5cbc LP |
528 | return 0; |
529 | } | |
530 | ||
c1dafe4f LP |
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 | ||
c1dafe4f | 635 | if (class == 0) |
4ac2ca1b ZJS |
636 | class = arg_class ?: DNS_CLASS_IN; |
637 | if (type == 0) | |
638 | type = arg_type ?: DNS_TYPE_A; | |
c1dafe4f LP |
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 | ||
45ec7efb LP |
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; | |
4afd3348 LP |
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; | |
45ec7efb LP |
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 | if (isempty(name)) | |
663 | name = NULL; | |
664 | if (isempty(type)) | |
665 | type = NULL; | |
666 | ||
667 | if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname)) | |
668 | return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex); | |
669 | ||
670 | if (name) | |
671 | 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); | |
672 | else if (type) | |
673 | log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname); | |
674 | else | |
675 | log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname); | |
676 | ||
677 | r = sd_bus_message_new_method_call( | |
678 | bus, | |
679 | &req, | |
680 | "org.freedesktop.resolve1", | |
681 | "/org/freedesktop/resolve1", | |
682 | "org.freedesktop.resolve1.Manager", | |
683 | "ResolveService"); | |
684 | if (r < 0) | |
685 | return bus_log_create_error(r); | |
686 | ||
687 | r = sd_bus_message_append(req, "isssit", arg_ifindex, name, type, domain, arg_family, arg_flags); | |
688 | if (r < 0) | |
689 | return bus_log_create_error(r); | |
690 | ||
691 | ts = now(CLOCK_MONOTONIC); | |
692 | ||
693 | r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply); | |
694 | if (r < 0) | |
695 | return log_error_errno(r, "Resolve call failed: %s", bus_error_message(&error, r)); | |
696 | ||
697 | ts = now(CLOCK_MONOTONIC) - ts; | |
698 | ||
699 | r = sd_bus_message_enter_container(reply, 'a', "(qqqsa(iiay)s)"); | |
700 | if (r < 0) | |
701 | return bus_log_parse_error(r); | |
702 | ||
703 | indent = | |
704 | (name ? strlen(name) + 1 : 0) + | |
705 | (type ? strlen(type) + 1 : 0) + | |
706 | strlen(domain) + 2; | |
707 | ||
708 | c = 0; | |
709 | while ((r = sd_bus_message_enter_container(reply, 'r', "qqqsa(iiay)s")) > 0) { | |
710 | uint16_t priority, weight, port; | |
711 | const char *hostname, *canonical; | |
712 | ||
713 | r = sd_bus_message_read(reply, "qqqs", &priority, &weight, &port, &hostname); | |
714 | if (r < 0) | |
715 | return bus_log_parse_error(r); | |
716 | ||
717 | if (name) | |
718 | printf("%*s%s", (int) strlen(name), c == 0 ? name : "", c == 0 ? "/" : " "); | |
719 | if (type) | |
720 | printf("%*s%s", (int) strlen(type), c == 0 ? type : "", c == 0 ? "/" : " "); | |
721 | ||
722 | printf("%*s%s %s:%u [priority=%u, weight=%u]\n", | |
723 | (int) strlen(domain), c == 0 ? domain : "", | |
724 | c == 0 ? ":" : " ", | |
725 | hostname, port, | |
726 | priority, weight); | |
727 | ||
728 | r = sd_bus_message_enter_container(reply, 'a', "(iiay)"); | |
729 | if (r < 0) | |
730 | return bus_log_parse_error(r); | |
731 | ||
732 | while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) { | |
733 | _cleanup_free_ char *pretty = NULL; | |
734 | int ifindex, family; | |
735 | const void *a; | |
736 | ||
737 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
738 | ||
739 | r = sd_bus_message_read(reply, "ii", &ifindex, &family); | |
740 | if (r < 0) | |
741 | return bus_log_parse_error(r); | |
742 | ||
743 | r = sd_bus_message_read_array(reply, 'y', &a, &sz); | |
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 | if (!IN_SET(family, AF_INET, AF_INET6)) { | |
752 | log_debug("%s: skipping entry with family %d (%s)", name, family, af_to_name(family) ?: "unknown"); | |
753 | continue; | |
754 | } | |
755 | ||
756 | if (sz != FAMILY_ADDRESS_SIZE(family)) { | |
757 | log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown"); | |
758 | return -EINVAL; | |
759 | } | |
760 | ||
761 | ifname[0] = 0; | |
762 | if (ifindex > 0 && !if_indextoname(ifindex, ifname)) | |
763 | log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex); | |
764 | ||
765 | r = in_addr_to_string(family, a, &pretty); | |
766 | if (r < 0) | |
767 | return log_error_errno(r, "Failed to print address for %s: %m", name); | |
768 | ||
769 | printf("%*s%s%s%s\n", (int) indent, "", pretty, isempty(ifname) ? "" : "%s", ifname); | |
770 | } | |
771 | if (r < 0) | |
772 | return bus_log_parse_error(r); | |
773 | ||
774 | r = sd_bus_message_exit_container(reply); | |
775 | if (r < 0) | |
776 | return bus_log_parse_error(r); | |
777 | ||
778 | r = sd_bus_message_read(reply, "s", &canonical); | |
779 | if (r < 0) | |
780 | return bus_log_parse_error(r); | |
781 | ||
782 | if (!streq(hostname, canonical)) | |
783 | printf("%*s(%s)\n", (int) indent, "", canonical); | |
784 | ||
785 | r = sd_bus_message_exit_container(reply); | |
786 | if (r < 0) | |
787 | return bus_log_parse_error(r); | |
788 | ||
789 | c++; | |
790 | } | |
791 | if (r < 0) | |
792 | return bus_log_parse_error(r); | |
793 | ||
794 | r = sd_bus_message_exit_container(reply); | |
795 | if (r < 0) | |
796 | return bus_log_parse_error(r); | |
797 | ||
798 | r = sd_bus_message_enter_container(reply, 'a', "ay"); | |
799 | if (r < 0) | |
800 | return bus_log_parse_error(r); | |
801 | ||
802 | c = 0; | |
803 | while ((r = sd_bus_message_read_array(reply, 'y', (const void**) &p, &sz)) > 0) { | |
804 | _cleanup_free_ char *escaped = NULL; | |
805 | ||
806 | escaped = cescape_length(p, sz); | |
807 | if (!escaped) | |
808 | return log_oom(); | |
809 | ||
810 | printf("%*s%s\n", (int) indent, "", escaped); | |
811 | c++; | |
812 | } | |
813 | if (r < 0) | |
814 | return bus_log_parse_error(r); | |
815 | ||
816 | r = sd_bus_message_exit_container(reply); | |
817 | if (r < 0) | |
818 | return bus_log_parse_error(r); | |
819 | ||
820 | r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags); | |
821 | if (r < 0) | |
822 | return bus_log_parse_error(r); | |
823 | ||
824 | if (isempty(canonical_name)) | |
825 | canonical_name = NULL; | |
826 | if (isempty(canonical_type)) | |
827 | canonical_type = NULL; | |
828 | ||
829 | if (!streq_ptr(name, canonical_name) || | |
830 | !streq_ptr(type, canonical_type) || | |
831 | !streq_ptr(domain, canonical_domain)) { | |
832 | ||
833 | printf("%*s(", (int) indent, ""); | |
834 | ||
835 | if (canonical_name) | |
836 | printf("%s/", canonical_name); | |
837 | if (canonical_type) | |
838 | printf("%s/", canonical_type); | |
839 | ||
840 | printf("%s)\n", canonical_domain); | |
841 | } | |
842 | ||
843 | print_source(flags, ts); | |
844 | ||
845 | return 0; | |
846 | } | |
847 | ||
4ac2ca1b ZJS |
848 | static int resolve_openpgp(sd_bus *bus, const char *address) { |
849 | const char *domain, *full; | |
850 | int r; | |
851 | _cleanup_free_ char *hashed = NULL; | |
852 | ||
853 | assert(bus); | |
854 | assert(address); | |
855 | ||
856 | domain = strrchr(address, '@'); | |
857 | if (!domain) { | |
858 | log_error("Address does not contain '@': \"%s\"", address); | |
859 | return -EINVAL; | |
860 | } else if (domain == address || domain[1] == '\0') { | |
861 | log_error("Address starts or ends with '@': \"%s\"", address); | |
862 | return -EINVAL; | |
863 | } | |
864 | domain++; | |
865 | ||
866 | r = string_hashsum(address, domain - 1 - address, GCRY_MD_SHA224, &hashed); | |
867 | if (r < 0) | |
868 | return log_error_errno(r, "Hashing failed: %m"); | |
869 | ||
870 | full = strjoina(hashed, "._openpgpkey.", domain); | |
871 | log_debug("Looking up \"%s\".", full); | |
872 | ||
873 | return resolve_record(bus, full, | |
874 | arg_class ?: DNS_CLASS_IN, | |
875 | arg_type ?: DNS_TYPE_OPENPGPKEY); | |
876 | } | |
877 | ||
82d1d240 ZJS |
878 | static int resolve_tlsa(sd_bus *bus, const char *address) { |
879 | const char *port; | |
880 | uint16_t port_num = 443; | |
881 | _cleanup_free_ char *full = NULL; | |
882 | int r; | |
883 | ||
884 | assert(bus); | |
885 | assert(address); | |
886 | ||
887 | port = strrchr(address, ':'); | |
888 | if (port) { | |
889 | r = safe_atou16(port + 1, &port_num); | |
890 | if (r < 0 || port_num == 0) | |
891 | return log_error_errno(r, "Invalid port \"%s\".", port + 1); | |
892 | ||
893 | address = strndupa(address, port - address); | |
894 | } | |
895 | ||
896 | r = asprintf(&full, "_%u.%s.%s", | |
897 | port_num, | |
898 | service_family_to_string(arg_service_family), | |
899 | address); | |
900 | if (r < 0) | |
901 | return log_oom(); | |
902 | ||
903 | log_debug("Looking up \"%s\".", full); | |
904 | ||
905 | return resolve_record(bus, full, | |
906 | arg_class ?: DNS_CLASS_IN, | |
907 | arg_type ?: DNS_TYPE_TLSA); | |
908 | } | |
909 | ||
a150ff5e LP |
910 | static int show_statistics(sd_bus *bus) { |
911 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
912 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
913 | uint64_t n_current_transactions, n_total_transactions, | |
914 | cache_size, n_cache_hit, n_cache_miss, | |
915 | n_dnssec_secure, n_dnssec_insecure, n_dnssec_bogus, n_dnssec_indeterminate; | |
593f665c | 916 | int r, dnssec_supported; |
a150ff5e LP |
917 | |
918 | assert(bus); | |
919 | ||
593f665c LP |
920 | r = sd_bus_get_property_trivial(bus, |
921 | "org.freedesktop.resolve1", | |
922 | "/org/freedesktop/resolve1", | |
923 | "org.freedesktop.resolve1.Manager", | |
924 | "DNSSECSupported", | |
925 | &error, | |
926 | 'b', | |
927 | &dnssec_supported); | |
928 | if (r < 0) | |
929 | return log_error_errno(r, "Failed to get DNSSEC supported state: %s", bus_error_message(&error, r)); | |
930 | ||
931 | printf("DNSSEC supported by current servers: %s%s%s\n\n", | |
932 | ansi_highlight(), | |
933 | yes_no(dnssec_supported), | |
934 | ansi_normal()); | |
935 | ||
a150ff5e LP |
936 | r = sd_bus_get_property(bus, |
937 | "org.freedesktop.resolve1", | |
938 | "/org/freedesktop/resolve1", | |
939 | "org.freedesktop.resolve1.Manager", | |
940 | "TransactionStatistics", | |
941 | &error, | |
942 | &reply, | |
943 | "(tt)"); | |
944 | if (r < 0) | |
945 | return log_error_errno(r, "Failed to get transaction statistics: %s", bus_error_message(&error, r)); | |
946 | ||
947 | r = sd_bus_message_read(reply, "(tt)", | |
948 | &n_current_transactions, | |
949 | &n_total_transactions); | |
950 | if (r < 0) | |
951 | return bus_log_parse_error(r); | |
952 | ||
953 | printf("%sTransactions%s\n" | |
954 | "Current Transactions: %" PRIu64 "\n" | |
955 | " Total Transactions: %" PRIu64 "\n", | |
956 | ansi_highlight(), | |
957 | ansi_normal(), | |
958 | n_current_transactions, | |
959 | n_total_transactions); | |
960 | ||
961 | reply = sd_bus_message_unref(reply); | |
962 | ||
963 | r = sd_bus_get_property(bus, | |
964 | "org.freedesktop.resolve1", | |
965 | "/org/freedesktop/resolve1", | |
966 | "org.freedesktop.resolve1.Manager", | |
967 | "CacheStatistics", | |
968 | &error, | |
969 | &reply, | |
970 | "(ttt)"); | |
f7700834 TA |
971 | if (r < 0) |
972 | return log_error_errno(r, "Failed to get cache statistics: %s", bus_error_message(&error, r)); | |
a150ff5e LP |
973 | |
974 | r = sd_bus_message_read(reply, "(ttt)", | |
975 | &cache_size, | |
976 | &n_cache_hit, | |
977 | &n_cache_miss); | |
978 | if (r < 0) | |
979 | return bus_log_parse_error(r); | |
980 | ||
981 | printf("\n%sCache%s\n" | |
982 | " Current Cache Size: %" PRIu64 "\n" | |
983 | " Cache Hits: %" PRIu64 "\n" | |
984 | " Cache Misses: %" PRIu64 "\n", | |
985 | ansi_highlight(), | |
986 | ansi_normal(), | |
987 | cache_size, | |
988 | n_cache_hit, | |
989 | n_cache_miss); | |
990 | ||
991 | reply = sd_bus_message_unref(reply); | |
992 | ||
993 | r = sd_bus_get_property(bus, | |
994 | "org.freedesktop.resolve1", | |
995 | "/org/freedesktop/resolve1", | |
996 | "org.freedesktop.resolve1.Manager", | |
997 | "DNSSECStatistics", | |
998 | &error, | |
999 | &reply, | |
1000 | "(tttt)"); | |
f7700834 TA |
1001 | if (r < 0) |
1002 | return log_error_errno(r, "Failed to get DNSSEC statistics: %s", bus_error_message(&error, r)); | |
a150ff5e LP |
1003 | |
1004 | r = sd_bus_message_read(reply, "(tttt)", | |
1005 | &n_dnssec_secure, | |
1006 | &n_dnssec_insecure, | |
1007 | &n_dnssec_bogus, | |
1008 | &n_dnssec_indeterminate); | |
1009 | if (r < 0) | |
1010 | return bus_log_parse_error(r); | |
1011 | ||
7405bb3e LP |
1012 | printf("\n%sDNSSEC Verdicts%s\n" |
1013 | " Secure: %" PRIu64 "\n" | |
1014 | " Insecure: %" PRIu64 "\n" | |
1015 | " Bogus: %" PRIu64 "\n" | |
1016 | " Indeterminate: %" PRIu64 "\n", | |
a150ff5e LP |
1017 | ansi_highlight(), |
1018 | ansi_normal(), | |
1019 | n_dnssec_secure, | |
1020 | n_dnssec_insecure, | |
1021 | n_dnssec_bogus, | |
1022 | n_dnssec_indeterminate); | |
1023 | ||
1024 | return 0; | |
1025 | } | |
1026 | ||
1027 | static int reset_statistics(sd_bus *bus) { | |
1028 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1029 | int r; | |
1030 | ||
1031 | r = sd_bus_call_method(bus, | |
1032 | "org.freedesktop.resolve1", | |
1033 | "/org/freedesktop/resolve1", | |
1034 | "org.freedesktop.resolve1.Manager", | |
1035 | "ResetStatistics", | |
1036 | &error, | |
1037 | NULL, | |
1038 | NULL); | |
1039 | if (r < 0) | |
1040 | return log_error_errno(r, "Failed to reset statistics: %s", bus_error_message(&error, r)); | |
1041 | ||
1042 | return 0; | |
1043 | } | |
1044 | ||
ba82da3b ZJS |
1045 | static void help_protocol_types(void) { |
1046 | if (arg_legend) | |
1047 | puts("Known protocol types:"); | |
1048 | puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6"); | |
1049 | } | |
1050 | ||
b93312f5 ZJS |
1051 | static void help_dns_types(void) { |
1052 | int i; | |
1053 | const char *t; | |
1054 | ||
1055 | if (arg_legend) | |
09b1fe14 | 1056 | puts("Known DNS RR types:"); |
b93312f5 ZJS |
1057 | for (i = 0; i < _DNS_TYPE_MAX; i++) { |
1058 | t = dns_type_to_string(i); | |
1059 | if (t) | |
1060 | puts(t); | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | static void help_dns_classes(void) { | |
1065 | int i; | |
1066 | const char *t; | |
1067 | ||
1068 | if (arg_legend) | |
09b1fe14 | 1069 | puts("Known DNS RR classes:"); |
b93312f5 ZJS |
1070 | for (i = 0; i < _DNS_CLASS_MAX; i++) { |
1071 | t = dns_class_to_string(i); | |
1072 | if (t) | |
1073 | puts(t); | |
1074 | } | |
1075 | } | |
1076 | ||
bdef7319 | 1077 | static void help(void) { |
1ace2438 ZJS |
1078 | printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n" |
1079 | "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n" | |
1080 | "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n" | |
1081 | "%1$s [OPTIONS...] --statistics\n" | |
1082 | "%1$s [OPTIONS...] --reset-statistics\n" | |
1083 | "\n" | |
300a716d | 1084 | "Resolve domain names, IPv4 and IPv6 addresses, DNS resource records, and services.\n\n" |
1ace2438 ZJS |
1085 | " -h --help Show this help\n" |
1086 | " --version Show package version\n" | |
1087 | " -4 Resolve IPv4 addresses\n" | |
1088 | " -6 Resolve IPv6 addresses\n" | |
1089 | " -i --interface=INTERFACE Look on interface\n" | |
1090 | " -p --protocol=PROTO|help Look via protocol\n" | |
1091 | " -t --type=TYPE|help Query RR with DNS type\n" | |
1092 | " -c --class=CLASS|help Query RR with DNS class\n" | |
1093 | " --service Resolve service (SRV)\n" | |
1094 | " --service-address=BOOL Resolve address for services (default: yes)\n" | |
1095 | " --service-txt=BOOL Resolve TXT records for services (default: yes)\n" | |
1096 | " --openpgp Query OpenPGP public key\n" | |
82d1d240 | 1097 | " --tlsa Query TLS public key\n" |
1ace2438 ZJS |
1098 | " --cname=BOOL Follow CNAME redirects (default: yes)\n" |
1099 | " --search=BOOL Use search domains for single-label names\n" | |
1100 | " (default: yes)\n" | |
dab48ea6 | 1101 | " --raw[=payload|packet] Dump the answer as binary data\n" |
1ace2438 ZJS |
1102 | " --legend=BOOL Print headers and additional info (default: yes)\n" |
1103 | " --statistics Show resolver statistics\n" | |
1104 | " --reset-statistics Reset resolver statistics\n" | |
1105 | , program_invocation_short_name); | |
bdef7319 ZJS |
1106 | } |
1107 | ||
1108 | static int parse_argv(int argc, char *argv[]) { | |
1109 | enum { | |
1110 | ARG_VERSION = 0x100, | |
dad29dff | 1111 | ARG_LEGEND, |
45ec7efb LP |
1112 | ARG_SERVICE, |
1113 | ARG_CNAME, | |
1114 | ARG_SERVICE_ADDRESS, | |
1115 | ARG_SERVICE_TXT, | |
4ac2ca1b | 1116 | ARG_OPENPGP, |
82d1d240 | 1117 | ARG_TLSA, |
2e74028a | 1118 | ARG_RAW, |
801ad6a6 | 1119 | ARG_SEARCH, |
a150ff5e LP |
1120 | ARG_STATISTICS, |
1121 | ARG_RESET_STATISTICS, | |
bdef7319 ZJS |
1122 | }; |
1123 | ||
1124 | static const struct option options[] = { | |
a150ff5e LP |
1125 | { "help", no_argument, NULL, 'h' }, |
1126 | { "version", no_argument, NULL, ARG_VERSION }, | |
1127 | { "type", required_argument, NULL, 't' }, | |
1128 | { "class", required_argument, NULL, 'c' }, | |
1129 | { "legend", required_argument, NULL, ARG_LEGEND }, | |
5b911843 | 1130 | { "interface", required_argument, NULL, 'i' }, |
a150ff5e LP |
1131 | { "protocol", required_argument, NULL, 'p' }, |
1132 | { "cname", required_argument, NULL, ARG_CNAME }, | |
1133 | { "service", no_argument, NULL, ARG_SERVICE }, | |
1134 | { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS }, | |
1135 | { "service-txt", required_argument, NULL, ARG_SERVICE_TXT }, | |
4ac2ca1b | 1136 | { "openpgp", no_argument, NULL, ARG_OPENPGP }, |
82d1d240 | 1137 | { "tlsa", optional_argument, NULL, ARG_TLSA }, |
dab48ea6 | 1138 | { "raw", optional_argument, NULL, ARG_RAW }, |
a150ff5e LP |
1139 | { "search", required_argument, NULL, ARG_SEARCH }, |
1140 | { "statistics", no_argument, NULL, ARG_STATISTICS, }, | |
1141 | { "reset-statistics", no_argument, NULL, ARG_RESET_STATISTICS }, | |
bdef7319 ZJS |
1142 | {} |
1143 | }; | |
1144 | ||
2d4c5cbc | 1145 | int c, r; |
bdef7319 ZJS |
1146 | |
1147 | assert(argc >= 0); | |
1148 | assert(argv); | |
1149 | ||
51323288 | 1150 | while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0) |
bdef7319 ZJS |
1151 | switch(c) { |
1152 | ||
1153 | case 'h': | |
1154 | help(); | |
1155 | return 0; /* done */; | |
1156 | ||
1157 | case ARG_VERSION: | |
3f6fd1ba | 1158 | return version(); |
bdef7319 ZJS |
1159 | |
1160 | case '4': | |
1161 | arg_family = AF_INET; | |
1162 | break; | |
1163 | ||
1164 | case '6': | |
1165 | arg_family = AF_INET6; | |
1166 | break; | |
1167 | ||
3fa4999b LP |
1168 | case 'i': { |
1169 | int ifi; | |
1170 | ||
6ad623a3 | 1171 | if (parse_ifindex(optarg, &ifi) >= 0) |
3fa4999b LP |
1172 | arg_ifindex = ifi; |
1173 | else { | |
1174 | ifi = if_nametoindex(optarg); | |
1175 | if (ifi <= 0) | |
1176 | return log_error_errno(errno, "Unknown interface %s: %m", optarg); | |
1177 | ||
1178 | arg_ifindex = ifi; | |
1179 | } | |
1180 | ||
2d4c5cbc | 1181 | break; |
3fa4999b | 1182 | } |
2d4c5cbc LP |
1183 | |
1184 | case 't': | |
b93312f5 ZJS |
1185 | if (streq(optarg, "help")) { |
1186 | help_dns_types(); | |
1187 | return 0; | |
1188 | } | |
1189 | ||
4b548ef3 LP |
1190 | r = dns_type_from_string(optarg); |
1191 | if (r < 0) { | |
2d4c5cbc | 1192 | log_error("Failed to parse RR record type %s", optarg); |
4b548ef3 | 1193 | return r; |
2d4c5cbc | 1194 | } |
4b548ef3 LP |
1195 | arg_type = (uint16_t) r; |
1196 | assert((int) arg_type == r); | |
b93312f5 | 1197 | |
a150ff5e | 1198 | arg_mode = MODE_RESOLVE_RECORD; |
2d4c5cbc LP |
1199 | break; |
1200 | ||
1201 | case 'c': | |
b93312f5 ZJS |
1202 | if (streq(optarg, "help")) { |
1203 | help_dns_classes(); | |
1204 | return 0; | |
1205 | } | |
1206 | ||
4b548ef3 | 1207 | r = dns_class_from_string(optarg); |
2d4c5cbc LP |
1208 | if (r < 0) { |
1209 | log_error("Failed to parse RR record class %s", optarg); | |
1210 | return r; | |
bdef7319 | 1211 | } |
4b548ef3 LP |
1212 | arg_class = (uint16_t) r; |
1213 | assert((int) arg_class == r); | |
b93312f5 ZJS |
1214 | |
1215 | break; | |
1216 | ||
dad29dff | 1217 | case ARG_LEGEND: |
45ec7efb LP |
1218 | r = parse_boolean(optarg); |
1219 | if (r < 0) | |
1220 | return log_error_errno(r, "Failed to parse --legend= argument"); | |
1221 | ||
1222 | arg_legend = r; | |
bdef7319 ZJS |
1223 | break; |
1224 | ||
51323288 | 1225 | case 'p': |
ba82da3b ZJS |
1226 | if (streq(optarg, "help")) { |
1227 | help_protocol_types(); | |
1228 | return 0; | |
1229 | } else if (streq(optarg, "dns")) | |
51323288 LP |
1230 | arg_flags |= SD_RESOLVED_DNS; |
1231 | else if (streq(optarg, "llmnr")) | |
1232 | arg_flags |= SD_RESOLVED_LLMNR; | |
1233 | else if (streq(optarg, "llmnr-ipv4")) | |
1234 | arg_flags |= SD_RESOLVED_LLMNR_IPV4; | |
1235 | else if (streq(optarg, "llmnr-ipv6")) | |
1236 | arg_flags |= SD_RESOLVED_LLMNR_IPV6; | |
1237 | else { | |
1238 | log_error("Unknown protocol specifier: %s", optarg); | |
1239 | return -EINVAL; | |
1240 | } | |
1241 | ||
1242 | break; | |
1243 | ||
45ec7efb | 1244 | case ARG_SERVICE: |
a150ff5e | 1245 | arg_mode = MODE_RESOLVE_SERVICE; |
45ec7efb LP |
1246 | break; |
1247 | ||
4ac2ca1b ZJS |
1248 | case ARG_OPENPGP: |
1249 | arg_mode = MODE_RESOLVE_OPENPGP; | |
1250 | break; | |
1251 | ||
82d1d240 ZJS |
1252 | case ARG_TLSA: |
1253 | arg_mode = MODE_RESOLVE_TLSA; | |
1254 | arg_service_family = service_family_from_string(optarg); | |
1255 | if (arg_service_family < 0) { | |
1256 | log_error("Unknown service family \"%s\".", optarg); | |
1257 | return -EINVAL; | |
1258 | } | |
1259 | break; | |
1260 | ||
2e74028a ZJS |
1261 | case ARG_RAW: |
1262 | if (on_tty()) { | |
1263 | log_error("Refusing to write binary data to tty."); | |
1264 | return -ENOTTY; | |
1265 | } | |
1266 | ||
dab48ea6 ZJS |
1267 | if (optarg == NULL || streq(optarg, "payload")) |
1268 | arg_raw = RAW_PAYLOAD; | |
1269 | else if (streq(optarg, "packet")) | |
1270 | arg_raw = RAW_PACKET; | |
1271 | else { | |
1272 | log_error("Unknown --raw specifier \"%s\".", optarg); | |
1273 | return -EINVAL; | |
1274 | } | |
1275 | ||
2e74028a ZJS |
1276 | arg_legend = false; |
1277 | break; | |
1278 | ||
45ec7efb LP |
1279 | case ARG_CNAME: |
1280 | r = parse_boolean(optarg); | |
1281 | if (r < 0) | |
1282 | return log_error_errno(r, "Failed to parse --cname= argument."); | |
1283 | if (r == 0) | |
1284 | arg_flags |= SD_RESOLVED_NO_CNAME; | |
1285 | else | |
1286 | arg_flags &= ~SD_RESOLVED_NO_CNAME; | |
1287 | break; | |
1288 | ||
1289 | case ARG_SERVICE_ADDRESS: | |
1290 | r = parse_boolean(optarg); | |
1291 | if (r < 0) | |
1292 | return log_error_errno(r, "Failed to parse --service-address= argument."); | |
1293 | if (r == 0) | |
1294 | arg_flags |= SD_RESOLVED_NO_ADDRESS; | |
1295 | else | |
1296 | arg_flags &= ~SD_RESOLVED_NO_ADDRESS; | |
1297 | break; | |
1298 | ||
1299 | case ARG_SERVICE_TXT: | |
1300 | r = parse_boolean(optarg); | |
1301 | if (r < 0) | |
1302 | return log_error_errno(r, "Failed to parse --service-txt= argument."); | |
1303 | if (r == 0) | |
1304 | arg_flags |= SD_RESOLVED_NO_TXT; | |
1305 | else | |
1306 | arg_flags &= ~SD_RESOLVED_NO_TXT; | |
1307 | break; | |
1308 | ||
801ad6a6 LP |
1309 | case ARG_SEARCH: |
1310 | r = parse_boolean(optarg); | |
1311 | if (r < 0) | |
1312 | return log_error_errno(r, "Failed to parse --search argument."); | |
1313 | if (r == 0) | |
1314 | arg_flags |= SD_RESOLVED_NO_SEARCH; | |
1315 | else | |
1316 | arg_flags &= ~SD_RESOLVED_NO_SEARCH; | |
1317 | break; | |
1318 | ||
a150ff5e LP |
1319 | case ARG_STATISTICS: |
1320 | arg_mode = MODE_STATISTICS; | |
1321 | break; | |
1322 | ||
1323 | case ARG_RESET_STATISTICS: | |
1324 | arg_mode = MODE_RESET_STATISTICS; | |
1325 | break; | |
1326 | ||
bdef7319 ZJS |
1327 | case '?': |
1328 | return -EINVAL; | |
1329 | ||
1330 | default: | |
1331 | assert_not_reached("Unhandled option"); | |
1332 | } | |
1333 | ||
2d4c5cbc | 1334 | if (arg_type == 0 && arg_class != 0) { |
45ec7efb LP |
1335 | log_error("--class= may only be used in conjunction with --type=."); |
1336 | return -EINVAL; | |
1337 | } | |
1338 | ||
82d1d240 | 1339 | if (arg_type != 0 && arg_mode == MODE_RESOLVE_SERVICE) { |
45ec7efb | 1340 | log_error("--service and --type= may not be combined."); |
2d4c5cbc LP |
1341 | return -EINVAL; |
1342 | } | |
1343 | ||
1344 | if (arg_type != 0 && arg_class == 0) | |
1345 | arg_class = DNS_CLASS_IN; | |
1346 | ||
c1dafe4f LP |
1347 | if (arg_class != 0 && arg_type == 0) |
1348 | arg_type = DNS_TYPE_A; | |
1349 | ||
bdef7319 ZJS |
1350 | return 1 /* work to do */; |
1351 | } | |
1352 | ||
bdef7319 | 1353 | int main(int argc, char **argv) { |
4afd3348 | 1354 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
bdef7319 ZJS |
1355 | int r; |
1356 | ||
1357 | log_parse_environment(); | |
1358 | log_open(); | |
1359 | ||
1360 | r = parse_argv(argc, argv); | |
1361 | if (r <= 0) | |
79266746 LP |
1362 | goto finish; |
1363 | ||
bdef7319 ZJS |
1364 | r = sd_bus_open_system(&bus); |
1365 | if (r < 0) { | |
da927ba9 | 1366 | log_error_errno(r, "sd_bus_open_system: %m"); |
79266746 | 1367 | goto finish; |
bdef7319 ZJS |
1368 | } |
1369 | ||
a150ff5e | 1370 | switch (arg_mode) { |
45ec7efb | 1371 | |
a150ff5e LP |
1372 | case MODE_RESOLVE_HOST: |
1373 | if (optind >= argc) { | |
300a716d | 1374 | log_error("No arguments passed."); |
a150ff5e LP |
1375 | r = -EINVAL; |
1376 | goto finish; | |
1377 | } | |
1378 | ||
1379 | while (argv[optind]) { | |
1380 | int family, ifindex, k; | |
1381 | union in_addr_union a; | |
1382 | ||
c1dafe4f LP |
1383 | if (startswith(argv[optind], "dns:")) |
1384 | k = resolve_rfc4501(bus, argv[optind]); | |
1385 | else { | |
1386 | k = parse_address(argv[optind], &family, &a, &ifindex); | |
1387 | if (k >= 0) | |
1388 | k = resolve_address(bus, family, &a, ifindex); | |
1389 | else | |
1390 | k = resolve_host(bus, argv[optind]); | |
1391 | } | |
a150ff5e LP |
1392 | |
1393 | if (r == 0) | |
1394 | r = k; | |
1395 | ||
1396 | optind++; | |
1397 | } | |
1398 | break; | |
1399 | ||
1400 | case MODE_RESOLVE_RECORD: | |
1401 | if (optind >= argc) { | |
300a716d | 1402 | log_error("No arguments passed."); |
a150ff5e LP |
1403 | r = -EINVAL; |
1404 | goto finish; | |
1405 | } | |
1406 | ||
1407 | while (argv[optind]) { | |
1408 | int k; | |
1409 | ||
c1dafe4f | 1410 | k = resolve_record(bus, argv[optind], arg_class, arg_type); |
a150ff5e LP |
1411 | if (r == 0) |
1412 | r = k; | |
1413 | ||
1414 | optind++; | |
1415 | } | |
1416 | break; | |
1417 | ||
1418 | case MODE_RESOLVE_SERVICE: | |
45ec7efb LP |
1419 | if (argc < optind + 1) { |
1420 | log_error("Domain specification required."); | |
1421 | r = -EINVAL; | |
1422 | goto finish; | |
1423 | ||
1424 | } else if (argc == optind + 1) | |
1425 | r = resolve_service(bus, NULL, NULL, argv[optind]); | |
1426 | else if (argc == optind + 2) | |
1427 | r = resolve_service(bus, NULL, argv[optind], argv[optind+1]); | |
1428 | else if (argc == optind + 3) | |
1429 | r = resolve_service(bus, argv[optind], argv[optind+1], argv[optind+2]); | |
1430 | else { | |
300a716d | 1431 | log_error("Too many arguments."); |
45ec7efb LP |
1432 | r = -EINVAL; |
1433 | goto finish; | |
1434 | } | |
1435 | ||
a150ff5e | 1436 | break; |
79266746 | 1437 | |
4ac2ca1b ZJS |
1438 | case MODE_RESOLVE_OPENPGP: |
1439 | if (argc < optind + 1) { | |
1440 | log_error("E-mail address required."); | |
1441 | r = -EINVAL; | |
1442 | goto finish; | |
1443 | ||
1444 | } | |
1445 | ||
1446 | r = 0; | |
1447 | while (optind < argc) { | |
1448 | int k; | |
1449 | ||
1450 | k = resolve_openpgp(bus, argv[optind++]); | |
1451 | if (k < 0) | |
1452 | r = k; | |
1453 | } | |
1454 | break; | |
1455 | ||
82d1d240 ZJS |
1456 | case MODE_RESOLVE_TLSA: |
1457 | if (argc < optind + 1) { | |
1458 | log_error("Domain name required."); | |
1459 | r = -EINVAL; | |
1460 | goto finish; | |
1461 | ||
1462 | } | |
1463 | ||
1464 | r = 0; | |
1465 | while (optind < argc) { | |
1466 | int k; | |
1467 | ||
1468 | k = resolve_tlsa(bus, argv[optind++]); | |
1469 | if (k < 0) | |
1470 | r = k; | |
1471 | } | |
1472 | break; | |
1473 | ||
a150ff5e LP |
1474 | case MODE_STATISTICS: |
1475 | if (argc > optind) { | |
1476 | log_error("Too many arguments."); | |
1477 | r = -EINVAL; | |
1478 | goto finish; | |
2d4c5cbc | 1479 | } |
bdef7319 | 1480 | |
a150ff5e LP |
1481 | r = show_statistics(bus); |
1482 | break; | |
1483 | ||
1484 | case MODE_RESET_STATISTICS: | |
1485 | if (argc > optind) { | |
1486 | log_error("Too many arguments."); | |
1487 | r = -EINVAL; | |
1488 | goto finish; | |
1489 | } | |
79266746 | 1490 | |
a150ff5e LP |
1491 | r = reset_statistics(bus); |
1492 | break; | |
bdef7319 ZJS |
1493 | } |
1494 | ||
79266746 | 1495 | finish: |
bdef7319 ZJS |
1496 | return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
1497 | } |