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_RESOLV_CONF
, &own
) >= 0 &&
65 st
.st_dev
== own
.st_dev
&&
66 st
.st_ino
== own
.st_ino
)
69 f
= fopen("/etc/resolv.conf", "re");
74 r
= log_warning_errno(errno
, "Failed to open /etc/resolv.conf: %m");
78 if (fstat(fileno(f
), &st
) < 0) {
79 r
= log_error_errno(errno
, "Failed to stat open file: %m");
83 dns_server_mark_all(m
->dns_servers
);
84 dns_search_domain_mark_all(m
->search_domains
);
86 FOREACH_LINE(line
, f
, r
= -errno
; goto clear
) {
91 if (IN_SET(*l
, '#', ';'))
94 a
= first_word(l
, "nameserver");
96 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_SYSTEM
, a
);
98 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring.", a
);
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");
107 r
= manager_parse_search_domains_and_warn(m
, a
);
109 log_warning_errno(r
, "Failed to parse search domain string '%s', ignoring.", a
);
113 m
->resolv_conf_mtime
= t
;
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
);
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
);
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
);
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
);
148 dns_server_unlink_all(m
->dns_servers
);
149 dns_search_domain_unlink_all(m
->search_domains
);
153 static void write_resolv_conf_server(DnsServer
*s
, FILE *f
, unsigned *count
) {
158 if (!dns_server_string(s
)) {
159 log_warning("Our of memory, or invalid DNS address. Ignoring server.");
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
));
174 fputs_unlocked("# Too many DNS servers configured, the following entries may be ignored.\n", f
);
177 fprintf(f
, "nameserver %s\n", dns_server_string(s
));
180 static void write_resolv_conf_search(
183 unsigned length
= 0, count
= 0;
190 fputs_unlocked("search", f
);
192 ORDERED_SET_FOREACH(domain
, domains
, i
) {
193 if (++count
> MAXDNSRCH
) {
194 fputs_unlocked("\n# Too many search domains configured, remaining ones ignored.", f
);
197 length
+= strlen(domain
) + 1;
199 fputs_unlocked("\n# Total length of all search domains is too long, remaining ones ignored.", f
);
202 fputc_unlocked(' ', f
);
203 fputs_unlocked(domain
, f
);
206 fputs_unlocked("\n", f
);
209 static int write_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
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
);
221 if (ordered_set_isempty(dns
))
222 fputs_unlocked("# No DNS servers known.\n", f
);
227 ORDERED_SET_FOREACH(s
, dns
, i
)
228 write_resolv_conf_server(s
, f
, &count
);
231 if (!ordered_set_isempty(domains
))
232 write_resolv_conf_search(domains
, f
);
234 return fflush_and_check(f
);
237 int manager_write_resolv_conf(Manager
*m
) {
239 _cleanup_ordered_set_free_ OrderedSet
*dns
= NULL
, *domains
= NULL
;
240 _cleanup_free_
char *temp_path
= NULL
;
241 _cleanup_fclose_
FILE *f
= NULL
;
246 /* Read the system /etc/resolv.conf first */
247 (void) manager_read_resolv_conf(m
);
249 /* Add the full list to a set, to filter out duplicates */
250 r
= manager_compile_dns_servers(m
, &dns
);
252 return log_warning_errno(r
, "Failed to compile list of DNS servers: %m");
254 r
= manager_compile_search_domains(m
, &domains
, false);
256 return log_warning_errno(r
, "Failed to compile list of search domains: %m");
258 r
= fopen_temporary_label(PRIVATE_RESOLV_CONF
, PRIVATE_RESOLV_CONF
, &f
, &temp_path
);
260 return log_warning_errno(r
, "Failed to open private resolv.conf file for writing: %m");
262 (void) fchmod(fileno(f
), 0644);
264 r
= write_resolv_conf_contents(f
, dns
, domains
);
266 log_error_errno(r
, "Failed to write private resolv.conf contents: %m");
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");
278 (void) unlink(PRIVATE_RESOLV_CONF
);
279 (void) unlink(temp_path
);