resolved: explicitly refuse zone transfers using the bus API
[thirdparty/systemd.git] / src / resolve / resolved-dns-rr.c
CommitLineData
74b2466e
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
0dae31d4
ZJS
20#include <math.h>
21
b5efdb8a 22#include "alloc-util.h"
4ad7f276 23#include "dns-domain.h"
7263f724 24#include "dns-type.h"
95052df3 25#include "escape.h"
e4e73a63 26#include "hexdecoct.h"
fc8eec10 27#include "resolved-dns-dnssec.h"
07630cea 28#include "resolved-dns-packet.h"
e4e73a63 29#include "resolved-dns-rr.h"
8730bccf 30#include "string-table.h"
07630cea
LP
31#include "string-util.h"
32#include "strv.h"
d7671a3e 33#include "terminal-util.h"
74b2466e 34
faa133f3
LP
35DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
36 DnsResourceKey *k;
37 size_t l;
74b2466e 38
faa133f3
LP
39 assert(name);
40
41 l = strlen(name);
42 k = malloc0(sizeof(DnsResourceKey) + l + 1);
43 if (!k)
44 return NULL;
45
46 k->n_ref = 1;
47 k->class = class;
48 k->type = type;
49
50 strcpy((char*) k + sizeof(DnsResourceKey), name);
51
52 return k;
53}
54
36d9205d 55DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname) {
58db254a
LP
56 int r;
57
36d9205d
TG
58 assert(key);
59 assert(cname);
60
58db254a
LP
61 assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
62
63 if (cname->key->type == DNS_TYPE_CNAME)
64 return dns_resource_key_new(key->class, key->type, cname->cname.name);
65 else {
66 DnsResourceKey *k;
67 char *destination = NULL;
68
1c02e7ba 69 r = dns_name_change_suffix(dns_resource_key_name(key), dns_resource_key_name(cname->key), cname->dname.name, &destination);
58db254a
LP
70 if (r < 0)
71 return NULL;
72 if (r == 0)
73 return dns_resource_key_ref((DnsResourceKey*) key);
74
75 k = dns_resource_key_new_consume(key->class, key->type, destination);
76 if (!k) {
77 free(destination);
78 return NULL;
79 }
80
81 return k;
82 }
36d9205d
TG
83}
84
801ad6a6
LP
85int dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name) {
86 DnsResourceKey *new_key;
87 char *joined;
88 int r;
89
90 assert(ret);
91 assert(key);
92 assert(name);
93
dc477e73 94 if (dns_name_is_root(name)) {
801ad6a6
LP
95 *ret = dns_resource_key_ref(key);
96 return 0;
97 }
98
1c02e7ba 99 r = dns_name_concat(dns_resource_key_name(key), name, &joined);
801ad6a6
LP
100 if (r < 0)
101 return r;
102
103 new_key = dns_resource_key_new_consume(key->class, key->type, joined);
104 if (!new_key) {
105 free(joined);
106 return -ENOMEM;
107 }
108
109 *ret = new_key;
110 return 0;
111}
112
faa133f3
LP
113DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
114 DnsResourceKey *k;
115
116 assert(name);
117
118 k = new0(DnsResourceKey, 1);
119 if (!k)
120 return NULL;
121
122 k->n_ref = 1;
123 k->class = class;
124 k->type = type;
125 k->_name = name;
126
127 return k;
128}
129
130DnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
131
132 if (!k)
133 return NULL;
134
1b4f6e79
LP
135 /* Static/const keys created with DNS_RESOURCE_KEY_CONST will
136 * set this to -1, they should not be reffed/unreffed */
137 assert(k->n_ref != (unsigned) -1);
138
faa133f3
LP
139 assert(k->n_ref > 0);
140 k->n_ref++;
141
142 return k;
143}
144
145DnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
146 if (!k)
147 return NULL;
148
1b4f6e79 149 assert(k->n_ref != (unsigned) -1);
faa133f3
LP
150 assert(k->n_ref > 0);
151
152 if (k->n_ref == 1) {
153 free(k->_name);
154 free(k);
155 } else
156 k->n_ref--;
157
158 return NULL;
159}
160
1c02e7ba
ZJS
161const char* dns_resource_key_name(const DnsResourceKey *key) {
162 const char *name;
163
164 if (!key)
165 return NULL;
166
167 if (key->_name)
168 name = key->_name;
169 else
170 name = (char*) key + sizeof(DnsResourceKey);
171
172 if (dns_name_is_root(name))
173 return ".";
174 else
175 return name;
176}
177
28b9b764
LP
178bool dns_resource_key_is_address(const DnsResourceKey *key) {
179 assert(key);
180
181 /* Check if this is an A or AAAA resource key */
182
183 return key->class == DNS_CLASS_IN && IN_SET(key->type, DNS_TYPE_A, DNS_TYPE_AAAA);
184}
185
faa133f3
LP
186int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
187 int r;
188
4d247a6c
LP
189 if (a == b)
190 return 1;
191
1c02e7ba 192 r = dns_name_equal(dns_resource_key_name(a), dns_resource_key_name(b));
faa133f3
LP
193 if (r <= 0)
194 return r;
195
196 if (a->class != b->class)
197 return 0;
198
199 if (a->type != b->type)
200 return 0;
201
202 return 1;
203}
204
105e1512 205int dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain) {
801ad6a6
LP
206 int r;
207
faa133f3
LP
208 assert(key);
209 assert(rr);
210
4d247a6c
LP
211 if (key == rr->key)
212 return 1;
213
801ad6a6
LP
214 /* Checks if an rr matches the specified key. If a search
215 * domain is specified, it will also be checked if the key
216 * with the search domain suffixed might match the RR. */
217
faa133f3
LP
218 if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
219 return 0;
220
221 if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
222 return 0;
223
1c02e7ba 224 r = dns_name_equal(dns_resource_key_name(rr->key), dns_resource_key_name(key));
801ad6a6
LP
225 if (r != 0)
226 return r;
227
228 if (search_domain) {
229 _cleanup_free_ char *joined = NULL;
230
1c02e7ba 231 r = dns_name_concat(dns_resource_key_name(key), search_domain, &joined);
801ad6a6
LP
232 if (r < 0)
233 return r;
234
1c02e7ba 235 return dns_name_equal(dns_resource_key_name(rr->key), joined);
801ad6a6
LP
236 }
237
238 return 0;
faa133f3
LP
239}
240
5d27351f 241int dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain) {
801ad6a6
LP
242 int r;
243
faa133f3 244 assert(key);
5d27351f 245 assert(cname);
faa133f3 246
5d27351f 247 if (cname->class != key->class && key->class != DNS_CLASS_ANY)
faa133f3
LP
248 return 0;
249
5d27351f 250 if (cname->type == DNS_TYPE_CNAME)
1c02e7ba 251 r = dns_name_equal(dns_resource_key_name(key), dns_resource_key_name(cname));
5d27351f 252 else if (cname->type == DNS_TYPE_DNAME)
1c02e7ba 253 r = dns_name_endswith(dns_resource_key_name(key), dns_resource_key_name(cname));
58db254a 254 else
faa133f3 255 return 0;
801ad6a6
LP
256
257 if (r != 0)
258 return r;
259
260 if (search_domain) {
261 _cleanup_free_ char *joined = NULL;
262
1c02e7ba 263 r = dns_name_concat(dns_resource_key_name(key), search_domain, &joined);
801ad6a6
LP
264 if (r < 0)
265 return r;
266
5d27351f 267 if (cname->type == DNS_TYPE_CNAME)
1c02e7ba 268 return dns_name_equal(joined, dns_resource_key_name(cname));
5d27351f 269 else if (cname->type == DNS_TYPE_DNAME)
1c02e7ba 270 return dns_name_endswith(joined, dns_resource_key_name(cname));
801ad6a6
LP
271 }
272
273 return 0;
547973de
LP
274}
275
276int dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa) {
277 assert(soa);
278 assert(key);
279
280 /* Checks whether 'soa' is a SOA record for the specified key. */
281
65b200e7 282 if (soa->class != key->class)
547973de 283 return 0;
801ad6a6 284
547973de
LP
285 if (soa->type != DNS_TYPE_SOA)
286 return 0;
287
1c02e7ba 288 return dns_name_endswith(dns_resource_key_name(key), dns_resource_key_name(soa));
74b2466e
LP
289}
290
b826ab58 291static void dns_resource_key_hash_func(const void *i, struct siphash *state) {
322345fd 292 const DnsResourceKey *k = i;
322345fd 293
b826ab58 294 assert(k);
322345fd 295
1c02e7ba 296 dns_name_hash_func(dns_resource_key_name(k), state);
b826ab58
TG
297 siphash24_compress(&k->class, sizeof(k->class), state);
298 siphash24_compress(&k->type, sizeof(k->type), state);
322345fd
LP
299}
300
d5099efc 301static int dns_resource_key_compare_func(const void *a, const void *b) {
322345fd
LP
302 const DnsResourceKey *x = a, *y = b;
303 int ret;
304
1c02e7ba 305 ret = dns_name_compare_func(dns_resource_key_name(x), dns_resource_key_name(y));
322345fd
LP
306 if (ret != 0)
307 return ret;
308
309 if (x->type < y->type)
310 return -1;
311 if (x->type > y->type)
312 return 1;
313
314 if (x->class < y->class)
315 return -1;
316 if (x->class > y->class)
317 return 1;
318
319 return 0;
320}
321
d5099efc
MS
322const struct hash_ops dns_resource_key_hash_ops = {
323 .hash = dns_resource_key_hash_func,
324 .compare = dns_resource_key_compare_func
325};
326
202b76ae 327char* dns_resource_key_to_string(const DnsResourceKey *key, char *buf, size_t buf_size) {
1c02e7ba 328 const char *c, *t;
202b76ae 329 char *ans = buf;
2d4c5cbc 330
6af47493
LP
331 /* If we cannot convert the CLASS/TYPE into a known string,
332 use the format recommended by RFC 3597, Section 5. */
333
2d4c5cbc 334 c = dns_class_to_string(key->class);
2d4c5cbc 335 t = dns_type_to_string(key->type);
2d4c5cbc 336
202b76ae
ZJS
337 snprintf(buf, buf_size, "%s %s%s%.0u %s%s%.0u",
338 dns_resource_key_name(key),
339 c ?: "", c ? "" : "CLASS", c ? 0 : key->class,
340 t ?: "", t ? "" : "TYPE", t ? 0 : key->class);
2d4c5cbc 341
202b76ae 342 return ans;
2d4c5cbc
LP
343}
344
f57e3cd5
LP
345bool dns_resource_key_reduce(DnsResourceKey **a, DnsResourceKey **b) {
346 assert(a);
347 assert(b);
348
349 /* Try to replace one RR key by another if they are identical, thus saving a bit of memory. Note that we do
350 * this only for RR keys, not for RRs themselves, as they carry a lot of additional metadata (where they come
351 * from, validity data, and suchlike), and cannot be replaced so easily by other RRs that have the same
352 * superficial data. */
353
354 if (!*a)
355 return false;
356 if (!*b)
357 return false;
358
359 /* We refuse merging const keys */
360 if ((*a)->n_ref == (unsigned) -1)
361 return false;
362 if ((*b)->n_ref == (unsigned) -1)
363 return false;
364
365 /* Already the same? */
366 if (*a == *b)
367 return true;
368
369 /* Are they really identical? */
370 if (dns_resource_key_equal(*a, *b) <= 0)
371 return false;
372
373 /* Keep the one which already has more references. */
374 if ((*a)->n_ref > (*b)->n_ref) {
375 dns_resource_key_unref(*b);
376 *b = dns_resource_key_ref(*a);
377 } else {
378 dns_resource_key_unref(*a);
379 *a = dns_resource_key_ref(*b);
380 }
381
382 return true;
383}
384
faa133f3 385DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
74b2466e
LP
386 DnsResourceRecord *rr;
387
388 rr = new0(DnsResourceRecord, 1);
389 if (!rr)
390 return NULL;
391
392 rr->n_ref = 1;
faa133f3 393 rr->key = dns_resource_key_ref(key);
ee3d6aff 394 rr->expiry = USEC_INFINITY;
97c67192 395 rr->n_skip_labels_signer = rr->n_skip_labels_source = (unsigned) -1;
faa133f3 396
74b2466e
LP
397 return rr;
398}
399
8bf52d3d
LP
400DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
401 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
402
403 key = dns_resource_key_new(class, type, name);
404 if (!key)
405 return NULL;
406
407 return dns_resource_record_new(key);
408}
409
74b2466e
LP
410DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
411 if (!rr)
412 return NULL;
413
414 assert(rr->n_ref > 0);
415 rr->n_ref++;
416
417 return rr;
418}
419
420DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
421 if (!rr)
422 return NULL;
423
424 assert(rr->n_ref > 0);
425
426 if (rr->n_ref > 1) {
427 rr->n_ref--;
428 return NULL;
429 }
430
faa133f3 431 if (rr->key) {
9de3e329 432 switch(rr->key->type) {
9c92ce6d
LP
433
434 case DNS_TYPE_SRV:
435 free(rr->srv.name);
436 break;
437
9de3e329
ZJS
438 case DNS_TYPE_PTR:
439 case DNS_TYPE_NS:
440 case DNS_TYPE_CNAME:
8ac4e9e1 441 case DNS_TYPE_DNAME:
faa133f3 442 free(rr->ptr.name);
9de3e329 443 break;
9c92ce6d 444
9de3e329 445 case DNS_TYPE_HINFO:
faa133f3
LP
446 free(rr->hinfo.cpu);
447 free(rr->hinfo.os);
9de3e329 448 break;
9c92ce6d 449
9de3e329 450 case DNS_TYPE_TXT:
9c92ce6d 451 case DNS_TYPE_SPF:
2001c805 452 dns_txt_item_free_all(rr->txt.items);
9de3e329 453 break;
9c92ce6d 454
9de3e329 455 case DNS_TYPE_SOA:
7e8e0422
LP
456 free(rr->soa.mname);
457 free(rr->soa.rname);
9de3e329 458 break;
9c92ce6d 459
9de3e329 460 case DNS_TYPE_MX:
946c7094 461 free(rr->mx.exchange);
9de3e329 462 break;
9c92ce6d 463
abf126a3
TG
464 case DNS_TYPE_DS:
465 free(rr->ds.digest);
466 break;
467
42cc2eeb 468 case DNS_TYPE_SSHFP:
549c1a25 469 free(rr->sshfp.fingerprint);
42cc2eeb
LP
470 break;
471
8db0d2f5
ZJS
472 case DNS_TYPE_DNSKEY:
473 free(rr->dnskey.key);
474 break;
475
151226ab
ZJS
476 case DNS_TYPE_RRSIG:
477 free(rr->rrsig.signer);
478 free(rr->rrsig.signature);
479 break;
480
50f1e641
TG
481 case DNS_TYPE_NSEC:
482 free(rr->nsec.next_domain_name);
483 bitmap_free(rr->nsec.types);
484 break;
485
5d45a880
TG
486 case DNS_TYPE_NSEC3:
487 free(rr->nsec3.next_hashed_name);
488 free(rr->nsec3.salt);
489 bitmap_free(rr->nsec3.types);
490 break;
491
0dae31d4 492 case DNS_TYPE_LOC:
9de3e329
ZJS
493 case DNS_TYPE_A:
494 case DNS_TYPE_AAAA:
495 break;
9c92ce6d 496
48d45d2b
ZJS
497 case DNS_TYPE_TLSA:
498 free(rr->tlsa.data);
499 break;
500
95052df3
ZJS
501 case DNS_TYPE_CAA:
502 free(rr->caa.tag);
503 free(rr->caa.value);
504 break;
505
d93a16b8 506 case DNS_TYPE_OPENPGPKEY:
9de3e329 507 default:
faa133f3 508 free(rr->generic.data);
9de3e329 509 }
322345fd 510
a8812dd7 511 free(rr->wire_format);
faa133f3
LP
512 dns_resource_key_unref(rr->key);
513 }
322345fd 514
7b50eb2e 515 free(rr->to_string);
faa133f3 516 free(rr);
322345fd 517
322345fd
LP
518 return NULL;
519}
520
623a4c97
LP
521int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
522 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
523 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
524 _cleanup_free_ char *ptr = NULL;
525 int r;
526
527 assert(ret);
528 assert(address);
529 assert(hostname);
530
531 r = dns_name_reverse(family, address, &ptr);
532 if (r < 0)
533 return r;
534
535 key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
536 if (!key)
537 return -ENOMEM;
538
539 ptr = NULL;
540
541 rr = dns_resource_record_new(key);
542 if (!rr)
543 return -ENOMEM;
544
545 rr->ptr.name = strdup(hostname);
546 if (!rr->ptr.name)
547 return -ENOMEM;
548
549 *ret = rr;
550 rr = NULL;
551
552 return 0;
553}
554
78c6a153
LP
555int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name) {
556 DnsResourceRecord *rr;
557
558 assert(ret);
559 assert(address);
560 assert(family);
561
562 if (family == AF_INET) {
563
564 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, name);
565 if (!rr)
566 return -ENOMEM;
567
568 rr->a.in_addr = address->in;
569
570 } else if (family == AF_INET6) {
571
572 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
573 if (!rr)
574 return -ENOMEM;
575
576 rr->aaaa.in6_addr = address->in6;
577 } else
578 return -EAFNOSUPPORT;
579
580 *ret = rr;
581
582 return 0;
583}
584
a43a068a
ZJS
585#define FIELD_EQUAL(a, b, field) \
586 ((a).field ## _size == (b).field ## _size && \
587 memcmp((a).field, (b).field, (a).field ## _size) == 0)
588
322345fd
LP
589int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
590 int r;
591
592 assert(a);
593 assert(b);
594
4d247a6c
LP
595 if (a == b)
596 return 1;
597
faa133f3 598 r = dns_resource_key_equal(a->key, b->key);
322345fd
LP
599 if (r <= 0)
600 return r;
601
fd0b4602
LP
602 if (a->unparseable != b->unparseable)
603 return 0;
604
605 switch (a->unparseable ? _DNS_TYPE_INVALID : a->key->type) {
2d4c5cbc 606
9c92ce6d
LP
607 case DNS_TYPE_SRV:
608 r = dns_name_equal(a->srv.name, b->srv.name);
609 if (r <= 0)
610 return r;
611
612 return a->srv.priority == b->srv.priority &&
613 a->srv.weight == b->srv.weight &&
614 a->srv.port == b->srv.port;
615
2d4c5cbc
LP
616 case DNS_TYPE_PTR:
617 case DNS_TYPE_NS:
618 case DNS_TYPE_CNAME:
8ac4e9e1 619 case DNS_TYPE_DNAME:
322345fd 620 return dns_name_equal(a->ptr.name, b->ptr.name);
2d4c5cbc
LP
621
622 case DNS_TYPE_HINFO:
623 return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
624 strcaseeq(a->hinfo.os, b->hinfo.os);
625
9de3e329 626 case DNS_TYPE_SPF: /* exactly the same as TXT */
0f84a72e 627 case DNS_TYPE_TXT:
2001c805 628 return dns_txt_item_equal(a->txt.items, b->txt.items);
2e276efc 629
2d4c5cbc 630 case DNS_TYPE_A:
322345fd 631 return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
2d4c5cbc
LP
632
633 case DNS_TYPE_AAAA:
322345fd 634 return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
2d4c5cbc
LP
635
636 case DNS_TYPE_SOA:
7e8e0422
LP
637 r = dns_name_equal(a->soa.mname, b->soa.mname);
638 if (r <= 0)
639 return r;
640 r = dns_name_equal(a->soa.rname, b->soa.rname);
641 if (r <= 0)
642 return r;
643
644 return a->soa.serial == b->soa.serial &&
645 a->soa.refresh == b->soa.refresh &&
646 a->soa.retry == b->soa.retry &&
647 a->soa.expire == b->soa.expire &&
648 a->soa.minimum == b->soa.minimum;
9c92ce6d 649
946c7094
ZJS
650 case DNS_TYPE_MX:
651 if (a->mx.priority != b->mx.priority)
652 return 0;
653
654 return dns_name_equal(a->mx.exchange, b->mx.exchange);
655
0dae31d4
ZJS
656 case DNS_TYPE_LOC:
657 assert(a->loc.version == b->loc.version);
658
659 return a->loc.size == b->loc.size &&
660 a->loc.horiz_pre == b->loc.horiz_pre &&
661 a->loc.vert_pre == b->loc.vert_pre &&
662 a->loc.latitude == b->loc.latitude &&
663 a->loc.longitude == b->loc.longitude &&
664 a->loc.altitude == b->loc.altitude;
665
abf126a3
TG
666 case DNS_TYPE_DS:
667 return a->ds.key_tag == b->ds.key_tag &&
668 a->ds.algorithm == b->ds.algorithm &&
669 a->ds.digest_type == b->ds.digest_type &&
a43a068a 670 FIELD_EQUAL(a->ds, b->ds, digest);
abf126a3 671
42cc2eeb
LP
672 case DNS_TYPE_SSHFP:
673 return a->sshfp.algorithm == b->sshfp.algorithm &&
674 a->sshfp.fptype == b->sshfp.fptype &&
a43a068a 675 FIELD_EQUAL(a->sshfp, b->sshfp, fingerprint);
42cc2eeb 676
8db0d2f5 677 case DNS_TYPE_DNSKEY:
f91dc240
LP
678 return a->dnskey.flags == b->dnskey.flags &&
679 a->dnskey.protocol == b->dnskey.protocol &&
8db0d2f5 680 a->dnskey.algorithm == b->dnskey.algorithm &&
a43a068a 681 FIELD_EQUAL(a->dnskey, b->dnskey, key);
8db0d2f5 682
151226ab
ZJS
683 case DNS_TYPE_RRSIG:
684 /* do the fast comparisons first */
a43a068a
ZJS
685 return a->rrsig.type_covered == b->rrsig.type_covered &&
686 a->rrsig.algorithm == b->rrsig.algorithm &&
687 a->rrsig.labels == b->rrsig.labels &&
688 a->rrsig.original_ttl == b->rrsig.original_ttl &&
689 a->rrsig.expiration == b->rrsig.expiration &&
690 a->rrsig.inception == b->rrsig.inception &&
691 a->rrsig.key_tag == b->rrsig.key_tag &&
692 FIELD_EQUAL(a->rrsig, b->rrsig, signature) &&
693 dns_name_equal(a->rrsig.signer, b->rrsig.signer);
151226ab 694
50f1e641
TG
695 case DNS_TYPE_NSEC:
696 return dns_name_equal(a->nsec.next_domain_name, b->nsec.next_domain_name) &&
697 bitmap_equal(a->nsec.types, b->nsec.types);
698
5d45a880
TG
699 case DNS_TYPE_NSEC3:
700 return a->nsec3.algorithm == b->nsec3.algorithm &&
a43a068a
ZJS
701 a->nsec3.flags == b->nsec3.flags &&
702 a->nsec3.iterations == b->nsec3.iterations &&
703 FIELD_EQUAL(a->nsec3, b->nsec3, salt) &&
704 FIELD_EQUAL(a->nsec3, b->nsec3, next_hashed_name) &&
705 bitmap_equal(a->nsec3.types, b->nsec3.types);
5d45a880 706
48d45d2b
ZJS
707 case DNS_TYPE_TLSA:
708 return a->tlsa.cert_usage == b->tlsa.cert_usage &&
709 a->tlsa.selector == b->tlsa.selector &&
710 a->tlsa.matching_type == b->tlsa.matching_type &&
a43a068a 711 FIELD_EQUAL(a->tlsa, b->tlsa, data);
48d45d2b 712
95052df3
ZJS
713 case DNS_TYPE_CAA:
714 return a->caa.flags == b->caa.flags &&
715 streq(a->caa.tag, b->caa.tag) &&
716 FIELD_EQUAL(a->caa, b->caa, value);
717
718 case DNS_TYPE_OPENPGPKEY:
2d4c5cbc 719 default:
a43a068a 720 return FIELD_EQUAL(a->generic, b->generic, data);
2d4c5cbc 721 }
322345fd
LP
722}
723
0dae31d4
ZJS
724static char* format_location(uint32_t latitude, uint32_t longitude, uint32_t altitude,
725 uint8_t size, uint8_t horiz_pre, uint8_t vert_pre) {
726 char *s;
727 char NS = latitude >= 1U<<31 ? 'N' : 'S';
728 char EW = longitude >= 1U<<31 ? 'E' : 'W';
729
730 int lat = latitude >= 1U<<31 ? (int) (latitude - (1U<<31)) : (int) ((1U<<31) - latitude);
731 int lon = longitude >= 1U<<31 ? (int) (longitude - (1U<<31)) : (int) ((1U<<31) - longitude);
732 double alt = altitude >= 10000000u ? altitude - 10000000u : -(double)(10000000u - altitude);
733 double siz = (size >> 4) * exp10((double) (size & 0xF));
734 double hor = (horiz_pre >> 4) * exp10((double) (horiz_pre & 0xF));
735 double ver = (vert_pre >> 4) * exp10((double) (vert_pre & 0xF));
736
737 if (asprintf(&s, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
738 (lat / 60000 / 60),
739 (lat / 60000) % 60,
740 (lat % 60000) / 1000.,
741 NS,
742 (lon / 60000 / 60),
743 (lon / 60000) % 60,
744 (lon % 60000) / 1000.,
745 EW,
746 alt / 100.,
747 siz / 100.,
748 hor / 100.,
749 ver / 100.) < 0)
750 return NULL;
751
752 return s;
753}
754
7c6423e1
TG
755static int format_timestamp_dns(char *buf, size_t l, time_t sec) {
756 struct tm tm;
757
758 assert(buf);
759 assert(l > strlen("YYYYMMDDHHmmSS"));
760
761 if (!gmtime_r(&sec, &tm))
762 return -EINVAL;
763
764 if (strftime(buf, l, "%Y%m%d%H%M%S", &tm) <= 0)
765 return -EINVAL;
766
767 return 0;
768}
769
50f1e641
TG
770static char *format_types(Bitmap *types) {
771 _cleanup_strv_free_ char **strv = NULL;
772 _cleanup_free_ char *str = NULL;
cb57dd41 773 Iterator i;
50f1e641
TG
774 unsigned type;
775 int r;
776
cb57dd41 777 BITMAP_FOREACH(type, types, i) {
50f1e641 778 if (dns_type_to_string(type)) {
2c1fb4f7 779 r = strv_extend(&strv, dns_type_to_string(type));
50f1e641
TG
780 if (r < 0)
781 return NULL;
782 } else {
783 char *t;
784
785 r = asprintf(&t, "TYPE%u", type);
786 if (r < 0)
787 return NULL;
788
2c1fb4f7 789 r = strv_consume(&strv, t);
50f1e641
TG
790 if (r < 0)
791 return NULL;
792 }
793 }
794
795 str = strv_join(strv, " ");
796 if (!str)
797 return NULL;
798
799 return strjoin("( ", str, " )", NULL);
800}
801
2001c805
LP
802static char *format_txt(DnsTxtItem *first) {
803 DnsTxtItem *i;
804 size_t c = 1;
805 char *p, *s;
806
807 LIST_FOREACH(items, i, first)
808 c += i->length * 4 + 3;
809
810 p = s = new(char, c);
811 if (!s)
812 return NULL;
813
814 LIST_FOREACH(items, i, first) {
815 size_t j;
816
817 if (i != first)
818 *(p++) = ' ';
819
820 *(p++) = '"';
821
822 for (j = 0; j < i->length; j++) {
823 if (i->data[j] < ' ' || i->data[j] == '"' || i->data[j] >= 127) {
824 *(p++) = '\\';
825 *(p++) = '0' + (i->data[j] / 100);
826 *(p++) = '0' + ((i->data[j] / 10) % 10);
827 *(p++) = '0' + (i->data[j] % 10);
828 } else
829 *(p++) = i->data[j];
830 }
831
832 *(p++) = '"';
833 }
834
835 *p = 0;
836 return s;
837}
838
7b50eb2e 839const char *dns_resource_record_to_string(DnsResourceRecord *rr) {
202b76ae
ZJS
840 _cleanup_free_ char *t = NULL;
841 char *s, k[DNS_RESOURCE_KEY_STRING_MAX];
2d4c5cbc 842 int r;
322345fd 843
2d4c5cbc 844 assert(rr);
322345fd 845
7b50eb2e
LP
846 if (rr->to_string)
847 return rr->to_string;
848
202b76ae 849 dns_resource_key_to_string(rr->key, k, sizeof(k));
322345fd 850
0dae31d4 851 switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
322345fd 852
9c92ce6d
LP
853 case DNS_TYPE_SRV:
854 r = asprintf(&s, "%s %u %u %u %s",
855 k,
856 rr->srv.priority,
857 rr->srv.weight,
858 rr->srv.port,
859 strna(rr->srv.name));
860 if (r < 0)
7b50eb2e 861 return NULL;
9c92ce6d
LP
862 break;
863
2d4c5cbc
LP
864 case DNS_TYPE_PTR:
865 case DNS_TYPE_NS:
866 case DNS_TYPE_CNAME:
8ac4e9e1 867 case DNS_TYPE_DNAME:
2d4c5cbc
LP
868 s = strjoin(k, " ", rr->ptr.name, NULL);
869 if (!s)
7b50eb2e 870 return NULL;
322345fd 871
2d4c5cbc 872 break;
322345fd 873
2d4c5cbc
LP
874 case DNS_TYPE_HINFO:
875 s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
876 if (!s)
7b50eb2e 877 return NULL;
2d4c5cbc 878 break;
322345fd 879
9de3e329 880 case DNS_TYPE_SPF: /* exactly the same as TXT */
8db0d2f5 881 case DNS_TYPE_TXT:
2001c805 882 t = format_txt(rr->txt.items);
2e276efc 883 if (!t)
7b50eb2e 884 return NULL;
2e276efc
ZJS
885
886 s = strjoin(k, " ", t, NULL);
887 if (!s)
7b50eb2e 888 return NULL;
2e276efc 889 break;
2e276efc 890
2d4c5cbc
LP
891 case DNS_TYPE_A: {
892 _cleanup_free_ char *x = NULL;
322345fd 893
2d4c5cbc
LP
894 r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
895 if (r < 0)
7b50eb2e 896 return NULL;
322345fd 897
2d4c5cbc
LP
898 s = strjoin(k, " ", x, NULL);
899 if (!s)
7b50eb2e 900 return NULL;
2d4c5cbc
LP
901 break;
902 }
322345fd 903
8db0d2f5
ZJS
904 case DNS_TYPE_AAAA:
905 r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &t);
2d4c5cbc 906 if (r < 0)
7b50eb2e 907 return NULL;
322345fd 908
8db0d2f5 909 s = strjoin(k, " ", t, NULL);
2d4c5cbc 910 if (!s)
7b50eb2e 911 return NULL;
2d4c5cbc 912 break;
322345fd 913
2d4c5cbc
LP
914 case DNS_TYPE_SOA:
915 r = asprintf(&s, "%s %s %s %u %u %u %u %u",
916 k,
917 strna(rr->soa.mname),
918 strna(rr->soa.rname),
919 rr->soa.serial,
920 rr->soa.refresh,
921 rr->soa.retry,
922 rr->soa.expire,
923 rr->soa.minimum);
924 if (r < 0)
7b50eb2e 925 return NULL;
2d4c5cbc
LP
926 break;
927
946c7094
ZJS
928 case DNS_TYPE_MX:
929 r = asprintf(&s, "%s %u %s",
930 k,
931 rr->mx.priority,
932 rr->mx.exchange);
933 if (r < 0)
7b50eb2e 934 return NULL;
946c7094
ZJS
935 break;
936
8db0d2f5 937 case DNS_TYPE_LOC:
0dae31d4
ZJS
938 assert(rr->loc.version == 0);
939
8db0d2f5
ZJS
940 t = format_location(rr->loc.latitude,
941 rr->loc.longitude,
942 rr->loc.altitude,
943 rr->loc.size,
944 rr->loc.horiz_pre,
945 rr->loc.vert_pre);
946 if (!t)
7b50eb2e 947 return NULL;
0dae31d4 948
8db0d2f5 949 s = strjoin(k, " ", t, NULL);
0dae31d4 950 if (!s)
7b50eb2e 951 return NULL;
0dae31d4 952 break;
0dae31d4 953
abf126a3
TG
954 case DNS_TYPE_DS:
955 t = hexmem(rr->ds.digest, rr->ds.digest_size);
956 if (!t)
7b50eb2e 957 return NULL;
abf126a3
TG
958
959 r = asprintf(&s, "%s %u %u %u %s",
960 k,
961 rr->ds.key_tag,
962 rr->ds.algorithm,
963 rr->ds.digest_type,
964 t);
965 if (r < 0)
7b50eb2e 966 return NULL;
abf126a3
TG
967 break;
968
8db0d2f5 969 case DNS_TYPE_SSHFP:
549c1a25 970 t = hexmem(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size);
8db0d2f5 971 if (!t)
7b50eb2e 972 return NULL;
42cc2eeb
LP
973
974 r = asprintf(&s, "%s %u %u %s",
975 k,
976 rr->sshfp.algorithm,
977 rr->sshfp.fptype,
8db0d2f5 978 t);
42cc2eeb 979 if (r < 0)
7b50eb2e 980 return NULL;
42cc2eeb 981 break;
42cc2eeb 982
ff3d6560 983 case DNS_TYPE_DNSKEY: {
8e54f5d9 984 _cleanup_free_ char *alg = NULL;
99e5ca6d 985 char *ss;
718af59e 986 int n;
fc8eec10
ZJS
987 uint16_t key_tag;
988
989 key_tag = dnssec_keytag(rr, true);
ff3d6560 990
8e54f5d9
LP
991 r = dnssec_algorithm_to_string_alloc(rr->dnskey.algorithm, &alg);
992 if (r < 0)
993 return NULL;
ff3d6560 994
718af59e 995 r = asprintf(&s, "%s %u %u %s %n",
8db0d2f5 996 k,
f91dc240
LP
997 rr->dnskey.flags,
998 rr->dnskey.protocol,
8e54f5d9 999 alg,
d7671a3e 1000 &n);
8db0d2f5 1001 if (r < 0)
7b50eb2e 1002 return NULL;
d7671a3e
ZJS
1003
1004 r = base64_append(&s, n,
1005 rr->dnskey.key, rr->dnskey.key_size,
1006 8, columns());
1007 if (r < 0)
1008 return NULL;
1009
99e5ca6d 1010 r = asprintf(&ss, "%s\n"
718af59e
ZJS
1011 " -- Flags:%s%s%s\n"
1012 " -- Key tag: %u",
99e5ca6d 1013 s,
99e5ca6d
ZJS
1014 rr->dnskey.flags & DNSKEY_FLAG_SEP ? " SEP" : "",
1015 rr->dnskey.flags & DNSKEY_FLAG_REVOKE ? " REVOKE" : "",
fc8eec10 1016 rr->dnskey.flags & DNSKEY_FLAG_ZONE_KEY ? " ZONE_KEY" : "",
fc8eec10 1017 key_tag);
99e5ca6d
ZJS
1018 if (r < 0)
1019 return NULL;
1020 free(s);
1021 s = ss;
1022
8db0d2f5 1023 break;
ff3d6560 1024 }
2d4c5cbc 1025
151226ab 1026 case DNS_TYPE_RRSIG: {
8e54f5d9 1027 _cleanup_free_ char *alg = NULL;
7c6423e1 1028 char expiration[strlen("YYYYMMDDHHmmSS") + 1], inception[strlen("YYYYMMDDHHmmSS") + 1];
8e54f5d9 1029 const char *type;
d7671a3e 1030 int n;
151226ab
ZJS
1031
1032 type = dns_type_to_string(rr->rrsig.type_covered);
8e54f5d9
LP
1033
1034 r = dnssec_algorithm_to_string_alloc(rr->rrsig.algorithm, &alg);
1035 if (r < 0)
1036 return NULL;
151226ab 1037
7c6423e1
TG
1038 r = format_timestamp_dns(expiration, sizeof(expiration), rr->rrsig.expiration);
1039 if (r < 0)
7b50eb2e 1040 return NULL;
7c6423e1
TG
1041
1042 r = format_timestamp_dns(inception, sizeof(inception), rr->rrsig.inception);
1043 if (r < 0)
7b50eb2e 1044 return NULL;
7c6423e1 1045
151226ab
ZJS
1046 /* TYPE?? follows
1047 * http://tools.ietf.org/html/rfc3597#section-5 */
1048
d7671a3e 1049 r = asprintf(&s, "%s %s%.*u %s %u %u %s %s %u %s %n",
151226ab
ZJS
1050 k,
1051 type ?: "TYPE",
1052 type ? 0 : 1, type ? 0u : (unsigned) rr->rrsig.type_covered,
8e54f5d9 1053 alg,
151226ab
ZJS
1054 rr->rrsig.labels,
1055 rr->rrsig.original_ttl,
7c6423e1
TG
1056 expiration,
1057 inception,
151226ab
ZJS
1058 rr->rrsig.key_tag,
1059 rr->rrsig.signer,
d7671a3e 1060 &n);
151226ab 1061 if (r < 0)
7b50eb2e 1062 return NULL;
d7671a3e
ZJS
1063
1064 r = base64_append(&s, n,
1065 rr->rrsig.signature, rr->rrsig.signature_size,
1066 8, columns());
1067 if (r < 0)
1068 return NULL;
1069
151226ab
ZJS
1070 break;
1071 }
1072
50f1e641
TG
1073 case DNS_TYPE_NSEC:
1074 t = format_types(rr->nsec.types);
1075 if (!t)
7b50eb2e 1076 return NULL;
50f1e641
TG
1077
1078 r = asprintf(&s, "%s %s %s",
1079 k,
1080 rr->nsec.next_domain_name,
1081 t);
1082 if (r < 0)
7b50eb2e 1083 return NULL;
50f1e641
TG
1084 break;
1085
5d45a880
TG
1086 case DNS_TYPE_NSEC3: {
1087 _cleanup_free_ char *salt = NULL, *hash = NULL;
1088
f5430a3e 1089 if (rr->nsec3.salt_size > 0) {
5d45a880
TG
1090 salt = hexmem(rr->nsec3.salt, rr->nsec3.salt_size);
1091 if (!salt)
7b50eb2e 1092 return NULL;
5d45a880
TG
1093 }
1094
1095 hash = base32hexmem(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, false);
1096 if (!hash)
7b50eb2e 1097 return NULL;
5d45a880
TG
1098
1099 t = format_types(rr->nsec3.types);
1100 if (!t)
7b50eb2e 1101 return NULL;
5d45a880
TG
1102
1103 r = asprintf(&s, "%s %"PRIu8" %"PRIu8" %"PRIu16" %s %s %s",
1104 k,
1105 rr->nsec3.algorithm,
1106 rr->nsec3.flags,
1107 rr->nsec3.iterations,
f5430a3e 1108 rr->nsec3.salt_size > 0 ? salt : "-",
5d45a880
TG
1109 hash,
1110 t);
1111 if (r < 0)
7b50eb2e 1112 return NULL;
5d45a880
TG
1113
1114 break;
1115 }
1116
48d45d2b 1117 case DNS_TYPE_TLSA: {
cfb90da3 1118 const char *cert_usage, *selector, *matching_type;
48d45d2b 1119
cfb90da3
ZJS
1120 cert_usage = tlsa_cert_usage_to_string(rr->tlsa.cert_usage);
1121 selector = tlsa_selector_to_string(rr->tlsa.selector);
1122 matching_type = tlsa_matching_type_to_string(rr->tlsa.matching_type);
1123
236d312b
ZJS
1124 t = hexmem(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size);
1125 if (!t)
48d45d2b 1126 return NULL;
cfb90da3 1127
236d312b
ZJS
1128 r = asprintf(&s,
1129 "%s %u %u %u %s\n"
718af59e
ZJS
1130 " -- Cert. usage: %s\n"
1131 " -- Selector: %s\n"
1132 " -- Matching type: %s",
236d312b
ZJS
1133 k,
1134 rr->tlsa.cert_usage,
1135 rr->tlsa.selector,
1136 rr->tlsa.matching_type,
1137 t,
718af59e
ZJS
1138 cert_usage,
1139 selector,
1140 matching_type);
cfb90da3
ZJS
1141 if (r < 0)
1142 return NULL;
cfb90da3 1143
48d45d2b
ZJS
1144 break;
1145 }
1146
95052df3
ZJS
1147 case DNS_TYPE_CAA: {
1148 _cleanup_free_ char *value;
1149
1150 value = octescape(rr->caa.value, rr->caa.value_size);
1151 if (!value)
1152 return NULL;
1153
718af59e 1154 r = asprintf(&s, "%s %u %s \"%s\"%s%s%s%.0u",
95052df3
ZJS
1155 k,
1156 rr->caa.flags,
1157 rr->caa.tag,
718af59e
ZJS
1158 value,
1159 rr->caa.flags ? "\n -- Flags:" : "",
1160 rr->caa.flags & CAA_FLAG_CRITICAL ? " critical" : "",
1161 rr->caa.flags & ~CAA_FLAG_CRITICAL ? " " : "",
1162 rr->caa.flags & ~CAA_FLAG_CRITICAL);
95052df3
ZJS
1163 if (r < 0)
1164 return NULL;
1165
1166 break;
1167 }
1168
d93a16b8
ZJS
1169 case DNS_TYPE_OPENPGPKEY: {
1170 int n;
1171
1172 r = asprintf(&s, "%s %n",
1173 k,
1174 &n);
1175 if (r < 0)
1176 return NULL;
1177
1178 r = base64_append(&s, n,
a43a068a 1179 rr->generic.data, rr->generic.data_size,
d93a16b8
ZJS
1180 8, columns());
1181 if (r < 0)
1182 return NULL;
1183 break;
1184 }
1185
8db0d2f5 1186 default:
a43a068a 1187 t = hexmem(rr->generic.data, rr->generic.data_size);
8db0d2f5 1188 if (!t)
7b50eb2e 1189 return NULL;
8db0d2f5 1190
6af47493 1191 /* Format as documented in RFC 3597, Section 5 */
a43a068a 1192 r = asprintf(&s, "%s \\# %zu %s", k, rr->generic.data_size, t);
d23a27a9 1193 if (r < 0)
7b50eb2e 1194 return NULL;
2d4c5cbc 1195 break;
8db0d2f5 1196 }
2d4c5cbc 1197
7b50eb2e
LP
1198 rr->to_string = s;
1199 return s;
2d4c5cbc 1200}
322345fd 1201
2e74028a
ZJS
1202ssize_t dns_resource_record_payload(DnsResourceRecord *rr, void **out) {
1203 assert(rr);
1204 assert(out);
1205
1206 switch(rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
1207 case DNS_TYPE_SRV:
1208 case DNS_TYPE_PTR:
1209 case DNS_TYPE_NS:
1210 case DNS_TYPE_CNAME:
1211 case DNS_TYPE_DNAME:
1212 case DNS_TYPE_HINFO:
1213 case DNS_TYPE_SPF:
1214 case DNS_TYPE_TXT:
1215 case DNS_TYPE_A:
1216 case DNS_TYPE_AAAA:
1217 case DNS_TYPE_SOA:
1218 case DNS_TYPE_MX:
1219 case DNS_TYPE_LOC:
1220 case DNS_TYPE_DS:
2e74028a
ZJS
1221 case DNS_TYPE_DNSKEY:
1222 case DNS_TYPE_RRSIG:
1223 case DNS_TYPE_NSEC:
1224 case DNS_TYPE_NSEC3:
1225 return -EINVAL;
1226
e1caa6e0
ZJS
1227 case DNS_TYPE_SSHFP:
1228 *out = rr->sshfp.fingerprint;
1229 return rr->sshfp.fingerprint_size;
1230
2e74028a
ZJS
1231 case DNS_TYPE_TLSA:
1232 *out = rr->tlsa.data;
1233 return rr->tlsa.data_size;
1234
1235
1236 case DNS_TYPE_OPENPGPKEY:
1237 default:
1238 *out = rr->generic.data;
1239 return rr->generic.data_size;
1240 }
1241}
1242
a8812dd7
LP
1243int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical) {
1244
1245 DnsPacket packet = {
1246 .n_ref = 1,
1247 .protocol = DNS_PROTOCOL_DNS,
1248 .on_stack = true,
1249 .refuse_compression = true,
1250 .canonical_form = canonical,
1251 };
1252
1253 size_t start, rds;
1254 int r;
1255
1256 assert(rr);
1257
1258 /* Generates the RR in wire-format, optionally in the
1259 * canonical form as discussed in the DNSSEC RFC 4034, Section
1260 * 6.2. We allocate a throw-away DnsPacket object on the stack
1261 * here, because we need some book-keeping for memory
1262 * management, and can reuse the DnsPacket serializer, that
1263 * can generate the canonical form, too, but also knows label
1264 * compression and suchlike. */
1265
1266 if (rr->wire_format && rr->wire_format_canonical == canonical)
1267 return 0;
1268
1269 r = dns_packet_append_rr(&packet, rr, &start, &rds);
1270 if (r < 0)
1271 return r;
1272
1273 assert(start == 0);
1274 assert(packet._data);
1275
1276 free(rr->wire_format);
1277 rr->wire_format = packet._data;
1278 rr->wire_format_size = packet.size;
1279 rr->wire_format_rdata_offset = rds;
1280 rr->wire_format_canonical = canonical;
1281
1282 packet._data = NULL;
1283 dns_packet_unref(&packet);
1284
1285 return 0;
1286}
1287
97c67192
LP
1288int dns_resource_record_signer(DnsResourceRecord *rr, const char **ret) {
1289 const char *n;
1290 int r;
1291
1292 assert(rr);
1293 assert(ret);
1294
1295 /* Returns the RRset's signer, if it is known. */
1296
1297 if (rr->n_skip_labels_signer == (unsigned) -1)
1298 return -ENODATA;
1299
1c02e7ba 1300 n = dns_resource_key_name(rr->key);
97c67192
LP
1301 r = dns_name_skip(n, rr->n_skip_labels_signer, &n);
1302 if (r < 0)
1303 return r;
1304 if (r == 0)
1305 return -EINVAL;
1306
1307 *ret = n;
1308 return 0;
1309}
1310
1311int dns_resource_record_source(DnsResourceRecord *rr, const char **ret) {
1312 const char *n;
1313 int r;
1314
1315 assert(rr);
1316 assert(ret);
1317
1318 /* Returns the RRset's synthesizing source, if it is known. */
1319
1320 if (rr->n_skip_labels_source == (unsigned) -1)
1321 return -ENODATA;
1322
1c02e7ba 1323 n = dns_resource_key_name(rr->key);
97c67192
LP
1324 r = dns_name_skip(n, rr->n_skip_labels_source, &n);
1325 if (r < 0)
1326 return r;
1327 if (r == 0)
1328 return -EINVAL;
1329
1330 *ret = n;
1331 return 0;
1332}
1333
1334int dns_resource_record_is_signer(DnsResourceRecord *rr, const char *zone) {
1335 const char *signer;
1336 int r;
1337
ab481675
LP
1338 assert(rr);
1339
97c67192
LP
1340 r = dns_resource_record_signer(rr, &signer);
1341 if (r < 0)
1342 return r;
1343
1344 return dns_name_equal(zone, signer);
1345}
1346
ab481675
LP
1347int dns_resource_record_is_synthetic(DnsResourceRecord *rr) {
1348 int r;
1349
1350 assert(rr);
1351
1352 /* Returns > 0 if the RR is generated from a wildcard, and is not the asterisk name itself */
1353
1354 if (rr->n_skip_labels_source == (unsigned) -1)
1355 return -ENODATA;
1356
1357 if (rr->n_skip_labels_source == 0)
1358 return 0;
1359
1360 if (rr->n_skip_labels_source > 1)
1361 return 1;
1362
1c02e7ba 1363 r = dns_name_startswith(dns_resource_key_name(rr->key), "*");
ab481675
LP
1364 if (r < 0)
1365 return r;
1366
1367 return !r;
1368}
1369
6d99904f 1370void dns_resource_record_hash_func(const void *i, struct siphash *state) {
c9c72065
LP
1371 const DnsResourceRecord *rr = i;
1372
1373 assert(rr);
1374
1375 dns_resource_key_hash_func(rr->key, state);
1376
1377 switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
1378
1379 case DNS_TYPE_SRV:
1380 siphash24_compress(&rr->srv.priority, sizeof(rr->srv.priority), state);
1381 siphash24_compress(&rr->srv.weight, sizeof(rr->srv.weight), state);
1382 siphash24_compress(&rr->srv.port, sizeof(rr->srv.port), state);
1383 dns_name_hash_func(rr->srv.name, state);
1384 break;
1385
1386 case DNS_TYPE_PTR:
1387 case DNS_TYPE_NS:
1388 case DNS_TYPE_CNAME:
1389 case DNS_TYPE_DNAME:
1390 dns_name_hash_func(rr->ptr.name, state);
1391 break;
1392
1393 case DNS_TYPE_HINFO:
1394 string_hash_func(rr->hinfo.cpu, state);
1395 string_hash_func(rr->hinfo.os, state);
1396 break;
1397
1398 case DNS_TYPE_TXT:
1399 case DNS_TYPE_SPF: {
1400 DnsTxtItem *j;
1401
1402 LIST_FOREACH(items, j, rr->txt.items) {
1403 siphash24_compress(j->data, j->length, state);
1404
d5115566
LP
1405 /* Add an extra NUL byte, so that "a" followed by "b" doesn't result in the same hash as "ab"
1406 * followed by "". */
1407 siphash24_compress_byte(0, state);
c9c72065
LP
1408 }
1409 break;
1410 }
1411
1412 case DNS_TYPE_A:
1413 siphash24_compress(&rr->a.in_addr, sizeof(rr->a.in_addr), state);
1414 break;
1415
1416 case DNS_TYPE_AAAA:
1417 siphash24_compress(&rr->aaaa.in6_addr, sizeof(rr->aaaa.in6_addr), state);
1418 break;
1419
1420 case DNS_TYPE_SOA:
1421 dns_name_hash_func(rr->soa.mname, state);
1422 dns_name_hash_func(rr->soa.rname, state);
1423 siphash24_compress(&rr->soa.serial, sizeof(rr->soa.serial), state);
1424 siphash24_compress(&rr->soa.refresh, sizeof(rr->soa.refresh), state);
1425 siphash24_compress(&rr->soa.retry, sizeof(rr->soa.retry), state);
1426 siphash24_compress(&rr->soa.expire, sizeof(rr->soa.expire), state);
1427 siphash24_compress(&rr->soa.minimum, sizeof(rr->soa.minimum), state);
1428 break;
1429
1430 case DNS_TYPE_MX:
1431 siphash24_compress(&rr->mx.priority, sizeof(rr->mx.priority), state);
1432 dns_name_hash_func(rr->mx.exchange, state);
1433 break;
1434
1435 case DNS_TYPE_LOC:
1436 siphash24_compress(&rr->loc.version, sizeof(rr->loc.version), state);
1437 siphash24_compress(&rr->loc.size, sizeof(rr->loc.size), state);
1438 siphash24_compress(&rr->loc.horiz_pre, sizeof(rr->loc.horiz_pre), state);
1439 siphash24_compress(&rr->loc.vert_pre, sizeof(rr->loc.vert_pre), state);
1440 siphash24_compress(&rr->loc.latitude, sizeof(rr->loc.latitude), state);
1441 siphash24_compress(&rr->loc.longitude, sizeof(rr->loc.longitude), state);
1442 siphash24_compress(&rr->loc.altitude, sizeof(rr->loc.altitude), state);
1443 break;
1444
1445 case DNS_TYPE_SSHFP:
1446 siphash24_compress(&rr->sshfp.algorithm, sizeof(rr->sshfp.algorithm), state);
1447 siphash24_compress(&rr->sshfp.fptype, sizeof(rr->sshfp.fptype), state);
1448 siphash24_compress(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size, state);
1449 break;
1450
1451 case DNS_TYPE_DNSKEY:
1452 siphash24_compress(&rr->dnskey.flags, sizeof(rr->dnskey.flags), state);
1453 siphash24_compress(&rr->dnskey.protocol, sizeof(rr->dnskey.protocol), state);
1454 siphash24_compress(&rr->dnskey.algorithm, sizeof(rr->dnskey.algorithm), state);
1455 siphash24_compress(rr->dnskey.key, rr->dnskey.key_size, state);
1456 break;
1457
1458 case DNS_TYPE_RRSIG:
1459 siphash24_compress(&rr->rrsig.type_covered, sizeof(rr->rrsig.type_covered), state);
1460 siphash24_compress(&rr->rrsig.algorithm, sizeof(rr->rrsig.algorithm), state);
1461 siphash24_compress(&rr->rrsig.labels, sizeof(rr->rrsig.labels), state);
1462 siphash24_compress(&rr->rrsig.original_ttl, sizeof(rr->rrsig.original_ttl), state);
1463 siphash24_compress(&rr->rrsig.expiration, sizeof(rr->rrsig.expiration), state);
1464 siphash24_compress(&rr->rrsig.inception, sizeof(rr->rrsig.inception), state);
1465 siphash24_compress(&rr->rrsig.key_tag, sizeof(rr->rrsig.key_tag), state);
1466 dns_name_hash_func(rr->rrsig.signer, state);
1467 siphash24_compress(rr->rrsig.signature, rr->rrsig.signature_size, state);
1468 break;
1469
1470 case DNS_TYPE_NSEC:
1471 dns_name_hash_func(rr->nsec.next_domain_name, state);
1472 /* FIXME: we leave out the type bitmap here. Hash
1473 * would be better if we'd take it into account
1474 * too. */
1475 break;
1476
1477 case DNS_TYPE_DS:
1478 siphash24_compress(&rr->ds.key_tag, sizeof(rr->ds.key_tag), state);
1479 siphash24_compress(&rr->ds.algorithm, sizeof(rr->ds.algorithm), state);
1480 siphash24_compress(&rr->ds.digest_type, sizeof(rr->ds.digest_type), state);
1481 siphash24_compress(rr->ds.digest, rr->ds.digest_size, state);
1482 break;
1483
1484 case DNS_TYPE_NSEC3:
1485 siphash24_compress(&rr->nsec3.algorithm, sizeof(rr->nsec3.algorithm), state);
1486 siphash24_compress(&rr->nsec3.flags, sizeof(rr->nsec3.flags), state);
1487 siphash24_compress(&rr->nsec3.iterations, sizeof(rr->nsec3.iterations), state);
1488 siphash24_compress(rr->nsec3.salt, rr->nsec3.salt_size, state);
1489 siphash24_compress(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, state);
1490 /* FIXME: We leave the bitmaps out */
1491 break;
48d45d2b
ZJS
1492
1493 case DNS_TYPE_TLSA:
1494 siphash24_compress(&rr->tlsa.cert_usage, sizeof(rr->tlsa.cert_usage), state);
1495 siphash24_compress(&rr->tlsa.selector, sizeof(rr->tlsa.selector), state);
1496 siphash24_compress(&rr->tlsa.matching_type, sizeof(rr->tlsa.matching_type), state);
fa45182e 1497 siphash24_compress(rr->tlsa.data, rr->tlsa.data_size, state);
48d45d2b 1498 break;
c9c72065 1499
95052df3
ZJS
1500 case DNS_TYPE_CAA:
1501 siphash24_compress(&rr->caa.flags, sizeof(rr->caa.flags), state);
1502 string_hash_func(rr->caa.tag, state);
1503 siphash24_compress(rr->caa.value, rr->caa.value_size, state);
48d45d2b 1504 break;
c9c72065 1505
d93a16b8 1506 case DNS_TYPE_OPENPGPKEY:
c9c72065 1507 default:
a43a068a 1508 siphash24_compress(rr->generic.data, rr->generic.data_size, state);
c9c72065
LP
1509 break;
1510 }
1511}
1512
1513static int dns_resource_record_compare_func(const void *a, const void *b) {
1514 const DnsResourceRecord *x = a, *y = b;
1515 int ret;
1516
1517 ret = dns_resource_key_compare_func(x->key, y->key);
1518 if (ret != 0)
1519 return ret;
1520
1521 if (dns_resource_record_equal(x, y))
1522 return 0;
1523
1f133e0d 1524 /* This is a bit dirty, we don't implement proper ordering, but
c9c72065
LP
1525 * the hashtable doesn't need ordering anyway, hence we don't
1526 * care. */
1527 return x < y ? -1 : 1;
1528}
1529
1530const struct hash_ops dns_resource_record_hash_ops = {
1531 .hash = dns_resource_record_hash_func,
1532 .compare = dns_resource_record_compare_func,
1533};
1534
2001c805
LP
1535DnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i) {
1536 DnsTxtItem *n;
1537
1538 if (!i)
1539 return NULL;
1540
1541 n = i->items_next;
1542
1543 free(i);
1544 return dns_txt_item_free_all(n);
1545}
1546
1547bool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b) {
1548
4d247a6c
LP
1549 if (a == b)
1550 return true;
1551
2001c805
LP
1552 if (!a != !b)
1553 return false;
1554
1555 if (!a)
1556 return true;
1557
1558 if (a->length != b->length)
1559 return false;
1560
1561 if (memcmp(a->data, b->data, a->length) != 0)
1562 return false;
1563
1564 return dns_txt_item_equal(a->items_next, b->items_next);
1565}
8730bccf
LP
1566
1567static const char* const dnssec_algorithm_table[_DNSSEC_ALGORITHM_MAX_DEFINED] = {
6f717d08 1568 /* Mnemonics as listed on https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
8730bccf
LP
1569 [DNSSEC_ALGORITHM_RSAMD5] = "RSAMD5",
1570 [DNSSEC_ALGORITHM_DH] = "DH",
1571 [DNSSEC_ALGORITHM_DSA] = "DSA",
1572 [DNSSEC_ALGORITHM_ECC] = "ECC",
1573 [DNSSEC_ALGORITHM_RSASHA1] = "RSASHA1",
1574 [DNSSEC_ALGORITHM_DSA_NSEC3_SHA1] = "DSA-NSEC3-SHA1",
1575 [DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1] = "RSASHA1-NSEC3-SHA1",
1576 [DNSSEC_ALGORITHM_RSASHA256] = "RSASHA256",
1577 [DNSSEC_ALGORITHM_RSASHA512] = "RSASHA512",
6f717d08
LP
1578 [DNSSEC_ALGORITHM_ECC_GOST] = "ECC-GOST",
1579 [DNSSEC_ALGORITHM_ECDSAP256SHA256] = "ECDSAP256SHA256",
1580 [DNSSEC_ALGORITHM_ECDSAP384SHA384] = "ECDSAP384SHA384",
8730bccf
LP
1581 [DNSSEC_ALGORITHM_INDIRECT] = "INDIRECT",
1582 [DNSSEC_ALGORITHM_PRIVATEDNS] = "PRIVATEDNS",
1583 [DNSSEC_ALGORITHM_PRIVATEOID] = "PRIVATEOID",
1584};
8e54f5d9 1585DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_algorithm, int, 255);
8730bccf
LP
1586
1587static const char* const dnssec_digest_table[_DNSSEC_DIGEST_MAX_DEFINED] = {
6f717d08
LP
1588 /* Names as listed on https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
1589 [DNSSEC_DIGEST_SHA1] = "SHA-1",
1590 [DNSSEC_DIGEST_SHA256] = "SHA-256",
1591 [DNSSEC_DIGEST_GOST_R_34_11_94] = "GOST_R_34.11-94",
1592 [DNSSEC_DIGEST_SHA384] = "SHA-384",
8730bccf 1593};
8e54f5d9 1594DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_digest, int, 255);