HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/resolve/resolvectl.c

155,789 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 149f229a7d7186a95b4a8379de226d95331c4a92

Lines 1001–2000 of 3,869

 
        STRV_FOREACH(p, reply.txt)
                printf("%*s%s\n", (int) indent, "", *p);
 
        const char *canonical_name = empty_to_null(reply.canonical.name);
        const char *canonical_type = empty_to_null(reply.canonical.type);
        const char *canonical_domain = reply.canonical.domain;
 
        if (!streq_ptr(name, canonical_name) ||
            !streq_ptr(type, canonical_type) ||
            !streq_ptr(domain, canonical_domain)) {
 
                printf("%*s(", (int) indent, "");
 
                if (canonical_name)
                        printf("%s/", canonical_name);
                if (canonical_type)
                        printf("%s/", canonical_type);
 
                printf("%s)\n", canonical_domain);
        }
 
        print_source(reply.flags, ts);
 
        return 0;
}
 
VERB(verb_service, "service", "[[NAME] TYPE] DOMAIN\0", 2, 4, 0,
     "Resolve service (SRV)");
static int verb_service(int argc, char *argv[], uintptr_t _data, void *userdata) {
        uint64_t flags = arg_flags;
 
        if (sd_json_format_enabled(arg_json_format_flags))
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format.");
 
        if (argc < 4 && !arg_service_txt_set)
                flags |= SD_RESOLVED_NO_TXT;
 
        if (argc == 2)
                return resolve_service(NULL, NULL, argv[1], flags);
        if (argc == 3)
                return resolve_service(NULL, argv[1], argv[2], flags);
 
        return resolve_service(argv[1], argv[2], argv[3], flags);
}
 
#if HAVE_OPENSSL
static int resolve_openpgp(const char *address) {
        int r;
 
        assert(address);
 
        const char *domain = strrchr(address, '@');
        if (!domain)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Address does not contain '@': \"%s\"", address);
        if (domain == address || domain[1] == '\0')
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Address starts or ends with '@': \"%s\"", address);
        domain++;
 
        _cleanup_free_ char *hashed = NULL;
        r = string_hashsum_sha256(address, domain - 1 - address, &hashed);
        if (r < 0)
                return log_error_errno(r, "Hashing failed: %m");
 
        strshorten(hashed, 56);
 
        _cleanup_free_ char *suffix = NULL;
        r = dns_name_concat("_openpgpkey", domain, /* flags= */ 0, &suffix);
        if (r < 0)
                return log_error_errno(r, "Failed to join DNS suffix: %m");
 
        _cleanup_free_ char *full = NULL;
        r = dns_name_concat(hashed, suffix, /* flags= */ 0, &full);
        if (r < 0)
                return log_error_errno(r, "Failed to join OPENPGPKEY name: %m");
        log_debug("Looking up \"%s\".", full);
 
        r = resolve_record(
                        full,
                        arg_class ?: DNS_CLASS_IN,
                        arg_type ?: DNS_TYPE_OPENPGPKEY,
                        /* warn_missing= */ false);
        if (!IN_SET(r, -ENXIO, -ESRCH)) /* Not NXDOMAIN or NODATA? Then fail immediately. */
                return r;
 
        hashed = mfree(hashed);
        r = string_hashsum_sha224(address, domain - 1 - address, &hashed);
        if (r < 0)
                return log_error_errno(r, "Hashing failed: %m");
 
        full = mfree(full);
        r = dns_name_concat(hashed, suffix, /* flags= */ 0, &full);
        if (r < 0)
                return log_error_errno(r, "Failed to join OPENPGPKEY name: %m");
        log_debug("Looking up \"%s\".", full);
 
        return resolve_record(
                        full,
                        arg_class ?: DNS_CLASS_IN,
                        arg_type ?: DNS_TYPE_OPENPGPKEY,
                        /* warn_missing= */ true);
}
#endif
 
VERB(verb_openpgp, "openpgp", "EMAIL@DOMAIN…\0", 2, VERB_ANY, 0,
     "Query OpenPGP public key");
static int verb_openpgp(int argc, char *argv[], uintptr_t _data, void *userdata) {
#if HAVE_OPENSSL
        int ret = 0;
 
        if (!IN_SET(arg_type, 0, DNS_TYPE_OPENPGPKEY))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The openpgp command may only be combined with --type=OPENPGPKEY.");
 
        STRV_FOREACH(p, strv_skip(argv, 1))
                RET_GATHER(ret, resolve_openpgp(*p));
 
        return ret;
#else
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "OpenSSL support is disabled, cannot query Open PGP keys.");
#endif
}
 
