security_windows.go 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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, error) {
  604. p, e := GetCurrentProcess()
  605. if e != nil {
  606. return 0, e
  607. }
  608. var t Token
  609. e = OpenProcessToken(p, TOKEN_QUERY|TOKEN_DUPLICATE, &t)
  610. if e != nil {
  611. return 0, e
  612. }
  613. return t, nil
  614. }
  615. // GetCurrentProcessToken returns the access token associated with
  616. // the current process. It is a pseudo token that does not need
  617. // to be closed.
  618. func GetCurrentProcessToken() Token {
  619. return Token(^uintptr(4 - 1))
  620. }
  621. // GetCurrentThreadToken return the access token associated with
  622. // the current thread. It is a pseudo token that does not need
  623. // to be closed.
  624. func GetCurrentThreadToken() Token {
  625. return Token(^uintptr(5 - 1))
  626. }
  627. // GetCurrentThreadEffectiveToken returns the effective access token
  628. // associated with the current thread. It is a pseudo token that does
  629. // not need to be closed.
  630. func GetCurrentThreadEffectiveToken() Token {
  631. return Token(^uintptr(6 - 1))
  632. }
  633. // Close releases access to access token.
  634. func (t Token) Close() error {
  635. return CloseHandle(Handle(t))
  636. }
  637. // getInfo retrieves a specified type of information about an access token.
  638. func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) {
  639. n := uint32(initSize)
  640. for {
  641. b := make([]byte, n)
  642. e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
  643. if e == nil {
  644. return unsafe.Pointer(&b[0]), nil
  645. }
  646. if e != ERROR_INSUFFICIENT_BUFFER {
  647. return nil, e
  648. }
  649. if n <= uint32(len(b)) {
  650. return nil, e
  651. }
  652. }
  653. }
  654. // GetTokenUser retrieves access token t user account information.
  655. func (t Token) GetTokenUser() (*Tokenuser, error) {
  656. i, e := t.getInfo(TokenUser, 50)
  657. if e != nil {
  658. return nil, e
  659. }
  660. return (*Tokenuser)(i), nil
  661. }
  662. // GetTokenGroups retrieves group accounts associated with access token t.
  663. func (t Token) GetTokenGroups() (*Tokengroups, error) {
  664. i, e := t.getInfo(TokenGroups, 50)
  665. if e != nil {
  666. return nil, e
  667. }
  668. return (*Tokengroups)(i), nil
  669. }
  670. // GetTokenPrimaryGroup retrieves access token t primary group information.
  671. // A pointer to a SID structure representing a group that will become
  672. // the primary group of any objects created by a process using this access token.
  673. func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
  674. i, e := t.getInfo(TokenPrimaryGroup, 50)
  675. if e != nil {
  676. return nil, e
  677. }
  678. return (*Tokenprimarygroup)(i), nil
  679. }
  680. // GetUserProfileDirectory retrieves path to the
  681. // root directory of the access token t user's profile.
  682. func (t Token) GetUserProfileDirectory() (string, error) {
  683. n := uint32(100)
  684. for {
  685. b := make([]uint16, n)
  686. e := GetUserProfileDirectory(t, &b[0], &n)
  687. if e == nil {
  688. return UTF16ToString(b), nil
  689. }
  690. if e != ERROR_INSUFFICIENT_BUFFER {
  691. return "", e
  692. }
  693. if n <= uint32(len(b)) {
  694. return "", e
  695. }
  696. }
  697. }
  698. // IsElevated returns whether the current token is elevated from a UAC perspective.
  699. func (token Token) IsElevated() bool {
  700. var isElevated uint32
  701. var outLen uint32
  702. err := GetTokenInformation(token, TokenElevation, (*byte)(unsafe.Pointer(&isElevated)), uint32(unsafe.Sizeof(isElevated)), &outLen)
  703. if err != nil {
  704. return false
  705. }
  706. return outLen == uint32(unsafe.Sizeof(isElevated)) && isElevated != 0
  707. }
  708. // GetLinkedToken returns the linked token, which may be an elevated UAC token.
  709. func (token Token) GetLinkedToken() (Token, error) {
  710. var linkedToken Token
  711. var outLen uint32
  712. err := GetTokenInformation(token, TokenLinkedToken, (*byte)(unsafe.Pointer(&linkedToken)), uint32(unsafe.Sizeof(linkedToken)), &outLen)
  713. if err != nil {
  714. return Token(0), err
  715. }
  716. return linkedToken, nil
  717. }
  718. // GetSystemDirectory retrieves the path to current location of the system
  719. // directory, which is typically, though not always, `C:\Windows\System32`.
  720. func GetSystemDirectory() (string, error) {
  721. n := uint32(MAX_PATH)
  722. for {
  723. b := make([]uint16, n)
  724. l, e := getSystemDirectory(&b[0], n)
  725. if e != nil {
  726. return "", e
  727. }
  728. if l <= n {
  729. return UTF16ToString(b[:l]), nil
  730. }
  731. n = l
  732. }
  733. }
  734. // GetWindowsDirectory retrieves the path to current location of the Windows
  735. // directory, which is typically, though not always, `C:\Windows`. This may
  736. // be a private user directory in the case that the application is running
  737. // under a terminal server.
  738. func GetWindowsDirectory() (string, error) {
  739. n := uint32(MAX_PATH)
  740. for {
  741. b := make([]uint16, n)
  742. l, e := getWindowsDirectory(&b[0], n)
  743. if e != nil {
  744. return "", e
  745. }
  746. if l <= n {
  747. return UTF16ToString(b[:l]), nil
  748. }
  749. n = l
  750. }
  751. }
  752. // GetSystemWindowsDirectory retrieves the path to current location of the
  753. // Windows directory, which is typically, though not always, `C:\Windows`.
  754. func GetSystemWindowsDirectory() (string, error) {
  755. n := uint32(MAX_PATH)
  756. for {
  757. b := make([]uint16, n)
  758. l, e := getSystemWindowsDirectory(&b[0], n)
  759. if e != nil {
  760. return "", e
  761. }
  762. if l <= n {
  763. return UTF16ToString(b[:l]), nil
  764. }
  765. n = l
  766. }
  767. }
  768. // IsMember reports whether the access token t is a member of the provided SID.
  769. func (t Token) IsMember(sid *SID) (bool, error) {
  770. var b int32
  771. if e := checkTokenMembership(t, sid, &b); e != nil {
  772. return false, e
  773. }
  774. return b != 0, nil
  775. }
  776. const (
  777. WTS_CONSOLE_CONNECT = 0x1
  778. WTS_CONSOLE_DISCONNECT = 0x2
  779. WTS_REMOTE_CONNECT = 0x3
  780. WTS_REMOTE_DISCONNECT = 0x4
  781. WTS_SESSION_LOGON = 0x5
  782. WTS_SESSION_LOGOFF = 0x6
  783. WTS_SESSION_LOCK = 0x7
  784. WTS_SESSION_UNLOCK = 0x8
  785. WTS_SESSION_REMOTE_CONTROL = 0x9
  786. WTS_SESSION_CREATE = 0xa
  787. WTS_SESSION_TERMINATE = 0xb
  788. )
  789. const (
  790. WTSActive = 0
  791. WTSConnected = 1
  792. WTSConnectQuery = 2
  793. WTSShadow = 3
  794. WTSDisconnected = 4
  795. WTSIdle = 5
  796. WTSListen = 6
  797. WTSReset = 7
  798. WTSDown = 8
  799. WTSInit = 9
  800. )
  801. type WTSSESSION_NOTIFICATION struct {
  802. Size uint32
  803. SessionID uint32
  804. }
  805. type WTS_SESSION_INFO struct {
  806. SessionID uint32
  807. WindowStationName *uint16
  808. State uint32
  809. }
  810. //sys WTSQueryUserToken(session uint32, token *Token) (err error) = wtsapi32.WTSQueryUserToken
  811. //sys WTSEnumerateSessions(handle Handle, reserved uint32, version uint32, sessions **WTS_SESSION_INFO, count *uint32) (err error) = wtsapi32.WTSEnumerateSessionsW
  812. //sys WTSFreeMemory(ptr uintptr) = wtsapi32.WTSFreeMemory
  813. type ACL struct {
  814. aclRevision byte
  815. sbz1 byte
  816. aclSize uint16
  817. aceCount uint16
  818. sbz2 uint16
  819. }
  820. type SECURITY_DESCRIPTOR struct {
  821. revision byte
  822. sbz1 byte
  823. control SECURITY_DESCRIPTOR_CONTROL
  824. owner *SID
  825. group *SID
  826. sacl *ACL
  827. dacl *ACL
  828. }
  829. type SE_OBJECT_TYPE uint32
  830. // Constants for type SE_OBJECT_TYPE
  831. const (
  832. SE_UNKNOWN_OBJECT_TYPE = 0
  833. SE_FILE_OBJECT = 1
  834. SE_SERVICE = 2
  835. SE_PRINTER = 3
  836. SE_REGISTRY_KEY = 4
  837. SE_LMSHARE = 5
  838. SE_KERNEL_OBJECT = 6
  839. SE_WINDOW_OBJECT = 7
  840. SE_DS_OBJECT = 8
  841. SE_DS_OBJECT_ALL = 9
  842. SE_PROVIDER_DEFINED_OBJECT = 10
  843. SE_WMIGUID_OBJECT = 11
  844. SE_REGISTRY_WOW64_32KEY = 12
  845. SE_REGISTRY_WOW64_64KEY = 13
  846. )
  847. type SECURITY_INFORMATION uint32
  848. // Constants for type SECURITY_INFORMATION
  849. const (
  850. OWNER_SECURITY_INFORMATION = 0x00000001
  851. GROUP_SECURITY_INFORMATION = 0x00000002
  852. DACL_SECURITY_INFORMATION = 0x00000004
  853. SACL_SECURITY_INFORMATION = 0x00000008
  854. LABEL_SECURITY_INFORMATION = 0x00000010
  855. ATTRIBUTE_SECURITY_INFORMATION = 0x00000020
  856. SCOPE_SECURITY_INFORMATION = 0x00000040
  857. BACKUP_SECURITY_INFORMATION = 0x00010000
  858. PROTECTED_DACL_SECURITY_INFORMATION = 0x80000000
  859. PROTECTED_SACL_SECURITY_INFORMATION = 0x40000000
  860. UNPROTECTED_DACL_SECURITY_INFORMATION = 0x20000000
  861. UNPROTECTED_SACL_SECURITY_INFORMATION = 0x10000000
  862. )
  863. type SECURITY_DESCRIPTOR_CONTROL uint16
  864. // Constants for type SECURITY_DESCRIPTOR_CONTROL
  865. const (
  866. SE_OWNER_DEFAULTED = 0x0001
  867. SE_GROUP_DEFAULTED = 0x0002
  868. SE_DACL_PRESENT = 0x0004
  869. SE_DACL_DEFAULTED = 0x0008
  870. SE_SACL_PRESENT = 0x0010
  871. SE_SACL_DEFAULTED = 0x0020
  872. SE_DACL_AUTO_INHERIT_REQ = 0x0100
  873. SE_SACL_AUTO_INHERIT_REQ = 0x0200
  874. SE_DACL_AUTO_INHERITED = 0x0400
  875. SE_SACL_AUTO_INHERITED = 0x0800
  876. SE_DACL_PROTECTED = 0x1000
  877. SE_SACL_PROTECTED = 0x2000
  878. SE_RM_CONTROL_VALID = 0x4000
  879. SE_SELF_RELATIVE = 0x8000
  880. )
  881. type ACCESS_MASK uint32
  882. // Constants for type ACCESS_MASK
  883. const (
  884. DELETE = 0x00010000
  885. READ_CONTROL = 0x00020000
  886. WRITE_DAC = 0x00040000
  887. WRITE_OWNER = 0x00080000
  888. SYNCHRONIZE = 0x00100000
  889. STANDARD_RIGHTS_REQUIRED = 0x000F0000
  890. STANDARD_RIGHTS_READ = READ_CONTROL
  891. STANDARD_RIGHTS_WRITE = READ_CONTROL
  892. STANDARD_RIGHTS_EXECUTE = READ_CONTROL
  893. STANDARD_RIGHTS_ALL = 0x001F0000
  894. SPECIFIC_RIGHTS_ALL = 0x0000FFFF
  895. ACCESS_SYSTEM_SECURITY = 0x01000000
  896. MAXIMUM_ALLOWED = 0x02000000
  897. GENERIC_READ = 0x80000000
  898. GENERIC_WRITE = 0x40000000
  899. GENERIC_EXECUTE = 0x20000000
  900. GENERIC_ALL = 0x10000000
  901. )
  902. type ACCESS_MODE uint32
  903. // Constants for type ACCESS_MODE
  904. const (
  905. NOT_USED_ACCESS = 0
  906. GRANT_ACCESS = 1
  907. SET_ACCESS = 2
  908. DENY_ACCESS = 3
  909. REVOKE_ACCESS = 4
  910. SET_AUDIT_SUCCESS = 5
  911. SET_AUDIT_FAILURE = 6
  912. )
  913. // Constants for AceFlags and Inheritance fields
  914. const (
  915. NO_INHERITANCE = 0x0
  916. SUB_OBJECTS_ONLY_INHERIT = 0x1
  917. SUB_CONTAINERS_ONLY_INHERIT = 0x2
  918. SUB_CONTAINERS_AND_OBJECTS_INHERIT = 0x3
  919. INHERIT_NO_PROPAGATE = 0x4
  920. INHERIT_ONLY = 0x8
  921. INHERITED_ACCESS_ENTRY = 0x10
  922. INHERITED_PARENT = 0x10000000
  923. INHERITED_GRANDPARENT = 0x20000000
  924. OBJECT_INHERIT_ACE = 0x1
  925. CONTAINER_INHERIT_ACE = 0x2
  926. NO_PROPAGATE_INHERIT_ACE = 0x4
  927. INHERIT_ONLY_ACE = 0x8
  928. INHERITED_ACE = 0x10
  929. VALID_INHERIT_FLAGS = 0x1F
  930. )
  931. type MULTIPLE_TRUSTEE_OPERATION uint32
  932. // Constants for MULTIPLE_TRUSTEE_OPERATION
  933. const (
  934. NO_MULTIPLE_TRUSTEE = 0
  935. TRUSTEE_IS_IMPERSONATE = 1
  936. )
  937. type TRUSTEE_FORM uint32
  938. // Constants for TRUSTEE_FORM
  939. const (
  940. TRUSTEE_IS_SID = 0
  941. TRUSTEE_IS_NAME = 1
  942. TRUSTEE_BAD_FORM = 2
  943. TRUSTEE_IS_OBJECTS_AND_SID = 3
  944. TRUSTEE_IS_OBJECTS_AND_NAME = 4
  945. )
  946. type TRUSTEE_TYPE uint32
  947. // Constants for TRUSTEE_TYPE
  948. const (
  949. TRUSTEE_IS_UNKNOWN = 0
  950. TRUSTEE_IS_USER = 1
  951. TRUSTEE_IS_GROUP = 2
  952. TRUSTEE_IS_DOMAIN = 3
  953. TRUSTEE_IS_ALIAS = 4
  954. TRUSTEE_IS_WELL_KNOWN_GROUP = 5
  955. TRUSTEE_IS_DELETED = 6
  956. TRUSTEE_IS_INVALID = 7
  957. TRUSTEE_IS_COMPUTER = 8
  958. )
  959. // Constants for ObjectsPresent field
  960. const (
  961. ACE_OBJECT_TYPE_PRESENT = 0x1
  962. ACE_INHERITED_OBJECT_TYPE_PRESENT = 0x2
  963. )
  964. type EXPLICIT_ACCESS struct {
  965. AccessPermissions ACCESS_MASK
  966. AccessMode ACCESS_MODE
  967. Inheritance uint32
  968. Trustee TRUSTEE
  969. }
  970. // This type is the union inside of TRUSTEE and must be created using one of the TrusteeValueFrom* functions.
  971. type TrusteeValue uintptr
  972. func TrusteeValueFromString(str string) TrusteeValue {
  973. return TrusteeValue(unsafe.Pointer(StringToUTF16Ptr(str)))
  974. }
  975. func TrusteeValueFromSID(sid *SID) TrusteeValue {
  976. return TrusteeValue(unsafe.Pointer(sid))
  977. }
  978. func TrusteeValueFromObjectsAndSid(objectsAndSid *OBJECTS_AND_SID) TrusteeValue {
  979. return TrusteeValue(unsafe.Pointer(objectsAndSid))
  980. }
  981. func TrusteeValueFromObjectsAndName(objectsAndName *OBJECTS_AND_NAME) TrusteeValue {
  982. return TrusteeValue(unsafe.Pointer(objectsAndName))
  983. }
  984. type TRUSTEE struct {
  985. MultipleTrustee *TRUSTEE
  986. MultipleTrusteeOperation MULTIPLE_TRUSTEE_OPERATION
  987. TrusteeForm TRUSTEE_FORM
  988. TrusteeType TRUSTEE_TYPE
  989. TrusteeValue TrusteeValue
  990. }
  991. type OBJECTS_AND_SID struct {
  992. ObjectsPresent uint32
  993. ObjectTypeGuid GUID
  994. InheritedObjectTypeGuid GUID
  995. Sid *SID
  996. }
  997. type OBJECTS_AND_NAME struct {
  998. ObjectsPresent uint32
  999. ObjectType SE_OBJECT_TYPE
  1000. ObjectTypeName *uint16
  1001. InheritedObjectTypeName *uint16
  1002. Name *uint16
  1003. }
  1004. //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
  1005. //sys SetSecurityInfo(handle Handle, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION, owner *SID, group *SID, dacl *ACL, sacl *ACL) = advapi32.SetSecurityInfo
  1006. //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
  1007. //sys SetNamedSecurityInfo(objectName string, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION, owner *SID, group *SID, dacl *ACL, sacl *ACL) (ret error) = advapi32.SetNamedSecurityInfoW
  1008. //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
  1009. //sys initializeSecurityDescriptor(absoluteSD *SECURITY_DESCRIPTOR, revision uint32) (err error) = advapi32.InitializeSecurityDescriptor
  1010. //sys getSecurityDescriptorControl(sd *SECURITY_DESCRIPTOR, control *SECURITY_DESCRIPTOR_CONTROL, revision *uint32) (err error) = advapi32.GetSecurityDescriptorControl
  1011. //sys getSecurityDescriptorDacl(sd *SECURITY_DESCRIPTOR, daclPresent *bool, dacl **ACL, daclDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorDacl
  1012. //sys getSecurityDescriptorSacl(sd *SECURITY_DESCRIPTOR, saclPresent *bool, sacl **ACL, saclDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorSacl
  1013. //sys getSecurityDescriptorOwner(sd *SECURITY_DESCRIPTOR, owner **SID, ownerDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorOwner
  1014. //sys getSecurityDescriptorGroup(sd *SECURITY_DESCRIPTOR, group **SID, groupDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorGroup
  1015. //sys getSecurityDescriptorLength(sd *SECURITY_DESCRIPTOR) (len uint32) = advapi32.GetSecurityDescriptorLength
  1016. //sys getSecurityDescriptorRMControl(sd *SECURITY_DESCRIPTOR, rmControl *uint8) (ret error) [failretval!=0] = advapi32.GetSecurityDescriptorRMControl
  1017. //sys isValidSecurityDescriptor(sd *SECURITY_DESCRIPTOR) (isValid bool) = advapi32.IsValidSecurityDescriptor
  1018. //sys setSecurityDescriptorControl(sd *SECURITY_DESCRIPTOR, controlBitsOfInterest SECURITY_DESCRIPTOR_CONTROL, controlBitsToSet SECURITY_DESCRIPTOR_CONTROL) (err error) = advapi32.SetSecurityDescriptorControl
  1019. //sys setSecurityDescriptorDacl(sd *SECURITY_DESCRIPTOR, daclPresent bool, dacl *ACL, daclDefaulted bool) (err error) = advapi32.SetSecurityDescriptorDacl
  1020. //sys setSecurityDescriptorSacl(sd *SECURITY_DESCRIPTOR, saclPresent bool, sacl *ACL, saclDefaulted bool) (err error) = advapi32.SetSecurityDescriptorSacl
  1021. //sys setSecurityDescriptorOwner(sd *SECURITY_DESCRIPTOR, owner *SID, ownerDefaulted bool) (err error) = advapi32.SetSecurityDescriptorOwner
  1022. //sys setSecurityDescriptorGroup(sd *SECURITY_DESCRIPTOR, group *SID, groupDefaulted bool) (err error) = advapi32.SetSecurityDescriptorGroup
  1023. //sys setSecurityDescriptorRMControl(sd *SECURITY_DESCRIPTOR, rmControl *uint8) = advapi32.SetSecurityDescriptorRMControl
  1024. //sys convertStringSecurityDescriptorToSecurityDescriptor(str string, revision uint32, sd **SECURITY_DESCRIPTOR, size *uint32) (err error) = advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW
  1025. //sys convertSecurityDescriptorToStringSecurityDescriptor(sd *SECURITY_DESCRIPTOR, revision uint32, securityInformation SECURITY_INFORMATION, str **uint16, strLen *uint32) (err error) = advapi32.ConvertSecurityDescriptorToStringSecurityDescriptorW
  1026. //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
  1027. //sys makeSelfRelativeSD(absoluteSD *SECURITY_DESCRIPTOR, selfRelativeSD *SECURITY_DESCRIPTOR, selfRelativeSDSize *uint32) (err error) = advapi32.MakeSelfRelativeSD
  1028. //sys setEntriesInAcl(countExplicitEntries uint32, explicitEntries *EXPLICIT_ACCESS, oldACL *ACL, newACL **ACL) (ret error) = advapi32.SetEntriesInAclW
  1029. // Control returns the security descriptor control bits.
  1030. func (sd *SECURITY_DESCRIPTOR) Control() (control SECURITY_DESCRIPTOR_CONTROL, revision uint32, err error) {
  1031. err = getSecurityDescriptorControl(sd, &control, &revision)
  1032. return
  1033. }
  1034. // SetControl sets the security descriptor control bits.
  1035. func (sd *SECURITY_DESCRIPTOR) SetControl(controlBitsOfInterest SECURITY_DESCRIPTOR_CONTROL, controlBitsToSet SECURITY_DESCRIPTOR_CONTROL) error {
  1036. return setSecurityDescriptorControl(sd, controlBitsOfInterest, controlBitsToSet)
  1037. }
  1038. // RMControl returns the security descriptor resource manager control bits.
  1039. func (sd *SECURITY_DESCRIPTOR) RMControl() (control uint8, err error) {
  1040. err = getSecurityDescriptorRMControl(sd, &control)
  1041. return
  1042. }
  1043. // SetRMControl sets the security descriptor resource manager control bits.
  1044. func (sd *SECURITY_DESCRIPTOR) SetRMControl(rmControl uint8) {
  1045. setSecurityDescriptorRMControl(sd, &rmControl)
  1046. }
  1047. // DACL returns the security descriptor DACL and whether it was defaulted. The dacl return value may be nil
  1048. // if a DACL exists but is an "empty DACL", meaning fully permissive. If the DACL does not exist, err returns
  1049. // ERROR_OBJECT_NOT_FOUND.
  1050. func (sd *SECURITY_DESCRIPTOR) DACL() (dacl *ACL, defaulted bool, err error) {
  1051. var present bool
  1052. err = getSecurityDescriptorDacl(sd, &present, &dacl, &defaulted)
  1053. if !present {
  1054. err = ERROR_OBJECT_NOT_FOUND
  1055. }
  1056. return
  1057. }
  1058. // SetDACL sets the absolute security descriptor DACL.
  1059. func (absoluteSD *SECURITY_DESCRIPTOR) SetDACL(dacl *ACL, present, defaulted bool) error {
  1060. return setSecurityDescriptorDacl(absoluteSD, present, dacl, defaulted)
  1061. }
  1062. // SACL returns the security descriptor SACL and whether it was defaulted. The sacl return value may be nil
  1063. // if a SACL exists but is an "empty SACL", meaning fully permissive. If the SACL does not exist, err returns
  1064. // ERROR_OBJECT_NOT_FOUND.
  1065. func (sd *SECURITY_DESCRIPTOR) SACL() (sacl *ACL, defaulted bool, err error) {
  1066. var present bool
  1067. err = getSecurityDescriptorSacl(sd, &present, &sacl, &defaulted)
  1068. if !present {
  1069. err = ERROR_OBJECT_NOT_FOUND
  1070. }
  1071. return
  1072. }
  1073. // SetSACL sets the absolute security descriptor SACL.
  1074. func (absoluteSD *SECURITY_DESCRIPTOR) SetSACL(sacl *ACL, present, defaulted bool) error {
  1075. return setSecurityDescriptorSacl(absoluteSD, present, sacl, defaulted)
  1076. }
  1077. // Owner returns the security descriptor owner and whether it was defaulted.
  1078. func (sd *SECURITY_DESCRIPTOR) Owner() (owner *SID, defaulted bool, err error) {
  1079. err = getSecurityDescriptorOwner(sd, &owner, &defaulted)
  1080. return
  1081. }
  1082. // SetOwner sets the absolute security descriptor owner.
  1083. func (absoluteSD *SECURITY_DESCRIPTOR) SetOwner(owner *SID, defaulted bool) error {
  1084. return setSecurityDescriptorOwner(absoluteSD, owner, defaulted)
  1085. }
  1086. // Group returns the security descriptor group and whether it was defaulted.
  1087. func (sd *SECURITY_DESCRIPTOR) Group() (group *SID, defaulted bool, err error) {
  1088. err = getSecurityDescriptorGroup(sd, &group, &defaulted)
  1089. return
  1090. }
  1091. // SetGroup sets the absolute security descriptor owner.
  1092. func (absoluteSD *SECURITY_DESCRIPTOR) SetGroup(group *SID, defaulted bool) error {
  1093. return setSecurityDescriptorGroup(absoluteSD, group, defaulted)
  1094. }
  1095. // Length returns the length of the security descriptor.
  1096. func (sd *SECURITY_DESCRIPTOR) Length() uint32 {
  1097. return getSecurityDescriptorLength(sd)
  1098. }
  1099. // IsValid returns whether the security descriptor is valid.
  1100. func (sd *SECURITY_DESCRIPTOR) IsValid() bool {
  1101. return isValidSecurityDescriptor(sd)
  1102. }
  1103. // String returns the SDDL form of the security descriptor, with a function signature that can be
  1104. // used with %v formatting directives.
  1105. func (sd *SECURITY_DESCRIPTOR) String() string {
  1106. var sddl *uint16
  1107. err := convertSecurityDescriptorToStringSecurityDescriptor(sd, 1, 0xff, &sddl, nil)
  1108. if err != nil {
  1109. return ""
  1110. }
  1111. defer LocalFree(Handle(unsafe.Pointer(sddl)))
  1112. return UTF16ToString((*[(1 << 30) - 1]uint16)(unsafe.Pointer(sddl))[:])
  1113. }
  1114. // ToAbsolute converts a self-relative security descriptor into an absolute one.
  1115. func (selfRelativeSD *SECURITY_DESCRIPTOR) ToAbsolute() (absoluteSD *SECURITY_DESCRIPTOR, err error) {
  1116. control, _, err := selfRelativeSD.Control()
  1117. if err != nil {
  1118. return
  1119. }
  1120. if control&SE_SELF_RELATIVE == 0 {
  1121. err = ERROR_INVALID_PARAMETER
  1122. return
  1123. }
  1124. var absoluteSDSize, daclSize, saclSize, ownerSize, groupSize uint32
  1125. err = makeAbsoluteSD(selfRelativeSD, nil, &absoluteSDSize,
  1126. nil, &daclSize, nil, &saclSize, nil, &ownerSize, nil, &groupSize)
  1127. switch err {
  1128. case ERROR_INSUFFICIENT_BUFFER:
  1129. case nil:
  1130. // makeAbsoluteSD is expected to fail, but it succeeds.
  1131. return nil, ERROR_INTERNAL_ERROR
  1132. default:
  1133. return nil, err
  1134. }
  1135. if absoluteSDSize > 0 {
  1136. absoluteSD = (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&make([]byte, absoluteSDSize)[0]))
  1137. }
  1138. var (
  1139. dacl *ACL
  1140. sacl *ACL
  1141. owner *SID
  1142. group *SID
  1143. )
  1144. if daclSize > 0 {
  1145. dacl = (*ACL)(unsafe.Pointer(&make([]byte, daclSize)[0]))
  1146. }
  1147. if saclSize > 0 {
  1148. sacl = (*ACL)(unsafe.Pointer(&make([]byte, saclSize)[0]))
  1149. }
  1150. if ownerSize > 0 {
  1151. owner = (*SID)(unsafe.Pointer(&make([]byte, ownerSize)[0]))
  1152. }
  1153. if groupSize > 0 {
  1154. group = (*SID)(unsafe.Pointer(&make([]byte, groupSize)[0]))
  1155. }
  1156. err = makeAbsoluteSD(selfRelativeSD, absoluteSD, &absoluteSDSize,
  1157. dacl, &daclSize, sacl, &saclSize, owner, &ownerSize, group, &groupSize)
  1158. return
  1159. }
  1160. // ToSelfRelative converts an absolute security descriptor into a self-relative one.
  1161. func (absoluteSD *SECURITY_DESCRIPTOR) ToSelfRelative() (selfRelativeSD *SECURITY_DESCRIPTOR, err error) {
  1162. control, _, err := absoluteSD.Control()
  1163. if err != nil {
  1164. return
  1165. }
  1166. if control&SE_SELF_RELATIVE != 0 {
  1167. err = ERROR_INVALID_PARAMETER
  1168. return
  1169. }
  1170. var selfRelativeSDSize uint32
  1171. err = makeSelfRelativeSD(absoluteSD, nil, &selfRelativeSDSize)
  1172. switch err {
  1173. case ERROR_INSUFFICIENT_BUFFER:
  1174. case nil:
  1175. // makeSelfRelativeSD is expected to fail, but it succeeds.
  1176. return nil, ERROR_INTERNAL_ERROR
  1177. default:
  1178. return nil, err
  1179. }
  1180. if selfRelativeSDSize > 0 {
  1181. selfRelativeSD = (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&make([]byte, selfRelativeSDSize)[0]))
  1182. }
  1183. err = makeSelfRelativeSD(absoluteSD, selfRelativeSD, &selfRelativeSDSize)
  1184. return
  1185. }
  1186. func (selfRelativeSD *SECURITY_DESCRIPTOR) copySelfRelativeSecurityDescriptor() *SECURITY_DESCRIPTOR {
  1187. sdBytes := make([]byte, selfRelativeSD.Length())
  1188. copy(sdBytes, (*[(1 << 31) - 1]byte)(unsafe.Pointer(selfRelativeSD))[:len(sdBytes)])
  1189. return (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&sdBytes[0]))
  1190. }
  1191. // SecurityDescriptorFromString converts an SDDL string describing a security descriptor into a
  1192. // self-relative security descriptor object allocated on the Go heap.
  1193. func SecurityDescriptorFromString(sddl string) (sd *SECURITY_DESCRIPTOR, err error) {
  1194. var winHeapSD *SECURITY_DESCRIPTOR
  1195. err = convertStringSecurityDescriptorToSecurityDescriptor(sddl, 1, &winHeapSD, nil)
  1196. if err != nil {
  1197. return
  1198. }
  1199. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1200. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1201. }
  1202. // GetSecurityInfo queries the security information for a given handle and returns the self-relative security
  1203. // descriptor result on the Go heap.
  1204. func GetSecurityInfo(handle Handle, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION) (sd *SECURITY_DESCRIPTOR, err error) {
  1205. var winHeapSD *SECURITY_DESCRIPTOR
  1206. err = getSecurityInfo(handle, objectType, securityInformation, nil, nil, nil, nil, &winHeapSD)
  1207. if err != nil {
  1208. return
  1209. }
  1210. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1211. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1212. }
  1213. // GetNamedSecurityInfo queries the security information for a given named object and returns the self-relative security
  1214. // descriptor result on the Go heap.
  1215. func GetNamedSecurityInfo(objectName string, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION) (sd *SECURITY_DESCRIPTOR, err error) {
  1216. var winHeapSD *SECURITY_DESCRIPTOR
  1217. err = getNamedSecurityInfo(objectName, objectType, securityInformation, nil, nil, nil, nil, &winHeapSD)
  1218. if err != nil {
  1219. return
  1220. }
  1221. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1222. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1223. }
  1224. // BuildSecurityDescriptor makes a new security descriptor using the input trustees, explicit access lists, and
  1225. // prior security descriptor to be merged, any of which can be nil, returning the self-relative security descriptor
  1226. // result on the Go heap.
  1227. func BuildSecurityDescriptor(owner *TRUSTEE, group *TRUSTEE, accessEntries []EXPLICIT_ACCESS, auditEntries []EXPLICIT_ACCESS, mergedSecurityDescriptor *SECURITY_DESCRIPTOR) (sd *SECURITY_DESCRIPTOR, err error) {
  1228. var winHeapSD *SECURITY_DESCRIPTOR
  1229. var winHeapSDSize uint32
  1230. var firstAccessEntry *EXPLICIT_ACCESS
  1231. if len(accessEntries) > 0 {
  1232. firstAccessEntry = &accessEntries[0]
  1233. }
  1234. var firstAuditEntry *EXPLICIT_ACCESS
  1235. if len(auditEntries) > 0 {
  1236. firstAuditEntry = &auditEntries[0]
  1237. }
  1238. err = buildSecurityDescriptor(owner, group, uint32(len(accessEntries)), firstAccessEntry, uint32(len(auditEntries)), firstAuditEntry, mergedSecurityDescriptor, &winHeapSDSize, &winHeapSD)
  1239. if err != nil {
  1240. return
  1241. }
  1242. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1243. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1244. }
  1245. // NewSecurityDescriptor creates and initializes a new absolute security descriptor.
  1246. func NewSecurityDescriptor() (absoluteSD *SECURITY_DESCRIPTOR, err error) {
  1247. absoluteSD = &SECURITY_DESCRIPTOR{}
  1248. err = initializeSecurityDescriptor(absoluteSD, 1)
  1249. return
  1250. }
  1251. // ACLFromEntries returns a new ACL on the Go heap containing a list of explicit entries as well as those of another ACL.
  1252. // Both explicitEntries and mergedACL are optional and can be nil.
  1253. func ACLFromEntries(explicitEntries []EXPLICIT_ACCESS, mergedACL *ACL) (acl *ACL, err error) {
  1254. var firstExplicitEntry *EXPLICIT_ACCESS
  1255. if len(explicitEntries) > 0 {
  1256. firstExplicitEntry = &explicitEntries[0]
  1257. }
  1258. var winHeapACL *ACL
  1259. err = setEntriesInAcl(uint32(len(explicitEntries)), firstExplicitEntry, mergedACL, &winHeapACL)
  1260. if err != nil {
  1261. return
  1262. }
  1263. defer LocalFree(Handle(unsafe.Pointer(winHeapACL)))
  1264. aclBytes := make([]byte, winHeapACL.aclSize)
  1265. copy(aclBytes, (*[(1 << 31) - 1]byte)(unsafe.Pointer(winHeapACL))[:len(aclBytes)])
  1266. return (*ACL)(unsafe.Pointer(&aclBytes[0])), nil
  1267. }