1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <stringprep.h>
27 #include "dns-domain.h"
29 int dns_label_unescape(const char **name, char *dest, size_t sz) {
53 if (r >= DNS_LABEL_MAX)
57 /* Escaped character */
65 else if (*n == '\\' || *n == '.') {
66 /* Escaped backslash or dot */
71 } else if (n[0] >= '0' && n[0] <= '9') {
74 /* Escaped literal ASCII character */
76 if (!(n[1] >= '0' && n[1] <= '9') ||
77 !(n[2] >= '0' && n[2] <= '9'))
80 k = ((unsigned) (n[0] - '0') * 100) +
81 ((unsigned) (n[1] - '0') * 10) +
82 ((unsigned) (n[2] - '0'));
84 /* Don't allow CC characters or anything that doesn't fit in 8bit */
85 if (k < ' ' || k > 255 || k == 127)
96 } else if ((uint8_t) *n >= (uint8_t) ' ' && *n != 127) {
98 /* Normal character */
106 /* Empty label that is not at the end? */
117 int dns_label_escape(const char *p, size_t l, char **ret) {
118 _cleanup_free_ char *s = NULL;
125 if (l > DNS_LABEL_MAX)
128 s = malloc(l * 4 + 1);
135 if (*p == '.' || *p == '\\') {
137 /* Dot or backslash */
141 } else if (*p == '_' ||
143 (*p >= '0' && *p <= '9') ||
144 (*p >= 'a' && *p <= 'z') ||
145 (*p >= 'A' && *p <= 'Z')) {
147 /* Proper character */
149 } else if ((uint8_t) *p >= (uint8_t) ' ' && *p != 127) {
151 /* Everything else */
153 *(q++) = '0' + (char) ((uint8_t) *p / 100);
154 *(q++) = '0' + (char) (((uint8_t) *p / 10) % 10);
155 *(q++) = '0' + (char) ((uint8_t) *p % 10);
172 int dns_label_apply_idna(const char *encoded, size_t encoded_size, char *decoded, size_t decoded_max) {
174 _cleanup_free_ uint32_t *input = NULL;
177 bool contains_8bit = false;
181 assert(decoded_max >= DNS_LABEL_MAX);
183 if (encoded_size <= 0)
186 for (p = encoded; p < encoded + encoded_size; p++)
187 if ((uint8_t) *p > 127)
188 contains_8bit = true;
193 input = stringprep_utf8_to_ucs4(encoded, encoded_size, &input_size);
197 if (idna_to_ascii_4i(input, input_size, decoded, 0) != 0)
200 return strlen(decoded);
206 int dns_label_undo_idna(const char *encoded, size_t encoded_size, char *decoded, size_t decoded_max) {
208 size_t input_size, output_size;
209 _cleanup_free_ uint32_t *input = NULL;
210 _cleanup_free_ char *result = NULL;
211 uint32_t *output = NULL;
214 /* To be invoked after unescaping */
219 if (encoded_size < sizeof(IDNA_ACE_PREFIX)-1)
222 if (memcmp(encoded, IDNA_ACE_PREFIX, sizeof(IDNA_ACE_PREFIX) -1) != 0)
225 input = stringprep_utf8_to_ucs4(encoded, encoded_size, &input_size);
229 output_size = input_size;
230 output = newa(uint32_t, output_size);
232 idna_to_unicode_44i(input, input_size, output, &output_size, 0);
234 result = stringprep_ucs4_to_utf8(output, output_size, NULL, &w);
239 if (w+1 > decoded_max)
242 memcpy(decoded, result, w+1);
249 int dns_name_normalize(const char *s, char **_ret) {
250 _cleanup_free_ char *ret = NULL;
251 size_t n = 0, allocated = 0;
259 _cleanup_free_ char *t = NULL;
260 char label[DNS_LABEL_MAX];
263 r = dns_label_unescape(&p, label, sizeof(label));
272 k = dns_label_undo_idna(label, r, label, sizeof(label));
278 r = dns_label_escape(label, r, &t);
282 if (!GREEDY_REALLOC(ret, allocated, n + !first + strlen(t) + 1))
290 memcpy(ret + n, t, r);
294 if (n > DNS_NAME_MAX)
297 if (!GREEDY_REALLOC(ret, allocated, n + 1))
310 unsigned long dns_name_hash_func(const void *s, const uint8_t hash_key[HASH_KEY_SIZE]) {
312 unsigned long ul = hash_key[0];
318 char label[DNS_LABEL_MAX+1];
321 r = dns_label_unescape(&p, label, sizeof(label));
325 k = dns_label_undo_idna(label, r, label, sizeof(label));
332 ascii_strlower(label);
334 ul = ul * hash_key[1] + ul + string_hash_func(label, hash_key);
340 int dns_name_compare_func(const void *a, const void *b) {
341 const char *x = a, *y = b;
348 char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1];
350 if (*x == 0 && *y == 0)
353 r = dns_label_unescape(&x, la, sizeof(la));
354 q = dns_label_unescape(&y, lb, sizeof(lb));
358 k = dns_label_undo_idna(la, r, la, sizeof(la));
359 w = dns_label_undo_idna(lb, q, lb, sizeof(lb));
368 r = strcasecmp(la, lb);
374 const struct hash_ops dns_name_hash_ops = {
375 .hash = dns_name_hash_func,
376 .compare = dns_name_compare_func
379 int dns_name_equal(const char *x, const char *y) {
386 char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1];
388 if (*x == 0 && *y == 0)
391 r = dns_label_unescape(&x, la, sizeof(la));
395 k = dns_label_undo_idna(la, r, la, sizeof(la));
401 q = dns_label_unescape(&y, lb, sizeof(lb));
404 w = dns_label_undo_idna(lb, q, lb, sizeof(lb));
411 if (strcasecmp(la, lb))
416 int dns_name_endswith(const char *name, const char *suffix) {
417 const char *n, *s, *saved_n = NULL;
427 char ln[DNS_LABEL_MAX+1], ls[DNS_LABEL_MAX+1];
429 r = dns_label_unescape(&n, ln, sizeof(ln));
432 k = dns_label_undo_idna(ln, r, ln, sizeof(ln));
441 q = dns_label_unescape(&s, ls, sizeof(ls));
444 w = dns_label_undo_idna(ls, q, ls, sizeof(ls));
450 if (r == 0 && q == 0)
452 if (r == 0 && saved_n == n)
457 if (r != q || strcasecmp(ln, ls)) {
459 /* Not the same, let's jump back, and try with the next label again */
467 int dns_name_between(const char *a, const char *b, const char *c) {
470 /* Determine if b is strictly greater than a and strictly smaller than c.
471 We consider the order of names to be circular, so that if a is
472 strictly greater than c, we consider b to be between them if it is
473 either greater than a or smaller than c. This is how the canonical
474 DNS name order used in NSEC records work. */
476 n = dns_name_compare_func(a, c);
481 return dns_name_compare_func(a, b) < 0 &&
482 dns_name_compare_func(b, c) < 0;
484 /* <--b--c a--b--> */
485 return dns_name_compare_func(b, c) < 0 ||
486 dns_name_compare_func(a, b) < 0;
489 int dns_name_reverse(int family, const union in_addr_union *a, char **ret) {
496 p = (const uint8_t*) a;
498 if (family == AF_INET)
499 r = asprintf(ret, "%u.%u.%u.%u.in-addr.arpa", p[3], p[2], p[1], p[0]);
500 else if (family == AF_INET6)
501 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",
502 hexchar(p[15] & 0xF), hexchar(p[15] >> 4), hexchar(p[14] & 0xF), hexchar(p[14] >> 4),
503 hexchar(p[13] & 0xF), hexchar(p[13] >> 4), hexchar(p[12] & 0xF), hexchar(p[12] >> 4),
504 hexchar(p[11] & 0xF), hexchar(p[11] >> 4), hexchar(p[10] & 0xF), hexchar(p[10] >> 4),
505 hexchar(p[ 9] & 0xF), hexchar(p[ 9] >> 4), hexchar(p[ 8] & 0xF), hexchar(p[ 8] >> 4),
506 hexchar(p[ 7] & 0xF), hexchar(p[ 7] >> 4), hexchar(p[ 6] & 0xF), hexchar(p[ 6] >> 4),
507 hexchar(p[ 5] & 0xF), hexchar(p[ 5] >> 4), hexchar(p[ 4] & 0xF), hexchar(p[ 4] >> 4),
508 hexchar(p[ 3] & 0xF), hexchar(p[ 3] >> 4), hexchar(p[ 2] & 0xF), hexchar(p[ 2] >> 4),
509 hexchar(p[ 1] & 0xF), hexchar(p[ 1] >> 4), hexchar(p[ 0] & 0xF), hexchar(p[ 0] >> 4));
511 return -EAFNOSUPPORT;
518 int dns_name_address(const char *p, int *family, union in_addr_union *address) {
525 r = dns_name_endswith(p, "in-addr.arpa");
532 for (i = 0; i < ELEMENTSOF(a); i++) {
533 char label[DNS_LABEL_MAX+1];
535 r = dns_label_unescape(&p, label, sizeof(label));
543 r = safe_atou8(label, &a[i]);
548 r = dns_name_equal(p, "in-addr.arpa");
553 address->in.s_addr = htobe32(((uint32_t) a[3] << 24) |
554 ((uint32_t) a[2] << 16) |
555 ((uint32_t) a[1] << 8) |
561 r = dns_name_endswith(p, "ip6.arpa");
568 for (i = 0; i < ELEMENTSOF(a.s6_addr); i++) {
569 char label[DNS_LABEL_MAX+1];
572 r = dns_label_unescape(&p, label, sizeof(label));
577 x = unhexchar(label[0]);
581 r = dns_label_unescape(&p, label, sizeof(label));
586 y = unhexchar(label[0]);
590 a.s6_addr[ELEMENTSOF(a.s6_addr) - i - 1] = (uint8_t) y << 4 | (uint8_t) x;
593 r = dns_name_equal(p, "ip6.arpa");
605 int dns_name_root(const char *name) {
606 char label[DNS_LABEL_MAX+1];
611 r = dns_label_unescape(&name, label, sizeof(label));
615 return r == 0 && *name == 0;
618 int dns_name_single_label(const char *name) {
619 char label[DNS_LABEL_MAX+1];
624 r = dns_label_unescape(&name, label, sizeof(label));
630 r = dns_label_unescape(&name, label, sizeof(label));
634 return r == 0 && *name == 0;