Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Remove dependencies on WindowsIdentity from Unix binaries #5576

Merged
merged 1 commit into from
Jan 21, 2016
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
18 changes: 18 additions & 0 deletions src/Common/src/System/Net/ContextAwareResult.Unix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace System.Net
{
partial class ContextAwareResult
{
private void SafeCaptureIdentity()
{
// WindowsIdentity is not supported on Unix
}

private void CleanupInternal()
{
// Nothing to cleanup
}
}
}
97 changes: 97 additions & 0 deletions src/Common/src/System/Net/ContextAwareResult.Windows.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using System.Security;
using System.Security.Principal;
using System.Threading;

namespace System.Net
{
partial class ContextAwareResult
{
private WindowsIdentity _windowsIdentity;

// Security: We need an assert for a call into WindowsIdentity.GetCurrent.
private void SafeCaptureIdentity()
{
_windowsIdentity = WindowsIdentity.GetCurrent();
}

// Just like ContextCopy.
internal WindowsIdentity Identity
{
get
{
if (InternalPeekCompleted)
{
if ((_flags & StateFlags.ThreadSafeContextCopy) == 0)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|Called on completed result.", LoggingHash.HashString(this));
}
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |Called on completed result.");
}

throw new InvalidOperationException(SR.net_completed_result);
}

if (_windowsIdentity != null)
{
return _windowsIdentity;
}

// Make sure the identity was requested.
if ((_flags & StateFlags.CaptureIdentity) == 0)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|No identity captured - specify captureIdentity.", LoggingHash.HashString(this));
}
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |No identity captured - specify captureIdentity.");
}

// Just use the lock to block. We might be on the thread that owns the lock which is great, it means we
// don't need an identity anyway.
if ((_flags & StateFlags.PostBlockFinished) == 0)
{
if (_lock == null)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|Must lock (StartPostingAsyncOp()) { ... FinishPostingAsyncOp(); } when calling Identity (unless it's only called after FinishPostingAsyncOp).", LoggingHash.HashString(this));
}
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |Must lock (StartPostingAsyncOp()) { ... FinishPostingAsyncOp(); } when calling Identity (unless it's only called after FinishPostingAsyncOp).");
}
lock (_lock) { }
}

if (InternalPeekCompleted)
{
if ((_flags & StateFlags.ThreadSafeContextCopy) == 0)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|Result became completed during call.", LoggingHash.HashString(this));
}
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |Result became completed during call.");
}

throw new InvalidOperationException(SR.net_completed_result);
}

return _windowsIdentity;
}
}

private void CleanupInternal()
{
if (_windowsIdentity != null)
{
_windowsIdentity.Dispose();
_windowsIdentity = null;
}
}
}
}
90 changes: 2 additions & 88 deletions src/Common/src/System/Net/ContextAwareResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal ExecutionContext Context

// This class will ensure that the correct context is restored on the thread before invoking
// a user callback.
internal class ContextAwareResult : LazyAsyncResult
internal partial class ContextAwareResult : LazyAsyncResult
{
[Flags]
private enum StateFlags
Expand All @@ -88,9 +88,6 @@ private enum StateFlags
private object _lock;
private StateFlags _flags;

#if !NetNative
private WindowsIdentity _windowsIdentity;
#endif

internal ContextAwareResult(object myObject, object myState, AsyncCallback myCallBack) :
this(false, false, myObject, myState, myCallBack)
Expand Down Expand Up @@ -125,14 +122,6 @@ internal ContextAwareResult(bool captureIdentity, bool forceCaptureContext, bool
}
}

// Security: We need an assert for a call into WindowsIdentity.GetCurrent.
private void SafeCaptureIdentity()
{
#if !NetNative
_windowsIdentity = WindowsIdentity.GetCurrent();
#endif
}

// This can be used to establish a context during an async op for something like calling a delegate or demanding a permission.
// May block briefly if the context is still being produced.
//
Expand Down Expand Up @@ -204,75 +193,6 @@ internal ExecutionContext ContextCopy
}
}

#if !NetNative
// Just like ContextCopy.
internal WindowsIdentity Identity
{
get
{
if (InternalPeekCompleted)
{
if ((_flags & StateFlags.ThreadSafeContextCopy) == 0)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|Called on completed result.", LoggingHash.HashString(this));
}
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |Called on completed result.");
}

throw new InvalidOperationException(SR.net_completed_result);
}

if (_windowsIdentity != null)
{
return _windowsIdentity;
}

// Make sure the identity was requested.
if ((_flags & StateFlags.CaptureIdentity) == 0)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|No identity captured - specify captureIdentity.", LoggingHash.HashString(this));
}
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |No identity captured - specify captureIdentity.");
}

// Just use the lock to block. We might be on the thread that owns the lock which is great, it means we
// don't need an identity anyway.
if ((_flags & StateFlags.PostBlockFinished) == 0)
{
if (_lock == null)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|Must lock (StartPostingAsyncOp()) { ... FinishPostingAsyncOp(); } when calling Identity (unless it's only called after FinishPostingAsyncOp).", LoggingHash.HashString(this));
}
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |Must lock (StartPostingAsyncOp()) { ... FinishPostingAsyncOp(); } when calling Identity (unless it's only called after FinishPostingAsyncOp).");
}
lock (_lock) { }
}