static int resolve_tlsa(const char *family, const char *address) {
        const char *port;
        uint16_t port_num = 443;
        _cleanup_free_ char *full = NULL;
        int r;
 
        assert(address);
 
        port = strrchr(address, ':');
        if (port) {
                r = parse_ip_port(port + 1, &port_num);
                if (r < 0)
                        return log_error_errno(r, "Invalid port \"%s\".", port + 1);
 
                address = strndupa_safe(address, port - address);
        }
 
        r = asprintf(&full, "_%u._%s.%s",
                     port_num,
                     family,
                     address);
        if (r < 0)
                return log_oom();
 
        log_debug("Looking up \"%s\".", full);
 
        return resolve_record(full,
                              arg_class ?: DNS_CLASS_IN,
                              arg_type ?: DNS_TYPE_TLSA, true);
}
 
static bool service_family_is_valid(const char *s) {
        return STR_IN_SET(s, "tcp", "udp", "sctp");
}
 
VERB(verb_tlsa, "tlsa", "DOMAIN[:PORT]…\0", 2, VERB_ANY, 0,
     "Query TLS public key");
static int verb_tlsa(int argc, char *argv[], uintptr_t _data, void *userdata) {
        const char *family = "tcp";
        char **args;
        int ret = 0;
 
        assert(argc >= 2);
 
        if (!IN_SET(arg_type, 0, DNS_TYPE_TLSA))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The tlsa command may only be combined with --type=TLSA.");
 
        if (service_family_is_valid(argv[1])) {
                family = argv[1];
                args = strv_skip(argv, 2);
        } else
                args = strv_skip(argv, 1);
 
        if (strv_isempty(args))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The tlsa command requires at least one domain.");
 
        STRV_FOREACH(p, args)
                RET_GATHER(ret, resolve_tlsa(family, *p));
 
        return ret;
}
 
static int varlink_dump_dns_configuration(sd_json_variant **ret) {
        _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
        sd_json_variant *reply = NULL;
        sd_json_variant *v;
        int r;
 
        assert(ret);
 
        r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve");
        if (r < 0)
                return log_error_errno(r, "Failed to connect to service /run/systemd/resolve/io.systemd.Resolve: %m");
 
        r = varlink_call_and_log(vl, "io.systemd.Resolve.DumpDNSConfiguration", /* parameters= */ NULL, &reply);
        if (r < 0)
                return r;
 
        v = sd_json_variant_by_key(reply, "configuration");
 
        if (!sd_json_variant_is_array(v))
                return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "DumpDNSConfiguration() response missing 'configuration' key.");
 
        *ret = sd_json_variant_ref(v);
        return 0;
}
 
static int status_json_filter_links(sd_json_variant **configuration, char **links) {
        _cleanup_set_free_ Set *links_by_index = NULL;
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
        sd_json_variant *w;
        int r;
 
        assert(configuration);
 
        if (links)
                STRV_FOREACH(ifname, links) {
                        int ifindex = rtnl_resolve_interface_or_warn(/* rtnl= */ NULL, *ifname);
                        if (ifindex < 0)
                                return ifindex;
 
                        r = set_ensure_put(&links_by_index, NULL, INT_TO_PTR(ifindex));
                        if (r < 0)
                                return r;
                }
 
        JSON_VARIANT_ARRAY_FOREACH(w, *configuration) {
                int ifindex = sd_json_variant_unsigned(sd_json_variant_by_key(w, "ifindex"));
 
                if (links_by_index) {
                        if (ifindex <= 0)
                                /* Possibly invalid, but most likely unset because this is global
                                 * or delegate configuration. */
                                continue;
 
                        if (!set_contains(links_by_index, INT_TO_PTR(ifindex)))
                                continue;
 
                } else if (ifindex == LOOPBACK_IFINDEX)
                        /* By default, exclude the loopback interface. */
                        continue;
 
                r = sd_json_variant_append_array(&v, w);
                if (r < 0)
                        return r;
        }
 
        return json_variant_unref_and_replace(*configuration, v);
}
 
