]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-resolv-conf.c
namespace: Clone root dir descriptor before use (#39939)
[thirdparty/systemd.git] / src / resolve / resolved-resolv-conf.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
f8dc7e34 2
87fbd333 3#include <resolv.h> /* IWYU pragma: keep */
ca78ad1d 4#include <sys/stat.h>
f8dc7e34
LP
5
6#include "alloc-util.h"
f8dc7e34 7#include "fd-util.h"
f8dc7e34 8#include "fileio.h"
ca8b81d9 9#include "fs-util.h"
284d7641 10#include "log.h"
f8dc7e34 11#include "ordered-set.h"
b8953115 12#include "path-util.h"
68527d30
DDM
13#include "resolved-dns-cache.h"
14#include "resolved-dns-scope.h"
15#include "resolved-dns-search-domain.h"
59c0fd0e 16#include "resolved-dns-server.h"
68527d30
DDM
17#include "resolved-dns-stub.h"
18#include "resolved-manager.h"
f8dc7e34 19#include "resolved-resolv-conf.h"
61c12865 20#include "stat-util.h"
4261ab65 21#include "string-table.h"
f8dc7e34
LP
22#include "string-util.h"
23#include "strv.h"
e4de7287 24#include "tmpfile-util-label.h"
f8dc7e34 25
f43580f1 26int manager_check_resolv_conf(const Manager *m) {
ca8b81d9 27 struct stat st, own;
f43580f1
YW
28
29 assert(m);
30
31 /* This warns only when our stub listener is disabled and /etc/resolv.conf is a symlink to
ca8b81d9 32 * PRIVATE_STATIC_RESOLV_CONF. */
f43580f1
YW
33
34 if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO)
35 return 0;
36
ca8b81d9 37 if (stat("/etc/resolv.conf", &st) < 0) {
f43580f1
YW
38 if (errno == ENOENT)
39 return 0;
40
41 return log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m");
42 }
43
ca8b81d9
ZJS
44 /* Is it symlinked to our own uplink file? */
45 if (stat(PRIVATE_STATIC_RESOLV_CONF, &own) >= 0 &&
a9dac7a6 46 stat_inode_same(&st, &own))
ca8b81d9
ZJS
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.");
f43580f1
YW
50
51 return 0;
52}
53
043d3928 54static bool file_is_our_own(const struct stat *st) {
043d3928
LP
55 assert(st);
56
b6de578d
LP
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 &&
a9dac7a6 66 stat_inode_same(st, &own))
b6de578d
LP
67 return true;
68 }
043d3928
LP
69
70 return false;
71}
72
f8dc7e34
LP
73int manager_read_resolv_conf(Manager *m) {
74 _cleanup_fclose_ FILE *f = NULL;
043d3928 75 struct stat st;
b351c300 76 unsigned n = 0;
f8dc7e34
LP
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)
bf7fabd6
LP
90 return 0;
91
92 r = log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m");
f8dc7e34
LP
93 goto clear;
94 }
95
96 /* Have we already seen the file? */
61c12865 97 if (stat_inode_unmodified(&st, &m->resolv_conf_stat))
f8dc7e34
LP
98 return 0;
99
043d3928 100 if (file_is_our_own(&st))
bf7fabd6 101 return 0;
f8dc7e34
LP
102
103 f = fopen("/etc/resolv.conf", "re");
104 if (!f) {
105 if (errno == ENOENT)
bf7fabd6
LP
106 return 0;
107
42ba9974 108 r = log_warning_errno(errno, "Failed to open %s: %m", "/etc/resolv.conf");
f8dc7e34
LP
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
043d3928
LP
117 if (file_is_our_own(&st))
118 return 0;
119
4b95f179 120 dns_server_mark_all(m->dns_servers);
a51c1048 121 dns_search_domain_mark_all(m->search_domains);
f8dc7e34 122
e1b9fc23
LP
123 for (;;) {
124 _cleanup_free_ char *line = NULL;
f8dc7e34 125 const char *a;
f8dc7e34 126
0ff6ff2b 127 r = read_stripped_line(f, LONG_LINE_MAX, &line);
e1b9fc23
LP
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
b351c300
LP
135 n++;
136
0ff6ff2b 137 if (IN_SET(*line, '#', ';', 0))
f8dc7e34
LP
138 continue;
139
0ff6ff2b 140 a = first_word(line, "nameserver");
f8dc7e34 141 if (a) {
281df579 142 r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a);
f8dc7e34
LP
143 if (r < 0)
144 log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
145
146 continue;
147 }
a51c1048 148
0ff6ff2b 149 a = first_word(line, "domain");
a51c1048 150 if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
0ff6ff2b 151 a = first_word(line, "search");
a51c1048
LP
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);
02c20535
LP
156
157 continue;
a51c1048 158 }
b351c300 159
0ff6ff2b 160 log_syntax(NULL, LOG_DEBUG, "/etc/resolv.conf", n, 0, "Ignoring resolv.conf line: %s", line);
f8dc7e34
LP
161 }
162
61c12865 163 m->resolv_conf_stat = st;
bf7fabd6 164
a51c1048
LP
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 */
4b95f179 168 dns_server_unlink_marked(m->dns_servers);
a51c1048 169 dns_search_domain_unlink_marked(m->search_domains);
f8dc7e34
LP
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
452b4e32
LP
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
59c0fd0e
LP
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
f8dc7e34
LP
195 return 0;
196
197clear:
4b95f179 198 dns_server_unlink_all(m->dns_servers);
a51c1048 199 dns_search_domain_unlink_all(m->search_domains);
f8dc7e34
LP
200 return r;
201}
202
203static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
f76fa088
LP
204 DnsScope *scope;
205
f8dc7e34
LP
206 assert(s);
207 assert(f);
208 assert(count);
209
2817157b 210 if (!dns_server_string(s)) {
be4bf266 211 log_warning("Out of memory, or invalid DNS address. Ignoring server.");
f8dc7e34
LP
212 return;
213 }
214
a50dadf2
LP
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
ca5394d2
LP
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) */
f76fa088 225 scope = dns_server_scope(s);
ca5394d2 226 if (scope && !dns_scope_is_default_route(scope)) {
f76fa088 227 log_debug("Scope of DNS server %s has only route-only domains, not using as global name server", dns_server_string(s));
b9fe94ca
MP
228 return;
229 }
230
f8dc7e34 231 if (*count == MAXNS)
0d536673 232 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
313cefa1 233 (*count)++;
f8dc7e34 234
2817157b 235 fprintf(f, "nameserver %s\n", dns_server_string(s));
f8dc7e34
LP
236}
237
238static void write_resolv_conf_search(
d2bc1251
MP
239 OrderedSet *domains,
240 FILE *f) {
d2bc1251 241 char *domain;
f8dc7e34 242
d2bc1251 243 assert(domains);
f8dc7e34 244 assert(f);
f8dc7e34 245
0d536673 246 fputs("search", f);
f8dc7e34 247
90e74a66 248 ORDERED_SET_FOREACH(domain, domains) {
0d536673
LP
249 fputc(' ', f);
250 fputs(domain, f);
f8dc7e34
LP
251 }
252
0d536673 253 fputs("\n", f);
f8dc7e34
LP
254}
255
e6b2d948 256static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
f8dc7e34 257
2b767e92
ZJS
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"
0d536673
LP
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"
ce416f42
LP
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"
0d536673
LP
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);
f8dc7e34
LP
274
275 if (ordered_set_isempty(dns))
0d536673 276 fputs("# No DNS servers known.\n", f);
f8dc7e34
LP
277 else {
278 unsigned count = 0;
279 DnsServer *s;
280
90e74a66 281 ORDERED_SET_FOREACH(s, dns)
f8dc7e34
LP
282 write_resolv_conf_server(s, f, &count);
283 }
284
b3ffa2b5 285 if (ordered_set_isempty(domains))
31619e2f
ZJS
286 fputs("search .\n", f); /* Make sure that if the local hostname is chosen as fqdn this does not
287 * imply a search domain */
b3ffa2b5 288 else
d2bc1251 289 write_resolv_conf_search(domains, f);
f8dc7e34
LP
290
291 return fflush_and_check(f);
292}
293
e6b2d948 294static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
2b767e92
ZJS
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"
ce416f42
LP
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"
a742f982 316 "options edns0 trust-ad\n", f);
e6b2d948 317
b3ffa2b5 318 if (ordered_set_isempty(domains))
31619e2f
ZJS
319 fputs("search .\n", f); /* Make sure that if the local hostname is chosen as fqdn this does not
320 * imply a search domain */
b3ffa2b5 321 else
e6b2d948
DJL
322 write_resolv_conf_search(domains, f);
323
324 return fflush_and_check(f);
325}
326
f8dc7e34 327int manager_write_resolv_conf(Manager *m) {
9176a57c 328 _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
e2ef1e9a 329 _cleanup_(unlink_and_freep) char *temp_path_uplink = NULL, *temp_path_stub = NULL;
0d536673 330 _cleanup_fclose_ FILE *f_uplink = NULL, *f_stub = NULL;
f8dc7e34
LP
331 int r;
332
333 assert(m);
334
335 /* Read the system /etc/resolv.conf first */
7207052d 336 (void) manager_read_resolv_conf(m);
f8dc7e34
LP
337
338 /* Add the full list to a set, to filter out duplicates */
9176a57c
LP
339 r = manager_compile_dns_servers(m, &dns);
340 if (r < 0)
e2ef1e9a 341 return log_warning_errno(r, "Failed to compile list of DNS servers, ignoring: %m");
f8dc7e34 342
6f7da49d 343 r = manager_compile_search_domains(m, &domains, false);
9176a57c 344 if (r < 0)
e2ef1e9a 345 return log_warning_errno(r, "Failed to compile list of search domains, ignoring: %m");
f8dc7e34 346
e6b2d948 347 r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink);
f8dc7e34 348 if (r < 0)
e2ef1e9a 349 return log_warning_errno(r, "Failed to open new %s for writing, ignoring: %m", PRIVATE_UPLINK_RESOLV_CONF);
0d536673 350
0d536673
LP
351 (void) fchmod(fileno(f_uplink), 0644);
352
e6b2d948 353 r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);
e2ef1e9a
LP
354 if (r < 0)
355 return log_warning_errno(r, "Failed to write new %s, ignoring: %m", PRIVATE_UPLINK_RESOLV_CONF);
f8dc7e34 356
ca8b81d9
ZJS
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);
e2ef1e9a
LP
359 if (r < 0)
360 return log_warning_errno(r, "Failed to open new %s for writing, ignoring: %m", PRIVATE_STUB_RESOLV_CONF);
f8dc7e34 361
ca8b81d9 362 (void) fchmod(fileno(f_stub), 0644);
965228a8 363
ca8b81d9 364 r = write_stub_resolv_conf_contents(f_stub, dns, domains);
e2ef1e9a
LP
365 if (r < 0)
366 return log_warning_errno(r, "Failed to write new %s, ignoring: %m", PRIVATE_STUB_RESOLV_CONF);
ca8b81d9 367
10195179 368 r = conservative_rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF);
f3e1f00d 369 if (r < 0)
e2ef1e9a 370 log_warning_errno(r, "Failed to move new %s into place, ignoring: %m", PRIVATE_STUB_RESOLV_CONF);
ca8b81d9 371
e2ef1e9a 372 temp_path_stub = mfree(temp_path_stub); /* free the string explicitly, so that we don't unlink anymore */
ca8b81d9 373 } else {
b8953115
SL
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
9ea5a6e7 379 r = symlinkat_atomic_full(fname, AT_FDCWD, PRIVATE_STUB_RESOLV_CONF, SYMLINK_LABEL);
ca8b81d9 380 if (r < 0)
e2ef1e9a 381 log_warning_errno(r, "Failed to symlink %s, ignoring: %m", PRIVATE_STUB_RESOLV_CONF);
e6b2d948
DJL
382 }
383
10195179 384 r = conservative_rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF);
f3e1f00d 385 if (r < 0)
e2ef1e9a 386 log_warning_errno(r, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF);
7207052d 387
e2ef1e9a 388 temp_path_uplink = mfree(temp_path_uplink); /* free the string explicitly, so that we don't unlink anymore */
f8dc7e34
LP
389 return r;
390}
4261ab65
LP
391
392int 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
a9dac7a6 421 if (stat_inode_same(&system_st, &our_st))
4261ab65
LP
422 return m;
423 }
424
425 return RESOLV_CONF_FOREIGN;
426}
427
428static 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};
435DEFINE_STRING_TABLE_LOOKUP(resolv_conf_mode, ResolvConfMode);