Debian Codegraph @

user/lookup_unix.go

raw symbols SCIP
1 //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
2
3 package user
4
5 import (
6 "io"
7 "os"
8 "strconv"
9
10 "golang.org/x/sys/unix"
11 )
12
13 // Unix-specific path to the passwd and group formatted files.
14 const (
15 unixPasswdPath = "/etc/passwd"
16 unixGroupPath = "/etc/group"
17 )
18
19 // LookupUser looks up a user by their username in /etc/passwd. If the user
20 // cannot be found (or there is no /etc/passwd file on the filesystem), then
21 // LookupUser returns an error.
22 func LookupUser(username string) (User, error) {
23 return lookupUserFunc(func(u User) bool {
24 return u.Name == username
25 })
26 }
27
28 // LookupUid looks up a user by their user id in /etc/passwd. If the user cannot
29 // be found (or there is no /etc/passwd file on the filesystem), then LookupId
30 // returns an error.
31 func LookupUid(uid int) (User, error) {
32 return lookupUserFunc(func(u User) bool {
33 return u.Uid == uid
34 })
35 }
36
37 func lookupUserFunc(filter func(u User) bool) (User, error) {
38 // Get operating system-specific passwd reader-closer.
39 passwd, err := GetPasswd()
40 if err != nil {
41 return User{}, err
42 }
43 defer passwd.Close()
44
45 // Get the users.
46 users, err := ParsePasswdFilter(passwd, filter)
47 if err != nil {
48 return User{}, err
49 }
50
51 // No user entries found.
52 if len(users) == 0 {
53 return User{}, ErrNoPasswdEntries
54 }
55
56 // Assume the first entry is the "correct" one.
57 return users[0], nil
58 }
59
60 // LookupGroup looks up a group by its name in /etc/group. If the group cannot
61 // be found (or there is no /etc/group file on the filesystem), then LookupGroup
62 // returns an error.
63 func LookupGroup(groupname string) (Group, error) {
64 return lookupGroupFunc(func(g Group) bool {
65 return g.Name == groupname
66 })
67 }
68
69 // LookupGid looks up a group by its group id in /etc/group. If the group cannot
70 // be found (or there is no /etc/group file on the filesystem), then LookupGid
71 // returns an error.
72 func LookupGid(gid int) (Group, error) {
73 return lookupGroupFunc(func(g Group) bool {
74 return g.Gid == gid
75 })
76 }
77
78 func lookupGroupFunc(filter func(g Group) bool) (Group, error) {
79 // Get operating system-specific group reader-closer.
80 group, err := GetGroup()
81 if err != nil {
82 return Group{}, err
83 }
84 defer group.Close()
85
86 // Get the users.
87 groups, err := ParseGroupFilter(group, filter)
88 if err != nil {
89 return Group{}, err
90 }
91
92 // No user entries found.
93 if len(groups) == 0 {
94 return Group{}, ErrNoGroupEntries
95 }
96
97 // Assume the first entry is the "correct" one.
98 return groups[0], nil
99 }
100
101 func GetPasswdPath() (string, error) {
102 return unixPasswdPath, nil
103 }
104
105 func GetPasswd() (io.ReadCloser, error) {
106 return os.Open(unixPasswdPath)
107 }
108
109 func GetGroupPath() (string, error) {
110 return unixGroupPath, nil
111 }
112
113 func GetGroup() (io.ReadCloser, error) {
114 return os.Open(unixGroupPath)
115 }
116
117 // CurrentUser looks up the current user by their user id in /etc/passwd. If the
118 // user cannot be found (or there is no /etc/passwd file on the filesystem),
119 // then CurrentUser returns an error.
120 func CurrentUser() (User, error) {
121 return LookupUid(unix.Getuid())
122 }
123
124 // CurrentGroup looks up the current user's group by their primary group id's
125 // entry in /etc/passwd. If the group cannot be found (or there is no
126 // /etc/group file on the filesystem), then CurrentGroup returns an error.
127 func CurrentGroup() (Group, error) {
128 return LookupGid(unix.Getgid())
129 }
130
131 func currentUserSubIDs(fileName string) ([]SubID, error) {
132 u, err := CurrentUser()
133 if err != nil {
134 return nil, err
135 }
136 filter := func(entry SubID) bool {
137 return entry.Name == u.Name || entry.Name == strconv.Itoa(u.Uid)
138 }
139 return ParseSubIDFileFilter(fileName, filter)
140 }
141
142 func CurrentUserSubUIDs() ([]SubID, error) {
143 return currentUserSubIDs("/etc/subuid")
144 }
145
146 func CurrentUserSubGIDs() ([]SubID, error) {
147 return currentUserSubIDs("/etc/subgid")
148 }
149
150 func CurrentProcessUIDMap() ([]IDMap, error) {
151 return ParseIDMapFile("/proc/self/uid_map")
152 }
153
154 func CurrentProcessGIDMap() ([]IDMap, error) {
155 return ParseIDMapFile("/proc/self/gid_map")
156 }