1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Tom Gundersen <teg@jklm.no>
11 #include "alloc-util.h"
12 #include "dns-domain.h"
14 #include "fileio-label.h"
16 #include "ordered-set.h"
17 #include "resolved-conf.h"
18 #include "resolved-dns-server.h"
19 #include "resolved-resolv-conf.h"
20 #include "string-util.h"
23 /* A resolv.conf file containing the DNS server and domain data we learnt from uplink, i.e. the full uplink data */
24 #define PRIVATE_UPLINK_RESOLV_CONF "/run/systemd/resolve/resolv.conf"
26 /* A resolv.conf file containing the domain data we learnt from uplink, but our own DNS server address. */
27 #define PRIVATE_STUB_RESOLV_CONF "/run/systemd/resolve/stub-resolv.conf"
29 /* A static resolv.conf file containing no domains, but only our own DNS sever address */
30 #define PRIVATE_STATIC_RESOLV_CONF ROOTLIBEXECDIR "/resolv.conf"
32 static bool file_is_our_own(const struct stat
*st
) {
38 PRIVATE_UPLINK_RESOLV_CONF
,
39 PRIVATE_STUB_RESOLV_CONF
,
40 PRIVATE_STATIC_RESOLV_CONF
) {
44 /* Is it symlinked to our own uplink file? */
45 if (stat(path
, &own
) >= 0 &&
46 st
->st_dev
== own
.st_dev
&&
47 st
->st_ino
== own
.st_ino
)
54 int manager_read_resolv_conf(Manager
*m
) {
55 _cleanup_fclose_
FILE *f
= NULL
;
63 /* Reads the system /etc/resolv.conf, if it exists and is not
64 * symlinked to our own resolv.conf instance */
66 if (!m
->read_resolv_conf
)
69 r
= stat("/etc/resolv.conf", &st
);
74 r
= log_warning_errno(errno
, "Failed to stat /etc/resolv.conf: %m");
78 /* Have we already seen the file? */
79 if (timespec_load(&st
.st_mtim
) == m
->resolv_conf_mtime
)
82 if (file_is_our_own(&st
))
85 f
= fopen("/etc/resolv.conf", "re");
90 r
= log_warning_errno(errno
, "Failed to open /etc/resolv.conf: %m");
94 if (fstat(fileno(f
), &st
) < 0) {
95 r
= log_error_errno(errno
, "Failed to stat open file: %m");
99 if (file_is_our_own(&st
))
102 dns_server_mark_all(m
->dns_servers
);
103 dns_search_domain_mark_all(m
->search_domains
);
105 FOREACH_LINE(line
, f
, r
= -errno
; goto clear
) {
112 if (IN_SET(*l
, '#', ';', 0))
115 a
= first_word(l
, "nameserver");
117 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_SYSTEM
, a
);
119 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring.", a
);
124 a
= first_word(l
, "domain");
125 if (!a
) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
126 a
= first_word(l
, "search");
128 r
= manager_parse_search_domains_and_warn(m
, a
);
130 log_warning_errno(r
, "Failed to parse search domain string '%s', ignoring.", a
);
133 log_syntax(NULL
, LOG_DEBUG
, "/etc/resolv.conf", n
, 0, "Ignoring resolv.conf line: %s", l
);
136 m
->resolv_conf_mtime
= timespec_load(&st
.st_mtim
);
138 /* Flush out all servers and search domains that are still
139 * marked. Those are then ones that didn't appear in the new
140 * /etc/resolv.conf */
141 dns_server_unlink_marked(m
->dns_servers
);
142 dns_search_domain_unlink_marked(m
->search_domains
);
144 /* Whenever /etc/resolv.conf changes, start using the first
145 * DNS server of it. This is useful to deal with broken
146 * network managing implementations (like NetworkManager),
147 * that when connecting to a VPN place both the VPN DNS
148 * servers and the local ones in /etc/resolv.conf. Without
149 * resetting the DNS server to use back to the first entry we
150 * will continue to use the local one thus being unable to
151 * resolve VPN domains. */
152 manager_set_dns_server(m
, m
->dns_servers
);
154 /* Unconditionally flush the cache when /etc/resolv.conf is
155 * modified, even if the data it contained was completely
156 * identical to the previous version we used. We do this
157 * because altering /etc/resolv.conf is typically done when
158 * the network configuration changes, and that should be
159 * enough to flush the global unicast DNS cache. */
160 if (m
->unicast_scope
)
161 dns_cache_flush(&m
->unicast_scope
->cache
);
163 /* If /etc/resolv.conf changed, make sure to forget everything we learned about the DNS servers. After all we
164 * might now talk to a very different DNS server that just happens to have the same IP address as an old one
165 * (think 192.168.1.1). */
166 dns_server_reset_features_all(m
->dns_servers
);
171 dns_server_unlink_all(m
->dns_servers
);
172 dns_search_domain_unlink_all(m
->search_domains
);
176 static void write_resolv_conf_server(DnsServer
*s
, FILE *f
, unsigned *count
) {
181 if (!dns_server_string(s
)) {
182 log_warning("Our of memory, or invalid DNS address. Ignoring server.");
186 /* Check if the DNS server is limited to particular domains;
187 * resolv.conf does not have a syntax to express that, so it must not
188 * appear as a global name server to avoid routing unrelated domains to
189 * it (which is a privacy violation, will most probably fail anyway,
190 * and adds unnecessary load) */
191 if (dns_server_limited_domains(s
)) {
192 log_debug("DNS server %s has route-only domains, not using as global name server", dns_server_string(s
));
197 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f
);
200 fprintf(f
, "nameserver %s\n", dns_server_string(s
));
203 static void write_resolv_conf_search(
206 unsigned length
= 0, count
= 0;
215 ORDERED_SET_FOREACH(domain
, domains
, i
) {
216 if (++count
> MAXDNSRCH
) {
217 fputs("\n# Too many search domains configured, remaining ones ignored.", f
);
220 length
+= strlen(domain
) + 1;
222 fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f
);
232 static int write_uplink_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
235 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
237 "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
238 "# all known uplink DNS servers. This file lists all configured search domains.\n"
240 "# Third party programs must not access this file directly, but only through the\n"
241 "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n"
242 "# replace this symlink by a static file or a different symlink.\n"
244 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
245 "# operation for /etc/resolv.conf.\n"
248 if (ordered_set_isempty(dns
))
249 fputs("# No DNS servers known.\n", f
);
254 ORDERED_SET_FOREACH(s
, dns
, i
)
255 write_resolv_conf_server(s
, f
, &count
);
258 if (!ordered_set_isempty(domains
))
259 write_resolv_conf_search(domains
, f
);
261 return fflush_and_check(f
);
264 static int write_stub_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
265 fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
267 "# This is a dynamic resolv.conf file for connecting local clients to the\n"
268 "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
269 "# configured search domains.\n"
271 "# Run \"systemd-resolve --status\" to see details about the uplink DNS servers\n"
272 "# currently in use.\n"
274 "# Third party programs must not access this file directly, but only through the\n"
275 "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n"
276 "# replace this symlink by a static file or a different symlink.\n"
278 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
279 "# operation for /etc/resolv.conf.\n"
281 "nameserver 127.0.0.53\n", f
);
283 if (!ordered_set_isempty(domains
))
284 write_resolv_conf_search(domains
, f
);
286 return fflush_and_check(f
);
289 int manager_write_resolv_conf(Manager
*m
) {
291 _cleanup_ordered_set_free_ OrderedSet
*dns
= NULL
, *domains
= NULL
;
292 _cleanup_free_
char *temp_path_uplink
= NULL
, *temp_path_stub
= NULL
;
293 _cleanup_fclose_
FILE *f_uplink
= NULL
, *f_stub
= NULL
;
298 /* Read the system /etc/resolv.conf first */
299 (void) manager_read_resolv_conf(m
);
301 /* Add the full list to a set, to filter out duplicates */
302 r
= manager_compile_dns_servers(m
, &dns
);
304 return log_warning_errno(r
, "Failed to compile list of DNS servers: %m");
306 r
= manager_compile_search_domains(m
, &domains
, false);
308 return log_warning_errno(r
, "Failed to compile list of search domains: %m");
310 r
= fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF
, PRIVATE_UPLINK_RESOLV_CONF
, &f_uplink
, &temp_path_uplink
);
312 return log_warning_errno(r
, "Failed to open private resolv.conf file for writing: %m");
314 (void) __fsetlocking(f_uplink
, FSETLOCKING_BYCALLER
);
315 (void) fchmod(fileno(f_uplink
), 0644);
317 r
= fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF
, PRIVATE_STUB_RESOLV_CONF
, &f_stub
, &temp_path_stub
);
319 return log_warning_errno(r
, "Failed to open private stub-resolv.conf file for writing: %m");
321 (void) __fsetlocking(f_stub
, FSETLOCKING_BYCALLER
);
322 (void) fchmod(fileno(f_stub
), 0644);
324 r
= write_uplink_resolv_conf_contents(f_uplink
, dns
, domains
);
326 log_error_errno(r
, "Failed to write private resolv.conf contents: %m");
330 if (rename(temp_path_uplink
, PRIVATE_UPLINK_RESOLV_CONF
) < 0) {
331 r
= log_error_errno(errno
, "Failed to move private resolv.conf file into place: %m");
335 r
= write_stub_resolv_conf_contents(f_stub
, dns
, domains
);
337 log_error_errno(r
, "Failed to write private stub-resolv.conf contents: %m");
341 if (rename(temp_path_stub
, PRIVATE_STUB_RESOLV_CONF
) < 0) {
342 r
= log_error_errno(errno
, "Failed to move private stub-resolv.conf file into place: %m");
349 (void) unlink(PRIVATE_UPLINK_RESOLV_CONF
);
350 (void) unlink(temp_path_uplink
);
351 (void) unlink(PRIVATE_STUB_RESOLV_CONF
);
352 (void) unlink(temp_path_stub
);