HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/resolve/resolved-bus.c

87,973 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 650259a8c0d21bec8049f497838f5a0141586a8e

Lines 1–1000 of 2,422

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include "sd-bus.h"
 
#include "alloc-util.h"
#include "bus-common-errors.h"
#include "bus-get-properties.h"
#include "bus-locator.h"
#include "bus-log-control-api.h"
#include "bus-message-util.h"
#include "bus-object.h"
#include "bus-polkit.h"
#include "bus-util.h"
#include "dns-answer.h"
#include "dns-domain.h"
#include "dns-packet.h"
#include "dns-question.h"
#include "dns-rr.h"
#include "format-util.h"
#include "path-util.h"
#include "resolve-util.h"
#include "resolved-bus.h"
#include "resolved-def.h"
#include "resolved-dns-delegate-bus.h"
#include "resolved-dns-delegate.h"
#include "resolved-dns-dnssec.h"
#include "resolved-dns-query.h"
#include "resolved-dns-scope.h"
#include "resolved-dns-search-domain.h"
#include "resolved-dns-server.h"
#include "resolved-dns-stream.h"
#include "resolved-dns-stub.h"
#include "resolved-dns-synthesize.h"
#include "resolved-dns-transaction.h"
#include "resolved-dnssd-bus.h"
#include "resolved-dnssd.h"
#include "resolved-link-bus.h"
#include "resolved-link.h"
#include "resolved-manager.h"
#include "resolved-resolv-conf.h"
#include "set.h"
#include "socket-netlink.h"
#include "string-util.h"
#include "utf8.h"
 
BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_resolve_support, resolve_support, ResolveSupport);
 
static int dns_query_new_for_bus(
                Manager *m,
                DnsQuery **ret,
                DnsQuestion *question_utf8,
                DnsQuestion *question_idna,
                DnsPacket *question_bypass,
                int ifindex,
                uint64_t flags,
                sd_bus_error *error) {
 
        int r;
 
        r = dns_query_new(m, ret, question_utf8, question_idna, question_bypass, ifindex, flags);
        if (r == -ENOANO)
                return sd_bus_error_set(error, BUS_ERROR_DNS_REFUSED, "DNS query type refused.");
        return r;
}
 
static int query_on_bus_track(sd_bus_track *t, void *userdata) {
        DnsQuery *q = ASSERT_PTR(userdata);
 
        assert(t);
 
        if (!DNS_TRANSACTION_IS_LIVE(q->state))
                return 0;
 
        log_debug("Client of active query vanished, aborting query.");
        dns_query_complete(q, DNS_TRANSACTION_ABORTED);
        return 0;
}
 
static int dns_query_bus_track(DnsQuery *q, sd_bus_message *m) {
        int r;
 
        assert(q);
        assert(m);
 
        if (!q->bus_track) {
                r = sd_bus_track_new(sd_bus_message_get_bus(m), &q->bus_track, query_on_bus_track, q);
                if (r < 0)
                        return r;
        }
 
        r = sd_bus_track_add_sender(q->bus_track, m);
        if (r < 0)
                return r;
 
        return 0;
}
 
static sd_bus_message *dns_query_steal_request(DnsQuery *q) {
        assert(q);
 
        /* Find the main query, it's the one that owns the message */
        while (q->auxiliary_for)
                q = q->auxiliary_for;
 
        /* Let's take the request message out of the DnsQuery object, so that we never send requests twice */
        return TAKE_PTR(q->bus_request);
}
 
_sd_printf_(3, 4) static int reply_method_errorf(
                DnsQuery *query,
                const char *error_name,
                const char *format,
                ...) {
 
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
        va_list ap;
        int r;
 
        assert(query);
        assert(format);
 
        req = dns_query_steal_request(query);
        if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
                return 0;
 
        va_start(ap, format);
        r = sd_bus_reply_method_errorfv(req, error_name, format, ap);
        va_end(ap);
 
        return r;
}
 
_sd_printf_(3, 4) static int reply_method_errnof(
                DnsQuery *query,
                int err,
                const char *format,
                ...) {
 
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
        int r;
 
        assert(query);
 
        req = dns_query_steal_request(query);
        if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
                return 0;
 
        if (format) {
                va_list ap;
 
                va_start(ap, format);
                r = sd_bus_reply_method_errnofv(req, err, format, ap);
                va_end(ap);
        } else
                r = sd_bus_reply_method_errno(req, err, NULL);
 
        return r;
}
 
