diff options
author | Luca Bruno <luca.bruno@coreos.com> | 2017-12-13 17:00:46 +0000 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2017-12-13 18:00:46 +0100 |
commit | 2de2abad62d4f23f7d9af0c3871f9955896d085c (patch) | |
tree | d449365dfbc974b187caf7cac3c493f698bcc4be /src/basic/hostname-util.c | |
parent | 3ce5a5df2cfb570ee30b86d1dd47f6cea452115a (diff) | |
download | systemd-2de2abad62d4f23f7d9af0c3871f9955896d085c.tar.gz |
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.
Diffstat (limited to 'src/basic/hostname-util.c')
-rw-r--r-- | src/basic/hostname-util.c | 32 |
1 files changed, 32 insertions, 0 deletions
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; |