]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-bus.c
tree-wide: make ++/-- usage consistent WRT spacing
[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 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, "Address '%s' does not have any RR of requested type", strna(ip));
429 goto finish;
430 }
431
432 r = sd_bus_message_close_container(reply);
433 if (r < 0)
434 goto finish;
435
436 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
437 if (r < 0)
438 goto finish;
439
440 r = sd_bus_send(q->manager->bus, reply, NULL);
441
442 finish:
443 if (r < 0) {
444 log_error_errno(r, "Failed to send address reply: %m");
445 sd_bus_reply_method_errno(q->request, r, NULL);
446 }
447
448 dns_query_free(q);
449 }
450
451 static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
452 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
453 Manager *m = userdata;
454 int family, ifindex;
455 uint64_t flags;
456 const void *d;
457 DnsQuery *q;
458 size_t sz;
459 int r;
460
461 assert(message);
462 assert(m);
463
464 assert_cc(sizeof(int) == sizeof(int32_t));
465
466 r = sd_bus_message_read(message, "ii", &ifindex, &family);
467 if (r < 0)
468 return r;
469
470 if (!IN_SET(family, AF_INET, AF_INET6))
471 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
472
473 r = sd_bus_message_read_array(message, 'y', &d, &sz);
474 if (r < 0)
475 return r;
476
477 if (sz != FAMILY_ADDRESS_SIZE(family))
478 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
479
480 r = sd_bus_message_read(message, "t", &flags);
481 if (r < 0)
482 return r;
483
484 r = check_ifindex_flags(ifindex, &flags, 0, error);
485 if (r < 0)
486 return r;
487
488 r = dns_question_new_reverse(&question, family, d);
489 if (r < 0)
490 return r;
491
492 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
493 if (r < 0)
494 return r;
495
496 q->request = sd_bus_message_ref(message);
497 q->request_family = family;
498 memcpy(&q->request_address, d, sz);
499 q->complete = bus_method_resolve_address_complete;
500
501 r = dns_query_bus_track(q, message);
502 if (r < 0)
503 goto fail;
504
505 r = dns_query_go(q);
506 if (r < 0)
507 goto fail;
508
509 return 1;
510
511 fail:
512 dns_query_free(q);
513 return r;
514 }
515
516 static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
517 int r;
518
519 assert(m);
520 assert(rr);
521
522 r = sd_bus_message_open_container(m, 'r', "iqqay");
523 if (r < 0)
524 return r;
525
526 r = sd_bus_message_append(m, "iqq",
527 ifindex,
528 rr->key->class,
529 rr->key->type);
530 if (r < 0)
531 return r;
532
533 r = dns_resource_record_to_wire_format(rr, false);
534 if (r < 0)
535 return r;
536
537 r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
538 if (r < 0)
539 return r;
540
541 return sd_bus_message_close_container(m);
542 }
543
544 static void bus_method_resolve_record_complete(DnsQuery *q) {
545 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
546 DnsResourceRecord *rr;
547 DnsQuestion *question;
548 unsigned added = 0;
549 int ifindex;
550 int r;
551
552 assert(q);
553
554 if (q->state != DNS_TRANSACTION_SUCCESS) {
555 r = reply_query_state(q);
556 goto finish;
557 }
558
559 r = dns_query_process_cname(q);
560 if (r == -ELOOP) {
561 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));
562 goto finish;
563 }
564 if (r < 0)
565 goto finish;
566 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
567 return;
568
569 r = sd_bus_message_new_method_return(q->request, &reply);
570 if (r < 0)
571 goto finish;
572
573 r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
574 if (r < 0)
575 goto finish;
576
577 question = dns_query_question_for_protocol(q, q->answer_protocol);
578
579 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
580 r = dns_question_matches_rr(question, rr, NULL);
581 if (r < 0)
582 goto finish;
583 if (r == 0)
584 continue;
585
586 r = bus_message_append_rr(reply, rr, ifindex);
587 if (r < 0)
588 goto finish;
589
590 added++;
591 }
592
593 if (added <= 0) {
594 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));
595 goto finish;
596 }
597
598 r = sd_bus_message_close_container(reply);
599 if (r < 0)
600 goto finish;
601
602 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
603 if (r < 0)
604 goto finish;
605
606 r = sd_bus_send(q->manager->bus, reply, NULL);
607
608 finish:
609 if (r < 0) {
610 log_error_errno(r, "Failed to send record reply: %m");
611 sd_bus_reply_method_errno(q->request, r, NULL);
612 }
613
614 dns_query_free(q);
615 }
616
617 static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
618 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
619 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
620 Manager *m = userdata;
621 uint16_t class, type;
622 const char *name;
623 int r, ifindex;
624 uint64_t flags;
625 DnsQuery *q;
626
627 assert(message);
628 assert(m);
629
630 assert_cc(sizeof(int) == sizeof(int32_t));
631
632 r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
633 if (r < 0)
634 return r;
635
636 r = dns_name_is_valid(name);
637 if (r < 0)
638 return r;
639 if (r == 0)
640 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
641
642 if (!dns_type_is_valid_query(type))
643 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified resource record type %" PRIu16 " may not be used in a query.", type);
644 if (dns_type_is_obsolete(type))
645 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
646
647 r = check_ifindex_flags(ifindex, &flags, 0, error);
648 if (r < 0)
649 return r;
650
651 question = dns_question_new(1);
652 if (!question)
653 return -ENOMEM;
654
655 key = dns_resource_key_new(class, type, name);
656 if (!key)
657 return -ENOMEM;
658
659 r = dns_question_add(question, key);
660 if (r < 0)
661 return r;
662
663 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
664 if (r < 0)
665 return r;
666
667 q->request = sd_bus_message_ref(message);
668 q->complete = bus_method_resolve_record_complete;
669
670 r = dns_query_bus_track(q, message);
671 if (r < 0)
672 goto fail;
673
674 r = dns_query_go(q);
675 if (r < 0)
676 goto fail;
677
678 return 1;
679
680 fail:
681 dns_query_free(q);
682 return r;
683 }
684
685 static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
686 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
687 _cleanup_free_ char *normalized = NULL;
688 DnsQuery *aux;
689 int r;
690
691 assert(q);
692 assert(reply);
693 assert(rr);
694 assert(rr->key);
695
696 if (rr->key->type != DNS_TYPE_SRV)
697 return 0;
698
699 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
700 /* First, let's see if we could find an appropriate A or AAAA
701 * record for the SRV record */
702 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
703 DnsResourceRecord *zz;
704 DnsQuestion *question;
705
706 if (aux->state != DNS_TRANSACTION_SUCCESS)
707 continue;
708 if (aux->auxiliary_result != 0)
709 continue;
710
711 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
712
713 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
714 if (r < 0)
715 return r;
716 if (r == 0)
717 continue;
718
719 DNS_ANSWER_FOREACH(zz, aux->answer) {
720
721 r = dns_question_matches_rr(question, zz, NULL);
722 if (r < 0)
723 return r;
724 if (r == 0)
725 continue;
726
727 canonical = dns_resource_record_ref(zz);
728 break;
729 }
730
731 if (canonical)
732 break;
733 }
734
735 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
736 if (!canonical)
737 return 0;
738 }
739
740 r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
741 if (r < 0)
742 return r;
743
744 r = dns_name_normalize(rr->srv.name, &normalized);
745 if (r < 0)
746 return r;
747
748 r = sd_bus_message_append(
749 reply,
750 "qqqs",
751 rr->srv.priority, rr->srv.weight, rr->srv.port, normalized);
752 if (r < 0)
753 return r;
754
755 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
756 if (r < 0)
757 return r;
758
759 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
760 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
761 DnsResourceRecord *zz;
762 DnsQuestion *question;
763 int ifindex;
764
765 if (aux->state != DNS_TRANSACTION_SUCCESS)
766 continue;
767 if (aux->auxiliary_result != 0)
768 continue;
769
770 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
771
772 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
773 if (r < 0)
774 return r;
775 if (r == 0)
776 continue;
777
778 DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
779
780 r = dns_question_matches_rr(question, zz, NULL);
781 if (r < 0)
782 return r;
783 if (r == 0)
784 continue;
785
786 r = append_address(reply, zz, ifindex);
787 if (r < 0)
788 return r;
789 }
790 }
791 }
792
793 r = sd_bus_message_close_container(reply);
794 if (r < 0)
795 return r;
796
797 if (canonical) {
798 normalized = mfree(normalized);
799
800 r = dns_name_normalize(dns_resource_key_name(canonical->key), &normalized);
801 if (r < 0)
802 return r;
803 }
804
805 /* Note that above we appended the hostname as encoded in the
806 * SRV, and here the canonical hostname this maps to. */
807 r = sd_bus_message_append(reply, "s", normalized);
808 if (r < 0)
809 return r;
810
811 r = sd_bus_message_close_container(reply);
812 if (r < 0)
813 return r;
814
815 return 1;
816 }
817
818 static int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
819 DnsTxtItem *i;
820 int r;
821
822 assert(reply);
823 assert(rr);
824 assert(rr->key);
825
826 if (rr->key->type != DNS_TYPE_TXT)
827 return 0;
828
829 LIST_FOREACH(items, i, rr->txt.items) {
830
831 if (i->length <= 0)
832 continue;
833
834 r = sd_bus_message_append_array(reply, 'y', i->data, i->length);
835 if (r < 0)
836 return r;
837 }
838
839 return 1;
840 }
841
842 static void resolve_service_all_complete(DnsQuery *q) {
843 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
844 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
845 _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL;
846 DnsQuestion *question;
847 DnsResourceRecord *rr;
848 unsigned added = 0;
849 DnsQuery *aux;
850 int r;
851
852 assert(q);
853
854 if (q->block_all_complete > 0)
855 return;
856
857 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
858 DnsQuery *bad = NULL;
859 bool have_success = false;
860
861 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
862
863 switch (aux->state) {
864
865 case DNS_TRANSACTION_PENDING:
866 /* If an auxiliary query is still pending, let's wait */
867 return;
868
869 case DNS_TRANSACTION_SUCCESS:
870 if (aux->auxiliary_result == 0)
871 have_success = true;
872 else
873 bad = aux;
874 break;
875
876 default:
877 bad = aux;
878 break;
879 }
880 }
881
882 if (!have_success) {
883 /* We can only return one error, hence pick the last error we encountered */
884
885 assert(bad);
886
887 if (bad->state == DNS_TRANSACTION_SUCCESS) {
888 assert(bad->auxiliary_result != 0);
889
890 if (bad->auxiliary_result == -ELOOP) {
891 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));
892 goto finish;
893 }
894
895 r = bad->auxiliary_result;
896 goto finish;
897 }
898
899 r = reply_query_state(bad);
900 goto finish;
901 }
902 }
903
904 r = sd_bus_message_new_method_return(q->request, &reply);
905 if (r < 0)
906 goto finish;
907
908 r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)");
909 if (r < 0)
910 goto finish;
911
912 question = dns_query_question_for_protocol(q, q->answer_protocol);
913 DNS_ANSWER_FOREACH(rr, q->answer) {
914 r = dns_question_matches_rr(question, rr, NULL);
915 if (r < 0)
916 goto finish;
917 if (r == 0)
918 continue;
919
920 r = append_srv(q, reply, rr);
921 if (r < 0)
922 goto finish;
923 if (r == 0) /* not an SRV record */
924 continue;
925
926 if (!canonical)
927 canonical = dns_resource_record_ref(rr);
928
929 added++;
930 }
931
932 if (added <= 0) {
933 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));
934 goto finish;
935 }
936
937 r = sd_bus_message_close_container(reply);
938 if (r < 0)
939 goto finish;
940
941 r = sd_bus_message_open_container(reply, 'a', "ay");
942 if (r < 0)
943 goto finish;
944
945 DNS_ANSWER_FOREACH(rr, q->answer) {
946 r = dns_question_matches_rr(question, rr, NULL);
947 if (r < 0)
948 goto finish;
949 if (r == 0)
950 continue;
951
952 r = append_txt(reply, rr);
953 if (r < 0)
954 goto finish;
955 }
956
957 r = sd_bus_message_close_container(reply);
958 if (r < 0)
959 goto finish;
960
961 assert(canonical);
962 r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain);
963 if (r < 0)
964 goto finish;
965
966 r = sd_bus_message_append(
967 reply,
968 "ssst",
969 name, type, domain,
970 SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
971 if (r < 0)
972 goto finish;
973
974 r = sd_bus_send(q->manager->bus, reply, NULL);
975
976 finish:
977 if (r < 0) {
978 log_error_errno(r, "Failed to send service reply: %m");
979 sd_bus_reply_method_errno(q->request, r, NULL);
980 }
981
982 dns_query_free(q);
983 }
984
985 static void resolve_service_hostname_complete(DnsQuery *q) {
986 int r;
987
988 assert(q);
989 assert(q->auxiliary_for);
990
991 if (q->state != DNS_TRANSACTION_SUCCESS) {
992 resolve_service_all_complete(q->auxiliary_for);
993 return;
994 }
995
996 r = dns_query_process_cname(q);
997 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
998 return;
999
1000 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1001 q->auxiliary_result = r;
1002 resolve_service_all_complete(q->auxiliary_for);
1003 }
1004
1005 static int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) {
1006 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
1007 DnsQuery *aux;
1008 int r;
1009
1010 assert(q);
1011 assert(rr);
1012 assert(rr->key);
1013 assert(rr->key->type == DNS_TYPE_SRV);
1014
1015 /* OK, we found an SRV record for the service. Let's resolve
1016 * the hostname included in it */
1017
1018 r = dns_question_new_address(&question, q->request_family, rr->srv.name, false);
1019 if (r < 0)
1020 return r;
1021
1022 r = dns_query_new(q->manager, &aux, question, question, ifindex, q->flags|SD_RESOLVED_NO_SEARCH);
1023 if (r < 0)
1024 return r;
1025
1026 aux->request_family = q->request_family;
1027 aux->complete = resolve_service_hostname_complete;
1028
1029 r = dns_query_make_auxiliary(aux, q);
1030 if (r == -EAGAIN) {
1031 /* Too many auxiliary lookups? If so, don't complain,
1032 * let's just not add this one, we already have more
1033 * than enough */
1034
1035 dns_query_free(aux);
1036 return 0;
1037 }
1038 if (r < 0)
1039 goto fail;
1040
1041 /* Note that auxiliary queries do not track the original bus
1042 * client, only the primary request does that. */
1043
1044 r = dns_query_go(aux);
1045 if (r < 0)
1046 goto fail;
1047
1048 return 1;
1049
1050 fail:
1051 dns_query_free(aux);
1052 return r;
1053 }
1054
1055 static void bus_method_resolve_service_complete(DnsQuery *q) {
1056 bool has_root_domain = false;
1057 DnsResourceRecord *rr;
1058 DnsQuestion *question;
1059 unsigned found = 0;
1060 int ifindex, r;
1061
1062 assert(q);
1063
1064 if (q->state != DNS_TRANSACTION_SUCCESS) {
1065 r = reply_query_state(q);
1066 goto finish;
1067 }
1068
1069 r = dns_query_process_cname(q);
1070 if (r == -ELOOP) {
1071 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));
1072 goto finish;
1073 }
1074 if (r < 0)
1075 goto finish;
1076 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
1077 return;
1078
1079 question = dns_query_question_for_protocol(q, q->answer_protocol);
1080
1081 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
1082 r = dns_question_matches_rr(question, rr, NULL);
1083 if (r < 0)
1084 goto finish;
1085 if (r == 0)
1086 continue;
1087
1088 if (rr->key->type != DNS_TYPE_SRV)
1089 continue;
1090
1091 if (dns_name_is_root(rr->srv.name)) {
1092 has_root_domain = true;
1093 continue;
1094 }
1095
1096 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1097 q->block_all_complete++;
1098 r = resolve_service_hostname(q, rr, ifindex);
1099 q->block_all_complete--;
1100
1101 if (r < 0)
1102 goto finish;
1103 }
1104
1105 found++;
1106 }
1107
1108 if (has_root_domain && found <= 0) {
1109 /* If there's exactly one SRV RR and it uses
1110 * the root domain as host name, then the
1111 * service is explicitly not offered on the
1112 * domain. Report this as a recognizable
1113 * error. See RFC 2782, Section "Usage
1114 * Rules". */
1115 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide the requested service", dns_query_string(q));
1116 goto finish;
1117 }
1118
1119 if (found <= 0) {
1120 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));
1121 goto finish;
1122 }
1123
1124 /* Maybe we are already finished? check now... */
1125 resolve_service_all_complete(q);
1126 return;
1127
1128 finish:
1129 if (r < 0) {
1130 log_error_errno(r, "Failed to send service reply: %m");
1131 sd_bus_reply_method_errno(q->request, r, NULL);
1132 }
1133
1134 dns_query_free(q);
1135 }
1136
1137 static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1138 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
1139 const char *name, *type, *domain;
1140 _cleanup_free_ char *n = NULL;
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", s->link ? s->link->ifindex : 0);
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 }