diff --git a/src/libraries/Common/src/Interop/Interop.Ldap.cs b/src/libraries/Common/src/Interop/Interop.Ldap.cs index cebf05d52b3f1c..fd12086b6780b5 100644 --- a/src/libraries/Common/src/Interop/Interop.Ldap.cs +++ b/src/libraries/Common/src/Interop/Interop.Ldap.cs @@ -162,7 +162,8 @@ internal enum LdapOption LDAP_OPT_X_TLS_NEWCTX = 0x600F, // Not Supported in Windows LDAP_OPT_X_SASL_REALM = 0x6101, LDAP_OPT_X_SASL_AUTHCID = 0x6102, - LDAP_OPT_X_SASL_AUTHZID = 0x6103 + LDAP_OPT_X_SASL_AUTHZID = 0x6103, + LDAP_OPT_X_SASL_NOCANON = 0x610b, } internal enum ResultAll diff --git a/src/libraries/Common/src/Interop/Linux/OpenLdap/Interop.Ldap.cs b/src/libraries/Common/src/Interop/Linux/OpenLdap/Interop.Ldap.cs index dcb97ac0d64f65..79c477af1a7ddf 100644 --- a/src/libraries/Common/src/Interop/Linux/OpenLdap/Interop.Ldap.cs +++ b/src/libraries/Common/src/Interop/Linux/OpenLdap/Interop.Ldap.cs @@ -151,7 +151,7 @@ public static partial int ldap_search( ref int messageNumber); [LibraryImport(Libraries.OpenLdap, EntryPoint = "ldap_set_option", SetLastError = true)] - public static partial int ldap_set_option_bool(ConnectionHandle ld, LdapOption option, [MarshalAs(UnmanagedType.Bool)] bool value); + public static partial int ldap_set_option_bool(ConnectionHandle ld, LdapOption option, IntPtr value); [LibraryImport(Libraries.OpenLdap, EntryPoint = "ldap_set_option")] public static partial int ldap_set_option_clientcert(ConnectionHandle ldapHandle, LdapOption option, QUERYCLIENTCERT outValue); diff --git a/src/libraries/System.DirectoryServices.Protocols/ref/System.DirectoryServices.Protocols.cs b/src/libraries/System.DirectoryServices.Protocols/ref/System.DirectoryServices.Protocols.cs index 9363d38c7cc5d0..0d8779689ea5f4 100644 --- a/src/libraries/System.DirectoryServices.Protocols/ref/System.DirectoryServices.Protocols.cs +++ b/src/libraries/System.DirectoryServices.Protocols/ref/System.DirectoryServices.Protocols.cs @@ -381,6 +381,9 @@ public partial class LdapSessionOptions { internal LdapSessionOptions() { } public bool AutoReconnect { get { throw null; } set { } } +#if NET + public bool CanonicalizeHostName { get { throw null; } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("windows")] set { } } +#endif public string DomainName { get { throw null; } set { } } #if NET [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("windows")] diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs index 3deef347af04b5..8862cff75062de 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs @@ -98,7 +98,7 @@ internal static int SearchDirectory(ConnectionHandle ldapHandle, string dn, int return Interop.Ldap.ldap_search(ldapHandle, dn, scope, filter, attributes, attributeOnly, servercontrol, clientcontrol, searchTimeout, sizelimit, ref messageNumber); } - internal static int SetBoolOption(ConnectionHandle ld, LdapOption option, bool value) => Interop.Ldap.ldap_set_option_bool(ld, option, value); + internal static int SetBoolOption(ConnectionHandle ld, LdapOption option, bool value) => Interop.Ldap.ldap_set_option_bool(ld, option, value ? (IntPtr)1 : IntPtr.Zero); // This option is not supported in Linux, so it would most likely throw. internal static int SetClientCertOption(ConnectionHandle ldapHandle, LdapOption option, QUERYCLIENTCERT outValue) => Interop.Ldap.ldap_set_option_clientcert(ldapHandle, option, outValue); diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Linux.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Linux.cs index ce6f5d28326626..d0a70ef3d3e957 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Linux.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Linux.cs @@ -13,6 +13,13 @@ public partial class LdapSessionOptions private bool _secureSocketLayer; + public bool CanonicalizeHostName + { + get => !GetBoolValueHelper(LdapOption.LDAP_OPT_X_SASL_NOCANON); + [UnsupportedOSPlatform("windows")] + set => SetBoolValueHelper(LdapOption.LDAP_OPT_X_SASL_NOCANON, !value); + } + /// /// Specifies the path of the directory containing CA certificates in the PEM format. /// Multiple directories may be specified by separating with a semi-colon. diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Windows.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Windows.cs index 6eef213d3fa4ce..83fe9192f09e80 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Windows.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Windows.cs @@ -10,6 +10,13 @@ public partial class LdapSessionOptions { private static void PALCertFreeCRLContext(IntPtr certPtr) => Interop.Ldap.CertFreeCRLContext(certPtr); + public bool CanonicalizeHostName + { + get => true; + [UnsupportedOSPlatform("windows")] + set => throw new PlatformNotSupportedException(); + } + [UnsupportedOSPlatform("windows")] public string TrustedCertificatesDirectory { diff --git a/src/libraries/System.DirectoryServices.Protocols/tests/LdapSessionOptionsTests.cs b/src/libraries/System.DirectoryServices.Protocols/tests/LdapSessionOptionsTests.cs index 2a8ab23a16d421..4ed89bcd71d133 100644 --- a/src/libraries/System.DirectoryServices.Protocols/tests/LdapSessionOptionsTests.cs +++ b/src/libraries/System.DirectoryServices.Protocols/tests/LdapSessionOptionsTests.cs @@ -785,6 +785,33 @@ public void CertificateDirectoryProperty_ThrowsPlatformNotSupportedException() Assert.Throws(() => options.TrustedCertificatesDirectory = "CertificateDirectory"); } } + + [Fact] + [PlatformSpecific(TestPlatforms.Windows)] + public void CanonicalizeHostName_GetReturnsTrue_SetThrowsPlatformNotSupportedException() + { + using (var connection = new LdapConnection("server")) + { + LdapSessionOptions options = connection.SessionOptions; + Assert.True(options.CanonicalizeHostName); + Assert.Throws(() => options.CanonicalizeHostName = false); + } + } + + [Fact] + [PlatformSpecific(TestPlatforms.Linux)] + public void CanonicalizeHostName_GetSet_ReturnsExpected() + { + using (var connection = new LdapConnection("server")) + { + LdapSessionOptions options = connection.SessionOptions; + Assert.True(options.CanonicalizeHostName); + options.CanonicalizeHostName = false; + Assert.False(options.CanonicalizeHostName); + options.CanonicalizeHostName = true; + Assert.True(options.CanonicalizeHostName); + } + } #endif } }