]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-query.c
Merge pull request #18777 from yuwata/network-set-ifname-to-engines
[thirdparty/systemd.git] / src / resolve / resolved-dns-query.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "dns-domain.h"
5 #include "dns-type.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"
12
13 #define QUERIES_MAX 2048
14 #define AUXILIARY_QUERIES_MAX 64
15
16 static int dns_query_candidate_new(DnsQueryCandidate **ret, DnsQuery *q, DnsScope *s) {
17 DnsQueryCandidate *c;
18
19 assert(ret);
20 assert(q);
21 assert(s);
22
23 c = new(DnsQueryCandidate, 1);
24 if (!c)
25 return -ENOMEM;
26
27 *c = (DnsQueryCandidate) {
28 .n_ref = 1,
29 .query = q,
30 .scope = s,
31 };
32
33 LIST_PREPEND(candidates_by_query, q->candidates, c);
34 LIST_PREPEND(candidates_by_scope, s->query_candidates, c);
35
36 *ret = c;
37 return 0;
38 }
39
40 static void dns_query_candidate_stop(DnsQueryCandidate *c) {
41 DnsTransaction *t;
42
43 assert(c);
44
45 while ((t = set_steal_first(c->transactions))) {
46 set_remove(t->notify_query_candidates, c);
47 set_remove(t->notify_query_candidates_done, c);
48 dns_transaction_gc(t);
49 }
50 }
51
52 static DnsQueryCandidate* dns_query_candidate_free(DnsQueryCandidate *c) {
53 if (!c)
54 return NULL;
55
56 dns_query_candidate_stop(c);
57
58 set_free(c->transactions);
59 dns_search_domain_unref(c->search_domain);
60
61 if (c->query)
62 LIST_REMOVE(candidates_by_query, c->query->candidates, c);
63
64 if (c->scope)
65 LIST_REMOVE(candidates_by_scope, c->scope->query_candidates, c);
66
67 return mfree(c);
68 }
69
70 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(DnsQueryCandidate, dns_query_candidate, dns_query_candidate_free);
71
72 static int dns_query_candidate_next_search_domain(DnsQueryCandidate *c) {
73 DnsSearchDomain *next;
74
75 assert(c);
76
77 if (c->search_domain && c->search_domain->linked)
78 next = c->search_domain->domains_next;
79 else
80 next = dns_scope_get_search_domains(c->scope);
81
82 for (;;) {
83 if (!next) /* We hit the end of the list */
84 return 0;
85
86 if (!next->route_only)
87 break;
88
89 /* Skip over route-only domains */
90 next = next->domains_next;
91 }
92
93 dns_search_domain_unref(c->search_domain);
94 c->search_domain = dns_search_domain_ref(next);
95
96 return 1;
97 }
98
99 static int dns_query_candidate_add_transaction(
100 DnsQueryCandidate *c,
101 DnsResourceKey *key,
102 DnsPacket *bypass) {
103
104 _cleanup_(dns_transaction_gcp) DnsTransaction *t = NULL;
105 int r;
106
107 assert(c);
108
109 if (key) {
110 /* Regular lookup with a resource key */
111 assert(!bypass);
112
113 t = dns_scope_find_transaction(c->scope, key, c->query->flags);
114 if (!t) {
115 r = dns_transaction_new(&t, c->scope, key, NULL, c->query->flags);
116 if (r < 0)
117 return r;
118 } else if (set_contains(c->transactions, t))
119 return 0;
120 } else {
121 /* "Bypass" lookup with a query packet */
122 assert(bypass);
123
124 r = dns_transaction_new(&t, c->scope, NULL, bypass, c->query->flags);
125 if (r < 0)
126 return r;
127 }
128
129 r = set_ensure_allocated(&t->notify_query_candidates_done, NULL);
130 if (r < 0)
131 return r;
132
133 r = set_ensure_put(&t->notify_query_candidates, NULL, c);
134 if (r < 0)
135 return r;
136
137 r = set_ensure_put(&c->transactions, NULL, t);
138 if (r < 0) {
139 (void) set_remove(t->notify_query_candidates, c);
140 return r;
141 }
142
143 TAKE_PTR(t);
144 return 1;
145 }
146
147 static int dns_query_candidate_go(DnsQueryCandidate *c) {
148 _cleanup_(dns_query_candidate_unrefp) DnsQueryCandidate *keep_c = NULL;
149 DnsTransaction *t;
150 int r;
151 unsigned n = 0;
152
153 assert(c);
154
155 /* Let's keep a reference to the query while we're operating */
156 keep_c = dns_query_candidate_ref(c);
157
158 /* Start the transactions that are not started yet */
159 SET_FOREACH(t, c->transactions) {
160 if (t->state != DNS_TRANSACTION_NULL)
161 continue;
162
163 r = dns_transaction_go(t);
164 if (r < 0)
165 return r;
166
167 n++;
168 }
169
170 /* If there was nothing to start, then let's proceed immediately */
171 if (n == 0)
172 dns_query_candidate_notify(c);
173
174 return 0;
175 }
176
177 static DnsTransactionState dns_query_candidate_state(DnsQueryCandidate *c) {
178 DnsTransactionState state = DNS_TRANSACTION_NO_SERVERS;
179 DnsTransaction *t;
180
181 assert(c);
182
183 if (c->error_code != 0)
184 return DNS_TRANSACTION_ERRNO;
185
186 SET_FOREACH(t, c->transactions) {
187
188 switch (t->state) {
189
190 case DNS_TRANSACTION_NULL:
191 /* If there's a NULL transaction pending, then
192 * this means not all transactions where
193 * started yet, and we were called from within
194 * the stackframe that is supposed to start
195 * remaining transactions. In this case,
196 * simply claim the candidate is pending. */
197
198 case DNS_TRANSACTION_PENDING:
199 case DNS_TRANSACTION_VALIDATING:
200 /* If there's one transaction currently in
201 * VALIDATING state, then this means there's
202 * also one in PENDING state, hence we can
203 * return PENDING immediately. */
204 return DNS_TRANSACTION_PENDING;
205
206 case DNS_TRANSACTION_SUCCESS:
207 state = t->state;
208 break;
209
210 default:
211 if (state != DNS_TRANSACTION_SUCCESS)
212 state = t->state;
213
214 break;
215 }
216 }
217
218 return state;
219 }
220
221 static int dns_query_candidate_setup_transactions(DnsQueryCandidate *c) {
222 DnsQuestion *question;
223 DnsResourceKey *key;
224 int n = 0, r;
225
226 assert(c);
227
228 dns_query_candidate_stop(c);
229
230 if (c->query->question_bypass) {
231 /* If this is a bypass query, then pass the original query packet along to the transaction */
232
233 assert(dns_question_size(c->query->question_bypass->question) == 1);
234
235 if (!dns_scope_good_key(c->scope, c->query->question_bypass->question->keys[0]))
236 return 0;
237
238 r = dns_query_candidate_add_transaction(c, NULL, c->query->question_bypass);
239 if (r < 0)
240 goto fail;
241
242 return 1;
243 }
244
245 question = dns_query_question_for_protocol(c->query, c->scope->protocol);
246
247 /* Create one transaction per question key */
248 DNS_QUESTION_FOREACH(key, question) {
249 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *new_key = NULL;
250 DnsResourceKey *qkey;
251
252 if (c->search_domain) {
253 r = dns_resource_key_new_append_suffix(&new_key, key, c->search_domain->name);
254 if (r < 0)
255 goto fail;
256
257 qkey = new_key;
258 } else
259 qkey = key;
260
261 if (!dns_scope_good_key(c->scope, qkey))
262 continue;
263
264 r = dns_query_candidate_add_transaction(c, qkey, NULL);
265 if (r < 0)
266 goto fail;
267
268 n++;
269 }
270
271 return n;
272
273 fail:
274 dns_query_candidate_stop(c);
275 return r;
276 }
277
278 void dns_query_candidate_notify(DnsQueryCandidate *c) {
279 DnsTransactionState state;
280 int r;
281
282 assert(c);
283
284 state = dns_query_candidate_state(c);
285
286 if (DNS_TRANSACTION_IS_LIVE(state))
287 return;
288
289 if (state != DNS_TRANSACTION_SUCCESS && c->search_domain) {
290
291 r = dns_query_candidate_next_search_domain(c);
292 if (r < 0)
293 goto fail;
294
295 if (r > 0) {
296 /* OK, there's another search domain to try, let's do so. */
297
298 r = dns_query_candidate_setup_transactions(c);
299 if (r < 0)
300 goto fail;
301
302 if (r > 0) {
303 /* New transactions where queued. Start them and wait */
304
305 r = dns_query_candidate_go(c);
306 if (r < 0)
307 goto fail;
308
309 return;
310 }
311 }
312
313 }
314
315 dns_query_ready(c->query);
316 return;
317
318 fail:
319 c->error_code = log_warning_errno(r, "Failed to follow search domains: %m");
320 dns_query_ready(c->query);
321 }
322
323 static void dns_query_stop(DnsQuery *q) {
324 DnsQueryCandidate *c;
325
326 assert(q);
327
328 q->timeout_event_source = sd_event_source_disable_unref(q->timeout_event_source);
329
330 LIST_FOREACH(candidates_by_query, c, q->candidates)
331 dns_query_candidate_stop(c);
332 }
333
334 static void dns_query_unref_candidates(DnsQuery *q) {
335 assert(q);
336
337 while (q->candidates)
338 dns_query_candidate_unref(q->candidates);
339 }
340
341 static void dns_query_reset_answer(DnsQuery *q) {
342 assert(q);
343
344 q->answer = dns_answer_unref(q->answer);
345 q->answer_rcode = 0;
346 q->answer_dnssec_result = _DNSSEC_RESULT_INVALID;
347 q->answer_errno = 0;
348 q->answer_query_flags = 0;
349 q->answer_protocol = _DNS_PROTOCOL_INVALID;
350 q->answer_family = AF_UNSPEC;
351 q->answer_search_domain = dns_search_domain_unref(q->answer_search_domain);
352 q->answer_full_packet = dns_packet_unref(q->answer_full_packet);
353 }
354
355 DnsQuery *dns_query_free(DnsQuery *q) {
356 if (!q)
357 return NULL;
358
359 while (q->auxiliary_queries)
360 dns_query_free(q->auxiliary_queries);
361
362 if (q->auxiliary_for) {
363 assert(q->auxiliary_for->n_auxiliary_queries > 0);
364 q->auxiliary_for->n_auxiliary_queries--;
365 LIST_REMOVE(auxiliary_queries, q->auxiliary_for->auxiliary_queries, q);
366 }
367
368 dns_query_unref_candidates(q);
369
370 dns_question_unref(q->question_idna);
371 dns_question_unref(q->question_utf8);
372 dns_packet_unref(q->question_bypass);
373
374 dns_query_reset_answer(q);
375
376 sd_bus_message_unref(q->bus_request);
377 sd_bus_track_unref(q->bus_track);
378
379 if (q->varlink_request) {
380 varlink_set_userdata(q->varlink_request, NULL);
381 varlink_unref(q->varlink_request);
382 }
383
384 if (q->request_packet)
385 hashmap_remove_value(q->stub_listener_extra ?
386 q->stub_listener_extra->queries_by_packet :
387 q->manager->stub_queries_by_packet,
388 q->request_packet,
389 q);
390
391 dns_packet_unref(q->request_packet);
392 dns_answer_unref(q->reply_answer);
393 dns_answer_unref(q->reply_authoritative);
394 dns_answer_unref(q->reply_additional);
395
396 if (q->request_stream) {
397 /* Detach the stream from our query, in case something else keeps a reference to it. */
398 (void) set_remove(q->request_stream->queries, q);
399 q->request_stream = dns_stream_unref(q->request_stream);
400 }
401
402 free(q->request_address_string);
403
404 if (q->manager) {
405 LIST_REMOVE(queries, q->manager->dns_queries, q);
406 q->manager->n_dns_queries--;
407 }
408
409 return mfree(q);
410 }
411
412 int dns_query_new(
413 Manager *m,
414 DnsQuery **ret,
415 DnsQuestion *question_utf8,
416 DnsQuestion *question_idna,
417 DnsPacket *question_bypass,
418 int ifindex,
419 uint64_t flags) {
420
421 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
422 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
423 DnsResourceKey *key;
424 int r;
425
426 assert(m);
427
428 if (question_bypass) {
429 /* It's either a "bypass" query, or a regular one, but can't be both. */
430 if (question_utf8 || question_idna)
431 return -EINVAL;
432
433 } else {
434 bool good = false;
435
436 /* This (primarily) checks two things:
437 *
438 * 1. That the question is not empty
439 * 2. That all RR keys in the question objects are for the same domain
440 *
441 * Or in other words, a single DnsQuery object may be used to look up A+AAAA combination for
442 * the same domain name, or SRV+TXT (for DNS-SD services), but not for unrelated lookups. */
443
444 if (dns_question_size(question_utf8) > 0) {
445 r = dns_question_is_valid_for_query(question_utf8);
446 if (r < 0)
447 return r;
448 if (r == 0)
449 return -EINVAL;
450
451 good = true;
452 }
453
454 /* If the IDNA and UTF8 questions are the same, merge their references */
455 r = dns_question_is_equal(question_idna, question_utf8);
456 if (r < 0)
457 return r;
458 if (r > 0)
459 question_idna = question_utf8;
460 else {
461 if (dns_question_size(question_idna) > 0) {
462 r = dns_question_is_valid_for_query(question_idna);
463 if (r < 0)
464 return r;
465 if (r == 0)
466 return -EINVAL;
467
468 good = true;
469 }
470 }
471
472 if (!good) /* don't allow empty queries */
473 return -EINVAL;
474 }
475
476 if (m->n_dns_queries >= QUERIES_MAX)
477 return -EBUSY;
478
479 q = new(DnsQuery, 1);
480 if (!q)
481 return -ENOMEM;
482
483 *q = (DnsQuery) {
484 .question_utf8 = dns_question_ref(question_utf8),
485 .question_idna = dns_question_ref(question_idna),
486 .question_bypass = dns_packet_ref(question_bypass),
487 .ifindex = ifindex,
488 .flags = flags,
489 .answer_dnssec_result = _DNSSEC_RESULT_INVALID,
490 .answer_protocol = _DNS_PROTOCOL_INVALID,
491 .answer_family = AF_UNSPEC,
492 };
493
494 if (question_bypass) {
495 DNS_QUESTION_FOREACH(key, question_bypass->question)
496 log_debug("Looking up bypass packet for %s.",
497 dns_resource_key_to_string(key, key_str, sizeof key_str));
498 } else {
499 /* First dump UTF8 question */
500 DNS_QUESTION_FOREACH(key, question_utf8)
501 log_debug("Looking up RR for %s.",
502 dns_resource_key_to_string(key, key_str, sizeof key_str));
503
504 /* And then dump the IDNA question, but only what hasn't been dumped already through the UTF8 question. */
505 DNS_QUESTION_FOREACH(key, question_idna) {
506 r = dns_question_contains(question_utf8, key);
507 if (r < 0)
508 return r;
509 if (r > 0)
510 continue;
511
512 log_debug("Looking up IDNA RR for %s.",
513 dns_resource_key_to_string(key, key_str, sizeof key_str));
514 }
515 }
516
517 LIST_PREPEND(queries, m->dns_queries, q);
518 m->n_dns_queries++;
519 q->manager = m;
520
521 if (ret)
522 *ret = q;
523
524 TAKE_PTR(q);
525 return 0;
526 }
527
528 int dns_query_make_auxiliary(DnsQuery *q, DnsQuery *auxiliary_for) {
529 assert(q);
530 assert(auxiliary_for);
531
532 /* Ensure that the query is not auxiliary yet, and
533 * nothing else is auxiliary to it either */
534 assert(!q->auxiliary_for);
535 assert(!q->auxiliary_queries);
536
537 /* Ensure that the unit we shall be made auxiliary for isn't
538 * auxiliary itself */
539 assert(!auxiliary_for->auxiliary_for);
540
541 if (auxiliary_for->n_auxiliary_queries >= AUXILIARY_QUERIES_MAX)
542 return -EAGAIN;
543
544 LIST_PREPEND(auxiliary_queries, auxiliary_for->auxiliary_queries, q);
545 q->auxiliary_for = auxiliary_for;
546
547 auxiliary_for->n_auxiliary_queries++;
548 return 0;
549 }
550
551 void dns_query_complete(DnsQuery *q, DnsTransactionState state) {
552 assert(q);
553 assert(!DNS_TRANSACTION_IS_LIVE(state));
554 assert(DNS_TRANSACTION_IS_LIVE(q->state));
555
556 /* Note that this call might invalidate the query. Callers should hence not attempt to access the
557 * query or transaction after calling this function. */
558
559 q->state = state;
560
561 dns_query_stop(q);
562 if (q->complete)
563 q->complete(q);
564 }
565
566 static int on_query_timeout(sd_event_source *s, usec_t usec, void *userdata) {
567 DnsQuery *q = userdata;
568
569 assert(s);
570 assert(q);
571
572 dns_query_complete(q, DNS_TRANSACTION_TIMEOUT);
573 return 0;
574 }
575
576 static int dns_query_add_candidate(DnsQuery *q, DnsScope *s) {
577 _cleanup_(dns_query_candidate_unrefp) DnsQueryCandidate *c = NULL;
578 int r;
579
580 assert(q);
581 assert(s);
582
583 r = dns_query_candidate_new(&c, q, s);
584 if (r < 0)
585 return r;
586
587 /* If this a single-label domain on DNS, we might append a suitable search domain first. */
588 if (!FLAGS_SET(q->flags, SD_RESOLVED_NO_SEARCH) &&
589 dns_scope_name_wants_search_domain(s, dns_question_first_name(q->question_idna))) {
590 /* OK, we want a search domain now. Let's find one for this scope */
591
592 r = dns_query_candidate_next_search_domain(c);
593 if (r < 0)
594 return r;
595 }
596
597 r = dns_query_candidate_setup_transactions(c);
598 if (r < 0)
599 return r;
600
601 TAKE_PTR(c);
602 return 0;
603 }
604
605 static int dns_query_synthesize_reply(DnsQuery *q, DnsTransactionState *state) {
606 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
607 int r;
608
609 assert(q);
610 assert(state);
611
612 /* Tries to synthesize localhost RR replies (and others) where appropriate. Note that this is done *after* the
613 * the normal lookup finished. The data from the network hence takes precedence over the data we
614 * synthesize. (But note that many scopes refuse to resolve certain domain names) */
615
616 if (!IN_SET(*state,
617 DNS_TRANSACTION_RCODE_FAILURE,
618 DNS_TRANSACTION_NO_SERVERS,
619 DNS_TRANSACTION_TIMEOUT,
620 DNS_TRANSACTION_ATTEMPTS_MAX_REACHED,
621 DNS_TRANSACTION_NETWORK_DOWN,
622 DNS_TRANSACTION_NOT_FOUND))
623 return 0;
624
625 if (FLAGS_SET(q->flags, SD_RESOLVED_NO_SYNTHESIZE))
626 return 0;
627
628 r = dns_synthesize_answer(
629 q->manager,
630 q->question_bypass ? q->question_bypass->question : q->question_utf8,
631 q->ifindex,
632 &answer);
633 if (r == -ENXIO) {
634 /* If we get ENXIO this tells us to generate NXDOMAIN unconditionally. */
635
636 dns_query_reset_answer(q);
637 q->answer_rcode = DNS_RCODE_NXDOMAIN;
638 q->answer_protocol = dns_synthesize_protocol(q->flags);
639 q->answer_family = dns_synthesize_family(q->flags);
640 q->answer_query_flags = SD_RESOLVED_AUTHENTICATED|SD_RESOLVED_CONFIDENTIAL|SD_RESOLVED_SYNTHETIC;
641 *state = DNS_TRANSACTION_RCODE_FAILURE;
642
643 return 0;
644 }
645 if (r <= 0)
646 return r;
647
648 dns_query_reset_answer(q);
649
650 q->answer = TAKE_PTR(answer);
651 q->answer_rcode = DNS_RCODE_SUCCESS;
652 q->answer_protocol = dns_synthesize_protocol(q->flags);
653 q->answer_family = dns_synthesize_family(q->flags);
654 q->answer_query_flags = SD_RESOLVED_AUTHENTICATED|SD_RESOLVED_CONFIDENTIAL|SD_RESOLVED_SYNTHETIC;
655
656 *state = DNS_TRANSACTION_SUCCESS;
657
658 return 1;
659 }
660
661 static int dns_query_try_etc_hosts(DnsQuery *q) {
662 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
663 int r;
664
665 assert(q);
666
667 /* Looks in /etc/hosts for matching entries. Note that this is done *before* the normal lookup is
668 * done. The data from /etc/hosts hence takes precedence over the network. */
669
670 if (FLAGS_SET(q->flags, SD_RESOLVED_NO_SYNTHESIZE))
671 return 0;
672
673 r = manager_etc_hosts_lookup(
674 q->manager,
675 q->question_bypass ? q->question_bypass->question : q->question_utf8,
676 &answer);
677 if (r <= 0)
678 return r;
679
680 dns_query_reset_answer(q);
681
682 q->answer = TAKE_PTR(answer);
683 q->answer_rcode = DNS_RCODE_SUCCESS;
684 q->answer_protocol = dns_synthesize_protocol(q->flags);
685 q->answer_family = dns_synthesize_family(q->flags);
686 q->answer_query_flags = SD_RESOLVED_AUTHENTICATED|SD_RESOLVED_CONFIDENTIAL|SD_RESOLVED_SYNTHETIC;
687
688 return 1;
689 }
690
691 int dns_query_go(DnsQuery *q) {
692 DnsScopeMatch found = DNS_SCOPE_NO;
693 DnsScope *s, *first = NULL;
694 DnsQueryCandidate *c;
695 int r;
696
697 assert(q);
698
699 if (q->state != DNS_TRANSACTION_NULL)
700 return 0;
701
702 r = dns_query_try_etc_hosts(q);
703 if (r < 0)
704 return r;
705 if (r > 0) {
706 dns_query_complete(q, DNS_TRANSACTION_SUCCESS);
707 return 1;
708 }
709
710 LIST_FOREACH(scopes, s, q->manager->dns_scopes) {
711 DnsScopeMatch match;
712 const char *name;
713
714 name = dns_question_first_name(dns_query_question_for_protocol(q, s->protocol));
715 if (!name)
716 continue;
717
718 match = dns_scope_good_domain(s, q->ifindex, q->flags, name);
719 if (match < 0) {
720 log_debug("Couldn't check if '%s' matches against scope, ignoring.", name);
721 continue;
722 }
723
724 if (match > found) { /* Does this match better? If so, remember how well it matched, and the first one
725 * that matches this well */
726 found = match;
727 first = s;
728 }
729 }
730
731 if (found == DNS_SCOPE_NO) {
732 DnsTransactionState state = DNS_TRANSACTION_NO_SERVERS;
733
734 r = dns_query_synthesize_reply(q, &state);
735 if (r < 0)
736 return r;
737
738 dns_query_complete(q, state);
739 return 1;
740 }
741
742 r = dns_query_add_candidate(q, first);
743 if (r < 0)
744 goto fail;
745
746 LIST_FOREACH(scopes, s, first->scopes_next) {
747 DnsScopeMatch match;
748 const char *name;
749
750 name = dns_question_first_name(dns_query_question_for_protocol(q, s->protocol));
751 if (!name)
752 continue;
753
754 match = dns_scope_good_domain(s, q->ifindex, q->flags, name);
755 if (match < 0) {
756 log_debug("Couldn't check if '%s' matches against scope, ignoring.", name);
757 continue;
758 }
759
760 if (match < found)
761 continue;
762
763 r = dns_query_add_candidate(q, s);
764 if (r < 0)
765 goto fail;
766 }
767
768 dns_query_reset_answer(q);
769
770 r = sd_event_add_time_relative(
771 q->manager->event,
772 &q->timeout_event_source,
773 clock_boottime_or_monotonic(),
774 SD_RESOLVED_QUERY_TIMEOUT_USEC,
775 0, on_query_timeout, q);
776 if (r < 0)
777 goto fail;
778
779 (void) sd_event_source_set_description(q->timeout_event_source, "query-timeout");
780
781 q->state = DNS_TRANSACTION_PENDING;
782 q->block_ready++;
783
784 /* Start the transactions */
785 LIST_FOREACH(candidates_by_query, c, q->candidates) {
786 r = dns_query_candidate_go(c);
787 if (r < 0) {
788 q->block_ready--;
789 goto fail;
790 }
791 }
792
793 q->block_ready--;
794 dns_query_ready(q);
795
796 return 1;
797
798 fail:
799 dns_query_stop(q);
800 return r;
801 }
802
803 static void dns_query_accept(DnsQuery *q, DnsQueryCandidate *c) {
804 DnsTransactionState state = DNS_TRANSACTION_NO_SERVERS;
805 bool has_authenticated = false, has_non_authenticated = false, has_confidential = false, has_non_confidential = false;
806 DnssecResult dnssec_result_authenticated = _DNSSEC_RESULT_INVALID, dnssec_result_non_authenticated = _DNSSEC_RESULT_INVALID;
807 DnsTransaction *t;
808 int r;
809
810 assert(q);
811
812 if (!c) {
813 r = dns_query_synthesize_reply(q, &state);
814 if (r < 0)
815 goto fail;
816
817 dns_query_complete(q, state);
818 return;
819 }
820
821 if (c->error_code != 0) {
822 /* If the candidate had an error condition of its own, start with that. */
823 state = DNS_TRANSACTION_ERRNO;
824 q->answer = dns_answer_unref(q->answer);
825 q->answer_rcode = 0;
826 q->answer_dnssec_result = _DNSSEC_RESULT_INVALID;
827 q->answer_query_flags = 0;
828 q->answer_errno = c->error_code;
829 q->answer_full_packet = dns_packet_unref(q->answer_full_packet);
830 }
831
832 SET_FOREACH(t, c->transactions) {
833
834 switch (t->state) {
835
836 case DNS_TRANSACTION_SUCCESS: {
837 /* We found a successful reply, merge it into the answer */
838
839 if (state == DNS_TRANSACTION_SUCCESS) {
840 r = dns_answer_extend(&q->answer, t->answer);
841 if (r < 0)
842 goto fail;
843
844 q->answer_query_flags |= dns_transaction_source_to_query_flags(t->answer_source);
845 } else {
846 /* Override non-successful previous answers */
847 dns_answer_unref(q->answer);
848 q->answer = dns_answer_ref(t->answer);
849
850 q->answer_query_flags = dns_transaction_source_to_query_flags(t->answer_source);
851 }
852
853 q->answer_rcode = t->answer_rcode;
854 q->answer_errno = 0;
855
856 dns_packet_unref(q->answer_full_packet);
857 q->answer_full_packet = dns_packet_ref(t->received);
858
859 if (FLAGS_SET(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED)) {
860 has_authenticated = true;
861 dnssec_result_authenticated = t->answer_dnssec_result;
862 } else {
863 has_non_authenticated = true;
864 dnssec_result_non_authenticated = t->answer_dnssec_result;
865 }
866
867 if (FLAGS_SET(t->answer_query_flags, SD_RESOLVED_CONFIDENTIAL))
868 has_confidential = true;
869 else
870 has_non_confidential = true;
871
872 state = DNS_TRANSACTION_SUCCESS;
873 break;
874 }
875
876 case DNS_TRANSACTION_NULL:
877 case DNS_TRANSACTION_PENDING:
878 case DNS_TRANSACTION_VALIDATING:
879 case DNS_TRANSACTION_ABORTED:
880 /* Ignore transactions that didn't complete */
881 continue;
882
883 default:
884 /* Any kind of failure? Store the data away, if there's nothing stored yet. */
885 if (state == DNS_TRANSACTION_SUCCESS)
886 continue;
887
888 /* If there's already an authenticated negative reply stored, then prefer that over any unauthenticated one */
889 if (FLAGS_SET(q->answer_query_flags, SD_RESOLVED_AUTHENTICATED) &&
890 !FLAGS_SET(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
891 continue;
892
893 dns_answer_unref(q->answer);
894 q->answer = dns_answer_ref(t->answer);
895 q->answer_rcode = t->answer_rcode;
896 q->answer_dnssec_result = t->answer_dnssec_result;
897 q->answer_query_flags = t->answer_query_flags | dns_transaction_source_to_query_flags(t->answer_source);
898 q->answer_errno = t->answer_errno;
899 dns_packet_unref(q->answer_full_packet);
900 q->answer_full_packet = dns_packet_ref(t->received);
901
902 state = t->state;
903 break;
904 }
905 }
906
907 if (state == DNS_TRANSACTION_SUCCESS) {
908 SET_FLAG(q->answer_query_flags, SD_RESOLVED_AUTHENTICATED, has_authenticated && !has_non_authenticated);
909 SET_FLAG(q->answer_query_flags, SD_RESOLVED_CONFIDENTIAL, has_confidential && !has_non_confidential);
910 q->answer_dnssec_result = FLAGS_SET(q->answer_query_flags, SD_RESOLVED_AUTHENTICATED) ? dnssec_result_authenticated : dnssec_result_non_authenticated;
911 }
912
913 q->answer_protocol = c->scope->protocol;
914 q->answer_family = c->scope->family;
915
916 dns_search_domain_unref(q->answer_search_domain);
917 q->answer_search_domain = dns_search_domain_ref(c->search_domain);
918
919 r = dns_query_synthesize_reply(q, &state);
920 if (r < 0)
921 goto fail;
922
923 dns_query_complete(q, state);
924 return;
925
926 fail:
927 q->answer_errno = -r;
928 dns_query_complete(q, DNS_TRANSACTION_ERRNO);
929 }
930
931 void dns_query_ready(DnsQuery *q) {
932
933 DnsQueryCandidate *bad = NULL, *c;
934 bool pending = false;
935
936 assert(q);
937 assert(DNS_TRANSACTION_IS_LIVE(q->state));
938
939 /* Note that this call might invalidate the query. Callers
940 * should hence not attempt to access the query or transaction
941 * after calling this function, unless the block_ready
942 * counter was explicitly bumped before doing so. */
943
944 if (q->block_ready > 0)
945 return;
946
947 LIST_FOREACH(candidates_by_query, c, q->candidates) {
948 DnsTransactionState state;
949
950 state = dns_query_candidate_state(c);
951 switch (state) {
952
953 case DNS_TRANSACTION_SUCCESS:
954 /* One of the candidates is successful,
955 * let's use it, and copy its data out */
956 dns_query_accept(q, c);
957 return;
958
959 case DNS_TRANSACTION_NULL:
960 case DNS_TRANSACTION_PENDING:
961 case DNS_TRANSACTION_VALIDATING:
962 /* One of the candidates is still going on,
963 * let's maybe wait for it */
964 pending = true;
965 break;
966
967 default:
968 /* Any kind of failure */
969 bad = c;
970 break;
971 }
972 }
973
974 if (pending)
975 return;
976
977 dns_query_accept(q, bad);
978 }
979
980 static int dns_query_cname_redirect(DnsQuery *q, const DnsResourceRecord *cname) {
981 _cleanup_(dns_question_unrefp) DnsQuestion *nq_idna = NULL, *nq_utf8 = NULL;
982 int r, k;
983
984 assert(q);
985
986 q->n_cname_redirects++;
987 if (q->n_cname_redirects > CNAME_REDIRECT_MAX)
988 return -ELOOP;
989
990 r = dns_question_cname_redirect(q->question_idna, cname, &nq_idna);
991 if (r < 0)
992 return r;
993 if (r > 0)
994 log_debug("Following CNAME/DNAME %s → %s.", dns_question_first_name(q->question_idna), dns_question_first_name(nq_idna));
995
996 k = dns_question_is_equal(q->question_idna, q->question_utf8);
997 if (k < 0)
998 return k;
999 if (k > 0) {
1000 /* Same question? Shortcut new question generation */
1001 nq_utf8 = dns_question_ref(nq_idna);
1002 k = r;
1003 } else {
1004 k = dns_question_cname_redirect(q->question_utf8, cname, &nq_utf8);
1005 if (k < 0)
1006 return k;
1007 if (k > 0)
1008 log_debug("Following UTF8 CNAME/DNAME %s → %s.", dns_question_first_name(q->question_utf8), dns_question_first_name(nq_utf8));
1009 }
1010
1011 if (r == 0 && k == 0) /* No actual cname happened? */
1012 return -ELOOP;
1013
1014 if (q->answer_protocol == DNS_PROTOCOL_DNS)
1015 /* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources
1016 * cannot invade the local namespace. The opposite way we permit: local names may redirect to global
1017 * ones. */
1018 q->flags &= ~(SD_RESOLVED_LLMNR|SD_RESOLVED_MDNS); /* mask away the local protocols */
1019
1020 /* Turn off searching for the new name */
1021 q->flags |= SD_RESOLVED_NO_SEARCH;
1022
1023 dns_question_unref(q->question_idna);
1024 q->question_idna = TAKE_PTR(nq_idna);
1025
1026 dns_question_unref(q->question_utf8);
1027 q->question_utf8 = TAKE_PTR(nq_utf8);
1028
1029 dns_query_unref_candidates(q);
1030
1031 /* Note that we do *not* reset the answer here, because the answer we previously got might already
1032 * include everything we need, let's check that first */
1033
1034 q->state = DNS_TRANSACTION_NULL;
1035
1036 return 0;
1037 }
1038
1039 int dns_query_process_cname_one(DnsQuery *q) {
1040 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL;
1041 DnsQuestion *question;
1042 DnsResourceRecord *rr;
1043 bool full_match = true;
1044 DnsResourceKey *k;
1045 int r;
1046
1047 assert(q);
1048
1049 /* Processes a CNAME redirect if there's one. Returns one of three values:
1050 *
1051 * CNAME_QUERY_MATCH → direct RR match, caller should just use the RRs in this answer (and not
1052 * bother with any CNAME/DNAME stuff)
1053 *
1054 * CNAME_QUERY_NOMATCH → no match at all, neither direct nor CNAME/DNAME, caller might decide to
1055 * restart query or take things as NODATA reply.
1056 *
1057 * CNAME_QUERY_CNAME → no direct RR match, but a CNAME/DNAME match that we now followed for one step.
1058 *
1059 * The function might also return a failure, in particular -ELOOP if we encountered too many
1060 * CNAMEs/DNAMEs in a chain or if following CNAMEs/DNAMEs was turned off.
1061 *
1062 * Note that this function doesn't actually restart the query. The caller can decide to do that in
1063 * case of CNAME_QUERY_CNAME, though. */
1064
1065 if (!IN_SET(q->state, DNS_TRANSACTION_SUCCESS, DNS_TRANSACTION_NULL))
1066 return DNS_QUERY_NOMATCH;
1067
1068 question = dns_query_question_for_protocol(q, q->answer_protocol);
1069
1070 /* Small reminder: our question will consist of one or more RR keys that match in name, but not in
1071 * record type. Specifically, when we do an address lookup the question will typically consist of one
1072 * A and one AAAA key lookup for the same domain name. When we get a response from a server we need
1073 * to check if the answer answers all our questions to use it. Note that a response of CNAME/DNAME
1074 * can answer both an A and the AAAA question for us, but an A/AAAA response only the relevant
1075 * type.
1076 *
1077 * Hence we first check of the answers we collected are sufficient to answer all our questions
1078 * directly. If one question wasn't answered we go on, waiting for more replies. However, if there's
1079 * a CNAME/DNAME response we use it, and redirect to it, regardless if it was a response to the A or
1080 * the AAAA query.*/
1081
1082 DNS_QUESTION_FOREACH(k, question) {
1083 bool match = false;
1084
1085 DNS_ANSWER_FOREACH(rr, q->answer) {
1086 r = dns_resource_key_match_rr(k, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
1087 if (r < 0)
1088 return r;
1089 if (r > 0) {
1090 match = true; /* Yay, we found an RR that matches the key we are looking for */
1091 break;
1092 }
1093 }
1094
1095 if (!match) {
1096 /* Hmm. :-( there's no response for this key. This doesn't match. */
1097 full_match = false;
1098 break;
1099 }
1100 }
1101
1102 if (full_match)
1103 return DNS_QUERY_MATCH; /* The answer can answer our question in full, no need to follow CNAMEs/DNAMEs */
1104
1105 /* Let's see if there is a CNAME/DNAME to match. This case is simpler: we accept the CNAME/DNAME that
1106 * matches any of our questions. */
1107 DNS_ANSWER_FOREACH(rr, q->answer) {
1108 r = dns_question_matches_cname_or_dname(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
1109 if (r < 0)
1110 return r;
1111 if (r > 0 && !cname)
1112 cname = dns_resource_record_ref(rr);
1113 }
1114
1115 if (!cname)
1116 return DNS_QUERY_NOMATCH; /* No match and no CNAME/DNAME to follow */
1117
1118 if (q->flags & SD_RESOLVED_NO_CNAME)
1119 return -ELOOP;
1120
1121 if (!FLAGS_SET(q->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
1122 q->previous_redirect_unauthenticated = true;
1123 if (!FLAGS_SET(q->answer_query_flags, SD_RESOLVED_CONFIDENTIAL))
1124 q->previous_redirect_non_confidential = true;
1125 if (!FLAGS_SET(q->answer_query_flags, SD_RESOLVED_SYNTHETIC))
1126 q->previous_redirect_non_synthetic = true;
1127
1128 /* OK, let's actually follow the CNAME */
1129 r = dns_query_cname_redirect(q, cname);
1130 if (r < 0)
1131 return r;
1132
1133 return DNS_QUERY_CNAME; /* Tell caller that we did a single CNAME/DNAME redirection step */
1134 }
1135
1136 int dns_query_process_cname_many(DnsQuery *q) {
1137 int r;
1138
1139 assert(q);
1140
1141 /* Follows CNAMEs through the current packet: as long as the current packet can fulfill our
1142 * redirected CNAME queries we keep going, and restart the query once the current packet isn't good
1143 * enough anymore. It's a wrapper around dns_query_process_cname_one() and returns the same values,
1144 * but with extended semantics. Specifically:
1145 *
1146 * DNS_QUERY_MATCH → as above
1147 *
1148 * DNS_QUERY_CNAME → we ran into a CNAME/DNAME redirect that we could not answer from the current
1149 * message, and thus restarted the query to resolve it.
1150 *
1151 * DNS_QUERY_NOMATCH → we reached the end of CNAME/DNAME chain, and there are no direct matches nor a
1152 * CNAME/DNAME match. i.e. this is a NODATA case.
1153 *
1154 * Note that this function will restart the query for the caller if needed, and that's the case
1155 * DNS_QUERY_CNAME is returned.
1156 */
1157
1158 r = dns_query_process_cname_one(q);
1159 if (r != DNS_QUERY_CNAME)
1160 return r; /* The first redirect is special: if it doesn't answer the question that's no
1161 * reason to restart the query, we just accept this as a NODATA answer. */
1162
1163 for (;;) {
1164 r = dns_query_process_cname_one(q);
1165 if (r < 0 || r == DNS_QUERY_MATCH)
1166 return r;
1167 if (r == DNS_QUERY_NOMATCH) {
1168 /* OK, so we followed one or more CNAME/DNAME RR but the existing packet can't answer
1169 * this. Let's restart the query hence, with the new question. Why the different
1170 * handling than the first chain element? Because if the server answers a direct
1171 * question with an empty answer then this is a NODATA response. But if it responds
1172 * with a CNAME chain that ultimately is incomplete (i.e. a non-empty but truncated
1173 * CNAME chain) then we better follow up ourselves and ask for the rest of the
1174 * chain. This is particular relevant since our cache will store CNAME/DNAME
1175 * redirects that we learnt about for lookups of certain DNS types, but later on we
1176 * can reuse this data even for other DNS types, but in that case need to follow up
1177 * with the final lookup of the chain ourselves with the RR type we ourselves are
1178 * interested in. */
1179 r = dns_query_go(q);
1180 if (r < 0)
1181 return r;
1182
1183 return DNS_QUERY_CNAME;
1184 }
1185
1186 /* So we found a CNAME that the existing packet already answers, again via a CNAME, let's
1187 * continue going then. */
1188 assert(r == DNS_QUERY_CNAME);
1189 }
1190 }
1191
1192 DnsQuestion* dns_query_question_for_protocol(DnsQuery *q, DnsProtocol protocol) {
1193 assert(q);
1194
1195 if (q->question_bypass)
1196 return q->question_bypass->question;
1197
1198 switch (protocol) {
1199
1200 case DNS_PROTOCOL_DNS:
1201 return q->question_idna;
1202
1203 case DNS_PROTOCOL_MDNS:
1204 case DNS_PROTOCOL_LLMNR:
1205 return q->question_utf8;
1206
1207 default:
1208 return NULL;
1209 }
1210 }
1211
1212 const char *dns_query_string(DnsQuery *q) {
1213 const char *name;
1214 int r;
1215
1216 /* Returns a somewhat useful human-readable lookup key string for this query */
1217
1218 if (q->question_bypass)
1219 return dns_question_first_name(q->question_bypass->question);
1220
1221 if (q->request_address_string)
1222 return q->request_address_string;
1223
1224 if (q->request_address_valid) {
1225 r = in_addr_to_string(q->request_family, &q->request_address, &q->request_address_string);
1226 if (r >= 0)
1227 return q->request_address_string;
1228 }
1229
1230 name = dns_question_first_name(q->question_utf8);
1231 if (name)
1232 return name;
1233
1234 return dns_question_first_name(q->question_idna);
1235 }
1236
1237 bool dns_query_fully_authenticated(DnsQuery *q) {
1238 assert(q);
1239
1240 return FLAGS_SET(q->answer_query_flags, SD_RESOLVED_AUTHENTICATED) && !q->previous_redirect_unauthenticated;
1241 }
1242
1243 bool dns_query_fully_confidential(DnsQuery *q) {
1244 assert(q);
1245
1246 return FLAGS_SET(q->answer_query_flags, SD_RESOLVED_CONFIDENTIAL) && !q->previous_redirect_non_confidential;
1247 }
1248
1249 bool dns_query_fully_authoritative(DnsQuery *q) {
1250 assert(q);
1251
1252 /* We are authoritative for everything synthetic (except if a previous CNAME/DNAME) wasn't
1253 * synthetic. (Note: SD_RESOLVED_SYNTHETIC is reset on each CNAME/DNAME, hence the explicit check for
1254 * previous synthetic DNAME/CNAME redirections.)*/
1255 if ((q->answer_query_flags & SD_RESOLVED_SYNTHETIC) && !q->previous_redirect_non_synthetic)
1256 return true;
1257
1258 /* We are also authoritative for everything coming only from the trust anchor and the local
1259 * zones. (Note: the SD_RESOLVED_FROM_xyz flags we merge on each redirect, hence no need to
1260 * explicitly check previous redirects here.)*/
1261 return (q->answer_query_flags & SD_RESOLVED_FROM_MASK & ~(SD_RESOLVED_FROM_TRUST_ANCHOR | SD_RESOLVED_FROM_ZONE)) == 0;
1262 }