static int reply_query_state(DnsQuery *q) {
        assert(q);
 
        switch (q->state) {
 
        case DNS_TRANSACTION_NO_SERVERS:
                return reply_method_errorf(q, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
 
        case DNS_TRANSACTION_TIMEOUT:
                return reply_method_errorf(q, SD_BUS_ERROR_TIMEOUT, "Query timed out");
 
        case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
                return reply_method_errorf(q, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
 
        case DNS_TRANSACTION_INVALID_REPLY:
                return reply_method_errorf(q, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
 
        case DNS_TRANSACTION_ERRNO:
                return reply_method_errnof(q, q->answer_errno, "Lookup failed due to system error: %m");
 
        case DNS_TRANSACTION_ABORTED:
                return reply_method_errorf(q, BUS_ERROR_ABORTED, "Query aborted");
 
        case DNS_TRANSACTION_DNSSEC_FAILED:
                return reply_method_errorf(q, BUS_ERROR_DNSSEC_FAILED, "DNSSEC validation failed: %s%s%s%s%s%s",
                                           dnssec_result_to_string(q->answer_dnssec_result),
                                           q->answer_ede_rcode >= 0 ? " (" : "",
                                           q->answer_ede_rcode >= 0 ? FORMAT_DNS_EDE_RCODE(q->answer_ede_rcode) : "",
                                           (q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg)) ? ": " : "",
                                           q->answer_ede_rcode >= 0 ? strempty(q->answer_ede_msg) : "",
                                           q->answer_ede_rcode >= 0 ? ")" : "");
 
        case DNS_TRANSACTION_NO_TRUST_ANCHOR:
                return reply_method_errorf(q, BUS_ERROR_NO_TRUST_ANCHOR, "No suitable trust anchor known");
 
        case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED:
                return reply_method_errorf(q, BUS_ERROR_RR_TYPE_UNSUPPORTED, "Server does not support requested resource record type");
 
        case DNS_TRANSACTION_NETWORK_DOWN:
                return reply_method_errorf(q, BUS_ERROR_NETWORK_DOWN, "Network is down");
 
        case DNS_TRANSACTION_NOT_FOUND:
                /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
                 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
                return reply_method_errorf(q, BUS_ERROR_DNS_NXDOMAIN, "'%s' not found", dns_query_string(q));
 
        case DNS_TRANSACTION_NO_SOURCE:
                return reply_method_errorf(q, BUS_ERROR_NO_SOURCE, "All suitable resolution sources turned off");
 
        case DNS_TRANSACTION_STUB_LOOP:
                return reply_method_errorf(q, BUS_ERROR_STUB_LOOP, "Configured DNS server loops back to us");
 
        case DNS_TRANSACTION_RCODE_FAILURE: {
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
 
                req = dns_query_steal_request(q);
                if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
                        return 0;
 
                if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
                        sd_bus_error_setf(&error, BUS_ERROR_DNS_NXDOMAIN, "Name '%s' not found", dns_query_string(q));
                else {
                        const char *rc, *n;
 
                        rc = FORMAT_DNS_RCODE(q->answer_rcode);
                        n = strjoina(_BUS_ERROR_DNS, rc);
                        sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error: %s%s%s%s%s%s",
                                          dns_query_string(q), rc,
                                          q->answer_ede_rcode >= 0 ? " (" : "",
                                          q->answer_ede_rcode >= 0 ? FORMAT_DNS_EDE_RCODE(q->answer_ede_rcode) : "",
                                          (q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg)) ? ": " : "",
                                          q->answer_ede_rcode >= 0 ? strempty(q->answer_ede_msg) : "",
                                          q->answer_ede_rcode >= 0 ? ")" : "");
                }
 
                return sd_bus_reply_method_error(req, &error);
        }
 
        case DNS_TRANSACTION_NULL:
        case DNS_TRANSACTION_PENDING:
        case DNS_TRANSACTION_VALIDATING:
        case DNS_TRANSACTION_SUCCESS:
        default:
                assert_not_reached();
        }
}
 
static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
        int r;
 
        assert(reply);
        assert(rr);
 
        r = sd_bus_message_open_container(reply, 'r', "iiay");
        if (r < 0)
                return r;
 
        r = sd_bus_message_append(reply, "i", ifindex);
        if (r < 0)
                return r;
 
        if (rr->key->type == DNS_TYPE_A) {
                r = sd_bus_message_append(reply, "i", AF_INET);
                if (r < 0)
                        return r;
 
                r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
 
        } else if (rr->key->type == DNS_TYPE_AAAA) {
                r = sd_bus_message_append(reply, "i", AF_INET6);
                if (r < 0)
                        return r;
 
                r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
        } else
                return -EAFNOSUPPORT;
 
        if (r < 0)
                return r;
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        return 0;
}
 
