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 // Predefined domain-relative RIDs for local groups.
136 // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa379649(v=vs.85).aspx
138 DOMAIN_ALIAS_RID_ADMINS = 0x220
139 DOMAIN_ALIAS_RID_USERS = 0x221
140 DOMAIN_ALIAS_RID_GUESTS = 0x222
141 DOMAIN_ALIAS_RID_POWER_USERS = 0x223
142 DOMAIN_ALIAS_RID_ACCOUNT_OPS = 0x224
143 DOMAIN_ALIAS_RID_SYSTEM_OPS = 0x225
144 DOMAIN_ALIAS_RID_PRINT_OPS = 0x226
145 DOMAIN_ALIAS_RID_BACKUP_OPS = 0x227
146 DOMAIN_ALIAS_RID_REPLICATOR = 0x228
147 DOMAIN_ALIAS_RID_RAS_SERVERS = 0x229
148 DOMAIN_ALIAS_RID_PREW2KCOMPACCESS = 0x22a
149 DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS = 0x22b
150 DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS = 0x22c
151 DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS = 0x22d
152 DOMAIN_ALIAS_RID_MONITORING_USERS = 0x22e
153 DOMAIN_ALIAS_RID_LOGGING_USERS = 0x22f
154 DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS = 0x230
155 DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS = 0x231
156 DOMAIN_ALIAS_RID_DCOM_USERS = 0x232
157 DOMAIN_ALIAS_RID_IUSERS = 0x238
158 DOMAIN_ALIAS_RID_CRYPTO_OPERATORS = 0x239
159 DOMAIN_ALIAS_RID_CACHEABLE_PRINCIPALS_GROUP = 0x23b
160 DOMAIN_ALIAS_RID_NON_CACHEABLE_PRINCIPALS_GROUP = 0x23c
161 DOMAIN_ALIAS_RID_EVENT_LOG_READERS_GROUP = 0x23d
162 DOMAIN_ALIAS_RID_CERTSVC_DCOM_ACCESS_GROUP = 0x23e
165 //sys LookupAccountSid(systemName *uint16, sid *SID, name *uint16, nameLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountSidW
166 //sys LookupAccountName(systemName *uint16, accountName *uint16, sid *SID, sidLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountNameW
167 //sys ConvertSidToStringSid(sid *SID, stringSid **uint16) (err error) = advapi32.ConvertSidToStringSidW
168 //sys ConvertStringSidToSid(stringSid *uint16, sid **SID) (err error) = advapi32.ConvertStringSidToSidW
169 //sys GetLengthSid(sid *SID) (len uint32) = advapi32.GetLengthSid
170 //sys CopySid(destSidLen uint32, destSid *SID, srcSid *SID) (err error) = advapi32.CopySid
171 //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
172 //sys FreeSid(sid *SID) (err error) [failretval!=0] = advapi32.FreeSid
173 //sys EqualSid(sid1 *SID, sid2 *SID) (isEqual bool) = advapi32.EqualSid
175 // The security identifier (SID) structure is a variable-length
176 // structure used to uniquely identify users or groups.
179 // StringToSid converts a string-format security identifier
180 // sid into a valid, functional sid.
181 func StringToSid(s string) (*SID, error) {
183 p, e := UTF16PtrFromString(s)
187 e = ConvertStringSidToSid(p, &sid)
191 defer LocalFree((Handle)(unsafe.Pointer(sid)))
195 // LookupSID retrieves a security identifier sid for the account
196 // and the name of the domain on which the account was found.
197 // System specify target computer to search.
198 func LookupSID(system, account string) (sid *SID, domain string, accType uint32, err error) {
199 if len(account) == 0 {
200 return nil, "", 0, syscall.EINVAL
202 acc, e := UTF16PtrFromString(account)
208 sys, e = UTF16PtrFromString(system)
217 db := make([]uint16, dn)
218 sid = (*SID)(unsafe.Pointer(&b[0]))
219 e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
221 return sid, UTF16ToString(db), accType, nil
223 if e != ERROR_INSUFFICIENT_BUFFER {
226 if n <= uint32(len(b)) {
232 // String converts sid to a string format
233 // suitable for display, storage, or transmission.
234 func (sid *SID) String() (string, error) {
236 e := ConvertSidToStringSid(sid, &s)
240 defer LocalFree((Handle)(unsafe.Pointer(s)))
241 return UTF16ToString((*[256]uint16)(unsafe.Pointer(s))[:]), nil
244 // Len returns the length, in bytes, of a valid security identifier sid.
245 func (sid *SID) Len() int {
246 return int(GetLengthSid(sid))
249 // Copy creates a duplicate of security identifier sid.
250 func (sid *SID) Copy() (*SID, error) {
251 b := make([]byte, sid.Len())
252 sid2 := (*SID)(unsafe.Pointer(&b[0]))
253 e := CopySid(uint32(len(b)), sid2, sid)
260 // LookupAccount retrieves the name of the account for this sid
261 // and the name of the first domain on which this sid is found.
262 // System specify target computer to search for.
263 func (sid *SID) LookupAccount(system string) (account, domain string, accType uint32, err error) {
266 sys, err = UTF16PtrFromString(system)
268 return "", "", 0, err
274 b := make([]uint16, n)
275 db := make([]uint16, dn)
276 e := LookupAccountSid(sys, sid, &b[0], &n, &db[0], &dn, &accType)
278 return UTF16ToString(b), UTF16ToString(db), accType, nil
280 if e != ERROR_INSUFFICIENT_BUFFER {
283 if n <= uint32(len(b)) {
291 TOKEN_ASSIGN_PRIMARY = 1 << iota
296 TOKEN_ADJUST_PRIVILEGES
299 TOKEN_ADJUST_SESSIONID
301 TOKEN_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
302 TOKEN_ASSIGN_PRIMARY |
307 TOKEN_ADJUST_PRIVILEGES |
308 TOKEN_ADJUST_GROUPS |
309 TOKEN_ADJUST_DEFAULT |
310 TOKEN_ADJUST_SESSIONID
311 TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY
312 TOKEN_WRITE = STANDARD_RIGHTS_WRITE |
313 TOKEN_ADJUST_PRIVILEGES |
314 TOKEN_ADJUST_GROUPS |
316 TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE
329 TokenImpersonationLevel
333 TokenGroupsAndPrivileges
334 TokenSessionReference
342 TokenAccessInformation
343 TokenVirtualizationAllowed
344 TokenVirtualizationEnabled
352 type SIDAndAttributes struct {
357 type Tokenuser struct {
358 User SIDAndAttributes
361 type Tokenprimarygroup struct {
365 type Tokengroups struct {
367 Groups [1]SIDAndAttributes
370 // Authorization Functions
371 //sys checkTokenMembership(tokenHandle Token, sidToCheck *SID, isMember *int32) (err error) = advapi32.CheckTokenMembership
372 //sys OpenProcessToken(h Handle, access uint32, token *Token) (err error) = advapi32.OpenProcessToken
373 //sys GetTokenInformation(t Token, infoClass uint32, info *byte, infoLen uint32, returnedLen *uint32) (err error) = advapi32.GetTokenInformation
374 //sys GetUserProfileDirectory(t Token, dir *uint16, dirLen *uint32) (err error) = userenv.GetUserProfileDirectoryW
375 //sys getSystemDirectory(dir *uint16, dirLen uint32) (len uint32, err error) = kernel32.GetSystemDirectoryW
377 // An access token contains the security information for a logon session.
378 // The system creates an access token when a user logs on, and every
379 // process executed on behalf of the user has a copy of the token.
380 // The token identifies the user, the user's groups, and the user's
381 // privileges. The system uses the token to control access to securable
382 // objects and to control the ability of the user to perform various
383 // system-related operations on the local computer.
386 // OpenCurrentProcessToken opens the access token
387 // associated with current process.
388 func OpenCurrentProcessToken() (Token, error) {
389 p, e := GetCurrentProcess()
394 e = OpenProcessToken(p, TOKEN_QUERY, &t)
401 // Close releases access to access token.
402 func (t Token) Close() error {
403 return CloseHandle(Handle(t))
406 // getInfo retrieves a specified type of information about an access token.
407 func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) {
408 n := uint32(initSize)
411 e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
413 return unsafe.Pointer(&b[0]), nil
415 if e != ERROR_INSUFFICIENT_BUFFER {
418 if n <= uint32(len(b)) {
424 // GetTokenUser retrieves access token t user account information.
425 func (t Token) GetTokenUser() (*Tokenuser, error) {
426 i, e := t.getInfo(TokenUser, 50)
430 return (*Tokenuser)(i), nil
433 // GetTokenGroups retrieves group accounts associated with access token t.
434 func (t Token) GetTokenGroups() (*Tokengroups, error) {
435 i, e := t.getInfo(TokenGroups, 50)
439 return (*Tokengroups)(i), nil
442 // GetTokenPrimaryGroup retrieves access token t primary group information.
443 // A pointer to a SID structure representing a group that will become
444 // the primary group of any objects created by a process using this access token.
445 func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
446 i, e := t.getInfo(TokenPrimaryGroup, 50)
450 return (*Tokenprimarygroup)(i), nil
453 // GetUserProfileDirectory retrieves path to the
454 // root directory of the access token t user's profile.
455 func (t Token) GetUserProfileDirectory() (string, error) {
458 b := make([]uint16, n)
459 e := GetUserProfileDirectory(t, &b[0], &n)
461 return UTF16ToString(b), nil
463 if e != ERROR_INSUFFICIENT_BUFFER {
466 if n <= uint32(len(b)) {
472 // GetSystemDirectory retrieves path to current location of the system
473 // directory, which is typically, though not always, C:\Windows\System32.
474 func GetSystemDirectory() (string, error) {
475 n := uint32(MAX_PATH)
477 b := make([]uint16, n)
478 l, e := getSystemDirectory(&b[0], n)
483 return UTF16ToString(b[:l]), nil
489 // IsMember reports whether the access token t is a member of the provided SID.
490 func (t Token) IsMember(sid *SID) (bool, error) {
492 if e := checkTokenMembership(t, sid, &b); e != nil {