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