Skip to content

Commit c0dfcfd

Browse files
rzikmam11
andauthored
Use managed ntlm on linux-bionic (#95274)
* Use managed ntlm on linux-bionic * Fix failing unit test * Fix compilation * Enable more tests on ubuntu-bionic * Change runtime identifier check to regex * Revert "Change runtime identifier check to regex" This reverts commit 82c1136. * add hyphen to startswith * Update src/libraries/Common/tests/System/Net/Capability.Security.Unix.cs Co-authored-by: Adeel Mujahid <[email protected]> --------- Co-authored-by: Adeel Mujahid <[email protected]>
1 parent 2046432 commit c0dfcfd

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.GssApiException.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,19 @@ private static string GetGssApiDisplayStatus(Status majorStatus, Status minorSta
7171

7272
private static string? GetGssApiDisplayStatus(Status status, bool isMinor)
7373
{
74+
if (!System.Net.NegotiateAuthenticationPal.HasSystemNetSecurityNative)
75+
{
76+
// avoid calling into libSystem.Net.Security.Native.
77+
return null;
78+
}
79+
7480
GssBuffer displayBuffer = default(GssBuffer);
7581

7682
try
7783
{
7884
Interop.NetSecurityNative.Status minStat;
7985
Interop.NetSecurityNative.Status displayCallStatus = isMinor ?
80-
DisplayMinorStatus(out minStat, status, ref displayBuffer):
86+
DisplayMinorStatus(out minStat, status, ref displayBuffer) :
8187
DisplayMajorStatus(out minStat, status, ref displayBuffer);
8288
return (Status.GSS_S_COMPLETE != displayCallStatus) ? null : Marshal.PtrToStringUTF8(displayBuffer._data);
8389
}
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Runtime.InteropServices;
5+
46
namespace System.Net.Test.Common
57
{
68
public static partial class Capability
79
{
810
public static bool IsNtlmInstalled()
911
{
10-
// GSS on Linux does not work with OpenSSL 3.0. Fix was submitted to gss-ntlm but it will take a while to make to
11-
// all supported distributions. The second part of the check should be removed when it does.
12-
return Interop.NetSecurityNative.IsNtlmInstalled() && (!PlatformDetection.IsOpenSslSupported || PlatformDetection.OpenSslVersion.Major < 3);
12+
return
13+
// Linux bionic uses managed NTLM implementation
14+
(OperatingSystem.IsLinux() && RuntimeInformation.RuntimeIdentifier.StartsWith("linux-bionic-", StringComparison.Ordinal)) ||
15+
// GSS on Linux does not work with OpenSSL 3.0. Fix was submitted to gss-ntlm but it will take a while to make to
16+
// all supported distributions. The second part of the check should be removed when it does.
17+
Interop.NetSecurityNative.IsNtlmInstalled() && (!PlatformDetection.IsOpenSslSupported || PlatformDetection.OpenSslVersion.Major < 3);
1318
}
1419
}
1520
}

src/libraries/System.Net.Security/src/System/Net/NegotiateAuthenticationPal.Unix.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ namespace System.Net
1919
{
2020
internal partial class NegotiateAuthenticationPal
2121
{
22+
private static readonly Lazy<bool> _hasSystemNetSecurityNative = new Lazy<bool>(CheckHasSystemNetSecurityNative);
23+
internal static bool HasSystemNetSecurityNative => _hasSystemNetSecurityNative.Value;
2224
private static bool UseManagedNtlm { get; } =
2325
AppContext.TryGetSwitch("System.Net.Security.UseManagedNtlm", out bool useManagedNtlm) ?
2426
useManagedNtlm :
25-
OperatingSystem.IsMacOS() || OperatingSystem.IsIOS() || OperatingSystem.IsMacCatalyst();
27+
OperatingSystem.IsMacOS() || OperatingSystem.IsIOS() || OperatingSystem.IsMacCatalyst() ||
28+
(OperatingSystem.IsLinux() && RuntimeInformation.RuntimeIdentifier.StartsWith("linux-bionic-", StringComparison.OrdinalIgnoreCase));
2629

2730
public static NegotiateAuthenticationPal Create(NegotiateAuthenticationClientOptions clientOptions)
2831
{
@@ -34,7 +37,7 @@ public static NegotiateAuthenticationPal Create(NegotiateAuthenticationClientOpt
3437
return ManagedNtlmNegotiateAuthenticationPal.Create(clientOptions);
3538

3639
case NegotiationInfoClass.Negotiate:
37-
return new ManagedSpnegoNegotiateAuthenticationPal(clientOptions, supportKerberos: true);
40+
return new ManagedSpnegoNegotiateAuthenticationPal(clientOptions, supportKerberos: HasSystemNetSecurityNative);
3841
}
3942
}
4043

@@ -559,7 +562,8 @@ private NegotiateAuthenticationStatusCode InitializeSecurityContext(
559562
{
560563
if (NetEventSource.Log.IsEnabled())
561564
{
562-
string protocol = _packageType switch {
565+
string protocol = _packageType switch
566+
{
563567
Interop.NetSecurityNative.PackageType.NTLM => "NTLM",
564568
Interop.NetSecurityNative.PackageType.Kerberos => "Kerberos",
565569
_ => "SPNEGO"
@@ -635,7 +639,8 @@ private NegotiateAuthenticationStatusCode InitializeSecurityContext(
635639
{
636640
if (NetEventSource.Log.IsEnabled())
637641
{
638-
string protocol = _packageType switch {
642+
string protocol = _packageType switch
643+
{
639644
Interop.NetSecurityNative.PackageType.NTLM => "NTLM",
640645
Interop.NetSecurityNative.PackageType.Kerberos => "Kerberos",
641646
_ => isNtlmUsed ? "SPNEGO-NTLM" : "SPNEGO-Kerberos"
@@ -764,5 +769,18 @@ internal static NegotiateAuthenticationStatusCode GetErrorCode(Interop.NetSecuri
764769
}
765770
}
766771
}
772+
773+
public static bool CheckHasSystemNetSecurityNative()
774+
{
775+
try
776+
{
777+
return Interop.NetSecurityNative.IsNtlmInstalled();
778+
}
779+
catch (Exception e) when (e is EntryPointNotFoundException || e is DllNotFoundException || e is TypeInitializationException)
780+
{
781+
// libSystem.Net.Security.Native is not available
782+
return false;
783+
}
784+
}
767785
}
768786
}

src/libraries/System.Net.Security/tests/UnitTests/NegotiateAuthenticationTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public void Constructor_Overloads_Validation()
3232
}
3333

3434
[Fact]
35-
[SkipOnPlatform(TestPlatforms.LinuxBionic, "https://github.com/dotnet/runtime/issues/93104")]
3635
public void RemoteIdentity_ThrowsOnUnauthenticated()
3736
{
3837
NegotiateAuthenticationClientOptions clientOptions = new NegotiateAuthenticationClientOptions { Credential = s_testCredentialRight, TargetName = "HTTP/foo" };
@@ -66,7 +65,6 @@ public void RemoteIdentity_ThrowsOnDisposed()
6665
}
6766

6867
[Fact]
69-
[SkipOnPlatform(TestPlatforms.LinuxBionic, "https://github.com/dotnet/runtime/issues/93104")]
7068
public void Package_Unsupported()
7169
{
7270
NegotiateAuthenticationClientOptions clientOptions = new NegotiateAuthenticationClientOptions { Package = "INVALID", Credential = s_testCredentialRight, TargetName = "HTTP/foo" };
@@ -98,7 +96,6 @@ public void Package_Unsupported_NTLM()
9896

9997
[Fact]
10098
[SkipOnPlatform(TestPlatforms.Windows, "The test is specific to GSSAPI / Managed implementations of NegotiateAuthentication")]
101-
[SkipOnPlatform(TestPlatforms.LinuxBionic, "https://github.com/dotnet/runtime/issues/93104")]
10299
public void DefaultNetworkCredentials_NTLM_DoesNotThrow()
103100
{
104101
NegotiateAuthenticationClientOptions clientOptions = new NegotiateAuthenticationClientOptions { Package = "NTLM", Credential = CredentialCache.DefaultNetworkCredentials, TargetName = "HTTP/foo" };
@@ -169,7 +166,7 @@ public static IEnumerable<object[]> TestCredentials()
169166
yield return new object[] { new NetworkCredential("rightusername", "rightpassword") };
170167
yield return new object[] { new NetworkCredential("rightusername", "rightpassword", "rightdomain") };
171168
yield return new object[] { new NetworkCredential("[email protected]", "rightpassword") };
172-
}
169+
}
173170

174171
[ConditionalTheory(nameof(IsNtlmAvailable))]
175172
[MemberData(nameof(TestCredentials))]

0 commit comments

Comments
 (0)