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