| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "dns-answer.h" |
| 7 |
#include "dns-domain.h" |
| 8 |
#include "dns-packet.h" |
| 9 |
#include "dns-question.h" |
| 10 |
#include "dns-rr.h" |
| 11 |
#include "hashmap.h" |
| 12 |
#include "local-addresses.h" |
| 13 |
#include "log.h" |
| 14 |
#include "machine.h" |
| 15 |
#include "machined.h" |
| 16 |
#include "machined-resolve-hook.h" |
| 17 |
#include "resolve-hook-util.h" |
| 18 |
#include "set.h" |
| 19 |
#include "varlink-util.h" |
| 20 |
|
| 21 |
static int manager_make_machine_array(Manager *m, sd_json_variant **ret) { |
| 22 |
int r; |
| 23 |
|
| 24 |
assert(m); |
| 25 |
assert(ret); |
| 26 |
|
| 27 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; |
| 28 |
Machine *machine; |
| 29 |
HASHMAP_FOREACH(machine, m->machines) { |
| 30 |
if (machine->class == MACHINE_HOST) |
| 31 |
continue; |
| 32 |
if (!machine->started) |
| 33 |
continue; |
| 34 |
|
| 35 |
r = sd_json_variant_append_arrayb(&array, SD_JSON_BUILD_STRING(machine->name)); |
| 36 |
if (r < 0) |
| 37 |
return r; |
| 38 |
} |
| 39 |
|
| 40 |
if (!array) |
| 41 |
return sd_json_variant_new_array(ret, /* array= */ NULL, /* n= */ 0); |
| 42 |
|
| 43 |
*ret = TAKE_PTR(array); |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
int manager_notify_hook_filters(Manager *m) { |
| 48 |
int r; |
| 49 |
|
| 50 |
assert(m); |
| 51 |
|
| 52 |
/* Called whenever a machine is added or dropped from the list */ |
| 53 |
|
| 54 |
if (set_isempty(m->query_filter_subscriptions)) |
| 55 |
return 0; |
| 56 |
|
| 57 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; |
| 58 |
r = manager_make_machine_array(m, &array); |
| 59 |
if (r < 0) |
| 60 |
return log_error_errno(r, "Failed to generate JSON array with machine names: %m"); |
| 61 |
|
| 62 |
r = varlink_many_notifybo(m->query_filter_subscriptions, SD_JSON_BUILD_PAIR_VARIANT("filterDomains", array)); |
| 63 |
if (r < 0) |
| 64 |
return log_error_errno(r, "Failed to notify filter subscribers: %m"); |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
int vl_method_query_filter( |
| 70 |
sd_varlink *link, |
| 71 |
sd_json_variant *parameters, |
| 72 |
sd_varlink_method_flags_t flags, |
| 73 |
void *userdata) { |
| 74 |
|
| 75 |
Manager *m = ASSERT_PTR(userdata); |
| 76 |
int r; |
| 77 |
|
| 78 |
assert(link); |
| 79 |
|
| 80 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; |
| 81 |
r = manager_make_machine_array(m, &array); |
| 82 |
if (r < 0) |
| 83 |
return r; |
| 84 |
|
| 85 |
if (flags & SD_VARLINK_METHOD_MORE) { |
| 86 |
/* If 'more' is set, this is a subscription request, keep track of the link */ |
| 87 |
|
| 88 |
r = sd_varlink_notifybo(link, SD_JSON_BUILD_PAIR_VARIANT("filterDomains", array)); |
| 89 |
if (r < 0) |
| 90 |
return log_error_errno(r, "Failed to notify filter subscribers: %m"); |
| 91 |
|
| 92 |
r = set_ensure_put(&m->query_filter_subscriptions, &varlink_hash_ops, link); |
| 93 |
if (r < 0) |
| 94 |
return r; |
| 95 |
|
| 96 |
sd_varlink_ref(link); |
| 97 |
} else { |
| 98 |
r = sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_VARIANT("filterDomains", array)); |
| 99 |
if (r < 0) |
| 100 |
return log_error_errno(r, "Failed to notify filter subscribers: %m"); |
| 101 |
} |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
int vl_method_resolve_record( |
| 107 |
sd_varlink *link, |
| 108 |
sd_json_variant *parameters, |
| 109 |
sd_varlink_method_flags_t flags, |
| 110 |
void *userdata) { |
| 111 |
|
| 112 |
Manager *m = ASSERT_PTR(userdata); |
| 113 |
int r; |
| 114 |
|
| 115 |
assert(link); |
| 116 |
|
| 117 |
_cleanup_(resolve_record_parameters_done) ResolveRecordParameters p = {}; |
| 118 |
r = sd_varlink_dispatch(link, parameters, resolve_record_parameters_dispatch_table, &p); |
| 119 |
if (r != 0) |
| 120 |
return r; |
| 121 |
|
| 122 |
if (dns_question_isempty(p.question)) |
| 123 |
return sd_varlink_error_invalid_parameter_name(link, "question"); |
| 124 |
|
| 125 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 126 |
|
| 127 |
_cleanup_free_ struct local_address *addresses = NULL; |
| 128 |
bool found = false, nxdomain = false; |
| 129 |
int n_addresses = -1; |
| 130 |
|
| 131 |
DnsResourceKey *key; |
| 132 |
DNS_QUESTION_FOREACH(key, p.question) { |
| 133 |
Machine *machine = hashmap_get(m->machines, dns_resource_key_name(key)); |
| 134 |
if (machine) { |
| 135 |
/* We found a perfect match, yay! */ |
| 136 |
found = true; |
| 137 |
|
| 138 |
if (!dns_resource_key_is_address(key)) |
| 139 |
continue; |
| 140 |
|
| 141 |
if (n_addresses < 0) { |
| 142 |
n_addresses = machine_get_addresses(machine, &addresses); |
| 143 |
if (n_addresses < 0) |
| 144 |
return n_addresses; |
| 145 |
} |
| 146 |
|
| 147 |
int family = dns_type_to_af(key->type); |
| 148 |
FOREACH_ARRAY(address, addresses, n_addresses) { |
| 149 |
if (address->family != family) |
| 150 |
continue; |
| 151 |
|
| 152 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 153 |
r = dns_resource_record_new_address(&rr, address->family, &address->address, machine->name); |
| 154 |
if (r < 0) |
| 155 |
return r; |
| 156 |
|
| 157 |
r = dns_answer_add_extend( |
| 158 |
&answer, |
| 159 |
rr, |
| 160 |
machine->n_netif == 1 ? machine->netif[0] : -1, |
| 161 |
DNS_ANSWER_AUTHENTICATED, |
| 162 |
/* rrsig= */ NULL); |
| 163 |
if (r < 0) |
| 164 |
return r; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/* So this is not a direct match? Then check if we find a prefix match */ |
| 169 |
const char *q = dns_resource_key_name(key); |
| 170 |
while (!nxdomain) { |
| 171 |
r = dns_name_parent(&q); |
| 172 |
if (r < 0) |
| 173 |
return r; |
| 174 |
if (r == 0) |
| 175 |
break; |
| 176 |
|
| 177 |
nxdomain = hashmap_contains(m->machines, q); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if (!found) { |
| 182 |
/* If we found a prefix match we own the subtree, and thus return NXDOMAIN because we know |
| 183 |
* that we only expose the machine A/AAAA records on the primary name, but nothing below. */ |
| 184 |
if (nxdomain) |
| 185 |
return sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_INTEGER("rcode", DNS_RCODE_NXDOMAIN)); |
| 186 |
|
| 187 |
/* Otherwise we return an empty response, which means: continue with the usual lookup */ |
| 188 |
return sd_varlink_reply(link, /* parameters= */ NULL); |
| 189 |
} |
| 190 |
|
| 191 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *ja = NULL; |
| 192 |
r = dns_answer_to_json(answer, &ja); |
| 193 |
if (r < 0) |
| 194 |
return r; |
| 195 |
|
| 196 |
return sd_varlink_replybo( |
| 197 |
link, |
| 198 |
SD_JSON_BUILD_PAIR_INTEGER("rcode", DNS_RCODE_SUCCESS), |
| 199 |
SD_JSON_BUILD_PAIR_VARIANT("answer", ja)); |
| 200 |
} |