Skip to content

Commit

Permalink
F #6712: Add extra check for a hostname (#3276)
Browse files Browse the repository at this point in the history
Changes have been made according to RFC 1034.
http://www.faqs.org/rfcs/rfc1034.html (Sections 3.1. Name space specifications and terminology)
  • Loading branch information
vvbohdan authored Nov 4, 2024
1 parent bfb6ff3 commit 5fa479c
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/host/HostPool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

Expand All @@ -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;
}
Expand Down

0 comments on commit 5fa479c

Please sign in to comment.