-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.