MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4// NOTE: A vast majority of this code was copied from BCL in5// Namespace: Microsoft.Win326//7/*8 * Notes to PInvoke users: Getting the syntax exactly correct is crucial, and9 * more than a little confusing. Here's some guidelines.10 *11 * For handles, you should use a SafeHandle subclass specific to your handle12 * type.13*/14 15namespace Microsoft.PowerShell.Commands.Internal16{17 using System;18 using System.Security;19 using System.Runtime.InteropServices;20 using System.Runtime.Versioning;21 using System.Management.Automation;22 using System.Diagnostics.CodeAnalysis;23 24 /**25 * Win32 encapsulation for MSCORLIB.26 */27 // Remove the default demands for all N/Direct methods with this28 // global declaration on the class.29 //30 [SuppressUnmanagedCodeSecurity]31 internal static class Win32Native32 {33 #region Integer Const34 35 internal const int ERROR_INSUFFICIENT_BUFFER = 0x7A;36 37 #endregion Integer Const38 39 #region Enum40 41 internal enum TOKEN_INFORMATION_CLASS42 {43 TokenUser = 1,44 TokenGroups,45 TokenPrivileges,46 TokenOwner,47 TokenPrimaryGroup,48 TokenDefaultDacl,49 TokenSource,50 TokenType,51 TokenImpersonationLevel,52 TokenStatistics,53 TokenRestrictedSids,54 TokenSessionId,55 TokenGroupsAndPrivileges,56 TokenSessionReference,57 TokenSandBoxInert,58 TokenAuditPolicy,59 TokenOrigin60 }61 62 internal enum SID_NAME_USE63 {64 SidTypeUser = 1,65 SidTypeGroup,66 SidTypeDomain,67 SidTypeAlias,68 SidTypeWellKnownGroup,69 SidTypeDeletedAccount,70 SidTypeInvalid,71 SidTypeUnknown,72 SidTypeComputer73 }74 75 #endregion Enum76 77 #region Struct78 79 [StructLayout(LayoutKind.Sequential)]80 internal struct SID_AND_ATTRIBUTES81 {82 internal IntPtr Sid;83 internal uint Attributes;84 }85 86 [StructLayout(LayoutKind.Sequential)]87 internal struct TOKEN_USER88 {89 internal SID_AND_ATTRIBUTES User;90 }91 92 #endregion Struct93 94 #region PInvoke methods95 96 /// <summary>97 /// The LookupAccountSid function accepts a security identifier (SID) as input. It retrieves the name98 /// of the account for this SID and the name of the first domain on which this SID is found.99 /// </summary>100 /// <param name="lpSystemName"></param>101 /// <param name="sid"></param>102 /// <param name="lpName"></param>103 /// <param name="cchName"></param>104 /// <param name="referencedDomainName"></param>105 /// <param name="cchReferencedDomainName"></param>106 /// <param name="peUse"></param>107 /// <returns></returns>108 [DllImport(PinvokeDllNames.LookupAccountSidDllName, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]109 [return: MarshalAs(UnmanagedType.Bool)]110 private static extern unsafe bool LookupAccountSid(string lpSystemName,111 IntPtr sid,112 char* lpName,113 ref int cchName,114 char* referencedDomainName,115 ref int cchReferencedDomainName,116 out SID_NAME_USE peUse);117 118 internal static unsafe bool LookupAccountSid(string lpSystemName,119 IntPtr sid,120 Span<char> userName,121 ref int cchName,122 Span<char> domainName,123 ref int cchDomainName,124 out SID_NAME_USE peUse)125 {126 fixed (char* userNamePtr = &MemoryMarshal.GetReference(userName))127 fixed (char* domainNamePtr = &MemoryMarshal.GetReference(domainName))128 {129 return LookupAccountSid(lpSystemName,130 sid,131 userNamePtr,132 ref cchName,133 domainNamePtr,134 ref cchDomainName,135 out peUse);136 }137 }138 139 [DllImport(PinvokeDllNames.CloseHandleDllName, SetLastError = true)]140 [return: MarshalAs(UnmanagedType.Bool)]141 internal static extern bool CloseHandle(IntPtr handle);142 143 /// <summary>144 /// Retrieves the current process token.145 /// </summary>146 /// <param name="processHandle">Process handle.</param>147 /// <param name="desiredAccess">Token access.</param>148 /// <param name="tokenHandle">Process token.</param>149 /// <returns>The current process token.</returns>150 [DllImport(PinvokeDllNames.OpenProcessTokenDllName, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]151 [return: MarshalAs(UnmanagedType.Bool)]152 internal static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccess, out IntPtr tokenHandle);153 154 /// <summary>155 /// The GetTokenInformation function retrieves a specified type of information about an access token.156 /// The calling process must have appropriate access rights to obtain the information.157 /// </summary>158 /// <param name="tokenHandle"></param>159 /// <param name="tokenInformationClass"></param>160 /// <param name="tokenInformation"></param>161 /// <param name="tokenInformationLength"></param>162 /// <param name="returnLength"></param>163 /// <returns></returns>164 [DllImport(PinvokeDllNames.GetTokenInformationDllName, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]165 [return: MarshalAs(UnmanagedType.Bool)]166 internal static extern bool GetTokenInformation(IntPtr tokenHandle,167 TOKEN_INFORMATION_CLASS tokenInformationClass,168 IntPtr tokenInformation,169 int tokenInformationLength,170 out int returnLength);171 172 #endregion PInvoke Methods173 }174}175 