Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[release/9.0-staging] Avoid exception when parsing AD path for port number #110224

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,9 @@ protected enum StoreCapabilityMap
// Must be called inside of lock(domainInfoLock)
protected virtual void LoadDomainInfo()
{
const int LdapDefaultPort = 389;
const int LdapsDefaultPort = 636;

GlobalDebug.WriteLineIf(GlobalDebug.Info, "ADStoreCtx", "LoadComputerInfo");

Debug.Assert(this.ctxBase != null);
Expand All @@ -2418,12 +2421,22 @@ protected virtual void LoadDomainInfo()
this.dnsHostName = ADUtils.GetServerName(this.ctxBase);

// Pull the requested port number
Uri ldapUri = new Uri(this.ctxBase.Path);
int port = ldapUri.Port != -1 ? ldapUri.Port : (ldapUri.Scheme.ToUpperInvariant() == "LDAPS" ? 636 : 389);
int port = LdapDefaultPort;
if (Uri.TryCreate(ctxBase.Path, UriKind.Absolute, out Uri ldapUri))
{
if (ldapUri.Port != -1)
{
port = ldapUri.Port;
}
else if (string.Equals(ldapUri.Scheme, "LDAPS", StringComparison.OrdinalIgnoreCase))
{
port = LdapsDefaultPort;
}
}

string dnsDomainName = "";

using (DirectoryEntry rootDse = new DirectoryEntry("LDAP://" + this.dnsHostName + ":" + port + "/rootDse", "", "", AuthenticationTypes.Anonymous))
using (DirectoryEntry rootDse = new DirectoryEntry($"LDAP://{this.dnsHostName}:{port}/rootDse", "", "", AuthenticationTypes.Anonymous))
{
this.defaultNamingContext = (string)rootDse.Properties["defaultNamingContext"][0];
this.contextBasePartitionDN = this.defaultNamingContext;
Expand Down
Loading