| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "conf-parser.h" |
| 5 |
#include "creds-util.h" |
| 6 |
#include "dns-type.h" |
| 7 |
#include "extract-word.h" |
| 8 |
#include "ordered-set.h" |
| 9 |
#include "proc-cmdline.h" |
| 10 |
#include "resolved-conf.h" |
| 11 |
#include "resolved-dns-cache.h" |
| 12 |
#include "resolved-dns-search-domain.h" |
| 13 |
#include "resolved-dns-server.h" |
| 14 |
#include "resolved-dns-stub.h" |
| 15 |
#include "resolved-manager.h" |
| 16 |
#include "set.h" |
| 17 |
#include "socket-netlink.h" |
| 18 |
#include "string-util.h" |
| 19 |
|
| 20 |
DEFINE_CONFIG_PARSE_ENUM(config_parse_dns_stub_listener_mode, dns_stub_listener_mode, DnsStubListenerMode); |
| 21 |
|
| 22 |
int config_parse_dns_servers( |
| 23 |
const char *unit, |
| 24 |
const char *filename, |
| 25 |
unsigned line, |
| 26 |
const char *section, |
| 27 |
unsigned section_line, |
| 28 |
const char *lvalue, |
| 29 |
int ltype, |
| 30 |
const char *rvalue, |
| 31 |
void *data, |
| 32 |
void *userdata) { |
| 33 |
|
| 34 |
Manager *m = ASSERT_PTR(userdata); |
| 35 |
int r; |
| 36 |
|
| 37 |
assert(filename); |
| 38 |
assert(lvalue); |
| 39 |
assert(rvalue); |
| 40 |
|
| 41 |
if (isempty(rvalue)) |
| 42 |
/* Empty assignment means clear the list */ |
| 43 |
dns_server_unlink_all(manager_get_first_dns_server(m, ltype)); |
| 44 |
else { |
| 45 |
/* Otherwise, add to the list */ |
| 46 |
r = manager_parse_dns_server_string_and_warn(m, ltype, rvalue); |
| 47 |
if (r < 0) { |
| 48 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 49 |
"Failed to parse DNS server string '%s', ignoring.", rvalue); |
| 50 |
return 0; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/* If we have a manual setting, then we stop reading |
| 55 |
* /etc/resolv.conf */ |
| 56 |
if (ltype == DNS_SERVER_SYSTEM) |
| 57 |
m->read_resolv_conf = false; |
| 58 |
if (ltype == DNS_SERVER_FALLBACK) |
| 59 |
m->need_builtin_fallbacks = false; |
| 60 |
|
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
int config_parse_search_domains( |
| 65 |
const char *unit, |
| 66 |
const char *filename, |
| 67 |
unsigned line, |
| 68 |
const char *section, |
| 69 |
unsigned section_line, |
| 70 |
const char *lvalue, |
| 71 |
int ltype, |
| 72 |
const char *rvalue, |
| 73 |
void *data, |
| 74 |
void *userdata) { |
| 75 |
|
| 76 |
Manager *m = ASSERT_PTR(userdata); |
| 77 |
int r; |
| 78 |
|
| 79 |
assert(filename); |
| 80 |
assert(lvalue); |
| 81 |
assert(rvalue); |
| 82 |
|
| 83 |
if (isempty(rvalue)) |
| 84 |
/* Empty assignment means clear the list */ |
| 85 |
dns_search_domain_unlink_all(m->search_domains); |
| 86 |
else { |
| 87 |
/* Otherwise, add to the list */ |
| 88 |
r = manager_parse_search_domains_and_warn(m, rvalue); |
| 89 |
if (r < 0) { |
| 90 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 91 |
"Failed to parse search domains string '%s', ignoring.", rvalue); |
| 92 |
return 0; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/* If we have a manual setting, then we stop reading |
| 97 |
* /etc/resolv.conf */ |
| 98 |
m->read_resolv_conf = false; |
| 99 |
|
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
int config_parse_dns_stub_listener_extra( |
| 104 |
const char *unit, |
| 105 |
const char *filename, |
| 106 |
unsigned line, |
| 107 |
const char *section, |
| 108 |
unsigned section_line, |
| 109 |
const char *lvalue, |
| 110 |
int ltype, |
| 111 |
const char *rvalue, |
| 112 |
void *data, |
| 113 |
void *userdata) { |
| 114 |
|
| 115 |
_cleanup_free_ DnsStubListenerExtra *stub = NULL; |
| 116 |
Manager *m = userdata; |
| 117 |
const char *p; |
| 118 |
int r; |
| 119 |
|
| 120 |
assert(filename); |
| 121 |
assert(lvalue); |
| 122 |
assert(rvalue); |
| 123 |
assert(data); |
| 124 |
|
| 125 |
if (isempty(rvalue)) { |
| 126 |
m->dns_extra_stub_listeners = ordered_set_free(m->dns_extra_stub_listeners); |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
r = dns_stub_listener_extra_new(m, &stub); |
| 131 |
if (r < 0) |
| 132 |
return log_oom(); |
| 133 |
|
| 134 |
p = startswith(rvalue, "udp:"); |
| 135 |
if (p) |
| 136 |
stub->mode = DNS_STUB_LISTENER_UDP; |
| 137 |
else { |
| 138 |
p = startswith(rvalue, "tcp:"); |
| 139 |
if (p) |
| 140 |
stub->mode = DNS_STUB_LISTENER_TCP; |
| 141 |
else { |
| 142 |
stub->mode = DNS_STUB_LISTENER_YES; |
| 143 |
p = rvalue; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
r = in_addr_port_ifindex_name_from_string_auto(p, &stub->family, &stub->address, &stub->port, NULL, NULL); |
| 148 |
if (r < 0) { |
| 149 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 150 |
"Failed to parse address in %s=%s, ignoring assignment: %m", |
| 151 |
lvalue, rvalue); |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
r = ordered_set_ensure_put(&m->dns_extra_stub_listeners, &dns_stub_listener_extra_hash_ops, stub); |
| 156 |
if (r == -ENOMEM) |
| 157 |
return log_oom(); |
| 158 |
if (r < 0) { |
| 159 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 160 |
"Failed to store %s=%s, ignoring assignment: %m", lvalue, rvalue); |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
TAKE_PTR(stub); |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static void read_credentials(Manager *m) { |
| 170 |
_cleanup_free_ char *dns = NULL, *domains = NULL; |
| 171 |
int r; |
| 172 |
|
| 173 |
assert(m); |
| 174 |
|
| 175 |
/* Hmm, if we aren't supposed to read /etc/resolv.conf because the DNS settings were already |
| 176 |
* configured explicitly in our config file, we don't want to honour credentials either */ |
| 177 |
if (!m->read_resolv_conf) |
| 178 |
return; |
| 179 |
|
| 180 |
r = read_credential_strings_many("network.dns", &dns, |
| 181 |
"network.search_domains", &domains); |
| 182 |
if (r < 0) |
| 183 |
log_warning_errno(r, "Failed to read credentials, ignoring: %m"); |
| 184 |
|
| 185 |
if (dns) { |
| 186 |
r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, dns); |
| 187 |
if (r < 0) |
| 188 |
log_warning_errno(r, "Failed to parse credential network.dns '%s', ignoring.", dns); |
| 189 |
|
| 190 |
m->read_resolv_conf = false; |
| 191 |
} |
| 192 |
|
| 193 |
if (domains) { |
| 194 |
r = manager_parse_search_domains_and_warn(m, domains); |
| 195 |
if (r < 0) |
| 196 |
log_warning_errno(r, "Failed to parse credential network.search_domains '%s', ignoring.", domains); |
| 197 |
|
| 198 |
m->read_resolv_conf = false; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
struct ProcCmdlineInfo { |
| 203 |
Manager *manager; |
| 204 |
|
| 205 |
/* If there's a setting configured via /proc/cmdline we want to reset the configured lists, but only |
| 206 |
* once, so that multiple nameserver= or domain= settings can be specified on the kernel command line |
| 207 |
* and will be combined. These booleans will be set once we erase the list once. */ |
| 208 |
bool dns_server_unlinked; |
| 209 |
bool search_domain_unlinked; |
| 210 |
}; |
| 211 |
|
| 212 |
static int proc_cmdline_callback(const char *key, const char *value, void *data) { |
| 213 |
struct ProcCmdlineInfo *info = ASSERT_PTR(data); |
| 214 |
int r; |
| 215 |
|
| 216 |
assert(key); |
| 217 |
assert(info->manager); |
| 218 |
|
| 219 |
/* The kernel command line option names are chosen to be compatible with what various tools already |
| 220 |
* interpret, for example dracut and SUSE Linux. */ |
| 221 |
|
| 222 |
if (streq(key, "nameserver")) { |
| 223 |
|
| 224 |
if (proc_cmdline_value_missing(key, value)) |
| 225 |
return 0; |
| 226 |
|
| 227 |
if (!info->dns_server_unlinked) { |
| 228 |
/* The kernel command line overrides any prior configuration */ |
| 229 |
dns_server_unlink_all(manager_get_first_dns_server(info->manager, DNS_SERVER_SYSTEM)); |
| 230 |
info->dns_server_unlinked = true; |
| 231 |
} |
| 232 |
|
| 233 |
r = manager_parse_dns_server_string_and_warn(info->manager, DNS_SERVER_SYSTEM, value); |
| 234 |
if (r < 0) |
| 235 |
log_warning_errno(r, "Failed to parse DNS server string '%s', ignoring.", value); |
| 236 |
|
| 237 |
info->manager->read_resolv_conf = false; |
| 238 |
|
| 239 |
} else if (streq(key, "domain")) { |
| 240 |
|
| 241 |
if (proc_cmdline_value_missing(key, value)) |
| 242 |
return 0; |
| 243 |
|
| 244 |
if (!info->search_domain_unlinked) { |
| 245 |
dns_search_domain_unlink_all(info->manager->search_domains); |
| 246 |
info->search_domain_unlinked = true; |
| 247 |
} |
| 248 |
|
| 249 |
r = manager_parse_search_domains_and_warn(info->manager, value); |
| 250 |
if (r < 0) |
| 251 |
log_warning_errno(r, "Failed to parse credential provided search domain string '%s', ignoring.", value); |
| 252 |
|
| 253 |
info->manager->read_resolv_conf = false; |
| 254 |
} |
| 255 |
|
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
static void read_proc_cmdline(Manager *m) { |
| 260 |
int r; |
| 261 |
|
| 262 |
assert(m); |
| 263 |
|
| 264 |
r = proc_cmdline_parse(proc_cmdline_callback, &(struct ProcCmdlineInfo) { .manager = m }, 0); |
| 265 |
if (r < 0) |
| 266 |
log_warning_errno(r, "Failed to read kernel command line, ignoring: %m"); |
| 267 |
} |
| 268 |
|
| 269 |
int manager_parse_config_file(Manager *m) { |
| 270 |
int r; |
| 271 |
|
| 272 |
assert(m); |
| 273 |
|
| 274 |
r = config_parse_standard_file_with_dropins( |
| 275 |
"systemd/resolved.conf", |
| 276 |
"Resolve\0", |
| 277 |
config_item_perf_lookup, resolved_gperf_lookup, |
| 278 |
CONFIG_PARSE_WARN, |
| 279 |
/* userdata= */ m); |
| 280 |
if (r < 0) |
| 281 |
return r; |
| 282 |
|
| 283 |
read_credentials(m); /* credentials are only used when nothing is explicitly configured … */ |
| 284 |
read_proc_cmdline(m); /* … but kernel command line overrides local configuration. */ |
| 285 |
|
| 286 |
if (m->need_builtin_fallbacks) { |
| 287 |
r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_FALLBACK, DNS_SERVERS); |
| 288 |
if (r < 0) |
| 289 |
return r; |
| 290 |
} |
| 291 |
|
| 292 |
#if !HAVE_OPENSSL |
| 293 |
if (m->dnssec_mode != DNSSEC_NO) { |
| 294 |
log_warning("DNSSEC option cannot be enabled or set to allow-downgrade when systemd-resolved is built without openssl. Turning off DNSSEC support."); |
| 295 |
m->dnssec_mode = DNSSEC_NO; |
| 296 |
} |
| 297 |
#endif |
| 298 |
|
| 299 |
#if !ENABLE_DNS_OVER_TLS |
| 300 |
if (m->dns_over_tls_mode != DNS_OVER_TLS_NO) { |
| 301 |
log_warning("DNS-over-TLS option cannot be enabled or set to opportunistic when systemd-resolved is built without DNS-over-TLS support. Turning off DNS-over-TLS support."); |
| 302 |
m->dns_over_tls_mode = DNS_OVER_TLS_NO; |
| 303 |
} |
| 304 |
#endif |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
int config_parse_dns_cache_max( |
| 309 |
const char *unit, |
| 310 |
const char *filename, |
| 311 |
unsigned line, |
| 312 |
const char *section, |
| 313 |
unsigned section_line, |
| 314 |
const char *lvalue, |
| 315 |
int ltype, |
| 316 |
const char *rvalue, |
| 317 |
void *data, |
| 318 |
void *userdata) { |
| 319 |
|
| 320 |
Manager *m = ASSERT_PTR(userdata); |
| 321 |
|
| 322 |
assert(ltype >= 0 && ltype < _DNS_PROTOCOL_MAX); |
| 323 |
|
| 324 |
return config_parse_unsigned_bounded( |
| 325 |
unit, filename, line, section, section_line, lvalue, rvalue, |
| 326 |
0, CACHE_MAX_UPPER_LIMIT, true, |
| 327 |
&m->cache_max[ltype]); |
| 328 |
} |
| 329 |
|
| 330 |
int config_parse_record_types( |
| 331 |
const char *unit, |
| 332 |
const char *filename, |
| 333 |
unsigned line, |
| 334 |
const char *section, |
| 335 |
unsigned section_line, |
| 336 |
const char *lvalue, |
| 337 |
int ltype, |
| 338 |
const char *rvalue, |
| 339 |
void *data, |
| 340 |
void *userdata) { |
| 341 |
|
| 342 |
Set **types = ASSERT_PTR(data); |
| 343 |
int r; |
| 344 |
|
| 345 |
if (isempty(rvalue)) { |
| 346 |
*types = set_free(*types); |
| 347 |
return 1; |
| 348 |
} |
| 349 |
|
| 350 |
for (const char *p = rvalue;;) { |
| 351 |
_cleanup_free_ char *word = NULL; |
| 352 |
r = extract_first_word(&p, &word, NULL, 0); |
| 353 |
if (r < 0) |
| 354 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 355 |
if (r == 0) |
| 356 |
return 1; |
| 357 |
|
| 358 |
r = dns_type_from_string(word); |
| 359 |
if (r < 0) { |
| 360 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid DNS record type, ignoring: %s", word); |
| 361 |
continue; |
| 362 |
} |
| 363 |
|
| 364 |
r = set_ensure_put(types, NULL, INT_TO_PTR(r)); |
| 365 |
if (r < 0) |
| 366 |
return log_oom(); |
| 367 |
} |
| 368 |
} |