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

Register a callback that tries to load other versions Open LDAP library if the default is failed #88851

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
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 @@ -9,7 +9,7 @@ internal static partial class Libraries
internal const string Liblog = "liblog";

internal const string Odbc32 = "libodbc.so.2";
internal const string OpenLdap = "libldap-2.4.so.2";
internal const string OpenLdap = "libldap-2.5.so.0";
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
internal const string MsQuic = "libmsquic.so";
}
}
24 changes: 23 additions & 1 deletion src/libraries/Common/src/Interop/Linux/OpenLdap/Interop.Ldap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using System.DirectoryServices.Protocols;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;

namespace System.DirectoryServices.Protocols
{
Expand Down Expand Up @@ -67,6 +69,26 @@ internal static partial class Ldap
{
static Ldap()
{
Assembly currentAssembly = typeof(Ldap).Assembly;

// Register callback that tries to load other libraries when the default library "libldap-2.5.so.0" not found
AssemblyLoadContext.GetLoadContext(currentAssembly).ResolvingUnmanagedDll += (assembly, ldapName) =>
{
if (assembly != currentAssembly || ldapName != Libraries.OpenLdap)
{
return IntPtr.Zero;
}

// Try load next (libldap-2.6.so.0) or previous (libldap-2.4.so.2) versions
if (NativeLibrary.TryLoad("libldap-2.6.so.0", out IntPtr handle) ||
NativeLibrary.TryLoad("libldap-2.4.so.2", out handle))
{
return handle;
}

return IntPtr.Zero;
};

// OpenLdap must be initialized on a single thread, once this is done it allows concurrent calls
// By doing so in the static constructor we guarantee this is run before any other methods are called.

Expand Down