2 This file is part of systemd.
4 Copyright 2014 Tom Gundersen <teg@jklm.no>
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.
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.
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/>.
22 #include "alloc-util.h"
23 #include "dns-domain.h"
25 #include "fileio-label.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"
34 int manager_read_resolv_conf(Manager
*m
) {
35 _cleanup_fclose_
FILE *f
= NULL
;
43 /* Reads the system /etc/resolv.conf, if it exists and is not
44 * symlinked to our own resolv.conf instance */
46 if (!m
->read_resolv_conf
)
49 r
= stat("/etc/resolv.conf", &st
);
54 r
= log_warning_errno(errno
, "Failed to stat /etc/resolv.conf: %m");
58 /* Have we already seen the file? */
59 t
= timespec_load(&st
.st_mtim
);
60 if (t
== m
->resolv_conf_mtime
)
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
)
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
)
75 f
= fopen("/etc/resolv.conf", "re");
80 r
= log_warning_errno(errno
, "Failed to open /etc/resolv.conf: %m");
84 if (fstat(fileno(f
), &st
) < 0) {
85 r
= log_error_errno(errno
, "Failed to stat open file: %m");
89 dns_server_mark_all(m
->dns_servers
);
90 dns_search_domain_mark_all(m
->search_domains
);
92 FOREACH_LINE(line
, f
, r
= -errno
; goto clear
) {
97 if (IN_SET(*l
, '#', ';'))
100 a
= first_word(l
, "nameserver");
102 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_SYSTEM
, a
);
104 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring.", a
);
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");
113 r
= manager_parse_search_domains_and_warn(m
, a
);
115 log_warning_errno(r
, "Failed to parse search domain string '%s', ignoring.", a
);
119 m
->resolv_conf_mtime
= t
;
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
);
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
);
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
);
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
);
154 dns_server_unlink_all(m
->dns_servers
);
155 dns_search_domain_unlink_all(m
->search_domains
);
159 static void write_resolv_conf_server(DnsServer
*s
, FILE *f
, unsigned *count
) {
164 if (!dns_server_string(s
)) {
165 log_warning("Our of memory, or invalid DNS address. Ignoring server.");
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
));
180 fputs_unlocked("# Too many DNS servers configured, the following entries may be ignored.\n", f
);
183 fprintf(f
, "nameserver %s\n", dns_server_string(s
));
186 static void write_resolv_conf_search(
189 unsigned length
= 0, count
= 0;
196 fputs_unlocked("search", f
);
198 ORDERED_SET_FOREACH(domain
, domains
, i
) {
199 if (++count
> MAXDNSRCH
) {
200 fputs_unlocked("\n# Too many search domains configured, remaining ones ignored.", f
);
203 length
+= strlen(domain
) + 1;
205 fputs_unlocked("\n# Total length of all search domains is too long, remaining ones ignored.", f
);
208 fputc_unlocked(' ', f
);
209 fputs_unlocked(domain
, f
);
212 fputs_unlocked("\n", f
);
215 static int write_uplink_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
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
);
227 if (ordered_set_isempty(dns
))
228 fputs_unlocked("# No DNS servers known.\n", f
);
233 ORDERED_SET_FOREACH(s
, dns
, i
)
234 write_resolv_conf_server(s
, f
, &count
);
237 if (!ordered_set_isempty(domains
))
238 write_resolv_conf_search(domains
, f
);
240 return fflush_and_check(f
);
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
);
249 if (!ordered_set_isempty(domains
))
250 write_resolv_conf_search(domains
, f
);
252 return fflush_and_check(f
);
255 int manager_write_resolv_conf(Manager
*m
) {
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
;
266 /* Read the system /etc/resolv.conf first */
267 (void) manager_read_resolv_conf(m
);
269 /* Add the full list to a set, to filter out duplicates */
270 r
= manager_compile_dns_servers(m
, &dns
);
272 return log_warning_errno(r
, "Failed to compile list of DNS servers: %m");
274 r
= manager_compile_search_domains(m
, &domains
, false);
276 return log_warning_errno(r
, "Failed to compile list of search domains: %m");
278 r
= fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF
, PRIVATE_UPLINK_RESOLV_CONF
, &f_uplink
, &temp_path_uplink
);
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
);
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);
287 r
= write_uplink_resolv_conf_contents(f_uplink
, dns
, domains
);
289 log_error_errno(r
, "Failed to write private resolv.conf contents: %m");
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");
298 r
= write_stub_resolv_conf_contents(f_stub
, dns
, domains
);
300 log_error_errno(r
, "Failed to write private stub-resolv.conf contents: %m");
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");
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
);