dns-domain.c revision ae72b22c40569de7dba810073ea9da299491ef60
/*-*- 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 "dns-domain.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_normalize(const char *s, char **_ret) {
const char *p = s;
bool first = true;
int r;
assert(s);
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;
break;
}
if (k < 0)
return k;
if (k > 0)
r = k;
r = dns_label_escape(label, r, &t);
if (r < 0)
return r;
return -ENOMEM;
if (!first)
ret[n++] = '.';
else
first = false;
n += r;
}
if (n > DNS_NAME_MAX)
return -EINVAL;
return -ENOMEM;
ret[n] = 0;
if (_ret) {
}
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;
label[r] = 0;
}
return ul;
}
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_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;
}