Fix LsaOpenPolicy interop definition
authorHugh Bellamy <hughbellars@gmail.com>
Sat, 13 Apr 2019 16:52:14 +0000 (17:52 +0100)
committerJeremy Barton <jbarton@microsoft.com>
Sat, 13 Apr 2019 16:52:14 +0000 (09:52 -0700)
* Move Lsa handles to Interop code

* Fix implementation of LsaOpenPolicy

* Move LSA_STRING and LSA_UNICODE_STRING to Advapi32

* Consolidate LSA_UNICODE_STRING and UNICODE_STRING

Commit migrated from https://github.com/dotnet/corefx/commit/f5e2679e76c102aea8218ae290ffb7f4d97e9cc2

27 files changed:
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LSA_STRING.cs [moved from src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LsaString.cs with 97% similarity]
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaLookupNames2.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaLookupSids.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaOpenPolicy.cs
src/libraries/Common/src/Interop/Windows/Interop.OBJECT_ATTRIBUTES.cs [new file with mode: 0644]
src/libraries/Common/src/Interop/Windows/NtDll/Interop.NtCreateFile.cs
src/libraries/Common/src/Interop/Windows/SspiCli/Interop.KerbS4uLogin.cs
src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LSAStructs.cs
src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LsaLogonUser.cs
src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LsaLookupAuthenticationPackage.cs
src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LsaUnicodeString.cs [deleted file]
src/libraries/Common/src/Interop/Windows/SspiCli/Interop.UNICODE_STRING.cs [deleted file]
src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeLsaMemoryHandle.cs [new file with mode: 0644]
src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeLsaPolicyHandle.cs [new file with mode: 0644]
src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeLsaReturnBufferHandle.cs [new file with mode: 0644]
src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeTokenHandle.cs [moved from src/libraries/System.Security.AccessControl/src/System/Security/SafeSecurityHandles.cs with 100% similarity]
src/libraries/Microsoft.IO.Redist/src/Microsoft.IO.Redist.csproj
src/libraries/System.Data.SqlClient/src/System.Data.SqlClient.csproj
src/libraries/System.Diagnostics.Process/src/Microsoft/Win32/SafeHandles/SafeTokenHandle.cs [deleted file]
src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj
src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj
src/libraries/System.Security.AccessControl/src/System.Security.AccessControl.csproj
src/libraries/System.Security.Principal.Windows/src/Microsoft/Win32/SafeHandles/SafeSecurityHandles.cs [deleted file]
src/libraries/System.Security.Principal.Windows/src/System.Security.Principal.Windows.csproj
src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/NTAccount.cs
src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/Win32.cs
src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/WindowsIdentity.cs

@@ -7,7 +7,7 @@ using System.Runtime.InteropServices;
 
 internal partial class Interop
 {
-    internal partial class SspiCli
+    internal partial class Advapi32
     {
         [StructLayout(LayoutKind.Sequential)]
         internal struct LSA_STRING
index dcb76be..084dfee 100644 (file)
@@ -15,9 +15,18 @@ internal static partial class Interop
             SafeLsaPolicyHandle handle,
             int flags,
             int count,
-            UNICODE_STRING[] names,
+            MARSHALLED_UNICODE_STRING[] names,
             out SafeLsaMemoryHandle referencedDomains,
             out SafeLsaMemoryHandle sids
-            );
+        );
+        
+        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
+        internal struct MARSHALLED_UNICODE_STRING
+        {
+            internal ushort Length;
+            internal ushort MaximumLength;
+            [MarshalAs(UnmanagedType.LPWStr)]
+            internal string Buffer;
+        }
     }
 }
index 576d8e5..b28132a 100644 (file)
@@ -17,6 +17,6 @@ internal static partial class Interop
             IntPtr[] sids,
             out SafeLsaMemoryHandle referencedDomains,
             out SafeLsaMemoryHandle names
-            );
+        );
     }
 }
