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, e := UTF16PtrFromString(username)
44 b := make([]uint16, 50)
46 e = TranslateName(u, from, to, &b[0], &n)
48 if e != ERROR_INSUFFICIENT_BUFFER {
51 // make receive buffers of requested size and try again
53 e = TranslateName(u, from, to, &b[0], &n)
58 return UTF16ToString(b), nil
61 type UserInfo10 struct {
68 //sys NetUserGetInfo(serverName *uint16, userName *uint16, level uint32, buf **byte) (neterr error) = netapi32.NetUserGetInfo
69 //sys NetApiBufferFree(buf *byte) (neterr error) = netapi32.NetApiBufferFree
73 SidTypeUser = 1 + iota
85 //sys LookupAccountSid(systemName *uint16, sid *SID, name *uint16, nameLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountSidW
86 //sys LookupAccountName(systemName *uint16, accountName *uint16, sid *SID, sidLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountNameW
87 //sys ConvertSidToStringSid(sid *SID, stringSid **uint16) (err error) = advapi32.ConvertSidToStringSidW
88 //sys ConvertStringSidToSid(stringSid *uint16, sid **SID) (err error) = advapi32.ConvertStringSidToSidW
89 //sys GetLengthSid(sid *SID) (len uint32) = advapi32.GetLengthSid
90 //sys CopySid(destSidLen uint32, destSid *SID, srcSid *SID) (err error) = advapi32.CopySid
92 // The security identifier (SID) structure is a variable-length
93 // structure used to uniquely identify users or groups.
96 // StringToSid converts a string-format security identifier
97 // sid into a valid, functional sid.
98 func StringToSid(s string) (*SID, error) {
100 p, e := UTF16PtrFromString(s)
104 e = ConvertStringSidToSid(p, &sid)
108 defer LocalFree((Handle)(unsafe.Pointer(sid)))
112 // LookupSID retrieves a security identifier sid for the account
113 // and the name of the domain on which the account was found.
114 // System specify target computer to search.
115 func LookupSID(system, account string) (sid *SID, domain string, accType uint32, err error) {
116 if len(account) == 0 {
117 return nil, "", 0, EINVAL
119 acc, e := UTF16PtrFromString(account)
125 sys, e = UTF16PtrFromString(system)
130 db := make([]uint16, 50)
131 dn := uint32(len(db))
132 b := make([]byte, 50)
134 sid = (*SID)(unsafe.Pointer(&b[0]))
135 e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
137 if e != ERROR_INSUFFICIENT_BUFFER {
140 // make receive buffers of requested size and try again
142 sid = (*SID)(unsafe.Pointer(&b[0]))
143 db = make([]uint16, dn)
144 e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
149 return sid, UTF16ToString(db), accType, nil
152 // String converts sid to a string format
153 // suitable for display, storage, or transmission.
154 func (sid *SID) String() (string, error) {
156 e := ConvertSidToStringSid(sid, &s)
160 defer LocalFree((Handle)(unsafe.Pointer(s)))
161 return UTF16ToString((*[256]uint16)(unsafe.Pointer(s))[:]), nil
164 // Len returns the length, in bytes, of a valid security identifier sid.
165 func (sid *SID) Len() int {
166 return int(GetLengthSid(sid))
169 // Copy creates a duplicate of security identifier sid.
170 func (sid *SID) Copy() (*SID, error) {
171 b := make([]byte, sid.Len())
172 sid2 := (*SID)(unsafe.Pointer(&b[0]))
173 e := CopySid(uint32(len(b)), sid2, sid)
180 // LookupAccount retrieves the name of the account for this sid
181 // and the name of the first domain on which this sid is found.
182 // System specify target computer to search for.
183 func (sid *SID) LookupAccount(system string) (account, domain string, accType uint32, err error) {
186 sys, err = UTF16PtrFromString(system)
188 return "", "", 0, err
191 b := make([]uint16, 50)
193 db := make([]uint16, 50)
194 dn := uint32(len(db))
195 e := LookupAccountSid(sys, sid, &b[0], &n, &db[0], &dn, &accType)
197 if e != ERROR_INSUFFICIENT_BUFFER {
200 // make receive buffers of requested size and try again
201 b = make([]uint16, n)
202 db = make([]uint16, dn)
203 e = LookupAccountSid(nil, sid, &b[0], &n, &db[0], &dn, &accType)
208 return UTF16ToString(b), UTF16ToString(db), accType, nil
213 TOKEN_ASSIGN_PRIMARY = 1 << iota
218 TOKEN_ADJUST_PRIVILEGES
222 TOKEN_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
223 TOKEN_ASSIGN_PRIMARY |
228 TOKEN_ADJUST_PRIVILEGES |
229 TOKEN_ADJUST_GROUPS |
231 TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY
232 TOKEN_WRITE = STANDARD_RIGHTS_WRITE |
233 TOKEN_ADJUST_PRIVILEGES |
234 TOKEN_ADJUST_GROUPS |
236 TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE
249 TokenImpersonationLevel
253 TokenGroupsAndPrivileges
254 TokenSessionReference
262 TokenAccessInformation
263 TokenVirtualizationAllowed
264 TokenVirtualizationEnabled
272 type SIDAndAttributes struct {
277 type Tokenuser struct {
278 User SIDAndAttributes
281 type Tokenprimarygroup struct {
285 //sys OpenProcessToken(h Handle, access uint32, token *Token) (err error) = advapi32.OpenProcessToken
286 //sys GetTokenInformation(t Token, infoClass uint32, info *byte, infoLen uint32, returnedLen *uint32) (err error) = advapi32.GetTokenInformation
287 //sys GetUserProfileDirectory(t Token, dir *uint16, dirLen *uint32) (err error) = userenv.GetUserProfileDirectoryW
289 // An access token contains the security information for a logon session.
290 // The system creates an access token when a user logs on, and every
291 // process executed on behalf of the user has a copy of the token.
292 // The token identifies the user, the user's groups, and the user's
293 // privileges. The system uses the token to control access to securable
294 // objects and to control the ability of the user to perform various
295 // system-related operations on the local computer.
298 // OpenCurrentProcessToken opens the access token
299 // associated with current process.
300 func OpenCurrentProcessToken() (Token, error) {
301 p, e := GetCurrentProcess()
306 e = OpenProcessToken(p, TOKEN_QUERY, &t)
313 // Close releases access to access token.
314 func (t Token) Close() error {
315 return CloseHandle(Handle(t))
318 // getInfo retrieves a specified type of information about an access token.
319 func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) {
320 b := make([]byte, initSize)
322 e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
324 if e != ERROR_INSUFFICIENT_BUFFER {
327 // make receive buffers of requested size and try again
329 e = GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
334 return unsafe.Pointer(&b[0]), nil
337 // GetTokenUser retrieves access token t user account information.
338 func (t Token) GetTokenUser() (*Tokenuser, error) {
339 i, e := t.getInfo(TokenUser, 50)
343 return (*Tokenuser)(i), nil
346 // GetTokenPrimaryGroup retrieves access token t primary group information.
347 // A pointer to a SID structure representing a group that will become
348 // the primary group of any objects created by a process using this access token.
349 func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
350 i, e := t.getInfo(TokenPrimaryGroup, 50)
354 return (*Tokenprimarygroup)(i), nil
357 // GetUserProfileDirectory retrieves path to the
358 // root directory of the access token t user's profile.
359 func (t Token) GetUserProfileDirectory() (string, error) {
360 b := make([]uint16, 100)
362 e := GetUserProfileDirectory(t, &b[0], &n)
364 if e != ERROR_INSUFFICIENT_BUFFER {
367 // make receive buffers of requested size and try again
368 b = make([]uint16, n)
369 e = GetUserProfileDirectory(t, &b[0], &n)
374 return UTF16ToString(b), nil