From 64931e31355657e73c8a8327f758a3517b66129d Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Tue, 19 Apr 2022 21:44:28 -0700 Subject: [PATCH 1/2] Fix incorrect definition of LSA_FOREST_TRUST_RECORD in System.DirectoryServices --- .../ForestTrustRelationshipInformation.cs | 10 ++++------ .../ActiveDirectory/UnsafeNativeMethods.cs | 8 ++++---- .../System/DirectoryServices/DirectoryServicesTests.cs | 8 ++++++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs index 39b03a093ec9c5..c9e75e66e1ead7 100644 --- a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs +++ b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs @@ -187,7 +187,6 @@ public void Save() { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } - record.DomainInfo = new LSA_FOREST_TRUST_DOMAIN_INFO(); record.DomainInfo.sid = pSid; sidList.Add(pSid); record.DomainInfo.DNSNameBuffer = Marshal.StringToHGlobalUni(tmp.DnsName); @@ -214,7 +213,6 @@ public void Save() record.Time = (LARGE_INTEGER)_binaryDataTime[i]!; record.Data.Length = ((byte[])_binaryData[i]!).Length; record.ForestTrustType = (LSA_FOREST_TRUST_RECORD_TYPE)_binaryRecordType[i]!; - record.Data = new LSA_FOREST_TRUST_BINARY_DATA(); if (record.Data.Length == 0) { record.Data.Buffer = (IntPtr)0; @@ -381,7 +379,7 @@ private void GetForestTrustInfoHelper() if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelName) { IntPtr myPtr = IntPtr.Add(addr, 16); - Marshal.PtrToStructure(myPtr, record.TopLevelName); + record.TopLevelName = (global::Interop.UNICODE_STRING)Marshal.PtrToStructure(myPtr, typeof(global::Interop.UNICODE_STRING))!; TopLevelName TLN = new TopLevelName(record.Flags, record.TopLevelName, record.Time); tmpTLNs.Add(TLN); } @@ -389,7 +387,7 @@ private void GetForestTrustInfoHelper() { // get the excluded TLN and put it in our collection IntPtr myPtr = IntPtr.Add(addr, 16); - Marshal.PtrToStructure(myPtr, record.TopLevelName); + record.TopLevelName = (global::Interop.UNICODE_STRING)Marshal.PtrToStructure(myPtr, typeof(global::Interop.UNICODE_STRING))!; string excludedName = Marshal.PtrToStringUni(record.TopLevelName.Buffer, record.TopLevelName.Length / 2); tmpExcludedTLNs.Add(excludedName); tmpExcludedNameTime.Add(excludedName, record.Time); @@ -397,14 +395,14 @@ private void GetForestTrustInfoHelper() else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustDomainInfo) { IntPtr myPtr = IntPtr.Add(addr, 16); - Marshal.PtrToStructure(myPtr, record.DomainInfo!); + record.DomainInfo = (LSA_FOREST_TRUST_DOMAIN_INFO)Marshal.PtrToStructure(myPtr, typeof(LSA_FOREST_TRUST_DOMAIN_INFO))!; ForestTrustDomainInformation dom = new ForestTrustDomainInformation(record.Flags, record.DomainInfo!, record.Time); tmpDomainInformation.Add(dom); } else { IntPtr myPtr = IntPtr.Add(addr, 16); - Marshal.PtrToStructure(myPtr, record.Data); + record.Data = (LSA_FOREST_TRUST_BINARY_DATA)Marshal.PtrToStructure(myPtr, typeof(LSA_FOREST_TRUST_BINARY_DATA))!; int length = record.Data.Length; byte[] byteArray = new byte[length]; if ((record.Data.Buffer != (IntPtr)0) && (length != 0)) diff --git a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs index 2b490f41c755b5..6b9439a1c71a43 100644 --- a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs +++ b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs @@ -414,9 +414,9 @@ internal sealed class LSA_FOREST_TRUST_RECORD [FieldOffset(16)] public global::Interop.UNICODE_STRING TopLevelName; [FieldOffset(16)] - public LSA_FOREST_TRUST_BINARY_DATA Data = null!; + public LSA_FOREST_TRUST_BINARY_DATA Data; [FieldOffset(16)] - public LSA_FOREST_TRUST_DOMAIN_INFO? DomainInfo; + public LSA_FOREST_TRUST_DOMAIN_INFO DomainInfo; } [StructLayout(LayoutKind.Sequential)] @@ -433,7 +433,7 @@ public LARGE_INTEGER() } [StructLayout(LayoutKind.Sequential)] - internal sealed class LSA_FOREST_TRUST_DOMAIN_INFO + internal struct LSA_FOREST_TRUST_DOMAIN_INFO { public IntPtr sid; public short DNSNameLength; @@ -445,7 +445,7 @@ internal sealed class LSA_FOREST_TRUST_DOMAIN_INFO } [StructLayout(LayoutKind.Sequential)] - internal sealed class LSA_FOREST_TRUST_BINARY_DATA + internal struct LSA_FOREST_TRUST_BINARY_DATA { public int Length; public IntPtr Buffer; diff --git a/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs b/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs index 78c7e96a9fc3ed..94e923e8f9d513 100644 --- a/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs +++ b/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs @@ -6,6 +6,7 @@ using System.Collections; using Xunit; using Xunit.Sdk; +using System.Reflection; namespace System.DirectoryServices.Tests { @@ -14,6 +15,13 @@ public partial class DirectoryServicesTests internal static bool IsLdapConfigurationExist => LdapConfiguration.Configuration != null; internal static bool IsActiveDirectoryServer => IsLdapConfigurationExist && LdapConfiguration.Configuration.IsActiveDirectoryServer; + [Fact] + public void TestGetAllTypes() + { + Type[] allTypes = typeof(DirectoryEntry).Assembly.GetTypes(); + Assert.Contains(typeof(DirectoryEntry), allTypes); + } + [ConditionalFact(nameof(IsLdapConfigurationExist))] public void TestOU() // adding and removing organization unit { From 5c3739b8ea6603d93703610fa3e55fc21dad6c8e Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Wed, 20 Apr 2022 10:55:33 -0700 Subject: [PATCH 2/2] PR feedback --- .../ForestTrustRelationshipInformation.cs | 10 +++++----- .../ActiveDirectory/UnsafeNativeMethods.cs | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs index c9e75e66e1ead7..4e21c7b81b49ca 100644 --- a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs +++ b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs @@ -315,7 +315,7 @@ public void Save() catch { throw; } } - private void GetForestTrustInfoHelper() + private unsafe void GetForestTrustInfoHelper() { IntPtr forestTrustInfo = (IntPtr)0; SafeLsaPolicyHandle? handle = null; @@ -379,7 +379,7 @@ private void GetForestTrustInfoHelper() if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelName) { IntPtr myPtr = IntPtr.Add(addr, 16); - record.TopLevelName = (global::Interop.UNICODE_STRING)Marshal.PtrToStructure(myPtr, typeof(global::Interop.UNICODE_STRING))!; + record.TopLevelName = *(global::Interop.UNICODE_STRING*)myPtr; TopLevelName TLN = new TopLevelName(record.Flags, record.TopLevelName, record.Time); tmpTLNs.Add(TLN); } @@ -387,7 +387,7 @@ private void GetForestTrustInfoHelper() { // get the excluded TLN and put it in our collection IntPtr myPtr = IntPtr.Add(addr, 16); - record.TopLevelName = (global::Interop.UNICODE_STRING)Marshal.PtrToStructure(myPtr, typeof(global::Interop.UNICODE_STRING))!; + record.TopLevelName = *(global::Interop.UNICODE_STRING*)myPtr; string excludedName = Marshal.PtrToStringUni(record.TopLevelName.Buffer, record.TopLevelName.Length / 2); tmpExcludedTLNs.Add(excludedName); tmpExcludedNameTime.Add(excludedName, record.Time); @@ -395,14 +395,14 @@ private void GetForestTrustInfoHelper() else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustDomainInfo) { IntPtr myPtr = IntPtr.Add(addr, 16); - record.DomainInfo = (LSA_FOREST_TRUST_DOMAIN_INFO)Marshal.PtrToStructure(myPtr, typeof(LSA_FOREST_TRUST_DOMAIN_INFO))!; + record.DomainInfo = *(LSA_FOREST_TRUST_DOMAIN_INFO*)myPtr; ForestTrustDomainInformation dom = new ForestTrustDomainInformation(record.Flags, record.DomainInfo!, record.Time); tmpDomainInformation.Add(dom); } else { IntPtr myPtr = IntPtr.Add(addr, 16); - record.Data = (LSA_FOREST_TRUST_BINARY_DATA)Marshal.PtrToStructure(myPtr, typeof(LSA_FOREST_TRUST_BINARY_DATA))!; + record.Data = *(LSA_FOREST_TRUST_BINARY_DATA*)myPtr; int length = record.Data.Length; byte[] byteArray = new byte[length]; if ((record.Data.Buffer != (IntPtr)0) && (length != 0)) diff --git a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs index 6b9439a1c71a43..82b09367f78ef2 100644 --- a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs +++ b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs @@ -432,7 +432,6 @@ public LARGE_INTEGER() } } - [StructLayout(LayoutKind.Sequential)] internal struct LSA_FOREST_TRUST_DOMAIN_INFO { public IntPtr sid; @@ -444,7 +443,6 @@ internal struct LSA_FOREST_TRUST_DOMAIN_INFO public IntPtr NetBIOSNameBuffer; } - [StructLayout(LayoutKind.Sequential)] internal struct LSA_FOREST_TRUST_BINARY_DATA { public int Length;