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