]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-resolv-conf.c
Merge pull request #16687 from DaanDeMeyer/bootloader-machine-id
[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 "stat-util.h"
18 #include "string-util.h"
19 #include "strv.h"
20 #include "tmpfile-util-label.h"
21
22 int manager_check_resolv_conf(const Manager *m) {
23 struct stat st, own;
24
25 assert(m);
26
27 /* This warns only when our stub listener is disabled and /etc/resolv.conf is a symlink to
28 * PRIVATE_STATIC_RESOLV_CONF. */
29
30 if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO)
31 return 0;
32
33 if (stat("/etc/resolv.conf", &st) < 0) {
34 if (errno == ENOENT)
35 return 0;
36
37 return log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m");
38 }
39
40 /* Is it symlinked to our own uplink file? */
41 if (stat(PRIVATE_STATIC_RESOLV_CONF, &own) >= 0 &&
42 st.st_dev == own.st_dev &&
43 st.st_ino == own.st_ino)
44 return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
45 "DNSStubListener= is disabled, but /etc/resolv.conf is a symlink to "
46 PRIVATE_STATIC_RESOLV_CONF " which expects DNSStubListener= to be enabled.");
47
48 return 0;
49 }
50
51 static bool file_is_our_own(const struct stat *st) {
52 const char *path;
53
54 assert(st);
55
56 FOREACH_STRING(path,
57 PRIVATE_UPLINK_RESOLV_CONF,
58 PRIVATE_STUB_RESOLV_CONF,
59 PRIVATE_STATIC_RESOLV_CONF) {
60
61 struct stat own;
62
63 /* Is it symlinked to our own uplink file? */
64 if (stat(path, &own) >= 0 &&
65 st->st_dev == own.st_dev &&
66 st->st_ino == own.st_ino)
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 /etc/resolv.conf: %m");
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 char *l;
127
128 r = read_line(f, LONG_LINE_MAX, &line);
129 if (r < 0) {
130 log_error_errno(r, "Failed to read /etc/resolv.conf: %m");
131 goto clear;
132 }
133 if (r == 0)
134 break;
135
136 n++;
137
138 l = strstrip(line);
139 if (IN_SET(*l, '#', ';', 0))
140 continue;
141
142 a = first_word(l, "nameserver");
143 if (a) {
144 r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a);
145 if (r < 0)
146 log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
147
148 continue;
149 }
150
151 a = first_word(l, "domain");
152 if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
153 a = first_word(l, "search");
154 if (a) {
155 r = manager_parse_search_domains_and_warn(m, a);
156 if (r < 0)
157 log_warning_errno(r, "Failed to parse search domain string '%s', ignoring.", a);
158 }
159
160 log_syntax(NULL, LOG_DEBUG, "/etc/resolv.conf", n, 0, "Ignoring resolv.conf line: %s", l);
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 /* Check if the scope this DNS server belongs to is suitable as 'default' route for lookups; resolv.conf does
216 * not have a syntax to express that, so it must not appear as a global name server to avoid routing unrelated
217 * domains to it (which is a privacy violation, will most probably fail anyway, and adds unnecessary load) */
218 scope = dns_server_scope(s);
219 if (scope && !dns_scope_is_default_route(scope)) {
220 log_debug("Scope of DNS server %s has only route-only domains, not using as global name server", dns_server_string(s));
221 return;
222 }
223
224 if (*count == MAXNS)
225 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
226 (*count)++;
227
228 fprintf(f, "nameserver %s\n", dns_server_string(s));
229 }
230
231 static void write_resolv_conf_search(
232 OrderedSet *domains,
233 FILE *f) {
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 fputc(' ', f);
244 fputs(domain, f);
245 }
246
247 fputs("\n", f);
248 }
249
250 static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
251 Iterator i;
252
253 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
254 "#\n"
255 "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
256 "# all known uplink DNS servers. This file lists all configured search domains.\n"
257 "#\n"
258 "# Third party programs should typically not access this file directly, but only\n"
259 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
260 "# different way, replace this symlink by a static file or a different symlink.\n"
261 "#\n"
262 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
263 "# operation for /etc/resolv.conf.\n"
264 "\n", f);
265
266 if (ordered_set_isempty(dns))
267 fputs("# No DNS servers known.\n", f);
268 else {
269 unsigned count = 0;
270 DnsServer *s;
271
272 ORDERED_SET_FOREACH(s, dns, i)
273 write_resolv_conf_server(s, f, &count);
274 }
275
276 if (!ordered_set_isempty(domains))
277 write_resolv_conf_search(domains, f);
278
279 return fflush_and_check(f);
280 }
281
282 static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
283 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
284 "#\n"
285 "# This is a dynamic resolv.conf file for connecting local clients to the\n"
286 "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
287 "# configured search domains.\n"
288 "#\n"
289 "# Run \"resolvectl status\" to see details about the uplink DNS servers\n"
290 "# currently in use.\n"
291 "#\n"
292 "# Third party programs should typically not access this file directly, but only\n"
293 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
294 "# different way, replace this symlink by a static file or a different symlink.\n"
295 "#\n"
296 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
297 "# operation for /etc/resolv.conf.\n"
298 "\n"
299 "nameserver 127.0.0.53\n"
300 "options edns0 trust-ad\n", f);
301
302 if (!ordered_set_isempty(domains))
303 write_resolv_conf_search(domains, f);
304
305 return fflush_and_check(f);
306 }
307
308 int manager_write_resolv_conf(Manager *m) {
309 _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
310 _cleanup_free_ char *temp_path_uplink = NULL, *temp_path_stub = NULL;
311 _cleanup_fclose_ FILE *f_uplink = NULL, *f_stub = NULL;
312 int r;
313
314 assert(m);
315
316 /* Read the system /etc/resolv.conf first */
317 (void) manager_read_resolv_conf(m);
318
319 /* Add the full list to a set, to filter out duplicates */
320 r = manager_compile_dns_servers(m, &dns);
321 if (r < 0)
322 return log_warning_errno(r, "Failed to compile list of DNS servers: %m");
323
324 r = manager_compile_search_domains(m, &domains, false);
325 if (r < 0)
326 return log_warning_errno(r, "Failed to compile list of search domains: %m");
327
328 r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink);
329 if (r < 0)
330 return log_warning_errno(r, "Failed to open new %s for writing: %m", PRIVATE_UPLINK_RESOLV_CONF);
331
332 (void) fchmod(fileno(f_uplink), 0644);
333
334 r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);
335 if (r < 0) {
336 log_error_errno(r, "Failed to write new %s: %m", PRIVATE_UPLINK_RESOLV_CONF);
337 goto fail;
338 }
339
340 if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO) {
341 r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
342 if (r < 0) {
343 log_warning_errno(r, "Failed to open new %s for writing: %m", PRIVATE_STUB_RESOLV_CONF);
344 goto fail;
345 }
346
347 (void) fchmod(fileno(f_stub), 0644);
348
349 r = write_stub_resolv_conf_contents(f_stub, dns, domains);
350 if (r < 0) {
351 log_error_errno(r, "Failed to write new %s: %m", PRIVATE_STUB_RESOLV_CONF);
352 goto fail;
353 }
354
355 if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0)
356 r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_STUB_RESOLV_CONF);
357
358 } else {
359 r = symlink_atomic(basename(PRIVATE_UPLINK_RESOLV_CONF), PRIVATE_STUB_RESOLV_CONF);
360 if (r < 0)
361 log_error_errno(r, "Failed to symlink %s: %m", PRIVATE_STUB_RESOLV_CONF);
362 }
363
364 if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0)
365 r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF);
366
367 fail:
368 if (r < 0) {
369 /* Something went wrong, perform cleanup... */
370 (void) unlink(temp_path_uplink);
371 (void) unlink(temp_path_stub);
372 }
373
374 return r;
375 }