HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/resolve/resolved-dns-transaction.c
Git blob: 7acad458341057b625463484a08179dca3da17c4
Lines 2001–3000 of 3,879
* freed. Hence, we need to restart the loop. */
return -EAGAIN;
usec_t timeout = transaction_get_resend_timeout(other);
r = dns_transaction_setup_timeout(other, timeout, usec_add(ts, timeout));
if (r < 0)
return r;
if (dns_key_is_shared(dns_transaction_key(other)))
add_known_answers = true;
qdcount++;
if (qdcount >= UINT16_MAX)
break;
}
DNS_PACKET_HEADER(p)->qdcount = htobe16(qdcount);
/* Append known answers section if we're asking for any shared record */
if (add_known_answers) {
r = dns_cache_export_shared_to_packet(&t->scope->cache, p, ts, 0);
if (r < 0)
return r;
}
*ret_packet = TAKE_PTR(p);
*ret_keys = TAKE_PTR(keys);
return add_known_answers;
}
static int dns_transaction_make_packet_mdns(DnsTransaction *t) {
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL, *dummy = NULL;
_cleanup_set_free_ Set *keys = NULL;
bool add_known_answers;
DnsResourceKey *k;
unsigned c;
int r;
assert(t);
assert(t->scope->protocol == DNS_PROTOCOL_MDNS);
/* Discard any previously prepared packet, so we can start over and coalesce again */
t->sent = dns_packet_unref(t->sent);
/* First, create a dummy packet to calculate the number of known answers to be appended in the first packet. */
for (;;) {
r = mdns_make_dummy_packet(t, &dummy, &keys);
if (r == -EAGAIN)
continue;
if (r < 0)
return r;
add_known_answers = r;
break;
}
/* Then, create actual packet. */
r = dns_packet_new_query(&p, t->scope->protocol, 0, false);
if (r < 0)
return r;
/* Questions */
c = 0;
SET_FOREACH(k, keys) {
r = dns_packet_append_key(p, k, 0, NULL);
if (r < 0)
return r;
c++;
}
DNS_PACKET_HEADER(p)->qdcount = htobe16(c);
/* Known answers */
if (add_known_answers) {
usec_t ts;
assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0);
r = dns_cache_export_shared_to_packet(&t->scope->cache, p, ts, be16toh(DNS_PACKET_HEADER(dummy)->ancount));
if (r < 0)
return r;
}
/* Authorities */
c = 0;
SET_FOREACH(k, keys) {
r = dns_packet_append_zone(p, t, k, &c);
if (r < 0)
return r;
}
DNS_PACKET_HEADER(p)->nscount = htobe16(c);
t->sent = TAKE_PTR(p);
return 0;
}
static int dns_transaction_make_packet(DnsTransaction *t) {
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
int r;
assert(t);
if (t->scope->protocol == DNS_PROTOCOL_MDNS)
return dns_transaction_make_packet_mdns(t);
if (t->sent)
return 0;
if (t->bypass && t->bypass->protocol == t->scope->protocol) {
/* If bypass logic is enabled and the protocol if the original packet and our scope match,
* take the original packet, copy it, and patch in our new ID */
r = dns_packet_dup(&p, t->bypass);
if (r < 0)
return r;
} else {
r = dns_packet_new_query(
&p, t->scope->protocol,
/* min_alloc_dsize= */ 0,
/* dnssec_checking_disabled= */ !FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) &&
t->scope->dnssec_mode != DNSSEC_NO);
if (r < 0)
return r;
r = dns_packet_append_key(p, dns_transaction_key(t), 0, NULL);
if (r < 0)
return r;
DNS_PACKET_HEADER(p)->qdcount = htobe16(1);
}
DNS_PACKET_HEADER(p)->id = t->id;
t->sent = TAKE_PTR(p);
return 0;
}
int dns_transaction_go(DnsTransaction *t) {
usec_t ts;
int r;
char key_str[DNS_RESOURCE_KEY_STRING_MAX];
assert(t);
/* Returns > 0 if the transaction is now pending, returns 0 if could be processed immediately and has
* finished now. In the latter case, the transaction and query candidate objects must not be accessed.
*/
assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0);
r = dns_transaction_prepare(t, ts);
if (r <= 0)
return r;
log_debug("Firing %s transaction %" PRIu16 " for <%s> scope %s on %s/%s (validate=%s).",
t->bypass ? "bypass" : "regular",
t->id,
dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str),
dns_protocol_to_string(t->scope->protocol),
t->scope->link ? t->scope->link->ifname : "*",
af_to_name_short(t->scope->family),
yes_no(!FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE)));
if (!t->initial_jitter_scheduled &&
IN_SET(t->scope->protocol, DNS_PROTOCOL_LLMNR, DNS_PROTOCOL_MDNS)) {
usec_t jitter;
/* RFC 4795 Section 2.7 suggests all LLMNR queries should be delayed by a random time from 0 to
* JITTER_INTERVAL.
* RFC 6762 Section 8.1 suggests initial probe queries should be delayed by a random time from
* 0 to 250ms. */
t->initial_jitter_scheduled = true;
t->n_attempts = 0;
switch (t->scope->protocol) {
case DNS_PROTOCOL_LLMNR:
jitter = random_u64_range(LLMNR_JITTER_INTERVAL_USEC);
break;
case DNS_PROTOCOL_MDNS:
if (t->probing)
jitter = random_u64_range(MDNS_PROBING_INTERVAL_USEC);
else
jitter = 0;
break;
default:
assert_not_reached();
}
r = dns_transaction_setup_timeout(t, jitter, ts);
if (r < 0)
return r;
log_debug("Delaying %s transaction %" PRIu16 " for " USEC_FMT "us.",
dns_protocol_to_string(t->scope->protocol),
t->id,
jitter);
return 1;
}
/* Otherwise, we need to ask the network */
r = dns_transaction_make_packet(t);
if (r < 0)
return r;
if (t->scope->protocol == DNS_PROTOCOL_LLMNR &&
(dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), "in-addr.arpa") > 0 ||
dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), "ip6.arpa") > 0)) {
/* RFC 4795, Section 2.4. says reverse lookups shall
* always be made via TCP on LLMNR */
r = dns_transaction_emit_tcp(t);
} else {
/* Try via UDP, and if that fails due to large size or lack of
* support try via TCP */
r = dns_transaction_emit_udp(t);
if (r == -EMSGSIZE)
log_debug("Sending query via TCP since it is too large.");
else if (r == -EAGAIN)
log_debug("Sending query via TCP since UDP isn't supported or DNS-over-TLS is selected.");
else if (r == -EPERM)
log_debug("Sending query via TCP since UDP is blocked.");
if (IN_SET(r, -EMSGSIZE, -EAGAIN, -EPERM))
r = dns_transaction_emit_tcp(t);
}
if (r == -ELOOP) {
if (t->scope->protocol != DNS_PROTOCOL_DNS)
return r;
/* One of our own stub listeners */
log_debug_errno(r, "Detected that specified DNS server is our own extra listener, switching DNS servers.");
dns_scope_next_dns_server(t->scope, t->server);
if (dns_scope_get_dns_server(t->scope) == t->server) {
log_debug_errno(r, "Still pointing to extra listener after switching DNS servers, refusing operation.");
dns_transaction_complete(t, DNS_TRANSACTION_STUB_LOOP);
return 0;
}
return dns_transaction_go(t);
}
if (r == -ESRCH) {
/* No servers to send this to? */
dns_transaction_complete(t, DNS_TRANSACTION_NO_SERVERS);
return 0;
}
if (r == -EOPNOTSUPP) {
/* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
dns_transaction_complete(t, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED);
return 0;
}
if (t->scope->protocol == DNS_PROTOCOL_LLMNR && ERRNO_IS_NEG_DISCONNECT(r)) {
/* On LLMNR, if we cannot connect to a host via TCP when doing reverse lookups. This means we cannot
* answer this request with this protocol. */
dns_transaction_complete(t, DNS_TRANSACTION_NOT_FOUND);
return 0;
}
if (r < 0) {
if (t->scope->protocol != DNS_PROTOCOL_DNS)
return r;
/* Couldn't send? Try immediately again, with a new server */
dns_scope_next_dns_server(t->scope, t->server);
return dns_transaction_go(t);
}
usec_t timeout = transaction_get_resend_timeout(t);
r = dns_transaction_setup_timeout(t, timeout, usec_add(ts, timeout));
if (r < 0)
return r;
return 1;
}
static int dns_transaction_find_cyclic(DnsTransaction *t, DnsTransaction *aux) {
DnsTransaction *n;
int r;
assert(t);
assert(aux);
/* Try to find cyclic dependencies between transaction objects */
if (t == aux)
return 1;
SET_FOREACH(n, aux->dnssec_transactions) {
r = dns_transaction_find_cyclic(t, n);
if (r != 0)
return r;
}
return 0;
}
static int dns_transaction_add_dnssec_transaction(DnsTransaction *t, DnsResourceKey *key, DnsTransaction **ret) {
_cleanup_(dns_transaction_gcp) DnsTransaction *aux = NULL;
int r;
assert(t);
assert(ret);
assert(key);
aux = dns_scope_find_transaction(t->scope, key, t->query_flags);
if (!aux) {
r = dns_transaction_new(&aux, t->scope, key, NULL, t->query_flags);
if (r < 0)
return r;
} else {
if (set_contains(t->dnssec_transactions, aux)) {
*ret = aux;
return 0;
}
r = dns_transaction_find_cyclic(t, aux);
if (r < 0)
return r;
if (r > 0) {
char s[DNS_RESOURCE_KEY_STRING_MAX], saux[DNS_RESOURCE_KEY_STRING_MAX];
return log_debug_errno(SYNTHETIC_ERRNO(ELOOP),
"Potential cyclic dependency, refusing to add transaction %" PRIu16 " (%s) as dependency for %" PRIu16 " (%s).",
aux->id,
dns_resource_key_to_string(dns_transaction_key(t), s, sizeof s),
t->id,
dns_resource_key_to_string(dns_transaction_key(aux), saux, sizeof saux));
}
}
r = set_ensure_allocated(&aux->notify_transactions_done, NULL);
if (r < 0)
return r;
r = set_ensure_put(&t->dnssec_transactions, NULL, aux);
if (r < 0)
return r;
r = set_ensure_put(&aux->notify_transactions, NULL, t);
if (r < 0) {
(void) set_remove(t->dnssec_transactions, aux);
return r;
}
*ret = TAKE_PTR(aux);
return 1;
}
static int dns_transaction_request_dnssec_rr_full(DnsTransaction *t, DnsResourceKey *key, DnsTransaction **ret) {
_cleanup_(dns_answer_unrefp) DnsAnswer *a = NULL;
DnsTransaction *aux;
int r;
assert(t);
assert(key);
/* Try to get the data from the trust anchor */
r = dns_trust_anchor_lookup_positive(&t->scope->manager->trust_anchor, key, &a);
if (r < 0)
return r;
if (r > 0) {
r = dns_answer_extend(&t->validated_keys, a);
if (r < 0)
return r;
if (ret)
*ret = NULL;
return 0;
}
/* This didn't work, ask for it via the network/cache then. */
r = dns_transaction_add_dnssec_transaction(t, key, &aux);
if (r == -ELOOP) { /* This would result in a cyclic dependency */
if (ret)
*ret = NULL;
return 0;
}
if (r < 0)
return r;
if (aux->state == DNS_TRANSACTION_NULL) {
r = dns_transaction_go(aux);
if (r < 0)
return r;
}
if (ret)
*ret = aux;
return 1;
}
static int dns_transaction_request_dnssec_rr(DnsTransaction *t, DnsResourceKey *key) {
assert(t);
assert(key);
return dns_transaction_request_dnssec_rr_full(t, key, NULL);
}
static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction *t, const char *name) {
int r;
assert(t);
/* Check whether the specified name is in the NTA
* database, either in the global one, or the link-local
* one. */
r = dns_trust_anchor_lookup_negative(&t->scope->manager->trust_anchor, name);
if (r != 0)
return r;
if (!t->scope->link)
return 0;
return link_negative_trust_anchor_lookup(t->scope->link, name);
}
static int dns_transaction_has_negative_answer(DnsTransaction *t) {
int r;
assert(t);
/* Checks whether the answer is negative, and lacks NSEC/NSEC3
* RRs to prove it */
r = dns_transaction_has_positive_answer(t, NULL);
if (r < 0)
return r;
if (r > 0)
return false;
/* Is this key explicitly listed as a negative trust anchor?
* If so, it's nothing we need to care about */
r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(dns_transaction_key(t)));
if (r < 0)
return r;
return !r;
}
static int dns_transaction_is_primary_response(DnsTransaction *t, DnsResourceRecord *rr) {
int r;
assert(t);
assert(rr);
/* Check if the specified RR is the "primary" response,
* i.e. either matches the question precisely or is a
* CNAME/DNAME for it. */
r = dns_resource_key_match_rr(dns_transaction_key(t), rr, NULL);
if (r != 0)
return r;
return dns_resource_key_match_cname_or_dname(dns_transaction_key(t), rr->key, NULL);
}
static bool dns_transaction_dnssec_supported(DnsTransaction *t) {
assert(t);
/* Checks whether our transaction's DNS server is assumed to be compatible with DNSSEC. Returns false as soon
* as we changed our mind about a server, and now believe it is incompatible with DNSSEC. */
if (t->scope->protocol != DNS_PROTOCOL_DNS)
return false;
/* If we have picked no server, then we are working from the cache or some other source, and DNSSEC might well
* be supported, hence return true. */
if (!t->server)
return true;
/* Same if the answer came from the cache on a retry: server and feature level are then stale. */
if (t->answer_source != DNS_TRANSACTION_NETWORK)
return true;
/* A query sent below DO cannot get RRSIGs back, whatever the server is capable of, e.g. after
* packet loss degraded the server or a SERVFAIL clamped this transaction. Validating it can only
* fail, so treat it like a reply from a server without DNSSEC. Except in strict mode, where
* unsigned data is only an error if it needed signing (negative trust anchors, insecure
* delegations), so leave that to validation, as dns_server_dnssec_supported() does.
*
* A bypass query goes out as the client built it, hence it's the client's DO bit that counts there,
* not the feature level. Note that we cannot look at t->sent for this: dns_packet_do() only works on
* extracted packets, and we never extract the packets we send. (If the client's packet has
* trailing garbage, extraction drops its OPT RR, so we see no DO here even if the copy we send
* carries it.) */
if (t->scope->dnssec_mode != DNSSEC_YES &&
!(t->bypass ? dns_packet_do(t->bypass) :
DNS_SERVER_FEATURE_LEVEL_IS_DNSSEC(t->current_feature_level)))
return false;
return dns_server_dnssec_supported(t->server);
}
static bool dns_transaction_dnssec_supported_full(DnsTransaction *t) {
DnsTransaction *dt;
assert(t);
/* Checks whether our transaction our any of the auxiliary transactions couldn't do DNSSEC. */
if (!dns_transaction_dnssec_supported(t))
return false;
SET_FOREACH(dt, t->dnssec_transactions)
if (!dns_transaction_dnssec_supported(dt))
return false;
return true;
}
int dns_transaction_request_dnssec_keys(DnsTransaction *t) {
DnsResourceRecord *rr;
/* Have we already requested a record that would be sufficient to validate an insecure delegation? */
bool chased_insecure = false;
int r;
assert(t);
/*
* Retrieve all auxiliary RRs for the answer we got, so that
* we can verify signatures or prove that RRs are rightfully
* unsigned. Specifically:
*
* - For RRSIG we get the matching DNSKEY
* - For DNSKEY we get the matching DS
* - For unsigned SOA/NS we get the matching DS
* - For unsigned CNAME/DNAME/DS we get the parent DS RR
* - For other unsigned RRs we get the matching DS RR
* - For SOA/NS queries with no matching response RR, and no NSEC/NSEC3, the DS RR
* - For DS queries with no matching response RRs, and no NSEC/NSEC3, the parent's DS RR
* - For other queries with no matching response RRs, and no NSEC/NSEC3, the DS RR
*/
if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) || t->scope->dnssec_mode == DNSSEC_NO)
return 0;
if (t->answer_source != DNS_TRANSACTION_NETWORK)
return 0; /* We only need to validate stuff from the network */
if (!dns_transaction_dnssec_supported(t))
return 0; /* If we can't do DNSSEC anyway there's no point in getting the auxiliary RRs */
DNS_ANSWER_FOREACH(rr, t->answer) {
if (dns_type_is_pseudo(rr->key->type))
continue;
/* If this RR is in the negative trust anchor, we don't need to validate it. */
r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key));
if (r < 0)
return r;
if (r > 0)
continue;
switch (rr->key->type) {
case DNS_TYPE_RRSIG: {
/* For each RRSIG we request the matching DNSKEY */
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *dnskey = NULL;
DnsTransaction *aux;
/* If this RRSIG is about a DNSKEY RR and the
* signer is the same as the owner, then we
* already have the DNSKEY, and we don't have
* to look for more. */
if (rr->rrsig.type_covered == DNS_TYPE_DNSKEY) {
r = dns_name_equal(rr->rrsig.signer, dns_resource_key_name(rr->key));
if (r < 0)
return r;
if (r > 0)
continue;
}
/* If the signer is not a parent of our
* original query, then this is about an
* auxiliary RRset, but not anything we asked
* for. In this case we aren't interested,
* because we don't want to request additional
* RRs for stuff we didn't really ask for, and
* also to avoid request loops, where
* additional RRs from one transaction result
* in another transaction whose additional RRs
* point back to the original transaction, and
* we deadlock. */
r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), rr->rrsig.signer);
if (r < 0)
return r;
if (r == 0)
continue;
dnskey = dns_resource_key_new(rr->key->class, DNS_TYPE_DNSKEY, rr->rrsig.signer);
if (!dnskey)
return -ENOMEM;
log_debug("Requesting DNSKEY to validate transaction %" PRIu16" (%s, RRSIG with key tag: %" PRIu16 ").",
t->id, dns_resource_key_name(rr->key), rr->rrsig.key_tag);
r = dns_transaction_request_dnssec_rr_full(t, dnskey, &aux);
if (r < 0)
return r;
/* If we are requesting a DNSKEY, we can anticipate that we will want the matching DS
* in the near future. Let's request it in advance so we don't have to wait in the
* common case. */
if (aux) {
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds =
dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(dnskey));
if (!ds)
return -ENOMEM;
r = dns_transaction_request_dnssec_rr(t, ds);
if (r < 0)
return r;
}
break;
}
case DNS_TYPE_DNSKEY: {
/* For each DNSKEY we request the matching DS */
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
/* If the DNSKEY we are looking at is not for
* zone we are interested in, nor any of its
* parents, we aren't interested, and don't
* request it. After all, we don't want to end
* up in request loops, and want to keep
* additional traffic down. */
r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), dns_resource_key_name(rr->key));
if (r < 0)
return r;
if (r == 0)
continue;
ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key));
if (!ds)
return -ENOMEM;
log_debug("Requesting DS to validate transaction %" PRIu16" (%s, DNSKEY with key tag: %" PRIu16 ").",
t->id, dns_resource_key_name(rr->key), dnssec_keytag(rr, false));
r = dns_transaction_request_dnssec_rr(t, ds);
if (r < 0)
return r;
break;
}
case DNS_TYPE_SOA:
case DNS_TYPE_NS: {
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
/* For an unsigned SOA or NS, try to acquire
* the matching DS RR, as we are at a zone cut
* then, and whether a DS exists tells us
* whether the zone is signed. Do so only if
* this RR matches our original question,
* however. */
r = dns_resource_key_match_rr(dns_transaction_key(t), rr, NULL);
if (r < 0)
return r;
if (r == 0) {
/* Hmm, so this SOA RR doesn't match our original question. In this case, maybe this is
* a negative reply, and we need the SOA RR's TTL in order to cache a negative entry?
* If so, we need to validate it, too. */
r = dns_answer_match_key(t->answer, dns_transaction_key(t), NULL);
if (r < 0)
return r;
if (r > 0) /* positive reply, we won't need the SOA and hence don't need to validate
* it. */
continue;
/* Only bother with this if the SOA/NS RR we are looking at is actually a parent of
* what we are looking for, otherwise there's no value in it for us. */
r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), dns_resource_key_name(rr->key));
if (r < 0)
return r;
if (r == 0)
continue;
/* If we were looking for the DS RR, don't request it again. */
r = dns_name_equal(dns_resource_key_name(dns_transaction_key(t)), dns_resource_key_name(rr->key));
if (r < 0)
return r;
if (r > 0 && dns_transaction_key(t)->type == DNS_TYPE_DS)
continue;
}
r = dnssec_has_rrsig(t->answer, rr->key);
if (r < 0)
return r;
if (r > 0)
continue;
chased_insecure = true;
ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key));
if (!ds)
return -ENOMEM;
log_debug("Requesting DS to validate transaction %" PRIu16 " (%s, unsigned SOA/NS RRset).",
t->id, dns_resource_key_name(rr->key));
r = dns_transaction_request_dnssec_rr(t, ds);
if (r < 0)
return r;
break;
}
case DNS_TYPE_DS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME: {
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
const char *name;
/* CNAMEs and DNAMEs cannot be located at a
* zone apex, hence ask for the parent DS for
* unsigned CNAME/DNAME RRs, maybe that's the
* apex. But do all that only if this is
* actually a response to our original
* question.
*
* Similar for DS RRs, which are signed when
* the parent SOA is signed. */
r = dns_transaction_is_primary_response(t, rr);
if (r < 0)
return r;
if (r == 0)
continue;
r = dnssec_has_rrsig(t->answer, rr->key);
if (r < 0)
return r;
if (r > 0)
continue;
r = dns_answer_has_dname_for_cname(t->answer, rr);
if (r < 0)
return r;
if (r > 0)
continue;
name = dns_resource_key_name(rr->key);
r = dns_name_parent(&name);
if (r < 0)
return r;
if (r == 0)
continue;
ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, name);
if (!ds)
return -ENOMEM;
log_debug("Requesting parent DS to validate transaction %" PRIu16 " (%s, unsigned CNAME/DNAME/DS RRset).",
t->id, dns_resource_key_name(rr->key));
r = dns_transaction_request_dnssec_rr(t, ds);
if (r < 0)
return r;
if (t->scope->dnssec_mode == DNSSEC_ALLOW_DOWNGRADE && dns_name_is_root(name)) {
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *soa = NULL;
/* We made it all the way to the root zone. If we are in allow-downgrade
* mode, we need to make at least one request that we can be certain should
* have been signed, to test for servers that are not dnssec aware. */
soa = dns_resource_key_new(rr->key->class, DNS_TYPE_SOA, name);
if (!soa)
return -ENOMEM;
log_debug("Requesting root zone SOA to probe dnssec support.");
r = dns_transaction_request_dnssec_rr(t, soa);
if (r < 0)
return r;
}
break;
}
default: {
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
/* For other unsigned RRsets (including
* NSEC/NSEC3!), look for proof the zone is
* unsigned, by requesting the DS RR of the
* zone. However, do so only if they are
* directly relevant to our original
* question. */
r = dns_transaction_is_primary_response(t, rr);
if (r < 0)
return r;
if (r == 0)
continue;
r = dnssec_has_rrsig(t->answer, rr->key);
if (r < 0)
return r;
if (r > 0)
continue;
ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key));
if (!ds)
return -ENOMEM;
log_debug("Requesting DS to validate transaction %" PRIu16 " (%s, unsigned non-SOA/NS RRset <%s>).",
t->id, dns_resource_key_name(rr->key), dns_resource_record_to_string(rr));
r = dns_transaction_request_dnssec_rr(t, ds);
if (r < 0)
return r;
break;
}}
}
/* Above, we requested everything necessary to validate what
* we got. Now, let's request what we need to validate what we
* didn't get... */
r = dns_transaction_has_negative_answer(t);
if (r < 0)
return r;
if (r > 0) {
const char *name = dns_resource_key_name(dns_transaction_key(t));
bool was_signed = dns_answer_contains_nsec_or_nsec3(t->answer);
/* If the response is empty, seek the DS for this name, just in case we're at a zone cut
* already, unless we just requested the DS, in which case we have to ask the parent to make
* progress.
*
* If this was an SOA or NS request, we could also skip to the parent, but in real world
* setups there are too many broken DNS servers (Hello, incapdns.net!) where non-terminal
* zones return NXDOMAIN even though they have further children. */
if (chased_insecure || was_signed)
/* In this case we already requested what we need above. */
name = NULL;
else if (dns_transaction_key(t)->type == DNS_TYPE_DS)
/* If the DS response is empty, we'll walk up the dns labels requesting DS until we
* find a referral to the SOA or hit it anyway and get a positive DS response. */
if (dns_name_parent(&name) <= 0)
name = NULL;
if (name) {
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
log_debug("Requesting DS (%s %s) to validate transaction %" PRIu16 " (%s empty response).",
glyph(GLYPH_ARROW_RIGHT), name, t->id,
dns_resource_key_name(dns_transaction_key(t)));
ds = dns_resource_key_new(dns_transaction_key(t)->class, DNS_TYPE_DS, name);
if (!ds)
return -ENOMEM;
r = dns_transaction_request_dnssec_rr(t, ds);
if (r < 0)
return r;
}
}
return dns_transaction_dnssec_is_live(t);
}
DnsResourceKey* dns_transaction_key(DnsTransaction *t) {
assert(t);
/* Return the lookup key of this transaction. Either takes the lookup key from the bypass packet if
* we are a bypass transaction. Or take the configured key for regular transactions. */
if (t->key)
return t->key;
assert(t->bypass);
return dns_question_first_key(t->bypass->question);
}
void dns_transaction_notify(DnsTransaction *t, DnsTransaction *source) {
assert(t);
assert(source);
/* Invoked whenever any of our auxiliary DNSSEC transactions completed its work. If the state is still PENDING,
we are still in the loop that adds further DNSSEC transactions, hence don't check if we are ready yet. If
the state is VALIDATING however, we should check if we are complete now. */
if (t->state == DNS_TRANSACTION_VALIDATING)
dns_transaction_process_dnssec(t);
}
static int dns_transaction_validate_dnskey_by_ds(DnsTransaction *t) {
DnsAnswerItem *item;
int r;
assert(t);
/* Add all DNSKEY RRs from the answer that are validated by DS
* RRs from the list of validated keys to the list of
* validated keys. */
DNS_ANSWER_FOREACH_ITEM(item, t->answer) {
r = dnssec_verify_dnskey_by_ds_search(item->rr, t->validated_keys);
if (r < 0 && r != -EOPNOTSUPP)
return r;
if (r == 0)
continue;
/* If so, the DNSKEY is validated too, but only mark it authenticated if the DS verification
* succeeded with a known algorithm. */
if (r == -EOPNOTSUPP)
r = dns_answer_add_extend(&t->validated_keys, item->rr, item->ifindex, item->flags, NULL);
else
r = dns_answer_add_extend(&t->validated_keys, item->rr, item->ifindex, item->flags|DNS_ANSWER_AUTHENTICATED, item->rrsig);
if (r < 0)
return r;
}
return 0;
}
static int dns_transaction_requires_rrsig(DnsTransaction *t, DnsResourceRecord *rr) {
int r;
assert(t);
assert(rr);
/* Checks if the RR we are looking for must be signed with an
* RRSIG. This is used for positive responses. */
if (t->scope->dnssec_mode == DNSSEC_NO)
return false;
if (dns_type_is_pseudo(rr->key->type))
return -EINVAL;
r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key));
if (r < 0)
return r;
if (r > 0)
return false;
switch (rr->key->type) {
case DNS_TYPE_RRSIG:
/* RRSIGs are the signatures themselves, they need no signing. */
return false;
case DNS_TYPE_SOA:
case DNS_TYPE_NS: {
DnsTransaction *dt;
/* For SOA or NS RRs we look for a matching DS transaction */
SET_FOREACH(dt, t->dnssec_transactions) {
if (dns_transaction_key(dt)->class != rr->key->class)
continue;
if (dns_transaction_key(dt)->type != DNS_TYPE_DS)
continue;
r = dns_name_endswith(dns_resource_key_name(rr->key), dns_resource_key_name(dns_transaction_key(dt)));
if (r < 0)
return r;
if (r == 0)
continue;
/* We found a DS transactions for the SOA/NS
* RRs we are looking at. If it discovered signed DS
* RRs, then we need to be signed, too. */
if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
return false;
return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL);
}
/* We found nothing that proves this is safe to leave
* this unauthenticated, hence ask inist on
* authentication. */
return true;
}
case DNS_TYPE_DS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME: {
const char *parent = NULL;
DnsTransaction *dt;
/*
* CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent DS.
*
* DS RRs are signed if the parent is signed, hence also look at the parent DS
*/
SET_FOREACH(dt, t->dnssec_transactions) {
if (dns_transaction_key(dt)->class != rr->key->class)
continue;
if (dns_transaction_key(dt)->type != DNS_TYPE_DS)
continue;
if (!parent) {
parent = dns_resource_key_name(rr->key);
r = dns_name_parent(&parent);
if (r < 0)