-
Notifications
You must be signed in to change notification settings - Fork 26
/
osusers.go
97 lines (76 loc) · 1.81 KB
/
osusers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2009 The Ninep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ninep
import (
"os/user"
"strconv"
"sync"
)
var once sync.Once
type osUser struct {
*user.User
uid int
gid int
}
type osUsers struct {
groups map[int]*osGroup
sync.Mutex
}
// Simple Users implementation that defers to os/user and fakes
// looking up groups by gid only.
var OsUsers *osUsers
func (u *osUser) Name() string { return u.Username }
func (u *osUser) Id() int { return u.uid }
func (u *osUser) Groups() []Group { return []Group{OsUsers.Gid2Group(u.gid)} }
func (u *osUser) IsMember(g Group) bool { return u.gid == g.Id() }
type osGroup struct {
gid int
}
func (g *osGroup) Name() string { return "" }
func (g *osGroup) Id() int { return g.gid }
func (g *osGroup) Members() []User { return nil }
func initOsusers() {
OsUsers = new(osUsers)
OsUsers.groups = make(map[int]*osGroup)
}
func newUser(u *user.User) *osUser {
uid, uerr := strconv.Atoi(u.Uid)
gid, gerr := strconv.Atoi(u.Gid)
if uerr != nil || gerr != nil {
/* non-numeric uid/gid => unsupported system */
return nil
}
return &osUser{u, uid, gid}
}
func (up *osUsers) Uid2User(uid int) User {
u, err := user.LookupId(strconv.Itoa(uid))
if err != nil {
return nil
}
return newUser(u)
}
func (up *osUsers) Uname2User(uname string) User {
u, err := user.Lookup(uname)
if err != nil {
return nil
}
return newUser(u)
}
func (up *osUsers) Gid2Group(gid int) Group {
once.Do(initOsusers)
OsUsers.Lock()
group, present := OsUsers.groups[gid]
if present {
OsUsers.Unlock()
return group
}
group = new(osGroup)
group.gid = gid
OsUsers.groups[gid] = group
OsUsers.Unlock()
return group
}
func (up *osUsers) Gname2Group(gname string) Group {
return nil
}