1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
9 #include "dns-domain.h"
13 #include "ordered-set.h"
14 #include "resolved-conf.h"
15 #include "resolved-dns-server.h"
16 #include "resolved-resolv-conf.h"
17 #include "stat-util.h"
18 #include "string-util.h"
20 #include "tmpfile-util-label.h"
22 int manager_check_resolv_conf(const Manager
*m
) {
27 /* This warns only when our stub listener is disabled and /etc/resolv.conf is a symlink to
28 * PRIVATE_STATIC_RESOLV_CONF. */
30 if (m
->dns_stub_listener_mode
!= DNS_STUB_LISTENER_NO
)
33 if (stat("/etc/resolv.conf", &st
) < 0) {
37 return log_warning_errno(errno
, "Failed to stat /etc/resolv.conf: %m");
40 /* Is it symlinked to our own uplink file? */
41 if (stat(PRIVATE_STATIC_RESOLV_CONF
, &own
) >= 0 &&
42 st
.st_dev
== own
.st_dev
&&
43 st
.st_ino
== own
.st_ino
)
44 return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
45 "DNSStubListener= is disabled, but /etc/resolv.conf is a symlink to "
46 PRIVATE_STATIC_RESOLV_CONF
" which expects DNSStubListener= to be enabled.");
51 static bool file_is_our_own(const struct stat
*st
) {
57 PRIVATE_UPLINK_RESOLV_CONF
,
58 PRIVATE_STUB_RESOLV_CONF
,
59 PRIVATE_STATIC_RESOLV_CONF
) {
63 /* Is it symlinked to our own uplink file? */
64 if (stat(path
, &own
) >= 0 &&
65 st
->st_dev
== own
.st_dev
&&
66 st
->st_ino
== own
.st_ino
)
73 int manager_read_resolv_conf(Manager
*m
) {
74 _cleanup_fclose_
FILE *f
= NULL
;
81 /* Reads the system /etc/resolv.conf, if it exists and is not
82 * symlinked to our own resolv.conf instance */
84 if (!m
->read_resolv_conf
)
87 r
= stat("/etc/resolv.conf", &st
);
92 r
= log_warning_errno(errno
, "Failed to stat /etc/resolv.conf: %m");
96 /* Have we already seen the file? */
97 if (stat_inode_unmodified(&st
, &m
->resolv_conf_stat
))
100 if (file_is_our_own(&st
))
103 f
= fopen("/etc/resolv.conf", "re");
108 r
= log_warning_errno(errno
, "Failed to open /etc/resolv.conf: %m");
112 if (fstat(fileno(f
), &st
) < 0) {
113 r
= log_error_errno(errno
, "Failed to stat open file: %m");
117 if (file_is_our_own(&st
))
120 dns_server_mark_all(m
->dns_servers
);
121 dns_search_domain_mark_all(m
->search_domains
);
124 _cleanup_free_
char *line
= NULL
;
128 r
= read_line(f
, LONG_LINE_MAX
, &line
);
130 log_error_errno(r
, "Failed to read /etc/resolv.conf: %m");
139 if (IN_SET(*l
, '#', ';', 0))
142 a
= first_word(l
, "nameserver");
144 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_SYSTEM
, a
);
146 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring.", a
);
151 a
= first_word(l
, "domain");
152 if (!a
) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
153 a
= first_word(l
, "search");
155 r
= manager_parse_search_domains_and_warn(m
, a
);
157 log_warning_errno(r
, "Failed to parse search domain string '%s', ignoring.", a
);
160 log_syntax(NULL
, LOG_DEBUG
, "/etc/resolv.conf", n
, 0, "Ignoring resolv.conf line: %s", l
);
163 m
->resolv_conf_stat
= st
;
165 /* Flush out all servers and search domains that are still
166 * marked. Those are then ones that didn't appear in the new
167 * /etc/resolv.conf */
168 dns_server_unlink_marked(m
->dns_servers
);
169 dns_search_domain_unlink_marked(m
->search_domains
);
171 /* Whenever /etc/resolv.conf changes, start using the first
172 * DNS server of it. This is useful to deal with broken
173 * network managing implementations (like NetworkManager),
174 * that when connecting to a VPN place both the VPN DNS
175 * servers and the local ones in /etc/resolv.conf. Without
176 * resetting the DNS server to use back to the first entry we
177 * will continue to use the local one thus being unable to
178 * resolve VPN domains. */
179 manager_set_dns_server(m
, m
->dns_servers
);
181 /* Unconditionally flush the cache when /etc/resolv.conf is
182 * modified, even if the data it contained was completely
183 * identical to the previous version we used. We do this
184 * because altering /etc/resolv.conf is typically done when
185 * the network configuration changes, and that should be
186 * enough to flush the global unicast DNS cache. */
187 if (m
->unicast_scope
)
188 dns_cache_flush(&m
->unicast_scope
->cache
);
190 /* If /etc/resolv.conf changed, make sure to forget everything we learned about the DNS servers. After all we
191 * might now talk to a very different DNS server that just happens to have the same IP address as an old one
192 * (think 192.168.1.1). */
193 dns_server_reset_features_all(m
->dns_servers
);
198 dns_server_unlink_all(m
->dns_servers
);
199 dns_search_domain_unlink_all(m
->search_domains
);
203 static void write_resolv_conf_server(DnsServer
*s
, FILE *f
, unsigned *count
) {
210 if (!dns_server_string(s
)) {
211 log_warning("Out of memory, or invalid DNS address. Ignoring server.");
215 /* Check if the scope this DNS server belongs to is suitable as 'default' route for lookups; resolv.conf does
216 * not have a syntax to express that, so it must not appear as a global name server to avoid routing unrelated
217 * domains to it (which is a privacy violation, will most probably fail anyway, and adds unnecessary load) */
218 scope
= dns_server_scope(s
);
219 if (scope
&& !dns_scope_is_default_route(scope
)) {
220 log_debug("Scope of DNS server %s has only route-only domains, not using as global name server", dns_server_string(s
));
225 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f
);
228 fprintf(f
, "nameserver %s\n", dns_server_string(s
));
231 static void write_resolv_conf_search(
241 ORDERED_SET_FOREACH(domain
, domains
) {
249 static int write_uplink_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
251 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
253 "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
254 "# all known uplink DNS servers. This file lists all configured search domains.\n"
256 "# Third party programs should typically not access this file directly, but only\n"
257 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
258 "# different way, replace this symlink by a static file or a different symlink.\n"
260 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
261 "# operation for /etc/resolv.conf.\n"
264 if (ordered_set_isempty(dns
))
265 fputs("# No DNS servers known.\n", f
);
270 ORDERED_SET_FOREACH(s
, dns
)
271 write_resolv_conf_server(s
, f
, &count
);
274 if (!ordered_set_isempty(domains
))
275 write_resolv_conf_search(domains
, f
);
277 return fflush_and_check(f
);
280 static int write_stub_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
281 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
283 "# This is a dynamic resolv.conf file for connecting local clients to the\n"
284 "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
285 "# configured search domains.\n"
287 "# Run \"resolvectl status\" to see details about the uplink DNS servers\n"
288 "# currently in use.\n"
290 "# Third party programs should typically not access this file directly, but only\n"
291 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
292 "# different way, replace this symlink by a static file or a different symlink.\n"
294 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
295 "# operation for /etc/resolv.conf.\n"
297 "nameserver 127.0.0.53\n"
298 "options edns0 trust-ad\n", f
);
300 if (!ordered_set_isempty(domains
))
301 write_resolv_conf_search(domains
, f
);
303 return fflush_and_check(f
);
306 int manager_write_resolv_conf(Manager
*m
) {
307 _cleanup_ordered_set_free_ OrderedSet
*dns
= NULL
, *domains
= NULL
;
308 _cleanup_free_
char *temp_path_uplink
= NULL
, *temp_path_stub
= NULL
;
309 _cleanup_fclose_
FILE *f_uplink
= NULL
, *f_stub
= NULL
;
314 /* Read the system /etc/resolv.conf first */
315 (void) manager_read_resolv_conf(m
);
317 /* Add the full list to a set, to filter out duplicates */
318 r
= manager_compile_dns_servers(m
, &dns
);
320 return log_warning_errno(r
, "Failed to compile list of DNS servers: %m");
322 r
= manager_compile_search_domains(m
, &domains
, false);
324 return log_warning_errno(r
, "Failed to compile list of search domains: %m");
326 r
= fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF
, PRIVATE_UPLINK_RESOLV_CONF
, &f_uplink
, &temp_path_uplink
);
328 return log_warning_errno(r
, "Failed to open new %s for writing: %m", PRIVATE_UPLINK_RESOLV_CONF
);
330 (void) fchmod(fileno(f_uplink
), 0644);
332 r
= write_uplink_resolv_conf_contents(f_uplink
, dns
, domains
);
334 log_error_errno(r
, "Failed to write new %s: %m", PRIVATE_UPLINK_RESOLV_CONF
);
338 if (m
->dns_stub_listener_mode
!= DNS_STUB_LISTENER_NO
) {
339 r
= fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF
, PRIVATE_STUB_RESOLV_CONF
, &f_stub
, &temp_path_stub
);
341 log_warning_errno(r
, "Failed to open new %s for writing: %m", PRIVATE_STUB_RESOLV_CONF
);
345 (void) fchmod(fileno(f_stub
), 0644);
347 r
= write_stub_resolv_conf_contents(f_stub
, dns
, domains
);
349 log_error_errno(r
, "Failed to write new %s: %m", PRIVATE_STUB_RESOLV_CONF
);
353 if (rename(temp_path_stub
, PRIVATE_STUB_RESOLV_CONF
) < 0)
354 r
= log_error_errno(errno
, "Failed to move new %s into place: %m", PRIVATE_STUB_RESOLV_CONF
);
357 r
= symlink_atomic(basename(PRIVATE_UPLINK_RESOLV_CONF
), PRIVATE_STUB_RESOLV_CONF
);
359 log_error_errno(r
, "Failed to symlink %s: %m", PRIVATE_STUB_RESOLV_CONF
);
362 if (rename(temp_path_uplink
, PRIVATE_UPLINK_RESOLV_CONF
) < 0)
363 r
= log_error_errno(errno
, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF
);
367 /* Something went wrong, perform cleanup... */
368 (void) unlink(temp_path_uplink
);
369 (void) unlink(temp_path_stub
);