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