]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-bus.c
shared: split out polkit stuff from bus-util.c → bus-polkit.c
[thirdparty/systemd.git] / src / resolve / resolved-bus.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "bus-common-errors.h"
5 #include "bus-polkit.h"
6 #include "bus-util.h"
7 #include "dns-domain.h"
8 #include "memory-util.h"
9 #include "missing_capability.h"
10 #include "resolved-bus.h"
11 #include "resolved-def.h"
12 #include "resolved-dns-synthesize.h"
13 #include "resolved-dnssd-bus.h"
14 #include "resolved-dnssd.h"
15 #include "resolved-link-bus.h"
16 #include "socket-netlink.h"
17 #include "stdio-util.h"
18 #include "strv.h"
19 #include "user-util.h"
20 #include "utf8.h"
21
22 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_resolve_support, resolve_support, ResolveSupport);
23
24 static int reply_query_state(DnsQuery *q) {
25
26 switch (q->state) {
27
28 case DNS_TRANSACTION_NO_SERVERS:
29 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
30
31 case DNS_TRANSACTION_TIMEOUT:
32 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
33
34 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
35 return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
36
37 case DNS_TRANSACTION_INVALID_REPLY:
38 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
39
40 case DNS_TRANSACTION_ERRNO:
41 return sd_bus_reply_method_errnof(q->request, q->answer_errno, "Lookup failed due to system error: %m");
42
43 case DNS_TRANSACTION_ABORTED:
44 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_ABORTED, "Query aborted");
45
46 case DNS_TRANSACTION_DNSSEC_FAILED:
47 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_DNSSEC_FAILED, "DNSSEC validation failed: %s",
48 dnssec_result_to_string(q->answer_dnssec_result));
49
50 case DNS_TRANSACTION_NO_TRUST_ANCHOR:
51 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_TRUST_ANCHOR, "No suitable trust anchor known");
52
53 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED:
54 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_RR_TYPE_UNSUPPORTED, "Server does not support requested resource record type");
55
56 case DNS_TRANSACTION_NETWORK_DOWN:
57 return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NETWORK_DOWN, "Network is down");
58
59 case DNS_TRANSACTION_NOT_FOUND:
60 /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
61 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
62 return sd_bus_reply_method_errorf(q->request, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", dns_query_string(q));
63
64 case DNS_TRANSACTION_RCODE_FAILURE: {
65 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
66
67 if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
68 sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", dns_query_string(q));
69 else {
70 const char *rc, *n;
71 char p[DECIMAL_STR_MAX(q->answer_rcode)];
72
73 rc = dns_rcode_to_string(q->answer_rcode);
74 if (!rc) {
75 xsprintf(p, "%i", q->answer_rcode);
76 rc = p;
77 }
78
79 n = strjoina(_BUS_ERROR_DNS, rc);
80 sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", dns_query_string(q), rc);
81 }
82
83 return sd_bus_reply_method_error(q->request, &error);
84 }
85
86 case DNS_TRANSACTION_NULL:
87 case DNS_TRANSACTION_PENDING:
88 case DNS_TRANSACTION_VALIDATING:
89 case DNS_TRANSACTION_SUCCESS:
90 default:
91 assert_not_reached("Impossible state");
92 }
93 }
94
95 static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
96 int r;
97
98 assert(reply);
99 assert(rr);
100
101 r = sd_bus_message_open_container(reply, 'r', "iiay");
102 if (r < 0)
103 return r;
104
105 r = sd_bus_message_append(reply, "i", ifindex);
106 if (r < 0)
107 return r;
108
109 if (rr->key->type == DNS_TYPE_A) {
110 r = sd_bus_message_append(reply, "i", AF_INET);
111 if (r < 0)
112 return r;
113
114 r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
115
116 } else if (rr->key->type == DNS_TYPE_AAAA) {
117 r = sd_bus_message_append(reply, "i", AF_INET6);
118 if (r < 0)
119 return r;
120
121 r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
122 } else
123 return -EAFNOSUPPORT;
124
125 if (r < 0)
126 return r;
127
128 r = sd_bus_message_close_container(reply);
129 if (r < 0)
130 return r;
131
132 return 0;
133 }
134
135 static void bus_method_resolve_hostname_complete(DnsQuery *q) {
136 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
137 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
138 _cleanup_free_ char *normalized = NULL;
139 DnsResourceRecord *rr;
140 unsigned added = 0;
141 int ifindex, r;
142
143 assert(q);
144
145 if (q->state != DNS_TRANSACTION_SUCCESS) {
146 r = reply_query_state(q);
147 goto finish;
148 }
149
150 r = dns_query_process_cname(q);
151 if (r == -ELOOP) {
152 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));
153 goto finish;
154 }
155 if (r < 0)
156 goto finish;
157 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
158 return;
159
160 r = sd_bus_message_new_method_return(q->request, &reply);
161 if (r < 0)
162 goto finish;
163
164 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
165 if (r < 0)
166 goto finish;
167
168 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
169 DnsQuestion *question;
170
171 question = dns_query_question_for_protocol(q, q->answer_protocol);
172
173 r = dns_question_matches_rr(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
174 if (r < 0)
175 goto finish;
176 if (r == 0)
177 continue;
178
179 r = append_address(reply, rr, ifindex);
180 if (r < 0)
181 goto finish;
182
183 if (!canonical)
184 canonical = dns_resource_record_ref(rr);
185
186 added++;
187 }
188
189 if (added <= 0) {
190 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));
191 goto finish;
192 }
193
194 r = sd_bus_message_close_container(reply);
195 if (r < 0)
196 goto finish;
197
198 /* The key names are not necessarily normalized, make sure that they are when we return them to our bus
199 * clients. */
200 r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
201 if (r < 0)
202 goto finish;
203
204 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
205 assert(canonical);
206 r = sd_bus_message_append(
207 reply, "st",
208 normalized,
209 SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
210 if (r < 0)
211 goto finish;
212
213 r = sd_bus_send(q->manager->bus, reply, NULL);
214
215 finish:
216 if (r < 0) {
217 log_error_errno(r, "Failed to send hostname reply: %m");
218 sd_bus_reply_method_errno(q->request, r, NULL);
219 }
220
221 dns_query_free(q);
222 }
223
224 static int check_ifindex_flags(int ifindex, uint64_t *flags, uint64_t ok, sd_bus_error *error) {
225 assert(flags);
226
227 if (ifindex < 0)
228 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
229
230 if (*flags & ~(SD_RESOLVED_PROTOCOLS_ALL|SD_RESOLVED_NO_CNAME|ok))
231 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
232
233 if ((*flags & SD_RESOLVED_PROTOCOLS_ALL) == 0) /* If no protocol is enabled, enable all */
234 *flags |= SD_RESOLVED_PROTOCOLS_ALL;
235
236 return 0;
237 }
238
239 static int parse_as_address(sd_bus_message *m, int ifindex, const char *hostname, int family, uint64_t flags) {
240 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
241 _cleanup_free_ char *canonical = NULL;
242 union in_addr_union parsed;
243 int r, ff, parsed_ifindex = 0;
244
245 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
246 * let's not attempt to look it up. */
247
248 r = in_addr_ifindex_from_string_auto(hostname, &ff, &parsed, &parsed_ifindex);
249 if (r < 0) /* not an address */
250 return 0;
251
252 if (family != AF_UNSPEC && ff != family)
253 return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address is not of the requested family.");
254 if (ifindex > 0 && parsed_ifindex > 0 && parsed_ifindex != ifindex)
255 return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address interface index does not match requested interface.");
256
257 if (parsed_ifindex > 0)
258 ifindex = parsed_ifindex;
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_ifindex_to_string(ff, &parsed, ifindex, &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 && r != -EALREADY)
344 return r;
345
346 r = dns_query_new(m, &q, question_utf8, question_idna ?: question_utf8, 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, 0, &normalized);
414 if (r < 0)
415 goto finish;
416
417 r = sd_bus_message_append(reply, "(is)", ifindex, normalized);
418 if (r < 0)
419 goto finish;
420
421 added++;
422 }
423
424 if (added <= 0) {
425 _cleanup_free_ char *ip = NULL;
426
427 (void) in_addr_to_string(q->request_family, &q->request_address, &ip);
428 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR,
429 "Address '%s' does not have any RR of requested type", strnull(ip));
430 goto finish;
431 }
432
433 r = sd_bus_message_close_container(reply);
434 if (r < 0)
435 goto finish;
436
437 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
438 if (r < 0)
439 goto finish;
440
441 r = sd_bus_send(q->manager->bus, reply, NULL);
442
443 finish:
444 if (r < 0) {
445 log_error_errno(r, "Failed to send address reply: %m");
446 sd_bus_reply_method_errno(q->request, r, NULL);
447 }
448
449 dns_query_free(q);
450 }
451
452 static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
453 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
454 Manager *m = userdata;
455 int family, ifindex;
456 uint64_t flags;
457 const void *d;
458 DnsQuery *q;
459 size_t sz;
460 int r;
461
462 assert(message);
463 assert(m);
464
465 assert_cc(sizeof(int) == sizeof(int32_t));
466
467 r = sd_bus_message_read(message, "ii", &ifindex, &family);
468 if (r < 0)
469 return r;
470
471 if (!IN_SET(family, AF_INET, AF_INET6))
472 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
473
474 r = sd_bus_message_read_array(message, 'y', &d, &sz);
475 if (r < 0)
476 return r;
477
478 if (sz != FAMILY_ADDRESS_SIZE(family))
479 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
480
481 r = sd_bus_message_read(message, "t", &flags);
482 if (r < 0)
483 return r;
484
485 r = check_ifindex_flags(ifindex, &flags, 0, error);
486 if (r < 0)
487 return r;
488
489 r = dns_question_new_reverse(&question, family, d);
490 if (r < 0)
491 return r;
492
493 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
494 if (r < 0)
495 return r;
496
497 q->request = sd_bus_message_ref(message);
498 q->request_family = family;
499 memcpy(&q->request_address, d, sz);
500 q->complete = bus_method_resolve_address_complete;
501
502 r = dns_query_bus_track(q, message);
503 if (r < 0)
504 goto fail;
505
506 r = dns_query_go(q);
507 if (r < 0)
508 goto fail;
509
510 return 1;
511
512 fail:
513 dns_query_free(q);
514 return r;
515 }
516
517 static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
518 int r;
519
520 assert(m);
521 assert(rr);
522
523 r = sd_bus_message_open_container(m, 'r', "iqqay");
524 if (r < 0)
525 return r;
526
527 r = sd_bus_message_append(m, "iqq",
528 ifindex,
529 rr->key->class,
530 rr->key->type);
531 if (r < 0)
532 return r;
533
534 r = dns_resource_record_to_wire_format(rr, false);
535 if (r < 0)
536 return r;
537
538 r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
539 if (r < 0)
540 return r;
541
542 return sd_bus_message_close_container(m);
543 }
544
545 static void bus_method_resolve_record_complete(DnsQuery *q) {
546 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
547 DnsResourceRecord *rr;
548 DnsQuestion *question;
549 unsigned added = 0;
550 int ifindex;
551 int r;
552
553 assert(q);
554
555 if (q->state != DNS_TRANSACTION_SUCCESS) {
556 r = reply_query_state(q);
557 goto finish;
558 }
559
560 r = dns_query_process_cname(q);
561 if (r == -ELOOP) {
562 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
563 goto finish;
564 }
565 if (r < 0)
566 goto finish;
567 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
568 return;
569
570 r = sd_bus_message_new_method_return(q->request, &reply);
571 if (r < 0)
572 goto finish;
573
574 r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
575 if (r < 0)
576 goto finish;
577
578 question = dns_query_question_for_protocol(q, q->answer_protocol);
579
580 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
581 r = dns_question_matches_rr(question, rr, NULL);
582 if (r < 0)
583 goto finish;
584 if (r == 0)
585 continue;
586
587 r = bus_message_append_rr(reply, rr, ifindex);
588 if (r < 0)
589 goto finish;
590
591 added++;
592 }
593
594 if (added <= 0) {
595 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", dns_query_string(q));
596 goto finish;
597 }
598
599 r = sd_bus_message_close_container(reply);
600 if (r < 0)
601 goto finish;
602
603 r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
604 if (r < 0)
605 goto finish;
606
607 r = sd_bus_send(q->manager->bus, reply, NULL);
608
609 finish:
610 if (r < 0) {
611 log_error_errno(r, "Failed to send record reply: %m");
612 sd_bus_reply_method_errno(q->request, r, NULL);
613 }
614
615 dns_query_free(q);
616 }
617
618 static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
619 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
620 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
621 Manager *m = userdata;
622 uint16_t class, type;
623 const char *name;
624 int r, ifindex;
625 uint64_t flags;
626 DnsQuery *q;
627
628 assert(message);
629 assert(m);
630
631 assert_cc(sizeof(int) == sizeof(int32_t));
632
633 r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
634 if (r < 0)
635 return r;
636
637 r = dns_name_is_valid(name);
638 if (r < 0)
639 return r;
640 if (r == 0)
641 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
642
643 if (!dns_type_is_valid_query(type))
644 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified resource record type %" PRIu16 " may not be used in a query.", type);
645 if (dns_type_is_zone_transer(type))
646 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Zone transfers not permitted via this programming interface.");
647 if (dns_type_is_obsolete(type))
648 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
649
650 r = check_ifindex_flags(ifindex, &flags, 0, error);
651 if (r < 0)
652 return r;
653
654 question = dns_question_new(1);
655 if (!question)
656 return -ENOMEM;
657
658 key = dns_resource_key_new(class, type, name);
659 if (!key)
660 return -ENOMEM;
661
662 r = dns_question_add(question, key);
663 if (r < 0)
664 return r;
665
666 r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
667 if (r < 0)
668 return r;
669
670 /* Let's request that the TTL is fixed up for locally cached entries, after all we return it in the wire format
671 * blob */
672 q->clamp_ttl = true;
673
674 q->request = sd_bus_message_ref(message);
675 q->complete = bus_method_resolve_record_complete;
676
677 r = dns_query_bus_track(q, message);
678 if (r < 0)
679 goto fail;
680
681 r = dns_query_go(q);
682 if (r < 0)
683 goto fail;
684
685 return 1;
686
687 fail:
688 dns_query_free(q);
689 return r;
690 }
691
692 static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
693 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
694 _cleanup_free_ char *normalized = NULL;
695 DnsQuery *aux;
696 int r;
697
698 assert(q);
699 assert(reply);
700 assert(rr);
701 assert(rr->key);
702
703 if (rr->key->type != DNS_TYPE_SRV)
704 return 0;
705
706 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
707 /* First, let's see if we could find an appropriate A or AAAA
708 * record for the SRV record */
709 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
710 DnsResourceRecord *zz;
711 DnsQuestion *question;
712
713 if (aux->state != DNS_TRANSACTION_SUCCESS)
714 continue;
715 if (aux->auxiliary_result != 0)
716 continue;
717
718 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
719
720 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
721 if (r < 0)
722 return r;
723 if (r == 0)
724 continue;
725
726 DNS_ANSWER_FOREACH(zz, aux->answer) {
727
728 r = dns_question_matches_rr(question, zz, NULL);
729 if (r < 0)
730 return r;
731 if (r == 0)
732 continue;
733
734 canonical = dns_resource_record_ref(zz);
735 break;
736 }
737
738 if (canonical)
739 break;
740 }
741
742 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
743 if (!canonical)
744 return 0;
745 }
746
747 r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
748 if (r < 0)
749 return r;
750
751 r = dns_name_normalize(rr->srv.name, 0, &normalized);
752 if (r < 0)
753 return r;
754
755 r = sd_bus_message_append(
756 reply,
757 "qqqs",
758 rr->srv.priority, rr->srv.weight, rr->srv.port, normalized);
759 if (r < 0)
760 return r;
761
762 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
763 if (r < 0)
764 return r;
765
766 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
767 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
768 DnsResourceRecord *zz;
769 DnsQuestion *question;
770 int ifindex;
771
772 if (aux->state != DNS_TRANSACTION_SUCCESS)
773 continue;
774 if (aux->auxiliary_result != 0)
775 continue;
776
777 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
778
779 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
780 if (r < 0)
781 return r;
782 if (r == 0)
783 continue;
784
785 DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
786
787 r = dns_question_matches_rr(question, zz, NULL);
788 if (r < 0)
789 return r;
790 if (r == 0)
791 continue;
792
793 r = append_address(reply, zz, ifindex);
794 if (r < 0)
795 return r;
796 }
797 }
798 }
799
800 r = sd_bus_message_close_container(reply);
801 if (r < 0)
802 return r;
803
804 if (canonical) {
805 normalized = mfree(normalized);
806
807 r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
808 if (r < 0)
809 return r;
810 }
811
812 /* Note that above we appended the hostname as encoded in the
813 * SRV, and here the canonical hostname this maps to. */
814 r = sd_bus_message_append(reply, "s", normalized);
815 if (r < 0)
816 return r;
817
818 r = sd_bus_message_close_container(reply);
819 if (r < 0)
820 return r;
821
822 return 1;
823 }
824
825 static int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
826 DnsTxtItem *i;
827 int r;
828
829 assert(reply);
830 assert(rr);
831 assert(rr->key);
832
833 if (rr->key->type != DNS_TYPE_TXT)
834 return 0;
835
836 LIST_FOREACH(items, i, rr->txt.items) {
837
838 if (i->length <= 0)
839 continue;
840
841 r = sd_bus_message_append_array(reply, 'y', i->data, i->length);
842 if (r < 0)
843 return r;
844 }
845
846 return 1;
847 }
848
849 static void resolve_service_all_complete(DnsQuery *q) {
850 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
851 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
852 _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL;
853 DnsQuestion *question;
854 DnsResourceRecord *rr;
855 unsigned added = 0;
856 DnsQuery *aux;
857 int r;
858
859 assert(q);
860
861 if (q->block_all_complete > 0)
862 return;
863
864 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
865 DnsQuery *bad = NULL;
866 bool have_success = false;
867
868 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
869
870 switch (aux->state) {
871
872 case DNS_TRANSACTION_PENDING:
873 /* If an auxiliary query is still pending, let's wait */
874 return;
875
876 case DNS_TRANSACTION_SUCCESS:
877 if (aux->auxiliary_result == 0)
878 have_success = true;
879 else
880 bad = aux;
881 break;
882
883 default:
884 bad = aux;
885 break;
886 }
887 }
888
889 if (!have_success) {
890 /* We can only return one error, hence pick the last error we encountered */
891
892 assert(bad);
893
894 if (bad->state == DNS_TRANSACTION_SUCCESS) {
895 assert(bad->auxiliary_result != 0);
896
897 if (bad->auxiliary_result == -ELOOP) {
898 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));
899 goto finish;
900 }
901
902 r = bad->auxiliary_result;
903 goto finish;
904 }
905
906 r = reply_query_state(bad);
907 goto finish;
908 }
909 }
910
911 r = sd_bus_message_new_method_return(q->request, &reply);
912 if (r < 0)
913 goto finish;
914
915 r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)");
916 if (r < 0)
917 goto finish;
918
919 question = dns_query_question_for_protocol(q, q->answer_protocol);
920 DNS_ANSWER_FOREACH(rr, q->answer) {
921 r = dns_question_matches_rr(question, rr, NULL);
922 if (r < 0)
923 goto finish;
924 if (r == 0)
925 continue;
926
927 r = append_srv(q, reply, rr);
928 if (r < 0)
929 goto finish;
930 if (r == 0) /* not an SRV record */
931 continue;
932
933 if (!canonical)
934 canonical = dns_resource_record_ref(rr);
935
936 added++;
937 }
938
939 if (added <= 0) {
940 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));
941 goto finish;
942 }
943
944 r = sd_bus_message_close_container(reply);
945 if (r < 0)
946 goto finish;
947
948 r = sd_bus_message_open_container(reply, 'a', "ay");
949 if (r < 0)
950 goto finish;
951
952 DNS_ANSWER_FOREACH(rr, q->answer) {
953 r = dns_question_matches_rr(question, rr, NULL);
954 if (r < 0)
955 goto finish;
956 if (r == 0)
957 continue;
958
959 r = append_txt(reply, rr);
960 if (r < 0)
961 goto finish;
962 }
963
964 r = sd_bus_message_close_container(reply);
965 if (r < 0)
966 goto finish;
967
968 assert(canonical);
969 r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain);
970 if (r < 0)
971 goto finish;
972
973 r = sd_bus_message_append(
974 reply,
975 "ssst",
976 name, type, domain,
977 SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, dns_query_fully_authenticated(q)));
978 if (r < 0)
979 goto finish;
980
981 r = sd_bus_send(q->manager->bus, reply, NULL);
982
983 finish:
984 if (r < 0) {
985 log_error_errno(r, "Failed to send service reply: %m");
986 sd_bus_reply_method_errno(q->request, r, NULL);
987 }
988
989 dns_query_free(q);
990 }
991
992 static void resolve_service_hostname_complete(DnsQuery *q) {
993 int r;
994
995 assert(q);
996 assert(q->auxiliary_for);
997
998 if (q->state != DNS_TRANSACTION_SUCCESS) {
999 resolve_service_all_complete(q->auxiliary_for);
1000 return;
1001 }
1002
1003 r = dns_query_process_cname(q);
1004 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
1005 return;
1006
1007 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1008 q->auxiliary_result = r;
1009 resolve_service_all_complete(q->auxiliary_for);
1010 }
1011
1012 static int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) {
1013 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
1014 DnsQuery *aux;
1015 int r;
1016
1017 assert(q);
1018 assert(rr);
1019 assert(rr->key);
1020 assert(rr->key->type == DNS_TYPE_SRV);
1021
1022 /* OK, we found an SRV record for the service. Let's resolve
1023 * the hostname included in it */
1024
1025 r = dns_question_new_address(&question, q->request_family, rr->srv.name, false);
1026 if (r < 0)
1027 return r;
1028
1029 r = dns_query_new(q->manager, &aux, question, question, ifindex, q->flags|SD_RESOLVED_NO_SEARCH);
1030 if (r < 0)
1031 return r;
1032
1033 aux->request_family = q->request_family;
1034 aux->complete = resolve_service_hostname_complete;
1035
1036 r = dns_query_make_auxiliary(aux, q);
1037 if (r == -EAGAIN) {
1038 /* Too many auxiliary lookups? If so, don't complain,
1039 * let's just not add this one, we already have more
1040 * than enough */
1041
1042 dns_query_free(aux);
1043 return 0;
1044 }
1045 if (r < 0)
1046 goto fail;
1047
1048 /* Note that auxiliary queries do not track the original bus
1049 * client, only the primary request does that. */
1050
1051 r = dns_query_go(aux);
1052 if (r < 0)
1053 goto fail;
1054
1055 return 1;
1056
1057 fail:
1058 dns_query_free(aux);
1059 return r;
1060 }
1061
1062 static void bus_method_resolve_service_complete(DnsQuery *q) {
1063 bool has_root_domain = false;
1064 DnsResourceRecord *rr;
1065 DnsQuestion *question;
1066 unsigned found = 0;
1067 int ifindex, r;
1068
1069 assert(q);
1070
1071 if (q->state != DNS_TRANSACTION_SUCCESS) {
1072 r = reply_query_state(q);
1073 goto finish;
1074 }
1075
1076 r = dns_query_process_cname(q);
1077 if (r == -ELOOP) {
1078 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));
1079 goto finish;
1080 }
1081 if (r < 0)
1082 goto finish;
1083 if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
1084 return;
1085
1086 question = dns_query_question_for_protocol(q, q->answer_protocol);
1087
1088 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
1089 r = dns_question_matches_rr(question, rr, NULL);
1090 if (r < 0)
1091 goto finish;
1092 if (r == 0)
1093 continue;
1094
1095 if (rr->key->type != DNS_TYPE_SRV)
1096 continue;
1097
1098 if (dns_name_is_root(rr->srv.name)) {
1099 has_root_domain = true;
1100 continue;
1101 }
1102
1103 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1104 q->block_all_complete++;
1105 r = resolve_service_hostname(q, rr, ifindex);
1106 q->block_all_complete--;
1107
1108 if (r < 0)
1109 goto finish;
1110 }
1111
1112 found++;
1113 }
1114
1115 if (has_root_domain && found <= 0) {
1116 /* If there's exactly one SRV RR and it uses
1117 * the root domain as host name, then the
1118 * service is explicitly not offered on the
1119 * domain. Report this as a recognizable
1120 * error. See RFC 2782, Section "Usage
1121 * Rules". */
1122 r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide the requested service", dns_query_string(q));
1123 goto finish;
1124 }
1125
1126 if (found <= 0) {
1127 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));
1128 goto finish;
1129 }
1130
1131 /* Maybe we are already finished? check now... */
1132 resolve_service_all_complete(q);
1133 return;
1134
1135 finish:
1136 if (r < 0) {
1137 log_error_errno(r, "Failed to send service reply: %m");
1138 sd_bus_reply_method_errno(q->request, r, NULL);
1139 }
1140
1141 dns_query_free(q);
1142 }
1143
1144 static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1145 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
1146 const char *name, *type, *domain;
1147 Manager *m = userdata;
1148 int family, ifindex;
1149 uint64_t flags;
1150 DnsQuery *q;
1151 int r;
1152
1153 assert(message);
1154 assert(m);
1155
1156 assert_cc(sizeof(int) == sizeof(int32_t));
1157
1158 r = sd_bus_message_read(message, "isssit", &ifindex, &name, &type, &domain, &family, &flags);
1159 if (r < 0)
1160 return r;
1161
1162 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
1163 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
1164
1165 if (isempty(name))
1166 name = NULL;
1167 else if (!dns_service_name_is_valid(name))
1168 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid service name '%s'", name);
1169
1170 if (isempty(type))
1171 type = NULL;
1172 else if (!dns_srv_type_is_valid(type))
1173 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid SRV service type '%s'", type);
1174
1175 r = dns_name_is_valid(domain);
1176 if (r < 0)
1177 return r;
1178 if (r == 0)
1179 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid domain '%s'", domain);
1180
1181 if (name && !type)
1182 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type.");
1183
1184 r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS, error);
1185 if (r < 0)
1186 return r;
1187
1188 r = dns_question_new_service(&question_utf8, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), false);
1189 if (r < 0)
1190 return r;
1191
1192 r = dns_question_new_service(&question_idna, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), true);
1193 if (r < 0)
1194 return r;
1195
1196 r = dns_query_new(m, &q, question_utf8, question_idna, ifindex, flags|SD_RESOLVED_NO_SEARCH);
1197 if (r < 0)
1198 return r;
1199
1200 q->request = sd_bus_message_ref(message);
1201 q->request_family = family;
1202 q->complete = bus_method_resolve_service_complete;
1203
1204 r = dns_query_bus_track(q, message);
1205 if (r < 0)
1206 goto fail;
1207
1208 r = dns_query_go(q);
1209 if (r < 0)
1210 goto fail;
1211
1212 return 1;
1213
1214 fail:
1215 dns_query_free(q);
1216 return r;
1217 }
1218
1219 int bus_dns_server_append(sd_bus_message *reply, DnsServer *s, bool with_ifindex) {
1220 int r;
1221
1222 assert(reply);
1223
1224 if (!s) {
1225 if (with_ifindex)
1226 return sd_bus_message_append(reply, "(iiay)", 0, AF_UNSPEC, 0);
1227 else
1228 return sd_bus_message_append(reply, "(iay)", AF_UNSPEC, 0);
1229 }
1230
1231 r = sd_bus_message_open_container(reply, 'r', with_ifindex ? "iiay" : "iay");
1232 if (r < 0)
1233 return r;
1234
1235 if (with_ifindex) {
1236 r = sd_bus_message_append(reply, "i", dns_server_ifindex(s));
1237 if (r < 0)
1238 return r;
1239 }
1240
1241 r = sd_bus_message_append(reply, "i", s->family);
1242 if (r < 0)
1243 return r;
1244
1245 r = sd_bus_message_append_array(reply, 'y', &s->address, FAMILY_ADDRESS_SIZE(s->family));
1246 if (r < 0)
1247 return r;
1248
1249 return sd_bus_message_close_container(reply);
1250 }
1251
1252 static int bus_property_get_dns_servers(
1253 sd_bus *bus,
1254 const char *path,
1255 const char *interface,
1256 const char *property,
1257 sd_bus_message *reply,
1258 void *userdata,
1259 sd_bus_error *error) {
1260
1261 Manager *m = userdata;
1262 DnsServer *s;
1263 Iterator i;
1264 Link *l;
1265 int r;
1266
1267 assert(reply);
1268 assert(m);
1269
1270 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
1271 if (r < 0)
1272 return r;
1273
1274 LIST_FOREACH(servers, s, m->dns_servers) {
1275 r = bus_dns_server_append(reply, s, true);
1276 if (r < 0)
1277 return r;
1278 }
1279
1280 HASHMAP_FOREACH(l, m->links, i)
1281 LIST_FOREACH(servers, s, l->dns_servers) {
1282 r = bus_dns_server_append(reply, s, true);
1283 if (r < 0)
1284 return r;
1285 }
1286
1287 return sd_bus_message_close_container(reply);
1288 }
1289
1290 static int bus_property_get_fallback_dns_servers(
1291 sd_bus *bus,
1292 const char *path,
1293 const char *interface,
1294 const char *property,
1295 sd_bus_message *reply,
1296 void *userdata,
1297 sd_bus_error *error) {
1298
1299 DnsServer *s, **f = userdata;
1300 int r;
1301
1302 assert(reply);
1303 assert(f);
1304
1305 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
1306 if (r < 0)
1307 return r;
1308
1309 LIST_FOREACH(servers, s, *f) {
1310 r = bus_dns_server_append(reply, s, true);
1311 if (r < 0)
1312 return r;
1313 }
1314
1315 return sd_bus_message_close_container(reply);
1316 }
1317
1318 static int bus_property_get_current_dns_server(
1319 sd_bus *bus,
1320 const char *path,
1321 const char *interface,
1322 const char *property,
1323 sd_bus_message *reply,
1324 void *userdata,
1325 sd_bus_error *error) {
1326
1327 DnsServer *s;
1328
1329 assert(reply);
1330 assert(userdata);
1331
1332 s = *(DnsServer **) userdata;
1333
1334 return bus_dns_server_append(reply, s, true);
1335 }
1336
1337 static int bus_property_get_domains(
1338 sd_bus *bus,
1339 const char *path,
1340 const char *interface,
1341 const char *property,
1342 sd_bus_message *reply,
1343 void *userdata,
1344 sd_bus_error *error) {
1345
1346 Manager *m = userdata;
1347 DnsSearchDomain *d;
1348 Iterator i;
1349 Link *l;
1350 int r;
1351
1352 assert(reply);
1353 assert(m);
1354
1355 r = sd_bus_message_open_container(reply, 'a', "(isb)");
1356 if (r < 0)
1357 return r;
1358
1359 LIST_FOREACH(domains, d, m->search_domains) {
1360 r = sd_bus_message_append(reply, "(isb)", 0, d->name, d->route_only);
1361 if (r < 0)
1362 return r;
1363 }
1364
1365 HASHMAP_FOREACH(l, m->links, i) {
1366 LIST_FOREACH(domains, d, l->search_domains) {
1367 r = sd_bus_message_append(reply, "(isb)", l->ifindex, d->name, d->route_only);
1368 if (r < 0)
1369 return r;
1370 }
1371 }
1372
1373 return sd_bus_message_close_container(reply);
1374 }
1375
1376 static int bus_property_get_transaction_statistics(
1377 sd_bus *bus,
1378 const char *path,
1379 const char *interface,
1380 const char *property,
1381 sd_bus_message *reply,
1382 void *userdata,
1383 sd_bus_error *error) {
1384
1385 Manager *m = userdata;
1386
1387 assert(reply);
1388 assert(m);
1389
1390 return sd_bus_message_append(reply, "(tt)",
1391 (uint64_t) hashmap_size(m->dns_transactions),
1392 (uint64_t) m->n_transactions_total);
1393 }
1394
1395 static int bus_property_get_cache_statistics(
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 uint64_t size = 0, hit = 0, miss = 0;
1405 Manager *m = userdata;
1406 DnsScope *s;
1407
1408 assert(reply);
1409 assert(m);
1410
1411 LIST_FOREACH(scopes, s, m->dns_scopes) {
1412 size += dns_cache_size(&s->cache);
1413 hit += s->cache.n_hit;
1414 miss += s->cache.n_miss;
1415 }
1416
1417 return sd_bus_message_append(reply, "(ttt)", size, hit, miss);
1418 }
1419
1420 static int bus_property_get_dnssec_statistics(
1421 sd_bus *bus,
1422 const char *path,
1423 const char *interface,
1424 const char *property,
1425 sd_bus_message *reply,
1426 void *userdata,
1427 sd_bus_error *error) {
1428
1429 Manager *m = userdata;
1430
1431 assert(reply);
1432 assert(m);
1433
1434 return sd_bus_message_append(reply, "(tttt)",
1435 (uint64_t) m->n_dnssec_verdict[DNSSEC_SECURE],
1436 (uint64_t) m->n_dnssec_verdict[DNSSEC_INSECURE],
1437 (uint64_t) m->n_dnssec_verdict[DNSSEC_BOGUS],
1438 (uint64_t) m->n_dnssec_verdict[DNSSEC_INDETERMINATE]);
1439 }
1440
1441 static int bus_property_get_ntas(
1442 sd_bus *bus,
1443 const char *path,
1444 const char *interface,
1445 const char *property,
1446 sd_bus_message *reply,
1447 void *userdata,
1448 sd_bus_error *error) {
1449
1450 Manager *m = userdata;
1451 const char *domain;
1452 Iterator i;
1453 int r;
1454
1455 assert(reply);
1456 assert(m);
1457
1458 r = sd_bus_message_open_container(reply, 'a', "s");
1459 if (r < 0)
1460 return r;
1461
1462 SET_FOREACH(domain, m->trust_anchor.negative_by_name, i) {
1463 r = sd_bus_message_append(reply, "s", domain);
1464 if (r < 0)
1465 return r;
1466 }
1467
1468 return sd_bus_message_close_container(reply);
1469 }
1470
1471 static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode, dns_stub_listener_mode, DnsStubListenerMode);
1472 static BUS_DEFINE_PROPERTY_GET(bus_property_get_dnssec_supported, "b", Manager, manager_dnssec_supported);
1473 static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dnssec_mode, "s", Manager, manager_get_dnssec_mode, dnssec_mode_to_string);
1474 static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dns_over_tls_mode, "s", Manager, manager_get_dns_over_tls_mode, dns_over_tls_mode_to_string);
1475
1476 static int bus_method_reset_statistics(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1477 Manager *m = userdata;
1478 DnsScope *s;
1479
1480 assert(message);
1481 assert(m);
1482
1483 LIST_FOREACH(scopes, s, m->dns_scopes)
1484 s->cache.n_hit = s->cache.n_miss = 0;
1485
1486 m->n_transactions_total = 0;
1487 zero(m->n_dnssec_verdict);
1488
1489 return sd_bus_reply_method_return(message, NULL);
1490 }
1491
1492 static int get_any_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) {
1493 Link *l;
1494
1495 assert(m);
1496 assert(ret);
1497
1498 if (ifindex <= 0)
1499 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
1500
1501 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
1502 if (!l)
1503 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %i not known", ifindex);
1504
1505 *ret = l;
1506 return 0;
1507 }
1508
1509 static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
1510 int ifindex, r;
1511 Link *l;
1512
1513 assert(m);
1514 assert(message);
1515 assert(handler);
1516
1517 assert_cc(sizeof(int) == sizeof(int32_t));
1518 r = sd_bus_message_read(message, "i", &ifindex);
1519 if (r < 0)
1520 return r;
1521
1522 r = get_any_link(m, ifindex, &l, error);
1523 if (r < 0)
1524 return r;
1525
1526 return handler(message, l, error);
1527 }
1528
1529 static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1530 return call_link_method(userdata, message, bus_link_method_set_dns_servers, error);
1531 }
1532
1533 static int bus_method_set_link_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1534 return call_link_method(userdata, message, bus_link_method_set_domains, error);
1535 }
1536
1537 static int bus_method_set_link_default_route(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1538 return call_link_method(userdata, message, bus_link_method_set_default_route, error);
1539 }
1540
1541 static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1542 return call_link_method(userdata, message, bus_link_method_set_llmnr, error);
1543 }
1544
1545 static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1546 return call_link_method(userdata, message, bus_link_method_set_mdns, error);
1547 }
1548
1549 static int bus_method_set_link_dns_over_tls(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1550 return call_link_method(userdata, message, bus_link_method_set_dns_over_tls, error);
1551 }
1552
1553 static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1554 return call_link_method(userdata, message, bus_link_method_set_dnssec, error);
1555 }
1556
1557 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1558 return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error);
1559 }
1560
1561 static int bus_method_revert_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1562 return call_link_method(userdata, message, bus_link_method_revert, error);
1563 }
1564
1565 static int bus_method_get_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1566 _cleanup_free_ char *p = NULL;
1567 Manager *m = userdata;
1568 int r, ifindex;
1569 Link *l;
1570
1571 assert(message);
1572 assert(m);
1573
1574 assert_cc(sizeof(int) == sizeof(int32_t));
1575 r = sd_bus_message_read(message, "i", &ifindex);
1576 if (r < 0)
1577 return r;
1578
1579 r = get_any_link(m, ifindex, &l, error);
1580 if (r < 0)
1581 return r;
1582
1583 p = link_bus_path(l);
1584 if (!p)
1585 return -ENOMEM;
1586
1587 return sd_bus_reply_method_return(message, "o", p);
1588 }
1589
1590 static int bus_method_flush_caches(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1591 Manager *m = userdata;
1592
1593 assert(message);
1594 assert(m);
1595
1596 manager_flush_caches(m);
1597
1598 return sd_bus_reply_method_return(message, NULL);
1599 }
1600
1601 static int bus_method_reset_server_features(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1602 Manager *m = userdata;
1603
1604 assert(message);
1605 assert(m);
1606
1607 manager_reset_server_features(m);
1608
1609 return sd_bus_reply_method_return(message, NULL);
1610 }
1611
1612 static int on_bus_track(sd_bus_track *t, void *userdata) {
1613 DnssdService *s = userdata;
1614
1615 assert(t);
1616 assert(s);
1617
1618 log_debug("Client of active request vanished, destroying DNS-SD service.");
1619 dnssd_service_free(s);
1620
1621 return 0;
1622 }
1623
1624 static int bus_method_register_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1625 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1626 _cleanup_(dnssd_service_freep) DnssdService *service = NULL;
1627 _cleanup_(sd_bus_track_unrefp) sd_bus_track *bus_track = NULL;
1628 _cleanup_free_ char *path = NULL;
1629 _cleanup_free_ char *instance_name = NULL;
1630 Manager *m = userdata;
1631 DnssdService *s = NULL;
1632 const char *name;
1633 const char *name_template;
1634 const char *type;
1635 uid_t euid;
1636 int r;
1637
1638 assert(message);
1639 assert(m);
1640
1641 if (m->mdns_support != RESOLVE_SUPPORT_YES)
1642 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Support for MulticastDNS is disabled");
1643
1644 service = new0(DnssdService, 1);
1645 if (!service)
1646 return log_oom();
1647
1648 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1649 if (r < 0)
1650 return r;
1651
1652 r = sd_bus_creds_get_euid(creds, &euid);
1653 if (r < 0)
1654 return r;
1655 service->originator = euid;
1656
1657 r = sd_bus_message_read(message, "sssqqq", &name, &name_template, &type,
1658 &service->port, &service->priority,
1659 &service->weight);
1660 if (r < 0)
1661 return r;
1662
1663 s = hashmap_get(m->dnssd_services, name);
1664 if (s)
1665 return sd_bus_error_setf(error, BUS_ERROR_DNSSD_SERVICE_EXISTS, "DNS-SD service '%s' exists already", name);
1666
1667 if (!dnssd_srv_type_is_valid(type))
1668 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DNS-SD service type '%s' is invalid", type);
1669
1670 service->name = strdup(name);
1671 if (!service->name)
1672 return log_oom();
1673
1674 service->name_template = strdup(name_template);
1675 if (!service->name_template)
1676 return log_oom();
1677
1678 service->type = strdup(type);
1679 if (!service->type)
1680 return log_oom();
1681
1682 r = dnssd_render_instance_name(service, &instance_name);
1683 if (r < 0)
1684 return r;
1685
1686 r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "a{say}");
1687 if (r < 0)
1688 return r;
1689
1690 while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "{say}")) > 0) {
1691 _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL;
1692 DnsTxtItem *last = NULL;
1693
1694 txt_data = new0(DnssdTxtData, 1);
1695 if (!txt_data)
1696 return log_oom();
1697
1698 while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_DICT_ENTRY, "say")) > 0) {
1699 const char *key;
1700 const void *value;
1701 size_t size;
1702 DnsTxtItem *i;
1703
1704 r = sd_bus_message_read(message, "s", &key);
1705 if (r < 0)
1706 return r;
1707
1708 if (isempty(key))
1709 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Keys in DNS-SD TXT RRs can't be empty");
1710
1711 if (!ascii_is_valid(key))
1712 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "TXT key '%s' contains non-ASCII symbols", key);
1713
1714 r = sd_bus_message_read_array(message, 'y', &value, &size);
1715 if (r < 0)
1716 return r;
1717
1718 r = dnssd_txt_item_new_from_data(key, value, size, &i);
1719 if (r < 0)
1720 return r;
1721
1722 LIST_INSERT_AFTER(items, txt_data->txt, last, i);
1723 last = i;
1724
1725 r = sd_bus_message_exit_container(message);
1726 if (r < 0)
1727 return r;
1728
1729 }
1730 if (r < 0)
1731 return r;
1732
1733 r = sd_bus_message_exit_container(message);
1734 if (r < 0)
1735 return r;
1736
1737 if (txt_data->txt) {
1738 LIST_PREPEND(items, service->txt_data_items, txt_data);
1739 txt_data = NULL;
1740 }
1741 }
1742 if (r < 0)
1743 return r;
1744
1745 r = sd_bus_message_exit_container(message);
1746 if (r < 0)
1747 return r;
1748
1749 if (!service->txt_data_items) {
1750 _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL;
1751
1752 txt_data = new0(DnssdTxtData, 1);
1753 if (!txt_data)
1754 return log_oom();
1755
1756 r = dns_txt_item_new_empty(&txt_data->txt);
1757 if (r < 0)
1758 return r;
1759
1760 LIST_PREPEND(items, service->txt_data_items, txt_data);
1761 txt_data = NULL;
1762 }
1763
1764 r = sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service->name, &path);
1765 if (r < 0)
1766 return r;
1767
1768 r = bus_verify_polkit_async(message, CAP_SYS_ADMIN,
1769 "org.freedesktop.resolve1.register-service",
1770 NULL, false, UID_INVALID,
1771 &m->polkit_registry, error);
1772 if (r < 0)
1773 return r;
1774 if (r == 0)
1775 return 1; /* Polkit will call us back */
1776
1777 r = hashmap_ensure_allocated(&m->dnssd_services, &string_hash_ops);
1778 if (r < 0)
1779 return r;
1780
1781 r = hashmap_put(m->dnssd_services, service->name, service);
1782 if (r < 0)
1783 return r;
1784
1785 r = sd_bus_track_new(sd_bus_message_get_bus(message), &bus_track, on_bus_track, service);
1786 if (r < 0)
1787 return r;
1788
1789 r = sd_bus_track_add_sender(bus_track, message);
1790 if (r < 0)
1791 return r;
1792
1793 service->manager = m;
1794
1795 service = NULL;
1796
1797 manager_refresh_rrs(m);
1798
1799 return sd_bus_reply_method_return(message, "o", path);
1800 }
1801
1802 static int call_dnssd_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
1803 _cleanup_free_ char *name = NULL;
1804 DnssdService *s = NULL;
1805 const char *path;
1806 int r;
1807
1808 assert(m);
1809 assert(message);
1810 assert(handler);
1811
1812 r = sd_bus_message_read(message, "o", &path);
1813 if (r < 0)
1814 return r;
1815
1816 r = sd_bus_path_decode(path, "/org/freedesktop/resolve1/dnssd", &name);
1817 if (r == 0)
1818 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DNSSD_SERVICE, "DNS-SD service with object path '%s' does not exist", path);
1819 if (r < 0)
1820 return r;
1821
1822 s = hashmap_get(m->dnssd_services, name);
1823 if (!s)
1824 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DNSSD_SERVICE, "DNS-SD service '%s' not known", name);
1825
1826 return handler(message, s, error);
1827 }
1828
1829 static int bus_method_unregister_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1830 Manager *m = userdata;
1831
1832 assert(message);
1833 assert(m);
1834
1835 return call_dnssd_method(m, message, bus_dnssd_method_unregister, error);
1836 }
1837
1838 static const sd_bus_vtable resolve_vtable[] = {
1839 SD_BUS_VTABLE_START(0),
1840 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL, offsetof(Manager, llmnr_hostname), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1841 SD_BUS_PROPERTY("LLMNR", "s", bus_property_get_resolve_support, offsetof(Manager, llmnr_support), 0),
1842 SD_BUS_PROPERTY("MulticastDNS", "s", bus_property_get_resolve_support, offsetof(Manager, mdns_support), 0),
1843 SD_BUS_PROPERTY("DNSOverTLS", "s", bus_property_get_dns_over_tls_mode, 0, 0),
1844 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1845 SD_BUS_PROPERTY("FallbackDNS", "a(iiay)", bus_property_get_fallback_dns_servers, offsetof(Manager, fallback_dns_servers), SD_BUS_VTABLE_PROPERTY_CONST),
1846 SD_BUS_PROPERTY("CurrentDNSServer", "(iiay)", bus_property_get_current_dns_server, offsetof(Manager, current_dns_server), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1847 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains, 0, 0),
1848 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics, 0, 0),
1849 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics, 0, 0),
1850 SD_BUS_PROPERTY("DNSSEC", "s", bus_property_get_dnssec_mode, 0, 0),
1851 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics, 0, 0),
1852 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported, 0, 0),
1853 SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_ntas, 0, 0),
1854 SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode, offsetof(Manager, dns_stub_listener_mode), 0),
1855
1856 SD_BUS_METHOD("ResolveHostname", "isit", "a(iiay)st", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
1857 SD_BUS_METHOD("ResolveAddress", "iiayt", "a(is)t", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
1858 SD_BUS_METHOD("ResolveRecord", "isqqt", "a(iqqay)t", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED),
1859 SD_BUS_METHOD("ResolveService", "isssit", "a(qqqsa(iiay)s)aayssst", bus_method_resolve_service, SD_BUS_VTABLE_UNPRIVILEGED),
1860 SD_BUS_METHOD("ResetStatistics", NULL, NULL, bus_method_reset_statistics, SD_BUS_VTABLE_UNPRIVILEGED),
1861 SD_BUS_METHOD("FlushCaches", NULL, NULL, bus_method_flush_caches, SD_BUS_VTABLE_UNPRIVILEGED),
1862 SD_BUS_METHOD("ResetServerFeatures", NULL, NULL, bus_method_reset_server_features, SD_BUS_VTABLE_UNPRIVILEGED),
1863 SD_BUS_METHOD("GetLink", "i", "o", bus_method_get_link, SD_BUS_VTABLE_UNPRIVILEGED),
1864 SD_BUS_METHOD("SetLinkDNS", "ia(iay)", NULL, bus_method_set_link_dns_servers, SD_BUS_VTABLE_UNPRIVILEGED),
1865 SD_BUS_METHOD("SetLinkDomains", "ia(sb)", NULL, bus_method_set_link_domains, SD_BUS_VTABLE_UNPRIVILEGED),
1866 SD_BUS_METHOD("SetLinkDefaultRoute", "ib", NULL, bus_method_set_link_default_route, SD_BUS_VTABLE_UNPRIVILEGED),
1867 SD_BUS_METHOD("SetLinkLLMNR", "is", NULL, bus_method_set_link_llmnr, SD_BUS_VTABLE_UNPRIVILEGED),
1868 SD_BUS_METHOD("SetLinkMulticastDNS", "is", NULL, bus_method_set_link_mdns, SD_BUS_VTABLE_UNPRIVILEGED),
1869 SD_BUS_METHOD("SetLinkDNSOverTLS", "is", NULL, bus_method_set_link_dns_over_tls, SD_BUS_VTABLE_UNPRIVILEGED),
1870 SD_BUS_METHOD("SetLinkDNSSEC", "is", NULL, bus_method_set_link_dnssec, SD_BUS_VTABLE_UNPRIVILEGED),
1871 SD_BUS_METHOD("SetLinkDNSSECNegativeTrustAnchors", "ias", NULL, bus_method_set_link_dnssec_negative_trust_anchors, SD_BUS_VTABLE_UNPRIVILEGED),
1872 SD_BUS_METHOD("RevertLink", "i", NULL, bus_method_revert_link, SD_BUS_VTABLE_UNPRIVILEGED),
1873
1874 SD_BUS_METHOD("RegisterService", "sssqqqaa{say}", "o", bus_method_register_service, SD_BUS_VTABLE_UNPRIVILEGED),
1875 SD_BUS_METHOD("UnregisterService", "o", NULL, bus_method_unregister_service, SD_BUS_VTABLE_UNPRIVILEGED),
1876 SD_BUS_VTABLE_END,
1877 };
1878
1879 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
1880 Manager *m = userdata;
1881 int b, r;
1882
1883 assert(message);
1884 assert(m);
1885
1886 r = sd_bus_message_read(message, "b", &b);
1887 if (r < 0) {
1888 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
1889 return 0;
1890 }
1891
1892 if (b)
1893 return 0;
1894
1895 log_debug("Coming back from suspend, verifying all RRs...");
1896
1897 manager_verify_all(m);
1898 return 0;
1899 }
1900
1901 int manager_connect_bus(Manager *m) {
1902 int r;
1903
1904 assert(m);
1905
1906 if (m->bus)
1907 return 0;
1908
1909 r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-resolve");
1910 if (r < 0)
1911 return log_error_errno(r, "Failed to connect to system bus: %m");
1912
1913 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable, m);
1914 if (r < 0)
1915 return log_error_errno(r, "Failed to register object: %m");
1916
1917 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/resolve1/link", "org.freedesktop.resolve1.Link", link_vtable, link_object_find, m);
1918 if (r < 0)
1919 return log_error_errno(r, "Failed to register link objects: %m");
1920
1921 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/resolve1/link", link_node_enumerator, m);
1922 if (r < 0)
1923 return log_error_errno(r, "Failed to register link enumerator: %m");
1924
1925 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/resolve1/dnssd", "org.freedesktop.resolve1.DnssdService", dnssd_vtable, dnssd_object_find, m);
1926 if (r < 0)
1927 return log_error_errno(r, "Failed to register dnssd objects: %m");
1928
1929 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/resolve1/dnssd", dnssd_node_enumerator, m);
1930 if (r < 0)
1931 return log_error_errno(r, "Failed to register dnssd enumerator: %m");
1932
1933 r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.resolve1", 0, NULL, NULL);
1934 if (r < 0)
1935 return log_error_errno(r, "Failed to request name: %m");
1936
1937 r = sd_bus_attach_event(m->bus, m->event, 0);
1938 if (r < 0)
1939 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1940
1941 r = sd_bus_match_signal_async(
1942 m->bus,
1943 NULL,
1944 "org.freedesktop.login1",
1945 "/org/freedesktop/login1",
1946 "org.freedesktop.login1.Manager",
1947 "PrepareForSleep",
1948 match_prepare_for_sleep,
1949 NULL,
1950 m);
1951 if (r < 0)
1952 log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m");
1953
1954 return 0;
1955 }
1956
1957 int _manager_send_changed(Manager *manager, const char *property, ...) {
1958 assert(manager);
1959
1960 char **l = strv_from_stdarg_alloca(property);
1961
1962 int r = sd_bus_emit_properties_changed_strv(
1963 manager->bus,
1964 "/org/freedesktop/resolve1",
1965 "org.freedesktop.resolve1.Manager",
1966 l);
1967 if (r < 0)
1968 log_notice_errno(r, "Failed to emit notification about changed property %s: %m", property);
1969 return r;
1970 }