Skip to content

Commit

Permalink
Adding OSX support for System.DirectoryServices.Protocols (#36669)
Browse files Browse the repository at this point in the history
* Adding OSX support for System.DirectoryServices.Protocols

* Addressing PR Feedback

* Fixing issue in netfx builds where local member is assigned but never used
  • Loading branch information
joperezr authored May 20, 2020
1 parent af2e5c9 commit e9c272c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
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)-Windows_NT;netcoreapp2.0-Windows_NT;$(NetCoreAppCurrent)-OSX;netcoreapp2.0-OSX;$(NetCoreAppCurrent)-Linux;netcoreapp2.0-Linux;netstandard2.0;_$(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,34 +16,31 @@ 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
{
#if NETCOREAPP
if (!_isLibLdapInstalled.HasValue)
{
try
if (PlatformDetection.IsOSX)
{
// Attempt PInvoking into libldap
IntPtr handle = ber_alloc(1);
ber_free(handle, 1);
_isLibLdapInstalled = true;
_isLibLdapInstalled = NativeLibrary.TryLoad("libldap.dylib", out _);
}
catch (Exception)
else
{
_isLibLdapInstalled = false;
_isLibLdapInstalled = NativeLibrary.TryLoad("libldap-2.4.so.2", out _);
}
}
return _isLibLdapInstalled.Value;
#else
_isLibLdapInstalled = true; // In .NET Framework ldap is always installed.
return _isLibLdapInstalled.Value;
#endif
}
}

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

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

[DllImport(OpenLdap, EntryPoint = "ber_free", CharSet = CharSet.Ansi)]
public static extern IntPtr ber_free([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

0 comments on commit e9c272c

Please sign in to comment.