You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

security_windows.go 51KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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. "golang.org/x/sys/internal/unsafeheader"
  9. )
  10. const (
  11. NameUnknown = 0
  12. NameFullyQualifiedDN = 1
  13. NameSamCompatible = 2
  14. NameDisplay = 3
  15. NameUniqueId = 6
  16. NameCanonical = 7
  17. NameUserPrincipal = 8
  18. NameCanonicalEx = 9
  19. NameServicePrincipal = 10
  20. NameDnsDomain = 12
  21. )
  22. // This function returns 1 byte BOOLEAN rather than the 4 byte BOOL.
  23. // http://blogs.msdn.com/b/drnick/archive/2007/12/19/windows-and-upn-format-credentials.aspx
  24. //sys TranslateName(accName *uint16, accNameFormat uint32, desiredNameFormat uint32, translatedName *uint16, nSize *uint32) (err error) [failretval&0xff==0] = secur32.TranslateNameW
  25. //sys GetUserNameEx(nameFormat uint32, nameBuffre *uint16, nSize *uint32) (err error) [failretval&0xff==0] = secur32.GetUserNameExW
  26. // TranslateAccountName converts a directory service
  27. // object name from one format to another.
  28. func TranslateAccountName(username string, from, to uint32, initSize int) (string, error) {
  29. u, e := UTF16PtrFromString(username)
  30. if e != nil {
  31. return "", e
  32. }
  33. n := uint32(50)
  34. for {
  35. b := make([]uint16, n)
  36. e = TranslateName(u, from, to, &b[0], &n)
  37. if e == nil {
  38. return UTF16ToString(b[:n]), nil
  39. }
  40. if e != ERROR_INSUFFICIENT_BUFFER {
  41. return "", e
  42. }
  43. if n <= uint32(len(b)) {
  44. return "", e
  45. }
  46. }
  47. }
  48. const (
  49. // do not reorder
  50. NetSetupUnknownStatus = iota
  51. NetSetupUnjoined
  52. NetSetupWorkgroupName
  53. NetSetupDomainName
  54. )
  55. type UserInfo10 struct {
  56. Name *uint16
  57. Comment *uint16
  58. UsrComment *uint16
  59. FullName *uint16
  60. }
  61. //sys NetUserGetInfo(serverName *uint16, userName *uint16, level uint32, buf **byte) (neterr error) = netapi32.NetUserGetInfo
  62. //sys NetGetJoinInformation(server *uint16, name **uint16, bufType *uint32) (neterr error) = netapi32.NetGetJoinInformation
  63. //sys NetApiBufferFree(buf *byte) (neterr error) = netapi32.NetApiBufferFree
  64. const (
  65. // do not reorder
  66. SidTypeUser = 1 + iota
  67. SidTypeGroup
  68. SidTypeDomain
  69. SidTypeAlias
  70. SidTypeWellKnownGroup
  71. SidTypeDeletedAccount
  72. SidTypeInvalid
  73. SidTypeUnknown
  74. SidTypeComputer
  75. SidTypeLabel
  76. )
  77. type SidIdentifierAuthority struct {
  78. Value [6]byte
  79. }
  80. var (
  81. SECURITY_NULL_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 0}}
  82. SECURITY_WORLD_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 1}}
  83. SECURITY_LOCAL_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 2}}
  84. SECURITY_CREATOR_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 3}}
  85. SECURITY_NON_UNIQUE_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 4}}
  86. SECURITY_NT_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 5}}
  87. SECURITY_MANDATORY_LABEL_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 16}}
  88. )
  89. const (
  90. SECURITY_NULL_RID = 0
  91. SECURITY_WORLD_RID = 0
  92. SECURITY_LOCAL_RID = 0
  93. SECURITY_CREATOR_OWNER_RID = 0
  94. SECURITY_CREATOR_GROUP_RID = 1
  95. SECURITY_DIALUP_RID = 1
  96. SECURITY_NETWORK_RID = 2
  97. SECURITY_BATCH_RID = 3
  98. SECURITY_INTERACTIVE_RID = 4
  99. SECURITY_LOGON_IDS_RID = 5
  100. SECURITY_SERVICE_RID = 6
  101. SECURITY_LOCAL_SYSTEM_RID = 18
  102. SECURITY_BUILTIN_DOMAIN_RID = 32
  103. SECURITY_PRINCIPAL_SELF_RID = 10
  104. SECURITY_CREATOR_OWNER_SERVER_RID = 0x2
  105. SECURITY_CREATOR_GROUP_SERVER_RID = 0x3
  106. SECURITY_LOGON_IDS_RID_COUNT = 0x3
  107. SECURITY_ANONYMOUS_LOGON_RID = 0x7
  108. SECURITY_PROXY_RID = 0x8
  109. SECURITY_ENTERPRISE_CONTROLLERS_RID = 0x9
  110. SECURITY_SERVER_LOGON_RID = SECURITY_ENTERPRISE_CONTROLLERS_RID
  111. SECURITY_AUTHENTICATED_USER_RID = 0xb
  112. SECURITY_RESTRICTED_CODE_RID = 0xc
  113. SECURITY_NT_NON_UNIQUE_RID = 0x15
  114. )
  115. // Predefined domain-relative RIDs for local groups.
  116. // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa379649(v=vs.85).aspx
  117. const (
  118. DOMAIN_ALIAS_RID_ADMINS = 0x220
  119. DOMAIN_ALIAS_RID_USERS = 0x221
  120. DOMAIN_ALIAS_RID_GUESTS = 0x222
  121. DOMAIN_ALIAS_RID_POWER_USERS = 0x223
  122. DOMAIN_ALIAS_RID_ACCOUNT_OPS = 0x224
  123. DOMAIN_ALIAS_RID_SYSTEM_OPS = 0x225
  124. DOMAIN_ALIAS_RID_PRINT_OPS = 0x226
  125. DOMAIN_ALIAS_RID_BACKUP_OPS = 0x227
  126. DOMAIN_ALIAS_RID_REPLICATOR = 0x228
  127. DOMAIN_ALIAS_RID_RAS_SERVERS = 0x229
  128. DOMAIN_ALIAS_RID_PREW2KCOMPACCESS = 0x22a
  129. DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS = 0x22b
  130. DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS = 0x22c
  131. DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS = 0x22d
  132. DOMAIN_ALIAS_RID_MONITORING_USERS = 0x22e
  133. DOMAIN_ALIAS_RID_LOGGING_USERS = 0x22f
  134. DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS = 0x230
  135. DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS = 0x231
  136. DOMAIN_ALIAS_RID_DCOM_USERS = 0x232
  137. DOMAIN_ALIAS_RID_IUSERS = 0x238
  138. DOMAIN_ALIAS_RID_CRYPTO_OPERATORS = 0x239
  139. DOMAIN_ALIAS_RID_CACHEABLE_PRINCIPALS_GROUP = 0x23b
  140. DOMAIN_ALIAS_RID_NON_CACHEABLE_PRINCIPALS_GROUP = 0x23c
  141. DOMAIN_ALIAS_RID_EVENT_LOG_READERS_GROUP = 0x23d
  142. DOMAIN_ALIAS_RID_CERTSVC_DCOM_ACCESS_GROUP = 0x23e
  143. )
  144. //sys LookupAccountSid(systemName *uint16, sid *SID, name *uint16, nameLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountSidW
  145. //sys LookupAccountName(systemName *uint16, accountName *uint16, sid *SID, sidLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountNameW
  146. //sys ConvertSidToStringSid(sid *SID, stringSid **uint16) (err error) = advapi32.ConvertSidToStringSidW
  147. //sys ConvertStringSidToSid(stringSid *uint16, sid **SID) (err error) = advapi32.ConvertStringSidToSidW
  148. //sys GetLengthSid(sid *SID) (len uint32) = advapi32.GetLengthSid
  149. //sys CopySid(destSidLen uint32, destSid *SID, srcSid *SID) (err error) = advapi32.CopySid
  150. //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
  151. //sys createWellKnownSid(sidType WELL_KNOWN_SID_TYPE, domainSid *SID, sid *SID, sizeSid *uint32) (err error) = advapi32.CreateWellKnownSid
  152. //sys isWellKnownSid(sid *SID, sidType WELL_KNOWN_SID_TYPE) (isWellKnown bool) = advapi32.IsWellKnownSid
  153. //sys FreeSid(sid *SID) (err error) [failretval!=0] = advapi32.FreeSid
  154. //sys EqualSid(sid1 *SID, sid2 *SID) (isEqual bool) = advapi32.EqualSid
  155. //sys getSidIdentifierAuthority(sid *SID) (authority *SidIdentifierAuthority) = advapi32.GetSidIdentifierAuthority
  156. //sys getSidSubAuthorityCount(sid *SID) (count *uint8) = advapi32.GetSidSubAuthorityCount
  157. //sys getSidSubAuthority(sid *SID, index uint32) (subAuthority *uint32) = advapi32.GetSidSubAuthority
  158. //sys isValidSid(sid *SID) (isValid bool) = advapi32.IsValidSid
  159. // The security identifier (SID) structure is a variable-length
  160. // structure used to uniquely identify users or groups.
  161. type SID struct{}
  162. // StringToSid converts a string-format security identifier
  163. // SID into a valid, functional SID.
  164. func StringToSid(s string) (*SID, error) {
  165. var sid *SID
  166. p, e := UTF16PtrFromString(s)
  167. if e != nil {
  168. return nil, e
  169. }
  170. e = ConvertStringSidToSid(p, &sid)
  171. if e != nil {
  172. return nil, e
  173. }
  174. defer LocalFree((Handle)(unsafe.Pointer(sid)))
  175. return sid.Copy()
  176. }
  177. // LookupSID retrieves a security identifier SID for the account
  178. // and the name of the domain on which the account was found.
  179. // System specify target computer to search.
  180. func LookupSID(system, account string) (sid *SID, domain string, accType uint32, err error) {
  181. if len(account) == 0 {
  182. return nil, "", 0, syscall.EINVAL
  183. }
  184. acc, e := UTF16PtrFromString(account)
  185. if e != nil {
  186. return nil, "", 0, e
  187. }
  188. var sys *uint16
  189. if len(system) > 0 {
  190. sys, e = UTF16PtrFromString(system)
  191. if e != nil {
  192. return nil, "", 0, e
  193. }
  194. }
  195. n := uint32(50)
  196. dn := uint32(50)
  197. for {
  198. b := make([]byte, n)
  199. db := make([]uint16, dn)
  200. sid = (*SID)(unsafe.Pointer(&b[0]))
  201. e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
  202. if e == nil {
  203. return sid, UTF16ToString(db), accType, nil
  204. }
  205. if e != ERROR_INSUFFICIENT_BUFFER {
  206. return nil, "", 0, e
  207. }
  208. if n <= uint32(len(b)) {
  209. return nil, "", 0, e
  210. }
  211. }
  212. }
  213. // String converts SID to a string format suitable for display, storage, or transmission.
  214. func (sid *SID) String() string {
  215. var s *uint16
  216. e := ConvertSidToStringSid(sid, &s)
  217. if e != nil {
  218. return ""
  219. }
  220. defer LocalFree((Handle)(unsafe.Pointer(s)))
  221. return UTF16ToString((*[256]uint16)(unsafe.Pointer(s))[:])
  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 an access token associated with current
  600. // process with TOKEN_QUERY access. It is a real token that needs to be closed.
  601. //
  602. // Deprecated: Explicitly call OpenProcessToken(CurrentProcess(), ...)
  603. // with the desired access instead, or use GetCurrentProcessToken for a
  604. // TOKEN_QUERY token.
  605. func OpenCurrentProcessToken() (Token, error) {
  606. var token Token
  607. err := OpenProcessToken(CurrentProcess(), TOKEN_QUERY, &token)
  608. return token, err
  609. }
  610. // GetCurrentProcessToken returns the access token associated with
  611. // the current process. It is a pseudo token that does not need
  612. // to be closed.
  613. func GetCurrentProcessToken() Token {
  614. return Token(^uintptr(4 - 1))
  615. }
  616. // GetCurrentThreadToken return the access token associated with
  617. // the current thread. It is a pseudo token that does not need
  618. // to be closed.
  619. func GetCurrentThreadToken() Token {
  620. return Token(^uintptr(5 - 1))
  621. }
  622. // GetCurrentThreadEffectiveToken returns the effective access token
  623. // associated with the current thread. It is a pseudo token that does
  624. // not need to be closed.
  625. func GetCurrentThreadEffectiveToken() Token {
  626. return Token(^uintptr(6 - 1))
  627. }
  628. // Close releases access to access token.
  629. func (t Token) Close() error {
  630. return CloseHandle(Handle(t))
  631. }
  632. // getInfo retrieves a specified type of information about an access token.
  633. func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) {
  634. n := uint32(initSize)
  635. for {
  636. b := make([]byte, n)
  637. e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
  638. if e == nil {
  639. return unsafe.Pointer(&b[0]), nil
  640. }
  641. if e != ERROR_INSUFFICIENT_BUFFER {
  642. return nil, e
  643. }
  644. if n <= uint32(len(b)) {
  645. return nil, e
  646. }
  647. }
  648. }
  649. // GetTokenUser retrieves access token t user account information.
  650. func (t Token) GetTokenUser() (*Tokenuser, error) {
  651. i, e := t.getInfo(TokenUser, 50)
  652. if e != nil {
  653. return nil, e
  654. }
  655. return (*Tokenuser)(i), nil
  656. }
  657. // GetTokenGroups retrieves group accounts associated with access token t.
  658. func (t Token) GetTokenGroups() (*Tokengroups, error) {
  659. i, e := t.getInfo(TokenGroups, 50)
  660. if e != nil {
  661. return nil, e
  662. }
  663. return (*Tokengroups)(i), nil
  664. }
  665. // GetTokenPrimaryGroup retrieves access token t primary group information.
  666. // A pointer to a SID structure representing a group that will become
  667. // the primary group of any objects created by a process using this access token.
  668. func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
  669. i, e := t.getInfo(TokenPrimaryGroup, 50)
  670. if e != nil {
  671. return nil, e
  672. }
  673. return (*Tokenprimarygroup)(i), nil
  674. }
  675. // GetUserProfileDirectory retrieves path to the
  676. // root directory of the access token t user's profile.
  677. func (t Token) GetUserProfileDirectory() (string, error) {
  678. n := uint32(100)
  679. for {
  680. b := make([]uint16, n)
  681. e := GetUserProfileDirectory(t, &b[0], &n)
  682. if e == nil {
  683. return UTF16ToString(b), nil
  684. }
  685. if e != ERROR_INSUFFICIENT_BUFFER {
  686. return "", e
  687. }
  688. if n <= uint32(len(b)) {
  689. return "", e
  690. }
  691. }
  692. }
  693. // IsElevated returns whether the current token is elevated from a UAC perspective.
  694. func (token Token) IsElevated() bool {
  695. var isElevated uint32
  696. var outLen uint32
  697. err := GetTokenInformation(token, TokenElevation, (*byte)(unsafe.Pointer(&isElevated)), uint32(unsafe.Sizeof(isElevated)), &outLen)
  698. if err != nil {
  699. return false
  700. }
  701. return outLen == uint32(unsafe.Sizeof(isElevated)) && isElevated != 0
  702. }
  703. // GetLinkedToken returns the linked token, which may be an elevated UAC token.
  704. func (token Token) GetLinkedToken() (Token, error) {
  705. var linkedToken Token
  706. var outLen uint32
  707. err := GetTokenInformation(token, TokenLinkedToken, (*byte)(unsafe.Pointer(&linkedToken)), uint32(unsafe.Sizeof(linkedToken)), &outLen)
  708. if err != nil {
  709. return Token(0), err
  710. }
  711. return linkedToken, nil
  712. }
  713. // GetSystemDirectory retrieves the path to current location of the system
  714. // directory, which is typically, though not always, `C:\Windows\System32`.
  715. func GetSystemDirectory() (string, error) {
  716. n := uint32(MAX_PATH)
  717. for {
  718. b := make([]uint16, n)
  719. l, e := getSystemDirectory(&b[0], n)
  720. if e != nil {
  721. return "", e
  722. }
  723. if l <= n {
  724. return UTF16ToString(b[:l]), nil
  725. }
  726. n = l
  727. }
  728. }
  729. // GetWindowsDirectory retrieves the path to current location of the Windows
  730. // directory, which is typically, though not always, `C:\Windows`. This may
  731. // be a private user directory in the case that the application is running
  732. // under a terminal server.
  733. func GetWindowsDirectory() (string, error) {
  734. n := uint32(MAX_PATH)
  735. for {
  736. b := make([]uint16, n)
  737. l, e := getWindowsDirectory(&b[0], n)
  738. if e != nil {
  739. return "", e
  740. }
  741. if l <= n {
  742. return UTF16ToString(b[:l]), nil
  743. }
  744. n = l
  745. }
  746. }
  747. // GetSystemWindowsDirectory retrieves the path to current location of the
  748. // Windows directory, which is typically, though not always, `C:\Windows`.
  749. func GetSystemWindowsDirectory() (string, error) {
  750. n := uint32(MAX_PATH)
  751. for {
  752. b := make([]uint16, n)
  753. l, e := getSystemWindowsDirectory(&b[0], n)
  754. if e != nil {
  755. return "", e
  756. }
  757. if l <= n {
  758. return UTF16ToString(b[:l]), nil
  759. }
  760. n = l
  761. }
  762. }
  763. // IsMember reports whether the access token t is a member of the provided SID.
  764. func (t Token) IsMember(sid *SID) (bool, error) {
  765. var b int32
  766. if e := checkTokenMembership(t, sid, &b); e != nil {
  767. return false, e
  768. }
  769. return b != 0, nil
  770. }
  771. const (
  772. WTS_CONSOLE_CONNECT = 0x1
  773. WTS_CONSOLE_DISCONNECT = 0x2
  774. WTS_REMOTE_CONNECT = 0x3
  775. WTS_REMOTE_DISCONNECT = 0x4
  776. WTS_SESSION_LOGON = 0x5
  777. WTS_SESSION_LOGOFF = 0x6
  778. WTS_SESSION_LOCK = 0x7
  779. WTS_SESSION_UNLOCK = 0x8
  780. WTS_SESSION_REMOTE_CONTROL = 0x9
  781. WTS_SESSION_CREATE = 0xa
  782. WTS_SESSION_TERMINATE = 0xb
  783. )
  784. const (
  785. WTSActive = 0
  786. WTSConnected = 1
  787. WTSConnectQuery = 2
  788. WTSShadow = 3
  789. WTSDisconnected = 4
  790. WTSIdle = 5
  791. WTSListen = 6
  792. WTSReset = 7
  793. WTSDown = 8
  794. WTSInit = 9
  795. )
  796. type WTSSESSION_NOTIFICATION struct {
  797. Size uint32
  798. SessionID uint32
  799. }
  800. type WTS_SESSION_INFO struct {
  801. SessionID uint32
  802. WindowStationName *uint16
  803. State uint32
  804. }
  805. //sys WTSQueryUserToken(session uint32, token *Token) (err error) = wtsapi32.WTSQueryUserToken
  806. //sys WTSEnumerateSessions(handle Handle, reserved uint32, version uint32, sessions **WTS_SESSION_INFO, count *uint32) (err error) = wtsapi32.WTSEnumerateSessionsW
  807. //sys WTSFreeMemory(ptr uintptr) = wtsapi32.WTSFreeMemory
  808. type ACL struct {
  809. aclRevision byte
  810. sbz1 byte
  811. aclSize uint16
  812. aceCount uint16
  813. sbz2 uint16
  814. }
  815. type SECURITY_DESCRIPTOR struct {
  816. revision byte
  817. sbz1 byte
  818. control SECURITY_DESCRIPTOR_CONTROL
  819. owner *SID
  820. group *SID
  821. sacl *ACL
  822. dacl *ACL
  823. }
  824. type SecurityAttributes struct {
  825. Length uint32
  826. SecurityDescriptor *SECURITY_DESCRIPTOR
  827. InheritHandle uint32
  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 UTF16PtrToString(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. sdLen := (int)(selfRelativeSD.Length())
  1188. var src []byte
  1189. h := (*unsafeheader.Slice)(unsafe.Pointer(&src))
  1190. h.Data = unsafe.Pointer(selfRelativeSD)
  1191. h.Len = sdLen
  1192. h.Cap = sdLen
  1193. dst := make([]byte, sdLen)
  1194. copy(dst, src)
  1195. return (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&dst[0]))
  1196. }
  1197. // SecurityDescriptorFromString converts an SDDL string describing a security descriptor into a
  1198. // self-relative security descriptor object allocated on the Go heap.
  1199. func SecurityDescriptorFromString(sddl string) (sd *SECURITY_DESCRIPTOR, err error) {
  1200. var winHeapSD *SECURITY_DESCRIPTOR
  1201. err = convertStringSecurityDescriptorToSecurityDescriptor(sddl, 1, &winHeapSD, nil)
  1202. if err != nil {
  1203. return
  1204. }
  1205. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1206. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1207. }
  1208. // GetSecurityInfo queries the security information for a given handle and returns the self-relative security
  1209. // descriptor result on the Go heap.
  1210. func GetSecurityInfo(handle Handle, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION) (sd *SECURITY_DESCRIPTOR, err error) {
  1211. var winHeapSD *SECURITY_DESCRIPTOR
  1212. err = getSecurityInfo(handle, objectType, securityInformation, nil, nil, nil, nil, &winHeapSD)
  1213. if err != nil {
  1214. return
  1215. }
  1216. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1217. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1218. }
  1219. // GetNamedSecurityInfo queries the security information for a given named object and returns the self-relative security
  1220. // descriptor result on the Go heap.
  1221. func GetNamedSecurityInfo(objectName string, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION) (sd *SECURITY_DESCRIPTOR, err error) {
  1222. var winHeapSD *SECURITY_DESCRIPTOR
  1223. err = getNamedSecurityInfo(objectName, objectType, securityInformation, nil, nil, nil, nil, &winHeapSD)
  1224. if err != nil {
  1225. return
  1226. }
  1227. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1228. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1229. }
  1230. // BuildSecurityDescriptor makes a new security descriptor using the input trustees, explicit access lists, and
  1231. // prior security descriptor to be merged, any of which can be nil, returning the self-relative security descriptor
  1232. // result on the Go heap.
  1233. func BuildSecurityDescriptor(owner *TRUSTEE, group *TRUSTEE, accessEntries []EXPLICIT_ACCESS, auditEntries []EXPLICIT_ACCESS, mergedSecurityDescriptor *SECURITY_DESCRIPTOR) (sd *SECURITY_DESCRIPTOR, err error) {
  1234. var winHeapSD *SECURITY_DESCRIPTOR
  1235. var winHeapSDSize uint32
  1236. var firstAccessEntry *EXPLICIT_ACCESS
  1237. if len(accessEntries) > 0 {
  1238. firstAccessEntry = &accessEntries[0]
  1239. }
  1240. var firstAuditEntry *EXPLICIT_ACCESS
  1241. if len(auditEntries) > 0 {
  1242. firstAuditEntry = &auditEntries[0]
  1243. }
  1244. err = buildSecurityDescriptor(owner, group, uint32(len(accessEntries)), firstAccessEntry, uint32(len(auditEntries)), firstAuditEntry, mergedSecurityDescriptor, &winHeapSDSize, &winHeapSD)
  1245. if err != nil {
  1246. return
  1247. }
  1248. defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
  1249. return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
  1250. }
  1251. // NewSecurityDescriptor creates and initializes a new absolute security descriptor.
  1252. func NewSecurityDescriptor() (absoluteSD *SECURITY_DESCRIPTOR, err error) {
  1253. absoluteSD = &SECURITY_DESCRIPTOR{}
  1254. err = initializeSecurityDescriptor(absoluteSD, 1)
  1255. return
  1256. }
  1257. // ACLFromEntries returns a new ACL on the Go heap containing a list of explicit entries as well as those of another ACL.
  1258. // Both explicitEntries and mergedACL are optional and can be nil.
  1259. func ACLFromEntries(explicitEntries []EXPLICIT_ACCESS, mergedACL *ACL) (acl *ACL, err error) {
  1260. var firstExplicitEntry *EXPLICIT_ACCESS
  1261. if len(explicitEntries) > 0 {
  1262. firstExplicitEntry = &explicitEntries[0]
  1263. }
  1264. var winHeapACL *ACL
  1265. err = setEntriesInAcl(uint32(len(explicitEntries)), firstExplicitEntry, mergedACL, &winHeapACL)
  1266. if err != nil {
  1267. return
  1268. }
  1269. defer LocalFree(Handle(unsafe.Pointer(winHeapACL)))
  1270. aclBytes := make([]byte, winHeapACL.aclSize)
  1271. copy(aclBytes, (*[(1 << 31) - 1]byte)(unsafe.Pointer(winHeapACL))[:len(aclBytes):len(aclBytes)])
  1272. return (*ACL)(unsafe.Pointer(&aclBytes[0])), nil
  1273. }