| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "af-list.h" |
| 6 |
#include "alloc-util.h" |
| 7 |
#include "dns-configuration.h" |
| 8 |
#include "hash-funcs.h" |
| 9 |
#include "in-addr-util.h" |
| 10 |
#include "iovec-util.h" |
| 11 |
#include "json-util.h" |
| 12 |
#include "ordered-set.h" |
| 13 |
#include "set.h" |
| 14 |
#include "string-util.h" |
| 15 |
#include "strv.h" |
| 16 |
|
| 17 |
DNSServer* dns_server_free(DNSServer *s) { |
| 18 |
if (!s) |
| 19 |
return NULL; |
| 20 |
|
| 21 |
free(s->server_name); |
| 22 |
iovec_done(&s->addr); |
| 23 |
|
| 24 |
return mfree(s); |
| 25 |
} |
| 26 |
|
| 27 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 28 |
dns_server_hash_ops, |
| 29 |
void, |
| 30 |
trivial_hash_func, |
| 31 |
trivial_compare_func, |
| 32 |
DNSServer, |
| 33 |
dns_server_free); |
| 34 |
|
| 35 |
static int dispatch_dns_server(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 36 |
static const sd_json_dispatch_field dns_server_dispatch_table[] = { |
| 37 |
{ "address", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(DNSServer, addr), SD_JSON_MANDATORY }, |
| 38 |
{ "addressString", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, |
| 39 |
{ "family", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint, offsetof(DNSServer, family), SD_JSON_MANDATORY }, |
| 40 |
{ "port", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint16, offsetof(DNSServer, port), 0 }, |
| 41 |
{ "ifindex", SD_JSON_VARIANT_UNSIGNED, json_dispatch_ifindex, offsetof(DNSServer, ifindex), SD_JSON_RELAX }, |
| 42 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSServer, server_name), 0 }, |
| 43 |
{ "accessible", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DNSServer, accessible), SD_JSON_MANDATORY }, |
| 44 |
{}, |
| 45 |
}; |
| 46 |
DNSServer **ret = ASSERT_PTR(userdata); |
| 47 |
_cleanup_(dns_server_freep) DNSServer *s = NULL; |
| 48 |
int r; |
| 49 |
|
| 50 |
s = new0(DNSServer, 1); |
| 51 |
if (!s) |
| 52 |
return log_oom(); |
| 53 |
|
| 54 |
r = sd_json_dispatch(variant, dns_server_dispatch_table, flags, s); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
|
| 58 |
if (s->addr.iov_len != FAMILY_ADDRESS_SIZE_SAFE(s->family)) |
| 59 |
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), |
| 60 |
"Dispatched address size (%zu) is incompatible with the family (%s).", |
| 61 |
s->addr.iov_len, af_to_ipv4_ipv6(s->family)); |
| 62 |
memcpy_safe(&s->in_addr, s->addr.iov_base, s->addr.iov_len); |
| 63 |
|
| 64 |
*ret = TAKE_PTR(s); |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
static int dispatch_dns_server_array(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 70 |
OrderedSet **ret = ASSERT_PTR(userdata); |
| 71 |
_cleanup_ordered_set_free_ OrderedSet *dns_servers = NULL; |
| 72 |
sd_json_variant *v; |
| 73 |
int r; |
| 74 |
|
| 75 |
JSON_VARIANT_ARRAY_FOREACH(v, variant) { |
| 76 |
_cleanup_(dns_server_freep) DNSServer *s = NULL; |
| 77 |
|
| 78 |
r = dispatch_dns_server(name, v, flags, &s); |
| 79 |
if (r < 0) |
| 80 |
return json_log(v, flags, r, "JSON array element is not a valid DNSServer."); |
| 81 |
|
| 82 |
r = ordered_set_ensure_put(&dns_servers, &dns_server_hash_ops, s); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
TAKE_PTR(s); |
| 86 |
} |
| 87 |
|
| 88 |
free_and_replace_full(*ret, dns_servers, ordered_set_free); |
| 89 |
|
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
SearchDomain* search_domain_free(SearchDomain *d) { |
| 94 |
if (!d) |
| 95 |
return NULL; |
| 96 |
|
| 97 |
free(d->name); |
| 98 |
|
| 99 |
return mfree(d); |
| 100 |
} |
| 101 |
|
| 102 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 103 |
search_domain_hash_ops, |
| 104 |
void, |
| 105 |
trivial_hash_func, |
| 106 |
trivial_compare_func, |
| 107 |
SearchDomain, |
| 108 |
search_domain_free); |
| 109 |
|
| 110 |
static int dispatch_search_domain(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 111 |
static const sd_json_dispatch_field search_domain_dispatch_table[] = { |
| 112 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(SearchDomain, name), SD_JSON_MANDATORY }, |
| 113 |
{ "routeOnly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(SearchDomain, route_only), SD_JSON_MANDATORY }, |
| 114 |
{ "ifindex", SD_JSON_VARIANT_UNSIGNED, json_dispatch_ifindex, offsetof(SearchDomain, ifindex), SD_JSON_RELAX }, |
| 115 |
{}, |
| 116 |
}; |
| 117 |
SearchDomain **ret = ASSERT_PTR(userdata); |
| 118 |
_cleanup_(search_domain_freep) SearchDomain *d = NULL; |
| 119 |
int r; |
| 120 |
|
| 121 |
d = new0(SearchDomain, 1); |
| 122 |
if (!d) |
| 123 |
return log_oom(); |
| 124 |
|
| 125 |
r = sd_json_dispatch(variant, search_domain_dispatch_table, flags, d); |
| 126 |
if (r < 0) |
| 127 |
return r; |
| 128 |
|
| 129 |
*ret = TAKE_PTR(d); |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
static int dispatch_search_domain_array(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 135 |
OrderedSet **ret = ASSERT_PTR(userdata); |
| 136 |
_cleanup_ordered_set_free_ OrderedSet *search_domains = NULL; |
| 137 |
sd_json_variant *v; |
| 138 |
int r; |
| 139 |
|
| 140 |
JSON_VARIANT_ARRAY_FOREACH(v, variant) { |
| 141 |
_cleanup_(search_domain_freep) SearchDomain *d = NULL; |
| 142 |
|
| 143 |
r = dispatch_search_domain(name, v, flags, &d); |
| 144 |
if (r < 0) |
| 145 |
return json_log(v, flags, r, "JSON array element is not a valid SearchDomain."); |
| 146 |
|
| 147 |
r = ordered_set_ensure_put(&search_domains, &search_domain_hash_ops, d); |
| 148 |
if (r < 0) |
| 149 |
return r; |
| 150 |
TAKE_PTR(d); |
| 151 |
} |
| 152 |
|
| 153 |
free_and_replace_full(*ret, search_domains, ordered_set_free); |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
DNSScope* dns_scope_free(DNSScope *s) { |
| 159 |
if (!s) |
| 160 |
return NULL; |
| 161 |
|
| 162 |
free(s->ifname); |
| 163 |
free(s->protocol); |
| 164 |
free(s->dnssec_mode_str); |
| 165 |
free(s->dns_over_tls_mode_str); |
| 166 |
|
| 167 |
return mfree(s); |
| 168 |
} |
| 169 |
|
| 170 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 171 |
dns_scope_hash_ops, |
| 172 |
void, |
| 173 |
trivial_hash_func, |
| 174 |
trivial_compare_func, |
| 175 |
DNSScope, |
| 176 |
dns_scope_free); |
| 177 |
|
| 178 |
static int dispatch_dns_scope(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 179 |
static const sd_json_dispatch_field dns_scope_dispatch_table[] = { |
| 180 |
{ "protocol", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSScope, protocol), SD_JSON_MANDATORY }, |
| 181 |
{ "family", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint, offsetof(DNSScope, family), 0 }, |
| 182 |
{ "ifname", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSScope, ifname), 0 }, |
| 183 |
{ "ifindex", SD_JSON_VARIANT_UNSIGNED, json_dispatch_ifindex, offsetof(DNSScope, ifindex), SD_JSON_RELAX }, |
| 184 |
{ "dnssec", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSScope, dnssec_mode_str), 0 }, |
| 185 |
{ "dnsOverTLS", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSScope, dns_over_tls_mode_str), 0 }, |
| 186 |
{}, |
| 187 |
}; |
| 188 |
DNSScope **ret = ASSERT_PTR(userdata); |
| 189 |
int r; |
| 190 |
|
| 191 |
_cleanup_(dns_scope_freep) DNSScope *s = new0(DNSScope, 1); |
| 192 |
if (!s) |
| 193 |
return log_oom(); |
| 194 |
|
| 195 |
r = sd_json_dispatch(variant, dns_scope_dispatch_table, flags, s); |
| 196 |
if (r < 0) |
| 197 |
return r; |
| 198 |
|
| 199 |
*ret = TAKE_PTR(s); |
| 200 |
|
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
static int dispatch_dns_scope_array(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 205 |
Set **ret = ASSERT_PTR(userdata); |
| 206 |
_cleanup_set_free_ Set *dns_scopes = NULL; |
| 207 |
sd_json_variant *v; |
| 208 |
int r; |
| 209 |
|
| 210 |
JSON_VARIANT_ARRAY_FOREACH(v, variant) { |
| 211 |
_cleanup_(dns_scope_freep) DNSScope *s = NULL; |
| 212 |
|
| 213 |
r = dispatch_dns_scope(name, v, flags, &s); |
| 214 |
if (r < 0) |
| 215 |
return json_log(v, flags, r, "JSON array element is not a valid DNSScope."); |
| 216 |
|
| 217 |
r = set_ensure_consume(&dns_scopes, &dns_scope_hash_ops, TAKE_PTR(s)); |
| 218 |
if (r < 0) |
| 219 |
return r; |
| 220 |
} |
| 221 |
|
| 222 |
set_free_and_replace(*ret, dns_scopes); |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
DNSConfiguration* dns_configuration_free(DNSConfiguration *c) { |
| 228 |
if (!c) |
| 229 |
return NULL; |
| 230 |
|
| 231 |
dns_server_free(c->current_dns_server); |
| 232 |
ordered_set_free(c->dns_servers); |
| 233 |
ordered_set_free(c->search_domains); |
| 234 |
ordered_set_free(c->fallback_dns_servers); |
| 235 |
set_free(c->dns_scopes); |
| 236 |
free(c->ifname); |
| 237 |
free(c->dnssec_mode_str); |
| 238 |
free(c->dns_over_tls_mode_str); |
| 239 |
free(c->llmnr_mode_str); |
| 240 |
free(c->mdns_mode_str); |
| 241 |
free(c->resolv_conf_mode_str); |
| 242 |
free(c->delegate); |
| 243 |
strv_free(c->negative_trust_anchors); |
| 244 |
|
| 245 |
return mfree(c); |
| 246 |
} |
| 247 |
|
| 248 |
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 249 |
dns_configuration_hash_ops, |
| 250 |
void, |
| 251 |
trivial_hash_func, |
| 252 |
trivial_compare_func, |
| 253 |
DNSConfiguration, |
| 254 |
dns_configuration_free); |
| 255 |
|
| 256 |
static int dispatch_dns_configuration(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 257 |
static const sd_json_dispatch_field dns_configuration_dispatch_table[] = { |
| 258 |
{ "ifname", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSConfiguration, ifname), 0 }, |
| 259 |
{ "ifindex", SD_JSON_VARIANT_UNSIGNED, json_dispatch_ifindex, offsetof(DNSConfiguration, ifindex), SD_JSON_RELAX }, |
| 260 |
{ "defaultRoute", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DNSConfiguration, default_route), 0 }, |
| 261 |
{ "currentServer", SD_JSON_VARIANT_OBJECT, dispatch_dns_server, offsetof(DNSConfiguration, current_dns_server), 0 }, |
| 262 |
{ "servers", SD_JSON_VARIANT_ARRAY, dispatch_dns_server_array, offsetof(DNSConfiguration, dns_servers), 0 }, |
| 263 |
{ "searchDomains", SD_JSON_VARIANT_ARRAY, dispatch_search_domain_array, offsetof(DNSConfiguration, search_domains), 0 }, |
| 264 |
{ "dnssecSupported", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DNSConfiguration, dnssec_supported), 0 }, |
| 265 |
{ "dnssec", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSConfiguration, dnssec_mode_str), 0 }, |
| 266 |
{ "dnsOverTLS", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSConfiguration, dns_over_tls_mode_str), 0 }, |
| 267 |
{ "llmnr", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSConfiguration, llmnr_mode_str), 0 }, |
| 268 |
{ "mDNS", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSConfiguration, mdns_mode_str), 0 }, |
| 269 |
{ "fallbackServers", SD_JSON_VARIANT_ARRAY, dispatch_dns_server_array, offsetof(DNSConfiguration, fallback_dns_servers), 0 }, |
| 270 |
{ "negativeTrustAnchors", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(DNSConfiguration, negative_trust_anchors), 0 }, |
| 271 |
{ "resolvConfMode", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSConfiguration, resolv_conf_mode_str), 0 }, |
| 272 |
{ "scopes", SD_JSON_VARIANT_ARRAY, dispatch_dns_scope_array, offsetof(DNSConfiguration, dns_scopes), 0 }, |
| 273 |
{ "delegate", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DNSConfiguration, delegate), 0 }, |
| 274 |
{}, |
| 275 |
|
| 276 |
}; |
| 277 |
DNSConfiguration **ret = ASSERT_PTR(userdata); |
| 278 |
_cleanup_(dns_configuration_freep) DNSConfiguration *c = NULL; |
| 279 |
int r; |
| 280 |
|
| 281 |
c = new0(DNSConfiguration, 1); |
| 282 |
if (!c) |
| 283 |
return log_oom(); |
| 284 |
|
| 285 |
r = sd_json_dispatch(variant, dns_configuration_dispatch_table, flags, c); |
| 286 |
if (r < 0) |
| 287 |
return r; |
| 288 |
|
| 289 |
strv_sort(c->negative_trust_anchors); |
| 290 |
|
| 291 |
*ret = TAKE_PTR(c); |
| 292 |
|
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
int dns_configuration_from_json(sd_json_variant *variant, DNSConfiguration **ret) { |
| 297 |
return dispatch_dns_configuration(NULL, variant, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, ret); |
| 298 |
} |
| 299 |
|
| 300 |
bool dns_is_accessible(DNSConfiguration *c) { |
| 301 |
DNSServer *s = NULL; |
| 302 |
|
| 303 |
if (!c) |
| 304 |
return false; |
| 305 |
|
| 306 |
if (c->current_dns_server && c->current_dns_server->accessible) |
| 307 |
return true; |
| 308 |
|
| 309 |
ORDERED_SET_FOREACH(s, c->dns_servers) |
| 310 |
if (s->accessible) |
| 311 |
return true; |
| 312 |
|
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
bool dns_configuration_contains_search_domain(DNSConfiguration *c, const char *domain) { |
| 317 |
SearchDomain *d = NULL; |
| 318 |
|
| 319 |
assert(domain); |
| 320 |
|
| 321 |
if (!c) |
| 322 |
return false; |
| 323 |
|
| 324 |
ORDERED_SET_FOREACH(d, c->search_domains) |
| 325 |
if (streq(d->name, domain)) |
| 326 |
return true; |
| 327 |
|
| 328 |
return false; |
| 329 |
} |