summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBeniamino Galvani <bgalvani@redhat.com>2020-11-11 22:59:39 +0100
committerBeniamino Galvani <bgalvani@redhat.com>2020-11-11 23:06:16 +0100
commit02b224d0e1e15d82881997c0503dabe232209a7e (patch)
treeb89a092fbcfa3a30887ef639c0000744e43509d5
parent344d190b2c842fb72291247af43820c8cb95f474 (diff)
downloadNetworkManager-bg/dhcp-shorten-hostname.tar.gz
core: shorten the hostname from DHCP when necessarybg/dhcp-shorten-hostname
The hostname received in option 12 can be a FQDN longer than the maximum allowed by kernel (64 characters). Change the code in NMPolicy to gracefully handle such hostnames by truncating them to the first dot or to the maximum length, whatever comes earlier. https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/572
-rw-r--r--src/nm-policy.c26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/nm-policy.c b/src/nm-policy.c
index 60f5fb024b..f96153bdbd 100644
--- a/src/nm-policy.c
+++ b/src/nm-policy.c
@@ -744,9 +744,18 @@ update_system_hostname(NMPolicy *self, const char *msg)
if (dhcp_config) {
dhcp_hostname = nm_dhcp_config_get_option(dhcp_config, "host_name");
if (dhcp_hostname && dhcp_hostname[0]) {
+ gs_free char *shortened = NULL;
+
p = nm_str_skip_leading_spaces(dhcp_hostname);
- if (p[0]) {
- _set_hostname(self, p, "from DHCPv4");
+ if (nm_utils_shorten_hostname(p, &shortened)) {
+ if (shortened) {
+ _LOGW(LOGD_DNS,
+ "set-hostname: DHCPv4-provided hostname '%s' is too long, shortened "
+ "to '%s'",
+ p,
+ shortened);
+ }
+ _set_hostname(self, shortened ?: p, "from DHCPv4");
priv->dhcp_hostname = TRUE;
return;
}
@@ -763,9 +772,18 @@ update_system_hostname(NMPolicy *self, const char *msg)
if (dhcp_config) {
dhcp_hostname = nm_dhcp_config_get_option(dhcp_config, "fqdn_fqdn");
if (dhcp_hostname && dhcp_hostname[0]) {
+ gs_free char *shortened = NULL;
+
p = nm_str_skip_leading_spaces(dhcp_hostname);
- if (p[0]) {
- _set_hostname(self, p, "from DHCPv6");
+ if (nm_utils_shorten_hostname(p, &shortened)) {
+ if (shortened) {
+ _LOGW(LOGD_DNS,
+ "set-hostname: DHCPv6-provided hostname '%s' is too long, shortened "
+ "to '%s'",
+ p,
+ shortened);
+ }
+ _set_hostname(self, shortened ?: p, "from DHCPv6");
priv->dhcp_hostname = TRUE;
return;
}