dns-domain.c revision b9282bc12840aff500a334836226f6b8df24926d
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_LIBIDN
#include <idna.h>
#include <stringprep.h>
#endif
#include <endian.h>
#include <stdio.h>
#include <string.h>
#include "alloc-util.h"
#include "dns-domain.h"
#include "hashmap.h"
#include "hexdecoct.h"
#include "in-addr-util.h"
#include "macro.h"
#include "parse-util.h"
#include "string-util.h"
#include "strv.h"
#include "utf8.h"
const char *n;
char *d;
int r = 0;
n = *name;
d = dest;
for (;;) {
if (*n == '.') {
n++;
break;
}
if (*n == 0)
break;
if (r >= DNS_LABEL_MAX)
return -EINVAL;
if (sz <= 0)
return -ENOBUFS;
if (*n == '\\') {
/* Escaped character */
n++;
if (*n == 0)
/* Ending NUL */
return -EINVAL;
else if (*n == '\\' || *n == '.') {
/* Escaped backslash or dot */
if (d)
*(d++) = *n;
sz--;
r++;
n++;
} else if (n[0] >= '0' && n[0] <= '9') {
unsigned k;
/* Escaped literal ASCII character */
if (!(n[1] >= '0' && n[1] <= '9') ||
!(n[2] >= '0' && n[2] <= '9'))
return -EINVAL;
k = ((unsigned) (n[0] - '0') * 100) +
((unsigned) (n[1] - '0') * 10) +
((unsigned) (n[2] - '0'));
/* Don't allow anything that doesn't
* fit in 8bit. Note that we do allow
* control characters, as some servers
* (e.g. cloudflare) are happy to
* generate labels with them
* inside. */
if (k > 255)
return -EINVAL;
if (d)
*(d++) = (char) k;
sz--;
r++;
n += 3;
} else
return -EINVAL;
/* Normal character */
if (d)
*(d++) = *n;
sz--;
r++;
n++;
} else
return -EINVAL;
}
/* Empty label that is not at the end? */
if (r == 0 && *n)
return -EINVAL;
if (sz >= 1 && d)
*d = 0;
*name = n;
return r;
}
/* @label_terminal: terminal character of a label, updated to point to the terminal character of
* the previous label (always skipping one dot) or to NULL if there are no more
* labels. */
int dns_label_unescape_suffix(const char *name, const char **label_terminal, char *dest, size_t sz) {
const char *terminal;
int r;
/* no more labels */
if (!*label_terminal) {
if (sz >= 1)
*dest = 0;
return 0;
}
/* Skip current terminal character (and accept domain names ending it ".") */
if (*terminal == 0)
terminal--;
terminal--;
/* Point name to the last label, and terminal to the preceding terminal symbol (or make it a NULL pointer) */
for (;;) {
/* Reached the first label, so indicate that there are no more */
break;
}
/* Find the start of the last label */
if (*terminal == '.') {
const char *y;
unsigned slashes = 0;
slashes ++;
if (slashes % 2 == 0) {
/* The '.' was not escaped */
break;
} else {
terminal = y;
continue;
}
}
terminal --;
}
if (r < 0)
return r;
return r;
}
char *q;
/* DNS labels must be between 1 and 63 characters long. A
* zero-length label does not exist. See RFC 2182, Section
* 11. */
if (l <= 0 || l > DNS_LABEL_MAX)
return -EINVAL;
if (sz < 1)
return -ENOBUFS;
assert(p);
q = dest;
while (l > 0) {
if (*p == '.' || *p == '\\') {
/* Dot or backslash */
if (sz < 3)
return -ENOBUFS;
*(q++) = '\\';
*(q++) = *p;
sz -= 2;
} else if (*p == '_' ||
*p == '-' ||
(*p >= '0' && *p <= '9') ||
(*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z')) {
/* Proper character */
if (sz < 2)
return -ENOBUFS;
*(q++) = *p;
sz -= 1;
} else {
/* Everything else */
if (sz < 5)
return -ENOBUFS;
*(q++) = '\\';
sz -= 4;
}
p++;
l--;
}
*q = 0;
return (int) (q - dest);
}
_cleanup_free_ char *s = NULL;
int r;
assert(p);
if (l <= 0 || l > DNS_LABEL_MAX)
return -EINVAL;
s = new(char, DNS_LABEL_ESCAPED_MAX);
if (!s)
return -ENOMEM;
r = dns_label_escape(p, l, s, DNS_LABEL_ESCAPED_MAX);
if (r < 0)
return r;
*ret = s;
s = NULL;
return r;
}
int dns_label_apply_idna(const char *encoded, size_t encoded_size, char *decoded, size_t decoded_max) {
#ifdef HAVE_LIBIDN
size_t input_size, l;
const char *p;
bool contains_8bit = false;
/* Converts an U-label into an A-label */
if (encoded_size <= 0)
return -EINVAL;
if ((uint8_t) *p > 127)
contains_8bit = true;
if (!contains_8bit) {
if (encoded_size > DNS_LABEL_MAX)
return -EINVAL;
return 0;
}
if (!input)
return -ENOMEM;
return -EINVAL;
/* Verify that the the result is not longer than one DNS label. */
if (l <= 0 || l > DNS_LABEL_MAX)
return -EINVAL;
if (l > decoded_max)
return -ENOBUFS;
/* If there's room, append a trailing NUL byte, but only then */
if (decoded_max > l)
decoded[l] = 0;
return (int) l;
#else
return 0;
#endif
}
int dns_label_undo_idna(const char *encoded, size_t encoded_size, char *decoded, size_t decoded_max) {
#ifdef HAVE_LIBIDN
size_t w;
/* To be invoked after unescaping. Converts an A-label into an U-label. */
return -EINVAL;
return 0;
return 0;
if (!input)
return -ENOMEM;
if (!result)
return -ENOMEM;
if (w <= 0)
return -EINVAL;
if (w > decoded_max)
return -ENOBUFS;
/* Append trailing NUL byte if there's space, but only then. */
if (decoded_max > w)
decoded[w] = 0;
return w;
#else
return 0;
#endif
}
int dns_name_concat(const char *a, const char *b, char **_ret) {
const char *p = a;
bool first = true;
int r;
assert(a);
for (;;) {
char label[DNS_LABEL_MAX];
int k;
if (r < 0)
return r;
if (r == 0) {
if (*p != 0)
return -EINVAL;
if (b) {
/* Now continue with the second string, if there is one */
p = b;
b = NULL;
continue;
}
break;
}
if (k < 0)
return k;
if (k > 0)
r = k;
if (_ret) {
return -ENOMEM;
if (r < 0)
return r;
if (!first)
ret[n] = '.';
} else {
char escaped[DNS_LABEL_ESCAPED_MAX];
if (r < 0)
return r;
}
if (!first)
n++;
else
first = false;
n += r;
}
if (n > DNS_HOSTNAME_MAX)
return -EINVAL;
if (_ret) {
return -ENOMEM;
ret[n] = 0;
}
return 0;
}
const char *p = s;
int r;
assert(p);
for (;;) {
int k;
if (r < 0)
break;
if (r == 0)
break;
if (k < 0)
break;
if (k > 0)
r = k;
ascii_strlower_n(label, r);
}
/* enforce that all names are terminated by the empty label */
}
int dns_name_compare_func(const void *a, const void *b) {
const char *x, *y;
int r, q, k, w;
assert(a);
assert(b);
x = (const char *) a + strlen(a);
y = (const char *) b + strlen(b);
for (;;) {
return 0;
if (r < 0 || q < 0)
return r - q;
if (r > 0)
else
k = 0;
if (q > 0)
else
w = 0;
if (k < 0 || w < 0)
return k - w;
if (k > 0)
r = k;
if (w > 0)
q = w;
if (r != 0)
return r;
}
}
const struct hash_ops dns_name_hash_ops = {
};
int r, k;
/* Clobbers all arguments on failure... */
if (r <= 0)
return r;
if (k < 0)
return k;
if (k == 0) /* not an IDNA name */
return r;
return k;
}
int dns_name_equal(const char *x, const char *y) {
int r, q;
assert(x);
assert(y);
for (;;) {
if (r < 0)
return r;
if (q < 0)
return q;
if (r != q)
return false;
if (r == 0)
return true;
return false;
}
}
int r, q;
n = name;
s = suffix;
for (;;) {
if (r < 0)
return r;
if (!saved_n)
saved_n = n;
if (q < 0)
return q;
if (r == 0 && q == 0)
return true;
if (r == 0 && saved_n == n)
return false;
/* Not the same, let's jump back, and try with the next label again */
s = suffix;
n = saved_n;
}
}
}
const char *n, *p;
int r, q;
n = name;
p = prefix;
for (;;) {
if (r < 0)
return r;
if (r == 0)
return true;
if (q < 0)
return q;
if (r != q)
return false;
return false;
}
}
int dns_name_change_suffix(const char *name, const char *old_suffix, const char *new_suffix, char **ret) {
int r, q;
n = name;
s = old_suffix;
for (;;) {
if (!saved_before)
saved_before = n;
if (r < 0)
return r;
if (!saved_after)
saved_after = n;
if (q < 0)
return q;
if (r == 0 && q == 0)
break;
if (r == 0 && saved_after == n) {
return 0;
}
/* Not the same, let's jump back, and try with the next label again */
s = old_suffix;
n = saved_after;
}
}
/* Found it! Now generate the new name */
if (r < 0)
return r;
return 1;
}
int dns_name_between(const char *a, const char *b, const char *c) {
int n;
/* Determine if b is strictly greater than a and strictly smaller than c.
We consider the order of names to be circular, so that if a is
strictly greater than c, we consider b to be between them if it is
either greater than a or smaller than c. This is how the canonical
DNS name order used in NSEC records work. */
n = dns_name_compare_func(a, c);
if (n == 0)
return -EINVAL;
else if (n < 0)
/* a<---b--->c */
return dns_name_compare_func(a, b) < 0 &&
dns_name_compare_func(b, c) < 0;
else
/* <--b--c a--b--> */
return dns_name_compare_func(b, c) < 0 ||
dns_name_compare_func(a, b) < 0;
}
const uint8_t *p;
int r;
assert(a);
p = (const uint8_t*) a;
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",
else
return -EAFNOSUPPORT;
if (r < 0)
return -ENOMEM;
return 0;
}
int r;
assert(p);
r = dns_name_endswith(p, "in-addr.arpa");
if (r < 0)
return r;
if (r > 0) {
uint8_t a[4];
unsigned i;
for (i = 0; i < ELEMENTSOF(a); i++) {
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
if (r > 3)
return -EINVAL;
r = safe_atou8(label, &a[i]);
if (r < 0)
return r;
}
r = dns_name_equal(p, "in-addr.arpa");
if (r <= 0)
return r;
(uint32_t) a[0]);
return 1;
}
r = dns_name_endswith(p, "ip6.arpa");
if (r < 0)
return r;
if (r > 0) {
struct in6_addr a;
unsigned i;
for (i = 0; i < ELEMENTSOF(a.s6_addr); i++) {
int x, y;
if (r <= 0)
return r;
if (r != 1)
return -EINVAL;
if (x < 0)
return -EINVAL;
if (r <= 0)
return r;
if (r != 1)
return -EINVAL;
if (y < 0)
return -EINVAL;
}
r = dns_name_equal(p, "ip6.arpa");
if (r <= 0)
return r;
return 1;
}
return 0;
}
bool dns_name_is_root(const char *name) {
/* There are exactly two ways to encode the root domain name:
* as empty string, or with a single dot. */
}
bool dns_name_is_single_label(const char *name) {
int r;
r = dns_name_parent(&name);
if (r <= 0)
return false;
return dns_name_is_root(name);
}
/* Encode a domain name according to RFC 1035 Section 3.1, without compression */
int r;
do {
/* Reserve a byte for label length */
if (len <= 0)
return -ENOBUFS;
len--;
label_length = out;
out++;
/* Convert and copy a single label. Note that
* dns_label_unescape() returns 0 when it hits the end
* of the domain name, which we rely on here to encode
* the trailing NUL byte. */
if (r < 0)
return r;
/* Optionally, output the name in DNSSEC canonical
* format, as described in RFC 4034, section 6.2. Or
* in other words: in lower-case. */
if (canonical)
/* Fill label length, move forward */
*label_length = r;
out += r;
len -= r;
} while (r != 0);
/* Verify the maximum size of the encoded name. The trailing
* dot + NUL byte account are included this time, hence
* compare against DNS_HOSTNAME_MAX + 2 (which is 255) this
* time. */
return -EINVAL;
}
size_t k;
if (n < 2) /* Label needs to be at least 2 chars long */
return false;
return false;
/* Second char must be a letter */
return false;
/* Third and further chars must be alphanumeric or a hyphen */
for (k = 2; k < n; k++) {
label[k] != '-')
return false;
}
return true;
}
bool dns_srv_type_is_valid(const char *name) {
unsigned c = 0;
int r;
if (!name)
return false;
for (;;) {
char label[DNS_LABEL_MAX];
/* This more or less implements RFC 6335, Section 5.1 */
if (r < 0)
return false;
if (r == 0)
break;
if (c >= 2)
return false;
if (!srv_type_label_is_valid(label, r))
return false;
c++;
}
return c == 2; /* exactly two labels */
}
bool dns_service_name_is_valid(const char *name) {
size_t l;
/* This more or less implements RFC 6763, Section 4.1.1 */
if (!name)
return false;
if (!utf8_is_valid(name))
return false;
return false;
if (l <= 0)
return false;
if (l > 63)
return false;
return true;
}
char escaped[DNS_LABEL_ESCAPED_MAX];
_cleanup_free_ char *n = NULL;
int r;
if (!dns_srv_type_is_valid(type))
return -EINVAL;
if (!name)
if (!dns_service_name_is_valid(name))
return -EINVAL;
if (r < 0)
return r;
if (r < 0)
return r;
}
char *s;
return false;
return dns_service_name_is_valid(s);
}
unsigned x = 0;
/* Get first label from the full name */
an = dns_label_unescape(&p, a, sizeof(a));
if (an < 0)
return an;
if (an > 0) {
x++;
/* If there was a first label, try to get the second one */
bn = dns_label_unescape(&p, b, sizeof(b));
if (bn < 0)
return bn;
if (bn > 0) {
x++;
/* If there was a second label, try to get the third one */
q = p;
cn = dns_label_unescape(&p, c, sizeof(c));
if (cn < 0)
return cn;
if (cn > 0)
x++;
} else
cn = 0;
} else
an = 0;
if (dns_service_name_label_is_valid(a, an)) {
/* OK, got <name> . <type> . <type2> . <domain> */
if (!name)
return -ENOMEM;
if (!type)
return -ENOMEM;
d = p;
goto finish;
}
} else if (srv_type_label_is_valid(a, an)) {
/* OK, got <type> . <type2> . <domain> */
if (!type)
return -ENOMEM;
d = q;
goto finish;
}
}
d = joined;
r = dns_name_normalize(d, &domain);
if (r < 0)
return r;
if (_domain) {
}
if (_type) {
}
if (_name) {
}
return 0;
}
const char *p;
unsigned n = 0;
int r;
p = name;
for (;;) {
if (n > DNS_N_LABELS_MAX)
return -EINVAL;
table[n] = p;
r = dns_name_parent(&p);
if (r < 0)
return r;
if (r == 0)
break;
n++;
}
return (int) n;
}
int n;
if (n < 0)
return n;
if ((unsigned) n < n_labels)
return -EINVAL;
return (int) (n - n_labels);
}
int r;
assert(a);
r = dns_name_parent(&a);
if (r < 0)
return r;
if (r == 0) {
*ret = "";
return 0;
}
}
*ret = a;
return 1;
}
int dns_name_count_labels(const char *name) {
unsigned n = 0;
const char *p;
int r;
p = name;
for (;;) {
r = dns_name_parent(&p);
if (r < 0)
return r;
if (r == 0)
break;
if (n >= DNS_N_LABELS_MAX)
return -EINVAL;
n++;
}
return (int) n;
}
int dns_name_equal_skip(const char *a, unsigned n_labels, const char *b) {
int r;
assert(a);
assert(b);
r = dns_name_skip(a, n_labels, &a);
if (r <= 0)
return r;
return dns_name_equal(a, b);
}
int dns_name_common_suffix(const char *a, const char *b, const char **ret) {
int n = 0, m = 0, k = 0, r, q;
assert(a);
assert(b);
/* Determines the common suffix of domain names a and b */
n = dns_name_build_suffix_table(a, a_labels);
if (n < 0)
return n;
m = dns_name_build_suffix_table(b, b_labels);
if (m < 0)
return m;
for (;;) {
const char *x, *y;
if (k >= n || k >= m) {
return 0;
}
x = a_labels[n - 1 - k];
if (r < 0)
return r;
y = b_labels[m - 1 - k];
if (q < 0)
return q;
return 0;
}
k++;
}
}