]>
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 LP |
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 && | |
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 | ||
157 | if (*count == MAXNS) | |
158 | fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f); | |
313cefa1 | 159 | (*count)++; |
f8dc7e34 | 160 | |
2817157b | 161 | fprintf(f, "nameserver %s\n", dns_server_string(s)); |
f8dc7e34 LP |
162 | } |
163 | ||
164 | static void write_resolv_conf_search( | |
d2bc1251 MP |
165 | OrderedSet *domains, |
166 | FILE *f) { | |
167 | unsigned length = 0, count = 0; | |
168 | Iterator i; | |
169 | char *domain; | |
f8dc7e34 | 170 | |
d2bc1251 | 171 | assert(domains); |
f8dc7e34 | 172 | assert(f); |
f8dc7e34 | 173 | |
d2bc1251 | 174 | fputs("search", f); |
f8dc7e34 | 175 | |
d2bc1251 MP |
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); | |
f8dc7e34 LP |
188 | } |
189 | ||
d2bc1251 | 190 | fputs("\n", f); |
f8dc7e34 LP |
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" | |
b30bf55d LP |
197 | "# This is a dynamic resolv.conf file for connecting local clients directly to\n" |
198 | "# all known DNS servers.\n#\n" | |
199 | "# Third party programs must not access this file directly, but only through the\n" | |
200 | "# symlink at /etc/resolv.conf. To manage resolv.conf(5) in a different way,\n" | |
201 | "# replace this symlink by a static file or a different symlink.\n#\n" | |
202 | "# See systemd-resolved.service(8) for details about the supported modes of\n" | |
203 | "# operation for /etc/resolv.conf.\n\n", f); | |
f8dc7e34 LP |
204 | |
205 | if (ordered_set_isempty(dns)) | |
206 | fputs("# No DNS servers known.\n", f); | |
207 | else { | |
208 | unsigned count = 0; | |
209 | DnsServer *s; | |
210 | ||
211 | ORDERED_SET_FOREACH(s, dns, i) | |
212 | write_resolv_conf_server(s, f, &count); | |
213 | } | |
214 | ||
d2bc1251 MP |
215 | if (!ordered_set_isempty(domains)) |
216 | write_resolv_conf_search(domains, f); | |
f8dc7e34 LP |
217 | |
218 | return fflush_and_check(f); | |
219 | } | |
220 | ||
221 | int manager_write_resolv_conf(Manager *m) { | |
9176a57c | 222 | |
9176a57c | 223 | _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL; |
f8dc7e34 LP |
224 | _cleanup_free_ char *temp_path = NULL; |
225 | _cleanup_fclose_ FILE *f = NULL; | |
f8dc7e34 LP |
226 | int r; |
227 | ||
228 | assert(m); | |
229 | ||
230 | /* Read the system /etc/resolv.conf first */ | |
7207052d | 231 | (void) manager_read_resolv_conf(m); |
f8dc7e34 LP |
232 | |
233 | /* Add the full list to a set, to filter out duplicates */ | |
9176a57c LP |
234 | r = manager_compile_dns_servers(m, &dns); |
235 | if (r < 0) | |
7207052d | 236 | return log_warning_errno(r, "Failed to compile list of DNS servers: %m"); |
f8dc7e34 | 237 | |
6f7da49d | 238 | r = manager_compile_search_domains(m, &domains, false); |
9176a57c | 239 | if (r < 0) |
7207052d | 240 | return log_warning_errno(r, "Failed to compile list of search domains: %m"); |
f8dc7e34 | 241 | |
9176a57c | 242 | r = fopen_temporary_label(PRIVATE_RESOLV_CONF, PRIVATE_RESOLV_CONF, &f, &temp_path); |
f8dc7e34 | 243 | if (r < 0) |
7207052d | 244 | return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m"); |
f8dc7e34 | 245 | |
7207052d | 246 | (void) fchmod(fileno(f), 0644); |
f8dc7e34 LP |
247 | |
248 | r = write_resolv_conf_contents(f, dns, domains); | |
7207052d LP |
249 | if (r < 0) { |
250 | log_error_errno(r, "Failed to write private resolv.conf contents: %m"); | |
f8dc7e34 | 251 | goto fail; |
7207052d | 252 | } |
f8dc7e34 | 253 | |
9176a57c | 254 | if (rename(temp_path, PRIVATE_RESOLV_CONF) < 0) { |
7207052d | 255 | r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m"); |
f8dc7e34 LP |
256 | goto fail; |
257 | } | |
258 | ||
259 | return 0; | |
260 | ||
261 | fail: | |
9176a57c | 262 | (void) unlink(PRIVATE_RESOLV_CONF); |
f8dc7e34 | 263 | (void) unlink(temp_path); |
7207052d | 264 | |
f8dc7e34 LP |
265 | return r; |
266 | } |