Skip to content

Commit bf74951

Browse files
committed
libcontainer/user: platform dependent calls
This rearranges a bit of the user and group lookup, such that only a basic subset is exposed. Signed-off-by: Vincent Batts <[email protected]>
1 parent aada2af commit bf74951

File tree

4 files changed

+154
-58
lines changed

4 files changed

+154
-58
lines changed

libcontainer/user/lookup.go

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,84 +12,30 @@ var (
1212
ErrNoGroupEntries = errors.New("no matching entries in group file")
1313
)
1414

15-
func lookupUser(filter func(u User) bool) (User, error) {
16-
// Get operating system-specific passwd reader-closer.
17-
passwd, err := GetPasswd()
18-
if err != nil {
19-
return User{}, err
20-
}
21-
defer passwd.Close()
22-
23-
// Get the users.
24-
users, err := ParsePasswdFilter(passwd, filter)
25-
if err != nil {
26-
return User{}, err
27-
}
28-
29-
// No user entries found.
30-
if len(users) == 0 {
31-
return User{}, ErrNoPasswdEntries
32-
}
33-
34-
// Assume the first entry is the "correct" one.
35-
return users[0], nil
36-
}
37-
3815
// LookupUser looks up a user by their username in /etc/passwd. If the user
3916
// cannot be found (or there is no /etc/passwd file on the filesystem), then
4017
// LookupUser returns an error.
4118
func LookupUser(username string) (User, error) {
42-
return lookupUser(func(u User) bool {
43-
return u.Name == username
44-
})
19+
return lookupUser(username)
4520
}
4621

4722
// LookupUid looks up a user by their user id in /etc/passwd. If the user cannot
4823
// be found (or there is no /etc/passwd file on the filesystem), then LookupId
4924
// returns an error.
5025
func LookupUid(uid int) (User, error) {
51-
return lookupUser(func(u User) bool {
52-
return u.Uid == uid
53-
})
54-
}
55-
56-
func lookupGroup(filter func(g Group) bool) (Group, error) {
57-
// Get operating system-specific group reader-closer.
58-
group, err := GetGroup()
59-
if err != nil {
60-
return Group{}, err
61-
}
62-
defer group.Close()
63-
64-
// Get the users.
65-
groups, err := ParseGroupFilter(group, filter)
66-
if err != nil {
67-
return Group{}, err
68-
}
69-
70-
// No user entries found.
71-
if len(groups) == 0 {
72-
return Group{}, ErrNoGroupEntries
73-
}
74-
75-
// Assume the first entry is the "correct" one.
76-
return groups[0], nil
26+
return lookupUid(uid)
7727
}
7828

7929
// LookupGroup looks up a group by its name in /etc/group. If the group cannot
8030
// be found (or there is no /etc/group file on the filesystem), then LookupGroup
8131
// returns an error.
8232
func LookupGroup(groupname string) (Group, error) {
83-
return lookupGroup(func(g Group) bool {
84-
return g.Name == groupname
85-
})
33+
return lookupGroup(groupname)
8634
}
8735

8836
// LookupGid looks up a group by its group id in /etc/group. If the group cannot
8937
// be found (or there is no /etc/group file on the filesystem), then LookupGid
9038
// returns an error.
9139
func LookupGid(gid int) (Group, error) {
92-
return lookupGroup(func(g Group) bool {
93-
return g.Gid == gid
94-
})
40+
return lookupGid(gid)
9541
}

libcontainer/user/lookup_unix.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,76 @@ const (
1515
unixGroupPath = "/etc/group"
1616
)
1717

