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