Debian Codegraph @

src/resolve/resolved-dns-delegate.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "conf-files.h"
5 #include "conf-parser.h"
6 #include "constants.h"
7 #include "dns-domain.h"
8 #include "extract-word.h"
9 #include "hashmap.h"
10 #include "in-addr-util.h"
11 #include "log.h"
12 #include "path-util.h"
13 #include "resolved-dns-delegate.h"
14 #include "resolved-dns-scope.h"
15 #include "resolved-dns-search-domain.h"
16 #include "resolved-dns-server.h"
17 #include "resolved-manager.h"
18 #include "socket-netlink.h"
19 #include "string-util.h"
20 #include "strv.h"
21
22 #define DNS_DELEGATES_MAX 4096U
23 #define DNS_DELEGATE_SEARCH_DIRS ((const char* const*) CONF_PATHS_STRV("systemd/dns-delegate.d"))
24
25 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
26 dns_delegate_hash_ops,
27 char,
28 string_hash_func,
29 string_compare_func,
30 DnsDelegate,
31 dns_delegate_free);
32
33 int dns_delegate_new(Manager *m, const char *id, DnsDelegate **ret) {
34 int r;
35
36 assert(m);
37 assert(id);
38
39 if (hashmap_size(m->delegates) >= DNS_DELEGATES_MAX)
40 return -E2BIG;
41
42 _cleanup_free_ char *id_copy = strdup(id);
43 if (!id_copy)
44 return -ENOMEM;
45
46 _cleanup_(dns_delegate_freep) DnsDelegate *d = new(DnsDelegate, 1);
47 if (!d)
48 return -ENOMEM;
49
50 *d = (DnsDelegate) {
51 .id = TAKE_PTR(id_copy),
52 .default_route = -1,
53 };
54
55 r = dns_scope_new(
56 m,
57 &d->scope,
58 DNS_SCOPE_DELEGATE,
59 /* link= */ NULL,
60 d,
61 DNS_PROTOCOL_DNS,
62 AF_UNSPEC);
63 if (r < 0)
64 return r;
65
66 r = hashmap_ensure_put(&m->delegates, &dns_delegate_hash_ops, d->id, d);
67 if (r < 0)
68 return r;
69
70 d->manager = m;
71
72 log_debug("New delegate '%s'.", id);
73
74 if (ret)
75 *ret = d;
76
77 TAKE_PTR(d);
78 return 0;
79 }
80
81 DnsDelegate *dns_delegate_free(DnsDelegate *d) {
82 if (!d)
83 return NULL;
84
85 Manager *m = d->manager;
86
87 log_debug("Removing delegate '%s'.", d->id);
88
89 dns_server_unlink_all(d->dns_servers);
90 dns_search_domain_unlink_all(d->search_domains);
91
92 dns_scope_free(d->scope);
93
94 if (m)
95 hashmap_remove(m->delegates, d->id);
96
97 free(d->id);
98
99 return mfree(d);
100 }
101
102 DnsServer* dns_delegate_set_dns_server(DnsDelegate *d, DnsServer *s) {
103 assert(d);
104
105 if (d->current_dns_server == s)
106 return s;
107
108 if (s)
109 log_debug("Switching delegate '%s' to DNS server %s.", d->id, strna(dns_server_string_full(s)));
110
111 dns_server_unref(d->current_dns_server);
112 d->current_dns_server = dns_server_ref(s);
113
114 /* Skip flushing the cache if server stale feature is enabled. */
115 if (d->manager->stale_retention_usec == 0)
116 dns_cache_flush(&d->scope->cache);
117
118 return s;
119 }
120
121 DnsServer *dns_delegate_get_dns_server(DnsDelegate *d) {
122 assert(d);
123
124 if (!d->current_dns_server)
125 dns_delegate_set_dns_server(d, d->dns_servers);
126
127 return d->current_dns_server;
128 }
129
130 void dns_delegate_next_dns_server(DnsDelegate *d, DnsServer *if_current) {
131 assert(d);
132
133 /* If we have issues with a DNS server, let's switch to the next one (in a round robin scheme). If
134 * non-NULL if_current points to the DNS server that was selected at the beginning of whatever bigger
135 * operation we are currently executing, and hence if we already switched away from it we suppress
136 * switching again, so that each operation only results in a single switch, not multiple. */
137
138 /* If the current server of the transaction is specified, and we already are at a different one,
139 * don't do anything */
140 if (if_current && d->current_dns_server != if_current)
141 return;
142
143 /* If currently have no DNS server, then don't do anything, we'll pick it lazily the next time a DNS
144 * server is needed. */
145 if (!d->current_dns_server)
146 return;
147
148 /* Change to the next one, but make sure to follow the linked list only if this server is actually
149 * still linked. */
150 if (d->current_dns_server->linked && d->current_dns_server->servers_next) {
151 dns_delegate_set_dns_server(d, d->current_dns_server->servers_next);
152 return;
153 }
154
155 /* Pick the first one again, after we reached the end */
156 dns_delegate_set_dns_server(d, d->dns_servers);
157 }
158
159 static int dns_delegate_load(Manager *m, const char *path) {
160 int r;
161
162 assert(m);
163 assert(path);
164
165 _cleanup_free_ char *fn = NULL;
166 r = path_extract_filename(path, &fn);
167 if (r < 0)
168 return log_error_errno(r, "Failed to extract filename from path '%s': %m", path);
169
170 const char *e = endswith(fn, ".dns-delegate");
171 if (!e)
172 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "DNS delegate file name does not end in .dns-delegate, refusing: %s", fn);
173
174 _cleanup_free_ char *id = strndup(fn, e - fn);
175 if (!string_is_safe(id, /* flags= */ 0))
176 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "DNS delegate file name is invalid, refusing: %s", fn);
177
178 _cleanup_free_ char *dropin_dirname = strjoin(id, ".dns-delegate.d");
179 if (!dropin_dirname)
180 return log_oom();
181
182 _cleanup_(dns_delegate_freep) DnsDelegate *d = NULL;
183 r = dns_delegate_new(m, id, &d);
184 if (r < 0)
185 return log_error_errno(r, "Failed to allocate delegate '%s': %m", id);
186
187 r = config_parse_many(
188 STRV_MAKE_CONST(path),
189 DNS_DELEGATE_SEARCH_DIRS,
190 dropin_dirname,
191 "Delegate\0",
192 config_item_perf_lookup,
193 resolved_dns_delegate_gperf_lookup,
194 /* flags= */ 0,
195 d);
196 if (r < 0)
197 return r;
198
199 log_info("Successfully loaded delegate '%s'.", d->id);
200
201 TAKE_PTR(d);
202 return 0;
203 }
204
205 int manager_load_delegates(Manager *m) {
206 _cleanup_strv_free_ char **files = NULL;
207 int r;
208
209 assert(m);
210
211 r = conf_files_list_strv(&files, ".dns-delegate", /* root= */ NULL, CONF_FILES_WARN, DNS_DELEGATE_SEARCH_DIRS);
212 if (r < 0)
213 return log_error_errno(r, "Failed to enumerate .dns-delegate files: %m");
214
215 STRV_FOREACH(f, files)
216 (void) dns_delegate_load(m, *f);
217
218 return 0;
219 }
220
221 int config_parse_delegate_dns_servers(
222 const char *unit,
223 const char *filename,
224 unsigned line,
225 const char *section,
226 unsigned section_line,
227 const char *lvalue,
228 int ltype,
229 const char *rvalue,
230 void *data,
231 void *userdata) {
232
233 DnsDelegate *d = ASSERT_PTR(userdata);
234 int r;
235
236 assert(filename);
237 assert(lvalue);
238 assert(rvalue);
239
240 /* Empty assignment means clear the list */
241 if (isempty(rvalue)) {
242 dns_server_unlink_all(d->dns_servers);
243 return 0;
244 }
245
246 /* Otherwise, add to the list */
247 for (;;) {
248 _cleanup_free_ char *word = NULL;
249 r = extract_first_word(&rvalue, &word, NULL, 0);
250 if (r < 0) {
251 log_syntax(unit, LOG_WARNING, filename, line, r,
252 "Failed to parse DNS server string '%s', ignoring.", rvalue);
253 return 0;
254 }
255 if (r == 0)
256 break;
257
258 _cleanup_free_ char *server_name = NULL;
259 union in_addr_union address;
260 int family, ifindex = 0;
261 uint16_t port;
262 r = in_addr_port_ifindex_name_from_string_auto(word, &family, &address, &port, &ifindex, &server_name);
263 if (r < 0) {
264 log_syntax(unit, LOG_WARNING, filename, line, r,
265 "Failed to parse DNS server string '%s', ignoring.", word);
266 continue;
267 }
268
269 /* Silently filter out 0.0.0.0, 127.0.0.53, 127.0.0.54 (our own stub DNS listener) */
270 if (!dns_server_address_valid(family, &address))
271 continue;
272
273 /* By default, the port number is determined with the transaction feature level.
274 * See dns_transaction_port() and dns_server_port(). */
275 if (IN_SET(port, 53, 853))
276 port = 0;
277
278 /* Filter out duplicates */
279 DnsServer *s = dns_server_find(d->dns_servers, family, &address, port, ifindex, server_name);
280 if (s) {
281 /* Drop the marker. This is used to find the servers that ceased to exist, see
282 * manager_mark_dns_servers() and manager_flush_marked_dns_servers(). */
283 dns_server_move_back_and_unmark(s);
284 return 0;
285 }
286
287 r = dns_server_new(
288 d->manager,
289 /* ret= */ NULL,
290 DNS_SERVER_DELEGATE,
291 /* link= */ NULL,
292 d,
293 family,
294 &address,
295 port,
296 ifindex,
297 server_name,
298 RESOLVE_CONFIG_SOURCE_FILE);
299 if (r < 0)
300 return log_error_errno(r, "Failed to add DNS server: %m");
301 }
302
303 return 0;
304 }
305
306 int config_parse_delegate_domains(
307 const char *unit,
308 const char *filename,
309 unsigned line,
310 const char *section,
311 unsigned section_line,
312 const char *lvalue,
313 int ltype,
314 const char *rvalue,
315 void *data,
316 void *userdata) {
317
318 DnsDelegate *d = ASSERT_PTR(userdata);
319 int r;
320
321 assert(filename);
322 assert(lvalue);
323 assert(rvalue);
324
325 /* Empty assignment means clear the list */
326 if (isempty(rvalue)) {
327 dns_search_domain_unlink_all(d->search_domains);
328 return 0;
329 }
330
331 /* Otherwise, add to the list */
332 for (;;) {
333 _cleanup_free_ char *word = NULL;
334 r = extract_first_word(&rvalue, &word, NULL, 0);
335 if (r < 0) {
336 log_syntax(unit, LOG_WARNING, filename, line, r,
337 "Failed to parse search domains string '%s', ignoring.", rvalue);
338 return 0;
339 }
340 if (r == 0)
341 break;
342
343 const char *name = word;
344
345 bool route_only = name[0] == '~';
346 if (route_only)
347 name++;
348
349 if (dns_name_is_root(name) || streq(name, "*")) {
350 route_only = true;
351 name = ".";
352 }
353
354 DnsSearchDomain *domain;
355 r = dns_search_domain_find(d->search_domains, name, &domain);
356 if (r < 0)
357 return log_error_errno(r, "Failed to find search domain: %m");
358 if (r > 0)
359 dns_search_domain_move_back_and_unmark(domain);
360 else {
361 r = dns_search_domain_new(d->manager, &domain, DNS_SEARCH_DOMAIN_DELEGATE, /* link= */ NULL, d, name);
362 if (r < 0)
363 return r;
364 }
365
366 domain->route_only = route_only;
367 }
368
369 return 0;
370 }