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