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