]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-rr.h
resolved: inroduce dns_txt_item_new_empty() function
[thirdparty/systemd.git] / src / resolve / resolved-dns-rr.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2014 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <netinet/in.h>
24
25 #include "bitmap.h"
26 #include "dns-type.h"
27 #include "hashmap.h"
28 #include "in-addr-util.h"
29 #include "list.h"
30 #include "string-util.h"
31
32 typedef struct DnsResourceKey DnsResourceKey;
33 typedef struct DnsResourceRecord DnsResourceRecord;
34 typedef struct DnsTxtItem DnsTxtItem;
35
36 /* DNSKEY RR flags */
37 #define DNSKEY_FLAG_SEP (UINT16_C(1) << 0)
38 #define DNSKEY_FLAG_REVOKE (UINT16_C(1) << 7)
39 #define DNSKEY_FLAG_ZONE_KEY (UINT16_C(1) << 8)
40
41 /* mDNS RR flags */
42 #define MDNS_RR_CACHE_FLUSH (UINT16_C(1) << 15)
43
44 /* DNSSEC algorithm identifiers, see
45 * http://tools.ietf.org/html/rfc4034#appendix-A.1 and
46 * https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
47 enum {
48 DNSSEC_ALGORITHM_RSAMD5 = 1,
49 DNSSEC_ALGORITHM_DH,
50 DNSSEC_ALGORITHM_DSA,
51 DNSSEC_ALGORITHM_ECC,
52 DNSSEC_ALGORITHM_RSASHA1,
53 DNSSEC_ALGORITHM_DSA_NSEC3_SHA1,
54 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1,
55 DNSSEC_ALGORITHM_RSASHA256 = 8, /* RFC 5702 */
56 DNSSEC_ALGORITHM_RSASHA512 = 10, /* RFC 5702 */
57 DNSSEC_ALGORITHM_ECC_GOST = 12, /* RFC 5933 */
58 DNSSEC_ALGORITHM_ECDSAP256SHA256 = 13, /* RFC 6605 */
59 DNSSEC_ALGORITHM_ECDSAP384SHA384 = 14, /* RFC 6605 */
60 DNSSEC_ALGORITHM_INDIRECT = 252,
61 DNSSEC_ALGORITHM_PRIVATEDNS,
62 DNSSEC_ALGORITHM_PRIVATEOID,
63 _DNSSEC_ALGORITHM_MAX_DEFINED
64 };
65
66 /* DNSSEC digest identifiers, see
67 * https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
68 enum {
69 DNSSEC_DIGEST_SHA1 = 1,
70 DNSSEC_DIGEST_SHA256 = 2, /* RFC 4509 */
71 DNSSEC_DIGEST_GOST_R_34_11_94 = 3, /* RFC 5933 */
72 DNSSEC_DIGEST_SHA384 = 4, /* RFC 6605 */
73 _DNSSEC_DIGEST_MAX_DEFINED
74 };
75
76 /* DNSSEC NSEC3 hash algorithms, see
77 * https://www.iana.org/assignments/dnssec-nsec3-parameters/dnssec-nsec3-parameters.xhtml */
78 enum {
79 NSEC3_ALGORITHM_SHA1 = 1,
80 _NSEC3_ALGORITHM_MAX_DEFINED
81 };
82
83 struct DnsResourceKey {
84 unsigned n_ref; /* (unsigned -1) for const keys, see below */
85 uint16_t class, type;
86 char *_name; /* don't access directly, use dns_resource_key_name()! */
87 };
88
89 /* Creates a temporary resource key. This is only useful to quickly
90 * look up something, without allocating a full DnsResourceKey object
91 * for it. Note that it is not OK to take references to this kind of
92 * resource key object. */
93 #define DNS_RESOURCE_KEY_CONST(c, t, n) \
94 ((DnsResourceKey) { \
95 .n_ref = (unsigned) -1, \
96 .class = c, \
97 .type = t, \
98 ._name = (char*) n, \
99 })
100
101
102 struct DnsTxtItem {
103 size_t length;
104 LIST_FIELDS(DnsTxtItem, items);
105 uint8_t data[];
106 };
107
108 struct DnsResourceRecord {
109 unsigned n_ref;
110 DnsResourceKey *key;
111
112 char *to_string;
113
114 uint32_t ttl;
115 usec_t expiry; /* RRSIG signature expiry */
116
117 /* How many labels to strip to determine "signer" of the RRSIG (aka, the zone). -1 if not signed. */
118 unsigned n_skip_labels_signer;
119 /* How many labels to strip to determine "synthesizing source" of this RR, i.e. the wildcard's immediate parent. -1 if not signed. */
120 unsigned n_skip_labels_source;
121
122 bool unparseable:1;
123
124 bool wire_format_canonical:1;
125 void *wire_format;
126 size_t wire_format_size;
127 size_t wire_format_rdata_offset;
128
129 union {
130 struct {
131 void *data;
132 size_t data_size;
133 } generic, opt;
134
135 struct {
136 uint16_t priority;
137 uint16_t weight;
138 uint16_t port;
139 char *name;
140 } srv;
141
142 struct {
143 char *name;
144 } ptr, ns, cname, dname;
145
146 struct {
147 char *cpu;
148 char *os;
149 } hinfo;
150
151 struct {
152 DnsTxtItem *items;
153 } txt, spf;
154
155 struct {
156 struct in_addr in_addr;
157 } a;
158
159 struct {
160 struct in6_addr in6_addr;
161 } aaaa;
162
163 struct {
164 char *mname;
165 char *rname;
166 uint32_t serial;
167 uint32_t refresh;
168 uint32_t retry;
169 uint32_t expire;
170 uint32_t minimum;
171 } soa;
172
173 struct {
174 uint16_t priority;
175 char *exchange;
176 } mx;
177
178 /* https://tools.ietf.org/html/rfc1876 */
179 struct {
180 uint8_t version;
181 uint8_t size;
182 uint8_t horiz_pre;
183 uint8_t vert_pre;
184 uint32_t latitude;
185 uint32_t longitude;
186 uint32_t altitude;
187 } loc;
188
189 /* https://tools.ietf.org/html/rfc4255#section-3.1 */
190 struct {
191 uint8_t algorithm;
192 uint8_t fptype;
193 void *fingerprint;
194 size_t fingerprint_size;
195 } sshfp;
196
197 /* http://tools.ietf.org/html/rfc4034#section-2.1 */
198 struct {
199 uint16_t flags;
200 uint8_t protocol;
201 uint8_t algorithm;
202 void* key;
203 size_t key_size;
204 } dnskey;
205
206 /* http://tools.ietf.org/html/rfc4034#section-3.1 */
207 struct {
208 uint16_t type_covered;
209 uint8_t algorithm;
210 uint8_t labels;
211 uint32_t original_ttl;
212 uint32_t expiration;
213 uint32_t inception;
214 uint16_t key_tag;
215 char *signer;
216 void *signature;
217 size_t signature_size;
218 } rrsig;
219
220 /* https://tools.ietf.org/html/rfc4034#section-4.1 */
221 struct {
222 char *next_domain_name;
223 Bitmap *types;
224 } nsec;
225
226 /* https://tools.ietf.org/html/rfc4034#section-5.1 */
227 struct {
228 uint16_t key_tag;
229 uint8_t algorithm;
230 uint8_t digest_type;
231 void *digest;
232 size_t digest_size;
233 } ds;
234
235 struct {
236 uint8_t algorithm;
237 uint8_t flags;
238 uint16_t iterations;
239 void *salt;
240 size_t salt_size;
241 void *next_hashed_name;
242 size_t next_hashed_name_size;
243 Bitmap *types;
244 } nsec3;
245
246 /* https://tools.ietf.org/html/draft-ietf-dane-protocol-23 */
247 struct {
248 uint8_t cert_usage;
249 uint8_t selector;
250 uint8_t matching_type;
251 void *data;
252 size_t data_size;
253 } tlsa;
254
255 /* https://tools.ietf.org/html/rfc6844 */
256 struct {
257 uint8_t flags;
258 char *tag;
259 void *value;
260 size_t value_size;
261 } caa;
262 };
263 };
264
265 static inline const void* DNS_RESOURCE_RECORD_RDATA(DnsResourceRecord *rr) {
266 if (!rr)
267 return NULL;
268
269 if (!rr->wire_format)
270 return NULL;
271
272 assert(rr->wire_format_rdata_offset <= rr->wire_format_size);
273 return (uint8_t*) rr->wire_format + rr->wire_format_rdata_offset;
274 }
275
276 static inline size_t DNS_RESOURCE_RECORD_RDATA_SIZE(DnsResourceRecord *rr) {
277 if (!rr)
278 return 0;
279 if (!rr->wire_format)
280 return 0;
281
282 assert(rr->wire_format_rdata_offset <= rr->wire_format_size);
283 return rr->wire_format_size - rr->wire_format_rdata_offset;
284 }
285
286 static inline uint8_t DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(DnsResourceRecord *rr) {
287 assert(rr);
288 assert(rr->key->type == DNS_TYPE_OPT);
289
290 return ((rr->ttl >> 16) & 0xFF) == 0;
291 }
292
293 DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name);
294 DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname);
295 int dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name);
296 DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name);
297 DnsResourceKey* dns_resource_key_ref(DnsResourceKey *key);
298 DnsResourceKey* dns_resource_key_unref(DnsResourceKey *key);
299 const char* dns_resource_key_name(const DnsResourceKey *key);
300 bool dns_resource_key_is_address(const DnsResourceKey *key);
301 int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b);
302 int dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain);
303 int dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain);
304 int dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa);
305
306 /* _DNS_{CLASS,TYPE}_STRING_MAX include one byte for NUL, which we use for space instead below.
307 * DNS_HOSTNAME_MAX does not include the NUL byte, so we need to add 1. */
308 #define DNS_RESOURCE_KEY_STRING_MAX (_DNS_CLASS_STRING_MAX + _DNS_TYPE_STRING_MAX + DNS_HOSTNAME_MAX + 1)
309
310 char* dns_resource_key_to_string(const DnsResourceKey *key, char *buf, size_t buf_size);
311 ssize_t dns_resource_record_payload(DnsResourceRecord *rr, void **out);
312
313 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref);
314
315 static inline bool dns_key_is_shared(const DnsResourceKey *key) {
316 return IN_SET(key->type, DNS_TYPE_PTR);
317 }
318
319 bool dns_resource_key_reduce(DnsResourceKey **a, DnsResourceKey **b);
320
321 DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key);
322 DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name);
323 DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr);
324 DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr);
325 int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name);
326 int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name);
327 int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b);
328 const char* dns_resource_record_to_string(DnsResourceRecord *rr);
329 DnsResourceRecord *dns_resource_record_copy(DnsResourceRecord *rr);
330 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceRecord*, dns_resource_record_unref);
331
332 int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical);
333
334 int dns_resource_record_signer(DnsResourceRecord *rr, const char **ret);
335 int dns_resource_record_source(DnsResourceRecord *rr, const char **ret);
336 int dns_resource_record_is_signer(DnsResourceRecord *rr, const char *zone);
337 int dns_resource_record_is_synthetic(DnsResourceRecord *rr);
338
339 int dns_resource_record_clamp_ttl(DnsResourceRecord **rr, uint32_t max_ttl);
340
341 DnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i);
342 bool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b);
343 DnsTxtItem *dns_txt_item_copy(DnsTxtItem *i);
344 int dns_txt_item_new_empty(DnsTxtItem **ret);
345
346 void dns_resource_record_hash_func(const void *i, struct siphash *state);
347
348 extern const struct hash_ops dns_resource_key_hash_ops;
349 extern const struct hash_ops dns_resource_record_hash_ops;
350
351 int dnssec_algorithm_to_string_alloc(int i, char **ret);
352 int dnssec_algorithm_from_string(const char *s) _pure_;
353
354 int dnssec_digest_to_string_alloc(int i, char **ret);
355 int dnssec_digest_from_string(const char *s) _pure_;