Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Avoid exception when parsing AD path for port number #109977

Merged
merged 1 commit into from
Nov 22, 2024
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another issue from the original PR - it seems odd to me that we ignore this scheme and instead use LDAP below with a port. Do you know if that's intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. should we use "LDAPS://" below when ldapUri.Scheme == "LDAPS"? Would the connection bel secure \ encrypted just because it's on the LDAPS port even though "LDAP://" is used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been like this from day one. After digging a little deeper and talking with my team, I found out that LDAPS is not valid provider name supported. Hence it uses LDAP as the supported provider name. This allows the user to call the api with LDAPS and the api in turn uses the ssl port(636) along with the supported provider name which LDAP. This is a less strict way to call the api and allow ssl port. Other way would not allow LDAPS at all which might break a lot of applications.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth a comment?

{
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it wasn't a reported problem, but double checking we are OK to add the default port values here when not specified by the user? Was that an important part of the original fix?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this code only interacts with a dc it is safe to use default ports, ie., 389 for regular LDAP and 636 for secure version of it.

{
this.defaultNamingContext = (string)rootDse.Properties["defaultNamingContext"][0];
this.contextBasePartitionDN = this.defaultNamingContext;
Expand Down
Loading