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