static int status_json_filter_fields(sd_json_variant **configuration, StatusMode mode) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
        sd_json_variant *w;
        const char *field;
        int r;
 
        assert(configuration);
 
        field = status_mode_json_field_to_string(mode);
        if (!field)
                /* Nothing to filter for this mode. */
                return 0;
 
        JSON_VARIANT_ARRAY_FOREACH(w, *configuration) {
                /* Always include identifier fields like ifname or delegate, and include the requested
                 * field even if it is empty in the configuration. */
                r = sd_json_variant_append_arraybo(
                                &v,
                                JSON_BUILD_PAIR_VARIANT_NON_NULL("ifname", sd_json_variant_by_key(w, "ifname")),
                                JSON_BUILD_PAIR_VARIANT_NON_NULL("ifindex", sd_json_variant_by_key(w, "ifindex")),
                                JSON_BUILD_PAIR_VARIANT_NON_NULL("delegate", sd_json_variant_by_key(w, "delegate")),
                                SD_JSON_BUILD_PAIR_VARIANT(field, sd_json_variant_by_key(w, field)));
                if (r < 0)
                        return r;
        }
 
        return json_variant_unref_and_replace(*configuration, v);
}
 
static int format_dns_server_one(DNSConfiguration *configuration, DNSServer *s, char **ret) {
        bool global;
        int r;
 
        assert(s);
        assert(ret);
 
        global = !(configuration->ifindex > 0 || configuration->delegate);
 
        if (global && s->ifindex > 0 && s->ifindex != LOOPBACK_IFINDEX) {
                /* This one has an (non-loopback) ifindex set, and we were told to suppress those. Hence do so. */
                *ret = NULL;
                return 0;
        }
 
        r = in_addr_port_ifindex_name_to_string(
                        s->family,
                        &s->in_addr,
                        s->port != 53 ? s->port : 0,
                        s->ifindex,
                        s->server_name,
                        ret);
        if (r < 0)
                return r;
 
        return 1;
}
 
static int format_dns_servers(DNSConfiguration *configuration, OrderedSet *servers, char ***ret) {
        int r;
 
        assert(ret);
 
        _cleanup_strv_free_ char **l = NULL;
        DNSServer *s;
        ORDERED_SET_FOREACH(s, servers) {
                _cleanup_free_ char *str = NULL;
                r = format_dns_server_one(configuration, s, &str);
                if (r < 0)
                        return r;
                if (r > 0) {
                        r = strv_consume(&l, TAKE_PTR(str));
                        if (r < 0)
                                return log_oom();
                }
        }
 
        *ret = TAKE_PTR(l);
        return 0;
}
 
static int format_search_domains(DNSConfiguration *configuration, OrderedSet *domains, char ***ret) {
        int r;
 
        assert(ret);
 
        _cleanup_strv_free_ char **l = NULL;
        SearchDomain *d;
        ORDERED_SET_FOREACH(d, domains) {
                if (!(configuration->ifindex > 0 || configuration->delegate) && d->ifindex > 0)
                        /* Only show the global ones here */
                        continue;
 
                _cleanup_free_ char *str = NULL;
                if (d->route_only)
                        str = strjoin("~", d->name);
                else
                        str = strdup(d->name);
                if (!str)
                        return log_oom();
 
                r = strv_consume(&l, TAKE_PTR(str));
                if (r < 0)
                        return log_oom();
        }
 
        *ret = TAKE_PTR(l);
        return 0;
}
 
static int format_protocol_status(DNSConfiguration *configuration, char ***ret) {
        _cleanup_strv_free_ char **s = NULL;
        int r;
 
        assert(configuration);
        assert(ret);
 
        if (configuration->ifindex > 0) {
                r = strv_extendf(&s, "%sDefaultRoute", plus_minus(configuration->default_route));
                if (r < 0)
                        return r;
        }
 
        r = strv_extend_extended_bool(&s, "LLMNR", configuration->llmnr_mode_str);
        if (r < 0)
                return r;
 
        r = strv_extend_extended_bool(&s, "mDNS", configuration->mdns_mode_str);
        if (r < 0)
                return r;
 
        r = strv_extend_extended_bool(&s, "DNSOverTLS", configuration->dns_over_tls_mode_str);
        if (r < 0)
                return r;
 
        r = strv_extendf(&s, "DNSSEC=%s/%s",
                         configuration->dnssec_mode_str ?: "???",
                         configuration->dnssec_supported ? "supported" : "unsupported");
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(s);
        return 0;
}
 
