2022-10-11 13:38:05 +00:00
|
|
|
//go:build unix
|
|
|
|
|
|
|
|
package users
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/user"
|
2022-12-07 22:02:25 +00:00
|
|
|
"strconv"
|
2022-10-11 13:38:05 +00:00
|
|
|
)
|
|
|
|
|
2022-12-07 22:02:25 +00:00
|
|
|
var (
|
|
|
|
// nobody is a cached copy of the nobody user, which is going to be looked-up
|
|
|
|
// frequently and is unlikely to be modified on the underlying system.
|
|
|
|
nobody user.User
|
|
|
|
|
|
|
|
// nobodyUID is a cached copy of the int value of the nobody user's UID.
|
|
|
|
nobodyUID uint32
|
|
|
|
|
|
|
|
// nobodyGID int is a cached copy of the int value of the nobody users's GID.
|
|
|
|
nobodyGID uint32
|
|
|
|
)
|
2022-10-11 13:38:05 +00:00
|
|
|
|
|
|
|
// Nobody returns User data for the "nobody" user on the system, bypassing the
|
|
|
|
// locking / file read / NSS lookup.
|
|
|
|
func Nobody() user.User {
|
|
|
|
return nobody
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:02:25 +00:00
|
|
|
// NobodyIDs returns the integer UID and GID of the nobody user.
|
|
|
|
func NobodyIDs() (uint32, uint32) {
|
|
|
|
return nobodyUID, nobodyGID
|
|
|
|
}
|
|
|
|
|
2022-10-11 13:38:05 +00:00
|
|
|
func init() {
|
2023-02-09 14:37:50 +00:00
|
|
|
u, err := internalLookupUser("nobody")
|
2022-10-11 13:38:05 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to lookup nobody user: %v", err))
|
|
|
|
}
|
|
|
|
nobody = *u
|
2022-12-07 22:02:25 +00:00
|
|
|
|
|
|
|
uid, err := strconv.Atoi(u.Uid)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to parse nobody UID: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
gid, err := strconv.Atoi(u.Gid)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to parse nobody GID: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
nobodyUID, nobodyGID = uint32(uid), uint32(gid)
|
2022-10-11 13:38:05 +00:00
|
|
|
}
|