]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-resolv-conf.c
Merge pull request #9489 from keszybz/copyright-removal-3
[thirdparty/systemd.git] / src / resolve / resolved-resolv-conf.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <resolv.h>
4 #include <stdio_ext.h>
5
6 #include "alloc-util.h"
7 #include "dns-domain.h"
8 #include "fd-util.h"
9 #include "fileio-label.h"
10 #include "fileio.h"
11 #include "ordered-set.h"
12 #include "resolved-conf.h"
13 #include "resolved-dns-server.h"
14 #include "resolved-resolv-conf.h"
15 #include "string-util.h"
16 #include "strv.h"
17
18 /* A resolv.conf file containing the DNS server and domain data we learnt from uplink, i.e. the full uplink data */
19 #define PRIVATE_UPLINK_RESOLV_CONF "/run/systemd/resolve/resolv.conf"
20
21 /* A resolv.conf file containing the domain data we learnt from uplink, but our own DNS server address. */
22 #define PRIVATE_STUB_RESOLV_CONF "/run/systemd/resolve/stub-resolv.conf"
23
24 /* A static resolv.conf file containing no domains, but only our own DNS server address */
25 #define PRIVATE_STATIC_RESOLV_CONF ROOTLIBEXECDIR "/resolv.conf"
26
27 int manager_check_resolv_conf(const Manager *m) {
28 const char *path;
29 struct stat st;
30 int r;
31
32 assert(m);
33
34 /* This warns only when our stub listener is disabled and /etc/resolv.conf is a symlink to
35 * PRIVATE_STATIC_RESOLV_CONF or PRIVATE_STUB_RESOLV_CONF. */
36
37 if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO)
38 return 0;
39
40 r = stat("/etc/resolv.conf", &st);
41 if (r < 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 FOREACH_STRING(path,
49 PRIVATE_STUB_RESOLV_CONF,
50 PRIVATE_STATIC_RESOLV_CONF) {
51
52 struct stat own;
53
54 /* Is it symlinked to our own uplink file? */
55 if (stat(path, &own) >= 0 &&
56 st.st_dev == own.st_dev &&
57 st.st_ino == own.st_ino) {
58 log_warning("DNSStubListener= is disabled, but /etc/resolv.conf is a symlink to %s "
59 "which expects DNSStubListener= to be enabled.", path);
60 return -EOPNOTSUPP;
61 }
62 }
63
64 return 0;
65 }
66
67 static bool file_is_our_own(const struct stat *st) {
68 const char *path;
69
70 assert(st);
71
72 FOREACH_STRING(path,
73 PRIVATE_UPLINK_RESOLV_CONF,
74 PRIVATE_STUB_RESOLV_CONF,
75 PRIVATE_STATIC_RESOLV_CONF) {
76
77 struct stat own;
78
79 /* Is it symlinked to our own uplink file? */
80 if (stat(path, &own) >= 0 &&
81 st->st_dev == own.st_dev &&
82 st->st_ino == own.st_ino)
83 return true;
84 }
85
86 return false;
87 }
88
89 int manager_read_resolv_conf(Manager *m) {
90 _cleanup_fclose_ FILE *f = NULL;
91 struct stat st;
92 char line[LINE_MAX];
93 unsigned n = 0;
94 int r;
95
96 assert(m);
97
98 /* Reads the system /etc/resolv.conf, if it exists and is not
99 * symlinked to our own resolv.conf instance */
100
101 if (!m->read_resolv_conf)
102 return 0;
103
104 r = stat("/etc/resolv.conf", &st);
105 if (r < 0) {
106 if (errno == ENOENT)
107 return 0;
108
109 r = log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m");
110 goto clear;
111 }
112
113 /* Have we already seen the file? */
114 if (timespec_load(&st.st_mtim) == m->resolv_conf_mtime)
115 return 0;
116
117 if (file_is_our_own(&st))
118 return 0;
119
120 f = fopen("/etc/resolv.conf", "re");
121 if (!f) {
122 if (errno == ENOENT)
123 return 0;
124
125 r = log_warning_errno(errno, "Failed to open /etc/resolv.conf: %m");
126 goto clear;
127 }
128
129 if (fstat(fileno(f), &st) < 0) {
130 r = log_error_errno(errno, "Failed to stat open file: %m");
131 goto clear;
132 }
133
134 if (file_is_our_own(&st))
135 return 0;
136
137 dns_server_mark_all(m->dns_servers);
138 dns_search_domain_mark_all(m->search_domains);
139
140 FOREACH_LINE(line, f, r = -errno; goto clear) {
141 const char *a;
142 char *l;
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 assert(s);
213 assert(f);
214 assert(count);
215
216 if (!dns_server_string(s)) {
217 log_warning("Out of memory, or invalid DNS address. Ignoring server.");
218 return;
219 }
220
221 /* Check if the DNS server is limited to particular domains;
222 * resolv.conf does not have a syntax to express that, so it must not
223 * appear as a global name server to avoid routing unrelated domains to
224 * it (which is a privacy violation, will most probably fail anyway,
225 * and adds unnecessary load) */
226 if (dns_server_limited_domains(s)) {
227 log_debug("DNS server %s has route-only domains, not using as global name server", dns_server_string(s));
228 return;
229 }
230
231 if (*count == MAXNS)
232 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
233 (*count)++;
234
235 fprintf(f, "nameserver %s\n", dns_server_string(s));
236 }
237
238 static void write_resolv_conf_search(
239 OrderedSet *domains,
240 FILE *f) {
241 unsigned length = 0, count = 0;
242 Iterator i;
243 char *domain;
244
245 assert(domains);
246 assert(f);
247
248 fputs("search", f);
249
250 ORDERED_SET_FOREACH(domain, domains, i) {
251 if (++count > MAXDNSRCH) {
252 fputs("\n# Too many search domains configured, remaining ones ignored.", f);
253 break;
254 }
255 length += strlen(domain) + 1;
256 if (length > 256) {
257 fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f);
258 break;
259 }
260 fputc(' ', f);
261 fputs(domain, f);
262 }
263
264 fputs("\n", f);
265 }
266
267 static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
268 Iterator i;
269
270 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
271 "#\n"
272 "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
273 "# all known uplink DNS servers. This file lists all configured search domains.\n"
274 "#\n"
275 "# Third party programs must not access this file directly, but only through the\n"
276 "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n"
277 "# replace this symlink by a static file or a different symlink.\n"
278 "#\n"
279 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
280 "# operation for /etc/resolv.conf.\n"
281 "\n", f);
282
283 if (ordered_set_isempty(dns))
284 fputs("# No DNS servers known.\n", f);
285 else {
286 unsigned count = 0;
287 DnsServer *s;
288
289 ORDERED_SET_FOREACH(s, dns, i)
290 write_resolv_conf_server(s, f, &count);
291 }
292
293 if (!ordered_set_isempty(domains))
294 write_resolv_conf_search(domains, f);
295
296 return fflush_and_check(f);
297 }
298
299 static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
300 fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
301 "#\n"
302 "# This is a dynamic resolv.conf file for connecting local clients to the\n"
303 "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
304 "# configured search domains.\n"
305 "#\n"
306 "# Run \"resolvectl status\" to see details about the uplink DNS servers\n"
307 "# currently in use.\n"
308 "#\n"
309 "# Third party programs must not access this file directly, but only through the\n"
310 "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n"
311 "# replace this symlink by a static file or a different symlink.\n"
312 "#\n"
313 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
314 "# operation for /etc/resolv.conf.\n"
315 "\n"
316 "nameserver 127.0.0.53\n", f);
317
318 if (!ordered_set_isempty(domains))
319 write_resolv_conf_search(domains, f);
320
321 return fflush_and_check(f);
322 }
323
324 int manager_write_resolv_conf(Manager *m) {
325
326 _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
327 _cleanup_free_ char *temp_path_uplink = NULL, *temp_path_stub = NULL;
328 _cleanup_fclose_ FILE *f_uplink = NULL, *f_stub = NULL;
329 int r;
330
331 assert(m);
332
333 /* Read the system /etc/resolv.conf first */
334 (void) manager_read_resolv_conf(m);
335
336 /* Add the full list to a set, to filter out duplicates */
337 r = manager_compile_dns_servers(m, &dns);
338 if (r < 0)
339 return log_warning_errno(r, "Failed to compile list of DNS servers: %m");
340
341 r = manager_compile_search_domains(m, &domains, false);
342 if (r < 0)
343 return log_warning_errno(r, "Failed to compile list of search domains: %m");
344
345 r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink);
346 if (r < 0)
347 return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m");
348
349 (void) __fsetlocking(f_uplink, FSETLOCKING_BYCALLER);
350 (void) fchmod(fileno(f_uplink), 0644);
351
352 r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
353 if (r < 0)
354 return log_warning_errno(r, "Failed to open private stub-resolv.conf file for writing: %m");
355
356 (void) __fsetlocking(f_stub, FSETLOCKING_BYCALLER);
357 (void) fchmod(fileno(f_stub), 0644);
358
359 r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);
360 if (r < 0) {
361 log_error_errno(r, "Failed to write private resolv.conf contents: %m");
362 goto fail;
363 }
364
365 if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0) {
366 r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m");
367 goto fail;
368 }
369
370 r = write_stub_resolv_conf_contents(f_stub, dns, domains);
371 if (r < 0) {
372 log_error_errno(r, "Failed to write private stub-resolv.conf contents: %m");
373 goto fail;
374 }
375
376 if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0) {
377 r = log_error_errno(errno, "Failed to move private stub-resolv.conf file into place: %m");
378 goto fail;
379 }
380
381 return 0;
382
383 fail:
384 (void) unlink(PRIVATE_UPLINK_RESOLV_CONF);
385 (void) unlink(temp_path_uplink);
386 (void) unlink(PRIVATE_STUB_RESOLV_CONF);
387 (void) unlink(temp_path_stub);
388
389 return r;
390 }