static int format_scopes_string(DNSConfiguration *configuration, char **ret) {
        assert(configuration);
        assert(ret);
 
        if (!configuration->dns_scopes) {
                *ret = NULL;
                return 0;
        }
 
        uint64_t scopes_mask = 0;
        DNSScope *scope;
        SET_FOREACH(scope, configuration->dns_scopes) {
                if (streq(scope->protocol, "dns"))
                        scopes_mask |= SD_RESOLVED_DNS;
                else if (streq(scope->protocol, "llmnr"))
                        scopes_mask |= scope->family == AF_INET ?
                                       SD_RESOLVED_LLMNR_IPV4 :
                                       SD_RESOLVED_LLMNR_IPV6;
                else if (streq(scope->protocol, "mdns"))
                        scopes_mask |= scope->family == AF_INET ?
                                       SD_RESOLVED_MDNS_IPV4 :
                                       SD_RESOLVED_MDNS_IPV6;
        }
 
        _cleanup_free_ char *buf = NULL;
        if (asprintf(&buf, "%s%s%s%s%s",
                     scopes_mask & SD_RESOLVED_DNS ? "DNS " : "",
                     scopes_mask & SD_RESOLVED_LLMNR_IPV4 ? "LLMNR/IPv4 " : "",
                     scopes_mask & SD_RESOLVED_LLMNR_IPV6 ? "LLMNR/IPv6 " : "",
                     scopes_mask & SD_RESOLVED_MDNS_IPV4 ? "mDNS/IPv4 " : "",
                     scopes_mask & SD_RESOLVED_MDNS_IPV6 ? "mDNS/IPv6 " : "") < 0)
                return log_oom();
 
        size_t len = strlen(buf);
        assert(len > 0);
        buf[len - 1] = '\0';
 
        *ret = TAKE_PTR(buf);
        return 0;
}
 
static void status_print_header(DNSConfiguration *c) {
        assert(c);
 
        if (c->ifname)
                printf("%sLink %i (%s)%s\n",
                       ansi_highlight(),
                       c->ifindex,
                       c->ifname,
                       ansi_normal());
        else if (c->delegate)
                printf("%sDelegate %s%s\n",
                       ansi_highlight(),
                       c->delegate,
                       ansi_normal());
        else
                printf("%sGlobal%s\n", ansi_highlight(), ansi_normal());
}
 
static void status_print_string(DNSConfiguration *c, const char *p) {
        assert(c);
 
        if (c->ifname)
                printf("%sLink %i (%s)%s: %s\n",
                       ansi_highlight(),
                       c->ifindex,
                       c->ifname,
                       ansi_normal(),
                       p);
        else if (c->delegate)
                printf("%sDelegate %s%s: %s\n",
                       ansi_highlight(),
                       c->delegate,
                       ansi_normal(),
                       p);
        else
                printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), p);
}
 
static int status_print_strv(DNSConfiguration *c,  char **p) {
        const unsigned indent = strlen("Global: "); /* Use the same indentation everywhere to make things nice */
        int pos1, pos2;
 
        assert(c);
 
        if (c->ifname)
                printf("%s%nLink %i (%s)%n%s:", ansi_highlight(), &pos1, c->ifindex, c->ifname, &pos2, ansi_normal());
        else if (c->delegate)
                printf("%s%nDelegate %s%n%s:", ansi_highlight(), &pos1, c->delegate, &pos2, ansi_normal());
        else
                printf("%s%nGlobal%n%s:", ansi_highlight(), &pos1, &pos2, ansi_normal());
 
        size_t cols = columns(), position = pos2 - pos1 + 2;
 
        STRV_FOREACH(i, p) {
                size_t our_len = utf8_console_width(*i); /* This returns -1 on invalid utf-8 (which shouldn't happen).
                                                          * If that happens, we'll just print one item per line. */
 
                if (position <= indent || size_add(size_add(position, 1), our_len) < cols) {
                        printf(" %s", *i);
                        position = size_add(size_add(position, 1), our_len);
                } else {
                        printf("\n%*s%s", (int) indent, "", *i);
                        position = size_add(our_len, indent);
                }
        }
 
        printf("\n");
 
        return 0;
}
 
