chiark / gitweb /
resolved: properly process DNAME RRs
[elogind.git] / src / resolve / resolved-dns-packet.c
index 1ff5687..5d4e097 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "utf8.h"
 #include "util.h"
+#include "strv.h"
 #include "resolved-dns-domain.h"
 #include "resolved-dns-packet.h"
 
@@ -72,10 +73,26 @@ int dns_packet_new_query(DnsPacket **ret, DnsProtocol protocol, size_t mtu) {
 
         h = DNS_PACKET_HEADER(p);
 
-        if (protocol == DNS_PROTOCOL_DNS)
-                h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0, 0, 0, 0, 1, 0, 0, 0, 0)); /* ask for recursion */
+        if (protocol == DNS_PROTOCOL_LLMNR)
+                h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
+                                                         0 /* opcode */,
+                                                         0 /* c */,
+                                                         0 /* tc */,
+                                                         0 /* t */,
+                                                         0 /* ra */,
+                                                         0 /* ad */,
+                                                         0 /* cd */,
+                                                         0 /* rcode */));
         else
-                h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0));
+                h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
+                                                         0 /* opcode */,
+                                                         0 /* aa */,
+                                                         0 /* tc */,
+                                                         1 /* rd (ask for recursion) */,
+                                                         0 /* ra */,
+                                                         0 /* ad */,
+                                                         0 /* cd */,
+                                                         0 /* rcode */));
 
         *ret = p;
         return 0;
@@ -148,6 +165,11 @@ int dns_packet_validate_reply(DnsPacket *p) {
         if (DNS_PACKET_OPCODE(p) != 0)
                 return -EBADMSG;
 
+        /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
+        if (p->protocol == DNS_PROTOCOL_LLMNR &&
+            DNS_PACKET_QDCOUNT(p) != 1)
+                return -EBADMSG;
+
         return 1;
 }
 
