chiark / gitweb /
treewide: use log_*_errno whenever %m is in the format string
[elogind.git] / src / resolve / resolved-bus.c
index 0b8ecf1b82f2c827e1c42b903f175aec5f32a5e7..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,22 +43,25 @@ static int reply_query_state(DnsQuery *q) {
 
         switch (q->state) {
 
-        case DNS_QUERY_NO_SERVERS:
+        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;
 
                 if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
@@ -79,21 +83,21 @@ 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', "iayi");
+        r = sd_bus_message_open_container(reply, 'r', "iay");
         if (r < 0)
                 return r;
 
@@ -116,10 +120,6 @@ static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifin
         if (r < 0)
                 return r;
 
-        r = sd_bus_message_append(reply, "i", ifindex);
-        if (r < 0)
-                return r;
-
         r = sd_bus_message_close_container(reply);
         if (r < 0)
                 return r;
@@ -132,11 +132,11 @@ static void bus_method_resolve_hostname_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, ifindex;
+        int r;
 
         assert(q);
 
-        if (q->state != DNS_QUERY_SUCCESS) {
+        if (q->state != DNS_TRANSACTION_SUCCESS) {
                 r = reply_query_state(q);
                 goto finish;
         }
@@ -145,11 +145,13 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
         if (r < 0)
                 goto finish;
 
-        r = sd_bus_message_open_container(reply, 'a', "(iayi)");
+        r = sd_bus_message_append(reply, "i", q->answer_ifindex);
         if (r < 0)
                 goto finish;
 
-        ifindex = q->answer_ifindex;
+        r = sd_bus_message_open_container(reply, 'a', "(iay)");
+        if (r < 0)
+                goto finish;
 
         if (q->answer) {
                 answer = dns_answer_ref(q->answer);
@@ -170,7 +172,7 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
                                 continue;
                         }
 
-                        r = append_address(reply, answer->rrs[i], ifindex);
+                        r = append_address(reply, answer->rrs[i]);
                         if (r < 0)
                                 goto finish;
 
@@ -208,7 +210,7 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
                         if (r == 0)
                                 continue;
 
-                        r = append_address(reply, answer->rrs[i], ifindex);
+                        r = append_address(reply, answer->rrs[i]);
                         if (r < 0)
                                 goto finish;
 
@@ -241,7 +243,7 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
 
         /* Return the precise spelling and uppercasing reported by the server */
         assert(canonical);
-        r = sd_bus_message_append(reply, "s", DNS_RESOURCE_KEY_NAME(canonical->key));
+        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;
 
@@ -249,18 +251,34 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
 
 finish:
         if (r < 0) {
-                log_error("Failed to send hostname reply: %s", strerror(-r));
+                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;
-        int family;
+        int family, ifindex;
+        uint64_t flags;
         DnsQuery *q;
         int r;
 
@@ -268,16 +286,21 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi
         assert(message);
         assert(m);
 
-        r = sd_bus_message_read(message, "si", &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 %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;
@@ -306,7 +329,7 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi
                         return r;
         }
 
-        r = dns_query_new(m, &q, question);
+        r = dns_query_new(m, &q, question, ifindex, flags);
         if (r < 0)
                 return r;
 
@@ -315,6 +338,10 @@ 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_bus_track(q, bus, message);
+        if (r < 0)
+                return r;
+
         r = dns_query_go(q);
         if (r < 0) {
                 dns_query_free(q);
@@ -336,7 +363,7 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
 
         assert(q);
 
-        if (q->state != DNS_QUERY_SUCCESS) {
+        if (q->state != DNS_TRANSACTION_SUCCESS) {
                 r = reply_query_state(q);
                 goto finish;
         }
@@ -345,6 +372,10 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
         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', "s");
         if (r < 0)
                 goto finish;
@@ -380,11 +411,15 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
         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("Failed to send address reply: %s", strerror(-r));
+                log_error_errno(r, "Failed to send address reply: %m");
                 sd_bus_reply_method_errno(q->request, -r, NULL);
         }
 
@@ -397,6 +432,7 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void
         _cleanup_free_ char *reverse = NULL;
         Manager *m = userdata;
         int family, ifindex;
+        uint64_t flags;
         const void *d;
         DnsQuery *q;
         size_t sz;
@@ -406,7 +442,7 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void
         assert(message);
         assert(m);
 
-        r = sd_bus_message_read(message, "i", &family);
+        r = sd_bus_message_read(message, "ii", &ifindex, &family);
         if (r < 0)
                 return r;
 
@@ -420,11 +456,13 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void
         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;
+
+        r = check_ifindex_flags(ifindex, &flags, error);
         if (r < 0)
                 return r;
-        if (ifindex < 0)
-                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
 
         r = dns_name_reverse(family, d, &reverse);
         if (r < 0)
@@ -444,7 +482,7 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void
         if (r < 0)
                 return r;
 
-        r = dns_query_new(m, &q, question);
+        r = dns_query_new(m, &q, question, ifindex, flags);
         if (r < 0)
                 return r;
 
@@ -453,6 +491,10 @@ 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_bus_track(q, bus, message);
+        if (r < 0)
+                return r;
+
         r = dns_query_go(q);
         if (r < 0) {
                 dns_query_free(q);
@@ -474,7 +516,7 @@ static void bus_method_resolve_record_complete(DnsQuery *q) {
 
         assert(q);
 
-        if (q->state != DNS_QUERY_SUCCESS) {
+        if (q->state != DNS_TRANSACTION_SUCCESS) {
                 r = reply_query_state(q);
                 goto finish;
         }
@@ -483,6 +525,10 @@ static void bus_method_resolve_record_complete(DnsQuery *q) {
         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;
@@ -537,11 +583,15 @@ static void bus_method_resolve_record_complete(DnsQuery *q) {
         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("Failed to send record reply: %s", strerror(-r));
+                log_error_errno(r, "Failed to send record reply: %m");
                 sd_bus_reply_method_errno(q->request, -r, NULL);
         }
 
@@ -551,18 +601,26 @@ finish:
 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;
-        _cleanup_free_ char *reverse = NULL;
         Manager *m = userdata;
-        DnsQuery *q;
-        int r;
         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, "sqq", &name, &class, &type);
+        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;
 
@@ -578,7 +636,7 @@ static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void
         if (r < 0)
                 return r;
 
-        r = dns_query_new(m, &q, question);
+        r = dns_query_new(m, &q, question, ifindex, flags);
         if (r < 0)
                 return r;
 
@@ -586,6 +644,10 @@ static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void
         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);
@@ -601,9 +663,9 @@ static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void
 
 static const sd_bus_vtable resolve_vtable[] = {
         SD_BUS_VTABLE_START(0),
-        SD_BUS_METHOD("ResolveHostname", "si", "a(iayi)s", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
-        SD_BUS_METHOD("ResolveAddress", "iayi", "as", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
-        SD_BUS_METHOD("ResolveRecord", "sqq", "a(qqay)", bus_method_resolve_record, 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,
 };
 
@@ -619,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;
 
@@ -633,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;
 }