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

Adding OSX support for System.DirectoryServices.Protocols #36669

Merged
merged 3 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal static partial class Libraries
internal const string LibSystemCommonCrypto = "/usr/lib/system/libcommonCrypto";
internal const string LibSystemKernel = "/usr/lib/system/libsystem_kernel";
internal const string Odbc32 = "libodbc.2.dylib";
internal const string OpenLdap = "libldap";
internal const string SystemConfigurationLibrary = "/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration";
internal const string AppleCryptoNative = "System.Security.Cryptography.Native.Apple";
internal const string MsQuic = "msquic";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);0649;CA1810</NoWarn>
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
<TargetFrameworks>$(NetCoreAppCurrent)-Linux;netcoreapp2.0-Linux;$(NetCoreAppCurrent)-Windows_NT;netstandard2.0;netcoreapp2.0-Windows_NT;_$(NetFrameworkCurrent)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent)-OSX;netcoreapp2.0-OSX;$(NetCoreAppCurrent)-Linux;netcoreapp2.0-Linux;$(NetCoreAppCurrent)-Windows_NT;netstandard2.0;netcoreapp2.0-Windows_NT;_$(NetFrameworkCurrent)</TargetFrameworks>
<ExcludeCurrentNetCoreAppFromPackage>true</ExcludeCurrentNetCoreAppFromPackage>
</PropertyGroup>
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
Expand Down Expand Up @@ -57,23 +57,30 @@
<Link>Common\Interop\Windows\Wldap32\Interop.Ber.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsLinux)' == 'true'">
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
<Compile Include="System\DirectoryServices\Protocols\common\BerConverter.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\LdapPal.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\BerPal.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\ldap\LdapConnection.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\ldap\LdapSessionOptions.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\SafeHandles.Linux.cs" />
<Compile Include="$(CommonPath)Interop\Linux\Interop.Libraries.cs">
<Link>Common\Interop\Linux\Interop.Libraries.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Linux\OpenLdap\Interop.Ldap.cs">
<Link>Common\Interop\Linux\OpenLdap\Interop.Ldap.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Linux\OpenLdap\Interop.Ber.cs">
<Link>Common\Interop\Linux\OpenLdap\Interop.Ber.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsLinux)' == 'true'">
<Compile Include="$(CommonPath)Interop\Linux\Interop.Libraries.cs">
<Link>Common\Interop\Linux\Interop.Libraries.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsOSX)' == 'true'">
<Compile Include="$(CommonPath)Interop\OSX\Interop.Libraries.cs">
<Link>Common\Interop\OSX\Interop.Libraries.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Reference Include="System.Security.AccessControl" />
<Reference Include="System.Security.Principal.Windows" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public static class DirectoryServicesTestHelpers
// Cache the check once we have performed it once
private static bool? _isLibLdapInstalled = null;

/// <summary>
/// Returns true if able to PInvoke into Linux or OSX, false otherwise
/// </summary>
public static bool IsLibLdapInstalled
{
get
Expand All @@ -24,26 +27,43 @@ public static bool IsLibLdapInstalled
{
try
{
// Attempt PInvoking into libldap
IntPtr handle = ber_alloc(1);
ber_free(handle, 1);
// Attempt PInvoking into libldap on Linux
IntPtr handle = ber_alloc_Linux(1);
ber_free_Linux(handle, 1);
_isLibLdapInstalled = true;
}
catch (Exception)
{
_isLibLdapInstalled = false;
try
{
// Attempt PInvoking into libldap on OSX
IntPtr handle = ber_alloc_OSX(1);
ber_free_OSX(handle, 1);
_isLibLdapInstalled = true;
}
catch (Exception)
{
_isLibLdapInstalled = false;
}
}
}
return _isLibLdapInstalled.Value;
}
}

internal const string OpenLdap = "libldap-2.4.so.2";
internal const string OpenLdapLinux = "libldap-2.4.so.2";
internal const string OpenLdapOSX = "libldap";

[DllImport(OpenLdap, EntryPoint = "ber_alloc_t", CharSet = CharSet.Ansi)]
internal static extern IntPtr ber_alloc(int option);
[DllImport(OpenLdapLinux, EntryPoint = "ber_alloc_t", CharSet = CharSet.Ansi)]
internal static extern IntPtr ber_alloc_Linux(int option);

[DllImport(OpenLdap, EntryPoint = "ber_free", CharSet = CharSet.Ansi)]
public static extern IntPtr ber_free([In] IntPtr berelement, int option);
[DllImport(OpenLdapLinux, EntryPoint = "ber_free", CharSet = CharSet.Ansi)]
public static extern IntPtr ber_free_Linux([In] IntPtr berelement, int option);

[DllImport(OpenLdapOSX, EntryPoint = "ber_alloc_t", CharSet = CharSet.Ansi)]
internal static extern IntPtr ber_alloc_OSX(int option);

[DllImport(OpenLdapOSX, EntryPoint = "ber_free", CharSet = CharSet.Ansi)]
public static extern IntPtr ber_free_OSX([In] IntPtr berelement, int option);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-Windows_NT;$(NetFrameworkCurrent)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="BerConverterTests.cs" />
Expand Down