diff options
author | Hans Dedecker <dedeckeh@gmail.com> | 2018-08-22 11:45:53 +0200 |
---|---|---|
committer | Hans Dedecker <hans.dedecker@technicolor.com> | 2018-09-05 10:35:42 +0200 |
commit | 881f66b0c2526bff6a2a51b30d314c512df813b4 (patch) | |
tree | 234850b1775f67884468265dd15485881d69cdbb /src/odhcpd.c | |
parent | 3e17fd926ff0f8feae97ff7c86dd5ce282315490 (diff) |
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
<hostname> <IP address> 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 <dedeckeh@gmail.com>
Diffstat (limited to 'src/odhcpd.c')
-rw-r--r-- | src/odhcpd.c | 35 |
1 files changed, 35 insertions, 0 deletions
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); +} |