index ca7ae1c..4360f33 100644 (file)
@@ -11,6 +11,34 @@ internal static partial class Interop
     internal static partial class Advapi32
     {
         [DllImport(Interop.Libraries.Advapi32, EntryPoint = "LsaOpenPolicy", SetLastError = true, CharSet = CharSet.Unicode)]
-        internal static extern uint LsaOpenPolicy(string systemName, ref LSA_OBJECT_ATTRIBUTES attributes, int accessMask, out SafeLsaPolicyHandle handle);
+        private static extern uint LsaOpenPolicy(
+            ref UNICODE_STRING SystemName,
+            ref OBJECT_ATTRIBUTES ObjectAttributes,
+            int AccessMask,
+            out SafeLsaPolicyHandle PolicyHandle
+        );
+
+        internal static unsafe uint LsaOpenPolicy(
+            string SystemName,
+            ref OBJECT_ATTRIBUTES Attributes,
+            int AccessMask,
+            out SafeLsaPolicyHandle PolicyHandle)
+        {
+            var systemNameUnicode = new UNICODE_STRING();
+            if (SystemName != null)
+            {
+                fixed (char* c = SystemName)
+                {
+                    systemNameUnicode.Length = checked((ushort)(SystemName.Length * sizeof(char)));
+                    systemNameUnicode.MaximumLength = checked((ushort)(SystemName.Length * sizeof(char)));
+                    systemNameUnicode.Buffer = (IntPtr)c;
+                    return LsaOpenPolicy(ref systemNameUnicode, ref Attributes, AccessMask, out PolicyHandle);
+                }
+            }
+            else
+            {
+                return LsaOpenPolicy(ref systemNameUnicode, ref Attributes, AccessMask, out PolicyHandle);
+            }
+        }
     }
 }
diff --git a/src/libraries/Common/src/Interop/Windows/Interop.OBJECT_ATTRIBUTES.cs b/src/libraries/Common/src/Interop/Windows/Interop.OBJECT_ATTRIBUTES.cs
new file mode 100644 (file)
index 0000000..c6e51ae
--- /dev/null
@@ -0,0 +1,103 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Runtime.InteropServices;
+
+internal static partial class Interop
+{
+    /// <summary>
+    /// <a href="https://msdn.microsoft.com/en-us/library/windows/hardware/ff557749.aspx">OBJECT_ATTRIBUTES</a> structure.
+    /// The OBJECT_ATTRIBUTES structure specifies attributes that can be applied to objects or object handles by routines 
+    /// that create objects and/or return handles to objects.
+    /// </summary>
+    internal unsafe struct OBJECT_ATTRIBUTES
+    {
+        public uint Length;
+
+        /// <summary>
+        /// Optional handle to root object directory for the given ObjectName.
+        /// Can be a file system directory or object manager directory.
+        /// </summary>
+        public IntPtr RootDirectory;
+
+        /// <summary>
+        /// Name of the object. Must be fully qualified if RootDirectory isn't set.
+        /// Otherwise is relative to RootDirectory.
+        /// </summary>
+        public UNICODE_STRING* ObjectName;
+
+        public ObjectAttributes Attributes;
+
+        /// <summary>
+        /// If null, object will receive default security settings.
+        /// </summary>
+        public void* SecurityDescriptor;
+
+        /// <summary>
+        /// Optional quality of service to be applied to the object. Used to indicate
+        /// security impersonation level and context tracking mode (dynamic or static).
+        /// </summary>
+        public void* SecurityQualityOfService;
+
+        /// <summary>
+        /// Equivalent of InitializeObjectAttributes macro with the exception that you can directly set SQOS.
+        /// </summary>
+        public unsafe OBJECT_ATTRIBUTES(UNICODE_STRING* objectName, ObjectAttributes attributes, IntPtr rootDirectory)
+        {
+            Length = (uint)sizeof(OBJECT_ATTRIBUTES);
+            RootDirectory = rootDirectory;
+            ObjectName = objectName;
+            Attributes = attributes;
+            SecurityDescriptor = null;
+            SecurityQualityOfService = null;
+        }
+    }
+
+    [Flags]
+    public enum ObjectAttributes : uint
+    {
+        // https://msdn.microsoft.com/en-us/library/windows/hardware/ff564586.aspx
+        // https://msdn.microsoft.com/en-us/library/windows/hardware/ff547804.aspx
+
+        /// <summary>
+        /// This handle can be inherited by child processes of the current process.
+        /// </summary>
+        OBJ_INHERIT = 0x00000002,
+
+        /// <summary>
+        /// This flag only applies to objects that are named within the object manager.
+        /// By default, such objects are deleted when all open handles to them are closed.
+        /// If this flag is specified, the object is not deleted when all open handles are closed.
+        /// </summary>
+        OBJ_PERMANENT = 0x00000010,
+
+        /// <summary>
+        /// Only a single handle can be open for this object.
+        /// </summary>
+        OBJ_EXCLUSIVE = 0x00000020,
+
+        /// <summary>
+        /// Lookups for this object should be case insensitive.
+        /// </summary>
+        OBJ_CASE_INSENSITIVE = 0x00000040,
+
+        /// <summary>
+        /// Create on existing object should open, not fail with STATUS_OBJECT_NAME_COLLISION.
+        /// </summary>
+        OBJ_OPENIF = 0x00000080,
+
+        /// <summary>
+        /// Open the symbolic link, not its target.
+        /// </summary>
+        OBJ_OPENLINK = 0x00000100,
+
+        // Only accessible from kernel mode
+        // OBJ_KERNEL_HANDLE
+
+        // Access checks enforced, even in kernel mode
+        // OBJ_FORCE_ACCESS_CHECK
+        // OBJ_VALID_ATTRIBUTES = 0x000001F2
+    }
+}
index 967b7bf..1fa7b72 100644 (file)
@@ -69,100 +69,6 @@ internal partial class Interop
         }
 
         /// <summary>
