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-resolv-conf.h"
30 #include "string-util.h"
33 int manager_read_resolv_conf(Manager
*m
) {
34 _cleanup_fclose_
FILE *f
= NULL
;
42 /* Reads the system /etc/resolv.conf, if it exists and is not
43 * symlinked to our own resolv.conf instance */
45 if (!m
->read_resolv_conf
)
48 r
= stat("/etc/resolv.conf", &st
);
53 r
= log_warning_errno(errno
, "Failed to stat /etc/resolv.conf: %m");
57 /* Have we already seen the file? */
58 t
= timespec_load(&st
.st_mtim
);
59 if (t
== m
->resolv_conf_mtime
)
62 /* Is it symlinked to our own file? */
63 if (stat("/run/systemd/resolve/resolv.conf", &own
) >= 0 &&
64 st
.st_dev
== own
.st_dev
&&
65 st
.st_ino
== own
.st_ino
)
68 f
= fopen("/etc/resolv.conf", "re");
73 r
= log_warning_errno(errno
, "Failed to open /etc/resolv.conf: %m");
77 if (fstat(fileno(f
), &st
) < 0) {
78 r
= log_error_errno(errno
, "Failed to stat open file: %m");
82 dns_server_mark_all(m
->dns_servers
);
83 dns_search_domain_mark_all(m
->search_domains
);
85 FOREACH_LINE(line
, f
, r
= -errno
; goto clear
) {
90 if (*l
== '#' || *l
== ';')
93 a
= first_word(l
, "nameserver");
95 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_SYSTEM
, a
);
97 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring.", a
);
102 a
= first_word(l
, "domain");
103 if (!a
) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
104 a
= first_word(l
, "search");
106 r
= manager_parse_search_domains_and_warn(m
, a
);
108 log_warning_errno(r
, "Failed to parse search domain string '%s', ignoring.", a
);
112 m
->resolv_conf_mtime
= t
;
114 /* Flush out all servers and search domains that are still
115 * marked. Those are then ones that didn't appear in the new
116 * /etc/resolv.conf */
117 dns_server_unlink_marked(m
->dns_servers
);
118 dns_search_domain_unlink_marked(m
->search_domains
);
120 /* Whenever /etc/resolv.conf changes, start using the first
121 * DNS server of it. This is useful to deal with broken
122 * network managing implementations (like NetworkManager),
123 * that when connecting to a VPN place both the VPN DNS
124 * servers and the local ones in /etc/resolv.conf. Without
125 * resetting the DNS server to use back to the first entry we
126 * will continue to use the local one thus being unable to
127 * resolve VPN domains. */
128 manager_set_dns_server(m
, m
->dns_servers
);
130 /* Unconditionally flush the cache when /etc/resolv.conf is
131 * modified, even if the data it contained was completely
132 * identical to the previous version we used. We do this
133 * because altering /etc/resolv.conf is typically done when
134 * the network configuration changes, and that should be
135 * enough to flush the global unicast DNS cache. */
136 if (m
->unicast_scope
)
137 dns_cache_flush(&m
->unicast_scope
->cache
);
142 dns_server_unlink_all(m
->dns_servers
);
143 dns_search_domain_unlink_all(m
->search_domains
);
147 static void write_resolv_conf_server(DnsServer
*s
, FILE *f
, unsigned *count
) {
152 if (!dns_server_string(s
)) {
153 log_warning("Our of memory, or invalid DNS address. Ignoring server.");
158 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f
);
161 fprintf(f
, "nameserver %s\n", dns_server_string(s
));
164 static void write_resolv_conf_search(
167 unsigned length
= 0, count
= 0;
176 ORDERED_SET_FOREACH(domain
, domains
, i
) {
177 if (++count
> MAXDNSRCH
) {
178 fputs("\n# Too many search domains configured, remaining ones ignored.", f
);
181 length
+= strlen(domain
) + 1;
183 fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f
);
193 static int write_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
196 fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
197 "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
198 "# all known DNS servers.\n#\n"
199 "# Third party programs must not access this file directly, but only through the\n"
200 "# symlink at /etc/resolv.conf. To manage resolv.conf(5) in a different way,\n"
201 "# replace this symlink by a static file or a different symlink.\n#\n"
202 "# See systemd-resolved.service(8) for details about the supported modes of\n"
203 "# operation for /etc/resolv.conf.\n\n", f
);
205 if (ordered_set_isempty(dns
))
206 fputs("# No DNS servers known.\n", f
);
211 ORDERED_SET_FOREACH(s
, dns
, i
)
212 write_resolv_conf_server(s
, f
, &count
);
215 if (!ordered_set_isempty(domains
))
216 write_resolv_conf_search(domains
, f
);
218 return fflush_and_check(f
);
221 int manager_write_resolv_conf(Manager
*m
) {
223 _cleanup_ordered_set_free_ OrderedSet
*dns
= NULL
, *domains
= NULL
;
224 _cleanup_free_
char *temp_path
= NULL
;
225 _cleanup_fclose_
FILE *f
= NULL
;
230 /* Read the system /etc/resolv.conf first */
231 (void) manager_read_resolv_conf(m
);
233 /* Add the full list to a set, to filter out duplicates */
234 r
= manager_compile_dns_servers(m
, &dns
);
236 return log_warning_errno(r
, "Failed to compile list of DNS servers: %m");
238 r
= manager_compile_search_domains(m
, &domains
, false);
240 return log_warning_errno(r
, "Failed to compile list of search domains: %m");
242 r
= fopen_temporary_label(PRIVATE_RESOLV_CONF
, PRIVATE_RESOLV_CONF
, &f
, &temp_path
);
244 return log_warning_errno(r
, "Failed to open private resolv.conf file for writing: %m");
246 (void) fchmod(fileno(f
), 0644);
248 r
= write_resolv_conf_contents(f
, dns
, domains
);
250 log_error_errno(r
, "Failed to write private resolv.conf contents: %m");
254 if (rename(temp_path
, PRIVATE_RESOLV_CONF
) < 0) {
255 r
= log_error_errno(errno
, "Failed to move private resolv.conf file into place: %m");
262 (void) unlink(PRIVATE_RESOLV_CONF
);
263 (void) unlink(temp_path
);