| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <resolv.h> /* IWYU pragma: keep */ |
| 4 |
#include <sys/stat.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "fd-util.h" |
| 8 |
#include "fileio.h" |
| 9 |
#include "fs-util.h" |
| 10 |
#include "log.h" |
| 11 |
#include "ordered-set.h" |
| 12 |
#include "path-util.h" |
| 13 |
#include "resolved-dns-cache.h" |
| 14 |
#include "resolved-dns-scope.h" |
| 15 |
#include "resolved-dns-search-domain.h" |
| 16 |
#include "resolved-dns-server.h" |
| 17 |
#include "resolved-dns-stub.h" |
| 18 |
#include "resolved-manager.h" |
| 19 |
#include "resolved-resolv-conf.h" |
| 20 |
#include "stat-util.h" |
| 21 |
#include "string-table.h" |
| 22 |
#include "string-util.h" |
| 23 |
#include "strv.h" |
| 24 |
#include "tmpfile-util.h" |
| 25 |
|
| 26 |
int manager_check_resolv_conf(const Manager *m) { |
| 27 |
struct stat st, own; |
| 28 |
|
| 29 |
assert(m); |
| 30 |
|
| 31 |
/* This warns only when our stub listener is disabled and /etc/resolv.conf is a symlink to |
| 32 |
* PRIVATE_STATIC_RESOLV_CONF. */ |
| 33 |
|
| 34 |
if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO) |
| 35 |
return 0; |
| 36 |
|
| 37 |
if (stat("/etc/resolv.conf", &st) < 0) { |
| 38 |
if (errno == ENOENT) |
| 39 |
return 0; |
| 40 |
|
| 41 |
return log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m"); |
| 42 |
} |
| 43 |
|
| 44 |
/* Is it symlinked to our own uplink file? */ |
| 45 |
if (stat(PRIVATE_STATIC_RESOLV_CONF, &own) >= 0 && |
| 46 |
stat_inode_same(&st, &own)) |
| 47 |
return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 48 |
"DNSStubListener= is disabled, but /etc/resolv.conf is a symlink to " |
| 49 |
PRIVATE_STATIC_RESOLV_CONF " which expects DNSStubListener= to be enabled."); |
| 50 |
|
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
static bool file_is_our_own(const struct stat *st) { |
| 55 |
assert(st); |
| 56 |
|
| 57 |
FOREACH_STRING(path, |
| 58 |
PRIVATE_UPLINK_RESOLV_CONF, |
| 59 |
PRIVATE_STUB_RESOLV_CONF, |
| 60 |
PRIVATE_STATIC_RESOLV_CONF) { |
| 61 |
|
| 62 |
struct stat own; |
| 63 |
|
| 64 |
/* Is it symlinked to our own uplink file? */ |
| 65 |
if (stat(path, &own) >= 0 && |
| 66 |
stat_inode_same(st, &own)) |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
int manager_read_resolv_conf(Manager *m) { |
| 74 |
_cleanup_fclose_ FILE *f = NULL; |
| 75 |
struct stat st; |
| 76 |
unsigned n = 0; |
| 77 |
int r; |
| 78 |
|
| 79 |
assert(m); |
| 80 |
|
| 81 |
/* Reads the system /etc/resolv.conf, if it exists and is not |
| 82 |
* symlinked to our own resolv.conf instance */ |
| 83 |
|
| 84 |
if (!m->read_resolv_conf) |
| 85 |
return 0; |
| 86 |
|
| 87 |
r = stat("/etc/resolv.conf", &st); |
| 88 |
if (r < 0) { |
| 89 |
if (errno == ENOENT) |
| 90 |
return 0; |
| 91 |
|
| 92 |
r = log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m"); |
| 93 |
goto clear; |
| 94 |
} |
| 95 |
|
| 96 |
/* Have we already seen the file? */ |
| 97 |
if (stat_inode_unmodified(&st, &m->resolv_conf_stat)) |
| 98 |
return 0; |
| 99 |
|
| 100 |
if (file_is_our_own(&st)) |
| 101 |
return 0; |
| 102 |
|
| 103 |
f = fopen("/etc/resolv.conf", "re"); |
| 104 |
if (!f) { |
| 105 |
if (errno == ENOENT) |
| 106 |
return 0; |
| 107 |
|
| 108 |
r = log_warning_errno(errno, "Failed to open %s: %m", "/etc/resolv.conf"); |
| 109 |
goto clear; |
| 110 |
} |
| 111 |
|
| 112 |
if (fstat(fileno(f), &st) < 0) { |
| 113 |
r = log_error_errno(errno, "Failed to stat open file: %m"); |
| 114 |
goto clear; |
| 115 |
} |
| 116 |
|
| 117 |
if (file_is_our_own(&st)) |
| 118 |
return 0; |
| 119 |
|
| 120 |
dns_server_mark_all(m->dns_servers); |
| 121 |
dns_search_domain_mark_all(m->search_domains); |
| 122 |
|
| 123 |
for (;;) { |
| 124 |
_cleanup_free_ char *line = NULL; |
| 125 |
const char *a; |
| 126 |
|
| 127 |
r = read_stripped_line(f, LONG_LINE_MAX, &line); |
| 128 |
if (r < 0) { |
| 129 |
log_error_errno(r, "Failed to read /etc/resolv.conf: %m"); |
| 130 |
goto clear; |
| 131 |
} |
| 132 |
if (r == 0) |
| 133 |
break; |
| 134 |
|
| 135 |
n++; |
| 136 |
|
| 137 |
if (IN_SET(*line, '#', ';', 0)) |
| 138 |
continue; |
| 139 |
|
| 140 |
a = first_word(line, "nameserver"); |
| 141 |
if (a) { |
| 142 |
r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a); |
| 143 |
if (r < 0) |
| 144 |
log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a); |
| 145 |
|
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
a = first_word(line, "domain"); |
| 150 |
if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */ |
| 151 |
a = first_word(line, "search"); |
| 152 |
if (a) { |
| 153 |
r = manager_parse_search_domains_and_warn(m, a); |
| 154 |
if (r < 0) |
| 155 |
log_warning_errno(r, "Failed to parse search domain string '%s', ignoring.", a); |
| 156 |
|
| 157 |
continue; |
| 158 |
} |
| 159 |
|
| 160 |
log_syntax(NULL, LOG_DEBUG, "/etc/resolv.conf", n, 0, "Ignoring resolv.conf line: %s", line); |
| 161 |
} |
| 162 |
|
| 163 |
m->resolv_conf_stat = st; |
| 164 |
|
| 165 |
/* Flush out all servers and search domains that are still |
| 166 |
* marked. Those are then ones that didn't appear in the new |
| 167 |
* /etc/resolv.conf */ |
| 168 |
dns_server_unlink_marked(m->dns_servers); |
| 169 |
dns_search_domain_unlink_marked(m->search_domains); |
| 170 |
|
| 171 |
/* Whenever /etc/resolv.conf changes, start using the first |
| 172 |
* DNS server of it. This is useful to deal with broken |
| 173 |
* network managing implementations (like NetworkManager), |
| 174 |
* that when connecting to a VPN place both the VPN DNS |
| 175 |
* servers and the local ones in /etc/resolv.conf. Without |
| 176 |
* resetting the DNS server to use back to the first entry we |
| 177 |
* will continue to use the local one thus being unable to |
| 178 |
* resolve VPN domains. */ |
| 179 |
manager_set_dns_server(m, m->dns_servers); |
| 180 |
|
| 181 |
/* Unconditionally flush the cache when /etc/resolv.conf is |
| 182 |
* modified, even if the data it contained was completely |
| 183 |
* identical to the previous version we used. We do this |
| 184 |
* because altering /etc/resolv.conf is typically done when |
| 185 |
* the network configuration changes, and that should be |
| 186 |
* enough to flush the global unicast DNS cache. */ |
| 187 |
if (m->unicast_scope) |
| 188 |
dns_cache_flush(&m->unicast_scope->cache); |
| 189 |
|
| 190 |
/* If /etc/resolv.conf changed, make sure to forget everything we learned about the DNS servers. After all we |
| 191 |
* might now talk to a very different DNS server that just happens to have the same IP address as an old one |
| 192 |
* (think 192.168.1.1). */ |
| 193 |
dns_server_reset_features_all(m->dns_servers); |
| 194 |
|
| 195 |
return 0; |
| 196 |
|
| 197 |
clear: |
| 198 |
dns_server_unlink_all(m->dns_servers); |
| 199 |
dns_search_domain_unlink_all(m->search_domains); |
| 200 |
return r; |
| 201 |
} |
| 202 |
|
| 203 |
static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) { |
| 204 |
DnsScope *scope; |
| 205 |
|
| 206 |
assert(s); |
| 207 |
assert(f); |
| 208 |
assert(count); |
| 209 |
|
| 210 |
if (!dns_server_string(s)) { |
| 211 |
log_warning("Out of memory, or invalid DNS address. Ignoring server."); |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
/* resolv.conf simply doesn't support any other ports than 53, hence there's nothing much we can |
| 216 |
* do — we have to suppress these entries */ |
| 217 |
if (dns_server_port(s) != 53) { |
| 218 |
log_debug("DNS server %s with non-standard UDP port number, suppressing from generated resolv.conf.", dns_server_string(s)); |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
/* Check if the scope this DNS server belongs to is suitable as 'default' route for lookups; resolv.conf does |
| 223 |
* not have a syntax to express that, so it must not appear as a global name server to avoid routing unrelated |
| 224 |
* domains to it (which is a privacy violation, will most probably fail anyway, and adds unnecessary load) */ |
| 225 |
scope = dns_server_scope(s); |
| 226 |
if (scope && !dns_scope_is_default_route(scope)) { |
| 227 |
log_debug("Scope of DNS server %s has only route-only domains, not using as global name server", dns_server_string(s)); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
if (*count == MAXNS) |
| 232 |
fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f); |
| 233 |
(*count)++; |
| 234 |
|
| 235 |
fprintf(f, "nameserver %s\n", dns_server_string(s)); |
| 236 |
} |
| 237 |
|
| 238 |
static void write_resolv_conf_search( |
| 239 |
OrderedSet *domains, |
| 240 |
FILE *f) { |
| 241 |
char *domain; |
| 242 |
|
| 243 |
assert(domains); |
| 244 |
assert(f); |
| 245 |
|
| 246 |
fputs("search", f); |
| 247 |
|
| 248 |
ORDERED_SET_FOREACH(domain, domains) { |
| 249 |
fputc(' ', f); |
| 250 |
fputs(domain, f); |
| 251 |
} |
| 252 |
|
| 253 |
fputs("\n", f); |
| 254 |
} |
| 255 |
|
| 256 |
static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) { |
| 257 |
|
| 258 |
fputs("# This is "PRIVATE_UPLINK_RESOLV_CONF" managed by man:systemd-resolved(8).\n" |
| 259 |
"# Do not edit.\n" |
| 260 |
"#\n" |
| 261 |
"# This file might be symlinked as /etc/resolv.conf. If you're looking at\n" |
| 262 |
"# /etc/resolv.conf and seeing this text, you have followed the symlink.\n" |
| 263 |
"#\n" |
| 264 |
"# This is a dynamic resolv.conf file for connecting local clients directly to\n" |
| 265 |
"# all known uplink DNS servers. This file lists all configured search domains.\n" |
| 266 |
"#\n" |
| 267 |
"# Third party programs should typically not access this file directly, but only\n" |
| 268 |
"# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n" |
| 269 |
"# different way, replace this symlink by a static file or a different symlink.\n" |
| 270 |
"#\n" |
| 271 |
"# See man:systemd-resolved.service(8) for details about the supported modes of\n" |
| 272 |
"# operation for /etc/resolv.conf.\n" |
| 273 |
"\n", f); |
| 274 |
|
| 275 |
if (ordered_set_isempty(dns)) |
| 276 |
fputs("# No DNS servers known.\n", f); |
| 277 |
else { |
| 278 |
unsigned count = 0; |
| 279 |
DnsServer *s; |
| 280 |
|
| 281 |
ORDERED_SET_FOREACH(s, dns) |
| 282 |
write_resolv_conf_server(s, f, &count); |
| 283 |
} |
| 284 |
|
| 285 |
if (ordered_set_isempty(domains)) |
| 286 |
fputs("search .\n", f); /* Make sure that if the local hostname is chosen as fqdn this does not |
| 287 |
* imply a search domain */ |
| 288 |
else |
| 289 |
write_resolv_conf_search(domains, f); |
| 290 |
|
| 291 |
return fflush_and_check(f); |
| 292 |
} |
| 293 |
|
| 294 |
static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) { |
| 295 |
fputs("# This is "PRIVATE_STUB_RESOLV_CONF" managed by man:systemd-resolved(8).\n" |
| 296 |
"# Do not edit.\n" |
| 297 |
"#\n" |
| 298 |
"# This file might be symlinked as /etc/resolv.conf. If you're looking at\n" |
| 299 |
"# /etc/resolv.conf and seeing this text, you have followed the symlink.\n" |
| 300 |
"#\n" |
| 301 |
"# This is a dynamic resolv.conf file for connecting local clients to the\n" |
| 302 |
"# internal DNS stub resolver of systemd-resolved. This file lists all\n" |
| 303 |
"# configured search domains.\n" |
| 304 |
"#\n" |
| 305 |
"# Run \"resolvectl status\" to see details about the uplink DNS servers\n" |
| 306 |
"# currently in use.\n" |
| 307 |
"#\n" |
| 308 |
"# Third party programs should typically not access this file directly, but only\n" |
| 309 |
"# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n" |
| 310 |
"# different way, replace this symlink by a static file or a different symlink.\n" |
| 311 |
"#\n" |
| 312 |
"# See man:systemd-resolved.service(8) for details about the supported modes of\n" |
| 313 |
"# operation for /etc/resolv.conf.\n" |
| 314 |
"\n" |
| 315 |
"nameserver 127.0.0.53\n" |
| 316 |
"options edns0 trust-ad\n", f); |
| 317 |
|
| 318 |
if (ordered_set_isempty(domains)) |
| 319 |
fputs("search .\n", f); /* Make sure that if the local hostname is chosen as fqdn this does not |
| 320 |
* imply a search domain */ |
| 321 |
else |
| 322 |
write_resolv_conf_search(domains, f); |
| 323 |
|
| 324 |
return fflush_and_check(f); |
| 325 |
} |
| 326 |
|
| 327 |
int manager_write_resolv_conf(Manager *m) { |
| 328 |
_cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL; |
| 329 |
_cleanup_(unlink_and_freep) char *temp_path_uplink = NULL, *temp_path_stub = NULL; |
| 330 |
_cleanup_fclose_ FILE *f_uplink = NULL, *f_stub = NULL; |
| 331 |
int r; |
| 332 |
|
| 333 |
assert(m); |
| 334 |
|
| 335 |
/* Read the system /etc/resolv.conf first */ |
| 336 |
(void) manager_read_resolv_conf(m); |
| 337 |
|
| 338 |
/* Add the full list to a set, to filter out duplicates */ |
| 339 |
r = manager_compile_dns_servers(m, &dns); |
| 340 |
if (r < 0) |
| 341 |
return log_warning_errno(r, "Failed to compile list of DNS servers, ignoring: %m"); |
| 342 |
|
| 343 |
r = manager_compile_search_domains(m, &domains, false); |
| 344 |
if (r < 0) |
| 345 |
return log_warning_errno(r, "Failed to compile list of search domains, ignoring: %m"); |
| 346 |
|
| 347 |
r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink); |
| 348 |
if (r < 0) |
| 349 |
return log_warning_errno(r, "Failed to open new %s for writing, ignoring: %m", PRIVATE_UPLINK_RESOLV_CONF); |
| 350 |
|
| 351 |
(void) fchmod(fileno(f_uplink), 0644); |
| 352 |
|
| 353 |
r = write_uplink_resolv_conf_contents(f_uplink, dns, domains); |
| 354 |
if (r < 0) |
| 355 |
return log_warning_errno(r, "Failed to write new %s, ignoring: %m", PRIVATE_UPLINK_RESOLV_CONF); |
| 356 |
|
| 357 |
if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO) { |
| 358 |
r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub); |
| 359 |
if (r < 0) |
| 360 |
return log_warning_errno(r, "Failed to open new %s for writing, ignoring: %m", PRIVATE_STUB_RESOLV_CONF); |
| 361 |
|
| 362 |
(void) fchmod(fileno(f_stub), 0644); |
| 363 |
|
| 364 |
r = write_stub_resolv_conf_contents(f_stub, dns, domains); |
| 365 |
if (r < 0) |
| 366 |
return log_warning_errno(r, "Failed to write new %s, ignoring: %m", PRIVATE_STUB_RESOLV_CONF); |
| 367 |
|
| 368 |
r = conservative_rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF); |
| 369 |
if (r < 0) |
| 370 |
log_warning_errno(r, "Failed to move new %s into place, ignoring: %m", PRIVATE_STUB_RESOLV_CONF); |
| 371 |
|
| 372 |
temp_path_stub = mfree(temp_path_stub); /* free the string explicitly, so that we don't unlink anymore */ |
| 373 |
} else { |
| 374 |
_cleanup_free_ char *fname = NULL; |
| 375 |
r = path_extract_filename(PRIVATE_UPLINK_RESOLV_CONF, &fname); |
| 376 |
if (r < 0) |
| 377 |
return log_warning_errno(r, "Failed to extract filename from path '" PRIVATE_UPLINK_RESOLV_CONF "', ignoring: %m"); |
| 378 |
|
| 379 |
r = symlinkat_atomic_full(fname, AT_FDCWD, PRIVATE_STUB_RESOLV_CONF, SYMLINK_LABEL); |
| 380 |
if (r < 0) |
| 381 |
log_warning_errno(r, "Failed to symlink %s, ignoring: %m", PRIVATE_STUB_RESOLV_CONF); |
| 382 |
} |
| 383 |
|
| 384 |
r = conservative_rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF); |
| 385 |
if (r < 0) |
| 386 |
log_warning_errno(r, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF); |
| 387 |
|
| 388 |
temp_path_uplink = mfree(temp_path_uplink); /* free the string explicitly, so that we don't unlink anymore */ |
| 389 |
return r; |
| 390 |
} |
| 391 |
|
| 392 |
int resolv_conf_mode(void) { |
| 393 |
static const char * const table[_RESOLV_CONF_MODE_MAX] = { |
| 394 |
[RESOLV_CONF_UPLINK] = PRIVATE_UPLINK_RESOLV_CONF, |
| 395 |
[RESOLV_CONF_STUB] = PRIVATE_STUB_RESOLV_CONF, |
| 396 |
[RESOLV_CONF_STATIC] = PRIVATE_STATIC_RESOLV_CONF, |
| 397 |
}; |
| 398 |
|
| 399 |
struct stat system_st; |
| 400 |
|
| 401 |
if (stat("/etc/resolv.conf", &system_st) < 0) { |
| 402 |
if (errno == ENOENT) |
| 403 |
return RESOLV_CONF_MISSING; |
| 404 |
|
| 405 |
return -errno; |
| 406 |
} |
| 407 |
|
| 408 |
for (ResolvConfMode m = 0; m < _RESOLV_CONF_MODE_MAX; m++) { |
| 409 |
struct stat our_st; |
| 410 |
|
| 411 |
if (!table[m]) |
| 412 |
continue; |
| 413 |
|
| 414 |
if (stat(table[m], &our_st) < 0) { |
| 415 |
if (errno != ENOENT) |
| 416 |
log_debug_errno(errno, "Failed to stat() %s, ignoring: %m", table[m]); |
| 417 |
|
| 418 |
continue; |
| 419 |
} |
| 420 |
|
| 421 |
if (stat_inode_same(&system_st, &our_st)) |
| 422 |
return m; |
| 423 |
} |
| 424 |
|
| 425 |
return RESOLV_CONF_FOREIGN; |
| 426 |
} |
| 427 |
|
| 428 |
static const char* const resolv_conf_mode_table[_RESOLV_CONF_MODE_MAX] = { |
| 429 |
[RESOLV_CONF_UPLINK] = "uplink", |
| 430 |
[RESOLV_CONF_STUB] = "stub", |
| 431 |
[RESOLV_CONF_STATIC] = "static", |
| 432 |
[RESOLV_CONF_MISSING] = "missing", |
| 433 |
[RESOLV_CONF_FOREIGN] = "foreign", |
| 434 |
}; |
| 435 |
DEFINE_STRING_TABLE_LOOKUP(resolv_conf_mode, ResolvConfMode); |