]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-query.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / resolve / resolved-dns-query.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "dns-domain.h"
22 #include "dns-type.h"
23 #include "hostname-util.h"
24 #include "local-addresses.h"
25 #include "resolved-dns-query.h"
26 #include "resolved-dns-synthesize.h"
27 #include "resolved-etc-hosts.h"
28 #include "string-util.h"
29
30 /* How long to wait for the query in total */
31 #define QUERY_TIMEOUT_USEC (30 * USEC_PER_SEC)
32
33 #define CNAME_MAX 8
34 #define QUERIES_MAX 2048
35 #define AUXILIARY_QUERIES_MAX 64
36
37 static int dns_query_candidate_new(DnsQueryCandidate **ret, DnsQuery *q, DnsScope *s) {
38 DnsQueryCandidate *c;
39
40 assert(ret);
41 assert(q);
42 assert(s);
43
44 c = new0(DnsQueryCandidate, 1);
45 if (!c)
46 return -ENOMEM;
47
48 c->query = q;
49 c->scope = s;
50
51 LIST_PREPEND(candidates_by_query, q->candidates, c);
52 LIST_PREPEND(candidates_by_scope, s->query_candidates, c);
53
54 *ret = c;
55 return 0;
56 }
57
58 static void dns_query_candidate_stop(DnsQueryCandidate *c) {
59 DnsTransaction *t;
60
61 assert(c);
62
63 while ((t = set_steal_first(c->transactions))) {
64 set_remove(t->notify_query_candidates, c);
65 dns_transaction_gc(t);
66 }
67 }
68
69 DnsQueryCandidate* dns_query_candidate_free(DnsQueryCandidate *c) {
70
71 if (!c)
72 return NULL;
73
74 dns_query_candidate_stop(c);
75
76 set_free(c->transactions);
77 dns_search_domain_unref(c->search_domain);
78
79 if (c->query)
80 LIST_REMOVE(candidates_by_query, c->query->candidates, c);
81
82 if (c->scope)
83 LIST_REMOVE(candidates_by_scope, c->scope->query_candidates, c);
84
85 free(c);
86
87 return NULL;
88 }
89
90 static int dns_query_candidate_next_search_domain(DnsQueryCandidate *c) {
91 DnsSearchDomain *next = NULL;
92
93 assert(c);
94
95 if (c->search_domain && c->search_domain->linked)
96 next = c->search_domain->domains_next;
97 else
98 next = dns_scope_get_search_domains(c->scope);
99
100 for (;;) {
101 if (!next) /* We hit the end of the list */
102 return 0;
103
104 if (!next->route_only)
105 break;
106
107 /* Skip over route-only domains */
108 next = next->domains_next;
109 }
110
111 dns_search_domain_unref(c->search_domain);
112 c->search_domain = dns_search_domain_ref(next);
113
114 return 1;
115 }
116
117 static int dns_query_candidate_add_transaction(DnsQueryCandidate *c, DnsResourceKey *key) {
118 DnsTransaction *t;
119 int r;
120
121 assert(c);
122 assert(key);
123
124 t = dns_scope_find_transaction(c->scope, key, true);
125 if (!t) {
126 r = dns_transaction_new(&t, c->scope, key);
127 if (r < 0)
128 return r;
129 } else {
130 if (set_contains(c->transactions, t))
131 return 0;
132 }
133
134 r = set_ensure_allocated(&c->transactions, NULL);
135 if (r < 0)
136 goto gc;
137
138 r = set_ensure_allocated(&t->notify_query_candidates, NULL);
139 if (r < 0)
140 goto gc;
141
142 r = set_put(t->notify_query_candidates, c);
143 if (r < 0)
144 goto gc;
145
146 r = set_put(c->transactions, t);
147 if (r < 0) {
148 (void) set_remove(t->notify_query_candidates, c);
149 goto gc;
150 }
151
152 return 1;
153
154 gc:
155 dns_transaction_gc(t);
156 return r;
157 }
158
159 static int dns_query_candidate_go(DnsQueryCandidate *c) {
160 DnsTransaction *t;
161 Iterator i;
162 int r;
163 unsigned n = 0;
164
165 assert(c);
166
167 /* Start the transactions that are not started yet */
168 SET_FOREACH(t, c->transactions, i) {
169 if (t->state != DNS_TRANSACTION_NULL)
170 continue;
171
172 r = dns_transaction_go(t);
173 if (r < 0)
174 return r;
175
176 n++;
177 }
178
179 /* If there was nothing to start, then let's proceed immediately */
180 if (n == 0)
181 dns_query_candidate_notify(c);
182
183 return 0;
184 }
185
186 static DnsTransactionState dns_query_candidate_state(DnsQueryCandidate *c) {
187 DnsTransactionState state = DNS_TRANSACTION_NO_SERVERS;
188 DnsTransaction *t;
189 Iterator i;
190
191 assert(c);
192
193 if (c->error_code != 0)
194 return DNS_TRANSACTION_ERRNO;
195
196 SET_FOREACH(t, c->transactions, i) {
197
198 switch (t->state) {
199
200 case DNS_TRANSACTION_NULL:
201 /* If there's a NULL transaction pending, then
202 * this means not all transactions where
203 * started yet, and we were called from within
204 * the stackframe that is supposed to start
205 * remaining transactions. In this case,
206 * simply claim the candidate is pending. */
207
208 case DNS_TRANSACTION_PENDING:
209 case DNS_TRANSACTION_VALIDATING:
210 /* If there's one transaction currently in
211 * VALIDATING state, then this means there's
212 * also one in PENDING state, hence we can
213 * return PENDING immediately. */
214 return DNS_TRANSACTION_PENDING;
215
216 case DNS_TRANSACTION_SUCCESS:
217 state = t->state;
218 break;
219
220 default:
221 if (state != DNS_TRANSACTION_SUCCESS)
222 state = t->state;
223
224 break;
225 }
226 }
227
228 return state;
229 }
230
231 static bool dns_query_candidate_is_routable(DnsQueryCandidate *c, uint16_t type) {
232 int family;
233
234 assert(c);
235
236 /* Checks whether the specified RR type matches an address family that is routable on the link(s) the scope of
237 * this candidate belongs to. Specifically, whether there's a routable IPv4 address on it if we query an A RR,
238 * or a routable IPv6 address if we query an AAAA RR. */
239
240 if (!c->query->suppress_unroutable_family)
241 return true;
242
243 if (c->scope->protocol != DNS_PROTOCOL_DNS)
244 return true;
245
246 family = dns_type_to_af(type);
247 if (family < 0)
248 return true;
249
250 if (c->scope->link)
251 return link_relevant(c->scope->link, family, false);
252 else
253 return manager_routable(c->scope->manager, family);
254 }
255
256 static int dns_query_candidate_setup_transactions(DnsQueryCandidate *c) {
257 DnsQuestion *question;
258 DnsResourceKey *key;
259 int n = 0, r;
260
261 assert(c);
262
263 dns_query_candidate_stop(c);
264
265 question = dns_query_question_for_protocol(c->query, c->scope->protocol);
266
267 /* Create one transaction per question key */
268 DNS_QUESTION_FOREACH(key, question) {
269 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *new_key = NULL;
270 DnsResourceKey *qkey;
271
272 if (!dns_query_candidate_is_routable(c, key->type))
273 continue;
274
275 if (c->search_domain) {
276 r = dns_resource_key_new_append_suffix(&new_key, key, c->search_domain->name);
277 if (r < 0)
278 goto fail;
279
280 qkey = new_key;
281 } else
282 qkey = key;
283
284 if (!dns_scope_good_key(c->scope, qkey))
285 continue;
286
287 r = dns_query_candidate_add_transaction(c, qkey);
288 if (r < 0)
289 goto fail;
290
291 n++;
292 }
293
294 return n;
295
296 fail:
297 dns_query_candidate_stop(c);
298 return r;
299 }
300
301 void dns_query_candidate_notify(DnsQueryCandidate *c) {
302 DnsTransactionState state;
303 int r;
304
305 assert(c);
306
307 state = dns_query_candidate_state(c);
308
309 if (DNS_TRANSACTION_IS_LIVE(state))
310 return;
311
312 if (state != DNS_TRANSACTION_SUCCESS && c->search_domain) {
313
314 r = dns_query_candidate_next_search_domain(c);
315 if (r < 0)
316 goto fail;
317
318 if (r > 0) {
319 /* OK, there's another search domain to try, let's do so. */
320
321 r = dns_query_candidate_setup_transactions(c);
322 if (r < 0)
323 goto fail;
324
325 if (r > 0) {
326 /* New transactions where queued. Start them and wait */
327
328 r = dns_query_candidate_go(c);
329 if (r < 0)
330 goto fail;
331
332 return;
333 }
334 }
335
336 }
337
338 dns_query_ready(c->query);
339 return;
340
341 fail:
342 log_warning_errno(r, "Failed to follow search domains: %m");
343 c->error_code = r;
344 dns_query_ready(c->query);
345 }
346
347 static void dns_query_stop(DnsQuery *q) {
348 DnsQueryCandidate *c;
349
350 assert(q);
351
352 q->timeout_event_source = sd_event_source_unref(q->timeout_event_source);
353
354 LIST_FOREACH(candidates_by_query, c, q->candidates)
355 dns_query_candidate_stop(c);
356 }
357
358 static void dns_query_free_candidates(DnsQuery *q) {
359 assert(q);
360
361 while (q->candidates)
362 dns_query_candidate_free(q->candidates);
363 }
364
365 static void dns_query_reset_answer(DnsQuery *q) {
366 assert(q);
367
368 q->answer = dns_answer_unref(q->answer);
369 q->answer_rcode = 0;
370 q->answer_dnssec_result = _DNSSEC_RESULT_INVALID;
371 q->answer_errno = 0;
372 q->answer_authenticated = false;
373 q->answer_protocol = _DNS_PROTOCOL_INVALID;
374 q->answer_family = AF_UNSPEC;
375 q->answer_search_domain = dns_search_domain_unref(q->answer_search_domain);
376 }
377
378 DnsQuery *dns_query_free(DnsQuery *q) {
379 if (!q)
380 return NULL;
381
382 while (q->auxiliary_queries)
383 dns_query_free(q->auxiliary_queries);
384
385 if (q->auxiliary_for) {
386 assert(q->auxiliary_for->n_auxiliary_queries > 0);
387 q->auxiliary_for->n_auxiliary_queries--;
388 LIST_REMOVE(auxiliary_queries, q->auxiliary_for->auxiliary_queries, q);
389 }
390
391 dns_query_free_candidates(q);
392
393 dns_question_unref(q->question_idna);
394 dns_question_unref(q->question_utf8);
395
396 dns_query_reset_answer(q);
397
398 sd_bus_message_unref(q->request);
399 sd_bus_track_unref(q->bus_track);
400
401 free(q->request_address_string);
402
403 if (q->manager) {
404 LIST_REMOVE(queries, q->manager->dns_queries, q);
405 q->manager->n_dns_queries--;
406 }
407
408 free(q);
409
410 return NULL;
411 }
412
413 int dns_query_new(
414 Manager *m,
415 DnsQuery **ret,
416 DnsQuestion *question_utf8,
417 DnsQuestion *question_idna,
418 int ifindex, uint64_t flags) {
419
420 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
421 DnsResourceKey *key;
422 bool good = false;
423 int r;
424
425 assert(m);
426
427 if (dns_question_size(question_utf8) > 0) {
428 r = dns_question_is_valid_for_query(question_utf8);
429 if (r < 0)
430 return r;
431 if (r == 0)
432 return -EINVAL;
433
434 good = true;
435 }
436
437 /* If the IDNA and UTF8 questions are the same, merge their references */
438 r = dns_question_is_equal(question_idna, question_utf8);
439 if (r < 0)
440 return r;
441 if (r > 0)
442 question_idna = question_utf8;
443 else {
444 if (dns_question_size(question_idna) > 0) {
445 r = dns_question_is_valid_for_query(question_idna);
446 if (r < 0)
447 return r;
448 if (r == 0)
449 return -EINVAL;
450
451 good = true;
452 }
453 }
454
455 if (!good) /* don't allow empty queries */
456 return -EINVAL;
457
458 if (m->n_dns_queries >= QUERIES_MAX)
459 return -EBUSY;
460
461 q = new0(DnsQuery, 1);
462 if (!q)
463 return -ENOMEM;
464
465 q->question_utf8 = dns_question_ref(question_utf8);
466 q->question_idna = dns_question_ref(question_idna);
467 q->ifindex = ifindex;
468 q->flags = flags;
469 q->answer_dnssec_result = _DNSSEC_RESULT_INVALID;
470 q->answer_protocol = _DNS_PROTOCOL_INVALID;
471 q->answer_family = AF_UNSPEC;
472
473 /* First dump UTF8 question */
474 DNS_QUESTION_FOREACH(key, question_utf8) {
475 _cleanup_free_ char *p = NULL;
476
477 r = dns_resource_key_to_string(key, &p);
478 if (r < 0)
479 return r;
480
481 log_debug("Looking up RR for %s.", strstrip(p));
482 }
483
484 /* And then dump the IDNA question, but only what hasn't been dumped already through the UTF8 question. */
485 DNS_QUESTION_FOREACH(key, question_idna) {
486 _cleanup_free_ char *p = NULL;
487
488 r = dns_question_contains(question_utf8, key);
489 if (r < 0)
490 return r;
491 if (r > 0)
492 continue;
493
494 r = dns_resource_key_to_string(key, &p);
495 if (r < 0)
496 return r;
497
498 log_debug("Looking up IDNA RR for %s.", strstrip(p));
499 }
500
501 LIST_PREPEND(queries, m->dns_queries, q);
502 m->n_dns_queries++;
503 q->manager = m;
504
505 if (ret)
506 *ret = q;
507 q = NULL;
508
509 return 0;
510 }
511
512 int dns_query_make_auxiliary(DnsQuery *q, DnsQuery *auxiliary_for) {
513 assert(q);
514 assert(auxiliary_for);
515
516 /* Ensure that that the query is not auxiliary yet, and
517 * nothing else is auxiliary to it either */
518 assert(!q->auxiliary_for);
519 assert(!q->auxiliary_queries);
520
521 /* Ensure that the unit we shall be made auxiliary for isn't
522 * auxiliary itself */
523 assert(!auxiliary_for->auxiliary_for);
524
525 if (auxiliary_for->n_auxiliary_queries >= AUXILIARY_QUERIES_MAX)
526 return -EAGAIN;
527
528 LIST_PREPEND(auxiliary_queries, auxiliary_for->auxiliary_queries, q);
529 q->auxiliary_for = auxiliary_for;
530
531 auxiliary_for->n_auxiliary_queries++;
532 return 0;
533 }
534
535 static void dns_query_complete(DnsQuery *q, DnsTransactionState state) {
536 assert(q);
537 assert(!DNS_TRANSACTION_IS_LIVE(state));
538 assert(DNS_TRANSACTION_IS_LIVE(q->state));
539
540 /* Note that this call might invalidate the query. Callers
541 * should hence not attempt to access the query or transaction
542 * after calling this function. */
543
544 q->state = state;
545
546 dns_query_stop(q);
547 if (q->complete)
548 q->complete(q);
549 }
550
551 static int on_query_timeout(sd_event_source *s, usec_t usec, void *userdata) {
552 DnsQuery *q = userdata;
553
554 assert(s);
555 assert(q);
556
557 dns_query_complete(q, DNS_TRANSACTION_TIMEOUT);
558 return 0;
559 }
560
561 static int dns_query_add_candidate(DnsQuery *q, DnsScope *s) {
562 DnsQueryCandidate *c;
563 int r;
564
565 assert(q);
566 assert(s);
567
568 r = dns_query_candidate_new(&c, q, s);
569 if (r < 0)
570 return r;
571
572 /* If this a single-label domain on DNS, we might append a suitable search domain first. */
573 if ((q->flags & SD_RESOLVED_NO_SEARCH) == 0) {
574 r = dns_scope_name_needs_search_domain(s, dns_question_first_name(q->question_idna));
575 if (r < 0)
576 goto fail;
577 if (r > 0) {
578 /* OK, we need a search domain now. Let's find one for this scope */
579
580 r = dns_query_candidate_next_search_domain(c);
581 if (r <= 0) /* if there's no search domain, then we won't add any transaction. */
582 goto fail;
583 }
584 }
585
586 r = dns_query_candidate_setup_transactions(c);
587 if (r < 0)
588 goto fail;
589
590 return 0;
591
592 fail:
593 dns_query_candidate_free(c);
594 return r;
595 }
596
597 static int dns_query_synthesize_reply(DnsQuery *q, DnsTransactionState *state) {
598 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
599 int r;
600
601 assert(q);
602 assert(state);
603
604 /* Tries to synthesize localhost RR replies (and others) where appropriate. Note that this is done *after* the
605 * the normal lookup finished. The data from the network hence takes precedence over the data we
606 * synthesize. (But note that many scopes refuse to resolve certain domain names) */
607
608 if (!IN_SET(*state,
609 DNS_TRANSACTION_RCODE_FAILURE,
610 DNS_TRANSACTION_NO_SERVERS,
611 DNS_TRANSACTION_TIMEOUT,
612 DNS_TRANSACTION_ATTEMPTS_MAX_REACHED,
613 DNS_TRANSACTION_NETWORK_DOWN,
614 DNS_TRANSACTION_NOT_FOUND))
615 return 0;
616
617 r = dns_synthesize_answer(
618 q->manager,
619 q->question_utf8,
620 q->ifindex,
621 &answer);
622
623 if (r <= 0)
624 return r;
625
626 dns_query_reset_answer(q);
627
628 q->answer = answer;
629 answer = NULL;
630 q->answer_rcode = DNS_RCODE_SUCCESS;
631 q->answer_protocol = dns_synthesize_protocol(q->flags);
632 q->answer_family = dns_synthesize_family(q->flags);
633 q->answer_authenticated = true;
634
635 *state = DNS_TRANSACTION_SUCCESS;
636
637 return 1;
638 }
639
640 static int dns_query_try_etc_hosts(DnsQuery *q) {
641 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
642 int r;
643
644 assert(q);
645
646 /* Looks in /etc/hosts for matching entries. Note that this is done *before* the normal lookup is done. The
647 * data from /etc/hosts hence takes precedence over the network. */
648
649 r = manager_etc_hosts_lookup(
650 q->manager,
651 q->question_utf8,
652 &answer);
653 if (r <= 0)
654 return r;
655
656 dns_query_reset_answer(q);
657
658 q->answer = answer;
659 answer = NULL;
660 q->answer_rcode = DNS_RCODE_SUCCESS;
661 q->answer_protocol = dns_synthesize_protocol(q->flags);
662 q->answer_family = dns_synthesize_family(q->flags);
663 q->answer_authenticated = true;
664
665 return 1;
666 }
667
668 int dns_query_go(DnsQuery *q) {
669 DnsScopeMatch found = DNS_SCOPE_NO;
670 DnsScope *s, *first = NULL;
671 DnsQueryCandidate *c;
672 int r;
673
674 assert(q);
675
676 if (q->state != DNS_TRANSACTION_NULL)
677 return 0;
678
679 r = dns_query_try_etc_hosts(q);
680 if (r < 0)
681 return r;
682 if (r > 0) {
683 dns_query_complete(q, DNS_TRANSACTION_SUCCESS);
684 return 1;
685 }
686
687 LIST_FOREACH(scopes, s, q->manager->dns_scopes) {
688 DnsScopeMatch match;
689 const char *name;
690
691 name = dns_question_first_name(dns_query_question_for_protocol(q, s->protocol));
692 if (!name)
693 continue;
694
695 match = dns_scope_good_domain(s, q->ifindex, q->flags, name);
696 if (match < 0)
697 return match;
698
699 if (match == DNS_SCOPE_NO)
700 continue;
701
702 found = match;
703
704 if (match == DNS_SCOPE_YES) {
705 first = s;
706 break;
707 } else {
708 assert(match == DNS_SCOPE_MAYBE);
709
710 if (!first)
711 first = s;
712 }
713 }
714
715 if (found == DNS_SCOPE_NO) {
716 DnsTransactionState state = DNS_TRANSACTION_NO_SERVERS;
717
718 r = dns_query_synthesize_reply(q, &state);
719 if (r < 0)
720 return r;
721
722 dns_query_complete(q, state);
723 return 1;
724 }
725
726 r = dns_query_add_candidate(q, first);
727 if (r < 0)
728 goto fail;
729
730 LIST_FOREACH(scopes, s, first->scopes_next) {
731 DnsScopeMatch match;
732 const char *name;
733
734 name = dns_question_first_name(dns_query_question_for_protocol(q, s->protocol));
735 if (!name)
736 continue;
737
738 match = dns_scope_good_domain(s, q->ifindex, q->flags, name);
739 if (match < 0)
740 goto fail;
741
742 if (match != found)
743 continue;
744
745 r = dns_query_add_candidate(q, s);
746 if (r < 0)
747 goto fail;
748 }
749
750 dns_query_reset_answer(q);
751
752 r = sd_event_add_time(
753 q->manager->event,
754 &q->timeout_event_source,
755 clock_boottime_or_monotonic(),
756 now(clock_boottime_or_monotonic()) + QUERY_TIMEOUT_USEC, 0,
757 on_query_timeout, q);
758 if (r < 0)
759 goto fail;
760
761 (void) sd_event_source_set_description(q->timeout_event_source, "query-timeout");
762
763 q->state = DNS_TRANSACTION_PENDING;
764 q->block_ready++;
765
766 /* Start the transactions */
767 LIST_FOREACH(candidates_by_query, c, q->candidates) {
768 r = dns_query_candidate_go(c);
769 if (r < 0) {
770 q->block_ready--;
771 goto fail;
772 }
773 }
774
775 q->block_ready--;
776 dns_query_ready(q);
777
778 return 1;
779
780 fail:
781 dns_query_stop(q);
782 return r;
783 }
784
785 static void dns_query_accept(DnsQuery *q, DnsQueryCandidate *c) {
786 DnsTransactionState state = DNS_TRANSACTION_NO_SERVERS;
787 bool has_authenticated = false, has_non_authenticated = false;
788 DnssecResult dnssec_result_authenticated = _DNSSEC_RESULT_INVALID, dnssec_result_non_authenticated = _DNSSEC_RESULT_INVALID;
789 DnsTransaction *t;
790 Iterator i;
791 int r;
792
793 assert(q);
794
795 if (!c) {
796 r = dns_query_synthesize_reply(q, &state);
797 if (r < 0)
798 goto fail;
799
800 dns_query_complete(q, state);
801 return;
802 }
803
804 if (c->error_code != 0) {
805 /* If the candidate had an error condition of its own, start with that. */
806 state = DNS_TRANSACTION_ERRNO;
807 q->answer = dns_answer_unref(q->answer);
808 q->answer_rcode = 0;
809 q->answer_dnssec_result = _DNSSEC_RESULT_INVALID;
810 q->answer_errno = c->error_code;
811 }
812
813 SET_FOREACH(t, c->transactions, i) {
814
815 switch (t->state) {
816
817 case DNS_TRANSACTION_SUCCESS: {
818 /* We found a successfuly reply, merge it into the answer */
819 r = dns_answer_extend(&q->answer, t->answer);
820 if (r < 0)
821 goto fail;
822
823 q->answer_rcode = t->answer_rcode;
824 q->answer_errno = 0;
825
826 if (t->answer_authenticated) {
827 has_authenticated = true;
828 dnssec_result_authenticated = t->answer_dnssec_result;
829 } else {
830 has_non_authenticated = true;
831 dnssec_result_non_authenticated = t->answer_dnssec_result;
832 }
833
834 state = DNS_TRANSACTION_SUCCESS;
835 break;
836 }
837
838 case DNS_TRANSACTION_NULL:
839 case DNS_TRANSACTION_PENDING:
840 case DNS_TRANSACTION_VALIDATING:
841 case DNS_TRANSACTION_ABORTED:
842 /* Ignore transactions that didn't complete */
843 continue;
844
845 default:
846 /* Any kind of failure? Store the data away,
847 * if there's nothing stored yet. */
848
849 if (state == DNS_TRANSACTION_SUCCESS)
850 continue;
851
852 q->answer = dns_answer_unref(q->answer);
853 q->answer_rcode = t->answer_rcode;
854 q->answer_dnssec_result = t->answer_dnssec_result;
855 q->answer_errno = t->answer_errno;
856
857 state = t->state;
858 break;
859 }
860 }
861
862 if (state == DNS_TRANSACTION_SUCCESS) {
863 q->answer_authenticated = has_authenticated && !has_non_authenticated;
864 q->answer_dnssec_result = q->answer_authenticated ? dnssec_result_authenticated : dnssec_result_non_authenticated;
865 }
866
867 q->answer_protocol = c->scope->protocol;
868 q->answer_family = c->scope->family;
869
870 dns_search_domain_unref(q->answer_search_domain);
871 q->answer_search_domain = dns_search_domain_ref(c->search_domain);
872
873 r = dns_query_synthesize_reply(q, &state);
874 if (r < 0)
875 goto fail;
876
877 dns_query_complete(q, state);
878 return;
879
880 fail:
881 q->answer_errno = -r;
882 dns_query_complete(q, DNS_TRANSACTION_ERRNO);
883 }
884
885 void dns_query_ready(DnsQuery *q) {
886
887 DnsQueryCandidate *bad = NULL, *c;
888 bool pending = false;
889
890 assert(q);
891 assert(DNS_TRANSACTION_IS_LIVE(q->state));
892
893 /* Note that this call might invalidate the query. Callers
894 * should hence not attempt to access the query or transaction
895 * after calling this function, unless the block_ready
896 * counter was explicitly bumped before doing so. */
897
898 if (q->block_ready > 0)
899 return;
900
901 LIST_FOREACH(candidates_by_query, c, q->candidates) {
902 DnsTransactionState state;
903
904 state = dns_query_candidate_state(c);
905 switch (state) {
906
907 case DNS_TRANSACTION_SUCCESS:
908 /* One of the candidates is successful,
909 * let's use it, and copy its data out */
910 dns_query_accept(q, c);
911 return;
912
913 case DNS_TRANSACTION_NULL:
914 case DNS_TRANSACTION_PENDING:
915 case DNS_TRANSACTION_VALIDATING:
916 /* One of the candidates is still going on,
917 * let's maybe wait for it */
918 pending = true;
919 break;
920
921 default:
922 /* Any kind of failure */
923 bad = c;
924 break;
925 }
926 }
927
928 if (pending)
929 return;
930
931 dns_query_accept(q, bad);
932 }
933
934 static int dns_query_cname_redirect(DnsQuery *q, const DnsResourceRecord *cname) {
935 _cleanup_(dns_question_unrefp) DnsQuestion *nq_idna = NULL, *nq_utf8 = NULL;
936 int r, k;
937
938 assert(q);
939
940 q->n_cname_redirects ++;
941 if (q->n_cname_redirects > CNAME_MAX)
942 return -ELOOP;
943
944 r = dns_question_cname_redirect(q->question_idna, cname, &nq_idna);
945 if (r < 0)
946 return r;
947 else if (r > 0)
948 log_debug("Following CNAME/DNAME %s → %s.", dns_question_first_name(q->question_idna), dns_question_first_name(nq_idna));
949
950 k = dns_question_is_equal(q->question_idna, q->question_utf8);
951 if (k < 0)
952 return r;
953 if (k > 0) {
954 /* Same question? Shortcut new question generation */
955 nq_utf8 = dns_question_ref(nq_idna);
956 k = r;
957 } else {
958 k = dns_question_cname_redirect(q->question_utf8, cname, &nq_utf8);
959 if (k < 0)
960 return k;
961 else if (k > 0)
962 log_debug("Following UTF8 CNAME/DNAME %s → %s.", dns_question_first_name(q->question_utf8), dns_question_first_name(nq_utf8));
963 }
964
965 if (r == 0 && k == 0) /* No actual cname happened? */
966 return -ELOOP;
967
968 if (q->answer_protocol == DNS_PROTOCOL_DNS) {
969 /* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources
970 * cannot invade the local namespace. The opposite way we permit: local names may redirect to global
971 * ones. */
972
973 q->flags &= ~(SD_RESOLVED_LLMNR|SD_RESOLVED_MDNS); /* mask away the local protocols */
974 }
975
976 /* Turn off searching for the new name */
977 q->flags |= SD_RESOLVED_NO_SEARCH;
978
979 dns_question_unref(q->question_idna);
980 q->question_idna = nq_idna;
981 nq_idna = NULL;
982
983 dns_question_unref(q->question_utf8);
984 q->question_utf8 = nq_utf8;
985 nq_utf8 = NULL;
986
987 dns_query_free_candidates(q);
988 dns_query_reset_answer(q);
989
990 q->state = DNS_TRANSACTION_NULL;
991
992 return 0;
993 }
994
995 int dns_query_process_cname(DnsQuery *q) {
996 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL;
997 DnsQuestion *question;
998 DnsResourceRecord *rr;
999 int r;
1000
1001 assert(q);
1002
1003 if (!IN_SET(q->state, DNS_TRANSACTION_SUCCESS, DNS_TRANSACTION_NULL))
1004 return DNS_QUERY_NOMATCH;
1005
1006 question = dns_query_question_for_protocol(q, q->answer_protocol);
1007
1008 DNS_ANSWER_FOREACH(rr, q->answer) {
1009 r = dns_question_matches_rr(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
1010 if (r < 0)
1011 return r;
1012 if (r > 0)
1013 return DNS_QUERY_MATCH; /* The answer matches directly, no need to follow cnames */
1014
1015 r = dns_question_matches_cname_or_dname(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
1016 if (r < 0)
1017 return r;
1018 if (r > 0 && !cname)
1019 cname = dns_resource_record_ref(rr);
1020 }
1021
1022 if (!cname)
1023 return DNS_QUERY_NOMATCH; /* No match and no cname to follow */
1024
1025 if (q->flags & SD_RESOLVED_NO_CNAME)
1026 return -ELOOP;
1027
1028 /* OK, let's actually follow the CNAME */
1029 r = dns_query_cname_redirect(q, cname);
1030 if (r < 0)
1031 return r;
1032
1033 /* Let's see if the answer can already answer the new
1034 * redirected question */
1035 r = dns_query_process_cname(q);
1036 if (r != DNS_QUERY_NOMATCH)
1037 return r;
1038
1039 /* OK, it cannot, let's begin with the new query */
1040 r = dns_query_go(q);
1041 if (r < 0)
1042 return r;
1043
1044 return DNS_QUERY_RESTARTED; /* We restarted the query for a new cname */
1045 }
1046
1047 static int on_bus_track(sd_bus_track *t, void *userdata) {
1048 DnsQuery *q = userdata;
1049
1050 assert(t);
1051 assert(q);
1052
1053 log_debug("Client of active query vanished, aborting query.");
1054 dns_query_complete(q, DNS_TRANSACTION_ABORTED);
1055 return 0;
1056 }
1057
1058 int dns_query_bus_track(DnsQuery *q, sd_bus_message *m) {
1059 int r;
1060
1061 assert(q);
1062 assert(m);
1063
1064 if (!q->bus_track) {
1065 r = sd_bus_track_new(sd_bus_message_get_bus(m), &q->bus_track, on_bus_track, q);
1066 if (r < 0)
1067 return r;
1068 }
1069
1070 r = sd_bus_track_add_sender(q->bus_track, m);
1071 if (r < 0)
1072 return r;
1073
1074 return 0;
1075 }
1076
1077 DnsQuestion* dns_query_question_for_protocol(DnsQuery *q, DnsProtocol protocol) {
1078 assert(q);
1079
1080 switch (protocol) {
1081
1082 case DNS_PROTOCOL_DNS:
1083 return q->question_idna;
1084
1085 case DNS_PROTOCOL_MDNS:
1086 case DNS_PROTOCOL_LLMNR:
1087 return q->question_utf8;
1088
1089 default:
1090 return NULL;
1091 }
1092 }
1093
1094 const char *dns_query_string(DnsQuery *q) {
1095 const char *name;
1096 int r;
1097
1098 /* Returns a somewhat useful human-readable lookup key string for this query */
1099
1100 if (q->request_address_string)
1101 return q->request_address_string;
1102
1103 if (q->request_address_valid) {
1104 r = in_addr_to_string(q->request_family, &q->request_address, &q->request_address_string);
1105 if (r >= 0)
1106 return q->request_address_string;
1107 }
1108
1109 name = dns_question_first_name(q->question_utf8);
1110 if (name)
1111 return name;
1112
1113 return dns_question_first_name(q->question_idna);
1114 }