]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-question.c
Merge pull request #2621 from keszybz/wheel-group
[thirdparty/systemd.git] / src / resolve / resolved-dns-question.c
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
20 #include "alloc-util.h"
21 #include "dns-domain.h"
22 #include "dns-type.h"
23 #include "resolved-dns-question.h"
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) {
68 unsigned i;
69 int r;
70
71 assert(key);
72
73 if (!q)
74 return -ENOSPC;
75
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
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
91 int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {
92 unsigned i;
93 int r;
94
95 assert(rr);
96
97 if (!q)
98 return 0;
99
100 for (i = 0; i < q->n_keys; i++) {
101 r = dns_resource_key_match_rr(q->keys[i], rr, search_domain);
102 if (r != 0)
103 return r;
104 }
105
106 return 0;
107 }
108
109 int dns_question_matches_cname_or_dname(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {
110 unsigned i;
111 int r;
112
113 assert(rr);
114
115 if (!q)
116 return 0;
117
118 if (!IN_SET(rr->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME))
119 return 0;
120
121 for (i = 0; i < q->n_keys; i++) {
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
126 r = dns_resource_key_match_cname_or_dname(q->keys[i], rr->key, search_domain);
127 if (r != 0)
128 return r;
129 }
130
131 return 0;
132 }
133
134 int dns_question_is_valid_for_query(DnsQuestion *q) {
135 const char *name;
136 unsigned i;
137 int r;
138
139 if (!q)
140 return 0;
141
142 if (q->n_keys <= 0)
143 return 0;
144
145 if (q->n_keys > 65535)
146 return 0;
147
148 name = dns_resource_key_name(q->keys[0]);
149 if (!name)
150 return 0;
151
152 /* Check that all keys in this question bear the same name */
153 for (i = 0; i < q->n_keys; i++) {
154 assert(q->keys[i]);
155
156 if (i > 0) {
157 r = dns_name_equal(dns_resource_key_name(q->keys[i]), name);
158 if (r <= 0)
159 return r;
160 }
161
162 if (!dns_type_is_valid_query(q->keys[i]->type))
163 return 0;
164 }
165
166 return 1;
167 }
168
169 int dns_question_contains(DnsQuestion *a, const DnsResourceKey *k) {
170 unsigned j;
171 int r;
172
173 assert(k);
174
175 if (!a)
176 return 0;
177
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
191 if (a == b)
192 return 1;
193
194 if (!a)
195 return !b || b->n_keys == 0;
196 if (!b)
197 return a->n_keys == 0;
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
216 int dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname, DnsQuestion **ret) {
217 _cleanup_(dns_question_unrefp) DnsQuestion *n = NULL;
218 DnsResourceKey *key;
219 bool same = true;
220 int r;
221
222 assert(cname);
223 assert(ret);
224 assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
225
226 if (dns_question_size(q) <= 0) {
227 *ret = NULL;
228 return 0;
229 }
230
231 DNS_QUESTION_FOREACH(key, q) {
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 {
238 r = dns_name_change_suffix(dns_resource_key_name(key), dns_resource_key_name(cname->key), cname->dname.name, &destination);
239 if (r < 0)
240 return r;
241 if (r == 0)
242 continue;
243
244 d = destination;
245 }
246
247 r = dns_name_equal(dns_resource_key_name(key), d);
248 if (r < 0)
249 return r;
250
251 if (r == 0) {
252 same = false;
253 break;
254 }
255 }
256
257 /* Fully the same, indicate we didn't do a thing */
258 if (same) {
259 *ret = NULL;
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 */
268 DNS_QUESTION_FOREACH(key, q) {
269 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL;
270
271 k = dns_resource_key_new_redirect(key, cname);
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 }
285
286 const char *dns_question_first_name(DnsQuestion *q) {
287
288 if (!q)
289 return NULL;
290
291 if (q->n_keys < 1)
292 return NULL;
293
294 return dns_resource_key_name(q->keys[0]);
295 }
296
297 int dns_question_new_address(DnsQuestion **ret, int family, const char *name, bool convert_idna) {
298 _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
299 _cleanup_free_ char *buf = NULL;
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
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
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
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
394 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
395 _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
396 _cleanup_free_ char *buf = NULL, *joined = NULL;
397 const char *name;
398 int r;
399
400 assert(ret);
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 }
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 }