HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/resolve/resolved-dns-query.c
Git blob: 1498b0e3b763719204e301767f6b6b9a4f74d07a
Lines 1001–1671 of 1,671
if (r < 0)
goto fail;
q->state = DNS_TRANSACTION_PENDING;
q->block_ready++;
/* Start the transactions */
LIST_FOREACH(candidates_by_query, c, q->candidates) {
r = dns_query_candidate_go(c);
if (r < 0) {
q->block_ready--;
goto fail;
}
}
q->block_ready--;
dns_query_ready(q);
return 1;
fail:
dns_query_stop(q);
return r;
}
static void on_hook_complete(HookQuery *hq, int rcode, DnsAnswer *answer, void *userdata) {
DnsQuery *q = ASSERT_PTR(userdata);
int r;
assert(hq);
assert(q->hook_query == hq);
assert(q->state == DNS_TRANSACTION_NULL);
q->hook_query = hook_query_free(q->hook_query);
TAKE_PTR(hq);
if (rcode < 0) {
log_debug("Hook yielded no results, proceeding.");
r = dns_query_go_scopes(q);
if (r < 0) {
dns_query_reset_answer(q);
q->answer_errno = r;
dns_query_complete(q, DNS_TRANSACTION_ERRNO);
}
return;
}
dns_query_reset_answer(q);
q->answer = dns_answer_ref(answer);
q->answer_rcode = rcode;
q->answer_protocol = dns_synthesize_protocol(q->flags);
q->answer_family = dns_synthesize_family(q->flags);
q->answer_query_flags = SD_RESOLVED_FROM_HOOK;
dns_query_complete(q, rcode == DNS_RCODE_SUCCESS ? DNS_TRANSACTION_SUCCESS : DNS_TRANSACTION_RCODE_FAILURE);
}
int dns_query_go(DnsQuery *q) {
int r;
assert(q);
/* Already ongoing? Then suppress */
if (q->hook_query ||
q->state != DNS_TRANSACTION_NULL)
return 0;
r = dns_query_try_static_records(q);
if (r < 0)
return r;
if (r > 0) {
dns_query_complete(q, DNS_TRANSACTION_SUCCESS);
return 1;
}
r = dns_query_try_etc_hosts(q);
if (r < 0)
return r;
if (r > 0) {
dns_query_complete(q, DNS_TRANSACTION_SUCCESS);
return 1;
}
r = manager_hook_query(
q->manager,
q->question_bypass ? q->question_bypass->question : q->question_idna,
q->question_bypass ? q->question_bypass->question : q->question_utf8,
on_hook_complete,
q,
&q->hook_query);
if (r < 0)
return r;
if (r > 0) /* hook calls are pending */
return 0;
return dns_query_go_scopes(q);
}
static void dns_query_accept(DnsQuery *q, DnsQueryCandidate *c) {
DnsTransactionState state = DNS_TRANSACTION_NO_SERVERS;
bool has_authenticated = false, has_non_authenticated = false, has_confidential = false, has_non_confidential = false;
DnssecResult dnssec_result_authenticated = _DNSSEC_RESULT_INVALID, dnssec_result_non_authenticated = _DNSSEC_RESULT_INVALID;
DnsTransaction *t;
int r;
assert(q);
if (!c) {
r = dns_query_synthesize_reply(q, &state);
if (r < 0)
goto fail;
dns_query_complete(q, state);
return;
}
if (c->error_code != 0) {
/* If the candidate had an error condition of its own, start with that. */
state = DNS_TRANSACTION_ERRNO;
q->answer = dns_answer_unref(q->answer);
q->answer_rcode = 0;
q->answer_dnssec_result = _DNSSEC_RESULT_INVALID;
q->answer_query_flags = 0;
q->answer_errno = c->error_code;
q->answer_full_packet = dns_packet_unref(q->answer_full_packet);
}
SET_FOREACH(t, c->transactions) {
switch (t->state) {
case DNS_TRANSACTION_SUCCESS: {
/* We found a successful reply, merge it into the answer */
if (state == DNS_TRANSACTION_SUCCESS) {
r = dns_answer_extend(&q->answer, t->answer);
if (r < 0)
goto fail;
q->answer_query_flags |= dns_transaction_source_to_query_flags(t->answer_source);
} else {
/* Override non-successful previous answers */
DNS_ANSWER_REPLACE(q->answer, dns_answer_ref(t->answer));
q->answer_query_flags = dns_transaction_source_to_query_flags(t->answer_source);
}
q->answer_rcode = t->answer_rcode;
q->answer_errno = 0;
DNS_PACKET_REPLACE(q->answer_full_packet, dns_packet_ref(t->received));
if (FLAGS_SET(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED)) {
has_authenticated = true;
dnssec_result_authenticated = t->answer_dnssec_result;
} else {
has_non_authenticated = true;
dnssec_result_non_authenticated = t->answer_dnssec_result;
}
if (FLAGS_SET(t->answer_query_flags, SD_RESOLVED_CONFIDENTIAL))
has_confidential = true;
else
has_non_confidential = true;
state = DNS_TRANSACTION_SUCCESS;
break;
}
case DNS_TRANSACTION_NULL:
case DNS_TRANSACTION_PENDING:
case DNS_TRANSACTION_VALIDATING:
case DNS_TRANSACTION_ABORTED:
/* Ignore transactions that didn't complete */
continue;
default:
/* Any kind of failure? Store the data away, if there's nothing stored yet. */
if (state == DNS_TRANSACTION_SUCCESS)
continue;
/* If there's already an authenticated negative reply stored, then prefer that over any unauthenticated one */
if (FLAGS_SET(q->answer_query_flags, SD_RESOLVED_AUTHENTICATED) &&
!FLAGS_SET(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
continue;
DNS_ANSWER_REPLACE(q->answer, dns_answer_ref(t->answer));
q->answer_rcode = t->answer_rcode;
q->answer_ede_rcode = t->answer_ede_rcode;
r = free_and_strdup_warn(&q->answer_ede_msg, t->answer_ede_msg);
if (r < 0)
goto fail;
q->answer_dnssec_result = t->answer_dnssec_result;
q->answer_query_flags = t->answer_query_flags | dns_transaction_source_to_query_flags(t->answer_source);
q->answer_errno = t->answer_errno;
DNS_PACKET_REPLACE(q->answer_full_packet, dns_packet_ref(t->received));
state = t->state;
}
}
if (state == DNS_TRANSACTION_SUCCESS) {
SET_FLAG(q->answer_query_flags, SD_RESOLVED_AUTHENTICATED, has_authenticated && !has_non_authenticated);
SET_FLAG(q->answer_query_flags, SD_RESOLVED_CONFIDENTIAL, has_confidential && !has_non_confidential);
q->answer_dnssec_result = FLAGS_SET(q->answer_query_flags, SD_RESOLVED_AUTHENTICATED) ? dnssec_result_authenticated : dnssec_result_non_authenticated;
}
q->answer_protocol = c->scope->protocol;
q->answer_family = c->scope->family;
dns_search_domain_unref(q->answer_search_domain);
q->answer_search_domain = dns_search_domain_ref(c->search_domain);
r = dns_query_synthesize_reply(q, &state);
if (r < 0)
goto fail;
dns_query_complete(q, state);
return;
fail:
q->answer_errno = -r;
dns_query_complete(q, DNS_TRANSACTION_ERRNO);
}
void dns_query_ready(DnsQuery *q) {
DnsQueryCandidate *bad = NULL;
bool pending = false;
assert(q);
assert(DNS_TRANSACTION_IS_LIVE(q->state));
/* Note that this call might invalidate the query. Callers
* should hence not attempt to access the query or transaction
* after calling this function, unless the block_ready
* counter was explicitly bumped before doing so. */
if (q->hook_query)
return;
if (q->block_ready > 0)
return;
LIST_FOREACH(candidates_by_query, c, q->candidates) {
DnsTransactionState state;
state = dns_query_candidate_state(c);
switch (state) {
case DNS_TRANSACTION_SUCCESS:
/* One of the candidates is successful,
* let's use it, and copy its data out */
dns_query_accept(q, c);
return;
case DNS_TRANSACTION_NULL:
case DNS_TRANSACTION_PENDING:
case DNS_TRANSACTION_VALIDATING:
/* One of the candidates is still going on,
* let's maybe wait for it */
pending = true;
break;
default:
/* Any kind of failure: save the most recent error, as long as we never saved one
* before or our current one is "conclusive" in the sense that we definitely did a
* lookup, and thus have a real answer (which might be a failure, but is still *some*
* answer). */
if (!bad || !IN_SET(state, DNS_TRANSACTION_NO_SERVERS, DNS_TRANSACTION_NETWORK_DOWN, DNS_TRANSACTION_NO_SOURCE))
bad = c;
break;
}
}
if (pending)
return;
dns_query_accept(q, bad);
}
static int dns_query_collect_question(DnsQuery *q, DnsQuestion *question) {
_cleanup_(dns_question_unrefp) DnsQuestion *merged = NULL;
int r;
assert(q);
if (dns_question_size(question) == 0)
return 0;
/* When redirecting, save the first element in the chain, for informational purposes when monitoring */
r = dns_question_merge(q->collected_questions, question, &merged);
if (r < 0)
return r;
dns_question_unref(q->collected_questions);
q->collected_questions = TAKE_PTR(merged);
return 0;
}
static int dns_query_cname_redirect(DnsQuery *q, const DnsResourceRecord *cname) {
_cleanup_(dns_question_unrefp) DnsQuestion *nq_idna = NULL, *nq_utf8 = NULL;
int r, k;
assert(q);
if (q->n_cname_redirects >= CNAME_REDIRECTS_MAX)
return -ELOOP;
q->n_cname_redirects++;
r = dns_question_cname_redirect(q->question_idna, cname, &nq_idna);
if (r < 0)
return r;
if (r > 0)
log_debug("Following CNAME/DNAME %s %s %s.",
dns_question_first_name(q->question_idna),
glyph(GLYPH_ARROW_RIGHT),
dns_question_first_name(nq_idna));
k = dns_question_is_equal(q->question_idna, q->question_utf8);
if (k < 0)
return k;
if (k > 0) {
/* Same question? Shortcut new question generation */
nq_utf8 = dns_question_ref(nq_idna);
k = r;
} else {
k = dns_question_cname_redirect(q->question_utf8, cname, &nq_utf8);
if (k < 0)
return k;
if (k > 0)
log_debug("Following UTF8 CNAME/DNAME %s %s %s.",
dns_question_first_name(q->question_utf8),
glyph(GLYPH_ARROW_RIGHT),
dns_question_first_name(nq_utf8));
}
if (r == 0 && k == 0) /* No actual cname happened? */
return -ELOOP;
if (r == 0)
nq_idna = dns_question_ref(q->question_idna);
if (k == 0)
nq_utf8 = dns_question_ref(q->question_utf8);
if (q->answer_protocol == DNS_PROTOCOL_DNS)
/* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources
* cannot invade the local namespace. The opposite way we permit: local names may redirect to global
* ones. */
q->flags &= ~(SD_RESOLVED_LLMNR|SD_RESOLVED_MDNS); /* mask away the local protocols */
/* Turn off searching for the new name */
q->flags |= SD_RESOLVED_NO_SEARCH;
r = dns_query_collect_question(q, q->question_idna);
if (r < 0)
return r;
r = dns_query_collect_question(q, q->question_utf8);
if (r < 0)
return r;
/* Install the redirected question */
dns_question_unref(q->question_idna);
q->question_idna = TAKE_PTR(nq_idna);
dns_question_unref(q->question_utf8);
q->question_utf8 = TAKE_PTR(nq_utf8);
dns_query_unlink_candidates(q);
/* Note that we do *not* reset the answer here, because the answer we previously got might already
* include everything we need, let's check that first */
q->state = DNS_TRANSACTION_NULL;
return 0;
}
int dns_query_process_cname_one(DnsQuery *q) {
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL;
DnsQuestion *question;
DnsResourceRecord *rr;
bool full_match = true;
DnsResourceKey *k;
int r;
assert(q);
/* Processes a CNAME redirect if there's one. Returns one of three values:
*
* CNAME_QUERY_MATCH → direct RR match, caller should just use the RRs in this answer (and not
* bother with any CNAME/DNAME stuff)
*
* CNAME_QUERY_NOMATCH → no match at all, neither direct nor CNAME/DNAME, caller might decide to
* restart query or take things as NODATA reply.
*
* CNAME_QUERY_CNAME → no direct RR match, but a CNAME/DNAME match that we now followed for one step.
*
* The function might also return a failure, in particular -ELOOP if we encountered too many
* CNAMEs/DNAMEs in a chain or if following CNAMEs/DNAMEs was turned off.
*
* Note that this function doesn't actually restart the query. The caller can decide to do that in
* case of CNAME_QUERY_CNAME, though. */
if (!IN_SET(q->state, DNS_TRANSACTION_SUCCESS, DNS_TRANSACTION_NULL))
return DNS_QUERY_NOMATCH;
question = dns_query_question_for_protocol(q, q->answer_protocol);
/* Small reminder: our question will consist of one or more RR keys that match in name, but not in
* record type. Specifically, when we do an address lookup the question will typically consist of one
* A and one AAAA key lookup for the same domain name. When we get a response from a server we need
* to check if the answer answers all our questions to use it. Note that a response of CNAME/DNAME
* can answer both an A and the AAAA question for us, but an A/AAAA response only the relevant
* type.
*
* Hence we first check of the answers we collected are sufficient to answer all our questions
* directly. If one question wasn't answered we go on, waiting for more replies. However, if there's
* a CNAME/DNAME response we use it, and redirect to it, regardless if it was a response to the A or
* the AAAA query. */
DNS_QUESTION_FOREACH(k, question) {
bool match = false;
DNS_ANSWER_FOREACH(rr, q->answer) {
r = dns_resource_key_match_rr(k, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
if (r < 0)
return r;
if (r > 0) {
match = true; /* Yay, we found an RR that matches the key we are looking for */
break;
}
}
if (!match) {
/* Hmm. :-( there's no response for this key. This doesn't match. */
full_match = false;
break;
}
}
if (full_match)
return DNS_QUERY_MATCH; /* The answer can answer our question in full, no need to follow CNAMEs/DNAMEs */
/* Let's see if there is a CNAME/DNAME to match. This case is simpler: we accept the CNAME/DNAME that
* matches any of our questions. */
DNS_ANSWER_FOREACH(rr, q->answer) {
r = dns_question_matches_cname_or_dname(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
if (r < 0)
return r;
if (r > 0 && !cname)
cname = dns_resource_record_ref(rr);
}
if (!cname)
return DNS_QUERY_NOMATCH; /* No match and no CNAME/DNAME to follow */
if (q->flags & SD_RESOLVED_NO_CNAME)
return -ELOOP;
if (!FLAGS_SET(q->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
q->previous_redirect_unauthenticated = true;
if (!FLAGS_SET(q->answer_query_flags, SD_RESOLVED_CONFIDENTIAL))
q->previous_redirect_non_confidential = true;
if (!FLAGS_SET(q->answer_query_flags, SD_RESOLVED_SYNTHETIC))
q->previous_redirect_non_synthetic = true;
/* OK, let's actually follow the CNAME */
r = dns_query_cname_redirect(q, cname);
if (r < 0)
return r;
return DNS_QUERY_CNAME; /* Tell caller that we did a single CNAME/DNAME redirection step */
}
int dns_query_process_cname_many(DnsQuery *q) {
int r;
assert(q);
/* Follows CNAMEs through the current packet: as long as the current packet can fulfill our
* redirected CNAME queries we keep going, and restart the query once the current packet isn't good
* enough anymore. It's a wrapper around dns_query_process_cname_one() and returns the same values,
* but with extended semantics. Specifically:
*
* DNS_QUERY_MATCH → as above
*
* DNS_QUERY_CNAME → we ran into a CNAME/DNAME redirect that we could not answer from the current
* message, and thus restarted the query to resolve it.
*
* DNS_QUERY_NOMATCH → we reached the end of CNAME/DNAME chain, and there are no direct matches nor a
* CNAME/DNAME match. i.e. this is a NODATA case.
*
* Note that this function will restart the query for the caller if needed, and that's the case
* DNS_QUERY_CNAME is returned.
*/
r = dns_query_process_cname_one(q);
if (r != DNS_QUERY_CNAME)
return r; /* The first redirect is special: if it doesn't answer the question that's no
* reason to restart the query, we just accept this as a NODATA answer. */
for (;;) {
r = dns_query_process_cname_one(q);
if (r < 0 || r == DNS_QUERY_MATCH)
return r;
if (r == DNS_QUERY_NOMATCH) {
/* OK, so we followed one or more CNAME/DNAME RR but the existing packet can't answer
* this. Let's restart the query hence, with the new question. Why the different
* handling than the first chain element? Because if the server answers a direct
* question with an empty answer then this is a NODATA response. But if it responds
* with a CNAME chain that ultimately is incomplete (i.e. a non-empty but truncated
* CNAME chain) then we better follow up ourselves and ask for the rest of the
* chain. This is particular relevant since our cache will store CNAME/DNAME
* redirects that we learnt about for lookups of certain DNS types, but later on we
* can reuse this data even for other DNS types, but in that case need to follow up
* with the final lookup of the chain ourselves with the RR type we ourselves are
* interested in. */
r = dns_query_go(q);
if (r < 0)
return r;
return DNS_QUERY_CNAME;
}
/* So we found a CNAME that the existing packet already answers, again via a CNAME, let's
* continue going then. */
assert(r == DNS_QUERY_CNAME);
}
}
DnsQuestion* dns_query_question_for_protocol(DnsQuery *q, DnsProtocol protocol) {
assert(q);
if (q->question_bypass)
return q->question_bypass->question;
switch (protocol) {
case DNS_PROTOCOL_DNS:
return q->question_idna;
case DNS_PROTOCOL_MDNS:
case DNS_PROTOCOL_LLMNR:
return q->question_utf8;
default:
return NULL;
}
}
const char* dns_query_string(DnsQuery *q) {
const char *name;
int r;
/* Returns a somewhat useful human-readable lookup key string for this query */
if (q->question_bypass)
return dns_question_first_name(q->question_bypass->question);
if (q->request_address_string)
return q->request_address_string;
if (q->request_address_valid) {
r = in_addr_to_string(q->request_family, &q->request_address, &q->request_address_string);
if (r >= 0)
return q->request_address_string;
}
name = dns_question_first_name(q->question_utf8);
if (name)
return name;
return dns_question_first_name(q->question_idna);
}
bool dns_query_fully_authenticated(DnsQuery *q) {
assert(q);
return FLAGS_SET(q->answer_query_flags, SD_RESOLVED_AUTHENTICATED) && !q->previous_redirect_unauthenticated;
}
bool dns_query_fully_confidential(DnsQuery *q) {
assert(q);
return FLAGS_SET(q->answer_query_flags, SD_RESOLVED_CONFIDENTIAL) && !q->previous_redirect_non_confidential;
}
bool dns_query_fully_authoritative(DnsQuery *q) {
assert(q);
/* We are authoritative for everything synthetic (except if a previous CNAME/DNAME) wasn't
* synthetic. (Note: SD_RESOLVED_SYNTHETIC is reset on each CNAME/DNAME, hence the explicit check for
* previous synthetic DNAME/CNAME redirections.) */
if ((q->answer_query_flags & SD_RESOLVED_SYNTHETIC) && !q->previous_redirect_non_synthetic)
return true;
/* We are also authoritative for everything coming only from the trust anchor and the local
* zones. (Note: the SD_RESOLVED_FROM_xyz flags we merge on each redirect, hence no need to
* explicitly check previous redirects here.) */
return (q->answer_query_flags & SD_RESOLVED_FROM_MASK & ~(SD_RESOLVED_FROM_TRUST_ANCHOR | SD_RESOLVED_FROM_ZONE)) == 0;
}
int validate_and_mangle_query_flags(
Manager *manager,
uint64_t *flags,
const char *name,
uint64_t ok) {
assert(manager);
assert(flags);
/* Checks that the client supplied interface index and flags parameter actually are valid and make
* sense in our method call context. Specifically:
*
* 1. Checks that the interface index is either 0 (meaning *all* interfaces) or positive
*
* 2. Only the protocols flags and a bunch of NO_XYZ flags are set, at most. Plus additional flags
* specific to our method, passed in the "ok" parameter.
*
* 3. If zero protocol flags are specified it is automatically turned into *all* protocols. This way
* clients can simply pass 0 as flags and all will work as it should. They can also use this so
* that clients don't have to know all the protocols resolved implements, but can just specify 0
* to mean "all supported protocols".
*/
if (*flags & ~(SD_RESOLVED_PROTOCOLS_ALL|
SD_RESOLVED_NO_CNAME|
SD_RESOLVED_NO_VALIDATE|
SD_RESOLVED_NO_SYNTHESIZE|
SD_RESOLVED_NO_CACHE|
SD_RESOLVED_NO_ZONE|
SD_RESOLVED_NO_TRUST_ANCHOR|
SD_RESOLVED_NO_NETWORK|
SD_RESOLVED_NO_STALE|
SD_RESOLVED_RELAX_SINGLE_LABEL|
ok))
return -EINVAL;
if ((*flags & SD_RESOLVED_PROTOCOLS_ALL) == 0) /* If no protocol is enabled, enable all */
*flags |= SD_RESOLVED_PROTOCOLS_ALL;
/* Imply SD_RESOLVED_NO_SEARCH if permitted and name is dot suffixed. */
if (name && FLAGS_SET(ok, SD_RESOLVED_NO_SEARCH) && dns_name_dot_suffixed(name) > 0)
*flags |= SD_RESOLVED_NO_SEARCH;
/* If both A and AAAA are refused, set SD_RESOLVED_NO_ADDRESS flag if it is allowed. */
if (set_contains(manager->refuse_record_types, INT_TO_PTR(DNS_TYPE_A)) &&
set_contains(manager->refuse_record_types, INT_TO_PTR(DNS_TYPE_AAAA)) &&
FLAGS_SET(ok, SD_RESOLVED_NO_ADDRESS))
*flags |= SD_RESOLVED_NO_ADDRESS;
/* Similarly, if TXT is refused, set SD_RESOLVED_NO_TXT flag if it is allowed. */
if (set_contains(manager->refuse_record_types, INT_TO_PTR(DNS_TYPE_TXT)) &&
FLAGS_SET(ok, SD_RESOLVED_NO_TXT))
*flags |= SD_RESOLVED_NO_TXT;
return 0;
}
uint64_t dns_query_reply_flags_make(DnsQuery *q) {
assert(q);
return SD_RESOLVED_FLAGS_MAKE(q->answer_protocol,
q->answer_family,
dns_query_fully_authenticated(q),
dns_query_fully_confidential(q)) |
(q->answer_query_flags & (SD_RESOLVED_FROM_MASK|SD_RESOLVED_SYNTHETIC));
}