if (InternalPeekCompleted)
{
if ((_flags & StateFlags.ThreadSafeContextCopy) == 0)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|Result became completed during call.", LoggingHash.HashString(this));
}
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |Result became completed during call.");
}

throw new InvalidOperationException(SR.net_completed_result);
}

return _windowsIdentity;
}
}
#endif

#if DEBUG
// Want to be able to verify that the Identity was requested. If it was requested but isn't available
// on the Identity property, it's either available via ContextCopy or wasn't needed (synchronous).
Expand Down Expand Up @@ -382,13 +302,7 @@ protected override void Cleanup()
GlobalLog.Print("ContextAwareResult#" + LoggingHash.HashString(this) + "::Cleanup()");
}

#if !NetNative
if (_windowsIdentity != null)
{
_windowsIdentity.Dispose();
_windowsIdentity = null;
}
#endif
CleanupInternal();
}

// This must be called right before returning the result to the user. It might call the callback itself,
Expand Down
18 changes: 18 additions & 0 deletions src/Common/src/System/Net/ContextAwareResult.netcore50.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should have a single file ContextAwareResult.WindowsIdentityNop.cs, or something like that, which we use for both the unix and netcore50 builds, rather than duplicating the same contents.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about that. My imppression was the networking team would want to fill in the real implementation for unix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is these will remain nops, but ok.

// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace System.Net
{
partial class ContextAwareResult
{
private void SafeCaptureIdentity()
{
// WindowsIdentity is not supported on NETCore50
}

private void CleanupInternal()
{
// Nothing to cleanup
}
}
}
7 changes: 6 additions & 1 deletion src/System.Data.SqlClient/src/System.Data.SqlClient.builds
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<ItemGroup>
<Project Include="System.Data.SqlClient.csproj" />
<Project Include="System.Data.SqlClient.csproj">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericstj What do these changes do ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are specifying that you actually have 2 distinct builds of the library: one for windows and one for linux (actually, unix but we don't have a unix only convention /cc @weshaggard)
This makes it consistent with the package.
The way it was structured before was as if the library only had a single cross-platform build.

<OSGroup>Linux</OSGroup>
</Project>
<Project Include="System.Data.SqlClient.csproj">
<OSGroup>Windows_NT</OSGroup>
</Project>
<Project Include="facade\System.Data.SqlClient.csproj">
<TargetGroup>net46</TargetGroup>
</Project>
Expand Down
10 changes: 6 additions & 4 deletions src/System.Data.SqlClient/src/System.Data.SqlClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PropertyGroup Condition=" '$(TargetsWindows)' != 'true' ">
<DefineConstants>$(DefineConstants);MANAGED_SNI</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Linux_Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Linux_Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Windows_Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Windows_Release|AnyCPU'" />
<ItemGroup>
<Compile Include="Interop\SNINativeMethodWrapper.cs" />
<Compile Include="Microsoft\SqlServer\Server\ITypedGetters.cs" />
Expand Down Expand Up @@ -146,9 +146,11 @@
</ItemGroup>
<ItemGroup Condition=" '$(TargetsWindows)' == 'true' ">
<Compile Include="Interop\LocaleMapper.Windows.cs" />
<Compile Include="System\Data\ProviderBase\DbConnectionPoolIdentity.Windows.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsWindows)' != 'true' ">
<Compile Include="System\Data\Locale\LocaleMapper.Unix.cs" />
<Compile Include="System\Data\ProviderBase\DbConnectionPoolIdentity.Unix.cs" />
<Compile Include="System\Data\SqlClient\SNI\SNIError.cs" />
<Compile Include="System\Data\SqlClient\SNI\SNIHandle.cs" />
<Compile Include="System\Data\SqlClient\SNI\SNILoadHandle.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.


//------------------------------------------------------------------------------

using System.Security.Principal;


namespace System.Data.ProviderBase
{
partial class DbConnectionPoolIdentity
{
static internal DbConnectionPoolIdentity GetCurrent()
{
throw new PlatformNotSupportedException();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.


//------------------------------------------------------------------------------

using System.Security.Principal;


namespace System.Data.ProviderBase
{
partial class DbConnectionPoolIdentity
{
static private DbConnectionPoolIdentity s_lastIdentity = null;

static internal DbConnectionPoolIdentity GetCurrent()
{
DbConnectionPoolIdentity current;
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
IntPtr token = identity.AccessToken.DangerousGetHandle();
bool isNetwork = identity.User.IsWellKnown(WellKnownSidType.NetworkSid);
string sidString = identity.User.Value;

// Win32NativeMethods.IsTokenRestricted will raise exception if the native call fails
bool isRestricted = Win32NativeMethods.IsTokenRestrictedWrapper(token);

var lastIdentity = s_lastIdentity;
if ((lastIdentity != null) && (lastIdentity._sidString == sidString) && (lastIdentity._isRestricted == isRestricted) && (lastIdentity._isNetwork == isNetwork))
{
current = lastIdentity;
}
else
{
current = new DbConnectionPoolIdentity(sidString, isRestricted, isNetwork);
}
}
s_lastIdentity = current;
return current;
}
}
}

Loading