Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Interop.cs
Go to the documentation of this file.
1using System;
5
6internal static class Interop
7{
8 internal enum BOOLEAN : byte
9 {
10 FALSE,
11 TRUE
12 }
13
14 internal struct SECURITY_QUALITY_OF_SERVICE
15 {
16 public uint Length;
17
18 public ImpersonationLevel ImpersonationLevel;
19
20 public ContextTrackingMode ContextTrackingMode;
21
22 public BOOLEAN EffectiveOnly;
23 }
24
25 public enum ImpersonationLevel : uint
26 {
31 }
32
33 public enum ContextTrackingMode : byte
34 {
35 Static,
36 Dynamic
37 }
38
39 internal struct UNICODE_STRING
40 {
41 internal ushort Length;
42
43 internal ushort MaximumLength;
44
45 internal IntPtr Buffer;
46 }
47
48 internal struct OBJECT_ATTRIBUTES
49 {
50 public uint Length;
51
52 public IntPtr RootDirectory;
53
54 public unsafe UNICODE_STRING* ObjectName;
55
56 public ObjectAttributes Attributes;
57
58 public unsafe void* SecurityDescriptor;
59
60 public unsafe SECURITY_QUALITY_OF_SERVICE* SecurityQualityOfService;
61 }
62
63 [Flags]
64 public enum ObjectAttributes : uint
65 {
66 OBJ_INHERIT = 2u,
67 OBJ_PERMANENT = 0x10u,
68 OBJ_EXCLUSIVE = 0x20u,
70 OBJ_OPENIF = 0x80u,
71 OBJ_OPENLINK = 0x100u
72 }
73
74 internal struct LUID
75 {
76 internal uint LowPart;
77
78 internal int HighPart;
79 }
80
81 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
82 internal struct TOKEN_GROUPS
83 {
84 internal uint GroupCount;
85
87 }
88
89 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
90 internal struct SID_AND_ATTRIBUTES
91 {
92 internal IntPtr Sid;
93
94 internal uint Attributes;
95 }
96
97 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
98 internal struct TOKEN_PRIMARY_GROUP
99 {
101 }
102
103 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
104 internal struct TOKEN_STATISTICS
105 {
106 internal LUID TokenId;
107
109
110 internal long ExpirationTime;
111
112 internal uint TokenType;
113
114 internal uint ImpersonationLevel;
115
116 internal uint DynamicCharged;
117
118 internal uint DynamicAvailable;
119
120 internal uint GroupCount;
121
122 internal uint PrivilegeCount;
123
124 internal LUID ModifiedId;
125 }
126
127 internal struct LSA_TRANSLATED_NAME
128 {
129 internal int Use;
130
132
133 internal int DomainIndex;
134 }
135
136 internal struct LSA_TRANSLATED_SID2
137 {
138 internal int Use;
139
140 internal IntPtr Sid;
141
142 internal int DomainIndex;
143
144 private uint Flags;
145 }
146
147 internal struct LSA_TRUST_INFORMATION
148 {
150
151 internal IntPtr Sid;
152 }
153
155 {
156 internal int Entries;
157
158 internal IntPtr Domains;
159 }
160
161 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
162 internal struct UNICODE_INTPTR_STRING
163 {
164 internal ushort Length;
165
166 internal ushort MaxLength;
167
168 internal IntPtr Buffer;
169 }
170
171 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
173 {
174 internal uint Size;
175
176 internal LUID LogonId;
177
179
181
183
184 internal uint LogonType;
185
186 internal uint Session;
187
188 internal IntPtr Sid;
189
190 internal long LogonTime;
191 }
192
193 internal static class Kernel32
194 {
195 [DllImport("kernel32.dll")]
196 internal static extern IntPtr GetCurrentProcess();
197
198 [DllImport("kernel32.dll")]
199 internal static extern IntPtr GetCurrentThread();
200
201 [DllImport("kernel32.dll", SetLastError = true)]
202 internal static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, IntPtr hSourceHandle, IntPtr hTargetProcessHandle, ref SafeAccessTokenHandle lpTargetHandle, uint dwDesiredAccess, bool bInheritHandle, uint dwOptions);
203
204 [DllImport("kernel32.dll", SetLastError = true)]
205 internal static extern bool CloseHandle(IntPtr handle);
206 }
207
208 [StructLayout(LayoutKind.Explicit)]
210 {
211 [FieldOffset(0)]
213 }
214
216 {
217 public ushort Version;
218
219 public ushort Reserved;
220
221 public uint AttributeCount;
222
224 }
225
226 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
228 {
229 [FieldOffset(0)]
231
232 [FieldOffset(0)]
234
235 [FieldOffset(0)]
237
238 [FieldOffset(0)]
239 public IntPtr pFqbn;
240
241 [FieldOffset(0)]
243 }
244
245 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
247 {
248 [MarshalAs(UnmanagedType.LPWStr)]
249 public string Name;
250
252
253 public ushort Reserved;
254
255 public uint Flags;
256
257 public uint ValueCount;
258
260 }
261
273
274 internal static class Advapi32
275 {
276 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
278 {
279 internal ushort Length;
280
281 internal ushort MaximumLength;
282
283 [MarshalAs(UnmanagedType.LPWStr)]
284 internal string Buffer;
285 }
286
287 internal struct LSA_STRING
288 {
289 internal ushort Length;
290
291 internal ushort MaximumLength;
292
293 internal IntPtr Buffer;
294
295 internal LSA_STRING(IntPtr pBuffer, ushort length)
296 {
297 Length = length;
299 Buffer = pBuffer;
300 }
301 }
302
303 [DllImport("advapi32.dll", SetLastError = true)]
304 internal static extern bool OpenProcessToken(IntPtr ProcessToken, TokenAccessLevels DesiredAccess, out SafeAccessTokenHandle TokenHandle);
305
306 [DllImport("advapi32.dll", SetLastError = true)]
307 internal static extern bool GetTokenInformation(SafeAccessTokenHandle TokenHandle, uint TokenInformationClass, SafeLocalAllocHandle TokenInformation, uint TokenInformationLength, out uint ReturnLength);
308
309 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
310 internal static extern bool GetTokenInformation(IntPtr TokenHandle, uint TokenInformationClass, IntPtr TokenInformation, uint TokenInformationLength, out uint ReturnLength);
311
312 [DllImport("advapi32.dll", SetLastError = true)]
313 internal static extern bool DuplicateTokenEx(SafeAccessTokenHandle hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, uint ImpersonationLevel, uint TokenType, ref SafeAccessTokenHandle phNewToken);
314
315 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
316 internal static extern uint LsaLookupNames2(SafeLsaPolicyHandle handle, int flags, int count, MARSHALLED_UNICODE_STRING[] names, out SafeLsaMemoryHandle referencedDomains, out SafeLsaMemoryHandle sids);
317
318 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
319 internal static extern uint LsaLookupSids(SafeLsaPolicyHandle handle, int count, IntPtr[] sids, out SafeLsaMemoryHandle referencedDomains, out SafeLsaMemoryHandle names);
320
321 [DllImport("advapi32.dll", SetLastError = true)]
322 internal static extern int LsaClose(IntPtr handle);
323
324 [DllImport("advapi32.dll", SetLastError = true)]
325 internal static extern int LsaFreeMemory(IntPtr handle);
326
327 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
328 private static extern uint LsaOpenPolicy(ref UNICODE_STRING SystemName, ref OBJECT_ATTRIBUTES ObjectAttributes, int AccessMask, out SafeLsaPolicyHandle PolicyHandle);
329
330 internal unsafe static uint LsaOpenPolicy(string SystemName, ref OBJECT_ATTRIBUTES Attributes, int AccessMask, out SafeLsaPolicyHandle PolicyHandle)
331 {
332 UNICODE_STRING SystemName2 = default(UNICODE_STRING);
333 checked
334 {
335 if (SystemName != null)
336 {
337 fixed (char* ptr = SystemName)
338 {
339 SystemName2.Length = (ushort)(SystemName.Length * 2);
340 SystemName2.MaximumLength = (ushort)(SystemName.Length * 2);
341 SystemName2.Buffer = (IntPtr)ptr;
342 return LsaOpenPolicy(ref SystemName2, ref Attributes, AccessMask, out PolicyHandle);
343 }
344 }
345 return LsaOpenPolicy(ref SystemName2, ref Attributes, AccessMask, out PolicyHandle);
346 }
347 }
348
349 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "ConvertStringSidToSidW", SetLastError = true)]
350 internal static extern int ConvertStringSidToSid(string stringSid, out IntPtr ByteArray);
351
352 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
353 internal static extern int CreateWellKnownSid(int sidType, byte[] domainSid, byte[] resultSid, ref uint resultSidLength);
354
355 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
356 internal static extern int GetWindowsAccountDomainSid(byte[] sid, byte[] resultSid, ref uint resultSidLength);
357
358 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
359 internal static extern int IsWellKnownSid(byte[] sid, int type);
360
361 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "EqualDomainSid", SetLastError = true)]
362 internal static extern int IsEqualDomainSid(byte[] sid1, byte[] sid2, out bool result);
363
364 [DllImport("advapi32.dll", SetLastError = true)]
365 private static extern bool OpenThreadToken(IntPtr ThreadHandle, TokenAccessLevels dwDesiredAccess, bool bOpenAsSelf, out SafeAccessTokenHandle phThreadToken);
366
367 internal static bool OpenThreadToken(TokenAccessLevels desiredAccess, WinSecurityContext openAs, out SafeAccessTokenHandle tokenHandle)
368 {
369 bool bOpenAsSelf = true;
370 if (openAs == WinSecurityContext.Thread)
371 {
372 bOpenAsSelf = false;
373 }
374 if (OpenThreadToken(Kernel32.GetCurrentThread(), desiredAccess, bOpenAsSelf, out tokenHandle))
375 {
376 return true;
377 }
378 if (openAs == WinSecurityContext.Both)
379 {
380 bOpenAsSelf = false;
381 if (OpenThreadToken(Kernel32.GetCurrentThread(), desiredAccess, bOpenAsSelf, out tokenHandle))
382 {
383 return true;
384 }
385 }
386 return false;
387 }
388
389 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
390 internal static extern bool RevertToSelf();
391
392 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
393 internal static extern bool ImpersonateLoggedOnUser(SafeAccessTokenHandle userToken);
394
395 [DllImport("advapi32.dll")]
396 internal static extern uint LsaNtStatusToWinError(uint status);
397
398 [DllImport("advapi32.dll")]
399 internal static extern bool AllocateLocallyUniqueId(out LUID Luid);
400
401 [DllImport("advapi32.dll", SetLastError = true)]
402 internal static extern bool CheckTokenMembership(SafeAccessTokenHandle TokenHandle, byte[] SidToCheck, ref bool IsMember);
403 }
404
405 internal static class SspiCli
406 {
408 {
409 KerbS4ULogon = 12
410 }
411
422
423 [Flags]
430
431 internal struct QUOTA_LIMITS
432 {
434
436
438
440
442
443 internal long TimeLimit;
444 }
445
447 {
448 Network = 3
449 }
450
451 internal struct TOKEN_SOURCE
452 {
453 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
454 internal byte[] SourceName;
455
457 }
458
459 [DllImport("sspicli.dll", SetLastError = true)]
460 internal static extern int LsaGetLogonSessionData(ref LUID LogonId, out SafeLsaReturnBufferHandle ppLogonSessionData);
461
462 [DllImport("sspicli.dll", SetLastError = true)]
463 internal static extern int LsaFreeReturnBuffer(IntPtr handle);
464
465 [DllImport("sspicli.dll")]
466 internal static extern int LsaConnectUntrusted(out SafeLsaHandle LsaHandle);
467
468 [DllImport("sspicli.dll")]
469 internal static extern int LsaDeregisterLogonProcess(IntPtr LsaHandle);
470
471 [DllImport("sspicli.dll")]
472 internal static extern int LsaLogonUser([In] SafeLsaHandle LsaHandle, [In] ref Advapi32.LSA_STRING OriginName, [In] SECURITY_LOGON_TYPE LogonType, [In] int AuthenticationPackage, [In] IntPtr AuthenticationInformation, [In] int AuthenticationInformationLength, [In] IntPtr LocalGroups, [In] ref TOKEN_SOURCE SourceContext, out SafeLsaReturnBufferHandle ProfileBuffer, out int ProfileBufferLength, out LUID LogonId, out SafeAccessTokenHandle Token, out QUOTA_LIMITS Quotas, out int SubStatus);
473
474 [DllImport("sspicli.dll")]
475 internal static extern int LsaLookupAuthenticationPackage(SafeLsaHandle LsaHandle, [In] ref Advapi32.LSA_STRING PackageName, out int AuthenticationPackage);
476 }
477}
static int IsWellKnownSid(byte[] sid, int type)
static int LsaFreeMemory(IntPtr handle)
static int CreateWellKnownSid(int sidType, byte[] domainSid, byte[] resultSid, ref uint resultSidLength)
static int LsaClose(IntPtr handle)
static int IsEqualDomainSid(byte[] sid1, byte[] sid2, out bool result)
static bool OpenProcessToken(IntPtr ProcessToken, TokenAccessLevels DesiredAccess, out SafeAccessTokenHandle TokenHandle)
static uint LsaLookupNames2(SafeLsaPolicyHandle handle, int flags, int count, MARSHALLED_UNICODE_STRING[] names, out SafeLsaMemoryHandle referencedDomains, out SafeLsaMemoryHandle sids)
static bool OpenThreadToken(IntPtr ThreadHandle, TokenAccessLevels dwDesiredAccess, bool bOpenAsSelf, out SafeTokenHandle phThreadToken)
static bool GetTokenInformation(SafeAccessTokenHandle TokenHandle, uint TokenInformationClass, SafeLocalAllocHandle TokenInformation, uint TokenInformationLength, out uint ReturnLength)
static bool DuplicateTokenEx(SafeAccessTokenHandle hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, uint ImpersonationLevel, uint TokenType, ref SafeAccessTokenHandle phNewToken)
static bool ImpersonateLoggedOnUser(SafeAccessTokenHandle userToken)
static bool OpenThreadToken(IntPtr ThreadHandle, TokenAccessLevels dwDesiredAccess, bool bOpenAsSelf, out SafeAccessTokenHandle phThreadToken)
static bool CheckTokenMembership(SafeAccessTokenHandle TokenHandle, byte[] SidToCheck, ref bool IsMember)
static bool GetTokenInformation(IntPtr TokenHandle, uint TokenInformationClass, IntPtr TokenInformation, uint TokenInformationLength, out uint ReturnLength)
static bool AllocateLocallyUniqueId(out LUID Luid)
static int GetWindowsAccountDomainSid(byte[] sid, byte[] resultSid, ref uint resultSidLength)
static uint LsaLookupSids(SafeLsaPolicyHandle handle, int count, IntPtr[] sids, out SafeLsaMemoryHandle referencedDomains, out SafeLsaMemoryHandle names)
static unsafe uint LsaOpenPolicy(string SystemName, ref OBJECT_ATTRIBUTES Attributes, int AccessMask, out SafeLsaPolicyHandle PolicyHandle)
Definition Interop.cs:330
static uint LsaOpenPolicy(ref UNICODE_STRING SystemName, ref OBJECT_ATTRIBUTES ObjectAttributes, int AccessMask, out SafeLsaPolicyHandle PolicyHandle)
static bool RevertToSelf()
static uint LsaNtStatusToWinError(uint status)
static bool OpenThreadToken(TokenAccessLevels desiredAccess, WinSecurityContext openAs, out SafeAccessTokenHandle tokenHandle)
Definition Interop.cs:367
static int ConvertStringSidToSid(string stringSid, out IntPtr ByteArray)
static void SetLastError(int errorCode)
static IntPtr GetCurrentThread()
static bool CloseHandle(IntPtr handle)
static bool DuplicateHandle(IntPtr hSourceProcessHandle, IntPtr hSourceHandle, IntPtr hTargetProcessHandle, ref SafeAccessTokenHandle lpTargetHandle, uint dwDesiredAccess, bool bInheritHandle, uint dwOptions)
static IntPtr GetCurrentProcess()
static int LsaLookupAuthenticationPackage(SafeLsaHandle LsaHandle, [In] ref Advapi32.LSA_STRING PackageName, out int AuthenticationPackage)
static int LsaGetLogonSessionData(ref LUID LogonId, out SafeLsaReturnBufferHandle ppLogonSessionData)
static int LsaLogonUser([In] SafeLsaHandle LsaHandle, [In] ref Advapi32.LSA_STRING OriginName, [In] SECURITY_LOGON_TYPE LogonType, [In] int AuthenticationPackage, [In] IntPtr AuthenticationInformation, [In] int AuthenticationInformationLength, [In] IntPtr LocalGroups, [In] ref TOKEN_SOURCE SourceContext, out SafeLsaReturnBufferHandle ProfileBuffer, out int ProfileBufferLength, out LUID LogonId, out SafeAccessTokenHandle Token, out QUOTA_LIMITS Quotas, out int SubStatus)
static int LsaConnectUntrusted(out SafeLsaHandle LsaHandle)
static int LsaFreeReturnBuffer(IntPtr handle)
static int LsaDeregisterLogonProcess(IntPtr LsaHandle)
ObjectAttributes
Definition Interop.cs:65
ContextTrackingMode
Definition Interop.cs:34
ClaimSecurityAttributeType
Definition Interop.cs:263
BOOLEAN
Definition Interop.cs:9
ImpersonationLevel
Definition Interop.cs:26
TokenType
Definition TokenType.cs:4
LSA_STRING(IntPtr pBuffer, ushort length)
Definition Interop.cs:295
CLAIM_SECURITY_ATTRIBUTE_INFORMATION_V1 Attribute
Definition Interop.cs:223
CLAIM_VALUES_ATTRIBUTE_V1 Values
Definition Interop.cs:259
ClaimSecurityAttributeType ValueType
Definition Interop.cs:251
UNICODE_INTPTR_STRING Name
Definition Interop.cs:131
UNICODE_INTPTR_STRING Name
Definition Interop.cs:149
int HighPart
Definition Interop.cs:78
uint LowPart
Definition Interop.cs:76
unsafe void * SecurityDescriptor
Definition Interop.cs:1733
unsafe SECURITY_QUALITY_OF_SERVICE * SecurityQualityOfService
Definition Interop.cs:1735
unsafe UNICODE_STRING * ObjectName
Definition Interop.cs:1729
ObjectAttributes Attributes
Definition Interop.cs:1731
UNICODE_INTPTR_STRING AuthenticationPackage
Definition Interop.cs:182
UNICODE_INTPTR_STRING UserName
Definition Interop.cs:178
UNICODE_INTPTR_STRING LogonDomain
Definition Interop.cs:180
ImpersonationLevel ImpersonationLevel
Definition Interop.cs:1702
ContextTrackingMode ContextTrackingMode
Definition Interop.cs:1704
KERB_LOGON_SUBMIT_TYPE MessageType
Definition Interop.cs:414
KerbS4uLogonFlags Flags
Definition Interop.cs:416
SID_AND_ATTRIBUTES Groups
Definition Interop.cs:86