1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
25 #include "dns-domain.h"
27 #include "fileio-label.h"
29 #include "ordered-set.h"
30 #include "resolved-conf.h"
31 #include "resolved-resolv-conf.h"
32 #include "string-util.h"
35 int manager_read_resolv_conf(Manager
*m
) {
36 _cleanup_fclose_
FILE *f
= NULL
;
44 /* Reads the system /etc/resolv.conf, if it exists and is not
45 * symlinked to our own resolv.conf instance */
47 if (!m
->read_resolv_conf
)
50 r
= stat("/etc/resolv.conf", &st
);
55 r
= log_warning_errno(errno
, "Failed to stat /etc/resolv.conf: %m");
59 /* Have we already seen the file? */
60 t
= timespec_load(&st
.st_mtim
);
61 if (t
== m
->resolv_conf_mtime
)
64 /* Is it symlinked to our own file? */
65 if (stat("/run/systemd/resolve/resolv.conf", &own
) >= 0 &&
66 st
.st_dev
== own
.st_dev
&&
67 st
.st_ino
== own
.st_ino
)
70 f
= fopen("/etc/resolv.conf", "re");
75 r
= log_warning_errno(errno
, "Failed to open /etc/resolv.conf: %m");
79 if (fstat(fileno(f
), &st
) < 0) {
80 r
= log_error_errno(errno
, "Failed to stat open file: %m");
84 dns_server_mark_all(m
->dns_servers
);
85 dns_search_domain_mark_all(m
->search_domains
);
87 FOREACH_LINE(line
, f
, r
= -errno
; goto clear
) {
92 if (*l
== '#' || *l
== ';')
95 a
= first_word(l
, "nameserver");
97 r
= manager_add_dns_server_by_string(m
, DNS_SERVER_SYSTEM
, a
);
99 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring.", a
);
104 a
= first_word(l
, "domain");
105 if (!a
) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
106 a
= first_word(l
, "search");
108 r
= manager_parse_search_domains_and_warn(m
, a
);
110 log_warning_errno(r
, "Failed to parse search domain string '%s', ignoring.", a
);
114 m
->resolv_conf_mtime
= t
;
116 /* Flush out all servers and search domains that are still
117 * marked. Those are then ones that didn't appear in the new
118 * /etc/resolv.conf */
119 dns_server_unlink_marked(m
->dns_servers
);
120 dns_search_domain_unlink_marked(m
->search_domains
);
122 /* Whenever /etc/resolv.conf changes, start using the first
123 * DNS server of it. This is useful to deal with broken
124 * network managing implementations (like NetworkManager),
125 * that when connecting to a VPN place both the VPN DNS
126 * servers and the local ones in /etc/resolv.conf. Without
127 * resetting the DNS server to use back to the first entry we
128 * will continue to use the local one thus being unable to
129 * resolve VPN domains. */
130 manager_set_dns_server(m
, m
->dns_servers
);
132 /* Unconditionally flush the cache when /etc/resolv.conf is
133 * modified, even if the data it contained was completely
134 * identical to the previous version we used. We do this
135 * because altering /etc/resolv.conf is typically done when
136 * the network configuration changes, and that should be
137 * enough to flush the global unicast DNS cache. */
138 if (m
->unicast_scope
)
139 dns_cache_flush(&m
->unicast_scope
->cache
);
144 dns_server_unlink_all(m
->dns_servers
);
145 dns_search_domain_unlink_all(m
->search_domains
);
149 static void write_resolv_conf_server(DnsServer
*s
, FILE *f
, unsigned *count
) {
150 _cleanup_free_
char *t
= NULL
;
157 r
= in_addr_to_string(s
->family
, &s
->address
, &t
);
159 log_warning_errno(r
, "Invalid DNS address. Ignoring: %m");
164 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f
);
167 fprintf(f
, "nameserver %s\n", t
);
170 static void write_resolv_conf_search(
180 if (*count
>= MAXDNSRCH
||
181 *length
+ strlen(domain
) > 256) {
182 if (*count
== MAXDNSRCH
)
183 fputs(" # Too many search domains configured, remaining ones ignored.", f
);
185 fputs(" # Total length of all search domains is too long, remaining ones ignored.", f
);
190 (*length
) += strlen(domain
);
197 static int write_resolv_conf_contents(FILE *f
, OrderedSet
*dns
, OrderedSet
*domains
) {
200 fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
201 "# Third party programs must not access this file directly, but\n"
202 "# only through the symlink at /etc/resolv.conf. To manage\n"
203 "# resolv.conf(5) in a different way, replace the symlink by a\n"
204 "# static file or a different symlink.\n\n", f
);
206 if (ordered_set_isempty(dns
))
207 fputs("# No DNS servers known.\n", f
);
212 ORDERED_SET_FOREACH(s
, dns
, i
)
213 write_resolv_conf_server(s
, f
, &count
);
216 if (!ordered_set_isempty(domains
)) {
217 unsigned length
= 0, count
= 0;
221 ORDERED_SET_FOREACH(domain
, domains
, i
)
222 write_resolv_conf_search(domain
, f
, &count
, &length
);
226 return fflush_and_check(f
);
229 int manager_write_resolv_conf(Manager
*m
) {
231 #define PRIVATE_RESOLV_CONF "/run/systemd/resolve/resolv.conf"
233 _cleanup_ordered_set_free_ OrderedSet
*dns
= NULL
, *domains
= NULL
;
234 _cleanup_free_
char *temp_path
= NULL
;
235 _cleanup_fclose_
FILE *f
= NULL
;
240 /* Read the system /etc/resolv.conf first */
241 manager_read_resolv_conf(m
);
243 /* Add the full list to a set, to filter out duplicates */
244 r
= manager_compile_dns_servers(m
, &dns
);
248 r
= manager_compile_search_domains(m
, &domains
);
252 r
= fopen_temporary_label(PRIVATE_RESOLV_CONF
, PRIVATE_RESOLV_CONF
, &f
, &temp_path
);
256 fchmod(fileno(f
), 0644);
258 r
= write_resolv_conf_contents(f
, dns
, domains
);
262 if (rename(temp_path
, PRIVATE_RESOLV_CONF
) < 0) {
270 (void) unlink(PRIVATE_RESOLV_CONF
);
271 (void) unlink(temp_path
);