Skip to content

Commit

Permalink
Fix incorrect definition of LSA_FOREST_TRUST_RECORD in System.Directo…
Browse files Browse the repository at this point in the history
…ryServices (dotnet#68274)
  • Loading branch information
elinor-fung committed Apr 21, 2022
1 parent 215b39a commit 0f98974
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -317,7 +315,7 @@ public void Save()
catch { throw; }
}

private void GetForestTrustInfoHelper()
private unsafe void GetForestTrustInfoHelper()
{
IntPtr forestTrustInfo = (IntPtr)0;
SafeLsaPolicyHandle? handle = null;
Expand Down Expand Up @@ -381,30 +379,30 @@ 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*)myPtr;
TopLevelName TLN = new TopLevelName(record.Flags, record.TopLevelName, record.Time);
tmpTLNs.Add(TLN);
}
else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelNameEx)
{
// 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*)myPtr;
string excludedName = Marshal.PtrToStringUni(record.TopLevelName.Buffer, record.TopLevelName.Length / 2);
tmpExcludedTLNs.Add(excludedName);
tmpExcludedNameTime.Add(excludedName, record.Time);
}
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*)myPtr;
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*)myPtr;
int length = record.Data.Length;
byte[] byteArray = new byte[length];
if ((record.Data.Buffer != (IntPtr)0) && (length != 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -432,8 +432,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;
Expand All @@ -444,8 +443,7 @@ internal sealed class LSA_FOREST_TRUST_DOMAIN_INFO
public IntPtr NetBIOSNameBuffer;
}

[StructLayout(LayoutKind.Sequential)]
internal sealed class LSA_FOREST_TRUST_BINARY_DATA
internal struct LSA_FOREST_TRUST_BINARY_DATA
{
public int Length;
public IntPtr Buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections;
using Xunit;
using Xunit.Sdk;
using System.Reflection;

namespace System.DirectoryServices.Tests
{
Expand All @@ -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
{
Expand Down

0 comments on commit 0f98974

Please sign in to comment.