Debian Codegraph @

src/network/networkd-dns.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "conf-parser.h"
5 #include "dns-domain.h"
6 #include "extract-word.h"
7 #include "hostname-util.h"
8 #include "networkd-dns.h"
9 #include "networkd-link.h"
10 #include "networkd-manager.h"
11 #include "networkd-network.h"
12 #include "ordered-set.h"
13 #include "set.h"
14 #include "string-table.h"
15 #include "string-util.h"
16
17 UseDomains link_get_use_domains(Link *link, NetworkConfigSource proto) {
18 UseDomains n, c, m;
19
20 assert(link);
21 assert(link->manager);
22
23 if (!link->network)
24 return USE_DOMAINS_NO;
25
26 switch (proto) {
27 case NETWORK_CONFIG_SOURCE_DHCP4:
28 n = link->network->dhcp_use_domains;
29 c = link->network->compat_dhcp_use_domains;
30 m = link->manager->dhcp_use_domains;
31 break;
32 case NETWORK_CONFIG_SOURCE_DHCP6:
33 n = link->network->dhcp6_use_domains;
34 c = link->network->compat_dhcp_use_domains;
35 m = link->manager->dhcp6_use_domains;
36 break;
37 case NETWORK_CONFIG_SOURCE_NDISC:
38 n = link->network->ndisc_use_domains;
39 c = _USE_DOMAINS_INVALID;
40 m = link->manager->ndisc_use_domains;
41 break;
42 default:
43 assert_not_reached();
44 }
45
46 /* If per-network and per-protocol setting is specified, use it. */
47 if (n >= 0)
48 return n;
49
50 /* If compat setting is specified, use it. */
51 if (c >= 0)
52 return c;
53
54 /* If per-network but protocol-independent setting is specified, use it. */
55 if (link->network->use_domains >= 0)
56 return link->network->use_domains;
57
58 /* If global per-protocol setting is specified, use it. */
59 if (m >= 0)
60 return m;
61
62 /* If none of them are specified, use the global protocol-independent value. */
63 return link->manager->use_domains;
64 }
65
66 bool link_get_use_dns(Link *link, NetworkConfigSource proto) {
67 int n, c;
68
69 assert(link);
70
71 if (!link->network)
72 return false;
73
74 switch (proto) {
75 case NETWORK_CONFIG_SOURCE_DHCP4:
76 n = link->network->dhcp_use_dns;
77 c = link->network->compat_dhcp_use_dns;
78 break;
79 case NETWORK_CONFIG_SOURCE_DHCP6:
80 n = link->network->dhcp6_use_dns;
81 c = link->network->compat_dhcp_use_dns;
82 break;
83 case NETWORK_CONFIG_SOURCE_NDISC:
84 n = link->network->ndisc_use_dns;
85 c = -1;
86 break;
87 default:
88 assert_not_reached();
89 }
90
91 /* If per-network and per-protocol setting is specified, use it. */
92 if (n >= 0)
93 return n;
94
95 /* If compat setting is specified, use it. */
96 if (c >= 0)
97 return c;
98
99 /* Otherwise, defaults to yes. */
100 return true;
101 }
102
103 bool link_get_use_dnr(Link *link, NetworkConfigSource proto) {
104 int n;
105
106 assert(link);
107
108 if (!link->network)
109 return false;
110
111 switch (proto) {
112 case NETWORK_CONFIG_SOURCE_DHCP4:
113 n = link->network->dhcp_use_dnr;
114 break;
115 case NETWORK_CONFIG_SOURCE_DHCP6:
116 n = link->network->dhcp6_use_dnr;
117 break;
118 case NETWORK_CONFIG_SOURCE_NDISC:
119 n = link->network->ndisc_use_dnr;
120 break;
121 default:
122 assert_not_reached();
123 }
124
125 /* If set explicitly, use that */
126 if (n >= 0)
127 return n;
128
129 /* Otherwise, default to the same as the UseDNS setting. After all,
130 * this is just another way for the server to tell us about DNS configuration. */
131 return link_get_use_dns(link, proto);
132 }
133
134 int config_parse_domains(
135 const char *unit,
136 const char *filename,
137 unsigned line,
138 const char *section,
139 unsigned section_line,
140 const char *lvalue,
141 int ltype,
142 const char *rvalue,
143 void *data,
144 void *userdata) {
145
146 Network *n = ASSERT_PTR(userdata);
147 int r;
148
149 assert(filename);
150 assert(lvalue);
151 assert(rvalue);
152
153 if (isempty(rvalue)) {
154 n->search_domains = ordered_set_free(n->search_domains);
155 n->route_domains = ordered_set_free(n->route_domains);
156 return 0;
157 }
158
159 for (const char *p = rvalue;;) {
160 _cleanup_free_ char *w = NULL, *normalized = NULL;
161 const char *domain;
162 bool is_route;
163
164 r = extract_first_word(&p, &w, NULL, 0);
165 if (r == -ENOMEM)
166 return log_oom();
167 if (r < 0) {
168 log_syntax(unit, LOG_WARNING, filename, line, r,
169 "Failed to extract search or route domain, ignoring: %s", rvalue);
170 return 0;
171 }
172 if (r == 0)
173 return 0;
174
175 is_route = w[0] == '~';
176 domain = is_route ? w + 1 : w;
177
178 if (dns_name_is_root(domain) || streq(domain, "*")) {
179 /* If the root domain appears as is, or the special token "*" is found, we'll
180 * consider this as routing domain, unconditionally. */
181 is_route = true;
182 domain = "."; /* make sure we don't allow empty strings, thus write the root
183 * domain as "." */
184 } else {
185 r = dns_name_normalize(domain, 0, &normalized);
186 if (r < 0) {
187 log_syntax(unit, LOG_WARNING, filename, line, r,
188 "'%s' is not a valid domain name, ignoring.", domain);
189 continue;
190 }
191
192 domain = normalized;
193
194 if (is_localhost(domain)) {
195 log_syntax(unit, LOG_WARNING, filename, line, 0,
196 "'localhost' domain may not be configured as search or route domain, ignoring assignment: %s",
197 domain);
198 continue;
199 }
200 }
201
202 OrderedSet **set = is_route ? &n->route_domains : &n->search_domains;
203 r = ordered_set_put_strdup_full(set, &dns_name_hash_ops_free, domain);
204 if (r == -EEXIST)
205 continue;
206 if (r < 0)
207 return log_oom();
208 }
209 }
210
211 int config_parse_dns(
212 const char *unit,
213 const char *filename,
214 unsigned line,
215 const char *section,
216 unsigned section_line,
217 const char *lvalue,
218 int ltype,
219 const char *rvalue,
220 void *data,
221 void *userdata) {
222
223 Network *n = ASSERT_PTR(userdata);
224 int r;
225
226 assert(filename);
227 assert(lvalue);
228 assert(rvalue);
229
230 if (isempty(rvalue)) {
231 for (unsigned i = 0; i < n->n_dns; i++)
232 in_addr_full_free(n->dns[i]);
233 n->dns = mfree(n->dns);
234 n->n_dns = 0;
235 return 0;
236 }
237
238 for (const char *p = rvalue;;) {
239 _cleanup_(in_addr_full_freep) struct in_addr_full *dns = NULL;
240 _cleanup_free_ char *w = NULL;
241
242 r = extract_first_word(&p, &w, NULL, 0);
243 if (r == -ENOMEM)
244 return log_oom();
245 if (r < 0) {
246 log_syntax(unit, LOG_WARNING, filename, line, r,
247 "Invalid syntax, ignoring: %s", rvalue);
248 return 0;
249 }
250 if (r == 0)
251 return 0;
252
253 r = in_addr_full_new_from_string(w, &dns);
254 if (r < 0) {
255 log_syntax(unit, LOG_WARNING, filename, line, r,
256 "Failed to parse dns server address, ignoring: %s", w);
257 continue;
258 }
259
260 if (IN_SET(dns->port, 53, 853))
261 dns->port = 0;
262
263 if (!GREEDY_REALLOC(n->dns, n->n_dns + 1))
264 return log_oom();
265
266 n->dns[n->n_dns++] = TAKE_PTR(dns);
267 }
268 }
269
270 int config_parse_dnssec_negative_trust_anchors(
271 const char *unit,
272 const char *filename,
273 unsigned line,
274 const char *section,
275 unsigned section_line,
276 const char *lvalue,
277 int ltype,
278 const char *rvalue,
279 void *data,
280 void *userdata) {
281
282 Set **nta = ASSERT_PTR(data);
283 int r;
284
285 assert(filename);
286 assert(lvalue);
287 assert(rvalue);
288
289 if (isempty(rvalue)) {
290 *nta = set_free(*nta);
291 return 0;
292 }
293
294 for (const char *p = rvalue;;) {
295 _cleanup_free_ char *w = NULL;
296
297 r = extract_first_word(&p, &w, NULL, 0);
298 if (r == -ENOMEM)
299 return log_oom();
300 if (r < 0) {
301 log_syntax(unit, LOG_WARNING, filename, line, r,
302 "Failed to extract negative trust anchor domain, ignoring: %s", rvalue);
303 return 0;
304 }
305 if (r == 0)
306 return 0;
307
308 r = dns_name_is_valid(w);
309 if (r <= 0) {
310 log_syntax(unit, LOG_WARNING, filename, line, r,
311 "%s is not a valid domain name, ignoring.", w);
312 continue;
313 }
314
315 r = set_ensure_consume(nta, &dns_name_hash_ops_free, TAKE_PTR(w));
316 if (r < 0)
317 return log_oom();
318 }
319 }
320
321 static const char* const use_domains_table[_USE_DOMAINS_MAX] = {
322 [USE_DOMAINS_NO] = "no",
323 [USE_DOMAINS_ROUTE] = "route",
324 [USE_DOMAINS_YES] = "yes",
325 };
326
327 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(use_domains, UseDomains, USE_DOMAINS_YES);
328 DEFINE_CONFIG_PARSE_ENUM(config_parse_use_domains, use_domains, UseDomains);