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