static int dump_list(Table *table, const char *field, char * const *l) {
        int r;
 
        if (strv_isempty(l))
                return 0;
 
        r = table_add_many(table,
                           TABLE_FIELD, field,
                           TABLE_STRV_WRAPPED, l);
        if (r < 0)
                return table_log_add_error(r);
 
        return 0;
}
 
static int print_configuration(DNSConfiguration *configuration, StatusMode mode, bool *empty_line) {
        _cleanup_(table_unrefp) Table *table = NULL;
        int r;
 
        assert(configuration);
        POINTER_MAY_BE_NULL(empty_line);
 
        pager_open(arg_pager_flags);
 
        bool global = !(configuration->ifindex > 0 || configuration->delegate);
        if (mode == STATUS_DNS) {
                _cleanup_strv_free_ char **l = NULL;
                r = format_dns_servers(configuration, configuration->dns_servers, &l);
                if (r < 0)
                        return r;
 
                return status_print_strv(configuration, l);
 
        } else if (mode == STATUS_DOMAIN) {
                _cleanup_strv_free_ char **l = NULL;
                r = format_search_domains(configuration, configuration->search_domains, &l);
                if (r < 0)
                        return r;
 
                return status_print_strv(configuration, l);
 
        } else if (mode == STATUS_NTA) {
                if (configuration->delegate)
                        return 0;
 
                return status_print_strv(configuration, configuration->negative_trust_anchors);
 
        } else if (mode == STATUS_DEFAULT_ROUTE) {
                if (global)
                        return 0;
 
                status_print_string(configuration, yes_no(configuration->default_route));
 
                return 0;
 
        } else if (IN_SET(mode, STATUS_LLMNR, STATUS_MDNS, STATUS_DNS_OVER_TLS, STATUS_DNSSEC)) {
                if (configuration->delegate)
                        return 0;
 
                status_print_string(configuration,
                                    strna(mode == STATUS_LLMNR ? configuration->llmnr_mode_str :
                                          mode == STATUS_MDNS ? configuration->mdns_mode_str :
                                          mode == STATUS_DNS_OVER_TLS ? configuration->dns_over_tls_mode_str :
                                          configuration->dnssec_mode_str));
 
                return 0;
 
        } else if (mode != STATUS_ALL)
                return 0;
 
        if (empty_line && *empty_line)
                fputc('\n', stdout);
 
        status_print_header(configuration);
 
        table = table_new_vertical();
        if (!table)
                return log_oom();
 
        if (configuration->ifindex > 0) {
                r = table_add_many(table,
                                   TABLE_FIELD, "Current Scopes",
                                   TABLE_SET_MINIMUM_WIDTH, 19);
                if (r < 0)
                        return table_log_add_error(r);
 
                _cleanup_free_ char *s = NULL;
                r = format_scopes_string(configuration, &s);
                if (r < 0)
                        return r;
 
                r = table_add_cell(table, NULL, TABLE_STRING, s ?: "none");
                if (r < 0)
                        return table_log_add_error(r);
        }
 
        if (!configuration->delegate) {
                _cleanup_strv_free_ char **l = NULL;
                r = format_protocol_status(configuration, &l);
                if (r < 0)
                        return r;
 
                r = table_add_many(table,
                                   TABLE_FIELD, "Protocols",
                                   TABLE_STRV_WRAPPED, l);
                if (r < 0)
                        return table_log_add_error(r);
        }
 
        if (configuration->resolv_conf_mode_str) {
                r = table_add_many(table,
                                   TABLE_FIELD, "resolv.conf mode",
                                   TABLE_STRING, configuration->resolv_conf_mode_str);
                if (r < 0)
                        return table_log_add_error(r);
        }
 
        if (configuration->current_dns_server) {
                _cleanup_free_ char *s = NULL;
                r = format_dns_server_one(configuration, configuration->current_dns_server, &s);
                if (r < 0)
                        return r;
 
                r = table_add_many(table,
                                   TABLE_FIELD, "Current DNS Server",
                                   TABLE_STRING, s);
                if (r < 0)
                        return table_log_add_error(r);
        }
 
        _cleanup_strv_free_ char **dns_servers = NULL;
        r = format_dns_servers(configuration, configuration->dns_servers, &dns_servers);
        if (r < 0)
                return r;
 
        r = dump_list(table, "DNS Servers", dns_servers);
        if (r < 0)
                return r;
 
        if (global) {
                _cleanup_strv_free_ char **l = NULL;
                r = format_dns_servers(configuration, configuration->fallback_dns_servers, &l);
                if (r < 0)
                        return r;
 
                r = dump_list(table, "Fallback DNS Servers", l);
                if (r < 0)
                        return r;
        }
 
        _cleanup_strv_free_ char **search_domains = NULL;
        r = format_search_domains(configuration, configuration->search_domains, &search_domains);
        if (r < 0)
                return r;
 
        r = dump_list(table, "DNS Domain", search_domains);
        if (r < 0)
                return r;
 
        if (!global) {
                r = table_add_many(table,
                                   TABLE_FIELD, "Default Route",
                                   TABLE_BOOLEAN, configuration->default_route);
                if (r < 0)
                        return table_log_add_error(r);
        }
 
        r = table_print_or_warn(table);
        if (r < 0)
                return r;
 
        if (empty_line)
                *empty_line = true;
 
        return 0;
}
 
