| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/socket.h> |
| 4 |
|
| 5 |
#include "dns-type.h" |
| 6 |
#include "parse-util.h" |
| 7 |
#include "string-util.h" |
| 8 |
|
| 9 |
typedef const struct { |
| 10 |
uint16_t type; |
| 11 |
const char *name; |
| 12 |
} dns_type; |
| 13 |
|
| 14 |
static const struct dns_type_name * |
| 15 |
lookup_dns_type (register const char *str, register GPERF_LEN_TYPE len); |
| 16 |
|
| 17 |
#include "dns_type-from-name.inc" |
| 18 |
#include "dns_type-to-name.inc" |
| 19 |
|
| 20 |
int dns_type_from_string(const char *s) { |
| 21 |
const struct dns_type_name *sc; |
| 22 |
|
| 23 |
assert(s); |
| 24 |
|
| 25 |
sc = lookup_dns_type(s, strlen(s)); |
| 26 |
if (sc) |
| 27 |
return sc->id; |
| 28 |
|
| 29 |
s = startswith_no_case(s, "TYPE"); |
| 30 |
if (s) { |
| 31 |
unsigned x; |
| 32 |
|
| 33 |
if (safe_atou(s, &x) >= 0 && |
| 34 |
x <= UINT16_MAX) |
| 35 |
return (int) x; |
| 36 |
} |
| 37 |
|
| 38 |
return _DNS_TYPE_INVALID; |
| 39 |
} |
| 40 |
|
| 41 |
bool dns_type_is_pseudo(uint16_t type) { |
| 42 |
|
| 43 |
/* Checks whether the specified type is a "pseudo-type". What |
| 44 |
* a "pseudo-type" precisely is, is defined only very weakly, |
| 45 |
* but apparently entails all RR types that are not actually |
| 46 |
* stored as RRs on the server and should hence also not be |
| 47 |
* cached. We use this list primarily to validate NSEC type |
| 48 |
* bitfields, and to verify what to cache. */ |
| 49 |
|
| 50 |
return IN_SET(type, |
| 51 |
0, /* A Pseudo RR type, according to RFC 2931 */ |
| 52 |
DNS_TYPE_ANY, |
| 53 |
DNS_TYPE_AXFR, |
| 54 |
DNS_TYPE_IXFR, |
| 55 |
DNS_TYPE_OPT, |
| 56 |
DNS_TYPE_TSIG, |
| 57 |
DNS_TYPE_TKEY |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
bool dns_class_is_pseudo(uint16_t class) { |
| 62 |
return class == DNS_CLASS_ANY; |
| 63 |
} |
| 64 |
|
| 65 |
bool dns_type_is_valid_query(uint16_t type) { |
| 66 |
|
| 67 |
/* The types valid as questions in packets */ |
| 68 |
|
| 69 |
return !IN_SET(type, |
| 70 |
0, |
| 71 |
DNS_TYPE_OPT, |
| 72 |
DNS_TYPE_TSIG, |
| 73 |
DNS_TYPE_TKEY, |
| 74 |
|
| 75 |
/* RRSIG are technically valid as questions, but we refuse doing explicit queries for them, as |
| 76 |
* they aren't really payload, but signatures for payload, and cannot be validated on their |
| 77 |
* own. After all they are the signatures, and have no signatures of their own validating |
| 78 |
* them. */ |
| 79 |
DNS_TYPE_RRSIG); |
| 80 |
} |
| 81 |
|
| 82 |
bool dns_type_is_zone_transfer(uint16_t type) { |
| 83 |
|
| 84 |
/* Zone transfers, either normal or incremental */ |
| 85 |
|
| 86 |
return IN_SET(type, |
| 87 |
DNS_TYPE_AXFR, |
| 88 |
DNS_TYPE_IXFR); |
| 89 |
} |
| 90 |
|
| 91 |
bool dns_type_is_valid_rr(uint16_t type) { |
| 92 |
|
| 93 |
/* The types valid as RR in packets (but not necessarily |
| 94 |
* stored on servers). */ |
| 95 |
|
| 96 |
return !IN_SET(type, |
| 97 |
DNS_TYPE_ANY, |
| 98 |
DNS_TYPE_AXFR, |
| 99 |
DNS_TYPE_IXFR); |
| 100 |
} |
| 101 |
|
| 102 |
bool dns_class_is_valid_rr(uint16_t class) { |
| 103 |
return class != DNS_CLASS_ANY; |
| 104 |
} |
| 105 |
|
| 106 |
bool dns_type_may_redirect(uint16_t type) { |
| 107 |
/* The following record types should never be redirected using |
| 108 |
* CNAME/DNAME RRs. See |
| 109 |
* <https://tools.ietf.org/html/rfc4035#section-2.5>. */ |
| 110 |
|
| 111 |
if (dns_type_is_pseudo(type)) |
| 112 |
return false; |
| 113 |
|
| 114 |
return !IN_SET(type, |
| 115 |
DNS_TYPE_CNAME, |
| 116 |
DNS_TYPE_DNAME, |
| 117 |
DNS_TYPE_NSEC3, |
| 118 |
DNS_TYPE_NSEC, |
| 119 |
DNS_TYPE_RRSIG, |
| 120 |
DNS_TYPE_NXT, |
| 121 |
DNS_TYPE_SIG, |
| 122 |
DNS_TYPE_KEY); |
| 123 |
} |
| 124 |
|
| 125 |
bool dns_type_may_wildcard(uint16_t type) { |
| 126 |
|
| 127 |
/* The following records may not be expanded from wildcard RRsets */ |
| 128 |
|
| 129 |
if (dns_type_is_pseudo(type)) |
| 130 |
return false; |
| 131 |
|
| 132 |
return !IN_SET(type, |
| 133 |
DNS_TYPE_NSEC3, |
| 134 |
DNS_TYPE_SOA, |
| 135 |
|
| 136 |
/* Prohibited by https://tools.ietf.org/html/rfc4592#section-4.4 */ |
| 137 |
DNS_TYPE_DNAME); |
| 138 |
} |
| 139 |
|
| 140 |
bool dns_type_apex_only(uint16_t type) { |
| 141 |
|
| 142 |
/* Returns true for all RR types that may only appear signed in a zone apex */ |
| 143 |
|
| 144 |
return IN_SET(type, |
| 145 |
DNS_TYPE_SOA, |
| 146 |
DNS_TYPE_NS, /* this one can appear elsewhere, too, but not signed */ |
| 147 |
DNS_TYPE_DNSKEY, |
| 148 |
DNS_TYPE_NSEC3PARAM); |
| 149 |
} |
| 150 |
|
| 151 |
bool dns_type_is_dnssec(uint16_t type) { |
| 152 |
return IN_SET(type, |
| 153 |
DNS_TYPE_DS, |
| 154 |
DNS_TYPE_DNSKEY, |
| 155 |
DNS_TYPE_RRSIG, |
| 156 |
DNS_TYPE_NSEC, |
| 157 |
DNS_TYPE_NSEC3, |
| 158 |
DNS_TYPE_NSEC3PARAM); |
| 159 |
} |
| 160 |
|
| 161 |
bool dns_type_is_obsolete(uint16_t type) { |
| 162 |
return IN_SET(type, |
| 163 |
/* Obsoleted by RFC 973 */ |
| 164 |
DNS_TYPE_MD, |
| 165 |
DNS_TYPE_MF, |
| 166 |
DNS_TYPE_MAILA, |
| 167 |
|
| 168 |
/* Kinda obsoleted by RFC 2505 */ |
| 169 |
DNS_TYPE_MB, |
| 170 |
DNS_TYPE_MG, |
| 171 |
DNS_TYPE_MR, |
| 172 |
DNS_TYPE_MINFO, |
| 173 |
DNS_TYPE_MAILB, |
| 174 |
|
| 175 |
/* RFC1127 kinda obsoleted this by recommending against its use */ |
| 176 |
DNS_TYPE_WKS, |
| 177 |
|
| 178 |
/* Declared historical by RFC 6563 */ |
| 179 |
DNS_TYPE_A6, |
| 180 |
|
| 181 |
/* Obsoleted by DNSSEC-bis */ |
| 182 |
DNS_TYPE_NXT, |
| 183 |
|
| 184 |
/* RFC 1035 removed support for concepts that needed this from RFC 883 */ |
| 185 |
DNS_TYPE_NULL); |
| 186 |
} |
| 187 |
|
| 188 |
bool dns_type_needs_authentication(uint16_t type) { |
| 189 |
|
| 190 |
/* Returns true for all (non-obsolete) RR types where records are not useful if they aren't |
| 191 |
* authenticated. I.e. everything that contains crypto keys. */ |
| 192 |
|
| 193 |
return IN_SET(type, |
| 194 |
DNS_TYPE_CERT, |
| 195 |
DNS_TYPE_SSHFP, |
| 196 |
DNS_TYPE_IPSECKEY, |
| 197 |
DNS_TYPE_DS, |
| 198 |
DNS_TYPE_DNSKEY, |
| 199 |
DNS_TYPE_TLSA, |
| 200 |
DNS_TYPE_CDNSKEY, |
| 201 |
DNS_TYPE_OPENPGPKEY, |
| 202 |
DNS_TYPE_CAA); |
| 203 |
} |
| 204 |
|
| 205 |
int dns_type_to_af(uint16_t t) { |
| 206 |
switch (t) { |
| 207 |
|
| 208 |
case DNS_TYPE_A: |
| 209 |
return AF_INET; |
| 210 |
|
| 211 |
case DNS_TYPE_AAAA: |
| 212 |
return AF_INET6; |
| 213 |
|
| 214 |
case DNS_TYPE_ANY: |
| 215 |
return AF_UNSPEC; |
| 216 |
|
| 217 |
default: |
| 218 |
return -EINVAL; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
const char* dns_class_to_string(uint16_t class) { |
| 223 |
|
| 224 |
switch (class) { |
| 225 |
|
| 226 |
case DNS_CLASS_IN: |
| 227 |
return "IN"; |
| 228 |
|
| 229 |
case DNS_CLASS_ANY: |
| 230 |
return "ANY"; |
| 231 |
} |
| 232 |
|
| 233 |
return NULL; |
| 234 |
} |
| 235 |
|
| 236 |
int dns_class_from_string(const char *s) { |
| 237 |
|
| 238 |
if (!s) |
| 239 |
return _DNS_CLASS_INVALID; |
| 240 |
|
| 241 |
if (strcaseeq(s, "IN")) |
| 242 |
return DNS_CLASS_IN; |
| 243 |
else if (strcaseeq(s, "ANY")) |
| 244 |
return DNS_CLASS_ANY; |
| 245 |
|
| 246 |
return _DNS_CLASS_INVALID; |
| 247 |
} |
| 248 |
|
| 249 |
const char* tlsa_cert_usage_to_string(uint8_t cert_usage) { |
| 250 |
|
| 251 |
switch (cert_usage) { |
| 252 |
|
| 253 |
case 0: |
| 254 |
return "CA constraint"; |
| 255 |
|
| 256 |
case 1: |
| 257 |
return "Service certificate constraint"; |
| 258 |
|
| 259 |
case 2: |
| 260 |
return "Trust anchor assertion"; |
| 261 |
|
| 262 |
case 3: |
| 263 |
return "Domain-issued certificate"; |
| 264 |
|
| 265 |
case 4 ... 254: |
| 266 |
return "Unassigned"; |
| 267 |
|
| 268 |
case 255: |
| 269 |
return "Private use"; |
| 270 |
} |
| 271 |
|
| 272 |
return NULL; /* clang cannot count that we covered everything */ |
| 273 |
} |
| 274 |
|
| 275 |
const char* tlsa_selector_to_string(uint8_t selector) { |
| 276 |
switch (selector) { |
| 277 |
|
| 278 |
case 0: |
| 279 |
return "Full Certificate"; |
| 280 |
|
| 281 |
case 1: |
| 282 |
return "SubjectPublicKeyInfo"; |
| 283 |
|
| 284 |
case 2 ... 254: |
| 285 |
return "Unassigned"; |
| 286 |
|
| 287 |
case 255: |
| 288 |
return "Private use"; |
| 289 |
} |
| 290 |
|
| 291 |
return NULL; |
| 292 |
} |
| 293 |
|
| 294 |
const char* tlsa_matching_type_to_string(uint8_t selector) { |
| 295 |
|
| 296 |
switch (selector) { |
| 297 |
|
| 298 |
case 0: |
| 299 |
return "No hash used"; |
| 300 |
|
| 301 |
case 1: |
| 302 |
return "SHA-256"; |
| 303 |
|
| 304 |
case 2: |
| 305 |
return "SHA-512"; |
| 306 |
|
| 307 |
case 3 ... 254: |
| 308 |
return "Unassigned"; |
| 309 |
|
| 310 |
case 255: |
| 311 |
return "Private use"; |
| 312 |
} |
| 313 |
|
| 314 |
return NULL; |
| 315 |
} |