]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-bus.c
man: document what SIGUSR1 and SIGUSR2 do to resolved
[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, q->answer_authenticated));
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;
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_from_string_auto(hostname, &ff, &parsed);
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
260 r = sd_bus_message_new_method_return(m, &reply);
261 if (r < 0)
262 return r;
263
264 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
265 if (r < 0)
266 return r;
267
268 r = sd_bus_message_open_container(reply, 'r', "iiay");
269 if (r < 0)
270 return r;
271
272 r = sd_bus_message_append(reply, "ii", ifindex, ff);
273 if (r < 0)
274 return r;
275
276 r = sd_bus_message_append_array(reply, 'y', &parsed, FAMILY_ADDRESS_SIZE(ff));
277 if (r < 0)
278 return r;
279
280 r = sd_bus_message_close_container(reply);
281 if (r < 0)
282 return r;
283
284 r = sd_bus_message_close_container(reply);
285 if (r < 0)
286 return r;
287
288 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
289 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
290 * omissions are always done the same way). */
291 r = in_addr_to_string(ff, &parsed, &canonical);
292 if (r < 0)
293 return r;
294
295 r = sd_bus_message_append(reply, "st", canonical,
296 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags), ff, true));
297 if (r < 0)
298 return r;
299
300 return sd_bus_send(sd_bus_message_get_bus(m), reply, NULL);
301 }
302
303 static int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
304 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
305 Manager *m = userdata;
306 const char *hostname;
307 int family, ifindex;
308 uint64_t flags;
309 DnsQuery *q;
310 int r;
311
312 assert(message);
313 assert(m);
314
315 assert_cc(sizeof(int) == sizeof(int32_t));
316
317 r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
318 if (r < 0)
319 return r;
320
321 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
322 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
323
324 r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_SEARCH, error);
325 if (r < 0)
326 return r;
327
328 r = parse_as_address(message, ifindex, hostname, family, flags);
329 if (r != 0)
330 return r;
331
332 r = dns_name_is_valid(hostname);
333 if (r < 0)
334 return r;
335 if (r == 0)
336 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
337
338 r = dns_question_new_address(&question_utf8, family, hostname, false);
339 if (r < 0)
340 return r;
341
342 r = dns_question_new_address(&question_idna, family, hostname, true);
343 if (r < 0)
344 return r;
345
346 r = dns_query_new(m, &q, question_utf8, question_idna, ifindex, flags);
347 if (r < 0)
348 return r;
349
350 q->request = sd_bus_message_ref(message);
351 q->request_family = family;
352 q->complete = bus_method_resolve_hostname_complete;
353 q->suppress_unroutable_family = family == AF_UNSPEC;
354
355 r = dns_query_bus_track(q, message);
356 if (r < 0)
357 goto fail;
358
359 r = dns_query_go(q);
360 if (r < 0)
361 goto fail;
362
363 return 1;
364
365 fail:
366 dns_query_free(q);
367 return r;
368 }
369
370 static void bus_method_resolve_address_complete(DnsQuery *q) {
371 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
372 DnsQuestion *question;
373 DnsResourceRecord *rr;
374 unsigned added = 0;
375 int ifindex, r;
376
377 assert(q);
378
379 if (q->state != DNS_TRANSACTION_SUCCESS) {
380 r = reply_query_state(q);
381 goto finish;
382 }
383
384 r = dns_query_process_cname(q);
385 if (r == -ELOOP) {
386 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));
387 goto finish;
388 }
389 if (r < 0)
390 goto finish;
391 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
392 return;
393
394 r = sd_bus_message_new_method_return(q->request, &reply);
395 if (r < 0)
396 goto finish;
397
398 r = sd_bus_message_open_container(reply, 'a', "(is)");
399 if (r < 0)
400 goto finish;
401
402 question = dns_query_question_for_protocol(q, q->answer_protocol);
403
404 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
405 _cleanup_free_ char *normalized = NULL;
406
407 r = dns_question_matches_rr(question, rr, NULL);
408 if (r < 0)
409 goto finish;
410 if (r == 0)
411 continue;
412
413 r = dns_name_normalize(rr->ptr.name, &normalized);
414 if (r < 0)
415 goto finish;
416
417 r = sd_bus_message_append(reply, "(is)", ifindex, normalized);
418 if (r < 0)
419 goto finish;
420
421 added++;
422 }
423
424 if (added <= 0) {
425 _cleanup_free_ char *ip = NULL;
426
427 (void) in_addr_to_string(q->request_family, &q->request_address, &ip);
428 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR,
429 "Address '%s' does not have any RR of requested type", strnull(ip));
430 goto finish;
431 }
432
433 r = sd_bus_message_close_container(reply);
434 if (r < 0)
435 goto finish;
436
437 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
438 if (r < 0)
439 goto finish;
440
441 r = sd_bus_send(q->manager->bus, reply, NULL);
442
443 finish:
444 if (r < 0) {
445 log_error_errno(r, "Failed to send address reply: %m");
446 sd_bus_reply_method_errno(q->request, r, NULL);
447 }
448
449 dns_query_free(q);
450 }
451
452 static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
453 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
454 Manager *m = userdata;
455 int family, ifindex;
456 uint64_t flags;
457 const void *d;
458 DnsQuery *q;
459 size_t sz;
460 int r;
461
462 assert(message);
463 assert(m);
464
465 assert_cc(sizeof(int) == sizeof(int32_t));
466
467 r = sd_bus_message_read(message, "ii", &ifindex, &family);
468 if (r < 0)
469 return r;
470
471 if (!IN_SET(family, AF_INET, AF_INET6))
472 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
473
474 r = sd_bus_message_read_array(message, 'y', &d, &sz);
475 if (r < 0)
476 return r;
477
478 if (sz != FAMILY_ADDRESS_SIZE(family))
479 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
480
481 r = sd_bus_message_read(message, "t", &flags);
482 if (r < 0)
483 return r;
484
485 r = check_ifindex_flags(ifindex, &flags, 0, error);
486 if (r < 0)
487 return r;
488
489 r = dns_question_new_reverse(&question, family, d);
490 if (r < 0)
491 return r;
492
493 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
494 if (r < 0)
495 return r;
496
497 q->request = sd_bus_message_ref(message);
498 q->request_family = family;
499 memcpy(&q->request_address, d, sz);
500 q->complete = bus_method_resolve_address_complete;
501
502 r = dns_query_bus_track(q, message);
503 if (r < 0)
504 goto fail;
505
506 r = dns_query_go(q);
507 if (r < 0)
508 goto fail;
509
510 return 1;
511
512 fail:
513 dns_query_free(q);
514 return r;
515 }
516
517 static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
518 int r;
519
520 assert(m);
521 assert(rr);
522
523 r = sd_bus_message_open_container(m, 'r', "iqqay");
524 if (r < 0)
525 return r;
526
527 r = sd_bus_message_append(m, "iqq",
528 ifindex,
529 rr->key->class,
530 rr->key->type);
531 if (r < 0)
532 return r;
533
534 r = dns_resource_record_to_wire_format(rr, false);
535 if (r < 0)
536 return r;
537
538 r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
539 if (r < 0)
540 return r;
541
542 return sd_bus_message_close_container(m);
543 }
544
545 static void bus_method_resolve_record_complete(DnsQuery *q) {
546 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
547 DnsResourceRecord *rr;
548 DnsQuestion *question;
549 unsigned added = 0;
550 int ifindex;
551 int r;
552
553 assert(q);
554
555 if (q->state != DNS_TRANSACTION_SUCCESS) {
556 r = reply_query_state(q);
557 goto finish;
558 }
559
560 r = dns_query_process_cname(q);
561 if (r == -ELOOP) {
562 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));
563 goto finish;
564 }
565 if (r < 0)
566 goto finish;
567 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
568 return;
569
570 r = sd_bus_message_new_method_return(q->request, &reply);
571 if (r < 0)
572 goto finish;
573
574 r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
575 if (r < 0)
576 goto finish;
577
578 question = dns_query_question_for_protocol(q, q->answer_protocol);
579
580 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
581 r = dns_question_matches_rr(question, rr, NULL);
582 if (r < 0)
583 goto finish;
584 if (r == 0)
585 continue;
586
587 r = bus_message_append_rr(reply, rr, ifindex);
588 if (r < 0)
589 goto finish;
590
591 added++;
592 }
593
594 if (added <= 0) {
595 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));
596 goto finish;
597 }
598
599 r = sd_bus_message_close_container(reply);
600 if (r < 0)
601 goto finish;
602
603 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
604 if (r < 0)
605 goto finish;
606
607 r = sd_bus_send(q->manager->bus, reply, NULL);
608
609 finish:
610 if (r < 0) {
611 log_error_errno(r, "Failed to send record reply: %m");
612 sd_bus_reply_method_errno(q->request, r, NULL);
613 }
614
615 dns_query_free(q);
616 }
617
618 static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
619 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
620 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
621 Manager *m = userdata;
622 uint16_t class, type;
623 const char *name;
624 int r, ifindex;
625 uint64_t flags;
626 DnsQuery *q;
627
628 assert(message);
629 assert(m);
630
631 assert_cc(sizeof(int) == sizeof(int32_t));
632
633 r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
634 if (r < 0)
635 return r;
636
637 r = dns_name_is_valid(name);
638 if (r < 0)
639 return r;
640 if (r == 0)
641 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
642
643 if (!dns_type_is_valid_query(type))
644 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified resource record type %" PRIu16 " may not be used in a query.", type);
645 if (dns_type_is_obsolete(type))
646 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
647
648 r = check_ifindex_flags(ifindex, &flags, 0, error);
649 if (r < 0)
650 return r;
651
652 question = dns_question_new(1);
653 if (!question)
654 return -ENOMEM;
655
656 key = dns_resource_key_new(class, type, name);
657 if (!key)
658 return -ENOMEM;
659
660 r = dns_question_add(question, key);
661 if (r < 0)
662 return r;
663
664 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
665 if (r < 0)
666 return r;
667
668 q->request = sd_bus_message_ref(message);
669 q->complete = bus_method_resolve_record_complete;
670
671 r = dns_query_bus_track(q, message);
672 if (r < 0)
673 goto fail;
674
675 r = dns_query_go(q);
676 if (r < 0)
677 goto fail;
678
679 return 1;
680
681 fail:
682 dns_query_free(q);
683 return r;
684 }
685
686 static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
687 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
688 _cleanup_free_ char *normalized = NULL;
689 DnsQuery *aux;
690 int r;
691
692 assert(q);
693 assert(reply);
694 assert(rr);
695 assert(rr->key);
696
697 if (rr->key->type != DNS_TYPE_SRV)
698 return 0;
699
700 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
701 /* First, let's see if we could find an appropriate A or AAAA
702 * record for the SRV record */
703 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
704 DnsResourceRecord *zz;
705 DnsQuestion *question;
706
707 if (aux->state != DNS_TRANSACTION_SUCCESS)
708 continue;
709 if (aux->auxiliary_result != 0)
710 continue;
711
712 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
713
714 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
715 if (r < 0)
716 return r;
717 if (r == 0)
718 continue;
719
720 DNS_ANSWER_FOREACH(zz, aux->answer) {
721
722 r = dns_question_matches_rr(question, zz, NULL);
723 if (r < 0)
724 return r;
725 if (r == 0)
726 continue;
727
728 canonical = dns_resource_record_ref(zz);
729 break;
730 }
731
732 if (canonical)
733 break;
734 }
735
736 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
737 if (!canonical)
738 return 0;
739 }
740
741 r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
742 if (r < 0)
743 return r;
744
745 r = dns_name_normalize(rr->srv.name, &normalized);
746 if (r < 0)
747 return r;
748
749 r = sd_bus_message_append(
750 reply,
751 "qqqs",
752 rr->srv.priority, rr->srv.weight, rr->srv.port, normalized);
753 if (r < 0)
754 return r;
755
756 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
757 if (r < 0)
758 return r;
759
760 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
761 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
762 DnsResourceRecord *zz;
763 DnsQuestion *question;
764 int ifindex;
765
766 if (aux->state != DNS_TRANSACTION_SUCCESS)
767 continue;
768 if (aux->auxiliary_result != 0)
769 continue;
770
771 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
772
773 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
774 if (r < 0)
775 return r;
776 if (r == 0)
777 continue;
778
779 DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
780
781 r = dns_question_matches_rr(question, zz, NULL);
782 if (r < 0)
783 return r;
784 if (r == 0)
785 continue;
786
787 r = append_address(reply, zz, ifindex);
788 if (r < 0)
789 return r;
790 }
791 }
792 }
793
794 r = sd_bus_message_close_container(reply);
795 if (r < 0)
796 return r;
797
798 if (canonical) {
799 normalized = mfree(normalized);
800
801 r = dns_name_normalize(dns_resource_key_name(canonical->key), &normalized);
802 if (r < 0)
803 return r;
804 }
805
806 /* Note that above we appended the hostname as encoded in the
807 * SRV, and here the canonical hostname this maps to. */
808 r = sd_bus_message_append(reply, "s", normalized);
809 if (r < 0)
810 return r;
811
812 r = sd_bus_message_close_container(reply);
813 if (r < 0)
814 return r;
815
816 return 1;
817 }
818
819 static int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
820 DnsTxtItem *i;
821 int r;
822
823 assert(reply);
824 assert(rr);
825 assert(rr->key);
826
827 if (rr->key->type != DNS_TYPE_TXT)
828 return 0;
829
830 LIST_FOREACH(items, i, rr->txt.items) {
831
832 if (i->length <= 0)
833 continue;
834
835 r = sd_bus_message_append_array(reply, 'y', i->data, i->length);
836 if (r < 0)
837 return r;
838 }
839
840 return 1;
841 }
842
843 static void resolve_service_all_complete(DnsQuery *q) {
844 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
845 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
846 _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL;
847 DnsQuestion *question;
848 DnsResourceRecord *rr;
849 unsigned added = 0;
850 DnsQuery *aux;
851 int r;
852
853 assert(q);
854
855 if (q->block_all_complete > 0)
856 return;
857
858 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
859 DnsQuery *bad = NULL;
860 bool have_success = false;
861
862 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
863
864 switch (aux->state) {
865
866 case DNS_TRANSACTION_PENDING:
867 /* If an auxiliary query is still pending, let's wait */
868 return;
869
870 case DNS_TRANSACTION_SUCCESS:
871 if (aux->auxiliary_result == 0)
872 have_success = true;
873 else
874 bad = aux;
875 break;
876
877 default:
878 bad = aux;
879 break;
880 }
881 }
882
883 if (!have_success) {
884 /* We can only return one error, hence pick the last error we encountered */
885
886 assert(bad);
887
888 if (bad->state == DNS_TRANSACTION_SUCCESS) {
889 assert(bad->auxiliary_result != 0);
890
891 if (bad->auxiliary_result == -ELOOP) {
892 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));
893 goto finish;
894 }
895
896 r = bad->auxiliary_result;
897 goto finish;
898 }
899
900 r = reply_query_state(bad);
901 goto finish;
902 }
903 }
904
905 r = sd_bus_message_new_method_return(q->request, &reply);
906 if (r < 0)
907 goto finish;
908
909 r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)");
910 if (r < 0)
911 goto finish;
912
913 question = dns_query_question_for_protocol(q, q->answer_protocol);
914 DNS_ANSWER_FOREACH(rr, q->answer) {
915 r = dns_question_matches_rr(question, rr, NULL);
916 if (r < 0)
917 goto finish;
918 if (r == 0)
919 continue;
920
921 r = append_srv(q, reply, rr);
922 if (r < 0)
923 goto finish;
924 if (r == 0) /* not an SRV record */
925 continue;
926
927 if (!canonical)
928 canonical = dns_resource_record_ref(rr);
929
930 added++;
931 }
932
933 if (added <= 0) {
934 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));
935 goto finish;
936 }
937
938 r = sd_bus_message_close_container(reply);
939 if (r < 0)
940 goto finish;
941
942 r = sd_bus_message_open_container(reply, 'a', "ay");
943 if (r < 0)
944 goto finish;
945
946 DNS_ANSWER_FOREACH(rr, q->answer) {
947 r = dns_question_matches_rr(question, rr, NULL);
948 if (r < 0)
949 goto finish;
950 if (r == 0)
951 continue;
952
953 r = append_txt(reply, rr);
954 if (r < 0)
955 goto finish;
956 }
957
958 r = sd_bus_message_close_container(reply);
959 if (r < 0)
960 goto finish;
961
962 assert(canonical);
963 r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain);
964 if (r < 0)
965 goto finish;
966
967 r = sd_bus_message_append(
968 reply,
969 "ssst",
970 name, type, domain,
971 SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
972 if (r < 0)
973 goto finish;
974
975 r = sd_bus_send(q->manager->bus, reply, NULL);
976
977 finish:
978 if (r < 0) {
979 log_error_errno(r, "Failed to send service reply: %m");
980 sd_bus_reply_method_errno(q->request, r, NULL);
981 }
982
983 dns_query_free(q);
984 }
985
986 static void resolve_service_hostname_complete(DnsQuery *q) {
987 int r;
988
989 assert(q);
990 assert(q->auxiliary_for);
991
992 if (q->state != DNS_TRANSACTION_SUCCESS) {
993 resolve_service_all_complete(q->auxiliary_for);
994 return;
995 }
996
997 r = dns_query_process_cname(q);
998 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
999 return;
1000
1001 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1002 q->auxiliary_result = r;
1003 resolve_service_all_complete(q->auxiliary_for);
1004 }
1005
1006 static int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) {
1007 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
1008 DnsQuery *aux;
1009 int r;
1010
1011 assert(q);
1012 assert(rr);
1013 assert(rr->key);
1014 assert(rr->key->type == DNS_TYPE_SRV);
1015
1016 /* OK, we found an SRV record for the service. Let's resolve
1017 * the hostname included in it */
1018
1019 r = dns_question_new_address(&question, q->request_family, rr->srv.name, false);
1020 if (r < 0)
1021 return r;
1022
1023 r = dns_query_new(q->manager, &aux, question, question, ifindex, q->flags|SD_RESOLVED_NO_SEARCH);
1024 if (r < 0)
1025 return r;
1026
1027 aux->request_family = q->request_family;
1028 aux->complete = resolve_service_hostname_complete;
1029
1030 r = dns_query_make_auxiliary(aux, q);
1031 if (r == -EAGAIN) {
1032 /* Too many auxiliary lookups? If so, don't complain,
1033 * let's just not add this one, we already have more
1034 * than enough */
1035
1036 dns_query_free(aux);
1037 return 0;
1038 }
1039 if (r < 0)
1040 goto fail;
1041
1042 /* Note that auxiliary queries do not track the original bus
1043 * client, only the primary request does that. */
1044
1045 r = dns_query_go(aux);
1046 if (r < 0)
1047 goto fail;
1048
1049 return 1;
1050
1051 fail:
1052 dns_query_free(aux);
1053 return r;
1054 }
1055
1056 static void bus_method_resolve_service_complete(DnsQuery *q) {
1057 bool has_root_domain = false;
1058 DnsResourceRecord *rr;
1059 DnsQuestion *question;
1060 unsigned found = 0;
1061 int ifindex, r;
1062
1063 assert(q);
1064
1065 if (q->state != DNS_TRANSACTION_SUCCESS) {
1066 r = reply_query_state(q);
1067 goto finish;
1068 }
1069
1070 r = dns_query_process_cname(q);
1071 if (r == -ELOOP) {
1072 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));
1073 goto finish;
1074 }
1075 if (r < 0)
1076 goto finish;
1077 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
1078 return;
1079
1080 question = dns_query_question_for_protocol(q, q->answer_protocol);
1081
1082 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
1083 r = dns_question_matches_rr(question, rr, NULL);
1084 if (r < 0)
1085 goto finish;
1086 if (r == 0)
1087 continue;
1088
1089 if (rr->key->type != DNS_TYPE_SRV)
1090 continue;
1091
1092 if (dns_name_is_root(rr->srv.name)) {
1093 has_root_domain = true;
1094 continue;
1095 }
1096
1097 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1098 q->block_all_complete++;
1099 r = resolve_service_hostname(q, rr, ifindex);
1100 q->block_all_complete--;
1101
1102 if (r < 0)
1103 goto finish;
1104 }
1105
1106 found++;
1107 }
1108
1109 if (has_root_domain && found <= 0) {
1110 /* If there's exactly one SRV RR and it uses
1111 * the root domain as host name, then the
1112 * service is explicitly not offered on the
1113 * domain. Report this as a recognizable
1114 * error. See RFC 2782, Section "Usage
1115 * Rules". */
1116 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide the requested service", dns_query_string(q));
1117 goto finish;
1118 }
1119
1120 if (found <= 0) {
1121 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));
1122 goto finish;
1123 }
1124
1125 /* Maybe we are already finished? check now... */
1126 resolve_service_all_complete(q);
1127 return;
1128
1129 finish:
1130 if (r < 0) {
1131 log_error_errno(r, "Failed to send service reply: %m");
1132 sd_bus_reply_method_errno(q->request, r, NULL);
1133 }
1134
1135 dns_query_free(q);
1136 }
1137
1138 static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1139 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
1140 const char *name, *type, *domain;
1141 Manager *m = userdata;
1142 int family, ifindex;
1143 uint64_t flags;
1144 DnsQuery *q;
1145 int r;
1146
1147 assert(message);
1148 assert(m);
1149
1150 assert_cc(sizeof(int) == sizeof(int32_t));
1151
1152 r = sd_bus_message_read(message, "isssit", &ifindex, &name, &type, &domain, &family, &flags);
1153 if (r < 0)
1154 return r;
1155
1156 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
1157 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
1158
1159 if (isempty(name))
1160 name = NULL;
1161 else if (!dns_service_name_is_valid(name))
1162 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid service name '%s'", name);
1163
1164 if (isempty(type))
1165 type = NULL;
1166 else if (!dns_srv_type_is_valid(type))
1167 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid SRV service type '%s'", type);
1168
1169 r = dns_name_is_valid(domain);
1170 if (r < 0)
1171 return r;
1172 if (r == 0)
1173 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid domain '%s'", domain);
1174
1175 if (name && !type)
1176 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type.");
1177
1178 r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS, error);
1179 if (r < 0)
1180 return r;
1181
1182 r = dns_question_new_service(&question_utf8, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), false);
1183 if (r < 0)
1184 return r;
1185
1186 r = dns_question_new_service(&question_idna, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), true);
1187 if (r < 0)
1188 return r;
1189
1190 r = dns_query_new(m, &q, question_utf8, question_idna, ifindex, flags|SD_RESOLVED_NO_SEARCH);
1191 if (r < 0)
1192 return r;
1193
1194 q->request = sd_bus_message_ref(message);
1195 q->request_family = family;
1196 q->complete = bus_method_resolve_service_complete;
1197
1198 r = dns_query_bus_track(q, message);
1199 if (r < 0)
1200 goto fail;
1201
1202 r = dns_query_go(q);
1203 if (r < 0)
1204 goto fail;
1205
1206 return 1;
1207
1208 fail:
1209 dns_query_free(q);
1210 return r;
1211 }
1212
1213 int bus_dns_server_append(sd_bus_message *reply, DnsServer *s, bool with_ifindex) {
1214 int r;
1215
1216 assert(reply);
1217 assert(s);
1218
1219 r = sd_bus_message_open_container(reply, 'r', with_ifindex ? "iiay" : "iay");
1220 if (r < 0)
1221 return r;
1222
1223 if (with_ifindex) {
1224 r = sd_bus_message_append(reply, "i", dns_server_ifindex(s));
1225 if (r < 0)
1226 return r;
1227 }
1228
1229 r = sd_bus_message_append(reply, "i", s->family);
1230 if (r < 0)
1231 return r;
1232
1233 r = sd_bus_message_append_array(reply, 'y', &s->address, FAMILY_ADDRESS_SIZE(s->family));
1234 if (r < 0)
1235 return r;
1236
1237 return sd_bus_message_close_container(reply);
1238 }
1239
1240 static int bus_property_get_dns_servers(
1241 sd_bus *bus,
1242 const char *path,
1243 const char *interface,
1244 const char *property,
1245 sd_bus_message *reply,
1246 void *userdata,
1247 sd_bus_error *error) {
1248
1249 Manager *m = userdata;
1250 unsigned c = 0;
1251 DnsServer *s;
1252 Iterator i;
1253 Link *l;
1254 int r;
1255
1256 assert(reply);
1257 assert(m);
1258
1259 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
1260 if (r < 0)
1261 return r;
1262
1263 LIST_FOREACH(servers, s, m->dns_servers) {
1264 r = bus_dns_server_append(reply, s, true);
1265 if (r < 0)
1266 return r;
1267
1268 c++;
1269 }
1270
1271 HASHMAP_FOREACH(l, m->links, i) {
1272 LIST_FOREACH(servers, s, l->dns_servers) {
1273 r = bus_dns_server_append(reply, s, true);
1274 if (r < 0)
1275 return r;
1276 c++;
1277 }
1278 }
1279
1280 if (c == 0) {
1281 LIST_FOREACH(servers, s, m->fallback_dns_servers) {
1282 r = bus_dns_server_append(reply, s, true);
1283 if (r < 0)
1284 return r;
1285 }
1286 }
1287
1288 return sd_bus_message_close_container(reply);
1289 }
1290
1291 static int bus_property_get_domains(
1292 sd_bus *bus,
1293 const char *path,
1294 const char *interface,
1295 const char *property,
1296 sd_bus_message *reply,
1297 void *userdata,
1298 sd_bus_error *error) {
1299
1300 Manager *m = userdata;
1301 DnsSearchDomain *d;
1302 Iterator i;
1303 Link *l;
1304 int r;
1305
1306 assert(reply);
1307 assert(m);
1308
1309 r = sd_bus_message_open_container(reply, 'a', "(isb)");
1310 if (r < 0)
1311 return r;
1312
1313 LIST_FOREACH(domains, d, m->search_domains) {
1314 r = sd_bus_message_append(reply, "(isb)", 0, d->name, d->route_only);
1315 if (r < 0)
1316 return r;
1317 }
1318
1319 HASHMAP_FOREACH(l, m->links, i) {
1320 LIST_FOREACH(domains, d, l->search_domains) {
1321 r = sd_bus_message_append(reply, "(isb)", l->ifindex, d->name, d->route_only);
1322 if (r < 0)
1323 return r;
1324 }
1325 }
1326
1327 return sd_bus_message_close_container(reply);
1328 }
1329
1330 static int bus_property_get_transaction_statistics(
1331 sd_bus *bus,
1332 const char *path,
1333 const char *interface,
1334 const char *property,
1335 sd_bus_message *reply,
1336 void *userdata,
1337 sd_bus_error *error) {
1338
1339 Manager *m = userdata;
1340
1341 assert(reply);
1342 assert(m);
1343
1344 return sd_bus_message_append(reply, "(tt)",
1345 (uint64_t) hashmap_size(m->dns_transactions),
1346 (uint64_t) m->n_transactions_total);
1347 }
1348
1349 static int bus_property_get_cache_statistics(
1350 sd_bus *bus,
1351 const char *path,
1352 const char *interface,
1353 const char *property,
1354 sd_bus_message *reply,
1355 void *userdata,
1356 sd_bus_error *error) {
1357
1358 uint64_t size = 0, hit = 0, miss = 0;
1359 Manager *m = userdata;
1360 DnsScope *s;
1361
1362 assert(reply);
1363 assert(m);
1364
1365 LIST_FOREACH(scopes, s, m->dns_scopes) {
1366 size += dns_cache_size(&s->cache);
1367 hit += s->cache.n_hit;
1368 miss += s->cache.n_miss;
1369 }
1370
1371 return sd_bus_message_append(reply, "(ttt)", size, hit, miss);
1372 }
1373
1374 static int bus_property_get_dnssec_statistics(
1375 sd_bus *bus,
1376 const char *path,
1377 const char *interface,
1378 const char *property,
1379 sd_bus_message *reply,
1380 void *userdata,
1381 sd_bus_error *error) {
1382
1383 Manager *m = userdata;
1384
1385 assert(reply);
1386 assert(m);
1387
1388 return sd_bus_message_append(reply, "(tttt)",
1389 (uint64_t) m->n_dnssec_verdict[DNSSEC_SECURE],
1390 (uint64_t) m->n_dnssec_verdict[DNSSEC_INSECURE],
1391 (uint64_t) m->n_dnssec_verdict[DNSSEC_BOGUS],
1392 (uint64_t) m->n_dnssec_verdict[DNSSEC_INDETERMINATE]);
1393 }
1394
1395 static int bus_property_get_dnssec_supported(
1396 sd_bus *bus,
1397 const char *path,
1398 const char *interface,
1399 const char *property,
1400 sd_bus_message *reply,
1401 void *userdata,
1402 sd_bus_error *error) {
1403
1404 Manager *m = userdata;
1405
1406 assert(reply);
1407 assert(m);
1408
1409 return sd_bus_message_append(reply, "b", manager_dnssec_supported(m));
1410 }
1411
1412 static int bus_method_reset_statistics(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1413 Manager *m = userdata;
1414 DnsScope *s;
1415
1416 assert(message);
1417 assert(m);
1418
1419 LIST_FOREACH(scopes, s, m->dns_scopes)
1420 s->cache.n_hit = s->cache.n_miss = 0;
1421
1422 m->n_transactions_total = 0;
1423 zero(m->n_dnssec_verdict);
1424
1425 return sd_bus_reply_method_return(message, NULL);
1426 }
1427
1428 static int get_any_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) {
1429 Link *l;
1430
1431 assert(m);
1432 assert(ret);
1433
1434 if (ifindex <= 0)
1435 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
1436
1437 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
1438 if (!l)
1439 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %i not known", ifindex);
1440
1441 *ret = l;
1442 return 0;
1443 }
1444
1445 static int get_unmanaged_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) {
1446 Link *l;
1447 int r;
1448
1449 assert(m);
1450 assert(ret);
1451
1452 r = get_any_link(m, ifindex, &l, error);
1453 if (r < 0)
1454 return r;
1455
1456 if (l->flags & IFF_LOOPBACK)
1457 return sd_bus_error_setf(error, BUS_ERROR_LINK_BUSY, "Link %s is loopback device.", l->name);
1458 if (l->is_managed)
1459 return sd_bus_error_setf(error, BUS_ERROR_LINK_BUSY, "Link %s is managed.", l->name);
1460
1461 *ret = l;
1462 return 0;
1463 }
1464
1465 static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
1466 int ifindex, r;
1467 Link *l;
1468
1469 assert(m);
1470 assert(message);
1471 assert(handler);
1472
1473 assert_cc(sizeof(int) == sizeof(int32_t));
1474 r = sd_bus_message_read(message, "i", &ifindex);
1475 if (r < 0)
1476 return r;
1477
1478 r = get_unmanaged_link(m, ifindex, &l, error);
1479 if (r < 0)
1480 return r;
1481
1482 return handler(message, l, error);
1483 }
1484
1485 static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1486 return call_link_method(userdata, message, bus_link_method_set_dns_servers, error);
1487 }
1488
1489 static int bus_method_set_link_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1490 return call_link_method(userdata, message, bus_link_method_set_domains, error);
1491 }
1492
1493 static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1494 return call_link_method(userdata, message, bus_link_method_set_llmnr, error);
1495 }
1496
1497 static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1498 return call_link_method(userdata, message, bus_link_method_set_mdns, error);
1499 }
1500
1501 static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1502 return call_link_method(userdata, message, bus_link_method_set_dnssec, error);
1503 }
1504
1505 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1506 return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error);
1507 }
1508
1509 static int bus_method_revert_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1510 return call_link_method(userdata, message, bus_link_method_revert, error);
1511 }
1512
1513 static int bus_method_get_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1514 _cleanup_free_ char *p = NULL;
1515 Manager *m = userdata;
1516 int r, ifindex;
1517 Link *l;
1518
1519 assert(message);
1520 assert(m);
1521
1522 assert_cc(sizeof(int) == sizeof(int32_t));
1523 r = sd_bus_message_read(message, "i", &ifindex);
1524 if (r < 0)
1525 return r;
1526
1527 r = get_any_link(m, ifindex, &l, error);
1528 if (r < 0)
1529 return r;
1530
1531 p = link_bus_path(l);
1532 if (!p)
1533 return -ENOMEM;
1534
1535 return sd_bus_reply_method_return(message, "o", p);
1536 }
1537
1538 static const sd_bus_vtable resolve_vtable[] = {
1539 SD_BUS_VTABLE_START(0),
1540 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL, offsetof(Manager, llmnr_hostname), 0),
1541 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers, 0, 0),
1542 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains, 0, 0),
1543 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics, 0, 0),
1544 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics, 0, 0),
1545 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics, 0, 0),
1546 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported, 0, 0),
1547
1548 SD_BUS_METHOD("ResolveHostname", "isit", "a(iiay)st", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
1549 SD_BUS_METHOD("ResolveAddress", "iiayt", "a(is)t", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
1550 SD_BUS_METHOD("ResolveRecord", "isqqt", "a(iqqay)t", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED),
1551 SD_BUS_METHOD("ResolveService", "isssit", "a(qqqsa(iiay)s)aayssst", bus_method_resolve_service, SD_BUS_VTABLE_UNPRIVILEGED),
1552 SD_BUS_METHOD("ResetStatistics", NULL, NULL, bus_method_reset_statistics, 0),
1553 SD_BUS_METHOD("GetLink", "i", "o", bus_method_get_link, SD_BUS_VTABLE_UNPRIVILEGED),
1554 SD_BUS_METHOD("SetLinkDNS", "ia(iay)", NULL, bus_method_set_link_dns_servers, 0),
1555 SD_BUS_METHOD("SetLinkDomains", "ia(sb)", NULL, bus_method_set_link_domains, 0),
1556 SD_BUS_METHOD("SetLinkLLMNR", "is", NULL, bus_method_set_link_llmnr, 0),
1557 SD_BUS_METHOD("SetLinkMulticastDNS", "is", NULL, bus_method_set_link_mdns, 0),
1558 SD_BUS_METHOD("SetLinkDNSSEC", "is", NULL, bus_method_set_link_dnssec, 0),
1559 SD_BUS_METHOD("SetLinkDNSSECNegativeTrustAnchors", "ias", NULL, bus_method_set_link_dnssec_negative_trust_anchors, 0),
1560 SD_BUS_METHOD("RevertLink", "i", NULL, bus_method_revert_link, 0),
1561
1562 SD_BUS_VTABLE_END,
1563 };
1564
1565 static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
1566 Manager *m = userdata;
1567
1568 assert(s);
1569 assert(m);
1570
1571 m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
1572
1573 manager_connect_bus(m);
1574 return 0;
1575 }
1576
1577 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
1578 Manager *m = userdata;
1579 int b, r;
1580
1581 assert(message);
1582 assert(m);
1583
1584 r = sd_bus_message_read(message, "b", &b);
1585 if (r < 0) {
1586 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
1587 return 0;
1588 }
1589
1590 if (b)
1591 return 0;
1592
1593 log_debug("Coming back from suspend, verifying all RRs...");
1594
1595 manager_verify_all(m);
1596 return 0;
1597 }
1598
1599 int manager_connect_bus(Manager *m) {
1600 int r;
1601
1602 assert(m);
1603
1604 if (m->bus)
1605 return 0;
1606
1607 r = sd_bus_default_system(&m->bus);
1608 if (r < 0) {
1609 /* We failed to connect? Yuck, we must be in early
1610 * boot. Let's try in 5s again. As soon as we have
1611 * kdbus we can stop doing this... */
1612
1613 log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m");
1614
1615 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);
1616 if (r < 0)
1617 return log_error_errno(r, "Failed to install bus reconnect time event: %m");
1618
1619 (void) sd_event_source_set_description(m->bus_retry_event_source, "bus-retry");
1620 return 0;
1621 }
1622
1623 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable, m);
1624 if (r < 0)
1625 return log_error_errno(r, "Failed to register object: %m");
1626
1627 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/resolve1/link", "org.freedesktop.resolve1.Link", link_vtable, link_object_find, m);
1628 if (r < 0)
1629 return log_error_errno(r, "Failed to register link objects: %m");
1630
1631 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/resolve1/link", link_node_enumerator, m);
1632 if (r < 0)
1633 return log_error_errno(r, "Failed to register link enumerator: %m");
1634
1635 r = sd_bus_request_name(m->bus, "org.freedesktop.resolve1", 0);
1636 if (r < 0)
1637 return log_error_errno(r, "Failed to register name: %m");
1638
1639 r = sd_bus_attach_event(m->bus, m->event, 0);
1640 if (r < 0)
1641 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1642
1643 r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot,
1644 "type='signal',"
1645 "sender='org.freedesktop.login1',"
1646 "interface='org.freedesktop.login1.Manager',"
1647 "member='PrepareForSleep',"
1648 "path='/org/freedesktop/login1'",
1649 match_prepare_for_sleep,
1650 m);
1651 if (r < 0)
1652 log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
1653
1654 return 0;
1655 }