18+
func lookupUser(username string) (User, error) {
19+
return lookupUserFunc(func(u User) bool {
20+
return u.Name == username
21+
})
22+
}
23+
24+
func lookupUid(uid int) (User, error) {
25+
return lookupUserFunc(func(u User) bool {
26+
return u.Uid == uid
27+
})
28+
}
29+
30+
func lookupUserFunc(filter func(u User) bool) (User, error) {
31+
// Get operating system-specific passwd reader-closer.
32+
passwd, err := GetPasswd()
33+
if err != nil {
34+
return User{}, err
35+
}
36+
defer passwd.Close()
37+
38+
// Get the users.
39+
users, err := ParsePasswdFilter(passwd, filter)
40+
if err != nil {
41+
return User{}, err
42+
}
43+
44+
// No user entries found.
45+
if len(users) == 0 {
46+
return User{}, ErrNoPasswdEntries
47+
}
48+
49+
// Assume the first entry is the "correct" one.
50+
return users[0], nil
51+
}
52+
53+
func lookupGroup(groupname string) (Group, error) {
54+
return lookupGroupFunc(func(g Group) bool {
55+
return g.Name == groupname
56+
})
57+
}
58+
59+
func lookupGid(gid int) (Group, error) {
60+
return lookupGroupFunc(func(g Group) bool {
61+
return g.Gid == gid
62+
})
63+
}
64+
65+
func lookupGroupFunc(filter func(g Group) bool) (Group, error) {
66+
// Get operating system-specific group reader-closer.
67+
group, err := GetGroup()
68+
if err != nil {
69+
return Group{}, err
70+
}
71+
defer group.Close()
72+
73+
// Get the users.
74+
groups, err := ParseGroupFilter(group, filter)
75+
if err != nil {
76+
return Group{}, err
77+
}
78+
79+
// No user entries found.
80+
if len(groups) == 0 {
81+
return Group{}, ErrNoGroupEntries
82+
}
83+
84+
// Assume the first entry is the "correct" one.
85+
return groups[0], nil
86+
}
87+
1888
func GetPasswdPath() (string, error) {
1989
return unixPasswdPath, nil
2090
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// +build windows
2+
3+
package user
4+
5+
import (
6+
"fmt"
7+
"os/user"
8+
)
9+
10+
func lookupUser(username string) (User, error) {
11+
u, err := user.Lookup(username)
12+
if err != nil {
13+
return User{}, err
14+
}
15+
return userFromOS(u)
16+
}
17+
18+
func lookupUid(uid int) (User, error) {
19+
u, err := user.LookupId(fmt.Sprintf("%d", uid))
20+
if err != nil {
21+
return User{}, err
22+
}
23+
return userFromOS(u)
24+
}
25+
26+
func lookupGroup(groupname string) (Group, error) {
27+
g, err := user.LookupGroup(groupname)
28+
if err != nil {
29+
return Group{}, err
30+
}
31+
return groupFromOS(g)
32+
}
33+
34+
func lookupGid(gid int) (Group, error) {
35+
g, err := user.LookupGroupId(fmt.Sprintf("%d", gid))
36+
if err != nil {
37+
return Group{}, err
38+
}
39+
return groupFromOS(g)
40+
}

libcontainer/user/user.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"os/user"
89
"strconv"
910
"strings"
1011
)
@@ -28,13 +29,52 @@ type User struct {
2829
Shell string
2930
}
3031

32+
// userFromOS converts an os/user.(*User) to local User
33+
//
34+
// (This does not include Pass, Shell or Gecos)
35+
func userFromOS(u *user.User) (User, error) {
36+
newUser := User{
37+
Name: u.Username,
38+
Home: u.HomeDir,
39+
}
40+
id, err := strconv.Atoi(u.Uid)
41+
if err != nil {
42+
return newUser, err
43+
}
44+
newUser.Uid = id
45+
46+
id, err = strconv.Atoi(u.Gid)
47+
if err != nil {
48+
return newUser, err
49+
}
50+
newUser.Gid = id
51+
return newUser, nil
52+
}
53+
3154
type Group struct {
3255
Name string
3356
Pass string
3457
Gid int
3558
List []string
3659
}
3760

61+
// groupFromOS converts an os/user.(*Group) to local Group
62+
//
63+
// (This does not include Pass, Shell or Gecos)
64+
func groupFromOS(g *user.Group) (Group, error) {
65+
newGroup := Group{
66+
Name: g.Name,
67+
}
68+
69+
id, err := strconv.Atoi(g.Gid)
70+
if err != nil {
71+
return newGroup, err
72+
}
73+
newGroup.Gid = id
74+
75+
return newGroup, nil
76+
}
77+
3878
func parseLine(line string, v ...interface{}) {
3979
if line == "" {
4080
return

0 commit comments

Comments
 (0)