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(
234 unsigned length
= 0, count
= 0;
243 ORDERED_SET_FOREACH(domain
, domains
, i
) {
244 if (++count
> MAXDNSRCH
) {
245 fputs("\n# Too many search domains configured, remaining ones ignored.", f
);
248 length
+= strlen(domain
) + 1;
250 fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f
);
260 static int write_uplink_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
263 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
265 "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
266 "# all known uplink DNS servers. This file lists all configured search domains.\n"
268 "# Third party programs should typically not access this file directly, but only\n"
269 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
270 "# different way, replace this symlink by a static file or a different symlink.\n"
272 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
273 "# operation for /etc/resolv.conf.\n"
276 if (ordered_set_isempty(dns
))
277 fputs("# No DNS servers known.\n", f
);
282 ORDERED_SET_FOREACH(s
, dns
, i
)
283 write_resolv_conf_server(s
, f
, &count
);
286 if (!ordered_set_isempty(domains
))
287 write_resolv_conf_search(domains
, f
);
289 return fflush_and_check(f
);
292 static int write_stub_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
293 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
295 "# This is a dynamic resolv.conf file for connecting local clients to the\n"
296 "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
297 "# configured search domains.\n"
299 "# Run \"resolvectl status\" to see details about the uplink DNS servers\n"
300 "# currently in use.\n"
302 "# Third party programs should typically not access this file directly, but only\n"
303 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
304 "# different way, replace this symlink by a static file or a different symlink.\n"
306 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
307 "# operation for /etc/resolv.conf.\n"
309 "nameserver 127.0.0.53\n"
310 "options edns0 trust-ad\n", f
);
312 if (!ordered_set_isempty(domains
))
313 write_resolv_conf_search(domains
, f
);
315 return fflush_and_check(f
);
318 int manager_write_resolv_conf(Manager
*m
) {
319 _cleanup_ordered_set_free_ OrderedSet
*dns
= NULL
, *domains
= NULL
;
320 _cleanup_free_
char *temp_path_uplink
= NULL
, *temp_path_stub
= NULL
;
321 _cleanup_fclose_
FILE *f_uplink
= NULL
, *f_stub
= NULL
;
326 /* Read the system /etc/resolv.conf first */
327 (void) manager_read_resolv_conf(m
);
329 /* Add the full list to a set, to filter out duplicates */
330 r
= manager_compile_dns_servers(m
, &dns
);
332 return log_warning_errno(r
, "Failed to compile list of DNS servers: %m");
334 r
= manager_compile_search_domains(m
, &domains
, false);
336 return log_warning_errno(r
, "Failed to compile list of search domains: %m");
338 r
= fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF
, PRIVATE_UPLINK_RESOLV_CONF
, &f_uplink
, &temp_path_uplink
);
340 return log_warning_errno(r
, "Failed to open new %s for writing: %m", PRIVATE_UPLINK_RESOLV_CONF
);
342 (void) fchmod(fileno(f_uplink
), 0644);
344 r
= write_uplink_resolv_conf_contents(f_uplink
, dns
, domains
);
346 log_error_errno(r
, "Failed to write new %s: %m", PRIVATE_UPLINK_RESOLV_CONF
);
350 if (m
->dns_stub_listener_mode
!= DNS_STUB_LISTENER_NO
) {
351 r
= fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF
, PRIVATE_STUB_RESOLV_CONF
, &f_stub
, &temp_path_stub
);
353 log_warning_errno(r
, "Failed to open new %s for writing: %m", PRIVATE_STUB_RESOLV_CONF
);
357 (void) fchmod(fileno(f_stub
), 0644);
359 r
= write_stub_resolv_conf_contents(f_stub
, dns
, domains
);
361 log_error_errno(r
, "Failed to write new %s: %m", PRIVATE_STUB_RESOLV_CONF
);
365 if (rename(temp_path_stub
, PRIVATE_STUB_RESOLV_CONF
) < 0)
366 r
= log_error_errno(errno
, "Failed to move new %s into place: %m", PRIVATE_STUB_RESOLV_CONF
);
369 r
= symlink_atomic(basename(PRIVATE_UPLINK_RESOLV_CONF
), PRIVATE_STUB_RESOLV_CONF
);
371 log_error_errno(r
, "Failed to symlink %s: %m", PRIVATE_STUB_RESOLV_CONF
);
374 if (rename(temp_path_uplink
, PRIVATE_UPLINK_RESOLV_CONF
) < 0)
375 r
= log_error_errno(errno
, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF
);
379 /* Something went wrong, perform cleanup... */
380 (void) unlink(temp_path_uplink
);
381 (void) unlink(temp_path_stub
);