chiark / gitweb /
resolved: properly pass canonical name information to resolving client
authorLennart Poettering <lennart@poettering.net>
Wed, 16 Jul 2014 20:50:41 +0000 (22:50 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 16 Jul 2014 20:51:50 +0000 (22:51 +0200)
Also, hook up nss-resolve to make use of this information

src/nss-resolve/nss-resolve.c
src/resolve/resolved-bus.c

index 7b0c24f..0dbc224 100644 (file)
@@ -43,12 +43,15 @@ NSS_GETHOSTBYADDR_PROTOTYPES(resolve);
 
 #define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC)
 
-static int count_addresses(sd_bus_message *m, unsigned af, unsigned *ret) {
-        unsigned c = 0;
-        int r;
+static int count_addresses(sd_bus_message *m, unsigned af, const char **canonical) {
+        int c = 0, r;
 
         assert(m);
-        assert(ret);
+        assert(canonical);
+
+        r = sd_bus_message_enter_container(m, 'a', "(yayi)");
+        if (r < 0)
+                return r;
 
         while ((r = sd_bus_message_enter_container(m, 'r', "yayi")) > 0) {
                 unsigned char family;
@@ -73,12 +76,19 @@ static int count_addresses(sd_bus_message *m, unsigned af, unsigned *ret) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_message_rewind(m, false);
+        r = sd_bus_message_exit_container(m);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_read(m, "s", canonical);
         if (r < 0)
                 return r;
 
-        *ret = c;
-        return 0;
+        r = sd_bus_message_rewind(m, true);
+        if (r < 0)
+                return r;
+
+        return c;
 }
 
 enum nss_status _nss_resolve_gethostbyname4_r(
@@ -92,10 +102,10 @@ enum nss_status _nss_resolve_gethostbyname4_r(
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL;
         _cleanup_bus_unref_ sd_bus *bus = NULL;
+        const char *canonical = NULL;
         size_t l, ms, idx;
         char *r_name;
-        unsigned i = 0, c = 0;
-        int r;
+        int c, r, i = 0;
 
         assert(name);
         assert(pat);
@@ -138,21 +148,21 @@ enum nss_status _nss_resolve_gethostbyname4_r(
                 return NSS_STATUS_UNAVAIL;
         }
 
-        r = sd_bus_message_enter_container(reply, 'a', "(yayi)");
-        if (r < 0)
-                goto fail;
-
-        r = count_addresses(reply, AF_UNSPEC, &c);
-        if (r < 0)
+        c = count_addresses(reply, AF_UNSPEC, &canonical);
+        if (c < 0) {
+                r = c;
                 goto fail;
-
-        if (c <= 0) {
+        }
+        if (c == 0) {
                 *errnop = ESRCH;
                 *h_errnop = HOST_NOT_FOUND;
                 return NSS_STATUS_NOTFOUND;
         }
 
-        l = strlen(name);
+        if (isempty(canonical))
+                canonical = name;
+
+        l = strlen(canonical);
         ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c;
         if (buflen < ms) {
                 *errnop = ENOMEM;
@@ -162,11 +172,16 @@ enum nss_status _nss_resolve_gethostbyname4_r(
 
         /* First, append name */
         r_name = buffer;
-        memcpy(r_name, name, l+1);
+        memcpy(r_name, canonical, l+1);
         idx = ALIGN(l+1);
 
         /* Second, append addresses */
         r_tuple_first = (struct gaih_addrtuple*) (buffer + idx);
+
+        r = sd_bus_message_enter_container(reply, 'a', "(yayi)");
+        if (r < 0)
+                goto fail;
+
         while ((r = sd_bus_message_enter_container(reply, 'r', "yayi")) > 0) {
                 unsigned char family;
                 const void *a;
@@ -212,13 +227,10 @@ enum nss_status _nss_resolve_gethostbyname4_r(
                 idx += ALIGN(sizeof(struct gaih_addrtuple));
                 i++;
         }
-
-        assert(i == c);
-
-        r = sd_bus_message_exit_container(reply);
         if (r < 0)
                 goto fail;
 
+        assert(i == c);
         assert(idx == ms);
 
         if (*pat)
@@ -250,9 +262,9 @@ enum nss_status _nss_resolve_gethostbyname3_r(
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         char *r_name, *r_aliases, *r_addr, *r_addr_list;
         _cleanup_bus_unref_ sd_bus *bus = NULL;
-        unsigned c = 0, i = 0;
         size_t l, idx, ms, alen;
-        int r;
+        const char *canonical;
+        int c, r, i = 0;
 
         assert(name);
         assert(result);
@@ -303,22 +315,22 @@ enum nss_status _nss_resolve_gethostbyname3_r(
                 return NSS_STATUS_UNAVAIL;
         }
 
-        r = sd_bus_message_enter_container(reply, 'a', "(yayi)");
-        if (r < 0)
+        c = count_addresses(reply, af, &canonical);
+        if (c < 0) {
+                r = c;
                 goto fail;
-
-        r = count_addresses(reply, af, &c);
-        if (r < 0)
-                goto fail;
-
-        if (c <= 0) {
+        }
+        if (c == 0) {
                 *errnop = ESRCH;
                 *h_errnop = HOST_NOT_FOUND;
                 return NSS_STATUS_NOTFOUND;
         }
 
+        if (isempty(canonical))
+                canonical = name;
+
         alen = PROTO_ADDRESS_SIZE(af);
-        l = strlen(name);
+        l = strlen(canonical);
 
         ms = ALIGN(l+1) +
                 sizeof(char*) +
@@ -333,16 +345,21 @@ enum nss_status _nss_resolve_gethostbyname3_r(
 
         /* First, append name */
         r_name = buffer;
-        memcpy(r_name, name, l+1);
+        memcpy(r_name, canonical, l+1);
         idx = ALIGN(l+1);
 
-        /* Second, create aliases array */
+        /* Second, create empty aliases array */
         r_aliases = buffer + idx;
         ((char**) r_aliases)[0] = NULL;
         idx += sizeof(char*);
 
         /* Third, append addresses */
         r_addr = buffer + idx;
+
+        r = sd_bus_message_enter_container(reply, 'a', "(yayi)");
+        if (r < 0)
+                goto fail;
+
         while ((r = sd_bus_message_enter_container(reply, 'r', "yayi")) > 0) {
                 unsigned char family;
                 const void *a;
@@ -381,15 +398,13 @@ enum nss_status _nss_resolve_gethostbyname3_r(
                 memcpy(r_addr + i*ALIGN(alen), a, alen);
                 i++;
         }
+        if (r < 0)
+                goto fail;
 
         assert(i == c);
         idx += c * ALIGN(alen);
 
-        r = sd_bus_message_exit_container(reply);
-        if (r < 0)
-                goto fail;
-
-        /* Third, append address pointer array */
+        /* Fourth, append address pointer array */
         r_addr_list = buffer + idx;
         for (i = 0; i < c; i++)
                 ((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen);
@@ -560,6 +575,8 @@ enum nss_status _nss_resolve_gethostbyaddr2_r(
 
                 idx += ALIGN(l+1);
         }
+        if (r < 0)
+                goto fail;
 
         ((char**) r_aliases)[c-1] = NULL;
         assert(idx == ms);
index ebb97d7..acdfd52 100644 (file)
@@ -43,7 +43,7 @@ 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");
+                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:
                 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
@@ -127,7 +127,7 @@ 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;
@@ -186,12 +186,15 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
                 if (r < 0)
                         goto finish;
 
+                if (!canonical)
+                        canonical = dns_resource_record_ref(rr);
+
                 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;
                 }
 
@@ -227,6 +230,9 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
                         if (r < 0)
                                 goto finish;
 
+                        if (!canonical)
+                                canonical = dns_resource_record_ref(rr);
+
                         added++;
                 }
 
@@ -234,6 +240,10 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
                  * query, this time with the cname */
                 if (added <= 0) {
                         r = dns_query_start(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;
@@ -246,6 +256,12 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
         if (r < 0)
                 goto finish;
 
+        /* Return the precise spelling and uppercasing reported by the server */
+        assert(canonical);
+        r = sd_bus_message_append(reply, "s", canonical->key.name);
+        if (r < 0)
+                goto finish;
+
         r = sd_bus_send(q->manager->bus, reply, NULL);
         goto finish;
 
@@ -308,6 +324,10 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi
         r = dns_query_start(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;
         }
 
@@ -369,7 +389,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;
         }
 
@@ -451,7 +471,7 @@ 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("ResolveHostname", "sy", "a(yayi)s", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("ResolveAddress", "yayi", "as", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_VTABLE_END,
 };