OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [os/] [user/] [lookup_windows.go] - Blame information for rev 801

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package user
6
 
7
import (
8
        "fmt"
9
        "syscall"
10
        "unsafe"
11
)
12
 
13
func lookupFullName(domain, username, domainAndUser string) (string, error) {
14
        // try domain controller first
15
        name, e := syscall.TranslateAccountName(domainAndUser,
16
                syscall.NameSamCompatible, syscall.NameDisplay, 50)
17
        if e != nil {
18
                // domain lookup failed, perhaps this pc is not part of domain
19
                d := syscall.StringToUTF16Ptr(domain)
20
                u := syscall.StringToUTF16Ptr(username)
21
                var p *byte
22
                e := syscall.NetUserGetInfo(d, u, 10, &p)
23
                if e != nil {
24
                        return "", e
25
                }
26
                defer syscall.NetApiBufferFree(p)
27
                i := (*syscall.UserInfo10)(unsafe.Pointer(p))
28
                if i.FullName == nil {
29
                        return "", nil
30
                }
31
                name = syscall.UTF16ToString((*[1024]uint16)(unsafe.Pointer(i.FullName))[:])
32
        }
33
        return name, nil
34
}
35
 
36
func newUser(usid *syscall.SID, gid, dir string) (*User, error) {
37
        username, domain, t, e := usid.LookupAccount("")
38
        if e != nil {
39
                return nil, e
40
        }
41
        if t != syscall.SidTypeUser {
42
                return nil, fmt.Errorf("user: should be user account type, not %d", t)
43
        }
44
        domainAndUser := domain + `\` + username
45
        uid, e := usid.String()
46
        if e != nil {
47
                return nil, e
48
        }
49
        name, e := lookupFullName(domain, username, domainAndUser)
50
        if e != nil {
51
                return nil, e
52
        }
53
        u := &User{
54
                Uid:      uid,
55
                Gid:      gid,
56
                Username: domainAndUser,
57
                Name:     name,
58
                HomeDir:  dir,
59
        }
60
        return u, nil
61
}
62
 
63
// Current returns the current user.
64
func Current() (*User, error) {
65
        t, e := syscall.OpenCurrentProcessToken()
66
        if e != nil {
67
                return nil, e
68
        }
69
        u, e := t.GetTokenUser()
70
        if e != nil {
71
                return nil, e
72
        }
73
        pg, e := t.GetTokenPrimaryGroup()
74
        if e != nil {
75
                return nil, e
76
        }
77
        gid, e := pg.PrimaryGroup.String()
78
        if e != nil {
79
                return nil, e
80
        }
81
        dir, e := t.GetUserProfileDirectory()
82
        if e != nil {
83
                return nil, e
84
        }
85
        return newUser(u.User.Sid, gid, dir)
86
}
87
 
88
// BUG(brainman): Lookup and LookupId functions do not set
89
// Gid and HomeDir fields in the User struct returned on windows.
90
 
91
func newUserFromSid(usid *syscall.SID) (*User, error) {
92
        // TODO(brainman): do not know where to get gid and dir fields
93
        gid := "unknown"
94
        dir := "Unknown directory"
95
        return newUser(usid, gid, dir)
96
}
97
 
98
// Lookup looks up a user by username.
99
func Lookup(username string) (*User, error) {
100
        sid, _, t, e := syscall.LookupSID("", username)
101
        if e != nil {
102
                return nil, e
103
        }
104
        if t != syscall.SidTypeUser {
105
                return nil, fmt.Errorf("user: should be user account type, not %d", t)
106
        }
107
        return newUserFromSid(sid)
108
}
109
 
110
// LookupId looks up a user by userid.
111
func LookupId(uid string) (*User, error) {
112
        sid, e := syscall.StringToSid(uid)
113
        if e != nil {
114
                return nil, e
115
        }
116
        return newUserFromSid(sid)
117
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.