-        /// <a href="https://msdn.microsoft.com/en-us/library/windows/hardware/ff557749.aspx">OBJECT_ATTRIBUTES</a> structure.
-        /// The OBJECT_ATTRIBUTES structure specifies attributes that can be applied to objects or object handles by routines 
-        /// that create objects and/or return handles to objects.
-        /// </summary>
-        internal unsafe struct OBJECT_ATTRIBUTES
-        {
-            public uint Length;
-
-            /// <summary>
-            /// Optional handle to root object directory for the given ObjectName.
-            /// Can be a file system directory or object manager directory.
-            /// </summary>
-            public IntPtr RootDirectory;
-
-            /// <summary>
-            /// Name of the object. Must be fully qualified if RootDirectory isn't set.
-            /// Otherwise is relative to RootDirectory.
-            /// </summary>
-            public UNICODE_STRING* ObjectName;
-
-            public ObjectAttributes Attributes;
-
-            /// <summary>
-            /// If null, object will receive default security settings.
-            /// </summary>
-            public void* SecurityDescriptor;
-
-            /// <summary>
-            /// Optional quality of service to be applied to the object. Used to indicate
-            /// security impersonation level and context tracking mode (dynamic or static).
-            /// </summary>
-            public void* SecurityQualityOfService;
-
-            /// <summary>
-            /// Equivalent of InitializeObjectAttributes macro with the exception that you can directly set SQOS.
-            /// </summary>
-            public unsafe OBJECT_ATTRIBUTES(UNICODE_STRING* objectName, ObjectAttributes attributes, IntPtr rootDirectory)
-            {
-                Length = (uint)sizeof(OBJECT_ATTRIBUTES);
-                RootDirectory = rootDirectory;
-                ObjectName = objectName;
-                Attributes = attributes;
-                SecurityDescriptor = null;
-                SecurityQualityOfService = null;
-            }
-        }
-
-        [Flags]
-        public enum ObjectAttributes : uint
-        {
-            // https://msdn.microsoft.com/en-us/library/windows/hardware/ff564586.aspx
-            // https://msdn.microsoft.com/en-us/library/windows/hardware/ff547804.aspx
-
-            /// <summary>
-            /// This handle can be inherited by child processes of the current process.
-            /// </summary>
-            OBJ_INHERIT = 0x00000002,
-
-            /// <summary>
-            /// This flag only applies to objects that are named within the object manager.
-            /// By default, such objects are deleted when all open handles to them are closed.
-            /// If this flag is specified, the object is not deleted when all open handles are closed.
-            /// </summary>
-            OBJ_PERMANENT = 0x00000010,
-
-            /// <summary>
-            /// Only a single handle can be open for this object.
-            /// </summary>
-            OBJ_EXCLUSIVE = 0x00000020,
-
-            /// <summary>
-            /// Lookups for this object should be case insensitive.
-            /// </summary>
-            OBJ_CASE_INSENSITIVE = 0x00000040,
-
-            /// <summary>
-            /// Create on existing object should open, not fail with STATUS_OBJECT_NAME_COLLISION.
-            /// </summary>
-            OBJ_OPENIF = 0x00000080,
-
-            /// <summary>
-            /// Open the symbolic link, not its target.
-            /// </summary>
-            OBJ_OPENLINK = 0x00000100,
-
-            // Only accessible from kernel mode
-            // OBJ_KERNEL_HANDLE
-
-            // Access checks enforced, even in kernel mode
-            // OBJ_FORCE_ACCESS_CHECK
-            // OBJ_VALID_ATTRIBUTES = 0x000001F2
-        }
-
-        /// <summary>
         /// File creation disposition when calling directly to NT APIs.
         /// </summary>
         public enum CreateDisposition : uint
