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