static int status_full(StatusMode mode, char **links) {
        bool empty_line = false;
        int r;
 
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
        r = varlink_dump_dns_configuration(&v);
        if (r < 0)
                return r;
 
        r = status_json_filter_links(&v, links);
        if (r < 0)
                return log_error_errno(r, "Failed to filter configuration JSON links: %m");
 
        if (sd_json_format_enabled(arg_json_format_flags)) {
                r = status_json_filter_fields(&v, mode);
                if (r < 0)
                        return log_error_errno(r, "Failed to filter configuration JSON fields: %m");
 
                return sd_json_variant_dump(v, arg_json_format_flags, /* f= */ NULL, /* prefix= */ NULL);
        }
 
        _cleanup_(dns_configuration_freep) DNSConfiguration *global_config = NULL;
        _cleanup_ordered_set_free_ OrderedSet *link_configs = NULL, *delegate_configs = NULL;
        sd_json_variant *w;
        JSON_VARIANT_ARRAY_FOREACH(w, v) {
                _cleanup_(dns_configuration_freep) DNSConfiguration *c = NULL;
                r = dns_configuration_from_json(w, &c);
                if (r < 0)
                        return r;
 
                if (c->ifindex > 0) {
                        r = ordered_set_ensure_put(&link_configs, &dns_configuration_hash_ops, c);
                        if (r < 0)
                                return r;
                        TAKE_PTR(c);
                } else if (c->delegate) {
                        r = ordered_set_ensure_put(&delegate_configs, &dns_configuration_hash_ops, c);
                        if (r < 0)
                                return r;
                        TAKE_PTR(c);
                } else {
                        assert(!global_config);
                        global_config = TAKE_PTR(c);
                }
        }
 
        if (global_config) {
                r = print_configuration(global_config, mode, &empty_line);
                if (r < 0)
                        return r;
        }
 
        DNSConfiguration *c;
        ORDERED_SET_FOREACH(c, link_configs) {
                r = print_configuration(c, mode, &empty_line);
                if (r < 0)
                        return r;
        }
 
        ORDERED_SET_FOREACH(c, delegate_configs) {
                r = print_configuration(c, mode, &empty_line);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int status_all(StatusMode mode) {
        return status_full(mode, /* links= */ NULL);
}
 
static int status_ifindex(int ifindex, StatusMode mode) {
        int r;
        char ifname[IF_NAMESIZE];
 
        assert(ifindex > 0);
 
        r = format_ifname(ifindex, ifname);
        if (r < 0)
                return log_error_errno(r, "Failed to resolve interface name for %i: %m", ifindex);
 
        return status_full(mode, STRV_MAKE(ifname));
}
 
VERB(verb_status, "status", "[LINK…]\0", VERB_ANY, VERB_ANY, VERB_DEFAULT,
     "Show link and server status");
static int verb_status(int argc, char *argv[], uintptr_t _data, void *userdata) {
        return status_full(STATUS_ALL, strv_skip(argv, 1));
}
 
VERB(verb_show_statistics, "statistics", NULL, VERB_ANY, 1, 0,
     "Show resolver statistics");
static int verb_show_statistics(int argc, char *argv[], uintptr_t _data, void *userdata) {
        _cleanup_(table_unrefp) Table *table = NULL;
        sd_json_variant *reply = NULL;
        _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
        int r;
 
        (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
 
        r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor");
        if (r < 0)
                return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m");
 
        r = varlink_callbo_and_log(
                        vl,
                        "io.systemd.Resolve.Monitor.DumpStatistics",
                        &reply,
                        SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password));
        if (r < 0)
                return r;
 
        if (sd_json_format_enabled(arg_json_format_flags))
                return sd_json_variant_dump(reply, arg_json_format_flags, NULL, NULL);
 
        struct statistics {
                sd_json_variant *transactions;
                sd_json_variant *cache;
                sd_json_variant *dnssec;
        } statistics;
 
        static const sd_json_dispatch_field statistics_dispatch_table[] = {
                { "transactions", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, transactions), SD_JSON_MANDATORY },
                { "cache",        SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, cache),        SD_JSON_MANDATORY },
                { "dnssec",       SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, dnssec),       SD_JSON_MANDATORY },
                {},
        };
 
        r = sd_json_dispatch(reply, statistics_dispatch_table, SD_JSON_LOG, &statistics);
        if (r < 0)
                return r;
 
        struct transactions {
                uint64_t n_current_transactions;
                uint64_t n_transactions_total;
                uint64_t n_timeouts_total;
                uint64_t n_timeouts_served_stale_total;
                uint64_t n_failure_responses_total;
                uint64_t n_failure_responses_served_stale_total;
        } transactions;
 
        static const sd_json_dispatch_field transactions_dispatch_table[] = {
                { "currentTransactions",             _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_current_transactions),                 SD_JSON_MANDATORY },
                { "totalTransactions",               _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_transactions_total),                   SD_JSON_MANDATORY },
                { "totalTimeouts",                   _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_timeouts_total),                       SD_JSON_MANDATORY },
                { "totalTimeoutsServedStale",        _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_timeouts_served_stale_total),          SD_JSON_MANDATORY },
                { "totalFailedResponses",            _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_total),              SD_JSON_MANDATORY },
                { "totalFailedResponsesServedStale", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_served_stale_total), SD_JSON_MANDATORY },
                {},
        };
 
        r = sd_json_dispatch(statistics.transactions, transactions_dispatch_table, SD_JSON_LOG, &transactions);
        if (r < 0)
                return r;
 
        struct cache {
                uint64_t cache_size;
                uint64_t n_cache_hit;
                uint64_t n_cache_miss;
        } cache;
 
        static const sd_json_dispatch_field cache_dispatch_table[] = {
                { "size",   _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, cache_size),   SD_JSON_MANDATORY },
                { "hits",   _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, n_cache_hit),  SD_JSON_MANDATORY },
                { "misses", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, n_cache_miss), SD_JSON_MANDATORY },
                {},
        };
 
        r = sd_json_dispatch(statistics.cache, cache_dispatch_table, SD_JSON_LOG, &cache);
        if (r < 0)
                return r;
 
        struct dnsssec {
                uint64_t n_dnssec_secure;
                uint64_t n_dnssec_insecure;
                uint64_t n_dnssec_bogus;
                uint64_t n_dnssec_indeterminate;
        } dnsssec;
 
        static const sd_json_dispatch_field dnssec_dispatch_table[] = {
                { "secure",        _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_secure),        SD_JSON_MANDATORY },
                { "insecure",      _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_insecure),      SD_JSON_MANDATORY },
                { "bogus",         _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_bogus),         SD_JSON_MANDATORY },
                { "indeterminate", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_indeterminate), SD_JSON_MANDATORY },
                {},
        };
 
        r = sd_json_dispatch(statistics.dnssec, dnssec_dispatch_table, SD_JSON_LOG, &dnsssec);
        if (r < 0)
                return r;
 
        table = table_new_vertical();
        if (!table)
                return log_oom();
 
        r = table_add_many(table,
                           TABLE_STRING, "Transactions",
                           TABLE_SET_COLOR, ansi_highlight(),
                           TABLE_SET_ALIGN_PERCENT, 0,
                           TABLE_EMPTY,
                           TABLE_FIELD, "Current Transactions",
                           TABLE_SET_ALIGN_PERCENT, 100,
                           TABLE_UINT64, transactions.n_current_transactions,
                           TABLE_SET_ALIGN_PERCENT, 100,
                           TABLE_FIELD, "Total Transactions",
                           TABLE_UINT64, transactions.n_transactions_total,
                           TABLE_EMPTY, TABLE_EMPTY,
                           TABLE_STRING, "Cache",
                           TABLE_SET_COLOR, ansi_highlight(),
                           TABLE_SET_ALIGN_PERCENT, 0,
                           TABLE_EMPTY,
                           TABLE_FIELD, "Current Cache Size",
                           TABLE_SET_ALIGN_PERCENT, 100,
                           TABLE_UINT64, cache.cache_size,
                           TABLE_FIELD, "Cache Hits",
                           TABLE_UINT64, cache.n_cache_hit,
                           TABLE_FIELD, "Cache Misses",
                           TABLE_UINT64, cache.n_cache_miss,
                           TABLE_EMPTY, TABLE_EMPTY,
                           TABLE_STRING, "Failure Transactions",
                           TABLE_SET_COLOR, ansi_highlight(),
                           TABLE_SET_ALIGN_PERCENT, 0,
                           TABLE_EMPTY,
                           TABLE_FIELD, "Total Timeouts",
                           TABLE_SET_ALIGN_PERCENT, 100,
                           TABLE_UINT64, transactions.n_timeouts_total,
                           TABLE_FIELD, "Total Timeouts (Stale Data Served)",
                           TABLE_UINT64, transactions.n_timeouts_served_stale_total,
                           TABLE_FIELD, "Total Failure Responses",
                           TABLE_UINT64, transactions.n_failure_responses_total,
                           TABLE_FIELD, "Total Failure Responses (Stale Data Served)",
                           TABLE_UINT64, transactions.n_failure_responses_served_stale_total,
                           TABLE_EMPTY, TABLE_EMPTY,
                           TABLE_STRING, "DNSSEC Verdicts",
                           TABLE_SET_COLOR, ansi_highlight(),
                           TABLE_SET_ALIGN_PERCENT, 0,
                           TABLE_EMPTY,
                           TABLE_FIELD, "Secure",
                           TABLE_SET_ALIGN_PERCENT, 100,
                           TABLE_UINT64, dnsssec.n_dnssec_secure,
                           TABLE_FIELD, "Insecure",
                           TABLE_UINT64, dnsssec.n_dnssec_insecure,
                           TABLE_FIELD, "Bogus",
                           TABLE_UINT64, dnsssec.n_dnssec_bogus,
                           TABLE_FIELD, "Indeterminate",
                           TABLE_UINT64, dnsssec.n_dnssec_indeterminate
                          );
        if (r < 0)
                return table_log_add_error(r);
 
        return table_print_or_warn(table);
}
 
