forked from sylabs/singularity
/
identity.go
92 lines (81 loc) · 2.05 KB
/
identity.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
/*
Copyright (c) 2018, Sylabs, Inc. All rights reserved.
This software is licensed under a 3-clause BSD license. Please
consult LICENSE.md file distributed with the sources of this project regarding
your rights to use or distribute this software.
*/
package user
import "strconv"
import osuser "os/user"
// User represents an Unix user account information
type User struct {
Name string
UID uint32
GID uint32
Gecos string
Dir string
Shell string
}
// Group represents an Unix group information
type Group struct {
Name string
GID uint32
}
func convertUser(user *osuser.User) (*User, error) {
uid, err := strconv.ParseUint(user.Uid, 10, 32)
if err != nil {
return nil, err
}
gid, err := strconv.ParseUint(user.Gid, 10, 32)
if err != nil {
return nil, err
}
u := &User{
Name: user.Username,
UID: uint32(uid),
GID: uint32(gid),
Dir: user.HomeDir,
Gecos: user.Name,
Shell: "/bin/sh",
}
return u, nil
}
func convertGroup(group *osuser.Group) (*Group, error) {
gid, err := strconv.ParseUint(group.Gid, 10, 32)
if err != nil {
return nil, err
}
return &Group{Name: group.Name, GID: uint32(gid)}, nil
}
// GetPwUID returns a pointer to User structure associated with user uid
func GetPwUID(uid uint32) (*User, error) {
u, err := osuser.LookupId(strconv.FormatUint(uint64(uid), 10))
if err != nil {
return nil, err
}
return convertUser(u)
}
// GetPwNam returns a pointer to User structure associated with user name
func GetPwNam(name string) (*User, error) {
u, err := osuser.Lookup(name)
if err != nil {
return nil, err
}
return convertUser(u)
}
// GetGrGID returns a pointer to Group structure associated with groud gid
func GetGrGID(gid uint32) (*Group, error) {
g, err := osuser.LookupGroupId(strconv.FormatUint(uint64(gid), 10))
if err != nil {
return nil, err
}
return convertGroup(g)
}
// GetGrNam returns a pointer to Group structure associated with group name
func GetGrNam(name string) (*Group, error) {
g, err := osuser.LookupGroup(name)
if err != nil {
return nil, err
}
return convertGroup(g)
}