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 "string-util.h"
19 #include "tmpfile-util-label.h"
21 int manager_check_resolv_conf(const Manager
*m
) {
26 /* This warns only when our stub listener is disabled and /etc/resolv.conf is a symlink to
27 * PRIVATE_STATIC_RESOLV_CONF. */
29 if (m
->dns_stub_listener_mode
!= DNS_STUB_LISTENER_NO
)
32 if (stat("/etc/resolv.conf", &st
) < 0) {
36 return log_warning_errno(errno
, "Failed to stat /etc/resolv.conf: %m");
39 /* Is it symlinked to our own uplink file? */
40 if (stat(PRIVATE_STATIC_RESOLV_CONF
, &own
) >= 0 &&
41 st
.st_dev
== own
.st_dev
&&
42 st
.st_ino
== own
.st_ino
)
43 return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
44 "DNSStubListener= is disabled, but /etc/resolv.conf is a symlink to "
45 PRIVATE_STATIC_RESOLV_CONF
" which expects DNSStubListener= to be enabled.");
50 static bool file_is_our_own(const struct stat
*st
) {
56 PRIVATE_UPLINK_RESOLV_CONF
,
57 PRIVATE_STUB_RESOLV_CONF
,
58 PRIVATE_STATIC_RESOLV_CONF
) {
62 /* Is it symlinked to our own uplink file? */
63 if (stat(path
, &own
) >= 0 &&
64 st
->st_dev
== own
.st_dev
&&
65 st
->st_ino
== own
.st_ino
)
72 int manager_read_resolv_conf(Manager
*m
) {
73 _cleanup_fclose_
FILE *f
= NULL
;
80 /* Reads the system /etc/resolv.conf, if it exists and is not
81 * symlinked to our own resolv.conf instance */
83 if (!m
->read_resolv_conf
)
86 r
= stat("/etc/resolv.conf", &st
);
91 r
= log_warning_errno(errno
, "Failed to stat /etc/resolv.conf: %m");
95 /* Have we already seen the file? */
96 if (timespec_load(&st
.st_mtim
) == m
->resolv_conf_mtime
)
99 if (file_is_our_own(&st
))
102 f
= fopen("/etc/resolv.conf", "re");
107 r
= log_warning_errno(errno
, "Failed to open /etc/resolv.conf: %m");
111 if (fstat(fileno(f
), &st
) < 0) {
112 r
= log_error_errno(errno
, "Failed to stat open file: %m");
116 if (file_is_our_own(&st
))
119 dns_server_mark_all(m
->dns_servers
);
120 dns_search_domain_mark_all(m
->search_domains
);
123 _cleanup_free_
char *line
= NULL
;
127 r
= read_line(f
, LONG_LINE_MAX
, &line
);
129 log_error_errno(r
, "Failed to read /etc/resolv.conf: %m");
138 if (IN_SET(*l
, '#', ';', 0))
141 a
= first_word(l
, "nameserver");
143 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_SYSTEM
, a
);
145 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring.", a
);
150 a
= first_word(l
, "domain");
151 if (!a
) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
152 a
= first_word(l
, "search");
154 r
= manager_parse_search_domains_and_warn(m
, a
);
156 log_warning_errno(r
, "Failed to parse search domain string '%s', ignoring.", a
);
159 log_syntax(NULL
, LOG_DEBUG
, "/etc/resolv.conf", n
, 0, "Ignoring resolv.conf line: %s", l
);
162 m
->resolv_conf_mtime
= timespec_load(&st
.st_mtim
);
164 /* Flush out all servers and search domains that are still
165 * marked. Those are then ones that didn't appear in the new
166 * /etc/resolv.conf */
167 dns_server_unlink_marked(m
->dns_servers
);
168 dns_search_domain_unlink_marked(m
->search_domains
);
170 /* Whenever /etc/resolv.conf changes, start using the first
171 * DNS server of it. This is useful to deal with broken
172 * network managing implementations (like NetworkManager),
173 * that when connecting to a VPN place both the VPN DNS
174 * servers and the local ones in /etc/resolv.conf. Without
175 * resetting the DNS server to use back to the first entry we
176 * will continue to use the local one thus being unable to
177 * resolve VPN domains. */
178 manager_set_dns_server(m
, m
->dns_servers
);
180 /* Unconditionally flush the cache when /etc/resolv.conf is
181 * modified, even if the data it contained was completely
182 * identical to the previous version we used. We do this
183 * because altering /etc/resolv.conf is typically done when
184 * the network configuration changes, and that should be
185 * enough to flush the global unicast DNS cache. */
186 if (m
->unicast_scope
)
187 dns_cache_flush(&m
->unicast_scope
->cache
);
189 /* If /etc/resolv.conf changed, make sure to forget everything we learned about the DNS servers. After all we
190 * might now talk to a very different DNS server that just happens to have the same IP address as an old one
191 * (think 192.168.1.1). */
192 dns_server_reset_features_all(m
->dns_servers
);
197 dns_server_unlink_all(m
->dns_servers
);
198 dns_search_domain_unlink_all(m
->search_domains
);
202 static void write_resolv_conf_server(DnsServer
*s
, FILE *f
, unsigned *count
) {
209 if (!dns_server_string(s
)) {
210 log_warning("Out of memory, or invalid DNS address. Ignoring server.");
214 /* Check if the scope this DNS server belongs to is suitable as 'default' route for lookups; resolv.conf does
215 * not have a syntax to express that, so it must not appear as a global name server to avoid routing unrelated
216 * domains to it (which is a privacy violation, will most probably fail anyway, and adds unnecessary load) */
217 scope
= dns_server_scope(s
);
218 if (scope
&& !dns_scope_is_default_route(scope
)) {
219 log_debug("Scope of DNS server %s has only route-only domains, not using as global name server", dns_server_string(s
));
224 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f
);
227 fprintf(f
, "nameserver %s\n", dns_server_string(s
));
230 static void write_resolv_conf_search(
233 unsigned length
= 0, count
= 0;
242 ORDERED_SET_FOREACH(domain
, domains
, i
) {
243 if (++count
> MAXDNSRCH
) {
244 fputs("\n# Too many search domains configured, remaining ones ignored.", f
);
247 length
+= strlen(domain
) + 1;
249 fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f
);
259 static int write_uplink_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
262 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
264 "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
265 "# all known uplink DNS servers. This file lists all configured search domains.\n"
267 "# Third party programs should typically not access this file directly, but only\n"
268 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
269 "# different way, replace this symlink by a static file or a different symlink.\n"
271 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
272 "# operation for /etc/resolv.conf.\n"
275 if (ordered_set_isempty(dns
))
276 fputs("# No DNS servers known.\n", f
);
281 ORDERED_SET_FOREACH(s
, dns
, i
)
282 write_resolv_conf_server(s
, f
, &count
);
285 if (!ordered_set_isempty(domains
))
286 write_resolv_conf_search(domains
, f
);
288 return fflush_and_check(f
);
291 static int write_stub_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
292 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
294 "# This is a dynamic resolv.conf file for connecting local clients to the\n"
295 "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
296 "# configured search domains.\n"
298 "# Run \"resolvectl status\" to see details about the uplink DNS servers\n"
299 "# currently in use.\n"
301 "# Third party programs should typically not access this file directly, but only\n"
302 "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
303 "# different way, replace this symlink by a static file or a different symlink.\n"
305 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
306 "# operation for /etc/resolv.conf.\n"
308 "nameserver 127.0.0.53\n"
309 "options edns0\n", f
);
311 if (!ordered_set_isempty(domains
))
312 write_resolv_conf_search(domains
, f
);
314 return fflush_and_check(f
);
317 int manager_write_resolv_conf(Manager
*m
) {
318 _cleanup_ordered_set_free_ OrderedSet
*dns
= NULL
, *domains
= NULL
;
319 _cleanup_free_
char *temp_path_uplink
= NULL
, *temp_path_stub
= NULL
;
320 _cleanup_fclose_
FILE *f_uplink
= NULL
, *f_stub
= NULL
;
325 /* Read the system /etc/resolv.conf first */
326 (void) manager_read_resolv_conf(m
);
328 /* Add the full list to a set, to filter out duplicates */
329 r
= manager_compile_dns_servers(m
, &dns
);
331 return log_warning_errno(r
, "Failed to compile list of DNS servers: %m");
333 r
= manager_compile_search_domains(m
, &domains
, false);
335 return log_warning_errno(r
, "Failed to compile list of search domains: %m");
337 r
= fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF
, PRIVATE_UPLINK_RESOLV_CONF
, &f_uplink
, &temp_path_uplink
);
339 return log_warning_errno(r
, "Failed to open new %s for writing: %m", PRIVATE_UPLINK_RESOLV_CONF
);
341 (void) fchmod(fileno(f_uplink
), 0644);
343 r
= write_uplink_resolv_conf_contents(f_uplink
, dns
, domains
);
345 log_error_errno(r
, "Failed to write new %s: %m", PRIVATE_UPLINK_RESOLV_CONF
);
349 if (m
->dns_stub_listener_mode
!= DNS_STUB_LISTENER_NO
) {
350 r
= fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF
, PRIVATE_STUB_RESOLV_CONF
, &f_stub
, &temp_path_stub
);
352 log_warning_errno(r
, "Failed to open new %s for writing: %m", PRIVATE_STUB_RESOLV_CONF
);
356 (void) fchmod(fileno(f_stub
), 0644);
358 r
= write_stub_resolv_conf_contents(f_stub
, dns
, domains
);
360 log_error_errno(r
, "Failed to write new %s: %m", PRIVATE_STUB_RESOLV_CONF
);
364 if (rename(temp_path_stub
, PRIVATE_STUB_RESOLV_CONF
) < 0)
365 r
= log_error_errno(errno
, "Failed to move new %s into place: %m", PRIVATE_STUB_RESOLV_CONF
);
368 r
= symlink_atomic(basename(PRIVATE_UPLINK_RESOLV_CONF
), PRIVATE_STUB_RESOLV_CONF
);
370 log_error_errno(r
, "Failed to symlink %s: %m", PRIVATE_STUB_RESOLV_CONF
);
373 if (rename(temp_path_uplink
, PRIVATE_UPLINK_RESOLV_CONF
) < 0)
374 r
= log_error_errno(errno
, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF
);
378 /* Something went wrong, perform cleanup... */
379 (void) unlink(temp_path_uplink
);
380 (void) unlink(temp_path_stub
);