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