chiark / gitweb /
treewide: use log_*_errno whenever %m is in the format string
[elogind.git] / src / resolve / resolved-bus.c
index ebb97d7fd34e487a2b327fe9fa7b576df5a9be08..ff741cdc6753d1f73f7b4e6571813afafb622b36 100644 (file)
@@ -22,8 +22,9 @@
 #include "bus-errors.h"
 #include "bus-util.h"
 
-#include "resolved.h"
 #include "resolved-dns-domain.h"
+#include "resolved-bus.h"
+#include "resolved-def.h"
 
 static int reply_query_state(DnsQuery *q) {
         _cleanup_free_ char *ip = NULL;
@@ -42,35 +43,36 @@ static int reply_query_state(DnsQuery *q) {
 
         switch (q->state) {
 
-        case DNS_QUERY_NO_SERVERS:
-                return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "Not appropriate name servers or networks found");
+        case DNS_TRANSACTION_NO_SERVERS:
+                return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
 
-        case DNS_QUERY_TIMEOUT:
+        case DNS_TRANSACTION_TIMEOUT:
                 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
 
-        case DNS_QUERY_ATTEMPTS_MAX:
+        case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
                 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
 
-        case DNS_QUERY_RESOURCES:
+        case DNS_TRANSACTION_INVALID_REPLY:
+                return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
+
+        case DNS_TRANSACTION_RESOURCES:
                 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_RESOURCES, "Not enough resources");
 
-        case DNS_QUERY_INVALID_REPLY:
-                return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
+        case DNS_TRANSACTION_ABORTED:
+                return sd_bus_reply_method_errorf(q->request, BUS_ERROR_ABORTED, "Query aborted");
 
-        case DNS_QUERY_FAILURE: {
+        case DNS_TRANSACTION_FAILURE: {
                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
 
-                assert(q->received);
-
-                if (DNS_PACKET_RCODE(q->received) == DNS_RCODE_NXDOMAIN)
+                if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
                         sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", name);
                 else {
                         const char *rc, *n;
                         char p[3]; /* the rcode is 4 bits long */
 
-                        rc = dns_rcode_to_string(DNS_PACKET_RCODE(q->received));
+                        rc = dns_rcode_to_string(q->answer_rcode);
                         if (!rc) {
-                                sprintf(p, "%i", DNS_PACKET_RCODE(q->received));
+                                sprintf(p, "%i", q->answer_rcode);
                                 rc = p;
                         }
 
@@ -81,41 +83,40 @@ static int reply_query_state(DnsQuery *q) {
                 return sd_bus_reply_method_error(q->request, &error);
         }
 
-        case DNS_QUERY_NULL:
-        case DNS_QUERY_PENDING:
-        case DNS_QUERY_SUCCESS:
+        case DNS_TRANSACTION_NULL:
+        case DNS_TRANSACTION_PENDING:
+        case DNS_TRANSACTION_SUCCESS:
         default:
                 assert_not_reached("Impossible state");
         }
 }
 
-static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
+static int append_address(sd_bus_message *reply, DnsResourceRecord *rr) {
         int r;
 
         assert(reply);
         assert(rr);
 
-        r = sd_bus_message_open_container(reply, 'r', "yayi");
+        r = sd_bus_message_open_container(reply, 'r', "iay");
         if (r < 0)
                 return r;
 
-        if (rr->key.type == DNS_TYPE_A) {
-                r = sd_bus_message_append(reply, "y", AF_INET);
+        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 {
-                r = sd_bus_message_append(reply, "y", AF_INET6);
+
+        } 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));
-        }
-        if (r < 0)
-                return r;
+        } else
+                return -EAFNOSUPPORT;
 
-        r = sd_bus_message_append(reply, "i", ifindex);
         if (r < 0)
                 return r;
 
