]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-resolv-conf.c
Merge pull request #15516 from poettering/nspawn-resolv-conf
[thirdparty/systemd.git] / src / resolve / resolved-resolv-conf.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <resolv.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7
8 #include "alloc-util.h"
9 #include "dns-domain.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "fs-util.h"
13 #include "ordered-set.h"
14 #include "resolved-conf.h"
15 #include "resolved-dns-server.h"
16 #include "resolved-resolv-conf.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "tmpfile-util-label.h"
20
21 int manager_check_resolv_conf(const Manager *m) {
22 struct stat st, own;
23
24 assert(m);
25
26 /* This warns only when our stub listener is disabled and /etc/resolv.conf is a symlink to
27 * PRIVATE_STATIC_RESOLV_CONF. */
28
29 if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO)
30 return 0;
31
32 if (stat("/etc/resolv.conf", &st) < 0) {
33 if (errno == ENOENT)
34 return 0;
35
36 return log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m");
37 }
38
39 /* Is it symlinked to our own uplink file? */
40 if (stat(PRIVATE_STATIC_RESOLV_CONF, &own) >= 0 &&
41 st.st_dev == own.st_dev &&
42 st.st_ino == own.st_ino)
43 return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
44 "DNSStubListener= is disabled, but /etc/resolv.conf is a symlink to "
45 PRIVATE_STATIC_RESOLV_CONF " which expects DNSStubListener= to be enabled.");
46
47 return 0;
48 }
49
50 static bool file_is_our_own(const struct stat *st) {
51 const char *path;
52
53 assert(st);
54
55 FOREACH_STRING(path,
56 PRIVATE_UPLINK_RESOLV_CONF,
57 PRIVATE_STUB_RESOLV_CONF,
58 PRIVATE_STATIC_RESOLV_CONF) {
59
60 struct stat own;
61
62 /* Is it symlinked to our own uplink file? */
63 if (stat(path, &own) >= 0 &&
64 st->st_dev == own.st_dev &&
65 st->st_ino == own.st_ino)
66 return true;
67 }
68
69 return false;
70 }
71
72 int manager_read_resolv_conf(Manager *m) {
73 _cleanup_fclose_ FILE *f = NULL;
74 struct stat st;
75 unsigned n = 0;
76 int r;
77
78 assert(m);
79
80 /* Reads the system /etc/resolv.conf, if it exists and is not
81 * symlinked to our own resolv.conf instance */
82
83 if (!m->read_resolv_conf)
84 return 0;
85
86 r = stat("/etc/resolv.conf", &st);
87 if (r < 0) {
88 if (errno == ENOENT)
89 return 0;
90
91 r = log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m");
92 goto clear;
93 }
94
95 /* Have we already seen the file? */
96 if (timespec_load(&st.st_mtim) == m->resolv_conf_mtime)
97 return 0;
98
99 if (file_is_our_own(&st))
100 return 0;
101
102 f = fopen("/etc/resolv.conf", "re");
103 if (!f) {
104 if (errno == ENOENT)
105 return 0;
106
107 r = log_warning_errno(errno, "Failed to open /etc/resolv.conf: %m");
108 goto clear;
109 }
110
111 if (fstat(fileno(f), &st) < 0) {
112 r = log_error_errno(errno, "Failed to stat open file: %m");
113 goto clear;
114 }
115
116 if (file_is_our_own(&st))
117 return 0;
118
119 dns_server_mark_all(m->dns_servers);
120 dns_search_domain_mark_all(m->search_domains);
121
122 for (;;) {
123 _cleanup_free_ char *line = NULL;
124 const char *a;
125 char *l;
126
127 r = read_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 l = strstrip(line);
138 if (IN_SET(*l, '#', ';', 0))
139 continue;
140
141 a = first_word(l, "nameserver");
142 if (a) {
143 r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a);
144 if (r < 0)
145 log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
146
147 continue;
148 }
149
150 a = first_word(l, "domain");
151 if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
152 a = first_word(l, "search");
153 if (a) {
154 r = manager_parse_search_domains_and_warn(m, a);
155 if (r < 0)
156 log_warning_errno(r, "Failed to parse search domain string '%s', ignoring.", a);
157 }
158
159 log_syntax(NULL, LOG_DEBUG, "/etc/resolv.conf", n, 0, "Ignoring resolv.conf line: %s", l);
160 }
161
162 m->resolv_conf_mtime = timespec_load(&st.st_mtim);
163
164 /* Flush out all servers and search domains that are still
165 * marked. Those are then ones that didn't appear in the new
166 * /etc/resolv.conf */
167 dns_server_unlink_marked(m->dns_servers);
168 dns_search_domain_unlink_marked(m->search_domains);
169
170 /* Whenever /etc/resolv.conf changes, start using the first
171 * DNS server of it. This is useful to deal with broken
172 * network managing implementations (like NetworkManager),
173 * that when connecting to a VPN place both the VPN DNS
174 * servers and the local ones in /etc/resolv.conf. Without
175 * resetting the DNS server to use back to the first entry we
176 * will continue to use the local one thus being unable to
177 * resolve VPN domains. */
178 manager_set_dns_server(m, m->dns_servers);
179
180 /* Unconditionally flush the cache when /etc/resolv.conf is
181 * modified, even if the data it contained was completely
182 * identical to the previous version we used. We do this
183 * because altering /etc/resolv.conf is typically done when
184 * the network configuration changes, and that should be
185 * enough to flush the global unicast DNS cache. */
186 if (m->unicast_scope)
187 dns_cache_flush(&m->unicast_scope->cache);
188
189 /* If /etc/resolv.conf changed, make sure to forget everything we learned about the DNS servers. After all we
190 * might now talk to a very different DNS server that just happens to have the same IP address as an old one
191 * (think 192.168.1.1). */
192 dns_server_reset_features_all(m->dns_servers);
193
194 return 0;
195
196 clear:
197 dns_server_unlink_all(m->dns_servers);
198 dns_search_domain_unlink_all(m->search_domains);
199 return r;
200 }
201
202 static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
203 DnsScope *scope;
204
205 assert(s);
206 assert(f);
207 assert(count);
208
209 if (!dns_server_string(s)) {
210 log_warning("Out of memory, or invalid DNS address. Ignoring server.");
211 return;
212 }
213
214 /* Check if the scope this DNS server belongs to is suitable as 'default' route for lookups; resolv.conf does
215 * not have a syntax to express that, so it must not appear as a global name server to avoid routing unrelated
216 * domains to it (which is a privacy violation, will most probably fail anyway, and adds unnecessary load) */
217 scope = dns_server_scope(s);
218 if (scope && !dns_scope_is_default_route(scope)) {
219 log_debug("Scope of DNS server %s has only route-only domains, not using as global name server", dns_server_string(s));
220 return;
221 }
222
223 if (*count == MAXNS)
224 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
225 (*count)++;
226
227 fprintf(f, "nameserver %s\n", dns_server_string(s));
228 }
229
230 static void write_resolv_conf_search(
231 OrderedSet *domains,
232 FILE *f) {
233 unsigned length = 0, count = 0;
234 Iterator i;
235 char *domain;
236
237 assert(domains);
238 assert(f);
239
240 fputs("search", f);
241
242 ORDERED_SET_FOREACH(domain, domains, i) {
243 if (++count > MAXDNSRCH) {
244 fputs("\n# Too many search domains configured, remaining ones ignored.", f);
245 break;
246 }
247 length += strlen(domain) + 1;
248 if (length > 256) {
249 fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f);
250 break;
251 }
252 fputc(' ', f);
253 fputs(domain, f);
254 }
255
256 fputs("\n", f);
257 }
258
259 static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
260 Iterator i;
261
262 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\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, i)
282 write_resolv_conf_server(s, f, &count);
283 }
284
285 if (!ordered_set_isempty(domains))
286 write_resolv_conf_search(domains, f);
287
288 return fflush_and_check(f);
289 }
290
291 static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
292 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
293 "#\n"
294 "# This is a dynamic resolv.conf file for connecting local clients to the\n"
295 "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
296 "# configured search domains.\n"
297 "#\n"
298 "# Run \"resolvectl status\" to see details about the uplink DNS servers\n"
299 "# currently in use.\n"
300 "#\n"
301 "# Third party programs should typically not access this file directly, but only\n"
302 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
303 "# different way, replace this symlink by a static file or a different symlink.\n"
304 "#\n"
305 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
306 "# operation for /etc/resolv.conf.\n"
307 "\n"
308 "nameserver 127.0.0.53\n"
309 "options edns0\n", f);
310
311 if (!ordered_set_isempty(domains))
312 write_resolv_conf_search(domains, f);
313
314 return fflush_and_check(f);
315 }
316
317 int manager_write_resolv_conf(Manager *m) {
318 _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
319 _cleanup_free_ char *temp_path_uplink = NULL, *temp_path_stub = NULL;
320 _cleanup_fclose_ FILE *f_uplink = NULL, *f_stub = NULL;
321 int r;
322
323 assert(m);
324
325 /* Read the system /etc/resolv.conf first */
326 (void) manager_read_resolv_conf(m);
327
328 /* Add the full list to a set, to filter out duplicates */
329 r = manager_compile_dns_servers(m, &dns);
330 if (r < 0)
331 return log_warning_errno(r, "Failed to compile list of DNS servers: %m");
332
333 r = manager_compile_search_domains(m, &domains, false);
334 if (r < 0)
335 return log_warning_errno(r, "Failed to compile list of search domains: %m");
336
337 r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink);
338 if (r < 0)
339 return log_warning_errno(r, "Failed to open new %s for writing: %m", PRIVATE_UPLINK_RESOLV_CONF);
340
341 (void) fchmod(fileno(f_uplink), 0644);
342
343 r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);
344 if (r < 0) {
345 log_error_errno(r, "Failed to write new %s: %m", PRIVATE_UPLINK_RESOLV_CONF);
346 goto fail;
347 }
348
349 if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO) {
350 r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
351 if (r < 0) {
352 log_warning_errno(r, "Failed to open new %s for writing: %m", PRIVATE_STUB_RESOLV_CONF);
353 goto fail;
354 }
355
356 (void) fchmod(fileno(f_stub), 0644);
357
358 r = write_stub_resolv_conf_contents(f_stub, dns, domains);
359 if (r < 0) {
360 log_error_errno(r, "Failed to write new %s: %m", PRIVATE_STUB_RESOLV_CONF);
361 goto fail;
362 }
363
364 if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0)
365 r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_STUB_RESOLV_CONF);
366
367 } else {
368 r = symlink_atomic(basename(PRIVATE_UPLINK_RESOLV_CONF), PRIVATE_STUB_RESOLV_CONF);
369 if (r < 0)
370 log_error_errno(r, "Failed to symlink %s: %m", PRIVATE_STUB_RESOLV_CONF);
371 }
372
373 if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0)
374 r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF);
375
376 fail:
377 if (r < 0) {
378 /* Something went wrong, perform cleanup... */
379 (void) unlink(temp_path_uplink);
380 (void) unlink(temp_path_stub);
381 }
382
383 return r;
384 }