static void bus_method_resolve_hostname_complete(DnsQuery *query) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(dns_query_freep) DnsQuery *q = query;
        _cleanup_free_ char *normalized = NULL;
        DnsQuestion *question;
        DnsResourceRecord *rr;
        unsigned added = 0;
        int ifindex, r;
 
        assert(q);
 
        if (q->state != DNS_TRANSACTION_SUCCESS) {
                r = reply_query_state(q);
                goto finish;
        }
 
        r = dns_query_process_cname_many(q);
        if (r == -ELOOP) {
                r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
                goto finish;
        }
        if (r < 0)
                goto finish;
        if (r == DNS_QUERY_CNAME) {
                /* This was a cname, and the query was restarted. */
                TAKE_PTR(q);
                return;
        }
 
        r = sd_bus_message_new_method_return(q->bus_request, &reply);
        if (r < 0)
                goto finish;
 
        r = sd_bus_message_open_container(reply, 'a', "(iiay)");
        if (r < 0)
                goto finish;
 
        question = dns_query_question_for_protocol(q, q->answer_protocol);
 
        DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
 
                r = dns_question_matches_rr(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
                if (r < 0)
                        goto finish;
                if (r == 0)
                        continue;
 
                r = append_address(reply, rr, ifindex);
                if (r < 0)
                        goto finish;
 
                if (!canonical)
                        canonical = dns_resource_record_ref(rr);
 
                added++;
        }
 
        if (added <= 0) {
                r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
                goto finish;
        }
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                goto finish;
 
        /* The key names are not necessarily normalized, make sure that they are when we return them to our
         * bus clients. */
        assert(canonical);
        r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
        if (r < 0)
                goto finish;
 
        /* Return the precise spelling and uppercasing and CNAME target reported by the server */
        r = sd_bus_message_append(
                        reply, "st",
                        normalized,
                        dns_query_reply_flags_make(q));
        if (r < 0)
                goto finish;
 
        q->bus_request = sd_bus_message_unref(q->bus_request);
        r = sd_bus_send(q->manager->bus, reply, NULL);
 
finish:
        if (r < 0) {
                log_error_errno(r, "Failed to send hostname reply: %m");
                (void) reply_method_errnof(q, r, NULL);
        }
}
 
static int parse_as_address(sd_bus_message *m, int ifindex, const char *hostname, int family, uint64_t flags) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_free_ char *canonical = NULL;
        union in_addr_union parsed;
        int r, ff, parsed_ifindex = 0;
 
        /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
         * let's not attempt to look it up. */
 
        r = in_addr_ifindex_from_string_auto(hostname, &ff, &parsed, &parsed_ifindex);
        if (r < 0) /* not an address */
                return 0;
 
        if (family != AF_UNSPEC && ff != family)
                return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address is not of the requested family.");
        if (ifindex > 0 && parsed_ifindex > 0 && parsed_ifindex != ifindex)
                return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address interface index does not match requested interface.");
 
        if (parsed_ifindex > 0)
                ifindex = parsed_ifindex;
 
        r = sd_bus_message_new_method_return(m, &reply);
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(reply, 'a', "(iiay)");
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(reply, 'r', "iiay");
        if (r < 0)
                return r;
 
        r = sd_bus_message_append(reply, "ii", ifindex, ff);
        if (r < 0)
                return r;
 
        r = sd_bus_message_append_array(reply, 'y', &parsed, FAMILY_ADDRESS_SIZE(ff));
        if (r < 0)
                return r;
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
         * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
         * omissions are always done the same way). */
        r = in_addr_ifindex_to_string(ff, &parsed, ifindex, &canonical);
        if (r < 0)
                return r;
 
        r = sd_bus_message_append(reply, "st", canonical,
                                  SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags), ff, true, true) |
                                  SD_RESOLVED_SYNTHETIC);
        if (r < 0)
                return r;
 
        return sd_bus_send(sd_bus_message_get_bus(m), reply, NULL);
}
 
