1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "dns-domain.h"
10 #include "ordered-set.h"
11 #include "resolved-conf.h"
12 #include "resolved-dns-server.h"
13 #include "resolved-resolv-conf.h"
14 #include "string-util.h"
16 #include "tmpfile-util-label.h"
18 /* A resolv.conf file containing the DNS server and domain data we learnt from uplink, i.e. the full uplink data */
19 #define PRIVATE_UPLINK_RESOLV_CONF "/run/systemd/resolve/resolv.conf"
21 /* A resolv.conf file containing the domain data we learnt from uplink, but our own DNS server address. */
22 #define PRIVATE_STUB_RESOLV_CONF "/run/systemd/resolve/stub-resolv.conf"
24 /* A static resolv.conf file containing no domains, but only our own DNS server address */
25 #define PRIVATE_STATIC_RESOLV_CONF ROOTLIBEXECDIR "/resolv.conf"
27 int manager_check_resolv_conf(const Manager
*m
) {
34 /* This warns only when our stub listener is disabled and /etc/resolv.conf is a symlink to
35 * PRIVATE_STATIC_RESOLV_CONF or PRIVATE_STUB_RESOLV_CONF. */
37 if (m
->dns_stub_listener_mode
!= DNS_STUB_LISTENER_NO
)
40 r
= stat("/etc/resolv.conf", &st
);
45 return log_warning_errno(errno
, "Failed to stat /etc/resolv.conf: %m");
49 PRIVATE_STUB_RESOLV_CONF
,
50 PRIVATE_STATIC_RESOLV_CONF
) {
54 /* Is it symlinked to our own uplink file? */
55 if (stat(path
, &own
) >= 0 &&
56 st
.st_dev
== own
.st_dev
&&
57 st
.st_ino
== own
.st_ino
) {
58 log_warning("DNSStubListener= is disabled, but /etc/resolv.conf is a symlink to %s "
59 "which expects DNSStubListener= to be enabled.", path
);
67 static bool file_is_our_own(const struct stat
*st
) {
73 PRIVATE_UPLINK_RESOLV_CONF
,
74 PRIVATE_STUB_RESOLV_CONF
,
75 PRIVATE_STATIC_RESOLV_CONF
) {
79 /* Is it symlinked to our own uplink file? */
80 if (stat(path
, &own
) >= 0 &&
81 st
->st_dev
== own
.st_dev
&&
82 st
->st_ino
== own
.st_ino
)
89 int manager_read_resolv_conf(Manager
*m
) {
90 _cleanup_fclose_
FILE *f
= NULL
;
97 /* Reads the system /etc/resolv.conf, if it exists and is not
98 * symlinked to our own resolv.conf instance */
100 if (!m
->read_resolv_conf
)
103 r
= stat("/etc/resolv.conf", &st
);
108 r
= log_warning_errno(errno
, "Failed to stat /etc/resolv.conf: %m");
112 /* Have we already seen the file? */
113 if (timespec_load(&st
.st_mtim
) == m
->resolv_conf_mtime
)
116 if (file_is_our_own(&st
))
119 f
= fopen("/etc/resolv.conf", "re");
124 r
= log_warning_errno(errno
, "Failed to open /etc/resolv.conf: %m");
128 if (fstat(fileno(f
), &st
) < 0) {
129 r
= log_error_errno(errno
, "Failed to stat open file: %m");
133 if (file_is_our_own(&st
))
136 dns_server_mark_all(m
->dns_servers
);
137 dns_search_domain_mark_all(m
->search_domains
);
140 _cleanup_free_
char *line
= NULL
;
144 r
= read_line(f
, LONG_LINE_MAX
, &line
);
146 log_error_errno(r
, "Failed to read /etc/resolv.conf: %m");
155 if (IN_SET(*l
, '#', ';', 0))
158 a
= first_word(l
, "nameserver");
160 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_SYSTEM
, a
);
162 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring.", a
);
167 a
= first_word(l
, "domain");
168 if (!a
) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
169 a
= first_word(l
, "search");
171 r
= manager_parse_search_domains_and_warn(m
, a
);
173 log_warning_errno(r
, "Failed to parse search domain string '%s', ignoring.", a
);
176 log_syntax(NULL
, LOG_DEBUG
, "/etc/resolv.conf", n
, 0, "Ignoring resolv.conf line: %s", l
);
179 m
->resolv_conf_mtime
= timespec_load(&st
.st_mtim
);
181 /* Flush out all servers and search domains that are still
182 * marked. Those are then ones that didn't appear in the new
183 * /etc/resolv.conf */
184 dns_server_unlink_marked(m
->dns_servers
);
185 dns_search_domain_unlink_marked(m
->search_domains
);
187 /* Whenever /etc/resolv.conf changes, start using the first
188 * DNS server of it. This is useful to deal with broken
189 * network managing implementations (like NetworkManager),
190 * that when connecting to a VPN place both the VPN DNS
191 * servers and the local ones in /etc/resolv.conf. Without
192 * resetting the DNS server to use back to the first entry we
193 * will continue to use the local one thus being unable to
194 * resolve VPN domains. */
195 manager_set_dns_server(m
, m
->dns_servers
);
197 /* Unconditionally flush the cache when /etc/resolv.conf is
198 * modified, even if the data it contained was completely
199 * identical to the previous version we used. We do this
200 * because altering /etc/resolv.conf is typically done when
201 * the network configuration changes, and that should be
202 * enough to flush the global unicast DNS cache. */
203 if (m
->unicast_scope
)
204 dns_cache_flush(&m
->unicast_scope
->cache
);
206 /* If /etc/resolv.conf changed, make sure to forget everything we learned about the DNS servers. After all we
207 * might now talk to a very different DNS server that just happens to have the same IP address as an old one
208 * (think 192.168.1.1). */
209 dns_server_reset_features_all(m
->dns_servers
);
214 dns_server_unlink_all(m
->dns_servers
);
215 dns_search_domain_unlink_all(m
->search_domains
);
219 static void write_resolv_conf_server(DnsServer
*s
, FILE *f
, unsigned *count
) {
224 if (!dns_server_string(s
)) {
225 log_warning("Out of memory, or invalid DNS address. Ignoring server.");
229 /* Check if the DNS server is limited to particular domains;
230 * resolv.conf does not have a syntax to express that, so it must not
231 * appear as a global name server to avoid routing unrelated domains to
232 * it (which is a privacy violation, will most probably fail anyway,
233 * and adds unnecessary load) */
234 if (dns_server_limited_domains(s
)) {
235 log_debug("DNS server %s has route-only domains, not using as global name server", dns_server_string(s
));
240 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f
);
243 fprintf(f
, "nameserver %s\n", dns_server_string(s
));
246 static void write_resolv_conf_search(
249 unsigned length
= 0, count
= 0;
258 ORDERED_SET_FOREACH(domain
, domains
, i
) {
259 if (++count
> MAXDNSRCH
) {
260 fputs("\n# Too many search domains configured, remaining ones ignored.", f
);
263 length
+= strlen(domain
) + 1;
265 fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f
);
275 static int write_uplink_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
278 fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
280 "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
281 "# all known uplink DNS servers. This file lists all configured search domains.\n"
283 "# Third party programs must not access this file directly, but only through the\n"
284 "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n"
285 "# replace this symlink by a static file or a different symlink.\n"
287 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
288 "# operation for /etc/resolv.conf.\n"
291 if (ordered_set_isempty(dns
))
292 fputs("# No DNS servers known.\n", f
);
297 ORDERED_SET_FOREACH(s
, dns
, i
)
298 write_resolv_conf_server(s
, f
, &count
);
301 if (!ordered_set_isempty(domains
))
302 write_resolv_conf_search(domains
, f
);
304 return fflush_and_check(f
);
307 static int write_stub_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
308 fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
310 "# This is a dynamic resolv.conf file for connecting local clients to the\n"
311 "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
312 "# configured search domains.\n"
314 "# Run \"resolvectl status\" to see details about the uplink DNS servers\n"
315 "# currently in use.\n"
317 "# Third party programs must not access this file directly, but only through the\n"
318 "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n"
319 "# replace this symlink by a static file or a different symlink.\n"
321 "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
322 "# operation for /etc/resolv.conf.\n"
324 "nameserver 127.0.0.53\n", f
);
326 if (!ordered_set_isempty(domains
))
327 write_resolv_conf_search(domains
, f
);
329 return fflush_and_check(f
);
332 int manager_write_resolv_conf(Manager
*m
) {
334 _cleanup_ordered_set_free_ OrderedSet
*dns
= NULL
, *domains
= NULL
;
335 _cleanup_free_
char *temp_path_uplink
= NULL
, *temp_path_stub
= NULL
;
336 _cleanup_fclose_
FILE *f_uplink
= NULL
, *f_stub
= NULL
;
341 /* Read the system /etc/resolv.conf first */
342 (void) manager_read_resolv_conf(m
);
344 /* Add the full list to a set, to filter out duplicates */
345 r
= manager_compile_dns_servers(m
, &dns
);
347 return log_warning_errno(r
, "Failed to compile list of DNS servers: %m");
349 r
= manager_compile_search_domains(m
, &domains
, false);
351 return log_warning_errno(r
, "Failed to compile list of search domains: %m");
353 r
= fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF
, PRIVATE_UPLINK_RESOLV_CONF
, &f_uplink
, &temp_path_uplink
);
355 return log_warning_errno(r
, "Failed to open private resolv.conf file for writing: %m");
357 (void) __fsetlocking(f_uplink
, FSETLOCKING_BYCALLER
);
358 (void) fchmod(fileno(f_uplink
), 0644);
360 r
= fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF
, PRIVATE_STUB_RESOLV_CONF
, &f_stub
, &temp_path_stub
);
362 return log_warning_errno(r
, "Failed to open private stub-resolv.conf file for writing: %m");
364 (void) __fsetlocking(f_stub
, FSETLOCKING_BYCALLER
);
365 (void) fchmod(fileno(f_stub
), 0644);
367 r
= write_uplink_resolv_conf_contents(f_uplink
, dns
, domains
);
369 log_error_errno(r
, "Failed to write private resolv.conf contents: %m");
373 if (rename(temp_path_uplink
, PRIVATE_UPLINK_RESOLV_CONF
) < 0) {
374 r
= log_error_errno(errno
, "Failed to move private resolv.conf file into place: %m");
378 r
= write_stub_resolv_conf_contents(f_stub
, dns
, domains
);
380 log_error_errno(r
, "Failed to write private stub-resolv.conf contents: %m");
384 if (rename(temp_path_stub
, PRIVATE_STUB_RESOLV_CONF
) < 0) {
385 r
= log_error_errno(errno
, "Failed to move private stub-resolv.conf file into place: %m");
392 (void) unlink(PRIVATE_UPLINK_RESOLV_CONF
);
393 (void) unlink(temp_path_uplink
);
394 (void) unlink(PRIVATE_STUB_RESOLV_CONF
);
395 (void) unlink(temp_path_stub
);