dns-domain.c revision 58db254ade4fb2ef77de68f28c4f13814819f6a1
/*-*- 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 "alloc-util.h"
#include "dns-domain.h"
#include "hexdecoct.h"
#include "parse-util.h"
#include "string-util.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 (sz <= 0)
return -ENOSPC;
if (r >= DNS_LABEL_MAX)
return -EINVAL;
if (*n == '\\') {
/* Escaped character */
n++;
if (*n == 0)
/* Ending NUL */
return -EINVAL;
else if (*n == '\\' || *n == '.') {
/* Escaped backslash or dot */
*(d++) = *(n++);
sz--;
r++;
} 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 CC characters or anything that doesn't fit in 8bit */
if (k < ' ' || k > 255 || k == 127)
return -EINVAL;
*(d++) = (char) k;
sz--;
r++;
n += 3;
} else
return -EINVAL;
/* Normal character */
*(d++) = *(n++);
sz--;
r++;
} else
return -EINVAL;
}
/* Empty label that is not at the end? */
if (r == 0 && *n)
return -EINVAL;
if (sz >= 1)
*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 */
/* 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;
}
_cleanup_free_ char *s = NULL;
char *q;
int r;
assert(p);
if (l > DNS_LABEL_MAX)
return -EINVAL;
if (!s)
return -ENOMEM;
q = s;
while (l > 0) {
if (*p == '.' || *p == '\\') {
/* Dot or backslash */
*(q++) = '\\';
*(q++) = *p;
} else if (*p == '_' ||
*p == '-' ||
(*p >= '0' && *p <= '9') ||
(*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z')) {
/* Proper character */
*(q++) = *p;
/* Everything else */
*(q++) = '\\';
} else
return -EINVAL;
p++;
l--;
}
*q = 0;
*ret = s;
r = q - 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
const char *p;
bool contains_8bit = false;
if (encoded_size <= 0)
return 0;
if ((uint8_t) *p > 127)
contains_8bit = true;
if (!contains_8bit)
return 0;
if (!input)
return -ENOMEM;
return -EINVAL;
#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 */
return 0;
return 0;
if (!input)
return -ENOMEM;
if (!result)
return -ENOMEM;
if (w <= 0)
return 0;
if (w+1 > decoded_max)
return -EINVAL;
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 (;;) {
_cleanup_free_ char *t = NULL;
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;
r = dns_label_escape(label, r, &t);
if (r < 0)
return r;
if (_ret) {
return -ENOMEM;
if (!first)
ret[n++] = '.';
else
first = false;
}
n += r;
}
if (n > DNS_NAME_MAX)
return -EINVAL;
if (_ret) {
return -ENOMEM;
ret[n] = 0;
}
return 0;
}
const char *p = s;
int r;
assert(p);
while (*p) {
int k;
if (r < 0)
break;
if (k < 0)
break;
if (k > 0)
r = k;
if (r == 0)
break;
label[r] = 0;
}
/* 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 (k < 0 || w < 0)
return k - w;
if (k > 0)
r = k;
if (w > 0)
r = w;
if (r != 0)
return r;
}
}
const struct hash_ops dns_name_hash_ops = {
};
int dns_name_equal(const char *x, const char *y) {
int r, q, k, w;
assert(x);
assert(y);
for (;;) {
if (*x == 0 && *y == 0)
return true;
if (r < 0)
return r;
if (k < 0)
return k;
if (k > 0)
r = k;
if (q < 0)
return q;
if (w < 0)
return w;
if (w > 0)
q = w;
return false;
}
}
int r, q, k, w;
n = name;
s = suffix;
for (;;) {
if (r < 0)
return r;
if (k < 0)
return k;
if (k > 0)
r = k;
if (!saved_n)
saved_n = n;
if (q < 0)
return q;
if (w < 0)
return w;
if (w > 0)
q = w;
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;
}
}
}
int dns_name_change_suffix(const char *name, const char *old_suffix, const char *new_suffix, char **ret) {
int r, q, k, w;
n = name;
s = old_suffix;
for (;;) {
if (!saved_before)
saved_before = n;
if (r < 0)
return r;
if (k < 0)
return k;
if (k > 0)
r = k;
if (!saved_after)
saved_after = n;
if (q < 0)
return q;
if (w < 0)
return w;
if (w > 0)
q = w;
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;
}
int dns_name_root(const char *name) {
int r;
if (r < 0)
return r;
return r == 0 && *name == 0;
}
int dns_name_single_label(const char *name) {
int r;
if (r < 0)
return r;
if (r == 0)
return 0;
if (r < 0)
return r;
return r == 0 && *name == 0;
}
/* Encode a domain name according to RFC 1035 Section 3.1 */
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 */
if (r < 0)
return r;
/* fill label length, move forward */
*label_length = r;
out += r;
len -= r;
} while (r != 0);
}
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;
}
int dns_srv_type_verify(const char *name) {
unsigned c = 0;
int r;
if (!name)
return 0;
for (;;) {
char label[DNS_LABEL_MAX];
/* This more or less implements RFC 6335, Section 5.1 */
if (r == -EINVAL)
return 0;
if (r < 0)
return r;
if (r == 0)
break;
if (c >= 2)
return 0;
if (!srv_type_label_is_valid(label, r))
return 0;
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;
}
int r;
if (!dns_srv_type_verify(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;
}