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