]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-resolv-conf.c
resolved: automatically forget all learnt DNS server information when the network...
[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_RESOLV_CONF, &own) >= 0 &&
65 st.st_dev == own.st_dev &&
66 st.st_ino == own.st_ino)
67 return 0;
68
69 f = fopen("/etc/resolv.conf", "re");
70 if (!f) {
71 if (errno == ENOENT)
72 return 0;
73
74 r = log_warning_errno(errno, "Failed to open /etc/resolv.conf: %m");
75 goto clear;
76 }
77
78 if (fstat(fileno(f), &st) < 0) {
79 r = log_error_errno(errno, "Failed to stat open file: %m");
80 goto clear;
81 }
82
83 dns_server_mark_all(m->dns_servers);
84 dns_search_domain_mark_all(m->search_domains);
85
86 FOREACH_LINE(line, f, r = -errno; goto clear) {
87 const char *a;
88 char *l;
89
90 l = strstrip(line);
91 if (IN_SET(*l, '#', ';'))
92 continue;
93
94 a = first_word(l, "nameserver");
95 if (a) {
96 r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a);
97 if (r < 0)
98 log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
99
100 continue;
101 }
102
103 a = first_word(l, "domain");
104 if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
105 a = first_word(l, "search");
106 if (a) {
107 r = manager_parse_search_domains_and_warn(m, a);
108 if (r < 0)
109 log_warning_errno(r, "Failed to parse search domain string '%s', ignoring.", a);
110 }
111 }
112
113 m->resolv_conf_mtime = t;
114
115 /* Flush out all servers and search domains that are still
116 * marked. Those are then ones that didn't appear in the new
117 * /etc/resolv.conf */
118 dns_server_unlink_marked(m->dns_servers);
119 dns_search_domain_unlink_marked(m->search_domains);
120
121 /* Whenever /etc/resolv.conf changes, start using the first
122 * DNS server of it. This is useful to deal with broken
123 * network managing implementations (like NetworkManager),
124 * that when connecting to a VPN place both the VPN DNS
125 * servers and the local ones in /etc/resolv.conf. Without
126 * resetting the DNS server to use back to the first entry we
127 * will continue to use the local one thus being unable to
128 * resolve VPN domains. */
129 manager_set_dns_server(m, m->dns_servers);
130
131 /* Unconditionally flush the cache when /etc/resolv.conf is
132 * modified, even if the data it contained was completely
133 * identical to the previous version we used. We do this
134 * because altering /etc/resolv.conf is typically done when
135 * the network configuration changes, and that should be
136 * enough to flush the global unicast DNS cache. */
137 if (m->unicast_scope)
138 dns_cache_flush(&m->unicast_scope->cache);
139
140 /* If /etc/resolv.conf changed, make sure to forget everything we learned about the DNS servers. After all we
141 * might now talk to a very different DNS server that just happens to have the same IP address as an old one
142 * (think 192.168.1.1). */
143 dns_server_reset_features_all(m->dns_servers);
144
145 return 0;
146
147 clear:
148 dns_server_unlink_all(m->dns_servers);
149 dns_search_domain_unlink_all(m->search_domains);
150 return r;
151 }
152
153 static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
154 assert(s);
155 assert(f);
156 assert(count);
157
158 if (!dns_server_string(s)) {
159 log_warning("Our of memory, or invalid DNS address. Ignoring server.");
160 return;
161 }
162
163 /* Check if the DNS server is limited to particular domains;
164 * resolv.conf does not have a syntax to express that, so it must not
165 * appear as a global name server to avoid routing unrelated domains to
166 * it (which is a privacy violation, will most probably fail anyway,
167 * and adds unnecessary load) */
168 if (dns_server_limited_domains(s)) {
169 log_debug("DNS server %s has route-only domains, not using as global name server", dns_server_string(s));
170 return;
171 }
172
173 if (*count == MAXNS)
174 fputs_unlocked("# Too many DNS servers configured, the following entries may be ignored.\n", f);
175 (*count)++;
176
177 fprintf(f, "nameserver %s\n", dns_server_string(s));
178 }
179
180 static void write_resolv_conf_search(
181 OrderedSet *domains,
182 FILE *f) {
183 unsigned length = 0, count = 0;
184 Iterator i;
185 char *domain;
186
187 assert(domains);
188 assert(f);
189
190 fputs_unlocked("search", f);
191
192 ORDERED_SET_FOREACH(domain, domains, i) {
193 if (++count > MAXDNSRCH) {
194 fputs_unlocked("\n# Too many search domains configured, remaining ones ignored.", f);
195 break;
196 }
197 length += strlen(domain) + 1;
198 if (length > 256) {
199 fputs_unlocked("\n# Total length of all search domains is too long, remaining ones ignored.", f);
200 break;
201 }
202 fputc_unlocked(' ', f);
203 fputs_unlocked(domain, f);
204 }
205
206 fputs_unlocked("\n", f);
207 }
208
209 static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
210 Iterator i;
211
212 fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n#\n"
213 "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
214 "# all known DNS servers.\n#\n"
215 "# Third party programs must not access this file directly, but only through the\n"
216 "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n"
217 "# replace this symlink by a static file or a different symlink.\n#\n"
218 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
219 "# operation for /etc/resolv.conf.\n\n", f);
220
221 if (ordered_set_isempty(dns))
222 fputs_unlocked("# No DNS servers known.\n", f);
223 else {
224 unsigned count = 0;
225 DnsServer *s;
226
227 ORDERED_SET_FOREACH(s, dns, i)
228 write_resolv_conf_server(s, f, &count);
229 }
230
231 if (!ordered_set_isempty(domains))
232 write_resolv_conf_search(domains, f);
233
234 return fflush_and_check(f);
235 }
236
237 int manager_write_resolv_conf(Manager *m) {
238
239 _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
240 _cleanup_free_ char *temp_path = NULL;
241 _cleanup_fclose_ FILE *f = NULL;
242 int r;
243
244 assert(m);
245
246 /* Read the system /etc/resolv.conf first */
247 (void) manager_read_resolv_conf(m);
248
249 /* Add the full list to a set, to filter out duplicates */
250 r = manager_compile_dns_servers(m, &dns);
251 if (r < 0)
252 return log_warning_errno(r, "Failed to compile list of DNS servers: %m");
253
254 r = manager_compile_search_domains(m, &domains, false);
255 if (r < 0)
256 return log_warning_errno(r, "Failed to compile list of search domains: %m");
257
258 r = fopen_temporary_label(PRIVATE_RESOLV_CONF, PRIVATE_RESOLV_CONF, &f, &temp_path);
259 if (r < 0)
260 return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m");
261
262 (void) fchmod(fileno(f), 0644);
263
264 r = write_resolv_conf_contents(f, dns, domains);
265 if (r < 0) {
266 log_error_errno(r, "Failed to write private resolv.conf contents: %m");
267 goto fail;
268 }
269
270 if (rename(temp_path, PRIVATE_RESOLV_CONF) < 0) {
271 r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m");
272 goto fail;
273 }
274
275 return 0;
276
277 fail:
278 (void) unlink(PRIVATE_RESOLV_CONF);
279 (void) unlink(temp_path);
280
281 return r;
282 }