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