1
2
3
4
5 package windows
6
7 import (
8 "syscall"
9 "unsafe"
10 )
11
12 const (
13 NameUnknown = 0
14 NameFullyQualifiedDN = 1
15 NameSamCompatible = 2
16 NameDisplay = 3
17 NameUniqueId = 6
18 NameCanonical = 7
19 NameUserPrincipal = 8
20 NameCanonicalEx = 9
21 NameServicePrincipal = 10
22 NameDnsDomain = 12
23 )
24
25
26
27
28
29
30
31
32 func TranslateAccountName(username string, from, to uint32, initSize int) (string, error) {
33 u, e := UTF16PtrFromString(username)
34 if e != nil {
35 return "", e
36 }
37 n := uint32(50)
38 for {
39 b := make([]uint16, n)
40 e = TranslateName(u, from, to, &b[0], &n)
41 if e == nil {
42 return UTF16ToString(b[:n]), nil
43 }
44 if e != ERROR_INSUFFICIENT_BUFFER {
45 return "", e
46 }
47 if n <= uint32(len(b)) {
48 return "", e
49 }
50 }
51 }
52
53 const (
54
55 NetSetupUnknownStatus = iota
56 NetSetupUnjoined
57 NetSetupWorkgroupName
58 NetSetupDomainName
59 )
60
61 type UserInfo10 struct {
62 Name *uint16
63 Comment *uint16
64 UsrComment *uint16
65 FullName *uint16
66 }
67
68
69
70
71
72 const (
73
74 SidTypeUser = 1 + iota
75 SidTypeGroup
76 SidTypeDomain
77 SidTypeAlias
78 SidTypeWellKnownGroup
79 SidTypeDeletedAccount
80 SidTypeInvalid
81 SidTypeUnknown
82 SidTypeComputer
83 SidTypeLabel
84 )
85
86 type SidIdentifierAuthority struct {
87 Value [6]byte
88 }
89
90 var (
91 SECURITY_NULL_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 0}}
92 SECURITY_WORLD_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 1}}
93 SECURITY_LOCAL_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 2}}
94 SECURITY_CREATOR_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 3}}
95 SECURITY_NON_UNIQUE_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 4}}
96 SECURITY_NT_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 5}}
97 SECURITY_MANDATORY_LABEL_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 16}}
98 )
99
100 const (
101 SECURITY_NULL_RID = 0
102 SECURITY_WORLD_RID = 0
103 SECURITY_LOCAL_RID = 0
104 SECURITY_CREATOR_OWNER_RID = 0
105 SECURITY_CREATOR_GROUP_RID = 1
106 SECURITY_DIALUP_RID = 1
107 SECURITY_NETWORK_RID = 2
108 SECURITY_BATCH_RID = 3
109 SECURITY_INTERACTIVE_RID = 4
110 SECURITY_LOGON_IDS_RID = 5
111 SECURITY_SERVICE_RID = 6
112 SECURITY_LOCAL_SYSTEM_RID = 18
113 SECURITY_BUILTIN_DOMAIN_RID = 32
114 SECURITY_PRINCIPAL_SELF_RID = 10
115 SECURITY_CREATOR_OWNER_SERVER_RID = 0x2
116 SECURITY_CREATOR_GROUP_SERVER_RID = 0x3
117 SECURITY_LOGON_IDS_RID_COUNT = 0x3
118 SECURITY_ANONYMOUS_LOGON_RID = 0x7
119 SECURITY_PROXY_RID = 0x8
120 SECURITY_ENTERPRISE_CONTROLLERS_RID = 0x9
121 SECURITY_SERVER_LOGON_RID = SECURITY_ENTERPRISE_CONTROLLERS_RID
122 SECURITY_AUTHENTICATED_USER_RID = 0xb
123 SECURITY_RESTRICTED_CODE_RID = 0xc
124 SECURITY_NT_NON_UNIQUE_RID = 0x15
125 )
126
127
128
129 const (
130 DOMAIN_ALIAS_RID_ADMINS = 0x220
131 DOMAIN_ALIAS_RID_USERS = 0x221
132 DOMAIN_ALIAS_RID_GUESTS = 0x222
133 DOMAIN_ALIAS_RID_POWER_USERS = 0x223
134 DOMAIN_ALIAS_RID_ACCOUNT_OPS = 0x224
135 DOMAIN_ALIAS_RID_SYSTEM_OPS = 0x225
136 DOMAIN_ALIAS_RID_PRINT_OPS = 0x226
137 DOMAIN_ALIAS_RID_BACKUP_OPS = 0x227
138 DOMAIN_ALIAS_RID_REPLICATOR = 0x228
139 DOMAIN_ALIAS_RID_RAS_SERVERS = 0x229
140 DOMAIN_ALIAS_RID_PREW2KCOMPACCESS = 0x22a
141 DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS = 0x22b
142 DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS = 0x22c
143 DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS = 0x22d
144 DOMAIN_ALIAS_RID_MONITORING_USERS = 0x22e
145 DOMAIN_ALIAS_RID_LOGGING_USERS = 0x22f
146 DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS = 0x230
147 DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS = 0x231
148 DOMAIN_ALIAS_RID_DCOM_USERS = 0x232
149 DOMAIN_ALIAS_RID_IUSERS = 0x238
150 DOMAIN_ALIAS_RID_CRYPTO_OPERATORS = 0x239
151 DOMAIN_ALIAS_RID_CACHEABLE_PRINCIPALS_GROUP = 0x23b
152 DOMAIN_ALIAS_RID_NON_CACHEABLE_PRINCIPALS_GROUP = 0x23c
153 DOMAIN_ALIAS_RID_EVENT_LOG_READERS_GROUP = 0x23d
154 DOMAIN_ALIAS_RID_CERTSVC_DCOM_ACCESS_GROUP = 0x23e
155 )
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 type SID struct{}
176
177
178
179 func StringToSid(s string) (*SID, error) {
180 var sid *SID
181 p, e := UTF16PtrFromString(s)
182 if e != nil {
183 return nil, e
184 }
185 e = ConvertStringSidToSid(p, &sid)
186 if e != nil {
187 return nil, e
188 }
189 defer LocalFree((Handle)(unsafe.Pointer(sid)))
190 return sid.Copy()
191 }
192
193
194
195
196 func LookupSID(system, account string) (sid *SID, domain string, accType uint32, err error) {
197 if len(account) == 0 {
198 return nil, "", 0, syscall.EINVAL
199 }
200 acc, e := UTF16PtrFromString(account)
201 if e != nil {
202 return nil, "", 0, e
203 }
204 var sys *uint16
205 if len(system) > 0 {
206 sys, e = UTF16PtrFromString(system)
207 if e != nil {
208 return nil, "", 0, e
209 }
210 }
211 n := uint32(50)
212 dn := uint32(50)
213 for {
214 b := make([]byte, n)
215 db := make([]uint16, dn)
216 sid = (*SID)(unsafe.Pointer(&b[0]))
217 e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
218 if e == nil {
219 return sid, UTF16ToString(db), accType, nil
220 }
221 if e != ERROR_INSUFFICIENT_BUFFER {
222 return nil, "", 0, e
223 }
224 if n <= uint32(len(b)) {
225 return nil, "", 0, e
226 }
227 }
228 }
229
230
231 func (sid *SID) String() string {
232 var s *uint16
233 e := ConvertSidToStringSid(sid, &s)
234 if e != nil {
235 return ""
236 }
237 defer LocalFree((Handle)(unsafe.Pointer(s)))
238 return UTF16ToString((*[256]uint16)(unsafe.Pointer(s))[:])
239 }
240
241
242 func (sid *SID) Len() int {
243 return int(GetLengthSid(sid))
244 }
245
246
247 func (sid *SID) Copy() (*SID, error) {
248 b := make([]byte, sid.Len())
249 sid2 := (*SID)(unsafe.Pointer(&b[0]))
250 e := CopySid(uint32(len(b)), sid2, sid)
251 if e != nil {
252 return nil, e
253 }
254 return sid2, nil
255 }
256
257
258 func (sid *SID) IdentifierAuthority() SidIdentifierAuthority {
259 return *getSidIdentifierAuthority(sid)
260 }
261
262
263 func (sid *SID) SubAuthorityCount() uint8 {
264 return *getSidSubAuthorityCount(sid)
265 }
266
267
268
269 func (sid *SID) SubAuthority(idx uint32) uint32 {
270 if idx >= uint32(sid.SubAuthorityCount()) {
271 panic("sub-authority index out of range")
272 }
273 return *getSidSubAuthority(sid, idx)
274 }
275
276
277 func (sid *SID) IsValid() bool {
278 return isValidSid(sid)
279 }
280
281
282 func (sid *SID) Equals(sid2 *SID) bool {
283 return EqualSid(sid, sid2)
284 }
285
286
287 func (sid *SID) IsWellKnown(sidType WELL_KNOWN_SID_TYPE) bool {
288 return isWellKnownSid(sid, sidType)
289 }
290
291
292
293
294 func (sid *SID) LookupAccount(system string) (account, domain string, accType uint32, err error) {
295 var sys *uint16
296 if len(system) > 0 {
297 sys, err = UTF16PtrFromString(system)
298 if err != nil {
299 return "", "", 0, err
300 }
301 }
302 n := uint32(50)
303 dn := uint32(50)
304 for {
305 b := make([]uint16, n)
306 db := make([]uint16, dn)
307 e := LookupAccountSid(sys, sid, &b[0], &n, &db[0], &dn, &accType)
308 if e == nil {
309 return UTF16ToString(b), UTF16ToString(db), accType, nil
310 }
311 if e != ERROR_INSUFFICIENT_BUFFER {
312 return "", "", 0, e
313 }
314 if n <= uint32(len(b)) {
315 return "", "", 0, e
316 }
317 }
318 }
319
320
321 type WELL_KNOWN_SID_TYPE uint32
322
323 const (
324 WinNullSid = 0
325 WinWorldSid = 1
326 WinLocalSid = 2
327 WinCreatorOwnerSid = 3
328 WinCreatorGroupSid = 4
329 WinCreatorOwnerServerSid = 5
330 WinCreatorGroupServerSid = 6
331 WinNtAuthoritySid = 7
332 WinDialupSid = 8
333 WinNetworkSid = 9
334 WinBatchSid = 10
335 WinInteractiveSid = 11
336 WinServiceSid = 12
337 WinAnonymousSid = 13
338 WinProxySid = 14
339 WinEnterpriseControllersSid = 15
340 WinSelfSid = 16
341 WinAuthenticatedUserSid = 17
342 WinRestrictedCodeSid = 18
343 WinTerminalServerSid = 19
344 WinRemoteLogonIdSid = 20
345 WinLogonIdsSid = 21
346 WinLocalSystemSid = 22
347 WinLocalServiceSid = 23
348 WinNetworkServiceSid = 24
349 WinBuiltinDomainSid = 25
350 WinBuiltinAdministratorsSid = 26
351 WinBuiltinUsersSid = 27
352 WinBuiltinGuestsSid = 28
353 WinBuiltinPowerUsersSid = 29
354 WinBuiltinAccountOperatorsSid = 30
355 WinBuiltinSystemOperatorsSid = 31
356 WinBuiltinPrintOperatorsSid = 32
357 WinBuiltinBackupOperatorsSid = 33
358 WinBuiltinReplicatorSid = 34
359 WinBuiltinPreWindows2000CompatibleAccessSid = 35
360 WinBuiltinRemoteDesktopUsersSid = 36
361 WinBuiltinNetworkConfigurationOperatorsSid = 37
362 WinAccountAdministratorSid = 38
363 WinAccountGuestSid = 39
364 WinAccountKrbtgtSid = 40
365 WinAccountDomainAdminsSid = 41
366 WinAccountDomainUsersSid = 42
367 WinAccountDomainGuestsSid = 43
368 WinAccountComputersSid = 44
369 WinAccountControllersSid = 45
370 WinAccountCertAdminsSid = 46
371 WinAccountSchemaAdminsSid = 47
372 WinAccountEnterpriseAdminsSid = 48
373 WinAccountPolicyAdminsSid = 49
374 WinAccountRasAndIasServersSid = 50
375 WinNTLMAuthenticationSid = 51
376 WinDigestAuthenticationSid = 52
377 WinSChannelAuthenticationSid = 53
378 WinThisOrganizationSid = 54
379 WinOtherOrganizationSid = 55
380 WinBuiltinIncomingForestTrustBuildersSid = 56
381 WinBuiltinPerfMonitoringUsersSid = 57
382 WinBuiltinPerfLoggingUsersSid = 58
383 WinBuiltinAuthorizationAccessSid = 59
384 WinBuiltinTerminalServerLicenseServersSid = 60
385 WinBuiltinDCOMUsersSid = 61
386 WinBuiltinIUsersSid = 62
387 WinIUserSid = 63
388 WinBuiltinCryptoOperatorsSid = 64
389 WinUntrustedLabelSid = 65
390 WinLowLabelSid = 66
391 WinMediumLabelSid = 67
392 WinHighLabelSid = 68
393 WinSystemLabelSid = 69
394 WinWriteRestrictedCodeSid = 70
395 WinCreatorOwnerRightsSid = 71
396 WinCacheablePrincipalsGroupSid = 72
397 WinNonCacheablePrincipalsGroupSid = 73
398 WinEnterpriseReadonlyControllersSid = 74
399 WinAccountReadonlyControllersSid = 75
400 WinBuiltinEventLogReadersGroup = 76
401 WinNewEnterpriseReadonlyControllersSid = 77
402 WinBuiltinCertSvcDComAccessGroup = 78
403 WinMediumPlusLabelSid = 79
404 WinLocalLogonSid = 80
405 WinConsoleLogonSid = 81
406 WinThisOrganizationCertificateSid = 82
407 WinApplicationPackageAuthoritySid = 83
408 WinBuiltinAnyPackageSid = 84
409 WinCapabilityInternetClientSid = 85
410 WinCapabilityInternetClientServerSid = 86
411 WinCapabilityPrivateNetworkClientServerSid = 87
412 WinCapabilityPicturesLibrarySid = 88
413 WinCapabilityVideosLibrarySid = 89
414 WinCapabilityMusicLibrarySid = 90
415 WinCapabilityDocumentsLibrarySid = 91
416 WinCapabilitySharedUserCertificatesSid = 92
417 WinCapabilityEnterpriseAuthenticationSid = 93
418 WinCapabilityRemovableStorageSid = 94
419 WinBuiltinRDSRemoteAccessServersSid = 95
420 WinBuiltinRDSEndpointServersSid = 96
421 WinBuiltinRDSManagementServersSid = 97
422 WinUserModeDriversSid = 98
423 WinBuiltinHyperVAdminsSid = 99
424 WinAccountCloneableControllersSid = 100
425 WinBuiltinAccessControlAssistanceOperatorsSid = 101
426 WinBuiltinRemoteManagementUsersSid = 102
427 WinAuthenticationAuthorityAssertedSid = 103
428 WinAuthenticationServiceAssertedSid = 104
429 WinLocalAccountSid = 105
430 WinLocalAccountAndAdministratorSid = 106
431 WinAccountProtectedUsersSid = 107
432 WinCapabilityAppointmentsSid = 108
433 WinCapabilityContactsSid = 109
434 WinAccountDefaultSystemManagedSid = 110
435 WinBuiltinDefaultSystemManagedGroupSid = 111
436 WinBuiltinStorageReplicaAdminsSid = 112
437 WinAccountKeyAdminsSid = 113
438 WinAccountEnterpriseKeyAdminsSid = 114
439 WinAuthenticationKeyTrustSid = 115
440 WinAuthenticationKeyPropertyMFASid = 116
441 WinAuthenticationKeyPropertyAttestationSid = 117
442 WinAuthenticationFreshKeyAuthSid = 118
443 WinBuiltinDeviceOwnersSid = 119
444 )
445
446
447
448 func CreateWellKnownSid(sidType WELL_KNOWN_SID_TYPE) (*SID, error) {
449 return CreateWellKnownDomainSid(sidType, nil)
450 }
451
452
453
454 func CreateWellKnownDomainSid(sidType WELL_KNOWN_SID_TYPE, domainSid *SID) (*SID, error) {
455 n := uint32(50)
456 for {
457 b := make([]byte, n)
458 sid := (*SID)(unsafe.Pointer(&b[0]))
459 err := createWellKnownSid(sidType, domainSid, sid, &n)
460 if err == nil {
461 return sid, nil
462 }
463 if err != ERROR_INSUFFICIENT_BUFFER {
464 return nil, err
465 }
466 if n <= uint32(len(b)) {
467 return nil, err
468 }
469 }
470 }
471
472 const (
473
474 TOKEN_ASSIGN_PRIMARY = 1 << iota
475 TOKEN_DUPLICATE
476 TOKEN_IMPERSONATE
477 TOKEN_QUERY
478 TOKEN_QUERY_SOURCE
479 TOKEN_ADJUST_PRIVILEGES
480 TOKEN_ADJUST_GROUPS
481 TOKEN_ADJUST_DEFAULT
482 TOKEN_ADJUST_SESSIONID
483
484 TOKEN_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
485 TOKEN_ASSIGN_PRIMARY |
486 TOKEN_DUPLICATE |
487 TOKEN_IMPERSONATE |
488 TOKEN_QUERY |
489 TOKEN_QUERY_SOURCE |
490 TOKEN_ADJUST_PRIVILEGES |
491 TOKEN_ADJUST_GROUPS |
492 TOKEN_ADJUST_DEFAULT |
493 TOKEN_ADJUST_SESSIONID
494 TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY
495 TOKEN_WRITE = STANDARD_RIGHTS_WRITE |
496 TOKEN_ADJUST_PRIVILEGES |
497 TOKEN_ADJUST_GROUPS |
498 TOKEN_ADJUST_DEFAULT
499 TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE
500 )
501
502 const (
503
504 TokenUser = 1 + iota
505 TokenGroups
506 TokenPrivileges
507 TokenOwner
508 TokenPrimaryGroup
509 TokenDefaultDacl
510 TokenSource
511 TokenType
512 TokenImpersonationLevel
513 TokenStatistics
514 TokenRestrictedSids
515 TokenSessionId
516 TokenGroupsAndPrivileges
517 TokenSessionReference
518 TokenSandBoxInert
519 TokenAuditPolicy
520 TokenOrigin
521 TokenElevationType
522 TokenLinkedToken
523 TokenElevation
524 TokenHasRestrictions
525 TokenAccessInformation
526 TokenVirtualizationAllowed
527 TokenVirtualizationEnabled
528 TokenIntegrityLevel
529 TokenUIAccess
530 TokenMandatoryPolicy
531 TokenLogonSid
532 MaxTokenInfoClass
533 )
534
535
536 const (
537 SE_GROUP_MANDATORY = 0x00000001
538 SE_GROUP_ENABLED_BY_DEFAULT = 0x00000002
539 SE_GROUP_ENABLED = 0x00000004
540 SE_GROUP_OWNER = 0x00000008
541 SE_GROUP_USE_FOR_DENY_ONLY = 0x00000010
542 SE_GROUP_INTEGRITY = 0x00000020
543 SE_GROUP_INTEGRITY_ENABLED = 0x00000040
544 SE_GROUP_LOGON_ID = 0xC0000000
545 SE_GROUP_RESOURCE = 0x20000000
546 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
547 )
548
549
550 const (
551 SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001
552 SE_PRIVILEGE_ENABLED = 0x00000002
553 SE_PRIVILEGE_REMOVED = 0x00000004
554 SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000
555 SE_PRIVILEGE_VALID_ATTRIBUTES = SE_PRIVILEGE_ENABLED_BY_DEFAULT | SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_REMOVED | SE_PRIVILEGE_USED_FOR_ACCESS
556 )
557
558
559 const (
560 TokenPrimary = 1
561 TokenImpersonation = 2
562 )
563
564
565 const (
566 SecurityAnonymous = 0
567 SecurityIdentification = 1
568 SecurityImpersonation = 2
569 SecurityDelegation = 3
570 )
571
572 type LUID struct {
573 LowPart uint32
574 HighPart int32
575 }
576
577 type LUIDAndAttributes struct {
578 Luid LUID
579 Attributes uint32
580 }
581
582 type SIDAndAttributes struct {
583 Sid *SID
584 Attributes uint32
585 }
586
587 type Tokenuser struct {
588 User SIDAndAttributes
589 }
590
591 type Tokenprimarygroup struct {
592 PrimaryGroup *SID
593 }
594
595 type Tokengroups struct {
596 GroupCount uint32
597 Groups [1]SIDAndAttributes
598 }
599
600
601 func (g *Tokengroups) AllGroups() []SIDAndAttributes {
602 return (*[(1 << 28) - 1]SIDAndAttributes)(unsafe.Pointer(&g.Groups[0]))[:g.GroupCount:g.GroupCount]
603 }
604
605 type Tokenprivileges struct {
606 PrivilegeCount uint32
607 Privileges [1]LUIDAndAttributes
608 }
609
610
611 func (p *Tokenprivileges) AllPrivileges() []LUIDAndAttributes {
612 return (*[(1 << 27) - 1]LUIDAndAttributes)(unsafe.Pointer(&p.Privileges[0]))[:p.PrivilegeCount:p.PrivilegeCount]
613 }
614
615 type Tokenmandatorylabel struct {
616 Label SIDAndAttributes
617 }
618
619 func (tml *Tokenmandatorylabel) Size() uint32 {
620 return uint32(unsafe.Sizeof(Tokenmandatorylabel{})) + GetLengthSid(tml.Label.Sid)
621 }
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649 type Token Handle
650
651
652
653
654
655
656
657 func OpenCurrentProcessToken() (Token, error) {
658 var token Token
659 err := OpenProcessToken(CurrentProcess(), TOKEN_QUERY, &token)
660 return token, err
661 }
662
663
664
665
666 func GetCurrentProcessToken() Token {
667 return Token(^uintptr(4 - 1))
668 }
669
670
671
672
673 func GetCurrentThreadToken() Token {
674 return Token(^uintptr(5 - 1))
675 }
676
677
678
679
680 func GetCurrentThreadEffectiveToken() Token {
681 return Token(^uintptr(6 - 1))
682 }
683
684
685 func (t Token) Close() error {
686 return CloseHandle(Handle(t))
687 }
688
689
690 func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) {
691 n := uint32(initSize)
692 for {
693 b := make([]byte, n)
694 e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
695 if e == nil {
696 return unsafe.Pointer(&b[0]), nil
697 }
698 if e != ERROR_INSUFFICIENT_BUFFER {
699 return nil, e
700 }
701 if n <= uint32(len(b)) {
702 return nil, e
703 }
704 }
705 }
706
707
708 func (t Token) GetTokenUser() (*Tokenuser, error) {
709 i, e := t.getInfo(TokenUser, 50)
710 if e != nil {
711 return nil, e
712 }
713 return (*Tokenuser)(i), nil
714 }
715
716
717 func (t Token) GetTokenGroups() (*Tokengroups, error) {
718 i, e := t.getInfo(TokenGroups, 50)
719 if e != nil {
720 return nil, e
721 }
722 return (*Tokengroups)(i), nil
723 }
724
725
726
727
728 func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
729 i, e := t.getInfo(TokenPrimaryGroup, 50)
730 if e != nil {
731 return nil, e
732 }
733 return (*Tokenprimarygroup)(i), nil
734 }
735
736
737
738 func (t Token) GetUserProfileDirectory() (string, error) {
739 n := uint32(100)
740 for {
741 b := make([]uint16, n)
742 e := GetUserProfileDirectory(t, &b[0], &n)
743 if e == nil {
744 return UTF16ToString(b), nil
745 }
746 if e != ERROR_INSUFFICIENT_BUFFER {
747 return "", e
748 }
749 if n <= uint32(len(b)) {
750 return "", e
751 }
752 }
753 }
754
755
756 func (token Token) IsElevated() bool {
757 var isElevated uint32
758 var outLen uint32
759 err := GetTokenInformation(token, TokenElevation, (*byte)(unsafe.Pointer(&isElevated)), uint32(unsafe.Sizeof(isElevated)), &outLen)
760 if err != nil {
761 return false
762 }
763 return outLen == uint32(unsafe.Sizeof(isElevated)) && isElevated != 0
764 }
765
766
767 func (token Token) GetLinkedToken() (Token, error) {
768 var linkedToken Token
769 var outLen uint32
770 err := GetTokenInformation(token, TokenLinkedToken, (*byte)(unsafe.Pointer(&linkedToken)), uint32(unsafe.Sizeof(linkedToken)), &outLen)
771 if err != nil {
772 return Token(0), err
773 }
774 return linkedToken, nil
775 }
776
777
778
779 func GetSystemDirectory() (string, error) {
780 n := uint32(MAX_PATH)
781 for {
782 b := make([]uint16, n)
783 l, e := getSystemDirectory(&b[0], n)
784 if e != nil {
785 return "", e
786 }
787 if l <= n {
788 return UTF16ToString(b[:l]), nil
789 }
790 n = l
791 }
792 }
793
794
795
796
797
798 func GetWindowsDirectory() (string, error) {
799 n := uint32(MAX_PATH)
800 for {
801 b := make([]uint16, n)
802 l, e := getWindowsDirectory(&b[0], n)
803 if e != nil {
804 return "", e
805 }
806 if l <= n {
807 return UTF16ToString(b[:l]), nil
808 }
809 n = l
810 }
811 }
812
813
814
815 func GetSystemWindowsDirectory() (string, error) {
816 n := uint32(MAX_PATH)
817 for {
818 b := make([]uint16, n)
819 l, e := getSystemWindowsDirectory(&b[0], n)
820 if e != nil {
821 return "", e
822 }
823 if l <= n {
824 return UTF16ToString(b[:l]), nil
825 }
826 n = l
827 }
828 }
829
830
831 func (t Token) IsMember(sid *SID) (bool, error) {
832 var b int32
833 if e := checkTokenMembership(t, sid, &b); e != nil {
834 return false, e
835 }
836 return b != 0, nil
837 }
838
839
840 func (t Token) IsRestricted() (isRestricted bool, err error) {
841 isRestricted, err = isTokenRestricted(t)
842 if !isRestricted && err == syscall.EINVAL {
843
844 err = nil
845 }
846 return
847 }
848
849 const (
850 WTS_CONSOLE_CONNECT = 0x1
851 WTS_CONSOLE_DISCONNECT = 0x2
852 WTS_REMOTE_CONNECT = 0x3
853 WTS_REMOTE_DISCONNECT = 0x4
854 WTS_SESSION_LOGON = 0x5
855 WTS_SESSION_LOGOFF = 0x6
856 WTS_SESSION_LOCK = 0x7
857 WTS_SESSION_UNLOCK = 0x8
858 WTS_SESSION_REMOTE_CONTROL = 0x9
859 WTS_SESSION_CREATE = 0xa
860 WTS_SESSION_TERMINATE = 0xb
861 )
862
863 const (
864 WTSActive = 0
865 WTSConnected = 1
866 WTSConnectQuery = 2
867 WTSShadow = 3
868 WTSDisconnected = 4
869 WTSIdle = 5
870 WTSListen = 6
871 WTSReset = 7
872 WTSDown = 8
873 WTSInit = 9
874 )
875
876 type WTSSESSION_NOTIFICATION struct {
877 Size uint32
878 SessionID uint32
879 }
880
881 type WTS_SESSION_INFO struct {
882 SessionID uint32
883 WindowStationName *uint16
884 State uint32
885 }
886
887
888
889
890
891
892 type ACL struct {
893 aclRevision byte
894 sbz1 byte
895 aclSize uint16
896 aceCount uint16
897 sbz2 uint16
898 }
899
900 type SECURITY_DESCRIPTOR struct {
901 revision byte
902 sbz1 byte
903 control SECURITY_DESCRIPTOR_CONTROL
904 owner *SID
905 group *SID
906 sacl *ACL
907 dacl *ACL
908 }
909
910 type SECURITY_QUALITY_OF_SERVICE struct {
911 Length uint32
912 ImpersonationLevel uint32
913 ContextTrackingMode byte
914 EffectiveOnly byte
915 }
916
917
918 const (
919 SECURITY_STATIC_TRACKING = 0
920 SECURITY_DYNAMIC_TRACKING = 1
921 )
922
923 type SecurityAttributes struct {
924 Length uint32
925 SecurityDescriptor *SECURITY_DESCRIPTOR
926 InheritHandle uint32
927 }
928
929 type SE_OBJECT_TYPE uint32
930
931
932 const (
933 SE_UNKNOWN_OBJECT_TYPE = 0
934 SE_FILE_OBJECT = 1
935 SE_SERVICE = 2
936 SE_PRINTER = 3
937 SE_REGISTRY_KEY = 4
938 SE_LMSHARE = 5
939 SE_KERNEL_OBJECT = 6
940 SE_WINDOW_OBJECT = 7
941 SE_DS_OBJECT = 8
942 SE_DS_OBJECT_ALL = 9
943 SE_PROVIDER_DEFINED_OBJECT = 10
944 SE_WMIGUID_OBJECT = 11
945 SE_REGISTRY_WOW64_32KEY = 12
946 SE_REGISTRY_WOW64_64KEY = 13
947 )
948
949 type SECURITY_INFORMATION uint32
950
951
952 const (
953 OWNER_SECURITY_INFORMATION = 0x00000001
954 GROUP_SECURITY_INFORMATION = 0x00000002
955 DACL_SECURITY_INFORMATION = 0x00000004
956 SACL_SECURITY_INFORMATION = 0x00000008
957 LABEL_SECURITY_INFORMATION = 0x00000010
958 ATTRIBUTE_SECURITY_INFORMATION = 0x00000020
959 SCOPE_SECURITY_INFORMATION = 0x00000040
960 BACKUP_SECURITY_INFORMATION = 0x00010000
961 PROTECTED_DACL_SECURITY_INFORMATION = 0x80000000
962 PROTECTED_SACL_SECURITY_INFORMATION = 0x40000000
963 UNPROTECTED_DACL_SECURITY_INFORMATION = 0x20000000
964 UNPROTECTED_SACL_SECURITY_INFORMATION = 0x10000000
965 )
966
967 type SECURITY_DESCRIPTOR_CONTROL uint16
968
969
970 const (
971 SE_OWNER_DEFAULTED = 0x0001
972 SE_GROUP_DEFAULTED = 0x0002
973 SE_DACL_PRESENT = 0x0004
974 SE_DACL_DEFAULTED = 0x0008
975 SE_SACL_PRESENT = 0x0010
976 SE_SACL_DEFAULTED = 0x0020
977 SE_DACL_AUTO_INHERIT_REQ = 0x0100
978 SE_SACL_AUTO_INHERIT_REQ = 0x0200
979 SE_DACL_AUTO_INHERITED = 0x0400
980 SE_SACL_AUTO_INHERITED = 0x0800
981 SE_DACL_PROTECTED = 0x1000
982 SE_SACL_PROTECTED = 0x2000
983 SE_RM_CONTROL_VALID = 0x4000
984 SE_SELF_RELATIVE = 0x8000
985 )
986
987 type ACCESS_MASK uint32
988
989
990 const (
991 DELETE = 0x00010000
992 READ_CONTROL = 0x00020000
993 WRITE_DAC = 0x00040000
994 WRITE_OWNER = 0x00080000
995 SYNCHRONIZE = 0x00100000
996 STANDARD_RIGHTS_REQUIRED = 0x000F0000
997 STANDARD_RIGHTS_READ = READ_CONTROL
998 STANDARD_RIGHTS_WRITE = READ_CONTROL
999 STANDARD_RIGHTS_EXECUTE = READ_CONTROL
1000 STANDARD_RIGHTS_ALL = 0x001F0000
1001 SPECIFIC_RIGHTS_ALL = 0x0000FFFF
1002 ACCESS_SYSTEM_SECURITY = 0x01000000
1003 MAXIMUM_ALLOWED = 0x02000000
1004 GENERIC_READ = 0x80000000
1005 GENERIC_WRITE = 0x40000000
1006 GENERIC_EXECUTE = 0x20000000
1007 GENERIC_ALL = 0x10000000
1008 )
1009
1010 type ACCESS_MODE uint32
1011
1012
1013 const (
1014 NOT_USED_ACCESS = 0
1015 GRANT_ACCESS = 1
1016 SET_ACCESS = 2
1017 DENY_ACCESS = 3
1018 REVOKE_ACCESS = 4
1019 SET_AUDIT_SUCCESS = 5
1020 SET_AUDIT_FAILURE = 6
1021 )
1022
1023
1024 const (
1025 NO_INHERITANCE = 0x0
1026 SUB_OBJECTS_ONLY_INHERIT = 0x1
1027 SUB_CONTAINERS_ONLY_INHERIT = 0x2
1028 SUB_CONTAINERS_AND_OBJECTS_INHERIT = 0x3
1029 INHERIT_NO_PROPAGATE = 0x4
1030 INHERIT_ONLY = 0x8
1031 INHERITED_ACCESS_ENTRY = 0x10
1032 INHERITED_PARENT = 0x10000000
1033 INHERITED_GRANDPARENT = 0x20000000
1034 OBJECT_INHERIT_ACE = 0x1
1035 CONTAINER_INHERIT_ACE = 0x2
1036 NO_PROPAGATE_INHERIT_ACE = 0x4
1037 INHERIT_ONLY_ACE = 0x8
1038 INHERITED_ACE = 0x10
1039 VALID_INHERIT_FLAGS = 0x1F
1040 )
1041
1042 type MULTIPLE_TRUSTEE_OPERATION uint32
1043
1044
1045 const (
1046 NO_MULTIPLE_TRUSTEE = 0
1047 TRUSTEE_IS_IMPERSONATE = 1
1048 )
1049
1050 type TRUSTEE_FORM uint32
1051
1052
1053 const (
1054 TRUSTEE_IS_SID = 0
1055 TRUSTEE_IS_NAME = 1
1056 TRUSTEE_BAD_FORM = 2
1057 TRUSTEE_IS_OBJECTS_AND_SID = 3
1058 TRUSTEE_IS_OBJECTS_AND_NAME = 4
1059 )
1060
1061 type TRUSTEE_TYPE uint32
1062
1063
1064 const (
1065 TRUSTEE_IS_UNKNOWN = 0
1066 TRUSTEE_IS_USER = 1
1067 TRUSTEE_IS_GROUP = 2
1068 TRUSTEE_IS_DOMAIN = 3
1069 TRUSTEE_IS_ALIAS = 4
1070 TRUSTEE_IS_WELL_KNOWN_GROUP = 5
1071 TRUSTEE_IS_DELETED = 6
1072 TRUSTEE_IS_INVALID = 7
1073 TRUSTEE_IS_COMPUTER = 8
1074 )
1075
1076
1077 const (
1078 ACE_OBJECT_TYPE_PRESENT = 0x1
1079 ACE_INHERITED_OBJECT_TYPE_PRESENT = 0x2
1080 )
1081
1082 type EXPLICIT_ACCESS struct {
1083 AccessPermissions ACCESS_MASK
1084 AccessMode ACCESS_MODE
1085 Inheritance uint32
1086 Trustee TRUSTEE
1087 }
1088
1089
1090 type TrusteeValue uintptr
1091
1092 func TrusteeValueFromString(str string) TrusteeValue {
1093 return TrusteeValue(unsafe.Pointer(StringToUTF16Ptr(str)))
1094 }
1095 func TrusteeValueFromSID(sid *SID) TrusteeValue {
1096 return TrusteeValue(unsafe.Pointer(sid))
1097 }
1098 func TrusteeValueFromObjectsAndSid(objectsAndSid *OBJECTS_AND_SID) TrusteeValue {
1099 return TrusteeValue(unsafe.Pointer(objectsAndSid))
1100 }
1101 func TrusteeValueFromObjectsAndName(objectsAndName *OBJECTS_AND_NAME) TrusteeValue {
1102 return TrusteeValue(unsafe.Pointer(objectsAndName))
1103 }
1104
1105 type TRUSTEE struct {
1106 MultipleTrustee *TRUSTEE
1107 MultipleTrusteeOperation MULTIPLE_TRUSTEE_OPERATION
1108 TrusteeForm TRUSTEE_FORM
1109 TrusteeType TRUSTEE_TYPE
1110 TrusteeValue TrusteeValue
1111 }
1112
1113 type OBJECTS_AND_SID struct {
1114 ObjectsPresent uint32
1115 ObjectTypeGuid GUID
1116 InheritedObjectTypeGuid GUID
1117 Sid *SID
1118 }
1119
1120 type OBJECTS_AND_NAME struct {
1121 ObjectsPresent uint32
1122 ObjectType SE_OBJECT_TYPE
1123 ObjectTypeName *uint16
1124 InheritedObjectTypeName *uint16
1125 Name *uint16
1126 }
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162 func (sd *SECURITY_DESCRIPTOR) Control() (control SECURITY_DESCRIPTOR_CONTROL, revision uint32, err error) {
1163 err = getSecurityDescriptorControl(sd, &control, &revision)
1164 return
1165 }
1166
1167
1168 func (sd *SECURITY_DESCRIPTOR) SetControl(controlBitsOfInterest SECURITY_DESCRIPTOR_CONTROL, controlBitsToSet SECURITY_DESCRIPTOR_CONTROL) error {
1169 return setSecurityDescriptorControl(sd, controlBitsOfInterest, controlBitsToSet)
1170 }
1171
1172
1173 func (sd *SECURITY_DESCRIPTOR) RMControl() (control uint8, err error) {
1174 err = getSecurityDescriptorRMControl(sd, &control)
1175 return
1176 }
1177
1178
1179 func (sd *SECURITY_DESCRIPTOR) SetRMControl(rmControl uint8) {
1180 setSecurityDescriptorRMControl(sd, &rmControl)
1181 }
1182
1183
1184
1185
1186 func (sd *SECURITY_DESCRIPTOR) DACL() (dacl *ACL, defaulted bool, err error) {
1187 var present bool
1188 err = getSecurityDescriptorDacl(sd, &present, &dacl, &defaulted)
1189 if !present {
1190 err = ERROR_OBJECT_NOT_FOUND
1191 }
1192 return
1193 }
1194
1195
1196 func (absoluteSD *SECURITY_DESCRIPTOR) SetDACL(dacl *ACL, present, defaulted bool) error {
1197 return setSecurityDescriptorDacl(absoluteSD, present, dacl, defaulted)
1198 }
1199
1200
1201
1202
1203 func (sd *SECURITY_DESCRIPTOR) SACL() (sacl *ACL, defaulted bool, err error) {
1204 var present bool
1205 err = getSecurityDescriptorSacl(sd, &present, &sacl, &defaulted)
1206 if !present {
1207 err = ERROR_OBJECT_NOT_FOUND
1208 }
1209 return
1210 }
1211
1212
1213 func (absoluteSD *SECURITY_DESCRIPTOR) SetSACL(sacl *ACL, present, defaulted bool) error {
1214 return setSecurityDescriptorSacl(absoluteSD, present, sacl, defaulted)
1215 }
1216
1217
1218 func (sd *SECURITY_DESCRIPTOR) Owner() (owner *SID, defaulted bool, err error) {
1219 err = getSecurityDescriptorOwner(sd, &owner, &defaulted)
1220 return
1221 }
1222
1223
1224 func (absoluteSD *SECURITY_DESCRIPTOR) SetOwner(owner *SID, defaulted bool) error {
1225 return setSecurityDescriptorOwner(absoluteSD, owner, defaulted)
1226 }
1227
1228
1229 func (sd *SECURITY_DESCRIPTOR) Group() (group *SID, defaulted bool, err error) {
1230 err = getSecurityDescriptorGroup(sd, &group, &defaulted)
1231 return
1232 }
1233
1234
1235 func (absoluteSD *SECURITY_DESCRIPTOR) SetGroup(group *SID, defaulted bool) error {
1236 return setSecurityDescriptorGroup(absoluteSD, group, defaulted)
1237 }
1238
1239
1240 func (sd *SECURITY_DESCRIPTOR) Length() uint32 {
1241 return getSecurityDescriptorLength(sd)
1242 }
1243
1244
1245 func (sd *SECURITY_DESCRIPTOR) IsValid() bool {
1246 return isValidSecurityDescriptor(sd)
1247 }
1248
1249
1250
1251 func (sd *SECURITY_DESCRIPTOR) String() string {
1252 var sddl *uint16
1253 err := convertSecurityDescriptorToStringSecurityDescriptor(sd, 1, 0xff, &sddl, nil)
1254 if err != nil {
1255 return ""
1256 }
1257 defer LocalFree(Handle(unsafe.Pointer(sddl)))
1258 return UTF16PtrToString(sddl)
1259 }
1260
1261
1262 func (selfRelativeSD *SECURITY_DESCRIPTOR) ToAbsolute() (absoluteSD *SECURITY_DESCRIPTOR, err error) {
1263 control, _, err := selfRelativeSD.Control()
1264 if err != nil {
1265 return
1266 }
1267 if control&SE_SELF_RELATIVE == 0 {
1268 err = ERROR_INVALID_PARAMETER
1269 return
1270 }
1271 var absoluteSDSize, daclSize, saclSize, ownerSize, groupSize uint32
1272 err = makeAbsoluteSD(selfRelativeSD, nil, &absoluteSDSize,
1273 nil, &daclSize, nil, &saclSize, nil, &ownerSize, nil, &groupSize)
1274 switch err {
1275 case ERROR_INSUFFICIENT_BUFFER:
1276 case nil:
1277
1278 return nil, ERROR_INTERNAL_ERROR
1279 default:
1280 return nil, err
1281 }
1282 if absoluteSDSize > 0 {
1283 absoluteSD = (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&make([]byte, absoluteSDSize)[0]))
1284 }
1285 var (
1286 dacl *ACL
1287 sacl *ACL
1288 owner *SID
1289 group *SID
1290 )
1291 if daclSize > 0 {
1292 dacl = (*ACL)(unsafe.Pointer(&make([]byte, daclSize)[0]))
1293 }
1294 if saclSize > 0 {
1295 sacl = (*ACL)(unsafe.Pointer(&make([]byte, saclSize)[0]))
1296 }
1297 if ownerSize > 0 {
1298 owner = (*SID)(unsafe.Pointer(&make([]byte, ownerSize)[0]))
1299 }
1300 if groupSize > 0 {
1301 group = (*SID)(unsafe.Pointer(&make([]byte, groupSize)[0]))
1302 }
1303 err = makeAbsoluteSD(selfRelativeSD, absoluteSD, &absoluteSDSize,
1304 dacl, &daclSize, sacl, &saclSize, owner, &ownerSize, group, &groupSize)
1305 return
1306 }
1307
1308
1309 func (absoluteSD *SECURITY_DESCRIPTOR) ToSelfRelative() (selfRelativeSD *SECURITY_DESCRIPTOR, err error) {
1310 control, _, err := absoluteSD.Control()
1311 if err != nil {
1312 return
1313 }
1314 if control&SE_SELF_RELATIVE != 0 {
1315 err = ERROR_INVALID_PARAMETER
1316 return
1317 }
1318 var selfRelativeSDSize uint32
1319 err = makeSelfRelativeSD(absoluteSD, nil, &selfRelativeSDSize)
1320 switch err {
1321 case ERROR_INSUFFICIENT_BUFFER:
1322 case nil:
1323
1324 return nil, ERROR_INTERNAL_ERROR
1325 default:
1326 return nil, err
1327 }
1328 if selfRelativeSDSize > 0 {
1329 selfRelativeSD = (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&make([]byte, selfRelativeSDSize)[0]))
1330 }
1331 err = makeSelfRelativeSD(absoluteSD, selfRelativeSD, &selfRelativeSDSize)
1332 return
1333 }
1334
1335 func (selfRelativeSD *SECURITY_DESCRIPTOR) copySelfRelativeSecurityDescriptor() *SECURITY_DESCRIPTOR {
1336 sdLen := int(selfRelativeSD.Length())
1337 const min = int(unsafe.Sizeof(SECURITY_DESCRIPTOR{}))
1338 if sdLen < min {
1339 sdLen = min
1340 }
1341
1342 src := unsafe.Slice((*byte)(unsafe.Pointer(selfRelativeSD)), sdLen)
1343
1344
1345
1346
1347 const psize = int(unsafe.Sizeof(uintptr(0)))
1348 alloc := make([]uintptr, (sdLen+psize-1)/psize)
1349 dst := unsafe.Slice((*byte)(unsafe.Pointer(&alloc[0])), sdLen)
1350 copy(dst, src)
1351 return (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&dst[0]))
1352 }
1353
1354
1355
1356 func SecurityDescriptorFromString(sddl string) (sd *SECURITY_DESCRIPTOR, err error) {
1357 var winHeapSD *SECURITY_DESCRIPTOR
1358 err = convertStringSecurityDescriptorToSecurityDescriptor(sddl, 1, &winHeapSD, nil)
1359 if err != nil {
1360 return
1361 }
1362 defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
1363 return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
1364 }
1365
1366
1367
1368 func GetSecurityInfo(handle Handle, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION) (sd *SECURITY_DESCRIPTOR, err error) {
1369 var winHeapSD *SECURITY_DESCRIPTOR
1370 err = getSecurityInfo(handle, objectType, securityInformation, nil, nil, nil, nil, &winHeapSD)
1371 if err != nil {
1372 return
1373 }
1374 defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
1375 return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
1376 }
1377
1378
1379
1380 func GetNamedSecurityInfo(objectName string, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION) (sd *SECURITY_DESCRIPTOR, err error) {
1381 var winHeapSD *SECURITY_DESCRIPTOR
1382 err = getNamedSecurityInfo(objectName, objectType, securityInformation, nil, nil, nil, nil, &winHeapSD)
1383 if err != nil {
1384 return
1385 }
1386 defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
1387 return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
1388 }
1389
1390
1391
1392
1393 func BuildSecurityDescriptor(owner *TRUSTEE, group *TRUSTEE, accessEntries []EXPLICIT_ACCESS, auditEntries []EXPLICIT_ACCESS, mergedSecurityDescriptor *SECURITY_DESCRIPTOR) (sd *SECURITY_DESCRIPTOR, err error) {
1394 var winHeapSD *SECURITY_DESCRIPTOR
1395 var winHeapSDSize uint32
1396 var firstAccessEntry *EXPLICIT_ACCESS
1397 if len(accessEntries) > 0 {
1398 firstAccessEntry = &accessEntries[0]
1399 }
1400 var firstAuditEntry *EXPLICIT_ACCESS
1401 if len(auditEntries) > 0 {
1402 firstAuditEntry = &auditEntries[0]
1403 }
1404 err = buildSecurityDescriptor(owner, group, uint32(len(accessEntries)), firstAccessEntry, uint32(len(auditEntries)), firstAuditEntry, mergedSecurityDescriptor, &winHeapSDSize, &winHeapSD)
1405 if err != nil {
1406 return
1407 }
1408 defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
1409 return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
1410 }
1411
1412
1413 func NewSecurityDescriptor() (absoluteSD *SECURITY_DESCRIPTOR, err error) {
1414 absoluteSD = &SECURITY_DESCRIPTOR{}
1415 err = initializeSecurityDescriptor(absoluteSD, 1)
1416 return
1417 }
1418
1419
1420
1421 func ACLFromEntries(explicitEntries []EXPLICIT_ACCESS, mergedACL *ACL) (acl *ACL, err error) {
1422 var firstExplicitEntry *EXPLICIT_ACCESS
1423 if len(explicitEntries) > 0 {
1424 firstExplicitEntry = &explicitEntries[0]
1425 }
1426 var winHeapACL *ACL
1427 err = setEntriesInAcl(uint32(len(explicitEntries)), firstExplicitEntry, mergedACL, &winHeapACL)
1428 if err != nil {
1429 return
1430 }
1431 defer LocalFree(Handle(unsafe.Pointer(winHeapACL)))
1432 aclBytes := make([]byte, winHeapACL.aclSize)
1433 copy(aclBytes, (*[(1 << 31) - 1]byte)(unsafe.Pointer(winHeapACL))[:len(aclBytes):len(aclBytes)])
1434 return (*ACL)(unsafe.Pointer(&aclBytes[0])), nil
1435 }
1436
View as plain text