]>
Commit | Line | Data |
---|---|---|
f8dc7e34 LP |
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" | |
59c0fd0e | 29 | #include "resolved-dns-server.h" |
f8dc7e34 LP |
30 | #include "resolved-resolv-conf.h" |
31 | #include "string-util.h" | |
32 | #include "strv.h" | |
33 | ||
34 | int manager_read_resolv_conf(Manager *m) { | |
35 | _cleanup_fclose_ FILE *f = NULL; | |
36 | struct stat st, own; | |
37 | char line[LINE_MAX]; | |
f8dc7e34 LP |
38 | usec_t t; |
39 | int r; | |
40 | ||
41 | assert(m); | |
42 | ||
43 | /* Reads the system /etc/resolv.conf, if it exists and is not | |
44 | * symlinked to our own resolv.conf instance */ | |
45 | ||
46 | if (!m->read_resolv_conf) | |
47 | return 0; | |
48 | ||
49 | r = stat("/etc/resolv.conf", &st); | |
50 | if (r < 0) { | |
51 | if (errno == ENOENT) | |
bf7fabd6 LP |
52 | return 0; |
53 | ||
54 | r = log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m"); | |
f8dc7e34 LP |
55 | goto clear; |
56 | } | |
57 | ||
58 | /* Have we already seen the file? */ | |
59 | t = timespec_load(&st.st_mtim); | |
60 | if (t == m->resolv_conf_mtime) | |
61 | return 0; | |
62 | ||
f8dc7e34 | 63 | /* Is it symlinked to our own file? */ |
604c9415 | 64 | if (stat(PRIVATE_RESOLV_CONF, &own) >= 0 && |
f8dc7e34 | 65 | st.st_dev == own.st_dev && |
bf7fabd6 LP |
66 | st.st_ino == own.st_ino) |
67 | return 0; | |
f8dc7e34 LP |
68 | |
69 | f = fopen("/etc/resolv.conf", "re"); | |
70 | if (!f) { | |
71 | if (errno == ENOENT) | |
bf7fabd6 LP |
72 | return 0; |
73 | ||
74 | r = log_warning_errno(errno, "Failed to open /etc/resolv.conf: %m"); | |
f8dc7e34 LP |
75 | goto clear; |
76 | } | |
77 | ||
78 | if (fstat(fileno(f), &st) < 0) { | |
79 | r = log_error_errno(errno, "Failed to stat open file: %m"); | |
80 | goto clear; | |
81 | } | |
82 | ||
4b95f179 | 83 | dns_server_mark_all(m->dns_servers); |
a51c1048 | 84 | dns_search_domain_mark_all(m->search_domains); |
f8dc7e34 LP |
85 | |
86 | FOREACH_LINE(line, f, r = -errno; goto clear) { | |
f8dc7e34 LP |
87 | const char *a; |
88 | char *l; | |
89 | ||
f8dc7e34 | 90 | l = strstrip(line); |
4c701096 | 91 | if (IN_SET(*l, '#', ';')) |
f8dc7e34 LP |
92 | continue; |
93 | ||
94 | a = first_word(l, "nameserver"); | |
95 | if (a) { | |
2817157b | 96 | r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a); |
f8dc7e34 LP |
97 | if (r < 0) |
98 | log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a); | |
99 | ||
100 | continue; | |
101 | } | |
a51c1048 LP |
102 | |
103 | a = first_word(l, "domain"); | |
104 | if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */ | |
105 | a = first_word(l, "search"); | |
106 | if (a) { | |
107 | r = manager_parse_search_domains_and_warn(m, a); | |
108 | if (r < 0) | |
109 | log_warning_errno(r, "Failed to parse search domain string '%s', ignoring.", a); | |
110 | } | |
f8dc7e34 LP |
111 | } |
112 | ||
bf7fabd6 LP |
113 | m->resolv_conf_mtime = t; |
114 | ||
a51c1048 LP |
115 | /* Flush out all servers and search domains that are still |
116 | * marked. Those are then ones that didn't appear in the new | |
117 | * /etc/resolv.conf */ | |
4b95f179 | 118 | dns_server_unlink_marked(m->dns_servers); |
a51c1048 | 119 | dns_search_domain_unlink_marked(m->search_domains); |
f8dc7e34 LP |
120 | |
121 | /* Whenever /etc/resolv.conf changes, start using the first | |
122 | * DNS server of it. This is useful to deal with broken | |
123 | * network managing implementations (like NetworkManager), | |
124 | * that when connecting to a VPN place both the VPN DNS | |
125 | * servers and the local ones in /etc/resolv.conf. Without | |
126 | * resetting the DNS server to use back to the first entry we | |
127 | * will continue to use the local one thus being unable to | |
128 | * resolve VPN domains. */ | |
129 | manager_set_dns_server(m, m->dns_servers); | |
130 | ||
452b4e32 LP |
131 | /* Unconditionally flush the cache when /etc/resolv.conf is |
132 | * modified, even if the data it contained was completely | |
133 | * identical to the previous version we used. We do this | |
134 | * because altering /etc/resolv.conf is typically done when | |
135 | * the network configuration changes, and that should be | |
136 | * enough to flush the global unicast DNS cache. */ | |
137 | if (m->unicast_scope) | |
138 | dns_cache_flush(&m->unicast_scope->cache); | |
139 | ||
59c0fd0e LP |
140 | /* If /etc/resolv.conf changed, make sure to forget everything we learned about the DNS servers. After all we |
141 | * might now talk to a very different DNS server that just happens to have the same IP address as an old one | |
142 | * (think 192.168.1.1). */ | |
143 | dns_server_reset_features_all(m->dns_servers); | |
144 | ||
f8dc7e34 LP |
145 | return 0; |
146 | ||
147 | clear: | |
4b95f179 | 148 | dns_server_unlink_all(m->dns_servers); |
a51c1048 | 149 | dns_search_domain_unlink_all(m->search_domains); |
f8dc7e34 LP |
150 | return r; |
151 | } | |
152 | ||
153 | static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) { | |
f8dc7e34 LP |
154 | assert(s); |
155 | assert(f); | |
156 | assert(count); | |
157 | ||
2817157b | 158 | if (!dns_server_string(s)) { |
6cb08a89 | 159 | log_warning("Our of memory, or invalid DNS address. Ignoring server."); |
f8dc7e34 LP |
160 | return; |
161 | } | |
162 | ||
b9fe94ca MP |
163 | /* Check if the DNS server is limited to particular domains; |
164 | * resolv.conf does not have a syntax to express that, so it must not | |
165 | * appear as a global name server to avoid routing unrelated domains to | |
166 | * it (which is a privacy violation, will most probably fail anyway, | |
167 | * and adds unnecessary load) */ | |
168 | if (dns_server_limited_domains(s)) { | |
169 | log_debug("DNS server %s has route-only domains, not using as global name server", dns_server_string(s)); | |
170 | return; | |
171 | } | |
172 | ||
f8dc7e34 | 173 | if (*count == MAXNS) |
4b61c875 | 174 | fputs_unlocked("# Too many DNS servers configured, the following entries may be ignored.\n", f); |
313cefa1 | 175 | (*count)++; |
f8dc7e34 | 176 | |
2817157b | 177 | fprintf(f, "nameserver %s\n", dns_server_string(s)); |
f8dc7e34 LP |
178 | } |
179 | ||
180 | static void write_resolv_conf_search( | |
d2bc1251 MP |
181 | OrderedSet *domains, |
182 | FILE *f) { | |
183 | unsigned length = 0, count = 0; | |
184 | Iterator i; | |
185 | char *domain; | |
f8dc7e34 | 186 | |
d2bc1251 | 187 | assert(domains); |
f8dc7e34 | 188 | assert(f); |
f8dc7e34 | 189 | |
4b61c875 | 190 | fputs_unlocked("search", f); |
f8dc7e34 | 191 | |
d2bc1251 MP |
192 | ORDERED_SET_FOREACH(domain, domains, i) { |
193 | if (++count > MAXDNSRCH) { | |
4b61c875 | 194 | fputs_unlocked("\n# Too many search domains configured, remaining ones ignored.", f); |
d2bc1251 MP |
195 | break; |
196 | } | |
197 | length += strlen(domain) + 1; | |
198 | if (length > 256) { | |
4b61c875 | 199 | fputs_unlocked("\n# Total length of all search domains is too long, remaining ones ignored.", f); |
d2bc1251 MP |
200 | break; |
201 | } | |
4b61c875 LP |
202 | fputc_unlocked(' ', f); |
203 | fputs_unlocked(domain, f); | |
f8dc7e34 LP |
204 | } |
205 | ||
4b61c875 | 206 | fputs_unlocked("\n", f); |
f8dc7e34 LP |
207 | } |
208 | ||
209 | static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) { | |
210 | Iterator i; | |
211 | ||
4b61c875 LP |
212 | fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n#\n" |
213 | "# This is a dynamic resolv.conf file for connecting local clients directly to\n" | |
214 | "# all known DNS servers.\n#\n" | |
215 | "# Third party programs must not access this file directly, but only through the\n" | |
216 | "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n" | |
217 | "# replace this symlink by a static file or a different symlink.\n#\n" | |
218 | "# See man:systemd-resolved.service(8) for details about the supported modes of\n" | |
219 | "# operation for /etc/resolv.conf.\n\n", f); | |
f8dc7e34 LP |
220 | |
221 | if (ordered_set_isempty(dns)) | |
4b61c875 | 222 | fputs_unlocked("# No DNS servers known.\n", f); |
f8dc7e34 LP |
223 | else { |
224 | unsigned count = 0; | |
225 | DnsServer *s; | |
226 | ||
227 | ORDERED_SET_FOREACH(s, dns, i) | |
228 | write_resolv_conf_server(s, f, &count); | |
229 | } | |
230 | ||
d2bc1251 MP |
231 | if (!ordered_set_isempty(domains)) |
232 | write_resolv_conf_search(domains, f); | |
f8dc7e34 LP |
233 | |
234 | return fflush_and_check(f); | |
235 | } | |
236 | ||
237 | int manager_write_resolv_conf(Manager *m) { | |
9176a57c | 238 | |
9176a57c | 239 | _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL; |
f8dc7e34 LP |
240 | _cleanup_free_ char *temp_path = NULL; |
241 | _cleanup_fclose_ FILE *f = NULL; | |
f8dc7e34 LP |
242 | int r; |
243 | ||
244 | assert(m); | |
245 | ||
246 | /* Read the system /etc/resolv.conf first */ | |
7207052d | 247 | (void) manager_read_resolv_conf(m); |
f8dc7e34 LP |
248 | |
249 | /* Add the full list to a set, to filter out duplicates */ | |
9176a57c LP |
250 | r = manager_compile_dns_servers(m, &dns); |
251 | if (r < 0) | |
7207052d | 252 | return log_warning_errno(r, "Failed to compile list of DNS servers: %m"); |
f8dc7e34 | 253 | |
6f7da49d | 254 | r = manager_compile_search_domains(m, &domains, false); |
9176a57c | 255 | if (r < 0) |
7207052d | 256 | return log_warning_errno(r, "Failed to compile list of search domains: %m"); |
f8dc7e34 | 257 | |
9176a57c | 258 | r = fopen_temporary_label(PRIVATE_RESOLV_CONF, PRIVATE_RESOLV_CONF, &f, &temp_path); |
f8dc7e34 | 259 | if (r < 0) |
7207052d | 260 | return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m"); |
f8dc7e34 | 261 | |
7207052d | 262 | (void) fchmod(fileno(f), 0644); |
f8dc7e34 LP |
263 | |
264 | r = write_resolv_conf_contents(f, dns, domains); | |
7207052d LP |
265 | if (r < 0) { |
266 | log_error_errno(r, "Failed to write private resolv.conf contents: %m"); | |
f8dc7e34 | 267 | goto fail; |
7207052d | 268 | } |
f8dc7e34 | 269 | |
9176a57c | 270 | if (rename(temp_path, PRIVATE_RESOLV_CONF) < 0) { |
7207052d | 271 | r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m"); |
f8dc7e34 LP |
272 | goto fail; |
273 | } | |
274 | ||
275 | return 0; | |
276 | ||
277 | fail: | |
9176a57c | 278 | (void) unlink(PRIVATE_RESOLV_CONF); |
f8dc7e34 | 279 | (void) unlink(temp_path); |
7207052d | 280 | |
f8dc7e34 LP |
281 | return r; |
282 | } |