summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBeniamino Galvani <bgalvani@redhat.com>2022-03-04 08:10:17 +0100
committerBeniamino Galvani <bgalvani@redhat.com>2022-03-09 08:34:26 +0100
commit59f57e8a0be1cb2633525160be4a0b804d7769ef (patch)
tree88578ce04953dfc5b5e7afd8058f60d6d653fed4
parent949870224283ad00073c1685de1568c683467424 (diff)
downloadNetworkManager-59f57e8a0be1cb2633525160be4a0b804d7769ef.tar.gz
core: shorten hostname when too long
If the hostname received via DHCP is too long, shorten it.
-rw-r--r--src/core/nm-policy.c50
1 files changed, 35 insertions, 15 deletions
diff --git a/src/core/nm-policy.c b/src/core/nm-policy.c
index bfc7d1e638..c6ecfa4bc7 100644
--- a/src/core/nm-policy.c
+++ b/src/core/nm-policy.c
@@ -75,8 +75,9 @@ typedef struct {
guint schedule_activate_all_id; /* idle handler for schedule_activate_all(). */
NMPolicyHostnameMode hostname_mode;
- char *orig_hostname; /* hostname at NM start time */
- char *cur_hostname; /* hostname we want to assign */
+ char *orig_hostname; /* hostname at NM start time */
+ char *cur_hostname; /* hostname we want to assign */
+ char *cur_hostname_full; /* similar to @last_hostname, but before shortening */
char *
last_hostname; /* last hostname NM set (to detect if someone else changed it in the meanwhile) */
@@ -560,6 +561,7 @@ _set_hostname(NMPolicy *self, const char *new_hostname, const char *msg)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
gs_free char *old_hostname = NULL;
+ gboolean cur_hostname_full_changed;
const char *name;
/* The incoming hostname *can* be NULL, which will get translated to
@@ -568,13 +570,36 @@ _set_hostname(NMPolicy *self, const char *new_hostname, const char *msg)
* there was no valid hostname to start with.
*/
+ if (nm_strdup_reset(&priv->cur_hostname_full, new_hostname)) {
+ gs_free char *shortened = NULL;
+
+ cur_hostname_full_changed = TRUE;
+
+ if (priv->cur_hostname_full
+ && !nm_utils_shorten_hostname(priv->cur_hostname_full, &shortened)) {
+ _LOGW(LOGD_DNS,
+ "set-hostname: hostname '%s' %s is invalid",
+ priv->cur_hostname_full,
+ msg);
+ return;
+ }
+
+ if (shortened) {
+ _LOGI(LOGD_DNS,
+ "set-hostname: shortened hostname %s from '%s' to '%s'",
+ msg,
+ priv->cur_hostname_full,
+ shortened);
+ nm_strdup_reset_take(&priv->cur_hostname, g_steal_pointer(&shortened));
+ } else
+ nm_strdup_reset(&priv->cur_hostname, priv->cur_hostname_full);
+ } else
+ cur_hostname_full_changed = FALSE;
+
/* Update the DNS only if the hostname is actually
* going to change.
*/
- if (!nm_streq0(priv->cur_hostname, new_hostname)) {
- g_free(priv->cur_hostname);
- priv->cur_hostname = g_strdup(new_hostname);
-
+ if (cur_hostname_full_changed) {
/* Notify the DNS manager of the hostname change so that the domain part, if
* present, can be added to the search list. Set the @updating_dns flag
* so that dns_config_changed() doesn't try again to restart DNS lookup.
@@ -587,13 +612,8 @@ _set_hostname(NMPolicy *self, const char *new_hostname, const char *msg)
}
/* Finally, set kernel hostname */
- if (!new_hostname)
- name = FALLBACK_HOSTNAME4;
- else if (!new_hostname[0]) {
- g_warn_if_reached();
- name = FALLBACK_HOSTNAME4;
- } else
- name = new_hostname;
+ nm_assert(!priv->cur_hostname || priv->cur_hostname[0]);
+ name = priv->cur_hostname ?: FALLBACK_HOSTNAME4;
/* Don't set the hostname if it isn't actually changing */
if ((old_hostname = _get_hostname(self)) && (nm_streq(name, old_hostname))) {
@@ -602,8 +622,7 @@ _set_hostname(NMPolicy *self, const char *new_hostname, const char *msg)
}
/* Keep track of the last set hostname */
- g_free(priv->last_hostname);
- priv->last_hostname = g_strdup(name);
+ nm_strdup_reset(&priv->last_hostname, name);
priv->changing_hostname = TRUE;
_LOGI(LOGD_DNS, "set-hostname: set hostname to '%s' (%s)", name, msg);
@@ -2895,6 +2914,7 @@ dispose(GObject *object)
nm_clear_g_free(&priv->orig_hostname);
nm_clear_g_free(&priv->cur_hostname);
+ nm_clear_g_free(&priv->cur_hostname_full);
nm_clear_g_free(&priv->last_hostname);
if (priv->hostname_manager) {