diff --git a/src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.NetSecurityNative.IsNtlmInstalled.cs b/src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.NetSecurityNative.IsNtlmInstalled.cs index b799ef433e8830..d841d4c65a7dd1 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.NetSecurityNative.IsNtlmInstalled.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.NetSecurityNative.IsNtlmInstalled.cs @@ -15,24 +15,13 @@ internal static partial class NetSecurityNative [LibraryImport(Interop.Libraries.NetSecurityNative, EntryPoint = "NetSecurityNative_EnsureGssInitialized")] private static partial int EnsureGssInitialized(); - static NetSecurityNative() - { - GssInitializer.Initialize(); - } + private const string GssApiLibraryName = "libgssapi_krb5.so.2"; - internal static class GssInitializer + static NetSecurityNative() { - static GssInitializer() - { - if (EnsureGssInitialized() != 0) - { - throw new InvalidOperationException(); - } - } - - internal static void Initialize() + if (EnsureGssInitialized() != 0) { - // No-op that exists to provide a hook for other static constructors. + throw new DllNotFoundException(GssApiLibraryName); } } } diff --git a/src/libraries/System.Net.Security/src/System/Net/NegotiateAuthenticationPal.Unix.cs b/src/libraries/System.Net.Security/src/System/Net/NegotiateAuthenticationPal.Unix.cs index 475483944f84a3..1daba776edc724 100644 --- a/src/libraries/System.Net.Security/src/System/Net/NegotiateAuthenticationPal.Unix.cs +++ b/src/libraries/System.Net.Security/src/System/Net/NegotiateAuthenticationPal.Unix.cs @@ -40,21 +40,22 @@ public static NegotiateAuthenticationPal Create(NegotiateAuthenticationClientOpt { return new UnixNegotiateAuthenticationPal(clientOptions); } - catch (Interop.NetSecurityNative.GssApiException gex) + catch (Exception ex) when (ex is Interop.NetSecurityNative.GssApiException or TypeInitializationException) { - if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, gex); - NegotiateAuthenticationStatusCode statusCode = UnixNegotiateAuthenticationPal.GetErrorCode(gex); - if (statusCode <= NegotiateAuthenticationStatusCode.GenericFailure) + if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, ex); + NegotiateAuthenticationStatusCode statusCode = NegotiateAuthenticationStatusCode.Unsupported; + + if (ex is Interop.NetSecurityNative.GssApiException gex) { - statusCode = NegotiateAuthenticationStatusCode.Unsupported; + statusCode = UnixNegotiateAuthenticationPal.GetErrorCode(gex); + if (statusCode <= NegotiateAuthenticationStatusCode.GenericFailure) + { + statusCode = NegotiateAuthenticationStatusCode.Unsupported; + } } + return new UnsupportedNegotiateAuthenticationPal(clientOptions, statusCode); } - catch (EntryPointNotFoundException) - { - // GSSAPI shim may not be available on some platforms (Linux Bionic) - return new UnsupportedNegotiateAuthenticationPal(clientOptions); - } } public static NegotiateAuthenticationPal Create(NegotiateAuthenticationServerOptions serverOptions) @@ -63,21 +64,22 @@ public static NegotiateAuthenticationPal Create(NegotiateAuthenticationServerOpt { return new UnixNegotiateAuthenticationPal(serverOptions); } - catch (Interop.NetSecurityNative.GssApiException gex) + catch (Exception ex) when (ex is Interop.NetSecurityNative.GssApiException or TypeInitializationException) { - if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, gex); - NegotiateAuthenticationStatusCode statusCode = UnixNegotiateAuthenticationPal.GetErrorCode(gex); - if (statusCode <= NegotiateAuthenticationStatusCode.GenericFailure) + if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, ex); + + NegotiateAuthenticationStatusCode statusCode = NegotiateAuthenticationStatusCode.Unsupported; + + if (ex is Interop.NetSecurityNative.GssApiException gex) { - statusCode = NegotiateAuthenticationStatusCode.Unsupported; + statusCode = UnixNegotiateAuthenticationPal.GetErrorCode(gex); + if (statusCode <= NegotiateAuthenticationStatusCode.GenericFailure) + { + statusCode = NegotiateAuthenticationStatusCode.Unsupported; + } } return new UnsupportedNegotiateAuthenticationPal(serverOptions, statusCode); } - catch (EntryPointNotFoundException) - { - // GSSAPI shim may not be available on some platforms (Linux Bionic) - return new UnsupportedNegotiateAuthenticationPal(serverOptions); - } } internal sealed class UnixNegotiateAuthenticationPal : NegotiateAuthenticationPal diff --git a/src/native/libs/System.Net.Security.Native/pal_gssapi.c b/src/native/libs/System.Net.Security.Native/pal_gssapi.c index 9be66b8a7566c2..d793217a4a671d 100644 --- a/src/native/libs/System.Net.Security.Native/pal_gssapi.c +++ b/src/native/libs/System.Net.Security.Native/pal_gssapi.c @@ -122,7 +122,7 @@ static void* volatile s_gssLib = NULL; static int32_t ensure_gss_shim_initialized(void) { void* lib = dlopen(gss_lib_name, RTLD_LAZY); - if (lib == NULL) { fprintf(stderr, "Cannot load library %s \nError: %s\n", gss_lib_name, dlerror()); return -1; } + if (lib == NULL) { return -1; } // check is someone else has opened and published s_gssLib already if (!pal_atomic_cas_ptr(&s_gssLib, lib, NULL)) @@ -130,12 +130,10 @@ static int32_t ensure_gss_shim_initialized(void) dlclose(lib); } - // initialize indirection pointers for all functions, like: - // gss_accept_sec_context_ptr = (TYPEOF(gss_accept_sec_context)*)dlsym(s_gssLib, "gss_accept_sec_context"); - // if (gss_accept_sec_context_ptr == NULL) { fprintf(stderr, "Cannot get symbol %s from %s \nError: %s\n", "gss_accept_sec_context", gss_lib_name, dlerror()); return -1; } + // initialize indirection pointers for all functions #define PER_FUNCTION_BLOCK(fn) \ fn##_ptr = (TYPEOF(fn)*)dlsym(s_gssLib, #fn); \ - if (fn##_ptr == NULL) { fprintf(stderr, "Cannot get symbol " #fn " from %s \nError: %s\n", gss_lib_name, dlerror()); return -1; } + if (fn##_ptr == NULL) { fprintf(stderr, "Cannot get symbol " #fn " from %s \nError: %s\n", gss_lib_name, dlerror()); abort(); } FOR_ALL_GSS_FUNCTIONS #undef PER_FUNCTION_BLOCK