1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
8 #include "alloc-util.h"
9 #include "dns-domain.h"
10 #include "resolved-dns-answer.h"
11 #include "resolved-dns-dnssec.h"
12 #include "string-util.h"
14 DnsAnswer
*dns_answer_new(size_t n
) {
17 a
= malloc0(offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * n
);
27 DnsAnswer
*dns_answer_ref(DnsAnswer
*a
) {
36 static void dns_answer_flush(DnsAnswer
*a
) {
37 DnsResourceRecord
*rr
;
42 DNS_ANSWER_FOREACH(rr
, a
)
43 dns_resource_record_unref(rr
);
48 DnsAnswer
*dns_answer_unref(DnsAnswer
*a
) {
63 static int dns_answer_add_raw(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
69 if (a
->n_rrs
>= a
->n_allocated
)
72 a
->items
[a
->n_rrs
++] = (DnsAnswerItem
) {
73 .rr
= dns_resource_record_ref(rr
),
81 static int dns_answer_add_raw_all(DnsAnswer
*a
, DnsAnswer
*source
) {
82 DnsResourceRecord
*rr
;
86 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, source
) {
87 r
= dns_answer_add_raw(a
, rr
, ifindex
, flags
);
95 int dns_answer_add(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
106 for (i
= 0; i
< a
->n_rrs
; i
++) {
107 if (a
->items
[i
].ifindex
!= ifindex
)
110 r
= dns_resource_record_equal(a
->items
[i
].rr
, rr
);
114 /* Don't mix contradicting TTLs (see below) */
115 if ((rr
->ttl
== 0) != (a
->items
[i
].rr
->ttl
== 0))
118 /* Entry already exists, keep the entry with
120 if (rr
->ttl
> a
->items
[i
].rr
->ttl
) {
121 dns_resource_record_ref(rr
);
122 dns_resource_record_unref(a
->items
[i
].rr
);
126 a
->items
[i
].flags
|= flags
;
130 r
= dns_resource_key_equal(a
->items
[i
].rr
->key
, rr
->key
);
134 /* There's already an RR of the same RRset in
135 * place! Let's see if the TTLs more or less
136 * match. We don't really care if they match
137 * precisely, but we do care whether one is 0
138 * and the other is not. See RFC 2181, Section
141 if ((rr
->ttl
== 0) != (a
->items
[i
].rr
->ttl
== 0))
146 return dns_answer_add_raw(a
, rr
, ifindex
, flags
);
149 static int dns_answer_add_all(DnsAnswer
*a
, DnsAnswer
*b
) {
150 DnsResourceRecord
*rr
;
151 DnsAnswerFlags flags
;
154 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, b
) {
155 r
= dns_answer_add(a
, rr
, ifindex
, flags
);
163 int dns_answer_add_extend(DnsAnswer
**a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
169 r
= dns_answer_reserve_or_clone(a
, 1);
173 return dns_answer_add(*a
, rr
, ifindex
, flags
);
176 int dns_answer_add_soa(DnsAnswer
*a
, const char *name
, uint32_t ttl
, int ifindex
) {
177 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*soa
= NULL
;
179 soa
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_SOA
, name
);
185 soa
->soa
.mname
= strdup(name
);
189 soa
->soa
.rname
= strappend("root.", name
);
194 soa
->soa
.refresh
= 1;
197 soa
->soa
.minimum
= ttl
;
199 return dns_answer_add(a
, soa
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
202 int dns_answer_match_key(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsAnswerFlags
*ret_flags
) {
203 DnsAnswerFlags flags
= 0, i_flags
;
204 DnsResourceRecord
*i
;
210 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
211 r
= dns_resource_key_match_rr(key
, i
, NULL
);
234 int dns_answer_contains_rr(DnsAnswer
*a
, DnsResourceRecord
*rr
, DnsAnswerFlags
*ret_flags
) {
235 DnsAnswerFlags flags
= 0, i_flags
;
236 DnsResourceRecord
*i
;
242 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
243 r
= dns_resource_record_equal(i
, rr
);
266 int dns_answer_contains_key(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsAnswerFlags
*ret_flags
) {
267 DnsAnswerFlags flags
= 0, i_flags
;
268 DnsResourceRecord
*i
;
274 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
275 r
= dns_resource_key_equal(i
->key
, key
);
298 int dns_answer_contains_nsec_or_nsec3(DnsAnswer
*a
) {
299 DnsResourceRecord
*i
;
301 DNS_ANSWER_FOREACH(i
, a
) {
302 if (IN_SET(i
->key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_NSEC3
))
309 int dns_answer_contains_zone_nsec3(DnsAnswer
*answer
, const char *zone
) {
310 DnsResourceRecord
*rr
;
313 /* Checks whether the specified answer contains at least one NSEC3 RR in the specified zone */
315 DNS_ANSWER_FOREACH(rr
, answer
) {
318 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
321 p
= dns_resource_key_name(rr
->key
);
322 r
= dns_name_parent(&p
);
328 r
= dns_name_equal(p
, zone
);
336 int dns_answer_find_soa(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
337 DnsResourceRecord
*rr
, *soa
= NULL
;
338 DnsAnswerFlags rr_flags
, soa_flags
= 0;
343 /* For a SOA record we can never find a matching SOA record */
344 if (key
->type
== DNS_TYPE_SOA
)
347 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
348 r
= dns_resource_key_match_soa(key
, rr
->key
);
354 r
= dns_name_endswith(dns_resource_key_name(rr
->key
), dns_resource_key_name(soa
->key
));
362 soa_flags
= rr_flags
;
377 int dns_answer_find_cname_or_dname(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
378 DnsResourceRecord
*rr
;
379 DnsAnswerFlags rr_flags
;
384 /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
385 if (!dns_type_may_redirect(key
->type
))
388 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
389 r
= dns_resource_key_match_cname_or_dname(key
, rr
->key
, NULL
);
404 int dns_answer_merge(DnsAnswer
*a
, DnsAnswer
*b
, DnsAnswer
**ret
) {
405 _cleanup_(dns_answer_unrefp
) DnsAnswer
*k
= NULL
;
410 if (dns_answer_size(a
) <= 0) {
411 *ret
= dns_answer_ref(b
);
415 if (dns_answer_size(b
) <= 0) {
416 *ret
= dns_answer_ref(a
);
420 k
= dns_answer_new(a
->n_rrs
+ b
->n_rrs
);
424 r
= dns_answer_add_raw_all(k
, a
);
428 r
= dns_answer_add_all(k
, b
);
437 int dns_answer_extend(DnsAnswer
**a
, DnsAnswer
*b
) {
443 r
= dns_answer_merge(*a
, b
, &merged
);
447 dns_answer_unref(*a
);
453 int dns_answer_remove_by_key(DnsAnswer
**a
, const DnsResourceKey
*key
) {
454 bool found
= false, other
= false;
455 DnsResourceRecord
*rr
;
462 /* Remove all entries matching the specified key from *a */
464 DNS_ANSWER_FOREACH(rr
, *a
) {
465 r
= dns_resource_key_equal(rr
->key
, key
);
481 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
485 if ((*a
)->n_ref
> 1) {
486 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
487 DnsAnswerFlags flags
;
490 copy
= dns_answer_new((*a
)->n_rrs
);
494 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
495 r
= dns_resource_key_equal(rr
->key
, key
);
501 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
506 dns_answer_unref(*a
);
512 /* Only a single reference, edit in-place */
516 if (i
>= (*a
)->n_rrs
)
519 r
= dns_resource_key_equal((*a
)->items
[i
].rr
->key
, key
);
523 /* Kill this entry */
525 dns_resource_record_unref((*a
)->items
[i
].rr
);
526 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
531 /* Keep this entry */
538 int dns_answer_remove_by_rr(DnsAnswer
**a
, DnsResourceRecord
*rm
) {
539 bool found
= false, other
= false;
540 DnsResourceRecord
*rr
;
547 /* Remove all entries matching the specified RR from *a */
549 DNS_ANSWER_FOREACH(rr
, *a
) {
550 r
= dns_resource_record_equal(rr
, rm
);
566 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
570 if ((*a
)->n_ref
> 1) {
571 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
572 DnsAnswerFlags flags
;
575 copy
= dns_answer_new((*a
)->n_rrs
);
579 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
580 r
= dns_resource_record_equal(rr
, rm
);
586 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
591 dns_answer_unref(*a
);
597 /* Only a single reference, edit in-place */
601 if (i
>= (*a
)->n_rrs
)
604 r
= dns_resource_record_equal((*a
)->items
[i
].rr
, rm
);
608 /* Kill this entry */
610 dns_resource_record_unref((*a
)->items
[i
].rr
);
611 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
616 /* Keep this entry */
623 int dns_answer_copy_by_key(DnsAnswer
**a
, DnsAnswer
*source
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
624 DnsResourceRecord
*rr_source
;
625 int ifindex_source
, r
;
626 DnsAnswerFlags flags_source
;
631 /* Copy all RRs matching the specified key from source into *a */
633 DNS_ANSWER_FOREACH_FULL(rr_source
, ifindex_source
, flags_source
, source
) {
635 r
= dns_resource_key_equal(rr_source
->key
, key
);
641 /* Make space for at least one entry */
642 r
= dns_answer_reserve_or_clone(a
, 1);
646 r
= dns_answer_add(*a
, rr_source
, ifindex_source
, flags_source
|or_flags
);
654 int dns_answer_move_by_key(DnsAnswer
**to
, DnsAnswer
**from
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
661 r
= dns_answer_copy_by_key(to
, *from
, key
, or_flags
);
665 return dns_answer_remove_by_key(from
, key
);
668 void dns_answer_order_by_scope(DnsAnswer
*a
, bool prefer_link_local
) {
669 DnsAnswerItem
*items
;
670 size_t i
, start
, end
;
681 /* RFC 4795, Section 2.6 suggests we should order entries
682 * depending on whether the sender is a link-local address. */
684 items
= newa(DnsAnswerItem
, a
->n_rrs
);
685 for (i
= 0; i
< a
->n_rrs
; i
++) {
687 if (a
->items
[i
].rr
->key
->class == DNS_CLASS_IN
&&
688 ((a
->items
[i
].rr
->key
->type
== DNS_TYPE_A
&& in_addr_is_link_local(AF_INET
, (union in_addr_union
*) &a
->items
[i
].rr
->a
.in_addr
) != prefer_link_local
) ||
689 (a
->items
[i
].rr
->key
->type
== DNS_TYPE_AAAA
&& in_addr_is_link_local(AF_INET6
, (union in_addr_union
*) &a
->items
[i
].rr
->aaaa
.in6_addr
) != prefer_link_local
)))
690 /* Order address records that are not preferred to the end of the array */
691 items
[end
--] = a
->items
[i
];
693 /* Order all other records to the beginning of the array */
694 items
[start
++] = a
->items
[i
];
697 assert(start
== end
+1);
698 memcpy(a
->items
, items
, sizeof(DnsAnswerItem
) * a
->n_rrs
);
701 int dns_answer_reserve(DnsAnswer
**a
, size_t n_free
) {
715 ns
= (*a
)->n_rrs
+ n_free
;
717 if ((*a
)->n_allocated
>= ns
)
720 /* Allocate more than we need */
723 n
= realloc(*a
, offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * ns
);
729 n
= dns_answer_new(n_free
);
738 int dns_answer_reserve_or_clone(DnsAnswer
**a
, size_t n_free
) {
739 _cleanup_(dns_answer_unrefp
) DnsAnswer
*n
= NULL
;
744 /* Tries to extend the DnsAnswer object. And if that's not
745 * possible, since we are not the sole owner, then allocate a
746 * new, appropriately sized one. Either way, after this call
747 * the object will only have a single reference, and has room
748 * for at least the specified number of RRs. */
750 r
= dns_answer_reserve(a
, n_free
);
756 n
= dns_answer_new(((*a
)->n_rrs
+ n_free
) * 2);
760 r
= dns_answer_add_raw_all(n
, *a
);
764 dns_answer_unref(*a
);
770 void dns_answer_dump(DnsAnswer
*answer
, FILE *f
) {
771 DnsResourceRecord
*rr
;
772 DnsAnswerFlags flags
;
778 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, answer
) {
783 t
= dns_resource_record_to_string(rr
);
791 if (ifindex
!= 0 || flags
& (DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
|DNS_ANSWER_SHARED_OWNER
))
795 printf(" ifindex=%i", ifindex
);
796 if (flags
& DNS_ANSWER_AUTHENTICATED
)
797 fputs(" authenticated", f
);
798 if (flags
& DNS_ANSWER_CACHEABLE
)
799 fputs(" cachable", f
);
800 if (flags
& DNS_ANSWER_SHARED_OWNER
)
801 fputs(" shared-owner", f
);
807 bool dns_answer_has_dname_for_cname(DnsAnswer
*a
, DnsResourceRecord
*cname
) {
808 DnsResourceRecord
*rr
;
813 /* Checks whether the answer contains a DNAME record that indicates that the specified CNAME record is
814 * synthesized from it */
816 if (cname
->key
->type
!= DNS_TYPE_CNAME
)
819 DNS_ANSWER_FOREACH(rr
, a
) {
820 _cleanup_free_
char *n
= NULL
;
822 if (rr
->key
->type
!= DNS_TYPE_DNAME
)
824 if (rr
->key
->class != cname
->key
->class)
827 r
= dns_name_change_suffix(cname
->cname
.name
, rr
->dname
.name
, dns_resource_key_name(rr
->key
), &n
);
833 r
= dns_name_equal(n
, dns_resource_key_name(cname
->key
));