VERB(verb_reset_statistics, "reset-statistics", NULL, VERB_ANY, 1, 0,
     "Reset resolver statistics");
static int verb_reset_statistics(int argc, char *argv[], uintptr_t _data, void *userdata) {
        sd_json_variant *reply = NULL;
        _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
        int r;
 
        (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
 
        r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor");
        if (r < 0)
                return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m");
 
        r = varlink_callbo_and_log(
                        vl,
                        "io.systemd.Resolve.Monitor.ResetStatistics",
                        &reply,
                        SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password));
        if (r < 0)
                return r;
 
        if (sd_json_format_enabled(arg_json_format_flags))
                return sd_json_variant_dump(reply, arg_json_format_flags, NULL, NULL);
 
        return 0;
}
 
VERB(verb_flush_caches, "flush-caches", NULL, VERB_ANY, 1, 0,
     "Flush all local DNS caches");
static int verb_flush_caches(int argc, char *argv[], uintptr_t _data, void *userdata) {
        int r;
 
        (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
 
        _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
        r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor");
        if (r < 0)
                return log_error_errno(r, "Failed to connect to /run/systemd/resolve/io.systemd.Resolve.Monitor: %m");
 
        return varlink_callbo_and_log(
                        vl,
                        "io.systemd.Resolve.Monitor.FlushCaches",
                        /* reply= */ NULL,
                        SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password));
}
 
VERB(verb_reset_server_features, "reset-server-features", NULL, VERB_ANY, 1, 0,
     "Forget learnt DNS server feature levels");
static int verb_reset_server_features(int argc, char *argv[], uintptr_t _data, void *userdata) {
        int r;
 
        (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
 
        _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
        r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor");
        if (r < 0)
                return log_error_errno(r, "Failed to connect to /run/systemd/resolve/io.systemd.Resolve.Monitor: %m");
 
        return varlink_callbo_and_log(