]>
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 | |
bdef7319 ZJS |
41 | #define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC) |
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 LP |
116 | flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "", |
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 | ||
360 | r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0); | |
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 | ||
c1dafe4f | 398 | static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type) { |
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) { | |
433 | log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r)); | |
434 | return r; | |
435 | } | |
436 | ||
74998408 TG |
437 | ts = now(CLOCK_MONOTONIC) - ts; |
438 | ||
78c6a153 | 439 | r = sd_bus_message_enter_container(reply, 'a', "(iqqay)"); |
51323288 LP |
440 | if (r < 0) |
441 | return bus_log_parse_error(r); | |
442 | ||
78c6a153 | 443 | while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) { |
2d4c5cbc | 444 | uint16_t c, t; |
78c6a153 | 445 | int ifindex; |
2d4c5cbc LP |
446 | const void *d; |
447 | size_t l; | |
448 | ||
78c6a153 LP |
449 | assert_cc(sizeof(int) == sizeof(int32_t)); |
450 | ||
451 | r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t); | |
2d4c5cbc LP |
452 | if (r < 0) |
453 | return bus_log_parse_error(r); | |
454 | ||
455 | r = sd_bus_message_read_array(reply, 'y', &d, &l); | |
456 | if (r < 0) | |
457 | return bus_log_parse_error(r); | |
458 | ||
459 | r = sd_bus_message_exit_container(reply); | |
460 | if (r < 0) | |
461 | return bus_log_parse_error(r); | |
462 | ||
dab48ea6 ZJS |
463 | if (arg_raw == RAW_PACKET) { |
464 | uint64_t u64 = htole64(l); | |
2d4c5cbc | 465 | |
dab48ea6 ZJS |
466 | fwrite(&u64, sizeof(u64), 1, stdout); |
467 | fwrite(d, 1, l, stdout); | |
2e74028a | 468 | } else { |
dab48ea6 ZJS |
469 | r = output_rr_packet(d, l, ifindex); |
470 | if (r < 0) | |
471 | return r; | |
2e74028a | 472 | } |
78c6a153 | 473 | |
41815a4a LP |
474 | if (dns_type_needs_authentication(t)) |
475 | needs_authentication = true; | |
476 | ||
2d4c5cbc LP |
477 | n++; |
478 | } | |
479 | if (r < 0) | |
480 | return bus_log_parse_error(r); | |
481 | ||
482 | r = sd_bus_message_exit_container(reply); | |
483 | if (r < 0) | |
484 | return bus_log_parse_error(r); | |
485 | ||
51323288 LP |
486 | r = sd_bus_message_read(reply, "t", &flags); |
487 | if (r < 0) | |
488 | return bus_log_parse_error(r); | |
489 | ||
2d4c5cbc LP |
490 | if (n == 0) { |
491 | log_error("%s: no records found", name); | |
492 | return -ESRCH; | |
493 | } | |
494 | ||
78c6a153 | 495 | print_source(flags, ts); |
51323288 | 496 | |
41815a4a LP |
497 | if ((flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) { |
498 | fflush(stdout); | |
499 | ||
500 | fprintf(stderr, "\n%s" | |
501 | "WARNING: The resources shown contain cryptographic key data which could not be\n" | |
502 | " authenticated. It is not suitable to authenticate any communication.\n" | |
503 | " This is usually indication that DNSSEC authentication was not enabled\n" | |
504 | " or is not available for the selected protocol or DNS servers.%s\n", | |
505 | ansi_highlight_red(), | |
506 | ansi_normal()); | |
507 | } | |
508 | ||
2d4c5cbc LP |
509 | return 0; |
510 | } | |
511 | ||
c1dafe4f LP |
512 | static int resolve_rfc4501(sd_bus *bus, const char *name) { |
513 | uint16_t type = 0, class = 0; | |
514 | const char *p, *q, *n; | |
515 | int r; | |
516 | ||
517 | assert(bus); | |
518 | assert(name); | |
519 | assert(startswith(name, "dns:")); | |
520 | ||
521 | /* Parse RFC 4501 dns: URIs */ | |
522 | ||
523 | p = name + 4; | |
524 | ||
525 | if (p[0] == '/') { | |
526 | const char *e; | |
527 | ||
528 | if (p[1] != '/') | |
529 | goto invalid; | |
530 | ||
531 | e = strchr(p + 2, '/'); | |
532 | if (!e) | |
533 | goto invalid; | |
534 | ||
535 | if (e != p + 2) | |
536 | log_warning("DNS authority specification not supported; ignoring specified authority."); | |
537 | ||
538 | p = e + 1; | |
539 | } | |
540 | ||
541 | q = strchr(p, '?'); | |
542 | if (q) { | |
543 | n = strndupa(p, q - p); | |
544 | q++; | |
545 | ||
546 | for (;;) { | |
547 | const char *f; | |
548 | ||
549 | f = startswith_no_case(q, "class="); | |
550 | if (f) { | |
551 | _cleanup_free_ char *t = NULL; | |
552 | const char *e; | |
553 | ||
554 | if (class != 0) { | |
555 | log_error("DNS class specified twice."); | |
556 | return -EINVAL; | |
557 | } | |
558 | ||
559 | e = strchrnul(f, ';'); | |
560 | t = strndup(f, e - f); | |
561 | if (!t) | |
562 | return log_oom(); | |
563 | ||
564 | r = dns_class_from_string(t); | |
565 | if (r < 0) { | |
566 | log_error("Unknown DNS class %s.", t); | |
567 | return -EINVAL; | |
568 | } | |
569 | ||
570 | class = r; | |
571 | ||
572 | if (*e == ';') { | |
573 | q = e + 1; | |
574 | continue; | |
575 | } | |
576 | ||
577 | break; | |
578 | } | |
579 | ||
580 | f = startswith_no_case(q, "type="); | |
581 | if (f) { | |
582 | _cleanup_free_ char *t = NULL; | |
583 | const char *e; | |
584 | ||
585 | if (type != 0) { | |
586 | log_error("DNS type specified twice."); | |
587 | return -EINVAL; | |
588 | } | |
589 | ||
590 | e = strchrnul(f, ';'); | |
591 | t = strndup(f, e - f); | |
592 | if (!t) | |
593 | return log_oom(); | |
594 | ||
595 | r = dns_type_from_string(t); | |
596 | if (r < 0) { | |
597 | log_error("Unknown DNS type %s.", t); | |
598 | return -EINVAL; | |
599 | } | |
600 | ||
601 | type = r; | |
602 | ||
603 | if (*e == ';') { | |
604 | q = e + 1; | |
605 | continue; | |
606 | } | |
607 | ||
608 | break; | |
609 | } | |
610 | ||
611 | goto invalid; | |
612 | } | |
613 | } else | |
614 | n = p; | |
615 | ||
c1dafe4f | 616 | if (class == 0) |
4ac2ca1b ZJS |
617 | class = arg_class ?: DNS_CLASS_IN; |
618 | if (type == 0) | |
619 | type = arg_type ?: DNS_TYPE_A; | |
c1dafe4f LP |
620 | |
621 | return resolve_record(bus, n, class, type); | |
622 | ||
623 | invalid: | |
624 | log_error("Invalid DNS URI: %s", name); | |
625 | return -EINVAL; | |
626 | } | |
627 | ||
45ec7efb LP |
628 | static int resolve_service(sd_bus *bus, const char *name, const char *type, const char *domain) { |
629 | const char *canonical_name, *canonical_type, *canonical_domain; | |
4afd3348 LP |
630 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; |
631 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
45ec7efb LP |
632 | char ifname[IF_NAMESIZE] = ""; |
633 | size_t indent, sz; | |
634 | uint64_t flags; | |
635 | const char *p; | |
636 | unsigned c; | |
637 | usec_t ts; | |
638 | int r; | |
639 | ||
640 | assert(bus); | |
641 | assert(domain); | |
642 | ||
3c6f7c34 LP |
643 | name = empty_to_null(name); |
644 | type = empty_to_null(type); | |
45ec7efb LP |
645 | |
646 | if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname)) | |
647 | return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex); | |
648 | ||
649 | if (name) | |
650 | 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); | |
651 | else if (type) | |
652 | log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname); | |
653 | else | |
654 | log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname); | |
655 | ||
656 | r = sd_bus_message_new_method_call( | |
657 | bus, | |
658 | &req, | |
659 | "org.freedesktop.resolve1", | |
660 | "/org/freedesktop/resolve1", | |
661 | "org.freedesktop.resolve1.Manager", | |
662 | "ResolveService"); | |
663 | if (r < 0) | |
664 | return bus_log_create_error(r); | |
665 | ||
666 | r = sd_bus_message_append(req, "isssit", arg_ifindex, name, type, domain, arg_family, arg_flags); | |
667 | if (r < 0) | |
668 | return bus_log_create_error(r); | |
669 | ||
670 | ts = now(CLOCK_MONOTONIC); | |
671 | ||
672 | r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply); | |
673 | if (r < 0) | |
674 | return log_error_errno(r, "Resolve call failed: %s", bus_error_message(&error, r)); | |
675 | ||
676 | ts = now(CLOCK_MONOTONIC) - ts; | |
677 | ||
678 | r = sd_bus_message_enter_container(reply, 'a', "(qqqsa(iiay)s)"); | |
679 | if (r < 0) | |
680 | return bus_log_parse_error(r); | |
681 | ||
682 | indent = | |
683 | (name ? strlen(name) + 1 : 0) + | |
684 | (type ? strlen(type) + 1 : 0) + | |
685 | strlen(domain) + 2; | |
686 | ||
687 | c = 0; | |
688 | while ((r = sd_bus_message_enter_container(reply, 'r', "qqqsa(iiay)s")) > 0) { | |
689 | uint16_t priority, weight, port; | |
690 | const char *hostname, *canonical; | |
691 | ||
692 | r = sd_bus_message_read(reply, "qqqs", &priority, &weight, &port, &hostname); | |
693 | if (r < 0) | |
694 | return bus_log_parse_error(r); | |
695 | ||
696 | if (name) | |
697 | printf("%*s%s", (int) strlen(name), c == 0 ? name : "", c == 0 ? "/" : " "); | |
698 | if (type) | |
699 | printf("%*s%s", (int) strlen(type), c == 0 ? type : "", c == 0 ? "/" : " "); | |
700 | ||
701 | printf("%*s%s %s:%u [priority=%u, weight=%u]\n", | |
702 | (int) strlen(domain), c == 0 ? domain : "", | |
703 | c == 0 ? ":" : " ", | |
704 | hostname, port, | |
705 | priority, weight); | |
706 | ||
707 | r = sd_bus_message_enter_container(reply, 'a', "(iiay)"); | |
708 | if (r < 0) | |
709 | return bus_log_parse_error(r); | |
710 | ||
711 | while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) { | |
712 | _cleanup_free_ char *pretty = NULL; | |
713 | int ifindex, family; | |
714 | const void *a; | |
715 | ||
716 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
717 | ||
718 | r = sd_bus_message_read(reply, "ii", &ifindex, &family); | |
719 | if (r < 0) | |
720 | return bus_log_parse_error(r); | |
721 | ||
722 | r = sd_bus_message_read_array(reply, 'y', &a, &sz); | |
723 | if (r < 0) | |
724 | return bus_log_parse_error(r); | |
725 | ||
726 | r = sd_bus_message_exit_container(reply); | |
727 | if (r < 0) | |
728 | return bus_log_parse_error(r); | |
729 | ||
730 | if (!IN_SET(family, AF_INET, AF_INET6)) { | |
731 | log_debug("%s: skipping entry with family %d (%s)", name, family, af_to_name(family) ?: "unknown"); | |
732 | continue; | |
733 | } | |
734 | ||
735 | if (sz != FAMILY_ADDRESS_SIZE(family)) { | |
736 | log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown"); | |
737 | return -EINVAL; | |
738 | } | |
739 | ||
740 | ifname[0] = 0; | |
741 | if (ifindex > 0 && !if_indextoname(ifindex, ifname)) | |
742 | log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex); | |
743 | ||
744 | r = in_addr_to_string(family, a, &pretty); | |
745 | if (r < 0) | |
746 | return log_error_errno(r, "Failed to print address for %s: %m", name); | |
747 | ||
748 | printf("%*s%s%s%s\n", (int) indent, "", pretty, isempty(ifname) ? "" : "%s", ifname); | |
749 | } | |
750 | if (r < 0) | |
751 | return bus_log_parse_error(r); | |
752 | ||
753 | r = sd_bus_message_exit_container(reply); | |
754 | if (r < 0) | |
755 | return bus_log_parse_error(r); | |
756 | ||
757 | r = sd_bus_message_read(reply, "s", &canonical); | |
758 | if (r < 0) | |
759 | return bus_log_parse_error(r); | |
760 | ||
761 | if (!streq(hostname, canonical)) | |
762 | printf("%*s(%s)\n", (int) indent, "", canonical); | |
763 | ||
764 | r = sd_bus_message_exit_container(reply); | |
765 | if (r < 0) | |
766 | return bus_log_parse_error(r); | |
767 | ||
768 | c++; | |
769 | } | |
770 | if (r < 0) | |
771 | return bus_log_parse_error(r); | |
772 | ||
773 | r = sd_bus_message_exit_container(reply); | |
774 | if (r < 0) | |
775 | return bus_log_parse_error(r); | |
776 | ||
777 | r = sd_bus_message_enter_container(reply, 'a', "ay"); | |
778 | if (r < 0) | |
779 | return bus_log_parse_error(r); | |
780 | ||
781 | c = 0; | |
782 | while ((r = sd_bus_message_read_array(reply, 'y', (const void**) &p, &sz)) > 0) { | |
783 | _cleanup_free_ char *escaped = NULL; | |
784 | ||
785 | escaped = cescape_length(p, sz); | |
786 | if (!escaped) | |
787 | return log_oom(); | |
788 | ||
789 | printf("%*s%s\n", (int) indent, "", escaped); | |
790 | c++; | |
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 | ||
b68f10bf | 843 | r = string_hashsum_sha224(address, domain - 1 - address, &hashed); |
4ac2ca1b ZJS |
844 | if (r < 0) |
845 | return log_error_errno(r, "Hashing failed: %m"); | |
846 | ||
847 | full = strjoina(hashed, "._openpgpkey.", domain); | |
848 | log_debug("Looking up \"%s\".", full); | |
849 | ||
850 | return resolve_record(bus, full, | |
851 | arg_class ?: DNS_CLASS_IN, | |
852 | arg_type ?: DNS_TYPE_OPENPGPKEY); | |
853 | } | |
854 | ||
82d1d240 ZJS |
855 | static int resolve_tlsa(sd_bus *bus, const char *address) { |
856 | const char *port; | |
857 | uint16_t port_num = 443; | |
858 | _cleanup_free_ char *full = NULL; | |
859 | int r; | |
860 | ||
861 | assert(bus); | |
862 | assert(address); | |
863 | ||
864 | port = strrchr(address, ':'); | |
865 | if (port) { | |
866 | r = safe_atou16(port + 1, &port_num); | |
867 | if (r < 0 || port_num == 0) | |
868 | return log_error_errno(r, "Invalid port \"%s\".", port + 1); | |
869 | ||
870 | address = strndupa(address, port - address); | |
871 | } | |
872 | ||
873 | r = asprintf(&full, "_%u.%s.%s", | |
874 | port_num, | |
875 | service_family_to_string(arg_service_family), | |
876 | address); | |
877 | if (r < 0) | |
878 | return log_oom(); | |
879 | ||
880 | log_debug("Looking up \"%s\".", full); | |
881 | ||
882 | return resolve_record(bus, full, | |
883 | arg_class ?: DNS_CLASS_IN, | |
884 | arg_type ?: DNS_TYPE_TLSA); | |
885 | } | |
886 | ||
a150ff5e LP |
887 | static int show_statistics(sd_bus *bus) { |
888 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
889 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
890 | uint64_t n_current_transactions, n_total_transactions, | |
891 | cache_size, n_cache_hit, n_cache_miss, | |
892 | n_dnssec_secure, n_dnssec_insecure, n_dnssec_bogus, n_dnssec_indeterminate; | |
593f665c | 893 | int r, dnssec_supported; |
a150ff5e LP |
894 | |
895 | assert(bus); | |
896 | ||
593f665c LP |
897 | r = sd_bus_get_property_trivial(bus, |
898 | "org.freedesktop.resolve1", | |
899 | "/org/freedesktop/resolve1", | |
900 | "org.freedesktop.resolve1.Manager", | |
901 | "DNSSECSupported", | |
902 | &error, | |
903 | 'b', | |
904 | &dnssec_supported); | |
905 | if (r < 0) | |
906 | return log_error_errno(r, "Failed to get DNSSEC supported state: %s", bus_error_message(&error, r)); | |
907 | ||
908 | printf("DNSSEC supported by current servers: %s%s%s\n\n", | |
909 | ansi_highlight(), | |
910 | yes_no(dnssec_supported), | |
911 | ansi_normal()); | |
912 | ||
a150ff5e LP |
913 | r = sd_bus_get_property(bus, |
914 | "org.freedesktop.resolve1", | |
915 | "/org/freedesktop/resolve1", | |
916 | "org.freedesktop.resolve1.Manager", | |
917 | "TransactionStatistics", | |
918 | &error, | |
919 | &reply, | |
920 | "(tt)"); | |
921 | if (r < 0) | |
922 | return log_error_errno(r, "Failed to get transaction statistics: %s", bus_error_message(&error, r)); | |
923 | ||
924 | r = sd_bus_message_read(reply, "(tt)", | |
925 | &n_current_transactions, | |
926 | &n_total_transactions); | |
927 | if (r < 0) | |
928 | return bus_log_parse_error(r); | |
929 | ||
930 | printf("%sTransactions%s\n" | |
931 | "Current Transactions: %" PRIu64 "\n" | |
932 | " Total Transactions: %" PRIu64 "\n", | |
933 | ansi_highlight(), | |
934 | ansi_normal(), | |
935 | n_current_transactions, | |
936 | n_total_transactions); | |
937 | ||
938 | reply = sd_bus_message_unref(reply); | |
939 | ||
940 | r = sd_bus_get_property(bus, | |
941 | "org.freedesktop.resolve1", | |
942 | "/org/freedesktop/resolve1", | |
943 | "org.freedesktop.resolve1.Manager", | |
944 | "CacheStatistics", | |
945 | &error, | |
946 | &reply, | |
947 | "(ttt)"); | |
f7700834 TA |
948 | if (r < 0) |
949 | return log_error_errno(r, "Failed to get cache statistics: %s", bus_error_message(&error, r)); | |
a150ff5e LP |
950 | |
951 | r = sd_bus_message_read(reply, "(ttt)", | |
952 | &cache_size, | |
953 | &n_cache_hit, | |
954 | &n_cache_miss); | |
955 | if (r < 0) | |
956 | return bus_log_parse_error(r); | |
957 | ||
958 | printf("\n%sCache%s\n" | |
959 | " Current Cache Size: %" PRIu64 "\n" | |
960 | " Cache Hits: %" PRIu64 "\n" | |
961 | " Cache Misses: %" PRIu64 "\n", | |
962 | ansi_highlight(), | |
963 | ansi_normal(), | |
964 | cache_size, | |
965 | n_cache_hit, | |
966 | n_cache_miss); | |
967 | ||
968 | reply = sd_bus_message_unref(reply); | |
969 | ||
970 | r = sd_bus_get_property(bus, | |
971 | "org.freedesktop.resolve1", | |
972 | "/org/freedesktop/resolve1", | |
973 | "org.freedesktop.resolve1.Manager", | |
974 | "DNSSECStatistics", | |
975 | &error, | |
976 | &reply, | |
977 | "(tttt)"); | |
f7700834 TA |
978 | if (r < 0) |
979 | return log_error_errno(r, "Failed to get DNSSEC statistics: %s", bus_error_message(&error, r)); | |
a150ff5e LP |
980 | |
981 | r = sd_bus_message_read(reply, "(tttt)", | |
982 | &n_dnssec_secure, | |
983 | &n_dnssec_insecure, | |
984 | &n_dnssec_bogus, | |
985 | &n_dnssec_indeterminate); | |
986 | if (r < 0) | |
987 | return bus_log_parse_error(r); | |
988 | ||
7405bb3e LP |
989 | printf("\n%sDNSSEC Verdicts%s\n" |
990 | " Secure: %" PRIu64 "\n" | |
991 | " Insecure: %" PRIu64 "\n" | |
992 | " Bogus: %" PRIu64 "\n" | |
993 | " Indeterminate: %" PRIu64 "\n", | |
a150ff5e LP |
994 | ansi_highlight(), |
995 | ansi_normal(), | |
996 | n_dnssec_secure, | |
997 | n_dnssec_insecure, | |
998 | n_dnssec_bogus, | |
999 | n_dnssec_indeterminate); | |
1000 | ||
1001 | return 0; | |
1002 | } | |
1003 | ||
1004 | static int reset_statistics(sd_bus *bus) { | |
1005 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1006 | int r; | |
1007 | ||
1008 | r = sd_bus_call_method(bus, | |
1009 | "org.freedesktop.resolve1", | |
1010 | "/org/freedesktop/resolve1", | |
1011 | "org.freedesktop.resolve1.Manager", | |
1012 | "ResetStatistics", | |
1013 | &error, | |
1014 | NULL, | |
1015 | NULL); | |
1016 | if (r < 0) | |
1017 | return log_error_errno(r, "Failed to reset statistics: %s", bus_error_message(&error, r)); | |
1018 | ||
1019 | return 0; | |
1020 | } | |
1021 | ||
ba35662f LP |
1022 | static int flush_caches(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 | "FlushCaches", | |
1031 | &error, | |
1032 | NULL, | |
1033 | NULL); | |
1034 | if (r < 0) | |
1035 | return log_error_errno(r, "Failed to flush caches: %s", bus_error_message(&error, r)); | |
1036 | ||
1037 | return 0; | |
1038 | } | |
1039 | ||
be371fe0 LP |
1040 | static int map_link_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1041 | char ***l = userdata; | |
1042 | int r; | |
1043 | ||
1044 | assert(bus); | |
1045 | assert(member); | |
1046 | assert(m); | |
1047 | assert(l); | |
1048 | ||
1049 | r = sd_bus_message_enter_container(m, 'a', "(iay)"); | |
1050 | if (r < 0) | |
1051 | return r; | |
1052 | ||
1053 | for (;;) { | |
1054 | const void *a; | |
1055 | char *pretty; | |
1056 | int family; | |
1057 | size_t sz; | |
1058 | ||
1059 | r = sd_bus_message_enter_container(m, 'r', "iay"); | |
1060 | if (r < 0) | |
1061 | return r; | |
1062 | if (r == 0) | |
1063 | break; | |
1064 | ||
1065 | r = sd_bus_message_read(m, "i", &family); | |
1066 | if (r < 0) | |
1067 | return r; | |
1068 | ||
1069 | r = sd_bus_message_read_array(m, 'y', &a, &sz); | |
1070 | if (r < 0) | |
1071 | return r; | |
1072 | ||
1073 | r = sd_bus_message_exit_container(m); | |
1074 | if (r < 0) | |
1075 | return r; | |
1076 | ||
1077 | if (!IN_SET(family, AF_INET, AF_INET6)) { | |
1078 | log_debug("Unexpected family, ignoring."); | |
1079 | continue; | |
1080 | } | |
1081 | ||
1082 | if (sz != FAMILY_ADDRESS_SIZE(family)) { | |
1083 | log_debug("Address size mismatch, ignoring."); | |
1084 | continue; | |
1085 | } | |
1086 | ||
1087 | r = in_addr_to_string(family, a, &pretty); | |
1088 | if (r < 0) | |
1089 | return r; | |
1090 | ||
1091 | r = strv_consume(l, pretty); | |
1092 | if (r < 0) | |
1093 | return r; | |
1094 | } | |
1095 | ||
1096 | r = sd_bus_message_exit_container(m); | |
1097 | if (r < 0) | |
1098 | return r; | |
1099 | ||
1100 | return 0; | |
1101 | } | |
1102 | ||
1103 | static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1104 | char ***l = userdata; | |
1105 | int r; | |
1106 | ||
1107 | assert(bus); | |
1108 | assert(member); | |
1109 | assert(m); | |
1110 | assert(l); | |
1111 | ||
1112 | r = sd_bus_message_enter_container(m, 'a', "(sb)"); | |
1113 | if (r < 0) | |
1114 | return r; | |
1115 | ||
1116 | for (;;) { | |
1117 | const char *domain; | |
1118 | int route_only; | |
1119 | char *pretty; | |
1120 | ||
1121 | r = sd_bus_message_read(m, "(sb)", &domain, &route_only); | |
1122 | if (r < 0) | |
1123 | return r; | |
1124 | if (r == 0) | |
1125 | break; | |
1126 | ||
1127 | if (route_only) | |
1128 | pretty = strappend("~", domain); | |
1129 | else | |
1130 | pretty = strdup(domain); | |
1131 | if (!pretty) | |
1132 | return -ENOMEM; | |
1133 | ||
1134 | r = strv_consume(l, pretty); | |
1135 | if (r < 0) | |
1136 | return r; | |
1137 | } | |
1138 | ||
1139 | r = sd_bus_message_exit_container(m); | |
1140 | if (r < 0) | |
1141 | return r; | |
1142 | ||
1143 | return 0; | |
1144 | } | |
1145 | ||
1146 | static int status_ifindex(sd_bus *bus, int ifindex, const char *name, bool *empty_line) { | |
1147 | ||
1148 | struct link_info { | |
1149 | uint64_t scopes_mask; | |
1150 | char *llmnr; | |
1151 | char *mdns; | |
1152 | char *dnssec; | |
1153 | char **dns; | |
1154 | char **domains; | |
1155 | char **ntas; | |
1156 | int dnssec_supported; | |
1157 | } link_info = {}; | |
1158 | ||
1159 | static const struct bus_properties_map property_map[] = { | |
1160 | { "ScopesMask", "t", NULL, offsetof(struct link_info, scopes_mask) }, | |
1161 | { "DNS", "a(iay)", map_link_dns_servers, offsetof(struct link_info, dns) }, | |
1162 | { "Domains", "a(sb)", map_link_domains, offsetof(struct link_info, domains) }, | |
1163 | { "LLMNR", "s", NULL, offsetof(struct link_info, llmnr) }, | |
1164 | { "MulticastDNS", "s", NULL, offsetof(struct link_info, mdns) }, | |
1165 | { "DNSSEC", "s", NULL, offsetof(struct link_info, dnssec) }, | |
1166 | { "DNSSECNegativeTrustAnchors", "as", NULL, offsetof(struct link_info, ntas) }, | |
1167 | { "DNSSECSupported", "b", NULL, offsetof(struct link_info, dnssec_supported) }, | |
1168 | {} | |
1169 | }; | |
1170 | ||
1171 | _cleanup_free_ char *ifi = NULL, *p = NULL; | |
1172 | char ifname[IF_NAMESIZE] = ""; | |
1173 | char **i; | |
1174 | int r; | |
1175 | ||
1176 | assert(bus); | |
1177 | assert(ifindex > 0); | |
1178 | assert(empty_line); | |
1179 | ||
1180 | if (!name) { | |
1181 | if (!if_indextoname(ifindex, ifname)) | |
1182 | return log_error_errno(errno, "Failed to resolve interface name for %i: %m", ifindex); | |
1183 | ||
1184 | name = ifname; | |
1185 | } | |
1186 | ||
1187 | if (asprintf(&ifi, "%i", ifindex) < 0) | |
1188 | return log_oom(); | |
1189 | ||
1190 | r = sd_bus_path_encode("/org/freedesktop/resolve1/link", ifi, &p); | |
1191 | if (r < 0) | |
1192 | return log_oom(); | |
1193 | ||
1194 | r = bus_map_all_properties(bus, | |
1195 | "org.freedesktop.resolve1", | |
1196 | p, | |
1197 | property_map, | |
1198 | &link_info); | |
1199 | if (r < 0) { | |
1200 | log_error_errno(r, "Failed to get link data for %i: %m", ifindex); | |
1201 | goto finish; | |
1202 | } | |
1203 | ||
1204 | pager_open(arg_no_pager, false); | |
1205 | ||
1206 | if (*empty_line) | |
1207 | fputc('\n', stdout); | |
1208 | ||
1209 | printf("%sLink %i (%s)%s\n", | |
1210 | ansi_highlight(), ifindex, name, ansi_normal()); | |
1211 | ||
1212 | if (link_info.scopes_mask == 0) | |
1213 | printf(" Current Scopes: none\n"); | |
1214 | else | |
1215 | printf(" Current Scopes:%s%s%s%s%s\n", | |
1216 | link_info.scopes_mask & SD_RESOLVED_DNS ? " DNS" : "", | |
1217 | link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "", | |
1218 | link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "", | |
1219 | link_info.scopes_mask & SD_RESOLVED_MDNS_IPV4 ? " mDNS/IPv4" : "", | |
1220 | link_info.scopes_mask & SD_RESOLVED_MDNS_IPV6 ? " mDNS/IPv6" : ""); | |
1221 | ||
1222 | printf(" LLMNR setting: %s\n" | |
1223 | "MulticastDNS setting: %s\n" | |
1224 | " DNSSEC setting: %s\n" | |
1225 | " DNSSEC supported: %s\n", | |
1226 | strna(link_info.llmnr), | |
1227 | strna(link_info.mdns), | |
1228 | strna(link_info.dnssec), | |
1229 | yes_no(link_info.dnssec_supported)); | |
1230 | ||
1231 | STRV_FOREACH(i, link_info.dns) { | |
7b7c1aac ZJS |
1232 | printf(" %s %s\n", |
1233 | i == link_info.dns ? "DNS Servers:" : " ", | |
be371fe0 LP |
1234 | *i); |
1235 | } | |
1236 | ||
1237 | STRV_FOREACH(i, link_info.domains) { | |
1238 | printf(" %s %s\n", | |
1239 | i == link_info.domains ? "DNS Domain:" : " ", | |
1240 | *i); | |
1241 | } | |
1242 | ||
1243 | STRV_FOREACH(i, link_info.ntas) { | |
1244 | printf(" %s %s\n", | |
1245 | i == link_info.ntas ? "DNSSEC NTA:" : " ", | |
1246 | *i); | |
1247 | } | |
1248 | ||
1249 | *empty_line = true; | |
1250 | ||
1251 | r = 0; | |
1252 | ||
1253 | finish: | |
1254 | strv_free(link_info.dns); | |
1255 | strv_free(link_info.domains); | |
1256 | free(link_info.llmnr); | |
1257 | free(link_info.mdns); | |
1258 | free(link_info.dnssec); | |
1259 | strv_free(link_info.ntas); | |
1260 | return r; | |
1261 | } | |
1262 | ||
1263 | static int map_global_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1264 | char ***l = userdata; | |
1265 | int r; | |
1266 | ||
1267 | assert(bus); | |
1268 | assert(member); | |
1269 | assert(m); | |
1270 | assert(l); | |
1271 | ||
1272 | r = sd_bus_message_enter_container(m, 'a', "(iiay)"); | |
1273 | if (r < 0) | |
1274 | return r; | |
1275 | ||
1276 | for (;;) { | |
1277 | const void *a; | |
1278 | char *pretty; | |
1279 | int family, ifindex; | |
1280 | size_t sz; | |
1281 | ||
1282 | r = sd_bus_message_enter_container(m, 'r', "iiay"); | |
1283 | if (r < 0) | |
1284 | return r; | |
1285 | if (r == 0) | |
1286 | break; | |
1287 | ||
1288 | r = sd_bus_message_read(m, "ii", &ifindex, &family); | |
1289 | if (r < 0) | |
1290 | return r; | |
1291 | ||
1292 | r = sd_bus_message_read_array(m, 'y', &a, &sz); | |
1293 | if (r < 0) | |
1294 | return r; | |
1295 | ||
1296 | r = sd_bus_message_exit_container(m); | |
1297 | if (r < 0) | |
1298 | return r; | |
1299 | ||
1300 | if (ifindex != 0) /* only show the global ones here */ | |
1301 | continue; | |
1302 | ||
1303 | if (!IN_SET(family, AF_INET, AF_INET6)) { | |
1304 | log_debug("Unexpected family, ignoring."); | |
1305 | continue; | |
1306 | } | |
1307 | ||
1308 | if (sz != FAMILY_ADDRESS_SIZE(family)) { | |
1309 | log_debug("Address size mismatch, ignoring."); | |
1310 | continue; | |
1311 | } | |
1312 | ||
1313 | r = in_addr_to_string(family, a, &pretty); | |
1314 | if (r < 0) | |
1315 | return r; | |
1316 | ||
1317 | r = strv_consume(l, pretty); | |
1318 | if (r < 0) | |
1319 | return r; | |
1320 | } | |
1321 | ||
1322 | r = sd_bus_message_exit_container(m); | |
1323 | if (r < 0) | |
1324 | return r; | |
1325 | ||
1326 | return 0; | |
1327 | } | |
1328 | ||
1329 | static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1330 | char ***l = userdata; | |
1331 | int r; | |
1332 | ||
1333 | assert(bus); | |
1334 | assert(member); | |
1335 | assert(m); | |
1336 | assert(l); | |
1337 | ||
1338 | r = sd_bus_message_enter_container(m, 'a', "(isb)"); | |
1339 | if (r < 0) | |
1340 | return r; | |
1341 | ||
1342 | for (;;) { | |
1343 | const char *domain; | |
1344 | int route_only, ifindex; | |
1345 | char *pretty; | |
1346 | ||
1347 | r = sd_bus_message_read(m, "(isb)", &ifindex, &domain, &route_only); | |
1348 | if (r < 0) | |
1349 | return r; | |
1350 | if (r == 0) | |
1351 | break; | |
1352 | ||
1353 | if (ifindex != 0) /* only show the global ones here */ | |
1354 | continue; | |
1355 | ||
1356 | if (route_only) | |
1357 | pretty = strappend("~", domain); | |
1358 | else | |
1359 | pretty = strdup(domain); | |
1360 | if (!pretty) | |
1361 | return -ENOMEM; | |
1362 | ||
1363 | r = strv_consume(l, pretty); | |
1364 | if (r < 0) | |
1365 | return r; | |
1366 | } | |
1367 | ||
1368 | r = sd_bus_message_exit_container(m); | |
1369 | if (r < 0) | |
1370 | return r; | |
1371 | ||
1372 | return 0; | |
1373 | } | |
1374 | ||
1375 | static int status_global(sd_bus *bus, bool *empty_line) { | |
1376 | ||
1377 | struct global_info { | |
1378 | char **dns; | |
1379 | char **domains; | |
1380 | char **ntas; | |
1381 | } global_info = {}; | |
1382 | ||
1383 | static const struct bus_properties_map property_map[] = { | |
1384 | { "DNS", "a(iiay)", map_global_dns_servers, offsetof(struct global_info, dns) }, | |
1385 | { "Domains", "a(isb)", map_global_domains, offsetof(struct global_info, domains) }, | |
1386 | { "DNSSECNegativeTrustAnchors", "as", NULL, offsetof(struct global_info, ntas) }, | |
1387 | {} | |
1388 | }; | |
1389 | ||
1390 | char **i; | |
1391 | int r; | |
1392 | ||
1393 | assert(bus); | |
1394 | assert(empty_line); | |
1395 | ||
1396 | r = bus_map_all_properties(bus, | |
1397 | "org.freedesktop.resolve1", | |
1398 | "/org/freedesktop/resolve1", | |
1399 | property_map, | |
1400 | &global_info); | |
1401 | if (r < 0) { | |
1402 | log_error_errno(r, "Failed to get global data: %m"); | |
1403 | goto finish; | |
1404 | } | |
1405 | ||
1406 | if (strv_isempty(global_info.dns) && strv_isempty(global_info.domains) && strv_isempty(global_info.ntas)) { | |
1407 | r = 0; | |
1408 | goto finish; | |
1409 | } | |
1410 | ||
1411 | pager_open(arg_no_pager, false); | |
1412 | ||
1413 | printf("%sGlobal%s\n", ansi_highlight(), ansi_normal()); | |
1414 | STRV_FOREACH(i, global_info.dns) { | |
7b7c1aac ZJS |
1415 | printf(" %s %s\n", |
1416 | i == global_info.dns ? "DNS Servers:" : " ", | |
be371fe0 LP |
1417 | *i); |
1418 | } | |
1419 | ||
1420 | STRV_FOREACH(i, global_info.domains) { | |
1421 | printf(" %s %s\n", | |
1422 | i == global_info.domains ? "DNS Domain:" : " ", | |
1423 | *i); | |
1424 | } | |
1425 | ||
1426 | strv_sort(global_info.ntas); | |
1427 | STRV_FOREACH(i, global_info.ntas) { | |
1428 | printf(" %s %s\n", | |
1429 | i == global_info.ntas ? "DNSSEC NTA:" : " ", | |
1430 | *i); | |
1431 | } | |
1432 | ||
1433 | *empty_line = true; | |
1434 | ||
1435 | r = 0; | |
1436 | ||
1437 | finish: | |
1438 | strv_free(global_info.dns); | |
1439 | strv_free(global_info.domains); | |
1440 | strv_free(global_info.ntas); | |
1441 | ||
1442 | return r; | |
1443 | } | |
1444 | ||
1445 | static int status_all(sd_bus *bus) { | |
1446 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
1447 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
1448 | sd_netlink_message *i; | |
96ace31d | 1449 | bool empty_line = false; |
be371fe0 LP |
1450 | int r; |
1451 | ||
1452 | assert(bus); | |
1453 | ||
1454 | r = status_global(bus, &empty_line); | |
1455 | if (r < 0) | |
1456 | return r; | |
1457 | ||
1458 | r = sd_netlink_open(&rtnl); | |
1459 | if (r < 0) | |
1460 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
1461 | ||
1462 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0); | |
1463 | if (r < 0) | |
1464 | return rtnl_log_create_error(r); | |
1465 | ||
1466 | r = sd_netlink_message_request_dump(req, true); | |
1467 | if (r < 0) | |
1468 | return rtnl_log_create_error(r); | |
1469 | ||
1470 | r = sd_netlink_call(rtnl, req, 0, &reply); | |
1471 | if (r < 0) | |
1472 | return log_error_errno(r, "Failed to enumerate links: %m"); | |
1473 | ||
1474 | r = 0; | |
1475 | for (i = reply; i; i = sd_netlink_message_next(i)) { | |
1476 | const char *name; | |
1477 | int ifindex, q; | |
1478 | uint16_t type; | |
1479 | ||
1480 | q = sd_netlink_message_get_type(i, &type); | |
1481 | if (q < 0) | |
1482 | return rtnl_log_parse_error(q); | |
1483 | ||
1484 | if (type != RTM_NEWLINK) | |
1485 | continue; | |
1486 | ||
1487 | q = sd_rtnl_message_link_get_ifindex(i, &ifindex); | |
1488 | if (q < 0) | |
1489 | return rtnl_log_parse_error(q); | |
1490 | ||
1491 | if (ifindex == LOOPBACK_IFINDEX) | |
1492 | continue; | |
1493 | ||
1494 | q = sd_netlink_message_read_string(i, IFLA_IFNAME, &name); | |
1495 | if (q < 0) | |
1496 | return rtnl_log_parse_error(q); | |
1497 | ||
1498 | q = status_ifindex(bus, ifindex, name, &empty_line); | |
1499 | if (q < 0 && r >= 0) | |
1500 | r = q; | |
1501 | } | |
1502 | ||
1503 | return r; | |
1504 | } | |
1505 | ||
ba82da3b ZJS |
1506 | static void help_protocol_types(void) { |
1507 | if (arg_legend) | |
1508 | puts("Known protocol types:"); | |
1509 | puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6"); | |
1510 | } | |
1511 | ||
b93312f5 | 1512 | static void help_dns_types(void) { |
b93312f5 | 1513 | const char *t; |
be371fe0 | 1514 | int i; |
b93312f5 ZJS |
1515 | |
1516 | if (arg_legend) | |
09b1fe14 | 1517 | puts("Known DNS RR types:"); |
b93312f5 ZJS |
1518 | for (i = 0; i < _DNS_TYPE_MAX; i++) { |
1519 | t = dns_type_to_string(i); | |
1520 | if (t) | |
1521 | puts(t); | |
1522 | } | |
1523 | } | |
1524 | ||
1525 | static void help_dns_classes(void) { | |
b93312f5 | 1526 | const char *t; |
be371fe0 | 1527 | int i; |
b93312f5 ZJS |
1528 | |
1529 | if (arg_legend) | |
09b1fe14 | 1530 | puts("Known DNS RR classes:"); |
b93312f5 ZJS |
1531 | for (i = 0; i < _DNS_CLASS_MAX; i++) { |
1532 | t = dns_class_to_string(i); | |
1533 | if (t) | |
1534 | puts(t); | |
1535 | } | |
1536 | } | |
1537 | ||
bdef7319 | 1538 | static void help(void) { |
1ace2438 ZJS |
1539 | printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n" |
1540 | "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n" | |
1541 | "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n" | |
1542 | "%1$s [OPTIONS...] --statistics\n" | |
1543 | "%1$s [OPTIONS...] --reset-statistics\n" | |
1544 | "\n" | |
300a716d | 1545 | "Resolve domain names, IPv4 and IPv6 addresses, DNS resource records, and services.\n\n" |
1ace2438 ZJS |
1546 | " -h --help Show this help\n" |
1547 | " --version Show package version\n" | |
be371fe0 | 1548 | " --no-pager Do not pipe output into a pager\n" |
1ace2438 ZJS |
1549 | " -4 Resolve IPv4 addresses\n" |
1550 | " -6 Resolve IPv6 addresses\n" | |
1551 | " -i --interface=INTERFACE Look on interface\n" | |
1552 | " -p --protocol=PROTO|help Look via protocol\n" | |
1553 | " -t --type=TYPE|help Query RR with DNS type\n" | |
1554 | " -c --class=CLASS|help Query RR with DNS class\n" | |
1555 | " --service Resolve service (SRV)\n" | |
1556 | " --service-address=BOOL Resolve address for services (default: yes)\n" | |
1557 | " --service-txt=BOOL Resolve TXT records for services (default: yes)\n" | |
1558 | " --openpgp Query OpenPGP public key\n" | |
82d1d240 | 1559 | " --tlsa Query TLS public key\n" |
1ace2438 ZJS |
1560 | " --cname=BOOL Follow CNAME redirects (default: yes)\n" |
1561 | " --search=BOOL Use search domains for single-label names\n" | |
1562 | " (default: yes)\n" | |
dab48ea6 | 1563 | " --raw[=payload|packet] Dump the answer as binary data\n" |
1ace2438 ZJS |
1564 | " --legend=BOOL Print headers and additional info (default: yes)\n" |
1565 | " --statistics Show resolver statistics\n" | |
1566 | " --reset-statistics Reset resolver statistics\n" | |
be371fe0 | 1567 | " --status Show link and server status\n" |
ba35662f | 1568 | " --flush-caches Flush all local DNS caches\n" |
1ace2438 | 1569 | , program_invocation_short_name); |
bdef7319 ZJS |
1570 | } |
1571 | ||
1572 | static int parse_argv(int argc, char *argv[]) { | |
1573 | enum { | |
1574 | ARG_VERSION = 0x100, | |
dad29dff | 1575 | ARG_LEGEND, |
45ec7efb LP |
1576 | ARG_SERVICE, |
1577 | ARG_CNAME, | |
1578 | ARG_SERVICE_ADDRESS, | |
1579 | ARG_SERVICE_TXT, | |
4ac2ca1b | 1580 | ARG_OPENPGP, |
82d1d240 | 1581 | ARG_TLSA, |
2e74028a | 1582 | ARG_RAW, |
801ad6a6 | 1583 | ARG_SEARCH, |
a150ff5e LP |
1584 | ARG_STATISTICS, |
1585 | ARG_RESET_STATISTICS, | |
be371fe0 | 1586 | ARG_STATUS, |
ba35662f | 1587 | ARG_FLUSH_CACHES, |
be371fe0 | 1588 | ARG_NO_PAGER, |
bdef7319 ZJS |
1589 | }; |
1590 | ||
1591 | static const struct option options[] = { | |
a150ff5e LP |
1592 | { "help", no_argument, NULL, 'h' }, |
1593 | { "version", no_argument, NULL, ARG_VERSION }, | |
1594 | { "type", required_argument, NULL, 't' }, | |
1595 | { "class", required_argument, NULL, 'c' }, | |
1596 | { "legend", required_argument, NULL, ARG_LEGEND }, | |
5b911843 | 1597 | { "interface", required_argument, NULL, 'i' }, |
a150ff5e LP |
1598 | { "protocol", required_argument, NULL, 'p' }, |
1599 | { "cname", required_argument, NULL, ARG_CNAME }, | |
1600 | { "service", no_argument, NULL, ARG_SERVICE }, | |
1601 | { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS }, | |
1602 | { "service-txt", required_argument, NULL, ARG_SERVICE_TXT }, | |
4ac2ca1b | 1603 | { "openpgp", no_argument, NULL, ARG_OPENPGP }, |
82d1d240 | 1604 | { "tlsa", optional_argument, NULL, ARG_TLSA }, |
dab48ea6 | 1605 | { "raw", optional_argument, NULL, ARG_RAW }, |
a150ff5e LP |
1606 | { "search", required_argument, NULL, ARG_SEARCH }, |
1607 | { "statistics", no_argument, NULL, ARG_STATISTICS, }, | |
1608 | { "reset-statistics", no_argument, NULL, ARG_RESET_STATISTICS }, | |
be371fe0 | 1609 | { "status", no_argument, NULL, ARG_STATUS }, |
ba35662f | 1610 | { "flush-caches", no_argument, NULL, ARG_FLUSH_CACHES }, |
be371fe0 | 1611 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
bdef7319 ZJS |
1612 | {} |
1613 | }; | |
1614 | ||
2d4c5cbc | 1615 | int c, r; |
bdef7319 ZJS |
1616 | |
1617 | assert(argc >= 0); | |
1618 | assert(argv); | |
1619 | ||
51323288 | 1620 | while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0) |
bdef7319 ZJS |
1621 | switch(c) { |
1622 | ||
1623 | case 'h': | |
1624 | help(); | |
1625 | return 0; /* done */; | |
1626 | ||
1627 | case ARG_VERSION: | |
3f6fd1ba | 1628 | return version(); |
bdef7319 ZJS |
1629 | |
1630 | case '4': | |
1631 | arg_family = AF_INET; | |
1632 | break; | |
1633 | ||
1634 | case '6': | |
1635 | arg_family = AF_INET6; | |
1636 | break; | |
1637 | ||
3fa4999b LP |
1638 | case 'i': { |
1639 | int ifi; | |
1640 | ||
6ad623a3 | 1641 | if (parse_ifindex(optarg, &ifi) >= 0) |
3fa4999b LP |
1642 | arg_ifindex = ifi; |
1643 | else { | |
1644 | ifi = if_nametoindex(optarg); | |
1645 | if (ifi <= 0) | |
1646 | return log_error_errno(errno, "Unknown interface %s: %m", optarg); | |
1647 | ||
1648 | arg_ifindex = ifi; | |
1649 | } | |
1650 | ||
2d4c5cbc | 1651 | break; |
3fa4999b | 1652 | } |
2d4c5cbc LP |
1653 | |
1654 | case 't': | |
b93312f5 ZJS |
1655 | if (streq(optarg, "help")) { |
1656 | help_dns_types(); | |
1657 | return 0; | |
1658 | } | |
1659 | ||
4b548ef3 LP |
1660 | r = dns_type_from_string(optarg); |
1661 | if (r < 0) { | |
2d4c5cbc | 1662 | log_error("Failed to parse RR record type %s", optarg); |
4b548ef3 | 1663 | return r; |
2d4c5cbc | 1664 | } |
4b548ef3 LP |
1665 | arg_type = (uint16_t) r; |
1666 | assert((int) arg_type == r); | |
b93312f5 | 1667 | |
a150ff5e | 1668 | arg_mode = MODE_RESOLVE_RECORD; |
2d4c5cbc LP |
1669 | break; |
1670 | ||
1671 | case 'c': | |
b93312f5 ZJS |
1672 | if (streq(optarg, "help")) { |
1673 | help_dns_classes(); | |
1674 | return 0; | |
1675 | } | |
1676 | ||
4b548ef3 | 1677 | r = dns_class_from_string(optarg); |
2d4c5cbc LP |
1678 | if (r < 0) { |
1679 | log_error("Failed to parse RR record class %s", optarg); | |
1680 | return r; | |
bdef7319 | 1681 | } |
4b548ef3 LP |
1682 | arg_class = (uint16_t) r; |
1683 | assert((int) arg_class == r); | |
b93312f5 ZJS |
1684 | |
1685 | break; | |
1686 | ||
dad29dff | 1687 | case ARG_LEGEND: |
45ec7efb LP |
1688 | r = parse_boolean(optarg); |
1689 | if (r < 0) | |
1690 | return log_error_errno(r, "Failed to parse --legend= argument"); | |
1691 | ||
1692 | arg_legend = r; | |
bdef7319 ZJS |
1693 | break; |
1694 | ||
51323288 | 1695 | case 'p': |
ba82da3b ZJS |
1696 | if (streq(optarg, "help")) { |
1697 | help_protocol_types(); | |
1698 | return 0; | |
1699 | } else if (streq(optarg, "dns")) | |
51323288 LP |
1700 | arg_flags |= SD_RESOLVED_DNS; |
1701 | else if (streq(optarg, "llmnr")) | |
1702 | arg_flags |= SD_RESOLVED_LLMNR; | |
1703 | else if (streq(optarg, "llmnr-ipv4")) | |
1704 | arg_flags |= SD_RESOLVED_LLMNR_IPV4; | |
1705 | else if (streq(optarg, "llmnr-ipv6")) | |
1706 | arg_flags |= SD_RESOLVED_LLMNR_IPV6; | |
1707 | else { | |
1708 | log_error("Unknown protocol specifier: %s", optarg); | |
1709 | return -EINVAL; | |
1710 | } | |
1711 | ||
1712 | break; | |
1713 | ||
45ec7efb | 1714 | case ARG_SERVICE: |
a150ff5e | 1715 | arg_mode = MODE_RESOLVE_SERVICE; |
45ec7efb LP |
1716 | break; |
1717 | ||
4ac2ca1b ZJS |
1718 | case ARG_OPENPGP: |
1719 | arg_mode = MODE_RESOLVE_OPENPGP; | |
1720 | break; | |
1721 | ||
82d1d240 ZJS |
1722 | case ARG_TLSA: |
1723 | arg_mode = MODE_RESOLVE_TLSA; | |
1724 | arg_service_family = service_family_from_string(optarg); | |
1725 | if (arg_service_family < 0) { | |
1726 | log_error("Unknown service family \"%s\".", optarg); | |
1727 | return -EINVAL; | |
1728 | } | |
1729 | break; | |
1730 | ||
2e74028a ZJS |
1731 | case ARG_RAW: |
1732 | if (on_tty()) { | |
1733 | log_error("Refusing to write binary data to tty."); | |
1734 | return -ENOTTY; | |
1735 | } | |
1736 | ||
dab48ea6 ZJS |
1737 | if (optarg == NULL || streq(optarg, "payload")) |
1738 | arg_raw = RAW_PAYLOAD; | |
1739 | else if (streq(optarg, "packet")) | |
1740 | arg_raw = RAW_PACKET; | |
1741 | else { | |
1742 | log_error("Unknown --raw specifier \"%s\".", optarg); | |
1743 | return -EINVAL; | |
1744 | } | |
1745 | ||
2e74028a ZJS |
1746 | arg_legend = false; |
1747 | break; | |
1748 | ||
45ec7efb LP |
1749 | case ARG_CNAME: |
1750 | r = parse_boolean(optarg); | |
1751 | if (r < 0) | |
1752 | return log_error_errno(r, "Failed to parse --cname= argument."); | |
5883ff60 | 1753 | SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0); |
45ec7efb LP |
1754 | break; |
1755 | ||
1756 | case ARG_SERVICE_ADDRESS: | |
1757 | r = parse_boolean(optarg); | |
1758 | if (r < 0) | |
1759 | return log_error_errno(r, "Failed to parse --service-address= argument."); | |
5883ff60 | 1760 | SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0); |
45ec7efb LP |
1761 | break; |
1762 | ||
1763 | case ARG_SERVICE_TXT: | |
1764 | r = parse_boolean(optarg); | |
1765 | if (r < 0) | |
1766 | return log_error_errno(r, "Failed to parse --service-txt= argument."); | |
5883ff60 | 1767 | SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0); |
45ec7efb LP |
1768 | break; |
1769 | ||
801ad6a6 LP |
1770 | case ARG_SEARCH: |
1771 | r = parse_boolean(optarg); | |
1772 | if (r < 0) | |
1773 | return log_error_errno(r, "Failed to parse --search argument."); | |
5883ff60 | 1774 | SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0); |
801ad6a6 LP |
1775 | break; |
1776 | ||
a150ff5e LP |
1777 | case ARG_STATISTICS: |
1778 | arg_mode = MODE_STATISTICS; | |
1779 | break; | |
1780 | ||
1781 | case ARG_RESET_STATISTICS: | |
1782 | arg_mode = MODE_RESET_STATISTICS; | |
1783 | break; | |
1784 | ||
ba35662f LP |
1785 | case ARG_FLUSH_CACHES: |
1786 | arg_mode = MODE_FLUSH_CACHES; | |
1787 | break; | |
1788 | ||
be371fe0 LP |
1789 | case ARG_STATUS: |
1790 | arg_mode = MODE_STATUS; | |
1791 | break; | |
1792 | ||
1793 | case ARG_NO_PAGER: | |
1794 | arg_no_pager = true; | |
1795 | break; | |
1796 | ||
bdef7319 ZJS |
1797 | case '?': |
1798 | return -EINVAL; | |
1799 | ||
1800 | default: | |
1801 | assert_not_reached("Unhandled option"); | |
1802 | } | |
1803 | ||
2d4c5cbc | 1804 | if (arg_type == 0 && arg_class != 0) { |
45ec7efb LP |
1805 | log_error("--class= may only be used in conjunction with --type=."); |
1806 | return -EINVAL; | |
1807 | } | |
1808 | ||
82d1d240 | 1809 | if (arg_type != 0 && arg_mode == MODE_RESOLVE_SERVICE) { |
45ec7efb | 1810 | log_error("--service and --type= may not be combined."); |
2d4c5cbc LP |
1811 | return -EINVAL; |
1812 | } | |
1813 | ||
1814 | if (arg_type != 0 && arg_class == 0) | |
1815 | arg_class = DNS_CLASS_IN; | |
1816 | ||
c1dafe4f LP |
1817 | if (arg_class != 0 && arg_type == 0) |
1818 | arg_type = DNS_TYPE_A; | |
1819 | ||
bdef7319 ZJS |
1820 | return 1 /* work to do */; |
1821 | } | |
1822 | ||
bdef7319 | 1823 | int main(int argc, char **argv) { |
4afd3348 | 1824 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
bdef7319 ZJS |
1825 | int r; |
1826 | ||
1827 | log_parse_environment(); | |
1828 | log_open(); | |
1829 | ||
1830 | r = parse_argv(argc, argv); | |
1831 | if (r <= 0) | |
79266746 LP |
1832 | goto finish; |
1833 | ||
bdef7319 ZJS |
1834 | r = sd_bus_open_system(&bus); |
1835 | if (r < 0) { | |
da927ba9 | 1836 | log_error_errno(r, "sd_bus_open_system: %m"); |
79266746 | 1837 | goto finish; |
bdef7319 ZJS |
1838 | } |
1839 | ||
a150ff5e | 1840 | switch (arg_mode) { |
45ec7efb | 1841 | |
a150ff5e LP |
1842 | case MODE_RESOLVE_HOST: |
1843 | if (optind >= argc) { | |
300a716d | 1844 | log_error("No arguments passed."); |
a150ff5e LP |
1845 | r = -EINVAL; |
1846 | goto finish; | |
1847 | } | |
1848 | ||
1849 | while (argv[optind]) { | |
1850 | int family, ifindex, k; | |
1851 | union in_addr_union a; | |
1852 | ||
c1dafe4f LP |
1853 | if (startswith(argv[optind], "dns:")) |
1854 | k = resolve_rfc4501(bus, argv[optind]); | |
1855 | else { | |
145fab1e | 1856 | k = in_addr_ifindex_from_string_auto(argv[optind], &family, &a, &ifindex); |
c1dafe4f LP |
1857 | if (k >= 0) |
1858 | k = resolve_address(bus, family, &a, ifindex); | |
1859 | else | |
1860 | k = resolve_host(bus, argv[optind]); | |
1861 | } | |
a150ff5e LP |
1862 | |
1863 | if (r == 0) | |
1864 | r = k; | |
1865 | ||
1866 | optind++; | |
1867 | } | |
1868 | break; | |
1869 | ||
1870 | case MODE_RESOLVE_RECORD: | |
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 k; | |
1879 | ||
c1dafe4f | 1880 | k = resolve_record(bus, argv[optind], arg_class, arg_type); |
a150ff5e LP |
1881 | if (r == 0) |
1882 | r = k; | |
1883 | ||
1884 | optind++; | |
1885 | } | |
1886 | break; | |
1887 | ||
1888 | case MODE_RESOLVE_SERVICE: | |
45ec7efb LP |
1889 | if (argc < optind + 1) { |
1890 | log_error("Domain specification required."); | |
1891 | r = -EINVAL; | |
1892 | goto finish; | |
1893 | ||
1894 | } else if (argc == optind + 1) | |
1895 | r = resolve_service(bus, NULL, NULL, argv[optind]); | |
1896 | else if (argc == optind + 2) | |
1897 | r = resolve_service(bus, NULL, argv[optind], argv[optind+1]); | |
1898 | else if (argc == optind + 3) | |
1899 | r = resolve_service(bus, argv[optind], argv[optind+1], argv[optind+2]); | |
1900 | else { | |
300a716d | 1901 | log_error("Too many arguments."); |
45ec7efb LP |
1902 | r = -EINVAL; |
1903 | goto finish; | |
1904 | } | |
1905 | ||
a150ff5e | 1906 | break; |
79266746 | 1907 | |
4ac2ca1b ZJS |
1908 | case MODE_RESOLVE_OPENPGP: |
1909 | if (argc < optind + 1) { | |
1910 | log_error("E-mail address required."); | |
1911 | r = -EINVAL; | |
1912 | goto finish; | |
1913 | ||
1914 | } | |
1915 | ||
1916 | r = 0; | |
1917 | while (optind < argc) { | |
1918 | int k; | |
1919 | ||
1920 | k = resolve_openpgp(bus, argv[optind++]); | |
1921 | if (k < 0) | |
1922 | r = k; | |
1923 | } | |
1924 | break; | |
1925 | ||
82d1d240 ZJS |
1926 | case MODE_RESOLVE_TLSA: |
1927 | if (argc < optind + 1) { | |
1928 | log_error("Domain name required."); | |
1929 | r = -EINVAL; | |
1930 | goto finish; | |
1931 | ||
1932 | } | |
1933 | ||
1934 | r = 0; | |
1935 | while (optind < argc) { | |
1936 | int k; | |
1937 | ||
1938 | k = resolve_tlsa(bus, argv[optind++]); | |
1939 | if (k < 0) | |
1940 | r = k; | |
1941 | } | |
1942 | break; | |
1943 | ||
a150ff5e LP |
1944 | case MODE_STATISTICS: |
1945 | if (argc > optind) { | |
1946 | log_error("Too many arguments."); | |
1947 | r = -EINVAL; | |
1948 | goto finish; | |
2d4c5cbc | 1949 | } |
bdef7319 | 1950 | |
a150ff5e LP |
1951 | r = show_statistics(bus); |
1952 | break; | |
1953 | ||
1954 | case MODE_RESET_STATISTICS: | |
1955 | if (argc > optind) { | |
1956 | log_error("Too many arguments."); | |
1957 | r = -EINVAL; | |
1958 | goto finish; | |
1959 | } | |
79266746 | 1960 | |
a150ff5e LP |
1961 | r = reset_statistics(bus); |
1962 | break; | |
ba35662f LP |
1963 | |
1964 | case MODE_FLUSH_CACHES: | |
1965 | if (argc > optind) { | |
1966 | log_error("Too many arguments."); | |
1967 | r = -EINVAL; | |
1968 | goto finish; | |
1969 | } | |
1970 | ||
1971 | r = flush_caches(bus); | |
1972 | break; | |
be371fe0 LP |
1973 | |
1974 | case MODE_STATUS: | |
1975 | ||
1976 | if (argc > optind) { | |
1977 | char **ifname; | |
1978 | bool empty_line = false; | |
1979 | ||
1980 | r = 0; | |
1981 | STRV_FOREACH(ifname, argv + optind) { | |
1982 | int ifindex, q; | |
1983 | ||
1984 | q = parse_ifindex(argv[optind], &ifindex); | |
1985 | if (q < 0) { | |
1986 | ifindex = if_nametoindex(argv[optind]); | |
1987 | if (ifindex <= 0) { | |
1988 | log_error_errno(errno, "Failed to resolve interface name: %s", argv[optind]); | |
1989 | continue; | |
1990 | } | |
1991 | } | |
1992 | ||
1993 | q = status_ifindex(bus, ifindex, NULL, &empty_line); | |
1994 | if (q < 0 && r >= 0) | |
1995 | r = q; | |
1996 | } | |
1997 | } else | |
1998 | r = status_all(bus); | |
1999 | ||
2000 | break; | |
bdef7319 ZJS |
2001 | } |
2002 | ||
79266746 | 2003 | finish: |
be371fe0 LP |
2004 | pager_close(); |
2005 | ||
bdef7319 ZJS |
2006 | return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
2007 | } |