From 881f66b0c2526bff6a2a51b30d314c512df813b4 Mon Sep 17 00:00:00 2001 From: Hans Dedecker Date: Wed, 22 Aug 2018 11:45:53 +0200 Subject: odhcpd: detect broken hostnames Check hostnames contain valid characters as defined in RFC 952 and RFC 1123. Invalid hostnames in uci configured host entries will result into a refusal to create the static lease. In case a client received hostname contains an invalid character no entry will be added to the lease file. In such case the leaseinfo description in the lease file will still contain the hostname but preceded by the string broken\x20 Signed-off-by: Hans Dedecker --- src/odhcpd.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/odhcpd.c') diff --git a/src/odhcpd.c b/src/odhcpd.c index fe4dd73..c1d51e7 100644 --- a/src/odhcpd.c +++ b/src/odhcpd.c @@ -581,3 +581,38 @@ bool odhcpd_bitlen2netmask(bool inet6, unsigned int bits, void *mask) return true; } + +bool odhcpd_valid_hostname(const char *name) +{ +#define MAX_LABEL 63 + const char *c, *label, *label_end; + int label_sz = 0; + + for (c = name, label_sz = 0, label = name, label_end = name + strcspn(name, ".") - 1; + *c && label_sz <= MAX_LABEL; c++) { + if ((*c >= '0' && *c <= '9') || + (*c >= 'A' && *c <= 'Z') || + (*c >= 'a' && *c <= 'z')) { + label_sz++; + continue; + } + + if ((*c == '_' || *c == '-') && c != label && c != label_end) { + label_sz++; + continue; + } + + if (*c == '.') { + if (*(c + 1)) { + label = c + 1; + label_end = label + strcspn(label, ".") - 1; + label_sz = 0; + } + continue; + } + + return false; + } + + return (label_sz && label_sz <= MAX_LABEL ? true : false); +} -- cgit v1.2.3