]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f8dc7e34 LP |
2 | |
3 | #include <resolv.h> | |
0d536673 | 4 | #include <stdio_ext.h> |
f8dc7e34 LP |
5 | |
6 | #include "alloc-util.h" | |
7 | #include "dns-domain.h" | |
8 | #include "fd-util.h" | |
f8dc7e34 LP |
9 | #include "fileio.h" |
10 | #include "ordered-set.h" | |
11 | #include "resolved-conf.h" | |
59c0fd0e | 12 | #include "resolved-dns-server.h" |
f8dc7e34 LP |
13 | #include "resolved-resolv-conf.h" |
14 | #include "string-util.h" | |
15 | #include "strv.h" | |
e4de7287 | 16 | #include "tmpfile-util-label.h" |
f8dc7e34 | 17 | |
b6de578d | 18 | /* A resolv.conf file containing the DNS server and domain data we learnt from uplink, i.e. the full uplink data */ |
a50d7910 | 19 | #define PRIVATE_UPLINK_RESOLV_CONF "/run/systemd/resolve/resolv.conf" |
b6de578d LP |
20 | |
21 | /* A resolv.conf file containing the domain data we learnt from uplink, but our own DNS server address. */ | |
a50d7910 LP |
22 | #define PRIVATE_STUB_RESOLV_CONF "/run/systemd/resolve/stub-resolv.conf" |
23 | ||
f43580f1 | 24 | /* A static resolv.conf file containing no domains, but only our own DNS server address */ |
b6de578d LP |
25 | #define PRIVATE_STATIC_RESOLV_CONF ROOTLIBEXECDIR "/resolv.conf" |
26 | ||
f43580f1 YW |
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) { | |
052a85d1 ZJS |
58 | log_warning("DNSStubListener= is disabled, but /etc/resolv.conf is a symlink to %s " |
59 | "which expects DNSStubListener= to be enabled.", path); | |
f43580f1 YW |
60 | return -EOPNOTSUPP; |
61 | } | |
62 | } | |
63 | ||
64 | return 0; | |
65 | } | |
66 | ||
043d3928 | 67 | static bool file_is_our_own(const struct stat *st) { |
b6de578d | 68 | const char *path; |
043d3928 LP |
69 | |
70 | assert(st); | |
71 | ||
b6de578d LP |
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 | } | |
043d3928 LP |
85 | |
86 | return false; | |
87 | } | |
88 | ||
f8dc7e34 LP |
89 | int manager_read_resolv_conf(Manager *m) { |
90 | _cleanup_fclose_ FILE *f = NULL; | |
043d3928 | 91 | struct stat st; |
b351c300 | 92 | unsigned n = 0; |
f8dc7e34 LP |
93 | int r; |
94 | ||
95 | assert(m); | |
96 | ||
97 | /* Reads the system /etc/resolv.conf, if it exists and is not | |
98 | * symlinked to our own resolv.conf instance */ | |
99 | ||
100 | if (!m->read_resolv_conf) | |
101 | return 0; | |
102 | ||
103 | r = stat("/etc/resolv.conf", &st); | |
104 | if (r < 0) { | |
105 | if (errno == ENOENT) | |
bf7fabd6 LP |
106 | return 0; |
107 | ||
108 | r = log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m"); | |
f8dc7e34 LP |
109 | goto clear; |
110 | } | |
111 | ||
112 | /* Have we already seen the file? */ | |
ace68cd7 | 113 | if (timespec_load(&st.st_mtim) == m->resolv_conf_mtime) |
f8dc7e34 LP |
114 | return 0; |
115 | ||
043d3928 | 116 | if (file_is_our_own(&st)) |
bf7fabd6 | 117 | return 0; |
f8dc7e34 LP |
118 | |
119 | f = fopen("/etc/resolv.conf", "re"); | |
120 | if (!f) { | |
121 | if (errno == ENOENT) | |
bf7fabd6 LP |
122 | return 0; |
123 | ||
124 | r = log_warning_errno(errno, "Failed to open /etc/resolv.conf: %m"); | |
f8dc7e34 LP |
125 | goto clear; |
126 | } | |
127 | ||
128 | if (fstat(fileno(f), &st) < 0) { | |
129 | r = log_error_errno(errno, "Failed to stat open file: %m"); | |
130 | goto clear; | |
131 | } | |
132 | ||
043d3928 LP |
133 | if (file_is_our_own(&st)) |
134 | return 0; | |
135 | ||
4b95f179 | 136 | dns_server_mark_all(m->dns_servers); |
a51c1048 | 137 | dns_search_domain_mark_all(m->search_domains); |
f8dc7e34 | 138 | |
e1b9fc23 LP |
139 | for (;;) { |
140 | _cleanup_free_ char *line = NULL; | |
f8dc7e34 LP |
141 | const char *a; |
142 | char *l; | |
143 | ||
e1b9fc23 LP |
144 | r = read_line(f, LONG_LINE_MAX, &line); |
145 | if (r < 0) { | |
146 | log_error_errno(r, "Failed to read /etc/resolv.conf: %m"); | |
147 | goto clear; | |
148 | } | |
149 | if (r == 0) | |
150 | break; | |
151 | ||
b351c300 LP |
152 | n++; |
153 | ||
f8dc7e34 | 154 | l = strstrip(line); |
b351c300 | 155 | if (IN_SET(*l, '#', ';', 0)) |
f8dc7e34 LP |
156 | continue; |
157 | ||
158 | a = first_word(l, "nameserver"); | |
159 | if (a) { | |
2817157b | 160 | r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a); |
f8dc7e34 LP |
161 | if (r < 0) |
162 | log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a); | |
163 | ||
164 | continue; | |
165 | } | |
a51c1048 LP |
166 | |
167 | a = first_word(l, "domain"); | |
168 | if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */ | |
169 | a = first_word(l, "search"); | |
170 | if (a) { | |
171 | r = manager_parse_search_domains_and_warn(m, a); | |
172 | if (r < 0) | |
173 | log_warning_errno(r, "Failed to parse search domain string '%s', ignoring.", a); | |
174 | } | |
b351c300 LP |
175 | |
176 | log_syntax(NULL, LOG_DEBUG, "/etc/resolv.conf", n, 0, "Ignoring resolv.conf line: %s", l); | |
f8dc7e34 LP |
177 | } |
178 | ||
ace68cd7 | 179 | m->resolv_conf_mtime = timespec_load(&st.st_mtim); |
bf7fabd6 | 180 | |
a51c1048 LP |
181 | /* Flush out all servers and search domains that are still |
182 | * marked. Those are then ones that didn't appear in the new | |
183 | * /etc/resolv.conf */ | |
4b95f179 | 184 | dns_server_unlink_marked(m->dns_servers); |
a51c1048 | 185 | dns_search_domain_unlink_marked(m->search_domains); |
f8dc7e34 LP |
186 | |
187 | /* Whenever /etc/resolv.conf changes, start using the first | |
188 | * DNS server of it. This is useful to deal with broken | |
189 | * network managing implementations (like NetworkManager), | |
190 | * that when connecting to a VPN place both the VPN DNS | |
191 | * servers and the local ones in /etc/resolv.conf. Without | |
192 | * resetting the DNS server to use back to the first entry we | |
193 | * will continue to use the local one thus being unable to | |
194 | * resolve VPN domains. */ | |
195 | manager_set_dns_server(m, m->dns_servers); | |
196 | ||
452b4e32 LP |
197 | /* Unconditionally flush the cache when /etc/resolv.conf is |
198 | * modified, even if the data it contained was completely | |
199 | * identical to the previous version we used. We do this | |
200 | * because altering /etc/resolv.conf is typically done when | |
201 | * the network configuration changes, and that should be | |
202 | * enough to flush the global unicast DNS cache. */ | |
203 | if (m->unicast_scope) | |
204 | dns_cache_flush(&m->unicast_scope->cache); | |
205 | ||
59c0fd0e LP |
206 | /* If /etc/resolv.conf changed, make sure to forget everything we learned about the DNS servers. After all we |
207 | * might now talk to a very different DNS server that just happens to have the same IP address as an old one | |
208 | * (think 192.168.1.1). */ | |
209 | dns_server_reset_features_all(m->dns_servers); | |
210 | ||
f8dc7e34 LP |
211 | return 0; |
212 | ||
213 | clear: | |
4b95f179 | 214 | dns_server_unlink_all(m->dns_servers); |
a51c1048 | 215 | dns_search_domain_unlink_all(m->search_domains); |
f8dc7e34 LP |
216 | return r; |
217 | } | |
218 | ||
219 | static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) { | |
f76fa088 LP |
220 | DnsScope *scope; |
221 | ||
f8dc7e34 LP |
222 | assert(s); |
223 | assert(f); | |
224 | assert(count); | |
225 | ||
2817157b | 226 | if (!dns_server_string(s)) { |
be4bf266 | 227 | log_warning("Out of memory, or invalid DNS address. Ignoring server."); |
f8dc7e34 LP |
228 | return; |
229 | } | |
230 | ||
f76fa088 LP |
231 | /* Check if the scope this DNS server belongs to is limited to particular domains; resolv.conf does not have a |
232 | * syntax to express that, so it must not appear as a global name server to avoid routing unrelated domains to | |
233 | * it (which is a privacy violation, will most probably fail anyway, and adds unnecessary load) */ | |
234 | scope = dns_server_scope(s); | |
235 | if (scope && dns_scope_has_route_only_domains(scope)) { | |
236 | log_debug("Scope of DNS server %s has only route-only domains, not using as global name server", dns_server_string(s)); | |
b9fe94ca MP |
237 | return; |
238 | } | |
239 | ||
f8dc7e34 | 240 | if (*count == MAXNS) |
0d536673 | 241 | fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f); |
313cefa1 | 242 | (*count)++; |
f8dc7e34 | 243 | |
2817157b | 244 | fprintf(f, "nameserver %s\n", dns_server_string(s)); |
f8dc7e34 LP |
245 | } |
246 | ||
247 | static void write_resolv_conf_search( | |
d2bc1251 MP |
248 | OrderedSet *domains, |
249 | FILE *f) { | |
250 | unsigned length = 0, count = 0; | |
251 | Iterator i; | |
252 | char *domain; | |
f8dc7e34 | 253 | |
d2bc1251 | 254 | assert(domains); |
f8dc7e34 | 255 | assert(f); |
f8dc7e34 | 256 | |
0d536673 | 257 | fputs("search", f); |
f8dc7e34 | 258 | |
d2bc1251 MP |
259 | ORDERED_SET_FOREACH(domain, domains, i) { |
260 | if (++count > MAXDNSRCH) { | |
0d536673 | 261 | fputs("\n# Too many search domains configured, remaining ones ignored.", f); |
d2bc1251 MP |
262 | break; |
263 | } | |
264 | length += strlen(domain) + 1; | |
265 | if (length > 256) { | |
0d536673 | 266 | fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f); |
d2bc1251 MP |
267 | break; |
268 | } | |
0d536673 LP |
269 | fputc(' ', f); |
270 | fputs(domain, f); | |
f8dc7e34 LP |
271 | } |
272 | ||
0d536673 | 273 | fputs("\n", f); |
f8dc7e34 LP |
274 | } |
275 | ||
e6b2d948 | 276 | static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) { |
f8dc7e34 LP |
277 | Iterator i; |
278 | ||
0d536673 LP |
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); | |
f8dc7e34 LP |
291 | |
292 | if (ordered_set_isempty(dns)) | |
0d536673 | 293 | fputs("# No DNS servers known.\n", f); |
f8dc7e34 LP |
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 | ||
d2bc1251 MP |
302 | if (!ordered_set_isempty(domains)) |
303 | write_resolv_conf_search(domains, f); | |
f8dc7e34 LP |
304 | |
305 | return fflush_and_check(f); | |
306 | } | |
307 | ||
e6b2d948 | 308 | static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) { |
d937ef74 LP |
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" | |
5bc53fee | 315 | "# Run \"resolvectl status\" to see details about the uplink DNS servers\n" |
d937ef74 LP |
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" | |
93158c77 TA |
325 | "nameserver 127.0.0.53\n" |
326 | "options edns0\n", f); | |
e6b2d948 DJL |
327 | |
328 | if (!ordered_set_isempty(domains)) | |
329 | write_resolv_conf_search(domains, f); | |
330 | ||
331 | return fflush_and_check(f); | |
332 | } | |
333 | ||
f8dc7e34 | 334 | int manager_write_resolv_conf(Manager *m) { |
9176a57c | 335 | |
9176a57c | 336 | _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL; |
0d536673 LP |
337 | _cleanup_free_ char *temp_path_uplink = NULL, *temp_path_stub = NULL; |
338 | _cleanup_fclose_ FILE *f_uplink = NULL, *f_stub = NULL; | |
f8dc7e34 LP |
339 | int r; |
340 | ||
341 | assert(m); | |
342 | ||
343 | /* Read the system /etc/resolv.conf first */ | |
7207052d | 344 | (void) manager_read_resolv_conf(m); |
f8dc7e34 LP |
345 | |
346 | /* Add the full list to a set, to filter out duplicates */ | |
9176a57c LP |
347 | r = manager_compile_dns_servers(m, &dns); |
348 | if (r < 0) | |
7207052d | 349 | return log_warning_errno(r, "Failed to compile list of DNS servers: %m"); |
f8dc7e34 | 350 | |
6f7da49d | 351 | r = manager_compile_search_domains(m, &domains, false); |
9176a57c | 352 | if (r < 0) |
7207052d | 353 | return log_warning_errno(r, "Failed to compile list of search domains: %m"); |
f8dc7e34 | 354 | |
e6b2d948 | 355 | r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink); |
f8dc7e34 | 356 | if (r < 0) |
7207052d | 357 | return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m"); |
0d536673 LP |
358 | |
359 | (void) __fsetlocking(f_uplink, FSETLOCKING_BYCALLER); | |
360 | (void) fchmod(fileno(f_uplink), 0644); | |
361 | ||
e6b2d948 DJL |
362 | r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub); |
363 | if (r < 0) | |
364 | return log_warning_errno(r, "Failed to open private stub-resolv.conf file for writing: %m"); | |
0d536673 LP |
365 | |
366 | (void) __fsetlocking(f_stub, FSETLOCKING_BYCALLER); | |
e6b2d948 | 367 | (void) fchmod(fileno(f_stub), 0644); |
f8dc7e34 | 368 | |
e6b2d948 | 369 | r = write_uplink_resolv_conf_contents(f_uplink, dns, domains); |
7207052d LP |
370 | if (r < 0) { |
371 | log_error_errno(r, "Failed to write private resolv.conf contents: %m"); | |
f8dc7e34 | 372 | goto fail; |
7207052d | 373 | } |
f8dc7e34 | 374 | |
e6b2d948 | 375 | if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0) { |
7207052d | 376 | r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m"); |
f8dc7e34 LP |
377 | goto fail; |
378 | } | |
379 | ||
e6b2d948 DJL |
380 | r = write_stub_resolv_conf_contents(f_stub, dns, domains); |
381 | if (r < 0) { | |
382 | log_error_errno(r, "Failed to write private stub-resolv.conf contents: %m"); | |
383 | goto fail; | |
384 | } | |
385 | ||
386 | if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0) { | |
387 | r = log_error_errno(errno, "Failed to move private stub-resolv.conf file into place: %m"); | |
388 | goto fail; | |
389 | } | |
390 | ||
f8dc7e34 LP |
391 | return 0; |
392 | ||
393 | fail: | |
e6b2d948 DJL |
394 | (void) unlink(PRIVATE_UPLINK_RESOLV_CONF); |
395 | (void) unlink(temp_path_uplink); | |
396 | (void) unlink(PRIVATE_STUB_RESOLV_CONF); | |
397 | (void) unlink(temp_path_stub); | |
7207052d | 398 | |
f8dc7e34 LP |
399 | return r; |
400 | } |