blob: e7a25a3b92fdcb025f1af9bb2aef68fbe9cf36d6 (
plain)
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
|
package internal
import (
"bufio"
"errors"
"io"
"os"
"path/filepath"
"strings"
)
// ListUsersWithHomeSubdir provides a list of user names on the current host.
//
// The users returned will only be those with a specifically named subdirectory
// of their home dir, which has the given permission bits.
func ListUsersWithHomeSubdir(subdir string, required_perms os.FileMode) ([]string, error) {
file, err := os.Open("/etc/passwd")
if err != nil {
return nil, err
}
defer func() {
_ = file.Close()
}()
users := []string{}
rdr := bufio.NewReader(file)
for {
line, err := rdr.ReadString('\n')
isEOF := errors.Is(err, io.EOF)
if err != nil && !isEOF {
return nil, err
}
if len(line) != 0 && line[0] == '#' {
continue
}
spl := strings.Split(line, ":")
home := spl[len(spl)-2]
st, err := os.Stat(filepath.Join(home, subdir))
if err != nil || !st.IsDir() || st.Mode()&required_perms != required_perms {
continue
}
users = append(users, spl[0])
if isEOF {
break
}
}
return users, nil
}
|