DhcpServer: implemented GetSanitizedHostName() method to sanitize hostname before using it to generate client domain name.

This commit is contained in:
Shreyas Zare
2024-09-28 16:10:59 +05:30
parent fe0f6de9db
commit 8bd1ec3b75

View File

@@ -28,6 +28,7 @@ using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TechnitiumLibrary.Net.Dns;
@@ -472,7 +473,7 @@ namespace DnsServerCore.Dhcp
string clientDomainName = null;
if (!string.IsNullOrWhiteSpace(reservedLeaseHostName))
clientDomainName = reservedLeaseHostName + "." + scope.DomainName;
clientDomainName = GetSanitizedHostName(reservedLeaseHostName) + "." + scope.DomainName;
if (string.IsNullOrWhiteSpace(clientDomainName))
{
@@ -488,8 +489,8 @@ namespace DnsServerCore.Dhcp
if (string.IsNullOrWhiteSpace(clientDomainName))
{
if ((request.HostName != null) && !string.IsNullOrWhiteSpace(request.HostName.HostName))
clientDomainName = request.HostName.HostName.Replace(' ', '-') + "." + scope.DomainName;
if ((request.HostName is not null) && !string.IsNullOrWhiteSpace(request.HostName.HostName))
clientDomainName = GetSanitizedHostName(request.HostName.HostName) + "." + scope.DomainName;
}
if (!string.IsNullOrWhiteSpace(clientDomainName))
@@ -610,8 +611,8 @@ namespace DnsServerCore.Dhcp
if (string.IsNullOrWhiteSpace(clientDomainName))
{
if (request.HostName != null)
clientDomainName = request.HostName.HostName.Replace(' ', '-') + "." + scope.DomainName;
if (request.HostName is not null)
clientDomainName = GetSanitizedHostName(request.HostName.HostName) + "." + scope.DomainName;
}
if (!string.IsNullOrWhiteSpace(clientDomainName))
@@ -698,6 +699,34 @@ namespace DnsServerCore.Dhcp
}
}
internal static string GetSanitizedHostName(string hostname)
{
StringBuilder sb = new StringBuilder(hostname.Length);
foreach (char c in hostname)
{
if ((c >= 97) && (c <= 122)) //[a-z]
sb.Append(c);
else if ((c >= 65) && (c <= 90)) //[A-Z]
sb.Append(c);
else if ((c >= 48) && (c <= 57)) //[0-9]
sb.Append(c);
else if (c == 45) //[-]
sb.Append(c);
else if (c == 95) //[_]
sb.Append(c);
else if (c == 47) //[/]
sb.Append(c);
else if (c == ' ')
sb.Append('-');
}
if (sb.Length > 63)
sb.Length = 63;
return sb.ToString();
}
private void UpdateDnsAuthZone(bool add, Scope scope, Lease lease)
{
UpdateDnsAuthZone(add, scope, lease.HostName, lease.Address, lease.Type == LeaseType.Reserved);