/src/systemd/src/resolve/resolved-dns-question.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | | |
3 | | #include "alloc-util.h" |
4 | | #include "dns-domain.h" |
5 | | #include "dns-type.h" |
6 | | #include "resolved-dns-question.h" |
7 | | |
8 | 979 | DnsQuestion *dns_question_new(size_t n) { |
9 | 979 | DnsQuestion *q; |
10 | 979 | |
11 | 979 | assert(n > 0); |
12 | 979 | |
13 | 979 | q = malloc0(offsetof(DnsQuestion, keys) + sizeof(DnsResourceKey*) * n); |
14 | 979 | if (!q) |
15 | 0 | return NULL; |
16 | 979 | |
17 | 979 | q->n_ref = 1; |
18 | 979 | q->n_allocated = n; |
19 | 979 | |
20 | 979 | return q; |
21 | 979 | } |
22 | | |
23 | 979 | static DnsQuestion *dns_question_free(DnsQuestion *q) { |
24 | 979 | size_t i; |
25 | 979 | |
26 | 979 | assert(q); |
27 | 979 | |
28 | 15.0k | for (i = 0; i < q->n_keys; i++) |
29 | 14.0k | dns_resource_key_unref(q->keys[i]); |
30 | 979 | return mfree(q); |
31 | 979 | } |
32 | | |
33 | | DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsQuestion, dns_question, dns_question_free); |
34 | | |
35 | 14.0k | int dns_question_add_raw(DnsQuestion *q, DnsResourceKey *key) { |
36 | 14.0k | /* Insert without checking for duplicates. */ |
37 | 14.0k | |
38 | 14.0k | assert(key); |
39 | 14.0k | assert(q); |
40 | 14.0k | |
41 | 14.0k | if (q->n_keys >= q->n_allocated) |
42 | 0 | return -ENOSPC; |
43 | 14.0k | |
44 | 14.0k | q->keys[q->n_keys++] = dns_resource_key_ref(key); |
45 | 14.0k | return 0; |
46 | 14.0k | } |
47 | | |
48 | 0 | int dns_question_add(DnsQuestion *q, DnsResourceKey *key) { |
49 | 0 | int r; |
50 | 0 |
|
51 | 0 | assert(key); |
52 | 0 |
|
53 | 0 | if (!q) |
54 | 0 | return -ENOSPC; |
55 | 0 | |
56 | 0 | for (size_t i = 0; i < q->n_keys; i++) { |
57 | 0 | r = dns_resource_key_equal(q->keys[i], key); |
58 | 0 | if (r < 0) |
59 | 0 | return r; |
60 | 0 | if (r > 0) |
61 | 0 | return 0; |
62 | 0 | } |
63 | 0 |
|
64 | 0 | return dns_question_add_raw(q, key); |
65 | 0 | } |
66 | | |
67 | 0 | int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) { |
68 | 0 | size_t i; |
69 | 0 | int r; |
70 | 0 |
|
71 | 0 | assert(rr); |
72 | 0 |
|
73 | 0 | if (!q) |
74 | 0 | return 0; |
75 | 0 | |
76 | 0 | for (i = 0; i < q->n_keys; i++) { |
77 | 0 | r = dns_resource_key_match_rr(q->keys[i], rr, search_domain); |
78 | 0 | if (r != 0) |
79 | 0 | return r; |
80 | 0 | } |
81 | 0 |
|
82 | 0 | return 0; |
83 | 0 | } |
84 | | |
85 | 0 | int dns_question_matches_cname_or_dname(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) { |
86 | 0 | size_t i; |
87 | 0 | int r; |
88 | 0 |
|
89 | 0 | assert(rr); |
90 | 0 |
|
91 | 0 | if (!q) |
92 | 0 | return 0; |
93 | 0 | |
94 | 0 | if (!IN_SET(rr->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME)) |
95 | 0 | return 0; |
96 | 0 | |
97 | 0 | for (i = 0; i < q->n_keys; i++) { |
98 | 0 | /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */ |
99 | 0 | if (!dns_type_may_redirect(q->keys[i]->type)) |
100 | 0 | return 0; |
101 | 0 | |
102 | 0 | r = dns_resource_key_match_cname_or_dname(q->keys[i], rr->key, search_domain); |
103 | 0 | if (r != 0) |
104 | 0 | return r; |
105 | 0 | } |
106 | 0 |
|
107 | 0 | return 0; |
108 | 0 | } |
109 | | |
110 | 0 | int dns_question_is_valid_for_query(DnsQuestion *q) { |
111 | 0 | const char *name; |
112 | 0 | size_t i; |
113 | 0 | int r; |
114 | 0 |
|
115 | 0 | if (!q) |
116 | 0 | return 0; |
117 | 0 | |
118 | 0 | if (q->n_keys <= 0) |
119 | 0 | return 0; |
120 | 0 | |
121 | 0 | if (q->n_keys > 65535) |
122 | 0 | return 0; |
123 | 0 | |
124 | 0 | name = dns_resource_key_name(q->keys[0]); |
125 | 0 | if (!name) |
126 | 0 | return 0; |
127 | 0 | |
128 | 0 | /* Check that all keys in this question bear the same name */ |
129 | 0 | for (i = 0; i < q->n_keys; i++) { |
130 | 0 | assert(q->keys[i]); |
131 | 0 |
|
132 | 0 | if (i > 0) { |
133 | 0 | r = dns_name_equal(dns_resource_key_name(q->keys[i]), name); |
134 | 0 | if (r <= 0) |
135 | 0 | return r; |
136 | 0 | } |
137 | 0 | |
138 | 0 | if (!dns_type_is_valid_query(q->keys[i]->type)) |
139 | 0 | return 0; |
140 | 0 | } |
141 | 0 |
|
142 | 0 | return 1; |
143 | 0 | } |
144 | | |
145 | 0 | int dns_question_contains(DnsQuestion *a, const DnsResourceKey *k) { |
146 | 0 | size_t j; |
147 | 0 | int r; |
148 | 0 |
|
149 | 0 | assert(k); |
150 | 0 |
|
151 | 0 | if (!a) |
152 | 0 | return 0; |
153 | 0 | |
154 | 0 | for (j = 0; j < a->n_keys; j++) { |
155 | 0 | r = dns_resource_key_equal(a->keys[j], k); |
156 | 0 | if (r != 0) |
157 | 0 | return r; |
158 | 0 | } |
159 | 0 |
|
160 | 0 | return 0; |
161 | 0 | } |
162 | | |
163 | 0 | int dns_question_is_equal(DnsQuestion *a, DnsQuestion *b) { |
164 | 0 | size_t j; |
165 | 0 | int r; |
166 | 0 |
|
167 | 0 | if (a == b) |
168 | 0 | return 1; |
169 | 0 | |
170 | 0 | if (!a) |
171 | 0 | return !b || b->n_keys == 0; |
172 | 0 | if (!b) |
173 | 0 | return a->n_keys == 0; |
174 | 0 | |
175 | 0 | /* Checks if all keys in a are also contained b, and vice versa */ |
176 | 0 | |
177 | 0 | for (j = 0; j < a->n_keys; j++) { |
178 | 0 | r = dns_question_contains(b, a->keys[j]); |
179 | 0 | if (r <= 0) |
180 | 0 | return r; |
181 | 0 | } |
182 | 0 |
|
183 | 0 | for (j = 0; j < b->n_keys; j++) { |
184 | 0 | r = dns_question_contains(a, b->keys[j]); |
185 | 0 | if (r <= 0) |
186 | 0 | return r; |
187 | 0 | } |
188 | 0 |
|
189 | 0 | return 1; |
190 | 0 | } |
191 | | |
192 | 0 | int dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname, DnsQuestion **ret) { |
193 | 0 | _cleanup_(dns_question_unrefp) DnsQuestion *n = NULL; |
194 | 0 | DnsResourceKey *key; |
195 | 0 | bool same = true; |
196 | 0 | int r; |
197 | 0 |
|
198 | 0 | assert(cname); |
199 | 0 | assert(ret); |
200 | 0 | assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME)); |
201 | 0 |
|
202 | 0 | if (dns_question_size(q) <= 0) { |
203 | 0 | *ret = NULL; |
204 | 0 | return 0; |
205 | 0 | } |
206 | 0 | |
207 | 0 | DNS_QUESTION_FOREACH(key, q) { |
208 | 0 | _cleanup_free_ char *destination = NULL; |
209 | 0 | const char *d; |
210 | 0 |
|
211 | 0 | if (cname->key->type == DNS_TYPE_CNAME) |
212 | 0 | d = cname->cname.name; |
213 | 0 | else { |
214 | 0 | r = dns_name_change_suffix(dns_resource_key_name(key), dns_resource_key_name(cname->key), cname->dname.name, &destination); |
215 | 0 | if (r < 0) |
216 | 0 | return r; |
217 | 0 | if (r == 0) |
218 | 0 | continue; |
219 | 0 | |
220 | 0 | d = destination; |
221 | 0 | } |
222 | 0 |
|
223 | 0 | r = dns_name_equal(dns_resource_key_name(key), d); |
224 | 0 | if (r < 0) |
225 | 0 | return r; |
226 | 0 | |
227 | 0 | if (r == 0) { |
228 | 0 | same = false; |
229 | 0 | break; |
230 | 0 | } |
231 | 0 | } |
232 | 0 |
|
233 | 0 | /* Fully the same, indicate we didn't do a thing */ |
234 | 0 | if (same) { |
235 | 0 | *ret = NULL; |
236 | 0 | return 0; |
237 | 0 | } |
238 | 0 | |
239 | 0 | n = dns_question_new(q->n_keys); |
240 | 0 | if (!n) |
241 | 0 | return -ENOMEM; |
242 | 0 | |
243 | 0 | /* Create a new question, and patch in the new name */ |
244 | 0 | DNS_QUESTION_FOREACH(key, q) { |
245 | 0 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL; |
246 | 0 |
|
247 | 0 | k = dns_resource_key_new_redirect(key, cname); |
248 | 0 | if (!k) |
249 | 0 | return -ENOMEM; |
250 | 0 | |
251 | 0 | r = dns_question_add(n, k); |
252 | 0 | if (r < 0) |
253 | 0 | return r; |
254 | 0 | } |
255 | 0 |
|
256 | 0 | *ret = TAKE_PTR(n); |
257 | 0 |
|
258 | 0 | return 1; |
259 | 0 | } |
260 | | |
261 | 0 | const char *dns_question_first_name(DnsQuestion *q) { |
262 | 0 |
|
263 | 0 | if (!q) |
264 | 0 | return NULL; |
265 | 0 | |
266 | 0 | if (q->n_keys < 1) |
267 | 0 | return NULL; |
268 | 0 | |
269 | 0 | return dns_resource_key_name(q->keys[0]); |
270 | 0 | } |
271 | | |
272 | 0 | int dns_question_new_address(DnsQuestion **ret, int family, const char *name, bool convert_idna) { |
273 | 0 | _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL; |
274 | 0 | _cleanup_free_ char *buf = NULL; |
275 | 0 | int r; |
276 | 0 |
|
277 | 0 | assert(ret); |
278 | 0 | assert(name); |
279 | 0 |
|
280 | 0 | if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC)) |
281 | 0 | return -EAFNOSUPPORT; |
282 | 0 | |
283 | 0 | if (convert_idna) { |
284 | 0 | r = dns_name_apply_idna(name, &buf); |
285 | 0 | if (r < 0) |
286 | 0 | return r; |
287 | 0 | if (r > 0 && !streq(name, buf)) |
288 | 0 | name = buf; |
289 | 0 | else |
290 | 0 | /* We did not manage to create convert the idna name, or it's |
291 | 0 | * the same as the original name. We assume the caller already |
292 | 0 | * created an unconverted question, so let's not repeat work |
293 | 0 | * unnecessarily. */ |
294 | 0 | return -EALREADY; |
295 | 0 | } |
296 | 0 | |
297 | 0 | q = dns_question_new(family == AF_UNSPEC ? 2 : 1); |
298 | 0 | if (!q) |
299 | 0 | return -ENOMEM; |
300 | 0 | |
301 | 0 | if (family != AF_INET6) { |
302 | 0 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
303 | 0 |
|
304 | 0 | key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, name); |
305 | 0 | if (!key) |
306 | 0 | return -ENOMEM; |
307 | 0 | |
308 | 0 | r = dns_question_add(q, key); |
309 | 0 | if (r < 0) |
310 | 0 | return r; |
311 | 0 | } |
312 | 0 | |
313 | 0 | if (family != AF_INET) { |
314 | 0 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
315 | 0 |
|
316 | 0 | key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, name); |
317 | 0 | if (!key) |
318 | 0 | return -ENOMEM; |
319 | 0 | |
320 | 0 | r = dns_question_add(q, key); |
321 | 0 | if (r < 0) |
322 | 0 | return r; |
323 | 0 | } |
324 | 0 | |
325 | 0 | *ret = TAKE_PTR(q); |
326 | 0 |
|
327 | 0 | return 0; |
328 | 0 | } |
329 | | |
330 | 0 | int dns_question_new_reverse(DnsQuestion **ret, int family, const union in_addr_union *a) { |
331 | 0 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
332 | 0 | _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL; |
333 | 0 | _cleanup_free_ char *reverse = NULL; |
334 | 0 | int r; |
335 | 0 |
|
336 | 0 | assert(ret); |
337 | 0 | assert(a); |
338 | 0 |
|
339 | 0 | if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC)) |
340 | 0 | return -EAFNOSUPPORT; |
341 | 0 | |
342 | 0 | r = dns_name_reverse(family, a, &reverse); |
343 | 0 | if (r < 0) |
344 | 0 | return r; |
345 | 0 | |
346 | 0 | q = dns_question_new(1); |
347 | 0 | if (!q) |
348 | 0 | return -ENOMEM; |
349 | 0 | |
350 | 0 | key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, reverse); |
351 | 0 | if (!key) |
352 | 0 | return -ENOMEM; |
353 | 0 | |
354 | 0 | reverse = NULL; |
355 | 0 |
|
356 | 0 | r = dns_question_add(q, key); |
357 | 0 | if (r < 0) |
358 | 0 | return r; |
359 | 0 | |
360 | 0 | *ret = TAKE_PTR(q); |
361 | 0 |
|
362 | 0 | return 0; |
363 | 0 | } |
364 | | |
365 | | int dns_question_new_service( |
366 | | DnsQuestion **ret, |
367 | | const char *service, |
368 | | const char *type, |
369 | | const char *domain, |
370 | | bool with_txt, |
371 | 0 | bool convert_idna) { |
372 | 0 |
|
373 | 0 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
374 | 0 | _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL; |
375 | 0 | _cleanup_free_ char *buf = NULL, *joined = NULL; |
376 | 0 | const char *name; |
377 | 0 | int r; |
378 | 0 |
|
379 | 0 | assert(ret); |
380 | 0 |
|
381 | 0 | /* We support three modes of invocation: |
382 | 0 | * |
383 | 0 | * 1. Only a domain is specified, in which case we assume a properly encoded SRV RR name, including service |
384 | 0 | * type and possibly a service name. If specified in this way we assume it's already IDNA converted if |
385 | 0 | * that's necessary. |
386 | 0 | * |
387 | 0 | * 2. Both service type and a domain specified, in which case a normal SRV RR is assumed, without a DNS-SD |
388 | 0 | * style prefix. In this case we'll IDNA convert the domain, if that's requested. |
389 | 0 | * |
390 | 0 | * 3. All three of service name, type and domain are specified, in which case a DNS-SD service is put |
391 | 0 | * together. The service name is never IDNA converted, and the domain is if requested. |
392 | 0 | * |
393 | 0 | * It's not supported to specify a service name without a type, or no domain name. |
394 | 0 | */ |
395 | 0 |
|
396 | 0 | if (!domain) |
397 | 0 | return -EINVAL; |
398 | 0 | |
399 | 0 | if (type) { |
400 | 0 | if (convert_idna) { |
401 | 0 | r = dns_name_apply_idna(domain, &buf); |
402 | 0 | if (r < 0) |
403 | 0 | return r; |
404 | 0 | if (r > 0) |
405 | 0 | domain = buf; |
406 | 0 | } |
407 | 0 |
|
408 | 0 | r = dns_service_join(service, type, domain, &joined); |
409 | 0 | if (r < 0) |
410 | 0 | return r; |
411 | 0 | |
412 | 0 | name = joined; |
413 | 0 | } else { |
414 | 0 | if (service) |
415 | 0 | return -EINVAL; |
416 | 0 | |
417 | 0 | name = domain; |
418 | 0 | } |
419 | 0 |
|
420 | 0 | q = dns_question_new(1 + with_txt); |
421 | 0 | if (!q) |
422 | 0 | return -ENOMEM; |
423 | 0 | |
424 | 0 | key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, name); |
425 | 0 | if (!key) |
426 | 0 | return -ENOMEM; |
427 | 0 | |
428 | 0 | r = dns_question_add(q, key); |
429 | 0 | if (r < 0) |
430 | 0 | return r; |
431 | 0 | |
432 | 0 | if (with_txt) { |
433 | 0 | dns_resource_key_unref(key); |
434 | 0 | key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_TXT, name); |
435 | 0 | if (!key) |
436 | 0 | return -ENOMEM; |
437 | 0 | |
438 | 0 | r = dns_question_add(q, key); |
439 | 0 | if (r < 0) |
440 | 0 | return r; |
441 | 0 | } |
442 | 0 | |
443 | 0 | *ret = TAKE_PTR(q); |
444 | 0 |
|
445 | 0 | return 0; |
446 | 0 | } |