void bus_client_log(sd_bus_message *m, const char *what) {
        _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
        const char *comm = NULL;
        uid_t uid = UID_INVALID;
        pid_t pid = 0;
        int r;
 
        assert(m);
        assert(what);
 
        if (!DEBUG_LOGGING)
                return;
 
        r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_COMM|SD_BUS_CREDS_AUGMENT, &creds);
        if (r < 0)
                return (void) log_debug_errno(r, "Failed to query client credentials, ignoring: %m");
 
        (void) sd_bus_creds_get_uid(creds, &uid);
        (void) sd_bus_creds_get_pid(creds, &pid);
        (void) sd_bus_creds_get_comm(creds, &comm);
 
        log_debug("D-Bus %s request from client PID " PID_FMT " (%s) with UID " UID_FMT,
                  what, pid, strna(comm), uid);
}
 
static int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
        _cleanup_(dns_query_freep) DnsQuery *q = NULL;
        Manager *m = ASSERT_PTR(userdata);
        const char *hostname;
        int family, ifindex;
        uint64_t flags;
        int r;
 
        assert(message);
 
        assert_cc(sizeof(int) == sizeof(int32_t));
 
        r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
        if (r < 0)
                return r;
 
        if (ifindex < 0)
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
 
        if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
 
        if (validate_and_mangle_query_flags(m, &flags, hostname, SD_RESOLVED_NO_SEARCH) < 0)
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
 
        r = parse_as_address(message, ifindex, hostname, family, flags);
        if (r != 0)
                return r;
 
        r = dns_name_is_valid(hostname);
        if (r < 0)
                return r;
        if (r == 0)
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
 
        r = dns_question_new_address(&question_utf8, family, hostname, false);
        if (r < 0)
                return r;
 
        r = dns_question_new_address(&question_idna, family, hostname, true);
        if (r < 0 && r != -EALREADY)
                return r;
 
        bus_client_log(message, "hostname resolution");
 
        r = dns_query_new_for_bus(m, &q, question_utf8, question_idna ?: question_utf8, NULL, ifindex, flags, error);
        if (r < 0)
                return r;
 
        q->bus_request = sd_bus_message_ref(message);
        q->request_family = family;
        q->complete = bus_method_resolve_hostname_complete;
 
        r = dns_query_bus_track(q, message);
        if (r < 0)
                return r;
 
        r = dns_query_go(q);
        if (r < 0)
                return r;
 
        TAKE_PTR(q);
        return 1;
}
 
static void bus_method_resolve_address_complete(DnsQuery *query) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(dns_query_freep) DnsQuery *q = query;
        DnsQuestion *question;
        DnsResourceRecord *rr;
        unsigned added = 0;
        int ifindex, r;
 
        assert(q);
 
        if (q->state != DNS_TRANSACTION_SUCCESS) {
                r = reply_query_state(q);
                goto finish;
        }
 
        r = dns_query_process_cname_many(q);
        if (r == -ELOOP) {
                r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
                goto finish;
        }
        if (r < 0)
                goto finish;
        if (r == DNS_QUERY_CNAME) {
                /* This was a cname, and the query was restarted. */
                TAKE_PTR(q);
                return;
        }
 
        r = sd_bus_message_new_method_return(q->bus_request, &reply);
        if (r < 0)
                goto finish;
 
        r = sd_bus_message_open_container(reply, 'a', "(is)");
        if (r < 0)
                goto finish;
 
        question = dns_query_question_for_protocol(q, q->answer_protocol);
 
        DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
                _cleanup_free_ char *normalized = NULL;
 
                r = dns_question_matches_rr(question, rr, NULL);
                if (r < 0)
                        goto finish;
                if (r == 0)
                        continue;
 
                r = dns_name_normalize(rr->ptr.name, 0, &normalized);
                if (r < 0)
                        goto finish;
 
                r = sd_bus_message_append(reply, "(is)", ifindex, normalized);
                if (r < 0)
                        goto finish;
 
                added++;
        }
 
        if (added <= 0) {
                r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR,
                                        "Address %s does not have any RR of requested type",
                                        IN_ADDR_TO_STRING(q->request_family, &q->request_address));
                goto finish;
        }
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                goto finish;
 
        r = sd_bus_message_append(reply, "t", dns_query_reply_flags_make(q));
        if (r < 0)
                goto finish;
 
        q->bus_request = sd_bus_message_unref(q->bus_request);
        r = sd_bus_send(q->manager->bus, reply, NULL);
 
finish:
        if (r < 0) {
                log_error_errno(r, "Failed to send address reply: %m");
                (void) reply_method_errnof(q, r, NULL);
        }
}
 