index 68e0022..2e9dd97 100644 (file)
@@ -14,8 +14,8 @@ internal partial class Interop
         {
             internal KERB_LOGON_SUBMIT_TYPE MessageType;
             internal KerbS4uLogonFlags Flags;
-            internal LSA_UNICODE_STRING ClientUpn;
-            internal LSA_UNICODE_STRING ClientRealm;
+            internal UNICODE_STRING ClientUpn;
+            internal UNICODE_STRING ClientRealm;
         }
 
         [Flags]
index 3d0d6a6..bba8c14 100644 (file)
@@ -18,17 +18,6 @@ internal static partial class Interop
     }
 
     [StructLayout(LayoutKind.Sequential)]
-    internal struct LSA_OBJECT_ATTRIBUTES
-    {
-        internal int Length;
-        internal IntPtr RootDirectory;
-        internal IntPtr ObjectName;
-        internal int Attributes;
-        internal IntPtr SecurityDescriptor;
-        internal IntPtr SecurityQualityOfService;
-    }
-
-    [StructLayout(LayoutKind.Sequential)]
     internal struct LSA_TRANSLATED_SID2
     {
         internal int Use;
index 86d4be2..268688c 100644 (file)
@@ -14,7 +14,7 @@ internal partial class Interop
         [DllImport(Libraries.SspiCli)]
         internal static extern int LsaLogonUser(
             [In]  SafeLsaHandle LsaHandle,
-            [In]  ref LSA_STRING OriginName,
+            [In]  ref Advapi32.LSA_STRING OriginName,
             [In]  SECURITY_LOGON_TYPE LogonType,
             [In]  int AuthenticationPackage,
             [In]  IntPtr AuthenticationInformation,
index 950cde7..6080240 100644 (file)
@@ -4,7 +4,6 @@
 
 using System;
 using System.Runtime.InteropServices;
-
 using Microsoft.Win32.SafeHandles;
 
 internal partial class Interop
@@ -12,6 +11,10 @@ internal partial class Interop
     internal partial class SspiCli
     {
         [DllImport(Libraries.SspiCli)]
-        internal static extern int LsaLookupAuthenticationPackage(SafeLsaHandle LsaHandle, [In] ref LSA_STRING PackageName, out int AuthenticationPackage);
+        internal static extern int LsaLookupAuthenticationPackage(
+            SafeLsaHandle LsaHandle,
+            [In] ref Advapi32.LSA_STRING PackageName,
+            out int AuthenticationPackage
+        );
     }
 }
diff --git a/src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LsaUnicodeString.cs b/src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LsaUnicodeString.cs
deleted file mode 100644 (file)
index 205fa8a..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-using System;
-using System.Runtime.InteropServices;
-
-internal partial class Interop
-{
-    internal partial class SspiCli
-    {
-        [StructLayout(LayoutKind.Sequential)]
-        internal struct LSA_UNICODE_STRING
-        {
-            internal LSA_UNICODE_STRING(IntPtr pBuffer, ushort length)
-            {
-                Length = length;
-                MaximumLength = length;
-                Buffer = pBuffer;
-            }
-
-            /// <summary>
-            /// Specifies the length, in bytes, of the string in Buffer. This value does not include the terminating null character, if any.
-            /// </summary>
-            internal ushort Length;
-
-            /// <summary>
-            /// Specifies the total size, in bytes, of Buffer. Up to MaximumLength bytes may be written into the buffer without trampling memory.
-            /// </summary>
-            internal ushort MaximumLength;
-
-            /// <summary>
-            /// Pointer to a wide character string. Note that strings returned by the LSA may not be null-terminated.
-            /// </summary>
-            internal IntPtr Buffer;
-        }
-    }
-}
diff --git a/src/libraries/Common/src/Interop/Windows/SspiCli/Interop.UNICODE_STRING.cs b/src/libraries/Common/src/Interop/Windows/SspiCli/Interop.UNICODE_STRING.cs
deleted file mode 100644 (file)
index e81b506..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-using System;
-using System.Runtime.InteropServices;
-
-internal static partial class Interop
-{
-    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
-    internal struct UNICODE_STRING
-    {
-        internal ushort Length;
-        internal ushort MaximumLength;
-        [MarshalAs(UnmanagedType.LPWStr)]
-        internal string Buffer;
-    }
-}
diff --git a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeLsaMemoryHandle.cs b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeLsaMemoryHandle.cs
new file mode 100644 (file)
index 0000000..d7beb58
--- /dev/null
@@ -0,0 +1,25 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace Microsoft.Win32.SafeHandles
+{
+    internal sealed class SafeLsaMemoryHandle : SafeBuffer
+    {
+        private SafeLsaMemoryHandle() : base(true) { }
+
+        // 0 is an Invalid Handle
+        internal SafeLsaMemoryHandle(IntPtr handle) : base(true)
+        {
+            SetHandle(handle);
+        }
+
+        override protected bool ReleaseHandle()
+        {
+            return Interop.Advapi32.LsaFreeMemory(handle) == 0;
+        }
+    }
+}
diff --git a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeLsaPolicyHandle.cs b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeLsaPolicyHandle.cs
new file mode 100644 (file)
index 0000000..856bea9
--- /dev/null
@@ -0,0 +1,24 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+
+namespace Microsoft.Win32.SafeHandles
+{
+    internal sealed class SafeLsaPolicyHandle : SafeHandleZeroOrMinusOneIsInvalid
+    {
+        private SafeLsaPolicyHandle() : base(true) { }
+
+        // 0 is an Invalid Handle
+        internal SafeLsaPolicyHandle(IntPtr handle) : base(true)
+        {
+            SetHandle(handle);
+        }
+
+        override protected bool ReleaseHandle()
+        {
+            return Interop.Advapi32.LsaClose(handle) == 0;
+        }
+    }
+}
diff --git a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeLsaReturnBufferHandle.cs b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeLsaReturnBufferHandle.cs
new file mode 100644 (file)
index 0000000..d310e6a
--- /dev/null
@@ -0,0 +1,27 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Runtime.InteropServices;
+using System.Security;
+
+namespace Microsoft.Win32.SafeHandles
+{
+    internal sealed class SafeLsaReturnBufferHandle : SafeBuffer
+    {
+        private SafeLsaReturnBufferHandle() : base(true) { }
+
+        // 0 is an Invalid Handle
+        internal SafeLsaReturnBufferHandle(IntPtr handle) : base(true)
+        {
+            SetHandle(handle);
+        }
+
+        override protected bool ReleaseHandle()
+        {
+            // LsaFreeReturnBuffer returns an NTSTATUS
+            return Interop.SspiCli.LsaFreeReturnBuffer(handle) >= 0;
+        }
+    }
+}
index feecce9..295b777 100644 (file)
@@ -52,6 +52,9 @@
     <Compile Include="$(CommonPath)\Interop\Windows\Interop.UNICODE_STRING.cs">
       <Link>Common\Interop\Windows\Interop.UNICODE_STRING.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\Interop\Windows\Interop.OBJECT_ATTRIBUTES.cs">
+      <Link>Common\Interop\Windows\Interop.OBJECT_ATTRIBUTES.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Interop.BOOLEAN.cs">
       <Link>Common\Interop\Windows\Interop.BOOLEAN.cs</Link>
     </Compile>
index ad70b88..a730f2e 100644 (file)
     <Compile Include="$(CommonPath)\Interop\Windows\Interop.UNICODE_STRING.cs">
       <Link>Common\Interop\Windows\Interop.UNICODE_STRING.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\Interop\Windows\Interop.OBJECT_ATTRIBUTES.cs">
+      <Link>Common\Interop\Windows\Interop.OBJECT_ATTRIBUTES.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.IoControlCodeAccess.cs">
       <Link>Common\Interop\Windows\Kernel32\Interop.IoControlCodeAccess.cs</Link>
     </Compile>
diff --git a/src/libraries/System.Diagnostics.Process/src/Microsoft/Win32/SafeHandles/SafeTokenHandle.cs b/src/libraries/System.Diagnostics.Process/src/Microsoft/Win32/SafeHandles/SafeTokenHandle.cs
deleted file mode 100644 (file)
index 83160f7..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-/*============================================================
-**
-** Class:  SafeTokenHandle 
-**
-** A wrapper for a process handle
-**
-** 
-===========================================================*/
-
-using System;
-using System.Diagnostics;
-using System.Runtime.InteropServices;
-using System.Security;
-
-namespace Microsoft.Win32.SafeHandles
-{
-    internal sealed class SafeTokenHandle : SafeHandle
-    {
-        private const int DefaultInvalidHandleValue = 0;
-
-        internal static readonly SafeTokenHandle InvalidHandle = new SafeTokenHandle(new IntPtr(DefaultInvalidHandleValue));
-
-        internal SafeTokenHandle() : base(new IntPtr(DefaultInvalidHandleValue), true) { }
-
-        internal SafeTokenHandle(IntPtr handle)
-            : base(new IntPtr(DefaultInvalidHandleValue), true)
-        {
-            SetHandle(handle);
-        }
-
-        public override bool IsInvalid
-        {
-            get { return handle == IntPtr.Zero || handle == new IntPtr(-1); }
-        }
-
-        protected override bool ReleaseHandle()
-        {
-            return Interop.Kernel32.CloseHandle(handle);
-        }
-    }
-}
index 8a215fb..dcec78c 100644 (file)
@@ -86,6 +86,9 @@
     </Compile>
   </ItemGroup>
   <ItemGroup Condition=" '$(TargetsWindows)' == 'true'">
+    <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeTokenHandle.cs">
+      <Link>Microsoft\Win32\SafeHandles\SafeTokenHandle.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
       <Link>Common\Interop\Windows\Interop.Libraries.cs</Link>
     </Compile>
     </Compile>
     <Compile Include="Microsoft\Win32\SafeHandles\SafeProcessHandle.Windows.cs" />
     <Compile Include="Microsoft\Win32\SafeHandles\SafeThreadHandle.cs" />
-    <Compile Include="Microsoft\Win32\SafeHandles\SafeTokenHandle.cs" />
     <Compile Include="System\Diagnostics\PerformanceCounterLib.cs" />
     <Compile Include="System\Diagnostics\Process.Windows.cs" />
     <Compile Include="System\Diagnostics\ProcessManager.Windows.cs" />
index 3f5fd4f..539e47f 100644 (file)
     <Compile Include="$(CommonPath)\Interop\Windows\Interop.UNICODE_STRING.cs">
       <Link>Common\Interop\Windows\Interop.UNICODE_STRING.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\Interop\Windows\Interop.OBJECT_ATTRIBUTES.cs">
+      <Link>Common\Interop\Windows\Interop.OBJECT_ATTRIBUTES.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Interop.BOOLEAN.cs">
       <Link>Common\Interop\Windows\Interop.BOOLEAN.cs</Link>
     </Compile>
index d3a1b96..4985809 100644 (file)
     <Compile Include="System\Security\AccessControl\Rules.cs" />
     <Compile Include="System\Security\AccessControl\Win32.cs" />
     <Compile Include="System\Security\Principal\Win32.cs" />
-    <Compile Include="System\Security\SafeSecurityHandles.cs" />
     <!-- PInvoke sources -->
     <Compile Include="$(CommonPath)\System\NotImplemented.cs">
       <Link>Common\System\NotImplemented.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeTokenHandle.cs">
+      <Link>Microsoft\Win32\SafeHandles\SafeTokenHandle.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
       <Link>Common\Interop\Interop.Libraries.cs</Link>
     </Compile>
diff --git a/src/libraries/System.Security.Principal.Windows/src/Microsoft/Win32/SafeHandles/SafeSecurityHandles.cs b/src/libraries/System.Security.Principal.Windows/src/Microsoft/Win32/SafeHandles/SafeSecurityHandles.cs
deleted file mode 100644 (file)
index 3db3714..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-using System;
-using System.Runtime.InteropServices;
-using System.Security;
-
-namespace Microsoft.Win32.SafeHandles
-{
-    internal sealed class SafeLsaMemoryHandle : SafeBuffer
-    {
-        private SafeLsaMemoryHandle() : base(true) { }
-
-        // 0 is an Invalid Handle
-        internal SafeLsaMemoryHandle(IntPtr handle) : base(true)
-        {
-            SetHandle(handle);
-        }
-
-        override protected bool ReleaseHandle()
-        {
-            return Interop.Advapi32.LsaFreeMemory(handle) == 0;
-        }
-    }
-
-    internal sealed class SafeLsaPolicyHandle : SafeHandleZeroOrMinusOneIsInvalid
-    {
-        private SafeLsaPolicyHandle() : base(true) { }
-
-        // 0 is an Invalid Handle
-        internal SafeLsaPolicyHandle(IntPtr handle) : base(true)
-        {
-            SetHandle(handle);
-        }
-
-        override protected bool ReleaseHandle()
-        {
-            return Interop.Advapi32.LsaClose(handle) == 0;
-        }
-    }
-
-    internal sealed class SafeLsaReturnBufferHandle : SafeBuffer
-    {
-        private SafeLsaReturnBufferHandle() : base(true) { }
-
-        // 0 is an Invalid Handle
-        internal SafeLsaReturnBufferHandle(IntPtr handle) : base(true)
-        {
-            SetHandle(handle);
-        }
-
-        override protected bool ReleaseHandle()
-        {
-            // LsaFreeReturnBuffer returns an NTSTATUS
-            return Interop.SspiCli.LsaFreeReturnBuffer(handle) >= 0;
-        }
-    }
-}
index 9bc6a0b..690f503 100644 (file)
@@ -10,7 +10,6 @@
   </PropertyGroup>
   <ItemGroup Condition="($(TargetGroup.StartsWith('netcoreapp')) or '$(TargetGroup)' == 'uap') AND '$(TargetsWindows)' == 'true'">
     <Compile Include="Microsoft\Win32\SafeHandles\SafeAccessTokenHandle.cs" />
-    <Compile Include="Microsoft\Win32\SafeHandles\SafeSecurityHandles.cs" />
     <Compile Include="System\Security\Principal\IdentityNotMappedException.cs" />
     <Compile Include="System\Security\Principal\IdentityReference.cs" />
     <Compile Include="System\Security\Principal\IRCollection.cs" />
     <Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
       <Link>Common\Interop\Interop.Libraries.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\Interop\Windows\Interop.UNICODE_STRING.cs">
+      <Link>Common\Interop\Windows\Advapi32\Interop.UNICODE_STRING.cs</Link>
+    </Compile>
+    <Compile Include="$(CommonPath)\Interop\Windows\Interop.OBJECT_ATTRIBUTES.cs">
+      <Link>Common\Interop\Windows\Advapi32\Interop.OBJECT_ATTRIBUTES.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\SspiCli\Interop.TOKENS.cs">
       <Link>Common\Interop\Interop.TOKENS.cs</Link>
     </Compile>
@@ -41,9 +46,6 @@
     <Compile Include="$(CommonPath)\Interop\Windows\SspiCli\Interop.SECURITY_LOGON_SESSION_DATA.cs">
       <Link>Common\Interop\Interop.SECURITY_LOGON_SESSION_DATA.cs</Link>
     </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\SspiCli\Interop.UNICODE_STRING.cs">
-      <Link>Common\Interop\Interop.UNICODE_STRING.cs</Link>
-    </Compile>
     <Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.GetCurrentProcess_IntPtr.cs">
       <Link>Common\Interop\Interop.GetCurrentProcess.cs</Link>
     </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\Advapi32\Interop.LsaNtStatusToWinError.cs">
       <Link>Common\Interop\Interop.LsaNtStatusToWinError.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\Interop\Windows\Advapi32\Interop.LSA_STRING.cs">
+      <Link>Common\Interop\Windows\Advapi32\Interop.LSA_STRING.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.LocalFree.cs">
       <Link>Common\Interop\Interop.LocalFree.cs</Link>
     </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\SspiCli\Interop.LsaLookupAuthenticationPackage.cs">
       <Link>Common\Interop\Windows\SspiCli\Interop.LsaLookupAuthenticationPackage.cs</Link>
     </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\SspiCli\Interop.LsaString.cs">
-      <Link>Common\Interop\Windows\SspiCli\Interop.LsaString.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\SspiCli\Interop.LsaUnicodeString.cs">
-      <Link>Common\Interop\Windows\SspiCli\Interop.LsaUnicodeString.cs</Link>
-    </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\SspiCli\Interop.QuotaLimits.cs">
       <Link>Common\Interop\Windows\SspiCli\Interop.QuotaLimits.cs</Link>
     </Compile>
     <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeLsaHandle.cs">
       <Link>Common\Microsoft\Win32\SafeHandles\SafeLsaHandle.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeLsaMemoryHandle.cs">
+      <Link>Common\Microsoft\Win32\SafeHandles\SafeLsaMemoryHandle.cs</Link>
+    </Compile>
+    <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeLsaPolicyHandle.cs">
+      <Link>Common\Microsoft\Win32\SafeHandles\SafeLsaPolicyHandle.cs</Link>
+    </Compile>
+    <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeLsaReturnBufferHandle.cs">
+      <Link>Common\Microsoft\Win32\SafeHandles\SafeLsaReturnBufferHandle.cs</Link>
+    </Compile>
   </ItemGroup>
   <ItemGroup Condition="$(TargetGroup.StartsWith('netcoreapp')) AND '$(TargetsWindows)' == 'true'">
     <Compile Include="$(CommonPath)\Interop\Windows\Advapi32\Interop.CheckTokenMembership.cs">
index 8ccf20b..c14e184 100644 (file)
@@ -247,7 +247,7 @@ namespace System.Security.Principal
                 // Construct an array of unicode strings
                 //
 
-                Interop.UNICODE_STRING[] Names = new Interop.UNICODE_STRING[sourceAccounts.Count];
+                Interop.Advapi32.MARSHALLED_UNICODE_STRING[] Names = new Interop.Advapi32.MARSHALLED_UNICODE_STRING[sourceAccounts.Count];
 
                 int currentName = 0;
                 foreach (IdentityReference id in sourceAccounts)
index 4e151b9..524a3e7 100644 (file)
@@ -40,33 +40,26 @@ namespace System.Security.Principal
             string systemName,
             PolicyRights rights)
         {
-            uint ReturnCode;
-            SafeLsaPolicyHandle Result;
-            Interop.LSA_OBJECT_ATTRIBUTES Loa;
-
-            Loa.Length = Marshal.SizeOf<Interop.LSA_OBJECT_ATTRIBUTES>();
-            Loa.RootDirectory = IntPtr.Zero;
-            Loa.ObjectName = IntPtr.Zero;
-            Loa.Attributes = 0;
-            Loa.SecurityDescriptor = IntPtr.Zero;
-            Loa.SecurityQualityOfService = IntPtr.Zero;
-
-            if (0 == (ReturnCode = Interop.Advapi32.LsaOpenPolicy(systemName, ref Loa, (int)rights, out Result)))
+            SafeLsaPolicyHandle policyHandle;
+
+            var attributes = new Interop.OBJECT_ATTRIBUTES();
+            uint error = Interop.Advapi32.LsaOpenPolicy(systemName, ref attributes, (int)rights, out policyHandle);
+            if (error == 0)
             {
-                return Result;
+                return policyHandle;
             }
-            else if (ReturnCode == Interop.StatusOptions.STATUS_ACCESS_DENIED)
+            else if (error == Interop.StatusOptions.STATUS_ACCESS_DENIED)
             {
                 throw new UnauthorizedAccessException();
             }
-            else if (ReturnCode == Interop.StatusOptions.STATUS_INSUFFICIENT_RESOURCES ||
-                      ReturnCode == Interop.StatusOptions.STATUS_NO_MEMORY)
+            else if (error == Interop.StatusOptions.STATUS_INSUFFICIENT_RESOURCES ||
+                      error == Interop.StatusOptions.STATUS_NO_MEMORY)
             {
                 throw new OutOfMemoryException();
             }
             else
             {
-                uint win32ErrorCode = Interop.Advapi32.LsaNtStatusToWinError(ReturnCode);
+                uint win32ErrorCode = Interop.Advapi32.LsaNtStatusToWinError(error);
 
                 throw new Win32Exception(unchecked((int)win32ErrorCode));
             }
index fa9f466..aef5d49 100644 (file)
@@ -16,7 +16,7 @@ using KERB_LOGON_SUBMIT_TYPE = Interop.SspiCli.KERB_LOGON_SUBMIT_TYPE;
 using KERB_S4U_LOGON = Interop.SspiCli.KERB_S4U_LOGON;
 using KerbS4uLogonFlags = Interop.SspiCli.KerbS4uLogonFlags;
 using LUID = Interop.LUID;
-using LSA_STRING = Interop.SspiCli.LSA_STRING;
+using LSA_STRING = Interop.Advapi32.LSA_STRING;
 using QUOTA_LIMITS = Interop.SspiCli.QUOTA_LIMITS;
 using SECURITY_LOGON_TYPE = Interop.SspiCli.SECURITY_LOGON_TYPE;
 using TOKEN_SOURCE = Interop.SspiCli.TOKEN_SOURCE;