diff options
Diffstat (limited to 'src/settings/nm-settings.c')
| -rw-r--r-- | src/settings/nm-settings.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index e200b83c..71009a0c 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -1286,6 +1286,33 @@ done: nm_auth_chain_unref (chain); } +static gboolean +validate_hostname (const char *hostname) +{ + const char *p; + gboolean dot = TRUE; + + if (!hostname || !hostname[0]) + return FALSE; + + for (p = hostname; *p; p++) { + if (*p == '.') { + if (dot) + return FALSE; + dot = TRUE; + } else { + if (!g_ascii_isalnum (*p) && (*p != '-') && (*p != '_')) + return FALSE; + dot = FALSE; + } + } + + if (dot) + return FALSE; + + return (p - hostname <= HOST_NAME_MAX); +} + static void impl_settings_save_hostname (NMSettings *self, const char *hostname, @@ -1295,6 +1322,16 @@ impl_settings_save_hostname (NMSettings *self, NMAuthChain *chain; GError *error = NULL; + /* Minimal validation of the hostname */ + if (!validate_hostname (hostname)) { + error = g_error_new_literal (NM_SETTINGS_ERROR, + NM_SETTINGS_ERROR_HOSTNAME_INVALID, + "The hostname was too long or contained invalid characters."); + dbus_g_method_return_error (context, error); + g_error_free (error); + return; + } + /* Do any of the plugins support setting the hostname? */ if (!get_plugin (self, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME)) { error = g_error_new_literal (NM_SETTINGS_ERROR, |