static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
        _cleanup_(dns_query_freep) DnsQuery *q = NULL;
        Manager *m = ASSERT_PTR(userdata);
        union in_addr_union a;
        int family, ifindex;
        uint64_t flags;
        int r;
 
        assert(message);
 
        assert_cc(sizeof(int) == sizeof(int32_t));
 
        r = sd_bus_message_read(message, "i", &ifindex);
        if (r < 0)
                return r;
 
        r = bus_message_read_in_addr_auto(message, error, &family, &a);
        if (r < 0)
                return r;
 
        r = sd_bus_message_read(message, "t", &flags);
        if (r < 0)
                return r;
 
        if (ifindex < 0)
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
 
        if (validate_and_mangle_query_flags(m, &flags, /* name= */ NULL, /* ok= */ 0) < 0)
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
 
        r = dns_question_new_reverse(&question, family, &a);
        if (r < 0)
                return r;
 
        bus_client_log(message, "address resolution");
 
        r = dns_query_new_for_bus(m, &q, question, question, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH, error);
        if (r < 0)
                return r;
 
        q->bus_request = sd_bus_message_ref(message);
        q->request_family = family;
        q->request_address = a;
        q->complete = bus_method_resolve_address_complete;
 
        r = dns_query_bus_track(q, message);
        if (r < 0)
                return r;
 
        r = dns_query_go(q);
        if (r < 0)
                return r;
 
        TAKE_PTR(q);
        return 1;
}
 
static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
        int r;
 
        assert(m);
        assert(rr);
 
        r = sd_bus_message_open_container(m, 'r', "iqqay");
        if (r < 0)
                return r;
 
        r = sd_bus_message_append(m, "iqq",
                                  ifindex,
                                  rr->key->class,
                                  rr->key->type);
        if (r < 0)
                return r;
 
        r = dns_resource_record_to_wire_format(rr, false);
        if (r < 0)
                return r;
 
        r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
        if (r < 0)
                return r;
 
        return sd_bus_message_close_container(m);
}
 
static void bus_method_resolve_record_complete(DnsQuery *query) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(dns_query_freep) DnsQuery *q = query;
        DnsResourceRecord *rr;
        DnsQuestion *question;
        unsigned added = 0;
        int ifindex;
        int r;
 
        assert(q);
 
        if (q->state != DNS_TRANSACTION_SUCCESS) {
                r = reply_query_state(q);
                goto finish;
        }
 
        r = dns_query_process_cname_many(q);
        if (r == -ELOOP) {
                r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
                goto finish;
        }
        if (r < 0)
                goto finish;
        if (r == DNS_QUERY_CNAME) {
                /* This was a cname, and the query was restarted. */
                TAKE_PTR(q);
                return;
        }
 
        r = sd_bus_message_new_method_return(q->bus_request, &reply);
        if (r < 0)
                goto finish;
 
        r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
        if (r < 0)
                goto finish;
 
        question = dns_query_question_for_protocol(q, q->answer_protocol);
 
        DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
                r = dns_question_matches_rr(question, rr, NULL);
                if (r < 0)
                        goto finish;
                if (r == 0)
                        continue;
 
                r = bus_message_append_rr(reply, rr, ifindex);
                if (r < 0)
                        goto finish;
 
                added++;
        }
 
        if (added <= 0) {
                r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", dns_query_string(q));
                goto finish;
        }
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                goto finish;
 
        r = sd_bus_message_append(reply, "t", dns_query_reply_flags_make(q));
        if (r < 0)
                goto finish;
 
        q->bus_request = sd_bus_message_unref(q->bus_request);
        r = sd_bus_send(q->manager->bus, reply, NULL);
 
finish:
        if (r < 0) {
                log_error_errno(r, "Failed to send record reply: %m");
                (void) reply_method_errnof(q, r, NULL);
        }
}
 
