security_windows.go 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package windows
  5. import (
  6. "syscall"
  7. "unsafe"
  8. )
  9. const (
  10. NameUnknown = 0
  11. NameFullyQualifiedDN = 1
  12. NameSamCompatible = 2
  13. NameDisplay = 3
  14. NameUniqueId = 6
  15. NameCanonical = 7
  16. NameUserPrincipal = 8
  17. NameCanonicalEx = 9
  18. NameServicePrincipal = 10
  19. NameDnsDomain = 12
  20. )
  21. // This function returns 1 byte BOOLEAN rather than the 4 byte BOOL.
  22. // http://blogs.msdn.com/b/drnick/archive/2007/12/19/windows-and-upn-format-credentials.aspx
  23. //sys TranslateName(accName *uint16, accNameFormat uint32, desiredNameFormat uint32, translatedName *uint16, nSize *uint32) (err error) [failretval&0xff==0] = secur32.TranslateNameW
  24. //sys GetUserNameEx(nameFormat uint32, nameBuffre *uint16, nSize *uint32) (err error) [failretval&0xff==0] = secur32.GetUserNameExW
  25. // TranslateAccountName converts a directory service
  26. // object name from one format to another.
  27. func TranslateAccountName(username string, from, to uint32, initSize int) (string, error) {
  28. u, e := UTF16PtrFromString(username)
  29. if e != nil {
  30. return "", e
  31. }
  32. n := uint32(50)
  33. for {
  34. b := make([]uint16, n)
  35. e = TranslateName(u, from, to, &b[0], &n)
  36. if e == nil {
  37. return UTF16ToString(b[:n]), nil
  38. }
  39. if e != ERROR_INSUFFICIENT_BUFFER {
  40. return "", e
  41. }
  42. if n <= uint32(len(b)) {
  43. return "", e
  44. }
  45. }
  46. }
  47. const (
  48. // do not reorder
  49. NetSetupUnknownStatus = iota
  50. NetSetupUnjoined
  51. NetSetupWorkgroupName
  52. NetSetupDomainName
  53. )
  54. type UserInfo10 struct {
  55. Name *uint16
  56. Comment *uint16
  57. UsrComment *uint16
  58. FullName *uint16
  59. }
  60. //sys NetUserGetInfo(serverName *uint16, userName *uint16, level uint32, buf **byte) (neterr error) = netapi32.NetUserGetInfo
  61. //sys NetGetJoinInformation(server *uint16, name **uint16, bufType *uint32) (neterr error) = netapi32.NetGetJoinInformation
  62. //sys NetApiBufferFree(buf *byte) (neterr error) = netapi32.NetApiBufferFree
  63. const (
  64. // do not reorder
  65. SidTypeUser = 1 + iota
  66. SidTypeGroup
  67. SidTypeDomain
  68. SidTypeAlias
  69. SidTypeWellKnownGroup
  70. SidTypeDeletedAccount
  71. SidTypeInvalid
  72. SidTypeUnknown
  73. SidTypeComputer
  74. SidTypeLabel
  75. )
  76. type SidIdentifierAuthority struct {
  77. Value [6]byte
  78. }
  79. var (
  80. SECURITY_NULL_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 0}}
  81. SECURITY_WORLD_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 1}}
  82. SECURITY_LOCAL_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 2}}
  83. SECURITY_CREATOR_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 3}}
  84. SECURITY_NON_UNIQUE_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 4}}
  85. SECURITY_NT_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 5}}
  86. SECURITY_MANDATORY_LABEL_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 16}}
  87. )
  88. const (
  89. SECURITY_NULL_RID = 0
  90. SECURITY_WORLD_RID = 0
  91. SECURITY_LOCAL_RID = 0
  92. SECURITY_CREATOR_OWNER_RID = 0
  93. SECURITY_CREATOR_GROUP_RID = 1
  94. SECURITY_DIALUP_RID = 1
  95. SECURITY_NETWORK_RID = 2
  96. SECURITY_BATCH_RID = 3
  97. SECURITY_INTERACTIVE_RID = 4
  98. SECURITY_LOGON_IDS_RID = 5
  99. SECURITY_SERVICE_RID = 6
  100. SECURITY_LOCAL_SYSTEM_RID = 18
  101. SECURITY_BUILTIN_DOMAIN_RID = 32
  102. SECURITY_PRINCIPAL_SELF_RID = 10
  103. SECURITY_CREATOR_OWNER_SERVER_RID = 0x2
  104. SECURITY_CREATOR_GROUP_SERVER_RID = 0x3
  105. SECURITY_LOGON_IDS_RID_COUNT = 0x3
  106. SECURITY_ANONYMOUS_LOGON_RID = 0x7
  107. SECURITY_PROXY_RID = 0x8
  108. SECURITY_ENTERPRISE_CONTROLLERS_RID = 0x9
  109. SECURITY_SERVER_LOGON_RID = SECURITY_ENTERPRISE_CONTROLLERS_RID
  110. SECURITY_AUTHENTICATED_USER_RID = 0xb
  111. SECURITY_RESTRICTED_CODE_RID = 0xc
  112. SECURITY_NT_NON_UNIQUE_RID = 0x15
  113. )
  114. // Predefined domain-relative RIDs for local groups.
  115. // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa379649(v=vs.85).aspx
  116. const (
  117. DOMAIN_ALIAS_RID_ADMINS = 0x220
  118. DOMAIN_ALIAS_RID_USERS = 0x221
  119. DOMAIN_ALIAS_RID_GUESTS = 0x222
  120. DOMAIN_ALIAS_RID_POWER_USERS = 0x223
  121. DOMAIN_ALIAS_RID_ACCOUNT_OPS = 0x224
  122. DOMAIN_ALIAS_RID_SYSTEM_OPS = 0x225
  123. DOMAIN_ALIAS_RID_PRINT_OPS = 0x226
  124. DOMAIN_ALIAS_RID_BACKUP_OPS = 0x227
  125. DOMAIN_ALIAS_RID_REPLICATOR = 0x228
  126. DOMAIN_ALIAS_RID_RAS_SERVERS = 0x229
  127. DOMAIN_ALIAS_RID_PREW2KCOMPACCESS = 0x22a
  128. DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS = 0x22b
  129. DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS = 0x22c
  130. DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS = 0x22d
  131. DOMAIN_ALIAS_RID_MONITORING_USERS = 0x22e
  132. DOMAIN_ALIAS_RID_LOGGING_USERS = 0x22f
  133. DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS = 0x230
  134. DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS = 0x231
  135. DOMAIN_ALIAS_RID_DCOM_USERS = 0x232
  136. DOMAIN_ALIAS_RID_IUSERS = 0x238
  137. DOMAIN_ALIAS_RID_CRYPTO_OPERATORS = 0x239
  138. DOMAIN_ALIAS_RID_CACHEABLE_PRINCIPALS_GROUP = 0x23b
  139. DOMAIN_ALIAS_RID_NON_CACHEABLE_PRINCIPALS_GROUP = 0x23c
  140. DOMAIN_ALIAS_RID_EVENT_LOG_READERS_GROUP = 0x23d
  141. DOMAIN_ALIAS_RID_CERTSVC_DCOM_ACCESS_GROUP = 0x23e
  142. )
  143. //sys LookupAccountSid(systemName *uint16, sid *SID, name *uint16, nameLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountSidW
  144. //sys LookupAccountName(systemName *uint16, accountName *uint16, sid *SID, sidLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountNameW
  145. //sys ConvertSidToStringSid(sid *SID, stringSid **uint16) (err error) = advapi32.ConvertSidToStringSidW
  146. //sys ConvertStringSidToSid(stringSid *uint16, sid **SID) (err error) = advapi32.ConvertStringSidToSidW
  147. //sys GetLengthSid(sid *SID) (len uint32) = advapi32.GetLengthSid
  148. //sys CopySid(destSidLen uint32, destSid *SID, srcSid *SID) (err error) = advapi32.CopySid
  149. //sys AllocateAndInitializeSid(identAuth *SidIdentifierAuthority, subAuth byte, subAuth0 uint32, subAuth1 uint32, subAuth2 uint32, subAuth3 uint32, subAuth4 uint32, subAuth5 uint32, subAuth6 uint32, subAuth7 uint32, sid **SID) (err error) = advapi32.AllocateAndInitializeSid
  150. //sys createWellKnownSid(sidType WELL_KNOWN_SID_TYPE, domainSid *SID, sid *SID, sizeSid *uint32) (err error) = advapi32.CreateWellKnownSid
  151. //sys isWellKnownSid(sid *SID, sidType WELL_KNOWN_SID_TYPE) (isWellKnown bool) = advapi32.IsWellKnownSid
  152. //sys FreeSid(sid *SID) (err error) [failretval!=0] = advapi32.FreeSid
  153. //sys EqualSid(sid1 *SID, sid2 *SID) (isEqual bool) = advapi32.EqualSid
  154. //sys getSidIdentifierAuthority(sid *SID) (authority *SidIdentifierAuthority) = advapi32.GetSidIdentifierAuthority
  155. //sys getSidSubAuthorityCount(sid *SID) (count *uint8) = advapi32.GetSidSubAuthorityCount
  156. //sys getSidSubAuthority(sid *SID, index uint32) (subAuthority *uint32) = advapi32.GetSidSubAuthority
  157. //sys isValidSid(sid *SID) (isValid bool) = advapi32.IsValidSid
  158. // The security identifier (SID) structure is a variable-length
  159. // structure used to uniquely identify users or groups.
  160. type SID struct{}
  161. // StringToSid converts a string-format security identifier
  162. // SID into a valid, functional SID.
  163. func StringToSid(s string) (*SID, error) {
  164. var sid *SID
  165. p, e := UTF16PtrFromString(s)
  166. if e != nil {
  167. return nil, e
  168. }
  169. e = ConvertStringSidToSid(p, &sid)
  170. if e != nil {
  171. return nil, e
  172. }
  173. defer LocalFree((Handle)(unsafe.Pointer(sid)))
  174. return sid.Copy()
  175. }
  176. // LookupSID retrieves a security identifier SID for the account
  177. // and the name of the domain on which the account was found.
  178. // System specify target computer to search.
  179. func LookupSID(system, account string) (sid *SID, domain string, accType uint32, err error) {
  180. if len(account) == 0 {
  181. return nil, "", 0, syscall.EINVAL
  182. }
  183. acc, e := UTF16PtrFromString(account)
  184. if e != nil {
  185. return nil, "", 0, e
  186. }
  187. var sys *uint16
  188. if len(system) > 0 {
  189. sys, e = UTF16PtrFromString(system)
  190. if e != nil {
  191. return nil, "", 0, e
  192. }
  193. }
  194. n := uint32(50)
  195. dn := uint32(50)
  196. for {
  197. b := make([]byte, n)
  198. db := make([]uint16, dn)
  199. sid = (*SID)(unsafe.Pointer(&b[0]))
  200. e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
  201. if e == nil {
  202. return sid, UTF16ToString(db), accType, nil
  203. }
  204. if e != ERROR_INSUFFICIENT_BUFFER {
  205. return nil, "", 0, e
  206. }
  207. if n <= uint32(len(b)) {
  208. return nil, "", 0, e
  209. }
  210. }
  211. }
  212. // String converts SID to a string format
  213. // suitable for display, storage, or transmission.
  214. func (sid *SID) String() (string, error) {
  215. var s *uint16
  216. e := ConvertSidToStringSid(sid, &s)
  217. if e != nil {
  218. return "", e
  219. }
  220. defer LocalFree((Handle)(unsafe.Pointer(s)))
  221. return UTF16ToString((*[256]uint16)(unsafe.Pointer(s))[:]), nil
  222. }
  223. // Len returns the length, in bytes, of a valid security identifier SID.
  224. func (sid *SID) Len() int {
  225. return int(GetLengthSid(sid))
  226. }
  227. // Copy creates a duplicate of security identifier SID.
  228. func (sid *SID) Copy() (*SID, error) {
  229. b := make([]byte, sid.Len())
  230. sid2 := (*SID)(unsafe.Pointer(&b[0]))
  231. e := CopySid(uint32(len(b)), sid2, sid)
  232. if e != nil {
  233. return nil, e
  234. }
  235. return sid2, nil
  236. }
  237. // IdentifierAuthority returns the identifier authority of the SID.
  238. func (sid *SID) IdentifierAuthority() SidIdentifierAuthority {
  239. return *getSidIdentifierAuthority(sid)
  240. }
  241. // SubAuthorityCount returns the number of sub-authorities in the SID.
  242. func (sid *SID) SubAuthorityCount() uint8 {
  243. return *getSidSubAuthorityCount(sid)
  244. }
  245. // SubAuthority returns the sub-authority of the SID as specified by
  246. // the index, which must be less than sid.SubAuthorityCount().
  247. func (sid *SID) SubAuthority(idx uint32) uint32 {
  248. if idx >= uint32(sid.SubAuthorityCount()) {
  249. panic("sub-authority index out of range")
  250. }
  251. return *getSidSubAuthority(sid, idx)
  252. }
  253. // IsValid returns whether the SID has a valid revision and length.
  254. func (sid *SID) IsValid() bool {
  255. return isValidSid(sid)
  256. }
  257. // Equals compares two SIDs for equality.
  258. func (sid *SID) Equals(sid2 *SID) bool {
  259. return EqualSid(sid, sid2)
  260. }
  261. // IsWellKnown determines whether the SID matches the well-known sidType.
  262. func (sid *SID) IsWellKnown(sidType WELL_KNOWN_SID_TYPE) bool {
  263. return isWellKnownSid(sid, sidType)
  264. }
  265. // LookupAccount retrieves the name of the account for this SID
  266. // and the name of the first domain on which this SID is found.
  267. // System specify target computer to search for.
  268. func (sid *SID) LookupAccount(system string) (account, domain string, accType uint32, err error) {
  269. var sys *uint16
  270. if len(system) > 0 {
  271. sys, err = UTF16PtrFromString(system)
  272. if err != nil {
  273. return "", "", 0, err
  274. }
  275. }
  276. n := uint32(50)
  277. dn := uint32(50)
  278. for {
  279. b := make([]uint16, n)
  280. db := make([]uint16, dn)
  281. e := LookupAccountSid(sys, sid, &b[0], &n, &db[0], &dn, &accType)
  282. if e == nil {
  283. return UTF16ToString(b), UTF16ToString(db), accType, nil
  284. }
  285. if e != ERROR_INSUFFICIENT_BUFFER {
  286. return "", "", 0, e
  287. }
  288. if n <= uint32(len(b)) {
  289. return "", "", 0, e
  290. }
  291. }
  292. }
  293. // Various types of pre-specified SIDs that can be synthesized and compared at runtime.
  294. type WELL_KNOWN_SID_TYPE uint32
  295. const (
  296. WinNullSid = 0
  297. WinWorldSid = 1
  298. WinLocalSid = 2
  299. WinCreatorOwnerSid = 3
  300. WinCreatorGroupSid = 4
  301. WinCreatorOwnerServerSid = 5
  302. WinCreatorGroupServerSid = 6
  303. WinNtAuthoritySid = 7
  304. WinDialupSid = 8
  305. WinNetworkSid = 9
  306. WinBatchSid = 10
  307. WinInteractiveSid = 11
  308. WinServiceSid = 12
  309. WinAnonymousSid = 13
  310. WinProxySid = 14
  311. WinEnterpriseControllersSid = 15
  312. WinSelfSid = 16
  313. WinAuthenticatedUserSid = 17
  314. WinRestrictedCodeSid = 18
  315. WinTerminalServerSid = 19
  316. WinRemoteLogonIdSid = 20
  317. WinLogonIdsSid = 21
  318. WinLocalSystemSid = 22
  319. WinLocalServiceSid = 23
  320. WinNetworkServiceSid = 24
  321. WinBuiltinDomainSid = 25
  322. WinBuiltinAdministratorsSid = 26
  323. WinBuiltinUsersSid = 27
  324. WinBuiltinGuestsSid = 28
  325. WinBuiltinPowerUsersSid = 29
  326. WinBuiltinAccountOperatorsSid = 30
  327. WinBuiltinSystemOperatorsSid = 31
  328. WinBuiltinPrintOperatorsSid = 32
  329. WinBuiltinBackupOperatorsSid = 33
  330. WinBuiltinReplicatorSid = 34
  331. WinBuiltinPreWindows2000CompatibleAccessSid = 35
  332. WinBuiltinRemoteDesktopUsersSid = 36
  333. WinBuiltinNetworkConfigurationOperatorsSid = 37
  334. WinAccountAdministratorSid = 38
  335. WinAccountGuestSid = 39
  336. WinAccountKrbtgtSid = 40
  337. WinAccountDomainAdminsSid = 41
  338. WinAccountDomainUsersSid = 42
  339. WinAccountDomainGuestsSid = 43
  340. WinAccountComputersSid = 44
  341. WinAccountControllersSid = 45
  342. WinAccountCertAdminsSid = 46
  343. WinAccountSchemaAdminsSid = 47
  344. WinAccountEnterpriseAdminsSid = 48
  345. WinAccountPolicyAdminsSid = 49
  346. WinAccountRasAndIasServersSid = 50
  347. WinNTLMAuthenticationSid = 51
  348. WinDigestAuthenticationSid = 52
  349. WinSChannelAuthenticationSid = 53
  350. WinThisOrganizationSid = 54
  351. WinOtherOrganizationSid = 55
  352. WinBuiltinIncomingForestTrustBuildersSid = 56
  353. WinBuiltinPerfMonitoringUsersSid = 57
  354. WinBuiltinPerfLoggingUsersSid = 58
  355. WinBuiltinAuthorizationAccessSid = 59
  356. WinBuiltinTerminalServerLicenseServersSid = 60
  357. WinBuiltinDCOMUsersSid = 61
  358. WinBuiltinIUsersSid = 62
  359. WinIUserSid = 63
  360. WinBuiltinCryptoOperatorsSid = 64
  361. WinUntrustedLabelSid = 65
  362. WinLowLabelSid = 66
  363. WinMediumLabelSid = 67
  364. WinHighLabelSid = 68
  365. WinSystemLabelSid = 69
  366. WinWriteRestrictedCodeSid = 70
  367. WinCreatorOwnerRightsSid = 71
  368. WinCacheablePrincipalsGroupSid = 72
  369. WinNonCacheablePrincipalsGroupSid = 73
  370. WinEnterpriseReadonlyControllersSid = 74
  371. WinAccountReadonlyControllersSid = 75
  372. WinBuiltinEventLogReadersGroup = 76
  373. WinNewEnterpriseReadonlyControllersSid = 77
  374. WinBuiltinCertSvcDComAccessGroup = 78
  375. WinMediumPlusLabelSid = 79
  376. WinLocalLogonSid = 80
  377. WinConsoleLogonSid = 81
  378. WinThisOrganizationCertificateSid = 82
  379. WinApplicationPackageAuthoritySid = 83
  380. WinBuiltinAnyPackageSid = 84
  381. WinCapabilityInternetClientSid = 85
  382. WinCapabilityInternetClientServerSid = 86
  383. WinCapabilityPrivateNetworkClientServerSid = 87
  384. WinCapabilityPicturesLibrarySid = 88
  385. WinCapabilityVideosLibrarySid = 89
  386. WinCapabilityMusicLibrarySid = 90
  387. WinCapabilityDocumentsLibrarySid = 91
  388. WinCapabilitySharedUserCertificatesSid = 92
  389. WinCapabilityEnterpriseAuthenticationSid = 93
  390. WinCapabilityRemovableStorageSid = 94
  391. WinBuiltinRDSRemoteAccessServersSid = 95
  392. WinBuiltinRDSEndpointServersSid = 96
  393. WinBuiltinRDSManagementServersSid = 97
  394. WinUserModeDriversSid = 98
  395. WinBuiltinHyperVAdminsSid = 99
  396. WinAccountCloneableControllersSid = 100
  397. WinBuiltinAccessControlAssistanceOperatorsSid = 101
  398. WinBuiltinRemoteManagementUsersSid = 102
  399. WinAuthenticationAuthorityAssertedSid = 103
  400. WinAuthenticationServiceAssertedSid = 104
  401. WinLocalAccountSid = 105
  402. WinLocalAccountAndAdministratorSid = 106
  403. WinAccountProtectedUsersSid = 107
  404. WinCapabilityAppointmentsSid = 108
  405. WinCapabilityContactsSid = 109
  406. WinAccountDefaultSystemManagedSid = 110
  407. WinBuiltinDefaultSystemManagedGroupSid = 111
  408. WinBuiltinStorageReplicaAdminsSid = 112
  409. WinAccountKeyAdminsSid = 113
  410. WinAccountEnterpriseKeyAdminsSid = 114
  411. WinAuthenticationKeyTrustSid = 115
  412. WinAuthenticationKeyPropertyMFASid = 116
  413. WinAuthenticationKeyPropertyAttestationSid = 117
  414. WinAuthenticationFreshKeyAuthSid = 118
  415. WinBuiltinDeviceOwnersSid = 119
  416. )
  417. // Creates a SID for a well-known predefined alias, generally using the constants of the form
  418. // Win*Sid, for the local machine.
  419. func CreateWellKnownSid(sidType WELL_KNOWN_SID_TYPE) (*SID, error) {
  420. return CreateWellKnownDomainSid(sidType, nil)
  421. }
  422. // Creates a SID for a well-known predefined alias, generally using the constants of the form
  423. // Win*Sid, for the domain specified by the domainSid parameter.
  424. func CreateWellKnownDomainSid(sidType WELL_KNOWN_SID_TYPE, domainSid *SID) (*SID, error) {
  425. n := uint32(50)
  426. for {
  427. b := make([]byte, n)
  428. sid := (*SID)(unsafe.Pointer(&b[0]))
  429. err := createWellKnownSid(sidType, domainSid, sid, &n)
  430. if err == nil {
  431. return sid, nil
  432. }
  433. if err != ERROR_INSUFFICIENT_BUFFER {
  434. return nil, err
  435. }
  436. if n <= uint32(len(b)) {
  437. return nil, err
  438. }
  439. }
  440. }
  441. const (
  442. // do not reorder
  443. TOKEN_ASSIGN_PRIMARY = 1 << iota
  444. TOKEN_DUPLICATE
  445. TOKEN_IMPERSONATE
  446. TOKEN_QUERY
  447. TOKEN_QUERY_SOURCE
  448. TOKEN_ADJUST_PRIVILEGES
  449. TOKEN_ADJUST_GROUPS
  450. TOKEN_ADJUST_DEFAULT
  451. TOKEN_ADJUST_SESSIONID
  452. TOKEN_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
  453. TOKEN_ASSIGN_PRIMARY |
  454. TOKEN_DUPLICATE |
  455. TOKEN_IMPERSONATE |
  456. TOKEN_QUERY |
  457. TOKEN_QUERY_SOURCE |
  458. TOKEN_ADJUST_PRIVILEGES |
  459. TOKEN_ADJUST_GROUPS |
  460. TOKEN_ADJUST_DEFAULT |
  461. TOKEN_ADJUST_SESSIONID
  462. TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY
  463. TOKEN_WRITE = STANDARD_RIGHTS_WRITE |
  464. TOKEN_ADJUST_PRIVILEGES |
  465. TOKEN_ADJUST_GROUPS |
  466. TOKEN_ADJUST_DEFAULT
  467. TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE
  468. )
  469. const (
  470. // do not reorder
  471. TokenUser = 1 + iota
  472. TokenGroups
  473. TokenPrivileges
  474. TokenOwner
  475. TokenPrimaryGroup
  476. TokenDefaultDacl
  477. TokenSource
  478. TokenType
  479. TokenImpersonationLevel
  480. TokenStatistics
  481. TokenRestrictedSids
  482. TokenSessionId
  483. TokenGroupsAndPrivileges
  484. TokenSessionReference
  485. TokenSandBoxInert
  486. TokenAuditPolicy
  487. TokenOrigin
  488. TokenElevationType
  489. TokenLinkedToken
  490. TokenElevation
  491. TokenHasRestrictions
  492. TokenAccessInformation
  493. TokenVirtualizationAllowed
  494. TokenVirtualizationEnabled
  495. TokenIntegrityLevel
  496. TokenUIAccess
  497. TokenMandatoryPolicy
  498. TokenLogonSid
  499. MaxTokenInfoClass
  500. )
  501. // Group attributes inside of Tokengroups.Groups[i].Attributes
  502. const (
  503. SE_GROUP_MANDATORY = 0x00000001
  504. SE_GROUP_ENABLED_BY_DEFAULT = 0x00000002
  505. SE_GROUP_ENABLED = 0x00000004
  506. SE_GROUP_OWNER = 0x00000008
  507. SE_GROUP_USE_FOR_DENY_ONLY = 0x00000010
  508. SE_GROUP_INTEGRITY = 0x00000020
  509. SE_GROUP_INTEGRITY_ENABLED = 0x00000040
  510. SE_GROUP_LOGON_ID = 0xC0000000
  511. SE_GROUP_RESOURCE = 0x20000000
  512. SE_GROUP_VALID_ATTRIBUTES = SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED | SE_GROUP_OWNER | SE_GROUP_USE_FOR_DENY_ONLY | SE_GROUP_LOGON_ID | SE_GROUP_RESOURCE | SE_GROUP_INTEGRITY | SE_GROUP_INTEGRITY_ENABLED
  513. )
  514. // Privilege attributes
  515. const (
  516. SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001
  517. SE_PRIVILEGE_ENABLED = 0x00000002
  518. SE_PRIVILEGE_REMOVED = 0x00000004
  519. SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000
  520. SE_PRIVILEGE_VALID_ATTRIBUTES = SE_PRIVILEGE_ENABLED_BY_DEFAULT | SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_REMOVED | SE_PRIVILEGE_USED_FOR_ACCESS
  521. )
  522. // Token types
  523. const (
  524. TokenPrimary = 1
  525. TokenImpersonation = 2
  526. )
  527. // Impersonation levels
  528. const (
  529. SecurityAnonymous = 0
  530. SecurityIdentification = 1
  531. SecurityImpersonation = 2
  532. SecurityDelegation = 3
  533. )
  534. type LUID struct {
  535. LowPart uint32
  536. HighPart int32
  537. }
  538. type LUIDAndAttributes struct {
  539. Luid LUID
  540. Attributes uint32
  541. }
  542. type SIDAndAttributes struct {
  543. Sid *SID
  544. Attributes uint32
  545. }
  546. type Tokenuser struct {
  547. User SIDAndAttributes
  548. }
  549. type Tokenprimarygroup struct {
  550. PrimaryGroup *SID
  551. }
  552. type Tokengroups struct {
  553. GroupCount uint32
  554. Groups [1]SIDAndAttributes // Use AllGroups() for iterating.
  555. }
  556. // AllGroups returns a slice that can be used to iterate over the groups in g.
  557. func (g *Tokengroups) AllGroups() []SIDAndAttributes {
  558. return (*[(1 << 28) - 1]SIDAndAttributes)(unsafe.Pointer(&g.Groups[0]))[:g.GroupCount:g.GroupCount]
  559. }
  560. type Tokenprivileges struct {
  561. PrivilegeCount uint32
  562. Privileges [1]LUIDAndAttributes // Use AllPrivileges() for iterating.
  563. }
  564. // AllPrivileges returns a slice that can be used to iterate over the privileges in p.
  565. func (p *Tokenprivileges) AllPrivileges() []LUIDAndAttributes {
  566. return (*[(1 << 27) - 1]LUIDAndAttributes)(unsafe.Pointer(&p.Privileges[0]))[:p.PrivilegeCount:p.PrivilegeCount]
  567. }
  568. type Tokenmandatorylabel struct {
  569. Label SIDAndAttributes
  570. }
  571. func (tml *Tokenmandatorylabel) Size() uint32 {
  572. return uint32(unsafe.Sizeof(Tokenmandatorylabel{})) + GetLengthSid(tml.Label.Sid)
  573. }
  574. // Authorization Functions
  575. //sys checkTokenMembership(tokenHandle Token, sidToCheck *SID, isMember *int32) (err error) = advapi32.CheckTokenMembership
  576. //sys OpenProcessToken(process Handle, access uint32, token *Token) (err error) = advapi32.OpenProcessToken
  577. //sys OpenThreadToken(thread Handle, access uint32, openAsSelf bool, token *Token) (err error) = advapi32.OpenThreadToken
  578. //sys ImpersonateSelf(impersonationlevel uint32) (err error) = advapi32.ImpersonateSelf
  579. //sys RevertToSelf() (err error) = advapi32.RevertToSelf
  580. //sys SetThreadToken(thread *Handle, token Token) (err error) = advapi32.SetThreadToken
  581. //sys LookupPrivilegeValue(systemname *uint16, name *uint16, luid *LUID) (err error) = advapi32.LookupPrivilegeValueW
  582. //sys AdjustTokenPrivileges(token Token, disableAllPrivileges bool, newstate *Tokenprivileges, buflen uint32, prevstate *Tokenprivileges, returnlen *uint32) (err error) = advapi32.AdjustTokenPrivileges
  583. //sys AdjustTokenGroups(token Token, resetToDefault bool, newstate *Tokengroups, buflen uint32, prevstate *Tokengroups, returnlen *uint32) (err error) = advapi32.AdjustTokenGroups
  584. //sys GetTokenInformation(token Token, infoClass uint32, info *byte, infoLen uint32, returnedLen *uint32) (err error) = advapi32.GetTokenInformation
  585. //sys SetTokenInformation(token Token, infoClass uint32, info *byte, infoLen uint32) (err error) = advapi32.SetTokenInformation
  586. //sys DuplicateTokenEx(existingToken Token, desiredAccess uint32, tokenAttributes *SecurityAttributes, impersonationLevel uint32, tokenType uint32, newToken *Token) (err error) = advapi32.DuplicateTokenEx
  587. //sys GetUserProfileDirectory(t Token, dir *uint16, dirLen *uint32) (err error) = userenv.GetUserProfileDirectoryW
  588. //sys getSystemDirectory(dir *uint16, dirLen uint32) (len uint32, err error) = kernel32.GetSystemDirectoryW
  589. //sys getWindowsDirectory(dir *uint16, dirLen uint32) (len uint32, err error) = kernel32.GetWindowsDirectoryW
  590. //sys getSystemWindowsDirectory(dir *uint16, dirLen uint32) (len uint32, err error) = kernel32.GetSystemWindowsDirectoryW
  591. // An access token contains the security information for a logon session.
  592. // The system creates an access token when a user logs on, and every
  593. // process executed on behalf of the user has a copy of the token.
  594. // The token identifies the user, the user's groups, and the user's
  595. // privileges. The system uses the token to control access to securable
  596. // objects and to control the ability of the user to perform various
  597. // system-related operations on the local computer.
  598. type Token Handle
  599. // OpenCurrentProcessToken opens the access token
  600. // associated with current process. It is a real
  601. // token that needs to be closed, unlike
  602. // GetCurrentProcessToken.
  603. func OpenCurrentProcessToken() (token Token, err error) {
  604. err = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &token)
  605. return
  606. }
  607. // GetCurrentProcessToken returns the access token associated with
  608. // the current process. It is a pseudo token that does not need
  609. // to be closed.
  610. func GetCurrentProcessToken() Token {
  611. return Token(^uintptr(4 - 1))
  612. }
  613. // GetCurrentThreadToken return the access token associated with
  614. // the current thread. It is a pseudo token that does not need
  615. // to be closed.
  616. func GetCurrentThreadToken() Token {
  617. return Token(^uintptr(5 - 1))
  618. }
  619. // GetCurrentThreadEffectiveToken returns the effective access token
  620. // associated with the current thread. It is a pseudo token that does
  621. // not need to be closed.
  622. func GetCurrentThreadEffectiveToken() Token {
  623. return Token(^uintptr(6 - 1))
  624. }
  625. // Close releases access to access token.
  626. func (t Token) Close() error {
  627. return CloseHandle(Handle(t))
  628. }
  629. // getInfo retrieves a specified type of information about an access token.
  630. func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) {
  631. n := uint32(initSize)
  632. for {
  633. b := make([]byte, n)
  634. e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
  635. if e == nil {
  636. return unsafe.Pointer(&b[0]), nil
  637. }
  638. if e != ERROR_INSUFFICIENT_BUFFER {
  639. return nil, e
  640. }
  641. if n <= uint32(len(b)) {
  642. return nil, e
  643. }
  644. }
  645. }
  646. // GetTokenUser retrieves access token t user account information.
  647. func (t Token) GetTokenUser() (*Tokenuser, error) {
  648. i, e := t.getInfo(TokenUser, 50)
  649. if e != nil {
  650. return nil, e
  651. }
  652. return (*Tokenuser)(i), nil
  653. }
  654. // GetTokenGroups retrieves group accounts associated with access token t.
  655. func (t Token) GetTokenGroups() (*Tokengroups, error) {
  656. i, e := t.getInfo(TokenGroups, 50)
  657. if e != nil {
  658. return nil, e
  659. }
  660. return (*Tokengroups)(i), nil
  661. }
  662. // GetTokenPrimaryGroup retrieves access token t primary group information.
  663. // A pointer to a SID structure representing a group that will become
  664. // the primary group of any objects created by a process using this access token.
  665. func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
  666. i, e := t.getInfo(TokenPrimaryGroup, 50)
  667. if e != nil {
  668. return nil, e
  669. }
  670. return (*Tokenprimarygroup)(i), nil
  671. }
  672. // GetUserProfileDirectory retrieves path to the
  673. // root directory of the access token t user's profile.
  674. func (t Token) GetUserProfileDirectory() (string, error) {
  675. n := uint32(100)
  676. for {
  677. b := make([]uint16, n)
  678. e := GetUserProfileDirectory(t, &b[0], &n)
  679. if e == nil {
  680. return UTF16ToString(b), nil
  681. }
  682. if e != ERROR_INSUFFICIENT_BUFFER {
  683. return "", e
  684. }
  685. if n <= uint32(len(b)) {
  686. return "", e
  687. }
  688. }
  689. }
  690. // IsElevated returns whether the current token is elevated from a UAC perspective.
  691. func (token Token) IsElevated() bool {
  692. var isElevated uint32
  693. var outLen uint32
  694. err := GetTokenInformation(token, TokenElevation, (*byte)(unsafe.Pointer(&isElevated)), uint32(unsafe.Sizeof(isElevated)), &outLen)
  695. if err != nil {
  696. return false
  697. }
  698. return outLen == uint32(unsafe.Sizeof(isElevated)) && isElevated != 0
  699. }
  700. // GetLinkedToken returns the linked token, which may be an elevated UAC token.
  701. func (token Token) GetLinkedToken() (Token, error) {
  702. var linkedToken Token
  703. var outLen uint32
  704. err := GetTokenInformation(token, TokenLinkedToken, (*byte)(unsafe.Pointer(&linkedToken)), uint32(unsafe.Sizeof(linkedToken)), &outLen)
  705. if err != nil {
  706. return Token(0), err
  707. }
  708. return linkedToken, nil
  709. }
  710. // GetSystemDirectory retrieves the path to current location of the system
  711. // directory, which is typically, though not always, `C:\Windows\System32`.
  712. func GetSystemDirectory() (string, error) {
  713. n := uint32(MAX_PATH)
  714. for {
  715. b := make([]uint16, n)
  716. l, e := getSystemDirectory(&b[0], n)
  717. if e != nil {
  718. return "", e
  719. }
  720. if l <= n {
  721. return UTF16ToString(b[:l]), nil
  722. }
  723. n = l
  724. }
  725. }
  726. // GetWindowsDirectory retrieves the path to current location of the Windows
  727. // directory, which is typically, though not always, `C:\Windows`. This may
  728. // be a private user directory in the case that the application is running
  729. // under a terminal server.
  730. func GetWindowsDirectory() (string, error) {
  731. n := uint32(MAX_PATH)
  732. for {
  733. b := make([]uint16, n)
  734. l, e := getWindowsDirectory(&b[0], n)
  735. if e != nil {
  736. return "", e
  737. }
  738. if l <= n {
  739. return UTF16ToString(b[:l]), nil
  740. }
  741. n = l
  742. }
  743. }
  744. // GetSystemWindowsDirectory retrieves the path to current location of the
  745. // Windows directory, which is typically, though not always, `C:\Windows`.
  746. func GetSystemWindowsDirectory() (string, error) {
  747. n := uint32(MAX_PATH)
  748. for {
  749. b := make([]uint16, n)
  750. l, e := getSystemWindowsDirectory(&b[0], n)
  751. if e != nil {
  752. return "", e
  753. }
  754. if l <= n {
  755. return UTF16ToString(b[:l]), nil
  756. }
  757. n = l
  758. }
  759. }
  760. // IsMember reports whether the access token t is a member of the provided SID.
  761. func (t Token) IsMember(sid *SID) (bool, error) {
  762. var b int32
  763. if e := checkTokenMembership(t, sid, &b); e != nil {
  764. return false, e
  765. }
  766. return b != 0, nil
  767. }
  768. const (
  769. WTS_CONSOLE_CONNECT = 0x1
  770. WTS_CONSOLE_DISCONNECT = 0x2
  771. WTS_REMOTE_CONNECT = 0x3
  772. WTS_REMOTE_DISCONNECT = 0x4
  773. WTS_SESSION_LOGON = 0x5
  774. WTS_SESSION_LOGOFF = 0x6
  775. WTS_SESSION_LOCK = 0x7
  776. WTS_SESSION_UNLOCK = 0x8
  777. WTS_SESSION_REMOTE_CONTROL = 0x9
  778. WTS_SESSION_CREATE = 0xa
  779. WTS_SESSION_TERMINATE = 0xb
  780. )
  781. const (
  782. WTSActive = 0
  783. WTSConnected = 1
  784. WTSConnectQuery = 2
  785. WTSShadow = 3
  786. WTSDisconnected = 4
  787. WTSIdle = 5
  788. WTSListen = 6
  789. WTSReset = 7
  790. WTSDown = 8
  791. WTSInit = 9
  792. )
  793. type WTSSESSION_NOTIFICATION struct {
  794. Size uint32
  795. SessionID uint32
  796. }
  797. type WTS_SESSION_INFO struct {
  798. SessionID uint32
  799. WindowStationName *uint16
  800. State uint32
  801. }
  802. //sys WTSQueryUserToken(session uint32, token *Token) (err error) = wtsapi32.WTSQueryUserToken
  803. //sys WTSEnumerateSessions(handle Handle, reserved uint32, version uint32, sessions **WTS_SESSION_INFO, count *uint32) (err error) = wtsapi32.WTSEnumerateSessionsW
  804. //sys WTSFreeMemory(ptr uintptr) = wtsapi32.WTSFreeMemory
  805. type ACL struct {
  806. aclRevision byte
  807. sbz1 byte
  808. aclSize uint16
  809. aceCount uint16
  810. sbz2 uint16
  811. }
  812. type SECURITY_DESCRIPTOR struct {
  813. revision byte
  814. sbz1 byte
  815. control SECURITY_DESCRIPTOR_CONTROL
  816. owner *SID
  817. group *SID
  818. sacl *ACL
  819. dacl *ACL
  820. }
  821. type SecurityAttributes struct {
  822. Length uint32
  823. SecurityDescriptor *SECURITY_DESCRIPTOR
  824. InheritHandle uint32
  825. }
  826. type SE_OBJECT_TYPE uint32
  827. // Constants for type SE_OBJECT_TYPE
  828. const (
  829. SE_UNKNOWN_OBJECT_TYPE = 0
  830. SE_FILE_OBJECT = 1
  831. SE_SERVICE = 2
  832. SE_PRINTER = 3
  833. SE_REGISTRY_KEY = 4
  834. SE_LMSHARE = 5
  835. SE_KERNEL_OBJECT = 6
  836. SE_WINDOW_OBJECT = 7
  837. SE_DS_OBJECT = 8
  838. SE_DS_OBJECT_ALL = 9
  839. SE_PROVIDER_DEFINED_OBJECT = 10
  840. SE_WMIGUID_OBJECT = 11
  841. SE_REGISTRY_WOW64_32KEY = 12
  842. SE_REGISTRY_WOW64_64KEY = 13
  843. )
  844. type SECURITY_INFORMATION uint32
  845. // Constants for type SECURITY_INFORMATION
  846. const (
  847. OWNER_SECURITY_INFORMATION = 0x00000001
  848. GROUP_SECURITY_INFORMATION = 0x00000002
  849. DACL_SECURITY_INFORMATION = 0x00000004
  850. SACL_SECURITY_INFORMATION = 0x00000008
  851. LABEL_SECURITY_INFORMATION = 0x00000010
  852. ATTRIBUTE_SECURITY_INFORMATION = 0x00000020
  853. SCOPE_SECURITY_INFORMATION = 0x00000040
  854. BACKUP_SECURITY_INFORMATION = 0x00010000
  855. PROTECTED_DACL_SECURITY_INFORMATION = 0x80000000
  856. PROTECTED_SACL_SECURITY_INFORMATION = 0x40000000
  857. UNPROTECTED_DACL_SECURITY_INFORMATION = 0x20000000
  858. UNPROTECTED_SACL_SECURITY_INFORMATION = 0x10000000
  859. )
  860. type SECURITY_DESCRIPTOR_CONTROL uint16
  861. // Constants for type SECURITY_DESCRIPTOR_CONTROL
  862. const (
  863. SE_OWNER_DEFAULTED = 0x0001
  864. SE_GROUP_DEFAULTED = 0x0002
  865. SE_DACL_PRESENT = 0x0004
  866. SE_DACL_DEFAULTED = 0x0008
  867. SE_SACL_PRESENT = 0x0010
  868. SE_SACL_DEFAULTED = 0x0020
  869. SE_DACL_AUTO_INHERIT_REQ = 0x0100
  870. SE_SACL_AUTO_INHERIT_REQ = 0x0200
  871. SE_DACL_AUTO_INHERITED = 0x0400
  872. SE_SACL_AUTO_INHERITED = 0x0800
  873. SE_DACL_PROTECTED = 0x1000
  874. SE_SACL_PROTECTED = 0x2000
  875. SE_RM_CONTROL_VALID = 0x4000
  876. SE_SELF_RELATIVE = 0x8000
  877. )
  878. type ACCESS_MASK uint32
  879. // Constants for type ACCESS_MASK
  880. const (
  881. DELETE = 0x00010000
  882. READ_CONTROL = 0x00020000
  883. WRITE_DAC = 0x00040000
  884. WRITE_OWNER = 0x00080000
  885. SYNCHRONIZE = 0x00100000
  886. STANDARD_RIGHTS_REQUIRED = 0x000F0000
  887. STANDARD_RIGHTS_READ = READ_CONTROL
  888. STANDARD_RIGHTS_WRITE = READ_CONTROL
  889. STANDARD_RIGHTS_EXECUTE = READ_CONTROL
  890. STANDARD_RIGHTS_ALL = 0x001F0000
  891. SPECIFIC_RIGHTS_ALL = 0x0000FFFF
  892. ACCESS_SYSTEM_SECURITY = 0x01000000
  893. MAXIMUM_ALLOWED = 0x02000000
  894. GENERIC_READ = 0x80000000
  895. GENERIC_WRITE = 0x40000000
  896. GENERIC_EXECUTE = 0x20000000
  897. GENERIC_ALL = 0x10000000
  898. )
  899. type ACCESS_MODE uint32
  900. // Constants for type ACCESS_MODE
  901. const (
  902. NOT_USED_ACCESS = 0
  903. GRANT_ACCESS = 1
  904. SET_ACCESS = 2
  905. DENY_ACCESS = 3
  906. REVOKE_ACCESS = 4
  907. SET_AUDIT_SUCCESS = 5
  908. SET_AUDIT_FAILURE = 6
  909. )
  910. // Constants for AceFlags and Inheritance fields
  911. const (
  912. NO_INHERITANCE = 0x0
  913. SUB_OBJECTS_ONLY_INHERIT = 0x1
  914. SUB_CONTAINERS_ONLY_INHERIT = 0x2
  915. SUB_CONTAINERS_AND_OBJECTS_INHERIT = 0x3
  916. INHERIT_NO_PROPAGATE = 0x4
  917. INHERIT_ONLY = 0x8
  918. INHERITED_ACCESS_ENTRY = 0x10
  919. INHERITED_PARENT = 0x10000000
  920. INHERITED_GRANDPARENT = 0x20000000
  921. OBJECT_INHERIT_ACE = 0x1
  922. CONTAINER_INHERIT_ACE = 0x2
  923. NO_PROPAGATE_INHERIT_ACE = 0x4
  924. INHERIT_ONLY_ACE = 0x8
  925. INHERITED_ACE = 0x10
  926. VALID_INHERIT_FLAGS = 0x1F
  927. )
  928. type MULTIPLE_TRUSTEE_OPERATION uint32
  929. // Constants for MULTIPLE_TRUSTEE_OPERATION
  930. const (
  931. NO_MULTIPLE_TRUSTEE = 0
  932. TRUSTEE_IS_IMPERSONATE = 1
  933. )
  934. type TRUSTEE_FORM uint32
  935. // Constants for TRUSTEE_FORM
  936. const (
  937. TRUSTEE_IS_SID = 0
  938. TRUSTEE_IS_NAME = 1
  939. TRUSTEE_BAD_FORM = 2
  940. TRUSTEE_IS_OBJECTS_AND_SID = 3
  941. TRUSTEE_IS_OBJECTS_AND_NAME = 4
  942. )
  943. type TRUSTEE_TYPE uint32
  944. // Constants for TRUSTEE_TYPE
  945. const (
  946. TRUSTEE_IS_UNKNOWN = 0
  947. TRUSTEE_IS_USER = 1
  948. TRUSTEE_IS_GROUP = 2
  949. TRUSTEE_IS_DOMAIN = 3
  950. TRUSTEE_IS_ALIAS = 4
  951. TRUSTEE_IS_WELL_KNOWN_GROUP = 5
  952. TRUSTEE_IS_DELETED = 6
  953. TRUSTEE_IS_INVALID = 7
  954. TRUSTEE_IS_COMPUTER = 8
  955. )
  956. // Constants for ObjectsPresent field
  957. const (
  958. ACE_OBJECT_TYPE_PRESENT = 0x1
  959. ACE_INHERITED_OBJECT_TYPE_PRESENT = 0x2
  960. )
  961. type EXPLICIT_ACCESS struct {
  962. AccessPermissions ACCESS_MASK
  963. AccessMode ACCESS_MODE
  964. Inheritance uint32
  965. Trustee TRUSTEE
  966. }
  967. // This type is the union inside of TRUSTEE and must be created using one of the TrusteeValueFrom* functions.
  968. type TrusteeValue uintptr
  969. func TrusteeValueFromString(str string) TrusteeValue {
  970. return TrusteeValue(unsafe.Pointer(StringToUTF16Ptr(str)))
  971. }
  972. func TrusteeValueFromSID(sid *SID) TrusteeValue {
  973. return TrusteeValue(unsafe.Pointer(sid))
  974. }
  975. func TrusteeValueFromObjectsAndSid(objectsAndSid *OBJECTS_AND_SID) TrusteeValue {
  976. return TrusteeValue(unsafe.Pointer(objectsAndSid))
  977. }
  978. func TrusteeValueFromObjectsAndName(objectsAndName *OBJECTS_AND_NAME) TrusteeValue {
  979. return TrusteeValue(unsafe.Pointer(objectsAndName))
  980. }
  981. type TRUSTEE struct {
  982. MultipleTrustee *TRUSTEE
  983. MultipleTrusteeOperation MULTIPLE_TRUSTEE_OPERATION
  984. TrusteeForm TRUSTEE_FORM
  985. TrusteeType TRUSTEE_TYPE
  986. TrusteeValue TrusteeValue
  987. }
  988. type OBJECTS_AND_SID struct {
  989. ObjectsPresent uint32
  990. ObjectTypeGuid GUID
  991. InheritedObjectTypeGuid GUID
  992. Sid *SID
  993. }
  994. type OBJECTS_AND_NAME struct {
  995. ObjectsPresent uint32
  996. ObjectType SE_OBJECT_TYPE
  997. ObjectTypeName *uint16
  998. InheritedObjectTypeName *uint16
  999. Name *uint16
  1000. }
  1001. //sys getSecurityInfo(handle Handle, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION, owner **SID, group **SID, dacl **ACL, sacl **ACL, sd **SECURITY_DESCRIPTOR) (ret error) = advapi32.GetSecurityInfo
  1002. //sys SetSecurityInfo(handle Handle, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION, owner *SID, group *SID, dacl *ACL, sacl *ACL) = advapi32.SetSecurityInfo
  1003. //sys getNamedSecurityInfo(objectName string, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION, owner **SID, group **SID, dacl **ACL, sacl **ACL, sd **SECURITY_DESCRIPTOR) (ret error) = advapi32.GetNamedSecurityInfoW
  1004. //sys SetNamedSecurityInfo(objectName string, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION, owner *SID, group *SID, dacl *ACL, sacl *ACL) (ret error) = advapi32.SetNamedSecurityInfoW
  1005. //sys buildSecurityDescriptor(owner *TRUSTEE, group *TRUSTEE, countAccessEntries uint32, accessEntries *EXPLICIT_ACCESS, countAuditEntries uint32, auditEntries *EXPLICIT_ACCESS, oldSecurityDescriptor *SECURITY_DESCRIPTOR, sizeNewSecurityDescriptor *uint32, newSecurityDescriptor **SECURITY_DESCRIPTOR) (ret error) = advapi32.BuildSecurityDescriptorW
  1006. //sys initializeSecurityDescriptor(absoluteSD *SECURITY_DESCRIPTOR, revision uint32) (err error) = advapi32.InitializeSecurityDescriptor
  1007. //sys getSecurityDescriptorControl(sd *SECURITY_DESCRIPTOR, control *SECURITY_DESCRIPTOR_CONTROL, revision *uint32) (err error) = advapi32.GetSecurityDescriptorControl
  1008. //sys getSecurityDescriptorDacl(sd *SECURITY_DESCRIPTOR, daclPresent *bool, dacl **ACL, daclDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorDacl
  1009. //sys getSecurityDescriptorSacl(sd *SECURITY_DESCRIPTOR, saclPresent *bool, sacl **ACL, saclDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorSacl
  1010. //sys getSecurityDescriptorOwner(sd *SECURITY_DESCRIPTOR, owner **SID, ownerDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorOwner
  1011. //sys getSecurityDescriptorGroup(sd *SECURITY_DESCRIPTOR, group **SID, groupDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorGroup
  1012. //sys getSecurityDescriptorLength(sd *SECURITY_DESCRIPTOR) (len uint32) = advapi32.GetSecurityDescriptorLength
  1013. //sys getSecurityDescriptorRMControl(sd *SECURITY_DESCRIPTOR, rmControl *uint8) (ret error) [failretval!=0] = advapi32.GetSecurityDescriptorRMControl
  1014. //sys isValidSecurityDescriptor(sd *SECURITY_DESCRIPTOR) (isValid bool) = advapi32.IsValidSecurityDescriptor
  1015. //sys setSecurityDescriptorControl(sd *SECURITY_DESCRIPTOR, controlBitsOfInterest SECURITY_DESCRIPTOR_CONTROL, controlBitsToSet SECURITY_DESCRIPTOR_CONTROL) (err error) = advapi32.SetSecurityDescriptorControl
  1016. //sys setSecurityDescriptorDacl(sd *SECURITY_DESCRIPTOR, daclPresent bool, dacl *ACL, daclDefaulted bool) (err error) = advapi32.SetSecurityDescriptorDacl
  1017. //sys setSecurityDescriptorSacl(sd *SECURITY_DESCRIPTOR, saclPresent bool, sacl *ACL, saclDefaulted bool) (err error) = advapi32.SetSecurityDescriptorSacl
  1018. //sys setSecurityDescriptorOwner(sd *SECURITY_DESCRIPTOR, owner *SID, ownerDefaulted bool) (err error) = advapi32.SetSecurityDescriptorOwner
  1019. //sys setSecurityDescriptorGroup(sd *SECURITY_DESCRIPTOR, group *SID, groupDefaulted bool) (err error) = advapi32.SetSecurityDescriptorGroup
  1020. //sys setSecurityDescriptorRMControl(sd *SECURITY_DESCRIPTOR, rmControl *uint8) = advapi32.SetSecurityDescriptorRMControl
  1021. //sys convertStringSecurityDescriptorToSecurityDescriptor(str string, revision uint32, sd **SECURITY_DESCRIPTOR, size *uint32) (err error) = advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW
  1022. //sys convertSecurityDescriptorToStringSecurityDescriptor(sd *SECURITY_DESCRIPTOR, revision uint32, securityInformation SECURITY_INFORMATION, str **uint16, strLen *uint32) (err error) = advapi32.ConvertSecurityDescriptorToStringSecurityDescriptorW
  1023. //sys makeAbsoluteSD(selfRelativeSD *SECURITY_DESCRIPTOR, absoluteSD *SECURITY_DESCRIPTOR, absoluteSDSize *uint32, dacl *ACL, daclSize *uint32, sacl *ACL, saclSize *uint32, owner *SID, ownerSize *uint32, group *SID, groupSize *uint32) (err error) = advapi32.MakeAbsoluteSD
  1024. //sys makeSelfRelativeSD(absoluteSD *SECURITY_DESCRIPTOR, selfRelativeSD *SECURITY_DESCRIPTOR, selfRelativeSDSize *uint32) (err error) = advapi32.MakeSelfRelativeSD
  1025. //sys setEntriesInAcl(countExplicitEntries uint32, explicitEntries *EXPLICIT_ACCESS, oldACL *ACL, newACL **ACL) (ret error) = advapi32.SetEntriesInAclW
  1026. // Control returns the security descriptor control bits.
  1027. func (sd *SECURITY_DESCRIPTOR) Control() (control SECURITY_DESCRIPTOR_CONTROL, revision uint32, err error) {
  1028. err = getSecurityDescriptorControl(sd, &control, &revision)
  1029. return
  1030. }
  1031. // SetControl sets the security descriptor control bits.
  1032. func (sd *SECURITY_DESCRIPTOR) SetControl(controlBitsOfInterest SECURITY_DESCRIPTOR_CONTROL, controlBitsToSet SECURITY_DESCRIPTOR_CONTROL) error {
  1033. return setSecurityDescriptorControl(sd, controlBitsOfInterest, controlBitsToSet)
  1034. }
  1035. // RMControl returns the security descriptor resource manager control bits.
  1036. func (sd *SECURITY_DESCRIPTOR) RMControl() (control uint8, err error) {
  1037. err = getSecurityDescriptorRMControl(sd, &control)
  1038. return
  1039. }
  1040. // SetRMControl sets the security descriptor resource manager control bits.
  1041. func (sd *SECURITY_DESCRIPTOR) SetRMControl(rmControl uint8) {
  1042. setSecurityDescriptorRMControl(sd, &rmControl)
  1043. }
  1044. // DACL returns the security descriptor DACL and whether it was defaulted. The dacl return value may be nil
  1045. // if a DACL exists but is an "empty DACL", meaning fully permissive. If the DACL does not exist, err returns
  1046. // ERROR_OBJECT_NOT_FOUND.
  1047. func (sd *SECURITY_DESCRIPTOR) DACL() (dacl *ACL, defaulted bool, err error) {
  1048. var present bool
  1049. err = getSecurityDescriptorDacl(sd, &present, &dacl, &defaulted)
  1050. if !present {
  1051. err = ERROR_OBJECT_NOT_FOUND
  1052. }
  1053. return
  1054. }
  1055. // SetDACL sets the absolute security descriptor DACL.
  1056. func (absoluteSD *SECURITY_DESCRIPTOR) SetDACL(dacl *ACL, present, defaulted bool) error {
  1057. return setSecurityDescriptorDacl(absoluteSD, present, dacl, defaulted)
  1058. }
  1059. // SACL returns the security descriptor SACL and whether it was defaulted. The sacl return value may be nil
  1060. // if a SACL exists but is an "empty SACL", meaning fully permissive. If the SACL does not exist, err returns
  1061. // ERROR_OBJECT_NOT_FOUND.
  1062. func (sd *SECURITY_DESCRIPTOR) SACL() (sacl *ACL, defaulted bool, err error) {
  1063. var present bool
  1064. err = getSecurityDescriptorSacl(sd, &present, &sacl, &defaulted)
  1065. if !present {
  1066. err = ERROR_OBJECT_NOT_FOUND
  1067. }
  1068. return
  1069. }
  1070. // SetSACL sets the absolute security descriptor SACL.
  1071. func (absoluteSD *SECURITY_DESCRIPTOR) SetSACL(sacl *ACL, present, defaulted bool) error {
  1072. return setSecurityDescriptorSacl(absoluteSD, present, sacl, defaulted)
  1073. }
  1074. // Owner returns the security descriptor owner and whether it was defaulted.
  1075. func (sd *SECURITY_DESCRIPTOR) Owner() (owner *SID, defaulted bool, err error) {
  1076. err = getSecurityDescriptorOwner(sd, &owner, &defaulted)
  1077. return
  1078. }
  1079. // SetOwner sets the absolute security descriptor owner.
  1080. func (absoluteSD *SECURITY_DESCRIPTOR) SetOwner(owner *SID, defaulted bool) error {
  1081. return setSecurityDescriptorOwner(absoluteSD, owner, defaulted)
  1082. }
  1083. // Group returns the security descriptor group and whether it was defaulted.
  1084. func (sd *SECURITY_DESCRIPTOR) Group() (group *SID, defaulted bool, err error) {
  1085. err = getSecurityDescriptorGroup(sd, &group, &defaulted)
  1086. return
  1087. }
  1088. // SetGroup sets the absolute security descriptor owner.
  1089. func (absoluteSD *SECURITY_DESCRIPTOR) SetGroup(group *SID, defaulted bool) error {
  1090. return setSecurityDescriptorGroup(absoluteSD, group, defaulted)
  1091. }
  1092. // Length returns the length of the security descriptor.
  1093. func (sd *SECURITY_DESCRIPTOR) Length() uint32 {
  1094. return getSecurityDescriptorLength(sd)
  1095. }
  1096. // IsValid returns whether the security descriptor is valid.
  1097. func (sd *SECURITY_DESCRIPTOR) IsValid() bool {
  1098. return isValidSecurityDescriptor(sd)
  1099. }
  1100. // String returns the SDDL form of the security descriptor, with a function signature that can be
  1101. // used with %v formatting directives.
  1102. func (sd *SECURITY_DESCRIPTOR) String() string {
  1103. var sddl *uint16
  1104. err := convertSecurityDescriptorToStringSecurityDescriptor(sd, 1, 0xff, &sddl, nil)
  1105. if err != nil {
  1106. return ""
  1107. }
  1108. defer LocalFree(Handle(unsafe.Pointer(sddl)))
  1109. return UTF16ToString((*[(1 << 30) - 1]uint16)(unsafe.Pointer(sddl))[:])
  1110. }
  1111. // ToAbsolute converts a self-relative security descriptor into an absolute one.
  1112. func (selfRelativeSD *SECURITY_DESCRIPTOR) ToAbsolute() (absoluteSD *SECURITY_DESCRIPTOR, err error) {
  1113. control, _, err := selfRelativeSD.Control()
  1114. if err != nil {
  1115. return
  1116. }
  1117. if control&SE_SELF_RELATIVE == 0 {
  1118. err = ERROR_INVALID_PARAMETER
  1119. return
  1120. }
  1121. var absoluteSDSize, daclSize, saclSize, ownerSize, groupSize uint32
  1122. err = makeAbsoluteSD(selfRelativeSD, nil, &absoluteSDSize,
  1123. nil, &daclSize, nil, &saclSize, nil, &ownerSize, nil, &groupSize)
  1124. switch err {
  1125. case ERROR_INSUFFICIENT_BUFFER:
  1126. case nil:
  1127. // makeAbsoluteSD is expected to fail, but it succeeds.
  1128. return nil, ERROR_INTERNAL_ERROR
  1129. default:
  1130. return nil, err
  1131. }
  1132. if absoluteSDSize > 0 {
  1133. absoluteSD = (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&make([]byte, absoluteSDSize)[0]))
  1134. }
  1135. var (
  1136. dacl *ACL
  1137. sacl *ACL
  1138. owner *SID
  1139. group *SID
  1140. )
  1141. if daclSize > 0 {
  1142. dacl = (*ACL)(unsafe.Pointer(&make([]byte, daclSize)[0]))
  1143. }
  1144. if saclSize > 0 {
  1145. sacl = (*ACL)(unsafe.Pointer(&make([]byte, saclSize)[0]))
  1146. }
  1147. if ownerSize > 0 {
  1148. owner = (*SID)(unsafe.Pointer(&make([]byte, ownerSize)[0]))
  1149. }
  1150. if groupSize > 0 {
  1151. group = (*SID)(unsafe.Pointer(&make([]byte, groupSize)[0]))
  1152. }
  1153. err = makeAbsoluteSD(selfRelativeSD, absoluteSD, &absoluteSDSize,
  1154. dacl, &daclSize, sacl, &saclSize, owner, &ownerSize, group, &groupSize)
  1155. return
  1156. }
  1157. // ToSelfRelative converts an absolute security descriptor into a self-relative one.
  1158. func (absoluteSD *SECURITY_DESCRIPTOR) ToSelfRelative() (selfRelativeSD *SECURITY_DESCRIPTOR, err error) {
  1159. control, _, err := absoluteSD.Control()
  1160. if err != nil {
  1161. return
  1162. }
  1163. if control&SE_SELF_RELATIVE != 0 {
  1164. err = ERROR_INVALID_PARAMETER
  1165. return
  1166. }
  1167. var selfRelativeSDSize uint32
  1168. err = makeSelfRelativeSD(absoluteSD, nil, &selfRelativeSDSize)
  1169. switch err {
  1170. case ERROR_INSUFFICIENT_BUFFER:
  1171. case nil:
  1172. // makeSelfRelativeSD is expected to fail, but it succeeds.
  1173. return nil, ERROR_INTERNAL_ERROR
  1174. default:
  1175. return nil, err
  1176. }
  1177. if selfRelativeSDSize > 0 {
  1178. selfRelativeSD = (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&make([]byte, selfRelativeSDSize)[0]))
  1179. }
  1180. err = makeSelfRelativeSD(absoluteSD, selfRelativeSD, &selfRelativeSDSize)
  1181. return
  1182. }
  1183. func (selfRelativeSD *SECURITY_DESCRIPTOR) copySelfRelativeSecurityDescriptor() *SECURITY_DESCRIPTOR {
  1184. sdBytes := make([]byte, selfRelativeSD.Length())
  1185. copy(sdBytes, (*[(1 << 31) - 1]byte)(unsafe.Pointer(selfRelativeSD))[:len(sdBytes)])
  1186. return (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&sdBytes[0]))
  1187. }
  1188. // SecurityDescriptorFromString converts an SDDL string describing a security descriptor into a
  1189. // self-relative security descriptor object allocated on the Go heap.
  1190. func SecurityDescriptorFromString(sddl string) (sd *SECURITY_DESCRIPTOR, err error) {
  1191. var winHeapSD *SECURITY_DESCRIPTOR
  1192. err = convertStringSecurityDescriptorToSecurityDescriptor(sddl, 1, &winHeapSD, nil)
  1193. if err != nil {
  1194. return
  1195. }
  1196. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1197. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1198. }
  1199. // GetSecurityInfo queries the security information for a given handle and returns the self-relative security
  1200. // descriptor result on the Go heap.
  1201. func GetSecurityInfo(handle Handle, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION) (sd *SECURITY_DESCRIPTOR, err error) {
  1202. var winHeapSD *SECURITY_DESCRIPTOR
  1203. err = getSecurityInfo(handle, objectType, securityInformation, nil, nil, nil, nil, &winHeapSD)
  1204. if err != nil {
  1205. return
  1206. }
  1207. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1208. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1209. }
  1210. // GetNamedSecurityInfo queries the security information for a given named object and returns the self-relative security
  1211. // descriptor result on the Go heap.
  1212. func GetNamedSecurityInfo(objectName string, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION) (sd *SECURITY_DESCRIPTOR, err error) {
  1213. var winHeapSD *SECURITY_DESCRIPTOR
  1214. err = getNamedSecurityInfo(objectName, objectType, securityInformation, nil, nil, nil, nil, &winHeapSD)
  1215. if err != nil {
  1216. return
  1217. }
  1218. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1219. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1220. }
  1221. // BuildSecurityDescriptor makes a new security descriptor using the input trustees, explicit access lists, and
  1222. // prior security descriptor to be merged, any of which can be nil, returning the self-relative security descriptor
  1223. // result on the Go heap.
  1224. func BuildSecurityDescriptor(owner *TRUSTEE, group *TRUSTEE, accessEntries []EXPLICIT_ACCESS, auditEntries []EXPLICIT_ACCESS, mergedSecurityDescriptor *SECURITY_DESCRIPTOR) (sd *SECURITY_DESCRIPTOR, err error) {
  1225. var winHeapSD *SECURITY_DESCRIPTOR
  1226. var winHeapSDSize uint32
  1227. var firstAccessEntry *EXPLICIT_ACCESS
  1228. if len(accessEntries) > 0 {
  1229. firstAccessEntry = &accessEntries[0]
  1230. }
  1231. var firstAuditEntry *EXPLICIT_ACCESS
  1232. if len(auditEntries) > 0 {
  1233. firstAuditEntry = &auditEntries[0]
  1234. }
  1235. err = buildSecurityDescriptor(owner, group, uint32(len(accessEntries)), firstAccessEntry, uint32(len(auditEntries)), firstAuditEntry, mergedSecurityDescriptor, &winHeapSDSize, &winHeapSD)
  1236. if err != nil {
  1237. return
  1238. }
  1239. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1240. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1241. }
  1242. // NewSecurityDescriptor creates and initializes a new absolute security descriptor.
  1243. func NewSecurityDescriptor() (absoluteSD *SECURITY_DESCRIPTOR, err error) {
  1244. absoluteSD = &SECURITY_DESCRIPTOR{}
  1245. err = initializeSecurityDescriptor(absoluteSD, 1)
  1246. return
  1247. }
  1248. // ACLFromEntries returns a new ACL on the Go heap containing a list of explicit entries as well as those of another ACL.
  1249. // Both explicitEntries and mergedACL are optional and can be nil.
  1250. func ACLFromEntries(explicitEntries []EXPLICIT_ACCESS, mergedACL *ACL) (acl *ACL, err error) {
  1251. var firstExplicitEntry *EXPLICIT_ACCESS
  1252. if len(explicitEntries) > 0 {
  1253. firstExplicitEntry = &explicitEntries[0]
  1254. }
  1255. var winHeapACL *ACL
  1256. err = setEntriesInAcl(uint32(len(explicitEntries)), firstExplicitEntry, mergedACL, &winHeapACL)
  1257. if err != nil {
  1258. return
  1259. }
  1260. defer LocalFree(Handle(unsafe.Pointer(winHeapACL)))
  1261. aclBytes := make([]byte, winHeapACL.aclSize)
  1262. copy(aclBytes, (*[(1 << 31) - 1]byte)(unsafe.Pointer(winHeapACL))[:len(aclBytes)])
  1263. return (*ACL)(unsafe.Pointer(&aclBytes[0])), nil
  1264. }