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