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