From 5fa479c5d3e5c820bb2a3ba89e86f0aeea39db1a Mon Sep 17 00:00:00 2001 From: Valentyn Bohdan Date: Mon, 4 Nov 2024 14:19:38 +0100 Subject: [PATCH] F OpenNebula/one#6712: Add extra check for a hostname (#3276) Changes have been made according to RFC 1034. http://www.faqs.org/rfcs/rfc1034.html (Sections 3.1. Name space specifications and terminology) --- src/host/HostPool.cc | 49 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/host/HostPool.cc b/src/host/HostPool.cc index 61a6701e373..b63db8b3eef 100644 --- a/src/host/HostPool.cc +++ b/src/host/HostPool.cc @@ -32,6 +32,53 @@ using namespace std; +// Reference +// https://learn.microsoft.com/en-us/troubleshoot/windows-server/active-directory/naming-conventions-for-computer-domain-site-ou +static bool hostname_is_valid(const string& hostname, string& error_str) +{ + if (hostname.size() < 2 || hostname.size() > 63) + { + error_str = "Invalid HOSTNAME, HOSTNAME length should be greater than 1, but smaller than 64 characters"; + return false; + } + + const auto firstChar = hostname.front(); + + if (firstChar == '-' || firstChar == '.' || firstChar == '_') + { + std::stringstream ss; + ss << "Invalid HOSTNAME, first character can't be '" << firstChar << "'"; + + error_str = ss.str(); + return false; + } + + for (const auto ch : hostname) + { + if (!std::isalnum(ch) && ch != '-' && ch != '.' && ch != '_') + { + std::stringstream ss; + ss << "Invalid HOSTNAME, '" << ch << "' is invalid character for a HOSTNAME"; + + error_str = ss.str(); + return false; + } + } + + const auto lastChar = hostname.back(); + + if (lastChar == '-' || lastChar == '.' || lastChar == '_') + { + std::stringstream ss; + ss << "Invalid HOSTNAME, last character can't be '" << lastChar << "'"; + + error_str = ss.str(); + return false; + } + + return true; +} + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -58,7 +105,7 @@ int HostPool::allocate ( int db_oid; - if ( !PoolObjectSQL::name_is_valid(hostname, error_str) ) + if ( !hostname_is_valid(hostname, error_str) || !PoolObjectSQL::name_is_valid(hostname, error_str) ) { goto error_name; }