]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <getopt.h> | |
4 | #include <locale.h> | |
5 | #include <net/if.h> | |
6 | ||
7 | #include "sd-bus.h" | |
8 | #include "sd-daemon.h" | |
9 | #include "sd-event.h" | |
10 | #include "sd-json.h" | |
11 | #include "sd-netlink.h" | |
12 | #include "sd-varlink.h" | |
13 | ||
14 | #include "af-list.h" | |
15 | #include "alloc-util.h" | |
16 | #include "argv-util.h" | |
17 | #include "build.h" | |
18 | #include "bus-common-errors.h" | |
19 | #include "bus-error.h" | |
20 | #include "bus-locator.h" | |
21 | #include "bus-map-properties.h" | |
22 | #include "bus-message-util.h" | |
23 | #include "bus-util.h" | |
24 | #include "dns-domain.h" | |
25 | #include "errno-list.h" | |
26 | #include "errno-util.h" | |
27 | #include "escape.h" | |
28 | #include "format-ifname.h" | |
29 | #include "format-table.h" | |
30 | #include "hostname-util.h" | |
31 | #include "json-util.h" | |
32 | #include "main-func.h" | |
33 | #include "missing_network.h" | |
34 | #include "netlink-util.h" | |
35 | #include "openssl-util.h" | |
36 | #include "pager.h" | |
37 | #include "parse-argument.h" | |
38 | #include "parse-util.h" | |
39 | #include "polkit-agent.h" | |
40 | #include "pretty-print.h" | |
41 | #include "resolvconf-compat.h" | |
42 | #include "resolve-util.h" | |
43 | #include "resolvectl.h" | |
44 | #include "resolved-def.h" | |
45 | #include "resolved-dns-packet.h" | |
46 | #include "resolved-dns-rr.h" | |
47 | #include "resolved-util.h" | |
48 | #include "socket-netlink.h" | |
49 | #include "sort-util.h" | |
50 | #include "stdio-util.h" | |
51 | #include "string-table.h" | |
52 | #include "string-util.h" | |
53 | #include "strv.h" | |
54 | #include "terminal-util.h" | |
55 | #include "time-util.h" | |
56 | #include "utf8.h" | |
57 | #include "varlink-util.h" | |
58 | #include "verb-log-control.h" | |
59 | #include "verbs.h" | |
60 | ||
61 | static int arg_family = AF_UNSPEC; | |
62 | static int arg_ifindex = 0; | |
63 | static char *arg_ifname = NULL; | |
64 | static uint16_t arg_type = 0; | |
65 | static uint16_t arg_class = 0; | |
66 | static bool arg_legend = true; | |
67 | static uint64_t arg_flags = 0; | |
68 | static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; | |
69 | static PagerFlags arg_pager_flags = 0; | |
70 | bool arg_ifindex_permissive = false; /* If true, don't generate an error if the specified interface index doesn't exist */ | |
71 | static const char *arg_service_family = NULL; | |
72 | static bool arg_ask_password = true; | |
73 | ||
74 | typedef enum RawType { | |
75 | RAW_NONE, | |
76 | RAW_PAYLOAD, | |
77 | RAW_PACKET, | |
78 | } RawType; | |
79 | static RawType arg_raw = RAW_NONE; | |
80 | ||
81 | /* Used by compat interfaces: systemd-resolve and resolvconf. */ | |
82 | ExecutionMode arg_mode = MODE_RESOLVE_HOST; | |
83 | char **arg_set_dns = NULL; | |
84 | char **arg_set_domain = NULL; | |
85 | bool arg_disable_default_route = false; | |
86 | static const char *arg_set_llmnr = NULL; | |
87 | static const char *arg_set_mdns = NULL; | |
88 | static const char *arg_set_dns_over_tls = NULL; | |
89 | static const char *arg_set_dnssec = NULL; | |
90 | static char **arg_set_nta = NULL; | |
91 | ||
92 | STATIC_DESTRUCTOR_REGISTER(arg_ifname, freep); | |
93 | STATIC_DESTRUCTOR_REGISTER(arg_set_dns, strv_freep); | |
94 | STATIC_DESTRUCTOR_REGISTER(arg_set_domain, strv_freep); | |
95 | STATIC_DESTRUCTOR_REGISTER(arg_set_nta, strv_freep); | |
96 | ||
97 | typedef enum StatusMode { | |
98 | STATUS_ALL, | |
99 | STATUS_DNS, | |
100 | STATUS_DOMAIN, | |
101 | STATUS_DEFAULT_ROUTE, | |
102 | STATUS_LLMNR, | |
103 | STATUS_MDNS, | |
104 | STATUS_PRIVATE, | |
105 | STATUS_DNSSEC, | |
106 | STATUS_NTA, | |
107 | } StatusMode; | |
108 | ||
109 | typedef struct InterfaceInfo { | |
110 | int index; | |
111 | const char *name; | |
112 | } InterfaceInfo; | |
113 | ||
114 | static int acquire_bus(sd_bus **ret) { | |
115 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; | |
116 | int r; | |
117 | ||
118 | assert(ret); | |
119 | ||
120 | r = sd_bus_open_system(&bus); | |
121 | if (r < 0) | |
122 | return log_error_errno(r, "sd_bus_open_system: %m"); | |
123 | ||
124 | (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password); | |
125 | ||
126 | *ret = TAKE_PTR(bus); | |
127 | return 0; | |
128 | } | |
129 | ||
130 | static int interface_info_compare(const InterfaceInfo *a, const InterfaceInfo *b) { | |
131 | int r; | |
132 | ||
133 | r = CMP(a->index, b->index); | |
134 | if (r != 0) | |
135 | return r; | |
136 | ||
137 | return strcmp_ptr(a->name, b->name); | |
138 | } | |
139 | ||
140 | int ifname_mangle_full(const char *s, bool drop_protocol_specifier) { | |
141 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
142 | _cleanup_strv_free_ char **found = NULL; | |
143 | int r; | |
144 | ||
145 | assert(s); | |
146 | ||
147 | if (drop_protocol_specifier) { | |
148 | _cleanup_free_ char *buf = NULL; | |
149 | int ifindex_longest_name = -ENODEV; | |
150 | ||
151 | /* When invoked as resolvconf, drop the protocol specifier(s) at the end. */ | |
152 | ||
153 | buf = strdup(s); | |
154 | if (!buf) | |
155 | return log_oom(); | |
156 | ||
157 | for (;;) { | |
158 | r = rtnl_resolve_interface(&rtnl, buf); | |
159 | if (r > 0) { | |
160 | if (ifindex_longest_name <= 0) | |
161 | ifindex_longest_name = r; | |
162 | ||
163 | r = strv_extend(&found, buf); | |
164 | if (r < 0) | |
165 | return log_oom(); | |
166 | } | |
167 | ||
168 | char *dot = strrchr(buf, '.'); | |
169 | if (!dot) | |
170 | break; | |
171 | ||
172 | *dot = '\0'; | |
173 | } | |
174 | ||
175 | unsigned n = strv_length(found); | |
176 | if (n > 1) { | |
177 | _cleanup_free_ char *joined = NULL; | |
178 | ||
179 | joined = strv_join(found, ", "); | |
180 | log_warning("Found multiple interfaces (%s) matching with '%s'. Using '%s' (ifindex=%i).", | |
181 | strna(joined), s, found[0], ifindex_longest_name); | |
182 | ||
183 | } else if (n == 1) { | |
184 | const char *proto; | |
185 | ||
186 | proto = ASSERT_PTR(startswith(s, found[0])); | |
187 | if (!isempty(proto)) | |
188 | log_info("Dropped protocol specifier '%s' from '%s'. Using '%s' (ifindex=%i).", | |
189 | proto, s, found[0], ifindex_longest_name); | |
190 | } | |
191 | ||
192 | r = ifindex_longest_name; | |
193 | } else | |
194 | r = rtnl_resolve_interface(&rtnl, s); | |
195 | if (r < 0) { | |
196 | if (ERRNO_IS_DEVICE_ABSENT(r) && arg_ifindex_permissive) { | |
197 | log_debug_errno(r, "Interface '%s' not found, but -f specified, ignoring: %m", s); | |
198 | return 0; /* done */ | |
199 | } | |
200 | return log_error_errno(r, "Failed to resolve interface \"%s\": %m", s); | |
201 | } | |
202 | ||
203 | if (arg_ifindex > 0 && arg_ifindex != r) | |
204 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified multiple different interfaces. Refusing."); | |
205 | ||
206 | arg_ifindex = r; | |
207 | return free_and_strdup_warn(&arg_ifname, found ? found[0] : s); /* found */ | |
208 | } | |
209 | ||
210 | static void print_source(uint64_t flags, usec_t rtt) { | |
211 | if (!arg_legend) | |
212 | return; | |
213 | ||
214 | if (sd_json_format_enabled(arg_json_format_flags)) | |
215 | return; | |
216 | ||
217 | if (flags == 0) | |
218 | return; | |
219 | ||
220 | printf("\n%s-- Information acquired via", ansi_grey()); | |
221 | ||
222 | printf(" protocol%s%s%s%s%s", | |
223 | flags & SD_RESOLVED_DNS ? " DNS" :"", | |
224 | flags & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "", | |
225 | flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "", | |
226 | flags & SD_RESOLVED_MDNS_IPV4 ? " mDNS/IPv4" : "", | |
227 | flags & SD_RESOLVED_MDNS_IPV6 ? " mDNS/IPv6" : ""); | |
228 | ||
229 | printf(" in %s.%s\n" | |
230 | "%s-- Data is authenticated: %s; Data was acquired via local or encrypted transport: %s%s\n", | |
231 | FORMAT_TIMESPAN(rtt, 100), | |
232 | ansi_normal(), | |
233 | ansi_grey(), | |
234 | yes_no(flags & SD_RESOLVED_AUTHENTICATED), | |
235 | yes_no(flags & SD_RESOLVED_CONFIDENTIAL), | |
236 | ansi_normal()); | |
237 | ||
238 | if ((flags & (SD_RESOLVED_FROM_MASK|SD_RESOLVED_SYNTHETIC)) != 0) | |
239 | printf("%s-- Data from:%s%s%s%s%s%s\n", | |
240 | ansi_grey(), | |
241 | FLAGS_SET(flags, SD_RESOLVED_SYNTHETIC) ? " synthetic" : "", | |
242 | FLAGS_SET(flags, SD_RESOLVED_FROM_CACHE) ? " cache" : "", | |
243 | FLAGS_SET(flags, SD_RESOLVED_FROM_ZONE) ? " zone" : "", | |
244 | FLAGS_SET(flags, SD_RESOLVED_FROM_TRUST_ANCHOR) ? " trust-anchor" : "", | |
245 | FLAGS_SET(flags, SD_RESOLVED_FROM_NETWORK) ? " network" : "", | |
246 | ansi_normal()); | |
247 | } | |
248 | ||
249 | static void print_ifindex_comment(int printed_so_far, int ifindex) { | |
250 | char ifname[IF_NAMESIZE]; | |
251 | int r; | |
252 | ||
253 | if (ifindex <= 0) | |
254 | return; | |
255 | ||
256 | r = format_ifname(ifindex, ifname); | |
257 | if (r < 0) | |
258 | return (void) log_warning_errno(r, "Failed to resolve interface name for index %i, ignoring: %m", ifindex); | |
259 | ||
260 | printf("%*s%s-- link: %s%s", | |
261 | 60 > printed_so_far ? 60 - printed_so_far : 0, " ", /* Align comment to the 60th column */ | |
262 | ansi_grey(), ifname, ansi_normal()); | |
263 | } | |
264 | ||
265 | static int resolve_host_error(const char *name, int r, const sd_bus_error *error) { | |
266 | if (sd_bus_error_has_name(error, BUS_ERROR_DNS_NXDOMAIN)) | |
267 | return log_error_errno(r, "%s: %s", name, bus_error_message(error, r)); | |
268 | ||
269 | return log_error_errno(r, "%s: resolve call failed: %s", name, bus_error_message(error, r)); | |
270 | } | |
271 | ||
272 | static int resolve_host(sd_bus *bus, const char *name) { | |
273 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; | |
274 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
275 | const char *canonical = NULL; | |
276 | unsigned c = 0; | |
277 | uint64_t flags; | |
278 | usec_t ts; | |
279 | int r; | |
280 | ||
281 | assert(name); | |
282 | ||
283 | if (sd_json_format_enabled(arg_json_format_flags)) | |
284 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type=A or --type=AAAA to acquire address record information in JSON format."); | |
285 | ||
286 | log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); | |
287 | ||
288 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveHostname"); | |
289 | if (r < 0) | |
290 | return bus_log_create_error(r); | |
291 | ||
292 | r = sd_bus_message_append(req, "isit", arg_ifindex, name, arg_family, arg_flags); | |
293 | if (r < 0) | |
294 | return bus_log_create_error(r); | |
295 | ||
296 | ts = now(CLOCK_MONOTONIC); | |
297 | ||
298 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); | |
299 | if (r < 0) | |
300 | return resolve_host_error(name, r, &error); | |
301 | ||
302 | ts = now(CLOCK_MONOTONIC) - ts; | |
303 | ||
304 | r = sd_bus_message_enter_container(reply, 'a', "(iiay)"); | |
305 | if (r < 0) | |
306 | return bus_log_parse_error(r); | |
307 | ||
308 | while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) { | |
309 | _cleanup_free_ char *pretty = NULL; | |
310 | int ifindex, family, k; | |
311 | union in_addr_union a; | |
312 | ||
313 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
314 | ||
315 | r = sd_bus_message_read(reply, "i", &ifindex); | |
316 | if (r < 0) | |
317 | return bus_log_parse_error(r); | |
318 | ||
319 | sd_bus_error_free(&error); | |
320 | r = bus_message_read_in_addr_auto(reply, &error, &family, &a); | |
321 | if (r < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) | |
322 | return log_error_errno(r, "%s: systemd-resolved returned invalid result: %s", name, bus_error_message(&error, r)); | |
323 | ||
324 | r = sd_bus_message_exit_container(reply); | |
325 | if (r < 0) | |
326 | return bus_log_parse_error(r); | |
327 | ||
328 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) { | |
329 | log_debug_errno(r, "%s: systemd-resolved returned invalid result, ignoring: %s", name, bus_error_message(&error, r)); | |
330 | continue; | |
331 | } | |
332 | ||
333 | r = in_addr_ifindex_to_string(family, &a, ifindex, &pretty); | |
334 | if (r < 0) | |
335 | return log_error_errno(r, "Failed to print address for %s: %m", name); | |
336 | ||
337 | k = printf("%*s%s %s%s%s", | |
338 | (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ", | |
339 | ansi_highlight(), pretty, ansi_normal()); | |
340 | ||
341 | print_ifindex_comment(k, ifindex); | |
342 | fputc('\n', stdout); | |
343 | ||
344 | c++; | |
345 | } | |
346 | if (r < 0) | |
347 | return bus_log_parse_error(r); | |
348 | ||
349 | r = sd_bus_message_exit_container(reply); | |
350 | if (r < 0) | |
351 | return bus_log_parse_error(r); | |
352 | ||
353 | r = sd_bus_message_read(reply, "st", &canonical, &flags); | |
354 | if (r < 0) | |
355 | return bus_log_parse_error(r); | |
356 | ||
357 | if (!streq(name, canonical)) | |
358 | printf("%*s%s (%s)\n", | |
359 | (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ", | |
360 | canonical); | |
361 | ||
362 | if (c == 0) | |
363 | return log_error_errno(SYNTHETIC_ERRNO(ESRCH), | |
364 | "%s: no addresses found", name); | |
365 | ||
366 | print_source(flags, ts); | |
367 | ||
368 | return 0; | |
369 | } | |
370 | ||
371 | static int resolve_address(sd_bus *bus, int family, const union in_addr_union *address, int ifindex) { | |
372 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; | |
373 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
374 | _cleanup_free_ char *pretty = NULL; | |
375 | uint64_t flags; | |
376 | unsigned c = 0; | |
377 | usec_t ts; | |
378 | int r; | |
379 | ||
380 | assert(bus); | |
381 | assert(IN_SET(family, AF_INET, AF_INET6)); | |
382 | assert(address); | |
383 | ||
384 | if (sd_json_format_enabled(arg_json_format_flags)) | |
385 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); | |
386 | ||
387 | if (ifindex <= 0) | |
388 | ifindex = arg_ifindex; | |
389 | ||
390 | r = in_addr_ifindex_to_string(family, address, ifindex, &pretty); | |
391 | if (r < 0) | |
392 | return log_oom(); | |
393 | ||
394 | log_debug("Resolving %s.", pretty); | |
395 | ||
396 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveAddress"); | |
397 | if (r < 0) | |
398 | return bus_log_create_error(r); | |
399 | ||
400 | r = sd_bus_message_append(req, "ii", ifindex, family); | |
401 | if (r < 0) | |
402 | return bus_log_create_error(r); | |
403 | ||
404 | r = sd_bus_message_append_array(req, 'y', address, FAMILY_ADDRESS_SIZE(family)); | |
405 | if (r < 0) | |
406 | return bus_log_create_error(r); | |
407 | ||
408 | r = sd_bus_message_append(req, "t", arg_flags); | |
409 | if (r < 0) | |
410 | return bus_log_create_error(r); | |
411 | ||
412 | ts = now(CLOCK_MONOTONIC); | |
413 | ||
414 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); | |
415 | if (r < 0) | |
416 | return log_error_errno(r, "%s: resolve call failed: %s", pretty, bus_error_message(&error, r)); | |
417 | ||
418 | ts = now(CLOCK_MONOTONIC) - ts; | |
419 | ||
420 | r = sd_bus_message_enter_container(reply, 'a', "(is)"); | |
421 | if (r < 0) | |
422 | return bus_log_create_error(r); | |
423 | ||
424 | while ((r = sd_bus_message_enter_container(reply, 'r', "is")) > 0) { | |
425 | const char *n; | |
426 | int k; | |
427 | ||
428 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
429 | ||
430 | r = sd_bus_message_read(reply, "is", &ifindex, &n); | |
431 | if (r < 0) | |
432 | return r; | |
433 | ||
434 | r = sd_bus_message_exit_container(reply); | |
435 | if (r < 0) | |
436 | return r; | |
437 | ||
438 | k = printf("%*s%s %s%s%s", | |
439 | (int) strlen(pretty), c == 0 ? pretty : "", | |
440 | c == 0 ? ":" : " ", | |
441 | ansi_highlight(), n, ansi_normal()); | |
442 | ||
443 | print_ifindex_comment(k, ifindex); | |
444 | fputc('\n', stdout); | |
445 | ||
446 | c++; | |
447 | } | |
448 | if (r < 0) | |
449 | return bus_log_parse_error(r); | |
450 | ||
451 | r = sd_bus_message_exit_container(reply); | |
452 | if (r < 0) | |
453 | return bus_log_parse_error(r); | |
454 | ||
455 | r = sd_bus_message_read(reply, "t", &flags); | |
456 | if (r < 0) | |
457 | return bus_log_parse_error(r); | |
458 | ||
459 | if (c == 0) | |
460 | return log_error_errno(SYNTHETIC_ERRNO(ESRCH), | |
461 | "%s: no names found", pretty); | |
462 | ||
463 | print_source(flags, ts); | |
464 | ||
465 | return 0; | |
466 | } | |
467 | ||
468 | static int output_rr_packet(const void *d, size_t l, int ifindex) { | |
469 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
470 | int r; | |
471 | ||
472 | assert(d || l == 0); | |
473 | ||
474 | r = dns_resource_record_new_from_raw(&rr, d, l); | |
475 | if (r < 0) | |
476 | return log_error_errno(r, "Failed to parse RR: %m"); | |
477 | ||
478 | if (sd_json_format_enabled(arg_json_format_flags)) { | |
479 | _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; | |
480 | r = dns_resource_record_to_json(rr, &j); | |
481 | if (r < 0) | |
482 | return log_error_errno(r, "Failed to convert RR to JSON: %m"); | |
483 | ||
484 | if (!j) | |
485 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "JSON formatting for records of type %s (%u) not available.", dns_type_to_string(rr->key->type), rr->key->type); | |
486 | ||
487 | r = sd_json_variant_dump(j, arg_json_format_flags, NULL, NULL); | |
488 | if (r < 0) | |
489 | return r; | |
490 | ||
491 | } else if (arg_raw == RAW_PAYLOAD) { | |
492 | const void *data; | |
493 | ssize_t k; | |
494 | ||
495 | k = dns_resource_record_payload(rr, &data); | |
496 | if (k == -EINVAL) | |
497 | return log_error_errno(k, "Dumping of binary payload not available for RRs of this type: %s", dns_type_to_string(rr->key->type)); | |
498 | if (k < 0) | |
499 | return log_error_errno(k, "Cannot dump RR: %m"); | |
500 | fwrite(data, 1, k, stdout); | |
501 | } else { | |
502 | const char *s; | |
503 | int k; | |
504 | ||
505 | s = dns_resource_record_to_string(rr); | |
506 | if (!s) | |
507 | return log_oom(); | |
508 | ||
509 | k = printf("%s", s); | |
510 | print_ifindex_comment(k, ifindex); | |
511 | fputc('\n', stdout); | |
512 | } | |
513 | ||
514 | return 0; | |
515 | } | |
516 | ||
517 | static int idna_candidate(const char *name, char **ret) { | |
518 | _cleanup_free_ char *idnafied = NULL; | |
519 | int r; | |
520 | ||
521 | assert(name); | |
522 | assert(ret); | |
523 | ||
524 | r = dns_name_apply_idna(name, &idnafied); | |
525 | if (r < 0) | |
526 | return log_error_errno(r, "Failed to apply IDNA to name '%s': %m", name); | |
527 | if (r > 0 && !streq(name, idnafied)) { | |
528 | *ret = TAKE_PTR(idnafied); | |
529 | return true; | |
530 | } | |
531 | ||
532 | *ret = NULL; | |
533 | return false; | |
534 | } | |
535 | ||
536 | static bool single_label_nonsynthetic(const char *name) { | |
537 | _cleanup_free_ char *first_label = NULL; | |
538 | int r; | |
539 | ||
540 | if (!dns_name_is_single_label(name)) | |
541 | return false; | |
542 | ||
543 | if (is_localhost(name) || | |
544 | is_gateway_hostname(name) || | |
545 | is_outbound_hostname(name) || | |
546 | is_dns_stub_hostname(name) || | |
547 | is_dns_proxy_stub_hostname(name)) | |
548 | return false; | |
549 | ||
550 | r = resolve_system_hostname(NULL, &first_label); | |
551 | if (r < 0) { | |
552 | log_warning_errno(r, "Failed to determine the hostname: %m"); | |
553 | return false; | |
554 | } | |
555 | ||
556 | return !streq(name, first_label); | |
557 | } | |
558 | ||
559 | static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type, bool warn_missing) { | |
560 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; | |
561 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
562 | _cleanup_free_ char *idnafied = NULL; | |
563 | bool needs_authentication = false; | |
564 | unsigned n = 0; | |
565 | uint64_t flags; | |
566 | usec_t ts; | |
567 | int r; | |
568 | ||
569 | assert(name); | |
570 | ||
571 | log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(class), dns_type_to_string(type), isempty(arg_ifname) ? "*" : arg_ifname); | |
572 | ||
573 | if (dns_name_dot_suffixed(name) == 0 && single_label_nonsynthetic(name)) | |
574 | log_notice("(Note that search domains are not appended when --type= is specified. " | |
575 | "Please specify fully qualified domain names, or remove --type= switch from invocation in order to request regular hostname resolution.)"); | |
576 | ||
577 | r = idna_candidate(name, &idnafied); | |
578 | if (r < 0) | |
579 | return r; | |
580 | if (r > 0) | |
581 | log_notice("(Note that IDNA translation is not applied when --type= is specified. " | |
582 | "Please specify translated domain names — i.e. '%s' — when resolving raw records, or remove --type= switch from invocation in order to request regular hostname resolution.", | |
583 | idnafied); | |
584 | ||
585 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveRecord"); | |
586 | if (r < 0) | |
587 | return bus_log_create_error(r); | |
588 | ||
589 | r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, class, type, arg_flags); | |
590 | if (r < 0) | |
591 | return bus_log_create_error(r); | |
592 | ||
593 | ts = now(CLOCK_MONOTONIC); | |
594 | ||
595 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); | |
596 | if (r < 0) { | |
597 | if (warn_missing || r != -ENXIO) | |
598 | log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r)); | |
599 | return r; | |
600 | } | |
601 | ||
602 | ts = now(CLOCK_MONOTONIC) - ts; | |
603 | ||
604 | r = sd_bus_message_enter_container(reply, 'a', "(iqqay)"); | |
605 | if (r < 0) | |
606 | return bus_log_parse_error(r); | |
607 | ||
608 | while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) { | |
609 | uint16_t c, t; | |
610 | int ifindex; | |
611 | const void *d; | |
612 | size_t l; | |
613 | ||
614 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
615 | ||
616 | r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t); | |
617 | if (r < 0) | |
618 | return bus_log_parse_error(r); | |
619 | ||
620 | r = sd_bus_message_read_array(reply, 'y', &d, &l); | |
621 | if (r < 0) | |
622 | return bus_log_parse_error(r); | |
623 | ||
624 | r = sd_bus_message_exit_container(reply); | |
625 | if (r < 0) | |
626 | return bus_log_parse_error(r); | |
627 | ||
628 | if (arg_raw == RAW_PACKET) { | |
629 | uint64_t u64 = htole64(l); | |
630 | ||
631 | fwrite(&u64, sizeof(u64), 1, stdout); | |
632 | fwrite(d, 1, l, stdout); | |
633 | } else { | |
634 | r = output_rr_packet(d, l, ifindex); | |
635 | if (r < 0) | |
636 | return r; | |
637 | } | |
638 | ||
639 | if (dns_type_needs_authentication(t)) | |
640 | needs_authentication = true; | |
641 | ||
642 | n++; | |
643 | } | |
644 | if (r < 0) | |
645 | return bus_log_parse_error(r); | |
646 | ||
647 | r = sd_bus_message_exit_container(reply); | |
648 | if (r < 0) | |
649 | return bus_log_parse_error(r); | |
650 | ||
651 | r = sd_bus_message_read(reply, "t", &flags); | |
652 | if (r < 0) | |
653 | return bus_log_parse_error(r); | |
654 | ||
655 | if (n == 0) { | |
656 | if (warn_missing) | |
657 | log_error("%s: no records found", name); | |
658 | return -ESRCH; | |
659 | } | |
660 | ||
661 | print_source(flags, ts); | |
662 | ||
663 | if ((flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) { | |
664 | fflush(stdout); | |
665 | ||
666 | fprintf(stderr, "\n%s" | |
667 | "WARNING: The resources shown contain cryptographic key data which could not be\n" | |
668 | " authenticated. It is not suitable to authenticate any communication.\n" | |
669 | " This is usually indication that DNSSEC authentication was not enabled\n" | |
670 | " or is not available for the selected protocol or DNS servers.%s\n", | |
671 | ansi_highlight_red(), | |
672 | ansi_normal()); | |
673 | } | |
674 | ||
675 | return 0; | |
676 | } | |
677 | ||
678 | static int resolve_rfc4501(sd_bus *bus, const char *name) { | |
679 | uint16_t type = 0, class = 0; | |
680 | const char *p, *q, *n; | |
681 | int r; | |
682 | ||
683 | assert(bus); | |
684 | assert(name); | |
685 | assert(startswith(name, "dns:")); | |
686 | ||
687 | /* Parse RFC 4501 dns: URIs */ | |
688 | ||
689 | p = name + 4; | |
690 | ||
691 | if (p[0] == '/') { | |
692 | const char *e; | |
693 | ||
694 | if (p[1] != '/') | |
695 | goto invalid; | |
696 | ||
697 | e = strchr(p + 2, '/'); | |
698 | if (!e) | |
699 | goto invalid; | |
700 | ||
701 | if (e != p + 2) | |
702 | log_warning("DNS authority specification not supported; ignoring specified authority."); | |
703 | ||
704 | p = e + 1; | |
705 | } | |
706 | ||
707 | q = strchr(p, '?'); | |
708 | if (q) { | |
709 | n = strndupa_safe(p, q - p); | |
710 | q++; | |
711 | ||
712 | for (;;) { | |
713 | const char *f; | |
714 | ||
715 | f = startswith_no_case(q, "class="); | |
716 | if (f) { | |
717 | _cleanup_free_ char *t = NULL; | |
718 | const char *e; | |
719 | ||
720 | if (class != 0) | |
721 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
722 | "DNS class specified twice."); | |
723 | ||
724 | e = strchrnul(f, ';'); | |
725 | t = strndup(f, e - f); | |
726 | if (!t) | |
727 | return log_oom(); | |
728 | ||
729 | r = dns_class_from_string(t); | |
730 | if (r < 0) | |
731 | return log_error_errno(r, "Unknown DNS class %s.", t); | |
732 | ||
733 | class = r; | |
734 | ||
735 | if (*e == ';') { | |
736 | q = e + 1; | |
737 | continue; | |
738 | } | |
739 | ||
740 | break; | |
741 | } | |
742 | ||
743 | f = startswith_no_case(q, "type="); | |
744 | if (f) { | |
745 | _cleanup_free_ char *t = NULL; | |
746 | const char *e; | |
747 | ||
748 | if (type != 0) | |
749 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
750 | "DNS type specified twice."); | |
751 | ||
752 | e = strchrnul(f, ';'); | |
753 | t = strndup(f, e - f); | |
754 | if (!t) | |
755 | return log_oom(); | |
756 | ||
757 | r = dns_type_from_string(t); | |
758 | if (r < 0) | |
759 | return log_error_errno(r, "Unknown DNS type %s: %m", t); | |
760 | ||
761 | type = r; | |
762 | ||
763 | if (*e == ';') { | |
764 | q = e + 1; | |
765 | continue; | |
766 | } | |
767 | ||
768 | break; | |
769 | } | |
770 | ||
771 | goto invalid; | |
772 | } | |
773 | } else | |
774 | n = p; | |
775 | ||
776 | if (class == 0) | |
777 | class = arg_class ?: DNS_CLASS_IN; | |
778 | if (type == 0) | |
779 | type = arg_type ?: DNS_TYPE_A; | |
780 | ||
781 | return resolve_record(bus, n, class, type, true); | |
782 | ||
783 | invalid: | |
784 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
785 | "Invalid DNS URI: %s", name); | |
786 | } | |
787 | ||
788 | static int verb_query(int argc, char **argv, void *userdata) { | |
789 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
790 | int ret = 0, r; | |
791 | ||
792 | r = acquire_bus(&bus); | |
793 | if (r < 0) | |
794 | return r; | |
795 | ||
796 | if (arg_type != 0) | |
797 | STRV_FOREACH(p, strv_skip(argv, 1)) | |
798 | RET_GATHER(ret, resolve_record(bus, *p, arg_class, arg_type, true)); | |
799 | ||
800 | else | |
801 | STRV_FOREACH(p, strv_skip(argv, 1)) { | |
802 | if (startswith(*p, "dns:")) | |
803 | RET_GATHER(ret, resolve_rfc4501(bus, *p)); | |
804 | else { | |
805 | int family, ifindex; | |
806 | union in_addr_union a; | |
807 | ||
808 | r = in_addr_ifindex_from_string_auto(*p, &family, &a, &ifindex); | |
809 | if (r >= 0) | |
810 | RET_GATHER(ret, resolve_address(bus, family, &a, ifindex)); | |
811 | else | |
812 | RET_GATHER(ret, resolve_host(bus, *p)); | |
813 | } | |
814 | } | |
815 | ||
816 | return ret; | |
817 | } | |
818 | ||
819 | static int resolve_service(sd_bus *bus, const char *name, const char *type, const char *domain) { | |
820 | const char *canonical_name, *canonical_type, *canonical_domain; | |
821 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; | |
822 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
823 | size_t indent, sz; | |
824 | uint64_t flags; | |
825 | const char *p; | |
826 | unsigned c; | |
827 | usec_t ts; | |
828 | int r; | |
829 | ||
830 | assert(bus); | |
831 | assert(domain); | |
832 | ||
833 | name = empty_to_null(name); | |
834 | type = empty_to_null(type); | |
835 | ||
836 | if (name) | |
837 | log_debug("Resolving service \"%s\" of type %s in %s (family %s, interface %s).", name, type, domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); | |
838 | else if (type) | |
839 | log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); | |
840 | else | |
841 | log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); | |
842 | ||
843 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveService"); | |
844 | if (r < 0) | |
845 | return bus_log_create_error(r); | |
846 | ||
847 | r = sd_bus_message_append(req, "isssit", arg_ifindex, name, type, domain, arg_family, arg_flags); | |
848 | if (r < 0) | |
849 | return bus_log_create_error(r); | |
850 | ||
851 | ts = now(CLOCK_MONOTONIC); | |
852 | ||
853 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); | |
854 | if (r < 0) | |
855 | return log_error_errno(r, "Resolve call failed: %s", bus_error_message(&error, r)); | |
856 | ||
857 | ts = now(CLOCK_MONOTONIC) - ts; | |
858 | ||
859 | r = sd_bus_message_enter_container(reply, 'a', "(qqqsa(iiay)s)"); | |
860 | if (r < 0) | |
861 | return bus_log_parse_error(r); | |
862 | ||
863 | indent = | |
864 | (name ? strlen(name) + 1 : 0) + | |
865 | (type ? strlen(type) + 1 : 0) + | |
866 | strlen(domain) + 2; | |
867 | ||
868 | c = 0; | |
869 | while ((r = sd_bus_message_enter_container(reply, 'r', "qqqsa(iiay)s")) > 0) { | |
870 | uint16_t priority, weight, port; | |
871 | const char *hostname, *canonical; | |
872 | ||
873 | r = sd_bus_message_read(reply, "qqqs", &priority, &weight, &port, &hostname); | |
874 | if (r < 0) | |
875 | return bus_log_parse_error(r); | |
876 | ||
877 | if (name) | |
878 | printf("%*s%s", (int) strlen(name), c == 0 ? name : "", c == 0 ? "/" : " "); | |
879 | if (type) | |
880 | printf("%*s%s", (int) strlen(type), c == 0 ? type : "", c == 0 ? "/" : " "); | |
881 | ||
882 | printf("%*s%s %s:%u [priority=%u, weight=%u]\n", | |
883 | (int) strlen(domain), c == 0 ? domain : "", | |
884 | c == 0 ? ":" : " ", | |
885 | hostname, port, | |
886 | priority, weight); | |
887 | ||
888 | r = sd_bus_message_enter_container(reply, 'a', "(iiay)"); | |
889 | if (r < 0) | |
890 | return bus_log_parse_error(r); | |
891 | ||
892 | while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) { | |
893 | _cleanup_free_ char *pretty = NULL; | |
894 | int ifindex, family, k; | |
895 | union in_addr_union a; | |
896 | ||
897 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
898 | ||
899 | r = sd_bus_message_read(reply, "i", &ifindex); | |
900 | if (r < 0) | |
901 | return bus_log_parse_error(r); | |
902 | ||
903 | sd_bus_error_free(&error); | |
904 | r = bus_message_read_in_addr_auto(reply, &error, &family, &a); | |
905 | if (r < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) | |
906 | return log_error_errno(r, "%s: systemd-resolved returned invalid result: %s", name, bus_error_message(&error, r)); | |
907 | ||
908 | r = sd_bus_message_exit_container(reply); | |
909 | if (r < 0) | |
910 | return bus_log_parse_error(r); | |
911 | ||
912 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) { | |
913 | log_debug_errno(r, "%s: systemd-resolved returned invalid result, ignoring: %s", name, bus_error_message(&error, r)); | |
914 | continue; | |
915 | } | |
916 | ||
917 | r = in_addr_ifindex_to_string(family, &a, ifindex, &pretty); | |
918 | if (r < 0) | |
919 | return log_error_errno(r, "Failed to print address for %s: %m", name); | |
920 | ||
921 | k = printf("%*s%s", (int) indent, "", pretty); | |
922 | print_ifindex_comment(k, ifindex); | |
923 | fputc('\n', stdout); | |
924 | } | |
925 | if (r < 0) | |
926 | return bus_log_parse_error(r); | |
927 | ||
928 | r = sd_bus_message_exit_container(reply); | |
929 | if (r < 0) | |
930 | return bus_log_parse_error(r); | |
931 | ||
932 | r = sd_bus_message_read(reply, "s", &canonical); | |
933 | if (r < 0) | |
934 | return bus_log_parse_error(r); | |
935 | ||
936 | if (!streq(hostname, canonical)) | |
937 | printf("%*s(%s)\n", (int) indent, "", canonical); | |
938 | ||
939 | r = sd_bus_message_exit_container(reply); | |
940 | if (r < 0) | |
941 | return bus_log_parse_error(r); | |
942 | ||
943 | c++; | |
944 | } | |
945 | if (r < 0) | |
946 | return bus_log_parse_error(r); | |
947 | ||
948 | r = sd_bus_message_exit_container(reply); | |
949 | if (r < 0) | |
950 | return bus_log_parse_error(r); | |
951 | ||
952 | r = sd_bus_message_enter_container(reply, 'a', "ay"); | |
953 | if (r < 0) | |
954 | return bus_log_parse_error(r); | |
955 | ||
956 | while ((r = sd_bus_message_read_array(reply, 'y', (const void**) &p, &sz)) > 0) { | |
957 | _cleanup_free_ char *escaped = NULL; | |
958 | ||
959 | escaped = cescape_length(p, sz); | |
960 | if (!escaped) | |
961 | return log_oom(); | |
962 | ||
963 | printf("%*s%s\n", (int) indent, "", escaped); | |
964 | } | |
965 | if (r < 0) | |
966 | return bus_log_parse_error(r); | |
967 | ||
968 | r = sd_bus_message_exit_container(reply); | |
969 | if (r < 0) | |
970 | return bus_log_parse_error(r); | |
971 | ||
972 | r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags); | |
973 | if (r < 0) | |
974 | return bus_log_parse_error(r); | |
975 | ||
976 | canonical_name = empty_to_null(canonical_name); | |
977 | canonical_type = empty_to_null(canonical_type); | |
978 | ||
979 | if (!streq_ptr(name, canonical_name) || | |
980 | !streq_ptr(type, canonical_type) || | |
981 | !streq_ptr(domain, canonical_domain)) { | |
982 | ||
983 | printf("%*s(", (int) indent, ""); | |
984 | ||
985 | if (canonical_name) | |
986 | printf("%s/", canonical_name); | |
987 | if (canonical_type) | |
988 | printf("%s/", canonical_type); | |
989 | ||
990 | printf("%s)\n", canonical_domain); | |
991 | } | |
992 | ||
993 | print_source(flags, ts); | |
994 | ||
995 | return 0; | |
996 | } | |
997 | ||
998 | static int verb_service(int argc, char **argv, void *userdata) { | |
999 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
1000 | int r; | |
1001 | ||
1002 | r = acquire_bus(&bus); | |
1003 | if (r < 0) | |
1004 | return r; | |
1005 | ||
1006 | if (sd_json_format_enabled(arg_json_format_flags)) | |
1007 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); | |
1008 | ||
1009 | if (argc == 2) | |
1010 | return resolve_service(bus, NULL, NULL, argv[1]); | |
1011 | else if (argc == 3) | |
1012 | return resolve_service(bus, NULL, argv[1], argv[2]); | |
1013 | else | |
1014 | return resolve_service(bus, argv[1], argv[2], argv[3]); | |
1015 | } | |
1016 | ||
1017 | static int resolve_openpgp(sd_bus *bus, const char *address) { | |
1018 | const char *domain, *full; | |
1019 | int r; | |
1020 | _cleanup_free_ char *hashed = NULL; | |
1021 | ||
1022 | assert(bus); | |
1023 | assert(address); | |
1024 | ||
1025 | domain = strrchr(address, '@'); | |
1026 | if (!domain) | |
1027 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1028 | "Address does not contain '@': \"%s\"", address); | |
1029 | if (domain == address || domain[1] == '\0') | |
1030 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1031 | "Address starts or ends with '@': \"%s\"", address); | |
1032 | domain++; | |
1033 | ||
1034 | r = string_hashsum_sha256(address, domain - 1 - address, &hashed); | |
1035 | if (r < 0) | |
1036 | return log_error_errno(r, "Hashing failed: %m"); | |
1037 | ||
1038 | strshorten(hashed, 56); | |
1039 | ||
1040 | full = strjoina(hashed, "._openpgpkey.", domain); | |
1041 | log_debug("Looking up \"%s\".", full); | |
1042 | ||
1043 | r = resolve_record(bus, full, | |
1044 | arg_class ?: DNS_CLASS_IN, | |
1045 | arg_type ?: DNS_TYPE_OPENPGPKEY, false); | |
1046 | ||
1047 | if (IN_SET(r, -ENXIO, -ESRCH)) { /* NXDOMAIN or NODATA? */ | |
1048 | hashed = mfree(hashed); | |
1049 | r = string_hashsum_sha224(address, domain - 1 - address, &hashed); | |
1050 | if (r < 0) | |
1051 | return log_error_errno(r, "Hashing failed: %m"); | |
1052 | ||
1053 | full = strjoina(hashed, "._openpgpkey.", domain); | |
1054 | log_debug("Looking up \"%s\".", full); | |
1055 | ||
1056 | return resolve_record(bus, full, | |
1057 | arg_class ?: DNS_CLASS_IN, | |
1058 | arg_type ?: DNS_TYPE_OPENPGPKEY, true); | |
1059 | } | |
1060 | ||
1061 | return r; | |
1062 | } | |
1063 | ||
1064 | static int verb_openpgp(int argc, char **argv, void *userdata) { | |
1065 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
1066 | int r, ret = 0; | |
1067 | ||
1068 | r = acquire_bus(&bus); | |
1069 | if (r < 0) | |
1070 | return r; | |
1071 | ||
1072 | if (sd_json_format_enabled(arg_json_format_flags)) | |
1073 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); | |
1074 | ||
1075 | STRV_FOREACH(p, strv_skip(argv, 1)) | |
1076 | RET_GATHER(ret, resolve_openpgp(bus, *p)); | |
1077 | ||
1078 | return ret; | |
1079 | } | |
1080 | ||
1081 | static int resolve_tlsa(sd_bus *bus, const char *family, const char *address) { | |
1082 | const char *port; | |
1083 | uint16_t port_num = 443; | |
1084 | _cleanup_free_ char *full = NULL; | |
1085 | int r; | |
1086 | ||
1087 | assert(bus); | |
1088 | assert(address); | |
1089 | ||
1090 | port = strrchr(address, ':'); | |
1091 | if (port) { | |
1092 | r = parse_ip_port(port + 1, &port_num); | |
1093 | if (r < 0) | |
1094 | return log_error_errno(r, "Invalid port \"%s\".", port + 1); | |
1095 | ||
1096 | address = strndupa_safe(address, port - address); | |
1097 | } | |
1098 | ||
1099 | r = asprintf(&full, "_%u._%s.%s", | |
1100 | port_num, | |
1101 | family, | |
1102 | address); | |
1103 | if (r < 0) | |
1104 | return log_oom(); | |
1105 | ||
1106 | log_debug("Looking up \"%s\".", full); | |
1107 | ||
1108 | return resolve_record(bus, full, | |
1109 | arg_class ?: DNS_CLASS_IN, | |
1110 | arg_type ?: DNS_TYPE_TLSA, true); | |
1111 | } | |
1112 | ||
1113 | static bool service_family_is_valid(const char *s) { | |
1114 | return STR_IN_SET(s, "tcp", "udp", "sctp"); | |
1115 | } | |
1116 | ||
1117 | static int verb_tlsa(int argc, char **argv, void *userdata) { | |
1118 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
1119 | const char *family = "tcp"; | |
1120 | char **args; | |
1121 | int r, ret = 0; | |
1122 | ||
1123 | assert(argc >= 2); | |
1124 | ||
1125 | r = acquire_bus(&bus); | |
1126 | if (r < 0) | |
1127 | return r; | |
1128 | ||
1129 | if (sd_json_format_enabled(arg_json_format_flags)) | |
1130 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); | |
1131 | ||
1132 | if (service_family_is_valid(argv[1])) { | |
1133 | family = argv[1]; | |
1134 | args = strv_skip(argv, 2); | |
1135 | } else | |
1136 | args = strv_skip(argv, 1); | |
1137 | ||
1138 | STRV_FOREACH(p, args) | |
1139 | RET_GATHER(ret, resolve_tlsa(bus, family, *p)); | |
1140 | ||
1141 | return ret; | |
1142 | } | |
1143 | ||
1144 | static int show_statistics(int argc, char **argv, void *userdata) { | |
1145 | _cleanup_(table_unrefp) Table *table = NULL; | |
1146 | sd_json_variant *reply = NULL; | |
1147 | _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; | |
1148 | int r; | |
1149 | ||
1150 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
1151 | ||
1152 | r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor"); | |
1153 | if (r < 0) | |
1154 | return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m"); | |
1155 | ||
1156 | r = varlink_callbo_and_log( | |
1157 | vl, | |
1158 | "io.systemd.Resolve.Monitor.DumpStatistics", | |
1159 | &reply, | |
1160 | SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password)); | |
1161 | if (r < 0) | |
1162 | return r; | |
1163 | ||
1164 | if (sd_json_format_enabled(arg_json_format_flags)) | |
1165 | return sd_json_variant_dump(reply, arg_json_format_flags, NULL, NULL); | |
1166 | ||
1167 | struct statistics { | |
1168 | sd_json_variant *transactions; | |
1169 | sd_json_variant *cache; | |
1170 | sd_json_variant *dnssec; | |
1171 | } statistics; | |
1172 | ||
1173 | static const sd_json_dispatch_field statistics_dispatch_table[] = { | |
1174 | { "transactions", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, transactions), SD_JSON_MANDATORY }, | |
1175 | { "cache", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, cache), SD_JSON_MANDATORY }, | |
1176 | { "dnssec", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, dnssec), SD_JSON_MANDATORY }, | |
1177 | {}, | |
1178 | }; | |
1179 | ||
1180 | r = sd_json_dispatch(reply, statistics_dispatch_table, SD_JSON_LOG, &statistics); | |
1181 | if (r < 0) | |
1182 | return r; | |
1183 | ||
1184 | struct transactions { | |
1185 | uint64_t n_current_transactions; | |
1186 | uint64_t n_transactions_total; | |
1187 | uint64_t n_timeouts_total; | |
1188 | uint64_t n_timeouts_served_stale_total; | |
1189 | uint64_t n_failure_responses_total; | |
1190 | uint64_t n_failure_responses_served_stale_total; | |
1191 | } transactions; | |
1192 | ||
1193 | static const sd_json_dispatch_field transactions_dispatch_table[] = { | |
1194 | { "currentTransactions", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_current_transactions), SD_JSON_MANDATORY }, | |
1195 | { "totalTransactions", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_transactions_total), SD_JSON_MANDATORY }, | |
1196 | { "totalTimeouts", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_timeouts_total), SD_JSON_MANDATORY }, | |
1197 | { "totalTimeoutsServedStale", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_timeouts_served_stale_total), SD_JSON_MANDATORY }, | |
1198 | { "totalFailedResponses", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_total), SD_JSON_MANDATORY }, | |
1199 | { "totalFailedResponsesServedStale", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_served_stale_total), SD_JSON_MANDATORY }, | |
1200 | {}, | |
1201 | }; | |
1202 | ||
1203 | r = sd_json_dispatch(statistics.transactions, transactions_dispatch_table, SD_JSON_LOG, &transactions); | |
1204 | if (r < 0) | |
1205 | return r; | |
1206 | ||
1207 | struct cache { | |
1208 | uint64_t cache_size; | |
1209 | uint64_t n_cache_hit; | |
1210 | uint64_t n_cache_miss; | |
1211 | } cache; | |
1212 | ||
1213 | static const sd_json_dispatch_field cache_dispatch_table[] = { | |
1214 | { "size", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, cache_size), SD_JSON_MANDATORY }, | |
1215 | { "hits", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, n_cache_hit), SD_JSON_MANDATORY }, | |
1216 | { "misses", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, n_cache_miss), SD_JSON_MANDATORY }, | |
1217 | {}, | |
1218 | }; | |
1219 | ||
1220 | r = sd_json_dispatch(statistics.cache, cache_dispatch_table, SD_JSON_LOG, &cache); | |
1221 | if (r < 0) | |
1222 | return r; | |
1223 | ||
1224 | struct dnsssec { | |
1225 | uint64_t n_dnssec_secure; | |
1226 | uint64_t n_dnssec_insecure; | |
1227 | uint64_t n_dnssec_bogus; | |
1228 | uint64_t n_dnssec_indeterminate; | |
1229 | } dnsssec; | |
1230 | ||
1231 | static const sd_json_dispatch_field dnssec_dispatch_table[] = { | |
1232 | { "secure", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_secure), SD_JSON_MANDATORY }, | |
1233 | { "insecure", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_insecure), SD_JSON_MANDATORY }, | |
1234 | { "bogus", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_bogus), SD_JSON_MANDATORY }, | |
1235 | { "indeterminate", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_indeterminate), SD_JSON_MANDATORY }, | |
1236 | {}, | |
1237 | }; | |
1238 | ||
1239 | r = sd_json_dispatch(statistics.dnssec, dnssec_dispatch_table, SD_JSON_LOG, &dnsssec); | |
1240 | if (r < 0) | |
1241 | return r; | |
1242 | ||
1243 | table = table_new_vertical(); | |
1244 | if (!table) | |
1245 | return log_oom(); | |
1246 | ||
1247 | r = table_add_many(table, | |
1248 | TABLE_STRING, "Transactions", | |
1249 | TABLE_SET_COLOR, ansi_highlight(), | |
1250 | TABLE_SET_ALIGN_PERCENT, 0, | |
1251 | TABLE_EMPTY, | |
1252 | TABLE_FIELD, "Current Transactions", | |
1253 | TABLE_SET_ALIGN_PERCENT, 100, | |
1254 | TABLE_UINT64, transactions.n_current_transactions, | |
1255 | TABLE_SET_ALIGN_PERCENT, 100, | |
1256 | TABLE_FIELD, "Total Transactions", | |
1257 | TABLE_UINT64, transactions.n_transactions_total, | |
1258 | TABLE_EMPTY, TABLE_EMPTY, | |
1259 | TABLE_STRING, "Cache", | |
1260 | TABLE_SET_COLOR, ansi_highlight(), | |
1261 | TABLE_SET_ALIGN_PERCENT, 0, | |
1262 | TABLE_EMPTY, | |
1263 | TABLE_FIELD, "Current Cache Size", | |
1264 | TABLE_SET_ALIGN_PERCENT, 100, | |
1265 | TABLE_UINT64, cache.cache_size, | |
1266 | TABLE_FIELD, "Cache Hits", | |
1267 | TABLE_UINT64, cache.n_cache_hit, | |
1268 | TABLE_FIELD, "Cache Misses", | |
1269 | TABLE_UINT64, cache.n_cache_miss, | |
1270 | TABLE_EMPTY, TABLE_EMPTY, | |
1271 | TABLE_STRING, "Failure Transactions", | |
1272 | TABLE_SET_COLOR, ansi_highlight(), | |
1273 | TABLE_SET_ALIGN_PERCENT, 0, | |
1274 | TABLE_EMPTY, | |
1275 | TABLE_FIELD, "Total Timeouts", | |
1276 | TABLE_SET_ALIGN_PERCENT, 100, | |
1277 | TABLE_UINT64, transactions.n_timeouts_total, | |
1278 | TABLE_FIELD, "Total Timeouts (Stale Data Served)", | |
1279 | TABLE_UINT64, transactions.n_timeouts_served_stale_total, | |
1280 | TABLE_FIELD, "Total Failure Responses", | |
1281 | TABLE_UINT64, transactions.n_failure_responses_total, | |
1282 | TABLE_FIELD, "Total Failure Responses (Stale Data Served)", | |
1283 | TABLE_UINT64, transactions.n_failure_responses_served_stale_total, | |
1284 | TABLE_EMPTY, TABLE_EMPTY, | |
1285 | TABLE_STRING, "DNSSEC Verdicts", | |
1286 | TABLE_SET_COLOR, ansi_highlight(), | |
1287 | TABLE_SET_ALIGN_PERCENT, 0, | |
1288 | TABLE_EMPTY, | |
1289 | TABLE_FIELD, "Secure", | |
1290 | TABLE_SET_ALIGN_PERCENT, 100, | |
1291 | TABLE_UINT64, dnsssec.n_dnssec_secure, | |
1292 | TABLE_FIELD, "Insecure", | |
1293 | TABLE_UINT64, dnsssec.n_dnssec_insecure, | |
1294 | TABLE_FIELD, "Bogus", | |
1295 | TABLE_UINT64, dnsssec.n_dnssec_bogus, | |
1296 | TABLE_FIELD, "Indeterminate", | |
1297 | TABLE_UINT64, dnsssec.n_dnssec_indeterminate | |
1298 | ); | |
1299 | if (r < 0) | |
1300 | return table_log_add_error(r); | |
1301 | ||
1302 | r = table_print(table, NULL); | |
1303 | if (r < 0) | |
1304 | return table_log_print_error(r); | |
1305 | ||
1306 | return 0; | |
1307 | } | |
1308 | ||
1309 | static int reset_statistics(int argc, char **argv, void *userdata) { | |
1310 | sd_json_variant *reply = NULL; | |
1311 | _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; | |
1312 | int r; | |
1313 | ||
1314 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
1315 | ||
1316 | r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor"); | |
1317 | if (r < 0) | |
1318 | return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m"); | |
1319 | ||
1320 | r = varlink_callbo_and_log( | |
1321 | vl, | |
1322 | "io.systemd.Resolve.Monitor.ResetStatistics", | |
1323 | &reply, | |
1324 | SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password)); | |
1325 | if (r < 0) | |
1326 | return r; | |
1327 | ||
1328 | if (sd_json_format_enabled(arg_json_format_flags)) | |
1329 | return sd_json_variant_dump(reply, arg_json_format_flags, NULL, NULL); | |
1330 | ||
1331 | return 0; | |
1332 | } | |
1333 | ||
1334 | static int flush_caches(int argc, char **argv, void *userdata) { | |
1335 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
1336 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1337 | int r; | |
1338 | ||
1339 | r = acquire_bus(&bus); | |
1340 | if (r < 0) | |
1341 | return r; | |
1342 | ||
1343 | r = bus_call_method(bus, bus_resolve_mgr, "FlushCaches", &error, NULL, NULL); | |
1344 | if (r < 0) | |
1345 | return log_error_errno(r, "Failed to flush caches: %s", bus_error_message(&error, r)); | |
1346 | ||
1347 | return 0; | |
1348 | } | |
1349 | ||
1350 | static int reset_server_features(int argc, char **argv, void *userdata) { | |
1351 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
1352 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1353 | int r; | |
1354 | ||
1355 | r = acquire_bus(&bus); | |
1356 | if (r < 0) | |
1357 | return r; | |
1358 | ||
1359 | r = bus_call_method(bus, bus_resolve_mgr, "ResetServerFeatures", &error, NULL, NULL); | |
1360 | if (r < 0) | |
1361 | return log_error_errno(r, "Failed to reset server features: %s", bus_error_message(&error, r)); | |
1362 | ||
1363 | return 0; | |
1364 | } | |
1365 | ||
1366 | typedef enum { | |
1367 | READ_DNS_WITH_IFINDEX = 1 << 0, /* read "ifindex" reply that also carries an interface index */ | |
1368 | READ_DNS_EXTENDED = 1 << 1, /* read "extended" reply, i.e. with port number and server name */ | |
1369 | READ_DNS_ONLY_GLOBAL = 1 << 2, /* suppress entries with an (non-loopback) ifindex set (i.e. which are specific to some interface) */ | |
1370 | } ReadDNSFlag; | |
1371 | ||
1372 | static const char *dns_server_property_signature(ReadDNSFlag flags) { | |
1373 | switch (flags & (READ_DNS_WITH_IFINDEX|READ_DNS_EXTENDED)) { | |
1374 | ||
1375 | case 0: | |
1376 | return "iay"; | |
1377 | ||
1378 | case READ_DNS_WITH_IFINDEX: | |
1379 | return "iiay"; | |
1380 | ||
1381 | case READ_DNS_EXTENDED: | |
1382 | return "iayqs"; | |
1383 | ||
1384 | case READ_DNS_WITH_IFINDEX|READ_DNS_EXTENDED: | |
1385 | return "iiayqs"; | |
1386 | ||
1387 | default: | |
1388 | assert_not_reached(); | |
1389 | } | |
1390 | } | |
1391 | ||
1392 | static int read_dns_server_one(sd_bus_message *m, ReadDNSFlag flags, char **ret) { | |
1393 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1394 | _cleanup_free_ char *pretty = NULL; | |
1395 | union in_addr_union a; | |
1396 | const char *name = NULL; | |
1397 | int32_t ifindex = 0; | |
1398 | int family, r; | |
1399 | uint16_t port = 0; | |
1400 | ||
1401 | assert(m); | |
1402 | assert(ret); | |
1403 | ||
1404 | r = sd_bus_message_enter_container( | |
1405 | m, | |
1406 | 'r', | |
1407 | dns_server_property_signature(flags)); | |
1408 | if (r <= 0) | |
1409 | return r; | |
1410 | ||
1411 | if (FLAGS_SET(flags, READ_DNS_WITH_IFINDEX)) { | |
1412 | r = sd_bus_message_read(m, "i", &ifindex); | |
1413 | if (r < 0) | |
1414 | return r; | |
1415 | } | |
1416 | ||
1417 | r = bus_message_read_in_addr_auto(m, &error, &family, &a); | |
1418 | if (r < 0) { | |
1419 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) { | |
1420 | /* CurrentDNSServer provides AF_UNSPEC when no current server assigned. */ | |
1421 | log_debug("Invalid DNS server, ignoring: %s", bus_error_message(&error, r)); | |
1422 | ||
1423 | for (;;) { | |
1424 | r = sd_bus_message_skip(m, NULL); | |
1425 | if (r == -ENXIO) /* End of the container */ | |
1426 | break; | |
1427 | if (r < 0) | |
1428 | return r; | |
1429 | } | |
1430 | ||
1431 | r = sd_bus_message_exit_container(m); | |
1432 | if (r < 0) | |
1433 | return r; | |
1434 | ||
1435 | *ret = NULL; | |
1436 | return 1; | |
1437 | } | |
1438 | ||
1439 | return r; | |
1440 | } | |
1441 | ||
1442 | if (FLAGS_SET(flags, READ_DNS_EXTENDED)) { | |
1443 | r = sd_bus_message_read(m, "qs", &port, &name); | |
1444 | if (r < 0) | |
1445 | return r; | |
1446 | } | |
1447 | ||
1448 | r = sd_bus_message_exit_container(m); | |
1449 | if (r < 0) | |
1450 | return r; | |
1451 | ||
1452 | if (FLAGS_SET(flags, READ_DNS_ONLY_GLOBAL) && ifindex > 0 && ifindex != LOOPBACK_IFINDEX) { | |
1453 | /* This one has an (non-loopback) ifindex set, and we were told to suppress those. Hence do so. */ | |
1454 | *ret = NULL; | |
1455 | return 1; | |
1456 | } | |
1457 | ||
1458 | r = in_addr_port_ifindex_name_to_string(family, &a, port, ifindex, name, &pretty); | |
1459 | if (r < 0) | |
1460 | return r; | |
1461 | ||
1462 | *ret = TAKE_PTR(pretty); | |
1463 | return 1; | |
1464 | } | |
1465 | ||
1466 | static int map_dns_servers_internal(sd_bus *bus, const char *member, sd_bus_message *m, ReadDNSFlag flags, sd_bus_error *error, void *userdata) { | |
1467 | char ***l = ASSERT_PTR(userdata); | |
1468 | int r; | |
1469 | ||
1470 | assert(bus); | |
1471 | assert(member); | |
1472 | assert(m); | |
1473 | ||
1474 | const char *sig = strjoina("(", dns_server_property_signature(flags), ")"); | |
1475 | ||
1476 | r = sd_bus_message_enter_container(m, 'a', sig); | |
1477 | if (r < 0) | |
1478 | return r; | |
1479 | ||
1480 | for (;;) { | |
1481 | _cleanup_free_ char *pretty = NULL; | |
1482 | ||
1483 | r = read_dns_server_one(m, flags, &pretty); | |
1484 | if (r < 0) | |
1485 | return r; | |
1486 | if (r == 0) | |
1487 | break; | |
1488 | ||
1489 | if (isempty(pretty)) | |
1490 | continue; | |
1491 | ||
1492 | r = strv_consume(l, TAKE_PTR(pretty)); | |
1493 | if (r < 0) | |
1494 | return r; | |
1495 | } | |
1496 | ||
1497 | r = sd_bus_message_exit_container(m); | |
1498 | if (r < 0) | |
1499 | return r; | |
1500 | ||
1501 | return 0; | |
1502 | } | |
1503 | ||
1504 | static int map_link_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1505 | return map_dns_servers_internal(bus, member, m, /* flags = */ 0, error, userdata); | |
1506 | } | |
1507 | ||
1508 | static int map_link_dns_servers_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1509 | return map_dns_servers_internal(bus, member, m, READ_DNS_EXTENDED, error, userdata); | |
1510 | } | |
1511 | ||
1512 | static int map_link_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1513 | assert(m); | |
1514 | assert(userdata); | |
1515 | ||
1516 | return read_dns_server_one(m, /* flags = */ 0, userdata); | |
1517 | } | |
1518 | ||
1519 | static int map_link_current_dns_server_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1520 | assert(m); | |
1521 | assert(userdata); | |
1522 | ||
1523 | return read_dns_server_one(m, READ_DNS_EXTENDED, userdata); | |
1524 | } | |
1525 | ||
1526 | static int read_domain_one(sd_bus_message *m, bool with_ifindex, char **ret) { | |
1527 | _cleanup_free_ char *str = NULL; | |
1528 | int ifindex, route_only, r; | |
1529 | const char *domain; | |
1530 | ||
1531 | assert(m); | |
1532 | assert(ret); | |
1533 | ||
1534 | if (with_ifindex) | |
1535 | r = sd_bus_message_read(m, "(isb)", &ifindex, &domain, &route_only); | |
1536 | else | |
1537 | r = sd_bus_message_read(m, "(sb)", &domain, &route_only); | |
1538 | if (r <= 0) | |
1539 | return r; | |
1540 | ||
1541 | if (with_ifindex && ifindex != 0) { | |
1542 | /* only show the global ones here */ | |
1543 | *ret = NULL; | |
1544 | return 1; | |
1545 | } | |
1546 | ||
1547 | if (route_only) | |
1548 | str = strjoin("~", domain); | |
1549 | else | |
1550 | str = strdup(domain); | |
1551 | if (!str) | |
1552 | return -ENOMEM; | |
1553 | ||
1554 | *ret = TAKE_PTR(str); | |
1555 | return 1; | |
1556 | } | |
1557 | ||
1558 | static int map_domains_internal( | |
1559 | sd_bus *bus, | |
1560 | const char *member, | |
1561 | sd_bus_message *m, | |
1562 | bool with_ifindex, | |
1563 | sd_bus_error *error, | |
1564 | void *userdata) { | |
1565 | ||
1566 | char ***l = ASSERT_PTR(userdata); | |
1567 | int r; | |
1568 | ||
1569 | assert(bus); | |
1570 | assert(member); | |
1571 | assert(m); | |
1572 | ||
1573 | r = sd_bus_message_enter_container(m, 'a', with_ifindex ? "(isb)" : "(sb)"); | |
1574 | if (r < 0) | |
1575 | return r; | |
1576 | ||
1577 | for (;;) { | |
1578 | _cleanup_free_ char *pretty = NULL; | |
1579 | ||
1580 | r = read_domain_one(m, with_ifindex, &pretty); | |
1581 | if (r < 0) | |
1582 | return r; | |
1583 | if (r == 0) | |
1584 | break; | |
1585 | ||
1586 | if (isempty(pretty)) | |
1587 | continue; | |
1588 | ||
1589 | r = strv_consume(l, TAKE_PTR(pretty)); | |
1590 | if (r < 0) | |
1591 | return r; | |
1592 | } | |
1593 | ||
1594 | r = sd_bus_message_exit_container(m); | |
1595 | if (r < 0) | |
1596 | return r; | |
1597 | ||
1598 | return 0; | |
1599 | } | |
1600 | ||
1601 | static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1602 | return map_domains_internal(bus, member, m, /* with_ifindex= */ false, error, userdata); | |
1603 | } | |
1604 | ||
1605 | static int status_print_strv_full(int ifindex, const char *ifname, const char *delegate_id, char **p) { | |
1606 | const unsigned indent = strlen("Global: "); /* Use the same indentation everywhere to make things nice */ | |
1607 | int pos1, pos2; | |
1608 | ||
1609 | if (ifname) | |
1610 | printf("%s%nLink %i (%s)%n%s:", ansi_highlight(), &pos1, ifindex, ifname, &pos2, ansi_normal()); | |
1611 | else if (delegate_id) | |
1612 | printf("%s%nDelegate %s%n%s:", ansi_highlight(), &pos1, delegate_id, &pos2, ansi_normal()); | |
1613 | else | |
1614 | printf("%s%nGlobal%n%s:", ansi_highlight(), &pos1, &pos2, ansi_normal()); | |
1615 | ||
1616 | size_t cols = columns(), position = pos2 - pos1 + 2; | |
1617 | ||
1618 | STRV_FOREACH(i, p) { | |
1619 | size_t our_len = utf8_console_width(*i); /* This returns -1 on invalid utf-8 (which shouldn't happen). | |
1620 | * If that happens, we'll just print one item per line. */ | |
1621 | ||
1622 | if (position <= indent || size_add(size_add(position, 1), our_len) < cols) { | |
1623 | printf(" %s", *i); | |
1624 | position = size_add(size_add(position, 1), our_len); | |
1625 | } else { | |
1626 | printf("\n%*s%s", (int) indent, "", *i); | |
1627 | position = size_add(our_len, indent); | |
1628 | } | |
1629 | } | |
1630 | ||
1631 | printf("\n"); | |
1632 | ||
1633 | return 0; | |
1634 | } | |
1635 | ||
1636 | static int status_print_strv_ifindex(int ifindex, const char *ifname, char **p) { | |
1637 | return status_print_strv_full(ifindex, ifname, NULL, p); | |
1638 | } | |
1639 | ||
1640 | static int status_print_strv_delegate(const char *delegate_id, char **p) { | |
1641 | return status_print_strv_full(0, NULL, delegate_id, p); | |
1642 | } | |
1643 | ||
1644 | static int status_print_strv_global(char **p) { | |
1645 | return status_print_strv_full(0, NULL, NULL, p); | |
1646 | } | |
1647 | ||
1648 | typedef struct LinkInfo { | |
1649 | uint64_t scopes_mask; | |
1650 | const char *llmnr; | |
1651 | const char *mdns; | |
1652 | const char *dns_over_tls; | |
1653 | const char *dnssec; | |
1654 | char *current_dns; | |
1655 | char *current_dns_ex; | |
1656 | char **dns; | |
1657 | char **dns_ex; | |
1658 | char **domains; | |
1659 | char **ntas; | |
1660 | bool dnssec_supported; | |
1661 | bool default_route; | |
1662 | } LinkInfo; | |
1663 | ||
1664 | typedef struct GlobalInfo { | |
1665 | char *current_dns; | |
1666 | char *current_dns_ex; | |
1667 | char **dns; | |
1668 | char **dns_ex; | |
1669 | char **fallback_dns; | |
1670 | char **fallback_dns_ex; | |
1671 | char **domains; | |
1672 | char **ntas; | |
1673 | const char *llmnr; | |
1674 | const char *mdns; | |
1675 | const char *dns_over_tls; | |
1676 | const char *dnssec; | |
1677 | const char *resolv_conf_mode; | |
1678 | bool dnssec_supported; | |
1679 | } GlobalInfo; | |
1680 | ||
1681 | static void link_info_done(LinkInfo *p) { | |
1682 | assert(p); | |
1683 | ||
1684 | free(p->current_dns); | |
1685 | free(p->current_dns_ex); | |
1686 | strv_free(p->dns); | |
1687 | strv_free(p->dns_ex); | |
1688 | strv_free(p->domains); | |
1689 | strv_free(p->ntas); | |
1690 | } | |
1691 | ||
1692 | static void global_info_done(GlobalInfo *p) { | |
1693 | assert(p); | |
1694 | ||
1695 | free(p->current_dns); | |
1696 | free(p->current_dns_ex); | |
1697 | strv_free(p->dns); | |
1698 | strv_free(p->dns_ex); | |
1699 | strv_free(p->fallback_dns); | |
1700 | strv_free(p->fallback_dns_ex); | |
1701 | strv_free(p->domains); | |
1702 | strv_free(p->ntas); | |
1703 | } | |
1704 | ||
1705 | static int dump_list(Table *table, const char *field, char * const *l) { | |
1706 | int r; | |
1707 | ||
1708 | if (strv_isempty(l)) | |
1709 | return 0; | |
1710 | ||
1711 | r = table_add_many(table, | |
1712 | TABLE_FIELD, field, | |
1713 | TABLE_STRV_WRAPPED, l); | |
1714 | if (r < 0) | |
1715 | return table_log_add_error(r); | |
1716 | ||
1717 | return 0; | |
1718 | } | |
1719 | ||
1720 | static int strv_extend_extended_bool(char ***strv, const char *name, const char *value) { | |
1721 | int r; | |
1722 | ||
1723 | if (value) { | |
1724 | r = parse_boolean(value); | |
1725 | if (r >= 0) | |
1726 | return strv_extendf(strv, "%s%s", plus_minus(r), name); | |
1727 | } | |
1728 | ||
1729 | return strv_extendf(strv, "%s=%s", name, value ?: "???"); | |
1730 | } | |
1731 | ||
1732 | static char** link_protocol_status(const LinkInfo *info) { | |
1733 | _cleanup_strv_free_ char **s = NULL; | |
1734 | ||
1735 | if (strv_extendf(&s, "%sDefaultRoute", plus_minus(info->default_route)) < 0) | |
1736 | return NULL; | |
1737 | ||
1738 | if (strv_extend_extended_bool(&s, "LLMNR", info->llmnr) < 0) | |
1739 | return NULL; | |
1740 | ||
1741 | if (strv_extend_extended_bool(&s, "mDNS", info->mdns) < 0) | |
1742 | return NULL; | |
1743 | ||
1744 | if (strv_extend_extended_bool(&s, "DNSOverTLS", info->dns_over_tls) < 0) | |
1745 | return NULL; | |
1746 | ||
1747 | if (strv_extendf(&s, "DNSSEC=%s/%s", | |
1748 | info->dnssec ?: "???", | |
1749 | info->dnssec_supported ? "supported" : "unsupported") < 0) | |
1750 | return NULL; | |
1751 | ||
1752 | return TAKE_PTR(s); | |
1753 | } | |
1754 | ||
1755 | static char** global_protocol_status(const GlobalInfo *info) { | |
1756 | _cleanup_strv_free_ char **s = NULL; | |
1757 | ||
1758 | if (strv_extend_extended_bool(&s, "LLMNR", info->llmnr) < 0) | |
1759 | return NULL; | |
1760 | ||
1761 | if (strv_extend_extended_bool(&s, "mDNS", info->mdns) < 0) | |
1762 | return NULL; | |
1763 | ||
1764 | if (strv_extend_extended_bool(&s, "DNSOverTLS", info->dns_over_tls) < 0) | |
1765 | return NULL; | |
1766 | ||
1767 | if (strv_extendf(&s, "DNSSEC=%s/%s", | |
1768 | info->dnssec ?: "???", | |
1769 | info->dnssec_supported ? "supported" : "unsupported") < 0) | |
1770 | return NULL; | |
1771 | ||
1772 | return TAKE_PTR(s); | |
1773 | } | |
1774 | ||
1775 | static int status_ifindex(sd_bus *bus, int ifindex, const char *name, StatusMode mode, bool *empty_line) { | |
1776 | static const struct bus_properties_map property_map[] = { | |
1777 | { "ScopesMask", "t", NULL, offsetof(LinkInfo, scopes_mask) }, | |
1778 | { "DNS", "a(iay)", map_link_dns_servers, offsetof(LinkInfo, dns) }, | |
1779 | { "DNSEx", "a(iayqs)", map_link_dns_servers_ex, offsetof(LinkInfo, dns_ex) }, | |
1780 | { "CurrentDNSServer", "(iay)", map_link_current_dns_server, offsetof(LinkInfo, current_dns) }, | |
1781 | { "CurrentDNSServerEx", "(iayqs)", map_link_current_dns_server_ex, offsetof(LinkInfo, current_dns_ex) }, | |
1782 | { "Domains", "a(sb)", map_link_domains, offsetof(LinkInfo, domains) }, | |
1783 | { "DefaultRoute", "b", NULL, offsetof(LinkInfo, default_route) }, | |
1784 | { "LLMNR", "s", NULL, offsetof(LinkInfo, llmnr) }, | |
1785 | { "MulticastDNS", "s", NULL, offsetof(LinkInfo, mdns) }, | |
1786 | { "DNSOverTLS", "s", NULL, offsetof(LinkInfo, dns_over_tls) }, | |
1787 | { "DNSSEC", "s", NULL, offsetof(LinkInfo, dnssec) }, | |
1788 | { "DNSSECNegativeTrustAnchors", "as", bus_map_strv_sort, offsetof(LinkInfo, ntas) }, | |
1789 | { "DNSSECSupported", "b", NULL, offsetof(LinkInfo, dnssec_supported) }, | |
1790 | {} | |
1791 | }; | |
1792 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1793 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
1794 | _cleanup_(link_info_done) LinkInfo link_info = {}; | |
1795 | _cleanup_(table_unrefp) Table *table = NULL; | |
1796 | _cleanup_free_ char *p = NULL; | |
1797 | char ifi[DECIMAL_STR_MAX(int)], ifname[IF_NAMESIZE]; | |
1798 | int r; | |
1799 | ||
1800 | assert(bus); | |
1801 | assert(ifindex > 0); | |
1802 | ||
1803 | if (!name) { | |
1804 | r = format_ifname(ifindex, ifname); | |
1805 | if (r < 0) | |
1806 | return log_error_errno(r, "Failed to resolve interface name for %i: %m", ifindex); | |
1807 | ||
1808 | name = ifname; | |
1809 | } | |
1810 | ||
1811 | xsprintf(ifi, "%i", ifindex); | |
1812 | r = sd_bus_path_encode("/org/freedesktop/resolve1/link", ifi, &p); | |
1813 | if (r < 0) | |
1814 | return log_oom(); | |
1815 | ||
1816 | r = bus_map_all_properties(bus, | |
1817 | "org.freedesktop.resolve1", | |
1818 | p, | |
1819 | property_map, | |
1820 | BUS_MAP_BOOLEAN_AS_BOOL, | |
1821 | &error, | |
1822 | &m, | |
1823 | &link_info); | |
1824 | if (r < 0) | |
1825 | return log_error_errno(r, "Failed to get link data for %i: %s", ifindex, bus_error_message(&error, r)); | |
1826 | ||
1827 | pager_open(arg_pager_flags); | |
1828 | ||
1829 | switch (mode) { | |
1830 | ||
1831 | case STATUS_DNS: | |
1832 | return status_print_strv_ifindex(ifindex, name, link_info.dns_ex ?: link_info.dns); | |
1833 | ||
1834 | case STATUS_DOMAIN: | |
1835 | return status_print_strv_ifindex(ifindex, name, link_info.domains); | |
1836 | ||
1837 | case STATUS_NTA: | |
1838 | return status_print_strv_ifindex(ifindex, name, link_info.ntas); | |
1839 | ||
1840 | case STATUS_DEFAULT_ROUTE: | |
1841 | printf("%sLink %i (%s)%s: %s\n", | |
1842 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1843 | yes_no(link_info.default_route)); | |
1844 | ||
1845 | return 0; | |
1846 | ||
1847 | case STATUS_LLMNR: | |
1848 | printf("%sLink %i (%s)%s: %s\n", | |
1849 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1850 | strna(link_info.llmnr)); | |
1851 | ||
1852 | return 0; | |
1853 | ||
1854 | case STATUS_MDNS: | |
1855 | printf("%sLink %i (%s)%s: %s\n", | |
1856 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1857 | strna(link_info.mdns)); | |
1858 | ||
1859 | return 0; | |
1860 | ||
1861 | case STATUS_PRIVATE: | |
1862 | printf("%sLink %i (%s)%s: %s\n", | |
1863 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1864 | strna(link_info.dns_over_tls)); | |
1865 | ||
1866 | return 0; | |
1867 | ||
1868 | case STATUS_DNSSEC: | |
1869 | printf("%sLink %i (%s)%s: %s\n", | |
1870 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1871 | strna(link_info.dnssec)); | |
1872 | ||
1873 | return 0; | |
1874 | ||
1875 | case STATUS_ALL: | |
1876 | break; | |
1877 | ||
1878 | default: | |
1879 | return 0; | |
1880 | } | |
1881 | ||
1882 | if (empty_line && *empty_line) | |
1883 | fputc('\n', stdout); | |
1884 | ||
1885 | printf("%sLink %i (%s)%s\n", | |
1886 | ansi_highlight(), ifindex, name, ansi_normal()); | |
1887 | ||
1888 | table = table_new_vertical(); | |
1889 | if (!table) | |
1890 | return log_oom(); | |
1891 | ||
1892 | r = table_add_many(table, | |
1893 | TABLE_FIELD, "Current Scopes", | |
1894 | TABLE_SET_MINIMUM_WIDTH, 19); | |
1895 | if (r < 0) | |
1896 | return table_log_add_error(r); | |
1897 | ||
1898 | if (link_info.scopes_mask == 0) | |
1899 | r = table_add_cell(table, NULL, TABLE_STRING, "none"); | |
1900 | else { | |
1901 | _cleanup_free_ char *buf = NULL; | |
1902 | size_t len; | |
1903 | ||
1904 | if (asprintf(&buf, "%s%s%s%s%s", | |
1905 | link_info.scopes_mask & SD_RESOLVED_DNS ? "DNS " : "", | |
1906 | link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV4 ? "LLMNR/IPv4 " : "", | |
1907 | link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV6 ? "LLMNR/IPv6 " : "", | |
1908 | link_info.scopes_mask & SD_RESOLVED_MDNS_IPV4 ? "mDNS/IPv4 " : "", | |
1909 | link_info.scopes_mask & SD_RESOLVED_MDNS_IPV6 ? "mDNS/IPv6 " : "") < 0) | |
1910 | return log_oom(); | |
1911 | ||
1912 | len = strlen(buf); | |
1913 | assert(len > 0); | |
1914 | buf[len - 1] = '\0'; | |
1915 | ||
1916 | r = table_add_cell(table, NULL, TABLE_STRING, buf); | |
1917 | } | |
1918 | if (r < 0) | |
1919 | return table_log_add_error(r); | |
1920 | ||
1921 | _cleanup_strv_free_ char **pstatus = link_protocol_status(&link_info); | |
1922 | if (!pstatus) | |
1923 | return log_oom(); | |
1924 | ||
1925 | r = table_add_many(table, | |
1926 | TABLE_FIELD, "Protocols", | |
1927 | TABLE_STRV_WRAPPED, pstatus); | |
1928 | if (r < 0) | |
1929 | return table_log_add_error(r); | |
1930 | ||
1931 | if (link_info.current_dns) { | |
1932 | r = table_add_many(table, | |
1933 | TABLE_FIELD, "Current DNS Server", | |
1934 | TABLE_STRING, link_info.current_dns_ex ?: link_info.current_dns); | |
1935 | if (r < 0) | |
1936 | return table_log_add_error(r); | |
1937 | } | |
1938 | ||
1939 | r = dump_list(table, "DNS Servers", link_info.dns_ex ?: link_info.dns); | |
1940 | if (r < 0) | |
1941 | return r; | |
1942 | ||
1943 | r = dump_list(table, "DNS Domain", link_info.domains); | |
1944 | if (r < 0) | |
1945 | return r; | |
1946 | ||
1947 | r = table_add_many(table, | |
1948 | TABLE_FIELD, "Default Route", | |
1949 | TABLE_BOOLEAN, link_info.default_route); | |
1950 | if (r < 0) | |
1951 | return table_log_add_error(r); | |
1952 | ||
1953 | r = table_print(table, NULL); | |
1954 | if (r < 0) | |
1955 | return table_log_print_error(r); | |
1956 | ||
1957 | if (empty_line) | |
1958 | *empty_line = true; | |
1959 | ||
1960 | return 0; | |
1961 | } | |
1962 | ||
1963 | static int map_global_dns_servers_internal( | |
1964 | sd_bus *bus, | |
1965 | const char *member, | |
1966 | sd_bus_message *m, | |
1967 | sd_bus_error *error, | |
1968 | void *userdata, | |
1969 | ReadDNSFlag flags) { | |
1970 | ||
1971 | char ***l = ASSERT_PTR(userdata); | |
1972 | int r; | |
1973 | ||
1974 | assert(bus); | |
1975 | assert(member); | |
1976 | assert(m); | |
1977 | ||
1978 | r = sd_bus_message_enter_container(m, 'a', FLAGS_SET(flags, READ_DNS_EXTENDED) ? "(iiayqs)" : "(iiay)"); | |
1979 | if (r < 0) | |
1980 | return r; | |
1981 | ||
1982 | for (;;) { | |
1983 | _cleanup_free_ char *pretty = NULL; | |
1984 | ||
1985 | r = read_dns_server_one(m, flags, &pretty); | |
1986 | if (r < 0) | |
1987 | return r; | |
1988 | if (r == 0) | |
1989 | break; | |
1990 | ||
1991 | if (isempty(pretty)) | |
1992 | continue; | |
1993 | ||
1994 | r = strv_consume(l, TAKE_PTR(pretty)); | |
1995 | if (r < 0) | |
1996 | return r; | |
1997 | } | |
1998 | ||
1999 | r = sd_bus_message_exit_container(m); | |
2000 | if (r < 0) | |
2001 | return r; | |
2002 | ||
2003 | return 0; | |
2004 | } | |
2005 | ||
2006 | static int map_global_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2007 | return map_global_dns_servers_internal(bus, member, m, error, userdata, READ_DNS_WITH_IFINDEX | READ_DNS_ONLY_GLOBAL); | |
2008 | } | |
2009 | ||
2010 | static int map_global_dns_servers_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2011 | return map_global_dns_servers_internal(bus, member, m, error, userdata, READ_DNS_WITH_IFINDEX | READ_DNS_ONLY_GLOBAL | READ_DNS_EXTENDED); | |
2012 | } | |
2013 | ||
2014 | static int map_global_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2015 | return read_dns_server_one(m, READ_DNS_WITH_IFINDEX | READ_DNS_ONLY_GLOBAL, userdata); | |
2016 | } | |
2017 | ||
2018 | static int map_global_current_dns_server_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2019 | return read_dns_server_one(m, READ_DNS_WITH_IFINDEX | READ_DNS_ONLY_GLOBAL | READ_DNS_EXTENDED, userdata); | |
2020 | } | |
2021 | ||
2022 | static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2023 | return map_domains_internal(bus, member, m, /* with_ifindex= */ true, error, userdata); | |
2024 | } | |
2025 | ||
2026 | static int status_global(sd_bus *bus, StatusMode mode, bool *empty_line) { | |
2027 | static const struct bus_properties_map property_map[] = { | |
2028 | { "DNS", "a(iiay)", map_global_dns_servers, offsetof(GlobalInfo, dns) }, | |
2029 | { "DNSEx", "a(iiayqs)", map_global_dns_servers_ex, offsetof(GlobalInfo, dns_ex) }, | |
2030 | { "FallbackDNS", "a(iiay)", map_global_dns_servers, offsetof(GlobalInfo, fallback_dns) }, | |
2031 | { "FallbackDNSEx", "a(iiayqs)", map_global_dns_servers_ex, offsetof(GlobalInfo, fallback_dns_ex) }, | |
2032 | { "CurrentDNSServer", "(iiay)", map_global_current_dns_server, offsetof(GlobalInfo, current_dns) }, | |
2033 | { "CurrentDNSServerEx", "(iiayqs)", map_global_current_dns_server_ex, offsetof(GlobalInfo, current_dns_ex) }, | |
2034 | { "Domains", "a(isb)", map_global_domains, offsetof(GlobalInfo, domains) }, | |
2035 | { "DNSSECNegativeTrustAnchors", "as", bus_map_strv_sort, offsetof(GlobalInfo, ntas) }, | |
2036 | { "LLMNR", "s", NULL, offsetof(GlobalInfo, llmnr) }, | |
2037 | { "MulticastDNS", "s", NULL, offsetof(GlobalInfo, mdns) }, | |
2038 | { "DNSOverTLS", "s", NULL, offsetof(GlobalInfo, dns_over_tls) }, | |
2039 | { "DNSSEC", "s", NULL, offsetof(GlobalInfo, dnssec) }, | |
2040 | { "DNSSECSupported", "b", NULL, offsetof(GlobalInfo, dnssec_supported) }, | |
2041 | { "ResolvConfMode", "s", NULL, offsetof(GlobalInfo, resolv_conf_mode) }, | |
2042 | {} | |
2043 | }; | |
2044 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2045 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
2046 | _cleanup_(global_info_done) GlobalInfo global_info = {}; | |
2047 | _cleanup_(table_unrefp) Table *table = NULL; | |
2048 | int r; | |
2049 | ||
2050 | assert(bus); | |
2051 | assert(empty_line); | |
2052 | ||
2053 | r = bus_map_all_properties(bus, | |
2054 | "org.freedesktop.resolve1", | |
2055 | "/org/freedesktop/resolve1", | |
2056 | property_map, | |
2057 | BUS_MAP_BOOLEAN_AS_BOOL, | |
2058 | &error, | |
2059 | &m, | |
2060 | &global_info); | |
2061 | if (r < 0) | |
2062 | return log_error_errno(r, "Failed to get global data: %s", bus_error_message(&error, r)); | |
2063 | ||
2064 | pager_open(arg_pager_flags); | |
2065 | ||
2066 | switch (mode) { | |
2067 | ||
2068 | case STATUS_DNS: | |
2069 | return status_print_strv_global(global_info.dns_ex ?: global_info.dns); | |
2070 | ||
2071 | case STATUS_DOMAIN: | |
2072 | return status_print_strv_global(global_info.domains); | |
2073 | ||
2074 | case STATUS_NTA: | |
2075 | return status_print_strv_global(global_info.ntas); | |
2076 | ||
2077 | case STATUS_LLMNR: | |
2078 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), | |
2079 | strna(global_info.llmnr)); | |
2080 | ||
2081 | return 0; | |
2082 | ||
2083 | case STATUS_MDNS: | |
2084 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), | |
2085 | strna(global_info.mdns)); | |
2086 | ||
2087 | return 0; | |
2088 | ||
2089 | case STATUS_PRIVATE: | |
2090 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), | |
2091 | strna(global_info.dns_over_tls)); | |
2092 | ||
2093 | return 0; | |
2094 | ||
2095 | case STATUS_DNSSEC: | |
2096 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), | |
2097 | strna(global_info.dnssec)); | |
2098 | ||
2099 | return 0; | |
2100 | ||
2101 | case STATUS_ALL: | |
2102 | break; | |
2103 | ||
2104 | default: | |
2105 | return 0; | |
2106 | } | |
2107 | ||
2108 | printf("%sGlobal%s\n", ansi_highlight(), ansi_normal()); | |
2109 | ||
2110 | table = table_new_vertical(); | |
2111 | if (!table) | |
2112 | return log_oom(); | |
2113 | ||
2114 | _cleanup_strv_free_ char **pstatus = global_protocol_status(&global_info); | |
2115 | if (!pstatus) | |
2116 | return log_oom(); | |
2117 | ||
2118 | r = table_add_many(table, | |
2119 | TABLE_FIELD, "Protocols", | |
2120 | TABLE_SET_MINIMUM_WIDTH, 19, | |
2121 | TABLE_STRV_WRAPPED, pstatus); | |
2122 | if (r < 0) | |
2123 | return table_log_add_error(r); | |
2124 | ||
2125 | if (global_info.resolv_conf_mode) { | |
2126 | r = table_add_many(table, | |
2127 | TABLE_FIELD, "resolv.conf mode", | |
2128 | TABLE_STRING, global_info.resolv_conf_mode); | |
2129 | if (r < 0) | |
2130 | return table_log_add_error(r); | |
2131 | } | |
2132 | ||
2133 | if (global_info.current_dns) { | |
2134 | r = table_add_many(table, | |
2135 | TABLE_FIELD, "Current DNS Server", | |
2136 | TABLE_STRING, global_info.current_dns_ex ?: global_info.current_dns); | |
2137 | if (r < 0) | |
2138 | return table_log_add_error(r); | |
2139 | } | |
2140 | ||
2141 | r = dump_list(table, "DNS Servers", global_info.dns_ex ?: global_info.dns); | |
2142 | if (r < 0) | |
2143 | return r; | |
2144 | ||
2145 | r = dump_list(table, "Fallback DNS Servers", global_info.fallback_dns_ex ?: global_info.fallback_dns); | |
2146 | if (r < 0) | |
2147 | return r; | |
2148 | ||
2149 | r = dump_list(table, "DNS Domain", global_info.domains); | |
2150 | if (r < 0) | |
2151 | return r; | |
2152 | ||
2153 | r = table_print(table, NULL); | |
2154 | if (r < 0) | |
2155 | return table_log_print_error(r); | |
2156 | ||
2157 | *empty_line = true; | |
2158 | ||
2159 | return 0; | |
2160 | } | |
2161 | ||
2162 | static int status_links(sd_bus *bus, StatusMode mode, bool *empty_line) { | |
2163 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
2164 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
2165 | int ret = 0, r; | |
2166 | ||
2167 | assert(bus); | |
2168 | ||
2169 | r = sd_netlink_open(&rtnl); | |
2170 | if (r < 0) | |
2171 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
2172 | ||
2173 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0); | |
2174 | if (r < 0) | |
2175 | return rtnl_log_create_error(r); | |
2176 | ||
2177 | r = sd_netlink_message_set_request_dump(req, true); | |
2178 | if (r < 0) | |
2179 | return rtnl_log_create_error(r); | |
2180 | ||
2181 | r = sd_netlink_call(rtnl, req, 0, &reply); | |
2182 | if (r < 0) | |
2183 | return log_error_errno(r, "Failed to enumerate links: %m"); | |
2184 | ||
2185 | _cleanup_free_ InterfaceInfo *infos = NULL; | |
2186 | size_t n_infos = 0; | |
2187 | ||
2188 | for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) { | |
2189 | const char *name; | |
2190 | int ifindex; | |
2191 | uint16_t type; | |
2192 | ||
2193 | r = sd_netlink_message_get_type(i, &type); | |
2194 | if (r < 0) | |
2195 | return rtnl_log_parse_error(r); | |
2196 | ||
2197 | if (type != RTM_NEWLINK) | |
2198 | continue; | |
2199 | ||
2200 | r = sd_rtnl_message_link_get_ifindex(i, &ifindex); | |
2201 | if (r < 0) | |
2202 | return rtnl_log_parse_error(r); | |
2203 | ||
2204 | if (ifindex == LOOPBACK_IFINDEX) | |
2205 | continue; | |
2206 | ||
2207 | r = sd_netlink_message_read_string(i, IFLA_IFNAME, &name); | |
2208 | if (r < 0) | |
2209 | return rtnl_log_parse_error(r); | |
2210 | ||
2211 | if (!GREEDY_REALLOC(infos, n_infos + 1)) | |
2212 | return log_oom(); | |
2213 | ||
2214 | infos[n_infos++] = (InterfaceInfo) { ifindex, name }; | |
2215 | } | |
2216 | ||
2217 | typesafe_qsort(infos, n_infos, interface_info_compare); | |
2218 | ||
2219 | FOREACH_ARRAY(info, infos, n_infos) | |
2220 | RET_GATHER(ret, status_ifindex(bus, info->index, info->name, mode, empty_line)); | |
2221 | ||
2222 | return ret; | |
2223 | } | |
2224 | ||
2225 | typedef struct DelegateInfo { | |
2226 | char *current_dns; | |
2227 | char **dns; | |
2228 | char **domains; | |
2229 | bool default_route; | |
2230 | } DelegateInfo; | |
2231 | ||
2232 | static void delegate_info_done(DelegateInfo *p) { | |
2233 | assert(p); | |
2234 | ||
2235 | free(p->current_dns); | |
2236 | strv_free(p->dns); | |
2237 | strv_free(p->domains); | |
2238 | } | |
2239 | ||
2240 | static int map_delegate_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2241 | return map_dns_servers_internal(bus, member, m, READ_DNS_WITH_IFINDEX|READ_DNS_EXTENDED, error, userdata); | |
2242 | } | |
2243 | ||
2244 | static int map_delegate_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2245 | return read_dns_server_one(m, READ_DNS_WITH_IFINDEX|READ_DNS_EXTENDED, userdata); | |
2246 | } | |
2247 | ||
2248 | static int map_delegate_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2249 | return map_domains_internal(bus, member, m, /* with_ifindex= */ false, error, userdata); | |
2250 | } | |
2251 | ||
2252 | static int status_delegate_one(sd_bus *bus, const char *id, StatusMode mode, bool *empty_line) { | |
2253 | ||
2254 | static const struct bus_properties_map property_map[] = { | |
2255 | { "DNS", "a(iiayqs)", map_delegate_dns_servers, offsetof(DelegateInfo, dns) }, | |
2256 | { "CurrentDNSServer", "(iiayqs)", map_delegate_current_dns_server, offsetof(DelegateInfo, current_dns) }, | |
2257 | { "Domains", "a(sb)", map_delegate_domains, offsetof(DelegateInfo, domains) }, | |
2258 | { "DefaultRoute", "b", NULL, offsetof(DelegateInfo, default_route) }, | |
2259 | {} | |
2260 | }; | |
2261 | ||
2262 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2263 | _cleanup_(delegate_info_done) DelegateInfo delegate_info = {}; | |
2264 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
2265 | _cleanup_free_ char *p = NULL; | |
2266 | int r; | |
2267 | ||
2268 | assert(bus); | |
2269 | assert(id); | |
2270 | ||
2271 | r = sd_bus_path_encode("/org/freedesktop/resolve1/dns_delegate", id, &p); | |
2272 | if (r < 0) | |
2273 | return log_oom(); | |
2274 | ||
2275 | r = bus_map_all_properties( | |
2276 | bus, | |
2277 | "org.freedesktop.resolve1", | |
2278 | p, | |
2279 | property_map, | |
2280 | BUS_MAP_BOOLEAN_AS_BOOL, | |
2281 | &error, | |
2282 | &m, | |
2283 | &delegate_info); | |
2284 | if (r < 0) | |
2285 | return log_error_errno(r, "Failed to get delegate data for %s: %s", id, bus_error_message(&error, r)); | |
2286 | ||
2287 | pager_open(arg_pager_flags); | |
2288 | ||
2289 | switch (mode) { | |
2290 | ||
2291 | case STATUS_DNS: | |
2292 | return status_print_strv_delegate(id, delegate_info.dns); | |
2293 | ||
2294 | case STATUS_DOMAIN: | |
2295 | return status_print_strv_delegate(id, delegate_info.domains); | |
2296 | ||
2297 | case STATUS_DEFAULT_ROUTE: | |
2298 | printf("%sDelegate %s%s: %s\n", | |
2299 | ansi_highlight(), id, ansi_normal(), | |
2300 | yes_no(delegate_info.default_route)); | |
2301 | ||
2302 | return 0; | |
2303 | ||
2304 | case STATUS_ALL: | |
2305 | break; | |
2306 | ||
2307 | default: | |
2308 | return 0; | |
2309 | } | |
2310 | ||
2311 | if (empty_line && *empty_line) | |
2312 | fputc('\n', stdout); | |
2313 | ||
2314 | printf("%sDelegate %s%s\n", | |
2315 | ansi_highlight(), id, ansi_normal()); | |
2316 | ||
2317 | _cleanup_(table_unrefp) Table *table = table_new_vertical(); | |
2318 | if (!table) | |
2319 | return log_oom(); | |
2320 | ||
2321 | if (delegate_info.current_dns) { | |
2322 | r = table_add_many(table, | |
2323 | TABLE_FIELD, "Current DNS Server", | |
2324 | TABLE_STRING, delegate_info.current_dns); | |
2325 | if (r < 0) | |
2326 | return table_log_add_error(r); | |
2327 | } | |
2328 | ||
2329 | r = dump_list(table, "DNS Servers", delegate_info.dns); | |
2330 | if (r < 0) | |
2331 | return r; | |
2332 | ||
2333 | r = dump_list(table, "DNS Domain", delegate_info.domains); | |
2334 | if (r < 0) | |
2335 | return r; | |
2336 | ||
2337 | r = table_add_many(table, | |
2338 | TABLE_FIELD, "Default Route", | |
2339 | TABLE_SET_MINIMUM_WIDTH, 19, | |
2340 | TABLE_BOOLEAN, delegate_info.default_route); | |
2341 | ||
2342 | r = table_print(table, NULL); | |
2343 | if (r < 0) | |
2344 | return table_log_print_error(r); | |
2345 | ||
2346 | if (empty_line) | |
2347 | *empty_line = true; | |
2348 | ||
2349 | return 0; | |
2350 | } | |
2351 | ||
2352 | static int status_delegates(sd_bus *bus, StatusMode mode, bool *empty_line) { | |
2353 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
2354 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2355 | int r, ret = 0; | |
2356 | ||
2357 | assert(bus); | |
2358 | ||
2359 | r = bus_call_method(bus, bus_resolve_mgr, "ListDelegates", &error, &reply, NULL); | |
2360 | if (r < 0) { | |
2361 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
2362 | log_debug("Delegates not supported, skipping."); | |
2363 | return 0; | |
2364 | } | |
2365 | return log_error_errno(r, "Failed to list delegates: %s", bus_error_message(&error, r)); | |
2366 | } | |
2367 | ||
2368 | r = sd_bus_message_enter_container(reply, 'a', "(so)"); | |
2369 | if (r < 0) | |
2370 | return bus_log_parse_error(r); | |
2371 | ||
2372 | _cleanup_strv_free_ char **l = NULL; | |
2373 | for (;;) { | |
2374 | const char *id; | |
2375 | ||
2376 | r = sd_bus_message_read(reply, "(so)", &id, NULL); | |
2377 | if (r < 0) | |
2378 | return bus_log_parse_error(r); | |
2379 | if (r == 0) | |
2380 | break; | |
2381 | ||
2382 | if (strv_extend(&l, id) < 0) | |
2383 | return log_oom(); | |
2384 | } | |
2385 | ||
2386 | r = sd_bus_message_exit_container(reply); | |
2387 | if (r < 0) | |
2388 | return bus_log_parse_error(r); | |
2389 | ||
2390 | strv_sort(l); | |
2391 | ||
2392 | STRV_FOREACH(i, l) | |
2393 | RET_GATHER(ret, status_delegate_one(bus, *i, mode, empty_line)); | |
2394 | ||
2395 | return ret; | |
2396 | } | |
2397 | ||
2398 | static int status_all(sd_bus *bus, StatusMode mode) { | |
2399 | bool empty_line = false; | |
2400 | int r; | |
2401 | ||
2402 | assert(bus); | |
2403 | ||
2404 | r = status_global(bus, mode, &empty_line); | |
2405 | if (r < 0) | |
2406 | return r; | |
2407 | ||
2408 | r = status_links(bus, mode, &empty_line); | |
2409 | if (r < 0) | |
2410 | return r; | |
2411 | ||
2412 | r = status_delegates(bus, mode, &empty_line); | |
2413 | if (r < 0) | |
2414 | return r; | |
2415 | ||
2416 | return 0; | |
2417 | } | |
2418 | ||
2419 | static int verb_status(int argc, char **argv, void *userdata) { | |
2420 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
2421 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
2422 | bool empty_line = false; | |
2423 | int r, ret = 0; | |
2424 | ||
2425 | r = acquire_bus(&bus); | |
2426 | if (r < 0) | |
2427 | return r; | |
2428 | ||
2429 | if (argc <= 1) | |
2430 | return status_all(bus, STATUS_ALL); | |
2431 | ||
2432 | STRV_FOREACH(ifname, strv_skip(argv, 1)) { | |
2433 | int ifindex; | |
2434 | ||
2435 | ifindex = rtnl_resolve_interface(&rtnl, *ifname); | |
2436 | if (ifindex < 0) { | |
2437 | log_warning_errno(ifindex, "Failed to resolve interface \"%s\", ignoring: %m", *ifname); | |
2438 | continue; | |
2439 | } | |
2440 | ||
2441 | RET_GATHER(ret, status_ifindex(bus, ifindex, NULL, STATUS_ALL, &empty_line)); | |
2442 | } | |
2443 | ||
2444 | return ret; | |
2445 | } | |
2446 | ||
2447 | static int call_dns(sd_bus *bus, char **dns, const BusLocator *locator, sd_bus_error *error, bool extended) { | |
2448 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL; | |
2449 | int r; | |
2450 | ||
2451 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
2452 | ||
2453 | r = bus_message_new_method_call(bus, &req, locator, extended ? "SetLinkDNSEx" : "SetLinkDNS"); | |
2454 | if (r < 0) | |
2455 | return bus_log_create_error(r); | |
2456 | ||
2457 | r = sd_bus_message_append(req, "i", arg_ifindex); | |
2458 | if (r < 0) | |
2459 | return bus_log_create_error(r); | |
2460 | ||
2461 | r = sd_bus_message_open_container(req, 'a', extended ? "(iayqs)" : "(iay)"); | |
2462 | if (r < 0) | |
2463 | return bus_log_create_error(r); | |
2464 | ||
2465 | /* If only argument is the empty string, then call SetLinkDNS() with an | |
2466 | * empty list, which will clear the list of domains for an interface. */ | |
2467 | if (!strv_equal(dns, STRV_MAKE(""))) | |
2468 | STRV_FOREACH(p, dns) { | |
2469 | _cleanup_free_ char *name = NULL; | |
2470 | struct in_addr_data data; | |
2471 | uint16_t port; | |
2472 | int ifindex; | |
2473 | ||
2474 | r = in_addr_port_ifindex_name_from_string_auto(*p, &data.family, &data.address, &port, &ifindex, &name); | |
2475 | if (r < 0) | |
2476 | return log_error_errno(r, "Failed to parse DNS server address: %s", *p); | |
2477 | ||
2478 | if (ifindex != 0 && ifindex != arg_ifindex) | |
2479 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid ifindex: %i", ifindex); | |
2480 | ||
2481 | r = sd_bus_message_open_container(req, 'r', extended ? "iayqs" : "iay"); | |
2482 | if (r < 0) | |
2483 | return bus_log_create_error(r); | |
2484 | ||
2485 | r = sd_bus_message_append(req, "i", data.family); | |
2486 | if (r < 0) | |
2487 | return bus_log_create_error(r); | |
2488 | ||
2489 | r = sd_bus_message_append_array(req, 'y', &data.address, FAMILY_ADDRESS_SIZE(data.family)); | |
2490 | if (r < 0) | |
2491 | return bus_log_create_error(r); | |
2492 | ||
2493 | if (extended) { | |
2494 | r = sd_bus_message_append(req, "q", port); | |
2495 | if (r < 0) | |
2496 | return bus_log_create_error(r); | |
2497 | ||
2498 | r = sd_bus_message_append(req, "s", name); | |
2499 | if (r < 0) | |
2500 | return bus_log_create_error(r); | |
2501 | } | |
2502 | ||
2503 | r = sd_bus_message_close_container(req); | |
2504 | if (r < 0) | |
2505 | return bus_log_create_error(r); | |
2506 | } | |
2507 | ||
2508 | r = sd_bus_message_close_container(req); | |
2509 | if (r < 0) | |
2510 | return bus_log_create_error(r); | |
2511 | ||
2512 | r = sd_bus_call(bus, req, 0, error, NULL); | |
2513 | if (r < 0 && extended && sd_bus_error_has_name(error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
2514 | sd_bus_error_free(error); | |
2515 | return call_dns(bus, dns, locator, error, false); | |
2516 | } | |
2517 | return r; | |
2518 | } | |
2519 | ||
2520 | static int verb_dns(int argc, char **argv, void *userdata) { | |
2521 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
2522 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2523 | int r; | |
2524 | ||
2525 | r = acquire_bus(&bus); | |
2526 | if (r < 0) | |
2527 | return r; | |
2528 | ||
2529 | if (argc >= 2) { | |
2530 | r = ifname_mangle(argv[1]); | |
2531 | if (r < 0) | |
2532 | return r; | |
2533 | } | |
2534 | ||
2535 | if (arg_ifindex <= 0) | |
2536 | return status_all(bus, STATUS_DNS); | |
2537 | ||
2538 | if (argc < 3) | |
2539 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DNS, NULL); | |
2540 | ||
2541 | char **args = strv_skip(argv, 2); | |
2542 | r = call_dns(bus, args, bus_resolve_mgr, &error, true); | |
2543 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { | |
2544 | sd_bus_error_free(&error); | |
2545 | ||
2546 | r = call_dns(bus, args, bus_network_mgr, &error, true); | |
2547 | } | |
2548 | if (r < 0) { | |
2549 | if (arg_ifindex_permissive && | |
2550 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2551 | return 0; | |
2552 | ||
2553 | return log_error_errno(r, "Failed to set DNS configuration: %s", bus_error_message(&error, r)); | |
2554 | } | |
2555 | ||
2556 | return 0; | |
2557 | } | |
2558 | ||
2559 | static int call_domain(sd_bus *bus, char **domain, const BusLocator *locator, sd_bus_error *error) { | |
2560 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL; | |
2561 | int r; | |
2562 | ||
2563 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
2564 | ||
2565 | r = bus_message_new_method_call(bus, &req, locator, "SetLinkDomains"); | |
2566 | if (r < 0) | |
2567 | return bus_log_create_error(r); | |
2568 | ||
2569 | r = sd_bus_message_append(req, "i", arg_ifindex); | |
2570 | if (r < 0) | |
2571 | return bus_log_create_error(r); | |
2572 | ||
2573 | r = sd_bus_message_open_container(req, 'a', "(sb)"); | |
2574 | if (r < 0) | |
2575 | return bus_log_create_error(r); | |
2576 | ||
2577 | /* If only argument is the empty string, then call SetLinkDomains() with an | |
2578 | * empty list, which will clear the list of domains for an interface. */ | |
2579 | if (!strv_equal(domain, STRV_MAKE(""))) | |
2580 | STRV_FOREACH(p, domain) { | |
2581 | const char *n; | |
2582 | ||
2583 | n = **p == '~' ? *p + 1 : *p; | |
2584 | ||
2585 | r = dns_name_is_valid(n); | |
2586 | if (r < 0) | |
2587 | return log_error_errno(r, "Failed to validate specified domain %s: %m", n); | |
2588 | if (r == 0) | |
2589 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2590 | "Domain not valid: %s", | |
2591 | n); | |
2592 | ||
2593 | r = sd_bus_message_append(req, "(sb)", n, **p == '~'); | |
2594 | if (r < 0) | |
2595 | return bus_log_create_error(r); | |
2596 | } | |
2597 | ||
2598 | r = sd_bus_message_close_container(req); | |
2599 | if (r < 0) | |
2600 | return bus_log_create_error(r); | |
2601 | ||
2602 | return sd_bus_call(bus, req, 0, error, NULL); | |
2603 | } | |
2604 | ||
2605 | static int verb_domain(int argc, char **argv, void *userdata) { | |
2606 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
2607 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2608 | int r; | |
2609 | ||
2610 | r = acquire_bus(&bus); | |
2611 | if (r < 0) | |
2612 | return r; | |
2613 | ||
2614 | if (argc >= 2) { | |
2615 | r = ifname_mangle(argv[1]); | |
2616 | if (r < 0) | |
2617 | return r; | |
2618 | } | |
2619 | ||
2620 | if (arg_ifindex <= 0) | |
2621 | return status_all(bus, STATUS_DOMAIN); | |
2622 | ||
2623 | if (argc < 3) | |
2624 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DOMAIN, NULL); | |
2625 | ||
2626 | char **args = strv_skip(argv, 2); | |
2627 | r = call_domain(bus, args, bus_resolve_mgr, &error); | |
2628 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { | |
2629 | sd_bus_error_free(&error); | |
2630 | ||
2631 | r = call_domain(bus, args, bus_network_mgr, &error); | |
2632 | } | |
2633 | if (r < 0) { | |
2634 | if (arg_ifindex_permissive && | |
2635 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2636 | return 0; | |
2637 | ||
2638 | return log_error_errno(r, "Failed to set domain configuration: %s", bus_error_message(&error, r)); | |
2639 | } | |
2640 | ||
2641 | return 0; | |
2642 | } | |
2643 | ||
2644 | static int verb_default_route(int argc, char **argv, void *userdata) { | |
2645 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
2646 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2647 | int r, b; | |
2648 | ||
2649 | r = acquire_bus(&bus); | |
2650 | if (r < 0) | |
2651 | return r; | |
2652 | ||
2653 | if (argc >= 2) { | |
2654 | r = ifname_mangle(argv[1]); | |
2655 | if (r < 0) | |
2656 | return r; | |
2657 | } | |
2658 | ||
2659 | if (arg_ifindex <= 0) | |
2660 | return status_all(bus, STATUS_DEFAULT_ROUTE); | |
2661 | ||
2662 | if (argc < 3) | |
2663 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DEFAULT_ROUTE, NULL); | |
2664 | ||
2665 | b = parse_boolean(argv[2]); | |
2666 | if (b < 0) | |
2667 | return log_error_errno(b, "Failed to parse boolean argument: %s", argv[2]); | |
2668 | ||
2669 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
2670 | ||
2671 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDefaultRoute", &error, NULL, "ib", arg_ifindex, b); | |
2672 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { | |
2673 | sd_bus_error_free(&error); | |
2674 | ||
2675 | r = bus_call_method(bus, bus_network_mgr, "SetLinkDefaultRoute", &error, NULL, "ib", arg_ifindex, b); | |
2676 | } | |
2677 | if (r < 0) { | |
2678 | if (arg_ifindex_permissive && | |
2679 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2680 | return 0; | |
2681 | ||
2682 | return log_error_errno(r, "Failed to set default route configuration: %s", bus_error_message(&error, r)); | |
2683 | } | |
2684 | ||
2685 | return 0; | |
2686 | } | |
2687 | ||
2688 | static int verb_llmnr(int argc, char **argv, void *userdata) { | |
2689 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
2690 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2691 | _cleanup_free_ char *global_llmnr_support_str = NULL; | |
2692 | ResolveSupport global_llmnr_support, llmnr_support; | |
2693 | int r; | |
2694 | ||
2695 | r = acquire_bus(&bus); | |
2696 | if (r < 0) | |
2697 | return r; | |
2698 | ||
2699 | if (argc >= 2) { | |
2700 | r = ifname_mangle(argv[1]); | |
2701 | if (r < 0) | |
2702 | return r; | |
2703 | } | |
2704 | ||
2705 | if (arg_ifindex <= 0) | |
2706 | return status_all(bus, STATUS_LLMNR); | |
2707 | ||
2708 | if (argc < 3) | |
2709 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_LLMNR, NULL); | |
2710 | ||
2711 | llmnr_support = resolve_support_from_string(argv[2]); | |
2712 | if (llmnr_support < 0) | |
2713 | return log_error_errno(llmnr_support, "Invalid LLMNR setting: %s", argv[2]); | |
2714 | ||
2715 | r = bus_get_property_string(bus, bus_resolve_mgr, "LLMNR", &error, &global_llmnr_support_str); | |
2716 | if (r < 0) | |
2717 | return log_error_errno(r, "Failed to get the global LLMNR support state: %s", bus_error_message(&error, r)); | |
2718 | ||
2719 | global_llmnr_support = resolve_support_from_string(global_llmnr_support_str); | |
2720 | if (global_llmnr_support < 0) | |
2721 | return log_error_errno(global_llmnr_support, "Received invalid global LLMNR setting: %s", global_llmnr_support_str); | |
2722 | ||
2723 | if (global_llmnr_support < llmnr_support) | |
2724 | log_warning("Setting LLMNR support level \"%s\" for \"%s\", but the global support level is \"%s\".", | |
2725 | argv[2], arg_ifname, global_llmnr_support_str); | |
2726 | ||
2727 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
2728 | ||
2729 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkLLMNR", &error, NULL, "is", arg_ifindex, argv[2]); | |
2730 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { | |
2731 | sd_bus_error_free(&error); | |
2732 | ||
2733 | r = bus_call_method(bus, bus_network_mgr, "SetLinkLLMNR", &error, NULL, "is", arg_ifindex, argv[2]); | |
2734 | } | |
2735 | if (r < 0) { | |
2736 | if (arg_ifindex_permissive && | |
2737 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2738 | return 0; | |
2739 | ||
2740 | return log_error_errno(r, "Failed to set LLMNR configuration: %s", bus_error_message(&error, r)); | |
2741 | } | |
2742 | ||
2743 | return 0; | |
2744 | } | |
2745 | ||
2746 | static int verb_mdns(int argc, char **argv, void *userdata) { | |
2747 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
2748 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2749 | _cleanup_free_ char *global_mdns_support_str = NULL; | |
2750 | ResolveSupport global_mdns_support, mdns_support; | |
2751 | int r; | |
2752 | ||
2753 | r = acquire_bus(&bus); | |
2754 | if (r < 0) | |
2755 | return r; | |
2756 | ||
2757 | if (argc >= 2) { | |
2758 | r = ifname_mangle(argv[1]); | |
2759 | if (r < 0) | |
2760 | return r; | |
2761 | } | |
2762 | ||
2763 | if (arg_ifindex <= 0) | |
2764 | return status_all(bus, STATUS_MDNS); | |
2765 | ||
2766 | if (argc < 3) | |
2767 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_MDNS, NULL); | |
2768 | ||
2769 | mdns_support = resolve_support_from_string(argv[2]); | |
2770 | if (mdns_support < 0) | |
2771 | return log_error_errno(mdns_support, "Invalid mDNS setting: %s", argv[2]); | |
2772 | ||
2773 | r = bus_get_property_string(bus, bus_resolve_mgr, "MulticastDNS", &error, &global_mdns_support_str); | |
2774 | if (r < 0) | |
2775 | return log_error_errno(r, "Failed to get the global mDNS support state: %s", bus_error_message(&error, r)); | |
2776 | ||
2777 | global_mdns_support = resolve_support_from_string(global_mdns_support_str); | |
2778 | if (global_mdns_support < 0) | |
2779 | return log_error_errno(global_mdns_support, "Received invalid global mDNS setting: %s", global_mdns_support_str); | |
2780 | ||
2781 | if (global_mdns_support < mdns_support) | |
2782 | log_warning("Setting mDNS support level \"%s\" for \"%s\", but the global support level is \"%s\".", | |
2783 | argv[2], arg_ifname, global_mdns_support_str); | |
2784 | ||
2785 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
2786 | ||
2787 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkMulticastDNS", &error, NULL, "is", arg_ifindex, argv[2]); | |
2788 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { | |
2789 | sd_bus_error_free(&error); | |
2790 | ||
2791 | r = bus_call_method( | |
2792 | bus, | |
2793 | bus_network_mgr, | |
2794 | "SetLinkMulticastDNS", | |
2795 | &error, | |
2796 | NULL, | |
2797 | "is", arg_ifindex, argv[2]); | |
2798 | } | |
2799 | if (r < 0) { | |
2800 | if (arg_ifindex_permissive && | |
2801 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2802 | return 0; | |
2803 | ||
2804 | return log_error_errno(r, "Failed to set MulticastDNS configuration: %s", bus_error_message(&error, r)); | |
2805 | } | |
2806 | ||
2807 | return 0; | |
2808 | } | |
2809 | ||
2810 | static int verb_dns_over_tls(int argc, char **argv, void *userdata) { | |
2811 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
2812 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2813 | int r; | |
2814 | ||
2815 | r = acquire_bus(&bus); | |
2816 | if (r < 0) | |
2817 | return r; | |
2818 | ||
2819 | if (argc >= 2) { | |
2820 | r = ifname_mangle(argv[1]); | |
2821 | if (r < 0) | |
2822 | return r; | |
2823 | } | |
2824 | ||
2825 | if (arg_ifindex <= 0) | |
2826 | return status_all(bus, STATUS_PRIVATE); | |
2827 | ||
2828 | if (argc < 3) | |
2829 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_PRIVATE, NULL); | |
2830 | ||
2831 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
2832 | ||
2833 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDNSOverTLS", &error, NULL, "is", arg_ifindex, argv[2]); | |
2834 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { | |
2835 | sd_bus_error_free(&error); | |
2836 | ||
2837 | r = bus_call_method( | |
2838 | bus, | |
2839 | bus_network_mgr, | |
2840 | "SetLinkDNSOverTLS", | |
2841 | &error, | |
2842 | NULL, | |
2843 | "is", arg_ifindex, argv[2]); | |
2844 | } | |
2845 | if (r < 0) { | |
2846 | if (arg_ifindex_permissive && | |
2847 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2848 | return 0; | |
2849 | ||
2850 | return log_error_errno(r, "Failed to set DNSOverTLS configuration: %s", bus_error_message(&error, r)); | |
2851 | } | |
2852 | ||
2853 | return 0; | |
2854 | } | |
2855 | ||
2856 | static int verb_dnssec(int argc, char **argv, void *userdata) { | |
2857 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
2858 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2859 | int r; | |
2860 | ||
2861 | r = acquire_bus(&bus); | |
2862 | if (r < 0) | |
2863 | return r; | |
2864 | ||
2865 | if (argc >= 2) { | |
2866 | r = ifname_mangle(argv[1]); | |
2867 | if (r < 0) | |
2868 | return r; | |
2869 | } | |
2870 | ||
2871 | if (arg_ifindex <= 0) | |
2872 | return status_all(bus, STATUS_DNSSEC); | |
2873 | ||
2874 | if (argc < 3) | |
2875 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DNSSEC, NULL); | |
2876 | ||
2877 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
2878 | ||
2879 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDNSSEC", &error, NULL, "is", arg_ifindex, argv[2]); | |
2880 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { | |
2881 | sd_bus_error_free(&error); | |
2882 | ||
2883 | r = bus_call_method(bus, bus_network_mgr, "SetLinkDNSSEC", &error, NULL, "is", arg_ifindex, argv[2]); | |
2884 | } | |
2885 | if (r < 0) { | |
2886 | if (arg_ifindex_permissive && | |
2887 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2888 | return 0; | |
2889 | ||
2890 | return log_error_errno(r, "Failed to set DNSSEC configuration: %s", bus_error_message(&error, r)); | |
2891 | } | |
2892 | ||
2893 | return 0; | |
2894 | } | |
2895 | ||
2896 | static int call_nta(sd_bus *bus, char **nta, const BusLocator *locator, sd_bus_error *error) { | |
2897 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL; | |
2898 | int r; | |
2899 | ||
2900 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
2901 | ||
2902 | r = bus_message_new_method_call(bus, &req, locator, "SetLinkDNSSECNegativeTrustAnchors"); | |
2903 | if (r < 0) | |
2904 | return bus_log_create_error(r); | |
2905 | ||
2906 | r = sd_bus_message_append(req, "i", arg_ifindex); | |
2907 | if (r < 0) | |
2908 | return bus_log_create_error(r); | |
2909 | ||
2910 | r = sd_bus_message_append_strv(req, nta); | |
2911 | if (r < 0) | |
2912 | return bus_log_create_error(r); | |
2913 | ||
2914 | return sd_bus_call(bus, req, 0, error, NULL); | |
2915 | } | |
2916 | ||
2917 | static int verb_nta(int argc, char **argv, void *userdata) { | |
2918 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
2919 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2920 | char **args; | |
2921 | bool clear; | |
2922 | int r; | |
2923 | ||
2924 | r = acquire_bus(&bus); | |
2925 | if (r < 0) | |
2926 | return r; | |
2927 | ||
2928 | if (argc >= 2) { | |
2929 | r = ifname_mangle(argv[1]); | |
2930 | if (r < 0) | |
2931 | return r; | |
2932 | } | |
2933 | ||
2934 | if (arg_ifindex <= 0) | |
2935 | return status_all(bus, STATUS_NTA); | |
2936 | ||
2937 | if (argc < 3) | |
2938 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_NTA, NULL); | |
2939 | ||
2940 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
2941 | ||
2942 | /* If only argument is the empty string, then call SetLinkDNSSECNegativeTrustAnchors() | |
2943 | * with an empty list, which will clear the list of domains for an interface. */ | |
2944 | args = strv_skip(argv, 2); | |
2945 | clear = strv_equal(args, STRV_MAKE("")); | |
2946 | ||
2947 | if (!clear) | |
2948 | STRV_FOREACH(p, args) { | |
2949 | r = dns_name_is_valid(*p); | |
2950 | if (r < 0) | |
2951 | return log_error_errno(r, "Failed to validate specified domain %s: %m", *p); | |
2952 | if (r == 0) | |
2953 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2954 | "Domain not valid: %s", | |
2955 | *p); | |
2956 | } | |
2957 | ||
2958 | r = call_nta(bus, clear ? NULL : args, bus_resolve_mgr, &error); | |
2959 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { | |
2960 | sd_bus_error_free(&error); | |
2961 | ||
2962 | r = call_nta(bus, clear ? NULL : args, bus_network_mgr, &error); | |
2963 | } | |
2964 | if (r < 0) { | |
2965 | if (arg_ifindex_permissive && | |
2966 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2967 | return 0; | |
2968 | ||
2969 | return log_error_errno(r, "Failed to set DNSSEC NTA configuration: %s", bus_error_message(&error, r)); | |
2970 | } | |
2971 | ||
2972 | return 0; | |
2973 | } | |
2974 | ||
2975 | static int verb_revert_link(int argc, char **argv, void *userdata) { | |
2976 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
2977 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2978 | int r; | |
2979 | ||
2980 | r = acquire_bus(&bus); | |
2981 | if (r < 0) | |
2982 | return r; | |
2983 | ||
2984 | if (argc >= 2) { | |
2985 | r = ifname_mangle(argv[1]); | |
2986 | if (r < 0) | |
2987 | return r; | |
2988 | } | |
2989 | ||
2990 | if (arg_ifindex <= 0) | |
2991 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Interface argument required."); | |
2992 | ||
2993 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
2994 | ||
2995 | r = bus_call_method(bus, bus_resolve_mgr, "RevertLink", &error, NULL, "i", arg_ifindex); | |
2996 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { | |
2997 | sd_bus_error_free(&error); | |
2998 | ||
2999 | r = bus_call_method(bus, bus_network_mgr, "RevertLinkDNS", &error, NULL, "i", arg_ifindex); | |
3000 | } | |
3001 | if (r < 0) { | |
3002 | if (arg_ifindex_permissive && | |
3003 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
3004 | return 0; | |
3005 | ||
3006 | return log_error_errno(r, "Failed to revert interface configuration: %s", bus_error_message(&error, r)); | |
3007 | } | |
3008 | ||
3009 | return 0; | |
3010 | } | |
3011 | ||
3012 | static int verb_log_level(int argc, char *argv[], void *userdata) { | |
3013 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
3014 | int r; | |
3015 | ||
3016 | r = acquire_bus(&bus); | |
3017 | if (r < 0) | |
3018 | return r; | |
3019 | ||
3020 | assert(IN_SET(argc, 1, 2)); | |
3021 | ||
3022 | return verb_log_control_common(bus, "org.freedesktop.resolve1", argv[0], argc == 2 ? argv[1] : NULL); | |
3023 | } | |
3024 | ||
3025 | static int print_question(char prefix, const char *color, sd_json_variant *question) { | |
3026 | sd_json_variant *q = NULL; | |
3027 | int r; | |
3028 | ||
3029 | assert(color); | |
3030 | ||
3031 | JSON_VARIANT_ARRAY_FOREACH(q, question) { | |
3032 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; | |
3033 | char buf[DNS_RESOURCE_KEY_STRING_MAX]; | |
3034 | ||
3035 | r = dns_resource_key_from_json(q, &key); | |
3036 | if (r < 0) { | |
3037 | log_warning_errno(r, "Received monitor message with invalid question key, ignoring: %m"); | |
3038 | continue; | |
3039 | } | |
3040 | ||
3041 | printf("%s%s %c%s: %s\n", | |
3042 | color, | |
3043 | glyph(GLYPH_ARROW_RIGHT), | |
3044 | prefix, | |
3045 | ansi_normal(), | |
3046 | dns_resource_key_to_string(key, buf, sizeof(buf))); | |
3047 | } | |
3048 | ||
3049 | return 0; | |
3050 | } | |
3051 | ||
3052 | static int print_answer(sd_json_variant *answer) { | |
3053 | sd_json_variant *a; | |
3054 | int r; | |
3055 | ||
3056 | JSON_VARIANT_ARRAY_FOREACH(a, answer) { | |
3057 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
3058 | _cleanup_free_ void *d = NULL; | |
3059 | sd_json_variant *jraw; | |
3060 | const char *s; | |
3061 | size_t l; | |
3062 | ||
3063 | jraw = sd_json_variant_by_key(a, "raw"); | |
3064 | if (!jraw) { | |
3065 | log_warning("Received monitor answer lacking valid raw data, ignoring."); | |
3066 | continue; | |
3067 | } | |
3068 | ||
3069 | r = sd_json_variant_unbase64(jraw, &d, &l); | |
3070 | if (r < 0) { | |
3071 | log_warning_errno(r, "Failed to undo base64 encoding of monitor answer raw data, ignoring."); | |
3072 | continue; | |
3073 | } | |
3074 | ||
3075 | r = dns_resource_record_new_from_raw(&rr, d, l); | |
3076 | if (r < 0) { | |
3077 | log_warning_errno(r, "Failed to parse monitor answer RR, ignoring: %m"); | |
3078 | continue; | |
3079 | } | |
3080 | ||
3081 | s = dns_resource_record_to_string(rr); | |
3082 | if (!s) | |
3083 | return log_oom(); | |
3084 | ||
3085 | printf("%s%s A%s: %s\n", | |
3086 | ansi_highlight_yellow(), | |
3087 | glyph(GLYPH_ARROW_LEFT), | |
3088 | ansi_normal(), | |
3089 | s); | |
3090 | } | |
3091 | ||
3092 | return 0; | |
3093 | } | |
3094 | ||
3095 | typedef struct MonitorQueryParams { | |
3096 | sd_json_variant *question; | |
3097 | sd_json_variant *answer; | |
3098 | sd_json_variant *collected_questions; | |
3099 | int rcode; | |
3100 | int error; | |
3101 | int ede_code; | |
3102 | const char *state; | |
3103 | const char *result; | |
3104 | const char *ede_msg; | |
3105 | } MonitorQueryParams; | |
3106 | ||
3107 | static void monitor_query_params_done(MonitorQueryParams *p) { | |
3108 | assert(p); | |
3109 | ||
3110 | sd_json_variant_unref(p->question); | |
3111 | sd_json_variant_unref(p->answer); | |
3112 | sd_json_variant_unref(p->collected_questions); | |
3113 | } | |
3114 | ||
3115 | static void monitor_query_dump(sd_json_variant *v) { | |
3116 | static const sd_json_dispatch_field dispatch_table[] = { | |
3117 | { "question", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(MonitorQueryParams, question), SD_JSON_MANDATORY }, | |
3118 | { "answer", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(MonitorQueryParams, answer), 0 }, | |
3119 | { "collectedQuestions", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(MonitorQueryParams, collected_questions), 0 }, | |
3120 | { "state", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MonitorQueryParams, state), SD_JSON_MANDATORY }, | |
3121 | { "result", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MonitorQueryParams, result), 0 }, | |
3122 | { "rcode", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(MonitorQueryParams, rcode), 0 }, | |
3123 | { "errno", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(MonitorQueryParams, error), 0 }, | |
3124 | { "extendedDNSErrorCode", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(MonitorQueryParams, ede_code), 0 }, | |
3125 | { "extendedDNSErrorMessage", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MonitorQueryParams, ede_msg), 0 }, | |
3126 | {} | |
3127 | }; | |
3128 | ||
3129 | _cleanup_(monitor_query_params_done) MonitorQueryParams p = { | |
3130 | .rcode = -1, | |
3131 | .ede_code = -1, | |
3132 | }; | |
3133 | ||
3134 | assert(v); | |
3135 | ||
3136 | if (sd_json_dispatch(v, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &p) < 0) | |
3137 | return; | |
3138 | ||
3139 | /* First show the current question */ | |
3140 | print_question('Q', ansi_highlight_cyan(), p.question); | |
3141 | ||
3142 | /* And then show the questions that led to this one in case this was a CNAME chain */ | |
3143 | print_question('C', ansi_highlight_grey(), p.collected_questions); | |
3144 | ||
3145 | printf("%s%s S%s: %s", | |
3146 | streq_ptr(p.state, "success") ? ansi_highlight_green() : ansi_highlight_red(), | |
3147 | glyph(GLYPH_ARROW_LEFT), | |
3148 | ansi_normal(), | |
3149 | strna(streq_ptr(p.state, "errno") ? errno_to_name(p.error) : | |
3150 | streq_ptr(p.state, "rcode-failure") ? dns_rcode_to_string(p.rcode) : | |
3151 | p.state)); | |
3152 | ||
3153 | if (!isempty(p.result)) | |
3154 | printf(": %s", p.result); | |
3155 | ||
3156 | if (p.ede_code >= 0) | |
3157 | printf(" (%s%s%s)", | |
3158 | FORMAT_DNS_EDE_RCODE(p.ede_code), | |
3159 | !isempty(p.ede_msg) ? ": " : "", | |
3160 | strempty(p.ede_msg)); | |
3161 | ||
3162 | puts(""); | |
3163 | ||
3164 | print_answer(p.answer); | |
3165 | } | |
3166 | ||
3167 | static int monitor_reply( | |
3168 | sd_varlink *link, | |
3169 | sd_json_variant *parameters, | |
3170 | const char *error_id, | |
3171 | sd_varlink_reply_flags_t flags, | |
3172 | void *userdata) { | |
3173 | ||
3174 | assert(link); | |
3175 | ||
3176 | if (error_id) { | |
3177 | bool disconnect; | |
3178 | ||
3179 | disconnect = streq(error_id, SD_VARLINK_ERROR_DISCONNECTED); | |
3180 | if (disconnect) | |
3181 | log_info("Disconnected."); | |
3182 | else | |
3183 | log_error("Varlink error: %s", error_id); | |
3184 | ||
3185 | (void) sd_event_exit(ASSERT_PTR(sd_varlink_get_event(link)), disconnect ? EXIT_SUCCESS : EXIT_FAILURE); | |
3186 | return 0; | |
3187 | } | |
3188 | ||
3189 | if (sd_json_variant_by_key(parameters, "ready")) { | |
3190 | /* The first message coming in will just indicate that we are now subscribed. We let our | |
3191 | * caller know if they asked for it. Once the caller sees this they should know that we are | |
3192 | * not going to miss any queries anymore. */ | |
3193 | (void) sd_notify(/* unset_environment=false */ false, "READY=1"); | |
3194 | return 0; | |
3195 | } | |
3196 | ||
3197 | if (!sd_json_format_enabled(arg_json_format_flags)) { | |
3198 | monitor_query_dump(parameters); | |
3199 | printf("\n"); | |
3200 | } else | |
3201 | sd_json_variant_dump(parameters, arg_json_format_flags, NULL, NULL); | |
3202 | ||
3203 | fflush(stdout); | |
3204 | ||
3205 | return 0; | |
3206 | } | |
3207 | ||
3208 | static int verb_monitor(int argc, char *argv[], void *userdata) { | |
3209 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
3210 | _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; | |
3211 | int r, c; | |
3212 | ||
3213 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
3214 | ||
3215 | r = sd_event_default(&event); | |
3216 | if (r < 0) | |
3217 | return log_error_errno(r, "Failed to get event loop: %m"); | |
3218 | ||
3219 | r = sd_event_set_signal_exit(event, true); | |
3220 | if (r < 0) | |
3221 | return log_error_errno(r, "Failed to enable exit on SIGINT/SIGTERM: %m"); | |
3222 | ||
3223 | r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor"); | |
3224 | if (r < 0) | |
3225 | return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m"); | |
3226 | ||
3227 | r = sd_varlink_set_relative_timeout(vl, USEC_INFINITY); /* We want the monitor to run basically forever */ | |
3228 | if (r < 0) | |
3229 | return log_error_errno(r, "Failed to set varlink timeout: %m"); | |
3230 | ||
3231 | r = sd_varlink_attach_event(vl, event, SD_EVENT_PRIORITY_NORMAL); | |
3232 | if (r < 0) | |
3233 | return log_error_errno(r, "Failed to attach varlink connection to event loop: %m"); | |
3234 | ||
3235 | r = sd_varlink_bind_reply(vl, monitor_reply); | |
3236 | if (r < 0) | |
3237 | return log_error_errno(r, "Failed to bind reply callback to varlink connection: %m"); | |
3238 | ||
3239 | r = sd_varlink_observebo( | |
3240 | vl, | |
3241 | "io.systemd.Resolve.Monitor.SubscribeQueryResults", | |
3242 | SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password)); | |
3243 | if (r < 0) | |
3244 | return log_error_errno(r, "Failed to issue SubscribeQueryResults() varlink call: %m"); | |
3245 | ||
3246 | r = sd_event_loop(event); | |
3247 | if (r < 0) | |
3248 | return log_error_errno(r, "Failed to run event loop: %m"); | |
3249 | ||
3250 | r = sd_event_get_exit_code(event, &c); | |
3251 | if (r < 0) | |
3252 | return log_error_errno(r, "Failed to get exit code: %m"); | |
3253 | ||
3254 | return c; | |
3255 | } | |
3256 | ||
3257 | static int dump_cache_item(sd_json_variant *item) { | |
3258 | ||
3259 | struct item_info { | |
3260 | sd_json_variant *key; | |
3261 | sd_json_variant *rrs; | |
3262 | const char *type; | |
3263 | uint64_t until; | |
3264 | } item_info = {}; | |
3265 | ||
3266 | static const sd_json_dispatch_field dispatch_table[] = { | |
3267 | { "key", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct item_info, key), SD_JSON_MANDATORY }, | |
3268 | { "rrs", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant_noref, offsetof(struct item_info, rrs), 0 }, | |
3269 | { "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct item_info, type), 0 }, | |
3270 | { "until", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct item_info, until), 0 }, | |
3271 | {}, | |
3272 | }; | |
3273 | ||
3274 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL; | |
3275 | int r, c = 0; | |
3276 | ||
3277 | r = sd_json_dispatch(item, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &item_info); | |
3278 | if (r < 0) | |
3279 | return r; | |
3280 | ||
3281 | r = dns_resource_key_from_json(item_info.key, &k); | |
3282 | if (r < 0) | |
3283 | return log_error_errno(r, "Failed to turn JSON data to resource key: %m"); | |
3284 | ||
3285 | if (item_info.type) | |
3286 | printf("%s %s%s%s\n", DNS_RESOURCE_KEY_TO_STRING(k), ansi_highlight_red(), item_info.type, ansi_normal()); | |
3287 | else { | |
3288 | sd_json_variant *i; | |
3289 | ||
3290 | JSON_VARIANT_ARRAY_FOREACH(i, item_info.rrs) { | |
3291 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
3292 | _cleanup_free_ void *data = NULL; | |
3293 | sd_json_variant *raw; | |
3294 | size_t size; | |
3295 | ||
3296 | raw = sd_json_variant_by_key(i, "raw"); | |
3297 | if (!raw) | |
3298 | return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "raw field missing from RR JSON data."); | |
3299 | ||
3300 | r = sd_json_variant_unbase64(raw, &data, &size); | |
3301 | if (r < 0) | |
3302 | return log_error_errno(r, "Unable to decode raw RR JSON data: %m"); | |
3303 | ||
3304 | r = dns_resource_record_new_from_raw(&rr, data, size); | |
3305 | if (r < 0) | |
3306 | return log_error_errno(r, "Failed to parse DNS data: %m"); | |
3307 | ||
3308 | printf("%s\n", dns_resource_record_to_string(rr)); | |
3309 | c++; | |
3310 | } | |
3311 | } | |
3312 | ||
3313 | return c; | |
3314 | } | |
3315 | ||
3316 | static int dump_cache_scope(sd_json_variant *scope) { | |
3317 | ||
3318 | struct scope_info { | |
3319 | const char *protocol; | |
3320 | int family; | |
3321 | int ifindex; | |
3322 | const char *ifname; | |
3323 | sd_json_variant *cache; | |
3324 | } scope_info = { | |
3325 | .family = AF_UNSPEC, | |
3326 | }; | |
3327 | sd_json_variant *i; | |
3328 | int r, c = 0; | |
3329 | ||
3330 | static const sd_json_dispatch_field dispatch_table[] = { | |
3331 | { "protocol", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct scope_info, protocol), SD_JSON_MANDATORY }, | |
3332 | { "family", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(struct scope_info, family), 0 }, | |
3333 | { "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(struct scope_info, ifindex), SD_JSON_RELAX }, | |
3334 | { "ifname", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct scope_info, ifname), 0 }, | |
3335 | { "cache", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant_noref, offsetof(struct scope_info, cache), SD_JSON_MANDATORY }, | |
3336 | {}, | |
3337 | }; | |
3338 | ||
3339 | r = sd_json_dispatch(scope, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &scope_info); | |
3340 | if (r < 0) | |
3341 | return r; | |
3342 | ||
3343 | printf("%sScope protocol=%s", ansi_underline(), scope_info.protocol); | |
3344 | ||
3345 | if (scope_info.family != AF_UNSPEC) | |
3346 | printf(" family=%s", af_to_name(scope_info.family)); | |
3347 | ||
3348 | if (scope_info.ifindex > 0) | |
3349 | printf(" ifindex=%i", scope_info.ifindex); | |
3350 | if (scope_info.ifname) | |
3351 | printf(" ifname=%s", scope_info.ifname); | |
3352 | ||
3353 | printf("%s\n", ansi_normal()); | |
3354 | ||
3355 | JSON_VARIANT_ARRAY_FOREACH(i, scope_info.cache) { | |
3356 | r = dump_cache_item(i); | |
3357 | if (r < 0) | |
3358 | return r; | |
3359 | ||
3360 | c += r; | |
3361 | } | |
3362 | ||
3363 | if (c == 0) | |
3364 | printf("%sNo entries.%s\n\n", ansi_grey(), ansi_normal()); | |
3365 | else | |
3366 | printf("\n"); | |
3367 | ||
3368 | return 0; | |
3369 | } | |
3370 | ||
3371 | static int verb_show_cache(int argc, char *argv[], void *userdata) { | |
3372 | sd_json_variant *reply = NULL, *d = NULL; | |
3373 | _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; | |
3374 | int r; | |
3375 | ||
3376 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
3377 | ||
3378 | r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor"); | |
3379 | if (r < 0) | |
3380 | return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m"); | |
3381 | ||
3382 | r = varlink_callbo_and_log( | |
3383 | vl, | |
3384 | "io.systemd.Resolve.Monitor.DumpCache", | |
3385 | &reply, | |
3386 | SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password)); | |
3387 | if (r < 0) | |
3388 | return r; | |
3389 | ||
3390 | d = sd_json_variant_by_key(reply, "dump"); | |
3391 | if (!d) | |
3392 | return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), | |
3393 | "DumpCache() response is missing 'dump' key."); | |
3394 | ||
3395 | if (!sd_json_variant_is_array(d)) | |
3396 | return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), | |
3397 | "DumpCache() response 'dump' field not an array"); | |
3398 | ||
3399 | if (!sd_json_format_enabled(arg_json_format_flags)) { | |
3400 | sd_json_variant *i; | |
3401 | ||
3402 | JSON_VARIANT_ARRAY_FOREACH(i, d) { | |
3403 | r = dump_cache_scope(i); | |
3404 | if (r < 0) | |
3405 | return r; | |
3406 | } | |
3407 | ||
3408 | return 0; | |
3409 | } | |
3410 | ||
3411 | return sd_json_variant_dump(d, arg_json_format_flags, NULL, NULL); | |
3412 | } | |
3413 | ||
3414 | static int dump_server_state(sd_json_variant *server) { | |
3415 | _cleanup_(table_unrefp) Table *table = NULL; | |
3416 | TableCell *cell; | |
3417 | ||
3418 | struct server_state { | |
3419 | const char *server_name; | |
3420 | const char *type; | |
3421 | const char *ifname; | |
3422 | int ifindex; | |
3423 | const char *verified_feature_level; | |
3424 | const char *possible_feature_level; | |
3425 | const char *dnssec_mode; | |
3426 | bool dnssec_supported; | |
3427 | size_t received_udp_fragment_max; | |
3428 | uint64_t n_failed_udp; | |
3429 | uint64_t n_failed_tcp; | |
3430 | bool packet_truncated; | |
3431 | bool packet_bad_opt; | |
3432 | bool packet_rrsig_missing; | |
3433 | bool packet_invalid; | |
3434 | bool packet_do_off; | |
3435 | } server_state = { | |
3436 | .ifindex = -1, | |
3437 | }; | |
3438 | ||
3439 | int r; | |
3440 | ||
3441 | static const sd_json_dispatch_field dispatch_table[] = { | |
3442 | { "Server", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, server_name), SD_JSON_MANDATORY }, | |
3443 | { "Type", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, type), SD_JSON_MANDATORY }, | |
3444 | { "Interface", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, ifname), 0 }, | |
3445 | { "InterfaceIndex", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(struct server_state, ifindex), SD_JSON_RELAX }, | |
3446 | { "VerifiedFeatureLevel", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, verified_feature_level), 0 }, | |
3447 | { "PossibleFeatureLevel", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, possible_feature_level), 0 }, | |
3448 | { "DNSSECMode", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, dnssec_mode), SD_JSON_MANDATORY }, | |
3449 | { "DNSSECSupported", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, dnssec_supported), SD_JSON_MANDATORY }, | |
3450 | { "ReceivedUDPFragmentMax", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct server_state, received_udp_fragment_max), SD_JSON_MANDATORY }, | |
3451 | { "FailedUDPAttempts", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct server_state, n_failed_udp), SD_JSON_MANDATORY }, | |
3452 | { "FailedTCPAttempts", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct server_state, n_failed_tcp), SD_JSON_MANDATORY }, | |
3453 | { "PacketTruncated", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_truncated), SD_JSON_MANDATORY }, | |
3454 | { "PacketBadOpt", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_bad_opt), SD_JSON_MANDATORY }, | |
3455 | { "PacketRRSIGMissing", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_rrsig_missing), SD_JSON_MANDATORY }, | |
3456 | { "PacketInvalid", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_invalid), SD_JSON_MANDATORY }, | |
3457 | { "PacketDoOff", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_do_off), SD_JSON_MANDATORY }, | |
3458 | {}, | |
3459 | }; | |
3460 | ||
3461 | r = sd_json_dispatch(server, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &server_state); | |
3462 | if (r < 0) | |
3463 | return r; | |
3464 | ||
3465 | table = table_new_vertical(); | |
3466 | if (!table) | |
3467 | return log_oom(); | |
3468 | ||
3469 | assert_se(cell = table_get_cell(table, 0, 0)); | |
3470 | (void) table_set_ellipsize_percent(table, cell, 100); | |
3471 | (void) table_set_align_percent(table, cell, 0); | |
3472 | ||
3473 | r = table_add_cell_stringf(table, NULL, "Server: %s", server_state.server_name); | |
3474 | if (r < 0) | |
3475 | return table_log_add_error(r); | |
3476 | ||
3477 | r = table_add_many(table, | |
3478 | TABLE_EMPTY, | |
3479 | TABLE_FIELD, "Type", | |
3480 | TABLE_SET_ALIGN_PERCENT, 100, | |
3481 | TABLE_STRING, server_state.type); | |
3482 | if (r < 0) | |
3483 | return table_log_add_error(r); | |
3484 | ||
3485 | if (server_state.ifname) { | |
3486 | r = table_add_many(table, | |
3487 | TABLE_FIELD, "Interface", | |
3488 | TABLE_STRING, server_state.ifname); | |
3489 | if (r < 0) | |
3490 | return table_log_add_error(r); | |
3491 | } | |
3492 | ||
3493 | if (server_state.ifindex >= 0) { | |
3494 | r = table_add_many(table, | |
3495 | TABLE_FIELD, "Interface Index", | |
3496 | TABLE_INT, server_state.ifindex); | |
3497 | if (r < 0) | |
3498 | return table_log_add_error(r); | |
3499 | } | |
3500 | ||
3501 | if (server_state.verified_feature_level) { | |
3502 | r = table_add_many(table, | |
3503 | TABLE_FIELD, "Verified feature level", | |
3504 | TABLE_STRING, server_state.verified_feature_level); | |
3505 | if (r < 0) | |
3506 | return table_log_add_error(r); | |
3507 | } | |
3508 | ||
3509 | if (server_state.possible_feature_level) { | |
3510 | r = table_add_many(table, | |
3511 | TABLE_FIELD, "Possible feature level", | |
3512 | TABLE_STRING, server_state.possible_feature_level); | |
3513 | if (r < 0) | |
3514 | return table_log_add_error(r); | |
3515 | } | |
3516 | ||
3517 | r = table_add_many(table, | |
3518 | TABLE_FIELD, "DNSSEC Mode", | |
3519 | TABLE_STRING, server_state.dnssec_mode, | |
3520 | TABLE_FIELD, "DNSSEC Supported", | |
3521 | TABLE_STRING, yes_no(server_state.dnssec_supported), | |
3522 | TABLE_FIELD, "Maximum UDP fragment size received", | |
3523 | TABLE_UINT64, server_state.received_udp_fragment_max, | |
3524 | TABLE_FIELD, "Failed UDP attempts", | |
3525 | TABLE_UINT64, server_state.n_failed_udp, | |
3526 | TABLE_FIELD, "Failed TCP attempts", | |
3527 | TABLE_UINT64, server_state.n_failed_tcp, | |
3528 | TABLE_FIELD, "Seen truncated packet", | |
3529 | TABLE_STRING, yes_no(server_state.packet_truncated), | |
3530 | TABLE_FIELD, "Seen OPT RR getting lost", | |
3531 | TABLE_STRING, yes_no(server_state.packet_bad_opt), | |
3532 | TABLE_FIELD, "Seen RRSIG RR missing", | |
3533 | TABLE_STRING, yes_no(server_state.packet_rrsig_missing), | |
3534 | TABLE_FIELD, "Seen invalid packet", | |
3535 | TABLE_STRING, yes_no(server_state.packet_invalid), | |
3536 | TABLE_FIELD, "Server dropped DO flag", | |
3537 | TABLE_STRING, yes_no(server_state.packet_do_off), | |
3538 | TABLE_SET_ALIGN_PERCENT, 0, | |
3539 | TABLE_EMPTY, TABLE_EMPTY); | |
3540 | ||
3541 | if (r < 0) | |
3542 | return table_log_add_error(r); | |
3543 | ||
3544 | r = table_print(table, NULL); | |
3545 | if (r < 0) | |
3546 | return table_log_print_error(r); | |
3547 | ||
3548 | return 0; | |
3549 | } | |
3550 | ||
3551 | static int verb_show_server_state(int argc, char *argv[], void *userdata) { | |
3552 | sd_json_variant *reply = NULL, *d = NULL; | |
3553 | _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; | |
3554 | int r; | |
3555 | ||
3556 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
3557 | ||
3558 | r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor"); | |
3559 | if (r < 0) | |
3560 | return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m"); | |
3561 | ||
3562 | r = varlink_callbo_and_log( | |
3563 | vl, | |
3564 | "io.systemd.Resolve.Monitor.DumpServerState", | |
3565 | &reply, | |
3566 | SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password)); | |
3567 | if (r < 0) | |
3568 | return r; | |
3569 | ||
3570 | d = sd_json_variant_by_key(reply, "dump"); | |
3571 | if (!d) | |
3572 | return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), | |
3573 | "DumpCache() response is missing 'dump' key."); | |
3574 | ||
3575 | if (!sd_json_variant_is_array(d)) | |
3576 | return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), | |
3577 | "DumpCache() response 'dump' field not an array"); | |
3578 | ||
3579 | if (!sd_json_format_enabled(arg_json_format_flags)) { | |
3580 | sd_json_variant *i; | |
3581 | ||
3582 | JSON_VARIANT_ARRAY_FOREACH(i, d) { | |
3583 | r = dump_server_state(i); | |
3584 | if (r < 0) | |
3585 | return r; | |
3586 | } | |
3587 | ||
3588 | return 0; | |
3589 | } | |
3590 | ||
3591 | return sd_json_variant_dump(d, arg_json_format_flags, NULL, NULL); | |
3592 | } | |
3593 | ||
3594 | static void help_protocol_types(void) { | |
3595 | if (arg_legend) | |
3596 | puts("Known protocol types:"); | |
3597 | puts("dns\n" | |
3598 | "llmnr\n" | |
3599 | "llmnr-ipv4\n" | |
3600 | "llmnr-ipv6\n" | |
3601 | "mdns\n" | |
3602 | "mdns-ipv4\n" | |
3603 | "mdns-ipv6"); | |
3604 | } | |
3605 | ||
3606 | static void help_dns_types(void) { | |
3607 | if (arg_legend) | |
3608 | puts("Known DNS RR types:"); | |
3609 | ||
3610 | DUMP_STRING_TABLE(dns_type, int, _DNS_TYPE_MAX); | |
3611 | } | |
3612 | ||
3613 | static void help_dns_classes(void) { | |
3614 | if (arg_legend) | |
3615 | puts("Known DNS RR classes:"); | |
3616 | ||
3617 | DUMP_STRING_TABLE(dns_class, int, _DNS_CLASS_MAX); | |
3618 | } | |
3619 | ||
3620 | static int compat_help(void) { | |
3621 | _cleanup_free_ char *link = NULL; | |
3622 | int r; | |
3623 | ||
3624 | r = terminal_urlify_man("resolvectl", "1", &link); | |
3625 | if (r < 0) | |
3626 | return log_oom(); | |
3627 | ||
3628 | printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n" | |
3629 | "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n" | |
3630 | "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n" | |
3631 | "%1$s [OPTIONS...] --statistics\n" | |
3632 | "%1$s [OPTIONS...] --reset-statistics\n" | |
3633 | "\n" | |
3634 | "%2$sResolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%3$s\n\n" | |
3635 | " -h --help Show this help\n" | |
3636 | " --version Show package version\n" | |
3637 | " --no-pager Do not pipe output into a pager\n" | |
3638 | " -4 Resolve IPv4 addresses\n" | |
3639 | " -6 Resolve IPv6 addresses\n" | |
3640 | " -i --interface=INTERFACE Look on interface\n" | |
3641 | " -p --protocol=PROTO|help Look via protocol\n" | |
3642 | " -t --type=TYPE|help Query RR with DNS type\n" | |
3643 | " -c --class=CLASS|help Query RR with DNS class\n" | |
3644 | " --service Resolve service (SRV)\n" | |
3645 | " --service-address=BOOL Resolve address for services (default: yes)\n" | |
3646 | " --service-txt=BOOL Resolve TXT records for services (default: yes)\n" | |
3647 | " --openpgp Query OpenPGP public key\n" | |
3648 | " --tlsa Query TLS public key\n" | |
3649 | " --cname=BOOL Follow CNAME redirects (default: yes)\n" | |
3650 | " --search=BOOL Use search domains for single-label names\n" | |
3651 | " (default: yes)\n" | |
3652 | " --raw[=payload|packet] Dump the answer as binary data\n" | |
3653 | " --legend=BOOL Print headers and additional info (default: yes)\n" | |
3654 | " --statistics Show resolver statistics\n" | |
3655 | " --reset-statistics Reset resolver statistics\n" | |
3656 | " --status Show link and server status\n" | |
3657 | " --flush-caches Flush all local DNS caches\n" | |
3658 | " --reset-server-features\n" | |
3659 | " Forget learnt DNS server feature levels\n" | |
3660 | " --set-dns=SERVER Set per-interface DNS server address\n" | |
3661 | " --set-domain=DOMAIN Set per-interface search domain\n" | |
3662 | " --set-llmnr=MODE Set per-interface LLMNR mode\n" | |
3663 | " --set-mdns=MODE Set per-interface MulticastDNS mode\n" | |
3664 | " --set-dnsovertls=MODE Set per-interface DNS-over-TLS mode\n" | |
3665 | " --set-dnssec=MODE Set per-interface DNSSEC mode\n" | |
3666 | " --set-nta=DOMAIN Set per-interface DNSSEC NTA\n" | |
3667 | " --revert Revert per-interface configuration\n" | |
3668 | "\nSee the %4$s for details.\n", | |
3669 | program_invocation_short_name, | |
3670 | ansi_highlight(), | |
3671 | ansi_normal(), | |
3672 | link); | |
3673 | ||
3674 | return 0; | |
3675 | } | |
3676 | ||
3677 | static int native_help(void) { | |
3678 | _cleanup_free_ char *link = NULL; | |
3679 | int r; | |
3680 | ||
3681 | r = terminal_urlify_man("resolvectl", "1", &link); | |
3682 | if (r < 0) | |
3683 | return log_oom(); | |
3684 | ||
3685 | printf("%1$s [OPTIONS...] COMMAND ...\n" | |
3686 | "\n" | |
3687 | "%5$sSend control commands to the network name resolution manager, or%6$s\n" | |
3688 | "%5$sresolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%6$s\n" | |
3689 | "\n%3$sCommands:%4$s\n" | |
3690 | " query HOSTNAME|ADDRESS... Resolve domain names, IPv4 and IPv6 addresses\n" | |
3691 | " service [[NAME] TYPE] DOMAIN Resolve service (SRV)\n" | |
3692 | " openpgp EMAIL@DOMAIN... Query OpenPGP public key\n" | |
3693 | " tlsa DOMAIN[:PORT]... Query TLS public key\n" | |
3694 | " status [LINK...] Show link and server status\n" | |
3695 | " statistics Show resolver statistics\n" | |
3696 | " reset-statistics Reset resolver statistics\n" | |
3697 | " flush-caches Flush all local DNS caches\n" | |
3698 | " reset-server-features Forget learnt DNS server feature levels\n" | |
3699 | " monitor Monitor DNS queries\n" | |
3700 | " show-cache Show cache contents\n" | |
3701 | " show-server-state Show servers state\n" | |
3702 | " dns [LINK [SERVER...]] Get/set per-interface DNS server address\n" | |
3703 | " domain [LINK [DOMAIN...]] Get/set per-interface search domain\n" | |
3704 | " default-route [LINK [BOOL]] Get/set per-interface default route flag\n" | |
3705 | " llmnr [LINK [MODE]] Get/set per-interface LLMNR mode\n" | |
3706 | " mdns [LINK [MODE]] Get/set per-interface MulticastDNS mode\n" | |
3707 | " dnsovertls [LINK [MODE]] Get/set per-interface DNS-over-TLS mode\n" | |
3708 | " dnssec [LINK [MODE]] Get/set per-interface DNSSEC mode\n" | |
3709 | " nta [LINK [DOMAIN...]] Get/set per-interface DNSSEC NTA\n" | |
3710 | " revert LINK Revert per-interface configuration\n" | |
3711 | " log-level [LEVEL] Get/set logging threshold for systemd-resolved\n" | |
3712 | "\n%3$sOptions:%4$s\n" | |
3713 | " -h --help Show this help\n" | |
3714 | " --version Show package version\n" | |
3715 | " --no-pager Do not pipe output into a pager\n" | |
3716 | " --no-ask-password Do not prompt for password\n" | |
3717 | " -4 Resolve IPv4 addresses\n" | |
3718 | " -6 Resolve IPv6 addresses\n" | |
3719 | " -i --interface=INTERFACE Look on interface\n" | |
3720 | " -p --protocol=PROTO|help Look via protocol\n" | |
3721 | " -t --type=TYPE|help Query RR with DNS type\n" | |
3722 | " -c --class=CLASS|help Query RR with DNS class\n" | |
3723 | " --service-address=BOOL Resolve address for services (default: yes)\n" | |
3724 | " --service-txt=BOOL Resolve TXT records for services (default: yes)\n" | |
3725 | " --cname=BOOL Follow CNAME redirects (default: yes)\n" | |
3726 | " --validate=BOOL Allow DNSSEC validation (default: yes)\n" | |
3727 | " --synthesize=BOOL Allow synthetic response (default: yes)\n" | |
3728 | " --cache=BOOL Allow response from cache (default: yes)\n" | |
3729 | " --stale-data=BOOL Allow response from cache with stale data (default: yes)\n" | |
3730 | " --relax-single-label=BOOL Allow single label lookups to go upstream (default: no)\n" | |
3731 | " --zone=BOOL Allow response from locally registered mDNS/LLMNR\n" | |
3732 | " records (default: yes)\n" | |
3733 | " --trust-anchor=BOOL Allow response from local trust anchor (default:\n" | |
3734 | " yes)\n" | |
3735 | " --network=BOOL Allow response from network (default: yes)\n" | |
3736 | " --search=BOOL Use search domains for single-label names (default:\n" | |
3737 | " yes)\n" | |
3738 | " --raw[=payload|packet] Dump the answer as binary data\n" | |
3739 | " --legend=BOOL Print headers and additional info (default: yes)\n" | |
3740 | " --json=MODE Output as JSON\n" | |
3741 | " -j Same as --json=pretty on tty, --json=short\n" | |
3742 | " otherwise\n" | |
3743 | "\nSee the %2$s for details.\n", | |
3744 | program_invocation_short_name, | |
3745 | link, | |
3746 | ansi_underline(), | |
3747 | ansi_normal(), | |
3748 | ansi_highlight(), | |
3749 | ansi_normal()); | |
3750 | ||
3751 | return 0; | |
3752 | } | |
3753 | ||
3754 | static int verb_help(int argc, char **argv, void *userdata) { | |
3755 | return native_help(); | |
3756 | } | |
3757 | ||
3758 | static int compat_parse_argv(int argc, char *argv[]) { | |
3759 | enum { | |
3760 | ARG_VERSION = 0x100, | |
3761 | ARG_LEGEND, | |
3762 | ARG_SERVICE, | |
3763 | ARG_CNAME, | |
3764 | ARG_SERVICE_ADDRESS, | |
3765 | ARG_SERVICE_TXT, | |
3766 | ARG_OPENPGP, | |
3767 | ARG_TLSA, | |
3768 | ARG_RAW, | |
3769 | ARG_SEARCH, | |
3770 | ARG_STATISTICS, | |
3771 | ARG_RESET_STATISTICS, | |
3772 | ARG_STATUS, | |
3773 | ARG_FLUSH_CACHES, | |
3774 | ARG_RESET_SERVER_FEATURES, | |
3775 | ARG_NO_PAGER, | |
3776 | ARG_SET_DNS, | |
3777 | ARG_SET_DOMAIN, | |
3778 | ARG_SET_LLMNR, | |
3779 | ARG_SET_MDNS, | |
3780 | ARG_SET_DNS_OVER_TLS, | |
3781 | ARG_SET_DNSSEC, | |
3782 | ARG_SET_NTA, | |
3783 | ARG_REVERT_LINK, | |
3784 | }; | |
3785 | ||
3786 | static const struct option options[] = { | |
3787 | { "help", no_argument, NULL, 'h' }, | |
3788 | { "version", no_argument, NULL, ARG_VERSION }, | |
3789 | { "type", required_argument, NULL, 't' }, | |
3790 | { "class", required_argument, NULL, 'c' }, | |
3791 | { "legend", required_argument, NULL, ARG_LEGEND }, | |
3792 | { "interface", required_argument, NULL, 'i' }, | |
3793 | { "protocol", required_argument, NULL, 'p' }, | |
3794 | { "cname", required_argument, NULL, ARG_CNAME }, | |
3795 | { "service", no_argument, NULL, ARG_SERVICE }, | |
3796 | { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS }, | |
3797 | { "service-txt", required_argument, NULL, ARG_SERVICE_TXT }, | |
3798 | { "openpgp", no_argument, NULL, ARG_OPENPGP }, | |
3799 | { "tlsa", optional_argument, NULL, ARG_TLSA }, | |
3800 | { "raw", optional_argument, NULL, ARG_RAW }, | |
3801 | { "search", required_argument, NULL, ARG_SEARCH }, | |
3802 | { "statistics", no_argument, NULL, ARG_STATISTICS, }, | |
3803 | { "reset-statistics", no_argument, NULL, ARG_RESET_STATISTICS }, | |
3804 | { "status", no_argument, NULL, ARG_STATUS }, | |
3805 | { "flush-caches", no_argument, NULL, ARG_FLUSH_CACHES }, | |
3806 | { "reset-server-features", no_argument, NULL, ARG_RESET_SERVER_FEATURES }, | |
3807 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
3808 | { "set-dns", required_argument, NULL, ARG_SET_DNS }, | |
3809 | { "set-domain", required_argument, NULL, ARG_SET_DOMAIN }, | |
3810 | { "set-llmnr", required_argument, NULL, ARG_SET_LLMNR }, | |
3811 | { "set-mdns", required_argument, NULL, ARG_SET_MDNS }, | |
3812 | { "set-dnsovertls", required_argument, NULL, ARG_SET_DNS_OVER_TLS }, | |
3813 | { "set-dnssec", required_argument, NULL, ARG_SET_DNSSEC }, | |
3814 | { "set-nta", required_argument, NULL, ARG_SET_NTA }, | |
3815 | { "revert", no_argument, NULL, ARG_REVERT_LINK }, | |
3816 | {} | |
3817 | }; | |
3818 | ||
3819 | int c, r; | |
3820 | ||
3821 | assert(argc >= 0); | |
3822 | assert(argv); | |
3823 | ||
3824 | while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0) | |
3825 | switch (c) { | |
3826 | ||
3827 | case 'h': | |
3828 | return compat_help(); | |
3829 | ||
3830 | case ARG_VERSION: | |
3831 | return version(); | |
3832 | ||
3833 | case '4': | |
3834 | arg_family = AF_INET; | |
3835 | break; | |
3836 | ||
3837 | case '6': | |
3838 | arg_family = AF_INET6; | |
3839 | break; | |
3840 | ||
3841 | case 'i': | |
3842 | r = ifname_mangle(optarg); | |
3843 | if (r < 0) | |
3844 | return r; | |
3845 | break; | |
3846 | ||
3847 | case 't': | |
3848 | if (streq(optarg, "help")) { | |
3849 | help_dns_types(); | |
3850 | return 0; | |
3851 | } | |
3852 | ||
3853 | r = dns_type_from_string(optarg); | |
3854 | if (r < 0) | |
3855 | return log_error_errno(r, "Failed to parse RR record type %s: %m", optarg); | |
3856 | ||
3857 | arg_type = (uint16_t) r; | |
3858 | assert((int) arg_type == r); | |
3859 | ||
3860 | arg_mode = MODE_RESOLVE_RECORD; | |
3861 | break; | |
3862 | ||
3863 | case 'c': | |
3864 | if (streq(optarg, "help")) { | |
3865 | help_dns_classes(); | |
3866 | return 0; | |
3867 | } | |
3868 | ||
3869 | r = dns_class_from_string(optarg); | |
3870 | if (r < 0) | |
3871 | return log_error_errno(r, "Failed to parse RR record class %s: %m", optarg); | |
3872 | ||
3873 | arg_class = (uint16_t) r; | |
3874 | assert((int) arg_class == r); | |
3875 | ||
3876 | break; | |
3877 | ||
3878 | case ARG_LEGEND: | |
3879 | r = parse_boolean_argument("--legend=", optarg, &arg_legend); | |
3880 | if (r < 0) | |
3881 | return r; | |
3882 | break; | |
3883 | ||
3884 | case 'p': | |
3885 | if (streq(optarg, "help")) { | |
3886 | help_protocol_types(); | |
3887 | return 0; | |
3888 | } else if (streq(optarg, "dns")) | |
3889 | arg_flags |= SD_RESOLVED_DNS; | |
3890 | else if (streq(optarg, "llmnr")) | |
3891 | arg_flags |= SD_RESOLVED_LLMNR; | |
3892 | else if (streq(optarg, "llmnr-ipv4")) | |
3893 | arg_flags |= SD_RESOLVED_LLMNR_IPV4; | |
3894 | else if (streq(optarg, "llmnr-ipv6")) | |
3895 | arg_flags |= SD_RESOLVED_LLMNR_IPV6; | |
3896 | else if (streq(optarg, "mdns")) | |
3897 | arg_flags |= SD_RESOLVED_MDNS; | |
3898 | else if (streq(optarg, "mdns-ipv4")) | |
3899 | arg_flags |= SD_RESOLVED_MDNS_IPV4; | |
3900 | else if (streq(optarg, "mdns-ipv6")) | |
3901 | arg_flags |= SD_RESOLVED_MDNS_IPV6; | |
3902 | else | |
3903 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3904 | "Unknown protocol specifier: %s", optarg); | |
3905 | ||
3906 | break; | |
3907 | ||
3908 | case ARG_SERVICE: | |
3909 | arg_mode = MODE_RESOLVE_SERVICE; | |
3910 | break; | |
3911 | ||
3912 | case ARG_OPENPGP: | |
3913 | arg_mode = MODE_RESOLVE_OPENPGP; | |
3914 | break; | |
3915 | ||
3916 | case ARG_TLSA: | |
3917 | arg_mode = MODE_RESOLVE_TLSA; | |
3918 | if (!optarg || service_family_is_valid(optarg)) | |
3919 | arg_service_family = optarg; | |
3920 | else | |
3921 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3922 | "Unknown service family \"%s\".", optarg); | |
3923 | break; | |
3924 | ||
3925 | case ARG_RAW: | |
3926 | if (on_tty()) | |
3927 | return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), | |
3928 | "Refusing to write binary data to tty."); | |
3929 | ||
3930 | if (optarg == NULL || streq(optarg, "payload")) | |
3931 | arg_raw = RAW_PAYLOAD; | |
3932 | else if (streq(optarg, "packet")) | |
3933 | arg_raw = RAW_PACKET; | |
3934 | else | |
3935 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3936 | "Unknown --raw specifier \"%s\".", | |
3937 | optarg); | |
3938 | ||
3939 | arg_legend = false; | |
3940 | break; | |
3941 | ||
3942 | case ARG_CNAME: | |
3943 | r = parse_boolean_argument("--cname=", optarg, NULL); | |
3944 | if (r < 0) | |
3945 | return r; | |
3946 | SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0); | |
3947 | break; | |
3948 | ||
3949 | case ARG_SERVICE_ADDRESS: | |
3950 | r = parse_boolean_argument("--service-address=", optarg, NULL); | |
3951 | if (r < 0) | |
3952 | return r; | |
3953 | SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0); | |
3954 | break; | |
3955 | ||
3956 | case ARG_SERVICE_TXT: | |
3957 | r = parse_boolean_argument("--service-txt=", optarg, NULL); | |
3958 | if (r < 0) | |
3959 | return r; | |
3960 | SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0); | |
3961 | break; | |
3962 | ||
3963 | case ARG_SEARCH: | |
3964 | r = parse_boolean_argument("--search=", optarg, NULL); | |
3965 | if (r < 0) | |
3966 | return r; | |
3967 | SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0); | |
3968 | break; | |
3969 | ||
3970 | case ARG_STATISTICS: | |
3971 | arg_mode = MODE_STATISTICS; | |
3972 | break; | |
3973 | ||
3974 | case ARG_RESET_STATISTICS: | |
3975 | arg_mode = MODE_RESET_STATISTICS; | |
3976 | break; | |
3977 | ||
3978 | case ARG_FLUSH_CACHES: | |
3979 | arg_mode = MODE_FLUSH_CACHES; | |
3980 | break; | |
3981 | ||
3982 | case ARG_RESET_SERVER_FEATURES: | |
3983 | arg_mode = MODE_RESET_SERVER_FEATURES; | |
3984 | break; | |
3985 | ||
3986 | case ARG_STATUS: | |
3987 | arg_mode = MODE_STATUS; | |
3988 | break; | |
3989 | ||
3990 | case ARG_NO_PAGER: | |
3991 | arg_pager_flags |= PAGER_DISABLE; | |
3992 | break; | |
3993 | ||
3994 | case ARG_SET_DNS: | |
3995 | r = strv_extend(&arg_set_dns, optarg); | |
3996 | if (r < 0) | |
3997 | return log_oom(); | |
3998 | ||
3999 | arg_mode = MODE_SET_LINK; | |
4000 | break; | |
4001 | ||
4002 | case ARG_SET_DOMAIN: | |
4003 | r = strv_extend(&arg_set_domain, optarg); | |
4004 | if (r < 0) | |
4005 | return log_oom(); | |
4006 | ||
4007 | arg_mode = MODE_SET_LINK; | |
4008 | break; | |
4009 | ||
4010 | case ARG_SET_LLMNR: | |
4011 | arg_set_llmnr = optarg; | |
4012 | arg_mode = MODE_SET_LINK; | |
4013 | break; | |
4014 | ||
4015 | case ARG_SET_MDNS: | |
4016 | arg_set_mdns = optarg; | |
4017 | arg_mode = MODE_SET_LINK; | |
4018 | break; | |
4019 | ||
4020 | case ARG_SET_DNS_OVER_TLS: | |
4021 | arg_set_dns_over_tls = optarg; | |
4022 | arg_mode = MODE_SET_LINK; | |
4023 | break; | |
4024 | ||
4025 | case ARG_SET_DNSSEC: | |
4026 | arg_set_dnssec = optarg; | |
4027 | arg_mode = MODE_SET_LINK; | |
4028 | break; | |
4029 | ||
4030 | case ARG_SET_NTA: | |
4031 | r = strv_extend(&arg_set_nta, optarg); | |
4032 | if (r < 0) | |
4033 | return log_oom(); | |
4034 | ||
4035 | arg_mode = MODE_SET_LINK; | |
4036 | break; | |
4037 | ||
4038 | case ARG_REVERT_LINK: | |
4039 | arg_mode = MODE_REVERT_LINK; | |
4040 | break; | |
4041 | ||
4042 | case '?': | |
4043 | return -EINVAL; | |
4044 | ||
4045 | default: | |
4046 | assert_not_reached(); | |
4047 | } | |
4048 | ||
4049 | if (arg_type == 0 && arg_class != 0) | |
4050 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4051 | "--class= may only be used in conjunction with --type=."); | |
4052 | ||
4053 | if (arg_type != 0 && arg_mode == MODE_RESOLVE_SERVICE) | |
4054 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4055 | "--service and --type= may not be combined."); | |
4056 | ||
4057 | if (arg_type != 0 && arg_class == 0) | |
4058 | arg_class = DNS_CLASS_IN; | |
4059 | ||
4060 | if (arg_class != 0 && arg_type == 0) | |
4061 | arg_type = DNS_TYPE_A; | |
4062 | ||
4063 | if (IN_SET(arg_mode, MODE_SET_LINK, MODE_REVERT_LINK)) { | |
4064 | ||
4065 | if (arg_ifindex <= 0) | |
4066 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4067 | "--set-dns=, --set-domain=, --set-llmnr=, --set-mdns=, --set-dnsovertls=, --set-dnssec=, --set-nta= and --revert require --interface=."); | |
4068 | } | |
4069 | ||
4070 | return 1 /* work to do */; | |
4071 | } | |
4072 | ||
4073 | static int native_parse_argv(int argc, char *argv[]) { | |
4074 | enum { | |
4075 | ARG_VERSION = 0x100, | |
4076 | ARG_LEGEND, | |
4077 | ARG_CNAME, | |
4078 | ARG_VALIDATE, | |
4079 | ARG_SYNTHESIZE, | |
4080 | ARG_CACHE, | |
4081 | ARG_ZONE, | |
4082 | ARG_TRUST_ANCHOR, | |
4083 | ARG_NETWORK, | |
4084 | ARG_SERVICE_ADDRESS, | |
4085 | ARG_SERVICE_TXT, | |
4086 | ARG_RAW, | |
4087 | ARG_SEARCH, | |
4088 | ARG_NO_PAGER, | |
4089 | ARG_NO_ASK_PASSWORD, | |
4090 | ARG_JSON, | |
4091 | ARG_STALE_DATA, | |
4092 | ARG_RELAX_SINGLE_LABEL, | |
4093 | }; | |
4094 | ||
4095 | static const struct option options[] = { | |
4096 | { "help", no_argument, NULL, 'h' }, | |
4097 | { "version", no_argument, NULL, ARG_VERSION }, | |
4098 | { "type", required_argument, NULL, 't' }, | |
4099 | { "class", required_argument, NULL, 'c' }, | |
4100 | { "legend", required_argument, NULL, ARG_LEGEND }, | |
4101 | { "interface", required_argument, NULL, 'i' }, | |
4102 | { "protocol", required_argument, NULL, 'p' }, | |
4103 | { "cname", required_argument, NULL, ARG_CNAME }, | |
4104 | { "validate", required_argument, NULL, ARG_VALIDATE }, | |
4105 | { "synthesize", required_argument, NULL, ARG_SYNTHESIZE }, | |
4106 | { "cache", required_argument, NULL, ARG_CACHE }, | |
4107 | { "zone", required_argument, NULL, ARG_ZONE }, | |
4108 | { "trust-anchor", required_argument, NULL, ARG_TRUST_ANCHOR }, | |
4109 | { "network", required_argument, NULL, ARG_NETWORK }, | |
4110 | { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS }, | |
4111 | { "service-txt", required_argument, NULL, ARG_SERVICE_TXT }, | |
4112 | { "raw", optional_argument, NULL, ARG_RAW }, | |
4113 | { "search", required_argument, NULL, ARG_SEARCH }, | |
4114 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
4115 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
4116 | { "json", required_argument, NULL, ARG_JSON }, | |
4117 | { "stale-data", required_argument, NULL, ARG_STALE_DATA }, | |
4118 | { "relax-single-label", required_argument, NULL, ARG_RELAX_SINGLE_LABEL }, | |
4119 | {} | |
4120 | }; | |
4121 | ||
4122 | int c, r; | |
4123 | ||
4124 | assert(argc >= 0); | |
4125 | assert(argv); | |
4126 | ||
4127 | while ((c = getopt_long(argc, argv, "h46i:t:c:p:j", options, NULL)) >= 0) | |
4128 | switch (c) { | |
4129 | ||
4130 | case 'h': | |
4131 | return native_help(); | |
4132 | ||
4133 | case ARG_VERSION: | |
4134 | return version(); | |
4135 | ||
4136 | case '4': | |
4137 | arg_family = AF_INET; | |
4138 | break; | |
4139 | ||
4140 | case '6': | |
4141 | arg_family = AF_INET6; | |
4142 | break; | |
4143 | ||
4144 | case 'i': | |
4145 | r = ifname_mangle(optarg); | |
4146 | if (r < 0) | |
4147 | return r; | |
4148 | break; | |
4149 | ||
4150 | case 't': | |
4151 | if (streq(optarg, "help")) { | |
4152 | help_dns_types(); | |
4153 | return 0; | |
4154 | } | |
4155 | ||
4156 | r = dns_type_from_string(optarg); | |
4157 | if (r < 0) | |
4158 | return log_error_errno(r, "Failed to parse RR record type %s: %m", optarg); | |
4159 | ||
4160 | arg_type = (uint16_t) r; | |
4161 | assert((int) arg_type == r); | |
4162 | ||
4163 | break; | |
4164 | ||
4165 | case 'c': | |
4166 | if (streq(optarg, "help")) { | |
4167 | help_dns_classes(); | |
4168 | return 0; | |
4169 | } | |
4170 | ||
4171 | r = dns_class_from_string(optarg); | |
4172 | if (r < 0) | |
4173 | return log_error_errno(r, "Failed to parse RR record class %s: %m", optarg); | |
4174 | ||
4175 | arg_class = (uint16_t) r; | |
4176 | assert((int) arg_class == r); | |
4177 | ||
4178 | break; | |
4179 | ||
4180 | case ARG_LEGEND: | |
4181 | r = parse_boolean_argument("--legend=", optarg, &arg_legend); | |
4182 | if (r < 0) | |
4183 | return r; | |
4184 | break; | |
4185 | ||
4186 | case 'p': | |
4187 | if (streq(optarg, "help")) { | |
4188 | help_protocol_types(); | |
4189 | return 0; | |
4190 | } else if (streq(optarg, "dns")) | |
4191 | arg_flags |= SD_RESOLVED_DNS; | |
4192 | else if (streq(optarg, "llmnr")) | |
4193 | arg_flags |= SD_RESOLVED_LLMNR; | |
4194 | else if (streq(optarg, "llmnr-ipv4")) | |
4195 | arg_flags |= SD_RESOLVED_LLMNR_IPV4; | |
4196 | else if (streq(optarg, "llmnr-ipv6")) | |
4197 | arg_flags |= SD_RESOLVED_LLMNR_IPV6; | |
4198 | else if (streq(optarg, "mdns")) | |
4199 | arg_flags |= SD_RESOLVED_MDNS; | |
4200 | else if (streq(optarg, "mdns-ipv4")) | |
4201 | arg_flags |= SD_RESOLVED_MDNS_IPV4; | |
4202 | else if (streq(optarg, "mdns-ipv6")) | |
4203 | arg_flags |= SD_RESOLVED_MDNS_IPV6; | |
4204 | else | |
4205 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4206 | "Unknown protocol specifier: %s", | |
4207 | optarg); | |
4208 | ||
4209 | break; | |
4210 | ||
4211 | case ARG_RAW: | |
4212 | if (on_tty()) | |
4213 | return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), | |
4214 | "Refusing to write binary data to tty."); | |
4215 | ||
4216 | if (optarg == NULL || streq(optarg, "payload")) | |
4217 | arg_raw = RAW_PAYLOAD; | |
4218 | else if (streq(optarg, "packet")) | |
4219 | arg_raw = RAW_PACKET; | |
4220 | else | |
4221 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4222 | "Unknown --raw specifier \"%s\".", | |
4223 | optarg); | |
4224 | ||
4225 | arg_legend = false; | |
4226 | break; | |
4227 | ||
4228 | case ARG_CNAME: | |
4229 | r = parse_boolean_argument("--cname=", optarg, NULL); | |
4230 | if (r < 0) | |
4231 | return r; | |
4232 | SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0); | |
4233 | break; | |
4234 | ||
4235 | case ARG_VALIDATE: | |
4236 | r = parse_boolean_argument("--validate=", optarg, NULL); | |
4237 | if (r < 0) | |
4238 | return r; | |
4239 | SET_FLAG(arg_flags, SD_RESOLVED_NO_VALIDATE, r == 0); | |
4240 | break; | |
4241 | ||
4242 | case ARG_SYNTHESIZE: | |
4243 | r = parse_boolean_argument("--synthesize=", optarg, NULL); | |
4244 | if (r < 0) | |
4245 | return r; | |
4246 | SET_FLAG(arg_flags, SD_RESOLVED_NO_SYNTHESIZE, r == 0); | |
4247 | break; | |
4248 | ||
4249 | case ARG_CACHE: | |
4250 | r = parse_boolean_argument("--cache=", optarg, NULL); | |
4251 | if (r < 0) | |
4252 | return r; | |
4253 | SET_FLAG(arg_flags, SD_RESOLVED_NO_CACHE, r == 0); | |
4254 | break; | |
4255 | ||
4256 | case ARG_STALE_DATA: | |
4257 | r = parse_boolean_argument("--stale-data=", optarg, NULL); | |
4258 | if (r < 0) | |
4259 | return r; | |
4260 | SET_FLAG(arg_flags, SD_RESOLVED_NO_STALE, r == 0); | |
4261 | break; | |
4262 | ||
4263 | case ARG_ZONE: | |
4264 | r = parse_boolean_argument("--zone=", optarg, NULL); | |
4265 | if (r < 0) | |
4266 | return r; | |
4267 | SET_FLAG(arg_flags, SD_RESOLVED_NO_ZONE, r == 0); | |
4268 | break; | |
4269 | ||
4270 | case ARG_TRUST_ANCHOR: | |
4271 | r = parse_boolean_argument("--trust-anchor=", optarg, NULL); | |
4272 | if (r < 0) | |
4273 | return r; | |
4274 | SET_FLAG(arg_flags, SD_RESOLVED_NO_TRUST_ANCHOR, r == 0); | |
4275 | break; | |
4276 | ||
4277 | case ARG_NETWORK: | |
4278 | r = parse_boolean_argument("--network=", optarg, NULL); | |
4279 | if (r < 0) | |
4280 | return r; | |
4281 | SET_FLAG(arg_flags, SD_RESOLVED_NO_NETWORK, r == 0); | |
4282 | break; | |
4283 | ||
4284 | case ARG_SERVICE_ADDRESS: | |
4285 | r = parse_boolean_argument("--service-address=", optarg, NULL); | |
4286 | if (r < 0) | |
4287 | return r; | |
4288 | SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0); | |
4289 | break; | |
4290 | ||
4291 | case ARG_SERVICE_TXT: | |
4292 | r = parse_boolean_argument("--service-txt=", optarg, NULL); | |
4293 | if (r < 0) | |
4294 | return r; | |
4295 | SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0); | |
4296 | break; | |
4297 | ||
4298 | case ARG_SEARCH: | |
4299 | r = parse_boolean_argument("--search=", optarg, NULL); | |
4300 | if (r < 0) | |
4301 | return r; | |
4302 | SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0); | |
4303 | break; | |
4304 | ||
4305 | case ARG_RELAX_SINGLE_LABEL: | |
4306 | r = parse_boolean_argument("--relax-single-label=", optarg, NULL); | |
4307 | if (r < 0) | |
4308 | return r; | |
4309 | SET_FLAG(arg_flags, SD_RESOLVED_RELAX_SINGLE_LABEL, r > 0); | |
4310 | break; | |
4311 | ||
4312 | case ARG_NO_PAGER: | |
4313 | arg_pager_flags |= PAGER_DISABLE; | |
4314 | break; | |
4315 | ||
4316 | case ARG_NO_ASK_PASSWORD: | |
4317 | arg_ask_password = false; | |
4318 | break; | |
4319 | ||
4320 | case ARG_JSON: | |
4321 | r = parse_json_argument(optarg, &arg_json_format_flags); | |
4322 | if (r <= 0) | |
4323 | return r; | |
4324 | ||
4325 | break; | |
4326 | ||
4327 | case 'j': | |
4328 | arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; | |
4329 | break; | |
4330 | ||
4331 | case '?': | |
4332 | return -EINVAL; | |
4333 | ||
4334 | default: | |
4335 | assert_not_reached(); | |
4336 | } | |
4337 | ||
4338 | if (arg_type == 0 && arg_class != 0) | |
4339 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4340 | "--class= may only be used in conjunction with --type=."); | |
4341 | ||
4342 | if (arg_type != 0 && arg_class == 0) | |
4343 | arg_class = DNS_CLASS_IN; | |
4344 | ||
4345 | if (arg_class != 0 && arg_type == 0) | |
4346 | arg_type = DNS_TYPE_A; | |
4347 | ||
4348 | return 1 /* work to do */; | |
4349 | } | |
4350 | ||
4351 | static int native_main(int argc, char *argv[]) { | |
4352 | ||
4353 | static const Verb verbs[] = { | |
4354 | { "help", VERB_ANY, VERB_ANY, 0, verb_help }, | |
4355 | { "status", VERB_ANY, VERB_ANY, VERB_DEFAULT, verb_status }, | |
4356 | { "query", 2, VERB_ANY, 0, verb_query }, | |
4357 | { "service", 2, 4, 0, verb_service }, | |
4358 | { "openpgp", 2, VERB_ANY, 0, verb_openpgp }, | |
4359 | { "tlsa", 2, VERB_ANY, 0, verb_tlsa }, | |
4360 | { "statistics", VERB_ANY, 1, 0, show_statistics }, | |
4361 | { "reset-statistics", VERB_ANY, 1, 0, reset_statistics }, | |
4362 | { "flush-caches", VERB_ANY, 1, 0, flush_caches }, | |
4363 | { "reset-server-features", VERB_ANY, 1, 0, reset_server_features }, | |
4364 | { "dns", VERB_ANY, VERB_ANY, 0, verb_dns }, | |
4365 | { "domain", VERB_ANY, VERB_ANY, 0, verb_domain }, | |
4366 | { "default-route", VERB_ANY, 3, 0, verb_default_route }, | |
4367 | { "llmnr", VERB_ANY, 3, 0, verb_llmnr }, | |
4368 | { "mdns", VERB_ANY, 3, 0, verb_mdns }, | |
4369 | { "dnsovertls", VERB_ANY, 3, 0, verb_dns_over_tls }, | |
4370 | { "dnssec", VERB_ANY, 3, 0, verb_dnssec }, | |
4371 | { "nta", VERB_ANY, VERB_ANY, 0, verb_nta }, | |
4372 | { "revert", VERB_ANY, 2, 0, verb_revert_link }, | |
4373 | { "log-level", VERB_ANY, 2, 0, verb_log_level }, | |
4374 | { "monitor", VERB_ANY, 1, 0, verb_monitor }, | |
4375 | { "show-cache", VERB_ANY, 1, 0, verb_show_cache }, | |
4376 | { "show-server-state", VERB_ANY, 1, 0, verb_show_server_state}, | |
4377 | {} | |
4378 | }; | |
4379 | ||
4380 | return dispatch_verb(argc, argv, verbs, /* userdata = */ NULL); | |
4381 | } | |
4382 | ||
4383 | static int translate(const char *verb, const char *single_arg, size_t num_args, char **args) { | |
4384 | char **fake, **p; | |
4385 | size_t num; | |
4386 | ||
4387 | assert(verb); | |
4388 | assert(num_args == 0 || args); | |
4389 | ||
4390 | num = !!single_arg + num_args + 1; | |
4391 | ||
4392 | p = fake = newa0(char *, num + 1); | |
4393 | *p++ = (char *) verb; | |
4394 | if (single_arg) | |
4395 | *p++ = (char *) single_arg; | |
4396 | FOREACH_ARRAY(arg, args, num_args) | |
4397 | *p++ = *arg; | |
4398 | ||
4399 | optind = 0; | |
4400 | return native_main((int) num, fake); | |
4401 | } | |
4402 | ||
4403 | static int compat_main(int argc, char *argv[]) { | |
4404 | int r = 0; | |
4405 | ||
4406 | switch (arg_mode) { | |
4407 | case MODE_RESOLVE_HOST: | |
4408 | case MODE_RESOLVE_RECORD: | |
4409 | return translate("query", NULL, argc - optind, argv + optind); | |
4410 | ||
4411 | case MODE_RESOLVE_SERVICE: | |
4412 | return translate("service", NULL, argc - optind, argv + optind); | |
4413 | ||
4414 | case MODE_RESOLVE_OPENPGP: | |
4415 | return translate("openpgp", NULL, argc - optind, argv + optind); | |
4416 | ||
4417 | case MODE_RESOLVE_TLSA: | |
4418 | return translate("tlsa", arg_service_family, argc - optind, argv + optind); | |
4419 | ||
4420 | case MODE_STATISTICS: | |
4421 | return translate("statistics", NULL, 0, NULL); | |
4422 | ||
4423 | case MODE_RESET_STATISTICS: | |
4424 | return translate("reset-statistics", NULL, 0, NULL); | |
4425 | ||
4426 | case MODE_FLUSH_CACHES: | |
4427 | return translate("flush-caches", NULL, 0, NULL); | |
4428 | ||
4429 | case MODE_RESET_SERVER_FEATURES: | |
4430 | return translate("reset-server-features", NULL, 0, NULL); | |
4431 | ||
4432 | case MODE_STATUS: | |
4433 | return translate("status", NULL, argc - optind, argv + optind); | |
4434 | ||
4435 | case MODE_SET_LINK: | |
4436 | assert(arg_ifname); | |
4437 | ||
4438 | if (arg_disable_default_route) { | |
4439 | r = translate("default-route", arg_ifname, 1, STRV_MAKE("no")); | |
4440 | if (r < 0) | |
4441 | return r; | |
4442 | } | |
4443 | ||
4444 | if (arg_set_dns) { | |
4445 | r = translate("dns", arg_ifname, strv_length(arg_set_dns), arg_set_dns); | |
4446 | if (r < 0) | |
4447 | return r; | |
4448 | } | |
4449 | ||
4450 | if (arg_set_domain) { | |
4451 | r = translate("domain", arg_ifname, strv_length(arg_set_domain), arg_set_domain); | |
4452 | if (r < 0) | |
4453 | return r; | |
4454 | } | |
4455 | ||
4456 | if (arg_set_nta) { | |
4457 | r = translate("nta", arg_ifname, strv_length(arg_set_nta), arg_set_nta); | |
4458 | if (r < 0) | |
4459 | return r; | |
4460 | } | |
4461 | ||
4462 | if (arg_set_llmnr) { | |
4463 | r = translate("llmnr", arg_ifname, 1, (char **) &arg_set_llmnr); | |
4464 | if (r < 0) | |
4465 | return r; | |
4466 | } | |
4467 | ||
4468 | if (arg_set_mdns) { | |
4469 | r = translate("mdns", arg_ifname, 1, (char **) &arg_set_mdns); | |
4470 | if (r < 0) | |
4471 | return r; | |
4472 | } | |
4473 | ||
4474 | if (arg_set_dns_over_tls) { | |
4475 | r = translate("dnsovertls", arg_ifname, 1, (char **) &arg_set_dns_over_tls); | |
4476 | if (r < 0) | |
4477 | return r; | |
4478 | } | |
4479 | ||
4480 | if (arg_set_dnssec) { | |
4481 | r = translate("dnssec", arg_ifname, 1, (char **) &arg_set_dnssec); | |
4482 | if (r < 0) | |
4483 | return r; | |
4484 | } | |
4485 | ||
4486 | return r; | |
4487 | ||
4488 | case MODE_REVERT_LINK: | |
4489 | assert(arg_ifname); | |
4490 | ||
4491 | return translate("revert", arg_ifname, 0, NULL); | |
4492 | ||
4493 | case _MODE_INVALID: | |
4494 | assert_not_reached(); | |
4495 | } | |
4496 | ||
4497 | return 0; | |
4498 | } | |
4499 | ||
4500 | static int run(int argc, char **argv) { | |
4501 | bool compat = false; | |
4502 | int r; | |
4503 | ||
4504 | setlocale(LC_ALL, ""); | |
4505 | log_setup(); | |
4506 | ||
4507 | if (invoked_as(argv, "resolvconf")) { | |
4508 | compat = true; | |
4509 | r = resolvconf_parse_argv(argc, argv); | |
4510 | } else if (invoked_as(argv, "systemd-resolve")) { | |
4511 | compat = true; | |
4512 | r = compat_parse_argv(argc, argv); | |
4513 | } else | |
4514 | r = native_parse_argv(argc, argv); | |
4515 | if (r <= 0) | |
4516 | return r; | |
4517 | ||
4518 | if (compat) | |
4519 | return compat_main(argc, argv); | |
4520 | ||
4521 | return native_main(argc, argv); | |
4522 | } | |
4523 | ||
4524 | DEFINE_MAIN_FUNCTION(run); |