]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-bus.c
man/systemd.mount: tmpfs automatically gains After=swap.target dep
[thirdparty/systemd.git] / src / resolve / resolved-bus.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "bus-common-errors.h"
5 #include "bus-get-properties.h"
6 #include "bus-locator.h"
7 #include "bus-log-control-api.h"
8 #include "bus-message-util.h"
9 #include "bus-polkit.h"
10 #include "dns-domain.h"
11 #include "format-util.h"
12 #include "memory-util.h"
13 #include "missing_capability.h"
14 #include "resolved-bus.h"
15 #include "resolved-def.h"
16 #include "resolved-dns-synthesize.h"
17 #include "resolved-dnssd-bus.h"
18 #include "resolved-dnssd.h"
19 #include "resolved-link-bus.h"
20 #include "resolved-resolv-conf.h"
21 #include "socket-netlink.h"
22 #include "stdio-util.h"
23 #include "strv.h"
24 #include "syslog-util.h"
25 #include "user-util.h"
26 #include "utf8.h"
27
28 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_resolve_support, resolve_support, ResolveSupport);
29
30 static int query_on_bus_track(sd_bus_track *t, void *userdata) {
31 DnsQuery *q = ASSERT_PTR(userdata);
32
33 assert(t);
34
35 if (!DNS_TRANSACTION_IS_LIVE(q->state))
36 return 0;
37
38 log_debug("Client of active query vanished, aborting query.");
39 dns_query_complete(q, DNS_TRANSACTION_ABORTED);
40 return 0;
41 }
42
43 static int dns_query_bus_track(DnsQuery *q, sd_bus_message *m) {
44 int r;
45
46 assert(q);
47 assert(m);
48
49 if (!q->bus_track) {
50 r = sd_bus_track_new(sd_bus_message_get_bus(m), &q->bus_track, query_on_bus_track, q);
51 if (r < 0)
52 return r;
53 }
54
55 r = sd_bus_track_add_sender(q->bus_track, m);
56 if (r < 0)
57 return r;
58
59 return 0;
60 }
61
62 static sd_bus_message *dns_query_steal_request(DnsQuery *q) {
63 assert(q);
64
65 /* Find the main query, it's the one that owns the message */
66 while (q->auxiliary_for)
67 q = q->auxiliary_for;
68
69 /* Let's take the request message out of the DnsQuery object, so that we never send requests twice */
70 return TAKE_PTR(q->bus_request);
71 }
72
73 _sd_printf_(3, 4) static int reply_method_errorf(
74 DnsQuery *query,
75 const char *error_name,
76 const char *format,
77 ...) {
78
79 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
80 va_list ap;
81 int r;
82
83 assert(query);
84 assert(format);
85
86 req = dns_query_steal_request(query);
87 if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
88 return 0;
89
90 va_start(ap, format);
91 r = sd_bus_reply_method_errorfv(req, error_name, format, ap);
92 va_end(ap);
93
94 return r;
95 }
96
97 _sd_printf_(3, 4) static int reply_method_errnof(
98 DnsQuery *query,
99 int err,
100 const char *format,
101 ...) {
102
103 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
104 int r;
105
106 assert(query);
107
108 req = dns_query_steal_request(query);
109 if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
110 return 0;
111
112 if (format) {
113 va_list ap;
114
115 va_start(ap, format);
116 r = sd_bus_reply_method_errnofv(req, err, format, ap);
117 va_end(ap);
118 } else
119 r = sd_bus_reply_method_errno(req, err, NULL);
120
121 return r;
122 }
123
124 static int reply_query_state(DnsQuery *q) {
125 assert(q);
126
127 switch (q->state) {
128
129 case DNS_TRANSACTION_NO_SERVERS:
130 return reply_method_errorf(q, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
131
132 case DNS_TRANSACTION_TIMEOUT:
133 return reply_method_errorf(q, SD_BUS_ERROR_TIMEOUT, "Query timed out");
134
135 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
136 return reply_method_errorf(q, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
137
138 case DNS_TRANSACTION_INVALID_REPLY:
139 return reply_method_errorf(q, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
140
141 case DNS_TRANSACTION_ERRNO:
142 return reply_method_errnof(q, q->answer_errno, "Lookup failed due to system error: %m");
143
144 case DNS_TRANSACTION_ABORTED:
145 return reply_method_errorf(q, BUS_ERROR_ABORTED, "Query aborted");
146
147 case DNS_TRANSACTION_DNSSEC_FAILED:
148 return reply_method_errorf(q, BUS_ERROR_DNSSEC_FAILED, "DNSSEC validation failed: %s",
149 dnssec_result_to_string(q->answer_dnssec_result));
150
151 case DNS_TRANSACTION_NO_TRUST_ANCHOR:
152 return reply_method_errorf(q, BUS_ERROR_NO_TRUST_ANCHOR, "No suitable trust anchor known");
153
154 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED:
155 return reply_method_errorf(q, BUS_ERROR_RR_TYPE_UNSUPPORTED, "Server does not support requested resource record type");
156
157 case DNS_TRANSACTION_NETWORK_DOWN:
158 return reply_method_errorf(q, BUS_ERROR_NETWORK_DOWN, "Network is down");
159
160 case DNS_TRANSACTION_NOT_FOUND:
161 /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
162 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
163 return reply_method_errorf(q, BUS_ERROR_DNS_NXDOMAIN, "'%s' not found", dns_query_string(q));
164
165 case DNS_TRANSACTION_NO_SOURCE:
166 return reply_method_errorf(q, BUS_ERROR_NO_SOURCE, "All suitable resolution sources turned off");
167
168 case DNS_TRANSACTION_STUB_LOOP:
169 return reply_method_errorf(q, BUS_ERROR_STUB_LOOP, "Configured DNS server loops back to us");
170
171 case DNS_TRANSACTION_RCODE_FAILURE: {
172 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
173 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
174
175 req = dns_query_steal_request(q);
176 if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
177 return 0;
178
179 if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
180 sd_bus_error_setf(&error, BUS_ERROR_DNS_NXDOMAIN, "Name '%s' not found", dns_query_string(q));
181 else {
182 const char *rc, *n;
183
184 rc = FORMAT_DNS_RCODE(q->answer_rcode);
185 n = strjoina(_BUS_ERROR_DNS, rc);
186 sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", dns_query_string(q), rc);
187 }
188
189 return sd_bus_reply_method_error(req, &error);
190 }
191
192 case DNS_TRANSACTION_NULL:
193 case DNS_TRANSACTION_PENDING:
194 case DNS_TRANSACTION_VALIDATING:
195 case DNS_TRANSACTION_SUCCESS:
196 default:
197 assert_not_reached();
198 }
199 }
200
201 static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
202 int r;
203
204 assert(reply);
205 assert(rr);
206
207 r = sd_bus_message_open_container(reply, 'r', "iiay");
208 if (r < 0)
209 return r;
210
211 r = sd_bus_message_append(reply, "i", ifindex);
212 if (r < 0)
213 return r;
214
215 if (rr->key->type == DNS_TYPE_A) {
216 r = sd_bus_message_append(reply, "i", AF_INET);
217 if (r < 0)
218 return r;
219
220 r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
221
222 } else if (rr->key->type == DNS_TYPE_AAAA) {
223 r = sd_bus_message_append(reply, "i", AF_INET6);
224 if (r < 0)
225 return r;
226
227 r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
228 } else
229 return -EAFNOSUPPORT;
230
231 if (r < 0)
232 return r;
233
234 r = sd_bus_message_close_container(reply);
235 if (r < 0)
236 return r;
237
238 return 0;
239 }
240
241 static void bus_method_resolve_hostname_complete(DnsQuery *query) {
242 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
243 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
244 _cleanup_(dns_query_freep) DnsQuery *q = query;
245 _cleanup_free_ char *normalized = NULL;
246 DnsQuestion *question;
247 DnsResourceRecord *rr;
248 unsigned added = 0;
249 int ifindex, r;
250
251 assert(q);
252
253 if (q->state != DNS_TRANSACTION_SUCCESS) {
254 r = reply_query_state(q);
255 goto finish;
256 }
257
258 r = dns_query_process_cname_many(q);
259 if (r == -ELOOP) {
260 r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
261 goto finish;
262 }
263 if (r < 0)
264 goto finish;
265 if (r == DNS_QUERY_CNAME) {
266 /* This was a cname, and the query was restarted. */
267 TAKE_PTR(q);
268 return;
269 }
270
271 r = sd_bus_message_new_method_return(q->bus_request, &reply);
272 if (r < 0)
273 goto finish;
274
275 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
276 if (r < 0)
277 goto finish;
278
279 question = dns_query_question_for_protocol(q, q->answer_protocol);
280
281 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
282
283 r = dns_question_matches_rr(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
284 if (r < 0)
285 goto finish;
286 if (r == 0)
287 continue;
288
289 r = append_address(reply, rr, ifindex);
290 if (r < 0)
291 goto finish;
292
293 if (!canonical)
294 canonical = dns_resource_record_ref(rr);
295
296 added++;
297 }
298
299 if (added <= 0) {
300 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
301 goto finish;
302 }
303
304 r = sd_bus_message_close_container(reply);
305 if (r < 0)
306 goto finish;
307
308 /* The key names are not necessarily normalized, make sure that they are when we return them to our
309 * bus clients. */
310 assert(canonical);
311 r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
312 if (r < 0)
313 goto finish;
314
315 /* Return the precise spelling and uppercasing and CNAME target reported by the server */
316 r = sd_bus_message_append(
317 reply, "st",
318 normalized,
319 dns_query_reply_flags_make(q));
320 if (r < 0)
321 goto finish;
322
323 q->bus_request = sd_bus_message_unref(q->bus_request);
324 r = sd_bus_send(q->manager->bus, reply, NULL);
325
326 finish:
327 if (r < 0) {
328 log_error_errno(r, "Failed to send hostname reply: %m");
329 (void) reply_method_errnof(q, r, NULL);
330 }
331 }
332
333 static int validate_and_mangle_flags(
334 const char *name,
335 uint64_t *flags,
336 uint64_t ok,
337 sd_bus_error *error) {
338
339 assert(flags);
340
341 /* Checks that the client supplied interface index and flags parameter actually are valid and make
342 * sense in our method call context. Specifically:
343 *
344 * 1. Checks that the interface index is either 0 (meaning *all* interfaces) or positive
345 *
346 * 2. Only the protocols flags and a bunch of NO_XYZ flags are set, at most. Plus additional flags
347 * specific to our method, passed in the "ok" parameter.
348 *
349 * 3. If zero protocol flags are specified it is automatically turned into *all* protocols. This way
350 * clients can simply pass 0 as flags and all will work as it should. They can also use this so
351 * that clients don't have to know all the protocols resolved implements, but can just specify 0
352 * to mean "all supported protocols".
353 */
354
355 if (*flags & ~(SD_RESOLVED_PROTOCOLS_ALL|
356 SD_RESOLVED_NO_CNAME|
357 SD_RESOLVED_NO_VALIDATE|
358 SD_RESOLVED_NO_SYNTHESIZE|
359 SD_RESOLVED_NO_CACHE|
360 SD_RESOLVED_NO_ZONE|
361 SD_RESOLVED_NO_TRUST_ANCHOR|
362 SD_RESOLVED_NO_NETWORK|
363 SD_RESOLVED_NO_STALE|
364 ok))
365 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
366
367 if ((*flags & SD_RESOLVED_PROTOCOLS_ALL) == 0) /* If no protocol is enabled, enable all */
368 *flags |= SD_RESOLVED_PROTOCOLS_ALL;
369
370 /* Imply SD_RESOLVED_NO_SEARCH if permitted and name is dot suffixed. */
371 if (name && FLAGS_SET(ok, SD_RESOLVED_NO_SEARCH) && dns_name_dot_suffixed(name) > 0)
372 *flags |= SD_RESOLVED_NO_SEARCH;
373
374 return 0;
375 }
376
377 static int parse_as_address(sd_bus_message *m, int ifindex, const char *hostname, int family, uint64_t flags) {
378 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
379 _cleanup_free_ char *canonical = NULL;
380 union in_addr_union parsed;
381 int r, ff, parsed_ifindex = 0;
382
383 /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
384 * let's not attempt to look it up. */
385
386 r = in_addr_ifindex_from_string_auto(hostname, &ff, &parsed, &parsed_ifindex);
387 if (r < 0) /* not an address */
388 return 0;
389
390 if (family != AF_UNSPEC && ff != family)
391 return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address is not of the requested family.");
392 if (ifindex > 0 && parsed_ifindex > 0 && parsed_ifindex != ifindex)
393 return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address interface index does not match requested interface.");
394
395 if (parsed_ifindex > 0)
396 ifindex = parsed_ifindex;
397
398 r = sd_bus_message_new_method_return(m, &reply);
399 if (r < 0)
400 return r;
401
402 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
403 if (r < 0)
404 return r;
405
406 r = sd_bus_message_open_container(reply, 'r', "iiay");
407 if (r < 0)
408 return r;
409
410 r = sd_bus_message_append(reply, "ii", ifindex, ff);
411 if (r < 0)
412 return r;
413
414 r = sd_bus_message_append_array(reply, 'y', &parsed, FAMILY_ADDRESS_SIZE(ff));
415 if (r < 0)
416 return r;
417
418 r = sd_bus_message_close_container(reply);
419 if (r < 0)
420 return r;
421
422 r = sd_bus_message_close_container(reply);
423 if (r < 0)
424 return r;
425
426 /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
427 * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
428 * omissions are always done the same way). */
429 r = in_addr_ifindex_to_string(ff, &parsed, ifindex, &canonical);
430 if (r < 0)
431 return r;
432
433 r = sd_bus_message_append(reply, "st", canonical,
434 SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags), ff, true, true) |
435 SD_RESOLVED_SYNTHETIC);
436 if (r < 0)
437 return r;
438
439 return sd_bus_send(sd_bus_message_get_bus(m), reply, NULL);
440 }
441
442 void bus_client_log(sd_bus_message *m, const char *what) {
443 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
444 const char *comm = NULL;
445 uid_t uid = UID_INVALID;
446 pid_t pid = 0;
447 int r;
448
449 assert(m);
450 assert(what);
451
452 if (!DEBUG_LOGGING)
453 return;
454
455 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_COMM|SD_BUS_CREDS_AUGMENT, &creds);
456 if (r < 0)
457 return (void) log_debug_errno(r, "Failed to query client credentials, ignoring: %m");
458
459 (void) sd_bus_creds_get_uid(creds, &uid);
460 (void) sd_bus_creds_get_pid(creds, &pid);
461 (void) sd_bus_creds_get_comm(creds, &comm);
462
463 log_debug("D-Bus %s request from client PID " PID_FMT " (%s) with UID " UID_FMT,
464 what, pid, strna(comm), uid);
465 }
466
467 static int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
468 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
469 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
470 Manager *m = ASSERT_PTR(userdata);
471 const char *hostname;
472 int family, ifindex;
473 uint64_t flags;
474 int r;
475
476 assert(message);
477
478 assert_cc(sizeof(int) == sizeof(int32_t));
479
480 r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
481 if (r < 0)
482 return r;
483
484 if (ifindex < 0)
485 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
486
487 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
488 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
489
490 r = validate_and_mangle_flags(hostname, &flags, SD_RESOLVED_NO_SEARCH, error);
491 if (r < 0)
492 return r;
493
494 r = parse_as_address(message, ifindex, hostname, family, flags);
495 if (r != 0)
496 return r;
497
498 r = dns_name_is_valid(hostname);
499 if (r < 0)
500 return r;
501 if (r == 0)
502 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
503
504 r = dns_question_new_address(&question_utf8, family, hostname, false);
505 if (r < 0)
506 return r;
507
508 r = dns_question_new_address(&question_idna, family, hostname, true);
509 if (r < 0 && r != -EALREADY)
510 return r;
511
512 bus_client_log(message, "hostname resolution");
513
514 r = dns_query_new(m, &q, question_utf8, question_idna ?: question_utf8, NULL, ifindex, flags);
515 if (r < 0)
516 return r;
517
518 q->bus_request = sd_bus_message_ref(message);
519 q->request_family = family;
520 q->complete = bus_method_resolve_hostname_complete;
521
522 r = dns_query_bus_track(q, message);
523 if (r < 0)
524 return r;
525
526 r = dns_query_go(q);
527 if (r < 0)
528 return r;
529
530 TAKE_PTR(q);
531 return 1;
532 }
533
534 static void bus_method_resolve_address_complete(DnsQuery *query) {
535 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
536 _cleanup_(dns_query_freep) DnsQuery *q = query;
537 DnsQuestion *question;
538 DnsResourceRecord *rr;
539 unsigned added = 0;
540 int ifindex, r;
541
542 assert(q);
543
544 if (q->state != DNS_TRANSACTION_SUCCESS) {
545 r = reply_query_state(q);
546 goto finish;
547 }
548
549 r = dns_query_process_cname_many(q);
550 if (r == -ELOOP) {
551 r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
552 goto finish;
553 }
554 if (r < 0)
555 goto finish;
556 if (r == DNS_QUERY_CNAME) {
557 /* This was a cname, and the query was restarted. */
558 TAKE_PTR(q);
559 return;
560 }
561
562 r = sd_bus_message_new_method_return(q->bus_request, &reply);
563 if (r < 0)
564 goto finish;
565
566 r = sd_bus_message_open_container(reply, 'a', "(is)");
567 if (r < 0)
568 goto finish;
569
570 question = dns_query_question_for_protocol(q, q->answer_protocol);
571
572 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
573 _cleanup_free_ char *normalized = NULL;
574
575 r = dns_question_matches_rr(question, rr, NULL);
576 if (r < 0)
577 goto finish;
578 if (r == 0)
579 continue;
580
581 r = dns_name_normalize(rr->ptr.name, 0, &normalized);
582 if (r < 0)
583 goto finish;
584
585 r = sd_bus_message_append(reply, "(is)", ifindex, normalized);
586 if (r < 0)
587 goto finish;
588
589 added++;
590 }
591
592 if (added <= 0) {
593 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR,
594 "Address %s does not have any RR of requested type",
595 IN_ADDR_TO_STRING(q->request_family, &q->request_address));
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", dns_query_reply_flags_make(q));
604 if (r < 0)
605 goto finish;
606
607 q->bus_request = sd_bus_message_unref(q->bus_request);
608 r = sd_bus_send(q->manager->bus, reply, NULL);
609
610 finish:
611 if (r < 0) {
612 log_error_errno(r, "Failed to send address reply: %m");
613 (void) reply_method_errnof(q, r, NULL);
614 }
615 }
616
617 static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
618 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
619 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
620 Manager *m = ASSERT_PTR(userdata);
621 union in_addr_union a;
622 int family, ifindex;
623 uint64_t flags;
624 int r;
625
626 assert(message);
627
628 assert_cc(sizeof(int) == sizeof(int32_t));
629
630 r = sd_bus_message_read(message, "i", &ifindex);
631 if (r < 0)
632 return r;
633
634 r = bus_message_read_in_addr_auto(message, error, &family, &a);
635 if (r < 0)
636 return r;
637
638 r = sd_bus_message_read(message, "t", &flags);
639 if (r < 0)
640 return r;
641
642 if (ifindex < 0)
643 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
644
645 r = validate_and_mangle_flags(NULL, &flags, 0, error);
646 if (r < 0)
647 return r;
648
649 r = dns_question_new_reverse(&question, family, &a);
650 if (r < 0)
651 return r;
652
653 bus_client_log(message, "address resolution");
654
655 r = dns_query_new(m, &q, question, question, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH);
656 if (r < 0)
657 return r;
658
659 q->bus_request = sd_bus_message_ref(message);
660 q->request_family = family;
661 q->request_address = a;
662 q->complete = bus_method_resolve_address_complete;
663
664 r = dns_query_bus_track(q, message);
665 if (r < 0)
666 return r;
667
668 r = dns_query_go(q);
669 if (r < 0)
670 return r;
671
672 TAKE_PTR(q);
673 return 1;
674 }
675
676 static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
677 int r;
678
679 assert(m);
680 assert(rr);
681
682 r = sd_bus_message_open_container(m, 'r', "iqqay");
683 if (r < 0)
684 return r;
685
686 r = sd_bus_message_append(m, "iqq",
687 ifindex,
688 rr->key->class,
689 rr->key->type);
690 if (r < 0)
691 return r;
692
693 r = dns_resource_record_to_wire_format(rr, false);
694 if (r < 0)
695 return r;
696
697 r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
698 if (r < 0)
699 return r;
700
701 return sd_bus_message_close_container(m);
702 }
703
704 static void bus_method_resolve_record_complete(DnsQuery *query) {
705 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
706 _cleanup_(dns_query_freep) DnsQuery *q = query;
707 DnsResourceRecord *rr;
708 DnsQuestion *question;
709 unsigned added = 0;
710 int ifindex;
711 int r;
712
713 assert(q);
714
715 if (q->state != DNS_TRANSACTION_SUCCESS) {
716 r = reply_query_state(q);
717 goto finish;
718 }
719
720 r = dns_query_process_cname_many(q);
721 if (r == -ELOOP) {
722 r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
723 goto finish;
724 }
725 if (r < 0)
726 goto finish;
727 if (r == DNS_QUERY_CNAME) {
728 /* This was a cname, and the query was restarted. */
729 TAKE_PTR(q);
730 return;
731 }
732
733 r = sd_bus_message_new_method_return(q->bus_request, &reply);
734 if (r < 0)
735 goto finish;
736
737 r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
738 if (r < 0)
739 goto finish;
740
741 question = dns_query_question_for_protocol(q, q->answer_protocol);
742
743 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
744 r = dns_question_matches_rr(question, rr, NULL);
745 if (r < 0)
746 goto finish;
747 if (r == 0)
748 continue;
749
750 r = bus_message_append_rr(reply, rr, ifindex);
751 if (r < 0)
752 goto finish;
753
754 added++;
755 }
756
757 if (added <= 0) {
758 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", dns_query_string(q));
759 goto finish;
760 }
761
762 r = sd_bus_message_close_container(reply);
763 if (r < 0)
764 goto finish;
765
766 r = sd_bus_message_append(reply, "t", dns_query_reply_flags_make(q));
767 if (r < 0)
768 goto finish;
769
770 q->bus_request = sd_bus_message_unref(q->bus_request);
771 r = sd_bus_send(q->manager->bus, reply, NULL);
772
773 finish:
774 if (r < 0) {
775 log_error_errno(r, "Failed to send record reply: %m");
776 (void) reply_method_errnof(q, r, NULL);
777 }
778 }
779
780 static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
781 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
782 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
783 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
784 Manager *m = ASSERT_PTR(userdata);
785 uint16_t class, type;
786 const char *name;
787 int r, ifindex;
788 uint64_t flags;
789
790 assert(message);
791
792 assert_cc(sizeof(int) == sizeof(int32_t));
793
794 r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
795 if (r < 0)
796 return r;
797
798 if (ifindex < 0)
799 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
800
801 r = dns_name_is_valid(name);
802 if (r < 0)
803 return r;
804 if (r == 0)
805 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
806
807 if (!dns_type_is_valid_query(type))
808 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified resource record type %" PRIu16 " may not be used in a query.", type);
809 if (dns_type_is_zone_transer(type))
810 return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Zone transfers not permitted via this programming interface.");
811 if (dns_type_is_obsolete(type))
812 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
813
814 r = validate_and_mangle_flags(name, &flags, 0, error);
815 if (r < 0)
816 return r;
817
818 question = dns_question_new(1);
819 if (!question)
820 return -ENOMEM;
821
822 key = dns_resource_key_new(class, type, name);
823 if (!key)
824 return -ENOMEM;
825
826 r = dns_question_add(question, key, 0);
827 if (r < 0)
828 return r;
829
830 bus_client_log(message, "resource record resolution");
831
832 /* Setting SD_RESOLVED_CLAMP_TTL: let's request that the TTL is fixed up for locally cached entries,
833 * after all we return it in the wire format blob. */
834 r = dns_query_new(m, &q, question, question, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH|SD_RESOLVED_CLAMP_TTL);
835 if (r < 0)
836 return r;
837
838 q->bus_request = sd_bus_message_ref(message);
839 q->complete = bus_method_resolve_record_complete;
840
841 r = dns_query_bus_track(q, message);
842 if (r < 0)
843 return r;
844
845 r = dns_query_go(q);
846 if (r < 0)
847 return r;
848
849 TAKE_PTR(q);
850 return 1;
851 }
852
853 static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
854 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
855 _cleanup_free_ char *normalized = NULL;
856 int r;
857
858 assert(q);
859 assert(reply);
860 assert(rr);
861 assert(rr->key);
862
863 if (rr->key->type != DNS_TYPE_SRV)
864 return 0;
865
866 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
867 /* First, let's see if we could find an appropriate A or AAAA
868 * record for the SRV record */
869 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
870 DnsResourceRecord *zz;
871 DnsQuestion *question;
872
873 if (aux->state != DNS_TRANSACTION_SUCCESS)
874 continue;
875 if (aux->auxiliary_result != 0)
876 continue;
877
878 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
879
880 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
881 if (r < 0)
882 return r;
883 if (r == 0)
884 continue;
885
886 DNS_ANSWER_FOREACH(zz, aux->answer) {
887
888 r = dns_question_matches_rr(question, zz, NULL);
889 if (r < 0)
890 return r;
891 if (r == 0)
892 continue;
893
894 canonical = dns_resource_record_ref(zz);
895 break;
896 }
897
898 if (canonical)
899 break;
900 }
901
902 /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
903 if (!canonical)
904 return 0;
905 }
906
907 r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
908 if (r < 0)
909 return r;
910
911 r = dns_name_normalize(rr->srv.name, 0, &normalized);
912 if (r < 0)
913 return r;
914
915 r = sd_bus_message_append(
916 reply,
917 "qqqs",
918 rr->srv.priority, rr->srv.weight, rr->srv.port, normalized);
919 if (r < 0)
920 return r;
921
922 r = sd_bus_message_open_container(reply, 'a', "(iiay)");
923 if (r < 0)
924 return r;
925
926 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
927 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
928 DnsResourceRecord *zz;
929 DnsQuestion *question;
930 int ifindex;
931
932 if (aux->state != DNS_TRANSACTION_SUCCESS)
933 continue;
934 if (aux->auxiliary_result != 0)
935 continue;
936
937 question = dns_query_question_for_protocol(aux, aux->answer_protocol);
938
939 r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
940 if (r < 0)
941 return r;
942 if (r == 0)
943 continue;
944
945 DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
946
947 r = dns_question_matches_rr(question, zz, NULL);
948 if (r < 0)
949 return r;
950 if (r == 0)
951 continue;
952
953 r = append_address(reply, zz, ifindex);
954 if (r < 0)
955 return r;
956 }
957 }
958 }
959
960 r = sd_bus_message_close_container(reply);
961 if (r < 0)
962 return r;
963
964 if (canonical) {
965 normalized = mfree(normalized);
966
967 r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
968 if (r < 0)
969 return r;
970 }
971
972 /* Note that above we appended the hostname as encoded in the
973 * SRV, and here the canonical hostname this maps to. */
974 r = sd_bus_message_append(reply, "s", normalized);
975 if (r < 0)
976 return r;
977
978 r = sd_bus_message_close_container(reply);
979 if (r < 0)
980 return r;
981
982 return 1;
983 }
984
985 static int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
986 int r;
987
988 assert(reply);
989 assert(rr);
990 assert(rr->key);
991
992 if (rr->key->type != DNS_TYPE_TXT)
993 return 0;
994
995 LIST_FOREACH(items, i, rr->txt.items) {
996
997 if (i->length <= 0)
998 continue;
999
1000 r = sd_bus_message_append_array(reply, 'y', i->data, i->length);
1001 if (r < 0)
1002 return r;
1003 }
1004
1005 return 1;
1006 }
1007
1008 static void resolve_service_all_complete(DnsQuery *query) {
1009 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
1010 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1011 _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL;
1012 _cleanup_(dns_query_freep) DnsQuery *q = query;
1013 DnsQuestion *question;
1014 DnsResourceRecord *rr;
1015 unsigned added = 0;
1016 int r;
1017
1018 assert(q);
1019
1020 if (q->block_all_complete > 0) {
1021 TAKE_PTR(q);
1022 return;
1023 }
1024
1025 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1026 DnsQuery *bad = NULL;
1027 bool have_success = false;
1028
1029 LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
1030
1031 switch (aux->state) {
1032
1033 case DNS_TRANSACTION_PENDING:
1034 /* If an auxiliary query is still pending, let's wait */
1035 TAKE_PTR(q);
1036 return;
1037
1038 case DNS_TRANSACTION_SUCCESS:
1039 if (aux->auxiliary_result == 0)
1040 have_success = true;
1041 else
1042 bad = aux;
1043 break;
1044
1045 default:
1046 bad = aux;
1047 break;
1048 }
1049 }
1050
1051 if (!have_success) {
1052 /* We can only return one error, hence pick the last error we encountered */
1053
1054 assert(bad);
1055
1056 if (bad->state == DNS_TRANSACTION_SUCCESS) {
1057 assert(bad->auxiliary_result != 0);
1058
1059 if (bad->auxiliary_result == -ELOOP) {
1060 r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad));
1061 goto finish;
1062 }
1063
1064 assert(bad->auxiliary_result < 0);
1065 r = bad->auxiliary_result;
1066 goto finish;
1067 }
1068
1069 r = reply_query_state(bad);
1070 goto finish;
1071 }
1072 }
1073
1074 r = sd_bus_message_new_method_return(q->bus_request, &reply);
1075 if (r < 0)
1076 goto finish;
1077
1078 r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)");
1079 if (r < 0)
1080 goto finish;
1081
1082 question = dns_query_question_for_protocol(q, q->answer_protocol);
1083
1084 DNS_ANSWER_FOREACH(rr, q->answer) {
1085 r = dns_question_matches_rr(question, rr, NULL);
1086 if (r < 0)
1087 goto finish;
1088 if (r == 0)
1089 continue;
1090
1091 r = append_srv(q, reply, rr);
1092 if (r < 0)
1093 goto finish;
1094 if (r == 0) /* not an SRV record */
1095 continue;
1096
1097 if (!canonical)
1098 canonical = dns_resource_record_ref(rr);
1099
1100 added++;
1101 }
1102
1103 if (added <= 0) {
1104 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
1105 goto finish;
1106 }
1107
1108 r = sd_bus_message_close_container(reply);
1109 if (r < 0)
1110 goto finish;
1111
1112 r = sd_bus_message_open_container(reply, 'a', "ay");
1113 if (r < 0)
1114 goto finish;
1115
1116 DNS_ANSWER_FOREACH(rr, q->answer) {
1117 r = dns_question_matches_rr(question, rr, NULL);
1118 if (r < 0)
1119 goto finish;
1120 if (r == 0)
1121 continue;
1122
1123 r = append_txt(reply, rr);
1124 if (r < 0)
1125 goto finish;
1126 }
1127
1128 r = sd_bus_message_close_container(reply);
1129 if (r < 0)
1130 goto finish;
1131
1132 assert(canonical);
1133 r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain);
1134 if (r < 0)
1135 goto finish;
1136
1137 r = sd_bus_message_append(
1138 reply,
1139 "ssst",
1140 name, type, domain,
1141 dns_query_reply_flags_make(q));
1142 if (r < 0)
1143 goto finish;
1144
1145 q->bus_request = sd_bus_message_unref(q->bus_request);
1146 r = sd_bus_send(q->manager->bus, reply, NULL);
1147
1148 finish:
1149 if (r < 0) {
1150 log_error_errno(r, "Failed to send service reply: %m");
1151 (void) reply_method_errnof(q, r, NULL);
1152 }
1153 }
1154
1155 static void resolve_service_hostname_complete(DnsQuery *q) {
1156 int r;
1157
1158 assert(q);
1159 assert(q->auxiliary_for);
1160
1161 if (q->state != DNS_TRANSACTION_SUCCESS) {
1162 resolve_service_all_complete(q->auxiliary_for);
1163 return;
1164 }
1165
1166 r = dns_query_process_cname_many(q);
1167 if (r == DNS_QUERY_CNAME) /* This was a cname, and the query was restarted. */
1168 return;
1169
1170 /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1171 q->auxiliary_result = r < 0 ? r : 0;
1172 resolve_service_all_complete(q->auxiliary_for);
1173 }
1174
1175 static int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) {
1176 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
1177 _cleanup_(dns_query_freep) DnsQuery *aux = NULL;
1178 int r;
1179
1180 assert(q);
1181 assert(rr);
1182 assert(rr->key);
1183 assert(rr->key->type == DNS_TYPE_SRV);
1184
1185 /* OK, we found an SRV record for the service. Let's resolve
1186 * the hostname included in it */
1187
1188 r = dns_question_new_address(&question, q->request_family, rr->srv.name, false);
1189 if (r < 0)
1190 return r;
1191
1192 r = dns_query_new(q->manager, &aux, question, question, NULL, ifindex, q->flags|SD_RESOLVED_NO_SEARCH);
1193 if (r < 0)
1194 return r;
1195
1196 aux->request_family = q->request_family;
1197 aux->complete = resolve_service_hostname_complete;
1198
1199 r = dns_query_make_auxiliary(aux, q);
1200 if (r == -EAGAIN)
1201 /* Too many auxiliary lookups? If so, don't complain,
1202 * let's just not add this one, we already have more
1203 * than enough */
1204 return 0;
1205 if (r < 0)
1206 return r;
1207
1208 /* Note that auxiliary queries do not track the original bus
1209 * client, only the primary request does that. */
1210
1211 r = dns_query_go(aux);
1212 if (r < 0)
1213 return r;
1214
1215 TAKE_PTR(aux);
1216 return 1;
1217 }
1218
1219 static void bus_method_resolve_service_complete(DnsQuery *query) {
1220 _cleanup_(dns_query_freep) DnsQuery *q = query;
1221 bool has_root_domain = false;
1222 DnsResourceRecord *rr;
1223 DnsQuestion *question;
1224 unsigned found = 0;
1225 int ifindex, r;
1226
1227 assert(q);
1228
1229 if (q->state != DNS_TRANSACTION_SUCCESS) {
1230 r = reply_query_state(q);
1231 goto finish;
1232 }
1233
1234 r = dns_query_process_cname_many(q);
1235 if (r == -ELOOP) {
1236 r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
1237 goto finish;
1238 }
1239 if (r < 0)
1240 goto finish;
1241 if (r == DNS_QUERY_CNAME) {
1242 /* This was a cname, and the query was restarted. */
1243 TAKE_PTR(q);
1244 return;
1245 }
1246
1247 question = dns_query_question_for_protocol(q, q->answer_protocol);
1248
1249 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
1250 r = dns_question_matches_rr(question, rr, NULL);
1251 if (r < 0)
1252 goto finish;
1253 if (r == 0)
1254 continue;
1255
1256 if (rr->key->type != DNS_TYPE_SRV)
1257 continue;
1258
1259 if (dns_name_is_root(rr->srv.name)) {
1260 has_root_domain = true;
1261 continue;
1262 }
1263
1264 if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1265 q->block_all_complete++;
1266 r = resolve_service_hostname(q, rr, ifindex);
1267 q->block_all_complete--;
1268
1269 if (r < 0)
1270 goto finish;
1271 }
1272
1273 found++;
1274 }
1275
1276 if (has_root_domain && found <= 0) {
1277 /* If there's exactly one SRV RR and it uses the root domain as hostname, then the service is
1278 * explicitly not offered on the domain. Report this as a recognizable error. See RFC 2782,
1279 * Section "Usage Rules". */
1280 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide the requested service", dns_query_string(q));
1281 goto finish;
1282 }
1283
1284 if (found <= 0) {
1285 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
1286 goto finish;
1287 }
1288
1289 /* Maybe we are already finished? check now... */
1290 resolve_service_all_complete(TAKE_PTR(q));
1291 return;
1292
1293 finish:
1294 if (r < 0) {
1295 log_error_errno(r, "Failed to send service reply: %m");
1296 (void) reply_method_errnof(q, r, NULL);
1297 }
1298 }
1299
1300 static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1301 _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
1302 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
1303 const char *name, *type, *domain;
1304 Manager *m = ASSERT_PTR(userdata);
1305 int family, ifindex;
1306 uint64_t flags;
1307 int r;
1308
1309 assert(message);
1310
1311 assert_cc(sizeof(int) == sizeof(int32_t));
1312
1313 r = sd_bus_message_read(message, "isssit", &ifindex, &name, &type, &domain, &family, &flags);
1314 if (r < 0)
1315 return r;
1316
1317 if (ifindex < 0)
1318 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
1319
1320 if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
1321 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
1322
1323 if (isempty(name))
1324 name = NULL;
1325 else if (!dns_service_name_is_valid(name))
1326 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid service name '%s'", name);
1327
1328 if (isempty(type))
1329 type = NULL;
1330 else if (!dns_srv_type_is_valid(type))
1331 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid SRV service type '%s'", type);
1332
1333 r = dns_name_is_valid(domain);
1334 if (r < 0)
1335 return r;
1336 if (r == 0)
1337 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid domain '%s'", domain);
1338
1339 if (name && !type)
1340 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type.");
1341
1342 r = validate_and_mangle_flags(name, &flags, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS, error);
1343 if (r < 0)
1344 return r;
1345
1346 r = dns_question_new_service(&question_utf8, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), false);
1347 if (r < 0)
1348 return r;
1349
1350 r = dns_question_new_service(&question_idna, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), true);
1351 if (r < 0)
1352 return r;
1353
1354 bus_client_log(message, "service resolution");
1355
1356 r = dns_query_new(m, &q, question_utf8, question_idna, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH);
1357 if (r < 0)
1358 return r;
1359
1360 q->bus_request = sd_bus_message_ref(message);
1361 q->request_family = family;
1362 q->complete = bus_method_resolve_service_complete;
1363
1364 r = dns_query_bus_track(q, message);
1365 if (r < 0)
1366 return r;
1367
1368 r = dns_query_go(q);
1369 if (r < 0)
1370 return r;
1371
1372 TAKE_PTR(q);
1373 return 1;
1374 }
1375
1376 int bus_dns_server_append(
1377 sd_bus_message *reply,
1378 DnsServer *s,
1379 bool with_ifindex, /* include "ifindex" field */
1380 bool extended) { /* also include port number and server name */
1381 int r;
1382
1383 assert(reply);
1384
1385 if (!s) {
1386 if (with_ifindex) {
1387 if (extended)
1388 return sd_bus_message_append(reply, "(iiayqs)", 0, AF_UNSPEC, 0, 0, NULL);
1389 else
1390 return sd_bus_message_append(reply, "(iiay)", 0, AF_UNSPEC, 0);
1391 } else {
1392 if (extended)
1393 return sd_bus_message_append(reply, "(iayqs)", AF_UNSPEC, 0, 0, NULL);
1394 else
1395 return sd_bus_message_append(reply, "(iay)", AF_UNSPEC, 0);
1396 }
1397 }
1398
1399 r = sd_bus_message_open_container(
1400 reply,
1401 'r',
1402 with_ifindex ? (extended ? "iiayqs" : "iiay") :
1403 (extended ? "iayqs" : "iay"));
1404 if (r < 0)
1405 return r;
1406
1407 if (with_ifindex) {
1408 r = sd_bus_message_append(reply, "i", dns_server_ifindex(s));
1409 if (r < 0)
1410 return r;
1411 }
1412
1413 r = sd_bus_message_append(reply, "i", s->family);
1414 if (r < 0)
1415 return r;
1416
1417 r = sd_bus_message_append_array(reply, 'y', &s->address, FAMILY_ADDRESS_SIZE(s->family));
1418 if (r < 0)
1419 return r;
1420
1421 if (extended) {
1422 r = sd_bus_message_append(reply, "q", s->port);
1423 if (r < 0)
1424 return r;
1425
1426 r = sd_bus_message_append(reply, "s", s->server_name);
1427 if (r < 0)
1428 return r;
1429 }
1430
1431 return sd_bus_message_close_container(reply);
1432 }
1433
1434 static int bus_property_get_dns_servers_internal(
1435 sd_bus *bus,
1436 const char *path,
1437 const char *interface,
1438 const char *property,
1439 sd_bus_message *reply,
1440 void *userdata,
1441 sd_bus_error *error,
1442 bool extended) {
1443
1444 Manager *m = ASSERT_PTR(userdata);
1445 Link *l;
1446 int r;
1447
1448 assert(reply);
1449
1450 r = sd_bus_message_open_container(reply, 'a', extended ? "(iiayqs)" : "(iiay)");
1451 if (r < 0)
1452 return r;
1453
1454 LIST_FOREACH(servers, s, m->dns_servers) {
1455 r = bus_dns_server_append(reply, s, true, extended);
1456 if (r < 0)
1457 return r;
1458 }
1459
1460 HASHMAP_FOREACH(l, m->links)
1461 LIST_FOREACH(servers, s, l->dns_servers) {
1462 r = bus_dns_server_append(reply, s, true, extended);
1463 if (r < 0)
1464 return r;
1465 }
1466
1467 return sd_bus_message_close_container(reply);
1468 }
1469
1470 static int bus_property_get_dns_servers(
1471 sd_bus *bus,
1472 const char *path,
1473 const char *interface,
1474 const char *property,
1475 sd_bus_message *reply,
1476 void *userdata,
1477 sd_bus_error *error) {
1478 return bus_property_get_dns_servers_internal(bus, path, interface, property, reply, userdata, error, false);
1479 }
1480
1481 static int bus_property_get_dns_servers_ex(
1482 sd_bus *bus,
1483 const char *path,
1484 const char *interface,
1485 const char *property,
1486 sd_bus_message *reply,
1487 void *userdata,
1488 sd_bus_error *error) {
1489 return bus_property_get_dns_servers_internal(bus, path, interface, property, reply, userdata, error, true);
1490 }
1491
1492 static int bus_property_get_fallback_dns_servers_internal(
1493 sd_bus *bus,
1494 const char *path,
1495 const char *interface,
1496 const char *property,
1497 sd_bus_message *reply,
1498 void *userdata,
1499 sd_bus_error *error,
1500 bool extended) {
1501
1502 DnsServer **f = ASSERT_PTR(userdata);
1503 int r;
1504
1505 assert(reply);
1506
1507 r = sd_bus_message_open_container(reply, 'a', extended ? "(iiayqs)" : "(iiay)");
1508 if (r < 0)
1509 return r;
1510
1511 LIST_FOREACH(servers, s, *f) {
1512 r = bus_dns_server_append(reply, s, true, extended);
1513 if (r < 0)
1514 return r;
1515 }
1516
1517 return sd_bus_message_close_container(reply);
1518 }
1519
1520 static int bus_property_get_fallback_dns_servers(
1521 sd_bus *bus,
1522 const char *path,
1523 const char *interface,
1524 const char *property,
1525 sd_bus_message *reply,
1526 void *userdata,
1527 sd_bus_error *error) {
1528 return bus_property_get_fallback_dns_servers_internal(bus, path, interface, property, reply, userdata, error, false);
1529 }
1530
1531 static int bus_property_get_fallback_dns_servers_ex(
1532 sd_bus *bus,
1533 const char *path,
1534 const char *interface,
1535 const char *property,
1536 sd_bus_message *reply,
1537 void *userdata,
1538 sd_bus_error *error) {
1539 return bus_property_get_fallback_dns_servers_internal(bus, path, interface, property, reply, userdata, error, true);
1540 }
1541
1542 static int bus_property_get_current_dns_server_internal(
1543 sd_bus *bus,
1544 const char *path,
1545 const char *interface,
1546 const char *property,
1547 sd_bus_message *reply,
1548 void *userdata,
1549 sd_bus_error *error,
1550 bool extended) {
1551
1552 DnsServer *s;
1553
1554 assert(reply);
1555 assert(userdata);
1556
1557 s = *(DnsServer **) userdata;
1558
1559 return bus_dns_server_append(reply, s, true, extended);
1560 }
1561
1562 static int bus_property_get_current_dns_server(
1563 sd_bus *bus,
1564 const char *path,
1565 const char *interface,
1566 const char *property,
1567 sd_bus_message *reply,
1568 void *userdata,
1569 sd_bus_error *error) {
1570 return bus_property_get_current_dns_server_internal(bus, path, interface, property, reply, userdata, error, false);
1571 }
1572
1573 static int bus_property_get_current_dns_server_ex(
1574 sd_bus *bus,
1575 const char *path,
1576 const char *interface,
1577 const char *property,
1578 sd_bus_message *reply,
1579 void *userdata,
1580 sd_bus_error *error) {
1581 return bus_property_get_current_dns_server_internal(bus, path, interface, property, reply, userdata, error, true);
1582 }
1583
1584 static int bus_property_get_domains(
1585 sd_bus *bus,
1586 const char *path,
1587 const char *interface,
1588 const char *property,
1589 sd_bus_message *reply,
1590 void *userdata,
1591 sd_bus_error *error) {
1592
1593 Manager *m = ASSERT_PTR(userdata);
1594 Link *l;
1595 int r;
1596
1597 assert(reply);
1598
1599 r = sd_bus_message_open_container(reply, 'a', "(isb)");
1600 if (r < 0)
1601 return r;
1602
1603 LIST_FOREACH(domains, d, m->search_domains) {
1604 r = sd_bus_message_append(reply, "(isb)", 0, d->name, d->route_only);
1605 if (r < 0)
1606 return r;
1607 }
1608
1609 HASHMAP_FOREACH(l, m->links) {
1610 LIST_FOREACH(domains, d, l->search_domains) {
1611 r = sd_bus_message_append(reply, "(isb)", l->ifindex, d->name, d->route_only);
1612 if (r < 0)
1613 return r;
1614 }
1615 }
1616
1617 return sd_bus_message_close_container(reply);
1618 }
1619
1620 static int bus_property_get_transaction_statistics(
1621 sd_bus *bus,
1622 const char *path,
1623 const char *interface,
1624 const char *property,
1625 sd_bus_message *reply,
1626 void *userdata,
1627 sd_bus_error *error) {
1628
1629 Manager *m = ASSERT_PTR(userdata);
1630
1631 assert(reply);
1632
1633 return sd_bus_message_append(reply, "(tt)",
1634 (uint64_t) hashmap_size(m->dns_transactions),
1635 (uint64_t) m->n_transactions_total);
1636 }
1637
1638 static int bus_property_get_cache_statistics(
1639 sd_bus *bus,
1640 const char *path,
1641 const char *interface,
1642 const char *property,
1643 sd_bus_message *reply,
1644 void *userdata,
1645 sd_bus_error *error) {
1646
1647 uint64_t size = 0, hit = 0, miss = 0;
1648 Manager *m = ASSERT_PTR(userdata);
1649
1650 assert(reply);
1651
1652 LIST_FOREACH(scopes, s, m->dns_scopes) {
1653 size += dns_cache_size(&s->cache);
1654 hit += s->cache.n_hit;
1655 miss += s->cache.n_miss;
1656 }
1657
1658 return sd_bus_message_append(reply, "(ttt)", size, hit, miss);
1659 }
1660
1661 static int bus_property_get_dnssec_statistics(
1662 sd_bus *bus,
1663 const char *path,
1664 const char *interface,
1665 const char *property,
1666 sd_bus_message *reply,
1667 void *userdata,
1668 sd_bus_error *error) {
1669
1670 Manager *m = ASSERT_PTR(userdata);
1671
1672 assert(reply);
1673
1674 return sd_bus_message_append(reply, "(tttt)",
1675 (uint64_t) m->n_dnssec_verdict[DNSSEC_SECURE],
1676 (uint64_t) m->n_dnssec_verdict[DNSSEC_INSECURE],
1677 (uint64_t) m->n_dnssec_verdict[DNSSEC_BOGUS],
1678 (uint64_t) m->n_dnssec_verdict[DNSSEC_INDETERMINATE]);
1679 }
1680
1681 static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode, dns_stub_listener_mode, DnsStubListenerMode);
1682 static BUS_DEFINE_PROPERTY_GET(bus_property_get_dnssec_supported, "b", Manager, manager_dnssec_supported);
1683 static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dnssec_mode, "s", Manager, manager_get_dnssec_mode, dnssec_mode_to_string);
1684 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);
1685
1686 static int bus_property_get_resolv_conf_mode(
1687 sd_bus *bus,
1688 const char *path,
1689 const char *interface,
1690 const char *property,
1691 sd_bus_message *reply,
1692 void *userdata,
1693 sd_bus_error *error) {
1694
1695 int r;
1696
1697 assert(reply);
1698
1699 r = resolv_conf_mode();
1700 if (r < 0) {
1701 log_warning_errno(r, "Failed to test /etc/resolv.conf mode, ignoring: %m");
1702 return sd_bus_message_append(reply, "s", NULL);
1703 }
1704
1705 return sd_bus_message_append(reply, "s", resolv_conf_mode_to_string(r));
1706 }
1707
1708 static int bus_method_reset_statistics(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1709 Manager *m = ASSERT_PTR(userdata);
1710
1711 assert(message);
1712
1713 bus_client_log(message, "statistics reset");
1714
1715 dns_manager_reset_statistics(m);
1716
1717 return sd_bus_reply_method_return(message, NULL);
1718 }
1719
1720 static int get_any_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) {
1721 Link *l;
1722
1723 assert(m);
1724 assert(ret);
1725
1726 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
1727 if (!l)
1728 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %i not known", ifindex);
1729
1730 *ret = l;
1731 return 0;
1732 }
1733
1734 static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
1735 int ifindex, r;
1736 Link *l;
1737
1738 assert(m);
1739 assert(message);
1740 assert(handler);
1741
1742 r = bus_message_read_ifindex(message, error, &ifindex);
1743 if (r < 0)
1744 return r;
1745
1746 r = get_any_link(m, ifindex, &l, error);
1747 if (r < 0)
1748 return r;
1749
1750 return handler(message, l, error);
1751 }
1752
1753 static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1754 return call_link_method(userdata, message, bus_link_method_set_dns_servers, error);
1755 }
1756
1757 static int bus_method_set_link_dns_servers_ex(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1758 return call_link_method(userdata, message, bus_link_method_set_dns_servers_ex, error);
1759 }
1760
1761 static int bus_method_set_link_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1762 return call_link_method(userdata, message, bus_link_method_set_domains, error);
1763 }
1764
1765 static int bus_method_set_link_default_route(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1766 return call_link_method(userdata, message, bus_link_method_set_default_route, error);
1767 }
1768
1769 static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1770 return call_link_method(userdata, message, bus_link_method_set_llmnr, error);
1771 }
1772
1773 static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1774 return call_link_method(userdata, message, bus_link_method_set_mdns, error);
1775 }
1776
1777 static int bus_method_set_link_dns_over_tls(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1778 return call_link_method(userdata, message, bus_link_method_set_dns_over_tls, error);
1779 }
1780
1781 static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1782 return call_link_method(userdata, message, bus_link_method_set_dnssec, error);
1783 }
1784
1785 static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1786 return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error);
1787 }
1788
1789 static int bus_method_revert_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1790 return call_link_method(userdata, message, bus_link_method_revert, error);
1791 }
1792
1793 static int bus_method_get_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1794 _cleanup_free_ char *p = NULL;
1795 Manager *m = ASSERT_PTR(userdata);
1796 int r, ifindex;
1797 Link *l;
1798
1799 assert(message);
1800
1801 r = bus_message_read_ifindex(message, error, &ifindex);
1802 if (r < 0)
1803 return r;
1804
1805 r = get_any_link(m, ifindex, &l, error);
1806 if (r < 0)
1807 return r;
1808
1809 p = link_bus_path(l);
1810 if (!p)
1811 return -ENOMEM;
1812
1813 return sd_bus_reply_method_return(message, "o", p);
1814 }
1815
1816 static int bus_method_flush_caches(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1817 Manager *m = ASSERT_PTR(userdata);
1818
1819 assert(message);
1820
1821 bus_client_log(message, "cache flush");
1822
1823 manager_flush_caches(m, LOG_INFO);
1824
1825 return sd_bus_reply_method_return(message, NULL);
1826 }
1827
1828 static int bus_method_reset_server_features(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1829 Manager *m = ASSERT_PTR(userdata);
1830
1831 assert(message);
1832
1833 bus_client_log(message, "server feature reset");
1834
1835 manager_reset_server_features(m);
1836
1837 return sd_bus_reply_method_return(message, NULL);
1838 }
1839
1840 static int dnssd_service_on_bus_track(sd_bus_track *t, void *userdata) {
1841 DnssdService *s = ASSERT_PTR(userdata);
1842
1843 assert(t);
1844
1845 log_debug("Client of active request vanished, destroying DNS-SD service.");
1846 dnssd_service_free(s);
1847
1848 return 0;
1849 }
1850
1851 static int bus_method_register_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1852 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1853 _cleanup_(dnssd_service_freep) DnssdService *service = NULL;
1854 _cleanup_(sd_bus_track_unrefp) sd_bus_track *bus_track = NULL;
1855 const char *name, *name_template, *type;
1856 _cleanup_free_ char *path = NULL;
1857 DnssdService *s = NULL;
1858 Manager *m = ASSERT_PTR(userdata);
1859 uid_t euid;
1860 int r;
1861
1862 assert(message);
1863
1864 if (m->mdns_support != RESOLVE_SUPPORT_YES)
1865 return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Support for MulticastDNS is disabled");
1866
1867 service = new0(DnssdService, 1);
1868 if (!service)
1869 return log_oom();
1870
1871 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1872 if (r < 0)
1873 return r;
1874
1875 r = sd_bus_creds_get_euid(creds, &euid);
1876 if (r < 0)
1877 return r;
1878 service->originator = euid;
1879
1880 r = sd_bus_message_read(message, "sssqqq", &name, &name_template, &type,
1881 &service->port, &service->priority,
1882 &service->weight);
1883 if (r < 0)
1884 return r;
1885
1886 s = hashmap_get(m->dnssd_services, name);
1887 if (s)
1888 return sd_bus_error_setf(error, BUS_ERROR_DNSSD_SERVICE_EXISTS, "DNS-SD service '%s' exists already", name);
1889
1890 if (!dnssd_srv_type_is_valid(type))
1891 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DNS-SD service type '%s' is invalid", type);
1892
1893 service->name = strdup(name);
1894 if (!service->name)
1895 return log_oom();
1896
1897 service->name_template = strdup(name_template);
1898 if (!service->name_template)
1899 return log_oom();
1900
1901 service->type = strdup(type);
1902 if (!service->type)
1903 return log_oom();
1904
1905 r = dnssd_render_instance_name(m, service, NULL);
1906 if (r < 0)
1907 return r;
1908
1909 r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "a{say}");
1910 if (r < 0)
1911 return r;
1912
1913 while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "{say}")) > 0) {
1914 _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL;
1915 DnsTxtItem *last = NULL;
1916
1917 txt_data = new0(DnssdTxtData, 1);
1918 if (!txt_data)
1919 return log_oom();
1920
1921 while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_DICT_ENTRY, "say")) > 0) {
1922 const char *key;
1923 const void *value;
1924 size_t size;
1925 DnsTxtItem *i;
1926
1927 r = sd_bus_message_read(message, "s", &key);
1928 if (r < 0)
1929 return r;
1930
1931 if (isempty(key))
1932 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Keys in DNS-SD TXT RRs can't be empty");
1933
1934 if (!ascii_is_valid(key))
1935 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "TXT key '%s' contains non-ASCII symbols", key);
1936
1937 r = sd_bus_message_read_array(message, 'y', &value, &size);
1938 if (r < 0)
1939 return r;
1940
1941 r = dnssd_txt_item_new_from_data(key, value, size, &i);
1942 if (r < 0)
1943 return r;
1944
1945 LIST_INSERT_AFTER(items, txt_data->txts, last, i);
1946 last = i;
1947
1948 r = sd_bus_message_exit_container(message);
1949 if (r < 0)
1950 return r;
1951
1952 }
1953 if (r < 0)
1954 return r;
1955
1956 r = sd_bus_message_exit_container(message);
1957 if (r < 0)
1958 return r;
1959
1960 if (txt_data->txts) {
1961 LIST_PREPEND(items, service->txt_data_items, txt_data);
1962 txt_data = NULL;
1963 }
1964 }
1965 if (r < 0)
1966 return r;
1967
1968 r = sd_bus_message_exit_container(message);
1969 if (r < 0)
1970 return r;
1971
1972 if (!service->txt_data_items) {
1973 _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL;
1974
1975 txt_data = new0(DnssdTxtData, 1);
1976 if (!txt_data)
1977 return log_oom();
1978
1979 r = dns_txt_item_new_empty(&txt_data->txts);
1980 if (r < 0)
1981 return r;
1982
1983 LIST_PREPEND(items, service->txt_data_items, txt_data);
1984 txt_data = NULL;
1985 }
1986
1987 r = sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service->name, &path);
1988 if (r < 0)
1989 return r;
1990
1991 r = bus_verify_polkit_async(message, CAP_SYS_ADMIN,
1992 "org.freedesktop.resolve1.register-service",
1993 NULL, false, UID_INVALID,
1994 &m->polkit_registry, error);
1995 if (r < 0)
1996 return r;
1997 if (r == 0)
1998 return 1; /* Polkit will call us back */
1999
2000 r = hashmap_ensure_put(&m->dnssd_services, &string_hash_ops, service->name, service);
2001 if (r < 0)
2002 return r;
2003
2004 r = sd_bus_track_new(sd_bus_message_get_bus(message), &bus_track, dnssd_service_on_bus_track, service);
2005 if (r < 0)
2006 return r;
2007
2008 r = sd_bus_track_add_sender(bus_track, message);
2009 if (r < 0)
2010 return r;
2011
2012 service->manager = m;
2013
2014 service = NULL;
2015
2016 manager_refresh_rrs(m);
2017
2018 return sd_bus_reply_method_return(message, "o", path);
2019 }
2020
2021 static int call_dnssd_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
2022 _cleanup_free_ char *name = NULL;
2023 DnssdService *s = NULL;
2024 const char *path;
2025 int r;
2026
2027 assert(m);
2028 assert(message);
2029 assert(handler);
2030
2031 r = sd_bus_message_read(message, "o", &path);
2032 if (r < 0)
2033 return r;
2034
2035 r = sd_bus_path_decode(path, "/org/freedesktop/resolve1/dnssd", &name);
2036 if (r == 0)
2037 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DNSSD_SERVICE, "DNS-SD service with object path '%s' does not exist", path);
2038 if (r < 0)
2039 return r;
2040
2041 s = hashmap_get(m->dnssd_services, name);
2042 if (!s)
2043 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DNSSD_SERVICE, "DNS-SD service '%s' not known", name);
2044
2045 return handler(message, s, error);
2046 }
2047
2048 static int bus_method_unregister_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2049 Manager *m = ASSERT_PTR(userdata);
2050
2051 assert(message);
2052
2053 return call_dnssd_method(m, message, bus_dnssd_method_unregister, error);
2054 }
2055
2056 static const sd_bus_vtable resolve_vtable[] = {
2057 SD_BUS_VTABLE_START(0),
2058 SD_BUS_PROPERTY("LLMNRHostname", "s", NULL, offsetof(Manager, llmnr_hostname), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2059 SD_BUS_PROPERTY("LLMNR", "s", bus_property_get_resolve_support, offsetof(Manager, llmnr_support), 0),
2060 SD_BUS_PROPERTY("MulticastDNS", "s", bus_property_get_resolve_support, offsetof(Manager, mdns_support), 0),
2061 SD_BUS_PROPERTY("DNSOverTLS", "s", bus_property_get_dns_over_tls_mode, 0, 0),
2062 SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2063 SD_BUS_PROPERTY("DNSEx", "a(iiayqs)", bus_property_get_dns_servers_ex, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2064 SD_BUS_PROPERTY("FallbackDNS", "a(iiay)", bus_property_get_fallback_dns_servers, offsetof(Manager, fallback_dns_servers), SD_BUS_VTABLE_PROPERTY_CONST),
2065 SD_BUS_PROPERTY("FallbackDNSEx", "a(iiayqs)", bus_property_get_fallback_dns_servers_ex, offsetof(Manager, fallback_dns_servers), SD_BUS_VTABLE_PROPERTY_CONST),
2066 SD_BUS_PROPERTY("CurrentDNSServer", "(iiay)", bus_property_get_current_dns_server, offsetof(Manager, current_dns_server), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2067 SD_BUS_PROPERTY("CurrentDNSServerEx", "(iiayqs)", bus_property_get_current_dns_server_ex, offsetof(Manager, current_dns_server), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2068 SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains, 0, 0),
2069 SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics, 0, 0),
2070 SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics, 0, 0),
2071 SD_BUS_PROPERTY("DNSSEC", "s", bus_property_get_dnssec_mode, 0, 0),
2072 SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics, 0, 0),
2073 SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported, 0, 0),
2074 SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_string_set, offsetof(Manager, trust_anchor.negative_by_name), 0),
2075 SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode, offsetof(Manager, dns_stub_listener_mode), 0),
2076 SD_BUS_PROPERTY("ResolvConfMode", "s", bus_property_get_resolv_conf_mode, 0, 0),
2077
2078 SD_BUS_METHOD_WITH_ARGS("ResolveHostname",
2079 SD_BUS_ARGS("i", ifindex, "s", name, "i", family, "t", flags),
2080 SD_BUS_RESULT("a(iiay)", addresses, "s", canonical, "t", flags),
2081 bus_method_resolve_hostname,
2082 SD_BUS_VTABLE_UNPRIVILEGED),
2083 SD_BUS_METHOD_WITH_ARGS("ResolveAddress",
2084 SD_BUS_ARGS("i", ifindex, "i", family, "ay", address, "t", flags),
2085 SD_BUS_RESULT("a(is)", names, "t", flags),
2086 bus_method_resolve_address,
2087 SD_BUS_VTABLE_UNPRIVILEGED),
2088 SD_BUS_METHOD_WITH_ARGS("ResolveRecord",
2089 SD_BUS_ARGS("i", ifindex, "s", name, "q", class, "q", type, "t", flags),
2090 SD_BUS_RESULT("a(iqqay)", records, "t", flags),
2091 bus_method_resolve_record,
2092 SD_BUS_VTABLE_UNPRIVILEGED),
2093 SD_BUS_METHOD_WITH_ARGS("ResolveService",
2094 SD_BUS_ARGS("i", ifindex,
2095 "s", name,
2096 "s", type,
2097 "s", domain,
2098 "i", family,
2099 "t", flags),
2100 SD_BUS_RESULT("a(qqqsa(iiay)s)", srv_data,
2101 "aay", txt_data,
2102 "s", canonical_name,
2103 "s", canonical_type,
2104 "s", canonical_domain,
2105 "t", flags),
2106 bus_method_resolve_service,
2107 SD_BUS_VTABLE_UNPRIVILEGED),
2108 SD_BUS_METHOD_WITH_ARGS("GetLink",
2109 SD_BUS_ARGS("i", ifindex),
2110 SD_BUS_RESULT("o", path),
2111 bus_method_get_link,
2112 SD_BUS_VTABLE_UNPRIVILEGED),
2113 SD_BUS_METHOD_WITH_ARGS("SetLinkDNS",
2114 SD_BUS_ARGS("i", ifindex, "a(iay)", addresses),
2115 SD_BUS_NO_RESULT,
2116 bus_method_set_link_dns_servers,
2117 SD_BUS_VTABLE_UNPRIVILEGED),
2118 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSEx",
2119 SD_BUS_ARGS("i", ifindex, "a(iayqs)", addresses),
2120 SD_BUS_NO_RESULT,
2121 bus_method_set_link_dns_servers_ex,
2122 SD_BUS_VTABLE_UNPRIVILEGED),
2123 SD_BUS_METHOD_WITH_ARGS("SetLinkDomains",
2124 SD_BUS_ARGS("i", ifindex, "a(sb)", domains),
2125 SD_BUS_NO_RESULT,
2126 bus_method_set_link_domains,
2127 SD_BUS_VTABLE_UNPRIVILEGED),
2128 SD_BUS_METHOD_WITH_ARGS("SetLinkDefaultRoute",
2129 SD_BUS_ARGS("i", ifindex, "b", enable),
2130 SD_BUS_NO_RESULT,
2131 bus_method_set_link_default_route,
2132 SD_BUS_VTABLE_UNPRIVILEGED),
2133 SD_BUS_METHOD_WITH_ARGS("SetLinkLLMNR",
2134 SD_BUS_ARGS("i", ifindex, "s", mode),
2135 SD_BUS_NO_RESULT,
2136 bus_method_set_link_llmnr,
2137 SD_BUS_VTABLE_UNPRIVILEGED),
2138 SD_BUS_METHOD_WITH_ARGS("SetLinkMulticastDNS",
2139 SD_BUS_ARGS("i", ifindex, "s", mode),
2140 SD_BUS_NO_RESULT,
2141 bus_method_set_link_mdns,
2142 SD_BUS_VTABLE_UNPRIVILEGED),
2143 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSOverTLS",
2144 SD_BUS_ARGS("i", ifindex, "s", mode),
2145 SD_BUS_NO_RESULT,
2146 bus_method_set_link_dns_over_tls,
2147 SD_BUS_VTABLE_UNPRIVILEGED),
2148 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSEC",
2149 SD_BUS_ARGS("i", ifindex, "s", mode),
2150 SD_BUS_NO_RESULT,
2151 bus_method_set_link_dnssec,
2152 SD_BUS_VTABLE_UNPRIVILEGED),
2153 SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSECNegativeTrustAnchors",
2154 SD_BUS_ARGS("i", ifindex, "as", names),
2155 SD_BUS_NO_RESULT,
2156 bus_method_set_link_dnssec_negative_trust_anchors,
2157 SD_BUS_VTABLE_UNPRIVILEGED),
2158 SD_BUS_METHOD_WITH_ARGS("RevertLink",
2159 SD_BUS_ARGS("i", ifindex),
2160 SD_BUS_NO_RESULT,
2161 bus_method_revert_link,
2162 SD_BUS_VTABLE_UNPRIVILEGED),
2163 SD_BUS_METHOD_WITH_ARGS("RegisterService",
2164 SD_BUS_ARGS("s", name,
2165 "s", name_template,
2166 "s", type,
2167 "q", service_port,
2168 "q", service_priority,
2169 "q", service_weight,
2170 "aa{say}", txt_datas),
2171 SD_BUS_RESULT("o", service_path),
2172 bus_method_register_service,
2173 SD_BUS_VTABLE_UNPRIVILEGED),
2174 SD_BUS_METHOD_WITH_ARGS("UnregisterService",
2175 SD_BUS_ARGS("o", service_path),
2176 SD_BUS_NO_RESULT,
2177 bus_method_unregister_service,
2178 SD_BUS_VTABLE_UNPRIVILEGED),
2179 SD_BUS_METHOD_WITH_ARGS("ResetStatistics",
2180 SD_BUS_NO_ARGS,
2181 SD_BUS_NO_RESULT,
2182 bus_method_reset_statistics,
2183 SD_BUS_VTABLE_UNPRIVILEGED),
2184 SD_BUS_METHOD_WITH_ARGS("FlushCaches",
2185 SD_BUS_NO_ARGS,
2186 SD_BUS_NO_RESULT,
2187 bus_method_flush_caches,
2188 SD_BUS_VTABLE_UNPRIVILEGED),
2189 SD_BUS_METHOD_WITH_ARGS("ResetServerFeatures",
2190 SD_BUS_NO_ARGS,
2191 SD_BUS_NO_RESULT,
2192 bus_method_reset_server_features,
2193 SD_BUS_VTABLE_UNPRIVILEGED),
2194
2195 SD_BUS_VTABLE_END,
2196 };
2197
2198 const BusObjectImplementation manager_object = {
2199 "/org/freedesktop/resolve1",
2200 "org.freedesktop.resolve1.Manager",
2201 .vtables = BUS_VTABLES(resolve_vtable),
2202 .children = BUS_IMPLEMENTATIONS(&link_object,
2203 &dnssd_object),
2204 };
2205
2206 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
2207 Manager *m = ASSERT_PTR(userdata);
2208 int b, r;
2209
2210 assert(message);
2211
2212 r = sd_bus_message_read(message, "b", &b);
2213 if (r < 0) {
2214 bus_log_parse_error(r);
2215 return 0;
2216 }
2217
2218 if (b)
2219 return 0;
2220
2221 log_debug("Coming back from suspend, verifying all RRs...");
2222
2223 manager_verify_all(m);
2224 return 0;
2225 }
2226
2227 int manager_connect_bus(Manager *m) {
2228 int r;
2229
2230 assert(m);
2231
2232 if (m->bus)
2233 return 0;
2234
2235 r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-resolve");
2236 if (r < 0)
2237 return log_error_errno(r, "Failed to connect to system bus: %m");
2238
2239 r = bus_add_implementation(m->bus, &manager_object, m);
2240 if (r < 0)
2241 return r;
2242
2243 r = bus_log_control_api_register(m->bus);
2244 if (r < 0)
2245 return r;
2246
2247 r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.resolve1", 0, NULL, NULL);
2248 if (r < 0)
2249 return log_error_errno(r, "Failed to request name: %m");
2250
2251 r = sd_bus_attach_event(m->bus, m->event, 0);
2252 if (r < 0)
2253 return log_error_errno(r, "Failed to attach bus to event loop: %m");
2254
2255 r = bus_match_signal_async(
2256 m->bus,
2257 NULL,
2258 bus_login_mgr,
2259 "PrepareForSleep",
2260 match_prepare_for_sleep,
2261 NULL,
2262 m);
2263 if (r < 0)
2264 log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m");
2265
2266 return 0;
2267 }
2268
2269 int _manager_send_changed(Manager *manager, const char *property, ...) {
2270 assert(manager);
2271
2272 if (sd_bus_is_ready(manager->bus) <= 0)
2273 return 0;
2274
2275 char **l = strv_from_stdarg_alloca(property);
2276
2277 int r = sd_bus_emit_properties_changed_strv(
2278 manager->bus,
2279 "/org/freedesktop/resolve1",
2280 "org.freedesktop.resolve1.Manager",
2281 l);
2282 if (r < 0)
2283 log_notice_errno(r, "Failed to emit notification about changed property %s: %m", property);
2284 return r;
2285 }