static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
        _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
        _cleanup_(dns_query_freep) DnsQuery *q = NULL;
        Manager *m = ASSERT_PTR(userdata);
        uint16_t class, type;
        const char *name;
        int r, ifindex;
        uint64_t flags;
 
        assert(message);
 
        assert_cc(sizeof(int) == sizeof(int32_t));
 
        r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
        if (r < 0)
                return r;
 
        if (ifindex < 0)
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
 
        r = dns_name_is_valid(name);
        if (r < 0)
                return r;
        if (r == 0)
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
 
        if (!dns_type_is_valid_query(type))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified resource record type %" PRIu16 " may not be used in a query.", type);
        if (dns_type_is_zone_transfer(type))
                return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Zone transfers not permitted via this programming interface.");
        if (dns_type_is_obsolete(type))
                return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
 
        if (validate_and_mangle_query_flags(m, &flags, name, SD_RESOLVED_NO_SEARCH) < 0)
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
 
        question = dns_question_new(1);
        if (!question)
                return -ENOMEM;
 
        key = dns_resource_key_new(class, type, name);
        if (!key)
                return -ENOMEM;
 
        r = dns_question_add(question, key, 0);
        if (r < 0)
                return r;
 
        bus_client_log(message, "resource record resolution");
 
        /* Setting SD_RESOLVED_CLAMP_TTL: let's request that the TTL is fixed up for locally cached entries,
         * after all we return it in the wire format blob. */
        r = dns_query_new_for_bus(m, &q, question, question, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH|SD_RESOLVED_CLAMP_TTL, error);
        if (r < 0)
                return r;
 
        q->bus_request = sd_bus_message_ref(message);
        q->complete = bus_method_resolve_record_complete;
 
        r = dns_query_bus_track(q, message);
        if (r < 0)
                return r;
 
        r = dns_query_go(q);
        if (r < 0)
                return r;
 
        TAKE_PTR(q);
        return 1;
}
 
static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
        _cleanup_free_ char *normalized = NULL;
        int r;
 
        assert(q);
        assert(reply);
        assert(rr);
        assert(rr->key);
 
        if (rr->key->type != DNS_TYPE_SRV)
                return 0;
 
        if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
                /* First, let's see if we could find an appropriate A or AAAA
                 * record for the SRV record */
                LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
                        DnsResourceRecord *zz;
                        DnsQuestion *question;
 
                        if (aux->state != DNS_TRANSACTION_SUCCESS)
                                continue;
                        if (aux->auxiliary_result != 0)
                                continue;
 
                        question = dns_query_question_for_protocol(aux, aux->answer_protocol);
 
                        r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
                        if (r < 0)
                                return r;
                        if (r == 0)
                                continue;
 
                        DNS_ANSWER_FOREACH(zz, aux->answer) {
 
                                r = dns_question_matches_rr(question, zz, NULL);
                                if (r < 0)
                                        return r;
                                if (r == 0)
                                        continue;
 
                                canonical = dns_resource_record_ref(zz);
                                break;
                        }
 
                        if (canonical)
                                break;
                }
 
                /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
                if (!canonical)
                        return 0;
        }
 
        r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
        if (r < 0)
                return r;
 
        r = dns_name_normalize(rr->srv.name, 0, &normalized);
        if (r < 0)
                return r;
 
        r = sd_bus_message_append(
                        reply,
                        "qqqs",
                        rr->srv.priority, rr->srv.weight, rr->srv.port, normalized);
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(reply, 'a', "(iiay)");
        if (r < 0)
                return r;
 
        if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
                LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
                        DnsResourceRecord *zz;
                        DnsQuestion *question;
                        int ifindex;
 
                        if (aux->state != DNS_TRANSACTION_SUCCESS)
                                continue;
                        if (aux->auxiliary_result != 0)
                                continue;
 
                        question = dns_query_question_for_protocol(aux, aux->answer_protocol);
 
                        r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
                        if (r < 0)
                                return r;
                        if (r == 0)
                                continue;
 
                        DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
 
                                r = dns_question_matches_rr(question, zz, NULL);
                                if (r < 0)
                                        return r;
                                if (r == 0)
                                        continue;
 
                                r = append_address(reply, zz, ifindex);
                                if (r < 0)
                                        return r;
                        }
                }
        }
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        if (canonical) {
                normalized = mfree(normalized);
 
                r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
                if (r < 0)
                        return r;
        }
 
        /* Note that above we appended the hostname as encoded in the
         * SRV, and here the canonical hostname this maps to. */
        r = sd_bus_message_append(reply, "s", normalized);
        if (r < 0)
                return r;
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        return 1;
}
 
static int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
        int r;
 
        assert(reply);
        assert(rr);
        assert(rr->key);
 
        if (rr->key->type != DNS_TYPE_TXT)
                return 0;
 
        LIST_FOREACH(items, i, rr->txt.items) {
 
                if (i->length <= 0)
                        continue;
 
                r = sd_bus_message_append_array(reply, 'y', i->data, i->length);