Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions user/lookup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
// cannot be found (or there is no /etc/passwd file on the filesystem), then
// LookupUser returns an error.
func LookupUser(username string) (User, error) {
return lookupUserFunc(func(u User) bool {
return lookupUserFileFilter(unixPasswdPath, func(u User) bool {
return u.Name == username
})
}
Expand All @@ -29,21 +29,13 @@ func LookupUser(username string) (User, error) {
// be found (or there is no /etc/passwd file on the filesystem), then LookupId
// returns an error.
func LookupUid(uid int) (User, error) {
return lookupUserFunc(func(u User) bool {
return lookupUserFileFilter(unixPasswdPath, func(u User) bool {
return u.Uid == uid
})
}

func lookupUserFunc(filter func(u User) bool) (User, error) {
// Get operating system-specific passwd reader-closer.
passwd, err := GetPasswd()
if err != nil {
return User{}, err
}
defer passwd.Close()

// Get the users.
users, err := ParsePasswdFilter(passwd, filter)
func lookupUserFileFilter(path string, filter func(u User) bool) (User, error) {
users, err := ParsePasswdFileFilter(path, filter)
if err != nil {
return User{}, err
}
Expand All @@ -61,7 +53,7 @@ func lookupUserFunc(filter func(u User) bool) (User, error) {
// be found (or there is no /etc/group file on the filesystem), then LookupGroup
// returns an error.
func LookupGroup(groupname string) (Group, error) {
return lookupGroupFunc(func(g Group) bool {
return lookupGroupFileFilter(unixGroupPath, func(g Group) bool {
return g.Name == groupname
})
}
Expand All @@ -70,21 +62,13 @@ func LookupGroup(groupname string) (Group, error) {
// be found (or there is no /etc/group file on the filesystem), then LookupGid
// returns an error.
func LookupGid(gid int) (Group, error) {
return lookupGroupFunc(func(g Group) bool {
return lookupGroupFileFilter(unixGroupPath, func(g Group) bool {
return g.Gid == gid
})
}

func lookupGroupFunc(filter func(g Group) bool) (Group, error) {
// Get operating system-specific group reader-closer.
group, err := GetGroup()
if err != nil {
return Group{}, err
}
defer group.Close()

// Get the users.
groups, err := ParseGroupFilter(group, filter)
func lookupGroupFileFilter(path string, filter func(g Group) bool) (Group, error) {
groups, err := ParseGroupFileFilter(path, filter)
if err != nil {
return Group{}, err
}
Expand All @@ -98,18 +82,30 @@ func lookupGroupFunc(filter func(g Group) bool) (Group, error) {
return groups[0], nil
}

// GetPasswdPath returns the path to the system passwd file.
func GetPasswdPath() (string, error) {
return unixPasswdPath, nil
}

// GetPasswd opens the system passwd file for reading.
//
// Unlike the file-based parsing functions, it does not restrict the file type
// or limit the amount of data that can be read. The caller is responsible for
// closing the returned reader.
func GetPasswd() (io.ReadCloser, error) {
return os.Open(unixPasswdPath)
}

// GetGroupPath returns the path to the system group file.
func GetGroupPath() (string, error) {
return unixGroupPath, nil
}

// GetGroup opens the system group file for reading.
//
// Unlike the file-based parsing functions, it does not restrict the file type
// or limit the amount of data that can be read. The caller is responsible for
// closing the returned reader.
func GetGroup() (io.ReadCloser, error) {
return os.Open(unixGroupPath)
}
Expand Down
19 changes: 11 additions & 8 deletions user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func ParsePasswd(passwd io.Reader) ([]User, error) {
}

func ParsePasswdFileFilter(path string, filter func(User) bool) ([]User, error) {
passwd, err := os.Open(path)
passwd, err := openUserFile(path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -500,13 +500,16 @@ func GetAdditionalGroups(additionalGroups []string, group io.Reader) ([]int, err
// that opens the groupPath given and gives it as an argument to
// GetAdditionalGroups.
func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int, error) {
var group io.Reader

if groupFile, err := os.Open(groupPath); err == nil {
group = groupFile
defer groupFile.Close()
groupFile, err := openUserFile(groupPath)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
return GetAdditionalGroups(additionalGroups, nil)
}
return GetAdditionalGroups(additionalGroups, group)
defer func() { _ = groupFile.Close() }()

return GetAdditionalGroups(additionalGroups, groupFile)
}

// maxSubID is the maximum subordinate ID (maxof(id_t) in shadow-utils).
Expand All @@ -532,7 +535,7 @@ func ParseSubID(subid io.Reader) ([]SubID, error) {
}

func ParseSubIDFileFilter(path string, filter func(SubID) bool) ([]SubID, error) {
subid, err := os.Open(path)
subid, err := openUserFile(path)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading