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