| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <endian.h> |
| 4 |
#include <netinet/in.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <sys/socket.h> |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "dns-def.h" |
| 10 |
#include "dns-domain.h" |
| 11 |
#include "glyph-util.h" |
| 12 |
#include "hash-funcs.h" |
| 13 |
#include "hexdecoct.h" |
| 14 |
#include "hostname-util.h" |
| 15 |
#include "idn-util.h" |
| 16 |
#include "in-addr-util.h" |
| 17 |
#include "log.h" |
| 18 |
#include "parse-util.h" |
| 19 |
#include "siphash24.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "strv.h" |
| 22 |
#include "utf8.h" |
| 23 |
|
| 24 |
int dns_label_unescape(const char **name, char *dest, size_t sz, DNSLabelFlags flags) { |
| 25 |
const char *n; |
| 26 |
char *d, last_char = 0; |
| 27 |
int r = 0; |
| 28 |
|
| 29 |
assert(name); |
| 30 |
|
| 31 |
if (isempty(*name)) { |
| 32 |
if (dest && sz >= 1) |
| 33 |
dest[0] = 0; |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
n = *name; |
| 38 |
d = dest; |
| 39 |
|
| 40 |
for (;;) { |
| 41 |
if (IN_SET(*n, 0, '.')) { |
| 42 |
if (FLAGS_SET(flags, DNS_LABEL_LDH) && last_char == '-') |
| 43 |
/* Trailing dash */ |
| 44 |
return -EINVAL; |
| 45 |
|
| 46 |
if (n[0] == '.' && (n[1] != 0 || !FLAGS_SET(flags, DNS_LABEL_LEAVE_TRAILING_DOT))) |
| 47 |
n++; |
| 48 |
|
| 49 |
break; |
| 50 |
} |
| 51 |
|
| 52 |
if (r >= DNS_LABEL_MAX) |
| 53 |
return -EINVAL; |
| 54 |
|
| 55 |
if (sz <= 0) |
| 56 |
return -ENOBUFS; |
| 57 |
|
| 58 |
if (*n == '\\') { |
| 59 |
/* Escaped character */ |
| 60 |
if (FLAGS_SET(flags, DNS_LABEL_NO_ESCAPES)) |
| 61 |
return -EINVAL; |
| 62 |
|
| 63 |
n++; |
| 64 |
|
| 65 |
if (*n == 0) |
| 66 |
/* Ending NUL */ |
| 67 |
return -EINVAL; |
| 68 |
|
| 69 |
else if (IN_SET(*n, '\\', '.')) { |
| 70 |
/* Escaped backslash or dot */ |
| 71 |
|
| 72 |
if (FLAGS_SET(flags, DNS_LABEL_LDH)) |
| 73 |
return -EINVAL; |
| 74 |
|
| 75 |
last_char = *n; |
| 76 |
if (d) |
| 77 |
*(d++) = *n; |
| 78 |
sz--; |
| 79 |
r++; |
| 80 |
n++; |
| 81 |
|
| 82 |
} else if (n[0] >= '0' && n[0] <= '9') { |
| 83 |
unsigned k; |
| 84 |
|
| 85 |
/* Escaped literal ASCII character */ |
| 86 |
|
| 87 |
if (!(n[1] >= '0' && n[1] <= '9') || |
| 88 |
!(n[2] >= '0' && n[2] <= '9')) |
| 89 |
return -EINVAL; |
| 90 |
|
| 91 |
k = ((unsigned) (n[0] - '0') * 100) + |
| 92 |
((unsigned) (n[1] - '0') * 10) + |
| 93 |
((unsigned) (n[2] - '0')); |
| 94 |
|
| 95 |
/* Don't allow anything that doesn't fit in 8 bits. Note that we do allow |
| 96 |
* control characters, as some servers (e.g. cloudflare) are happy to |
| 97 |
* generate labels with them inside. */ |
| 98 |
if (k > 255) |
| 99 |
return -EINVAL; |
| 100 |
|
| 101 |
if (FLAGS_SET(flags, DNS_LABEL_LDH) && |
| 102 |
!valid_ldh_char((char) k)) |
| 103 |
return -EINVAL; |
| 104 |
|
| 105 |
last_char = (char) k; |
| 106 |
if (d) |
| 107 |
*(d++) = (char) k; |
| 108 |
sz--; |
| 109 |
r++; |
| 110 |
|
| 111 |
n += 3; |
| 112 |
} else |
| 113 |
return -EINVAL; |
| 114 |
|
| 115 |
} else if ((uint8_t) *n >= (uint8_t) ' ' && *n != 127) { |
| 116 |
|
| 117 |
/* Normal character */ |
| 118 |
|
| 119 |
if (FLAGS_SET(flags, DNS_LABEL_LDH)) { |
| 120 |
if (!valid_ldh_char(*n)) |
| 121 |
return -EINVAL; |
| 122 |
if (r == 0 && *n == '-') |
| 123 |
/* Leading dash */ |
| 124 |
return -EINVAL; |
| 125 |
} |
| 126 |
|
| 127 |
last_char = *n; |
| 128 |
if (d) |
| 129 |
*(d++) = *n; |
| 130 |
sz--; |
| 131 |
r++; |
| 132 |
n++; |
| 133 |
} else |
| 134 |
return -EINVAL; |
| 135 |
} |
| 136 |
|
| 137 |
/* Empty label that is not at the end? */ |
| 138 |
if (r == 0 && *n) |
| 139 |
return -EINVAL; |
| 140 |
|
| 141 |
/* More than one trailing dot? */ |
| 142 |
if (n[0] == '.' && !FLAGS_SET(flags, DNS_LABEL_LEAVE_TRAILING_DOT)) |
| 143 |
return -EINVAL; |
| 144 |
|
| 145 |
if (sz >= 1 && d) |
| 146 |
*d = 0; |
| 147 |
|
| 148 |
*name = n; |
| 149 |
return r; |
| 150 |
} |
| 151 |
|
| 152 |
/* @label_terminal: terminal character of a label, updated to point to the terminal character of |
| 153 |
* the previous label (always skipping one dot) or to NULL if there are no more |
| 154 |
* labels. */ |
| 155 |
int dns_label_unescape_suffix(const char *name, const char **label_terminal, char *dest, size_t sz) { |
| 156 |
const char *terminal; |
| 157 |
int r; |
| 158 |
|
| 159 |
assert(name); |
| 160 |
assert(label_terminal); |
| 161 |
assert(dest); |
| 162 |
|
| 163 |
/* no more labels */ |
| 164 |
if (!*label_terminal) { |
| 165 |
if (sz >= 1) |
| 166 |
*dest = 0; |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
terminal = *label_terminal; |
| 172 |
assert(IN_SET(*terminal, 0, '.')); |
| 173 |
|
| 174 |
/* Skip current terminal character (and accept domain names ending it ".") */ |
| 175 |
if (*terminal == 0) |
| 176 |
terminal = PTR_SUB1(terminal, name); |
| 177 |
if (terminal >= name && *terminal == '.') |
| 178 |
terminal = PTR_SUB1(terminal, name); |
| 179 |
|
| 180 |
/* Point name to the last label, and terminal to the preceding terminal symbol (or make it a NULL pointer) */ |
| 181 |
while (terminal) { |
| 182 |
/* Find the start of the last label */ |
| 183 |
if (*terminal == '.') { |
| 184 |
const char *y; |
| 185 |
unsigned slashes = 0; |
| 186 |
|
| 187 |
for (y = PTR_SUB1(terminal, name); y && *y == '\\'; y = PTR_SUB1(y, name)) |
| 188 |
slashes++; |
| 189 |
|
| 190 |
if (slashes % 2 == 0) { |
| 191 |
/* The '.' was not escaped */ |
| 192 |
name = terminal + 1; |
| 193 |
break; |
| 194 |
} else { |
| 195 |
terminal = y; |
| 196 |
continue; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
terminal = PTR_SUB1(terminal, name); |
| 201 |
} |
| 202 |
|
| 203 |
r = dns_label_unescape(&name, dest, sz, 0); |
| 204 |
if (r < 0) |
| 205 |
return r; |
| 206 |
|
| 207 |
*label_terminal = terminal; |
| 208 |
|
| 209 |
return r; |
| 210 |
} |
| 211 |
|
| 212 |
int dns_label_escape(const char *p, size_t l, char *dest, size_t sz) { |
| 213 |
char *q; |
| 214 |
|
| 215 |
/* DNS labels must be between 1 and 63 characters long. A |
| 216 |
* zero-length label does not exist. See RFC 2181, Section |
| 217 |
* 11. */ |
| 218 |
|
| 219 |
if (l <= 0 || l > DNS_LABEL_MAX) |
| 220 |
return -EINVAL; |
| 221 |
if (sz < 1) |
| 222 |
return -ENOBUFS; |
| 223 |
|
| 224 |
assert(p); |
| 225 |
assert(dest); |
| 226 |
|
| 227 |
q = dest; |
| 228 |
while (l > 0) { |
| 229 |
|
| 230 |
if (IN_SET(*p, '.', '\\')) { |
| 231 |
|
| 232 |
/* Dot or backslash */ |
| 233 |
|
| 234 |
if (sz < 3) |
| 235 |
return -ENOBUFS; |
| 236 |
|
| 237 |
*(q++) = '\\'; |
| 238 |
*(q++) = *p; |
| 239 |
|
| 240 |
sz -= 2; |
| 241 |
|
| 242 |
} else if (IN_SET(*p, '_', '-') || |
| 243 |
ascii_isdigit(*p) || |
| 244 |
ascii_isalpha(*p)) { |
| 245 |
|
| 246 |
/* Proper character */ |
| 247 |
|
| 248 |
if (sz < 2) |
| 249 |
return -ENOBUFS; |
| 250 |
|
| 251 |
*(q++) = *p; |
| 252 |
sz -= 1; |
| 253 |
|
| 254 |
} else { |
| 255 |
|
| 256 |
/* Everything else */ |
| 257 |
|
| 258 |
if (sz < 5) |
| 259 |
return -ENOBUFS; |
| 260 |
|
| 261 |
*(q++) = '\\'; |
| 262 |
*(q++) = '0' + (char) ((uint8_t) *p / 100); |
| 263 |
*(q++) = '0' + (char) (((uint8_t) *p / 10) % 10); |
| 264 |
*(q++) = '0' + (char) ((uint8_t) *p % 10); |
| 265 |
|
| 266 |
sz -= 4; |
| 267 |
} |
| 268 |
|
| 269 |
p++; |
| 270 |
l--; |
| 271 |
} |
| 272 |
|
| 273 |
*q = 0; |
| 274 |
return (int) (q - dest); |
| 275 |
} |
| 276 |
|
| 277 |
int dns_label_escape_new(const char *p, size_t l, char **ret) { |
| 278 |
_cleanup_free_ char *s = NULL; |
| 279 |
int r; |
| 280 |
|
| 281 |
assert(p); |
| 282 |
assert(ret); |
| 283 |
|
| 284 |
if (l <= 0 || l > DNS_LABEL_MAX) |
| 285 |
return -EINVAL; |
| 286 |
|
| 287 |
s = new(char, DNS_LABEL_ESCAPED_MAX); |
| 288 |
if (!s) |
| 289 |
return -ENOMEM; |
| 290 |
|
| 291 |
r = dns_label_escape(p, l, s, DNS_LABEL_ESCAPED_MAX); |
| 292 |
if (r < 0) |
| 293 |
return r; |
| 294 |
|
| 295 |
*ret = TAKE_PTR(s); |
| 296 |
|
| 297 |
return r; |
| 298 |
} |
| 299 |
|
| 300 |
int dns_name_parent(const char **name) { |
| 301 |
return dns_label_unescape(name, NULL, DNS_LABEL_MAX, 0); |
| 302 |
} |
| 303 |
|
| 304 |
int dns_name_concat(const char *a, const char *b, DNSLabelFlags flags, char **ret) { |
| 305 |
_cleanup_free_ char *result = NULL; |
| 306 |
size_t n_result = 0, n_unescaped = 0; |
| 307 |
const char *p; |
| 308 |
bool first = true; |
| 309 |
int r; |
| 310 |
|
| 311 |
if (a) |
| 312 |
p = a; |
| 313 |
else if (b) |
| 314 |
p = TAKE_PTR(b); |
| 315 |
else |
| 316 |
goto finish; |
| 317 |
|
| 318 |
for (;;) { |
| 319 |
char label[DNS_LABEL_MAX+1]; |
| 320 |
|
| 321 |
r = dns_label_unescape(&p, label, sizeof label, flags); |
| 322 |
if (r < 0) |
| 323 |
return r; |
| 324 |
if (r == 0) { |
| 325 |
if (*p != 0) |
| 326 |
return -EINVAL; |
| 327 |
|
| 328 |
if (b) { |
| 329 |
/* Now continue with the second string, if there is one */ |
| 330 |
p = TAKE_PTR(b); |
| 331 |
continue; |
| 332 |
} |
| 333 |
|
| 334 |
break; |
| 335 |
} |
| 336 |
n_unescaped += r + !first; /* Count unescaped length to make max length determination below */ |
| 337 |
|
| 338 |
if (ret) { |
| 339 |
if (!GREEDY_REALLOC(result, n_result + !first + DNS_LABEL_ESCAPED_MAX)) |
| 340 |
return -ENOMEM; |
| 341 |
|
| 342 |
r = dns_label_escape(label, r, result + n_result + !first, DNS_LABEL_ESCAPED_MAX); |
| 343 |
if (r < 0) |
| 344 |
return r; |
| 345 |
|
| 346 |
if (!first) |
| 347 |
result[n_result] = '.'; |
| 348 |
} else { |
| 349 |
char escaped[DNS_LABEL_ESCAPED_MAX]; |
| 350 |
|
| 351 |
r = dns_label_escape(label, r, escaped, sizeof(escaped)); |
| 352 |
if (r < 0) |
| 353 |
return r; |
| 354 |
} |
| 355 |
|
| 356 |
n_result += r + !first; |
| 357 |
first = false; |
| 358 |
} |
| 359 |
|
| 360 |
finish: |
| 361 |
if (n_unescaped == 0) { |
| 362 |
/* Nothing appended? If so, generate at least a single dot, to indicate the DNS root domain */ |
| 363 |
|
| 364 |
if (ret) { |
| 365 |
if (!GREEDY_REALLOC(result, 2)) /* Room for dot, and already pre-allocate space for the trailing NUL byte at the same time */ |
| 366 |
return -ENOMEM; |
| 367 |
|
| 368 |
result[n_result++] = '.'; |
| 369 |
} |
| 370 |
|
| 371 |
n_unescaped++; |
| 372 |
} |
| 373 |
|
| 374 |
if (n_unescaped > DNS_HOSTNAME_MAX) /* Enforce max length check on unescaped length */ |
| 375 |
return -EINVAL; |
| 376 |
|
| 377 |
if (ret) { |
| 378 |
/* Suffix with a NUL byte */ |
| 379 |
if (!GREEDY_REALLOC(result, n_result + 1)) |
| 380 |
return -ENOMEM; |
| 381 |
|
| 382 |
result[n_result] = 0; |
| 383 |
*ret = TAKE_PTR(result); |
| 384 |
} |
| 385 |
|
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
void dns_name_hash_func(const char *name, struct siphash *state) { |
| 390 |
int r; |
| 391 |
|
| 392 |
assert(name); |
| 393 |
|
| 394 |
for (const char *p = name;;) { |
| 395 |
char label[DNS_LABEL_MAX+1]; |
| 396 |
|
| 397 |
r = dns_label_unescape(&p, label, sizeof label, 0); |
| 398 |
if (r < 0) |
| 399 |
return string_hash_func(p, state); /* fallback for invalid DNS names */ |
| 400 |
if (r == 0) |
| 401 |
break; |
| 402 |
|
| 403 |
ascii_strlower_n(label, r); |
| 404 |
siphash24_compress(label, r, state); |
| 405 |
siphash24_compress_byte(0, state); /* make sure foobar and foo.bar result in different hashes */ |
| 406 |
} |
| 407 |
|
| 408 |
/* enforce that all names are terminated by the empty label */ |
| 409 |
string_hash_func("", state); |
| 410 |
} |
| 411 |
|
| 412 |
int dns_name_compare_func(const char *a, const char *b) { |
| 413 |
const char *x, *y; |
| 414 |
int r, q; |
| 415 |
|
| 416 |
assert(a); |
| 417 |
assert(b); |
| 418 |
|
| 419 |
x = a + strlen(a); |
| 420 |
y = b + strlen(b); |
| 421 |
|
| 422 |
for (;;) { |
| 423 |
char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1]; |
| 424 |
|
| 425 |
if (!x && !y) |
| 426 |
return 0; |
| 427 |
|
| 428 |
r = dns_label_unescape_suffix(a, &x, la, sizeof(la)); |
| 429 |
q = dns_label_unescape_suffix(b, &y, lb, sizeof(lb)); |
| 430 |
if (r < 0 || q < 0) |
| 431 |
return strcmp(a, b); /* if not valid DNS labels, then let's compare the whole strings as is */ |
| 432 |
|
| 433 |
r = ascii_strcasecmp_nn(la, r, lb, q); |
| 434 |
if (r != 0) |
| 435 |
return r; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
DEFINE_HASH_OPS( |
| 440 |
dns_name_hash_ops, |
| 441 |
char, |
| 442 |
dns_name_hash_func, |
| 443 |
dns_name_compare_func); |
| 444 |
|
| 445 |
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR( |
| 446 |
dns_name_hash_ops_free, |
| 447 |
char, |
| 448 |
dns_name_hash_func, |
| 449 |
dns_name_compare_func, |
| 450 |
free); |
| 451 |
|
| 452 |
int dns_name_equal(const char *x, const char *y) { |
| 453 |
int r, q; |
| 454 |
|
| 455 |
assert(x); |
| 456 |
assert(y); |
| 457 |
|
| 458 |
for (;;) { |
| 459 |
char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1]; |
| 460 |
|
| 461 |
r = dns_label_unescape(&x, la, sizeof la, 0); |
| 462 |
if (r < 0) |
| 463 |
return r; |
| 464 |
|
| 465 |
q = dns_label_unescape(&y, lb, sizeof lb, 0); |
| 466 |
if (q < 0) |
| 467 |
return q; |
| 468 |
|
| 469 |
if (r != q) |
| 470 |
return false; |
| 471 |
if (r == 0) |
| 472 |
return true; |
| 473 |
|
| 474 |
if (ascii_strcasecmp_n(la, lb, r) != 0) |
| 475 |
return false; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
int dns_name_endswith(const char *name, const char *suffix) { |
| 480 |
const char *n, *s, *saved_n = NULL; |
| 481 |
int r, q; |
| 482 |
|
| 483 |
assert(name); |
| 484 |
assert(suffix); |
| 485 |
|
| 486 |
n = name; |
| 487 |
s = suffix; |
| 488 |
|
| 489 |
for (;;) { |
| 490 |
char ln[DNS_LABEL_MAX+1], ls[DNS_LABEL_MAX+1]; |
| 491 |
|
| 492 |
r = dns_label_unescape(&n, ln, sizeof ln, 0); |
| 493 |
if (r < 0) |
| 494 |
return r; |
| 495 |
|
| 496 |
if (!saved_n) |
| 497 |
saved_n = n; |
| 498 |
|
| 499 |
q = dns_label_unescape(&s, ls, sizeof ls, 0); |
| 500 |
if (q < 0) |
| 501 |
return q; |
| 502 |
|
| 503 |
if (r == 0 && q == 0) |
| 504 |
return true; |
| 505 |
if (r == 0 && saved_n == n) |
| 506 |
return false; |
| 507 |
|
| 508 |
if (r != q || ascii_strcasecmp_n(ln, ls, r) != 0) { |
| 509 |
|
| 510 |
/* Not the same, let's jump back, and try with the next label again */ |
| 511 |
s = suffix; |
| 512 |
n = TAKE_PTR(saved_n); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
int dns_name_startswith(const char *name, const char *prefix) { |
| 518 |
const char *n, *p; |
| 519 |
int r, q; |
| 520 |
|
| 521 |
assert(name); |
| 522 |
assert(prefix); |
| 523 |
|
| 524 |
n = name; |
| 525 |
p = prefix; |
| 526 |
|
| 527 |
for (;;) { |
| 528 |
char ln[DNS_LABEL_MAX+1], lp[DNS_LABEL_MAX+1]; |
| 529 |
|
| 530 |
r = dns_label_unescape(&p, lp, sizeof lp, 0); |
| 531 |
if (r < 0) |
| 532 |
return r; |
| 533 |
if (r == 0) |
| 534 |
return true; |
| 535 |
|
| 536 |
q = dns_label_unescape(&n, ln, sizeof ln, 0); |
| 537 |
if (q < 0) |
| 538 |
return q; |
| 539 |
|
| 540 |
if (r != q) |
| 541 |
return false; |
| 542 |
if (ascii_strcasecmp_n(ln, lp, r) != 0) |
| 543 |
return false; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
int dns_name_change_suffix(const char *name, const char *old_suffix, const char *new_suffix, char **ret) { |
| 548 |
const char *n, *s, *saved_before = NULL, *saved_after = NULL, *prefix; |
| 549 |
int r, q; |
| 550 |
|
| 551 |
assert(name); |
| 552 |
assert(ret); |
| 553 |
|
| 554 |
n = name; |
| 555 |
s = strempty(old_suffix); |
| 556 |
|
| 557 |
for (;;) { |
| 558 |
char ln[DNS_LABEL_MAX+1], ls[DNS_LABEL_MAX+1]; |
| 559 |
|
| 560 |
if (!saved_before) |
| 561 |
saved_before = n; |
| 562 |
|
| 563 |
r = dns_label_unescape(&n, ln, sizeof ln, 0); |
| 564 |
if (r < 0) |
| 565 |
return r; |
| 566 |
|
| 567 |
if (!saved_after) |
| 568 |
saved_after = n; |
| 569 |
|
| 570 |
q = dns_label_unescape(&s, ls, sizeof ls, 0); |
| 571 |
if (q < 0) |
| 572 |
return q; |
| 573 |
|
| 574 |
if (r == 0 && q == 0) |
| 575 |
break; |
| 576 |
if (r == 0 && saved_after == n) { |
| 577 |
*ret = NULL; /* doesn't match */ |
| 578 |
return 0; |
| 579 |
} |
| 580 |
|
| 581 |
if (r != q || ascii_strcasecmp_n(ln, ls, r) != 0) { |
| 582 |
|
| 583 |
/* Not the same, let's jump back, and try with the next label again */ |
| 584 |
s = old_suffix; |
| 585 |
n = TAKE_PTR(saved_after); |
| 586 |
saved_before = NULL; |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
/* Found it! Now generate the new name */ |
| 591 |
prefix = strndupa_safe(name, saved_before - name); |
| 592 |
|
| 593 |
r = dns_name_concat(prefix, new_suffix, 0, ret); |
| 594 |
if (r < 0) |
| 595 |
return r; |
| 596 |
|
| 597 |
return 1; |
| 598 |
} |
| 599 |
|
| 600 |
int dns_name_between(const char *a, const char *b, const char *c) { |
| 601 |
/* Determine if b is strictly greater than a and strictly smaller than c. |
| 602 |
We consider the order of names to be circular, so that if a is |
| 603 |
strictly greater than c, we consider b to be between them if it is |
| 604 |
either greater than a or smaller than c. This is how the canonical |
| 605 |
DNS name order used in NSEC records work. */ |
| 606 |
|
| 607 |
if (dns_name_compare_func(a, c) < 0) |
| 608 |
/* |
| 609 |
a and c are properly ordered: |
| 610 |
a<---b--->c |
| 611 |
*/ |
| 612 |
return dns_name_compare_func(a, b) < 0 && |
| 613 |
dns_name_compare_func(b, c) < 0; |
| 614 |
else |
| 615 |
/* |
| 616 |
a and c are equal or 'reversed': |
| 617 |
<--b--c a-----> |
| 618 |
or: |
| 619 |
<-----c a--b--> |
| 620 |
*/ |
| 621 |
return dns_name_compare_func(b, c) < 0 || |
| 622 |
dns_name_compare_func(a, b) < 0; |
| 623 |
} |
| 624 |
|
| 625 |
int dns_name_reverse(int family, const union in_addr_union *a, char **ret) { |
| 626 |
const uint8_t *p; |
| 627 |
int r; |
| 628 |
|
| 629 |
assert(a); |
| 630 |
assert(ret); |
| 631 |
|
| 632 |
p = (const uint8_t*) a; |
| 633 |
|
| 634 |
if (family == AF_INET) |
| 635 |
r = asprintf(ret, "%u.%u.%u.%u.in-addr.arpa", p[3], p[2], p[1], p[0]); |
| 636 |
else if (family == AF_INET6) |
| 637 |
r = asprintf(ret, "%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.ip6.arpa", |
| 638 |
hexchar(p[15] & 0xF), hexchar(p[15] >> 4), hexchar(p[14] & 0xF), hexchar(p[14] >> 4), |
| 639 |
hexchar(p[13] & 0xF), hexchar(p[13] >> 4), hexchar(p[12] & 0xF), hexchar(p[12] >> 4), |
| 640 |
hexchar(p[11] & 0xF), hexchar(p[11] >> 4), hexchar(p[10] & 0xF), hexchar(p[10] >> 4), |
| 641 |
hexchar(p[ 9] & 0xF), hexchar(p[ 9] >> 4), hexchar(p[ 8] & 0xF), hexchar(p[ 8] >> 4), |
| 642 |
hexchar(p[ 7] & 0xF), hexchar(p[ 7] >> 4), hexchar(p[ 6] & 0xF), hexchar(p[ 6] >> 4), |
| 643 |
hexchar(p[ 5] & 0xF), hexchar(p[ 5] >> 4), hexchar(p[ 4] & 0xF), hexchar(p[ 4] >> 4), |
| 644 |
hexchar(p[ 3] & 0xF), hexchar(p[ 3] >> 4), hexchar(p[ 2] & 0xF), hexchar(p[ 2] >> 4), |
| 645 |
hexchar(p[ 1] & 0xF), hexchar(p[ 1] >> 4), hexchar(p[ 0] & 0xF), hexchar(p[ 0] >> 4)); |
| 646 |
else |
| 647 |
return -EAFNOSUPPORT; |
| 648 |
if (r < 0) |
| 649 |
return -ENOMEM; |
| 650 |
|
| 651 |
return 0; |
| 652 |
} |
| 653 |
|
| 654 |
int dns_name_address(const char *p, int *ret_family, union in_addr_union *ret) { |
| 655 |
int r; |
| 656 |
|
| 657 |
assert(p); |
| 658 |
assert(ret_family); |
| 659 |
assert(ret); |
| 660 |
|
| 661 |
r = dns_name_endswith(p, "in-addr.arpa"); |
| 662 |
if (r < 0) |
| 663 |
return r; |
| 664 |
if (r > 0) { |
| 665 |
uint8_t a[4]; |
| 666 |
|
| 667 |
FOREACH_ELEMENT(i, a) { |
| 668 |
char label[DNS_LABEL_MAX+1]; |
| 669 |
|
| 670 |
r = dns_label_unescape(&p, label, sizeof label, 0); |
| 671 |
if (r < 0) |
| 672 |
return r; |
| 673 |
if (r == 0) |
| 674 |
return -EINVAL; |
| 675 |
if (r > 3) |
| 676 |
return -EINVAL; |
| 677 |
|
| 678 |
r = safe_atou8(label, i); |
| 679 |
if (r < 0) |
| 680 |
return r; |
| 681 |
} |
| 682 |
|
| 683 |
r = dns_name_equal(p, "in-addr.arpa"); |
| 684 |
if (r <= 0) |
| 685 |
return r; |
| 686 |
|
| 687 |
*ret_family = AF_INET; |
| 688 |
ret->in.s_addr = htobe32(((uint32_t) a[3] << 24) | |
| 689 |
((uint32_t) a[2] << 16) | |
| 690 |
((uint32_t) a[1] << 8) | |
| 691 |
(uint32_t) a[0]); |
| 692 |
|
| 693 |
return 1; |
| 694 |
} |
| 695 |
|
| 696 |
r = dns_name_endswith(p, "ip6.arpa"); |
| 697 |
if (r < 0) |
| 698 |
return r; |
| 699 |
if (r > 0) { |
| 700 |
struct in6_addr a; |
| 701 |
|
| 702 |
for (size_t i = 0; i < ELEMENTSOF(a.s6_addr); i++) { |
| 703 |
char label[DNS_LABEL_MAX+1]; |
| 704 |
int x, y; |
| 705 |
|
| 706 |
r = dns_label_unescape(&p, label, sizeof label, 0); |
| 707 |
if (r <= 0) |
| 708 |
return r; |
| 709 |
if (r != 1) |
| 710 |
return -EINVAL; |
| 711 |
x = unhexchar(label[0]); |
| 712 |
if (x < 0) |
| 713 |
return -EINVAL; |
| 714 |
|
| 715 |
r = dns_label_unescape(&p, label, sizeof label, 0); |
| 716 |
if (r <= 0) |
| 717 |
return r; |
| 718 |
if (r != 1) |
| 719 |
return -EINVAL; |
| 720 |
y = unhexchar(label[0]); |
| 721 |
if (y < 0) |
| 722 |
return -EINVAL; |
| 723 |
|
| 724 |
a.s6_addr[ELEMENTSOF(a.s6_addr) - i - 1] = (uint8_t) y << 4 | (uint8_t) x; |
| 725 |
} |
| 726 |
|
| 727 |
r = dns_name_equal(p, "ip6.arpa"); |
| 728 |
if (r <= 0) |
| 729 |
return r; |
| 730 |
|
| 731 |
*ret_family = AF_INET6; |
| 732 |
ret->in6 = a; |
| 733 |
return 1; |
| 734 |
} |
| 735 |
|
| 736 |
*ret_family = AF_UNSPEC; |
| 737 |
*ret = IN_ADDR_NULL; |
| 738 |
|
| 739 |
return 0; |
| 740 |
} |
| 741 |
|
| 742 |
bool dns_name_is_root(const char *name) { |
| 743 |
assert(name); |
| 744 |
|
| 745 |
/* There are exactly two ways to encode the root domain name: |
| 746 |
* as empty string, or with a single dot. */ |
| 747 |
|
| 748 |
return STR_IN_SET(name, "", "."); |
| 749 |
} |
| 750 |
|
| 751 |
bool dns_name_is_single_label(const char *name) { |
| 752 |
int r; |
| 753 |
|
| 754 |
assert(name); |
| 755 |
|
| 756 |
r = dns_name_parent(&name); |
| 757 |
if (r <= 0) |
| 758 |
return false; |
| 759 |
|
| 760 |
return dns_name_is_root(name); |
| 761 |
} |
| 762 |
|
| 763 |
/* Encode a domain name according to RFC 1035 Section 3.1, without compression */ |
| 764 |
int dns_name_to_wire_format(const char *domain, uint8_t *buffer, size_t len, bool canonical) { |
| 765 |
uint8_t *label_length, *out; |
| 766 |
int r; |
| 767 |
|
| 768 |
assert(domain); |
| 769 |
assert(buffer); |
| 770 |
|
| 771 |
out = buffer; |
| 772 |
|
| 773 |
do { |
| 774 |
/* Reserve a byte for label length */ |
| 775 |
if (len <= 0) |
| 776 |
return -ENOBUFS; |
| 777 |
len--; |
| 778 |
label_length = out; |
| 779 |
out++; |
| 780 |
|
| 781 |
/* Convert and copy a single label. Note that |
| 782 |
* dns_label_unescape() returns 0 when it hits the end |
| 783 |
* of the domain name, which we rely on here to encode |
| 784 |
* the trailing NUL byte. */ |
| 785 |
r = dns_label_unescape(&domain, (char *) out, len, 0); |
| 786 |
if (r < 0) |
| 787 |
return r; |
| 788 |
|
| 789 |
/* Optionally, output the name in DNSSEC canonical |
| 790 |
* format, as described in RFC 4034, section 6.2. Or |
| 791 |
* in other words: in lower-case. */ |
| 792 |
if (canonical) |
| 793 |
ascii_strlower_n((char*) out, (size_t) r); |
| 794 |
|
| 795 |
/* Fill label length, move forward */ |
| 796 |
*label_length = r; |
| 797 |
out += r; |
| 798 |
len -= r; |
| 799 |
|
| 800 |
} while (r != 0); |
| 801 |
|
| 802 |
/* Verify the maximum size of the encoded name. The trailing |
| 803 |
* dot + NUL byte account are included this time, hence |
| 804 |
* compare against DNS_HOSTNAME_MAX + 2 (which is 255) this |
| 805 |
* time. */ |
| 806 |
if (out - buffer > DNS_HOSTNAME_MAX + 2) |
| 807 |
return -EINVAL; |
| 808 |
|
| 809 |
return out - buffer; |
| 810 |
} |
| 811 |
|
| 812 |
/* Decode a domain name according to RFC 1035 Section 3.1, without compression */ |
| 813 |
int dns_name_from_wire_format(const uint8_t **data, size_t *len, char **ret) { |
| 814 |
_cleanup_free_ char *domain = NULL; |
| 815 |
const uint8_t *optval; |
| 816 |
size_t optlen, n = 0; |
| 817 |
int r; |
| 818 |
|
| 819 |
assert(data); |
| 820 |
assert(len); |
| 821 |
assert(*data || *len == 0); |
| 822 |
assert(ret); |
| 823 |
|
| 824 |
optval = *data; |
| 825 |
optlen = *len; |
| 826 |
|
| 827 |
for (;;) { |
| 828 |
const char *label; |
| 829 |
uint8_t c; |
| 830 |
|
| 831 |
/* RFC 4704 § 4: fully qualified domain names include the terminating |
| 832 |
* zero-length label, partial names don't. According to the RFC, DHCPv6 |
| 833 |
* servers should always send the fully qualified name, but that's not |
| 834 |
* true in practice. Also accept partial names. */ |
| 835 |
if (optlen == 0) |
| 836 |
break; |
| 837 |
|
| 838 |
/* RFC 1035 § 3.1 total length of encoded name is limited to 255 octets */ |
| 839 |
if (*len - optlen > 255) |
| 840 |
return -EMSGSIZE; |
| 841 |
|
| 842 |
c = *optval; |
| 843 |
optval++; |
| 844 |
optlen--; |
| 845 |
|
| 846 |
if (c == 0) |
| 847 |
/* End label */ |
| 848 |
break; |
| 849 |
if (c > DNS_LABEL_MAX) |
| 850 |
return -EBADMSG; |
| 851 |
if (c > optlen) |
| 852 |
return -EMSGSIZE; |
| 853 |
|
| 854 |
/* Literal label */ |
| 855 |
label = (const char*) optval; |
| 856 |
optval += c; |
| 857 |
optlen -= c; |
| 858 |
|
| 859 |
if (!GREEDY_REALLOC(domain, n + (n != 0) + DNS_LABEL_ESCAPED_MAX)) |
| 860 |
return -ENOMEM; |
| 861 |
|
| 862 |
if (n != 0) |
| 863 |
domain[n++] = '.'; |
| 864 |
|
| 865 |
r = dns_label_escape(label, c, domain + n, DNS_LABEL_ESCAPED_MAX); |
| 866 |
if (r < 0) |
| 867 |
return r; |
| 868 |
|
| 869 |
n += r; |
| 870 |
} |
| 871 |
|
| 872 |
if (!GREEDY_REALLOC(domain, n + 1)) |
| 873 |
return -ENOMEM; |
| 874 |
|
| 875 |
domain[n] = '\0'; |
| 876 |
|
| 877 |
*ret = TAKE_PTR(domain); |
| 878 |
*data = optval; |
| 879 |
*len = optlen; |
| 880 |
|
| 881 |
return n; |
| 882 |
} |
| 883 |
|
| 884 |
static bool srv_type_label_is_valid(const char *label, size_t n) { |
| 885 |
assert(label); |
| 886 |
|
| 887 |
if (n < 2) /* Label needs to be at least 2 chars long */ |
| 888 |
return false; |
| 889 |
|
| 890 |
if (label[0] != '_') /* First label char needs to be underscore */ |
| 891 |
return false; |
| 892 |
|
| 893 |
/* Second char must be a letter */ |
| 894 |
if (!ascii_isalpha(label[1])) |
| 895 |
return false; |
| 896 |
|
| 897 |
/* Third and further chars must be alphanumeric or a hyphen */ |
| 898 |
for (size_t k = 2; k < n; k++) |
| 899 |
if (!ascii_isalpha(label[k]) && |
| 900 |
!ascii_isdigit(label[k]) && |
| 901 |
label[k] != '-') |
| 902 |
return false; |
| 903 |
|
| 904 |
return true; |
| 905 |
} |
| 906 |
|
| 907 |
bool dns_srv_type_is_valid(const char *name) { |
| 908 |
unsigned c = 0; |
| 909 |
int r; |
| 910 |
|
| 911 |
if (!name) |
| 912 |
return false; |
| 913 |
|
| 914 |
for (;;) { |
| 915 |
char label[DNS_LABEL_MAX+1]; |
| 916 |
|
| 917 |
/* This more or less implements RFC 6335, Section 5.1 */ |
| 918 |
|
| 919 |
r = dns_label_unescape(&name, label, sizeof label, 0); |
| 920 |
if (r < 0) |
| 921 |
return false; |
| 922 |
if (r == 0) |
| 923 |
break; |
| 924 |
|
| 925 |
if (c >= 2) |
| 926 |
return false; |
| 927 |
|
| 928 |
if (!srv_type_label_is_valid(label, r)) |
| 929 |
return false; |
| 930 |
|
| 931 |
c++; |
| 932 |
} |
| 933 |
|
| 934 |
return c == 2; /* exactly two labels */ |
| 935 |
} |
| 936 |
|
| 937 |
bool dnssd_srv_type_is_valid(const char *name) { |
| 938 |
return dns_srv_type_is_valid(name) && |
| 939 |
((dns_name_endswith(name, "_tcp") > 0) || |
| 940 |
(dns_name_endswith(name, "_udp") > 0)); /* Specific to DNS-SD. RFC 6763, Section 7 */ |
| 941 |
} |
| 942 |
|
| 943 |
bool dns_service_name_is_valid(const char *name) { |
| 944 |
size_t l; |
| 945 |
|
| 946 |
/* This more or less implements RFC 6763, Section 4.1.1 */ |
| 947 |
|
| 948 |
if (!name) |
| 949 |
return false; |
| 950 |
|
| 951 |
if (!utf8_is_valid(name)) |
| 952 |
return false; |
| 953 |
|
| 954 |
if (string_has_cc(name, NULL)) |
| 955 |
return false; |
| 956 |
|
| 957 |
l = strlen(name); |
| 958 |
if (l <= 0) |
| 959 |
return false; |
| 960 |
if (l > DNS_LABEL_MAX) |
| 961 |
return false; |
| 962 |
|
| 963 |
return true; |
| 964 |
} |
| 965 |
|
| 966 |
bool dns_subtype_name_is_valid(const char *name) { |
| 967 |
size_t l; |
| 968 |
|
| 969 |
/* This more or less implements RFC 6763, Section 7.2 */ |
| 970 |
|
| 971 |
if (!name) |
| 972 |
return false; |
| 973 |
|
| 974 |
if (!utf8_is_valid(name)) |
| 975 |
return false; |
| 976 |
|
| 977 |
if (string_has_cc(name, NULL)) |
| 978 |
return false; |
| 979 |
|
| 980 |
l = strlen(name); |
| 981 |
if (l <= 0) |
| 982 |
return false; |
| 983 |
if (l > DNS_LABEL_MAX) |
| 984 |
return false; |
| 985 |
|
| 986 |
return true; |
| 987 |
} |
| 988 |
|
| 989 |
int dns_service_join(const char *name, const char *type, const char *domain, char **ret) { |
| 990 |
char escaped[DNS_LABEL_ESCAPED_MAX]; |
| 991 |
_cleanup_free_ char *n = NULL; |
| 992 |
int r; |
| 993 |
|
| 994 |
assert(type); |
| 995 |
assert(domain); |
| 996 |
assert(ret); |
| 997 |
|
| 998 |
if (!dns_srv_type_is_valid(type)) |
| 999 |
return -EINVAL; |
| 1000 |
|
| 1001 |
if (!name) |
| 1002 |
return dns_name_concat(type, domain, 0, ret); |
| 1003 |
|
| 1004 |
if (!dns_service_name_is_valid(name)) |
| 1005 |
return -EINVAL; |
| 1006 |
|
| 1007 |
r = dns_label_escape(name, strlen(name), escaped, sizeof(escaped)); |
| 1008 |
if (r < 0) |
| 1009 |
return r; |
| 1010 |
|
| 1011 |
r = dns_name_concat(type, domain, 0, &n); |
| 1012 |
if (r < 0) |
| 1013 |
return r; |
| 1014 |
|
| 1015 |
return dns_name_concat(escaped, n, 0, ret); |
| 1016 |
} |
| 1017 |
|
| 1018 |
static bool dns_service_name_label_is_valid(const char *label, size_t n) { |
| 1019 |
char *s; |
| 1020 |
|
| 1021 |
assert(label); |
| 1022 |
|
| 1023 |
if (memchr(label, 0, n)) |
| 1024 |
return false; |
| 1025 |
|
| 1026 |
s = strndupa_safe(label, n); |
| 1027 |
return dns_service_name_is_valid(s); |
| 1028 |
} |
| 1029 |
|
| 1030 |
int dns_service_split(const char *joined, char **ret_name, char **ret_type, char **ret_domain) { |
| 1031 |
_cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL; |
| 1032 |
const char *p = joined, *q = NULL, *d = joined; |
| 1033 |
char a[DNS_LABEL_MAX+1], b[DNS_LABEL_MAX+1], c[DNS_LABEL_MAX+1]; |
| 1034 |
int an, bn, cn, r; |
| 1035 |
unsigned x = 0; |
| 1036 |
|
| 1037 |
assert(joined); |
| 1038 |
|
| 1039 |
/* Get first label from the full name */ |
| 1040 |
an = dns_label_unescape(&p, a, sizeof(a), 0); |
| 1041 |
if (an < 0) |
| 1042 |
return an; |
| 1043 |
|
| 1044 |
if (an > 0) { |
| 1045 |
x++; |
| 1046 |
|
| 1047 |
/* If there was a first label, try to get the second one */ |
| 1048 |
bn = dns_label_unescape(&p, b, sizeof(b), 0); |
| 1049 |
if (bn < 0) |
| 1050 |
return bn; |
| 1051 |
|
| 1052 |
if (bn > 0) { |
| 1053 |
if (!srv_type_label_is_valid(b, bn)) |
| 1054 |
goto finish; |
| 1055 |
|
| 1056 |
x++; |
| 1057 |
|
| 1058 |
/* If there was a second label, try to get the third one */ |
| 1059 |
q = p; |
| 1060 |
cn = dns_label_unescape(&p, c, sizeof(c), 0); |
| 1061 |
if (cn < 0) |
| 1062 |
return cn; |
| 1063 |
|
| 1064 |
if (cn > 0 && srv_type_label_is_valid(c, cn)) |
| 1065 |
x++; |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
switch (x) { |
| 1070 |
case 2: |
| 1071 |
if (!srv_type_label_is_valid(a, an)) |
| 1072 |
break; |
| 1073 |
|
| 1074 |
/* OK, got <type> . <type2> . <domain> */ |
| 1075 |
|
| 1076 |
name = NULL; |
| 1077 |
|
| 1078 |
type = strjoin(a, ".", b); |
| 1079 |
if (!type) |
| 1080 |
return -ENOMEM; |
| 1081 |
|
| 1082 |
d = q; |
| 1083 |
break; |
| 1084 |
|
| 1085 |
case 3: |
| 1086 |
if (!dns_service_name_label_is_valid(a, an)) |
| 1087 |
break; |
| 1088 |
|
| 1089 |
/* OK, got <name> . <type> . <type2> . <domain> */ |
| 1090 |
|
| 1091 |
name = strndup(a, an); |
| 1092 |
if (!name) |
| 1093 |
return -ENOMEM; |
| 1094 |
|
| 1095 |
type = strjoin(b, ".", c); |
| 1096 |
if (!type) |
| 1097 |
return -ENOMEM; |
| 1098 |
|
| 1099 |
d = p; |
| 1100 |
break; |
| 1101 |
} |
| 1102 |
|
| 1103 |
finish: |
| 1104 |
r = dns_name_normalize(d, 0, &domain); |
| 1105 |
if (r < 0) |
| 1106 |
return r; |
| 1107 |
|
| 1108 |
if (ret_domain) |
| 1109 |
*ret_domain = TAKE_PTR(domain); |
| 1110 |
|
| 1111 |
if (ret_type) |
| 1112 |
*ret_type = TAKE_PTR(type); |
| 1113 |
|
| 1114 |
if (ret_name) |
| 1115 |
*ret_name = TAKE_PTR(name); |
| 1116 |
|
| 1117 |
return 0; |
| 1118 |
} |
| 1119 |
|
| 1120 |
static int dns_name_build_suffix_table(const char *name, const char *table[]) { |
| 1121 |
const char *p = ASSERT_PTR(name); |
| 1122 |
unsigned n = 0; |
| 1123 |
int r; |
| 1124 |
|
| 1125 |
assert(table); |
| 1126 |
|
| 1127 |
for (;;) { |
| 1128 |
if (n > DNS_N_LABELS_MAX) |
| 1129 |
return -EINVAL; |
| 1130 |
|
| 1131 |
table[n] = p; |
| 1132 |
r = dns_name_parent(&p); |
| 1133 |
if (r < 0) |
| 1134 |
return r; |
| 1135 |
if (r == 0) |
| 1136 |
break; |
| 1137 |
|
| 1138 |
n++; |
| 1139 |
} |
| 1140 |
|
| 1141 |
return (int) n; |
| 1142 |
} |
| 1143 |
|
| 1144 |
int dns_name_suffix(const char *name, unsigned n_labels, const char **ret) { |
| 1145 |
const char* labels[DNS_N_LABELS_MAX+1]; |
| 1146 |
int n; |
| 1147 |
|
| 1148 |
assert(name); |
| 1149 |
assert(ret); |
| 1150 |
|
| 1151 |
n = dns_name_build_suffix_table(name, labels); |
| 1152 |
if (n < 0) |
| 1153 |
return n; |
| 1154 |
|
| 1155 |
if ((unsigned) n < n_labels) |
| 1156 |
return -EINVAL; |
| 1157 |
|
| 1158 |
*ret = labels[n - n_labels]; |
| 1159 |
return (int) (n - n_labels); |
| 1160 |
} |
| 1161 |
|
| 1162 |
int dns_name_skip(const char *a, unsigned n_labels, const char **ret) { |
| 1163 |
int r; |
| 1164 |
|
| 1165 |
assert(a); |
| 1166 |
assert(ret); |
| 1167 |
|
| 1168 |
for (; n_labels > 0; n_labels--) { |
| 1169 |
r = dns_name_parent(&a); |
| 1170 |
if (r < 0) |
| 1171 |
return r; |
| 1172 |
if (r == 0) { |
| 1173 |
*ret = ""; |
| 1174 |
return 0; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
*ret = a; |
| 1179 |
return 1; |
| 1180 |
} |
| 1181 |
|
| 1182 |
int dns_name_count_labels(const char *name) { |
| 1183 |
unsigned n = 0; |
| 1184 |
int r; |
| 1185 |
|
| 1186 |
assert(name); |
| 1187 |
|
| 1188 |
for (const char *p = name;;) { |
| 1189 |
r = dns_name_parent(&p); |
| 1190 |
if (r < 0) |
| 1191 |
return r; |
| 1192 |
if (r == 0) |
| 1193 |
break; |
| 1194 |
|
| 1195 |
if (n >= DNS_N_LABELS_MAX) |
| 1196 |
return -EINVAL; |
| 1197 |
|
| 1198 |
n++; |
| 1199 |
} |
| 1200 |
|
| 1201 |
return n; |
| 1202 |
} |
| 1203 |
|
| 1204 |
int dns_name_equal_skip(const char *a, unsigned n_labels, const char *b) { |
| 1205 |
int r; |
| 1206 |
|
| 1207 |
assert(a); |
| 1208 |
assert(b); |
| 1209 |
|
| 1210 |
r = dns_name_skip(a, n_labels, &a); |
| 1211 |
if (r <= 0) |
| 1212 |
return r; |
| 1213 |
|
| 1214 |
return dns_name_equal(a, b); |
| 1215 |
} |
| 1216 |
|
| 1217 |
int dns_name_common_suffix(const char *a, const char *b, const char **ret) { |
| 1218 |
const char *a_labels[DNS_N_LABELS_MAX+1], *b_labels[DNS_N_LABELS_MAX+1]; |
| 1219 |
int n = 0, m = 0, k = 0, r, q; |
| 1220 |
|
| 1221 |
assert(a); |
| 1222 |
assert(b); |
| 1223 |
assert(ret); |
| 1224 |
|
| 1225 |
/* Determines the common suffix of domain names a and b */ |
| 1226 |
|
| 1227 |
n = dns_name_build_suffix_table(a, a_labels); |
| 1228 |
if (n < 0) |
| 1229 |
return n; |
| 1230 |
|
| 1231 |
m = dns_name_build_suffix_table(b, b_labels); |
| 1232 |
if (m < 0) |
| 1233 |
return m; |
| 1234 |
|
| 1235 |
for (;;) { |
| 1236 |
char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1]; |
| 1237 |
const char *x, *y; |
| 1238 |
|
| 1239 |
if (k >= n || k >= m) { |
| 1240 |
*ret = a_labels[n - k]; |
| 1241 |
return 0; |
| 1242 |
} |
| 1243 |
|
| 1244 |
x = a_labels[n - 1 - k]; |
| 1245 |
r = dns_label_unescape(&x, la, sizeof la, 0); |
| 1246 |
if (r < 0) |
| 1247 |
return r; |
| 1248 |
|
| 1249 |
y = b_labels[m - 1 - k]; |
| 1250 |
q = dns_label_unescape(&y, lb, sizeof lb, 0); |
| 1251 |
if (q < 0) |
| 1252 |
return q; |
| 1253 |
|
| 1254 |
if (r != q || ascii_strcasecmp_n(la, lb, r) != 0) { |
| 1255 |
*ret = a_labels[n - k]; |
| 1256 |
return 0; |
| 1257 |
} |
| 1258 |
|
| 1259 |
k++; |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
int dns_name_apply_idna(const char *name, char **ret) { |
| 1264 |
|
| 1265 |
/* Return negative on error, 0 if not implemented, positive on success. */ |
| 1266 |
|
| 1267 |
#if HAVE_LIBIDN2 |
| 1268 |
int r; |
| 1269 |
|
| 1270 |
r = dlopen_idn(LOG_DEBUG); |
| 1271 |
if (r == -EOPNOTSUPP) { |
| 1272 |
*ret = NULL; |
| 1273 |
return 0; |
| 1274 |
} |
| 1275 |
if (r < 0) |
| 1276 |
return r; |
| 1277 |
|
| 1278 |
_cleanup_free_ char *t = NULL; |
| 1279 |
|
| 1280 |
assert(name); |
| 1281 |
assert(ret); |
| 1282 |
|
| 1283 |
/* First, try non-transitional mode (i.e. IDN2008 rules) */ |
| 1284 |
r = sym_idn2_lookup_u8((uint8_t*) name, (uint8_t**) &t, |
| 1285 |
IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL); |
| 1286 |
if (r == IDN2_DISALLOWED) /* If that failed, because of disallowed characters, try transitional mode. |
| 1287 |
* (i.e. IDN2003 rules which supports some unicode chars IDN2008 doesn't allow). */ |
| 1288 |
r = sym_idn2_lookup_u8((uint8_t*) name, (uint8_t**) &t, |
| 1289 |
IDN2_NFC_INPUT | IDN2_TRANSITIONAL); |
| 1290 |
|
| 1291 |
log_debug("idn2_lookup_u8: %s %s %s", name, glyph(GLYPH_ARROW_RIGHT), t); |
| 1292 |
if (r == IDN2_OK) { |
| 1293 |
if (!startswith(name, "xn--")) { |
| 1294 |
_cleanup_free_ char *s = NULL; |
| 1295 |
|
| 1296 |
r = sym_idn2_to_unicode_8z8z(t, &s, 0); |
| 1297 |
if (r != IDN2_OK) { |
| 1298 |
log_debug("idn2_to_unicode_8z8z(\"%s\") failed: %d/%s", |
| 1299 |
t, r, sym_idn2_strerror(r)); |
| 1300 |
*ret = NULL; |
| 1301 |
return 0; |
| 1302 |
} |
| 1303 |
|
| 1304 |
if (!streq_ptr(name, s)) { |
| 1305 |
log_debug("idn2 roundtrip failed: \"%s\" %s \"%s\" %s \"%s\", ignoring.", |
| 1306 |
name, glyph(GLYPH_ARROW_RIGHT), t, |
| 1307 |
glyph(GLYPH_ARROW_RIGHT), s); |
| 1308 |
*ret = NULL; |
| 1309 |
return 0; |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
*ret = TAKE_PTR(t); |
| 1314 |
return 1; /* *ret has been written */ |
| 1315 |
} |
| 1316 |
|
| 1317 |
log_debug("idn2_lookup_u8(\"%s\") failed: %d/%s", name, r, sym_idn2_strerror(r)); |
| 1318 |
if (r == IDN2_2HYPHEN) |
| 1319 |
/* The name has two hyphens — forbidden by IDNA2008 in some cases */ |
| 1320 |
return 0; |
| 1321 |
if (IN_SET(r, IDN2_TOO_BIG_DOMAIN, IDN2_TOO_BIG_LABEL)) |
| 1322 |
return -ENOSPC; |
| 1323 |
|
| 1324 |
return -EINVAL; |
| 1325 |
#else |
| 1326 |
*ret = NULL; |
| 1327 |
return 0; |
| 1328 |
#endif |
| 1329 |
} |
| 1330 |
|
| 1331 |
int dns_name_is_valid_or_address(const char *name) { |
| 1332 |
/* Returns > 0 if the specified name is either a valid IP address formatted as string or a valid DNS name */ |
| 1333 |
|
| 1334 |
if (isempty(name)) |
| 1335 |
return 0; |
| 1336 |
|
| 1337 |
if (in_addr_from_string_auto(name, NULL, NULL) >= 0) |
| 1338 |
return 1; |
| 1339 |
|
| 1340 |
return dns_name_is_valid(name); |
| 1341 |
} |
| 1342 |
|
| 1343 |
int dns_name_dot_suffixed(const char *name) { |
| 1344 |
const char *p = name; |
| 1345 |
int r; |
| 1346 |
|
| 1347 |
for (;;) { |
| 1348 |
if (streq(p, ".")) |
| 1349 |
return true; |
| 1350 |
|
| 1351 |
r = dns_label_unescape(&p, NULL, DNS_LABEL_MAX, DNS_LABEL_LEAVE_TRAILING_DOT); |
| 1352 |
if (r < 0) |
| 1353 |
return r; |
| 1354 |
if (r == 0) |
| 1355 |
return false; |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
bool dns_name_dont_resolve(const char *name) { |
| 1360 |
|
| 1361 |
/* Never respond to some of the domains listed in RFC6303 */ |
| 1362 |
if (dns_name_endswith(name, "0.in-addr.arpa") > 0 || |
| 1363 |
dns_name_equal(name, "255.255.255.255.in-addr.arpa") > 0 || |
| 1364 |
dns_name_equal(name, "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa") > 0) |
| 1365 |
return true; |
| 1366 |
|
| 1367 |
/* Never respond to some of the domains listed in RFC6761 */ |
| 1368 |
if (dns_name_endswith(name, "invalid") > 0) |
| 1369 |
return true; |
| 1370 |
|
| 1371 |
/* Never respond to some of the domains listed in RFC9476 */ |
| 1372 |
if (dns_name_endswith(name, "alt") > 0) |
| 1373 |
return true; |
| 1374 |
|
| 1375 |
return false; |
| 1376 |
} |