Skip to content

Commit ec7b11d

Browse files
authored
Make System.DirectoryServices.AccountManagement use GeneratedDllImport (#61805)
1 parent f45dfdc commit ec7b11d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+703
-695
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Activeds
10+
{
11+
[DllImport(Interop.Libraries.Activeds, CharSet = CharSet.Unicode, ExactSpelling = true)]
12+
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);
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Advapi32
10+
{
11+
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "ConvertSidToStringSidW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
12+
internal static partial BOOL ConvertSidToStringSid(IntPtr sid, ref string stringSid);
13+
}
14+
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ConvertStringSidToSid.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal static partial class Interop
99
internal static partial class Advapi32
1010
{
1111
[GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "ConvertStringSidToSidW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
12-
internal static partial int ConvertStringSidToSid(
12+
internal static partial BOOL ConvertStringSidToSid(
1313
string stringSid,
1414
out IntPtr ByteArray);
1515
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Advapi32
10+
{
11+
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
12+
internal static partial bool CopySid(int destinationLength, IntPtr pSidDestination, IntPtr pSidSource);
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Advapi32
10+
{
11+
[GeneratedDllImport(Interop.Libraries.Advapi32)]
12+
public static partial bool EqualDomainSid(IntPtr pSid1, IntPtr pSid2, ref bool equal);
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Advapi32
10+
{
11+
[GeneratedDllImport(Interop.Libraries.Advapi32)]
12+
internal static partial int GetLengthSid(IntPtr sid);
13+
}
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Advapi32
10+
{
11+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
12+
internal struct SID_IDENTIFIER_AUTHORITY
13+
{
14+
public byte b1;
15+
public byte b2;
16+
public byte b3;
17+
public byte b4;
18+
public byte b5;
19+
public byte b6;
20+
}
21+
22+
[GeneratedDllImport(Libraries.Advapi32)]
23+
internal static partial IntPtr GetSidIdentifierAuthority(IntPtr sid);
24+
25+
[GeneratedDllImport(Interop.Libraries.Advapi32)]
26+
internal static partial IntPtr GetSidSubAuthority(IntPtr sid, int index);
27+
28+
[GeneratedDllImport(Interop.Libraries.Advapi32)]
29+
internal static partial IntPtr GetSidSubAuthorityCount(IntPtr sid);
30+
}
31+
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.GetTokenInformation.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,6 @@ internal static partial class Interop
99
{
1010
internal static partial class Advapi32
1111
{
12-
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
13-
internal static partial bool GetTokenInformation(
14-
SafeAccessTokenHandle TokenHandle,
15-
uint TokenInformationClass,
16-
SafeLocalAllocHandle TokenInformation,
17-
uint TokenInformationLength,
18-
out uint ReturnLength);
19-
20-
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
21-
internal static partial bool GetTokenInformation(
22-
IntPtr TokenHandle,
23-
uint TokenInformationClass,
24-
SafeLocalAllocHandle TokenInformation,
25-
uint TokenInformationLength,
26-
out uint ReturnLength);
27-
2812
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
2913
internal static partial bool GetTokenInformation(
3014
IntPtr TokenHandle,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.Win32.SafeHandles;
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
internal static partial class Interop
9+
{
10+
internal static partial class Advapi32
11+
{
12+
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
13+
internal static partial bool GetTokenInformation(
14+
SafeAccessTokenHandle TokenHandle,
15+
uint TokenInformationClass,
16+
SafeLocalAllocHandle TokenInformation,
17+
uint TokenInformationLength,
18+
out uint ReturnLength);
19+
20+
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
21+
internal static partial bool GetTokenInformation(
22+
IntPtr TokenHandle,
23+
uint TokenInformationClass,
24+
SafeLocalAllocHandle TokenInformation,
25+
uint TokenInformationLength,
26+
out uint ReturnLength);
27+
}
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Advapi32
10+
{
11+
[GeneratedDllImport(Libraries.Advapi32, SetLastError = true)]
12+
internal static partial int ImpersonateLoggedOnUser(IntPtr userToken);
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Advapi32
10+
{
11+
[GeneratedDllImport(Libraries.Advapi32, SetLastError = true)]
12+
internal static partial bool IsValidSid(IntPtr sid);
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Advapi32
10+
{
11+
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "LogonUserW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
12+
internal static partial int LogonUser(
13+
string username,
14+
string domain,
15+
string password,
16+
int logonType,
17+
int logonProvider,
18+
ref IntPtr token);
19+
}
20+
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LookupAccountSid.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ internal static partial class Interop
88
{
99
internal static partial class Advapi32
1010
{
11-
[DllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, EntryPoint = "LookupAccountSidW", SetLastError = true)]
12-
public static extern unsafe int LookupAccountSid(
11+
[GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "LookupAccountSidW", CharSet = CharSet.Unicode, SetLastError = true)]
12+
public static unsafe partial int LookupAccountSid(
1313
string lpSystemName,
1414
byte[] Sid,
1515
char* Name,

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaLookupSids.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,39 @@ out SafeLsaMemoryHandle names
1919
);
2020
}
2121
}
22+
23+
internal static partial class SafeLsaMemoryHandleExtensions
24+
{
25+
public static unsafe void InitializeReferencedDomainsList(this SafeLsaMemoryHandle referencedDomains)
26+
{
27+
// We don't know the real size of the referenced domains yet, so we need to set an initial
28+
// size based on the LSA_REFERENCED_DOMAIN_LIST structure, then resize it to include all of
29+
// the domains.
30+
referencedDomains.Initialize((uint)Marshal.SizeOf<Interop.LSA_REFERENCED_DOMAIN_LIST>());
31+
Interop.LSA_REFERENCED_DOMAIN_LIST domainList = referencedDomains.Read<Interop.LSA_REFERENCED_DOMAIN_LIST>(0);
32+
33+
byte* pRdl = null;
34+
try
35+
{
36+
referencedDomains.AcquirePointer(ref pRdl);
37+
38+
// If there is a trust information list, then the buffer size is the end of that list minus
39+
// the beginning of the domain list. Otherwise, then the buffer is just the size of the
40+
// referenced domain list structure, which is what we defaulted to.
41+
if (domainList.Domains != IntPtr.Zero)
42+
{
43+
Interop.LSA_TRUST_INFORMATION* pTrustInformation = (Interop.LSA_TRUST_INFORMATION*)domainList.Domains;
44+
pTrustInformation = pTrustInformation + domainList.Entries;
45+
46+
long bufferSize = (byte*)pTrustInformation - pRdl;
47+
System.Diagnostics.Debug.Assert(bufferSize > 0, "bufferSize > 0");
48+
referencedDomains.Initialize((ulong)bufferSize);
49+
}
50+
}
51+
finally
52+
{
53+
if (pRdl != null)
54+
referencedDomains.ReleasePointer();
55+
}
56+
}
57+
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaOpenPolicy.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ internal static partial class Interop
99
{
1010
internal static partial class Advapi32
1111
{
12+
[Flags]
13+
internal enum PolicyRights
14+
{
15+
POLICY_VIEW_LOCAL_INFORMATION = 0x00000001,
16+
POLICY_VIEW_AUDIT_INFORMATION = 0x00000002,
17+
POLICY_GET_PRIVATE_INFORMATION = 0x00000004,
18+
POLICY_TRUST_ADMIN = 0x00000008,
19+
POLICY_CREATE_ACCOUNT = 0x00000010,
20+
POLICY_CREATE_SECRET = 0x00000020,
21+
POLICY_CREATE_PRIVILEGE = 0x00000040,
22+
POLICY_SET_DEFAULT_QUOTA_LIMITS = 0x00000080,
23+
POLICY_SET_AUDIT_REQUIREMENTS = 0x00000100,
24+
POLICY_AUDIT_LOG_ADMIN = 0x00000200,
25+
POLICY_SERVER_ADMIN = 0x00000400,
26+
POLICY_LOOKUP_NAMES = 0x00000800,
27+
POLICY_NOTIFICATION = 0x00001000,
28+
}
29+
1230
[GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "LsaOpenPolicy", SetLastError = true)]
1331
private static partial uint LsaOpenPolicy(
1432
ref UNICODE_STRING SystemName,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Advapi32
10+
{
11+
[GeneratedDllImport(Libraries.Advapi32)]
12+
internal static partial uint LsaQueryInformationPolicy(
13+
IntPtr policyHandle,
14+
int policyInformationClass,
15+
ref IntPtr buffer);
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Authz
10+
{
11+
[GeneratedDllImport(Libraries.Authz, SetLastError = true)]
12+
internal static partial bool AuthzGetInformationFromContext(
13+
IntPtr hAuthzClientContext,
14+
int InfoClass,
15+
int BufferSize,
16+
out int pSizeRequired,
17+
IntPtr Buffer);
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Authz
10+
{
11+
internal const int AUTHZ_RM_FLAG_NO_AUDIT = 0x1;
12+
internal const int AUTHZ_RM_FLAG_INITIALIZE_UNDER_IMPERSONATION = 0x2;
13+
internal const int AUTHZ_VALID_RM_INIT_FLAGS = (AUTHZ_RM_FLAG_NO_AUDIT | AUTHZ_RM_FLAG_INITIALIZE_UNDER_IMPERSONATION);
14+
15+
[GeneratedDllImport(Interop.Libraries.Authz, SetLastError = true)]
16+
internal static partial bool AuthzInitializeContextFromSid(
17+
int Flags,
18+
IntPtr UserSid,
19+
IntPtr AuthzResourceManager,
20+
IntPtr pExpirationTime,
21+
Interop.LUID Identitifier,
22+
IntPtr DynamicGroupArgs,
23+
out IntPtr pAuthzClientContext);
24+
25+
[GeneratedDllImport(Interop.Libraries.Authz)]
26+
internal static partial bool AuthzFreeContext(IntPtr AuthzClientContext);
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Authz
10+
{
11+
[GeneratedDllImport(Libraries.Authz, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
12+
internal static partial bool AuthzInitializeResourceManager(
13+
int flags,
14+
IntPtr pfnAccessCheck,
15+
IntPtr pfnComputeDynamicGroups,
16+
IntPtr pfnFreeDynamicGroups,
17+
string name,
18+
out IntPtr rm);
19+
20+
[GeneratedDllImport(Libraries.Authz)]
21+
internal static partial bool AuthzFreeResourceManager(IntPtr rm);
22+
}
23+
}

0 commit comments

Comments
 (0)