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.
13
STANDARD_RIGHTS_REQUIRED = 0xf0000
14
STANDARD_RIGHTS_READ = 0x20000
15
STANDARD_RIGHTS_WRITE = 0x20000
16
STANDARD_RIGHTS_EXECUTE = 0x20000
17
STANDARD_RIGHTS_ALL = 0x1F0000
22
NameFullyQualifiedDN = 1
29
NameServicePrincipal = 10
33
// This function returns 1 byte BOOLEAN rather than the 4 byte BOOL.
34
// http://blogs.msdn.com/b/drnick/archive/2007/12/19/windows-and-upn-format-credentials.aspx
35
//sys TranslateName(accName *uint16, accNameFormat uint32, desiredNameFormat uint32, translatedName *uint16, nSize *uint32) (err error) [failretval&0xff==0] = secur32.TranslateNameW
36
//sys GetUserNameEx(nameFormat uint32, nameBuffre *uint16, nSize *uint32) (err error) [failretval&0xff==0] = secur32.GetUserNameExW
38
// TranslateAccountName converts a directory service
39
// object name from one format to another.
40
func TranslateAccountName(username string, from, to uint32, initSize int) (string, error) {
41
u, e := UTF16PtrFromString(username)
47
b := make([]uint16, n)
48
e = TranslateName(u, from, to, &b[0], &n)
50
return UTF16ToString(b[:n]), nil
52
if e != ERROR_INSUFFICIENT_BUFFER {
55
if n <= uint32(len(b)) {
63
NetSetupUnknownStatus = iota
69
type UserInfo10 struct {
76
//sys NetUserGetInfo(serverName *uint16, userName *uint16, level uint32, buf **byte) (neterr error) = netapi32.NetUserGetInfo
77
//sys NetGetJoinInformation(server *uint16, name **uint16, bufType *uint32) (neterr error) = netapi32.NetGetJoinInformation
78
//sys NetApiBufferFree(buf *byte) (neterr error) = netapi32.NetApiBufferFree
82
SidTypeUser = 1 + iota
94
type SidIdentifierAuthority struct {
99
SECURITY_NULL_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 0}}
100
SECURITY_WORLD_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 1}}
101
SECURITY_LOCAL_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 2}}
102
SECURITY_CREATOR_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 3}}
103
SECURITY_NON_UNIQUE_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 4}}
104
SECURITY_NT_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 5}}
105
SECURITY_MANDATORY_LABEL_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 16}}
109
SECURITY_NULL_RID = 0
110
SECURITY_WORLD_RID = 0
111
SECURITY_LOCAL_RID = 0
112
SECURITY_CREATOR_OWNER_RID = 0
113
SECURITY_CREATOR_GROUP_RID = 1
114
SECURITY_DIALUP_RID = 1
115
SECURITY_NETWORK_RID = 2
116
SECURITY_BATCH_RID = 3
117
SECURITY_INTERACTIVE_RID = 4
118
SECURITY_LOGON_IDS_RID = 5
119
SECURITY_SERVICE_RID = 6
120
SECURITY_LOCAL_SYSTEM_RID = 18
121
SECURITY_BUILTIN_DOMAIN_RID = 32
122
SECURITY_PRINCIPAL_SELF_RID = 10
123
SECURITY_CREATOR_OWNER_SERVER_RID = 0x2
124
SECURITY_CREATOR_GROUP_SERVER_RID = 0x3
125
SECURITY_LOGON_IDS_RID_COUNT = 0x3
126
SECURITY_ANONYMOUS_LOGON_RID = 0x7
127
SECURITY_PROXY_RID = 0x8
128
SECURITY_ENTERPRISE_CONTROLLERS_RID = 0x9
129
SECURITY_SERVER_LOGON_RID = SECURITY_ENTERPRISE_CONTROLLERS_RID
130
SECURITY_AUTHENTICATED_USER_RID = 0xb
131
SECURITY_RESTRICTED_CODE_RID = 0xc
132
SECURITY_NT_NON_UNIQUE_RID = 0x15
135
//sys LookupAccountSid(systemName *uint16, sid *SID, name *uint16, nameLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountSidW
136
//sys LookupAccountName(systemName *uint16, accountName *uint16, sid *SID, sidLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountNameW
137
//sys ConvertSidToStringSid(sid *SID, stringSid **uint16) (err error) = advapi32.ConvertSidToStringSidW
138
//sys ConvertStringSidToSid(stringSid *uint16, sid **SID) (err error) = advapi32.ConvertStringSidToSidW
139
//sys GetLengthSid(sid *SID) (len uint32) = advapi32.GetLengthSid
140
//sys CopySid(destSidLen uint32, destSid *SID, srcSid *SID) (err error) = advapi32.CopySid
141
//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
142
//sys FreeSid(sid *SID) (err error) [failretval!=0] = advapi32.FreeSid
143
//sys EqualSid(sid1 *SID, sid2 *SID) (isEqual bool) = advapi32.EqualSid
145
// The security identifier (SID) structure is a variable-length
146
// structure used to uniquely identify users or groups.
149
// StringToSid converts a string-format security identifier
150
// sid into a valid, functional sid.
151
func StringToSid(s string) (*SID, error) {
153
p, e := UTF16PtrFromString(s)
157
e = ConvertStringSidToSid(p, &sid)
161
defer LocalFree((Handle)(unsafe.Pointer(sid)))
165
// LookupSID retrieves a security identifier sid for the account
166
// and the name of the domain on which the account was found.
167
// System specify target computer to search.
168
func LookupSID(system, account string) (sid *SID, domain string, accType uint32, err error) {
169
if len(account) == 0 {
170
return nil, "", 0, syscall.EINVAL
172
acc, e := UTF16PtrFromString(account)
178
sys, e = UTF16PtrFromString(system)
187
db := make([]uint16, dn)
188
sid = (*SID)(unsafe.Pointer(&b[0]))
189
e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
191
return sid, UTF16ToString(db), accType, nil
193
if e != ERROR_INSUFFICIENT_BUFFER {
196
if n <= uint32(len(b)) {
202
// String converts sid to a string format
203
// suitable for display, storage, or transmission.
204
func (sid *SID) String() (string, error) {
206
e := ConvertSidToStringSid(sid, &s)
210
defer LocalFree((Handle)(unsafe.Pointer(s)))
211
return UTF16ToString((*[256]uint16)(unsafe.Pointer(s))[:]), nil
214
// Len returns the length, in bytes, of a valid security identifier sid.
215
func (sid *SID) Len() int {
216
return int(GetLengthSid(sid))
219
// Copy creates a duplicate of security identifier sid.
220
func (sid *SID) Copy() (*SID, error) {
221
b := make([]byte, sid.Len())
222
sid2 := (*SID)(unsafe.Pointer(&b[0]))
223
e := CopySid(uint32(len(b)), sid2, sid)
230
// LookupAccount retrieves the name of the account for this sid
231
// and the name of the first domain on which this sid is found.
232
// System specify target computer to search for.
233
func (sid *SID) LookupAccount(system string) (account, domain string, accType uint32, err error) {
236
sys, err = UTF16PtrFromString(system)
238
return "", "", 0, err
244
b := make([]uint16, n)
245
db := make([]uint16, dn)
246
e := LookupAccountSid(sys, sid, &b[0], &n, &db[0], &dn, &accType)
248
return UTF16ToString(b), UTF16ToString(db), accType, nil
250
if e != ERROR_INSUFFICIENT_BUFFER {
253
if n <= uint32(len(b)) {
261
TOKEN_ASSIGN_PRIMARY = 1 << iota
266
TOKEN_ADJUST_PRIVILEGES
270
TOKEN_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
271
TOKEN_ASSIGN_PRIMARY |
276
TOKEN_ADJUST_PRIVILEGES |
277
TOKEN_ADJUST_GROUPS |
279
TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY
280
TOKEN_WRITE = STANDARD_RIGHTS_WRITE |
281
TOKEN_ADJUST_PRIVILEGES |
282
TOKEN_ADJUST_GROUPS |
284
TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE
297
TokenImpersonationLevel
301
TokenGroupsAndPrivileges
302
TokenSessionReference
310
TokenAccessInformation
311
TokenVirtualizationAllowed
312
TokenVirtualizationEnabled
320
type SIDAndAttributes struct {
325
type Tokenuser struct {
326
User SIDAndAttributes
329
type Tokenprimarygroup struct {
333
type Tokengroups struct {
335
Groups [1]SIDAndAttributes
338
//sys OpenProcessToken(h Handle, access uint32, token *Token) (err error) = advapi32.OpenProcessToken
339
//sys GetTokenInformation(t Token, infoClass uint32, info *byte, infoLen uint32, returnedLen *uint32) (err error) = advapi32.GetTokenInformation
340
//sys GetUserProfileDirectory(t Token, dir *uint16, dirLen *uint32) (err error) = userenv.GetUserProfileDirectoryW
342
// An access token contains the security information for a logon session.
343
// The system creates an access token when a user logs on, and every
344
// process executed on behalf of the user has a copy of the token.
345
// The token identifies the user, the user's groups, and the user's
346
// privileges. The system uses the token to control access to securable
347
// objects and to control the ability of the user to perform various
348
// system-related operations on the local computer.
351
// OpenCurrentProcessToken opens the access token
352
// associated with current process.
353
func OpenCurrentProcessToken() (Token, error) {
354
p, e := GetCurrentProcess()
359
e = OpenProcessToken(p, TOKEN_QUERY, &t)
366
// Close releases access to access token.
367
func (t Token) Close() error {
368
return CloseHandle(Handle(t))
371
// getInfo retrieves a specified type of information about an access token.
372
func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) {
373
n := uint32(initSize)
376
e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
378
return unsafe.Pointer(&b[0]), nil
380
if e != ERROR_INSUFFICIENT_BUFFER {
383
if n <= uint32(len(b)) {
389
// GetTokenUser retrieves access token t user account information.
390
func (t Token) GetTokenUser() (*Tokenuser, error) {
391
i, e := t.getInfo(TokenUser, 50)
395
return (*Tokenuser)(i), nil
398
// GetTokenGroups retrieves group accounts associated with access token t.
399
func (t Token) GetTokenGroups() (*Tokengroups, error) {
400
i, e := t.getInfo(TokenGroups, 50)
404
return (*Tokengroups)(i), nil
407
// GetTokenPrimaryGroup retrieves access token t primary group information.
408
// A pointer to a SID structure representing a group that will become
409
// the primary group of any objects created by a process using this access token.
410
func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
411
i, e := t.getInfo(TokenPrimaryGroup, 50)
415
return (*Tokenprimarygroup)(i), nil
418
// GetUserProfileDirectory retrieves path to the
419
// root directory of the access token t user's profile.
420
func (t Token) GetUserProfileDirectory() (string, error) {
423
b := make([]uint16, n)
424
e := GetUserProfileDirectory(t, &b[0], &n)
426
return UTF16ToString(b), nil
428
if e != ERROR_INSUFFICIENT_BUFFER {
431
if n <= uint32(len(b)) {