1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "dns-domain.h"
6 #include "hostname-util.h"
7 #include "local-addresses.h"
8 #include "resolved-dns-query.h"
9 #include "resolved-dns-synthesize.h"
10 #include "resolved-etc-hosts.h"
11 #include "string-util.h"
14 #define QUERIES_MAX 2048
15 #define AUXILIARY_QUERIES_MAX 64
17 static int dns_query_candidate_new(DnsQueryCandidate
**ret
, DnsQuery
*q
, DnsScope
*s
) {
24 c
= new0(DnsQueryCandidate
, 1);
31 LIST_PREPEND(candidates_by_query
, q
->candidates
, c
);
32 LIST_PREPEND(candidates_by_scope
, s
->query_candidates
, c
);
38 static void dns_query_candidate_stop(DnsQueryCandidate
*c
) {
43 while ((t
= set_steal_first(c
->transactions
))) {
44 set_remove(t
->notify_query_candidates
, c
);
45 set_remove(t
->notify_query_candidates_done
, c
);
46 dns_transaction_gc(t
);
50 DnsQueryCandidate
* dns_query_candidate_free(DnsQueryCandidate
*c
) {
55 dns_query_candidate_stop(c
);
57 set_free(c
->transactions
);
58 dns_search_domain_unref(c
->search_domain
);
61 LIST_REMOVE(candidates_by_query
, c
->query
->candidates
, c
);
64 LIST_REMOVE(candidates_by_scope
, c
->scope
->query_candidates
, c
);
69 static int dns_query_candidate_next_search_domain(DnsQueryCandidate
*c
) {
70 DnsSearchDomain
*next
= NULL
;
74 if (c
->search_domain
&& c
->search_domain
->linked
)
75 next
= c
->search_domain
->domains_next
;
77 next
= dns_scope_get_search_domains(c
->scope
);
80 if (!next
) /* We hit the end of the list */
83 if (!next
->route_only
)
86 /* Skip over route-only domains */
87 next
= next
->domains_next
;
90 dns_search_domain_unref(c
->search_domain
);
91 c
->search_domain
= dns_search_domain_ref(next
);
96 static int dns_query_candidate_add_transaction(DnsQueryCandidate
*c
, DnsResourceKey
*key
) {
103 t
= dns_scope_find_transaction(c
->scope
, key
, true);
105 r
= dns_transaction_new(&t
, c
->scope
, key
);
109 if (set_contains(c
->transactions
, t
))
113 r
= set_ensure_allocated(&c
->transactions
, NULL
);
117 r
= set_ensure_allocated(&t
->notify_query_candidates
, NULL
);
121 r
= set_ensure_allocated(&t
->notify_query_candidates_done
, NULL
);
125 r
= set_put(t
->notify_query_candidates
, c
);
129 r
= set_put(c
->transactions
, t
);
131 (void) set_remove(t
->notify_query_candidates
, c
);
135 t
->clamp_ttl
= c
->query
->clamp_ttl
;
139 dns_transaction_gc(t
);
143 static int dns_query_candidate_go(DnsQueryCandidate
*c
) {
151 /* Start the transactions that are not started yet */
152 SET_FOREACH(t
, c
->transactions
, i
) {
153 if (t
->state
!= DNS_TRANSACTION_NULL
)
156 r
= dns_transaction_go(t
);
163 /* If there was nothing to start, then let's proceed immediately */
165 dns_query_candidate_notify(c
);
170 static DnsTransactionState
dns_query_candidate_state(DnsQueryCandidate
*c
) {
171 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
177 if (c
->error_code
!= 0)
178 return DNS_TRANSACTION_ERRNO
;
180 SET_FOREACH(t
, c
->transactions
, i
) {
184 case DNS_TRANSACTION_NULL
:
185 /* If there's a NULL transaction pending, then
186 * this means not all transactions where
187 * started yet, and we were called from within
188 * the stackframe that is supposed to start
189 * remaining transactions. In this case,
190 * simply claim the candidate is pending. */
192 case DNS_TRANSACTION_PENDING
:
193 case DNS_TRANSACTION_VALIDATING
:
194 /* If there's one transaction currently in
195 * VALIDATING state, then this means there's
196 * also one in PENDING state, hence we can
197 * return PENDING immediately. */
198 return DNS_TRANSACTION_PENDING
;
200 case DNS_TRANSACTION_SUCCESS
:
205 if (state
!= DNS_TRANSACTION_SUCCESS
)
215 static int dns_query_candidate_setup_transactions(DnsQueryCandidate
*c
) {
216 DnsQuestion
*question
;
222 dns_query_candidate_stop(c
);
224 question
= dns_query_question_for_protocol(c
->query
, c
->scope
->protocol
);
226 /* Create one transaction per question key */
227 DNS_QUESTION_FOREACH(key
, question
) {
228 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*new_key
= NULL
;
229 DnsResourceKey
*qkey
;
231 if (c
->search_domain
) {
232 r
= dns_resource_key_new_append_suffix(&new_key
, key
, c
->search_domain
->name
);
240 if (!dns_scope_good_key(c
->scope
, qkey
))
243 r
= dns_query_candidate_add_transaction(c
, qkey
);
253 dns_query_candidate_stop(c
);
257 void dns_query_candidate_notify(DnsQueryCandidate
*c
) {
258 DnsTransactionState state
;
263 state
= dns_query_candidate_state(c
);
265 if (DNS_TRANSACTION_IS_LIVE(state
))
268 if (state
!= DNS_TRANSACTION_SUCCESS
&& c
->search_domain
) {
270 r
= dns_query_candidate_next_search_domain(c
);
275 /* OK, there's another search domain to try, let's do so. */
277 r
= dns_query_candidate_setup_transactions(c
);
282 /* New transactions where queued. Start them and wait */
284 r
= dns_query_candidate_go(c
);
294 dns_query_ready(c
->query
);
298 log_warning_errno(r
, "Failed to follow search domains: %m");
300 dns_query_ready(c
->query
);
303 static void dns_query_stop(DnsQuery
*q
) {
304 DnsQueryCandidate
*c
;
308 q
->timeout_event_source
= sd_event_source_unref(q
->timeout_event_source
);
310 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
)
311 dns_query_candidate_stop(c
);
314 static void dns_query_free_candidates(DnsQuery
*q
) {
317 while (q
->candidates
)
318 dns_query_candidate_free(q
->candidates
);
321 static void dns_query_reset_answer(DnsQuery
*q
) {
324 q
->answer
= dns_answer_unref(q
->answer
);
326 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
328 q
->answer_authenticated
= false;
329 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
330 q
->answer_family
= AF_UNSPEC
;
331 q
->answer_search_domain
= dns_search_domain_unref(q
->answer_search_domain
);
334 DnsQuery
*dns_query_free(DnsQuery
*q
) {
338 while (q
->auxiliary_queries
)
339 dns_query_free(q
->auxiliary_queries
);
341 if (q
->auxiliary_for
) {
342 assert(q
->auxiliary_for
->n_auxiliary_queries
> 0);
343 q
->auxiliary_for
->n_auxiliary_queries
--;
344 LIST_REMOVE(auxiliary_queries
, q
->auxiliary_for
->auxiliary_queries
, q
);
347 dns_query_free_candidates(q
);
349 dns_question_unref(q
->question_idna
);
350 dns_question_unref(q
->question_utf8
);
352 dns_query_reset_answer(q
);
354 sd_bus_message_unref(q
->request
);
355 sd_bus_track_unref(q
->bus_track
);
357 dns_packet_unref(q
->request_dns_packet
);
358 dns_packet_unref(q
->reply_dns_packet
);
360 if (q
->request_dns_stream
) {
361 /* Detach the stream from our query, in case something else keeps a reference to it. */
362 (void) set_remove(q
->request_dns_stream
->queries
, q
);
363 q
->request_dns_stream
= dns_stream_unref(q
->request_dns_stream
);
366 free(q
->request_address_string
);
369 LIST_REMOVE(queries
, q
->manager
->dns_queries
, q
);
370 q
->manager
->n_dns_queries
--;
379 DnsQuestion
*question_utf8
,
380 DnsQuestion
*question_idna
,
384 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
388 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
392 if (dns_question_size(question_utf8
) > 0) {
393 r
= dns_question_is_valid_for_query(question_utf8
);
402 /* If the IDNA and UTF8 questions are the same, merge their references */
403 r
= dns_question_is_equal(question_idna
, question_utf8
);
407 question_idna
= question_utf8
;
409 if (dns_question_size(question_idna
) > 0) {
410 r
= dns_question_is_valid_for_query(question_idna
);
420 if (!good
) /* don't allow empty queries */
423 if (m
->n_dns_queries
>= QUERIES_MAX
)
426 q
= new0(DnsQuery
, 1);
430 q
->question_utf8
= dns_question_ref(question_utf8
);
431 q
->question_idna
= dns_question_ref(question_idna
);
432 q
->ifindex
= ifindex
;
434 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
435 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
436 q
->answer_family
= AF_UNSPEC
;
438 /* First dump UTF8 question */
439 DNS_QUESTION_FOREACH(key
, question_utf8
)
440 log_debug("Looking up RR for %s.",
441 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
443 /* And then dump the IDNA question, but only what hasn't been dumped already through the UTF8 question. */
444 DNS_QUESTION_FOREACH(key
, question_idna
) {
445 r
= dns_question_contains(question_utf8
, key
);
451 log_debug("Looking up IDNA RR for %s.",
452 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
455 LIST_PREPEND(queries
, m
->dns_queries
, q
);
466 int dns_query_make_auxiliary(DnsQuery
*q
, DnsQuery
*auxiliary_for
) {
468 assert(auxiliary_for
);
470 /* Ensure that the query is not auxiliary yet, and
471 * nothing else is auxiliary to it either */
472 assert(!q
->auxiliary_for
);
473 assert(!q
->auxiliary_queries
);
475 /* Ensure that the unit we shall be made auxiliary for isn't
476 * auxiliary itself */
477 assert(!auxiliary_for
->auxiliary_for
);
479 if (auxiliary_for
->n_auxiliary_queries
>= AUXILIARY_QUERIES_MAX
)
482 LIST_PREPEND(auxiliary_queries
, auxiliary_for
->auxiliary_queries
, q
);
483 q
->auxiliary_for
= auxiliary_for
;
485 auxiliary_for
->n_auxiliary_queries
++;
489 static void dns_query_complete(DnsQuery
*q
, DnsTransactionState state
) {
491 assert(!DNS_TRANSACTION_IS_LIVE(state
));
492 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
494 /* Note that this call might invalidate the query. Callers
495 * should hence not attempt to access the query or transaction
496 * after calling this function. */
505 static int on_query_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
506 DnsQuery
*q
= userdata
;
511 dns_query_complete(q
, DNS_TRANSACTION_TIMEOUT
);
515 static int dns_query_add_candidate(DnsQuery
*q
, DnsScope
*s
) {
516 DnsQueryCandidate
*c
;
522 r
= dns_query_candidate_new(&c
, q
, s
);
526 /* If this a single-label domain on DNS, we might append a suitable search domain first. */
527 if ((q
->flags
& SD_RESOLVED_NO_SEARCH
) == 0 &&
528 dns_scope_name_needs_search_domain(s
, dns_question_first_name(q
->question_idna
))) {
529 /* OK, we need a search domain now. Let's find one for this scope */
531 r
= dns_query_candidate_next_search_domain(c
);
532 if (r
<= 0) /* if there's no search domain, then we won't add any transaction. */
536 r
= dns_query_candidate_setup_transactions(c
);
543 dns_query_candidate_free(c
);
547 static int dns_query_synthesize_reply(DnsQuery
*q
, DnsTransactionState
*state
) {
548 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
554 /* Tries to synthesize localhost RR replies (and others) where appropriate. Note that this is done *after* the
555 * the normal lookup finished. The data from the network hence takes precedence over the data we
556 * synthesize. (But note that many scopes refuse to resolve certain domain names) */
559 DNS_TRANSACTION_RCODE_FAILURE
,
560 DNS_TRANSACTION_NO_SERVERS
,
561 DNS_TRANSACTION_TIMEOUT
,
562 DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
,
563 DNS_TRANSACTION_NETWORK_DOWN
,
564 DNS_TRANSACTION_NOT_FOUND
))
567 r
= dns_synthesize_answer(
573 /* If we get ENXIO this tells us to generate NXDOMAIN unconditionally. */
575 dns_query_reset_answer(q
);
576 q
->answer_rcode
= DNS_RCODE_NXDOMAIN
;
577 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
578 q
->answer_family
= dns_synthesize_family(q
->flags
);
579 q
->answer_authenticated
= true;
580 *state
= DNS_TRANSACTION_RCODE_FAILURE
;
587 dns_query_reset_answer(q
);
589 q
->answer
= TAKE_PTR(answer
);
590 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
591 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
592 q
->answer_family
= dns_synthesize_family(q
->flags
);
593 q
->answer_authenticated
= true;
595 *state
= DNS_TRANSACTION_SUCCESS
;
600 static int dns_query_try_etc_hosts(DnsQuery
*q
) {
601 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
606 /* Looks in /etc/hosts for matching entries. Note that this is done *before* the normal lookup is done. The
607 * data from /etc/hosts hence takes precedence over the network. */
609 r
= manager_etc_hosts_lookup(
616 dns_query_reset_answer(q
);
618 q
->answer
= TAKE_PTR(answer
);
619 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
620 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
621 q
->answer_family
= dns_synthesize_family(q
->flags
);
622 q
->answer_authenticated
= true;
627 int dns_query_go(DnsQuery
*q
) {
628 DnsScopeMatch found
= DNS_SCOPE_NO
;
629 DnsScope
*s
, *first
= NULL
;
630 DnsQueryCandidate
*c
;
635 if (q
->state
!= DNS_TRANSACTION_NULL
)
638 r
= dns_query_try_etc_hosts(q
);
642 dns_query_complete(q
, DNS_TRANSACTION_SUCCESS
);
646 LIST_FOREACH(scopes
, s
, q
->manager
->dns_scopes
) {
650 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
654 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
656 log_debug("Couldn't check if '%s' matches against scope, ignoring.", name
);
660 if (match
> found
) { /* Does this match better? If so, remember how well it matched, and the first one
661 * that matches this well */
667 if (found
== DNS_SCOPE_NO
) {
668 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
670 r
= dns_query_synthesize_reply(q
, &state
);
674 dns_query_complete(q
, state
);
678 r
= dns_query_add_candidate(q
, first
);
682 LIST_FOREACH(scopes
, s
, first
->scopes_next
) {
686 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
690 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
692 log_debug("Couldn't check if '%s' matches against scope, ignoring.", name
);
699 r
= dns_query_add_candidate(q
, s
);
704 dns_query_reset_answer(q
);
706 r
= sd_event_add_time(
708 &q
->timeout_event_source
,
709 clock_boottime_or_monotonic(),
710 now(clock_boottime_or_monotonic()) + SD_RESOLVED_QUERY_TIMEOUT_USEC
,
711 0, on_query_timeout
, q
);
715 (void) sd_event_source_set_description(q
->timeout_event_source
, "query-timeout");
717 q
->state
= DNS_TRANSACTION_PENDING
;
720 /* Start the transactions */
721 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
722 r
= dns_query_candidate_go(c
);
739 static void dns_query_accept(DnsQuery
*q
, DnsQueryCandidate
*c
) {
740 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
741 bool has_authenticated
= false, has_non_authenticated
= false;
742 DnssecResult dnssec_result_authenticated
= _DNSSEC_RESULT_INVALID
, dnssec_result_non_authenticated
= _DNSSEC_RESULT_INVALID
;
750 r
= dns_query_synthesize_reply(q
, &state
);
754 dns_query_complete(q
, state
);
758 if (c
->error_code
!= 0) {
759 /* If the candidate had an error condition of its own, start with that. */
760 state
= DNS_TRANSACTION_ERRNO
;
761 q
->answer
= dns_answer_unref(q
->answer
);
763 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
764 q
->answer_authenticated
= false;
765 q
->answer_errno
= c
->error_code
;
768 SET_FOREACH(t
, c
->transactions
, i
) {
772 case DNS_TRANSACTION_SUCCESS
: {
773 /* We found a successfully reply, merge it into the answer */
774 r
= dns_answer_extend(&q
->answer
, t
->answer
);
778 q
->answer_rcode
= t
->answer_rcode
;
781 if (t
->answer_authenticated
) {
782 has_authenticated
= true;
783 dnssec_result_authenticated
= t
->answer_dnssec_result
;
785 has_non_authenticated
= true;
786 dnssec_result_non_authenticated
= t
->answer_dnssec_result
;
789 state
= DNS_TRANSACTION_SUCCESS
;
793 case DNS_TRANSACTION_NULL
:
794 case DNS_TRANSACTION_PENDING
:
795 case DNS_TRANSACTION_VALIDATING
:
796 case DNS_TRANSACTION_ABORTED
:
797 /* Ignore transactions that didn't complete */
801 /* Any kind of failure? Store the data away, if there's nothing stored yet. */
802 if (state
== DNS_TRANSACTION_SUCCESS
)
805 /* If there's already an authenticated negative reply stored, then prefer that over any unauthenticated one */
806 if (q
->answer_authenticated
&& !t
->answer_authenticated
)
809 q
->answer
= dns_answer_unref(q
->answer
);
810 q
->answer_rcode
= t
->answer_rcode
;
811 q
->answer_dnssec_result
= t
->answer_dnssec_result
;
812 q
->answer_authenticated
= t
->answer_authenticated
;
813 q
->answer_errno
= t
->answer_errno
;
820 if (state
== DNS_TRANSACTION_SUCCESS
) {
821 q
->answer_authenticated
= has_authenticated
&& !has_non_authenticated
;
822 q
->answer_dnssec_result
= q
->answer_authenticated
? dnssec_result_authenticated
: dnssec_result_non_authenticated
;
825 q
->answer_protocol
= c
->scope
->protocol
;
826 q
->answer_family
= c
->scope
->family
;
828 dns_search_domain_unref(q
->answer_search_domain
);
829 q
->answer_search_domain
= dns_search_domain_ref(c
->search_domain
);
831 r
= dns_query_synthesize_reply(q
, &state
);
835 dns_query_complete(q
, state
);
839 q
->answer_errno
= -r
;
840 dns_query_complete(q
, DNS_TRANSACTION_ERRNO
);
843 void dns_query_ready(DnsQuery
*q
) {
845 DnsQueryCandidate
*bad
= NULL
, *c
;
846 bool pending
= false;
849 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
851 /* Note that this call might invalidate the query. Callers
852 * should hence not attempt to access the query or transaction
853 * after calling this function, unless the block_ready
854 * counter was explicitly bumped before doing so. */
856 if (q
->block_ready
> 0)
859 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
860 DnsTransactionState state
;
862 state
= dns_query_candidate_state(c
);
865 case DNS_TRANSACTION_SUCCESS
:
866 /* One of the candidates is successful,
867 * let's use it, and copy its data out */
868 dns_query_accept(q
, c
);
871 case DNS_TRANSACTION_NULL
:
872 case DNS_TRANSACTION_PENDING
:
873 case DNS_TRANSACTION_VALIDATING
:
874 /* One of the candidates is still going on,
875 * let's maybe wait for it */
880 /* Any kind of failure */
889 dns_query_accept(q
, bad
);
892 static int dns_query_cname_redirect(DnsQuery
*q
, const DnsResourceRecord
*cname
) {
893 _cleanup_(dns_question_unrefp
) DnsQuestion
*nq_idna
= NULL
, *nq_utf8
= NULL
;
898 q
->n_cname_redirects
++;
899 if (q
->n_cname_redirects
> CNAME_MAX
)
902 r
= dns_question_cname_redirect(q
->question_idna
, cname
, &nq_idna
);
906 log_debug("Following CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_idna
), dns_question_first_name(nq_idna
));
908 k
= dns_question_is_equal(q
->question_idna
, q
->question_utf8
);
912 /* Same question? Shortcut new question generation */
913 nq_utf8
= dns_question_ref(nq_idna
);
916 k
= dns_question_cname_redirect(q
->question_utf8
, cname
, &nq_utf8
);
920 log_debug("Following UTF8 CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_utf8
), dns_question_first_name(nq_utf8
));
923 if (r
== 0 && k
== 0) /* No actual cname happened? */
926 if (q
->answer_protocol
== DNS_PROTOCOL_DNS
) {
927 /* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources
928 * cannot invade the local namespace. The opposite way we permit: local names may redirect to global
931 q
->flags
&= ~(SD_RESOLVED_LLMNR
|SD_RESOLVED_MDNS
); /* mask away the local protocols */
934 /* Turn off searching for the new name */
935 q
->flags
|= SD_RESOLVED_NO_SEARCH
;
937 dns_question_unref(q
->question_idna
);
938 q
->question_idna
= TAKE_PTR(nq_idna
);
940 dns_question_unref(q
->question_utf8
);
941 q
->question_utf8
= TAKE_PTR(nq_utf8
);
943 dns_query_free_candidates(q
);
944 dns_query_reset_answer(q
);
946 q
->state
= DNS_TRANSACTION_NULL
;
951 int dns_query_process_cname(DnsQuery
*q
) {
952 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*cname
= NULL
;
953 DnsQuestion
*question
;
954 DnsResourceRecord
*rr
;
959 if (!IN_SET(q
->state
, DNS_TRANSACTION_SUCCESS
, DNS_TRANSACTION_NULL
))
960 return DNS_QUERY_NOMATCH
;
962 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
964 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
965 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
969 return DNS_QUERY_MATCH
; /* The answer matches directly, no need to follow cnames */
971 r
= dns_question_matches_cname_or_dname(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
975 cname
= dns_resource_record_ref(rr
);
979 return DNS_QUERY_NOMATCH
; /* No match and no cname to follow */
981 if (q
->flags
& SD_RESOLVED_NO_CNAME
)
984 if (!q
->answer_authenticated
)
985 q
->previous_redirect_unauthenticated
= true;
987 /* OK, let's actually follow the CNAME */
988 r
= dns_query_cname_redirect(q
, cname
);
992 /* Let's see if the answer can already answer the new
993 * redirected question */
994 r
= dns_query_process_cname(q
);
995 if (r
!= DNS_QUERY_NOMATCH
)
998 /* OK, it cannot, let's begin with the new query */
1003 return DNS_QUERY_RESTARTED
; /* We restarted the query for a new cname */
1006 static int on_bus_track(sd_bus_track
*t
, void *userdata
) {
1007 DnsQuery
*q
= userdata
;
1012 log_debug("Client of active query vanished, aborting query.");
1013 dns_query_complete(q
, DNS_TRANSACTION_ABORTED
);
1017 int dns_query_bus_track(DnsQuery
*q
, sd_bus_message
*m
) {
1023 if (!q
->bus_track
) {
1024 r
= sd_bus_track_new(sd_bus_message_get_bus(m
), &q
->bus_track
, on_bus_track
, q
);
1029 r
= sd_bus_track_add_sender(q
->bus_track
, m
);
1036 DnsQuestion
* dns_query_question_for_protocol(DnsQuery
*q
, DnsProtocol protocol
) {
1041 case DNS_PROTOCOL_DNS
:
1042 return q
->question_idna
;
1044 case DNS_PROTOCOL_MDNS
:
1045 case DNS_PROTOCOL_LLMNR
:
1046 return q
->question_utf8
;
1053 const char *dns_query_string(DnsQuery
*q
) {
1057 /* Returns a somewhat useful human-readable lookup key string for this query */
1059 if (q
->request_address_string
)
1060 return q
->request_address_string
;
1062 if (q
->request_address_valid
) {
1063 r
= in_addr_to_string(q
->request_family
, &q
->request_address
, &q
->request_address_string
);
1065 return q
->request_address_string
;
1068 name
= dns_question_first_name(q
->question_utf8
);
1072 return dns_question_first_name(q
->question_idna
);
1075 bool dns_query_fully_authenticated(DnsQuery
*q
) {
1078 return q
->answer_authenticated
&& !q
->previous_redirect_unauthenticated
;