| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-event.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-polkit.h" |
| 7 |
#include "dns-answer.h" |
| 8 |
#include "dns-domain.h" |
| 9 |
#include "dns-packet.h" |
| 10 |
#include "dns-question.h" |
| 11 |
#include "dns-rr.h" |
| 12 |
#include "dns-type.h" |
| 13 |
#include "errno-util.h" |
| 14 |
#include "in-addr-util.h" |
| 15 |
#include "iovec-util.h" |
| 16 |
#include "json-util.h" |
| 17 |
#include "resolved-dns-browse-services.h" |
| 18 |
#include "resolved-dns-dnssec.h" |
| 19 |
#include "resolved-dns-query.h" |
| 20 |
#include "resolved-dns-scope.h" |
| 21 |
#include "resolved-dns-search-domain.h" |
| 22 |
#include "resolved-dns-server.h" |
| 23 |
#include "resolved-dns-synthesize.h" |
| 24 |
#include "resolved-dns-transaction.h" |
| 25 |
#include "resolved-link.h" |
| 26 |
#include "resolved-manager.h" |
| 27 |
#include "resolved-varlink.h" |
| 28 |
#include "set.h" |
| 29 |
#include "socket-netlink.h" |
| 30 |
#include "string-util.h" |
| 31 |
#include "varlink-io.systemd.Resolve.h" |
| 32 |
#include "varlink-io.systemd.Resolve.Monitor.h" |
| 33 |
#include "varlink-io.systemd.service.h" |
| 34 |
#include "varlink-util.h" |
| 35 |
|
| 36 |
typedef struct LookupParameters { |
| 37 |
int ifindex; |
| 38 |
uint64_t flags; |
| 39 |
int family; |
| 40 |
struct iovec address; |
| 41 |
const char *name; |
| 42 |
uint16_t class; |
| 43 |
uint16_t type; |
| 44 |
} LookupParameters; |
| 45 |
|
| 46 |
typedef struct LookupParametersResolveService { |
| 47 |
const char *name; |
| 48 |
const char *type; |
| 49 |
const char *domain; |
| 50 |
int family; |
| 51 |
int ifindex; |
| 52 |
uint64_t flags; |
| 53 |
} LookupParametersResolveService; |
| 54 |
|
| 55 |
typedef struct LookupParamatersBrowseServices { |
| 56 |
const char *domain; |
| 57 |
const char *type; |
| 58 |
int ifindex; |
| 59 |
uint64_t flags; |
| 60 |
} LookupParamatersBrowseServices; |
| 61 |
|
| 62 |
static void lookup_parameters_destroy(LookupParameters *p) { |
| 63 |
assert(p); |
| 64 |
|
| 65 |
iovec_done(&p->address); |
| 66 |
} |
| 67 |
|
| 68 |
static int dns_query_new_for_varlink( |
| 69 |
Manager *m, |
| 70 |
DnsQuery **ret, |
| 71 |
DnsQuestion *question_utf8, |
| 72 |
DnsQuestion *question_idna, |
| 73 |
DnsPacket *question_bypass, |
| 74 |
int ifindex, |
| 75 |
uint64_t flags, |
| 76 |
sd_varlink *link) { |
| 77 |
|
| 78 |
int r; |
| 79 |
|
| 80 |
r = dns_query_new(m, ret, question_utf8, question_idna, question_bypass, ifindex, flags); |
| 81 |
if (r == -ENOANO) |
| 82 |
return sd_varlink_error(link, "io.systemd.Resolve.QueryRefused", NULL); |
| 83 |
return r; |
| 84 |
} |
| 85 |
|
| 86 |
static int reply_query_state(DnsQuery *q) { |
| 87 |
|
| 88 |
assert(q); |
| 89 |
assert(q->varlink_request); |
| 90 |
|
| 91 |
switch (q->state) { |
| 92 |
|
| 93 |
case DNS_TRANSACTION_NO_SERVERS: |
| 94 |
return sd_varlink_error(q->varlink_request, "io.systemd.Resolve.NoNameServers", NULL); |
| 95 |
|
| 96 |
case DNS_TRANSACTION_TIMEOUT: |
| 97 |
return sd_varlink_error(q->varlink_request, "io.systemd.Resolve.QueryTimedOut", NULL); |
| 98 |
|
| 99 |
case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED: |
| 100 |
return sd_varlink_error(q->varlink_request, "io.systemd.Resolve.MaxAttemptsReached", NULL); |
| 101 |
|
| 102 |
case DNS_TRANSACTION_INVALID_REPLY: |
| 103 |
return sd_varlink_error(q->varlink_request, "io.systemd.Resolve.InvalidReply", NULL); |
| 104 |
|
| 105 |
case DNS_TRANSACTION_ERRNO: |
| 106 |
return sd_varlink_error_errno(q->varlink_request, q->answer_errno); |
| 107 |
|
| 108 |
case DNS_TRANSACTION_ABORTED: |
| 109 |
return sd_varlink_error(q->varlink_request, "io.systemd.Resolve.QueryAborted", NULL); |
| 110 |
|
| 111 |
case DNS_TRANSACTION_DNSSEC_FAILED: |
| 112 |
return sd_varlink_errorbo(q->varlink_request, "io.systemd.Resolve.DNSSECValidationFailed", |
| 113 |
SD_JSON_BUILD_PAIR_STRING("result", dnssec_result_to_string(q->answer_dnssec_result)), |
| 114 |
SD_JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0, |
| 115 |
"extendedDNSErrorCode", SD_JSON_BUILD_INTEGER(q->answer_ede_rcode)), |
| 116 |
SD_JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg), |
| 117 |
"extendedDNSErrorMessage", SD_JSON_BUILD_STRING(q->answer_ede_msg))); |
| 118 |
|
| 119 |
case DNS_TRANSACTION_NO_TRUST_ANCHOR: |
| 120 |
return sd_varlink_error(q->varlink_request, "io.systemd.Resolve.NoTrustAnchor", NULL); |
| 121 |
|
| 122 |
case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED: |
| 123 |
return sd_varlink_error(q->varlink_request, "io.systemd.Resolve.ResourceRecordTypeUnsupported", NULL); |
| 124 |
|
| 125 |
case DNS_TRANSACTION_NETWORK_DOWN: |
| 126 |
return sd_varlink_error(q->varlink_request, "io.systemd.Resolve.NetworkDown", NULL); |
| 127 |
|
| 128 |
case DNS_TRANSACTION_NO_SOURCE: |
| 129 |
return sd_varlink_error(q->varlink_request, "io.systemd.Resolve.NoSource", NULL); |
| 130 |
|
| 131 |
case DNS_TRANSACTION_STUB_LOOP: |
| 132 |
return sd_varlink_error(q->varlink_request, "io.systemd.Resolve.StubLoop", NULL); |
| 133 |
|
| 134 |
case DNS_TRANSACTION_NOT_FOUND: |
| 135 |
/* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we |
| 136 |
* thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */ |
| 137 |
return sd_varlink_errorbo(q->varlink_request, "io.systemd.Resolve.DNSError", |
| 138 |
SD_JSON_BUILD_PAIR_INTEGER("rcode", DNS_RCODE_NXDOMAIN)); |
| 139 |
|
| 140 |
case DNS_TRANSACTION_RCODE_FAILURE: |
| 141 |
return sd_varlink_errorbo(q->varlink_request, "io.systemd.Resolve.DNSError", |
| 142 |
SD_JSON_BUILD_PAIR_INTEGER("rcode", q->answer_rcode), |
| 143 |
SD_JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0, |
| 144 |
"extendedDNSErrorCode", SD_JSON_BUILD_INTEGER(q->answer_ede_rcode)), |
| 145 |
SD_JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg), |
| 146 |
"extendedDNSErrorMessage", SD_JSON_BUILD_STRING(q->answer_ede_msg))); |
| 147 |
|
| 148 |
case DNS_TRANSACTION_NULL: |
| 149 |
case DNS_TRANSACTION_PENDING: |
| 150 |
case DNS_TRANSACTION_VALIDATING: |
| 151 |
case DNS_TRANSACTION_SUCCESS: |
| 152 |
default: |
| 153 |
assert_not_reached(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
static void vl_on_disconnect(sd_varlink_server *s, sd_varlink *link, void *userdata) { |
| 158 |
DnsQuery *q; |
| 159 |
Manager *m; |
| 160 |
|
| 161 |
assert(s); |
| 162 |
assert(link); |
| 163 |
|
| 164 |
m = sd_varlink_server_get_userdata(s); |
| 165 |
if (!m) |
| 166 |
return; |
| 167 |
|
| 168 |
DnsServiceBrowser *sb = hashmap_remove(m->dns_service_browsers, link); |
| 169 |
dns_service_browser_unref(sb); |
| 170 |
|
| 171 |
q = sd_varlink_get_userdata(link); |
| 172 |
if (!q) |
| 173 |
return; |
| 174 |
|
| 175 |
if (!DNS_TRANSACTION_IS_LIVE(q->state)) |
| 176 |
return; |
| 177 |
|
| 178 |
log_debug("Client of active query vanished, aborting query."); |
| 179 |
dns_query_complete(q, DNS_TRANSACTION_ABORTED); |
| 180 |
} |
| 181 |
|
| 182 |
static void vl_on_notification_disconnect(sd_varlink_server *s, sd_varlink *link, void *userdata) { |
| 183 |
Manager *m = ASSERT_PTR(userdata); |
| 184 |
sd_varlink *removed_link = NULL; |
| 185 |
|
| 186 |
assert(s); |
| 187 |
assert(link); |
| 188 |
|
| 189 |
removed_link = set_remove(m->varlink_query_results_subscription, link); |
| 190 |
if (removed_link) { |
| 191 |
sd_varlink_unref(removed_link); |
| 192 |
log_debug("%u query result monitor clients remain active", set_size(m->varlink_query_results_subscription)); |
| 193 |
} |
| 194 |
|
| 195 |
removed_link = set_remove(m->varlink_dns_configuration_subscription, link); |
| 196 |
if (removed_link) { |
| 197 |
sd_varlink_unref(removed_link); |
| 198 |
log_debug("%u DNS monitor clients remain active", set_size(m->varlink_dns_configuration_subscription)); |
| 199 |
|
| 200 |
if (set_isempty(m->varlink_dns_configuration_subscription)) |
| 201 |
manager_stop_dns_configuration_monitor(m); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
static int find_addr_records( |
| 206 |
sd_json_variant **array, |
| 207 |
DnsQuestion *question, |
| 208 |
DnsQuery *q, |
| 209 |
DnsResourceRecord **canonical, |
| 210 |
const char *search_domain) { |
| 211 |
DnsResourceRecord *rr; |
| 212 |
int ifindex, r; |
| 213 |
|
| 214 |
assert(q); |
| 215 |
POINTER_MAY_BE_NULL(canonical); |
| 216 |
|
| 217 |
DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) { |
| 218 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *entry = NULL; |
| 219 |
int family; |
| 220 |
const void *p; |
| 221 |
|
| 222 |
r = dns_question_matches_rr(question, rr, search_domain); |
| 223 |
if (r < 0) |
| 224 |
return r; |
| 225 |
if (r == 0) |
| 226 |
continue; |
| 227 |
|
| 228 |
if (rr->key->type == DNS_TYPE_A) { |
| 229 |
family = AF_INET; |
| 230 |
p = &rr->a.in_addr; |
| 231 |
} else if (rr->key->type == DNS_TYPE_AAAA) { |
| 232 |
family = AF_INET6; |
| 233 |
p = &rr->aaaa.in6_addr; |
| 234 |
} else { |
| 235 |
return -EAFNOSUPPORT; |
| 236 |
} |
| 237 |
|
| 238 |
r = sd_json_buildo( |
| 239 |
&entry, |
| 240 |
SD_JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", SD_JSON_BUILD_INTEGER(ifindex)), |
| 241 |
SD_JSON_BUILD_PAIR_INTEGER("family", family), |
| 242 |
SD_JSON_BUILD_PAIR_BYTE_ARRAY("address", p, FAMILY_ADDRESS_SIZE(family))); |
| 243 |
if (r < 0) |
| 244 |
return r; |
| 245 |
|
| 246 |
r = sd_json_variant_append_array(array, entry); |
| 247 |
if (r < 0) |
| 248 |
return r; |
| 249 |
|
| 250 |
if (canonical && !*canonical) |
| 251 |
*canonical = dns_resource_record_ref(rr); |
| 252 |
} |
| 253 |
|
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
static void vl_method_resolve_hostname_complete(DnsQuery *query) { |
| 258 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL; |
| 259 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; |
| 260 |
_cleanup_(dns_query_freep) DnsQuery *q = query; |
| 261 |
_cleanup_free_ char *normalized = NULL; |
| 262 |
DnsQuestion *question; |
| 263 |
int r; |
| 264 |
|
| 265 |
assert(q); |
| 266 |
|
| 267 |
if (q->state != DNS_TRANSACTION_SUCCESS) |
| 268 |
return (void) reply_query_state(q); |
| 269 |
|
| 270 |
r = dns_query_process_cname_many(q); |
| 271 |
if (r == -ELOOP) |
| 272 |
return (void) sd_varlink_error(q->varlink_request, "io.systemd.Resolve.CNAMELoop", NULL); |
| 273 |
if (r < 0) |
| 274 |
goto finish; |
| 275 |
if (r == DNS_QUERY_CNAME) { |
| 276 |
/* This was a cname, and the query was restarted. */ |
| 277 |
TAKE_PTR(q); |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
question = dns_query_question_for_protocol(q, q->answer_protocol); |
| 282 |
|
| 283 |
r = find_addr_records(&array, question, q, &canonical, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain)); |
| 284 |
if (r < 0) |
| 285 |
goto finish; |
| 286 |
|
| 287 |
if (sd_json_variant_is_blank_object(array)) |
| 288 |
return (void) sd_varlink_error(q->varlink_request, "io.systemd.Resolve.NoSuchResourceRecord", NULL); |
| 289 |
|
| 290 |
assert(canonical); |
| 291 |
r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized); |
| 292 |
if (r < 0) |
| 293 |
goto finish; |
| 294 |
|
| 295 |
r = sd_varlink_replybo( |
| 296 |
q->varlink_request, |
| 297 |
SD_JSON_BUILD_PAIR_VARIANT("addresses", array), |
| 298 |
SD_JSON_BUILD_PAIR_STRING("name", normalized), |
| 299 |
SD_JSON_BUILD_PAIR_INTEGER("flags", dns_query_reply_flags_make(q))); |
| 300 |
finish: |
| 301 |
if (r < 0) { |
| 302 |
log_full_errno(ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_ERR, r, "Failed to send hostname reply: %m"); |
| 303 |
(void) sd_varlink_error_errno(q->varlink_request, r); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
static int parse_as_address(sd_varlink *link, LookupParameters *p) { |
| 308 |
_cleanup_free_ char *canonical = NULL; |
| 309 |
int r, ff, parsed_ifindex, ifindex; |
| 310 |
union in_addr_union parsed; |
| 311 |
|
| 312 |
assert(link); |
| 313 |
assert(p); |
| 314 |
|
| 315 |
/* Check if this parses as literal address. If so, just parse it and return that, do not involve networking */ |
| 316 |
r = in_addr_ifindex_from_string_auto(p->name, &ff, &parsed, &parsed_ifindex); |
| 317 |
if (r < 0) |
| 318 |
return 0; /* not a literal address */ |
| 319 |
|
| 320 |
/* Make sure the data we parsed matches what is requested */ |
| 321 |
if ((p->family != AF_UNSPEC && ff != p->family) || |
| 322 |
(p->ifindex > 0 && parsed_ifindex > 0 && parsed_ifindex != p->ifindex)) |
| 323 |
return sd_varlink_error(link, "io.systemd.Resolve.NoSuchResourceRecord", NULL); |
| 324 |
|
| 325 |
ifindex = parsed_ifindex > 0 ? parsed_ifindex : p->ifindex; |
| 326 |
|
| 327 |
/* Reformat the address as string, to return as canonicalized name */ |
| 328 |
r = in_addr_ifindex_to_string(ff, &parsed, ifindex, &canonical); |
| 329 |
if (r < 0) |
| 330 |
return r; |
| 331 |
|
| 332 |
return sd_varlink_replybo( |
| 333 |
link, |
| 334 |
SD_JSON_BUILD_PAIR("addresses", |
| 335 |
SD_JSON_BUILD_ARRAY( |
| 336 |
SD_JSON_BUILD_OBJECT( |
| 337 |
SD_JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", SD_JSON_BUILD_INTEGER(ifindex)), |
| 338 |
SD_JSON_BUILD_PAIR_INTEGER("family", ff), |
| 339 |
SD_JSON_BUILD_PAIR_BYTE_ARRAY("address", &parsed, FAMILY_ADDRESS_SIZE(ff))))), |
| 340 |
SD_JSON_BUILD_PAIR_STRING("name", canonical), |
| 341 |
SD_JSON_BUILD_PAIR_INTEGER("flags", SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(p->flags), ff, true, true)| |
| 342 |
SD_RESOLVED_SYNTHETIC)); |
| 343 |
} |
| 344 |
|
| 345 |
static int vl_method_resolve_hostname(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 346 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 347 |
{ "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(LookupParameters, ifindex), SD_JSON_RELAX }, |
| 348 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, name), SD_JSON_MANDATORY }, |
| 349 |
{ "family", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParameters, family), 0 }, |
| 350 |
{ "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LookupParameters, flags), 0 }, |
| 351 |
{} |
| 352 |
}; |
| 353 |
|
| 354 |
_cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL; |
| 355 |
_cleanup_(lookup_parameters_destroy) LookupParameters p = { |
| 356 |
.family = AF_UNSPEC, |
| 357 |
}; |
| 358 |
_cleanup_(dns_query_freep) DnsQuery *q = NULL; |
| 359 |
Manager *m; |
| 360 |
int r; |
| 361 |
|
| 362 |
assert(link); |
| 363 |
|
| 364 |
m = sd_varlink_server_get_userdata(sd_varlink_get_server(link)); |
| 365 |
assert(m); |
| 366 |
|
| 367 |
if (FLAGS_SET(flags, SD_VARLINK_METHOD_ONEWAY)) |
| 368 |
return -EINVAL; |
| 369 |
|
| 370 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 371 |
if (r != 0) |
| 372 |
return r; |
| 373 |
|
| 374 |
r = dns_name_is_valid(p.name); |
| 375 |
if (r < 0) |
| 376 |
return r; |
| 377 |
if (r == 0) |
| 378 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("name")); |
| 379 |
|
| 380 |
if (!IN_SET(p.family, AF_UNSPEC, AF_INET, AF_INET6)) |
| 381 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("family")); |
| 382 |
|
| 383 |
if (validate_and_mangle_query_flags(m, &p.flags, p.name, SD_RESOLVED_NO_SEARCH) < 0) |
| 384 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("flags")); |
| 385 |
|
| 386 |
r = parse_as_address(link, &p); |
| 387 |
if (r != 0) |
| 388 |
return r; |
| 389 |
|
| 390 |
r = dns_question_new_address(&question_utf8, p.family, p.name, false); |
| 391 |
if (r < 0) |
| 392 |
return r; |
| 393 |
|
| 394 |
r = dns_question_new_address(&question_idna, p.family, p.name, true); |
| 395 |
if (r < 0 && r != -EALREADY) |
| 396 |
return r; |
| 397 |
|
| 398 |
r = dns_query_new_for_varlink(m, &q, question_utf8, question_idna ?: question_utf8, NULL, p.ifindex, p.flags, link); |
| 399 |
if (r < 0) |
| 400 |
return r; |
| 401 |
|
| 402 |
q->varlink_request = sd_varlink_ref(link); |
| 403 |
sd_varlink_set_userdata(link, q); |
| 404 |
q->request_family = p.family; |
| 405 |
q->complete = vl_method_resolve_hostname_complete; |
| 406 |
|
| 407 |
r = dns_query_go(q); |
| 408 |
if (r < 0) |
| 409 |
return r; |
| 410 |
|
| 411 |
TAKE_PTR(q); |
| 412 |
return 1; |
| 413 |
} |
| 414 |
|
| 415 |
static void vl_method_resolve_address_complete(DnsQuery *query) { |
| 416 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; |
| 417 |
_cleanup_(dns_query_freep) DnsQuery *q = query; |
| 418 |
DnsQuestion *question; |
| 419 |
DnsResourceRecord *rr; |
| 420 |
int ifindex, r; |
| 421 |
|
| 422 |
assert(q); |
| 423 |
|
| 424 |
if (q->state != DNS_TRANSACTION_SUCCESS) |
| 425 |
return (void) reply_query_state(q); |
| 426 |
|
| 427 |
r = dns_query_process_cname_many(q); |
| 428 |
if (r == -ELOOP) |
| 429 |
return (void) sd_varlink_error(q->varlink_request, "io.systemd.Resolve.CNAMELoop", NULL); |
| 430 |
if (r < 0) |
| 431 |
goto finish; |
| 432 |
if (r == DNS_QUERY_CNAME) { |
| 433 |
/* This was a cname, and the query was restarted. */ |
| 434 |
TAKE_PTR(q); |
| 435 |
return; |
| 436 |
} |
| 437 |
|
| 438 |
question = dns_query_question_for_protocol(q, q->answer_protocol); |
| 439 |
|
| 440 |
DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) { |
| 441 |
_cleanup_free_ char *normalized = NULL; |
| 442 |
|
| 443 |
r = dns_question_matches_rr(question, rr, NULL); |
| 444 |
if (r < 0) |
| 445 |
goto finish; |
| 446 |
if (r == 0) |
| 447 |
continue; |
| 448 |
|
| 449 |
r = dns_name_normalize(rr->ptr.name, 0, &normalized); |
| 450 |
if (r < 0) |
| 451 |
goto finish; |
| 452 |
|
| 453 |
r = sd_json_variant_append_arraybo( |
| 454 |
&array, |
| 455 |
SD_JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", SD_JSON_BUILD_INTEGER(ifindex)), |
| 456 |
SD_JSON_BUILD_PAIR_STRING("name", normalized)); |
| 457 |
if (r < 0) |
| 458 |
goto finish; |
| 459 |
} |
| 460 |
|
| 461 |
if (sd_json_variant_is_blank_object(array)) |
| 462 |
return (void) sd_varlink_error(q->varlink_request, "io.systemd.Resolve.NoSuchResourceRecord", NULL); |
| 463 |
|
| 464 |
r = sd_varlink_replybo( |
| 465 |
q->varlink_request, |
| 466 |
SD_JSON_BUILD_PAIR_VARIANT("names", array), |
| 467 |
SD_JSON_BUILD_PAIR_INTEGER("flags", dns_query_reply_flags_make(q))); |
| 468 |
finish: |
| 469 |
if (r < 0) { |
| 470 |
log_full_errno(ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_ERR, r, "Failed to send address reply: %m"); |
| 471 |
(void) sd_varlink_error_errno(q->varlink_request, r); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
static int vl_method_resolve_address(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 476 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 477 |
{ "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(LookupParameters, ifindex), SD_JSON_RELAX }, |
| 478 |
{ "family", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParameters, family), SD_JSON_MANDATORY }, |
| 479 |
{ "address", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(LookupParameters, address), SD_JSON_MANDATORY }, |
| 480 |
{ "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LookupParameters, flags), 0 }, |
| 481 |
{} |
| 482 |
}; |
| 483 |
|
| 484 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 485 |
_cleanup_(lookup_parameters_destroy) LookupParameters p = { |
| 486 |
.family = AF_UNSPEC, |
| 487 |
}; |
| 488 |
_cleanup_(dns_query_freep) DnsQuery *q = NULL; |
| 489 |
Manager *m; |
| 490 |
int r; |
| 491 |
|
| 492 |
assert(link); |
| 493 |
|
| 494 |
m = sd_varlink_server_get_userdata(sd_varlink_get_server(link)); |
| 495 |
assert(m); |
| 496 |
|
| 497 |
if (FLAGS_SET(flags, SD_VARLINK_METHOD_ONEWAY)) |
| 498 |
return -EINVAL; |
| 499 |
|
| 500 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 501 |
if (r != 0) |
| 502 |
return r; |
| 503 |
|
| 504 |
if (!IN_SET(p.family, AF_INET, AF_INET6)) |
| 505 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("family")); |
| 506 |
|
| 507 |
if (FAMILY_ADDRESS_SIZE(p.family) != p.address.iov_len) |
| 508 |
return sd_varlink_error(link, "io.systemd.Resolve.BadAddressSize", NULL); |
| 509 |
|
| 510 |
if (validate_and_mangle_query_flags(m, &p.flags, /* name= */ NULL, /* ok= */ 0) < 0) |
| 511 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("flags")); |
| 512 |
|
| 513 |
union in_addr_union a = IN_ADDR_NULL; |
| 514 |
memcpy(&a, p.address.iov_base, p.address.iov_len); |
| 515 |
r = dns_question_new_reverse(&question, p.family, &a); |
| 516 |
if (r < 0) |
| 517 |
return r; |
| 518 |
|
| 519 |
r = dns_query_new_for_varlink(m, &q, question, question, NULL, p.ifindex, p.flags|SD_RESOLVED_NO_SEARCH, link); |
| 520 |
if (r < 0) |
| 521 |
return r; |
| 522 |
|
| 523 |
q->varlink_request = sd_varlink_ref(link); |
| 524 |
sd_varlink_set_userdata(link, q); |
| 525 |
|
| 526 |
q->request_family = p.family; |
| 527 |
q->request_address = a; |
| 528 |
q->complete = vl_method_resolve_address_complete; |
| 529 |
|
| 530 |
r = dns_query_go(q); |
| 531 |
if (r < 0) |
| 532 |
return r; |
| 533 |
|
| 534 |
TAKE_PTR(q); |
| 535 |
return 1; |
| 536 |
} |
| 537 |
|
| 538 |
static int append_txt(sd_json_variant **txt, DnsResourceRecord *rr) { |
| 539 |
int r; |
| 540 |
|
| 541 |
assert(txt); |
| 542 |
assert(rr); |
| 543 |
assert(rr->key); |
| 544 |
|
| 545 |
if (rr->key->type != DNS_TYPE_TXT) |
| 546 |
return 0; |
| 547 |
|
| 548 |
LIST_FOREACH(items, i, rr->txt.items) { |
| 549 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *entry = NULL; |
| 550 |
|
| 551 |
if (i->length <= 0) |
| 552 |
continue; |
| 553 |
|
| 554 |
r = sd_json_variant_new_octescape(&entry, i->data, i->length); |
| 555 |
if (r < 0) |
| 556 |
return r; |
| 557 |
|
| 558 |
r = sd_json_variant_append_array(txt, entry); |
| 559 |
if (r < 0) |
| 560 |
return r; |
| 561 |
} |
| 562 |
|
| 563 |
return 1; |
| 564 |
} |
| 565 |
|
| 566 |
static int append_srv( |
| 567 |
DnsQuery *q, |
| 568 |
DnsResourceRecord *rr, |
| 569 |
sd_json_variant **array) { |
| 570 |
|
| 571 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL; |
| 572 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 573 |
_cleanup_free_ char *normalized = NULL; |
| 574 |
int r; |
| 575 |
|
| 576 |
assert(q); |
| 577 |
assert(rr); |
| 578 |
assert(rr->key); |
| 579 |
assert(array); |
| 580 |
|
| 581 |
if (rr->key->type != DNS_TYPE_SRV) |
| 582 |
return 0; |
| 583 |
|
| 584 |
if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) { |
| 585 |
/* First, let's see if we could find an appropriate A or AAAA |
| 586 |
* record for the SRV record */ |
| 587 |
LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) { |
| 588 |
DnsResourceRecord *zz; |
| 589 |
DnsQuestion *question; |
| 590 |
|
| 591 |
if (aux->state != DNS_TRANSACTION_SUCCESS) |
| 592 |
continue; |
| 593 |
if (aux->auxiliary_result != 0) |
| 594 |
continue; |
| 595 |
|
| 596 |
question = dns_query_question_for_protocol(aux, aux->answer_protocol); |
| 597 |
|
| 598 |
r = dns_name_equal(dns_question_first_name(question), rr->srv.name); |
| 599 |
if (r < 0) |
| 600 |
return r; |
| 601 |
if (r == 0) |
| 602 |
continue; |
| 603 |
|
| 604 |
DNS_ANSWER_FOREACH(zz, aux->answer) { |
| 605 |
r = dns_question_matches_rr(question, zz, NULL); |
| 606 |
if (r < 0) |
| 607 |
return r; |
| 608 |
if (r == 0) |
| 609 |
continue; |
| 610 |
|
| 611 |
canonical = dns_resource_record_ref(zz); |
| 612 |
break; |
| 613 |
} |
| 614 |
|
| 615 |
if (canonical) |
| 616 |
break; |
| 617 |
} |
| 618 |
|
| 619 |
/* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */ |
| 620 |
if (!canonical) |
| 621 |
return 0; |
| 622 |
} |
| 623 |
|
| 624 |
r = dns_name_normalize(rr->srv.name, 0, &normalized); |
| 625 |
if (r < 0) |
| 626 |
return r; |
| 627 |
|
| 628 |
r = sd_json_buildo( |
| 629 |
&v, |
| 630 |
SD_JSON_BUILD_PAIR_UNSIGNED("priority", rr->srv.priority), |
| 631 |
SD_JSON_BUILD_PAIR_UNSIGNED("weight", rr->srv.weight), |
| 632 |
SD_JSON_BUILD_PAIR_UNSIGNED("port", rr->srv.port), |
| 633 |
SD_JSON_BUILD_PAIR_STRING("hostname", normalized)); |
| 634 |
if (r < 0) |
| 635 |
return r; |
| 636 |
|
| 637 |
if (canonical) { |
| 638 |
normalized = mfree(normalized); |
| 639 |
|
| 640 |
r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized); |
| 641 |
if (r < 0) |
| 642 |
return r; |
| 643 |
|
| 644 |
r = sd_json_variant_set_field_string(&v, "canonicalName", normalized); |
| 645 |
if (r < 0) |
| 646 |
return r; |
| 647 |
} |
| 648 |
|
| 649 |
if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) { |
| 650 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *addresses = NULL; |
| 651 |
|
| 652 |
LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) { |
| 653 |
DnsQuestion *question; |
| 654 |
|
| 655 |
if (aux->state != DNS_TRANSACTION_SUCCESS) |
| 656 |
continue; |
| 657 |
if (aux->auxiliary_result != 0) |
| 658 |
continue; |
| 659 |
|
| 660 |
question = dns_query_question_for_protocol(aux, aux->answer_protocol); |
| 661 |
|
| 662 |
r = dns_name_equal(dns_question_first_name(question), rr->srv.name); |
| 663 |
if (r < 0) |
| 664 |
return r; |
| 665 |
if (r == 0) |
| 666 |
continue; |
| 667 |
|
| 668 |
r = find_addr_records(&addresses, question, aux, NULL, NULL); |
| 669 |
if (r < 0) |
| 670 |
return r; |
| 671 |
} |
| 672 |
|
| 673 |
r = sd_json_variant_set_field(&v, "addresses", addresses); |
| 674 |
if (r < 0) |
| 675 |
return r; |
| 676 |
} |
| 677 |
|
| 678 |
r = sd_json_variant_append_array(array, v); |
| 679 |
if (r < 0) |
| 680 |
return r; |
| 681 |
|
| 682 |
return 1; /* added */ |
| 683 |
} |
| 684 |
|
| 685 |
static sd_varlink* take_vl_link_aux_query(DnsQuery *aux) { |
| 686 |
assert(aux); |
| 687 |
|
| 688 |
/* Find the main query */ |
| 689 |
while (aux->auxiliary_for) |
| 690 |
aux = aux->auxiliary_for; |
| 691 |
|
| 692 |
return TAKE_PTR(aux->varlink_request); |
| 693 |
} |
| 694 |
|
| 695 |
static void resolve_service_all_complete(DnsQuery *query) { |
| 696 |
_cleanup_(dns_query_freep) DnsQuery *q = query; |
| 697 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *srv = NULL, *txt = NULL; |
| 698 |
_cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL; |
| 699 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL; |
| 700 |
DnsQuestion *question; |
| 701 |
DnsResourceRecord *rr; |
| 702 |
int r; |
| 703 |
|
| 704 |
assert(q); |
| 705 |
|
| 706 |
if (q->hook_query || q->block_all_complete > 0) { |
| 707 |
TAKE_PTR(q); |
| 708 |
return; |
| 709 |
} |
| 710 |
|
| 711 |
if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) { |
| 712 |
DnsQuery *bad = NULL; |
| 713 |
bool have_success = false; |
| 714 |
|
| 715 |
LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) { |
| 716 |
|
| 717 |
if (aux->hook_query) { |
| 718 |
/* If an auxiliary query's hook is still pending, let's wait */ |
| 719 |
TAKE_PTR(q); |
| 720 |
return; |
| 721 |
} |
| 722 |
|
| 723 |
switch (aux->state) { |
| 724 |
|
| 725 |
case DNS_TRANSACTION_PENDING: |
| 726 |
/* If an auxiliary query is still pending, let's wait */ |
| 727 |
TAKE_PTR(q); |
| 728 |
return; |
| 729 |
|
| 730 |
case DNS_TRANSACTION_SUCCESS: |
| 731 |
if (aux->auxiliary_result == 0) |
| 732 |
have_success = true; |
| 733 |
else |
| 734 |
bad = aux; |
| 735 |
break; |
| 736 |
|
| 737 |
default: |
| 738 |
bad = aux; |
| 739 |
} |
| 740 |
} |
| 741 |
if (!have_success) { |
| 742 |
/* We can only return one error, hence pick the last error we encountered */ |
| 743 |
|
| 744 |
assert(bad); |
| 745 |
if (bad->state == DNS_TRANSACTION_SUCCESS) { |
| 746 |
assert(bad->auxiliary_result != 0); |
| 747 |
|
| 748 |
if (bad->auxiliary_result == -ELOOP) |
| 749 |
return (void) sd_varlink_error(query->varlink_request, "io.systemd.Resolve.CNAMELoop", NULL); |
| 750 |
|
| 751 |
assert(bad->auxiliary_result < 0); |
| 752 |
r = bad->auxiliary_result; |
| 753 |
goto finish; |
| 754 |
} |
| 755 |
|
| 756 |
bad->varlink_request = take_vl_link_aux_query(bad); |
| 757 |
return (void) reply_query_state(bad); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
question = dns_query_question_for_protocol(q, q->answer_protocol); |
| 762 |
|
| 763 |
DNS_ANSWER_FOREACH(rr, q->answer) { |
| 764 |
r = dns_question_matches_rr(question, rr, NULL); |
| 765 |
if (r < 0) |
| 766 |
goto finish; |
| 767 |
if (r == 0) |
| 768 |
continue; |
| 769 |
|
| 770 |
r = append_srv(q, rr, &srv); |
| 771 |
if (r < 0) |
| 772 |
goto finish; |
| 773 |
if (r == 0) /* not an SRV record */ |
| 774 |
continue; |
| 775 |
|
| 776 |
if (!canonical) |
| 777 |
canonical = dns_resource_record_ref(rr); |
| 778 |
} |
| 779 |
|
| 780 |
if (sd_json_variant_is_blank_object(srv)) |
| 781 |
return (void) sd_varlink_error(query->varlink_request, "io.systemd.Resolve.NoSuchResourceRecord", NULL); |
| 782 |
|
| 783 |
DNS_ANSWER_FOREACH(rr, q->answer) { |
| 784 |
r = dns_question_matches_rr(question, rr, NULL); |
| 785 |
if (r < 0) |
| 786 |
goto finish; |
| 787 |
if (r == 0) |
| 788 |
continue; |
| 789 |
|
| 790 |
if (rr->key->type != DNS_TYPE_TXT) |
| 791 |
continue; |
| 792 |
|
| 793 |
r = append_txt(&txt, rr); |
| 794 |
if (r < 0) |
| 795 |
goto finish; |
| 796 |
} |
| 797 |
|
| 798 |
assert(canonical); |
| 799 |
r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain); |
| 800 |
if (r < 0) |
| 801 |
goto finish; |
| 802 |
|
| 803 |
if (isempty(type)) |
| 804 |
return (void) sd_varlink_error(q->varlink_request, "io.systemd.Resolve.InconsistentServiceRecords", NULL); |
| 805 |
|
| 806 |
r = sd_varlink_replybo( |
| 807 |
query->varlink_request, |
| 808 |
SD_JSON_BUILD_PAIR_VARIANT("services", srv), |
| 809 |
SD_JSON_BUILD_PAIR_CONDITION(!sd_json_variant_is_blank_object(txt), "txt", SD_JSON_BUILD_VARIANT(txt)), |
| 810 |
SD_JSON_BUILD_PAIR("canonical", SD_JSON_BUILD_OBJECT( |
| 811 |
SD_JSON_BUILD_PAIR_STRING("name", name), |
| 812 |
SD_JSON_BUILD_PAIR_STRING("type", type), |
| 813 |
SD_JSON_BUILD_PAIR_STRING("domain", domain))), |
| 814 |
SD_JSON_BUILD_PAIR_UNSIGNED("flags", dns_query_reply_flags_make(query))); |
| 815 |
|
| 816 |
finish: |
| 817 |
if (r < 0) { |
| 818 |
log_error_errno(r, "Failed to resolve service: %m"); |
| 819 |
(void) sd_varlink_error_errno(q->varlink_request, r); |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
static void resolve_service_hostname_complete(DnsQuery *q) { |
| 824 |
int r; |
| 825 |
|
| 826 |
assert(q); |
| 827 |
assert(q->auxiliary_for); |
| 828 |
|
| 829 |
if (q->state != DNS_TRANSACTION_SUCCESS) { |
| 830 |
resolve_service_all_complete(q->auxiliary_for); |
| 831 |
return; |
| 832 |
} |
| 833 |
|
| 834 |
r = dns_query_process_cname_many(q); |
| 835 |
if (r == DNS_QUERY_CNAME) /* This was a cname, and the query was restarted. */ |
| 836 |
return; |
| 837 |
|
| 838 |
/* This auxiliary lookup is finished or failed, let's see if all are finished now. */ |
| 839 |
q->auxiliary_result = r < 0 ? r : 0; |
| 840 |
resolve_service_all_complete(q->auxiliary_for); |
| 841 |
} |
| 842 |
|
| 843 |
static int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) { |
| 844 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 845 |
_cleanup_(dns_query_freep) DnsQuery *aux = NULL; |
| 846 |
int r; |
| 847 |
|
| 848 |
assert(q); |
| 849 |
assert(rr); |
| 850 |
assert(rr->key); |
| 851 |
assert(rr->key->type == DNS_TYPE_SRV); |
| 852 |
|
| 853 |
/* OK, we found an SRV record for the service. Let's resolve |
| 854 |
* the hostname included in it */ |
| 855 |
|
| 856 |
r = dns_question_new_address(&question, q->request_family, rr->srv.name, false); |
| 857 |
if (r < 0) |
| 858 |
return r; |
| 859 |
|
| 860 |
r = dns_query_new_for_varlink(q->manager, &aux, question, question, NULL, ifindex, q->flags|SD_RESOLVED_NO_SEARCH, q->varlink_request); |
| 861 |
if (r < 0) |
| 862 |
return r; |
| 863 |
|
| 864 |
aux->request_family = q->request_family; |
| 865 |
aux->complete = resolve_service_hostname_complete; |
| 866 |
|
| 867 |
r = dns_query_make_auxiliary(aux, q); |
| 868 |
if (r == -EAGAIN) |
| 869 |
/* Too many auxiliary lookups? If so, don't complain, |
| 870 |
* let's just not add this one, we already have more |
| 871 |
* than enough */ |
| 872 |
return 0; |
| 873 |
if (r < 0) |
| 874 |
return r; |
| 875 |
|
| 876 |
/* Note that auxiliary queries do not track the original |
| 877 |
* client, only the primary request does that. */ |
| 878 |
|
| 879 |
r = dns_query_go(aux); |
| 880 |
if (r < 0) |
| 881 |
return r; |
| 882 |
|
| 883 |
TAKE_PTR(aux); |
| 884 |
return 1; |
| 885 |
} |
| 886 |
|
| 887 |
static void vl_method_resolve_service_complete(DnsQuery *query) { |
| 888 |
_cleanup_(dns_query_freep) DnsQuery *q = query; |
| 889 |
bool has_root_domain = false; |
| 890 |
DnsResourceRecord *rr; |
| 891 |
DnsQuestion *question; |
| 892 |
unsigned found = 0; |
| 893 |
int ifindex, r; |
| 894 |
|
| 895 |
assert(q); |
| 896 |
|
| 897 |
if (q->state != DNS_TRANSACTION_SUCCESS) |
| 898 |
return (void) reply_query_state(q); |
| 899 |
|
| 900 |
r = dns_query_process_cname_many(q); |
| 901 |
if (r == -ELOOP) |
| 902 |
return (void) sd_varlink_error(q->varlink_request, "io.systemd.Resolve.CNAMELoop", NULL); |
| 903 |
if (r < 0) |
| 904 |
goto fail; |
| 905 |
if (r == DNS_QUERY_CNAME) { |
| 906 |
/* This was a cname, and the query was restarted. */ |
| 907 |
TAKE_PTR(q); |
| 908 |
return; |
| 909 |
} |
| 910 |
|
| 911 |
question = dns_query_question_for_protocol(q, q->answer_protocol); |
| 912 |
|
| 913 |
DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) { |
| 914 |
r = dns_question_matches_rr(question, rr, NULL); |
| 915 |
if (r < 0) |
| 916 |
goto fail; |
| 917 |
if (r == 0) |
| 918 |
continue; |
| 919 |
|
| 920 |
if (rr->key->type != DNS_TYPE_SRV) |
| 921 |
continue; |
| 922 |
|
| 923 |
if (dns_name_is_root(rr->srv.name)) { |
| 924 |
has_root_domain = true; |
| 925 |
continue; |
| 926 |
} |
| 927 |
|
| 928 |
if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) { |
| 929 |
q->block_all_complete++; |
| 930 |
r = resolve_service_hostname(q, rr, ifindex); |
| 931 |
q->block_all_complete--; |
| 932 |
|
| 933 |
if (r < 0) |
| 934 |
goto fail; |
| 935 |
} |
| 936 |
|
| 937 |
found++; |
| 938 |
} |
| 939 |
|
| 940 |
if (has_root_domain && found <= 0) |
| 941 |
/* If there's exactly one SRV RR and it uses the root domain as hostname, then the service is |
| 942 |
* explicitly not offered on the domain. Report this as a recognizable error. See RFC 2782, |
| 943 |
* Section "Usage Rules". */ |
| 944 |
return (void) sd_varlink_error(q->varlink_request, "io.systemd.Resolve.ServiceNotProvided", NULL); |
| 945 |
|
| 946 |
if (found <= 0) |
| 947 |
return (void) sd_varlink_error(q->varlink_request, "io.systemd.Resolve.NoSuchResourceRecord", NULL); |
| 948 |
|
| 949 |
/* Maybe we are already finished? check now... */ |
| 950 |
resolve_service_all_complete(TAKE_PTR(q)); |
| 951 |
return; |
| 952 |
|
| 953 |
fail: |
| 954 |
log_error_errno(r, "Failed to send address reply: %m"); |
| 955 |
(void) sd_varlink_error_errno(q->varlink_request, r); |
| 956 |
} |
| 957 |
|
| 958 |
static int vl_method_resolve_service(sd_varlink* link, sd_json_variant* parameters, sd_varlink_method_flags_t flags, void* userdata) { |
| 959 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 960 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParametersResolveService, name), 0 }, |
| 961 |
{ "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParametersResolveService, type), 0 }, |
| 962 |
{ "domain", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParametersResolveService, domain), SD_JSON_MANDATORY }, |
| 963 |
{ "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(LookupParametersResolveService, ifindex), SD_JSON_RELAX }, |
| 964 |
{ "family", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParametersResolveService, family), 0 }, |
| 965 |
{ "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LookupParametersResolveService, flags), 0 }, |
| 966 |
{} |
| 967 |
}; |
| 968 |
|
| 969 |
_cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL; |
| 970 |
LookupParametersResolveService p = { |
| 971 |
.family = AF_UNSPEC, |
| 972 |
}; |
| 973 |
|
| 974 |
_cleanup_(dns_query_freep) DnsQuery *q = NULL; |
| 975 |
Manager *m; |
| 976 |
int r; |
| 977 |
|
| 978 |
assert(link); |
| 979 |
|
| 980 |
m = sd_varlink_server_get_userdata(sd_varlink_get_server(link)); |
| 981 |
assert(m); |
| 982 |
|
| 983 |
if (FLAGS_SET(flags, SD_VARLINK_METHOD_ONEWAY)) |
| 984 |
return -EINVAL; |
| 985 |
|
| 986 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 987 |
if (r != 0) |
| 988 |
return r; |
| 989 |
|
| 990 |
if (!IN_SET(p.family, AF_INET, AF_INET6, AF_UNSPEC)) |
| 991 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("family")); |
| 992 |
|
| 993 |
if (isempty(p.name)) |
| 994 |
p.name = NULL; |
| 995 |
else if (!dns_service_name_is_valid(p.name)) |
| 996 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("name")); |
| 997 |
|
| 998 |
if (isempty(p.type)) |
| 999 |
p.type = NULL; |
| 1000 |
else if (!dns_srv_type_is_valid(p.type)) |
| 1001 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("type")); |
| 1002 |
|
| 1003 |
r = dns_name_is_valid(p.domain); |
| 1004 |
if (r < 0) |
| 1005 |
return r; |
| 1006 |
if (r == 0) |
| 1007 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("domain")); |
| 1008 |
|
| 1009 |
if (p.name && !p.type) /* Service name cannot be specified without service type. */ |
| 1010 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("type")); |
| 1011 |
|
| 1012 |
if (validate_and_mangle_query_flags(m, &p.flags, p.name, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS) < 0) |
| 1013 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("flags")); |
| 1014 |
|
| 1015 |
/* Refuse the method if SRV is filtered. */ |
| 1016 |
if (set_contains(m->refuse_record_types, INT_TO_PTR(DNS_TYPE_SRV))) |
| 1017 |
return sd_varlink_error(link, "io.systemd.Resolve.QueryRefused", NULL); |
| 1018 |
|
| 1019 |
r = dns_question_new_service(&question_utf8, p.name, p.type, p.domain, !(p.flags & SD_RESOLVED_NO_TXT), false); |
| 1020 |
if (r < 0) |
| 1021 |
return r; |
| 1022 |
|
| 1023 |
r = dns_question_new_service(&question_idna, p.name, p.type, p.domain, !(p.flags & SD_RESOLVED_NO_TXT), true); |
| 1024 |
if (r < 0) |
| 1025 |
return r; |
| 1026 |
|
| 1027 |
r = dns_query_new_for_varlink(m, &q, question_utf8, question_idna, NULL, p.ifindex, p.flags|SD_RESOLVED_NO_SEARCH, link); |
| 1028 |
if (r < 0) |
| 1029 |
return r; |
| 1030 |
|
| 1031 |
q->varlink_request = sd_varlink_ref(link); |
| 1032 |
q->request_family = p.family; |
| 1033 |
q->complete = vl_method_resolve_service_complete; |
| 1034 |
|
| 1035 |
sd_varlink_set_userdata(link, q); |
| 1036 |
|
| 1037 |
r = dns_query_go(q); |
| 1038 |
if (r < 0) |
| 1039 |
return r; |
| 1040 |
|
| 1041 |
TAKE_PTR(q); |
| 1042 |
return 1; |
| 1043 |
} |
| 1044 |
|
| 1045 |
static void vl_method_resolve_record_complete(DnsQuery *query) { |
| 1046 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; |
| 1047 |
_cleanup_(dns_query_freep) DnsQuery *q = query; |
| 1048 |
DnsQuestion *question; |
| 1049 |
int r; |
| 1050 |
|
| 1051 |
assert(q); |
| 1052 |
|
| 1053 |
if (q->state != DNS_TRANSACTION_SUCCESS) |
| 1054 |
return (void) reply_query_state(q); |
| 1055 |
|
| 1056 |
r = dns_query_process_cname_many(q); |
| 1057 |
if (r == -ELOOP) |
| 1058 |
return (void) sd_varlink_error(q->varlink_request, "io.systemd.Resolve.CNAMELoop", NULL); |
| 1059 |
if (r < 0) |
| 1060 |
goto finish; |
| 1061 |
if (r == DNS_QUERY_CNAME) { |
| 1062 |
/* This was a cname, and the query was restarted. */ |
| 1063 |
TAKE_PTR(q); |
| 1064 |
return; |
| 1065 |
} |
| 1066 |
|
| 1067 |
question = dns_query_question_for_protocol(q, q->answer_protocol); |
| 1068 |
|
| 1069 |
unsigned added = 0; |
| 1070 |
int ifindex; |
| 1071 |
DnsResourceRecord *rr; |
| 1072 |
DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) { |
| 1073 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 1074 |
|
| 1075 |
r = dns_question_matches_rr(question, rr, NULL); |
| 1076 |
if (r < 0) |
| 1077 |
goto finish; |
| 1078 |
if (r == 0) |
| 1079 |
continue; |
| 1080 |
|
| 1081 |
r = dns_resource_record_to_json(rr, &v); |
| 1082 |
if (r < 0) |
| 1083 |
goto finish; |
| 1084 |
|
| 1085 |
r = dns_resource_record_to_wire_format(rr, /* canonical= */ false); /* don't use DNSSEC canonical format, since it removes casing, but we want that for DNS_SD compat */ |
| 1086 |
if (r < 0) |
| 1087 |
goto finish; |
| 1088 |
|
| 1089 |
r = sd_json_variant_append_arraybo( |
| 1090 |
&array, |
| 1091 |
SD_JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", SD_JSON_BUILD_INTEGER(ifindex)), |
| 1092 |
SD_JSON_BUILD_PAIR_CONDITION(!!v, "rr", SD_JSON_BUILD_VARIANT(v)), |
| 1093 |
SD_JSON_BUILD_PAIR_BASE64("raw", rr->wire_format, rr->wire_format_size)); |
| 1094 |
if (r < 0) |
| 1095 |
goto finish; |
| 1096 |
|
| 1097 |
added++; |
| 1098 |
} |
| 1099 |
|
| 1100 |
if (added <= 0) |
| 1101 |
return (void) sd_varlink_error(q->varlink_request, "io.systemd.Resolve.NoSuchResourceRecord", NULL); |
| 1102 |
|
| 1103 |
r = sd_varlink_replybo( |
| 1104 |
q->varlink_request, |
| 1105 |
SD_JSON_BUILD_PAIR_VARIANT("rrs", array), |
| 1106 |
SD_JSON_BUILD_PAIR_INTEGER("flags", dns_query_reply_flags_make(q))); |
| 1107 |
finish: |
| 1108 |
if (r < 0) { |
| 1109 |
log_full_errno(ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_ERR, r, "Failed to send record reply: %m"); |
| 1110 |
(void) sd_varlink_error_errno(q->varlink_request, r); |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
static int vl_method_resolve_record(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1115 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 1116 |
{ "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(LookupParameters, ifindex), SD_JSON_RELAX }, |
| 1117 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, name), SD_JSON_MANDATORY }, |
| 1118 |
{ "class", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(LookupParameters, class), 0 }, |
| 1119 |
{ "type", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(LookupParameters, type), SD_JSON_MANDATORY }, |
| 1120 |
{ "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LookupParameters, flags), 0 }, |
| 1121 |
{} |
| 1122 |
}; |
| 1123 |
|
| 1124 |
_cleanup_(lookup_parameters_destroy) LookupParameters p = { |
| 1125 |
.class = DNS_CLASS_IN, |
| 1126 |
.type = _DNS_TYPE_INVALID, |
| 1127 |
}; |
| 1128 |
_cleanup_(dns_query_freep) DnsQuery *q = NULL; |
| 1129 |
Manager *m; |
| 1130 |
int r; |
| 1131 |
|
| 1132 |
assert(link); |
| 1133 |
|
| 1134 |
m = ASSERT_PTR(sd_varlink_server_get_userdata(sd_varlink_get_server(link))); |
| 1135 |
|
| 1136 |
if (FLAGS_SET(flags, SD_VARLINK_METHOD_ONEWAY)) |
| 1137 |
return -EINVAL; |
| 1138 |
|
| 1139 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 1140 |
if (r != 0) |
| 1141 |
return r; |
| 1142 |
|
| 1143 |
r = dns_name_is_valid(p.name); |
| 1144 |
if (r < 0) |
| 1145 |
return r; |
| 1146 |
if (r == 0) |
| 1147 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("name")); |
| 1148 |
|
| 1149 |
if (!dns_type_is_valid_query(p.type)) |
| 1150 |
return sd_varlink_error(link, "io.systemd.Resolve.ResourceRecordTypeInvalidForQuery", NULL); |
| 1151 |
if (dns_type_is_zone_transfer(p.type)) |
| 1152 |
return sd_varlink_error(link, "io.systemd.Resolve.ZoneTransfersNotPermitted", NULL); |
| 1153 |
if (dns_type_is_obsolete(p.type)) |
| 1154 |
return sd_varlink_error(link, "io.systemd.Resolve.ResourceRecordTypeObsolete", NULL); |
| 1155 |
|
| 1156 |
if (validate_and_mangle_query_flags(m, &p.flags, p.name, SD_RESOLVED_NO_SEARCH) < 0) |
| 1157 |
return sd_varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("flags")); |
| 1158 |
|
| 1159 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = dns_question_new(1); |
| 1160 |
if (!question) |
| 1161 |
return -ENOMEM; |
| 1162 |
|
| 1163 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 1164 |
key = dns_resource_key_new(p.class, p.type, p.name); |
| 1165 |
if (!key) |
| 1166 |
return -ENOMEM; |
| 1167 |
|
| 1168 |
r = dns_question_add(question, key, /* flags= */ 0); |
| 1169 |
if (r < 0) |
| 1170 |
return r; |
| 1171 |
|
| 1172 |
r = dns_query_new_for_varlink(m, &q, question, question, NULL, p.ifindex, p.flags|SD_RESOLVED_NO_SEARCH|SD_RESOLVED_CLAMP_TTL, link); |
| 1173 |
if (r < 0) |
| 1174 |
return r; |
| 1175 |
|
| 1176 |
q->varlink_request = sd_varlink_ref(link); |
| 1177 |
sd_varlink_set_userdata(link, q); |
| 1178 |
q->complete = vl_method_resolve_record_complete; |
| 1179 |
|
| 1180 |
r = dns_query_go(q); |
| 1181 |
if (r < 0) |
| 1182 |
return r; |
| 1183 |
|
| 1184 |
TAKE_PTR(q); |
| 1185 |
return 1; |
| 1186 |
} |
| 1187 |
|
| 1188 |
static int verify_polkit(sd_varlink *link, sd_json_variant *parameters, const char *action) { |
| 1189 |
int r; |
| 1190 |
Manager *m = ASSERT_PTR(sd_varlink_get_userdata(ASSERT_PTR(link))); |
| 1191 |
|
| 1192 |
assert(action); |
| 1193 |
|
| 1194 |
r = sd_varlink_dispatch(link, parameters, dispatch_table_polkit_only, /* userdata= */ NULL); |
| 1195 |
if (r != 0) |
| 1196 |
return r; |
| 1197 |
|
| 1198 |
return varlink_verify_polkit_async( |
| 1199 |
link, |
| 1200 |
m->bus, |
| 1201 |
action, |
| 1202 |
/* details= */ NULL, |
| 1203 |
&m->polkit_registry); |
| 1204 |
} |
| 1205 |
|
| 1206 |
static int vl_method_browse_services(sd_varlink* link, sd_json_variant* parameters, sd_varlink_method_flags_t flags, void* userdata) { |
| 1207 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 1208 |
{ "domain", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParamatersBrowseServices, domain), 0 }, |
| 1209 |
{ "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParamatersBrowseServices, type), 0 }, |
| 1210 |
{ "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParamatersBrowseServices, ifindex), 0 }, |
| 1211 |
{ "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LookupParamatersBrowseServices, flags), 0 }, |
| 1212 |
{} |
| 1213 |
}; |
| 1214 |
|
| 1215 |
LookupParamatersBrowseServices p = {}; |
| 1216 |
Manager *m; |
| 1217 |
int r = 0; |
| 1218 |
|
| 1219 |
assert(link); |
| 1220 |
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)); |
| 1221 |
|
| 1222 |
m = ASSERT_PTR(sd_varlink_server_get_userdata(sd_varlink_get_server(link))); |
| 1223 |
|
| 1224 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 1225 |
if (r != 0) |
| 1226 |
return r; |
| 1227 |
|
| 1228 |
if (validate_and_mangle_query_flags(m, &p.flags, /* name= */ NULL, /* ok= */ 0)) |
| 1229 |
return sd_varlink_error_invalid_parameter_name(link, "flags"); |
| 1230 |
|
| 1231 |
r = dns_subscribe_browse_service(m, link, p.domain, p.type, p.ifindex, p.flags); |
| 1232 |
if (r < 0) |
| 1233 |
return sd_varlink_error_errno(link, r); |
| 1234 |
|
| 1235 |
return 1; |
| 1236 |
} |
| 1237 |
|
| 1238 |
static int vl_method_subscribe_query_results(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1239 |
Manager *m = ASSERT_PTR(sd_varlink_get_userdata(ASSERT_PTR(link))); |
| 1240 |
int r; |
| 1241 |
|
| 1242 |
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)); |
| 1243 |
|
| 1244 |
r = verify_polkit(link, parameters, "org.freedesktop.resolve1.subscribe-query-results"); |
| 1245 |
if (r <= 0) |
| 1246 |
return r; |
| 1247 |
|
| 1248 |
/* Send a ready message to the connecting client, to indicate that we are now listinening, and all |
| 1249 |
* queries issued after the point the client sees this will also be reported to the client. */ |
| 1250 |
r = sd_varlink_notifybo(link, SD_JSON_BUILD_PAIR_BOOLEAN("ready", true)); |
| 1251 |
if (r < 0) |
| 1252 |
return log_error_errno(r, "Failed to report monitor to be established: %m"); |
| 1253 |
|
| 1254 |
r = set_ensure_put(&m->varlink_query_results_subscription, NULL, link); |
| 1255 |
if (r < 0) |
| 1256 |
return log_error_errno(r, "Failed to add subscription to set: %m"); |
| 1257 |
sd_varlink_ref(link); |
| 1258 |
|
| 1259 |
log_debug("%u clients now attached for query result varlink notifications", set_size(m->varlink_query_results_subscription)); |
| 1260 |
|
| 1261 |
return 1; |
| 1262 |
} |
| 1263 |
|
| 1264 |
static int vl_method_dump_cache(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1265 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *list = NULL; |
| 1266 |
Manager *m = ASSERT_PTR(sd_varlink_get_userdata(ASSERT_PTR(link))); |
| 1267 |
int r; |
| 1268 |
|
| 1269 |
assert(link); |
| 1270 |
|
| 1271 |
r = verify_polkit(link, parameters, "org.freedesktop.resolve1.dump-cache"); |
| 1272 |
if (r <= 0) |
| 1273 |
return r; |
| 1274 |
|
| 1275 |
LIST_FOREACH(scopes, s, m->dns_scopes) { |
| 1276 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; |
| 1277 |
|
| 1278 |
r = dns_scope_to_json(s, /* with_cache= */ true, &j); |
| 1279 |
if (r < 0) |
| 1280 |
return r; |
| 1281 |
|
| 1282 |
r = sd_json_variant_append_array(&list, j); |
| 1283 |
if (r < 0) |
| 1284 |
return r; |
| 1285 |
} |
| 1286 |
|
| 1287 |
if (!list) { |
| 1288 |
r = sd_json_variant_new_array(&list, NULL, 0); |
| 1289 |
if (r < 0) |
| 1290 |
return r; |
| 1291 |
} |
| 1292 |
|
| 1293 |
return sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_VARIANT("dump", list)); |
| 1294 |
} |
| 1295 |
|
| 1296 |
static int dns_server_dump_state_to_json_list(DnsServer *server, sd_json_variant **list) { |
| 1297 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; |
| 1298 |
int r; |
| 1299 |
|
| 1300 |
assert(list); |
| 1301 |
assert(server); |
| 1302 |
|
| 1303 |
r = dns_server_dump_state_to_json(server, &j); |
| 1304 |
if (r < 0) |
| 1305 |
return r; |
| 1306 |
|
| 1307 |
return sd_json_variant_append_array(list, j); |
| 1308 |
} |
| 1309 |
|
| 1310 |
static int vl_method_dump_server_state(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1311 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *list = NULL; |
| 1312 |
Manager *m = ASSERT_PTR(sd_varlink_get_userdata(ASSERT_PTR(link))); |
| 1313 |
Link *l; |
| 1314 |
int r; |
| 1315 |
|
| 1316 |
r = verify_polkit(link, parameters, "org.freedesktop.resolve1.dump-server-state"); |
| 1317 |
if (r <= 0) |
| 1318 |
return r; |
| 1319 |
|
| 1320 |
LIST_FOREACH(servers, server, m->dns_servers) { |
| 1321 |
r = dns_server_dump_state_to_json_list(server, &list); |
| 1322 |
if (r < 0) |
| 1323 |
return r; |
| 1324 |
} |
| 1325 |
|
| 1326 |
LIST_FOREACH(servers, server, m->fallback_dns_servers) { |
| 1327 |
r = dns_server_dump_state_to_json_list(server, &list); |
| 1328 |
if (r < 0) |
| 1329 |
return r; |
| 1330 |
} |
| 1331 |
|
| 1332 |
HASHMAP_FOREACH(l, m->links) |
| 1333 |
LIST_FOREACH(servers, server, l->dns_servers) { |
| 1334 |
r = dns_server_dump_state_to_json_list(server, &list); |
| 1335 |
if (r < 0) |
| 1336 |
return r; |
| 1337 |
} |
| 1338 |
|
| 1339 |
if (!list) { |
| 1340 |
r = sd_json_variant_new_array(&list, NULL, 0); |
| 1341 |
if (r < 0) |
| 1342 |
return r; |
| 1343 |
} |
| 1344 |
|
| 1345 |
return sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_VARIANT("dump", list)); |
| 1346 |
} |
| 1347 |
|
| 1348 |
static int vl_method_dump_statistics(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1349 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; |
| 1350 |
Manager *m = ASSERT_PTR(sd_varlink_get_userdata(ASSERT_PTR(link))); |
| 1351 |
int r; |
| 1352 |
|
| 1353 |
assert(link); |
| 1354 |
|
| 1355 |
r = verify_polkit(link, parameters, "org.freedesktop.resolve1.dump-statistics"); |
| 1356 |
if (r <= 0) |
| 1357 |
return r; |
| 1358 |
|
| 1359 |
r = dns_manager_dump_statistics_json(m, &j); |
| 1360 |
if (r < 0) |
| 1361 |
return r; |
| 1362 |
|
| 1363 |
return sd_varlink_replyb(link, SD_JSON_BUILD_VARIANT(j)); |
| 1364 |
} |
| 1365 |
|
| 1366 |
static int vl_method_reset_statistics(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1367 |
Manager *m = ASSERT_PTR(sd_varlink_get_userdata(ASSERT_PTR(link))); |
| 1368 |
int r; |
| 1369 |
|
| 1370 |
r = verify_polkit(link, parameters, "org.freedesktop.resolve1.reset-statistics"); |
| 1371 |
if (r <= 0) |
| 1372 |
return r; |
| 1373 |
|
| 1374 |
dns_manager_reset_statistics(m); |
| 1375 |
|
| 1376 |
return sd_varlink_replyb(link, SD_JSON_BUILD_EMPTY_OBJECT); |
| 1377 |
} |
| 1378 |
|
| 1379 |
static int vl_method_subscribe_dns_configuration(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1380 |
Manager *m = ASSERT_PTR(sd_varlink_get_userdata(ASSERT_PTR(link))); |
| 1381 |
int r; |
| 1382 |
|
| 1383 |
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)); |
| 1384 |
|
| 1385 |
r = verify_polkit(link, parameters, "org.freedesktop.resolve1.subscribe-dns-configuration"); |
| 1386 |
if (r <= 0) |
| 1387 |
return r; |
| 1388 |
|
| 1389 |
if (set_isempty(m->varlink_dns_configuration_subscription)) { |
| 1390 |
r = manager_start_dns_configuration_monitor(m); |
| 1391 |
if (r < 0) |
| 1392 |
return log_error_errno(r, "Failed to start DNS configuration monitor: %m"); |
| 1393 |
} |
| 1394 |
|
| 1395 |
r = sd_varlink_notify(link, m->dns_configuration_json); |
| 1396 |
if (r < 0) |
| 1397 |
goto fail; |
| 1398 |
|
| 1399 |
r = set_ensure_put(&m->varlink_dns_configuration_subscription, NULL, link); |
| 1400 |
if (r < 0) |
| 1401 |
goto fail; |
| 1402 |
sd_varlink_ref(link); |
| 1403 |
|
| 1404 |
log_debug("%u clients now attached for link configuration varlink notifications", |
| 1405 |
set_size(m->varlink_dns_configuration_subscription)); |
| 1406 |
|
| 1407 |
return 1; |
| 1408 |
fail: |
| 1409 |
if (set_isempty(m->varlink_dns_configuration_subscription)) |
| 1410 |
manager_stop_dns_configuration_monitor(m); |
| 1411 |
|
| 1412 |
return log_debug_errno(r, "Failed to subscribe client to DNS configuration monitor: %m"); |
| 1413 |
} |
| 1414 |
|
| 1415 |
static int vl_method_dump_dns_configuration(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1416 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *configuration = NULL; |
| 1417 |
Manager *m; |
| 1418 |
Link *l; |
| 1419 |
int r; |
| 1420 |
|
| 1421 |
assert(link); |
| 1422 |
|
| 1423 |
m = ASSERT_PTR(sd_varlink_server_get_userdata(sd_varlink_get_server(link))); |
| 1424 |
|
| 1425 |
/* Make sure the accessible flag is not stale. */ |
| 1426 |
dns_server_reset_accessible_all(m->dns_servers); |
| 1427 |
|
| 1428 |
HASHMAP_FOREACH(l, m->links) |
| 1429 |
dns_server_reset_accessible_all(l->dns_servers); |
| 1430 |
|
| 1431 |
r = manager_dump_dns_configuration_json(m, &configuration); |
| 1432 |
if (r < 0) |
| 1433 |
return r; |
| 1434 |
|
| 1435 |
return sd_varlink_reply(link, configuration); |
| 1436 |
} |
| 1437 |
|
| 1438 |
static int varlink_monitor_server_init(Manager *m) { |
| 1439 |
_cleanup_(sd_varlink_server_unrefp) sd_varlink_server *server = NULL; |
| 1440 |
int r; |
| 1441 |
|
| 1442 |
assert(m); |
| 1443 |
|
| 1444 |
if (m->varlink_monitor_server) |
| 1445 |
return 0; |
| 1446 |
|
| 1447 |
r = varlink_server_new(&server, SD_VARLINK_SERVER_ACCOUNT_UID|SD_VARLINK_SERVER_INHERIT_USERDATA, m); |
| 1448 |
if (r < 0) |
| 1449 |
return log_error_errno(r, "Failed to allocate varlink server object: %m"); |
| 1450 |
|
| 1451 |
r = sd_varlink_server_add_interface(server, &vl_interface_io_systemd_Resolve_Monitor); |
| 1452 |
if (r < 0) |
| 1453 |
return log_error_errno(r, "Failed to add Resolve.Monitor interface to varlink server: %m"); |
| 1454 |
|
| 1455 |
r = sd_varlink_server_bind_method_many( |
| 1456 |
server, |
| 1457 |
"io.systemd.Resolve.Monitor.SubscribeQueryResults", vl_method_subscribe_query_results, |
| 1458 |
"io.systemd.Resolve.Monitor.DumpCache", vl_method_dump_cache, |
| 1459 |
"io.systemd.Resolve.Monitor.DumpServerState", vl_method_dump_server_state, |
| 1460 |
"io.systemd.Resolve.Monitor.DumpStatistics", vl_method_dump_statistics, |
| 1461 |
"io.systemd.Resolve.Monitor.ResetStatistics", vl_method_reset_statistics, |
| 1462 |
"io.systemd.Resolve.Monitor.SubscribeDNSConfiguration", vl_method_subscribe_dns_configuration); |
| 1463 |
if (r < 0) |
| 1464 |
return log_error_errno(r, "Failed to register varlink methods: %m"); |
| 1465 |
|
| 1466 |
r = sd_varlink_server_bind_disconnect(server, vl_on_notification_disconnect); |
| 1467 |
if (r < 0) |
| 1468 |
return log_error_errno(r, "Failed to register varlink disconnect handler: %m"); |
| 1469 |
|
| 1470 |
r = sd_varlink_server_listen_name(server, "varlink-monitor"); |
| 1471 |
if (r < 0) |
| 1472 |
return log_error_errno(r, "Failed to get varlink listen fd: %m"); |
| 1473 |
if (r == 0) { |
| 1474 |
r = sd_varlink_server_listen_address(server, "/run/systemd/resolve/io.systemd.Resolve.Monitor", 0666); |
| 1475 |
if (r < 0) |
| 1476 |
return log_error_errno(r, "Failed to bind to varlink socket '/run/systemd/resolve/io.systemd.Resolve.Monitor': %m"); |
| 1477 |
} |
| 1478 |
|
| 1479 |
r = sd_varlink_server_attach_event(server, m->event, SD_EVENT_PRIORITY_NORMAL); |
| 1480 |
if (r < 0) |
| 1481 |
return log_error_errno(r, "Failed to attach varlink connection to event loop: %m"); |
| 1482 |
|
| 1483 |
m->varlink_monitor_server = TAKE_PTR(server); |
| 1484 |
|
| 1485 |
return 0; |
| 1486 |
} |
| 1487 |
|
| 1488 |
static int varlink_main_server_init(Manager *m) { |
| 1489 |
_cleanup_(sd_varlink_server_unrefp) sd_varlink_server *s = NULL; |
| 1490 |
int r; |
| 1491 |
|
| 1492 |
assert(m); |
| 1493 |
|
| 1494 |
if (m->varlink_server) |
| 1495 |
return 0; |
| 1496 |
|
| 1497 |
r = varlink_server_new(&s, SD_VARLINK_SERVER_ACCOUNT_UID, m); |
| 1498 |
if (r < 0) |
| 1499 |
return log_error_errno(r, "Failed to allocate varlink server object: %m"); |
| 1500 |
|
| 1501 |
r = sd_varlink_server_add_interface_many( |
| 1502 |
s, |
| 1503 |
&vl_interface_io_systemd_Resolve, |
| 1504 |
&vl_interface_io_systemd_service); |
| 1505 |
if (r < 0) |
| 1506 |
return log_error_errno(r, "Failed to add Resolve interface to varlink server: %m"); |
| 1507 |
|
| 1508 |
r = sd_varlink_server_bind_method_many( |
| 1509 |
s, |
| 1510 |
"io.systemd.Resolve.ResolveHostname", vl_method_resolve_hostname, |
| 1511 |
"io.systemd.Resolve.ResolveAddress", vl_method_resolve_address, |
| 1512 |
"io.systemd.Resolve.ResolveService", vl_method_resolve_service, |
| 1513 |
"io.systemd.Resolve.ResolveRecord", vl_method_resolve_record, |
| 1514 |
"io.systemd.service.Ping", varlink_method_ping, |
| 1515 |
"io.systemd.service.SetLogLevel", varlink_method_set_log_level, |
| 1516 |
"io.systemd.service.GetEnvironment", varlink_method_get_environment, |
| 1517 |
"io.systemd.Resolve.BrowseServices", vl_method_browse_services, |
| 1518 |
"io.systemd.Resolve.DumpDNSConfiguration", vl_method_dump_dns_configuration); |
| 1519 |
if (r < 0) |
| 1520 |
return log_error_errno(r, "Failed to register varlink methods: %m"); |
| 1521 |
|
| 1522 |
r = sd_varlink_server_bind_disconnect(s, vl_on_disconnect); |
| 1523 |
if (r < 0) |
| 1524 |
return log_error_errno(r, "Failed to register varlink disconnect handler: %m"); |
| 1525 |
|
| 1526 |
r = sd_varlink_server_listen_auto(s); |
| 1527 |
if (r < 0) |
| 1528 |
return log_error_errno(r, "Failed to get varlink listen fd: %m"); |
| 1529 |
if (r == 0) { |
| 1530 |
r = sd_varlink_server_listen_address(s, "/run/systemd/resolve/io.systemd.Resolve", 0666); |
| 1531 |
if (r < 0) |
| 1532 |
return log_error_errno(r, "Failed to bind to varlink socket '/run/systemd/resolve/io.systemd.Resolve': %m"); |
| 1533 |
} |
| 1534 |
|
| 1535 |
r = sd_varlink_server_attach_event(s, m->event, SD_EVENT_PRIORITY_NORMAL); |
| 1536 |
if (r < 0) |
| 1537 |
return log_error_errno(r, "Failed to attach varlink connection to event loop: %m"); |
| 1538 |
|
| 1539 |
m->varlink_server = TAKE_PTR(s); |
| 1540 |
return 0; |
| 1541 |
} |
| 1542 |
|
| 1543 |
int manager_varlink_init(Manager *m) { |
| 1544 |
int r; |
| 1545 |
|
| 1546 |
r = varlink_main_server_init(m); |
| 1547 |
if (r < 0) |
| 1548 |
return r; |
| 1549 |
|
| 1550 |
r = varlink_monitor_server_init(m); |
| 1551 |
if (r < 0) |
| 1552 |
return r; |
| 1553 |
|
| 1554 |
return 0; |
| 1555 |
} |
| 1556 |
|
| 1557 |
void manager_varlink_done(Manager *m) { |
| 1558 |
assert(m); |
| 1559 |
|
| 1560 |
m->varlink_server = sd_varlink_server_unref(m->varlink_server); |
| 1561 |
m->varlink_monitor_server = sd_varlink_server_unref(m->varlink_monitor_server); |
| 1562 |
} |