@@ -127,77 +128,70 @@ static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifin
 }
 
 static void bus_method_resolve_hostname_complete(DnsQuery *q) {
-        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL;
+        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL, *canonical = NULL;
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
-        unsigned i, n, added = 0;
-        size_t answer_rindex;
+        _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
+        unsigned added = 0, i;
         int r;
 
         assert(q);
 
-        if (q->state != DNS_QUERY_SUCCESS) {
+        if (q->state != DNS_TRANSACTION_SUCCESS) {
                 r = reply_query_state(q);
                 goto finish;
         }
 
-        assert(q->received);
-
-        r = dns_packet_skip_question(q->received);
+        r = sd_bus_message_new_method_return(q->request, &reply);
         if (r < 0)
-                goto parse_fail;
-
-        answer_rindex = q->received->rindex;
+                goto finish;
 
-        r = sd_bus_message_new_method_return(q->request, &reply);
+        r = sd_bus_message_append(reply, "i", q->answer_ifindex);
         if (r < 0)
                 goto finish;
 
-        r = sd_bus_message_open_container(reply, 'a', "(yayi)");
+        r = sd_bus_message_open_container(reply, 'a', "(iay)");
         if (r < 0)
                 goto finish;
 
-        n = DNS_PACKET_ANCOUNT(q->received) +
-            DNS_PACKET_NSCOUNT(q->received) +
-            DNS_PACKET_ARCOUNT(q->received);
+        if (q->answer) {
+                answer = dns_answer_ref(q->answer);
 
-        for (i = 0; i < n; i++) {
-                _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
+                for (i = 0; i < answer->n_rrs; i++) {
+                        r = dns_question_matches_rr(q->question, answer->rrs[i]);
+                        if (r < 0)
+                                goto finish;
+                        if (r == 0) {
+                                /* Hmm, if this is not an address record,
+                                   maybe it's a cname? If so, remember this */
+                                r = dns_question_matches_cname(q->question, answer->rrs[i]);
+                                if (r < 0)
+                                        goto finish;
+                                if (r > 0)
+                                        cname = dns_resource_record_ref(answer->rrs[i]);
 
-                r = dns_packet_read_rr(q->received, &rr, NULL);
-                if (r < 0)
-                        goto parse_fail;
+                                continue;
+                        }
 
-                r = dns_query_matches_rr(q, rr);
-                if (r < 0)
-                        goto parse_fail;
-                if (r == 0) {
-                        /* Hmm, if this is not an address record,
-                           maybe it's a cname? If so, remember this */
-                        r = dns_query_matches_cname(q, rr);
+                        r = append_address(reply, answer->rrs[i]);
                         if (r < 0)
-                                goto parse_fail;
-                        if (r > 0)
-                                cname = dns_resource_record_ref(rr);
-
-                        continue;
-                }
+                                goto finish;
 
-                r = append_address(reply, rr, q->received->ifindex);
-                if (r < 0)
-                        goto finish;
+                        if (!canonical)
+                                canonical = dns_resource_record_ref(answer->rrs[i]);
 
-                added ++;
+                        added ++;
+                }
         }
 
         if (added <= 0) {
                 if (!cname) {
-                        r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have RR of this type", q->request_hostname);
+                        r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of requested type", q->request_hostname);
                         goto finish;
                 }
 
                 /* This has a cname? Then update the query with the
                  * new cname. */
-                r = dns_query_follow_cname(q, cname->cname.name);
+                r = dns_query_cname_redirect(q, cname->cname.name);
                 if (r < 0) {
                         if (r == -ELOOP)
                                 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop on '%s'", q->request_hostname);
@@ -209,35 +203,36 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
 
                 /* Before we restart the query, let's see if any of
                  * the RRs we already got already answers our query */
-                dns_packet_rewind(q->received, answer_rindex);
-                for (i = 0; i < n; i++) {
-                        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
-
-                        r = dns_packet_read_rr(q->received, &rr, NULL);
-                        if (r < 0)
-                                goto parse_fail;
-
-                        r = dns_query_matches_rr(q, rr);
+                for (i = 0; i < answer->n_rrs; i++) {
+                        r = dns_question_matches_rr(q->question, answer->rrs[i]);
                         if (r < 0)
-                                goto parse_fail;
+                                goto finish;
                         if (r == 0)
                                 continue;
 
-                        r = append_address(reply, rr, q->received->ifindex);
+                        r = append_address(reply, answer->rrs[i]);
                         if (r < 0)
                                 goto finish;
 
+                        if (!canonical)
+                                canonical = dns_resource_record_ref(answer->rrs[i]);
+
                         added++;
                 }
 
                 /* If we didn't find anything, then let's restart the
                  * query, this time with the cname */
                 if (added <= 0) {
-                        r = dns_query_start(q);
+                        r = dns_query_go(q);
+                        if (r == -ESRCH) {
+                                r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
+                                goto finish;
+                        }
                         if (r < 0) {
                                 r = sd_bus_reply_method_errno(q->request, -r, NULL);
                                 goto finish;
                         }
+
                         return;
                 }
         }
@@ -246,57 +241,95 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
         if (r < 0)
                 goto finish;
 
-        r = sd_bus_send(q->manager->bus, reply, NULL);
-        goto finish;
+        /* Return the precise spelling and uppercasing reported by the server */
+        assert(canonical);
+        r = sd_bus_message_append(reply, "st", DNS_RESOURCE_KEY_NAME(canonical->key), SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
+        if (r < 0)
+                goto finish;
 
-parse_fail:
-        r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
+        r = sd_bus_send(q->manager->bus, reply, NULL);
 
 finish:
-        if (r < 0)
-                log_error("Failed to send bus reply: %s", strerror(-r));
+        if (r < 0) {
+                log_error_errno(r, "Failed to send hostname reply: %m");
+                sd_bus_reply_method_errno(q->request, -r, NULL);
+        }
 
         dns_query_free(q);
 }
 
+static int check_ifindex_flags(int ifindex, uint64_t *flags, sd_bus_error *error) {
+        assert(flags);
+
+        if (ifindex < 0)
+                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
+
+        if (*flags & ~SD_RESOLVED_FLAGS_ALL)
+                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
+
+        if (*flags == 0)
+                *flags = SD_RESOLVED_FLAGS_DEFAULT;
+
+        return 0;
+}
+
 static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+        _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
         Manager *m = userdata;
         const char *hostname;
-        uint8_t family;
-        DnsResourceKey keys[2];
+        int family, ifindex;
+        uint64_t flags;
         DnsQuery *q;
-        unsigned n = 0;
         int r;
 
         assert(bus);
         assert(message);
         assert(m);
 
-        r = sd_bus_message_read(message, "sy", &hostname, &family);
+        r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
         if (r < 0)
                 return r;
 
         if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
-                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %u", family);
+                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
 
-        if (!hostname_is_valid(hostname))
+        r = dns_name_normalize(hostname, NULL);
+        if (r < 0)
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
 
+        r = check_ifindex_flags(ifindex, &flags, error);
+        if (r < 0)
+                return r;
+
+        question = dns_question_new(family == AF_UNSPEC ? 2 : 1);
+        if (!question)
+                return -ENOMEM;
+
         if (family != AF_INET6) {
-                keys[n].class = DNS_CLASS_IN;
-                keys[n].type = DNS_TYPE_A;
-                keys[n].name = (char*) hostname;
-                n++;
+                _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
+
+                key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, hostname);
+                if (!key)
+                        return -ENOMEM;
+
+                r = dns_question_add(question, key);
+                if (r < 0)
+                        return r;
         }
 
         if (family != AF_INET) {
-                keys[n].class = DNS_CLASS_IN;
-                keys[n].type = DNS_TYPE_AAAA;
-                keys[n].name = (char*) hostname;
-                n++;
+                _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
+
+                key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, hostname);
+                if (!key)
+                        return -ENOMEM;
+
+                r = dns_question_add(question, key);
+                if (r < 0)
+                        return r;
         }
 
-        r = dns_query_new(m, &q, keys, n);
+        r = dns_query_new(m, &q, question, ifindex, flags);
         if (r < 0)
                 return r;
 
@@ -305,9 +338,17 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi
         q->request_hostname = hostname;
         q->complete = bus_method_resolve_hostname_complete;
 
-        r = dns_query_start(q);
+        r = dns_query_bus_track(q, bus, message);
+        if (r < 0)
+                return r;
+
+        r = dns_query_go(q);
         if (r < 0) {
                 dns_query_free(q);
+
+                if (r == -ESRCH)
+                        sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
+
                 return r;
         }
 
@@ -316,23 +357,22 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi
 
 static void bus_method_resolve_address_complete(DnsQuery *q) {
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
-        unsigned i, n, added = 0;
+        _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
+        unsigned added = 0, i;
         int r;
 
         assert(q);
 
-        if (q->state != DNS_QUERY_SUCCESS) {
+        if (q->state != DNS_TRANSACTION_SUCCESS) {
                 r = reply_query_state(q);
                 goto finish;
         }
 
-        assert(q->received);
-
-        r = dns_packet_skip_question(q->received);
+        r = sd_bus_message_new_method_return(q->request, &reply);
         if (r < 0)
-                goto parse_fail;
+                goto finish;
 
-        r = sd_bus_message_new_method_return(q->request, &reply);
+        r = sd_bus_message_append(reply, "i", q->answer_ifindex);
         if (r < 0)
                 goto finish;
 
@@ -340,28 +380,22 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
         if (r < 0)
                 goto finish;
 
-        n = DNS_PACKET_ANCOUNT(q->received) +
-            DNS_PACKET_NSCOUNT(q->received) +
-            DNS_PACKET_ARCOUNT(q->received);
-
-        for (i = 0; i < n; i++) {
-                _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
-
-                r = dns_packet_read_rr(q->received, &rr, NULL);
-                if (r < 0)
-                        goto parse_fail;
+        if (q->answer) {
+                answer = dns_answer_ref(q->answer);
 
-                r = dns_query_matches_rr(q, rr);
-                if (r < 0)
-                        goto parse_fail;
-                if (r == 0)
-                        continue;
+                for (i = 0; i < answer->n_rrs; i++) {
+                        r = dns_question_matches_rr(q->question, answer->rrs[i]);
+                        if (r < 0)
+                                goto finish;
+                        if (r == 0)
+                                continue;
 
-                r = sd_bus_message_append(reply, "s", rr->ptr.name);
-                if (r < 0)
-                        goto finish;
+                        r = sd_bus_message_append(reply, "s", answer->rrs[i]->ptr.name);
+                        if (r < 0)
+                                goto finish;
 
-                added ++;
+                        added ++;
+                }
         }
 
         if (added <= 0) {
@@ -369,7 +403,7 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
 
                 in_addr_to_string(q->request_family, &q->request_address, &ip);
 
-                r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address '%s' does not have RR of this type", ip);
+                r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address '%s' does not have any RR of requested type", ip);
                 goto finish;
         }
 
@@ -377,25 +411,29 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
         if (r < 0)
                 goto finish;
 
-        r = sd_bus_send(q->manager->bus, reply, NULL);
-        goto finish;
+        r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
+        if (r < 0)
+                goto finish;
 
-parse_fail:
-        r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
+        r = sd_bus_send(q->manager->bus, reply, NULL);
 
 finish:
-        if (r < 0)
-                log_error("Failed to send bus reply: %s", strerror(-r));
+        if (r < 0) {
+                log_error_errno(r, "Failed to send address reply: %m");
+                sd_bus_reply_method_errno(q->request, -r, NULL);
+        }
 
         dns_query_free(q);
 }
 
 static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
-        _cleanup_(dns_resource_key_free) DnsResourceKey key = {};
+        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
+        _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
+        _cleanup_free_ char *reverse = NULL;
         Manager *m = userdata;
-        uint8_t family;
+        int family, ifindex;
+        uint64_t flags;
         const void *d;
-        int ifindex;
         DnsQuery *q;
         size_t sz;
         int r;
@@ -404,34 +442,47 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void
         assert(message);
         assert(m);
 
-        r = sd_bus_message_read(message, "y", &family);
+        r = sd_bus_message_read(message, "ii", &ifindex, &family);
         if (r < 0)
                 return r;
 
         if (!IN_SET(family, AF_INET, AF_INET6))
-                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %u", family);
+                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
 
         r = sd_bus_message_read_array(message, 'y', &d, &sz);
         if (r < 0)
                 return r;
 
-        if ((family == AF_INET && sz != sizeof(struct in_addr)) ||
-            (family == AF_INET6 && sz != sizeof(struct in6_addr)))
+        if (sz != FAMILY_ADDRESS_SIZE(family))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
 
-        r = sd_bus_message_read(message, "i", &ifindex);
+        r = sd_bus_message_read(message, "t", &flags);
         if (r < 0)
                 return r;
-        if (ifindex < 0)
-                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
 
-        key.class = DNS_CLASS_IN;
-        key.type = DNS_TYPE_PTR;
-        r = dns_name_reverse(family, d, &key.name);
+        r = check_ifindex_flags(ifindex, &flags, error);
         if (r < 0)
                 return r;
 
-        r = dns_query_new(m, &q, &key, 1);
+        r = dns_name_reverse(family, d, &reverse);
+        if (r < 0)
+                return r;
+
+        question = dns_question_new(1);
+        if (!question)
+                return -ENOMEM;
+
+        key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, reverse);
+        if (!key)
+                return -ENOMEM;
+
+        reverse = NULL;
+
+        r = dns_question_add(question, key);
+        if (r < 0)
+                return r;
+
+        r = dns_query_new(m, &q, question, ifindex, flags);
         if (r < 0)
                 return r;
 
@@ -440,9 +491,170 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void
         memcpy(&q->request_address, d, sz);
         q->complete = bus_method_resolve_address_complete;
 
-        r = dns_query_start(q);
+        r = dns_query_bus_track(q, bus, message);
+        if (r < 0)
+                return r;
+
+        r = dns_query_go(q);
         if (r < 0) {
                 dns_query_free(q);
+
+                if (r == -ESRCH)
+                        sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
+
+                return r;
+        }
+
+        return 1;
+}
+
+static void bus_method_resolve_record_complete(DnsQuery *q) {
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
+        unsigned added = 0, i;
+        int r;
+
+        assert(q);
+
+        if (q->state != DNS_TRANSACTION_SUCCESS) {
+                r = reply_query_state(q);
+                goto finish;
+        }
+
+        r = sd_bus_message_new_method_return(q->request, &reply);
+        if (r < 0)
+                goto finish;
+
+        r = sd_bus_message_append(reply, "i", q->answer_ifindex);
+        if (r < 0)
+                goto finish;
+
+        r = sd_bus_message_open_container(reply, 'a', "(qqay)");
+        if (r < 0)
+                goto finish;
+
+        if (q->answer) {
+                answer = dns_answer_ref(q->answer);
+
+                for (i = 0; i < answer->n_rrs; i++) {
+                        _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
+                        size_t start;
+
+                        r = dns_question_matches_rr(q->question, answer->rrs[i]);
+                        if (r < 0)
+                                goto finish;
+                        if (r == 0)
+                                continue;
+
+                        r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0);
+                        if (r < 0)
+                                goto finish;
+
+                        r = dns_packet_append_rr(p, answer->rrs[i], &start);
+                        if (r < 0)
+                                goto finish;
+
+                        r = sd_bus_message_open_container(reply, 'r', "qqay");
+                        if (r < 0)
+                                goto finish;
+
+                        r = sd_bus_message_append(reply, "qq", answer->rrs[i]->key->class, answer->rrs[i]->key->type);
+                        if (r < 0)
+                                goto finish;
+
+                        r = sd_bus_message_append_array(reply, 'y', DNS_PACKET_DATA(p) + start, p->size - start);
+                        if (r < 0)
+                                goto finish;
+
+                        r = sd_bus_message_close_container(reply);
+                        if (r < 0)
+                                goto finish;
+
+                        added ++;
+                }
+        }
+
+        if (added <= 0) {
+                r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", q->request_hostname);
+                goto finish;
+        }
+
+        r = sd_bus_message_close_container(reply);
+        if (r < 0)
+                goto finish;
+
+        r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
+        if (r < 0)
+                goto finish;
+
+        r = sd_bus_send(q->manager->bus, reply, NULL);
+
+finish:
+        if (r < 0) {
+                log_error_errno(r, "Failed to send record reply: %m");
+                sd_bus_reply_method_errno(q->request, -r, NULL);
+        }
+
+        dns_query_free(q);
+}
+
+static int bus_method_resolve_record(sd_bus *bus, 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;
+        Manager *m = userdata;
+        uint16_t class, type;
+        const char *name;
+        int r, ifindex;
+        uint64_t flags;
+        DnsQuery *q;
+
+        assert(bus);
+        assert(message);
+        assert(m);
+
+        r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
+        if (r < 0)
+                return r;
+
+        r = dns_name_normalize(name, NULL);
+        if (r < 0)
+                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
+
+        r = check_ifindex_flags(ifindex, &flags, error);
+        if (r < 0)
+                return r;
+
+        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);
+        if (r < 0)
+                return r;
+
+        r = dns_query_new(m, &q, question, ifindex, flags);
+        if (r < 0)
+                return r;
+
+        q->request = sd_bus_message_ref(message);
+        q->request_hostname = name;
+        q->complete = bus_method_resolve_record_complete;
+
+        r = dns_query_bus_track(q, bus, message);
+        if (r < 0)
+                return r;
+
+        r = dns_query_go(q);
+        if (r < 0) {
+                dns_query_free(q);
+
+                if (r == -ESRCH)
+                        sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
+
                 return r;
         }
 
@@ -451,8 +663,9 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void
 
 static const sd_bus_vtable resolve_vtable[] = {
         SD_BUS_VTABLE_START(0),
-        SD_BUS_METHOD("ResolveHostname", "sy", "a(yayi)", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
-        SD_BUS_METHOD("ResolveAddress", "yayi", "as", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("ResolveHostname", "isit", "ia(iay)st", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("ResolveAddress", "iiayt", "iast", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("ResolveRecord", "isqqt", "ia(qqay)t", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_VTABLE_END,
 };
 
@@ -468,6 +681,28 @@ static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
         return 0;
 }
 
+static int match_prepare_for_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
+        Manager *m = userdata;
+        int b, r;
+
+        assert(bus);
+        assert(bus);
+
+        r = sd_bus_message_read(message, "b", &b);
+        if (r < 0) {
+                log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
+                return 0;
+        }
+
+        if (b)
+                return 0;
+
+        log_debug("Coming back from suspend, verifying all RRs...");
+
+        manager_verify_all(m);
+        return 0;
+}
+
 int manager_connect_bus(Manager *m) {
         int r;
 
@@ -482,34 +717,37 @@ int manager_connect_bus(Manager *m) {
                  * boot. Let's try in 5s again. As soon as we have
                  * kdbus we can stop doing this... */
 
-                log_debug("Failed to connect to bus, trying again in 5s: %s", strerror(-r));
+                log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m");
 
                 r = sd_event_add_time(m->event, &m->bus_retry_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 5*USEC_PER_SEC, 0, on_bus_retry, m);
-                if (r < 0) {
-                        log_error("Failed to install bus reconnect time event: %s", strerror(-r));
-                        return r;
-                }
+                if (r < 0)
+                        return log_error_errno(r, "Failed to install bus reconnect time event: %m");
 
                 return 0;
         }
 
         r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable, m);
-        if (r < 0) {
-                log_error("Failed to register object: %s", strerror(-r));
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to register object: %m");
 
         r = sd_bus_request_name(m->bus, "org.freedesktop.resolve1", 0);
-        if (r < 0) {
-                log_error("Failed to register name: %s", strerror(-r));
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to register name: %m");
 
         r = sd_bus_attach_event(m->bus, m->event, 0);
-        if (r < 0) {
-                log_error("Failed to attach bus to event loop: %s", strerror(-r));
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to attach bus to event loop: %m");
+
+        r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot,
+                             "type='signal',"
+                             "sender='org.freedesktop.login1',"
+                             "interface='org.freedesktop.login1.Manager',"
+                             "member='PrepareForSleep',"
+                             "path='/org/freedesktop/login1'",
+                             match_prepare_for_sleep,
+                             m);
+        if (r < 0)
+                log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
 
         return 0;
 }