]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-resolv-conf.c
resolve: enable RES_TRUSTAD towards the 127.0.0.53 stub resolver
[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 unsigned length = 0, count = 0;
235 Iterator i;
236 char *domain;
237
238 assert(domains);
239 assert(f);
240
241 fputs("search", f);
242
243 ORDERED_SET_FOREACH(domain, domains, i) {
244 if (++count > MAXDNSRCH) {
245 fputs("\n# Too many search domains configured, remaining ones ignored.", f);
246 break;
247 }
248 length += strlen(domain) + 1;
249 if (length > 256) {
250 fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f);
251 break;
252 }
253 fputc(' ', f);
254 fputs(domain, f);
255 }
256
257 fputs("\n", f);
258 }
259
260 static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
261 Iterator i;
262
263 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
264 "#\n"
265 "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
266 "# all known uplink DNS servers. This file lists all configured search domains.\n"
267 "#\n"
268 "# Third party programs should typically not access this file directly, but only\n"
269 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
270 "# different way, replace this symlink by a static file or a different symlink.\n"
271 "#\n"
272 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
273 "# operation for /etc/resolv.conf.\n"
274 "\n", f);
275
276 if (ordered_set_isempty(dns))
277 fputs("# No DNS servers known.\n", f);
278 else {
279 unsigned count = 0;
280 DnsServer *s;
281
282 ORDERED_SET_FOREACH(s, dns, i)
283 write_resolv_conf_server(s, f, &count);
284 }
285
286 if (!ordered_set_isempty(domains))
287 write_resolv_conf_search(domains, f);
288
289 return fflush_and_check(f);
290 }
291
292 static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
293 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
294 "#\n"
295 "# This is a dynamic resolv.conf file for connecting local clients to the\n"
296 "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
297 "# configured search domains.\n"
298 "#\n"
299 "# Run \"resolvectl status\" to see details about the uplink DNS servers\n"
300 "# currently in use.\n"
301 "#\n"
302 "# Third party programs should typically not access this file directly, but only\n"
303 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
304 "# different way, replace this symlink by a static file or a different symlink.\n"
305 "#\n"
306 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
307 "# operation for /etc/resolv.conf.\n"
308 "\n"
309 "nameserver 127.0.0.53\n"
310 "options edns0 trust-ad\n", f);
311
312 if (!ordered_set_isempty(domains))
313 write_resolv_conf_search(domains, f);
314
315 return fflush_and_check(f);
316 }
317
318 int manager_write_resolv_conf(Manager *m) {
319 _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
320 _cleanup_free_ char *temp_path_uplink = NULL, *temp_path_stub = NULL;
321 _cleanup_fclose_ FILE *f_uplink = NULL, *f_stub = NULL;
322 int r;
323
324 assert(m);
325
326 /* Read the system /etc/resolv.conf first */
327 (void) manager_read_resolv_conf(m);
328
329 /* Add the full list to a set, to filter out duplicates */
330 r = manager_compile_dns_servers(m, &dns);
331 if (r < 0)
332 return log_warning_errno(r, "Failed to compile list of DNS servers: %m");
333
334 r = manager_compile_search_domains(m, &domains, false);
335 if (r < 0)
336 return log_warning_errno(r, "Failed to compile list of search domains: %m");
337
338 r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink);
339 if (r < 0)
340 return log_warning_errno(r, "Failed to open new %s for writing: %m", PRIVATE_UPLINK_RESOLV_CONF);
341
342 (void) fchmod(fileno(f_uplink), 0644);
343
344 r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);
345 if (r < 0) {
346 log_error_errno(r, "Failed to write new %s: %m", PRIVATE_UPLINK_RESOLV_CONF);
347 goto fail;
348 }
349
350 if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO) {
351 r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
352 if (r < 0) {
353 log_warning_errno(r, "Failed to open new %s for writing: %m", PRIVATE_STUB_RESOLV_CONF);
354 goto fail;
355 }
356
357 (void) fchmod(fileno(f_stub), 0644);
358
359 r = write_stub_resolv_conf_contents(f_stub, dns, domains);
360 if (r < 0) {
361 log_error_errno(r, "Failed to write new %s: %m", PRIVATE_STUB_RESOLV_CONF);
362 goto fail;
363 }
364
365 if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0)
366 r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_STUB_RESOLV_CONF);
367
368 } else {
369 r = symlink_atomic(basename(PRIVATE_UPLINK_RESOLV_CONF), PRIVATE_STUB_RESOLV_CONF);
370 if (r < 0)
371 log_error_errno(r, "Failed to symlink %s: %m", PRIVATE_STUB_RESOLV_CONF);
372 }
373
374 if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0)
375 r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF);
376
377 fail:
378 if (r < 0) {
379 /* Something went wrong, perform cleanup... */
380 (void) unlink(temp_path_uplink);
381 (void) unlink(temp_path_stub);
382 }
383
384 return r;
385 }