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