@@ -169,13 +191,16 @@ int dns_packet_validate_query(DnsPacket *p) {
         if (DNS_PACKET_TC(p))
                 return -EBADMSG;
 
+        /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
         if (p->protocol == DNS_PROTOCOL_LLMNR &&
             DNS_PACKET_QDCOUNT(p) != 1)
                 return -EBADMSG;
 
+        /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
         if (DNS_PACKET_ANCOUNT(p) > 0)
                 return -EBADMSG;
 
+        /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
         if (DNS_PACKET_NSCOUNT(p) > 0)
                 return -EBADMSG;
 
@@ -472,11 +497,12 @@ int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *star
         if (r < 0)
                 goto fail;
 
-        switch (rr->key->type) {
+        switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
 
         case DNS_TYPE_PTR:
         case DNS_TYPE_NS:
         case DNS_TYPE_CNAME:
+        case DNS_TYPE_DNAME:
                 r = dns_packet_append_name(p, rr->ptr.name, NULL);
                 break;
 
@@ -488,6 +514,19 @@ int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *star
                 r = dns_packet_append_string(p, rr->hinfo.os, NULL);
                 break;
 
+        case DNS_TYPE_SPF: /* exactly the same as TXT */
+        case DNS_TYPE_TXT: {
+                char **s;
+
+                STRV_FOREACH(s, rr->txt.strings) {
+                        r = dns_packet_append_string(p, *s, NULL);
+                        if (r < 0)
+                                goto fail;
+                }
+
+                break;
+        }
+
         case DNS_TYPE_A:
                 r = dns_packet_append_blob(p, &rr->a.in_addr, sizeof(struct in_addr), NULL);
                 break;
@@ -525,11 +564,46 @@ int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *star
                 break;
 
         case DNS_TYPE_MX:
-        case DNS_TYPE_TXT:
+                r = dns_packet_append_uint16(p, rr->mx.priority, NULL);
+                if (r < 0)
+                        goto fail;
+
+                r = dns_packet_append_name(p, rr->mx.exchange, NULL);
+                break;
+
+        case DNS_TYPE_LOC:
+                r = dns_packet_append_uint8(p, rr->loc.version, NULL);
+                if (r < 0)
+                        goto fail;
+
+                r = dns_packet_append_uint8(p, rr->loc.size, NULL);
+                if (r < 0)
+                        goto fail;
+
+                r = dns_packet_append_uint8(p, rr->loc.horiz_pre, NULL);
+                if (r < 0)
+                        goto fail;
+
+                r = dns_packet_append_uint8(p, rr->loc.vert_pre, NULL);
+                if (r < 0)
+                        goto fail;
+
+                r = dns_packet_append_uint16(p, rr->loc.latitude, NULL);
+                if (r < 0)
+                        goto fail;
+
+                r = dns_packet_append_uint16(p, rr->loc.longitude, NULL);
+                if (r < 0)
+                        goto fail;
+
+                r = dns_packet_append_uint16(p, rr->loc.altitude, NULL);
+                break;
+
         case DNS_TYPE_SRV:
-        case DNS_TYPE_DNAME:
         case DNS_TYPE_SSHFP:
+        case _DNS_TYPE_INVALID: /* unparseable */
         default:
+
                 r = dns_packet_append_blob(p, rr->generic.data, rr->generic.size, NULL);
                 break;
         }
@@ -550,6 +624,9 @@ int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *star
                 goto fail;
         p->size = end;
 
+        if (start)
+                *start = saved_size;
+
         return 0;
 
 fail:
@@ -844,6 +921,12 @@ int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start) {
         if (r < 0)
                 goto fail;
 
+        if (key->class == DNS_CLASS_ANY ||
+            key->type == DNS_TYPE_ANY) {
+                r = -EBADMSG;
+                goto fail;
+        }
+
         rr = dns_resource_record_new(key);
         if (!rr) {
                 r = -ENOMEM;
@@ -870,6 +953,7 @@ int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start) {
         case DNS_TYPE_PTR:
         case DNS_TYPE_NS:
         case DNS_TYPE_CNAME:
+        case DNS_TYPE_DNAME:
                 r = dns_packet_read_name(p, &rr->ptr.name, NULL);
                 break;
 
@@ -881,6 +965,22 @@ int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start) {
                 r = dns_packet_read_string(p, &rr->hinfo.os, NULL);
                 break;
 
+        case DNS_TYPE_SPF: /* exactly the same as TXT */
+        case DNS_TYPE_TXT: {
+                char *s;
+
+                while (p->rindex < offset + rdlength) {
+                        r = dns_packet_read_string(p, &s, NULL);
+                        if (r < 0)
+                                goto fail;
+
+                        r = strv_consume(&rr->txt.strings, s);
+                        if (r < 0)
+                                goto fail;
+                };
+                break;
+        }
+
         case DNS_TYPE_A:
                 r = dns_packet_read_blob(p, &rr->a.in_addr, sizeof(struct in_addr), NULL);
                 break;
@@ -918,9 +1018,57 @@ int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start) {
                 break;
 
         case DNS_TYPE_MX:
-        case DNS_TYPE_TXT:
+                r = dns_packet_read_uint16(p, &rr->mx.priority, NULL);
+                if (r < 0)
+                        goto fail;
+
+                r = dns_packet_read_name(p, &rr->mx.exchange, NULL);
+                break;
+
+        case DNS_TYPE_LOC: {
+                uint8_t t;
+                size_t pos;
+
+                r = dns_packet_read_uint8(p, &t, &pos);
+                if (r < 0)
+                        goto fail;
+
+                if (t == 0) {
+                        rr->loc.version = t;
+
+                        r = dns_packet_read_uint8(p, &rr->loc.size, NULL);
+                        if (r < 0)
+                                goto fail;
+
+                        r = dns_packet_read_uint8(p, &rr->loc.horiz_pre, NULL);
+                        if (r < 0)
+                                goto fail;
+
+                        r = dns_packet_read_uint8(p, &rr->loc.vert_pre, NULL);
+                        if (r < 0)
+                                goto fail;
+
+                        r = dns_packet_read_uint32(p, &rr->loc.latitude, NULL);
+                        if (r < 0)
+                                goto fail;
+
+                        r = dns_packet_read_uint32(p, &rr->loc.longitude, NULL);
+                        if (r < 0)
+                                goto fail;
+
+                        r = dns_packet_read_uint32(p, &rr->loc.altitude, NULL);
+                        if (r < 0)
+                                goto fail;
+
+                        break;
+                } else {
+                        dns_packet_rewind(p, pos);
+                        rr->unparseable = true;
+                        /* fall through */
+                }
+        }
+
         case DNS_TYPE_SRV:
-        case DNS_TYPE_DNAME:
         case DNS_TYPE_SSHFP:
         default:
                 r = dns_packet_read(p, rdlength, &d, NULL);