Skip to content

Commit

Permalink
Make System.DirectoryServices.AccountManagement use GeneratedDllImport (
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung authored Nov 22, 2021
1 parent f45dfdc commit ec7b11d
Show file tree
Hide file tree
Showing 43 changed files with 703 additions and 695 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Activeds
{
[DllImport(Interop.Libraries.Activeds, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int ADsOpenObject(string path, string userName, string password, int flags, [In, Out] ref Guid iid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Advapi32
{
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "ConvertSidToStringSidW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial BOOL ConvertSidToStringSid(IntPtr sid, ref string stringSid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static partial class Interop
internal static partial class Advapi32
{
[GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "ConvertStringSidToSidW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial int ConvertStringSidToSid(
internal static partial BOOL ConvertStringSidToSid(
string stringSid,
out IntPtr ByteArray);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Advapi32
{
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static partial bool CopySid(int destinationLength, IntPtr pSidDestination, IntPtr pSidSource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Advapi32
{
[GeneratedDllImport(Interop.Libraries.Advapi32)]
public static partial bool EqualDomainSid(IntPtr pSid1, IntPtr pSid2, ref bool equal);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Advapi32
{
[GeneratedDllImport(Interop.Libraries.Advapi32)]
internal static partial int GetLengthSid(IntPtr sid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Advapi32
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct SID_IDENTIFIER_AUTHORITY
{
public byte b1;
public byte b2;
public byte b3;
public byte b4;
public byte b5;
public byte b6;
}

[GeneratedDllImport(Libraries.Advapi32)]
internal static partial IntPtr GetSidIdentifierAuthority(IntPtr sid);

[GeneratedDllImport(Interop.Libraries.Advapi32)]
internal static partial IntPtr GetSidSubAuthority(IntPtr sid, int index);

[GeneratedDllImport(Interop.Libraries.Advapi32)]
internal static partial IntPtr GetSidSubAuthorityCount(IntPtr sid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static partial bool GetTokenInformation(
SafeAccessTokenHandle TokenHandle,
uint TokenInformationClass,
SafeLocalAllocHandle TokenInformation,
uint TokenInformationLength,
out uint ReturnLength);

[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static partial bool GetTokenInformation(
IntPtr TokenHandle,
uint TokenInformationClass,
SafeLocalAllocHandle TokenInformation,
uint TokenInformationLength,
out uint ReturnLength);

[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static partial bool GetTokenInformation(
IntPtr TokenHandle,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Advapi32
{
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static partial bool GetTokenInformation(
SafeAccessTokenHandle TokenHandle,
uint TokenInformationClass,
SafeLocalAllocHandle TokenInformation,
uint TokenInformationLength,
out uint ReturnLength);

[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static partial bool GetTokenInformation(
IntPtr TokenHandle,
uint TokenInformationClass,
SafeLocalAllocHandle TokenInformation,
uint TokenInformationLength,
out uint ReturnLength);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Advapi32
{
[GeneratedDllImport(Libraries.Advapi32, SetLastError = true)]
internal static partial int ImpersonateLoggedOnUser(IntPtr userToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Advapi32
{
[GeneratedDllImport(Libraries.Advapi32, SetLastError = true)]
internal static partial bool IsValidSid(IntPtr sid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Advapi32
{
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "LogonUserW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial int LogonUser(
string username,
string domain,
string password,
int logonType,
int logonProvider,
ref IntPtr token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, EntryPoint = "LookupAccountSidW", SetLastError = true)]
public static extern unsafe int LookupAccountSid(
[GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "LookupAccountSidW", CharSet = CharSet.Unicode, SetLastError = true)]
public static unsafe partial int LookupAccountSid(
string lpSystemName,
byte[] Sid,
char* Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,39 @@ out SafeLsaMemoryHandle names
);
}
}

internal static partial class SafeLsaMemoryHandleExtensions
{
public static unsafe void InitializeReferencedDomainsList(this SafeLsaMemoryHandle referencedDomains)
{
// We don't know the real size of the referenced domains yet, so we need to set an initial
// size based on the LSA_REFERENCED_DOMAIN_LIST structure, then resize it to include all of
// the domains.
referencedDomains.Initialize((uint)Marshal.SizeOf<Interop.LSA_REFERENCED_DOMAIN_LIST>());
Interop.LSA_REFERENCED_DOMAIN_LIST domainList = referencedDomains.Read<Interop.LSA_REFERENCED_DOMAIN_LIST>(0);

byte* pRdl = null;
try
{
referencedDomains.AcquirePointer(ref pRdl);

// If there is a trust information list, then the buffer size is the end of that list minus
// the beginning of the domain list. Otherwise, then the buffer is just the size of the
// referenced domain list structure, which is what we defaulted to.
if (domainList.Domains != IntPtr.Zero)
{
Interop.LSA_TRUST_INFORMATION* pTrustInformation = (Interop.LSA_TRUST_INFORMATION*)domainList.Domains;
pTrustInformation = pTrustInformation + domainList.Entries;

long bufferSize = (byte*)pTrustInformation - pRdl;
System.Diagnostics.Debug.Assert(bufferSize > 0, "bufferSize > 0");
referencedDomains.Initialize((ulong)bufferSize);
}
}
finally
{
if (pRdl != null)
referencedDomains.ReleasePointer();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[Flags]
internal enum PolicyRights
{
POLICY_VIEW_LOCAL_INFORMATION = 0x00000001,
POLICY_VIEW_AUDIT_INFORMATION = 0x00000002,
POLICY_GET_PRIVATE_INFORMATION = 0x00000004,
POLICY_TRUST_ADMIN = 0x00000008,
POLICY_CREATE_ACCOUNT = 0x00000010,
POLICY_CREATE_SECRET = 0x00000020,
POLICY_CREATE_PRIVILEGE = 0x00000040,
POLICY_SET_DEFAULT_QUOTA_LIMITS = 0x00000080,
POLICY_SET_AUDIT_REQUIREMENTS = 0x00000100,
POLICY_AUDIT_LOG_ADMIN = 0x00000200,
POLICY_SERVER_ADMIN = 0x00000400,
POLICY_LOOKUP_NAMES = 0x00000800,
POLICY_NOTIFICATION = 0x00001000,
}

[GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "LsaOpenPolicy", SetLastError = true)]
private static partial uint LsaOpenPolicy(
ref UNICODE_STRING SystemName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Advapi32
{
[GeneratedDllImport(Libraries.Advapi32)]
internal static partial uint LsaQueryInformationPolicy(
IntPtr policyHandle,
int policyInformationClass,
ref IntPtr buffer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Authz
{
[GeneratedDllImport(Libraries.Authz, SetLastError = true)]
internal static partial bool AuthzGetInformationFromContext(
IntPtr hAuthzClientContext,
int InfoClass,
int BufferSize,
out int pSizeRequired,
IntPtr Buffer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Authz
{
internal const int AUTHZ_RM_FLAG_NO_AUDIT = 0x1;
internal const int AUTHZ_RM_FLAG_INITIALIZE_UNDER_IMPERSONATION = 0x2;
internal const int AUTHZ_VALID_RM_INIT_FLAGS = (AUTHZ_RM_FLAG_NO_AUDIT | AUTHZ_RM_FLAG_INITIALIZE_UNDER_IMPERSONATION);

[GeneratedDllImport(Interop.Libraries.Authz, SetLastError = true)]
internal static partial bool AuthzInitializeContextFromSid(
int Flags,
IntPtr UserSid,
IntPtr AuthzResourceManager,
IntPtr pExpirationTime,
Interop.LUID Identitifier,
IntPtr DynamicGroupArgs,
out IntPtr pAuthzClientContext);

[GeneratedDllImport(Interop.Libraries.Authz)]
internal static partial bool AuthzFreeContext(IntPtr AuthzClientContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Authz
{
[GeneratedDllImport(Libraries.Authz, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial bool AuthzInitializeResourceManager(
int flags,
IntPtr pfnAccessCheck,
IntPtr pfnComputeDynamicGroups,
IntPtr pfnFreeDynamicGroups,
string name,
out IntPtr rm);

[GeneratedDllImport(Libraries.Authz)]
internal static partial bool AuthzFreeResourceManager(IntPtr rm);
}
}
Loading

0 comments on commit ec7b11d

Please sign in to comment.