advapi32.odin 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #+build windows
  2. package sys_windows
  3. foreign import advapi32 "system:Advapi32.lib"
  4. HCRYPTPROV :: distinct HANDLE
  5. @(default_calling_convention="system")
  6. foreign advapi32 {
  7. @(link_name = "SystemFunction036")
  8. RtlGenRandom :: proc(RandomBuffer: ^u8, RandomBufferLength: ULONG) -> BOOLEAN ---
  9. OpenProcessToken :: proc(ProcessHandle: HANDLE,
  10. DesiredAccess: DWORD,
  11. TokenHandle: ^HANDLE) -> BOOL ---
  12. OpenThreadToken :: proc(ThreadHandle: HANDLE,
  13. DesiredAccess: DWORD,
  14. OpenAsSelf: BOOL,
  15. TokenHandle: ^HANDLE) -> BOOL ---
  16. GetTokenInformation :: proc (
  17. TokenHandle: HANDLE,
  18. TokenInformationClass: TOKEN_INFORMATION_CLASS,
  19. TokenInformation: LPVOID,
  20. TokenInformationLength: DWORD,
  21. ReturnLength: PDWORD,
  22. ) -> BOOL ---
  23. CryptAcquireContextW :: proc(hProv: ^HCRYPTPROV, szContainer, szProvider: wstring, dwProvType, dwFlags: DWORD) -> DWORD ---
  24. CryptGenRandom :: proc(hProv: HCRYPTPROV, dwLen: DWORD, buf: LPVOID) -> DWORD ---
  25. CryptReleaseContext :: proc(hProv: HCRYPTPROV, dwFlags: DWORD) -> DWORD ---
  26. }
  27. // Necessary to create a token to impersonate a user with for CreateProcessAsUser
  28. @(default_calling_convention="system")
  29. foreign advapi32 {
  30. LogonUserW :: proc(
  31. lpszUsername: LPCWSTR,
  32. lpszDomain: LPCWSTR,
  33. lpszPassword: LPCWSTR,
  34. dwLogonType: Logon32_Type,
  35. dwLogonProvider: Logon32_Provider,
  36. phToken: ^HANDLE,
  37. ) -> BOOL ---
  38. // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-lookupaccountnamew
  39. // To look up the SID to use with DeleteProfileW.
  40. LookupAccountNameW :: proc(
  41. lpSystemName: wstring,
  42. lpAccountName: wstring,
  43. Sid: ^SID,
  44. cbSid: ^DWORD,
  45. ReferencedDomainName: wstring,
  46. cchReferencedDomainName: ^DWORD,
  47. peUse: PSID_NAME_USE,
  48. ) -> BOOL ---
  49. LookupAccountSidW :: proc (
  50. lpSystemName: LPCWSTR,
  51. Sid: PSID,
  52. Name: LPWSTR,
  53. cchName: LPDWORD,
  54. ReferencedDomainName: LPWSTR,
  55. cchReferencedDomainName: LPDWORD,
  56. peUse: PSID_NAME_USE,
  57. ) -> BOOL ---
  58. CreateProcessWithLogonW :: proc(
  59. lpUsername: wstring,
  60. lpDomain: wstring,
  61. lpPassword: wstring,
  62. dwLogonFlags: DWORD,
  63. lpApplicationName: wstring,
  64. lpCommandLine: wstring,
  65. dwCreationFlags: DWORD,
  66. lpEnvironment: LPVOID,
  67. lpCurrentDirectory: wstring,
  68. lpStartupInfo: LPSTARTUPINFOW,
  69. lpProcessInformation: LPPROCESS_INFORMATION,
  70. ) -> BOOL ---
  71. // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessasuserw
  72. CreateProcessAsUserW :: proc(
  73. hToken: HANDLE,
  74. lpApplicationName: wstring,
  75. lpCommandLine: wstring,
  76. lpProcessAttributes: LPSECURITY_ATTRIBUTES,
  77. lpThreadAttributes: LPSECURITY_ATTRIBUTES,
  78. bInheritHandles: BOOL,
  79. dwCreationFlags: DWORD,
  80. lpEnvironment: LPVOID,
  81. lpCurrentDirectory: wstring,
  82. lpStartupInfo: LPSTARTUPINFOW,
  83. lpProcessInformation: LPPROCESS_INFORMATION,
  84. ) -> BOOL ---
  85. RegCreateKeyExW :: proc(
  86. hKey: HKEY,
  87. lpSubKey: LPCWSTR,
  88. Reserved: DWORD,
  89. lpClass: LPWSTR,
  90. dwOptions: DWORD,
  91. samDesired: REGSAM,
  92. lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
  93. phkResult: PHKEY,
  94. lpdwDisposition: LPDWORD,
  95. ) -> LSTATUS ---
  96. RegOpenKeyW :: proc(
  97. hKey: HKEY,
  98. lpSubKey: LPCWSTR,
  99. phkResult: PHKEY,
  100. ) -> LSTATUS ---
  101. RegOpenKeyExW :: proc(
  102. hKey: HKEY,
  103. lpSubKey: LPCWSTR,
  104. ulOptions: DWORD,
  105. samDesired: REGSAM,
  106. phkResult: PHKEY,
  107. ) -> LSTATUS ---
  108. RegCloseKey :: proc(
  109. hKey: HKEY,
  110. ) -> LSTATUS ---
  111. RegGetValueW :: proc(
  112. hkey: HKEY,
  113. lpSubKey: LPCWSTR,
  114. lpValue: LPCWSTR,
  115. dwFlags: DWORD,
  116. pdwType: LPDWORD,
  117. pvData: PVOID,
  118. pcbData: LPDWORD,
  119. ) -> LSTATUS ---
  120. RegSetValueExW :: proc(
  121. hKey: HKEY,
  122. lpValueName: LPCWSTR,
  123. Reserved: DWORD,
  124. dwType: DWORD,
  125. lpData: ^BYTE,
  126. cbData: DWORD,
  127. ) -> LSTATUS ---
  128. RegSetKeyValueW :: proc(
  129. hKey: HKEY,
  130. lpSubKey: LPCWSTR,
  131. lpValueName: LPCWSTR,
  132. dwType: DWORD,
  133. lpData: LPCVOID,
  134. cbData: DWORD,
  135. ) -> LSTATUS ---
  136. RegQueryInfoKeyW :: proc(
  137. hKey: HKEY,
  138. lpClass: LPWSTR,
  139. lpcchClass: LPDWORD,
  140. lpReserved: LPDWORD,
  141. lpcSubKeys: LPDWORD,
  142. lpcbMaxSubKeyLen: LPDWORD,
  143. lpcbMaxClassLen: LPDWORD,
  144. lpcValues: LPDWORD,
  145. lpcbMaxValueNameLen: LPDWORD,
  146. lpcbMaxValueLen: LPDWORD,
  147. lpcbSecurityDescriptor: LPDWORD,
  148. lpftLastWriteTime: ^FILETIME,
  149. ) -> LSTATUS ---
  150. RegEnumKeyExW :: proc(
  151. hKey: HKEY,
  152. dwIndex: DWORD,
  153. lpName: LPWSTR,
  154. lpcchName: LPDWORD,
  155. lpReserved: LPDWORD,
  156. lpClass: LPWSTR,
  157. lpcchClass: LPDWORD,
  158. lpftLastWriteTime: ^FILETIME,
  159. ) -> LSTATUS ---
  160. RegEnumValueW :: proc(
  161. hKey: HKEY,
  162. dwIndex: DWORD,
  163. lpValueName: LPWSTR,
  164. lpcchValueName: LPDWORD,
  165. lpReserved: LPDWORD,
  166. lpType: LPDWORD,
  167. lpData: LPBYTE,
  168. lpcbData: LPDWORD,
  169. ) -> LSTATUS ---
  170. GetFileSecurityW :: proc(
  171. lpFileName: LPCWSTR,
  172. RequestedInformation: SECURITY_INFORMATION,
  173. pSecurityDescriptor: PSECURITY_DESCRIPTOR,
  174. nLength: DWORD,
  175. lpnLengthNeeded: LPDWORD,
  176. ) -> BOOL ---
  177. DuplicateToken :: proc(
  178. ExistingTokenHandle: HANDLE,
  179. ImpersonationLevel: SECURITY_IMPERSONATION_LEVEL,
  180. DuplicateTokenHandle: PHANDLE,
  181. ) -> BOOL ---
  182. MapGenericMask :: proc(
  183. AccessMask: PDWORD,
  184. GenericMapping: PGENERIC_MAPPING,
  185. ) ---
  186. AccessCheck :: proc(
  187. pSecurityDescriptor: PSECURITY_DESCRIPTOR,
  188. ClientToken: HANDLE,
  189. DesiredAccess: DWORD,
  190. GenericMapping: PGENERIC_MAPPING,
  191. PrivilegeSet: PPRIVILEGE_SET,
  192. PrivilegeSetLength: LPDWORD,
  193. GrantedAccess: LPDWORD,
  194. AccessStatus: LPBOOL,
  195. ) -> BOOL ---
  196. }
  197. PTOKEN_INFORMATION_CLASS :: ^TOKEN_INFORMATION_CLASS
  198. TOKEN_INFORMATION_CLASS :: enum i32 {
  199. TokenUser = 1,
  200. TokenGroups,
  201. TokenPrivileges,
  202. TokenOwner,
  203. TokenPrimaryGroup,
  204. TokenDefaultDacl,
  205. TokenSource,
  206. TokenType,
  207. TokenImpersonationLevel,
  208. TokenStatistics,
  209. TokenRestrictedSids,
  210. TokenSessionId,
  211. TokenGroupsAndPrivileges,
  212. TokenSessionReference,
  213. TokenSandBoxInert,
  214. TokenAuditPolicy,
  215. TokenOrigin,
  216. TokenElevationType,
  217. TokenLinkedToken,
  218. TokenElevation,
  219. TokenHasRestrictions,
  220. TokenAccessInformation,
  221. TokenVirtualizationAllowed,
  222. TokenVirtualizationEnabled,
  223. TokenIntegrityLevel,
  224. TokenUIAccess,
  225. TokenMandatoryPolicy,
  226. TokenLogonSid,
  227. TokenIsAppContainer,
  228. TokenCapabilities,
  229. TokenAppContainerSid,
  230. TokenAppContainerNumber,
  231. TokenUserClaimAttributes,
  232. TokenDeviceClaimAttributes,
  233. TokenRestrictedUserClaimAttributes,
  234. TokenRestrictedDeviceClaimAttributes,
  235. TokenDeviceGroups,
  236. TokenRestrictedDeviceGroups,
  237. TokenSecurityAttributes,
  238. TokenIsRestricted,
  239. TokenProcessTrustLevel,
  240. TokenPrivateNameSpace,
  241. TokenSingletonAttributes,
  242. TokenBnoIsolation,
  243. TokenChildProcessFlags,
  244. TokenIsLessPrivilegedAppContainer,
  245. TokenIsSandboxed,
  246. TokenIsAppSilo,
  247. TokenLoggingInformation,
  248. MaxTokenInfoClass,
  249. }
  250. PSID_NAME_USE :: ^SID_NAME_USE
  251. SID_NAME_USE :: enum i32 {
  252. SidTypeUser = 1,
  253. SidTypeGroup,
  254. SidTypeDomain,
  255. SidTypeAlias,
  256. SidTypeWellKnownGroup,
  257. SidTypeDeletedAccount,
  258. SidTypeInvalid,
  259. SidTypeUnknown,
  260. SidTypeComputer,
  261. SidTypeLabel,
  262. SidTypeLogonSession,
  263. }
  264. PTOKEN_USER :: ^TOKEN_USER
  265. TOKEN_USER :: struct {
  266. User: SID_AND_ATTRIBUTES,
  267. }
  268. PSID_AND_ATTRIBUTES :: ^SID_AND_ATTRIBUTES
  269. SID_AND_ATTRIBUTES :: struct {
  270. Sid: rawptr,
  271. Attributes: ULONG,
  272. }
  273. PTOKEN_TYPE :: ^TOKEN_TYPE
  274. TOKEN_TYPE :: enum {
  275. TokenPrimary = 1,
  276. TokenImpersonation = 2,
  277. }
  278. PTOKEN_STATISTICS :: ^TOKEN_STATISTICS
  279. TOKEN_STATISTICS :: struct {
  280. TokenId: LUID,
  281. AuthenticationId: LUID,
  282. ExpirationTime: LARGE_INTEGER,
  283. TokenType: TOKEN_TYPE,
  284. ImpersonationLevel: SECURITY_IMPERSONATION_LEVEL,
  285. DynamicCharged: DWORD,
  286. DynamicAvailable: DWORD,
  287. GroupCount: DWORD,
  288. PrivilegeCount: DWORD,
  289. ModifiedId: LUID,
  290. }
  291. TOKEN_SOURCE_LENGTH :: 8
  292. PTOKEN_SOURCE :: ^TOKEN_SOURCE
  293. TOKEN_SOURCE :: struct {
  294. SourceName: [TOKEN_SOURCE_LENGTH]CHAR,
  295. SourceIdentifier: LUID,
  296. }
  297. PTOKEN_PRIVILEGES :: ^TOKEN_PRIVILEGES
  298. TOKEN_PRIVILEGES :: struct {
  299. PrivilegeCount: DWORD,
  300. Privileges: [0]LUID_AND_ATTRIBUTES,
  301. }
  302. PTOKEN_PRIMARY_GROUP :: ^TOKEN_PRIMARY_GROUP
  303. TOKEN_PRIMARY_GROUP :: struct {
  304. PrimaryGroup: PSID,
  305. }
  306. PTOKEN_OWNER :: ^TOKEN_OWNER
  307. TOKEN_OWNER :: struct {
  308. Owner: PSID,
  309. }
  310. PTOKEN_GROUPS_AND_PRIVILEGES :: ^TOKEN_GROUPS_AND_PRIVILEGES
  311. TOKEN_GROUPS_AND_PRIVILEGES :: struct {
  312. SidCount: DWORD,
  313. SidLength: DWORD,
  314. Sids: PSID_AND_ATTRIBUTES,
  315. RestrictedSidCount: DWORD,
  316. RestrictedSidLength: DWORD,
  317. RestrictedSids: PSID_AND_ATTRIBUTES,
  318. PrivilegeCount: DWORD,
  319. PrivilegeLength: DWORD,
  320. Privileges: PLUID_AND_ATTRIBUTES,
  321. AuthenticationId: LUID,
  322. }
  323. PTOKEN_DEFAULT_DACL :: ^TOKEN_DEFAULT_DACL
  324. TOKEN_DEFAULT_DACL :: struct {
  325. DefaultDacl: PACL,
  326. }
  327. PACL :: ^ACL
  328. ACL :: struct {
  329. AclRevision: BYTE,
  330. Sbz1: BYTE,
  331. AclSize: WORD,
  332. AceCount: WORD,
  333. Sbz2: WORD,
  334. }