]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-resolv-conf.c
resolved: also rewrite private /etc/resolv.conf when configuration is changed via...
[thirdparty/systemd.git] / src / resolve / resolved-resolv-conf.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Tom Gundersen <teg@jklm.no>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <resolv.h>
21
22 #include "alloc-util.h"
23 #include "dns-domain.h"
24 #include "fd-util.h"
25 #include "fileio-label.h"
26 #include "fileio.h"
27 #include "ordered-set.h"
28 #include "resolved-conf.h"
29 #include "resolved-resolv-conf.h"
30 #include "string-util.h"
31 #include "strv.h"
32
33 int manager_read_resolv_conf(Manager *m) {
34 _cleanup_fclose_ FILE *f = NULL;
35 struct stat st, own;
36 char line[LINE_MAX];
37 usec_t t;
38 int r;
39
40 assert(m);
41
42 /* Reads the system /etc/resolv.conf, if it exists and is not
43 * symlinked to our own resolv.conf instance */
44
45 if (!m->read_resolv_conf)
46 return 0;
47
48 r = stat("/etc/resolv.conf", &st);
49 if (r < 0) {
50 if (errno == ENOENT)
51 return 0;
52
53 r = log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m");
54 goto clear;
55 }
56
57 /* Have we already seen the file? */
58 t = timespec_load(&st.st_mtim);
59 if (t == m->resolv_conf_mtime)
60 return 0;
61
62 /* Is it symlinked to our own file? */
63 if (stat("/run/systemd/resolve/resolv.conf", &own) >= 0 &&
64 st.st_dev == own.st_dev &&
65 st.st_ino == own.st_ino)
66 return 0;
67
68 f = fopen("/etc/resolv.conf", "re");
69 if (!f) {
70 if (errno == ENOENT)
71 return 0;
72
73 r = log_warning_errno(errno, "Failed to open /etc/resolv.conf: %m");
74 goto clear;
75 }
76
77 if (fstat(fileno(f), &st) < 0) {
78 r = log_error_errno(errno, "Failed to stat open file: %m");
79 goto clear;
80 }
81
82 dns_server_mark_all(m->dns_servers);
83 dns_search_domain_mark_all(m->search_domains);
84
85 FOREACH_LINE(line, f, r = -errno; goto clear) {
86 const char *a;
87 char *l;
88
89 l = strstrip(line);
90 if (*l == '#' || *l == ';')
91 continue;
92
93 a = first_word(l, "nameserver");
94 if (a) {
95 r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a);
96 if (r < 0)
97 log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
98
99 continue;
100 }
101
102 a = first_word(l, "domain");
103 if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
104 a = first_word(l, "search");
105 if (a) {
106 r = manager_parse_search_domains_and_warn(m, a);
107 if (r < 0)
108 log_warning_errno(r, "Failed to parse search domain string '%s', ignoring.", a);
109 }
110 }
111
112 m->resolv_conf_mtime = t;
113
114 /* Flush out all servers and search domains that are still
115 * marked. Those are then ones that didn't appear in the new
116 * /etc/resolv.conf */
117 dns_server_unlink_marked(m->dns_servers);
118 dns_search_domain_unlink_marked(m->search_domains);
119
120 /* Whenever /etc/resolv.conf changes, start using the first
121 * DNS server of it. This is useful to deal with broken
122 * network managing implementations (like NetworkManager),
123 * that when connecting to a VPN place both the VPN DNS
124 * servers and the local ones in /etc/resolv.conf. Without
125 * resetting the DNS server to use back to the first entry we
126 * will continue to use the local one thus being unable to
127 * resolve VPN domains. */
128 manager_set_dns_server(m, m->dns_servers);
129
130 /* Unconditionally flush the cache when /etc/resolv.conf is
131 * modified, even if the data it contained was completely
132 * identical to the previous version we used. We do this
133 * because altering /etc/resolv.conf is typically done when
134 * the network configuration changes, and that should be
135 * enough to flush the global unicast DNS cache. */
136 if (m->unicast_scope)
137 dns_cache_flush(&m->unicast_scope->cache);
138
139 return 0;
140
141 clear:
142 dns_server_unlink_all(m->dns_servers);
143 dns_search_domain_unlink_all(m->search_domains);
144 return r;
145 }
146
147 static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
148 assert(s);
149 assert(f);
150 assert(count);
151
152 if (!dns_server_string(s)) {
153 log_warning("Our of memory, or invalid DNS address. Ignoring server.");
154 return;
155 }
156
157 if (*count == MAXNS)
158 fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
159 (*count)++;
160
161 fprintf(f, "nameserver %s\n", dns_server_string(s));
162 }
163
164 static void write_resolv_conf_search(
165 OrderedSet *domains,
166 FILE *f) {
167 unsigned length = 0, count = 0;
168 Iterator i;
169 char *domain;
170
171 assert(domains);
172 assert(f);
173
174 fputs("search", f);
175
176 ORDERED_SET_FOREACH(domain, domains, i) {
177 if (++count > MAXDNSRCH) {
178 fputs("\n# Too many search domains configured, remaining ones ignored.", f);
179 break;
180 }
181 length += strlen(domain) + 1;
182 if (length > 256) {
183 fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f);
184 break;
185 }
186 fputc(' ', f);
187 fputs(domain, f);
188 }
189
190 fputs("\n", f);
191 }
192
193 static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
194 Iterator i;
195
196 fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
197 "# Third party programs must not access this file directly, but\n"
198 "# only through the symlink at /etc/resolv.conf. To manage\n"
199 "# resolv.conf(5) in a different way, replace the symlink by a\n"
200 "# static file or a different symlink.\n\n", f);
201
202 if (ordered_set_isempty(dns))
203 fputs("# No DNS servers known.\n", f);
204 else {
205 unsigned count = 0;
206 DnsServer *s;
207
208 ORDERED_SET_FOREACH(s, dns, i)
209 write_resolv_conf_server(s, f, &count);
210 }
211
212 if (!ordered_set_isempty(domains))
213 write_resolv_conf_search(domains, f);
214
215 return fflush_and_check(f);
216 }
217
218 int manager_write_resolv_conf(Manager *m) {
219
220 _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
221 _cleanup_free_ char *temp_path = NULL;
222 _cleanup_fclose_ FILE *f = NULL;
223 int r;
224
225 assert(m);
226
227 /* Read the system /etc/resolv.conf first */
228 (void) manager_read_resolv_conf(m);
229
230 /* Add the full list to a set, to filter out duplicates */
231 r = manager_compile_dns_servers(m, &dns);
232 if (r < 0)
233 return log_warning_errno(r, "Failed to compile list of DNS servers: %m");
234
235 r = manager_compile_search_domains(m, &domains);
236 if (r < 0)
237 return log_warning_errno(r, "Failed to compile list of search domains: %m");
238
239 r = fopen_temporary_label(PRIVATE_RESOLV_CONF, PRIVATE_RESOLV_CONF, &f, &temp_path);
240 if (r < 0)
241 return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m");
242
243 (void) fchmod(fileno(f), 0644);
244
245 r = write_resolv_conf_contents(f, dns, domains);
246 if (r < 0) {
247 log_error_errno(r, "Failed to write private resolv.conf contents: %m");
248 goto fail;
249 }
250
251 if (rename(temp_path, PRIVATE_RESOLV_CONF) < 0) {
252 r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m");
253 goto fail;
254 }
255
256 return 0;
257
258 fail:
259 (void) unlink(PRIVATE_RESOLV_CONF);
260 (void) unlink(temp_path);
261
262 return r;
263 }