2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "dns-domain.h"
26 #include "hexdecoct.h"
27 #include "resolved-dns-dnssec.h"
28 #include "resolved-dns-packet.h"
29 #include "resolved-dns-rr.h"
30 #include "string-table.h"
31 #include "string-util.h"
33 #include "terminal-util.h"
35 DnsResourceKey
* dns_resource_key_new(uint16_t class, uint16_t type
, const char *name
) {
42 k
= malloc0(sizeof(DnsResourceKey
) + l
+ 1);
50 strcpy((char*) k
+ sizeof(DnsResourceKey
), name
);
55 DnsResourceKey
* dns_resource_key_new_redirect(const DnsResourceKey
*key
, const DnsResourceRecord
*cname
) {
61 assert(IN_SET(cname
->key
->type
, DNS_TYPE_CNAME
, DNS_TYPE_DNAME
));
63 if (cname
->key
->type
== DNS_TYPE_CNAME
)
64 return dns_resource_key_new(key
->class, key
->type
, cname
->cname
.name
);
67 char *destination
= NULL
;
69 r
= dns_name_change_suffix(dns_resource_key_name(key
), dns_resource_key_name(cname
->key
), cname
->dname
.name
, &destination
);
73 return dns_resource_key_ref((DnsResourceKey
*) key
);
75 k
= dns_resource_key_new_consume(key
->class, key
->type
, destination
);
85 int dns_resource_key_new_append_suffix(DnsResourceKey
**ret
, DnsResourceKey
*key
, char *name
) {
86 DnsResourceKey
*new_key
;
94 if (dns_name_is_root(name
)) {
95 *ret
= dns_resource_key_ref(key
);
99 r
= dns_name_concat(dns_resource_key_name(key
), name
, &joined
);
103 new_key
= dns_resource_key_new_consume(key
->class, key
->type
, joined
);
113 DnsResourceKey
* dns_resource_key_new_consume(uint16_t class, uint16_t type
, char *name
) {
118 k
= new0(DnsResourceKey
, 1);
130 DnsResourceKey
* dns_resource_key_ref(DnsResourceKey
*k
) {
135 /* Static/const keys created with DNS_RESOURCE_KEY_CONST will
136 * set this to -1, they should not be reffed/unreffed */
137 assert(k
->n_ref
!= (unsigned) -1);
139 assert(k
->n_ref
> 0);
145 DnsResourceKey
* dns_resource_key_unref(DnsResourceKey
*k
) {
149 assert(k
->n_ref
!= (unsigned) -1);
150 assert(k
->n_ref
> 0);
161 const char* dns_resource_key_name(const DnsResourceKey
*key
) {
170 name
= (char*) key
+ sizeof(DnsResourceKey
);
172 if (dns_name_is_root(name
))
178 bool dns_resource_key_is_address(const DnsResourceKey
*key
) {
181 /* Check if this is an A or AAAA resource key */
183 return key
->class == DNS_CLASS_IN
&& IN_SET(key
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
);
186 int dns_resource_key_equal(const DnsResourceKey
*a
, const DnsResourceKey
*b
) {
192 r
= dns_name_equal(dns_resource_key_name(a
), dns_resource_key_name(b
));
196 if (a
->class != b
->class)
199 if (a
->type
!= b
->type
)
205 int dns_resource_key_match_rr(const DnsResourceKey
*key
, DnsResourceRecord
*rr
, const char *search_domain
) {
214 /* Checks if an rr matches the specified key. If a search
215 * domain is specified, it will also be checked if the key
216 * with the search domain suffixed might match the RR. */
218 if (rr
->key
->class != key
->class && key
->class != DNS_CLASS_ANY
)
221 if (rr
->key
->type
!= key
->type
&& key
->type
!= DNS_TYPE_ANY
)
224 r
= dns_name_equal(dns_resource_key_name(rr
->key
), dns_resource_key_name(key
));
229 _cleanup_free_
char *joined
= NULL
;
231 r
= dns_name_concat(dns_resource_key_name(key
), search_domain
, &joined
);
235 return dns_name_equal(dns_resource_key_name(rr
->key
), joined
);
241 int dns_resource_key_match_cname_or_dname(const DnsResourceKey
*key
, const DnsResourceKey
*cname
, const char *search_domain
) {
247 if (cname
->class != key
->class && key
->class != DNS_CLASS_ANY
)
250 if (cname
->type
== DNS_TYPE_CNAME
)
251 r
= dns_name_equal(dns_resource_key_name(key
), dns_resource_key_name(cname
));
252 else if (cname
->type
== DNS_TYPE_DNAME
)
253 r
= dns_name_endswith(dns_resource_key_name(key
), dns_resource_key_name(cname
));
261 _cleanup_free_
char *joined
= NULL
;
263 r
= dns_name_concat(dns_resource_key_name(key
), search_domain
, &joined
);
267 if (cname
->type
== DNS_TYPE_CNAME
)
268 return dns_name_equal(joined
, dns_resource_key_name(cname
));
269 else if (cname
->type
== DNS_TYPE_DNAME
)
270 return dns_name_endswith(joined
, dns_resource_key_name(cname
));
276 int dns_resource_key_match_soa(const DnsResourceKey
*key
, const DnsResourceKey
*soa
) {
280 /* Checks whether 'soa' is a SOA record for the specified key. */
282 if (soa
->class != key
->class)
285 if (soa
->type
!= DNS_TYPE_SOA
)
288 return dns_name_endswith(dns_resource_key_name(key
), dns_resource_key_name(soa
));
291 static void dns_resource_key_hash_func(const void *i
, struct siphash
*state
) {
292 const DnsResourceKey
*k
= i
;
296 dns_name_hash_func(dns_resource_key_name(k
), state
);
297 siphash24_compress(&k
->class, sizeof(k
->class), state
);
298 siphash24_compress(&k
->type
, sizeof(k
->type
), state
);
301 static int dns_resource_key_compare_func(const void *a
, const void *b
) {
302 const DnsResourceKey
*x
= a
, *y
= b
;
305 ret
= dns_name_compare_func(dns_resource_key_name(x
), dns_resource_key_name(y
));
309 if (x
->type
< y
->type
)
311 if (x
->type
> y
->type
)
314 if (x
->class < y
->class)
316 if (x
->class > y
->class)
322 const struct hash_ops dns_resource_key_hash_ops
= {
323 .hash
= dns_resource_key_hash_func
,
324 .compare
= dns_resource_key_compare_func
327 char* dns_resource_key_to_string(const DnsResourceKey
*key
, char *buf
, size_t buf_size
) {
331 /* If we cannot convert the CLASS/TYPE into a known string,
332 use the format recommended by RFC 3597, Section 5. */
334 c
= dns_class_to_string(key
->class);
335 t
= dns_type_to_string(key
->type
);
337 snprintf(buf
, buf_size
, "%s %s%s%.0u %s%s%.0u",
338 dns_resource_key_name(key
),
339 c
?: "", c
? "" : "CLASS", c
? 0 : key
->class,
340 t
?: "", t
? "" : "TYPE", t
? 0 : key
->class);
345 bool dns_resource_key_reduce(DnsResourceKey
**a
, DnsResourceKey
**b
) {
349 /* Try to replace one RR key by another if they are identical, thus saving a bit of memory. Note that we do
350 * this only for RR keys, not for RRs themselves, as they carry a lot of additional metadata (where they come
351 * from, validity data, and suchlike), and cannot be replaced so easily by other RRs that have the same
352 * superficial data. */
359 /* We refuse merging const keys */
360 if ((*a
)->n_ref
== (unsigned) -1)
362 if ((*b
)->n_ref
== (unsigned) -1)
365 /* Already the same? */
369 /* Are they really identical? */
370 if (dns_resource_key_equal(*a
, *b
) <= 0)
373 /* Keep the one which already has more references. */
374 if ((*a
)->n_ref
> (*b
)->n_ref
) {
375 dns_resource_key_unref(*b
);
376 *b
= dns_resource_key_ref(*a
);
378 dns_resource_key_unref(*a
);
379 *a
= dns_resource_key_ref(*b
);
385 DnsResourceRecord
* dns_resource_record_new(DnsResourceKey
*key
) {
386 DnsResourceRecord
*rr
;
388 rr
= new0(DnsResourceRecord
, 1);
393 rr
->key
= dns_resource_key_ref(key
);
394 rr
->expiry
= USEC_INFINITY
;
395 rr
->n_skip_labels_signer
= rr
->n_skip_labels_source
= (unsigned) -1;
400 DnsResourceRecord
* dns_resource_record_new_full(uint16_t class, uint16_t type
, const char *name
) {
401 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
403 key
= dns_resource_key_new(class, type
, name
);
407 return dns_resource_record_new(key
);
410 DnsResourceRecord
* dns_resource_record_ref(DnsResourceRecord
*rr
) {
414 assert(rr
->n_ref
> 0);
420 DnsResourceRecord
* dns_resource_record_unref(DnsResourceRecord
*rr
) {
424 assert(rr
->n_ref
> 0);
432 switch(rr
->key
->type
) {
452 dns_txt_item_free_all(rr
->txt
.items
);
461 free(rr
->mx
.exchange
);
469 free(rr
->sshfp
.fingerprint
);
472 case DNS_TYPE_DNSKEY
:
473 free(rr
->dnskey
.key
);
477 free(rr
->rrsig
.signer
);
478 free(rr
->rrsig
.signature
);
482 free(rr
->nsec
.next_domain_name
);
483 bitmap_free(rr
->nsec
.types
);
487 free(rr
->nsec3
.next_hashed_name
);
488 free(rr
->nsec3
.salt
);
489 bitmap_free(rr
->nsec3
.types
);
506 case DNS_TYPE_OPENPGPKEY
:
508 free(rr
->generic
.data
);
511 free(rr
->wire_format
);
512 dns_resource_key_unref(rr
->key
);
521 int dns_resource_record_new_reverse(DnsResourceRecord
**ret
, int family
, const union in_addr_union
*address
, const char *hostname
) {
522 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
523 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
524 _cleanup_free_
char *ptr
= NULL
;
531 r
= dns_name_reverse(family
, address
, &ptr
);
535 key
= dns_resource_key_new_consume(DNS_CLASS_IN
, DNS_TYPE_PTR
, ptr
);
541 rr
= dns_resource_record_new(key
);
545 rr
->ptr
.name
= strdup(hostname
);
555 int dns_resource_record_new_address(DnsResourceRecord
**ret
, int family
, const union in_addr_union
*address
, const char *name
) {
556 DnsResourceRecord
*rr
;
562 if (family
== AF_INET
) {
564 rr
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_A
, name
);
568 rr
->a
.in_addr
= address
->in
;
570 } else if (family
== AF_INET6
) {
572 rr
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_AAAA
, name
);
576 rr
->aaaa
.in6_addr
= address
->in6
;
578 return -EAFNOSUPPORT
;
585 #define FIELD_EQUAL(a, b, field) \
586 ((a).field ## _size == (b).field ## _size && \
587 memcmp((a).field, (b).field, (a).field ## _size) == 0)
589 int dns_resource_record_equal(const DnsResourceRecord
*a
, const DnsResourceRecord
*b
) {
598 r
= dns_resource_key_equal(a
->key
, b
->key
);
602 if (a
->unparseable
!= b
->unparseable
)
605 switch (a
->unparseable
? _DNS_TYPE_INVALID
: a
->key
->type
) {
608 r
= dns_name_equal(a
->srv
.name
, b
->srv
.name
);
612 return a
->srv
.priority
== b
->srv
.priority
&&
613 a
->srv
.weight
== b
->srv
.weight
&&
614 a
->srv
.port
== b
->srv
.port
;
620 return dns_name_equal(a
->ptr
.name
, b
->ptr
.name
);
623 return strcaseeq(a
->hinfo
.cpu
, b
->hinfo
.cpu
) &&
624 strcaseeq(a
->hinfo
.os
, b
->hinfo
.os
);
626 case DNS_TYPE_SPF
: /* exactly the same as TXT */
628 return dns_txt_item_equal(a
->txt
.items
, b
->txt
.items
);
631 return memcmp(&a
->a
.in_addr
, &b
->a
.in_addr
, sizeof(struct in_addr
)) == 0;
634 return memcmp(&a
->aaaa
.in6_addr
, &b
->aaaa
.in6_addr
, sizeof(struct in6_addr
)) == 0;
637 r
= dns_name_equal(a
->soa
.mname
, b
->soa
.mname
);
640 r
= dns_name_equal(a
->soa
.rname
, b
->soa
.rname
);
644 return a
->soa
.serial
== b
->soa
.serial
&&
645 a
->soa
.refresh
== b
->soa
.refresh
&&
646 a
->soa
.retry
== b
->soa
.retry
&&
647 a
->soa
.expire
== b
->soa
.expire
&&
648 a
->soa
.minimum
== b
->soa
.minimum
;
651 if (a
->mx
.priority
!= b
->mx
.priority
)
654 return dns_name_equal(a
->mx
.exchange
, b
->mx
.exchange
);
657 assert(a
->loc
.version
== b
->loc
.version
);
659 return a
->loc
.size
== b
->loc
.size
&&
660 a
->loc
.horiz_pre
== b
->loc
.horiz_pre
&&
661 a
->loc
.vert_pre
== b
->loc
.vert_pre
&&
662 a
->loc
.latitude
== b
->loc
.latitude
&&
663 a
->loc
.longitude
== b
->loc
.longitude
&&
664 a
->loc
.altitude
== b
->loc
.altitude
;
667 return a
->ds
.key_tag
== b
->ds
.key_tag
&&
668 a
->ds
.algorithm
== b
->ds
.algorithm
&&
669 a
->ds
.digest_type
== b
->ds
.digest_type
&&
670 FIELD_EQUAL(a
->ds
, b
->ds
, digest
);
673 return a
->sshfp
.algorithm
== b
->sshfp
.algorithm
&&
674 a
->sshfp
.fptype
== b
->sshfp
.fptype
&&
675 FIELD_EQUAL(a
->sshfp
, b
->sshfp
, fingerprint
);
677 case DNS_TYPE_DNSKEY
:
678 return a
->dnskey
.flags
== b
->dnskey
.flags
&&
679 a
->dnskey
.protocol
== b
->dnskey
.protocol
&&
680 a
->dnskey
.algorithm
== b
->dnskey
.algorithm
&&
681 FIELD_EQUAL(a
->dnskey
, b
->dnskey
, key
);
684 /* do the fast comparisons first */
685 return a
->rrsig
.type_covered
== b
->rrsig
.type_covered
&&
686 a
->rrsig
.algorithm
== b
->rrsig
.algorithm
&&
687 a
->rrsig
.labels
== b
->rrsig
.labels
&&
688 a
->rrsig
.original_ttl
== b
->rrsig
.original_ttl
&&
689 a
->rrsig
.expiration
== b
->rrsig
.expiration
&&
690 a
->rrsig
.inception
== b
->rrsig
.inception
&&
691 a
->rrsig
.key_tag
== b
->rrsig
.key_tag
&&
692 FIELD_EQUAL(a
->rrsig
, b
->rrsig
, signature
) &&
693 dns_name_equal(a
->rrsig
.signer
, b
->rrsig
.signer
);
696 return dns_name_equal(a
->nsec
.next_domain_name
, b
->nsec
.next_domain_name
) &&
697 bitmap_equal(a
->nsec
.types
, b
->nsec
.types
);
700 return a
->nsec3
.algorithm
== b
->nsec3
.algorithm
&&
701 a
->nsec3
.flags
== b
->nsec3
.flags
&&
702 a
->nsec3
.iterations
== b
->nsec3
.iterations
&&
703 FIELD_EQUAL(a
->nsec3
, b
->nsec3
, salt
) &&
704 FIELD_EQUAL(a
->nsec3
, b
->nsec3
, next_hashed_name
) &&
705 bitmap_equal(a
->nsec3
.types
, b
->nsec3
.types
);
708 return a
->tlsa
.cert_usage
== b
->tlsa
.cert_usage
&&
709 a
->tlsa
.selector
== b
->tlsa
.selector
&&
710 a
->tlsa
.matching_type
== b
->tlsa
.matching_type
&&
711 FIELD_EQUAL(a
->tlsa
, b
->tlsa
, data
);
714 return a
->caa
.flags
== b
->caa
.flags
&&
715 streq(a
->caa
.tag
, b
->caa
.tag
) &&
716 FIELD_EQUAL(a
->caa
, b
->caa
, value
);
718 case DNS_TYPE_OPENPGPKEY
:
720 return FIELD_EQUAL(a
->generic
, b
->generic
, data
);
724 static char* format_location(uint32_t latitude
, uint32_t longitude
, uint32_t altitude
,
725 uint8_t size
, uint8_t horiz_pre
, uint8_t vert_pre
) {
727 char NS
= latitude
>= 1U<<31 ? 'N' : 'S';
728 char EW
= longitude
>= 1U<<31 ? 'E' : 'W';
730 int lat
= latitude
>= 1U<<31 ? (int) (latitude
- (1U<<31)) : (int) ((1U<<31) - latitude
);
731 int lon
= longitude
>= 1U<<31 ? (int) (longitude
- (1U<<31)) : (int) ((1U<<31) - longitude
);
732 double alt
= altitude
>= 10000000u ? altitude
- 10000000u : -(double)(10000000u - altitude
);
733 double siz
= (size
>> 4) * exp10((double) (size
& 0xF));
734 double hor
= (horiz_pre
>> 4) * exp10((double) (horiz_pre
& 0xF));
735 double ver
= (vert_pre
>> 4) * exp10((double) (vert_pre
& 0xF));
737 if (asprintf(&s
, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
740 (lat
% 60000) / 1000.,
744 (lon
% 60000) / 1000.,
755 static int format_timestamp_dns(char *buf
, size_t l
, time_t sec
) {
759 assert(l
> strlen("YYYYMMDDHHmmSS"));
761 if (!gmtime_r(&sec
, &tm
))
764 if (strftime(buf
, l
, "%Y%m%d%H%M%S", &tm
) <= 0)
770 static char *format_types(Bitmap
*types
) {
771 _cleanup_strv_free_
char **strv
= NULL
;
772 _cleanup_free_
char *str
= NULL
;
777 BITMAP_FOREACH(type
, types
, i
) {
778 if (dns_type_to_string(type
)) {
779 r
= strv_extend(&strv
, dns_type_to_string(type
));
785 r
= asprintf(&t
, "TYPE%u", type
);
789 r
= strv_consume(&strv
, t
);
795 str
= strv_join(strv
, " ");
799 return strjoin("( ", str
, " )", NULL
);
802 static char *format_txt(DnsTxtItem
*first
) {
807 LIST_FOREACH(items
, i
, first
)
808 c
+= i
->length
* 4 + 3;
810 p
= s
= new(char, c
);
814 LIST_FOREACH(items
, i
, first
) {
822 for (j
= 0; j
< i
->length
; j
++) {
823 if (i
->data
[j
] < ' ' || i
->data
[j
] == '"' || i
->data
[j
] >= 127) {
825 *(p
++) = '0' + (i
->data
[j
] / 100);
826 *(p
++) = '0' + ((i
->data
[j
] / 10) % 10);
827 *(p
++) = '0' + (i
->data
[j
] % 10);
839 const char *dns_resource_record_to_string(DnsResourceRecord
*rr
) {
840 _cleanup_free_
char *t
= NULL
;
841 char *s
, k
[DNS_RESOURCE_KEY_STRING_MAX
];
847 return rr
->to_string
;
849 dns_resource_key_to_string(rr
->key
, k
, sizeof(k
));
851 switch (rr
->unparseable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
854 r
= asprintf(&s
, "%s %u %u %u %s",
859 strna(rr
->srv
.name
));
868 s
= strjoin(k
, " ", rr
->ptr
.name
, NULL
);
875 s
= strjoin(k
, " ", rr
->hinfo
.cpu
, " ", rr
->hinfo
.os
, NULL
);
880 case DNS_TYPE_SPF
: /* exactly the same as TXT */
882 t
= format_txt(rr
->txt
.items
);
886 s
= strjoin(k
, " ", t
, NULL
);
892 _cleanup_free_
char *x
= NULL
;
894 r
= in_addr_to_string(AF_INET
, (const union in_addr_union
*) &rr
->a
.in_addr
, &x
);
898 s
= strjoin(k
, " ", x
, NULL
);
905 r
= in_addr_to_string(AF_INET6
, (const union in_addr_union
*) &rr
->aaaa
.in6_addr
, &t
);
909 s
= strjoin(k
, " ", t
, NULL
);
915 r
= asprintf(&s
, "%s %s %s %u %u %u %u %u",
917 strna(rr
->soa
.mname
),
918 strna(rr
->soa
.rname
),
929 r
= asprintf(&s
, "%s %u %s",
938 assert(rr
->loc
.version
== 0);
940 t
= format_location(rr
->loc
.latitude
,
949 s
= strjoin(k
, " ", t
, NULL
);
955 t
= hexmem(rr
->ds
.digest
, rr
->ds
.digest_size
);
959 r
= asprintf(&s
, "%s %u %u %u %s",
970 t
= hexmem(rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
);
974 r
= asprintf(&s
, "%s %u %u %s",
983 case DNS_TYPE_DNSKEY
: {
984 _cleanup_free_
char *alg
= NULL
;
989 key_tag
= dnssec_keytag(rr
, true);
991 r
= dnssec_algorithm_to_string_alloc(rr
->dnskey
.algorithm
, &alg
);
995 r
= asprintf(&s
, "%s %u %u %s %n",
1004 r
= base64_append(&s
, n
,
1005 rr
->dnskey
.key
, rr
->dnskey
.key_size
,
1010 r
= asprintf(&ss
, "%s\n"
1011 " -- Flags:%s%s%s\n"
1014 rr
->dnskey
.flags
& DNSKEY_FLAG_SEP
? " SEP" : "",
1015 rr
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
? " REVOKE" : "",
1016 rr
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
? " ZONE_KEY" : "",
1026 case DNS_TYPE_RRSIG
: {
1027 _cleanup_free_
char *alg
= NULL
;
1028 char expiration
[strlen("YYYYMMDDHHmmSS") + 1], inception
[strlen("YYYYMMDDHHmmSS") + 1];
1032 type
= dns_type_to_string(rr
->rrsig
.type_covered
);
1034 r
= dnssec_algorithm_to_string_alloc(rr
->rrsig
.algorithm
, &alg
);
1038 r
= format_timestamp_dns(expiration
, sizeof(expiration
), rr
->rrsig
.expiration
);
1042 r
= format_timestamp_dns(inception
, sizeof(inception
), rr
->rrsig
.inception
);
1047 * http://tools.ietf.org/html/rfc3597#section-5 */
1049 r
= asprintf(&s
, "%s %s%.*u %s %u %u %s %s %u %s %n",
1052 type
? 0 : 1, type
? 0u : (unsigned) rr
->rrsig
.type_covered
,
1055 rr
->rrsig
.original_ttl
,
1064 r
= base64_append(&s
, n
,
1065 rr
->rrsig
.signature
, rr
->rrsig
.signature_size
,
1074 t
= format_types(rr
->nsec
.types
);
1078 r
= asprintf(&s
, "%s %s %s",
1080 rr
->nsec
.next_domain_name
,
1086 case DNS_TYPE_NSEC3
: {
1087 _cleanup_free_
char *salt
= NULL
, *hash
= NULL
;
1089 if (rr
->nsec3
.salt_size
> 0) {
1090 salt
= hexmem(rr
->nsec3
.salt
, rr
->nsec3
.salt_size
);
1095 hash
= base32hexmem(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, false);
1099 t
= format_types(rr
->nsec3
.types
);
1103 r
= asprintf(&s
, "%s %"PRIu8
" %"PRIu8
" %"PRIu16
" %s %s %s",
1105 rr
->nsec3
.algorithm
,
1107 rr
->nsec3
.iterations
,
1108 rr
->nsec3
.salt_size
> 0 ? salt
: "-",
1117 case DNS_TYPE_TLSA
: {
1118 const char *cert_usage
, *selector
, *matching_type
;
1122 cert_usage
= tlsa_cert_usage_to_string(rr
->tlsa
.cert_usage
);
1123 selector
= tlsa_selector_to_string(rr
->tlsa
.selector
);
1124 matching_type
= tlsa_matching_type_to_string(rr
->tlsa
.matching_type
);
1126 r
= asprintf(&s
, "%s %u %u %u %n",
1128 rr
->tlsa
.cert_usage
,
1130 rr
->tlsa
.matching_type
,
1135 r
= base64_append(&s
, n
,
1136 rr
->tlsa
.data
, rr
->tlsa
.data_size
,
1141 r
= asprintf(&ss
, "%s\n"
1142 " -- Cert. usage: %s\n"
1143 " -- Selector: %s\n"
1144 " -- Matching type: %s",
1157 case DNS_TYPE_CAA
: {
1158 _cleanup_free_
char *value
;
1160 value
= octescape(rr
->caa
.value
, rr
->caa
.value_size
);
1164 r
= asprintf(&s
, "%s %u %s \"%s\"%s%s%s%.0u",
1169 rr
->caa
.flags
? "\n -- Flags:" : "",
1170 rr
->caa
.flags
& CAA_FLAG_CRITICAL
? " critical" : "",
1171 rr
->caa
.flags
& ~CAA_FLAG_CRITICAL
? " " : "",
1172 rr
->caa
.flags
& ~CAA_FLAG_CRITICAL
);
1179 case DNS_TYPE_OPENPGPKEY
: {
1182 r
= asprintf(&s
, "%s %n",
1188 r
= base64_append(&s
, n
,
1189 rr
->generic
.data
, rr
->generic
.data_size
,
1197 t
= hexmem(rr
->generic
.data
, rr
->generic
.data_size
);
1201 /* Format as documented in RFC 3597, Section 5 */
1202 r
= asprintf(&s
, "%s \\# %zu %s", k
, rr
->generic
.data_size
, t
);
1212 ssize_t
dns_resource_record_payload(DnsResourceRecord
*rr
, void **out
) {
1216 switch(rr
->unparseable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
1220 case DNS_TYPE_CNAME
:
1221 case DNS_TYPE_DNAME
:
1222 case DNS_TYPE_HINFO
:
1231 case DNS_TYPE_SSHFP
:
1232 case DNS_TYPE_DNSKEY
:
1233 case DNS_TYPE_RRSIG
:
1235 case DNS_TYPE_NSEC3
:
1239 *out
= rr
->tlsa
.data
;
1240 return rr
->tlsa
.data_size
;
1243 case DNS_TYPE_OPENPGPKEY
:
1245 *out
= rr
->generic
.data
;
1246 return rr
->generic
.data_size
;
1250 int dns_resource_record_to_wire_format(DnsResourceRecord
*rr
, bool canonical
) {
1252 DnsPacket packet
= {
1254 .protocol
= DNS_PROTOCOL_DNS
,
1256 .refuse_compression
= true,
1257 .canonical_form
= canonical
,
1265 /* Generates the RR in wire-format, optionally in the
1266 * canonical form as discussed in the DNSSEC RFC 4034, Section
1267 * 6.2. We allocate a throw-away DnsPacket object on the stack
1268 * here, because we need some book-keeping for memory
1269 * management, and can reuse the DnsPacket serializer, that
1270 * can generate the canonical form, too, but also knows label
1271 * compression and suchlike. */
1273 if (rr
->wire_format
&& rr
->wire_format_canonical
== canonical
)
1276 r
= dns_packet_append_rr(&packet
, rr
, &start
, &rds
);
1281 assert(packet
._data
);
1283 free(rr
->wire_format
);
1284 rr
->wire_format
= packet
._data
;
1285 rr
->wire_format_size
= packet
.size
;
1286 rr
->wire_format_rdata_offset
= rds
;
1287 rr
->wire_format_canonical
= canonical
;
1289 packet
._data
= NULL
;
1290 dns_packet_unref(&packet
);
1295 int dns_resource_record_signer(DnsResourceRecord
*rr
, const char **ret
) {
1302 /* Returns the RRset's signer, if it is known. */
1304 if (rr
->n_skip_labels_signer
== (unsigned) -1)
1307 n
= dns_resource_key_name(rr
->key
);
1308 r
= dns_name_skip(n
, rr
->n_skip_labels_signer
, &n
);
1318 int dns_resource_record_source(DnsResourceRecord
*rr
, const char **ret
) {
1325 /* Returns the RRset's synthesizing source, if it is known. */
1327 if (rr
->n_skip_labels_source
== (unsigned) -1)
1330 n
= dns_resource_key_name(rr
->key
);
1331 r
= dns_name_skip(n
, rr
->n_skip_labels_source
, &n
);
1341 int dns_resource_record_is_signer(DnsResourceRecord
*rr
, const char *zone
) {
1347 r
= dns_resource_record_signer(rr
, &signer
);
1351 return dns_name_equal(zone
, signer
);
1354 int dns_resource_record_is_synthetic(DnsResourceRecord
*rr
) {
1359 /* Returns > 0 if the RR is generated from a wildcard, and is not the asterisk name itself */
1361 if (rr
->n_skip_labels_source
== (unsigned) -1)
1364 if (rr
->n_skip_labels_source
== 0)
1367 if (rr
->n_skip_labels_source
> 1)
1370 r
= dns_name_startswith(dns_resource_key_name(rr
->key
), "*");
1377 void dns_resource_record_hash_func(const void *i
, struct siphash
*state
) {
1378 const DnsResourceRecord
*rr
= i
;
1382 dns_resource_key_hash_func(rr
->key
, state
);
1384 switch (rr
->unparseable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
1387 siphash24_compress(&rr
->srv
.priority
, sizeof(rr
->srv
.priority
), state
);
1388 siphash24_compress(&rr
->srv
.weight
, sizeof(rr
->srv
.weight
), state
);
1389 siphash24_compress(&rr
->srv
.port
, sizeof(rr
->srv
.port
), state
);
1390 dns_name_hash_func(rr
->srv
.name
, state
);
1395 case DNS_TYPE_CNAME
:
1396 case DNS_TYPE_DNAME
:
1397 dns_name_hash_func(rr
->ptr
.name
, state
);
1400 case DNS_TYPE_HINFO
:
1401 string_hash_func(rr
->hinfo
.cpu
, state
);
1402 string_hash_func(rr
->hinfo
.os
, state
);
1406 case DNS_TYPE_SPF
: {
1409 LIST_FOREACH(items
, j
, rr
->txt
.items
) {
1410 siphash24_compress(j
->data
, j
->length
, state
);
1412 /* Add an extra NUL byte, so that "a" followed by "b" doesn't result in the same hash as "ab"
1413 * followed by "". */
1414 siphash24_compress_byte(0, state
);
1420 siphash24_compress(&rr
->a
.in_addr
, sizeof(rr
->a
.in_addr
), state
);
1424 siphash24_compress(&rr
->aaaa
.in6_addr
, sizeof(rr
->aaaa
.in6_addr
), state
);
1428 dns_name_hash_func(rr
->soa
.mname
, state
);
1429 dns_name_hash_func(rr
->soa
.rname
, state
);
1430 siphash24_compress(&rr
->soa
.serial
, sizeof(rr
->soa
.serial
), state
);
1431 siphash24_compress(&rr
->soa
.refresh
, sizeof(rr
->soa
.refresh
), state
);
1432 siphash24_compress(&rr
->soa
.retry
, sizeof(rr
->soa
.retry
), state
);
1433 siphash24_compress(&rr
->soa
.expire
, sizeof(rr
->soa
.expire
), state
);
1434 siphash24_compress(&rr
->soa
.minimum
, sizeof(rr
->soa
.minimum
), state
);
1438 siphash24_compress(&rr
->mx
.priority
, sizeof(rr
->mx
.priority
), state
);
1439 dns_name_hash_func(rr
->mx
.exchange
, state
);
1443 siphash24_compress(&rr
->loc
.version
, sizeof(rr
->loc
.version
), state
);
1444 siphash24_compress(&rr
->loc
.size
, sizeof(rr
->loc
.size
), state
);
1445 siphash24_compress(&rr
->loc
.horiz_pre
, sizeof(rr
->loc
.horiz_pre
), state
);
1446 siphash24_compress(&rr
->loc
.vert_pre
, sizeof(rr
->loc
.vert_pre
), state
);
1447 siphash24_compress(&rr
->loc
.latitude
, sizeof(rr
->loc
.latitude
), state
);
1448 siphash24_compress(&rr
->loc
.longitude
, sizeof(rr
->loc
.longitude
), state
);
1449 siphash24_compress(&rr
->loc
.altitude
, sizeof(rr
->loc
.altitude
), state
);
1452 case DNS_TYPE_SSHFP
:
1453 siphash24_compress(&rr
->sshfp
.algorithm
, sizeof(rr
->sshfp
.algorithm
), state
);
1454 siphash24_compress(&rr
->sshfp
.fptype
, sizeof(rr
->sshfp
.fptype
), state
);
1455 siphash24_compress(rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, state
);
1458 case DNS_TYPE_DNSKEY
:
1459 siphash24_compress(&rr
->dnskey
.flags
, sizeof(rr
->dnskey
.flags
), state
);
1460 siphash24_compress(&rr
->dnskey
.protocol
, sizeof(rr
->dnskey
.protocol
), state
);
1461 siphash24_compress(&rr
->dnskey
.algorithm
, sizeof(rr
->dnskey
.algorithm
), state
);
1462 siphash24_compress(rr
->dnskey
.key
, rr
->dnskey
.key_size
, state
);
1465 case DNS_TYPE_RRSIG
:
1466 siphash24_compress(&rr
->rrsig
.type_covered
, sizeof(rr
->rrsig
.type_covered
), state
);
1467 siphash24_compress(&rr
->rrsig
.algorithm
, sizeof(rr
->rrsig
.algorithm
), state
);
1468 siphash24_compress(&rr
->rrsig
.labels
, sizeof(rr
->rrsig
.labels
), state
);
1469 siphash24_compress(&rr
->rrsig
.original_ttl
, sizeof(rr
->rrsig
.original_ttl
), state
);
1470 siphash24_compress(&rr
->rrsig
.expiration
, sizeof(rr
->rrsig
.expiration
), state
);
1471 siphash24_compress(&rr
->rrsig
.inception
, sizeof(rr
->rrsig
.inception
), state
);
1472 siphash24_compress(&rr
->rrsig
.key_tag
, sizeof(rr
->rrsig
.key_tag
), state
);
1473 dns_name_hash_func(rr
->rrsig
.signer
, state
);
1474 siphash24_compress(rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, state
);
1478 dns_name_hash_func(rr
->nsec
.next_domain_name
, state
);
1479 /* FIXME: we leave out the type bitmap here. Hash
1480 * would be better if we'd take it into account
1485 siphash24_compress(&rr
->ds
.key_tag
, sizeof(rr
->ds
.key_tag
), state
);
1486 siphash24_compress(&rr
->ds
.algorithm
, sizeof(rr
->ds
.algorithm
), state
);
1487 siphash24_compress(&rr
->ds
.digest_type
, sizeof(rr
->ds
.digest_type
), state
);
1488 siphash24_compress(rr
->ds
.digest
, rr
->ds
.digest_size
, state
);
1491 case DNS_TYPE_NSEC3
:
1492 siphash24_compress(&rr
->nsec3
.algorithm
, sizeof(rr
->nsec3
.algorithm
), state
);
1493 siphash24_compress(&rr
->nsec3
.flags
, sizeof(rr
->nsec3
.flags
), state
);
1494 siphash24_compress(&rr
->nsec3
.iterations
, sizeof(rr
->nsec3
.iterations
), state
);
1495 siphash24_compress(rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, state
);
1496 siphash24_compress(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, state
);
1497 /* FIXME: We leave the bitmaps out */
1501 siphash24_compress(&rr
->tlsa
.cert_usage
, sizeof(rr
->tlsa
.cert_usage
), state
);
1502 siphash24_compress(&rr
->tlsa
.selector
, sizeof(rr
->tlsa
.selector
), state
);
1503 siphash24_compress(&rr
->tlsa
.matching_type
, sizeof(rr
->tlsa
.matching_type
), state
);
1504 siphash24_compress(rr
->tlsa
.data
, rr
->tlsa
.data_size
, state
);
1508 siphash24_compress(&rr
->caa
.flags
, sizeof(rr
->caa
.flags
), state
);
1509 string_hash_func(rr
->caa
.tag
, state
);
1510 siphash24_compress(rr
->caa
.value
, rr
->caa
.value_size
, state
);
1513 case DNS_TYPE_OPENPGPKEY
:
1515 siphash24_compress(rr
->generic
.data
, rr
->generic
.data_size
, state
);
1520 static int dns_resource_record_compare_func(const void *a
, const void *b
) {
1521 const DnsResourceRecord
*x
= a
, *y
= b
;
1524 ret
= dns_resource_key_compare_func(x
->key
, y
->key
);
1528 if (dns_resource_record_equal(x
, y
))
1531 /* This is a bit dirty, we don't implement proper ordering, but
1532 * the hashtable doesn't need ordering anyway, hence we don't
1534 return x
< y
? -1 : 1;
1537 const struct hash_ops dns_resource_record_hash_ops
= {
1538 .hash
= dns_resource_record_hash_func
,
1539 .compare
= dns_resource_record_compare_func
,
1542 DnsTxtItem
*dns_txt_item_free_all(DnsTxtItem
*i
) {
1551 return dns_txt_item_free_all(n
);
1554 bool dns_txt_item_equal(DnsTxtItem
*a
, DnsTxtItem
*b
) {
1565 if (a
->length
!= b
->length
)
1568 if (memcmp(a
->data
, b
->data
, a
->length
) != 0)
1571 return dns_txt_item_equal(a
->items_next
, b
->items_next
);
1574 static const char* const dnssec_algorithm_table
[_DNSSEC_ALGORITHM_MAX_DEFINED
] = {
1575 /* Mnemonics as listed on https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
1576 [DNSSEC_ALGORITHM_RSAMD5
] = "RSAMD5",
1577 [DNSSEC_ALGORITHM_DH
] = "DH",
1578 [DNSSEC_ALGORITHM_DSA
] = "DSA",
1579 [DNSSEC_ALGORITHM_ECC
] = "ECC",
1580 [DNSSEC_ALGORITHM_RSASHA1
] = "RSASHA1",
1581 [DNSSEC_ALGORITHM_DSA_NSEC3_SHA1
] = "DSA-NSEC3-SHA1",
1582 [DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
] = "RSASHA1-NSEC3-SHA1",
1583 [DNSSEC_ALGORITHM_RSASHA256
] = "RSASHA256",
1584 [DNSSEC_ALGORITHM_RSASHA512
] = "RSASHA512",
1585 [DNSSEC_ALGORITHM_ECC_GOST
] = "ECC-GOST",
1586 [DNSSEC_ALGORITHM_ECDSAP256SHA256
] = "ECDSAP256SHA256",
1587 [DNSSEC_ALGORITHM_ECDSAP384SHA384
] = "ECDSAP384SHA384",
1588 [DNSSEC_ALGORITHM_INDIRECT
] = "INDIRECT",
1589 [DNSSEC_ALGORITHM_PRIVATEDNS
] = "PRIVATEDNS",
1590 [DNSSEC_ALGORITHM_PRIVATEOID
] = "PRIVATEOID",
1592 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_algorithm
, int, 255);
1594 static const char* const dnssec_digest_table
[_DNSSEC_DIGEST_MAX_DEFINED
] = {
1595 /* Names as listed on https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
1596 [DNSSEC_DIGEST_SHA1
] = "SHA-1",
1597 [DNSSEC_DIGEST_SHA256
] = "SHA-256",
1598 [DNSSEC_DIGEST_GOST_R_34_11_94
] = "GOST_R_34.11-94",
1599 [DNSSEC_DIGEST_SHA384
] = "SHA-384",
1601 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_digest
, int, 255);