]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-bus.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.git] / src / resolve / resolved-bus.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "bus-common-errors.h"
22 #include "bus-util.h"
23 #include "dns-domain.h"
24 #include "resolved-bus.h"
25 #include "resolved-def.h"
26 #include "resolved-dns-synthesize.h"
27 #include "resolved-link-bus.h"
28
29 static int reply_query_state(DnsQuery *q) {
30
31 switch (q->state) {
32
33 case DNS_TRANSACTION_NO_SERVERS:
34 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
35
36 case DNS_TRANSACTION_TIMEOUT:
37 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
38
39 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
40 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
41
42 case DNS_TRANSACTION_INVALID_REPLY:
43 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
44
45 case DNS_TRANSACTION_ERRNO:
46 return sd_bus_reply_method_errnof(q->request, q->answer_errno, "Lookup failed due to system error: %m");
47
48 case DNS_TRANSACTION_ABORTED:
49 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_ABORTED, "Query aborted");
50
51 case DNS_TRANSACTION_DNSSEC_FAILED:
52 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_DNSSEC_FAILED, "DNSSEC validation failed: %s",
53 dnssec_result_to_string(q->answer_dnssec_result));
54
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
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
61 case DNS_TRANSACTION_NETWORK_DOWN:
62 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NETWORK_DOWN, "Network is down");
63
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
69 case DNS_TRANSACTION_RCODE_FAILURE: {
70 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
71
72 if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
73 sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", dns_query_string(q));
74 else {
75 const char *rc, *n;
76 char p[DECIMAL_STR_MAX(q->answer_rcode)];
77
78 rc = dns_rcode_to_string(q->answer_rcode);
79 if (!rc) {
80 sprintf(p, "%i", q->answer_rcode);
81 rc = p;
82 }
83
84 n = strjoina(_BUS_ERROR_DNS, rc);
85 sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", dns_query_string(q), rc);
86 }
87
88 return sd_bus_reply_method_error(q->request, &error);
89 }
90
91 case DNS_TRANSACTION_NULL:
92 case DNS_TRANSACTION_PENDING:
93 case DNS_TRANSACTION_VALIDATING:
94 case DNS_TRANSACTION_SUCCESS:
95 default:
96 assert_not_reached("Impossible state");
97 }
98 }
99
100 static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
101 int r;
102
103 assert(reply);
104 assert(rr);
105
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);
111 if (r < 0)
112 return r;
113
114 if (rr->key->type == DNS_TYPE_A) {
115 r = sd_bus_message_append(reply, "i", AF_INET);
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));
120
121 } else if (rr->key->type == DNS_TYPE_AAAA) {
122 r = sd_bus_message_append(reply, "i", AF_INET6);
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));
127 } else
128 return -EAFNOSUPPORT;
129
130 if (r < 0)
131 return r;
132
133 r = sd_bus_message_close_container(reply);
134 if (r < 0)
135 return r;
136
137 return 0;
138 }
139
140 static void bus_method_resolve_hostname_complete(DnsQuery *q) {
141 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
142 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
143 _cleanup_free_ char *normalized = NULL;
144 DnsResourceRecord *rr;
145 unsigned added = 0;
146 int ifindex, r;
147
148 assert(q);
149
150 if (q->state != DNS_TRANSACTION_SUCCESS) {
151 r = reply_query_state(q);
152 goto finish;
153 }
154
155 r = dns_query_process_cname(q);
156 if (r == -ELOOP) {
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));
158 goto finish;
159 }
160 if (r < 0)
161 goto finish;
162 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
163 return;
164
165 r = sd_bus_message_new_method_return(q->request, &reply);
166 if (r < 0)
167 goto finish;
168
169 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
170 if (r < 0)
171 goto finish;
172
173 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
174 DnsQuestion *question;
175
176 question = dns_query_question_for_protocol(q, q->answer_protocol);
177
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;
183
184 r = append_address(reply, rr, ifindex);
185 if (r < 0)
186 goto finish;
187
188 if (!canonical)
189 canonical = dns_resource_record_ref(rr);
190
191 added++;
192 }
193
194 if (added <= 0) {
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));
196 goto finish;
197 }
198
199 r = sd_bus_message_close_container(reply);
200 if (r < 0)
201 goto finish;
202
203 /* The key names are not necessarily normalized, make sure that they are when we return them to our bus
204 * clients. */
205 r = dns_name_normalize(dns_resource_key_name(canonical->key), &normalized);
206 if (r < 0)
207 goto finish;
208
209 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
210 assert(canonical);
211 r = sd_bus_message_append(
212 reply, "st",
213 normalized,
214 SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
215 if (r < 0)
216 goto finish;
217
218 r = sd_bus_send(q->manager->bus, reply, NULL);
219
220 finish:
221 if (r < 0) {
222 log_error_errno(r, "Failed to send hostname reply: %m");
223 sd_bus_reply_method_errno(q->request, r, NULL);
224 }
225
226 dns_query_free(q);
227 }
228
229 static int check_ifindex_flags(int ifindex, uint64_t *flags, uint64_t ok, sd_bus_error *error) {
230 assert(flags);
231
232 if (ifindex < 0)
233 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
234
235 if (*flags & ~(SD_RESOLVED_PROTOCOLS_ALL|SD_RESOLVED_NO_CNAME|ok))
236 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
237
238 if ((*flags & SD_RESOLVED_PROTOCOLS_ALL) == 0) /* If no protocol is enabled, enable all */
239 *flags |= SD_RESOLVED_PROTOCOLS_ALL;
240
241 return 0;
242 }
243
244 static 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;
248 int r, ff, parsed_ifindex = 0;
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
253 r = in_addr_ifindex_from_string_auto(hostname, &ff, &parsed, &parsed_ifindex);
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.");
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;
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). */
296 r = in_addr_ifindex_to_string(ff, &parsed, ifindex, &canonical);
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
308 static int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
309 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
310 Manager *m = userdata;
311 const char *hostname;
312 int family, ifindex;
313 uint64_t flags;
314 DnsQuery *q;
315 int r;
316
317 assert(message);
318 assert(m);
319
320 assert_cc(sizeof(int) == sizeof(int32_t));
321
322 r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
323 if (r < 0)
324 return r;
325
326 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
327 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
328
329 r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_SEARCH, error);
330 if (r < 0)
331 return r;
332
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);
338 if (r < 0)
339 return r;
340 if (r == 0)
341 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
342
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);
348 if (r < 0 && r != -EALREADY)
349 return r;
350
351 r = dns_query_new(m, &q, question_utf8, question_idna ?: question_utf8, ifindex, flags);
352 if (r < 0)
353 return r;
354
355 q->request = sd_bus_message_ref(message);
356 q->request_family = family;
357 q->complete = bus_method_resolve_hostname_complete;
358 q->suppress_unroutable_family = family == AF_UNSPEC;
359
360 r = dns_query_bus_track(q, message);
361 if (r < 0)
362 goto fail;
363
364 r = dns_query_go(q);
365 if (r < 0)
366 goto fail;
367
368 return 1;
369
370 fail:
371 dns_query_free(q);
372 return r;
373 }
374
375 static void bus_method_resolve_address_complete(DnsQuery *q) {
376 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
377 DnsQuestion *question;
378 DnsResourceRecord *rr;
379 unsigned added = 0;
380 int ifindex, r;
381
382 assert(q);
383
384 if (q->state != DNS_TRANSACTION_SUCCESS) {
385 r = reply_query_state(q);
386 goto finish;
387 }
388
389 r = dns_query_process_cname(q);
390 if (r == -ELOOP) {
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));
392 goto finish;
393 }
394 if (r < 0)
395 goto finish;
396 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
397 return;
398
399 r = sd_bus_message_new_method_return(q->request, &reply);
400 if (r < 0)
401 goto finish;
402
403 r = sd_bus_message_open_container(reply, 'a', "(is)");
404 if (r < 0)
405 goto finish;
406
407 question = dns_query_question_for_protocol(q, q->answer_protocol);
408
409 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
410 _cleanup_free_ char *normalized = NULL;
411
412 r = dns_question_matches_rr(question, rr, NULL);
413 if (r < 0)
414 goto finish;
415 if (r == 0)
416 continue;
417
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);
423 if (r < 0)
424 goto finish;
425
426 added++;
427 }
428
429 if (added <= 0) {
430 _cleanup_free_ char *ip = NULL;
431
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));
435 goto finish;
436 }
437
438 r = sd_bus_message_close_container(reply);
439 if (r < 0)
440 goto finish;
441
442 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
443 if (r < 0)
444 goto finish;
445
446 r = sd_bus_send(q->manager->bus, reply, NULL);
447
448 finish:
449 if (r < 0) {
450 log_error_errno(r, "Failed to send address reply: %m");
451 sd_bus_reply_method_errno(q->request, r, NULL);
452 }
453
454 dns_query_free(q);
455 }
456
457 static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
458 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
459 Manager *m = userdata;
460 int family, ifindex;
461 uint64_t flags;
462 const void *d;
463 DnsQuery *q;
464 size_t sz;
465 int r;
466
467 assert(message);
468 assert(m);
469
470 assert_cc(sizeof(int) == sizeof(int32_t));
471
472 r = sd_bus_message_read(message, "ii", &ifindex, &family);
473 if (r < 0)
474 return r;
475
476 if (!IN_SET(family, AF_INET, AF_INET6))
477 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
478
479 r = sd_bus_message_read_array(message, 'y', &d, &sz);
480 if (r < 0)
481 return r;
482
483 if (sz != FAMILY_ADDRESS_SIZE(family))
484 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
485
486 r = sd_bus_message_read(message, "t", &flags);
487 if (r < 0)
488 return r;
489
490 r = check_ifindex_flags(ifindex, &flags, 0, error);
491 if (r < 0)
492 return r;
493
494 r = dns_question_new_reverse(&question, family, d);
495 if (r < 0)
496 return r;
497
498 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
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
507 r = dns_query_bus_track(q, message);
508 if (r < 0)
509 goto fail;
510
511 r = dns_query_go(q);
512 if (r < 0)
513 goto fail;
514
515 return 1;
516
517 fail:
518 dns_query_free(q);
519 return r;
520 }
521
522 static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
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
539 r = dns_resource_record_to_wire_format(rr, false);
540 if (r < 0)
541 return r;
542
543 r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
544 if (r < 0)
545 return r;
546
547 return sd_bus_message_close_container(m);
548 }
549
550 static void bus_method_resolve_record_complete(DnsQuery *q) {
551 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
552 DnsResourceRecord *rr;
553 DnsQuestion *question;
554 unsigned added = 0;
555 int ifindex;
556 int r;
557
558 assert(q);
559
560 if (q->state != DNS_TRANSACTION_SUCCESS) {
561 r = reply_query_state(q);
562 goto finish;
563 }
564
565 r = dns_query_process_cname(q);
566 if (r == -ELOOP) {
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));
568 goto finish;
569 }
570 if (r < 0)
571 goto finish;
572 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
573 return;
574
575 r = sd_bus_message_new_method_return(q->request, &reply);
576 if (r < 0)
577 goto finish;
578
579 r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
580 if (r < 0)
581 goto finish;
582
583 question = dns_query_question_for_protocol(q, q->answer_protocol);
584
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;
591
592 r = bus_message_append_rr(reply, rr, ifindex);
593 if (r < 0)
594 goto finish;
595
596 added++;
597 }
598
599 if (added <= 0) {
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));
601 goto finish;
602 }
603
604 r = sd_bus_message_close_container(reply);
605 if (r < 0)
606 goto finish;
607
608 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
609 if (r < 0)
610 goto finish;
611
612 r = sd_bus_send(q->manager->bus, reply, NULL);
613
614 finish:
615 if (r < 0) {
616 log_error_errno(r, "Failed to send record reply: %m");
617 sd_bus_reply_method_errno(q->request, r, NULL);
618 }
619
620 dns_query_free(q);
621 }
622
623 static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
624 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
625 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
626 Manager *m = userdata;
627 uint16_t class, type;
628 const char *name;
629 int r, ifindex;
630 uint64_t flags;
631 DnsQuery *q;
632
633 assert(message);
634 assert(m);
635
636 assert_cc(sizeof(int) == sizeof(int32_t));
637
638 r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
639 if (r < 0)
640 return r;
641
642 r = dns_name_is_valid(name);
643 if (r < 0)
644 return r;
645 if (r == 0)
646 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
647
648 if (!dns_type_is_valid_query(type))
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);
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.");
652 if (dns_type_is_obsolete(type))
653 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
654
655 r = check_ifindex_flags(ifindex, &flags, 0, error);
656 if (r < 0)
657 return r;
658
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
671 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
672 if (r < 0)
673 return r;
674
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
679 q->request = sd_bus_message_ref(message);
680 q->complete = bus_method_resolve_record_complete;
681
682 r = dns_query_bus_track(q, message);
683 if (r < 0)
684 goto fail;
685
686 r = dns_query_go(q);
687 if (r < 0)
688 goto fail;
689
690 return 1;
691
692 fail:
693 dns_query_free(q);
694 return r;
695 }
696
697 static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
698 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
699 _cleanup_free_ char *normalized = NULL;
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;
716 DnsQuestion *question;
717
718 if (aux->state != DNS_TRANSACTION_SUCCESS)
719 continue;
720 if (aux->auxiliary_result != 0)
721 continue;
722
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);
726 if (r < 0)
727 return r;
728 if (r == 0)
729 continue;
730
731 DNS_ANSWER_FOREACH(zz, aux->answer) {
732
733 r = dns_question_matches_rr(question, zz, NULL);
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
756 r = dns_name_normalize(rr->srv.name, &normalized);
757 if (r < 0)
758 return r;
759
760 r = sd_bus_message_append(
761 reply,
762 "qqqs",
763 rr->srv.priority, rr->srv.weight, rr->srv.port, normalized);
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;
774 DnsQuestion *question;
775 int ifindex;
776
777 if (aux->state != DNS_TRANSACTION_SUCCESS)
778 continue;
779 if (aux->auxiliary_result != 0)
780 continue;
781
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);
785 if (r < 0)
786 return r;
787 if (r == 0)
788 continue;
789
790 DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
791
792 r = dns_question_matches_rr(question, zz, NULL);
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
809 if (canonical) {
810 normalized = mfree(normalized);
811
812 r = dns_name_normalize(dns_resource_key_name(canonical->key), &normalized);
813 if (r < 0)
814 return r;
815 }
816
817 /* Note that above we appended the hostname as encoded in the
818 * SRV, and here the canonical hostname this maps to. */
819 r = sd_bus_message_append(reply, "s", normalized);
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
830 static 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
854 static 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;
858 DnsQuestion *question;
859 DnsResourceRecord *rr;
860 unsigned added = 0;
861 DnsQuery *aux;
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) {
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));
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
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;
931
932 r = append_srv(q, reply, rr);
933 if (r < 0)
934 goto finish;
935 if (r == 0) /* not an SRV record */
936 continue;
937
938 if (!canonical)
939 canonical = dns_resource_record_ref(rr);
940
941 added++;
942 }
943
944 if (added <= 0) {
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));
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
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;
963
964 r = append_txt(reply, rr);
965 if (r < 0)
966 goto finish;
967 }
968
969 r = sd_bus_message_close_container(reply);
970 if (r < 0)
971 goto finish;
972
973 assert(canonical);
974 r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain);
975 if (r < 0)
976 goto finish;
977
978 r = sd_bus_message_append(
979 reply,
980 "ssst",
981 name, type, domain,
982 SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
983 if (r < 0)
984 goto finish;
985
986 r = sd_bus_send(q->manager->bus, reply, NULL);
987
988 finish:
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
997 static 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);
1009 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
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
1017 static 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
1030 r = dns_question_new_address(&question, q->request_family, rr->srv.name, false);
1031 if (r < 0)
1032 return r;
1033
1034 r = dns_query_new(q->manager, &aux, question, question, ifindex, q->flags|SD_RESOLVED_NO_SEARCH);
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
1062 fail:
1063 dns_query_free(aux);
1064 return r;
1065 }
1066
1067 static void bus_method_resolve_service_complete(DnsQuery *q) {
1068 bool has_root_domain = false;
1069 DnsResourceRecord *rr;
1070 DnsQuestion *question;
1071 unsigned found = 0;
1072 int ifindex, r;
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) {
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));
1084 goto finish;
1085 }
1086 if (r < 0)
1087 goto finish;
1088 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
1089 return;
1090
1091 question = dns_query_question_for_protocol(q, q->answer_protocol);
1092
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;
1099
1100 if (rr->key->type != DNS_TYPE_SRV)
1101 continue;
1102
1103 if (dns_name_is_root(rr->srv.name)) {
1104 has_root_domain = true;
1105 continue;
1106 }
1107
1108 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1109 q->block_all_complete++;
1110 r = resolve_service_hostname(q, rr, ifindex);
1111 q->block_all_complete--;
1112
1113 if (r < 0)
1114 goto finish;
1115 }
1116
1117 found++;
1118 }
1119
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;
1129 }
1130
1131 if (found <= 0) {
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));
1133 goto finish;
1134 }
1135
1136 /* Maybe we are already finished? check now... */
1137 resolve_service_all_complete(q);
1138 return;
1139
1140 finish:
1141 if (r < 0) {
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
1149 static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1150 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
1151 const char *name, *type, *domain;
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)
1165 return r;
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;
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);
1174
1175 if (isempty(type))
1176 type = NULL;
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);
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
1193 r = dns_question_new_service(&question_utf8, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), false);
1194 if (r < 0)
1195 return r;
1196
1197 r = dns_question_new_service(&question_idna, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), true);
1198 if (r < 0)
1199 return r;
1200
1201 r = dns_query_new(m, &q, question_utf8, question_idna, ifindex, flags|SD_RESOLVED_NO_SEARCH);
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
1217 return 1;
1218
1219 fail:
1220 dns_query_free(q);
1221 return r;
1222 }
1223
1224 int bus_dns_server_append(sd_bus_message *reply, DnsServer *s, bool with_ifindex) {
1225 int r;
1226
1227 assert(reply);
1228 assert(s);
1229
1230 r = sd_bus_message_open_container(reply, 'r', with_ifindex ? "iiay" : "iay");
1231 if (r < 0)
1232 return r;
1233
1234 if (with_ifindex) {
1235 r = sd_bus_message_append(reply, "i", dns_server_ifindex(s));
1236 if (r < 0)
1237 return r;
1238 }
1239
1240 r = sd_bus_message_append(reply, "i", s->family);
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
1251 static 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) {
1275 r = bus_dns_server_append(reply, s, true);
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) {
1284 r = bus_dns_server_append(reply, s, true);
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) {
1293 r = bus_dns_server_append(reply, s, true);
1294 if (r < 0)
1295 return r;
1296 }
1297 }
1298
1299 return sd_bus_message_close_container(reply);
1300 }
1301
1302 static int bus_property_get_domains(
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
1320 r = sd_bus_message_open_container(reply, 'a', "(isb)");
1321 if (r < 0)
1322 return r;
1323
1324 LIST_FOREACH(domains, d, m->search_domains) {
1325 r = sd_bus_message_append(reply, "(isb)", 0, d->name, d->route_only);
1326 if (r < 0)
1327 return r;
1328 }
1329
1330 HASHMAP_FOREACH(l, m->links, i) {
1331 LIST_FOREACH(domains, d, l->search_domains) {
1332 r = sd_bus_message_append(reply, "(isb)", l->ifindex, d->name, d->route_only);
1333 if (r < 0)
1334 return r;
1335 }
1336 }
1337
1338 return sd_bus_message_close_container(reply);
1339 }
1340
1341 static 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
1360 static 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
1385 static 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)",
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]);
1404 }
1405
1406 static 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;
1416
1417 assert(reply);
1418 assert(m);
1419
1420 return sd_bus_message_append(reply, "b", manager_dnssec_supported(m));
1421 }
1422
1423 static 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
1453 static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode, dns_stub_listener_mode, DnsStubListenerMode);
1454
1455 static 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;
1466 zero(m->n_dnssec_verdict);
1467
1468 return sd_bus_reply_method_return(message, NULL);
1469 }
1470
1471 static int get_any_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) {
1472 Link *l;
1473
1474 assert(m);
1475 assert(ret);
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);
1483
1484 *ret = l;
1485 return 0;
1486 }
1487
1488 static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
1489 int ifindex, r;
1490 Link *l;
1491
1492 assert(m);
1493 assert(message);
1494 assert(handler);
1495
1496 assert_cc(sizeof(int) == sizeof(int32_t));
1497 r = sd_bus_message_read(message, "i", &ifindex);
1498 if (r < 0)
1499 return r;
1500
1501 r = get_any_link(m, ifindex, &l, error);
1502 if (r < 0)
1503 return r;
1504
1505 return handler(message, l, error);
1506 }
1507
1508 static 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 }
1511
1512 static 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);
1514 }
1515
1516 static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1517 return call_link_method(userdata, message, bus_link_method_set_llmnr, error);
1518 }
1519
1520 static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1521 return call_link_method(userdata, message, bus_link_method_set_mdns, error);
1522 }
1523
1524 static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1525 return call_link_method(userdata, message, bus_link_method_set_dnssec, error);
1526 }
1527
1528 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1529 return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error);
1530 }
1531
1532 static int bus_method_revert_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1533 return call_link_method(userdata, message, bus_link_method_revert, error);
1534 }
1535
1536 static 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));
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
1561 static 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
1572 static int bus_method_reset_server_features(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1573 Manager *m = userdata;
1574
1575 assert(message);
1576 assert(m);
1577
1578 manager_reset_server_features(m);
1579
1580 return sd_bus_reply_method_return(message, NULL);
1581 }
1582
1583 static const sd_bus_vtable resolve_vtable[] = {
1584 SD_BUS_VTABLE_START(0),
1585 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL, offsetof(Manager, llmnr_hostname), 0),
1586 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers, 0, 0),
1587 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains, 0, 0),
1588 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics, 0, 0),
1589 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics, 0, 0),
1590 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics, 0, 0),
1591 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported, 0, 0),
1592 SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_ntas, 0, 0),
1593 SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode, offsetof(Manager, dns_stub_listener_mode), 0),
1594
1595 SD_BUS_METHOD("ResolveHostname", "isit", "a(iiay)st", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
1596 SD_BUS_METHOD("ResolveAddress", "iiayt", "a(is)t", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
1597 SD_BUS_METHOD("ResolveRecord", "isqqt", "a(iqqay)t", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED),
1598 SD_BUS_METHOD("ResolveService", "isssit", "a(qqqsa(iiay)s)aayssst", bus_method_resolve_service, SD_BUS_VTABLE_UNPRIVILEGED),
1599 SD_BUS_METHOD("ResetStatistics", NULL, NULL, bus_method_reset_statistics, 0),
1600 SD_BUS_METHOD("FlushCaches", NULL, NULL, bus_method_flush_caches, 0),
1601 SD_BUS_METHOD("ResetServerFeatures", NULL, NULL, bus_method_reset_server_features, 0),
1602 SD_BUS_METHOD("GetLink", "i", "o", bus_method_get_link, SD_BUS_VTABLE_UNPRIVILEGED),
1603 SD_BUS_METHOD("SetLinkDNS", "ia(iay)", NULL, bus_method_set_link_dns_servers, 0),
1604 SD_BUS_METHOD("SetLinkDomains", "ia(sb)", NULL, bus_method_set_link_domains, 0),
1605 SD_BUS_METHOD("SetLinkLLMNR", "is", NULL, bus_method_set_link_llmnr, 0),
1606 SD_BUS_METHOD("SetLinkMulticastDNS", "is", NULL, bus_method_set_link_mdns, 0),
1607 SD_BUS_METHOD("SetLinkDNSSEC", "is", NULL, bus_method_set_link_dnssec, 0),
1608 SD_BUS_METHOD("SetLinkDNSSECNegativeTrustAnchors", "ias", NULL, bus_method_set_link_dnssec_negative_trust_anchors, 0),
1609 SD_BUS_METHOD("RevertLink", "i", NULL, bus_method_revert_link, 0),
1610
1611 SD_BUS_VTABLE_END,
1612 };
1613
1614 static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
1615 Manager *m = userdata;
1616
1617 assert(s);
1618 assert(m);
1619
1620 m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
1621
1622 manager_connect_bus(m);
1623 return 0;
1624 }
1625
1626 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
1627 Manager *m = userdata;
1628 int b, r;
1629
1630 assert(message);
1631 assert(m);
1632
1633 r = sd_bus_message_read(message, "b", &b);
1634 if (r < 0) {
1635 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
1636 return 0;
1637 }
1638
1639 if (b)
1640 return 0;
1641
1642 log_debug("Coming back from suspend, verifying all RRs...");
1643
1644 manager_verify_all(m);
1645 return 0;
1646 }
1647
1648 int manager_connect_bus(Manager *m) {
1649 int r;
1650
1651 assert(m);
1652
1653 if (m->bus)
1654 return 0;
1655
1656 r = sd_bus_default_system(&m->bus);
1657 if (r < 0) {
1658 /* We failed to connect? Yuck, we must be in early
1659 * boot. Let's try in 5s again. */
1660
1661 log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m");
1662
1663 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);
1664 if (r < 0)
1665 return log_error_errno(r, "Failed to install bus reconnect time event: %m");
1666
1667 (void) sd_event_source_set_description(m->bus_retry_event_source, "bus-retry");
1668 return 0;
1669 }
1670
1671 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable, m);
1672 if (r < 0)
1673 return log_error_errno(r, "Failed to register object: %m");
1674
1675 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/resolve1/link", "org.freedesktop.resolve1.Link", link_vtable, link_object_find, m);
1676 if (r < 0)
1677 return log_error_errno(r, "Failed to register link objects: %m");
1678
1679 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/resolve1/link", link_node_enumerator, m);
1680 if (r < 0)
1681 return log_error_errno(r, "Failed to register link enumerator: %m");
1682
1683 r = sd_bus_request_name(m->bus, "org.freedesktop.resolve1", 0);
1684 if (r < 0)
1685 return log_error_errno(r, "Failed to register name: %m");
1686
1687 r = sd_bus_attach_event(m->bus, m->event, 0);
1688 if (r < 0)
1689 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1690
1691 r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot,
1692 "type='signal',"
1693 "sender='org.freedesktop.login1',"
1694 "interface='org.freedesktop.login1.Manager',"
1695 "member='PrepareForSleep',"
1696 "path='/org/freedesktop/login1'",
1697 match_prepare_for_sleep,
1698 m);
1699 if (r < 0)
1700 log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
1701
1702 return 0;
1703 }