]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-bus.c
resolved: automatically forget all learnt DNS server information when the network...
[thirdparty/systemd.git] / src / resolve / resolved-bus.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"
96aad8d1 21#include "bus-common-errors.h"
74b2466e 22#include "bus-util.h"
4ad7f276 23#include "dns-domain.h"
39d8db04 24#include "resolved-bus.h"
51323288 25#include "resolved-def.h"
c3be369f 26#include "resolved-dns-synthesize.h"
3abaabda 27#include "resolved-link-bus.h"
74b2466e 28
ad867662 29static int reply_query_state(DnsQuery *q) {
ad867662
LP
30
31 switch (q->state) {
74b2466e 32
ec2c5e43 33 case DNS_TRANSACTION_NO_SERVERS:
309e9d86 34 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
74b2466e 35
ec2c5e43 36 case DNS_TRANSACTION_TIMEOUT:
ad867662 37 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
74b2466e 38
ec2c5e43 39 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
ad867662
LP
40 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
41
818f766b
LP
42 case DNS_TRANSACTION_INVALID_REPLY:
43 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
44
7cc6ed7b
LP
45 case DNS_TRANSACTION_ERRNO:
46 return sd_bus_reply_method_errnof(q->request, q->answer_errno, "Lookup failed due to system error: %m");
ad867662 47
818f766b
LP
48 case DNS_TRANSACTION_ABORTED:
49 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_ABORTED, "Query aborted");
74b2466e 50
547973de 51 case DNS_TRANSACTION_DNSSEC_FAILED:
a761c1ca 52 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_DNSSEC_FAILED, "DNSSEC validation failed: %s",
019036a4 53 dnssec_result_to_string(q->answer_dnssec_result));
547973de 54
b2b796b8
LP
55 case DNS_TRANSACTION_NO_TRUST_ANCHOR:
56 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_TRUST_ANCHOR, "No suitable trust anchor known");
57
91adc4db
LP
58 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED:
59 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_RR_TYPE_UNSUPPORTED, "Server does not support requested resource record type");
60
edbcc1fd
LP
61 case DNS_TRANSACTION_NETWORK_DOWN:
62 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NETWORK_DOWN, "Network is down");
63
0791110f
LP
64 case DNS_TRANSACTION_NOT_FOUND:
65 /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
66 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
67 return sd_bus_reply_method_errorf(q->request, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", dns_query_string(q));
68
3bbdc31d 69 case DNS_TRANSACTION_RCODE_FAILURE: {
4afd3348 70 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
74b2466e 71
faa133f3 72 if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
23b298bc 73 sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", dns_query_string(q));
74b2466e
LP
74 else {
75 const char *rc, *n;
3347dd5e 76 char p[DECIMAL_STR_MAX(q->answer_rcode)];
74b2466e 77
faa133f3 78 rc = dns_rcode_to_string(q->answer_rcode);
74b2466e 79 if (!rc) {
faa133f3 80 sprintf(p, "%i", q->answer_rcode);
74b2466e
LP
81 rc = p;
82 }
83
63c372cb 84 n = strjoina(_BUS_ERROR_DNS, rc);
23b298bc 85 sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", dns_query_string(q), rc);
74b2466e
LP
86 }
87
ad867662 88 return sd_bus_reply_method_error(q->request, &error);
74b2466e
LP
89 }
90
ec2c5e43
LP
91 case DNS_TRANSACTION_NULL:
92 case DNS_TRANSACTION_PENDING:
ef9fb66c 93 case DNS_TRANSACTION_VALIDATING:
ec2c5e43 94 case DNS_TRANSACTION_SUCCESS:
8ba9fd9c 95 default:
ad867662
LP
96 assert_not_reached("Impossible state");
97 }
98}
74b2466e 99
78c6a153 100static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
8ba9fd9c
LP
101 int r;
102
103 assert(reply);
104 assert(rr);
105
78c6a153
LP
106 r = sd_bus_message_open_container(reply, 'r', "iiay");
107 if (r < 0)
108 return r;
109
110 r = sd_bus_message_append(reply, "i", ifindex);
8ba9fd9c
LP
111 if (r < 0)
112 return r;
113
faa133f3 114 if (rr->key->type == DNS_TYPE_A) {
0dd25fb9 115 r = sd_bus_message_append(reply, "i", AF_INET);
8ba9fd9c
LP
116 if (r < 0)
117 return r;
118
119 r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
faa133f3
LP
120
121 } else if (rr->key->type == DNS_TYPE_AAAA) {
0dd25fb9 122 r = sd_bus_message_append(reply, "i", AF_INET6);
8ba9fd9c
LP
123 if (r < 0)
124 return r;
125
126 r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
faa133f3
LP
127 } else
128 return -EAFNOSUPPORT;
129
8ba9fd9c
LP
130 if (r < 0)
131 return r;
132
8ba9fd9c
LP
133 r = sd_bus_message_close_container(reply);
134 if (r < 0)
135 return r;
136
137 return 0;
138}
139
ad867662 140static void bus_method_resolve_hostname_complete(DnsQuery *q) {
45ec7efb 141 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
4afd3348 142 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
c77d2612 143 _cleanup_free_ char *normalized = NULL;
ce736ace 144 DnsResourceRecord *rr;
45ec7efb 145 unsigned added = 0;
ce736ace 146 int ifindex, r;
74b2466e 147
ad867662 148 assert(q);
74b2466e 149
ec2c5e43 150 if (q->state != DNS_TRANSACTION_SUCCESS) {
ad867662
LP
151 r = reply_query_state(q);
152 goto finish;
153 }
74b2466e 154
45ec7efb
LP
155 r = dns_query_process_cname(q);
156 if (r == -ELOOP) {
23b298bc 157 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
45ec7efb
LP
158 goto finish;
159 }
160 if (r < 0)
161 goto finish;
7588460a 162 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
45ec7efb
LP
163 return;
164
ad867662
LP
165 r = sd_bus_message_new_method_return(q->request, &reply);
166 if (r < 0)
167 goto finish;
74b2466e 168
78c6a153 169 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
51323288
LP
170 if (r < 0)
171 goto finish;
8ba9fd9c 172
ce736ace
LP
173 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
174 DnsQuestion *question;
23b298bc 175
ce736ace 176 question = dns_query_question_for_protocol(q, q->answer_protocol);
23b298bc 177
ce736ace
LP
178 r = dns_question_matches_rr(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
179 if (r < 0)
180 goto finish;
181 if (r == 0)
182 continue;
74b2466e 183
ce736ace
LP
184 r = append_address(reply, rr, ifindex);
185 if (r < 0)
186 goto finish;
74b2466e 187
ce736ace
LP
188 if (!canonical)
189 canonical = dns_resource_record_ref(rr);
309e9d86 190
313cefa1 191 added++;
8ba9fd9c 192 }
74b2466e 193
45ec7efb 194 if (added <= 0) {
23b298bc 195 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
45ec7efb 196 goto finish;
74b2466e
LP
197 }
198
ad867662
LP
199 r = sd_bus_message_close_container(reply);
200 if (r < 0)
201 goto finish;
202
c77d2612
LP
203 /* The key names are not necessarily normalized, make sure that they are when we return them to our bus
204 * clients. */
1c02e7ba 205 r = dns_name_normalize(dns_resource_key_name(canonical->key), &normalized);
c77d2612
LP
206 if (r < 0)
207 goto finish;
208
45ec7efb 209 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
309e9d86 210 assert(canonical);
45ec7efb
LP
211 r = sd_bus_message_append(
212 reply, "st",
c77d2612 213 normalized,
28830a64 214 SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
309e9d86
LP
215 if (r < 0)
216 goto finish;
217
ad867662 218 r = sd_bus_send(q->manager->bus, reply, NULL);
ad867662 219
74b2466e 220finish:
2d4c5cbc 221 if (r < 0) {
da927ba9 222 log_error_errno(r, "Failed to send hostname reply: %m");
45ec7efb 223 sd_bus_reply_method_errno(q->request, r, NULL);
2d4c5cbc 224 }
74b2466e
LP
225
226 dns_query_free(q);
227}
228
45ec7efb 229static int check_ifindex_flags(int ifindex, uint64_t *flags, uint64_t ok, sd_bus_error *error) {
51323288
LP
230 assert(flags);
231
232 if (ifindex < 0)
233 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
234
45ec7efb 235 if (*flags & ~(SD_RESOLVED_PROTOCOLS_ALL|SD_RESOLVED_NO_CNAME|ok))
51323288
LP
236 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
237
45ec7efb
LP
238 if ((*flags & SD_RESOLVED_PROTOCOLS_ALL) == 0) /* If no protocol is enabled, enable all */
239 *flags |= SD_RESOLVED_PROTOCOLS_ALL;
51323288
LP
240
241 return 0;
242}
243
c3be369f
LP
244static int parse_as_address(sd_bus_message *m, int ifindex, const char *hostname, int family, uint64_t flags) {
245 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
246 _cleanup_free_ char *canonical = NULL;
247 union in_addr_union parsed;
94831eae 248 int r, ff, parsed_ifindex = 0;
c3be369f
LP
249
250 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
251 * let's not attempt to look it up. */
252
94831eae 253 r = in_addr_ifindex_from_string_auto(hostname, &ff, &parsed, &parsed_ifindex);
c3be369f
LP
254 if (r < 0) /* not an address */
255 return 0;
256
257 if (family != AF_UNSPEC && ff != family)
258 return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address is not of the requested family.");
94831eae
LP
259 if (ifindex > 0 && parsed_ifindex > 0 && parsed_ifindex != ifindex)
260 return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address interface index does not match requested interface.");
261
262 if (parsed_ifindex > 0)
263 ifindex = parsed_ifindex;
c3be369f
LP
264
265 r = sd_bus_message_new_method_return(m, &reply);
266 if (r < 0)
267 return r;
268
269 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
270 if (r < 0)
271 return r;
272
273 r = sd_bus_message_open_container(reply, 'r', "iiay");
274 if (r < 0)
275 return r;
276
277 r = sd_bus_message_append(reply, "ii", ifindex, ff);
278 if (r < 0)
279 return r;
280
281 r = sd_bus_message_append_array(reply, 'y', &parsed, FAMILY_ADDRESS_SIZE(ff));
282 if (r < 0)
283 return r;
284
285 r = sd_bus_message_close_container(reply);
286 if (r < 0)
287 return r;
288
289 r = sd_bus_message_close_container(reply);
290 if (r < 0)
291 return r;
292
293 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
294 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
295 * omissions are always done the same way). */
94831eae 296 r = in_addr_ifindex_to_string(ff, &parsed, ifindex, &canonical);
c3be369f
LP
297 if (r < 0)
298 return r;
299
300 r = sd_bus_message_append(reply, "st", canonical,
301 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags), ff, true));
302 if (r < 0)
303 return r;
304
305 return sd_bus_send(sd_bus_message_get_bus(m), reply, NULL);
306}
307
19070062 308static int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
23b298bc 309 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
74b2466e
LP
310 Manager *m = userdata;
311 const char *hostname;
51323288
LP
312 int family, ifindex;
313 uint64_t flags;
74b2466e 314 DnsQuery *q;
74b2466e
LP
315 int r;
316
74b2466e
LP
317 assert(message);
318 assert(m);
319
45ec7efb
LP
320 assert_cc(sizeof(int) == sizeof(int32_t));
321
51323288 322 r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
74b2466e
LP
323 if (r < 0)
324 return r;
325
326 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
0dd25fb9 327 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
74b2466e 328
c3be369f 329 r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_SEARCH, error);
7b9f7afc 330 if (r < 0)
45ec7efb 331 return r;
74b2466e 332
c3be369f
LP
333 r = parse_as_address(message, ifindex, hostname, family, flags);
334 if (r != 0)
335 return r;
336
337 r = dns_name_is_valid(hostname);
51323288
LP
338 if (r < 0)
339 return r;
c3be369f
LP
340 if (r == 0)
341 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
51323288 342
23b298bc
LP
343 r = dns_question_new_address(&question_utf8, family, hostname, false);
344 if (r < 0)
345 return r;
346
347 r = dns_question_new_address(&question_idna, family, hostname, true);
ad1f3fe6 348 if (r < 0 && r != -EALREADY)
45ec7efb 349 return r;
74b2466e 350
ad1f3fe6 351 r = dns_query_new(m, &q, question_utf8, question_idna ?: question_utf8, ifindex, flags);
74b2466e
LP
352 if (r < 0)
353 return r;
354
355 q->request = sd_bus_message_ref(message);
356 q->request_family = family;
74b2466e 357 q->complete = bus_method_resolve_hostname_complete;
011696f7 358 q->suppress_unroutable_family = family == AF_UNSPEC;
74b2466e 359
966c66e3 360 r = dns_query_bus_track(q, message);
82bd6ddd 361 if (r < 0)
45ec7efb 362 goto fail;
82bd6ddd 363
322345fd 364 r = dns_query_go(q);
45ec7efb
LP
365 if (r < 0)
366 goto fail;
74b2466e
LP
367
368 return 1;
45ec7efb
LP
369
370fail:
371 dns_query_free(q);
372 return r;
74b2466e
LP
373}
374
375static void bus_method_resolve_address_complete(DnsQuery *q) {
4afd3348 376 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
23b298bc 377 DnsQuestion *question;
45ec7efb
LP
378 DnsResourceRecord *rr;
379 unsigned added = 0;
380 int ifindex, r;
74b2466e
LP
381
382 assert(q);
383
ec2c5e43 384 if (q->state != DNS_TRANSACTION_SUCCESS) {
ad867662
LP
385 r = reply_query_state(q);
386 goto finish;
387 }
74b2466e 388
95d46fca
TG
389 r = dns_query_process_cname(q);
390 if (r == -ELOOP) {
23b298bc 391 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
95d46fca
TG
392 goto finish;
393 }
394 if (r < 0)
395 goto finish;
7588460a 396 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
95d46fca 397 return;
45ec7efb 398
ad867662
LP
399 r = sd_bus_message_new_method_return(q->request, &reply);
400 if (r < 0)
401 goto finish;
74b2466e 402
78c6a153 403 r = sd_bus_message_open_container(reply, 'a', "(is)");
ad867662
LP
404 if (r < 0)
405 goto finish;
74b2466e 406
23b298bc 407 question = dns_query_question_for_protocol(q, q->answer_protocol);
74b2466e 408
23b298bc 409 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
c77d2612
LP
410 _cleanup_free_ char *normalized = NULL;
411
23b298bc
LP
412 r = dns_question_matches_rr(question, rr, NULL);
413 if (r < 0)
414 goto finish;
415 if (r == 0)
416 continue;
74b2466e 417
c77d2612
LP
418 r = dns_name_normalize(rr->ptr.name, &normalized);
419 if (r < 0)
420 goto finish;
421
422 r = sd_bus_message_append(reply, "(is)", ifindex, normalized);
23b298bc
LP
423 if (r < 0)
424 goto finish;
425
313cefa1 426 added++;
ad867662 427 }
74b2466e 428
45ec7efb 429 if (added <= 0) {
ad867662 430 _cleanup_free_ char *ip = NULL;
74b2466e 431
164d025d
ZJS
432 (void) in_addr_to_string(q->request_family, &q->request_address, &ip);
433 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR,
434 "Address '%s' does not have any RR of requested type", strnull(ip));
ad867662 435 goto finish;
74b2466e
LP
436 }
437
ad867662
LP
438 r = sd_bus_message_close_container(reply);
439 if (r < 0)
440 goto finish;
74b2466e 441
28830a64 442 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
51323288
LP
443 if (r < 0)
444 goto finish;
445
ad867662 446 r = sd_bus_send(q->manager->bus, reply, NULL);
74b2466e
LP
447
448finish:
2d4c5cbc 449 if (r < 0) {
da927ba9 450 log_error_errno(r, "Failed to send address reply: %m");
45ec7efb 451 sd_bus_reply_method_errno(q->request, r, NULL);
2d4c5cbc 452 }
74b2466e
LP
453
454 dns_query_free(q);
455}
456
19070062 457static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
faa133f3 458 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
74b2466e 459 Manager *m = userdata;
faa133f3 460 int family, ifindex;
51323288 461 uint64_t flags;
74b2466e 462 const void *d;
74b2466e
LP
463 DnsQuery *q;
464 size_t sz;
465 int r;
466
74b2466e
LP
467 assert(message);
468 assert(m);
469
45ec7efb
LP
470 assert_cc(sizeof(int) == sizeof(int32_t));
471
51323288 472 r = sd_bus_message_read(message, "ii", &ifindex, &family);
74b2466e
LP
473 if (r < 0)
474 return r;
475
476 if (!IN_SET(family, AF_INET, AF_INET6))
0dd25fb9 477 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
74b2466e
LP
478
479 r = sd_bus_message_read_array(message, 'y', &d, &sz);
480 if (r < 0)
481 return r;
482
0dd25fb9 483 if (sz != FAMILY_ADDRESS_SIZE(family))
74b2466e
LP
484 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
485
51323288
LP
486 r = sd_bus_message_read(message, "t", &flags);
487 if (r < 0)
488 return r;
489
45ec7efb 490 r = check_ifindex_flags(ifindex, &flags, 0, error);
faa133f3
LP
491 if (r < 0)
492 return r;
493
45ec7efb 494 r = dns_question_new_reverse(&question, family, d);
74b2466e
LP
495 if (r < 0)
496 return r;
497
23b298bc 498 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
74b2466e
LP
499 if (r < 0)
500 return r;
501
502 q->request = sd_bus_message_ref(message);
503 q->request_family = family;
504 memcpy(&q->request_address, d, sz);
505 q->complete = bus_method_resolve_address_complete;
506
966c66e3 507 r = dns_query_bus_track(q, message);
82bd6ddd 508 if (r < 0)
45ec7efb 509 goto fail;
82bd6ddd 510
322345fd 511 r = dns_query_go(q);
45ec7efb
LP
512 if (r < 0)
513 goto fail;
74b2466e
LP
514
515 return 1;
45ec7efb
LP
516
517fail:
518 dns_query_free(q);
519 return r;
520}
521
522static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
45ec7efb
LP
523 int r;
524
525 assert(m);
526 assert(rr);
527
528 r = sd_bus_message_open_container(m, 'r', "iqqay");
529 if (r < 0)
530 return r;
531
532 r = sd_bus_message_append(m, "iqq",
533 ifindex,
534 rr->key->class,
535 rr->key->type);
536 if (r < 0)
537 return r;
538
4e2d538f 539 r = dns_resource_record_to_wire_format(rr, false);
45ec7efb
LP
540 if (r < 0)
541 return r;
542
4e2d538f 543 r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
45ec7efb
LP
544 if (r < 0)
545 return r;
546
547 return sd_bus_message_close_container(m);
74b2466e
LP
548}
549
2d4c5cbc 550static void bus_method_resolve_record_complete(DnsQuery *q) {
4afd3348 551 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
23b298bc
LP
552 DnsResourceRecord *rr;
553 DnsQuestion *question;
45ec7efb 554 unsigned added = 0;
23b298bc 555 int ifindex;
2d4c5cbc
LP
556 int r;
557
558 assert(q);
559
ec2c5e43 560 if (q->state != DNS_TRANSACTION_SUCCESS) {
2d4c5cbc
LP
561 r = reply_query_state(q);
562 goto finish;
563 }
564
45ec7efb
LP
565 r = dns_query_process_cname(q);
566 if (r == -ELOOP) {
23b298bc 567 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
45ec7efb
LP
568 goto finish;
569 }
570 if (r < 0)
571 goto finish;
7588460a 572 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
45ec7efb
LP
573 return;
574
2d4c5cbc
LP
575 r = sd_bus_message_new_method_return(q->request, &reply);
576 if (r < 0)
577 goto finish;
578
78c6a153 579 r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
2d4c5cbc
LP
580 if (r < 0)
581 goto finish;
582
23b298bc 583 question = dns_query_question_for_protocol(q, q->answer_protocol);
2d4c5cbc 584
23b298bc
LP
585 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
586 r = dns_question_matches_rr(question, rr, NULL);
587 if (r < 0)
588 goto finish;
589 if (r == 0)
590 continue;
2d4c5cbc 591
23b298bc
LP
592 r = bus_message_append_rr(reply, rr, ifindex);
593 if (r < 0)
594 goto finish;
2d4c5cbc 595
313cefa1 596 added++;
2d4c5cbc
LP
597 }
598
599 if (added <= 0) {
23b298bc 600 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", dns_query_string(q));
2d4c5cbc
LP
601 goto finish;
602 }
603
604 r = sd_bus_message_close_container(reply);
605 if (r < 0)
606 goto finish;
607
28830a64 608 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
51323288
LP
609 if (r < 0)
610 goto finish;
611
2d4c5cbc
LP
612 r = sd_bus_send(q->manager->bus, reply, NULL);
613
614finish:
615 if (r < 0) {
da927ba9 616 log_error_errno(r, "Failed to send record reply: %m");
45ec7efb 617 sd_bus_reply_method_errno(q->request, r, NULL);
2d4c5cbc
LP
618 }
619
620 dns_query_free(q);
621}
622
19070062 623static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2d4c5cbc
LP
624 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
625 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
2d4c5cbc 626 Manager *m = userdata;
2d4c5cbc
LP
627 uint16_t class, type;
628 const char *name;
51323288
LP
629 int r, ifindex;
630 uint64_t flags;
631 DnsQuery *q;
2d4c5cbc 632
2d4c5cbc
LP
633 assert(message);
634 assert(m);
635
45ec7efb
LP
636 assert_cc(sizeof(int) == sizeof(int32_t));
637
51323288 638 r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
2d4c5cbc
LP
639 if (r < 0)
640 return r;
641
45ec7efb 642 r = dns_name_is_valid(name);
7b9f7afc 643 if (r < 0)
45ec7efb
LP
644 return r;
645 if (r == 0)
7b9f7afc
LP
646 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
647
c463eb78 648 if (!dns_type_is_valid_query(type))
eee026a7 649 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified resource record type %" PRIu16 " may not be used in a query.", type);
6ebd1e33
LP
650 if (dns_type_is_zone_transer(type))
651 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Zone transfers not permitted via this programming interface.");
d0129ddb 652 if (dns_type_is_obsolete(type))
eee026a7 653 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
c463eb78 654
45ec7efb 655 r = check_ifindex_flags(ifindex, &flags, 0, error);
51323288
LP
656 if (r < 0)
657 return r;
658
2d4c5cbc
LP
659 question = dns_question_new(1);
660 if (!question)
661 return -ENOMEM;
662
663 key = dns_resource_key_new(class, type, name);
664 if (!key)
665 return -ENOMEM;
666
667 r = dns_question_add(question, key);
668 if (r < 0)
669 return r;
670
23b298bc 671 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
2d4c5cbc
LP
672 if (r < 0)
673 return r;
674
17c8de63
LP
675 /* Let's request that the TTL is fixed up for locally cached entries, after all we return it in the wire format
676 * blob */
677 q->clamp_ttl = true;
678
2d4c5cbc 679 q->request = sd_bus_message_ref(message);
2d4c5cbc
LP
680 q->complete = bus_method_resolve_record_complete;
681
966c66e3 682 r = dns_query_bus_track(q, message);
82bd6ddd 683 if (r < 0)
45ec7efb 684 goto fail;
82bd6ddd 685
2d4c5cbc 686 r = dns_query_go(q);
45ec7efb
LP
687 if (r < 0)
688 goto fail;
689
690 return 1;
691
692fail:
693 dns_query_free(q);
694 return r;
695}
696
697static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
698 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
c77d2612 699 _cleanup_free_ char *normalized = NULL;
45ec7efb
LP
700 DnsQuery *aux;
701 int r;
702
703 assert(q);
704 assert(reply);
705 assert(rr);
706 assert(rr->key);
707
708 if (rr->key->type != DNS_TYPE_SRV)
709 return 0;
710
711 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
712 /* First, let's see if we could find an appropriate A or AAAA
713 * record for the SRV record */
714 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
715 DnsResourceRecord *zz;
23b298bc 716 DnsQuestion *question;
45ec7efb
LP
717
718 if (aux->state != DNS_TRANSACTION_SUCCESS)
719 continue;
720 if (aux->auxiliary_result != 0)
721 continue;
722
23b298bc
LP
723 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
724
725 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
45ec7efb
LP
726 if (r < 0)
727 return r;
728 if (r == 0)
729 continue;
730
731 DNS_ANSWER_FOREACH(zz, aux->answer) {
732
23b298bc 733 r = dns_question_matches_rr(question, zz, NULL);
45ec7efb
LP
734 if (r < 0)
735 return r;
736 if (r == 0)
737 continue;
738
739 canonical = dns_resource_record_ref(zz);
740 break;
741 }
742
743 if (canonical)
744 break;
745 }
746
747 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
748 if (!canonical)
749 return 0;
750 }
751
752 r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
753 if (r < 0)
754 return r;
755
c77d2612
LP
756 r = dns_name_normalize(rr->srv.name, &normalized);
757 if (r < 0)
758 return r;
759
45ec7efb
LP
760 r = sd_bus_message_append(
761 reply,
762 "qqqs",
c77d2612 763 rr->srv.priority, rr->srv.weight, rr->srv.port, normalized);
45ec7efb
LP
764 if (r < 0)
765 return r;
766
767 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
768 if (r < 0)
769 return r;
770
771 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
772 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
773 DnsResourceRecord *zz;
23b298bc 774 DnsQuestion *question;
45ec7efb
LP
775 int ifindex;
776
777 if (aux->state != DNS_TRANSACTION_SUCCESS)
778 continue;
779 if (aux->auxiliary_result != 0)
780 continue;
781
23b298bc
LP
782 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
783
784 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
45ec7efb
LP
785 if (r < 0)
786 return r;
787 if (r == 0)
788 continue;
789
790 DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
791
23b298bc 792 r = dns_question_matches_rr(question, zz, NULL);
45ec7efb
LP
793 if (r < 0)
794 return r;
795 if (r == 0)
796 continue;
797
798 r = append_address(reply, zz, ifindex);
799 if (r < 0)
800 return r;
801 }
802 }
803 }
804
805 r = sd_bus_message_close_container(reply);
806 if (r < 0)
807 return r;
808
c77d2612
LP
809 if (canonical) {
810 normalized = mfree(normalized);
811
1c02e7ba 812 r = dns_name_normalize(dns_resource_key_name(canonical->key), &normalized);
c77d2612
LP
813 if (r < 0)
814 return r;
815 }
816
45ec7efb
LP
817 /* Note that above we appended the hostname as encoded in the
818 * SRV, and here the canonical hostname this maps to. */
c77d2612 819 r = sd_bus_message_append(reply, "s", normalized);
45ec7efb
LP
820 if (r < 0)
821 return r;
822
823 r = sd_bus_message_close_container(reply);
824 if (r < 0)
825 return r;
826
827 return 1;
828}
829
830static int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
831 DnsTxtItem *i;
832 int r;
833
834 assert(reply);
835 assert(rr);
836 assert(rr->key);
837
838 if (rr->key->type != DNS_TYPE_TXT)
839 return 0;
840
841 LIST_FOREACH(items, i, rr->txt.items) {
842
843 if (i->length <= 0)
844 continue;
845
846 r = sd_bus_message_append_array(reply, 'y', i->data, i->length);
847 if (r < 0)
848 return r;
849 }
850
851 return 1;
852}
853
854static void resolve_service_all_complete(DnsQuery *q) {
855 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
856 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
857 _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL;
23b298bc
LP
858 DnsQuestion *question;
859 DnsResourceRecord *rr;
860 unsigned added = 0;
45ec7efb 861 DnsQuery *aux;
45ec7efb
LP
862 int r;
863
864 assert(q);
865
866 if (q->block_all_complete > 0)
867 return;
868
869 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
870 DnsQuery *bad = NULL;
871 bool have_success = false;
872
873 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
874
875 switch (aux->state) {
876
877 case DNS_TRANSACTION_PENDING:
878 /* If an auxiliary query is still pending, let's wait */
879 return;
880
881 case DNS_TRANSACTION_SUCCESS:
882 if (aux->auxiliary_result == 0)
883 have_success = true;
884 else
885 bad = aux;
886 break;
887
888 default:
889 bad = aux;
890 break;
891 }
892 }
893
894 if (!have_success) {
895 /* We can only return one error, hence pick the last error we encountered */
896
897 assert(bad);
898
899 if (bad->state == DNS_TRANSACTION_SUCCESS) {
900 assert(bad->auxiliary_result != 0);
901
902 if (bad->auxiliary_result == -ELOOP) {
23b298bc 903 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad));
45ec7efb
LP
904 goto finish;
905 }
906
907 r = bad->auxiliary_result;
908 goto finish;
909 }
910
911 r = reply_query_state(bad);
912 goto finish;
913 }
914 }
915
916 r = sd_bus_message_new_method_return(q->request, &reply);
917 if (r < 0)
918 goto finish;
919
920 r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)");
921 if (r < 0)
922 goto finish;
923
23b298bc
LP
924 question = dns_query_question_for_protocol(q, q->answer_protocol);
925 DNS_ANSWER_FOREACH(rr, q->answer) {
926 r = dns_question_matches_rr(question, rr, NULL);
927 if (r < 0)
928 goto finish;
929 if (r == 0)
930 continue;
45ec7efb 931
23b298bc
LP
932 r = append_srv(q, reply, rr);
933 if (r < 0)
934 goto finish;
935 if (r == 0) /* not an SRV record */
936 continue;
45ec7efb 937
23b298bc
LP
938 if (!canonical)
939 canonical = dns_resource_record_ref(rr);
45ec7efb 940
23b298bc 941 added++;
45ec7efb
LP
942 }
943
944 if (added <= 0) {
23b298bc 945 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
45ec7efb
LP
946 goto finish;
947 }
948
949 r = sd_bus_message_close_container(reply);
950 if (r < 0)
951 goto finish;
952
953 r = sd_bus_message_open_container(reply, 'a', "ay");
954 if (r < 0)
955 goto finish;
956
23b298bc
LP
957 DNS_ANSWER_FOREACH(rr, q->answer) {
958 r = dns_question_matches_rr(question, rr, NULL);
959 if (r < 0)
960 goto finish;
961 if (r == 0)
962 continue;
45ec7efb 963
23b298bc
LP
964 r = append_txt(reply, rr);
965 if (r < 0)
966 goto finish;
45ec7efb
LP
967 }
968
969 r = sd_bus_message_close_container(reply);
970 if (r < 0)
971 goto finish;
972
973 assert(canonical);
1c02e7ba 974 r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain);
45ec7efb
LP
975 if (r < 0)
976 goto finish;
977
978 r = sd_bus_message_append(
979 reply,
980 "ssst",
981 name, type, domain,
28830a64 982 SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
45ec7efb
LP
983 if (r < 0)
984 goto finish;
985
986 r = sd_bus_send(q->manager->bus, reply, NULL);
987
988finish:
989 if (r < 0) {
990 log_error_errno(r, "Failed to send service reply: %m");
991 sd_bus_reply_method_errno(q->request, r, NULL);
992 }
993
994 dns_query_free(q);
995}
996
997static void resolve_service_hostname_complete(DnsQuery *q) {
998 int r;
999
1000 assert(q);
1001 assert(q->auxiliary_for);
1002
1003 if (q->state != DNS_TRANSACTION_SUCCESS) {
1004 resolve_service_all_complete(q->auxiliary_for);
1005 return;
1006 }
1007
1008 r = dns_query_process_cname(q);
7588460a 1009 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
45ec7efb
LP
1010 return;
1011
1012 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1013 q->auxiliary_result = r;
1014 resolve_service_all_complete(q->auxiliary_for);
1015}
1016
1017static int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) {
1018 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
1019 DnsQuery *aux;
1020 int r;
1021
1022 assert(q);
1023 assert(rr);
1024 assert(rr->key);
1025 assert(rr->key->type == DNS_TYPE_SRV);
1026
1027 /* OK, we found an SRV record for the service. Let's resolve
1028 * the hostname included in it */
1029
23b298bc 1030 r = dns_question_new_address(&question, q->request_family, rr->srv.name, false);
45ec7efb
LP
1031 if (r < 0)
1032 return r;
1033
23b298bc 1034 r = dns_query_new(q->manager, &aux, question, question, ifindex, q->flags|SD_RESOLVED_NO_SEARCH);
45ec7efb
LP
1035 if (r < 0)
1036 return r;
1037
1038 aux->request_family = q->request_family;
1039 aux->complete = resolve_service_hostname_complete;
1040
1041 r = dns_query_make_auxiliary(aux, q);
1042 if (r == -EAGAIN) {
1043 /* Too many auxiliary lookups? If so, don't complain,
1044 * let's just not add this one, we already have more
1045 * than enough */
1046
1047 dns_query_free(aux);
1048 return 0;
1049 }
1050 if (r < 0)
1051 goto fail;
1052
1053 /* Note that auxiliary queries do not track the original bus
1054 * client, only the primary request does that. */
1055
1056 r = dns_query_go(aux);
1057 if (r < 0)
1058 goto fail;
1059
1060 return 1;
1061
1062fail:
1063 dns_query_free(aux);
1064 return r;
1065}
1066
1067static void bus_method_resolve_service_complete(DnsQuery *q) {
23b298bc
LP
1068 bool has_root_domain = false;
1069 DnsResourceRecord *rr;
1070 DnsQuestion *question;
45ec7efb 1071 unsigned found = 0;
23b298bc 1072 int ifindex, r;
45ec7efb
LP
1073
1074 assert(q);
1075
1076 if (q->state != DNS_TRANSACTION_SUCCESS) {
1077 r = reply_query_state(q);
1078 goto finish;
1079 }
1080
1081 r = dns_query_process_cname(q);
1082 if (r == -ELOOP) {
23b298bc 1083 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
45ec7efb
LP
1084 goto finish;
1085 }
1086 if (r < 0)
1087 goto finish;
7588460a 1088 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
45ec7efb
LP
1089 return;
1090
23b298bc 1091 question = dns_query_question_for_protocol(q, q->answer_protocol);
45ec7efb 1092
23b298bc
LP
1093 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
1094 r = dns_question_matches_rr(question, rr, NULL);
1095 if (r < 0)
1096 goto finish;
1097 if (r == 0)
1098 continue;
45ec7efb 1099
23b298bc
LP
1100 if (rr->key->type != DNS_TYPE_SRV)
1101 continue;
9a1f0c28 1102
23b298bc
LP
1103 if (dns_name_is_root(rr->srv.name)) {
1104 has_root_domain = true;
1105 continue;
1106 }
45ec7efb 1107
23b298bc 1108 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
313cefa1 1109 q->block_all_complete++;
23b298bc 1110 r = resolve_service_hostname(q, rr, ifindex);
313cefa1 1111 q->block_all_complete--;
45ec7efb 1112
23b298bc
LP
1113 if (r < 0)
1114 goto finish;
45ec7efb 1115 }
9a1f0c28 1116
23b298bc
LP
1117 found++;
1118 }
9a1f0c28 1119
23b298bc
LP
1120 if (has_root_domain && found <= 0) {
1121 /* If there's exactly one SRV RR and it uses
1122 * the root domain as host name, then the
1123 * service is explicitly not offered on the
1124 * domain. Report this as a recognizable
1125 * error. See RFC 2782, Section "Usage
1126 * Rules". */
1127 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide the requested service", dns_query_string(q));
1128 goto finish;
45ec7efb
LP
1129 }
1130
1131 if (found <= 0) {
23b298bc 1132 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
45ec7efb
LP
1133 goto finish;
1134 }
1135
1136 /* Maybe we are already finished? check now... */
1137 resolve_service_all_complete(q);
1138 return;
1139
1140finish:
2d4c5cbc 1141 if (r < 0) {
45ec7efb
LP
1142 log_error_errno(r, "Failed to send service reply: %m");
1143 sd_bus_reply_method_errno(q->request, r, NULL);
1144 }
1145
1146 dns_query_free(q);
1147}
1148
1149static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
23b298bc
LP
1150 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
1151 const char *name, *type, *domain;
45ec7efb
LP
1152 Manager *m = userdata;
1153 int family, ifindex;
1154 uint64_t flags;
1155 DnsQuery *q;
1156 int r;
1157
1158 assert(message);
1159 assert(m);
1160
1161 assert_cc(sizeof(int) == sizeof(int32_t));
1162
1163 r = sd_bus_message_read(message, "isssit", &ifindex, &name, &type, &domain, &family, &flags);
1164 if (r < 0)
2d4c5cbc 1165 return r;
45ec7efb
LP
1166
1167 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
1168 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
1169
1170 if (isempty(name))
1171 name = NULL;
23b298bc
LP
1172 else if (!dns_service_name_is_valid(name))
1173 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid service name '%s'", name);
2d4c5cbc 1174
45ec7efb
LP
1175 if (isempty(type))
1176 type = NULL;
7e8131e9
LP
1177 else if (!dns_srv_type_is_valid(type))
1178 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid SRV service type '%s'", type);
45ec7efb
LP
1179
1180 r = dns_name_is_valid(domain);
1181 if (r < 0)
1182 return r;
1183 if (r == 0)
1184 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid domain '%s'", domain);
1185
1186 if (name && !type)
1187 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type.");
1188
1189 r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS, error);
1190 if (r < 0)
1191 return r;
1192
23b298bc
LP
1193 r = dns_question_new_service(&question_utf8, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), false);
1194 if (r < 0)
1195 return r;
45ec7efb 1196
23b298bc 1197 r = dns_question_new_service(&question_idna, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), true);
45ec7efb
LP
1198 if (r < 0)
1199 return r;
1200
23b298bc 1201 r = dns_query_new(m, &q, question_utf8, question_idna, ifindex, flags|SD_RESOLVED_NO_SEARCH);
45ec7efb
LP
1202 if (r < 0)
1203 return r;
1204
1205 q->request = sd_bus_message_ref(message);
1206 q->request_family = family;
1207 q->complete = bus_method_resolve_service_complete;
1208
1209 r = dns_query_bus_track(q, message);
1210 if (r < 0)
1211 goto fail;
1212
1213 r = dns_query_go(q);
1214 if (r < 0)
1215 goto fail;
1216
2d4c5cbc 1217 return 1;
45ec7efb
LP
1218
1219fail:
1220 dns_query_free(q);
1221 return r;
2d4c5cbc
LP
1222}
1223
3abaabda 1224int bus_dns_server_append(sd_bus_message *reply, DnsServer *s, bool with_ifindex) {
7f220d94
LP
1225 int r;
1226
1227 assert(reply);
1228 assert(s);
1229
3abaabda 1230 r = sd_bus_message_open_container(reply, 'r', with_ifindex ? "iiay" : "iay");
7f220d94
LP
1231 if (r < 0)
1232 return r;
1233
3abaabda 1234 if (with_ifindex) {
2817157b 1235 r = sd_bus_message_append(reply, "i", dns_server_ifindex(s));
3abaabda
LP
1236 if (r < 0)
1237 return r;
1238 }
1239
1240 r = sd_bus_message_append(reply, "i", s->family);
7f220d94
LP
1241 if (r < 0)
1242 return r;
1243
1244 r = sd_bus_message_append_array(reply, 'y', &s->address, FAMILY_ADDRESS_SIZE(s->family));
1245 if (r < 0)
1246 return r;
1247
1248 return sd_bus_message_close_container(reply);
1249}
1250
1251static int bus_property_get_dns_servers(
1252 sd_bus *bus,
1253 const char *path,
1254 const char *interface,
1255 const char *property,
1256 sd_bus_message *reply,
1257 void *userdata,
1258 sd_bus_error *error) {
1259
1260 Manager *m = userdata;
1261 unsigned c = 0;
1262 DnsServer *s;
1263 Iterator i;
1264 Link *l;
1265 int r;
1266
1267 assert(reply);
1268 assert(m);
1269
1270 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
1271 if (r < 0)
1272 return r;
1273
1274 LIST_FOREACH(servers, s, m->dns_servers) {
3abaabda 1275 r = bus_dns_server_append(reply, s, true);
7f220d94
LP
1276 if (r < 0)
1277 return r;
1278
1279 c++;
1280 }
1281
1282 HASHMAP_FOREACH(l, m->links, i) {
1283 LIST_FOREACH(servers, s, l->dns_servers) {
3abaabda 1284 r = bus_dns_server_append(reply, s, true);
7f220d94
LP
1285 if (r < 0)
1286 return r;
1287 c++;
1288 }
1289 }
1290
1291 if (c == 0) {
1292 LIST_FOREACH(servers, s, m->fallback_dns_servers) {
3abaabda 1293 r = bus_dns_server_append(reply, s, true);
7f220d94
LP
1294 if (r < 0)
1295 return r;
1296 }
1297 }
1298
1299 return sd_bus_message_close_container(reply);
1300}
1301
ee116b54 1302static int bus_property_get_domains(
7f220d94
LP
1303 sd_bus *bus,
1304 const char *path,
1305 const char *interface,
1306 const char *property,
1307 sd_bus_message *reply,
1308 void *userdata,
1309 sd_bus_error *error) {
1310
1311 Manager *m = userdata;
1312 DnsSearchDomain *d;
1313 Iterator i;
1314 Link *l;
1315 int r;
1316
1317 assert(reply);
1318 assert(m);
1319
ad44b56b 1320 r = sd_bus_message_open_container(reply, 'a', "(isb)");
7f220d94
LP
1321 if (r < 0)
1322 return r;
1323
1324 LIST_FOREACH(domains, d, m->search_domains) {
ad44b56b 1325 r = sd_bus_message_append(reply, "(isb)", 0, d->name, d->route_only);
7f220d94
LP
1326 if (r < 0)
1327 return r;
1328 }
1329
1330 HASHMAP_FOREACH(l, m->links, i) {
1331 LIST_FOREACH(domains, d, l->search_domains) {
ad44b56b 1332 r = sd_bus_message_append(reply, "(isb)", l->ifindex, d->name, d->route_only);
7f220d94
LP
1333 if (r < 0)
1334 return r;
1335 }
1336 }
1337
1338 return sd_bus_message_close_container(reply);
1339}
1340
a150ff5e
LP
1341static int bus_property_get_transaction_statistics(
1342 sd_bus *bus,
1343 const char *path,
1344 const char *interface,
1345 const char *property,
1346 sd_bus_message *reply,
1347 void *userdata,
1348 sd_bus_error *error) {
1349
1350 Manager *m = userdata;
1351
1352 assert(reply);
1353 assert(m);
1354
1355 return sd_bus_message_append(reply, "(tt)",
1356 (uint64_t) hashmap_size(m->dns_transactions),
1357 (uint64_t) m->n_transactions_total);
1358}
1359
1360static int bus_property_get_cache_statistics(
1361 sd_bus *bus,
1362 const char *path,
1363 const char *interface,
1364 const char *property,
1365 sd_bus_message *reply,
1366 void *userdata,
1367 sd_bus_error *error) {
1368
1369 uint64_t size = 0, hit = 0, miss = 0;
1370 Manager *m = userdata;
1371 DnsScope *s;
1372
1373 assert(reply);
1374 assert(m);
1375
1376 LIST_FOREACH(scopes, s, m->dns_scopes) {
1377 size += dns_cache_size(&s->cache);
1378 hit += s->cache.n_hit;
1379 miss += s->cache.n_miss;
1380 }
1381
1382 return sd_bus_message_append(reply, "(ttt)", size, hit, miss);
1383}
1384
1385static int bus_property_get_dnssec_statistics(
1386 sd_bus *bus,
1387 const char *path,
1388 const char *interface,
1389 const char *property,
1390 sd_bus_message *reply,
1391 void *userdata,
1392 sd_bus_error *error) {
1393
1394 Manager *m = userdata;
1395
1396 assert(reply);
1397 assert(m);
1398
1399 return sd_bus_message_append(reply, "(tttt)",
59c5b597
LP
1400 (uint64_t) m->n_dnssec_verdict[DNSSEC_SECURE],
1401 (uint64_t) m->n_dnssec_verdict[DNSSEC_INSECURE],
1402 (uint64_t) m->n_dnssec_verdict[DNSSEC_BOGUS],
1403 (uint64_t) m->n_dnssec_verdict[DNSSEC_INDETERMINATE]);
a150ff5e
LP
1404}
1405
593f665c
LP
1406static int bus_property_get_dnssec_supported(
1407 sd_bus *bus,
1408 const char *path,
1409 const char *interface,
1410 const char *property,
1411 sd_bus_message *reply,
1412 void *userdata,
1413 sd_bus_error *error) {
1414
1415 Manager *m = userdata;
593f665c
LP
1416
1417 assert(reply);
1418 assert(m);
1419
c69fa7e3 1420 return sd_bus_message_append(reply, "b", manager_dnssec_supported(m));
593f665c
LP
1421}
1422
476798b1
LP
1423static int bus_property_get_ntas(
1424 sd_bus *bus,
1425 const char *path,
1426 const char *interface,
1427 const char *property,
1428 sd_bus_message *reply,
1429 void *userdata,
1430 sd_bus_error *error) {
1431
1432 Manager *m = userdata;
1433 const char *domain;
1434 Iterator i;
1435 int r;
1436
1437 assert(reply);
1438 assert(m);
1439
1440 r = sd_bus_message_open_container(reply, 'a', "s");
1441 if (r < 0)
1442 return r;
1443
1444 SET_FOREACH(domain, m->trust_anchor.negative_by_name, i) {
1445 r = sd_bus_message_append(reply, "s", domain);
1446 if (r < 0)
1447 return r;
1448 }
1449
1450 return sd_bus_message_close_container(reply);
1451}
1452
7ed1565a
DM
1453static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode, dns_stub_listener_mode, DnsStubListenerMode);
1454
a150ff5e
LP
1455static int bus_method_reset_statistics(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1456 Manager *m = userdata;
1457 DnsScope *s;
1458
1459 assert(message);
1460 assert(m);
1461
1462 LIST_FOREACH(scopes, s, m->dns_scopes)
1463 s->cache.n_hit = s->cache.n_miss = 0;
1464
1465 m->n_transactions_total = 0;
59c5b597 1466 zero(m->n_dnssec_verdict);
a150ff5e
LP
1467
1468 return sd_bus_reply_method_return(message, NULL);
1469}
1470
3abaabda 1471static int get_any_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) {
97e5d693
LP
1472 Link *l;
1473
1474 assert(m);
3abaabda 1475 assert(ret);
97e5d693
LP
1476
1477 if (ifindex <= 0)
1478 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
1479
1480 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
1481 if (!l)
1482 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %i not known", ifindex);
3abaabda
LP
1483
1484 *ret = l;
1485 return 0;
1486}
1487
d2ec6608 1488static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
97e5d693 1489 int ifindex, r;
97e5d693
LP
1490 Link *l;
1491
97e5d693 1492 assert(m);
97e5d693 1493 assert(message);
d2ec6608 1494 assert(handler);
97e5d693 1495
d2ec6608 1496 assert_cc(sizeof(int) == sizeof(int32_t));
97e5d693
LP
1497 r = sd_bus_message_read(message, "i", &ifindex);
1498 if (r < 0)
1499 return r;
1500
04b764bf 1501 r = get_any_link(m, ifindex, &l, error);
97e5d693
LP
1502 if (r < 0)
1503 return r;
1504
d2ec6608
LP
1505 return handler(message, l, error);
1506}
97e5d693 1507
d2ec6608
LP
1508static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1509 return call_link_method(userdata, message, bus_link_method_set_dns_servers, error);
1510}
97e5d693 1511
ee116b54
LP
1512static int bus_method_set_link_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1513 return call_link_method(userdata, message, bus_link_method_set_domains, error);
97e5d693
LP
1514}
1515
1516static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) {
d2ec6608 1517 return call_link_method(userdata, message, bus_link_method_set_llmnr, error);
97e5d693
LP
1518}
1519
1520static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
d2ec6608 1521 return call_link_method(userdata, message, bus_link_method_set_mdns, error);
97e5d693
LP
1522}
1523
1524static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
d2ec6608 1525 return call_link_method(userdata, message, bus_link_method_set_dnssec, error);
97e5d693
LP
1526}
1527
1528static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
d2ec6608 1529 return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error);
97e5d693
LP
1530}
1531
1532static int bus_method_revert_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
d2ec6608 1533 return call_link_method(userdata, message, bus_link_method_revert, error);
97e5d693
LP
1534}
1535
3abaabda
LP
1536static int bus_method_get_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1537 _cleanup_free_ char *p = NULL;
1538 Manager *m = userdata;
1539 int r, ifindex;
1540 Link *l;
1541
1542 assert(message);
1543 assert(m);
1544
1545 assert_cc(sizeof(int) == sizeof(int32_t));
3abaabda
LP
1546 r = sd_bus_message_read(message, "i", &ifindex);
1547 if (r < 0)
1548 return r;
1549
1550 r = get_any_link(m, ifindex, &l, error);
1551 if (r < 0)
1552 return r;
1553
1554 p = link_bus_path(l);
1555 if (!p)
1556 return -ENOMEM;
1557
1558 return sd_bus_reply_method_return(message, "o", p);
1559}
1560
ba35662f
LP
1561static int bus_method_flush_caches(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1562 Manager *m = userdata;
1563
1564 assert(message);
1565 assert(m);
1566
1567 manager_flush_caches(m);
1568
1569 return sd_bus_reply_method_return(message, NULL);
1570}
1571
74b2466e
LP
1572static const sd_bus_vtable resolve_vtable[] = {
1573 SD_BUS_VTABLE_START(0),
7f220d94 1574 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL, offsetof(Manager, llmnr_hostname), 0),
e40f0647 1575 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers, 0, 0),
ee116b54 1576 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains, 0, 0),
a150ff5e
LP
1577 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics, 0, 0),
1578 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics, 0, 0),
1579 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics, 0, 0),
593f665c 1580 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported, 0, 0),
476798b1 1581 SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_ntas, 0, 0),
7ed1565a 1582 SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode, offsetof(Manager, dns_stub_listener_mode), 0),
7f220d94 1583
78c6a153
LP
1584 SD_BUS_METHOD("ResolveHostname", "isit", "a(iiay)st", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
1585 SD_BUS_METHOD("ResolveAddress", "iiayt", "a(is)t", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
1586 SD_BUS_METHOD("ResolveRecord", "isqqt", "a(iqqay)t", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED),
45ec7efb 1587 SD_BUS_METHOD("ResolveService", "isssit", "a(qqqsa(iiay)s)aayssst", bus_method_resolve_service, SD_BUS_VTABLE_UNPRIVILEGED),
a150ff5e 1588 SD_BUS_METHOD("ResetStatistics", NULL, NULL, bus_method_reset_statistics, 0),
ba35662f 1589 SD_BUS_METHOD("FlushCaches", NULL, NULL, bus_method_flush_caches, 0),
3abaabda 1590 SD_BUS_METHOD("GetLink", "i", "o", bus_method_get_link, SD_BUS_VTABLE_UNPRIVILEGED),
97e5d693 1591 SD_BUS_METHOD("SetLinkDNS", "ia(iay)", NULL, bus_method_set_link_dns_servers, 0),
ee116b54 1592 SD_BUS_METHOD("SetLinkDomains", "ia(sb)", NULL, bus_method_set_link_domains, 0),
97e5d693
LP
1593 SD_BUS_METHOD("SetLinkLLMNR", "is", NULL, bus_method_set_link_llmnr, 0),
1594 SD_BUS_METHOD("SetLinkMulticastDNS", "is", NULL, bus_method_set_link_mdns, 0),
1595 SD_BUS_METHOD("SetLinkDNSSEC", "is", NULL, bus_method_set_link_dnssec, 0),
1596 SD_BUS_METHOD("SetLinkDNSSECNegativeTrustAnchors", "ias", NULL, bus_method_set_link_dnssec_negative_trust_anchors, 0),
1597 SD_BUS_METHOD("RevertLink", "i", NULL, bus_method_revert_link, 0),
1598
74b2466e
LP
1599 SD_BUS_VTABLE_END,
1600};
1601
1602static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
1603 Manager *m = userdata;
1604
1605 assert(s);
1606 assert(m);
1607
1608 m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
1609
1610 manager_connect_bus(m);
1611 return 0;
1612}
1613
19070062 1614static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
902bb5d8
LP
1615 Manager *m = userdata;
1616 int b, r;
1617
19070062
LP
1618 assert(message);
1619 assert(m);
902bb5d8
LP
1620
1621 r = sd_bus_message_read(message, "b", &b);
1622 if (r < 0) {
da927ba9 1623 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
902bb5d8
LP
1624 return 0;
1625 }
1626
1627 if (b)
1628 return 0;
1629
1630 log_debug("Coming back from suspend, verifying all RRs...");
1631
1632 manager_verify_all(m);
1633 return 0;
1634}
1635
74b2466e
LP
1636int manager_connect_bus(Manager *m) {
1637 int r;
1638
1639 assert(m);
1640
1641 if (m->bus)
1642 return 0;
1643
1644 r = sd_bus_default_system(&m->bus);
1645 if (r < 0) {
1646 /* We failed to connect? Yuck, we must be in early
d7ea7bb8 1647 * boot. Let's try in 5s again. */
74b2466e 1648
da927ba9 1649 log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m");
74b2466e
LP
1650
1651 r = sd_event_add_time(m->event, &m->bus_retry_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 5*USEC_PER_SEC, 0, on_bus_retry, m);
f647962d
MS
1652 if (r < 0)
1653 return log_error_errno(r, "Failed to install bus reconnect time event: %m");
74b2466e 1654
aa4a9deb 1655 (void) sd_event_source_set_description(m->bus_retry_event_source, "bus-retry");
74b2466e
LP
1656 return 0;
1657 }
1658
4d1cf1e2 1659 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable, m);
f647962d
MS
1660 if (r < 0)
1661 return log_error_errno(r, "Failed to register object: %m");
74b2466e 1662
3abaabda
LP
1663 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/resolve1/link", "org.freedesktop.resolve1.Link", link_vtable, link_object_find, m);
1664 if (r < 0)
1665 return log_error_errno(r, "Failed to register link objects: %m");
1666
1667 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/resolve1/link", link_node_enumerator, m);
1668 if (r < 0)
1669 return log_error_errno(r, "Failed to register link enumerator: %m");
1670
74b2466e 1671 r = sd_bus_request_name(m->bus, "org.freedesktop.resolve1", 0);
f647962d
MS
1672 if (r < 0)
1673 return log_error_errno(r, "Failed to register name: %m");
74b2466e
LP
1674
1675 r = sd_bus_attach_event(m->bus, m->event, 0);
f647962d
MS
1676 if (r < 0)
1677 return log_error_errno(r, "Failed to attach bus to event loop: %m");
74b2466e 1678
902bb5d8
LP
1679 r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot,
1680 "type='signal',"
1681 "sender='org.freedesktop.login1',"
1682 "interface='org.freedesktop.login1.Manager',"
1683 "member='PrepareForSleep',"
1684 "path='/org/freedesktop/login1'",
1685 match_prepare_for_sleep,
1686 m);
1687 if (r < 0)
da927ba9 1688 log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
902bb5d8 1689
74b2466e
LP
1690 return 0;
1691}