]>
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) { | |
95 | r = manager_add_dns_server_by_string(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 | } | |
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 | ||
6cb08a89 LP |
152 | (void) dns_server_string(s); |
153 | ||
154 | if (!s->server_string) { | |
155 | log_warning("Our of memory, or invalid DNS address. Ignoring server."); | |
f8dc7e34 LP |
156 | return; |
157 | } | |
158 | ||
159 | if (*count == MAXNS) | |
160 | fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f); | |
161 | (*count) ++; | |
162 | ||
6cb08a89 | 163 | fprintf(f, "nameserver %s\n", s->server_string); |
f8dc7e34 LP |
164 | } |
165 | ||
166 | static void write_resolv_conf_search( | |
167 | const char *domain, | |
168 | FILE *f, | |
169 | unsigned *count, | |
170 | unsigned *length) { | |
171 | ||
172 | assert(domain); | |
173 | assert(f); | |
174 | assert(length); | |
175 | ||
176 | if (*count >= MAXDNSRCH || | |
177 | *length + strlen(domain) > 256) { | |
178 | if (*count == MAXDNSRCH) | |
179 | fputs(" # Too many search domains configured, remaining ones ignored.", f); | |
180 | if (*length <= 256) | |
181 | fputs(" # Total length of all search domains is too long, remaining ones ignored.", f); | |
182 | ||
183 | return; | |
184 | } | |
185 | ||
186 | (*length) += strlen(domain); | |
187 | (*count) ++; | |
188 | ||
189 | fputc(' ', f); | |
190 | fputs(domain, 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 | unsigned length = 0, count = 0; | |
214 | char *domain; | |
215 | ||
216 | fputs("search", f); | |
217 | ORDERED_SET_FOREACH(domain, domains, i) | |
218 | write_resolv_conf_search(domain, f, &count, &length); | |
219 | fputs("\n", f); | |
220 | } | |
221 | ||
222 | return fflush_and_check(f); | |
223 | } | |
224 | ||
225 | int manager_write_resolv_conf(Manager *m) { | |
9176a57c | 226 | |
9176a57c | 227 | _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL; |
f8dc7e34 LP |
228 | _cleanup_free_ char *temp_path = NULL; |
229 | _cleanup_fclose_ FILE *f = NULL; | |
f8dc7e34 LP |
230 | int r; |
231 | ||
232 | assert(m); | |
233 | ||
234 | /* Read the system /etc/resolv.conf first */ | |
235 | manager_read_resolv_conf(m); | |
236 | ||
237 | /* Add the full list to a set, to filter out duplicates */ | |
9176a57c LP |
238 | r = manager_compile_dns_servers(m, &dns); |
239 | if (r < 0) | |
240 | return r; | |
f8dc7e34 | 241 | |
9176a57c LP |
242 | r = manager_compile_search_domains(m, &domains); |
243 | if (r < 0) | |
244 | return r; | |
f8dc7e34 | 245 | |
9176a57c | 246 | r = fopen_temporary_label(PRIVATE_RESOLV_CONF, PRIVATE_RESOLV_CONF, &f, &temp_path); |
f8dc7e34 LP |
247 | if (r < 0) |
248 | return r; | |
249 | ||
250 | fchmod(fileno(f), 0644); | |
251 | ||
252 | r = write_resolv_conf_contents(f, dns, domains); | |
253 | if (r < 0) | |
254 | goto fail; | |
255 | ||
9176a57c | 256 | if (rename(temp_path, PRIVATE_RESOLV_CONF) < 0) { |
f8dc7e34 LP |
257 | r = -errno; |
258 | goto fail; | |
259 | } | |
260 | ||
261 | return 0; | |
262 | ||
263 | fail: | |
9176a57c | 264 | (void) unlink(PRIVATE_RESOLV_CONF); |
f8dc7e34 LP |
265 | (void) unlink(temp_path); |
266 | return r; | |
267 | } |