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.
12 STANDARD_RIGHTS_REQUIRED = 0xf0000
13 STANDARD_RIGHTS_READ = 0x20000
14 STANDARD_RIGHTS_WRITE = 0x20000
15 STANDARD_RIGHTS_EXECUTE = 0x20000
16 STANDARD_RIGHTS_ALL = 0x1F0000
21 NameFullyQualifiedDN = 1
28 NameServicePrincipal = 10
32 // This function returns 1 byte BOOLEAN rather than the 4 byte BOOL.
33 // http://blogs.msdn.com/b/drnick/archive/2007/12/19/windows-and-upn-format-credentials.aspx
34 //sys TranslateName(accName *uint16, accNameFormat uint32, desiredNameFormat uint32, translatedName *uint16, nSize *uint32) (err error) [failretval&0xff==0] = secur32.TranslateNameW
35 //sys GetUserNameEx(nameFormat uint32, nameBuffre *uint16, nSize *uint32) (err error) [failretval&0xff==0] = secur32.GetUserNameExW
37 // TranslateAccountName converts a directory service
38 // object name from one format to another.
39 func TranslateAccountName(username string, from, to uint32, initSize int) (string, error) {
40 u := StringToUTF16Ptr(username)
41 b := make([]uint16, 50)
43 e := TranslateName(u, from, to, &b[0], &n)
45 if e != ERROR_INSUFFICIENT_BUFFER {
48 // make receive buffers of requested size and try again
50 e = TranslateName(u, from, to, &b[0], &n)
55 return UTF16ToString(b), nil
58 type UserInfo10 struct {
65 //sys NetUserGetInfo(serverName *uint16, userName *uint16, level uint32, buf **byte) (neterr error) = netapi32.NetUserGetInfo
66 //sys NetApiBufferFree(buf *byte) (neterr error) = netapi32.NetApiBufferFree
70 SidTypeUser = 1 << iota
82 //sys LookupAccountSid(systemName *uint16, sid *SID, name *uint16, nameLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountSidW
83 //sys LookupAccountName(systemName *uint16, accountName *uint16, sid *SID, sidLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountNameW
84 //sys ConvertSidToStringSid(sid *SID, stringSid **uint16) (err error) = advapi32.ConvertSidToStringSidW
85 //sys ConvertStringSidToSid(stringSid *uint16, sid **SID) (err error) = advapi32.ConvertStringSidToSidW
86 //sys GetLengthSid(sid *SID) (len uint32) = advapi32.GetLengthSid
87 //sys CopySid(destSidLen uint32, destSid *SID, srcSid *SID) (err error) = advapi32.CopySid
89 // The security identifier (SID) structure is a variable-length
90 // structure used to uniquely identify users or groups.
93 // StringToSid converts a string-format security identifier
94 // sid into a valid, functional sid.
95 func StringToSid(s string) (*SID, error) {
97 e := ConvertStringSidToSid(StringToUTF16Ptr(s), &sid)
101 defer LocalFree((Handle)(unsafe.Pointer(sid)))
105 // LookupSID retrieves a security identifier sid for the account
106 // and the name of the domain on which the account was found.
107 // System specify target computer to search.
108 func LookupSID(system, account string) (sid *SID, domain string, accType uint32, err error) {
109 if len(account) == 0 {
110 return nil, "", 0, EINVAL
112 acc := StringToUTF16Ptr(account)
115 sys = StringToUTF16Ptr(system)
117 db := make([]uint16, 50)
118 dn := uint32(len(db))
119 b := make([]byte, 50)
121 sid = (*SID)(unsafe.Pointer(&b[0]))
122 e := LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
124 if e != ERROR_INSUFFICIENT_BUFFER {
127 // make receive buffers of requested size and try again
129 sid = (*SID)(unsafe.Pointer(&b[0]))
130 db = make([]uint16, dn)
131 e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
136 return sid, UTF16ToString(db), accType, nil
139 // String converts sid to a string format
140 // suitable for display, storage, or transmission.
141 func (sid *SID) String() (string, error) {
143 e := ConvertSidToStringSid(sid, &s)
147 defer LocalFree((Handle)(unsafe.Pointer(s)))
148 return UTF16ToString((*[256]uint16)(unsafe.Pointer(s))[:]), nil
151 // Len returns the length, in bytes, of a valid security identifier sid.
152 func (sid *SID) Len() int {
153 return int(GetLengthSid(sid))
156 // Copy creates a duplicate of security identifier sid.
157 func (sid *SID) Copy() (*SID, error) {
158 b := make([]byte, sid.Len())
159 sid2 := (*SID)(unsafe.Pointer(&b[0]))
160 e := CopySid(uint32(len(b)), sid2, sid)
167 // LookupAccount retrieves the name of the account for this sid
168 // and the name of the first domain on which this sid is found.
169 // System specify target computer to search for.
170 func (sid *SID) LookupAccount(system string) (account, domain string, accType uint32, err error) {
173 sys = StringToUTF16Ptr(system)
175 b := make([]uint16, 50)
177 db := make([]uint16, 50)
178 dn := uint32(len(db))
179 e := LookupAccountSid(sys, sid, &b[0], &n, &db[0], &dn, &accType)
181 if e != ERROR_INSUFFICIENT_BUFFER {
184 // make receive buffers of requested size and try again
185 b = make([]uint16, n)
186 db = make([]uint16, dn)
187 e = LookupAccountSid(nil, sid, &b[0], &n, &db[0], &dn, &accType)
192 return UTF16ToString(b), UTF16ToString(db), accType, nil
197 TOKEN_ASSIGN_PRIMARY = 1 << iota
202 TOKEN_ADJUST_PRIVILEGES
206 TOKEN_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
207 TOKEN_ASSIGN_PRIMARY |
212 TOKEN_ADJUST_PRIVILEGES |
213 TOKEN_ADJUST_GROUPS |
215 TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY
216 TOKEN_WRITE = STANDARD_RIGHTS_WRITE |
217 TOKEN_ADJUST_PRIVILEGES |
218 TOKEN_ADJUST_GROUPS |
220 TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE
233 TokenImpersonationLevel
237 TokenGroupsAndPrivileges
238 TokenSessionReference
246 TokenAccessInformation
247 TokenVirtualizationAllowed
248 TokenVirtualizationEnabled
256 type SIDAndAttributes struct {
261 type Tokenuser struct {
262 User SIDAndAttributes
265 type Tokenprimarygroup struct {
269 //sys OpenProcessToken(h Handle, access uint32, token *Token) (err error) = advapi32.OpenProcessToken
270 //sys GetTokenInformation(t Token, infoClass uint32, info *byte, infoLen uint32, returnedLen *uint32) (err error) = advapi32.GetTokenInformation
271 //sys GetUserProfileDirectory(t Token, dir *uint16, dirLen *uint32) (err error) = userenv.GetUserProfileDirectoryW
273 // An access token contains the security information for a logon session.
274 // The system creates an access token when a user logs on, and every
275 // process executed on behalf of the user has a copy of the token.
276 // The token identifies the user, the user's groups, and the user's
277 // privileges. The system uses the token to control access to securable
278 // objects and to control the ability of the user to perform various
279 // system-related operations on the local computer.
282 // OpenCurrentProcessToken opens the access token
283 // associated with current process.
284 func OpenCurrentProcessToken() (Token, error) {
285 p, e := GetCurrentProcess()
290 e = OpenProcessToken(p, TOKEN_QUERY, &t)
297 // Close releases access to access token.
298 func (t Token) Close() error {
299 return CloseHandle(Handle(t))
302 // getInfo retrieves a specified type of information about an access token.
303 func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) {
304 b := make([]byte, initSize)
306 e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
308 if e != ERROR_INSUFFICIENT_BUFFER {
311 // make receive buffers of requested size and try again
313 e = GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
318 return unsafe.Pointer(&b[0]), nil
321 // GetTokenUser retrieves access token t user account information.
322 func (t Token) GetTokenUser() (*Tokenuser, error) {
323 i, e := t.getInfo(TokenUser, 50)
327 return (*Tokenuser)(i), nil
330 // GetTokenPrimaryGroup retrieves access token t primary group information.
331 // A pointer to a SID structure representing a group that will become
332 // the primary group of any objects created by a process using this access token.
333 func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
334 i, e := t.getInfo(TokenPrimaryGroup, 50)
338 return (*Tokenprimarygroup)(i), nil
341 // GetUserProfileDirectory retrieves path to the
342 // root directory of the access token t user's profile.
343 func (t Token) GetUserProfileDirectory() (string, error) {
344 b := make([]uint16, 100)
346 e := GetUserProfileDirectory(t, &b[0], &n)
348 if e != ERROR_INSUFFICIENT_BUFFER {
351 // make receive buffers of requested size and try again
352 b = make([]uint16, n)
353 e = GetUserProfileDirectory(t, &b[0], &n)
358 return UTF16ToString(b), nil