From 2de2abad62d4f23f7d9af0c3871f9955896d085c Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Wed, 13 Dec 2017 17:00:46 +0000 Subject: networkd/dhcp: shorten overlong hostname (#7616) This commit updates networkd behavior to check if the hostname option received via DHCP is too long for Linux limit, and in case shorten it. An overlong hostname will be truncated to the first dot or to `HOST_MAX_LEN`, whatever comes earlier. --- src/basic/hostname-util.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/basic/hostname-util.c') diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 12a579b38a..b59e5425a5 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -221,6 +221,38 @@ int sethostname_idempotent(const char *s) { return 1; } +int shorten_overlong(const char *s, char **ret) { + char *h, *p; + + /* Shorten an overlong name to HOST_NAME_MAX or to the first dot, + * whatever comes earlier. */ + + assert(s); + + h = strdup(s); + if (!h) + return -ENOMEM; + + if (hostname_is_valid(h, false)) { + *ret = h; + return 0; + } + + p = strchr(h, '.'); + if (p) + *p = 0; + + strshorten(h, HOST_NAME_MAX); + + if (!hostname_is_valid(h, false)) { + free(h); + return -EDOM; + } + + *ret = h; + return 1; +} + int read_etc_hostname_stream(FILE *f, char **ret) { int r; -- cgit v1.2.1