]>
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" | |
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]; | |
f8dc7e34 LP |
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) | |
bf7fabd6 LP |
51 | return 0; |
52 | ||
53 | r = log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m"); | |
f8dc7e34 LP |
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 | ||
f8dc7e34 | 62 | /* Is it symlinked to our own file? */ |
604c9415 | 63 | if (stat(PRIVATE_RESOLV_CONF, &own) >= 0 && |
f8dc7e34 | 64 | st.st_dev == own.st_dev && |
bf7fabd6 LP |
65 | st.st_ino == own.st_ino) |
66 | return 0; | |
f8dc7e34 LP |
67 | |
68 | f = fopen("/etc/resolv.conf", "re"); | |
69 | if (!f) { | |
70 | if (errno == ENOENT) | |
bf7fabd6 LP |
71 | return 0; |
72 | ||
73 | r = log_warning_errno(errno, "Failed to open /etc/resolv.conf: %m"); | |
f8dc7e34 LP |
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 | ||
4b95f179 | 82 | dns_server_mark_all(m->dns_servers); |
a51c1048 | 83 | dns_search_domain_mark_all(m->search_domains); |
f8dc7e34 LP |
84 | |
85 | FOREACH_LINE(line, f, r = -errno; goto clear) { | |
f8dc7e34 LP |
86 | const char *a; |
87 | char *l; | |
88 | ||
f8dc7e34 LP |
89 | l = strstrip(line); |
90 | if (*l == '#' || *l == ';') | |
91 | continue; | |
92 | ||
93 | a = first_word(l, "nameserver"); | |
94 | if (a) { | |
2817157b | 95 | r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a); |
f8dc7e34 LP |
96 | if (r < 0) |
97 | log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a); | |
98 | ||
99 | continue; | |
100 | } | |
a51c1048 LP |
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 | } | |
f8dc7e34 LP |
110 | } |
111 | ||
bf7fabd6 LP |
112 | m->resolv_conf_mtime = t; |
113 | ||
a51c1048 LP |
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 */ | |
4b95f179 | 117 | dns_server_unlink_marked(m->dns_servers); |
a51c1048 | 118 | dns_search_domain_unlink_marked(m->search_domains); |
f8dc7e34 LP |
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 | ||
452b4e32 LP |
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 | ||
f8dc7e34 LP |
139 | return 0; |
140 | ||
141 | clear: | |
4b95f179 | 142 | dns_server_unlink_all(m->dns_servers); |
a51c1048 | 143 | dns_search_domain_unlink_all(m->search_domains); |
f8dc7e34 LP |
144 | return r; |
145 | } | |
146 | ||
147 | static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) { | |
f8dc7e34 LP |
148 | assert(s); |
149 | assert(f); | |
150 | assert(count); | |
151 | ||
2817157b | 152 | if (!dns_server_string(s)) { |
6cb08a89 | 153 | log_warning("Our of memory, or invalid DNS address. Ignoring server."); |
f8dc7e34 LP |
154 | return; |
155 | } | |
156 | ||
b9fe94ca MP |
157 | /* Check if the DNS server is limited to particular domains; |
158 | * resolv.conf does not have a syntax to express that, so it must not | |
159 | * appear as a global name server to avoid routing unrelated domains to | |
160 | * it (which is a privacy violation, will most probably fail anyway, | |
161 | * and adds unnecessary load) */ | |
162 | if (dns_server_limited_domains(s)) { | |
163 | log_debug("DNS server %s has route-only domains, not using as global name server", dns_server_string(s)); | |
164 | return; | |
165 | } | |
166 | ||
f8dc7e34 | 167 | if (*count == MAXNS) |
4b61c875 | 168 | fputs_unlocked("# Too many DNS servers configured, the following entries may be ignored.\n", f); |
313cefa1 | 169 | (*count)++; |
f8dc7e34 | 170 | |
2817157b | 171 | fprintf(f, "nameserver %s\n", dns_server_string(s)); |
f8dc7e34 LP |
172 | } |
173 | ||
174 | static void write_resolv_conf_search( | |
d2bc1251 MP |
175 | OrderedSet *domains, |
176 | FILE *f) { | |
177 | unsigned length = 0, count = 0; | |
178 | Iterator i; | |
179 | char *domain; | |
f8dc7e34 | 180 | |
d2bc1251 | 181 | assert(domains); |
f8dc7e34 | 182 | assert(f); |
f8dc7e34 | 183 | |
4b61c875 | 184 | fputs_unlocked("search", f); |
f8dc7e34 | 185 | |
d2bc1251 MP |
186 | ORDERED_SET_FOREACH(domain, domains, i) { |
187 | if (++count > MAXDNSRCH) { | |
4b61c875 | 188 | fputs_unlocked("\n# Too many search domains configured, remaining ones ignored.", f); |
d2bc1251 MP |
189 | break; |
190 | } | |
191 | length += strlen(domain) + 1; | |
192 | if (length > 256) { | |
4b61c875 | 193 | fputs_unlocked("\n# Total length of all search domains is too long, remaining ones ignored.", f); |
d2bc1251 MP |
194 | break; |
195 | } | |
4b61c875 LP |
196 | fputc_unlocked(' ', f); |
197 | fputs_unlocked(domain, f); | |
f8dc7e34 LP |
198 | } |
199 | ||
4b61c875 | 200 | fputs_unlocked("\n", f); |
f8dc7e34 LP |
201 | } |
202 | ||
203 | static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) { | |
204 | Iterator i; | |
205 | ||
4b61c875 LP |
206 | fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n#\n" |
207 | "# This is a dynamic resolv.conf file for connecting local clients directly to\n" | |
208 | "# all known DNS servers.\n#\n" | |
209 | "# Third party programs must not access this file directly, but only through the\n" | |
210 | "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n" | |
211 | "# replace this symlink by a static file or a different symlink.\n#\n" | |
212 | "# See man:systemd-resolved.service(8) for details about the supported modes of\n" | |
213 | "# operation for /etc/resolv.conf.\n\n", f); | |
f8dc7e34 LP |
214 | |
215 | if (ordered_set_isempty(dns)) | |
4b61c875 | 216 | fputs_unlocked("# No DNS servers known.\n", f); |
f8dc7e34 LP |
217 | else { |
218 | unsigned count = 0; | |
219 | DnsServer *s; | |
220 | ||
221 | ORDERED_SET_FOREACH(s, dns, i) | |
222 | write_resolv_conf_server(s, f, &count); | |
223 | } | |
224 | ||
d2bc1251 MP |
225 | if (!ordered_set_isempty(domains)) |
226 | write_resolv_conf_search(domains, f); | |
f8dc7e34 LP |
227 | |
228 | return fflush_and_check(f); | |
229 | } | |
230 | ||
231 | int manager_write_resolv_conf(Manager *m) { | |
9176a57c | 232 | |
9176a57c | 233 | _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL; |
f8dc7e34 LP |
234 | _cleanup_free_ char *temp_path = NULL; |
235 | _cleanup_fclose_ FILE *f = NULL; | |
f8dc7e34 LP |
236 | int r; |
237 | ||
238 | assert(m); | |
239 | ||
240 | /* Read the system /etc/resolv.conf first */ | |
7207052d | 241 | (void) manager_read_resolv_conf(m); |
f8dc7e34 LP |
242 | |
243 | /* Add the full list to a set, to filter out duplicates */ | |
9176a57c LP |
244 | r = manager_compile_dns_servers(m, &dns); |
245 | if (r < 0) | |
7207052d | 246 | return log_warning_errno(r, "Failed to compile list of DNS servers: %m"); |
f8dc7e34 | 247 | |
6f7da49d | 248 | r = manager_compile_search_domains(m, &domains, false); |
9176a57c | 249 | if (r < 0) |
7207052d | 250 | return log_warning_errno(r, "Failed to compile list of search domains: %m"); |
f8dc7e34 | 251 | |
9176a57c | 252 | r = fopen_temporary_label(PRIVATE_RESOLV_CONF, PRIVATE_RESOLV_CONF, &f, &temp_path); |
f8dc7e34 | 253 | if (r < 0) |
7207052d | 254 | return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m"); |
f8dc7e34 | 255 | |
7207052d | 256 | (void) fchmod(fileno(f), 0644); |
f8dc7e34 LP |
257 | |
258 | r = write_resolv_conf_contents(f, dns, domains); | |
7207052d LP |
259 | if (r < 0) { |
260 | log_error_errno(r, "Failed to write private resolv.conf contents: %m"); | |
f8dc7e34 | 261 | goto fail; |
7207052d | 262 | } |
f8dc7e34 | 263 | |
9176a57c | 264 | if (rename(temp_path, PRIVATE_RESOLV_CONF) < 0) { |
7207052d | 265 | r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m"); |
f8dc7e34 LP |
266 | goto fail; |
267 | } | |
268 | ||
269 | return 0; | |
270 | ||
271 | fail: | |
9176a57c | 272 | (void) unlink(PRIVATE_RESOLV_CONF); |
f8dc7e34 | 273 | (void) unlink(temp_path); |
7207052d | 274 | |
f8dc7e34 LP |
275 | return r; |
276 | } |