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