]>
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 | |
ce736ace | 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; | |
248 | int r, ff; | |
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 | ||
253 | r = in_addr_from_string_auto(hostname, &ff, &parsed); | |
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."); | |
259 | ||
260 | r = sd_bus_message_new_method_return(m, &reply); | |
261 | if (r < 0) | |
262 | return r; | |
263 | ||
264 | r = sd_bus_message_open_container(reply, 'a', "(iiay)"); | |
265 | if (r < 0) | |
266 | return r; | |
267 | ||
268 | r = sd_bus_message_open_container(reply, 'r', "iiay"); | |
269 | if (r < 0) | |
270 | return r; | |
271 | ||
272 | r = sd_bus_message_append(reply, "ii", ifindex, ff); | |
273 | if (r < 0) | |
274 | return r; | |
275 | ||
276 | r = sd_bus_message_append_array(reply, 'y', &parsed, FAMILY_ADDRESS_SIZE(ff)); | |
277 | if (r < 0) | |
278 | return r; | |
279 | ||
280 | r = sd_bus_message_close_container(reply); | |
281 | if (r < 0) | |
282 | return r; | |
283 | ||
284 | r = sd_bus_message_close_container(reply); | |
285 | if (r < 0) | |
286 | return r; | |
287 | ||
288 | /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS | |
289 | * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner | |
290 | * omissions are always done the same way). */ | |
291 | r = in_addr_to_string(ff, &parsed, &canonical); | |
292 | if (r < 0) | |
293 | return r; | |
294 | ||
295 | r = sd_bus_message_append(reply, "st", canonical, | |
296 | SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags), ff, true)); | |
297 | if (r < 0) | |
298 | return r; | |
299 | ||
300 | return sd_bus_send(sd_bus_message_get_bus(m), reply, NULL); | |
301 | } | |
302 | ||
19070062 | 303 | static 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); | |
45ec7efb LP |
343 | if (r < 0) |
344 | return r; | |
74b2466e | 345 | |
23b298bc | 346 | r = dns_query_new(m, &q, question_utf8, question_idna, 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 | |
365 | fail: | |
366 | dns_query_free(q); | |
367 | return r; | |
74b2466e LP |
368 | } |
369 | ||
370 | static 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 | |
c77d2612 LP |
413 | r = dns_name_normalize(rr->ptr.name, &normalized); |
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 | ||
421 | added ++; | |
ad867662 | 422 | } |
74b2466e | 423 | |
45ec7efb | 424 | if (added <= 0) { |
ad867662 | 425 | _cleanup_free_ char *ip = NULL; |
74b2466e | 426 | |
ad867662 | 427 | in_addr_to_string(q->request_family, &q->request_address, &ip); |
45ec7efb | 428 | r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address '%s' does not have any RR of requested type", strna(ip)); |
ad867662 | 429 | goto finish; |
74b2466e LP |
430 | } |
431 | ||
ad867662 LP |
432 | r = sd_bus_message_close_container(reply); |
433 | if (r < 0) | |
434 | goto finish; | |
74b2466e | 435 | |
931851e8 | 436 | r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated)); |
51323288 LP |
437 | if (r < 0) |
438 | goto finish; | |
439 | ||
ad867662 | 440 | r = sd_bus_send(q->manager->bus, reply, NULL); |
74b2466e LP |
441 | |
442 | finish: | |
2d4c5cbc | 443 | if (r < 0) { |
da927ba9 | 444 | log_error_errno(r, "Failed to send address reply: %m"); |
45ec7efb | 445 | sd_bus_reply_method_errno(q->request, r, NULL); |
2d4c5cbc | 446 | } |
74b2466e LP |
447 | |
448 | dns_query_free(q); | |
449 | } | |
450 | ||
19070062 | 451 | static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
faa133f3 | 452 | _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
74b2466e | 453 | Manager *m = userdata; |
faa133f3 | 454 | int family, ifindex; |
51323288 | 455 | uint64_t flags; |
74b2466e | 456 | const void *d; |
74b2466e LP |
457 | DnsQuery *q; |
458 | size_t sz; | |
459 | int r; | |
460 | ||
74b2466e LP |
461 | assert(message); |
462 | assert(m); | |
463 | ||
45ec7efb LP |
464 | assert_cc(sizeof(int) == sizeof(int32_t)); |
465 | ||
51323288 | 466 | r = sd_bus_message_read(message, "ii", &ifindex, &family); |
74b2466e LP |
467 | if (r < 0) |
468 | return r; | |
469 | ||
470 | if (!IN_SET(family, AF_INET, AF_INET6)) | |
0dd25fb9 | 471 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family); |
74b2466e LP |
472 | |
473 | r = sd_bus_message_read_array(message, 'y', &d, &sz); | |
474 | if (r < 0) | |
475 | return r; | |
476 | ||
0dd25fb9 | 477 | if (sz != FAMILY_ADDRESS_SIZE(family)) |
74b2466e LP |
478 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size"); |
479 | ||
51323288 LP |
480 | r = sd_bus_message_read(message, "t", &flags); |
481 | if (r < 0) | |
482 | return r; | |
483 | ||
45ec7efb | 484 | r = check_ifindex_flags(ifindex, &flags, 0, error); |
faa133f3 LP |
485 | if (r < 0) |
486 | return r; | |
487 | ||
45ec7efb | 488 | r = dns_question_new_reverse(&question, family, d); |
74b2466e LP |
489 | if (r < 0) |
490 | return r; | |
491 | ||
23b298bc | 492 | r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH); |
74b2466e LP |
493 | if (r < 0) |
494 | return r; | |
495 | ||
496 | q->request = sd_bus_message_ref(message); | |
497 | q->request_family = family; | |
498 | memcpy(&q->request_address, d, sz); | |
499 | q->complete = bus_method_resolve_address_complete; | |
500 | ||
966c66e3 | 501 | r = dns_query_bus_track(q, message); |
82bd6ddd | 502 | if (r < 0) |
45ec7efb | 503 | goto fail; |
82bd6ddd | 504 | |
322345fd | 505 | r = dns_query_go(q); |
45ec7efb LP |
506 | if (r < 0) |
507 | goto fail; | |
74b2466e LP |
508 | |
509 | return 1; | |
45ec7efb LP |
510 | |
511 | fail: | |
512 | dns_query_free(q); | |
513 | return r; | |
514 | } | |
515 | ||
516 | static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) { | |
45ec7efb LP |
517 | int r; |
518 | ||
519 | assert(m); | |
520 | assert(rr); | |
521 | ||
522 | r = sd_bus_message_open_container(m, 'r', "iqqay"); | |
523 | if (r < 0) | |
524 | return r; | |
525 | ||
526 | r = sd_bus_message_append(m, "iqq", | |
527 | ifindex, | |
528 | rr->key->class, | |
529 | rr->key->type); | |
530 | if (r < 0) | |
531 | return r; | |
532 | ||
4e2d538f | 533 | r = dns_resource_record_to_wire_format(rr, false); |
45ec7efb LP |
534 | if (r < 0) |
535 | return r; | |
536 | ||
4e2d538f | 537 | r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size); |
45ec7efb LP |
538 | if (r < 0) |
539 | return r; | |
540 | ||
541 | return sd_bus_message_close_container(m); | |
74b2466e LP |
542 | } |
543 | ||
2d4c5cbc | 544 | static void bus_method_resolve_record_complete(DnsQuery *q) { |
4afd3348 | 545 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
23b298bc LP |
546 | DnsResourceRecord *rr; |
547 | DnsQuestion *question; | |
45ec7efb | 548 | unsigned added = 0; |
23b298bc | 549 | int ifindex; |
2d4c5cbc LP |
550 | int r; |
551 | ||
552 | assert(q); | |
553 | ||
ec2c5e43 | 554 | if (q->state != DNS_TRANSACTION_SUCCESS) { |
2d4c5cbc LP |
555 | r = reply_query_state(q); |
556 | goto finish; | |
557 | } | |
558 | ||
45ec7efb LP |
559 | r = dns_query_process_cname(q); |
560 | if (r == -ELOOP) { | |
23b298bc | 561 | 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 |
562 | goto finish; |
563 | } | |
564 | if (r < 0) | |
565 | goto finish; | |
7588460a | 566 | if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */ |
45ec7efb LP |
567 | return; |
568 | ||
2d4c5cbc LP |
569 | r = sd_bus_message_new_method_return(q->request, &reply); |
570 | if (r < 0) | |
571 | goto finish; | |
572 | ||
78c6a153 | 573 | r = sd_bus_message_open_container(reply, 'a', "(iqqay)"); |
2d4c5cbc LP |
574 | if (r < 0) |
575 | goto finish; | |
576 | ||
23b298bc | 577 | question = dns_query_question_for_protocol(q, q->answer_protocol); |
2d4c5cbc | 578 | |
23b298bc LP |
579 | DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) { |
580 | r = dns_question_matches_rr(question, rr, NULL); | |
581 | if (r < 0) | |
582 | goto finish; | |
583 | if (r == 0) | |
584 | continue; | |
2d4c5cbc | 585 | |
23b298bc LP |
586 | r = bus_message_append_rr(reply, rr, ifindex); |
587 | if (r < 0) | |
588 | goto finish; | |
2d4c5cbc | 589 | |
23b298bc | 590 | added ++; |
2d4c5cbc LP |
591 | } |
592 | ||
593 | if (added <= 0) { | |
23b298bc | 594 | 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 |
595 | goto finish; |
596 | } | |
597 | ||
598 | r = sd_bus_message_close_container(reply); | |
599 | if (r < 0) | |
600 | goto finish; | |
601 | ||
931851e8 | 602 | r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated)); |
51323288 LP |
603 | if (r < 0) |
604 | goto finish; | |
605 | ||
2d4c5cbc LP |
606 | r = sd_bus_send(q->manager->bus, reply, NULL); |
607 | ||
608 | finish: | |
609 | if (r < 0) { | |
da927ba9 | 610 | log_error_errno(r, "Failed to send record reply: %m"); |
45ec7efb | 611 | sd_bus_reply_method_errno(q->request, r, NULL); |
2d4c5cbc LP |
612 | } |
613 | ||
614 | dns_query_free(q); | |
615 | } | |
616 | ||
19070062 | 617 | static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d4c5cbc LP |
618 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
619 | _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; | |
2d4c5cbc | 620 | Manager *m = userdata; |
2d4c5cbc LP |
621 | uint16_t class, type; |
622 | const char *name; | |
51323288 LP |
623 | int r, ifindex; |
624 | uint64_t flags; | |
625 | DnsQuery *q; | |
2d4c5cbc | 626 | |
2d4c5cbc LP |
627 | assert(message); |
628 | assert(m); | |
629 | ||
45ec7efb LP |
630 | assert_cc(sizeof(int) == sizeof(int32_t)); |
631 | ||
51323288 | 632 | r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags); |
2d4c5cbc LP |
633 | if (r < 0) |
634 | return r; | |
635 | ||
45ec7efb | 636 | r = dns_name_is_valid(name); |
7b9f7afc | 637 | if (r < 0) |
45ec7efb LP |
638 | return r; |
639 | if (r == 0) | |
7b9f7afc LP |
640 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name); |
641 | ||
c463eb78 | 642 | if (!dns_type_is_valid_query(type)) |
eee026a7 | 643 | 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 | 644 | if (dns_type_is_obsolete(type)) |
eee026a7 | 645 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type); |
c463eb78 | 646 | |
45ec7efb | 647 | r = check_ifindex_flags(ifindex, &flags, 0, error); |
51323288 LP |
648 | if (r < 0) |
649 | return r; | |
650 | ||
2d4c5cbc LP |
651 | question = dns_question_new(1); |
652 | if (!question) | |
653 | return -ENOMEM; | |
654 | ||
655 | key = dns_resource_key_new(class, type, name); | |
656 | if (!key) | |
657 | return -ENOMEM; | |
658 | ||
659 | r = dns_question_add(question, key); | |
660 | if (r < 0) | |
661 | return r; | |
662 | ||
23b298bc | 663 | r = dns_query_new(m, &q, question, question, ifindex, flags|SD_RESOLVED_NO_SEARCH); |
2d4c5cbc LP |
664 | if (r < 0) |
665 | return r; | |
666 | ||
667 | q->request = sd_bus_message_ref(message); | |
2d4c5cbc LP |
668 | q->complete = bus_method_resolve_record_complete; |
669 | ||
966c66e3 | 670 | r = dns_query_bus_track(q, message); |
82bd6ddd | 671 | if (r < 0) |
45ec7efb | 672 | goto fail; |
82bd6ddd | 673 | |
2d4c5cbc | 674 | r = dns_query_go(q); |
45ec7efb LP |
675 | if (r < 0) |
676 | goto fail; | |
677 | ||
678 | return 1; | |
679 | ||
680 | fail: | |
681 | dns_query_free(q); | |
682 | return r; | |
683 | } | |
684 | ||
685 | static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) { | |
686 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL; | |
c77d2612 | 687 | _cleanup_free_ char *normalized = NULL; |
45ec7efb LP |
688 | DnsQuery *aux; |
689 | int r; | |
690 | ||
691 | assert(q); | |
692 | assert(reply); | |
693 | assert(rr); | |
694 | assert(rr->key); | |
695 | ||
696 | if (rr->key->type != DNS_TYPE_SRV) | |
697 | return 0; | |
698 | ||
699 | if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) { | |
700 | /* First, let's see if we could find an appropriate A or AAAA | |
701 | * record for the SRV record */ | |
702 | LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) { | |
703 | DnsResourceRecord *zz; | |
23b298bc | 704 | DnsQuestion *question; |
45ec7efb LP |
705 | |
706 | if (aux->state != DNS_TRANSACTION_SUCCESS) | |
707 | continue; | |
708 | if (aux->auxiliary_result != 0) | |
709 | continue; | |
710 | ||
23b298bc LP |
711 | question = dns_query_question_for_protocol(aux, aux->answer_protocol); |
712 | ||
713 | r = dns_name_equal(dns_question_first_name(question), rr->srv.name); | |
45ec7efb LP |
714 | if (r < 0) |
715 | return r; | |
716 | if (r == 0) | |
717 | continue; | |
718 | ||
719 | DNS_ANSWER_FOREACH(zz, aux->answer) { | |
720 | ||
23b298bc | 721 | r = dns_question_matches_rr(question, zz, NULL); |
45ec7efb LP |
722 | if (r < 0) |
723 | return r; | |
724 | if (r == 0) | |
725 | continue; | |
726 | ||
727 | canonical = dns_resource_record_ref(zz); | |
728 | break; | |
729 | } | |
730 | ||
731 | if (canonical) | |
732 | break; | |
733 | } | |
734 | ||
735 | /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */ | |
736 | if (!canonical) | |
737 | return 0; | |
738 | } | |
739 | ||
740 | r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s"); | |
741 | if (r < 0) | |
742 | return r; | |
743 | ||
c77d2612 LP |
744 | r = dns_name_normalize(rr->srv.name, &normalized); |
745 | if (r < 0) | |
746 | return r; | |
747 | ||
45ec7efb LP |
748 | r = sd_bus_message_append( |
749 | reply, | |
750 | "qqqs", | |
c77d2612 | 751 | rr->srv.priority, rr->srv.weight, rr->srv.port, normalized); |
45ec7efb LP |
752 | if (r < 0) |
753 | return r; | |
754 | ||
755 | r = sd_bus_message_open_container(reply, 'a', "(iiay)"); | |
756 | if (r < 0) | |
757 | return r; | |
758 | ||
759 | if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) { | |
760 | LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) { | |
761 | DnsResourceRecord *zz; | |
23b298bc | 762 | DnsQuestion *question; |
45ec7efb LP |
763 | int ifindex; |
764 | ||
765 | if (aux->state != DNS_TRANSACTION_SUCCESS) | |
766 | continue; | |
767 | if (aux->auxiliary_result != 0) | |
768 | continue; | |
769 | ||
23b298bc LP |
770 | question = dns_query_question_for_protocol(aux, aux->answer_protocol); |
771 | ||
772 | r = dns_name_equal(dns_question_first_name(question), rr->srv.name); | |
45ec7efb LP |
773 | if (r < 0) |
774 | return r; | |
775 | if (r == 0) | |
776 | continue; | |
777 | ||
778 | DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) { | |
779 | ||
23b298bc | 780 | r = dns_question_matches_rr(question, zz, NULL); |
45ec7efb LP |
781 | if (r < 0) |
782 | return r; | |
783 | if (r == 0) | |
784 | continue; | |
785 | ||
786 | r = append_address(reply, zz, ifindex); | |
787 | if (r < 0) | |
788 | return r; | |
789 | } | |
790 | } | |
791 | } | |
792 | ||
793 | r = sd_bus_message_close_container(reply); | |
794 | if (r < 0) | |
795 | return r; | |
796 | ||
c77d2612 LP |
797 | if (canonical) { |
798 | normalized = mfree(normalized); | |
799 | ||
1c02e7ba | 800 | r = dns_name_normalize(dns_resource_key_name(canonical->key), &normalized); |
c77d2612 LP |
801 | if (r < 0) |
802 | return r; | |
803 | } | |
804 | ||
45ec7efb LP |
805 | /* Note that above we appended the hostname as encoded in the |
806 | * SRV, and here the canonical hostname this maps to. */ | |
c77d2612 | 807 | r = sd_bus_message_append(reply, "s", normalized); |
45ec7efb LP |
808 | if (r < 0) |
809 | return r; | |
810 | ||
811 | r = sd_bus_message_close_container(reply); | |
812 | if (r < 0) | |
813 | return r; | |
814 | ||
815 | return 1; | |
816 | } | |
817 | ||
818 | static int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) { | |
819 | DnsTxtItem *i; | |
820 | int r; | |
821 | ||
822 | assert(reply); | |
823 | assert(rr); | |
824 | assert(rr->key); | |
825 | ||
826 | if (rr->key->type != DNS_TYPE_TXT) | |
827 | return 0; | |
828 | ||
829 | LIST_FOREACH(items, i, rr->txt.items) { | |
830 | ||
831 | if (i->length <= 0) | |
832 | continue; | |
833 | ||
834 | r = sd_bus_message_append_array(reply, 'y', i->data, i->length); | |
835 | if (r < 0) | |
836 | return r; | |
837 | } | |
838 | ||
839 | return 1; | |
840 | } | |
841 | ||
842 | static void resolve_service_all_complete(DnsQuery *q) { | |
843 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL; | |
844 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
845 | _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL; | |
23b298bc LP |
846 | DnsQuestion *question; |
847 | DnsResourceRecord *rr; | |
848 | unsigned added = 0; | |
45ec7efb | 849 | DnsQuery *aux; |
45ec7efb LP |
850 | int r; |
851 | ||
852 | assert(q); | |
853 | ||
854 | if (q->block_all_complete > 0) | |
855 | return; | |
856 | ||
857 | if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) { | |
858 | DnsQuery *bad = NULL; | |
859 | bool have_success = false; | |
860 | ||
861 | LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) { | |
862 | ||
863 | switch (aux->state) { | |
864 | ||
865 | case DNS_TRANSACTION_PENDING: | |
866 | /* If an auxiliary query is still pending, let's wait */ | |
867 | return; | |
868 | ||
869 | case DNS_TRANSACTION_SUCCESS: | |
870 | if (aux->auxiliary_result == 0) | |
871 | have_success = true; | |
872 | else | |
873 | bad = aux; | |
874 | break; | |
875 | ||
876 | default: | |
877 | bad = aux; | |
878 | break; | |
879 | } | |
880 | } | |
881 | ||
882 | if (!have_success) { | |
883 | /* We can only return one error, hence pick the last error we encountered */ | |
884 | ||
885 | assert(bad); | |
886 | ||
887 | if (bad->state == DNS_TRANSACTION_SUCCESS) { | |
888 | assert(bad->auxiliary_result != 0); | |
889 | ||
890 | if (bad->auxiliary_result == -ELOOP) { | |
23b298bc | 891 | 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 |
892 | goto finish; |
893 | } | |
894 | ||
895 | r = bad->auxiliary_result; | |
896 | goto finish; | |
897 | } | |
898 | ||
899 | r = reply_query_state(bad); | |
900 | goto finish; | |
901 | } | |
902 | } | |
903 | ||
904 | r = sd_bus_message_new_method_return(q->request, &reply); | |
905 | if (r < 0) | |
906 | goto finish; | |
907 | ||
908 | r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)"); | |
909 | if (r < 0) | |
910 | goto finish; | |
911 | ||
23b298bc LP |
912 | question = dns_query_question_for_protocol(q, q->answer_protocol); |
913 | DNS_ANSWER_FOREACH(rr, q->answer) { | |
914 | r = dns_question_matches_rr(question, rr, NULL); | |
915 | if (r < 0) | |
916 | goto finish; | |
917 | if (r == 0) | |
918 | continue; | |
45ec7efb | 919 | |
23b298bc LP |
920 | r = append_srv(q, reply, rr); |
921 | if (r < 0) | |
922 | goto finish; | |
923 | if (r == 0) /* not an SRV record */ | |
924 | continue; | |
45ec7efb | 925 | |
23b298bc LP |
926 | if (!canonical) |
927 | canonical = dns_resource_record_ref(rr); | |
45ec7efb | 928 | |
23b298bc | 929 | added++; |
45ec7efb LP |
930 | } |
931 | ||
932 | if (added <= 0) { | |
23b298bc | 933 | 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 |
934 | goto finish; |
935 | } | |
936 | ||
937 | r = sd_bus_message_close_container(reply); | |
938 | if (r < 0) | |
939 | goto finish; | |
940 | ||
941 | r = sd_bus_message_open_container(reply, 'a', "ay"); | |
942 | if (r < 0) | |
943 | goto finish; | |
944 | ||
23b298bc LP |
945 | DNS_ANSWER_FOREACH(rr, q->answer) { |
946 | r = dns_question_matches_rr(question, rr, NULL); | |
947 | if (r < 0) | |
948 | goto finish; | |
949 | if (r == 0) | |
950 | continue; | |
45ec7efb | 951 | |
23b298bc LP |
952 | r = append_txt(reply, rr); |
953 | if (r < 0) | |
954 | goto finish; | |
45ec7efb LP |
955 | } |
956 | ||
957 | r = sd_bus_message_close_container(reply); | |
958 | if (r < 0) | |
959 | goto finish; | |
960 | ||
961 | assert(canonical); | |
1c02e7ba | 962 | r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain); |
45ec7efb LP |
963 | if (r < 0) |
964 | goto finish; | |
965 | ||
966 | r = sd_bus_message_append( | |
967 | reply, | |
968 | "ssst", | |
969 | name, type, domain, | |
931851e8 | 970 | SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated)); |
45ec7efb LP |
971 | if (r < 0) |
972 | goto finish; | |
973 | ||
974 | r = sd_bus_send(q->manager->bus, reply, NULL); | |
975 | ||
976 | finish: | |
977 | if (r < 0) { | |
978 | log_error_errno(r, "Failed to send service reply: %m"); | |
979 | sd_bus_reply_method_errno(q->request, r, NULL); | |
980 | } | |
981 | ||
982 | dns_query_free(q); | |
983 | } | |
984 | ||
985 | static void resolve_service_hostname_complete(DnsQuery *q) { | |
986 | int r; | |
987 | ||
988 | assert(q); | |
989 | assert(q->auxiliary_for); | |
990 | ||
991 | if (q->state != DNS_TRANSACTION_SUCCESS) { | |
992 | resolve_service_all_complete(q->auxiliary_for); | |
993 | return; | |
994 | } | |
995 | ||
996 | r = dns_query_process_cname(q); | |
7588460a | 997 | if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */ |
45ec7efb LP |
998 | return; |
999 | ||
1000 | /* This auxiliary lookup is finished or failed, let's see if all are finished now. */ | |
1001 | q->auxiliary_result = r; | |
1002 | resolve_service_all_complete(q->auxiliary_for); | |
1003 | } | |
1004 | ||
1005 | static int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) { | |
1006 | _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; | |
1007 | DnsQuery *aux; | |
1008 | int r; | |
1009 | ||
1010 | assert(q); | |
1011 | assert(rr); | |
1012 | assert(rr->key); | |
1013 | assert(rr->key->type == DNS_TYPE_SRV); | |
1014 | ||
1015 | /* OK, we found an SRV record for the service. Let's resolve | |
1016 | * the hostname included in it */ | |
1017 | ||
23b298bc | 1018 | r = dns_question_new_address(&question, q->request_family, rr->srv.name, false); |
45ec7efb LP |
1019 | if (r < 0) |
1020 | return r; | |
1021 | ||
23b298bc | 1022 | r = dns_query_new(q->manager, &aux, question, question, ifindex, q->flags|SD_RESOLVED_NO_SEARCH); |
45ec7efb LP |
1023 | if (r < 0) |
1024 | return r; | |
1025 | ||
1026 | aux->request_family = q->request_family; | |
1027 | aux->complete = resolve_service_hostname_complete; | |
1028 | ||
1029 | r = dns_query_make_auxiliary(aux, q); | |
1030 | if (r == -EAGAIN) { | |
1031 | /* Too many auxiliary lookups? If so, don't complain, | |
1032 | * let's just not add this one, we already have more | |
1033 | * than enough */ | |
1034 | ||
1035 | dns_query_free(aux); | |
1036 | return 0; | |
1037 | } | |
1038 | if (r < 0) | |
1039 | goto fail; | |
1040 | ||
1041 | /* Note that auxiliary queries do not track the original bus | |
1042 | * client, only the primary request does that. */ | |
1043 | ||
1044 | r = dns_query_go(aux); | |
1045 | if (r < 0) | |
1046 | goto fail; | |
1047 | ||
1048 | return 1; | |
1049 | ||
1050 | fail: | |
1051 | dns_query_free(aux); | |
1052 | return r; | |
1053 | } | |
1054 | ||
1055 | static void bus_method_resolve_service_complete(DnsQuery *q) { | |
23b298bc LP |
1056 | bool has_root_domain = false; |
1057 | DnsResourceRecord *rr; | |
1058 | DnsQuestion *question; | |
45ec7efb | 1059 | unsigned found = 0; |
23b298bc | 1060 | int ifindex, r; |
45ec7efb LP |
1061 | |
1062 | assert(q); | |
1063 | ||
1064 | if (q->state != DNS_TRANSACTION_SUCCESS) { | |
1065 | r = reply_query_state(q); | |
1066 | goto finish; | |
1067 | } | |
1068 | ||
1069 | r = dns_query_process_cname(q); | |
1070 | if (r == -ELOOP) { | |
23b298bc | 1071 | 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 |
1072 | goto finish; |
1073 | } | |
1074 | if (r < 0) | |
1075 | goto finish; | |
7588460a | 1076 | if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */ |
45ec7efb LP |
1077 | return; |
1078 | ||
23b298bc | 1079 | question = dns_query_question_for_protocol(q, q->answer_protocol); |
45ec7efb | 1080 | |
23b298bc LP |
1081 | DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) { |
1082 | r = dns_question_matches_rr(question, rr, NULL); | |
1083 | if (r < 0) | |
1084 | goto finish; | |
1085 | if (r == 0) | |
1086 | continue; | |
45ec7efb | 1087 | |
23b298bc LP |
1088 | if (rr->key->type != DNS_TYPE_SRV) |
1089 | continue; | |
9a1f0c28 | 1090 | |
23b298bc LP |
1091 | if (dns_name_is_root(rr->srv.name)) { |
1092 | has_root_domain = true; | |
1093 | continue; | |
1094 | } | |
45ec7efb | 1095 | |
23b298bc LP |
1096 | if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) { |
1097 | q->block_all_complete ++; | |
1098 | r = resolve_service_hostname(q, rr, ifindex); | |
1099 | q->block_all_complete --; | |
45ec7efb | 1100 | |
23b298bc LP |
1101 | if (r < 0) |
1102 | goto finish; | |
45ec7efb | 1103 | } |
9a1f0c28 | 1104 | |
23b298bc LP |
1105 | found++; |
1106 | } | |
9a1f0c28 | 1107 | |
23b298bc LP |
1108 | if (has_root_domain && found <= 0) { |
1109 | /* If there's exactly one SRV RR and it uses | |
1110 | * the root domain as host name, then the | |
1111 | * service is explicitly not offered on the | |
1112 | * domain. Report this as a recognizable | |
1113 | * error. See RFC 2782, Section "Usage | |
1114 | * Rules". */ | |
1115 | r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide the requested service", dns_query_string(q)); | |
1116 | goto finish; | |
45ec7efb LP |
1117 | } |
1118 | ||
1119 | if (found <= 0) { | |
23b298bc | 1120 | 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 |
1121 | goto finish; |
1122 | } | |
1123 | ||
1124 | /* Maybe we are already finished? check now... */ | |
1125 | resolve_service_all_complete(q); | |
1126 | return; | |
1127 | ||
1128 | finish: | |
2d4c5cbc | 1129 | if (r < 0) { |
45ec7efb LP |
1130 | log_error_errno(r, "Failed to send service reply: %m"); |
1131 | sd_bus_reply_method_errno(q->request, r, NULL); | |
1132 | } | |
1133 | ||
1134 | dns_query_free(q); | |
1135 | } | |
1136 | ||
1137 | static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
23b298bc LP |
1138 | _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL; |
1139 | const char *name, *type, *domain; | |
45ec7efb LP |
1140 | _cleanup_free_ char *n = NULL; |
1141 | Manager *m = userdata; | |
1142 | int family, ifindex; | |
1143 | uint64_t flags; | |
1144 | DnsQuery *q; | |
1145 | int r; | |
1146 | ||
1147 | assert(message); | |
1148 | assert(m); | |
1149 | ||
1150 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
1151 | ||
1152 | r = sd_bus_message_read(message, "isssit", &ifindex, &name, &type, &domain, &family, &flags); | |
1153 | if (r < 0) | |
2d4c5cbc | 1154 | return r; |
45ec7efb LP |
1155 | |
1156 | if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC)) | |
1157 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family); | |
1158 | ||
1159 | if (isempty(name)) | |
1160 | name = NULL; | |
23b298bc LP |
1161 | else if (!dns_service_name_is_valid(name)) |
1162 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid service name '%s'", name); | |
2d4c5cbc | 1163 | |
45ec7efb LP |
1164 | if (isempty(type)) |
1165 | type = NULL; | |
7e8131e9 LP |
1166 | else if (!dns_srv_type_is_valid(type)) |
1167 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid SRV service type '%s'", type); | |
45ec7efb LP |
1168 | |
1169 | r = dns_name_is_valid(domain); | |
1170 | if (r < 0) | |
1171 | return r; | |
1172 | if (r == 0) | |
1173 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid domain '%s'", domain); | |
1174 | ||
1175 | if (name && !type) | |
1176 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type."); | |
1177 | ||
1178 | r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS, error); | |
1179 | if (r < 0) | |
1180 | return r; | |
1181 | ||
23b298bc LP |
1182 | r = dns_question_new_service(&question_utf8, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), false); |
1183 | if (r < 0) | |
1184 | return r; | |
45ec7efb | 1185 | |
23b298bc | 1186 | r = dns_question_new_service(&question_idna, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), true); |
45ec7efb LP |
1187 | if (r < 0) |
1188 | return r; | |
1189 | ||
23b298bc | 1190 | r = dns_query_new(m, &q, question_utf8, question_idna, ifindex, flags|SD_RESOLVED_NO_SEARCH); |
45ec7efb LP |
1191 | if (r < 0) |
1192 | return r; | |
1193 | ||
1194 | q->request = sd_bus_message_ref(message); | |
1195 | q->request_family = family; | |
1196 | q->complete = bus_method_resolve_service_complete; | |
1197 | ||
1198 | r = dns_query_bus_track(q, message); | |
1199 | if (r < 0) | |
1200 | goto fail; | |
1201 | ||
1202 | r = dns_query_go(q); | |
1203 | if (r < 0) | |
1204 | goto fail; | |
1205 | ||
2d4c5cbc | 1206 | return 1; |
45ec7efb LP |
1207 | |
1208 | fail: | |
1209 | dns_query_free(q); | |
1210 | return r; | |
2d4c5cbc LP |
1211 | } |
1212 | ||
3abaabda | 1213 | int bus_dns_server_append(sd_bus_message *reply, DnsServer *s, bool with_ifindex) { |
7f220d94 LP |
1214 | int r; |
1215 | ||
1216 | assert(reply); | |
1217 | assert(s); | |
1218 | ||
3abaabda | 1219 | r = sd_bus_message_open_container(reply, 'r', with_ifindex ? "iiay" : "iay"); |
7f220d94 LP |
1220 | if (r < 0) |
1221 | return r; | |
1222 | ||
3abaabda LP |
1223 | if (with_ifindex) { |
1224 | r = sd_bus_message_append(reply, "i", s->link ? s->link->ifindex : 0); | |
1225 | if (r < 0) | |
1226 | return r; | |
1227 | } | |
1228 | ||
1229 | r = sd_bus_message_append(reply, "i", s->family); | |
7f220d94 LP |
1230 | if (r < 0) |
1231 | return r; | |
1232 | ||
1233 | r = sd_bus_message_append_array(reply, 'y', &s->address, FAMILY_ADDRESS_SIZE(s->family)); | |
1234 | if (r < 0) | |
1235 | return r; | |
1236 | ||
1237 | return sd_bus_message_close_container(reply); | |
1238 | } | |
1239 | ||
1240 | static int bus_property_get_dns_servers( | |
1241 | sd_bus *bus, | |
1242 | const char *path, | |
1243 | const char *interface, | |
1244 | const char *property, | |
1245 | sd_bus_message *reply, | |
1246 | void *userdata, | |
1247 | sd_bus_error *error) { | |
1248 | ||
1249 | Manager *m = userdata; | |
1250 | unsigned c = 0; | |
1251 | DnsServer *s; | |
1252 | Iterator i; | |
1253 | Link *l; | |
1254 | int r; | |
1255 | ||
1256 | assert(reply); | |
1257 | assert(m); | |
1258 | ||
1259 | r = sd_bus_message_open_container(reply, 'a', "(iiay)"); | |
1260 | if (r < 0) | |
1261 | return r; | |
1262 | ||
1263 | LIST_FOREACH(servers, s, m->dns_servers) { | |
3abaabda | 1264 | r = bus_dns_server_append(reply, s, true); |
7f220d94 LP |
1265 | if (r < 0) |
1266 | return r; | |
1267 | ||
1268 | c++; | |
1269 | } | |
1270 | ||
1271 | HASHMAP_FOREACH(l, m->links, i) { | |
1272 | LIST_FOREACH(servers, s, l->dns_servers) { | |
3abaabda | 1273 | r = bus_dns_server_append(reply, s, true); |
7f220d94 LP |
1274 | if (r < 0) |
1275 | return r; | |
1276 | c++; | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | if (c == 0) { | |
1281 | LIST_FOREACH(servers, s, m->fallback_dns_servers) { | |
3abaabda | 1282 | r = bus_dns_server_append(reply, s, true); |
7f220d94 LP |
1283 | if (r < 0) |
1284 | return r; | |
1285 | } | |
1286 | } | |
1287 | ||
1288 | return sd_bus_message_close_container(reply); | |
1289 | } | |
1290 | ||
ee116b54 | 1291 | static int bus_property_get_domains( |
7f220d94 LP |
1292 | sd_bus *bus, |
1293 | const char *path, | |
1294 | const char *interface, | |
1295 | const char *property, | |
1296 | sd_bus_message *reply, | |
1297 | void *userdata, | |
1298 | sd_bus_error *error) { | |
1299 | ||
1300 | Manager *m = userdata; | |
1301 | DnsSearchDomain *d; | |
1302 | Iterator i; | |
1303 | Link *l; | |
1304 | int r; | |
1305 | ||
1306 | assert(reply); | |
1307 | assert(m); | |
1308 | ||
ad44b56b | 1309 | r = sd_bus_message_open_container(reply, 'a', "(isb)"); |
7f220d94 LP |
1310 | if (r < 0) |
1311 | return r; | |
1312 | ||
1313 | LIST_FOREACH(domains, d, m->search_domains) { | |
ad44b56b | 1314 | r = sd_bus_message_append(reply, "(isb)", 0, d->name, d->route_only); |
7f220d94 LP |
1315 | if (r < 0) |
1316 | return r; | |
1317 | } | |
1318 | ||
1319 | HASHMAP_FOREACH(l, m->links, i) { | |
1320 | LIST_FOREACH(domains, d, l->search_domains) { | |
ad44b56b | 1321 | r = sd_bus_message_append(reply, "(isb)", l->ifindex, d->name, d->route_only); |
7f220d94 LP |
1322 | if (r < 0) |
1323 | return r; | |
1324 | } | |
1325 | } | |
1326 | ||
1327 | return sd_bus_message_close_container(reply); | |
1328 | } | |
1329 | ||
a150ff5e LP |
1330 | static int bus_property_get_transaction_statistics( |
1331 | sd_bus *bus, | |
1332 | const char *path, | |
1333 | const char *interface, | |
1334 | const char *property, | |
1335 | sd_bus_message *reply, | |
1336 | void *userdata, | |
1337 | sd_bus_error *error) { | |
1338 | ||
1339 | Manager *m = userdata; | |
1340 | ||
1341 | assert(reply); | |
1342 | assert(m); | |
1343 | ||
1344 | return sd_bus_message_append(reply, "(tt)", | |
1345 | (uint64_t) hashmap_size(m->dns_transactions), | |
1346 | (uint64_t) m->n_transactions_total); | |
1347 | } | |
1348 | ||
1349 | static int bus_property_get_cache_statistics( | |
1350 | sd_bus *bus, | |
1351 | const char *path, | |
1352 | const char *interface, | |
1353 | const char *property, | |
1354 | sd_bus_message *reply, | |
1355 | void *userdata, | |
1356 | sd_bus_error *error) { | |
1357 | ||
1358 | uint64_t size = 0, hit = 0, miss = 0; | |
1359 | Manager *m = userdata; | |
1360 | DnsScope *s; | |
1361 | ||
1362 | assert(reply); | |
1363 | assert(m); | |
1364 | ||
1365 | LIST_FOREACH(scopes, s, m->dns_scopes) { | |
1366 | size += dns_cache_size(&s->cache); | |
1367 | hit += s->cache.n_hit; | |
1368 | miss += s->cache.n_miss; | |
1369 | } | |
1370 | ||
1371 | return sd_bus_message_append(reply, "(ttt)", size, hit, miss); | |
1372 | } | |
1373 | ||
1374 | static int bus_property_get_dnssec_statistics( | |
1375 | sd_bus *bus, | |
1376 | const char *path, | |
1377 | const char *interface, | |
1378 | const char *property, | |
1379 | sd_bus_message *reply, | |
1380 | void *userdata, | |
1381 | sd_bus_error *error) { | |
1382 | ||
1383 | Manager *m = userdata; | |
1384 | ||
1385 | assert(reply); | |
1386 | assert(m); | |
1387 | ||
1388 | return sd_bus_message_append(reply, "(tttt)", | |
59c5b597 LP |
1389 | (uint64_t) m->n_dnssec_verdict[DNSSEC_SECURE], |
1390 | (uint64_t) m->n_dnssec_verdict[DNSSEC_INSECURE], | |
1391 | (uint64_t) m->n_dnssec_verdict[DNSSEC_BOGUS], | |
1392 | (uint64_t) m->n_dnssec_verdict[DNSSEC_INDETERMINATE]); | |
a150ff5e LP |
1393 | } |
1394 | ||
593f665c LP |
1395 | static int bus_property_get_dnssec_supported( |
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 | Manager *m = userdata; | |
593f665c LP |
1405 | |
1406 | assert(reply); | |
1407 | assert(m); | |
1408 | ||
c69fa7e3 | 1409 | return sd_bus_message_append(reply, "b", manager_dnssec_supported(m)); |
593f665c LP |
1410 | } |
1411 | ||
a150ff5e LP |
1412 | static int bus_method_reset_statistics(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1413 | Manager *m = userdata; | |
1414 | DnsScope *s; | |
1415 | ||
1416 | assert(message); | |
1417 | assert(m); | |
1418 | ||
1419 | LIST_FOREACH(scopes, s, m->dns_scopes) | |
1420 | s->cache.n_hit = s->cache.n_miss = 0; | |
1421 | ||
1422 | m->n_transactions_total = 0; | |
59c5b597 | 1423 | zero(m->n_dnssec_verdict); |
a150ff5e LP |
1424 | |
1425 | return sd_bus_reply_method_return(message, NULL); | |
1426 | } | |
1427 | ||
3abaabda | 1428 | static int get_any_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) { |
97e5d693 LP |
1429 | Link *l; |
1430 | ||
1431 | assert(m); | |
3abaabda | 1432 | assert(ret); |
97e5d693 LP |
1433 | |
1434 | if (ifindex <= 0) | |
1435 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index"); | |
1436 | ||
1437 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
1438 | if (!l) | |
1439 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %i not known", ifindex); | |
3abaabda LP |
1440 | |
1441 | *ret = l; | |
1442 | return 0; | |
1443 | } | |
1444 | ||
1445 | static int get_unmanaged_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) { | |
1446 | Link *l; | |
1447 | int r; | |
1448 | ||
1449 | assert(m); | |
1450 | assert(ret); | |
1451 | ||
1452 | r = get_any_link(m, ifindex, &l, error); | |
1453 | if (r < 0) | |
1454 | return r; | |
1455 | ||
97e5d693 LP |
1456 | if (l->flags & IFF_LOOPBACK) |
1457 | return sd_bus_error_setf(error, BUS_ERROR_LINK_BUSY, "Link %s is loopback device.", l->name); | |
1458 | if (l->is_managed) | |
1459 | return sd_bus_error_setf(error, BUS_ERROR_LINK_BUSY, "Link %s is managed.", l->name); | |
1460 | ||
1461 | *ret = l; | |
1462 | return 0; | |
1463 | } | |
1464 | ||
d2ec6608 | 1465 | static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) { |
97e5d693 | 1466 | int ifindex, r; |
97e5d693 LP |
1467 | Link *l; |
1468 | ||
97e5d693 | 1469 | assert(m); |
97e5d693 | 1470 | assert(message); |
d2ec6608 | 1471 | assert(handler); |
97e5d693 | 1472 | |
d2ec6608 | 1473 | assert_cc(sizeof(int) == sizeof(int32_t)); |
97e5d693 LP |
1474 | r = sd_bus_message_read(message, "i", &ifindex); |
1475 | if (r < 0) | |
1476 | return r; | |
1477 | ||
1478 | r = get_unmanaged_link(m, ifindex, &l, error); | |
1479 | if (r < 0) | |
1480 | return r; | |
1481 | ||
d2ec6608 LP |
1482 | return handler(message, l, error); |
1483 | } | |
97e5d693 | 1484 | |
d2ec6608 LP |
1485 | static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1486 | return call_link_method(userdata, message, bus_link_method_set_dns_servers, error); | |
1487 | } | |
97e5d693 | 1488 | |
ee116b54 LP |
1489 | static int bus_method_set_link_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1490 | return call_link_method(userdata, message, bus_link_method_set_domains, error); | |
97e5d693 LP |
1491 | } |
1492 | ||
1493 | static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
d2ec6608 | 1494 | return call_link_method(userdata, message, bus_link_method_set_llmnr, error); |
97e5d693 LP |
1495 | } |
1496 | ||
1497 | static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
d2ec6608 | 1498 | return call_link_method(userdata, message, bus_link_method_set_mdns, error); |
97e5d693 LP |
1499 | } |
1500 | ||
1501 | static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
d2ec6608 | 1502 | return call_link_method(userdata, message, bus_link_method_set_dnssec, error); |
97e5d693 LP |
1503 | } |
1504 | ||
1505 | static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
d2ec6608 | 1506 | return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error); |
97e5d693 LP |
1507 | } |
1508 | ||
1509 | static int bus_method_revert_link(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
d2ec6608 | 1510 | return call_link_method(userdata, message, bus_link_method_revert, error); |
97e5d693 LP |
1511 | } |
1512 | ||
3abaabda LP |
1513 | static int bus_method_get_link(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1514 | _cleanup_free_ char *p = NULL; | |
1515 | Manager *m = userdata; | |
1516 | int r, ifindex; | |
1517 | Link *l; | |
1518 | ||
1519 | assert(message); | |
1520 | assert(m); | |
1521 | ||
1522 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
3abaabda LP |
1523 | r = sd_bus_message_read(message, "i", &ifindex); |
1524 | if (r < 0) | |
1525 | return r; | |
1526 | ||
1527 | r = get_any_link(m, ifindex, &l, error); | |
1528 | if (r < 0) | |
1529 | return r; | |
1530 | ||
1531 | p = link_bus_path(l); | |
1532 | if (!p) | |
1533 | return -ENOMEM; | |
1534 | ||
1535 | return sd_bus_reply_method_return(message, "o", p); | |
1536 | } | |
1537 | ||
74b2466e LP |
1538 | static const sd_bus_vtable resolve_vtable[] = { |
1539 | SD_BUS_VTABLE_START(0), | |
7f220d94 | 1540 | SD_BUS_PROPERTY("LLMNRHostname", "s", NULL, offsetof(Manager, llmnr_hostname), 0), |
e40f0647 | 1541 | SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers, 0, 0), |
ee116b54 | 1542 | SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains, 0, 0), |
a150ff5e LP |
1543 | SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics, 0, 0), |
1544 | SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics, 0, 0), | |
1545 | SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics, 0, 0), | |
593f665c | 1546 | SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported, 0, 0), |
7f220d94 | 1547 | |
78c6a153 LP |
1548 | SD_BUS_METHOD("ResolveHostname", "isit", "a(iiay)st", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED), |
1549 | SD_BUS_METHOD("ResolveAddress", "iiayt", "a(is)t", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED), | |
1550 | SD_BUS_METHOD("ResolveRecord", "isqqt", "a(iqqay)t", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED), | |
45ec7efb | 1551 | SD_BUS_METHOD("ResolveService", "isssit", "a(qqqsa(iiay)s)aayssst", bus_method_resolve_service, SD_BUS_VTABLE_UNPRIVILEGED), |
a150ff5e | 1552 | SD_BUS_METHOD("ResetStatistics", NULL, NULL, bus_method_reset_statistics, 0), |
3abaabda | 1553 | SD_BUS_METHOD("GetLink", "i", "o", bus_method_get_link, SD_BUS_VTABLE_UNPRIVILEGED), |
97e5d693 | 1554 | SD_BUS_METHOD("SetLinkDNS", "ia(iay)", NULL, bus_method_set_link_dns_servers, 0), |
ee116b54 | 1555 | SD_BUS_METHOD("SetLinkDomains", "ia(sb)", NULL, bus_method_set_link_domains, 0), |
97e5d693 LP |
1556 | SD_BUS_METHOD("SetLinkLLMNR", "is", NULL, bus_method_set_link_llmnr, 0), |
1557 | SD_BUS_METHOD("SetLinkMulticastDNS", "is", NULL, bus_method_set_link_mdns, 0), | |
1558 | SD_BUS_METHOD("SetLinkDNSSEC", "is", NULL, bus_method_set_link_dnssec, 0), | |
1559 | SD_BUS_METHOD("SetLinkDNSSECNegativeTrustAnchors", "ias", NULL, bus_method_set_link_dnssec_negative_trust_anchors, 0), | |
1560 | SD_BUS_METHOD("RevertLink", "i", NULL, bus_method_revert_link, 0), | |
1561 | ||
74b2466e LP |
1562 | SD_BUS_VTABLE_END, |
1563 | }; | |
1564 | ||
1565 | static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) { | |
1566 | Manager *m = userdata; | |
1567 | ||
1568 | assert(s); | |
1569 | assert(m); | |
1570 | ||
1571 | m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source); | |
1572 | ||
1573 | manager_connect_bus(m); | |
1574 | return 0; | |
1575 | } | |
1576 | ||
19070062 | 1577 | static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
902bb5d8 LP |
1578 | Manager *m = userdata; |
1579 | int b, r; | |
1580 | ||
19070062 LP |
1581 | assert(message); |
1582 | assert(m); | |
902bb5d8 LP |
1583 | |
1584 | r = sd_bus_message_read(message, "b", &b); | |
1585 | if (r < 0) { | |
da927ba9 | 1586 | log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m"); |
902bb5d8 LP |
1587 | return 0; |
1588 | } | |
1589 | ||
1590 | if (b) | |
1591 | return 0; | |
1592 | ||
1593 | log_debug("Coming back from suspend, verifying all RRs..."); | |
1594 | ||
1595 | manager_verify_all(m); | |
1596 | return 0; | |
1597 | } | |
1598 | ||
74b2466e LP |
1599 | int manager_connect_bus(Manager *m) { |
1600 | int r; | |
1601 | ||
1602 | assert(m); | |
1603 | ||
1604 | if (m->bus) | |
1605 | return 0; | |
1606 | ||
1607 | r = sd_bus_default_system(&m->bus); | |
1608 | if (r < 0) { | |
1609 | /* We failed to connect? Yuck, we must be in early | |
1610 | * boot. Let's try in 5s again. As soon as we have | |
1611 | * kdbus we can stop doing this... */ | |
1612 | ||
da927ba9 | 1613 | log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m"); |
74b2466e LP |
1614 | |
1615 | 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 |
1616 | if (r < 0) |
1617 | return log_error_errno(r, "Failed to install bus reconnect time event: %m"); | |
74b2466e | 1618 | |
aa4a9deb | 1619 | (void) sd_event_source_set_description(m->bus_retry_event_source, "bus-retry"); |
74b2466e LP |
1620 | return 0; |
1621 | } | |
1622 | ||
4d1cf1e2 | 1623 | r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable, m); |
f647962d MS |
1624 | if (r < 0) |
1625 | return log_error_errno(r, "Failed to register object: %m"); | |
74b2466e | 1626 | |
3abaabda LP |
1627 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/resolve1/link", "org.freedesktop.resolve1.Link", link_vtable, link_object_find, m); |
1628 | if (r < 0) | |
1629 | return log_error_errno(r, "Failed to register link objects: %m"); | |
1630 | ||
1631 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/resolve1/link", link_node_enumerator, m); | |
1632 | if (r < 0) | |
1633 | return log_error_errno(r, "Failed to register link enumerator: %m"); | |
1634 | ||
74b2466e | 1635 | r = sd_bus_request_name(m->bus, "org.freedesktop.resolve1", 0); |
f647962d MS |
1636 | if (r < 0) |
1637 | return log_error_errno(r, "Failed to register name: %m"); | |
74b2466e LP |
1638 | |
1639 | r = sd_bus_attach_event(m->bus, m->event, 0); | |
f647962d MS |
1640 | if (r < 0) |
1641 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
74b2466e | 1642 | |
902bb5d8 LP |
1643 | r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot, |
1644 | "type='signal'," | |
1645 | "sender='org.freedesktop.login1'," | |
1646 | "interface='org.freedesktop.login1.Manager'," | |
1647 | "member='PrepareForSleep'," | |
1648 | "path='/org/freedesktop/login1'", | |
1649 | match_prepare_for_sleep, | |
1650 | m); | |
1651 | if (r < 0) | |
da927ba9 | 1652 | log_error_errno(r, "Failed to add match for PrepareForSleep: %m"); |
902bb5d8 | 1653 | |
74b2466e LP |
1654 | return 0; |
1655 | } |